]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/sorter/array-simple.h
Opt: Added OPT_HELP_COLUMNS
[libucw.git] / ucw / sorter / array-simple.h
index d5a9c787d3279643f7df1c5edfb3a52f95fee29a..aa9b7806e49fdb49ab8cad3ed4d58fecfcc6d127 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.
  *  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_LT(x,y)      x < y for ASORT_TYPE (default: "x<y")
+ *  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_KEY_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)
  *  ASORT_THRESHOLD    threshold for switching between quicksort and insertsort
  *  ASORT_EXTRA_ARGS   extra arguments for the sort function (they are always
  *                     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.
+ *  After including this file, a function ASORT_PREFIX(sort)(uint array_size)
+ *  or ASORT_PREFIX(sort)(ASORT_KEY_TYPE *array, uint 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 ASORT_KEY_TYPE *array,
+#define ASORT_ELT(i) array[i]
+#else
+#define ASORT_ARRAY_ARG
+#endif
+
+/**
+ * The generated sorting function. If `ASORT_ELT` macro is not provided, the
+ * @ASORT_ARRAY_ARG is equal to `ASORT_KEY_TYPE *array` and is the array to be
+ * sorted. If the macro is provided, this parameter is omitted. In that case,
+ * you can sort global variables or pass your structure by @ASORT_EXTRA_ARGS.
+ **/
+static void ASORT_PREFIX(sort)(ASORT_ARRAY_ARG uint array_size ASORT_EXTRA_ARGS)
 {
-  struct stk { int l, r; } stack[8*sizeof(uns)];
+  struct stk { int l, r; } stack[8*sizeof(uint)];
   int l, r, left, right, m;
-  uns sp = 0;
+  uint sp = 0;
   ASORT_KEY_TYPE pivot;
 
   if (array_size <= 1)
@@ -172,3 +188,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