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})
973 tmp.split_pos(0, left, right);
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)
1291 for (
int i = 0; i < 200; ++i)
1293 if (
int key =
rand() % 500; set.
insert(key) !=
nullptr)
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);
1431 throw std::runtime_error(
"search_or_insert failed");
1439 bool verify()
const {
return true; }
1453 TrackedKey::alive = 0;
1458 const int baseline = TrackedKey::alive;
1459 set.
insert(TrackedKey(1));
1464 EXPECT_THROW((
void) set.insert(TrackedKey(2)), std::runtime_error);
1475 TrackedKey::alive = 0;
1480 const int baseline = TrackedKey::alive;
1481 set.
insert(TrackedKey(1));
1486 EXPECT_THROW((
void) set.search_or_insert(TrackedKey(2)), std::runtime_error);
1497 TrackedKey::alive = 0;
1502 const int baseline = TrackedKey::alive;
1503 set.
insert(TrackedKey(1));
1508 EXPECT_THROW((
void) set.contains_or_insert(TrackedKey(2)), std::runtime_error);
1523 auto * p1 = this->set.
append(10);
1528 auto * p2 = this->set.
append(20);
1534 auto * p3 = this->set.
append(10);
1541 auto * p1 = this->set.
put(10);
1546 auto * p2 = this->set.
put(20);
1552 auto * p3 = this->set.
put(10);
1566 auto * p = this->set.
insert(std::move(val));
1574 auto * p = this->set.
append(std::move(val));
1582 auto * p = this->set.
put(std::move(val));
1608 auto * p1 = this->set.
insert_dup(std::move(val1));
1612 auto * p2 = this->set.
insert_dup(std::move(val2));
1629 for (
int i = 2; i <= 10; ++i)
1632 size_t h = this->set.
height();
1669 for (
int i : {5, 3, 7, 1, 9})
1678 for (
int i : {5, 3, 7, 1, 9})
1713 for (
int i : {1, 2, 3})
1716 for (
int i : {10, 20})
1722 for (
int i : {1, 2, 3})
1729 for (
int i : {1, 2, 3})
1732 this->set = this->set;
1735 for (
int i : {1, 2, 3})
1743 for (
int i : {1, 2, 3})
1746 for (
int i : {10, 20})
1749 set2 = std::move(this->set);
1752 for (
int i : {1, 2, 3})
1760 for (
int i : {1, 2, 3})
1764#if defined(__clang__)
1765# pragma clang diagnostic push
1766# pragma clang diagnostic ignored "-Wself-move"
1767#elif defined(__GNUC__)
1768# pragma GCC diagnostic push
1769# pragma GCC diagnostic ignored "-Wself-move"
1771 this->set = std::move(this->set);
1772#if defined(__clang__)
1773# pragma clang diagnostic pop
1774#elif defined(__GNUC__)
1775# pragma GCC diagnostic pop
1791 for (
int i : {1, 3, 5, 7, 9})
1818 for (
int i : {1, 2, 3, 4, 5})
1863 for (
int i : {5, 3, 7, 1, 9})
1876 for (
int i : {1, 2, 3})
1894 for (
int i : {1, 2, 3})
1914 for (
int i : {1, 3, 5, 7, 9})
1927 for (
int i : {5, 3, 7, 1, 9})
1942 typename TypeParam::Iterator it(this->set);
1949 for (
int i : {1, 2, 3})
1952 typename TypeParam::Iterator it(this->set);
1955 while (it.has_curr())
1965 for (
int i : {1, 2, 3})
1968 typename TypeParam::Iterator it(this->set);
1981 for (
int i : {1, 2, 3, 4, 5})
1996 for (
int i : {1, 2, 3, 4, 5})
2019 for (
int i : {1, 2, 3})
2047 for (
int i : {1, 2, 3})
2091 auto * p = set.
insert(std::move(s));
2102 string s1 =
"hello";
2107 string s2 =
"hello";
2186 for (
int k = 0;
k <
N; ++
k)
2198 for (
int k =
N - 1;
k >= 0; --
k)
2209 for (
int k = 0;
k <
N; ++
k)
2215 for (
int k = 0;
k <
N; ++
k)
2224 std::mt19937
gen(12345);
2225 std::uniform_int_distribution<>
key_dist(0, 1000);
2226 std::uniform_int_distribution<>
op_dist(0, 2);
2235 if (this->set.
insert(key) !=
nullptr)
2263 std::mt19937
gen(54321);
2264 std::uniform_int_distribution<>
key_dist(0, 500);
2272 if (this->set.
insert(key) !=
nullptr)
2296 std::mt19937
gen(99999);
2297 std::uniform_int_distribution<>
key_dist(0, 10000);
2298 std::uniform_int_distribution<>
op_dist(0, 2);
2307 if (tree.insert(key) !=
nullptr)
2348 ::testing::InitGoogleTest(&
argc,
argv);
bool operator<(const Time &l, const Time &r)
WeightedDigraph::Node Node
Node for binary search tree.
static BinNode *const NullPtr
T & insert(const T &item)
Insert a new item by copy.
T remove()
Remove the first item of the list.
void empty() noexcept
empty the list
DynList & swap(DynList &l) noexcept
Swap this with l.
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
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
constexpr bool is_empty() const noexcept
Return true if list is empty.
size_t size() const noexcept
Count the number of elements of the list.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
::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)
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.
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.
DynList< T > maps(const C &c, Op op)
Classic map operation.
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.
bool traverse(Operation &operation) noexcept(traverse_is_noexcept< Operation >())
Traverse the container via its iterator and performs a conditioned operation on each item.
Dynamic set implementations based on balanced binary search trees.