44#include <gtest/gtest.h>
87 for (
int i : {5, 3, 7, 1, 9, 2, 8})
89 auto * p = this->set.insert(i);
98 for (
int i : {1, 2, 3, 5, 7, 8, 9})
107 auto * p1 = this->set.insert(42);
111 auto * p2 = this->set.insert(42);
118 for (
int i : {5, 3, 7, 1, 9})
131 auto * p1 = this->set.search_or_insert(42);
136 auto * p2 = this->set.search_or_insert(42);
143 auto [p1,
found1] = this->set.contains_or_insert(42);
148 auto [p2,
found2] = this->set.contains_or_insert(42);
155 this->set.insert(42);
157 const auto & key = this->set.find(42);
162 int removed = this->set.del(42);
171 for (
int i : {1, 2, 3, 4, 5})
177 for (
int i : {1, 2, 3, 4, 5})
188 for (
int i : {1, 2, 3, 4, 5})
194 for (
int i : {1, 2, 3, 4, 5})
204 for (
int i : {1, 2, 3})
206 for (
int i : {10, 20})
209 this->set.swap(
set2);
219 for (
int i = 0; i < 50; ++i)
232 for (
int i : {5, 3, 7, 1, 9})
236 for (
typename TypeParam::Iterator it(this->set); it.has_curr(); it.next_ne())
237 keys.push_back(it.get_curr());
239 vector<int>
expected = {1, 3, 5, 7, 9};
245 for (
int i : {5, 3, 7, 1, 9})
249 this->set.for_each_inorder([&
keys](
const int &
k) {
keys.push_back(
k); });
251 vector<int>
expected = {1, 3, 5, 7, 9};
257 for (
int i : {1, 2, 3, 4, 5})
272 for (
int i : {5, 3, 7, 1, 9, 2, 8, 4, 6})
281 for (
int i = 0; i < 500; ++i)
289 for (
int i = 0; i < 500; i += 2)
295 for (
int i = 1; i < 500; i += 2)
323 auto * p = set.
insert(42);
338 for (
int i : {5, 3, 7, 1, 9, 2, 8})
349 for (
int i : {1, 2, 3, 5, 7, 8, 9})
360 auto * p1 = set.
insert(42);
365 auto * p2 = set.
insert(42);
393 auto * p = set.
search(20);
451 const auto & key = set.
find(42);
530 for (
int i = 0; i < 10; ++i)
535 for (
int i = 0; i < 10; ++i)
546 for (
int i = 0; i < 100; ++i)
565 for (
int i : {1, 2, 3, 4, 5})
571 for (
int i : {1, 2, 3, 4, 5})
584 for (
int i : {1, 2, 3})
587 for (
int i : {10, 20})
593 for (
int i : {1, 2, 3})
603 for (
int i : {1, 2, 3})
609 for (
int i : {1, 2, 3})
617 for (
int i : {1, 2, 3, 4, 5})
623 for (
int i : {1, 2, 3, 4, 5})
633 for (
int i : {1, 2, 3})
636 for (
int i : {10, 20})
642 for (
int i : {1, 2, 3})
652 for (
int i : {1, 2, 3})
655 for (
int i : {10, 20})
686 for (
int i : {5, 3, 7, 1, 9})
691 keys.push_back(it.get_curr());
693 vector<int>
expected = {1, 3, 5, 7, 9};
701 for (
int i : {1, 2, 3})
724 for (
int i : {5, 3, 7, 1, 9})
730 vector<int>
expected = {1, 3, 5, 7, 9};
738 for (
int i : {5, 3, 7, 1, 9})
746 for (
int i : {1, 3, 5, 7, 9})
754 for (
int i : {5, 3, 7, 1, 9})
762 for (
int i : {1, 3, 5, 7, 9})
770 for (
int i : {1, 2, 3, 4, 5})
787 for (
int i : {1, 2, 3, 4, 5})
810 for (
int i : {5, 3, 7, 1, 9, 2, 8})
825 for (
int i : {5, 3, 7, 1, 9})
841 for (
int i : {5, 3, 7, 1, 9})
858 for (
int i : {5, 3, 7, 1, 9})
872 for (
int i : {5, 3, 7, 1, 9})
890 for (
int i : {2, 4, 6, 8, 10, 12, 14, 16, 18})
903 for (
int i : {2, 4, 6, 8, 10})
908 for (
int i : {12, 14, 16, 18})
920 for (
int i : {1, 2, 3, 4, 5, 6, 7, 8, 9})
942 for (
int i : {1, 2, 3, 4, 5, 6, 7, 8, 9})
953 for (
int i : {1, 2, 3, 4, 5})
958 for (
int i : {6, 7, 8, 9})
966 for (
int i : {1, 2, 3, 4, 5})
1003 for (
int i : {1, 2, 3})
1021 for (
int i : {1, 3, 5})
1024 for (
int i : {2, 4, 6})
1032 for (
int i : {1, 2, 3, 4, 5, 6})
1042 for (
int i : {1, 2, 3, 4, 5})
1045 for (
int i : {3, 4, 5, 6, 7})
1053 for (
int i : {1, 2, 3, 4, 5, 6, 7})
1057 for (
int i : {3, 4, 5})
1065 for (
int i : {1, 2, 3})
1068 for (
int i : {3, 4, 5})
1081template <
typename T>
1092 for (
typename T::Iterator it(set); it.has_curr(); it.next_ne())
1093 if (it.get_curr() == 42)
1114template <
typename T>
1125 EXPECT_THROW(set.split_pos(0, left, right), domain_error);
1147 for (
int i : {5, 3, 7, 1, 9})
1159 for (
int i : {5, 3, 7, 1, 9})
1170 for (
int i : {5, 3, 7, 1, 9})
1181 for (
int i : {5, 3, 7, 1, 9})
1198 for (
int i : {5, 3, 7, 1, 9})
1205 for (
DynSetTree<
int,
Avl_Tree, std::greater<int>>::Iterator it(set); it.has_curr(); it.next_ne())
1206 keys.push_back(it.get_curr());
1208 vector<int>
expected = {9, 7, 5, 3, 1};
1229 const int &
ref = set[42];
1243 for (
int i : {5, 3, 7, 1, 9, 2, 8, 4, 6})
1258 for (
int i = 0; i < 1000; ++i)
1266 for (
int i = 0; i < 1000; ++i)
1270 for (
int i = 0; i < 1000; i += 2)
1276 for (
int i = 0; i < 1000; ++i)
1288 vector<int> inserted;
1291 for (
int i = 0; i < 200; ++i)
1293 if (
int key =
rand() % 500; set.
insert(key) !=
nullptr)
1294 inserted.push_back(key);
1298 for (
int key : inserted)
1304 for (
size_t i = 0; i < inserted.size() / 2; ++i)
1306 int idx =
rand() % inserted.size();
1307 set.
remove(inserted[idx]);
1308 inserted.erase(inserted.begin() + idx);
1312 for (
int key : inserted)
1362 for (
int i = 0; i < 50; ++i)
1367 for (
int i = 0; i < 50; ++i)
1378 for (
int i : {1, 2, 3, 4, 5})
1392 inline static int alive = 0;
1394 TrackedKey() : v(0) { ++alive; }
1395 explicit TrackedKey(
int vv) : v(
vv) { ++alive; }
1396 TrackedKey(
const TrackedKey &
other) : v(
other.v) { ++alive; }
1401inline bool operator < (
const TrackedKey & a,
const TrackedKey & b)
1406template <
typename Key,
class Compare>
1407class ThrowingSearchOrInsertTree
1413 inline static bool enabled =
false;
1415 ThrowingSearchOrInsertTree(Compare c = Compare()) :
cmp(c) { }
1417 Compare get_compare()
const {
return cmp; }
1422 void swap(ThrowingSearchOrInsertTree &
other)
noexcept
1424 std::swap(root,
other.root);
1425 std::swap(cmp,
other.cmp);
1436 throw std::runtime_error(
"search_or_insert failed");
1444 Node * insert_dup(
Node * p)
noexcept
1451 Node *
remove(
const Key &)
noexcept {
return nullptr; }
1453 bool verify()
const {
return true; }
1467 TrackedKey::alive = 0;
1472 const int baseline = TrackedKey::alive;
1473 set.
insert(TrackedKey(1));
1478 EXPECT_THROW((
void) set.insert(TrackedKey(2)), std::runtime_error);
1489 TrackedKey::alive = 0;
1494 const int baseline = TrackedKey::alive;
1495 set.
insert(TrackedKey(1));
1500 EXPECT_THROW((
void) set.search_or_insert(TrackedKey(2)), std::runtime_error);
1511 TrackedKey::alive = 0;
1516 const int baseline = TrackedKey::alive;
1517 set.
insert(TrackedKey(1));
1522 EXPECT_THROW((
void) set.contains_or_insert(TrackedKey(2)), std::runtime_error);
1537 auto * p1 = this->set.
append(10);
1542 auto * p2 = this->set.
append(20);
1548 auto * p3 = this->set.
append(10);
1555 auto * p1 = this->set.
put(10);
1560 auto * p2 = this->set.
put(20);
1566 auto * p3 = this->set.
put(10);
1580 auto * p = this->set.
insert(std::move(val));
1588 auto * p = this->set.
append(std::move(val));
1596 auto * p = this->set.
put(std::move(val));
1622 auto * p1 = this->set.
insert_dup(std::move(val1));
1626 auto * p2 = this->set.
insert_dup(std::move(val2));
1643 for (
int i = 2; i <= 10; ++i)
1646 size_t h = this->set.
height();
1683 for (
int i : {5, 3, 7, 1, 9})
1692 for (
int i : {5, 3, 7, 1, 9})
1727 for (
int i : {1, 2, 3})
1730 for (
int i : {10, 20})
1736 for (
int i : {1, 2, 3})
1743 for (
int i : {1, 2, 3})
1746 this->set = this->set;
1749 for (
int i : {1, 2, 3})
1757 for (
int i : {1, 2, 3})
1760 for (
int i : {10, 20})
1763 set2 = std::move(this->set);
1766 for (
int i : {1, 2, 3})
1774 for (
int i : {1, 2, 3})
1778#if defined(__clang__)
1779# pragma clang diagnostic push
1780# pragma clang diagnostic ignored "-Wself-move"
1781#elif defined(__GNUC__)
1782# pragma GCC diagnostic push
1783# pragma GCC diagnostic ignored "-Wself-move"
1785 this->set = std::move(this->set);
1786#if defined(__clang__)
1787# pragma clang diagnostic pop
1788#elif defined(__GNUC__)
1789# pragma GCC diagnostic pop
1805 for (
int i : {1, 3, 5, 7, 9})
1832 for (
int i : {1, 2, 3, 4, 5})
1877 for (
int i : {5, 3, 7, 1, 9})
1890 for (
int i : {1, 2, 3})
1908 for (
int i : {1, 2, 3})
1928 for (
int i : {1, 3, 5, 7, 9})
1941 for (
int i : {5, 3, 7, 1, 9})
1956 typename TypeParam::Iterator it(this->set);
1963 for (
int i : {1, 2, 3})
1966 typename TypeParam::Iterator it(this->set);
1969 while (it.has_curr())
1979 for (
int i : {1, 2, 3})
1982 typename TypeParam::Iterator it(this->set);
1995 for (
int i : {1, 2, 3, 4, 5})
2010 for (
int i : {1, 2, 3, 4, 5})
2033 for (
int i : {1, 2, 3})
2061 for (
int i : {1, 2, 3})
2105 auto * p = set.
insert(std::move(s));
2116 string s1 =
"hello";
2121 string s2 =
"hello";
2200 for (
int k = 0;
k <
N; ++
k)
2212 for (
int k =
N - 1;
k >= 0; --
k)
2223 for (
int k = 0;
k <
N; ++
k)
2229 for (
int k = 0;
k <
N; ++
k)
2238 std::mt19937
gen(12345);
2239 std::uniform_int_distribution<>
key_dist(0, 1000);
2240 std::uniform_int_distribution<>
op_dist(0, 2);
2249 if (this->set.
insert(key) !=
nullptr)
2252 else if (op == 1 && !
oracle.empty())
2254 auto it =
oracle.begin();
2255 std::advance(it,
gen() %
oracle.size());
2277 std::mt19937
gen(54321);
2278 std::uniform_int_distribution<>
key_dist(0, 500);
2286 if (this->set.
insert(key) !=
nullptr)
2289 else if (!
oracle.empty())
2291 auto it =
oracle.begin();
2292 std::advance(it,
gen() %
oracle.size());
2310 std::mt19937
gen(99999);
2311 std::uniform_int_distribution<>
key_dist(0, 10000);
2312 std::uniform_int_distribution<>
op_dist(0, 2);
2321 if (tree.insert(key) !=
nullptr)
2324 else if (op == 1 && !
oracle.empty())
2326 auto it =
oracle.begin();
2327 std::advance(it,
gen() %
oracle.size());
2362 ::testing::InitGoogleTest(&
argc,
argv);
bool operator<(const Time &l, const Time &r)
WeightedDigraph::Node Node
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
size_t height() const
Calculates and returns the height of the binary search tree.
long position(const Key &key) const
Returns the infix (ordered) position of the key.
Key * append(const Key &key)
const Key & get_last() const
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 a key from the dynamic set.
Key del(const Key &key)
Deletes key and returns a full copy of stored key.
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.
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 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)
Key & find(const Key &key) const
Returns a modifiable reference to an element within the set.
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.
const Key & min() const
Returns the smallest key contained in the set according to the criterion comparison given.
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 * 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.
Key * search(const Key &key) const
Find an element in the set.
Key & select(size_t i)
Returns the ith node in infix position.
void empty()
remove all elements from the set
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.
const Key & max() const
Returns the largest key contained in the set according to the criteria comparison given.
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
::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.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
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.
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.
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 virtual destructor.
Dynamic set implementations based on balanced binary search trees.