| 1 |
#ifndef _median_of_ninthers_h_ |
| 2 |
#define _median_of_ninthers_h_ |
| 3 |
/* Copyright Andrei Alexandrescu, 2016-. |
| 4 |
* Distributed under the Boost Software License, Version 1.0. |
| 5 |
* (See accompanying file LICENSE_1_0.txt or copy at |
| 6 |
* https://boost.org/LICENSE_1_0.txt) |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "melder.h" |
| 10 |
#include "median_common.h" |
| 11 |
|
| 12 |
template <class T> |
| 13 |
size_t partitionImpl(T* beg, size_t length); |
| 14 |
|
| 15 |
template <class T> |
| 16 |
void adaptiveQuickselect(T* beg, size_t n, size_t length); |
| 17 |
|
| 18 |
/** |
| 19 |
Median of minima |
| 20 |
*/ |
| 21 |
template <class T> |
| 22 |
size_t medianOfMinima(T*const r, const size_t n, const size_t length) |
| 23 |
{ |
| 24 |
Melder_assert (length >= 2); |
| 25 |
Melder_assert (n * 4 <= length); |
| 26 |
Melder_assert (n > 0); |
| 27 |
const size_t subset = n * 2, |
| 28 |
computeMinOver = (length - subset) / subset; |
| 29 |
Melder_assert (computeMinOver > 0); |
| 30 |
for (size_t i = 0, j = subset; i < subset; ++i) |
| 31 |
{ |
| 32 |
const auto limit = j + computeMinOver; |
| 33 |
size_t minIndex = j; |
| 34 |
while (++j < limit) |
| 35 |
if (r[j] < r[minIndex]) |
| 36 |
minIndex = j; |
| 37 |
if (r[minIndex] < r[i]) |
| 38 |
std::swap (r[i], r[minIndex]); |
| 39 |
Melder_assert (j < length || i + 1 == subset); |
| 40 |
} |
| 41 |
adaptiveQuickselect(r, n, subset); |
| 42 |
return expandPartition(r, 0, n, subset, length); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
Median of maxima |
| 47 |
*/ |
| 48 |
template <class T> |
| 49 |
size_t medianOfMaxima(T*const r, const size_t n, const size_t length) |
| 50 |
{ |
| 51 |
Melder_assert (length >= 2); |
| 52 |
Melder_assert (n * 4 >= length * 3 && n < length); |
| 53 |
const size_t subset = (length - n) * 2, |
| 54 |
subsetStart = length - subset, |
| 55 |
computeMaxOver = subsetStart / subset; |
| 56 |
Melder_assert (computeMaxOver > 0); |
| 57 |
for (size_t i = subsetStart, j = i - subset * computeMaxOver; i < length; ++i) |
| 58 |
{ |
| 59 |
const auto limit = j + computeMaxOver; |
| 60 |
size_t maxIndex = j; |
| 61 |
while (++j < limit) |
| 62 |
if (r[j] > r[maxIndex]) |
| 63 |
maxIndex = j; |
| 64 |
if (r[maxIndex] > r[i]) |
| 65 |
std::swap (r[i], r[maxIndex]); |
| 66 |
Melder_assert (j != 0 || i + 1 == length); |
| 67 |
} |
| 68 |
adaptiveQuickselect(r + subsetStart, length - n, subset); |
| 69 |
return expandPartition(r, subsetStart, n, length, length); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
Partitions r[0 .. length] using a pivot of its own choosing. Attempts to pick a |
| 74 |
pivot that approximates the median. Returns the position of the pivot. |
| 75 |
*/ |
| 76 |
template <class T> |
| 77 |
size_t medianOfNinthers(T*const r, const size_t length) |
| 78 |
{ |
| 79 |
Melder_assert (length >= 12); |
| 80 |
const auto frac = |
| 81 |
length <= 1024 ? length / 12 : |
| 82 |
length <= 128 * 1024 ? length / 64 |
| 83 |
: length / 1024; |
| 84 |
auto pivot = frac / 2; |
| 85 |
const auto lo = length / 2 - pivot, hi = lo + frac; |
| 86 |
Melder_assert (lo >= frac * 4); |
| 87 |
Melder_assert (length - hi >= frac * 4); |
| 88 |
Melder_assert (lo / 2 >= pivot); |
| 89 |
const auto gap = (length - 9 * frac) / 4; |
| 90 |
auto a = lo - 4 * frac - gap, b = hi + gap; |
| 91 |
for (size_t i = lo; i < hi; ++i, a += 3, b += 3) |
| 92 |
{ |
| 93 |
ninther(r, a, i - frac, b, a + 1, i, b + 1, a + 2, i + frac, b + 2); |
| 94 |
} |
| 95 |
|
| 96 |
adaptiveQuickselect(r + lo, pivot, frac); |
| 97 |
return expandPartition(r, lo, lo + pivot, hi, length); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
|
| 102 |
Quickselect driver for medianOfNinthers, medianOfMinima, and medianOfMaxima. |
| 103 |
Dispathes to each depending on the relationship between n (the sought order |
| 104 |
statistics) and length. |
| 105 |
|
| 106 |
*/ |
| 107 |
template <class T> |
| 108 |
void adaptiveQuickselect(T* r, size_t n, size_t length) |
| 109 |
{ |
| 110 |
trace (n, U" ", length); |
| 111 |
Melder_assert (n < length); |
| 112 |
for (;;) |
| 113 |
{ |
| 114 |
// Decide strategy for partitioning |
| 115 |
if (n == 0) |
| 116 |
{ |
| 117 |
// That would be the max |
| 118 |
auto pivot = n; |
| 119 |
for (++n; n < length; ++n) |
| 120 |
if (r[n] < r[pivot]) pivot = n; |
| 121 |
std::swap (r[0], r[pivot]); |
| 122 |
return; |
| 123 |
} |
| 124 |
if (n + 1 == length) |
| 125 |
{ |
| 126 |
// That would be the min |
| 127 |
auto pivot = 0; |
| 128 |
for (n = 1; n < length; ++n) |
| 129 |
if (r[pivot] < r[n]) pivot = n; |
| 130 |
std::swap (r[pivot], r[length - 1]); |
| 131 |
return; |
| 132 |
} |
| 133 |
Melder_assert (n < length); |
| 134 |
size_t pivot; |
| 135 |
if (length <= 16) |
| 136 |
pivot = pivotPartition(r, n, length) - r; |
| 137 |
else if (n * 6 <= length) |
| 138 |
pivot = medianOfMinima(r, n, length); |
| 139 |
else if (n * 6 >= length * 5) |
| 140 |
pivot = medianOfMaxima(r, n, length); |
| 141 |
else |
| 142 |
pivot = medianOfNinthers(r, length); |
| 143 |
|
| 144 |
// See how the pivot fares |
| 145 |
if (pivot == n) |
| 146 |
{ |
| 147 |
return; |
| 148 |
} |
| 149 |
if (pivot > n) |
| 150 |
{ |
| 151 |
length = pivot; |
| 152 |
} |
| 153 |
else |
| 154 |
{ |
| 155 |
++pivot; |
| 156 |
r += pivot; |
| 157 |
length -= pivot; |
| 158 |
n -= pivot; |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
#endif /* _median_of_ninthers_h_ */ |