1*5b49b507Sjoerg /* $NetBSD: merge.c,v 1.16 2018/05/23 21:21:27 joerg Exp $ */
26dda330eSthorpej
32c1d5008Smycroft /*-
42c1d5008Smycroft * Copyright (c) 1992, 1993
52c1d5008Smycroft * The Regents of the University of California. All rights reserved.
62c1d5008Smycroft *
72c1d5008Smycroft * This code is derived from software contributed to Berkeley by
82c1d5008Smycroft * Peter McIlroy.
92c1d5008Smycroft *
102c1d5008Smycroft * Redistribution and use in source and binary forms, with or without
112c1d5008Smycroft * modification, are permitted provided that the following conditions
122c1d5008Smycroft * are met:
132c1d5008Smycroft * 1. Redistributions of source code must retain the above copyright
142c1d5008Smycroft * notice, this list of conditions and the following disclaimer.
152c1d5008Smycroft * 2. Redistributions in binary form must reproduce the above copyright
162c1d5008Smycroft * notice, this list of conditions and the following disclaimer in the
172c1d5008Smycroft * documentation and/or other materials provided with the distribution.
18eb7c1594Sagc * 3. Neither the name of the University nor the names of its contributors
192c1d5008Smycroft * may be used to endorse or promote products derived from this software
202c1d5008Smycroft * without specific prior written permission.
212c1d5008Smycroft *
222c1d5008Smycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232c1d5008Smycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242c1d5008Smycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252c1d5008Smycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262c1d5008Smycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272c1d5008Smycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282c1d5008Smycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292c1d5008Smycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302c1d5008Smycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312c1d5008Smycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322c1d5008Smycroft * SUCH DAMAGE.
332c1d5008Smycroft */
342c1d5008Smycroft
35bd906777Schristos #include <sys/cdefs.h>
362c1d5008Smycroft #if defined(LIBC_SCCS) && !defined(lint)
376dda330eSthorpej #if 0
386dda330eSthorpej static char sccsid[] = "from: @(#)merge.c 8.2 (Berkeley) 2/14/94";
396dda330eSthorpej #else
40*5b49b507Sjoerg __RCSID("$NetBSD: merge.c,v 1.16 2018/05/23 21:21:27 joerg Exp $");
416dda330eSthorpej #endif
422c1d5008Smycroft #endif /* LIBC_SCCS and not lint */
432c1d5008Smycroft
442c1d5008Smycroft /*
452c1d5008Smycroft * Hybrid exponential search/linear search merge sort with hybrid
462c1d5008Smycroft * natural/pairwise first pass. Requires about .3% more comparisons
472c1d5008Smycroft * for random data than LSMS with pairwise first pass alone.
482c1d5008Smycroft * It works for objects as small as two bytes.
492c1d5008Smycroft */
502c1d5008Smycroft
512c1d5008Smycroft #define NATURAL
522c1d5008Smycroft #define THRESHOLD 16 /* Best choice for natural merge cut-off. */
532c1d5008Smycroft
542c1d5008Smycroft /* #define NATURAL to get hybrid natural merge.
552c1d5008Smycroft * (The default is pairwise merging.)
562c1d5008Smycroft */
572c1d5008Smycroft
5843fa6fe3Sjtc #include "namespace.h"
592c1d5008Smycroft #include <sys/types.h>
602c1d5008Smycroft
61b48252f3Slukem #include <assert.h>
622c1d5008Smycroft #include <errno.h>
632c1d5008Smycroft #include <stdlib.h>
642c1d5008Smycroft #include <string.h>
652c1d5008Smycroft
6643fa6fe3Sjtc #ifdef __weak_alias
6760549036Smycroft __weak_alias(mergesort,_mergesort)
6843fa6fe3Sjtc #endif
6943fa6fe3Sjtc
7067d513ddSdsl static void setup(u_char *, u_char *, size_t, size_t,
7167d513ddSdsl int (*)(const void *, const void *));
7267d513ddSdsl static void insertionsort(u_char *, size_t, size_t,
7367d513ddSdsl int (*)(const void *, const void *));
742c1d5008Smycroft
752c1d5008Smycroft #define ISIZE sizeof(int)
762c1d5008Smycroft #define PSIZE sizeof(u_char *)
772c1d5008Smycroft #define ICOPY_LIST(src, dst, last) \
782c1d5008Smycroft do \
7933edc15aSchristos *(int*)(void *)dst = *(int*)(void *)src, \
8033edc15aSchristos src += ISIZE, dst += ISIZE; \
812c1d5008Smycroft while(src < last)
822c1d5008Smycroft #define ICOPY_ELT(src, dst, i) \
832c1d5008Smycroft do \
8433edc15aSchristos *(int*)(void *)dst = *(int*)(void *)src, \
8533edc15aSchristos src += ISIZE, dst += ISIZE; \
862c1d5008Smycroft while (i -= ISIZE)
872c1d5008Smycroft
882c1d5008Smycroft #define CCOPY_LIST(src, dst, last) \
892c1d5008Smycroft do \
902c1d5008Smycroft *dst++ = *src++; \
912c1d5008Smycroft while (src < last)
922c1d5008Smycroft #define CCOPY_ELT(src, dst, i) \
932c1d5008Smycroft do \
942c1d5008Smycroft *dst++ = *src++; \
952c1d5008Smycroft while (i -= 1)
962c1d5008Smycroft
972c1d5008Smycroft /*
982c1d5008Smycroft * Find the next possible pointer head. (Trickery for forcing an array
992c1d5008Smycroft * to do double duty as a linked list when objects do not align with word
1002c1d5008Smycroft * boundaries.
1012c1d5008Smycroft */
1022c1d5008Smycroft /* Assumption: PSIZE is a power of 2. */
10333edc15aSchristos #define EVAL(p) ((u_char **)(void *) \
104*5b49b507Sjoerg (((u_char *)(void *)(p) + PSIZE - 1 - (u_char *) 0) & ~(PSIZE - 1)))
1052c1d5008Smycroft
1062c1d5008Smycroft /*
1072c1d5008Smycroft * Arguments are as for qsort.
1082c1d5008Smycroft */
1092c1d5008Smycroft int
mergesort(void * base,size_t nmemb,size_t size,int (* cmp)(const void *,const void *))110c5e820caSchristos mergesort(void *base, size_t nmemb, size_t size,
111c5e820caSchristos int (*cmp)(const void *, const void *))
1122c1d5008Smycroft {
11319b04688Slukem size_t i;
11419b04688Slukem int sense;
1152c1d5008Smycroft int big, iflag;
116c8bafd62Sperry u_char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2;
1172c1d5008Smycroft u_char *list2, *list1, *p2, *p, *last, **p1;
1182c1d5008Smycroft
119b48252f3Slukem _DIAGASSERT(base != NULL);
120b48252f3Slukem _DIAGASSERT(cmp != NULL);
121b48252f3Slukem
1222c1d5008Smycroft if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */
1232c1d5008Smycroft errno = EINVAL;
1242c1d5008Smycroft return (-1);
1252c1d5008Smycroft }
1262c1d5008Smycroft
127bc294828Sginsbach if (nmemb == 0)
128bc294828Sginsbach return (0);
129bc294828Sginsbach
1302c1d5008Smycroft /*
1312c1d5008Smycroft * XXX
1322c1d5008Smycroft * Stupid subtraction for the Cray.
1332c1d5008Smycroft */
1342c1d5008Smycroft iflag = 0;
1352c1d5008Smycroft if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE))
1362c1d5008Smycroft iflag = 1;
1372c1d5008Smycroft
1382c1d5008Smycroft if ((list2 = malloc(nmemb * size + PSIZE)) == NULL)
1392c1d5008Smycroft return (-1);
1402c1d5008Smycroft
1412c1d5008Smycroft list1 = base;
1422c1d5008Smycroft setup(list1, list2, nmemb, size, cmp);
1432c1d5008Smycroft last = list2 + nmemb * size;
1442c1d5008Smycroft i = big = 0;
1452c1d5008Smycroft while (*EVAL(list2) != last) {
1462c1d5008Smycroft l2 = list1;
1472c1d5008Smycroft p1 = EVAL(list1);
1482c1d5008Smycroft for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) {
1492c1d5008Smycroft p2 = *EVAL(p2);
1502c1d5008Smycroft f1 = l2;
1512c1d5008Smycroft f2 = l1 = list1 + (p2 - list2);
1522c1d5008Smycroft if (p2 != last)
1532c1d5008Smycroft p2 = *EVAL(p2);
1542c1d5008Smycroft l2 = list1 + (p2 - list2);
1552c1d5008Smycroft while (f1 < l1 && f2 < l2) {
1562c1d5008Smycroft if ((*cmp)(f1, f2) <= 0) {
1572c1d5008Smycroft q = f2;
1582c1d5008Smycroft b = f1, t = l1;
1592c1d5008Smycroft sense = -1;
1602c1d5008Smycroft } else {
1612c1d5008Smycroft q = f1;
1622c1d5008Smycroft b = f2, t = l2;
1632c1d5008Smycroft sense = 0;
1642c1d5008Smycroft }
1652c1d5008Smycroft if (!big) { /* here i = 0 */
166bd906777Schristos #if 0
167bd906777Schristos LINEAR:
168bd906777Schristos #endif
169bd906777Schristos while ((b += size) < t && cmp(q, b) >sense)
1702c1d5008Smycroft if (++i == 6) {
1712c1d5008Smycroft big = 1;
1722c1d5008Smycroft goto EXPONENTIAL;
1732c1d5008Smycroft }
1742c1d5008Smycroft } else {
1752c1d5008Smycroft EXPONENTIAL: for (i = size; ; i <<= 1)
1762c1d5008Smycroft if ((p = (b + i)) >= t) {
1772c1d5008Smycroft if ((p = t - size) > b &&
1782c1d5008Smycroft (*cmp)(q, p) <= sense)
1792c1d5008Smycroft t = p;
1802c1d5008Smycroft else
1812c1d5008Smycroft b = p;
1822c1d5008Smycroft break;
1832c1d5008Smycroft } else if ((*cmp)(q, p) <= sense) {
1842c1d5008Smycroft t = p;
1852c1d5008Smycroft if (i == size)
1862c1d5008Smycroft big = 0;
1872c1d5008Smycroft goto FASTCASE;
1882c1d5008Smycroft } else
1892c1d5008Smycroft b = p;
190bd906777Schristos #if 0
191bd906777Schristos SLOWCASE:
192bd906777Schristos #endif
193bd906777Schristos while (t > b+size) {
1942c1d5008Smycroft i = (((t - b) / size) >> 1) * size;
1952c1d5008Smycroft if ((*cmp)(q, p = b + i) <= sense)
1962c1d5008Smycroft t = p;
1972c1d5008Smycroft else
1982c1d5008Smycroft b = p;
1992c1d5008Smycroft }
2002c1d5008Smycroft goto COPY;
2012c1d5008Smycroft FASTCASE: while (i > size)
2022c1d5008Smycroft if ((*cmp)(q,
20333edc15aSchristos p = b +
20433edc15aSchristos (i = (unsigned int) i >> 1)) <= sense)
2052c1d5008Smycroft t = p;
2062c1d5008Smycroft else
2072c1d5008Smycroft b = p;
2082c1d5008Smycroft COPY: b = t;
2092c1d5008Smycroft }
2102c1d5008Smycroft i = size;
2112c1d5008Smycroft if (q == f1) {
2122c1d5008Smycroft if (iflag) {
2132c1d5008Smycroft ICOPY_LIST(f2, tp2, b);
2142c1d5008Smycroft ICOPY_ELT(f1, tp2, i);
2152c1d5008Smycroft } else {
2162c1d5008Smycroft CCOPY_LIST(f2, tp2, b);
2172c1d5008Smycroft CCOPY_ELT(f1, tp2, i);
2182c1d5008Smycroft }
2192c1d5008Smycroft } else {
2202c1d5008Smycroft if (iflag) {
2212c1d5008Smycroft ICOPY_LIST(f1, tp2, b);
2222c1d5008Smycroft ICOPY_ELT(f2, tp2, i);
2232c1d5008Smycroft } else {
2242c1d5008Smycroft CCOPY_LIST(f1, tp2, b);
2252c1d5008Smycroft CCOPY_ELT(f2, tp2, i);
2262c1d5008Smycroft }
2272c1d5008Smycroft }
2282c1d5008Smycroft }
2292c1d5008Smycroft if (f2 < l2) {
2302c1d5008Smycroft if (iflag)
2312c1d5008Smycroft ICOPY_LIST(f2, tp2, l2);
2322c1d5008Smycroft else
2332c1d5008Smycroft CCOPY_LIST(f2, tp2, l2);
2342c1d5008Smycroft } else if (f1 < l1) {
2352c1d5008Smycroft if (iflag)
2362c1d5008Smycroft ICOPY_LIST(f1, tp2, l1);
2372c1d5008Smycroft else
2382c1d5008Smycroft CCOPY_LIST(f1, tp2, l1);
2392c1d5008Smycroft }
2402c1d5008Smycroft *p1 = l2;
2412c1d5008Smycroft }
2422c1d5008Smycroft tp2 = list1; /* swap list1, list2 */
2432c1d5008Smycroft list1 = list2;
2442c1d5008Smycroft list2 = tp2;
2452c1d5008Smycroft last = list2 + nmemb*size;
2462c1d5008Smycroft }
2472c1d5008Smycroft if (base == list2) {
2482c1d5008Smycroft memmove(list2, list1, nmemb*size);
2492c1d5008Smycroft list2 = list1;
2502c1d5008Smycroft }
2512c1d5008Smycroft free(list2);
2522c1d5008Smycroft return (0);
2532c1d5008Smycroft }
2542c1d5008Smycroft
2552c1d5008Smycroft #define swap(a, b) { \
2562c1d5008Smycroft s = b; \
2572c1d5008Smycroft i = size; \
2582c1d5008Smycroft do { \
2592c1d5008Smycroft tmp = *a; *a++ = *s; *s++ = tmp; \
2602c1d5008Smycroft } while (--i); \
2612c1d5008Smycroft a -= size; \
2622c1d5008Smycroft }
2632c1d5008Smycroft #define reverse(bot, top) { \
2642c1d5008Smycroft s = top; \
2652c1d5008Smycroft do { \
2662c1d5008Smycroft i = size; \
2672c1d5008Smycroft do { \
2682c1d5008Smycroft tmp = *bot; *bot++ = *s; *s++ = tmp; \
2692c1d5008Smycroft } while (--i); \
2702c1d5008Smycroft s -= size2; \
2712c1d5008Smycroft } while(bot < s); \
2722c1d5008Smycroft }
2732c1d5008Smycroft
2742c1d5008Smycroft /*
2752c1d5008Smycroft * Optional hybrid natural/pairwise first pass. Eats up list1 in runs of
2762c1d5008Smycroft * increasing order, list2 in a corresponding linked list. Checks for runs
2772c1d5008Smycroft * when THRESHOLD/2 pairs compare with same sense. (Only used when NATURAL
2782c1d5008Smycroft * is defined. Otherwise simple pairwise merging is used.)
2792c1d5008Smycroft */
280b48252f3Slukem
281b48252f3Slukem /* XXX: shouldn't this function be static? - lukem 990810 */
2822c1d5008Smycroft void
setup(u_char * list1,u_char * list2,size_t n,size_t size,int (* cmp)(const void *,const void *))283c5e820caSchristos setup(u_char *list1, u_char *list2, size_t n, size_t size,
284c5e820caSchristos int (*cmp)(const void *, const void *))
2852c1d5008Smycroft {
286c5e820caSchristos int length, tmp, sense;
2872c1d5008Smycroft u_char *f1, *f2, *s, *l2, *last, *p2;
288c5e820caSchristos size_t size2, i;
2892c1d5008Smycroft
290b48252f3Slukem _DIAGASSERT(cmp != NULL);
291b48252f3Slukem _DIAGASSERT(list1 != NULL);
292b48252f3Slukem _DIAGASSERT(list2 != NULL);
293b48252f3Slukem
2942c1d5008Smycroft size2 = size*2;
2952c1d5008Smycroft if (n <= 5) {
2962c1d5008Smycroft insertionsort(list1, n, size, cmp);
29733edc15aSchristos *EVAL(list2) = list2 + n*size;
2982c1d5008Smycroft return;
2992c1d5008Smycroft }
3002c1d5008Smycroft /*
3012c1d5008Smycroft * Avoid running pointers out of bounds; limit n to evens
3022c1d5008Smycroft * for simplicity.
3032c1d5008Smycroft */
3042c1d5008Smycroft i = 4 + (n & 1);
305c5e820caSchristos insertionsort(list1 + (n - i) * size, i, size, cmp);
3062c1d5008Smycroft last = list1 + size * (n - i);
3072c1d5008Smycroft *EVAL(list2 + (last - list1)) = list2 + n * size;
3082c1d5008Smycroft
3092c1d5008Smycroft #ifdef NATURAL
3102c1d5008Smycroft p2 = list2;
3112c1d5008Smycroft f1 = list1;
3122c1d5008Smycroft sense = (cmp(f1, f1 + size) > 0);
3132c1d5008Smycroft for (; f1 < last; sense = !sense) {
3142c1d5008Smycroft length = 2;
3152c1d5008Smycroft /* Find pairs with same sense. */
3162c1d5008Smycroft for (f2 = f1 + size2; f2 < last; f2 += size2) {
3172c1d5008Smycroft if ((cmp(f2, f2+ size) > 0) != sense)
3182c1d5008Smycroft break;
3192c1d5008Smycroft length += 2;
3202c1d5008Smycroft }
3212c1d5008Smycroft if (length < THRESHOLD) { /* Pairwise merge */
3222c1d5008Smycroft do {
3232c1d5008Smycroft p2 = *EVAL(p2) = f1 + size2 - list1 + list2;
3242c1d5008Smycroft if (sense > 0)
3252c1d5008Smycroft swap (f1, f1 + size);
3262c1d5008Smycroft } while ((f1 += size2) < f2);
3272c1d5008Smycroft } else { /* Natural merge */
3282c1d5008Smycroft l2 = f2;
3292c1d5008Smycroft for (f2 = f1 + size2; f2 < l2; f2 += size2) {
3302c1d5008Smycroft if ((cmp(f2-size, f2) > 0) != sense) {
3312c1d5008Smycroft p2 = *EVAL(p2) = f2 - list1 + list2;
3322c1d5008Smycroft if (sense > 0)
3332c1d5008Smycroft reverse(f1, f2 - size);
3342c1d5008Smycroft f1 = f2;
3352c1d5008Smycroft }
3362c1d5008Smycroft }
3372c1d5008Smycroft if (sense > 0)
3382c1d5008Smycroft reverse(f1, f2 - size);
3392c1d5008Smycroft f1 = f2;
3402c1d5008Smycroft if (f2 < last || cmp(f2 - size, f2) > 0)
3412c1d5008Smycroft p2 = *EVAL(p2) = f2 - list1 + list2;
3422c1d5008Smycroft else
3432c1d5008Smycroft p2 = *EVAL(p2) = list2 + n*size;
3442c1d5008Smycroft }
3452c1d5008Smycroft }
3462c1d5008Smycroft #else /* pairwise merge only. */
3472c1d5008Smycroft for (f1 = list1, p2 = list2; f1 < last; f1 += size2) {
3482c1d5008Smycroft p2 = *EVAL(p2) = p2 + size2;
3492c1d5008Smycroft if (cmp (f1, f1 + size) > 0)
3502c1d5008Smycroft swap(f1, f1 + size);
3512c1d5008Smycroft }
3522c1d5008Smycroft #endif /* NATURAL */
3532c1d5008Smycroft }
3542c1d5008Smycroft
3552c1d5008Smycroft /*
3562c1d5008Smycroft * This is to avoid out-of-bounds addresses in sorting the
3572c1d5008Smycroft * last 4 elements.
3582c1d5008Smycroft */
3592c1d5008Smycroft static void
insertionsort(u_char * a,size_t n,size_t size,int (* cmp)(const void *,const void *))360c5e820caSchristos insertionsort(u_char *a, size_t n, size_t size,
361c5e820caSchristos int (*cmp)(const void *, const void *))
3622c1d5008Smycroft {
3632c1d5008Smycroft u_char *ai, *s, *t, *u, tmp;
364c5e820caSchristos size_t i;
3652c1d5008Smycroft
366b48252f3Slukem _DIAGASSERT(a != NULL);
367b48252f3Slukem _DIAGASSERT(cmp != NULL);
368b48252f3Slukem
3692c1d5008Smycroft for (ai = a+size; --n >= 1; ai += size)
3702c1d5008Smycroft for (t = ai; t > a; t -= size) {
3712c1d5008Smycroft u = t - size;
3722c1d5008Smycroft if (cmp(u, t) <= 0)
3732c1d5008Smycroft break;
3742c1d5008Smycroft swap(u, t);
3752c1d5008Smycroft }
3762c1d5008Smycroft }
377