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: // The implementation of splay trees is based on the article and code published Chris@16: // in C++ Users Journal "Implementing Splay Trees in C++" (September 1, 2005). Chris@16: // Chris@16: // The splay code has been modified and (supposedly) improved by Ion Gaztanaga. Chris@16: // Chris@16: // Here is the copyright notice of the original file containing the splay code: Chris@16: // Chris@16: // splay_tree.h -- implementation of a STL compatible splay tree. Chris@16: // Chris@16: // Copyright (c) 2004 Ralf Mattethat Chris@16: // Chris@16: // Permission to copy, use, modify, sell and distribute this software Chris@16: // is granted provided this copyright notice appears in all copies. Chris@16: // This software is provided "as is" without express or implied Chris@16: // warranty, and with no claim as to its suitability for any purpose. Chris@16: // Chris@16: ///////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: #ifndef BOOST_INTRUSIVE_SPLAYTREE_ALGORITHMS_HPP Chris@16: #define BOOST_INTRUSIVE_SPLAYTREE_ALGORITHMS_HPP Chris@16: Chris@16: #include Chris@101: #include Chris@16: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: Chris@16: #include Chris@101: Chris@101: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@101: # pragma once Chris@101: #endif Chris@16: Chris@16: namespace boost { Chris@16: namespace intrusive { Chris@16: Chris@16: /// @cond Chris@16: namespace detail { Chris@16: Chris@16: template Chris@101: struct splaydown_assemble_and_fix_header Chris@16: { Chris@16: typedef typename NodeTraits::node_ptr node_ptr; Chris@101: Chris@101: splaydown_assemble_and_fix_header(const node_ptr & t, const node_ptr & header, const node_ptr &leftmost, const node_ptr &rightmost) Chris@101: : t_(t) Chris@101: , null_node_(header) Chris@101: , l_(null_node_) Chris@101: , r_(null_node_) Chris@101: , leftmost_(leftmost) Chris@101: , rightmost_(rightmost) Chris@16: {} Chris@16: Chris@101: ~splaydown_assemble_and_fix_header() Chris@101: { Chris@101: this->assemble(); Chris@16: Chris@101: //Now recover the original header except for the Chris@101: //splayed root node. Chris@101: //"t_" is the current root and "null_node_" is the header node Chris@101: NodeTraits::set_parent(null_node_, t_); Chris@101: NodeTraits::set_parent(t_, null_node_); Chris@101: //Recover leftmost/rightmost pointers Chris@101: NodeTraits::set_left (null_node_, leftmost_); Chris@101: NodeTraits::set_right(null_node_, rightmost_); Chris@101: } Chris@101: Chris@101: private: Chris@101: Chris@101: void assemble() Chris@16: { Chris@101: //procedure assemble; Chris@101: // left(r), right(l) := right(t), left(t); Chris@101: // left(t), right(t) := right(null), left(null); Chris@101: //end assemble; Chris@101: { // left(r), right(l) := right(t), left(t); Chris@101: Chris@101: node_ptr const old_t_left = NodeTraits::get_left(t_); Chris@101: node_ptr const old_t_right = NodeTraits::get_right(t_); Chris@101: NodeTraits::set_right(l_, old_t_left); Chris@101: NodeTraits::set_left (r_, old_t_right); Chris@101: if(old_t_left){ Chris@101: NodeTraits::set_parent(old_t_left, l_); Chris@101: } Chris@101: if(old_t_right){ Chris@101: NodeTraits::set_parent(old_t_right, r_); Chris@101: } Chris@101: } Chris@101: { // left(t), right(t) := right(null), left(null); Chris@101: node_ptr const null_right = NodeTraits::get_right(null_node_); Chris@101: node_ptr const null_left = NodeTraits::get_left(null_node_); Chris@101: NodeTraits::set_left (t_, null_right); Chris@101: NodeTraits::set_right(t_, null_left); Chris@101: if(null_right){ Chris@101: NodeTraits::set_parent(null_right, t_); Chris@101: } Chris@101: if(null_left){ Chris@101: NodeTraits::set_parent(null_left, t_); Chris@101: } Chris@16: } Chris@16: } Chris@101: Chris@101: public: Chris@101: node_ptr t_, null_node_, l_, r_, leftmost_, rightmost_; Chris@16: }; Chris@16: Chris@16: } //namespace detail { Chris@16: /// @endcond Chris@16: Chris@16: //! A splay tree is an implementation of a binary search tree. The tree is Chris@16: //! self balancing using the splay algorithm as described in Chris@16: //! Chris@16: //! "Self-Adjusting Binary Search Trees Chris@16: //! by Daniel Dominic Sleator and Robert Endre Tarjan Chris@16: //! AT&T Bell Laboratories, Murray Hill, NJ Chris@16: //! Journal of the ACM, Vol 32, no 3, July 1985, pp 652-686 Chris@16: //! Chris@16: //! splaytree_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 splaytree_algorithms Chris@16: #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@16: : public bstree_algorithms Chris@16: #endif Chris@16: { Chris@16: /// @cond Chris@16: private: Chris@16: typedef bstree_algorithms bstree_algo; Chris@16: /// @endcond 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: //! This type is the information that will be Chris@16: //! filled by insert_unique_check Chris@16: typedef typename bstree_algo::insert_commit_data insert_commit_data; Chris@16: Chris@16: public: 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: //! @copydoc ::boost::intrusive::bstree_algorithms::unlink(const node_ptr&) 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@101: 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@101: //! Additional notes: the previous node of z is splayed to speed up range deletions. Chris@101: static void erase(const node_ptr & header, const node_ptr & z) Chris@16: { Chris@16: //posibility 1 Chris@101: if(NodeTraits::get_left(z)){ Chris@16: splay_up(bstree_algo::prev_node(z), header); Chris@16: } Chris@101: Chris@16: //possibility 2 Chris@101: //if(NodeTraits::get_left(z)){ Chris@101: // node_ptr l = NodeTraits::get_left(z); Chris@101: // splay_up(l, header); Chris@101: //} Chris@101: Chris@101: //if(NodeTraits::get_left(z)){ Chris@101: // node_ptr l = bstree_algo::prev_node(z); Chris@101: // splay_up_impl(l, z); Chris@101: //} Chris@101: Chris@16: //possibility 4 Chris@101: //splay_up(z, header); Chris@16: Chris@16: bstree_algo::erase(header, 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: #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::count(const const_node_ptr&,const KeyType&,KeyNodePtrCompare) Chris@101: //! Additional notes: an element with key `key` is splayed. Chris@16: template Chris@16: static std::size_t count Chris@16: (const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) Chris@16: { Chris@16: std::pair ret = equal_range(header, key, comp); Chris@16: std::size_t n = 0; Chris@16: while(ret.first != ret.second){ Chris@16: ++n; Chris@16: ret.first = next_node(ret.first); Chris@16: } Chris@16: return n; Chris@16: } Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::count(const const_node_ptr&,const KeyType&,KeyNodePtrCompare) Chris@16: //! Additional note: no splaying is performed Chris@16: template Chris@16: static std::size_t count Chris@16: (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) Chris@16: { return bstree_algo::count(header, key, comp); } Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::lower_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare) Chris@101: //! Additional notes: the first node of the range is splayed. Chris@16: template Chris@16: static node_ptr lower_bound Chris@101: (const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) Chris@16: { Chris@101: splay_down(detail::uncast(header), key, comp); Chris@16: node_ptr y = bstree_algo::lower_bound(header, key, comp); Chris@101: //splay_up(y, detail::uncast(header)); Chris@16: return y; Chris@16: } Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::lower_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare) Chris@16: //! Additional note: no splaying is performed Chris@16: template Chris@16: static node_ptr lower_bound Chris@16: (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) Chris@16: { return bstree_algo::lower_bound(header, key, comp); } Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::upper_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare) Chris@101: //! Additional notes: the first node of the range is splayed. Chris@16: template Chris@16: static node_ptr upper_bound Chris@101: (const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) Chris@16: { Chris@101: splay_down(detail::uncast(header), key, comp); Chris@16: node_ptr y = bstree_algo::upper_bound(header, key, comp); Chris@101: //splay_up(y, detail::uncast(header)); Chris@16: return y; Chris@16: } Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::upper_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare) Chris@16: //! Additional note: no splaying is performed Chris@16: template Chris@16: static node_ptr upper_bound Chris@16: (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) Chris@16: { return bstree_algo::upper_bound(header, key, comp); } Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::find(const const_node_ptr&, const KeyType&,KeyNodePtrCompare) Chris@101: //! Additional notes: the found node of the lower bound is splayed. Chris@16: template Chris@16: static node_ptr find Chris@101: (const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) Chris@16: { Chris@101: splay_down(detail::uncast(header), key, comp); Chris@101: return bstree_algo::find(header, key, comp); Chris@16: } Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::find(const const_node_ptr&, const KeyType&,KeyNodePtrCompare) Chris@16: //! Additional note: no splaying is performed Chris@16: template Chris@16: static node_ptr find Chris@16: (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) Chris@16: { return bstree_algo::find(header, key, comp); } Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::equal_range(const const_node_ptr&,const KeyType&,KeyNodePtrCompare) Chris@101: //! Additional notes: the first node of the range is splayed. Chris@16: template Chris@16: static std::pair equal_range Chris@101: (const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) Chris@16: { Chris@101: splay_down(detail::uncast(header), key, comp); Chris@16: std::pair ret = bstree_algo::equal_range(header, key, comp); Chris@101: //splay_up(ret.first, detail::uncast(header)); Chris@16: return ret; Chris@16: } Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::equal_range(const const_node_ptr&,const KeyType&,KeyNodePtrCompare) Chris@16: //! Additional note: no splaying is performed Chris@16: template Chris@16: static std::pair equal_range Chris@16: (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) Chris@16: { return bstree_algo::equal_range(header, key, comp); } Chris@16: Chris@101: //! @copydoc ::boost::intrusive::bstree_algorithms::lower_bound_range(const const_node_ptr&,const KeyType&,KeyNodePtrCompare) Chris@101: //! Additional notes: the first node of the range is splayed. Chris@101: template Chris@101: static std::pair lower_bound_range Chris@101: (const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) Chris@101: { Chris@101: splay_down(detail::uncast(header), key, comp); Chris@101: std::pair ret = bstree_algo::lower_bound_range(header, key, comp); Chris@101: //splay_up(ret.first, detail::uncast(header)); Chris@101: return ret; Chris@101: } Chris@101: Chris@101: //! @copydoc ::boost::intrusive::bstree_algorithms::lower_bound_range(const const_node_ptr&,const KeyType&,KeyNodePtrCompare) Chris@101: //! Additional note: no splaying is performed Chris@101: template Chris@101: static std::pair lower_bound_range Chris@101: (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) Chris@101: { return bstree_algo::lower_bound_range(header, key, comp); } Chris@101: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::bounded_range(const const_node_ptr&,const KeyType&,const KeyType&,KeyNodePtrCompare,bool,bool) Chris@101: //! Additional notes: the first node of the range is splayed. Chris@16: template Chris@16: static std::pair bounded_range Chris@16: (const node_ptr & header, const KeyType &lower_key, const KeyType &upper_key, KeyNodePtrCompare comp Chris@101: , bool left_closed, bool right_closed) Chris@16: { Chris@101: splay_down(detail::uncast(header), lower_key, comp); Chris@16: std::pair ret = Chris@16: bstree_algo::bounded_range(header, lower_key, upper_key, comp, left_closed, right_closed); Chris@101: //splay_up(ret.first, detail::uncast(header)); Chris@16: return ret; Chris@16: } Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::bounded_range(const const_node_ptr&,const KeyType&,const KeyType&,KeyNodePtrCompare,bool,bool) Chris@16: //! Additional note: no splaying is performed 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: { return bstree_algo::bounded_range(header, lower_key, upper_key, comp, left_closed, right_closed); } Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_upper_bound(const node_ptr&,const node_ptr&,NodePtrCompare) Chris@16: //! Additional note: the inserted node is splayed Chris@16: template Chris@16: static node_ptr insert_equal_upper_bound Chris@16: (const node_ptr & header, const node_ptr & new_node, NodePtrCompare comp) Chris@16: { Chris@16: splay_down(header, new_node, comp); Chris@16: return bstree_algo::insert_equal_upper_bound(header, new_node, comp); Chris@16: } Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_lower_bound(const node_ptr&,const node_ptr&,NodePtrCompare) Chris@16: //! Additional note: the inserted node is splayed Chris@16: template Chris@16: static node_ptr insert_equal_lower_bound Chris@16: (const node_ptr & header, const node_ptr & new_node, NodePtrCompare comp) Chris@16: { Chris@16: splay_down(header, new_node, comp); Chris@16: return bstree_algo::insert_equal_lower_bound(header, new_node, comp); 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: //! Additional note: the inserted node is splayed 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: { Chris@16: splay_down(header, new_node, comp); Chris@16: return bstree_algo::insert_equal(header, hint, new_node, comp); Chris@16: } Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::insert_before(const node_ptr&,const node_ptr&,const node_ptr&) Chris@16: //! Additional note: the inserted node is splayed Chris@16: static node_ptr insert_before Chris@16: (const node_ptr & header, const node_ptr & pos, const node_ptr & new_node) Chris@16: { Chris@16: bstree_algo::insert_before(header, pos, new_node); Chris@16: splay_up(new_node, header); 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: //! Additional note: the inserted node is splayed Chris@16: static void push_back(const node_ptr & header, const node_ptr & new_node) Chris@16: { Chris@16: bstree_algo::push_back(header, new_node); Chris@16: splay_up(new_node, header); Chris@16: } Chris@16: Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::push_front(const node_ptr&,const node_ptr&) Chris@16: //! Additional note: the inserted node is splayed Chris@16: static void push_front(const node_ptr & header, const node_ptr & new_node) Chris@16: { Chris@16: bstree_algo::push_front(header, new_node); Chris@16: splay_up(new_node, header); 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: //! Additional note: nodes with the given key are splayed Chris@16: template Chris@16: static std::pair insert_unique_check Chris@16: (const node_ptr & header, const KeyType &key Chris@16: ,KeyNodePtrCompare comp, insert_commit_data &commit_data) Chris@16: { Chris@16: splay_down(header, key, comp); Chris@16: return bstree_algo::insert_unique_check(header, key, comp, commit_data); 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: //! Additional note: nodes with the given key are splayed Chris@16: template Chris@16: static std::pair insert_unique_check Chris@16: (const node_ptr & header, const node_ptr &hint, const KeyType &key Chris@16: ,KeyNodePtrCompare comp, insert_commit_data &commit_data) Chris@16: { Chris@16: splay_down(header, key, comp); Chris@16: return bstree_algo::insert_unique_check(header, hint, key, comp, commit_data); Chris@16: } Chris@16: Chris@16: #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@16: //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_commit(const node_ptr&,const node_ptr&,const insert_commit_data&) 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: 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::rebalance 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: Chris@16: #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@16: Chris@16: // bottom-up splay, use data_ as parent for n | complexity : logarithmic | exception : nothrow Chris@16: static void splay_up(const node_ptr & node, const node_ptr & header) Chris@101: { priv_splay_up(node, header); } Chris@101: Chris@101: // top-down splay | complexity : logarithmic | exception : strong, note A Chris@101: template Chris@101: static node_ptr splay_down(const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp, bool *pfound = 0) Chris@101: { return priv_splay_down(header, key, comp, pfound); } Chris@101: Chris@101: private: Chris@101: Chris@101: /// @cond Chris@101: Chris@101: // bottom-up splay, use data_ as parent for n | complexity : logarithmic | exception : nothrow Chris@101: template Chris@101: static void priv_splay_up(const node_ptr & node, const node_ptr & header) Chris@16: { Chris@16: // If (node == header) do a splay for the right most node instead Chris@16: // this is to boost performance of equal_range/count on equivalent containers in the case Chris@16: // where there are many equal elements at the end Chris@16: node_ptr n((node == header) ? NodeTraits::get_right(header) : node); Chris@16: node_ptr t(header); Chris@16: Chris@16: if( n == t ) return; Chris@16: Chris@16: for( ;; ){ Chris@16: node_ptr p(NodeTraits::get_parent(n)); Chris@16: node_ptr g(NodeTraits::get_parent(p)); Chris@16: Chris@16: if( p == t ) break; Chris@16: Chris@16: if( g == t ){ Chris@16: // zig Chris@16: rotate(n); Chris@16: } Chris@16: else if ((NodeTraits::get_left(p) == n && NodeTraits::get_left(g) == p) || Chris@16: (NodeTraits::get_right(p) == n && NodeTraits::get_right(g) == p) ){ Chris@16: // zig-zig Chris@16: rotate(p); Chris@16: rotate(n); Chris@16: } Chris@101: else { Chris@16: // zig-zag Chris@16: rotate(n); Chris@101: if(!SimpleSplay){ Chris@101: rotate(n); Chris@101: } Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@101: template Chris@101: static node_ptr priv_splay_down(const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp, bool *pfound = 0) Chris@16: { Chris@16: //Most splay tree implementations use a dummy/null node to implement. Chris@16: //this function. This has some problems for a generic library like Intrusive: Chris@16: // Chris@16: // * The node might not have a default constructor. Chris@16: // * The default constructor could throw. Chris@16: // Chris@16: //We already have a header node. Leftmost and rightmost nodes of the tree Chris@16: //are not changed when splaying (because the invariants of the tree don't Chris@16: //change) We can back up them, use the header as the null node and Chris@16: //reassign old values after the function has been completed. Chris@101: node_ptr const old_root = NodeTraits::get_parent(header); Chris@101: node_ptr const leftmost = NodeTraits::get_left(header); Chris@101: node_ptr const rightmost = NodeTraits::get_right(header); Chris@101: if(leftmost == rightmost){ //Empty or unique node Chris@101: if(pfound){ Chris@101: *pfound = old_root && !comp(key, old_root) && !comp(old_root, key); Chris@101: } Chris@101: return old_root ? old_root : header; Chris@101: } Chris@101: else{ Chris@101: //Initialize "null node" (the header in our case) Chris@101: NodeTraits::set_left (header, node_ptr()); Chris@101: NodeTraits::set_right(header, node_ptr()); Chris@101: //Class that will backup leftmost/rightmost from header, commit the assemble(), Chris@101: //and will restore leftmost/rightmost to header even if "comp" throws Chris@101: detail::splaydown_assemble_and_fix_header commit(old_root, header, leftmost, rightmost); Chris@101: bool found = false; Chris@16: Chris@16: for( ;; ){ Chris@101: if(comp(key, commit.t_)){ Chris@101: node_ptr const t_left = NodeTraits::get_left(commit.t_); Chris@101: if(!t_left) Chris@16: break; Chris@101: if(comp(key, t_left)){ Chris@101: bstree_algo::rotate_right_no_parent_fix(commit.t_, t_left); Chris@101: commit.t_ = t_left; Chris@101: if( !NodeTraits::get_left(commit.t_) ) Chris@16: break; Chris@101: link_right(commit.t_, commit.r_); Chris@16: } Chris@16: else{ Chris@101: link_right(commit.t_, commit.r_); Chris@101: if(!SimpleSplay && comp(t_left, key)){ Chris@101: if( !NodeTraits::get_right(commit.t_) ) Chris@101: break; Chris@101: link_left(commit.t_, commit.l_); Chris@101: } Chris@16: } Chris@16: } Chris@101: else if(comp(commit.t_, key)){ Chris@101: node_ptr const t_right = NodeTraits::get_right(commit.t_); Chris@101: if(!t_right) Chris@16: break; Chris@16: Chris@101: if(comp(t_right, key)){ Chris@101: bstree_algo::rotate_left_no_parent_fix(commit.t_, t_right); Chris@101: commit.t_ = t_right; Chris@101: if( !NodeTraits::get_right(commit.t_) ) Chris@16: break; Chris@101: link_left(commit.t_, commit.l_); Chris@16: } Chris@16: else{ Chris@101: link_left(commit.t_, commit.l_); Chris@101: if(!SimpleSplay && comp(key, t_right)){ Chris@101: if( !NodeTraits::get_left(commit.t_) ) Chris@101: break; Chris@101: link_right(commit.t_, commit.r_); Chris@101: } Chris@16: } Chris@16: } Chris@16: else{ Chris@101: found = true; Chris@16: break; Chris@16: } Chris@16: } Chris@16: Chris@101: //commit.~splaydown_assemble_and_fix_header() will first Chris@101: //"assemble()" + link the new root & recover header's leftmost & rightmost Chris@101: if(pfound){ Chris@101: *pfound = found; Chris@101: } Chris@101: return commit.t_; Chris@16: } Chris@16: } Chris@16: Chris@16: // break link to left child node and attach it to left tree pointed to by l | complexity : constant | exception : nothrow Chris@16: static void link_left(node_ptr & t, node_ptr & l) Chris@16: { Chris@101: //procedure link_left; Chris@101: // t, l, right(l) := right(t), t, t Chris@101: //end link_left Chris@16: NodeTraits::set_right(l, t); Chris@16: NodeTraits::set_parent(t, l); Chris@16: l = t; Chris@16: t = NodeTraits::get_right(t); Chris@16: } Chris@16: Chris@16: // break link to right child node and attach it to right tree pointed to by r | complexity : constant | exception : nothrow Chris@16: static void link_right(node_ptr & t, node_ptr & r) Chris@16: { Chris@101: //procedure link_right; Chris@101: // t, r, left(r) := left(t), t, t Chris@101: //end link_right; Chris@16: NodeTraits::set_left(r, t); Chris@16: NodeTraits::set_parent(t, r); Chris@16: r = t; Chris@16: t = NodeTraits::get_left(t); Chris@16: } Chris@16: Chris@16: // rotate n with its parent | complexity : constant | exception : nothrow Chris@16: static void rotate(const node_ptr & n) Chris@16: { Chris@101: //procedure rotate_left; Chris@101: // t, right(t), left(right(t)) := right(t), left(right(t)), t Chris@101: //end rotate_left; Chris@16: node_ptr p = NodeTraits::get_parent(n); Chris@16: node_ptr g = NodeTraits::get_parent(p); Chris@16: //Test if g is header before breaking tree Chris@16: //invariants that would make is_header invalid Chris@16: bool g_is_header = bstree_algo::is_header(g); Chris@16: Chris@16: if(NodeTraits::get_left(p) == n){ Chris@16: NodeTraits::set_left(p, NodeTraits::get_right(n)); Chris@101: if(NodeTraits::get_left(p)) Chris@16: NodeTraits::set_parent(NodeTraits::get_left(p), p); Chris@16: NodeTraits::set_right(n, p); Chris@16: } Chris@16: else{ // must be ( p->right == n ) Chris@16: NodeTraits::set_right(p, NodeTraits::get_left(n)); Chris@101: if(NodeTraits::get_right(p)) Chris@16: NodeTraits::set_parent(NodeTraits::get_right(p), p); Chris@16: NodeTraits::set_left(n, p); Chris@16: } Chris@16: Chris@16: NodeTraits::set_parent(p, n); Chris@16: NodeTraits::set_parent(n, g); Chris@16: Chris@16: if(g_is_header){ Chris@16: if(NodeTraits::get_parent(g) == p) Chris@16: NodeTraits::set_parent(g, n); Chris@16: else{//must be ( g->right == p ) Chris@16: BOOST_INTRUSIVE_INVARIANT_ASSERT(false); Chris@16: NodeTraits::set_right(g, n); Chris@16: } Chris@16: } Chris@16: else{ Chris@16: if(NodeTraits::get_left(g) == p) Chris@16: NodeTraits::set_left(g, n); Chris@16: else //must be ( g->right == p ) Chris@16: NodeTraits::set_right(g, n); Chris@16: } 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 splaytree_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_SPLAYTREE_ALGORITHMS_HPP