2 * Sherlock Library -- Universal Array Sorter
4 * (c) 2003 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 * This is not a normal header file, it's a generator of sorting
12 * routines. Each time you include it with parameters set in the
13 * corresponding preprocessor macros, it generates an array sorter
14 * with the parameters given.
16 * You might wonder why the heck do we implement our own array sorter
17 * instead of using qsort(). The primary reason is that qsort handles
18 * only continuous arrays, but we need to sort array-like data structures
19 * where the only way to access elements is by using an indexing macro.
20 * Besides that, we are more than 2 times faster.
22 * So much for advocacy, there are the parameters (those marked with [*]
25 * ASORT_PREFIX(x) [*] add a name prefix (used on all global names
26 * defined by the sorter)
27 * ASORT_KEY_TYPE [*] data type of a single array entry key
28 * ASORT_ELT(i) [*] returns the key of i-th element
29 * ASORT_LT(x,y) x < y for ASORT_TYPE (default: "x<y")
30 * ASORT_SWAP(i,j) swap i-th and j-th element (default: assume _ELT
31 * is an l-value and swap just the keys)
32 * ASORT_THRESHOLD threshold for switching between quicksort and insertsort
33 * ASORT_EXTRA_ARGS extra arguments for the sort function (they are always
34 * visible in all the macros supplied above), starts with comma
36 * After including this file, a function ASORT_PREFIX(sort)(uns array_size)
37 * is declared and all parameter macros are automatically undef'd.
41 #define ASORT_LT(x,y) ((x) < (y))
45 #define ASORT_SWAP(i,j) do { ASORT_KEY_TYPE tmp = ASORT_ELT(i); ASORT_ELT(i)=ASORT_ELT(j); ASORT_ELT(j)=tmp; } while (0)
48 #ifndef ASORT_THRESHOLD
49 #define ASORT_THRESHOLD 8 /* Guesswork and experimentation */
52 #ifndef ASORT_EXTRA_ARGS
53 #define ASORT_EXTRA_ARGS
56 static void ASORT_PREFIX(sort)(uns array_size ASORT_EXTRA_ARGS)
58 struct stk { int l, r; } stack[8*sizeof(uns)];
59 int l, r, left, right, m;
66 /* QuickSort with optimizations a'la Sedgewick, but stop at ASORT_THRESHOLD */
69 right = array_size - 1;
75 if (ASORT_LT(ASORT_ELT(m), ASORT_ELT(l)))
77 if (ASORT_LT(ASORT_ELT(r), ASORT_ELT(m)))
80 if (ASORT_LT(ASORT_ELT(m), ASORT_ELT(l)))
86 while (ASORT_LT(ASORT_ELT(l), pivot))
88 while (ASORT_LT(pivot, ASORT_ELT(r)))
103 if ((r - left) >= ASORT_THRESHOLD && (right - l) >= ASORT_THRESHOLD)
105 /* Both partitions ok => push the larger one */
106 if ((r - left) > (right - l))
120 else if ((r - left) >= ASORT_THRESHOLD)
122 /* Left partition OK, right undersize */
125 else if ((right - l) >= ASORT_THRESHOLD)
127 /* Right partition OK, left undersize */
132 /* Both partitions undersize => pop */
142 * We have a partially sorted array, finish by insertsort. Inspired
143 * by qsort() in GNU libc.
146 /* Find minimal element which will serve as a barrier */
147 r = MIN(array_size, ASORT_THRESHOLD);
150 if (ASORT_LT(ASORT_ELT(l),ASORT_ELT(m)))
155 for (m=1; m<(int)array_size; m++)
158 while (ASORT_LT(ASORT_ELT(m),ASORT_ELT(l-1)))
173 #undef ASORT_THRESHOLD
174 #undef ASORT_EXTRA_ARGS