Radix.xs

filter: Statementstmt 71.4% 225 / 315 Branchbran 59.0% 104 / 176 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 67.0% 329 / 491 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Sort/Key/Radix.pm »
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 #define BE4 1
12 #define LE4 2
13 #define BE8 3
14 #define LE8 4
15 #define LE12 5
16 #define BE12 6
17 #define LE12_x86 7
18 #define BE16 9
19 #define LE16 10
20 #define LE16_x86 11
21  
22 #include "rconfig.h"
23  
24 #define CUTOFF 16
25  
26 #ifdef inline
27 #undef inline
28 #endif
29  
30 #if defined(__GNU__)
31 #define inline __inline__
32 #else
33 #define inline
34 #endif
35  
36  
37 static void
38 0 print_keys(pTHX_ char *name, unsigned char **keys, int n, int byte) {
39 int i;
40 0 printf("%s[%d] %p:", name, byte, keys);
41 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
42 int j;
43 0 UV acu = 0;
44 0 printf(" ");
45 0 ▶ for (j = byte; j >= 0; j--)
Branch: 1 branch
BranchTrueFalseTotal
for (j = byte; j >= 0; j--) 0 0 0
46 0 printf("%02x", keys[i][j]);
47 0 printf("(");
48 0 ▶ for (j = byte; j >= 0; j--)
Branch: 1 branch
BranchTrueFalseTotal
for (j = byte; j >= 0; j--) 0 0 0
49 0 ▶ printf("%c", (isalnum(keys[i][j]) ? keys[i][j] : '.'));
Branch: 1 branch
BranchTrueFalseTotal
printf("%c", (isalnum(keys[i][j]) ? keys[i][j] : '.')); 0 0 0
50 0 printf(")");
51 }
52 0 printf("\n\n");
53 0 fflush(stdout);
54 0 }
55  
56 static void
57 0 print_blens(pTHX_ int *blens) {
58 int i;
59 0 printf("blens:");
60 0 ▶ for (i = 0; i < 256; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 256; i++) 0 0 0
61 0 printf(" %d", blens[i]);
62 0 printf("\n\n");
63 0 fflush(stdout);
64 0 }
65  
66 static void
67 119734 radix_sort_1(unsigned char **keys, unsigned char **temps, int n, int byte) {
68
69 /* byte must be equal or greater than 0! */
70 /* assert (byte >= 0); */
71  
72 119734 ▶ if (n > CUTOFF) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > CUTOFF) { 5110 114624 119734
73 unsigned char **bucket[256];
74 int blen[256];
75 int i;
76  
77 5110 Zero(blen, 256, int);
78
79 1862184 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 1857074 5110 1862184
80 1857074 blen[keys[i][byte]]++;
81  
82 /* print_blens(blen); */
83
84 5110 bucket[0] = temps;
85 1308160 ▶ for (i = 0; i < 255; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 255; i++) 1303050 5110 1308160
86 1303050 bucket[i + 1] = bucket[i] + blen[i];
87
88 1862184 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 1857074 5110 1862184
89 1857074 *(bucket[keys[i][byte]]++) = keys[i];
90  
91 5110 ▶ if (byte > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (byte > 0) { 2586 2524 5110
92 2586 byte--;
93 664602 ▶ for (i = 0; i < 256; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 256; i++) { 662016 2586 664602
94 662016 int bli = blen[i];
95 662016 ▶ if (bli) {
Branch: 1 branch
BranchTrueFalseTotal
if (bli) { 119703 542313 662016
96 119703 radix_sort_1(temps, keys, bli, byte);
97 119703 keys += bli;
98 119703 temps += bli;
99 }
100 }
101 }
102 }
103 else {
104 /* insertion sort */
105
106 114624 ▶ unsigned char **dest = (byte & 1) ? keys : temps;
Branch: 1 branch
BranchTrueFalseTotal
unsigned char **dest = (byte & 1) ? keys : temps; 69741 44883 114624
107 int i;
108  
109 255230 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 140606 114624 255230
110 140606 unsigned char *pivot = keys[i];
111 140606 int j = i;
112 162966 ▶ while (j) {
Branch: 1 branch
BranchTrueFalseTotal
while (j) { 38325 124641 162966
113 int k;
114 43878 ▶ for (k = byte; k >= 0; k--) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = byte; k >= 0; k--) { 41136 2742 43878
115 41136 unsigned char db = dest[j - 1][k];
116
117 41136 ▶ if (pivot[k] > db)
Branch: 1 branch
BranchTrueFalseTotal
if (pivot[k] > db) 13223 27913 41136
118 13223 goto insert_now;
119  
120 27913 ▶ if (pivot[k] < db) {
Branch: 1 branch
BranchTrueFalseTotal
if (pivot[k] < db) { 22360 5553 27913
121 /* printf("pivot is <\n"); */
122 22360 dest[j] = dest[j - 1];
123 22360 goto continue_loop_j;
124 }
125 }
126 2742 break;
127 22360 continue_loop_j:
128 22360 --j;
129 }
130 124641 insert_now:
131 140606 dest[j] = pivot;
132 }
133 }
134 119734 }
135  
136 static unsigned char *
137 31 init_keys(pTHX_
138 SV **data, I32 offset, I32 ax, int n,
139 SV *keygen,
140 void (*sv_to_key)(pTHX_ SV *, unsigned char *, int, int *),
141 unsigned char ***keys, unsigned char ***temps, int klen, int *byte) {
142 unsigned char *start, *key;
143 int i;
144 SV **svkeys;
145  
146 31 Newx(*keys, n * 2, unsigned char *);
147 31 SAVEFREEPV(*keys);
148 31 *temps = *keys + n;
149
150 31 Newx(start, n * klen, unsigned char);
151 31 SAVEFREEPV(start);
152
153 31 *byte = 0;
154 31 key = start;
155  
156 431039 ▶ for (i = 0; i < n; i++, key += klen) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++, key += klen) { 431008 31 431039
157 431008 ▶ SV *current = data ? data[i] : ST(i + offset);
Branch: 1 branch
BranchTrueFalseTotal
SV *current = data ? data[i] : ST(i + offset); 150003 281005 431008
158 431008 (*keys)[i] = key;
159 431008 ▶ if (keygen) {
Branch: 1 branch
BranchTrueFalseTotal
if (keygen) { 110000 321008 431008
160 110000 dSP;
161 IV count;
162 SV *result;
163 /* printf ("calc key\n"); fflush(stdout); */
164 110000 ENTER;
165 110000 SAVETMPS;
166 110000 SAVE_DEFSV;
167 110000 ▶ DEFSV = sv_2mortal(current ? SvREFCNT_inc(current) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
DEFSV = sv_2mortal(current ? SvREFCNT_inc(current) : newSV(0)); 110000 0 110000
DEFSV = sv_2mortal(current ? SvREFCNT_inc(current) : newSV(0)); 110000 0 110000
168 110000 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 110000 110000
169 110000 PUTBACK;
170 110000 count = call_sv(keygen, G_SCALAR);
171 110000 SPAGAIN;
172 110000 ▶ if (count != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) 0 110000 110000
173 0 Perl_croak(aTHX_ "wrong number of results returned from key generation sub");
174 110000 result = POPs;
175 110000 (*sv_to_key)(aTHX_ result, key, klen, byte);
176 110000 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 110000 0 110000
177 110000 LEAVE;
178 }
179 else {
180 321008 (*sv_to_key)(aTHX_ current, key, klen, byte);
181 }
182
183 }
184 31 return start;
185 }
186  
187 static int
188 3 calc_klen_pv(pTHX_ SV **keys, I32 offset, I32 ax, int n) {
189 3 int klen = 0;
190 int i;
191  
192 200006 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 200003 3 200006
193 200003 ▶ SV *current = keys ? keys[i] : ST(offset + i);
Branch: 1 branch
BranchTrueFalseTotal
SV *current = keys ? keys[i] : ST(offset + i); 100003 100000 200003
194 200003 ▶ if (current) {
Branch: 1 branch
BranchTrueFalseTotal
if (current) { 200003 0 200003
195 200003 STRLEN cur = SvCUR(current);
196 200003 ▶ if (cur > klen)
Branch: 1 branch
BranchTrueFalseTotal
if (cur > klen) 6 199997 200003
197 6 klen = cur;
198 }
199 }
200 3 return klen;
201 }
202  
203 static SV **
204 2 calc_svkeys(pTHX_ SV *keygen, SV **data, I32 offset, I32 ax, int n) {
205 int i;
206 2 SV **keys = 0;
207 AV *av;
208  
209 2 av = newAV();
210 2 sv_2mortal((SV*)av);
211 2 av_extend(av, n);
212 2 keys = AvARRAY(av);
213
214 100005 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 100003 2 100005
215 100003 dSP;
216 IV count;
217 SV *result;
218 100003 ▶ SV *current = data ? data[i] : ST(offset + i);
Branch: 1 branch
BranchTrueFalseTotal
SV *current = data ? data[i] : ST(offset + i); 0 100003 100003
219 100003 ENTER;
220 100003 SAVETMPS;
221 100003 SAVE_DEFSV;
222 100003 ▶ DEFSV = sv_2mortal(current ? SvREFCNT_inc(current) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
DEFSV = sv_2mortal(current ? SvREFCNT_inc(current) : newSV(0)); 100003 0 100003
DEFSV = sv_2mortal(current ? SvREFCNT_inc(current) : newSV(0)); 100003 0 100003
223 100003 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 100003 100003
224 100003 PUTBACK;
225 100003 count = call_sv(keygen, G_SCALAR);
226 100003 SPAGAIN;
227 100003 ▶ if (count != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) 0 100003 100003
228 0 Perl_croak(aTHX_ "wrong number of results returned from key generation sub");
229 100003 keys[i] = result = POPs;
230 100003 ▶ if (result)
Branch: 1 branch
BranchTrueFalseTotal
if (result) 100003 0 100003
231 100003 SvREFCNT_inc(result);
232 100003 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 100003 0 100003
233 100003 LEAVE;
234 }
235 2 return keys;
236 }
237  
238 static void
239 31 resort_as_keys(pTHX_ SV **data, SV **dest, unsigned char *start, unsigned char **keys, int n, int klen) {
240 31 SV **sorted = (SV**)keys;
241 int i;
242 431039 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 431008 31 431039
243 431008 int ix = (keys[i] - start) / klen;
244 431008 sorted[i] = data[ix];
245 }
246 31 Move(sorted, dest, n, SV *);
247 31 }
248  
249 inline static void
250 0 uv_to_key(pTHX_ UV uv, unsigned char *key, int *byte) {
251 #if UV_FORMAT == LE4 || UV_FORMAT == LE8
252 0 *((UV*)key) = uv;
253 0 ▶ while (uv_byte_mask[*byte] & uv)
Branch: 1 branch
BranchTrueFalseTotal
while (uv_byte_mask[*byte] & uv) 0 0 0
254 0 (*byte)++;
255 #else
256 int i;
257 for (i = sizeof(UV) - 1; i >= 0; i--) {
258 unsigned char uc = (uv >> (8 * i));
259 key[i] = uc;
260 if (uc && i > *byte)
261 *byte = i;
262 }
263 #endif
264 0 }
265  
266 static void
267 31005 sv_uv_to_key(pTHX_ SV *sv, unsigned char *key, int klen, int *byte) {
268 #if UV_FORMAT == LE4 || UV_FORMAT == LE8
269 31005 UV uv = SvUV(sv);
270 31005 *((UV*)key) = uv;
271 31021 ▶ while (uv_byte_mask[*byte] & uv)
Branch: 1 branch
BranchTrueFalseTotal
while (uv_byte_mask[*byte] & uv) 16 31005 31021
272 16 (*byte)++;
273 #else
274 uv_to_key(aTHX_ SvUV(sv), key, byte);
275 #endif
276 31005 }
277  
278 static void
279 30000 sv_ruv_to_key(pTHX_ SV *sv, unsigned char *key, int klen, int *byte) {
280 #if UV_FORMAT == LE4 || UV_FORMAT == LE8
281 30000 UV uv = ~SvUV(sv);
282 30000 *((UV*)key) = uv;
283 30021 ▶ while (uv_byte_mask[*byte] & uv)
Branch: 1 branch
BranchTrueFalseTotal
while (uv_byte_mask[*byte] & uv) 21 30000 30021
284 21 (*byte)++;
285 #else
286 uv_to_key(aTHX_ ~SvUV(sv), key, byte);
287 #endif
288 30000 }
289  
290 static void
291 60000 sv_iv_to_key(pTHX_ SV *iv, unsigned char *key, int klen, int *byte) {
292 #if UV_FORMAT == LE4 || UV_FORMAT == LE8
293 60000 UV uv = SvIV(iv) + (((UV)1) << (8 * sizeof(IV) - 1));
294 60000 *((UV*)key) = uv;
295 60042 ▶ while (uv_byte_mask[*byte] & uv)
Branch: 1 branch
BranchTrueFalseTotal
while (uv_byte_mask[*byte] & uv) 42 60000 60042
296 42 (*byte)++;
297 #else
298 uv_to_key(aTHX_ SvIV(iv) + (((UV)1) << (8 * sizeof(IV) - 1)), key, byte);
299 #endif
300 60000 }
301  
302 static void
303 50000 sv_riv_to_key(pTHX_ SV *iv, unsigned char *key, int klen, int *byte) {
304 #if UV_FORMAT == LE4 || UV_FORMAT == LE8
305 50000 UV uv = ~(UV)(SvIV(iv) + (((UV)1) << (8 * sizeof(IV) - 1)));
306 50000 *((UV*)key) = uv;
307 50035 ▶ while (uv_byte_mask[*byte] & uv)
Branch: 1 branch
BranchTrueFalseTotal
while (uv_byte_mask[*byte] & uv) 35 50000 50035
308 35 (*byte)++;
309 #else
310 uv_to_key(aTHX_ ~(UV)(SvIV(iv) + (((UV)1) << (8 * sizeof(IV) - 1))), key, byte);
311 #endif
312 50000 }
313  
314 #if NV_FORMAT == LE12_x86 || NV_FORMAT == LE16_x86
315 #define NVBYTE1 10
316 #else
317 #define NVBYTE1 (sizeof(NV))
318 #endif
319  
320 static void
321 60000 nv_to_key(pTHX_ NV nv, unsigned char *key, int klen, int *byte) {
322 #if defined(NV_FORMAT)
323 int i;
324 60000 *((NV *)key) = nv;
325 #if NV_FORMAT == BE8 || NV_FORMAT == BE12 || NV_FORMAT == BE16
326 for (i = 0; i < (NVBYTE1 / 2); i++) {
327 unsigned char tmp;
328 tmp = key[i];
329 key[i] = key[(NVBYTE1 - 1) - i];
330 key[(NVBYTE1 - 1) - i] = tmp;
331 }
332 #endif
333 60000 ▶ if (nv >= 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if (nv >= 0.0) 35039 24961 60000
334 35039 key[NVBYTE1 - 1] |= 0x80;
335 else
336 224649 ▶ for (i = 0; i < NVBYTE1; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < NVBYTE1; i++) 199688 24961 224649
337 199688 key[i] = ~key[i];
338 60000 *byte = NVBYTE1 - 1;
339 #else
340 Perl_croak(aTHX_ "Sorting of floating point keys is not supported on this computer. Please, send a bug report to Sort::Key::Radix author (0.124 => " NV_0124 ")");
341 #endif
342 60000 }
343  
344 static void
345 40000 sv_nv_to_key(pTHX_ SV *sv, unsigned char *key, int klen, int *byte) {
346 40000 nv_to_key(aTHX_ SvNV(sv), key, klen, byte);
347 40000 }
348  
349 static void
350 20000 sv_rnv_to_key(pTHX_ SV *sv, unsigned char *key, int klen, int *byte) {
351 20000 nv_to_key(aTHX_ -SvNV(sv), key, klen, byte);
352 20000 }
353  
354 static void
355 0 sf_to_key(pTHX_ float sf, unsigned char *key, int klen, int *byte) {
356 #if defined(SF_FORMAT)
357 int i;
358 0 *((float *)key) = sf;
359 #if SF_FORMAT == BE8 || SF_FORMAT == BE4 || SF_FORMAT == BE12 || SF_FORMAT == BE16
360 for (i = 0; i < (sizeof(float) >> 1); i++) {
361 unsigned char tmp;
362 tmp = key[i];
363 key[i] = key[(sizeof(float) - 1) - i];
364 key[(sizeof(float) - 1) - i] = tmp;
365 }
366 #endif
367 0 ▶ if (sf >= 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if (sf >= 0.0) 0 0 0
368 0 key[sizeof(float) - 1] |= 0x80;
369 else
370 0 ▶ for (i = 0; i < sizeof(float); i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sizeof(float); i++) 0 0 0
371 0 key[i] = ~key[i];
372 0 *byte = sizeof(float) - 1;
373 #else
374 Perl_croak(aTHX_ "Sorting of single floating point keys is not supported on this computer. Please, send a bug report to Sort::Key::Radix author (0.124 => " SF_0124 ")");
375 #endif
376 0 }
377  
378 static void
379 0 sv_sf_to_key(pTHX_ SV *sv, unsigned char *key, int klen, int *byte) {
380 0 sf_to_key(aTHX_ SvNV(sv), key, klen, byte);
381 0 }
382  
383 static void
384 0 sv_rsf_to_key(pTHX_ SV *sv, unsigned char *key, int klen, int *byte) {
385 0 sf_to_key(aTHX_ -SvNV(sv), key, klen, byte);
386 0 }
387  
388 static void
389 200003 sv_pv_to_key(pTHX_ SV *sv, unsigned char *key, int klen, int *byte) {
390 STRLEN len, i;
391 200003 char *pv = SvPV(sv, len);
392 /* printf("sv: %s, len: %d\n", pv, len); fflush(stdout); */
393 679061 ▶ for (i = 0; i < klen && i < len; i++)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < klen && i < len; i++) 588962 90099 679061
for (i = 0; i < klen && i < len; i++) 479058 109904 588962
394 479058 key[klen - 1 - i] = pv[i];
395 200003 ▶ if (*byte < i - 1)
Branch: 1 branch
BranchTrueFalseTotal
if (*byte < i - 1) 6 199997 200003
396 6 *byte = i - 1;
397 320987 ▶ for (; i < klen; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (; i < klen; i++) 120984 200003 320987
398 120984 key[klen - 1 - i] = 0;
399 200003 }
400  
401 static void
402 0 sv_rpv_to_key(pTHX_ SV *sv, unsigned char *key, int klen, int *byte) {
403 STRLEN len, i;
404 0 char *pv = SvPV(sv, len);
405 0 ▶ for (i = 0; i < klen && i < len; i++)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < klen && i < len; i++) 0 0 0
for (i = 0; i < klen && i < len; i++) 0 0 0
406 0 key[klen - 1 - i] = ~pv[i];
407 0 ▶ if (*byte < i - 1)
Branch: 1 branch
BranchTrueFalseTotal
if (*byte < i - 1) 0 0 0
408 0 *byte = i - 1;
409 0 ▶ for (; i < klen; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (; i < klen; i++) 0 0 0
410 0 key[klen - 1 - i] = ~0;
411 0 }
412  
413 static void
414 31 radix_sort(pTHX_ IV type, SV *keygen, SV **values, I32 offset, I32 ax, IV n) {
415 31 dSP;
416 31 ▶ if (n) {
Branch: 1 branch
BranchTrueFalseTotal
if (n) { 31 0 31
417 unsigned char **keys, **temps, *start;
418 int klen, byte;
419 int (*calc_klen)(pTHX_ SV **, I32, I32, int);
420 void (*sv_to_key)(pTHX_ SV *, unsigned char *, int, int *);
421 int hints;
422
423 31 calc_klen = 0;
424  
425 #if (PERL_VERSION < 9)
426 hints = PL_curcop->op_private;
427 #else
428 31 hints = CopHINTS_get(PL_curcop);
429 #endif
430  
431 31 ▶ if ((type == 2 || type == 130) && (hints & HINT_INTEGER))
Branch: 3 branches
BranchTrueFalseTotal
if ((type == 2 || type == 130) && (hints & HINT_INTEGER)) 24 7 31
if ((type == 2 || type == 130) && (hints & HINT_INTEGER)) 4 20 24
if ((type == 2 || type == 130) && (hints & HINT_INTEGER)) 5 6 11
432 5 type |= 1;
433
434 31 switch(type) {
435 4 case 2:
436 4 klen = sizeof(NV);
437 4 sv_to_key = &sv_nv_to_key;
438 4 break;
439 6 case 3:
440 6 klen = sizeof(UV);
441 6 sv_to_key = &sv_iv_to_key;
442 6 break;
443 8 case 4:
444 8 klen = sizeof(UV);
445 8 sv_to_key = &sv_uv_to_key;
446 8 break;
447 0 case 5:
448 0 klen = sizeof(float);
449 0 sv_to_key = &sv_sf_to_key;
450 0 break;
451 3 case 7:
452 3 klen = 0;
453 3 calc_klen = *calc_klen_pv;
454 3 sv_to_key = &sv_pv_to_key;
455 3 break;
456 2 case 130:
457 2 klen = sizeof(NV);
458 2 sv_to_key = &sv_rnv_to_key;
459 2 break;
460 5 case 131:
461 5 klen = sizeof(UV);
462 5 sv_to_key = &sv_riv_to_key;
463 5 break;
464 3 case 132:
465 3 klen = sizeof(UV);
466 3 sv_to_key = &sv_ruv_to_key;
467 3 break;
468 0 case 133:
469 0 klen = sizeof(float);
470 0 sv_to_key = &sv_rsf_to_key;
471 0 break;
472 0 case 135:
473 0 klen = 0;
474 0 calc_klen = *calc_klen_pv;
475 0 sv_to_key = &sv_rpv_to_key;
476 0 break;
477  
478 0 default:
479 0 Perl_croak(aTHX_ "internal error: bad ix value");
480 }
481  
482 31 ▶ if (calc_klen) {
Branch: 1 branch
BranchTrueFalseTotal
if (calc_klen) { 3 28 31
483 3 SV **svkeys = ( keygen
484 2 ? calc_svkeys(aTHX_ keygen, values, offset, ax, n)
485 3 ▶ : values );
Branch: 1 branch
BranchTrueFalseTotal
: values ); 2 1 3
486 3 klen = (*calc_klen)(aTHX_ svkeys, offset, ax, n);
487 3 ▶ if (klen) {
Branch: 1 branch
BranchTrueFalseTotal
if (klen) { 3 0 3
488 3 start = init_keys(aTHX_
489 svkeys, offset, ax, n, NULL,
490 sv_to_key, &keys, &temps, klen, &byte);
491  
492 /* if (n) print_keys("subkeys", keys, n, byte); */
493
494  
495 }
496 }
497 28 ▶ else if (klen) {
Branch: 1 branch
BranchTrueFalseTotal
else if (klen) { 28 0 28
498 28 start = init_keys(aTHX_
499 values, offset, ax, n, keygen,
500 sv_to_key, &keys, &temps, klen, &byte);
501 }
502 31 ▶ if (byte >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (byte >= 0) { 31 0 31
503 31 SPAGAIN;
504 /* printf("byte: %d\n", byte); */
505 31 radix_sort_1(keys, temps, n, byte);
506
507 31 ▶ if (values)
Branch: 1 branch
BranchTrueFalseTotal
if (values) 5 26 31
508 5 resort_as_keys(aTHX_
509 values, values, start,
510 5 ▶ ((byte & 1) ? keys : temps),
Branch: 1 branch
BranchTrueFalseTotal
((byte & 1) ? keys : temps), 5 0 5
511 n, klen);
512 else
513 26 resort_as_keys(aTHX_
514 26 &(ST(offset)), &(ST(0)), start,
515 26 ▶ ((byte & 1) ? keys : temps),
Branch: 1 branch
BranchTrueFalseTotal
((byte & 1) ? keys : temps), 20 6 26
516 n, klen);
517 }
518 }
519 31 }
520  
521 MODULE = Sort::Key::Radix PACKAGE = Sort::Key::Radix
522  
523 void
524 _sort(...)
525 ALIAS:
526 nsort = 2
527 isort = 3
528 usort = 4
529 fsort = 5
530 ssort = 7
531 rnsort = 130
532 risort = 131
533 rusort = 132
534 rfsort = 133
535 rssort = 135
536 PPCODE:
537 14 radix_sort(aTHX_ ix, 0, 0, 0, ax, items);
538 14 SPAGAIN;
539 14 XSRETURN(items);
540  
541 void
542 _sort_inplace(AV *values)
543 PROTOTYPE: \@
544 PREINIT:
545 4 ▶ AV *magic_values = 0;
Branch: 1 branch
BranchTrueFalseTotal
AV *magic_values = 0; 4 0 4
546 int len;
547 ALIAS:
548 nsort_inplace = 2
549 isort_inplace = 3
550 usort_inplace = 4
551 fsort_inplace = 5
552 ssort_inplace = 7
553 rnsort_inplace = 130
554 risort_inplace = 131
555 rusort_inplace = 132
556 rfsort_inplace = 133
557 rssort_inplace = 135
558 CODE:
559 4 ▶ if ((len = av_len(values) + 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((len = av_len(values) + 1)) { 4 0 4
560 /* warn("ix=%d\n", ix); */
561 4 ▶ if (SvMAGICAL(values) || AvREIFY(values)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvMAGICAL(values) || AvREIFY(values)) { 4 0 4
if (SvMAGICAL(values) || AvREIFY(values)) { 0 4 4
562 int i;
563 0 magic_values = values;
564 0 values = (AV*)sv_2mortal((SV*)newAV());
565 0 av_extend(values, len-1);
566 0 ▶ for (i=0; i<len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<len; i++) { 0 0 0
567 0 SV **currentp = av_fetch(magic_values, i, 0);
568 0 ▶ av_store( values, i,
Branch: 1 branch
BranchTrueFalseTotal
av_store( values, i, 0 0 0
569 ( currentp
570 ? SvREFCNT_inc(*currentp)
571 : newSV(0) ) );
572 }
573 }
574 4 radix_sort(aTHX_ ix, 0, AvARRAY(values), 0, 0, len);
575 4 SPAGAIN;
576 4 ▶ if (magic_values) {
Branch: 1 branch
BranchTrueFalseTotal
if (magic_values) { 0 4 4
577 int i;
578 0 SV **values_array = AvARRAY(values);
579 0 ▶ for(i=0; i<len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<len; i++) { 0 0 0
580 0 SV *current = values_array[i];
581 0 ▶ if (!current) current = &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
if (!current) current = &PL_sv_undef; 0 0 0
582 0 ▶ if (!av_store(magic_values, i, SvREFCNT_inc(current)))
Branch: 1 branch
BranchTrueFalseTotal
if (!av_store(magic_values, i, SvREFCNT_inc(current))) 0 0 0
583 0 SvREFCNT_dec(current);
584 }
585 }
586 }
587  
588 void
589 _keysort(SV *keygen, ...)
590 PROTOTYPE: &@
591 ALIAS:
592 nkeysort = 2
593 ikeysort = 3
594 ukeysort = 4
595 fkeysort = 5
596 skeysort = 7
597 rnkeysort = 130
598 rikeysort = 131
599 rukeysort = 132
600 rfkeysort = 133
601 rskeysort = 135
602 PPCODE:
603 12 radix_sort(aTHX_ ix, keygen, 0, 1, ax, items - 1);
604 12 SPAGAIN;
605 12 XSRETURN(items - 1);
606  
607  
608 void
609 _keysort_inplace(SV *keygen, AV *values)
610 PROTOTYPE: &\@
611 PREINIT:
612 1 ▶ AV *magic_values = 0;
Branch: 1 branch
BranchTrueFalseTotal
AV *magic_values = 0; 1 0 1
613 int len;
614 ALIAS:
615 nkeysort_inplace = 2
616 ikeysort_inplace = 3
617 ukeysort_inplace = 4
618 fkeysort_inplace = 5
619 skeysort_inplace = 7
620 rnkeysort_inplace = 130
621 rikeysort_inplace = 131
622 rukeysort_inplace = 132
623 rfkeysort_inplace = 133
624 rskeysort_inplace = 135
625 CODE:
626 1 ▶ if ((len = av_len(values) + 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((len = av_len(values) + 1)) { 1 0 1
627 /* warn("ix=%d\n", ix); */
628 1 ▶ if (SvMAGICAL(values) || AvREIFY(values)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvMAGICAL(values) || AvREIFY(values)) { 1 0 1
if (SvMAGICAL(values) || AvREIFY(values)) { 0 1 1
629 int i;
630 0 magic_values = values;
631 0 values = (AV*)sv_2mortal((SV*)newAV());
632 0 av_extend(values, len-1);
633 0 ▶ for (i=0; i<len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<len; i++) { 0 0 0
634 0 SV **currentp = av_fetch(magic_values, i, 0);
635 0 ▶ av_store( values, i,
Branch: 1 branch
BranchTrueFalseTotal
av_store( values, i, 0 0 0
636 ( currentp
637 ? SvREFCNT_inc(*currentp)
638 : newSV(0) ) );
639 }
640 }
641 1 radix_sort(aTHX_ ix, keygen, AvARRAY(values), 0, 0, len);
642 1 SPAGAIN;
643 1 ▶ if (magic_values) {
Branch: 1 branch
BranchTrueFalseTotal
if (magic_values) { 0 1 1
644 int i;
645 0 SV **values_array = AvARRAY(values);
646 0 ▶ for(i=0; i<len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<len; i++) { 0 0 0
647 0 SV *current = values_array[i];
648 0 ▶ if (!current) current = &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
if (!current) current = &PL_sv_undef; 0 0 0
649 0 ▶ if (!av_store(magic_values, i, SvREFCNT_inc(current)))
Branch: 1 branch
BranchTrueFalseTotal
if (!av_store(magic_values, i, SvREFCNT_inc(current))) 0 0 0
650 0 SvREFCNT_dec(current);
651 }
652 }
653 }
654  
↑ Summary blib/lib/Sort/Key/Radix.pm »