Chris@16: ///////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@101: // (C) Copyright Ion Gaztanaga 2007-2014 Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. Chris@16: // (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org/libs/intrusive for documentation. Chris@16: // Chris@16: ///////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: #ifndef BOOST_INTRUSIVE_BSTREE_ALGORITHMS_HPP Chris@16: #define BOOST_INTRUSIVE_BSTREE_ALGORITHMS_HPP Chris@16: Chris@101: #include Chris@16: #include Chris@101: #include Chris@101: #include Chris@16: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: Chris@101: #include Chris@101: Chris@101: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@101: # pragma once Chris@101: #endif Chris@16: Chris@16: namespace boost { Chris@16: namespace intrusive { Chris@16: Chris@16: /// @cond Chris@16: Chris@16: //! This type is the information that will be filled by insert_unique_check Chris@16: template Chris@16: struct insert_commit_data_t Chris@16: { Chris@16: insert_commit_data_t() Chris@16: : link_left(false) Chris@16: , node() Chris@16: {} Chris@16: bool link_left; Chris@16: NodePtr node; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct data_for_rebalance_t Chris@16: { Chris@16: NodePtr x; Chris@16: NodePtr x_parent; Chris@16: NodePtr y; Chris@16: }; Chris@16: Chris@101: namespace detail { Chris@101: Chris@101: template Chris@101: struct bstree_node_checker Chris@101: : public ExtraChecker Chris@101: { Chris@101: typedef ExtraChecker base_checker_t; Chris@101: typedef ValueTraits value_traits; Chris@101: typedef typename value_traits::node_traits node_traits; Chris@101: typedef typename node_traits::const_node_ptr const_node_ptr; Chris@101: Chris@101: struct return_type Chris@101: : public base_checker_t::return_type Chris@101: { Chris@101: return_type() : min_key_node_ptr(const_node_ptr()), max_key_node_ptr(const_node_ptr()), node_count(0) {} Chris@101: Chris@101: const_node_ptr min_key_node_ptr; Chris@101: const_node_ptr max_key_node_ptr; Chris@101: size_t node_count; Chris@101: }; Chris@101: Chris@101: bstree_node_checker(const NodePtrCompare& comp, ExtraChecker extra_checker) Chris@101: : base_checker_t(extra_checker), comp_(comp) Chris@101: {} Chris@101: Chris@101: void operator () (const const_node_ptr& p, Chris@101: const return_type& check_return_left, const return_type& check_return_right, Chris@101: return_type& check_return) Chris@101: { Chris@101: if (check_return_left.max_key_node_ptr) Chris@101: BOOST_INTRUSIVE_INVARIANT_ASSERT(!comp_(p, check_return_left.max_key_node_ptr)); Chris@101: if (check_return_right.min_key_node_ptr) Chris@101: BOOST_INTRUSIVE_INVARIANT_ASSERT(!comp_(check_return_right.min_key_node_ptr, p)); Chris@101: check_return.min_key_node_ptr = node_traits::get_left(p)? check_return_left.min_key_node_ptr : p; Chris@101: check_return.max_key_node_ptr = node_traits::get_right(p)? check_return_right.max_key_node_ptr : p; Chris@101: check_return.node_count = check_return_left.node_count + check_return_right.node_count + 1; Chris@101: base_checker_t::operator()(p, check_return_left, check_return_right, check_return); Chris@101: } Chris@101: Chris@101: const NodePtrCompare comp_; Chris@101: }; Chris@101: Chris@101: } // namespace detail Chris@101: Chris@16: /// @endcond Chris@16: Chris@16: Chris@16: Chris@16: //! This is an implementation of a binary search tree. Chris@16: //! A node in the search tree has references to its children and its parent. This Chris@16: //! is to allow traversal of the whole tree from a given node making the Chris@16: //! implementation of iterator a pointer to a node. Chris@16: //! At the top of the tree a node is used specially. This node's parent pointer Chris@16: //! is pointing to the root of the tree. Its left pointer points to the Chris@16: //! leftmost node in the tree and the right pointer to the rightmost one. Chris@16: //! This node is used to represent the end-iterator. Chris@16: //! Chris@16: //! +---------+ Chris@16: //! header------------------------------>| | Chris@16: //! | | Chris@16: //! +----------(left)--------| |--------(right)---------+ Chris@16: //! | +---------+ | Chris@16: //! | | | Chris@16: //! | | (parent) | Chris@16: //! | | | Chris@16: //! | | | Chris@16: //! | +---------+ | Chris@16: //! root of tree ..|......................> | | | Chris@16: //! | | D | | Chris@16: //! | | | | Chris@16: //! | +-------+---------+-------+ | Chris@16: //! | | | | Chris@16: //! | | | | Chris@16: //! | | | | Chris@16: //! | | | | Chris@16: //! | | | | Chris@16: //! | +---------+ +---------+ | Chris@16: //! | | | | | | Chris@16: //! | | B | | F | | Chris@16: //! | | | | | | Chris@16: //! | +--+---------+--+ +--+---------+--+ | Chris@16: //! | | | | | | Chris@16: //! | | | | | | Chris@16: //! | | | | | | Chris@16: //! | +---+-----+ +-----+---+ +---+-----+ +-----+---+ | Chris@16: //! +-->| | | | | | | |<--+ Chris@16: //! | A | | C | | E | | G | Chris@16: //! | | | | | | | | Chris@16: //! +---------+ +---------+ +---------+ +---------+ Chris@16: //! Chris@16: //! bstree_algorithms is configured with a NodeTraits class, which encapsulates the Chris@16: //! information about the node to be manipulated. NodeTraits must support the Chris@16: //! following interface: Chris@16: //! Chris@16: //! Typedefs: Chris@16: //! Chris@16: //! node: The type of the node that forms the binary search tree Chris@16: //! Chris@16: //! node_ptr: A pointer to a node Chris@16: //! Chris@16: //! const_node_ptr: A pointer to a const node Chris@16: //! Chris@16: //! Static functions: Chris@16: //! Chris@16: //! static node_ptr get_parent(const_node_ptr n); Chris@16: //! Chris@16: //! static void set_parent(node_ptr n, node_ptr parent); Chris@16: //! Chris@16: //! static node_ptr get_left(const_node_ptr n); Chris@16: //! Chris@16: //! static void set_left(node_ptr n, node_ptr left); Chris@16: //! Chris@16: //! static node_ptr get_right(const_node_ptr n); Chris@16: //! Chris@16: //! static void set_right(node_ptr n, node_ptr right); Chris@16: template Chris@101: class bstree_algorithms : public bstree_algorithms_base Chris@16: { Chris@16: public: Chris@16: typedef typename NodeTraits::node node; Chris@16: typedef NodeTraits node_traits; Chris@16: typedef typename NodeTraits::node_ptr node_ptr; Chris@16: typedef typename NodeTraits::const_node_ptr const_node_ptr; Chris@16: typedef insert_commit_data_t insert_commit_data; Chris@16: typedef data_for_rebalance_t data_for_rebalance; Chris@16: Chris@16: /// @cond Chris@101: typedef bstree_algorithms this_type; Chris@101: typedef bstree_algorithms_base base_type; Chris@16: private: Chris@16: template Chris@16: struct dispose_subtree_disposer Chris@16: { Chris@16: dispose_subtree_disposer(Disposer &disp, const node_ptr & subtree) Chris@16: : disposer_(&disp), subtree_(subtree) Chris@16: {} Chris@16: Chris@16: void release() Chris@16: { disposer_ = 0; } Chris@16: Chris@16: ~dispose_subtree_disposer() Chris@16: { Chris@16: if(disposer_){ Chris@16: dispose_subtree(subtree_, *disposer_); Chris@16: } Chris@16: } Chris@16: Disposer *disposer_; Chris@16: const node_ptr subtree_; Chris@16: }; Chris@16: Chris@16: /// @endcond Chris@16: Chris@16: public: Chris@16: //! Requires: 'header' is the header node of a tree. Chris@16: //! Chris@16: //! Effects: Returns the first node of the tree, the header if the tree is empty. Chris@16: //! Chris@16: //! Complexity: Constant time. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: static node_ptr begin_node(const const_node_ptr & header) Chris@16: { return node_traits::get_left(header); } Chris@16: Chris@16: //! Requires: 'header' is the header node of a tree. Chris@16: //! Chris@16: //! Effects: Returns the header of the tree. Chris@16: //! Chris@16: //! Complexity: Constant time. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: static node_ptr end_node(const const_node_ptr & header) Chris@16: { return detail::uncast(header); } Chris@16: Chris@101: //! Requires: 'header' is the header node of a tree. Chris@101: //! Chris@101: //! Effects: Returns the root of the tree if any, header otherwise Chris@101: //! Chris@101: //! Complexity: Constant time. Chris@101: //! Chris@101: //! Throws: Nothing. Chris@101: static node_ptr root_node(const const_node_ptr & header) Chris@101: { Chris@101: node_ptr p = node_traits::get_parent(header); Chris@101: return p ? p : detail::uncast(header); Chris@101: } Chris@101: Chris@101: //! Requires: 'node' is a node of the tree or a node initialized Chris@16: //! by init(...) or init_node. Chris@16: //! Chris@16: //! Effects: Returns true if the node is initialized by init() or init_node(). Chris@16: //! Chris@16: //! Complexity: Constant time. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: static bool unique(const const_node_ptr & node) Chris@16: { return !NodeTraits::get_parent(node); } Chris@16: Chris@101: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: //! Requires: 'node' is a node of the tree or a header node. Chris@16: //! Chris@16: //! Effects: Returns the header of the tree. Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@101: static node_ptr get_header(const const_node_ptr & node); Chris@101: #endif Chris@16: Chris@16: //! Requires: node1 and node2 can't be header nodes Chris@16: //! of two trees. Chris@16: //! Chris@16: //! Effects: Swaps two nodes. After the function node1 will be inserted Chris@16: //! in the position node2 before the function. node2 will be inserted in the Chris@16: //! position node1 had before the function. Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: This function will break container ordering invariants if Chris@16: //! node1 and node2 are not equivalent according to the ordering rules. Chris@16: //! Chris@16: //!Experimental function Chris@16: static void swap_nodes(const node_ptr & node1, const node_ptr & node2) Chris@16: { Chris@16: if(node1 == node2) Chris@16: return; Chris@16: Chris@101: node_ptr header1(base_type::get_header(node1)), header2(base_type::get_header(node2)); Chris@16: swap_nodes(node1, header1, node2, header2); Chris@16: } Chris@16: Chris@16: //! Requires: node1 and node2 can't be header nodes Chris@16: //! of two trees with header header1 and header2. Chris@16: //! Chris@16: //! Effects: Swaps two nodes. After the function node1 will be inserted Chris@16: //! in the position node2 before the function. node2 will be inserted in the Chris@16: //! position node1 had before the function. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: This function will break container ordering invariants if Chris@16: //! node1 and node2 are not equivalent according to the ordering rules. Chris@16: //! Chris@16: //!Experimental function Chris@16: static void swap_nodes(const node_ptr & node1, const node_ptr & header1, const node_ptr & node2, const node_ptr & header2) Chris@16: { Chris@16: if(node1 == node2) Chris@16: return; Chris@16: Chris@16: //node1 and node2 must not be header nodes Chris@16: //BOOST_INTRUSIVE_INVARIANT_ASSERT((header1 != node1 && header2 != node2)); Chris@16: if(header1 != header2){ Chris@16: //Update header1 if necessary Chris@16: if(node1 == NodeTraits::get_left(header1)){ Chris@16: NodeTraits::set_left(header1, node2); Chris@16: } Chris@16: Chris@16: if(node1 == NodeTraits::get_right(header1)){ Chris@16: NodeTraits::set_right(header1, node2); Chris@16: } Chris@16: Chris@16: if(node1 == NodeTraits::get_parent(header1)){ Chris@16: NodeTraits::set_parent(header1, node2); Chris@16: } Chris@16: Chris@16: //Update header2 if necessary Chris@16: if(node2 == NodeTraits::get_left(header2)){ Chris@16: NodeTraits::set_left(header2, node1); Chris@16: } Chris@16: Chris@16: if(node2 == NodeTraits::get_right(header2)){ Chris@16: NodeTraits::set_right(header2, node1); Chris@16: } Chris@16: Chris@16: if(node2 == NodeTraits::get_parent(header2)){ Chris@16: NodeTraits::set_parent(header2, node1); Chris@16: } Chris@16: } Chris@16: else{ Chris@16: //If both nodes are from the same tree Chris@16: //Update header if necessary Chris@16: if(node1 == NodeTraits::get_left(header1)){ Chris@16: NodeTraits::set_left(header1, node2); Chris@16: } Chris@16: else if(node2 == NodeTraits::get_left(header2)){ Chris@16: NodeTraits::set_left(header2, node1); Chris@16: } Chris@16: Chris@16: if(node1 == NodeTraits::get_right(header1)){ Chris@16: NodeTraits::set_right(header1, node2); Chris@16: } Chris@16: else if(node2 == NodeTraits::get_right(header2)){ Chris@16: NodeTraits::set_right(header2, node1); Chris@16: } Chris@16: Chris@16: if(node1 == NodeTraits::get_parent(header1)){ Chris@16: NodeTraits::set_parent(header1, node2); Chris@16: } Chris@16: else if(node2 == NodeTraits::get_parent(header2)){ Chris@16: NodeTraits::set_parent(header2, node1); Chris@16: } Chris@16: Chris@16: //Adjust data in nodes to be swapped Chris@16: //so that final link swap works as expected Chris@16: if(node1 == NodeTraits::get_parent(node2)){ Chris@16: NodeTraits::set_parent(node2, node2); Chris@16: Chris@16: if(node2 == NodeTraits::get_right(node1)){ Chris@16: NodeTraits::set_right(node1, node1); Chris@16: } Chris@16: else{ Chris@16: NodeTraits::set_left(node1, node1); Chris@16: } Chris@16: } Chris@16: else if(node2 == NodeTraits::get_parent(node1)){ Chris@16: NodeTraits::set_parent(node1, node1); Chris@16: Chris@16: if(node1 == NodeTraits::get_right(node2)){ Chris@16: NodeTraits::set_right(node2, node2); Chris@16: } Chris@16: else{ Chris@16: NodeTraits::set_left(node2, node2); Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: //Now swap all the links Chris@16: node_ptr temp; Chris@16: //swap left link Chris@16: temp = NodeTraits::get_left(node1); Chris@16: NodeTraits::set_left(node1, NodeTraits::get_left(node2)); Chris@16: NodeTraits::set_left(node2, temp); Chris@16: //swap right link Chris@16: temp = NodeTraits::get_right(node1); Chris@16: NodeTraits::set_right(node1, NodeTraits::get_right(node2)); Chris@16: NodeTraits::set_right(node2, temp); Chris@16: //swap parent link Chris@16: temp = NodeTraits::get_parent(node1); Chris@16: NodeTraits::set_parent(node1, NodeTraits::get_parent(node2)); Chris@16: NodeTraits::set_parent(node2, temp); Chris@16: Chris@16: //Now adjust adjacent nodes for newly inserted node 1 Chris@16: if((temp = NodeTraits::get_left(node1))){ Chris@16: NodeTraits::set_parent(temp, node1); Chris@16: } Chris@16: if((temp = NodeTraits::get_right(node1))){ Chris@16: NodeTraits::set_parent(temp, node1); Chris@16: } Chris@16: if((temp = NodeTraits::get_parent(node1)) && Chris@16: //The header has been already updated so avoid it Chris@16: temp != header2){ Chris@16: if(NodeTraits::get_left(temp) == node2){ Chris@16: NodeTraits::set_left(temp, node1); Chris@16: } Chris@16: if(NodeTraits::get_right(temp) == node2){ Chris@16: NodeTraits::set_right(temp, node1); Chris@16: } Chris@16: } Chris@16: //Now adjust adjacent nodes for newly inserted node 2 Chris@16: if((temp = NodeTraits::get_left(node2))){ Chris@16: NodeTraits::set_parent(temp, node2); Chris@16: } Chris@16: if((temp = NodeTraits::get_right(node2))){ Chris@16: NodeTraits::set_parent(temp, node2); Chris@16: } Chris@16: if((temp = NodeTraits::get_parent(node2)) && Chris@16: //The header has been already updated so avoid it Chris@16: temp != header1){ Chris@16: if(NodeTraits::get_left(temp) == node1){ Chris@16: NodeTraits::set_left(temp, node2); Chris@16: } Chris@16: if(NodeTraits::get_right(temp) == node1){ Chris@16: NodeTraits::set_right(temp, node2); Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: //! Requires: node_to_be_replaced must be inserted in a tree Chris@16: //! and new_node must not be inserted in a tree. Chris@16: //! Chris@16: //! Effects: Replaces node_to_be_replaced in its position in the Chris@16: //! tree with new_node. The tree does not need to be rebalanced Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: This function will break container ordering invariants if Chris@16: //! new_node is not equivalent to node_to_be_replaced according to the Chris@16: //! ordering rules. This function is faster than erasing and inserting Chris@16: //! the node, since no rebalancing and comparison is needed. Experimental function Chris@16: static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & new_node) Chris@16: { Chris@16: if(node_to_be_replaced == new_node) Chris@16: return; Chris@101: replace_node(node_to_be_replaced, base_type::get_header(node_to_be_replaced), new_node); Chris@16: } Chris@16: Chris@16: //! Requires: node_to_be_replaced must be inserted in a tree Chris@16: //! with header "header" and new_node must not be inserted in a tree. Chris@16: //! Chris@16: //! Effects: Replaces node_to_be_replaced in its position in the Chris@16: //! tree with new_node. The tree does not need to be rebalanced Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: This function will break container ordering invariants if Chris@16: //! new_node is not equivalent to node_to_be_replaced according to the Chris@16: //! ordering rules. This function is faster than erasing and inserting Chris@16: //! the node, since no rebalancing or comparison is needed. Experimental function Chris@16: static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & header, const node_ptr & new_node) Chris@16: { Chris@16: if(node_to_be_replaced == new_node) Chris@16: return; Chris@16: Chris@16: //Update header if necessary Chris@16: if(node_to_be_replaced == NodeTraits::get_left(header)){ Chris@16: NodeTraits::set_left(header, new_node); Chris@16: } Chris@16: Chris@16: if(node_to_be_replaced == NodeTraits::get_right(header)){ Chris@16: NodeTraits::set_right(header, new_node); Chris@16: } Chris@16: Chris@16: if(node_to_be_replaced == NodeTraits::get_parent(header)){ Chris@16: NodeTraits::set_parent(header, new_node); Chris@16: } Chris@16: Chris@16: //Now set data from the original node Chris@16: node_ptr temp; Chris@16: NodeTraits::set_left(new_node, NodeTraits::get_left(node_to_be_replaced)); Chris@16: NodeTraits::set_right(new_node, NodeTraits::get_right(node_to_be_replaced)); Chris@16: NodeTraits::set_parent(new_node, NodeTraits::get_parent(node_to_be_replaced)); Chris@16: Chris@16: //Now adjust adjacent nodes for newly inserted node Chris@16: if((temp = NodeTraits::get_left(new_node))){ Chris@16: NodeTraits::set_parent(temp, new_node); Chris@16: } Chris@16: if((temp = NodeTraits::get_right(new_node))){ Chris@16: NodeTraits::set_parent(temp, new_node); Chris@16: } Chris@16: if((temp = NodeTraits::get_parent(new_node)) && Chris@16: //The header has been already updated so avoid it Chris@16: temp != header){ Chris@16: if(NodeTraits::get_left(temp) == node_to_be_replaced){ Chris@16: NodeTraits::set_left(temp, new_node); Chris@16: } Chris@16: if(NodeTraits::get_right(temp) == node_to_be_replaced){ Chris@16: NodeTraits::set_right(temp, new_node); Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@101: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: //! Requires: 'node' is a node from the tree except the header. Chris@16: //! Chris@16: //! Effects: Returns the next node of the tree. Chris@16: //! Chris@16: //! Complexity: Average constant time. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@101: static node_ptr next_node(const node_ptr & node); Chris@16: Chris@16: //! Requires: 'node' is a node from the tree except the leftmost node. Chris@16: //! Chris@16: //! Effects: Returns the previous node of the tree. Chris@16: //! Chris@16: //! Complexity: Average constant time. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@101: static node_ptr prev_node(const node_ptr & node); Chris@16: Chris@16: //! Requires: 'node' is a node of a tree but not the header. Chris@16: //! Chris@16: //! Effects: Returns the minimum node of the subtree starting at p. Chris@16: //! Chris@16: //! Complexity: Logarithmic to the size of the subtree. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@101: static node_ptr minimum(node_ptr node); Chris@16: Chris@16: //! Requires: 'node' is a node of a tree but not the header. Chris@16: //! Chris@16: //! Effects: Returns the maximum node of the subtree starting at p. Chris@16: //! Chris@16: //! Complexity: Logarithmic to the size of the subtree. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@101: static node_ptr maximum(node_ptr node); Chris@101: #endif Chris@16: Chris@16: //! Requires: 'node' must not be part of any tree. Chris@16: //! Chris@16: //! Effects: After the function unique(node) == true. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Nodes: If node is inserted in a tree, this function corrupts the tree. Chris@16: static void init(const node_ptr & node) Chris@16: { Chris@16: NodeTraits::set_parent(node, node_ptr()); Chris@16: NodeTraits::set_left(node, node_ptr()); Chris@16: NodeTraits::set_right(node, node_ptr()); Chris@16: }; Chris@16: Chris@16: //! Effects: Returns true if node is in the same state as if called init(node) Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: static bool inited(const const_node_ptr & node) Chris@16: { Chris@16: return !NodeTraits::get_parent(node) && Chris@16: !NodeTraits::get_left(node) && Chris@16: !NodeTraits::get_right(node) ; Chris@16: }; Chris@16: Chris@16: //! Requires: node must not be part of any tree. Chris@16: //! Chris@16: //! Effects: Initializes the header to represent an empty tree. Chris@16: //! unique(header) == true. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Nodes: If node is inserted in a tree, this function corrupts the tree. Chris@16: static void init_header(const node_ptr & header) Chris@16: { Chris@16: NodeTraits::set_parent(header, node_ptr()); Chris@16: NodeTraits::set_left(header, header); Chris@16: NodeTraits::set_right(header, header); Chris@16: } Chris@16: Chris@16: //! Requires: "disposer" must be an object function Chris@16: //! taking a node_ptr parameter and shouldn't throw. Chris@16: //! Chris@16: //! Effects: Empties the target tree calling Chris@16: //! void disposer::operator()(const node_ptr &) for every node of the tree Chris@16: //! except the header. Chris@16: //! Chris@16: //! Complexity: Linear to the number of element of the source tree plus the. Chris@16: //! number of elements of tree target tree when calling this function. Chris@16: //! Chris@16: //! Throws: If cloner functor throws. If this happens target nodes are disposed. Chris@16: template Chris@16: static void clear_and_dispose(const node_ptr & header, Disposer disposer) Chris@16: { Chris@16: node_ptr source_root = NodeTraits::get_parent(header); Chris@16: if(!source_root) Chris@16: return; Chris@16: dispose_subtree(source_root, disposer); Chris@16: init_header(header); Chris@16: } Chris@16: Chris@16: //! Requires: header is the header of a tree. Chris@16: //! Chris@16: //! Effects: Unlinks the leftmost node from the tree, and Chris@16: //! updates the header link to the new leftmost node. Chris@16: //! Chris@16: //! Complexity: Average complexity is constant time. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Notes: This function breaks the tree and the tree can Chris@16: //! only be used for more unlink_leftmost_without_rebalance calls. Chris@16: //! This function is normally used to achieve a step by step Chris@16: //! controlled destruction of the tree. Chris@16: static node_ptr unlink_leftmost_without_rebalance(const node_ptr & header) Chris@16: { Chris@16: node_ptr leftmost = NodeTraits::get_left(header); Chris@16: if (leftmost == header) Chris@16: return node_ptr(); Chris@16: node_ptr leftmost_parent(NodeTraits::get_parent(leftmost)); Chris@16: node_ptr leftmost_right (NodeTraits::get_right(leftmost)); Chris@16: bool is_root = leftmost_parent == header; Chris@16: Chris@16: if (leftmost_right){ Chris@16: NodeTraits::set_parent(leftmost_right, leftmost_parent); Chris@101: NodeTraits::set_left(header, base_type::minimum(leftmost_right)); Chris@16: Chris@16: if (is_root) Chris@16: NodeTraits::set_parent(header, leftmost_right); Chris@16: else Chris@16: NodeTraits::set_left(NodeTraits::get_parent(header), leftmost_right); Chris@16: } Chris@16: else if (is_root){ Chris@16: NodeTraits::set_parent(header, node_ptr()); Chris@16: NodeTraits::set_left(header, header); Chris@16: NodeTraits::set_right(header, header); Chris@16: } Chris@16: else{ Chris@16: NodeTraits::set_left(leftmost_parent, node_ptr()); Chris@16: NodeTraits::set_left(header, leftmost_parent); Chris@16: } Chris@16: return leftmost; Chris@16: } Chris@16: Chris@16: //! Requires: node is a node of the tree but it's not the header. Chris@16: //! Chris@16: //! Effects: Returns the number of nodes of the subtree. Chris@16: //! Chris@16: //! Complexity: Linear time. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: static std::size_t size(const const_node_ptr & header) Chris@16: { Chris@16: node_ptr beg(begin_node(header)); Chris@16: node_ptr end(end_node(header)); Chris@16: std::size_t i = 0; Chris@101: for(;beg != end; beg = base_type::next_node(beg)) ++i; Chris@16: return i; Chris@16: } Chris@16: Chris@16: //! Requires: header1 and header2 must be the header nodes Chris@16: //! of two trees. Chris@16: //! Chris@16: //! Effects: Swaps two trees. After the function header1 will contain Chris@16: //! links to the second tree and header2 will have links to the first tree. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: static void swap_tree(const node_ptr & header1, const node_ptr & header2) Chris@16: { Chris@16: if(header1 == header2) Chris@16: return; Chris@16: Chris@16: node_ptr tmp; Chris@16: Chris@16: //Parent swap Chris@16: tmp = NodeTraits::get_parent(header1); Chris@16: NodeTraits::set_parent(header1, NodeTraits::get_parent(header2)); Chris@16: NodeTraits::set_parent(header2, tmp); Chris@16: //Left swap Chris@16: tmp = NodeTraits::get_left(header1); Chris@16: NodeTraits::set_left(header1, NodeTraits::get_left(header2)); Chris@16: NodeTraits::set_left(header2, tmp); Chris@16: //Right swap Chris@16: tmp = NodeTraits::get_right(header1); Chris@16: NodeTraits::set_right(header1, NodeTraits::get_right(header2)); Chris@16: NodeTraits::set_right(header2, tmp); Chris@16: Chris@16: //Now test parent Chris@16: node_ptr h1_parent(NodeTraits::get_parent(header1)); Chris@16: if(h1_parent){ Chris@16: NodeTraits::set_parent(h1_parent, header1); Chris@16: } Chris@16: else{ Chris@16: NodeTraits::set_left(header1, header1); Chris@16: NodeTraits::set_right(header1, header1); Chris@16: } Chris@16: Chris@16: node_ptr h2_parent(NodeTraits::get_parent(header2)); Chris@16: if(h2_parent){ Chris@16: NodeTraits::set_parent(h2_parent, header2); Chris@16: } Chris@16: else{ Chris@16: NodeTraits::set_left(header2, header2); Chris@16: NodeTraits::set_right(header2, header2); Chris@16: } Chris@16: } Chris@16: Chris@101: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: //! Requires: p is a node of a tree. Chris@16: //! Chris@16: //! Effects: Returns true if p is the header of the tree. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@101: static bool is_header(const const_node_ptr & p); Chris@101: #endif Chris@16: Chris@16: //! Requires: "header" must be the header node of a tree. Chris@16: //! KeyNodePtrCompare is a function object that induces a strict weak Chris@16: //! ordering compatible with the strict weak ordering used to create the Chris@16: //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs. Chris@16: //! Chris@101: //! Effects: Returns a node_ptr to the first element that is equivalent to Chris@16: //! "key" according to "comp" or "header" if that element does not exist. Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@16: //! Throws: If "comp" throws. Chris@16: template Chris@16: static node_ptr find Chris@16: (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) Chris@16: { Chris@16: node_ptr end = detail::uncast(header); Chris@16: node_ptr y = lower_bound(header, key, comp); Chris@16: return (y == end || comp(key, y)) ? end : y; Chris@16: } Chris@16: Chris@16: //! Requires: "header" must be the header node of a tree. Chris@16: //! KeyNodePtrCompare is a function object that induces a strict weak Chris@16: //! ordering compatible with the strict weak ordering used to create the Chris@16: //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs. Chris@16: //! 'lower_key' must not be greater than 'upper_key' according to 'comp'. If Chris@101: //! 'lower_key' == 'upper_key', ('left_closed' || 'right_closed') must be true. Chris@16: //! Chris@16: //! Effects: Returns an a pair with the following criteria: Chris@16: //! Chris@16: //! first = lower_bound(lower_key) if left_closed, upper_bound(lower_key) otherwise Chris@16: //! Chris@16: //! second = upper_bound(upper_key) if right_closed, lower_bound(upper_key) otherwise Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@16: //! Throws: If "comp" throws. Chris@16: //! Chris@16: //! Note: This function can be more efficient than calling upper_bound Chris@16: //! and lower_bound for lower_key and upper_key. Chris@16: //! Chris@16: //! Note: Experimental function, the interface might change. Chris@16: template< class KeyType, class KeyNodePtrCompare> Chris@16: static std::pair bounded_range Chris@16: ( const const_node_ptr & header Chris@16: , const KeyType &lower_key Chris@16: , const KeyType &upper_key Chris@16: , KeyNodePtrCompare comp Chris@16: , bool left_closed Chris@16: , bool right_closed) Chris@16: { Chris@16: node_ptr y = detail::uncast(header); Chris@16: node_ptr x = NodeTraits::get_parent(header); Chris@16: Chris@16: while(x){ Chris@16: //If x is less than lower_key the target Chris@16: //range is on the right part Chris@16: if(comp(x, lower_key)){ Chris@16: //Check for invalid input range Chris@16: BOOST_INTRUSIVE_INVARIANT_ASSERT(comp(x, upper_key)); Chris@16: x = NodeTraits::get_right(x); Chris@16: } Chris@16: //If the upper_key is less than x, the target Chris@16: //range is on the left part Chris@16: else if(comp(upper_key, x)){ Chris@16: y = x; Chris@16: x = NodeTraits::get_left(x); Chris@16: } Chris@16: else{ Chris@101: //x is inside the bounded range(lower_key <= x <= upper_key), Chris@16: //so we must split lower and upper searches Chris@16: // Chris@16: //Sanity check: if lower_key and upper_key are equal, then both left_closed and right_closed can't be false Chris@16: BOOST_INTRUSIVE_INVARIANT_ASSERT(left_closed || right_closed || comp(lower_key, x) || comp(x, upper_key)); Chris@16: return std::pair( Chris@16: left_closed Chris@16: //If left_closed, then comp(x, lower_key) is already the lower_bound Chris@16: //condition so we save one comparison and go to the next level Chris@16: //following traditional lower_bound algo Chris@16: ? lower_bound_loop(NodeTraits::get_left(x), x, lower_key, comp) Chris@16: //If left-open, comp(x, lower_key) is not the upper_bound algo Chris@16: //condition so we must recheck current 'x' node with upper_bound algo Chris@16: : upper_bound_loop(x, y, lower_key, comp) Chris@16: , Chris@16: right_closed Chris@16: //If right_closed, then comp(upper_key, x) is already the upper_bound Chris@16: //condition so we can save one comparison and go to the next level Chris@16: //following lower_bound algo Chris@16: ? upper_bound_loop(NodeTraits::get_right(x), y, upper_key, comp) Chris@16: //If right-open, comp(upper_key, x) is not the lower_bound algo Chris@16: //condition so we must recheck current 'x' node with lower_bound algo Chris@16: : lower_bound_loop(x, y, upper_key, comp) Chris@16: ); Chris@16: } Chris@16: } Chris@16: return std::pair (y, y); Chris@16: } Chris@16: Chris@16: //! Requires: "header" must be the header node of a tree. Chris@16: //! KeyNodePtrCompare is a function object that induces a strict weak Chris@16: //! ordering compatible with the strict weak ordering used to create the Chris@16: //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs. Chris@16: //! Chris@101: //! Effects: Returns the number of elements with a key equivalent to "key" Chris@16: //! according to "comp". Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@16: //! Throws: If "comp" throws. Chris@16: template Chris@16: static std::size_t count Chris@16: (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) Chris@16: { Chris@16: std::pair ret = equal_range(header, key, comp); Chris@16: std::size_t n = 0; Chris@16: while(ret.first != ret.second){ Chris@16: ++n; Chris@101: ret.first = base_type::next_node(ret.first); Chris@16: } Chris@16: return n; Chris@16: } Chris@16: Chris@16: //! Requires: "header" must be the header node of a tree. Chris@16: //! KeyNodePtrCompare is a function object that induces a strict weak Chris@16: //! ordering compatible with the strict weak ordering used to create the Chris@16: //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs. Chris@16: //! Chris@16: //! Effects: Returns an a pair of node_ptr delimiting a range containing Chris@16: //! all elements that are equivalent to "key" according to "comp" or an Chris@16: //! empty range that indicates the position where those elements would be Chris@16: //! if there are no equivalent elements. Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@16: //! Throws: If "comp" throws. Chris@16: template Chris@16: static std::pair equal_range Chris@16: (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) Chris@16: { Chris@16: return bounded_range(header, key, key, comp, true, true); Chris@16: } Chris@16: Chris@16: //! Requires: "header" must be the header node of a tree. Chris@16: //! KeyNodePtrCompare is a function object that induces a strict weak Chris@16: //! ordering compatible with the strict weak ordering used to create the Chris@16: //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs. Chris@16: //! Chris@101: //! Effects: Returns an a pair of node_ptr delimiting a range containing Chris@101: //! the first element that is equivalent to "key" according to "comp" or an Chris@101: //! empty range that indicates the position where that element would be Chris@101: //! if there are no equivalent elements. Chris@101: //! Chris@101: //! Complexity: Logarithmic. Chris@101: //! Chris@101: //! Throws: If "comp" throws. Chris@101: template Chris@101: static std::pair lower_bound_range Chris@101: (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) Chris@101: { Chris@101: node_ptr const lb(lower_bound(header, key, comp)); Chris@101: std::pair ret_ii(lb, lb); Chris@101: if(lb != header && !comp(key, lb)){ Chris@101: ret_ii.second = base_type::next_node(ret_ii.second); Chris@101: } Chris@101: return ret_ii; Chris@101: } Chris@101: Chris@101: //! Requires: "header" must be the header node of a tree. Chris@101: //! KeyNodePtrCompare is a function object that induces a strict weak Chris@101: //! ordering compatible with the strict weak ordering used to create the Chris@101: //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs. Chris@101: //! Chris@101: //! Effects: Returns a node_ptr to the first element that is Chris@16: //! not less than "key" according to "comp" or "header" if that element does Chris@16: //! not exist. Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@16: //! Throws: If "comp" throws. Chris@16: template Chris@16: static node_ptr lower_bound Chris@16: (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) Chris@16: { Chris@16: return lower_bound_loop(NodeTraits::get_parent(header), detail::uncast(header), key, comp); Chris@16: } Chris@16: Chris@16: //! Requires: "header" must be the header node of a tree. Chris@16: //! KeyNodePtrCompare is a function object that induces a strict weak Chris@16: //! ordering compatible with the strict weak ordering used to create the Chris@16: //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs. Chris@16: //! Chris@101: //! Effects: Returns a node_ptr to the first element that is greater Chris@16: //! than "key" according to "comp" or "header" if that element does not exist. Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@16: //! Throws: If "comp" throws. Chris@16: template Chris@16: static node_ptr upper_bound Chris@16: (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) Chris@16: { Chris@16: return upper_bound_loop(NodeTraits::get_parent(header), detail::uncast(header), key, comp); Chris@16: } Chris@16: Chris@16: //! Requires: "header" must be the header node of a tree. Chris@16: //! "commit_data" must have been obtained from a previous call to Chris@16: //! "insert_unique_check". No objects should have been inserted or erased Chris@16: //! from the set between the "insert_unique_check" that filled "commit_data" Chris@16: //! and the call to "insert_commit". Chris@16: //! Chris@16: //! Chris@16: //! Effects: Inserts new_node in the set using the information obtained Chris@16: //! from the "commit_data" that a previous "insert_check" filled. Chris@16: //! Chris@16: //! Complexity: Constant time. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Notes: This function has only sense if a "insert_unique_check" has been Chris@16: //! previously executed to fill "commit_data". No value should be inserted or Chris@16: //! erased between the "insert_check" and "insert_commit" calls. Chris@16: static void insert_unique_commit Chris@16: (const node_ptr & header, const node_ptr & new_value, const insert_commit_data &commit_data) Chris@16: { return insert_commit(header, new_value, commit_data); } Chris@16: Chris@16: //! Requires: "header" must be the header node of a tree. Chris@16: //! KeyNodePtrCompare is a function object that induces a strict weak Chris@16: //! ordering compatible with the strict weak ordering used to create the Chris@16: //! the tree. NodePtrCompare compares KeyType with a node_ptr. Chris@16: //! Chris@16: //! Effects: Checks if there is an equivalent node to "key" in the Chris@16: //! tree according to "comp" and obtains the needed information to realize Chris@16: //! a constant-time node insertion if there is no equivalent node. Chris@16: //! Chris@16: //! Returns: If there is an equivalent value Chris@16: //! returns a pair containing a node_ptr to the already present node Chris@16: //! and false. If there is not equivalent key can be inserted returns true Chris@16: //! in the returned pair's boolean and fills "commit_data" that is meant to Chris@16: //! be used with the "insert_commit" function to achieve a constant-time Chris@16: //! insertion function. Chris@16: //! Chris@16: //! Complexity: Average complexity is at most logarithmic. Chris@16: //! Chris@16: //! Throws: If "comp" throws. Chris@16: //! Chris@16: //! Notes: This function is used to improve performance when constructing Chris@16: //! a node is expensive and the user does not want to have two equivalent nodes Chris@16: //! in the tree: if there is an equivalent value Chris@16: //! the constructed object must be discarded. Many times, the part of the Chris@16: //! node that is used to impose the order is much cheaper to construct Chris@16: //! than the node and this function offers the possibility to use that part Chris@16: //! to check if the insertion will be successful. Chris@16: //! Chris@16: //! If the check is successful, the user can construct the node and use Chris@16: //! "insert_commit" to insert the node in constant-time. This gives a total Chris@16: //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)). Chris@16: //! Chris@16: //! "commit_data" remains valid for a subsequent "insert_unique_commit" only Chris@16: //! if no more objects are inserted or erased from the set. Chris@16: template Chris@16: static std::pair insert_unique_check Chris@16: (const const_node_ptr & header, const KeyType &key Chris@16: ,KeyNodePtrCompare comp, insert_commit_data &commit_data Chris@16: #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@101: , std::size_t *pdepth = 0 Chris@16: #endif Chris@16: ) Chris@16: { Chris@16: std::size_t depth = 0; Chris@16: node_ptr h(detail::uncast(header)); Chris@16: node_ptr y(h); Chris@16: node_ptr x(NodeTraits::get_parent(y)); Chris@16: node_ptr prev = node_ptr(); Chris@16: Chris@16: //Find the upper bound, cache the previous value and if we should Chris@16: //store it in the left or right node Chris@16: bool left_child = true; Chris@16: while(x){ Chris@16: ++depth; Chris@16: y = x; Chris@16: x = (left_child = comp(key, x)) ? Chris@16: NodeTraits::get_left(x) : (prev = y, NodeTraits::get_right(x)); Chris@16: } Chris@16: Chris@16: if(pdepth) *pdepth = depth; Chris@16: Chris@16: //Since we've found the upper bound there is no other value with the same key if: Chris@16: // - There is no previous node Chris@16: // - The previous node is less than the key Chris@101: const bool not_present = !prev || comp(prev, key); Chris@101: if(not_present){ Chris@16: commit_data.link_left = left_child; Chris@16: commit_data.node = y; Chris@16: } Chris@101: return std::pair(prev, not_present); Chris@16: } Chris@16: Chris@16: //! Requires: "header" must be the header node of a tree. Chris@16: //! KeyNodePtrCompare is a function object that induces a strict weak Chris@16: //! ordering compatible with the strict weak ordering used to create the Chris@16: //! the tree. NodePtrCompare compares KeyType with a node_ptr. Chris@16: //! "hint" is node from the "header"'s tree. Chris@16: //! Chris@16: //! Effects: Checks if there is an equivalent node to "key" in the Chris@16: //! tree according to "comp" using "hint" as a hint to where it should be Chris@16: //! inserted and obtains the needed information to realize Chris@16: //! a constant-time node insertion if there is no equivalent node. Chris@16: //! If "hint" is the upper_bound the function has constant time Chris@16: //! complexity (two comparisons in the worst case). Chris@16: //! Chris@16: //! Returns: If there is an equivalent value Chris@16: //! returns a pair containing a node_ptr to the already present node Chris@16: //! and false. If there is not equivalent key can be inserted returns true Chris@16: //! in the returned pair's boolean and fills "commit_data" that is meant to Chris@16: //! be used with the "insert_commit" function to achieve a constant-time Chris@16: //! insertion function. Chris@16: //! Chris@16: //! Complexity: Average complexity is at most logarithmic, but it is Chris@16: //! amortized constant time if new_node should be inserted immediately before "hint". Chris@16: //! Chris@16: //! Throws: If "comp" throws. Chris@16: //! Chris@16: //! Notes: This function is used to improve performance when constructing Chris@16: //! a node is expensive and the user does not want to have two equivalent nodes Chris@16: //! in the tree: if there is an equivalent value Chris@16: //! the constructed object must be discarded. Many times, the part of the Chris@16: //! node that is used to impose the order is much cheaper to construct Chris@16: //! than the node and this function offers the possibility to use that part Chris@16: //! to check if the insertion will be successful. Chris@16: //! Chris@16: //! If the check is successful, the user can construct the node and use Chris@16: //! "insert_commit" to insert the node in constant-time. This gives a total Chris@16: //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)). Chris@16: //! Chris@16: //! "commit_data" remains valid for a subsequent "insert_unique_commit" only Chris@16: //! if no more objects are inserted or erased from the set. Chris@16: template Chris@16: static std::pair insert_unique_check Chris@16: (const const_node_ptr & header, const node_ptr &hint, const KeyType &key Chris@16: ,KeyNodePtrCompare comp, insert_commit_data &commit_data Chris@16: #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@101: , std::size_t *pdepth = 0 Chris@16: #endif Chris@16: ) Chris@16: { Chris@16: //hint must be bigger than the key Chris@16: if(hint == header || comp(key, hint)){ Chris@16: node_ptr prev(hint); Chris@16: //Previous value should be less than the key Chris@101: if(hint == begin_node(header) || comp((prev = base_type::prev_node(hint)), key)){ Chris@16: commit_data.link_left = unique(header) || !NodeTraits::get_left(hint); Chris@16: commit_data.node = commit_data.link_left ? hint : prev; Chris@16: if(pdepth){ Chris@16: *pdepth = commit_data.node == header ? 0 : depth(commit_data.node) + 1; Chris@16: } Chris@16: return std::pair(node_ptr(), true); Chris@16: } Chris@16: } Chris@16: //Hint was wrong, use hintless insertion Chris@16: return insert_unique_check(header, key, comp, commit_data, pdepth); Chris@16: } Chris@16: Chris@16: //! Requires: "header" must be the header node of a tree. Chris@16: //! NodePtrCompare is a function object that induces a strict weak Chris@16: //! ordering compatible with the strict weak ordering used to create the Chris@16: //! the tree. NodePtrCompare compares two node_ptrs. "hint" is node from Chris@16: //! the "header"'s tree. Chris@16: //! Chris@16: //! Effects: Inserts new_node into the tree, using "hint" as a hint to Chris@16: //! where it will be inserted. If "hint" is the upper_bound Chris@16: //! the insertion takes constant time (two comparisons in the worst case). Chris@16: //! Chris@16: //! Complexity: Logarithmic in general, but it is amortized Chris@16: //! constant time if new_node is inserted immediately before "hint". Chris@16: //! Chris@16: //! Throws: If "comp" throws. Chris@16: template Chris@16: static node_ptr insert_equal Chris@16: (const node_ptr & h, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp Chris@16: #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@101: , std::size_t *pdepth = 0 Chris@16: #endif Chris@16: ) Chris@16: { Chris@16: insert_commit_data commit_data; Chris@16: insert_equal_check(h, hint, new_node, comp, commit_data, pdepth); Chris@16: insert_commit(h, new_node, commit_data); Chris@16: return new_node; Chris@16: } Chris@16: Chris@16: //! Requires: "h" must be the header node of a tree. Chris@16: //! NodePtrCompare is a function object that induces a strict weak Chris@16: //! ordering compatible with the strict weak ordering used to create the Chris@16: //! the tree. NodePtrCompare compares two node_ptrs. Chris@16: //! Chris@16: //! Effects: Inserts new_node into the tree before the upper bound Chris@16: //! according to "comp". Chris@16: //! Chris@16: //! Complexity: Average complexity for insert element is at Chris@16: //! most logarithmic. Chris@16: //! Chris@16: //! Throws: If "comp" throws. Chris@16: template Chris@16: static node_ptr insert_equal_upper_bound Chris@16: (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp Chris@16: #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@101: , std::size_t *pdepth = 0 Chris@16: #endif Chris@16: ) Chris@16: { Chris@16: insert_commit_data commit_data; Chris@16: insert_equal_upper_bound_check(h, new_node, comp, commit_data, pdepth); Chris@16: insert_commit(h, new_node, commit_data); Chris@16: return new_node; Chris@16: } Chris@16: Chris@16: //! Requires: "h" must be the header node of a tree. Chris@16: //! NodePtrCompare is a function object that induces a strict weak Chris@16: //! ordering compatible with the strict weak ordering used to create the Chris@16: //! the tree. NodePtrCompare compares two node_ptrs. Chris@16: //! Chris@16: //! Effects: Inserts new_node into the tree before the lower bound Chris@16: //! according to "comp". Chris@16: //! Chris@16: //! Complexity: Average complexity for insert element is at Chris@16: //! most logarithmic. Chris@16: //! Chris@16: //! Throws: If "comp" throws. Chris@16: template Chris@16: static node_ptr insert_equal_lower_bound Chris@16: (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp Chris@16: #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@101: , std::size_t *pdepth = 0 Chris@16: #endif Chris@16: ) Chris@16: { Chris@16: insert_commit_data commit_data; Chris@16: insert_equal_lower_bound_check(h, new_node, comp, commit_data, pdepth); Chris@16: insert_commit(h, new_node, commit_data); Chris@16: return new_node; Chris@16: } Chris@16: Chris@16: //! Requires: "header" must be the header node of a tree. Chris@16: //! "pos" must be a valid iterator or header (end) node. Chris@16: //! "pos" must be an iterator pointing to the successor to "new_node" Chris@16: //! once inserted according to the order of already inserted nodes. This function does not Chris@16: //! check "pos" and this precondition must be guaranteed by the caller. Chris@16: //! Chris@16: //! Effects: Inserts new_node into the tree before "pos". Chris@16: //! Chris@16: //! Complexity: Constant-time. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: If "pos" is not the successor of the newly inserted "new_node" Chris@16: //! tree invariants might be broken. Chris@16: static node_ptr insert_before Chris@16: (const node_ptr & header, const node_ptr & pos, const node_ptr & new_node Chris@16: #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@101: , std::size_t *pdepth = 0 Chris@16: #endif Chris@16: ) Chris@16: { Chris@16: insert_commit_data commit_data; Chris@16: insert_before_check(header, pos, commit_data, pdepth); Chris@16: insert_commit(header, new_node, commit_data); Chris@16: return new_node; Chris@16: } Chris@16: Chris@16: //! Requires: "header" must be the header node of a tree. Chris@16: //! "new_node" must be, according to the used ordering no less than the Chris@16: //! greatest inserted key. Chris@16: //! Chris@16: //! Effects: Inserts new_node into the tree before "pos". Chris@16: //! Chris@16: //! Complexity: Constant-time. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: If "new_node" is less than the greatest inserted key Chris@16: //! tree invariants are broken. This function is slightly faster than Chris@16: //! using "insert_before". Chris@16: static void push_back Chris@16: (const node_ptr & header, const node_ptr & new_node Chris@16: #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@101: , std::size_t *pdepth = 0 Chris@16: #endif Chris@16: ) Chris@16: { Chris@16: insert_commit_data commit_data; Chris@16: push_back_check(header, commit_data, pdepth); Chris@16: insert_commit(header, new_node, commit_data); Chris@16: } Chris@16: Chris@16: //! Requires: "header" must be the header node of a tree. Chris@16: //! "new_node" must be, according to the used ordering, no greater than the Chris@16: //! lowest inserted key. Chris@16: //! Chris@16: //! Effects: Inserts new_node into the tree before "pos". Chris@16: //! Chris@16: //! Complexity: Constant-time. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: If "new_node" is greater than the lowest inserted key Chris@16: //! tree invariants are broken. This function is slightly faster than Chris@16: //! using "insert_before". Chris@16: static void push_front Chris@16: (const node_ptr & header, const node_ptr & new_node Chris@16: #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@101: , std::size_t *pdepth = 0 Chris@16: #endif Chris@16: ) Chris@16: { Chris@16: insert_commit_data commit_data; Chris@16: push_front_check(header, commit_data, pdepth); Chris@16: insert_commit(header, new_node, commit_data); Chris@16: } Chris@16: Chris@16: //! Requires: 'node' can't be a header node. Chris@16: //! Chris@16: //! Effects: Calculates the depth of a node: the depth of a Chris@16: //! node is the length (number of edges) of the path from the root Chris@16: //! to that node. (The root node is at depth 0.) Chris@16: //! Chris@16: //! Complexity: Logarithmic to the number of nodes in the tree. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: static std::size_t depth(const_node_ptr node) Chris@16: { Chris@16: std::size_t depth = 0; Chris@16: node_ptr p_parent; Chris@16: while(node != NodeTraits::get_parent(p_parent = NodeTraits::get_parent(node))){ Chris@16: ++depth; Chris@16: node = p_parent; Chris@16: } Chris@16: return depth; Chris@16: } Chris@16: Chris@16: //! Requires: "cloner" must be a function Chris@16: //! object taking a node_ptr and returning a new cloned node of it. "disposer" must Chris@16: //! take a node_ptr and shouldn't throw. Chris@16: //! Chris@16: //! Effects: First empties target tree calling Chris@16: //! void disposer::operator()(const node_ptr &) for every node of the tree Chris@16: //! except the header. Chris@16: //! Chris@16: //! Then, duplicates the entire tree pointed by "source_header" cloning each Chris@16: //! source node with node_ptr Cloner::operator()(const node_ptr &) to obtain Chris@16: //! the nodes of the target tree. If "cloner" throws, the cloned target nodes Chris@16: //! are disposed using void disposer(const node_ptr &). Chris@16: //! Chris@101: //! Complexity: Linear to the number of element of the source tree plus the Chris@16: //! number of elements of tree target tree when calling this function. Chris@16: //! Chris@16: //! Throws: If cloner functor throws. If this happens target nodes are disposed. Chris@16: template Chris@16: static void clone Chris@16: (const const_node_ptr & source_header, const node_ptr & target_header, Cloner cloner, Disposer disposer) Chris@16: { Chris@16: if(!unique(target_header)){ Chris@16: clear_and_dispose(target_header, disposer); Chris@16: } Chris@16: Chris@16: node_ptr leftmost, rightmost; Chris@16: node_ptr new_root = clone_subtree Chris@16: (source_header, target_header, cloner, disposer, leftmost, rightmost); Chris@16: Chris@16: //Now update header node Chris@16: NodeTraits::set_parent(target_header, new_root); Chris@16: NodeTraits::set_left (target_header, leftmost); Chris@16: NodeTraits::set_right (target_header, rightmost); Chris@16: } Chris@16: Chris@16: //! Requires: header must be the header of a tree, z a node Chris@16: //! of that tree and z != header. Chris@16: //! Chris@16: //! Effects: Erases node "z" from the tree with header "header". Chris@16: //! Chris@16: //! Complexity: Amortized constant time. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: static void erase(const node_ptr & header, const node_ptr & z) Chris@16: { Chris@16: data_for_rebalance ignored; Chris@101: erase(header, z, ignored); Chris@16: } Chris@16: Chris@16: //! Requires: node is a tree node but not the header. Chris@16: //! Chris@16: //! Effects: Unlinks the node and rebalances the tree. Chris@16: //! Chris@16: //! Complexity: Average complexity is constant time. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: static void unlink(const node_ptr & node) Chris@16: { Chris@16: node_ptr x = NodeTraits::get_parent(node); Chris@16: if(x){ Chris@101: while(!base_type::is_header(x)) Chris@16: x = NodeTraits::get_parent(x); Chris@16: erase(x, node); Chris@16: } Chris@16: } Chris@16: Chris@16: //! Requires: header must be the header of a tree. Chris@16: //! Chris@16: //! Effects: Rebalances the tree. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Linear. Chris@16: static void rebalance(const node_ptr & header) Chris@16: { Chris@16: node_ptr root = NodeTraits::get_parent(header); Chris@16: if(root){ Chris@16: rebalance_subtree(root); Chris@16: } Chris@16: } Chris@16: Chris@16: //! Requires: old_root is a node of a tree. It shall not be null. Chris@16: //! Chris@16: //! Effects: Rebalances the subtree rooted at old_root. Chris@16: //! Chris@16: //! Returns: The new root of the subtree. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Linear. Chris@16: static node_ptr rebalance_subtree(const node_ptr & old_root) Chris@16: { Chris@16: //Taken from: Chris@16: //"Tree rebalancing in optimal time and space" Chris@16: //Quentin F. Stout and Bette L. Warren Chris@16: Chris@16: //To avoid irregularities in the algorithm (old_root can be a Chris@16: //left or right child or even the root of the tree) just put the Chris@16: //root as the right child of its parent. Before doing this backup Chris@16: //information to restore the original relationship after Chris@16: //the algorithm is applied. Chris@16: node_ptr super_root = NodeTraits::get_parent(old_root); Chris@16: BOOST_INTRUSIVE_INVARIANT_ASSERT(super_root); Chris@16: Chris@16: //Get root info Chris@16: node_ptr super_root_right_backup = NodeTraits::get_right(super_root); Chris@16: bool super_root_is_header = NodeTraits::get_parent(super_root) == old_root; Chris@16: bool old_root_is_right = is_right_child(old_root); Chris@16: NodeTraits::set_right(super_root, old_root); Chris@16: Chris@16: std::size_t size; Chris@16: subtree_to_vine(super_root, size); Chris@16: vine_to_subtree(super_root, size); Chris@16: node_ptr new_root = NodeTraits::get_right(super_root); Chris@16: Chris@16: //Recover root Chris@16: if(super_root_is_header){ Chris@16: NodeTraits::set_right(super_root, super_root_right_backup); Chris@16: NodeTraits::set_parent(super_root, new_root); Chris@16: } Chris@16: else if(old_root_is_right){ Chris@16: NodeTraits::set_right(super_root, new_root); Chris@16: } Chris@16: else{ Chris@16: NodeTraits::set_right(super_root, super_root_right_backup); Chris@16: NodeTraits::set_left(super_root, new_root); Chris@16: } Chris@16: return new_root; Chris@16: } Chris@16: Chris@101: //! Effects: Asserts the integrity of the container with additional checks provided by the user. Chris@101: //! Chris@101: //! Requires: header must be the header of a tree. Chris@101: //! Chris@101: //! Complexity: Linear time. Chris@101: //! Chris@101: //! Note: The method might not have effect when asserts are turned off (e.g., with NDEBUG). Chris@101: //! Experimental function, interface might change in future versions. Chris@101: template Chris@101: static void check(const const_node_ptr& header, Checker checker, typename Checker::return_type& checker_return) Chris@101: { Chris@101: const_node_ptr root_node_ptr = NodeTraits::get_parent(header); Chris@101: if (!root_node_ptr){ Chris@101: // check left&right header pointers Chris@101: BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_left(header) == header); Chris@101: BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_right(header) == header); Chris@101: } Chris@101: else{ Chris@101: // check parent pointer of root node Chris@101: BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_parent(root_node_ptr) == header); Chris@101: // check subtree from root Chris@101: check_subtree(root_node_ptr, checker, checker_return); Chris@101: // check left&right header pointers Chris@101: const_node_ptr p = root_node_ptr; Chris@101: while (NodeTraits::get_left(p)) { p = NodeTraits::get_left(p); } Chris@101: BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_left(header) == p); Chris@101: p = root_node_ptr; Chris@101: while (NodeTraits::get_right(p)) { p = NodeTraits::get_right(p); } Chris@101: BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_right(header) == p); Chris@101: } Chris@101: } Chris@101: Chris@16: protected: Chris@101: static void erase(const node_ptr & header, const node_ptr & z, data_for_rebalance &info) Chris@101: { Chris@101: node_ptr y(z); Chris@101: node_ptr x; Chris@101: const node_ptr z_left(NodeTraits::get_left(z)); Chris@101: const node_ptr z_right(NodeTraits::get_right(z)); Chris@101: Chris@101: if(!z_left){ Chris@101: x = z_right; // x might be null. Chris@101: } Chris@101: else if(!z_right){ // z has exactly one non-null child. y == z. Chris@101: x = z_left; // x is not null. Chris@101: BOOST_ASSERT(x); Chris@101: } Chris@101: else{ //make y != z Chris@101: // y = find z's successor Chris@101: y = base_type::minimum(z_right); Chris@101: x = NodeTraits::get_right(y); // x might be null. Chris@101: } Chris@101: Chris@101: node_ptr x_parent; Chris@101: const node_ptr z_parent(NodeTraits::get_parent(z)); Chris@101: const bool z_is_leftchild(NodeTraits::get_left(z_parent) == z); Chris@101: Chris@101: if(y != z){ //has two children and y is the minimum of z Chris@101: //y is z's successor and it has a null left child. Chris@101: //x is the right child of y (it can be null) Chris@101: //Relink y in place of z and link x with y's old parent Chris@101: NodeTraits::set_parent(z_left, y); Chris@101: NodeTraits::set_left(y, z_left); Chris@101: if(y != z_right){ Chris@101: //Link y with the right tree of z Chris@101: NodeTraits::set_right(y, z_right); Chris@101: NodeTraits::set_parent(z_right, y); Chris@101: //Link x with y's old parent (y must be a left child) Chris@101: x_parent = NodeTraits::get_parent(y); Chris@101: BOOST_ASSERT(NodeTraits::get_left(x_parent) == y); Chris@101: if(x) Chris@101: NodeTraits::set_parent(x, x_parent); Chris@101: //Since y was the successor and not the right child of z, it must be a left child Chris@101: NodeTraits::set_left(x_parent, x); Chris@101: } Chris@101: else{ //y was the right child of y so no need to fix x's position Chris@101: x_parent = y; Chris@101: } Chris@101: NodeTraits::set_parent(y, z_parent); Chris@101: this_type::set_child(header, y, z_parent, z_is_leftchild); Chris@101: } Chris@101: else { // z has zero or one child, x is one child (it can be null) Chris@101: //Just link x to z's parent Chris@101: x_parent = z_parent; Chris@101: if(x) Chris@101: NodeTraits::set_parent(x, z_parent); Chris@101: this_type::set_child(header, x, z_parent, z_is_leftchild); Chris@101: Chris@101: //Now update leftmost/rightmost in case z was one of them Chris@101: if(NodeTraits::get_left(header) == z){ Chris@101: //z_left must be null because z is the leftmost Chris@101: BOOST_ASSERT(!z_left); Chris@101: NodeTraits::set_left(header, !z_right ? Chris@101: z_parent : // makes leftmost == header if z == root Chris@101: base_type::minimum(z_right)); Chris@101: } Chris@101: if(NodeTraits::get_right(header) == z){ Chris@101: //z_right must be null because z is the rightmost Chris@101: BOOST_ASSERT(!z_right); Chris@101: NodeTraits::set_right(header, !z_left ? Chris@101: z_parent : // makes rightmost == header if z == root Chris@101: base_type::maximum(z_left)); Chris@101: } Chris@101: } Chris@101: Chris@101: //If z had 0/1 child, y == z and one of its children (and maybe null) Chris@101: //If z had 2 children, y is the successor of z and x is the right child of y Chris@101: info.x = x; Chris@101: info.y = y; Chris@101: //If z had 0/1 child, x_parent is the new parent of the old right child of y (z's successor) Chris@101: //If z had 2 children, x_parent is the new parent of y (z_parent) Chris@101: BOOST_ASSERT(!x || NodeTraits::get_parent(x) == x_parent); Chris@101: info.x_parent = x_parent; Chris@101: } Chris@101: Chris@16: //! Requires: node is a node of the tree but it's not the header. Chris@16: //! Chris@16: //! Effects: Returns the number of nodes of the subtree. Chris@16: //! Chris@16: //! Complexity: Linear time. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: static std::size_t subtree_size(const const_node_ptr & subtree) Chris@16: { Chris@16: std::size_t count = 0; Chris@16: if (subtree){ Chris@16: node_ptr n = detail::uncast(subtree); Chris@16: node_ptr m = NodeTraits::get_left(n); Chris@16: while(m){ Chris@16: n = m; Chris@16: m = NodeTraits::get_left(n); Chris@16: } Chris@16: Chris@16: while(1){ Chris@16: ++count; Chris@16: node_ptr n_right(NodeTraits::get_right(n)); Chris@16: if(n_right){ Chris@16: n = n_right; Chris@16: m = NodeTraits::get_left(n); Chris@16: while(m){ Chris@16: n = m; Chris@16: m = NodeTraits::get_left(n); Chris@16: } Chris@16: } Chris@16: else { Chris@16: do{ Chris@16: if (n == subtree){ Chris@16: return count; Chris@16: } Chris@16: m = n; Chris@16: n = NodeTraits::get_parent(n); Chris@16: }while(NodeTraits::get_left(n) != m); Chris@16: } Chris@16: } Chris@16: } Chris@16: return count; Chris@16: } Chris@16: Chris@16: //! Requires: p is a node of a tree. Chris@16: //! Chris@16: //! Effects: Returns true if p is a left child. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: static bool is_left_child(const node_ptr & p) Chris@16: { return NodeTraits::get_left(NodeTraits::get_parent(p)) == p; } Chris@16: Chris@16: //! Requires: p is a node of a tree. Chris@16: //! Chris@16: //! Effects: Returns true if p is a right child. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: static bool is_right_child(const node_ptr & p) Chris@16: { return NodeTraits::get_right(NodeTraits::get_parent(p)) == p; } Chris@16: Chris@16: static void insert_before_check Chris@16: (const node_ptr &header, const node_ptr & pos Chris@16: , insert_commit_data &commit_data Chris@16: #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@101: , std::size_t *pdepth = 0 Chris@16: #endif Chris@16: ) Chris@16: { Chris@16: node_ptr prev(pos); Chris@16: if(pos != NodeTraits::get_left(header)) Chris@101: prev = base_type::prev_node(pos); Chris@16: bool link_left = unique(header) || !NodeTraits::get_left(pos); Chris@16: commit_data.link_left = link_left; Chris@16: commit_data.node = link_left ? pos : prev; Chris@16: if(pdepth){ Chris@16: *pdepth = commit_data.node == header ? 0 : depth(commit_data.node) + 1; Chris@16: } Chris@16: } Chris@16: Chris@16: static void push_back_check Chris@16: (const node_ptr & header, insert_commit_data &commit_data Chris@16: #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@101: , std::size_t *pdepth = 0 Chris@16: #endif Chris@16: ) Chris@16: { Chris@16: node_ptr prev(NodeTraits::get_right(header)); Chris@16: if(pdepth){ Chris@16: *pdepth = prev == header ? 0 : depth(prev) + 1; Chris@16: } Chris@16: commit_data.link_left = false; Chris@16: commit_data.node = prev; Chris@16: } Chris@16: Chris@16: static void push_front_check Chris@16: (const node_ptr & header, insert_commit_data &commit_data Chris@16: #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@101: , std::size_t *pdepth = 0 Chris@16: #endif Chris@16: ) Chris@16: { Chris@16: node_ptr pos(NodeTraits::get_left(header)); Chris@16: if(pdepth){ Chris@16: *pdepth = pos == header ? 0 : depth(pos) + 1; Chris@16: } Chris@16: commit_data.link_left = true; Chris@16: commit_data.node = pos; Chris@16: } Chris@16: Chris@16: template Chris@16: static void insert_equal_check Chris@16: (const node_ptr &header, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp Chris@16: , insert_commit_data &commit_data Chris@16: /// @cond Chris@16: , std::size_t *pdepth = 0 Chris@16: /// @endcond Chris@16: ) Chris@16: { Chris@16: if(hint == header || !comp(hint, new_node)){ Chris@16: node_ptr prev(hint); Chris@16: if(hint == NodeTraits::get_left(header) || Chris@101: !comp(new_node, (prev = base_type::prev_node(hint)))){ Chris@16: bool link_left = unique(header) || !NodeTraits::get_left(hint); Chris@16: commit_data.link_left = link_left; Chris@16: commit_data.node = link_left ? hint : prev; Chris@16: if(pdepth){ Chris@16: *pdepth = commit_data.node == header ? 0 : depth(commit_data.node) + 1; Chris@16: } Chris@16: } Chris@16: else{ Chris@16: insert_equal_upper_bound_check(header, new_node, comp, commit_data, pdepth); Chris@16: } Chris@16: } Chris@16: else{ Chris@16: insert_equal_lower_bound_check(header, new_node, comp, commit_data, pdepth); Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: static void insert_equal_upper_bound_check Chris@16: (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp, insert_commit_data & commit_data, std::size_t *pdepth = 0) Chris@101: { Chris@101: std::size_t depth = 0; Chris@101: node_ptr y(h); Chris@101: node_ptr x(NodeTraits::get_parent(y)); Chris@101: Chris@101: while(x){ Chris@101: ++depth; Chris@101: y = x; Chris@101: x = comp(new_node, x) ? Chris@101: NodeTraits::get_left(x) : NodeTraits::get_right(x); Chris@101: } Chris@101: if(pdepth) *pdepth = depth; Chris@101: commit_data.link_left = (y == h) || comp(new_node, y); Chris@101: commit_data.node = y; Chris@101: } Chris@16: Chris@16: template Chris@16: static void insert_equal_lower_bound_check Chris@16: (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp, insert_commit_data & commit_data, std::size_t *pdepth = 0) Chris@101: { Chris@101: std::size_t depth = 0; Chris@101: node_ptr y(h); Chris@101: node_ptr x(NodeTraits::get_parent(y)); Chris@101: Chris@101: while(x){ Chris@101: ++depth; Chris@101: y = x; Chris@101: x = !comp(x, new_node) ? Chris@101: NodeTraits::get_left(x) : NodeTraits::get_right(x); Chris@101: } Chris@101: if(pdepth) *pdepth = depth; Chris@101: commit_data.link_left = (y == h) || !comp(y, new_node); Chris@101: commit_data.node = y; Chris@101: } Chris@16: Chris@16: static void insert_commit Chris@16: (const node_ptr & header, const node_ptr & new_node, const insert_commit_data &commit_data) Chris@16: { Chris@16: //Check if commit_data has not been initialized by a insert_unique_check call. Chris@16: BOOST_INTRUSIVE_INVARIANT_ASSERT(commit_data.node != node_ptr()); Chris@16: node_ptr parent_node(commit_data.node); Chris@16: if(parent_node == header){ Chris@16: NodeTraits::set_parent(header, new_node); Chris@16: NodeTraits::set_right(header, new_node); Chris@16: NodeTraits::set_left(header, new_node); Chris@16: } Chris@16: else if(commit_data.link_left){ Chris@16: NodeTraits::set_left(parent_node, new_node); Chris@16: if(parent_node == NodeTraits::get_left(header)) Chris@16: NodeTraits::set_left(header, new_node); Chris@16: } Chris@16: else{ Chris@16: NodeTraits::set_right(parent_node, new_node); Chris@16: if(parent_node == NodeTraits::get_right(header)) Chris@16: NodeTraits::set_right(header, new_node); Chris@16: } Chris@16: NodeTraits::set_parent(new_node, parent_node); Chris@16: NodeTraits::set_right(new_node, node_ptr()); Chris@16: NodeTraits::set_left(new_node, node_ptr()); Chris@16: } Chris@16: Chris@101: //Fix header and own's parent data when replacing x with own, providing own's old data with parent Chris@101: static void set_child(const node_ptr & header, const node_ptr & new_child, const node_ptr & new_parent, const bool link_left) Chris@101: { Chris@101: if(new_parent == header) Chris@101: NodeTraits::set_parent(header, new_child); Chris@101: else if(link_left) Chris@101: NodeTraits::set_left(new_parent, new_child); Chris@101: else Chris@101: NodeTraits::set_right(new_parent, new_child); Chris@101: } Chris@101: Chris@101: // rotate p to left (no header and p's parent fixup) Chris@101: static void rotate_left_no_parent_fix(const node_ptr & p, const node_ptr &p_right) Chris@101: { Chris@101: node_ptr p_right_left(NodeTraits::get_left(p_right)); Chris@101: NodeTraits::set_right(p, p_right_left); Chris@101: if(p_right_left){ Chris@101: NodeTraits::set_parent(p_right_left, p); Chris@101: } Chris@101: NodeTraits::set_left(p_right, p); Chris@101: NodeTraits::set_parent(p, p_right); Chris@101: } Chris@101: Chris@101: // rotate p to left (with header and p's parent fixup) Chris@101: static void rotate_left(const node_ptr & p, const node_ptr & p_right, const node_ptr & p_parent, const node_ptr & header) Chris@101: { Chris@101: const bool p_was_left(NodeTraits::get_left(p_parent) == p); Chris@101: rotate_left_no_parent_fix(p, p_right); Chris@101: NodeTraits::set_parent(p_right, p_parent); Chris@101: set_child(header, p_right, p_parent, p_was_left); Chris@101: } Chris@101: Chris@101: // rotate p to right (no header and p's parent fixup) Chris@101: static void rotate_right_no_parent_fix(const node_ptr & p, const node_ptr &p_left) Chris@101: { Chris@101: node_ptr p_left_right(NodeTraits::get_right(p_left)); Chris@101: NodeTraits::set_left(p, p_left_right); Chris@101: if(p_left_right){ Chris@101: NodeTraits::set_parent(p_left_right, p); Chris@101: } Chris@101: NodeTraits::set_right(p_left, p); Chris@101: NodeTraits::set_parent(p, p_left); Chris@101: } Chris@101: Chris@101: // rotate p to right (with header and p's parent fixup) Chris@101: static void rotate_right(const node_ptr & p, const node_ptr & p_left, const node_ptr & p_parent, const node_ptr & header) Chris@101: { Chris@101: const bool p_was_left(NodeTraits::get_left(p_parent) == p); Chris@101: rotate_right_no_parent_fix(p, p_left); Chris@101: NodeTraits::set_parent(p_left, p_parent); Chris@101: set_child(header, p_left, p_parent, p_was_left); Chris@101: } Chris@101: Chris@16: private: Chris@101: Chris@16: static void subtree_to_vine(node_ptr vine_tail, std::size_t &size) Chris@16: { Chris@16: //Inspired by LibAVL: Chris@16: //It uses a clever optimization for trees with parent pointers. Chris@16: //No parent pointer is updated when transforming a tree to a vine as Chris@16: //most of them will be overriten during compression rotations. Chris@16: //A final pass must be made after the rebalancing to updated those Chris@16: //pointers not updated by tree_to_vine + compression calls Chris@16: std::size_t len = 0; Chris@16: node_ptr remainder = NodeTraits::get_right(vine_tail); Chris@16: while(remainder){ Chris@16: node_ptr tempptr = NodeTraits::get_left(remainder); Chris@16: if(!tempptr){ //move vine-tail down one Chris@16: vine_tail = remainder; Chris@16: remainder = NodeTraits::get_right(remainder); Chris@16: ++len; Chris@16: } Chris@16: else{ //rotate Chris@16: NodeTraits::set_left(remainder, NodeTraits::get_right(tempptr)); Chris@16: NodeTraits::set_right(tempptr, remainder); Chris@16: remainder = tempptr; Chris@16: NodeTraits::set_right(vine_tail, tempptr); Chris@16: } Chris@16: } Chris@16: size = len; Chris@101: } Chris@16: Chris@16: static void compress_subtree(node_ptr scanner, std::size_t count) Chris@16: { Chris@16: while(count--){ //compress "count" spine nodes in the tree with pseudo-root scanner Chris@16: node_ptr child = NodeTraits::get_right(scanner); Chris@16: node_ptr child_right = NodeTraits::get_right(child); Chris@16: NodeTraits::set_right(scanner, child_right); Chris@16: //Avoid setting the parent of child_right Chris@16: scanner = child_right; Chris@16: node_ptr scanner_left = NodeTraits::get_left(scanner); Chris@16: NodeTraits::set_right(child, scanner_left); Chris@16: if(scanner_left) Chris@16: NodeTraits::set_parent(scanner_left, child); Chris@16: NodeTraits::set_left(scanner, child); Chris@16: NodeTraits::set_parent(child, scanner); Chris@16: } Chris@16: } Chris@16: Chris@16: static void vine_to_subtree(const node_ptr & super_root, std::size_t count) Chris@16: { Chris@101: const std::size_t one_szt = 1u; Chris@101: std::size_t leaf_nodes = count + one_szt - std::size_t(one_szt << detail::floor_log2(count + one_szt)); Chris@16: compress_subtree(super_root, leaf_nodes); //create deepest leaves Chris@16: std::size_t vine_nodes = count - leaf_nodes; Chris@16: while(vine_nodes > 1){ Chris@16: vine_nodes /= 2; Chris@16: compress_subtree(super_root, vine_nodes); Chris@16: } Chris@16: Chris@16: //Update parents of nodes still in the in the original vine line Chris@16: //as those have not been updated by subtree_to_vine or compress_subtree Chris@16: for ( node_ptr q = super_root, p = NodeTraits::get_right(super_root) Chris@16: ; p Chris@16: ; q = p, p = NodeTraits::get_right(p)){ Chris@16: NodeTraits::set_parent(p, q); Chris@16: } Chris@16: } Chris@16: Chris@16: //! Requires: "n" must be a node inserted in a tree. Chris@16: //! Chris@16: //! Effects: Returns a pointer to the header node of the tree. Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: static node_ptr get_root(const node_ptr & node) Chris@16: { Chris@16: BOOST_INTRUSIVE_INVARIANT_ASSERT((!inited(node))); Chris@16: node_ptr x = NodeTraits::get_parent(node); Chris@16: if(x){ Chris@101: while(!base_type::is_header(x)){ Chris@16: x = NodeTraits::get_parent(x); Chris@16: } Chris@16: return x; Chris@16: } Chris@16: else{ Chris@16: return node; Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: static node_ptr clone_subtree Chris@16: (const const_node_ptr &source_parent, const node_ptr &target_parent Chris@16: , Cloner cloner, Disposer disposer Chris@16: , node_ptr &leftmost_out, node_ptr &rightmost_out Chris@16: ) Chris@16: { Chris@16: node_ptr target_sub_root = target_parent; Chris@16: node_ptr source_root = NodeTraits::get_parent(source_parent); Chris@16: if(!source_root){ Chris@16: leftmost_out = rightmost_out = source_root; Chris@16: } Chris@16: else{ Chris@16: //We'll calculate leftmost and rightmost nodes while iterating Chris@16: node_ptr current = source_root; Chris@16: node_ptr insertion_point = target_sub_root = cloner(current); Chris@16: Chris@16: //We'll calculate leftmost and rightmost nodes while iterating Chris@16: node_ptr leftmost = target_sub_root; Chris@16: node_ptr rightmost = target_sub_root; Chris@16: Chris@16: //First set the subroot Chris@16: NodeTraits::set_left(target_sub_root, node_ptr()); Chris@16: NodeTraits::set_right(target_sub_root, node_ptr()); Chris@16: NodeTraits::set_parent(target_sub_root, target_parent); Chris@16: Chris@16: dispose_subtree_disposer rollback(disposer, target_sub_root); Chris@16: while(true) { Chris@16: //First clone left nodes Chris@16: if( NodeTraits::get_left(current) && Chris@16: !NodeTraits::get_left(insertion_point)) { Chris@16: current = NodeTraits::get_left(current); Chris@16: node_ptr temp = insertion_point; Chris@16: //Clone and mark as leaf Chris@16: insertion_point = cloner(current); Chris@16: NodeTraits::set_left (insertion_point, node_ptr()); Chris@16: NodeTraits::set_right (insertion_point, node_ptr()); Chris@16: //Insert left Chris@16: NodeTraits::set_parent(insertion_point, temp); Chris@16: NodeTraits::set_left (temp, insertion_point); Chris@16: //Update leftmost Chris@16: if(rightmost == target_sub_root) Chris@16: leftmost = insertion_point; Chris@16: } Chris@16: //Then clone right nodes Chris@16: else if( NodeTraits::get_right(current) && Chris@16: !NodeTraits::get_right(insertion_point)){ Chris@16: current = NodeTraits::get_right(current); Chris@16: node_ptr temp = insertion_point; Chris@16: //Clone and mark as leaf Chris@16: insertion_point = cloner(current); Chris@16: NodeTraits::set_left (insertion_point, node_ptr()); Chris@16: NodeTraits::set_right (insertion_point, node_ptr()); Chris@16: //Insert right Chris@16: NodeTraits::set_parent(insertion_point, temp); Chris@16: NodeTraits::set_right (temp, insertion_point); Chris@16: //Update rightmost Chris@16: rightmost = insertion_point; Chris@16: } Chris@16: //If not, go up Chris@16: else if(current == source_root){ Chris@16: break; Chris@16: } Chris@16: else{ Chris@16: //Branch completed, go up searching more nodes to clone Chris@16: current = NodeTraits::get_parent(current); Chris@16: insertion_point = NodeTraits::get_parent(insertion_point); Chris@16: } Chris@16: } Chris@16: rollback.release(); Chris@16: leftmost_out = leftmost; Chris@16: rightmost_out = rightmost; Chris@16: } Chris@16: return target_sub_root; Chris@16: } Chris@16: Chris@16: template Chris@16: static void dispose_subtree(node_ptr x, Disposer disposer) Chris@16: { Chris@16: while (x){ Chris@16: node_ptr save(NodeTraits::get_left(x)); Chris@16: if (save) { Chris@16: // Right rotation Chris@16: NodeTraits::set_left(x, NodeTraits::get_right(save)); Chris@16: NodeTraits::set_right(save, x); Chris@16: } Chris@16: else { Chris@16: save = NodeTraits::get_right(x); Chris@16: init(x); Chris@16: disposer(x); Chris@16: } Chris@16: x = save; Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: static node_ptr lower_bound_loop Chris@16: (node_ptr x, node_ptr y, const KeyType &key, KeyNodePtrCompare comp) Chris@16: { Chris@16: while(x){ Chris@16: if(comp(x, key)){ Chris@16: x = NodeTraits::get_right(x); Chris@16: } Chris@16: else{ Chris@16: y = x; Chris@16: x = NodeTraits::get_left(x); Chris@16: } Chris@16: } Chris@16: return y; Chris@16: } Chris@16: Chris@16: template Chris@16: static node_ptr upper_bound_loop Chris@16: (node_ptr x, node_ptr y, const KeyType &key, KeyNodePtrCompare comp) Chris@16: { Chris@16: while(x){ Chris@16: if(comp(key, x)){ Chris@16: y = x; Chris@16: x = NodeTraits::get_left(x); Chris@16: } Chris@16: else{ Chris@16: x = NodeTraits::get_right(x); Chris@16: } Chris@16: } Chris@16: return y; Chris@16: } Chris@16: Chris@101: template Chris@101: static void check_subtree(const const_node_ptr& node, Checker checker, typename Checker::return_type& check_return) Chris@16: { Chris@101: const_node_ptr left = NodeTraits::get_left(node); Chris@101: const_node_ptr right = NodeTraits::get_right(node); Chris@101: typename Checker::return_type check_return_left; Chris@101: typename Checker::return_type check_return_right; Chris@101: if (left) Chris@101: { Chris@101: BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_parent(left) == node); Chris@101: check_subtree(left, checker, check_return_left); Chris@16: } Chris@101: if (right) Chris@101: { Chris@101: BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_parent(right) == node); Chris@101: check_subtree(right, checker, check_return_right); Chris@16: } Chris@101: checker(node, check_return_left, check_return_right, check_return); Chris@16: } Chris@16: }; Chris@16: Chris@16: /// @cond Chris@16: Chris@16: template Chris@16: struct get_algo Chris@16: { Chris@16: typedef bstree_algorithms type; Chris@16: }; Chris@16: Chris@101: template Chris@101: struct get_node_checker Chris@101: { Chris@101: typedef detail::bstree_node_checker type; Chris@101: }; Chris@101: Chris@16: /// @endcond Chris@16: Chris@16: } //namespace intrusive Chris@16: } //namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif //BOOST_INTRUSIVE_BSTREE_ALGORITHMS_HPP