Added MACS source
[htsworkflow.git] / htswanalysis / MACS / lib / gsl / gsl-1.11 / histogram / calloc_range.c
1 /* gsl_histogram_calloc_range.c
2  * Copyright (C) 2000  Simone Piccardi
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 /***************************************************************
20  *
21  * File gsl_histogram_calloc_range.c: 
22  * Routine to create a variable binning histogram providing 
23  * an input range vector. Need GSL library and header.
24  * Do range check and allocate the histogram data. 
25  *
26  * Author: S. Piccardi
27  * Jan. 2000
28  *
29  ***************************************************************/
30 #include <config.h>
31 #include <stdlib.h>
32 #include <gsl/gsl_errno.h>
33 #include <gsl/gsl_histogram.h>
34
35 gsl_histogram *
36 gsl_histogram_calloc_range (size_t n, double *range)
37 {
38   size_t i;
39   gsl_histogram *h;
40
41   /* check arguments */
42
43   if (n == 0)
44     {
45       GSL_ERROR_VAL ("histogram length n must be positive integer",
46                         GSL_EDOM, 0);
47     }
48
49   /* check ranges */
50
51   for (i = 0; i < n; i++)
52     {
53       if (range[i] >= range[i + 1])
54         {
55           GSL_ERROR_VAL ("histogram bin extremes  must be "
56                             "in increasing order", GSL_EDOM, 0);
57         }
58     }
59
60   /* Allocate histogram  */
61
62   h = (gsl_histogram *) malloc (sizeof (gsl_histogram));
63
64   if (h == 0)
65     {
66       GSL_ERROR_VAL ("failed to allocate space for histogram struct",
67                         GSL_ENOMEM, 0);
68     }
69
70   h->range = (double *) malloc ((n + 1) * sizeof (double));
71
72   if (h->range == 0)
73     {
74       /* exception in constructor, avoid memory leak */
75       free (h);
76       GSL_ERROR_VAL ("failed to allocate space for histogram ranges",
77                         GSL_ENOMEM, 0);
78     }
79
80   h->bin = (double *) malloc (n * sizeof (double));
81
82   if (h->bin == 0)
83     {
84       /* exception in constructor, avoid memory leak */
85       free (h->range);
86       free (h);
87       GSL_ERROR_VAL ("failed to allocate space for histogram bins",
88                         GSL_ENOMEM, 0);
89     }
90
91   /* initialize ranges */
92
93   for (i = 0; i <= n; i++)
94     {
95       h->range[i] = range[i];
96     }
97
98   /* clear contents */
99
100   for (i = 0; i < n; i++)
101     {
102       h->bin[i] = 0;
103     }
104
105   h->n = n;
106
107   return h;
108 }