forked from timsort/cpp-TimSort
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimsort.hpp
More file actions
745 lines (617 loc) · 22.6 KB
/
Copy pathtimsort.hpp
File metadata and controls
745 lines (617 loc) · 22.6 KB
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
/*
* C++ implementation of timsort
*
* ported from Python's and OpenJDK's:
* - http://svn.python.org/projects/python/trunk/Objects/listobject.c
* - http://cr.openjdk.java.net/~martin/webrevs/openjdk7/timsort/raw_files/new/src/share/classes/java/util/TimSort.java
*
* Copyright (c) 2011 Fuji, Goro (gfx) <gfuji@cpan.org>.
* Copyright (c) 2019-2020 Morwenn.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef GFX_TIMSORT_HPP
#define GFX_TIMSORT_HPP
#include <algorithm>
#include <functional>
#include <iterator>
#include <utility>
#include <vector>
// Semantic versioning macros
#define GFX_TIMSORT_VERSION_MAJOR 2
#define GFX_TIMSORT_VERSION_MINOR 0
#define GFX_TIMSORT_VERSION_PATCH 1
// Diagnostic selection macros
#ifdef GFX_TIMSORT_ENABLE_ASSERT
# include <cassert>
# define GFX_TIMSORT_ASSERT(expr) assert(expr)
#else
# define GFX_TIMSORT_ASSERT(expr) ((void)0)
#endif
#ifdef GFX_TIMSORT_ENABLE_LOG
# include <iostream>
# define GFX_TIMSORT_LOG(expr) (std::clog << "# " << __func__ << ": " << expr << std::endl)
#else
# define GFX_TIMSORT_LOG(expr) ((void)0)
#endif
namespace gfx {
// ---------------------------------------
// Implementation details
// ---------------------------------------
namespace detail {
// Equivalent to C++20 std::identity
struct identity {
template <typename T>
constexpr T&& operator()(T&& value) const noexcept
{
return std::forward<T>(value);
}
};
// Merge a predicate and a projection function
template <typename Compare, typename Projection>
struct projection_compare {
projection_compare(Compare comp, Projection proj) : compare(comp), projection(proj) {
}
template <typename T, typename U>
bool operator()(T &&lhs, U &&rhs) {
#ifdef __cpp_lib_invoke
return static_cast<bool>(std::invoke(compare,
std::invoke(projection, std::forward<T>(lhs)),
std::invoke(projection, std::forward<U>(rhs))
));
#else
return static_cast<bool>(compare(
projection(std::forward<T>(lhs)),
projection(std::forward<U>(rhs))
));
#endif
}
Compare compare;
Projection projection;
};
template <typename Iterator> struct run {
typedef typename std::iterator_traits<Iterator>::difference_type diff_t;
Iterator base;
diff_t len;
run(Iterator b, diff_t l) : base(b), len(l) {
}
};
template <typename RandomAccessIterator, typename Compare> class TimSort {
typedef RandomAccessIterator iter_t;
typedef typename std::iterator_traits<iter_t>::value_type value_t;
typedef typename std::iterator_traits<iter_t>::reference ref_t;
typedef typename std::iterator_traits<iter_t>::difference_type diff_t;
static const int MIN_MERGE = 32;
static const int MIN_GALLOP = 7;
int minGallop_; // default to MIN_GALLOP
std::vector<value_t> tmp_; // temp storage for merges
typedef typename std::vector<value_t>::iterator tmp_iter_t;
std::vector<run<RandomAccessIterator> > pending_;
static void binarySort(iter_t const lo, iter_t const hi, iter_t start, Compare compare) {
GFX_TIMSORT_ASSERT(lo <= start);
GFX_TIMSORT_ASSERT(start <= hi);
if (start == lo) {
++start;
}
for (; start < hi; ++start) {
GFX_TIMSORT_ASSERT(lo <= start);
value_t pivot = std::move(*start);
iter_t const pos = std::upper_bound(lo, start, pivot, compare);
for (iter_t p = start; p > pos; --p) {
*p = std::move(*(p - 1));
}
*pos = std::move(pivot);
}
}
static diff_t countRunAndMakeAscending(iter_t const lo, iter_t const hi, Compare compare) {
GFX_TIMSORT_ASSERT(lo < hi);
iter_t runHi = lo + 1;
if (runHi == hi) {
return 1;
}
if (compare(*runHi, *lo)) { // decreasing
do {
++runHi;
} while (runHi < hi && compare(*runHi, *(runHi - 1)));
std::reverse(lo, runHi);
} else { // non-decreasing
do {
++runHi;
} while (runHi < hi && !compare(*runHi, *(runHi - 1)));
}
return runHi - lo;
}
static diff_t minRunLength(diff_t n) {
GFX_TIMSORT_ASSERT(n >= 0);
diff_t r = 0;
while (n >= 2 * MIN_MERGE) {
r |= (n & 1);
n >>= 1;
}
return n + r;
}
TimSort() : minGallop_(MIN_GALLOP) {
}
// Silence GCC -Winline warning
~TimSort() {}
void pushRun(iter_t const runBase, diff_t const runLen) {
pending_.push_back(run<iter_t>(runBase, runLen));
}
void mergeCollapse(Compare compare) {
while (pending_.size() > 1) {
diff_t n = pending_.size() - 2;
if ((n > 0 && pending_[n - 1].len <= pending_[n].len + pending_[n + 1].len) ||
(n > 1 && pending_[n - 2].len <= pending_[n - 1].len + pending_[n].len)) {
if (pending_[n - 1].len < pending_[n + 1].len) {
--n;
}
mergeAt(n, compare);
} else if (pending_[n].len <= pending_[n + 1].len) {
mergeAt(n, compare);
} else {
break;
}
}
}
void mergeForceCollapse(Compare compare) {
while (pending_.size() > 1) {
diff_t n = pending_.size() - 2;
if (n > 0 && pending_[n - 1].len < pending_[n + 1].len) {
--n;
}
mergeAt(n, compare);
}
}
void mergeAt(diff_t const i, Compare compare) {
diff_t const stackSize = pending_.size();
GFX_TIMSORT_ASSERT(stackSize >= 2);
GFX_TIMSORT_ASSERT(i >= 0);
GFX_TIMSORT_ASSERT(i == stackSize - 2 || i == stackSize - 3);
iter_t base1 = pending_[i].base;
diff_t len1 = pending_[i].len;
iter_t base2 = pending_[i + 1].base;
diff_t len2 = pending_[i + 1].len;
GFX_TIMSORT_ASSERT(len1 > 0);
GFX_TIMSORT_ASSERT(len2 > 0);
GFX_TIMSORT_ASSERT(base1 + len1 == base2);
pending_[i].len = len1 + len2;
if (i == stackSize - 3) {
pending_[i + 1] = pending_[i + 2];
}
pending_.pop_back();
diff_t const k = gallopRight(*base2, base1, len1, 0, compare);
GFX_TIMSORT_ASSERT(k >= 0);
base1 += k;
len1 -= k;
if (len1 == 0) {
return;
}
len2 = gallopLeft(*(base1 + (len1 - 1)), base2, len2, len2 - 1, compare);
GFX_TIMSORT_ASSERT(len2 >= 0);
if (len2 == 0) {
return;
}
if (len1 <= len2) {
mergeLo(base1, len1, base2, len2, compare);
} else {
mergeHi(base1, len1, base2, len2, compare);
}
}
template <typename Iter>
static diff_t gallopLeft(ref_t key, Iter const base, diff_t const len,
diff_t const hint, Compare compare) {
GFX_TIMSORT_ASSERT(len > 0);
GFX_TIMSORT_ASSERT(hint >= 0);
GFX_TIMSORT_ASSERT(hint < len);
diff_t lastOfs = 0;
diff_t ofs = 1;
if (compare(*(base + hint), key)) {
diff_t const maxOfs = len - hint;
while (ofs < maxOfs && compare(*(base + (hint + ofs)), key)) {
lastOfs = ofs;
ofs = (ofs << 1) + 1;
if (ofs <= 0) { // int overflow
ofs = maxOfs;
}
}
if (ofs > maxOfs) {
ofs = maxOfs;
}
lastOfs += hint;
ofs += hint;
} else {
diff_t const maxOfs = hint + 1;
while (ofs < maxOfs && !compare(*(base + (hint - ofs)), key)) {
lastOfs = ofs;
ofs = (ofs << 1) + 1;
if (ofs <= 0) {
ofs = maxOfs;
}
}
if (ofs > maxOfs) {
ofs = maxOfs;
}
diff_t const tmp = lastOfs;
lastOfs = hint - ofs;
ofs = hint - tmp;
}
GFX_TIMSORT_ASSERT(-1 <= lastOfs);
GFX_TIMSORT_ASSERT(lastOfs < ofs);
GFX_TIMSORT_ASSERT(ofs <= len);
return std::lower_bound(base + (lastOfs + 1), base + ofs, key, compare) - base;
}
template <typename Iter>
static diff_t gallopRight(ref_t key, Iter const base, diff_t const len,
diff_t const hint, Compare compare) {
GFX_TIMSORT_ASSERT(len > 0);
GFX_TIMSORT_ASSERT(hint >= 0);
GFX_TIMSORT_ASSERT(hint < len);
diff_t ofs = 1;
diff_t lastOfs = 0;
if (compare(key, *(base + hint))) {
diff_t const maxOfs = hint + 1;
while (ofs < maxOfs && compare(key, *(base + (hint - ofs)))) {
lastOfs = ofs;
ofs = (ofs << 1) + 1;
if (ofs <= 0) {
ofs = maxOfs;
}
}
if (ofs > maxOfs) {
ofs = maxOfs;
}
diff_t const tmp = lastOfs;
lastOfs = hint - ofs;
ofs = hint - tmp;
} else {
diff_t const maxOfs = len - hint;
while (ofs < maxOfs && !compare(key, *(base + (hint + ofs)))) {
lastOfs = ofs;
ofs = (ofs << 1) + 1;
if (ofs <= 0) { // int overflow
ofs = maxOfs;
}
}
if (ofs > maxOfs) {
ofs = maxOfs;
}
lastOfs += hint;
ofs += hint;
}
GFX_TIMSORT_ASSERT(-1 <= lastOfs);
GFX_TIMSORT_ASSERT(lastOfs < ofs);
GFX_TIMSORT_ASSERT(ofs <= len);
return std::upper_bound(base + (lastOfs + 1), base + ofs, key, compare) - base;
}
static void rotateLeft(iter_t first, iter_t last)
{
value_t tmp = std::move(*first);
iter_t last_1 = std::move(first + 1, last, first);
*last_1 = std::move(tmp);
}
static void rotateRight(iter_t first, iter_t last)
{
iter_t last_1 = last - 1;
value_t tmp = std::move(*last_1);
std::move_backward(first, last_1, last);
*first = std::move(tmp);
}
void mergeLo(iter_t const base1, diff_t len1, iter_t const base2, diff_t len2, Compare compare) {
GFX_TIMSORT_ASSERT(len1 > 0);
GFX_TIMSORT_ASSERT(len2 > 0);
GFX_TIMSORT_ASSERT(base1 + len1 == base2);
if (len1 == 1) {
return rotateLeft(base1, base2 + len2);
}
if (len2 == 1) {
return rotateRight(base1, base2 + len2);
}
copy_to_tmp(base1, len1);
tmp_iter_t cursor1 = tmp_.begin();
iter_t cursor2 = base2;
iter_t dest = base1;
*dest = std::move(*cursor2);
++cursor2;
++dest;
--len2;
int minGallop(minGallop_);
// outer:
while (true) {
diff_t count1 = 0;
diff_t count2 = 0;
do {
GFX_TIMSORT_ASSERT(len1 > 1);
GFX_TIMSORT_ASSERT(len2 > 0);
if (compare(*cursor2, *cursor1)) {
*dest = std::move(*cursor2);
++cursor2;
++dest;
++count2;
count1 = 0;
if (--len2 == 0) {
goto epilogue;
}
} else {
*dest = std::move(*cursor1);
++cursor1;
++dest;
++count1;
count2 = 0;
if (--len1 == 1) {
goto epilogue;
}
}
} while ((count1 | count2) < minGallop);
do {
GFX_TIMSORT_ASSERT(len1 > 1);
GFX_TIMSORT_ASSERT(len2 > 0);
count1 = gallopRight(*cursor2, cursor1, len1, 0, compare);
if (count1 != 0) {
std::move_backward(cursor1, cursor1 + count1, dest + count1);
dest += count1;
cursor1 += count1;
len1 -= count1;
if (len1 <= 1) {
goto epilogue;
}
}
*dest = std::move(*cursor2);
++cursor2;
++dest;
if (--len2 == 0) {
goto epilogue;
}
count2 = gallopLeft(*cursor1, cursor2, len2, 0, compare);
if (count2 != 0) {
std::move(cursor2, cursor2 + count2, dest);
dest += count2;
cursor2 += count2;
len2 -= count2;
if (len2 == 0) {
goto epilogue;
}
}
*dest = std::move(*cursor1);
++cursor1;
++dest;
if (--len1 == 1) {
goto epilogue;
}
--minGallop;
} while ((count1 >= MIN_GALLOP) | (count2 >= MIN_GALLOP));
if (minGallop < 0) {
minGallop = 0;
}
minGallop += 2;
} // end of "outer" loop
epilogue: // merge what is left from either cursor1 or cursor2
minGallop_ = (std::min)(minGallop, 1);
if (len1 == 1) {
GFX_TIMSORT_ASSERT(len2 > 0);
std::move(cursor2, cursor2 + len2, dest);
*(dest + len2) = std::move(*cursor1);
} else {
GFX_TIMSORT_ASSERT(len1 != 0 && "Comparison function violates its general contract");
GFX_TIMSORT_ASSERT(len2 == 0);
GFX_TIMSORT_ASSERT(len1 > 1);
std::move(cursor1, cursor1 + len1, dest);
}
}
void mergeHi(iter_t const base1, diff_t len1, iter_t const base2, diff_t len2, Compare compare) {
GFX_TIMSORT_ASSERT(len1 > 0);
GFX_TIMSORT_ASSERT(len2 > 0);
GFX_TIMSORT_ASSERT(base1 + len1 == base2);
if (len1 == 1) {
return rotateLeft(base1, base2 + len2);
}
if (len2 == 1) {
return rotateRight(base1, base2 + len2);
}
copy_to_tmp(base2, len2);
iter_t cursor1 = base1 + len1;
tmp_iter_t cursor2 = tmp_.begin() + (len2 - 1);
iter_t dest = base2 + (len2 - 1);
*dest = std::move(*(--cursor1));
--dest;
--len1;
int minGallop(minGallop_);
// outer:
while (true) {
diff_t count1 = 0;
diff_t count2 = 0;
// The next loop is a hot path of the algorithm, so we decrement
// eagerly the cursor so that it always points directly to the value
// to compare, but we have to implement some trickier logic to make
// sure that it points to the next value again by the end of said loop
--cursor1;
do {
GFX_TIMSORT_ASSERT(len1 > 0);
GFX_TIMSORT_ASSERT(len2 > 1);
if (compare(*cursor2, *cursor1)) {
*dest = std::move(*cursor1);
--dest;
++count1;
count2 = 0;
if (--len1 == 0) {
goto epilogue;
}
--cursor1;
} else {
*dest = std::move(*cursor2);
--cursor2;
--dest;
++count2;
count1 = 0;
if (--len2 == 1) {
++cursor1; // See comment before the loop
goto epilogue;
}
}
} while ((count1 | count2) < minGallop);
++cursor1; // See comment before the loop
do {
GFX_TIMSORT_ASSERT(len1 > 0);
GFX_TIMSORT_ASSERT(len2 > 1);
count1 = len1 - gallopRight(*cursor2, base1, len1, len1 - 1, compare);
if (count1 != 0) {
dest -= count1;
cursor1 -= count1;
len1 -= count1;
std::move_backward(cursor1, cursor1 + count1, dest + (1 + count1));
if (len1 == 0) {
goto epilogue;
}
}
*dest = std::move(*cursor2);
--cursor2;
--dest;
if (--len2 == 1) {
goto epilogue;
}
count2 = len2 - gallopLeft(*(cursor1 - 1), tmp_.begin(), len2, len2 - 1, compare);
if (count2 != 0) {
dest -= count2;
cursor2 -= count2;
len2 -= count2;
std::move(cursor2 + 1, cursor2 + (1 + count2), dest + 1);
if (len2 <= 1) {
goto epilogue;
}
}
*dest = std::move(*(--cursor1));
--dest;
if (--len1 == 0) {
goto epilogue;
}
--minGallop;
} while ((count1 >= MIN_GALLOP) | (count2 >= MIN_GALLOP));
if (minGallop < 0) {
minGallop = 0;
}
minGallop += 2;
} // end of "outer" loop
epilogue: // merge what is left from either cursor1 or cursor2
minGallop_ = (std::min)(minGallop, 1);
if (len2 == 1) {
GFX_TIMSORT_ASSERT(len1 > 0);
dest -= len1;
std::move_backward(cursor1 - len1, cursor1, dest + (1 + len1));
*dest = std::move(*cursor2);
} else {
GFX_TIMSORT_ASSERT(len2 != 0 && "Comparison function violates its general contract");
GFX_TIMSORT_ASSERT(len1 == 0);
GFX_TIMSORT_ASSERT(len2 > 1);
std::move(tmp_.begin(), tmp_.begin() + len2, dest - (len2 - 1));
}
}
void copy_to_tmp(iter_t const begin, diff_t len) {
tmp_.assign(std::make_move_iterator(begin),
std::make_move_iterator(begin + len));
}
public:
static void sort(iter_t const lo, iter_t const hi, Compare compare) {
GFX_TIMSORT_ASSERT(lo <= hi);
diff_t nRemaining = (hi - lo);
if (nRemaining < 2) {
return; // nothing to do
}
if (nRemaining < MIN_MERGE) {
diff_t const initRunLen = countRunAndMakeAscending(lo, hi, compare);
GFX_TIMSORT_LOG("initRunLen: " << initRunLen);
binarySort(lo, hi, lo + initRunLen, compare);
return;
}
TimSort ts;
diff_t const minRun = minRunLength(nRemaining);
iter_t cur = lo;
do {
diff_t runLen = countRunAndMakeAscending(cur, hi, compare);
if (runLen < minRun) {
diff_t const force = (std::min)(nRemaining, minRun);
binarySort(cur, cur + force, cur + runLen, compare);
runLen = force;
}
ts.pushRun(cur, runLen);
ts.mergeCollapse(compare);
cur += runLen;
nRemaining -= runLen;
} while (nRemaining != 0);
GFX_TIMSORT_ASSERT(cur == hi);
ts.mergeForceCollapse(compare);
GFX_TIMSORT_ASSERT(ts.pending_.size() == 1);
GFX_TIMSORT_LOG("size: " << (hi - lo) << " tmp_.size(): " << ts.tmp_.size()
<< " pending_.size(): " << ts.pending_.size());
}
};
} // namespace detail
// ---------------------------------------
// Public interface implementation
// ---------------------------------------
/**
* Stably sorts a range with a comparison function and a projection function.
*/
template <typename RandomAccessIterator, typename Compare, typename Projection>
void timsort(RandomAccessIterator const first, RandomAccessIterator const last,
Compare compare, Projection projection) {
typedef detail::projection_compare<Compare, Projection> compare_t;
compare_t comp(std::move(compare), std::move(projection));
detail::TimSort<RandomAccessIterator, compare_t>::sort(first, last, std::move(comp));
}
/**
* Same as std::stable_sort(first, last, compare).
*/
template <typename RandomAccessIterator, typename Compare>
void timsort(RandomAccessIterator const first, RandomAccessIterator const last, Compare compare) {
gfx::timsort(first, last, compare, detail::identity());
}
/**
* Same as std::stable_sort(first, last).
*/
template <typename RandomAccessIterator>
void timsort(RandomAccessIterator const first, RandomAccessIterator const last) {
typedef typename std::iterator_traits<RandomAccessIterator>::value_type value_type;
gfx::timsort(first, last, std::less<value_type>(), detail::identity());
}
/**
* Stably sorts a range with a comparison function and a projection function.
*/
template <typename RandomAccessRange, typename Compare, typename Projection>
void timsort(RandomAccessRange &range, Compare compare, Projection projection) {
gfx::timsort(std::begin(range), std::end(range), compare, projection);
}
/**
* Same as std::stable_sort(std::begin(range), std::end(range), compare).
*/
template <typename RandomAccessRange, typename Compare>
void timsort(RandomAccessRange &range, Compare compare) {
gfx::timsort(std::begin(range), std::end(range), compare);
}
/**
* Same as std::stable_sort(std::begin(range), std::end(range)).
*/
template <typename RandomAccessRange>
void timsort(RandomAccessRange &range) {
gfx::timsort(std::begin(range), std::end(range));
}
} // namespace gfx
#undef GFX_TIMSORT_ENABLE_ASSERT
#undef GFX_TIMSORT_ASSERT
#undef GFX_TIMSORT_ENABLE_LOG
#undef GFX_TIMSORT_LOG
#endif // GFX_TIMSORT_HPP