2 * UCW Library -- Universal Sorter
4 * (c) 2001--2007 Martin Mares <mj@ucw.cz>
5 * (c) 2004 Robert Spalek <robert@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
12 * This is not a normal header file, but a generator of sorting
13 * routines. Each time you include it with parameters set in the
14 * corresponding preprocessor macros, it generates a file sorter
15 * with the parameters given.
17 * FIXME: explain the basics (keys and data, callbacks etc.)
19 * Basic parameters and callbacks:
21 * SORT_PREFIX(x) add a name prefix (used on all global names
22 * defined by the sorter)
24 * SORT_KEY data type capable of storing a single key.
25 * SORT_KEY_REGULAR data type holding a single key both in memory and on disk;
26 * in this case, bread() and bwrite() is used to read/write keys
27 * and it's also assumed that the keys are not very long.
28 * int PREFIX_compare(SORT_KEY *a, SORT_KEY *b)
29 * compares two keys, result like strcmp(). Mandatory.
30 * int PREFIX_read_key(struct fastbuf *f, SORT_KEY *k)
31 * reads a key from a fastbuf, returns nonzero=ok, 0=EOF.
32 * Mandatory unless SORT_KEY_REGULAR is defined.
33 * void PREFIX_write_key(struct fastbuf *f, SORT_KEY *k)
34 * writes a key to a fastbuf. Mandatory unless SORT_KEY_REGULAR.
36 * SORT_KEY_SIZE(key) returns the real size of a key (a SORT_KEY type in memory
37 * can be truncated to this number of bytes without any harm;
38 * used to save memory when the keys have variable sizes).
39 * Default: always store the whole SORT_KEY.
40 * SORT_DATA_SIZE(key) gets a key and returns the amount of data following it.
41 * Default: records consist of keys only.
45 * SORT_INT(key) We are sorting by an integer value. In this mode, PREFIX_compare
46 * is supplied automatically and the sorting function gets an extra
47 * parameter specifying a range of the integers. The better the range
48 * fits, the faster we sort. Sets up SORT_HASH_xxx automatically.
50 * Hashing (optional, but it can speed sorting up):
52 * SORT_HASH_BITS signals that a monotone hashing function returning a given number of
53 * bits is available. Monotone hash is a function f such that f(x) < f(y)
54 * implies x < y and which is approximately uniformly distributed.
55 * uns PREFIX_hash(SORT_KEY *a)
59 * SORT_UNIFY merge items with identical keys, needs the following functions:
60 * void PREFIX_write_merged(struct fastbuf *f, SORT_KEY **keys, void **data, uns n, void *buf)
61 * takes n records in memory with keys which compare equal and writes
62 * a single record to the given fastbuf. `buf' points to a buffer which
63 * is guaranteed to hold all given records.
64 * void PREFIX_copy_merged(SORT_KEY **keys, struct fastbuf **data, uns n, struct fastbuf *dest)
65 * takes n records with keys in memory and data in fastbufs and writes
68 * Input (choose one of these):
70 * SORT_INPUT_FILE file of a given name
71 * SORT_INPUT_FB seekable fastbuf stream
72 * SORT_INPUT_PIPE non-seekable fastbuf stream
73 * SORT_INPUT_PRESORT custom presorter. Calls function
74 * int PREFIX_presort(struct fastbuf *dest, void *buf, size_t bufsize);
75 * to get successive batches of pre-sorted data.
76 * The function is passed a page-aligned presorting buffer.
77 * It returns 1 on success or 0 on EOF.
78 * SORT_DELETE_INPUT A C expression, if true, then the input files are deleted
79 * as soon as possible.
81 * Output (chose one of these):
83 * SORT_OUTPUT_FILE file of a given name
84 * SORT_OUTPUT_FB temporary fastbuf stream
85 * SORT_OUTPUT_THIS_FB a given fastbuf stream which can already contain a header
89 * SORT_UNIQUE all items have distinct keys (checked in debug mode)
91 * The function generated:
93 * <outfb> PREFIX_sort(<in>, <out> [,<range>]), where:
94 * <in> = input file name/fastbuf or NULL
95 * <out> = output file name/fastbuf or NULL
96 * <range> = maximum integer value for the SORT_INT mode
97 * <outfb> = output fastbuf (in SORT_OUTPUT_FB mode)
99 * After including this file, all parameter macros are automatically
103 #include "lib/sorter/common.h"
104 #include "lib/fastbuf.h"
108 #define P(x) SORT_PREFIX(x)
110 #ifdef SORT_KEY_REGULAR
111 typedef SORT_KEY_REGULAR P(key);
112 static inline int P(read_key) (struct fastbuf *f, P(key) *k)
114 return breadb(f, k, sizeof(P(key)));
116 static inline void P(write_key) (struct fastbuf *f, P(key) *k)
118 bwrite(f, k, sizeof(P(key)));
120 #elif defined(SORT_KEY)
121 typedef SORT_KEY P(key);
123 #error Missing definition of sorting key.
127 static inline int P(compare) (P(key) *x, P(key) *y)
129 if (SORT_INT(*x) < SORT_INT(*y))
131 if (SORT_INT(*x) > SORT_INT(*y))
136 #ifndef SORT_HASH_BITS
137 static inline int P(hash) (P(key) *x)
139 return SORT_INT((*x));
149 #define SWAP(x,y,z) do { z=x; x=y; y=z; } while(0)
151 #if defined(SORT_UNIQUE) && defined(DEBUG_ASSERTS)
152 #define SORT_ASSERT_UNIQUE
158 #define SORT_KEY_SIZE(key) sizeof(key)
161 #ifdef SORT_DATA_SIZE
162 #define SORT_VAR_DATA
164 #define SORT_DATA_SIZE(key) 0
167 static inline void P(copy_data)(P(key) *key, struct fastbuf *in, struct fastbuf *out)
169 P(write_key)(out, key);
171 bbcopy(in, out, SORT_DATA_SIZE(*key));
177 #if defined(SORT_VAR_KEY) || defined(SORT_VAR_DATA)
178 #include "lib/sorter/s-internal.h"
180 #include "lib/sorter/s-fixint.h"
183 #include "lib/sorter/s-twoway.h"
185 #if defined(SORT_HASH_BITS) || defined(SORT_INT)
186 #include "lib/sorter/s-radix.h"
189 static struct fastbuf *P(sort)(
190 #ifdef SORT_INPUT_FILE
195 #ifdef SORT_OUTPUT_FILE
205 struct sort_context ctx;
206 bzero(&ctx, sizeof(ctx));
208 #ifdef SORT_INPUT_FILE
209 ctx.in_fb = bopen(in, O_RDONLY, sorter_stream_bufsize);
210 ctx.in_size = bfilesize(ctx.in_fb);
211 #elif defined(SORT_INPUT_FB)
213 ctx.in_size = bfilesize(in);
214 #elif defined(SORT_INPUT_PIPE)
216 ctx.in_size = ~(u64)0;
217 #elif defined(SORT_INPUT_PRESORT)
219 ctx.custom_presort = P(presort);
220 ctx.in_size = ~(u64)0;
222 #error No input given.
224 #ifdef SORT_DELETE_INPUT
225 if (SORT_DELETE_INPUT)
226 bconfig(ctx.in_fb, BCONFIG_IS_TEMP_FILE, 1);
229 #ifdef SORT_OUTPUT_FB
231 #elif defined(SORT_OUTPUT_THIS_FB)
233 #elif defined(SORT_OUTPUT_FILE)
234 /* Just assume fastbuf output and rename the fastbuf later */
236 #error No output given.
239 #ifdef SORT_HASH_BITS
240 ctx.hash_bits = SORT_HASH_BITS;
241 ctx.radix_split = P(radix_split);
242 #elif defined(SORT_INT)
244 while (ctx.hash_bits < 32 && (int_range >> ctx.hash_bits))
246 ctx.radix_split = P(radix_split);
249 ctx.internal_sort = P(internal);
250 ctx.internal_estimate = P(internal_estimate);
251 ctx.twoway_merge = P(twoway_merge);
255 #ifdef SORT_OUTPUT_FILE
256 if (rename(ctx.out_fb->name, out) < 0)
257 die("Cannot rename %s to %s: %m", ctx.out_fb->name, out);
258 bconfig(ctx.out_fb, BCONFIG_IS_TEMP_FILE, 0);
267 #undef SORT_KEY_REGULAR
269 #undef SORT_DATA_SIZE
273 #undef SORT_HASH_BITS
275 #undef SORT_INPUT_FILE
277 #undef SORT_INPUT_PRESORT
278 #undef SORT_OUTPUT_FILE
279 #undef SORT_OUTPUT_FB
280 #undef SORT_OUTPUT_THIS_FB
282 #undef SORT_ASSERT_UNIQUE
283 #undef SORT_DELETE_INPUT
287 /* FIXME: Check that we undef everything we should. */