| 1 |
|
| /* -*- Mode: C -*- */ |
| 2 |
|
| |
| 3 |
|
| #define PERL_NO_GET_CONTEXT 1 |
| 4 |
|
| |
| 5 |
|
| #include "EXTERN.h" |
| 6 |
|
| #include "perl.h" |
| 7 |
|
| #include "XSUB.h" |
| 8 |
|
| |
| 9 |
|
| #include "ppport.h" |
| 10 |
|
| |
| 11 |
|
| #if (PERL_VERSION < 7) |
| 12 |
|
| #include "sort.h" |
| 13 |
|
| #endif |
| 14 |
|
| |
| 15 |
|
| static I32 |
| 16 |
120754 |
| ix_sv_cmp(pTHX_ SV **a, SV **b) { |
| 17 |
120754 |
| return sv_cmp(*a, *b); |
| 18 |
|
| } |
| 19 |
|
| |
| 20 |
|
| static I32 |
| 21 |
7940 |
| ix_rsv_cmp(pTHX_ SV **a, SV **b) { |
| 22 |
7940 |
| return sv_cmp(*b, *a); |
| 23 |
|
| } |
| 24 |
|
| |
| 25 |
|
| static I32 |
| 26 |
0 |
| ix_lsv_cmp(pTHX_ SV **a, SV **b) { |
| 27 |
0 |
| return sv_cmp_locale(*a, *b); |
| 28 |
|
| } |
| 29 |
|
| |
| 30 |
|
| static I32 |
| 31 |
0 |
| ix_rlsv_cmp(pTHX_ SV **a, SV **b) { |
| 32 |
0 |
| return sv_cmp_locale(*b, *a); |
| 33 |
|
| } |
| 34 |
|
| |
| 35 |
|
| static I32 |
| 36 |
614075 |
| ix_n_cmp(pTHX_ NV *a, NV *b) { |
| 37 |
614075 |
| NV nv1 = *a; |
| 38 |
614075 |
| NV nv2 = *b; |
| 39 |
614075 |
▶ | return nv1 < nv2 ? -1 : nv1 > nv2 ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| return nv1 < nv2 ? -1 : nv1 > nv2 ? 1 : 0; |
322563 |
291512 |
614075 |
|
| 40 |
|
| } |
| 41 |
|
| |
| 42 |
|
| static I32 |
| 43 |
130800 |
| ix_rn_cmp(pTHX_ NV *a, NV *b) { |
| 44 |
130800 |
| NV nv1 = *b; |
| 45 |
130800 |
| NV nv2 = *a; |
| 46 |
130800 |
▶ | return nv1 < nv2 ? -1 : nv1 > nv2 ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| return nv1 < nv2 ? -1 : nv1 > nv2 ? 1 : 0; |
62676 |
68124 |
130800 |
|
| 47 |
|
| } |
| 48 |
|
| |
| 49 |
|
| static I32 |
| 50 |
483254 |
| ix_i_cmp(pTHX_ IV *a, IV *b) { |
| 51 |
483254 |
| IV iv1 = *a; |
| 52 |
483254 |
| IV iv2 = *b; |
| 53 |
483254 |
▶ | return iv1 < iv2 ? -1 : iv1 > iv2 ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| return iv1 < iv2 ? -1 : iv1 > iv2 ? 1 : 0; |
234530 |
248724 |
483254 |
|
| 54 |
|
| } |
| 55 |
|
| |
| 56 |
|
| static I32 |
| 57 |
362285 |
| ix_ri_cmp(pTHX_ IV *a, IV *b) { |
| 58 |
362285 |
| IV iv1 = *b; |
| 59 |
362285 |
| IV iv2 = *a; |
| 60 |
362285 |
▶ | return iv1 < iv2 ? -1 : iv1 > iv2 ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| return iv1 < iv2 ? -1 : iv1 > iv2 ? 1 : 0; |
175859 |
186426 |
362285 |
|
| 61 |
|
| } |
| 62 |
|
| |
| 63 |
|
| static I32 |
| 64 |
124619 |
| ix_u_cmp(pTHX_ UV *a, UV *b) { |
| 65 |
124619 |
| UV uv1 = *a; |
| 66 |
124619 |
| UV uv2 = *b; |
| 67 |
124619 |
▶ | return uv1 < uv2 ? -1 : uv1 > uv2 ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| return uv1 < uv2 ? -1 : uv1 > uv2 ? 1 : 0; |
60679 |
63940 |
124619 |
|
| 68 |
|
| } |
| 69 |
|
| |
| 70 |
|
| static I32 |
| 71 |
120812 |
| ix_ru_cmp(pTHX_ UV *a, UV *b) { |
| 72 |
120812 |
| UV uv1 = *b; |
| 73 |
120812 |
| UV uv2 = *a; |
| 74 |
120812 |
▶ | return uv1 < uv2 ? -1 : uv1 > uv2 ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| return uv1 < uv2 ? -1 : uv1 > uv2 ? 1 : 0; |
58116 |
62696 |
120812 |
|
| 75 |
|
| } |
| 76 |
|
| |
| 77 |
32 |
| static void *v_alloc(pTHX_ IV n, IV lsize) { |
| 78 |
|
| void *r; |
| 79 |
32 |
| Newxc(r, n<<lsize, char, void); |
| 80 |
32 |
| SAVEFREEPV(r); |
| 81 |
32 |
| return r; |
| 82 |
|
| } |
| 83 |
|
| |
| 84 |
12 |
| static void *av_alloc(pTHX_ IV n, IV lsize) { |
| 85 |
12 |
| AV *av=(AV*)sv_2mortal((SV*)newAV()); |
| 86 |
12 |
| av_fill(av, n-1); |
| 87 |
12 |
| return AvARRAY(av); |
| 88 |
|
| } |
| 89 |
|
| |
| 90 |
70007 |
| static void i_store(pTHX_ SV *v, void *to) { |
| 91 |
70007 |
| *((IV*)to)=SvIV(v); |
| 92 |
70007 |
| } |
| 93 |
|
| |
| 94 |
24000 |
| static void u_store(pTHX_ SV *v, void *to) { |
| 95 |
24000 |
| *((UV*)to)=SvUV(v); |
| 96 |
24000 |
| } |
| 97 |
|
| |
| 98 |
162000 |
| static void n_store(pTHX_ SV *v, void *to) { |
| 99 |
162000 |
| *((NV*)to)=SvNV(v); |
| 100 |
162000 |
| } |
| 101 |
|
| |
| 102 |
12117 |
| static void sv_store(pTHX_ SV *v, void *to) { |
| 103 |
12117 |
| *((SV**)to)=SvREFCNT_inc(v); |
| 104 |
12117 |
| } |
| 105 |
|
| |
| 106 |
|
| #define lsizeof(A) (ilog2(sizeof(A))) |
| 107 |
|
| |
| 108 |
|
| |
| 109 |
44 |
| static int ilog2(int i) { |
| 110 |
44 |
▶ | if (i>256) croak("internal error"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (i>256) croak("internal error"); |
0 |
44 |
44 |
|
| 111 |
44 |
▶ | if (i>128) return 8; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i>128) return 8; |
0 |
44 |
44 |
|
| 112 |
44 |
▶ | if (i>64) return 7; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i>64) return 7; |
0 |
44 |
44 |
|
| 113 |
44 |
▶ | if (i>32) return 6; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i>32) return 6; |
0 |
44 |
44 |
|
| 114 |
44 |
▶ | if (i>16) return 5; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i>16) return 5; |
0 |
44 |
44 |
|
| 115 |
44 |
▶ | if (i>8) return 4; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i>8) return 4; |
0 |
44 |
44 |
|
| 116 |
44 |
▶ | if (i>4) return 3; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i>4) return 3; |
44 |
0 |
44 |
|
| 117 |
0 |
▶ | if (i>2) return 2; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i>2) return 2; |
0 |
0 |
0 |
|
| 118 |
0 |
▶ | if (i>1) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i>1) return 1; |
0 |
0 |
0 |
|
| 119 |
0 |
| return 0; |
| 120 |
|
| } |
| 121 |
|
| |
| 122 |
|
| /* sorting types: |
| 123 |
|
| |
| 124 |
|
| 0 => string |
| 125 |
|
| 1 => locale |
| 126 |
|
| 2 => number |
| 127 |
|
| 3 => integer |
| 128 |
|
| 4 => unsigned_integer |
| 129 |
|
| 5 => single precission float - not implemented |
| 130 |
|
| |
| 131 |
|
| 128 => reverse string |
| 132 |
|
| 129 => reverse locale |
| 133 |
|
| 130 => reverse number |
| 134 |
|
| 131 => reverse integer |
| 135 |
|
| 132 => reverse unsigned_integer |
| 136 |
|
| 133 => reverse s. p. float - not implemented |
| 137 |
|
| |
| 138 |
|
| */ |
| 139 |
|
| |
| 140 |
|
| typedef I32 (*COMPARE_t)(pTHX_ void*, void*); |
| 141 |
|
| typedef void (*STORE_t)(pTHX_ SV*, void*); |
| 142 |
|
| |
| 143 |
|
| static void |
| 144 |
19 |
| _keysort(pTHX_ IV type, SV *keygen, SV **values, I32 offset, I32 ax, IV len) { |
| 145 |
19 |
| dSP; |
| 146 |
19 |
▶ | if (len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len) { |
19 |
0 |
19 |
|
| 147 |
|
| void *keys; |
| 148 |
|
| void **ixkeys; |
| 149 |
|
| IV i; |
| 150 |
|
| SV **from, **to; |
| 151 |
|
| |
| 152 |
|
| IV lsize; |
| 153 |
|
| COMPARE_t cmp; |
| 154 |
|
| STORE_t store; |
| 155 |
|
| |
| 156 |
|
| #if (PERL_VERSION < 9) |
| 157 |
|
| int hints = PL_curcop->op_private; |
| 158 |
|
| #else |
| 159 |
19 |
| int hints = CopHINTS_get(PL_curcop); |
| 160 |
|
| #endif |
| 161 |
|
| |
| 162 |
|
| /* fprintf (stderr, "hints=0x%x, int=0x%x, loc=0x%x\n", hints, HINT_INTEGER, HINT_LOCALE );fflush(stderr); */ |
| 163 |
|
| |
| 164 |
19 |
| switch(type) { |
| 165 |
2 |
| case 0: |
| 166 |
|
| case 128: |
| 167 |
2 |
▶ | if (hints & HINT_LOCALE) type = type | 128; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hints & HINT_LOCALE) type = type | 128; |
0 |
2 |
2 |
|
| 168 |
2 |
| break; |
| 169 |
13 |
| case 2: |
| 170 |
|
| case 130: |
| 171 |
13 |
▶ | if (hints & HINT_INTEGER) type = type | 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hints & HINT_INTEGER) type = type | 1; |
5 |
8 |
13 |
|
| 172 |
13 |
| break; |
| 173 |
|
| } |
| 174 |
|
| |
| 175 |
19 |
| switch(type) { |
| 176 |
2 |
| case 0: |
| 177 |
2 |
| cmp = (COMPARE_t)&ix_sv_cmp; |
| 178 |
2 |
| lsize = lsizeof(SV*); |
| 179 |
2 |
| keys = av_alloc(aTHX_ len, lsize); |
| 180 |
2 |
| store = &sv_store; |
| 181 |
2 |
| break; |
| 182 |
0 |
| case 1: |
| 183 |
0 |
| cmp = (COMPARE_t)&ix_lsv_cmp; |
| 184 |
0 |
| lsize = lsizeof(SV*); |
| 185 |
0 |
| keys = av_alloc(aTHX_ len, lsize); |
| 186 |
0 |
| store = &sv_store; |
| 187 |
0 |
| break; |
| 188 |
6 |
| case 2: |
| 189 |
6 |
| cmp = (COMPARE_t)&ix_n_cmp; |
| 190 |
6 |
| lsize = lsizeof(NV); |
| 191 |
6 |
| keys = v_alloc(aTHX_ len, lsize); |
| 192 |
6 |
| store = &n_store; |
| 193 |
6 |
| break; |
| 194 |
4 |
| case 3: |
| 195 |
4 |
| cmp = (COMPARE_t)&ix_i_cmp; |
| 196 |
4 |
| lsize = lsizeof(IV); |
| 197 |
4 |
| keys = v_alloc(aTHX_ len, lsize); |
| 198 |
4 |
| store = &i_store; |
| 199 |
4 |
| break; |
| 200 |
1 |
| case 4: |
| 201 |
1 |
| cmp = (COMPARE_t)&ix_u_cmp; |
| 202 |
1 |
| lsize = lsizeof(UV); |
| 203 |
1 |
| keys = v_alloc(aTHX_ len, lsize); |
| 204 |
1 |
| store = &u_store; |
| 205 |
1 |
| break; |
| 206 |
0 |
| case 128: |
| 207 |
0 |
| cmp = (COMPARE_t)&ix_rsv_cmp; |
| 208 |
0 |
| lsize = lsizeof(SV*); |
| 209 |
0 |
| keys = av_alloc(aTHX_ len, lsize); |
| 210 |
0 |
| store = &sv_store; |
| 211 |
0 |
| break; |
| 212 |
0 |
| case 129: |
| 213 |
0 |
| cmp = (COMPARE_t)&ix_rlsv_cmp; |
| 214 |
0 |
| lsize = lsizeof(SV*); |
| 215 |
0 |
| keys = av_alloc(aTHX_ len, lsize); |
| 216 |
0 |
| store = &sv_store; |
| 217 |
0 |
| break; |
| 218 |
2 |
| case 130: |
| 219 |
2 |
| cmp = (COMPARE_t)&ix_rn_cmp; |
| 220 |
2 |
| lsize = lsizeof(NV); |
| 221 |
2 |
| keys = v_alloc(aTHX_ len, lsize); |
| 222 |
2 |
| store = &n_store; |
| 223 |
2 |
| break; |
| 224 |
3 |
| case 131: |
| 225 |
3 |
| cmp = (COMPARE_t)&ix_ri_cmp; |
| 226 |
3 |
| lsize = lsizeof(IV); |
| 227 |
3 |
| keys = v_alloc(aTHX_ len, lsize); |
| 228 |
3 |
| store = &i_store; |
| 229 |
3 |
| break; |
| 230 |
1 |
| case 132: |
| 231 |
1 |
| cmp = (COMPARE_t)&ix_ru_cmp; |
| 232 |
1 |
| lsize = lsizeof(UV); |
| 233 |
1 |
| keys = v_alloc(aTHX_ len, lsize); |
| 234 |
1 |
| store = &u_store; |
| 235 |
1 |
| break; |
| 236 |
0 |
| default: |
| 237 |
0 |
| croak("unsupported sort type %d", type); |
| 238 |
|
| } |
| 239 |
|
| |
| 240 |
19 |
▶ | Newx(ixkeys, len, void*); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(ixkeys, len, void*); |
0 |
19 |
19 |
|
| 241 |
19 |
| SAVEFREEPV(ixkeys); |
| 242 |
19 |
▶ | if (keygen) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (keygen) { |
15 |
4 |
19 |
|
| 243 |
140028 |
▶ | for (i=0; i<len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<len; i++) { |
140013 |
15 |
140028 |
|
| 244 |
|
| IV count; |
| 245 |
|
| SV *current; |
| 246 |
|
| SV *result; |
| 247 |
|
| void *target; |
| 248 |
|
| /* warn("values=%p SP=%p SP-len=%p, &ST(0)=%p\n", values, SP, SP-len, &ST(0)); */ |
| 249 |
140013 |
| ENTER; |
| 250 |
140013 |
| SAVETMPS; |
| 251 |
140013 |
| SAVE_DEFSV; |
| 252 |
140013 |
▶ | current = values ? values[i] : ST(i + offset); |
Branch: 1 branch
| Branch | True | False | Total |
| current = values ? values[i] : ST(i + offset); |
10000 |
130013 |
140013 |
|
| 253 |
140013 |
▶ | DEFSV = sv_2mortal(current ? SvREFCNT_inc(current) : newSV(0)); |
Branch: 2 branches
| Branch | True | False | Total |
| DEFSV = sv_2mortal(current ? SvREFCNT_inc(current) : newSV(0)); |
140013 |
0 |
140013 |
| DEFSV = sv_2mortal(current ? SvREFCNT_inc(current) : newSV(0)); |
140013 |
0 |
140013 |
|
| 254 |
140013 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
140013 |
140013 |
|
| 255 |
140013 |
| PUTBACK; |
| 256 |
140013 |
| count = call_sv(keygen, G_SCALAR); |
| 257 |
140013 |
| SPAGAIN; |
| 258 |
140013 |
▶ | if (count != 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (count != 1) |
0 |
140013 |
140013 |
|
| 259 |
0 |
| croak("wrong number of results returned from key generation sub"); |
| 260 |
140013 |
| result = POPs; |
| 261 |
|
| /* warn("key: %_\n", result); */ |
| 262 |
140013 |
| ixkeys[i] = target = ((char*)keys) + (i << lsize); |
| 263 |
140013 |
| (*store)(aTHX_ result, target); |
| 264 |
140013 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
140013 |
0 |
140013 |
|
| 265 |
140013 |
| LEAVE; |
| 266 |
|
| } |
| 267 |
|
| } |
| 268 |
|
| else { |
| 269 |
40004 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
40000 |
4 |
40004 |
|
| 270 |
|
| void *target; |
| 271 |
40000 |
▶ | SV *current = values ? values[i] : ST(i + offset); |
Branch: 1 branch
| Branch | True | False | Total |
| SV *current = values ? values[i] : ST(i + offset); |
0 |
40000 |
40000 |
|
| 272 |
40000 |
| ixkeys[i] = target = ((char*)keys) + (i << lsize); |
| 273 |
|
| |
| 274 |
40000 |
▶ | (*store)(aTHX_ |
Branch: 1 branch
| Branch | True | False | Total |
| (*store)(aTHX_ |
0 |
40000 |
40000 |
|
| 275 |
0 |
| current ? current : sv_2mortal(newSV(0)), |
| 276 |
|
| target); |
| 277 |
|
| } |
| 278 |
|
| } |
| 279 |
19 |
| sortsv((SV**)ixkeys, len, (SVCOMPARE_t)cmp); |
| 280 |
19 |
▶ | if (values) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (values) { |
1 |
18 |
19 |
|
| 281 |
1 |
| from = to = values; |
| 282 |
|
| } |
| 283 |
|
| else { |
| 284 |
18 |
| from = &ST(offset); |
| 285 |
18 |
| to = &ST(0); |
| 286 |
|
| } |
| 287 |
180032 |
▶ | for(i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i < len; i++) { |
180013 |
19 |
180032 |
|
| 288 |
180013 |
| IV j = ( ((char*)(ixkeys[i])) - ((char*)keys) )>>lsize; |
| 289 |
180013 |
| ixkeys[i] = from[j]; |
| 290 |
|
| } |
| 291 |
180032 |
▶ | for(i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i < len; i++) { |
180013 |
19 |
180032 |
|
| 292 |
180013 |
| to[i] = (SV*)ixkeys[i]; |
| 293 |
|
| } |
| 294 |
|
| } |
| 295 |
19 |
| } |
| 296 |
|
| |
| 297 |
|
| typedef struct multikey { |
| 298 |
|
| COMPARE_t cmp; |
| 299 |
|
| void *data; |
| 300 |
|
| IV lsize; |
| 301 |
|
| } MK; |
| 302 |
|
| |
| 303 |
|
| |
| 304 |
0 |
| static I32 _multikeycmp(pTHX_ void *a, void *b) { |
| 305 |
0 |
| MK *keys = (MK*)PL_sortcop; |
| 306 |
0 |
| IV r = (*(keys->cmp))(aTHX_ a, b); |
| 307 |
0 |
▶ | if (r) |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) |
0 |
0 |
0 |
|
| 308 |
0 |
| return r; |
| 309 |
|
| else { |
| 310 |
0 |
| IV ixa = ( ((char*)a) - ((char*)(keys->data)) ) >> keys->lsize; |
| 311 |
0 |
| IV ixb = ( ((char*)b) - ((char*)(keys->data)) ) >> keys->lsize; |
| 312 |
|
| COMPARE_t cmp; |
| 313 |
|
| while(1) { |
| 314 |
0 |
| keys++; |
| 315 |
0 |
| cmp=keys->cmp; |
| 316 |
0 |
▶ | if (!cmp) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cmp) |
0 |
0 |
0 |
|
| 317 |
0 |
| return 0; |
| 318 |
0 |
| a = ((char*)(keys->data))+(ixa<<keys->lsize); |
| 319 |
0 |
| b = ((char*)(keys->data))+(ixb<<keys->lsize); |
| 320 |
0 |
| r = (*cmp)(aTHX_ a, b); |
| 321 |
0 |
▶ | if (r) |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) |
0 |
0 |
0 |
|
| 322 |
0 |
| return r; |
| 323 |
|
| } |
| 324 |
|
| } |
| 325 |
|
| return 0; /* dead code just to remove warnings from some |
| 326 |
|
| * compilers */ |
| 327 |
|
| } |
| 328 |
|
| |
| 329 |
16996 |
| static I32 _secondkeycmp(pTHX_ void *a, void *b) { |
| 330 |
16996 |
| MK *keys = (MK*)PL_sortcop; |
| 331 |
16996 |
| IV ixa = ( ((char*)a) - ((char*)(keys->data)) ) >> keys->lsize; |
| 332 |
16996 |
| IV ixb = ( ((char*)b) - ((char*)(keys->data)) ) >> keys->lsize; |
| 333 |
|
| COMPARE_t cmp; |
| 334 |
440 |
| while(1) { |
| 335 |
|
| I32 r; |
| 336 |
17436 |
| keys++; |
| 337 |
17436 |
| cmp=keys->cmp; |
| 338 |
17436 |
▶ | if (!cmp) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cmp) |
5636 |
11800 |
17436 |
|
| 339 |
5636 |
| return 0; |
| 340 |
11800 |
| a = ((char*)(keys->data))+(ixa<<keys->lsize); |
| 341 |
11800 |
| b = ((char*)(keys->data))+(ixb<<keys->lsize); |
| 342 |
11800 |
| r = (*cmp)(aTHX_ a, b); |
| 343 |
11800 |
▶ | if (r) |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) |
11360 |
440 |
11800 |
|
| 344 |
11360 |
| return r; |
| 345 |
|
| } |
| 346 |
|
| return 0; /* dead code just to remove warnings from some |
| 347 |
|
| * compilers */ |
| 348 |
|
| } |
| 349 |
|
| |
| 350 |
|
| static I32 |
| 351 |
116 |
| ix_sv_mcmp(pTHX_ SV **a, SV **b) { |
| 352 |
116 |
| I32 r = sv_cmp(*a, *b); |
| 353 |
116 |
▶ | if (r) return r; |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) return r; |
104 |
12 |
116 |
|
| 354 |
12 |
| return _secondkeycmp(aTHX_ a, b); |
| 355 |
|
| } |
| 356 |
|
| |
| 357 |
|
| static I32 |
| 358 |
128 |
| ix_rsv_mcmp(pTHX_ SV **a, SV **b) { |
| 359 |
128 |
| I32 r = sv_cmp(*b, *a); |
| 360 |
128 |
▶ | if (r) return r; |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) return r; |
116 |
12 |
128 |
|
| 361 |
12 |
| return _secondkeycmp(aTHX_ a, b); |
| 362 |
|
| } |
| 363 |
|
| |
| 364 |
|
| static I32 |
| 365 |
0 |
| ix_lsv_mcmp(pTHX_ SV **a, SV **b) { |
| 366 |
0 |
| I32 r = sv_cmp_locale(*a, *b); |
| 367 |
0 |
▶ | if (r) return r; |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) return r; |
0 |
0 |
0 |
|
| 368 |
0 |
| return _secondkeycmp(aTHX_ a, b); |
| 369 |
|
| } |
| 370 |
|
| |
| 371 |
|
| static I32 |
| 372 |
0 |
| ix_rlsv_mcmp(pTHX_ SV **a, SV **b) { |
| 373 |
0 |
| I32 r = sv_cmp_locale(*b, *a); |
| 374 |
0 |
▶ | if (r) return r; |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) return r; |
0 |
0 |
0 |
|
| 375 |
0 |
| return _secondkeycmp(aTHX_ a, b); |
| 376 |
|
| } |
| 377 |
|
| |
| 378 |
|
| static I32 |
| 379 |
259198 |
| ix_n_mcmp(pTHX_ NV *a, NV *b) { |
| 380 |
259198 |
| NV nv1 = *a; |
| 381 |
259198 |
| NV nv2 = *b; |
| 382 |
259198 |
▶ | return nv1 < nv2 ? -1 : nv1 > nv2 ? 1 : _secondkeycmp(aTHX_ a, b); |
Branch: 2 branches
| Branch | True | False | Total |
| return nv1 < nv2 ? -1 : nv1 > nv2 ? 1 : _secondkeycmp(aTHX_ a, b); |
130744 |
128454 |
259198 |
| return nv1 < nv2 ? -1 : nv1 > nv2 ? 1 : _secondkeycmp(aTHX_ a, b); |
120514 |
7940 |
128454 |
|
| 383 |
|
| } |
| 384 |
|
| |
| 385 |
|
| static I32 |
| 386 |
0 |
| ix_rn_mcmp(pTHX_ NV *a, NV *b) { |
| 387 |
0 |
| NV nv1 = *b; |
| 388 |
0 |
| NV nv2 = *a; |
| 389 |
0 |
▶ | return nv1 < nv2 ? -1 : nv1 > nv2 ? 1 : _secondkeycmp(aTHX_ a, b); |
Branch: 2 branches
| Branch | True | False | Total |
| return nv1 < nv2 ? -1 : nv1 > nv2 ? 1 : _secondkeycmp(aTHX_ a, b); |
0 |
0 |
0 |
| return nv1 < nv2 ? -1 : nv1 > nv2 ? 1 : _secondkeycmp(aTHX_ a, b); |
0 |
0 |
0 |
|
| 390 |
|
| } |
| 391 |
|
| |
| 392 |
|
| static I32 |
| 393 |
0 |
| ix_i_mcmp(pTHX_ IV *a, IV *b) { |
| 394 |
0 |
| IV iv1 = *a; |
| 395 |
0 |
| IV iv2 = *b; |
| 396 |
0 |
▶ | return iv1 < iv2 ? -1 : iv1 > iv2 ? 1 : _secondkeycmp(aTHX_ a, b); |
Branch: 2 branches
| Branch | True | False | Total |
| return iv1 < iv2 ? -1 : iv1 > iv2 ? 1 : _secondkeycmp(aTHX_ a, b); |
0 |
0 |
0 |
| return iv1 < iv2 ? -1 : iv1 > iv2 ? 1 : _secondkeycmp(aTHX_ a, b); |
0 |
0 |
0 |
|
| 397 |
|
| } |
| 398 |
|
| |
| 399 |
|
| static I32 |
| 400 |
12 |
| ix_ri_mcmp(pTHX_ IV *a, IV *b) { |
| 401 |
12 |
| IV iv1 = *b; |
| 402 |
12 |
| IV iv2 = *a; |
| 403 |
12 |
▶ | return iv1 < iv2 ? -1 : iv1 > iv2 ? 1 : _secondkeycmp(aTHX_ a, b); |
Branch: 2 branches
| Branch | True | False | Total |
| return iv1 < iv2 ? -1 : iv1 > iv2 ? 1 : _secondkeycmp(aTHX_ a, b); |
5 |
7 |
12 |
| return iv1 < iv2 ? -1 : iv1 > iv2 ? 1 : _secondkeycmp(aTHX_ a, b); |
0 |
5 |
5 |
|
| 404 |
|
| } |
| 405 |
|
| |
| 406 |
|
| static I32 |
| 407 |
23514 |
| ix_u_mcmp(pTHX_ UV *a, UV *b) { |
| 408 |
23514 |
| UV uv1 = *a; |
| 409 |
23514 |
| UV uv2 = *b; |
| 410 |
23514 |
▶ | return uv1 < uv2 ? -1 : uv1 > uv2 ? 1 : _secondkeycmp(aTHX_ a, b); |
Branch: 2 branches
| Branch | True | False | Total |
| return uv1 < uv2 ? -1 : uv1 > uv2 ? 1 : _secondkeycmp(aTHX_ a, b); |
17787 |
5727 |
23514 |
| return uv1 < uv2 ? -1 : uv1 > uv2 ? 1 : _secondkeycmp(aTHX_ a, b); |
9032 |
8755 |
17787 |
|
| 411 |
|
| } |
| 412 |
|
| |
| 413 |
|
| static I32 |
| 414 |
0 |
| ix_ru_mcmp(pTHX_ UV *a, UV *b) { |
| 415 |
0 |
| UV uv1 = *b; |
| 416 |
0 |
| UV uv2 = *a; |
| 417 |
0 |
▶ | return uv1 < uv2 ? -1 : uv1 > uv2 ? 1 : _secondkeycmp(aTHX_ a, b); |
Branch: 2 branches
| Branch | True | False | Total |
| return uv1 < uv2 ? -1 : uv1 > uv2 ? 1 : _secondkeycmp(aTHX_ a, b); |
0 |
0 |
0 |
| return uv1 < uv2 ? -1 : uv1 > uv2 ? 1 : _secondkeycmp(aTHX_ a, b); |
0 |
0 |
0 |
|
| 418 |
|
| } |
| 419 |
|
| |
| 420 |
|
| static void |
| 421 |
16 |
| _multikeysort(pTHX_ SV *keytypes, SV *keygen, SV *post, |
| 422 |
|
| SV**values, I32 from_offset, I32 ax, I32 len) { |
| 423 |
16 |
| dSP; |
| 424 |
|
| STRLEN nkeys; |
| 425 |
16 |
| unsigned char *types=(unsigned char *)SvPV(keytypes, nkeys); |
| 426 |
|
| |
| 427 |
16 |
▶ | if (nkeys<1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nkeys<1) |
0 |
16 |
16 |
|
| 428 |
0 |
| croak("empty multikey type list passed"); |
| 429 |
|
| |
| 430 |
16 |
▶ | if (len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len) { |
16 |
0 |
16 |
|
| 431 |
|
| IV i; |
| 432 |
|
| MK *keys; |
| 433 |
|
| STORE_t *store; |
| 434 |
|
| void **ixkeys; |
| 435 |
|
| SV **from, **to; |
| 436 |
16 |
| COMPARE_t cmp = (COMPARE_t)&_multikeycmp; |
| 437 |
|
| |
| 438 |
16 |
▶ | Newx(keys, nkeys+1, MK); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(keys, nkeys+1, MK); |
0 |
16 |
16 |
|
| 439 |
16 |
| SAVEFREEPV(keys); |
| 440 |
16 |
▶ | Newx(store, nkeys, STORE_t); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(store, nkeys, STORE_t); |
0 |
16 |
16 |
|
| 441 |
16 |
| SAVEFREEPV(store); |
| 442 |
|
| |
| 443 |
41 |
▶ | for(i=0; i<nkeys; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<nkeys; i++) { |
25 |
16 |
41 |
|
| 444 |
25 |
| MK *key = keys+i; |
| 445 |
25 |
| switch(types[i]) { |
| 446 |
4 |
| case 0: |
| 447 |
4 |
▶ | if (i==0) cmp = (COMPARE_t)&ix_sv_mcmp; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i==0) cmp = (COMPARE_t)&ix_sv_mcmp; |
4 |
0 |
4 |
|
| 448 |
4 |
| key->cmp = (COMPARE_t)&ix_sv_cmp; |
| 449 |
4 |
| key->lsize = lsizeof(SV*); |
| 450 |
4 |
| key->data = av_alloc(aTHX_ len, key->lsize); |
| 451 |
4 |
| store[i] = &sv_store; |
| 452 |
4 |
| break; |
| 453 |
0 |
| case 1: |
| 454 |
0 |
▶ | if (i==0) cmp = (COMPARE_t)&ix_lsv_mcmp; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i==0) cmp = (COMPARE_t)&ix_lsv_mcmp; |
0 |
0 |
0 |
|
| 455 |
0 |
| key->cmp = (COMPARE_t)&ix_lsv_cmp; |
| 456 |
0 |
| key->lsize = lsizeof(SV*); |
| 457 |
0 |
| key->data = av_alloc(aTHX_ len, key->lsize); |
| 458 |
0 |
| store[i] = &sv_store; |
| 459 |
0 |
| break; |
| 460 |
10 |
| case 2: |
| 461 |
10 |
▶ | if (i==0) cmp = (COMPARE_t)&ix_n_mcmp; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i==0) cmp = (COMPARE_t)&ix_n_mcmp; |
4 |
6 |
10 |
|
| 462 |
10 |
| key->cmp = (COMPARE_t)&ix_n_cmp; |
| 463 |
10 |
| key->lsize = lsizeof(NV); |
| 464 |
10 |
| key->data = v_alloc(aTHX_ len, key->lsize); |
| 465 |
10 |
| store[i] = &n_store; |
| 466 |
10 |
| break; |
| 467 |
0 |
| case 3: |
| 468 |
0 |
▶ | if (i==0) cmp = (COMPARE_t)&ix_i_mcmp; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i==0) cmp = (COMPARE_t)&ix_i_mcmp; |
0 |
0 |
0 |
|
| 469 |
0 |
| key->cmp = (COMPARE_t)&ix_i_cmp; |
| 470 |
0 |
| key->lsize = lsizeof(IV); |
| 471 |
0 |
| key->data = v_alloc(aTHX_ len, key->lsize); |
| 472 |
0 |
| store[i] = &i_store; |
| 473 |
0 |
| break; |
| 474 |
4 |
| case 4: |
| 475 |
4 |
▶ | if (i==0) cmp = (COMPARE_t)&ix_u_mcmp; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i==0) cmp = (COMPARE_t)&ix_u_mcmp; |
3 |
1 |
4 |
|
| 476 |
4 |
| key->cmp = (COMPARE_t)&ix_u_cmp; |
| 477 |
4 |
| key->lsize = lsizeof(UV); |
| 478 |
4 |
| key->data = v_alloc(aTHX_ len, key->lsize); |
| 479 |
4 |
| store[i] = &u_store; |
| 480 |
4 |
| break; |
| 481 |
6 |
| case 128: |
| 482 |
6 |
▶ | if (i==0) cmp = (COMPARE_t)&ix_rsv_mcmp; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i==0) cmp = (COMPARE_t)&ix_rsv_mcmp; |
4 |
2 |
6 |
|
| 483 |
6 |
| key->cmp = (COMPARE_t)&ix_rsv_cmp; |
| 484 |
6 |
| key->lsize = lsizeof(SV*); |
| 485 |
6 |
| key->data = av_alloc(aTHX_ len, key->lsize); |
| 486 |
6 |
| store[i] = &sv_store; |
| 487 |
6 |
| break; |
| 488 |
0 |
| case 129: |
| 489 |
0 |
▶ | if (i==0) cmp = (COMPARE_t)&ix_rlsv_mcmp; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i==0) cmp = (COMPARE_t)&ix_rlsv_mcmp; |
0 |
0 |
0 |
|
| 490 |
0 |
| key->cmp = (COMPARE_t)&ix_rlsv_cmp; |
| 491 |
0 |
| key->lsize = lsizeof(SV*); |
| 492 |
0 |
| key->data = av_alloc(aTHX_ len, key->lsize); |
| 493 |
0 |
| store[i] = &sv_store; |
| 494 |
0 |
| break; |
| 495 |
0 |
| case 130: |
| 496 |
0 |
▶ | if (i==0) cmp = (COMPARE_t)&ix_rn_mcmp; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i==0) cmp = (COMPARE_t)&ix_rn_mcmp; |
0 |
0 |
0 |
|
| 497 |
0 |
| key->cmp = (COMPARE_t)&ix_rn_cmp; |
| 498 |
0 |
| key->lsize = lsizeof(NV); |
| 499 |
0 |
| key->data = v_alloc(aTHX_ len, key->lsize); |
| 500 |
0 |
| store[i] = &n_store; |
| 501 |
0 |
| break; |
| 502 |
1 |
| case 131: |
| 503 |
1 |
▶ | if (i==0) cmp = (COMPARE_t)&ix_ri_mcmp; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i==0) cmp = (COMPARE_t)&ix_ri_mcmp; |
1 |
0 |
1 |
|
| 504 |
1 |
| key->cmp = (COMPARE_t)&ix_ri_cmp; |
| 505 |
1 |
| key->lsize = lsizeof(IV); |
| 506 |
1 |
| key->data = v_alloc(aTHX_ len, key->lsize); |
| 507 |
1 |
| store[i] = &i_store; |
| 508 |
1 |
| break; |
| 509 |
0 |
| case 132: |
| 510 |
0 |
▶ | if (i==0) cmp = (COMPARE_t)&ix_ru_mcmp; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i==0) cmp = (COMPARE_t)&ix_ru_mcmp; |
0 |
0 |
0 |
|
| 511 |
0 |
| key->cmp = (COMPARE_t)&ix_ru_cmp; |
| 512 |
0 |
| key->lsize = lsizeof(UV); |
| 513 |
0 |
| key->data = v_alloc(aTHX_ len, key->lsize); |
| 514 |
0 |
| store[i] = &u_store; |
| 515 |
0 |
| break; |
| 516 |
0 |
| default: |
| 517 |
0 |
| croak("unsupported sort type %d", types[i]); |
| 518 |
|
| } |
| 519 |
|
| } |
| 520 |
|
| |
| 521 |
16 |
| keys[nkeys].cmp = 0; |
| 522 |
16 |
| keys[nkeys].data = 0; |
| 523 |
16 |
| keys[nkeys].lsize = 0; |
| 524 |
|
| |
| 525 |
16 |
| Newx(ixkeys, len, void*); |
| 526 |
16 |
| SAVEFREEPV(ixkeys); |
| 527 |
25127 |
▶ | for (i=0; i<len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<len; i++) { |
25111 |
16 |
25127 |
|
| 528 |
|
| IV count; |
| 529 |
|
| SV *current; |
| 530 |
|
| void *target; |
| 531 |
25111 |
| ENTER; |
| 532 |
25111 |
| SAVETMPS; |
| 533 |
25111 |
| SAVE_DEFSV; |
| 534 |
25111 |
▶ | current = values ? values[i] : ST(i+from_offset); |
Branch: 1 branch
| Branch | True | False | Total |
| current = values ? values[i] : ST(i+from_offset); |
12052 |
13059 |
25111 |
|
| 535 |
25111 |
▶ | DEFSV = sv_2mortal(current ? SvREFCNT_inc(current) : newSV(0)); |
Branch: 2 branches
| Branch | True | False | Total |
| DEFSV = sv_2mortal(current ? SvREFCNT_inc(current) : newSV(0)); |
25111 |
0 |
25111 |
| DEFSV = sv_2mortal(current ? SvREFCNT_inc(current) : newSV(0)); |
25111 |
0 |
25111 |
|
| 536 |
25111 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
25111 |
25111 |
|
| 537 |
25111 |
| PUTBACK; |
| 538 |
25111 |
| count = call_sv(keygen, G_ARRAY); |
| 539 |
25111 |
| SPAGAIN; |
| 540 |
25111 |
▶ | if (post) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (post) { |
2052 |
23059 |
25111 |
|
| 541 |
2052 |
▶ | PUSHMARK(SP-count); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP-count); |
0 |
2052 |
2052 |
|
| 542 |
2052 |
| PUTBACK; |
| 543 |
2052 |
| count = call_sv(post, G_ARRAY); |
| 544 |
2052 |
| SPAGAIN; |
| 545 |
|
| } |
| 546 |
25111 |
▶ | if (count != nkeys) |
Branch: 1 branch
| Branch | True | False | Total |
| if (count != nkeys) |
0 |
25111 |
25111 |
|
| 547 |
0 |
| croak("wrong number of results returned " |
| 548 |
|
| "from multikey generation sub " |
| 549 |
|
| "(%d expected, %d returned)", |
| 550 |
|
| nkeys, count); |
| 551 |
113222 |
▶ | while(count-- > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(count-- > 0) { |
88111 |
25111 |
113222 |
|
| 552 |
88111 |
| SV *result = POPs; |
| 553 |
88111 |
| MK *key = keys+count; |
| 554 |
88111 |
| target = ((char*)(key->data)) + (i<<key->lsize); |
| 555 |
88111 |
| (*(store[count]))(aTHX_ result, target); |
| 556 |
|
| } |
| 557 |
25111 |
| ixkeys[i] = target; |
| 558 |
25111 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
25111 |
0 |
25111 |
|
| 559 |
25111 |
| LEAVE; |
| 560 |
|
| } |
| 561 |
16 |
| SAVEVPTR(PL_sortcop); |
| 562 |
16 |
| PL_sortcop = (OP*)keys; |
| 563 |
16 |
| sortsv((SV**)ixkeys, len, (SVCOMPARE_t)cmp); |
| 564 |
16 |
▶ | if (values) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (values) { |
7 |
9 |
16 |
|
| 565 |
7 |
| from = to = values; |
| 566 |
|
| } |
| 567 |
|
| else { |
| 568 |
9 |
| from = &ST(from_offset); |
| 569 |
9 |
| to = &ST(0); |
| 570 |
|
| } |
| 571 |
25127 |
▶ | for(i=0; i<len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<len; i++) { |
25111 |
16 |
25127 |
|
| 572 |
25111 |
| IV j = ( ((char*)(ixkeys[i])) - ((char*)(keys->data)) )>>keys->lsize; |
| 573 |
25111 |
| ixkeys[i] = from[j]; |
| 574 |
|
| } |
| 575 |
25127 |
▶ | for(i=0; i<len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<len; i++) { |
25111 |
16 |
25127 |
|
| 576 |
25111 |
| to[i] = (SV*)ixkeys[i]; |
| 577 |
|
| } |
| 578 |
|
| } |
| 579 |
16 |
| } |
| 580 |
|
| |
| 581 |
|
| static AV * |
| 582 |
16 |
| _xclosure_defaults(pTHX_ CV *cv) { |
| 583 |
16 |
| MAGIC *magic = mg_find((SV*)cv, '~'); |
| 584 |
16 |
▶ | if (magic) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (magic) { |
16 |
0 |
16 |
|
| 585 |
16 |
▶ | if ( magic->mg_obj |
Branch: 1 branch
| Branch | True | False | Total |
| if ( magic->mg_obj |
16 |
0 |
16 |
|
| 586 |
16 |
▶ | && SvTYPE((SV*)(magic->mg_obj)) == SVt_PVAV ) |
Branch: 1 branch
| Branch | True | False | Total |
| && SvTYPE((SV*)(magic->mg_obj)) == SVt_PVAV ) |
16 |
0 |
16 |
|
| 587 |
16 |
| return (AV*)(magic->mg_obj); |
| 588 |
0 |
| croak("internal error: bad XSUB closure"); |
| 589 |
|
| } |
| 590 |
0 |
| return NULL; |
| 591 |
|
| } |
| 592 |
|
| |
| 593 |
|
| static void |
| 594 |
24 |
| _xclosure_make(pTHX_ CV *cv, AV *defaults) { |
| 595 |
24 |
| sv_magic((SV*)cv, (SV*)defaults, '~', "XCLOSURE", 0); |
| 596 |
24 |
| } |
| 597 |
|
| |
| 598 |
|
| XS(XS_Sort__Key__multikeysort); |
| 599 |
9 |
| XS(XS_Sort__Key__multikeysort) |
| 600 |
|
| { |
| 601 |
9 |
| dXSARGS; |
| 602 |
9 |
| SV *gen=0; |
| 603 |
9 |
| SV *post=0; |
| 604 |
9 |
| SV *types=0; |
| 605 |
9 |
| IV offset=0; |
| 606 |
|
| |
| 607 |
9 |
| AV *defaults = _xclosure_defaults(aTHX_ cv); |
| 608 |
|
| |
| 609 |
9 |
▶ | if (defaults) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (defaults) { |
9 |
0 |
9 |
|
| 610 |
9 |
| types = *(av_fetch(defaults, 0, 1)); |
| 611 |
9 |
| gen = *(av_fetch(defaults, 1, 1)); |
| 612 |
9 |
| post = *(av_fetch(defaults, 2, 1)); |
| 613 |
9 |
▶ | if (!SvOK(post)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(post)) |
6 |
3 |
9 |
|
| 614 |
6 |
| post = 0; |
| 615 |
|
| } |
| 616 |
|
| |
| 617 |
9 |
▶ | if (!types || !SvOK(types)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!types || !SvOK(types)) { |
9 |
0 |
9 |
| if (!types || !SvOK(types)) { |
0 |
9 |
9 |
|
| 618 |
0 |
▶ | if (items--) |
Branch: 1 branch
| Branch | True | False | Total |
| if (items--) |
0 |
0 |
0 |
|
| 619 |
0 |
| types = ST(offset++); |
| 620 |
|
| else |
| 621 |
0 |
| croak("not enough arguments"); |
| 622 |
|
| |
| 623 |
|
| } |
| 624 |
9 |
▶ | if (!gen || !SvOK(gen)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!gen || !SvOK(gen)) { |
9 |
0 |
9 |
| if (!gen || !SvOK(gen)) { |
5 |
4 |
9 |
|
| 625 |
5 |
▶ | if (items--) |
Branch: 1 branch
| Branch | True | False | Total |
| if (items--) |
5 |
0 |
5 |
|
| 626 |
5 |
| gen = ST(offset++); |
| 627 |
|
| else |
| 628 |
0 |
| croak("not enough arguments"); |
| 629 |
|
| } |
| 630 |
|
| |
| 631 |
9 |
| _multikeysort(aTHX_ types, gen, post, 0, offset, ax, items); |
| 632 |
9 |
| SP=&ST(items-1); |
| 633 |
9 |
| PUTBACK; |
| 634 |
9 |
| return; |
| 635 |
|
| } |
| 636 |
|
| |
| 637 |
|
| |
| 638 |
|
| XS(XS_Sort__Key__multikeysort_inplace); |
| 639 |
7 |
| XS(XS_Sort__Key__multikeysort_inplace) |
| 640 |
|
| { |
| 641 |
7 |
| dXSARGS; |
| 642 |
7 |
| SV *gen = 0; |
| 643 |
7 |
| SV *post = 0; |
| 644 |
7 |
| SV *types = 0; |
| 645 |
|
| AV *values; |
| 646 |
|
| |
| 647 |
7 |
| AV *magic_values=0; |
| 648 |
|
| I32 len; |
| 649 |
7 |
| I32 offset=0; |
| 650 |
|
| |
| 651 |
7 |
| AV *defaults = _xclosure_defaults(aTHX_ cv); |
| 652 |
|
| |
| 653 |
7 |
▶ | if (defaults) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (defaults) { |
7 |
0 |
7 |
|
| 654 |
7 |
| types = *(av_fetch(defaults, 0, 1)); |
| 655 |
7 |
| gen = *(av_fetch(defaults, 1, 1)); |
| 656 |
7 |
| post = *(av_fetch(defaults, 2, 1)); |
| 657 |
7 |
▶ | if (!SvOK(post)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(post)) |
4 |
3 |
7 |
|
| 658 |
4 |
| post = 0; |
| 659 |
|
| } |
| 660 |
|
| |
| 661 |
7 |
| SP-=items; |
| 662 |
|
| |
| 663 |
7 |
▶ | if (!types || !SvOK(types)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!types || !SvOK(types)) { |
7 |
0 |
7 |
| if (!types || !SvOK(types)) { |
0 |
7 |
7 |
|
| 664 |
0 |
▶ | if (items--) |
Branch: 1 branch
| Branch | True | False | Total |
| if (items--) |
0 |
0 |
0 |
|
| 665 |
0 |
| types = ST(offset++); |
| 666 |
|
| else |
| 667 |
0 |
| croak("not enough arguments, packed multikey type descriptor required"); |
| 668 |
|
| } |
| 669 |
7 |
▶ | if (!gen || !SvOK(gen)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!gen || !SvOK(gen)) { |
7 |
0 |
7 |
| if (!gen || !SvOK(gen)) { |
3 |
4 |
7 |
|
| 670 |
3 |
▶ | if (items--) |
Branch: 1 branch
| Branch | True | False | Total |
| if (items--) |
3 |
0 |
3 |
|
| 671 |
3 |
| gen = ST(offset++); |
| 672 |
|
| else |
| 673 |
0 |
| croak("not enough arguments, reference to multikey generation subroutine required"); |
| 674 |
|
| } |
| 675 |
|
| |
| 676 |
7 |
▶ | if(!(SvROK(gen) && SvTYPE(SvRV(gen))==SVt_PVCV)) |
Branch: 2 branches
| Branch | True | False | Total |
| if(!(SvROK(gen) && SvTYPE(SvRV(gen))==SVt_PVCV)) |
7 |
0 |
7 |
| if(!(SvROK(gen) && SvTYPE(SvRV(gen))==SVt_PVCV)) |
0 |
7 |
7 |
|
| 677 |
0 |
| croak("wrong argument type, subroutine reference required"); |
| 678 |
|
| |
| 679 |
7 |
▶ | if (items != 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (items != 1) |
0 |
7 |
7 |
|
| 680 |
0 |
| croak("not enough arguments, array reference required"); |
| 681 |
|
| |
| 682 |
7 |
▶ | if (SvROK(ST(offset)) && SvTYPE(SvRV(ST(offset)))==SVt_PVAV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(ST(offset)) && SvTYPE(SvRV(ST(offset)))==SVt_PVAV) |
7 |
0 |
7 |
| if (SvROK(ST(offset)) && SvTYPE(SvRV(ST(offset)))==SVt_PVAV) |
7 |
0 |
7 |
|
| 683 |
7 |
| values = (AV*)SvRV(ST(offset)); |
| 684 |
0 |
| else croak("wrong argument type, array reference required"); |
| 685 |
|
| |
| 686 |
7 |
▶ | if ((len=av_len(values)+1)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((len=av_len(values)+1)) { |
7 |
0 |
7 |
|
| 687 |
|
| /* warn("ix=%d\n", ix); */ |
| 688 |
7 |
▶ | if (SvMAGICAL(values) || AvREIFY(values)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvMAGICAL(values) || AvREIFY(values)) { |
7 |
0 |
7 |
| if (SvMAGICAL(values) || AvREIFY(values)) { |
0 |
7 |
7 |
|
| 689 |
|
| int i; |
| 690 |
0 |
| magic_values = values; |
| 691 |
0 |
| values = (AV*)sv_2mortal((SV*)newAV()); |
| 692 |
0 |
| av_extend(values, len-1); |
| 693 |
0 |
▶ | for (i=0; i<len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<len; i++) { |
0 |
0 |
0 |
|
| 694 |
0 |
| SV **currentp = av_fetch(magic_values, i, 0); |
| 695 |
0 |
▶ | av_store( values, i, |
Branch: 1 branch
| Branch | True | False | Total |
| av_store( values, i, |
0 |
0 |
0 |
|
| 696 |
|
| ( currentp |
| 697 |
|
| ? SvREFCNT_inc(*currentp) |
| 698 |
|
| : newSV(0) ) ); |
| 699 |
|
| } |
| 700 |
|
| } |
| 701 |
|
| |
| 702 |
7 |
| _multikeysort(aTHX_ types, gen, post, AvARRAY(values), 0, 0, len); |
| 703 |
|
| |
| 704 |
7 |
▶ | if (magic_values) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (magic_values) { |
0 |
7 |
7 |
|
| 705 |
|
| int i; |
| 706 |
0 |
| SV **values_array = AvARRAY(values); |
| 707 |
0 |
▶ | for(i=0; i<len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<len; i++) { |
0 |
0 |
0 |
|
| 708 |
0 |
| SV *current = values_array[i]; |
| 709 |
0 |
▶ | if (!current) current = &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!current) current = &PL_sv_undef; |
0 |
0 |
0 |
|
| 710 |
0 |
▶ | if (!av_store(magic_values, i, SvREFCNT_inc(current))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!av_store(magic_values, i, SvREFCNT_inc(current))) |
0 |
0 |
0 |
|
| 711 |
0 |
| SvREFCNT_dec(current); |
| 712 |
|
| } |
| 713 |
|
| } |
| 714 |
|
| } |
| 715 |
7 |
| PUTBACK; |
| 716 |
7 |
| } |
| 717 |
|
| |
| 718 |
|
| |
| 719 |
|
| MODULE = Sort::Key PACKAGE = Sort::Key |
| 720 |
|
| PROTOTYPES: ENABLE |
| 721 |
|
| |
| 722 |
|
| void |
| 723 |
|
| keysort(SV *keygen, ...) |
| 724 |
|
| PROTOTYPE: &@ |
| 725 |
|
| ALIAS: |
| 726 |
|
| lkeysort = 1 |
| 727 |
|
| nkeysort = 2 |
| 728 |
|
| ikeysort = 3 |
| 729 |
|
| ukeysort = 4 |
| 730 |
|
| rkeysort = 128 |
| 731 |
|
| rlkeysort = 129 |
| 732 |
|
| rnkeysort = 130 |
| 733 |
|
| rikeysort = 131 |
| 734 |
|
| rukeysort = 132 |
| 735 |
|
| PPCODE: |
| 736 |
14 |
| items--; |
| 737 |
14 |
▶ | if (items) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items) { |
14 |
0 |
14 |
|
| 738 |
14 |
| _keysort(aTHX_ ix, keygen, 0, 1, ax, items); |
| 739 |
14 |
| SPAGAIN; |
| 740 |
14 |
| SP = &ST(items-1); |
| 741 |
|
| } |
| 742 |
|
| |
| 743 |
|
| |
| 744 |
|
| void |
| 745 |
|
| keysort_inplace(SV *keygen, AV *values) |
| 746 |
|
| PROTOTYPE: &\@ |
| 747 |
|
| PREINIT: |
| 748 |
1 |
▶ | AV *magic_values=0; |
Branch: 1 branch
| Branch | True | False | Total |
| AV *magic_values=0; |
1 |
0 |
1 |
|
| 749 |
|
| int len; |
| 750 |
|
| ALIAS: |
| 751 |
|
| lkeysort_inplace = 1 |
| 752 |
|
| nkeysort_inplace = 2 |
| 753 |
|
| ikeysort_inplace = 3 |
| 754 |
|
| ukeysort_inplace = 4 |
| 755 |
|
| rkeysort_inplace = 128 |
| 756 |
|
| rlkeysort_inplace = 129 |
| 757 |
|
| rnkeysort_inplace = 130 |
| 758 |
|
| rikeysort_inplace = 131 |
| 759 |
|
| rukeysort_inplace = 132 |
| 760 |
|
| PPCODE: |
| 761 |
1 |
▶ | if ((len=av_len(values)+1)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((len=av_len(values)+1)) { |
1 |
0 |
1 |
|
| 762 |
|
| /* warn("ix=%d\n", ix); */ |
| 763 |
1 |
▶ | if (SvMAGICAL(values) || AvREIFY(values)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvMAGICAL(values) || AvREIFY(values)) { |
1 |
0 |
1 |
| if (SvMAGICAL(values) || AvREIFY(values)) { |
0 |
1 |
1 |
|
| 764 |
|
| int i; |
| 765 |
0 |
| magic_values = values; |
| 766 |
0 |
| values = (AV*)sv_2mortal((SV*)newAV()); |
| 767 |
0 |
| av_extend(values, len-1); |
| 768 |
0 |
▶ | for (i=0; i<len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<len; i++) { |
0 |
0 |
0 |
|
| 769 |
0 |
| SV **currentp = av_fetch(magic_values, i, 0); |
| 770 |
0 |
▶ | av_store( values, i, |
Branch: 1 branch
| Branch | True | False | Total |
| av_store( values, i, |
0 |
0 |
0 |
|
| 771 |
|
| ( currentp |
| 772 |
|
| ? SvREFCNT_inc(*currentp) |
| 773 |
|
| : newSV(0) ) ); |
| 774 |
|
| } |
| 775 |
|
| } |
| 776 |
1 |
| _keysort(aTHX_ ix, keygen, AvARRAY(values), 0, 0, len); |
| 777 |
1 |
| SPAGAIN; |
| 778 |
1 |
▶ | if (magic_values) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (magic_values) { |
0 |
1 |
1 |
|
| 779 |
|
| int i; |
| 780 |
0 |
| SV **values_array = AvARRAY(values); |
| 781 |
0 |
▶ | for(i=0; i<len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<len; i++) { |
0 |
0 |
0 |
|
| 782 |
0 |
| SV *current = values_array[i]; |
| 783 |
0 |
▶ | if (!current) current = &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!current) current = &PL_sv_undef; |
0 |
0 |
0 |
|
| 784 |
0 |
▶ | if (!av_store(magic_values, i, SvREFCNT_inc(current))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!av_store(magic_values, i, SvREFCNT_inc(current))) |
0 |
0 |
0 |
|
| 785 |
0 |
| SvREFCNT_dec(current); |
| 786 |
|
| } |
| 787 |
|
| } |
| 788 |
|
| } |
| 789 |
|
| |
| 790 |
|
| void |
| 791 |
|
| _sort(...) |
| 792 |
|
| PROTOTYPE: @ |
| 793 |
|
| ALIAS: |
| 794 |
|
| lsort = 1 |
| 795 |
|
| nsort = 2 |
| 796 |
|
| isort = 3 |
| 797 |
|
| usort = 4 |
| 798 |
|
| rsort = 128 |
| 799 |
|
| rlsort = 129 |
| 800 |
|
| rnsort = 130 |
| 801 |
|
| risort = 131 |
| 802 |
|
| rusort = 132 |
| 803 |
|
| PPCODE: |
| 804 |
4 |
▶ | if (items) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items) { |
4 |
0 |
4 |
|
| 805 |
4 |
| _keysort(aTHX_ ix, 0, 0, 0, ax, items); |
| 806 |
4 |
| SPAGAIN; |
| 807 |
4 |
| SP = &ST(items-1); |
| 808 |
|
| } |
| 809 |
|
| |
| 810 |
|
| void |
| 811 |
|
| _sort_inplace(AV *values) |
| 812 |
|
| PROTOTYPE: \@ |
| 813 |
|
| PREINIT: |
| 814 |
0 |
▶ | AV *magic_values=0; |
Branch: 1 branch
| Branch | True | False | Total |
| AV *magic_values=0; |
0 |
0 |
0 |
|
| 815 |
|
| int len; |
| 816 |
|
| ALIAS: |
| 817 |
|
| lsort_inplace = 1 |
| 818 |
|
| nsort_inplace = 2 |
| 819 |
|
| isort_inplace = 3 |
| 820 |
|
| usort_inplace = 4 |
| 821 |
|
| rsort_inplace = 128 |
| 822 |
|
| rlsort_inplace = 129 |
| 823 |
|
| rnsort_inplace = 130 |
| 824 |
|
| risort_inplace = 131 |
| 825 |
|
| rusort_inplace = 132 |
| 826 |
|
| PPCODE: |
| 827 |
0 |
▶ | if ((len=av_len(values)+1)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((len=av_len(values)+1)) { |
0 |
0 |
0 |
|
| 828 |
|
| /* warn("ix=%d\n", ix); */ |
| 829 |
0 |
▶ | if (SvMAGICAL(values) || AvREIFY(values)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvMAGICAL(values) || AvREIFY(values)) { |
0 |
0 |
0 |
| if (SvMAGICAL(values) || AvREIFY(values)) { |
0 |
0 |
0 |
|
| 830 |
|
| int i; |
| 831 |
0 |
| magic_values = values; |
| 832 |
0 |
| values = (AV*)sv_2mortal((SV*)newAV()); |
| 833 |
0 |
| av_extend(values, len-1); |
| 834 |
0 |
▶ | for (i=0; i<len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<len; i++) { |
0 |
0 |
0 |
|
| 835 |
0 |
| SV **currentp = av_fetch(magic_values, i, 0); |
| 836 |
0 |
▶ | av_store( values, i, |
Branch: 1 branch
| Branch | True | False | Total |
| av_store( values, i, |
0 |
0 |
0 |
|
| 837 |
|
| ( currentp |
| 838 |
|
| ? SvREFCNT_inc(*currentp) |
| 839 |
|
| : newSV(0) ) ); |
| 840 |
|
| } |
| 841 |
|
| } |
| 842 |
|
| |
| 843 |
0 |
| _keysort(aTHX_ ix, 0, AvARRAY(values), 0, 0, len); |
| 844 |
0 |
| SPAGAIN; |
| 845 |
0 |
▶ | if (magic_values) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (magic_values) { |
0 |
0 |
0 |
|
| 846 |
|
| int i; |
| 847 |
0 |
| SV **values_array = AvARRAY(values); |
| 848 |
0 |
▶ | for(i=0; i<len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<len; i++) { |
0 |
0 |
0 |
|
| 849 |
0 |
| SV *current = values_array[i]; |
| 850 |
0 |
▶ | if (!current) current = &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!current) current = &PL_sv_undef; |
0 |
0 |
0 |
|
| 851 |
0 |
▶ | if (!av_store(magic_values, i, SvREFCNT_inc(current))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!av_store(magic_values, i, SvREFCNT_inc(current))) |
0 |
0 |
0 |
|
| 852 |
0 |
| SvREFCNT_dec(current); |
| 853 |
|
| } |
| 854 |
|
| } |
| 855 |
|
| } |
| 856 |
|
| |
| 857 |
|
| |
| 858 |
|
| PROTOTYPES: DISABLE |
| 859 |
|
| |
| 860 |
|
| CV * |
| 861 |
|
| _multikeysorter(SV *types, SV *gen, SV *post) |
| 862 |
|
| PREINIT: |
| 863 |
|
| AV *defaults; |
| 864 |
|
| CODE: |
| 865 |
13 |
▶ | if (!SvOK(types) || sv_len(types)<1) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvOK(types) || sv_len(types)<1) |
13 |
0 |
13 |
| if (!SvOK(types) || sv_len(types)<1) |
0 |
13 |
13 |
|
| 866 |
0 |
| croak("invalid packed types argument"); |
| 867 |
13 |
| RETVAL = newXS(0, &XS_Sort__Key__multikeysort, __FILE__); |
| 868 |
13 |
| defaults = (AV*)sv_2mortal((SV*)newAV()); |
| 869 |
13 |
| av_store(defaults, 0, newSVsv(types)); |
| 870 |
13 |
| av_store(defaults, 1, newSVsv(gen)); |
| 871 |
13 |
| av_store(defaults, 2, newSVsv(post)); |
| 872 |
13 |
| _xclosure_make(aTHX_ RETVAL, defaults); |
| 873 |
13 |
▶ | if (!SvOK(gen)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(gen)) |
7 |
6 |
13 |
|
| 874 |
7 |
| sv_setpv((SV*)RETVAL, "&@"); |
| 875 |
|
| OUTPUT: |
| 876 |
|
| RETVAL |
| 877 |
|
| |
| 878 |
|
| CV * |
| 879 |
|
| _multikeysorter_inplace(SV *types, SV *gen, SV *post) |
| 880 |
|
| PREINIT: |
| 881 |
|
| AV *defaults; |
| 882 |
|
| CODE: |
| 883 |
11 |
▶ | if (!SvOK(types) || sv_len(types)<1) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvOK(types) || sv_len(types)<1) |
11 |
0 |
11 |
| if (!SvOK(types) || sv_len(types)<1) |
0 |
11 |
11 |
|
| 884 |
0 |
| croak("invalid packed types argument"); |
| 885 |
11 |
| RETVAL = newXS(0, &XS_Sort__Key__multikeysort_inplace, __FILE__); |
| 886 |
11 |
| defaults = (AV*)sv_2mortal((SV*)newAV()); |
| 887 |
11 |
| av_store(defaults, 0, newSVsv(types)); |
| 888 |
11 |
| av_store(defaults, 1, newSVsv(gen)); |
| 889 |
11 |
| av_store(defaults, 2, newSVsv(post)); |
| 890 |
11 |
| _xclosure_make(aTHX_ RETVAL, defaults); |
| 891 |
11 |
▶ | if (!SvOK(gen)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(gen)) |
5 |
6 |
11 |
|
| 892 |
5 |
| sv_setpv((SV*)RETVAL, "&\\@"); |
| 893 |
|
| else |
| 894 |
6 |
| sv_setpv((SV*)RETVAL, "\\@"); |
| 895 |
|
| OUTPUT: |
| 896 |
|
| RETVAL |
| 897 |
|
| |
| 898 |
|
| |