| 1 |
|
| #include "mh_axis.h" |
| 2 |
|
| |
| 3 |
|
| #include <stdlib.h> |
| 4 |
|
| #include <string.h> |
| 5 |
|
| #include <float.h> |
| 6 |
|
| |
| 7 |
|
| mh_axis_t * |
| 8 |
36 |
| mh_axis_create(unsigned int nbins, unsigned short have_varbins) |
| 9 |
|
| { |
| 10 |
|
| mh_axis_t *axis; |
| 11 |
36 |
| axis = (mh_axis_t *)malloc(sizeof(mh_axis_t)); |
| 12 |
36 |
▶ | if (axis == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (axis == NULL) |
0 |
36 |
36 |
|
| 13 |
|
| return NULL; |
| 14 |
36 |
| axis->nbins = nbins; |
| 15 |
|
| |
| 16 |
36 |
▶ | if (have_varbins != MH_AXIS_OPT_FIXEDBINS) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (have_varbins != MH_AXIS_OPT_FIXEDBINS) { |
11 |
25 |
36 |
|
| 17 |
11 |
| axis->bins = (double *)malloc(sizeof(double) * (nbins+1)); |
| 18 |
11 |
▶ | if (axis->bins == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (axis->bins == NULL) { |
0 |
11 |
11 |
|
| 19 |
0 |
| free(axis); |
| 20 |
0 |
| return NULL; |
| 21 |
|
| } |
| 22 |
|
| } |
| 23 |
|
| else { |
| 24 |
25 |
| axis->bins = NULL; |
| 25 |
|
| } |
| 26 |
|
| |
| 27 |
36 |
| axis->userdata = NULL; |
| 28 |
|
| |
| 29 |
36 |
| return axis; |
| 30 |
|
| } |
| 31 |
|
| |
| 32 |
|
| |
| 33 |
|
| mh_axis_t * |
| 34 |
11 |
| mh_axis_clone(mh_axis_t *axis_proto) |
| 35 |
|
| { |
| 36 |
11 |
| mh_axis_t *axis_out = (mh_axis_t *)malloc(sizeof(mh_axis_t)); |
| 37 |
11 |
▶ | if (axis_out == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (axis_out == NULL) |
0 |
11 |
11 |
|
| 38 |
|
| return NULL; |
| 39 |
|
| |
| 40 |
11 |
| axis_out->nbins = axis_proto->nbins; |
| 41 |
11 |
▶ | if (!MH_AXIS_ISFIXBIN(axis_proto)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!MH_AXIS_ISFIXBIN(axis_proto)) { |
3 |
8 |
11 |
|
| 42 |
3 |
| axis_out->bins = (double *)malloc(sizeof(double) * (axis_proto->nbins+1)); |
| 43 |
3 |
▶ | if (axis_out->bins == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (axis_out->bins == NULL) { |
0 |
3 |
3 |
|
| 44 |
0 |
| free(axis_out); |
| 45 |
0 |
| return NULL; |
| 46 |
|
| } |
| 47 |
|
| memcpy(axis_out->bins, axis_proto->bins, sizeof(double) * (axis_proto->nbins+1)); |
| 48 |
|
| } |
| 49 |
|
| else { |
| 50 |
8 |
| axis_out->bins = NULL; |
| 51 |
|
| } |
| 52 |
|
| |
| 53 |
11 |
| axis_out->binsize = axis_proto->binsize; |
| 54 |
11 |
| axis_out->width = axis_proto->width; |
| 55 |
11 |
| axis_out->min = axis_proto->min; |
| 56 |
11 |
| axis_out->max = axis_proto->max; |
| 57 |
11 |
| axis_out->userdata = axis_proto->userdata; |
| 58 |
|
| |
| 59 |
11 |
| return axis_out; |
| 60 |
|
| } |
| 61 |
|
| |
| 62 |
|
| |
| 63 |
|
| void |
| 64 |
36 |
| mh_axis_init(mh_axis_t *axis, double min, double max) |
| 65 |
|
| { |
| 66 |
36 |
| axis->min = min; |
| 67 |
36 |
| axis->max = max; |
| 68 |
36 |
| axis->width = max-min; |
| 69 |
36 |
▶ | if (MH_AXIS_ISFIXBIN(axis)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (MH_AXIS_ISFIXBIN(axis)) |
25 |
11 |
36 |
|
| 70 |
25 |
| axis->binsize = axis->width / (double)MH_AXIS_NBINS(axis); |
| 71 |
36 |
| } |
| 72 |
|
| |
| 73 |
|
| |
| 74 |
|
| void |
| 75 |
47 |
| mh_axis_free(mh_axis_t *axis) |
| 76 |
|
| { |
| 77 |
47 |
▶ | if (! MH_AXIS_ISFIXBIN(axis)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (! MH_AXIS_ISFIXBIN(axis)) |
14 |
33 |
47 |
|
| 78 |
14 |
| free(axis->bins); |
| 79 |
47 |
| free(axis); |
| 80 |
47 |
| } |
| 81 |
|
| |
| 82 |
|
| |
| 83 |
|
| unsigned int |
| 84 |
2525 |
| mh_axis_find_bin(mh_axis_t *axis, double x) |
| 85 |
|
| { |
| 86 |
2525 |
▶ | if (MH_AXIS_ISFIXBIN(axis)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (MH_AXIS_ISFIXBIN(axis)) { |
1954 |
571 |
2525 |
|
| 87 |
|
| unsigned int bin; |
| 88 |
1954 |
| const double min = MH_AXIS_MIN(axis); |
| 89 |
|
| |
| 90 |
1954 |
▶ | if (x < min) |
Branch: 1 branch
| Branch | True | False | Total |
| if (x < min) |
1702 |
252 |
1954 |
|
| 91 |
|
| bin = 0; |
| 92 |
1702 |
▶ | else if (x >= MH_AXIS_MAX(axis)) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (x >= MH_AXIS_MAX(axis)) |
260 |
1442 |
1702 |
|
| 93 |
260 |
| bin = MH_AXIS_NBINS(axis)+1; |
| 94 |
|
| else |
| 95 |
1442 |
| bin = 1 + (unsigned int)((x + DBL_EPSILON - min) / MH_AXIS_BINSIZE_FIX(axis)); |
| 96 |
1954 |
| return bin; |
| 97 |
|
| } |
| 98 |
|
| else |
| 99 |
571 |
| return mh_axis_find_bin_var(axis, x); |
| 100 |
|
| } |
| 101 |
|
| |
| 102 |
|
| |
| 103 |
|
| unsigned int |
| 104 |
571 |
| mh_axis_find_bin_var(mh_axis_t *axis, double x) |
| 105 |
|
| { |
| 106 |
|
| /* TODO optimize */ |
| 107 |
|
| unsigned int mid; |
| 108 |
|
| double mid_val; |
| 109 |
|
| unsigned int imin = 0; |
| 110 |
571 |
| unsigned int imax = MH_AXIS_NBINS(axis); |
| 111 |
571 |
| double *bins = axis->bins; |
| 112 |
571 |
| x += DBL_EPSILON; /* FIXME */ |
| 113 |
|
| |
| 114 |
571 |
▶ | if (x < MH_AXIS_MIN(axis)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (x < MH_AXIS_MIN(axis)) |
445 |
126 |
571 |
|
| 115 |
|
| return 0; |
| 116 |
445 |
▶ | else if (x >= MH_AXIS_MAX(axis)) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (x >= MH_AXIS_MAX(axis)) |
131 |
314 |
445 |
|
| 117 |
131 |
| return imax+1; |
| 118 |
|
| |
| 119 |
|
| /* This algorithm is based on 0-based bin indices, we switch to 1-based |
| 120 |
|
| * only in the very return statements! */ |
| 121 |
|
| while (1) { |
| 122 |
911 |
| mid = imin + (imax-imin)/2; |
| 123 |
911 |
| mid_val = bins[mid]; |
| 124 |
911 |
▶ | if (mid_val == x) |
Branch: 1 branch
| Branch | True | False | Total |
| if (mid_val == x) |
87 |
824 |
911 |
|
| 125 |
87 |
| return mid+1; |
| 126 |
824 |
▶ | else if (mid_val > x) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (mid_val > x) { |
441 |
383 |
824 |
|
| 127 |
441 |
▶ | if (mid == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (mid == 0) |
441 |
0 |
441 |
|
| 128 |
|
| return 1; |
| 129 |
441 |
| imax = mid-1; |
| 130 |
441 |
▶ | if (imin > imax) |
Branch: 1 branch
| Branch | True | False | Total |
| if (imin > imax) |
221 |
220 |
441 |
|
| 131 |
220 |
| return mid; |
| 132 |
|
| } |
| 133 |
|
| else { |
| 134 |
383 |
| imin = mid+1; |
| 135 |
383 |
▶ | if (imin > imax) |
Branch: 1 branch
| Branch | True | False | Total |
| if (imin > imax) |
376 |
7 |
383 |
|
| 136 |
7 |
| return imin; |
| 137 |
|
| } |
| 138 |
|
| } |
| 139 |
|
| } |
| 140 |
|
| |