Chris@16: ///////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // (C) Copyright Ion Gaztanaga 2007-2013 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@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include 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@16: struct splaydown_rollback Chris@16: { Chris@16: typedef typename NodeTraits::node_ptr node_ptr; Chris@16: splaydown_rollback( const node_ptr *pcur_subtree, const node_ptr & header Chris@16: , const node_ptr & leftmost , const node_ptr & rightmost) Chris@16: : pcur_subtree_(pcur_subtree) , header_(header) Chris@16: , leftmost_(leftmost) , rightmost_(rightmost) Chris@16: {} Chris@16: Chris@16: void release() Chris@16: { pcur_subtree_ = 0; } Chris@16: Chris@16: ~splaydown_rollback() Chris@16: { Chris@16: if(pcur_subtree_){ Chris@16: //Exception can only be thrown by comp, but Chris@16: //tree invariants still hold. *pcur_subtree is the current root Chris@16: //so link it to the header. Chris@16: NodeTraits::set_parent(*pcur_subtree_, header_); Chris@16: NodeTraits::set_parent(header_, *pcur_subtree_); Chris@16: //Recover leftmost/rightmost pointers Chris@16: NodeTraits::set_left (header_, leftmost_); Chris@16: NodeTraits::set_right(header_, rightmost_); Chris@16: } Chris@16: } Chris@16: const node_ptr *pcur_subtree_; Chris@16: node_ptr header_, 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@16: 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: //! Additional notes: the previous node of z is splayed. The "splay" parameter which indicated if splaying Chris@16: //! should be performed, it's deprecated and will disappear in future versions. Chris@16: static void erase(const node_ptr & header, const node_ptr & z, bool splay = true) Chris@16: { Chris@16: //posibility 1 Chris@16: if(splay && NodeTraits::get_left(z)){ Chris@16: splay_up(bstree_algo::prev_node(z), header); Chris@16: } Chris@16: /* Chris@16: //possibility 2 Chris@16: if(splay && NodeTraits::get_left(z)){ Chris@16: node_ptr l = NodeTraits::get_left(z); Chris@16: splay_up(l, header); Chris@16: }*//* Chris@16: if(splay && NodeTraits::get_left(z)){ Chris@16: node_ptr l = bstree_algo::prev_node(z); Chris@16: splay_up_impl(l, z); Chris@16: }*/ Chris@16: /* Chris@16: //possibility 4 Chris@16: if(splay){ Chris@16: splay_up(z, header); Chris@16: }*/ Chris@16: Chris@16: //if(splay) Chris@16: //splay_up(z, header); 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@16: //! Additional notes: the first node of the range 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@16: //! Additional notes: the first node of the range is splayed. The "splay" parameter which indicated if splaying Chris@16: //! should be performed, it's deprecated and will disappear in future versions. Chris@16: template Chris@16: static node_ptr lower_bound Chris@16: (const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp, bool splay = true) Chris@16: { Chris@16: //splay_down(detail::uncast(header), key, comp); Chris@16: node_ptr y = bstree_algo::lower_bound(header, key, comp); Chris@16: if(splay) 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@16: //! Additional notes: the first node of the range is splayed. The "splay" parameter which indicated if splaying Chris@16: //! should be performed, it's deprecated and will disappear in future versions. Chris@16: template Chris@16: static node_ptr upper_bound Chris@16: (const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp, bool splay = true) Chris@16: { Chris@16: //splay_down(detail::uncast(header), key, comp); Chris@16: node_ptr y = bstree_algo::upper_bound(header, key, comp); Chris@16: if(splay) 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@16: //! Additional notes: the found node of the lower bound is splayed. The "splay" parameter which indicated if splaying Chris@16: //! should be performed, it's deprecated and will disappear in future versions. Chris@16: template Chris@16: static node_ptr find Chris@16: (const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp, bool splay = true) Chris@16: { Chris@16: if(splay) splay_down(detail::uncast(header), key, comp); Chris@16: node_ptr end = detail::uncast(header); Chris@16: node_ptr y = bstree_algo::lower_bound(header, key, comp); Chris@16: node_ptr r = (y == end || comp(key, y)) ? end : y; Chris@16: return r; 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@16: //! Additional notes: the first node of the range is splayed. The "splay" parameter which indicated if splaying Chris@16: //! should be performed, it's deprecated and will disappear in future versions. Chris@16: template Chris@16: static std::pair equal_range Chris@16: (const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp, bool splay = true) Chris@16: { Chris@16: //splay_down(detail::uncast(header), key, comp); Chris@16: std::pair ret = bstree_algo::equal_range(header, key, comp); Chris@16: if(splay) 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@16: //! @copydoc ::boost::intrusive::bstree_algorithms::bounded_range(const const_node_ptr&,const KeyType&,const KeyType&,KeyNodePtrCompare,bool,bool) Chris@16: //! Additional notes: the first node of the range is splayed. The "splay" parameter which indicated if splaying Chris@16: //! should be performed, it's deprecated and will disappear in future versions. 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@16: , bool left_closed, bool right_closed, bool splay = true) Chris@16: { Chris@16: std::pair ret = Chris@16: bstree_algo::bounded_range(header, lower_key, upper_key, comp, left_closed, right_closed); Chris@16: if(splay) 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@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@16: else{ Chris@16: // zig-zag Chris@16: rotate(n); Chris@16: rotate(n); Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: // top-down splay | complexity : logarithmic | exception : strong, note A Chris@16: template Chris@16: static node_ptr splay_down(const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) Chris@16: { Chris@16: if(!NodeTraits::get_parent(header)) Chris@16: return header; 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@16: node_ptr t = NodeTraits::get_parent(header); Chris@16: //Check if tree has a single node Chris@16: if(!NodeTraits::get_left(t) && !NodeTraits::get_right(t)) Chris@16: return t; Chris@16: //Backup leftmost/rightmost Chris@16: node_ptr leftmost (NodeTraits::get_left(header)); Chris@16: node_ptr rightmost(NodeTraits::get_right(header)); Chris@16: { Chris@16: //Anti-exception rollback, recovers the original header node if an exception is thrown. Chris@16: detail::splaydown_rollback rollback(&t, header, leftmost, rightmost); Chris@16: node_ptr null_node = header; Chris@16: node_ptr l = null_node; Chris@16: node_ptr r = null_node; Chris@16: Chris@16: for( ;; ){ Chris@16: if(comp(key, t)){ Chris@16: if(NodeTraits::get_left(t) == node_ptr() ) Chris@16: break; Chris@16: if(comp(key, NodeTraits::get_left(t))){ Chris@16: t = bstree_algo::rotate_right(t); Chris@16: Chris@16: if(NodeTraits::get_left(t) == node_ptr()) Chris@16: break; Chris@16: link_right(t, r); Chris@16: } Chris@16: else if(comp(NodeTraits::get_left(t), key)){ Chris@16: link_right(t, r); Chris@16: Chris@16: if(NodeTraits::get_right(t) == node_ptr() ) Chris@16: break; Chris@16: link_left(t, l); Chris@16: } Chris@16: else{ Chris@16: link_right(t, r); Chris@16: } Chris@16: } Chris@16: else if(comp(t, key)){ Chris@16: if(NodeTraits::get_right(t) == node_ptr() ) Chris@16: break; Chris@16: Chris@16: if(comp(NodeTraits::get_right(t), key)){ Chris@16: t = bstree_algo::rotate_left( t ); Chris@16: Chris@16: if(NodeTraits::get_right(t) == node_ptr() ) Chris@16: break; Chris@16: link_left(t, l); Chris@16: } Chris@16: else if(comp(key, NodeTraits::get_right(t))){ Chris@16: link_left(t, l); Chris@16: Chris@16: if(NodeTraits::get_left(t) == node_ptr()) Chris@16: break; Chris@16: Chris@16: link_right(t, r); Chris@16: } Chris@16: else{ Chris@16: link_left(t, l); Chris@16: } Chris@16: } Chris@16: else{ Chris@16: break; Chris@16: } Chris@16: } Chris@16: Chris@16: assemble(t, l, r, null_node); Chris@16: rollback.release(); Chris@16: } Chris@16: Chris@16: //Now recover the original header except for the Chris@16: //splayed root node. Chris@16: //t is the current root Chris@16: NodeTraits::set_parent(header, t); Chris@16: NodeTraits::set_parent(t, header); Chris@16: //Recover leftmost/rightmost pointers Chris@16: NodeTraits::set_left (header, leftmost); Chris@16: NodeTraits::set_right(header, rightmost); Chris@16: return t; Chris@16: } Chris@16: Chris@16: private: Chris@16: Chris@16: /// @cond Chris@16: Chris@16: // assemble the three sub-trees into new tree pointed to by t | complexity : constant | exception : nothrow Chris@16: static void assemble(const node_ptr &t, const node_ptr & l, const node_ptr & r, const const_node_ptr & null_node ) Chris@16: { Chris@16: NodeTraits::set_right(l, NodeTraits::get_left(t)); Chris@16: NodeTraits::set_left(r, NodeTraits::get_right(t)); Chris@16: Chris@16: if(NodeTraits::get_right(l) != node_ptr()){ Chris@16: NodeTraits::set_parent(NodeTraits::get_right(l), l); Chris@16: } Chris@16: Chris@16: if(NodeTraits::get_left(r) != node_ptr()){ Chris@16: NodeTraits::set_parent(NodeTraits::get_left(r), r); Chris@16: } Chris@16: Chris@16: NodeTraits::set_left (t, NodeTraits::get_right(null_node)); Chris@16: NodeTraits::set_right(t, NodeTraits::get_left(null_node)); Chris@16: Chris@16: if( NodeTraits::get_left(t) != node_ptr() ){ Chris@16: NodeTraits::set_parent(NodeTraits::get_left(t), t); Chris@16: } Chris@16: Chris@16: if( NodeTraits::get_right(t) ){ Chris@16: NodeTraits::set_parent(NodeTraits::get_right(t), 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@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@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@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@16: if(NodeTraits::get_left(p) != node_ptr()) 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@16: if(NodeTraits::get_right(p) != node_ptr()) 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@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