2 * UCW Library -- Universal Sorter: Governing Routines
4 * (c) 2007 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 #include "lib/fastbuf.h"
12 #include "lib/mempool.h"
13 #include "lib/sorter/common.h"
16 sorter_new_bucket(struct sort_context *ctx)
18 return mp_alloc_zero(ctx->pool, sizeof(struct sort_bucket));
22 sorter_open_read(struct sort_bucket *b)
24 /* FIXME: These functions should handle buckets with no fb and only name. */
30 sorter_open_write(struct sort_bucket *b)
33 b->fb = bopen_tmp(sorter_stream_bufsize);
38 sorter_close_read(struct sort_bucket *b)
48 sorter_close_write(struct sort_bucket *b)
52 b->size = btell(b->fb);
55 /* FIXME: Remove empty buckets from the list automatically? */
59 sorter_run(struct sort_context *ctx)
61 ctx->pool = mp_new(4096);
62 ASSERT(!ctx->custom_presort);
64 clist_init(&ctx->bucket_list);
66 /* FIXME: There should be a way how to detect size of the input file */
68 /* Trivial 2-way merge with no presorting (just a testing hack) */
69 struct sort_bucket *bin = sorter_new_bucket(ctx);
70 bin->flags = SBF_SOURCE;
73 struct sort_bucket *ins[3], *outs[3];
78 outs[0] = sorter_new_bucket(ctx);
79 outs[1] = sorter_new_bucket(ctx);
81 log(L_DEBUG, "Pass...");
82 ctx->twoway_merge(ctx, ins, outs);
83 log(L_DEBUG, "Done (%d+%d runs)", outs[0]->runs, outs[1]->runs);
84 sorter_close_write(outs[0]);
85 sorter_close_write(outs[1]);
86 memcpy(ins, outs, 3*sizeof(struct sort_bucket *));
89 ctx->out_fb = sorter_open_read(ins[0]);