diff --git a/include/boost/intrusive/detail/generic_hook.hpp b/include/boost/intrusive/detail/generic_hook.hpp index e01c5e9..1cd1225 100644 --- a/include/boost/intrusive/detail/generic_hook.hpp +++ b/include/boost/intrusive/detail/generic_hook.hpp @@ -32,6 +32,7 @@ enum , SlistBaseHook , SetBaseHook , UsetBaseHook +, SplaySetBaseHook }; struct no_default_definer{}; @@ -51,6 +52,10 @@ template struct default_definer { typedef Hook default_set_hook; }; +template +struct default_definer +{ typedef Hook default_splay_set_hook; }; + template struct default_definer { typedef Hook default_uset_hook; }; @@ -115,7 +120,7 @@ class generic_hook static const link_mode_type link_mode = LinkMode; typedef Tag tag; typedef typename GetNodeAlgorithms::type node_algorithms; - typedef typename node_algorithms::node_traits node_traits; + typedef typename node_algorithms::node_traits node_traits; typedef typename node_traits::node node; typedef typename node_traits::node_ptr node_ptr; typedef typename node_traits::const_node_ptr const_node_ptr; diff --git a/include/boost/intrusive/detail/mpl.hpp b/include/boost/intrusive/detail/mpl.hpp index f4fd3f6..5dcd75c 100644 --- a/include/boost/intrusive/detail/mpl.hpp +++ b/include/boost/intrusive/detail/mpl.hpp @@ -253,6 +253,14 @@ template struct add_const { typedef const T type; }; +template +struct remove_const +{ typedef T type; }; + +template +struct remove_const +{ typedef T type; }; + template struct remove_reference { diff --git a/include/boost/intrusive/detail/rbtree_node.hpp b/include/boost/intrusive/detail/rbtree_node.hpp index 55a9110..aba08a2 100644 --- a/include/boost/intrusive/detail/rbtree_node.hpp +++ b/include/boost/intrusive/detail/rbtree_node.hpp @@ -157,17 +157,18 @@ struct rbtree_node_traits_dispatch {}; //Inherit from the detail::link_dispatch depending on the embedding capabilities -template +template struct rbtree_node_traits : public rbtree_node_traits_dispatch < VoidPointer - , has_pointer_plus_bit + , OptimizeSize && + has_pointer_plus_bit < VoidPointer , detail::alignment_of >::value >::value > {}; - +/* ///////////////////////////////////////////////////////////////////////////// // // // Implementation of the rbtree iterator // @@ -291,7 +292,7 @@ class rbtree_iterator node_ptr nodeptr_; } members_; }; - +*/ } //namespace intrusive } //namespace boost diff --git a/include/boost/intrusive/detail/tree_algorithms.hpp b/include/boost/intrusive/detail/tree_algorithms.hpp new file mode 100644 index 0000000..423f9fc --- /dev/null +++ b/include/boost/intrusive/detail/tree_algorithms.hpp @@ -0,0 +1,1332 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2007. +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_TREE_ALGORITHMS_HPP +#define BOOST_INTRUSIVE_TREE_ALGORITHMS_HPP + +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace intrusive { +namespace detail { + +//! This is an implementation of a binary search tree. +//! A node in the search tree has references to its children and its parent. This +//! is to allow traversal of the whole tree from a given node making the +//! implementation of iterator a pointer to a node. +//! At the top of the tree a node is used specially. This node's parent pointer +//! is pointing to the root of the tree. Its left pointer points to the +//! leftmost node in the tree and the right pointer to the rightmost one. +//! This node is used to represent the end-iterator. +//! +//! +---------+ +//! header------------------------------>| | +//! | | +//! +----------(left)--------| |--------(right)---------+ +//! | +---------+ | +//! | | | +//! | | (parent) | +//! | | | +//! | | | +//! | +---------+ | +//! root of tree ..|......................> | | | +//! | | D | | +//! | | | | +//! | +-------+---------+-------+ | +//! | | | | +//! | | | | +//! | | | | +//! | | | | +//! | | | | +//! | +---------+ +---------+ | +//! | | | | | | +//! | | B | | F | | +//! | | | | | | +//! | +--+---------+--+ +--+---------+--+ | +//! | | | | | | +//! | | | | | | +//! | | | | | | +//! | +---+-----+ +-----+---+ +---+-----+ +-----+---+ | +//! +-->| | | | | | | |<--+ +//! | A | | C | | E | | G | +//! | | | | | | | | +//! +---------+ +---------+ +---------+ +---------+ +//! + +//! tree_algorithms is configured with a NodeTraits class, which encapsulates the +//! information about the node to be manipulated. NodeTraits must support the +//! following interface: +//! +//! Typedefs: +//! +//! node: The type of the node that forms the circular list +//! +//! node_ptr: A pointer to a node +//! +//! const_node_ptr: A pointer to a const node +//! +//! Static functions: +//! +//! static node_ptr get_parent(const_node_ptr n); +//! +//! static void set_parent(node_ptr n, node_ptr parent); +//! +//! static node_ptr get_left(const_node_ptr n); +//! +//! static void set_left(node_ptr n, node_ptr left); +//! +//! static node_ptr get_right(const_node_ptr n); +//! +//! static void set_right(node_ptr n, node_ptr right); +template +class tree_algorithms +{ + /// @cond + private: + typedef typename NodeTraits::node node; + /// @endcond + + public: + typedef NodeTraits node_traits; + typedef typename NodeTraits::node_ptr node_ptr; + typedef typename NodeTraits::const_node_ptr const_node_ptr; + + //! This type is the information that will be filled by insert_unique_check + struct insert_commit_data + { + insert_commit_data() + : link_left(false) + , node(0) + {} + bool link_left; + node_ptr node; + }; + + struct nop_erase_fixup + { + void operator()(node_ptr to_erase, node_ptr successor){} + }; + + /// @cond + private: + static node_ptr uncast(const_node_ptr ptr) + { + return node_ptr(const_cast(::boost::intrusive::detail::get_pointer(ptr))); + } + /// @endcond + + public: + static node_ptr begin_node(const_node_ptr header) + { return node_traits::get_left(header); } + + static node_ptr end_node(const_node_ptr header) + { return uncast(header); } + + //! Requires: node is a node of the tree or an node initialized + //! by init(...). + //! + //! Effects: Returns true if the node is initialized by init(). + //! + //! Complexity: Constant time. + //! + //! Throws: Nothing. + static bool unique(const_node_ptr node) + { return NodeTraits::get_parent(node) == 0; } + + static node_ptr get_header(const_node_ptr node) + { + node_ptr h = uncast(node); + if(NodeTraits::get_parent(node)){ + h = NodeTraits::get_parent(node); + while(!is_header(h)) + h = NodeTraits::get_parent(h); + } + return h; + } + + //! Requires: node1 and node2 can't be header nodes + //! of two trees. + //! + //! Effects: Swaps two nodes. After the function node1 will be inserted + //! in the position node2 before the function. node2 will be inserted in the + //! position node1 had before the function. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + //! + //! Note: This function will break container ordering invariants if + //! node1 and node2 are not equivalent according to the ordering rules. + //! + //!Experimental function + static void swap_nodes(node_ptr node1, node_ptr node2) + { + if(node1 == node2) + return; + + node_ptr header1(get_header(node1)), header2(get_header(node2)); + swap_nodes(node1, header1, node2, header2); + } + + //! Requires: node1 and node2 can't be header nodes + //! of two trees with header header1 and header2. + //! + //! Effects: Swaps two nodes. After the function node1 will be inserted + //! in the position node2 before the function. node2 will be inserted in the + //! position node1 had before the function. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Note: This function will break container ordering invariants if + //! node1 and node2 are not equivalent according to the ordering rules. + //! + //!Experimental function + static void swap_nodes(node_ptr node1, node_ptr header1, node_ptr node2, node_ptr header2) + { + if(node1 == node2) + return; + + //node1 and node2 must not be header nodes + //BOOST_INTRUSIVE_INVARIANT_ASSERT((header1 != node1 && header2 != node2)); + if(header1 != header2){ + //Update header1 if necessary + if(node1 == NodeTraits::get_left(header1)){ + NodeTraits::set_left(header1, node2); + } + + if(node1 == NodeTraits::get_right(header1)){ + NodeTraits::set_right(header1, node2); + } + + if(node1 == NodeTraits::get_parent(header1)){ + NodeTraits::set_parent(header1, node2); + } + + //Update header2 if necessary + if(node2 == NodeTraits::get_left(header2)){ + NodeTraits::set_left(header2, node1); + } + + if(node2 == NodeTraits::get_right(header2)){ + NodeTraits::set_right(header2, node1); + } + + if(node2 == NodeTraits::get_parent(header2)){ + NodeTraits::set_parent(header2, node1); + } + } + else{ + //If both nodes are from the same tree + //Update header if necessary + if(node1 == NodeTraits::get_left(header1)){ + NodeTraits::set_left(header1, node2); + } + else if(node2 == NodeTraits::get_left(header2)){ + NodeTraits::set_left(header2, node1); + } + + if(node1 == NodeTraits::get_right(header1)){ + NodeTraits::set_right(header1, node2); + } + else if(node2 == NodeTraits::get_right(header2)){ + NodeTraits::set_right(header2, node1); + } + + if(node1 == NodeTraits::get_parent(header1)){ + NodeTraits::set_parent(header1, node2); + } + else if(node2 == NodeTraits::get_parent(header2)){ + NodeTraits::set_parent(header2, node1); + } + + //Adjust data in nodes to be swapped + //so that final link swap works as expected + if(node1 == NodeTraits::get_parent(node2)){ + NodeTraits::set_parent(node2, node2); + + if(node2 == NodeTraits::get_right(node1)){ + NodeTraits::set_right(node1, node1); + } + else{ + NodeTraits::set_left(node1, node1); + } + } + else if(node2 == NodeTraits::get_parent(node1)){ + NodeTraits::set_parent(node1, node1); + + if(node1 == NodeTraits::get_right(node2)){ + NodeTraits::set_right(node2, node2); + } + else{ + NodeTraits::set_left(node2, node2); + } + } + } + + //Now swap all the links + node_ptr temp; + //swap left link + temp = NodeTraits::get_left(node1); + NodeTraits::set_left(node1, NodeTraits::get_left(node2)); + NodeTraits::set_left(node2, temp); + //swap right link + temp = NodeTraits::get_right(node1); + NodeTraits::set_right(node1, NodeTraits::get_right(node2)); + NodeTraits::set_right(node2, temp); + //swap parent link + temp = NodeTraits::get_parent(node1); + NodeTraits::set_parent(node1, NodeTraits::get_parent(node2)); + NodeTraits::set_parent(node2, temp); + + //Now adjust adjacent nodes for newly inserted node 1 + if((temp = NodeTraits::get_left(node1))){ + NodeTraits::set_parent(temp, node1); + } + if((temp = NodeTraits::get_right(node1))){ + NodeTraits::set_parent(temp, node1); + } + if((temp = NodeTraits::get_parent(node1)) && + //The header has been already updated so avoid it + temp != header2){ + if(NodeTraits::get_left(temp) == node2){ + NodeTraits::set_left(temp, node1); + } + if(NodeTraits::get_right(temp) == node2){ + NodeTraits::set_right(temp, node1); + } + } + //Now adjust adjacent nodes for newly inserted node 2 + if((temp = NodeTraits::get_left(node2))){ + NodeTraits::set_parent(temp, node2); + } + if((temp = NodeTraits::get_right(node2))){ + NodeTraits::set_parent(temp, node2); + } + if((temp = NodeTraits::get_parent(node2)) && + //The header has been already updated so avoid it + temp != header1){ + if(NodeTraits::get_left(temp) == node1){ + NodeTraits::set_left(temp, node2); + } + if(NodeTraits::get_right(temp) == node1){ + NodeTraits::set_right(temp, node2); + } + } + } + + //! Requires: node_to_be_replaced must be inserted in a tree + //! and new_node must not be inserted in a tree. + //! + //! Effects: Replaces node_to_be_replaced in its position in the + //! tree with new_node. The tree does not need to be rebalanced + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + //! + //! Note: This function will break container ordering invariants if + //! new_node is not equivalent to node_to_be_replaced according to the + //! ordering rules. This function is faster than erasing and inserting + //! the node, since no rebalancing and comparison is needed. + //! + //!Experimental function + static void replace_node(node_ptr node_to_be_replaced, node_ptr new_node) + { + if(node_to_be_replaced == new_node) + return; + replace_node(node_to_be_replaced, get_header(node_to_be_replaced), new_node); + } + + //! Requires: node_to_be_replaced must be inserted in a tree + //! with header "header" and new_node must not be inserted in a tree. + //! + //! Effects: Replaces node_to_be_replaced in its position in the + //! tree with new_node. The tree does not need to be rebalanced + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Note: This function will break container ordering invariants if + //! new_node is not equivalent to node_to_be_replaced according to the + //! ordering rules. This function is faster than erasing and inserting + //! the node, since no rebalancing or comparison is needed. + //! + //!Experimental function + static void replace_node(node_ptr node_to_be_replaced, node_ptr header, node_ptr new_node) + { + if(node_to_be_replaced == new_node) + return; + + //Update header if necessary + if(node_to_be_replaced == NodeTraits::get_left(header)){ + NodeTraits::set_left(header, new_node); + } + + if(node_to_be_replaced == NodeTraits::get_right(header)){ + NodeTraits::set_right(header, new_node); + } + + if(node_to_be_replaced == NodeTraits::get_parent(header)){ + NodeTraits::set_parent(header, new_node); + } + + //Now set data from the original node + node_ptr temp; + NodeTraits::set_left(new_node, NodeTraits::get_left(node_to_be_replaced)); + NodeTraits::set_right(new_node, NodeTraits::get_right(node_to_be_replaced)); + NodeTraits::set_parent(new_node, NodeTraits::get_parent(node_to_be_replaced)); + + //Now adjust adjacent nodes for newly inserted node + if((temp = NodeTraits::get_left(new_node))){ + NodeTraits::set_parent(temp, new_node); + } + if((temp = NodeTraits::get_right(new_node))){ + NodeTraits::set_parent(temp, new_node); + } + if((temp = NodeTraits::get_parent(new_node)) && + //The header has been already updated so avoid it + temp != header){ + if(NodeTraits::get_left(temp) == node_to_be_replaced){ + NodeTraits::set_left(temp, new_node); + } + if(NodeTraits::get_right(temp) == node_to_be_replaced){ + NodeTraits::set_right(temp, new_node); + } + } + } + + //! Requires: p is a node from the tree except the header. + //! + //! Effects: Returns the next node of the tree. + //! + //! Complexity: Average constant time. + //! + //! Throws: Nothing. + static node_ptr next_node(node_ptr p) + { + node_ptr p_right(NodeTraits::get_right(p)); + if(p_right){ + return minimum(p_right); + } + else { + node_ptr x = NodeTraits::get_parent(p); + while(p == NodeTraits::get_right(x)){ + p = x; + x = NodeTraits::get_parent(x); + } + return NodeTraits::get_right(p) != x ? x : uncast(p); + } + } + + //! Requires: p is a node from the tree except the leftmost node. + //! + //! Effects: Returns the previous node of the tree. + //! + //! Complexity: Average constant time. + //! + //! Throws: Nothing. + static node_ptr prev_node(node_ptr p) + { + if(is_header(p)){ + return maximum(NodeTraits::get_parent(p)); + } + else if(NodeTraits::get_left(p)){ + return maximum(NodeTraits::get_left(p)); + } + else { + node_ptr x = NodeTraits::get_parent(p); + while(p == NodeTraits::get_left(x)){ + p = x; + x = NodeTraits::get_parent(x); + } + return x; + } + } + + //! Requires: p is a node of a tree but not the header. + //! + //! Effects: Returns the minimum node of the subtree starting at p. + //! + //! Complexity: Logarithmic to the size of the subtree. + //! + //! Throws: Nothing. + static node_ptr minimum (node_ptr p) + { + for(node_ptr p_left = NodeTraits::get_left(p) + ;p_left + ;p_left = NodeTraits::get_left(p)){ + p = p_left; + } + return p; + } + + //! Requires: p is a node of a tree but not the header. + //! + //! Effects: Returns the maximum node of the subtree starting at p. + //! + //! Complexity: Logarithmic to the size of the subtree. + //! + //! Throws: Nothing. + static node_ptr maximum(node_ptr p) + { + for(node_ptr p_right = NodeTraits::get_right(p) + ;p_right + ;p_right = NodeTraits::get_right(p)){ + p = p_right; + } + return p; + } + + //! Requires: node must not be part of any tree. + //! + //! Effects: After the function unique(node) == true. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Nodes: If node is inserted in a tree, this function corrupts the tree. + static void init(node_ptr node) + { + NodeTraits::set_parent(node, 0); + NodeTraits::set_left(node, 0); + NodeTraits::set_right(node, 0); + }; + + //! Requires: node must not be part of any tree. + //! + //! Effects: Initializes the header to represent an empty tree. + //! unique(header) == true. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Nodes: If node is inserted in a tree, this function corrupts the tree. + static void init_header(node_ptr header) + { + NodeTraits::set_parent(header, 0); + NodeTraits::set_left(header, header); + NodeTraits::set_right(header, header); + } + + //! Requires: "disposer" must be an object function + //! taking a node_ptr parameter and shouldn't throw. + //! + //! Effects: Empties the target tree calling + //! void disposer::operator()(node_ptr) for every node of the tree + //! except the header. + //! + //! Complexity: Linear to the number of element of the source tree plus the. + //! number of elements of tree target tree when calling this function. + //! + //! Throws: If cloner functor throws. If this happens target nodes are disposed. + template + static void clear_and_dispose(node_ptr header, Disposer disposer) + { + node_ptr source_root = NodeTraits::get_parent(header); + if(!source_root) + return; + dispose_subtree(source_root, disposer); + init_header(header); + } + + //! Requires: header is the header of a tree. + //! + //! Effects: Unlinks the leftmost node from the tree, and + //! updates the header link to the new leftmost node. + //! + //! Complexity: Average complexity is constant time. + //! + //! Throws: Nothing. + //! + //! Notes: This function breaks the tree and the tree can + //! only be used for more unlink_leftmost_without_rebalance calls. + //! This function is normally used to achieve a step by step + //! controlled destruction of the tree. + static node_ptr unlink_leftmost_without_rebalance(node_ptr header) + { + node_ptr leftmost = NodeTraits::get_left(header); + if (leftmost == header) + return 0; + node_ptr leftmost_parent(NodeTraits::get_parent(leftmost)); + node_ptr leftmost_right (NodeTraits::get_right(leftmost)); + bool is_root = leftmost_parent == header; + + if (leftmost_right){ + NodeTraits::set_parent(leftmost_right, leftmost_parent); + NodeTraits::set_left(header, tree_algorithms::minimum(leftmost_right)); + + if (is_root) + NodeTraits::set_parent(header, leftmost_right); + else + NodeTraits::set_left(NodeTraits::get_parent(header), leftmost_right); + } + else if (is_root){ + NodeTraits::set_parent(header, 0); + NodeTraits::set_left(header, header); + NodeTraits::set_right(header, header); + } + else{ + NodeTraits::set_left(leftmost_parent, 0); + NodeTraits::set_left(header, leftmost_parent); + } + return leftmost; + } + + //! Requires: node is a node of the tree but it's not the header. + //! + //! Effects: Returns the number of nodes of the subtree. + //! + //! Complexity: Linear time. + //! + //! Throws: Nothing. + static std::size_t count(const_node_ptr node) + { + std::size_t result = 1; + if(NodeTraits::get_left(node)) + result += count(NodeTraits::get_left(node)); + if(NodeTraits::get_right(node)) + result += count(NodeTraits::get_right(node)); + return result; + } + + //! Requires: header1 and header2 must be the header nodes + //! of two trees. + //! + //! Effects: Swaps two trees. After the function header1 will contain + //! links to the second tree and header2 will have links to the first tree. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + static void swap_tree(node_ptr header1, node_ptr header2) + { + if(header1 == header2) + return; + + node_ptr tmp; + + //Parent swap + tmp = NodeTraits::get_parent(header1); + NodeTraits::set_parent(header1, NodeTraits::get_parent(header2)); + NodeTraits::set_parent(header2, tmp); + //Left swap + tmp = NodeTraits::get_left(header1); + NodeTraits::set_left(header1, NodeTraits::get_left(header2)); + NodeTraits::set_left(header2, tmp); + //Right swap + tmp = NodeTraits::get_right(header1); + NodeTraits::set_right(header1, NodeTraits::get_right(header2)); + NodeTraits::set_right(header2, tmp); + + //Now test parent + node_ptr h1_parent(NodeTraits::get_parent(header1)); + if(h1_parent){ + NodeTraits::set_parent(h1_parent, header1); + } + else{ + NodeTraits::set_left(header1, header1); + NodeTraits::set_right(header1, header1); + } + + node_ptr h2_parent(NodeTraits::get_parent(header2)); + if(h2_parent){ + NodeTraits::set_parent(h2_parent, header2); + } + else{ + NodeTraits::set_left(header2, header2); + NodeTraits::set_right(header2, header2); + } + } + + static bool is_header(const_node_ptr p) + { + bool is_header = false; + if(NodeTraits::get_parent(p) == p){ + is_header = true; + } + else if(NodeTraits::get_parent(NodeTraits::get_parent(p)) == p){ + if(NodeTraits::get_left(p) != 0){ + if(NodeTraits::get_parent(NodeTraits::get_left(p)) != p){ + is_header = true; + } + if(NodeTraits::get_parent(p) == NodeTraits::get_left(p)){ + is_header = true; + } + } + } + return is_header; + } + + //! Requires: "header" must be the header node of a tree. + //! KeyNodePtrCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs. + //! + //! Effects: Returns an node_ptr to the element that is equivalent to + //! "key" according to "comp" or "header" if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If "comp" throws. + template + static node_ptr find + (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp) + { + node_ptr end = uncast(header); + node_ptr y = lower_bound(header, key, comp); + node_ptr r = (y == end || comp(key, y)) ? end : y; + return r; + } + + //! Requires: "header" must be the header node of a tree. + //! KeyNodePtrCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs. + //! + //! Effects: Returns an a pair of node_ptr delimiting a range containing + //! all elements that are equivalent to "key" according to "comp" or an + //! empty range that indicates the position where those elements would be + //! if they there are no equivalent elements. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If "comp" throws. + template + static std::pair equal_range + (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp) + { + node_ptr y = uncast(header); + node_ptr x = NodeTraits::get_parent(header); + + while(x){ + if(comp(x, key)){ + x = NodeTraits::get_right(x); + } + else if(comp(key, x)){ + y = x; + x = NodeTraits::get_left(x); + } + else{ + node_ptr xu(x), yu(y); + y = x, x = NodeTraits::get_left(x); + xu = NodeTraits::get_right(xu); + + while(x){ + if(comp(x, key)){ + x = NodeTraits::get_right(x); + } + else { + y = x; + x = NodeTraits::get_left(x); + } + } + + while(xu){ + if(comp(key, xu)){ + yu = xu; + xu = NodeTraits::get_left(xu); + } + else { + xu = NodeTraits::get_right(xu); + } + } + return std::pair (y, yu); + } + } + return std::pair (y, y); + } + + //! Requires: "header" must be the header node of a tree. + //! KeyNodePtrCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs. + //! + //! Effects: Returns an node_ptr to the first element that is + //! not less than "key" according to "comp" or "header" if that element does + //! not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If "comp" throws. + template + static node_ptr lower_bound + (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp) + { + node_ptr y = uncast(header); + node_ptr x = NodeTraits::get_parent(header); + while(x){ + if(comp(x, key)){ + x = NodeTraits::get_right(x); + } + else { + y = x; + x = NodeTraits::get_left(x); + } + } + return y; + } + + //! Requires: "header" must be the header node of a tree. + //! KeyNodePtrCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs. + //! + //! Effects: Returns an node_ptr to the first element that is greater + //! than "key" according to "comp" or "header" if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If "comp" throws. + template + static node_ptr upper_bound + (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp) + { + node_ptr y = uncast(header); + node_ptr x = NodeTraits::get_parent(header); + while(x){ + if(comp(key, x)){ + y = x; + x = NodeTraits::get_left(x); + } + else { + x = NodeTraits::get_right(x); + } + } + return y; + } + + //! Requires: "header" must be the header node of a tree. + //! "commit_data" must have been obtained from a previous call to + //! "insert_unique_check". No objects should have been inserted or erased + //! from the set between the "insert_unique_check" that filled "commit_data" + //! and the call to "insert_commit". + //! + //! + //! Effects: Inserts new_node in the set using the information obtained + //! from the "commit_data" that a previous "insert_check" filled. + //! + //! Complexity: Constant time. + //! + //! Throws: Nothing. + //! + //! Notes: This function has only sense if a "insert_unique_check" has been + //! previously executed to fill "commit_data". No value should be inserted or + //! erased between the "insert_check" and "insert_commit" calls. + static void insert_unique_commit + (node_ptr header, node_ptr new_value, const insert_commit_data &commit_data) + { + //Check if commit_data has not been initialized by a insert_unique_check call. + BOOST_INTRUSIVE_INVARIANT_ASSERT(commit_data.node != 0); + link(header, new_value, commit_data.node, commit_data.link_left); + } + + //! Requires: "header" must be the header node of a tree. + //! KeyNodePtrCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the tree. NodePtrCompare compares KeyType with a node_ptr. + //! + //! Effects: Checks if there is an equivalent node to "key" in the + //! tree according to "comp" and obtains the needed information to realize + //! a constant-time node insertion if there is no equivalent node. + //! + //! Returns: If there is an equivalent value + //! returns a pair containing a node_ptr to the already present node + //! and false. If there is not equivalent key can be inserted returns true + //! in the returned pair's boolean and fills "commit_data" that is meant to + //! be used with the "insert_commit" function to achieve a constant-time + //! insertion function. + //! + //! Complexity: Average complexity is at most logarithmic. + //! + //! Throws: If "comp" throws. + //! + //! Notes: This function is used to improve performance when constructing + //! a node is expensive and the user does not want to have two equivalent nodes + //! in the tree: if there is an equivalent value + //! the constructed object must be discarded. Many times, the part of the + //! node that is used to impose the order is much cheaper to construct + //! than the node and this function offers the possibility to use that part + //! to check if the insertion will be successful. + //! + //! If the check is successful, the user can construct the node and use + //! "insert_commit" to insert the node in constant-time. This gives a total + //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)). + //! + //! "commit_data" remains valid for a subsequent "insert_unique_commit" only + //! if no more objects are inserted or erased from the set. + template + static std::pair insert_unique_check + (const_node_ptr header, const KeyType &key + ,KeyNodePtrCompare comp, insert_commit_data &commit_data) + { + node_ptr h(uncast(header)); + node_ptr y(h); + node_ptr x(NodeTraits::get_parent(y)); + node_ptr prev(0); + + //Find the upper bound, cache the previous value and if we should + //store it in the left or right node + bool left_child = true; + while(x){ + y = x; + x = (left_child = comp(key, x)) ? + NodeTraits::get_left(x) : (prev = y, NodeTraits::get_right(x)); + } + + //Since we've found the upper bound there is no other value with the same key if: + // - There is no previous node + // - The previous node is less than the key + if(!prev || comp(prev, key)){ + commit_data.link_left = left_child; + commit_data.node = y; + return std::pair(node_ptr(), true); + } + //If the previous value was not less than key, it means that it's equal + //(because we've checked the upper bound) + else{ + return std::pair(prev, false); + } + } + + template + static std::pair insert_unique_check + (const_node_ptr header, node_ptr hint, const KeyType &key + ,KeyNodePtrCompare comp, insert_commit_data &commit_data) + { + //hint must be bigger than the key + if(hint == header || comp(key, hint)){ + node_ptr prev = hint; + //The previous value should be less than the key + if(prev == NodeTraits::get_left(header) || comp((prev = prev_node(hint)), key)){ + commit_data.link_left = unique(header) || !NodeTraits::get_left(hint); + commit_data.node = commit_data.link_left ? hint : prev; + return std::pair(node_ptr(), true); + } + else{ + return insert_unique_check(header, key, comp, commit_data); + } + } + //The hint was wrong, use hintless insert + else{ + return insert_unique_check(header, key, comp, commit_data); + } + } + + //! Requires: "header" must be the header node of a tree. + //! NodePtrCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the tree. NodePtrCompare compares two node_ptrs. "hint" is node from + //! the "header"'s tree. + //! + //! Effects: Inserts new_node into the tree, using "hint" as a hint to + //! where it will be inserted. If "hint" is the upper_bound + //! the insertion takes constant time (two comparisons in the worst case). + //! + //! Complexity: Logarithmic in general, but it is amortized + //! constant time if new_node is inserted immediately before "hint". + //! + //! Throws: If "comp" throws. + template + static node_ptr insert_equal + (node_ptr header, node_ptr hint, node_ptr new_node, NodePtrCompare comp) + { + if(hint == header || !comp(hint, new_node)){ + node_ptr prev(hint); + if(hint == NodeTraits::get_left(header) || + !comp(new_node, (prev = prev_node(hint)))){ + bool link_left = unique(header) || !NodeTraits::get_left(hint); + link(header, new_node, link_left ? hint : prev, link_left); + return new_node; + } + else{ + return insert_equal_upper_bound(header, new_node, comp); + } + } + else{ + return insert_equal_lower_bound(header, new_node, comp); + } + } + + template + static node_ptr insert_equal_upper_bound + (node_ptr h, node_ptr new_node, NodePtrCompare comp) + { + node_ptr y(h); + node_ptr x(NodeTraits::get_parent(y)); + + while(x){ + y = x; + x = comp(new_node, x) ? + NodeTraits::get_left(x) : NodeTraits::get_right(x); + } + + bool link_left = (y == h) || comp(new_node, y); + link(h, new_node, y, link_left); + return new_node; + } + + template + static node_ptr insert_equal_lower_bound + (node_ptr h, node_ptr new_node, NodePtrCompare comp) + { + node_ptr y(h); + node_ptr x(NodeTraits::get_parent(y)); + + while(x){ + y = x; + x = !comp(x, new_node) ? + NodeTraits::get_left(x) : NodeTraits::get_right(x); + } + + bool link_left = (y == h) || !comp(y, new_node); + link(h, new_node, y, link_left); + return new_node; + } + + //! Requires: "cloner" must be a function + //! object taking a node_ptr and returning a new cloned node of it. "disposer" must + //! take a node_ptr and shouldn't throw. + //! + //! Effects: First empties target tree calling + //! void disposer::operator()(node_ptr) for every node of the tree + //! except the header. + //! + //! Then, duplicates the entire tree pointed by "source_header" cloning each + //! source node with node_ptr Cloner::operator()(node_ptr) to obtain + //! the nodes of the target tree. If "cloner" throws, the cloned target nodes + //! are disposed using void disposer(node_ptr). + //! + //! Complexity: Linear to the number of element of the source tree plus the. + //! number of elements of tree target tree when calling this function. + //! + //! Throws: If cloner functor throws. If this happens target nodes are disposed. + template + static void clone + (const_node_ptr source_header, node_ptr target_header, Cloner cloner, Disposer disposer) + { + if(!unique(target_header)){ + clear_and_dispose(target_header, disposer); + } + + node_ptr leftmost, rightmost; + node_ptr new_root = clone_subtree + (source_header, target_header, cloner, disposer, leftmost, rightmost); + + //Now update header node + NodeTraits::set_parent(target_header, new_root); + NodeTraits::set_left (target_header, leftmost); + NodeTraits::set_right (target_header, rightmost); + } + + template + static node_ptr clone_subtree + ( const_node_ptr source_parent, node_ptr target_parent + , Cloner cloner, Disposer disposer + , node_ptr &leftmost_out, node_ptr &rightmost_out + ) + { + node_ptr target_sub_root = target_parent; + node_ptr source_root = NodeTraits::get_parent(source_parent); + if(!source_root){ + leftmost_out = rightmost_out = source_root; + } + else{ + //We'll calculate leftmost and rightmost nodes while iterating + node_ptr current = source_root; + node_ptr insertion_point = target_sub_root = cloner(current); + + //We'll calculate leftmost and rightmost nodes while iterating + node_ptr leftmost = target_sub_root; + node_ptr rightmost = target_sub_root; + + //First set the subroot + NodeTraits::set_left(target_sub_root, 0); + NodeTraits::set_right(target_sub_root, 0); + NodeTraits::set_parent(target_sub_root, target_parent); + + try { + while(true) { + //First clone left nodes + if( NodeTraits::get_left(current) && + !NodeTraits::get_left(insertion_point)) { + current = NodeTraits::get_left(current); + node_ptr temp = insertion_point; + //Clone and mark as leaf + insertion_point = cloner(current); + NodeTraits::set_left (insertion_point, 0); + NodeTraits::set_right (insertion_point, 0); + //Insert left + NodeTraits::set_parent(insertion_point, temp); + NodeTraits::set_left (temp, insertion_point); + //Update leftmost + if(rightmost == target_sub_root) + leftmost = insertion_point; + } + //Then clone right nodes + else if( NodeTraits::get_right(current) && + !NodeTraits::get_right(insertion_point)){ + current = NodeTraits::get_right(current); + node_ptr temp = insertion_point; + //Clone and mark as leaf + insertion_point = cloner(current); + NodeTraits::set_left (insertion_point, 0); + NodeTraits::set_right (insertion_point, 0); + //Insert right + NodeTraits::set_parent(insertion_point, temp); + NodeTraits::set_right (temp, insertion_point); + //Update rightmost + rightmost = insertion_point; + } + //If not, go up + else if(current == source_root){ + break; + } + else{ + //Branch completed, go up searching more nodes to clone + current = NodeTraits::get_parent(current); + insertion_point = NodeTraits::get_parent(insertion_point); + } + } + } + catch(...) { + dispose_subtree(target_sub_root, disposer); + throw; + } + leftmost_out = leftmost; + rightmost_out = rightmost; + } + return target_sub_root; + } + + template + static void dispose_subtree(node_ptr x, Disposer disposer) + { + node_ptr save; + while (x){ + save = NodeTraits::get_left(x); + if (save) { + // Right rotation + NodeTraits::set_left(x, NodeTraits::get_right(save)); + NodeTraits::set_right(save, x); + } + else { + save = NodeTraits::get_right(x); + init(x); + disposer(x); + } + x = save; + } + } + + //! Requires: p is a node of a tree. + //! + //! Effects: Returns true if p is a left child. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + static bool is_left_child(node_ptr p) + { return NodeTraits::get_left(NodeTraits::get_parent(p)) == p; } + + //! Requires: p is a node of a tree. + //! + //! Effects: Returns true if p is a right child. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + static bool is_right_child (node_ptr p) + { return NodeTraits::get_right(NodeTraits::get_parent(p)) == p; } + + static void replace_own (node_ptr own, node_ptr x, node_ptr header) + { + if(NodeTraits::get_parent(header) == own) + NodeTraits::set_parent(header, x); + else if(is_left_child(own)) + NodeTraits::set_left(NodeTraits::get_parent(own), x); + else + NodeTraits::set_right(NodeTraits::get_parent(own), x); + } + + static void rotate_left(node_ptr p, node_ptr header) + { + node_ptr x = NodeTraits::get_right(p); + NodeTraits::set_right(p, NodeTraits::get_left(x)); + if(NodeTraits::get_left(x) != 0) + NodeTraits::set_parent(NodeTraits::get_left(x), p); + NodeTraits::set_parent(x, NodeTraits::get_parent(p)); + replace_own (p, x, header); + NodeTraits::set_left(x, p); + NodeTraits::set_parent(p, x); + } + + static void rotate_right(node_ptr p, node_ptr header) + { + node_ptr x(NodeTraits::get_left(p)); + node_ptr x_right(NodeTraits::get_right(x)); + NodeTraits::set_left(p, x_right); + if(x_right) + NodeTraits::set_parent(x_right, p); + NodeTraits::set_parent(x, NodeTraits::get_parent(p)); + replace_own (p, x, header); + NodeTraits::set_right(x, p); + NodeTraits::set_parent(p, x); + } + + // rotate node t with left child | complexity : constant | exception : nothrow + static node_ptr rotate_left(node_ptr t) + { + node_ptr x = NodeTraits::get_right(t); + NodeTraits::set_right(t, NodeTraits::get_left(x)); + + if( NodeTraits::get_right(t) != 0 ){ + NodeTraits::set_parent(NodeTraits::get_right(t), t ); + } + NodeTraits::set_left(x, t); + NodeTraits::set_parent(t, x); + return x; + } + + // rotate node t with right child | complexity : constant | exception : nothrow + static node_ptr rotate_right(node_ptr t) + { + node_ptr x = NodeTraits::get_left(t); + NodeTraits::set_left(t, NodeTraits::get_right(x)); + if( NodeTraits::get_left(t) != 0 ){ + NodeTraits::set_parent(NodeTraits::get_left(t), t); + } + NodeTraits::set_right(x, t); + NodeTraits::set_parent(t, x); + return x; + } + + static void link(node_ptr header, node_ptr z, node_ptr par, bool left) + { + if(par == header){ + NodeTraits::set_parent(header, z); + NodeTraits::set_right(header, z); + NodeTraits::set_left(header, z); + } + else if(left){ + NodeTraits::set_left(par, z); + if(par == NodeTraits::get_left(header)) + NodeTraits::set_left(header, z); + } + else{ + NodeTraits::set_right(par, z); + if(par == NodeTraits::get_right(header)) + NodeTraits::set_right(header, z); + } + NodeTraits::set_parent(z, par); + NodeTraits::set_right(z, 0); + NodeTraits::set_left(z, 0); + } + + // delete node | complexity : constant | exception : nothrow + static void erase(node_ptr header, node_ptr z) + { erase(header, z, nop_erase_fixup()); } + + struct data_for_rebalance + { + node_ptr x; + node_ptr x_parent; + }; + + template + static void erase(node_ptr header, node_ptr z, F z_and_successor_fixup, data_for_rebalance * info = 0) + { + node_ptr y(z); + node_ptr x; + node_ptr x_parent(0); + node_ptr z_left(NodeTraits::get_left(z)); + node_ptr z_right(NodeTraits::get_right(z)); + if(!z_left){ + x = z_right; // x might be null. + } + else if(!z_right){ // z has exactly one non-null child. y == z. + x = z_left; // x is not null. + } + else{ + // find z's successor + y = tree_algorithms::minimum (z_right); + x = NodeTraits::get_right(y); // x might be null. + } + + if(y != z){ + // relink y in place of z. y is z's successor + NodeTraits::set_parent(NodeTraits::get_left(z), y); + NodeTraits::set_left(y, NodeTraits::get_left(z)); + if(y != NodeTraits::get_right(z)){ + x_parent = NodeTraits::get_parent(y); + if(x) + NodeTraits::set_parent(x, x_parent); + NodeTraits::set_left(x_parent, x); // y must be a child of left_ + NodeTraits::set_right(y, NodeTraits::get_right(z)); + NodeTraits::set_parent(NodeTraits::get_right(z), y); + } + else + x_parent = y; + tree_algorithms::replace_own (z, y, header); + NodeTraits::set_parent(y, NodeTraits::get_parent(z)); + z_and_successor_fixup(z, y); + } + else { // y == z --> z has only one child, or none + x_parent = NodeTraits::get_parent(z); + if(x) + NodeTraits::set_parent(x, x_parent); + tree_algorithms::replace_own (z, x, header); + if(NodeTraits::get_left(header) == z){ + NodeTraits::set_left(header, NodeTraits::get_right(z) == 0 ? // z->get_left() must be null also + NodeTraits::get_parent(z) : // makes leftmost == header if z == root + tree_algorithms::minimum (x)); + } + if(NodeTraits::get_right(header) == z){ + NodeTraits::set_right(header, NodeTraits::get_left(z) == 0 ? // z->get_right() must be null also + NodeTraits::get_parent(z) : // makes rightmost == header if z == root + tree_algorithms::maximum(x)); + } + } + + if(info){ + info->x = x; + info->x_parent = x_parent; + } + } + + static void unlink(node_ptr node) + { + node_ptr x = NodeTraits::get_parent(node); + if(x){ + while(!is_header(x)) + x = NodeTraits::get_parent(x); + erase(x, node); + } + } +}; + +} //namespace detail { +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_TREE_ALGORITHMS_HPP diff --git a/include/boost/intrusive/detail/tree_node.hpp b/include/boost/intrusive/detail/tree_node.hpp new file mode 100644 index 0000000..ea53b1c --- /dev/null +++ b/include/boost/intrusive/detail/tree_node.hpp @@ -0,0 +1,192 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2007. +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_TREE_NODE_HPP +#define BOOST_INTRUSIVE_TREE_NODE_HPP + +#include +#include +#include +#include + +namespace boost { +namespace intrusive { + +template +struct tree_node +{ + typedef typename pointer_to_other + >::type node_ptr; + + node_ptr parent_, left_, right_; +}; + +template +struct tree_node_traits +{ + typedef tree_node node; + + typedef typename boost::pointer_to_other + ::type node_ptr; + typedef typename boost::pointer_to_other + ::type const_node_ptr; + + static node_ptr get_parent(const_node_ptr n) + { return n->parent_; } + + static void set_parent(node_ptr n, node_ptr p) + { n->parent_ = p; } + + static node_ptr get_left(const_node_ptr n) + { return n->left_; } + + static void set_left(node_ptr n, node_ptr l) + { n->left_ = l; } + + static node_ptr get_right(const_node_ptr n) + { return n->right_; } + + static void set_right(node_ptr n, node_ptr r) + { n->right_ = r; } +}; + +///////////////////////////////////////////////////////////////////////////// +// // +// Implementation of the tree iterator // +// // +///////////////////////////////////////////////////////////////////////////// + +// tree_iterator provides some basic functions for a +// node oriented bidirectional iterator: +template +class tree_iterator + : public std::iterator + < std::bidirectional_iterator_tag + , typename detail::add_const_if_c + ::type + > +{ + protected: + typedef typename Container::real_value_traits real_value_traits; + typedef typename Container::node_algorithms node_algorithms; + typedef typename real_value_traits::node_traits node_traits; + typedef typename node_traits::node node; + typedef typename node_traits::node_ptr node_ptr; + typedef typename boost::pointer_to_other + ::type void_pointer; + static const bool store_container_ptr = + detail::store_cont_ptr_on_it::value; + + public: + public: + typedef typename detail::add_const_if_c + + ::type value_type; + typedef value_type & reference; + typedef value_type * pointer; + + tree_iterator() + : members_ (0, 0) + {} + + explicit tree_iterator(node_ptr node, const Container *cont_ptr) + : members_ (node, cont_ptr) + {} + + tree_iterator(tree_iterator const& other) + : members_(other.pointed_node(), other.get_container()) + {} + + const node_ptr &pointed_node() const + { return members_.nodeptr_; } + + tree_iterator &operator=(const node_ptr &node) + { members_.nodeptr_ = node; return static_cast(*this); } + + public: + tree_iterator& operator++() + { + members_.nodeptr_ = node_algorithms::next_node(members_.nodeptr_); + return static_cast (*this); + } + + tree_iterator operator++(int) + { + tree_iterator result (*this); + members_.nodeptr_ = node_algorithms::next_node(members_.nodeptr_); + return result; + } + + tree_iterator& operator--() + { + members_.nodeptr_ = node_algorithms::prev_node(members_.nodeptr_); + return static_cast (*this); + } + + tree_iterator operator--(int) + { + tree_iterator result (*this); + members_.nodeptr_ = node_algorithms::prev_node(members_.nodeptr_); + return result; + } + + bool operator== (const tree_iterator& i) const + { return members_.nodeptr_ == i.pointed_node(); } + + bool operator!= (const tree_iterator& i) const + { return !operator== (i); } + + value_type& operator*() const + { return *operator->(); } + + pointer operator->() const + { return detail::get_pointer(this->get_real_value_traits()->to_value_ptr(members_.nodeptr_)); } + + const Container *get_container() const + { + if(store_container_ptr) + return static_cast(members_.get_ptr()); + else + return 0; + } + + const real_value_traits *get_real_value_traits() const + { + if(store_container_ptr) + return &this->get_container()->get_real_value_traits(); + else + return 0; + } + + private: + struct members + : public detail::select_constptr + ::type + { + typedef typename detail::select_constptr + ::type Base; + + members(const node_ptr &n_ptr, const void *cont) + : Base(cont), nodeptr_(n_ptr) + {} + + node_ptr nodeptr_; + } members_; +}; + +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_TREE_NODE_HPP diff --git a/include/boost/intrusive/detail/utilities.hpp b/include/boost/intrusive/detail/utilities.hpp index 92449a8..a07d3b2 100644 --- a/include/boost/intrusive/detail/utilities.hpp +++ b/include/boost/intrusive/detail/utilities.hpp @@ -42,13 +42,13 @@ struct internal_base_hook_bool template static one test(...); template static two_or_three test (detail::bool_* = 0); - static const int value = sizeof(test(0)); + static const std::size_t value = sizeof(test(0)); }; template struct internal_base_hook_bool_is_true { - static const bool value = internal_base_hook_bool::value == 3; + static const bool value = internal_base_hook_bool::value > sizeof(one)*2; }; template @@ -59,7 +59,7 @@ struct external_value_traits_bool template static one test(...); template static two_or_three test (detail::bool_* = 0); - static const int value = sizeof(test(0)); + static const std::size_t value = sizeof(test(0)); }; template @@ -70,13 +70,13 @@ struct external_bucket_traits_bool template static one test(...); template static two_or_three test (detail::bool_* = 0); - static const int value = sizeof(test(0)); + static const std::size_t value = sizeof(test(0)); }; template struct external_value_traits_is_true { - static const bool value = external_value_traits_bool::value == 3; + static const bool value = external_value_traits_bool::value > sizeof(one)*2; }; template diff --git a/include/boost/intrusive/intrusive_fwd.hpp b/include/boost/intrusive/intrusive_fwd.hpp index 451bdb8..04b1f8c 100644 --- a/include/boost/intrusive/intrusive_fwd.hpp +++ b/include/boost/intrusive/intrusive_fwd.hpp @@ -141,6 +141,7 @@ template < class O1 = none , class O2 = none , class O3 = none + , class O4 = none > class set_base_hook; @@ -148,9 +149,52 @@ template < class O1 = none , class O2 = none , class O3 = none + , class O4 = none > class set_member_hook; +//splaytree/splay_set/splay_multiset +template + < class T + , class O1 = none + , class O2 = none + , class O3 = none + , class O4 = none + > +class splaytree; + +template + < class T + , class O1 = none + , class O2 = none + , class O3 = none + , class O4 = none + > +class splay_set; + +template + < class T + , class O1 = none + , class O2 = none + , class O3 = none + , class O4 = none + > +class splay_multiset; + +template + < class O1 = none + , class O2 = none + , class O3 = none + > +class splay_set_base_hook; + +template + < class O1 = none + , class O2 = none + , class O3 = none + > +class splay_set_member_hook; + //hash/unordered //rbtree/set/multiset template diff --git a/include/boost/intrusive/options.hpp b/include/boost/intrusive/options.hpp index c7683f7..8a1a67a 100644 --- a/include/boost/intrusive/options.hpp +++ b/include/boost/intrusive/options.hpp @@ -282,14 +282,14 @@ struct void_pointer //!the tag of a base hook. A type can not have two //!base hooks of the same type, so a tag can be used //!to differentiate two base hooks with otherwise same type -template +template struct tag { /// @cond template struct pack : Base { - typedef BaseTag tag; + typedef Tag tag; }; /// @endcond }; @@ -310,6 +310,22 @@ struct link_mode /// @endcond }; +//!This option setter specifies the type of +//!a void pointer. This will instruct the hook +//!to use this type of pointer instead of the +//!default one +template +struct optimize_size +{ +/// @cond + template + struct pack : Base + { + static const bool optimize_size = Enabled; + }; +/// @endcond +}; + //!This option setter specifies the bucket traits //!class for unordered associative containers. When this option is specified, //!instead of using the default bucket traits, a user defined holder will be defined @@ -416,6 +432,7 @@ struct hook_defaults , void_pointer , link_mode , tag + , optimize_size >::type {}; diff --git a/include/boost/intrusive/pointer_plus_bit.hpp b/include/boost/intrusive/pointer_plus_bit.hpp index 9a69ea3..863f5f8 100644 --- a/include/boost/intrusive/pointer_plus_bit.hpp +++ b/include/boost/intrusive/pointer_plus_bit.hpp @@ -44,7 +44,10 @@ struct has_pointer_plus_bit //!has_pointer_plus_bit<>::value is non-zero can make use of these //!operations to embed the bit in the pointer. template -struct pointer_plus_bit; +struct pointer_plus_bit +{ + static const bool value = false; +}; //!This is the specialization to embed an extra bit of information //!in a raw pointer. The extra bit is stored in the lower bit of the pointer. diff --git a/include/boost/intrusive/rbtree.hpp b/include/boost/intrusive/rbtree.hpp index d682f4e..fd2019b 100644 --- a/include/boost/intrusive/rbtree.hpp +++ b/include/boost/intrusive/rbtree.hpp @@ -13,22 +13,23 @@ #define BOOST_INTRUSIVE_RBTREE_HPP #include +#include +#include #include #include #include + #include #include #include -#include #include #include +#include #include +#include #include #include #include -#include -#include -#include namespace boost { namespace intrusive { @@ -78,7 +79,7 @@ struct set_defaults /// @endcond //! The class template rbtree is an intrusive red-black tree container, that -//! is used to construct intrusive set and tree containers. The no-throw +//! is used to construct intrusive set and multiset containers. The no-throw //! guarantee holds only, if the value_compare object //! doesn't throw. //! @@ -118,11 +119,11 @@ class rbtree_impl typedef typename Config::size_type size_type; typedef typename Config::compare value_compare; typedef value_compare key_compare; - typedef rbtree_iterator iterator; - typedef rbtree_iterator const_iterator; + typedef tree_iterator iterator; + typedef tree_iterator const_iterator; typedef std::reverse_iterator reverse_iterator; typedef std::reverse_iterator const_reverse_iterator; - typedef typename real_value_traits::node_traits node_traits; + typedef typename real_value_traits::node_traits node_traits; typedef typename node_traits::node node; typedef typename boost::pointer_to_other ::type node_ptr; @@ -235,7 +236,7 @@ class rbtree_impl //! [b, e). //! //! Complexity: Linear in N if [b, e) is already sorted using - //! comp and otherwise N * log N, where N is last ­ first. + //! comp and otherwise N * log N, where N is the distance between first and last. //! //! Throws: Nothing unless the copy constructor of the value_compare object throws. template @@ -411,8 +412,12 @@ class rbtree_impl { if(constant_time_size) return this->priv_size_traits().get_size(); - else - return empty() ? 0 : node_algorithms::count(node_traits::get_parent(const_node_ptr(&priv_header()))); + else{ + const_iterator beg(this->cbegin()), end(this->cend()); + size_type i = 0; + for(;beg != end; ++beg) ++i; + return i; + } } //! Effects: Swaps the contents of two multisets. @@ -445,7 +450,7 @@ class rbtree_impl //! //! Note: Does not affect the validity of iterators and references. //! No copy-constructors are called. - iterator insert_equal_upper_bound(reference value) + iterator insert_equal(reference value) { detail::key_nodeptr_comp key_node_comp(priv_comp(), this); @@ -457,29 +462,6 @@ class rbtree_impl (node_ptr(&priv_header()), to_insert, key_node_comp), this); } - //! Requires: value must be an lvalue - //! - //! Effects: Inserts value into the tree before the lower bound. - //! - //! Complexity: Average complexity for insert element is at - //! most logarithmic. - //! - //! Throws: Nothing. - //! - //! Note: Does not affect the validity of iterators and references. - //! No copy-constructors are called. - iterator insert_equal_lower_bound(reference value) - { - detail::key_nodeptr_comp - key_node_comp(priv_comp(), this); - node_ptr to_insert(get_real_value_traits().to_node_ptr(value)); - if(safemode_or_autounlink) - BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert)); - this->priv_size_traits().increment(); - return iterator(node_algorithms::insert_equal_lower_bound - (node_ptr(&priv_header()), to_insert, key_node_comp), this); - } - //! Requires: value must be an lvalue, and "hint" must be //! a valid iterator. //! @@ -523,15 +505,9 @@ class rbtree_impl template void insert_equal(Iterator b, Iterator e) { - if(this->empty()){ - iterator end(this->end()); - for (; b != e; ++b) - this->insert_equal(end, *b); - } - else{ - for (; b != e; ++b) - this->insert_equal_upper_bound(*b); - } + iterator end(this->end()); + for (; b != e; ++b) + this->insert_equal(end, *b); } //! Requires: value must be an lvalue @@ -1059,6 +1035,19 @@ class rbtree_impl return std::pair(const_iterator(ret.first, this), const_iterator(ret.second, this)); } + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! + //! Effects: Erases all the elements from *this + //! calling Disposer::operator()(pointer), clones all the + //! elements from src calling Cloner::operator()(const_reference ) + //! and inserts them on *this. + //! + //! If cloner throws, all cloned elements are unlinked and disposed + //! calling Disposer::operator()(pointer). + //! + //! Complexity: Linear to erased plus inserted elements. + //! + //! Throws: If cloner throws. template void clone_from(const rbtree_impl &src, Cloner cloner, Disposer disposer) { @@ -1073,6 +1062,16 @@ class rbtree_impl } } + //! Effects: Unlinks the leftmost node from the tree. + //! + //! Complexity: Average complexity is constant time. + //! + //! Throws: Nothing. + //! + //! Notes: This function breaks the tree and the tree can + //! only be used for more unlink_leftmost_without_rebalance calls. + //! This function is normally used to achieve a step by step + //! controlled destruction of the tree. pointer unlink_leftmost_without_rebalance() { node_ptr to_be_disposed(node_algorithms::unlink_leftmost_without_rebalance @@ -1132,7 +1131,7 @@ class rbtree_impl //! //! Complexity: Constant. //! - //! Throws: Nothing.ç + //! Throws: Nothing. //! //! Note: This static function is available only if the value traits //! is stateless. @@ -1266,14 +1265,14 @@ bool operator== { typedef rbtree_impl tree_type; typedef typename tree_type::const_iterator const_iterator; - const bool CS = tree_type::constant_time_size; - if(CS && x.size() != y.size()){ + + if(tree_type::constant_time_size && x.size() != y.size()){ return false; } const_iterator end1 = x.end(); const_iterator i1 = x.begin(); const_iterator i2 = y.begin(); - if(CS){ + if(tree_type::constant_time_size){ while (i1 != end1 && *i1 == *i2) { ++i1; ++i2; diff --git a/include/boost/intrusive/rbtree_algorithms.hpp b/include/boost/intrusive/rbtree_algorithms.hpp index ebdb464..43ebaa7 100644 --- a/include/boost/intrusive/rbtree_algorithms.hpp +++ b/include/boost/intrusive/rbtree_algorithms.hpp @@ -49,11 +49,14 @@ #define BOOST_INTRUSIVE_RBTREE_ALGORITHMS_HPP #include -#include -#include + #include +#include + +#include #include #include +#include namespace boost { @@ -112,11 +115,6 @@ namespace intrusive { template class rbtree_algorithms { - /// @cond - private: - typedef typename NodeTraits::node node; - /// @endcond - public: typedef NodeTraits node_traits; typedef typename NodeTraits::node_ptr node_ptr; @@ -125,45 +123,55 @@ class rbtree_algorithms /// @cond private: + + typedef typename NodeTraits::node node; + typedef detail::tree_algorithms tree_algorithms; + + template + struct rbtree_node_cloner + : private detail::ebo_functor_holder + { + typedef detail::ebo_functor_holder base_t; + + rbtree_node_cloner(F f) + : base_t(f) + {} + + node_ptr operator()(node_ptr p) + { + node_ptr n = base_t::get()(p); + NodeTraits::set_color(n, NodeTraits::get_color(p)); + return n; + } + }; + + struct rbtree_erase_fixup + { + void operator()(node_ptr to_erase, node_ptr successor) + { + //Swap color of y and z + color tmp(NodeTraits::get_color(successor)); + NodeTraits::set_color(successor, NodeTraits::get_color(to_erase)); + NodeTraits::set_color(to_erase, tmp); + } + }; + static node_ptr uncast(const_node_ptr ptr) { return node_ptr(const_cast(::boost::intrusive::detail::get_pointer(ptr))); } - - static void swap_left(node_ptr this_node, node_ptr other_node) - { - node_ptr temp(NodeTraits::get_left(this_node)); - NodeTraits::set_left(this_node, NodeTraits::get_left(other_node)); - NodeTraits::set_left(other_node, temp); - } - - static void swap_right(node_ptr this_node, node_ptr other_node) - { - node_ptr temp(NodeTraits::get_right(this_node)); - NodeTraits::set_right(this_node, NodeTraits::get_right(other_node)); - NodeTraits::set_right(other_node, temp); - } - - static void swap_parent(node_ptr this_node, node_ptr other_node) - { - node_ptr temp(NodeTraits::get_parent(this_node)); - NodeTraits::set_parent(this_node, NodeTraits::get_parent(other_node)); - NodeTraits::set_parent(other_node, temp); - } /// @endcond public: + static node_ptr begin_node(const_node_ptr header) + { return tree_algorithms::begin_node(header); } - //! This type is the information that will be filled by insert_unique_check - struct insert_commit_data - { - insert_commit_data() - : link_left(false) - , node(0) - {} - bool link_left; - node_ptr node; - }; + static node_ptr end_node(const_node_ptr header) + { return tree_algorithms::end_node(header); } + + //! This type is the information that will be + //! filled by insert_unique_check + typedef typename tree_algorithms::insert_commit_data insert_commit_data; //! Requires: header1 and header2 must be the header nodes //! of two trees. @@ -175,62 +183,7 @@ class rbtree_algorithms //! //! Throws: Nothing. static void swap_tree(node_ptr header1, node_ptr header2) - {/* - if(NodeTraits::get_parent(header1)){ - NodeTraits::node n1; - node_ptr n2(NodeTraits::get_parent(header1)); - init(&n1); - swap_nodes(&n1, n2); - swap_nodes(&n1, n2); - }*/ - if(header1 == header2) - return; - - node_ptr tmp; - - //Parent swap - tmp = NodeTraits::get_parent(header1); - NodeTraits::set_parent(header1, NodeTraits::get_parent(header2)); - NodeTraits::set_parent(header2, tmp); - //Left swap - tmp = NodeTraits::get_left(header1); - NodeTraits::set_left(header1, NodeTraits::get_left(header2)); - NodeTraits::set_left(header2, tmp); - //Right swap - tmp = NodeTraits::get_right(header1); - NodeTraits::set_right(header1, NodeTraits::get_right(header2)); - NodeTraits::set_right(header2, tmp); - - //Now test parent - node_ptr h1_parent(NodeTraits::get_parent(header1)); - if(h1_parent){ - NodeTraits::set_parent(h1_parent, header1); - } - else{ - NodeTraits::set_left(header1, header1); - NodeTraits::set_right(header1, header1); - } - - node_ptr h2_parent(NodeTraits::get_parent(header2)); - if(NodeTraits::get_parent(header2)){ - NodeTraits::set_parent(h2_parent, header2); - } - else{ - NodeTraits::set_left(header2, header2); - NodeTraits::set_right(header2, header2); - } - } - - static node_ptr get_header(const_node_ptr node) - { - node_ptr h = uncast(node); - if(NodeTraits::get_parent(node)){ - h = NodeTraits::get_parent(node); - while(!is_header(h)) - h = NodeTraits::get_parent(h); - } - return h; - } + { return tree_algorithms::swap_tree(header1, header2); } //! Requires: node1 and node2 can't be header nodes //! of two trees. @@ -252,7 +205,7 @@ class rbtree_algorithms if(node1 == node2) return; - node_ptr header1(get_header(node1)), header2(get_header(node2)); + node_ptr header1(tree_algorithms::get_header(node1)), header2(tree_algorithms::get_header(node2)); swap_nodes(node1, header1, node2, header2); } @@ -273,139 +226,13 @@ class rbtree_algorithms //!Experimental function static void swap_nodes(node_ptr node1, node_ptr header1, node_ptr node2, node_ptr header2) { - if(node1 == node2) - return; - - //node1 and node2 must not be header nodes - //BOOST_INTRUSIVE_INVARIANT_ASSERT((header1 != node1 && header2 != node2)); - if(header1 != header2){ - //Update header1 if necessary - if(node1 == NodeTraits::get_left(header1)){ - NodeTraits::set_left(header1, node2); - } + if(node1 == node2) return; - if(node1 == NodeTraits::get_right(header1)){ - NodeTraits::set_right(header1, node2); - } - - if(node1 == NodeTraits::get_parent(header1)){ - NodeTraits::set_parent(header1, node2); - } - - //Update header2 if necessary - if(node2 == NodeTraits::get_left(header2)){ - NodeTraits::set_left(header2, node1); - } - - if(node2 == NodeTraits::get_right(header2)){ - NodeTraits::set_right(header2, node1); - } - - if(node2 == NodeTraits::get_parent(header2)){ - NodeTraits::set_parent(header2, node1); - } - } - else{ - //If both nodes are from the same tree - //Update header if necessary - if(node1 == NodeTraits::get_left(header1)){ - NodeTraits::set_left(header1, node2); - } - else if(node2 == NodeTraits::get_left(header2)){ - NodeTraits::set_left(header2, node1); - } - - if(node1 == NodeTraits::get_right(header1)){ - NodeTraits::set_right(header1, node2); - } - else if(node2 == NodeTraits::get_right(header2)){ - NodeTraits::set_right(header2, node1); - } - - if(node1 == NodeTraits::get_parent(header1)){ - NodeTraits::set_parent(header1, node2); - } - else if(node2 == NodeTraits::get_parent(header2)){ - NodeTraits::set_parent(header2, node1); - } - - //Adjust data in nodes to be swapped - //so that final link swap works as expected - if(node1 == NodeTraits::get_parent(node2)){ - NodeTraits::set_parent(node2, node2); - - if(node2 == NodeTraits::get_right(node1)){ - NodeTraits::set_right(node1, node1); - } - else{ - NodeTraits::set_left(node1, node1); - } - } - else if(node2 == NodeTraits::get_parent(node1)){ - NodeTraits::set_parent(node1, node1); - - if(node1 == NodeTraits::get_right(node2)){ - NodeTraits::set_right(node2, node2); - } - else{ - NodeTraits::set_left(node2, node2); - } - } - } - - //Now swap all the links - node_ptr temp; - //swap left link - temp = NodeTraits::get_left(node1); - NodeTraits::set_left(node1, NodeTraits::get_left(node2)); - NodeTraits::set_left(node2, temp); - //swap right link - temp = NodeTraits::get_right(node1); - NodeTraits::set_right(node1, NodeTraits::get_right(node2)); - NodeTraits::set_right(node2, temp); - //swap parent link - temp = NodeTraits::get_parent(node1); - NodeTraits::set_parent(node1, NodeTraits::get_parent(node2)); - NodeTraits::set_parent(node2, temp); + tree_algorithms::swap_nodes(node1, header1, node2, header2); //Swap color color c = NodeTraits::get_color(node1); NodeTraits::set_color(node1, NodeTraits::get_color(node2)); NodeTraits::set_color(node2, c); - - //Now adjust adjacent nodes for newly inserted node 1 - if((temp = NodeTraits::get_left(node1))){ - NodeTraits::set_parent(temp, node1); - } - if((temp = NodeTraits::get_right(node1))){ - NodeTraits::set_parent(temp, node1); - } - if((temp = NodeTraits::get_parent(node1)) && - //The header has been already updated so avoid it - temp != header2){ - if(NodeTraits::get_left(temp) == node2){ - NodeTraits::set_left(temp, node1); - } - if(NodeTraits::get_right(temp) == node2){ - NodeTraits::set_right(temp, node1); - } - } - //Now adjust adjacent nodes for newly inserted node 2 - if((temp = NodeTraits::get_left(node2))){ - NodeTraits::set_parent(temp, node2); - } - if((temp = NodeTraits::get_right(node2))){ - NodeTraits::set_parent(temp, node2); - } - if((temp = NodeTraits::get_parent(node2)) && - //The header has been already updated so avoid it - temp != header1){ - if(NodeTraits::get_left(temp) == node1){ - NodeTraits::set_left(temp, node2); - } - if(NodeTraits::get_right(temp) == node1){ - NodeTraits::set_right(temp, node2); - } - } } //! Requires: node_to_be_replaced must be inserted in a tree @@ -428,7 +255,7 @@ class rbtree_algorithms { if(node_to_be_replaced == new_node) return; - replace_node(node_to_be_replaced, get_header(node_to_be_replaced), new_node); + replace_node(node_to_be_replaced, tree_algorithms::get_header(node_to_be_replaced), new_node); } //! Requires: node_to_be_replaced must be inserted in a tree @@ -449,46 +276,8 @@ class rbtree_algorithms //!Experimental function static void replace_node(node_ptr node_to_be_replaced, node_ptr header, node_ptr new_node) { - if(node_to_be_replaced == new_node) - return; - - //Update header if necessary - if(node_to_be_replaced == NodeTraits::get_left(header)){ - NodeTraits::set_left(header, new_node); - } - - if(node_to_be_replaced == NodeTraits::get_right(header)){ - NodeTraits::set_right(header, new_node); - } - - if(node_to_be_replaced == NodeTraits::get_parent(header)){ - NodeTraits::set_parent(header, new_node); - } - - //Now set data from the original node - node_ptr temp; - NodeTraits::set_left(new_node, NodeTraits::get_left(node_to_be_replaced)); - NodeTraits::set_right(new_node, NodeTraits::get_right(node_to_be_replaced)); - NodeTraits::set_parent(new_node, NodeTraits::get_parent(node_to_be_replaced)); + tree_algorithms::replace_node(node_to_be_replaced, header, new_node); NodeTraits::set_color(new_node, NodeTraits::get_color(node_to_be_replaced)); - - //Now adjust adjacent nodes for newly inserted node - if((temp = NodeTraits::get_left(new_node))){ - NodeTraits::set_parent(temp, new_node); - } - if((temp = NodeTraits::get_right(new_node))){ - NodeTraits::set_parent(temp, new_node); - } - if((temp = NodeTraits::get_parent(new_node)) && - //The header has been already updated so avoid it - temp != header){ - if(NodeTraits::get_left(temp) == node_to_be_replaced){ - NodeTraits::set_left(temp, new_node); - } - if(NodeTraits::get_right(temp) == node_to_be_replaced){ - NodeTraits::set_right(temp, new_node); - } - } } //! Requires: node is a tree node but not the header. @@ -498,20 +287,16 @@ class rbtree_algorithms //! Complexity: Average complexity is constant time. //! //! Throws: Nothing. - static void unlink_and_rebalance(node_ptr node) + static void unlink(node_ptr node) { - if(NodeTraits::get_parent(node)){ - node_ptr x = NodeTraits::get_parent(node); + node_ptr x = NodeTraits::get_parent(node); + if(x){ while(!is_header(x)) x = NodeTraits::get_parent(x); erase(x, node); } } - static void unlink(node_ptr node) - { unlink_and_rebalance(node); } - - //! Requires: header is the header of a tree. //! //! Effects: Unlinks the leftmost node from the tree, and @@ -526,34 +311,7 @@ class rbtree_algorithms //! This function is normally used to achieve a step by step //! controlled destruction of the tree. static node_ptr unlink_leftmost_without_rebalance(node_ptr header) - { - node_ptr leftmost = NodeTraits::get_left(header); - if (leftmost == header) - return 0; - node_ptr leftmost_parent(NodeTraits::get_parent(leftmost)); - node_ptr leftmost_right (NodeTraits::get_right(leftmost)); - bool is_root = leftmost_parent == header; - - if (leftmost_right){ - NodeTraits::set_parent(leftmost_right, leftmost_parent); - NodeTraits::set_left(header, minimum(leftmost_right)); - - if (is_root) - NodeTraits::set_parent(header, leftmost_right); - else - NodeTraits::set_left(NodeTraits::get_parent(header), leftmost_right); - } - else if (is_root){ - NodeTraits::set_parent(header, 0); - NodeTraits::set_left(header, header); - NodeTraits::set_right(header, header); - } - else{ - NodeTraits::set_left(leftmost_parent, 0); - NodeTraits::set_left(header, leftmost_parent); - } - return leftmost; - } + { return tree_algorithms::unlink_leftmost_without_rebalance(header); } //! Requires: node is a node of the tree or an node initialized //! by init(...). @@ -564,24 +322,17 @@ class rbtree_algorithms //! //! Throws: Nothing. static bool unique(const_node_ptr node) - { return NodeTraits::get_parent(node) == 0; } + { return tree_algorithms::unique(node); } //! Requires: node is a node of the tree but it's not the header. //! //! Effects: Returns the number of nodes of the subtree. //! - //! Complexity: Constant time. + //! Complexity: Linear time. //! //! Throws: Nothing. static std::size_t count(const_node_ptr node) - { - std::size_t result = 1; - if(NodeTraits::get_left(node)) - result += count(NodeTraits::get_left(node)); - if(NodeTraits::get_right(node)) - result += count(NodeTraits::get_right(node)); - return result; - } + { return tree_algorithms::count(node); } //! Requires: p is a node from the tree except the header. //! @@ -591,20 +342,7 @@ class rbtree_algorithms //! //! Throws: Nothing. static node_ptr next_node(node_ptr p) - { - node_ptr p_right(NodeTraits::get_right(p)); - if(p_right){ - return minimum(p_right); - } - else { - node_ptr x = NodeTraits::get_parent(p); - while(p == NodeTraits::get_right(x)){ - p = x; - x = NodeTraits::get_parent(x); - } - return NodeTraits::get_right(p) != x ? x : uncast(p); - } - } + { return tree_algorithms::next_node(p); } //! Requires: p is a node from the tree except the leftmost node. //! @@ -614,22 +352,7 @@ class rbtree_algorithms //! //! Throws: Nothing. static node_ptr prev_node(node_ptr p) - { - if(is_header(p)){ - return NodeTraits::get_right(p); // p is header, return rightmost - } - else if(NodeTraits::get_left(p)){ - return maximum(NodeTraits::get_left(p)); - } - else { - node_ptr x = NodeTraits::get_parent(p); - while(p == NodeTraits::get_left(x)){ - p = x; - x = NodeTraits::get_parent(x); - } - return x; - } - } + { return tree_algorithms::prev_node(p); } //! Requires: node must not be part of any tree. //! @@ -641,12 +364,7 @@ class rbtree_algorithms //! //! Nodes: If node is inserted in a tree, this function corrupts the tree. static void init(node_ptr node) - { - NodeTraits::set_parent(node, 0); - NodeTraits::set_left(node, 0); - NodeTraits::set_right(node, 0); - NodeTraits::set_color(node, NodeTraits::black()); - }; + { tree_algorithms::init(node); } //! Requires: node must not be part of any tree. //! @@ -660,11 +378,9 @@ class rbtree_algorithms //! Nodes: If node is inserted in a tree, this function corrupts the tree. static void init_header(node_ptr header) { - NodeTraits::set_parent(header, 0); - NodeTraits::set_left(header, header); - NodeTraits::set_right(header, header); + tree_algorithms::init_header(header); NodeTraits::set_color(header, NodeTraits::red()); - }; + } //! Requires: header must be the header of a tree, z a node //! of that tree and z != header. @@ -676,128 +392,16 @@ class rbtree_algorithms //! Throws: Nothing. static node_ptr erase(node_ptr header, node_ptr z) { - node_ptr y(z); - node_ptr x(0); - node_ptr x_parent(0); - node_ptr y_left(NodeTraits::get_left(y)); - node_ptr y_right(NodeTraits::get_right(y)); - if(!y_left){ - x = y_right; // x might be null. - } - else if(!y_right){ // z has exactly one non-null child. y == z. - x = y_left; // x is not null. - } - else{ - y = minimum (y_right); - x = NodeTraits::get_right(y); // x might be null. - } + typename tree_algorithms::data_for_rebalance info; + tree_algorithms::erase(header, z, rbtree_erase_fixup(), &info); + node_ptr x = info.x; + node_ptr x_parent = info.x_parent; - if(y != z){ - // relink y in place of z. y is z's successor - NodeTraits::set_parent(NodeTraits::get_left(z), y); - NodeTraits::set_left(y, NodeTraits::get_left(z)); - if(y != NodeTraits::get_right(z)){ - x_parent = NodeTraits::get_parent(y); - if(x) - NodeTraits::set_parent(x, NodeTraits::get_parent(y)); - NodeTraits::set_left(NodeTraits::get_parent(y), x); // y must be a child of left_ - NodeTraits::set_right(y, NodeTraits::get_right(z)); - NodeTraits::set_parent(NodeTraits::get_right(z), y); - } - else - x_parent = y; - replace_own (z, y, header); - NodeTraits::set_parent(y, NodeTraits::get_parent(z)); - color tmp(NodeTraits::get_color(y)); - tmp = NodeTraits::get_color(y); - NodeTraits::set_color(y, NodeTraits::get_color(z)); - NodeTraits::set_color(z, tmp); -// std::swap(NodeTraits::get_color(y), NodeTraits::get_color(z)); - y = z; - // y now points to node to be actually deleted + //Rebalance rbtree + if(NodeTraits::get_color(z) != NodeTraits::red()){ + rebalance_after_erasure(header, x, x_parent); } - else { // y == z - x_parent = NodeTraits::get_parent(y); - if(x) - NodeTraits::set_parent(x, NodeTraits::get_parent(y)); - replace_own (z, x, header); - if(NodeTraits::get_left(header) == z){ - NodeTraits::set_left(header, NodeTraits::get_right(z) == 0 ? // z->get_left() must be null also - NodeTraits::get_parent(z) : // makes leftmost == header if z == root - minimum (x)); - } - if(NodeTraits::get_right(header) == z){ - NodeTraits::set_right(header, NodeTraits::get_left(z) == 0 ? // z->get_right() must be null also - NodeTraits::get_parent(z) : // makes rightmost == header if z == root - maximum(x)); - } - } - if(NodeTraits::get_color(y) != NodeTraits::red()){ - while(x != NodeTraits::get_parent(header) && (x == 0 || NodeTraits::get_color(x) == NodeTraits::black())){ - if(x == NodeTraits::get_left(x_parent)){ - node_ptr w = NodeTraits::get_right(x_parent); - if(NodeTraits::get_color(w) == NodeTraits::red()){ - NodeTraits::set_color(w, NodeTraits::black()); - NodeTraits::set_color(x_parent, NodeTraits::red()); - rotate_left(x_parent, header); - w = NodeTraits::get_right(x_parent); - } - if((NodeTraits::get_left(w) == 0 || NodeTraits::get_color(NodeTraits::get_left(w)) == NodeTraits::black()) && - (NodeTraits::get_right(w) == 0 || NodeTraits::get_color(NodeTraits::get_right(w)) == NodeTraits::black())){ - NodeTraits::set_color(w, NodeTraits::red()); - x = x_parent; - x_parent = NodeTraits::get_parent(x_parent); - } - else { - if(NodeTraits::get_right(w) == 0 || NodeTraits::get_color(NodeTraits::get_right(w)) == NodeTraits::black()){ - NodeTraits::set_color(NodeTraits::get_left(w), NodeTraits::black()); - NodeTraits::set_color(w, NodeTraits::red()); - rotate_right(w, header); - w = NodeTraits::get_right(x_parent); - } - NodeTraits::set_color(w, NodeTraits::get_color(x_parent)); - NodeTraits::set_color(x_parent, NodeTraits::black()); - if(NodeTraits::get_right(w)) - NodeTraits::set_color(NodeTraits::get_right(w), NodeTraits::black()); - rotate_left(x_parent, header); - break; - } - } - else { - // same as above, with right_ <-> left_. - node_ptr w = NodeTraits::get_left(x_parent); - if(NodeTraits::get_color(w) == NodeTraits::red()){ - NodeTraits::set_color(w, NodeTraits::black()); - NodeTraits::set_color(x_parent, NodeTraits::red()); - rotate_right(x_parent, header); - w = NodeTraits::get_left(x_parent); - } - if((NodeTraits::get_right(w) == 0 || NodeTraits::get_color(NodeTraits::get_right(w)) == NodeTraits::black()) && - (NodeTraits::get_left(w) == 0 || NodeTraits::get_color(NodeTraits::get_left(w)) == NodeTraits::black())){ - NodeTraits::set_color(w, NodeTraits::red()); - x = x_parent; - x_parent = NodeTraits::get_parent(x_parent); - } - else { - if(NodeTraits::get_left(w) == 0 || NodeTraits::get_color(NodeTraits::get_left(w)) == NodeTraits::black()){ - NodeTraits::set_color(NodeTraits::get_right(w), NodeTraits::black()); - NodeTraits::set_color(w, NodeTraits::red()); - rotate_left(w, header); - w = NodeTraits::get_left(x_parent); - } - NodeTraits::set_color(w, NodeTraits::get_color(x_parent)); - NodeTraits::set_color(x_parent, NodeTraits::black()); - if(NodeTraits::get_left(w)) - NodeTraits::set_color(NodeTraits::get_left(w), NodeTraits::black()); - rotate_right(x_parent, header); - break; - } - } - } - if(x) - NodeTraits::set_color(x, NodeTraits::black()); - } - return y; + return z; } //! Requires: "cloner" must be a function @@ -821,18 +425,8 @@ class rbtree_algorithms static void clone (const_node_ptr source_header, node_ptr target_header, Cloner cloner, Disposer disposer) { - if(!unique(target_header)){ - clear_and_dispose(target_header, disposer); - } - - node_ptr leftmost, rightmost; - node_ptr new_root = clone_subtree - (source_header, target_header, cloner, disposer, leftmost, rightmost); - - //Now update header node - NodeTraits::set_parent(target_header, new_root); - NodeTraits::set_left (target_header, leftmost); - NodeTraits::set_right (target_header, rightmost); + rbtree_node_cloner new_cloner(cloner); + tree_algorithms::clone(source_header, target_header, new_cloner, disposer); } //! Requires: "disposer" must be an object function @@ -848,13 +442,7 @@ class rbtree_algorithms //! Throws: If cloner functor throws. If this happens target nodes are disposed. template static void clear_and_dispose(node_ptr header, Disposer disposer) - { - node_ptr source_root = NodeTraits::get_parent(header); - if(!source_root) - return; - dispose_subtree(source_root, disposer); - init_header(header); - } + { tree_algorithms::clear_and_dispose(header, disposer); } //! Requires: "header" must be the header node of a tree. //! KeyNodePtrCompare is a function object that induces a strict weak @@ -871,20 +459,7 @@ class rbtree_algorithms template static node_ptr lower_bound (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp) - { - node_ptr y = uncast(header); - node_ptr x = NodeTraits::get_parent(header); - while(x){ - if(comp(x, key)){ - x = NodeTraits::get_right(x); - } - else { - y = x; - x = NodeTraits::get_left(x); - } - } - return y; - } + { return tree_algorithms::lower_bound(header, key, comp); } //! Requires: "header" must be the header node of a tree. //! KeyNodePtrCompare is a function object that induces a strict weak @@ -900,20 +475,7 @@ class rbtree_algorithms template static node_ptr upper_bound (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp) - { - node_ptr y = uncast(header); - node_ptr x = NodeTraits::get_parent(header); - while(x){ - if(comp(key, x)){ - y = x; - x = NodeTraits::get_left(x); - } - else { - x = NodeTraits::get_right(x); - } - } - return y; - } + { return tree_algorithms::upper_bound(header, key, comp); } //! Requires: "header" must be the header node of a tree. //! KeyNodePtrCompare is a function object that induces a strict weak @@ -929,11 +491,7 @@ class rbtree_algorithms template static node_ptr find (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp) - { - node_ptr end = uncast(header); - node_ptr y = lower_bound(header, key, comp); - return (y == end || comp(key, y)) ? end : y; - } + { return tree_algorithms::find(header, key, comp); } //! Requires: "header" must be the header node of a tree. //! KeyNodePtrCompare is a function object that induces a strict weak @@ -951,47 +509,7 @@ class rbtree_algorithms template static std::pair equal_range (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp) - { - node_ptr y = uncast(header); - node_ptr x = NodeTraits::get_parent(header); - - while(x){ - if(comp(x, key)){ - x = NodeTraits::get_right(x); - } - else if(comp(key, x)){ - y = x; - x = NodeTraits::get_left(x); - } - else{ - node_ptr xu(x), yu(y); - y = x, x = NodeTraits::get_left(x); - xu = NodeTraits::get_right(xu); - - while(x){ - if(comp(x, key)){ - x = NodeTraits::get_right(x); - } - else { - y = x; - x = NodeTraits::get_left(x); - } - } - - while(xu){ - if(comp(key, xu)){ - yu = xu; - xu = NodeTraits::get_left(xu); - } - else { - xu = NodeTraits::get_right(xu); - } - } - return std::pair(y, yu); - } - } - return std::pair(y, y); - } + { return tree_algorithms::equal_range(header, key, comp); } //! Requires: "h" must be the header node of a tree. //! NodePtrCompare is a function object that induces a strict weak @@ -1009,29 +527,8 @@ class rbtree_algorithms static node_ptr insert_equal_upper_bound (node_ptr h, node_ptr new_node, NodePtrCompare comp) { - node_ptr y(h); - node_ptr x(NodeTraits::get_parent(y)); - - while(x){ - y = x; - x = comp(new_node, x) ? - NodeTraits::get_left(x) : NodeTraits::get_right(x); - } - - bool link_left = (y == h) || - comp(new_node, y); - link_and_balance(new_node, y, link_left, h); -/* - //erase me - NodeTraits::node n; - init(&n); - if(y!=h) - x = x; - node_ptr n1(y!=h ? y : &n); - node_ptr n2(new_node); - swap_nodes(n2, n1); - swap_nodes(n2, n1); -*/ + tree_algorithms::insert_equal_upper_bound(h, new_node, comp); + rebalance_after_insertion(h, new_node); return new_node; } @@ -1051,29 +548,8 @@ class rbtree_algorithms static node_ptr insert_equal_lower_bound (node_ptr h, node_ptr new_node, NodePtrCompare comp) { - node_ptr y(h); - node_ptr x(NodeTraits::get_parent(y)); - - while(x){ - y = x; - x = !comp(x, new_node) ? - NodeTraits::get_left(x) : NodeTraits::get_right(x); - } - - bool link_left = (y == h) || - !comp(y, new_node); - link_and_balance(new_node, y, link_left, h); -/* - //erase me - NodeTraits::node n; - init(&n); - if(y!=h) - x = x; - node_ptr n1(y!=h ? y : &n); - node_ptr n2(new_node); - swap_nodes(n2, n1); - swap_nodes(n2, n1); -*/ + tree_algorithms::insert_equal_lower_bound(h, new_node, comp); + rebalance_after_insertion(h, new_node); return new_node; } @@ -1095,29 +571,9 @@ class rbtree_algorithms static node_ptr insert_equal (node_ptr header, node_ptr hint, node_ptr new_node, NodePtrCompare comp) { - if(hint == header || !comp(hint, new_node)){ - node_ptr prev(hint); - if(hint == NodeTraits::get_left(header) || - !comp(new_node, (prev = prev_node(hint)))){ - bool link_left = unique(header) || !NodeTraits::get_left(hint); - link_and_balance(new_node, link_left ? hint : prev, link_left, header); -/* - //erase me - NodeTraits::node n1; - node_ptr n2(new_node); - init(&n1); - swap_nodes(n2, &n1); - swap_nodes(&n1, n2); -*/ - return new_node; - } - else{ - return insert_equal_upper_bound(header, new_node, comp); - } - } - else{ - return insert_equal_lower_bound(header, new_node, comp); - } + tree_algorithms::insert_equal(header, hint, new_node, comp); + rebalance_after_insertion(header, new_node); + return new_node; } //! Requires: "header" must be the header node of a tree. @@ -1158,35 +614,7 @@ class rbtree_algorithms static std::pair insert_unique_check (const_node_ptr header, const KeyType &key ,KeyNodePtrCompare comp, insert_commit_data &commit_data) - { - node_ptr h(uncast(header)); - node_ptr y(h); - node_ptr x(NodeTraits::get_parent(y)); - node_ptr prev(0); - - //Find the upper bound, cache the previous value and if we should - //store it in the left or right node - bool left_child = true; - while(x){ - y = x; - x = (left_child = comp(key, x)) ? - NodeTraits::get_left(x) : (prev = y, NodeTraits::get_right(x)); - } - - //Since we've found the upper bound there is no other value with the same key if: - // - There is no previous node - // - The previous node is less than the key - if(!prev || comp(prev, key)){ - commit_data.link_left = left_child; - commit_data.node = y; - return std::pair(node_ptr(), true); - } - //If the previous value was not less than key, it means that it's equal - //(because we've checked the upper bound) - else{ - return std::pair(prev, false); - } - } + { return tree_algorithms::insert_unique_check(header, key, comp, commit_data); } //! Requires: "header" must be the header node of a tree. //! KeyNodePtrCompare is a function object that induces a strict weak @@ -1231,26 +659,7 @@ class rbtree_algorithms static std::pair insert_unique_check (const_node_ptr header, node_ptr hint, const KeyType &key ,KeyNodePtrCompare comp, insert_commit_data &commit_data) - { - //hint must be bigger than the key - if(hint == header || comp(key, hint)){ - node_ptr prev = hint; - //The previous value should be less than the key - if(prev == NodeTraits::get_left(header) || comp((prev = prev_node(hint)), key)){ - commit_data.link_left = unique(header) || !NodeTraits::get_left(hint); - commit_data.node = commit_data.link_left ? hint : prev; - return std::pair(node_ptr(), true); - } - else{ - return insert_unique_check(header, key, comp, commit_data); - //return std::pair(prev, false); - } - } - //The hint was wrong, use hintless insert - else{ - return insert_unique_check(header, key, comp, commit_data); - } - } + { return tree_algorithms::insert_unique_check(header, hint, key, comp, commit_data); } //! Requires: "header" must be the header node of a tree. //! "commit_data" must have been obtained from a previous call to @@ -1272,182 +681,12 @@ class rbtree_algorithms static void insert_unique_commit (node_ptr header, node_ptr new_value, const insert_commit_data &commit_data) { - //Check if commit_data has not been initialized by a insert_unique_check call. - BOOST_INTRUSIVE_INVARIANT_ASSERT(commit_data.node != 0); - link_and_balance(new_value, commit_data.node, commit_data.link_left, header); + tree_algorithms::insert_unique_commit(header, new_value, commit_data); + rebalance_after_insertion(header, new_value); } /// @cond - - template - static node_ptr clone_subtree - ( const_node_ptr source_parent, node_ptr target_parent - , Cloner cloner, Disposer disposer - , node_ptr &leftmost_out, node_ptr &rightmost_out - ) - { - node_ptr target_sub_root = target_parent; - node_ptr source_root = NodeTraits::get_parent(source_parent); - if(!source_root){ - leftmost_out = rightmost_out = source_root; - } - else{ - //We'll calculate leftmost and rightmost nodes while iterating - node_ptr current = source_root; - node_ptr insertion_point = target_sub_root = cloner(current); - - //We'll calculate leftmost and rightmost nodes while iterating - node_ptr leftmost = target_sub_root; - node_ptr rightmost = target_sub_root; - - //First set the subroot - NodeTraits::set_left(target_sub_root, 0); - NodeTraits::set_right(target_sub_root, 0); - NodeTraits::set_parent(target_sub_root, target_parent); - NodeTraits::set_color(target_sub_root, NodeTraits::get_color(current)); - - try { - while(true) { - //First clone left nodes - if( NodeTraits::get_left(current) && - !NodeTraits::get_left(insertion_point)) { - current = NodeTraits::get_left(current); - node_ptr temp = insertion_point; - //Clone and mark as leaf - insertion_point = cloner(current); - NodeTraits::set_left (insertion_point, 0); - NodeTraits::set_right (insertion_point, 0); - NodeTraits::set_color (insertion_point, NodeTraits::get_color(current)); - //Insert left - NodeTraits::set_parent(insertion_point, temp); - NodeTraits::set_left (temp, insertion_point); - //Update leftmost - if(rightmost == target_sub_root) - leftmost = insertion_point; - } - //Then clone right nodes - else if( NodeTraits::get_right(current) && - !NodeTraits::get_right(insertion_point)){ - current = NodeTraits::get_right(current); - node_ptr temp = insertion_point; - //Clone and mark as leaf - insertion_point = cloner(current); - NodeTraits::set_left (insertion_point, 0); - NodeTraits::set_right (insertion_point, 0); - NodeTraits::set_color (insertion_point, NodeTraits::get_color(current)); - //Insert right - NodeTraits::set_parent(insertion_point, temp); - NodeTraits::set_right (temp, insertion_point); - //Update rightmost - rightmost = insertion_point; - } - //If not, go up - else if(current == source_root){ - break; - } - else{ - //Branch completed, go up searching more nodes to clone - current = NodeTraits::get_parent(current); - insertion_point = NodeTraits::get_parent(insertion_point); - } - } - } - catch(...) { - dispose_subtree(target_sub_root, disposer); - throw; - } - leftmost_out = leftmost; - rightmost_out = rightmost; - } - return target_sub_root; - } - - template - static void dispose_subtree(node_ptr x, Disposer disposer) - { - node_ptr save; - while (x){ - save = NodeTraits::get_left(x); - if (save) { - // Right rotation - NodeTraits::set_left(x, NodeTraits::get_right(save)); - NodeTraits::set_right(save, x); - } - else { - save = NodeTraits::get_right(x); - init(x); - disposer(x); - } - x = save; - } - } - - //! Requires: z is the node to be inserted, par is its parent, - //! left, indicates if z should be a left node of par and header is the header - //! of the tree. - //! - //! Effects: If left is true links z as a left child of par or as a right - //! child otherwise. After that rebalances the tree. - //! - //! Complexity: Average constant time.??? - //! - //! Throws: Nothing. - static void link_and_balance (node_ptr z, node_ptr par, bool left, node_ptr header) - { - if(par == header){ - NodeTraits::set_parent(header, z); - NodeTraits::set_right(header, z); - NodeTraits::set_left(header, z); - } - else if(left){ - NodeTraits::set_left(par, z); - if(par == NodeTraits::get_left(header)) - NodeTraits::set_left(header, z); - } - else{ - NodeTraits::set_right(par, z); - if(par == NodeTraits::get_right(header)) - NodeTraits::set_right(header, z); - } - NodeTraits::set_parent(z, par); - NodeTraits::set_right(z, 0); - NodeTraits::set_left(z, 0); - rebalance(z, header); - } - - //! Requires: p is a node of a tree but not the header. - //! - //! Effects: Returns the minimum node of the subtree starting at p. - //! - //! Complexity: Logarithmic to the size of the subtree. - //! - //! Throws: Nothing. - static node_ptr minimum (node_ptr p) - { - for(node_ptr p_left = NodeTraits::get_left(p) - ;p_left - ;p_left = NodeTraits::get_left(p)){ - p = p_left; - } - return p; - } - - //! Requires: p is a node of a tree but not the header. - //! - //! Effects: Returns the maximum node of the subtree starting at p. - //! - //! Complexity: Logarithmic to the size of the subtree. - //! - //! Throws: Nothing. - static node_ptr maximum(node_ptr p) - { - for(node_ptr p_right = NodeTraits::get_right(p) - ;p_right - ;p_right = NodeTraits::get_right(p)){ - p = p_right; - } - return p; - } + private: //! Requires: p is a node of a tree. //! @@ -1458,72 +697,87 @@ class rbtree_algorithms //! Throws: Nothing. static bool is_header(const_node_ptr p) { - return NodeTraits::get_color(p) == NodeTraits::red() && - NodeTraits::get_parent(NodeTraits::get_parent(p)) == p; + return NodeTraits::get_color(p) == NodeTraits::red() && + tree_algorithms::is_header(p); + //return NodeTraits::get_color(p) == NodeTraits::red() && + // NodeTraits::get_parent(NodeTraits::get_parent(p)) == p; } - //! Requires: p is a node of a tree. - //! - //! Effects: Returns true if p is a left child. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - static bool is_left_child(node_ptr p) - { return NodeTraits::get_left(NodeTraits::get_parent(p)) == p; } - - //! Requires: p is a node of a tree. - //! - //! Effects: Returns true if p is a right child. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - static bool is_right_child (node_ptr p) - { return NodeTraits::get_right(NodeTraits::get_parent(p)) == p; } - - static void replace_own (node_ptr own, node_ptr x, node_ptr header) + static void rebalance_after_erasure(node_ptr header, node_ptr x, node_ptr x_parent) { - if(NodeTraits::get_parent(header) == own) - NodeTraits::set_parent(header, x); - else if(is_left_child(own)) - NodeTraits::set_left(NodeTraits::get_parent(own), x); - else - NodeTraits::set_right(NodeTraits::get_parent(own), x); + while(x != NodeTraits::get_parent(header) && (x == 0 || NodeTraits::get_color(x) == NodeTraits::black())){ + if(x == NodeTraits::get_left(x_parent)){ + node_ptr w = NodeTraits::get_right(x_parent); + if(NodeTraits::get_color(w) == NodeTraits::red()){ + NodeTraits::set_color(w, NodeTraits::black()); + NodeTraits::set_color(x_parent, NodeTraits::red()); + tree_algorithms::rotate_left(x_parent, header); + w = NodeTraits::get_right(x_parent); + } + if((NodeTraits::get_left(w) == 0 || NodeTraits::get_color(NodeTraits::get_left(w)) == NodeTraits::black()) && + (NodeTraits::get_right(w) == 0 || NodeTraits::get_color(NodeTraits::get_right(w)) == NodeTraits::black())){ + NodeTraits::set_color(w, NodeTraits::red()); + x = x_parent; + x_parent = NodeTraits::get_parent(x_parent); + } + else { + if(NodeTraits::get_right(w) == 0 || NodeTraits::get_color(NodeTraits::get_right(w)) == NodeTraits::black()){ + NodeTraits::set_color(NodeTraits::get_left(w), NodeTraits::black()); + NodeTraits::set_color(w, NodeTraits::red()); + tree_algorithms::rotate_right(w, header); + w = NodeTraits::get_right(x_parent); + } + NodeTraits::set_color(w, NodeTraits::get_color(x_parent)); + NodeTraits::set_color(x_parent, NodeTraits::black()); + if(NodeTraits::get_right(w)) + NodeTraits::set_color(NodeTraits::get_right(w), NodeTraits::black()); + tree_algorithms::rotate_left(x_parent, header); + break; + } + } + else { + // same as above, with right_ <-> left_. + node_ptr w = NodeTraits::get_left(x_parent); + if(NodeTraits::get_color(w) == NodeTraits::red()){ + NodeTraits::set_color(w, NodeTraits::black()); + NodeTraits::set_color(x_parent, NodeTraits::red()); + tree_algorithms::rotate_right(x_parent, header); + w = NodeTraits::get_left(x_parent); + } + if((NodeTraits::get_right(w) == 0 || NodeTraits::get_color(NodeTraits::get_right(w)) == NodeTraits::black()) && + (NodeTraits::get_left(w) == 0 || NodeTraits::get_color(NodeTraits::get_left(w)) == NodeTraits::black())){ + NodeTraits::set_color(w, NodeTraits::red()); + x = x_parent; + x_parent = NodeTraits::get_parent(x_parent); + } + else { + if(NodeTraits::get_left(w) == 0 || NodeTraits::get_color(NodeTraits::get_left(w)) == NodeTraits::black()){ + NodeTraits::set_color(NodeTraits::get_right(w), NodeTraits::black()); + NodeTraits::set_color(w, NodeTraits::red()); + tree_algorithms::rotate_left(w, header); + w = NodeTraits::get_left(x_parent); + } + NodeTraits::set_color(w, NodeTraits::get_color(x_parent)); + NodeTraits::set_color(x_parent, NodeTraits::black()); + if(NodeTraits::get_left(w)) + NodeTraits::set_color(NodeTraits::get_left(w), NodeTraits::black()); + tree_algorithms::rotate_right(x_parent, header); + break; + } + } + } + if(x) + NodeTraits::set_color(x, NodeTraits::black()); } - static void rotate_left(node_ptr p, node_ptr header) - { - node_ptr x = NodeTraits::get_right(p); - NodeTraits::set_right(p, NodeTraits::get_left(x)); - if(NodeTraits::get_left(x) != 0) - NodeTraits::set_parent(NodeTraits::get_left(x), p); - NodeTraits::set_parent(x, NodeTraits::get_parent(p)); - replace_own (p, x, header); - NodeTraits::set_left(x, p); - NodeTraits::set_parent(p, x); - } - static void rotate_right(node_ptr p, node_ptr header) - { - node_ptr x(NodeTraits::get_left(p)); - node_ptr x_right(NodeTraits::get_right(x)); - NodeTraits::set_left(p, x_right); - if(x_right) - NodeTraits::set_parent(x_right, p); - NodeTraits::set_parent(x, NodeTraits::get_parent(p)); - replace_own (p, x, header); - NodeTraits::set_right(x, p); - NodeTraits::set_parent(p, x); - } - - static void rebalance(node_ptr p, node_ptr header) + static void rebalance_after_insertion(node_ptr header, node_ptr p) { NodeTraits::set_color(p, NodeTraits::red()); while(p != NodeTraits::get_parent(header) && NodeTraits::get_color(NodeTraits::get_parent(p)) == NodeTraits::red()){ node_ptr p_parent(NodeTraits::get_parent(p)); node_ptr p_parent_parent(NodeTraits::get_parent(p_parent)); - if(is_left_child(p_parent)){ + if(tree_algorithms::is_left_child(p_parent)){ node_ptr x = NodeTraits::get_right(p_parent_parent); if(x && NodeTraits::get_color(x) == NodeTraits::red()){ NodeTraits::set_color(p_parent, NodeTraits::black()); @@ -1532,15 +786,15 @@ class rbtree_algorithms p = p_parent_parent; } else { - if(!is_left_child(p)){ + if(!tree_algorithms::is_left_child(p)){ p = p_parent; - rotate_left(p, header); + tree_algorithms::rotate_left(p, header); } node_ptr new_p_parent(NodeTraits::get_parent(p)); node_ptr new_p_parent_parent(NodeTraits::get_parent(new_p_parent)); NodeTraits::set_color(new_p_parent, NodeTraits::black()); NodeTraits::set_color(new_p_parent_parent, NodeTraits::red()); - rotate_right(new_p_parent_parent, header); + tree_algorithms::rotate_right(new_p_parent_parent, header); } } else{ @@ -1552,15 +806,15 @@ class rbtree_algorithms p = p_parent_parent; } else{ - if(is_left_child(p)){ + if(tree_algorithms::is_left_child(p)){ p = p_parent; - rotate_right(p, header); + tree_algorithms::rotate_right(p, header); } node_ptr new_p_parent(NodeTraits::get_parent(p)); node_ptr new_p_parent_parent(NodeTraits::get_parent(new_p_parent)); NodeTraits::set_color(new_p_parent, NodeTraits::black()); NodeTraits::set_color(new_p_parent_parent, NodeTraits::red()); - rotate_left(new_p_parent_parent, header); + tree_algorithms::rotate_left(new_p_parent_parent, header); } } } diff --git a/include/boost/intrusive/set.hpp b/include/boost/intrusive/set.hpp index 9b8fcca..a622cf9 100644 --- a/include/boost/intrusive/set.hpp +++ b/include/boost/intrusive/set.hpp @@ -109,7 +109,7 @@ class set_impl , const value_compare &cmp = value_compare() , const value_traits &v_traits = value_traits()) : tree_(true, b, e, cmp, v_traits) - { insert(b, e); } + {} //! Effects: Detaches all elements from this. The objects in the set //! are not deleted (i.e. no destructors are called). @@ -930,6 +930,19 @@ class set_impl static void init_node(reference value) { tree_type::init_node(value); } + //! Effects: Unlinks the leftmost node from the tree. + //! + //! Complexity: Average complexity is constant time. + //! + //! Throws: Nothing. + //! + //! Notes: This function breaks the tree and the tree can + //! only be used for more unlink_leftmost_without_rebalance calls. + //! This function is normally used to achieve a step by step + //! controlled destruction of the tree. + pointer unlink_leftmost_without_rebalance() + { return tree_.unlink_leftmost_without_rebalance(); } + //! Requires: replace_this must be a valid iterator of *this //! and with_this must not be inserted in any tree. //! @@ -1151,7 +1164,7 @@ class multiset_impl //! [b, e). //! //! Complexity: Linear in N if [b, e) is already sorted using - //! comp and otherwise N * log N, where N is last ­ first. + //! comp and otherwise N * log N, where N is the distance between first and last //! //! Throws: If value_traits::node_traits::node //! constructor throws (this does not happen with predefined Boost.Intrusive hooks) @@ -1379,7 +1392,7 @@ class multiset_impl //! Note: Does not affect the validity of iterators and references. //! No copy-constructors are called. iterator insert(reference value) - { return tree_.insert_equal_upper_bound(value); } + { return tree_.insert_equal(value); } //! Requires: value must be an lvalue //! @@ -1889,6 +1902,19 @@ class multiset_impl static void init_node(reference value) { tree_type::init_node(value); } + //! Effects: Unlinks the leftmost node from the tree. + //! + //! Complexity: Average complexity is constant time. + //! + //! Throws: Nothing. + //! + //! Notes: This function breaks the tree and the tree can + //! only be used for more unlink_leftmost_without_rebalance calls. + //! This function is normally used to achieve a step by step + //! controlled destruction of the tree. + pointer unlink_leftmost_without_rebalance() + { return tree_.unlink_leftmost_without_rebalance(); } + //! Requires: replace_this must be a valid iterator of *this //! and with_this must not be inserted in any tree. //! diff --git a/include/boost/intrusive/set_hook.hpp b/include/boost/intrusive/set_hook.hpp index df0c164..aea5398 100644 --- a/include/boost/intrusive/set_hook.hpp +++ b/include/boost/intrusive/set_hook.hpp @@ -26,10 +26,10 @@ namespace boost { namespace intrusive { /// @cond -template +template struct get_set_node_algo { - typedef rbtree_algorithms > type; + typedef rbtree_algorithms > type; }; /// @endcond @@ -38,16 +38,17 @@ struct get_set_node_algo #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED template #else -template +template #endif struct make_set_base_hook { /// @cond typedef typename pack_options - < hook_defaults, O1, O2, O3>::type packed_options; + < hook_defaults, O1, O2, O3, O4>::type packed_options; typedef detail::generic_hook - < get_set_node_algo + < get_set_node_algo , typename packed_options::tag , packed_options::link_mode , detail::SetBaseHook @@ -72,10 +73,10 @@ struct make_set_base_hook #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED template #else -template +template #endif class set_base_hook - : public make_set_base_hook::type + : public make_set_base_hook::type { #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED //! Effects: If link_mode is \c auto_unlink or \c safe_link @@ -149,16 +150,17 @@ class set_base_hook #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED template #else -template +template #endif struct make_set_member_hook { /// @cond typedef typename pack_options - < hook_defaults, O1, O2, O3>::type packed_options; + < hook_defaults, O1, O2, O3, O4>::type packed_options; typedef detail::generic_hook - < get_set_node_algo + < get_set_node_algo , member_tag , packed_options::link_mode , detail::NoBaseHook @@ -178,10 +180,10 @@ struct make_set_member_hook #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED template #else -template +template #endif class set_member_hook - : public make_set_member_hook::type + : public make_set_member_hook::type { #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED //! Effects: If link_mode is \c auto_unlink or \c safe_link diff --git a/include/boost/intrusive/slist.hpp b/include/boost/intrusive/slist.hpp index 91ac7cb..dbfb503 100644 --- a/include/boost/intrusive/slist.hpp +++ b/include/boost/intrusive/slist.hpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -906,7 +907,7 @@ class slist_impl void dispose_and_assign(Disposer disposer, Iterator b, Iterator e) { this->clear_and_dispose(disposer); - this->insert_after(before_begin(), b, e, disposer); + this->insert_after(this->before_begin(), b, e, disposer); } //! Requires: prev is an iterator to an element or x.end()/x.before_begin() in x. diff --git a/include/boost/intrusive/splay_set.hpp b/include/boost/intrusive/splay_set.hpp new file mode 100644 index 0000000..fb803c3 --- /dev/null +++ b/include/boost/intrusive/splay_set.hpp @@ -0,0 +1,2183 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2007 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +#ifndef BOOST_INTRUSIVE_SPLAY_SET_HPP +#define BOOST_INTRUSIVE_SPLAY_SET_HPP + +#include +#include +#include +#include + +namespace boost { +namespace intrusive { + +//! The class template splay_set is an intrusive container, that mimics most of +//! the interface of std::set as described in the C++ standard. +//! +//! The template parameter \c T is the type to be managed by the container. +//! The user can specify additional options and if no options are provided +//! default options are used. +//! +//! The container supports the following options: +//! \c base_hook<>/member_hook<>/value_traits<>, +//! \c constant_time_size<>, \c size_type<> and +//! \c compare<>. +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +class splay_set_impl +{ + /// @cond + typedef splaytree_impl tree_type; + //! This class is + //! non-copyable + splay_set_impl (const splay_set_impl&); + + //! This class is + //! non-assignable + splay_set_impl &operator =(const splay_set_impl&); + + typedef tree_type implementation_defined; + /// @endcond + + public: + typedef typename implementation_defined::value_type value_type; + typedef typename implementation_defined::value_traits value_traits; + typedef typename implementation_defined::pointer pointer; + typedef typename implementation_defined::const_pointer const_pointer; + typedef typename implementation_defined::reference reference; + typedef typename implementation_defined::const_reference const_reference; + typedef typename implementation_defined::difference_type difference_type; + typedef typename implementation_defined::size_type size_type; + typedef typename implementation_defined::value_compare value_compare; + typedef typename implementation_defined::key_compare key_compare; + typedef typename implementation_defined::iterator iterator; + typedef typename implementation_defined::const_iterator const_iterator; + typedef typename implementation_defined::reverse_iterator reverse_iterator; + typedef typename implementation_defined::const_reverse_iterator const_reverse_iterator; + typedef typename implementation_defined::insert_commit_data insert_commit_data; + typedef typename implementation_defined::node_traits node_traits; + typedef typename implementation_defined::node node; + typedef typename implementation_defined::node_ptr node_ptr; + typedef typename implementation_defined::const_node_ptr const_node_ptr; + typedef typename implementation_defined::node_algorithms node_algorithms; + + /// @cond + private: + tree_type tree_; + /// @endcond + + public: + //! Effects: Constructs an empty splay_set. + //! + //! Complexity: Constant. + //! + //! Throws: If value_traits::node_traits::node + //! constructor throws (this does not happen with predefined Boost.Intrusive hooks) + //! or the copy constructor of the value_compare object throws. + splay_set_impl( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : tree_(cmp, v_traits) + {} + + //! Requires: Dereferencing iterator must yield an lvalue of type value_type. + //! cmp must be a comparison function that induces a strict weak ordering. + //! + //! Effects: Constructs an empty splay_set and inserts elements from + //! [b, e). + //! + //! Complexity: Linear in N if [b, e) is already sorted using + //! comp and otherwise N * log N, where N is std::distance(last, first). + //! + //! Throws: If value_traits::node_traits::node + //! constructor throws (this does not happen with predefined Boost.Intrusive hooks) + //! or the copy constructor/operator() of the value_compare object throws. + template + splay_set_impl( Iterator b, Iterator e + , const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : tree_(true, b, e, cmp, v_traits) + {} + + //! Effects: Detaches all elements from this. The objects in the splay_set + //! are not deleted (i.e. no destructors are called). + //! + //! Complexity: O(log(size()) + size()) if it's a safe-mode or auto-unlink + //! value. Otherwise constant. + //! + //! Throws: Nothing. + ~splay_set_impl() + {} + + //! Effects: Returns an iterator pointing to the beginning of the splay_set. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + iterator begin() + { return tree_.begin(); } + + //! Effects: Returns a const_iterator pointing to the beginning of the splay_set. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_iterator begin() const + { return tree_.begin(); } + + //! Effects: Returns a const_iterator pointing to the beginning of the splay_set. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_iterator cbegin() const + { return tree_.cbegin(); } + + //! Effects: Returns an iterator pointing to the end of the splay_set. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + iterator end() + { return tree_.end(); } + + //! Effects: Returns a const_iterator pointing to the end of the splay_set. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_iterator end() const + { return tree_.end(); } + + //! Effects: Returns a const_iterator pointing to the end of the splay_set. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_iterator cend() const + { return tree_.cend(); } + + //! Effects: Returns a reverse_iterator pointing to the beginning of the + //! reversed splay_set. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + reverse_iterator rbegin() + { return tree_.rbegin(); } + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed splay_set. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_reverse_iterator rbegin() const + { return tree_.rbegin(); } + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed splay_set. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_reverse_iterator crbegin() const + { return tree_.crbegin(); } + + //! Effects: Returns a reverse_iterator pointing to the end + //! of the reversed splay_set. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + reverse_iterator rend() + { return tree_.rend(); } + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed splay_set. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_reverse_iterator rend() const + { return tree_.rend(); } + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed splay_set. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_reverse_iterator crend() const + { return tree_.crend(); } + + //! Precondition: end_iterator must be a valid end iterator + //! of splay_set. + //! + //! Effects: Returns a const reference to the splay_set associated to the end iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + static splay_set_impl &container_from_end_iterator(iterator end_iterator) + { + return *detail::parent_from_member + ( &tree_type::container_from_end_iterator(end_iterator) + , &splay_set_impl::tree_); + } + + //! Precondition: end_iterator must be a valid end const_iterator + //! of splay_set. + //! + //! Effects: Returns a const reference to the splay_set associated to the end iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + static const splay_set_impl &container_from_end_iterator(const_iterator end_iterator) + { + return *detail::parent_from_member + ( &tree_type::container_from_end_iterator(end_iterator) + , &splay_set_impl::tree_); + } + + //! Effects: Returns the key_compare object used by the splay_set. + //! + //! Complexity: Constant. + //! + //! Throws: If key_compare copy-constructor throws. + key_compare key_comp() const + { return tree_.value_comp(); } + + //! Effects: Returns the value_compare object used by the splay_set. + //! + //! Complexity: Constant. + //! + //! Throws: If value_compare copy-constructor throws. + value_compare value_comp() const + { return tree_.value_comp(); } + + //! Effects: Returns true is the container is empty. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + bool empty() const + { return tree_.empty(); } + + //! Effects: Returns the number of elements stored in the splay_set. + //! + //! Complexity: Linear to elements contained in *this if, + //! constant-time size option is enabled. Constant-time otherwise. + //! + //! Throws: Nothing. + size_type size() const + { return tree_.size(); } + + //! Effects: Swaps the contents of two splay_sets. + //! + //! Complexity: Constant. + //! + //! Throws: If the swap() call for the comparison functor + //! found using ADL throws. Strong guarantee. + void swap(splay_set_impl& other) + { tree_.swap(other.tree_); } + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! + //! Effects: Erases all the elements from *this + //! calling Disposer::operator()(pointer), clones all the + //! elements from src calling Cloner::operator()(const_reference ) + //! and inserts them on *this. + //! + //! If cloner throws, all cloned elements are unlinked and disposed + //! calling Disposer::operator()(pointer). + //! + //! Complexity: Linear to erased plus inserted elements. + //! + //! Throws: If cloner throws. + template + void clone_from(const splay_set_impl &src, Cloner cloner, Disposer disposer) + { tree_.clone_from(src.tree_, cloner, disposer); } + + //! Requires: value must be an lvalue + //! + //! Effects: Tries to inserts value into the splay_set. + //! + //! Returns: If the value + //! is not already present inserts it and returns a pair containing the + //! iterator to the new value and true. If there is an equivalent value + //! returns a pair containing an iterator to the already present value + //! and false. + //! + //! Complexity: Average complexity for insert element is at + //! most logarithmic. + //! + //! Throws: If the internal value_compare ordering function throws. Strong guarantee. + //! + //! Note: Does not affect the validity of iterators and references. + //! No copy-constructors are called. + std::pair insert(reference value) + { return tree_.insert_unique(value); } + + //! Requires: value must be an lvalue + //! + //! Effects: Tries to to insert x into the splay_set, using "hint" + //! as a hint to where it will be inserted. + //! + //! Returns: An iterator that points to the position where the + //! new element was inserted into the splay_set. + //! + //! Complexity: Logarithmic in general, but it's amortized + //! constant time if t is inserted immediately before hint. + //! + //! Throws: If the internal value_compare ordering function throws. Strong guarantee. + //! + //! Note: Does not affect the validity of iterators and references. + //! No copy-constructors are called. + iterator insert(const_iterator hint, reference value) + { return tree_.insert_unique(hint, value); } + + //! Requires: key_value_comp must be a comparison function that induces + //! the same strict weak ordering as value_compare. The difference is that + //! key_value_comp compares an arbitrary key with the contained values. + //! + //! Effects: Checks if a value can be inserted in the splay_set, using + //! a user provided key instead of the value itself. + //! + //! Returns: If there is an equivalent value + //! returns a pair containing an iterator to the already present value + //! and false. If the value can be inserted returns true in the returned + //! pair boolean and fills "commit_data" that is meant to be used with + //! the "insert_commit" function. + //! + //! Complexity: Average complexity is at most logarithmic. + //! + //! Throws: If the key_value_comp ordering function throws. Strong guarantee. + //! + //! Notes: This function is used to improve performance when constructing + //! a value_type is expensive: if there is an equivalent value + //! the constructed object must be discarded. Many times, the part of the + //! node that is used to impose the order is much cheaper to construct + //! than the value_type and this function offers the possibility to use that + //! part to check if the insertion will be successful. + //! + //! If the check is successful, the user can construct the value_type and use + //! "insert_commit" to insert the object in constant-time. This gives a total + //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)). + //! + //! "commit_data" remains valid for a subsequent "insert_commit" only if no more + //! objects are inserted or erased from the splay_set. + template + std::pair insert_check + (const KeyType &key, KeyValueCompare key_value_comp, insert_commit_data &commit_data) + { return tree_.insert_unique_check(key, key_value_comp, commit_data); } + + //! Requires: key_value_comp must be a comparison function that induces + //! the same strict weak ordering as value_compare. The difference is that + //! key_value_comp compares an arbitrary key with the contained values. + //! + //! Effects: Checks if a value can be inserted in the splay_set, using + //! a user provided key instead of the value itself, using "hint" + //! as a hint to where it will be inserted. + //! + //! Returns: If there is an equivalent value + //! returns a pair containing an iterator to the already present value + //! and false. If the value can be inserted returns true in the returned + //! pair boolean and fills "commit_data" that is meant to be used with + //! the "insert_commit" function. + //! + //! Complexity: Logarithmic in general, but it's amortized + //! constant time if t is inserted immediately before hint. + //! + //! Throws: If the key_value_comp ordering function throws. Strong guarantee. + //! + //! Notes: This function is used to improve performance when constructing + //! a value_type is expensive: if there is an equivalent value + //! the constructed object must be discarded. Many times, the part of the + //! constructing that is used to impose the order is much cheaper to construct + //! than the value_type and this function offers the possibility to use that key + //! to check if the insertion will be successful. + //! + //! If the check is successful, the user can construct the value_type and use + //! "insert_commit" to insert the object in constant-time. This can give a total + //! constant-time complexity to the insertion: check(O(1)) + commit(O(1)). + //! + //! "commit_data" remains valid for a subsequent "insert_commit" only if no more + //! objects are inserted or erased from the splay_set. + template + std::pair insert_check + (const_iterator hint, const KeyType &key + ,KeyValueCompare key_value_comp, insert_commit_data &commit_data) + { return tree_.insert_unique_check(hint, key, key_value_comp, commit_data); } + + //! Requires: value must be an lvalue of type value_type. commit_data + //! must have been obtained from a previous call to "insert_check". + //! No objects should have been inserted or erased from the splay_set between + //! the "insert_check" that filled "commit_data" and the call to "insert_commit". + //! + //! Effects: Inserts the value in the splay_set using the information obtained + //! from the "commit_data" that a previous "insert_check" filled. + //! + //! Returns: An iterator to the newly inserted object. + //! + //! Complexity: Constant time. + //! + //! Throws: Nothing. + //! + //! Notes: This function has only sense if a "insert_check" has been + //! previously executed to fill "commit_data". No value should be inserted or + //! erased between the "insert_check" and "insert_commit" calls. + iterator insert_commit(reference value, const insert_commit_data &commit_data) + { return tree_.insert_unique_commit(value, commit_data); } + + //! Requires: Dereferencing iterator must yield an lvalue + //! of type value_type. + //! + //! Effects: Inserts a range into the splay_set. + //! + //! Complexity: Insert range is in general O(N * log(N)), where N is the + //! size of the range. However, it is linear in N if the range is already sorted + //! by value_comp(). + //! + //! Throws: If the internal value_compare ordering function throws. Basic guarantee. + //! + //! Note: Does not affect the validity of iterators and references. + //! No copy-constructors are called. + template + void insert(Iterator b, Iterator e) + { tree_.insert_unique(b, e); } + + //! Effects: Erases the element pointed to by pos. + //! + //! Complexity: Average complexity is constant time. + //! + //! Returns: An iterator to the element after the erased element. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + iterator erase(iterator i) + { return tree_.erase(i); } + + //! Effects: Erases the range pointed to by b end e. + //! + //! Complexity: Average complexity for erase range is at most + //! O(log(size() + N)), where N is the number of elements in the range. + //! + //! Returns: An iterator to the element after the erased elements. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + iterator erase(iterator b, iterator e) + { return tree_.erase(b, e); } + + //! Effects: Erases all the elements with the given value. + //! + //! Returns: The number of erased elements. + //! + //! Complexity: O(log(size()) + this->count(value)). + //! + //! Throws: If the internal value_compare ordering function throws. Basic guarantee. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + size_type erase(const_reference value) + { return tree_.erase(value); } + + //! Effects: Erases all the elements that compare equal with + //! the given key and the given comparison functor. + //! + //! Returns: The number of erased elements. + //! + //! Complexity: O(log(size() + this->count(key, comp)). + //! + //! Throws: If the comp ordering function throws. Basic guarantee. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + template + size_type erase(const KeyType& key, KeyValueCompare comp) + { return tree_.erase(key, comp); } + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! + //! Effects: Erases the element pointed to by pos. + //! Disposer::operator()(pointer) is called for the removed element. + //! + //! Complexity: Average complexity for erase element is constant time. + //! + //! Returns: An iterator to the element after the erased element. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators + //! to the erased elements. + template + iterator erase_and_dispose(iterator i, Disposer disposer) + { return tree_.erase_and_dispose(i, disposer); } + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! + //! Effects: Erases the range pointed to by b end e. + //! Disposer::operator()(pointer) is called for the removed elements. + //! + //! Complexity: Average complexity for erase range is at most + //! O(log(size() + N)), where N is the number of elements in the range. + //! + //! Returns: An iterator to the element after the erased elements. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators + //! to the erased elements. + template + iterator erase_and_dispose(iterator b, iterator e, Disposer disposer) + { return tree_.erase_and_dispose(b, e, disposer); } + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! + //! Effects: Erases all the elements with the given value. + //! Disposer::operator()(pointer) is called for the removed elements. + //! + //! Throws: If the internal value_compare ordering function throws. + //! + //! Complexity: O(log(size() + this->count(value)). Basic guarantee. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + template + size_type erase_and_dispose(const_reference value, Disposer disposer) + { return tree_.erase_and_dispose(value, disposer); } + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! + //! Effects: Erases all the elements with the given key. + //! according to the comparison functor "comp". + //! Disposer::operator()(pointer) is called for the removed elements. + //! + //! Returns: The number of erased elements. + //! + //! Complexity: O(log(size() + this->count(key, comp)). + //! + //! Throws: If comp ordering function throws. Basic guarantee. + //! + //! Note: Invalidates the iterators + //! to the erased elements. + template + size_type erase_and_dispose(const KeyType& key, KeyValueCompare comp, Disposer disposer) + { return tree_.erase_and_dispose(key, comp, disposer); } + + //! Effects: Erases all the elements of the container. + //! + //! Complexity: Linear to the number of elements on the container. + //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + void clear() + { return tree_.clear(); } + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! + //! Effects: Erases all the elements of the container. + //! + //! Complexity: Linear to the number of elements on the container. + //! Disposer::operator()(pointer) is called for the removed elements. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + template + void clear_and_dispose(Disposer disposer) + { return tree_.clear_and_dispose(disposer); } + + //! Effects: Returns the number of contained elements with the given key + //! + //! Complexity: Logarithmic to the number of elements contained plus lineal + //! to number of objects with the given key. + //! + //! Throws: If the internal value_compare ordering function throws. + size_type count(const_reference value) + { return tree_.find(value) != end(); } + + //! Effects: Returns the number of contained elements with the same key + //! compared with the given comparison functor. + //! + //! Complexity: Logarithmic to the number of elements contained plus lineal + //! to number of objects with the given key. + //! + //! Throws: If comp ordering function throws. + template + size_type count(const KeyType& key, KeyValueCompare comp) + { return tree_.find(key, comp) != end(); } + + //! Effects: Returns the number of contained elements with the given key + //! + //! Complexity: Logarithmic to the number of elements contained plus lineal + //! to number of objects with the given key. + //! + //! Throws: If the internal value_compare ordering function throws. + size_type count_dont_splay(const_reference value)const + { return tree_.find_dont_splay(value) != end(); } + + //! Effects: Returns the number of contained elements with the same key + //! compared with the given comparison functor. + //! + //! Complexity: Logarithmic to the number of elements contained plus lineal + //! to number of objects with the given key. + //! + //! Throws: If comp ordering function throws. + template + size_type count_dont_splay(const KeyType& key, KeyValueCompare comp)const + { return tree_.find_dont_splay(key, comp) != end(); } + + //! Effects: Returns an iterator to the first element whose + //! key is not less than k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If the internal value_compare ordering function throws. + iterator lower_bound(const_reference value) + { return tree_.lower_bound(value); } + + //! Requires: comp must imply the same element order as + //! value_compare. Usually key is the part of the value_type + //! that is used in the ordering functor. + //! + //! Effects: Returns an iterator to the first element whose + //! key according to the comparison functor is not less than k or + //! end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If comp ordering function throws. + //! + //! Note: This function is used when constructing a value_type + //! is expensive and the value_type can be compared with a cheaper + //! key type. Usually this key is part of the value_type. + template + iterator lower_bound(const KeyType& key, KeyValueCompare comp) + { return tree_.lower_bound(key, comp); } + + //! Effects: Returns a const iterator to the first element whose + //! key is not less than k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If the internal value_compare ordering function throws. + const_iterator lower_bound_dont_splay(const_reference value) const + { return tree_.lower_bound_dont_splay(value); } + + //! Requires: comp must imply the same element order as + //! value_compare. Usually key is the part of the value_type + //! that is used in the ordering functor. + //! + //! Effects: Returns a const_iterator to the first element whose + //! key according to the comparison functor is not less than k or + //! end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If comp ordering function throws. + //! + //! Note: This function is used when constructing a value_type + //! is expensive and the value_type can be compared with a cheaper + //! key type. Usually this key is part of the value_type. + template + const_iterator lower_bound_dont_splay(const KeyType& key, KeyValueCompare comp) const + { return tree_.lower_bound_dont_splay(key, comp); } + + //! Effects: Returns an iterator to the first element whose + //! key is greater than k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If the internal value_compare ordering function throws. + iterator upper_bound(const_reference value) + { return tree_.upper_bound(value); } + + //! Requires: comp must imply the same element order as + //! value_compare. Usually key is the part of the value_type + //! that is used in the ordering functor. + //! + //! Effects: Returns an iterator to the first element whose + //! key according to the comparison functor is greater than key or + //! end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If comp ordering function throws. + //! + //! Note: This function is used when constructing a value_type + //! is expensive and the value_type can be compared with a cheaper + //! key type. Usually this key is part of the value_type. + template + iterator upper_bound(const KeyType& key, KeyValueCompare comp) + { return tree_.upper_bound(key, comp); } + + //! Effects: Returns an iterator to the first element whose + //! key is greater than k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If the internal value_compare ordering function throws. + const_iterator upper_bound_dont_splay(const_reference value) const + { return tree_.upper_bound_dont_splay(value); } + + //! Requires: comp must imply the same element order as + //! value_compare. Usually key is the part of the value_type + //! that is used in the ordering functor. + //! + //! Effects: Returns a const_iterator to the first element whose + //! key according to the comparison functor is greater than key or + //! end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If comp ordering function throws. + //! + //! Note: This function is used when constructing a value_type + //! is expensive and the value_type can be compared with a cheaper + //! key type. Usually this key is part of the value_type. + template + const_iterator upper_bound_dont_splay(const KeyType& key, KeyValueCompare comp) const + { return tree_.upper_bound_dont_splay(key, comp); } + + //! Effects: Finds an iterator to the first element whose value is + //! "value" or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If the internal value_compare ordering function throws. + iterator find(const_reference value) + { return tree_.find(value); } + + //! Requires: comp must imply the same element order as + //! value_compare. Usually key is the part of the value_type + //! that is used in the ordering functor. + //! + //! Effects: Finds an iterator to the first element whose key is + //! "key" according to the comparison functor or end() if that element + //! does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If comp ordering function throws. + //! + //! Note: This function is used when constructing a value_type + //! is expensive and the value_type can be compared with a cheaper + //! key type. Usually this key is part of the value_type. + template + iterator find(const KeyType& key, KeyValueCompare comp) + { return tree_.find(key, comp); } + + //! Effects: Finds a const_iterator to the first element whose value is + //! "value" or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If the internal value_compare ordering function throws. + const_iterator find_dont_splay(const_reference value) const + { return tree_.find_dont_splay(value); } + + //! Requires: comp must imply the same element order as + //! value_compare. Usually key is the part of the value_type + //! that is used in the ordering functor. + //! + //! Effects: Finds a const_iterator to the first element whose key is + //! "key" according to the comparison functor or end() if that element + //! does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If comp ordering function throws. + //! + //! Note: This function is used when constructing a value_type + //! is expensive and the value_type can be compared with a cheaper + //! key type. Usually this key is part of the value_type. + template + const_iterator find_dont_splay(const KeyType& key, KeyValueCompare comp) const + { return tree_.find_dont_splay(key, comp); } + + //! Effects: Finds a range containing all elements whose key is k or + //! an empty range that indicates the position where those elements would be + //! if they there is no elements with key k. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If the internal value_compare ordering function throws. + std::pair equal_range(const_reference value) + { return tree_.equal_range(value); } + + //! Requires: comp must imply the same element order as + //! value_compare. Usually key is the part of the value_type + //! that is used in the ordering functor. + //! + //! Effects: Finds a range containing all elements whose key is k + //! according to the comparison functor or an empty range + //! that indicates the position where those elements would be + //! if they there is no elements with key k. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If comp ordering function throws. + //! + //! Note: This function is used when constructing a value_type + //! is expensive and the value_type can be compared with a cheaper + //! key type. Usually this key is part of the value_type. + template + std::pair equal_range(const KeyType& key, KeyValueCompare comp) + { return tree_.equal_range(key, comp); } + + //! Effects: Finds a range containing all elements whose key is k or + //! an empty range that indicates the position where those elements would be + //! if they there is no elements with key k. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If the internal value_compare ordering function throws. + std::pair + equal_range_dont_splay(const_reference value) const + { return tree_.equal_range_dont_splay(value); } + + //! Requires: comp must imply the same element order as + //! value_compare. Usually key is the part of the value_type + //! that is used in the ordering functor. + //! + //! Effects: Finds a range containing all elements whose key is k + //! according to the comparison functor or an empty range + //! that indicates the position where those elements would be + //! if they there is no elements with key k. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If comp ordering function throws. + //! + //! Note: This function is used when constructing a value_type + //! is expensive and the value_type can be compared with a cheaper + //! key type. Usually this key is part of the value_type. + template + std::pair + equal_range_dont_splay(const KeyType& key, KeyValueCompare comp) const + { return tree_.equal_range_dont_splay(key, comp); } + + //! Requires: value must be an lvalue and shall be in a splay_set of + //! appropriate type. Otherwise the behavior is undefined. + //! + //! Effects: Returns: a valid iterator i belonging to the splay_set + //! that points to the value + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Note: This static function is available only if the value traits + //! is stateless. + static iterator s_iterator_to(reference value) + { return tree_type::s_iterator_to(value); } + + //! Requires: value must be an lvalue and shall be in a splay_set of + //! appropriate type. Otherwise the behavior is undefined. + //! + //! Effects: Returns: a valid const_iterator i belonging to the + //! splay_set that points to the value + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Note: This static function is available only if the value traits + //! is stateless. + static const_iterator s_iterator_to(const_reference value) + { return tree_type::s_iterator_to(value); } + + //! Requires: value must be an lvalue and shall be in a splay_set of + //! appropriate type. Otherwise the behavior is undefined. + //! + //! Effects: Returns: a valid iterator i belonging to the splay_set + //! that points to the value + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + iterator iterator_to(reference value) + { return tree_.iterator_to(value); } + + //! Requires: value must be an lvalue and shall be in a splay_set of + //! appropriate type. Otherwise the behavior is undefined. + //! + //! Effects: Returns: a valid const_iterator i belonging to the + //! splay_set that points to the value + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_iterator iterator_to(const_reference value) const + { return tree_.iterator_to(value); } + + //! Requires: value shall not be in a splay_set/multisplay_set. + //! + //! Effects: init_node puts the hook of a value in a well-known default + //! state. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant time. + //! + //! Note: This function puts the hook in the well-known default state + //! used by auto_unlink and safe hooks. + static void init_node(reference value) + { tree_type::init_node(value); } + + //! Effects: Unlinks the leftmost node from the tree. + //! + //! Complexity: Average complexity is constant time. + //! + //! Throws: Nothing. + //! + //! Notes: This function breaks the tree and the tree can + //! only be used for more unlink_leftmost_without_rebalance calls. + //! This function is normally used to achieve a step by step + //! controlled destruction of the tree. + pointer unlink_leftmost_without_rebalance() + { return tree_.unlink_leftmost_without_rebalance(); } + + //! Requires: replace_this must be a valid iterator of *this + //! and with_this must not be inserted in any tree. + //! + //! Effects: Replaces replace_this in its position in the + //! tree with with_this. The tree does not need to be rebalanced. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Note: This function will break container ordering invariants if + //! with_this is not equivalent to *replace_this according to the + //! ordering rules. This function is faster than erasing and inserting + //! the node, since no rebalancing or comparison is needed. + void replace_node(iterator replace_this, reference with_this) + { tree_.replace_node(replace_this, with_this); } + + //! Requires: i must be a valid iterator of *this. + //! + //! Effects: Rearranges the splay set so that the element pointed by i + //! is placed as the root of the tree, improving future searches of this value. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + void splay_up(iterator i) + { tree_.splay_up(i); } + + //! Effects: Rearranges the splay set so that if *this stores an element + //! with a key equivalent to value the element is placed as the root of the + //! tree. If the element is not present returns the last node compared with the key. + //! If the tree is empty, end() is returned. + //! + //! Complexity: Logarithmic. + //! + //! Returns: An iterator to the new root of the tree, end() if the tree is empty. + //! + //! Throws: If the comparison functor throws. + template + iterator splay_down(const KeyType &key, KeyNodePtrCompare comp) + { return tree_.splay_down(key, comp); } + + //! Effects: Rearranges the splay set so that if *this stores an element + //! with a key equivalent to value the element is placed as the root of the + //! tree. + //! + //! Complexity: Logarithmic. + //! + //! Returns: An iterator to the new root of the tree, end() if the tree is empty. + //! + //! Throws: If the predicate throws. + iterator splay_down(const value_type &value) + { return tree_.splay_down(value); } + + /// @cond + friend bool operator==(const splay_set_impl &x, const splay_set_impl &y) + { return x.tree_ == y.tree_; } + + friend bool operator<(const splay_set_impl &x, const splay_set_impl &y) + { return x.tree_ < y.tree_; } + /// @endcond +}; + +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +inline bool operator!= +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +(const splay_set_impl &x, const splay_set_impl &y) +#else +(const splay_set_impl &x, const splay_set_impl &y) +#endif +{ return !(x == y); } + +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +inline bool operator> +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +(const splay_set_impl &x, const splay_set_impl &y) +#else +(const splay_set_impl &x, const splay_set_impl &y) +#endif +{ return y < x; } + +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +inline bool operator<= +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +(const splay_set_impl &x, const splay_set_impl &y) +#else +(const splay_set_impl &x, const splay_set_impl &y) +#endif +{ return !(y < x); } + +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +inline bool operator>= +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +(const splay_set_impl &x, const splay_set_impl &y) +#else +(const splay_set_impl &x, const splay_set_impl &y) +#endif +{ return !(x < y); } + +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +inline void swap +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +(splay_set_impl &x, splay_set_impl &y) +#else +(splay_set_impl &x, splay_set_impl &y) +#endif +{ x.swap(y); } + +//! Helper metafunction to define a \c splay_set that yields to the same type when the +//! same options (either explicitly or implicitly) are used. +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +struct make_splay_set +{ + /// @cond + typedef splay_set_impl + < typename make_splaytree_opt::type + > implementation_defined; + /// @endcond + typedef implementation_defined type; +}; + +#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +class splay_set + : public make_splay_set::type +{ + typedef typename make_splay_set + ::type Base; + + public: + typedef typename Base::value_compare value_compare; + typedef typename Base::value_traits value_traits; + typedef typename Base::iterator iterator; + typedef typename Base::const_iterator const_iterator; + + //Assert if passed value traits are compatible with the type + BOOST_STATIC_ASSERT((detail::is_same::value)); + + splay_set( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : Base(cmp, v_traits) + {} + + template + splay_set( Iterator b, Iterator e + , const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : Base(b, e, cmp, v_traits) + {} + + static splay_set &container_from_end_iterator(iterator end_iterator) + { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static const splay_set &container_from_end_iterator(const_iterator end_iterator) + { return static_cast(Base::container_from_end_iterator(end_iterator)); } +}; + +#endif + +//! The class template splay_multiset is an intrusive container, that mimics most of +//! the interface of std::multiset as described in the C++ standard. +//! +//! The template parameter \c T is the type to be managed by the container. +//! The user can specify additional options and if no options are provided +//! default options are used. +//! +//! The container supports the following options: +//! \c base_hook<>/member_hook<>/value_traits<>, +//! \c constant_time_size<>, \c size_type<> and +//! \c compare<>. +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +class splay_multiset_impl +{ + /// @cond + typedef splaytree_impl tree_type; + + //Non-copyable and non-assignable + splay_multiset_impl (const splay_multiset_impl&); + splay_multiset_impl &operator =(const splay_multiset_impl&); + typedef tree_type implementation_defined; + /// @endcond + + public: + typedef typename implementation_defined::value_type value_type; + typedef typename implementation_defined::value_traits value_traits; + typedef typename implementation_defined::pointer pointer; + typedef typename implementation_defined::const_pointer const_pointer; + typedef typename implementation_defined::reference reference; + typedef typename implementation_defined::const_reference const_reference; + typedef typename implementation_defined::difference_type difference_type; + typedef typename implementation_defined::size_type size_type; + typedef typename implementation_defined::value_compare value_compare; + typedef typename implementation_defined::key_compare key_compare; + typedef typename implementation_defined::iterator iterator; + typedef typename implementation_defined::const_iterator const_iterator; + typedef typename implementation_defined::reverse_iterator reverse_iterator; + typedef typename implementation_defined::const_reverse_iterator const_reverse_iterator; + typedef typename implementation_defined::insert_commit_data insert_commit_data; + typedef typename implementation_defined::node_traits node_traits; + typedef typename implementation_defined::node node; + typedef typename implementation_defined::node_ptr node_ptr; + typedef typename implementation_defined::const_node_ptr const_node_ptr; + typedef typename implementation_defined::node_algorithms node_algorithms; + + /// @cond + private: + tree_type tree_; + /// @endcond + + public: + //! Effects: Constructs an empty splay_multiset. + //! + //! Complexity: Constant. + //! + //! Throws: If value_traits::node_traits::node + //! constructor throws (this does not happen with predefined Boost.Intrusive hooks) + //! or the copy constructor/operator() of the value_compare object throws. + splay_multiset_impl( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : tree_(cmp, v_traits) + {} + + //! Requires: Dereferencing iterator must yield an lvalue of type value_type. + //! cmp must be a comparison function that induces a strict weak ordering. + //! + //! Effects: Constructs an empty splay_multiset and inserts elements from + //! [b, e). + //! + //! Complexity: Linear in N if [b, e) is already sorted using + //! comp and otherwise N * log N, where N is the distance between first and last. + //! + //! Throws: If value_traits::node_traits::node + //! constructor throws (this does not happen with predefined Boost.Intrusive hooks) + //! or the copy constructor/operator() of the value_compare object throws. + template + splay_multiset_impl( Iterator b, Iterator e + , const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : tree_(false, b, e, cmp, v_traits) + {} + + //! Effects: Detaches all elements from this. The objects in the set + //! are not deleted (i.e. no destructors are called). + //! + //! Complexity: O(log(size()) + size()) if it's a safe-mode or + //! auto-unlink value. Otherwise constant. + //! + //! Throws: Nothing. + ~splay_multiset_impl() + {} + + //! Effects: Returns an iterator pointing to the beginning of the splay_multiset. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + iterator begin() + { return tree_.begin(); } + + //! Effects: Returns a const_iterator pointing to the beginning of the splay_multiset. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_iterator begin() const + { return tree_.begin(); } + + //! Effects: Returns a const_iterator pointing to the beginning of the splay_multiset. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_iterator cbegin() const + { return tree_.cbegin(); } + + //! Effects: Returns an iterator pointing to the end of the splay_multiset. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + iterator end() + { return tree_.end(); } + + //! Effects: Returns a const_iterator pointing to the end of the splay_multiset. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_iterator end() const + { return tree_.end(); } + + //! Effects: Returns a const_iterator pointing to the end of the splay_multiset. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_iterator cend() const + { return tree_.cend(); } + + //! Effects: Returns a reverse_iterator pointing to the beginning of the + //! reversed splay_multiset. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + reverse_iterator rbegin() + { return tree_.rbegin(); } + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed splay_multiset. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_reverse_iterator rbegin() const + { return tree_.rbegin(); } + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed splay_multiset. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_reverse_iterator crbegin() const + { return tree_.crbegin(); } + + //! Effects: Returns a reverse_iterator pointing to the end + //! of the reversed splay_multiset. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + reverse_iterator rend() + { return tree_.rend(); } + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed splay_multiset. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_reverse_iterator rend() const + { return tree_.rend(); } + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed splay_multiset. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_reverse_iterator crend() const + { return tree_.crend(); } + + //! Precondition: end_iterator must be a valid end iterator + //! of splay_multiset. + //! + //! Effects: Returns a const reference to the splay_multiset associated to the end iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + static splay_multiset_impl &container_from_end_iterator(iterator end_iterator) + { + return *detail::parent_from_member + ( &tree_type::container_from_end_iterator(end_iterator) + , &splay_multiset_impl::tree_); + } + + //! Precondition: end_iterator must be a valid end const_iterator + //! of splay_multiset. + //! + //! Effects: Returns a const reference to the splay_multiset associated to the end iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + static const splay_multiset_impl &container_from_end_iterator(const_iterator end_iterator) + { + return *detail::parent_from_member + ( &tree_type::container_from_end_iterator(end_iterator) + , &splay_multiset_impl::tree_); + } + + //! Effects: Returns the key_compare object used by the splay_multiset. + //! + //! Complexity: Constant. + //! + //! Throws: If key_compare copy-constructor throws. + key_compare key_comp() const + { return tree_.value_comp(); } + + //! Effects: Returns the value_compare object used by the splay_multiset. + //! + //! Complexity: Constant. + //! + //! Throws: If value_compare copy-constructor throws. + value_compare value_comp() const + { return tree_.value_comp(); } + + //! Effects: Returns true is the container is empty. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + bool empty() const + { return tree_.empty(); } + + //! Effects: Returns the number of elements stored in the splay_multiset. + //! + //! Complexity: Linear to elements contained in *this if, + //! constant-time size option is enabled. Constant-time otherwise. + //! + //! Throws: Nothing. + size_type size() const + { return tree_.size(); } + + //! Effects: Swaps the contents of two splay_multisets. + //! + //! Complexity: Constant. + //! + //! Throws: If the swap() call for the comparison functor + //! found using ADL throws. Strong guarantee. + void swap(splay_multiset_impl& other) + { tree_.swap(other.tree_); } + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! + //! Effects: Erases all the elements from *this + //! calling Disposer::operator()(pointer), clones all the + //! elements from src calling Cloner::operator()(const_reference ) + //! and inserts them on *this. + //! + //! If cloner throws, all cloned elements are unlinked and disposed + //! calling Disposer::operator()(pointer). + //! + //! Complexity: Linear to erased plus inserted elements. + //! + //! Throws: If cloner throws. Basic guarantee. + template + void clone_from(const splay_multiset_impl &src, Cloner cloner, Disposer disposer) + { tree_.clone_from(src.tree_, cloner, disposer); } + + //! Requires: value must be an lvalue + //! + //! Effects: Inserts value into the splay_multiset. + //! + //! Returns: An iterator that points to the position where the new + //! element was inserted. + //! + //! Complexity: Average complexity for insert element is at + //! most logarithmic. + //! + //! Throws: If the internal value_compare ordering function throws. Strong guarantee. + //! + //! Note: Does not affect the validity of iterators and references. + //! No copy-constructors are called. + iterator insert(reference value) + { return tree_.insert_equal(this->end(), value); } + + //! Requires: value must be an lvalue + //! + //! Effects: Inserts x into the splay_multiset, using pos as a hint to + //! where it will be inserted. + //! + //! Returns: An iterator that points to the position where the new + //! element was inserted. + //! + //! Complexity: Logarithmic in general, but it is amortized + //! constant time if t is inserted immediately before hint. + //! + //! Throws: If the internal value_compare ordering function throws. Strong guarantee. + //! + //! Note: Does not affect the validity of iterators and references. + //! No copy-constructors are called. + iterator insert(const_iterator hint, reference value) + { return tree_.insert_equal(hint, value); } + + //! Requires: Dereferencing iterator must yield an lvalue + //! of type value_type. + //! + //! Effects: Inserts a range into the splay_multiset. + //! + //! Returns: An iterator that points to the position where the new + //! element was inserted. + //! + //! Complexity: Insert range is in general O(N * log(N)), where N is the + //! size of the range. However, it is linear in N if the range is already sorted + //! by value_comp(). + //! + //! Throws: If the internal value_compare ordering function throws. Basic guarantee. + //! + //! Note: Does not affect the validity of iterators and references. + //! No copy-constructors are called. + template + void insert(Iterator b, Iterator e) + { tree_.insert_equal(b, e); } + + //! Effects: Erases the element pointed to by pos. + //! + //! Complexity: Average complexity is constant time. + //! + //! Returns: An iterator to the element after the erased element. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + iterator erase(iterator i) + { return tree_.erase(i); } + + //! Effects: Erases the range pointed to by b end e. + //! + //! Returns: An iterator to the element after the erased elements. + //! + //! Complexity: Average complexity for erase range is at most + //! O(log(size() + N)), where N is the number of elements in the range. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + iterator erase(iterator b, iterator e) + { return tree_.erase(b, e); } + + //! Effects: Erases all the elements with the given value. + //! + //! Returns: The number of erased elements. + //! + //! Complexity: O(log(size() + this->count(value)). + //! + //! Throws: If the internal value_compare ordering function throws. Basic guarantee. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + size_type erase(const_reference value) + { return tree_.erase(value); } + + //! Effects: Erases all the elements that compare equal with + //! the given key and the given comparison functor. + //! + //! Returns: The number of erased elements. + //! + //! Complexity: O(log(size() + this->count(key, comp)). + //! + //! Throws: If comp ordering function throws. Basic guarantee. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + template + size_type erase(const KeyType& key, KeyValueCompare comp) + { return tree_.erase(key, comp); } + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! + //! Returns: An iterator to the element after the erased element. + //! + //! Effects: Erases the element pointed to by pos. + //! Disposer::operator()(pointer) is called for the removed element. + //! + //! Complexity: Average complexity for erase element is constant time. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators + //! to the erased elements. + template + iterator erase_and_dispose(iterator i, Disposer disposer) + { return tree_.erase_and_dispose(i, disposer); } + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! + //! Returns: An iterator to the element after the erased elements. + //! + //! Effects: Erases the range pointed to by b end e. + //! Disposer::operator()(pointer) is called for the removed elements. + //! + //! Complexity: Average complexity for erase range is at most + //! O(log(size() + N)), where N is the number of elements in the range. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators + //! to the erased elements. + template + iterator erase_and_dispose(iterator b, iterator e, Disposer disposer) + { return tree_.erase_and_dispose(b, e, disposer); } + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! + //! Effects: Erases all the elements with the given value. + //! Disposer::operator()(pointer) is called for the removed elements. + //! + //! Returns: The number of erased elements. + //! + //! Complexity: O(log(size() + this->count(value)). + //! + //! Throws: If the internal value_compare ordering function throws. Basic guarantee. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + template + size_type erase_and_dispose(const_reference value, Disposer disposer) + { return tree_.erase_and_dispose(value, disposer); } + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! + //! Effects: Erases all the elements with the given key. + //! according to the comparison functor "comp". + //! Disposer::operator()(pointer) is called for the removed elements. + //! + //! Returns: The number of erased elements. + //! + //! Complexity: O(log(size() + this->count(key, comp)). + //! + //! Throws: If comp ordering function throws. Basic guarantee. + //! + //! Note: Invalidates the iterators + //! to the erased elements. + template + size_type erase_and_dispose(const KeyType& key, KeyValueCompare comp, Disposer disposer) + { return tree_.erase_and_dispose(key, comp, disposer); } + + //! Effects: Erases all the elements of the container. + //! + //! Complexity: Linear to the number of elements on the container. + //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + void clear() + { return tree_.clear(); } + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! + //! Effects: Erases all the elements of the container. + //! + //! Complexity: Linear to the number of elements on the container. + //! Disposer::operator()(pointer) is called for the removed elements. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + template + void clear_and_dispose(Disposer disposer) + { return tree_.clear_and_dispose(disposer); } + + //! Effects: Returns the number of contained elements with the given key + //! + //! Complexity: Logarithmic to the number of elements contained plus lineal + //! to number of objects with the given key. + //! + //! Throws: If the internal value_compare ordering function throws. + size_type count(const_reference value) + { return tree_.count(value); } + + //! Effects: Returns the number of contained elements with the same key + //! compared with the given comparison functor. + //! + //! Complexity: Logarithmic to the number of elements contained plus lineal + //! to number of objects with the given key. + //! + //! Throws: If comp ordering function throws. + template + size_type count(const KeyType& key, KeyValueCompare comp) + { return tree_.count(key, comp); } + + //! Effects: Returns the number of contained elements with the given key + //! + //! Complexity: Logarithmic to the number of elements contained plus lineal + //! to number of objects with the given key. + //! + //! Throws: If the internal value_compare ordering function throws. + size_type count_dont_splay(const_reference value) const + { return tree_.count_dont_splay(value); } + + //! Effects: Returns the number of contained elements with the same key + //! compared with the given comparison functor. + //! + //! Complexity: Logarithmic to the number of elements contained plus lineal + //! to number of objects with the given key. + //! + //! Throws: If comp ordering function throws. + template + size_type count_dont_splay(const KeyType& key, KeyValueCompare comp) const + { return tree_.count_dont_splay(key, comp); } + + //! Effects: Returns an iterator to the first element whose + //! key is not less than k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If the internal value_compare ordering function throws. + iterator lower_bound(const_reference value) + { return tree_.lower_bound(value); } + + //! Requires: comp must imply the same element order as + //! value_compare. Usually key is the part of the value_type + //! that is used in the ordering functor. + //! + //! Effects: Returns an iterator to the first element whose + //! key according to the comparison functor is not less than k or + //! end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If comp ordering function throws. + //! + //! Note: This function is used when constructing a value_type + //! is expensive and the value_type can be compared with a cheaper + //! key type. Usually this key is part of the value_type. + template + iterator lower_bound(const KeyType& key, KeyValueCompare comp) + { return tree_.lower_bound(key, comp); } + + //! Effects: Returns a const iterator to the first element whose + //! key is not less than k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If the internal value_compare ordering function throws. + const_iterator lower_bound_dont_splay(const_reference value) const + { return tree_.lower_bound_dont_splay(value); } + + //! Requires: comp must imply the same element order as + //! value_compare. Usually key is the part of the value_type + //! that is used in the ordering functor. + //! + //! Effects: Returns a const_iterator to the first element whose + //! key according to the comparison functor is not less than k or + //! end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If comp ordering function throws. + //! + //! Note: This function is used when constructing a value_type + //! is expensive and the value_type can be compared with a cheaper + //! key type. Usually this key is part of the value_type. + template + const_iterator lower_bound_dont_splay(const KeyType& key, KeyValueCompare comp) const + { return tree_.lower_bound_dont_splay(key, comp); } + + //! Effects: Returns an iterator to the first element whose + //! key is greater than k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If the internal value_compare ordering function throws. + iterator upper_bound(const_reference value) + { return tree_.upper_bound(value); } + + //! Requires: comp must imply the same element order as + //! value_compare. Usually key is the part of the value_type + //! that is used in the ordering functor. + //! + //! Effects: Returns an iterator to the first element whose + //! key according to the comparison functor is greater than key or + //! end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If comp ordering function throws. + //! + //! Note: This function is used when constructing a value_type + //! is expensive and the value_type can be compared with a cheaper + //! key type. Usually this key is part of the value_type. + template + iterator upper_bound(const KeyType& key, KeyValueCompare comp) + { return tree_.upper_bound(key, comp); } + + //! Effects: Returns an iterator to the first element whose + //! key is greater than k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If the internal value_compare ordering function throws. + const_iterator upper_bound_dont_splay(const_reference value) const + { return tree_.upper_bound_dont_splay(value); } + + //! Requires: comp must imply the same element order as + //! value_compare. Usually key is the part of the value_type + //! that is used in the ordering functor. + //! + //! Effects: Returns a const_iterator to the first element whose + //! key according to the comparison functor is greater than key or + //! end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If comp ordering function throws. + //! + //! Note: This function is used when constructing a value_type + //! is expensive and the value_type can be compared with a cheaper + //! key type. Usually this key is part of the value_type. + template + const_iterator upper_bound_dont_splay(const KeyType& key, KeyValueCompare comp) const + { return tree_.upper_bound_dont_splay(key, comp); } + + //! Effects: Finds an iterator to the first element whose value is + //! "value" or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If the internal value_compare ordering function throws. + iterator find(const_reference value) + { return tree_.find(value); } + + //! Requires: comp must imply the same element order as + //! value_compare. Usually key is the part of the value_type + //! that is used in the ordering functor. + //! + //! Effects: Finds an iterator to the first element whose key is + //! "key" according to the comparison functor or end() if that element + //! does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If comp ordering function throws. + //! + //! Note: This function is used when constructing a value_type + //! is expensive and the value_type can be compared with a cheaper + //! key type. Usually this key is part of the value_type. + template + iterator find(const KeyType& key, KeyValueCompare comp) + { return tree_.find(key, comp); } + + //! Effects: Finds a const_iterator to the first element whose value is + //! "value" or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If the internal value_compare ordering function throws. + const_iterator find_dont_splay(const_reference value) const + { return tree_.find_dont_splay(value); } + + //! Requires: comp must imply the same element order as + //! value_compare. Usually key is the part of the value_type + //! that is used in the ordering functor. + //! + //! Effects: Finds a const_iterator to the first element whose key is + //! "key" according to the comparison functor or end() if that element + //! does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If comp ordering function throws. + //! + //! Note: This function is used when constructing a value_type + //! is expensive and the value_type can be compared with a cheaper + //! key type. Usually this key is part of the value_type. + template + const_iterator find_dont_splay(const KeyType& key, KeyValueCompare comp) const + { return tree_.find_dont_splay(key, comp); } + + //! Effects: Finds a range containing all elements whose key is k or + //! an empty range that indicates the position where those elements would be + //! if they there is no elements with key k. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If the internal value_compare ordering function throws. + std::pair equal_range(const_reference value) + { return tree_.equal_range(value); } + + //! Requires: comp must imply the same element order as + //! value_compare. Usually key is the part of the value_type + //! that is used in the ordering functor. + //! + //! Effects: Finds a range containing all elements whose key is k + //! according to the comparison functor or an empty range + //! that indicates the position where those elements would be + //! if they there is no elements with key k. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If comp ordering function throws. + //! + //! Note: This function is used when constructing a value_type + //! is expensive and the value_type can be compared with a cheaper + //! key type. Usually this key is part of the value_type. + template + std::pair equal_range(const KeyType& key, KeyValueCompare comp) + { return tree_.equal_range(key, comp); } + + //! Effects: Finds a range containing all elements whose key is k or + //! an empty range that indicates the position where those elements would be + //! if they there is no elements with key k. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If the internal value_compare ordering function throws. + std::pair + equal_range_dont_splay(const_reference value) const + { return tree_.equal_range_dont_splay(value); } + + //! Requires: comp must imply the same element order as + //! value_compare. Usually key is the part of the value_type + //! that is used in the ordering functor. + //! + //! Effects: Finds a range containing all elements whose key is k + //! according to the comparison functor or an empty range + //! that indicates the position where those elements would be + //! if they there is no elements with key k. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If comp ordering function throws. + //! + //! Note: This function is used when constructing a value_type + //! is expensive and the value_type can be compared with a cheaper + //! key type. Usually this key is part of the value_type. + template + std::pair + equal_range_dont_splay(const KeyType& key, KeyValueCompare comp) const + { return tree_.equal_range_dont_splay(key, comp); } + + //! Requires: value must be an lvalue and shall be in a set of + //! appropriate type. Otherwise the behavior is undefined. + //! + //! Effects: Returns: a valid iterator i belonging to the set + //! that points to the value + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Note: This static function is available only if the value traits + //! is stateless. + static iterator s_iterator_to(reference value) + { return tree_type::s_iterator_to(value); } + + //! Requires: value must be an lvalue and shall be in a set of + //! appropriate type. Otherwise the behavior is undefined. + //! + //! Effects: Returns: a valid const_iterator i belonging to the + //! set that points to the value + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Note: This static function is available only if the value traits + //! is stateless. + static const_iterator s_iterator_to(const_reference value) + { return tree_type::s_iterator_to(value); } + + //! Requires: value must be an lvalue and shall be in a set of + //! appropriate type. Otherwise the behavior is undefined. + //! + //! Effects: Returns: a valid iterator i belonging to the set + //! that points to the value + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + iterator iterator_to(reference value) + { return tree_.iterator_to(value); } + + //! Requires: value must be an lvalue and shall be in a set of + //! appropriate type. Otherwise the behavior is undefined. + //! + //! Effects: Returns: a valid const_iterator i belonging to the + //! set that points to the value + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_iterator iterator_to(const_reference value) const + { return tree_.iterator_to(value); } + + //! Requires: value shall not be in a set/splay_multiset. + //! + //! Effects: init_node puts the hook of a value in a well-known default + //! state. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant time. + //! + //! Note: This function puts the hook in the well-known default state + //! used by auto_unlink and safe hooks. + static void init_node(reference value) + { tree_type::init_node(value); } + + //! Effects: Unlinks the leftmost node from the tree. + //! + //! Complexity: Average complexity is constant time. + //! + //! Throws: Nothing. + //! + //! Notes: This function breaks the tree and the tree can + //! only be used for more unlink_leftmost_without_rebalance calls. + //! This function is normally used to achieve a step by step + //! controlled destruction of the tree. + pointer unlink_leftmost_without_rebalance() + { return tree_.unlink_leftmost_without_rebalance(); } + + //! Requires: replace_this must be a valid iterator of *this + //! and with_this must not be inserted in any tree. + //! + //! Effects: Replaces replace_this in its position in the + //! tree with with_this. The tree does not need to be rebalanced. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Note: This function will break container ordering invariants if + //! with_this is not equivalent to *replace_this according to the + //! ordering rules. This function is faster than erasing and inserting + //! the node, since no rebalancing or comparison is needed. + void replace_node(iterator replace_this, reference with_this) + { tree_.replace_node(replace_this, with_this); } + + //! Requires: i must be a valid iterator of *this. + //! + //! Effects: Rearranges the splay set so that the element pointed by i + //! is placed as the root of the tree, improving future searches of this value. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + void splay_up(iterator i) + { tree_.splay_up(i); } + + //! Effects: Rearranges the splay set so that if *this stores an element + //! with a key equivalent to value the element is placed as the root of the + //! tree. If the element is not present returns the last node compared with the key. + //! If the tree is empty, end() is returned. + //! + //! Complexity: Logarithmic. + //! + //! Returns: An iterator to the new root of the tree, end() if the tree is empty. + //! + //! Throws: If the comparison functor throws. + template + iterator splay_down(const KeyType &key, KeyNodePtrCompare comp) + { return tree_.splay_down(key, comp); } + + //! Effects: Rearranges the splay set so that if *this stores an element + //! with a key equivalent to value the element is placed as the root of the + //! tree. + //! + //! Complexity: Logarithmic. + //! + //! Returns: An iterator to the new root of the tree, end() if the tree is empty. + //! + //! Throws: If the predicate throws. + iterator splay_down(const value_type &value) + { return tree_.splay_down(value); } + + /// @cond + friend bool operator==(const splay_multiset_impl &x, const splay_multiset_impl &y) + { return x.tree_ == y.tree_; } + + friend bool operator<(const splay_multiset_impl &x, const splay_multiset_impl &y) + { return x.tree_ < y.tree_; } + /// @endcond +}; + +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +inline bool operator!= +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +(const splay_multiset_impl &x, const splay_multiset_impl &y) +#else +(const splay_multiset_impl &x, const splay_multiset_impl &y) +#endif +{ return !(x == y); } + +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +inline bool operator> +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +(const splay_multiset_impl &x, const splay_multiset_impl &y) +#else +(const splay_multiset_impl &x, const splay_multiset_impl &y) +#endif +{ return y < x; } + +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +inline bool operator<= +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +(const splay_multiset_impl &x, const splay_multiset_impl &y) +#else +(const splay_multiset_impl &x, const splay_multiset_impl &y) +#endif +{ return !(y < x); } + +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +inline bool operator>= +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +(const splay_multiset_impl &x, const splay_multiset_impl &y) +#else +(const splay_multiset_impl &x, const splay_multiset_impl &y) +#endif +{ return !(x < y); } + +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +inline void swap +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +(splay_multiset_impl &x, splay_multiset_impl &y) +#else +(splay_multiset_impl &x, splay_multiset_impl &y) +#endif +{ x.swap(y); } + +//! Helper metafunction to define a \c splay_multiset that yields to the same type when the +//! same options (either explicitly or implicitly) are used. +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +struct make_splay_multiset +{ + /// @cond + typedef splay_multiset_impl + < typename make_splaytree_opt::type + > implementation_defined; + /// @endcond + typedef implementation_defined type; +}; + +#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +class splay_multiset + : public make_splay_multiset::type +{ + typedef typename make_splay_multiset + ::type Base; + + public: + typedef typename Base::value_compare value_compare; + typedef typename Base::value_traits value_traits; + typedef typename Base::iterator iterator; + typedef typename Base::const_iterator const_iterator; + + //Assert if passed value traits are compatible with the type + BOOST_STATIC_ASSERT((detail::is_same::value)); + + splay_multiset( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : Base(cmp, v_traits) + {} + + template + splay_multiset( Iterator b, Iterator e + , const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : Base(b, e, cmp, v_traits) + {} + + static splay_multiset &container_from_end_iterator(iterator end_iterator) + { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static const splay_multiset &container_from_end_iterator(const_iterator end_iterator) + { return static_cast(Base::container_from_end_iterator(end_iterator)); } +}; + +#endif + +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_SPLAY_SET_HPP diff --git a/include/boost/intrusive/splay_set_hook.hpp b/include/boost/intrusive/splay_set_hook.hpp new file mode 100644 index 0000000..69c5306 --- /dev/null +++ b/include/boost/intrusive/splay_set_hook.hpp @@ -0,0 +1,257 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Olaf Krzikalla 2004-2006. +// (C) Copyright Ion Gaztanaga 2006-2007 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +#ifndef BOOST_INTRUSIVE_SPLAY_SET_HOOK_HPP +#define BOOST_INTRUSIVE_SPLAY_SET_HOOK_HPP + +#include +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace intrusive { + +/// @cond +template +struct get_splay_set_node_algo +{ + typedef splaytree_algorithms > type; +}; +/// @endcond + +//! Helper metafunction to define a \c splay_set_base_hook that yields to the same +//! type when the same options (either explicitly or implicitly) are used. +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +struct make_splay_set_base_hook +{ + /// @cond + typedef typename pack_options + < hook_defaults, O1, O2, O3>::type packed_options; + + typedef detail::generic_hook + < get_splay_set_node_algo + , typename packed_options::tag + , packed_options::link_mode + , detail::SplaySetBaseHook + > implementation_defined; + /// @endcond + typedef implementation_defined type; +}; + +//! Derive a class from splay_set_base_hook in order to store objects in +//! in an set/multiset. splay_set_base_hook holds the data necessary to maintain +//! the set/multiset and provides an appropriate value_traits class for set/multiset. +//! +//! The first integer template argument defines a tag to identify the node. +//! The same tag value can be used in different classes, but if a class is +//! derived from more than one splay_set_base_hook, then each splay_set_base_hook needs its +//! unique tag. +//! +//! The second boolean template parameter will specify the linking mode of the hook. +//! +//! The third argument is the pointer type that will be used internally in the hook +//! and the set/multiset configured from this hook. +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +class splay_set_base_hook + : public make_splay_set_base_hook::type +{ + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + //! Effects: If link_mode is \c auto_unlink or \c safe_link + //! initializes the node to an unlinked state. + //! + //! Throws: Nothing. + splay_set_base_hook(); + + //! Effects: If link_mode is \c auto_unlink or \c safe_link + //! initializes the node to an unlinked state. The argument is ignored. + //! + //! Throws: Nothing. + //! + //! Rationale: Providing a copy-constructor + //! makes classes using the hook STL-compliant without forcing the + //! user to do some additional work. \c swap can be used to emulate + //! move-semantics. + splay_set_base_hook(const splay_set_base_hook& ); + + //! Effects: Empty function. The argument is ignored. + //! + //! Throws: Nothing. + //! + //! Rationale: Providing an assignment operator + //! makes classes using the hook STL-compliant without forcing the + //! user to do some additional work. \c swap can be used to emulate + //! move-semantics. + splay_set_base_hook& operator=(const splay_set_base_hook& ); + + //! Effects: If link_mode is \c normal_link, the destructor does + //! nothing (ie. no code is generated). If link_mode is \c safe_link and the + //! object is stored in an set an assertion is raised. If link_mode is + //! \c auto_unlink and \c is_linked() is true, the node is unlinked. + //! + //! Throws: Nothing. + ~splay_set_base_hook(); + + //! Effects: Swapping two nodes swaps the position of the elements + //! related to those nodes in one or two containers. That is, if the node + //! this is part of the element e1, the node x is part of the element e2 + //! and both elements are included in the containers s1 and s2, then after + //! the swap-operation e1 is in s2 at the position of e2 and e2 is in s1 + //! at the position of e1. If one element is not in a container, then + //! after the swap-operation the other element is not in a container. + //! Iterators to e1 and e2 related to those nodes are invalidated. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + void swap_nodes(splay_set_base_hook &other); + + //! Precondition: link_mode must be \c safe_link or \c auto_unlink. + //! + //! Returns: true, if the node belongs to a container, false + //! otherwise. This function can be used to test whether \c set::iterator_to + //! will return a valid iterator. + //! + //! Complexity: Constant + bool is_linked() const; + + //! Effects: Removes the node if it's inserted in a container. + //! This function is only allowed if link_mode is \c auto_unlink. + //! + //! Throws: Nothing. + void unlink(); + #endif +}; + +//! Helper metafunction to define a \c splay_set_member_hook that yields to the same +//! type when the same options (either explicitly or implicitly) are used. +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +struct make_splay_set_member_hook +{ + /// @cond + typedef typename pack_options + < hook_defaults, O1, O2, O3>::type packed_options; + + typedef detail::generic_hook + < get_splay_set_node_algo + , member_tag + , packed_options::link_mode + , detail::NoBaseHook + > implementation_defined; + /// @endcond + typedef implementation_defined type; +}; + +//! Put a public data member splay_set_member_hook in order to store objects of this class in +//! an set/multiset. splay_set_member_hook holds the data necessary for maintaining the +//! set/multiset and provides an appropriate value_traits class for set/multiset. +//! +//! The first boolean template parameter will specify the linking mode of the hook. +//! +//! The second argument is the pointer type that will be used internally in the hook +//! and the set/multiset configured from this hook. +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +class splay_set_member_hook + : public make_splay_set_member_hook::type +{ + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + //! Effects: If link_mode is \c auto_unlink or \c safe_link + //! initializes the node to an unlinked state. + //! + //! Throws: Nothing. + splay_set_member_hook(); + + //! Effects: If link_mode is \c auto_unlink or \c safe_link + //! initializes the node to an unlinked state. The argument is ignored. + //! + //! Throws: Nothing. + //! + //! Rationale: Providing a copy-constructor + //! makes classes using the hook STL-compliant without forcing the + //! user to do some additional work. \c swap can be used to emulate + //! move-semantics. + splay_set_member_hook(const splay_set_member_hook& ); + + //! Effects: Empty function. The argument is ignored. + //! + //! Throws: Nothing. + //! + //! Rationale: Providing an assignment operator + //! makes classes using the hook STL-compliant without forcing the + //! user to do some additional work. \c swap can be used to emulate + //! move-semantics. + splay_set_member_hook& operator=(const splay_set_member_hook& ); + + //! Effects: If link_mode is \c normal_link, the destructor does + //! nothing (ie. no code is generated). If link_mode is \c safe_link and the + //! object is stored in an set an assertion is raised. If link_mode is + //! \c auto_unlink and \c is_linked() is true, the node is unlinked. + //! + //! Throws: Nothing. + ~splay_set_member_hook(); + + //! Effects: Swapping two nodes swaps the position of the elements + //! related to those nodes in one or two containers. That is, if the node + //! this is part of the element e1, the node x is part of the element e2 + //! and both elements are included in the containers s1 and s2, then after + //! the swap-operation e1 is in s2 at the position of e2 and e2 is in s1 + //! at the position of e1. If one element is not in a container, then + //! after the swap-operation the other element is not in a container. + //! Iterators to e1 and e2 related to those nodes are invalidated. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + void swap_nodes(splay_set_member_hook &other); + + //! Precondition: link_mode must be \c safe_link or \c auto_unlink. + //! + //! Returns: true, if the node belongs to a container, false + //! otherwise. This function can be used to test whether \c set::iterator_to + //! will return a valid iterator. + //! + //! Complexity: Constant + bool is_linked() const; + + //! Effects: Removes the node if it's inserted in a container. + //! This function is only allowed if link_mode is \c auto_unlink. + //! + //! Throws: Nothing. + void unlink(); + #endif +}; + +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_SPLAY_SET_HOOK_HPP diff --git a/include/boost/intrusive/splaytree.hpp b/include/boost/intrusive/splaytree.hpp new file mode 100644 index 0000000..8b6fe15 --- /dev/null +++ b/include/boost/intrusive/splaytree.hpp @@ -0,0 +1,1501 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2007 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +#ifndef BOOST_INTRUSIVE_SPLAYTREE_HPP +#define BOOST_INTRUSIVE_SPLAYTREE_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +namespace boost { +namespace intrusive { + +/// @cond + +template +struct internal_default_splay_set_hook +{ + template static detail::one test(...); + template static detail::two test(typename U::default_splay_set_hook* = 0); + static const bool value = sizeof(test(0)) == sizeof(detail::two); +}; + +template +struct get_default_splay_set_hook +{ + typedef typename T::default_splay_set_hook type; +}; + +template +struct splaysetopt +{ + typedef ValueTraits value_traits; + typedef Compare compare; + typedef SizeType size_type; + static const bool constant_time_size = ConstantTimeSize; +}; + +template +struct splay_set_defaults + : pack_options + < none + , base_hook + < typename detail::eval_if_c + < internal_default_splay_set_hook::value + , get_default_splay_set_hook + , detail::identity + >::type + > + , constant_time_size + , size_type + , compare > + >::type +{}; + +/// @endcond + +//! The class template splaytree is an intrusive splay tree container that +//! is used to construct intrusive splay_set and splay_multiset containers. The no-throw +//! guarantee holds only, if the value_compare object +//! doesn't throw. +//! +//! The template parameter \c T is the type to be managed by the container. +//! The user can specify additional options and if no options are provided +//! default options are used. +//! +//! The container supports the following options: +//! \c base_hook<>/member_hook<>/value_traits<>, +//! \c constant_time_size<>, \c size_type<> and +//! \c compare<>. +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +class splaytree_impl +{ + public: + typedef typename Config::value_traits value_traits; + /// @cond + static const bool external_value_traits = + detail::external_value_traits_is_true::value; + typedef typename detail::eval_if_c + < external_value_traits + , detail::eval_value_traits + , detail::identity + >::type real_value_traits; + /// @endcond + typedef typename real_value_traits::pointer pointer; + typedef typename real_value_traits::const_pointer const_pointer; + typedef typename std::iterator_traits::value_type value_type; + typedef value_type key_type; + typedef typename std::iterator_traits::reference reference; + typedef typename std::iterator_traits::reference const_reference; + typedef typename std::iterator_traits::difference_type difference_type; + typedef typename Config::size_type size_type; + typedef typename Config::compare value_compare; + typedef value_compare key_compare; + typedef tree_iterator iterator; + typedef tree_iterator const_iterator; + typedef std::reverse_iterator reverse_iterator; + typedef std::reverse_iterator const_reverse_iterator; + typedef typename real_value_traits::node_traits node_traits; + typedef typename node_traits::node node; + typedef typename boost::pointer_to_other + ::type node_ptr; + typedef typename boost::pointer_to_other + ::type const_node_ptr; + typedef splaytree_algorithms node_algorithms; + + static const bool constant_time_size = Config::constant_time_size; + static const bool stateful_value_traits = detail::store_cont_ptr_on_it::value; + + /// @cond + private: + typedef detail::size_holder size_traits; + + //noncopyable + splaytree_impl (const splaytree_impl&); + splaytree_impl operator =(const splaytree_impl&); + + enum { safemode_or_autounlink = + (int)real_value_traits::link_mode == (int)auto_unlink || + (int)real_value_traits::link_mode == (int)safe_link }; + + //Constant-time size is incompatible with auto-unlink hooks! + BOOST_STATIC_ASSERT(!(constant_time_size && ((int)real_value_traits::link_mode == (int)auto_unlink))); + + struct header_plus_size : public size_traits + { node header_; }; + + struct node_plus_pred_t : public detail::ebo_functor_holder + { + node_plus_pred_t(const value_compare &comp) + : detail::ebo_functor_holder(comp) + {} + header_plus_size header_plus_size_; + }; + + struct data_t : public splaytree_impl::value_traits + { + typedef typename splaytree_impl::value_traits value_traits; + data_t(const value_compare & comp, const value_traits &val_traits) + : value_traits(val_traits), node_plus_pred_(comp) + {} + node_plus_pred_t node_plus_pred_; + } data_; + + const value_compare &priv_comp() const + { return data_.node_plus_pred_.get(); } + + value_compare &priv_comp() + { return data_.node_plus_pred_.get(); } + + const node &priv_header() const + { return data_.node_plus_pred_.header_plus_size_.header_; } + + node &priv_header() + { return data_.node_plus_pred_.header_plus_size_.header_; } + + static node_ptr uncast(const_node_ptr ptr) + { + return node_ptr(const_cast(detail::get_pointer(ptr))); + } + + size_traits &priv_size_traits() + { return data_.node_plus_pred_.header_plus_size_; } + + const size_traits &priv_size_traits() const + { return data_.node_plus_pred_.header_plus_size_; } + + const real_value_traits &get_real_value_traits(detail::bool_) const + { return data_; } + + const real_value_traits &get_real_value_traits(detail::bool_) const + { return data_.get_value_traits(*this); } + + real_value_traits &get_real_value_traits(detail::bool_) + { return data_; } + + real_value_traits &get_real_value_traits(detail::bool_) + { return data_.get_value_traits(*this); } + + /// @endcond + + public: + + const real_value_traits &get_real_value_traits() const + { return this->get_real_value_traits(detail::bool_()); } + + real_value_traits &get_real_value_traits() + { return this->get_real_value_traits(detail::bool_()); } + + typedef typename node_algorithms::insert_commit_data insert_commit_data; + + //! Effects: Constructs an empty tree. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing unless the copy constructor of the value_compare object throws. + splaytree_impl( value_compare cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : data_(cmp, v_traits) + { + node_algorithms::init_header(&priv_header()); + this->priv_size_traits().set_size(size_type(0)); + } + + //! Requires: Dereferencing iterator must yield an lvalue of type value_type. + //! cmp must be a comparison function that induces a strict weak ordering. + //! + //! Effects: Constructs an empty tree and inserts elements from + //! [b, e). + //! + //! Complexity: Linear in N if [b, e) is already sorted using + //! comp and otherwise N * log N, where N is the distance between first and last. + //! + //! Throws: Nothing unless the copy constructor of the value_compare object throws. + template + splaytree_impl( bool unique, Iterator b, Iterator e + , value_compare cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : data_(cmp, v_traits) + { + node_algorithms::init_header(&priv_header()); + this->priv_size_traits().set_size(size_type(0)); + if(unique) + this->insert_unique(b, e); + else + this->insert_equal(b, e); + } + + //! Effects: Detaches all elements from this. The objects in the set + //! are not deleted (i.e. no destructors are called), but the nodes according to + //! the value_traits template parameter are reinitialized and thus can be reused. + //! + //! Complexity: Linear to elements contained in *this. + //! + //! Throws: Nothing. + ~splaytree_impl() + { this->clear(); } + + //! Effects: Returns an iterator pointing to the beginning of the tree. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + iterator begin() + { return iterator(node_algorithms::begin_node(&priv_header()), this); } + + //! Effects: Returns a const_iterator pointing to the beginning of the tree. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_iterator begin() const + { return cbegin(); } + + //! Effects: Returns a const_iterator pointing to the beginning of the tree. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_iterator cbegin() const + { return const_iterator(node_algorithms::begin_node(&priv_header()), this); } + + //! Effects: Returns an iterator pointing to the end of the tree. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + iterator end() + { return iterator (node_ptr(&priv_header()), this); } + + //! Effects: Returns a const_iterator pointing to the end of the tree. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_iterator end() const + { return cend(); } + + //! Effects: Returns a const_iterator pointing to the end of the tree. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_iterator cend() const + { return const_iterator (uncast(const_node_ptr(&priv_header())), this); } + + //! Effects: Returns a reverse_iterator pointing to the beginning of the + //! reversed tree. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + reverse_iterator rbegin() + { return reverse_iterator(end()); } + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed tree. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_reverse_iterator rbegin() const + { return const_reverse_iterator(end()); } + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed tree. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_reverse_iterator crbegin() const + { return const_reverse_iterator(end()); } + + //! Effects: Returns a reverse_iterator pointing to the end + //! of the reversed tree. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + reverse_iterator rend() + { return reverse_iterator(begin()); } + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed tree. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_reverse_iterator rend() const + { return const_reverse_iterator(begin()); } + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed tree. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_reverse_iterator crend() const + { return const_reverse_iterator(begin()); } + + //! Precondition: end_iterator must be a valid end iterator + //! of splaytree. + //! + //! Effects: Returns a const reference to the splaytree associated to the end iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + static splaytree_impl &container_from_end_iterator(iterator end_iterator) + { return priv_container_from_end_iterator(end_iterator); } + + //! Precondition: end_iterator must be a valid end const_iterator + //! of splaytree. + //! + //! Effects: Returns a const reference to the splaytree associated to the end iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + static const splaytree_impl &container_from_end_iterator(const_iterator end_iterator) + { return priv_container_from_end_iterator(end_iterator); } + + //! Effects: Returns the value_compare object used by the tree. + //! + //! Complexity: Constant. + //! + //! Throws: If value_compare copy-constructor throws. + value_compare value_comp() const + { return priv_comp(); } + + //! Effects: Returns true is the container is empty. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + bool empty() const + { return this->cbegin() == this->cend(); } + + //! Effects: Returns the number of elements stored in the tree. + //! + //! Complexity: Linear to elements contained in *this. + //! + //! Throws: Nothing. + size_type size() const + { + if(constant_time_size){ + return this->priv_size_traits().get_size(); + } + else{ + const_iterator beg(this->cbegin()), end(this->cend()); + size_type i = 0; + for(;beg != end; ++beg) ++i; + return i; + } + } + + //! Effects: Swaps the contents of two multisets. + //! + //! Complexity: Constant. + //! + //! Throws: If the comparison functor's swap call throws. + void swap(splaytree_impl& other) + { + //This can throw + using std::swap; + swap(priv_comp(), priv_comp()); + //These can't throw + node_algorithms::swap_tree(node_ptr(&priv_header()), node_ptr(&other.priv_header())); + if(constant_time_size){ + size_type backup = this->priv_size_traits().get_size(); + this->priv_size_traits().set_size(other.priv_size_traits().get_size()); + other.priv_size_traits().set_size(backup); + } + } + + //! Requires: value must be an lvalue + //! + //! Effects: Inserts value into the tree before the lower bound. + //! + //! Complexity: Average complexity for insert element is at + //! most logarithmic. + //! + //! Throws: Nothing. + //! + //! Note: Does not affect the validity of iterators and references. + //! No copy-constructors are called. + iterator insert_equal(reference value) + { + detail::key_nodeptr_comp + key_node_comp(priv_comp(), this); + node_ptr to_insert(get_real_value_traits().to_node_ptr(value)); + if(safemode_or_autounlink) + BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert)); + this->priv_size_traits().increment(); + return iterator(node_algorithms::insert_equal_lower_bound + (node_ptr(&priv_header()), to_insert, key_node_comp), this); + } + + //! Requires: value must be an lvalue, and "hint" must be + //! a valid iterator. + //! + //! Effects: Inserts x into the tree, using "hint" as a hint to + //! where it will be inserted. If "hint" is the upper_bound + //! the insertion takes constant time (two comparisons in the worst case) + //! + //! Complexity: Logarithmic in general, but it is amortized + //! constant time if t is inserted immediately before hint. + //! + //! Throws: Nothing. + //! + //! Note: Does not affect the validity of iterators and references. + //! No copy-constructors are called. + iterator insert_equal(const_iterator hint, reference value) + { + detail::key_nodeptr_comp + key_node_comp(priv_comp(), this); + node_ptr to_insert(get_real_value_traits().to_node_ptr(value)); + if(safemode_or_autounlink) + BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert)); + this->priv_size_traits().increment(); + return iterator(node_algorithms::insert_equal + (node_ptr(&priv_header()), hint.pointed_node(), to_insert, key_node_comp), this); + } + + //! Requires: Dereferencing iterator must yield an lvalue + //! of type value_type. + //! + //! Effects: Inserts a each element of a range into the tree + //! before the upper bound of the key of each element. + //! + //! Complexity: Insert range is in general O(N * log(N)), where N is the + //! size of the range. However, it is linear in N if the range is already sorted + //! by value_comp(). + //! + //! Throws: Nothing. + //! + //! Note: Does not affect the validity of iterators and references. + //! No copy-constructors are called. + template + void insert_equal(Iterator b, Iterator e) + { + if(this->empty()){ + iterator end(this->end()); + for (; b != e; ++b) + this->insert_equal(end, *b); + } + } + + //! Requires: value must be an lvalue + //! + //! Effects: Inserts value into the tree if the value + //! is not already present. + //! + //! Complexity: Average complexity for insert element is at + //! most logarithmic. + //! + //! Throws: Nothing. + //! + //! Note: Does not affect the validity of iterators and references. + //! No copy-constructors are called. + std::pair insert_unique(reference value) + { + insert_commit_data commit_data; + std::pair ret = insert_unique_check(value, commit_data); + if(!ret.second) + return ret; + return std::pair (insert_unique_commit(value, commit_data), true); + } + + //! Requires: value must be an lvalue, and "hint" must be + //! a valid iterator + //! + //! Effects: Tries to insert x into the tree, using "hint" as a hint + //! to where it will be inserted. + //! + //! Complexity: Logarithmic in general, but it is amortized + //! constant time (two comparisons in the worst case) + //! if t is inserted immediately before hint. + //! + //! Throws: Nothing. + //! + //! Note: Does not affect the validity of iterators and references. + //! No copy-constructors are called. + iterator insert_unique(const_iterator hint, reference value) + { + insert_commit_data commit_data; + std::pair ret = insert_unique_check(hint, value, commit_data); + if(!ret.second) + return ret.first; + return insert_unique_commit(value, commit_data); + } + + //! Requires: Dereferencing iterator must yield an lvalue + //! of type value_type. + //! + //! Effects: Tries to insert each element of a range into the tree. + //! + //! Complexity: Insert range is in general O(N * log(N)), where N is the + //! size of the range. However, it is linear in N if the range is already sorted + //! by value_comp(). + //! + //! Throws: Nothing. + //! + //! Note: Does not affect the validity of iterators and references. + //! No copy-constructors are called. + template + void insert_unique(Iterator b, Iterator e) + { + for (; b != e; ++b) + this->insert_unique(*b); + } + + std::pair insert_unique_check + (const_reference value, insert_commit_data &commit_data) + { return insert_unique_check(value, priv_comp(), commit_data); } + + template + std::pair insert_unique_check + (const KeyType &key, KeyValueCompare key_value_comp, insert_commit_data &commit_data) + { + detail::key_nodeptr_comp + comp(key_value_comp, this); + std::pair ret = + (node_algorithms::insert_unique_check + (node_ptr(&priv_header()), key, comp, commit_data)); + return std::pair(iterator(ret.first, this), ret.second); + } + + std::pair insert_unique_check + (const_iterator hint, const_reference value, insert_commit_data &commit_data) + { return insert_unique_check(hint, value, priv_comp(), commit_data); } + + template + std::pair insert_unique_check + (const_iterator hint, const KeyType &key + ,KeyValueCompare key_value_comp, insert_commit_data &commit_data) + { + detail::key_nodeptr_comp + comp(key_value_comp, this); + std::pair ret = + node_algorithms::insert_unique_check + (node_ptr(&priv_header()), hint.pointed_node(), key, comp, commit_data); + return std::pair(iterator(ret.first, this), ret.second); + } + + iterator insert_unique_commit(reference value, const insert_commit_data &commit_data) + { + node_ptr to_insert(get_real_value_traits().to_node_ptr(value)); + if(safemode_or_autounlink) + BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert)); + this->priv_size_traits().increment(); + node_algorithms::insert_unique_commit + (node_ptr(&priv_header()), to_insert, commit_data); + return iterator(to_insert, this); + } + + //! Effects: Erases the element pointed to by pos. + //! + //! Complexity: Average complexity for erase element is constant time. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + iterator erase(iterator i) + { + iterator ret(i); + ++ret; + node_ptr to_erase(i.pointed_node()); + if(safemode_or_autounlink) + BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!node_algorithms::unique(to_erase)); + node_algorithms::erase(&priv_header(), to_erase); + this->priv_size_traits().decrement(); + if(safemode_or_autounlink) + node_algorithms::init(to_erase); + return ret; + } + + //! Effects: Erases the range pointed to by b end e. + //! + //! Complexity: Average complexity for erase range is at most + //! O(log(size() + N)), where N is the number of elements in the range. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + iterator erase(iterator b, iterator e) + { size_type n; return private_erase(b, e, n); } + + //! Effects: Erases all the elements with the given value. + //! + //! Returns: The number of erased elements. + //! + //! Complexity: O(log(size() + N). + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + size_type erase(const_reference value) + { return this->erase(value, priv_comp()); } + + //! Effects: Erases all the elements with the given key. + //! according to the comparison functor "comp". + //! + //! Returns: The number of erased elements. + //! + //! Complexity: O(log(size() + N). + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + template + size_type erase(const KeyType& key, KeyValueCompare comp) + { + std::pair p = this->equal_range(key, comp); + size_type n; + private_erase(p.first, p.second, n); + return n; + } + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! + //! Effects: Erases the element pointed to by pos. + //! Disposer::operator()(pointer) is called for the removed element. + //! + //! Complexity: Average complexity for erase element is constant time. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators + //! to the erased elements. + template + iterator erase_and_dispose(iterator i, Disposer disposer) + { + node_ptr to_erase(i.pointed_node()); + iterator ret(this->erase(i)); + disposer(get_real_value_traits().to_value_ptr(to_erase)); + return ret; + } + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! + //! Effects: Erases the range pointed to by b end e. + //! Disposer::operator()(pointer) is called for the removed elements. + //! + //! Complexity: Average complexity for erase range is at most + //! O(log(size() + N)), where N is the number of elements in the range. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators + //! to the erased elements. + template + iterator erase_and_dispose(iterator b, iterator e, Disposer disposer) + { size_type n; return private_erase(b, e, n, disposer); } + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! + //! Effects: Erases all the elements with the given value. + //! Disposer::operator()(pointer) is called for the removed elements. + //! + //! Returns: The number of erased elements. + //! + //! Complexity: O(log(size() + N). + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + template + size_type erase_and_dispose(const_reference value, Disposer disposer) + { + std::pair p = this->equal_range(value); + size_type n; + private_erase(p.first, p.second, n, disposer); + return n; + } + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! + //! Effects: Erases all the elements with the given key. + //! according to the comparison functor "comp". + //! Disposer::operator()(pointer) is called for the removed elements. + //! + //! Returns: The number of erased elements. + //! + //! Complexity: O(log(size() + N). + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators + //! to the erased elements. + template + size_type erase_and_dispose(const KeyType& key, KeyValueCompare comp, Disposer disposer) + { + std::pair p = this->equal_range(key, comp); + size_type n; + private_erase(p.first, p.second, n, disposer); + return n; + } + + //! Effects: Erases all of the elements. + //! + //! Complexity: Linear to the number of elements on the container. + //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + void clear() + { + if(safemode_or_autounlink){ + this->clear_and_dispose(detail::null_disposer()); + } + else{ + node_algorithms::init_header(&priv_header()); + this->priv_size_traits().set_size(0); + } + } + + //! Effects: Erases all of the elements calling disposer(p) for + //! each node to be erased. + //! Complexity: Average complexity for is at most O(log(size() + N)), + //! where N is the number of elements in the container. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. Calls N times to disposer functor. + template + void clear_and_dispose(Disposer disposer) + { + node_algorithms::clear_and_dispose(node_ptr(&priv_header()) + , detail::node_disposer(disposer, this)); + node_algorithms::init_header(&priv_header()); + this->priv_size_traits().set_size(0); + } + + //! Effects: Returns the number of contained elements with the given value + //! + //! Complexity: Logarithmic to the number of elements contained plus lineal + //! to number of objects with the given value. + //! + //! Throws: Nothing. + size_type count(const_reference value) + { return this->count(value, priv_comp()); } + + //! Effects: Returns the number of contained elements with the given key + //! + //! Complexity: Logarithmic to the number of elements contained plus lineal + //! to number of objects with the given key. + //! + //! Throws: Nothing. + template + size_type count(const KeyType &key, KeyValueCompare comp) + { + std::pair ret = this->equal_range(key, comp); + return std::distance(ret.first, ret.second); + } + + //! Effects: Returns the number of contained elements with the given value + //! + //! Complexity: Logarithmic to the number of elements contained plus lineal + //! to number of objects with the given value. + //! + //! Throws: Nothing. + size_type count_dont_splay(const_reference value) const + { return this->count_dont_splay(value, priv_comp()); } + + //! Effects: Returns the number of contained elements with the given key + //! + //! Complexity: Logarithmic to the number of elements contained plus lineal + //! to number of objects with the given key. + //! + //! Throws: Nothing. + template + size_type count_dont_splay(const KeyType &key, KeyValueCompare comp) const + { + std::pair ret = this->equal_range_dont_splay(key, comp); + return std::distance(ret.first, ret.second); + } + + //! Effects: Returns an iterator to the first element whose + //! key is not less than k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + iterator lower_bound(const_reference value) + { return this->lower_bound(value, priv_comp()); } + + //! Effects: Returns an iterator to the first element whose + //! key is not less than k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + const_iterator lower_bound_dont_splay(const_reference value) const + { return this->lower_bound_dont_splay(value, priv_comp()); } + + //! Effects: Returns an iterator to the first element whose + //! key is not less than k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + template + iterator lower_bound(const KeyType &key, KeyValueCompare comp) + { + detail::key_nodeptr_comp + key_node_comp(comp, this); + return iterator(node_algorithms::lower_bound + (const_node_ptr(&priv_header()), key, key_node_comp), this); + } + + //! Effects: Returns a const iterator to the first element whose + //! key is not less than k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + template + const_iterator lower_bound_dont_splay(const KeyType &key, KeyValueCompare comp) const + { + detail::key_nodeptr_comp + key_node_comp(comp, this); + return const_iterator(node_algorithms::lower_bound + (const_node_ptr(&priv_header()), key, key_node_comp, false), this); + } + + //! Effects: Returns an iterator to the first element whose + //! key is greater than k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + iterator upper_bound(const_reference value) + { return this->upper_bound(value, priv_comp()); } + + //! Effects: Returns an iterator to the first element whose + //! key is greater than k according to comp or end() if that element + //! does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + template + iterator upper_bound(const KeyType &key, KeyValueCompare comp) + { + detail::key_nodeptr_comp + key_node_comp(comp, this); + return iterator(node_algorithms::upper_bound + (const_node_ptr(&priv_header()), key, key_node_comp), this); + } + + //! Effects: Returns an iterator to the first element whose + //! key is greater than k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + const_iterator upper_bound_dont_splay(const_reference value) const + { return this->upper_bound_dont_splay(value, priv_comp()); } + + //! Effects: Returns an iterator to the first element whose + //! key is greater than k according to comp or end() if that element + //! does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + template + const_iterator upper_bound_dont_splay(const KeyType &key, KeyValueCompare comp) const + { + detail::key_nodeptr_comp + key_node_comp(comp, this); + return const_iterator(node_algorithms::upper_bound_dont_splay + (const_node_ptr(&priv_header()), key, key_node_comp, false), this); + } + + //! Effects: Finds an iterator to the first element whose key is + //! k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + iterator find(const_reference value) + { return this->find(value, priv_comp()); } + + //! Effects: Finds an iterator to the first element whose key is + //! k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + template + iterator find(const KeyType &key, KeyValueCompare comp) + { + detail::key_nodeptr_comp + key_node_comp(comp, this); + return iterator + (node_algorithms::find(const_node_ptr(&priv_header()), key, key_node_comp), this); + } + + //! Effects: Finds a const_iterator to the first element whose key is + //! k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + const_iterator find_dont_splay(const_reference value) const + { return this->find_dont_splay(value, priv_comp()); } + + //! Effects: Finds a const_iterator to the first element whose key is + //! k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + template + const_iterator find_dont_splay(const KeyType &key, KeyValueCompare comp) const + { + detail::key_nodeptr_comp + key_node_comp(comp, this); + return const_iterator + (node_algorithms::find(const_node_ptr(&priv_header()), key, key_node_comp, false), this); + } + + //! Effects: Finds a range containing all elements whose key is k or + //! an empty range that indicates the position where those elements would be + //! if they there is no elements with key k. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + std::pair equal_range(const_reference value) + { return this->equal_range(value, priv_comp()); } + + //! Effects: Finds a range containing all elements whose key is k or + //! an empty range that indicates the position where those elements would be + //! if they there is no elements with key k. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + template + std::pair equal_range(const KeyType &key, KeyValueCompare comp) + { + detail::key_nodeptr_comp + key_node_comp(comp, this); + std::pair ret + (node_algorithms::equal_range(const_node_ptr(&priv_header()), key, key_node_comp)); + return std::pair(iterator(ret.first, this), iterator(ret.second, this)); + } + + //! Effects: Finds a range containing all elements whose key is k or + //! an empty range that indicates the position where those elements would be + //! if they there is no elements with key k. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + std::pair + equal_range_dont_splay(const_reference value) const + { return this->equal_range_dont_splay(value, priv_comp()); } + + //! Effects: Finds a range containing all elements whose key is k or + //! an empty range that indicates the position where those elements would be + //! if they there is no elements with key k. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + template + std::pair + equal_range_dont_splay(const KeyType &key, KeyValueCompare comp) const + { + detail::key_nodeptr_comp + key_node_comp(comp, this); + std::pair ret + (node_algorithms::equal_range(const_node_ptr(&priv_header()), key, key_node_comp, false)); + return std::pair(const_iterator(ret.first, this), const_iterator(ret.second, this)); + } + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! + //! Effects: Erases all the elements from *this + //! calling Disposer::operator()(pointer), clones all the + //! elements from src calling Cloner::operator()(const_reference ) + //! and inserts them on *this. + //! + //! If cloner throws, all cloned elements are unlinked and disposed + //! calling Disposer::operator()(pointer). + //! + //! Complexity: Linear to erased plus inserted elements. + //! + //! Throws: If cloner throws. + template + void clone_from(const splaytree_impl &src, Cloner cloner, Disposer disposer) + { + this->clear_and_dispose(disposer); + if(!src.empty()){ + node_algorithms::clone + (const_node_ptr(&src.priv_header()) + ,node_ptr(&this->priv_header()) + ,detail::node_cloner(cloner, this) + ,detail::node_disposer(disposer, this)); + this->priv_size_traits().set_size(src.priv_size_traits().get_size()); + } + } + + //! Effects: Unlinks the leftmost node from the tree. + //! + //! Complexity: Average complexity is constant time. + //! + //! Throws: Nothing. + //! + //! Notes: This function breaks the tree and the tree can + //! only be used for more unlink_leftmost_without_rebalance calls. + //! This function is normally used to achieve a step by step + //! controlled destruction of the tree. + pointer unlink_leftmost_without_rebalance() + { + node_ptr to_be_disposed(node_algorithms::unlink_leftmost_without_rebalance + (node_ptr(&priv_header()))); + if(!to_be_disposed) + return 0; + this->priv_size_traits().decrement(); + if(safemode_or_autounlink)//If this is commented does not work with normal_link + node_algorithms::init(to_be_disposed); + return get_real_value_traits().to_value_ptr(to_be_disposed); + } + + //! Requires: i must be a valid iterator of *this. + //! + //! Effects: Rearranges the splay set so that the element pointed by i + //! is placed as the root of the tree, improving future searches of this value. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + void splay_up(iterator i) + { return node_algorithms::splay_up(i.pointed_node(), &priv_header()); } + + //! Effects: Rearranges the splay set so that if *this stores an element + //! with a key equivalent to value the element is placed as the root of the + //! tree. If the element is not present returns the last node compared with the key. + //! If the tree is empty, end() is returned. + //! + //! Complexity: Logarithmic. + //! + //! Returns: An iterator to the new root of the tree, end() if the tree is empty. + //! + //! Throws: If the comparison functor throws. + template + iterator splay_down(const KeyType &key, KeyValueCompare comp) + { + detail::key_nodeptr_comp + key_node_comp(comp, this); + node_ptr r = node_algorithms::splay_down(&priv_header(), key, key_node_comp); + return iterator(r, this); + } + + //! Effects: Rearranges the splay set so that if *this stores an element + //! with a key equivalent to value the element is placed as the root of the + //! tree. + //! + //! Complexity: Logarithmic. + //! + //! Returns: An iterator to the new root of the tree, end() if the tree is empty. + //! + //! Throws: If the predicate throws. + iterator splay_down(const value_type &value) + { return this->splay_down(value, priv_comp()); } + + //! Requires: replace_this must be a valid iterator of *this + //! and with_this must not be inserted in any tree. + //! + //! Effects: Replaces replace_this in its position in the + //! tree with with_this. The tree does not need to be rebalanced. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Note: This function will break container ordering invariants if + //! with_this is not equivalent to *replace_this according to the + //! ordering rules. This function is faster than erasing and inserting + //! the node, since no rebalancing or comparison is needed. + void replace_node(iterator replace_this, reference with_this) + { + node_algorithms::replace_node( get_real_value_traits().to_node_ptr(*replace_this) + , node_ptr(&priv_header()) + , get_real_value_traits().to_node_ptr(with_this)); + } + + //! Requires: value must be an lvalue and shall be in a set of + //! appropriate type. Otherwise the behavior is undefined. + //! + //! Effects: Returns: a valid iterator i belonging to the set + //! that points to the value + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Note: This static function is available only if the value traits + //! is stateless. + static iterator s_iterator_to(reference value) + { + BOOST_STATIC_ASSERT((!stateful_value_traits)); + return iterator (value_traits::to_node_ptr(value), 0); + } + + //! Requires: value must be an lvalue and shall be in a set of + //! appropriate type. Otherwise the behavior is undefined. + //! + //! Effects: Returns: a valid const_iterator i belonging to the + //! set that points to the value + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Note: This static function is available only if the value traits + //! is stateless. + static const_iterator s_iterator_to(const_reference value) + { + BOOST_STATIC_ASSERT((!stateful_value_traits)); + return const_iterator (value_traits::to_node_ptr(const_cast (value)), 0); + } + + //! Requires: value must be an lvalue and shall be in a set of + //! appropriate type. Otherwise the behavior is undefined. + //! + //! Effects: Returns: a valid iterator i belonging to the set + //! that points to the value + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + iterator iterator_to(reference value) + { return iterator (value_traits::to_node_ptr(value), this); } + + //! Requires: value must be an lvalue and shall be in a set of + //! appropriate type. Otherwise the behavior is undefined. + //! + //! Effects: Returns: a valid const_iterator i belonging to the + //! set that points to the value + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_iterator iterator_to(const_reference value) const + { return const_iterator (value_traits::to_node_ptr(const_cast (value)), this); } + + //! Requires: value shall not be in a tree. + //! + //! Effects: init_node puts the hook of a value in a well-known default + //! state. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant time. + //! + //! Note: This function puts the hook in the well-known default state + //! used by auto_unlink and safe hooks. + static void init_node(reference value) + { node_algorithms::init(value_traits::to_node_ptr(value)); } + +/* + //! Effects: removes x from a tree of the appropriate type. It has no effect, + //! if x is not in such a tree. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant time. + //! + //! Note: This static function is only usable with the "safe mode" + //! hook and non-constant time size lists. Otherwise, the user must use + //! the non-static "erase(reference )" member. If the user calls + //! this function with a non "safe mode" or constant time size list + //! a compilation error will be issued. + template + static void remove_node(T& value) + { + //This function is only usable for safe mode hooks and non-constant + //time lists. + //BOOST_STATIC_ASSERT((!(safemode_or_autounlink && constant_time_size))); + BOOST_STATIC_ASSERT((!constant_time_size)); + BOOST_STATIC_ASSERT((boost::is_convertible::value)); + node_ptr to_remove(value_traits::to_node_ptr(value)); + node_algorithms::unlink_and_rebalance(to_remove); + if(safemode_or_autounlink) + node_algorithms::init(to_remove); + } +*/ + + /// @cond + private: + template + iterator private_erase(iterator b, iterator e, size_type &n, Disposer disposer) + { + for(n = 0; b != e; ++n) + this->erase_and_dispose(b++, disposer); + return b; + } + + iterator private_erase(iterator b, iterator e, size_type &n) + { + for(n = 0; b != e; ++n) + this->erase(b++); + return b; + } + /// @endcond + + private: + static splaytree_impl &priv_container_from_end_iterator(const const_iterator &end_iterator) + { + header_plus_size *r = detail::parent_from_member + ( detail::get_pointer(end_iterator.pointed_node()), &header_plus_size::header_); + node_plus_pred_t *n = detail::parent_from_member + (r, &node_plus_pred_t::header_plus_size_); + data_t *d = detail::parent_from_member(n, &data_t::node_plus_pred_); + splaytree_impl *rb = detail::parent_from_member(d, &splaytree_impl::data_); + return *rb; + } +}; + +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +inline bool operator< +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +(const splaytree_impl &x, const splaytree_impl &y) +#else +(const splaytree_impl &x, const splaytree_impl &y) +#endif +{ return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); } + +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +bool operator== +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +(const splaytree_impl &x, const splaytree_impl &y) +#else +(const splaytree_impl &x, const splaytree_impl &y) +#endif +{ + typedef splaytree_impl tree_type; + typedef typename tree_type::const_iterator const_iterator; + + if(tree_type::constant_time_size && x.size() != y.size()){ + return false; + } + const_iterator end1 = x.end(); + const_iterator i1 = x.begin(); + const_iterator i2 = y.begin(); + if(tree_type::constant_time_size){ + while (i1 != end1 && *i1 == *i2) { + ++i1; + ++i2; + } + return i1 == end1; + } + else{ + const_iterator end2 = y.end(); + while (i1 != end1 && i2 != end2 && *i1 == *i2) { + ++i1; + ++i2; + } + return i1 == end1 && i2 == end2; + } +} + +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +inline bool operator!= +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +(const splaytree_impl &x, const splaytree_impl &y) +#else +(const splaytree_impl &x, const splaytree_impl &y) +#endif +{ return !(x == y); } + +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +inline bool operator> +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +(const splaytree_impl &x, const splaytree_impl &y) +#else +(const splaytree_impl &x, const splaytree_impl &y) +#endif +{ return y < x; } + +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +inline bool operator<= +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +(const splaytree_impl &x, const splaytree_impl &y) +#else +(const splaytree_impl &x, const splaytree_impl &y) +#endif +{ return !(y < x); } + +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +inline bool operator>= +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +(const splaytree_impl &x, const splaytree_impl &y) +#else +(const splaytree_impl &x, const splaytree_impl &y) +#endif +{ return !(x < y); } + +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +inline void swap +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +(splaytree_impl &x, splaytree_impl &y) +#else +(splaytree_impl &x, splaytree_impl &y) +#endif +{ x.swap(y); } + +/// @cond +template +struct make_splaytree_opt +{ + typedef typename pack_options + < splay_set_defaults, O1, O2, O3, O4>::type packed_options; + typedef typename detail::get_value_traits + ::type value_traits; + + typedef splaysetopt + < value_traits + , typename packed_options::compare + , typename packed_options::size_type + , packed_options::constant_time_size + > type; +}; +/// @endcond + +//! Helper metafunction to define a \c splaytree that yields to the same type when the +//! same options (either explicitly or implicitly) are used. +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +struct make_splaytree +{ + /// @cond + typedef splaytree_impl + < typename make_splaytree_opt::type + > implementation_defined; + /// @endcond + typedef implementation_defined type; +}; + +#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +class splaytree + : public make_splaytree::type +{ + typedef typename make_splaytree + ::type Base; + + public: + typedef typename Base::value_compare value_compare; + typedef typename Base::value_traits value_traits; + typedef typename Base::real_value_traits real_value_traits; + typedef typename Base::iterator iterator; + typedef typename Base::const_iterator const_iterator; + + //Assert if passed value traits are compatible with the type + BOOST_STATIC_ASSERT((detail::is_same::value)); + + splaytree( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : Base(cmp, v_traits) + {} + + template + splaytree( bool unique, Iterator b, Iterator e + , const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : Base(unique, b, e, cmp, v_traits) + {} + + static splaytree &container_from_end_iterator(iterator end_iterator) + { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static const splaytree &container_from_end_iterator(const_iterator end_iterator) + { return static_cast(Base::container_from_end_iterator(end_iterator)); } +}; + +#endif + + +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_SPLAYTREE_HPP diff --git a/include/boost/intrusive/splaytree_algorithms.hpp b/include/boost/intrusive/splaytree_algorithms.hpp new file mode 100644 index 0000000..92a11ec --- /dev/null +++ b/include/boost/intrusive/splaytree_algorithms.hpp @@ -0,0 +1,828 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2007. +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +// The implementation of splay trees is based on the article and code published +// in C++ Users Journal "Implementing Splay Trees in C++" (September 1, 2005). +// +// The code has been modified and (supposely) improved by Ion Gaztanaga. +// Here is the header of the file used as base code: +// +// splay_tree.h -- implementation of a STL complatible splay tree. +// +// Copyright (c) 2004 Ralf Mattethat +// +// Permission to copy, use, modify, sell and distribute this software +// is granted provided this copyright notice appears in all copies. +// This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. +// +// Please send questions, comments, complaints, performance data, etc to +// ralf.mattethat@teknologisk.dk +// +// Requirements for element type +// * must be copy-constructible +// * destructor must not throw exception +// +// Methods marked with note A only throws an exception if the evaluation of the +// predicate throws an exception. If an exception is thrown the call has no +// effect on the containers state +// +// Methods marked with note B only throws an exception if the coppy constructor +// or assignment operator of the predicate throws an exception. If an exception +// is thrown the call has no effect on the containers state +// +// iterators are only invalidated, if the element pointed to by the iterator +// is deleted. The same goes for element references +// + +#ifndef BOOST_INTRUSIVE_SPLAYTREE_ALGORITHMS_HPP +#define BOOST_INTRUSIVE_SPLAYTREE_ALGORITHMS_HPP + +#include +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace intrusive { + +//! A splay tree is an implementation of a binary search tree. The tree is +//! self balancing using the splay algorithm as described in +//! +//! "Self-Adjusting Binary Search Trees +//! by Daniel Dominic Sleator and Robert Endre Tarjan +//! AT&T Bell Laboratories, Murray Hill, NJ +//! Journal of the ACM, Vol 32, no 3, July 1985, pp 652-686 + +//! splaytree_algorithms is configured with a NodeTraits class, which encapsulates the +//! information about the node to be manipulated. NodeTraits must support the +//! following interface: +//! +//! Typedefs: +//! +//! node: The type of the node that forms the circular list +//! +//! node_ptr: A pointer to a node +//! +//! const_node_ptr: A pointer to a const node +//! +//! Static functions: +//! +//! static node_ptr get_parent(const_node_ptr n); +//! +//! static void set_parent(node_ptr n, node_ptr parent); +//! +//! static node_ptr get_left(const_node_ptr n); +//! +//! static void set_left(node_ptr n, node_ptr left); +//! +//! static node_ptr get_right(const_node_ptr n); +//! +//! static void set_right(node_ptr n, node_ptr right); +template +class splaytree_algorithms +{ + /// @cond + private: + typedef typename NodeTraits::node node; + typedef detail::tree_algorithms tree_algorithms; + /// @endcond + + public: + typedef NodeTraits node_traits; + typedef typename NodeTraits::node_ptr node_ptr; + typedef typename NodeTraits::const_node_ptr const_node_ptr; + + //! This type is the information that will be + //! filled by insert_unique_check + typedef typename tree_algorithms::insert_commit_data insert_commit_data; + + /// @cond + private: + static node_ptr uncast(const_node_ptr ptr) + { + return node_ptr(const_cast(::boost::intrusive::detail::get_pointer(ptr))); + } + /// @endcond + + public: + static node_ptr begin_node(const_node_ptr header) + { return tree_algorithms::begin_node(header); } + + static node_ptr end_node(const_node_ptr header) + { return tree_algorithms::end_node(header); } + + //! Requires: node is a node of the tree or an node initialized + //! by init(...). + //! + //! Effects: Returns true if the node is initialized by init(). + //! + //! Complexity: Constant time. + //! + //! Throws: Nothing. + static bool unique(const_node_ptr node) + { return tree_algorithms::unique(node); } + + static void unlink(node_ptr node) + { tree_algorithms::unlink(node); } + + //! Requires: node1 and node2 can't be header nodes + //! of two trees. + //! + //! Effects: Swaps two nodes. After the function node1 will be inserted + //! in the position node2 before the function. node2 will be inserted in the + //! position node1 had before the function. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + //! + //! Note: This function will break container ordering invariants if + //! node1 and node2 are not equivalent according to the ordering rules. + //! + //!Experimental function + static void swap_nodes(node_ptr node1, node_ptr node2) + { + if(node1 == node2) + return; + + node_ptr header1(tree_algorithms::get_header(node1)), header2(tree_algorithms::get_header(node2)); + swap_nodes(node1, header1, node2, header2); + } + + //! Requires: node1 and node2 can't be header nodes + //! of two trees with header header1 and header2. + //! + //! Effects: Swaps two nodes. After the function node1 will be inserted + //! in the position node2 before the function. node2 will be inserted in the + //! position node1 had before the function. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Note: This function will break container ordering invariants if + //! node1 and node2 are not equivalent according to the ordering rules. + //! + //!Experimental function + static void swap_nodes(node_ptr node1, node_ptr header1, node_ptr node2, node_ptr header2) + { tree_algorithms::swap_nodes(node1, header1, node2, header2); } + + //! Requires: node_to_be_replaced must be inserted in a tree + //! and new_node must not be inserted in a tree. + //! + //! Effects: Replaces node_to_be_replaced in its position in the + //! tree with new_node. The tree does not need to be rebalanced + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + //! + //! Note: This function will break container ordering invariants if + //! new_node is not equivalent to node_to_be_replaced according to the + //! ordering rules. This function is faster than erasing and inserting + //! the node, since no rebalancing and comparison is needed. + //! + //!Experimental function + static void replace_node(node_ptr node_to_be_replaced, node_ptr new_node) + { + if(node_to_be_replaced == new_node) + return; + replace_node(node_to_be_replaced, tree_algorithms::get_header(node_to_be_replaced), new_node); + } + + //! Requires: node_to_be_replaced must be inserted in a tree + //! with header "header" and new_node must not be inserted in a tree. + //! + //! Effects: Replaces node_to_be_replaced in its position in the + //! tree with new_node. The tree does not need to be rebalanced + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Note: This function will break container ordering invariants if + //! new_node is not equivalent to node_to_be_replaced according to the + //! ordering rules. This function is faster than erasing and inserting + //! the node, since no rebalancing or comparison is needed. + //! + //!Experimental function + static void replace_node(node_ptr node_to_be_replaced, node_ptr header, node_ptr new_node) + { tree_algorithms::replace_node(node_to_be_replaced, header, new_node); } + + //! Requires: p is a node from the tree except the header. + //! + //! Effects: Returns the next node of the tree. + //! + //! Complexity: Average constant time. + //! + //! Throws: Nothing. + static node_ptr next_node(node_ptr p) + { return tree_algorithms::next_node(p); } + + //! Requires: p is a node from the tree except the leftmost node. + //! + //! Effects: Returns the previous node of the tree. + //! + //! Complexity: Average constant time. + //! + //! Throws: Nothing. + static node_ptr prev_node(node_ptr p) + { return tree_algorithms::prev_node(p); } + + //! Requires: node must not be part of any tree. + //! + //! Effects: After the function unique(node) == true. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Nodes: If node is inserted in a tree, this function corrupts the tree. + static void init(node_ptr node) + { tree_algorithms::init(node); } + + //! Requires: node must not be part of any tree. + //! + //! Effects: Initializes the header to represent an empty tree. + //! unique(header) == true. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Nodes: If node is inserted in a tree, this function corrupts the tree. + static void init_header(node_ptr header) + { tree_algorithms::init_header(header); } + + //! Requires: "disposer" must be an object function + //! taking a node_ptr parameter and shouldn't throw. + //! + //! Effects: Empties the target tree calling + //! void disposer::operator()(node_ptr) for every node of the tree + //! except the header. + //! + //! Complexity: Linear to the number of element of the source tree plus the. + //! number of elements of tree target tree when calling this function. + //! + //! Throws: If cloner functor throws. If this happens target nodes are disposed. + template + static void clear_and_dispose(node_ptr header, Disposer disposer) + { tree_algorithms::clear_and_dispose(header, disposer); } + + //! Requires: node is a node of the tree but it's not the header. + //! + //! Effects: Returns the number of nodes of the subtree. + //! + //! Complexity: Linear time. + //! + //! Throws: Nothing. + static std::size_t count(const_node_ptr node) + { return tree_algorithms::count(node); } + + //! Requires: header1 and header2 must be the header nodes + //! of two trees. + //! + //! Effects: Swaps two trees. After the function header1 will contain + //! links to the second tree and header2 will have links to the first tree. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + static void swap_tree(node_ptr header1, node_ptr header2) + { return tree_algorithms::swap_tree(header1, header2); } + + //! Requires: "header" must be the header node of a tree. + //! "commit_data" must have been obtained from a previous call to + //! "insert_unique_check". No objects should have been inserted or erased + //! from the set between the "insert_unique_check" that filled "commit_data" + //! and the call to "insert_commit". + //! + //! + //! Effects: Inserts new_node in the set using the information obtained + //! from the "commit_data" that a previous "insert_check" filled. + //! + //! Complexity: Constant time. + //! + //! Throws: Nothing. + //! + //! Notes: This function has only sense if a "insert_unique_check" has been + //! previously executed to fill "commit_data". No value should be inserted or + //! erased between the "insert_check" and "insert_commit" calls. + static void insert_unique_commit + (node_ptr header, node_ptr new_value, const insert_commit_data &commit_data) + { tree_algorithms::insert_unique_commit(header, new_value, commit_data); } + + //! Requires: "header" must be the header node of a tree. + //! KeyNodePtrCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the tree. NodePtrCompare compares KeyType with a node_ptr. + //! + //! Effects: Checks if there is an equivalent node to "key" in the + //! tree according to "comp" and obtains the needed information to realize + //! a constant-time node insertion if there is no equivalent node. + //! + //! Returns: If there is an equivalent value + //! returns a pair containing a node_ptr to the already present node + //! and false. If there is not equivalent key can be inserted returns true + //! in the returned pair's boolean and fills "commit_data" that is meant to + //! be used with the "insert_commit" function to achieve a constant-time + //! insertion function. + //! + //! Complexity: Average complexity is at most logarithmic. + //! + //! Throws: If "comp" throws. + //! + //! Notes: This function is used to improve performance when constructing + //! a node is expensive and the user does not want to have two equivalent nodes + //! in the tree: if there is an equivalent value + //! the constructed object must be discarded. Many times, the part of the + //! node that is used to impose the order is much cheaper to construct + //! than the node and this function offers the possibility to use that part + //! to check if the insertion will be successful. + //! + //! If the check is successful, the user can construct the node and use + //! "insert_commit" to insert the node in constant-time. This gives a total + //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)). + //! + //! "commit_data" remains valid for a subsequent "insert_unique_commit" only + //! if no more objects are inserted or erased from the set. + template + static std::pair insert_unique_check + (node_ptr header, const KeyType &key + ,KeyNodePtrCompare comp, insert_commit_data &commit_data, bool splay = true) + { + if(splay) + splay_down(header, key, comp); + return tree_algorithms::insert_unique_check(header, key, comp, commit_data); + } + + template + static std::pair insert_unique_check + (node_ptr header, node_ptr hint, const KeyType &key + ,KeyNodePtrCompare comp, insert_commit_data &commit_data, bool splay = true) + { + if(splay) + splay_down(header, key, comp); + return tree_algorithms::insert_unique_check(header, hint, key, comp, commit_data); + } + + static bool is_header(const_node_ptr p) + { return tree_algorithms::is_header(p); } + + //! Requires: "header" must be the header node of a tree. + //! KeyNodePtrCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs. + //! + //! Effects: Returns an node_ptr to the element that is equivalent to + //! "key" according to "comp" or "header" if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If "comp" throws. + template + static node_ptr find + (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp, bool splay = true) + { + if(splay) + splay_down(uncast(header), key, comp); + node_ptr end = uncast(header); + node_ptr y = lower_bound(header, key, comp, false); + node_ptr r = (y == end || comp(key, y)) ? end : y; + return r; + } + + //! Requires: "header" must be the header node of a tree. + //! KeyNodePtrCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs. + //! + //! Effects: Returns an a pair of node_ptr delimiting a range containing + //! all elements that are equivalent to "key" according to "comp" or an + //! empty range that indicates the position where those elements would be + //! if they there are no equivalent elements. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If "comp" throws. + template + static std::pair equal_range + (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp, bool splay = true) + { + //if(splay) + //splay_down(uncast(header), key, comp); + std::pair ret = + tree_algorithms::equal_range(header, key, comp); + + if(splay) + splay_up(ret.first, uncast(header)); + return ret; + } + + //! Requires: "header" must be the header node of a tree. + //! KeyNodePtrCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs. + //! + //! Effects: Returns an node_ptr to the first element that is + //! not less than "key" according to "comp" or "header" if that element does + //! not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If "comp" throws. + template + static node_ptr lower_bound + (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp, bool splay = true) + { + //if(splay) + //splay_down(uncast(header), key, comp); + node_ptr y = tree_algorithms::lower_bound(header, key, comp); + if(splay) + splay_up(y, uncast(header)); + return y; + } + + //! Requires: "header" must be the header node of a tree. + //! KeyNodePtrCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs. + //! + //! Effects: Returns an node_ptr to the first element that is greater + //! than "key" according to "comp" or "header" if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If "comp" throws. + template + static node_ptr upper_bound + (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp, bool splay = true) + { + //if(splay) + //splay_down(uncast(header), key, comp); + node_ptr y = tree_algorithms::upper_bound(header, key, comp); + if(splay) + splay_up(y, uncast(header)); + return y; + } + + //! Requires: "header" must be the header node of a tree. + //! NodePtrCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the tree. NodePtrCompare compares two node_ptrs. "hint" is node from + //! the "header"'s tree. + //! + //! Effects: Inserts new_node into the tree, using "hint" as a hint to + //! where it will be inserted. If "hint" is the upper_bound + //! the insertion takes constant time (two comparisons in the worst case). + //! + //! Complexity: Logarithmic in general, but it is amortized + //! constant time if new_node is inserted immediately before "hint". + //! + //! Throws: If "comp" throws. + template + static node_ptr insert_equal + (node_ptr header, node_ptr hint, node_ptr new_node, NodePtrCompare comp, bool splay = true) + { + if(splay) + splay_down(header, new_node, comp); + return tree_algorithms::insert_equal(header, hint, new_node, comp); + } + + template + static node_ptr insert_equal_upper_bound + (node_ptr header, node_ptr new_node, NodePtrCompare comp, bool splay = true) + { + if(splay) + splay_down(header, new_node, comp); + return tree_algorithms::insert_equal_upper_bound(header, new_node, comp); + } + + template + static node_ptr insert_equal_lower_bound + (node_ptr header, node_ptr new_node, NodePtrCompare comp, bool splay = true) + { + if(splay) + splay_down(header, new_node, comp); + return tree_algorithms::insert_equal_lower_bound(header, new_node, comp); + } + + //! Requires: "cloner" must be a function + //! object taking a node_ptr and returning a new cloned node of it. "disposer" must + //! take a node_ptr and shouldn't throw. + //! + //! Effects: First empties target tree calling + //! void disposer::operator()(node_ptr) for every node of the tree + //! except the header. + //! + //! Then, duplicates the entire tree pointed by "source_header" cloning each + //! source node with node_ptr Cloner::operator()(node_ptr) to obtain + //! the nodes of the target tree. If "cloner" throws, the cloned target nodes + //! are disposed using void disposer(node_ptr). + //! + //! Complexity: Linear to the number of element of the source tree plus the. + //! number of elements of tree target tree when calling this function. + //! + //! Throws: If cloner functor throws. If this happens target nodes are disposed. + template + static void clone + (const_node_ptr source_header, node_ptr target_header, Cloner cloner, Disposer disposer) + { tree_algorithms::clone(source_header, target_header, cloner, disposer); } + + // delete node | complexity : constant | exception : nothrow + static void erase(node_ptr header, node_ptr z, bool splay = true) + { +// node_base* n = t->right; +// if( t->left != 0 ){ +// node_base* l = t->previous(); +// splay_up( l , t ); +// n = t->left; +// n->right = t->right; +// if( n->right != 0 ) +// n->right->parent = n; +// } +// +// if( n != 0 ) +// n->parent = t->parent; +// +// if( t->parent->left == t ) +// t->parent->left = n; +// else // must be ( t->parent->right == t ) +// t->parent->right = n; +// +// if( data_->parent == t ) +// data_->parent = find_leftmost(); + //posibility 1 + if(splay && NodeTraits::get_left(z) != 0 ){ + node_ptr l = prev_node(z); + splay_up(l, header); + } + /* + //possibility 2 + if(splay && NodeTraits::get_left(z) != 0 ){ + node_ptr l = NodeTraits::get_left(z); + splay_up(l, header); + }*//* + if(splay && NodeTraits::get_left(z) != 0 ){ + node_ptr l = prev_node(z); + splay_up_impl(l, z); + }*/ + /* + //possibility 4 + if(splay){ + splay_up(z, header); + }*/ + + //if(splay) + //splay_up(z, header); + tree_algorithms::erase(header, z); + } + + // bottom-up splay, use data_ as parent for n | complexity : logarithmic | exception : nothrow + static void splay_up(node_ptr n, node_ptr header) + { + if(n == header){ // do a splay for the right most node instead + // this is to boost performance of equal_range/count on equivalent containers in the case + // where there are many equal elements at the end + n = NodeTraits::get_right(header); + } + + node_ptr t = header; + + if( n == t ) return; + + for( ;; ){ + node_ptr p = NodeTraits::get_parent(n); + node_ptr g = NodeTraits::get_parent(p); + + if( p == t ) break; + + if( g == t ){ + // zig + rotate(n); + } + else if ((NodeTraits::get_left(p) == n && NodeTraits::get_left(g) == p) || + (NodeTraits::get_right(p) == n && NodeTraits::get_right(g) == p) ){ + // zig-zig + rotate(p); + rotate(n); + } + else{ + // zig-zag + rotate(n); + rotate(n); + } + } + } + + // top-down splay | complexity : logarithmic | exception : strong, note A + template + static node_ptr splay_down(node_ptr header, const KeyType &key, KeyNodePtrCompare comp, bool splay = true) + { + if(!NodeTraits::get_parent(header)) + return header; + //Most splay tree implementations use a dummy/null node to implement. + //this function. This has some problems for a generic library like Intrusive: + // + // * The node might not have a default constructor. + // * The default constructor could throw. + // + //We already have a header node. Leftmost and rightmost nodes of the tree + //are not changed when splaying (because the invariants of the tree don't + //change) We can back up them, use the header as the null node and + //reassign old values after the function has been completed. + node_ptr t = NodeTraits::get_parent(header); + //Check if tree has a single node + if(!NodeTraits::get_left(t) && !NodeTraits::get_right(t)) + return t; + //Backup leftmost/rightmost + node_ptr leftmost = NodeTraits::get_left(header); + node_ptr rightmost = NodeTraits::get_right(header); + + try{ + node_ptr null = header; + node_ptr l = null; + node_ptr r = null; + + for( ;; ){ + if(comp(key, t)){ + if(NodeTraits::get_left(t) == 0 ) + break; + if(comp(key, NodeTraits::get_left(t))){ + t = tree_algorithms::rotate_right(t); + + if(NodeTraits::get_left(t) == 0) + break; + link_right(t, r); + } + else if(comp(NodeTraits::get_left(t), key)){ + link_right(t, r); + + if(NodeTraits::get_right(t) == 0 ) + break; + link_left(t, l); + } + else{ + link_right(t, r); + } + } + else if(comp(t, key)){ + if(NodeTraits::get_right(t) == 0 ) + break; + + if(comp(NodeTraits::get_right(t), key)){ + t = tree_algorithms::rotate_left( t ); + + if(NodeTraits::get_right(t) == 0 ) + break; + link_left(t, l); + } + else if(comp(key, NodeTraits::get_right(t))){ + link_left(t, l); + + if(NodeTraits::get_left(t) == 0) + break; + + link_right(t, r); + } + else{ + link_left(t, l); + } + } + else{ + break; + } + } + + assemble(t, l, r, null); + } + catch(...){ + //Exception can only be thrown by comp, but + //tree invariants still hold. t is the current root + //so link it to the header. + NodeTraits::set_parent(t, header); + NodeTraits::set_parent(header, t); + //Recover leftmost/rightmost pointers + NodeTraits::set_left (header, leftmost); + NodeTraits::set_right(header, rightmost); + throw; + } + //t is the current root + NodeTraits::set_parent(header, t); + NodeTraits::set_parent(t, header); + //Recover leftmost/rightmost pointers + NodeTraits::set_left (header, leftmost); + NodeTraits::set_right(header, rightmost); + return t; + } + + private: + + /// @cond + + // assemble the three sub-trees into new tree pointed to by t | complexity : constant | exception : nothrow + static void assemble( node_ptr t, node_ptr l, node_ptr r, const_node_ptr null_node ) + { + NodeTraits::set_right(l, NodeTraits::get_left(t)); + NodeTraits::set_left(r, NodeTraits::get_right(t)); + + if(NodeTraits::get_right(l) != 0){ + NodeTraits::set_parent(NodeTraits::get_right(l), l); + } + + if(NodeTraits::get_left(r) != 0){ + NodeTraits::set_parent(NodeTraits::get_left(r), r); + } + + NodeTraits::set_left (t, NodeTraits::get_right(null_node)); + NodeTraits::set_right(t, NodeTraits::get_left(null_node)); + + if( NodeTraits::get_left(t) != 0 ){ + NodeTraits::set_parent(NodeTraits::get_left(t), t); + } + + if( NodeTraits::get_right(t) ){ + NodeTraits::set_parent(NodeTraits::get_right(t), t); + } + } + + // break link to left child node and attach it to left tree pointed to by l | complexity : constant | exception : nothrow + static void link_left(node_ptr& t, node_ptr& l) + { + NodeTraits::set_right(l, t); + NodeTraits::set_parent(t, l); + l = t; + t = NodeTraits::get_right(t); + } + + // break link to right child node and attach it to right tree pointed to by r | complexity : constant | exception : nothrow + static void link_right(node_ptr& t, node_ptr& r) + { + NodeTraits::set_left(r, t); + NodeTraits::set_parent(t, r); + r = t; + t = NodeTraits::get_left(t); + } + + // rotate n with its parent | complexity : constant | exception : nothrow + static void rotate(node_ptr n) + { + node_ptr p = NodeTraits::get_parent(n); + node_ptr g = NodeTraits::get_parent(p); + //Test if g is header before breaking tree + //invariants that would make is_header invalid + bool g_is_header = is_header(g); + + if(NodeTraits::get_left(p) == n){ + NodeTraits::set_left(p, NodeTraits::get_right(n)); + if(NodeTraits::get_left(p) != 0) + NodeTraits::set_parent(NodeTraits::get_left(p), p); + NodeTraits::set_right(n, p); + } + else{ // must be ( p->right == n ) + NodeTraits::set_right(p, NodeTraits::get_left(n)); + if(NodeTraits::get_right(p) != 0) + NodeTraits::set_parent(NodeTraits::get_right(p), p); + NodeTraits::set_left(n, p); + } + + NodeTraits::set_parent(p, n); + NodeTraits::set_parent(n, g); + + if(g_is_header){ + if(NodeTraits::get_parent(g) == p) + NodeTraits::set_parent(g, n); + else{//must be ( g->right == p ) + assert(0); + NodeTraits::set_right(g, n); + } + } + else{ + if(NodeTraits::get_left(g) == p) + NodeTraits::set_left(g, n); + else //must be ( g->right == p ) + NodeTraits::set_right(g, n); + } + } + + /// @endcond +}; + +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_SPLAYTREE_ALGORITHMS_HPP