2 * sgen-qsort.h: Fast inline sorting
4 * Copyright (C) 2014 Xamarin Inc
6 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
8 #ifndef __MONO_SGENQSORT_H__
9 #define __MONO_SGENQSORT_H__
11 /* Copied from non-inline implementation in sgen-qsort.c */
12 #define DEF_QSORT_INLINE(name, type, compare) \
14 qsort_swap_##name (type array[], const ssize_t i, const ssize_t j, type *const swap_tmp) \
16 *swap_tmp = array [i]; \
17 array [i] = array [j]; \
18 array [j] = *swap_tmp; \
26 type *const pivot_tmp, \
27 type *const swap_tmp) \
29 ssize_t left, right, middle, pivot; \
30 while (begin < end) { \
33 middle = begin + (end - begin) / 2; \
34 if (compare (array [middle], array [left]) < 0) \
35 qsort_swap_##name (array, middle, left, swap_tmp); \
36 if (compare (array [right], array [left]) < 0) \
37 qsort_swap_##name (array, right, left, swap_tmp); \
38 if (compare (array [right], array [middle]) < 0) \
39 qsort_swap_##name (array, right, middle, swap_tmp); \
41 *pivot_tmp = array [pivot]; \
43 while (left <= right && compare (array [left], *pivot_tmp) <= 0) \
45 while (left <= right && compare (array [right], *pivot_tmp) > 0) \
49 qsort_swap_##name (array, left, right, swap_tmp); \
55 array [pivot] = array [right]; \
56 array [right] = *pivot_tmp; \
58 if (right - begin < end - left) { \
59 qsort_rec_##name (array, begin, right, pivot_tmp, swap_tmp); \
62 qsort_rec_##name (array, left, end, pivot_tmp, swap_tmp); \
69 qsort_##name (type array[], size_t count) \
73 qsort_rec_##name (array, 0, (ssize_t)count - 1, &pivot_tmp, &swap_tmp); \