Home | History | Annotate | Line # | Download | only in stdlib
merge.c revision 1.17
      1  1.17  riastrad /*	$NetBSD: merge.c,v 1.17 2025/03/02 16:35:41 riastradh Exp $	*/
      2   1.3   thorpej 
      3   1.1   mycroft /*-
      4   1.1   mycroft  * Copyright (c) 1992, 1993
      5   1.1   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1   mycroft  *
      7   1.1   mycroft  * This code is derived from software contributed to Berkeley by
      8   1.1   mycroft  * Peter McIlroy.
      9   1.1   mycroft  *
     10   1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     11   1.1   mycroft  * modification, are permitted provided that the following conditions
     12   1.1   mycroft  * are met:
     13   1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     14   1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     15   1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     17   1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     18  1.11       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1   mycroft  *    may be used to endorse or promote products derived from this software
     20   1.1   mycroft  *    without specific prior written permission.
     21   1.1   mycroft  *
     22   1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1   mycroft  * SUCH DAMAGE.
     33   1.1   mycroft  */
     34   1.1   mycroft 
     35   1.4  christos #include <sys/cdefs.h>
     36   1.1   mycroft #if defined(LIBC_SCCS) && !defined(lint)
     37   1.3   thorpej #if 0
     38   1.3   thorpej static char sccsid[] = "from: @(#)merge.c	8.2 (Berkeley) 2/14/94";
     39   1.3   thorpej #else
     40  1.17  riastrad __RCSID("$NetBSD: merge.c,v 1.17 2025/03/02 16:35:41 riastradh Exp $");
     41   1.3   thorpej #endif
     42   1.1   mycroft #endif /* LIBC_SCCS and not lint */
     43   1.1   mycroft 
     44   1.1   mycroft /*
     45   1.1   mycroft  * Hybrid exponential search/linear search merge sort with hybrid
     46   1.1   mycroft  * natural/pairwise first pass.  Requires about .3% more comparisons
     47   1.1   mycroft  * for random data than LSMS with pairwise first pass alone.
     48   1.1   mycroft  * It works for objects as small as two bytes.
     49   1.1   mycroft  */
     50   1.1   mycroft 
     51   1.1   mycroft #define NATURAL
     52   1.1   mycroft #define THRESHOLD 16	/* Best choice for natural merge cut-off. */
     53   1.1   mycroft 
     54   1.1   mycroft /* #define NATURAL to get hybrid natural merge.
     55   1.1   mycroft  * (The default is pairwise merging.)
     56   1.1   mycroft  */
     57   1.1   mycroft 
     58   1.5       jtc #include "namespace.h"
     59   1.1   mycroft #include <sys/types.h>
     60   1.1   mycroft 
     61   1.8     lukem #include <assert.h>
     62   1.1   mycroft #include <errno.h>
     63   1.1   mycroft #include <stdlib.h>
     64   1.1   mycroft #include <string.h>
     65   1.5       jtc 
     66   1.5       jtc #ifdef __weak_alias
     67  1.10   mycroft __weak_alias(mergesort,_mergesort)
     68  1.17  riastrad __weak_alias(mergesort_r,_mergesort_r)
     69   1.5       jtc #endif
     70   1.1   mycroft 
     71  1.13       dsl static void setup(u_char *, u_char *, size_t, size_t,
     72  1.17  riastrad     int (*)(const void *, const void *, void *), void *);
     73  1.13       dsl static void insertionsort(u_char *, size_t, size_t,
     74  1.17  riastrad     int (*)(const void *, const void *, void *), void *);
     75   1.1   mycroft 
     76   1.1   mycroft #define ISIZE sizeof(int)
     77   1.1   mycroft #define PSIZE sizeof(u_char *)
     78   1.1   mycroft #define ICOPY_LIST(src, dst, last)				\
     79   1.1   mycroft 	do							\
     80   1.7  christos 	*(int*)(void *)dst = *(int*)(void *)src,		\
     81   1.7  christos 		src += ISIZE, dst += ISIZE;			\
     82   1.1   mycroft 	while(src < last)
     83   1.1   mycroft #define ICOPY_ELT(src, dst, i)					\
     84   1.1   mycroft 	do							\
     85   1.7  christos 	*(int*)(void *)dst = *(int*)(void *)src,		\
     86   1.7  christos 	src += ISIZE, dst += ISIZE;				\
     87   1.1   mycroft 	while (i -= ISIZE)
     88   1.1   mycroft 
     89   1.1   mycroft #define CCOPY_LIST(src, dst, last)		\
     90   1.1   mycroft 	do					\
     91   1.1   mycroft 		*dst++ = *src++;		\
     92   1.1   mycroft 	while (src < last)
     93   1.1   mycroft #define CCOPY_ELT(src, dst, i)			\
     94   1.1   mycroft 	do					\
     95   1.1   mycroft 		*dst++ = *src++;		\
     96   1.1   mycroft 	while (i -= 1)
     97  1.17  riastrad 
     98   1.1   mycroft /*
     99   1.1   mycroft  * Find the next possible pointer head.  (Trickery for forcing an array
    100   1.1   mycroft  * to do double duty as a linked list when objects do not align with word
    101   1.1   mycroft  * boundaries.
    102   1.1   mycroft  */
    103   1.1   mycroft /* Assumption: PSIZE is a power of 2. */
    104   1.7  christos #define EVAL(p) ((u_char **)(void *)					\
    105  1.16     joerg     (((u_char *)(void *)(p) + PSIZE - 1 - (u_char *) 0) & ~(PSIZE - 1)))
    106   1.1   mycroft 
    107   1.1   mycroft /*
    108   1.1   mycroft  * Arguments are as for qsort.
    109   1.1   mycroft  */
    110   1.1   mycroft int
    111  1.17  riastrad mergesort_r(void *base, size_t nmemb, size_t size,
    112  1.17  riastrad     int (*cmp)(const void *, const void *, void *), void *cookie)
    113   1.1   mycroft {
    114  1.12     lukem 	size_t i;
    115  1.12     lukem 	int sense;
    116   1.1   mycroft 	int big, iflag;
    117   1.6     perry 	u_char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2;
    118   1.1   mycroft 	u_char *list2, *list1, *p2, *p, *last, **p1;
    119   1.1   mycroft 
    120   1.8     lukem 	_DIAGASSERT(base != NULL);
    121   1.8     lukem 	_DIAGASSERT(cmp != NULL);
    122   1.8     lukem 
    123   1.1   mycroft 	if (size < PSIZE / 2) {		/* Pointers must fit into 2 * size. */
    124   1.1   mycroft 		errno = EINVAL;
    125   1.1   mycroft 		return (-1);
    126   1.1   mycroft 	}
    127   1.1   mycroft 
    128  1.15  ginsbach 	if (nmemb == 0)
    129  1.15  ginsbach 		return (0);
    130  1.15  ginsbach 
    131   1.1   mycroft 	/*
    132   1.1   mycroft 	 * XXX
    133   1.1   mycroft 	 * Stupid subtraction for the Cray.
    134   1.1   mycroft 	 */
    135   1.1   mycroft 	iflag = 0;
    136   1.1   mycroft 	if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE))
    137   1.1   mycroft 		iflag = 1;
    138   1.1   mycroft 
    139   1.1   mycroft 	if ((list2 = malloc(nmemb * size + PSIZE)) == NULL)
    140   1.1   mycroft 		return (-1);
    141   1.1   mycroft 
    142   1.1   mycroft 	list1 = base;
    143  1.17  riastrad 	setup(list1, list2, nmemb, size, cmp, cookie);
    144   1.1   mycroft 	last = list2 + nmemb * size;
    145   1.1   mycroft 	i = big = 0;
    146   1.1   mycroft 	while (*EVAL(list2) != last) {
    147   1.1   mycroft 	    l2 = list1;
    148   1.1   mycroft 	    p1 = EVAL(list1);
    149   1.1   mycroft 	    for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) {
    150   1.1   mycroft 	    	p2 = *EVAL(p2);
    151   1.1   mycroft 	    	f1 = l2;
    152   1.1   mycroft 	    	f2 = l1 = list1 + (p2 - list2);
    153   1.1   mycroft 	    	if (p2 != last)
    154   1.1   mycroft 	    		p2 = *EVAL(p2);
    155   1.1   mycroft 	    	l2 = list1 + (p2 - list2);
    156   1.1   mycroft 	    	while (f1 < l1 && f2 < l2) {
    157  1.17  riastrad 	    		if ((*cmp)(f1, f2, cookie) <= 0) {
    158   1.1   mycroft 	    			q = f2;
    159   1.1   mycroft 	    			b = f1, t = l1;
    160   1.1   mycroft 	    			sense = -1;
    161   1.1   mycroft 	    		} else {
    162   1.1   mycroft 	    			q = f1;
    163   1.1   mycroft 	    			b = f2, t = l2;
    164   1.1   mycroft 	    			sense = 0;
    165   1.1   mycroft 	    		}
    166   1.1   mycroft 	    		if (!big) {	/* here i = 0 */
    167   1.4  christos #if 0
    168   1.4  christos LINEAR:
    169   1.4  christos #endif
    170  1.17  riastrad 	    			while ((b += size) < t &&
    171  1.17  riastrad 	    			    cmp(q, b, cookie) >sense)
    172   1.1   mycroft 	    				if (++i == 6) {
    173   1.1   mycroft 	    					big = 1;
    174   1.1   mycroft 	    					goto EXPONENTIAL;
    175   1.1   mycroft 	    				}
    176   1.1   mycroft 	    		} else {
    177   1.1   mycroft EXPONENTIAL:	    		for (i = size; ; i <<= 1)
    178   1.1   mycroft 	    				if ((p = (b + i)) >= t) {
    179   1.1   mycroft 	    					if ((p = t - size) > b &&
    180  1.17  riastrad 	    					    ((*cmp)(q, p, cookie) <=
    181  1.17  riastrad 	    						sense))
    182   1.1   mycroft 	    						t = p;
    183   1.1   mycroft 	    					else
    184   1.1   mycroft 	    						b = p;
    185   1.1   mycroft 	    					break;
    186  1.17  riastrad 	    				} else if ((*cmp)(q, p, cookie) <=
    187  1.17  riastrad 	    				    sense) {
    188   1.1   mycroft 	    					t = p;
    189   1.1   mycroft 	    					if (i == size)
    190  1.17  riastrad 	    						big = 0;
    191   1.1   mycroft 	    					goto FASTCASE;
    192   1.1   mycroft 	    				} else
    193   1.1   mycroft 	    					b = p;
    194   1.4  christos #if 0
    195   1.4  christos SLOWCASE:
    196   1.4  christos #endif
    197   1.4  christos 				while (t > b+size) {
    198   1.1   mycroft 	    				i = (((t - b) / size) >> 1) * size;
    199  1.17  riastrad 	    				if ((*cmp)(q, p = b + i, cookie) <=
    200  1.17  riastrad 	    				    sense)
    201   1.1   mycroft 	    					t = p;
    202   1.1   mycroft 	    				else
    203   1.1   mycroft 	    					b = p;
    204   1.1   mycroft 	    			}
    205   1.1   mycroft 	    			goto COPY;
    206  1.17  riastrad FASTCASE:	    		while (i > size) {
    207  1.17  riastrad 	    				i = (unsigned int)i >> 1;
    208  1.17  riastrad 	    				p = b + i;
    209  1.17  riastrad 	    				if ((*cmp)(q, p, cookie) <= sense)
    210   1.1   mycroft 	    					t = p;
    211   1.1   mycroft 	    				else
    212   1.1   mycroft 	    					b = p;
    213  1.17  riastrad 	    			}
    214   1.1   mycroft COPY:	    			b = t;
    215   1.1   mycroft 	    		}
    216   1.1   mycroft 	    		i = size;
    217   1.1   mycroft 	    		if (q == f1) {
    218   1.1   mycroft 	    			if (iflag) {
    219   1.1   mycroft 	    				ICOPY_LIST(f2, tp2, b);
    220   1.1   mycroft 	    				ICOPY_ELT(f1, tp2, i);
    221   1.1   mycroft 	    			} else {
    222   1.1   mycroft 	    				CCOPY_LIST(f2, tp2, b);
    223   1.1   mycroft 	    				CCOPY_ELT(f1, tp2, i);
    224   1.1   mycroft 	    			}
    225   1.1   mycroft 	    		} else {
    226   1.1   mycroft 	    			if (iflag) {
    227   1.1   mycroft 	    				ICOPY_LIST(f1, tp2, b);
    228   1.1   mycroft 	    				ICOPY_ELT(f2, tp2, i);
    229   1.1   mycroft 	    			} else {
    230   1.1   mycroft 	    				CCOPY_LIST(f1, tp2, b);
    231   1.1   mycroft 	    				CCOPY_ELT(f2, tp2, i);
    232   1.1   mycroft 	    			}
    233   1.1   mycroft 	    		}
    234   1.1   mycroft 	    	}
    235   1.1   mycroft 	    	if (f2 < l2) {
    236   1.1   mycroft 	    		if (iflag)
    237   1.1   mycroft 	    			ICOPY_LIST(f2, tp2, l2);
    238   1.1   mycroft 	    		else
    239   1.1   mycroft 	    			CCOPY_LIST(f2, tp2, l2);
    240   1.1   mycroft 	    	} else if (f1 < l1) {
    241   1.1   mycroft 	    		if (iflag)
    242   1.1   mycroft 	    			ICOPY_LIST(f1, tp2, l1);
    243   1.1   mycroft 	    		else
    244   1.1   mycroft 	    			CCOPY_LIST(f1, tp2, l1);
    245   1.1   mycroft 	    	}
    246   1.1   mycroft 	    	*p1 = l2;
    247   1.1   mycroft 	    }
    248   1.1   mycroft 	    tp2 = list1;	/* swap list1, list2 */
    249   1.1   mycroft 	    list1 = list2;
    250   1.1   mycroft 	    list2 = tp2;
    251   1.1   mycroft 	    last = list2 + nmemb*size;
    252   1.1   mycroft 	}
    253   1.1   mycroft 	if (base == list2) {
    254   1.1   mycroft 		memmove(list2, list1, nmemb*size);
    255   1.1   mycroft 		list2 = list1;
    256   1.1   mycroft 	}
    257   1.1   mycroft 	free(list2);
    258   1.1   mycroft 	return (0);
    259   1.1   mycroft }
    260   1.1   mycroft 
    261   1.1   mycroft #define	swap(a, b) {					\
    262   1.1   mycroft 		s = b;					\
    263   1.1   mycroft 		i = size;				\
    264   1.1   mycroft 		do {					\
    265   1.1   mycroft 			tmp = *a; *a++ = *s; *s++ = tmp; \
    266   1.1   mycroft 		} while (--i);				\
    267   1.1   mycroft 		a -= size;				\
    268   1.1   mycroft 	}
    269   1.1   mycroft #define reverse(bot, top) {				\
    270   1.1   mycroft 	s = top;					\
    271   1.1   mycroft 	do {						\
    272   1.1   mycroft 		i = size;				\
    273   1.1   mycroft 		do {					\
    274   1.1   mycroft 			tmp = *bot; *bot++ = *s; *s++ = tmp; \
    275   1.1   mycroft 		} while (--i);				\
    276   1.1   mycroft 		s -= size2;				\
    277   1.1   mycroft 	} while(bot < s);				\
    278   1.1   mycroft }
    279   1.1   mycroft 
    280   1.1   mycroft /*
    281   1.1   mycroft  * Optional hybrid natural/pairwise first pass.  Eats up list1 in runs of
    282   1.1   mycroft  * increasing order, list2 in a corresponding linked list.  Checks for runs
    283   1.1   mycroft  * when THRESHOLD/2 pairs compare with same sense.  (Only used when NATURAL
    284   1.1   mycroft  * is defined.  Otherwise simple pairwise merging is used.)
    285   1.1   mycroft  */
    286  1.17  riastrad static void
    287  1.14  christos setup(u_char *list1, u_char *list2, size_t n, size_t size,
    288  1.17  riastrad     int (*cmp)(const void *, const void *, void *), void *cookie)
    289   1.1   mycroft {
    290  1.14  christos 	int length, tmp, sense;
    291   1.1   mycroft 	u_char *f1, *f2, *s, *l2, *last, *p2;
    292  1.14  christos 	size_t size2, i;
    293   1.1   mycroft 
    294   1.8     lukem 	_DIAGASSERT(cmp != NULL);
    295   1.8     lukem 	_DIAGASSERT(list1 != NULL);
    296   1.8     lukem 	_DIAGASSERT(list2 != NULL);
    297   1.8     lukem 
    298   1.1   mycroft 	size2 = size*2;
    299   1.1   mycroft 	if (n <= 5) {
    300  1.17  riastrad 		insertionsort(list1, n, size, cmp, cookie);
    301   1.7  christos 		*EVAL(list2) = list2 + n*size;
    302   1.1   mycroft 		return;
    303   1.1   mycroft 	}
    304   1.1   mycroft 	/*
    305   1.1   mycroft 	 * Avoid running pointers out of bounds; limit n to evens
    306   1.1   mycroft 	 * for simplicity.
    307   1.1   mycroft 	 */
    308   1.1   mycroft 	i = 4 + (n & 1);
    309  1.17  riastrad 	insertionsort(list1 + (n - i) * size, i, size, cmp, cookie);
    310   1.1   mycroft 	last = list1 + size * (n - i);
    311   1.1   mycroft 	*EVAL(list2 + (last - list1)) = list2 + n * size;
    312   1.1   mycroft 
    313   1.1   mycroft #ifdef NATURAL
    314   1.1   mycroft 	p2 = list2;
    315   1.1   mycroft 	f1 = list1;
    316  1.17  riastrad 	sense = (cmp(f1, f1 + size, cookie) > 0);
    317   1.1   mycroft 	for (; f1 < last; sense = !sense) {
    318   1.1   mycroft 		length = 2;
    319   1.1   mycroft 					/* Find pairs with same sense. */
    320   1.1   mycroft 		for (f2 = f1 + size2; f2 < last; f2 += size2) {
    321  1.17  riastrad 			if ((cmp(f2, f2 + size, cookie) > 0) != sense)
    322   1.1   mycroft 				break;
    323   1.1   mycroft 			length += 2;
    324   1.1   mycroft 		}
    325   1.1   mycroft 		if (length < THRESHOLD) {		/* Pairwise merge */
    326   1.1   mycroft 			do {
    327   1.1   mycroft 				p2 = *EVAL(p2) = f1 + size2 - list1 + list2;
    328   1.1   mycroft 				if (sense > 0)
    329   1.1   mycroft 					swap (f1, f1 + size);
    330   1.1   mycroft 			} while ((f1 += size2) < f2);
    331   1.1   mycroft 		} else {				/* Natural merge */
    332   1.1   mycroft 			l2 = f2;
    333   1.1   mycroft 			for (f2 = f1 + size2; f2 < l2; f2 += size2) {
    334  1.17  riastrad 				if ((cmp(f2-size, f2, cookie) > 0) != sense) {
    335   1.1   mycroft 					p2 = *EVAL(p2) = f2 - list1 + list2;
    336   1.1   mycroft 					if (sense > 0)
    337  1.14  christos 						reverse(f1, f2 - size);
    338   1.1   mycroft 					f1 = f2;
    339   1.1   mycroft 				}
    340   1.1   mycroft 			}
    341   1.1   mycroft 			if (sense > 0)
    342  1.14  christos 				reverse(f1, f2 - size);
    343   1.1   mycroft 			f1 = f2;
    344  1.17  riastrad 			if (f2 < last || cmp(f2 - size, f2, cookie) > 0)
    345   1.1   mycroft 				p2 = *EVAL(p2) = f2 - list1 + list2;
    346   1.1   mycroft 			else
    347   1.1   mycroft 				p2 = *EVAL(p2) = list2 + n*size;
    348   1.1   mycroft 		}
    349   1.1   mycroft 	}
    350   1.1   mycroft #else		/* pairwise merge only. */
    351   1.1   mycroft 	for (f1 = list1, p2 = list2; f1 < last; f1 += size2) {
    352   1.1   mycroft 		p2 = *EVAL(p2) = p2 + size2;
    353  1.17  riastrad 		if (cmp(f1, f1 + size, cookie) > 0)
    354   1.1   mycroft 			swap(f1, f1 + size);
    355   1.1   mycroft 	}
    356   1.1   mycroft #endif /* NATURAL */
    357   1.1   mycroft }
    358   1.1   mycroft 
    359   1.1   mycroft /*
    360   1.1   mycroft  * This is to avoid out-of-bounds addresses in sorting the
    361   1.1   mycroft  * last 4 elements.
    362   1.1   mycroft  */
    363   1.1   mycroft static void
    364  1.14  christos insertionsort(u_char *a, size_t n, size_t size,
    365  1.17  riastrad     int (*cmp)(const void *, const void *, void *), void *cookie)
    366   1.1   mycroft {
    367   1.1   mycroft 	u_char *ai, *s, *t, *u, tmp;
    368  1.14  christos 	size_t i;
    369   1.8     lukem 
    370   1.8     lukem 	_DIAGASSERT(a != NULL);
    371   1.8     lukem 	_DIAGASSERT(cmp != NULL);
    372   1.1   mycroft 
    373   1.1   mycroft 	for (ai = a+size; --n >= 1; ai += size)
    374   1.1   mycroft 		for (t = ai; t > a; t -= size) {
    375   1.1   mycroft 			u = t - size;
    376  1.17  riastrad 			if (cmp(u, t, cookie) <= 0)
    377   1.1   mycroft 				break;
    378   1.1   mycroft 			swap(u, t);
    379   1.1   mycroft 		}
    380   1.1   mycroft }
    381  1.17  riastrad 
    382  1.17  riastrad static int
    383  1.17  riastrad cmpnocookie(const void *a, const void *b, void *cookie)
    384  1.17  riastrad {
    385  1.17  riastrad 	int (*cmp)(const void *, const void *) = cookie;
    386  1.17  riastrad 
    387  1.17  riastrad 	return cmp(a, b);
    388  1.17  riastrad }
    389  1.17  riastrad 
    390  1.17  riastrad int
    391  1.17  riastrad mergesort(void *a, size_t n, size_t es,
    392  1.17  riastrad     int (*cmp)(const void *, const void *))
    393  1.17  riastrad {
    394  1.17  riastrad 
    395  1.17  riastrad 	return mergesort_r(a, n, es, cmpnocookie, cmp);
    396  1.17  riastrad }
    397