]> mj.ucw.cz Git - libucw.git/commitdiff
Added SORT_INT64 mode.
authorMartin Mares <mj@ucw.cz>
Wed, 23 May 2007 20:59:11 +0000 (22:59 +0200)
committerMartin Mares <mj@ucw.cz>
Wed, 23 May 2007 20:59:11 +0000 (22:59 +0200)
lib/sorter/govern.c
lib/sorter/s-radix.h
lib/sorter/sort-test.c
lib/sorter/sorter.h

index 52d90e96dc7aacd8c7167d5acbb850678e0562c4..6b4dc696ca06de80d4bce4e3c6067f99133fa647 100644 (file)
@@ -251,7 +251,7 @@ sorter_run(struct sort_context *ctx)
   bin->size = ctx->in_size;
   bin->hash_bits = ctx->hash_bits;
   clist_add_tail(&ctx->bucket_list, &bin->n);
-  SORT_XTRACE(2, "Input size: %s", F_BSIZE(bin));
+  SORT_XTRACE(2, "Input size: %s, %d hash bits", F_BSIZE(bin), bin->hash_bits);
 
   // Create bucket for the output
   struct sort_bucket *bout = sbuck_new(ctx);
index 0b5f5e9ff2be852991c1c61c5b2e75064868d1c0..289f255d69b96996d21612b050bb9f0cb621b617 100644 (file)
@@ -21,7 +21,7 @@ static void P(radix_split)(struct sort_context *ctx UNUSED, struct sort_bucket *
 
   while (P(read_key)(in, &k))
     {
-      uns h = P(hash)(&k);
+      P(hash_t) h = P(hash)(&k);
       uns i = (h >> bitpos) & mask;
       if (unlikely(!outs[i]))
        outs[i] = sbuck_write(bouts[i]);
index 9a822d141847037182f8a40cf3553bd8a16d8904..34a74c723a2be2df304e48b3c0fb4b81d2ba1bc1 100644 (file)
@@ -560,6 +560,47 @@ test_graph(uns mode, u64 size)
   bclose(f);
 }
 
+/*** Simple 8-byte integer keys ***/
+
+struct key6 {
+  u64 x;
+};
+
+#define SORT_KEY_REGULAR struct key6
+#define SORT_PREFIX(x) s6_##x
+#define SORT_INPUT_FB
+#define SORT_OUTPUT_FB
+#define SORT_UNIQUE
+#define SORT_INT64(k) (k).x
+
+#include "lib/sorter/sorter.h"
+
+static void
+test_int64(int mode, u64 size)
+{
+  u64 N = size ? nextprime(MIN(size/8, 0xffff0000)) : 0;
+  u64 K = N/4*3;
+  log(L_INFO, ">>> 64-bit integers (%s, N=%llu)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
+
+  struct fastbuf *f = bopen_tmp(65536);
+  for (u64 i=0; i<N; i++)
+    bputq(f, 777777*((mode==0) ? i : (mode==1) ? N-1-i : ((u64)i * K + 17) % N));
+  brewind(f);
+
+  start();
+  f = s6_sort(f, NULL, 777777*(N-1));
+  stop();
+
+  SORT_XTRACE(2, "Verifying");
+  for (u64 i=0; i<N; i++)
+    {
+      u64 j = bgetq(f);
+      if (777777*i != j)
+       die("Discrepancy: %llu instead of %llu", j, 777777*i);
+    }
+  bclose(f);
+}
+
 /*** Main ***/
 
 static void
@@ -593,7 +634,13 @@ run_test(uns i, u64 size)
       test_graph(0, size); break;
     case 12:
       test_graph(1, size); break;
-#define TMAX 13
+    case 13:
+      test_int64(0, size); break;
+    case 14:
+      test_int64(1, size); break;
+    case 15:
+      test_int64(2, size); break;
+#define TMAX 16
     }
 }
 
index 2b2357cbcc4fd188f819c8d8a07f1cf4b1b8681d..83d3e7dcacf32203528e634190914dd69780354b 100644 (file)
@@ -46,6 +46,7 @@
  *                     is supplied automatically and the sorting function gets an extra
  *                     parameter specifying a range of the integers. The better the range
  *                     fits, the faster we sort. Sets up SORT_HASH_xxx automatically.
+ *  SORT_INT64(key)    the same for 64-bit integers.
  *
  *  Hashing (optional, but it can speed sorting up):
  *
@@ -127,6 +128,13 @@ typedef SORT_KEY P(key);
 #error Missing definition of sorting key.
 #endif
 
+#ifdef SORT_INT64
+typedef u64 P(hash_t);
+#define SORT_INT SORT_INT64
+#else
+typedef uns P(hash_t);
+#endif
+
 #ifdef SORT_INT
 static inline int P(compare) (P(key) *x, P(key) *y)
 {
@@ -138,7 +146,7 @@ static inline int P(compare) (P(key) *x, P(key) *y)
 }
 
 #ifndef SORT_HASH_BITS
-static inline int P(hash) (P(key) *x)
+static inline P(hash_t) P(hash) (P(key) *x)
 {
   return SORT_INT((*x));
 }
@@ -209,7 +217,7 @@ static struct fastbuf *P(sort)(
                               struct fastbuf *out
 #endif
 #ifdef SORT_INT
-                              , uns int_range
+                              , u64 int_range
 #endif
                               )
 {
@@ -252,7 +260,7 @@ static struct fastbuf *P(sort)(
   ctx.radix_split = P(radix_split);
 #elif defined(SORT_INT)
   ctx.hash_bits = 0;
-  while (ctx.hash_bits < 32 && (int_range >> ctx.hash_bits))
+  while (ctx.hash_bits < 64 && (int_range >> ctx.hash_bits))
     ctx.hash_bits++;
   ctx.radix_split = P(radix_split);
 #endif
@@ -281,6 +289,7 @@ static struct fastbuf *P(sort)(
 #undef SORT_VAR_KEY
 #undef SORT_VAR_DATA
 #undef SORT_INT
+#undef SORT_INT64
 #undef SORT_HASH_BITS
 #undef SORT_UNIFY
 #undef SORT_UNIFY_WORKSPACE