3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid[] = "@(#)merge.c 8.2 (Berkeley) 2/14/94";
40 #endif /* LIBC_SCCS and not lint */
43 * Hybrid exponential search/linear search merge sort with hybrid
44 * natural/pairwise first pass. Requires about .3% more comparisons
45 * for random data than LSMS with pairwise first pass alone.
46 * It works for objects as small as two bytes.
50 #define THRESHOLD 16 /* Best choice for natural merge cut-off. */
52 /* #define NATURAL to get hybrid natural merge.
53 * (The default is pairwise merging.)
59 #include "lib/rpmdb_internal.h" /* XXX for mergesort */
61 #define ISIZE sizeof(int)
62 #define PSIZE sizeof(unsigned char *)
63 #define ICOPY_LIST(src, dst, last) \
65 *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \
67 #define ICOPY_ELT(src, dst, i) \
69 *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \
72 #define CCOPY_LIST(src, dst, last) \
76 #define CCOPY_ELT(src, dst, i) \
82 * Find the next possible pointer head. (Trickery for forcing an array
83 * to do double duty as a linked list when objects do not align with word
86 /* Assumption: PSIZE is a power of 2. */
87 #define EVAL(p) (unsigned char **) \
88 ((unsigned char *)0 + \
89 (((unsigned char *)p + PSIZE - 1 - (unsigned char *) 0) & ~(PSIZE - 1)))
91 #define swap(a, b) { \
95 tmp = *a; *a++ = *s; *s++ = tmp; \
99 #define reverse(bot, top) { \
104 tmp = *bot; *bot++ = *s; *s++ = tmp; \
111 * This is to avoid out-of-bounds addresses in sorting the
115 insertionsort(unsigned char *a, size_t n, size_t size,
116 int (*cmp) (const void *, const void *))
118 unsigned char *ai, *s, *t, *u, tmp;
121 for (ai = a+size; --n >= 1; ai += size)
122 for (t = ai; t > a; t -= size) {
131 * Optional hybrid natural/pairwise first pass. Eats up list1 in runs of
132 * increasing order, list2 in a corresponding linked list. Checks for runs
133 * when THRESHOLD/2 pairs compare with same sense. (Only used when NATURAL
134 * is defined. Otherwise simple pairwise merging is used.)
137 setup(unsigned char *list1, unsigned char *list2,
138 size_t n, size_t size, int (*cmp) (const void *, const void *))
140 int i, length, size2, tmp, sense;
141 unsigned char *f1, *f2, *s, *l2, *last, *p2;
145 insertionsort(list1, n, size, cmp);
146 *EVAL(list2) = (unsigned char*) list2 + n*size;
150 * Avoid running pointers out of bounds; limit n to evens
154 insertionsort(list1 + (n - i) * size, i, size, cmp);
155 last = list1 + size * (n - i);
156 *EVAL(list2 + (last - list1)) = list2 + n * size;
161 sense = (cmp(f1, f1 + size) > 0);
162 for (; f1 < last; sense = !sense) {
164 /* Find pairs with same sense. */
165 for (f2 = f1 + size2; f2 < last; f2 += size2) {
166 if ((cmp(f2, f2+ size) > 0) != sense)
170 if (length < THRESHOLD) { /* Pairwise merge */
172 p2 = *EVAL(p2) = f1 + size2 - list1 + list2;
174 swap (f1, f1 + size);
175 } while ((f1 += size2) < f2);
176 } else { /* Natural merge */
178 for (f2 = f1 + size2; f2 < l2; f2 += size2) {
179 if ((cmp(f2-size, f2) > 0) != sense) {
180 p2 = *EVAL(p2) = f2 - list1 + list2;
182 reverse(f1, f2-size);
187 reverse (f1, f2-size);
189 if (f2 < last || cmp(f2 - size, f2) > 0)
190 p2 = *EVAL(p2) = f2 - list1 + list2;
192 p2 = *EVAL(p2) = list2 + n*size;
195 #else /* pairwise merge only. */
196 for (f1 = list1, p2 = list2; f1 < last; f1 += size2) {
197 p2 = *EVAL(p2) = p2 + size2;
198 if (cmp (f1, f1 + size) > 0)
205 * Arguments are as for qsort.
208 mergesort(void *base, size_t nmemb, size_t size,
209 int (*cmp) (const void *, const void *))
211 register int i, sense;
213 register unsigned char *f1, *f2, *t, *b, *q, *l1, *l2;
214 register unsigned char *tp2;
215 unsigned char *list2;
216 unsigned char *list1;
217 unsigned char *p2, *p, *last, **p1;
219 if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */
229 * Stupid subtraction for the Cray.
232 if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE))
235 if ((list2 = malloc(nmemb * size + PSIZE)) == NULL)
239 setup(list1, list2, nmemb, size, cmp);
240 last = list2 + nmemb * size;
242 while (*EVAL(list2) != last) {
245 for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) {
248 f2 = l1 = list1 + (p2 - list2);
251 l2 = list1 + (p2 - list2);
252 while (f1 < l1 && f2 < l2) {
253 if ((*cmp)(f1, f2) <= 0) {
262 if (!big) { /* here i = 0 */
263 while ((b += size) < t && cmp(q, b) >sense)
269 EXPONENTIAL: for (i = size; ; i <<= 1)
270 if ((p = (b + i)) >= t) {
271 if ((p = t - size) > b &&
272 (*cmp)(q, p) <= sense)
277 } else if ((*cmp)(q, p) <= sense) {
285 i = (((t - b) / size) >> 1) * size;
286 if ((*cmp)(q, p = b + i) <= sense)
292 FASTCASE: while (i > size)
294 p = b + (i >>= 1)) <= sense)
303 ICOPY_LIST(f2, tp2, b);
304 ICOPY_ELT(f1, tp2, i);
306 CCOPY_LIST(f2, tp2, b);
307 CCOPY_ELT(f1, tp2, i);
311 ICOPY_LIST(f1, tp2, b);
312 ICOPY_ELT(f2, tp2, i);
314 CCOPY_LIST(f1, tp2, b);
315 CCOPY_ELT(f2, tp2, i);
321 ICOPY_LIST(f2, tp2, l2);
323 CCOPY_LIST(f2, tp2, l2);
324 } else if (f1 < l1) {
326 ICOPY_LIST(f1, tp2, l1);
328 CCOPY_LIST(f1, tp2, l1);
332 tp2 = list1; /* swap list1, list2 */
335 last = list2 + nmemb*size;
338 memmove(list2, list1, nmemb*size);
345 /* mergesort is implemented in System on Mac OS X */
346 #endif /* __APPLE__ */