mirror of
https://github.com/boostorg/intrusive.git
synced 2025-08-03 22:44:43 +02:00
Added avl trees
[SVN r40597]
This commit is contained in:
2069
include/boost/intrusive/avl_set.hpp
Normal file
2069
include/boost/intrusive/avl_set.hpp
Normal file
File diff suppressed because it is too large
Load Diff
259
include/boost/intrusive/avl_set_hook.hpp
Normal file
259
include/boost/intrusive/avl_set_hook.hpp
Normal file
@@ -0,0 +1,259 @@
|
|||||||
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// (C) Copyright Ion Gaztanaga 2007
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0.
|
||||||
|
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||||
|
// http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
// See http://www.boost.org/libs/intrusive for documentation.
|
||||||
|
//
|
||||||
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#ifndef BOOST_INTRUSIVE_AVL_SET_HOOK_HPP
|
||||||
|
#define BOOST_INTRUSIVE_AVL_SET_HOOK_HPP
|
||||||
|
|
||||||
|
#include <boost/intrusive/detail/config_begin.hpp>
|
||||||
|
#include <boost/intrusive/intrusive_fwd.hpp>
|
||||||
|
#include <boost/intrusive/detail/utilities.hpp>
|
||||||
|
#include <boost/intrusive/detail/avltree_node.hpp>
|
||||||
|
#include <boost/intrusive/avltree_algorithms.hpp>
|
||||||
|
#include <boost/intrusive/options.hpp>
|
||||||
|
#include <boost/intrusive/detail/generic_hook.hpp>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace intrusive {
|
||||||
|
|
||||||
|
/// @cond
|
||||||
|
template<class VoidPointer, bool OptimizeSize = false>
|
||||||
|
struct get_avl_set_node_algo
|
||||||
|
{
|
||||||
|
typedef avltree_algorithms<avltree_node_traits<VoidPointer, OptimizeSize> > type;
|
||||||
|
};
|
||||||
|
/// @endcond
|
||||||
|
|
||||||
|
//! Helper metafunction to define a \c avl_set_base_hook that yields to the same
|
||||||
|
//! type when the same options (either explicitly or implicitly) are used.
|
||||||
|
#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
||||||
|
template<class ...Options>
|
||||||
|
#else
|
||||||
|
template<class O1 = none, class O2 = none, class O3 = none, class O4 = none>
|
||||||
|
#endif
|
||||||
|
struct make_avl_set_base_hook
|
||||||
|
{
|
||||||
|
/// @cond
|
||||||
|
typedef typename pack_options
|
||||||
|
< hook_defaults, O1, O2, O3, O4>::type packed_options;
|
||||||
|
|
||||||
|
typedef detail::generic_hook
|
||||||
|
< get_avl_set_node_algo<typename packed_options::void_pointer
|
||||||
|
,packed_options::optimize_size>
|
||||||
|
, typename packed_options::tag
|
||||||
|
, packed_options::link_mode
|
||||||
|
, detail::AvlSetBaseHook
|
||||||
|
> implementation_defined;
|
||||||
|
/// @endcond
|
||||||
|
typedef implementation_defined type;
|
||||||
|
};
|
||||||
|
|
||||||
|
//! Derive a class from avl_set_base_hook in order to store objects in
|
||||||
|
//! in an set/multiset. avl_set_base_hook holds the data necessary to maintain
|
||||||
|
//! the set/multiset and provides an appropriate value_traits class for set/multiset.
|
||||||
|
//!
|
||||||
|
//! The first integer template argument defines a tag to identify the node.
|
||||||
|
//! The same tag value can be used in different classes, but if a class is
|
||||||
|
//! derived from more than one avl_set_base_hook, then each avl_set_base_hook needs its
|
||||||
|
//! unique tag.
|
||||||
|
//!
|
||||||
|
//! The second boolean template parameter will specify the linking mode of the hook.
|
||||||
|
//!
|
||||||
|
//! The third argument is the pointer type that will be used internally in the hook
|
||||||
|
//! and the set/multiset configured from this hook.
|
||||||
|
#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
||||||
|
template<class ...Options>
|
||||||
|
#else
|
||||||
|
template<class O1, class O2, class O3, class O4>
|
||||||
|
#endif
|
||||||
|
class avl_set_base_hook
|
||||||
|
: public make_avl_set_base_hook<O1, O2, O3, O4>::type
|
||||||
|
{
|
||||||
|
#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
||||||
|
//! <b>Effects</b>: If link_mode is \c auto_unlink or \c safe_link
|
||||||
|
//! initializes the node to an unlinked state.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
avl_set_base_hook();
|
||||||
|
|
||||||
|
//! <b>Effects</b>: If link_mode is \c auto_unlink or \c safe_link
|
||||||
|
//! initializes the node to an unlinked state. The argument is ignored.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
//!
|
||||||
|
//! <b>Rationale</b>: Providing a copy-constructor
|
||||||
|
//! makes classes using the hook STL-compliant without forcing the
|
||||||
|
//! user to do some additional work. \c swap can be used to emulate
|
||||||
|
//! move-semantics.
|
||||||
|
avl_set_base_hook(const avl_set_base_hook& );
|
||||||
|
|
||||||
|
//! <b>Effects</b>: Empty function. The argument is ignored.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
//!
|
||||||
|
//! <b>Rationale</b>: Providing an assignment operator
|
||||||
|
//! makes classes using the hook STL-compliant without forcing the
|
||||||
|
//! user to do some additional work. \c swap can be used to emulate
|
||||||
|
//! move-semantics.
|
||||||
|
avl_set_base_hook& operator=(const avl_set_base_hook& );
|
||||||
|
|
||||||
|
//! <b>Effects</b>: If link_mode is \c normal_link, the destructor does
|
||||||
|
//! nothing (ie. no code is generated). If link_mode is \c safe_link and the
|
||||||
|
//! object is stored in an set an assertion is raised. If link_mode is
|
||||||
|
//! \c auto_unlink and \c is_linked() is true, the node is unlinked.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
~avl_set_base_hook();
|
||||||
|
|
||||||
|
//! <b>Effects</b>: Swapping two nodes swaps the position of the elements
|
||||||
|
//! related to those nodes in one or two containers. That is, if the node
|
||||||
|
//! this is part of the element e1, the node x is part of the element e2
|
||||||
|
//! and both elements are included in the containers s1 and s2, then after
|
||||||
|
//! the swap-operation e1 is in s2 at the position of e2 and e2 is in s1
|
||||||
|
//! at the position of e1. If one element is not in a container, then
|
||||||
|
//! after the swap-operation the other element is not in a container.
|
||||||
|
//! Iterators to e1 and e2 related to those nodes are invalidated.
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Constant
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
void swap_nodes(avl_set_base_hook &other);
|
||||||
|
|
||||||
|
//! <b>Precondition</b>: link_mode must be \c safe_link or \c auto_unlink.
|
||||||
|
//!
|
||||||
|
//! <b>Returns</b>: true, if the node belongs to a container, false
|
||||||
|
//! otherwise. This function can be used to test whether \c set::iterator_to
|
||||||
|
//! will return a valid iterator.
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Constant
|
||||||
|
bool is_linked() const;
|
||||||
|
|
||||||
|
//! <b>Effects</b>: Removes the node if it's inserted in a container.
|
||||||
|
//! This function is only allowed if link_mode is \c auto_unlink.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
void unlink();
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
//! Helper metafunction to define a \c avl_set_member_hook that yields to the same
|
||||||
|
//! type when the same options (either explicitly or implicitly) are used.
|
||||||
|
#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
||||||
|
template<class ...Options>
|
||||||
|
#else
|
||||||
|
template<class O1 = none, class O2 = none, class O3 = none, class O4 = none>
|
||||||
|
#endif
|
||||||
|
struct make_avl_set_member_hook
|
||||||
|
{
|
||||||
|
/// @cond
|
||||||
|
typedef typename pack_options
|
||||||
|
< hook_defaults, O1, O2, O3, O4>::type packed_options;
|
||||||
|
|
||||||
|
typedef detail::generic_hook
|
||||||
|
< get_avl_set_node_algo<typename packed_options::void_pointer
|
||||||
|
,packed_options::optimize_size>
|
||||||
|
, member_tag
|
||||||
|
, packed_options::link_mode
|
||||||
|
, detail::NoBaseHook
|
||||||
|
> implementation_defined;
|
||||||
|
/// @endcond
|
||||||
|
typedef implementation_defined type;
|
||||||
|
};
|
||||||
|
|
||||||
|
//! Put a public data member avl_set_member_hook in order to store objects of this class in
|
||||||
|
//! an set/multiset. avl_set_member_hook holds the data necessary for maintaining the
|
||||||
|
//! set/multiset and provides an appropriate value_traits class for set/multiset.
|
||||||
|
//!
|
||||||
|
//! The first boolean template parameter will specify the linking mode of the hook.
|
||||||
|
//!
|
||||||
|
//! The second argument is the pointer type that will be used internally in the hook
|
||||||
|
//! and the set/multiset configured from this hook.
|
||||||
|
#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
||||||
|
template<class ...Options>
|
||||||
|
#else
|
||||||
|
template<class O1, class O2, class O3, class O4>
|
||||||
|
#endif
|
||||||
|
class avl_set_member_hook
|
||||||
|
: public make_avl_set_member_hook<O1, O2, O3, O4>::type
|
||||||
|
{
|
||||||
|
#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
||||||
|
//! <b>Effects</b>: If link_mode is \c auto_unlink or \c safe_link
|
||||||
|
//! initializes the node to an unlinked state.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
avl_set_member_hook();
|
||||||
|
|
||||||
|
//! <b>Effects</b>: If link_mode is \c auto_unlink or \c safe_link
|
||||||
|
//! initializes the node to an unlinked state. The argument is ignored.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
//!
|
||||||
|
//! <b>Rationale</b>: Providing a copy-constructor
|
||||||
|
//! makes classes using the hook STL-compliant without forcing the
|
||||||
|
//! user to do some additional work. \c swap can be used to emulate
|
||||||
|
//! move-semantics.
|
||||||
|
avl_set_member_hook(const avl_set_member_hook& );
|
||||||
|
|
||||||
|
//! <b>Effects</b>: Empty function. The argument is ignored.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
//!
|
||||||
|
//! <b>Rationale</b>: Providing an assignment operator
|
||||||
|
//! makes classes using the hook STL-compliant without forcing the
|
||||||
|
//! user to do some additional work. \c swap can be used to emulate
|
||||||
|
//! move-semantics.
|
||||||
|
avl_set_member_hook& operator=(const avl_set_member_hook& );
|
||||||
|
|
||||||
|
//! <b>Effects</b>: If link_mode is \c normal_link, the destructor does
|
||||||
|
//! nothing (ie. no code is generated). If link_mode is \c safe_link and the
|
||||||
|
//! object is stored in an set an assertion is raised. If link_mode is
|
||||||
|
//! \c auto_unlink and \c is_linked() is true, the node is unlinked.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
~avl_set_member_hook();
|
||||||
|
|
||||||
|
//! <b>Effects</b>: Swapping two nodes swaps the position of the elements
|
||||||
|
//! related to those nodes in one or two containers. That is, if the node
|
||||||
|
//! this is part of the element e1, the node x is part of the element e2
|
||||||
|
//! and both elements are included in the containers s1 and s2, then after
|
||||||
|
//! the swap-operation e1 is in s2 at the position of e2 and e2 is in s1
|
||||||
|
//! at the position of e1. If one element is not in a container, then
|
||||||
|
//! after the swap-operation the other element is not in a container.
|
||||||
|
//! Iterators to e1 and e2 related to those nodes are invalidated.
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Constant
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
void swap_nodes(avl_set_member_hook &other);
|
||||||
|
|
||||||
|
//! <b>Precondition</b>: link_mode must be \c safe_link or \c auto_unlink.
|
||||||
|
//!
|
||||||
|
//! <b>Returns</b>: true, if the node belongs to a container, false
|
||||||
|
//! otherwise. This function can be used to test whether \c set::iterator_to
|
||||||
|
//! will return a valid iterator.
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Constant
|
||||||
|
bool is_linked() const;
|
||||||
|
|
||||||
|
//! <b>Effects</b>: Removes the node if it's inserted in a container.
|
||||||
|
//! This function is only allowed if link_mode is \c auto_unlink.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
void unlink();
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
} //namespace intrusive
|
||||||
|
} //namespace boost
|
||||||
|
|
||||||
|
#include <boost/intrusive/detail/config_end.hpp>
|
||||||
|
|
||||||
|
#endif //BOOST_INTRUSIVE_AVL_SET_HOOK_HPP
|
1442
include/boost/intrusive/avltree.hpp
Normal file
1442
include/boost/intrusive/avltree.hpp
Normal file
File diff suppressed because it is too large
Load Diff
975
include/boost/intrusive/avltree_algorithms.hpp
Normal file
975
include/boost/intrusive/avltree_algorithms.hpp
Normal file
@@ -0,0 +1,975 @@
|
|||||||
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// (C) Copyright Daniel K. O. 2005.
|
||||||
|
// (C) Copyright Ion Gaztanaga 2007.
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0.
|
||||||
|
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||||
|
// http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
// See http://www.boost.org/libs/intrusive for documentation.
|
||||||
|
//
|
||||||
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#ifndef BOOST_INTRUSIVE_AVLTREE_ALGORITHMS_HPP
|
||||||
|
#define BOOST_INTRUSIVE_AVLTREE_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/no_exceptions_support.hpp>
|
||||||
|
#include <boost/intrusive/detail/utilities.hpp>
|
||||||
|
#include <boost/intrusive/detail/tree_algorithms.hpp>
|
||||||
|
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace intrusive {
|
||||||
|
|
||||||
|
//! avltree_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>:
|
||||||
|
//!
|
||||||
|
//! <tt>node</tt>: The type of the node that forms the circular list
|
||||||
|
//!
|
||||||
|
//! <tt>node_ptr</tt>: A pointer to a node
|
||||||
|
//!
|
||||||
|
//! <tt>const_node_ptr</tt>: A pointer to a const node
|
||||||
|
//!
|
||||||
|
//! <tt>balance</tt>: The type of the balance factor
|
||||||
|
//!
|
||||||
|
//! <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 balance get_balance(const_node_ptr n);</tt>
|
||||||
|
//!
|
||||||
|
//! <tt>static void set_balance(node_ptr n, balance b);</tt>
|
||||||
|
//!
|
||||||
|
//! <tt>static balance negative();</tt>
|
||||||
|
//!
|
||||||
|
//! <tt>static balance zero();</tt>
|
||||||
|
//!
|
||||||
|
//! <tt>static balance positive();</tt>
|
||||||
|
template<class NodeTraits>
|
||||||
|
class avltree_algorithms
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
typedef NodeTraits node_traits;
|
||||||
|
typedef typename NodeTraits::node_ptr node_ptr;
|
||||||
|
typedef typename NodeTraits::const_node_ptr const_node_ptr;
|
||||||
|
typedef typename NodeTraits::balance balance;
|
||||||
|
|
||||||
|
/// @cond
|
||||||
|
private:
|
||||||
|
|
||||||
|
typedef typename NodeTraits::node node;
|
||||||
|
typedef detail::tree_algorithms<NodeTraits> tree_algorithms;
|
||||||
|
|
||||||
|
template<class F>
|
||||||
|
struct avltree_node_cloner
|
||||||
|
: private detail::ebo_functor_holder<F>
|
||||||
|
{
|
||||||
|
typedef detail::ebo_functor_holder<F> base_t;
|
||||||
|
|
||||||
|
avltree_node_cloner(F f)
|
||||||
|
: base_t(f)
|
||||||
|
{}
|
||||||
|
|
||||||
|
node_ptr operator()(node_ptr p)
|
||||||
|
{
|
||||||
|
node_ptr n = base_t::get()(p);
|
||||||
|
NodeTraits::set_balance(n, NodeTraits::get_balance(p));
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct avltree_erase_fixup
|
||||||
|
{
|
||||||
|
void operator()(node_ptr to_erase, node_ptr successor)
|
||||||
|
{ NodeTraits::set_balance(successor, NodeTraits::get_balance(to_erase)); }
|
||||||
|
};
|
||||||
|
|
||||||
|
static node_ptr uncast(const_node_ptr ptr)
|
||||||
|
{
|
||||||
|
return node_ptr(const_cast<node*>(::boost::intrusive::detail::get_pointer(ptr)));
|
||||||
|
}
|
||||||
|
/// @endcond
|
||||||
|
|
||||||
|
public:
|
||||||
|
static node_ptr begin_node(const_node_ptr header)
|
||||||
|
{ return tree_algorithms::begin_node(header); }
|
||||||
|
|
||||||
|
static node_ptr end_node(const_node_ptr header)
|
||||||
|
{ return tree_algorithms::end_node(header); }
|
||||||
|
|
||||||
|
//! This type is the information that will be
|
||||||
|
//! filled by insert_unique_check
|
||||||
|
typedef typename tree_algorithms::insert_commit_data insert_commit_data;
|
||||||
|
|
||||||
|
//! <b>Requires</b>: header1 and header2 must be the header nodes
|
||||||
|
//! of two trees.
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: Swaps two trees. After the function header1 will contain
|
||||||
|
//! links to the second tree and header2 will have links to the first tree.
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Constant.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
static void swap_tree(node_ptr header1, node_ptr header2)
|
||||||
|
{ return tree_algorithms::swap_tree(header1, header2); }
|
||||||
|
|
||||||
|
//! <b>Requires</b>: node1 and node2 can't be header nodes
|
||||||
|
//! of two trees.
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: Swaps two nodes. After the function node1 will be inserted
|
||||||
|
//! in the position node2 before the function. node2 will be inserted in the
|
||||||
|
//! position node1 had before the function.
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Logarithmic.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
//!
|
||||||
|
//! <b>Note</b>: This function will break container ordering invariants if
|
||||||
|
//! node1 and node2 are not equivalent according to the ordering rules.
|
||||||
|
//!
|
||||||
|
//!Experimental function
|
||||||
|
static void swap_nodes(node_ptr node1, node_ptr node2)
|
||||||
|
{
|
||||||
|
if(node1 == node2)
|
||||||
|
return;
|
||||||
|
|
||||||
|
node_ptr header1(tree_algorithms::get_header(node1)), header2(tree_algorithms::get_header(node2));
|
||||||
|
swap_nodes(node1, header1, node2, header2);
|
||||||
|
}
|
||||||
|
|
||||||
|
//! <b>Requires</b>: node1 and node2 can't be header nodes
|
||||||
|
//! of two trees with header header1 and header2.
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: Swaps two nodes. After the function node1 will be inserted
|
||||||
|
//! in the position node2 before the function. node2 will be inserted in the
|
||||||
|
//! position node1 had before the function.
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Constant.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
//!
|
||||||
|
//! <b>Note</b>: This function will break container ordering invariants if
|
||||||
|
//! node1 and node2 are not equivalent according to the ordering rules.
|
||||||
|
//!
|
||||||
|
//!Experimental function
|
||||||
|
static void swap_nodes(node_ptr node1, node_ptr header1, node_ptr node2, node_ptr header2)
|
||||||
|
{
|
||||||
|
if(node1 == node2) return;
|
||||||
|
|
||||||
|
tree_algorithms::swap_nodes(node1, header1, node2, header2);
|
||||||
|
//Swap balance
|
||||||
|
balance c = NodeTraits::get_balance(node1);
|
||||||
|
NodeTraits::set_balance(node1, NodeTraits::get_balance(node2));
|
||||||
|
NodeTraits::set_balance(node2, c);
|
||||||
|
}
|
||||||
|
|
||||||
|
//! <b>Requires</b>: node_to_be_replaced must be inserted in a tree
|
||||||
|
//! and new_node must not be inserted in a tree.
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: Replaces node_to_be_replaced in its position in the
|
||||||
|
//! tree with new_node. The tree does not need to be rebalanced
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Logarithmic.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
//!
|
||||||
|
//! <b>Note</b>: This function will break container ordering invariants if
|
||||||
|
//! new_node is not equivalent to node_to_be_replaced according to the
|
||||||
|
//! ordering rules. This function is faster than erasing and inserting
|
||||||
|
//! the node, since no rebalancing and comparison is needed.
|
||||||
|
//!
|
||||||
|
//!Experimental function
|
||||||
|
static void replace_node(node_ptr node_to_be_replaced, node_ptr new_node)
|
||||||
|
{
|
||||||
|
if(node_to_be_replaced == new_node)
|
||||||
|
return;
|
||||||
|
replace_node(node_to_be_replaced, tree_algorithms::get_header(node_to_be_replaced), new_node);
|
||||||
|
}
|
||||||
|
|
||||||
|
//! <b>Requires</b>: node_to_be_replaced must be inserted in a tree
|
||||||
|
//! with header "header" and new_node must not be inserted in a tree.
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: Replaces node_to_be_replaced in its position in the
|
||||||
|
//! tree with new_node. The tree does not need to be rebalanced
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Constant.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
//!
|
||||||
|
//! <b>Note</b>: This function will break container ordering invariants if
|
||||||
|
//! new_node is not equivalent to node_to_be_replaced according to the
|
||||||
|
//! ordering rules. This function is faster than erasing and inserting
|
||||||
|
//! the node, since no rebalancing or comparison is needed.
|
||||||
|
//!
|
||||||
|
//!Experimental function
|
||||||
|
static void replace_node(node_ptr node_to_be_replaced, node_ptr header, node_ptr new_node)
|
||||||
|
{
|
||||||
|
tree_algorithms::replace_node(node_to_be_replaced, header, new_node);
|
||||||
|
NodeTraits::set_balance(new_node, NodeTraits::get_balance(node_to_be_replaced));
|
||||||
|
}
|
||||||
|
|
||||||
|
//! <b>Requires</b>: node is a tree node but not the header.
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: Unlinks the node and rebalances the tree.
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Average complexity is constant time.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
static void unlink(node_ptr node)
|
||||||
|
{
|
||||||
|
node_ptr x = NodeTraits::get_parent(node);
|
||||||
|
if(x){
|
||||||
|
while(!is_header(x))
|
||||||
|
x = NodeTraits::get_parent(x);
|
||||||
|
erase(x, node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//! <b>Requires</b>: header is the header of a tree.
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: Unlinks the leftmost node from the tree, and
|
||||||
|
//! updates the header link to the new leftmost node.
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Average complexity is constant time.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
//!
|
||||||
|
//! <b>Notes</b>: This function breaks the tree and the tree can
|
||||||
|
//! only be used for more unlink_leftmost_without_rebalance calls.
|
||||||
|
//! This function is normally used to achieve a step by step
|
||||||
|
//! controlled destruction of the tree.
|
||||||
|
static node_ptr unlink_leftmost_without_rebalance(node_ptr header)
|
||||||
|
{ return tree_algorithms::unlink_leftmost_without_rebalance(header); }
|
||||||
|
|
||||||
|
//! <b>Requires</b>: node is a node of the tree or an node initialized
|
||||||
|
//! by init(...).
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: Returns true if the node is initialized by init().
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Constant time.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
static bool unique(const_node_ptr node)
|
||||||
|
{ return tree_algorithms::unique(node); }
|
||||||
|
|
||||||
|
//! <b>Requires</b>: node is a node of the tree but it's not the header.
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: Returns the number of nodes of the subtree.
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Linear time.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
static std::size_t count(const_node_ptr node)
|
||||||
|
{ return tree_algorithms::count(node); }
|
||||||
|
|
||||||
|
//! <b>Requires</b>: p is a node from the tree except the header.
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: Returns the next node of the tree.
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Average constant time.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
static node_ptr next_node(node_ptr p)
|
||||||
|
{ return tree_algorithms::next_node(p); }
|
||||||
|
|
||||||
|
//! <b>Requires</b>: p is a node from the tree except the leftmost node.
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: Returns the previous node of the tree.
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Average constant time.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
static node_ptr prev_node(node_ptr p)
|
||||||
|
{ return tree_algorithms::prev_node(p); }
|
||||||
|
|
||||||
|
//! <b>Requires</b>: node must not be part of any tree.
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: After the function unique(node) == true.
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Constant.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
//!
|
||||||
|
//! <b>Nodes</b>: If node is inserted in a tree, this function corrupts the tree.
|
||||||
|
static void init(node_ptr node)
|
||||||
|
{ tree_algorithms::init(node); }
|
||||||
|
|
||||||
|
//! <b>Requires</b>: node must not be part of any tree.
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: Initializes the header to represent an empty tree.
|
||||||
|
//! unique(header) == true.
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Constant.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
//!
|
||||||
|
//! <b>Nodes</b>: If node is inserted in a tree, this function corrupts the tree.
|
||||||
|
static void init_header(node_ptr header)
|
||||||
|
{
|
||||||
|
tree_algorithms::init_header(header);
|
||||||
|
NodeTraits::set_balance(header, NodeTraits::zero());
|
||||||
|
}
|
||||||
|
|
||||||
|
//! <b>Requires</b>: header must be the header of a tree, z a node
|
||||||
|
//! of that tree and z != header.
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: Erases node "z" from the tree with header "header".
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Amortized constant time.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
static node_ptr erase(node_ptr header, node_ptr z)
|
||||||
|
{
|
||||||
|
typename tree_algorithms::data_for_rebalance info;
|
||||||
|
tree_algorithms::erase(header, z, avltree_erase_fixup(), info);
|
||||||
|
node_ptr x = info.x;
|
||||||
|
node_ptr x_parent = info.x_parent;
|
||||||
|
|
||||||
|
//Rebalance avltree
|
||||||
|
rebalance_after_erasure(header, x, x_parent);
|
||||||
|
return z;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! <b>Requires</b>: "cloner" must be a function
|
||||||
|
//! object taking a node_ptr and returning a new cloned node of it. "disposer" must
|
||||||
|
//! take a node_ptr and shouldn't throw.
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: First empties target tree calling
|
||||||
|
//! <tt>void disposer::operator()(node_ptr)</tt> for every node of the tree
|
||||||
|
//! except the header.
|
||||||
|
//!
|
||||||
|
//! Then, duplicates the entire tree pointed by "source_header" cloning each
|
||||||
|
//! source node with <tt>node_ptr Cloner::operator()(node_ptr)</tt> to obtain
|
||||||
|
//! the nodes of the target tree. If "cloner" throws, the cloned target nodes
|
||||||
|
//! are disposed using <tt>void disposer(node_ptr)</tt>.
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Linear to the number of element of the source tree plus the.
|
||||||
|
//! number of elements of tree target tree when calling this function.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: If cloner functor throws. If this happens target nodes are disposed.
|
||||||
|
template <class Cloner, class Disposer>
|
||||||
|
static void clone
|
||||||
|
(const_node_ptr source_header, node_ptr target_header, Cloner cloner, Disposer disposer)
|
||||||
|
{
|
||||||
|
avltree_node_cloner<Cloner> new_cloner(cloner);
|
||||||
|
tree_algorithms::clone(source_header, target_header, new_cloner, disposer);
|
||||||
|
}
|
||||||
|
|
||||||
|
//! <b>Requires</b>: "disposer" must be an object function
|
||||||
|
//! taking a node_ptr parameter and shouldn't throw.
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: Empties the target tree calling
|
||||||
|
//! <tt>void disposer::operator()(node_ptr)</tt> for every node of the tree
|
||||||
|
//! except the header.
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Linear to the number of element of the source tree plus the.
|
||||||
|
//! number of elements of tree target tree when calling this function.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: If cloner functor throws. If this happens target nodes are disposed.
|
||||||
|
template<class Disposer>
|
||||||
|
static void clear_and_dispose(node_ptr header, Disposer disposer)
|
||||||
|
{ tree_algorithms::clear_and_dispose(header, disposer); }
|
||||||
|
|
||||||
|
//! <b>Requires</b>: "header" must be the header node of a tree.
|
||||||
|
//! KeyNodePtrCompare is a function object that induces a strict weak
|
||||||
|
//! ordering compatible with the strict weak ordering used to create the
|
||||||
|
//! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: Returns an node_ptr to the first element that is
|
||||||
|
//! not less than "key" according to "comp" or "header" if that element does
|
||||||
|
//! not exist.
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Logarithmic.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: If "comp" throws.
|
||||||
|
template<class KeyType, class KeyNodePtrCompare>
|
||||||
|
static node_ptr lower_bound
|
||||||
|
(const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp)
|
||||||
|
{ return tree_algorithms::lower_bound(header, key, comp); }
|
||||||
|
|
||||||
|
//! <b>Requires</b>: "header" must be the header node of a tree.
|
||||||
|
//! KeyNodePtrCompare is a function object that induces a strict weak
|
||||||
|
//! ordering compatible with the strict weak ordering used to create the
|
||||||
|
//! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: Returns an node_ptr to the first element that is greater
|
||||||
|
//! than "key" according to "comp" or "header" if that element does not exist.
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Logarithmic.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: If "comp" throws.
|
||||||
|
template<class KeyType, class KeyNodePtrCompare>
|
||||||
|
static node_ptr upper_bound
|
||||||
|
(const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp)
|
||||||
|
{ return tree_algorithms::upper_bound(header, key, comp); }
|
||||||
|
|
||||||
|
//! <b>Requires</b>: "header" must be the header node of a tree.
|
||||||
|
//! KeyNodePtrCompare is a function object that induces a strict weak
|
||||||
|
//! ordering compatible with the strict weak ordering used to create the
|
||||||
|
//! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: Returns an node_ptr to the element that is equivalent to
|
||||||
|
//! "key" according to "comp" or "header" if that element does not exist.
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Logarithmic.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: If "comp" throws.
|
||||||
|
template<class KeyType, class KeyNodePtrCompare>
|
||||||
|
static node_ptr find
|
||||||
|
(const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp)
|
||||||
|
{ return tree_algorithms::find(header, key, comp); }
|
||||||
|
|
||||||
|
//! <b>Requires</b>: "header" must be the header node of a tree.
|
||||||
|
//! KeyNodePtrCompare is a function object that induces a strict weak
|
||||||
|
//! ordering compatible with the strict weak ordering used to create the
|
||||||
|
//! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: Returns an a pair of node_ptr delimiting a range containing
|
||||||
|
//! all elements that are equivalent to "key" according to "comp" or an
|
||||||
|
//! empty range that indicates the position where those elements would be
|
||||||
|
//! if they there are no equivalent elements.
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Logarithmic.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: If "comp" throws.
|
||||||
|
template<class KeyType, class KeyNodePtrCompare>
|
||||||
|
static std::pair<node_ptr, node_ptr> equal_range
|
||||||
|
(const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp)
|
||||||
|
{ return tree_algorithms::equal_range(header, key, comp); }
|
||||||
|
|
||||||
|
//! <b>Requires</b>: "h" must be the header node of a tree.
|
||||||
|
//! NodePtrCompare is a function object that induces a strict weak
|
||||||
|
//! ordering compatible with the strict weak ordering used to create the
|
||||||
|
//! the tree. NodePtrCompare compares two node_ptrs.
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: Inserts new_node into the tree before the upper bound
|
||||||
|
//! according to "comp".
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Average complexity for insert element is at
|
||||||
|
//! most logarithmic.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: If "comp" throws.
|
||||||
|
template<class NodePtrCompare>
|
||||||
|
static node_ptr insert_equal_upper_bound
|
||||||
|
(node_ptr h, node_ptr new_node, NodePtrCompare comp)
|
||||||
|
{
|
||||||
|
tree_algorithms::insert_equal_upper_bound(h, new_node, comp);
|
||||||
|
rebalance_after_insertion(h, new_node);
|
||||||
|
return new_node;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! <b>Requires</b>: "h" must be the header node of a tree.
|
||||||
|
//! NodePtrCompare is a function object that induces a strict weak
|
||||||
|
//! ordering compatible with the strict weak ordering used to create the
|
||||||
|
//! the tree. NodePtrCompare compares two node_ptrs.
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: Inserts new_node into the tree before the lower bound
|
||||||
|
//! according to "comp".
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Average complexity for insert element is at
|
||||||
|
//! most logarithmic.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: If "comp" throws.
|
||||||
|
template<class NodePtrCompare>
|
||||||
|
static node_ptr insert_equal_lower_bound
|
||||||
|
(node_ptr h, node_ptr new_node, NodePtrCompare comp)
|
||||||
|
{
|
||||||
|
tree_algorithms::insert_equal_lower_bound(h, new_node, comp);
|
||||||
|
rebalance_after_insertion(h, new_node);
|
||||||
|
return new_node;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! <b>Requires</b>: "header" must be the header node of a tree.
|
||||||
|
//! NodePtrCompare is a function object that induces a strict weak
|
||||||
|
//! ordering compatible with the strict weak ordering used to create the
|
||||||
|
//! the tree. NodePtrCompare compares two node_ptrs. "hint" is node from
|
||||||
|
//! the "header"'s tree.
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: Inserts new_node into the tree, using "hint" as a hint to
|
||||||
|
//! where it will be inserted. If "hint" is the upper_bound
|
||||||
|
//! the insertion takes constant time (two comparisons in the worst case).
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Logarithmic in general, but it is amortized
|
||||||
|
//! constant time if new_node is inserted immediately before "hint".
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: If "comp" throws.
|
||||||
|
template<class NodePtrCompare>
|
||||||
|
static node_ptr insert_equal
|
||||||
|
(node_ptr header, node_ptr hint, node_ptr new_node, NodePtrCompare comp)
|
||||||
|
{
|
||||||
|
tree_algorithms::insert_equal(header, hint, new_node, comp);
|
||||||
|
rebalance_after_insertion(header, new_node);
|
||||||
|
return new_node;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! <b>Requires</b>: "header" must be the header node of a tree.
|
||||||
|
//! KeyNodePtrCompare is a function object that induces a strict weak
|
||||||
|
//! ordering compatible with the strict weak ordering used to create the
|
||||||
|
//! the tree. NodePtrCompare compares KeyType with a node_ptr.
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: Checks if there is an equivalent node to "key" in the
|
||||||
|
//! tree according to "comp" and obtains the needed information to realize
|
||||||
|
//! a constant-time node insertion if there is no equivalent node.
|
||||||
|
//!
|
||||||
|
//! <b>Returns</b>: If there is an equivalent value
|
||||||
|
//! returns a pair containing a node_ptr to the already present node
|
||||||
|
//! and false. If there is not equivalent key can be inserted returns true
|
||||||
|
//! in the returned pair's boolean and fills "commit_data" that is meant to
|
||||||
|
//! be used with the "insert_commit" function to achieve a constant-time
|
||||||
|
//! insertion function.
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Average complexity is at most logarithmic.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: If "comp" throws.
|
||||||
|
//!
|
||||||
|
//! <b>Notes</b>: This function is used to improve performance when constructing
|
||||||
|
//! a node is expensive and the user does not want to have two equivalent nodes
|
||||||
|
//! in the tree: if there is an equivalent value
|
||||||
|
//! the constructed object must be discarded. Many times, the part of the
|
||||||
|
//! node that is used to impose the order is much cheaper to construct
|
||||||
|
//! than the node and this function offers the possibility to use that part
|
||||||
|
//! to check if the insertion will be successful.
|
||||||
|
//!
|
||||||
|
//! If the check is successful, the user can construct the node and use
|
||||||
|
//! "insert_commit" to insert the node in constant-time. This gives a total
|
||||||
|
//! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
|
||||||
|
//!
|
||||||
|
//! "commit_data" remains valid for a subsequent "insert_unique_commit" only
|
||||||
|
//! if no more objects are inserted or erased from the set.
|
||||||
|
template<class KeyType, class KeyNodePtrCompare>
|
||||||
|
static std::pair<node_ptr, bool> insert_unique_check
|
||||||
|
(const_node_ptr header, const KeyType &key
|
||||||
|
,KeyNodePtrCompare comp, insert_commit_data &commit_data)
|
||||||
|
{ return tree_algorithms::insert_unique_check(header, key, comp, commit_data); }
|
||||||
|
|
||||||
|
//! <b>Requires</b>: "header" must be the header node of a tree.
|
||||||
|
//! KeyNodePtrCompare is a function object that induces a strict weak
|
||||||
|
//! ordering compatible with the strict weak ordering used to create the
|
||||||
|
//! the tree. NodePtrCompare compares KeyType with a node_ptr.
|
||||||
|
//! "hint" is node from the "header"'s tree.
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: Checks if there is an equivalent node to "key" in the
|
||||||
|
//! tree according to "comp" using "hint" as a hint to where it should be
|
||||||
|
//! inserted and obtains the needed information to realize
|
||||||
|
//! a constant-time node insertion if there is no equivalent node.
|
||||||
|
//! If "hint" is the upper_bound the function has constant time
|
||||||
|
//! complexity (two comparisons in the worst case).
|
||||||
|
//!
|
||||||
|
//! <b>Returns</b>: If there is an equivalent value
|
||||||
|
//! returns a pair containing a node_ptr to the already present node
|
||||||
|
//! and false. If there is not equivalent key can be inserted returns true
|
||||||
|
//! in the returned pair's boolean and fills "commit_data" that is meant to
|
||||||
|
//! be used with the "insert_commit" function to achieve a constant-time
|
||||||
|
//! insertion function.
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Average complexity is at most logarithmic, but it is
|
||||||
|
//! amortized constant time if new_node should be inserted immediately before "hint".
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: If "comp" throws.
|
||||||
|
//!
|
||||||
|
//! <b>Notes</b>: This function is used to improve performance when constructing
|
||||||
|
//! a node is expensive and the user does not want to have two equivalent nodes
|
||||||
|
//! in the tree: if there is an equivalent value
|
||||||
|
//! the constructed object must be discarded. Many times, the part of the
|
||||||
|
//! node that is used to impose the order is much cheaper to construct
|
||||||
|
//! than the node and this function offers the possibility to use that part
|
||||||
|
//! to check if the insertion will be successful.
|
||||||
|
//!
|
||||||
|
//! If the check is successful, the user can construct the node and use
|
||||||
|
//! "insert_commit" to insert the node in constant-time. This gives a total
|
||||||
|
//! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
|
||||||
|
//!
|
||||||
|
//! "commit_data" remains valid for a subsequent "insert_unique_commit" only
|
||||||
|
//! if no more objects are inserted or erased from the set.
|
||||||
|
template<class KeyType, class KeyNodePtrCompare>
|
||||||
|
static std::pair<node_ptr, bool> insert_unique_check
|
||||||
|
(const_node_ptr header, node_ptr hint, const KeyType &key
|
||||||
|
,KeyNodePtrCompare comp, insert_commit_data &commit_data)
|
||||||
|
{ return tree_algorithms::insert_unique_check(header, hint, key, comp, commit_data); }
|
||||||
|
|
||||||
|
//! <b>Requires</b>: "header" must be the header node of a tree.
|
||||||
|
//! "commit_data" must have been obtained from a previous call to
|
||||||
|
//! "insert_unique_check". No objects should have been inserted or erased
|
||||||
|
//! from the set between the "insert_unique_check" that filled "commit_data"
|
||||||
|
//! and the call to "insert_commit".
|
||||||
|
//!
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: Inserts new_node in the set using the information obtained
|
||||||
|
//! from the "commit_data" that a previous "insert_check" filled.
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Constant time.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
//!
|
||||||
|
//! <b>Notes</b>: This function has only sense if a "insert_unique_check" has been
|
||||||
|
//! previously executed to fill "commit_data". No value should be inserted or
|
||||||
|
//! erased between the "insert_check" and "insert_commit" calls.
|
||||||
|
static void insert_unique_commit
|
||||||
|
(node_ptr header, node_ptr new_value, const insert_commit_data &commit_data)
|
||||||
|
{
|
||||||
|
tree_algorithms::insert_unique_commit(header, new_value, commit_data);
|
||||||
|
rebalance_after_insertion(header, new_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @cond
|
||||||
|
private:
|
||||||
|
|
||||||
|
//! <b>Requires</b>: p is a node of a tree.
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: Returns true if p is the header of the tree.
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Constant.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: Nothing.
|
||||||
|
static bool is_header(const_node_ptr p)
|
||||||
|
{ return NodeTraits::get_balance(p) == NodeTraits::zero() && tree_algorithms::is_header(p); }
|
||||||
|
|
||||||
|
static void rebalance_after_erasure(node_ptr header, node_ptr x, node_ptr x_parent)
|
||||||
|
{
|
||||||
|
node_ptr root = NodeTraits::get_parent(header);
|
||||||
|
while (x != root) {
|
||||||
|
const balance x_parent_balance = NodeTraits::get_balance(x_parent);
|
||||||
|
if(x_parent_balance == NodeTraits::zero()){
|
||||||
|
NodeTraits::set_balance(x_parent,
|
||||||
|
(x == NodeTraits::get_right(x_parent) ? NodeTraits::negative() : NodeTraits::positive()));
|
||||||
|
break; // the height didn't change, let's stop here
|
||||||
|
}
|
||||||
|
else if(x_parent_balance == NodeTraits::negative()){
|
||||||
|
if (x == NodeTraits::get_left(x_parent)) {
|
||||||
|
NodeTraits::set_balance(x_parent, NodeTraits::zero()); // balanced
|
||||||
|
x = x_parent;
|
||||||
|
x_parent = NodeTraits::get_parent(x_parent);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// x is right child
|
||||||
|
// a is left child
|
||||||
|
node_ptr a = NodeTraits::get_left(x_parent);
|
||||||
|
assert(a);
|
||||||
|
if (NodeTraits::get_balance(a) == NodeTraits::positive()) {
|
||||||
|
// a MUST have a right child
|
||||||
|
assert(NodeTraits::get_right(a));
|
||||||
|
rotate_left_right(x_parent, root);
|
||||||
|
|
||||||
|
x = NodeTraits::get_parent(x_parent);
|
||||||
|
x_parent = NodeTraits::get_parent(x);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
rotate_right(x_parent, root);
|
||||||
|
x = NodeTraits::get_parent(x_parent);
|
||||||
|
x_parent = NodeTraits::get_parent(x);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// if changed from negative to NodeTraits::positive(), no need to check above
|
||||||
|
if (NodeTraits::get_balance(x) == NodeTraits::positive()){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if(x_parent_balance == NodeTraits::positive()){
|
||||||
|
if (x == NodeTraits::get_right(x_parent)) {
|
||||||
|
NodeTraits::set_balance(x_parent, NodeTraits::zero()); // balanced
|
||||||
|
x = x_parent;
|
||||||
|
x_parent = NodeTraits::get_parent(x_parent);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// x is left child
|
||||||
|
// a is right child
|
||||||
|
node_ptr a = NodeTraits::get_right(x_parent);
|
||||||
|
assert(a);
|
||||||
|
if (NodeTraits::get_balance(a) == NodeTraits::negative()) {
|
||||||
|
// a MUST have then a left child
|
||||||
|
assert(NodeTraits::get_left(a));
|
||||||
|
rotate_right_left(x_parent, root);
|
||||||
|
|
||||||
|
x = NodeTraits::get_parent(x_parent);
|
||||||
|
x_parent = NodeTraits::get_parent(x);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
rotate_left(x_parent, root);
|
||||||
|
x = NodeTraits::get_parent(x_parent);
|
||||||
|
x_parent = NodeTraits::get_parent(x);
|
||||||
|
}
|
||||||
|
// if changed from NodeTraits::positive() to negative, no need to check above
|
||||||
|
if (NodeTraits::get_balance(x) == NodeTraits::negative()){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
assert(false); // never reached
|
||||||
|
}
|
||||||
|
}
|
||||||
|
NodeTraits::set_parent(header, root);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void rebalance_after_insertion(node_ptr header, node_ptr x)
|
||||||
|
{
|
||||||
|
node_ptr root = NodeTraits::get_parent(header);
|
||||||
|
NodeTraits::set_balance(x, NodeTraits::zero());
|
||||||
|
|
||||||
|
// Rebalance.
|
||||||
|
while (x != root){
|
||||||
|
const balance x_parent_balance = NodeTraits::get_balance(NodeTraits::get_parent(x));
|
||||||
|
|
||||||
|
if(x_parent_balance == NodeTraits::zero()){
|
||||||
|
// if x is left, parent will have parent->bal_factor = negative
|
||||||
|
// else, parent->bal_factor = NodeTraits::positive()
|
||||||
|
NodeTraits::set_balance( NodeTraits::get_parent(x)
|
||||||
|
, x == NodeTraits::get_left(NodeTraits::get_parent(x))
|
||||||
|
? NodeTraits::negative() : NodeTraits::positive() );
|
||||||
|
x = NodeTraits::get_parent(x);
|
||||||
|
}
|
||||||
|
else if(x_parent_balance == NodeTraits::positive()){
|
||||||
|
// if x is a left child, parent->bal_factor = zero
|
||||||
|
if (x == NodeTraits::get_left(NodeTraits::get_parent(x)))
|
||||||
|
NodeTraits::set_balance(NodeTraits::get_parent(x), NodeTraits::zero());
|
||||||
|
else{ // x is a right child, needs rebalancing
|
||||||
|
if (NodeTraits::get_balance(x) == NodeTraits::negative())
|
||||||
|
rotate_right_left(NodeTraits::get_parent(x), root);
|
||||||
|
else
|
||||||
|
rotate_left(NodeTraits::get_parent(x), root);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else if(x_parent_balance == NodeTraits::negative()){
|
||||||
|
// if x is a left child, needs rebalancing
|
||||||
|
if (x == NodeTraits::get_left(NodeTraits::get_parent(x))) {
|
||||||
|
if (NodeTraits::get_balance(x) == NodeTraits::positive())
|
||||||
|
rotate_left_right(NodeTraits::get_parent(x), root);
|
||||||
|
else
|
||||||
|
rotate_right(NodeTraits::get_parent(x), root);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
NodeTraits::set_balance(NodeTraits::get_parent(x), NodeTraits::zero());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
assert(false); // never reached
|
||||||
|
}
|
||||||
|
}
|
||||||
|
NodeTraits::set_parent(header, root);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void rotate_left_right(node_ptr a, node_ptr &root)
|
||||||
|
{
|
||||||
|
// | | //
|
||||||
|
// a(-2) c //
|
||||||
|
// / \ / \ //
|
||||||
|
// / \ ==> / \ //
|
||||||
|
// (pos)b [g] b a //
|
||||||
|
// / \ / \ / \ //
|
||||||
|
// [d] c [d] e f [g] //
|
||||||
|
// / \ //
|
||||||
|
// e f //
|
||||||
|
node_ptr b = NodeTraits::get_left(a), c = NodeTraits::get_right(b);
|
||||||
|
|
||||||
|
// switch
|
||||||
|
NodeTraits::set_left(a, NodeTraits::get_right(c));
|
||||||
|
NodeTraits::set_right(b, NodeTraits::get_left(c));
|
||||||
|
|
||||||
|
NodeTraits::set_right(c, a);
|
||||||
|
NodeTraits::set_left(c, b);
|
||||||
|
|
||||||
|
// set the parents
|
||||||
|
NodeTraits::set_parent(c, NodeTraits::get_parent(a));
|
||||||
|
NodeTraits::set_parent(a, c);
|
||||||
|
NodeTraits::set_parent(b, c);
|
||||||
|
|
||||||
|
if (NodeTraits::get_left(a)) // do we have f?
|
||||||
|
NodeTraits::set_parent(NodeTraits::get_left(a), a);
|
||||||
|
if (NodeTraits::get_right(b)) // do we have e?
|
||||||
|
NodeTraits::set_parent(NodeTraits::get_right(b), b);
|
||||||
|
|
||||||
|
if (a==root) root = c;
|
||||||
|
else // a had a parent, his child is now c
|
||||||
|
if (a == NodeTraits::get_left(NodeTraits::get_parent(c)))
|
||||||
|
NodeTraits::set_left(NodeTraits::get_parent(c), c);
|
||||||
|
else
|
||||||
|
NodeTraits::set_right(NodeTraits::get_parent(c), c);
|
||||||
|
|
||||||
|
// balancing...
|
||||||
|
const balance c_balance = NodeTraits::get_balance(c);
|
||||||
|
if(c_balance == NodeTraits::negative()){
|
||||||
|
NodeTraits::set_balance(a, NodeTraits::positive());
|
||||||
|
NodeTraits::set_balance(b, NodeTraits::zero());
|
||||||
|
}
|
||||||
|
else if(c_balance == NodeTraits::zero()){
|
||||||
|
NodeTraits::set_balance(a, NodeTraits::zero());
|
||||||
|
NodeTraits::set_balance(b, NodeTraits::zero());
|
||||||
|
}
|
||||||
|
else if(c_balance == NodeTraits::positive()){
|
||||||
|
NodeTraits::set_balance(a, NodeTraits::zero());
|
||||||
|
NodeTraits::set_balance(b, NodeTraits::negative());
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
assert(false); // never reached
|
||||||
|
}
|
||||||
|
NodeTraits::set_balance(c, NodeTraits::zero());
|
||||||
|
}
|
||||||
|
|
||||||
|
static void rotate_right_left(node_ptr a, node_ptr &root)
|
||||||
|
{
|
||||||
|
// | | //
|
||||||
|
// a(pos) c //
|
||||||
|
// / \ / \ //
|
||||||
|
// / \ / \ //
|
||||||
|
// [d] b(neg) ==> a b //
|
||||||
|
// / \ / \ / \ //
|
||||||
|
// c [g] [d] e f [g] //
|
||||||
|
// / \ //
|
||||||
|
// e f //
|
||||||
|
node_ptr b = NodeTraits::get_right(a), c = NodeTraits::get_left(b);
|
||||||
|
|
||||||
|
// switch
|
||||||
|
NodeTraits::set_right(a, NodeTraits::get_left(c));
|
||||||
|
NodeTraits::set_left(b, NodeTraits::get_right(c));
|
||||||
|
|
||||||
|
NodeTraits::set_left(c, a);
|
||||||
|
NodeTraits::set_right(c, b);
|
||||||
|
|
||||||
|
// set the parents
|
||||||
|
NodeTraits::set_parent(c, NodeTraits::get_parent(a));
|
||||||
|
NodeTraits::set_parent(a, c);
|
||||||
|
NodeTraits::set_parent(b, c);
|
||||||
|
|
||||||
|
if (NodeTraits::get_right(a)) // do we have e?
|
||||||
|
NodeTraits::set_parent(NodeTraits::get_right(a), a);
|
||||||
|
if (NodeTraits::get_left(b)) // do we have f?
|
||||||
|
NodeTraits::set_parent(NodeTraits::get_left(b), b);
|
||||||
|
|
||||||
|
if (a==root) root = c;
|
||||||
|
else // a had a parent, his child is now c
|
||||||
|
if (a == NodeTraits::get_left(NodeTraits::get_parent(c)))
|
||||||
|
NodeTraits::set_left(NodeTraits::get_parent(c), c);
|
||||||
|
else
|
||||||
|
NodeTraits::set_right(NodeTraits::get_parent(c), c);
|
||||||
|
|
||||||
|
// balancing...
|
||||||
|
const balance c_balance = NodeTraits::get_balance(c);
|
||||||
|
if(c_balance == NodeTraits::negative()){
|
||||||
|
NodeTraits::set_balance(a, NodeTraits::zero());
|
||||||
|
NodeTraits::set_balance(b, NodeTraits::positive());
|
||||||
|
}
|
||||||
|
else if(c_balance == NodeTraits::zero()){
|
||||||
|
NodeTraits::set_balance(a, NodeTraits::zero());
|
||||||
|
NodeTraits::set_balance(b, NodeTraits::zero());
|
||||||
|
}
|
||||||
|
else if(c_balance == NodeTraits::positive()){
|
||||||
|
NodeTraits::set_balance(a, NodeTraits::negative());
|
||||||
|
NodeTraits::set_balance(b, NodeTraits::zero());
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
NodeTraits::set_balance(c, NodeTraits::zero());
|
||||||
|
}
|
||||||
|
|
||||||
|
static void rotate_left(node_ptr x, node_ptr & root)
|
||||||
|
{
|
||||||
|
// | | //
|
||||||
|
// x(2) y(0) //
|
||||||
|
// / \ ==> / \ //
|
||||||
|
// n[a] y(1)n+2 n+1(0)x [c]n+1 //
|
||||||
|
// / \ / \ //
|
||||||
|
// n[b] [c]n+1 n[a] [b]n //
|
||||||
|
node_ptr y = NodeTraits::get_right(x);
|
||||||
|
|
||||||
|
// switch
|
||||||
|
NodeTraits::set_right(x, NodeTraits::get_left(y));
|
||||||
|
NodeTraits::set_left(y, x);
|
||||||
|
|
||||||
|
// rearrange parents
|
||||||
|
NodeTraits::set_parent(y, NodeTraits::get_parent(x));
|
||||||
|
NodeTraits::set_parent(x, y);
|
||||||
|
|
||||||
|
// do we have [b]?
|
||||||
|
if (NodeTraits::get_right(x))
|
||||||
|
NodeTraits::set_parent(NodeTraits::get_right(x), x);
|
||||||
|
|
||||||
|
if (x == root)
|
||||||
|
root = y;
|
||||||
|
else
|
||||||
|
// need to reparent y
|
||||||
|
if (NodeTraits::get_left(NodeTraits::get_parent(y)) == x)
|
||||||
|
NodeTraits::set_left(NodeTraits::get_parent(y), y);
|
||||||
|
else
|
||||||
|
NodeTraits::set_right(NodeTraits::get_parent(y), y);
|
||||||
|
|
||||||
|
// reset the balancing factor
|
||||||
|
if (NodeTraits::get_balance(y) == NodeTraits::positive()) {
|
||||||
|
NodeTraits::set_balance(x, NodeTraits::zero());
|
||||||
|
NodeTraits::set_balance(y, NodeTraits::zero());
|
||||||
|
}
|
||||||
|
else { // this doesn't happen during insertions
|
||||||
|
NodeTraits::set_balance(x, NodeTraits::positive());
|
||||||
|
NodeTraits::set_balance(y, NodeTraits::negative());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void rotate_right(node_ptr x, node_ptr &root)
|
||||||
|
{
|
||||||
|
node_ptr y = NodeTraits::get_left(x);
|
||||||
|
|
||||||
|
// switch
|
||||||
|
NodeTraits::set_left(x, NodeTraits::get_right(y));
|
||||||
|
NodeTraits::set_right(y, x);
|
||||||
|
|
||||||
|
// rearrange parents
|
||||||
|
NodeTraits::set_parent(y, NodeTraits::get_parent(x));
|
||||||
|
NodeTraits::set_parent(x, y);
|
||||||
|
|
||||||
|
// do we have [b]?
|
||||||
|
if (NodeTraits::get_left(x))
|
||||||
|
NodeTraits::set_parent(NodeTraits::get_left(x), x);
|
||||||
|
|
||||||
|
if (x == root)
|
||||||
|
root = y;
|
||||||
|
else
|
||||||
|
// need to reparent y
|
||||||
|
if (NodeTraits::get_left(NodeTraits::get_parent(y)) == x)
|
||||||
|
NodeTraits::set_left(NodeTraits::get_parent(y), y);
|
||||||
|
else
|
||||||
|
NodeTraits::set_right(NodeTraits::get_parent(y), y);
|
||||||
|
|
||||||
|
// reset the balancing factor
|
||||||
|
if (NodeTraits::get_balance(y) == NodeTraits::negative()) {
|
||||||
|
NodeTraits::set_balance(x, NodeTraits::zero());
|
||||||
|
NodeTraits::set_balance(y, NodeTraits::zero());
|
||||||
|
}
|
||||||
|
else { // this doesn't happen during insertions
|
||||||
|
NodeTraits::set_balance(x, NodeTraits::negative());
|
||||||
|
NodeTraits::set_balance(y, NodeTraits::positive());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @endcond
|
||||||
|
};
|
||||||
|
|
||||||
|
} //namespace intrusive
|
||||||
|
} //namespace boost
|
||||||
|
|
||||||
|
#include <boost/intrusive/detail/config_end.hpp>
|
||||||
|
|
||||||
|
#endif //BOOST_INTRUSIVE_AVLTREE_ALGORITHMS_HPP
|
179
include/boost/intrusive/detail/avltree_node.hpp
Normal file
179
include/boost/intrusive/detail/avltree_node.hpp
Normal file
@@ -0,0 +1,179 @@
|
|||||||
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// (C) Copyright Ion Gaztanaga 2007.
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0.
|
||||||
|
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||||
|
// http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
// See http://www.boost.org/libs/intrusive for documentation.
|
||||||
|
//
|
||||||
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#ifndef BOOST_INTRUSIVE_AVLTREE_NODE_HPP
|
||||||
|
#define BOOST_INTRUSIVE_AVLTREE_NODE_HPP
|
||||||
|
|
||||||
|
#include <boost/intrusive/detail/config_begin.hpp>
|
||||||
|
#include <iterator>
|
||||||
|
#include <boost/intrusive/detail/pointer_to_other.hpp>
|
||||||
|
#include <boost/intrusive/avltree_algorithms.hpp>
|
||||||
|
#include <boost/intrusive/pointer_plus_2_bits.hpp>
|
||||||
|
#include <boost/intrusive/detail/mpl.hpp>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace intrusive {
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
// //
|
||||||
|
// Generic node_traits for any pointer type //
|
||||||
|
// //
|
||||||
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
//This is the compact representation: 3 pointers
|
||||||
|
template<class VoidPointer>
|
||||||
|
struct compact_avltree_node
|
||||||
|
{
|
||||||
|
typedef typename pointer_to_other
|
||||||
|
<VoidPointer
|
||||||
|
,compact_avltree_node<VoidPointer> >::type node_ptr;
|
||||||
|
enum balance { negative_t, zero_t, positive_t };
|
||||||
|
node_ptr parent_, left_, right_;
|
||||||
|
};
|
||||||
|
|
||||||
|
//This is the normal representation: 3 pointers + enum
|
||||||
|
template<class VoidPointer>
|
||||||
|
struct avltree_node
|
||||||
|
{
|
||||||
|
typedef typename pointer_to_other
|
||||||
|
<VoidPointer
|
||||||
|
,avltree_node<VoidPointer> >::type node_ptr;
|
||||||
|
enum balance { negative_t, zero_t, positive_t };
|
||||||
|
node_ptr parent_, left_, right_;
|
||||||
|
balance balance_;
|
||||||
|
};
|
||||||
|
|
||||||
|
//This is the default node traits implementation
|
||||||
|
//using a node with 3 generic pointers plus an enum
|
||||||
|
template<class VoidPointer>
|
||||||
|
struct default_avltree_node_traits_impl
|
||||||
|
{
|
||||||
|
typedef avltree_node<VoidPointer> node;
|
||||||
|
|
||||||
|
typedef typename boost::pointer_to_other
|
||||||
|
<VoidPointer, node>::type node_ptr;
|
||||||
|
typedef typename boost::pointer_to_other
|
||||||
|
<VoidPointer, const node>::type const_node_ptr;
|
||||||
|
typedef typename node::balance balance;
|
||||||
|
|
||||||
|
static node_ptr get_parent(const_node_ptr n)
|
||||||
|
{ return n->parent_; }
|
||||||
|
|
||||||
|
static void set_parent(node_ptr n, node_ptr p)
|
||||||
|
{ n->parent_ = p; }
|
||||||
|
|
||||||
|
static node_ptr get_left(const_node_ptr n)
|
||||||
|
{ return n->left_; }
|
||||||
|
|
||||||
|
static void set_left(node_ptr n, node_ptr l)
|
||||||
|
{ n->left_ = l; }
|
||||||
|
|
||||||
|
static node_ptr get_right(const_node_ptr n)
|
||||||
|
{ return n->right_; }
|
||||||
|
|
||||||
|
static void set_right(node_ptr n, node_ptr r)
|
||||||
|
{ n->right_ = r; }
|
||||||
|
|
||||||
|
static balance get_balance(const_node_ptr n)
|
||||||
|
{ return n->balance_; }
|
||||||
|
|
||||||
|
static void set_balance(node_ptr n, balance b)
|
||||||
|
{ n->balance_ = b; }
|
||||||
|
|
||||||
|
static balance negative()
|
||||||
|
{ return node::negative_t; }
|
||||||
|
|
||||||
|
static balance zero()
|
||||||
|
{ return node::zero_t; }
|
||||||
|
|
||||||
|
static balance positive()
|
||||||
|
{ return node::positive_t; }
|
||||||
|
};
|
||||||
|
|
||||||
|
//This is the compact node traits implementation
|
||||||
|
//using a node with 3 generic pointers
|
||||||
|
template<class VoidPointer>
|
||||||
|
struct compact_avltree_node_traits_impl
|
||||||
|
{
|
||||||
|
typedef compact_avltree_node<VoidPointer> node;
|
||||||
|
typedef typename boost::pointer_to_other
|
||||||
|
<VoidPointer, node>::type node_ptr;
|
||||||
|
typedef typename boost::pointer_to_other
|
||||||
|
<VoidPointer, const node>::type const_node_ptr;
|
||||||
|
typedef typename node::balance balance;
|
||||||
|
|
||||||
|
typedef pointer_plus_2_bits<node_ptr> ptr_bit;
|
||||||
|
|
||||||
|
static node_ptr get_parent(const_node_ptr n)
|
||||||
|
{ return ptr_bit::get_pointer(n->parent_); }
|
||||||
|
|
||||||
|
static void set_parent(node_ptr n, node_ptr p)
|
||||||
|
{ ptr_bit::set_pointer(n->parent_, p); }
|
||||||
|
|
||||||
|
static node_ptr get_left(const_node_ptr n)
|
||||||
|
{ return n->left_; }
|
||||||
|
|
||||||
|
static void set_left(node_ptr n, node_ptr l)
|
||||||
|
{ n->left_ = l; }
|
||||||
|
|
||||||
|
static node_ptr get_right(const_node_ptr n)
|
||||||
|
{ return n->right_; }
|
||||||
|
|
||||||
|
static void set_right(node_ptr n, node_ptr r)
|
||||||
|
{ n->right_ = r; }
|
||||||
|
|
||||||
|
static balance get_balance(const_node_ptr n)
|
||||||
|
{ return (balance)ptr_bit::get_bits(n->parent_); }
|
||||||
|
|
||||||
|
static void set_balance(node_ptr n, balance b)
|
||||||
|
{ ptr_bit::set_bits(n->parent_, (std::size_t)b); }
|
||||||
|
|
||||||
|
static balance negative()
|
||||||
|
{ return node::negative_t; }
|
||||||
|
|
||||||
|
static balance zero()
|
||||||
|
{ return node::zero_t; }
|
||||||
|
|
||||||
|
static balance positive()
|
||||||
|
{ return node::positive_t; }
|
||||||
|
};
|
||||||
|
|
||||||
|
//Dispatches the implementation based on the boolean
|
||||||
|
template<class VoidPointer, bool compact>
|
||||||
|
struct avltree_node_traits_dispatch
|
||||||
|
: public default_avltree_node_traits_impl<VoidPointer>
|
||||||
|
{};
|
||||||
|
|
||||||
|
template<class VoidPointer>
|
||||||
|
struct avltree_node_traits_dispatch<VoidPointer, true>
|
||||||
|
: public compact_avltree_node_traits_impl<VoidPointer>
|
||||||
|
{};
|
||||||
|
|
||||||
|
//Inherit from the detail::link_dispatch depending on the embedding capabilities
|
||||||
|
template<class VoidPointer, bool OptimizeSize = false>
|
||||||
|
struct avltree_node_traits
|
||||||
|
: public avltree_node_traits_dispatch
|
||||||
|
< VoidPointer
|
||||||
|
, OptimizeSize &&
|
||||||
|
has_pointer_plus_2_bits
|
||||||
|
< VoidPointer
|
||||||
|
, detail::alignment_of<compact_avltree_node<VoidPointer> >::value
|
||||||
|
>::value
|
||||||
|
>
|
||||||
|
{};
|
||||||
|
|
||||||
|
} //namespace intrusive
|
||||||
|
} //namespace boost
|
||||||
|
|
||||||
|
#include <boost/intrusive/detail/config_end.hpp>
|
||||||
|
|
||||||
|
#endif //BOOST_INTRUSIVE_AVLTREE_NODE_HPP
|
@@ -33,6 +33,7 @@ enum
|
|||||||
, SetBaseHook
|
, SetBaseHook
|
||||||
, UsetBaseHook
|
, UsetBaseHook
|
||||||
, SplaySetBaseHook
|
, SplaySetBaseHook
|
||||||
|
, AvlSetBaseHook
|
||||||
};
|
};
|
||||||
|
|
||||||
struct no_default_definer{};
|
struct no_default_definer{};
|
||||||
@@ -52,13 +53,17 @@ template <class Hook>
|
|||||||
struct default_definer<Hook, SetBaseHook>
|
struct default_definer<Hook, SetBaseHook>
|
||||||
{ typedef Hook default_set_hook; };
|
{ typedef Hook default_set_hook; };
|
||||||
|
|
||||||
|
template <class Hook>
|
||||||
|
struct default_definer<Hook, UsetBaseHook>
|
||||||
|
{ typedef Hook default_uset_hook; };
|
||||||
|
|
||||||
template <class Hook>
|
template <class Hook>
|
||||||
struct default_definer<Hook, SplaySetBaseHook>
|
struct default_definer<Hook, SplaySetBaseHook>
|
||||||
{ typedef Hook default_splay_set_hook; };
|
{ typedef Hook default_splay_set_hook; };
|
||||||
|
|
||||||
template <class Hook>
|
template <class Hook>
|
||||||
struct default_definer<Hook, UsetBaseHook>
|
struct default_definer<Hook, AvlSetBaseHook>
|
||||||
{ typedef Hook default_uset_hook; };
|
{ typedef Hook default_avl_set_hook; };
|
||||||
|
|
||||||
template <class Hook, unsigned int BaseHookType>
|
template <class Hook, unsigned int BaseHookType>
|
||||||
struct make_default_definer
|
struct make_default_definer
|
||||||
|
@@ -168,131 +168,7 @@ struct rbtree_node_traits
|
|||||||
>::value
|
>::value
|
||||||
>
|
>
|
||||||
{};
|
{};
|
||||||
/*
|
|
||||||
/////////////////////////////////////////////////////////////////////////////
|
|
||||||
// //
|
|
||||||
// Implementation of the rbtree iterator //
|
|
||||||
// //
|
|
||||||
/////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
// rbtree_iterator provides some basic functions for a
|
|
||||||
// node oriented bidirectional iterator:
|
|
||||||
template<class Container, bool IsConst>
|
|
||||||
class rbtree_iterator
|
|
||||||
: public std::iterator
|
|
||||||
< std::bidirectional_iterator_tag
|
|
||||||
, typename detail::add_const_if_c
|
|
||||||
<typename Container::value_type, IsConst>::type
|
|
||||||
>
|
|
||||||
{
|
|
||||||
protected:
|
|
||||||
typedef typename Container::real_value_traits real_value_traits;
|
|
||||||
typedef typename real_value_traits::node_traits node_traits;
|
|
||||||
typedef typename node_traits::node node;
|
|
||||||
typedef typename node_traits::node_ptr node_ptr;
|
|
||||||
typedef rbtree_algorithms<node_traits> node_algorithms;
|
|
||||||
typedef typename boost::pointer_to_other
|
|
||||||
<node_ptr, void>::type void_pointer;
|
|
||||||
static const bool store_container_ptr =
|
|
||||||
detail::store_cont_ptr_on_it<Container>::value;
|
|
||||||
|
|
||||||
public:
|
|
||||||
public:
|
|
||||||
typedef typename detail::add_const_if_c
|
|
||||||
<typename Container::value_type, IsConst>
|
|
||||||
::type value_type;
|
|
||||||
typedef value_type & reference;
|
|
||||||
typedef value_type * pointer;
|
|
||||||
|
|
||||||
rbtree_iterator()
|
|
||||||
: members_ (0, 0)
|
|
||||||
{}
|
|
||||||
|
|
||||||
explicit rbtree_iterator(node_ptr node, const Container *cont_ptr)
|
|
||||||
: members_ (node, cont_ptr)
|
|
||||||
{}
|
|
||||||
|
|
||||||
rbtree_iterator(rbtree_iterator<Container, false> const& other)
|
|
||||||
: members_(other.pointed_node(), other.get_container())
|
|
||||||
{}
|
|
||||||
|
|
||||||
const node_ptr &pointed_node() const
|
|
||||||
{ return members_.nodeptr_; }
|
|
||||||
|
|
||||||
rbtree_iterator &operator=(const node_ptr &node)
|
|
||||||
{ members_.nodeptr_ = node; return static_cast<rbtree_iterator&>(*this); }
|
|
||||||
|
|
||||||
public:
|
|
||||||
rbtree_iterator& operator++()
|
|
||||||
{
|
|
||||||
members_.nodeptr_ = node_algorithms::next_node(members_.nodeptr_);
|
|
||||||
return static_cast<rbtree_iterator&> (*this);
|
|
||||||
}
|
|
||||||
|
|
||||||
rbtree_iterator operator++(int)
|
|
||||||
{
|
|
||||||
rbtree_iterator result (*this);
|
|
||||||
members_.nodeptr_ = node_algorithms::next_node(members_.nodeptr_);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
rbtree_iterator& operator--()
|
|
||||||
{
|
|
||||||
members_.nodeptr_ = node_algorithms::prev_node(members_.nodeptr_);
|
|
||||||
return static_cast<rbtree_iterator&> (*this);
|
|
||||||
}
|
|
||||||
|
|
||||||
rbtree_iterator operator--(int)
|
|
||||||
{
|
|
||||||
rbtree_iterator result (*this);
|
|
||||||
members_.nodeptr_ = node_algorithms::prev_node(members_.nodeptr_);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator== (const rbtree_iterator& i) const
|
|
||||||
{ return members_.nodeptr_ == i.pointed_node(); }
|
|
||||||
|
|
||||||
bool operator!= (const rbtree_iterator& i) const
|
|
||||||
{ return !operator== (i); }
|
|
||||||
|
|
||||||
value_type& operator*() const
|
|
||||||
{ return *operator->(); }
|
|
||||||
|
|
||||||
pointer operator->() const
|
|
||||||
{ return detail::get_pointer(this->get_real_value_traits()->to_value_ptr(members_.nodeptr_)); }
|
|
||||||
|
|
||||||
const Container *get_container() const
|
|
||||||
{
|
|
||||||
if(store_container_ptr)
|
|
||||||
return static_cast<const Container*>(members_.get_ptr());
|
|
||||||
else
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
const real_value_traits *get_real_value_traits() const
|
|
||||||
{
|
|
||||||
if(store_container_ptr)
|
|
||||||
return &this->get_container()->get_real_value_traits();
|
|
||||||
else
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
struct members
|
|
||||||
: public detail::select_constptr
|
|
||||||
<void_pointer, store_container_ptr>::type
|
|
||||||
{
|
|
||||||
typedef typename detail::select_constptr
|
|
||||||
<void_pointer, store_container_ptr>::type Base;
|
|
||||||
|
|
||||||
members(const node_ptr &n_ptr, const void *cont)
|
|
||||||
: Base(cont), nodeptr_(n_ptr)
|
|
||||||
{}
|
|
||||||
|
|
||||||
node_ptr nodeptr_;
|
|
||||||
} members_;
|
|
||||||
};
|
|
||||||
*/
|
|
||||||
} //namespace intrusive
|
} //namespace intrusive
|
||||||
} //namespace boost
|
} //namespace boost
|
||||||
|
|
||||||
|
@@ -693,8 +693,7 @@ class tree_algorithms
|
|||||||
{
|
{
|
||||||
node_ptr end = uncast(header);
|
node_ptr end = uncast(header);
|
||||||
node_ptr y = lower_bound(header, key, comp);
|
node_ptr y = lower_bound(header, key, comp);
|
||||||
node_ptr r = (y == end || comp(key, y)) ? end : y;
|
return (y == end || comp(key, y)) ? end : y;
|
||||||
return r;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//! <b>Requires</b>: "header" must be the header node of a tree.
|
//! <b>Requires</b>: "header" must be the header node of a tree.
|
||||||
@@ -1243,16 +1242,39 @@ class tree_algorithms
|
|||||||
|
|
||||||
// delete node | complexity : constant | exception : nothrow
|
// delete node | complexity : constant | exception : nothrow
|
||||||
static void erase(node_ptr header, node_ptr z)
|
static void erase(node_ptr header, node_ptr z)
|
||||||
{ erase(header, z, nop_erase_fixup()); }
|
{
|
||||||
|
data_for_rebalance ignored;
|
||||||
|
erase(header, z, nop_erase_fixup(), ignored);
|
||||||
|
}
|
||||||
|
|
||||||
struct data_for_rebalance
|
struct data_for_rebalance
|
||||||
{
|
{
|
||||||
node_ptr x;
|
node_ptr x;
|
||||||
node_ptr x_parent;
|
node_ptr x_parent;
|
||||||
|
node_ptr y;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class F>
|
template<class F>
|
||||||
static void erase(node_ptr header, node_ptr z, F z_and_successor_fixup, data_for_rebalance * info = 0)
|
static void erase(node_ptr header, node_ptr z, F z_and_successor_fixup, data_for_rebalance &info)
|
||||||
|
{
|
||||||
|
erase_impl(header, z, info);
|
||||||
|
if(info.y != z){
|
||||||
|
z_and_successor_fixup(z, info.y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void unlink(node_ptr node)
|
||||||
|
{
|
||||||
|
node_ptr x = NodeTraits::get_parent(node);
|
||||||
|
if(x){
|
||||||
|
while(!is_header(x))
|
||||||
|
x = NodeTraits::get_parent(x);
|
||||||
|
erase(x, node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static void erase_impl(node_ptr header, node_ptr z, data_for_rebalance &info)
|
||||||
{
|
{
|
||||||
node_ptr y(z);
|
node_ptr y(z);
|
||||||
node_ptr x;
|
node_ptr x;
|
||||||
@@ -1287,7 +1309,6 @@ class tree_algorithms
|
|||||||
x_parent = y;
|
x_parent = y;
|
||||||
tree_algorithms::replace_own (z, y, header);
|
tree_algorithms::replace_own (z, y, header);
|
||||||
NodeTraits::set_parent(y, NodeTraits::get_parent(z));
|
NodeTraits::set_parent(y, NodeTraits::get_parent(z));
|
||||||
z_and_successor_fixup(z, y);
|
|
||||||
}
|
}
|
||||||
else { // y == z --> z has only one child, or none
|
else { // y == z --> z has only one child, or none
|
||||||
x_parent = NodeTraits::get_parent(z);
|
x_parent = NodeTraits::get_parent(z);
|
||||||
@@ -1306,21 +1327,11 @@ class tree_algorithms
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(info){
|
info.x = x;
|
||||||
info->x = x;
|
info.x_parent = x_parent;
|
||||||
info->x_parent = x_parent;
|
info.y = y;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void unlink(node_ptr node)
|
|
||||||
{
|
|
||||||
node_ptr x = NodeTraits::get_parent(node);
|
|
||||||
if(x){
|
|
||||||
while(!is_header(x))
|
|
||||||
x = NodeTraits::get_parent(x);
|
|
||||||
erase(x, node);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} //namespace detail {
|
} //namespace detail {
|
||||||
|
@@ -195,6 +195,50 @@ template
|
|||||||
>
|
>
|
||||||
class splay_set_member_hook;
|
class splay_set_member_hook;
|
||||||
|
|
||||||
|
//avltree/avl_set/avl_multiset
|
||||||
|
template
|
||||||
|
< class T
|
||||||
|
, class O1 = none
|
||||||
|
, class O2 = none
|
||||||
|
, class O3 = none
|
||||||
|
, class O4 = none
|
||||||
|
>
|
||||||
|
class avltree;
|
||||||
|
|
||||||
|
template
|
||||||
|
< class T
|
||||||
|
, class O1 = none
|
||||||
|
, class O2 = none
|
||||||
|
, class O3 = none
|
||||||
|
, class O4 = none
|
||||||
|
>
|
||||||
|
class avl_set;
|
||||||
|
|
||||||
|
template
|
||||||
|
< class T
|
||||||
|
, class O1 = none
|
||||||
|
, class O2 = none
|
||||||
|
, class O3 = none
|
||||||
|
, class O4 = none
|
||||||
|
>
|
||||||
|
class avl_multiset;
|
||||||
|
|
||||||
|
template
|
||||||
|
< class O1 = none
|
||||||
|
, class O2 = none
|
||||||
|
, class O3 = none
|
||||||
|
, class O4 = none
|
||||||
|
>
|
||||||
|
class avl_set_base_hook;
|
||||||
|
|
||||||
|
template
|
||||||
|
< class O1 = none
|
||||||
|
, class O2 = none
|
||||||
|
, class O3 = none
|
||||||
|
, class O4 = none
|
||||||
|
>
|
||||||
|
class avl_set_member_hook;
|
||||||
|
|
||||||
//hash/unordered
|
//hash/unordered
|
||||||
//rbtree/set/multiset
|
//rbtree/set/multiset
|
||||||
template
|
template
|
||||||
|
82
include/boost/intrusive/pointer_plus_2_bits.hpp
Normal file
82
include/boost/intrusive/pointer_plus_2_bits.hpp
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// (C) Copyright Ion Gaztanaga 2007
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0.
|
||||||
|
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||||
|
// http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
// See http://www.boost.org/libs/intrusive for documentation.
|
||||||
|
//
|
||||||
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#ifndef BOOST_INTRUSIVE_POINTER_PLUS_2_BIT_HPP
|
||||||
|
#define BOOST_INTRUSIVE_POINTER_PLUS_2_BIT_HPP
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace intrusive {
|
||||||
|
|
||||||
|
//!This trait class is used to know if a pointer
|
||||||
|
//!can embed 2 extra bits of information if
|
||||||
|
//!it's going to be used to point to objects
|
||||||
|
//!with an alignment of "Alignment" bytes.
|
||||||
|
template<class VoidPointer, std::size_t Alignment>
|
||||||
|
struct has_pointer_plus_2_bits
|
||||||
|
{
|
||||||
|
static const bool value = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
//!This is an specialization for raw pointers.
|
||||||
|
//!Raw pointers can embed two extra bits in the lower bits
|
||||||
|
//!if the alignment is multiple of 4.
|
||||||
|
template<std::size_t N>
|
||||||
|
struct has_pointer_plus_2_bits<void*, N>
|
||||||
|
{
|
||||||
|
static const bool value = (N % 4u == 0);
|
||||||
|
};
|
||||||
|
|
||||||
|
//!This is class that is supposed to have static methods
|
||||||
|
//!to embed 2 extra bits of information in a pointer.
|
||||||
|
//!
|
||||||
|
//!This is a declaration and there is no default implementation,
|
||||||
|
//!because operations to embed bits change with every pointer type.
|
||||||
|
//!
|
||||||
|
//!An implementation that detects that a pointer type whose
|
||||||
|
//!has_pointer_plus_2_bits<>::value is non-zero can make use of these
|
||||||
|
//!operations to embed bits in the pointer.
|
||||||
|
template<class Pointer>
|
||||||
|
struct pointer_plus_2_bits
|
||||||
|
{
|
||||||
|
static const bool value = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
//!This is the specialization to embed 2 extra bits of information
|
||||||
|
//!in a raw pointer. Extra bits are stored in the lower bits of the pointer.
|
||||||
|
template<class T>
|
||||||
|
struct pointer_plus_2_bits<T*>
|
||||||
|
{
|
||||||
|
typedef T* pointer;
|
||||||
|
|
||||||
|
static pointer get_pointer(pointer n)
|
||||||
|
{ return pointer(std::size_t(n) & ~std::size_t(3u)); }
|
||||||
|
|
||||||
|
static void set_pointer(pointer &n, pointer p)
|
||||||
|
{
|
||||||
|
assert(0 == (std::size_t(p) & std::size_t(3u)));
|
||||||
|
n = pointer(std::size_t(p) | (std::size_t(n) & std::size_t(3u)));
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::size_t get_bits(pointer n)
|
||||||
|
{ return (std::size_t(n) & std::size_t(3u)); }
|
||||||
|
|
||||||
|
static void set_bits(pointer &n, std::size_t c)
|
||||||
|
{
|
||||||
|
assert(c < 4);
|
||||||
|
n = pointer(std::size_t(get_pointer(n)) | c);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} //namespace intrusive
|
||||||
|
} //namespace boost
|
||||||
|
|
||||||
|
#endif //BOOST_INTRUSIVE_POINTER_PLUS_2_BIT_HPP
|
@@ -393,7 +393,7 @@ class rbtree_algorithms
|
|||||||
static node_ptr erase(node_ptr header, node_ptr z)
|
static node_ptr erase(node_ptr header, node_ptr z)
|
||||||
{
|
{
|
||||||
typename tree_algorithms::data_for_rebalance info;
|
typename tree_algorithms::data_for_rebalance info;
|
||||||
tree_algorithms::erase(header, z, rbtree_erase_fixup(), &info);
|
tree_algorithms::erase(header, z, rbtree_erase_fixup(), info);
|
||||||
node_ptr x = info.x;
|
node_ptr x = info.x;
|
||||||
node_ptr x_parent = info.x_parent;
|
node_ptr x_parent = info.x_parent;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user