forked from jonstewart/boost-svn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rbtree_algorithm_test.cpp
212 lines (182 loc) · 7.47 KB
/
rbtree_algorithm_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "boost/intrusive/options.hpp"
#include "boost/intrusive/rbtree_algorithms.hpp"
#include "boost/intrusive/monoid_annotation.hpp"
#include "boost/intrusive/detail/generic_annotation_list.hpp"
#include "boost/intrusive/detail/generic_annotated_node.hpp"
#include "boost/intrusive/detail/rbtree_node.hpp"
#include "boost/intrusive/set_hook.hpp"
#include "boost/intrusive/rbtree.hpp"
#include <cstddef>
//#include <type_traits>
#include <iostream>
struct subtree_count_annotation :
boost::intrusive::fixed_value_semigroup_annotation< subtree_count_annotation
, std::size_t
, std::plus<std::size_t>
, 1>
{};
typedef boost::intrusive::annotations<subtree_count_annotation> my_annotation_list;
typedef boost::intrusive::detail::generic_annotation_list_traits<void*,my_annotation_list> annotation_list_traits;
typedef boost::intrusive::detail::generic_annotation_list_traits<void*,my_annotation_list> annotation_list_traits;
typedef boost::intrusive::detail::generic_annotated_node_traits<boost::intrusive::rbtree_node_traits<void*,false>, annotation_list_traits> my_annotated_node_traits;
typedef my_annotated_node_traits::annotated_node MonoidNode;
struct Value : public MonoidNode
{
static int a;
Value() : val(a++) {}
int val;
};
int Value::a = 999;
bool operator<(const my_annotated_node_traits::node& a, const my_annotated_node_traits::node& b) { return static_cast<const Value&>(a).val < static_cast<const Value&>(b).val; }
struct Compare
{
bool operator()(my_annotated_node_traits::const_node_ptr a, my_annotated_node_traits::const_node_ptr b) { return *a < *b; }
};
typedef boost::intrusive::annotated_rbtree_algorithms<my_annotated_node_traits,my_annotation_list> ralgo;
typedef boost::intrusive::detail::annotated_tree_algorithms<my_annotated_node_traits,my_annotation_list> algo;
bool check(my_annotated_node_traits::node_ptr header, MonoidNode* node)
{
size_t count = 0;
while (node != header) {
if (ralgo::count(node) != my_annotated_node_traits::annotation_list_traits::get_annotation_value<subtree_count_annotation>(
my_annotated_node_traits::to_annotation_list_ptr(node)))
std::cout << "oops!" << std::endl;
node = static_cast<MonoidNode*>(ralgo::next_node(node));
}
}
void test_algo()
{
my_annotated_node_traits::node header;
ralgo::init_header(&header);
for (int i = 0; i < 100; ++i) {
ralgo::insert_equal(&header, &header, new Value(), Compare());
check(&header, static_cast<MonoidNode*>(my_annotated_node_traits::node_traits::get_left(&header)));
std::cout << i << " ";
}
std::cout << std::endl;
Value::a = -100;
for (int i = 0; i < 100; ++i) {
ralgo::insert_equal(&header, &header, new Value(), Compare());
check(&header, static_cast<MonoidNode*>(my_annotated_node_traits::node_traits::get_left(&header)));
std::cout << i << " ";
}
std::cout << std::endl;
Value::a = 0;
for (int i = 0; i < 50; ++i) {
// XXX the explicit temporary pointer is a workaround for bug #6978
//ralgo::erase(&header, my_annotated_node_traits::node_ptr(my_annotated_node_traits::node_traits::get_left(&header)));
ralgo::erase(&header, my_annotated_node_traits::node_traits::get_left(&header));
check(&header, static_cast<MonoidNode*>(my_annotated_node_traits::node_traits::get_left(&header)));
std::cout << i << " ";
}
std::cout << std::endl;
for (int i = 0; i < 50; ++i) {
ralgo::erase(&header, my_annotated_node_traits::node_ptr(my_annotated_node_traits::node_traits::get_right(&header)));
check(&header, static_cast<MonoidNode*>(my_annotated_node_traits::node_traits::get_right(&header)));
std::cout << i << " ";
}
std::cout << std::endl;
}
typedef boost::intrusive::set_base_hook<my_annotation_list> my_hook;
struct ValueUsingHook : public my_hook
{
static int a;
ValueUsingHook() : val(a++) {}
int val;
};
int ValueUsingHook::a = 998;
struct CompareUsingHook
{
bool operator()(my_hook::boost_intrusive_tags::node_traits::const_node_ptr a, my_hook::boost_intrusive_tags::node_traits::const_node_ptr b) { return static_cast<const ValueUsingHook&>(*a).val < static_cast<const ValueUsingHook&>(*b).val; }
};
bool check_hook(my_hook::boost_intrusive_tags::node_traits::node_ptr header, my_hook* node)
{
size_t count = 0;
while (node != header) {
if (ralgo::count(node) != node->get_annotation_value<subtree_count_annotation>())
std::cout << "oops!" << std::endl;
node = static_cast<my_hook*>(ralgo::next_node(node));
}
}
void test_hook()
{
typedef boost::intrusive::annotated_rbtree_algorithms<my_hook::boost_intrusive_tags::annotated_node_traits, my_annotation_list> node_algorithms;
// typedef boost::intrusive::annotated_rbtree_algorithms<my_hook::boost_intrusive_tags::annotated_node_traits, boost::intrusive::annotations<> > node_algorithms;
node_algorithms::node header;
node_algorithms::init_header(&header);
typedef node_algorithms ralgo;
typedef ralgo::node_traits node_traits;
for (int i = 0; i < 100; ++i) {
ralgo::insert_equal(&header, &header, new ValueUsingHook(), CompareUsingHook());
check_hook(&header, static_cast<my_hook*>(node_traits::get_left(&header)));
std::cout << i << " ";
}
std::cout << std::endl;
ValueUsingHook::a = -100;
for (int i = 0; i < 100; ++i) {
ralgo::insert_equal(&header, &header, new ValueUsingHook(), Compare());
check_hook(&header, static_cast<my_hook*>(node_traits::get_left(&header)));
std::cout << i << " ";
}
std::cout << std::endl;
ValueUsingHook::a = 0;
for (int i = 0; i < 50; ++i) {
ralgo::erase(&header, my_annotated_node_traits::node_ptr(node_traits::get_left(&header)));
check_hook(&header, static_cast<my_hook*>(node_traits::get_left(&header)));
std::cout << i << " ";
}
std::cout << std::endl;
for (int i = 0; i < 50; ++i) {
ralgo::erase(&header, my_annotated_node_traits::node_ptr(node_traits::get_right(&header)));
check_hook(&header, static_cast<my_hook*>(node_traits::get_right(&header)));
std::cout << i << " ";
}
std::cout << std::endl;
}
bool operator<(const ValueUsingHook& a, const ValueUsingHook& b) { return a.val < b.val; }
void test_tree()
{
typedef boost::intrusive::rbtree<ValueUsingHook, boost::intrusive::base_hook<my_hook> > my_tree;
// typedef boost::intrusive::rbtree<ValueUsingHook, boost::intrusive::base_hook<my_hook>, boost::intrusive::annotations<> > my_tree;
my_tree tree;
typedef my_tree::node_algorithms node_algorithms;
typedef my_tree::node_traits node_traits;
node_algorithms::node& header(*tree.end());
ValueUsingHook::a = 0;
for (int i = 0; i < 100; ++i) {
tree.insert_equal(*(new ValueUsingHook));
check_hook(&header, static_cast<my_hook*>(node_traits::get_left(&header)));
std::cout << i << " ";
}
std::cout << std::endl;
ValueUsingHook::a = -100;
for (int i = 0; i < 100; ++i) {
tree.insert_equal(*(new ValueUsingHook));
check_hook(&header, static_cast<my_hook*>(node_traits::get_left(&header)));
std::cout << i << " ";
}
std::cout << std::endl;
ValueUsingHook::a = 0;
for (int i = 0; i < 50; ++i) {
tree.erase(tree.begin());
check_hook(&header, static_cast<my_hook*>(node_traits::get_left(&header)));
std::cout << i << " ";
}
std::cout << std::endl;
for (int i = 0; i < 50; ++i) {
my_tree::iterator endIt = tree.end();
tree.erase(--endIt);
check_hook(&header, static_cast<my_hook*>(node_traits::get_right(&header)));
std::cout << i << " ";
}
std::cout << std::endl;
}
int main()
{
test_algo();
test_hook();
test_tree();
// int x = *((int*)0);
// ralgo::rebalance_after_erasure(&header, 0, &n5);
return 0;
}