X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Fsorter%2Fsort-test.c;h=5464f744fd1eb226d2af7df4a82d8b43805021fb;hb=332c927b8301d01c2c2d8f047a091a4396a6bc82;hp=814a4ad6b7829812af392b8ee7b773b3e57153c0;hpb=889bb33b738e79732f3fb1ca79b361ac0375aa76;p=libucw.git diff --git a/lib/sorter/sort-test.c b/lib/sorter/sort-test.c index 814a4ad6..5464f744 100644 --- a/lib/sorter/sort-test.c +++ b/lib/sorter/sort-test.c @@ -18,18 +18,21 @@ #include #include #include +#include /*** Time measurement ***/ static void start(void) { + sync(); init_timer(); } static void stop(void) { + sync(); log(L_INFO, "Test took %.3fs", get_timer() / 1000.); } @@ -51,9 +54,9 @@ struct key1 { static void test_int(int mode, u64 size) { - uns N = nextprime(MIN(size/4, 0xffff0000)); + uns N = size ? nextprime(MIN(size/4, 0xffff0000)) : 0; uns K = N/4*3; - log(L_INFO, ">>> Integers (%s, N=%d)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N); + log(L_INFO, ">>> Integers (%s, N=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N); struct fastbuf *f = bopen_tmp(65536); for (uns i=0; i 0xffff0000) mult++; - uns N = nextprime(items/(2*mult)); + uns N = items ? nextprime(items/(2*mult)) : 0; uns K = N/4*3; - log(L_INFO, ">>> Counted integers (%s, N=%d, mult=%d)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N, mult); + log(L_INFO, ">>> Counted integers (%s, N=%u, mult=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N, mult); struct fastbuf *f = bopen_tmp(65536); for (uns m=0; m>> Hashes (%s, N=%d)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N); + log(L_INFO, ">>> Hashes (%s, N=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N); struct key3 k, lastk; struct fastbuf *f = bopen_tmp(65536); @@ -323,7 +326,7 @@ test_strings(uns mode, u64 size) { uns avg_item_size = KEY4_MAX/2 + 4 + (mode ? 128 : 0); uns N = MIN(size / avg_item_size, 0xffffffff); - log(L_INFO, ">>> Strings %s(N=%d)", (mode ? "with data " : ""), N); + log(L_INFO, ">>> Strings %s(N=%u)", (mode ? "with data " : ""), N); srand(1); struct key4 k, lastk; @@ -371,6 +374,196 @@ test_strings(uns mode, u64 size) bclose(f); } +/*** Graph-like structure with custom presorting ***/ + +struct key5 { + u32 x; + u32 cnt; +}; + +static uns s5_N, s5_K, s5_L, s5_i, s5_j; + +struct s5_pair { + uns x, y; +}; + +static int s5_gen(struct s5_pair *p) +{ + if (s5_j >= s5_N) + { + if (s5_i >= s5_N-1) + return 0; + s5_j = 0; + s5_i++; + } + p->x = ((u64)s5_j * s5_K) % s5_N; + p->y = ((u64)(s5_i + s5_j) * s5_L) % s5_N; + s5_j++; + return 1; +} + +#define ASORT_PREFIX(x) s5m_##x +#define ASORT_KEY_TYPE u32 +#define ASORT_ELT(i) ary[i] +#define ASORT_EXTRA_ARGS , u32 *ary +#include "lib/arraysort.h" + +static void s5_write_merged(struct fastbuf *f, struct key5 **keys, void **data, uns n, void *buf) +{ + /* FIXME: Allow mode where this function is not defined? */ + u32 *a = buf; + uns m = 0; + for (uns i=0; icnt); + m += keys[i]->cnt; + } + s5m_sort(m, a); + keys[0]->cnt = m; + bwrite(f, keys[0], sizeof(struct key5)); + bwrite(f, a, 4*m); /* FIXME: Might overflow here */ +} + +static void s5_copy_merged(struct key5 **keys, struct fastbuf **data, uns n, struct fastbuf *dest) +{ + u32 k[n]; + uns m = 0; + for (uns i=0; icnt; + } + struct key5 key = { .x = keys[0]->x, .cnt = m }; + bwrite(dest, &key, sizeof(key)); + while (key.cnt--) + { + uns b = 0; + for (uns i=1; icnt) + k[b] = bgetl(data[b]); + else + k[b] = ~0U; + } +} + +static inline int s5p_lt(struct s5_pair x, struct s5_pair y) +{ + COMPARE_LT(x.x, y.x); + COMPARE_LT(x.y, y.y); + return 0; +} + +/* FIXME: Use smarter internal sorter when it's available */ +#define ASORT_PREFIX(x) s5p_##x +#define ASORT_KEY_TYPE struct s5_pair +#define ASORT_ELT(i) ary[i] +#define ASORT_LT(x,y) s5p_lt(x,y) +#define ASORT_EXTRA_ARGS , struct s5_pair *ary +#include "lib/arraysort.h" + +static int s5_presort(struct fastbuf *dest, void *buf, size_t bufsize) +{ + uns max = MIN(bufsize/sizeof(struct s5_pair), 0xffffffff); + struct s5_pair *a = buf; + uns n = 0; + while (n>> Graph%s (N=%u)", (mode ? "" : " with custom presorting"), N); + s5_N = N; + s5_K = N/4*3; + s5_L = N/3*2; + s5_i = s5_j = 0; + + struct fastbuf *in = NULL; + if (mode) + { + struct s5_pair p; + in = bopen_tmp(65536); + while (s5_gen(&p)) + { + struct key5 k = { .x = p.x, .cnt = 1 }; + bwrite(in, &k, sizeof(k)); + bputl(in, p.y); + } + brewind(in); + } + + start(); + struct fastbuf *f = bopen_tmp(65536); + bputl(f, 0xfeedcafe); + struct fastbuf *g = (mode ? s5b_sort(in, f, s5_N-1) : s5_sort(NULL, f, s5_N-1)); + ASSERT(f == g); + stop(); + + SORT_XTRACE(2, "Verifying"); + uns c = bgetl(f); + ASSERT(c == 0xfeedcafe); + for (uns i=0; i= 0) + while ((c = cf_getopt(argc, argv, CF_SHORT_OPTS "d:s:t:v", CF_NO_LONG_OPTS, NULL)) >= 0) switch (c) { + case 'd': + sorter_debug = atol(optarg); + break; case 's': if (cf_parse_u64(optarg, &size)) goto usage; @@ -427,7 +627,7 @@ main(int argc, char **argv) break; default: usage: - fputs("Usage: sort-test [-s ] [-t ]\n", stderr); + fputs("Usage: sort-test [-v] [-d ] [-s ] [-t ]\n", stderr); exit(1); } if (optind != argc)