2013-03-16 11:00:13 -07:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// (C) Copyright Olaf Krzikalla 2004-2006.
|
2014-03-01 11:00:20 -08:00
|
|
|
// (C) Copyright Ion Gaztanaga 2006-2013.
|
2013-03-16 11:00:13 -07:00
|
|
|
//
|
|
|
|
// 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 internal implementation of red-black trees is based on that of SGI STL
|
|
|
|
// stl_tree.h file:
|
|
|
|
//
|
|
|
|
// Copyright (c) 1996,1997
|
|
|
|
// Silicon Graphics Computer Systems, Inc.
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, distribute and sell this software
|
|
|
|
// and its documentation for any purpose is hereby granted without fee,
|
|
|
|
// provided that the above copyright notice appear in all copies and
|
|
|
|
// that both that copyright notice and this permission notice appear
|
|
|
|
// in supporting documentation. Silicon Graphics makes no
|
|
|
|
// representations about the suitability of this software for any
|
|
|
|
// purpose. It is provided "as is" without express or implied warranty.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Copyright (c) 1994
|
|
|
|
// Hewlett-Packard Company
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, distribute and sell this software
|
|
|
|
// and its documentation for any purpose is hereby granted without fee,
|
|
|
|
// provided that the above copyright notice appear in all copies and
|
|
|
|
// that both that copyright notice and this permission notice appear
|
|
|
|
// in supporting documentation. Hewlett-Packard Company makes no
|
|
|
|
// representations about the suitability of this software for any
|
|
|
|
// purpose. It is provided "as is" without express or implied warranty.
|
|
|
|
//
|
|
|
|
// The tree destruction algorithm is based on Julienne Walker and The EC Team code:
|
|
|
|
//
|
|
|
|
// This code is in the public domain. Anyone may use it or change it in any way that
|
|
|
|
// they see fit. The author assumes no responsibility for damages incurred through
|
|
|
|
// use of the original code or any variations thereof.
|
|
|
|
//
|
|
|
|
// It is requested, but not required, that due credit is given to the original author
|
|
|
|
// and anyone who has modified the code through a header comment, such as this one.
|
|
|
|
|
|
|
|
#ifndef BOOST_INTRUSIVE_RBTREE_ALGORITHMS_HPP
|
|
|
|
#define BOOST_INTRUSIVE_RBTREE_ALGORITHMS_HPP
|
|
|
|
|
|
|
|
#include <boost/intrusive/detail/config_begin.hpp>
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <boost/intrusive/intrusive_fwd.hpp>
|
|
|
|
|
|
|
|
#include <boost/intrusive/detail/assert.hpp>
|
|
|
|
#include <boost/intrusive/detail/utilities.hpp>
|
2014-03-01 11:00:20 -08:00
|
|
|
#include <boost/intrusive/bstree_algorithms.hpp>
|
2013-03-16 11:00:13 -07:00
|
|
|
#include <boost/intrusive/pointer_traits.hpp>
|
|
|
|
|
|
|
|
namespace boost {
|
|
|
|
namespace intrusive {
|
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
|
|
|
|
|
|
|
template<class NodeTraits, class F>
|
|
|
|
struct rbtree_node_cloner
|
|
|
|
: private detail::ebo_functor_holder<F>
|
|
|
|
{
|
|
|
|
typedef typename NodeTraits::node_ptr node_ptr;
|
|
|
|
typedef detail::ebo_functor_holder<F> base_t;
|
|
|
|
|
|
|
|
rbtree_node_cloner(F f)
|
|
|
|
: base_t(f)
|
|
|
|
{}
|
|
|
|
|
|
|
|
node_ptr operator()(const node_ptr & p)
|
|
|
|
{
|
|
|
|
node_ptr n = base_t::get()(p);
|
|
|
|
NodeTraits::set_color(n, NodeTraits::get_color(p));
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class NodeTraits>
|
|
|
|
struct rbtree_erase_fixup
|
|
|
|
{
|
|
|
|
typedef typename NodeTraits::node_ptr node_ptr;
|
|
|
|
typedef typename NodeTraits::color color;
|
|
|
|
|
|
|
|
void operator()(const node_ptr & to_erase, const 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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif //#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
|
|
|
|
2013-03-16 11:00:13 -07:00
|
|
|
//! rbtree_algorithms provides basic algorithms to manipulate
|
|
|
|
//! nodes forming a red-black tree. The insertion and deletion algorithms are
|
|
|
|
//! based on those in Cormen, Leiserson, and Rivest, Introduction to Algorithms
|
|
|
|
//! (MIT Press, 1990), except that
|
|
|
|
//!
|
|
|
|
//! (1) the header node is maintained with links not only to the root
|
|
|
|
//! but also to the leftmost node of the tree, to enable constant time
|
|
|
|
//! begin(), and to the rightmost node of the tree, to enable linear time
|
|
|
|
//! performance when used with the generic set algorithms (set_union,
|
|
|
|
//! etc.);
|
|
|
|
//!
|
|
|
|
//! (2) when a node being deleted has two children its successor node is
|
|
|
|
//! relinked into its place, rather than copied, so that the only
|
|
|
|
//! pointers invalidated are those referring to the deleted node.
|
|
|
|
//!
|
|
|
|
//! rbtree_algorithms is configured with a NodeTraits class, which encapsulates the
|
|
|
|
//! information about the node to be manipulated. NodeTraits must support the
|
|
|
|
//! following interface:
|
|
|
|
//!
|
|
|
|
//! <b>Typedefs</b>:
|
|
|
|
//!
|
2014-03-01 11:00:20 -08:00
|
|
|
//! <tt>node</tt>: The type of the node that forms the binary search tree
|
2013-03-16 11:00:13 -07:00
|
|
|
//!
|
|
|
|
//! <tt>node_ptr</tt>: A pointer to a node
|
|
|
|
//!
|
|
|
|
//! <tt>const_node_ptr</tt>: A pointer to a const node
|
|
|
|
//!
|
|
|
|
//! <tt>color</tt>: The type that can store the color of a node
|
|
|
|
//!
|
|
|
|
//! <b>Static functions</b>:
|
|
|
|
//!
|
|
|
|
//! <tt>static node_ptr get_parent(const_node_ptr n);</tt>
|
|
|
|
//!
|
|
|
|
//! <tt>static void set_parent(node_ptr n, node_ptr parent);</tt>
|
|
|
|
//!
|
|
|
|
//! <tt>static node_ptr get_left(const_node_ptr n);</tt>
|
|
|
|
//!
|
|
|
|
//! <tt>static void set_left(node_ptr n, node_ptr left);</tt>
|
|
|
|
//!
|
|
|
|
//! <tt>static node_ptr get_right(const_node_ptr n);</tt>
|
|
|
|
//!
|
|
|
|
//! <tt>static void set_right(node_ptr n, node_ptr right);</tt>
|
|
|
|
//!
|
|
|
|
//! <tt>static color get_color(const_node_ptr n);</tt>
|
|
|
|
//!
|
|
|
|
//! <tt>static void set_color(node_ptr n, color c);</tt>
|
|
|
|
//!
|
|
|
|
//! <tt>static color black();</tt>
|
|
|
|
//!
|
|
|
|
//! <tt>static color red();</tt>
|
|
|
|
template<class NodeTraits>
|
|
|
|
class rbtree_algorithms
|
2014-03-01 11:00:20 -08:00
|
|
|
#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
|
|
|
: public bstree_algorithms<NodeTraits>
|
|
|
|
#endif
|
2013-03-16 11:00:13 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef NodeTraits node_traits;
|
|
|
|
typedef typename NodeTraits::node node;
|
|
|
|
typedef typename NodeTraits::node_ptr node_ptr;
|
|
|
|
typedef typename NodeTraits::const_node_ptr const_node_ptr;
|
|
|
|
typedef typename NodeTraits::color color;
|
|
|
|
|
|
|
|
/// @cond
|
|
|
|
private:
|
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
typedef bstree_algorithms<NodeTraits> bstree_algo;
|
2013-03-16 11:00:13 -07:00
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
/// @endcond
|
2013-03-16 11:00:13 -07:00
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
public:
|
2013-03-16 11:00:13 -07:00
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
//! This type is the information that will be
|
|
|
|
//! filled by insert_unique_check
|
|
|
|
typedef typename bstree_algo::insert_commit_data insert_commit_data;
|
2013-03-16 11:00:13 -07:00
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
2013-03-16 11:00:13 -07:00
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::get_header(const const_node_ptr&)
|
|
|
|
static node_ptr get_header(const const_node_ptr & n);
|
2013-03-16 11:00:13 -07:00
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::begin_node
|
|
|
|
static node_ptr begin_node(const const_node_ptr & header);
|
2013-03-16 11:00:13 -07:00
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::end_node
|
|
|
|
static node_ptr end_node(const const_node_ptr & header);
|
2013-03-16 11:00:13 -07:00
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::swap_tree
|
|
|
|
static void swap_tree(const node_ptr & header1, const node_ptr & header2);
|
|
|
|
|
|
|
|
#endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
|
|
|
|
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(const node_ptr&,const node_ptr&)
|
2013-03-16 11:00:13 -07:00
|
|
|
static void swap_nodes(const node_ptr & node1, const node_ptr & node2)
|
|
|
|
{
|
|
|
|
if(node1 == node2)
|
|
|
|
return;
|
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
node_ptr header1(bstree_algo::get_header(node1)), header2(bstree_algo::get_header(node2));
|
2013-03-16 11:00:13 -07:00
|
|
|
swap_nodes(node1, header1, node2, header2);
|
|
|
|
}
|
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(const node_ptr&,const node_ptr&,const node_ptr&,const node_ptr&)
|
2013-03-16 11:00:13 -07:00
|
|
|
static void swap_nodes(const node_ptr & node1, const node_ptr & header1, const node_ptr & node2, const node_ptr & header2)
|
|
|
|
{
|
|
|
|
if(node1 == node2) return;
|
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
bstree_algo::swap_nodes(node1, header1, node2, header2);
|
2013-03-16 11:00:13 -07:00
|
|
|
//Swap color
|
|
|
|
color c = NodeTraits::get_color(node1);
|
|
|
|
NodeTraits::set_color(node1, NodeTraits::get_color(node2));
|
|
|
|
NodeTraits::set_color(node2, c);
|
|
|
|
}
|
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(const node_ptr&,const node_ptr&)
|
2013-03-16 11:00:13 -07:00
|
|
|
static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & new_node)
|
|
|
|
{
|
|
|
|
if(node_to_be_replaced == new_node)
|
|
|
|
return;
|
2014-03-01 11:00:20 -08:00
|
|
|
replace_node(node_to_be_replaced, bstree_algo::get_header(node_to_be_replaced), new_node);
|
2013-03-16 11:00:13 -07:00
|
|
|
}
|
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(const node_ptr&,const node_ptr&,const node_ptr&)
|
2013-03-16 11:00:13 -07:00
|
|
|
static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & header, const node_ptr & new_node)
|
|
|
|
{
|
2014-03-01 11:00:20 -08:00
|
|
|
bstree_algo::replace_node(node_to_be_replaced, header, new_node);
|
2013-03-16 11:00:13 -07:00
|
|
|
NodeTraits::set_color(new_node, NodeTraits::get_color(node_to_be_replaced));
|
|
|
|
}
|
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::unlink(const node_ptr&)
|
|
|
|
static void unlink(const node_ptr& node)
|
2013-03-16 11:00:13 -07:00
|
|
|
{
|
|
|
|
node_ptr x = NodeTraits::get_parent(node);
|
|
|
|
if(x){
|
|
|
|
while(!is_header(x))
|
|
|
|
x = NodeTraits::get_parent(x);
|
|
|
|
erase(x, node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::unlink_leftmost_without_rebalance
|
|
|
|
static node_ptr unlink_leftmost_without_rebalance(const node_ptr & header);
|
|
|
|
|
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::unique(const const_node_ptr&)
|
|
|
|
static bool unique(const const_node_ptr & node);
|
|
|
|
|
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::size(const const_node_ptr&)
|
|
|
|
static std::size_t size(const const_node_ptr & header);
|
|
|
|
|
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::next_node(const node_ptr&)
|
|
|
|
static node_ptr next_node(const node_ptr & node);
|
|
|
|
|
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::prev_node(const node_ptr&)
|
|
|
|
static node_ptr prev_node(const node_ptr & node);
|
|
|
|
|
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::init(const node_ptr&)
|
|
|
|
static void init(const node_ptr & node);
|
|
|
|
#endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
|
|
|
|
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::init_header(const node_ptr&)
|
2013-03-16 11:00:13 -07:00
|
|
|
static void init_header(const node_ptr & header)
|
|
|
|
{
|
2014-03-01 11:00:20 -08:00
|
|
|
bstree_algo::init_header(header);
|
2013-03-16 11:00:13 -07:00
|
|
|
NodeTraits::set_color(header, NodeTraits::red());
|
|
|
|
}
|
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::erase(const node_ptr&,const node_ptr&)
|
2013-03-16 11:00:13 -07:00
|
|
|
static node_ptr erase(const node_ptr & header, const node_ptr & z)
|
|
|
|
{
|
2014-03-01 11:00:20 -08:00
|
|
|
typename bstree_algo::data_for_rebalance info;
|
|
|
|
bstree_algo::erase(header, z, rbtree_erase_fixup<NodeTraits>(), info);
|
2013-03-16 11:00:13 -07:00
|
|
|
|
|
|
|
//Rebalance rbtree
|
|
|
|
if(NodeTraits::get_color(z) != NodeTraits::red()){
|
2014-03-01 11:00:20 -08:00
|
|
|
rebalance_after_erasure(header, info.x, info.x_parent);
|
2013-03-16 11:00:13 -07:00
|
|
|
}
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::clone(const const_node_ptr&,const node_ptr&,Cloner,Disposer)
|
2013-03-16 11:00:13 -07:00
|
|
|
template <class Cloner, class Disposer>
|
|
|
|
static void clone
|
|
|
|
(const const_node_ptr & source_header, const node_ptr & target_header, Cloner cloner, Disposer disposer)
|
|
|
|
{
|
2014-03-01 11:00:20 -08:00
|
|
|
rbtree_node_cloner<NodeTraits, Cloner> new_cloner(cloner);
|
|
|
|
bstree_algo::clone(source_header, target_header, new_cloner, disposer);
|
2013-03-16 11:00:13 -07:00
|
|
|
}
|
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::clear_and_dispose(const node_ptr&,Disposer)
|
2013-03-16 11:00:13 -07:00
|
|
|
template<class Disposer>
|
2014-03-01 11:00:20 -08:00
|
|
|
static void clear_and_dispose(const node_ptr & header, Disposer disposer);
|
|
|
|
|
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::lower_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
|
2013-03-16 11:00:13 -07:00
|
|
|
template<class KeyType, class KeyNodePtrCompare>
|
|
|
|
static node_ptr lower_bound
|
2014-03-01 11:00:20 -08:00
|
|
|
(const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
|
|
|
|
|
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::upper_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
|
2013-03-16 11:00:13 -07:00
|
|
|
template<class KeyType, class KeyNodePtrCompare>
|
|
|
|
static node_ptr upper_bound
|
2014-03-01 11:00:20 -08:00
|
|
|
(const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
|
|
|
|
|
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::find(const const_node_ptr&, const KeyType&,KeyNodePtrCompare)
|
2013-03-16 11:00:13 -07:00
|
|
|
template<class KeyType, class KeyNodePtrCompare>
|
|
|
|
static node_ptr find
|
2014-03-01 11:00:20 -08:00
|
|
|
(const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
|
|
|
|
|
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::equal_range(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
|
2013-03-16 11:00:13 -07:00
|
|
|
template<class KeyType, class KeyNodePtrCompare>
|
|
|
|
static std::pair<node_ptr, node_ptr> equal_range
|
2014-03-01 11:00:20 -08:00
|
|
|
(const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
|
|
|
|
|
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::bounded_range(const const_node_ptr&,const KeyType&,const KeyType&,KeyNodePtrCompare,bool,bool)
|
2013-03-16 11:00:13 -07:00
|
|
|
template<class KeyType, class KeyNodePtrCompare>
|
|
|
|
static std::pair<node_ptr, node_ptr> bounded_range
|
|
|
|
(const const_node_ptr & header, const KeyType &lower_key, const KeyType &upper_key, KeyNodePtrCompare comp
|
2014-03-01 11:00:20 -08:00
|
|
|
, bool left_closed, bool right_closed);
|
|
|
|
|
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::count(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
|
|
|
|
template<class KeyType, class KeyNodePtrCompare>
|
|
|
|
static std::size_t count(const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
|
|
|
|
#endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
|
|
|
|
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_upper_bound(const node_ptr&,const node_ptr&,NodePtrCompare)
|
2013-03-16 11:00:13 -07:00
|
|
|
template<class NodePtrCompare>
|
|
|
|
static node_ptr insert_equal_upper_bound
|
|
|
|
(const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp)
|
|
|
|
{
|
2014-03-01 11:00:20 -08:00
|
|
|
bstree_algo::insert_equal_upper_bound(h, new_node, comp);
|
2013-03-16 11:00:13 -07:00
|
|
|
rebalance_after_insertion(h, new_node);
|
|
|
|
return new_node;
|
|
|
|
}
|
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_lower_bound(const node_ptr&,const node_ptr&,NodePtrCompare)
|
2013-03-16 11:00:13 -07:00
|
|
|
template<class NodePtrCompare>
|
|
|
|
static node_ptr insert_equal_lower_bound
|
|
|
|
(const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp)
|
|
|
|
{
|
2014-03-01 11:00:20 -08:00
|
|
|
bstree_algo::insert_equal_lower_bound(h, new_node, comp);
|
2013-03-16 11:00:13 -07:00
|
|
|
rebalance_after_insertion(h, new_node);
|
|
|
|
return new_node;
|
|
|
|
}
|
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal(const node_ptr&,const node_ptr&,const node_ptr&,NodePtrCompare)
|
2013-03-16 11:00:13 -07:00
|
|
|
template<class NodePtrCompare>
|
|
|
|
static node_ptr insert_equal
|
|
|
|
(const node_ptr & header, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp)
|
|
|
|
{
|
2014-03-01 11:00:20 -08:00
|
|
|
bstree_algo::insert_equal(header, hint, new_node, comp);
|
2013-03-16 11:00:13 -07:00
|
|
|
rebalance_after_insertion(header, new_node);
|
|
|
|
return new_node;
|
|
|
|
}
|
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::insert_before(const node_ptr&,const node_ptr&,const node_ptr&)
|
2013-03-16 11:00:13 -07:00
|
|
|
static node_ptr insert_before
|
|
|
|
(const node_ptr & header, const node_ptr & pos, const node_ptr & new_node)
|
|
|
|
{
|
2014-03-01 11:00:20 -08:00
|
|
|
bstree_algo::insert_before(header, pos, new_node);
|
2013-03-16 11:00:13 -07:00
|
|
|
rebalance_after_insertion(header, new_node);
|
|
|
|
return new_node;
|
|
|
|
}
|
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::push_back(const node_ptr&,const node_ptr&)
|
2013-03-16 11:00:13 -07:00
|
|
|
static void push_back(const node_ptr & header, const node_ptr & new_node)
|
|
|
|
{
|
2014-03-01 11:00:20 -08:00
|
|
|
bstree_algo::push_back(header, new_node);
|
2013-03-16 11:00:13 -07:00
|
|
|
rebalance_after_insertion(header, new_node);
|
|
|
|
}
|
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::push_front(const node_ptr&,const node_ptr&)
|
2013-03-16 11:00:13 -07:00
|
|
|
static void push_front(const node_ptr & header, const node_ptr & new_node)
|
|
|
|
{
|
2014-03-01 11:00:20 -08:00
|
|
|
bstree_algo::push_front(header, new_node);
|
2013-03-16 11:00:13 -07:00
|
|
|
rebalance_after_insertion(header, new_node);
|
|
|
|
}
|
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const const_node_ptr&,const KeyType&,KeyNodePtrCompare,insert_commit_data&)
|
2013-03-16 11:00:13 -07:00
|
|
|
template<class KeyType, class KeyNodePtrCompare>
|
|
|
|
static std::pair<node_ptr, bool> insert_unique_check
|
|
|
|
(const const_node_ptr & header, const KeyType &key
|
2014-03-01 11:00:20 -08:00
|
|
|
,KeyNodePtrCompare comp, insert_commit_data &commit_data);
|
|
|
|
|
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const const_node_ptr&,const node_ptr&,const KeyType&,KeyNodePtrCompare,insert_commit_data&)
|
2013-03-16 11:00:13 -07:00
|
|
|
template<class KeyType, class KeyNodePtrCompare>
|
|
|
|
static std::pair<node_ptr, bool> insert_unique_check
|
|
|
|
(const const_node_ptr & header, const node_ptr &hint, const KeyType &key
|
2014-03-01 11:00:20 -08:00
|
|
|
,KeyNodePtrCompare comp, insert_commit_data &commit_data);
|
|
|
|
#endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
|
|
|
|
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_commit(const node_ptr&,const node_ptr&,const insert_commit_data&)
|
2013-03-16 11:00:13 -07:00
|
|
|
static void insert_unique_commit
|
|
|
|
(const node_ptr & header, const node_ptr & new_value, const insert_commit_data &commit_data)
|
|
|
|
{
|
2014-03-01 11:00:20 -08:00
|
|
|
bstree_algo::insert_unique_commit(header, new_value, commit_data);
|
2013-03-16 11:00:13 -07:00
|
|
|
rebalance_after_insertion(header, new_value);
|
|
|
|
}
|
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
//! @copydoc ::boost::intrusive::bstree_algorithms::is_header
|
2013-03-16 11:00:13 -07:00
|
|
|
static bool is_header(const const_node_ptr & p)
|
|
|
|
{
|
|
|
|
return NodeTraits::get_color(p) == NodeTraits::red() &&
|
2014-03-01 11:00:20 -08:00
|
|
|
bstree_algo::is_header(p);
|
2013-03-16 11:00:13 -07:00
|
|
|
}
|
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
/// @cond
|
|
|
|
private:
|
|
|
|
|
2013-03-16 11:00:13 -07:00
|
|
|
static void rebalance_after_erasure(const node_ptr & header, node_ptr x, node_ptr x_parent)
|
|
|
|
{
|
|
|
|
while(x != NodeTraits::get_parent(header) && (!x || NodeTraits::get_color(x) == NodeTraits::black())){
|
|
|
|
if(x == NodeTraits::get_left(x_parent)){
|
|
|
|
node_ptr w = NodeTraits::get_right(x_parent);
|
|
|
|
BOOST_ASSERT(w);
|
|
|
|
if(NodeTraits::get_color(w) == NodeTraits::red()){
|
|
|
|
NodeTraits::set_color(w, NodeTraits::black());
|
|
|
|
NodeTraits::set_color(x_parent, NodeTraits::red());
|
2014-03-01 11:00:20 -08:00
|
|
|
bstree_algo::rotate_left(x_parent, header);
|
2013-03-16 11:00:13 -07:00
|
|
|
w = NodeTraits::get_right(x_parent);
|
|
|
|
}
|
|
|
|
if((!NodeTraits::get_left(w) || NodeTraits::get_color(NodeTraits::get_left(w)) == NodeTraits::black()) &&
|
|
|
|
(!NodeTraits::get_right(w) || 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) || NodeTraits::get_color(NodeTraits::get_right(w)) == NodeTraits::black()){
|
|
|
|
NodeTraits::set_color(NodeTraits::get_left(w), NodeTraits::black());
|
|
|
|
NodeTraits::set_color(w, NodeTraits::red());
|
2014-03-01 11:00:20 -08:00
|
|
|
bstree_algo::rotate_right(w, header);
|
2013-03-16 11:00:13 -07:00
|
|
|
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());
|
2014-03-01 11:00:20 -08:00
|
|
|
bstree_algo::rotate_left(x_parent, header);
|
2013-03-16 11:00:13 -07:00
|
|
|
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());
|
2014-03-01 11:00:20 -08:00
|
|
|
bstree_algo::rotate_right(x_parent, header);
|
2013-03-16 11:00:13 -07:00
|
|
|
w = NodeTraits::get_left(x_parent);
|
|
|
|
}
|
|
|
|
if((!NodeTraits::get_right(w) || NodeTraits::get_color(NodeTraits::get_right(w)) == NodeTraits::black()) &&
|
|
|
|
(!NodeTraits::get_left(w) || 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) || NodeTraits::get_color(NodeTraits::get_left(w)) == NodeTraits::black()){
|
|
|
|
NodeTraits::set_color(NodeTraits::get_right(w), NodeTraits::black());
|
|
|
|
NodeTraits::set_color(w, NodeTraits::red());
|
2014-03-01 11:00:20 -08:00
|
|
|
bstree_algo::rotate_left(w, header);
|
2013-03-16 11:00:13 -07:00
|
|
|
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());
|
2014-03-01 11:00:20 -08:00
|
|
|
bstree_algo::rotate_right(x_parent, header);
|
2013-03-16 11:00:13 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(x)
|
|
|
|
NodeTraits::set_color(x, NodeTraits::black());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rebalance_after_insertion(const 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));
|
2014-03-01 11:00:20 -08:00
|
|
|
if(bstree_algo::is_left_child(p_parent)){
|
2013-03-16 11:00:13 -07:00
|
|
|
node_ptr x = NodeTraits::get_right(p_parent_parent);
|
|
|
|
if(x && NodeTraits::get_color(x) == NodeTraits::red()){
|
|
|
|
NodeTraits::set_color(p_parent, NodeTraits::black());
|
|
|
|
NodeTraits::set_color(p_parent_parent, NodeTraits::red());
|
|
|
|
NodeTraits::set_color(x, NodeTraits::black());
|
|
|
|
p = p_parent_parent;
|
|
|
|
}
|
|
|
|
else {
|
2014-03-01 11:00:20 -08:00
|
|
|
if(!bstree_algo::is_left_child(p)){
|
2013-03-16 11:00:13 -07:00
|
|
|
p = p_parent;
|
2014-03-01 11:00:20 -08:00
|
|
|
bstree_algo::rotate_left(p, header);
|
2013-03-16 11:00:13 -07:00
|
|
|
}
|
|
|
|
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());
|
2014-03-01 11:00:20 -08:00
|
|
|
bstree_algo::rotate_right(new_p_parent_parent, header);
|
2013-03-16 11:00:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
node_ptr x = NodeTraits::get_left(p_parent_parent);
|
|
|
|
if(x && NodeTraits::get_color(x) == NodeTraits::red()){
|
|
|
|
NodeTraits::set_color(p_parent, NodeTraits::black());
|
|
|
|
NodeTraits::set_color(p_parent_parent, NodeTraits::red());
|
|
|
|
NodeTraits::set_color(x, NodeTraits::black());
|
|
|
|
p = p_parent_parent;
|
|
|
|
}
|
|
|
|
else{
|
2014-03-01 11:00:20 -08:00
|
|
|
if(bstree_algo::is_left_child(p)){
|
2013-03-16 11:00:13 -07:00
|
|
|
p = p_parent;
|
2014-03-01 11:00:20 -08:00
|
|
|
bstree_algo::rotate_right(p, header);
|
2013-03-16 11:00:13 -07:00
|
|
|
}
|
|
|
|
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());
|
2014-03-01 11:00:20 -08:00
|
|
|
bstree_algo::rotate_left(new_p_parent_parent, header);
|
2013-03-16 11:00:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NodeTraits::set_color(NodeTraits::get_parent(header), NodeTraits::black());
|
|
|
|
}
|
|
|
|
/// @endcond
|
|
|
|
};
|
|
|
|
|
2014-03-01 11:00:20 -08:00
|
|
|
/// @cond
|
|
|
|
|
|
|
|
template<class NodeTraits>
|
|
|
|
struct get_algo<RbTreeAlgorithms, NodeTraits>
|
|
|
|
{
|
|
|
|
typedef rbtree_algorithms<NodeTraits> type;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// @endcond
|
|
|
|
|
2013-03-16 11:00:13 -07:00
|
|
|
} //namespace intrusive
|
|
|
|
} //namespace boost
|
|
|
|
|
|
|
|
#include <boost/intrusive/detail/config_end.hpp>
|
|
|
|
|
|
|
|
#endif //BOOST_INTRUSIVE_RBTREE_ALGORITHMS_HPP
|