39 #if defined(LIBC_SCCS) && !defined(lint)
40 static char sccsid[] =
"@(#)merge.c 8.2 (Berkeley) 2/14/94";
59 #define ISIZE sizeof(int)
60 #define PSIZE sizeof(unsigned char *)
61 #define ICOPY_LIST(src, dst, last) \
63 *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \
65 #define ICOPY_ELT(src, dst, i) \
67 *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \
70 #define CCOPY_LIST(src, dst, last) \
74 #define CCOPY_ELT(src, dst, i) \
85 #define EVAL(p) (unsigned char **) \
86 ((unsigned char *)0 + \
87 (((unsigned char *)p + PSIZE - 1 - (unsigned char *) 0) & ~(PSIZE - 1)))
89 #define swap(a, b) { \
93 tmp = *a; *a++ = *s; *s++ = tmp; \
97 #define reverse(bot, top) { \
102 tmp = *bot; *bot++ = *s; *s++ = tmp; \
114 int (*cmp) (
const void *,
const void *))
117 u_char *ai, *s, *t, *u, tmp;
120 for (ai = a+size; --n >= 1; ai += size)
121 for (t = ai; t > a; t -= size) {
136 setup(
unsigned char *list1,
unsigned char *list2,
137 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;
146 *
EVAL(list2) = (
unsigned char*) list2 + n*size;
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) {
165 for (f2 = f1 + size2; f2 < last; f2 += size2) {
166 if ((cmp(f2, f2+ size) > 0) != sense)
172 p2 = *
EVAL(p2) = f1 + size2 - list1 + list2;
174 swap (f1, f1 + size);
175 }
while ((f1 += size2) < f2);
178 for (f2 = f1 + size2; f2 < l2; f2 += size2) {
179 if ((cmp(f2-size, f2) > 0) != sense) {
180 p2 = *
EVAL(p2) = f2 - list1 + list2;
189 if (f2 < last || cmp(f2 - size, f2) > 0)
190 p2 = *
EVAL(p2) = f2 - list1 + list2;
192 p2 = *
EVAL(p2) = list2 + n*size;
196 for (f1 = list1, p2 = list2; f1 < last; f1 += size2) {
197 p2 = *
EVAL(p2) = p2 + size2;
198 if (cmp (f1, f1 + size) > 0)
209 int (*cmp) (
const void *,
const void *))
211 register int i, sense;
213 register unsigned char *f1, *f2, *t, *b, *q, *l1, *l2;
215 register unsigned char *tp2;
217 unsigned char *list2;
219 unsigned char *list1;
220 unsigned char *p2, *p, *last, **p1;
222 if (size <
PSIZE / 2) {
235 if (!(size %
ISIZE) && !(((
char *)base - (
char *)0) % ISIZE))
238 if ((list2 = malloc(nmemb * size +
PSIZE)) == NULL)
242 setup(list1, list2, nmemb, size, cmp);
243 last = list2 + nmemb * size;
246 while (*
EVAL(list2) != last) {
249 for (tp2 = p2 = list2; p2 != last; p1 =
EVAL(l2)) {
252 f2 = l1 = list1 + (p2 - list2);
255 l2 = list1 + (p2 - list2);
256 while (f1 < l1 && f2 < l2) {
257 if ((*cmp)(f1, f2) <= 0) {
267 while ((b += size) < t && cmp(q, b) >sense)
274 EXPONENTIAL:
for (i = size; ; i <<= 1)
275 if ((p = (b + i)) >= t) {
276 if ((p = t - size) > b &&
277 (*cmp)(q, p) <= sense)
282 }
else if ((*cmp)(q, p) <= sense) {
291 i = (((t - b) / size) >> 1) * size;
292 if ((*cmp)(q, p = b + i) <= sense)
298 FASTCASE:
while (i > size)
300 p = b + (i >>= 1)) <= sense)
332 }
else if (f1 < l1) {
345 last = list2 + nmemb*size;
349 memmove(list2, list1, nmemb*size);