19 #ifndef THRILL_COMMON_QSORT_HEADER 20 #define THRILL_COMMON_QSORT_HEADER 28 namespace qsort_local {
31 template <
typename ValueType>
32 void rotate3(ValueType& a0, ValueType& a1, ValueType& a2) {
33 ValueType tmp = std::move(a0);
40 template <
typename ValueType>
41 void rotate4(ValueType& a0, ValueType& a1, ValueType& a2, ValueType& a3) {
42 ValueType tmp = std::move(a0);
50 template <
typename Compare,
typename Iterator>
51 void sort3(Iterator
x, Iterator y, Iterator z, Compare cmp) {
75 template <
typename Compare,
typename Iterator>
76 void sort4(Iterator x1, Iterator x2, Iterator x3, Iterator x4, Compare cmp) {
79 sort3<Compare>(x1, x2, x3, cmp);
92 template <
typename Compare,
typename Iterator>
93 void sort5(Iterator x1, Iterator x2, Iterator x3,
94 Iterator x4, Iterator x5, Compare cmp) {
97 sort4<Compare>(x1, x2, x3, x4, cmp);
112 template <
typename Compare,
typename Iterator>
114 using value_type =
typename std::iterator_traits<Iterator>::value_type;
117 switch (right - left) {
122 if (cmp(*(--right), *left))
126 return qsort_local::sort3<Compare>(left, left + 1, left + 2, cmp);
128 return qsort_local::sort4<Compare>(
129 left, left + 1, left + 2, left + 3, cmp);
131 return qsort_local::sort5<Compare>(
132 left, left + 1, left + 2, left + 3, left + 4, cmp);
135 for (Iterator i = left + 1; i != right; ++i) {
137 value_type t(std::move(*j));
138 for (Iterator k = i; k != left && cmp(t, *(--k)); --j)
144 template <
typename Iterator,
typename Compare>
145 Iterator
median3(Iterator a, Iterator b, Iterator c, Compare cmp) {
149 else if (cmp(*a, *c))
157 else if (cmp(*b, *c))
165 template <
typename Iterator,
typename Compare>
167 for (
size_t i = 1; i < size; i++) {
171 if (cmp(*t, *(A[j - 1]))) {
185 template <
typename Compare,
typename Iterator>
187 using value_type =
typename std::iterator_traits<Iterator>::value_type;
191 return qsort_local::InsertionSort<Compare>(lo, hi, cmp);
195 Iterator samples[7] = {
196 lo + n * 1 / 8, lo + n * 2 / 8, lo + n * 3 / 8,
197 lo + n * 4 / 8, lo + n * 5 / 8, lo + n * 6 / 8, lo + n * 7 / 8
202 swap(*lo, *(samples[2]));
203 swap(*(hi - 1), *(samples[4]));
205 const value_type p = *lo;
206 const value_type q = *(hi - 1);
217 else if (!cmp(*k, q)) {
238 qsort_two_pivots_yaroslavskiy<Compare>(lo, l, cmp);
239 qsort_two_pivots_yaroslavskiy<Compare>(l + 1, g, cmp);
240 qsort_two_pivots_yaroslavskiy<Compare>(g + 1, hi, cmp);
243 template <
typename Compare,
typename Iterator>
245 using value_type =
typename std::iterator_traits<Iterator>::value_type;
248 size_t n = right - left;
253 Iterator samples[7] = {
254 left + n * 1 / 8, left + n * 2 / 8, left + n * 3 / 8,
255 left + n * 4 / 8, left + n * 5 / 8, left + n * 6 / 8, left + n * 7 / 8
260 swap(*left, *(samples[1]));
261 swap(*(left + 1), *(samples[3]));
262 swap(*(right - 1), *(samples[5]));
264 Iterator i = left + 2;
267 Iterator k = right - 2;
270 const value_type p = *left;
271 const value_type q = *(left + 1);
272 const value_type r = *(right - 1);
317 swap(*left, *(i - 2));
318 swap(*(right - 1), *(l + 1));
320 sLOG0 <<
"qsort_three_pivots: " 326 qsort_three_pivots<Compare>(left, i - 2, cmp);
327 qsort_three_pivots<Compare>(i - 1, j - 1, cmp);
328 qsort_three_pivots<Compare>(j, l + 1, cmp);
329 qsort_three_pivots<Compare>(l + 2, right, cmp);
335 #endif // !THRILL_COMMON_QSORT_HEADER void rotate3(ValueType &a0, ValueType &a1, ValueType &a2)
Assigns a0 <- a1, a1 <- a2, and a2 <- a0.
void InsertionSort(Iterator left, Iterator right, Compare cmp)
void qsort_three_pivots(Iterator left, Iterator right, Compare cmp)
void qsort_two_pivots_yaroslavskiy(Iterator lo, Iterator hi, Compare cmp)
void sort3(Iterator x, Iterator y, Iterator z, Compare cmp)
Sort three items, stable, 2-3 compares, 0-2 swaps.
void rotate4(ValueType &a0, ValueType &a1, ValueType &a2, ValueType &a3)
Assigns a0 <- a1, a1 <- a2, a2 <- a3, and a3 <- a0.
#define sLOG0
Override default output: never or always output log.
void sort_samples(Iterator *A, size_t size, Compare cmp)
Sort iterators by their content, used for sorting pivot samples.
Iterator median3(Iterator a, Iterator b, Iterator c, Compare cmp)
void sort4(Iterator x1, Iterator x2, Iterator x3, Iterator x4, Compare cmp)
Sort four items, stable, 3-6 compares, 0-5 swaps.
void swap(CountingPtr< A, D > &a1, CountingPtr< A, D > &a2) noexcept
void sort5(Iterator x1, Iterator x2, Iterator x3, Iterator x4, Iterator x5, Compare cmp)
Sort five items, 4-10 compares, 0-9 swaps.