Chris@16: ///////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@101: // (C) Copyright Ion Gaztanaga 2007-2014 Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. Chris@16: // (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org/libs/intrusive for documentation. Chris@16: // Chris@16: ///////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // Scapegoat tree algorithms are taken from the paper titled: Chris@16: // "Scapegoat Trees" by Igal Galperin Ronald L. Rivest. Chris@16: // Chris@16: ///////////////////////////////////////////////////////////////////////////// Chris@16: #ifndef BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP Chris@16: #define BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP Chris@16: Chris@16: #include Chris@101: #include Chris@16: Chris@16: #include Chris@101: #include Chris@16: #include Chris@16: Chris@101: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@101: # pragma once Chris@101: #endif Chris@16: Chris@16: namespace boost { Chris@16: namespace intrusive { Chris@16: Chris@16: //! sgtree_algorithms is configured with a NodeTraits class, which encapsulates the Chris@16: //! information about the node to be manipulated. NodeTraits must support the Chris@16: //! following interface: Chris@16: //! Chris@16: //! Typedefs: Chris@16: //! Chris@16: //! node: The type of the node that forms the binary search tree Chris@16: //! Chris@16: //! node_ptr: A pointer to a node Chris@16: //! Chris@16: //! const_node_ptr: A pointer to a const node Chris@16: //! Chris@16: //! Static functions: Chris@16: //! Chris@16: //! static node_ptr get_parent(const_node_ptr n); Chris@16: //! Chris@16: //! static void set_parent(node_ptr n, node_ptr parent); Chris@16: //! Chris@16: //! static node_ptr get_left(const_node_ptr n); Chris@16: //! Chris@16: //! static void set_left(node_ptr n, node_ptr left); Chris@16: //! Chris@16: //! static node_ptr get_right(const_node_ptr n); Chris@16: //! Chris@16: //! static void set_right(node_ptr n, node_ptr right); Chris@16: template Chris@16: class sgtree_algorithms Chris@16: #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@16: : public bstree_algorithms Chris@16: #endif Chris@16: { Chris@16: public: Chris@16: typedef typename NodeTraits::node node; Chris@16: typedef NodeTraits node_traits; Chris@16: typedef typename NodeTraits::node_ptr node_ptr; Chris@16: typedef typename NodeTraits::const_node_ptr const_node_ptr; Chris@16: Chris@16: /// @cond Chris@16: private: Chris@16: Chris@16: typedef bstree_algorithms bstree_algo; Chris@16: Chris@16: /// @endcond Chris@16: Chris@16: public: Chris@16: //! This type is the information that will be Chris@16: //! filled by insert_unique_check Chris@16: struct insert_commit_data Chris@16: : bstree_algo::insert_commit_data Chris@16: { Chris@16: std::size_t depth; Chris@16: }; Chris@16: Chris@16: #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::get_header(const const_node_ptr&) Chris@16: static node_ptr get_header(const const_node_ptr & n); Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::begin_node Chris@16: static node_ptr begin_node(const const_node_ptr & header); Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::end_node Chris@16: static node_ptr end_node(const const_node_ptr & header); Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::swap_tree Chris@16: static void swap_tree(const node_ptr & header1, const node_ptr & header2); Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(const node_ptr&,const node_ptr&) Chris@16: static void swap_nodes(const node_ptr & node1, const node_ptr & node2); Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(const node_ptr&,const node_ptr&,const node_ptr&,const node_ptr&) Chris@16: static void swap_nodes(const node_ptr & node1, const node_ptr & header1, const node_ptr & node2, const node_ptr & header2); Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(const node_ptr&,const node_ptr&) Chris@16: static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & new_node); Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(const node_ptr&,const node_ptr&,const node_ptr&) Chris@16: static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & header, const node_ptr & new_node); Chris@16: Chris@16: //Unlink is not possible since tree metadata is needed to update the tree Chris@16: //!static void unlink(const node_ptr & node); Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::unlink_leftmost_without_rebalance Chris@16: static node_ptr unlink_leftmost_without_rebalance(const node_ptr & header); Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::unique(const const_node_ptr&) Chris@16: static bool unique(const const_node_ptr & node); Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::size(const const_node_ptr&) Chris@16: static std::size_t size(const const_node_ptr & header); Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::next_node(const node_ptr&) Chris@16: static node_ptr next_node(const node_ptr & node); Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::prev_node(const node_ptr&) Chris@16: static node_ptr prev_node(const node_ptr & node); Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::init(const node_ptr&) Chris@16: static void init(const node_ptr & node); Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::init_header(const node_ptr&) Chris@16: static void init_header(const node_ptr & header); Chris@16: #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::erase(const node_ptr&,const node_ptr&) Chris@16: template Chris@16: static node_ptr erase(const node_ptr & header, const node_ptr & z, std::size_t tree_size, std::size_t &max_tree_size, AlphaByMaxSize alpha_by_maxsize) Chris@16: { Chris@16: //typename bstree_algo::data_for_rebalance info; Chris@16: bstree_algo::erase(header, z); Chris@16: --tree_size; Chris@16: if (tree_size > 0 && Chris@16: tree_size < alpha_by_maxsize(max_tree_size)){ Chris@16: bstree_algo::rebalance(header); Chris@16: max_tree_size = tree_size; Chris@16: } Chris@16: return z; Chris@16: } Chris@16: Chris@16: #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::clone(const const_node_ptr&,const node_ptr&,Cloner,Disposer) Chris@16: template Chris@16: static void clone Chris@16: (const const_node_ptr & source_header, const node_ptr & target_header, Cloner cloner, Disposer disposer); Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::clear_and_dispose(const node_ptr&,Disposer) Chris@16: template Chris@16: static void clear_and_dispose(const node_ptr & header, Disposer disposer); Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::lower_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare) Chris@16: template Chris@16: static node_ptr lower_bound Chris@16: (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp); Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::upper_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare) Chris@16: template Chris@16: static node_ptr upper_bound Chris@16: (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp); Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::find(const const_node_ptr&, const KeyType&,KeyNodePtrCompare) Chris@16: template Chris@16: static node_ptr find Chris@16: (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp); Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::equal_range(const const_node_ptr&,const KeyType&,KeyNodePtrCompare) Chris@16: template Chris@16: static std::pair equal_range Chris@16: (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp); Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::bounded_range(const const_node_ptr&,const KeyType&,const KeyType&,KeyNodePtrCompare,bool,bool) Chris@16: template Chris@16: static std::pair bounded_range Chris@16: (const const_node_ptr & header, const KeyType &lower_key, const KeyType &upper_key, KeyNodePtrCompare comp Chris@16: , bool left_closed, bool right_closed); Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::count(const const_node_ptr&,const KeyType&,KeyNodePtrCompare) Chris@16: template Chris@16: static std::size_t count(const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp); Chris@101: Chris@16: #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_upper_bound(const node_ptr&,const node_ptr&,NodePtrCompare) Chris@16: template Chris@16: static node_ptr insert_equal_upper_bound Chris@16: (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp Chris@16: ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size) Chris@16: { Chris@16: std::size_t depth; Chris@16: bstree_algo::insert_equal_upper_bound(h, new_node, comp, &depth); Chris@16: rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size); Chris@16: return new_node; Chris@16: } Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_lower_bound(const node_ptr&,const node_ptr&,NodePtrCompare) Chris@16: template Chris@16: static node_ptr insert_equal_lower_bound Chris@16: (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp Chris@16: ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size) Chris@16: { Chris@16: std::size_t depth; Chris@16: bstree_algo::insert_equal_lower_bound(h, new_node, comp, &depth); Chris@16: rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size); Chris@16: return new_node; Chris@16: } Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal(const node_ptr&,const node_ptr&,const node_ptr&,NodePtrCompare) Chris@16: template Chris@16: static node_ptr insert_equal Chris@16: (const node_ptr & header, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp Chris@16: ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size) Chris@16: { Chris@16: std::size_t depth; Chris@16: bstree_algo::insert_equal(header, hint, new_node, comp, &depth); Chris@16: rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size); Chris@16: return new_node; Chris@16: } Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::insert_before(const node_ptr&,const node_ptr&,const node_ptr&) Chris@16: template Chris@16: static node_ptr insert_before Chris@16: (const node_ptr & header, const node_ptr & pos, const node_ptr & new_node Chris@16: ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size) Chris@16: { Chris@16: std::size_t depth; Chris@16: bstree_algo::insert_before(header, pos, new_node, &depth); Chris@16: rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size); Chris@16: return new_node; Chris@16: } Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::push_back(const node_ptr&,const node_ptr&) Chris@16: template Chris@16: static void push_back(const node_ptr & header, const node_ptr & new_node Chris@16: ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size) Chris@16: { Chris@16: std::size_t depth; Chris@16: bstree_algo::push_back(header, new_node, &depth); Chris@16: rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size); Chris@16: } Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::push_front(const node_ptr&,const node_ptr&) Chris@16: template Chris@16: static void push_front(const node_ptr & header, const node_ptr & new_node Chris@16: ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size) Chris@16: { Chris@16: std::size_t depth; Chris@16: bstree_algo::push_front(header, new_node, &depth); Chris@16: rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size); Chris@16: } Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const const_node_ptr&,const KeyType&,KeyNodePtrCompare,insert_commit_data&) Chris@16: template Chris@16: static std::pair insert_unique_check Chris@16: (const const_node_ptr & header, const KeyType &key Chris@16: ,KeyNodePtrCompare comp, insert_commit_data &commit_data) Chris@16: { Chris@16: std::size_t depth; Chris@16: std::pair ret = Chris@16: bstree_algo::insert_unique_check(header, key, comp, commit_data, &depth); Chris@16: commit_data.depth = depth; Chris@16: return ret; Chris@16: } Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const const_node_ptr&,const node_ptr&,const KeyType&,KeyNodePtrCompare,insert_commit_data&) Chris@16: template Chris@16: static std::pair insert_unique_check Chris@16: (const const_node_ptr & header, const node_ptr &hint, const KeyType &key Chris@16: ,KeyNodePtrCompare comp, insert_commit_data &commit_data) Chris@16: { Chris@16: std::size_t depth; Chris@16: std::pair ret = Chris@16: bstree_algo::insert_unique_check Chris@16: (header, hint, key, comp, commit_data, &depth); Chris@16: commit_data.depth = depth; Chris@16: return ret; Chris@16: } Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_commit(const node_ptr&,const node_ptr&,const insert_commit_data&) Chris@16: template Chris@16: static void insert_unique_commit Chris@16: (const node_ptr & header, const node_ptr & new_value, const insert_commit_data &commit_data Chris@16: ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size) Chris@16: { Chris@16: bstree_algo::insert_unique_commit(header, new_value, commit_data); Chris@16: rebalance_after_insertion(new_value, commit_data.depth, tree_size+1, h_alpha, max_tree_size); Chris@16: } Chris@16: Chris@16: #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::is_header Chris@16: static bool is_header(const const_node_ptr & p); Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::is_header Chris@16: static void rebalance(const node_ptr & header); Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::rebalance_subtree Chris@16: static node_ptr rebalance_subtree(const node_ptr & old_root) Chris@16: #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@16: Chris@16: /// @cond Chris@16: private: Chris@16: Chris@16: template Chris@16: static void rebalance_after_insertion Chris@16: (const node_ptr &x, std::size_t depth Chris@16: , std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size) Chris@16: { Chris@16: if(tree_size > max_tree_size) Chris@16: max_tree_size = tree_size; Chris@16: Chris@101: if(tree_size > 2 && //Nothing to do with only the root Chris@16: //Check if the root node is unbalanced Chris@16: //Scapegoat paper depth counts root depth as zero and "depth" counts root as 1, Chris@16: //but since "depth" is the depth of the ancestor of x, i == depth Chris@16: depth > h_alpha(tree_size)){ Chris@101: Chris@16: //Find the first non height-balanced node Chris@16: //as described in the section 4.2 of the paper. Chris@16: //This method is the alternative method described Chris@16: //in the paper. Authors claim that this method Chris@16: //may tend to yield more balanced trees on the average Chris@16: //than the weight balanced method. Chris@16: node_ptr s = x; Chris@16: std::size_t size = 1; Chris@101: for(std::size_t ancestor = 1; ancestor != depth; ++ancestor){ Chris@101: const node_ptr s_parent = NodeTraits::get_parent(s); Chris@101: const node_ptr s_parent_left = NodeTraits::get_left(s_parent); Chris@101: //Obtain parent's size (previous size + parent + sibling tree) Chris@101: const node_ptr s_sibling = s_parent_left == s ? NodeTraits::get_right(s_parent) : s_parent_left; Chris@101: size += 1 + bstree_algo::subtree_size(s_sibling); Chris@101: s = s_parent; Chris@101: if(ancestor > h_alpha(size)){ //is 's' scapegoat? Chris@101: bstree_algo::rebalance_subtree(s); Chris@101: return; Chris@16: } Chris@16: } Chris@101: //The whole tree must be rebuilt Chris@101: max_tree_size = tree_size; Chris@101: bstree_algo::rebalance_subtree(NodeTraits::get_parent(s)); Chris@16: } Chris@16: } Chris@16: /// @endcond Chris@16: }; Chris@16: Chris@16: /// @cond Chris@16: Chris@16: template Chris@16: struct get_algo Chris@16: { Chris@16: typedef sgtree_algorithms type; Chris@16: }; Chris@16: Chris@101: template Chris@101: struct get_node_checker Chris@101: { Chris@101: typedef detail::bstree_node_checker type; Chris@101: }; Chris@101: Chris@16: /// @endcond Chris@16: Chris@16: } //namespace intrusive Chris@16: } //namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif //BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP