forked from boostorg/intrusive
Fixed Solaris-gcc errors and added splay trees
[SVN r40428]
This commit is contained in:
@@ -32,6 +32,7 @@ enum
|
|||||||
, SlistBaseHook
|
, SlistBaseHook
|
||||||
, SetBaseHook
|
, SetBaseHook
|
||||||
, UsetBaseHook
|
, UsetBaseHook
|
||||||
|
, SplaySetBaseHook
|
||||||
};
|
};
|
||||||
|
|
||||||
struct no_default_definer{};
|
struct no_default_definer{};
|
||||||
@@ -51,6 +52,10 @@ 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, SplaySetBaseHook>
|
||||||
|
{ typedef Hook default_splay_set_hook; };
|
||||||
|
|
||||||
template <class Hook>
|
template <class Hook>
|
||||||
struct default_definer<Hook, UsetBaseHook>
|
struct default_definer<Hook, UsetBaseHook>
|
||||||
{ typedef Hook default_uset_hook; };
|
{ typedef Hook default_uset_hook; };
|
||||||
@@ -115,7 +120,7 @@ class generic_hook
|
|||||||
static const link_mode_type link_mode = LinkMode;
|
static const link_mode_type link_mode = LinkMode;
|
||||||
typedef Tag tag;
|
typedef Tag tag;
|
||||||
typedef typename GetNodeAlgorithms::type node_algorithms;
|
typedef typename GetNodeAlgorithms::type node_algorithms;
|
||||||
typedef typename node_algorithms::node_traits node_traits;
|
typedef typename node_algorithms::node_traits node_traits;
|
||||||
typedef typename node_traits::node node;
|
typedef typename node_traits::node node;
|
||||||
typedef typename node_traits::node_ptr node_ptr;
|
typedef typename node_traits::node_ptr node_ptr;
|
||||||
typedef typename node_traits::const_node_ptr const_node_ptr;
|
typedef typename node_traits::const_node_ptr const_node_ptr;
|
||||||
|
@@ -253,6 +253,14 @@ template<typename T>
|
|||||||
struct add_const
|
struct add_const
|
||||||
{ typedef const T type; };
|
{ typedef const T type; };
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct remove_const
|
||||||
|
{ typedef T type; };
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct remove_const<const T>
|
||||||
|
{ typedef T type; };
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
struct remove_reference
|
struct remove_reference
|
||||||
{
|
{
|
||||||
|
@@ -157,17 +157,18 @@ struct rbtree_node_traits_dispatch<VoidPointer, true>
|
|||||||
{};
|
{};
|
||||||
|
|
||||||
//Inherit from the detail::link_dispatch depending on the embedding capabilities
|
//Inherit from the detail::link_dispatch depending on the embedding capabilities
|
||||||
template<class VoidPointer>
|
template<class VoidPointer, bool OptimizeSize = false>
|
||||||
struct rbtree_node_traits
|
struct rbtree_node_traits
|
||||||
: public rbtree_node_traits_dispatch
|
: public rbtree_node_traits_dispatch
|
||||||
< VoidPointer
|
< VoidPointer
|
||||||
, has_pointer_plus_bit
|
, OptimizeSize &&
|
||||||
|
has_pointer_plus_bit
|
||||||
< VoidPointer
|
< VoidPointer
|
||||||
, detail::alignment_of<compact_rbtree_node<VoidPointer> >::value
|
, detail::alignment_of<compact_rbtree_node<VoidPointer> >::value
|
||||||
>::value
|
>::value
|
||||||
>
|
>
|
||||||
{};
|
{};
|
||||||
|
/*
|
||||||
/////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
// //
|
// //
|
||||||
// Implementation of the rbtree iterator //
|
// Implementation of the rbtree iterator //
|
||||||
@@ -291,7 +292,7 @@ class rbtree_iterator
|
|||||||
node_ptr nodeptr_;
|
node_ptr nodeptr_;
|
||||||
} members_;
|
} members_;
|
||||||
};
|
};
|
||||||
|
*/
|
||||||
} //namespace intrusive
|
} //namespace intrusive
|
||||||
} //namespace boost
|
} //namespace boost
|
||||||
|
|
||||||
|
1332
include/boost/intrusive/detail/tree_algorithms.hpp
Normal file
1332
include/boost/intrusive/detail/tree_algorithms.hpp
Normal file
File diff suppressed because it is too large
Load Diff
192
include/boost/intrusive/detail/tree_node.hpp
Normal file
192
include/boost/intrusive/detail/tree_node.hpp
Normal file
@@ -0,0 +1,192 @@
|
|||||||
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// (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_TREE_NODE_HPP
|
||||||
|
#define BOOST_INTRUSIVE_TREE_NODE_HPP
|
||||||
|
|
||||||
|
#include <boost/intrusive/detail/config_begin.hpp>
|
||||||
|
#include <iterator>
|
||||||
|
#include <boost/intrusive/detail/pointer_to_other.hpp>
|
||||||
|
#include <boost/intrusive/detail/mpl.hpp>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace intrusive {
|
||||||
|
|
||||||
|
template<class VoidPointer>
|
||||||
|
struct tree_node
|
||||||
|
{
|
||||||
|
typedef typename pointer_to_other
|
||||||
|
<VoidPointer
|
||||||
|
,tree_node<VoidPointer> >::type node_ptr;
|
||||||
|
|
||||||
|
node_ptr parent_, left_, right_;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class VoidPointer>
|
||||||
|
struct tree_node_traits
|
||||||
|
{
|
||||||
|
typedef tree_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;
|
||||||
|
|
||||||
|
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; }
|
||||||
|
};
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
// //
|
||||||
|
// Implementation of the tree iterator //
|
||||||
|
// //
|
||||||
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// tree_iterator provides some basic functions for a
|
||||||
|
// node oriented bidirectional iterator:
|
||||||
|
template<class Container, bool IsConst>
|
||||||
|
class tree_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 Container::node_algorithms node_algorithms;
|
||||||
|
typedef typename real_value_traits::node_traits node_traits;
|
||||||
|
typedef typename node_traits::node node;
|
||||||
|
typedef typename node_traits::node_ptr node_ptr;
|
||||||
|
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;
|
||||||
|
|
||||||
|
tree_iterator()
|
||||||
|
: members_ (0, 0)
|
||||||
|
{}
|
||||||
|
|
||||||
|
explicit tree_iterator(node_ptr node, const Container *cont_ptr)
|
||||||
|
: members_ (node, cont_ptr)
|
||||||
|
{}
|
||||||
|
|
||||||
|
tree_iterator(tree_iterator<Container, false> const& other)
|
||||||
|
: members_(other.pointed_node(), other.get_container())
|
||||||
|
{}
|
||||||
|
|
||||||
|
const node_ptr &pointed_node() const
|
||||||
|
{ return members_.nodeptr_; }
|
||||||
|
|
||||||
|
tree_iterator &operator=(const node_ptr &node)
|
||||||
|
{ members_.nodeptr_ = node; return static_cast<tree_iterator&>(*this); }
|
||||||
|
|
||||||
|
public:
|
||||||
|
tree_iterator& operator++()
|
||||||
|
{
|
||||||
|
members_.nodeptr_ = node_algorithms::next_node(members_.nodeptr_);
|
||||||
|
return static_cast<tree_iterator&> (*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
tree_iterator operator++(int)
|
||||||
|
{
|
||||||
|
tree_iterator result (*this);
|
||||||
|
members_.nodeptr_ = node_algorithms::next_node(members_.nodeptr_);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
tree_iterator& operator--()
|
||||||
|
{
|
||||||
|
members_.nodeptr_ = node_algorithms::prev_node(members_.nodeptr_);
|
||||||
|
return static_cast<tree_iterator&> (*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
tree_iterator operator--(int)
|
||||||
|
{
|
||||||
|
tree_iterator result (*this);
|
||||||
|
members_.nodeptr_ = node_algorithms::prev_node(members_.nodeptr_);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator== (const tree_iterator& i) const
|
||||||
|
{ return members_.nodeptr_ == i.pointed_node(); }
|
||||||
|
|
||||||
|
bool operator!= (const tree_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 boost
|
||||||
|
|
||||||
|
#include <boost/intrusive/detail/config_end.hpp>
|
||||||
|
|
||||||
|
#endif //BOOST_INTRUSIVE_TREE_NODE_HPP
|
@@ -42,13 +42,13 @@ struct internal_base_hook_bool
|
|||||||
template <class U> static one test(...);
|
template <class U> static one test(...);
|
||||||
template <class U> static two_or_three<U::boost_intrusive_tags::is_base_hook>
|
template <class U> static two_or_three<U::boost_intrusive_tags::is_base_hook>
|
||||||
test (detail::bool_<U::boost_intrusive_tags::is_base_hook>* = 0);
|
test (detail::bool_<U::boost_intrusive_tags::is_base_hook>* = 0);
|
||||||
static const int value = sizeof(test<T>(0));
|
static const std::size_t value = sizeof(test<T>(0));
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
struct internal_base_hook_bool_is_true
|
struct internal_base_hook_bool_is_true
|
||||||
{
|
{
|
||||||
static const bool value = internal_base_hook_bool<T>::value == 3;
|
static const bool value = internal_base_hook_bool<T>::value > sizeof(one)*2;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
@@ -59,7 +59,7 @@ struct external_value_traits_bool
|
|||||||
template <class U> static one test(...);
|
template <class U> static one test(...);
|
||||||
template <class U> static two_or_three<U::external_value_traits>
|
template <class U> static two_or_three<U::external_value_traits>
|
||||||
test (detail::bool_<U::external_value_traits>* = 0);
|
test (detail::bool_<U::external_value_traits>* = 0);
|
||||||
static const int value = sizeof(test<T>(0));
|
static const std::size_t value = sizeof(test<T>(0));
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
@@ -70,13 +70,13 @@ struct external_bucket_traits_bool
|
|||||||
template <class U> static one test(...);
|
template <class U> static one test(...);
|
||||||
template <class U> static two_or_three<U::external_bucket_traits>
|
template <class U> static two_or_three<U::external_bucket_traits>
|
||||||
test (detail::bool_<U::external_bucket_traits>* = 0);
|
test (detail::bool_<U::external_bucket_traits>* = 0);
|
||||||
static const int value = sizeof(test<T>(0));
|
static const std::size_t value = sizeof(test<T>(0));
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
struct external_value_traits_is_true
|
struct external_value_traits_is_true
|
||||||
{
|
{
|
||||||
static const bool value = external_value_traits_bool<T>::value == 3;
|
static const bool value = external_value_traits_bool<T>::value > sizeof(one)*2;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class Node, class Tag, link_mode_type LinkMode, int>
|
template<class Node, class Tag, link_mode_type LinkMode, int>
|
||||||
|
@@ -141,6 +141,7 @@ template
|
|||||||
< class O1 = none
|
< class O1 = none
|
||||||
, class O2 = none
|
, class O2 = none
|
||||||
, class O3 = none
|
, class O3 = none
|
||||||
|
, class O4 = none
|
||||||
>
|
>
|
||||||
class set_base_hook;
|
class set_base_hook;
|
||||||
|
|
||||||
@@ -148,9 +149,52 @@ template
|
|||||||
< class O1 = none
|
< class O1 = none
|
||||||
, class O2 = none
|
, class O2 = none
|
||||||
, class O3 = none
|
, class O3 = none
|
||||||
|
, class O4 = none
|
||||||
>
|
>
|
||||||
class set_member_hook;
|
class set_member_hook;
|
||||||
|
|
||||||
|
//splaytree/splay_set/splay_multiset
|
||||||
|
template
|
||||||
|
< class T
|
||||||
|
, class O1 = none
|
||||||
|
, class O2 = none
|
||||||
|
, class O3 = none
|
||||||
|
, class O4 = none
|
||||||
|
>
|
||||||
|
class splaytree;
|
||||||
|
|
||||||
|
template
|
||||||
|
< class T
|
||||||
|
, class O1 = none
|
||||||
|
, class O2 = none
|
||||||
|
, class O3 = none
|
||||||
|
, class O4 = none
|
||||||
|
>
|
||||||
|
class splay_set;
|
||||||
|
|
||||||
|
template
|
||||||
|
< class T
|
||||||
|
, class O1 = none
|
||||||
|
, class O2 = none
|
||||||
|
, class O3 = none
|
||||||
|
, class O4 = none
|
||||||
|
>
|
||||||
|
class splay_multiset;
|
||||||
|
|
||||||
|
template
|
||||||
|
< class O1 = none
|
||||||
|
, class O2 = none
|
||||||
|
, class O3 = none
|
||||||
|
>
|
||||||
|
class splay_set_base_hook;
|
||||||
|
|
||||||
|
template
|
||||||
|
< class O1 = none
|
||||||
|
, class O2 = none
|
||||||
|
, class O3 = none
|
||||||
|
>
|
||||||
|
class splay_set_member_hook;
|
||||||
|
|
||||||
//hash/unordered
|
//hash/unordered
|
||||||
//rbtree/set/multiset
|
//rbtree/set/multiset
|
||||||
template
|
template
|
||||||
|
@@ -282,14 +282,14 @@ struct void_pointer
|
|||||||
//!the tag of a base hook. A type can not have two
|
//!the tag of a base hook. A type can not have two
|
||||||
//!base hooks of the same type, so a tag can be used
|
//!base hooks of the same type, so a tag can be used
|
||||||
//!to differentiate two base hooks with otherwise same type
|
//!to differentiate two base hooks with otherwise same type
|
||||||
template<class BaseTag>
|
template<class Tag>
|
||||||
struct tag
|
struct tag
|
||||||
{
|
{
|
||||||
/// @cond
|
/// @cond
|
||||||
template<class Base>
|
template<class Base>
|
||||||
struct pack : Base
|
struct pack : Base
|
||||||
{
|
{
|
||||||
typedef BaseTag tag;
|
typedef Tag tag;
|
||||||
};
|
};
|
||||||
/// @endcond
|
/// @endcond
|
||||||
};
|
};
|
||||||
@@ -310,6 +310,22 @@ struct link_mode
|
|||||||
/// @endcond
|
/// @endcond
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//!This option setter specifies the type of
|
||||||
|
//!a void pointer. This will instruct the hook
|
||||||
|
//!to use this type of pointer instead of the
|
||||||
|
//!default one
|
||||||
|
template<bool Enabled>
|
||||||
|
struct optimize_size
|
||||||
|
{
|
||||||
|
/// @cond
|
||||||
|
template<class Base>
|
||||||
|
struct pack : Base
|
||||||
|
{
|
||||||
|
static const bool optimize_size = Enabled;
|
||||||
|
};
|
||||||
|
/// @endcond
|
||||||
|
};
|
||||||
|
|
||||||
//!This option setter specifies the bucket traits
|
//!This option setter specifies the bucket traits
|
||||||
//!class for unordered associative containers. When this option is specified,
|
//!class for unordered associative containers. When this option is specified,
|
||||||
//!instead of using the default bucket traits, a user defined holder will be defined
|
//!instead of using the default bucket traits, a user defined holder will be defined
|
||||||
@@ -416,6 +432,7 @@ struct hook_defaults
|
|||||||
, void_pointer<void*>
|
, void_pointer<void*>
|
||||||
, link_mode<safe_link>
|
, link_mode<safe_link>
|
||||||
, tag<default_tag>
|
, tag<default_tag>
|
||||||
|
, optimize_size<false>
|
||||||
>::type
|
>::type
|
||||||
{};
|
{};
|
||||||
|
|
||||||
|
@@ -44,7 +44,10 @@ struct has_pointer_plus_bit<void*, N>
|
|||||||
//!has_pointer_plus_bit<>::value is non-zero can make use of these
|
//!has_pointer_plus_bit<>::value is non-zero can make use of these
|
||||||
//!operations to embed the bit in the pointer.
|
//!operations to embed the bit in the pointer.
|
||||||
template<class Pointer>
|
template<class Pointer>
|
||||||
struct pointer_plus_bit;
|
struct pointer_plus_bit
|
||||||
|
{
|
||||||
|
static const bool value = false;
|
||||||
|
};
|
||||||
|
|
||||||
//!This is the specialization to embed an extra bit of information
|
//!This is the specialization to embed an extra bit of information
|
||||||
//!in a raw pointer. The extra bit is stored in the lower bit of the pointer.
|
//!in a raw pointer. The extra bit is stored in the lower bit of the pointer.
|
||||||
|
@@ -13,22 +13,23 @@
|
|||||||
#define BOOST_INTRUSIVE_RBTREE_HPP
|
#define BOOST_INTRUSIVE_RBTREE_HPP
|
||||||
|
|
||||||
#include <boost/intrusive/detail/config_begin.hpp>
|
#include <boost/intrusive/detail/config_begin.hpp>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cstddef>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
#include <boost/intrusive/detail/assert.hpp>
|
#include <boost/intrusive/detail/assert.hpp>
|
||||||
#include <boost/static_assert.hpp>
|
#include <boost/static_assert.hpp>
|
||||||
#include <boost/intrusive/intrusive_fwd.hpp>
|
#include <boost/intrusive/intrusive_fwd.hpp>
|
||||||
#include <boost/intrusive/detail/pointer_to_other.hpp>
|
|
||||||
#include <boost/intrusive/set_hook.hpp>
|
#include <boost/intrusive/set_hook.hpp>
|
||||||
#include <boost/intrusive/detail/rbtree_node.hpp>
|
#include <boost/intrusive/detail/rbtree_node.hpp>
|
||||||
|
#include <boost/intrusive/detail/tree_node.hpp>
|
||||||
#include <boost/intrusive/detail/ebo_functor_holder.hpp>
|
#include <boost/intrusive/detail/ebo_functor_holder.hpp>
|
||||||
|
#include <boost/intrusive/detail/pointer_to_other.hpp>
|
||||||
#include <boost/intrusive/options.hpp>
|
#include <boost/intrusive/options.hpp>
|
||||||
#include <boost/intrusive/rbtree_algorithms.hpp>
|
#include <boost/intrusive/rbtree_algorithms.hpp>
|
||||||
#include <boost/intrusive/link_mode.hpp>
|
#include <boost/intrusive/link_mode.hpp>
|
||||||
#include <cstddef>
|
|
||||||
#include <iterator>
|
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
namespace boost {
|
namespace boost {
|
||||||
namespace intrusive {
|
namespace intrusive {
|
||||||
@@ -78,7 +79,7 @@ struct set_defaults
|
|||||||
/// @endcond
|
/// @endcond
|
||||||
|
|
||||||
//! The class template rbtree is an intrusive red-black tree container, that
|
//! The class template rbtree is an intrusive red-black tree container, that
|
||||||
//! is used to construct intrusive set and tree containers. The no-throw
|
//! is used to construct intrusive set and multiset containers. The no-throw
|
||||||
//! guarantee holds only, if the value_compare object
|
//! guarantee holds only, if the value_compare object
|
||||||
//! doesn't throw.
|
//! doesn't throw.
|
||||||
//!
|
//!
|
||||||
@@ -118,11 +119,11 @@ class rbtree_impl
|
|||||||
typedef typename Config::size_type size_type;
|
typedef typename Config::size_type size_type;
|
||||||
typedef typename Config::compare value_compare;
|
typedef typename Config::compare value_compare;
|
||||||
typedef value_compare key_compare;
|
typedef value_compare key_compare;
|
||||||
typedef rbtree_iterator<rbtree_impl, false> iterator;
|
typedef tree_iterator<rbtree_impl, false> iterator;
|
||||||
typedef rbtree_iterator<rbtree_impl, true> const_iterator;
|
typedef tree_iterator<rbtree_impl, true> const_iterator;
|
||||||
typedef std::reverse_iterator<iterator> reverse_iterator;
|
typedef std::reverse_iterator<iterator> reverse_iterator;
|
||||||
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
|
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
|
||||||
typedef typename real_value_traits::node_traits node_traits;
|
typedef typename real_value_traits::node_traits node_traits;
|
||||||
typedef typename node_traits::node node;
|
typedef typename node_traits::node node;
|
||||||
typedef typename boost::pointer_to_other
|
typedef typename boost::pointer_to_other
|
||||||
<pointer, node>::type node_ptr;
|
<pointer, node>::type node_ptr;
|
||||||
@@ -235,7 +236,7 @@ class rbtree_impl
|
|||||||
//! [b, e).
|
//! [b, e).
|
||||||
//!
|
//!
|
||||||
//! <b>Complexity</b>: Linear in N if [b, e) is already sorted using
|
//! <b>Complexity</b>: Linear in N if [b, e) is already sorted using
|
||||||
//! comp and otherwise N * log N, where N is last <20> first.
|
//! comp and otherwise N * log N, where N is the distance between first and last.
|
||||||
//!
|
//!
|
||||||
//! <b>Throws</b>: Nothing unless the copy constructor of the value_compare object throws.
|
//! <b>Throws</b>: Nothing unless the copy constructor of the value_compare object throws.
|
||||||
template<class Iterator>
|
template<class Iterator>
|
||||||
@@ -411,8 +412,12 @@ class rbtree_impl
|
|||||||
{
|
{
|
||||||
if(constant_time_size)
|
if(constant_time_size)
|
||||||
return this->priv_size_traits().get_size();
|
return this->priv_size_traits().get_size();
|
||||||
else
|
else{
|
||||||
return empty() ? 0 : node_algorithms::count(node_traits::get_parent(const_node_ptr(&priv_header())));
|
const_iterator beg(this->cbegin()), end(this->cend());
|
||||||
|
size_type i = 0;
|
||||||
|
for(;beg != end; ++beg) ++i;
|
||||||
|
return i;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//! <b>Effects</b>: Swaps the contents of two multisets.
|
//! <b>Effects</b>: Swaps the contents of two multisets.
|
||||||
@@ -445,7 +450,7 @@ class rbtree_impl
|
|||||||
//!
|
//!
|
||||||
//! <b>Note</b>: Does not affect the validity of iterators and references.
|
//! <b>Note</b>: Does not affect the validity of iterators and references.
|
||||||
//! No copy-constructors are called.
|
//! No copy-constructors are called.
|
||||||
iterator insert_equal_upper_bound(reference value)
|
iterator insert_equal(reference value)
|
||||||
{
|
{
|
||||||
detail::key_nodeptr_comp<value_compare, rbtree_impl>
|
detail::key_nodeptr_comp<value_compare, rbtree_impl>
|
||||||
key_node_comp(priv_comp(), this);
|
key_node_comp(priv_comp(), this);
|
||||||
@@ -457,29 +462,6 @@ class rbtree_impl
|
|||||||
(node_ptr(&priv_header()), to_insert, key_node_comp), this);
|
(node_ptr(&priv_header()), to_insert, key_node_comp), this);
|
||||||
}
|
}
|
||||||
|
|
||||||
//! <b>Requires</b>: value must be an lvalue
|
|
||||||
//!
|
|
||||||
//! <b>Effects</b>: Inserts value into the tree before the lower bound.
|
|
||||||
//!
|
|
||||||
//! <b>Complexity</b>: Average complexity for insert element is at
|
|
||||||
//! most logarithmic.
|
|
||||||
//!
|
|
||||||
//! <b>Throws</b>: Nothing.
|
|
||||||
//!
|
|
||||||
//! <b>Note</b>: Does not affect the validity of iterators and references.
|
|
||||||
//! No copy-constructors are called.
|
|
||||||
iterator insert_equal_lower_bound(reference value)
|
|
||||||
{
|
|
||||||
detail::key_nodeptr_comp<value_compare, rbtree_impl>
|
|
||||||
key_node_comp(priv_comp(), this);
|
|
||||||
node_ptr to_insert(get_real_value_traits().to_node_ptr(value));
|
|
||||||
if(safemode_or_autounlink)
|
|
||||||
BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert));
|
|
||||||
this->priv_size_traits().increment();
|
|
||||||
return iterator(node_algorithms::insert_equal_lower_bound
|
|
||||||
(node_ptr(&priv_header()), to_insert, key_node_comp), this);
|
|
||||||
}
|
|
||||||
|
|
||||||
//! <b>Requires</b>: value must be an lvalue, and "hint" must be
|
//! <b>Requires</b>: value must be an lvalue, and "hint" must be
|
||||||
//! a valid iterator.
|
//! a valid iterator.
|
||||||
//!
|
//!
|
||||||
@@ -523,15 +505,9 @@ class rbtree_impl
|
|||||||
template<class Iterator>
|
template<class Iterator>
|
||||||
void insert_equal(Iterator b, Iterator e)
|
void insert_equal(Iterator b, Iterator e)
|
||||||
{
|
{
|
||||||
if(this->empty()){
|
iterator end(this->end());
|
||||||
iterator end(this->end());
|
for (; b != e; ++b)
|
||||||
for (; b != e; ++b)
|
this->insert_equal(end, *b);
|
||||||
this->insert_equal(end, *b);
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
for (; b != e; ++b)
|
|
||||||
this->insert_equal_upper_bound(*b);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//! <b>Requires</b>: value must be an lvalue
|
//! <b>Requires</b>: value must be an lvalue
|
||||||
@@ -1059,6 +1035,19 @@ class rbtree_impl
|
|||||||
return std::pair<const_iterator, const_iterator>(const_iterator(ret.first, this), const_iterator(ret.second, this));
|
return std::pair<const_iterator, const_iterator>(const_iterator(ret.first, this), const_iterator(ret.second, this));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
|
||||||
|
//!
|
||||||
|
//! <b>Effects</b>: Erases all the elements from *this
|
||||||
|
//! calling Disposer::operator()(pointer), clones all the
|
||||||
|
//! elements from src calling Cloner::operator()(const_reference )
|
||||||
|
//! and inserts them on *this.
|
||||||
|
//!
|
||||||
|
//! If cloner throws, all cloned elements are unlinked and disposed
|
||||||
|
//! calling Disposer::operator()(pointer).
|
||||||
|
//!
|
||||||
|
//! <b>Complexity</b>: Linear to erased plus inserted elements.
|
||||||
|
//!
|
||||||
|
//! <b>Throws</b>: If cloner throws.
|
||||||
template <class Cloner, class Disposer>
|
template <class Cloner, class Disposer>
|
||||||
void clone_from(const rbtree_impl &src, Cloner cloner, Disposer disposer)
|
void clone_from(const rbtree_impl &src, Cloner cloner, Disposer disposer)
|
||||||
{
|
{
|
||||||
@@ -1073,6 +1062,16 @@ class rbtree_impl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//! <b>Effects</b>: Unlinks the leftmost node from the tree.
|
||||||
|
//!
|
||||||
|
//! <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.
|
||||||
pointer unlink_leftmost_without_rebalance()
|
pointer unlink_leftmost_without_rebalance()
|
||||||
{
|
{
|
||||||
node_ptr to_be_disposed(node_algorithms::unlink_leftmost_without_rebalance
|
node_ptr to_be_disposed(node_algorithms::unlink_leftmost_without_rebalance
|
||||||
@@ -1132,7 +1131,7 @@ class rbtree_impl
|
|||||||
//!
|
//!
|
||||||
//! <b>Complexity</b>: Constant.
|
//! <b>Complexity</b>: Constant.
|
||||||
//!
|
//!
|
||||||
//! <b>Throws</b>: Nothing.<EFBFBD>
|
//! <b>Throws</b>: Nothing.
|
||||||
//!
|
//!
|
||||||
//! <b>Note</b>: This static function is available only if the <i>value traits</i>
|
//! <b>Note</b>: This static function is available only if the <i>value traits</i>
|
||||||
//! is stateless.
|
//! is stateless.
|
||||||
@@ -1266,14 +1265,14 @@ bool operator==
|
|||||||
{
|
{
|
||||||
typedef rbtree_impl<Config> tree_type;
|
typedef rbtree_impl<Config> tree_type;
|
||||||
typedef typename tree_type::const_iterator const_iterator;
|
typedef typename tree_type::const_iterator const_iterator;
|
||||||
const bool CS = tree_type::constant_time_size;
|
|
||||||
if(CS && x.size() != y.size()){
|
if(tree_type::constant_time_size && x.size() != y.size()){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const_iterator end1 = x.end();
|
const_iterator end1 = x.end();
|
||||||
const_iterator i1 = x.begin();
|
const_iterator i1 = x.begin();
|
||||||
const_iterator i2 = y.begin();
|
const_iterator i2 = y.begin();
|
||||||
if(CS){
|
if(tree_type::constant_time_size){
|
||||||
while (i1 != end1 && *i1 == *i2) {
|
while (i1 != end1 && *i1 == *i2) {
|
||||||
++i1;
|
++i1;
|
||||||
++i2;
|
++i2;
|
||||||
|
File diff suppressed because it is too large
Load Diff
@@ -109,7 +109,7 @@ class set_impl
|
|||||||
, const value_compare &cmp = value_compare()
|
, const value_compare &cmp = value_compare()
|
||||||
, const value_traits &v_traits = value_traits())
|
, const value_traits &v_traits = value_traits())
|
||||||
: tree_(true, b, e, cmp, v_traits)
|
: tree_(true, b, e, cmp, v_traits)
|
||||||
{ insert(b, e); }
|
{}
|
||||||
|
|
||||||
//! <b>Effects</b>: Detaches all elements from this. The objects in the set
|
//! <b>Effects</b>: Detaches all elements from this. The objects in the set
|
||||||
//! are not deleted (i.e. no destructors are called).
|
//! are not deleted (i.e. no destructors are called).
|
||||||
@@ -930,6 +930,19 @@ class set_impl
|
|||||||
static void init_node(reference value)
|
static void init_node(reference value)
|
||||||
{ tree_type::init_node(value); }
|
{ tree_type::init_node(value); }
|
||||||
|
|
||||||
|
//! <b>Effects</b>: Unlinks the leftmost node from the tree.
|
||||||
|
//!
|
||||||
|
//! <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.
|
||||||
|
pointer unlink_leftmost_without_rebalance()
|
||||||
|
{ return tree_.unlink_leftmost_without_rebalance(); }
|
||||||
|
|
||||||
//! <b>Requires</b>: replace_this must be a valid iterator of *this
|
//! <b>Requires</b>: replace_this must be a valid iterator of *this
|
||||||
//! and with_this must not be inserted in any tree.
|
//! and with_this must not be inserted in any tree.
|
||||||
//!
|
//!
|
||||||
@@ -1151,7 +1164,7 @@ class multiset_impl
|
|||||||
//! [b, e).
|
//! [b, e).
|
||||||
//!
|
//!
|
||||||
//! <b>Complexity</b>: Linear in N if [b, e) is already sorted using
|
//! <b>Complexity</b>: Linear in N if [b, e) is already sorted using
|
||||||
//! comp and otherwise N * log N, where N is last <20> first.
|
//! comp and otherwise N * log N, where N is the distance between first and last
|
||||||
//!
|
//!
|
||||||
//! <b>Throws</b>: If value_traits::node_traits::node
|
//! <b>Throws</b>: If value_traits::node_traits::node
|
||||||
//! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
|
//! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
|
||||||
@@ -1379,7 +1392,7 @@ class multiset_impl
|
|||||||
//! <b>Note</b>: Does not affect the validity of iterators and references.
|
//! <b>Note</b>: Does not affect the validity of iterators and references.
|
||||||
//! No copy-constructors are called.
|
//! No copy-constructors are called.
|
||||||
iterator insert(reference value)
|
iterator insert(reference value)
|
||||||
{ return tree_.insert_equal_upper_bound(value); }
|
{ return tree_.insert_equal(value); }
|
||||||
|
|
||||||
//! <b>Requires</b>: value must be an lvalue
|
//! <b>Requires</b>: value must be an lvalue
|
||||||
//!
|
//!
|
||||||
@@ -1889,6 +1902,19 @@ class multiset_impl
|
|||||||
static void init_node(reference value)
|
static void init_node(reference value)
|
||||||
{ tree_type::init_node(value); }
|
{ tree_type::init_node(value); }
|
||||||
|
|
||||||
|
//! <b>Effects</b>: Unlinks the leftmost node from the tree.
|
||||||
|
//!
|
||||||
|
//! <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.
|
||||||
|
pointer unlink_leftmost_without_rebalance()
|
||||||
|
{ return tree_.unlink_leftmost_without_rebalance(); }
|
||||||
|
|
||||||
//! <b>Requires</b>: replace_this must be a valid iterator of *this
|
//! <b>Requires</b>: replace_this must be a valid iterator of *this
|
||||||
//! and with_this must not be inserted in any tree.
|
//! and with_this must not be inserted in any tree.
|
||||||
//!
|
//!
|
||||||
|
@@ -26,10 +26,10 @@ namespace boost {
|
|||||||
namespace intrusive {
|
namespace intrusive {
|
||||||
|
|
||||||
/// @cond
|
/// @cond
|
||||||
template<class VoidPointer>
|
template<class VoidPointer, bool OptimizeSize = false>
|
||||||
struct get_set_node_algo
|
struct get_set_node_algo
|
||||||
{
|
{
|
||||||
typedef rbtree_algorithms<rbtree_node_traits<VoidPointer> > type;
|
typedef rbtree_algorithms<rbtree_node_traits<VoidPointer, OptimizeSize> > type;
|
||||||
};
|
};
|
||||||
/// @endcond
|
/// @endcond
|
||||||
|
|
||||||
@@ -38,16 +38,17 @@ struct get_set_node_algo
|
|||||||
#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
||||||
template<class ...Options>
|
template<class ...Options>
|
||||||
#else
|
#else
|
||||||
template<class O1 = none, class O2 = none, class O3 = none>
|
template<class O1 = none, class O2 = none, class O3 = none, class O4 = none>
|
||||||
#endif
|
#endif
|
||||||
struct make_set_base_hook
|
struct make_set_base_hook
|
||||||
{
|
{
|
||||||
/// @cond
|
/// @cond
|
||||||
typedef typename pack_options
|
typedef typename pack_options
|
||||||
< hook_defaults, O1, O2, O3>::type packed_options;
|
< hook_defaults, O1, O2, O3, O4>::type packed_options;
|
||||||
|
|
||||||
typedef detail::generic_hook
|
typedef detail::generic_hook
|
||||||
< get_set_node_algo<typename packed_options::void_pointer>
|
< get_set_node_algo<typename packed_options::void_pointer
|
||||||
|
,packed_options::optimize_size>
|
||||||
, typename packed_options::tag
|
, typename packed_options::tag
|
||||||
, packed_options::link_mode
|
, packed_options::link_mode
|
||||||
, detail::SetBaseHook
|
, detail::SetBaseHook
|
||||||
@@ -72,10 +73,10 @@ struct make_set_base_hook
|
|||||||
#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
||||||
template<class ...Options>
|
template<class ...Options>
|
||||||
#else
|
#else
|
||||||
template<class O1, class O2, class O3>
|
template<class O1, class O2, class O3, class O4>
|
||||||
#endif
|
#endif
|
||||||
class set_base_hook
|
class set_base_hook
|
||||||
: public make_set_base_hook<O1, O2, O3>::type
|
: public make_set_base_hook<O1, O2, O3, O4>::type
|
||||||
{
|
{
|
||||||
#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
||||||
//! <b>Effects</b>: If link_mode is \c auto_unlink or \c safe_link
|
//! <b>Effects</b>: If link_mode is \c auto_unlink or \c safe_link
|
||||||
@@ -149,16 +150,17 @@ class set_base_hook
|
|||||||
#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
||||||
template<class ...Options>
|
template<class ...Options>
|
||||||
#else
|
#else
|
||||||
template<class O1 = none, class O2 = none, class O3 = none>
|
template<class O1 = none, class O2 = none, class O3 = none, class O4 = none>
|
||||||
#endif
|
#endif
|
||||||
struct make_set_member_hook
|
struct make_set_member_hook
|
||||||
{
|
{
|
||||||
/// @cond
|
/// @cond
|
||||||
typedef typename pack_options
|
typedef typename pack_options
|
||||||
< hook_defaults, O1, O2, O3>::type packed_options;
|
< hook_defaults, O1, O2, O3, O4>::type packed_options;
|
||||||
|
|
||||||
typedef detail::generic_hook
|
typedef detail::generic_hook
|
||||||
< get_set_node_algo<typename packed_options::void_pointer>
|
< get_set_node_algo<typename packed_options::void_pointer
|
||||||
|
,packed_options::optimize_size>
|
||||||
, member_tag
|
, member_tag
|
||||||
, packed_options::link_mode
|
, packed_options::link_mode
|
||||||
, detail::NoBaseHook
|
, detail::NoBaseHook
|
||||||
@@ -178,10 +180,10 @@ struct make_set_member_hook
|
|||||||
#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
||||||
template<class ...Options>
|
template<class ...Options>
|
||||||
#else
|
#else
|
||||||
template<class O1, class O2, class O3>
|
template<class O1, class O2, class O3, class O4>
|
||||||
#endif
|
#endif
|
||||||
class set_member_hook
|
class set_member_hook
|
||||||
: public make_set_member_hook<O1, O2, O3>::type
|
: public make_set_member_hook<O1, O2, O3, O4>::type
|
||||||
{
|
{
|
||||||
#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
||||||
//! <b>Effects</b>: If link_mode is \c auto_unlink or \c safe_link
|
//! <b>Effects</b>: If link_mode is \c auto_unlink or \c safe_link
|
||||||
|
@@ -24,6 +24,7 @@
|
|||||||
#include <boost/intrusive/detail/pointer_to_other.hpp>
|
#include <boost/intrusive/detail/pointer_to_other.hpp>
|
||||||
#include <boost/intrusive/link_mode.hpp>
|
#include <boost/intrusive/link_mode.hpp>
|
||||||
#include <boost/intrusive/options.hpp>
|
#include <boost/intrusive/options.hpp>
|
||||||
|
#include <iterator>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
@@ -906,7 +907,7 @@ class slist_impl
|
|||||||
void dispose_and_assign(Disposer disposer, Iterator b, Iterator e)
|
void dispose_and_assign(Disposer disposer, Iterator b, Iterator e)
|
||||||
{
|
{
|
||||||
this->clear_and_dispose(disposer);
|
this->clear_and_dispose(disposer);
|
||||||
this->insert_after(before_begin(), b, e, disposer);
|
this->insert_after(this->before_begin(), b, e, disposer);
|
||||||
}
|
}
|
||||||
|
|
||||||
//! <b>Requires</b>: prev is an iterator to an element or x.end()/x.before_begin() in x.
|
//! <b>Requires</b>: prev is an iterator to an element or x.end()/x.before_begin() in x.
|
||||||
|
2183
include/boost/intrusive/splay_set.hpp
Normal file
2183
include/boost/intrusive/splay_set.hpp
Normal file
File diff suppressed because it is too large
Load Diff
257
include/boost/intrusive/splay_set_hook.hpp
Normal file
257
include/boost/intrusive/splay_set_hook.hpp
Normal file
@@ -0,0 +1,257 @@
|
|||||||
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// (C) Copyright Olaf Krzikalla 2004-2006.
|
||||||
|
// (C) Copyright Ion Gaztanaga 2006-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_SPLAY_SET_HOOK_HPP
|
||||||
|
#define BOOST_INTRUSIVE_SPLAY_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/tree_node.hpp>
|
||||||
|
#include <boost/intrusive/splaytree_algorithms.hpp>
|
||||||
|
#include <boost/intrusive/options.hpp>
|
||||||
|
#include <boost/intrusive/detail/generic_hook.hpp>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace intrusive {
|
||||||
|
|
||||||
|
/// @cond
|
||||||
|
template<class VoidPointer>
|
||||||
|
struct get_splay_set_node_algo
|
||||||
|
{
|
||||||
|
typedef splaytree_algorithms<tree_node_traits<VoidPointer> > type;
|
||||||
|
};
|
||||||
|
/// @endcond
|
||||||
|
|
||||||
|
//! Helper metafunction to define a \c splay_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>
|
||||||
|
#endif
|
||||||
|
struct make_splay_set_base_hook
|
||||||
|
{
|
||||||
|
/// @cond
|
||||||
|
typedef typename pack_options
|
||||||
|
< hook_defaults, O1, O2, O3>::type packed_options;
|
||||||
|
|
||||||
|
typedef detail::generic_hook
|
||||||
|
< get_splay_set_node_algo<typename packed_options::void_pointer>
|
||||||
|
, typename packed_options::tag
|
||||||
|
, packed_options::link_mode
|
||||||
|
, detail::SplaySetBaseHook
|
||||||
|
> implementation_defined;
|
||||||
|
/// @endcond
|
||||||
|
typedef implementation_defined type;
|
||||||
|
};
|
||||||
|
|
||||||
|
//! Derive a class from splay_set_base_hook in order to store objects in
|
||||||
|
//! in an set/multiset. splay_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 splay_set_base_hook, then each splay_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>
|
||||||
|
#endif
|
||||||
|
class splay_set_base_hook
|
||||||
|
: public make_splay_set_base_hook<O1, O2, O3>::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.
|
||||||
|
splay_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.
|
||||||
|
splay_set_base_hook(const splay_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.
|
||||||
|
splay_set_base_hook& operator=(const splay_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.
|
||||||
|
~splay_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(splay_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 splay_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>
|
||||||
|
#endif
|
||||||
|
struct make_splay_set_member_hook
|
||||||
|
{
|
||||||
|
/// @cond
|
||||||
|
typedef typename pack_options
|
||||||
|
< hook_defaults, O1, O2, O3>::type packed_options;
|
||||||
|
|
||||||
|
typedef detail::generic_hook
|
||||||
|
< get_splay_set_node_algo<typename packed_options::void_pointer>
|
||||||
|
, member_tag
|
||||||
|
, packed_options::link_mode
|
||||||
|
, detail::NoBaseHook
|
||||||
|
> implementation_defined;
|
||||||
|
/// @endcond
|
||||||
|
typedef implementation_defined type;
|
||||||
|
};
|
||||||
|
|
||||||
|
//! Put a public data member splay_set_member_hook in order to store objects of this class in
|
||||||
|
//! an set/multiset. splay_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>
|
||||||
|
#endif
|
||||||
|
class splay_set_member_hook
|
||||||
|
: public make_splay_set_member_hook<O1, O2, O3>::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.
|
||||||
|
splay_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.
|
||||||
|
splay_set_member_hook(const splay_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.
|
||||||
|
splay_set_member_hook& operator=(const splay_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.
|
||||||
|
~splay_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(splay_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_SPLAY_SET_HOOK_HPP
|
1501
include/boost/intrusive/splaytree.hpp
Normal file
1501
include/boost/intrusive/splaytree.hpp
Normal file
File diff suppressed because it is too large
Load Diff
828
include/boost/intrusive/splaytree_algorithms.hpp
Normal file
828
include/boost/intrusive/splaytree_algorithms.hpp
Normal file
@@ -0,0 +1,828 @@
|
|||||||
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// (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.
|
||||||
|
//
|
||||||
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
// The implementation of splay trees is based on the article and code published
|
||||||
|
// in C++ Users Journal "Implementing Splay Trees in C++" (September 1, 2005).
|
||||||
|
//
|
||||||
|
// The code has been modified and (supposely) improved by Ion Gaztanaga.
|
||||||
|
// Here is the header of the file used as base code:
|
||||||
|
//
|
||||||
|
// splay_tree.h -- implementation of a STL complatible splay tree.
|
||||||
|
//
|
||||||
|
// Copyright (c) 2004 Ralf Mattethat
|
||||||
|
//
|
||||||
|
// Permission to copy, use, modify, sell and distribute this software
|
||||||
|
// is granted provided this copyright notice appears in all copies.
|
||||||
|
// This software is provided "as is" without express or implied
|
||||||
|
// warranty, and with no claim as to its suitability for any purpose.
|
||||||
|
//
|
||||||
|
// Please send questions, comments, complaints, performance data, etc to
|
||||||
|
// ralf.mattethat@teknologisk.dk
|
||||||
|
//
|
||||||
|
// Requirements for element type
|
||||||
|
// * must be copy-constructible
|
||||||
|
// * destructor must not throw exception
|
||||||
|
//
|
||||||
|
// Methods marked with note A only throws an exception if the evaluation of the
|
||||||
|
// predicate throws an exception. If an exception is thrown the call has no
|
||||||
|
// effect on the containers state
|
||||||
|
//
|
||||||
|
// Methods marked with note B only throws an exception if the coppy constructor
|
||||||
|
// or assignment operator of the predicate throws an exception. If an exception
|
||||||
|
// is thrown the call has no effect on the containers state
|
||||||
|
//
|
||||||
|
// iterators are only invalidated, if the element pointed to by the iterator
|
||||||
|
// is deleted. The same goes for element references
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef BOOST_INTRUSIVE_SPLAYTREE_ALGORITHMS_HPP
|
||||||
|
#define BOOST_INTRUSIVE_SPLAYTREE_ALGORITHMS_HPP
|
||||||
|
|
||||||
|
#include <boost/intrusive/detail/config_begin.hpp>
|
||||||
|
#include <boost/intrusive/detail/assert.hpp>
|
||||||
|
#include <boost/intrusive/intrusive_fwd.hpp>
|
||||||
|
#include <cstddef>
|
||||||
|
#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 {
|
||||||
|
|
||||||
|
//! A splay tree is an implementation of a binary search tree. The tree is
|
||||||
|
//! self balancing using the splay algorithm as described in
|
||||||
|
//!
|
||||||
|
//! "Self-Adjusting Binary Search Trees
|
||||||
|
//! by Daniel Dominic Sleator and Robert Endre Tarjan
|
||||||
|
//! AT&T Bell Laboratories, Murray Hill, NJ
|
||||||
|
//! Journal of the ACM, Vol 32, no 3, July 1985, pp 652-686
|
||||||
|
|
||||||
|
//! splaytree_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
|
||||||
|
//!
|
||||||
|
//! <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>
|
||||||
|
template<class NodeTraits>
|
||||||
|
class splaytree_algorithms
|
||||||
|
{
|
||||||
|
/// @cond
|
||||||
|
private:
|
||||||
|
typedef typename NodeTraits::node node;
|
||||||
|
typedef detail::tree_algorithms<NodeTraits> tree_algorithms;
|
||||||
|
/// @endcond
|
||||||
|
|
||||||
|
public:
|
||||||
|
typedef NodeTraits node_traits;
|
||||||
|
typedef typename NodeTraits::node_ptr node_ptr;
|
||||||
|
typedef typename NodeTraits::const_node_ptr const_node_ptr;
|
||||||
|
|
||||||
|
//! This type is the information that will be
|
||||||
|
//! filled by insert_unique_check
|
||||||
|
typedef typename tree_algorithms::insert_commit_data insert_commit_data;
|
||||||
|
|
||||||
|
/// @cond
|
||||||
|
private:
|
||||||
|
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); }
|
||||||
|
|
||||||
|
//! <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); }
|
||||||
|
|
||||||
|
static void unlink(node_ptr node)
|
||||||
|
{ tree_algorithms::unlink(node); }
|
||||||
|
|
||||||
|
//! <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)
|
||||||
|
{ tree_algorithms::swap_nodes(node1, header1, node2, header2); }
|
||||||
|
|
||||||
|
//! <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); }
|
||||||
|
|
||||||
|
//! <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); }
|
||||||
|
|
||||||
|
//! <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>: 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>: 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>: "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); }
|
||||||
|
|
||||||
|
//! <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
|
||||||
|
(node_ptr header, const KeyType &key
|
||||||
|
,KeyNodePtrCompare comp, insert_commit_data &commit_data, bool splay = true)
|
||||||
|
{
|
||||||
|
if(splay)
|
||||||
|
splay_down(header, key, comp);
|
||||||
|
return tree_algorithms::insert_unique_check(header, key, comp, commit_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class KeyType, class KeyNodePtrCompare>
|
||||||
|
static std::pair<node_ptr, bool> insert_unique_check
|
||||||
|
(node_ptr header, node_ptr hint, const KeyType &key
|
||||||
|
,KeyNodePtrCompare comp, insert_commit_data &commit_data, bool splay = true)
|
||||||
|
{
|
||||||
|
if(splay)
|
||||||
|
splay_down(header, key, comp);
|
||||||
|
return tree_algorithms::insert_unique_check(header, hint, key, comp, commit_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool is_header(const_node_ptr p)
|
||||||
|
{ return tree_algorithms::is_header(p); }
|
||||||
|
|
||||||
|
//! <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, bool splay = true)
|
||||||
|
{
|
||||||
|
if(splay)
|
||||||
|
splay_down(uncast(header), key, comp);
|
||||||
|
node_ptr end = uncast(header);
|
||||||
|
node_ptr y = lower_bound(header, key, comp, false);
|
||||||
|
node_ptr r = (y == end || comp(key, y)) ? end : y;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! <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, bool splay = true)
|
||||||
|
{
|
||||||
|
//if(splay)
|
||||||
|
//splay_down(uncast(header), key, comp);
|
||||||
|
std::pair<node_ptr, node_ptr> ret =
|
||||||
|
tree_algorithms::equal_range(header, key, comp);
|
||||||
|
|
||||||
|
if(splay)
|
||||||
|
splay_up(ret.first, uncast(header));
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! <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, bool splay = true)
|
||||||
|
{
|
||||||
|
//if(splay)
|
||||||
|
//splay_down(uncast(header), key, comp);
|
||||||
|
node_ptr y = tree_algorithms::lower_bound(header, key, comp);
|
||||||
|
if(splay)
|
||||||
|
splay_up(y, uncast(header));
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! <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, bool splay = true)
|
||||||
|
{
|
||||||
|
//if(splay)
|
||||||
|
//splay_down(uncast(header), key, comp);
|
||||||
|
node_ptr y = tree_algorithms::upper_bound(header, key, comp);
|
||||||
|
if(splay)
|
||||||
|
splay_up(y, uncast(header));
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! <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, bool splay = true)
|
||||||
|
{
|
||||||
|
if(splay)
|
||||||
|
splay_down(header, new_node, comp);
|
||||||
|
return tree_algorithms::insert_equal(header, hint, new_node, comp);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class NodePtrCompare>
|
||||||
|
static node_ptr insert_equal_upper_bound
|
||||||
|
(node_ptr header, node_ptr new_node, NodePtrCompare comp, bool splay = true)
|
||||||
|
{
|
||||||
|
if(splay)
|
||||||
|
splay_down(header, new_node, comp);
|
||||||
|
return tree_algorithms::insert_equal_upper_bound(header, new_node, comp);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class NodePtrCompare>
|
||||||
|
static node_ptr insert_equal_lower_bound
|
||||||
|
(node_ptr header, node_ptr new_node, NodePtrCompare comp, bool splay = true)
|
||||||
|
{
|
||||||
|
if(splay)
|
||||||
|
splay_down(header, new_node, comp);
|
||||||
|
return tree_algorithms::insert_equal_lower_bound(header, new_node, comp);
|
||||||
|
}
|
||||||
|
|
||||||
|
//! <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)
|
||||||
|
{ tree_algorithms::clone(source_header, target_header, cloner, disposer); }
|
||||||
|
|
||||||
|
// delete node | complexity : constant | exception : nothrow
|
||||||
|
static void erase(node_ptr header, node_ptr z, bool splay = true)
|
||||||
|
{
|
||||||
|
// node_base* n = t->right;
|
||||||
|
// if( t->left != 0 ){
|
||||||
|
// node_base* l = t->previous();
|
||||||
|
// splay_up( l , t );
|
||||||
|
// n = t->left;
|
||||||
|
// n->right = t->right;
|
||||||
|
// if( n->right != 0 )
|
||||||
|
// n->right->parent = n;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// if( n != 0 )
|
||||||
|
// n->parent = t->parent;
|
||||||
|
//
|
||||||
|
// if( t->parent->left == t )
|
||||||
|
// t->parent->left = n;
|
||||||
|
// else // must be ( t->parent->right == t )
|
||||||
|
// t->parent->right = n;
|
||||||
|
//
|
||||||
|
// if( data_->parent == t )
|
||||||
|
// data_->parent = find_leftmost();
|
||||||
|
//posibility 1
|
||||||
|
if(splay && NodeTraits::get_left(z) != 0 ){
|
||||||
|
node_ptr l = prev_node(z);
|
||||||
|
splay_up(l, header);
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
//possibility 2
|
||||||
|
if(splay && NodeTraits::get_left(z) != 0 ){
|
||||||
|
node_ptr l = NodeTraits::get_left(z);
|
||||||
|
splay_up(l, header);
|
||||||
|
}*//*
|
||||||
|
if(splay && NodeTraits::get_left(z) != 0 ){
|
||||||
|
node_ptr l = prev_node(z);
|
||||||
|
splay_up_impl(l, z);
|
||||||
|
}*/
|
||||||
|
/*
|
||||||
|
//possibility 4
|
||||||
|
if(splay){
|
||||||
|
splay_up(z, header);
|
||||||
|
}*/
|
||||||
|
|
||||||
|
//if(splay)
|
||||||
|
//splay_up(z, header);
|
||||||
|
tree_algorithms::erase(header, z);
|
||||||
|
}
|
||||||
|
|
||||||
|
// bottom-up splay, use data_ as parent for n | complexity : logarithmic | exception : nothrow
|
||||||
|
static void splay_up(node_ptr n, node_ptr header)
|
||||||
|
{
|
||||||
|
if(n == header){ // do a splay for the right most node instead
|
||||||
|
// this is to boost performance of equal_range/count on equivalent containers in the case
|
||||||
|
// where there are many equal elements at the end
|
||||||
|
n = NodeTraits::get_right(header);
|
||||||
|
}
|
||||||
|
|
||||||
|
node_ptr t = header;
|
||||||
|
|
||||||
|
if( n == t ) return;
|
||||||
|
|
||||||
|
for( ;; ){
|
||||||
|
node_ptr p = NodeTraits::get_parent(n);
|
||||||
|
node_ptr g = NodeTraits::get_parent(p);
|
||||||
|
|
||||||
|
if( p == t ) break;
|
||||||
|
|
||||||
|
if( g == t ){
|
||||||
|
// zig
|
||||||
|
rotate(n);
|
||||||
|
}
|
||||||
|
else if ((NodeTraits::get_left(p) == n && NodeTraits::get_left(g) == p) ||
|
||||||
|
(NodeTraits::get_right(p) == n && NodeTraits::get_right(g) == p) ){
|
||||||
|
// zig-zig
|
||||||
|
rotate(p);
|
||||||
|
rotate(n);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
// zig-zag
|
||||||
|
rotate(n);
|
||||||
|
rotate(n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// top-down splay | complexity : logarithmic | exception : strong, note A
|
||||||
|
template<class KeyType, class KeyNodePtrCompare>
|
||||||
|
static node_ptr splay_down(node_ptr header, const KeyType &key, KeyNodePtrCompare comp, bool splay = true)
|
||||||
|
{
|
||||||
|
if(!NodeTraits::get_parent(header))
|
||||||
|
return header;
|
||||||
|
//Most splay tree implementations use a dummy/null node to implement.
|
||||||
|
//this function. This has some problems for a generic library like Intrusive:
|
||||||
|
//
|
||||||
|
// * The node might not have a default constructor.
|
||||||
|
// * The default constructor could throw.
|
||||||
|
//
|
||||||
|
//We already have a header node. Leftmost and rightmost nodes of the tree
|
||||||
|
//are not changed when splaying (because the invariants of the tree don't
|
||||||
|
//change) We can back up them, use the header as the null node and
|
||||||
|
//reassign old values after the function has been completed.
|
||||||
|
node_ptr t = NodeTraits::get_parent(header);
|
||||||
|
//Check if tree has a single node
|
||||||
|
if(!NodeTraits::get_left(t) && !NodeTraits::get_right(t))
|
||||||
|
return t;
|
||||||
|
//Backup leftmost/rightmost
|
||||||
|
node_ptr leftmost = NodeTraits::get_left(header);
|
||||||
|
node_ptr rightmost = NodeTraits::get_right(header);
|
||||||
|
|
||||||
|
try{
|
||||||
|
node_ptr null = header;
|
||||||
|
node_ptr l = null;
|
||||||
|
node_ptr r = null;
|
||||||
|
|
||||||
|
for( ;; ){
|
||||||
|
if(comp(key, t)){
|
||||||
|
if(NodeTraits::get_left(t) == 0 )
|
||||||
|
break;
|
||||||
|
if(comp(key, NodeTraits::get_left(t))){
|
||||||
|
t = tree_algorithms::rotate_right(t);
|
||||||
|
|
||||||
|
if(NodeTraits::get_left(t) == 0)
|
||||||
|
break;
|
||||||
|
link_right(t, r);
|
||||||
|
}
|
||||||
|
else if(comp(NodeTraits::get_left(t), key)){
|
||||||
|
link_right(t, r);
|
||||||
|
|
||||||
|
if(NodeTraits::get_right(t) == 0 )
|
||||||
|
break;
|
||||||
|
link_left(t, l);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
link_right(t, r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if(comp(t, key)){
|
||||||
|
if(NodeTraits::get_right(t) == 0 )
|
||||||
|
break;
|
||||||
|
|
||||||
|
if(comp(NodeTraits::get_right(t), key)){
|
||||||
|
t = tree_algorithms::rotate_left( t );
|
||||||
|
|
||||||
|
if(NodeTraits::get_right(t) == 0 )
|
||||||
|
break;
|
||||||
|
link_left(t, l);
|
||||||
|
}
|
||||||
|
else if(comp(key, NodeTraits::get_right(t))){
|
||||||
|
link_left(t, l);
|
||||||
|
|
||||||
|
if(NodeTraits::get_left(t) == 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
link_right(t, r);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
link_left(t, l);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assemble(t, l, r, null);
|
||||||
|
}
|
||||||
|
catch(...){
|
||||||
|
//Exception can only be thrown by comp, but
|
||||||
|
//tree invariants still hold. t is the current root
|
||||||
|
//so link it to the header.
|
||||||
|
NodeTraits::set_parent(t, header);
|
||||||
|
NodeTraits::set_parent(header, t);
|
||||||
|
//Recover leftmost/rightmost pointers
|
||||||
|
NodeTraits::set_left (header, leftmost);
|
||||||
|
NodeTraits::set_right(header, rightmost);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
//t is the current root
|
||||||
|
NodeTraits::set_parent(header, t);
|
||||||
|
NodeTraits::set_parent(t, header);
|
||||||
|
//Recover leftmost/rightmost pointers
|
||||||
|
NodeTraits::set_left (header, leftmost);
|
||||||
|
NodeTraits::set_right(header, rightmost);
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
/// @cond
|
||||||
|
|
||||||
|
// assemble the three sub-trees into new tree pointed to by t | complexity : constant | exception : nothrow
|
||||||
|
static void assemble( node_ptr t, node_ptr l, node_ptr r, const_node_ptr null_node )
|
||||||
|
{
|
||||||
|
NodeTraits::set_right(l, NodeTraits::get_left(t));
|
||||||
|
NodeTraits::set_left(r, NodeTraits::get_right(t));
|
||||||
|
|
||||||
|
if(NodeTraits::get_right(l) != 0){
|
||||||
|
NodeTraits::set_parent(NodeTraits::get_right(l), l);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(NodeTraits::get_left(r) != 0){
|
||||||
|
NodeTraits::set_parent(NodeTraits::get_left(r), r);
|
||||||
|
}
|
||||||
|
|
||||||
|
NodeTraits::set_left (t, NodeTraits::get_right(null_node));
|
||||||
|
NodeTraits::set_right(t, NodeTraits::get_left(null_node));
|
||||||
|
|
||||||
|
if( NodeTraits::get_left(t) != 0 ){
|
||||||
|
NodeTraits::set_parent(NodeTraits::get_left(t), t);
|
||||||
|
}
|
||||||
|
|
||||||
|
if( NodeTraits::get_right(t) ){
|
||||||
|
NodeTraits::set_parent(NodeTraits::get_right(t), t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// break link to left child node and attach it to left tree pointed to by l | complexity : constant | exception : nothrow
|
||||||
|
static void link_left(node_ptr& t, node_ptr& l)
|
||||||
|
{
|
||||||
|
NodeTraits::set_right(l, t);
|
||||||
|
NodeTraits::set_parent(t, l);
|
||||||
|
l = t;
|
||||||
|
t = NodeTraits::get_right(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
// break link to right child node and attach it to right tree pointed to by r | complexity : constant | exception : nothrow
|
||||||
|
static void link_right(node_ptr& t, node_ptr& r)
|
||||||
|
{
|
||||||
|
NodeTraits::set_left(r, t);
|
||||||
|
NodeTraits::set_parent(t, r);
|
||||||
|
r = t;
|
||||||
|
t = NodeTraits::get_left(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
// rotate n with its parent | complexity : constant | exception : nothrow
|
||||||
|
static void rotate(node_ptr n)
|
||||||
|
{
|
||||||
|
node_ptr p = NodeTraits::get_parent(n);
|
||||||
|
node_ptr g = NodeTraits::get_parent(p);
|
||||||
|
//Test if g is header before breaking tree
|
||||||
|
//invariants that would make is_header invalid
|
||||||
|
bool g_is_header = is_header(g);
|
||||||
|
|
||||||
|
if(NodeTraits::get_left(p) == n){
|
||||||
|
NodeTraits::set_left(p, NodeTraits::get_right(n));
|
||||||
|
if(NodeTraits::get_left(p) != 0)
|
||||||
|
NodeTraits::set_parent(NodeTraits::get_left(p), p);
|
||||||
|
NodeTraits::set_right(n, p);
|
||||||
|
}
|
||||||
|
else{ // must be ( p->right == n )
|
||||||
|
NodeTraits::set_right(p, NodeTraits::get_left(n));
|
||||||
|
if(NodeTraits::get_right(p) != 0)
|
||||||
|
NodeTraits::set_parent(NodeTraits::get_right(p), p);
|
||||||
|
NodeTraits::set_left(n, p);
|
||||||
|
}
|
||||||
|
|
||||||
|
NodeTraits::set_parent(p, n);
|
||||||
|
NodeTraits::set_parent(n, g);
|
||||||
|
|
||||||
|
if(g_is_header){
|
||||||
|
if(NodeTraits::get_parent(g) == p)
|
||||||
|
NodeTraits::set_parent(g, n);
|
||||||
|
else{//must be ( g->right == p )
|
||||||
|
assert(0);
|
||||||
|
NodeTraits::set_right(g, n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
if(NodeTraits::get_left(g) == p)
|
||||||
|
NodeTraits::set_left(g, n);
|
||||||
|
else //must be ( g->right == p )
|
||||||
|
NodeTraits::set_right(g, n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @endcond
|
||||||
|
};
|
||||||
|
|
||||||
|
} //namespace intrusive
|
||||||
|
} //namespace boost
|
||||||
|
|
||||||
|
#include <boost/intrusive/detail/config_end.hpp>
|
||||||
|
|
||||||
|
#endif //BOOST_INTRUSIVE_SPLAYTREE_ALGORITHMS_HPP
|
Reference in New Issue
Block a user