Debian Codegraph @

external/num/median_common.h

raw symbols SCIP
1 #ifndef _median_common_h_
2 #define _median_common_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
11 /**
12 Swaps the median of r[a], r[b], and r[c] into r[b].
13 */
14
15 template <class T>
16 void median3(T* r, size_t a, size_t b, size_t c)
17 {
18 if (r[b] < r[a]) // b < a
19 {
20 if (r[b] < r[c]) // b < a, b < c
21 {
22 if (r[c] < r[a]) // b < c < a
23 std::swap (r[b], r[c]);
24 else // b < a <= c
25 std::swap (r[b], r[a]);
26 }
27 }
28 else if (r[c] < r[b]) // a <= b, c < b
29 {
30 if (r[c] < r[a]) // c < a <= b
31 std::swap (r[b], r[a]);
32 else // a <= c < b
33 std::swap (r[b], r[c]);
34 }
35 Melder_assert (r[a] <= r[b] && r[b] <= r[c] || r[a] >= r[b] && r[b] >= r[c]);
36 }
37
38 /**
39 Sorts in place r[a], r[b], and r[c].
40 */
41 template <class T>
42 void sort3(T* r, size_t a, size_t b, size_t c)
43 {
44 if (r[b] < r[a]) // b < a
45 {
46 if (r[c] < r[b]) // c < b < a
47 {
48 std::swap (r[a], r[c]); // a < b < c
49 }
50 else // b < a, b <= c
51 {
52 auto t = r[a];
53 r[a] = r[b];
54 if (r[c] < t) // b <= c < a
55 {
56 r[b] = r[c];
57 r[c] = t;
58 }
59 else // b < a <= c
60 {
61 r[b] = t;
62 }
63 }
64 }
65 else if (r[c] < r[b]) // a <= b, c < b
66 {
67 auto t = r[c];
68 r[c] = r[b];
69 if (t < r[a]) // c < a < b
70 {
71 r[b] = r[a];
72 r[a] = t;
73 }
74 else // a <= c < b
75 {
76 r[b] = t;
77 }
78 }
79
80 Melder_assert (r[a] <= r[b] && r[b] <= r[c]);
81 }
82
83 /**
84 If leanRight == false, swaps the lower median of r[a]...r[d] into r[b] and
85 the minimum into r[a]. If leanRight == true, swaps the upper median of
86 r[a]...r[d] into r[c] and the minimum into r[d].
87 */
88 template <bool leanRight, class T>
89 void partition4(T* r, size_t a, size_t b, size_t c, size_t d)
90 {
91 Melder_assert (a != b && a != c && a != d && b != c && b != d
92 && c != d);
93 /* static */ if (leanRight)
94 {
95 // In the median of 5 algorithm, consider r[e] infinite
96 if (r[c] < r[a]) {
97 std::swap (r[a], r[c]);
98 } // a <= c
99 if (r[d] < r[b]) {
100 std::swap (r[b], r[d]);
101 } // a <= c, b <= d
102 if (r[d] < r[c]) {
103 std::swap (r[c], r[d]); // a <= d, b <= c < d
104 std::swap (r[a], r[b]); // b <= d, a <= c < d
105 } // a <= c <= d, b <= d
106 if (r[c] < r[b]) { // a <= c <= d, c < b <= d
107 std::swap (r[b], r[c]); // a <= b <= c <= d
108 } // a <= b <= c <= d
109 Melder_assert (r[a] <= r[c] && r[b] <= r[c] && r[c] <= r[d]);
110 }
111 else
112 {
113 // In the median of 5 algorithm consider r[a] infinitely small, then
114 // change b->a. c->b, d->c, e->d
115 if (r[c] < r[a]) {
116 std::swap (r[a], r[c]);
117 }
118 if (r[c] < r[b]) {
119 std::swap (r[b], r[c]);
120 }
121 if (r[d] < r[a]) {
122 std::swap (r[a], r[d]);
123 }
124 if (r[d] < r[b]) {
125 std::swap (r[b], r[d]);
126 } else {
127 if (r[b] < r[a]) {
128 std::swap (r[a], r[b]);
129 }
130 }
131 Melder_assert (r[a] <= r[b] && r[b] <= r[c] && r[b] <= r[d]);
132 }
133 }
134
135 /**
136 Places the median of r[a]...r[e] in r[c] and partitions the other elements
137 around it.
138 */
139 template <class T>
140 void partition5(T* r, size_t a, size_t b, size_t c, size_t d, size_t e)
141 {
142 Melder_assert (a != b && a != c && a != d && a != e && b != c && b != d && b != e
143 && c != d && c != e && d != e);
144 if (r[c] < r[a]) {
145 std::swap (r[a], r[c]);
146 }
147 if (r[d] < r[b]) {
148 std::swap (r[b], r[d]);
149 }
150 if (r[d] < r[c]) {
151 std::swap (r[c], r[d]);
152 std::swap (r[a], r[b]);
153 }
154 if (r[e] < r[b]) {
155 std::swap (r[b], r[e]);
156 }
157 if (r[e] < r[c]) {
158 std::swap (r[c], r[e]);
159 if (r[c] < r[a]) {
160 std::swap (r[a], r[c]);
161 }
162 } else {
163 if (r[c] < r[b]) {
164 std::swap (r[b], r[c]);
165 }
166 }
167 Melder_assert (r[a] <= r[c] && r[b] <= r[c] && r[c] <= r[d] && r[c] <= r[e]);
168 }
169
170 /**
171 Implements Hoare partition.
172 */
173 template <class T>
174 T* pivotPartition(T* r, size_t k, size_t length)
175 {
176 Melder_assert (k < length);
177 std::swap (*r, r[k]);
178 size_t lo = 1, hi = length - 1;
179 for (;; ++lo, --hi)
180 {
181 for (;; ++lo)
182 {
183 if (lo > hi) goto loop_done;
184 if (r[lo] >= *r) break;
185 }
186 // found the left bound: r[lo] >= r[0]
187 Melder_assert (lo <= hi);
188 for (; *r < r[hi]; --hi)
189 {
190 }
191 if (lo >= hi) break;
192 // found the right bound: r[hi] <= r[0], swap & make progress
193 Melder_assert (r[lo] >= r[hi]);
194 std::swap (r[lo], r[hi]);
195 }
196 loop_done:
197 --lo;
198 std::swap (r[lo], *r);
199 return r + lo;
200 }
201
202 /**
203 Implements the quickselect algorithm, parameterized with a partition function.
204 */
205 template <class T, T* (*partition)(T*, T*)>
206 void quickselect(T* r, T* mid, T* end)
207 {
208 if (r == end || mid >= end) return;
209 Melder_assert (r <= mid && mid < end);
210 for (;;) switch (end - r)
211 {
212 case 1:
213 return;
214 case 2:
215 if (*r > r[1]) std::swap (*r, r[1]);
216 return;
217 case 3:
218 sort3(r, 0, 1, 2);
219 return;
220 case 4:
221 switch (mid - r)
222 {
223 case 0: goto select_min;
224 case 1: partition4<false>(r, 0, 1, 2, 3); break;
225 case 2: partition4<true>(r, 0, 1, 2, 3); break;
226 case 3: goto select_max;
227 default: Melder_assert (false);
228 }
229 return;
230 default:
231 Melder_assert (end - r > 4);
232 if (r == mid)
233 {
234 select_min:
235 auto pivot = r;
236 for (++mid; mid < end; ++mid)
237 if (*mid < *pivot) pivot = mid;
238 std::swap (*r, *pivot);
239 return;
240 }
241 if (mid + 1 == end)
242 {
243 select_max:
244 auto pivot = r;
245 for (mid = r + 1; mid < end; ++mid)
246 if (*pivot < *mid) pivot = mid;
247 std::swap (*pivot, end[-1]);
248 return;
249 }
250 auto pivot = partition(r, end);
251 if (pivot == mid) return;
252 if (pivot > mid)
253 {
254 end = pivot;
255 }
256 else
257 {
258 r = pivot + 1;
259 }
260 }
261 }
262
263 /**
264 Returns the index of the median of r[a], r[b], and r[c] without writing
265 anything.
266 */
267 template <class T>
268 size_t medianIndex(const T* r, size_t a, size_t b, size_t c)
269 {
270 if (r[a] > r[c]) std::swap(a, c);
271 if (r[b] > r[c]) return c;
272 if (r[b] < r[a]) return a;
273 return b;
274 }
275
276 /**
277 Returns the index of the median of r[a], r[b], r[c], and r[d] without writing
278 anything. If leanRight is true, computes the upper median. Otherwise, conputes
279 the lower median.
280 */
281 template <bool leanRight, class T>
282 static size_t medianIndex(T* r, size_t a, size_t b, size_t c, size_t d)
283 {
284 if (r[d] < r[c]) std::swap(c, d);
285 Melder_assert (r[c] <= r[d]);
286 /* static */ if (leanRight)
287 {
288 if (r[c] < r[a])
289 {
290 Melder_assert (r[c] < r[a] && r[c] <= r[d]); // so r[c] is out
291 return medianIndex(r, a, b, d);
292 }
293 Melder_assert (r[a] <= r[c] && r[a] <= r[d]); // so r[a] is out
294 }
295 else
296 {
297 if (r[d] >= r[a])
298 {
299 Melder_assert (r[d] >= r[c] && r[d] >= r[a]); // so r[d] is out
300 return medianIndex(r, a, b, c);
301 }
302 Melder_assert (r[a] > r[d] && r[a] > r[c]); // so r[a] is out
303 }
304 // Could return medianIndex(r, b, c, d) but we already know r[c] <= r[d]
305 if (r[b] <= r[c]) return c;
306 if (r[b] > r[d]) return d;
307 return b;
308 }
309
310 /**
311 Tukey's Ninther: compute the median of r[_1], r[_2], r[_3], then the median of
312 r[_4], r[_5], r[_6], then the median of r[_7], r[_8], r[_9], and then swap the
313 median of those three medians into r[_5].
314 */
315 template <class T>
316 void ninther(T* r, size_t _1, size_t _2, size_t _3, size_t _4, size_t _5,
317 size_t _6, size_t _7, size_t _8, size_t _9)
318 {
319 _2 = medianIndex(r, _1, _2, _3);
320 _8 = medianIndex(r, _7, _8, _9);
321 if (r[_2] > r[_8]) std::swap(_2, _8);
322 if (r[_4] > r[_6]) std::swap(_4, _6);
323 // Here we know that r[_2] and r[_8] are the other two medians and that
324 // r[_2] <= r[_8]. We also know that r[_4] <= r[_6]
325 if (r[_5] < r[_4])
326 {
327 // r[_4] is the median of r[_4], r[_5], r[_6]
328 }
329 else if (r[_5] > r[_6])
330 {
331 // r[_6] is the median of r[_4], r[_5], r[_6]
332 _4 = _6;
333 }
334 else
335 {
336 // Here we know r[_5] is the median of r[_4], r[_5], r[_6]
337 if (r[_5] < r[_2]) return std::swap (r[_5], r[_2]);
338 if (r[_5] > r[_8]) return std::swap (r[_5], r[_8]);
339 // This is the only path that returns with no swap
340 return;
341 }
342 // Here we know r[_4] is the median of r[_4], r[_5], r[_6]
343 if (r[_4] < r[_2]) _4 = _2;
344 else if (r[_4] > r[_8]) _4 = _8;
345 std::swap (r[_5], r[_4]);
346 }
347
348 /**
349 Input assumptions:
350
351 (a) hi <= rite
352 (c) the range r[0 .. hi] contains elements no smaller than r[0]
353
354 Output guarantee: same as Hoare partition using r[0] as pivot. Returns the new
355 position of the pivot.
356 */
357 template <class T>
358 size_t expandPartitionRight(T* r, size_t hi, size_t rite)
359 {
360 size_t pivot = 0;
361 Melder_assert (pivot <= hi);
362 Melder_assert (hi <= rite);
363 // First loop: spend r[pivot .. hi]
364 for (; pivot < hi; --rite)
365 {
366 if (rite == hi) goto done;
367 if (r[rite] >= r[0]) continue;
368 ++pivot;
369 Melder_assert (r[pivot] >= r[0]);
370 std::swap (r[rite], r[pivot]);
371 }
372 // Second loop: make left and pivot meet
373 for (; rite > pivot; --rite)
374 {
375 if (r[rite] >= r[0]) continue;
376 while (rite > pivot)
377 {
378 ++pivot;
379 if (r[0] < r[pivot])
380 {
381 std::swap (r[rite], r[pivot]);
382 break;
383 }
384 }
385 }
386
387 done:
388 std::swap (r[0], r[pivot]);
389 return pivot;
390 }
391
392 /**
393 Input assumptions:
394
395 (a) lo > 0, lo <= pivot
396 (b) the range r[lo .. pivot] already contains elements no greater than r[pivot]
397
398 Output guarantee: Same as Hoare partition around r[pivot]. Returns the new
399 position of the pivot.
400
401 */
402 template <class T>
403 size_t expandPartitionLeft(T* r, size_t lo, size_t pivot)
404 {
405 Melder_assert (lo > 0 && lo <= pivot);
406 size_t left = 0;
407 const auto oldPivot = pivot;
408 for (; lo < pivot; ++left)
409 {
410 if (left == lo) goto done;
411 if (r[oldPivot] >= r[left]) continue;
412 --pivot;
413 Melder_assert (r[oldPivot] >= r[pivot]);
414 std::swap (r[left], r[pivot]);
415 }
416 // Second loop: make left and pivot meet
417 for (;; ++left)
418 {
419 if (left == pivot) break;
420 if (r[oldPivot] >= r[left]) continue;
421 for (;;)
422 {
423 if (left == pivot) goto done;
424 --pivot;
425 if (r[pivot] < r[oldPivot])
426 {
427 std::swap (r[left], r[pivot]);
428 break;
429 }
430 }
431 }
432
433 done:
434 std::swap (r[oldPivot], r[pivot]);
435 return pivot;
436 }
437
438 /**
439 Input assumptions:
440
441 (a) lo <= pivot, pivot < hi, hi <= length
442 (b) the range r[lo .. pivot] already contains elements no greater than
443 r[pivot]
444 (c) the range r[pivot .. hi] already contains elements no smaller than
445 r[pivot]
446
447 Output guarantee: Same as Hoare partition around r[pivot], returning the new
448 position of the pivot.
449 */
450 template <class T>
451 size_t expandPartition(T* r, size_t lo, size_t pivot, size_t hi, size_t length)
452 {
453 Melder_assert (lo <= pivot && pivot < hi && hi <= length);
454 --hi;
455 --length;
456 size_t left = 0;
457 for (;; ++left, --length)
458 {
459 for (;; ++left)
460 {
461 if (left == lo)
462 return pivot +
463 expandPartitionRight(r + pivot, hi - pivot, length - pivot);
464 if (r[left] > r[pivot]) break;
465 }
466 for (;; --length)
467 {
468 if (length == hi)
469 return left +
470 expandPartitionLeft(r + left, lo - left, pivot - left);
471 if (r[pivot] >= r[length]) break;
472 }
473 std::swap (r[left], r[length]);
474 }
475 }
476
477 #endif /* _median_common_h_ */