__ goes away
[unix-history] / usr / src / lib / libc / stdlib / merge.c
CommitLineData
ad59b6a4
KB
1/*-
2 * Copyright (c) 1992 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Peter McIlroy.
7 *
8 * %sccs.include.redist.c%
9 */
10
11#if defined(LIBC_SCCS) && !defined(lint)
6f40f148 12static char sccsid[] = "@(#)merge.c 5.2 (Berkeley) %G%";
ad59b6a4
KB
13#endif /* LIBC_SCCS and not lint */
14
15/*
16 * Hybrid exponential search/linear search merge sort with hybrid
17 * natural/pairwise first pass. Requires about .3% more comparisons
18 * for random data than LSMS with pairwise first pass alone.
19 * It works for objects as small as two bytes.
20 */
21
22#define NATURAL
23#define THRESHOLD 16 /* Best choice for natural merge cut-off. */
24
25/* #define NATURAL to get hybrid natural merge.
26 * (The default is pairwise merging.)
27 */
28
29#include <sys/types.h>
30
31#include <errno.h>
32#include <stdlib.h>
33#include <string.h>
34
35static void setup __P((u_char *, u_char *, size_t, size_t, int (*)()));
36static void insertionsort __P((u_char *, size_t, size_t, int (*)()));
37
38#define ISIZE sizeof(int)
39#define PSIZE sizeof(u_char *)
40#define ICOPY_LIST(src, dst, last) \
41 do \
42 *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \
43 while(src < last)
44#define ICOPY_ELT(src, dst, i) \
45 do \
46 *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \
47 while (i -= ISIZE)
48
49#define CCOPY_LIST(src, dst, last) \
50 do \
51 *dst++ = *src++; \
52 while (src < last)
53#define CCOPY_ELT(src, dst, i) \
54 do \
55 *dst++ = *src++; \
56 while (i -= 1)
57
58/*
59 * Find the next possible pointer head. (Trickery for forcing an array
60 * to do double duty as a linked list when objects do not align with word
61 * boundaries.
62 */
63/* Assumption: PSIZE is a power of 2. */
64#define EVAL(p) (u_char **) \
65 ((u_char *)0 + \
66 (((u_char *)p + PSIZE - 1 - (u_char *) 0) & ~(PSIZE - 1)))
67
68/*
69 * Arguments are as for qsort.
70 */
71int
72mergesort(base, nmemb, size, cmp)
73 void *base;
74 size_t nmemb;
75 register size_t size;
76 int (*cmp) __P((const void *, const void *));
77{
78 register int i, sense;
79 int big, iflag;
80 register u_char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2;
81 u_char *list2, *list1, *p2, *p, *last, **p1;
82
6f40f148 83 if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */
ad59b6a4
KB
84 errno = EINVAL;
85 return (-1);
86 }
87
88 /*
89 * XXX
90 * Stupid subtraction for the Cray.
91 */
92 iflag = 0;
93 if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE))
94 iflag = 1;
95
96 if ((list2 = malloc(nmemb * size + PSIZE)) == NULL)
97 return (-1);
98
99 list1 = base;
100 setup(list1, list2, nmemb, size, cmp);
101 last = list2 + nmemb * size;
102 i = big = 0;
103 while (*EVAL(list2) != last) {
104 l2 = list1;
105 p1 = EVAL(list1);
106 for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) {
107 p2 = *EVAL(p2);
108 f1 = l2;
109 f2 = l1 = list1 + (p2 - list2);
110 if (p2 != last)
111 p2 = *EVAL(p2);
112 l2 = list1 + (p2 - list2);
113 while (f1 < l1 && f2 < l2) {
114 if ((*cmp)(f1, f2) <= 0) {
115 q = f2;
116 b = f1, t = l1;
117 sense = -1;
118 } else {
119 q = f1;
120 b = f2, t = l2;
121 sense = 0;
122 }
123 if (!big) { /* here i = 0 */
124LINEAR: while ((b += size) < t && cmp(q, b) >sense)
125 if (++i == 6) {
126 big = 1;
127 goto EXPONENTIAL;
128 }
129 } else {
130EXPONENTIAL: for (i = size; ; i <<= 1)
131 if ((p = (b + i)) >= t) {
132 if ((p = t - size) > b &&
133 (*cmp)(q, p) <= sense)
134 t = p;
135 else
136 b = p;
137 break;
138 } else if ((*cmp)(q, p) <= sense) {
139 t = p;
140 if (i == size)
141 big = 0;
142 goto FASTCASE;
143 } else
144 b = p;
145SLOWCASE: while (t > b+size) {
146 i = (((t - b) / size) >> 1) * size;
147 if ((*cmp)(q, p = b + i) <= sense)
148 t = p;
149 else
150 b = p;
151 }
152 goto COPY;
153FASTCASE: while (i > size)
154 if ((*cmp)(q,
155 p = b + (i >>= 1)) <= sense)
156 t = p;
157 else
158 b = p;
159COPY: b = t;
160 }
161 i = size;
162 if (q == f1) {
163 if (iflag) {
164 ICOPY_LIST(f2, tp2, b);
165 ICOPY_ELT(f1, tp2, i);
166 } else {
167 CCOPY_LIST(f2, tp2, b);
168 CCOPY_ELT(f1, tp2, i);
169 }
170 } else {
171 if (iflag) {
172 ICOPY_LIST(f1, tp2, b);
173 ICOPY_ELT(f2, tp2, i);
174 } else {
175 CCOPY_LIST(f1, tp2, b);
176 CCOPY_ELT(f2, tp2, i);
177 }
178 }
179 }
180 if (f2 < l2) {
181 if (iflag)
182 ICOPY_LIST(f2, tp2, l2);
183 else
184 CCOPY_LIST(f2, tp2, l2);
185 } else if (f1 < l1) {
186 if (iflag)
187 ICOPY_LIST(f1, tp2, l1);
188 else
189 CCOPY_LIST(f1, tp2, l1);
190 }
191 *p1 = l2;
192 }
193 tp2 = list1; /* swap list1, list2 */
194 list1 = list2;
195 list2 = tp2;
196 last = list2 + nmemb*size;
197 }
198 if (base == list2) {
199 memmove(list2, list1, nmemb*size);
200 list2 = list1;
201 }
202 free(list2);
203 return (0);
204}
205
206#define swap(a, b) { \
207 s = b; \
208 i = size; \
209 do { \
210 tmp = *a; *a++ = *s; *s++ = tmp; \
211 } while (--i); \
212 a -= size; \
213 }
214#define reverse(bot, top) { \
215 s = top; \
216 do { \
217 i = size; \
218 do { \
219 tmp = *bot; *bot++ = *s; *s++ = tmp; \
220 } while (--i); \
221 s -= size2; \
222 } while(bot < s); \
223}
224
225/*
226 * Optional hybrid natural/pairwise first pass. Eats up list1 in runs of
227 * increasing order, list2 in a corresponding linked list. Checks for runs
228 * when THRESHOLD/2 pairs compare with same sense. (Only used when NATURAL
229 * is defined. Otherwise simple pairwise merging is used.)
230 */
231void
232setup(list1, list2, n, size, cmp)
233 size_t n, size;
234 int (*cmp) __P((const void *, const void *));
235 u_char *list1, *list2;
236{
237 int i, length, size2, tmp, sense;
238 u_char *f1, *f2, *s, *l2, *last, *p2;
239
240 size2 = size*2;
241 if (n <= 5) {
242 insertionsort(list1, n, size, cmp);
243 *EVAL(list2) = (u_char*) list2 + n*size;
244 return;
245 }
246 /*
247 * Avoid running pointers out of bounds; limit n to evens
248 * for simplicity.
249 */
250 i = 4 + (n & 1);
251 insertionsort(list1 + (n - i) * size, i, size, cmp);
252 last = list1 + size * (n - i);
253 *EVAL(list2 + (last - list1)) = list2 + n * size;
254
255#ifdef NATURAL
256 p2 = list2;
257 f1 = list1;
258 sense = (cmp(f1, f1 + size) > 0);
259 for (; f1 < last; sense = !sense) {
260 length = 2;
261 /* Find pairs with same sense. */
262 for (f2 = f1 + size2; f2 < last; f2 += size2) {
263 if ((cmp(f2, f2+ size) > 0) != sense)
264 break;
265 length += 2;
266 }
267 if (length < THRESHOLD) { /* Pairwise merge */
268 do {
269 p2 = *EVAL(p2) = f1 + size2 - list1 + list2;
270 if (sense > 0)
271 swap (f1, f1 + size);
272 } while ((f1 += size2) < f2);
273 } else { /* Natural merge */
274 l2 = f2;
275 for (f2 = f1 + size2; f2 < l2; f2 += size2) {
276 if ((cmp(f2-size, f2) > 0) != sense) {
277 p2 = *EVAL(p2) = f2 - list1 + list2;
278 if (sense > 0)
279 reverse(f1, f2-size);
280 f1 = f2;
281 }
282 }
283 if (sense > 0)
284 reverse (f1, f2-size);
285 f1 = f2;
286 if (f2 < last || cmp(f2, f2 + size) > 0)
287 p2 = *EVAL(p2) = f2 - list1 + list2;
288 else
289 p2 = *EVAL(p2) = list2 + n*size;
290 }
291 }
292#else /* pairwise merge only. */
293 for (f1 = list1, p2 = list2; f1 < last; f1 += size2) {
294 p2 = *EVAL(p2) = p2 + size2;
295 if (cmp (f1, f1 + size) > 0)
296 swap(f1, f1 + size);
297 }
298#endif /* NATURAL */
299}
300
301/*
302 * This is to avoid out-of-bounds addresses in sorting the
303 * last 4 elements.
304 */
305static void
306insertionsort(a, n, size, cmp)
307 u_char *a;
308 size_t n, size;
309 int (*cmp) __P((const void *, const void *));
310{
311 u_char *ai, *s, *t, *u, tmp;
312 int i;
313
314 for (ai = a+size; --n >= 1; ai += size)
315 for (t = ai; t > a; t -= size) {
316 u = t - size;
317 if (cmp(u, t) <= 0)
318 break;
319 swap(u, t);
320 }
321}