1 /* Isaac Turner 29 April 2014 Public Domain */
10 sort_r function to be exported.
13 base is the array to be sorted
14 nel is the number of elements in the array
15 width is the size in bytes of each element of the array
16 compar is the comparison function
17 arg is a pointer to be passed to the comparison function
19 void sort_r(void *base, size_t nel, size_t width,
20 int (*compar)(const void *_a, const void *_b, void *_arg),
25 #define _SORT_R_INLINE inline
27 #if (defined HAVE_GNU_QSORT_R)
28 # define _SORT_R_LINUX
29 #elif (defined HAVE_BSD_QSORT_R)
31 #elif (defined __gnu_hurd__ || defined __GNU__ || \
32 defined __linux__ || defined __MINGW32__ || defined __GLIBC__)
33 # define _SORT_R_LINUX
34 #elif (defined __APPLE__ || defined __MACH__ || defined __DARWIN__ || \
35 defined __FreeBSD__ || defined __DragonFly__)
37 #elif (defined _WIN32 || defined _WIN64 || defined __WINDOWS__)
38 # define _SORT_R_WINDOWS
39 # undef _SORT_R_INLINE
40 # define _SORT_R_INLINE __inline
42 /* Using our own recursive quicksort sort_r_simple() */
45 #if (defined NESTED_QSORT && NESTED_QSORT == 0)
49 #define SORT_R_SWAP(a,b,tmp) ((tmp) = (a), (a) = (b), (b) = (tmp))
52 /* a and b must not be equal! */
53 static _SORT_R_INLINE void sort_r_swap(char *__restrict a, char *__restrict b,
57 for(; a < end; a++, b++) { SORT_R_SWAP(*a, *b, tmp); }
60 /* swap a, b iff a>b */
61 /* a and b must not be equal! */
62 /* __restrict is same as restrict but better support on old machines */
63 static _SORT_R_INLINE int sort_r_cmpswap(char *__restrict a,
64 char *__restrict b, size_t w,
65 int (*compar)(const void *_a,
70 if(compar(a, b, arg) > 0) {
78 Swap consecutive blocks of bytes of size na and nb starting at memory addr ptr,
79 with the smallest swap so that the blocks are in the opposite order. Blocks may
80 be internally re-ordered e.g.
86 static _SORT_R_INLINE void sort_r_swap_blocks(char *ptr, size_t na, size_t nb)
88 if(na > 0 && nb > 0) {
89 if(na > nb) { sort_r_swap(ptr, ptr+na, nb); }
90 else { sort_r_swap(ptr, ptr+nb, na); }
94 /* Implement recursive quicksort ourselves */
95 /* Note: quicksort is not stable, equivalent values may be swapped */
96 static _SORT_R_INLINE void sort_r_simple(void *base, size_t nel, size_t w,
97 int (*compar)(const void *_a,
102 char *b = (char *)base, *end = b + nel*w;
104 /* for(size_t i=0; i<nel; i++) {printf("%4i", *(int*)(b + i*sizeof(int)));}
108 /* Insertion sort for arbitrarily small inputs */
110 for(pi = b+w; pi < end; pi += w) {
111 for(pj = pi; pj > b && sort_r_cmpswap(pj-w,pj,w,compar,arg); pj -= w) {}
116 /* nel > 6; Quicksort */
119 char *pl, *ple, *pr, *pre, *pivot;
120 char *last = b+w*(nel-1), *tmp;
123 Use median of second, middle and second-last items as pivot.
124 First and last may have been swapped with pivot and therefore be extreme
131 /* printf("pivots: %i, %i, %i\n", *(int*)l[0], *(int*)l[1], *(int*)l[2]); */
133 if(compar(l[0],l[1],arg) > 0) { SORT_R_SWAP(l[0], l[1], tmp); }
134 if(compar(l[1],l[2],arg) > 0) {
135 SORT_R_SWAP(l[1], l[2], tmp);
136 if(compar(l[0],l[1],arg) > 0) { SORT_R_SWAP(l[0], l[1], tmp); }
139 /* swap mid value (l[1]), and last element to put pivot as last element */
140 if(l[1] != last) { sort_r_swap(l[1], last, w); }
143 pl is the next item on the left to be compared to the pivot
144 pr is the last item on the right that was compared to the pivot
145 ple is the left position to put the next item that equals the pivot
146 ple is the last right position where we put an item that equals the pivot
148 v- end (beyond the array)
149 EEEEEELLLLLLLLuuuuuuuuGGGGGGGEEEEEEEE.
150 ^- b ^- ple ^- pl ^- pr ^- pre ^- last (where the pivot is)
152 Pivot comparison key:
153 E = equal, L = less than, u = unknown, G = greater than, E = equal
161 Loop into the list from the left and right at the same time to find:
162 - an item on the left that is greater than the pivot
163 - an item on the right that is less than the pivot
164 Once found, they are swapped and the loop continues.
165 Meanwhile items that are equal to the pivot are moved to the edges of the
169 /* Move left hand items which are equal to the pivot to the far left.
170 break when we find an item that is greater than the pivot */
171 for(; pl < pr; pl += w) {
172 cmp = compar(pl, pivot, arg);
173 if(cmp > 0) { break; }
175 if(ple < pl) { sort_r_swap(ple, pl, w); }
179 /* break if last batch of left hand items were equal to pivot */
180 if(pl >= pr) { break; }
181 /* Move right hand items which are equal to the pivot to the far right.
182 break when we find an item that is less than the pivot */
184 pr -= w; /* Move right pointer onto an unprocessed item */
185 cmp = compar(pr, pivot, arg);
188 if(pr < pre) { sort_r_swap(pr, pre, w); }
191 if(pl < pr) { sort_r_swap(pl, pr, w); }
198 pl = pr; /* pr may have gone below pl */
201 Now we need to go from: EEELLLGGGGEEEE
204 Pivot comparison key:
205 E = equal, L = less than, u = unknown, G = greater than, E = equal
207 sort_r_swap_blocks(b, ple-b, pl-ple);
208 sort_r_swap_blocks(pr, pre-pr, end-pre);
210 /*for(size_t i=0; i<nel; i++) {printf("%4i", *(int*)(b + i*sizeof(int)));}
213 sort_r_simple(b, (pl-ple)/w, w, compar, arg);
214 sort_r_simple(end-(pre-pr), (pre-pr)/w, w, compar, arg);
219 #if defined NESTED_QSORT
221 static _SORT_R_INLINE void sort_r(void *base, size_t nel, size_t width,
222 int (*compar)(const void *_a,
227 int nested_cmp(const void *a, const void *b)
229 return compar(a, b, arg);
232 qsort(base, nel, width, nested_cmp);
235 #else /* !NESTED_QSORT */
237 /* Declare structs and functions */
239 #if defined _SORT_R_BSD
241 /* Ensure qsort_r is defined */
242 extern void qsort_r(void *base, size_t nel, size_t width, void *thunk,
243 int (*compar)(void *_thunk,
244 const void *_a, const void *_b));
248 #if defined _SORT_R_BSD || defined _SORT_R_WINDOWS
250 /* BSD (qsort_r), Windows (qsort_s) require argument swap */
255 int (*compar)(const void *_a, const void *_b, void *_arg);
258 static _SORT_R_INLINE int sort_r_arg_swap(void *s,
259 const void *a, const void *b)
261 struct sort_r_data *ss = (struct sort_r_data*)s;
262 return (ss->compar)(a, b, ss->arg);
267 #if defined _SORT_R_LINUX
269 typedef int(* __compar_d_fn_t)(const void *, const void *, void *);
270 extern void qsort_r(void *base, size_t nel, size_t width,
271 __compar_d_fn_t __compar, void *arg)
272 __attribute__((nonnull (1, 4)));
278 static _SORT_R_INLINE void sort_r(void *base, size_t nel, size_t width,
279 int (*compar)(const void *_a,
280 const void *_b, void *_arg),
283 #if defined _SORT_R_LINUX
285 #if defined __GLIBC__ && ((__GLIBC__ < 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 8))
287 /* no qsort_r in glibc before 2.8, need to use nested qsort */
288 sort_r_simple(base, nel, width, compar, arg);
292 qsort_r(base, nel, width, compar, arg);
296 #elif defined _SORT_R_BSD
298 struct sort_r_data tmp;
301 qsort_r(base, nel, width, &tmp, sort_r_arg_swap);
303 #elif defined _SORT_R_WINDOWS
305 struct sort_r_data tmp;
308 qsort_s(base, nel, width, sort_r_arg_swap, &tmp);
312 /* Fall back to our own quicksort implementation */
313 sort_r_simple(base, nel, width, compar, arg);
318 #endif /* !NESTED_QSORT */
320 #undef _SORT_R_INLINE
321 #undef _SORT_R_WINDOWS
325 #endif /* SORT_R_H_ */