Remove <string.h> include from system.h into the few places that still need
[platform/upstream/rpm.git] / lib / merge.c
1 #ifndef __APPLE__
2 /*-
3  * Copyright (c) 1992, 1993
4  *      The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Peter McIlroy.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
24  *
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
35  * SUCH DAMAGE.
36  */
37
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 */
41
42 /*
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.
47  */
48
49 #define NATURAL
50 #define THRESHOLD 16    /* Best choice for natural merge cut-off. */
51
52 /* #define NATURAL to get hybrid natural merge.
53  * (The default is pairwise merging.)
54  */
55
56 #include "system.h"
57 #include <stdlib.h>
58 #include <string.h>
59 #include "lib/rpmdb_internal.h" /* XXX for mergesort */
60
61 #define ISIZE sizeof(int)
62 #define PSIZE sizeof(unsigned char *)
63 #define ICOPY_LIST(src, dst, last)                              \
64         do                                                      \
65         *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE;    \
66         while(src < last)
67 #define ICOPY_ELT(src, dst, i)                                  \
68         do                                                      \
69         *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE;  \
70         while (i -= ISIZE)
71
72 #define CCOPY_LIST(src, dst, last)              \
73         do                                      \
74                 *dst++ = *src++;                \
75         while (src < last)
76 #define CCOPY_ELT(src, dst, i)                  \
77         do                                      \
78                 *dst++ = *src++;                \
79         while (i -= 1)
80
81 /*
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
84  * boundaries.
85  */
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)))
90
91 #define swap(a, b) {                                    \
92                 s = b;                                  \
93                 i = size;                               \
94                 do {                                    \
95                         tmp = *a; *a++ = *s; *s++ = tmp; \
96                 } while (--i);                          \
97                 a -= size;                              \
98         }
99 #define reverse(bot, top) {                             \
100         s = top;                                        \
101         do {                                            \
102                 i = size;                               \
103                 do {                                    \
104                         tmp = *bot; *bot++ = *s; *s++ = tmp; \
105                 } while (--i);                          \
106                 s -= size2;                             \
107         } while(bot < s);                               \
108 }
109
110 /*
111  * This is to avoid out-of-bounds addresses in sorting the
112  * last 4 elements.
113  */
114 static void
115 insertionsort(unsigned char *a, size_t n, size_t size,
116                 int (*cmp) (const void *, const void *))
117 {
118         unsigned char *ai, *s, *t, *u, tmp;
119         int i;
120
121         for (ai = a+size; --n >= 1; ai += size)
122                 for (t = ai; t > a; t -= size) {
123                         u = t - size;
124                         if (cmp(u, t) <= 0)
125                                 break;
126                         swap(u, t);
127                 }
128 }
129
130 /*
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.)
135  */
136 static void
137 setup(unsigned char *list1, unsigned char *list2,
138                 size_t n, size_t size, int (*cmp) (const void *, const void *))
139 {
140         int i, length, size2, tmp, sense;
141         unsigned char *f1, *f2, *s, *l2, *last, *p2;
142
143         size2 = size*2;
144         if (n <= 5) {
145                 insertionsort(list1, n, size, cmp);
146                 *EVAL(list2) = (unsigned char*) list2 + n*size;
147                 return;
148         }
149         /*
150          * Avoid running pointers out of bounds; limit n to evens
151          * for simplicity.
152          */
153         i = 4 + (n & 1);
154         insertionsort(list1 + (n - i) * size, i, size, cmp);
155         last = list1 + size * (n - i);
156         *EVAL(list2 + (last - list1)) = list2 + n * size;
157
158 #ifdef NATURAL
159         p2 = list2;
160         f1 = list1;
161         sense = (cmp(f1, f1 + size) > 0);
162         for (; f1 < last; sense = !sense) {
163                 length = 2;
164                                         /* Find pairs with same sense. */
165                 for (f2 = f1 + size2; f2 < last; f2 += size2) {
166                         if ((cmp(f2, f2+ size) > 0) != sense)
167                                 break;
168                         length += 2;
169                 }
170                 if (length < THRESHOLD) {               /* Pairwise merge */
171                         do {
172                                 p2 = *EVAL(p2) = f1 + size2 - list1 + list2;
173                                 if (sense > 0)
174                                         swap (f1, f1 + size);
175                         } while ((f1 += size2) < f2);
176                 } else {                                /* Natural merge */
177                         l2 = 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;
181                                         if (sense > 0)
182                                                 reverse(f1, f2-size);
183                                         f1 = f2;
184                                 }
185                         }
186                         if (sense > 0)
187                                 reverse (f1, f2-size);
188                         f1 = f2;
189                         if (f2 < last || cmp(f2 - size, f2) > 0)
190                                 p2 = *EVAL(p2) = f2 - list1 + list2;
191                         else
192                                 p2 = *EVAL(p2) = list2 + n*size;
193                 }
194         }
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)
199                         swap(f1, f1 + size);
200         }
201 #endif /* NATURAL */
202 }
203
204 /*
205  * Arguments are as for qsort.
206  */
207 int
208 mergesort(void *base, size_t nmemb, size_t size,
209                 int (*cmp) (const void *, const void *))
210 {
211         register int i, sense;
212         int big, iflag;
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;
218
219         if (size < PSIZE / 2) {         /* Pointers must fit into 2 * size. */
220                 errno = EINVAL;
221                 return (-1);
222         }
223
224         if (nmemb == 0)
225                 return (0);
226
227         /*
228          * XXX
229          * Stupid subtraction for the Cray.
230          */
231         iflag = 0;
232         if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE))
233                 iflag = 1;
234
235         if ((list2 = malloc(nmemb * size + PSIZE)) == NULL)
236                 return (-1);
237
238         list1 = base;
239         setup(list1, list2, nmemb, size, cmp);
240         last = list2 + nmemb * size;
241         i = big = 0;
242         while (*EVAL(list2) != last) {
243             l2 = list1;
244             p1 = EVAL(list1);
245             for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) {
246                 p2 = *EVAL(p2);
247                 f1 = l2;
248                 f2 = l1 = list1 + (p2 - list2);
249                 if (p2 != last)
250                         p2 = *EVAL(p2);
251                 l2 = list1 + (p2 - list2);
252                 while (f1 < l1 && f2 < l2) {
253                         if ((*cmp)(f1, f2) <= 0) {
254                                 q = f2;
255                                 b = f1, t = l1;
256                                 sense = -1;
257                         } else {
258                                 q = f1;
259                                 b = f2, t = l2;
260                                 sense = 0;
261                         }
262                         if (!big) {     /* here i = 0 */
263                                 while ((b += size) < t && cmp(q, b) >sense)
264                                         if (++i == 6) {
265                                                 big = 1;
266                                                 goto EXPONENTIAL;
267                                         }
268                         } else {
269 EXPONENTIAL:                    for (i = size; ; i <<= 1)
270                                         if ((p = (b + i)) >= t) {
271                                                 if ((p = t - size) > b &&
272                                                     (*cmp)(q, p) <= sense)
273                                                         t = p;
274                                                 else
275                                                         b = p;
276                                                 break;
277                                         } else if ((*cmp)(q, p) <= sense) {
278                                                 t = p;
279                                                 if (i == size)
280                                                         big = 0;
281                                                 goto FASTCASE;
282                                         } else
283                                                 b = p;
284                                 while (t > b+size) {
285                                         i = (((t - b) / size) >> 1) * size;
286                                         if ((*cmp)(q, p = b + i) <= sense)
287                                                 t = p;
288                                         else
289                                                 b = p;
290                                 }
291                                 goto COPY;
292 FASTCASE:                       while (i > size)
293                                         if ((*cmp)(q,
294                                                 p = b + (i >>= 1)) <= sense)
295                                                 t = p;
296                                         else
297                                                 b = p;
298 COPY:                           b = t;
299                         }
300                         i = size;
301                         if (q == f1) {
302                                 if (iflag) {
303                                         ICOPY_LIST(f2, tp2, b);
304                                         ICOPY_ELT(f1, tp2, i);
305                                 } else {
306                                         CCOPY_LIST(f2, tp2, b);
307                                         CCOPY_ELT(f1, tp2, i);
308                                 }
309                         } else {
310                                 if (iflag) {
311                                         ICOPY_LIST(f1, tp2, b);
312                                         ICOPY_ELT(f2, tp2, i);
313                                 } else {
314                                         CCOPY_LIST(f1, tp2, b);
315                                         CCOPY_ELT(f2, tp2, i);
316                                 }
317                         }
318                 }
319                 if (f2 < l2) {
320                         if (iflag)
321                                 ICOPY_LIST(f2, tp2, l2);
322                         else
323                                 CCOPY_LIST(f2, tp2, l2);
324                 } else if (f1 < l1) {
325                         if (iflag)
326                                 ICOPY_LIST(f1, tp2, l1);
327                         else
328                                 CCOPY_LIST(f1, tp2, l1);
329                 }
330                 *p1 = l2;
331             }
332             tp2 = list1;        /* swap list1, list2 */
333             list1 = list2;
334             list2 = tp2;
335             last = list2 + nmemb*size;
336         }
337         if (base == list2) {
338                 memmove(list2, list1, nmemb*size);
339                 list2 = list1;
340         }
341         free(list2);
342         return (0);
343 }
344 #else
345 /* mergesort is implemented in System on Mac OS X */
346 #endif /* __APPLE__ */