Skip to content

Commit 6b9ff4d

Browse files
committed
Modernize/simplify internals a bit
1 parent e02eb81 commit 6b9ff4d

File tree

1 file changed

+22
-31
lines changed

1 file changed

+22
-31
lines changed

include/gfx/timsort.hpp

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,9 @@ class TimSort {
9595
static constexpr int MIN_MERGE = 32;
9696
static constexpr int MIN_GALLOP = 7;
9797

98-
int minGallop_; // default to MIN_GALLOP
99-
98+
int minGallop_ = MIN_GALLOP;
10099
std::vector<value_t> tmp_; // temp storage for merges
101-
using tmp_iter_t = typename std::vector<value_t>::iterator;
102-
103-
std::vector<run<RandomAccessIterator> > pending_;
100+
std::vector<run<RandomAccessIterator>> pending_;
104101

105102
template <typename Compare, typename Projection>
106103
static void binarySort(iter_t const lo, iter_t const hi, iter_t start,
@@ -122,7 +119,7 @@ class TimSort {
122119
Compare comp, Projection proj) {
123120
GFX_TIMSORT_ASSERT(lo < hi);
124121

125-
iter_t runHi = std::ranges::next(lo);
122+
auto runHi = std::ranges::next(lo);
126123
if (runHi == hi) {
127124
return 1;
128125
}
@@ -156,14 +153,8 @@ class TimSort {
156153
return n + r;
157154
}
158155

159-
TimSort() : minGallop_(MIN_GALLOP) {
160-
}
161-
162-
// Silence GCC -Winline warning
163-
~TimSort() {}
164-
165156
void pushRun(iter_t const runBase, diff_t const runLen) {
166-
pending_.push_back(run<iter_t>(runBase, runLen));
157+
pending_.emplace_back(runBase, runLen);
167158
}
168159

169160
template <typename Compare, typename Projection>
@@ -204,10 +195,10 @@ class TimSort {
204195
GFX_TIMSORT_ASSERT(i >= 0);
205196
GFX_TIMSORT_ASSERT(i == stackSize - 2 || i == stackSize - 3);
206197

207-
iter_t base1 = pending_[i].base;
208-
diff_t len1 = pending_[i].len;
209-
iter_t base2 = pending_[i + 1].base;
210-
diff_t len2 = pending_[i + 1].len;
198+
auto base1 = pending_[i].base;
199+
auto len1 = pending_[i].len;
200+
auto base2 = pending_[i + 1].base;
201+
auto len2 = pending_[i + 1].len;
211202

212203
pending_[i].len = len1 + len2;
213204

@@ -227,7 +218,7 @@ class TimSort {
227218
GFX_TIMSORT_ASSERT(len2 > 0);
228219
GFX_TIMSORT_ASSERT(base1 + len1 == base2);
229220

230-
diff_t const k = gallopRight(std::invoke(proj, *base2), base1, len1, 0, comp, proj);
221+
auto k = gallopRight(std::invoke(proj, *base2), base1, len1, 0, comp, proj);
231222
GFX_TIMSORT_ASSERT(k >= 0);
232223

233224
base1 += k;
@@ -261,7 +252,7 @@ class TimSort {
261252
diff_t ofs = 1;
262253

263254
if (std::invoke(comp, std::invoke(proj, base[hint]), key)) {
264-
diff_t const maxOfs = len - hint;
255+
auto maxOfs = len - hint;
265256
while (ofs < maxOfs && std::invoke(comp, std::invoke(proj, base[hint + ofs]), key)) {
266257
lastOfs = ofs;
267258
ofs = (ofs << 1) + 1;
@@ -381,9 +372,9 @@ class TimSort {
381372

382373
move_to_tmp(base1, len1);
383374

384-
tmp_iter_t cursor1 = tmp_.begin();
385-
iter_t cursor2 = base2;
386-
iter_t dest = base1;
375+
auto cursor1 = tmp_.begin();
376+
auto cursor2 = base2;
377+
auto dest = base1;
387378

388379
*dest = std::ranges::iter_move(cursor2);
389380
++cursor2;
@@ -502,9 +493,9 @@ class TimSort {
502493

503494
move_to_tmp(base2, len2);
504495

505-
iter_t cursor1 = base1 + len1;
506-
tmp_iter_t cursor2 = tmp_.begin() + (len2 - 1);
507-
iter_t dest = base2 + (len2 - 1);
496+
auto cursor1 = base1 + len1;
497+
auto cursor2 = tmp_.begin() + (len2 - 1);
498+
auto dest = base2 + (len2 - 1);
508499

509500
*dest = std::ranges::iter_move(--cursor1);
510501
--dest;
@@ -646,26 +637,26 @@ class TimSort {
646637
static void sort(iter_t const lo, iter_t const hi, Compare comp, Projection proj) {
647638
GFX_TIMSORT_ASSERT(lo <= hi);
648639

649-
diff_t nRemaining = (hi - lo);
640+
auto nRemaining = hi - lo;
650641
if (nRemaining < 2) {
651642
return; // nothing to do
652643
}
653644

654645
if (nRemaining < MIN_MERGE) {
655-
diff_t const initRunLen = countRunAndMakeAscending(lo, hi, comp, proj);
646+
auto initRunLen = countRunAndMakeAscending(lo, hi, comp, proj);
656647
GFX_TIMSORT_LOG("initRunLen: " << initRunLen);
657648
binarySort(lo, hi, lo + initRunLen, comp, proj);
658649
return;
659650
}
660651

661652
TimSort ts;
662-
diff_t const minRun = minRunLength(nRemaining);
663-
iter_t cur = lo;
653+
auto minRun = minRunLength(nRemaining);
654+
auto cur = lo;
664655
do {
665-
diff_t runLen = countRunAndMakeAscending(cur, hi, comp, proj);
656+
auto runLen = countRunAndMakeAscending(cur, hi, comp, proj);
666657

667658
if (runLen < minRun) {
668-
diff_t const force = (std::min)(nRemaining, minRun);
659+
auto force = (std::min)(nRemaining, minRun);
669660
binarySort(cur, cur + force, cur + runLen, comp, proj);
670661
runLen = force;
671662
}

0 commit comments

Comments
 (0)