| 1 |
|
| #define PERL_NO_GET_CONTEXT // we'll define thread context if necessary (faster) |
| 2 |
|
| #include "EXTERN.h" // globals/constant import locations |
| 3 |
|
| #include "perl.h" // Perl symbols, structures and constants definition |
| 4 |
|
| #include "XSUB.h" // xsubpp functions and macros |
| 5 |
|
| #include <stdlib.h> // rand() |
| 6 |
|
| #include <string.h> |
| 7 |
|
| |
| 8 |
2 |
| double mode(double arr[], int size) { |
| 9 |
2 |
| double max_value = 0, max_count = 0; |
| 10 |
|
| int i, j; |
| 11 |
11 |
▶ | for (i = 0; i < size; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < size; ++i) { |
9 |
2 |
11 |
|
| 12 |
9 |
| int cnt = 0; |
| 13 |
50 |
▶ | for (j = 0; j < size; ++j) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < size; ++j) { |
41 |
9 |
50 |
|
| 14 |
41 |
▶ | if (arr[j] == arr[i]) |
Branch: 1 branch
| Branch | True | False | Total |
| if (arr[j] == arr[i]) |
13 |
28 |
41 |
|
| 15 |
13 |
| ++cnt; |
| 16 |
|
| } |
| 17 |
9 |
▶ | if (cnt > max_count) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cnt > max_count) { |
3 |
6 |
9 |
|
| 18 |
3 |
| max_count = cnt; |
| 19 |
3 |
| max_value = arr[i]; |
| 20 |
|
| } |
| 21 |
|
| } |
| 22 |
2 |
| return max_value; |
| 23 |
|
| } |
| 24 |
|
| |
| 25 |
9 |
| int compare( const void* a, const void* b) |
| 26 |
|
| { |
| 27 |
9 |
| double int_a = * ( (double*) a ); |
| 28 |
9 |
| double int_b = * ( (double*) b ); |
| 29 |
|
| |
| 30 |
9 |
▶ | if ( int_a == int_b ) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if ( int_a == int_b ) return 0; |
1 |
8 |
9 |
|
| 31 |
8 |
▶ | else if ( int_a < int_b ) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( int_a < int_b ) return -1; |
8 |
0 |
8 |
|
| 32 |
0 |
| else return 1; |
| 33 |
|
| } |
| 34 |
|
| |
| 35 |
|
| |
| 36 |
|
| MODULE = Math::Basic::XS PACKAGE = Math::Basic::XS |
| 37 |
|
| PROTOTYPES: ENABLE |
| 38 |
|
| |
| 39 |
|
| SV * |
| 40 |
|
| sum(...) |
| 41 |
|
| PROTOTYPE: &@ |
| 42 |
|
| CODE: |
| 43 |
3 |
| SV * callback = ST(0); |
| 44 |
3 |
| int sum = 0, i = 1; |
| 45 |
17 |
▶ | for (i = 1; i < items; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < items; i++) { |
14 |
3 |
17 |
|
| 46 |
14 |
| dSP; |
| 47 |
14 |
| GvSV(PL_defgv) = newSVsv(ST(i)); |
| 48 |
14 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
14 |
14 |
|
| 49 |
14 |
| call_sv(callback, 3); |
| 50 |
14 |
| SPAGAIN; |
| 51 |
14 |
| SV * val = POPs; |
| 52 |
14 |
| sum += SvNV(val); |
| 53 |
14 |
| PUTBACK; |
| 54 |
|
| } |
| 55 |
3 |
| RETVAL = newSVnv(sum); |
| 56 |
|
| OUTPUT: |
| 57 |
|
| RETVAL |
| 58 |
|
| |
| 59 |
|
| |
| 60 |
|
| SV * |
| 61 |
|
| min(...) |
| 62 |
|
| PROTOTYPE: &@ |
| 63 |
|
| CODE: |
| 64 |
2 |
| SV * callback = ST(0); |
| 65 |
2 |
| int min = 0, i = 1; |
| 66 |
2 |
| int set = 0; |
| 67 |
11 |
▶ | for (i = 1; i < items; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < items; i++) { |
9 |
2 |
11 |
|
| 68 |
9 |
| dSP; |
| 69 |
9 |
| GvSV(PL_defgv) = newSVsv(ST(i)); |
| 70 |
9 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
9 |
9 |
|
| 71 |
9 |
| call_sv(callback, G_SCALAR); |
| 72 |
9 |
| SPAGAIN; |
| 73 |
9 |
| SV * val = POPs; |
| 74 |
9 |
| double ret = SvNV(val); |
| 75 |
9 |
▶ | if (!set || ret < min) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!set || ret < min) { |
7 |
2 |
9 |
| if (!set || ret < min) { |
0 |
7 |
7 |
|
| 76 |
2 |
| min = ret; |
| 77 |
2 |
| set = 1; |
| 78 |
|
| } |
| 79 |
9 |
| PUTBACK; |
| 80 |
|
| } |
| 81 |
2 |
| RETVAL = newSVnv(min); |
| 82 |
|
| OUTPUT: |
| 83 |
|
| RETVAL |
| 84 |
|
| |
| 85 |
|
| SV * |
| 86 |
|
| max(...) |
| 87 |
|
| PROTOTYPE: &@ |
| 88 |
|
| CODE: |
| 89 |
2 |
| SV * callback = ST(0); |
| 90 |
2 |
| int max = 0, i = 1; |
| 91 |
2 |
| int set = 0; |
| 92 |
11 |
▶ | for (i = 1; i < items; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < items; i++) { |
9 |
2 |
11 |
|
| 93 |
9 |
| dSP; |
| 94 |
9 |
| GvSV(PL_defgv) = newSVsv(ST(i)); |
| 95 |
9 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
9 |
9 |
|
| 96 |
9 |
| call_sv(callback, G_SCALAR); |
| 97 |
9 |
| SPAGAIN; |
| 98 |
9 |
| SV * val = POPs; |
| 99 |
9 |
| int ret = SvNV(val); |
| 100 |
9 |
▶ | if (!set || ret > max) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!set || ret > max) { |
7 |
2 |
9 |
| if (!set || ret > max) { |
7 |
0 |
7 |
|
| 101 |
9 |
| max = ret; |
| 102 |
9 |
| set = 1; |
| 103 |
|
| } |
| 104 |
9 |
| PUTBACK; |
| 105 |
|
| } |
| 106 |
2 |
| RETVAL = newSVnv(max); |
| 107 |
|
| OUTPUT: |
| 108 |
|
| RETVAL |
| 109 |
|
| |
| 110 |
|
| SV * |
| 111 |
|
| mean(...) |
| 112 |
|
| PROTOTYPE: &@ |
| 113 |
|
| CODE: |
| 114 |
2 |
| SV * callback = ST(0); |
| 115 |
2 |
| double sum = 0; |
| 116 |
2 |
| int i = 1; |
| 117 |
11 |
▶ | for (i = 1; i < items; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < items; i++) { |
9 |
2 |
11 |
|
| 118 |
9 |
| dSP; |
| 119 |
9 |
| GvSV(PL_defgv) = newSVsv(ST(i)); |
| 120 |
9 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
9 |
9 |
|
| 121 |
9 |
| call_sv(callback, 3); |
| 122 |
9 |
| SPAGAIN; |
| 123 |
9 |
| SV * val = POPs; |
| 124 |
9 |
| sum += SvNV(val); |
| 125 |
9 |
| PUTBACK; |
| 126 |
|
| } |
| 127 |
2 |
| RETVAL = newSVnv(sum / (items - 1)); |
| 128 |
|
| OUTPUT: |
| 129 |
|
| RETVAL |
| 130 |
|
| |
| 131 |
|
| SV * |
| 132 |
|
| median(...) |
| 133 |
|
| PROTOTYPE: &@ |
| 134 |
|
| CODE: |
| 135 |
2 |
| SV * callback = ST(0); |
| 136 |
|
| int i; |
| 137 |
2 |
| double median[items - 1]; |
| 138 |
11 |
▶ | for (i = 1; i < items; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < items; i++) { |
9 |
2 |
11 |
|
| 139 |
9 |
| dSP; |
| 140 |
9 |
| GvSV(PL_defgv) = newSVsv(ST(i)); |
| 141 |
9 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
9 |
9 |
|
| 142 |
9 |
| call_sv(callback, 3); |
| 143 |
9 |
| SPAGAIN; |
| 144 |
9 |
| SV * val = POPs; |
| 145 |
9 |
| median[i - 1] = SvNV(val); |
| 146 |
9 |
| PUTBACK; |
| 147 |
|
| } |
| 148 |
2 |
| qsort( median, items - 1, sizeof(double), compare ); |
| 149 |
2 |
| i = (items - 1) / 2; |
| 150 |
2 |
▶ | if (i % 2 != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i % 2 != 0) { |
0 |
2 |
2 |
|
| 151 |
0 |
| i++; |
| 152 |
|
| } |
| 153 |
2 |
| RETVAL = newSVnv(median[i]); |
| 154 |
|
| OUTPUT: |
| 155 |
|
| RETVAL |
| 156 |
|
| |
| 157 |
|
| |
| 158 |
|
| SV * |
| 159 |
|
| mode(...) |
| 160 |
|
| PROTOTYPE: &@ |
| 161 |
|
| CODE: |
| 162 |
2 |
| SV * callback = ST(0); |
| 163 |
|
| int i; |
| 164 |
2 |
| double arr[items - 1]; |
| 165 |
11 |
▶ | for (i = 1; i < items; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < items; i++) { |
9 |
2 |
11 |
|
| 166 |
9 |
| dSP; |
| 167 |
9 |
| GvSV(PL_defgv) = newSVsv(ST(i)); |
| 168 |
9 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
9 |
9 |
|
| 169 |
9 |
| call_sv(callback, 3); |
| 170 |
9 |
| SPAGAIN; |
| 171 |
9 |
| SV * val = POPs; |
| 172 |
9 |
| arr[i - 1] = SvNV(val); |
| 173 |
9 |
| PUTBACK; |
| 174 |
|
| } |
| 175 |
2 |
| RETVAL = newSVnv(mode(arr, items - 1)); |
| 176 |
|
| OUTPUT: |
| 177 |
|
| RETVAL |
| 178 |
|
| |
| 179 |
|
| |
| 180 |
|
| |