XS.xs

filter: Statementstmt 83.8% 161 / 192 Branchbran 68.3% 97 / 142 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 77.2% 258 / 334 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« XS/construction.xs ↑ Summary const-c.inc »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #include "ppport.h"
7  
8 #include "histogram.h"
9 #include "histo_perl_interf.h"
10  
11 #include "mt.h"
12 #include "const-c.inc"
13  
14 /* More HS_* macros to be found in histogram.h. Those here
15 * are more chummy with perl than those in histogram.h, which only
16 * currently use the memory allocation macros of perl. */
17  
18 /* Ideally, HS_ASSERT_BIN_RANGE would be part of the histogram.h
19 * API, but given that we know the unsigned data type here AND have
20 * access to perl's croak conveniently, that seems premature cleanup. */
21 #define HS_ASSERT_BIN_RANGE(self, i) STMT_START { \
22 if (/* i < 0 || */ i >= self->nbins) { \
23 croak("Bin %u outside histogram range (highest bin index is %u", i, self->nbins); \
24 } } STMT_END
25  
26 #define HS_CLONE_GET_CLASS(classname, src, where) STMT_START { \
27 if (!sv_isobject(src)) \
28 croak("Cannot call " #where "() on non-object"); \
29 classname = sv_reftype(SvRV(src), TRUE); \
30 if ( !sv_isobject(src) || (SvTYPE(SvRV(src)) != SVt_PVMG) ) \
31 croak( "%s::" #where "() -- self is not a blessed SV reference", classname); \
32 } STMT_END
33  
34  
35 /* The following couple of lines are for the RNG, taken from Math::Random::MT */
36 typedef struct mt* Math__SimpleHisto__XS__RNG;
37  
38 void*
39 11 U32ArrayPtr (pTHX_ int n)
40 {
41 11 SV * sv = sv_2mortal( NEWSV( 0, n*sizeof(U32) ) );
42 11 return SvPVX(sv);
43 }
44  
45  
46 MODULE = Math::SimpleHisto::XS PACKAGE = Math::SimpleHisto::XS
47  
48 REQUIRE: 2.2201
49  
50 INCLUDE: const-xs.inc
51  
52 INCLUDE: XS/rdgen.xs
53  
54 INCLUDE: XS/construction.xs
55  
56 INCLUDE: XS/bins.xs
57  
58 INCLUDE: XS/aggregate.xs
59  
60 void
61 DESTROY(self)
62 simple_histo_1d* self
63 CODE:
64 54 ▶ HS_DEALLOCATE(self);
Branch: 1 branch
BranchTrueFalseTotal
HS_DEALLOCATE(self); 19 35 54
65  
66 void
67 multiply_constant(self, factor = 1.)
68 simple_histo_1d* self
69 double factor
70 CODE:
71 2 ▶ if (factor < 0.)
Branch: 1 branch
BranchTrueFalseTotal
if (factor < 0.) 0 2 2
72 0 croak("Cannot multiply histogram with negative value %f", factor);
73 2 histo_multiply_constant(self, factor);
74  
75 void
76 add_histogram(self, operand)
77 simple_histo_1d* self
78 simple_histo_1d* operand
79 ALIAS:
80 subtract_histogram = 1
81 multiply_histogram = 2
82 divide_histogram = 3
83 PREINIT:
84 bool ok;
85 CODE:
86 6 ▶ if (ix == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ix == 0) 2 4 6
87 2 ok = histo_add_histogram(self, operand);
88 4 ▶ else if (ix == 1)
Branch: 1 branch
BranchTrueFalseTotal
else if (ix == 1) 2 2 4
89 2 ok = histo_subtract_histogram(self, operand);
90 2 ▶ else if (ix == 2)
Branch: 1 branch
BranchTrueFalseTotal
else if (ix == 2) 1 1 2
91 1 ok = histo_multiply_histogram(self, operand);
92 else
93 1 ok = histo_divide_histogram(self, operand);
94  
95 6 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 0 6 6
96 char *reason;
97 if (ix == 0)
98 reason = "add";
99 else if (ix == 1)
100 reason = "subtract";
101 else if (ix == 2)
102 reason = "multiply";
103 else
104 reason = "divide";
105 0 croak("Failed to %s incompatible histogram. Binning not the same?", reason);
106 }
107  
108 void
109 normalize(self, normalization = 1.)
110 simple_histo_1d* self
111 double normalization
112 CODE:
113 1 ▶ if (normalization <= 0.)
Branch: 1 branch
BranchTrueFalseTotal
if (normalization <= 0.) 0 1 1
114 0 croak("Cannot normalize to %f", normalization);
115 2 ▶ if (self->total == 0.)
Branch: 1 branch
BranchTrueFalseTotal
if (self->total == 0.) 0 2 2
116 0 croak("Cannot normalize histogram without data");
117 2 histo_multiply_constant(self, normalization / self->total);
118  
119 void
120 fill(self, ...)
121 simple_histo_1d* self
122 CODE:
123 4053 ▶ if (items == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 2) { 23 4030 4053
124 23 SV* const x_tmp = ST(1);
125 23 SvGETMAGIC(x_tmp);
126 25 ▶ if (SvROK(x_tmp) && SvTYPE(SvRV(x_tmp)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(x_tmp) && SvTYPE(SvRV(x_tmp)) == SVt_PVAV) { 2 21 23
if (SvROK(x_tmp) && SvTYPE(SvRV(x_tmp)) == SVt_PVAV) { 2 0 2
127 int i, n;
128 SV** sv;
129 double* x;
130 AV* av = (AV*)SvRV(x_tmp);
131 2 n = av_len(av);
132 2 Newx(x, n+1, double);
133 11 ▶ for (i = 0; i <= n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= n; ++i) { 9 2 11
134 9 sv = av_fetch(av, i, 0);
135 9 ▶ if (UNLIKELY( sv == NULL )) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY( sv == NULL )) { 0 9 9
136 0 Safefree(x);
137 0 croak("Shouldn't happen");
138 }
139 9 x[i] = SvNV(*sv);
140 }
141 2 histo_fill(self, n+1, x, NULL);
142 2 Safefree(x);
143 }
144 else {
145 21 double x = SvNV(ST(1));
146 21 histo_fill(self, 1, &x, NULL);
147 }
148 }
149 4030 ▶ else if (items == 3) {
Branch: 1 branch
BranchTrueFalseTotal
else if (items == 3) { 4030 0 4030
150 4030 SV* const x_tmp = ST(1);
151 4030 SV* const w_tmp = ST(2);
152 4030 SvGETMAGIC(x_tmp);
153 4030 SvGETMAGIC(w_tmp);
154 4033 ▶ if (SvROK(x_tmp) && SvTYPE(SvRV(x_tmp)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(x_tmp) && SvTYPE(SvRV(x_tmp)) == SVt_PVAV) { 3 4027 4030
if (SvROK(x_tmp) && SvTYPE(SvRV(x_tmp)) == SVt_PVAV) { 3 0 3
155 int i, n;
156 SV** sv;
157 double *x, *w;
158 AV *xav, *wav;
159 3 ▶ if (UNLIKELY( !SvROK(w_tmp) || SvTYPE(SvRV(x_tmp)) != SVt_PVAV )) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY( !SvROK(w_tmp) || SvTYPE(SvRV(x_tmp)) != SVt_PVAV )) { 0 3 3
160 0 croak("Need array of weights if using array of x values");
161 }
162 xav = (AV*)SvRV(x_tmp);
163 3 wav = (AV*)SvRV(w_tmp);
164 3 n = av_len(xav);
165 3 ▶ if (UNLIKELY( av_len(wav) != n )) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY( av_len(wav) != n )) { 0 3 3
166 0 croak("x and w array lengths differ");
167 }
168  
169 3 Newx(x, n+1, double);
170 3 Newx(w, n+1, double);
171 8 ▶ for (i = 0; i <= n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= n; ++i) { 5 3 8
172 5 sv = av_fetch(xav, i, 0);
173 5 ▶ if (UNLIKELY( sv == NULL )) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY( sv == NULL )) { 0 5 5
174 0 Safefree(x);
175 0 Safefree(w);
176 0 croak("Shouldn't happen");
177 }
178 5 x[i] = SvNV(*sv);
179  
180 5 sv = av_fetch(wav, i, 0);
181 5 ▶ if (UNLIKELY( sv == NULL )) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY( sv == NULL )) { 0 5 5
182 0 Safefree(x);
183 0 Safefree(w);
184 0 croak("Shouldn't happen");
185 }
186 5 w[i] = SvNV(*sv);
187 }
188 3 histo_fill(self, n+1, x, w);
189 3 Safefree(x);
190 3 Safefree(w);
191 }
192 else {
193 4027 double x = SvNV(ST(1));
194 4027 double w = SvNV(ST(2));
195 4027 histo_fill(self, 1, &x, &w);
196 }
197 }
198 else {
199 0 croak("Invalid number of arguments to fill(self, ...)");
200 }
201  
202 void
203 fill_by_bin(self, ...)
204 simple_histo_1d* self
205 CODE:
206 6 ▶ HS_INVALIDATE_CUMULATIVE(self);
Branch: 2 branches
BranchTrueFalseTotal
HS_INVALIDATE_CUMULATIVE(self); 0 6 6
HS_INVALIDATE_CUMULATIVE(self); 0 0 0
207 6 ▶ if (items == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 2) { 3 3 6
208 3 SV* const x_tmp = ST(1);
209 3 SvGETMAGIC(x_tmp);
210 4 ▶ if (SvROK(x_tmp) && SvTYPE(SvRV(x_tmp)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(x_tmp) && SvTYPE(SvRV(x_tmp)) == SVt_PVAV) { 1 2 3
if (SvROK(x_tmp) && SvTYPE(SvRV(x_tmp)) == SVt_PVAV) { 1 0 1
211 int i, n;
212 SV** sv;
213 int* ibin;
214 AV* av = (AV*)SvRV(x_tmp);
215 1 n = av_len(av);
216 1 Newx(ibin, n+1, int);
217 3 ▶ for (i = 0; i <= n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= n; ++i) { 2 1 3
218 2 sv = av_fetch(av, i, 0);
219 2 ▶ if (sv == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv == NULL) { 0 2 2
220 0 Safefree(ibin);
221 0 croak("Shouldn't happen");
222 }
223 2 ibin[i] = SvIV(*sv);
224 }
225 1 histo_fill_by_bin(self, n+1, ibin, NULL);
226 1 Safefree(ibin);
227 }
228 else {
229 2 const int ibin = SvUV(ST(1));
230 2 histo_fill_by_bin(self, 1, &ibin, NULL);
231 }
232 }
233 3 ▶ else if (items == 3) {
Branch: 1 branch
BranchTrueFalseTotal
else if (items == 3) { 3 0 3
234 3 SV* const x_tmp = ST(1);
235 3 SV* const w_tmp = ST(2);
236 3 SvGETMAGIC(x_tmp);
237 3 SvGETMAGIC(w_tmp);
238 4 ▶ if (SvROK(x_tmp) && SvTYPE(SvRV(x_tmp)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(x_tmp) && SvTYPE(SvRV(x_tmp)) == SVt_PVAV) { 1 2 3
if (SvROK(x_tmp) && SvTYPE(SvRV(x_tmp)) == SVt_PVAV) { 1 0 1
239 int i, n;
240 SV** sv;
241 int *ibin;
242 double *w;
243 AV *xav, *wav;
244 1 ▶ if (!SvROK(w_tmp) || SvTYPE(SvRV(x_tmp)) != SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(w_tmp) || SvTYPE(SvRV(x_tmp)) != SVt_PVAV) { 0 1 1
245 0 croak("Need array of weights if using array of bin numbers");
246 }
247 xav = (AV*)SvRV(x_tmp);
248 1 wav = (AV*)SvRV(w_tmp);
249 1 n = av_len(xav);
250 1 ▶ if (av_len(wav) != n) {
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(wav) != n) { 0 1 1
251 0 croak("ibin and w array lengths differ");
252 }
253  
254 1 Newx(ibin, n+1, int);
255 1 Newx(w, n+1, double);
256 3 ▶ for (i = 0; i <= n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= n; ++i) { 2 1 3
257 2 sv = av_fetch(xav, i, 0);
258 2 ▶ if (sv == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv == NULL) { 0 2 2
259 0 Safefree(ibin);
260 0 Safefree(w);
261 0 croak("Shouldn't happen");
262 }
263 2 ibin[i] = SvIV(*sv);
264  
265 2 sv = av_fetch(wav, i, 0);
266 2 ▶ if (sv == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv == NULL) { 0 2 2
267 0 Safefree(ibin);
268 0 Safefree(w);
269 0 croak("Shouldn't happen");
270 }
271 2 w[i] = SvNV(*sv);
272 }
273 1 histo_fill_by_bin(self, n+1, ibin, w);
274 1 Safefree(ibin);
275 1 Safefree(w);
276 }
277 else {
278 2 int ibin = SvIV(ST(1));
279 2 double w = SvNV(ST(2));
280 2 histo_fill_by_bin(self, 1, &ibin, &w);
281 }
282 }
283 else {
284 0 croak("Invalid number of arguments to fill(self, ...)");
285 }
286  
287  
288 double
289 min(self)
290 simple_histo_1d* self
291 CODE:
292 4042 ▶ RETVAL = self->min;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->min; 4024 18 4042
293 OUTPUT: RETVAL
294  
295 double
296 max(self)
297 simple_histo_1d* self
298 CODE:
299 44 ▶ RETVAL = self->max;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->max; 27 17 44
300 OUTPUT: RETVAL
301  
302 double
303 width(self)
304 simple_histo_1d* self
305 CODE:
306 4024 ▶ RETVAL = self->width;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->width; 4018 6 4024
307 OUTPUT: RETVAL
308  
309 double
310 overflow(self)
311 simple_histo_1d* self
312 CODE:
313 54 ▶ RETVAL = self->overflow;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->overflow; 38 16 54
314 OUTPUT: RETVAL
315  
316 double
317 underflow(self)
318 simple_histo_1d* self
319 CODE:
320 54 ▶ RETVAL = self->underflow;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->underflow; 42 12 54
321 OUTPUT: RETVAL
322  
323 double
324 total(self)
325 simple_histo_1d* self
326 CODE:
327 71 ▶ RETVAL = self->total;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->total; 34 37 71
328 OUTPUT: RETVAL
329  
330 unsigned int
331 nbins(self)
332 simple_histo_1d* self
333 CODE:
334 59 ▶ RETVAL = self->nbins;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->nbins; 40 19 59
335 OUTPUT: RETVAL
336  
337 unsigned int
338 highest_bin(self)
339 simple_histo_1d* self
340 CODE:
341 /* I know. Trivial, but convienient! */
342 0 ▶ RETVAL = self->nbins-1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->nbins-1; 0 0 0
343 OUTPUT: RETVAL
344  
345 double
346 binsize(self, ibin = 0)
347 simple_histo_1d* self
348 unsigned int ibin
349 CODE:
350 45 ▶ HS_ASSERT_BIN_RANGE(self, ibin);
Branch: 1 branch
BranchTrueFalseTotal
HS_ASSERT_BIN_RANGE(self, ibin); 4 41 45
351 41 ▶ if (self->bins == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (self->bins == NULL) 23 18 41
352 23 RETVAL = self->binsize;
353 else
354 18 RETVAL = self->bins[ibin+1] - self->bins[ibin];
355 OUTPUT: RETVAL
356  
357 unsigned int
358 nfills(self)
359 simple_histo_1d* self
360 CODE:
361 56 ▶ RETVAL = self->nfills;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->nfills; 27 29 56
362 OUTPUT: RETVAL
363  
364  
365 void
366 all_bin_contents(self)
367 simple_histo_1d* self
368 PREINIT:
369 SV* rv;
370 PPCODE:
371 rv = histo_data_av(aTHX_ self);
372 46 ▶ XPUSHs(sv_2mortal(rv));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(rv)); 0 46 46
373  
374 void
375 set_all_bin_contents(self, new_data)
376 simple_histo_1d* self
377 AV* new_data
378 PREINIT:
379 unsigned int n, i;
380 double* data;
381 SV** elem;
382 CODE:
383 /* While this would be nicer in the histogram API, it will be much faster
384 * to access the AV* on the fly instead of doing blanket conversion to remove
385 * dependence on perl data structures, so this stays here for the time being. */
386 11 ▶ HS_INVALIDATE_CUMULATIVE(self);
Branch: 2 branches
BranchTrueFalseTotal
HS_INVALIDATE_CUMULATIVE(self); 0 11 11
HS_INVALIDATE_CUMULATIVE(self); 0 0 0
387 11 n = self->nbins;
388 11 ▶ if ((unsigned int)(av_len(new_data)+1) != n) {
Branch: 1 branch
BranchTrueFalseTotal
if ((unsigned int)(av_len(new_data)+1) != n) { 0 11 11
389 0 croak("Length of new data is %u, size of histogram is %u. That doesn't work.", (unsigned int)(av_len(new_data)+1), n);
390 }
391 11 data = self->data;
392 154 ▶ for (i = 0; i < n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; ++i) { 143 11 154
393 143 elem = av_fetch(new_data, i, 0);
394 143 ▶ if (elem == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (elem == NULL) { 0 143 143
395 0 croak("Shouldn't happen");
396 }
397 143 self->total -= data[i];
398 143 data[i] = SvNV(*elem);
399 143 self->total += data[i];
400 }
401  
402 double
403 bin_content(self, ibin)
404 simple_histo_1d* self
405 unsigned int ibin
406 CODE:
407 1045 ▶ HS_ASSERT_BIN_RANGE(self, ibin);
Branch: 1 branch
BranchTrueFalseTotal
HS_ASSERT_BIN_RANGE(self, ibin); 0 1045 1045
408 1045 ▶ RETVAL = self->data[ibin];
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->data[ibin]; 745 300 1045
409 OUTPUT: RETVAL
410  
411 void
412 set_bin_content(self, ibin, content)
413 simple_histo_1d* self
414 unsigned int ibin
415 double content
416 PPCODE:
417 /* Would be nicer in the API, but again, this is faster. */
418 57 ▶ HS_ASSERT_BIN_RANGE(self, ibin);
Branch: 1 branch
BranchTrueFalseTotal
HS_ASSERT_BIN_RANGE(self, ibin); 0 57 57
419 57 ▶ HS_INVALIDATE_CUMULATIVE(self);
Branch: 2 branches
BranchTrueFalseTotal
HS_INVALIDATE_CUMULATIVE(self); 0 57 57
HS_INVALIDATE_CUMULATIVE(self); 0 0 0
420 57 self->total += content - self->data[ibin];
421 57 self->data[ibin] = content;
422  
423 void
424 set_underflow(self, content)
425 simple_histo_1d* self
426 double content
427 PPCODE:
428 /* This doesn't invalidate the INTERNAL cumulative histo */
429 13 self->underflow = content;
430  
431 void
432 set_overflow(self, content)
433 simple_histo_1d* self
434 double content
435 PPCODE:
436 /* This doesn't invalidate the INTERNAL cumulative histo */
437 13 self->overflow = content;
438  
439  
440 void
441 set_nfills(self, nfills)
442 simple_histo_1d* self
443 unsigned int nfills
444 PPCODE:
445 /* This doesn't invalidate the INTERNAL cumulative histo */
446 12 self->nfills = nfills;
447  
448  
449 #void
450 #binary_dump(self)
451 # simple_histo_1d* self
452 # PREINIT:
453 # char* out;
454 # SV* outSv;
455 # double* tmp;
456 # unsigned int size;
457 # PPCODE:
458 # size = sizeof(simple_histo_1d) + sizeof(double)*self->nbins;
459 # outSv = newSVpvs("");
460 # SvGROW(outSv, size+1);
461 # printf(" %u\n", SvLEN(outSv));
462 # out = SvPVX(outSv);
463 # SvLEN_set(outSv, size);
464 # printf("%u\n", SvLEN(outSv));
465 # /*Newx(out, size+1, char);*/
466 # tmp = self->data;
467 # self->data = NULL;
468 # Copy(self, out, sizeof(simple_histo_1d), char);
469 # Copy(tmp, out+sizeof(simple_histo_1d), sizeof(double)*self->nbins, char);
470 # out[size] = '\0';
471 # printf("%u\n", SvLEN(outSv));
472 # self->data = tmp;
473 # XPUSHs(sv_2mortal(outSv));
474  
475  
476 void
477 rand(self, ...)
478 simple_histo_1d* self
479 PREINIT:
480 double rndval;
481 double retval;
482 SV* rngsv;
483 Math__SimpleHisto__XS__RNG rng;
484 unsigned int ibin;
485 PREINIT:
486 simple_histo_1d* cum_hist;
487 PPCODE:
488 400 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 200 200 400
489 200 rngsv = ST(1);
490 }
491 else {
492 200 rngsv = get_sv("Math::SimpleHisto::XS::RNG::Gen", 0);
493 200 ▶ if (rngsv == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rngsv == 0) { 0 200 200
494 0 croak("Cannot find default random number generator!");
495 }
496 }
497 400 ▶ if (sv_derived_from(rngsv, "Math::SimpleHisto::XS::RNG")) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(rngsv, "Math::SimpleHisto::XS::RNG")) { 400 0 400
498 400 IV tmp = SvIV((SV*)SvRV(rngsv));
499 400 rng = INT2PTR(Math__SimpleHisto__XS__RNG, tmp);
500 }
501 else
502 0 Perl_croak(aTHX_ "%s: %s is not of type %s",
503 "Math::SimpleHisto::XS::rand",
504 "rng", "Math::SimpleHisto::XS::RNG");
505 400 rndval = mt_genrand(rng);
506  
507 /* Get the properly normalized internal cumulative */
508 400 ▶ HS_ASSERT_CUMULATIVE(self);
Branch: 1 branch
BranchTrueFalseTotal
HS_ASSERT_CUMULATIVE(self); 2 398 400
509 400 cum_hist = self->cumulative_hist;
510  
511 /* This all operates on the cumulative histogram */
512 400 ▶ ibin = rndval < cum_hist->data[0] ? 0 : find_bin_nonconstant(rndval, cum_hist->nbins, cum_hist->data);
Branch: 1 branch
BranchTrueFalseTotal
ibin = rndval < cum_hist->data[0] ? 0 : find_bin_nonconstant(rndval, cum_hist->nbins, cum_hist->data); 400 0 400
513 400 ▶ if (cum_hist->bins == 0) { /* constant bin size */
Branch: 1 branch
BranchTrueFalseTotal
if (cum_hist->bins == 0) { /* constant bin size */ 200 200 400
514 200 retval = cum_hist->min + cum_hist->binsize * (double)(ibin+1);
515 200 ▶ if (rndval > cum_hist->data[ibin]) {
Branch: 1 branch
BranchTrueFalseTotal
if (rndval > cum_hist->data[ibin]) { 200 0 200
516 200 retval += cum_hist->binsize * (rndval - cum_hist->data[ibin])
517 200 / (cum_hist->data[ibin+1] - cum_hist->data[ibin]);
518 }
519 }
520 else { /* variable bin size */
521 200 retval = cum_hist->bins[ibin+1];
522 200 ▶ if (rndval > cum_hist->data[ibin]) {
Branch: 1 branch
BranchTrueFalseTotal
if (rndval > cum_hist->data[ibin]) { 200 0 200
523 200 retval += (cum_hist->bins[ibin+1] - cum_hist->bins[ibin])
524 200 * (rndval - cum_hist->data[ibin])
525 200 / (cum_hist->data[ibin+1] - cum_hist->data[ibin]);
526 }
527 }
528 400 ▶ XPUSHs(sv_2mortal(newSVnv(retval)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVnv(retval))); 0 400 400
529  
530  
531 void
532 _get_info(self)
533 simple_histo_1d* self
534 PREINIT:
535 SV* data_ary;
536 SV* bins_ary;
537 PPCODE:
538 /* min, max, nbins, nfills, overflow, underflow, dataref, binsref*/
539 11 ▶ EXTEND(SP, 8);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 8); 0 11 11
540 11 mPUSHn(self->min);
541 11 mPUSHn(self->max);
542 11 mPUSHu(self->nbins);
543 11 mPUSHu(self->nfills);
544 11 mPUSHn(self->overflow);
545 11 mPUSHn(self->underflow);
546 data_ary = histo_data_av(aTHX_ self);
547 11 ▶ XPUSHs(sv_2mortal(data_ary));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(data_ary)); 0 11 11
548 11 ▶ if (self->bins == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (self->bins == NULL) 5 6 11
549 bins_ary = &PL_sv_undef;
550 else
551 5 bins_ary = sv_2mortal(histo_bins_av(aTHX_ self));
552 11 ▶ XPUSHs(bins_ary);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(bins_ary); 0 11 11
553  
« XS/construction.xs ↑ Summary const-c.inc »