/*
- * 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.
* 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)
* 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;
#undef ASORT_SWAP
#undef ASORT_THRESHOLD
#undef ASORT_EXTRA_ARGS
+#undef ASORT_ARRAY_ARG
#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)
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);