src/sort_bucket.c

filter: Statementstmt 100% 71 / 71 Branchbran 100% 26 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 100% 97 / 97 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/mergesort_algo.c ↑ Summary
1 /*
2 =head1 NAME
3  
4 sort_bucket.c - the main bucket sort code
5  
6 =head1 SYNOPSIS
7  
8 #include "sort_bucket.c"
9  
10 ...
11  
12 do_bucket_sort(...);
13  
14 =head1 DESCRIPTION
15  
16 Provides a function to perform an in-place bucket sort on a vector of SVs:
17  
18 static void
19 do_bucket_sort(SV** svs, int svcount, int bucket_bits);
20  
21 The parameters are:
22  
23 =over
24  
25 =item SV** B<svs>
26  
27 A pointer to the start of the SV vector.
28  
29 =item int B<svcount>
30  
31 The number of SVs in the vector.
32  
33 =item int B<bucket_bits>
34  
35 The number of bits to use in the major bucket values. Must not be less than
36 1 or more than 31. The input SVs will be distributed into 2^B<bucket_bits>
37 buckets.
38  
39 =back
40  
41 =cut
42 */
43  
44 typedef struct {
45 U32 mse_key;
46 SV* mse_sv;
47 } ms_elt_t;
48 #define ms_cp(dest, src) ( (dest) = (src) )
49 #define ms_gt(a,b) ( (a).mse_key > (b).mse_key )
50 #include "src/mergesort_algo.c"
51  
52 #include "src/calculate_bucket_values.c"
53  
54 static void sb__init_by_bucket_cursor(
55 ms_elt_t *by_bucket, ms_elt_t** by_bucket_cursor,
56 int major_bucket_count, int *count_by_bucket);
57  
58 static void sb__sort_within_major_buckets(
59 ms_elt_t **by_bucket_cursor, int major_bucket_count,
60 int* count_by_bucket, SV** dest);
61  
62 static void sb__populate_count_by_bucket(int *count_by_bucket,
63 U32 *major_b, U32 *major_b_end);
64  
65 static void sb__populate_buckets(
66 ms_elt_t** by_bucket_cursor, U32* major_b, U32 *minor_b,
67 SV** svs, int svcount);
68  
69 static int sb__max_count_by_bucket(
70 int major_bucket_count, int *count_by_bucket);
71  
72 static void sb__init_by_bucket_cursor(
73 ms_elt_t *by_bucket, ms_elt_t** by_bucket_cursor,
74 int major_bucket_count, int *count_by_bucket);
75  
76 static void sb__sort_within_major_buckets(
77 ms_elt_t **by_bucket_cursor, int major_bucket_count,
78 int* count_by_bucket, SV** dest);
79  
80 static void
81 23971 do_bucket_sort(SV** svs, int svcount, int bucket_bits)
82 {
83 U32 *minor_b;
84 U32 *major_b;
85 23971 int major_bucket_count = 1 << bucket_bits;
86 int *count_by_bucket, max_count_by_bucket;
87 ms_elt_t *by_bucket_storage, *by_bucket;
88 ms_elt_t **by_bucket_cursor_storage, **by_bucket_cursor;
89  
90 /* Compute the major and minor bucket values for each SV */
91 23971 Newx(major_b, svcount, U32);
92 23971 Newx(minor_b, svcount, U32);
93 23971 calculate_bucket_values(svcount, svs, major_b, minor_b, bucket_bits);
94  
95 /* Count the number of SVs that will fall into each major bucket */
96 23971 Newxz(count_by_bucket, major_bucket_count, int);
97 23971 sb__populate_count_by_bucket(count_by_bucket, major_b, major_b+svcount);
98  
99 23971 max_count_by_bucket = sb__max_count_by_bucket(
100 major_bucket_count, count_by_bucket);
101
102 /* Allocate storage for a vector of count (SV*, minor bucket) pairs,
103 * into which we will load the input SVs and their minor bucket values
104 * in major bucket order. Allow max_count_by_bucket extra slots at
105 * the start, to be sure there's enough room for the partially in-place
106 * sort within each bucket. */
107 23971 Newx(by_bucket_storage, svcount+max_count_by_bucket, ms_elt_t);
108 23971 by_bucket = by_bucket_storage + max_count_by_bucket;
109  
110 /* We'll need a vector of pointers into by_bucket, to keep track of
111 * where we should place the next entry for each possible major bucket
112 * value. */
113 23971 Newx(by_bucket_cursor_storage, major_bucket_count+1, ms_elt_t*);
114 23971 by_bucket_cursor = by_bucket_cursor_storage+1;
115 23971 sb__init_by_bucket_cursor(by_bucket, by_bucket_cursor,
116 major_bucket_count, count_by_bucket);
117  
118 /* Now we're ready to load the SVs and their minor bucket values into
119 * by_bucket in major bucket order. */
120 23971 sb__populate_buckets(by_bucket_cursor, major_b, minor_b, svs, svcount);
121 23971 Safefree(major_b);
122 23971 Safefree(minor_b);
123  
124 /* Now by_bucket_cursor[b] points to the first slot beyond bucket b,
125 * i.e. the start of bucket b+1. Adjust so that by_bucket_cursor[b]
126 * points to the start of bucket b again. */
127 23971 --by_bucket_cursor;
128 23971 by_bucket_cursor[0] = by_bucket;
129
130 /* Sort the SVs within each major bucket, and copy the sorted SV*s
131 * into the SV vector that we're sorting. */
132 23971 sb__sort_within_major_buckets(
133 by_bucket_cursor, major_bucket_count, count_by_bucket, svs);
134  
135 23971 Safefree(by_bucket_cursor_storage);
136 23971 Safefree(by_bucket_storage);
137 23971 Safefree(count_by_bucket);
138 23971 }
139  
140 /*
141 =head1 PRIVATE FUNCTIONS
142  
143 Functions with names starting in C<sb__> are not intended for use outside
144 this file.
145  
146 =over
147  
148 =item sb__populate_count_by_bucket(B<count_by_bucket>, B<major_b>, B<major_b_end>)
149  
150 Totals up the number of SVs that take each possible major bucket value, and
151 populates the B<count_by_bucket> vector of ints.
152  
153 The elements of B<count_by_bucket> must initially be 0.
154  
155 =cut
156 */
157  
158 static void
159 23971 sb__populate_count_by_bucket(int *count_by_bucket,
160 U32 *major_b, U32 *major_b_end)
161 {
162 11366245 ▶ while ( major_b < major_b_end ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( major_b < major_b_end ) { 11342274 23971 11366245
163 11342274 ++count_by_bucket[ *major_b++ ];
164 }
165 23971 }
166  
167 /*
168 =item sb__populate_buckets(B<by_bucket_cursor>, B<major_b>, B<minor_b>, B<svs>, B<svcount>)
169  
170 Copies the input SVs into the vector of SV/minor_bucket pairs.
171  
172 =cut
173 */
174  
175 static void
176 23971 sb__populate_buckets(ms_elt_t** by_bucket_cursor, U32* major_b, U32 *minor_b,
177 SV** svs, int svcount)
178 {
179 ms_elt_t *elt;
180 23971 U32 *major_b_end = major_b + svcount;
181  
182 11366245 ▶ while ( major_b < major_b_end ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( major_b < major_b_end ) { 11342274 23971 11366245
183 11342274 elt = by_bucket_cursor[*major_b++]++;
184 11342274 elt->mse_key = *minor_b++;
185 11342274 elt->mse_sv = *svs++;
186 }
187 23971 }
188  
189 /*
190 =item sb__max_count_by_bucket(B<major_bucket_count>, B<count_by_bucket>)
191  
192 Finds the maximum value in the B<count_by_bucket> vector.
193  
194 =cut
195 */
196  
197 static int
198 23971 sb__max_count_by_bucket(int major_bucket_count, int *count_by_bucket)
199 {
200 23971 int i, max_count_by_bucket = 0;
201  
202 45948941 ▶ for ( i=0 ; i<major_bucket_count ; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i=0 ; i<major_bucket_count ; i++ ) { 45924970 23971 45948941
203 45924970 ▶ if (count_by_bucket[i] > max_count_by_bucket) {
Branch: 1 branch
BranchTrueFalseTotal
if (count_by_bucket[i] > max_count_by_bucket) { 24164 45900806 45924970
204 24164 max_count_by_bucket = count_by_bucket[i];
205 }
206 }
207  
208 23971 return max_count_by_bucket;
209 }
210  
211 /*
212 =item sb__init_by_bucket_cursor(B<by_bucket>, B<by_bucket_cursor>, B<major_bucket_count>, B<count_by_bucket>)
213  
214 Initialises the B<by_bucket_cursor> vector of pointers into B<by_bucket>, so
215 that each entry in B<by_bucket_cursor> points to the start of the section of
216 B<by_bucket> that corresponds to that major bucket value.
217  
218 =cut
219 */
220  
221 static void
222 23971 sb__init_by_bucket_cursor(ms_elt_t *by_bucket, ms_elt_t** by_bucket_cursor,
223 int major_bucket_count, int *count_by_bucket)
224 {
225 45948941 ▶ while (major_bucket_count--) {
Branch: 1 branch
BranchTrueFalseTotal
while (major_bucket_count--) { 45924970 23971 45948941
226 45924970 *by_bucket_cursor++ = by_bucket;
227 45924970 by_bucket += *count_by_bucket++;
228 }
229 23971 }
230  
231 /*
232 =item sb__sort_within_major_buckets(B<by_bucket_cursor>, B<major_bucket_count>, B<count_by_bucket>, B<dest>)
233  
234 Sorts the elements within each major bucket, initially in minor bucket order
235 and then falling back to sortsv() for runs that compare equal in minor bucket.
236  
237 Copies the sorted SV* values out into a destination vector at the same time.
238  
239 =cut
240 */
241  
242 static void
243 23971 sb__sort_within_major_buckets(
244 ms_elt_t **by_bucket_cursor, int major_bucket_count,
245 int* count_by_bucket, SV** dest)
246 {
247 int b, i;
248  
249 45948941 ▶ for ( b=0 ; b<major_bucket_count ; b++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( b=0 ; b<major_bucket_count ; b++ ) { 45924970 23971 45948941
250 45924970 ▶ if (count_by_bucket[b]) {
Branch: 1 branch
BranchTrueFalseTotal
if (count_by_bucket[b]) { 1194577 44730393 45924970
251 1194577 ▶ if (count_by_bucket[b] > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count_by_bucket[b] > 1) { 424239 770338 1194577
252 424239 ms_elt_t *bvals = by_bucket_cursor[b];
253 424239 int blen = count_by_bucket[b];
254 424239 int in_run =0, runstart =0;
255 U32 prev;
256  
257 424239 ms_do_mergesort(bvals, blen);
258 424239 bvals -= blen-1;
259  
260 /* Fall back to Perl's sort for runs that compared equal by
261 * both major and minor bucket. */
262 424239 prev = bvals[0].mse_key;
263 424239 *dest++ = bvals[0].mse_sv;
264 10571936 ▶ for ( i=1 ; i<blen ; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i=1 ; i<blen ; i++ ) { 10147697 424239 10571936
265 10147697 *dest++ = bvals[i].mse_sv;
266 10147697 ▶ if (in_run) {
Branch: 1 branch
BranchTrueFalseTotal
if (in_run) { 957454 9190243 10147697
267 957454 ▶ if (bvals[i].mse_key != prev) {
Branch: 1 branch
BranchTrueFalseTotal
if (bvals[i].mse_key != prev) { 458948 498506 957454
268 /* End of the run, sort it in dest */
269 458948 sortsv((dest-1)-(i-runstart), i-runstart, Perl_sv_cmp);
270 458948 in_run = 0;
271 458948 prev = bvals[i].mse_key;
272 }
273 } else {
274 9190243 ▶ if (bvals[i].mse_key == prev) {
Branch: 1 branch
BranchTrueFalseTotal
if (bvals[i].mse_key == prev) { 462757 8727486 9190243
275 /* The start of a new run */
276 462757 in_run = 1;
277 462757 runstart = i-1;
278 } else {
279 8727486 prev = bvals[i].mse_key;
280 }
281 }
282 }
283 424239 ▶ if (in_run) {
Branch: 1 branch
BranchTrueFalseTotal
if (in_run) { 3809 420430 424239
284 /* This bucket ends on a run. */
285 3809 sortsv(dest-(i-runstart), i-runstart, Perl_sv_cmp);
286 }
287 } else {
288 770338 *dest++ = by_bucket_cursor[b][0].mse_sv;
289 }
290 }
291 }
292 23971 }
293  
294 /*
295 =back
296  
297 =head1 AUTHOR
298  
299 Nick Cleaton, E<lt>nick@cleaton.netE<gt>
300  
301 =head1 COPYRIGHT AND LICENSE
302  
303 Copyright (C) 2010 by Nick Cleaton
304  
305 This library is free software; you can redistribute it and/or modify
306 it under the same terms as Perl itself, either Perl version 5.10.0 or,
307 at your option, any later version of Perl 5 you may have available.
308  
309 =cut
310 */
« src/mergesort_algo.c ↑ Summary