-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkdTreeMergeSort.h
588 lines (514 loc) · 28.8 KB
/
kdTreeMergeSort.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/*
* Copyright (c) 2015, 2021, 2023, 2024 Russell A. Brown
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef KD_TREE_MERGE_SORT_H
#define KD_TREE_MERGE_SORT_H
/* A cutoff for switching from merge sort to insertion sort in the KdNode::mergeSort* functions */
#ifndef INSERTION_SORT_CUTOFF
#define INSERTION_SORT_CUTOFF 15
#endif
/* Forward references to KdNode and KdTree friend classes */
template <typename>
class KdNode;
template <typename>
class KdTree;
/* The merge sort functions */
template <typename K>
class MergeSort {
/*
* The superKeyCompare function compares two T arrays in all k dimensions,
* and uses the sorting or partition coordinate as the most significant dimension.
*
* Calling parameters:
*
* a - a K *
* b - a K *
* p - the most significant dimension
* dim - the number of dimensions
*
* returns a T result of comparing two T arrays
*/
private:
inline
static K superKeyCompare(K const* const a,
K const* const b,
signed_size_t const p,
signed_size_t const dim) {
// Typically, this first calculation of diff will be non-zero and bypass the 'for' loop.
K diff = a[p] - b[p];
for (signed_size_t i = 1; diff == 0 && i < dim; ++i) {
signed_size_t r = i + p;
// A fast alternative to the modulus operator for (i + p) < 2 * dim.
r = (r < dim) ? r : r - dim;
diff = a[r] - b[r];
}
return diff;
}
/*
* The following four merge sort functions are adapted from the mergesort function that is shown
* on p. 166 of Robert Sedgewick's "Algorithms in C++", Addison-Wesley, Reading, MA, 1992.
* That elegant implementation of the merge sort algorithm eliminates the requirement to test
* whether the upper and lower halves of an auxiliary array have become exhausted during the
* merge operation that copies from the auxiliary array to a result array. This elimination is
* made possible by inverting the order of the upper half of the auxiliary array and by accessing
* elements of the upper half of the auxiliary array from highest address to lowest address while
* accessing elements of the lower half of the auxiliary array from lowest address to highest
* address.
*
* The following four merge sort functions also implement two suggestions from p. 275 of Robert
* Sedgewick's and Kevin Wayne's "Algorithms 4th Edition", Addison-Wesley, New York, 2011. The
* first suggestion is to replace merge sort with insertion sort when the size of the array to
* sort falls below a threshold. The second suggestion is to avoid unnecessary copying to the
* auxiliary array prior to the merge step of the algorithm by implementing two versions of
* merge sort and by applying some "recursive trickery" to arrange that the required result is
* returned in an auxiliary array by one version and in a result array by the other version.
* The following four merge sort methods build upon this suggestion and return their result in
* either ascending or descending order, as discussed on pp. 173-174 of Robert Sedgewick's
* "Algorithms in C++", Addison-Wesley, Reading, MA, 1992.
*
* During multi-threaded execution, the upper and lower halves of the result array may be filled
* from the auxiliary array (or vice versa) simultaneously by two threads. The lower half of the
* result array is filled by accessing elements of the upper half of the auxiliary array from highest
* address to lowest address while accessing elements of the lower half of the auxiliary array from
* lowest address to highest address, as explained above for elimination of the test for exhaustion.
* The upper half of the result array is filled by addressing elements from the upper half of the
* auxiliary array from lowest address to highest address while accessing the elements from the lower
* half of the auxiliary array from highest address to lowest address. Note: for the upper half
* of the result array, there is no requirement to test for exhaustion provided that the upper half
* of the result array never comprises more elements than the lower half of the result array. This
* provision is satisfied by computing the median address of the result array as shown below for
* all four merge sort methods.
*
* The mergeSortReferenceAscending function recursively subdivides the reference array then
* merges the elements in ascending order and leaves the result in the reference array.
*
* Calling parameters:
*
* reference - a K ** that represents the array of (x, y, z, w...) coordinates to sort
* temporary - a K ** temporary array from which to copy sorted results;
* this array must be as large as the reference array
* low - the start index of the region of the reference array to sort
* high - the end index of the region of the reference array to sort
* p - the sorting partition (x, y, z, w...)
* dim - the number of dimensions
* maximumSubmitDepth - the maximum tree depth at which a child task may be launched
* depth - the tree depth
*/
private:
static void mergeSortReferenceAscending(K ** const reference,
K ** const temporary,
signed_size_t const low,
signed_size_t const high,
signed_size_t const p,
signed_size_t const dim,
signed_size_t const maximumSubmitDepth,
signed_size_t const depth) {
if (high - low > INSERTION_SORT_CUTOFF) {
// Avoid overflow when calculating the median.
signed_size_t const mid = low + ((high - low) >> 1);
// Subdivide the lower half of the array with a child thread at as many levels of subdivision as possible.
// Create the child threads as high in the subdivision hierarchy as possible for greater utilization.
// Is a child thread available to subdivide the lower half of the reference array?
if (maximumSubmitDepth < 0 || depth > maximumSubmitDepth) {
// No, recursively subdivide the lower half of the reference array with the current
// thread and return the result in the temporary array in ascending order.
mergeSortTemporaryAscending(reference, temporary, low, mid, p, dim, maximumSubmitDepth, depth + 1);
// Then recursively subdivide the upper half of the reference array with the current
// thread and return the result in the temporary array in descending order.
mergeSortTemporaryDescending(reference, temporary, mid + 1, high, p, dim, maximumSubmitDepth, depth + 1);
// Compare the results in the temporary array in ascending order and merge them into
// the reference array in ascending order.
for (signed_size_t i = low, j = high, k = low; k <= high; ++k) {
reference[k] =
(superKeyCompare(temporary[i], temporary[j], p, dim) < 0) ? temporary[i++] : temporary[j--];
}
}
else {
// Yes, a child thread is available, so recursively subdivide the lower half of the reference
// array with a child thread and return the result in the temporary array in ascending order.
auto sortFuture = async(launch::async, mergeSortTemporaryAscending, reference, temporary,
low, mid, p, dim, maximumSubmitDepth, depth + 1);
// And simultaneously, recursively subdivide the upper half of the reference array with
// the current thread and return the result in the temporary array in descending order.
mergeSortTemporaryDescending(reference, temporary, mid + 1, high, p, dim, maximumSubmitDepth, depth + 1);
// Wait for the child thread to finish execution.
try {
sortFuture.get();
}
catch (exception const& e) {
throw runtime_error("\n\ncaught exception for sort future in mergeSortReferenceAscending\n");
}
// Compare the results in the temporary array in ascending order with a child thread
// and merge them into the lower half of the reference array in ascending order.
auto mergeFuture =
async(launch::async, [&] {
for (signed_size_t i = low, j = high, k = low; k <= mid; ++k) {
reference[k] =
(superKeyCompare(temporary[i], temporary[j], p, dim) <= 0)
? temporary[i++] : temporary[j--];
}
});
// And simultaneously compare the results in the temporary array in descending order with the
// current thread and merge them into the upper half of the reference array in ascending order.
for (signed_size_t i = mid, j = mid + 1, k = high; k > mid; --k) {
reference[k] =
(superKeyCompare(temporary[i], temporary[j], p, dim) > 0) ? temporary[i--] : temporary[j++];
}
// Wait for the child thread to finish execution.
try {
mergeFuture.get();
}
catch (exception const& e) {
throw runtime_error("\n\ncaught exception for merge future in mergeSortReferenceAscending\n");
}
}
}
else {
// Here is Jon Benley's implementation of insertion sort from "Programming Pearls", pp. 115-116,
// Addison-Wesley, 1999, that sorts in ascending order and leaves the result in the reference array.
for (signed_size_t i = low + 1; i <= high; ++i) {
K * const tmp = reference[i];
signed_size_t j;
for (j = i; j > low && superKeyCompare(reference[j - 1], tmp, p, dim) > 0; --j) {
reference[j] = reference[j - 1];
}
reference[j] = tmp;
}
}
}
/*
* The mergeSortReferenceDescending function recursively subdivides the reference array then
* merges the elements in descending order and leaves the result in the reference array.
*
* Calling parameters:
*
* reference - a K ** that represents the array of (x, y, z, w...) coordinates to sort
* temporary - a K ** temporary array from which to copy sorted results;
* this array must be as large as the reference array
* low - the start index of the region of the reference array to sort
* high - the end index of the region of the reference array to sort
* p - the sorting partition (x, y, z, w...)
* dim - the number of dimensions
* maximumSubmitDepth - the maximum tree depth at which a child task may be launched
* depth - the tree depth
*/
private:
static void mergeSortReferenceDescending(K ** const reference,
K ** const temporary,
signed_size_t const low,
signed_size_t const high,
signed_size_t const p,
signed_size_t const dim,
signed_size_t const maximumSubmitDepth,
signed_size_t const depth) {
if (high - low > INSERTION_SORT_CUTOFF) {
// Avoid overflow when calculating the median.
signed_size_t const mid = low + ((high - low) >> 1);
// Subdivide the lower half of the array with a child thread at as many levels of subdivision as possible.
// Create the child threads as high in the subdivision hierarchy as possible for greater utilization.
// Is a child thread available to subdivide the lower half of the reference array?
if (maximumSubmitDepth < 0 || depth > maximumSubmitDepth) {
// No, recursively subdivide the lower half of the reference array with the current
// thread and return the result in the temporary array in descending order.
mergeSortTemporaryDescending(reference, temporary, low, mid, p, dim, maximumSubmitDepth, depth + 1);
// Then recursively subdivide the upper half of the reference array with the current
// thread and return the result in the temporary array in ascending order.
mergeSortTemporaryAscending(reference, temporary, mid + 1, high, p, dim, maximumSubmitDepth, depth + 1);
// Compare the results in the temporary array in ascending order and merge them into
// the reference array in descending order.
for (signed_size_t i = low, j = high, k = low; k <= high; ++k) {
reference[k] =
(superKeyCompare(temporary[i], temporary[j], p, dim) > 0) ? temporary[i++] : temporary[j--];
}
}
else {
// Yes, a child thread is available, so recursively subdivide the lower half of the reference
// array with a child thread and return the result in the temporary array in descending order.
auto sortFuture = async(launch::async, mergeSortTemporaryDescending, reference, temporary,
low, mid, p, dim, maximumSubmitDepth, depth + 1);
// And simultaneously, recursively subdivide the upper half of the reference array with
// the current thread and return the result in the temporary array in ascending order.
mergeSortTemporaryAscending(reference, temporary, mid + 1, high, p, dim, maximumSubmitDepth, depth + 1);
// Wait for the child thread to finish execution.
try {
sortFuture.get();
}
catch (exception const& e) {
throw runtime_error("\n\ncaught exception for sort future in mergeSortReferenceDescending\n");
}
// Compare the results in the temporary array in ascending order with a child thread
// and merge them into the lower half of the reference array in descending order.
auto mergeFuture =
async(launch::async, [&] {
for (signed_size_t i = low, j = high, k = low; k <= mid; ++k) {
reference[k] =
(superKeyCompare(temporary[i], temporary[j], p, dim) >= 0)
? temporary[i++] : temporary[j--];
}
});
// And simultaneously compare the results in the temporary array in descending order with the
// current thread and merge them into the upper half of the reference array in descending order.
for (signed_size_t i = mid, j = mid + 1, k = high; k > mid; --k) {
reference[k] =
(superKeyCompare(temporary[i], temporary[j], p, dim) < 0) ? temporary[i--] : temporary[j++];
}
// Wait for the child thread to finish execution.
try {
mergeFuture.get();
}
catch (exception const& e) {
throw runtime_error("\n\ncaught exception for merge future in mergeSortReferenceDescending\n");
}
}
}
else {
// Here is Jon Benley's implementation of insertion sort from "Programming Pearls", pp. 115-116,
// Addison-Wesley, 1999, that sorts in descending order and leaves the result in the reference array.
for (signed_size_t i = low + 1; i <= high; ++i) {
K * const tmp = reference[i];
signed_size_t j;
for (j = i; j > low && superKeyCompare(reference[j - 1], tmp, p, dim) < 0; --j) {
reference[j] = reference[j - 1];
}
reference[j] = tmp;
}
}
}
/*
* The mergeSortTemporaryAscending function recursively subdivides the reference array then
* merges the elements in ascending order and leaves the result in the temporary array.
*
* Calling parameters:
*
* reference - a K ** that represents the array of (x, y, z, w...) coordinates to sort
* temporary - a K ** temporary array into which to copy sorted results;
* this array must be as large as the reference array
* low - the start index of the region of the reference array to sort
* high - the end index of the region of the reference array to sort
* p - the sorting partition (x, y, z, w...)
* dim - the number of dimensions
* maximumSubmitDepth - the maximum tree depth at which a child task may be launched
* depth - the tree depth
*/
private:
static void mergeSortTemporaryAscending(K ** const reference,
K ** const temporary,
signed_size_t const low,
signed_size_t const high,
signed_size_t const p,
signed_size_t const dim,
signed_size_t const maximumSubmitDepth,
signed_size_t const depth) {
if (high - low > INSERTION_SORT_CUTOFF) {
// Avoid overflow when calculating the median.
signed_size_t const mid = low + ((high - low) >> 1);
// Subdivide the lower half of the array with a child thread at as many levels of subdivision as possible.
// Create the child threads as high in the subdivision hierarchy as possible for greater utilization.
// Is a child thread available to subdivide the lower half of the reference array?
if (maximumSubmitDepth < 0 || depth > maximumSubmitDepth) {
// No, recursively subdivide the lower half of the reference array with the current
// thread and return the result in the reference array in ascending order.
mergeSortReferenceAscending(reference, temporary, low, mid, p, dim, maximumSubmitDepth, depth + 1);
// Then recursively subdivide the upper half of the reference array with the current
// thread and return the result in the reference array in descending order.
mergeSortReferenceDescending(reference, temporary, mid + 1, high, p, dim, maximumSubmitDepth, depth + 1);
// Compare the results in the reference array in ascending order and merge them into
// the temporary array in ascending order.
for (signed_size_t i = low, j = high, k = low; k <= high; ++k) {
temporary[k] =
(superKeyCompare(reference[i], reference[j], p, dim) < 0) ? reference[i++] : reference[j--];
}
}
else {
// Yes, a child thread is available, so recursively subdivide the lower half of the reference
// array with a child thread and return the result in the reference array in ascending order.
auto sortFuture = async(launch::async, mergeSortReferenceAscending, reference, temporary,
low, mid, p, dim, maximumSubmitDepth, depth + 1);
// And simultaneously, recursively subdivide the upper half of the reference array with
// the current thread and return the result in the reference array in descending order.
mergeSortReferenceDescending(reference, temporary, mid + 1, high, p, dim, maximumSubmitDepth, depth + 1);
// Wait for the child thread to finish execution.
try {
sortFuture.get();
}
catch (exception const& e) {
throw runtime_error("\n\ncaught exception for sort future in mergeSortTemporaryAscending\n");
}
// Compare the results in the reference array in ascending order with a child thread
// and merge them into the lower half of the temporary array in ascending order.
auto mergeFuture =
async(launch::async, [&] {
for (signed_size_t i = low, j = high, k = low; k <= mid; ++k) {
temporary[k] =
(superKeyCompare(reference[i], reference[j], p, dim) <= 0)
? reference[i++] : reference[j--];
}
});
// And simultaneously compare the results in the reference array in descending order with the
// current thread and merge them into the upper half of the temporary array in ascending order.
for (signed_size_t i = mid, j = mid + 1, k = high; k > mid; --k) {
temporary[k] =
(superKeyCompare(reference[i], reference[j], p, dim) > 0) ? reference[i--] : reference[j++];
}
// Wait for the child thread to finish execution.
try {
mergeFuture.get();
}
catch (exception const& e) {
throw runtime_error("\n\ncaught exception for merge future in mergeSortTemporaryAscending\n");
}
}
}
else {
// Here is John Robinson's implementation of insertion sort that sorts in ascending order
// and leaves the result in the temporary array.
temporary[high] = reference[high];
signed_size_t i;
signed_size_t j; // MUST be signed because it can decrement to -1
for (j = high - 1; j >= low; --j) {
for (i = j; i < high; ++i) {
if (superKeyCompare(reference[j], temporary[i + 1], p, dim) > 0) {
temporary[i] = temporary[i + 1];
}
else {
break;
}
}
temporary[i] = reference[j];
}
}
}
/*
* The mergeSortTemporaryDescending function recursively subdivides the reference array
* then merges the elements in descending order and leaves the result in the reference array.
*
* Calling parameters:
*
* reference - a K ** that represents the array of (x, y, z, w...) coordinates to sort
* temporary - a K ** temporary array into which to copy sorted results;
* this array must be as large as the reference array
* low - the start index of the region of the reference array to sort
* high - the end index of the region of the reference array to sort
* p - the sorting partition (x, y, z, w...)
* dim - the number of dimensions
* maximumSubmitDepth - the maximum tree depth at which a child task may be launched
* depth - the tree depth
*/
private:
static void mergeSortTemporaryDescending(K ** const reference,
K ** const temporary,
signed_size_t const low,
signed_size_t const high,
signed_size_t const p,
signed_size_t const dim,
signed_size_t const maximumSubmitDepth,
signed_size_t const depth) {
if (high - low > INSERTION_SORT_CUTOFF) {
// Avoid overflow when calculating the median.
signed_size_t const mid = low + ((high - low) >> 1);
// Subdivide the lower half of the array with a child thread at as many levels of subdivision as possible.
// Create the child threads as high in the subdivision hierarchy as possible for greater utilization.
// Is a child thread available to subdivide the lower half of the reference array?
if (maximumSubmitDepth < 0 || depth > maximumSubmitDepth) {
// No, recursively subdivide the lower half of the reference array with the current
// thread and return the result in the reference array in descending order.
mergeSortReferenceDescending(reference, temporary, low, mid, p, dim, maximumSubmitDepth, depth + 1);
// Then recursively subdivide the upper half of the reference array with the current
// thread and return the result in the reference array in ascending order.
mergeSortReferenceAscending(reference, temporary, mid + 1, high, p, dim, maximumSubmitDepth, depth + 1);
// Compare the results in the reference array in ascending order and merge them into
// the temporary array in descending order.
for (signed_size_t i = low, j = high, k = low; k <= high; ++k) {
temporary[k] =
(superKeyCompare(reference[i], reference[j], p, dim) > 0) ? reference[i++] : reference[j--];
}
}
else {
// Yes, a child thread is available, so recursively subdivide the lower half of the reference
// array with a child thread and return the result in the reference array in descending order.
auto sortFuture = async(launch::async, mergeSortReferenceDescending, reference, temporary,
low, mid, p, dim, maximumSubmitDepth, depth + 1);
// And simultaneously, recursively subdivide the upper half of the reference array with
// the current thread and return the result in the reference array in ascending order.
mergeSortReferenceAscending(reference, temporary, mid + 1, high, p, dim, maximumSubmitDepth, depth + 1);
// Wait for the child thread to finish execution.
try {
sortFuture.get();
}
catch (exception const& e) {
throw runtime_error("\n\ncaught exception for sort future in mergeSortTemporaryDescending\n");
}
// Compare the results in the reference array in ascending order with a child thread
// and merge them into the lower half of the temporary array in descending order.
auto mergeFuture =
async(launch::async, [&] {
for (signed_size_t i = low, j = high, k = low; k <= mid; ++k) {
temporary[k] =
(superKeyCompare(reference[i], reference[j], p, dim) >= 0)
? reference[i++] : reference[j--];
}
});
// And simultaneously compare the results in the reference array in descending order with the
// current thread and merge them into the upper half of the temporary array in descending order.
for (signed_size_t i = mid, j = mid + 1, k = high; k > mid; --k) {
temporary[k] =
(superKeyCompare(reference[i], reference[j], p, dim) < 0) ? reference[i--] : reference[j++];
}
// Wait for the child thread to finish execution.
try {
mergeFuture.get();
}
catch (exception const& e) {
throw runtime_error("\n\ncaught exception for merge future in mergeSortTemporaryDescending\n");
}
}
}
else {
// Here is John Robinson's implementation of insertion sort that sorts in descending order
// and leaves the result in the temporary array.
temporary[high] = reference[high];
signed_size_t i;
signed_size_t j; // MUST be signed because it can decrement to -1
for (j = high - 1; j >= low; --j) {
for (i = j; i < high; ++i) {
if (superKeyCompare(reference[j], temporary[i + 1], p, dim) < 0) {
temporary[i] = temporary[i + 1];
}
else {
break;
}
}
temporary[i] = reference[j];
}
}
}
friend class KdNode<K>;
friend class KdTree<K>;
}; // class MergeSort
#endif // KD_TREE_MERGE_SORT_H