Skip to content

Commit 99b612d

Browse files
committed
Use single-parameter std::next/std::prev when possible
Some standard library implementations have special cases and call operators ++/-- instead of +=/-= when possible, which can lead to slightly better codegen for non-trivial random-access iterator types such as those of std::deque.
1 parent 12cfa60 commit 99b612d

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

include/gfx/timsort.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ template <typename RandomAccessIterator, typename Compare> class TimSort {
149149

150150
iter_t const pos = std::upper_bound(lo, start, pivot, compare);
151151
for (iter_t p = start; p > pos; --p) {
152-
*p = std::move(*(p - 1));
152+
*p = std::move(*std::prev(p));
153153
}
154154
*pos = std::move(pivot);
155155
}
@@ -158,20 +158,20 @@ template <typename RandomAccessIterator, typename Compare> class TimSort {
158158
static diff_t countRunAndMakeAscending(iter_t const lo, iter_t const hi, Compare compare) {
159159
GFX_TIMSORT_ASSERT(lo < hi);
160160

161-
iter_t runHi = lo + 1;
161+
auto runHi = std::next(lo);
162162
if (runHi == hi) {
163163
return 1;
164164
}
165165

166166
if (compare(*runHi, *lo)) { // decreasing
167167
do {
168168
++runHi;
169-
} while (runHi < hi && compare(*runHi, *(runHi - 1)));
169+
} while (runHi < hi && compare(*runHi, *std::prev(runHi)));
170170
std::reverse(lo, runHi);
171171
} else { // non-decreasing
172172
do {
173173
++runHi;
174-
} while (runHi < hi && !compare(*runHi, *(runHi - 1)));
174+
} while (runHi < hi && !compare(*runHi, *std::prev(runHi)));
175175
}
176176

177177
return runHi - lo;
@@ -382,13 +382,13 @@ template <typename RandomAccessIterator, typename Compare> class TimSort {
382382
static void rotateLeft(iter_t first, iter_t last)
383383
{
384384
value_t tmp = std::move(*first);
385-
iter_t last_1 = std::move(first + 1, last, first);
385+
auto last_1 = std::move(std::next(first), last, first);
386386
*last_1 = std::move(tmp);
387387
}
388388

389389
static void rotateRight(iter_t first, iter_t last)
390390
{
391-
iter_t last_1 = last - 1;
391+
auto last_1 = std::prev(last);
392392
value_t tmp = std::move(*last_1);
393393
std::move_backward(first, last_1, last);
394394
*first = std::move(tmp);
@@ -598,12 +598,12 @@ template <typename RandomAccessIterator, typename Compare> class TimSort {
598598
goto epilogue;
599599
}
600600

601-
count2 = len2 - gallopLeft(*(cursor1 - 1), tmp_.begin(), len2, len2 - 1, compare);
601+
count2 = len2 - gallopLeft(*std::prev(cursor1), tmp_.begin(), len2, len2 - 1, compare);
602602
if (count2 != 0) {
603603
dest -= count2;
604604
cursor2 -= count2;
605605
len2 -= count2;
606-
std::move(cursor2 + 1, cursor2 + (1 + count2), dest + 1);
606+
std::move(std::next(cursor2), cursor2 + (1 + count2), std::next(dest));
607607
if (len2 <= 1) {
608608
goto epilogue;
609609
}

0 commit comments

Comments
 (0)