Commit dc2d83a
committed
Fix set::min()/max() comparator bug and non-const operator[] (#28)
set::min()/max() used std::min_element/std::max_element, which rank by
operator< instead of the set's TCompare. For sets with a custom comparator
this returned the wrong element, required operator< even when a comparator
was supplied, and was O(n). They now return *begin() / *std::prev(end()),
which respects the comparator and is O(1).
The non-const set::operator[] did `auto it = std::advance(begin(), index)`,
but std::advance returns void, so the overload failed to compile whenever
instantiated (subscripting a non-const set). It now advances an iterator in
place, matching the const overload, and works on both C++11 and C++17.
Added tests: min/max with a comparator that differs from operator<
(descending int comparator), and non-const subscripting.1 parent 764adeb commit dc2d83a
2 files changed
Lines changed: 42 additions & 17 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
606 | 606 | | |
607 | 607 | | |
608 | 608 | | |
609 | | - | |
610 | | - | |
611 | | - | |
| 609 | + | |
| 610 | + | |
612 | 611 | | |
613 | | - | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
614 | 616 | | |
615 | 617 | | |
616 | 618 | | |
| |||
627 | 629 | | |
628 | 630 | | |
629 | 631 | | |
630 | | - | |
631 | | - | |
632 | | - | |
| 632 | + | |
| 633 | + | |
633 | 634 | | |
634 | | - | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
635 | 639 | | |
636 | 640 | | |
637 | 641 | | |
| |||
1102 | 1106 | | |
1103 | 1107 | | |
1104 | 1108 | | |
1105 | | - | |
1106 | | - | |
1107 | | - | |
1108 | | - | |
1109 | | - | |
1110 | 1109 | | |
1111 | | - | |
1112 | | - | |
1113 | | - | |
| 1110 | + | |
1114 | 1111 | | |
1115 | | - | |
1116 | 1112 | | |
1117 | 1113 | | |
1118 | 1114 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
332 | 332 | | |
333 | 333 | | |
334 | 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 | + | |
335 | 364 | | |
336 | 365 | | |
337 | 366 | | |
| |||
0 commit comments