Chris@16
|
1 /////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 //
|
Chris@16
|
3 // (C) Copyright Daniel K. O. 2005.
|
Chris@101
|
4 // (C) Copyright Ion Gaztanaga 2007-2014
|
Chris@16
|
5 //
|
Chris@16
|
6 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
7 // (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
8 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //
|
Chris@16
|
10 // See http://www.boost.org/libs/intrusive for documentation.
|
Chris@16
|
11 //
|
Chris@16
|
12 /////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
13
|
Chris@16
|
14 #ifndef BOOST_INTRUSIVE_AVLTREE_ALGORITHMS_HPP
|
Chris@16
|
15 #define BOOST_INTRUSIVE_AVLTREE_ALGORITHMS_HPP
|
Chris@16
|
16
|
Chris@16
|
17 #include <boost/intrusive/detail/config_begin.hpp>
|
Chris@101
|
18 #include <boost/intrusive/intrusive_fwd.hpp>
|
Chris@16
|
19
|
Chris@16
|
20 #include <cstddef>
|
Chris@16
|
21
|
Chris@16
|
22 #include <boost/intrusive/detail/assert.hpp>
|
Chris@101
|
23 #include <boost/intrusive/detail/algo_type.hpp>
|
Chris@101
|
24 #include <boost/intrusive/detail/ebo_functor_holder.hpp>
|
Chris@16
|
25 #include <boost/intrusive/bstree_algorithms.hpp>
|
Chris@101
|
26
|
Chris@101
|
27 #if defined(BOOST_HAS_PRAGMA_ONCE)
|
Chris@101
|
28 # pragma once
|
Chris@101
|
29 #endif
|
Chris@16
|
30
|
Chris@16
|
31
|
Chris@16
|
32 namespace boost {
|
Chris@16
|
33 namespace intrusive {
|
Chris@16
|
34
|
Chris@16
|
35 /// @cond
|
Chris@16
|
36
|
Chris@16
|
37 template<class NodeTraits, class F>
|
Chris@16
|
38 struct avltree_node_cloner
|
Chris@101
|
39 //Use public inheritance to avoid MSVC bugs with closures
|
Chris@101
|
40 : public detail::ebo_functor_holder<F>
|
Chris@16
|
41 {
|
Chris@16
|
42 typedef typename NodeTraits::node_ptr node_ptr;
|
Chris@16
|
43 typedef detail::ebo_functor_holder<F> base_t;
|
Chris@16
|
44
|
Chris@16
|
45 avltree_node_cloner(F f)
|
Chris@16
|
46 : base_t(f)
|
Chris@16
|
47 {}
|
Chris@16
|
48
|
Chris@16
|
49 node_ptr operator()(const node_ptr & p)
|
Chris@16
|
50 {
|
Chris@16
|
51 node_ptr n = base_t::get()(p);
|
Chris@16
|
52 NodeTraits::set_balance(n, NodeTraits::get_balance(p));
|
Chris@16
|
53 return n;
|
Chris@16
|
54 }
|
Chris@101
|
55
|
Chris@101
|
56 node_ptr operator()(const node_ptr & p) const
|
Chris@101
|
57 {
|
Chris@101
|
58 node_ptr n = base_t::get()(p);
|
Chris@101
|
59 NodeTraits::set_balance(n, NodeTraits::get_balance(p));
|
Chris@101
|
60 return n;
|
Chris@101
|
61 }
|
Chris@16
|
62 };
|
Chris@16
|
63
|
Chris@101
|
64 namespace detail {
|
Chris@101
|
65
|
Chris@101
|
66 template<class ValueTraits, class NodePtrCompare, class ExtraChecker>
|
Chris@101
|
67 struct avltree_node_checker
|
Chris@101
|
68 : public bstree_node_checker<ValueTraits, NodePtrCompare, ExtraChecker>
|
Chris@16
|
69 {
|
Chris@101
|
70 typedef bstree_node_checker<ValueTraits, NodePtrCompare, ExtraChecker> base_checker_t;
|
Chris@101
|
71 typedef ValueTraits value_traits;
|
Chris@101
|
72 typedef typename value_traits::node_traits node_traits;
|
Chris@101
|
73 typedef typename node_traits::const_node_ptr const_node_ptr;
|
Chris@16
|
74
|
Chris@101
|
75 struct return_type
|
Chris@101
|
76 : public base_checker_t::return_type
|
Chris@101
|
77 {
|
Chris@101
|
78 return_type() : height(0) {}
|
Chris@101
|
79 int height;
|
Chris@101
|
80 };
|
Chris@101
|
81
|
Chris@101
|
82 avltree_node_checker(const NodePtrCompare& comp, ExtraChecker extra_checker)
|
Chris@101
|
83 : base_checker_t(comp, extra_checker)
|
Chris@101
|
84 {}
|
Chris@101
|
85
|
Chris@101
|
86 void operator () (const const_node_ptr& p,
|
Chris@101
|
87 const return_type& check_return_left, const return_type& check_return_right,
|
Chris@101
|
88 return_type& check_return)
|
Chris@101
|
89 {
|
Chris@101
|
90 const int height_diff = check_return_right.height - check_return_left.height; (void)height_diff;
|
Chris@101
|
91 BOOST_INTRUSIVE_INVARIANT_ASSERT(
|
Chris@101
|
92 (height_diff == -1 && node_traits::get_balance(p) == node_traits::negative()) ||
|
Chris@101
|
93 (height_diff == 0 && node_traits::get_balance(p) == node_traits::zero()) ||
|
Chris@101
|
94 (height_diff == 1 && node_traits::get_balance(p) == node_traits::positive())
|
Chris@101
|
95 );
|
Chris@101
|
96 check_return.height = 1 +
|
Chris@101
|
97 (check_return_left.height > check_return_right.height ? check_return_left.height : check_return_right.height);
|
Chris@101
|
98 base_checker_t::operator()(p, check_return_left, check_return_right, check_return);
|
Chris@101
|
99 }
|
Chris@16
|
100 };
|
Chris@16
|
101
|
Chris@101
|
102 } // namespace detail
|
Chris@101
|
103
|
Chris@16
|
104 /// @endcond
|
Chris@16
|
105
|
Chris@16
|
106 //! avltree_algorithms is configured with a NodeTraits class, which encapsulates the
|
Chris@16
|
107 //! information about the node to be manipulated. NodeTraits must support the
|
Chris@16
|
108 //! following interface:
|
Chris@16
|
109 //!
|
Chris@16
|
110 //! <b>Typedefs</b>:
|
Chris@16
|
111 //!
|
Chris@16
|
112 //! <tt>node</tt>: The type of the node that forms the binary search tree
|
Chris@16
|
113 //!
|
Chris@16
|
114 //! <tt>node_ptr</tt>: A pointer to a node
|
Chris@16
|
115 //!
|
Chris@16
|
116 //! <tt>const_node_ptr</tt>: A pointer to a const node
|
Chris@16
|
117 //!
|
Chris@16
|
118 //! <tt>balance</tt>: The type of the balance factor
|
Chris@16
|
119 //!
|
Chris@16
|
120 //! <b>Static functions</b>:
|
Chris@16
|
121 //!
|
Chris@16
|
122 //! <tt>static node_ptr get_parent(const_node_ptr n);</tt>
|
Chris@16
|
123 //!
|
Chris@16
|
124 //! <tt>static void set_parent(node_ptr n, node_ptr parent);</tt>
|
Chris@16
|
125 //!
|
Chris@16
|
126 //! <tt>static node_ptr get_left(const_node_ptr n);</tt>
|
Chris@16
|
127 //!
|
Chris@16
|
128 //! <tt>static void set_left(node_ptr n, node_ptr left);</tt>
|
Chris@16
|
129 //!
|
Chris@16
|
130 //! <tt>static node_ptr get_right(const_node_ptr n);</tt>
|
Chris@16
|
131 //!
|
Chris@16
|
132 //! <tt>static void set_right(node_ptr n, node_ptr right);</tt>
|
Chris@16
|
133 //!
|
Chris@16
|
134 //! <tt>static balance get_balance(const_node_ptr n);</tt>
|
Chris@16
|
135 //!
|
Chris@16
|
136 //! <tt>static void set_balance(node_ptr n, balance b);</tt>
|
Chris@16
|
137 //!
|
Chris@16
|
138 //! <tt>static balance negative();</tt>
|
Chris@16
|
139 //!
|
Chris@16
|
140 //! <tt>static balance zero();</tt>
|
Chris@16
|
141 //!
|
Chris@16
|
142 //! <tt>static balance positive();</tt>
|
Chris@16
|
143 template<class NodeTraits>
|
Chris@16
|
144 class avltree_algorithms
|
Chris@16
|
145 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
Chris@16
|
146 : public bstree_algorithms<NodeTraits>
|
Chris@16
|
147 #endif
|
Chris@16
|
148 {
|
Chris@16
|
149 public:
|
Chris@16
|
150 typedef typename NodeTraits::node node;
|
Chris@16
|
151 typedef NodeTraits node_traits;
|
Chris@16
|
152 typedef typename NodeTraits::node_ptr node_ptr;
|
Chris@16
|
153 typedef typename NodeTraits::const_node_ptr const_node_ptr;
|
Chris@16
|
154 typedef typename NodeTraits::balance balance;
|
Chris@16
|
155
|
Chris@16
|
156 /// @cond
|
Chris@16
|
157 private:
|
Chris@16
|
158 typedef bstree_algorithms<NodeTraits> bstree_algo;
|
Chris@16
|
159
|
Chris@16
|
160 /// @endcond
|
Chris@16
|
161
|
Chris@16
|
162 public:
|
Chris@16
|
163 //! This type is the information that will be
|
Chris@16
|
164 //! filled by insert_unique_check
|
Chris@16
|
165 typedef typename bstree_algo::insert_commit_data insert_commit_data;
|
Chris@16
|
166
|
Chris@16
|
167 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
Chris@16
|
168
|
Chris@16
|
169 //! @copydoc ::boost::intrusive::bstree_algorithms::get_header(const const_node_ptr&)
|
Chris@16
|
170 static node_ptr get_header(const const_node_ptr & n);
|
Chris@16
|
171
|
Chris@16
|
172 //! @copydoc ::boost::intrusive::bstree_algorithms::begin_node
|
Chris@16
|
173 static node_ptr begin_node(const const_node_ptr & header);
|
Chris@16
|
174
|
Chris@16
|
175 //! @copydoc ::boost::intrusive::bstree_algorithms::end_node
|
Chris@16
|
176 static node_ptr end_node(const const_node_ptr & header);
|
Chris@16
|
177
|
Chris@16
|
178 //! @copydoc ::boost::intrusive::bstree_algorithms::swap_tree
|
Chris@16
|
179 static void swap_tree(const node_ptr & header1, const node_ptr & header2);
|
Chris@101
|
180
|
Chris@16
|
181 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
Chris@16
|
182
|
Chris@16
|
183 //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(const node_ptr&,const node_ptr&)
|
Chris@16
|
184 static void swap_nodes(const node_ptr & node1, const node_ptr & node2)
|
Chris@16
|
185 {
|
Chris@16
|
186 if(node1 == node2)
|
Chris@16
|
187 return;
|
Chris@16
|
188
|
Chris@16
|
189 node_ptr header1(bstree_algo::get_header(node1)), header2(bstree_algo::get_header(node2));
|
Chris@16
|
190 swap_nodes(node1, header1, node2, header2);
|
Chris@16
|
191 }
|
Chris@16
|
192
|
Chris@16
|
193 //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(const node_ptr&,const node_ptr&,const node_ptr&,const node_ptr&)
|
Chris@16
|
194 static void swap_nodes(const node_ptr & node1, const node_ptr & header1, const node_ptr & node2, const node_ptr & header2)
|
Chris@16
|
195 {
|
Chris@16
|
196 if(node1 == node2) return;
|
Chris@16
|
197
|
Chris@16
|
198 bstree_algo::swap_nodes(node1, header1, node2, header2);
|
Chris@16
|
199 //Swap balance
|
Chris@16
|
200 balance c = NodeTraits::get_balance(node1);
|
Chris@16
|
201 NodeTraits::set_balance(node1, NodeTraits::get_balance(node2));
|
Chris@16
|
202 NodeTraits::set_balance(node2, c);
|
Chris@16
|
203 }
|
Chris@16
|
204
|
Chris@16
|
205 //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(const node_ptr&,const node_ptr&)
|
Chris@16
|
206 static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & new_node)
|
Chris@16
|
207 {
|
Chris@16
|
208 if(node_to_be_replaced == new_node)
|
Chris@16
|
209 return;
|
Chris@16
|
210 replace_node(node_to_be_replaced, bstree_algo::get_header(node_to_be_replaced), new_node);
|
Chris@16
|
211 }
|
Chris@16
|
212
|
Chris@16
|
213 //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(const node_ptr&,const node_ptr&,const node_ptr&)
|
Chris@16
|
214 static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & header, const node_ptr & new_node)
|
Chris@16
|
215 {
|
Chris@16
|
216 bstree_algo::replace_node(node_to_be_replaced, header, new_node);
|
Chris@16
|
217 NodeTraits::set_balance(new_node, NodeTraits::get_balance(node_to_be_replaced));
|
Chris@16
|
218 }
|
Chris@16
|
219
|
Chris@16
|
220 //! @copydoc ::boost::intrusive::bstree_algorithms::unlink(const node_ptr&)
|
Chris@16
|
221 static void unlink(const node_ptr & node)
|
Chris@16
|
222 {
|
Chris@16
|
223 node_ptr x = NodeTraits::get_parent(node);
|
Chris@16
|
224 if(x){
|
Chris@16
|
225 while(!is_header(x))
|
Chris@16
|
226 x = NodeTraits::get_parent(x);
|
Chris@16
|
227 erase(x, node);
|
Chris@16
|
228 }
|
Chris@16
|
229 }
|
Chris@16
|
230
|
Chris@16
|
231 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
Chris@16
|
232 //! @copydoc ::boost::intrusive::bstree_algorithms::unlink_leftmost_without_rebalance
|
Chris@16
|
233 static node_ptr unlink_leftmost_without_rebalance(const node_ptr & header);
|
Chris@16
|
234
|
Chris@16
|
235 //! @copydoc ::boost::intrusive::bstree_algorithms::unique(const const_node_ptr&)
|
Chris@16
|
236 static bool unique(const const_node_ptr & node);
|
Chris@16
|
237
|
Chris@16
|
238 //! @copydoc ::boost::intrusive::bstree_algorithms::size(const const_node_ptr&)
|
Chris@16
|
239 static std::size_t size(const const_node_ptr & header);
|
Chris@16
|
240
|
Chris@16
|
241 //! @copydoc ::boost::intrusive::bstree_algorithms::next_node(const node_ptr&)
|
Chris@16
|
242 static node_ptr next_node(const node_ptr & node);
|
Chris@16
|
243
|
Chris@16
|
244 //! @copydoc ::boost::intrusive::bstree_algorithms::prev_node(const node_ptr&)
|
Chris@16
|
245 static node_ptr prev_node(const node_ptr & node);
|
Chris@16
|
246
|
Chris@16
|
247 //! @copydoc ::boost::intrusive::bstree_algorithms::init(const node_ptr&)
|
Chris@16
|
248 static void init(const node_ptr & node);
|
Chris@16
|
249 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
Chris@16
|
250
|
Chris@16
|
251 //! <b>Requires</b>: node must not be part of any tree.
|
Chris@16
|
252 //!
|
Chris@16
|
253 //! <b>Effects</b>: Initializes the header to represent an empty tree.
|
Chris@16
|
254 //! unique(header) == true.
|
Chris@16
|
255 //!
|
Chris@16
|
256 //! <b>Complexity</b>: Constant.
|
Chris@16
|
257 //!
|
Chris@16
|
258 //! <b>Throws</b>: Nothing.
|
Chris@16
|
259 //!
|
Chris@16
|
260 //! <b>Nodes</b>: If node is inserted in a tree, this function corrupts the tree.
|
Chris@16
|
261 static void init_header(const node_ptr & header)
|
Chris@16
|
262 {
|
Chris@16
|
263 bstree_algo::init_header(header);
|
Chris@16
|
264 NodeTraits::set_balance(header, NodeTraits::zero());
|
Chris@16
|
265 }
|
Chris@16
|
266
|
Chris@16
|
267 //! @copydoc ::boost::intrusive::bstree_algorithms::erase(const node_ptr&,const node_ptr&)
|
Chris@16
|
268 static node_ptr erase(const node_ptr & header, const node_ptr & z)
|
Chris@16
|
269 {
|
Chris@16
|
270 typename bstree_algo::data_for_rebalance info;
|
Chris@101
|
271 bstree_algo::erase(header, z, info);
|
Chris@101
|
272 if(info.y != z){
|
Chris@101
|
273 NodeTraits::set_balance(info.y, NodeTraits::get_balance(z));
|
Chris@101
|
274 }
|
Chris@16
|
275 //Rebalance avltree
|
Chris@16
|
276 rebalance_after_erasure(header, info.x, info.x_parent);
|
Chris@16
|
277 return z;
|
Chris@16
|
278 }
|
Chris@16
|
279
|
Chris@16
|
280 //! @copydoc ::boost::intrusive::bstree_algorithms::clone(const const_node_ptr&,const node_ptr&,Cloner,Disposer)
|
Chris@16
|
281 template <class Cloner, class Disposer>
|
Chris@16
|
282 static void clone
|
Chris@16
|
283 (const const_node_ptr & source_header, const node_ptr & target_header, Cloner cloner, Disposer disposer)
|
Chris@16
|
284 {
|
Chris@16
|
285 avltree_node_cloner<NodeTraits, Cloner> new_cloner(cloner);
|
Chris@16
|
286 bstree_algo::clone(source_header, target_header, new_cloner, disposer);
|
Chris@16
|
287 }
|
Chris@16
|
288
|
Chris@16
|
289 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
Chris@16
|
290 //! @copydoc ::boost::intrusive::bstree_algorithms::clear_and_dispose(const node_ptr&,Disposer)
|
Chris@16
|
291 template<class Disposer>
|
Chris@16
|
292 static void clear_and_dispose(const node_ptr & header, Disposer disposer);
|
Chris@16
|
293
|
Chris@16
|
294 //! @copydoc ::boost::intrusive::bstree_algorithms::lower_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
|
Chris@16
|
295 template<class KeyType, class KeyNodePtrCompare>
|
Chris@16
|
296 static node_ptr lower_bound
|
Chris@16
|
297 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
|
Chris@16
|
298
|
Chris@16
|
299 //! @copydoc ::boost::intrusive::bstree_algorithms::upper_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
|
Chris@16
|
300 template<class KeyType, class KeyNodePtrCompare>
|
Chris@16
|
301 static node_ptr upper_bound
|
Chris@16
|
302 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
|
Chris@16
|
303
|
Chris@16
|
304 //! @copydoc ::boost::intrusive::bstree_algorithms::find(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
|
Chris@16
|
305 template<class KeyType, class KeyNodePtrCompare>
|
Chris@16
|
306 static node_ptr find
|
Chris@16
|
307 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
|
Chris@16
|
308
|
Chris@16
|
309 //! @copydoc ::boost::intrusive::bstree_algorithms::equal_range(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
|
Chris@16
|
310 template<class KeyType, class KeyNodePtrCompare>
|
Chris@16
|
311 static std::pair<node_ptr, node_ptr> equal_range
|
Chris@16
|
312 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
|
Chris@16
|
313
|
Chris@16
|
314 //! @copydoc ::boost::intrusive::bstree_algorithms::bounded_range(const const_node_ptr&,const KeyType&,const KeyType&,KeyNodePtrCompare,bool,bool)
|
Chris@16
|
315 template<class KeyType, class KeyNodePtrCompare>
|
Chris@16
|
316 static std::pair<node_ptr, node_ptr> bounded_range
|
Chris@16
|
317 (const const_node_ptr & header, const KeyType &lower_key, const KeyType &upper_key, KeyNodePtrCompare comp
|
Chris@16
|
318 , bool left_closed, bool right_closed);
|
Chris@16
|
319
|
Chris@16
|
320 //! @copydoc ::boost::intrusive::bstree_algorithms::count(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
|
Chris@16
|
321 template<class KeyType, class KeyNodePtrCompare>
|
Chris@16
|
322 static std::size_t count(const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
|
Chris@101
|
323
|
Chris@16
|
324 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
Chris@16
|
325
|
Chris@16
|
326 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_upper_bound(const node_ptr&,const node_ptr&,NodePtrCompare)
|
Chris@16
|
327 template<class NodePtrCompare>
|
Chris@16
|
328 static node_ptr insert_equal_upper_bound
|
Chris@16
|
329 (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp)
|
Chris@16
|
330 {
|
Chris@16
|
331 bstree_algo::insert_equal_upper_bound(h, new_node, comp);
|
Chris@16
|
332 rebalance_after_insertion(h, new_node);
|
Chris@16
|
333 return new_node;
|
Chris@16
|
334 }
|
Chris@16
|
335
|
Chris@16
|
336 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_lower_bound(const node_ptr&,const node_ptr&,NodePtrCompare)
|
Chris@16
|
337 template<class NodePtrCompare>
|
Chris@16
|
338 static node_ptr insert_equal_lower_bound
|
Chris@16
|
339 (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp)
|
Chris@16
|
340 {
|
Chris@16
|
341 bstree_algo::insert_equal_lower_bound(h, new_node, comp);
|
Chris@16
|
342 rebalance_after_insertion(h, new_node);
|
Chris@16
|
343 return new_node;
|
Chris@16
|
344 }
|
Chris@16
|
345
|
Chris@16
|
346 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal(const node_ptr&,const node_ptr&,const node_ptr&,NodePtrCompare)
|
Chris@16
|
347 template<class NodePtrCompare>
|
Chris@16
|
348 static node_ptr insert_equal
|
Chris@16
|
349 (const node_ptr & header, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp)
|
Chris@16
|
350 {
|
Chris@16
|
351 bstree_algo::insert_equal(header, hint, new_node, comp);
|
Chris@16
|
352 rebalance_after_insertion(header, new_node);
|
Chris@16
|
353 return new_node;
|
Chris@16
|
354 }
|
Chris@16
|
355
|
Chris@16
|
356 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_before(const node_ptr&,const node_ptr&,const node_ptr&)
|
Chris@16
|
357 static node_ptr insert_before
|
Chris@16
|
358 (const node_ptr & header, const node_ptr & pos, const node_ptr & new_node)
|
Chris@16
|
359 {
|
Chris@16
|
360 bstree_algo::insert_before(header, pos, new_node);
|
Chris@16
|
361 rebalance_after_insertion(header, new_node);
|
Chris@16
|
362 return new_node;
|
Chris@16
|
363 }
|
Chris@16
|
364
|
Chris@16
|
365 //! @copydoc ::boost::intrusive::bstree_algorithms::push_back(const node_ptr&,const node_ptr&)
|
Chris@16
|
366 static void push_back(const node_ptr & header, const node_ptr & new_node)
|
Chris@16
|
367 {
|
Chris@16
|
368 bstree_algo::push_back(header, new_node);
|
Chris@16
|
369 rebalance_after_insertion(header, new_node);
|
Chris@16
|
370 }
|
Chris@16
|
371
|
Chris@16
|
372 //! @copydoc ::boost::intrusive::bstree_algorithms::push_front(const node_ptr&,const node_ptr&)
|
Chris@16
|
373 static void push_front(const node_ptr & header, const node_ptr & new_node)
|
Chris@16
|
374 {
|
Chris@16
|
375 bstree_algo::push_front(header, new_node);
|
Chris@16
|
376 rebalance_after_insertion(header, new_node);
|
Chris@16
|
377 }
|
Chris@16
|
378
|
Chris@16
|
379 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
Chris@16
|
380 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const const_node_ptr&,const KeyType&,KeyNodePtrCompare,insert_commit_data&)
|
Chris@16
|
381 template<class KeyType, class KeyNodePtrCompare>
|
Chris@16
|
382 static std::pair<node_ptr, bool> insert_unique_check
|
Chris@16
|
383 (const const_node_ptr & header, const KeyType &key
|
Chris@16
|
384 ,KeyNodePtrCompare comp, insert_commit_data &commit_data);
|
Chris@16
|
385
|
Chris@16
|
386 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const const_node_ptr&,const node_ptr&,const KeyType&,KeyNodePtrCompare,insert_commit_data&)
|
Chris@16
|
387 template<class KeyType, class KeyNodePtrCompare>
|
Chris@16
|
388 static std::pair<node_ptr, bool> insert_unique_check
|
Chris@16
|
389 (const const_node_ptr & header, const node_ptr &hint, const KeyType &key
|
Chris@16
|
390 ,KeyNodePtrCompare comp, insert_commit_data &commit_data);
|
Chris@16
|
391 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
Chris@16
|
392
|
Chris@16
|
393 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_commit(const node_ptr&,const node_ptr&,const insert_commit_data &)
|
Chris@16
|
394 static void insert_unique_commit
|
Chris@16
|
395 (const node_ptr & header, const node_ptr & new_value, const insert_commit_data &commit_data)
|
Chris@16
|
396 {
|
Chris@16
|
397 bstree_algo::insert_unique_commit(header, new_value, commit_data);
|
Chris@16
|
398 rebalance_after_insertion(header, new_value);
|
Chris@16
|
399 }
|
Chris@16
|
400
|
Chris@16
|
401 //! @copydoc ::boost::intrusive::bstree_algorithms::is_header
|
Chris@16
|
402 static bool is_header(const const_node_ptr & p)
|
Chris@16
|
403 { return NodeTraits::get_balance(p) == NodeTraits::zero() && bstree_algo::is_header(p); }
|
Chris@16
|
404
|
Chris@16
|
405
|
Chris@16
|
406 /// @cond
|
Chris@101
|
407 static bool verify(const node_ptr &header)
|
Chris@101
|
408 {
|
Chris@101
|
409 std::size_t height;
|
Chris@101
|
410 std::size_t count;
|
Chris@101
|
411 return verify_recursion(NodeTraits::get_parent(header), count, height);
|
Chris@101
|
412 }
|
Chris@101
|
413
|
Chris@16
|
414 private:
|
Chris@16
|
415
|
Chris@101
|
416 static bool verify_recursion(node_ptr n, std::size_t &count, std::size_t &height)
|
Chris@16
|
417 {
|
Chris@101
|
418 if (!n){
|
Chris@101
|
419 count = 0;
|
Chris@101
|
420 height = 0;
|
Chris@101
|
421 return true;
|
Chris@101
|
422 }
|
Chris@101
|
423 std::size_t leftcount, rightcount;
|
Chris@101
|
424 std::size_t leftheight, rightheight;
|
Chris@101
|
425 if(!verify_recursion(NodeTraits::get_left (n), leftcount, leftheight) ||
|
Chris@101
|
426 !verify_recursion(NodeTraits::get_right(n), rightcount, rightheight) ){
|
Chris@101
|
427 return false;
|
Chris@101
|
428 }
|
Chris@101
|
429 count = 1u + leftcount + rightcount;
|
Chris@101
|
430 height = 1u + (leftheight > rightheight ? leftheight : rightheight);
|
Chris@101
|
431
|
Chris@101
|
432 //If equal height, balance must be zero
|
Chris@101
|
433 if(rightheight == leftheight){
|
Chris@101
|
434 if(NodeTraits::get_balance(n) != NodeTraits::zero()){
|
Chris@101
|
435 BOOST_ASSERT(0);
|
Chris@101
|
436 return false;
|
Chris@101
|
437 }
|
Chris@101
|
438 }
|
Chris@101
|
439 //If right is taller than left, then the difference must be at least 1 and the balance positive
|
Chris@101
|
440 else if(rightheight > leftheight){
|
Chris@101
|
441 if(rightheight - leftheight > 1 ){
|
Chris@101
|
442 BOOST_ASSERT(0);
|
Chris@101
|
443 return false;
|
Chris@101
|
444 }
|
Chris@101
|
445 else if(NodeTraits::get_balance(n) != NodeTraits::positive()){
|
Chris@101
|
446 BOOST_ASSERT(0);
|
Chris@101
|
447 return false;
|
Chris@101
|
448 }
|
Chris@101
|
449 }
|
Chris@101
|
450 //If left is taller than right, then the difference must be at least 1 and the balance negative
|
Chris@101
|
451 else{
|
Chris@101
|
452 if(leftheight - rightheight > 1 ){
|
Chris@101
|
453 BOOST_ASSERT(0);
|
Chris@101
|
454 return false;
|
Chris@101
|
455 }
|
Chris@101
|
456 else if(NodeTraits::get_balance(n) != NodeTraits::negative()){
|
Chris@101
|
457 BOOST_ASSERT(0);
|
Chris@101
|
458 return false;
|
Chris@101
|
459 }
|
Chris@101
|
460 }
|
Chris@101
|
461 return true;
|
Chris@101
|
462 }
|
Chris@101
|
463
|
Chris@101
|
464 static void rebalance_after_erasure(const node_ptr & header, node_ptr x, node_ptr x_parent)
|
Chris@101
|
465 {
|
Chris@101
|
466 for ( node_ptr root = NodeTraits::get_parent(header)
|
Chris@101
|
467 ; x != root
|
Chris@101
|
468 ; root = NodeTraits::get_parent(header), x_parent = NodeTraits::get_parent(x)) {
|
Chris@16
|
469 const balance x_parent_balance = NodeTraits::get_balance(x_parent);
|
Chris@101
|
470 //Don't cache x_is_leftchild or similar because x can be null and
|
Chris@101
|
471 //equal to both x_parent_left and x_parent_right
|
Chris@101
|
472 const node_ptr x_parent_left (NodeTraits::get_left(x_parent));
|
Chris@101
|
473 const node_ptr x_parent_right(NodeTraits::get_right(x_parent));
|
Chris@101
|
474
|
Chris@16
|
475 if(x_parent_balance == NodeTraits::zero()){
|
Chris@101
|
476 NodeTraits::set_balance( x_parent, x == x_parent_right ? NodeTraits::negative() : NodeTraits::positive() );
|
Chris@16
|
477 break; // the height didn't change, let's stop here
|
Chris@16
|
478 }
|
Chris@16
|
479 else if(x_parent_balance == NodeTraits::negative()){
|
Chris@101
|
480 if (x == x_parent_left) { ////x is left child or x and sibling are null
|
Chris@16
|
481 NodeTraits::set_balance(x_parent, NodeTraits::zero()); // balanced
|
Chris@16
|
482 x = x_parent;
|
Chris@16
|
483 }
|
Chris@16
|
484 else {
|
Chris@101
|
485 // x is right child (x_parent_left is the left child)
|
Chris@101
|
486 BOOST_INTRUSIVE_INVARIANT_ASSERT(x_parent_left);
|
Chris@101
|
487 if (NodeTraits::get_balance(x_parent_left) == NodeTraits::positive()) {
|
Chris@101
|
488 // x_parent_left MUST have a right child
|
Chris@101
|
489 BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_right(x_parent_left));
|
Chris@101
|
490 x = avl_rotate_left_right(x_parent, x_parent_left, header);
|
Chris@16
|
491 }
|
Chris@16
|
492 else {
|
Chris@101
|
493 avl_rotate_right(x_parent, x_parent_left, header);
|
Chris@101
|
494 x = x_parent_left;
|
Chris@16
|
495 }
|
Chris@16
|
496
|
Chris@16
|
497 // if changed from negative to NodeTraits::positive(), no need to check above
|
Chris@16
|
498 if (NodeTraits::get_balance(x) == NodeTraits::positive()){
|
Chris@16
|
499 break;
|
Chris@16
|
500 }
|
Chris@16
|
501 }
|
Chris@16
|
502 }
|
Chris@16
|
503 else if(x_parent_balance == NodeTraits::positive()){
|
Chris@101
|
504 if (x == x_parent_right) { //x is right child or x and sibling are null
|
Chris@16
|
505 NodeTraits::set_balance(x_parent, NodeTraits::zero()); // balanced
|
Chris@16
|
506 x = x_parent;
|
Chris@16
|
507 }
|
Chris@16
|
508 else {
|
Chris@101
|
509 // x is left child (x_parent_right is the right child)
|
Chris@101
|
510 BOOST_INTRUSIVE_INVARIANT_ASSERT(x_parent_right);
|
Chris@101
|
511 if (NodeTraits::get_balance(x_parent_right) == NodeTraits::negative()) {
|
Chris@101
|
512 // x_parent_right MUST have then a left child
|
Chris@101
|
513 BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_left(x_parent_right));
|
Chris@101
|
514 x = avl_rotate_right_left(x_parent, x_parent_right, header);
|
Chris@16
|
515 }
|
Chris@16
|
516 else {
|
Chris@101
|
517 avl_rotate_left(x_parent, x_parent_right, header);
|
Chris@101
|
518 x = x_parent_right;
|
Chris@16
|
519 }
|
Chris@16
|
520 // if changed from NodeTraits::positive() to negative, no need to check above
|
Chris@16
|
521 if (NodeTraits::get_balance(x) == NodeTraits::negative()){
|
Chris@16
|
522 break;
|
Chris@16
|
523 }
|
Chris@16
|
524 }
|
Chris@16
|
525 }
|
Chris@16
|
526 else{
|
Chris@16
|
527 BOOST_INTRUSIVE_INVARIANT_ASSERT(false); // never reached
|
Chris@16
|
528 }
|
Chris@16
|
529 }
|
Chris@16
|
530 }
|
Chris@16
|
531
|
Chris@101
|
532 static void rebalance_after_insertion(const node_ptr & header, node_ptr x)
|
Chris@16
|
533 {
|
Chris@16
|
534 NodeTraits::set_balance(x, NodeTraits::zero());
|
Chris@16
|
535 // Rebalance.
|
Chris@16
|
536 for(node_ptr root = NodeTraits::get_parent(header); x != root; root = NodeTraits::get_parent(header)){
|
Chris@101
|
537 node_ptr const x_parent(NodeTraits::get_parent(x));
|
Chris@101
|
538 node_ptr const x_parent_left(NodeTraits::get_left(x_parent));
|
Chris@101
|
539 const balance x_parent_balance = NodeTraits::get_balance(x_parent);
|
Chris@101
|
540 const bool x_is_leftchild(x == x_parent_left);
|
Chris@16
|
541 if(x_parent_balance == NodeTraits::zero()){
|
Chris@16
|
542 // if x is left, parent will have parent->bal_factor = negative
|
Chris@16
|
543 // else, parent->bal_factor = NodeTraits::positive()
|
Chris@101
|
544 NodeTraits::set_balance( x_parent, x_is_leftchild ? NodeTraits::negative() : NodeTraits::positive() );
|
Chris@101
|
545 x = x_parent;
|
Chris@16
|
546 }
|
Chris@16
|
547 else if(x_parent_balance == NodeTraits::positive()){
|
Chris@16
|
548 // if x is a left child, parent->bal_factor = zero
|
Chris@101
|
549 if (x_is_leftchild)
|
Chris@101
|
550 NodeTraits::set_balance(x_parent, NodeTraits::zero());
|
Chris@16
|
551 else{ // x is a right child, needs rebalancing
|
Chris@16
|
552 if (NodeTraits::get_balance(x) == NodeTraits::negative())
|
Chris@101
|
553 avl_rotate_right_left(x_parent, x, header);
|
Chris@16
|
554 else
|
Chris@101
|
555 avl_rotate_left(x_parent, x, header);
|
Chris@16
|
556 }
|
Chris@16
|
557 break;
|
Chris@16
|
558 }
|
Chris@16
|
559 else if(x_parent_balance == NodeTraits::negative()){
|
Chris@16
|
560 // if x is a left child, needs rebalancing
|
Chris@101
|
561 if (x_is_leftchild) {
|
Chris@16
|
562 if (NodeTraits::get_balance(x) == NodeTraits::positive())
|
Chris@101
|
563 avl_rotate_left_right(x_parent, x, header);
|
Chris@16
|
564 else
|
Chris@101
|
565 avl_rotate_right(x_parent, x, header);
|
Chris@16
|
566 }
|
Chris@16
|
567 else
|
Chris@101
|
568 NodeTraits::set_balance(x_parent, NodeTraits::zero());
|
Chris@16
|
569 break;
|
Chris@16
|
570 }
|
Chris@16
|
571 else{
|
Chris@16
|
572 BOOST_INTRUSIVE_INVARIANT_ASSERT(false); // never reached
|
Chris@16
|
573 }
|
Chris@16
|
574 }
|
Chris@16
|
575 }
|
Chris@16
|
576
|
Chris@16
|
577 static void left_right_balancing(const node_ptr & a, const node_ptr & b, const node_ptr & c)
|
Chris@16
|
578 {
|
Chris@16
|
579 // balancing...
|
Chris@16
|
580 const balance c_balance = NodeTraits::get_balance(c);
|
Chris@16
|
581 const balance zero_balance = NodeTraits::zero();
|
Chris@101
|
582 const balance posi_balance = NodeTraits::positive();
|
Chris@101
|
583 const balance nega_balance = NodeTraits::negative();
|
Chris@16
|
584 NodeTraits::set_balance(c, zero_balance);
|
Chris@101
|
585 if(c_balance == nega_balance){
|
Chris@101
|
586 NodeTraits::set_balance(a, posi_balance);
|
Chris@16
|
587 NodeTraits::set_balance(b, zero_balance);
|
Chris@16
|
588 }
|
Chris@16
|
589 else if(c_balance == zero_balance){
|
Chris@16
|
590 NodeTraits::set_balance(a, zero_balance);
|
Chris@16
|
591 NodeTraits::set_balance(b, zero_balance);
|
Chris@16
|
592 }
|
Chris@101
|
593 else if(c_balance == posi_balance){
|
Chris@16
|
594 NodeTraits::set_balance(a, zero_balance);
|
Chris@101
|
595 NodeTraits::set_balance(b, nega_balance);
|
Chris@16
|
596 }
|
Chris@16
|
597 else{
|
Chris@16
|
598 BOOST_INTRUSIVE_INVARIANT_ASSERT(false); // never reached
|
Chris@16
|
599 }
|
Chris@16
|
600 }
|
Chris@16
|
601
|
Chris@101
|
602 static node_ptr avl_rotate_left_right(const node_ptr a, const node_ptr a_oldleft, const node_ptr & hdr)
|
Chris@101
|
603 { // [note: 'a_oldleft' is 'b']
|
Chris@16
|
604 // | | //
|
Chris@16
|
605 // a(-2) c //
|
Chris@16
|
606 // / \ / \ //
|
Chris@16
|
607 // / \ ==> / \ //
|
Chris@16
|
608 // (pos)b [g] b a //
|
Chris@16
|
609 // / \ / \ / \ //
|
Chris@16
|
610 // [d] c [d] e f [g] //
|
Chris@101
|
611 // / \ //
|
Chris@101
|
612 // e f //
|
Chris@101
|
613 const node_ptr c = NodeTraits::get_right(a_oldleft);
|
Chris@101
|
614 bstree_algo::rotate_left_no_parent_fix(a_oldleft, c);
|
Chris@101
|
615 //No need to link c with a [NodeTraits::set_parent(c, a) + NodeTraits::set_left(a, c)]
|
Chris@101
|
616 //as c is not root and another rotation is coming
|
Chris@101
|
617 bstree_algo::rotate_right(a, c, NodeTraits::get_parent(a), hdr);
|
Chris@101
|
618 left_right_balancing(a, a_oldleft, c);
|
Chris@101
|
619 return c;
|
Chris@16
|
620 }
|
Chris@16
|
621
|
Chris@101
|
622 static node_ptr avl_rotate_right_left(const node_ptr a, const node_ptr a_oldright, const node_ptr & hdr)
|
Chris@101
|
623 { // [note: 'a_oldright' is 'b']
|
Chris@16
|
624 // | | //
|
Chris@16
|
625 // a(pos) c //
|
Chris@16
|
626 // / \ / \ //
|
Chris@16
|
627 // / \ / \ //
|
Chris@16
|
628 // [d] b(neg) ==> a b //
|
Chris@16
|
629 // / \ / \ / \ //
|
Chris@16
|
630 // c [g] [d] e f [g] //
|
Chris@16
|
631 // / \ //
|
Chris@16
|
632 // e f //
|
Chris@101
|
633 const node_ptr c (NodeTraits::get_left(a_oldright));
|
Chris@101
|
634 bstree_algo::rotate_right_no_parent_fix(a_oldright, c);
|
Chris@101
|
635 //No need to link c with a [NodeTraits::set_parent(c, a) + NodeTraits::set_right(a, c)]
|
Chris@101
|
636 //as c is not root and another rotation is coming.
|
Chris@101
|
637 bstree_algo::rotate_left(a, c, NodeTraits::get_parent(a), hdr);
|
Chris@101
|
638 left_right_balancing(a_oldright, a, c);
|
Chris@101
|
639 return c;
|
Chris@16
|
640 }
|
Chris@16
|
641
|
Chris@101
|
642 static void avl_rotate_left(const node_ptr &x, const node_ptr &x_oldright, const node_ptr & hdr)
|
Chris@16
|
643 {
|
Chris@101
|
644 bstree_algo::rotate_left(x, x_oldright, NodeTraits::get_parent(x), hdr);
|
Chris@16
|
645
|
Chris@16
|
646 // reset the balancing factor
|
Chris@101
|
647 if (NodeTraits::get_balance(x_oldright) == NodeTraits::positive()) {
|
Chris@16
|
648 NodeTraits::set_balance(x, NodeTraits::zero());
|
Chris@101
|
649 NodeTraits::set_balance(x_oldright, NodeTraits::zero());
|
Chris@16
|
650 }
|
Chris@16
|
651 else { // this doesn't happen during insertions
|
Chris@16
|
652 NodeTraits::set_balance(x, NodeTraits::positive());
|
Chris@101
|
653 NodeTraits::set_balance(x_oldright, NodeTraits::negative());
|
Chris@16
|
654 }
|
Chris@16
|
655 }
|
Chris@16
|
656
|
Chris@101
|
657 static void avl_rotate_right(const node_ptr &x, const node_ptr &x_oldleft, const node_ptr & hdr)
|
Chris@16
|
658 {
|
Chris@101
|
659 bstree_algo::rotate_right(x, x_oldleft, NodeTraits::get_parent(x), hdr);
|
Chris@16
|
660
|
Chris@16
|
661 // reset the balancing factor
|
Chris@101
|
662 if (NodeTraits::get_balance(x_oldleft) == NodeTraits::negative()) {
|
Chris@16
|
663 NodeTraits::set_balance(x, NodeTraits::zero());
|
Chris@101
|
664 NodeTraits::set_balance(x_oldleft, NodeTraits::zero());
|
Chris@16
|
665 }
|
Chris@16
|
666 else { // this doesn't happen during insertions
|
Chris@16
|
667 NodeTraits::set_balance(x, NodeTraits::negative());
|
Chris@101
|
668 NodeTraits::set_balance(x_oldleft, NodeTraits::positive());
|
Chris@16
|
669 }
|
Chris@16
|
670 }
|
Chris@16
|
671
|
Chris@16
|
672 /// @endcond
|
Chris@16
|
673 };
|
Chris@16
|
674
|
Chris@16
|
675 /// @cond
|
Chris@16
|
676
|
Chris@16
|
677 template<class NodeTraits>
|
Chris@16
|
678 struct get_algo<AvlTreeAlgorithms, NodeTraits>
|
Chris@16
|
679 {
|
Chris@16
|
680 typedef avltree_algorithms<NodeTraits> type;
|
Chris@16
|
681 };
|
Chris@16
|
682
|
Chris@101
|
683 template <class ValueTraits, class NodePtrCompare, class ExtraChecker>
|
Chris@101
|
684 struct get_node_checker<AvlTreeAlgorithms, ValueTraits, NodePtrCompare, ExtraChecker>
|
Chris@101
|
685 {
|
Chris@101
|
686 typedef detail::avltree_node_checker<ValueTraits, NodePtrCompare, ExtraChecker> type;
|
Chris@101
|
687 };
|
Chris@101
|
688
|
Chris@16
|
689 /// @endcond
|
Chris@16
|
690
|
Chris@16
|
691 } //namespace intrusive
|
Chris@16
|
692 } //namespace boost
|
Chris@16
|
693
|
Chris@16
|
694 #include <boost/intrusive/detail/config_end.hpp>
|
Chris@16
|
695
|
Chris@16
|
696 #endif //BOOST_INTRUSIVE_AVLTREE_ALGORITHMS_HPP
|