5 * Copyright (C) 2014 Xamarin Inc
7 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
9 #ifndef __MONO_SGENQSORT_H__
10 #define __MONO_SGENQSORT_H__
12 /* Copied from non-inline implementation in sgen-qsort.c */
13 #define DEF_QSORT_INLINE(name, type, compare) \
15 qsort_swap_##name (type array[], const ssize_t i, const ssize_t j, type *const swap_tmp) \
17 *swap_tmp = array [i]; \
18 array [i] = array [j]; \
19 array [j] = *swap_tmp; \
27 type *const pivot_tmp, \
28 type *const swap_tmp) \
30 ssize_t left, right, middle, pivot; \
31 while (begin < end) { \
34 middle = begin + (end - begin) / 2; \
35 if (compare (array [middle], array [left]) < 0) \
36 qsort_swap_##name (array, middle, left, swap_tmp); \
37 if (compare (array [right], array [left]) < 0) \
38 qsort_swap_##name (array, right, left, swap_tmp); \
39 if (compare (array [right], array [middle]) < 0) \
40 qsort_swap_##name (array, right, middle, swap_tmp); \
42 *pivot_tmp = array [pivot]; \
44 while (left <= right && compare (array [left], *pivot_tmp) <= 0) \
46 while (left <= right && compare (array [right], *pivot_tmp) > 0) \
50 qsort_swap_##name (array, left, right, swap_tmp); \
56 array [pivot] = array [right]; \
57 array [right] = *pivot_tmp; \
59 if (right - begin < end - left) { \
60 qsort_rec_##name (array, begin, right, pivot_tmp, swap_tmp); \
63 qsort_rec_##name (array, left, end, pivot_tmp, swap_tmp); \
70 qsort_##name (type array[], size_t count) \
74 qsort_rec_##name (array, 0, (ssize_t)count - 1, &pivot_tmp, &swap_tmp); \