merge.h

filter: Statementstmt 85.4% 117 / 137 Branchbran 67.7% 84 / 124 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 77.0% 201 / 261 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Sort/Packed.pm ↑ Summary Packed.xs »
1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Peter McIlroy.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32  
33 /*
34 * Hybrid exponential search/linear search merge sort with hybrid
35 * natural/pairwise first pass. Requires about .3% more comparisons
36 * for random data than LSMS with pairwise first pass alone.
37 * It works for objects as small as two bytes.
38 */
39  
40 #define NATURAL
41 #define THRESHOLD 16 /* Best choice for natural merge cut-off. */
42  
43 /* #define NATURAL to get hybrid natural merge.
44 * (The default is pairwise merging.)
45 */
46  
47 typedef int (*my_cmp_t)(pTHX_ const void *, const void *, const void *);
48  
49 static void
50 setup(pTHX_ unsigned char *, unsigned char *, size_t, size_t, my_cmp_t, void *);
51 static void
52 insertionsort(pTHX_ unsigned char *, size_t, size_t, my_cmp_t, void *);
53  
54 #define ISIZE sizeof(int)
55 #define PSIZE sizeof(unsigned char *)
56 #define ICOPY_LIST(src, dst, last) \
57 do \
58 *(int*)(void *)dst = *(int*)(void *)src, \
59 src += ISIZE, dst += ISIZE; \
60 while(src < last)
61 #define ICOPY_ELT(src, dst, i) \
62 do \
63 *(int*)(void *)dst = *(int*)(void *)src, \
64 src += ISIZE, dst += ISIZE; \
65 while (i -= ISIZE)
66  
67 #define CCOPY_LIST(src, dst, last) \
68 do \
69 *dst++ = *src++; \
70 while (src < last)
71 #define CCOPY_ELT(src, dst, i) \
72 do \
73 *dst++ = *src++; \
74 while (i -= 1)
75
76 /*
77 * Find the next possible pointer head. (Trickery for forcing an array
78 * to do double duty as a linked list when objects do not align with word
79 * boundaries.
80 */
81 /* Assumption: PSIZE is a power of 2. */
82 #define EVAL(p) ((unsigned char **)(void *) \
83 ((unsigned char *)0 + \
84 (((unsigned char *)(void *)(p) + PSIZE - 1 - (unsigned char *) 0) & ~(PSIZE - 1))))
85  
86 static void
87 756 my_mergesort(pTHX_
88 void *base, size_t nmemb, size_t size,
89 my_cmp_t cmp,
90 void *cmp_extra) {
91 int i, sense;
92 int big, iflag;
93 unsigned char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2;
94 unsigned char *list2, *list1, *p2, *p, *last, **p1;
95  
96 /* fprintf(stderr, "mergesort base: %p, nmemb: %d, size: %d, cmp: %p, extra: %p\n", */
97 /* base, nmemb, size, cmp, cmp_extra); */
98  
99 756 ▶ if (size < PSIZE / 2)
Branch: 1 branch
BranchTrueFalseTotal
if (size < PSIZE / 2) 0 756 756
100 0 Perl_croak(aTHX_ "internal error: record size %d below minimum %d",
101 size, PSIZE / 2);
102 /*
103 * XXX
104 * Stupid subtraction for the Cray.
105 */
106 756 iflag = 0;
107 756 ▶ if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE))
Branch: 2 branches
BranchTrueFalseTotal
if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE)) 756 0 756
if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE)) 756 0 756
108 756 iflag = 1;
109  
110 756 list2 = (unsigned char *)SvPVX(sv_2mortal(newSV(nmemb * size + PSIZE)));
111  
112 756 list1 = base;
113 756 setup(aTHX_ list1, list2, nmemb, size, cmp, cmp_extra);
114 756 last = list2 + nmemb * size;
115 756 i = big = 0;
116 3192 ▶ while (*EVAL(list2) != last) {
Branch: 1 branch
BranchTrueFalseTotal
while (*EVAL(list2) != last) { 2436 756 3192
117 2436 l2 = list1;
118 2436 p1 = EVAL(list1);
119 28980 ▶ for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) {
Branch: 1 branch
BranchTrueFalseTotal
for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) { 26544 2436 28980
120 26544 p2 = *EVAL(p2);
121 26544 f1 = l2;
122 26544 f2 = l1 = list1 + (p2 - list2);
123 26544 ▶ if (p2 != last)
Branch: 1 branch
BranchTrueFalseTotal
if (p2 != last) 25200 1344 26544
124 25200 p2 = *EVAL(p2);
125 26544 l2 = list1 + (p2 - list2);
126 154235 ▶ while (f1 < l1 && f2 < l2) {
Branch: 2 branches
BranchTrueFalseTotal
while (f1 < l1 && f2 < l2) { 133654 20581 154235
while (f1 < l1 && f2 < l2) { 127691 5963 133654
127 127691 ▶ if ((*cmp)(aTHX_ f1, f2, cmp_extra) <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((*cmp)(aTHX_ f1, f2, cmp_extra) <= 0) { 70185 57506 127691
128 70185 q = f2;
129 70185 b = f1, t = l1;
130 70185 sense = -1;
131 }
132 else {
133 57506 q = f1;
134 57506 b = f2, t = l2;
135 57506 sense = 0;
136 }
137 127691 ▶ if (!big) { /* here i = 0 */
Branch: 1 branch
BranchTrueFalseTotal
if (!big) { /* here i = 0 */ 125112 2579 127691
138 #if 0
139 LINEAR:
140 #endif
141 196067 ▶ while ((b += size) < t && cmp(aTHX_ q, b, cmp_extra) >sense)
Branch: 2 branches
BranchTrueFalseTotal
while ((b += size) < t && cmp(aTHX_ q, b, cmp_extra) >sense) 173901 22166 196067
while ((b += size) < t && cmp(aTHX_ q, b, cmp_extra) >sense) 71813 102088 173901
142 71813 ▶ if (++i == 6) {
Branch: 1 branch
BranchTrueFalseTotal
if (++i == 6) { 858 70955 71813
143 858 big = 1;
144 858 goto EXPONENTIAL;
145 }
146 }
147 else {
148 2579 EXPONENTIAL:
149 3437 for (i = size; ; i <<= 1)
150 8761 ▶ if ((p = (b + i)) >= t) {
Branch: 1 branch
BranchTrueFalseTotal
if ((p = (b + i)) >= t) { 295 8466 8761
151 512 if ((p = t - size) > b &&
152 217 (*cmp)(aTHX_ q, p, cmp_extra) <= sense)
153 78 t = p;
154 else
155 217 b = p;
156 295 break;
157 }
158 8466 ▶ else if ((*cmp)(aTHX_ q, p, cmp_extra) <= sense) {
Branch: 1 branch
BranchTrueFalseTotal
else if ((*cmp)(aTHX_ q, p, cmp_extra) <= sense) { 3142 5324 8466
159 3142 t = p;
160 3142 ▶ if (i == size)
Branch: 1 branch
BranchTrueFalseTotal
if (i == size) 812 2330 3142
161 812 big = 0;
162 3142 goto FASTCASE;
163 }
164 else
165 5324 b = p;
166 #if 0
167 SLOWCASE:
168 #endif
169 444 ▶ while (t > b+size) {
Branch: 1 branch
BranchTrueFalseTotal
while (t > b+size) { 149 295 444
170 149 i = (((t - b) / size) >> 1) * size;
171 149 ▶ if ((*cmp)(aTHX_ q, p = b + i, cmp_extra) <= sense)
Branch: 1 branch
BranchTrueFalseTotal
if ((*cmp)(aTHX_ q, p = b + i, cmp_extra) <= sense) 91 58 149
172 91 t = p;
173 else
174 58 b = p;
175 }
176 295 goto COPY;
177 3142 FASTCASE:
178 7791 ▶ while (i > size)
Branch: 1 branch
BranchTrueFalseTotal
while (i > size) 4649 3142 7791
179 4649 ▶ if ((*cmp)(aTHX_
Branch: 1 branch
BranchTrueFalseTotal
if ((*cmp)(aTHX_ 2548 2101 4649
180 q,
181 4649 p = b + (i = (unsigned int) i >> 1),
182 cmp_extra ) <= sense)
183 2548 t = p;
184 else
185 2101 b = p;
186 3142 COPY:
187 3437 b = t;
188 }
189 127691 i = size;
190 127691 ▶ if (q == f1) {
Branch: 1 branch
BranchTrueFalseTotal
if (q == f1) { 57506 70185 127691
191 57506 ▶ if (iflag) {
Branch: 1 branch
BranchTrueFalseTotal
if (iflag) { 57506 0 57506
192 142327 ▶ ICOPY_LIST(f2, tp2, b);
Branch: 1 branch
BranchTrueFalseTotal
ICOPY_LIST(f2, tp2, b); 84821 57506 142327
193 96402 ▶ ICOPY_ELT(f1, tp2, i);
Branch: 1 branch
BranchTrueFalseTotal
ICOPY_ELT(f1, tp2, i); 38896 57506 96402
194 }
195 else {
196 0 ▶ CCOPY_LIST(f2, tp2, b);
Branch: 1 branch
BranchTrueFalseTotal
CCOPY_LIST(f2, tp2, b); 0 0 0
197 0 ▶ CCOPY_ELT(f1, tp2, i);
Branch: 1 branch
BranchTrueFalseTotal
CCOPY_ELT(f1, tp2, i); 0 0 0
198 }
199 }
200 else {
201 70185 ▶ if (iflag) {
Branch: 1 branch
BranchTrueFalseTotal
if (iflag) { 70185 0 70185
202 211467 ▶ ICOPY_LIST(f1, tp2, b);
Branch: 1 branch
BranchTrueFalseTotal
ICOPY_LIST(f1, tp2, b); 141282 70185 211467
203 117185 ▶ ICOPY_ELT(f2, tp2, i);
Branch: 1 branch
BranchTrueFalseTotal
ICOPY_ELT(f2, tp2, i); 47000 70185 117185
204 }
205 else {
206 0 ▶ CCOPY_LIST(f1, tp2, b);
Branch: 1 branch
BranchTrueFalseTotal
CCOPY_LIST(f1, tp2, b); 0 0 0
207 0 ▶ CCOPY_ELT(f2, tp2, i);
Branch: 1 branch
BranchTrueFalseTotal
CCOPY_ELT(f2, tp2, i); 0 0 0
208 }
209 }
210 }
211 26544 ▶ if (f2 < l2) {
Branch: 1 branch
BranchTrueFalseTotal
if (f2 < l2) { 6235 20309 26544
212 6235 ▶ if (iflag)
Branch: 1 branch
BranchTrueFalseTotal
if (iflag) 6235 0 6235
213 13728 ▶ ICOPY_LIST(f2, tp2, l2);
Branch: 1 branch
BranchTrueFalseTotal
ICOPY_LIST(f2, tp2, l2); 7493 6235 13728
214 else
215 0 ▶ CCOPY_LIST(f2, tp2, l2);
Branch: 1 branch
BranchTrueFalseTotal
CCOPY_LIST(f2, tp2, l2); 0 0 0
216 }
217 20309 ▶ else if (f1 < l1) {
Branch: 1 branch
BranchTrueFalseTotal
else if (f1 < l1) { 5963 14346 20309
218 5963 ▶ if (iflag)
Branch: 1 branch
BranchTrueFalseTotal
if (iflag) 5963 0 5963
219 29403 ▶ ICOPY_LIST(f1, tp2, l1);
Branch: 1 branch
BranchTrueFalseTotal
ICOPY_LIST(f1, tp2, l1); 23440 5963 29403
220 else
221 0 ▶ CCOPY_LIST(f1, tp2, l1);
Branch: 1 branch
BranchTrueFalseTotal
CCOPY_LIST(f1, tp2, l1); 0 0 0
222 }
223 26544 *p1 = l2;
224 }
225 2436 tp2 = list1; /* swap list1, list2 */
226 2436 list1 = list2;
227 2436 list2 = tp2;
228 2436 last = list2 + nmemb*size;
229 }
230 756 ▶ if (base == list2) {
Branch: 1 branch
BranchTrueFalseTotal
if (base == list2) { 84 672 756
231 84 memmove(list2, list1, nmemb*size);
232 84 list2 = list1;
233 }
234 756 }
235  
236 #define swap(a, b) { \
237 s = b; \
238 i = size; \
239 do { \
240 tmp = *a; *a++ = *s; *s++ = tmp; \
241 } while (--i); \
242 a -= size; \
243 }
244 #define reverse(bot, top) { \
245 s = top; \
246 do { \
247 i = size; \
248 do { \
249 tmp = *bot; *bot++ = *s; *s++ = tmp; \
250 } while (--i); \
251 s -= size2; \
252 } while(bot < s); \
253 }
254  
255 /*
256 * Optional hybrid natural/pairwise first pass. Eats up list1 in runs of
257 * increasing order, list2 in a corresponding linked list. Checks for runs
258 * when THRESHOLD/2 pairs compare with same sense. (Only used when NATURAL
259 * is defined. Otherwise simple pairwise merging is used.)
260 */
261  
262 /* XXX: shouldn't this function be static? - lukem 990810 */
263 void
264 756 setup(pTHX_ unsigned char *list1, unsigned char *list2, size_t n, size_t size,
265 my_cmp_t cmp, void *cmp_extra) {
266  
267 int i, length, size2, tmp, sense;
268 unsigned char *f1, *f2, *s, *l2, *last, *p2;
269  
270 756 size2 = size * 2;
271 756 ▶ if (n <= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 5) { 252 504 756
272 252 insertionsort(aTHX_ list1, n, size, cmp, cmp_extra);
273 252 *EVAL(list2) = list2 + n*size;
274 252 return;
275 }
276 /*
277 * Avoid running pointers out of bounds; limit n to evens
278 * for simplicity.
279 */
280 504 i = 4 + (n & 1);
281 504 insertionsort(aTHX_ list1 + (n - i) * size, (size_t)i, size, cmp, cmp_extra);
282 504 last = list1 + size * (n - i);
283 504 *EVAL(list2 + (last - list1)) = list2 + n * size;
284  
285 #ifdef NATURAL
286 504 p2 = list2;
287 504 f1 = list1;
288 504 sense = (cmp(aTHX_ f1, f1 + size, cmp_extra) > 0);
289 13628 ▶ for (; f1 < last; sense = !sense) {
Branch: 1 branch
BranchTrueFalseTotal
for (; f1 < last; sense = !sense) { 13124 504 13628
290 13124 length = 2;
291 /* Find pairs with same sense. */
292 25200 ▶ for (f2 = f1 + size2; f2 < last; f2 += size2) {
Branch: 1 branch
BranchTrueFalseTotal
for (f2 = f1 + size2; f2 < last; f2 += size2) { 24696 504 25200
293 24696 ▶ if ((cmp(aTHX_ f2, f2+ size, cmp_extra) > 0) != sense)
Branch: 1 branch
BranchTrueFalseTotal
if ((cmp(aTHX_ f2, f2+ size, cmp_extra) > 0) != sense) 12620 12076 24696
294 12620 break;
295 12076 length += 2;
296 }
297 13124 ▶ if (length < THRESHOLD) { /* Pairwise merge */
Branch: 1 branch
BranchTrueFalseTotal
if (length < THRESHOLD) { /* Pairwise merge */ 13124 0 13124
298 do {
299 25200 p2 = *EVAL(p2) = f1 + size2 - list1 + list2;
300 25200 ▶ if (sense > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (sense > 0) 11937 13263 25200
301 79372 ▶ swap (f1, f1 + size);
Branch: 1 branch
BranchTrueFalseTotal
swap (f1, f1 + size); 67435 11937 79372
302 25200 ▶ } while ((f1 += size2) < f2);
Branch: 1 branch
BranchTrueFalseTotal
} while ((f1 += size2) < f2); 12076 13124 25200
303 }
304 else { /* Natural merge */
305 0 l2 = f2;
306 0 ▶ for (f2 = f1 + size2; f2 < l2; f2 += size2) {
Branch: 1 branch
BranchTrueFalseTotal
for (f2 = f1 + size2; f2 < l2; f2 += size2) { 0 0 0
307 0 ▶ if ((cmp(aTHX_ f2-size, f2, cmp_extra) > 0) != sense) {
Branch: 1 branch
BranchTrueFalseTotal
if ((cmp(aTHX_ f2-size, f2, cmp_extra) > 0) != sense) { 0 0 0
308 0 p2 = *EVAL(p2) = f2 - list1 + list2;
309 0 ▶ if (sense > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (sense > 0) 0 0 0
310 0 ▶ reverse(f1, f2-size);
Branch: 2 branches
BranchTrueFalseTotal
reverse(f1, f2-size); 0 0 0
reverse(f1, f2-size); 0 0 0
311 0 f1 = f2;
312 }
313 }
314 0 ▶ if (sense > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (sense > 0) 0 0 0
315 0 ▶ reverse (f1, f2-size);
Branch: 2 branches
BranchTrueFalseTotal
reverse (f1, f2-size); 0 0 0
reverse (f1, f2-size); 0 0 0
316 0 f1 = f2;
317 0 ▶ if (f2 < last || cmp(aTHX_ f2 - size, f2, cmp_extra) > 0)
Branch: 2 branches
BranchTrueFalseTotal
if (f2 < last || cmp(aTHX_ f2 - size, f2, cmp_extra) > 0) 0 0 0
if (f2 < last || cmp(aTHX_ f2 - size, f2, cmp_extra) > 0) 0 0 0
318 0 p2 = *EVAL(p2) = f2 - list1 + list2;
319 else
320 0 p2 = *EVAL(p2) = list2 + n*size;
321 }
322 }
323 #else /* pairwise merge only. */
324 for (f1 = list1, p2 = list2; f1 < last; f1 += size2) {
325 p2 = *EVAL(p2) = p2 + size2;
326 if (cmp (aTHX_ f1, f1 + size, cmp_extra) > 0)
327 swap(f1, f1 + size);
328 }
329 #endif /* NATURAL */
330 }
331  
332 static void
333 756 insertionsort(pTHX_ unsigned char *a, size_t n, size_t size,
334 my_cmp_t cmp,
335 void *cmp_extra) {
336  
337 unsigned char *ai, *s, *t, *u, tmp;
338 756 int i, n1 = n;
339
340 2856 ▶ for (ai = a+size; --n >= 1; ai += size)
Branch: 1 branch
BranchTrueFalseTotal
for (ai = a+size; --n >= 1; ai += size) 2100 756 2856
341 3844 ▶ for (t = ai; t > a; t -= size) {
Branch: 1 branch
BranchTrueFalseTotal
for (t = ai; t > a; t -= size) { 3156 688 3844
342 3156 u = t - size;
343 3156 ▶ if (cmp(aTHX_ u, t, cmp_extra) <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (cmp(aTHX_ u, t, cmp_extra) <= 0) 1412 1744 3156
344 1412 break;
345 11544 ▶ swap(u, t);
Branch: 1 branch
BranchTrueFalseTotal
swap(u, t); 9800 1744 11544
346 }
347 756 }
« blib/lib/Sort/Packed.pm ↑ Summary Packed.xs »