44#include <gtest/gtest.h>
49using namespace testing;
58 bool operator()(
int a,
int b)
const
60 return std::abs(a) < std::abs(b);
66 std::vector<Node *> allocated;
70 auto * p =
new Node(
k);
71 allocated.push_back(p);
77 for (
auto & q : allocated)
87 for (
auto * p : allocated)
94 if (
root == Node::NullPtr)
103 std::vector<int> keys;
104 if (
root == Node::NullPtr)
108 keys.insert(keys.end(), left.begin(), left.end());
111 keys.insert(keys.end(), right.begin(), right.end());
119 if (
root != Node::NullPtr)
144 auto * p = pool.make(42);
159 for (
int k : {5, 3, 7, 1, 4, 6, 8})
161 auto * p = pool.make(
k);
169 EXPECT_EQ(keys, (std::vector<int>{1, 3, 4, 5, 6, 7, 8}));
177 auto * p1 = pool.make(10);
180 auto * p2 = pool.make(10);
192 for (
int i = 0; i < 5; ++i)
199 EXPECT_EQ(keys, (std::vector<int>{42, 42, 42, 42, 42}));
207 for (
int k : {1, 2, 3, 4, 5})
210 for (
int k : {1, 2, 3, 4, 5})
225 for (
int k : {1, 3, 5})
242 auto * p1 = pool.make(10);
248 auto * p2 = pool.make(10);
266 for (
int k : {1, 2, 3, 4, 5})
280 EXPECT_EQ(keys, (std::vector<int>{1, 2, 4, 5}));
288 for (
int k : {1, 3, 5})
312 tree.
insert(pool.make(1));
313 tree.
insert(pool.make(2));
314 tree.
insert(pool.make(3));
332 std::vector<int> keys = {5, 3, 7, 1, 4, 6, 8};
358 for (
int k : {5, 3, 7, 1, 4, 6, 8})
362 std::vector<int>
expected = {1, 3, 4, 5, 6, 7, 8};
366 auto * node = tree.
select(i);
367 ASSERT_NE(node, Node::NullPtr) <<
"select(" << i <<
") returned null";
377 for (
int k : {1, 2, 3})
389 for (
int k : {5, 3, 7, 1, 4, 6, 8})
393 std::vector<std::pair<int, long>>
expected = {
394 {1, 0}, {3, 1}, {4, 2}, {5, 3}, {6, 4}, {7, 5}, {8, 6}
400 EXPECT_EQ(result.first, pos) <<
"position(" << key <<
") wrong";
411 for (
int k : {2, 4, 6})
438 for (
int k : {1, 2, 3, 4, 5})
462 for (
int k : {5, 3, 7, 1, 4, 6, 8})
468 for (
int k : {1, 8, 4, 6, 3, 7, 5})
485 auto * p = pool.make(42);
504 for (
int k = 10;
k >= 1; --
k)
511 EXPECT_EQ(keys, (std::vector<int>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}));
519 for (
int k = 1;
k <= 10; ++
k)
526 EXPECT_EQ(keys, (std::vector<int>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}));
536 using NodeGt = TreeGt::Node;
539 std::vector<NodeGt *>
nodes;
541 for (
int k : {1, 2, 3, 4, 5})
552 std::vector<int> keys;
554 if (r == NodeGt::NullPtr)
return;
556 keys.push_back(
KEY(r));
561 EXPECT_EQ(keys, (std::vector<int>{5, 4, 3, 2, 1}));
563 for (
auto * p :
nodes)
594 std::mt19937
rng(42);
595 std::uniform_int_distribution<int> dist(0, 500);
598 for (
int i = 0; i < 200; ++i)
601 auto * p = pool.make(
k);
602 if (tree.
insert(p) !=
nullptr)
619 for (
int i = 0; i < 100; ++i)
635 for (
int i = 0; i < 150; ++i)
664 std::mt19937
rng(123);
665 std::uniform_int_distribution<int> dist(0, 1000);
668 for (
int i = 0; i < 100; ++i)
671 auto * p = pool.make(
k);
672 if (tree.
insert(p) !=
nullptr)
685 auto * node = tree.
select(i);
704 for (
int k : {5, 3, 7, 1, 4, 6, 8})
727 for (
int k : {1, 2, 3})
730 for (
int k : {10, 20})
755 for (
int k = 0;
k <
N; ++
k)
766 for (
int i = 0; i < 100; ++i)
768 int pos =
rand() %
N;
769 auto * node = tree.
select(pos);
781 for (
int k =
N - 1;
k >= 0; --
k)
794 for (
int i = 0; i <
N; ++i)
796 int k = (i % 2 == 0) ? i / 2 :
N - 1 - i / 2;
810 std::mt19937
gen(98765);
811 std::uniform_int_distribution<>
key_dist(0, 20000);
812 std::uniform_int_distribution<>
op_dist(0, 2);
821 auto * p = pool.make(key);
822 if (tree.
insert(p) !=
nullptr)
853 if (
iter % 2000 == 0)
868 for (
int k = 0;
k <
N; ++
k)
877 std::mt19937
gen(11111);
899 for (
int k = 0;
k <
N; ++
k)
900 for (
int d = 0; d <
DUPS; ++d)
912 auto * node = tree.
select(0);
937 std::mt19937
gen(22222);
938 std::uniform_int_distribution<>
key_dist(0, 5000);
941 for (
int i = 0; i < 2000; ++i)
944 auto * p = pool.make(key);
945 if (tree.
insert(p) !=
nullptr)
959 auto * node = tree.
select(i);
976 for (
int k = 0;
k <
N; ++
k)
979 std::mt19937
gen(33333);
980 std::uniform_int_distribution<> dist(0,
N - 1);
984 for (
int i = 0; i < 5000; ++i)
WeightedDigraph::Node Node
T & insert(const T &item)
Insert a new item by copy.
void empty() noexcept
empty the list
DynList & swap(DynList &l) noexcept
Swap this with l.
bool is_empty() const noexcept
Return true if the tree is empty.
bool verify() const
Return true if this is a consistent randomized tree.
Node * remove(const Key &key) noexcept
Remove a key from the tree.
Node * search_or_insert(Node *p) noexcept
Search or insert a key.
Node * insert_dup(Node *p) noexcept
Insert a node in the tree.
Node * insert(Node *p) noexcept
Insert a node in the tree.
size_t size() const noexcept
Return the number of nodes that have the tree.
Node * select(const size_t i) const
Return the i-th node in inorder sense.
Node *& getRoot() noexcept
Return the tree's root.
Node * search(const Key &key) const noexcept
Search a key.
size_t size() const noexcept
Count the number of elements of the list.
Top-down splay tree with rank support.
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
__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)
void delete_tree(Tree_Node< T > *node)
DynArray< Graph::Node * > nodes
std::pair< long, Node * > position(const Key &key) const noexcept
Compute the inorder position of a key.
constexpr Node *& RLINK(Node *p) noexcept
Return the right tree of p.
auto & COUNT(Node *p) noexcept
Return the number of nodes of the tree fron p is root.
constexpr Node *& LLINK(Node *p) noexcept
Return a pointer to left subtree.
Main namespace for Aleph-w library functions.
DynList< T > maps(const C &c, Op op)
Classic map operation.
std::vector< int > inorder_keys(NodeT *root)
Top-down splay tree with rank support.