Skip to content

Commit f61add2

Browse files
committed
Split assertions for more precise diagnostics
1 parent e451bbb commit f61add2

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

include/gfx/timsort.hpp

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ template <typename RandomAccessIterator, typename Compare> class TimSort {
130130
std::vector<run<RandomAccessIterator> > pending_;
131131

132132
static void binarySort(iter_t const lo, iter_t const hi, iter_t start, Compare compare) {
133-
GFX_TIMSORT_ASSERT(lo <= start && start <= hi);
133+
GFX_TIMSORT_ASSERT(lo <= start);
134+
GFX_TIMSORT_ASSERT(start <= hi);
134135
if (start == lo) {
135136
++start;
136137
}
@@ -229,7 +230,8 @@ template <typename RandomAccessIterator, typename Compare> class TimSort {
229230
iter_t base2 = pending_[i + 1].base;
230231
diff_t len2 = pending_[i + 1].len;
231232

232-
GFX_TIMSORT_ASSERT(len1 > 0 && len2 > 0);
233+
GFX_TIMSORT_ASSERT(len1 > 0);
234+
GFX_TIMSORT_ASSERT(len2 > 0);
233235
GFX_TIMSORT_ASSERT(base1 + len1 == base2);
234236

235237
pending_[i].len = len1 + len2;
@@ -265,7 +267,9 @@ template <typename RandomAccessIterator, typename Compare> class TimSort {
265267

266268
template <typename Iter>
267269
diff_t gallopLeft(ref_t key, Iter const base, diff_t const len, diff_t const hint, Compare compare) {
268-
GFX_TIMSORT_ASSERT(len > 0 && hint >= 0 && hint < len);
270+
GFX_TIMSORT_ASSERT(len > 0);
271+
GFX_TIMSORT_ASSERT(hint >= 0);
272+
GFX_TIMSORT_ASSERT(hint < len);
269273

270274
diff_t lastOfs = 0;
271275
diff_t ofs = 1;
@@ -304,14 +308,18 @@ template <typename RandomAccessIterator, typename Compare> class TimSort {
304308
lastOfs = hint - ofs;
305309
ofs = hint - tmp;
306310
}
307-
GFX_TIMSORT_ASSERT(-1 <= lastOfs && lastOfs < ofs && ofs <= len);
311+
GFX_TIMSORT_ASSERT(-1 <= lastOfs);
312+
GFX_TIMSORT_ASSERT(lastOfs < ofs);
313+
GFX_TIMSORT_ASSERT(ofs <= len);
308314

309315
return std::lower_bound(base + (lastOfs + 1), base + ofs, key, compare) - base;
310316
}
311317

312318
template <typename Iter>
313319
diff_t gallopRight(ref_t key, Iter const base, diff_t const len, diff_t const hint, Compare compare) {
314-
GFX_TIMSORT_ASSERT(len > 0 && hint >= 0 && hint < len);
320+
GFX_TIMSORT_ASSERT(len > 0);
321+
GFX_TIMSORT_ASSERT(hint >= 0);
322+
GFX_TIMSORT_ASSERT(hint < len);
315323

316324
diff_t ofs = 1;
317325
diff_t lastOfs = 0;
@@ -350,7 +358,9 @@ template <typename RandomAccessIterator, typename Compare> class TimSort {
350358
lastOfs += hint;
351359
ofs += hint;
352360
}
353-
GFX_TIMSORT_ASSERT(-1 <= lastOfs && lastOfs < ofs && ofs <= len);
361+
GFX_TIMSORT_ASSERT(-1 <= lastOfs);
362+
GFX_TIMSORT_ASSERT(lastOfs < ofs);
363+
GFX_TIMSORT_ASSERT(ofs <= len);
354364

355365
return std::upper_bound(base + (lastOfs + 1), base + ofs, key, compare) - base;
356366
}
@@ -372,7 +382,9 @@ template <typename RandomAccessIterator, typename Compare> class TimSort {
372382

373383

374384
void mergeLo(iter_t const base1, diff_t len1, iter_t const base2, diff_t len2, Compare compare) {
375-
GFX_TIMSORT_ASSERT(len1 > 0 && len2 > 0 && base1 + len1 == base2);
385+
GFX_TIMSORT_ASSERT(len1 > 0);
386+
GFX_TIMSORT_ASSERT(len2 > 0);
387+
GFX_TIMSORT_ASSERT(base1 + len1 == base2);
376388

377389
if (len1 == 1) {
378390
return rotateLeft(base1, base2 + len2);
@@ -398,7 +410,8 @@ template <typename RandomAccessIterator, typename Compare> class TimSort {
398410
diff_t count2 = 0;
399411

400412
do {
401-
GFX_TIMSORT_ASSERT(len1 > 1 && len2 > 0);
413+
GFX_TIMSORT_ASSERT(len1 > 1);
414+
GFX_TIMSORT_ASSERT(len2 > 0);
402415

403416
if (compare(*cursor2, *cursor1)) {
404417
*(dest++) = GFX_TIMSORT_MOVE(*(cursor2++));
@@ -418,7 +431,8 @@ template <typename RandomAccessIterator, typename Compare> class TimSort {
418431
} while ((count1 | count2) < minGallop);
419432

420433
do {
421-
GFX_TIMSORT_ASSERT(len1 > 1 && len2 > 0);
434+
GFX_TIMSORT_ASSERT(len1 > 1);
435+
GFX_TIMSORT_ASSERT(len2 > 0);
422436

423437
count1 = gallopRight(*cursor2, cursor1, len1, 0, compare);
424438
if (count1 != 0) {
@@ -477,7 +491,9 @@ template <typename RandomAccessIterator, typename Compare> class TimSort {
477491
}
478492

479493
void mergeHi(iter_t const base1, diff_t len1, iter_t const base2, diff_t len2, Compare compare) {
480-
GFX_TIMSORT_ASSERT(len1 > 0 && len2 > 0 && base1 + len1 == base2);
494+
GFX_TIMSORT_ASSERT(len1 > 0);
495+
GFX_TIMSORT_ASSERT(len2 > 0);
496+
GFX_TIMSORT_ASSERT(base1 + len1 == base2);
481497

482498
if (len1 == 1) {
483499
return rotateLeft(base1, base2 + len2);
@@ -509,7 +525,8 @@ template <typename RandomAccessIterator, typename Compare> class TimSort {
509525
--cursor1;
510526

511527
do {
512-
GFX_TIMSORT_ASSERT(len1 > 0 && len2 > 1);
528+
GFX_TIMSORT_ASSERT(len1 > 0);
529+
GFX_TIMSORT_ASSERT(len2 > 1);
513530

514531
if (compare(*cursor2, *cursor1)) {
515532
*(dest--) = GFX_TIMSORT_MOVE(*cursor1);
@@ -532,7 +549,8 @@ template <typename RandomAccessIterator, typename Compare> class TimSort {
532549
++cursor1; // See comment before the loop
533550

534551
do {
535-
GFX_TIMSORT_ASSERT(len1 > 0 && len2 > 1);
552+
GFX_TIMSORT_ASSERT(len1 > 0);
553+
GFX_TIMSORT_ASSERT(len2 > 1);
536554

537555
count1 = len1 - gallopRight(*cursor2, base1, len1, len1 - 1, compare);
538556
if (count1 != 0) {

0 commit comments

Comments
 (0)