1 /////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Daniel K. O. 2005.
4 // (C) Copyright Ion Gaztanaga 2007-2014
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
10 // See http://www.boost.org/libs/intrusive for documentation.
12 /////////////////////////////////////////////////////////////////////////////
14 #ifndef BOOST_INTRUSIVE_AVLTREE_ALGORITHMS_HPP
15 #define BOOST_INTRUSIVE_AVLTREE_ALGORITHMS_HPP
17 #include <boost/intrusive/detail/config_begin.hpp>
18 #include <boost/intrusive/intrusive_fwd.hpp>
22 #include <boost/intrusive/detail/assert.hpp>
23 #include <boost/intrusive/detail/algo_type.hpp>
24 #include <boost/intrusive/detail/ebo_functor_holder.hpp>
25 #include <boost/intrusive/bstree_algorithms.hpp>
27 #if defined(BOOST_HAS_PRAGMA_ONCE)
37 template<class NodeTraits, class F>
38 struct avltree_node_cloner
39 //Use public inheritance to avoid MSVC bugs with closures
40 : public detail::ebo_functor_holder<F>
42 typedef typename NodeTraits::node_ptr node_ptr;
43 typedef detail::ebo_functor_holder<F> base_t;
45 avltree_node_cloner(F f)
49 node_ptr operator()(const node_ptr & p)
51 node_ptr n = base_t::get()(p);
52 NodeTraits::set_balance(n, NodeTraits::get_balance(p));
56 node_ptr operator()(const node_ptr & p) const
58 node_ptr n = base_t::get()(p);
59 NodeTraits::set_balance(n, NodeTraits::get_balance(p));
66 template<class ValueTraits, class NodePtrCompare, class ExtraChecker>
67 struct avltree_node_checker
68 : public bstree_node_checker<ValueTraits, NodePtrCompare, ExtraChecker>
70 typedef bstree_node_checker<ValueTraits, NodePtrCompare, ExtraChecker> base_checker_t;
71 typedef ValueTraits value_traits;
72 typedef typename value_traits::node_traits node_traits;
73 typedef typename node_traits::const_node_ptr const_node_ptr;
76 : public base_checker_t::return_type
78 return_type() : height(0) {}
82 avltree_node_checker(const NodePtrCompare& comp, ExtraChecker extra_checker)
83 : base_checker_t(comp, extra_checker)
86 void operator () (const const_node_ptr& p,
87 const return_type& check_return_left, const return_type& check_return_right,
88 return_type& check_return)
90 const int height_diff = check_return_right.height - check_return_left.height; (void)height_diff;
91 BOOST_INTRUSIVE_INVARIANT_ASSERT(
92 (height_diff == -1 && node_traits::get_balance(p) == node_traits::negative()) ||
93 (height_diff == 0 && node_traits::get_balance(p) == node_traits::zero()) ||
94 (height_diff == 1 && node_traits::get_balance(p) == node_traits::positive())
96 check_return.height = 1 +
97 (check_return_left.height > check_return_right.height ? check_return_left.height : check_return_right.height);
98 base_checker_t::operator()(p, check_return_left, check_return_right, check_return);
102 } // namespace detail
106 //! avltree_algorithms is configured with a NodeTraits class, which encapsulates the
107 //! information about the node to be manipulated. NodeTraits must support the
108 //! following interface:
112 //! <tt>node</tt>: The type of the node that forms the binary search tree
114 //! <tt>node_ptr</tt>: A pointer to a node
116 //! <tt>const_node_ptr</tt>: A pointer to a const node
118 //! <tt>balance</tt>: The type of the balance factor
120 //! <b>Static functions</b>:
122 //! <tt>static node_ptr get_parent(const_node_ptr n);</tt>
124 //! <tt>static void set_parent(node_ptr n, node_ptr parent);</tt>
126 //! <tt>static node_ptr get_left(const_node_ptr n);</tt>
128 //! <tt>static void set_left(node_ptr n, node_ptr left);</tt>
130 //! <tt>static node_ptr get_right(const_node_ptr n);</tt>
132 //! <tt>static void set_right(node_ptr n, node_ptr right);</tt>
134 //! <tt>static balance get_balance(const_node_ptr n);</tt>
136 //! <tt>static void set_balance(node_ptr n, balance b);</tt>
138 //! <tt>static balance negative();</tt>
140 //! <tt>static balance zero();</tt>
142 //! <tt>static balance positive();</tt>
143 template<class NodeTraits>
144 class avltree_algorithms
145 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
146 : public bstree_algorithms<NodeTraits>
150 typedef typename NodeTraits::node node;
151 typedef NodeTraits node_traits;
152 typedef typename NodeTraits::node_ptr node_ptr;
153 typedef typename NodeTraits::const_node_ptr const_node_ptr;
154 typedef typename NodeTraits::balance balance;
158 typedef bstree_algorithms<NodeTraits> bstree_algo;
163 //! This type is the information that will be
164 //! filled by insert_unique_check
165 typedef typename bstree_algo::insert_commit_data insert_commit_data;
167 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
169 //! @copydoc ::boost::intrusive::bstree_algorithms::get_header(const const_node_ptr&)
170 static node_ptr get_header(const const_node_ptr & n);
172 //! @copydoc ::boost::intrusive::bstree_algorithms::begin_node
173 static node_ptr begin_node(const const_node_ptr & header);
175 //! @copydoc ::boost::intrusive::bstree_algorithms::end_node
176 static node_ptr end_node(const const_node_ptr & header);
178 //! @copydoc ::boost::intrusive::bstree_algorithms::swap_tree
179 static void swap_tree(const node_ptr & header1, const node_ptr & header2);
181 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
183 //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(const node_ptr&,const node_ptr&)
184 static void swap_nodes(const node_ptr & node1, const node_ptr & node2)
189 node_ptr header1(bstree_algo::get_header(node1)), header2(bstree_algo::get_header(node2));
190 swap_nodes(node1, header1, node2, header2);
193 //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(const node_ptr&,const node_ptr&,const node_ptr&,const node_ptr&)
194 static void swap_nodes(const node_ptr & node1, const node_ptr & header1, const node_ptr & node2, const node_ptr & header2)
196 if(node1 == node2) return;
198 bstree_algo::swap_nodes(node1, header1, node2, header2);
200 balance c = NodeTraits::get_balance(node1);
201 NodeTraits::set_balance(node1, NodeTraits::get_balance(node2));
202 NodeTraits::set_balance(node2, c);
205 //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(const node_ptr&,const node_ptr&)
206 static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & new_node)
208 if(node_to_be_replaced == new_node)
210 replace_node(node_to_be_replaced, bstree_algo::get_header(node_to_be_replaced), new_node);
213 //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(const node_ptr&,const node_ptr&,const node_ptr&)
214 static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & header, const node_ptr & new_node)
216 bstree_algo::replace_node(node_to_be_replaced, header, new_node);
217 NodeTraits::set_balance(new_node, NodeTraits::get_balance(node_to_be_replaced));
220 //! @copydoc ::boost::intrusive::bstree_algorithms::unlink(const node_ptr&)
221 static void unlink(const node_ptr & node)
223 node_ptr x = NodeTraits::get_parent(node);
226 x = NodeTraits::get_parent(x);
231 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
232 //! @copydoc ::boost::intrusive::bstree_algorithms::unlink_leftmost_without_rebalance
233 static node_ptr unlink_leftmost_without_rebalance(const node_ptr & header);
235 //! @copydoc ::boost::intrusive::bstree_algorithms::unique(const const_node_ptr&)
236 static bool unique(const const_node_ptr & node);
238 //! @copydoc ::boost::intrusive::bstree_algorithms::size(const const_node_ptr&)
239 static std::size_t size(const const_node_ptr & header);
241 //! @copydoc ::boost::intrusive::bstree_algorithms::next_node(const node_ptr&)
242 static node_ptr next_node(const node_ptr & node);
244 //! @copydoc ::boost::intrusive::bstree_algorithms::prev_node(const node_ptr&)
245 static node_ptr prev_node(const node_ptr & node);
247 //! @copydoc ::boost::intrusive::bstree_algorithms::init(const node_ptr&)
248 static void init(const node_ptr & node);
249 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
251 //! <b>Requires</b>: node must not be part of any tree.
253 //! <b>Effects</b>: Initializes the header to represent an empty tree.
254 //! unique(header) == true.
256 //! <b>Complexity</b>: Constant.
258 //! <b>Throws</b>: Nothing.
260 //! <b>Nodes</b>: If node is inserted in a tree, this function corrupts the tree.
261 static void init_header(const node_ptr & header)
263 bstree_algo::init_header(header);
264 NodeTraits::set_balance(header, NodeTraits::zero());
267 //! @copydoc ::boost::intrusive::bstree_algorithms::erase(const node_ptr&,const node_ptr&)
268 static node_ptr erase(const node_ptr & header, const node_ptr & z)
270 typename bstree_algo::data_for_rebalance info;
271 bstree_algo::erase(header, z, info);
272 rebalance_after_erasure(header, z, info);
276 //! @copydoc ::boost::intrusive::bstree_algorithms::transfer_unique
277 template<class NodePtrCompare>
278 static bool transfer_unique
279 (const node_ptr & header1, NodePtrCompare comp, const node_ptr &header2, const node_ptr & z)
281 typename bstree_algo::data_for_rebalance info;
282 bool const transferred = bstree_algo::transfer_unique(header1, comp, header2, z, info);
284 rebalance_after_erasure(header2, z, info);
285 rebalance_after_insertion(header1, z);
290 //! @copydoc ::boost::intrusive::bstree_algorithms::transfer_equal
291 template<class NodePtrCompare>
292 static void transfer_equal
293 (const node_ptr & header1, NodePtrCompare comp, const node_ptr &header2, const node_ptr & z)
295 typename bstree_algo::data_for_rebalance info;
296 bstree_algo::transfer_equal(header1, comp, header2, z, info);
297 rebalance_after_erasure(header2, z, info);
298 rebalance_after_insertion(header1, z);
301 //! @copydoc ::boost::intrusive::bstree_algorithms::clone(const const_node_ptr&,const node_ptr&,Cloner,Disposer)
302 template <class Cloner, class Disposer>
304 (const const_node_ptr & source_header, const node_ptr & target_header, Cloner cloner, Disposer disposer)
306 avltree_node_cloner<NodeTraits, Cloner> new_cloner(cloner);
307 bstree_algo::clone(source_header, target_header, new_cloner, disposer);
310 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
311 //! @copydoc ::boost::intrusive::bstree_algorithms::clear_and_dispose(const node_ptr&,Disposer)
312 template<class Disposer>
313 static void clear_and_dispose(const node_ptr & header, Disposer disposer);
315 //! @copydoc ::boost::intrusive::bstree_algorithms::lower_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
316 template<class KeyType, class KeyNodePtrCompare>
317 static node_ptr lower_bound
318 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
320 //! @copydoc ::boost::intrusive::bstree_algorithms::upper_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
321 template<class KeyType, class KeyNodePtrCompare>
322 static node_ptr upper_bound
323 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
325 //! @copydoc ::boost::intrusive::bstree_algorithms::find(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
326 template<class KeyType, class KeyNodePtrCompare>
328 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
330 //! @copydoc ::boost::intrusive::bstree_algorithms::equal_range(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
331 template<class KeyType, class KeyNodePtrCompare>
332 static std::pair<node_ptr, node_ptr> equal_range
333 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
335 //! @copydoc ::boost::intrusive::bstree_algorithms::bounded_range(const const_node_ptr&,const KeyType&,const KeyType&,KeyNodePtrCompare,bool,bool)
336 template<class KeyType, class KeyNodePtrCompare>
337 static std::pair<node_ptr, node_ptr> bounded_range
338 (const const_node_ptr & header, const KeyType &lower_key, const KeyType &upper_key, KeyNodePtrCompare comp
339 , bool left_closed, bool right_closed);
341 //! @copydoc ::boost::intrusive::bstree_algorithms::count(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
342 template<class KeyType, class KeyNodePtrCompare>
343 static std::size_t count(const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
345 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
347 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_upper_bound(const node_ptr&,const node_ptr&,NodePtrCompare)
348 template<class NodePtrCompare>
349 static node_ptr insert_equal_upper_bound
350 (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp)
352 bstree_algo::insert_equal_upper_bound(h, new_node, comp);
353 rebalance_after_insertion(h, new_node);
357 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_lower_bound(const node_ptr&,const node_ptr&,NodePtrCompare)
358 template<class NodePtrCompare>
359 static node_ptr insert_equal_lower_bound
360 (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp)
362 bstree_algo::insert_equal_lower_bound(h, new_node, comp);
363 rebalance_after_insertion(h, new_node);
367 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal(const node_ptr&,const node_ptr&,const node_ptr&,NodePtrCompare)
368 template<class NodePtrCompare>
369 static node_ptr insert_equal
370 (const node_ptr & header, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp)
372 bstree_algo::insert_equal(header, hint, new_node, comp);
373 rebalance_after_insertion(header, new_node);
377 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_before(const node_ptr&,const node_ptr&,const node_ptr&)
378 static node_ptr insert_before
379 (const node_ptr & header, const node_ptr & pos, const node_ptr & new_node)
381 bstree_algo::insert_before(header, pos, new_node);
382 rebalance_after_insertion(header, new_node);
386 //! @copydoc ::boost::intrusive::bstree_algorithms::push_back(const node_ptr&,const node_ptr&)
387 static void push_back(const node_ptr & header, const node_ptr & new_node)
389 bstree_algo::push_back(header, new_node);
390 rebalance_after_insertion(header, new_node);
393 //! @copydoc ::boost::intrusive::bstree_algorithms::push_front(const node_ptr&,const node_ptr&)
394 static void push_front(const node_ptr & header, const node_ptr & new_node)
396 bstree_algo::push_front(header, new_node);
397 rebalance_after_insertion(header, new_node);
400 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
401 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const const_node_ptr&,const KeyType&,KeyNodePtrCompare,insert_commit_data&)
402 template<class KeyType, class KeyNodePtrCompare>
403 static std::pair<node_ptr, bool> insert_unique_check
404 (const const_node_ptr & header, const KeyType &key
405 ,KeyNodePtrCompare comp, insert_commit_data &commit_data);
407 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const const_node_ptr&,const node_ptr&,const KeyType&,KeyNodePtrCompare,insert_commit_data&)
408 template<class KeyType, class KeyNodePtrCompare>
409 static std::pair<node_ptr, bool> insert_unique_check
410 (const const_node_ptr & header, const node_ptr &hint, const KeyType &key
411 ,KeyNodePtrCompare comp, insert_commit_data &commit_data);
412 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
414 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_commit(const node_ptr&,const node_ptr&,const insert_commit_data &)
415 static void insert_unique_commit
416 (const node_ptr & header, const node_ptr & new_value, const insert_commit_data &commit_data)
418 bstree_algo::insert_unique_commit(header, new_value, commit_data);
419 rebalance_after_insertion(header, new_value);
422 //! @copydoc ::boost::intrusive::bstree_algorithms::is_header
423 static bool is_header(const const_node_ptr & p)
424 { return NodeTraits::get_balance(p) == NodeTraits::zero() && bstree_algo::is_header(p); }
428 static bool verify(const node_ptr &header)
432 return verify_recursion(NodeTraits::get_parent(header), count, height);
437 static bool verify_recursion(node_ptr n, std::size_t &count, std::size_t &height)
444 std::size_t leftcount, rightcount;
445 std::size_t leftheight, rightheight;
446 if(!verify_recursion(NodeTraits::get_left (n), leftcount, leftheight) ||
447 !verify_recursion(NodeTraits::get_right(n), rightcount, rightheight) ){
450 count = 1u + leftcount + rightcount;
451 height = 1u + (leftheight > rightheight ? leftheight : rightheight);
453 //If equal height, balance must be zero
454 if(rightheight == leftheight){
455 if(NodeTraits::get_balance(n) != NodeTraits::zero()){
460 //If right is taller than left, then the difference must be at least 1 and the balance positive
461 else if(rightheight > leftheight){
462 if(rightheight - leftheight > 1 ){
466 else if(NodeTraits::get_balance(n) != NodeTraits::positive()){
471 //If left is taller than right, then the difference must be at least 1 and the balance negative
473 if(leftheight - rightheight > 1 ){
477 else if(NodeTraits::get_balance(n) != NodeTraits::negative()){
485 static void rebalance_after_erasure
486 ( const node_ptr & header, const node_ptr &z, const typename bstree_algo::data_for_rebalance &info)
489 NodeTraits::set_balance(info.y, NodeTraits::get_balance(z));
492 rebalance_after_erasure_restore_invariants(header, info.x, info.x_parent);
495 static void rebalance_after_erasure_restore_invariants(const node_ptr & header, node_ptr x, node_ptr x_parent)
497 for ( node_ptr root = NodeTraits::get_parent(header)
499 ; root = NodeTraits::get_parent(header), x_parent = NodeTraits::get_parent(x)) {
500 const balance x_parent_balance = NodeTraits::get_balance(x_parent);
501 //Don't cache x_is_leftchild or similar because x can be null and
502 //equal to both x_parent_left and x_parent_right
503 const node_ptr x_parent_left (NodeTraits::get_left(x_parent));
504 const node_ptr x_parent_right(NodeTraits::get_right(x_parent));
506 if(x_parent_balance == NodeTraits::zero()){
507 NodeTraits::set_balance( x_parent, x == x_parent_right ? NodeTraits::negative() : NodeTraits::positive() );
508 break; // the height didn't change, let's stop here
510 else if(x_parent_balance == NodeTraits::negative()){
511 if (x == x_parent_left) { ////x is left child or x and sibling are null
512 NodeTraits::set_balance(x_parent, NodeTraits::zero()); // balanced
516 // x is right child (x_parent_left is the left child)
517 BOOST_INTRUSIVE_INVARIANT_ASSERT(x_parent_left);
518 if (NodeTraits::get_balance(x_parent_left) == NodeTraits::positive()) {
519 // x_parent_left MUST have a right child
520 BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_right(x_parent_left));
521 x = avl_rotate_left_right(x_parent, x_parent_left, header);
524 avl_rotate_right(x_parent, x_parent_left, header);
528 // if changed from negative to NodeTraits::positive(), no need to check above
529 if (NodeTraits::get_balance(x) == NodeTraits::positive()){
534 else if(x_parent_balance == NodeTraits::positive()){
535 if (x == x_parent_right) { //x is right child or x and sibling are null
536 NodeTraits::set_balance(x_parent, NodeTraits::zero()); // balanced
540 // x is left child (x_parent_right is the right child)
541 BOOST_INTRUSIVE_INVARIANT_ASSERT(x_parent_right);
542 if (NodeTraits::get_balance(x_parent_right) == NodeTraits::negative()) {
543 // x_parent_right MUST have then a left child
544 BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_left(x_parent_right));
545 x = avl_rotate_right_left(x_parent, x_parent_right, header);
548 avl_rotate_left(x_parent, x_parent_right, header);
551 // if changed from NodeTraits::positive() to negative, no need to check above
552 if (NodeTraits::get_balance(x) == NodeTraits::negative()){
558 BOOST_INTRUSIVE_INVARIANT_ASSERT(false); // never reached
563 static void rebalance_after_insertion(const node_ptr & header, node_ptr x)
565 NodeTraits::set_balance(x, NodeTraits::zero());
567 for(node_ptr root = NodeTraits::get_parent(header); x != root; root = NodeTraits::get_parent(header)){
568 node_ptr const x_parent(NodeTraits::get_parent(x));
569 node_ptr const x_parent_left(NodeTraits::get_left(x_parent));
570 const balance x_parent_balance = NodeTraits::get_balance(x_parent);
571 const bool x_is_leftchild(x == x_parent_left);
572 if(x_parent_balance == NodeTraits::zero()){
573 // if x is left, parent will have parent->bal_factor = negative
574 // else, parent->bal_factor = NodeTraits::positive()
575 NodeTraits::set_balance( x_parent, x_is_leftchild ? NodeTraits::negative() : NodeTraits::positive() );
578 else if(x_parent_balance == NodeTraits::positive()){
579 // if x is a left child, parent->bal_factor = zero
581 NodeTraits::set_balance(x_parent, NodeTraits::zero());
582 else{ // x is a right child, needs rebalancing
583 if (NodeTraits::get_balance(x) == NodeTraits::negative())
584 avl_rotate_right_left(x_parent, x, header);
586 avl_rotate_left(x_parent, x, header);
590 else if(x_parent_balance == NodeTraits::negative()){
591 // if x is a left child, needs rebalancing
592 if (x_is_leftchild) {
593 if (NodeTraits::get_balance(x) == NodeTraits::positive())
594 avl_rotate_left_right(x_parent, x, header);
596 avl_rotate_right(x_parent, x, header);
599 NodeTraits::set_balance(x_parent, NodeTraits::zero());
603 BOOST_INTRUSIVE_INVARIANT_ASSERT(false); // never reached
608 static void left_right_balancing(const node_ptr & a, const node_ptr & b, const node_ptr & c)
611 const balance c_balance = NodeTraits::get_balance(c);
612 const balance zero_balance = NodeTraits::zero();
613 const balance posi_balance = NodeTraits::positive();
614 const balance nega_balance = NodeTraits::negative();
615 NodeTraits::set_balance(c, zero_balance);
616 if(c_balance == nega_balance){
617 NodeTraits::set_balance(a, posi_balance);
618 NodeTraits::set_balance(b, zero_balance);
620 else if(c_balance == zero_balance){
621 NodeTraits::set_balance(a, zero_balance);
622 NodeTraits::set_balance(b, zero_balance);
624 else if(c_balance == posi_balance){
625 NodeTraits::set_balance(a, zero_balance);
626 NodeTraits::set_balance(b, nega_balance);
629 BOOST_INTRUSIVE_INVARIANT_ASSERT(false); // never reached
633 static node_ptr avl_rotate_left_right(const node_ptr a, const node_ptr a_oldleft, const node_ptr & hdr)
634 { // [note: 'a_oldleft' is 'b']
641 // [d] c [d] e f [g] //
644 const node_ptr c = NodeTraits::get_right(a_oldleft);
645 bstree_algo::rotate_left_no_parent_fix(a_oldleft, c);
646 //No need to link c with a [NodeTraits::set_parent(c, a) + NodeTraits::set_left(a, c)]
647 //as c is not root and another rotation is coming
648 bstree_algo::rotate_right(a, c, NodeTraits::get_parent(a), hdr);
649 left_right_balancing(a, a_oldleft, c);
653 static node_ptr avl_rotate_right_left(const node_ptr a, const node_ptr a_oldright, const node_ptr & hdr)
654 { // [note: 'a_oldright' is 'b']
659 // [d] b(neg) ==> a b //
661 // c [g] [d] e f [g] //
664 const node_ptr c (NodeTraits::get_left(a_oldright));
665 bstree_algo::rotate_right_no_parent_fix(a_oldright, c);
666 //No need to link c with a [NodeTraits::set_parent(c, a) + NodeTraits::set_right(a, c)]
667 //as c is not root and another rotation is coming.
668 bstree_algo::rotate_left(a, c, NodeTraits::get_parent(a), hdr);
669 left_right_balancing(a_oldright, a, c);
673 static void avl_rotate_left(const node_ptr &x, const node_ptr &x_oldright, const node_ptr & hdr)
675 bstree_algo::rotate_left(x, x_oldright, NodeTraits::get_parent(x), hdr);
677 // reset the balancing factor
678 if (NodeTraits::get_balance(x_oldright) == NodeTraits::positive()) {
679 NodeTraits::set_balance(x, NodeTraits::zero());
680 NodeTraits::set_balance(x_oldright, NodeTraits::zero());
682 else { // this doesn't happen during insertions
683 NodeTraits::set_balance(x, NodeTraits::positive());
684 NodeTraits::set_balance(x_oldright, NodeTraits::negative());
688 static void avl_rotate_right(const node_ptr &x, const node_ptr &x_oldleft, const node_ptr & hdr)
690 bstree_algo::rotate_right(x, x_oldleft, NodeTraits::get_parent(x), hdr);
692 // reset the balancing factor
693 if (NodeTraits::get_balance(x_oldleft) == NodeTraits::negative()) {
694 NodeTraits::set_balance(x, NodeTraits::zero());
695 NodeTraits::set_balance(x_oldleft, NodeTraits::zero());
697 else { // this doesn't happen during insertions
698 NodeTraits::set_balance(x, NodeTraits::negative());
699 NodeTraits::set_balance(x_oldleft, NodeTraits::positive());
708 template<class NodeTraits>
709 struct get_algo<AvlTreeAlgorithms, NodeTraits>
711 typedef avltree_algorithms<NodeTraits> type;
714 template <class ValueTraits, class NodePtrCompare, class ExtraChecker>
715 struct get_node_checker<AvlTreeAlgorithms, ValueTraits, NodePtrCompare, ExtraChecker>
717 typedef detail::avltree_node_checker<ValueTraits, NodePtrCompare, ExtraChecker> type;
722 } //namespace intrusive
725 #include <boost/intrusive/detail/config_end.hpp>
727 #endif //BOOST_INTRUSIVE_AVLTREE_ALGORITHMS_HPP