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 _GNU_SOURCE || defined __gnu_hurd__ || defined __GNU__ || \
28 defined __linux__ || defined __MINGW32__ || defined __GLIBC__)
29 # define _SORT_R_LINUX
30 #elif (defined __APPLE__ || defined __MACH__ || defined __DARWIN__ || \
31 defined __FreeBSD__ || defined __DragonFly__)
33 #elif (defined _WIN32 || defined _WIN64 || defined __WINDOWS__)
34 # define _SORT_R_WINDOWS
35 # undef _SORT_R_INLINE
36 # define _SORT_R_INLINE __inline
38 /* Using our own recursive quicksort sort_r_simple() */
41 #if (defined NESTED_QSORT && NESTED_QSORT == 0)
45 #define SORT_R_SWAP(a,b,tmp) ((tmp) = (a), (a) = (b), (b) = (tmp))
48 /* a and b must not be equal! */
49 static _SORT_R_INLINE void sort_r_swap(char *__restrict a, char *__restrict b,
53 for(; a < end; a++, b++) { SORT_R_SWAP(*a, *b, tmp); }
56 /* swap a, b iff a>b */
57 /* a and b must not be equal! */
58 /* __restrict is same as restrict but better support on old machines */
59 static _SORT_R_INLINE int sort_r_cmpswap(char *__restrict a,
60 char *__restrict b, size_t w,
61 int (*compar)(const void *_a,
66 if(compar(a, b, arg) > 0) {
74 Swap consecutive blocks of bytes of size na and nb starting at memory addr ptr,
75 with the smallest swap so that the blocks are in the opposite order. Blocks may
76 be internally re-ordered e.g.
82 static _SORT_R_INLINE void sort_r_swap_blocks(char *ptr, size_t na, size_t nb)
84 if(na > 0 && nb > 0) {
85 if(na > nb) { sort_r_swap(ptr, ptr+na, nb); }
86 else { sort_r_swap(ptr, ptr+nb, na); }
90 /* Implement recursive quicksort ourselves */
91 /* Note: quicksort is not stable, equivalent values may be swapped */
92 static _SORT_R_INLINE void sort_r_simple(void *base, size_t nel, size_t w,
93 int (*compar)(const void *_a,
98 char *b = (char *)base, *end = b + nel*w;
100 /* for(size_t i=0; i<nel; i++) {printf("%4i", *(int*)(b + i*sizeof(int)));}
104 /* Insertion sort for arbitrarily small inputs */
106 for(pi = b+w; pi < end; pi += w) {
107 for(pj = pi; pj > b && sort_r_cmpswap(pj-w,pj,w,compar,arg); pj -= w) {}
112 /* nel > 6; Quicksort */
115 char *pl, *ple, *pr, *pre, *pivot;
116 char *last = b+w*(nel-1), *tmp;
119 Use median of second, middle and second-last items as pivot.
120 First and last may have been swapped with pivot and therefore be extreme
127 /* printf("pivots: %i, %i, %i\n", *(int*)l[0], *(int*)l[1], *(int*)l[2]); */
129 if(compar(l[0],l[1],arg) > 0) { SORT_R_SWAP(l[0], l[1], tmp); }
130 if(compar(l[1],l[2],arg) > 0) {
131 SORT_R_SWAP(l[1], l[2], tmp);
132 if(compar(l[0],l[1],arg) > 0) { SORT_R_SWAP(l[0], l[1], tmp); }
135 /* swap mid value (l[1]), and last element to put pivot as last element */
136 if(l[1] != last) { sort_r_swap(l[1], last, w); }
139 pl is the next item on the left to be compared to the pivot
140 pr is the last item on the right that was compared to the pivot
141 ple is the left position to put the next item that equals the pivot
142 ple is the last right position where we put an item that equals the pivot
144 v- end (beyond the array)
145 EEEEEELLLLLLLLuuuuuuuuGGGGGGGEEEEEEEE.
146 ^- b ^- ple ^- pl ^- pr ^- pre ^- last (where the pivot is)
148 Pivot comparison key:
149 E = equal, L = less than, u = unknown, G = greater than, E = equal
157 Loop into the list from the left and right at the same time to find:
158 - an item on the left that is greater than the pivot
159 - an item on the right that is less than the pivot
160 Once found, they are swapped and the loop continues.
161 Meanwhile items that are equal to the pivot are moved to the edges of the
165 /* Move left hand items which are equal to the pivot to the far left.
166 break when we find an item that is greater than the pivot */
167 for(; pl < pr; pl += w) {
168 cmp = compar(pl, pivot, arg);
169 if(cmp > 0) { break; }
171 if(ple < pl) { sort_r_swap(ple, pl, w); }
175 /* break if last batch of left hand items were equal to pivot */
176 if(pl >= pr) { break; }
177 /* Move right hand items which are equal to the pivot to the far right.
178 break when we find an item that is less than the pivot */
180 pr -= w; /* Move right pointer onto an unprocessed item */
181 cmp = compar(pr, pivot, arg);
184 if(pr < pre) { sort_r_swap(pr, pre, w); }
187 if(pl < pr) { sort_r_swap(pl, pr, w); }
194 pl = pr; /* pr may have gone below pl */
197 Now we need to go from: EEELLLGGGGEEEE
200 Pivot comparison key:
201 E = equal, L = less than, u = unknown, G = greater than, E = equal
203 sort_r_swap_blocks(b, ple-b, pl-ple);
204 sort_r_swap_blocks(pr, pre-pr, end-pre);
206 /*for(size_t i=0; i<nel; i++) {printf("%4i", *(int*)(b + i*sizeof(int)));}
209 sort_r_simple(b, (pl-ple)/w, w, compar, arg);
210 sort_r_simple(end-(pre-pr), (pre-pr)/w, w, compar, arg);
215 #if defined NESTED_QSORT
217 static _SORT_R_INLINE void sort_r(void *base, size_t nel, size_t width,
218 int (*compar)(const void *_a,
223 int nested_cmp(const void *a, const void *b)
225 return compar(a, b, arg);
228 qsort(base, nel, width, nested_cmp);
231 #else /* !NESTED_QSORT */
233 /* Declare structs and functions */
235 #if defined _SORT_R_BSD
237 /* Ensure qsort_r is defined */
238 extern void qsort_r(void *base, size_t nel, size_t width, void *thunk,
239 int (*compar)(void *_thunk,
240 const void *_a, const void *_b));
244 #if defined _SORT_R_BSD || defined _SORT_R_WINDOWS
246 /* BSD (qsort_r), Windows (qsort_s) require argument swap */
251 int (*compar)(const void *_a, const void *_b, void *_arg);
254 static _SORT_R_INLINE int sort_r_arg_swap(void *s,
255 const void *a, const void *b)
257 struct sort_r_data *ss = (struct sort_r_data*)s;
258 return (ss->compar)(a, b, ss->arg);
263 #if defined _SORT_R_LINUX
265 typedef int(* __compar_d_fn_t)(const void *, const void *, void *);
266 extern void qsort_r(void *base, size_t nel, size_t width,
267 __compar_d_fn_t __compar, void *arg)
268 __attribute__((nonnull (1, 4)));
274 static _SORT_R_INLINE void sort_r(void *base, size_t nel, size_t width,
275 int (*compar)(const void *_a,
276 const void *_b, void *_arg),
279 #if defined _SORT_R_LINUX
281 #if defined __GLIBC__ && ((__GLIBC__ < 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 8))
283 /* no qsort_r in glibc before 2.8, need to use nested qsort */
284 sort_r_simple(base, nel, width, compar, arg);
288 qsort_r(base, nel, width, compar, arg);
292 #elif defined _SORT_R_BSD
294 struct sort_r_data tmp;
297 qsort_r(base, nel, width, &tmp, sort_r_arg_swap);
299 #elif defined _SORT_R_WINDOWS
301 struct sort_r_data tmp;
304 qsort_s(base, nel, width, sort_r_arg_swap, &tmp);
308 /* Fall back to our own quicksort implementation */
309 sort_r_simple(base, nel, width, compar, arg);
314 #endif /* !NESTED_QSORT */
316 #undef _SORT_R_INLINE
317 #undef _SORT_R_WINDOWS
321 #endif /* SORT_R_H_ */