]> mj.ucw.cz Git - libucw.git/commitdiff
Bring interface of sorter/array-simple.h in line with sorter/array.h.
authorMartin Mares <mj@ucw.cz>
Mon, 3 Nov 2008 22:39:03 +0000 (23:39 +0100)
committerMartin Mares <mj@ucw.cz>
Mon, 3 Nov 2008 22:39:03 +0000 (23:39 +0100)
ASORT_ELT is no longer mandatory; if it is not used, the sorting function
gets a pointer of an array to be sorted as its first argument.

ucw/sorter/array-simple.h
ucw/sorter/sort-test.c

index d5a9c787d3279643f7df1c5edfb3a52f95fee29a..393ebd9315219c78c31e01632fb2e3d34676bce6 100644 (file)
@@ -1,7 +1,7 @@
 /*
- *     UCW Library -- Universal Array Sorter
+ *     UCW Library -- Universal Simple Array Sorter
  *
- *     (c) 2003 Martin Mares <mj@ucw.cz>
+ *     (c) 2003--2008 Martin Mares <mj@ucw.cz>
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
@@ -25,7 +25,8 @@
  *  ASORT_PREFIX(x) [*]        add a name prefix (used on all global names
  *                     defined by the sorter)
  *  ASORT_KEY_TYPE  [*]        data type of a single array entry key
- *  ASORT_ELT(i)    [*]        returns the key of i-th element
+ *  ASORT_ELT(i)       returns the key of i-th element; if this macro is not
+ *                     defined, the function gets a pointer to an array to be sorted
  *  ASORT_LT(x,y)      x < y for ASORT_TYPE (default: "x<y")
  *  ASORT_SWAP(i,j)    swap i-th and j-th element (default: assume _ELT
  *                     is an l-value and swap just the keys)
@@ -34,7 +35,9 @@
  *                     visible in all the macros supplied above), starts with comma
  *
  *  After including this file, a function ASORT_PREFIX(sort)(uns array_size)
- *  is declared and all parameter macros are automatically undef'd.
+ *  or ASORT_PREFIX(sort)(ASORT_KEY_TYPE *array, uns array_size) [if ASORT_ELT
+ *  is not defined] is declared and all parameter macros are automatically
+ *  undef'd.
  */
 
 #ifndef ASORT_LT
 #define ASORT_EXTRA_ARGS
 #endif
 
-static void ASORT_PREFIX(sort)(uns array_size ASORT_EXTRA_ARGS)
+#ifndef ASORT_ELT
+#define ASORT_ARRAY_ARG
+#define ASORT_ELT(i) array[i]
+#endif
+
+static void ASORT_PREFIX(sort)(
+#ifdef ASORT_ARRAY_ARG
+  ASORT_KEY_TYPE *array,
+#endif
+  uns array_size ASORT_EXTRA_ARGS)
 {
   struct stk { int l, r; } stack[8*sizeof(uns)];
   int l, r, left, right, m;
@@ -172,3 +184,4 @@ static void ASORT_PREFIX(sort)(uns array_size ASORT_EXTRA_ARGS)
 #undef ASORT_SWAP
 #undef ASORT_THRESHOLD
 #undef ASORT_EXTRA_ARGS
+#undef ASORT_ARRAY_ARG
index 17c6e7f0d09c72b7aeeed71adf021df7b8d9e06c..fcd782a84f238f85291ac86db325c0d7e02f0827 100644 (file)
@@ -409,8 +409,6 @@ static int s5_gen(struct s5_pair *p)
 
 #define ASORT_PREFIX(x) s5m_##x
 #define ASORT_KEY_TYPE u32
-#define ASORT_ELT(i) ary[i]
-#define ASORT_EXTRA_ARGS , u32 *ary
 #include "ucw/sorter/array-simple.h"
 
 static void s5_write_merged(struct fastbuf *f, struct key5 **keys, void **data, uns n, void *buf)
@@ -422,7 +420,7 @@ static void s5_write_merged(struct fastbuf *f, struct key5 **keys, void **data,
       memcpy(&a[m], data[i], 4*keys[i]->cnt);
       m += keys[i]->cnt;
     }
-  s5m_sort(m, a);
+  s5m_sort(a, m);
   keys[0]->cnt = m;
   bwrite(f, keys[0], sizeof(struct key5));
   bwrite(f, a, 4*m);