LibRan  0.1
Pseudo-random number distribution generator
LRbin.c
Go to the documentation of this file.
1 
58 /*
59  * Copyright 2019 R.K. Owen, Ph.D.
60  * License see lgpl.md (Gnu Lesser General Public License)
61  */
62 #ifdef __cplusplus
63 extern "C" {
64 #endif
65 
66 #include <stdlib.h>
67 #include <math.h> /* isnan() */
68 #include "libran.h"
69 
81 LR_bin *LR_bin_new(int n) {
82  LR_bin *ptr = (void *) NULL;
83 
84  if (!(ptr = (LR_bin *) malloc(sizeof(LR_bin))))
85  return ptr;
86  ptr->n = n;
87  ptr->nn = 1; /* always start with one bin */
88  ptr->c = 0;
89  ptr->errno = 0;
90 
91  if (!(ptr->bdrs = (double *) calloc(n, sizeof(double))))
92  goto bad0;
93 
94  if (!(ptr->bins = (long *) calloc(n, sizeof(long))))
95  goto bad1;
96 
97  return ptr;
98 
99 bad1:
100  free((void *) ptr->bdrs);
101 
102 bad0:
103  free((void *) ptr);
104  return (void *) NULL;
105 }
106 
116 int LR_bin_rm(LR_bin **b) {
117  if (b && *b) {
118  free((void *) (*b)->bins);
119  free((void *) (*b)->bdrs);
120  free((void *) *b);
121  *b = (LR_bin *) NULL;
122  return LRerr_OK;
123  }
124  return LRerr_BinGeneric;
125 }
126 
137 int LR_bin_set(LR_bin *b, double x) {
138  int i = 0;
139  double t;
140  if (b->n < b->nn + 1) {
141  /* too many values given */
142  return b->errno = LRerr_TooManyValues;
143  }
144  t = b->bdrs[0];
145  for (int i = 0, i1 = 1; i <= b->nn; i++,i1++) {
146  if (b->nn == i1) { /* got to top entry */
147  b->bdrs[i] = x;
148  b->nn++;
149  return LRerr_OK;
150  }
151  if (x > t) {
152  /* compare to next one, next round */
153  t = b->bdrs[i1];
154  } else {
155  /* insert here, but keep current */
156  t = b->bdrs[i];
157  b->bdrs[i] = x;
158  /* use current for comparison */
159  x = t;
160  t = b->bdrs[i1];
161  }
162  }
163  b->nn++;
164 
165  return LRerr_OK;
166 }
167 
178 int LR_bin_add(LR_bin *b, double x) {
179  int i = 0;
180 
181  if (isnan(x))
182  return b->errno = LRerr_InvalidInputValue;
183 
184  while (i <= b->nn - 1) {
185  if (x < b->bdrs[i]
186  || i == b->nn - 1) {
187  b->bins[i]++;
188  break;
189  }
190  i++;
191  }
192  if (i >= b->nn)
193  return b->errno = LRerr_InvalidRange;
194  b->c++;
195 
196  return LRerr_OK;
197 }
198 
199 #ifdef __cplusplus
200 }
201 #endif
#define LRerr_OK
Definition: libran.h:32
#define LRerr_BinGeneric
Definition: libran.h:39
long * bins
Definition: libran.h:189
#define LRerr_TooManyValues
Definition: libran.h:40
int LR_bin_rm(LR_bin **b)
LR_bin_rm(LR_bin **b) - remove binning object.
Definition: LRbin.c:116
int LR_bin_add(LR_bin *b, double x)
LR_bin_add(LR_bin *b, double x) - collect value to be binned.
Definition: LRbin.c:178
int errno
Definition: libran.h:190
#define LRerr_InvalidRange
Definition: libran.h:42
LR_bin * LR_bin_new(int n)
LR_bin_new(LR_data_type d, int n) - create new binning object.
Definition: LRbin.c:81
int n
Definition: libran.h:185
the binning object - for tallying results
Definition: libran.h:184
long c
Definition: libran.h:187
int nn
Definition: libran.h:186
The LibRan common header file.
#define LRerr_InvalidInputValue
Definition: libran.h:41
double * bdrs
Definition: libran.h:188
int LR_bin_set(LR_bin *b, double x)
LR_bin_set(LR_bin *b, double x) - add bin boundary.
Definition: LRbin.c:137