]> mj.ucw.cz Git - libucw.git/blobdiff - lib/arraysort.h
Merged obj2buck.h and buck2obj.h to object.h, the number of includes
[libucw.git] / lib / arraysort.h
index 316bcd20edf5f196cf57724d4914bcb6a9828f06..b47fa8c38ac5287142608e50b7ea713f43da8994 100644 (file)
  *  ASORT_ELT(i)    [*]        returns the key of i-th element
  *  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 a l-value and swap just the keys)
+ *                     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.
 #define ASORT_THRESHOLD 8              /* Guesswork and experimentation */
 #endif
 
-static void ASORT_PREFIX(sort)(uns array_size)
+#ifndef ASORT_EXTRA_ARGS
+#define ASORT_EXTRA_ARGS
+#endif
+
+static void ASORT_PREFIX(sort)(uns array_size ASORT_EXTRA_ARGS)
 {
   struct stk { int l, r; } stack[8*sizeof(uns)];
   int l, r, left, right, m;
@@ -94,7 +100,7 @@ static void ASORT_PREFIX(sort)(uns array_size)
            }
        }
       while (l <= r);
-      if ((r - left) > ASORT_THRESHOLD && (right - l) > ASORT_THRESHOLD)
+      if ((r - left) >= ASORT_THRESHOLD && (right - l) >= ASORT_THRESHOLD)
        {
          /* Both partitions ok => push the larger one */
          if ((r - left) > (right - l))
@@ -111,12 +117,12 @@ static void ASORT_PREFIX(sort)(uns array_size)
            }
          sp++;
        }
-      else if ((r - left) > ASORT_THRESHOLD)
+      else if ((r - left) >= ASORT_THRESHOLD)
        {
          /* Left partition OK, right undersize */
          right = r;
        }
-      else if ((right - l) > ASORT_THRESHOLD)
+      else if ((right - l) >= ASORT_THRESHOLD)
        {
          /* Right partition OK, left undersize */
          left = l;
@@ -160,8 +166,9 @@ static void ASORT_PREFIX(sort)(uns array_size)
 }
 
 #undef ASORT_PREFIX
-#undef ASORT_TYPE
+#undef ASORT_KEY_TYPE
 #undef ASORT_ELT
-#undef ASORT_EQ
+#undef ASORT_LT
 #undef ASORT_SWAP
 #undef ASORT_THRESHOLD
+#undef ASORT_EXTRA_ARGS