| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| #include "EXTERN.h" |
| 3 |
|
| #include "perl.h" |
| 4 |
|
| #include "XSUB.h" |
| 5 |
|
| |
| 6 |
|
| #include "assert.h" |
| 7 |
|
| #include "ppport.h" |
| 8 |
|
| |
| 9 |
|
| #include "perl_type_tools.h" |
| 10 |
|
| |
| 11 |
|
| #include "mh_histogram.h" |
| 12 |
|
| |
| 13 |
|
| #define ASSERT_BIN_RANGE(axis, ibin) \ |
| 14 |
|
| STMT_START { \ |
| 15 |
|
| if (ibin < 1 || ibin > MH_AXIS_NBINS(axis)) \ |
| 16 |
|
| croak("Bin %u outside axis bin range (min: 1, max: %u)", MH_AXIS_NBINS(axis)); \ |
| 17 |
|
| } STMT_END |
| 18 |
|
| |
| 19 |
|
| #define ASSERT_BIN_RANGE_WITH_OVERFLOW(axis, ibin) \ |
| 20 |
|
| STMT_START { \ |
| 21 |
|
| if (ibin < 0 || ibin > MH_AXIS_NBINS(axis)+1) \ |
| 22 |
|
| croak("Bin %u outside axis bin range (incl. under- and overflow: min: 0, max: %u)", MH_AXIS_NBINS(axis)+1); \ |
| 23 |
|
| } STMT_END |
| 24 |
|
| |
| 25 |
|
| |
| 26 |
|
| /* The following are flags that we use on the userdata slot of an axis. |
| 27 |
|
| * Right now, that's just using the first bit (take care not to use more than 32...) |
| 28 |
|
| * indicating that if set, the axis is owned by a histogram. If that's the case, |
| 29 |
|
| * using that axis in another histogram will create a clone of the axis. |
| 30 |
|
| * At the same time, any explicit Perl-level reference to the axis will not free |
| 31 |
|
| * the underlying C object if that bit is set as the Perl-level reference goes out of |
| 32 |
|
| * scope. */ |
| 33 |
|
| #define F_AXIS_OWNED_BY_HIST 1 |
| 34 |
|
| |
| 35 |
|
| static SV * |
| 36 |
4 |
| axis_to_hashref(pTHX_ mh_axis_t *axis) |
| 37 |
|
| { |
| 38 |
|
| SV *rv; |
| 39 |
|
| HV *hash; |
| 40 |
4 |
| hash = newHV(); |
| 41 |
|
| |
| 42 |
4 |
▶ | if (MH_AXIS_ISFIXBIN(axis)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (MH_AXIS_ISFIXBIN(axis)) { |
1 |
3 |
4 |
|
| 43 |
|
| assert( hv_stores(hash, "nbins", newSVuv(MH_AXIS_NBINS(axis))) ); |
| 44 |
|
| assert( hv_stores(hash, "min", newSVnv(MH_AXIS_MIN(axis))) ); |
| 45 |
|
| assert( hv_stores(hash, "max", newSVnv(MH_AXIS_MAX(axis))) ); |
| 46 |
|
| } |
| 47 |
|
| else { |
| 48 |
|
| unsigned int i, n; |
| 49 |
|
| AV *bin_av; |
| 50 |
|
| double *bins = axis->bins; |
| 51 |
1 |
| n = MH_AXIS_NBINS(axis); |
| 52 |
1 |
| bin_av = newAV(); |
| 53 |
|
| assert( hv_stores(hash, "bins", newRV_noinc((SV *)bin_av)) ); |
| 54 |
1 |
| av_extend(bin_av, n); |
| 55 |
6 |
▶ | for (i = 0; i <= n; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= n; ++i) |
5 |
1 |
6 |
|
| 56 |
5 |
| av_store(bin_av, i, newSVnv(bins[i])); |
| 57 |
|
| } |
| 58 |
4 |
| rv = newRV_noinc((SV *)hash); |
| 59 |
|
| |
| 60 |
4 |
| return rv; |
| 61 |
|
| } |
| 62 |
|
| |
| 63 |
|
| static mh_axis_t * |
| 64 |
1 |
| hash_to_axis(pTHX_ HV *hash) |
| 65 |
|
| { |
| 66 |
|
| unsigned int nbins; |
| 67 |
|
| SV *tmp; |
| 68 |
|
| SV **svptr; |
| 69 |
|
| mh_axis_t *rv; |
| 70 |
|
| |
| 71 |
1 |
▶ | if (hv_exists(hash, "bins", 4)) { /* varbins */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (hv_exists(hash, "bins", 4)) { /* varbins */ |
0 |
1 |
1 |
|
| 72 |
|
| AV *bin_av; |
| 73 |
0 |
| tmp = *hv_fetchs(hash, "bins", 0); |
| 74 |
0 |
▶ | DEREF_RV_TO_AV(bin_av, tmp); |
Branch: 2 branches
| Branch | True | False | Total |
| DEREF_RV_TO_AV(bin_av, tmp); |
0 |
0 |
0 |
| DEREF_RV_TO_AV(bin_av, tmp); |
0 |
0 |
0 |
|
| 75 |
|
| if (bin_av == NULL) |
| 76 |
0 |
| croak("'bins' entry is not an array reference"); |
| 77 |
0 |
| nbins = av_len(bin_av); |
| 78 |
0 |
| rv = mh_axis_create( nbins, MH_AXIS_OPT_VARBINS ); |
| 79 |
0 |
▶ | if (rv == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv == NULL) |
0 |
0 |
0 |
|
| 80 |
0 |
| croak("Cannot create Math::Histogram::Axis! Invalid number of bins or out of memory."); |
| 81 |
0 |
| av_to_double_ary(aTHX_ bin_av, rv->bins); |
| 82 |
|
| /* FIXME include same bin order sanity check as for the normal constructor? */ |
| 83 |
0 |
| mh_axis_init( rv, rv->bins[0], rv->bins[nbins] ); |
| 84 |
|
| } |
| 85 |
|
| else { /* fixed width bins */ |
| 86 |
|
| double min, max; |
| 87 |
|
| svptr = hv_fetchs(hash, "nbins", 0); |
| 88 |
1 |
▶ | if (svptr == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (svptr == NULL) |
1 |
0 |
1 |
|
| 89 |
1 |
| croak("Missing 'bins' and 'nbins' hash entries"); |
| 90 |
0 |
| nbins = SvUV(*svptr); |
| 91 |
|
| svptr = hv_fetchs(hash, "min", 0); |
| 92 |
0 |
▶ | if (svptr == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (svptr == NULL) |
0 |
0 |
0 |
|
| 93 |
0 |
| croak("Missing 'min' hash entry"); |
| 94 |
0 |
| min = SvNV(*svptr); |
| 95 |
|
| svptr = hv_fetchs(hash, "max", 0); |
| 96 |
0 |
▶ | if (svptr == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (svptr == NULL) |
0 |
0 |
0 |
|
| 97 |
0 |
| croak("Missing 'max' hash entry"); |
| 98 |
0 |
| max = SvNV(*svptr); |
| 99 |
0 |
▶ | if (min > max) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (min > max) { |
0 |
0 |
0 |
|
| 100 |
|
| double tmp = min; |
| 101 |
|
| min = max; |
| 102 |
|
| max = tmp; |
| 103 |
|
| } |
| 104 |
0 |
| rv = mh_axis_create( nbins, MH_AXIS_OPT_FIXEDBINS ); |
| 105 |
0 |
▶ | if (rv == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv == NULL) |
0 |
0 |
0 |
|
| 106 |
0 |
| croak("Cannot create Math::Histogram::Axis! Invalid bin number or out of memory."); |
| 107 |
0 |
| mh_axis_init( rv, min, max ); |
| 108 |
|
| } |
| 109 |
|
| |
| 110 |
0 |
| return rv; |
| 111 |
|
| } |
| 112 |
|
| |
| 113 |
|
| /* |
| 114 |
|
| * FIXME This file has a bunch of hardcoded class names for non-constructor methods |
| 115 |
|
| * that return objects. That needs to be fixed! |
| 116 |
|
| */ |
| 117 |
|
| |
| 118 |
|
| MODULE = Math::Histogram PACKAGE = Math::Histogram |
| 119 |
|
| |
| 120 |
|
| PROTOTYPES: DISABLE |
| 121 |
|
| |
| 122 |
|
| REQUIRE: 2.21 |
| 123 |
|
| |
| 124 |
|
| INCLUDE: XS/Axis.xs |
| 125 |
|
| |
| 126 |
|
| INCLUDE: XS/Histogram.xs |