]> mj.ucw.cz Git - libucw.git/blob - lib/sorter/sorter.h
fe6cefc11f28b67df6c4d92e9f95704441fccce6
[libucw.git] / lib / sorter / sorter.h
1 /*
2  *      UCW Library -- Universal Sorter
3  *
4  *      (c) 2001--2007 Martin Mares <mj@ucw.cz>
5  *      (c) 2004 Robert Spalek <robert@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 /*
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.
16  *
17  *  FIXME: explain the basics (keys and data, callbacks etc.)
18  *
19  *  Basic parameters and callbacks:
20  *
21  *  SORT_PREFIX(x)      add a name prefix (used on all global names
22  *                      defined by the sorter)
23  *
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.
35  *
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.
42  *
43  *  Integer sorting:
44  *
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.
49  *
50  *  Hashing (optional, but it can speed sorting up):
51  *
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, SORT_KEY *b)
56  *
57  *  Unification:
58  *
59  *  SORT_MERGE          merge items with identical keys, needs the following functions:
60  *  void PREFIX_write_merged(struct fastbuf *f, SORT_KEY **keys, uns n, byte *buf)
61  *                      takes n records in memory with keys which compare equal and writes
62  *                      a single record to the given fastbuf. Data for each key can
63  *                      be accessed by the SORT_GET_DATA(*key) macro. `buf' points
64  *                      to a buffer which is guaranteed to hold all given records.
65  *  void PREFIX_copy_merged(SORT_KEY **keys, struct fastbuf **data, uns n, struct fastbuf *dest)
66  *                      takes n records with keys in memory and data in fastbufs and writes
67  *                      a single record.
68  *
69  *  Input (choose one of these):
70  *
71  *  SORT_INPUT_FILE     file of a given name
72  *  SORT_INPUT_FB       fastbuf stream
73  *  SORT_INPUT_PRESORT  custom presorter: call function PREFIX_presorter (see below)
74  *                      to get successive batches of pre-sorted data as temporary
75  *                      fastbuf streams or NULL if no more data is available.
76  *                      The function is passed a page-aligned presorting buffer.
77  *
78  *  Output (chose one of these):
79  *
80  *  SORT_OUTPUT_FILE    file of a given name
81  *  SORT_OUTPUT_FB      temporary fastbuf stream
82  *  SORT_OUTPUT_THIS_FB a given fastbuf stream which can already contain a header
83  *
84  *  Other switches:
85  *
86  *  SORT_UNIQUE         all items have distinct keys (checked in debug mode)
87  *
88  *  FIXME: Maybe implement these:
89  *  ??? SORT_DELETE_INPUT       a C expression, if true, the input files are
90  *                      deleted as soon as possible
91  *  ??? SORT_ALIGNED
92  *
93  *  The function generated:
94  *
95  *  <outfb> PREFIX_SORT(<in>, <out> [,<range>]), where:
96  *                      <in> = input file name/fastbuf or NULL
97  *                      <out> = output file name/fastbuf or NULL
98  *                      <range> = maximum integer value for the SORT_INT mode
99  *                      <outfb> = output fastbuf (in SORT_OUTPUT_FB mode)
100  *
101  *  After including this file, all parameter macros are automatically
102  *  undef'd.
103  */
104
105 #include "lib/sorter/common.h"
106 #include "lib/fastbuf.h"
107
108 #include <fcntl.h>
109
110 #define P(x) SORT_PREFIX(x)
111
112 #ifdef SORT_KEY_REGULAR
113 typedef SORT_KEY_REGULAR P(key);
114 static inline int P(read_key) (struct fastbuf *f, P(key) *k)
115 {
116   return breadb(f, k, sizeof(P(key)));
117 }
118 static inline void P(write_key) (struct fastbuf *f, P(key) *k)
119 {
120   bwrite(f, k, sizeof(P(key)));
121 }
122 #elif defined(SORT_KEY)
123 typedef SORT_KEY P(key);
124 #else
125 #error Missing definition of sorting key.
126 #endif
127
128 #ifdef SORT_INT
129 static inline int P(compare) (P(key) *x, P(key) *y)
130 {
131   if (SORT_INT(*x) < SORT_INT(*y))
132     return -1;
133   if (SORT_INT(*x) > SORT_INT(*y))
134     return 1;
135   return 0;
136 }
137
138 #ifndef SORT_HASH_BITS
139 static inline int P(hash) (P(key) *x)
140 {
141   return SORT_INT((*x));
142 }
143 #endif
144 #endif
145
146 #ifdef SORT_MERGE
147 #define LESS <
148 #else
149 #define LESS <=
150 #endif
151 #define SWAP(x,y,z) do { z=x; x=y; y=z; } while(0)
152
153 #if defined(SORT_UNIQUE) && defined(DEBUG_ASSERTS)
154 #define SORT_ASSERT_UNIQUE
155 #endif
156
157 static inline void P(copy_data)(P(key) *key, struct fastbuf *in, struct fastbuf *out)
158 {
159   bwrite(out, key, sizeof(P(key)));
160 #ifdef SORT_DATA_SIZE
161   bbcopy(in, out, SORT_DATA_SIZE(*key));
162 #else
163   (void) in;
164 #endif
165 }
166
167 #include "lib/sorter/s-internal.h"
168 #include "lib/sorter/s-twoway.h"
169
170 static struct fastbuf *P(sort)(
171 #ifdef SORT_INPUT_FILE
172                                byte *in,
173 #else
174                                struct fastbuf *in,
175 #endif
176 #ifdef SORT_OUTPUT_FILE
177                                byte *out
178 #else
179                                struct fastbuf *out
180 #endif
181 #ifdef SORT_INT
182                                , uns int_range
183 #endif
184                                )
185 {
186   struct sort_context ctx;
187   bzero(&ctx, sizeof(ctx));
188
189 #ifdef SORT_INPUT_FILE
190   ctx.in_fb = bopen(in, O_RDONLY, sorter_stream_bufsize);
191 #elif defined(SORT_INPUT_FB)
192   ctx.in_fb = in;
193 #elif defined(SORT_INPUT_PRESORT)
194   ASSERT(!in);
195   ctx.custom_presort = P(presorter);
196 #else
197 #error No input given.
198 #endif
199
200 #ifdef SORT_OUTPUT_FB
201   ASSERT(!out);
202 #elif defined(SORT_OUTPUT_THIS_FB)
203   ctx.out_fb = out;
204 #elif defined(SORT_OUTPUT_FILE)
205   /* Just assume fastbuf output and rename the fastbuf later */
206 #else
207 #error No output given.
208 #endif
209
210 #ifdef SORT_HASH_BITS
211   ctx.hash_bits = SORT_HASH_BITS;
212 #elif defined(SORT_INT)
213   ctx.hash_bits = 0;
214   while (ctx.hash_bits < 32 && (int_range >> ctx.hash_bits))
215     ctx.hash_bits++;
216 #endif
217
218   ctx.internal_sort = P(internal);
219   ctx.twoway_merge = P(twoway_merge);
220
221   sorter_run(&ctx);
222
223 #ifdef SORT_OUTPUT_FILE
224   if (rename(ctx.out_fb->name, out) < 0)
225     die("Cannot rename %s to %s: %m", ctx.out_fb->name, out);
226   bconfig(ctx.out_fb, BCONFIG_IS_TEMP_FILE, 0);
227   bclose(ctx.out_fb);
228   ctx.out_fb = NULL;
229 #endif
230   return ctx.out_fb;
231 }
232
233 #undef SORT_KEY
234 #undef SORT_KEY_REGULAR
235 #undef SORT_KEY_SIZE
236 #undef SORT_DATA_SIZE
237 #undef SORT_INT
238 #undef SORT_HASH_BITS
239 #undef SORT_MERGE
240 #undef SORT_INPUT_FILE
241 #undef SORT_INPUT_FB
242 #undef SORT_INPUT_PRESORT
243 #undef SORT_OUTPUT_FILE
244 #undef SORT_OUTPUT_FB
245 #undef SORT_OUTPUT_THIS_FB
246 #undef SORT_UNIQUE
247 #undef SORT_ASSERT_UNIQUE
248 #undef SWAP
249 #undef LESS
250 #undef P
251 /* FIXME: Check that we undef everything we should. */