rpm  5.4.10
merge.c
Go to the documentation of this file.
1 /*@-mustmod -sizeoftype @*/
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 
58 #include <rpmiotypes.h>
59 #include <rpmtag.h>
60 #include <rpmdb.h>
61 
62 #include "debug.h"
63 
64 #define ISIZE sizeof(int)
65 #define PSIZE sizeof(unsigned char *)
66 #define ICOPY_LIST(src, dst, last) \
67  do \
68  *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \
69  while(src < last)
70 #define ICOPY_ELT(src, dst, i) \
71  do \
72  *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \
73  while (i -= ISIZE)
74 
75 #define CCOPY_LIST(src, dst, last) \
76  do \
77  *dst++ = *src++; \
78  while (src < last)
79 #define CCOPY_ELT(src, dst, i) \
80  do \
81  *dst++ = *src++; \
82  while (i -= 1)
83 
84 /*
85  * Find the next possible pointer head. (Trickery for forcing an array
86  * to do double duty as a linked list when objects do not align with word
87  * boundaries.
88  */
89 /* Assumption: PSIZE is a power of 2. */
90 #define EVAL(p) (unsigned char **) \
91  ((unsigned char *)0 + \
92  (((unsigned char *)p + PSIZE - 1 - (unsigned char *) 0) & ~(PSIZE - 1)))
93 
94 #define swap(a, b) { \
95  s = b; \
96  i = (int) size; \
97  do { \
98  tmp = *a; *a++ = *s; *s++ = tmp; \
99  } while (--i); \
100  a -= size; \
101  }
102 #define reverse(bot, top) { \
103  s = top; \
104  do { \
105  i = (int) size; \
106  do { \
107  tmp = *bot; *bot++ = *s; *s++ = tmp; \
108  } while (--i); \
109  s -= size2; \
110  } while(bot < s); \
111 }
112 
113 /*
114  * This is to avoid out-of-bounds addresses in sorting the
115  * last 4 elements.
116  */
117 static void
118 insertionsort(unsigned char *a, size_t n, size_t size,
119  int (*cmp) (const void *, const void *))
120  /*@modifies *a @*/
121 {
122  u_char *ai, *s, *t, *u, tmp;
123  int i;
124 
125  for (ai = a+size; --n >= 1; ai += size)
126  for (t = ai; t > a; t -= size) {
127  u = t - size;
128  if (cmp(u, t) <= 0)
129  /*@innerbreak@*/ break;
130  swap(u, t);
131  }
132 }
133 
134 /*
135  * Optional hybrid natural/pairwise first pass. Eats up list1 in runs of
136  * increasing order, list2 in a corresponding linked list. Checks for runs
137  * when THRESHOLD/2 pairs compare with same sense. (Only used when NATURAL
138  * is defined. Otherwise simple pairwise merging is used.)
139  */
140 static void
141 setup(unsigned char *list1, /*@out@*/ unsigned char *list2,
142  size_t n, size_t size, int (*cmp) (const void *, const void *))
143  /*@modifies list1, list2 @*/
144 {
145  int i, length, size2, sense;
146  unsigned char *f1, *f2, *s, *l2, *last, *p2, tmp;
147 
148  size2 = size*2;
149  if (n <= 5) {
150  insertionsort(list1, n, size, cmp);
151  *EVAL(list2) = (unsigned char*) list2 + n*size;
152  return;
153  }
154  /*
155  * Avoid running pointers out of bounds; limit n to evens
156  * for simplicity.
157  */
158  i = (int)(4 + (n & 1));
159  insertionsort(list1 + (n - i) * size, i, size, cmp);
160  last = list1 + size * (n - i);
161  *EVAL(list2 + (last - list1)) = list2 + n * size;
162 
163 #ifdef NATURAL
164  p2 = list2;
165  f1 = list1;
166  sense = (cmp(f1, f1 + size) > 0);
167  for (; f1 < last; sense = !sense) {
168  length = 2;
169  /* Find pairs with same sense. */
170  for (f2 = f1 + size2; f2 < last; f2 += size2) {
171  if ((cmp(f2, f2+ size) > 0) != sense)
172  /*@innerbreak@*/ break;
173  length += 2;
174  }
175  if (length < THRESHOLD) { /* Pairwise merge */
176  do {
177  p2 = *EVAL(p2) = f1 + size2 - list1 + list2;
178  if (sense > 0)
179  swap (f1, f1 + size);
180  } while ((f1 += size2) < f2);
181  } else { /* Natural merge */
182  l2 = f2;
183  for (f2 = f1 + size2; f2 < l2; f2 += size2) {
184  if ((cmp(f2-size, f2) > 0) != sense) {
185  p2 = *EVAL(p2) = f2 - list1 + list2;
186  if (sense > 0)
187  reverse(f1, f2-size);
188  f1 = f2;
189  }
190  }
191  if (sense > 0)
192  reverse (f1, f2-size);
193  f1 = f2;
194  if (f2 < last || cmp(f2 - size, f2) > 0)
195  p2 = *EVAL(p2) = f2 - list1 + list2;
196  else
197  p2 = *EVAL(p2) = list2 + n*size;
198  }
199  }
200 #else /* pairwise merge only. */
201  for (f1 = list1, p2 = list2; f1 < last; f1 += size2) {
202  p2 = *EVAL(p2) = p2 + size2;
203  if (cmp (f1, f1 + size) > 0)
204  swap(f1, f1 + size);
205  }
206 #endif /* NATURAL */
207 }
208 
209 /*
210  * Arguments are as for qsort.
211  */
212 int
213 rpm_mergesort(void *base, size_t nmemb, size_t size,
214  int (*cmp) (const void *, const void *))
215 {
216  register int i, sense;
217  int big, iflag;
218  register unsigned char *f1, *f2, *t, *b, *q, *l1, *l2;
219  /*@dependent@*/
220  register unsigned char *tp2;
221  /*@owned@*/
222  unsigned char *list2;
223  /*@dependent@*/
224  unsigned char *list1;
225  unsigned char *p2, *p, *last, **p1;
226 
227  if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */
228  errno = EINVAL;
229  return (-1);
230  }
231 
232  if (nmemb == 0)
233  return (0);
234 
235  /*
236  * XXX
237  * Stupid subtraction for the Cray.
238  */
239  iflag = 0;
240  if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE))
241  iflag = 1;
242 
243  if ((list2 = (unsigned char *) malloc(nmemb * size + PSIZE)) == NULL)
244  return (-1);
245 
246  list1 = (unsigned char *) base;
247  setup(list1, list2, nmemb, size, cmp);
248  last = list2 + nmemb * size;
249  i = big = 0;
250  while (*EVAL(list2) != last) {
251  l2 = list1;
252  p1 = EVAL(list1);
253  for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) {
254  p2 = *EVAL(p2);
255  f1 = l2;
256  f2 = l1 = list1 + (p2 - list2);
257  if (p2 != last)
258  p2 = *EVAL(p2);
259  l2 = list1 + (p2 - list2);
260  while (f1 < l1 && f2 < l2) {
261  if ((*cmp)(f1, f2) <= 0) {
262  q = f2;
263  b = f1, t = l1;
264  sense = -1;
265  } else {
266  q = f1;
267  b = f2, t = l2;
268  sense = 0;
269  }
270  if (!big) { /* here i = 0 */
271  while ((b += size) < t && cmp(q, b) >sense)
272  if (++i == 6) {
273  big = 1;
274  goto EXPONENTIAL;
275  }
276  } else {
277 /*@-shiftimplementation@*/
278 EXPONENTIAL: for (i = (int) size; ; i <<= 1)
279  if ((p = (b + i)) >= t) {
280  if ((p = t - size) > b &&
281  (*cmp)(q, p) <= sense)
282  t = p;
283  else
284  b = p;
285  /*@innerbreak@*/ break;
286  } else if ((*cmp)(q, p) <= sense) {
287  t = p;
288  if (i == (int) size)
289  big = 0;
290  goto FASTCASE;
291  } else
292  b = p;
293  /*@-infloopsuncon@*/
294  while (t > b+size) {
295  i = (int) ((((t - b) / size) >> 1) * size);
296  if ((*cmp)(q, p = b + i) <= sense)
297  t = p;
298  else
299  b = p;
300  }
301  goto COPY;
302 FASTCASE: while (i > (int) size)
303  if ((*cmp)(q,
304  p = b + (i >>= 1)) <= sense)
305  t = p;
306  else
307  b = p;
308  /*@=infloopsuncon@*/
309 /*@=shiftimplementation@*/
310 COPY: b = t;
311  }
312  i = (int) size;
313  if (q == f1) {
314  if (iflag) {
315  ICOPY_LIST(f2, tp2, b);
316  ICOPY_ELT(f1, tp2, i);
317  } else {
318  CCOPY_LIST(f2, tp2, b);
319  CCOPY_ELT(f1, tp2, i);
320  }
321  } else {
322  if (iflag) {
323  ICOPY_LIST(f1, tp2, b);
324  ICOPY_ELT(f2, tp2, i);
325  } else {
326  CCOPY_LIST(f1, tp2, b);
327  CCOPY_ELT(f2, tp2, i);
328  }
329  }
330  }
331  if (f2 < l2) {
332  if (iflag)
333  ICOPY_LIST(f2, tp2, l2);
334  else
335  CCOPY_LIST(f2, tp2, l2);
336  } else if (f1 < l1) {
337  if (iflag)
338  ICOPY_LIST(f1, tp2, l1);
339  else
340  CCOPY_LIST(f1, tp2, l1);
341  }
342  *p1 = l2;
343  }
344 /*@-dependenttrans@*/
345  tp2 = list1; /* swap list1, list2 */
346  list1 = list2;
347  list2 = tp2;
348 /*@=dependenttrans@*/
349  last = list2 + nmemb*size;
350  }
351  if (base == list2) {
352  memmove(list2, list1, nmemb*size);
353  list2 = list1;
354  }
355 /*@-usereleased@*/
356  free(list2);
357 /*@=usereleased@*/
358  return (0);
359 }
360 /*@=mustmod =sizeoftype @*/
#define reverse(bot, top)
Definition: merge.c:102
int errno
#define THRESHOLD
Definition: merge.c:50
static void insertionsort(unsigned char *a, size_t n, size_t size, int(*cmp)(const void *, const void *))
Definition: merge.c:118
#define ICOPY_ELT(src, dst, i)
Definition: merge.c:70
static void setup(unsigned char *list1, unsigned char *list2, size_t n, size_t size, int(*cmp)(const void *, const void *))
Definition: merge.c:141
#define EVAL(p)
Definition: merge.c:90
#define CCOPY_LIST(src, dst, last)
Definition: merge.c:75
int rpm_mergesort(void *base, size_t nmemb, size_t size, int(*cmp)(const void *, const void *))
Mergesort, same arguments as qsort(2).
Definition: merge.c:213
#define swap(a, b)
Definition: merge.c:94
#define ICOPY_LIST(src, dst, last)
Definition: merge.c:66
Access RPM indices using Berkeley DB interface(s).
#define ISIZE
Definition: merge.c:64
#define CCOPY_ELT(src, dst, i)
Definition: merge.c:79
#define PSIZE
Definition: merge.c:65