51#include <gtest/gtest.h>
57using namespace testing;
66 EXPECT_EQ(heap.
size(), 3u);
68 EXPECT_EQ(heap.
top(), 1);
70 EXPECT_EQ(heap.
getMin(), 1);
71 EXPECT_EQ(heap.
top(), 2);
72 EXPECT_EQ(heap.
get(), 2);
73 EXPECT_EQ(heap.
getMax(), 3);
80 auto & ref = heap.
insert(5);
81 const auto & ref2 = heap.
insert(7);
85 EXPECT_EQ(heap.
top(), 1);
89 EXPECT_EQ(heap.
size(), 1u);
90 EXPECT_EQ(heap.
top(), 7);
98 EXPECT_THROW(heap.
insert(30), std::overflow_error);
104 const std::vector<int> input{5, 3, 8, 1, 4, 9, 2, 7};
105 for (
int value : input)
108 std::vector<int> drained;
110 drained.push_back(heap.
getMin());
112 std::vector<int> sorted = input;
113 std::sort(sorted.begin(), sorted.end());
114 EXPECT_EQ(drained, sorted);
129 static constexpr int gone = -1;
135 Item(
const int p,
const int i) noexcept : pri(p), id(i) {}
137 Item & operator=(
const Item &) =
default;
138 Item(
Item && o) noexcept : pri(o.pri), id(std::exchange(o.id, gone)) {}
139 Item & operator=(
Item && o)
noexcept
142 id = std::exchange(o.id, gone);
148 void PrintTo(
const Item & x, std::ostream *os) { *os << x.pri <<
'#' << x.id; }
153 static_assert(std::is_nothrow_move_assignable_v<Item>);
161 std::vector<std::pair<int, int>> items;
165 struct Counted_Less_Of
169 bool operator()(
const X & a,
const X & b)
const
171 if (++calls->count == calls->throw_at)
172 throw std::runtime_error(
"comparison");
173 calls->items.emplace_back(a.id, b.id);
174 return a.pri < b.pri;
178 using Counted_Less = Counted_Less_Of<Item>;
183 std::vector<X> layout(
const ArrayHeap<
X, Counted_Less_Of<X>> &
h)
185 std::vector<X> items;
186 for (
size_t i = 1; i <=
h.size(); ++i)
187 items.push_back(
h[i]);
197 std::vector<X> a = std::vector<X>(1);
199 Counted_Less_Of<X>
cmp;
201 void sift_up(
const size_t l,
const size_t r)
203 for (
size_t p, i =
r; i >
l; i = p)
208 std::swap(a[p], a[i]);
214 for (
size_t i =
l; ;)
219 if (c + 1 <=
r and cmp(a[c + 1], a[c]))
223 std::swap(a[c], a[i]);
236 a.resize(std::max(a.size(), n + 2));
237 a[n + 1] = std::move(x);
254 void remove(
const size_t i)
265 std::vector<X> layout()
const {
return {a.begin() + 1, a.begin() + 1 +
static_cast<long>(n)}; }
268 using Old_Heap = Old_Heap_Of<Item>;
274 static inline int left = 0;
280 Husk(
const int p,
const int i) noexcept : pri(p), id(i) {}
281 Husk(
const Husk &) =
default;
282 Husk & operator=(
const Husk &) =
default;
283 Husk(Husk && o) noexcept : pri(std::exchange(o.pri, left)), id(std::exchange(o.id, 0)) {}
284 Husk & operator=(Husk && o)
noexcept
286 pri = std::exchange(o.pri, left);
287 id = std::exchange(o.id, 0);
290 bool operator==(
const Husk &)
const =
default;
293 static_assert(std::is_nothrow_move_assignable_v<Husk>);
295 void PrintTo(
const Husk & x, std::ostream *os) { *os << x.pri <<
'#' << x.id; }
297 using Husk_Less = Counted_Less_Of<Husk>;
306TEST(ArrayHeapThrowingCompare, SameComparisonsAndPlacesAsTheOldSifts)
308 std::mt19937
rng(77);
309 std::uniform_int_distribution<int> values(0, 50);
310 Calls calls, old_calls;
311 Counted_Heap heap(6000, Counted_Less{&calls});
312 Old_Heap old{.cmp = Counted_Less{&old_calls}};
315 for (
int k = 0;
k < 6000; ++
k)
317 const unsigned op = old.n == 0 ? 0 :
rng() % 8;
323 case 0: heap.insert(x);
break;
324 case 1: heap.insert(
Item(x));
break;
325 case 2: heap.insert_ne(x);
break;
326 default: heap.insert_ne(
Item(x));
332 const Item min = heap.getMin();
333 ASSERT_EQ(
min, old.getMin());
337 const size_t i = 1 +
rng() % old.n;
338 heap.remove(heap[i]);
344 const size_t i = 1 +
rng() % old.n;
345 const int pri = values(
rng);
348 heap.update(heap[i]);
352 ASSERT_EQ(calls.items, old_calls.items) <<
"operation " <<
k;
354 old_calls.items.clear();
355 ASSERT_EQ(layout(heap), old.layout()) <<
"operation " <<
k;
357 EXPECT_GT(updates, 1000u);
358 EXPECT_GT(removals, 500u);
359 EXPECT_GT(heap.size(), 100u);
367TEST(ArrayHeapThrowingCompare, EveryFailedCallLeavesTheHeapAsItWas)
369 std::mt19937
rng(20261121);
370 std::uniform_int_distribution<int> values(0, 30);
372 for (
size_t n = 0; n <= 40; ++n)
373 for (
int op = 0; op < 7; ++op)
375 if (op >= 4
and n == 0)
377 std::vector<Item> items;
378 for (
size_t k = 0;
k < n; ++
k)
379 items.emplace_back(values(
rng),
static_cast<int>(
k + 1));
380 const Item key(values(
rng),
static_cast<int>(n + 1));
381 const size_t where = n == 0 ? 0 : 1 +
rng() % n;
382 const int new_pri = values(
rng);
383 for (
size_t at = 1; ; ++at)
385 SCOPED_TRACE(testing::Message() <<
"n " << n <<
", op " << op <<
", call " << at);
387 Counted_Heap heap(n + 2, Counted_Less{&calls});
388 for (
const Item & x : items)
391 heap[where].pri = new_pri;
392 const std::vector<Item> before = layout(heap);
393 calls.throw_at = calls.count + at;
400 case 0: heap.insert(key);
break;
401 case 1: heap.insert(std::move(moved));
break;
402 case 2: heap.insert_ne(key);
break;
403 case 3: heap.insert_ne(std::move(moved));
break;
404 case 4: (void) heap.getMin();
break;
405 case 5: heap.remove(heap[where]);
break;
406 default: heap.update(heap[where]);
409 catch (
const std::runtime_error &)
416 ASSERT_TRUE(
valid_heap(&heap[0], 1, heap.size(), Counted_Less{&calls}));
420 EXPECT_EQ(heap.size(), n);
421 EXPECT_EQ(layout(heap), before);
422 EXPECT_EQ(moved, key);
425 EXPECT_GT(failed, 500u);
432TEST(ArrayHeapThrowingCompare, OneThrowLosesNothing)
438 bool operator()(
const int a,
const int b)
const
440 if (std::exchange(*armed,
false))
441 throw std::runtime_error(
"comparison");
451 EXPECT_THROW(heap.insert(1), std::runtime_error);
452 EXPECT_EQ(heap.size(), 1u);
453 EXPECT_EQ(heap.top(), 5);
457 for (
const int i : {1, 2, 3, 4})
460 EXPECT_THROW((
void) heap.getMin(), std::runtime_error);
461 EXPECT_EQ(heap.size(), 4u);
462 std::vector<int> drained;
463 while (not heap.is_empty())
464 drained.push_back(heap.getMin());
465 EXPECT_EQ(drained, (std::vector<int>{1, 2, 3, 4}));
475TEST(ArrayHeapThrowingCompare, AKeyThatItsMoveChangesInTheHeap)
477 for (
const int left : {-1, 1, 99})
478 for (
const bool checked : {
false,
true})
479 for (
int n = 1; n <= 12; ++n)
480 for (
int place = 1; place <= n; ++place)
482 SCOPED_TRACE(testing::Message() <<
"left " << left <<
", checked " << checked
483 <<
", n " << n <<
", place " << place);
485 Calls calls, old_calls;
486 Husk_Heap heap(n + 2, Husk_Less{&calls});
487 Old_Heap_Of<Husk> old{.cmp = Husk_Less{&old_calls}};
488 for (
int k = 1;
k <= n; ++
k)
490 const Husk x(
k % 3,
k);
494 ASSERT_EQ(layout(heap), old.layout());
497 heap.insert(std::move(heap[place]));
499 heap.insert_ne(std::move(heap[place]));
501 old.insert(std::move(old.a[place]));
503 EXPECT_EQ(calls.items, old_calls.items);
504 EXPECT_EQ(layout(heap), old.layout());
510TEST(ArrayHeapThrowingCompare, AKeyThatItsMoveChangesStaysInTheHeap)
513 for (
const int left : {-1, 99})
514 for (
const bool checked : {
false,
true})
515 for (
int n = 1; n <= 9; ++n)
516 for (
int place = 1; place <= n; ++place)
517 for (
size_t at = 1; ; ++at)
519 SCOPED_TRACE(testing::Message() <<
"left " << left <<
", checked " << checked <<
", n " << n
520 <<
", place " << place <<
", call " << at);
523 Husk_Heap heap(n + 2, Husk_Less{&calls});
524 for (
int k = 1;
k <= n; ++
k)
525 heap.insert(Husk(
k % 3,
k));
526 const std::vector<Husk> before = layout(heap);
527 calls.throw_at = calls.count + at;
532 heap.insert(std::move(heap[place]));
534 heap.insert_ne(std::move(heap[place]));
536 catch (
const std::runtime_error &)
544 EXPECT_EQ(heap.size(),
static_cast<size_t>(n));
545 EXPECT_EQ(layout(heap), before);
547 EXPECT_GT(failed, 100u);
552TEST(ArrayHeapThrowingCompare, AFailedHeapsortKeepsEveryItem)
555 std::uniform_int_distribution<int> values(0, 20);
557 for (
const bool faster : {
false,
true})
558 for (
size_t n = 0; n <= 24; ++n)
560 std::vector<Item> items;
561 for (
size_t k = 0;
k < n; ++
k)
562 items.emplace_back(values(
rng),
static_cast<int>(
k + 1));
563 for (
size_t at = 1; ; ++at)
565 SCOPED_TRACE(testing::Message() <<
"faster " << faster <<
", n " << n <<
", call " << at);
566 std::vector<Item> a = items;
575 heapsort(a.data(), a.size(), Counted_Less{&calls});
577 catch (
const std::runtime_error &)
582 std::vector<Item> by_id = a;
583 std::sort(by_id.begin(), by_id.end(),
584 [](
const Item & x,
const Item &
y) { return x.id < y.id; });
585 EXPECT_EQ(by_id, items);
588 EXPECT_TRUE(std::is_sorted(a.begin(), a.end(),
589 [](
const Item & x,
const Item &
y) { return x.pri < y.pri; }));
595 EXPECT_GT(failed, 1000u);
606 bool operator()(
const Item &,
const Item &)
const {
return (*rng)() & 1; }
613TEST(ArrayHeapThrowingCompare, AComparisonThatContradictsItselfKeepsTheItems)
617 std::vector<int> ids;
618 size_t removals = 0, updates = 0;
619 for (
int k = 0;
k < 20000; ++
k)
621 const unsigned op = heap.
is_empty() ? 0 :
rng() % 7;
622 if (op < 3
and heap.size() < 150)
625 heap.insert(
Item(
k,
k + 1));
627 heap.insert_ne(
Item(
k,
k + 1));
628 ids.push_back(
k + 1);
632 const Item min = heap.getMin();
633 const auto it = std::find(ids.
begin(), ids.
end(),
min.id);
634 ASSERT_NE(it, ids.
end()) <<
"operation " <<
k;
639 const size_t i = 1 +
rng() % heap.
size();
642 const auto it = std::find(ids.
begin(), ids.
end(), heap[i].id);
643 ASSERT_NE(it, ids.
end()) <<
"operation " <<
k;
650 heap.update(heap[i]);
654 ASSERT_EQ(heap.size(), ids.
size()) <<
"operation " <<
k;
655 std::vector<int> now;
656 for (
size_t i = 1; i <= heap.size(); ++i)
657 now.push_back(heap[i].id);
658 std::sort(now.begin(), now.end());
659 std::vector<int> sorted = ids;
660 std::sort(sorted.begin(), sorted.end());
661 ASSERT_EQ(now, sorted) <<
"operation " <<
k;
663 EXPECT_GT(removals, 1000u);
664 EXPECT_GT(updates, 1000u);
673 static inline bool armed =
false;
674 static inline size_t copies = 0;
679 Fragile(
const int p) noexcept : pri(p) {}
680 Fragile(
const Fragile & o) : pri(o.pri) { copied(); }
681 Fragile & operator=(
const Fragile & o)
687 Fragile(Fragile &&) noexcept = default;
688 Fragile & operator=(Fragile &&) noexcept = default;
693 throw std::bad_alloc();
697 bool operator<(
const Fragile & o)
const noexcept {
return pri < o.pri; }
706TEST(ArrayHeapThrowingCompare, ACopyThatThrowsLeavesTheHeapAsItWas)
709 for (
const int p : {5, 3, 8, 1})
712 const Fragile key(0);
713 Fragile::armed =
true;
714 EXPECT_THROW(heap.
insert(key), std::bad_alloc);
715 EXPECT_THROW(heap.
insert_ne(key), std::bad_alloc);
716 EXPECT_EQ(heap.
size(), 4u);
717 EXPECT_EQ(heap.
top().pri, 1);
719 std::vector<int>
out;
722 Fragile::armed =
false;
723 EXPECT_EQ(
out, (std::vector<int>{1, 3, 5, 8}));
725 for (
const int p : {5, 3, 8, 1})
730 EXPECT_EQ(Fragile::copies, 0u);
738 bool operator()(
const Item & a,
const Item & b)
const noexcept {
return a.pri < b.pri; }
742 template <
bool nothrow>
745 bool operator()(
const std::shared_ptr<int> & a,
const std::shared_ptr<int> & b)
const noexcept(nothrow)
757 std::mt19937
rng(17);
758 std::uniform_int_distribution<int> values(0, 50);
760 std::vector<int> ids;
762 for (
int k = 0;
k < 6000; ++
k)
767 ids.push_back(
k + 1);
771 const size_t i = 1 +
rng() % heap.
size();
772 ids.erase(std::find(ids.begin(), ids.end(), heap[i].id));
776 std::vector<int> now;
777 for (
size_t i = 1; i <= heap.
size(); ++i)
778 now.push_back(heap[i].id);
779 std::sort(now.begin(), now.end());
780 std::vector<int> sorted = ids;
781 std::sort(sorted.begin(), sorted.end());
782 ASSERT_EQ(now, sorted) <<
"operation " <<
k;
786 EXPECT_GT(removals, 2000u);
796 auto check = [](
auto & heap)
798 std::vector<std::weak_ptr<int>> seen;
799 for (
const int v : {5, 3, 8, 1, 9, 2})
801 auto p = std::make_shared<int>(v);
803 heap.insert(std::move(p));
805 const std::weak_ptr<int> second = heap[2];
806 heap.remove(heap[2]);
807 const auto alive_of = [&seen]
809 return std::count_if(seen.begin(), seen.end(), [](
const auto &
w) { return not w.expired(); });
811 EXPECT_TRUE(second.expired());
812 EXPECT_EQ(alive_of(), 5);
813 heap.remove(heap[heap.size()]);
814 EXPECT_EQ(alive_of(), 5);
815 EXPECT_EQ(heap.size(), 4u);
817 for (
size_t i = 1; i <= heap.size(); ++i)
818 in_heap += heap[i] !=
nullptr ? 1 : 0;
819 EXPECT_EQ(in_heap, 4u);
832 heapsort<int>(
nullptr, 0);
833 faster_heapsort<int>(
nullptr, 0);
834 std::vector<int>
none;
842 std::vector<int> two{2, 1};
844 EXPECT_EQ(two, (std::vector<int>{1, 2}));
847 EXPECT_EQ(two, (std::vector<int>{1, 2}));
858 constexpr bool operator () (
const int & a,
const int & b)
const noexcept {
return a < b; }
864 constexpr bool the_heapsorts_sort()
866 int a[] = {5, 3, 8, 1, 9, 2, 7, 3};
867 int b[] = {5, 3, 8, 1, 9, 2, 7, 3};
870 for (
int i = 1; i < 8; ++i)
871 if (a[i - 1] > a[i] or b[i - 1] > b[i])
873 return a[0] == 1
and a[7] == 9
and b[0] == 1
and b[7] == 9;
875 static_assert(the_heapsorts_sort());
880 static constexpr int three[] = {10, 20, 30};
888 std::uint64_t x = 20261125;
889 for (
size_t n = 2; n <= 70; ++n)
890 for (
int trial = 0; trial < 4; ++trial)
893 for (
size_t i = 0; i < n; ++i)
895 x = x * 6364136223846793005ULL + 1442695040888963407ULL;
896 items(i) =
static_cast<int>((x >> 33) % 16);
897 ++counts(
static_cast<size_t>(items(i)));
903 for (
int v = 0; v < 16; ++v)
904 for (
int k = 0; k < counts(static_cast<size_t>(v)); ++
k)
906 ASSERT_EQ(items, sorted) << n <<
" items, trial " << trial;
907 ASSERT_EQ(other, sorted) << n <<
" items, trial " << trial;
922 operator bool() const & noexcept {
return value; }
924 explicit operator bool() &&
926 if (++*conversions == throw_at)
927 throw std::runtime_error(
"conversion");
939 bool operator()(
const int & a,
const int & b)
const noexcept {
return a < b; }
941 Sly_Bool operator()(
int & a,
int & b)
const noexcept {
return {a < b, conversions, *throw_at}; }
944 static_assert(std::is_nothrow_invocable_r_v<bool, Sly_Less &, int &, int &>);
945 static_assert(not
noexcept(
static_cast<bool>(std::declval<Sly_Less &>()(std::declval<int &>(),
946 std::declval<int &>()))));
954TEST(ArrayHeapThrowingCompare, AConversionInAConditionThatThrowsIsUndone)
957 for (
size_t i = 1; i <= 5; ++i)
958 for (
long at = 1; ; ++at)
960 SCOPED_TRACE(testing::Message() <<
"item " << i <<
", conversion " << at);
961 long conversions = 0, throw_at = 0;
963 for (
const int v : {1, 2, 3, 4, 5, 6})
965 const auto items_of = [&heap]
967 std::vector<int> items;
968 for (
size_t k = 1;
k <= heap.size(); ++
k)
969 items.push_back(heap[
k]);
972 const std::vector<int> before = items_of();
978 heap.remove(heap[i]);
980 catch (
const std::runtime_error &)
987 EXPECT_EQ(heap.size(), 5u);
988 std::vector<int> left = items_of(),
expected = before;
990 std::sort(left.begin(), left.end());
996 EXPECT_EQ(items_of(), before);
998 EXPECT_GT(failed, 5u);
1009 std::shared_ptr<int> held;
1012 Rooted(
const int p,
const int i) : pri(p), id(i), held(
std::make_shared<int>(i)) {}
1015 struct Rooted_Nothrow_Less
1017 bool operator()(
const Rooted & a,
const Rooted & b)
const noexcept {
return a.pri < b.pri; }
1022 template <
class Heap>
1023 std::vector<std::weak_ptr<int>> fill_rooted(Heap & heap)
1025 std::vector<std::weak_ptr<int>> seen;
1027 for (
const int v : {5, 3, 8, 1, 9, 2})
1029 Rooted item(v, ++
id);
1030 seen.push_back(item.held);
1031 heap.insert(std::move(item));
1036 size_t alive_of(
const std::vector<std::weak_ptr<int>> & seen)
1038 return static_cast<size_t>(std::count_if(seen.begin(), seen.end(),
1039 [](
const auto &
w) { return not w.expired(); }));
1044 template <
class Heap>
1045 std::vector<std::pair<int, int>> rooted_in(
const Heap & heap)
1047 std::vector<std::pair<int, int>> items;
1048 for (
size_t i = 1; i <= heap.size(); ++i)
1049 items.emplace_back(heap[i].pri, heap[i].id);
1063 std::vector<Rooted> buffer(8, Rooted(0, 0));
1065 const auto seen = fill_rooted(heap);
1067 EXPECT_EQ(heap.
size(), 5u);
1068 EXPECT_EQ(alive_of(seen), 5u);
1073 EXPECT_LE(last,
min.pri);
1080 for (
size_t i = 1; i <= 5; ++i)
1081 for (
size_t at = 1; ; ++at)
1083 SCOPED_TRACE(testing::Message() <<
"item " << i <<
", call " << at);
1084 std::vector<Rooted> buffer(8, Rooted(0, 0));
1087 const auto seen = fill_rooted(heap);
1088 const auto before = rooted_in(heap);
1089 calls.throw_at = calls.count + at;
1090 bool thrown =
false;
1095 catch (
const std::runtime_error &)
1102 EXPECT_EQ(heap.
size(), 5u);
1103 EXPECT_EQ(alive_of(seen), 5u);
1107 EXPECT_EQ(rooted_in(heap), before);
1108 EXPECT_EQ(alive_of(seen), 6u);
1110 EXPECT_GT(failed, 5u);
1118 static inline bool armed =
false;
1119 static inline long built = 0;
1128 throw std::runtime_error(
"default constructor");
1131 Touchy(
const int p,
const int i) noexcept : pri(p), id(i) {}
1134 struct Touchy_Nothrow_Less
1136 bool operator()(
const Touchy & a,
const Touchy & b)
const noexcept {
return a.pri < b.pri; }
1145TEST(ArrayHeapThrowingCompare, RemoveBuildsNoItemWhoseConstructorMayThrow)
1147 static_assert(not std::is_nothrow_default_constructible_v<Touchy>);
1152 for (
const int v : {5, 3, 8, 1, 9, 2})
1154 heap.insert(Touchy(v, ++
id));
1155 quiet.
insert(Touchy(v,
id));
1158 const long built = Touchy::built;
1159 Touchy::armed =
true;
1160 EXPECT_NO_THROW(heap.remove(heap[2]));
1161 EXPECT_NO_THROW(quiet.
remove(quiet[2]));
1162 Touchy::armed =
false;
1163 EXPECT_EQ(Touchy::built, built);
1164 EXPECT_EQ(heap.size(), 5u);
1165 EXPECT_EQ(quiet.
size(), 5u);
1167 while (not heap.is_empty())
1169 const Touchy
min = heap.getMin();
1170 EXPECT_LE(last,
min.pri);
1185 Fussy() noexcept = default;
1186 Fussy(const
int p, const
int i) noexcept : pri(p),
id(i) {}
1187 Fussy(
const Fussy &) =
default;
1188 Fussy & operator=(
const Fussy &) =
default;
1189 Fussy(Fussy && o) noexcept : pri(o.pri), id(std::exchange(o.id, 0)) {}
1191 Fussy & operator=(Fussy && o)
1194 throw std::runtime_error(
"move assignment from an empty item");
1196 id = std::exchange(o.id, 0);
1203 template <
class Heap,
class Pri_And_Id>
1204 std::vector<int> drained_ids(Heap & heap, Pri_And_Id pri_and_id)
1206 std::vector<int> ids;
1208 while (not heap.is_empty())
1210 const auto min = heap.getMin();
1211 const std::pair<int, int> item = pri_and_id(
min);
1212 EXPECT_LE(last, item.first);
1214 ids.push_back(item.second);
1216 std::sort(ids.begin(), ids.end());
1221 std::vector<int> all_but(
const int n,
const int out)
1223 std::vector<int> ids;
1224 for (
int i = 1; i <= n; ++i)
1237TEST(ArrayHeapThrowingCompare, RemoveAssignsNoEmptyItemIfThatMayThrow)
1239 static_assert(std::is_nothrow_default_constructible_v<Fussy>);
1240 static_assert(not std::is_nothrow_move_assignable_v<Fussy>);
1241 static_assert(std::is_nothrow_move_constructible_v<Fussy>);
1242 std::vector<Fussy> buffer(8);
1246 for (
const int v : {5, 3, 8, 1, 9, 2})
1247 heap.
insert(Fussy(v, ++
id));
1248 const int removed = heap[2].id;
1249 ASSERT_NE(removed, 0);
1251 EXPECT_NO_THROW(heap.
remove(heap[2]));
1252 EXPECT_EQ(heap.
size(), 5u);
1253 EXPECT_EQ(buffer[6].
id, 0);
1254 EXPECT_EQ(drained_ids(heap, [](
const Fussy & x) {
return std::make_pair(x.pri, x.id); }),
1255 all_but(6, removed));
1265 static inline bool armed =
false;
1269 std::shared_ptr<int> held;
1274 throw std::runtime_error(
"default constructor");
1277 Welded(
const int p,
const int i) : pri(p), id(i), held(
std::make_shared<int>(i)) {}
1278 Welded(
const Welded &) =
default;
1279 Welded & operator=(
const Welded &) =
default;
1281 Welded(Welded && o) : pri(o.pri), id(o.id), held(o.held)
1284 throw std::runtime_error(
"move constructor");
1287 Welded & operator=(Welded &&) noexcept = default;
1289 friend
void swap(Welded & a, Welded & b) noexcept
1291 std::swap(a.pri, b.pri);
1292 std::swap(a.id, b.id);
1293 a.held.swap(b.held);
1300 using Welded_Item = std::pair<Welded, int>;
1304 bool operator()(
const Welded_Item & a,
const Welded_Item & b)
const
1306 return a.first.pri < b.first.pri;
1317TEST(ArrayHeapThrowingCompare, RemoveMovesNoItemOutIfThatMayThrow)
1319 static_assert(
noexcept(std::swap(std::declval<Welded_Item &>(), std::declval<Welded_Item &>())));
1320 static_assert(not std::is_nothrow_default_constructible_v<Welded_Item>);
1321 static_assert(not std::is_nothrow_move_constructible_v<Welded_Item>);
1322 std::vector<Welded_Item> buffer(8);
1324 std::vector<std::weak_ptr<int>> seen;
1326 for (
const int v : {5, 3, 8, 1, 9, 2})
1328 Welded_Item item(Welded(v, ++
id), v);
1329 seen.push_back(item.first.held);
1330 heap.
insert(std::move(item));
1332 const int removed = heap[2].first.id;
1334 Welded::armed =
true;
1335 EXPECT_NO_THROW(heap.
remove(heap[2]));
1336 Welded::armed =
false;
1337 EXPECT_EQ(heap.
size(), 5u);
1338 EXPECT_EQ(buffer[6].first.id, removed);
1339 EXPECT_EQ(alive_of(seen), 6u);
1341 Welded_Item seventh(Welded(4, ++
id), 4);
1342 seen.push_back(seventh.first.held);
1343 heap.
insert(std::move(seventh));
1344 EXPECT_EQ(alive_of(seen), 6u);
1345 EXPECT_TRUE(seen[
static_cast<size_t>(removed) - 1].expired());
1346 EXPECT_EQ(drained_ids(heap, [](
const Welded_Item & x) {
return std::make_pair(x.first.pri, x.first.id); }),
1347 all_but(7, removed));
bool operator<(const Time &l, const Time &r)
void PrintTo(const mpq_class &q, std::ostream *os)
size_t size_t int32_t value
size_t size_t int32_t * out
Fixed-capacity binary heap backed by a raw array.
T & insert(const T &key)
Insert an element into the heap.
T & insert_ne(const T &key)
Insert an element without checking capacity.
T getMin()
Remove the smallest element in the heap and return a copy of its value.
constexpr bool is_empty() const noexcept
Return true if the heap is empty.
void update(T &data)
Update the priority of an element stored in the heap.
T & top()
Return a mutable reference to the minimum element.
constexpr size_t size() const noexcept
Return the number of elements currently stored.
void remove(T &item)
Remove an element from the heap given a reference to it.
Simple dynamic array with automatic resizing and functional operations.
T & append(const T &data)
Append a copy of data
Minimal std::expected-style result type for C++20.
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_min_function > > min(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Main namespace for Aleph-w library functions.
void sift_down_up(T *ptr, const size_t l, const size_t i, const size_t r, Compare &cmp)
Restore the heap property by sifting down and then sifting up.
bool operator==(const DynList< T > &l1, const DynList< T > &l2)
Equality operator for DynList.
void sift_down(T *ptr, const size_t l, const size_t r, Compare &cmp)
Restore the heap property by moving the element at position l downwards.
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
constexpr Bit_Column bits(const unsigned n)
Column of n bits without sign: the integers of [0, 2^n - 1].
bool valid_heap(T *array, const size_t l, const size_t r, const Compare &cmp=Compare())
Check whether a range satisfies the heap property.
and
Check uniqueness with explicit hash + equality functors.
Fw_Itor remove(Fw_Itor __first, const Fw_Itor &__last, const T &__value)
Remove elements equal to a value.
constexpr void faster_heapsort(T *array, const size_t n, const Compare &cmp=Compare())
Optimized version of heapsort.
constexpr void heapsort(T *array, const size_t n, const Compare &cmp=Compare())
Sort an array using the heapsort algorithm.
bool none(const Container &container, Operation &operation)
Return true if no element satisfies operation.
T & sift_up(T *ptr, const size_t l, const size_t r, Compare &cmp)
Restore the heap property by moving the element at position r upwards.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
The items of an array, numbered from 1.
Fixed-capacity binary heap and heapsort algorithms.
Dynamic array container with automatic resizing.