1 /////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2007-2014
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // See http://www.boost.org/libs/intrusive for documentation.
11 /////////////////////////////////////////////////////////////////////////////
13 #ifndef BOOST_INTRUSIVE_BSTREE_ALGORITHMS_HPP
14 #define BOOST_INTRUSIVE_BSTREE_ALGORITHMS_HPP
17 #include <boost/intrusive/detail/config_begin.hpp>
18 #include <boost/intrusive/intrusive_fwd.hpp>
19 #include <boost/intrusive/detail/bstree_algorithms_base.hpp>
20 #include <boost/intrusive/detail/assert.hpp>
21 #include <boost/intrusive/detail/uncast.hpp>
22 #include <boost/intrusive/detail/math.hpp>
23 #include <boost/intrusive/detail/algo_type.hpp>
25 #include <boost/intrusive/detail/minimal_pair_header.hpp>
27 #if defined(BOOST_HAS_PRAGMA_ONCE)
36 //! This type is the information that will be filled by insert_unique_check
37 template <class NodePtr>
38 struct insert_commit_data_t
44 template <class NodePtr>
45 struct data_for_rebalance_t
54 template<class ValueTraits, class NodePtrCompare, class ExtraChecker>
55 struct bstree_node_checker
58 typedef ExtraChecker base_checker_t;
59 typedef ValueTraits value_traits;
60 typedef typename value_traits::node_traits node_traits;
61 typedef typename node_traits::const_node_ptr const_node_ptr;
64 : public base_checker_t::return_type
66 BOOST_INTRUSIVE_FORCEINLINE return_type()
67 : min_key_node_ptr(const_node_ptr()), max_key_node_ptr(const_node_ptr()), node_count(0)
70 const_node_ptr min_key_node_ptr;
71 const_node_ptr max_key_node_ptr;
75 BOOST_INTRUSIVE_FORCEINLINE bstree_node_checker(const NodePtrCompare& comp, ExtraChecker extra_checker)
76 : base_checker_t(extra_checker), comp_(comp)
79 void operator () (const const_node_ptr& p,
80 const return_type& check_return_left, const return_type& check_return_right,
81 return_type& check_return)
83 if (check_return_left.max_key_node_ptr)
84 BOOST_INTRUSIVE_INVARIANT_ASSERT(!comp_(p, check_return_left.max_key_node_ptr));
85 if (check_return_right.min_key_node_ptr)
86 BOOST_INTRUSIVE_INVARIANT_ASSERT(!comp_(check_return_right.min_key_node_ptr, p));
87 check_return.min_key_node_ptr = node_traits::get_left(p)? check_return_left.min_key_node_ptr : p;
88 check_return.max_key_node_ptr = node_traits::get_right(p)? check_return_right.max_key_node_ptr : p;
89 check_return.node_count = check_return_left.node_count + check_return_right.node_count + 1;
90 base_checker_t::operator()(p, check_return_left, check_return_right, check_return);
93 const NodePtrCompare comp_;
102 //! This is an implementation of a binary search tree.
103 //! A node in the search tree has references to its children and its parent. This
104 //! is to allow traversal of the whole tree from a given node making the
105 //! implementation of iterator a pointer to a node.
106 //! At the top of the tree a node is used specially. This node's parent pointer
107 //! is pointing to the root of the tree. Its left pointer points to the
108 //! leftmost node in the tree and the right pointer to the rightmost one.
109 //! This node is used to represent the end-iterator.
112 //! header------------------------------>| |
114 //! +----------(left)--------| |--------(right)---------+
121 //! root of tree ..|......................> | | |
124 //! | +-------+---------+-------+ |
130 //! | +---------+ +---------+ |
134 //! | +--+---------+--+ +--+---------+--+ |
138 //! | +---+-----+ +-----+---+ +---+-----+ +-----+---+ |
139 //! +-->| | | | | | | |<--+
140 //! | A | | C | | E | | G |
142 //! +---------+ +---------+ +---------+ +---------+
144 //! bstree_algorithms is configured with a NodeTraits class, which encapsulates the
145 //! information about the node to be manipulated. NodeTraits must support the
146 //! following interface:
150 //! <tt>node</tt>: The type of the node that forms the binary search tree
152 //! <tt>node_ptr</tt>: A pointer to a node
154 //! <tt>const_node_ptr</tt>: A pointer to a const node
156 //! <b>Static functions</b>:
158 //! <tt>static node_ptr get_parent(const_node_ptr n);</tt>
160 //! <tt>static void set_parent(node_ptr n, node_ptr parent);</tt>
162 //! <tt>static node_ptr get_left(const_node_ptr n);</tt>
164 //! <tt>static void set_left(node_ptr n, node_ptr left);</tt>
166 //! <tt>static node_ptr get_right(const_node_ptr n);</tt>
168 //! <tt>static void set_right(node_ptr n, node_ptr right);</tt>
169 template<class NodeTraits>
170 class bstree_algorithms : public bstree_algorithms_base<NodeTraits>
173 typedef typename NodeTraits::node node;
174 typedef NodeTraits node_traits;
175 typedef typename NodeTraits::node_ptr node_ptr;
176 typedef typename NodeTraits::const_node_ptr const_node_ptr;
177 typedef insert_commit_data_t<node_ptr> insert_commit_data;
178 typedef data_for_rebalance_t<node_ptr> data_for_rebalance;
181 typedef bstree_algorithms<NodeTraits> this_type;
182 typedef bstree_algorithms_base<NodeTraits> base_type;
184 template<class Disposer>
185 struct dispose_subtree_disposer
187 BOOST_INTRUSIVE_FORCEINLINE dispose_subtree_disposer(Disposer &disp, const node_ptr & subtree)
188 : disposer_(&disp), subtree_(subtree)
191 BOOST_INTRUSIVE_FORCEINLINE void release()
194 BOOST_INTRUSIVE_FORCEINLINE ~dispose_subtree_disposer()
197 dispose_subtree(subtree_, *disposer_);
201 const node_ptr subtree_;
207 //! <b>Requires</b>: 'header' is the header node of a tree.
209 //! <b>Effects</b>: Returns the first node of the tree, the header if the tree is empty.
211 //! <b>Complexity</b>: Constant time.
213 //! <b>Throws</b>: Nothing.
214 BOOST_INTRUSIVE_FORCEINLINE static node_ptr begin_node(const const_node_ptr & header)
215 { return node_traits::get_left(header); }
217 //! <b>Requires</b>: 'header' is the header node of a tree.
219 //! <b>Effects</b>: Returns the header of the tree.
221 //! <b>Complexity</b>: Constant time.
223 //! <b>Throws</b>: Nothing.
224 BOOST_INTRUSIVE_FORCEINLINE static node_ptr end_node(const const_node_ptr & header)
225 { return detail::uncast(header); }
227 //! <b>Requires</b>: 'header' is the header node of a tree.
229 //! <b>Effects</b>: Returns the root of the tree if any, header otherwise
231 //! <b>Complexity</b>: Constant time.
233 //! <b>Throws</b>: Nothing.
234 BOOST_INTRUSIVE_FORCEINLINE static node_ptr root_node(const const_node_ptr & header)
236 node_ptr p = node_traits::get_parent(header);
237 return p ? p : detail::uncast(header);
240 //! <b>Requires</b>: 'node' is a node of the tree or a node initialized
241 //! by init(...) or init_node.
243 //! <b>Effects</b>: Returns true if the node is initialized by init() or init_node().
245 //! <b>Complexity</b>: Constant time.
247 //! <b>Throws</b>: Nothing.
248 BOOST_INTRUSIVE_FORCEINLINE static bool unique(const const_node_ptr & node)
249 { return !NodeTraits::get_parent(node); }
251 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
252 //! <b>Requires</b>: 'node' is a node of the tree or a header node.
254 //! <b>Effects</b>: Returns the header of the tree.
256 //! <b>Complexity</b>: Logarithmic.
258 //! <b>Throws</b>: Nothing.
259 static node_ptr get_header(const const_node_ptr & node);
262 //! <b>Requires</b>: node1 and node2 can't be header nodes
265 //! <b>Effects</b>: Swaps two nodes. After the function node1 will be inserted
266 //! in the position node2 before the function. node2 will be inserted in the
267 //! position node1 had before the function.
269 //! <b>Complexity</b>: Logarithmic.
271 //! <b>Throws</b>: Nothing.
273 //! <b>Note</b>: This function will break container ordering invariants if
274 //! node1 and node2 are not equivalent according to the ordering rules.
276 //!Experimental function
277 static void swap_nodes(const node_ptr & node1, const node_ptr & node2)
282 node_ptr header1(base_type::get_header(node1)), header2(base_type::get_header(node2));
283 swap_nodes(node1, header1, node2, header2);
286 //! <b>Requires</b>: node1 and node2 can't be header nodes
287 //! of two trees with header header1 and header2.
289 //! <b>Effects</b>: Swaps two nodes. After the function node1 will be inserted
290 //! in the position node2 before the function. node2 will be inserted in the
291 //! position node1 had before the function.
293 //! <b>Complexity</b>: Constant.
295 //! <b>Throws</b>: Nothing.
297 //! <b>Note</b>: This function will break container ordering invariants if
298 //! node1 and node2 are not equivalent according to the ordering rules.
300 //!Experimental function
301 static void swap_nodes(const node_ptr & node1, const node_ptr & header1, const node_ptr & node2, const node_ptr & header2)
306 //node1 and node2 must not be header nodes
307 //BOOST_INTRUSIVE_INVARIANT_ASSERT((header1 != node1 && header2 != node2));
308 if(header1 != header2){
309 //Update header1 if necessary
310 if(node1 == NodeTraits::get_left(header1)){
311 NodeTraits::set_left(header1, node2);
314 if(node1 == NodeTraits::get_right(header1)){
315 NodeTraits::set_right(header1, node2);
318 if(node1 == NodeTraits::get_parent(header1)){
319 NodeTraits::set_parent(header1, node2);
322 //Update header2 if necessary
323 if(node2 == NodeTraits::get_left(header2)){
324 NodeTraits::set_left(header2, node1);
327 if(node2 == NodeTraits::get_right(header2)){
328 NodeTraits::set_right(header2, node1);
331 if(node2 == NodeTraits::get_parent(header2)){
332 NodeTraits::set_parent(header2, node1);
336 //If both nodes are from the same tree
337 //Update header if necessary
338 if(node1 == NodeTraits::get_left(header1)){
339 NodeTraits::set_left(header1, node2);
341 else if(node2 == NodeTraits::get_left(header2)){
342 NodeTraits::set_left(header2, node1);
345 if(node1 == NodeTraits::get_right(header1)){
346 NodeTraits::set_right(header1, node2);
348 else if(node2 == NodeTraits::get_right(header2)){
349 NodeTraits::set_right(header2, node1);
352 if(node1 == NodeTraits::get_parent(header1)){
353 NodeTraits::set_parent(header1, node2);
355 else if(node2 == NodeTraits::get_parent(header2)){
356 NodeTraits::set_parent(header2, node1);
359 //Adjust data in nodes to be swapped
360 //so that final link swap works as expected
361 if(node1 == NodeTraits::get_parent(node2)){
362 NodeTraits::set_parent(node2, node2);
364 if(node2 == NodeTraits::get_right(node1)){
365 NodeTraits::set_right(node1, node1);
368 NodeTraits::set_left(node1, node1);
371 else if(node2 == NodeTraits::get_parent(node1)){
372 NodeTraits::set_parent(node1, node1);
374 if(node1 == NodeTraits::get_right(node2)){
375 NodeTraits::set_right(node2, node2);
378 NodeTraits::set_left(node2, node2);
383 //Now swap all the links
386 temp = NodeTraits::get_left(node1);
387 NodeTraits::set_left(node1, NodeTraits::get_left(node2));
388 NodeTraits::set_left(node2, temp);
390 temp = NodeTraits::get_right(node1);
391 NodeTraits::set_right(node1, NodeTraits::get_right(node2));
392 NodeTraits::set_right(node2, temp);
394 temp = NodeTraits::get_parent(node1);
395 NodeTraits::set_parent(node1, NodeTraits::get_parent(node2));
396 NodeTraits::set_parent(node2, temp);
398 //Now adjust adjacent nodes for newly inserted node 1
399 if((temp = NodeTraits::get_left(node1))){
400 NodeTraits::set_parent(temp, node1);
402 if((temp = NodeTraits::get_right(node1))){
403 NodeTraits::set_parent(temp, node1);
405 if((temp = NodeTraits::get_parent(node1)) &&
406 //The header has been already updated so avoid it
408 if(NodeTraits::get_left(temp) == node2){
409 NodeTraits::set_left(temp, node1);
411 if(NodeTraits::get_right(temp) == node2){
412 NodeTraits::set_right(temp, node1);
415 //Now adjust adjacent nodes for newly inserted node 2
416 if((temp = NodeTraits::get_left(node2))){
417 NodeTraits::set_parent(temp, node2);
419 if((temp = NodeTraits::get_right(node2))){
420 NodeTraits::set_parent(temp, node2);
422 if((temp = NodeTraits::get_parent(node2)) &&
423 //The header has been already updated so avoid it
425 if(NodeTraits::get_left(temp) == node1){
426 NodeTraits::set_left(temp, node2);
428 if(NodeTraits::get_right(temp) == node1){
429 NodeTraits::set_right(temp, node2);
434 //! <b>Requires</b>: node_to_be_replaced must be inserted in a tree
435 //! and new_node must not be inserted in a tree.
437 //! <b>Effects</b>: Replaces node_to_be_replaced in its position in the
438 //! tree with new_node. The tree does not need to be rebalanced
440 //! <b>Complexity</b>: Logarithmic.
442 //! <b>Throws</b>: Nothing.
444 //! <b>Note</b>: This function will break container ordering invariants if
445 //! new_node is not equivalent to node_to_be_replaced according to the
446 //! ordering rules. This function is faster than erasing and inserting
447 //! the node, since no rebalancing and comparison is needed. Experimental function
448 BOOST_INTRUSIVE_FORCEINLINE static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & new_node)
450 if(node_to_be_replaced == new_node)
452 replace_node(node_to_be_replaced, base_type::get_header(node_to_be_replaced), new_node);
455 //! <b>Requires</b>: node_to_be_replaced must be inserted in a tree
456 //! with header "header" and new_node must not be inserted in a tree.
458 //! <b>Effects</b>: Replaces node_to_be_replaced in its position in the
459 //! tree with new_node. The tree does not need to be rebalanced
461 //! <b>Complexity</b>: Constant.
463 //! <b>Throws</b>: Nothing.
465 //! <b>Note</b>: This function will break container ordering invariants if
466 //! new_node is not equivalent to node_to_be_replaced according to the
467 //! ordering rules. This function is faster than erasing and inserting
468 //! the node, since no rebalancing or comparison is needed. Experimental function
469 static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & header, const node_ptr & new_node)
471 if(node_to_be_replaced == new_node)
474 //Update header if necessary
475 if(node_to_be_replaced == NodeTraits::get_left(header)){
476 NodeTraits::set_left(header, new_node);
479 if(node_to_be_replaced == NodeTraits::get_right(header)){
480 NodeTraits::set_right(header, new_node);
483 if(node_to_be_replaced == NodeTraits::get_parent(header)){
484 NodeTraits::set_parent(header, new_node);
487 //Now set data from the original node
489 NodeTraits::set_left(new_node, NodeTraits::get_left(node_to_be_replaced));
490 NodeTraits::set_right(new_node, NodeTraits::get_right(node_to_be_replaced));
491 NodeTraits::set_parent(new_node, NodeTraits::get_parent(node_to_be_replaced));
493 //Now adjust adjacent nodes for newly inserted node
494 if((temp = NodeTraits::get_left(new_node))){
495 NodeTraits::set_parent(temp, new_node);
497 if((temp = NodeTraits::get_right(new_node))){
498 NodeTraits::set_parent(temp, new_node);
500 if((temp = NodeTraits::get_parent(new_node)) &&
501 //The header has been already updated so avoid it
503 if(NodeTraits::get_left(temp) == node_to_be_replaced){
504 NodeTraits::set_left(temp, new_node);
506 if(NodeTraits::get_right(temp) == node_to_be_replaced){
507 NodeTraits::set_right(temp, new_node);
512 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
513 //! <b>Requires</b>: 'node' is a node from the tree except the header.
515 //! <b>Effects</b>: Returns the next node of the tree.
517 //! <b>Complexity</b>: Average constant time.
519 //! <b>Throws</b>: Nothing.
520 static node_ptr next_node(const node_ptr & node);
522 //! <b>Requires</b>: 'node' is a node from the tree except the leftmost node.
524 //! <b>Effects</b>: Returns the previous node of the tree.
526 //! <b>Complexity</b>: Average constant time.
528 //! <b>Throws</b>: Nothing.
529 static node_ptr prev_node(const node_ptr & node);
531 //! <b>Requires</b>: 'node' is a node of a tree but not the header.
533 //! <b>Effects</b>: Returns the minimum node of the subtree starting at p.
535 //! <b>Complexity</b>: Logarithmic to the size of the subtree.
537 //! <b>Throws</b>: Nothing.
538 static node_ptr minimum(node_ptr node);
540 //! <b>Requires</b>: 'node' is a node of a tree but not the header.
542 //! <b>Effects</b>: Returns the maximum node of the subtree starting at p.
544 //! <b>Complexity</b>: Logarithmic to the size of the subtree.
546 //! <b>Throws</b>: Nothing.
547 static node_ptr maximum(node_ptr node);
550 //! <b>Requires</b>: 'node' must not be part of any tree.
552 //! <b>Effects</b>: After the function unique(node) == true.
554 //! <b>Complexity</b>: Constant.
556 //! <b>Throws</b>: Nothing.
558 //! <b>Nodes</b>: If node is inserted in a tree, this function corrupts the tree.
559 BOOST_INTRUSIVE_FORCEINLINE static void init(const node_ptr & node)
561 NodeTraits::set_parent(node, node_ptr());
562 NodeTraits::set_left(node, node_ptr());
563 NodeTraits::set_right(node, node_ptr());
566 //! <b>Effects</b>: Returns true if node is in the same state as if called init(node)
568 //! <b>Complexity</b>: Constant.
570 //! <b>Throws</b>: Nothing.
571 BOOST_INTRUSIVE_FORCEINLINE static bool inited(const const_node_ptr & node)
573 return !NodeTraits::get_parent(node) &&
574 !NodeTraits::get_left(node) &&
575 !NodeTraits::get_right(node) ;
578 //! <b>Requires</b>: node must not be part of any tree.
580 //! <b>Effects</b>: Initializes the header to represent an empty tree.
581 //! unique(header) == true.
583 //! <b>Complexity</b>: Constant.
585 //! <b>Throws</b>: Nothing.
587 //! <b>Nodes</b>: If node is inserted in a tree, this function corrupts the tree.
588 BOOST_INTRUSIVE_FORCEINLINE static void init_header(const node_ptr & header)
590 NodeTraits::set_parent(header, node_ptr());
591 NodeTraits::set_left(header, header);
592 NodeTraits::set_right(header, header);
595 //! <b>Requires</b>: "disposer" must be an object function
596 //! taking a node_ptr parameter and shouldn't throw.
598 //! <b>Effects</b>: Empties the target tree calling
599 //! <tt>void disposer::operator()(const node_ptr &)</tt> for every node of the tree
600 //! except the header.
602 //! <b>Complexity</b>: Linear to the number of element of the source tree plus the.
603 //! number of elements of tree target tree when calling this function.
605 //! <b>Throws</b>: If cloner functor throws. If this happens target nodes are disposed.
606 template<class Disposer>
607 static void clear_and_dispose(const node_ptr & header, Disposer disposer)
609 node_ptr source_root = NodeTraits::get_parent(header);
612 dispose_subtree(source_root, disposer);
616 //! <b>Requires</b>: header is the header of a tree.
618 //! <b>Effects</b>: Unlinks the leftmost node from the tree, and
619 //! updates the header link to the new leftmost node.
621 //! <b>Complexity</b>: Average complexity is constant time.
623 //! <b>Throws</b>: Nothing.
625 //! <b>Notes</b>: This function breaks the tree and the tree can
626 //! only be used for more unlink_leftmost_without_rebalance calls.
627 //! This function is normally used to achieve a step by step
628 //! controlled destruction of the tree.
629 static node_ptr unlink_leftmost_without_rebalance(const node_ptr & header)
631 node_ptr leftmost = NodeTraits::get_left(header);
632 if (leftmost == header)
634 node_ptr leftmost_parent(NodeTraits::get_parent(leftmost));
635 node_ptr leftmost_right (NodeTraits::get_right(leftmost));
636 bool is_root = leftmost_parent == header;
639 NodeTraits::set_parent(leftmost_right, leftmost_parent);
640 NodeTraits::set_left(header, base_type::minimum(leftmost_right));
643 NodeTraits::set_parent(header, leftmost_right);
645 NodeTraits::set_left(NodeTraits::get_parent(header), leftmost_right);
648 NodeTraits::set_parent(header, node_ptr());
649 NodeTraits::set_left(header, header);
650 NodeTraits::set_right(header, header);
653 NodeTraits::set_left(leftmost_parent, node_ptr());
654 NodeTraits::set_left(header, leftmost_parent);
659 //! <b>Requires</b>: node is a node of the tree but it's not the header.
661 //! <b>Effects</b>: Returns the number of nodes of the subtree.
663 //! <b>Complexity</b>: Linear time.
665 //! <b>Throws</b>: Nothing.
666 static std::size_t size(const const_node_ptr & header)
668 node_ptr beg(begin_node(header));
669 node_ptr end(end_node(header));
671 for(;beg != end; beg = base_type::next_node(beg)) ++i;
675 //! <b>Requires</b>: header1 and header2 must be the header nodes
678 //! <b>Effects</b>: Swaps two trees. After the function header1 will contain
679 //! links to the second tree and header2 will have links to the first tree.
681 //! <b>Complexity</b>: Constant.
683 //! <b>Throws</b>: Nothing.
684 static void swap_tree(const node_ptr & header1, const node_ptr & header2)
686 if(header1 == header2)
692 tmp = NodeTraits::get_parent(header1);
693 NodeTraits::set_parent(header1, NodeTraits::get_parent(header2));
694 NodeTraits::set_parent(header2, tmp);
696 tmp = NodeTraits::get_left(header1);
697 NodeTraits::set_left(header1, NodeTraits::get_left(header2));
698 NodeTraits::set_left(header2, tmp);
700 tmp = NodeTraits::get_right(header1);
701 NodeTraits::set_right(header1, NodeTraits::get_right(header2));
702 NodeTraits::set_right(header2, tmp);
705 node_ptr h1_parent(NodeTraits::get_parent(header1));
707 NodeTraits::set_parent(h1_parent, header1);
710 NodeTraits::set_left(header1, header1);
711 NodeTraits::set_right(header1, header1);
714 node_ptr h2_parent(NodeTraits::get_parent(header2));
716 NodeTraits::set_parent(h2_parent, header2);
719 NodeTraits::set_left(header2, header2);
720 NodeTraits::set_right(header2, header2);
724 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
725 //! <b>Requires</b>: p is a node of a tree.
727 //! <b>Effects</b>: Returns true if p is the header of the tree.
729 //! <b>Complexity</b>: Constant.
731 //! <b>Throws</b>: Nothing.
732 static bool is_header(const const_node_ptr & p);
735 //! <b>Requires</b>: "header" must be the header node of a tree.
736 //! KeyNodePtrCompare is a function object that induces a strict weak
737 //! ordering compatible with the strict weak ordering used to create the
738 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
740 //! <b>Effects</b>: Returns a node_ptr to the first element that is equivalent to
741 //! "key" according to "comp" or "header" if that element does not exist.
743 //! <b>Complexity</b>: Logarithmic.
745 //! <b>Throws</b>: If "comp" throws.
746 template<class KeyType, class KeyNodePtrCompare>
748 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
750 node_ptr end = detail::uncast(header);
751 node_ptr y = lower_bound(header, key, comp);
752 return (y == end || comp(key, y)) ? end : y;
755 //! <b>Requires</b>: "header" must be the header node of a tree.
756 //! KeyNodePtrCompare is a function object that induces a strict weak
757 //! ordering compatible with the strict weak ordering used to create the
758 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
759 //! 'lower_key' must not be greater than 'upper_key' according to 'comp'. If
760 //! 'lower_key' == 'upper_key', ('left_closed' || 'right_closed') must be true.
762 //! <b>Effects</b>: Returns an a pair with the following criteria:
764 //! first = lower_bound(lower_key) if left_closed, upper_bound(lower_key) otherwise
766 //! second = upper_bound(upper_key) if right_closed, lower_bound(upper_key) otherwise
768 //! <b>Complexity</b>: Logarithmic.
770 //! <b>Throws</b>: If "comp" throws.
772 //! <b>Note</b>: This function can be more efficient than calling upper_bound
773 //! and lower_bound for lower_key and upper_key.
775 //! <b>Note</b>: Experimental function, the interface might change.
776 template< class KeyType, class KeyNodePtrCompare>
777 static std::pair<node_ptr, node_ptr> bounded_range
778 ( const const_node_ptr & header
779 , const KeyType &lower_key
780 , const KeyType &upper_key
781 , KeyNodePtrCompare comp
785 node_ptr y = detail::uncast(header);
786 node_ptr x = NodeTraits::get_parent(header);
789 //If x is less than lower_key the target
790 //range is on the right part
791 if(comp(x, lower_key)){
792 //Check for invalid input range
793 BOOST_INTRUSIVE_INVARIANT_ASSERT(comp(x, upper_key));
794 x = NodeTraits::get_right(x);
796 //If the upper_key is less than x, the target
797 //range is on the left part
798 else if(comp(upper_key, x)){
800 x = NodeTraits::get_left(x);
803 //x is inside the bounded range(lower_key <= x <= upper_key),
804 //so we must split lower and upper searches
806 //Sanity check: if lower_key and upper_key are equal, then both left_closed and right_closed can't be false
807 BOOST_INTRUSIVE_INVARIANT_ASSERT(left_closed || right_closed || comp(lower_key, x) || comp(x, upper_key));
808 return std::pair<node_ptr,node_ptr>(
810 //If left_closed, then comp(x, lower_key) is already the lower_bound
811 //condition so we save one comparison and go to the next level
812 //following traditional lower_bound algo
813 ? lower_bound_loop(NodeTraits::get_left(x), x, lower_key, comp)
814 //If left-open, comp(x, lower_key) is not the upper_bound algo
815 //condition so we must recheck current 'x' node with upper_bound algo
816 : upper_bound_loop(x, y, lower_key, comp)
819 //If right_closed, then comp(upper_key, x) is already the upper_bound
820 //condition so we can save one comparison and go to the next level
821 //following lower_bound algo
822 ? upper_bound_loop(NodeTraits::get_right(x), y, upper_key, comp)
823 //If right-open, comp(upper_key, x) is not the lower_bound algo
824 //condition so we must recheck current 'x' node with lower_bound algo
825 : lower_bound_loop(x, y, upper_key, comp)
829 return std::pair<node_ptr,node_ptr> (y, y);
832 //! <b>Requires</b>: "header" must be the header node of a tree.
833 //! KeyNodePtrCompare is a function object that induces a strict weak
834 //! ordering compatible with the strict weak ordering used to create the
835 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
837 //! <b>Effects</b>: Returns the number of elements with a key equivalent to "key"
838 //! according to "comp".
840 //! <b>Complexity</b>: Logarithmic.
842 //! <b>Throws</b>: If "comp" throws.
843 template<class KeyType, class KeyNodePtrCompare>
844 static std::size_t count
845 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
847 std::pair<node_ptr, node_ptr> ret = equal_range(header, key, comp);
849 while(ret.first != ret.second){
851 ret.first = base_type::next_node(ret.first);
856 //! <b>Requires</b>: "header" must be the header node of a tree.
857 //! KeyNodePtrCompare is a function object that induces a strict weak
858 //! ordering compatible with the strict weak ordering used to create the
859 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
861 //! <b>Effects</b>: Returns an a pair of node_ptr delimiting a range containing
862 //! all elements that are equivalent to "key" according to "comp" or an
863 //! empty range that indicates the position where those elements would be
864 //! if there are no equivalent elements.
866 //! <b>Complexity</b>: Logarithmic.
868 //! <b>Throws</b>: If "comp" throws.
869 template<class KeyType, class KeyNodePtrCompare>
870 BOOST_INTRUSIVE_FORCEINLINE static std::pair<node_ptr, node_ptr> equal_range
871 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
873 return bounded_range(header, key, key, comp, true, true);
876 //! <b>Requires</b>: "header" must be the header node of a tree.
877 //! KeyNodePtrCompare is a function object that induces a strict weak
878 //! ordering compatible with the strict weak ordering used to create the
879 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
881 //! <b>Effects</b>: Returns an a pair of node_ptr delimiting a range containing
882 //! the first element that is equivalent to "key" according to "comp" or an
883 //! empty range that indicates the position where that element would be
884 //! if there are no equivalent elements.
886 //! <b>Complexity</b>: Logarithmic.
888 //! <b>Throws</b>: If "comp" throws.
889 template<class KeyType, class KeyNodePtrCompare>
890 static std::pair<node_ptr, node_ptr> lower_bound_range
891 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
893 node_ptr const lb(lower_bound(header, key, comp));
894 std::pair<node_ptr, node_ptr> ret_ii(lb, lb);
895 if(lb != header && !comp(key, lb)){
896 ret_ii.second = base_type::next_node(ret_ii.second);
901 //! <b>Requires</b>: "header" must be the header node of a tree.
902 //! KeyNodePtrCompare is a function object that induces a strict weak
903 //! ordering compatible with the strict weak ordering used to create the
904 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
906 //! <b>Effects</b>: Returns a node_ptr to the first element that is
907 //! not less than "key" according to "comp" or "header" if that element does
910 //! <b>Complexity</b>: Logarithmic.
912 //! <b>Throws</b>: If "comp" throws.
913 template<class KeyType, class KeyNodePtrCompare>
914 BOOST_INTRUSIVE_FORCEINLINE static node_ptr lower_bound
915 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
917 return lower_bound_loop(NodeTraits::get_parent(header), detail::uncast(header), key, comp);
920 //! <b>Requires</b>: "header" must be the header node of a tree.
921 //! KeyNodePtrCompare is a function object that induces a strict weak
922 //! ordering compatible with the strict weak ordering used to create the
923 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
925 //! <b>Effects</b>: Returns a node_ptr to the first element that is greater
926 //! than "key" according to "comp" or "header" if that element does not exist.
928 //! <b>Complexity</b>: Logarithmic.
930 //! <b>Throws</b>: If "comp" throws.
931 template<class KeyType, class KeyNodePtrCompare>
932 BOOST_INTRUSIVE_FORCEINLINE static node_ptr upper_bound
933 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
935 return upper_bound_loop(NodeTraits::get_parent(header), detail::uncast(header), key, comp);
938 //! <b>Requires</b>: "header" must be the header node of a tree.
939 //! "commit_data" must have been obtained from a previous call to
940 //! "insert_unique_check". No objects should have been inserted or erased
941 //! from the set between the "insert_unique_check" that filled "commit_data"
942 //! and the call to "insert_commit".
945 //! <b>Effects</b>: Inserts new_node in the set using the information obtained
946 //! from the "commit_data" that a previous "insert_check" filled.
948 //! <b>Complexity</b>: Constant time.
950 //! <b>Throws</b>: Nothing.
952 //! <b>Notes</b>: This function has only sense if a "insert_unique_check" has been
953 //! previously executed to fill "commit_data". No value should be inserted or
954 //! erased between the "insert_check" and "insert_commit" calls.
955 BOOST_INTRUSIVE_FORCEINLINE static void insert_unique_commit
956 (const node_ptr & header, const node_ptr & new_value, const insert_commit_data &commit_data)
957 { return insert_commit(header, new_value, commit_data); }
959 //! <b>Requires</b>: "header" must be the header node of a tree.
960 //! KeyNodePtrCompare is a function object that induces a strict weak
961 //! ordering compatible with the strict weak ordering used to create the
962 //! the tree. NodePtrCompare compares KeyType with a node_ptr.
964 //! <b>Effects</b>: Checks if there is an equivalent node to "key" in the
965 //! tree according to "comp" and obtains the needed information to realize
966 //! a constant-time node insertion if there is no equivalent node.
968 //! <b>Returns</b>: If there is an equivalent value
969 //! returns a pair containing a node_ptr to the already present node
970 //! and false. If there is not equivalent key can be inserted returns true
971 //! in the returned pair's boolean and fills "commit_data" that is meant to
972 //! be used with the "insert_commit" function to achieve a constant-time
973 //! insertion function.
975 //! <b>Complexity</b>: Average complexity is at most logarithmic.
977 //! <b>Throws</b>: If "comp" throws.
979 //! <b>Notes</b>: This function is used to improve performance when constructing
980 //! a node is expensive and the user does not want to have two equivalent nodes
981 //! in the tree: if there is an equivalent value
982 //! the constructed object must be discarded. Many times, the part of the
983 //! node that is used to impose the order is much cheaper to construct
984 //! than the node and this function offers the possibility to use that part
985 //! to check if the insertion will be successful.
987 //! If the check is successful, the user can construct the node and use
988 //! "insert_commit" to insert the node in constant-time. This gives a total
989 //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
991 //! "commit_data" remains valid for a subsequent "insert_unique_commit" only
992 //! if no more objects are inserted or erased from the set.
993 template<class KeyType, class KeyNodePtrCompare>
994 static std::pair<node_ptr, bool> insert_unique_check
995 (const const_node_ptr & header, const KeyType &key
996 ,KeyNodePtrCompare comp, insert_commit_data &commit_data
997 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
998 , std::size_t *pdepth = 0
1002 std::size_t depth = 0;
1003 node_ptr h(detail::uncast(header));
1005 node_ptr x(NodeTraits::get_parent(y));
1006 node_ptr prev = node_ptr();
1008 //Find the upper bound, cache the previous value and if we should
1009 //store it in the left or right node
1010 bool left_child = true;
1014 x = (left_child = comp(key, x)) ?
1015 NodeTraits::get_left(x) : (prev = y, NodeTraits::get_right(x));
1018 if(pdepth) *pdepth = depth;
1020 //Since we've found the upper bound there is no other value with the same key if:
1021 // - There is no previous node
1022 // - The previous node is less than the key
1023 const bool not_present = !prev || comp(prev, key);
1025 commit_data.link_left = left_child;
1026 commit_data.node = y;
1028 return std::pair<node_ptr, bool>(prev, not_present);
1031 //! <b>Requires</b>: "header" must be the header node of a tree.
1032 //! KeyNodePtrCompare is a function object that induces a strict weak
1033 //! ordering compatible with the strict weak ordering used to create the
1034 //! the tree. NodePtrCompare compares KeyType with a node_ptr.
1035 //! "hint" is node from the "header"'s tree.
1037 //! <b>Effects</b>: Checks if there is an equivalent node to "key" in the
1038 //! tree according to "comp" using "hint" as a hint to where it should be
1039 //! inserted and obtains the needed information to realize
1040 //! a constant-time node insertion if there is no equivalent node.
1041 //! If "hint" is the upper_bound the function has constant time
1042 //! complexity (two comparisons in the worst case).
1044 //! <b>Returns</b>: If there is an equivalent value
1045 //! returns a pair containing a node_ptr to the already present node
1046 //! and false. If there is not equivalent key can be inserted returns true
1047 //! in the returned pair's boolean and fills "commit_data" that is meant to
1048 //! be used with the "insert_commit" function to achieve a constant-time
1049 //! insertion function.
1051 //! <b>Complexity</b>: Average complexity is at most logarithmic, but it is
1052 //! amortized constant time if new_node should be inserted immediately before "hint".
1054 //! <b>Throws</b>: If "comp" throws.
1056 //! <b>Notes</b>: This function is used to improve performance when constructing
1057 //! a node is expensive and the user does not want to have two equivalent nodes
1058 //! in the tree: if there is an equivalent value
1059 //! the constructed object must be discarded. Many times, the part of the
1060 //! node that is used to impose the order is much cheaper to construct
1061 //! than the node and this function offers the possibility to use that part
1062 //! to check if the insertion will be successful.
1064 //! If the check is successful, the user can construct the node and use
1065 //! "insert_commit" to insert the node in constant-time. This gives a total
1066 //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
1068 //! "commit_data" remains valid for a subsequent "insert_unique_commit" only
1069 //! if no more objects are inserted or erased from the set.
1070 template<class KeyType, class KeyNodePtrCompare>
1071 static std::pair<node_ptr, bool> insert_unique_check
1072 (const const_node_ptr & header, const node_ptr &hint, const KeyType &key
1073 ,KeyNodePtrCompare comp, insert_commit_data &commit_data
1074 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
1075 , std::size_t *pdepth = 0
1079 //hint must be bigger than the key
1080 if(hint == header || comp(key, hint)){
1081 node_ptr prev(hint);
1082 //Previous value should be less than the key
1083 if(hint == begin_node(header) || comp((prev = base_type::prev_node(hint)), key)){
1084 commit_data.link_left = unique(header) || !NodeTraits::get_left(hint);
1085 commit_data.node = commit_data.link_left ? hint : prev;
1087 *pdepth = commit_data.node == header ? 0 : depth(commit_data.node) + 1;
1089 return std::pair<node_ptr, bool>(node_ptr(), true);
1092 //Hint was wrong, use hintless insertion
1093 return insert_unique_check(header, key, comp, commit_data, pdepth);
1096 //! <b>Requires</b>: "header" must be the header node of a tree.
1097 //! NodePtrCompare is a function object that induces a strict weak
1098 //! ordering compatible with the strict weak ordering used to create the
1099 //! the tree. NodePtrCompare compares two node_ptrs. "hint" is node from
1100 //! the "header"'s tree.
1102 //! <b>Effects</b>: Inserts new_node into the tree, using "hint" as a hint to
1103 //! where it will be inserted. If "hint" is the upper_bound
1104 //! the insertion takes constant time (two comparisons in the worst case).
1106 //! <b>Complexity</b>: Logarithmic in general, but it is amortized
1107 //! constant time if new_node is inserted immediately before "hint".
1109 //! <b>Throws</b>: If "comp" throws.
1110 template<class NodePtrCompare>
1111 static node_ptr insert_equal
1112 (const node_ptr & h, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp
1113 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
1114 , std::size_t *pdepth = 0
1118 insert_commit_data commit_data;
1119 insert_equal_check(h, hint, new_node, comp, commit_data, pdepth);
1120 insert_commit(h, new_node, commit_data);
1124 //! <b>Requires</b>: "h" must be the header node of a tree.
1125 //! NodePtrCompare is a function object that induces a strict weak
1126 //! ordering compatible with the strict weak ordering used to create the
1127 //! the tree. NodePtrCompare compares two node_ptrs.
1129 //! <b>Effects</b>: Inserts new_node into the tree before the upper bound
1130 //! according to "comp".
1132 //! <b>Complexity</b>: Average complexity for insert element is at
1133 //! most logarithmic.
1135 //! <b>Throws</b>: If "comp" throws.
1136 template<class NodePtrCompare>
1137 static node_ptr insert_equal_upper_bound
1138 (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp
1139 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
1140 , std::size_t *pdepth = 0
1144 insert_commit_data commit_data;
1145 insert_equal_upper_bound_check(h, new_node, comp, commit_data, pdepth);
1146 insert_commit(h, new_node, commit_data);
1150 //! <b>Requires</b>: "h" must be the header node of a tree.
1151 //! NodePtrCompare is a function object that induces a strict weak
1152 //! ordering compatible with the strict weak ordering used to create the
1153 //! the tree. NodePtrCompare compares two node_ptrs.
1155 //! <b>Effects</b>: Inserts new_node into the tree before the lower bound
1156 //! according to "comp".
1158 //! <b>Complexity</b>: Average complexity for insert element is at
1159 //! most logarithmic.
1161 //! <b>Throws</b>: If "comp" throws.
1162 template<class NodePtrCompare>
1163 static node_ptr insert_equal_lower_bound
1164 (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp
1165 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
1166 , std::size_t *pdepth = 0
1170 insert_commit_data commit_data;
1171 insert_equal_lower_bound_check(h, new_node, comp, commit_data, pdepth);
1172 insert_commit(h, new_node, commit_data);
1176 //! <b>Requires</b>: "header" must be the header node of a tree.
1177 //! "pos" must be a valid iterator or header (end) node.
1178 //! "pos" must be an iterator pointing to the successor to "new_node"
1179 //! once inserted according to the order of already inserted nodes. This function does not
1180 //! check "pos" and this precondition must be guaranteed by the caller.
1182 //! <b>Effects</b>: Inserts new_node into the tree before "pos".
1184 //! <b>Complexity</b>: Constant-time.
1186 //! <b>Throws</b>: Nothing.
1188 //! <b>Note</b>: If "pos" is not the successor of the newly inserted "new_node"
1189 //! tree invariants might be broken.
1190 static node_ptr insert_before
1191 (const node_ptr & header, const node_ptr & pos, const node_ptr & new_node
1192 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
1193 , std::size_t *pdepth = 0
1197 insert_commit_data commit_data;
1198 insert_before_check(header, pos, commit_data, pdepth);
1199 insert_commit(header, new_node, commit_data);
1203 //! <b>Requires</b>: "header" must be the header node of a tree.
1204 //! "new_node" must be, according to the used ordering no less than the
1205 //! greatest inserted key.
1207 //! <b>Effects</b>: Inserts new_node into the tree before "pos".
1209 //! <b>Complexity</b>: Constant-time.
1211 //! <b>Throws</b>: Nothing.
1213 //! <b>Note</b>: If "new_node" is less than the greatest inserted key
1214 //! tree invariants are broken. This function is slightly faster than
1215 //! using "insert_before".
1216 static void push_back
1217 (const node_ptr & header, const node_ptr & new_node
1218 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
1219 , std::size_t *pdepth = 0
1223 insert_commit_data commit_data;
1224 push_back_check(header, commit_data, pdepth);
1225 insert_commit(header, new_node, commit_data);
1228 //! <b>Requires</b>: "header" must be the header node of a tree.
1229 //! "new_node" must be, according to the used ordering, no greater than the
1230 //! lowest inserted key.
1232 //! <b>Effects</b>: Inserts new_node into the tree before "pos".
1234 //! <b>Complexity</b>: Constant-time.
1236 //! <b>Throws</b>: Nothing.
1238 //! <b>Note</b>: If "new_node" is greater than the lowest inserted key
1239 //! tree invariants are broken. This function is slightly faster than
1240 //! using "insert_before".
1241 static void push_front
1242 (const node_ptr & header, const node_ptr & new_node
1243 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
1244 , std::size_t *pdepth = 0
1248 insert_commit_data commit_data;
1249 push_front_check(header, commit_data, pdepth);
1250 insert_commit(header, new_node, commit_data);
1253 //! <b>Requires</b>: 'node' can't be a header node.
1255 //! <b>Effects</b>: Calculates the depth of a node: the depth of a
1256 //! node is the length (number of edges) of the path from the root
1257 //! to that node. (The root node is at depth 0.)
1259 //! <b>Complexity</b>: Logarithmic to the number of nodes in the tree.
1261 //! <b>Throws</b>: Nothing.
1262 static std::size_t depth(const_node_ptr node)
1264 std::size_t depth = 0;
1266 while(node != NodeTraits::get_parent(p_parent = NodeTraits::get_parent(node))){
1273 //! <b>Requires</b>: "cloner" must be a function
1274 //! object taking a node_ptr and returning a new cloned node of it. "disposer" must
1275 //! take a node_ptr and shouldn't throw.
1277 //! <b>Effects</b>: First empties target tree calling
1278 //! <tt>void disposer::operator()(const node_ptr &)</tt> for every node of the tree
1279 //! except the header.
1281 //! Then, duplicates the entire tree pointed by "source_header" cloning each
1282 //! source node with <tt>node_ptr Cloner::operator()(const node_ptr &)</tt> to obtain
1283 //! the nodes of the target tree. If "cloner" throws, the cloned target nodes
1284 //! are disposed using <tt>void disposer(const node_ptr &)</tt>.
1286 //! <b>Complexity</b>: Linear to the number of element of the source tree plus the
1287 //! number of elements of tree target tree when calling this function.
1289 //! <b>Throws</b>: If cloner functor throws. If this happens target nodes are disposed.
1290 template <class Cloner, class Disposer>
1292 (const const_node_ptr & source_header, const node_ptr & target_header, Cloner cloner, Disposer disposer)
1294 if(!unique(target_header)){
1295 clear_and_dispose(target_header, disposer);
1298 node_ptr leftmost, rightmost;
1299 node_ptr new_root = clone_subtree
1300 (source_header, target_header, cloner, disposer, leftmost, rightmost);
1302 //Now update header node
1303 NodeTraits::set_parent(target_header, new_root);
1304 NodeTraits::set_left (target_header, leftmost);
1305 NodeTraits::set_right (target_header, rightmost);
1308 //! <b>Requires</b>: header must be the header of a tree, z a node
1309 //! of that tree and z != header.
1311 //! <b>Effects</b>: Erases node "z" from the tree with header "header".
1313 //! <b>Complexity</b>: Amortized constant time.
1315 //! <b>Throws</b>: Nothing.
1316 BOOST_INTRUSIVE_FORCEINLINE static void erase(const node_ptr & header, const node_ptr & z)
1318 data_for_rebalance ignored;
1319 erase(header, z, ignored);
1322 //! <b>Requires</b>: header1 and header2 must be the headers of trees tree1 and tree2
1323 //! respectively, z a non-header node of tree1. NodePtrCompare is the comparison
1324 //! function of tree1..
1326 //! <b>Effects</b>: Transfers node "z" from tree1 to tree2 if tree1 does not contain
1327 //! a node that is equivalent to z.
1329 //! <b>Returns</b>: True if the node was trasferred, false otherwise.
1331 //! <b>Complexity</b>: Logarithmic.
1333 //! <b>Throws</b>: If the comparison throws.
1334 template<class NodePtrCompare>
1335 BOOST_INTRUSIVE_FORCEINLINE static bool transfer_unique
1336 (const node_ptr & header1, NodePtrCompare comp, const node_ptr &header2, const node_ptr & z)
1338 data_for_rebalance ignored;
1339 return transfer_unique(header1, comp, header2, z, ignored);
1342 //! <b>Requires</b>: header1 and header2 must be the headers of trees tree1 and tree2
1343 //! respectively, z a non-header node of tree1. NodePtrCompare is the comparison
1344 //! function of tree1..
1346 //! <b>Effects</b>: Transfers node "z" from tree1 to tree2.
1348 //! <b>Complexity</b>: Logarithmic.
1350 //! <b>Throws</b>: If the comparison throws.
1351 template<class NodePtrCompare>
1352 BOOST_INTRUSIVE_FORCEINLINE static void transfer_equal
1353 (const node_ptr & header1, NodePtrCompare comp, const node_ptr &header2, const node_ptr & z)
1355 data_for_rebalance ignored;
1356 transfer_equal(header1, comp, header2, z, ignored);
1359 //! <b>Requires</b>: node is a tree node but not the header.
1361 //! <b>Effects</b>: Unlinks the node and rebalances the tree.
1363 //! <b>Complexity</b>: Average complexity is constant time.
1365 //! <b>Throws</b>: Nothing.
1366 static void unlink(const node_ptr & node)
1368 node_ptr x = NodeTraits::get_parent(node);
1370 while(!base_type::is_header(x))
1371 x = NodeTraits::get_parent(x);
1376 //! <b>Requires</b>: header must be the header of a tree.
1378 //! <b>Effects</b>: Rebalances the tree.
1380 //! <b>Throws</b>: Nothing.
1382 //! <b>Complexity</b>: Linear.
1383 static void rebalance(const node_ptr & header)
1385 node_ptr root = NodeTraits::get_parent(header);
1387 rebalance_subtree(root);
1391 //! <b>Requires</b>: old_root is a node of a tree. It shall not be null.
1393 //! <b>Effects</b>: Rebalances the subtree rooted at old_root.
1395 //! <b>Returns</b>: The new root of the subtree.
1397 //! <b>Throws</b>: Nothing.
1399 //! <b>Complexity</b>: Linear.
1400 static node_ptr rebalance_subtree(const node_ptr & old_root)
1403 //"Tree rebalancing in optimal time and space"
1404 //Quentin F. Stout and Bette L. Warren
1406 //To avoid irregularities in the algorithm (old_root can be a
1407 //left or right child or even the root of the tree) just put the
1408 //root as the right child of its parent. Before doing this backup
1409 //information to restore the original relationship after
1410 //the algorithm is applied.
1411 node_ptr super_root = NodeTraits::get_parent(old_root);
1412 BOOST_INTRUSIVE_INVARIANT_ASSERT(super_root);
1415 node_ptr super_root_right_backup = NodeTraits::get_right(super_root);
1416 bool super_root_is_header = NodeTraits::get_parent(super_root) == old_root;
1417 bool old_root_is_right = is_right_child(old_root);
1418 NodeTraits::set_right(super_root, old_root);
1421 subtree_to_vine(super_root, size);
1422 vine_to_subtree(super_root, size);
1423 node_ptr new_root = NodeTraits::get_right(super_root);
1426 if(super_root_is_header){
1427 NodeTraits::set_right(super_root, super_root_right_backup);
1428 NodeTraits::set_parent(super_root, new_root);
1430 else if(old_root_is_right){
1431 NodeTraits::set_right(super_root, new_root);
1434 NodeTraits::set_right(super_root, super_root_right_backup);
1435 NodeTraits::set_left(super_root, new_root);
1440 //! <b>Effects</b>: Asserts the integrity of the container with additional checks provided by the user.
1442 //! <b>Requires</b>: header must be the header of a tree.
1444 //! <b>Complexity</b>: Linear time.
1446 //! <b>Note</b>: The method might not have effect when asserts are turned off (e.g., with NDEBUG).
1447 //! Experimental function, interface might change in future versions.
1448 template<class Checker>
1449 static void check(const const_node_ptr& header, Checker checker, typename Checker::return_type& checker_return)
1451 const_node_ptr root_node_ptr = NodeTraits::get_parent(header);
1452 if (!root_node_ptr){
1453 // check left&right header pointers
1454 BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_left(header) == header);
1455 BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_right(header) == header);
1458 // check parent pointer of root node
1459 BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_parent(root_node_ptr) == header);
1460 // check subtree from root
1461 check_subtree(root_node_ptr, checker, checker_return);
1462 // check left&right header pointers
1463 const_node_ptr p = root_node_ptr;
1464 while (NodeTraits::get_left(p)) { p = NodeTraits::get_left(p); }
1465 BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_left(header) == p);
1467 while (NodeTraits::get_right(p)) { p = NodeTraits::get_right(p); }
1468 BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_right(header) == p);
1474 template<class NodePtrCompare>
1475 static bool transfer_unique
1476 (const node_ptr & header1, NodePtrCompare comp, const node_ptr &header2, const node_ptr & z, data_for_rebalance &info)
1478 insert_commit_data commit_data;
1479 bool const transferable = insert_unique_check(header1, z, comp, commit_data).second;
1481 erase(header2, z, info);
1482 insert_commit(header1, z, commit_data);
1484 return transferable;
1487 template<class NodePtrCompare>
1488 static void transfer_equal
1489 (const node_ptr & header1, NodePtrCompare comp, const node_ptr &header2, const node_ptr & z, data_for_rebalance &info)
1491 insert_commit_data commit_data;
1492 insert_equal_upper_bound_check(header1, z, comp, commit_data);
1493 erase(header2, z, info);
1494 insert_commit(header1, z, commit_data);
1497 static void erase(const node_ptr & header, const node_ptr & z, data_for_rebalance &info)
1501 const node_ptr z_left(NodeTraits::get_left(z));
1502 const node_ptr z_right(NodeTraits::get_right(z));
1505 x = z_right; // x might be null.
1507 else if(!z_right){ // z has exactly one non-null child. y == z.
1508 x = z_left; // x is not null.
1512 // y = find z's successor
1513 y = base_type::minimum(z_right);
1514 x = NodeTraits::get_right(y); // x might be null.
1518 const node_ptr z_parent(NodeTraits::get_parent(z));
1519 const bool z_is_leftchild(NodeTraits::get_left(z_parent) == z);
1521 if(y != z){ //has two children and y is the minimum of z
1522 //y is z's successor and it has a null left child.
1523 //x is the right child of y (it can be null)
1524 //Relink y in place of z and link x with y's old parent
1525 NodeTraits::set_parent(z_left, y);
1526 NodeTraits::set_left(y, z_left);
1528 //Link y with the right tree of z
1529 NodeTraits::set_right(y, z_right);
1530 NodeTraits::set_parent(z_right, y);
1531 //Link x with y's old parent (y must be a left child)
1532 x_parent = NodeTraits::get_parent(y);
1533 BOOST_ASSERT(NodeTraits::get_left(x_parent) == y);
1535 NodeTraits::set_parent(x, x_parent);
1536 //Since y was the successor and not the right child of z, it must be a left child
1537 NodeTraits::set_left(x_parent, x);
1539 else{ //y was the right child of y so no need to fix x's position
1542 NodeTraits::set_parent(y, z_parent);
1543 this_type::set_child(header, y, z_parent, z_is_leftchild);
1545 else { // z has zero or one child, x is one child (it can be null)
1546 //Just link x to z's parent
1547 x_parent = z_parent;
1549 NodeTraits::set_parent(x, z_parent);
1550 this_type::set_child(header, x, z_parent, z_is_leftchild);
1552 //Now update leftmost/rightmost in case z was one of them
1553 if(NodeTraits::get_left(header) == z){
1554 //z_left must be null because z is the leftmost
1555 BOOST_ASSERT(!z_left);
1556 NodeTraits::set_left(header, !z_right ?
1557 z_parent : // makes leftmost == header if z == root
1558 base_type::minimum(z_right));
1560 if(NodeTraits::get_right(header) == z){
1561 //z_right must be null because z is the rightmost
1562 BOOST_ASSERT(!z_right);
1563 NodeTraits::set_right(header, !z_left ?
1564 z_parent : // makes rightmost == header if z == root
1565 base_type::maximum(z_left));
1569 //If z had 0/1 child, y == z and one of its children (and maybe null)
1570 //If z had 2 children, y is the successor of z and x is the right child of y
1573 //If z had 0/1 child, x_parent is the new parent of the old right child of y (z's successor)
1574 //If z had 2 children, x_parent is the new parent of y (z_parent)
1575 BOOST_ASSERT(!x || NodeTraits::get_parent(x) == x_parent);
1576 info.x_parent = x_parent;
1579 //! <b>Requires</b>: node is a node of the tree but it's not the header.
1581 //! <b>Effects</b>: Returns the number of nodes of the subtree.
1583 //! <b>Complexity</b>: Linear time.
1585 //! <b>Throws</b>: Nothing.
1586 static std::size_t subtree_size(const const_node_ptr & subtree)
1588 std::size_t count = 0;
1590 node_ptr n = detail::uncast(subtree);
1591 node_ptr m = NodeTraits::get_left(n);
1594 m = NodeTraits::get_left(n);
1599 node_ptr n_right(NodeTraits::get_right(n));
1602 m = NodeTraits::get_left(n);
1605 m = NodeTraits::get_left(n);
1614 n = NodeTraits::get_parent(n);
1615 }while(NodeTraits::get_left(n) != m);
1622 //! <b>Requires</b>: p is a node of a tree.
1624 //! <b>Effects</b>: Returns true if p is a left child.
1626 //! <b>Complexity</b>: Constant.
1628 //! <b>Throws</b>: Nothing.
1629 BOOST_INTRUSIVE_FORCEINLINE static bool is_left_child(const node_ptr & p)
1630 { return NodeTraits::get_left(NodeTraits::get_parent(p)) == p; }
1632 //! <b>Requires</b>: p is a node of a tree.
1634 //! <b>Effects</b>: Returns true if p is a right child.
1636 //! <b>Complexity</b>: Constant.
1638 //! <b>Throws</b>: Nothing.
1639 BOOST_INTRUSIVE_FORCEINLINE static bool is_right_child(const node_ptr & p)
1640 { return NodeTraits::get_right(NodeTraits::get_parent(p)) == p; }
1642 static void insert_before_check
1643 (const node_ptr &header, const node_ptr & pos
1644 , insert_commit_data &commit_data
1645 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
1646 , std::size_t *pdepth = 0
1651 if(pos != NodeTraits::get_left(header))
1652 prev = base_type::prev_node(pos);
1653 bool link_left = unique(header) || !NodeTraits::get_left(pos);
1654 commit_data.link_left = link_left;
1655 commit_data.node = link_left ? pos : prev;
1657 *pdepth = commit_data.node == header ? 0 : depth(commit_data.node) + 1;
1661 static void push_back_check
1662 (const node_ptr & header, insert_commit_data &commit_data
1663 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
1664 , std::size_t *pdepth = 0
1668 node_ptr prev(NodeTraits::get_right(header));
1670 *pdepth = prev == header ? 0 : depth(prev) + 1;
1672 commit_data.link_left = false;
1673 commit_data.node = prev;
1676 static void push_front_check
1677 (const node_ptr & header, insert_commit_data &commit_data
1678 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
1679 , std::size_t *pdepth = 0
1683 node_ptr pos(NodeTraits::get_left(header));
1685 *pdepth = pos == header ? 0 : depth(pos) + 1;
1687 commit_data.link_left = true;
1688 commit_data.node = pos;
1691 template<class NodePtrCompare>
1692 static void insert_equal_check
1693 (const node_ptr &header, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp
1694 , insert_commit_data &commit_data
1696 , std::size_t *pdepth = 0
1700 if(hint == header || !comp(hint, new_node)){
1701 node_ptr prev(hint);
1702 if(hint == NodeTraits::get_left(header) ||
1703 !comp(new_node, (prev = base_type::prev_node(hint)))){
1704 bool link_left = unique(header) || !NodeTraits::get_left(hint);
1705 commit_data.link_left = link_left;
1706 commit_data.node = link_left ? hint : prev;
1708 *pdepth = commit_data.node == header ? 0 : depth(commit_data.node) + 1;
1712 insert_equal_upper_bound_check(header, new_node, comp, commit_data, pdepth);
1716 insert_equal_lower_bound_check(header, new_node, comp, commit_data, pdepth);
1720 template<class NodePtrCompare>
1721 static void insert_equal_upper_bound_check
1722 (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp, insert_commit_data & commit_data, std::size_t *pdepth = 0)
1724 std::size_t depth = 0;
1726 node_ptr x(NodeTraits::get_parent(y));
1731 x = comp(new_node, x) ?
1732 NodeTraits::get_left(x) : NodeTraits::get_right(x);
1734 if(pdepth) *pdepth = depth;
1735 commit_data.link_left = (y == h) || comp(new_node, y);
1736 commit_data.node = y;
1739 template<class NodePtrCompare>
1740 static void insert_equal_lower_bound_check
1741 (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp, insert_commit_data & commit_data, std::size_t *pdepth = 0)
1743 std::size_t depth = 0;
1745 node_ptr x(NodeTraits::get_parent(y));
1750 x = !comp(x, new_node) ?
1751 NodeTraits::get_left(x) : NodeTraits::get_right(x);
1753 if(pdepth) *pdepth = depth;
1754 commit_data.link_left = (y == h) || !comp(y, new_node);
1755 commit_data.node = y;
1758 static void insert_commit
1759 (const node_ptr & header, const node_ptr & new_node, const insert_commit_data &commit_data)
1761 //Check if commit_data has not been initialized by a insert_unique_check call.
1762 BOOST_INTRUSIVE_INVARIANT_ASSERT(commit_data.node != node_ptr());
1763 node_ptr parent_node(commit_data.node);
1764 if(parent_node == header){
1765 NodeTraits::set_parent(header, new_node);
1766 NodeTraits::set_right(header, new_node);
1767 NodeTraits::set_left(header, new_node);
1769 else if(commit_data.link_left){
1770 NodeTraits::set_left(parent_node, new_node);
1771 if(parent_node == NodeTraits::get_left(header))
1772 NodeTraits::set_left(header, new_node);
1775 NodeTraits::set_right(parent_node, new_node);
1776 if(parent_node == NodeTraits::get_right(header))
1777 NodeTraits::set_right(header, new_node);
1779 NodeTraits::set_parent(new_node, parent_node);
1780 NodeTraits::set_right(new_node, node_ptr());
1781 NodeTraits::set_left(new_node, node_ptr());
1784 //Fix header and own's parent data when replacing x with own, providing own's old data with parent
1785 static void set_child(const node_ptr & header, const node_ptr & new_child, const node_ptr & new_parent, const bool link_left)
1787 if(new_parent == header)
1788 NodeTraits::set_parent(header, new_child);
1790 NodeTraits::set_left(new_parent, new_child);
1792 NodeTraits::set_right(new_parent, new_child);
1795 // rotate p to left (no header and p's parent fixup)
1796 static void rotate_left_no_parent_fix(const node_ptr & p, const node_ptr &p_right)
1798 node_ptr p_right_left(NodeTraits::get_left(p_right));
1799 NodeTraits::set_right(p, p_right_left);
1801 NodeTraits::set_parent(p_right_left, p);
1803 NodeTraits::set_left(p_right, p);
1804 NodeTraits::set_parent(p, p_right);
1807 // rotate p to left (with header and p's parent fixup)
1808 static void rotate_left(const node_ptr & p, const node_ptr & p_right, const node_ptr & p_parent, const node_ptr & header)
1810 const bool p_was_left(NodeTraits::get_left(p_parent) == p);
1811 rotate_left_no_parent_fix(p, p_right);
1812 NodeTraits::set_parent(p_right, p_parent);
1813 set_child(header, p_right, p_parent, p_was_left);
1816 // rotate p to right (no header and p's parent fixup)
1817 static void rotate_right_no_parent_fix(const node_ptr & p, const node_ptr &p_left)
1819 node_ptr p_left_right(NodeTraits::get_right(p_left));
1820 NodeTraits::set_left(p, p_left_right);
1822 NodeTraits::set_parent(p_left_right, p);
1824 NodeTraits::set_right(p_left, p);
1825 NodeTraits::set_parent(p, p_left);
1828 // rotate p to right (with header and p's parent fixup)
1829 static void rotate_right(const node_ptr & p, const node_ptr & p_left, const node_ptr & p_parent, const node_ptr & header)
1831 const bool p_was_left(NodeTraits::get_left(p_parent) == p);
1832 rotate_right_no_parent_fix(p, p_left);
1833 NodeTraits::set_parent(p_left, p_parent);
1834 set_child(header, p_left, p_parent, p_was_left);
1839 static void subtree_to_vine(node_ptr vine_tail, std::size_t &size)
1841 //Inspired by LibAVL:
1842 //It uses a clever optimization for trees with parent pointers.
1843 //No parent pointer is updated when transforming a tree to a vine as
1844 //most of them will be overriten during compression rotations.
1845 //A final pass must be made after the rebalancing to updated those
1846 //pointers not updated by tree_to_vine + compression calls
1847 std::size_t len = 0;
1848 node_ptr remainder = NodeTraits::get_right(vine_tail);
1850 node_ptr tempptr = NodeTraits::get_left(remainder);
1851 if(!tempptr){ //move vine-tail down one
1852 vine_tail = remainder;
1853 remainder = NodeTraits::get_right(remainder);
1857 NodeTraits::set_left(remainder, NodeTraits::get_right(tempptr));
1858 NodeTraits::set_right(tempptr, remainder);
1859 remainder = tempptr;
1860 NodeTraits::set_right(vine_tail, tempptr);
1866 static void compress_subtree(node_ptr scanner, std::size_t count)
1868 while(count--){ //compress "count" spine nodes in the tree with pseudo-root scanner
1869 node_ptr child = NodeTraits::get_right(scanner);
1870 node_ptr child_right = NodeTraits::get_right(child);
1871 NodeTraits::set_right(scanner, child_right);
1872 //Avoid setting the parent of child_right
1873 scanner = child_right;
1874 node_ptr scanner_left = NodeTraits::get_left(scanner);
1875 NodeTraits::set_right(child, scanner_left);
1877 NodeTraits::set_parent(scanner_left, child);
1878 NodeTraits::set_left(scanner, child);
1879 NodeTraits::set_parent(child, scanner);
1883 static void vine_to_subtree(const node_ptr & super_root, std::size_t count)
1885 const std::size_t one_szt = 1u;
1886 std::size_t leaf_nodes = count + one_szt - std::size_t(one_szt << detail::floor_log2(count + one_szt));
1887 compress_subtree(super_root, leaf_nodes); //create deepest leaves
1888 std::size_t vine_nodes = count - leaf_nodes;
1889 while(vine_nodes > 1){
1891 compress_subtree(super_root, vine_nodes);
1894 //Update parents of nodes still in the in the original vine line
1895 //as those have not been updated by subtree_to_vine or compress_subtree
1896 for ( node_ptr q = super_root, p = NodeTraits::get_right(super_root)
1898 ; q = p, p = NodeTraits::get_right(p)){
1899 NodeTraits::set_parent(p, q);
1903 //! <b>Requires</b>: "n" must be a node inserted in a tree.
1905 //! <b>Effects</b>: Returns a pointer to the header node of the tree.
1907 //! <b>Complexity</b>: Logarithmic.
1909 //! <b>Throws</b>: Nothing.
1910 static node_ptr get_root(const node_ptr & node)
1912 BOOST_INTRUSIVE_INVARIANT_ASSERT((!inited(node)));
1913 node_ptr x = NodeTraits::get_parent(node);
1915 while(!base_type::is_header(x)){
1916 x = NodeTraits::get_parent(x);
1925 template <class Cloner, class Disposer>
1926 static node_ptr clone_subtree
1927 (const const_node_ptr &source_parent, const node_ptr &target_parent
1928 , Cloner cloner, Disposer disposer
1929 , node_ptr &leftmost_out, node_ptr &rightmost_out
1932 node_ptr target_sub_root = target_parent;
1933 node_ptr source_root = NodeTraits::get_parent(source_parent);
1935 leftmost_out = rightmost_out = source_root;
1938 //We'll calculate leftmost and rightmost nodes while iterating
1939 node_ptr current = source_root;
1940 node_ptr insertion_point = target_sub_root = cloner(current);
1942 //We'll calculate leftmost and rightmost nodes while iterating
1943 node_ptr leftmost = target_sub_root;
1944 node_ptr rightmost = target_sub_root;
1946 //First set the subroot
1947 NodeTraits::set_left(target_sub_root, node_ptr());
1948 NodeTraits::set_right(target_sub_root, node_ptr());
1949 NodeTraits::set_parent(target_sub_root, target_parent);
1951 dispose_subtree_disposer<Disposer> rollback(disposer, target_sub_root);
1953 //First clone left nodes
1954 if( NodeTraits::get_left(current) &&
1955 !NodeTraits::get_left(insertion_point)) {
1956 current = NodeTraits::get_left(current);
1957 node_ptr temp = insertion_point;
1958 //Clone and mark as leaf
1959 insertion_point = cloner(current);
1960 NodeTraits::set_left (insertion_point, node_ptr());
1961 NodeTraits::set_right (insertion_point, node_ptr());
1963 NodeTraits::set_parent(insertion_point, temp);
1964 NodeTraits::set_left (temp, insertion_point);
1966 if(rightmost == target_sub_root)
1967 leftmost = insertion_point;
1969 //Then clone right nodes
1970 else if( NodeTraits::get_right(current) &&
1971 !NodeTraits::get_right(insertion_point)){
1972 current = NodeTraits::get_right(current);
1973 node_ptr temp = insertion_point;
1974 //Clone and mark as leaf
1975 insertion_point = cloner(current);
1976 NodeTraits::set_left (insertion_point, node_ptr());
1977 NodeTraits::set_right (insertion_point, node_ptr());
1979 NodeTraits::set_parent(insertion_point, temp);
1980 NodeTraits::set_right (temp, insertion_point);
1982 rightmost = insertion_point;
1985 else if(current == source_root){
1989 //Branch completed, go up searching more nodes to clone
1990 current = NodeTraits::get_parent(current);
1991 insertion_point = NodeTraits::get_parent(insertion_point);
1995 leftmost_out = leftmost;
1996 rightmost_out = rightmost;
1998 return target_sub_root;
2001 template<class Disposer>
2002 static void dispose_subtree(node_ptr x, Disposer disposer)
2005 node_ptr save(NodeTraits::get_left(x));
2008 NodeTraits::set_left(x, NodeTraits::get_right(save));
2009 NodeTraits::set_right(save, x);
2012 save = NodeTraits::get_right(x);
2020 template<class KeyType, class KeyNodePtrCompare>
2021 static node_ptr lower_bound_loop
2022 (node_ptr x, node_ptr y, const KeyType &key, KeyNodePtrCompare comp)
2026 x = NodeTraits::get_right(x);
2030 x = NodeTraits::get_left(x);
2036 template<class KeyType, class KeyNodePtrCompare>
2037 static node_ptr upper_bound_loop
2038 (node_ptr x, node_ptr y, const KeyType &key, KeyNodePtrCompare comp)
2043 x = NodeTraits::get_left(x);
2046 x = NodeTraits::get_right(x);
2052 template<class Checker>
2053 static void check_subtree(const const_node_ptr& node, Checker checker, typename Checker::return_type& check_return)
2055 const_node_ptr left = NodeTraits::get_left(node);
2056 const_node_ptr right = NodeTraits::get_right(node);
2057 typename Checker::return_type check_return_left;
2058 typename Checker::return_type check_return_right;
2061 BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_parent(left) == node);
2062 check_subtree(left, checker, check_return_left);
2066 BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_parent(right) == node);
2067 check_subtree(right, checker, check_return_right);
2069 checker(node, check_return_left, check_return_right, check_return);
2075 template<class NodeTraits>
2076 struct get_algo<BsTreeAlgorithms, NodeTraits>
2078 typedef bstree_algorithms<NodeTraits> type;
2081 template <class ValueTraits, class NodePtrCompare, class ExtraChecker>
2082 struct get_node_checker<BsTreeAlgorithms, ValueTraits, NodePtrCompare, ExtraChecker>
2084 typedef detail::bstree_node_checker<ValueTraits, NodePtrCompare, ExtraChecker> type;
2089 } //namespace intrusive
2092 #include <boost/intrusive/detail/config_end.hpp>
2094 #endif //BOOST_INTRUSIVE_BSTREE_ALGORITHMS_HPP