lib/Sort/Bucket.xs

filter: Statementstmt 80.5% 58 / 72 Branchbran 69.2% 36 / 52 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 75.8% 94 / 124 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Sort/Bucket.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 #include "ppport.h"
6  
7 #include "src/sort_bucket.c"
8  
9 MODULE = Sort::Bucket PACKAGE = Sort::Bucket
10  
11 void
12 inplace_bucket_sort(av, bucket_bits=0)
13 AV * av
14 int bucket_bits
15 PROTOTYPE: \@;$
16 INIT:
17 int len;
18 CODE:
19 23974 len = 1 + av_len(av);
20  
21 23974 ▶ if (SvREADONLY(av))
Branch: 1 branch
BranchTrueFalseTotal
if (SvREADONLY(av)) 1 23973 23974
22 1 croak("%s", PL_no_modify);
23  
24 23973 ▶ if (bucket_bits) {
Branch: 1 branch
BranchTrueFalseTotal
if (bucket_bits) { 21834 2139 23973
25 21834 ▶ if (bucket_bits < 1 || bucket_bits > 31) {
Branch: 2 branches
BranchTrueFalseTotal
if (bucket_bits < 1 || bucket_bits > 31) { 21833 1 21834
if (bucket_bits < 1 || bucket_bits > 31) { 1 21832 21833
26 2 croak("%s", "bucket bits out of range");
27 }
28 2139 ▶ } else if (len < 1024) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (len < 1024) { 2123 16 2139
29 2123 bucket_bits = 8;
30 } else {
31 16 int i = len>>12;
32 16 bucket_bits = 8;
33 102 ▶ while (i) {
Branch: 1 branch
BranchTrueFalseTotal
while (i) { 86 16 102
34 86 ++bucket_bits;
35 86 i >>= 1;
36 }
37 }
38  
39 23971 SvREADONLY_on(av);
40 23971 do_bucket_sort(AvARRAY(av), len, bucket_bits);
41 23971 SvREADONLY_off(av);
42  
43 =head1 FUNCTIONS FOR TESTING
44  
45 These functions are intended for use in the test suite.
46  
47 =over
48  
49 =item _set_readonly_for_testing(B<av>)
50  
51 Sets the readonly flag on an AV.
52  
53 =cut
54  
55 void
56 _set_readonly_for_testing(av)
57 AV * av
58 PROTOTYPE: \@
59 CODE:
60 1 SvREADONLY_on(av);
61  
62 =item _ms_do_mergesort_testharness(B<values>, B<offset>, B<sortlen>)
63  
64 This function gives the test suite low-level access to ms_do_mergesort().
65  
66 The B<values> AV must hold U32 values. Calls ms_do_mergesort() on
67 B<values>[B<offset> .. B<offset>+B<sortlen>-1], which will sort those
68 elements partially in place, moving them down B<sortlen>-1 slots in the
69 process. See F<src/mergesort_algo.c>.
70  
71 After this function returns, the elements of B<values> will be error
72 message strings if a problem was detected, otherwise they will be integers
73 giving the index within B<values> of the element that was moved to this
74 slot by the sort.
75  
76 =cut
77  
78 void
79 _ms_do_mergesort_testharness(values, offset, sortlen)
80 AV* values
81 int offset
82 int sortlen
83 PROTOTYPE: \@$$
84 INIT:
85 int vec_len, i, j;
86 ms_elt_t *storage, *orig_storage;
87 unsigned char *pv;
88 STRLEN cur;
89 SV *dummy_sv_vector;
90 CODE:
91 454 ▶ if (sortlen < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (sortlen < 1) { 0 454 454
92 0 croak("sortlen < 1");
93 }
94 454 vec_len = 1 + av_len(values);
95 454 ▶ if (offset + sortlen > vec_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (offset + sortlen > vec_len) { 0 454 454
96 0 croak("sort would read off the top of values");
97 }
98 454 ▶ if (offset < sortlen-1) {
Branch: 1 branch
BranchTrueFalseTotal
if (offset < sortlen-1) { 0 454 454
99 0 croak("sort would write off the bottom of values");
100 }
101  
102 454 Newx(storage, vec_len, ms_elt_t);
103 454 Newxz(dummy_sv_vector, vec_len, SV);
104 235562 ▶ for ( i=0 ; i<vec_len ; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i=0 ; i<vec_len ; i++ ) { 235108 454 235562
105 235108 SV** elt = av_fetch(values, i, 0);
106 235108 ▶ if (!elt) {
Branch: 1 branch
BranchTrueFalseTotal
if (!elt) { 0 235108 235108
107 0 Safefree(dummy_sv_vector); Safefree(storage);
108 0 croak("missing elt at %d", i);
109 }
110 235108 storage[i].mse_key = SvUV(*elt);
111 235108 storage[i].mse_sv = dummy_sv_vector + i;
112 }
113
114 454 Newx(orig_storage, vec_len, ms_elt_t);
115 454 Copy(storage, orig_storage, vec_len, ms_elt_t);
116 454 ms_do_mergesort(storage+offset, sortlen);
117  
118 235562 ▶ for ( i=0 ; i<vec_len ; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i=0 ; i<vec_len ; i++ ) { 235108 454 235562
119 235108 SV** elt = av_fetch(values, i, 0);
120 235108 int orig_pos = storage[i].mse_sv - dummy_sv_vector;
121 235108 ▶ if (orig_pos < 0 || orig_pos >= vec_len) {
Branch: 2 branches
BranchTrueFalseTotal
if (orig_pos < 0 || orig_pos >= vec_len) { 235108 0 235108
if (orig_pos < 0 || orig_pos >= vec_len) { 0 235108 235108
122 0 sv_setpv(*elt, "mse_sv out of bounds");
123 235108 ▶ } else if ( storage[i].mse_key != orig_storage[orig_pos].mse_key ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( storage[i].mse_key != orig_storage[orig_pos].mse_key ) { 0 235108 235108
124 0 sv_setpv(*elt, "mse_key does not match mse_sv");
125 } else {
126 235108 sv_setiv(*elt, orig_pos);
127 }
128 }
129  
130 454 Safefree(orig_storage);
131 454 Safefree(storage);
132 454 Safefree(dummy_sv_vector);
133  
134 =item _cbv_testharness(B<sv>, B<bits>, B<major_out>, B<minor_out>)
135  
136 Compute the major and minor bucket values of a single SV.
137  
138 =cut
139  
140 void
141 _cbv_testharness(sv, bits, major_out, minor_out)
142 SV* sv
143 int bits
144 SV* major_out
145 SV* minor_out
146 PROTOTYPE: $$$$
147 INIT:
148 U32 b_major[1], b_minor[1];
149 CODE:
150 372 ▶ if (!sv || !major_out || !minor_out)
Branch: 3 branches
BranchTrueFalseTotal
if (!sv || !major_out || !minor_out) 372 0 372
if (!sv || !major_out || !minor_out) 372 0 372
if (!sv || !major_out || !minor_out) 0 372 372
151 0 croak("null SV*");
152 372 ▶ if (bits < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (bits < 1) 0 372 372
153 0 croak("bits < 1");
154 372 ▶ if (bits > 31)
Branch: 1 branch
BranchTrueFalseTotal
if (bits > 31) 0 372 372
155 0 croak("bits > 31");
156  
157 372 calculate_bucket_values(1, &sv, b_major, b_minor, bits);
158  
159 372 sv_setnv(major_out, (double)b_major[0]);
160 372 sv_setnv(minor_out, (double)b_minor[0]);
161  
162 =item _cbv_testharness_check_for_descending(B<array>, B<bits>)
163  
164 Compute the major and minor bucket values for an array, using B<bits>
165 major bits. Scan through the bucket values and identify the index of the
166 first element with bucket values lower than the previous element.
167 Return the index, or 0 if the bucket value sequence is non-descending.
168  
169 Handy for testing - you feed it a sorted array and if it finds a
170 point where the bucket values descend that's a bug in the bucket value
171 calculation.
172  
173 =cut
174  
175 int
176 _cbv_testharness_check_for_descending(array, bits)
177 AV* array
178 int bits
179 PROTOTYPE: \@$
180 INIT:
181 U32 *b_major, *b_minor;
182 int len, i;
183 CODE:
184 806 ▶ if (bits < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (bits < 1) 0 806 806
185 0 croak("bits < 1");
186 806 ▶ if (bits > 31)
Branch: 1 branch
BranchTrueFalseTotal
if (bits > 31) 0 806 806
187 0 croak("bits > 31");
188  
189 806 len = 1 + av_len(array);
190 806 Newx(b_major, len, U32);
191 806 Newx(b_minor, len, U32);
192 806 calculate_bucket_values(len, AvARRAY(array), b_major, b_minor, bits);
193  
194 806 RETVAL = 0;
195 53054950 ▶ for ( i=1 ; i<len ; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i=1 ; i<len ; i++ ) { 53054144 806 53054950
196 53054144 ▶ if (b_major[i] < b_major[i-1]
Branch: 1 branch
BranchTrueFalseTotal
if (b_major[i] < b_major[i-1] 53054144 0 53054144
197 53054144 ▶ || (b_major[i] == b_major[i-1] && b_minor[i] < b_minor[i-1])
Branch: 2 branches
BranchTrueFalseTotal
|| (b_major[i] == b_major[i-1] && b_minor[i] < b_minor[i-1]) 30818931 22235213 53054144
|| (b_major[i] == b_major[i-1] && b_minor[i] < b_minor[i-1]) 0 30818931 30818931
198 ) {
199 /* disorder at i */
200 0 RETVAL = i;
201 0 break;
202 }
203 }
204  
205 806 Safefree(b_major);
206 806 Safefree(b_minor);
207 OUTPUT:
208 RETVAL
209  
210 =back
211  
212 =cut
213  
↑ Summary blib/lib/Sort/Bucket.pm »