]> mj.ucw.cz Git - libucw.git/commitdiff
Added s-fixint module (internal sorter for small fixed-size records).
authorMartin Mares <mj@ucw.cz>
Sat, 10 Feb 2007 13:14:50 +0000 (14:14 +0100)
committerMartin Mares <mj@ucw.cz>
Sat, 10 Feb 2007 13:14:50 +0000 (14:14 +0100)
lib/sorter/s-fixint.h [new file with mode: 0644]
lib/sorter/s-internal.h
lib/sorter/sorter.h

diff --git a/lib/sorter/s-fixint.h b/lib/sorter/s-fixint.h
new file mode 100644 (file)
index 0000000..a36be55
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ *     UCW Library -- Universal Sorter: Fixed-Size Internal Sorting Module
+ *
+ *     (c) 2007 Martin Mares <mj@ucw.cz>
+ *
+ *     This software may be freely distributed and used according to the terms
+ *     of the GNU Lesser General Public License.
+ */
+
+#define ASORT_PREFIX(x) SORT_PREFIX(array_##x)
+#define ASORT_KEY_TYPE P(key)
+#define ASORT_ELT(i) ary[i]
+#define ASORT_LT(x,y) (P(compare)(&(x), &(y)) < 0)
+#define ASORT_EXTRA_ARGS , P(key) *ary
+#include "lib/arraysort.h"
+
+static int P(internal)(struct sort_context *ctx, struct sort_bucket *bin, struct sort_bucket *bout, struct sort_bucket *bout_only)
+{
+  sorter_alloc_buf(ctx);
+  struct fastbuf *in = sbuck_read(bin);
+  P(key) *buf = ctx->big_buf;
+  size_t bufsize = ctx->big_buf_half_size;     /* FIXME: In some cases, we can use the whole buffer */
+#ifdef CPU_64BIT_POINTERS
+  bufsize = MIN((u64)bufsize, (u64)~0U * sizeof(P(key)));      // The number of records must fit in uns
+#endif
+  uns maxkeys = bufsize / sizeof(P(key));
+
+  SORT_XTRACE(3, "s-fixint: Reading (maxkeys=%u)", maxkeys);
+  uns n = 0;
+  while (n < maxkeys && P(read_key)(in, &buf[n]))
+    n++;
+
+  SORT_XTRACE(3, "s-fixint: Sorting %u items", n);
+  P(array_sort)(n, buf);
+
+  SORT_XTRACE(3, "s-fixint: Writing");
+  struct fastbuf *out = sbuck_write((n < maxkeys) ? bout_only : bout);
+  bout->runs++;
+  uns merged UNUSED = 0;
+  for (uns i=0; i<n; i++)
+    {
+#ifdef SORT_UNIFY
+      if (i < n-1 && !P(compare)(&buf[i], &buf[i+1]))
+       {
+         ASSERT(0);                    /* FIXME: Implement */
+         continue;
+       }
+#endif
+#ifdef SORT_ASSERT_UNIQUE
+      ASSERT(i == n-1 || P(compare)(&buf[i], &buf[i+1]) < 0);
+#endif
+      P(write_key)(out, &buf[i]);
+    }
+#ifdef SORT_UNIFY
+  SORT_XTRACE(3, "Merging reduced %d records", merged);
+#endif
+
+  return (n == maxkeys);
+}
index 665234fea2ba9168f3f04af9a568c1b837e2dbbf..a3c38128a850419e5625905294f1fa53345c524d 100644 (file)
@@ -91,7 +91,7 @@ static int P(internal)(struct sort_context *ctx, struct sort_bucket *bin, struct
   last_item = item;
 
   uns count = last_item - item_array;
-  SORT_XTRACE(3, "s-internal: Sorting %d items", count);
+  SORT_XTRACE(3, "s-internal: Sorting %u items", count);
   P(array_sort)(count, item_array);
 
   SORT_XTRACE(3, "s-internal: Writing");
@@ -132,7 +132,7 @@ static int P(internal)(struct sort_context *ctx, struct sort_bucket *bin, struct
 #endif
     }
 #ifdef SORT_UNIFY
-  SORT_XTRACE(3, "Merging reduced %d records", merged);
+  SORT_XTRACE(3, "Merging reduced %u records", merged);
 #endif
 
   return ctx->more_keys;
index 98dd8f776e3aa0bacc0e4423217c26a440ec328a..39bdcb97a7a2d3cf7a995f2da43de4a830e318d3 100644 (file)
@@ -172,7 +172,12 @@ static inline void P(copy_data)(P(key) *key, struct fastbuf *in, struct fastbuf
 #endif
 }
 
+#if defined(SORT_VAR_KEY) || defined(SORT_VAR_DATA) || defined(SORT_UNIFY)
 #include "lib/sorter/s-internal.h"
+#else
+#include "lib/sorter/s-fixint.h"
+#endif
+
 #include "lib/sorter/s-twoway.h"
 
 #if defined(SORT_HASH_BITS) || defined(SORT_INT)