2 * Sherlock Library -- Universal Sorter
4 * (c) 2001--2004 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 * This is not a normal header file, it's a generator of sorting
12 * routines. Each time you include it with parameters set in the
13 * corresponding preprocessor macros, it generates a file sorter
14 * with the parameters given.
16 * Recognized parameter macros: (those marked with [*] are mandatory)
18 * SORT_KEY [*] data type capable of storing a single key
19 * SORT_PREFIX(x) [*] add a name prefix (used on all global names
20 * defined by the sorter)
21 * SORT_PRESORT include an in-core presorting pass. Beware, when in
22 * the pre-sorting mode, it's quite possible that the
23 * comparison function will be called with both arguments
25 * SORT_UNIFY merge items with identical keys
26 * SORT_UNIQUE all items have distinct keys (checked in debug mode)
27 * SORT_REGULAR all items are equally long and they don't contain
28 * anything else than the key. In this case, the sorter
29 * automatically supplies fetch_key, copy_data, fetch_item
30 * and store_item functions. Item size is also expected
32 * SORT_DELETE_INPUT a C expression, if true, the input files are
33 * deleted as soon as possible
34 * SORT_INPUT_FILE input is a file with this name
35 * SORT_INPUT_FB input is a fastbuf stream
36 * (can be safely NULL if you want to treat original
37 * input in a different way by file read functions)
38 * SORT_INPUT_FBPAIR input is a pair of fastbuf streams
39 * (not supported by the presorter)
40 * SORT_OUTPUT_FILE output is a file with this name
41 * SORT_OUTPUT_FB output is a temporary fastbuf stream
43 * You also need to define some (usually inline) functions which
44 * are called by the sorter to process your data:
46 * int PREFIX_compare(SORT_KEY *a, *b)
47 * compare two keys, result like strcmp
48 * int PREFIX_fetch_key(struct fastbuf *f, SORT_KEY *k)
49 * fetch next key, returns 1=ok, 0=eof
50 * void PREFIX_copy_data(struct fastbuf *src, *dest, SORT_KEY *k)
51 * write just fetched key k to dest and copy all data
52 * belonging to this key from src to dest.
53 * void PREFIX_merge_data(struct fastbuf *src1, *src2, *dest, SORT_KEY *k1, *k2)
54 * [used only in case SORT_UNIFY is defined]
55 * write just fetched key k to dest and merge data from
56 * two records with the same key (k1 and k2 are key occurences
57 * in the corresponding streams).
58 * byte * PREFIX_fetch_item(struct fastbuf *f, SORT_KEY *k, byte *limit)
59 * [used only with SORT_PRESORT]
60 * fetch data belonging to a just fetched key and store
61 * them to memory following the key, but not over limit.
62 * Returns a pointer to first byte after the data
63 * or NULL if the data don't fit. For variable-length
64 * keys, it can use the rest of SORT_KEY and even return
65 * pointer before end of the key.
66 * Important: before PREFIX_fetch_item() succeeds, the key
67 * must be position independent, the sorter can copy it.
68 * void PREFIX_store_item(struct fastbuf *f, SORT_KEY *k)
69 * [used only with SORT_PRESORT]
70 * write key and all its data read with PREFIX_fetch_data
71 * to the stream given.
72 * SORT_KEY * PREFIX_merge_items(SORT_KEY *a, SORT_KEY *b)
73 * [used only with SORT_PRESORT && SORT_UNIFY]
74 * merge two items with the same key, returns pointer
75 * to at most one of the items, the rest will be removed
76 * from the list of items, but not deallocated, so
77 * the remaining item can freely reference data of the
80 * After including this file, all parameter macros are automatically
84 /* Declarations of externals from sorter.c */
86 #ifndef SORT_DECLS_READ
87 #define SORT_DECLS_READ
89 extern uns sorter_trace;
90 extern uns sorter_presort_bufsize;
91 extern uns sorter_stream_bufsize;
93 extern uns sorter_pass_counter;
95 #endif /* !SORT_DECLS_READ */
97 /* The sorter proper */
99 #ifndef SORT_DECLARE_ONLY
101 #include "lib/fastbuf.h"
106 #if !defined(SORT_KEY) || !defined(SORT_PREFIX)
107 #error Some of the mandatory configuration macros are missing.
110 #define P(x) SORT_PREFIX(x)
111 #define SWAP(x,y,z) do { z=x; x=y; y=z; } while(0)
113 #if defined(SORT_UNIFY) || defined(SORT_UNIQUE)
119 #if defined(SORT_UNIQUE) && defined(DEBUG)
120 #define SORT_ASSERT_UNIQUE
126 P(fetch_key)(struct fastbuf *in, SORT_KEY *x)
128 return breadb(in, x, sizeof(*x));
132 P(copy_data)(struct fastbuf *in UNUSED, struct fastbuf *out, SORT_KEY *x)
134 bwrite(out, x, sizeof(*x));
138 P(fetch_item)(struct fastbuf *in UNUSED, SORT_KEY *x UNUSED, byte *limit UNUSED)
140 return (byte *)(x+1);
144 P(store_item)(struct fastbuf *out, SORT_KEY *x)
146 bwrite(out, x, sizeof(*x));
151 static struct fastbuf *
152 P(flush_out)(struct fastbuf *out)
160 P(pass)(struct fastbuf **fb1, struct fastbuf **fb2)
162 struct fastbuf *in1 = *fb1;
163 struct fastbuf *in2 = *fb2;
164 struct fastbuf *out1 = NULL;
165 struct fastbuf *out2 = NULL;
166 SORT_KEY kbuf1, kbuf2, kbuf3, kbuf4;
167 SORT_KEY *kin1 = &kbuf1;
168 SORT_KEY *kprev1 = &kbuf2;
169 SORT_KEY *kin2 = &kbuf3;
170 SORT_KEY *kprev2 = &kbuf4;
171 SORT_KEY *kout = NULL;
173 int next1, next2, comp;
177 run1 = next1 = in1 ? P(fetch_key)(in1, kin1) : 0;
178 run2 = next2 = in2 ? P(fetch_key)(in2, kin2) : 0;
179 while (next1 || next2)
186 comp = P(compare)(kin1, kin2);
187 ktmp = (comp <= 0) ? kin1 : kin2;
188 if (!kout || !(P(compare)(kout, ktmp) LESS 0))
193 out1 = bopen_tmp(sorter_stream_bufsize);
198 P(copy_data)(in1, out1, kin1);
199 SWAP(kin1, kprev1, ktmp);
200 next1 = P(fetch_key)(in1, kin1);
201 run1 = next1 && (P(compare)(kprev1, kin1) LESS 0);
207 P(merge_data)(in1, in2, out1, kin1, kin2);
208 SWAP(kin1, kprev1, ktmp);
209 next1 = P(fetch_key)(in1, kin1);
210 run1 = next1 && (P(compare)(kprev1, kin1) LESS 0);
211 SWAP(kin2, kprev2, ktmp);
212 next2 = P(fetch_key)(in2, kin2);
213 run2 = next2 && (P(compare)(kprev2, kin2) LESS 0);
217 #ifdef SORT_ASSERT_UNIQUE
218 else if (unlikely(comp == 0))
223 P(copy_data)(in2, out1, kin2);
224 SWAP(kin2, kprev2, ktmp);
225 next2 = P(fetch_key)(in2, kin2);
226 run2 = next2 && (P(compare)(kprev2, kin2) LESS 0);
238 log(L_INFO, "Pass %d: %d runs, %d+%d KB", sorter_pass_counter, run_count,
239 (out1 ? (int)((btell(out1) + 1023) / 1024) : 0),
240 (out2 ? (int)((btell(out2) + 1023) / 1024) : 0));
241 *fb1 = P(flush_out)(out1);
242 *fb2 = P(flush_out)(out2);
243 sorter_pass_counter++;
248 #if defined(SORT_REGULAR) && !defined(SORT_UNIFY)
250 /* If we are doing a simple sort on a regular file, we can use a faster presorting strategy */
252 static SORT_KEY *P(array);
254 #define ASORT_PREFIX(x) SORT_PREFIX(x##_array)
255 #define ASORT_KEY_TYPE SORT_KEY
256 #define ASORT_ELT(i) P(array)[i]
257 #define ASORT_LT(x,y) (P(compare)(&(x),&(y)) < 0)
259 #include "lib/arraysort.h"
262 P(presort)(struct fastbuf **fb1, struct fastbuf **fb2)
264 struct fastbuf *in = *fb1;
265 struct fastbuf *out1 = NULL;
266 struct fastbuf *out2 = NULL;
267 struct fastbuf *tbuf;
268 uns buf_items = sorter_presort_bufsize / sizeof(SORT_KEY);
270 SORT_KEY last_out, *array;
274 die("PresortBuffer set too low");
275 P(array) = array = xmalloc(buf_items * sizeof(SORT_KEY));
279 uns s = bread(in, array, buf_items * sizeof(SORT_KEY));
280 uns n = s / sizeof(SORT_KEY);
281 ASSERT(!(s % sizeof(SORT_KEY)));
285 #ifdef SORT_ASSERT_UNIQUE
286 for (uns i=0; i<n-1; i++)
287 if (unlikely(P(compare)(&array[i], &array[i+1]) >= 0))
289 ASSERT(!run_count || P(compare)(&last_out, &array[0]));
291 if (!run_count || P(compare)(&last_out, &array[0]) > 0)
294 SWAP(out1, out2, tbuf);
296 out1 = bopen_tmp(sorter_stream_bufsize);
298 last_out = array[n-1];
299 bwrite(out1, array, n * sizeof(SORT_KEY));
304 log(L_INFO, "Pass 0: %d runs, %d+%d KB",
306 (out1 ? (int)((btell(out1) + 1023) / 1024) : 0),
307 (out2 ? (int)((btell(out2) + 1023) / 1024) : 0));
308 *fb1 = P(flush_out)(out1);
309 *fb2 = P(flush_out)(out2);
315 #define SORT_NODE struct P(presort_node)
323 P(mergesort)(SORT_NODE *x)
325 SORT_NODE *f1, **l1, *f2, **l2, **l;
343 f1 = P(mergesort)(f1);
345 f2 = P(mergesort)(f2);
349 if (P(compare)(&f1->key, &f2->key) <= 0)
367 P(presort)(struct fastbuf **fb1, struct fastbuf **fb2)
369 struct fastbuf *in = *fb1;
370 struct fastbuf *out1 = NULL;
371 struct fastbuf *out2 = NULL;
372 struct fastbuf *tbuf;
373 byte *buffer, *bufend, *current;
374 SORT_NODE *first, **last, *this, *leftover;
381 if (sorter_presort_bufsize < 2*sizeof(SORT_NODE))
382 die("PresortBuffer set too low");
383 buffer = xmalloc(sorter_presort_bufsize);
384 bufend = buffer + sorter_presort_bufsize;
388 SWAP(out1, out2, tbuf);
390 out1 = bopen_tmp(sorter_stream_bufsize);
395 memmove(buffer, leftover, sizeof(SORT_NODE));
396 this = leftover = (SORT_NODE *) buffer;
402 current = (byte *) ALIGN((addr_int_t) current, CPU_STRUCT_ALIGN);
403 if (current + sizeof(*this) > bufend)
405 this = (SORT_NODE *) current;
406 cont = P(fetch_key)(in, &this->key);
410 current = P(fetch_item)(in, &this->key, bufend);
413 if (leftover) /* Single node too large */
415 P(copy_data)(in, out1, &leftover->key);
420 else /* Node will be left over to the next phase */
432 first = P(mergesort)(first);
437 SORT_NODE *second = first->next;
438 if (second && !P(compare)(&first->key, &second->key))
440 SORT_KEY *n = P(merge_items)(&first->key, &second->key);
441 if (n == &first->key)
442 first->next = second->next;
446 first = second->next;
450 #ifdef SORT_ASSERT_UNIQUE
451 ASSERT(!first->next || P(compare)(&first->key, &first->next->key));
453 P(store_item)(out1, &first->key);
460 log(L_INFO, "Pass 0: %d runs (%d giants, %d splits), %d+%d KB",
461 run_count, giant_count, split_count,
462 (out1 ? (int)((btell(out1) + 1023) / 1024) : 0),
463 (out2 ? (int)((btell(out2) + 1023) / 1024) : 0));
464 *fb1 = P(flush_out)(out1);
465 *fb2 = P(flush_out)(out2);
469 #endif /* SORT_REGULAR && !SORT_UNIFY */
471 #endif /* SORT_PRESORT */
474 #ifdef SORT_OUTPUT_FB
476 #elif defined(SORT_OUTPUT_FILE)
479 #error No output defined.
482 #ifdef SORT_INPUT_FILE
484 #elif defined(SORT_INPUT_FB)
486 #elif defined(SORT_INPUT_FBPAIR)
487 struct fastbuf *fb1, struct fastbuf *fb2
489 #error No input defined.
491 #ifdef SORT_OUTPUT_FILE
496 #ifdef SORT_INPUT_FILE
497 struct fastbuf *fb1, *fb2;
498 fb1 = bopen(inname, O_RDONLY, sorter_stream_bufsize);
500 #elif defined(SORT_INPUT_FB)
501 struct fastbuf *fb2 = NULL;
504 #ifdef SORT_DELETE_INPUT
505 bconfig(fb1, BCONFIG_IS_TEMP_FILE, SORT_DELETE_INPUT);
507 sorter_pass_counter = 1;
509 P(presort)(&fb1, &fb2);
512 do P(pass)(&fb1, &fb2); while (fb1 && fb2);
514 fb1 = bopen_tmp(sorter_stream_bufsize);
516 #ifdef SORT_OUTPUT_FB
519 bconfig(fb1, BCONFIG_IS_TEMP_FILE, 0);
520 if (rename(fb1->name, outname) < 0)
521 die("rename(%s,%s): %m", fb1->name, outname);
534 #undef SORT_ASSERT_UNIQUE
536 #undef SORT_DELETE_INPUT
537 #undef SORT_INPUT_FILE
539 #undef SORT_INPUT_FBPAIR
540 #undef SORT_OUTPUT_FILE
541 #undef SORT_OUTPUT_FB
543 #endif /* !SORT_DECLARE_ONLY */