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)
163 P(pass)(struct fastbuf **fb1, struct fastbuf **fb2)
165 struct fastbuf *in1 = *fb1;
166 struct fastbuf *in2 = *fb2;
167 struct fastbuf *out1 = NULL;
168 struct fastbuf *out2 = NULL;
169 SORT_KEY kbuf1, kbuf2, kbuf3, kbuf4;
170 SORT_KEY *kin1 = &kbuf1;
171 SORT_KEY *kprev1 = &kbuf2;
172 SORT_KEY *kin2 = &kbuf3;
173 SORT_KEY *kprev2 = &kbuf4;
174 SORT_KEY *kout = NULL;
176 int next1, next2, comp;
180 run1 = next1 = in1 ? P(fetch_key)(in1, kin1) : 0;
181 run2 = next2 = in2 ? P(fetch_key)(in2, kin2) : 0;
182 while (next1 || next2)
189 comp = P(compare)(kin1, kin2);
190 ktmp = (comp <= 0) ? kin1 : kin2;
191 if (!kout || !(P(compare)(kout, ktmp) LESS 0))
196 out1 = bopen_tmp(sorter_stream_bufsize);
201 P(copy_data)(in1, out1, kin1);
202 SWAP(kin1, kprev1, ktmp);
203 next1 = P(fetch_key)(in1, kin1);
204 run1 = next1 && (P(compare)(kprev1, kin1) LESS 0);
210 P(merge_data)(in1, in2, out1, kin1, kin2);
211 SWAP(kin1, kprev1, ktmp);
212 next1 = P(fetch_key)(in1, kin1);
213 run1 = next1 && (P(compare)(kprev1, kin1) LESS 0);
214 SWAP(kin2, kprev2, ktmp);
215 next2 = P(fetch_key)(in2, kin2);
216 run2 = next2 && (P(compare)(kprev2, kin2) LESS 0);
220 #ifdef SORT_ASSERT_UNIQUE
221 else if (unlikely(comp == 0))
226 P(copy_data)(in2, out1, kin2);
227 SWAP(kin2, kprev2, ktmp);
228 next2 = P(fetch_key)(in2, kin2);
229 run2 = next2 && (P(compare)(kprev2, kin2) LESS 0);
241 log(L_INFO, "Pass %d: %d runs, %d+%d KB", sorter_pass_counter, run_count,
242 (out1 ? (int)((btell(out1) + 1023) / 1024) : 0),
243 (out2 ? (int)((btell(out2) + 1023) / 1024) : 0));
244 *fb1 = P(flush_out)(out1);
245 *fb2 = P(flush_out)(out2);
246 sorter_pass_counter++;
251 #if defined(SORT_REGULAR) && !defined(SORT_UNIFY)
253 /* If we are doing a simple sort on a regular file, we can use a faster presorting strategy */
255 static SORT_KEY *P(array);
257 #define ASORT_PREFIX(x) SORT_PREFIX(x##_array)
258 #define ASORT_KEY_TYPE SORT_KEY
259 #define ASORT_ELT(i) P(array)[i]
260 #define ASORT_LT(x,y) (P(compare)(&(x),&(y)) < 0)
262 #include "lib/arraysort.h"
265 P(presort)(struct fastbuf **fb1, struct fastbuf **fb2)
267 struct fastbuf *in = *fb1;
268 struct fastbuf *out1 = NULL;
269 struct fastbuf *out2 = NULL;
270 struct fastbuf *tbuf;
271 uns buf_items = sorter_presort_bufsize / sizeof(SORT_KEY);
273 SORT_KEY last_out, *array;
277 die("PresortBuffer set too low");
278 P(array) = array = xmalloc(buf_items * sizeof(SORT_KEY));
282 uns s = bread(in, array, buf_items * sizeof(SORT_KEY));
283 uns n = s / sizeof(SORT_KEY);
284 ASSERT(!(s % sizeof(SORT_KEY)));
288 #ifdef SORT_ASSERT_UNIQUE
289 for (uns i=0; i<n-1; i++)
290 if (unlikely(P(compare)(&array[i], &array[i+1]) >= 0))
292 ASSERT(!run_count || P(compare)(&last_out, &array[0]));
294 if (!run_count || P(compare)(&last_out, &array[0]) > 0)
297 SWAP(out1, out2, tbuf);
299 out1 = bopen_tmp(sorter_stream_bufsize);
301 last_out = array[n-1];
302 bwrite(out1, array, n * sizeof(SORT_KEY));
307 log(L_INFO, "Pass 0: %d runs, %d+%d KB",
309 (out1 ? (int)((btell(out1) + 1023) / 1024) : 0),
310 (out2 ? (int)((btell(out2) + 1023) / 1024) : 0));
311 *fb1 = P(flush_out)(out1);
312 *fb2 = P(flush_out)(out2);
318 #define SORT_NODE struct P(presort_node)
326 P(mergesort)(SORT_NODE *x)
328 SORT_NODE *f1, **l1, *f2, **l2, **l;
346 f1 = P(mergesort)(f1);
348 f2 = P(mergesort)(f2);
352 if (P(compare)(&f1->key, &f2->key) <= 0)
370 P(presort)(struct fastbuf **fb1, struct fastbuf **fb2)
372 struct fastbuf *in = *fb1;
373 struct fastbuf *out1 = NULL;
374 struct fastbuf *out2 = NULL;
375 struct fastbuf *tbuf;
376 byte *buffer, *bufend, *current;
377 SORT_NODE *first, **last, *this, *leftover;
384 if (sorter_presort_bufsize < 2*sizeof(SORT_NODE))
385 die("PresortBuffer set too low");
386 buffer = xmalloc(sorter_presort_bufsize);
387 bufend = buffer + sorter_presort_bufsize;
391 SWAP(out1, out2, tbuf);
393 out1 = bopen_tmp(sorter_stream_bufsize);
398 memmove(buffer, leftover, sizeof(SORT_NODE));
399 this = leftover = (SORT_NODE *) buffer;
405 current = (byte *) ALIGN((addr_int_t) current, CPU_STRUCT_ALIGN);
406 if (current + sizeof(*this) > bufend)
408 this = (SORT_NODE *) current;
409 cont = P(fetch_key)(in, &this->key);
413 current = P(fetch_item)(in, &this->key, bufend);
416 if (leftover) /* Single node too large */
418 P(copy_data)(in, out1, &leftover->key);
423 else /* Node will be left over to the next phase */
435 first = P(mergesort)(first);
440 SORT_NODE *second = first->next;
441 if (second && !P(compare)(&first->key, &second->key))
443 SORT_KEY *n = P(merge_items)(&first->key, &second->key);
444 if (n == &first->key)
445 first->next = second->next;
449 first = second->next;
453 #ifdef SORT_ASSERT_UNIQUE
454 ASSERT(!first->next || P(compare)(&first->key, &first->next->key));
456 P(store_item)(out1, &first->key);
463 log(L_INFO, "Pass 0: %d runs (%d giants, %d splits), %d+%d KB",
464 run_count, giant_count, split_count,
465 (out1 ? (int)((btell(out1) + 1023) / 1024) : 0),
466 (out2 ? (int)((btell(out2) + 1023) / 1024) : 0));
467 *fb1 = P(flush_out)(out1);
468 *fb2 = P(flush_out)(out2);
472 #endif /* SORT_REGULAR && !SORT_UNIFY */
474 #endif /* SORT_PRESORT */
477 #ifdef SORT_OUTPUT_FB
479 #elif defined(SORT_OUTPUT_FILE)
482 #error No output defined.
485 #ifdef SORT_INPUT_FILE
487 #elif defined(SORT_INPUT_FB)
489 #elif defined(SORT_INPUT_FBPAIR)
490 struct fastbuf *fb1, struct fastbuf *fb2
492 #error No input defined.
494 #ifdef SORT_OUTPUT_FILE
499 #ifdef SORT_INPUT_FILE
500 struct fastbuf *fb1, *fb2;
501 fb1 = bopen(inname, O_RDONLY, sorter_stream_bufsize);
503 #elif defined(SORT_INPUT_FB)
504 struct fastbuf *fb2 = NULL;
507 #ifdef SORT_DELETE_INPUT
508 bconfig(fb1, BCONFIG_IS_TEMP_FILE, SORT_DELETE_INPUT);
510 sorter_pass_counter = 1;
512 P(presort)(&fb1, &fb2);
515 do P(pass)(&fb1, &fb2); while (fb1 && fb2);
517 fb1 = bopen_tmp(sorter_stream_bufsize);
519 #ifdef SORT_OUTPUT_FB
522 bconfig(fb1, BCONFIG_IS_TEMP_FILE, 0);
523 if (rename(fb1->name, outname) < 0)
524 die("rename(%s,%s): %m", fb1->name, outname);
537 #undef SORT_ASSERT_UNIQUE
539 #undef SORT_DELETE_INPUT
540 #undef SORT_INPUT_FILE
542 #undef SORT_INPUT_FBPAIR
543 #undef SORT_OUTPUT_FILE
544 #undef SORT_OUTPUT_FB
546 #endif /* !SORT_DECLARE_ONLY */