45#include <gtest/gtest.h>
77 EXPECT_TRUE(this->set.is_empty());
78 EXPECT_EQ(this->set.size(), 0u);
79 EXPECT_THROW(this->set.min(), domain_error);
80 EXPECT_THROW(this->set.max(), domain_error);
81 EXPECT_THROW(this->set.get_root(), domain_error);
82 EXPECT_FALSE(this->set.contains(42));
83 EXPECT_EQ(this->set.search(42),
nullptr);
88 for (
int i : {5, 3, 7, 1, 9, 2, 8})
90 auto * p = this->set.insert(i);
91 ASSERT_NE(p,
nullptr);
95 EXPECT_EQ(this->set.size(), 7u);
96 EXPECT_EQ(this->set.min(), 1);
97 EXPECT_EQ(this->set.max(), 9);
99 for (
int i : {1, 2, 3, 5, 7, 8, 9})
100 EXPECT_TRUE(this->set.contains(i));
102 EXPECT_FALSE(this->set.contains(4));
103 EXPECT_FALSE(this->set.contains(6));
108 auto * p1 = this->set.insert(42);
109 ASSERT_NE(p1,
nullptr);
110 EXPECT_EQ(this->set.size(), 1u);
112 auto * p2 = this->set.insert(42);
113 EXPECT_EQ(p2,
nullptr);
114 EXPECT_EQ(this->set.size(), 1u);
119 for (
int i : {5, 3, 7, 1, 9})
122 EXPECT_EQ(this->set.size(), 5u);
125 EXPECT_EQ(this->set.size(), 4u);
126 EXPECT_FALSE(this->set.contains(3));
127 EXPECT_TRUE(this->set.contains(5));
132 auto * p1 = this->set.search_or_insert(42);
133 ASSERT_NE(p1,
nullptr);
135 EXPECT_EQ(this->set.size(), 1u);
137 auto * p2 = this->set.search_or_insert(42);
139 EXPECT_EQ(this->set.size(), 1u);
144 auto [p1, found1] = this->set.contains_or_insert(42);
145 ASSERT_NE(p1,
nullptr);
146 EXPECT_FALSE(found1);
147 EXPECT_EQ(this->set.size(), 1u);
149 auto [p2, found2] = this->set.contains_or_insert(42);
151 EXPECT_EQ(this->set.size(), 1u);
156 this->set.insert(42);
158 const auto & key = this->set.find(42);
161 EXPECT_THROW(this->set.find(99), domain_error);
163 int removed = this->set.del(42);
164 EXPECT_EQ(removed, 42);
165 EXPECT_TRUE(this->set.is_empty());
167 EXPECT_THROW(this->set.del(42), domain_error);
172 for (
int i : {1, 2, 3, 4, 5})
175 TypeParam
copy(this->set);
177 EXPECT_EQ(
copy.size(), 5u);
178 for (
int i : {1, 2, 3, 4, 5})
179 EXPECT_TRUE(
copy.contains(i));
183 EXPECT_FALSE(this->set.contains(3));
184 EXPECT_TRUE(
copy.contains(3));
189 for (
int i : {1, 2, 3, 4, 5})
192 TypeParam moved(std::move(this->set));
194 EXPECT_EQ(moved.size(), 5u);
195 for (
int i : {1, 2, 3, 4, 5})
196 EXPECT_TRUE(moved.contains(i));
198 EXPECT_TRUE(this->set.is_empty());
205 for (
int i : {1, 2, 3})
207 for (
int i : {10, 20})
210 this->set.swap(set2);
212 EXPECT_EQ(this->set.size(), 2u);
213 EXPECT_TRUE(this->set.contains(10));
214 EXPECT_EQ(set2.size(), 3u);
215 EXPECT_TRUE(set2.contains(1));
220 for (
int i = 0; i < 50; ++i)
223 EXPECT_EQ(this->set.size(), 50u);
227 EXPECT_TRUE(this->set.is_empty());
228 EXPECT_EQ(this->set.size(), 0u);
233 for (
int i : {5, 3, 7, 1, 9})
237 for (
typename TypeParam::Iterator it(this->set); it.has_curr(); it.next_ne())
238 keys.push_back(it.get_curr());
240 vector<int>
expected = {1, 3, 5, 7, 9};
246 for (
int i : {5, 3, 7, 1, 9})
250 this->set.for_each_inorder([&
keys](
const int &
k) {
keys.push_back(
k); });
252 vector<int>
expected = {1, 3, 5, 7, 9};
258 for (
int i : {1, 2, 3, 4, 5})
273 for (
int i : {5, 3, 7, 1, 9, 2, 8, 4, 6})
276 EXPECT_TRUE(this->set.verify());
282 for (
int i = 0; i < 500; ++i)
285 EXPECT_EQ(this->set.size(), 500u);
286 EXPECT_EQ(this->set.min(), 0);
287 EXPECT_EQ(this->set.max(), 499);
290 for (
int i = 0; i < 500; i += 2)
293 EXPECT_EQ(this->set.size(), 250u);
296 for (
int i = 1; i < 500; i += 2)
297 EXPECT_TRUE(this->set.contains(i));
311 EXPECT_EQ(set.
size(), 0u);
312 EXPECT_THROW(set.
min(), domain_error);
313 EXPECT_THROW(set.
max(), domain_error);
314 EXPECT_THROW(set.
get_root(), domain_error);
315 EXPECT_THROW(set.
get_item(), domain_error);
317 EXPECT_EQ(set.
search(42),
nullptr);
324 auto * p = set.
insert(42);
325 ASSERT_NE(p,
nullptr);
328 EXPECT_EQ(set.
size(), 1u);
330 EXPECT_EQ(set.
min(), 42);
331 EXPECT_EQ(set.
max(), 42);
339 for (
int i : {5, 3, 7, 1, 9, 2, 8})
342 ASSERT_NE(p,
nullptr);
346 EXPECT_EQ(set.
size(), 7u);
347 EXPECT_EQ(set.
min(), 1);
348 EXPECT_EQ(set.
max(), 9);
350 for (
int i : {1, 2, 3, 5, 7, 8, 9})
361 auto * p1 = set.
insert(42);
362 ASSERT_NE(p1,
nullptr);
364 EXPECT_EQ(set.
size(), 1u);
366 auto * p2 = set.
insert(42);
367 EXPECT_EQ(p2,
nullptr);
368 EXPECT_EQ(set.
size(), 1u);
376 ASSERT_NE(p1,
nullptr);
380 ASSERT_NE(p2,
nullptr);
383 EXPECT_EQ(set.
size(), 2u);
394 auto * p = set.
search(20);
395 ASSERT_NE(p,
nullptr);
406 EXPECT_EQ(set.
search(20),
nullptr);
415 ASSERT_NE(p1,
nullptr);
417 EXPECT_EQ(set.
size(), 1u);
421 ASSERT_NE(p2,
nullptr);
423 EXPECT_EQ(set.
size(), 1u);
433 ASSERT_NE(p1,
nullptr);
435 EXPECT_FALSE(found1);
436 EXPECT_EQ(set.
size(), 1u);
440 ASSERT_NE(p2,
nullptr);
443 EXPECT_EQ(set.
size(), 1u);
452 const auto & key = set.
find(42);
462 EXPECT_THROW(set.
find(42), domain_error);
477 size_t new_size = set.
remove(20);
478 EXPECT_EQ(new_size, 2u);
479 EXPECT_EQ(set.
size(), 2u);
492 size_t new_size = set.
remove(20);
493 EXPECT_EQ(new_size, 2u);
494 EXPECT_EQ(set.
size(), 2u);
501 size_t new_size = set.
remove(42);
502 EXPECT_EQ(new_size, 0u);
512 int removed = set.
del(42);
513 EXPECT_EQ(removed, 42);
515 EXPECT_EQ(set.
size(), 0u);
524 EXPECT_THROW(set.
del(42), domain_error);
531 for (
int i = 0; i < 10; ++i)
534 EXPECT_EQ(set.
size(), 10u);
536 for (
int i = 0; i < 10; ++i)
540 EXPECT_EQ(set.
size(), 0u);
547 for (
int i = 0; i < 100; ++i)
550 EXPECT_EQ(set.
size(), 100u);
555 EXPECT_EQ(set.
size(), 0u);
566 for (
int i : {1, 2, 3, 4, 5})
571 EXPECT_EQ(set2.size(), 5u);
572 for (
int i : {1, 2, 3, 4, 5})
573 EXPECT_TRUE(set2.contains(i));
578 EXPECT_TRUE(set2.contains(3));
585 for (
int i : {1, 2, 3})
588 for (
int i : {10, 20})
593 EXPECT_EQ(set2.
size(), 3u);
594 for (
int i : {1, 2, 3})
604 for (
int i : {1, 2, 3})
609 EXPECT_EQ(set.
size(), 3u);
610 for (
int i : {1, 2, 3})
618 for (
int i : {1, 2, 3, 4, 5})
623 EXPECT_EQ(set2.
size(), 5u);
624 for (
int i : {1, 2, 3, 4, 5})
634 for (
int i : {1, 2, 3})
637 for (
int i : {10, 20})
640 set2 = std::move(set1);
642 EXPECT_EQ(set2.
size(), 3u);
643 for (
int i : {1, 2, 3})
653 for (
int i : {1, 2, 3})
656 for (
int i : {10, 20})
661 EXPECT_EQ(set1.
size(), 2u);
665 EXPECT_EQ(set2.
size(), 3u);
680 EXPECT_FALSE(it.has_curr());
687 for (
int i : {5, 3, 7, 1, 9})
692 keys.push_back(it.get_curr());
694 vector<int>
expected = {1, 3, 5, 7, 9};
702 for (
int i : {1, 2, 3})
707 ASSERT_TRUE(it.has_curr());
708 EXPECT_EQ(it.get_curr(), 1);
711 EXPECT_EQ(it.get_curr(), 2);
714 EXPECT_EQ(it.get_curr(), 1);
725 for (
int i : {5, 3, 7, 1, 9})
731 vector<int>
expected = {1, 3, 5, 7, 9};
739 for (
int i : {5, 3, 7, 1, 9})
746 EXPECT_EQ(
keys.size(), 5u);
747 for (
int i : {1, 3, 5, 7, 9})
755 for (
int i : {5, 3, 7, 1, 9})
762 EXPECT_EQ(
keys.size(), 5u);
763 for (
int i : {1, 3, 5, 7, 9})
771 for (
int i : {1, 2, 3, 4, 5})
788 for (
int i : {1, 2, 3, 4, 5})
811 for (
int i : {5, 3, 7, 1, 9, 2, 8})
815 EXPECT_EQ(set.
select(0), 1);
816 EXPECT_EQ(set.
select(1), 2);
817 EXPECT_EQ(set.
select(2), 3);
818 EXPECT_EQ(set.
select(3), 5);
819 EXPECT_EQ(set.
select(6), 9);
826 for (
int i : {5, 3, 7, 1, 9})
842 for (
int i : {5, 3, 7, 1, 9})
847 ASSERT_NE(key_ptr,
nullptr);
848 EXPECT_EQ(*key_ptr, 5);
852 ASSERT_NE(key_ptr2,
nullptr);
859 for (
int i : {5, 3, 7, 1, 9})
864 EXPECT_EQ(removed, 5);
865 EXPECT_EQ(set.
size(), 4u);
873 for (
int i : {5, 3, 7, 1, 9})
877 EXPECT_EQ(set(0), 1);
878 EXPECT_EQ(set(2), 5);
879 EXPECT_EQ(set(4), 9);
891 for (
int i : {2, 4, 6, 8, 10, 12, 14, 16, 18})
897 bool success = set.
split_key(11, left, right);
899 EXPECT_TRUE(success);
903 EXPECT_EQ(left.
size(), 5u);
904 for (
int i : {2, 4, 6, 8, 10})
908 EXPECT_EQ(right.
size(), 4u);
909 for (
int i : {12, 14, 16, 18})
921 for (
int i : {1, 2, 3, 4, 5, 6, 7, 8, 9})
931 EXPECT_EQ(left.
size(), 5u);
935 EXPECT_EQ(right.
size(), 4u);
943 for (
int i : {1, 2, 3, 4, 5, 6, 7, 8, 9})
953 EXPECT_EQ(left.
size(), 5u);
954 for (
int i : {1, 2, 3, 4, 5})
958 EXPECT_EQ(right.
size(), 4u);
959 for (
int i : {6, 7, 8, 9})
967 for (
int i : {1, 2, 3, 4, 5})
976 EXPECT_EQ(left.
size(), 1u);
977 EXPECT_EQ(right.
size(), 4u);
988 EXPECT_EQ(left.
size(), 5u);
996 EXPECT_THROW(tmp.
split_pos(tmp.
size(), left, right), out_of_range);
997 EXPECT_THROW(tmp.
split_pos(tmp.
size() + 10, left, right), out_of_range);
1004 for (
int i : {1, 2, 3})
1007 EXPECT_THROW(set.
select(3), out_of_range);
1008 EXPECT_THROW(set.
remove_pos(3), out_of_range);
1012 EXPECT_THROW(empty.
select(0), out_of_range);
1013 EXPECT_THROW(empty.
remove_pos(0), out_of_range);
1015 EXPECT_THROW(empty.
split_pos(0, left, right), out_of_range);
1022 for (
int i : {1, 3, 5})
1025 for (
int i : {2, 4, 6})
1029 set1.
join(set2, dup);
1032 EXPECT_EQ(set1.
size(), 6u);
1033 for (
int i : {1, 2, 3, 4, 5, 6})
1043 for (
int i : {1, 2, 3, 4, 5})
1046 for (
int i : {3, 4, 5, 6, 7})
1050 set1.
join(set2, dup);
1053 EXPECT_EQ(set1.
size(), 7u);
1054 for (
int i : {1, 2, 3, 4, 5, 6, 7})
1057 EXPECT_EQ(dup.
size(), 3u);
1058 for (
int i : {3, 4, 5})
1066 for (
int i : {1, 2, 3})
1069 for (
int i : {3, 4, 5})
1075 EXPECT_EQ(set1.
size(), 6u);
1082template <
typename T>
1090 EXPECT_EQ(set.size(), 3u);
1092 size_t count_42 = 0;
1093 for (
typename T::Iterator it(set); it.has_curr(); it.next_ne())
1094 if (it.get_curr() == 42)
1097 EXPECT_EQ(count_42, 2u);
1102 insert_dup_traversal_test<DynSetRbTree<int>>();
1103 insert_dup_traversal_test<DynSetTreap<int>>();
1104 insert_dup_traversal_test<DynSetTreapRk<int>>();
1105 insert_dup_traversal_test<DynSetSplayTree<int>>();
1106 insert_dup_traversal_test<DynSetRandTree<int>>();
1107 insert_dup_traversal_test<DynSetBinTree<int>>();
1108 insert_dup_traversal_test<DynSetAvlTree<int>>();
1115template <
typename T>
1121 EXPECT_THROW(set.position(1), domain_error);
1122 EXPECT_THROW(set.find_position(1), domain_error);
1123 EXPECT_THROW(set.select(0), domain_error);
1124 EXPECT_THROW(set.remove_pos(0), domain_error);
1126 EXPECT_THROW(set.split_pos(0, left, right), domain_error);
1131 range_methods_throw_domain_error_test<DynSetAvlTree<int>>();
1132 range_methods_throw_domain_error_test<DynSetRbTree<int>>();
1133 range_methods_throw_domain_error_test<DynSetSplayTree<int>>();
1134 range_methods_throw_domain_error_test<DynSetTreap<int>>();
1135 range_methods_throw_domain_error_test<DynSetBinTree<int>>();
1148 for (
int i : {5, 3, 7, 1, 9})
1151 EXPECT_EQ(set.
size(), 5u);
1160 for (
int i : {5, 3, 7, 1, 9})
1163 EXPECT_EQ(set.
size(), 5u);
1171 for (
int i : {5, 3, 7, 1, 9})
1174 EXPECT_EQ(set.
size(), 5u);
1182 for (
int i : {5, 3, 7, 1, 9})
1185 EXPECT_EQ(set.
size(), 5u);
1199 for (
int i : {5, 3, 7, 1, 9})
1202 EXPECT_EQ(set.
min(), 9);
1203 EXPECT_EQ(set.
max(), 1);
1206 for (
DynSetTree<
int,
Avl_Tree, std::greater<int>>::Iterator it(set); it.has_curr(); it.next_ne())
1207 keys.push_back(it.get_curr());
1209 vector<int>
expected = {9, 7, 5, 3, 1};
1221 EXPECT_THROW(set[42], domain_error);
1230 const int & ref = set[42];
1233 EXPECT_EQ(set.
size(), 1u);
1244 for (
int i : {5, 3, 7, 1, 9, 2, 8, 4, 6})
1247 EXPECT_TRUE(set.
verify());
1259 for (
int i = 0; i < 1000; ++i)
1262 EXPECT_EQ(set.
size(), 1000u);
1263 EXPECT_EQ(set.
min(), 0);
1264 EXPECT_EQ(set.
max(), 999);
1267 for (
int i = 0; i < 1000; ++i)
1271 for (
int i = 0; i < 1000; i += 2)
1274 EXPECT_EQ(set.
size(), 500u);
1277 for (
int i = 0; i < 1000; ++i)
1289 vector<int> inserted;
1292 for (
int i = 0; i < 200; ++i)
1294 if (
int key = rand() % 500; set.
insert(key) !=
nullptr)
1295 inserted.push_back(key);
1299 for (
int key : inserted)
1302 EXPECT_EQ(set.
size(), inserted.size());
1305 for (
size_t i = 0; i < inserted.size() / 2; ++i)
1307 int idx = rand() % inserted.size();
1308 set.
remove(inserted[idx]);
1309 inserted.erase(inserted.begin() + idx);
1312 EXPECT_EQ(set.
size(), inserted.size());
1313 for (
int key : inserted)
1329 EXPECT_EQ(set.
size(), 3u);
1330 EXPECT_TRUE(set.
contains(
"apple"));
1331 EXPECT_TRUE(set.
contains(
"banana"));
1332 EXPECT_FALSE(set.
contains(
"date"));
1334 EXPECT_EQ(set.
min(),
"apple");
1335 EXPECT_EQ(set.
max(),
"cherry");
1348 EXPECT_EQ(set.
min(), 42);
1349 EXPECT_EQ(set.
max(), 42);
1351 EXPECT_EQ(set.
size(), 1u);
1361 for (
int cycle = 0; cycle < 10; ++cycle)
1363 for (
int i = 0; i < 50; ++i)
1366 EXPECT_EQ(set.
size(), 50u);
1368 for (
int i = 0; i < 50; ++i)
1379 for (
int i : {1, 2, 3, 4, 5})
1384 EXPECT_EQ(set.
get(), 5);
1393 inline static int alive = 0;
1395 TrackedKey() : v(0) { ++alive; }
1396 explicit TrackedKey(
int vv) : v(vv) { ++alive; }
1397 TrackedKey(
const TrackedKey & other) : v(other.v) { ++alive; }
1398 TrackedKey(TrackedKey && other) noexcept : v(other.v) { ++alive; }
1399 ~TrackedKey() { --alive; }
1402inline bool operator < (
const TrackedKey & a,
const TrackedKey & b)
1407template <
typename Key,
class Compare>
1408class ThrowingSearchOrInsertTree
1414 inline static bool enabled =
false;
1416 ThrowingSearchOrInsertTree(Compare c = Compare()) :
cmp(c) { }
1418 Compare get_compare()
const {
return cmp; }
1420 Node *& getRoot() noexcept {
return root; }
1421 Node * getRoot() const noexcept {
return root; }
1423 void swap(ThrowingSearchOrInsertTree & other)
noexcept
1425 std::swap(root, other.root);
1426 std::swap(cmp, other.cmp);
1437 throw std::runtime_error(
"search_or_insert failed");
1445 Node * insert_dup(
Node * p)
noexcept
1452 Node *
remove(
const Key &)
noexcept {
return nullptr; }
1454 bool verify()
const {
return true; }
1466TEST(DynSetTreeHardening, InsertDoesNotLeakOnThrow)
1468 TrackedKey::alive = 0;
1469 ThrowingSearchOrInsertTree<TrackedKey, Aleph::less<TrackedKey>>::enabled =
false;
1473 const int baseline = TrackedKey::alive;
1474 set.
insert(TrackedKey(1));
1475 EXPECT_EQ(set.size(), 1u);
1476 EXPECT_EQ(TrackedKey::alive, baseline + 1);
1478 ThrowingSearchOrInsertTree<TrackedKey, Aleph::less<TrackedKey>>::enabled =
true;
1479 EXPECT_THROW((
void) set.insert(TrackedKey(2)), std::runtime_error);
1480 EXPECT_EQ(set.size(), 1u);
1481 EXPECT_EQ(TrackedKey::alive, baseline + 1);
1484 ThrowingSearchOrInsertTree<TrackedKey, Aleph::less<TrackedKey>>::enabled =
false;
1485 EXPECT_EQ(TrackedKey::alive, 0);
1488TEST(DynSetTreeHardening, SearchOrInsertDoesNotLeakOnThrow)
1490 TrackedKey::alive = 0;
1491 ThrowingSearchOrInsertTree<TrackedKey, Aleph::less<TrackedKey>>::enabled =
false;
1495 const int baseline = TrackedKey::alive;
1496 set.
insert(TrackedKey(1));
1497 EXPECT_EQ(set.size(), 1u);
1498 EXPECT_EQ(TrackedKey::alive, baseline + 1);
1500 ThrowingSearchOrInsertTree<TrackedKey, Aleph::less<TrackedKey>>::enabled =
true;
1501 EXPECT_THROW((
void) set.search_or_insert(TrackedKey(2)), std::runtime_error);
1502 EXPECT_EQ(set.size(), 1u);
1503 EXPECT_EQ(TrackedKey::alive, baseline + 1);
1506 ThrowingSearchOrInsertTree<TrackedKey, Aleph::less<TrackedKey>>::enabled =
false;
1507 EXPECT_EQ(TrackedKey::alive, 0);
1510TEST(DynSetTreeHardening, ContainsOrInsertDoesNotLeakOnThrow)
1512 TrackedKey::alive = 0;
1513 ThrowingSearchOrInsertTree<TrackedKey, Aleph::less<TrackedKey>>::enabled =
false;
1517 const int baseline = TrackedKey::alive;
1518 set.
insert(TrackedKey(1));
1519 EXPECT_EQ(set.size(), 1u);
1520 EXPECT_EQ(TrackedKey::alive, baseline + 1);
1522 ThrowingSearchOrInsertTree<TrackedKey, Aleph::less<TrackedKey>>::enabled =
true;
1523 EXPECT_THROW((
void) set.contains_or_insert(TrackedKey(2)), std::runtime_error);
1524 EXPECT_EQ(set.size(), 1u);
1525 EXPECT_EQ(TrackedKey::alive, baseline + 1);
1528 ThrowingSearchOrInsertTree<TrackedKey, Aleph::less<TrackedKey>>::enabled =
false;
1529 EXPECT_EQ(TrackedKey::alive, 0);
1538 auto * p1 = this->set.
append(10);
1539 ASSERT_NE(p1,
nullptr);
1541 EXPECT_EQ(this->set.
size(), 1u);
1543 auto * p2 = this->set.
append(20);
1544 ASSERT_NE(p2,
nullptr);
1546 EXPECT_EQ(this->set.
size(), 2u);
1549 auto * p3 = this->set.
append(10);
1550 EXPECT_EQ(p3,
nullptr);
1551 EXPECT_EQ(this->set.
size(), 2u);
1556 auto * p1 = this->set.
put(10);
1557 ASSERT_NE(p1,
nullptr);
1559 EXPECT_EQ(this->set.
size(), 1u);
1561 auto * p2 = this->set.
put(20);
1562 ASSERT_NE(p2,
nullptr);
1564 EXPECT_EQ(this->set.
size(), 2u);
1567 auto * p3 = this->set.
put(10);
1568 EXPECT_EQ(p3,
nullptr);
1569 EXPECT_EQ(this->set.
size(), 2u);
1581 auto * p = this->set.
insert(std::move(val));
1582 ASSERT_NE(p,
nullptr);
1589 auto * p = this->set.
append(std::move(val));
1590 ASSERT_NE(p,
nullptr);
1597 auto * p = this->set.
put(std::move(val));
1598 ASSERT_NE(p,
nullptr);
1606 ASSERT_NE(p,
nullptr);
1608 EXPECT_EQ(this->set.
size(), 1u);
1615 ASSERT_NE(p,
nullptr);
1616 EXPECT_FALSE(found);
1623 auto * p1 = this->set.
insert_dup(std::move(val1));
1624 ASSERT_NE(p1,
nullptr);
1627 auto * p2 = this->set.
insert_dup(std::move(val2));
1628 ASSERT_NE(p2,
nullptr);
1630 EXPECT_EQ(this->set.
size(), 2u);
1639 EXPECT_EQ(this->set.
height(), 0u);
1642 EXPECT_GE(this->set.
height(), 1u);
1644 for (
int i = 2; i <= 10; ++i)
1647 size_t h = this->set.
height();
1672 ASSERT_NE(node,
nullptr);
1675 EXPECT_EQ(node->get_key(), 42);
1684 for (
int i : {5, 3, 7, 1, 9})
1688 EXPECT_EQ(this->set.
get_last(), 9);
1693 for (
int i : {5, 3, 7, 1, 9})
1697 EXPECT_EQ(this->set.
get(), 9);
1705 EXPECT_NO_THROW(this->set.
get_item());
1713 EXPECT_EQ(this->set.
exist(42), this->set.has(42));
1714 EXPECT_EQ(this->set.
has(42), this->set.contains(42));
1716 EXPECT_EQ(this->set.
exist(99), this->set.has(99));
1717 EXPECT_EQ(this->set.
has(99), this->set.contains(99));
1728 for (
int i : {1, 2, 3})
1731 for (
int i : {10, 20})
1736 EXPECT_EQ(set2.size(), 3u);
1737 for (
int i : {1, 2, 3})
1738 EXPECT_TRUE(set2.contains(i));
1739 EXPECT_FALSE(set2.contains(10));
1744 for (
int i : {1, 2, 3})
1747 this->set = this->set;
1749 EXPECT_EQ(this->set.
size(), 3u);
1750 for (
int i : {1, 2, 3})
1751 EXPECT_TRUE(this->set.
contains(i));
1758 for (
int i : {1, 2, 3})
1761 for (
int i : {10, 20})
1764 set2 = std::move(this->set);
1766 EXPECT_EQ(set2.size(), 3u);
1767 for (
int i : {1, 2, 3})
1768 EXPECT_TRUE(set2.contains(i));
1775 for (
int i : {1, 2, 3})
1779#if defined(__clang__)
1780# pragma clang diagnostic push
1781# pragma clang diagnostic ignored "-Wself-move"
1782#elif defined(__GNUC__)
1783# pragma GCC diagnostic push
1784# pragma GCC diagnostic ignored "-Wself-move"
1786 this->set = std::move(this->set);
1787#if defined(__clang__)
1788# pragma clang diagnostic pop
1789#elif defined(__GNUC__)
1790# pragma GCC diagnostic pop
1805 EXPECT_EQ(set.
size(), 5u);
1806 for (
int i : {1, 3, 5, 7, 9})
1815 EXPECT_EQ(set.
size(), 3u);
1833 for (
int i : {1, 2, 3, 4, 5})
1839 bool success = set.
split_key(3, left, right);
1841 EXPECT_FALSE(success);
1843 EXPECT_EQ(set.
size(), 5u);
1853 bool success = set.
split_key(42, left, right);
1855 EXPECT_TRUE(success);
1878 for (
int i : {5, 3, 7, 1, 9})
1882 EXPECT_EQ(set.
access(0), 1);
1883 EXPECT_EQ(set.
access(2), 5);
1884 EXPECT_EQ(set.
access(4), 9);
1891 for (
int i : {1, 2, 3})
1894 EXPECT_THROW(set.
select(3), out_of_range);
1895 EXPECT_THROW(set.
select(100), out_of_range);
1902 EXPECT_THROW(set.
select(0), out_of_range);
1909 for (
int i : {1, 2, 3})
1912 EXPECT_THROW(set.
remove_pos(3), out_of_range);
1913 EXPECT_THROW(set.
remove_pos(100), out_of_range);
1922 EXPECT_EQ(ptr,
nullptr);
1929 for (
int i : {1, 3, 5, 7, 9})
1942 for (
int i : {5, 3, 7, 1, 9})
1945 const auto & const_set = set;
1947 EXPECT_EQ(const_set.select(0), 1);
1948 EXPECT_EQ(const_set.select(4), 9);
1957 typename TypeParam::Iterator it(this->set);
1959 EXPECT_FALSE(it.has_curr());
1964 for (
int i : {1, 2, 3})
1967 typename TypeParam::Iterator it(this->set);
1970 while (it.has_curr())
1974 EXPECT_TRUE(it.has_curr());
1975 EXPECT_EQ(it.get_curr(), 1);
1980 for (
int i : {1, 2, 3})
1983 typename TypeParam::Iterator it(this->set);
1986 EXPECT_TRUE(it.has_curr());
1987 EXPECT_EQ(it.get_curr(), 3);
1996 for (
int i : {1, 2, 3, 4, 5})
2006 EXPECT_EQ(
count, 3);
2011 for (
int i : {1, 2, 3, 4, 5})
2014 const auto & const_set = this->set;
2034 for (
int i : {1, 2, 3})
2038 EXPECT_THROW(set(0), domain_error);
2050 set1.
join(set2, dup);
2062 for (
int i : {1, 2, 3})
2065 set1.
join(set2, dup);
2067 EXPECT_EQ(set1.
size(), 3u);
2088 EXPECT_TRUE(this->set.
verify());
2094 EXPECT_TRUE(this->set.
verify());
2106 auto * p = set.
insert(std::move(s));
2108 ASSERT_NE(p,
nullptr);
2109 EXPECT_EQ(*p,
"hello");
2110 EXPECT_EQ(set.
size(), 1u);
2117 string s1 =
"hello";
2119 ASSERT_NE(p1,
nullptr);
2120 EXPECT_EQ(*p1,
"hello");
2122 string s2 =
"hello";
2131TEST(DynSetTreeAliases, DynSetBinTreeWorks)
2136 EXPECT_EQ(set.
size(), 2u);
2137 EXPECT_TRUE(set.
verify());
2140TEST(DynSetTreeAliases, DynSetAvlTreeWorks)
2145 EXPECT_EQ(set.
size(), 2u);
2146 EXPECT_TRUE(set.
verify());
2149TEST(DynSetTreeAliases, DynSetSplayTreeWorks)
2154 EXPECT_EQ(set.
size(), 2u);
2155 EXPECT_TRUE(set.
verify());
2158TEST(DynSetTreeAliases, DynSetRandTreeWorks)
2163 EXPECT_EQ(set.
size(), 2u);
2164 EXPECT_TRUE(set.
verify());
2167TEST(DynSetTreeAliases, DynSetTreapWorks)
2172 EXPECT_EQ(set.
size(), 2u);
2173 EXPECT_TRUE(set.
verify());
2176TEST(DynSetTreeAliases, DynSetTreapRkWorks)
2181 EXPECT_EQ(set.
size(), 2u);
2182 EXPECT_TRUE(set.
verify());
2185TEST(DynSetTreeAliases, DynSetRbTreeWorks)
2190 EXPECT_EQ(set.
size(), 2u);
2191 EXPECT_TRUE(set.
verify());
2201 for (
int k = 0;
k <
N; ++
k)
2204 EXPECT_EQ(this->set.
size(),
static_cast<size_t>(
N));
2205 EXPECT_TRUE(this->set.
verify());
2206 EXPECT_EQ(this->set.
min(), 0);
2207 EXPECT_EQ(this->set.
max(),
N - 1);
2213 for (
int k =
N - 1;
k >= 0; --
k)
2216 EXPECT_EQ(this->set.
size(),
static_cast<size_t>(
N));
2217 EXPECT_TRUE(this->set.
verify());
2224 for (
int k = 0;
k <
N; ++
k)
2227 EXPECT_EQ(this->set.
size(),
static_cast<size_t>(
N));
2230 for (
int k = 0;
k <
N; ++
k)
2239 std::mt19937 gen(12345);
2240 std::uniform_int_distribution<> key_dist(0, 1000);
2241 std::uniform_int_distribution<> op_dist(0, 2);
2243 for (
int iter = 0; iter < 5000; ++iter)
2245 int key = key_dist(gen);
2246 int op = op_dist(gen);
2250 if (this->set.
insert(key) !=
nullptr)
2253 else if (op == 1 && !
oracle.empty())
2255 auto it =
oracle.begin();
2256 std::advance(it, gen() %
oracle.size());
2264 bool in_set = this->set.
contains(key);
2265 bool in_oracle =
oracle.count(key) > 0;
2266 EXPECT_EQ(in_set, in_oracle);
2272 EXPECT_TRUE(this->set.
verify());
2278 std::mt19937 gen(54321);
2279 std::uniform_int_distribution<> key_dist(0, 500);
2281 for (
int iter = 0; iter < 3000; ++iter)
2283 int key = key_dist(gen);
2287 if (this->set.
insert(key) !=
nullptr)
2290 else if (!
oracle.empty())
2292 auto it =
oracle.begin();
2293 std::advance(it, gen() %
oracle.size());
2303 EXPECT_TRUE(this->set.
verify());
2306TEST(DynSetTreeStress, AllTypesLargeScale)
2311 std::mt19937 gen(99999);
2312 std::uniform_int_distribution<> key_dist(0, 10000);
2313 std::uniform_int_distribution<> op_dist(0, 2);
2315 for (
int iter = 0; iter < 10000; ++iter)
2317 int key = key_dist(gen);
2318 int op = op_dist(gen);
2322 if (tree.insert(key) !=
nullptr)
2325 else if (op == 1 && !
oracle.empty())
2327 auto it =
oracle.begin();
2328 std::advance(it, gen() %
oracle.size());
2335 EXPECT_EQ(tree.contains(key),
oracle.count(key) > 0);
2339 EXPECT_EQ(tree.size(),
oracle.size());
2340 EXPECT_TRUE(tree.verify());
2368 struct Switchable_Order
2370 bool descending =
false;
2372 bool operator () (
const int a,
const int b)
const noexcept
2374 return descending ? b < a : a < b;
2379 struct Throwing_Copy_Key
2381 static inline bool fail =
false;
2385 Throwing_Copy_Key() =
default;
2386 Throwing_Copy_Key(
const int v) :
value(v) {}
2387 Throwing_Copy_Key(
const Throwing_Copy_Key & key) :
value(key.
value)
2390 throw std::runtime_error(
"copy of key");
2392 Throwing_Copy_Key(Throwing_Copy_Key &&) noexcept = default;
2393 Throwing_Copy_Key & operator = (const Throwing_Copy_Key &) = default;
2394 Throwing_Copy_Key & operator = (Throwing_Copy_Key &&) noexcept = default;
2396 bool operator < (const Throwing_Copy_Key & key) const noexcept
2398 return value < key.value;
2402 template <
class Set>
2403 vector<int> keys_in_order(
const Set & s)
2406 for (
auto it = s.get_it(); it.has_curr(); it.next_ne())
2407 keys.push_back(it.get_curr());
2413 template <
class Set>
2414 void expect_descending(Set & s,
const char * how)
2417 EXPECT_EQ(keys_in_order(s), (vector<int>{5, 4, 3, 2, 1}));
2418 EXPECT_TRUE(s.verify());
2419 for (
const int k : {1, 2, 3, 4, 5})
2420 EXPECT_TRUE(s.contains(
k)) <<
"key " <<
k;
2421 EXPECT_FALSE(s.contains(0));
2425 EXPECT_EQ(keys_in_order(s), (vector<int>{6, 5, 4, 3, 2, 1, 0}));
2426 EXPECT_TRUE(s.verify());
2429 template <
template <
typename,
class>
class Tree>
2430 void check_that_copies_carry_the_order(
const char * tree_name)
2432 SCOPED_TRACE(tree_name);
2435 Set source(Switchable_Order{
true});
2436 for (
const int k : {3, 1, 5, 2, 4})
2438 ASSERT_EQ(keys_in_order(source), (vector<int>{5, 4, 3, 2, 1}));
2440 Set assigned(Switchable_Order{
false});
2441 for (
const int k : {10, 20})
2444 expect_descending(assigned,
"copy assignment");
2447 expect_descending(copied,
"copy construction");
2449 Set to_move(source);
2450 Set moved(std::move(to_move));
2451 expect_descending(moved,
"move construction");
2453 Set move_assigned(Switchable_Order{
false});
2454 move_assigned.insert(10);
2455 move_assigned = Set(source);
2456 expect_descending(move_assigned,
"move assignment");
2458 Set swapped(Switchable_Order{
false});
2461 swapped.swap(other);
2462 expect_descending(swapped,
"swap");
2463 EXPECT_EQ(keys_in_order(other), (vector<int>{10}));
2466 EXPECT_EQ(keys_in_order(source), (vector<int>{5, 4, 3, 2, 1}));
2467 EXPECT_TRUE(source.verify());
2473 check_that_copies_carry_the_order<BinTree>(
"BinTree");
2474 check_that_copies_carry_the_order<Avl_Tree>(
"Avl_Tree");
2475 check_that_copies_carry_the_order<Splay_Tree>(
"Splay_Tree");
2476 check_that_copies_carry_the_order<Rand_Tree>(
"Rand_Tree");
2477 check_that_copies_carry_the_order<Treap>(
"Treap");
2478 check_that_copies_carry_the_order<Treap_Rk>(
"Treap_Rk");
2479 check_that_copies_carry_the_order<Rb_Tree>(
"Rb_Tree");
2485 for (
const int k : {1, 2, 3, 4, 5})
2486 source.insert(Throwing_Copy_Key(
k));
2487 for (
const int k : {10, 20})
2488 target.
insert(Throwing_Copy_Key(
k));
2490 Throwing_Copy_Key::fail =
true;
2491 EXPECT_THROW(target = source, std::runtime_error);
2492 Throwing_Copy_Key::fail =
false;
2494 EXPECT_EQ(target.
size(), 2u);
2495 EXPECT_TRUE(target.
contains(Throwing_Copy_Key(10)));
2496 EXPECT_TRUE(target.
contains(Throwing_Copy_Key(20)));
2497 EXPECT_TRUE(target.
verify());
2500 EXPECT_EQ(target.
size(), 5u);
2501 EXPECT_TRUE(target.
contains(Throwing_Copy_Key(3)));
2509 for (
const int k : {1, 2, 3})
2514 for (
const int k : {10, 20, 30, 40})
2520 EXPECT_EQ(keys_in_order(target), (vector<int>{10, 20, 30, 40}));
2523 EXPECT_EQ(keys_in_order(target), (vector<int>{20, 30, 40, 50}));
2524 EXPECT_TRUE(target.
verify());
2533 template <
typename Key,
class Compare>
2534 struct Shared_Copy_Tree :
public BinTree<Key, Compare>
2539 Shared_Copy_Tree() =
default;
2541 Shared_Copy_Tree(
const Shared_Copy_Tree & other)
2542 : Base(const_cast<Shared_Copy_Tree &>(other).key_comp())
2544 this->getRoot() =
const_cast<Shared_Copy_Tree &
>(other).getRoot();
2557 static_assert(std::is_copy_constructible_v<Shared_Copy_Tree<int, Aleph::less<int>>>);
2560 for (
const int k : {5, 3, 8, 1, 4})
2562 Set target(std::move(source));
2563 EXPECT_EQ(target.size(), 5u);
2564 for (
const int k : {1, 3, 4, 5, 8})
2565 EXPECT_TRUE(target.contains(
k)) <<
k;
2566 EXPECT_TRUE(source.is_empty());
2567 EXPECT_EQ(source.search(5),
nullptr) <<
"the two sets hold the same nodes";
2568 EXPECT_TRUE(target.verify());
2573 EXPECT_EQ(source.size(), 1u);
2574 EXPECT_TRUE(source.contains(2));
2575 EXPECT_FALSE(source.contains(9));
2576 EXPECT_EQ(target.size(), 6u);
2577 EXPECT_TRUE(target.contains(9));
2578 EXPECT_FALSE(target.contains(2));
2579 EXPECT_TRUE(source.verify());
2580 EXPECT_TRUE(target.verify());
2590 for (
const int k : {1, 2, 3})
2592 ASSERT_TRUE(source.uses_arena());
2596 EXPECT_FALSE(source.uses_arena());
2597 EXPECT_EQ(target.
size(), 3u);
2598 EXPECT_TRUE(source.is_empty());
2602 EXPECT_EQ(target.
size(), 3u);
2606 EXPECT_TRUE(target.
verify());
2610 EXPECT_EQ(source.size(), 2u);
2611 EXPECT_TRUE(source.contains(7));
2612 EXPECT_TRUE(source.verify());
2620 constexpr long n = 1'000'000;
2622 for (
long i = 0; i < n; ++i)
2624 ASSERT_EQ(set.
size(),
static_cast<size_t>(n));
2625 EXPECT_EQ(set.
height(),
static_cast<size_t>(n));
2628 EXPECT_EQ(copied.
size(),
static_cast<size_t>(n));
2629 EXPECT_EQ(copied.
height(),
static_cast<size_t>(n));
2634 EXPECT_EQ(assigned.
size(),
static_cast<size_t>(n));
2635 EXPECT_EQ(assigned.
min(), 0);
2636 EXPECT_EQ(assigned.
max(), n - 1);
2650class DynSetTreeArena :
public ::testing::Test {};
2652using ArenaTreeTypes = ::testing::Types<
2659constexpr size_t Arena_Bytes = 4096;
2660constexpr int Arena_Rounds = 2000;
2662TYPED_TEST(DynSetTreeArena, EmptyReleasesTheWholeArena)
2664 TypeParam set(Arena_Bytes);
2665 for (
int round = 0; round < Arena_Rounds; ++round)
2667 for (
int i = 0; i < 10; ++i)
2668 ASSERT_NO_THROW(set.
insert(i)) <<
"round " << round;
2673 ASSERT_EQ(set.
size(), 0u);
2679TYPED_TEST(DynSetTreeArena, RemovalsInTheOrderOfInsertionDoNotExhaustTheArena)
2681 TypeParam set(Arena_Bytes);
2682 for (
int i = 0; i < 8; ++i)
2684 for (
int i = 8; i < Arena_Rounds; ++i)
2686 ASSERT_NO_THROW(set.
insert(i)) <<
"insertion " << i;
2688 ASSERT_EQ(set.
size(), 8u);
2689 ASSERT_EQ(set.
min(), i - 7);
2690 ASSERT_EQ(set.
max(), i);
2692 EXPECT_TRUE(set.
verify());
2697TYPED_TEST(DynSetTreeArena, InsertionsAndRemovalsInMixedOrders)
2699 TypeParam set(Arena_Bytes);
2700 unsigned present = 0;
2701 for (
int step = 1; step < 4096; ++step)
2703 const int key = std::countr_zero(
static_cast<unsigned>(step)) % 8;
2704 if (present & (1u << key))
2707 ASSERT_NE(set.
insert(key),
nullptr) <<
"step " << step;
2708 present ^= 1u << key;
2709 for (
int k = 0;
k < 8; ++
k)
2710 ASSERT_EQ(set.
contains(
k), (present >>
k & 1u) != 0) <<
"step " << step;
2714TYPED_TEST(DynSetTreeArena, AFullArenaTakesAsManyKeysAfterRemovingThem)
2716 TypeParam set(Arena_Bytes);
2717 const auto fill = [&set]
2728 catch (
const std::bad_alloc &) {}
2732 const int capacity =
fill();
2733 ASSERT_GT(capacity, 1);
2734 for (
int turn = 0; turn < 3; ++turn)
2736 for (
int i = 0; i < capacity; ++i)
2738 ASSERT_EQ(set.
size(), 0u);
2739 ASSERT_EQ(
fill(), capacity) <<
"turn " << turn;
2742 EXPECT_EQ(
fill(), capacity);
2745TYPED_TEST(DynSetTreeArena, ARepeatedKeyTakesNoRoom)
2747 TypeParam set(Arena_Bytes);
2750 for (
int i = 0; i < Arena_Rounds; ++i)
2751 ASSERT_NO_THROW(set.
insert(1)) <<
"attempt " << i;
2752 EXPECT_EQ(set.
size(), 2u);
2758 static inline bool fail =
false;
2761 Arena_Key() =
default;
2762 Arena_Key(
const int i) : text(
std::string(40,
'k') +
std::
to_string(1000 + i)) {}
2763 Arena_Key(
const Arena_Key & other) : text(other.text)
2766 throw std::runtime_error(
"copy");
2768 Arena_Key(Arena_Key &&) =
default;
2769 Arena_Key & operator = (
const Arena_Key &) =
default;
2770 Arena_Key & operator = (Arena_Key &&) =
default;
2771 bool operator < (
const Arena_Key & other)
const {
return text < other.text; }
2774TEST(DynSetTreeArenaKeys, AKeyWhoseCopyThrowsTakesNoRoom)
2777 const Arena_Key first(1), second(2);
2780 Arena_Key::fail =
true;
2781 for (
int i = 0; i < Arena_Rounds; ++i)
2782 ASSERT_THROW(set.
insert(second), std::runtime_error) <<
"attempt " << i;
2783 Arena_Key::fail =
false;
2785 ASSERT_NO_THROW(set.
insert(second));
2786 EXPECT_EQ(set.
size(), 2u);
2789TEST(DynSetTreeArenaKeys, KeysThatOwnMemoryAreReleasedAndTheirRoomReused)
2792 for (
int i = 0; i < 500; ++i)
2794 ASSERT_NO_THROW(set.
insert(Arena_Key(i)));
2796 set.
remove(Arena_Key(i - 4));
2805TEST(DynSetTreeArenaKeys, AnExternalBufferIsReused)
2807 alignas(std::max_align_t)
static char buffer[Arena_Bytes];
2809 for (
int i = 0; i < Arena_Rounds; ++i)
2811 ASSERT_NO_THROW(set.
insert(i));
2822template <
class Set,
class Transfer>
2823void check_given_nodes_survive(Transfer && transfer)
2825 constexpr size_t bytes = 1 << 16;
2826 Set lender(bytes), first(bytes), second(bytes);
2827 for (
int i = 0; i < 10; ++i)
2830 transfer(lender, first, second);
2831 ASSERT_EQ(lender.size(), 0u);
2832 ASSERT_EQ(first.size() + second.size(), 10u);
2834 const auto reuse = [&lender]
2836 for (
int turn = 0; turn < 3; ++turn)
2839 for (
int i = 100; i < 120; ++i)
2841 for (
int i = 100; i < 120; ++i)
2843 for (
int i = 200; i < 220; ++i)
2847 const auto check = [&first, &second]
2849 for (
int i = 0; i < 10; ++i)
2850 ASSERT_TRUE(first.contains(i) or second.contains(i)) <<
"key " << i;
2851 ASSERT_TRUE(first.verify());
2852 ASSERT_TRUE(second.verify());
2859 for (Set * receiver : {&first, &second})
2860 if (not receiver->is_empty())
2862 const int key = receiver->min();
2863 receiver->remove(key);
2864 receiver->insert(1000);
2866 EXPECT_TRUE(receiver->contains(1000));
2867 receiver->remove(1000);
2868 receiver->insert(key);
2877TEST(DynSetTreeArenaSplitPos, AReceiverDoesNotBuildInTheArenaOfAnother)
2880 constexpr size_t bytes = 1 << 16;
2881 Set first(bytes), second(bytes);
2884 for (
int i = 0; i < 10; ++i)
2886 lender.split_pos(4, first, second);
2887 for (
int i = 0; i < 10; ++i)
2888 (i < 5 ? first : second).
remove(i);
2889 for (
int i = 0; i < 20; ++i)
2890 first.insert(100 + i);
2893 for (
int i = 0; i < 20; ++i)
2894 EXPECT_TRUE(first.contains(100 + i));
2895 EXPECT_TRUE(first.verify());
2900class DynSetTreeArenaJoins :
public ::testing::Test {};
2902using JoinableArenaTreeTypes = ::testing::Types<
2908TYPED_TEST(DynSetTreeArenaJoins, GivenNodesSurviveAJoin)
2910 check_given_nodes_survive<TypeParam>(
2911 [] (
auto & lender,
auto & first,
auto & second)
2912 { first.join(lender, second); });
2920TYPED_TEST(DynSetTreeArenaJoins, TheDuplicatesSurviveWhenTheJoinedSetIsEmptied)
2922 constexpr size_t bytes = 1 << 16;
2923 TypeParam joined(bytes), other(bytes), dup(bytes);
2924 for (
int i = 0; i < 10; ++i)
2926 for (
int i = 5; i < 15; ++i)
2928 joined.join(other, dup);
2929 ASSERT_EQ(joined.size(), 15u);
2930 ASSERT_EQ(dup.size(), 5u);
2932 for (
int turn = 0; turn < 3; ++turn)
2935 for (
int i = 100; i < 140; ++i)
2940 dup.for_each([&
count] (
const int & key)
2943 EXPECT_TRUE(key >= 5
and key < 10) <<
"key " << key;
2945 EXPECT_EQ(
count, 5u);
2946 for (
int i = 5; i < 10; ++i)
2947 EXPECT_TRUE(dup.contains(i)) <<
"key " << i;
2948 EXPECT_TRUE(dup.verify());
2953TYPED_TEST(DynSetTreeArenaJoins, AJoinWithoutDuplicatesLeavesTheArenaOfThisFree)
2955 constexpr size_t bytes = 1 << 16;
2956 TypeParam joined(bytes), other(bytes), dup(bytes);
2957 for (
int i = 0; i < 10; ++i)
2959 for (
int i = 10; i < 20; ++i)
2961 joined.join(other, dup);
2962 ASSERT_EQ(joined.size(), 20u);
2963 ASSERT_EQ(dup.size(), 0u);
2966 EXPECT_EQ(joined.arena_allocated_size(), 0u);
2969TYPED_TEST(DynSetTreeArenaJoins, GivenNodesSurviveAJoinWithRepetitions)
2971 check_given_nodes_survive<TypeParam>(
2972 [] (
auto & lender,
auto & first,
auto &) { first.join_dup(lender); });
2975TYPED_TEST(DynSetTreeArenaJoins, GivenNodesSurviveASplitByKey)
2977 check_given_nodes_survive<TypeParam>(
2978 [] (
auto & lender,
auto &
l,
auto &
r)
2981 ASSERT_TRUE(lender.split_key(5,
l,
r));
2986TYPED_TEST(DynSetTreeArenaJoins, GivenNodesSurviveASplitByKeyWithRepetitions)
2988 check_given_nodes_survive<TypeParam>(
2989 [] (
auto & lender,
auto &
l,
auto &
r)
2990 { lender.split_key_dup(5,
l,
r); });
2994TEST(DynSetTreeArenaSplitPos, GivenNodesSurviveASplitByPosition)
2996 const auto split = [] (
auto & lender,
auto &
l,
auto &
r)
2997 { lender.split_pos(4,
l,
r); };
2998 check_given_nodes_survive<DynSetTree<int, Treap_Rk>>(
split);
2999 check_given_nodes_survive<DynSetTree<int, Rand_Tree>>(
split);
3006 ::testing::InitGoogleTest(&argc, argv);
3007 return RUN_ALL_TESTS();
bool operator<(const Time &l, const Time &r)
WeightedDigraph::Node Node
size_t size_t int32_t value
Node for binary search tree.
static BinNode *const NullPtr
Dynamic set implemented using AVL binary search trees of type Avl_Tree<Key>.
Dynamic set implemented using binary search trees of type BinTree<Key>.
Dynamic set implemented using randomized binary search trees of type Rand_Tree<Key>.
Dynamic set implemented using Red-Black binary search trees of type Rb_Tree<Key> (bottom-up implement...
Dynamic set implemented using splay binary search trees of type Splay_Tree<Key>.
Dynamic set implemented using extended treap binary search trees with rank support of type Treap_Rk<K...
Dynamic set implemented using randomized treap binary search trees of type Treap<Key>.
Dynamic set backed by balanced binary search trees with automatic memory management.
const Key & get_first() const
See min.
size_t height() const
Calculates and returns the height of the binary search tree.
const Key & min() const
Returns the smallest key contained in the set according to the criterion comparison given.
long position(const Key &key) const
Returns the infix (ordered) position of the key.
Key * append(const Key &key)
Key & find(const Key &key)
Returns a modifiable reference to an element within the set.
const Key & get_last() const
See max.
bool split_key(const Key &key, DynSetTree &l, DynSetTree &r)
Partitions the binary search tree based on a key.
const Key & get_item() const
Returns any element of the set.
const size_t & size() const
Returns the cardinality of the set.
Key remove_pos(const size_t i)
Removes the key at the inorder position i.
Key del(const Key &key)
Deletes key and returns a full copy of stored key.
void clear()
Empties the container.
std::pair< long, Key * > find_position(const Key &key) const
Returns the infix (ordinate) position of the key key or whatever It would be your position of belongi...
Key * insert(const Key &key)
Inserts a key into the dynamic set.
const Key & max() const
Returns the largest key contained in the set according to the criteria comparison given.
bool exist(const Key &key) const
Returns true if key belongs to the dynamic set.
void split_key_dup(const Key &key, DynSetTree &l, DynSetTree &r)
Partitions the binary search tree based on a key that may be present in the tree.
bool has(const Key &key) const
void swap(DynSetTree &dset) noexcept(noexcept(tree.swap(dset.tree)) and noexcept(std::swap(num_nodes, dset.num_nodes)) and noexcept(std::swap(arena_allocator, dset.arena_allocator)))
Exchange all elements of this set with dset in constant time (and extremely fast).
void for_each_postorder(Key_Op &key_op) const
Performs a postfix traversal over all keys in the set and invokes operation Op.
Key * put(const Key &key)
size_t internal_path_length() const
Calculates and returns the length of the internal path of the tree search binary.
size_t remove(const Key &key)
Removes a key from the dynamic set.
bool contains(const Key &key) const
Checks if a key exists in the set.
const Key & get() const
Synonym of max.
const Key & get_root() const
Key * search(const Key &key)
Find an element in the set.
Key * insert_dup(const Key &key)
void split_pos(const size_t pos, DynSetTree &l, DynSetTree &r)
Partitions the binary search tree based on an infix position.
size_t arena_allocated_size() const noexcept
Returns the allocated size from the arena (0 if not using arena)
Key & select(size_t i)
Returns the ith node in infix position.
void empty()
Remove all the elements of the set.
bool uses_arena() const noexcept
Returns true if the set is using an arena allocator.
bool traverse(Operation &op)
Traverse all the set of pairs and for each key executes the operation op.
Key * search_or_insert(const Key &key)
Look for the key in the binary search tree or inserts it if it is not found.
void for_each_preorder(Key_Op &key_op) const
Performs a prefix traversal over all keys in the set and invokes operation Op.
void for_each_inorder(Key_Op &key_op) const
Performs an infix traversal over all keys in the set and invokes operation Op.
std::pair< Key *, bool > contains_or_insert(const Key &key)
bool is_empty() const
returns true if the set is empty
Node * get_root_node() const
Number Theoretic Transform over Z / MOD Z.
Minimal std::expected-style result type for C++20.
QuadTree - Hierarchical spatial index for 2D points.
::testing::Types< DynSetBinTree< int >, DynSetAvlTree< int >, DynSetRbTree< int >, DynSetSplayTree< int >, DynSetTreap< int >, DynSetRandTree< int >, DynSetTreapRk< int > > AllTreeTypes
static void range_methods_throw_domain_error_test()
static void insert_dup_traversal_test()
TYPED_TEST(DynSetTreeTypedTest, EmptySetProperties)
TYPED_TEST_SUITE(DynSetTreeTypedTest, AllTreeTypes)
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
DynSetTree & join(DynSetTree &t, DynSetTree &dup)
Union of two binary search trees.
DynSetTree & join_dup(DynSetTree &t)
Union of two binary search trees.
Main namespace for Aleph-w library functions.
bool completed() const noexcept
Return true if all underlying iterators are finished.
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
void fill(Itor beg, const Itor &end, const T &value)
Fill a range with a value.
std::decay_t< typename HeadC::Item_Type > T
Itor find(const Itor &beg, const Itor &end, const T &value)
Find the first element equal to a value.
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.
Itor1 search(Itor1 beg, const Itor1 &end, Itor2 searchBeg, const Itor2 &searchEnd, BinaryPredicate op=BinaryPredicate())
Search for a subrange within a range.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
std::vector< std::string > & split(const std::string &s, const char delim, std::vector< std::string > &elems)
Split a std::string by a single delimiter character.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
AVL binary search tree with nodes without a virtual destructor.
Binary search tree with nodes without virtual destructors,.
GenBinTree< BinNode, Key, Compare > Base
Dynamic set implementations based on balanced binary search trees.