From 08c1e93fa36a49f49325a07fe91ff92c964c2b6c Mon Sep 17 00:00:00 2001 From: Chanho Park Date: Thu, 11 Dec 2014 18:55:56 +0900 Subject: Imported Upstream version 1.57.0 --- boost/intrusive/any_hook.hpp | 102 +- boost/intrusive/avl_set.hpp | 2728 ++++--------------- boost/intrusive/avl_set_hook.hpp | 38 +- boost/intrusive/avltree.hpp | 1917 +++---------- boost/intrusive/avltree_algorithms.hpp | 925 +++---- boost/intrusive/bs_set.hpp | 948 +++++++ boost/intrusive/bs_set_hook.hpp | 44 +- boost/intrusive/bstree.hpp | 2177 +++++++++++++++ boost/intrusive/bstree_algorithms.hpp | 2127 +++++++++++++++ boost/intrusive/circular_list_algorithms.hpp | 120 +- boost/intrusive/circular_slist_algorithms.hpp | 68 +- boost/intrusive/derivation_value_traits.hpp | 37 +- boost/intrusive/detail/algo_type.hpp | 46 + boost/intrusive/detail/any_node_and_algorithms.hpp | 79 +- boost/intrusive/detail/array_initializer.hpp | 90 + boost/intrusive/detail/assert.hpp | 4 +- boost/intrusive/detail/avltree_node.hpp | 61 +- .../intrusive/detail/clear_on_destructor_base.hpp | 36 - boost/intrusive/detail/common_slist_algorithms.hpp | 109 +- boost/intrusive/detail/config_begin.hpp | 5 +- boost/intrusive/detail/config_end.hpp | 2 +- boost/intrusive/detail/default_header_holder.hpp | 65 + boost/intrusive/detail/ebo_functor_holder.hpp | 141 +- boost/intrusive/detail/empty_node_checker.hpp | 40 + boost/intrusive/detail/equal_to_value.hpp | 44 + boost/intrusive/detail/exception_disposer.hpp | 84 + boost/intrusive/detail/function_detector.hpp | 8 +- boost/intrusive/detail/generic_hook.hpp | 198 +- boost/intrusive/detail/get_value_traits.hpp | 218 ++ .../detail/has_member_function_callable_with.hpp | 264 +- boost/intrusive/detail/hashtable_node.hpp | 200 +- boost/intrusive/detail/hook_traits.hpp | 182 ++ boost/intrusive/detail/iiterator.hpp | 227 ++ .../intrusive/detail/is_stateful_value_traits.hpp | 8 +- boost/intrusive/detail/key_nodeptr_comp.hpp | 72 + boost/intrusive/detail/list_iterator.hpp | 129 + boost/intrusive/detail/list_node.hpp | 159 +- boost/intrusive/detail/math.hpp | 271 ++ boost/intrusive/detail/memory_util.hpp | 214 +- boost/intrusive/detail/mpl.hpp | 355 +-- boost/intrusive/detail/node_cloner_disposer.hpp | 109 + boost/intrusive/detail/node_holder.hpp | 31 + boost/intrusive/detail/node_to_value.hpp | 126 + boost/intrusive/detail/parent_from_member.hpp | 45 +- boost/intrusive/detail/pointer_element.hpp | 170 ++ boost/intrusive/detail/preprocessor.hpp | 16 +- boost/intrusive/detail/rbtree_node.hpp | 71 +- boost/intrusive/detail/reverse_iterator.hpp | 139 + boost/intrusive/detail/simple_disposers.hpp | 46 + boost/intrusive/detail/size_holder.hpp | 80 + boost/intrusive/detail/slist_iterator.hpp | 120 + boost/intrusive/detail/slist_node.hpp | 132 +- boost/intrusive/detail/std_fwd.hpp | 54 + boost/intrusive/detail/to_raw_pointer.hpp | 42 + boost/intrusive/detail/transform_iterator.hpp | 13 +- boost/intrusive/detail/tree_algorithms.hpp | 1747 ------------ boost/intrusive/detail/tree_iterator.hpp | 141 + boost/intrusive/detail/tree_node.hpp | 157 +- boost/intrusive/detail/uncast.hpp | 51 + boost/intrusive/detail/utilities.hpp | 879 ------ boost/intrusive/detail/workaround.hpp | 16 +- boost/intrusive/hashtable.hpp | 2546 +++++++++--------- boost/intrusive/intrusive_fwd.hpp | 647 +++-- boost/intrusive/linear_slist_algorithms.hpp | 17 +- boost/intrusive/link_mode.hpp | 25 +- boost/intrusive/list.hpp | 391 +-- boost/intrusive/list_hook.hpp | 36 +- boost/intrusive/member_value_traits.hpp | 28 +- boost/intrusive/options.hpp | 738 +---- boost/intrusive/pack_options.hpp | 374 +++ boost/intrusive/parent_from_member.hpp | 14 +- boost/intrusive/pointer_plus_bits.hpp | 16 +- boost/intrusive/pointer_rebind.hpp | 188 ++ boost/intrusive/pointer_traits.hpp | 74 +- boost/intrusive/priority_compare.hpp | 27 +- boost/intrusive/rbtree.hpp | 1886 +++---------- boost/intrusive/rbtree_algorithms.hpp | 922 ++----- boost/intrusive/set.hpp | 2684 ++++--------------- boost/intrusive/set_hook.hpp | 38 +- boost/intrusive/sg_set.hpp | 2787 ++++--------------- boost/intrusive/sgtree.hpp | 1847 +++---------- boost/intrusive/sgtree_algorithms.hpp | 759 ++---- boost/intrusive/slist.hpp | 766 +++--- boost/intrusive/slist_hook.hpp | 37 +- boost/intrusive/splay_set.hpp | 2810 +++++--------------- boost/intrusive/splay_set_hook.hpp | 292 -- boost/intrusive/splaytree.hpp | 1863 +++---------- boost/intrusive/splaytree_algorithms.hpp | 1127 +++----- boost/intrusive/treap.hpp | 1395 +++------- boost/intrusive/treap_algorithms.hpp | 565 ++-- boost/intrusive/treap_set.hpp | 2671 ++++--------------- boost/intrusive/trivial_value_traits.hpp | 16 +- boost/intrusive/unordered_set.hpp | 408 +-- boost/intrusive/unordered_set_hook.hpp | 63 +- 94 files changed, 18539 insertions(+), 28245 deletions(-) create mode 100644 boost/intrusive/bs_set.hpp create mode 100644 boost/intrusive/bstree.hpp create mode 100644 boost/intrusive/bstree_algorithms.hpp create mode 100644 boost/intrusive/detail/algo_type.hpp create mode 100644 boost/intrusive/detail/array_initializer.hpp delete mode 100644 boost/intrusive/detail/clear_on_destructor_base.hpp create mode 100644 boost/intrusive/detail/default_header_holder.hpp create mode 100644 boost/intrusive/detail/empty_node_checker.hpp create mode 100644 boost/intrusive/detail/equal_to_value.hpp create mode 100644 boost/intrusive/detail/exception_disposer.hpp create mode 100644 boost/intrusive/detail/get_value_traits.hpp create mode 100644 boost/intrusive/detail/hook_traits.hpp create mode 100644 boost/intrusive/detail/iiterator.hpp create mode 100644 boost/intrusive/detail/key_nodeptr_comp.hpp create mode 100644 boost/intrusive/detail/list_iterator.hpp create mode 100644 boost/intrusive/detail/math.hpp create mode 100644 boost/intrusive/detail/node_cloner_disposer.hpp create mode 100644 boost/intrusive/detail/node_holder.hpp create mode 100644 boost/intrusive/detail/node_to_value.hpp create mode 100644 boost/intrusive/detail/pointer_element.hpp create mode 100644 boost/intrusive/detail/reverse_iterator.hpp create mode 100644 boost/intrusive/detail/simple_disposers.hpp create mode 100644 boost/intrusive/detail/size_holder.hpp create mode 100644 boost/intrusive/detail/slist_iterator.hpp create mode 100644 boost/intrusive/detail/std_fwd.hpp create mode 100644 boost/intrusive/detail/to_raw_pointer.hpp delete mode 100644 boost/intrusive/detail/tree_algorithms.hpp create mode 100644 boost/intrusive/detail/tree_iterator.hpp create mode 100644 boost/intrusive/detail/uncast.hpp delete mode 100644 boost/intrusive/detail/utilities.hpp create mode 100644 boost/intrusive/pack_options.hpp create mode 100644 boost/intrusive/pointer_rebind.hpp delete mode 100644 boost/intrusive/splay_set_hook.hpp (limited to 'boost/intrusive') diff --git a/boost/intrusive/any_hook.hpp b/boost/intrusive/any_hook.hpp index 7d60de001a..1b538373d9 100644 --- a/boost/intrusive/any_hook.hpp +++ b/boost/intrusive/any_hook.hpp @@ -1,6 +1,6 @@ ///////////////////////////////////////////////////////////////////////////// // -// (C) Copyright Ion Gaztanaga 2006-2012 +// (C) Copyright Ion Gaztanaga 2006-2013 // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at @@ -13,31 +13,27 @@ #ifndef BOOST_INTRUSIVE_ANY_HOOK_HPP #define BOOST_INTRUSIVE_ANY_HOOK_HPP +#if defined(_MSC_VER) +# pragma once +#endif + #include #include -#include #include #include #include -#include +#include +#include namespace boost { namespace intrusive { -/// @cond -template -struct get_any_node_algo -{ - typedef any_algorithms type; -}; -/// @endcond - //! Helper metafunction to define a \c \c any_base_hook that yields to the same //! type when the same options (either explicitly or implicitly) are used. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) template #else -template +template #endif struct make_any_base_hook { @@ -51,11 +47,11 @@ struct make_any_base_hook #endif >::type packed_options; - typedef detail::generic_hook - < get_any_node_algo + typedef generic_hook + < any_algorithms , typename packed_options::tag , packed_options::link_mode - , detail::AnyBaseHook + , AnyBaseHookId > implementation_defined; /// @endcond typedef implementation_defined type; @@ -75,7 +71,7 @@ struct make_any_base_hook //! \c link_mode<> will specify the linking mode of the hook (\c normal_link, \c safe_link). //! //! \c void_pointer<> is the pointer type that will be used internally in the hook -//! and the the container configured to use this hook. +//! and the container configured to use this hook. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) template #else @@ -142,7 +138,7 @@ class any_base_hook #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) template #else -template +template #endif struct make_any_member_hook { @@ -156,11 +152,11 @@ struct make_any_member_hook #endif >::type packed_options; - typedef detail::generic_hook - < get_any_node_algo + typedef generic_hook + < any_algorithms , member_tag , packed_options::link_mode - , detail::NoBaseHook + , NoBaseHookId > implementation_defined; /// @endcond typedef implementation_defined type; @@ -175,7 +171,7 @@ struct make_any_member_hook //! \c link_mode<> will specify the linking mode of the hook (\c normal_link or \c safe_link). //! //! \c void_pointer<> is the pointer type that will be used internally in the hook -//! and the the container configured to use this hook. +//! and the container configured to use this hook. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) template #else @@ -241,38 +237,34 @@ class any_member_hook namespace detail{ -template -struct any_to_get_base_pointer_type -{ - typedef typename pointer_traits::template - rebind_pointer::type type; -}; - -template -struct any_to_get_member_pointer_type -{ - typedef typename pointer_traits - ::template rebind_pointer::type type; -}; +BOOST_INTRUSIVE_INTERNAL_STATIC_BOOL_IS_TRUE(old_proto_value_traits_base_hook, hooktags::is_base_hook) //!This option setter specifies that the container //!must use the specified base hook -template class NodeTraits> +template class NodeTraits> struct any_to_some_hook { - typedef typename BaseHook::template pack::value_traits old_value_traits; + typedef typename BasicHook::template pack::proto_value_traits old_proto_value_traits; + template struct pack : public Base { - struct value_traits : public old_value_traits + struct proto_value_traits { + //proto_value_traits::hooktags::is_base_hook is used by get_value_traits + //to detect base hooks, so mark it in case BasicHook has it. + struct hooktags + { + static const bool is_base_hook = old_proto_value_traits_base_hook_bool_is_true + ::value; + }; + + typedef old_proto_value_traits basic_hook_t; static const bool is_any_hook = true; - typedef typename detail::eval_if_c - < detail::internal_base_hook_bool_is_true::value - , any_to_get_base_pointer_type - , any_to_get_member_pointer_type - >::type void_pointer; - typedef NodeTraits node_traits; + + template + struct node_traits_from_voidptr + { typedef NodeTraits type; }; }; }; }; @@ -283,55 +275,55 @@ struct any_to_some_hook //!This option setter specifies that //!any hook should behave as an slist hook -template +template struct any_to_slist_hook /// @cond - : public detail::any_to_some_hook + : public detail::any_to_some_hook /// @endcond {}; //!This option setter specifies that //!any hook should behave as an list hook -template +template struct any_to_list_hook /// @cond - : public detail::any_to_some_hook + : public detail::any_to_some_hook /// @endcond {}; //!This option setter specifies that //!any hook should behave as a set hook -template +template struct any_to_set_hook /// @cond - : public detail::any_to_some_hook + : public detail::any_to_some_hook /// @endcond {}; //!This option setter specifies that //!any hook should behave as an avl_set hook -template +template struct any_to_avl_set_hook /// @cond - : public detail::any_to_some_hook + : public detail::any_to_some_hook /// @endcond {}; //!This option setter specifies that any //!hook should behave as a bs_set hook -template +template struct any_to_bs_set_hook /// @cond - : public detail::any_to_some_hook + : public detail::any_to_some_hook /// @endcond {}; //!This option setter specifies that any hook //!should behave as an unordered set hook -template +template struct any_to_unordered_set_hook /// @cond - : public detail::any_to_some_hook + : public detail::any_to_some_hook /// @endcond {}; diff --git a/boost/intrusive/avl_set.hpp b/boost/intrusive/avl_set.hpp index 91e2dbbdf0..58d6108f95 100644 --- a/boost/intrusive/avl_set.hpp +++ b/boost/intrusive/avl_set.hpp @@ -1,6 +1,6 @@ ///////////////////////////////////////////////////////////////////////////// // -// (C) Copyright Ion Gaztanaga 2007-2012 +// (C) Copyright Ion Gaztanaga 2007-2014 // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at @@ -12,12 +12,16 @@ #ifndef BOOST_INTRUSIVE_AVL_SET_HPP #define BOOST_INTRUSIVE_AVL_SET_HPP +#if defined(_MSC_VER) +# pragma once +#endif + #include #include #include #include -#include -#include +#include +#include namespace boost { namespace intrusive { @@ -36,14 +40,15 @@ namespace intrusive { #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) template #else -template +template #endif class avl_set_impl +#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + : public bstree_impl +#endif { /// @cond - typedef avltree_impl tree_type; - //! This class is - //! movable + typedef bstree_impl tree_type; BOOST_MOVABLE_BUT_NOT_COPYABLE(avl_set_impl) typedef tree_type implementation_defined; @@ -71,1208 +76,385 @@ class avl_set_impl typedef typename implementation_defined::const_node_ptr const_node_ptr; typedef typename implementation_defined::node_algorithms node_algorithms; - static const bool constant_time_size = Config::constant_time_size; - - /// @cond - private: - tree_type tree_; - /// @endcond + static const bool constant_time_size = tree_type::constant_time_size; public: - //! Effects: Constructs an empty avl_set. - //! - //! Complexity: Constant. - //! - //! Throws: If value_traits::node_traits::node - //! constructor throws (this does not happen with predefined Boost.Intrusive hooks) - //! or the copy constructor of the value_compare object throws. - avl_set_impl( const value_compare &cmp = value_compare() - , const value_traits &v_traits = value_traits()) - : tree_(cmp, v_traits) + + //! @copydoc ::boost::intrusive::avltree::avltree(const value_compare &,const value_traits &) + explicit avl_set_impl( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : tree_type(cmp, v_traits) {} - //! Requires: Dereferencing iterator must yield an lvalue of type value_type. - //! cmp must be a comparison function that induces a strict weak ordering. - //! - //! Effects: Constructs an empty avl_set and inserts elements from - //! [b, e). - //! - //! Complexity: Linear in N if [b, e) is already sorted using - //! comp and otherwise N * log N, where N is std::distance(last, first). - //! - //! Throws: If value_traits::node_traits::node - //! constructor throws (this does not happen with predefined Boost.Intrusive hooks) - //! or the copy constructor/operator() of the value_compare object throws. + //! @copydoc ::boost::intrusive::avltree::avltree(bool,Iterator,Iterator,const value_compare &,const value_traits &) template avl_set_impl( Iterator b, Iterator e , const value_compare &cmp = value_compare() , const value_traits &v_traits = value_traits()) - : tree_(true, b, e, cmp, v_traits) + : tree_type(true, b, e, cmp, v_traits) {} - //! Effects: to-do - //! + //! @copydoc ::boost::intrusive::avltree::avltree(avltree &&) avl_set_impl(BOOST_RV_REF(avl_set_impl) x) - : tree_(::boost::move(x.tree_)) + : tree_type(::boost::move(static_cast(x))) {} - //! Effects: to-do - //! + //! @copydoc ::boost::intrusive::avltree::operator=(avltree &&) avl_set_impl& operator=(BOOST_RV_REF(avl_set_impl) x) - { tree_ = ::boost::move(x.tree_); return *this; } - - //! Effects: Detaches all elements from this. The objects in the avl_set - //! are not deleted (i.e. no destructors are called). - //! - //! Complexity: Linear to the number of elements on the container. - //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise. - //! - //! Throws: Nothing. - ~avl_set_impl() - {} + { return static_cast(tree_type::operator=(::boost::move(static_cast(x)))); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::avltree::~avltree() + ~avl_set_impl(); + + //! @copydoc ::boost::intrusive::avltree::begin() + iterator begin(); + + //! @copydoc ::boost::intrusive::avltree::begin()const + const_iterator begin() const; + + //! @copydoc ::boost::intrusive::avltree::cbegin()const + const_iterator cbegin() const; + + //! @copydoc ::boost::intrusive::avltree::end() + iterator end(); + + //! @copydoc ::boost::intrusive::avltree::end()const + const_iterator end() const; + + //! @copydoc ::boost::intrusive::avltree::cend()const + const_iterator cend() const; + + //! @copydoc ::boost::intrusive::avltree::begin() + reverse_iterator avlegin(); + + //! @copydoc ::boost::intrusive::avltree::begin()const + const_reverse_iterator avlegin() const; + + //! @copydoc ::boost::intrusive::avltree::crbegin()const + const_reverse_iterator crbegin() const; + + //! @copydoc ::boost::intrusive::avltree::rend() + reverse_iterator rend(); + + //! @copydoc ::boost::intrusive::avltree::rend()const + const_reverse_iterator rend() const; + + //! @copydoc ::boost::intrusive::avltree::crend()const + const_reverse_iterator crend() const; + + //! @copydoc ::boost::intrusive::avltree::container_from_end_iterator(iterator) + static avl_set_impl &container_from_end_iterator(iterator end_iterator); + + //! @copydoc ::boost::intrusive::avltree::container_from_end_iterator(const_iterator) + static const avl_set_impl &container_from_end_iterator(const_iterator end_iterator); + + //! @copydoc ::boost::intrusive::avltree::container_from_iterator(iterator) + static avl_set_impl &container_from_iterator(iterator it); + + //! @copydoc ::boost::intrusive::avltree::container_from_iterator(const_iterator) + static const avl_set_impl &container_from_iterator(const_iterator it); + + //! @copydoc ::boost::intrusive::avltree::key_comp()const + key_compare key_comp() const; + + //! @copydoc ::boost::intrusive::avltree::value_comp()const + value_compare value_comp() const; + + //! @copydoc ::boost::intrusive::avltree::empty()const + bool empty() const; + + //! @copydoc ::boost::intrusive::avltree::size()const + size_type size() const; + + //! @copydoc ::boost::intrusive::avltree::swap + void swap(avl_set_impl& other); - //! Effects: Returns an iterator pointing to the beginning of the avl_set. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - iterator begin() - { return tree_.begin(); } - - //! Effects: Returns a const_iterator pointing to the beginning of the avl_set. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_iterator begin() const - { return tree_.begin(); } - - //! Effects: Returns a const_iterator pointing to the beginning of the avl_set. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_iterator cbegin() const - { return tree_.cbegin(); } - - //! Effects: Returns an iterator pointing to the end of the avl_set. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - iterator end() - { return tree_.end(); } - - //! Effects: Returns a const_iterator pointing to the end of the avl_set. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_iterator end() const - { return tree_.end(); } - - //! Effects: Returns a const_iterator pointing to the end of the avl_set. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_iterator cend() const - { return tree_.cend(); } - - //! Effects: Returns a reverse_iterator pointing to the beginning of the - //! reversed avl_set. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - reverse_iterator rbegin() - { return tree_.rbegin(); } - - //! Effects: Returns a const_reverse_iterator pointing to the beginning - //! of the reversed avl_set. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_reverse_iterator rbegin() const - { return tree_.rbegin(); } - - //! Effects: Returns a const_reverse_iterator pointing to the beginning - //! of the reversed avl_set. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_reverse_iterator crbegin() const - { return tree_.crbegin(); } - - //! Effects: Returns a reverse_iterator pointing to the end - //! of the reversed avl_set. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - reverse_iterator rend() - { return tree_.rend(); } - - //! Effects: Returns a const_reverse_iterator pointing to the end - //! of the reversed avl_set. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_reverse_iterator rend() const - { return tree_.rend(); } - - //! Effects: Returns a const_reverse_iterator pointing to the end - //! of the reversed avl_set. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_reverse_iterator crend() const - { return tree_.crend(); } - - //! Precondition: end_iterator must be a valid end iterator - //! of avl_set. - //! - //! Effects: Returns a const reference to the avl_set associated to the end iterator - //! - //! Throws: Nothing. - //! - //! Complexity: Constant. - static avl_set_impl &container_from_end_iterator(iterator end_iterator) - { - return *detail::parent_from_member - ( &tree_type::container_from_end_iterator(end_iterator) - , &avl_set_impl::tree_); - } - - //! Precondition: end_iterator must be a valid end const_iterator - //! of avl_set. - //! - //! Effects: Returns a const reference to the set associated to the end iterator - //! - //! Throws: Nothing. - //! - //! Complexity: Constant. - static const avl_set_impl &container_from_end_iterator(const_iterator end_iterator) - { - return *detail::parent_from_member - ( &tree_type::container_from_end_iterator(end_iterator) - , &avl_set_impl::tree_); - } - - //! Precondition: it must be a valid iterator of set. - //! - //! Effects: Returns a reference to the set associated to the iterator - //! - //! Throws: Nothing. - //! - //! Complexity: Logarithmic. - static avl_set_impl &container_from_iterator(iterator it) - { - return *detail::parent_from_member - ( &tree_type::container_from_iterator(it) - , &avl_set_impl::tree_); - } - - //! Precondition: it must be a valid const_iterator of set. - //! - //! Effects: Returns a const reference to the set associated to the iterator - //! - //! Throws: Nothing. - //! - //! Complexity: Logarithmic. - static const avl_set_impl &container_from_iterator(const_iterator it) - { - return *detail::parent_from_member - ( &tree_type::container_from_iterator(it) - , &avl_set_impl::tree_); - } - - //! Effects: Returns the key_compare object used by the avl_set. - //! - //! Complexity: Constant. - //! - //! Throws: If key_compare copy-constructor throws. - key_compare key_comp() const - { return tree_.value_comp(); } - - //! Effects: Returns the value_compare object used by the avl_set. - //! - //! Complexity: Constant. - //! - //! Throws: If value_compare copy-constructor throws. - value_compare value_comp() const - { return tree_.value_comp(); } - - //! Effects: Returns true is the container is empty. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - bool empty() const - { return tree_.empty(); } - - //! Effects: Returns the number of elements stored in the avl_set. - //! - //! Complexity: Linear to elements contained in *this if, - //! constant-time size option is enabled. Constant-time otherwise. - //! - //! Throws: Nothing. - size_type size() const - { return tree_.size(); } - - //! Effects: Swaps the contents of two sets. - //! - //! Complexity: Constant. - //! - //! Throws: If the swap() call for the comparison functor - //! found using ADL throws. Strong guarantee. - void swap(avl_set_impl& other) - { tree_.swap(other.tree_); } - - //! Requires: Disposer::operator()(pointer) shouldn't throw. - //! Cloner should yield to nodes equivalent to the original nodes. - //! - //! Effects: 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. Copies the predicate from the source container. - //! - //! If cloner throws, all cloned elements are unlinked and disposed - //! calling Disposer::operator()(pointer). - //! - //! Complexity: Linear to erased plus inserted elements. - //! - //! Throws: If cloner throws or predicate copy assignment throws. Basic guarantee. + //! @copydoc ::boost::intrusive::avltree::clone_from template - void clone_from(const avl_set_impl &src, Cloner cloner, Disposer disposer) - { tree_.clone_from(src.tree_, cloner, disposer); } - - //! Requires: value must be an lvalue - //! - //! Effects: Treaps to inserts value into the avl_set. - //! - //! Returns: If the value - //! is not already present inserts it and returns a pair containing the - //! iterator to the new value and true. If there is an equivalent value - //! returns a pair containing an iterator to the already present value - //! and false. - //! - //! Complexity: Average complexity for insert element is at - //! most logarithmic. - //! - //! Throws: If the internal value_compare ordering function throws. Strong guarantee. - //! - //! Note: Does not affect the validity of iterators and references. - //! No copy-constructors are called. + void clone_from(const avl_set_impl &src, Cloner cloner, Disposer disposer); + + #endif //#ifdef BOOST_iNTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::avltree::insert_unique(reference) std::pair insert(reference value) - { return tree_.insert_unique(value); } - - //! Requires: value must be an lvalue - //! - //! Effects: Treaps to to insert x into the avl_set, using "hint" - //! as a hint to where it will be inserted. - //! - //! Returns: An iterator that points to the position where the - //! new element was inserted into the avl_set. - //! - //! Complexity: Logarithmic in general, but it's amortized - //! constant time if t is inserted immediately before hint. - //! - //! Throws: If the internal value_compare ordering function throws. Strong guarantee. - //! - //! Note: Does not affect the validity of iterators and references. - //! No copy-constructors are called. + { return tree_type::insert_unique(value); } + + //! @copydoc ::boost::intrusive::avltree::insert_unique(const_iterator,reference) iterator insert(const_iterator hint, reference value) - { return tree_.insert_unique(hint, value); } - - //! Requires: key_value_comp must be a comparison function that induces - //! the same strict weak ordering as value_compare. The difference is that - //! key_value_comp compares an arbitrary key with the contained values. - //! - //! Effects: Checks if a value can be inserted in the avl_set, using - //! a user provided key instead of the value itself. - //! - //! Returns: If there is an equivalent value - //! returns a pair containing an iterator to the already present value - //! and false. If the value can be inserted returns true in the returned - //! pair boolean and fills "commit_data" that is meant to be used with - //! the "insert_commit" function. - //! - //! Complexity: Average complexity is at most logarithmic. - //! - //! Throws: If the key_value_comp ordering function throws. Strong guarantee. - //! - //! Notes: This function is used to improve performance when constructing - //! a value_type is expensive: 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 value_type 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 value_type and use - //! "insert_commit" to insert the object 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_commit" only if no more - //! objects are inserted or erased from the avl_set. + { return tree_type::insert_unique(hint, value); } + + //! @copydoc ::boost::intrusive::avltree::insert_unique_check(const KeyType&,KeyValueCompare,insert_commit_data&) template std::pair insert_check (const KeyType &key, KeyValueCompare key_value_comp, insert_commit_data &commit_data) - { return tree_.insert_unique_check(key, key_value_comp, commit_data); } - - //! Requires: key_value_comp must be a comparison function that induces - //! the same strict weak ordering as value_compare. The difference is that - //! key_value_comp compares an arbitrary key with the contained values. - //! - //! Effects: Checks if a value can be inserted in the avl_set, using - //! a user provided key instead of the value itself, using "hint" - //! as a hint to where it will be inserted. - //! - //! Returns: If there is an equivalent value - //! returns a pair containing an iterator to the already present value - //! and false. If the value can be inserted returns true in the returned - //! pair boolean and fills "commit_data" that is meant to be used with - //! the "insert_commit" function. - //! - //! Complexity: Logarithmic in general, but it's amortized - //! constant time if t is inserted immediately before hint. - //! - //! Throws: If the key_value_comp ordering function throws. Strong guarantee. - //! - //! Notes: This function is used to improve performance when constructing - //! a value_type is expensive: if there is an equivalent value - //! the constructed object must be discarded. Many times, the part of the - //! constructing that is used to impose the order is much cheaper to construct - //! than the value_type and this function offers the possibility to use that key - //! to check if the insertion will be successful. - //! - //! If the check is successful, the user can construct the value_type and use - //! "insert_commit" to insert the object in constant-time. This can give a total - //! constant-time complexity to the insertion: check(O(1)) + commit(O(1)). - //! - //! "commit_data" remains valid for a subsequent "insert_commit" only if no more - //! objects are inserted or erased from the avl_set. + { return tree_type::insert_unique_check(key, key_value_comp, commit_data); } + + //! @copydoc ::boost::intrusive::avltree::insert_unique_check(const_iterator,const KeyType&,KeyValueCompare,insert_commit_data&) template std::pair insert_check (const_iterator hint, const KeyType &key ,KeyValueCompare key_value_comp, insert_commit_data &commit_data) - { return tree_.insert_unique_check(hint, key, key_value_comp, commit_data); } - - //! Requires: value must be an lvalue of type value_type. commit_data - //! must have been obtained from a previous call to "insert_check". - //! No objects should have been inserted or erased from the avl_set between - //! the "insert_check" that filled "commit_data" and the call to "insert_commit". - //! - //! Effects: Inserts the value in the avl_set using the information obtained - //! from the "commit_data" that a previous "insert_check" filled. - //! - //! Returns: An iterator to the newly inserted object. - //! - //! Complexity: Constant time. - //! - //! Throws: Nothing. - //! - //! Notes: This function has only sense if a "insert_check" has been - //! previously executed to fill "commit_data". No value should be inserted or - //! erased between the "insert_check" and "insert_commit" calls. - iterator insert_commit(reference value, const insert_commit_data &commit_data) - { return tree_.insert_unique_commit(value, commit_data); } - - //! Requires: Dereferencing iterator must yield an lvalue - //! of type value_type. - //! - //! Effects: Inserts a range into the avl_set. - //! - //! Complexity: Insert range is in general O(N * log(N)), where N is the - //! size of the range. However, it is linear in N if the range is already sorted - //! by value_comp(). - //! - //! Throws: If the internal value_compare ordering function throws. Basic guarantee. - //! - //! Note: Does not affect the validity of iterators and references. - //! No copy-constructors are called. + { return tree_type::insert_unique_check(hint, key, key_value_comp, commit_data); } + + //! @copydoc ::boost::intrusive::avltree::insert_unique(Iterator,Iterator) template void insert(Iterator b, Iterator e) - { tree_.insert_unique(b, e); } - - //! Requires: value must be an lvalue, "pos" must be - //! a valid iterator (or end) and must be the succesor of value - //! once inserted according to the predicate. "value" must not be equal to any - //! inserted key according to the predicate. - //! - //! Effects: Inserts x into the tree before "pos". - //! - //! Complexity: Constant time. - //! - //! Throws: Nothing. - //! - //! Note: This function does not check preconditions so if "pos" is not - //! the successor of "value" or "value" is not unique tree ordering and uniqueness - //! invariants will be broken respectively. - //! This is a low-level function to be used only for performance reasons - //! by advanced users. - iterator insert_before(const_iterator pos, reference value) - { return tree_.insert_before(pos, value); } - - //! Requires: value must be an lvalue, and it must be greater than - //! any inserted key according to the predicate. - //! - //! Effects: Inserts x into the tree in the last position. - //! - //! Complexity: Constant time. - //! - //! Throws: Nothing. - //! - //! Note: This function does not check preconditions so if value is - //! less than or equal to the greatest inserted key tree ordering invariant will be broken. - //! This function is slightly more efficient than using "insert_before". - //! This is a low-level function to be used only for performance reasons - //! by advanced users. - void push_back(reference value) - { tree_.push_back(value); } - - //! Requires: value must be an lvalue, and it must be less - //! than any inserted key according to the predicate. - //! - //! Effects: Inserts x into the tree in the first position. - //! - //! Complexity: Constant time. - //! - //! Throws: Nothing. - //! - //! Note: This function does not check preconditions so if value is - //! greater than or equal to the the mimum inserted key tree ordering or uniqueness - //! invariants will be broken. - //! This function is slightly more efficient than using "insert_before". - //! This is a low-level function to be used only for performance reasons - //! by advanced users. - void push_front(reference value) - { tree_.push_front(value); } - - //! Effects: Erases the element pointed to by pos. - //! - //! Complexity: Average complexity is constant time. - //! - //! Returns: An iterator to the element after the erased element. - //! - //! Throws: Nothing. - //! - //! Note: Invalidates the iterators (but not the references) - //! to the erased elements. No destructors are called. - iterator erase(const_iterator i) - { return tree_.erase(i); } - - //! Effects: Erases the range pointed to by b end e. - //! - //! Complexity: Average complexity for erase range is at most - //! O(log(size() + N)), where N is the number of elements in the range. - //! - //! Returns: An iterator to the element after the erased elements. - //! - //! Throws: Nothing. - //! - //! Note: Invalidates the iterators (but not the references) - //! to the erased elements. No destructors are called. - iterator erase(const_iterator b, const_iterator e) - { return tree_.erase(b, e); } - - //! Effects: Erases all the elements with the given value. - //! - //! Returns: The number of erased elements. - //! - //! Complexity: O(log(size()) + this->count(value)). - //! - //! Throws: If the internal value_compare ordering function throws. Basic guarantee. - //! - //! Note: Invalidates the iterators (but not the references) - //! to the erased elements. No destructors are called. - size_type erase(const_reference value) - { return tree_.erase(value); } - - //! Effects: Erases all the elements that compare equal with - //! the given key and the given comparison functor. - //! - //! Returns: The number of erased elements. - //! - //! Complexity: O(log(size() + this->count(key, comp)). - //! - //! Throws: If the comp ordering function throws. Basic guarantee. - //! - //! Note: Invalidates the iterators (but not the references) - //! to the erased elements. No destructors are called. + { tree_type::insert_unique(b, e); } + + //! @copydoc ::boost::intrusive::avltree::insert_unique_commit + iterator insert_commit(reference value, const insert_commit_data &commit_data) + { return tree_type::insert_unique_commit(value, commit_data); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + //! @copydoc ::boost::intrusive::avltree::insert_before + iterator insert_before(const_iterator pos, reference value); + + //! @copydoc ::boost::intrusive::avltree::push_back + void push_back(reference value); + + //! @copydoc ::boost::intrusive::avltree::push_front + void push_front(reference value); + + //! @copydoc ::boost::intrusive::avltree::erase(const_iterator) + iterator erase(const_iterator i); + + //! @copydoc ::boost::intrusive::avltree::erase(const_iterator,const_iterator) + iterator erase(const_iterator b, const_iterator e); + + //! @copydoc ::boost::intrusive::avltree::erase(const_reference) + size_type erase(const_reference value); + + //! @copydoc ::boost::intrusive::avltree::erase(const KeyType&,KeyValueCompare) template - size_type erase(const KeyType& key, KeyValueCompare comp - /// @cond - , typename detail::enable_if_c::value >::type * = 0 - /// @endcond - ) - { return tree_.erase(key, comp); } - - //! Requires: Disposer::operator()(pointer) shouldn't throw. - //! - //! Effects: Erases the element pointed to by pos. - //! Disposer::operator()(pointer) is called for the removed element. - //! - //! Complexity: Average complexity for erase element is constant time. - //! - //! Returns: An iterator to the element after the erased element. - //! - //! Throws: Nothing. - //! - //! Note: Invalidates the iterators - //! to the erased elements. - template - iterator erase_and_dispose(const_iterator i, Disposer disposer) - { return tree_.erase_and_dispose(i, disposer); } + size_type erase(const KeyType& key, KeyValueCompare comp); - #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + //! @copydoc ::boost::intrusive::avltree::erase_and_dispose(const_iterator,Disposer) template - iterator erase_and_dispose(iterator i, Disposer disposer) - { return this->erase_and_dispose(const_iterator(i), disposer); } - #endif + iterator erase_and_dispose(const_iterator i, Disposer disposer); - //! Requires: Disposer::operator()(pointer) shouldn't throw. - //! - //! Effects: Erases the range pointed to by b end e. - //! Disposer::operator()(pointer) is called for the removed elements. - //! - //! Complexity: Average complexity for erase range is at most - //! O(log(size() + N)), where N is the number of elements in the range. - //! - //! Returns: An iterator to the element after the erased elements. - //! - //! Throws: Nothing. - //! - //! Note: Invalidates the iterators - //! to the erased elements. + //! @copydoc ::boost::intrusive::avltree::erase_and_dispose(const_iterator,const_iterator,Disposer) template - iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer) - { return tree_.erase_and_dispose(b, e, disposer); } - - //! Requires: Disposer::operator()(pointer) shouldn't throw. - //! - //! Effects: Erases all the elements with the given value. - //! Disposer::operator()(pointer) is called for the removed elements. - //! - //! Throws: If the internal value_compare ordering function throws. - //! - //! Complexity: O(log(size() + this->count(value)). Basic guarantee. - //! - //! Throws: Nothing. - //! - //! Note: Invalidates the iterators (but not the references) - //! to the erased elements. No destructors are called. + iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer); + + //! @copydoc ::boost::intrusive::avltree::erase_and_dispose(const_reference, Disposer) template - size_type erase_and_dispose(const_reference value, Disposer disposer) - { return tree_.erase_and_dispose(value, disposer); } - - //! Requires: Disposer::operator()(pointer) shouldn't throw. - //! - //! Effects: Erases all the elements with the given key. - //! according to the comparison functor "comp". - //! Disposer::operator()(pointer) is called for the removed elements. - //! - //! Returns: The number of erased elements. - //! - //! Complexity: O(log(size() + this->count(key, comp)). - //! - //! Throws: If comp ordering function throws. Basic guarantee. - //! - //! Note: Invalidates the iterators - //! to the erased elements. + size_type erase_and_dispose(const_reference value, Disposer disposer); + + //! @copydoc ::boost::intrusive::avltree::erase_and_dispose(const KeyType&,KeyValueCompare,Disposer) template - size_type erase_and_dispose(const KeyType& key, KeyValueCompare comp, Disposer disposer - /// @cond - , typename detail::enable_if_c::value >::type * = 0 - /// @endcond - ) - { return tree_.erase_and_dispose(key, comp, disposer); } - - //! Effects: Erases all the elements of the container. - //! - //! Complexity: Linear to the number of elements on the container. - //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise. - //! - //! Throws: Nothing. - //! - //! Note: Invalidates the iterators (but not the references) - //! to the erased elements. No destructors are called. - void clear() - { return tree_.clear(); } - - //! Requires: Disposer::operator()(pointer) shouldn't throw. - //! - //! Effects: Erases all the elements of the container. - //! - //! Complexity: Linear to the number of elements on the container. - //! Disposer::operator()(pointer) is called for the removed elements. - //! - //! Throws: Nothing. - //! - //! Note: Invalidates the iterators (but not the references) - //! to the erased elements. No destructors are called. + size_type erase_and_dispose(const KeyType& key, KeyValueCompare comp, Disposer disposer); + + //! @copydoc ::boost::intrusive::avltree::clear + void clear(); + + //! @copydoc ::boost::intrusive::avltree::clear_and_dispose template - void clear_and_dispose(Disposer disposer) - { return tree_.clear_and_dispose(disposer); } - - //! Effects: Returns the number of contained elements with the given key - //! - //! Complexity: Logarithmic to the number of elements contained plus lineal - //! to number of objects with the given key. - //! - //! Throws: If the internal value_compare ordering function throws. + void clear_and_dispose(Disposer disposer); + + #endif // #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::avltree::count(const_reference)const size_type count(const_reference value) const - { return tree_.find(value) != end(); } - - //! Effects: Returns the number of contained elements with the same key - //! compared with the given comparison functor. - //! - //! Complexity: Logarithmic to the number of elements contained plus lineal - //! to number of objects with the given key. - //! - //! Throws: If comp ordering function throws. + { return static_cast(this->tree_type::find(value) != this->tree_type::cend()); } + + //! @copydoc ::boost::intrusive::avltree::count(const KeyType&,KeyValueCompare)const template size_type count(const KeyType& key, KeyValueCompare comp) const - { return tree_.find(key, comp) != end(); } - - //! Effects: Returns an iterator to the first element whose - //! key is not less than k or end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If the internal value_compare ordering function throws. - iterator lower_bound(const_reference value) - { return tree_.lower_bound(value); } - - //! Requires: comp must imply the same element order as - //! value_compare. Usually key is the part of the value_type - //! that is used in the ordering functor. - //! - //! Effects: Returns an iterator to the first element whose - //! key according to the comparison functor is not less than k or - //! end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If comp ordering function throws. - //! - //! Note: This function is used when constructing a value_type - //! is expensive and the value_type can be compared with a cheaper - //! key type. Usually this key is part of the value_type. + { return static_cast(this->tree_type::find(key, comp) != this->tree_type::cend()); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::avltree::lower_bound(const_reference) + iterator lower_bound(const_reference value); + + //! @copydoc ::boost::intrusive::avltree::lower_bound(const KeyType&,KeyValueCompare) template - iterator lower_bound(const KeyType& key, KeyValueCompare comp) - { return tree_.lower_bound(key, comp); } - - //! Effects: Returns a const iterator to the first element whose - //! key is not less than k or end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If the internal value_compare ordering function throws. - const_iterator lower_bound(const_reference value) const - { return tree_.lower_bound(value); } - - //! Requires: comp must imply the same element order as - //! value_compare. Usually key is the part of the value_type - //! that is used in the ordering functor. - //! - //! Effects: Returns a const_iterator to the first element whose - //! key according to the comparison functor is not less than k or - //! end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If comp ordering function throws. - //! - //! Note: This function is used when constructing a value_type - //! is expensive and the value_type can be compared with a cheaper - //! key type. Usually this key is part of the value_type. + iterator lower_bound(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::avltree::lower_bound(const_reference)const + const_iterator lower_bound(const_reference value) const; + + //! @copydoc ::boost::intrusive::avltree::lower_bound(const KeyType&,KeyValueCompare)const template - const_iterator lower_bound(const KeyType& key, KeyValueCompare comp) const - { return tree_.lower_bound(key, comp); } - - //! Effects: Returns an iterator to the first element whose - //! key is greater than k or end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If the internal value_compare ordering function throws. - iterator upper_bound(const_reference value) - { return tree_.upper_bound(value); } - - //! Requires: comp must imply the same element order as - //! value_compare. Usually key is the part of the value_type - //! that is used in the ordering functor. - //! - //! Effects: Returns an iterator to the first element whose - //! key according to the comparison functor is greater than key or - //! end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If comp ordering function throws. - //! - //! Note: This function is used when constructing a value_type - //! is expensive and the value_type can be compared with a cheaper - //! key type. Usually this key is part of the value_type. + const_iterator lower_bound(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::avltree::upper_bound(const_reference) + iterator upper_bound(const_reference value); + + //! @copydoc ::boost::intrusive::avltree::upper_bound(const KeyType&,KeyValueCompare) template - iterator upper_bound(const KeyType& key, KeyValueCompare comp) - { return tree_.upper_bound(key, comp); } - - //! Effects: Returns an iterator to the first element whose - //! key is greater than k or end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If the internal value_compare ordering function throws. - const_iterator upper_bound(const_reference value) const - { return tree_.upper_bound(value); } - - //! Requires: comp must imply the same element order as - //! value_compare. Usually key is the part of the value_type - //! that is used in the ordering functor. - //! - //! Effects: Returns a const_iterator to the first element whose - //! key according to the comparison functor is greater than key or - //! end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If comp ordering function throws. - //! - //! Note: This function is used when constructing a value_type - //! is expensive and the value_type can be compared with a cheaper - //! key type. Usually this key is part of the value_type. + iterator upper_bound(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::avltree::upper_bound(const_reference)const + const_iterator upper_bound(const_reference value) const; + + //! @copydoc ::boost::intrusive::avltree::upper_bound(const KeyType&,KeyValueCompare)const template - const_iterator upper_bound(const KeyType& key, KeyValueCompare comp) const - { return tree_.upper_bound(key, comp); } - - //! Effects: Finds an iterator to the first element whose value is - //! "value" or end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If the internal value_compare ordering function throws. - iterator find(const_reference value) - { return tree_.find(value); } - - //! Requires: comp must imply the same element order as - //! value_compare. Usually key is the part of the value_type - //! that is used in the ordering functor. - //! - //! Effects: Finds an iterator to the first element whose key is - //! "key" according to the comparison functor or end() if that element - //! does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If comp ordering function throws. - //! - //! Note: This function is used when constructing a value_type - //! is expensive and the value_type can be compared with a cheaper - //! key type. Usually this key is part of the value_type. + const_iterator upper_bound(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::avltree::find(const_reference) + iterator find(const_reference value); + + //! @copydoc ::boost::intrusive::avltree::find(const KeyType&,KeyValueCompare) template - iterator find(const KeyType& key, KeyValueCompare comp) - { return tree_.find(key, comp); } - - //! Effects: Finds a const_iterator to the first element whose value is - //! "value" or end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If the internal value_compare ordering function throws. - const_iterator find(const_reference value) const - { return tree_.find(value); } - - //! Requires: comp must imply the same element order as - //! value_compare. Usually key is the part of the value_type - //! that is used in the ordering functor. - //! - //! Effects: Finds a const_iterator to the first element whose key is - //! "key" according to the comparison functor or end() if that element - //! does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If comp ordering function throws. - //! - //! Note: This function is used when constructing a value_type - //! is expensive and the value_type can be compared with a cheaper - //! key type. Usually this key is part of the value_type. + iterator find(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::avltree::find(const_reference)const + const_iterator find(const_reference value) const; + + //! @copydoc ::boost::intrusive::avltree::find(const KeyType&,KeyValueCompare)const template - const_iterator find(const KeyType& key, KeyValueCompare comp) const - { return tree_.find(key, comp); } - - //! Effects: Finds a range containing all elements whose key is k or - //! an empty range that indicates the position where those elements would be - //! if they there is no elements with key k. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If the internal value_compare ordering function throws. + const_iterator find(const KeyType& key, KeyValueCompare comp) const; + + #endif // #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::rbtree::equal_range(const_reference) std::pair equal_range(const_reference value) - { return tree_.equal_range(value); } - - //! Requires: comp must imply the same element order as - //! value_compare. Usually key is the part of the value_type - //! that is used in the ordering functor. - //! - //! Effects: Finds a range containing all elements whose key is k - //! according to the comparison functor or an empty range - //! that indicates the position where those elements would be - //! if they there is no elements with key k. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If comp ordering function throws. - //! - //! Note: This function is used when constructing a value_type - //! is expensive and the value_type can be compared with a cheaper - //! key type. Usually this key is part of the value_type. + { return this->tree_type::lower_bound_range(value); } + + //! @copydoc ::boost::intrusive::rbtree::equal_range(const KeyType&,KeyValueCompare) template std::pair equal_range(const KeyType& key, KeyValueCompare comp) - { return tree_.equal_range(key, comp); } - - //! Effects: Finds a range containing all elements whose key is k or - //! an empty range that indicates the position where those elements would be - //! if they there is no elements with key k. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If the internal value_compare ordering function throws. + { return this->tree_type::lower_bound_range(key, comp); } + + //! @copydoc ::boost::intrusive::rbtree::equal_range(const_reference)const std::pair equal_range(const_reference value) const - { return tree_.equal_range(value); } - - //! Requires: comp must imply the same element order as - //! value_compare. Usually key is the part of the value_type - //! that is used in the ordering functor. - //! - //! Effects: Finds a range containing all elements whose key is k - //! according to the comparison functor or an empty range - //! that indicates the position where those elements would be - //! if they there is no elements with key k. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If comp ordering function throws. - //! - //! Note: This function is used when constructing a value_type - //! is expensive and the value_type can be compared with a cheaper - //! key type. Usually this key is part of the value_type. + { return this->tree_type::lower_bound_range(value); } + + //! @copydoc ::boost::intrusive::rbtree::equal_range(const KeyType&,KeyValueCompare)const template std::pair equal_range(const KeyType& key, KeyValueCompare comp) const - { return tree_.equal_range(key, comp); } - - //! Requires: 'lower_value' must not be greater than 'upper_value'. If - //! 'lower_value' == 'upper_value', ('left_closed' || 'right_closed') must be false. - //! - //! Effects: Returns an a pair with the following criteria: - //! - //! first = lower_bound(lower_key) if left_closed, upper_bound(lower_key) otherwise - //! - //! second = upper_bound(upper_key) if right_closed, lower_bound(upper_key) otherwise - //! - //! Complexity: Logarithmic. - //! - //! Throws: If the predicate throws. - //! - //! Note: This function can be more efficient than calling upper_bound - //! and lower_bound for lower_value and upper_value. + { return this->tree_type::lower_bound_range(key, comp); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::avltree::bounded_range(const_reference,const_reference,bool,bool) std::pair bounded_range - (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed) - { return tree_.bounded_range(lower_value, upper_value, left_closed, right_closed); } - - //! Requires: KeyValueCompare is a function object that induces a strict weak - //! ordering compatible with the strict weak ordering used to create the - //! the tree. - //! 'lower_key' must not be greater than 'upper_key' according to 'comp'. If - //! 'lower_key' == 'upper_key', ('left_closed' || 'right_closed') must be false. - //! - //! Effects: Returns an a pair with the following criteria: - //! - //! first = lower_bound(lower_key, comp) if left_closed, upper_bound(lower_key, comp) otherwise - //! - //! second = upper_bound(upper_key, comp) if right_closed, lower_bound(upper_key, comp) otherwise - //! - //! Complexity: Logarithmic. - //! - //! Throws: If "comp" throws. - //! - //! Note: This function can be more efficient than calling upper_bound - //! and lower_bound for lower_key and upper_key. + (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed); + + //! @copydoc ::boost::intrusive::avltree::bounded_range(const KeyType&,const KeyType&,KeyValueCompare,bool,bool) template std::pair bounded_range - (const KeyType& lower_key, const KeyType& upper_key, KeyValueCompare comp, bool left_closed, bool right_closed) - { return tree_.bounded_range(lower_key, upper_key, comp, left_closed, right_closed); } - - //! Requires: 'lower_value' must not be greater than 'upper_value'. If - //! 'lower_value' == 'upper_value', ('left_closed' || 'right_closed') must be false. - //! - //! Effects: Returns an a pair with the following criteria: - //! - //! first = lower_bound(lower_key) if left_closed, upper_bound(lower_key) otherwise - //! - //! second = upper_bound(upper_key) if right_closed, lower_bound(upper_key) otherwise - //! - //! Complexity: Logarithmic. - //! - //! Throws: If the predicate throws. - //! - //! Note: This function can be more efficient than calling upper_bound - //! and lower_bound for lower_value and upper_value. + (const KeyType& lower_key, const KeyType& upper_key, KeyValueCompare comp, bool left_closed, bool right_closed); + + //! @copydoc ::boost::intrusive::avltree::bounded_range(const_reference,const_reference,bool,bool)const std::pair - bounded_range(const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed) const - { return tree_.bounded_range(lower_value, upper_value, left_closed, right_closed); } - - //! Requires: KeyValueCompare is a function object that induces a strict weak - //! ordering compatible with the strict weak ordering used to create the - //! the tree. - //! 'lower_key' must not be greater than 'upper_key' according to 'comp'. If - //! 'lower_key' == 'upper_key', ('left_closed' || 'right_closed') must be false. - //! - //! Effects: Returns an a pair with the following criteria: - //! - //! first = lower_bound(lower_key, comp) if left_closed, upper_bound(lower_key, comp) otherwise - //! - //! second = upper_bound(upper_key, comp) if right_closed, lower_bound(upper_key, comp) otherwise - //! - //! Complexity: Logarithmic. - //! - //! Throws: If "comp" throws. - //! - //! Note: This function can be more efficient than calling upper_bound - //! and lower_bound for lower_key and upper_key. + bounded_range(const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed) const; + + //! @copydoc ::boost::intrusive::avltree::bounded_range(const KeyType&,const KeyType&,KeyValueCompare,bool,bool)const template - std::pair - bounded_range - (const KeyType& lower_key, const KeyType& upper_key, KeyValueCompare comp, bool left_closed, bool right_closed) const - { return tree_.bounded_range(lower_key, upper_key, comp, left_closed, right_closed); } - - //! Requires: value must be an lvalue and shall be in a avl_set of - //! appropriate type. Otherwise the behavior is undefined. - //! - //! Effects: Returns: a valid iterator i belonging to the avl_set - //! that points to the value - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - //! - //! Note: This static function is available only if the value traits - //! is stateless. - static iterator s_iterator_to(reference value) - { return tree_type::s_iterator_to(value); } - - //! Requires: value must be an lvalue and shall be in a avl_set of - //! appropriate type. Otherwise the behavior is undefined. - //! - //! Effects: Returns: a valid const_iterator i belonging to the - //! avl_set that points to the value - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - //! - //! Note: This static function is available only if the value traits - //! is stateless. - static const_iterator s_iterator_to(const_reference value) - { return tree_type::s_iterator_to(value); } - - //! Requires: value must be an lvalue and shall be in a avl_set of - //! appropriate type. Otherwise the behavior is undefined. - //! - //! Effects: Returns: a valid iterator i belonging to the avl_set - //! that points to the value - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - iterator iterator_to(reference value) - { return tree_.iterator_to(value); } - - //! Requires: value must be an lvalue and shall be in a avl_set of - //! appropriate type. Otherwise the behavior is undefined. - //! - //! Effects: Returns: a valid const_iterator i belonging to the - //! avl_set that points to the value - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_iterator iterator_to(const_reference value) const - { return tree_.iterator_to(value); } - - //! Requires: value shall not be in a avl_set/avl_multiset. - //! - //! Effects: init_node puts the hook of a value in a well-known default - //! state. - //! - //! Throws: Nothing. - //! - //! Complexity: Constant time. - //! - //! Note: This function puts the hook in the well-known default state - //! used by auto_unlink and safe hooks. - static void init_node(reference value) - { tree_type::init_node(value); } - - //! Effects: Unlinks the leftmost node from the tree. - //! - //! Complexity: Average complexity is constant time. - //! - //! Throws: Nothing. - //! - //! Notes: 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(); } - - //! Requires: replace_this must be a valid iterator of *this - //! and with_this must not be inserted in any tree. - //! - //! Effects: Replaces replace_this in its position in the - //! tree with with_this. The tree does not need to be rebalanced. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - //! - //! Note: This function will break container ordering invariants if - //! with_this is not equivalent to *replace_this according to the - //! ordering rules. This function is faster than erasing and inserting - //! the node, since no rebalancing or comparison is needed. - void replace_node(iterator replace_this, reference with_this) - { tree_.replace_node(replace_this, with_this); } + std::pair bounded_range + (const KeyType& lower_key, const KeyType& upper_key, KeyValueCompare comp, bool left_closed, bool right_closed) const; - /// @cond - friend bool operator==(const avl_set_impl &x, const avl_set_impl &y) - { return x.tree_ == y.tree_; } + //! @copydoc ::boost::intrusive::avltree::s_iterator_to(reference) + static iterator s_iterator_to(reference value); - friend bool operator<(const avl_set_impl &x, const avl_set_impl &y) - { return x.tree_ < y.tree_; } - /// @endcond + //! @copydoc ::boost::intrusive::avltree::s_iterator_to(const_reference) + static const_iterator s_iterator_to(const_reference value); + + //! @copydoc ::boost::intrusive::avltree::iterator_to(reference) + iterator iterator_to(reference value); + + //! @copydoc ::boost::intrusive::avltree::iterator_to(const_reference)const + const_iterator iterator_to(const_reference value) const; + + //! @copydoc ::boost::intrusive::avltree::init_node(reference) + static void init_node(reference value); + + //! @copydoc ::boost::intrusive::avltree::unlink_leftmost_without_rebalance + pointer unlink_leftmost_without_rebalance(); + + //! @copydoc ::boost::intrusive::avltree::replace_node + void replace_node(iterator replace_this, reference with_this); + + //! @copydoc ::boost::intrusive::avltree::remove_node + void remove_node(reference value); + #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED }; #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + template -#else -template -#endif -inline bool operator!= -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) -(const avl_set_impl &x, const avl_set_impl &y) -#else -(const avl_set_impl &x, const avl_set_impl &y) -#endif -{ return !(x == y); } +bool operator!= (const avl_set_impl &x, const avl_set_impl &y); -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) template -#else -template -#endif -inline bool operator> -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) -(const avl_set_impl &x, const avl_set_impl &y) -#else -(const avl_set_impl &x, const avl_set_impl &y) -#endif -{ return y < x; } +bool operator>(const avl_set_impl &x, const avl_set_impl &y); -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) template -#else -template -#endif -inline bool operator<= -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) -(const avl_set_impl &x, const avl_set_impl &y) -#else -(const avl_set_impl &x, const avl_set_impl &y) -#endif -{ return !(y < x); } +bool operator<=(const avl_set_impl &x, const avl_set_impl &y); -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) template -#else -template -#endif -inline bool operator>= -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) -(const avl_set_impl &x, const avl_set_impl &y) -#else -(const avl_set_impl &x, const avl_set_impl &y) -#endif -{ return !(x < y); } +bool operator>=(const avl_set_impl &x, const avl_set_impl &y); -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) template -#else -template -#endif -inline void swap -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) -(avl_set_impl &x, avl_set_impl &y) -#else -(avl_set_impl &x, avl_set_impl &y) -#endif -{ x.swap(y); } +void swap(avl_set_impl &x, avl_set_impl &y); + +#endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) -//! Helper metafunction to define a \c avl_set that yields to the same type when the +//! Helper metafunction to define a \c set that yields to the same type when the //! same options (either explicitly or implicitly) are used. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) template #else -template +template #endif struct make_avl_set { /// @cond - typedef avl_set_impl - < typename make_avltree_opt + typedef typename pack_options + < avltree_defaults, #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) - + O1, O2, O3, O4, O5 #else - + Options... #endif - ::type - > implementation_defined; + >::type packed_options; + + typedef typename detail::get_value_traits + ::type value_traits; + typedef typename detail::get_header_holder_type + < value_traits, typename packed_options::header_holder_type >::type header_holder_type; + + typedef avl_set_impl + < value_traits + , typename packed_options::compare + , typename packed_options::size_type + , packed_options::constant_time_size + , header_holder_type + > implementation_defined; /// @endcond typedef implementation_defined type; }; #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED - #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) -template +template #else template #endif class avl_set - : public make_avl_set - #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) - - #else - - #endif - ::type + : public make_avl_set::type { typedef typename make_avl_set + + O1, O2, O3, O4, O5 #else - + Options... #endif - ::type Base; + >::type Base; BOOST_MOVABLE_BUT_NOT_COPYABLE(avl_set) public: @@ -1284,8 +466,8 @@ class avl_set //Assert if passed value traits are compatible with the type BOOST_STATIC_ASSERT((detail::is_same::value)); - avl_set( const value_compare &cmp = value_compare() - , const value_traits &v_traits = value_traits()) + explicit avl_set( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) : Base(cmp, v_traits) {} @@ -1301,7 +483,7 @@ class avl_set {} avl_set& operator=(BOOST_RV_REF(avl_set) x) - { this->Base::operator=(::boost::move(static_cast(x))); return *this; } + { return static_cast(this->Base::operator=(::boost::move(static_cast(x)))); } static avl_set &container_from_end_iterator(iterator end_iterator) { return static_cast(Base::container_from_end_iterator(end_iterator)); } @@ -1309,17 +491,17 @@ class avl_set static const avl_set &container_from_end_iterator(const_iterator end_iterator) { return static_cast(Base::container_from_end_iterator(end_iterator)); } - static avl_set &container_from_iterator(iterator end_iterator) - { return static_cast(Base::container_from_iterator(end_iterator)); } + static avl_set &container_from_iterator(iterator it) + { return static_cast(Base::container_from_iterator(it)); } - static const avl_set &container_from_iterator(const_iterator end_iterator) - { return static_cast(Base::container_from_iterator(end_iterator)); } + static const avl_set &container_from_iterator(const_iterator it) + { return static_cast(Base::container_from_iterator(it)); } }; #endif //! The class template avl_multiset is an intrusive container, that mimics most of -//! the interface of std::avl_multiset as described in the C++ standard. +//! the interface of std::_multiset as described in the C++ standard. //! //! The template parameter \c T is the type to be managed by the container. //! The user can specify additional options and if no options are provided @@ -1332,14 +514,16 @@ class avl_set #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) template #else -template +template #endif class avl_multiset_impl +#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + : public bstree_impl +#endif { /// @cond - typedef avltree_impl tree_type; + typedef bstree_impl tree_type; - //Movable BOOST_MOVABLE_BUT_NOT_COPYABLE(avl_multiset_impl) typedef tree_type implementation_defined; /// @endcond @@ -1366,1088 +550,325 @@ class avl_multiset_impl typedef typename implementation_defined::const_node_ptr const_node_ptr; typedef typename implementation_defined::node_algorithms node_algorithms; - static const bool constant_time_size = Config::constant_time_size; - - /// @cond - private: - tree_type tree_; - /// @endcond + static const bool constant_time_size = tree_type::constant_time_size; public: - //! Effects: Constructs an empty avl_multiset. - //! - //! Complexity: Constant. - //! - //! Throws: If value_traits::node_traits::node - //! constructor throws (this does not happen with predefined Boost.Intrusive hooks) - //! or the copy constructor/operator() of the value_compare object throws. - avl_multiset_impl( const value_compare &cmp = value_compare() - , const value_traits &v_traits = value_traits()) - : tree_(cmp, v_traits) + //! @copydoc ::boost::intrusive::avltree::avltree(const value_compare &,const value_traits &) + explicit avl_multiset_impl( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : tree_type(cmp, v_traits) {} - //! Requires: Dereferencing iterator must yield an lvalue of type value_type. - //! cmp must be a comparison function that induces a strict weak ordering. - //! - //! Effects: Constructs an empty avl_multiset and inserts elements from - //! [b, e). - //! - //! Complexity: Linear in N if [b, e) is already sorted using - //! comp and otherwise N * log N, where N is the distance between first and last - //! - //! Throws: If value_traits::node_traits::node - //! constructor throws (this does not happen with predefined Boost.Intrusive hooks) - //! or the copy constructor/operator() of the value_compare object throws. + //! @copydoc ::boost::intrusive::avltree::avltree(bool,Iterator,Iterator,const value_compare &,const value_traits &) template avl_multiset_impl( Iterator b, Iterator e , const value_compare &cmp = value_compare() , const value_traits &v_traits = value_traits()) - : tree_(false, b, e, cmp, v_traits) + : tree_type(false, b, e, cmp, v_traits) {} - //! Effects: to-do - //! + //! @copydoc ::boost::intrusive::avltree::avltree(avltree &&) avl_multiset_impl(BOOST_RV_REF(avl_multiset_impl) x) - : tree_(::boost::move(x.tree_)) + : tree_type(::boost::move(static_cast(x))) {} - //! Effects: to-do - //! + //! @copydoc ::boost::intrusive::avltree::operator=(avltree &&) avl_multiset_impl& operator=(BOOST_RV_REF(avl_multiset_impl) x) - { tree_ = ::boost::move(x.tree_); return *this; } - - //! Effects: Detaches all elements from this. The objects in the avl_multiset - //! are not deleted (i.e. no destructors are called). - //! - //! Complexity: Linear to the number of elements on the container. - //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise. - //! - //! Throws: Nothing. - ~avl_multiset_impl() - {} + { return static_cast(tree_type::operator=(::boost::move(static_cast(x)))); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + //! @copydoc ::boost::intrusive::avltree::~avltree() + ~avl_multiset_impl(); + + //! @copydoc ::boost::intrusive::avltree::begin() + iterator begin(); + + //! @copydoc ::boost::intrusive::avltree::begin()const + const_iterator begin() const; + + //! @copydoc ::boost::intrusive::avltree::cbegin()const + const_iterator cbegin() const; + + //! @copydoc ::boost::intrusive::avltree::end() + iterator end(); + + //! @copydoc ::boost::intrusive::avltree::end()const + const_iterator end() const; + + //! @copydoc ::boost::intrusive::avltree::cend()const + const_iterator cend() const; - //! Effects: Returns an iterator pointing to the beginning of the avl_multiset. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - iterator begin() - { return tree_.begin(); } - - //! Effects: Returns a const_iterator pointing to the beginning of the avl_multiset. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_iterator begin() const - { return tree_.begin(); } - - //! Effects: Returns a const_iterator pointing to the beginning of the avl_multiset. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_iterator cbegin() const - { return tree_.cbegin(); } - - //! Effects: Returns an iterator pointing to the end of the avl_multiset. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - iterator end() - { return tree_.end(); } - - //! Effects: Returns a const_iterator pointing to the end of the avl_multiset. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_iterator end() const - { return tree_.end(); } - - //! Effects: Returns a const_iterator pointing to the end of the avl_multiset. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_iterator cend() const - { return tree_.cend(); } - - //! Effects: Returns a reverse_iterator pointing to the beginning of the - //! reversed avl_multiset. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - reverse_iterator rbegin() - { return tree_.rbegin(); } - - //! Effects: Returns a const_reverse_iterator pointing to the beginning - //! of the reversed avl_multiset. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_reverse_iterator rbegin() const - { return tree_.rbegin(); } - - //! Effects: Returns a const_reverse_iterator pointing to the beginning - //! of the reversed avl_multiset. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_reverse_iterator crbegin() const - { return tree_.crbegin(); } - - //! Effects: Returns a reverse_iterator pointing to the end - //! of the reversed avl_multiset. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - reverse_iterator rend() - { return tree_.rend(); } - - //! Effects: Returns a const_reverse_iterator pointing to the end - //! of the reversed avl_multiset. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_reverse_iterator rend() const - { return tree_.rend(); } - - //! Effects: Returns a const_reverse_iterator pointing to the end - //! of the reversed avl_multiset. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_reverse_iterator crend() const - { return tree_.crend(); } - - //! Precondition: end_iterator must be a valid end iterator - //! of avl_multiset. - //! - //! Effects: Returns a const reference to the avl_multiset associated to the end iterator - //! - //! Throws: Nothing. - //! - //! Complexity: Constant. - static avl_multiset_impl &container_from_end_iterator(iterator end_iterator) - { - return *detail::parent_from_member - ( &tree_type::container_from_end_iterator(end_iterator) - , &avl_multiset_impl::tree_); - } - - //! Precondition: end_iterator must be a valid end const_iterator - //! of avl_multiset. - //! - //! Effects: Returns a const reference to the avl_multiset associated to the end iterator - //! - //! Throws: Nothing. - //! - //! Complexity: Constant. - static const avl_multiset_impl &container_from_end_iterator(const_iterator end_iterator) - { - return *detail::parent_from_member - ( &tree_type::container_from_end_iterator(end_iterator) - , &avl_multiset_impl::tree_); - } - - //! Precondition: it must be a valid iterator of multiset. - //! - //! Effects: Returns a const reference to the multiset associated to the iterator - //! - //! Throws: Nothing. - //! - //! Complexity: Logarithmic. - static avl_multiset_impl &container_from_iterator(iterator it) - { - return *detail::parent_from_member - ( &tree_type::container_from_iterator(it) - , &avl_multiset_impl::tree_); - } - - //! Precondition: it must be a valid const_iterator of multiset. - //! - //! Effects: Returns a const reference to the multiset associated to the iterator - //! - //! Throws: Nothing. - //! - //! Complexity: Logarithmic. - static const avl_multiset_impl &container_from_iterator(const_iterator it) - { - return *detail::parent_from_member - ( &tree_type::container_from_iterator(it) - , &avl_multiset_impl::tree_); - } - - //! Effects: Returns the key_compare object used by the avl_multiset. - //! - //! Complexity: Constant. - //! - //! Throws: If key_compare copy-constructor throws. - key_compare key_comp() const - { return tree_.value_comp(); } - - //! Effects: Returns the value_compare object used by the avl_multiset. - //! - //! Complexity: Constant. - //! - //! Throws: If value_compare copy-constructor throws. - value_compare value_comp() const - { return tree_.value_comp(); } - - //! Effects: Returns true is the container is empty. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - bool empty() const - { return tree_.empty(); } - - //! Effects: Returns the number of elements stored in the avl_multiset. - //! - //! Complexity: Linear to elements contained in *this if, - //! constant-time size option is enabled. Constant-time otherwise. - //! - //! Throws: Nothing. - size_type size() const - { return tree_.size(); } - - //! Effects: Swaps the contents of two avl_multisets. - //! - //! Complexity: Constant. - //! - //! Throws: If the swap() call for the comparison functor - //! found using ADL throws. Strong guarantee. - void swap(avl_multiset_impl& other) - { tree_.swap(other.tree_); } - - //! Requires: Disposer::operator()(pointer) shouldn't throw. - //! Cloner should yield to nodes equivalent to the original nodes. - //! - //! Effects: 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. Copies the predicate from the source container. - //! - //! If cloner throws, all cloned elements are unlinked and disposed - //! calling Disposer::operator()(pointer). - //! - //! Complexity: Linear to erased plus inserted elements. - //! - //! Throws: If cloner throws or predicate copy assignment throws. Basic guarantee. + //! @copydoc ::boost::intrusive::avltree::rbegin() + reverse_iterator rbegin(); + + //! @copydoc ::boost::intrusive::avltree::rbegin()const + const_reverse_iterator rbegin() const; + + //! @copydoc ::boost::intrusive::avltree::crbegin()const + const_reverse_iterator crbegin() const; + + //! @copydoc ::boost::intrusive::avltree::rend() + reverse_iterator rend(); + + //! @copydoc ::boost::intrusive::avltree::rend()const + const_reverse_iterator rend() const; + + //! @copydoc ::boost::intrusive::avltree::crend()const + const_reverse_iterator crend() const; + + //! @copydoc ::boost::intrusive::avltree::container_from_end_iterator(iterator) + static avl_multiset_impl &container_from_end_iterator(iterator end_iterator); + + //! @copydoc ::boost::intrusive::avltree::container_from_end_iterator(const_iterator) + static const avl_multiset_impl &container_from_end_iterator(const_iterator end_iterator); + + //! @copydoc ::boost::intrusive::avltree::container_from_iterator(iterator) + static avl_multiset_impl &container_from_iterator(iterator it); + + //! @copydoc ::boost::intrusive::avltree::container_from_iterator(const_iterator) + static const avl_multiset_impl &container_from_iterator(const_iterator it); + + //! @copydoc ::boost::intrusive::avltree::key_comp()const + key_compare key_comp() const; + + //! @copydoc ::boost::intrusive::avltree::value_comp()const + value_compare value_comp() const; + + //! @copydoc ::boost::intrusive::avltree::empty()const + bool empty() const; + + //! @copydoc ::boost::intrusive::avltree::size()const + size_type size() const; + + //! @copydoc ::boost::intrusive::avltree::swap + void swap(avl_multiset_impl& other); + + //! @copydoc ::boost::intrusive::avltree::clone_from template - void clone_from(const avl_multiset_impl &src, Cloner cloner, Disposer disposer) - { tree_.clone_from(src.tree_, cloner, disposer); } - - //! Requires: value must be an lvalue - //! - //! Effects: Inserts value into the avl_multiset. - //! - //! Returns: An iterator that points to the position where the new - //! element was inserted. - //! - //! Complexity: Average complexity for insert element is at - //! most logarithmic. - //! - //! Throws: If the internal value_compare ordering function throws. Strong guarantee. - //! - //! Note: Does not affect the validity of iterators and references. - //! No copy-constructors are called. + void clone_from(const avl_multiset_impl &src, Cloner cloner, Disposer disposer); + + #endif //#ifdef BOOST_iNTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::avltree::insert_equal(reference) iterator insert(reference value) - { return tree_.insert_equal(value); } - - //! Requires: value must be an lvalue - //! - //! Effects: Inserts x into the avl_multiset, using pos as a hint to - //! where it will be inserted. - //! - //! Returns: An iterator that points to the position where the new - //! element was inserted. - //! - //! Complexity: Logarithmic in general, but it is amortized - //! constant time if t is inserted immediately before hint. - //! - //! Throws: If the internal value_compare ordering function throws. Strong guarantee. - //! - //! Note: Does not affect the validity of iterators and references. - //! No copy-constructors are called. + { return tree_type::insert_equal(value); } + + //! @copydoc ::boost::intrusive::avltree::insert_equal(const_iterator,reference) iterator insert(const_iterator hint, reference value) - { return tree_.insert_equal(hint, value); } - - //! Requires: Dereferencing iterator must yield an lvalue - //! of type value_type. - //! - //! Effects: Inserts a range into the avl_multiset. - //! - //! Returns: An iterator that points to the position where the new - //! element was inserted. - //! - //! Complexity: Insert range is in general O(N * log(N)), where N is the - //! size of the range. However, it is linear in N if the range is already sorted - //! by value_comp(). - //! - //! Throws: If the internal value_compare ordering function throws. Basic guarantee. - //! - //! Note: Does not affect the validity of iterators and references. - //! No copy-constructors are called. + { return tree_type::insert_equal(hint, value); } + + //! @copydoc ::boost::intrusive::avltree::insert_equal(Iterator,Iterator) template void insert(Iterator b, Iterator e) - { tree_.insert_equal(b, e); } - - //! Requires: value must be an lvalue, "pos" must be - //! a valid iterator (or end) and must be the succesor of value - //! once inserted according to the predicate. "value" must not be equal to any - //! inserted key according to the predicate. - //! - //! Effects: Inserts x into the tree before "pos". - //! - //! Complexity: Constant time. - //! - //! Throws: Nothing. - //! - //! Note: This function does not check preconditions so if "pos" is not - //! the successor of "value" or "value" is not unique tree ordering and uniqueness - //! invariants will be broken respectively. - //! This is a low-level function to be used only for performance reasons - //! by advanced users. - iterator insert_before(const_iterator pos, reference value) - { return tree_.insert_before(pos, value); } - - //! Requires: value must be an lvalue, and it must be greater than - //! any inserted key according to the predicate. - //! - //! Effects: Inserts x into the tree in the last position. - //! - //! Complexity: Constant time. - //! - //! Throws: Nothing. - //! - //! Note: This function does not check preconditions so if value is - //! less than or equal to the greatest inserted key tree ordering invariant will be broken. - //! This function is slightly more efficient than using "insert_before". - //! This is a low-level function to be used only for performance reasons - //! by advanced users. - void push_back(reference value) - { tree_.push_back(value); } - - //! Requires: value must be an lvalue, and it must be less - //! than any inserted key according to the predicate. - //! - //! Effects: Inserts x into the tree in the first position. - //! - //! Complexity: Constant time. - //! - //! Throws: Nothing. - //! - //! Note: This function does not check preconditions so if value is - //! greater than or equal to the the mimum inserted key tree ordering or uniqueness - //! invariants will be broken. - //! This function is slightly more efficient than using "insert_before". - //! This is a low-level function to be used only for performance reasons - //! by advanced users. - void push_front(reference value) - { tree_.push_front(value); } - - //! Effects: Erases the element pointed to by pos. - //! - //! Complexity: Average complexity is constant time. - //! - //! Returns: An iterator to the element after the erased element. - //! - //! Throws: Nothing. - //! - //! Note: Invalidates the iterators (but not the references) - //! to the erased elements. No destructors are called. - iterator erase(const_iterator i) - { return tree_.erase(i); } - - //! Effects: Erases the range pointed to by b end e. - //! - //! Returns: An iterator to the element after the erased elements. - //! - //! Complexity: Average complexity for erase range is at most - //! O(log(size() + N)), where N is the number of elements in the range. - //! - //! Throws: Nothing. - //! - //! Note: Invalidates the iterators (but not the references) - //! to the erased elements. No destructors are called. - iterator erase(const_iterator b, const_iterator e) - { return tree_.erase(b, e); } - - //! Effects: Erases all the elements with the given value. - //! - //! Returns: The number of erased elements. - //! - //! Complexity: O(log(size() + this->count(value)). - //! - //! Throws: If the internal value_compare ordering function throws. Basic guarantee. - //! - //! Note: Invalidates the iterators (but not the references) - //! to the erased elements. No destructors are called. - size_type erase(const_reference value) - { return tree_.erase(value); } - - //! Effects: Erases all the elements that compare equal with - //! the given key and the given comparison functor. - //! - //! Returns: The number of erased elements. - //! - //! Complexity: O(log(size() + this->count(key, comp)). - //! - //! Throws: If comp ordering function throws. Basic guarantee. - //! - //! Note: Invalidates the iterators (but not the references) - //! to the erased elements. No destructors are called. + { tree_type::insert_equal(b, e); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + //! @copydoc ::boost::intrusive::avltree::insert_before + iterator insert_before(const_iterator pos, reference value); + + //! @copydoc ::boost::intrusive::avltree::push_back + void push_back(reference value); + + //! @copydoc ::boost::intrusive::avltree::push_front + void push_front(reference value); + + //! @copydoc ::boost::intrusive::avltree::erase(const_iterator) + iterator erase(const_iterator i); + + //! @copydoc ::boost::intrusive::avltree::erase(const_iterator,const_iterator) + iterator erase(const_iterator b, const_iterator e); + + //! @copydoc ::boost::intrusive::avltree::erase(const_reference) + size_type erase(const_reference value); + + //! @copydoc ::boost::intrusive::avltree::erase(const KeyType&,KeyValueCompare) template - size_type erase(const KeyType& key, KeyValueCompare comp - /// @cond - , typename detail::enable_if_c::value >::type * = 0 - /// @endcond - ) - { return tree_.erase(key, comp); } - - //! Requires: Disposer::operator()(pointer) shouldn't throw. - //! - //! Returns: An iterator to the element after the erased element. - //! - //! Effects: Erases the element pointed to by pos. - //! Disposer::operator()(pointer) is called for the removed element. - //! - //! Complexity: Average complexity for erase element is constant time. - //! - //! Throws: Nothing. - //! - //! Note: Invalidates the iterators - //! to the erased elements. - template - iterator erase_and_dispose(const_iterator i, Disposer disposer) - { return tree_.erase_and_dispose(i, disposer); } + size_type erase(const KeyType& key, KeyValueCompare comp); - #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + //! @copydoc ::boost::intrusive::avltree::erase_and_dispose(const_iterator,Disposer) template - iterator erase_and_dispose(iterator i, Disposer disposer) - { return this->erase_and_dispose(const_iterator(i), disposer); } - #endif + iterator erase_and_dispose(const_iterator i, Disposer disposer); - //! Requires: Disposer::operator()(pointer) shouldn't throw. - //! - //! Returns: An iterator to the element after the erased elements. - //! - //! Effects: Erases the range pointed to by b end e. - //! Disposer::operator()(pointer) is called for the removed elements. - //! - //! Complexity: Average complexity for erase range is at most - //! O(log(size() + N)), where N is the number of elements in the range. - //! - //! Throws: Nothing. - //! - //! Note: Invalidates the iterators - //! to the erased elements. + //! @copydoc ::boost::intrusive::avltree::erase_and_dispose(const_iterator,const_iterator,Disposer) template - iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer) - { return tree_.erase_and_dispose(b, e, disposer); } - - //! Requires: Disposer::operator()(pointer) shouldn't throw. - //! - //! Effects: Erases all the elements with the given value. - //! Disposer::operator()(pointer) is called for the removed elements. - //! - //! Returns: The number of erased elements. - //! - //! Complexity: O(log(size() + this->count(value)). - //! - //! Throws: If the internal value_compare ordering function throws. Basic guarantee. - //! - //! Note: Invalidates the iterators (but not the references) - //! to the erased elements. No destructors are called. + iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer); + + //! @copydoc ::boost::intrusive::avltree::erase_and_dispose(const_reference, Disposer) template - size_type erase_and_dispose(const_reference value, Disposer disposer) - { return tree_.erase_and_dispose(value, disposer); } - - //! Requires: Disposer::operator()(pointer) shouldn't throw. - //! - //! Effects: Erases all the elements with the given key. - //! according to the comparison functor "comp". - //! Disposer::operator()(pointer) is called for the removed elements. - //! - //! Returns: The number of erased elements. - //! - //! Complexity: O(log(size() + this->count(key, comp)). - //! - //! Throws: If comp ordering function throws. Basic guarantee. - //! - //! Note: Invalidates the iterators - //! to the erased elements. + size_type erase_and_dispose(const_reference value, Disposer disposer); + + //! @copydoc ::boost::intrusive::avltree::erase_and_dispose(const KeyType&,KeyValueCompare,Disposer) template - size_type erase_and_dispose(const KeyType& key, KeyValueCompare comp, Disposer disposer - /// @cond - , typename detail::enable_if_c::value >::type * = 0 - /// @endcond - ) - { return tree_.erase_and_dispose(key, comp, disposer); } - - //! Effects: Erases all the elements of the container. - //! - //! Complexity: Linear to the number of elements on the container. - //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise. - //! - //! Throws: Nothing. - //! - //! Note: Invalidates the iterators (but not the references) - //! to the erased elements. No destructors are called. - void clear() - { return tree_.clear(); } - - //! Requires: Disposer::operator()(pointer) shouldn't throw. - //! - //! Effects: Erases all the elements of the container. - //! - //! Complexity: Linear to the number of elements on the container. - //! Disposer::operator()(pointer) is called for the removed elements. - //! - //! Throws: Nothing. - //! - //! Note: Invalidates the iterators (but not the references) - //! to the erased elements. No destructors are called. + size_type erase_and_dispose(const KeyType& key, KeyValueCompare comp, Disposer disposer); + + //! @copydoc ::boost::intrusive::avltree::clear + void clear(); + + //! @copydoc ::boost::intrusive::avltree::clear_and_dispose template - void clear_and_dispose(Disposer disposer) - { return tree_.clear_and_dispose(disposer); } - - //! Effects: Returns the number of contained elements with the given key - //! - //! Complexity: Logarithmic to the number of elements contained plus lineal - //! to number of objects with the given key. - //! - //! Throws: If the internal value_compare ordering function throws. - size_type count(const_reference value) const - { return tree_.count(value); } - - //! Effects: Returns the number of contained elements with the same key - //! compared with the given comparison functor. - //! - //! Complexity: Logarithmic to the number of elements contained plus lineal - //! to number of objects with the given key. - //! - //! Throws: If comp ordering function throws. + void clear_and_dispose(Disposer disposer); + + //! @copydoc ::boost::intrusive::avltree::count(const_reference)const + size_type count(const_reference value) const; + + //! @copydoc ::boost::intrusive::avltree::count(const KeyType&,KeyValueCompare)const template - size_type count(const KeyType& key, KeyValueCompare comp) const - { return tree_.count(key, comp); } - - //! Effects: Returns an iterator to the first element whose - //! key is not less than k or end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If the internal value_compare ordering function throws. - iterator lower_bound(const_reference value) - { return tree_.lower_bound(value); } - - //! Requires: comp must imply the same element order as - //! value_compare. Usually key is the part of the value_type - //! that is used in the ordering functor. - //! - //! Effects: Returns an iterator to the first element whose - //! key according to the comparison functor is not less than k or - //! end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If comp ordering function throws. - //! - //! Note: This function is used when constructing a value_type - //! is expensive and the value_type can be compared with a cheaper - //! key type. Usually this key is part of the value_type. + size_type count(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::avltree::lower_bound(const_reference) + iterator lower_bound(const_reference value); + + //! @copydoc ::boost::intrusive::avltree::lower_bound(const KeyType&,KeyValueCompare) template - iterator lower_bound(const KeyType& key, KeyValueCompare comp) - { return tree_.lower_bound(key, comp); } - - //! Effects: Returns a const iterator to the first element whose - //! key is not less than k or end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If the internal value_compare ordering function throws. - const_iterator lower_bound(const_reference value) const - { return tree_.lower_bound(value); } - - //! Requires: comp must imply the same element order as - //! value_compare. Usually key is the part of the value_type - //! that is used in the ordering functor. - //! - //! Effects: Returns a const_iterator to the first element whose - //! key according to the comparison functor is not less than k or - //! end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If comp ordering function throws. - //! - //! Note: This function is used when constructing a value_type - //! is expensive and the value_type can be compared with a cheaper - //! key type. Usually this key is part of the value_type. + iterator lower_bound(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::avltree::lower_bound(const_reference)const + const_iterator lower_bound(const_reference value) const; + + //! @copydoc ::boost::intrusive::avltree::lower_bound(const KeyType&,KeyValueCompare)const template - const_iterator lower_bound(const KeyType& key, KeyValueCompare comp) const - { return tree_.lower_bound(key, comp); } - - //! Effects: Returns an iterator to the first element whose - //! key is greater than k or end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If the internal value_compare ordering function throws. - iterator upper_bound(const_reference value) - { return tree_.upper_bound(value); } - - //! Requires: comp must imply the same element order as - //! value_compare. Usually key is the part of the value_type - //! that is used in the ordering functor. - //! - //! Effects: Returns an iterator to the first element whose - //! key according to the comparison functor is greater than key or - //! end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If comp ordering function throws. - //! - //! Note: This function is used when constructing a value_type - //! is expensive and the value_type can be compared with a cheaper - //! key type. Usually this key is part of the value_type. + const_iterator lower_bound(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::avltree::upper_bound(const_reference) + iterator upper_bound(const_reference value); + + //! @copydoc ::boost::intrusive::avltree::upper_bound(const KeyType&,KeyValueCompare) template - iterator upper_bound(const KeyType& key, KeyValueCompare comp) - { return tree_.upper_bound(key, comp); } - - //! Effects: Returns an iterator to the first element whose - //! key is greater than k or end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If the internal value_compare ordering function throws. - const_iterator upper_bound(const_reference value) const - { return tree_.upper_bound(value); } - - //! Requires: comp must imply the same element order as - //! value_compare. Usually key is the part of the value_type - //! that is used in the ordering functor. - //! - //! Effects: Returns a const_iterator to the first element whose - //! key according to the comparison functor is greater than key or - //! end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If comp ordering function throws. - //! - //! Note: This function is used when constructing a value_type - //! is expensive and the value_type can be compared with a cheaper - //! key type. Usually this key is part of the value_type. + iterator upper_bound(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::avltree::upper_bound(const_reference)const + const_iterator upper_bound(const_reference value) const; + + //! @copydoc ::boost::intrusive::avltree::upper_bound(const KeyType&,KeyValueCompare)const template - const_iterator upper_bound(const KeyType& key, KeyValueCompare comp) const - { return tree_.upper_bound(key, comp); } - - //! Effects: Finds an iterator to the first element whose value is - //! "value" or end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If the internal value_compare ordering function throws. - iterator find(const_reference value) - { return tree_.find(value); } - - //! Requires: comp must imply the same element order as - //! value_compare. Usually key is the part of the value_type - //! that is used in the ordering functor. - //! - //! Effects: Finds an iterator to the first element whose key is - //! "key" according to the comparison functor or end() if that element - //! does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If comp ordering function throws. - //! - //! Note: This function is used when constructing a value_type - //! is expensive and the value_type can be compared with a cheaper - //! key type. Usually this key is part of the value_type. + const_iterator upper_bound(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::avltree::find(const_reference) + iterator find(const_reference value); + + //! @copydoc ::boost::intrusive::avltree::find(const KeyType&,KeyValueCompare) template - iterator find(const KeyType& key, KeyValueCompare comp) - { return tree_.find(key, comp); } - - //! Effects: Finds a const_iterator to the first element whose value is - //! "value" or end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If the internal value_compare ordering function throws. - const_iterator find(const_reference value) const - { return tree_.find(value); } - - //! Requires: comp must imply the same element order as - //! value_compare. Usually key is the part of the value_type - //! that is used in the ordering functor. - //! - //! Effects: Finds a const_iterator to the first element whose key is - //! "key" according to the comparison functor or end() if that element - //! does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If comp ordering function throws. - //! - //! Note: This function is used when constructing a value_type - //! is expensive and the value_type can be compared with a cheaper - //! key type. Usually this key is part of the value_type. + iterator find(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::avltree::find(const_reference)const + const_iterator find(const_reference value) const; + + //! @copydoc ::boost::intrusive::avltree::find(const KeyType&,KeyValueCompare)const template - const_iterator find(const KeyType& key, KeyValueCompare comp) const - { return tree_.find(key, comp); } - - //! Effects: Finds a range containing all elements whose key is k or - //! an empty range that indicates the position where those elements would be - //! if they there is no elements with key k. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If the internal value_compare ordering function throws. - std::pair equal_range(const_reference value) - { return tree_.equal_range(value); } - - //! Requires: comp must imply the same element order as - //! value_compare. Usually key is the part of the value_type - //! that is used in the ordering functor. - //! - //! Effects: Finds a range containing all elements whose key is k - //! according to the comparison functor or an empty range - //! that indicates the position where those elements would be - //! if they there is no elements with key k. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If comp ordering function throws. - //! - //! Note: This function is used when constructing a value_type - //! is expensive and the value_type can be compared with a cheaper - //! key type. Usually this key is part of the value_type. + const_iterator find(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::avltree::equal_range(const_reference) + std::pair equal_range(const_reference value); + + //! @copydoc ::boost::intrusive::avltree::equal_range(const KeyType&,KeyValueCompare) template - std::pair equal_range(const KeyType& key, KeyValueCompare comp) - { return tree_.equal_range(key, comp); } - - //! Effects: Finds a range containing all elements whose key is k or - //! an empty range that indicates the position where those elements would be - //! if they there is no elements with key k. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If the internal value_compare ordering function throws. + std::pair equal_range(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::avltree::equal_range(const_reference)const std::pair - equal_range(const_reference value) const - { return tree_.equal_range(value); } - - //! Requires: comp must imply the same element order as - //! value_compare. Usually key is the part of the value_type - //! that is used in the ordering functor. - //! - //! Effects: Finds a range containing all elements whose key is k - //! according to the comparison functor or an empty range - //! that indicates the position where those elements would be - //! if they there is no elements with key k. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If comp ordering function throws. - //! - //! Note: This function is used when constructing a value_type - //! is expensive and the value_type can be compared with a cheaper - //! key type. Usually this key is part of the value_type. + equal_range(const_reference value) const; + + //! @copydoc ::boost::intrusive::avltree::equal_range(const KeyType&,KeyValueCompare)const template std::pair - equal_range(const KeyType& key, KeyValueCompare comp) const - { return tree_.equal_range(key, comp); } - - //! Requires: 'lower_value' must not be greater than 'upper_value'. If - //! 'lower_value' == 'upper_value', ('left_closed' || 'right_closed') must be false. - //! - //! Effects: Returns an a pair with the following criteria: - //! - //! first = lower_bound(lower_key) if left_closed, upper_bound(lower_key) otherwise - //! - //! second = upper_bound(upper_key) if right_closed, lower_bound(upper_key) otherwise - //! - //! Complexity: Logarithmic. - //! - //! Throws: If the predicate throws. - //! - //! Note: This function can be more efficient than calling upper_bound - //! and lower_bound for lower_value and upper_value. + equal_range(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::avltree::bounded_range(const_reference,const_reference,bool,bool) std::pair bounded_range - (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed) - { return tree_.bounded_range(lower_value, upper_value, left_closed, right_closed); } - - //! Requires: KeyValueCompare is a function object that induces a strict weak - //! ordering compatible with the strict weak ordering used to create the - //! the tree. - //! 'lower_key' must not be greater than 'upper_key' according to 'comp'. If - //! 'lower_key' == 'upper_key', ('left_closed' || 'right_closed') must be false. - //! - //! Effects: Returns an a pair with the following criteria: - //! - //! first = lower_bound(lower_key, comp) if left_closed, upper_bound(lower_key, comp) otherwise - //! - //! second = upper_bound(upper_key, comp) if right_closed, lower_bound(upper_key, comp) otherwise - //! - //! Complexity: Logarithmic. - //! - //! Throws: If "comp" throws. - //! - //! Note: This function can be more efficient than calling upper_bound - //! and lower_bound for lower_key and upper_key. + (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed); + + //! @copydoc ::boost::intrusive::avltree::bounded_range(const KeyType&,const KeyType&,KeyValueCompare,bool,bool) template std::pair bounded_range - (const KeyType& lower_key, const KeyType& upper_key, KeyValueCompare comp, bool left_closed, bool right_closed) - { return tree_.bounded_range(lower_key, upper_key, comp, left_closed, right_closed); } - - //! Requires: 'lower_value' must not be greater than 'upper_value'. If - //! 'lower_value' == 'upper_value', ('left_closed' || 'right_closed') must be false. - //! - //! Effects: Returns an a pair with the following criteria: - //! - //! first = lower_bound(lower_key) if left_closed, upper_bound(lower_key) otherwise - //! - //! second = upper_bound(upper_key) if right_closed, lower_bound(upper_key) otherwise - //! - //! Complexity: Logarithmic. - //! - //! Throws: If the predicate throws. - //! - //! Note: This function can be more efficient than calling upper_bound - //! and lower_bound for lower_value and upper_value. + (const KeyType& lower_key, const KeyType& upper_key, KeyValueCompare comp, bool left_closed, bool right_closed); + + //! @copydoc ::boost::intrusive::avltree::bounded_range(const_reference,const_reference,bool,bool)const std::pair - bounded_range(const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed) const - { return tree_.bounded_range(lower_value, upper_value, left_closed, right_closed); } - - //! Requires: KeyValueCompare is a function object that induces a strict weak - //! ordering compatible with the strict weak ordering used to create the - //! the tree. - //! 'lower_key' must not be greater than 'upper_key' according to 'comp'. If - //! 'lower_key' == 'upper_key', ('left_closed' || 'right_closed') must be false. - //! - //! Effects: Returns an a pair with the following criteria: - //! - //! first = lower_bound(lower_key, comp) if left_closed, upper_bound(lower_key, comp) otherwise - //! - //! second = upper_bound(upper_key, comp) if right_closed, lower_bound(upper_key, comp) otherwise - //! - //! Complexity: Logarithmic. - //! - //! Throws: If "comp" throws. - //! - //! Note: This function can be more efficient than calling upper_bound - //! and lower_bound for lower_key and upper_key. + bounded_range(const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed) const; + + //! @copydoc ::boost::intrusive::avltree::bounded_range(const KeyType&,const KeyType&,KeyValueCompare,bool,bool)const template - std::pair - bounded_range - (const KeyType& lower_key, const KeyType& upper_key, KeyValueCompare comp, bool left_closed, bool right_closed) const - { return tree_.bounded_range(lower_key, upper_key, comp, left_closed, right_closed); } - - //! Requires: value must be an lvalue and shall be in a avl_multiset of - //! appropriate type. Otherwise the behavior is undefined. - //! - //! Effects: Returns: a valid iterator i belonging to the avl_multiset - //! that points to the value - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - //! - //! Note: This static function is available only if the value traits - //! is stateless. - static iterator s_iterator_to(reference value) - { return tree_type::s_iterator_to(value); } - - //! Requires: value must be an lvalue and shall be in a avl_multiset of - //! appropriate type. Otherwise the behavior is undefined. - //! - //! Effects: Returns: a valid const_iterator i belonging to the - //! avl_multiset that points to the value - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - //! - //! Note: This static function is available only if the value traits - //! is stateless. - static const_iterator s_iterator_to(const_reference value) - { return tree_type::s_iterator_to(value); } - - //! Requires: value must be an lvalue and shall be in a avl_multiset of - //! appropriate type. Otherwise the behavior is undefined. - //! - //! Effects: Returns: a valid iterator i belonging to the avl_multiset - //! that points to the value - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - iterator iterator_to(reference value) - { return tree_.iterator_to(value); } - - //! Requires: value must be an lvalue and shall be in a avl_multiset of - //! appropriate type. Otherwise the behavior is undefined. - //! - //! Effects: Returns: a valid const_iterator i belonging to the - //! avl_multiset that points to the value - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_iterator iterator_to(const_reference value) const - { return tree_.iterator_to(value); } - - //! Requires: value shall not be in a avl_multiset/avl_multiset. - //! - //! Effects: init_node puts the hook of a value in a well-known default - //! state. - //! - //! Throws: Nothing. - //! - //! Complexity: Constant time. - //! - //! Note: This function puts the hook in the well-known default state - //! used by auto_unlink and safe hooks. - static void init_node(reference value) - { tree_type::init_node(value); } - - //! Effects: Unlinks the leftmost node from the tree. - //! - //! Complexity: Average complexity is constant time. - //! - //! Throws: Nothing. - //! - //! Notes: 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(); } - - //! Requires: replace_this must be a valid iterator of *this - //! and with_this must not be inserted in any tree. - //! - //! Effects: Replaces replace_this in its position in the - //! tree with with_this. The tree does not need to be rebalanced. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - //! - //! Note: This function will break container ordering invariants if - //! with_this is not equivalent to *replace_this according to the - //! ordering rules. This function is faster than erasing and inserting - //! the node, since no rebalancing or comparison is needed. - void replace_node(iterator replace_this, reference with_this) - { tree_.replace_node(replace_this, with_this); } + std::pair bounded_range + (const KeyType& lower_key, const KeyType& upper_key, KeyValueCompare comp, bool left_closed, bool right_closed) const; - /// @cond - friend bool operator==(const avl_multiset_impl &x, const avl_multiset_impl &y) - { return x.tree_ == y.tree_; } + //! @copydoc ::boost::intrusive::avltree::s_iterator_to(reference) + static iterator s_iterator_to(reference value); - friend bool operator<(const avl_multiset_impl &x, const avl_multiset_impl &y) - { return x.tree_ < y.tree_; } - /// @endcond + //! @copydoc ::boost::intrusive::avltree::s_iterator_to(const_reference) + static const_iterator s_iterator_to(const_reference value); + + //! @copydoc ::boost::intrusive::avltree::iterator_to(reference) + iterator iterator_to(reference value); + + //! @copydoc ::boost::intrusive::avltree::iterator_to(const_reference)const + const_iterator iterator_to(const_reference value) const; + + //! @copydoc ::boost::intrusive::avltree::init_node(reference) + static void init_node(reference value); + + //! @copydoc ::boost::intrusive::avltree::unlink_leftmost_without_rebalance + pointer unlink_leftmost_without_rebalance(); + + //! @copydoc ::boost::intrusive::avltree::replace_node + void replace_node(iterator replace_this, reference with_this); + + //! @copydoc ::boost::intrusive::avltree::remove_node + void remove_node(reference value); + #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED }; #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + template -#else -template -#endif -inline bool operator!= -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) -(const avl_multiset_impl &x, const avl_multiset_impl &y) -#else -(const avl_multiset_impl &x, const avl_multiset_impl &y) -#endif -{ return !(x == y); } +bool operator!= (const avl_multiset_impl &x, const avl_multiset_impl &y); -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) template -#else -template -#endif -inline bool operator> -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) -(const avl_multiset_impl &x, const avl_multiset_impl &y) -#else -(const avl_multiset_impl &x, const avl_multiset_impl &y) -#endif -{ return y < x; } +bool operator>(const avl_multiset_impl &x, const avl_multiset_impl &y); -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) template -#else -template -#endif -inline bool operator<= -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) -(const avl_multiset_impl &x, const avl_multiset_impl &y) -#else -(const avl_multiset_impl &x, const avl_multiset_impl &y) -#endif -{ return !(y < x); } +bool operator<=(const avl_multiset_impl &x, const avl_multiset_impl &y); -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) template -#else -template -#endif -inline bool operator>= -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) -(const avl_multiset_impl &x, const avl_multiset_impl &y) -#else -(const avl_multiset_impl &x, const avl_multiset_impl &y) -#endif -{ return !(x < y); } +bool operator>=(const avl_multiset_impl &x, const avl_multiset_impl &y); -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) template -#else -template -#endif -inline void swap -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) -(avl_multiset_impl &x, avl_multiset_impl &y) -#else -(avl_multiset_impl &x, avl_multiset_impl &y) -#endif -{ x.swap(y); } +void swap(avl_multiset_impl &x, avl_multiset_impl &y); + +#endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) //! Helper metafunction to define a \c avl_multiset that yields to the same type when the //! same options (either explicitly or implicitly) are used. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) template #else -template +template #endif struct make_avl_multiset { /// @cond + typedef typename pack_options + < avltree_defaults, + #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) + O1, O2, O3, O4, O5 + #else + Options... + #endif + >::type packed_options; + + typedef typename detail::get_value_traits + ::type value_traits; + typedef typename detail::get_header_holder_type + < value_traits, typename packed_options::header_holder_type >::type header_holder_type; + typedef avl_multiset_impl - < typename make_avltree_opt - #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) - - #else - - #endif - ::type - > implementation_defined; + < value_traits + , typename packed_options::compare + , typename packed_options::size_type + , packed_options::constant_time_size + , header_holder_type + > implementation_defined; /// @endcond typedef implementation_defined type; }; @@ -2455,28 +876,29 @@ struct make_avl_multiset #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) -template +template #else template #endif class avl_multiset : public make_avl_multiset::type + #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) + O1, O2, O3, O4, O5 + #else + Options... + #endif + >::type { - typedef typename make_avl_multiset + typedef typename make_avl_multiset + O1, O2, O3, O4, O5 #else - + Options... #endif - ::type Base; + >::type Base; BOOST_MOVABLE_BUT_NOT_COPYABLE(avl_multiset) + public: typedef typename Base::value_compare value_compare; typedef typename Base::value_traits value_traits; @@ -2486,8 +908,8 @@ class avl_multiset //Assert if passed value traits are compatible with the type BOOST_STATIC_ASSERT((detail::is_same::value)); - avl_multiset( const value_compare &cmp = value_compare() - , const value_traits &v_traits = value_traits()) + explicit avl_multiset( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) : Base(cmp, v_traits) {} @@ -2503,7 +925,7 @@ class avl_multiset {} avl_multiset& operator=(BOOST_RV_REF(avl_multiset) x) - { this->Base::operator=(::boost::move(static_cast(x))); return *this; } + { return static_cast(this->Base::operator=(::boost::move(static_cast(x)))); } static avl_multiset &container_from_end_iterator(iterator end_iterator) { return static_cast(Base::container_from_end_iterator(end_iterator)); } @@ -2511,11 +933,11 @@ class avl_multiset static const avl_multiset &container_from_end_iterator(const_iterator end_iterator) { return static_cast(Base::container_from_end_iterator(end_iterator)); } - static avl_multiset &container_from_iterator(iterator end_iterator) - { return static_cast(Base::container_from_iterator(end_iterator)); } + static avl_multiset &container_from_iterator(iterator it) + { return static_cast(Base::container_from_iterator(it)); } - static const avl_multiset &container_from_iterator(const_iterator end_iterator) - { return static_cast(Base::container_from_iterator(end_iterator)); } + static const avl_multiset &container_from_iterator(const_iterator it) + { return static_cast(Base::container_from_iterator(it)); } }; #endif diff --git a/boost/intrusive/avl_set_hook.hpp b/boost/intrusive/avl_set_hook.hpp index 825921f509..6fdc51b707 100644 --- a/boost/intrusive/avl_set_hook.hpp +++ b/boost/intrusive/avl_set_hook.hpp @@ -1,6 +1,6 @@ ///////////////////////////////////////////////////////////////////////////// // -// (C) Copyright Ion Gaztanaga 2007-2012 +// (C) Copyright Ion Gaztanaga 2007-2013 // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at @@ -13,9 +13,13 @@ #ifndef BOOST_INTRUSIVE_AVL_SET_HOOK_HPP #define BOOST_INTRUSIVE_AVL_SET_HOOK_HPP +#if defined(_MSC_VER) +# pragma once +#endif + #include #include -#include + #include #include #include @@ -24,20 +28,12 @@ namespace boost { namespace intrusive { -/// @cond -template -struct get_avl_set_node_algo -{ - typedef avltree_algorithms > 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. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) template #else -template +template #endif struct make_avl_set_base_hook { @@ -50,12 +46,11 @@ struct make_avl_set_base_hook #endif ::type packed_options; - typedef detail::generic_hook - < get_avl_set_node_algo + typedef generic_hook + < avltree_algorithms > , typename packed_options::tag , packed_options::link_mode - , detail::AvlSetBaseHook + , AvlTreeBaseHookId > implementation_defined; /// @endcond typedef implementation_defined type; @@ -74,7 +69,7 @@ struct make_avl_set_base_hook //! unique tag. //! //! \c void_pointer<> is the pointer type that will be used internally in the hook -//! and the the container configured to use this hook. +//! and the container configured to use this hook. //! //! \c link_mode<> will specify the linking mode of the hook (\c normal_link, //! \c auto_unlink or \c safe_link). @@ -168,7 +163,7 @@ class avl_set_base_hook #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) template #else -template +template #endif struct make_avl_set_member_hook { @@ -181,12 +176,11 @@ struct make_avl_set_member_hook #endif ::type packed_options; - typedef detail::generic_hook - < get_avl_set_node_algo + typedef generic_hook + < avltree_algorithms > , member_tag , packed_options::link_mode - , detail::NoBaseHook + , NoBaseHookId > implementation_defined; /// @endcond typedef implementation_defined type; @@ -200,7 +194,7 @@ struct make_avl_set_member_hook //! \c link_mode<> and \c optimize_size<>. //! //! \c void_pointer<> is the pointer type that will be used internally in the hook -//! and the the container configured to use this hook. +//! and the container configured to use this hook. //! //! \c link_mode<> will specify the linking mode of the hook (\c normal_link, //! \c auto_unlink or \c safe_link). diff --git a/boost/intrusive/avltree.hpp b/boost/intrusive/avltree.hpp index 84376a87c7..d2adf24917 100644 --- a/boost/intrusive/avltree.hpp +++ b/boost/intrusive/avltree.hpp @@ -1,6 +1,6 @@ ///////////////////////////////////////////////////////////////////////////// // -// (C) Copyright Ion Gaztanaga 2007-2012 +// (C) Copyright Ion Gaztanaga 2007-2013 // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at @@ -12,55 +12,50 @@ #ifndef BOOST_INTRUSIVE_AVLTREE_HPP #define BOOST_INTRUSIVE_AVLTREE_HPP +#if defined(_MSC_VER) +# pragma once +#endif + #include -#include +#include #include #include -#include #include -#include #include -#include #include #include +#include #include #include #include #include -#include -#include -#include -#include +#include #include #include -#include +#include namespace boost { namespace intrusive { /// @cond -template -struct avl_setopt +struct default_avltree_hook_applier +{ template struct apply{ typedef typename T::default_avltree_hook type; }; }; + +template<> +struct is_default_hook_tag +{ static const bool value = true; }; + +struct avltree_defaults { - typedef ValueTraits value_traits; - typedef Compare compare; - typedef SizeType size_type; - static const bool constant_time_size = ConstantTimeSize; + typedef default_avltree_hook_applier proto_value_traits; + static const bool constant_time_size = true; + typedef std::size_t size_type; + typedef void compare; + typedef void header_holder_type; }; -template -struct avl_set_defaults - : pack_options - < none - , base_hook - , constant_time_size - , size_type - , compare > - >::type -{}; - /// @endcond //! The class template avltree is an intrusive AVL tree container, that @@ -79,1672 +74,441 @@ struct avl_set_defaults #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) template #else -template +template #endif class avltree_impl - : private detail::clear_on_destructor_base > + /// @cond + : public bstree_impl + /// @endcond { - template friend class detail::clear_on_destructor_base; public: - typedef typename Config::value_traits value_traits; + typedef ValueTraits value_traits; /// @cond - static const bool external_value_traits = - detail::external_value_traits_is_true::value; - typedef typename detail::eval_if_c - < external_value_traits - , detail::eval_value_traits - , detail::identity - >::type real_value_traits; + typedef bstree_impl< ValueTraits, VoidOrKeyComp, SizeType + , ConstantTimeSize, AvlTreeAlgorithms + , HeaderHolder> tree_type; + typedef tree_type implementation_defined; /// @endcond - typedef typename real_value_traits::pointer pointer; - typedef typename real_value_traits::const_pointer const_pointer; - typedef typename boost::intrusive:: - pointer_traits::element_type value_type; - typedef value_type key_type; - typedef typename boost::intrusive:: - pointer_traits::reference reference; - typedef typename boost::intrusive:: - pointer_traits::reference const_reference; - typedef typename boost::intrusive:: - pointer_traits::difference_type difference_type; - typedef typename Config::size_type size_type; - typedef typename Config::compare value_compare; - typedef value_compare key_compare; - typedef tree_iterator iterator; - typedef tree_iterator const_iterator; - typedef boost::intrusive::detail::reverse_iterator reverse_iterator; - typedef boost::intrusive::detail::reverse_iteratorconst_reverse_iterator; - typedef typename real_value_traits::node_traits node_traits; - typedef typename node_traits::node node; - typedef typename pointer_traits - ::template rebind_pointer - ::type node_ptr; - typedef typename pointer_traits - ::template rebind_pointer - ::type const_node_ptr; - typedef avltree_algorithms node_algorithms; - - static const bool constant_time_size = Config::constant_time_size; - static const bool stateful_value_traits = detail::store_cont_ptr_on_it::value; + typedef typename implementation_defined::pointer pointer; + typedef typename implementation_defined::const_pointer const_pointer; + typedef typename implementation_defined::value_type value_type; + typedef typename implementation_defined::key_type key_type; + typedef typename implementation_defined::reference reference; + typedef typename implementation_defined::const_reference const_reference; + typedef typename implementation_defined::difference_type difference_type; + typedef typename implementation_defined::size_type size_type; + typedef typename implementation_defined::value_compare value_compare; + typedef typename implementation_defined::key_compare key_compare; + typedef typename implementation_defined::iterator iterator; + typedef typename implementation_defined::const_iterator const_iterator; + typedef typename implementation_defined::reverse_iterator reverse_iterator; + typedef typename implementation_defined::const_reverse_iterator const_reverse_iterator; + typedef typename implementation_defined::node_traits node_traits; + typedef typename implementation_defined::node node; + typedef typename implementation_defined::node_ptr node_ptr; + typedef typename implementation_defined::const_node_ptr const_node_ptr; + typedef typename implementation_defined::node_algorithms node_algorithms; + + static const bool constant_time_size = implementation_defined::constant_time_size; /// @cond private: - typedef detail::size_holder size_traits; - //noncopyable, movable + //noncopyable BOOST_MOVABLE_BUT_NOT_COPYABLE(avltree_impl) - enum { safemode_or_autounlink = - (int)real_value_traits::link_mode == (int)auto_unlink || - (int)real_value_traits::link_mode == (int)safe_link }; + /// @endcond - //Constant-time size is incompatible with auto-unlink hooks! - BOOST_STATIC_ASSERT(!(constant_time_size && ((int)real_value_traits::link_mode == (int)auto_unlink))); + public: - struct header_plus_size : public size_traits - { node header_; }; + typedef typename implementation_defined::insert_commit_data insert_commit_data; - struct node_plus_pred_t : public detail::ebo_functor_holder - { - node_plus_pred_t(const value_compare &comp) - : detail::ebo_functor_holder(comp) - {} - header_plus_size header_plus_size_; - }; - struct data_t : public avltree_impl::value_traits - { - typedef typename avltree_impl::value_traits value_traits; - data_t(const value_compare & comp, const value_traits &val_traits) - : value_traits(val_traits), node_plus_pred_(comp) - {} - node_plus_pred_t node_plus_pred_; - } data_; + //! @copydoc ::boost::intrusive::bstree::bstree(const value_compare &,const value_traits &) + explicit avltree_impl( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : tree_type(cmp, v_traits) + {} - const value_compare &priv_comp() const - { return data_.node_plus_pred_.get(); } + //! @copydoc ::boost::intrusive::bstree::bstree(bool,Iterator,Iterator,const value_compare &,const value_traits &) + template + avltree_impl( bool unique, Iterator b, Iterator e + , const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : tree_type(unique, b, e, cmp, v_traits) + {} - value_compare &priv_comp() - { return data_.node_plus_pred_.get(); } + //! @copydoc ::boost::intrusive::bstree::bstree(bstree &&) + avltree_impl(BOOST_RV_REF(avltree_impl) x) + : tree_type(::boost::move(static_cast(x))) + {} - const value_traits &priv_value_traits() const - { return data_; } + //! @copydoc ::boost::intrusive::bstree::operator=(bstree &&) + avltree_impl& operator=(BOOST_RV_REF(avltree_impl) x) + { return static_cast(tree_type::operator=(::boost::move(static_cast(x)))); } - value_traits &priv_value_traits() - { return data_; } + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED - node_ptr priv_header_ptr() - { return pointer_traits::pointer_to(data_.node_plus_pred_.header_plus_size_.header_); } + //! @copydoc ::boost::intrusive::bstree::~bstree() + ~avltree_impl(); - const_node_ptr priv_header_ptr() const - { return pointer_traits::pointer_to(data_.node_plus_pred_.header_plus_size_.header_); } + //! @copydoc ::boost::intrusive::bstree::begin() + iterator begin(); - static node_ptr uncast(const const_node_ptr & ptr) - { return pointer_traits::const_cast_from(ptr); } + //! @copydoc ::boost::intrusive::bstree::begin()const + const_iterator begin() const; - size_traits &priv_size_traits() - { return data_.node_plus_pred_.header_plus_size_; } + //! @copydoc ::boost::intrusive::bstree::cbegin()const + const_iterator cbegin() const; - const size_traits &priv_size_traits() const - { return data_.node_plus_pred_.header_plus_size_; } + //! @copydoc ::boost::intrusive::bstree::end() + iterator end(); - const real_value_traits &get_real_value_traits(detail::bool_) const - { return data_; } + //! @copydoc ::boost::intrusive::bstree::end()const + const_iterator end() const; - const real_value_traits &get_real_value_traits(detail::bool_) const - { return data_.get_value_traits(*this); } + //! @copydoc ::boost::intrusive::bstree::cend()const + const_iterator cend() const; - real_value_traits &get_real_value_traits(detail::bool_) - { return data_; } + //! @copydoc ::boost::intrusive::bstree::rbegin() + reverse_iterator rbegin(); - real_value_traits &get_real_value_traits(detail::bool_) - { return data_.get_value_traits(*this); } + //! @copydoc ::boost::intrusive::bstree::rbegin()const + const_reverse_iterator rbegin() const; - /// @endcond + //! @copydoc ::boost::intrusive::bstree::crbegin()const + const_reverse_iterator crbegin() const; - public: + //! @copydoc ::boost::intrusive::bstree::rend() + reverse_iterator rend(); - const real_value_traits &get_real_value_traits() const - { return this->get_real_value_traits(detail::bool_()); } - - real_value_traits &get_real_value_traits() - { return this->get_real_value_traits(detail::bool_()); } - - typedef typename node_algorithms::insert_commit_data insert_commit_data; - - //! Effects: Constructs an empty tree. - //! - //! Complexity: Constant. - //! - //! Throws: If value_traits::node_traits::node - //! constructor throws (this does not happen with predefined Boost.Intrusive hooks) - //! or the copy constructor of the value_compare object throws. Basic guarantee. - avltree_impl( const value_compare &cmp = value_compare() - , const value_traits &v_traits = value_traits()) - : data_(cmp, v_traits) - { - node_algorithms::init_header(this->priv_header_ptr()); - this->priv_size_traits().set_size(size_type(0)); - } - - //! Requires: Dereferencing iterator must yield an lvalue of type value_type. - //! cmp must be a comparison function that induces a strict weak ordering. - //! - //! Effects: Constructs an empty tree and inserts elements from - //! [b, e). - //! - //! Complexity: Linear in N if [b, e) is already sorted using - //! comp and otherwise N * log N, where N is the distance between first and last. - //! - //! Throws: If value_traits::node_traits::node - //! constructor throws (this does not happen with predefined Boost.Intrusive hooks) - //! or the copy constructor/operator() of the value_compare object throws. Basic guarantee. - template - avltree_impl( bool unique, Iterator b, Iterator e - , const value_compare &cmp = value_compare() - , const value_traits &v_traits = value_traits()) - : data_(cmp, v_traits) - { - node_algorithms::init_header(this->priv_header_ptr()); - this->priv_size_traits().set_size(size_type(0)); - if(unique) - this->insert_unique(b, e); - else - this->insert_equal(b, e); - } - - //! Effects: to-do - //! - avltree_impl(BOOST_RV_REF(avltree_impl) x) - : data_(::boost::move(x.priv_comp()), ::boost::move(x.priv_value_traits())) - { - node_algorithms::init_header(this->priv_header_ptr()); - this->priv_size_traits().set_size(size_type(0)); - this->swap(x); - } - - //! Effects: to-do - //! - avltree_impl& operator=(BOOST_RV_REF(avltree_impl) x) - { this->swap(x); return *this; } - - //! Effects: Detaches all elements from this. The objects in the set - //! are not deleted (i.e. no destructors are called), but the nodes according to - //! the value_traits template parameter are reinitialized and thus can be reused. - //! - //! Complexity: Linear to elements contained in *this. - //! - //! Throws: Nothing. - ~avltree_impl() - {} + //! @copydoc ::boost::intrusive::bstree::rend()const + const_reverse_iterator rend() const; - //! Effects: Returns an iterator pointing to the beginning of the tree. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - iterator begin() - { return iterator (node_traits::get_left(this->priv_header_ptr()), this); } - - //! Effects: Returns a const_iterator pointing to the beginning of the tree. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_iterator begin() const - { return cbegin(); } - - //! Effects: Returns a const_iterator pointing to the beginning of the tree. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_iterator cbegin() const - { return const_iterator (node_traits::get_left(this->priv_header_ptr()), this); } - - //! Effects: Returns an iterator pointing to the end of the tree. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - iterator end() - { return iterator (this->priv_header_ptr(), this); } - - //! Effects: Returns a const_iterator pointing to the end of the tree. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_iterator end() const - { return cend(); } - - //! Effects: Returns a const_iterator pointing to the end of the tree. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_iterator cend() const - { return const_iterator (uncast(this->priv_header_ptr()), this); } - - //! Effects: Returns a reverse_iterator pointing to the beginning of the - //! reversed tree. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - reverse_iterator rbegin() - { return reverse_iterator(end()); } - - //! Effects: Returns a const_reverse_iterator pointing to the beginning - //! of the reversed tree. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_reverse_iterator rbegin() const - { return const_reverse_iterator(end()); } - - //! Effects: Returns a const_reverse_iterator pointing to the beginning - //! of the reversed tree. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_reverse_iterator crbegin() const - { return const_reverse_iterator(end()); } - - //! Effects: Returns a reverse_iterator pointing to the end - //! of the reversed tree. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - reverse_iterator rend() - { return reverse_iterator(begin()); } - - //! Effects: Returns a const_reverse_iterator pointing to the end - //! of the reversed tree. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_reverse_iterator rend() const - { return const_reverse_iterator(begin()); } - - //! Effects: Returns a const_reverse_iterator pointing to the end - //! of the reversed tree. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_reverse_iterator crend() const - { return const_reverse_iterator(begin()); } - - //! Precondition: end_iterator must be a valid end iterator - //! of avltree. - //! - //! Effects: Returns a const reference to the avltree associated to the end iterator - //! - //! Throws: Nothing. - //! - //! Complexity: Constant. - static avltree_impl &container_from_end_iterator(iterator end_iterator) - { return priv_container_from_end_iterator(end_iterator); } - - //! Precondition: end_iterator must be a valid end const_iterator - //! of avltree. - //! - //! Effects: Returns a const reference to the avltree associated to the end iterator - //! - //! Throws: Nothing. - //! - //! Complexity: Constant. - static const avltree_impl &container_from_end_iterator(const_iterator end_iterator) - { return priv_container_from_end_iterator(end_iterator); } - - //! Precondition: it must be a valid iterator - //! of rbtree. - //! - //! Effects: Returns a const reference to the tree associated to the iterator - //! - //! Throws: Nothing. - //! - //! Complexity: Logarithmic. - static avltree_impl &container_from_iterator(iterator it) - { return priv_container_from_iterator(it); } - - //! Precondition: it must be a valid end const_iterator - //! of rbtree. - //! - //! Effects: Returns a const reference to the tree associated to the iterator - //! - //! Throws: Nothing. - //! - //! Complexity: Logarithmic. - static const avltree_impl &container_from_iterator(const_iterator it) - { return priv_container_from_iterator(it); } - - //! Effects: Returns the value_compare object used by the tree. - //! - //! Complexity: Constant. - //! - //! Throws: If value_compare copy-constructor throws. - value_compare value_comp() const - { return priv_comp(); } - - //! Effects: Returns true if the container is empty. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - bool empty() const - { return node_algorithms::unique(this->priv_header_ptr()); } - - //! Effects: Returns the number of elements stored in the tree. - //! - //! Complexity: Linear to elements contained in *this - //! if constant-time size option is disabled. Constant time otherwise. - //! - //! Throws: Nothing. - size_type size() const - { - if(constant_time_size) - return this->priv_size_traits().get_size(); - else{ - return (size_type)node_algorithms::size(this->priv_header_ptr()); - } - } - - //! Effects: Swaps the contents of two avltrees. - //! - //! Complexity: Constant. - //! - //! Throws: If the comparison functor's swap call throws. - void swap(avltree_impl& other) - { - //This can throw - using std::swap; - swap(priv_comp(), priv_comp()); - //These can't throw - node_algorithms::swap_tree(this->priv_header_ptr(), other.priv_header_ptr()); - if(constant_time_size){ - size_type backup = this->priv_size_traits().get_size(); - this->priv_size_traits().set_size(other.priv_size_traits().get_size()); - other.priv_size_traits().set_size(backup); - } - } - - //! Requires: value must be an lvalue - //! - //! Effects: Inserts value into the tree before the upper bound. - //! - //! Complexity: Average complexity for insert element is at - //! most logarithmic. - //! - //! Throws: If the internal value_compare ordering function throws. Strong guarantee. - //! - //! Note: Does not affect the validity of iterators and references. - //! No copy-constructors are called. - iterator insert_equal(reference value) - { - detail::key_nodeptr_comp - 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)); - iterator ret(node_algorithms::insert_equal_upper_bound - (this->priv_header_ptr(), to_insert, key_node_comp), this); - this->priv_size_traits().increment(); - return ret; - } - - //! Requires: value must be an lvalue, and "hint" must be - //! a valid iterator. - //! - //! Effects: Inserts x 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) - //! - //! Complexity: Logarithmic in general, but it is amortized - //! constant time if t is inserted immediately before hint. - //! - //! Throws: If the internal value_compare ordering function throws. Strong guarantee. - //! - //! Note: Does not affect the validity of iterators and references. - //! No copy-constructors are called. - iterator insert_equal(const_iterator hint, reference value) - { - detail::key_nodeptr_comp - 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)); - iterator ret(node_algorithms::insert_equal - (this->priv_header_ptr(), hint.pointed_node(), to_insert, key_node_comp), this); - this->priv_size_traits().increment(); - return ret; - } - - //! Requires: Dereferencing iterator must yield an lvalue - //! of type value_type. - //! - //! Effects: Inserts a each element of a range into the tree - //! before the upper bound of the key of each element. - //! - //! Complexity: Insert range is in general O(N * log(N)), where N is the - //! size of the range. However, it is linear in N if the range is already sorted - //! by value_comp(). - //! - //! Throws: Nothing. - //! - //! Note: Does not affect the validity of iterators and references. - //! No copy-constructors are called. - template - void insert_equal(Iterator b, Iterator e) - { - iterator end(this->end()); - for (; b != e; ++b) - this->insert_equal(end, *b); - } - - //! Requires: value must be an lvalue - //! - //! Effects: Inserts value into the tree if the value - //! is not already present. - //! - //! Complexity: Average complexity for insert element is at - //! most logarithmic. - //! - //! Throws: Nothing. - //! - //! Note: Does not affect the validity of iterators and references. - //! No copy-constructors are called. - std::pair insert_unique(reference value) - { - insert_commit_data commit_data; - std::pair ret = insert_unique_check(value, priv_comp(), commit_data); - if(!ret.second) - return ret; - return std::pair (insert_unique_commit(value, commit_data), true); - } - - //! Requires: value must be an lvalue, and "hint" must be - //! a valid iterator - //! - //! Effects: Tries to insert x into the tree, using "hint" as a hint - //! to where it will be inserted. - //! - //! Complexity: Logarithmic in general, but it is amortized - //! constant time (two comparisons in the worst case) - //! if t is inserted immediately before hint. - //! - //! Throws: Nothing. - //! - //! Note: Does not affect the validity of iterators and references. - //! No copy-constructors are called. - iterator insert_unique(const_iterator hint, reference value) - { - insert_commit_data commit_data; - std::pair ret = insert_unique_check(hint, value, priv_comp(), commit_data); - if(!ret.second) - return ret.first; - return insert_unique_commit(value, commit_data); - } - - //! Requires: Dereferencing iterator must yield an lvalue - //! of type value_type. - //! - //! Effects: Tries to insert each element of a range into the tree. - //! - //! Complexity: Insert range is in general O(N * log(N)), where N is the - //! size of the range. However, it is linear in N if the range is already sorted - //! by value_comp(). - //! - //! Throws: Nothing. - //! - //! Note: Does not affect the validity of iterators and references. - //! No copy-constructors are called. + //! @copydoc ::boost::intrusive::bstree::crend()const + const_reverse_iterator crend() const; + + //! @copydoc ::boost::intrusive::bstree::container_from_end_iterator(iterator) + static avltree_impl &container_from_end_iterator(iterator end_iterator); + + //! @copydoc ::boost::intrusive::bstree::container_from_end_iterator(const_iterator) + static const avltree_impl &container_from_end_iterator(const_iterator end_iterator); + + //! @copydoc ::boost::intrusive::bstree::container_from_iterator(iterator) + static avltree_impl &container_from_iterator(iterator it); + + //! @copydoc ::boost::intrusive::bstree::container_from_iterator(const_iterator) + static const avltree_impl &container_from_iterator(const_iterator it); + + //! @copydoc ::boost::intrusive::bstree::key_comp()const + key_compare key_comp() const; + + //! @copydoc ::boost::intrusive::bstree::value_comp()const + value_compare value_comp() const; + + //! @copydoc ::boost::intrusive::bstree::empty()const + bool empty() const; + + //! @copydoc ::boost::intrusive::bstree::size()const + size_type size() const; + + //! @copydoc ::boost::intrusive::bstree::swap + void swap(avltree_impl& other); + + //! @copydoc ::boost::intrusive::bstree::clone_from + template + void clone_from(const avltree_impl &src, Cloner cloner, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::insert_equal(reference) + iterator insert_equal(reference value); + + //! @copydoc ::boost::intrusive::bstree::insert_equal(const_iterator,reference) + iterator insert_equal(const_iterator hint, reference value); + + //! @copydoc ::boost::intrusive::bstree::insert_equal(Iterator,Iterator) template - void insert_unique(Iterator b, Iterator e) - { - if(this->empty()){ - iterator end(this->end()); - for (; b != e; ++b) - this->insert_unique(end, *b); - } - else{ - for (; b != e; ++b) - this->insert_unique(*b); - } - } - - //! Requires: key_value_comp must be a comparison function that induces - //! the same strict weak ordering as value_compare. The difference is that - //! key_value_comp compares an arbitrary key with the contained values. - //! - //! Effects: Checks if a value can be inserted in the container, using - //! a user provided key instead of the value itself. - //! - //! Returns: If there is an equivalent value - //! returns a pair containing an iterator to the already present value - //! and false. If the value can be inserted returns true in the returned - //! pair boolean and fills "commit_data" that is meant to be used with - //! the "insert_commit" function. - //! - //! Complexity: Average complexity is at most logarithmic. - //! - //! Throws: If the key_value_comp ordering function throws. Strong guarantee. - //! - //! Notes: This function is used to improve performance when constructing - //! a value_type is expensive: 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 value_type 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 value_type and use - //! "insert_commit" to insert the object 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_commit" only if no more - //! objects are inserted or erased from the container. + void insert_equal(Iterator b, Iterator e); + + //! @copydoc ::boost::intrusive::bstree::insert_unique(reference) + std::pair insert_unique(reference value); + + //! @copydoc ::boost::intrusive::bstree::insert_unique(const_iterator,reference) + iterator insert_unique(const_iterator hint, reference value); + + //! @copydoc ::boost::intrusive::bstree::insert_unique_check(const KeyType&,KeyValueCompare,insert_commit_data&) template std::pair insert_unique_check - (const KeyType &key, KeyValueCompare key_value_comp, insert_commit_data &commit_data) - { - detail::key_nodeptr_comp - comp(key_value_comp, this); - std::pair ret = - (node_algorithms::insert_unique_check - (this->priv_header_ptr(), key, comp, commit_data)); - return std::pair(iterator(ret.first, this), ret.second); - } - - //! Requires: key_value_comp must be a comparison function that induces - //! the same strict weak ordering as value_compare. The difference is that - //! key_value_comp compares an arbitrary key with the contained values. - //! - //! Effects: Checks if a value can be inserted in the container, using - //! a user provided key instead of the value itself, using "hint" - //! as a hint to where it will be inserted. - //! - //! Returns: If there is an equivalent value - //! returns a pair containing an iterator to the already present value - //! and false. If the value can be inserted returns true in the returned - //! pair boolean and fills "commit_data" that is meant to be used with - //! the "insert_commit" function. - //! - //! Complexity: Logarithmic in general, but it's amortized - //! constant time if t is inserted immediately before hint. - //! - //! Throws: If the key_value_comp ordering function throws. Strong guarantee. - //! - //! Notes: This function is used to improve performance when constructing - //! a value_type is expensive: if there is an equivalent value - //! the constructed object must be discarded. Many times, the part of the - //! constructing that is used to impose the order is much cheaper to construct - //! than the value_type and this function offers the possibility to use that key - //! to check if the insertion will be successful. - //! - //! If the check is successful, the user can construct the value_type and use - //! "insert_commit" to insert the object in constant-time. This can give a total - //! constant-time complexity to the insertion: check(O(1)) + commit(O(1)). - //! - //! "commit_data" remains valid for a subsequent "insert_commit" only if no more - //! objects are inserted or erased from the container. + (const KeyType &key, KeyValueCompare key_value_comp, insert_commit_data &commit_data); + + //! @copydoc ::boost::intrusive::bstree::insert_unique_check(const_iterator,const KeyType&,KeyValueCompare,insert_commit_data&) template std::pair insert_unique_check (const_iterator hint, const KeyType &key - ,KeyValueCompare key_value_comp, insert_commit_data &commit_data) - { - detail::key_nodeptr_comp - comp(key_value_comp, this); - std::pair ret = - (node_algorithms::insert_unique_check - (this->priv_header_ptr(), hint.pointed_node(), key, comp, commit_data)); - return std::pair(iterator(ret.first, this), ret.second); - } - - //! Requires: value must be an lvalue of type value_type. commit_data - //! must have been obtained from a previous call to "insert_check". - //! No objects should have been inserted or erased from the container between - //! the "insert_check" that filled "commit_data" and the call to "insert_commit". - //! - //! Effects: Inserts the value in the avl_set using the information obtained - //! from the "commit_data" that a previous "insert_check" filled. - //! - //! Returns: An iterator to the newly inserted object. - //! - //! Complexity: Constant time. - //! - //! Throws: Nothing. - //! - //! Notes: This function has only sense if a "insert_check" has been - //! previously executed to fill "commit_data". No value should be inserted or - //! erased between the "insert_check" and "insert_commit" calls. - iterator insert_unique_commit(reference value, const insert_commit_data &commit_data) - { - 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)); - node_algorithms::insert_unique_commit - (this->priv_header_ptr(), to_insert, commit_data); - this->priv_size_traits().increment(); - return iterator(to_insert, this); - } - - //! Requires: value must be an lvalue, "pos" must be - //! a valid iterator (or end) and must be the succesor of value - //! once inserted according to the predicate - //! - //! Effects: Inserts x into the tree before "pos". - //! - //! Complexity: Constant time. - //! - //! Throws: Nothing. - //! - //! Note: This function does not check preconditions so if "pos" is not - //! the successor of "value" tree ordering invariant will be broken. - //! This is a low-level function to be used only for performance reasons - //! by advanced users. - iterator insert_before(const_iterator pos, reference value) - { - 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)); - iterator ret(node_algorithms::insert_before - (this->priv_header_ptr(), pos.pointed_node(), to_insert), this); - this->priv_size_traits().increment(); - return ret; - } - - //! Requires: value must be an lvalue, and it must be no less - //! than the greatest inserted key - //! - //! Effects: Inserts x into the tree in the last position. - //! - //! Complexity: Constant time. - //! - //! Throws: Nothing. - //! - //! Note: This function does not check preconditions so if value is - //! less than the greatest inserted key tree ordering invariant will be broken. - //! This function is slightly more efficient than using "insert_before". - //! This is a low-level function to be used only for performance reasons - //! by advanced users. - void push_back(reference value) - { - 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)); - node_algorithms::push_back(this->priv_header_ptr(), to_insert); - this->priv_size_traits().increment(); - } - - //! Requires: value must be an lvalue, and it must be no greater - //! than the minimum inserted key - //! - //! Effects: Inserts x into the tree in the first position. - //! - //! Complexity: Constant time. - //! - //! Throws: Nothing. - //! - //! Note: This function does not check preconditions so if value is - //! greater than the minimum inserted key tree ordering invariant will be broken. - //! This function is slightly more efficient than using "insert_before". - //! This is a low-level function to be used only for performance reasons - //! by advanced users. - void push_front(reference value) - { - 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)); - node_algorithms::push_front(this->priv_header_ptr(), to_insert); - this->priv_size_traits().increment(); - } - - //! Effects: Erases the element pointed to by pos. - //! - //! Complexity: Average complexity for erase element is constant time. - //! - //! Throws: Nothing. - //! - //! Note: Invalidates the iterators (but not the references) - //! to the erased elements. No destructors are called. - iterator erase(const_iterator i) - { - const_iterator ret(i); - ++ret; - node_ptr to_erase(i.pointed_node()); - if(safemode_or_autounlink) - BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!node_algorithms::unique(to_erase)); - node_algorithms::erase(this->priv_header_ptr(), to_erase); - this->priv_size_traits().decrement(); - if(safemode_or_autounlink) - node_algorithms::init(to_erase); - return ret.unconst(); - } - - //! Effects: Erases the range pointed to by b end e. - //! - //! Complexity: Average complexity for erase range is at most - //! O(log(size() + N)), where N is the number of elements in the range. - //! - //! Throws: Nothing. - //! - //! Note: Invalidates the iterators (but not the references) - //! to the erased elements. No destructors are called. - iterator erase(const_iterator b, const_iterator e) - { size_type n; return private_erase(b, e, n); } - - //! Effects: Erases all the elements with the given value. - //! - //! Returns: The number of erased elements. - //! - //! Complexity: O(log(size() + N). - //! - //! Throws: Nothing. - //! - //! Note: Invalidates the iterators (but not the references) - //! to the erased elements. No destructors are called. - size_type erase(const_reference value) - { return this->erase(value, priv_comp()); } - - //! Effects: Erases all the elements with the given key. - //! according to the comparison functor "comp". - //! - //! Returns: The number of erased elements. - //! - //! Complexity: O(log(size() + N). - //! - //! Throws: Nothing. - //! - //! Note: Invalidates the iterators (but not the references) - //! to the erased elements. No destructors are called. + ,KeyValueCompare key_value_comp, insert_commit_data &commit_data); + + //! @copydoc ::boost::intrusive::bstree::insert_unique_commit + iterator insert_unique_commit(reference value, const insert_commit_data &commit_data); + + //! @copydoc ::boost::intrusive::bstree::insert_unique(Iterator,Iterator) + template + void insert_unique(Iterator b, Iterator e); + + //! @copydoc ::boost::intrusive::bstree::insert_before + iterator insert_before(const_iterator pos, reference value); + + //! @copydoc ::boost::intrusive::bstree::push_back + void push_back(reference value); + + //! @copydoc ::boost::intrusive::bstree::push_front + void push_front(reference value); + + //! @copydoc ::boost::intrusive::bstree::erase(const_iterator) + iterator erase(const_iterator i); + + //! @copydoc ::boost::intrusive::bstree::erase(const_iterator,const_iterator) + iterator erase(const_iterator b, const_iterator e); + + //! @copydoc ::boost::intrusive::bstree::erase(const_reference) + size_type erase(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::erase(const KeyType&,KeyValueCompare) template - size_type erase(const KeyType& key, KeyValueCompare comp - /// @cond - , typename detail::enable_if_c::value >::type * = 0 - /// @endcond - ) - { - std::pair p = this->equal_range(key, comp); - size_type n; - private_erase(p.first, p.second, n); - return n; - } - - //! Requires: Disposer::operator()(pointer) shouldn't throw. - //! - //! Effects: Erases the element pointed to by pos. - //! Disposer::operator()(pointer) is called for the removed element. - //! - //! Complexity: Average complexity for erase element is constant time. - //! - //! Throws: Nothing. - //! - //! Note: Invalidates the iterators - //! to the erased elements. - template - iterator erase_and_dispose(const_iterator i, Disposer disposer) - { - node_ptr to_erase(i.pointed_node()); - iterator ret(this->erase(i)); - disposer(get_real_value_traits().to_value_ptr(to_erase)); - return ret; - } - - #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + size_type erase(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const_iterator,Disposer) template - iterator erase_and_dispose(iterator i, Disposer disposer) - { return this->erase_and_dispose(const_iterator(i), disposer); } - #endif - - //! Requires: Disposer::operator()(pointer) shouldn't throw. - //! - //! Effects: Erases the range pointed to by b end e. - //! Disposer::operator()(pointer) is called for the removed elements. - //! - //! Complexity: Average complexity for erase range is at most - //! O(log(size() + N)), where N is the number of elements in the range. - //! - //! Throws: Nothing. - //! - //! Note: Invalidates the iterators - //! to the erased elements. + iterator erase_and_dispose(const_iterator i, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const_iterator,const_iterator,Disposer) template - iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer) - { size_type n; return private_erase(b, e, n, disposer); } - - //! Requires: Disposer::operator()(pointer) shouldn't throw. - //! - //! Effects: Erases all the elements with the given value. - //! Disposer::operator()(pointer) is called for the removed elements. - //! - //! Returns: The number of erased elements. - //! - //! Complexity: O(log(size() + N). - //! - //! Throws: Nothing. - //! - //! Note: Invalidates the iterators (but not the references) - //! to the erased elements. No destructors are called. + iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const_reference, Disposer) template - size_type erase_and_dispose(const_reference value, Disposer disposer) - { - std::pair p = this->equal_range(value); - size_type n; - private_erase(p.first, p.second, n, disposer); - return n; - } - - //! Requires: Disposer::operator()(pointer) shouldn't throw. - //! - //! Effects: Erases all the elements with the given key. - //! according to the comparison functor "comp". - //! Disposer::operator()(pointer) is called for the removed elements. - //! - //! Returns: The number of erased elements. - //! - //! Complexity: O(log(size() + N). - //! - //! Throws: Nothing. - //! - //! Note: Invalidates the iterators - //! to the erased elements. + size_type erase_and_dispose(const_reference value, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const KeyType&,KeyValueCompare,Disposer) template - size_type erase_and_dispose(const KeyType& key, KeyValueCompare comp, Disposer disposer - /// @cond - , typename detail::enable_if_c::value >::type * = 0 - /// @endcond - ) - { - std::pair p = this->equal_range(key, comp); - size_type n; - private_erase(p.first, p.second, n, disposer); - return n; - } - - //! Effects: Erases all of the elements. - //! - //! Complexity: Linear to the number of elements on the container. - //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise. - //! - //! Throws: Nothing. - //! - //! Note: Invalidates the iterators (but not the references) - //! to the erased elements. No destructors are called. - void clear() - { - if(safemode_or_autounlink){ - this->clear_and_dispose(detail::null_disposer()); - } - else{ - node_algorithms::init_header(this->priv_header_ptr()); - this->priv_size_traits().set_size(0); - } - } - - //! Effects: Erases all of the elements calling disposer(p) for - //! each node to be erased. - //! Complexity: Average complexity for is at most O(log(size() + N)), - //! where N is the number of elements in the container. - //! - //! Throws: Nothing. - //! - //! Note: Invalidates the iterators (but not the references) - //! to the erased elements. Calls N times to disposer functor. + size_type erase_and_dispose(const KeyType& key, KeyValueCompare comp, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::clear + void clear(); + + //! @copydoc ::boost::intrusive::bstree::clear_and_dispose template - void clear_and_dispose(Disposer disposer) - { - node_algorithms::clear_and_dispose(this->priv_header_ptr() - , detail::node_disposer(disposer, this)); - node_algorithms::init_header(this->priv_header_ptr()); - this->priv_size_traits().set_size(0); - } - - //! Effects: Returns the number of contained elements with the given value - //! - //! Complexity: Logarithmic to the number of elements contained plus lineal - //! to number of objects with the given value. - //! - //! Throws: Nothing. - size_type count(const_reference value) const - { return this->count(value, priv_comp()); } - - //! Effects: Returns the number of contained elements with the given key - //! - //! Complexity: Logarithmic to the number of elements contained plus lineal - //! to number of objects with the given key. - //! - //! Throws: Nothing. + void clear_and_dispose(Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::count(const_reference)const + size_type count(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::count(const KeyType&,KeyValueCompare)const template - size_type count(const KeyType &key, KeyValueCompare comp) const - { - std::pair ret = this->equal_range(key, comp); - return std::distance(ret.first, ret.second); - } - - //! Effects: Returns an iterator to the first element whose - //! key is not less than k or end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: Nothing. - iterator lower_bound(const_reference value) - { return this->lower_bound(value, priv_comp()); } - - //! Effects: Returns an iterator to the first element whose - //! key is not less than k or end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: Nothing. - const_iterator lower_bound(const_reference value) const - { return this->lower_bound(value, priv_comp()); } - - //! Effects: Returns an iterator to the first element whose - //! key is not less than k or end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: Nothing. + size_type count(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::bstree::lower_bound(const_reference) + iterator lower_bound(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::lower_bound(const KeyType&,KeyValueCompare) template - iterator lower_bound(const KeyType &key, KeyValueCompare comp) - { - detail::key_nodeptr_comp - key_node_comp(comp, this); - return iterator(node_algorithms::lower_bound - (this->priv_header_ptr(), key, key_node_comp), this); - } - - //! Effects: Returns a const iterator to the first element whose - //! key is not less than k or end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: Nothing. + iterator lower_bound(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::lower_bound(const_reference)const + const_iterator lower_bound(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::lower_bound(const KeyType&,KeyValueCompare)const template - const_iterator lower_bound(const KeyType &key, KeyValueCompare comp) const - { - detail::key_nodeptr_comp - key_node_comp(comp, this); - return const_iterator(node_algorithms::lower_bound - (this->priv_header_ptr(), key, key_node_comp), this); - } - - //! Effects: Returns an iterator to the first element whose - //! key is greater than k or end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: Nothing. - iterator upper_bound(const_reference value) - { return this->upper_bound(value, priv_comp()); } - - //! Effects: Returns an iterator to the first element whose - //! key is greater than k according to comp or end() if that element - //! does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: Nothing. + const_iterator lower_bound(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::bstree::upper_bound(const_reference) + iterator upper_bound(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::upper_bound(const KeyType&,KeyValueCompare) template - iterator upper_bound(const KeyType &key, KeyValueCompare comp) - { - detail::key_nodeptr_comp - key_node_comp(comp, this); - return iterator(node_algorithms::upper_bound - (this->priv_header_ptr(), key, key_node_comp), this); - } - - //! Effects: Returns an iterator to the first element whose - //! key is greater than k or end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: Nothing. - const_iterator upper_bound(const_reference value) const - { return this->upper_bound(value, priv_comp()); } - - //! Effects: Returns an iterator to the first element whose - //! key is greater than k according to comp or end() if that element - //! does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: Nothing. + iterator upper_bound(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::upper_bound(const_reference)const + const_iterator upper_bound(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::upper_bound(const KeyType&,KeyValueCompare)const template - const_iterator upper_bound(const KeyType &key, KeyValueCompare comp) const - { - detail::key_nodeptr_comp - key_node_comp(comp, this); - return const_iterator(node_algorithms::upper_bound - (this->priv_header_ptr(), key, key_node_comp), this); - } - - //! Effects: Finds an iterator to the first element whose key is - //! k or end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: Nothing. - iterator find(const_reference value) - { return this->find(value, priv_comp()); } - - //! Effects: Finds an iterator to the first element whose key is - //! k or end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: Nothing. + const_iterator upper_bound(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::bstree::find(const_reference) + iterator find(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::find(const KeyType&,KeyValueCompare) template - iterator find(const KeyType &key, KeyValueCompare comp) - { - detail::key_nodeptr_comp - key_node_comp(comp, this); - return iterator - (node_algorithms::find(this->priv_header_ptr(), key, key_node_comp), this); - } - - //! Effects: Finds a const_iterator to the first element whose key is - //! k or end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: Nothing. - const_iterator find(const_reference value) const - { return this->find(value, priv_comp()); } - - //! Effects: Finds a const_iterator to the first element whose key is - //! k or end() if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: Nothing. + iterator find(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::find(const_reference)const + const_iterator find(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::find(const KeyType&,KeyValueCompare)const template - const_iterator find(const KeyType &key, KeyValueCompare comp) const - { - detail::key_nodeptr_comp - key_node_comp(comp, this); - return const_iterator - (node_algorithms::find(this->priv_header_ptr(), key, key_node_comp), this); - } - - //! Effects: Finds a range containing all elements whose key is k or - //! an empty range that indicates the position where those elements would be - //! if they there is no elements with key k. - //! - //! Complexity: Logarithmic. - //! - //! Throws: Nothing. - std::pair equal_range(const_reference value) - { return this->equal_range(value, priv_comp()); } - - //! Effects: Finds a range containing all elements whose key is k or - //! an empty range that indicates the position where those elements would be - //! if they there is no elements with key k. - //! - //! Complexity: Logarithmic. - //! - //! Throws: Nothing. + const_iterator find(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::bstree::equal_range(const_reference) + std::pair equal_range(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::equal_range(const KeyType&,KeyValueCompare) template - std::pair equal_range(const KeyType &key, KeyValueCompare comp) - { - detail::key_nodeptr_comp - key_node_comp(comp, this); - std::pair ret - (node_algorithms::equal_range(this->priv_header_ptr(), key, key_node_comp)); - return std::pair(iterator(ret.first, this), iterator(ret.second, this)); - } - - //! Effects: Finds a range containing all elements whose key is k or - //! an empty range that indicates the position where those elements would be - //! if they there is no elements with key k. - //! - //! Complexity: Logarithmic. - //! - //! Throws: Nothing. + std::pair equal_range(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::equal_range(const_reference)const std::pair - equal_range(const_reference value) const - { return this->equal_range(value, priv_comp()); } - - //! Effects: Finds a range containing all elements whose key is k or - //! an empty range that indicates the position where those elements would be - //! if they there is no elements with key k. - //! - //! Complexity: Logarithmic. - //! - //! Throws: Nothing. + equal_range(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::equal_range(const KeyType&,KeyValueCompare)const template std::pair - equal_range(const KeyType &key, KeyValueCompare comp) const - { - detail::key_nodeptr_comp - key_node_comp(comp, this); - std::pair ret - (node_algorithms::equal_range(this->priv_header_ptr(), key, key_node_comp)); - return std::pair(const_iterator(ret.first, this), const_iterator(ret.second, this)); - } - - //! Requires: 'lower_value' must not be greater than 'upper_value'. If - //! 'lower_value' == 'upper_value', ('left_closed' || 'right_closed') must be false. - //! - //! Effects: Returns an a pair with the following criteria: - //! - //! first = lower_bound(lower_key) if left_closed, upper_bound(lower_key) otherwise - //! - //! second = upper_bound(upper_key) if right_closed, lower_bound(upper_key) otherwise - //! - //! Complexity: Logarithmic. - //! - //! Throws: If the predicate throws. - //! - //! Note: This function can be more efficient than calling upper_bound - //! and lower_bound for lower_value and upper_value. + equal_range(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::bstree::bounded_range(const_reference,const_reference,bool,bool) std::pair bounded_range - (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed) - { return this->bounded_range(lower_value, upper_value, priv_comp(), left_closed, right_closed); } - - //! Requires: KeyValueCompare is a function object that induces a strict weak - //! ordering compatible with the strict weak ordering used to create the - //! the tree. - //! 'lower_key' must not be greater than 'upper_key' according to 'comp'. If - //! 'lower_key' == 'upper_key', ('left_closed' || 'right_closed') must be false. - //! - //! Effects: Returns an a pair with the following criteria: - //! - //! first = lower_bound(lower_key, comp) if left_closed, upper_bound(lower_key, comp) otherwise - //! - //! second = upper_bound(upper_key, comp) if right_closed, lower_bound(upper_key, comp) otherwise - //! - //! Complexity: Logarithmic. - //! - //! Throws: If "comp" throws. - //! - //! Note: This function can be more efficient than calling upper_bound - //! and lower_bound for lower_key and upper_key. + (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed); + + //! @copydoc ::boost::intrusive::bstree::bounded_range(const KeyType&,const KeyType&,KeyValueCompare,bool,bool) template std::pair bounded_range - (const KeyType &lower_key, const KeyType &upper_key, KeyValueCompare comp, bool left_closed, bool right_closed) - { - detail::key_nodeptr_comp - key_node_comp(comp, this); - std::pair ret - (node_algorithms::bounded_range - (this->priv_header_ptr(), lower_key, upper_key, key_node_comp, left_closed, right_closed)); - return std::pair(iterator(ret.first, this), iterator(ret.second, this)); - } - - //! Requires: 'lower_value' must not be greater than 'upper_value'. If - //! 'lower_value' == 'upper_value', ('left_closed' || 'right_closed') must be false. - //! - //! Effects: Returns an a pair with the following criteria: - //! - //! first = lower_bound(lower_key) if left_closed, upper_bound(lower_key) otherwise - //! - //! second = upper_bound(upper_key) if right_closed, lower_bound(upper_key) otherwise - //! - //! Complexity: Logarithmic. - //! - //! Throws: If the predicate throws. - //! - //! Note: This function can be more efficient than calling upper_bound - //! and lower_bound for lower_value and upper_value. - std::pair bounded_range - (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed) const - { return this->bounded_range(lower_value, upper_value, priv_comp(), left_closed, right_closed); } - - //! Requires: KeyValueCompare is a function object that induces a strict weak - //! ordering compatible with the strict weak ordering used to create the - //! the tree. - //! 'lower_key' must not be greater than 'upper_key' according to 'comp'. If - //! 'lower_key' == 'upper_key', ('left_closed' || 'right_closed') must be false. - //! - //! Effects: Returns an a pair with the following criteria: - //! - //! first = lower_bound(lower_key, comp) if left_closed, upper_bound(lower_key, comp) otherwise - //! - //! second = upper_bound(upper_key, comp) if right_closed, lower_bound(upper_key, comp) otherwise - //! - //! Complexity: Logarithmic. - //! - //! Throws: If "comp" throws. - //! - //! Note: This function can be more efficient than calling upper_bound - //! and lower_bound for lower_key and upper_key. + (const KeyType& lower_key, const KeyType& upper_key, KeyValueCompare comp, bool left_closed, bool right_closed); + + //! @copydoc ::boost::intrusive::bstree::bounded_range(const_reference,const_reference,bool,bool)const + std::pair + bounded_range(const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed) const; + + //! @copydoc ::boost::intrusive::bstree::bounded_range(const KeyType&,const KeyType&,KeyValueCompare,bool,bool)const template - std::pair bounded_range - (const KeyType &lower_key, const KeyType &upper_key, KeyValueCompare comp, bool left_closed, bool right_closed) const - { - detail::key_nodeptr_comp - key_node_comp(comp, this); - std::pair ret - (node_algorithms::bounded_range - (this->priv_header_ptr(), lower_key, upper_key, key_node_comp, left_closed, right_closed)); - return std::pair(const_iterator(ret.first, this), const_iterator(ret.second, this)); - } - - //! Requires: Disposer::operator()(pointer) shouldn't throw. - //! Cloner should yield to nodes equivalent to the original nodes. - //! - //! Effects: 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. Copies the predicate from the source container. - //! - //! If cloner throws, all cloned elements are unlinked and disposed - //! calling Disposer::operator()(pointer). - //! - //! Complexity: Linear to erased plus inserted elements. - //! - //! Throws: If cloner throws or predicate copy assignment throws. Basic guarantee. - template - void clone_from(const avltree_impl &src, Cloner cloner, Disposer disposer) - { - this->clear_and_dispose(disposer); - if(!src.empty()){ - detail::exception_disposer - rollback(*this, disposer); - node_algorithms::clone - (src.priv_header_ptr() - ,this->priv_header_ptr() - ,detail::node_cloner(cloner, this) - ,detail::node_disposer(disposer, this)); - this->priv_size_traits().set_size(src.priv_size_traits().get_size()); - this->priv_comp() = src.priv_comp(); - rollback.release(); - } - } - - //! Effects: Unlinks the leftmost node from the tree. - //! - //! Complexity: Average complexity is constant time. - //! - //! Throws: Nothing. - //! - //! Notes: 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() - { - node_ptr to_be_disposed(node_algorithms::unlink_leftmost_without_rebalance - (this->priv_header_ptr())); - if(!to_be_disposed) - return 0; - this->priv_size_traits().decrement(); - if(safemode_or_autounlink)//If this is commented does not work with normal_link - node_algorithms::init(to_be_disposed); - return get_real_value_traits().to_value_ptr(to_be_disposed); - } - - //! Requires: replace_this must be a valid iterator of *this - //! and with_this must not be inserted in any tree. - //! - //! Effects: Replaces replace_this in its position in the - //! tree with with_this. The tree does not need to be rebalanced. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - //! - //! Note: This function will break container ordering invariants if - //! with_this is not equivalent to *replace_this according to the - //! ordering rules. This function is faster than erasing and inserting - //! the node, since no rebalancing or comparison is needed. - void replace_node(iterator replace_this, reference with_this) - { - node_algorithms::replace_node( get_real_value_traits().to_node_ptr(*replace_this) - , this->priv_header_ptr() - , get_real_value_traits().to_node_ptr(with_this)); - if(safemode_or_autounlink) - node_algorithms::init(replace_this.pointed_node()); - } - - //! Requires: value must be an lvalue and shall be in a set of - //! appropriate type. Otherwise the behavior is undefined. - //! - //! Effects: Returns: a valid iterator i belonging to the set - //! that points to the value - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - //! - //! Note: This static function is available only if the value traits - //! is stateless. - static iterator s_iterator_to(reference value) - { - BOOST_STATIC_ASSERT((!stateful_value_traits)); - return iterator (value_traits::to_node_ptr(value), 0); - } - - //! Requires: value must be an lvalue and shall be in a set of - //! appropriate type. Otherwise the behavior is undefined. - //! - //! Effects: Returns: a valid const_iterator i belonging to the - //! set that points to the value - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - //! - //! Note: This static function is available only if the value traits - //! is stateless. - static const_iterator s_iterator_to(const_reference value) - { - BOOST_STATIC_ASSERT((!stateful_value_traits)); - return const_iterator (value_traits::to_node_ptr(const_cast (value)), 0); - } - - //! Requires: value must be an lvalue and shall be in a set of - //! appropriate type. Otherwise the behavior is undefined. - //! - //! Effects: Returns: a valid iterator i belonging to the set - //! that points to the value - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - iterator iterator_to(reference value) - { return iterator (value_traits::to_node_ptr(value), this); } - - //! Requires: value must be an lvalue and shall be in a set of - //! appropriate type. Otherwise the behavior is undefined. - //! - //! Effects: Returns: a valid const_iterator i belonging to the - //! set that points to the value - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - const_iterator iterator_to(const_reference value) const - { return const_iterator (value_traits::to_node_ptr(const_cast (value)), this); } - - //! Requires: value shall not be in a tree. - //! - //! Effects: init_node puts the hook of a value in a well-known default - //! state. - //! - //! Throws: Nothing. - //! - //! Complexity: Constant time. - //! - //! Note: This function puts the hook in the well-known default state - //! used by auto_unlink and safe hooks. - static void init_node(reference value) - { node_algorithms::init(value_traits::to_node_ptr(value)); } - -/* - //! Effects: removes x from a tree of the appropriate type. It has no effect, - //! if x is not in such a tree. - //! - //! Throws: Nothing. - //! - //! Complexity: Constant time. - //! - //! Note: This static function is only usable with the "safe mode" - //! hook and non-constant time size lists. Otherwise, the user must use - //! the non-static "erase(reference )" member. If the user calls - //! this function with a non "safe mode" or constant time size list - //! a compilation error will be issued. - template - static void remove_node(T& value) - { - //This function is only usable for safe mode hooks and non-constant - //time lists. - //BOOST_STATIC_ASSERT((!(safemode_or_autounlink && constant_time_size))); - BOOST_STATIC_ASSERT((!constant_time_size)); - BOOST_STATIC_ASSERT((boost::is_convertible::value)); - node_ptr to_remove(value_traits::to_node_ptr(value)); - node_algorithms::unlink_and_rebalance(to_remove); - if(safemode_or_autounlink) - node_algorithms::init(to_remove); - } -*/ + std::pair bounded_range + (const KeyType& lower_key, const KeyType& upper_key, KeyValueCompare comp, bool left_closed, bool right_closed) const; - /// @cond + //! @copydoc ::boost::intrusive::bstree::s_iterator_to(reference) + static iterator s_iterator_to(reference value); - private: - template - iterator private_erase(const_iterator b, const_iterator e, size_type &n, Disposer disposer) - { - for(n = 0; b != e; ++n) - this->erase_and_dispose(b++, disposer); - return b.unconst(); - } - - iterator private_erase(const_iterator b, const_iterator e, size_type &n) - { - for(n = 0; b != e; ++n) - this->erase(b++); - return b.unconst(); - } - /// @endcond + //! @copydoc ::boost::intrusive::bstree::s_iterator_to(const_reference) + static const_iterator s_iterator_to(const_reference value); - private: - static avltree_impl &priv_container_from_end_iterator(const const_iterator &end_iterator) - { - header_plus_size *r = detail::parent_from_member - ( boost::intrusive::detail::to_raw_pointer(end_iterator.pointed_node()), &header_plus_size::header_); - node_plus_pred_t *n = detail::parent_from_member - (r, &node_plus_pred_t::header_plus_size_); - data_t *d = detail::parent_from_member(n, &data_t::node_plus_pred_); - avltree_impl *avl = detail::parent_from_member(d, &avltree_impl::data_); - return *avl; - } - - static avltree_impl &priv_container_from_iterator(const const_iterator &it) - { return priv_container_from_end_iterator(it.end_iterator_from_it()); } + //! @copydoc ::boost::intrusive::bstree::iterator_to(reference) + iterator iterator_to(reference value); + + //! @copydoc ::boost::intrusive::bstree::iterator_to(const_reference)const + const_iterator iterator_to(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::init_node(reference) + static void init_node(reference value); + + //! @copydoc ::boost::intrusive::bstree::unlink_leftmost_without_rebalance + pointer unlink_leftmost_without_rebalance(); + + //! @copydoc ::boost::intrusive::bstree::replace_node + void replace_node(iterator replace_this, reference with_this); + + //! @copydoc ::boost::intrusive::bstree::remove_node + void remove_node(reference value); + #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED }; #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + template -#else -template -#endif -inline bool operator< -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) -(const avltree_impl &x, const avltree_impl &y) -#else -(const avltree_impl &x, const avltree_impl &y) -#endif -{ return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); } +bool operator< (const avltree_impl &x, const avltree_impl &y); -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) template -#else -template -#endif -bool operator== -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) -(const avltree_impl &x, const avltree_impl &y) -#else -(const avltree_impl &x, const avltree_impl &y) -#endif -{ - typedef avltree_impl tree_type; - typedef typename tree_type::const_iterator const_iterator; - - if(tree_type::constant_time_size && x.size() != y.size()){ - return false; - } - const_iterator end1 = x.end(); - const_iterator i1 = x.begin(); - const_iterator i2 = y.begin(); - if(tree_type::constant_time_size){ - while (i1 != end1 && *i1 == *i2) { - ++i1; - ++i2; - } - return i1 == end1; - } - else{ - const_iterator end2 = y.end(); - while (i1 != end1 && i2 != end2 && *i1 == *i2) { - ++i1; - ++i2; - } - return i1 == end1 && i2 == end2; - } -} +bool operator==(const avltree_impl &x, const avltree_impl &y); -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) template -#else -template -#endif -inline bool operator!= -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) -(const avltree_impl &x, const avltree_impl &y) -#else -(const avltree_impl &x, const avltree_impl &y) -#endif -{ return !(x == y); } +bool operator!= (const avltree_impl &x, const avltree_impl &y); -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) template -#else -template -#endif -inline bool operator> -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) -(const avltree_impl &x, const avltree_impl &y) -#else -(const avltree_impl &x, const avltree_impl &y) -#endif -{ return y < x; } +bool operator>(const avltree_impl &x, const avltree_impl &y); -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) template -#else -template -#endif -inline bool operator<= -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) -(const avltree_impl &x, const avltree_impl &y) -#else -(const avltree_impl &x, const avltree_impl &y) -#endif -{ return !(y < x); } +bool operator<=(const avltree_impl &x, const avltree_impl &y); -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) template -#else -template -#endif -inline bool operator>= -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) -(const avltree_impl &x, const avltree_impl &y) -#else -(const avltree_impl &x, const avltree_impl &y) -#endif -{ return !(x < y); } +bool operator>=(const avltree_impl &x, const avltree_impl &y); -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) template -#else -template -#endif -inline void swap -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) -(avltree_impl &x, avltree_impl &y) -#else -(avltree_impl &x, avltree_impl &y) -#endif -{ x.swap(y); } +void swap(avltree_impl &x, avltree_impl &y); -/// @cond +#endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) -#if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) -template -#else +//! Helper metafunction to define a \c avltree that yields to the same type when the +//! same options (either explicitly or implicitly) are used. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) template +#else +template #endif -struct make_avltree_opt +struct make_avltree { + /// @cond typedef typename pack_options + < avltree_defaults, #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) - < avl_set_defaults, O1, O2, O3, O4> + O1, O2, O3, O4, O5 #else - < avl_set_defaults, Options...> + Options... #endif - ::type packed_options; + >::type packed_options; typedef typename detail::get_value_traits - ::type value_traits; + ::type value_traits; + typedef typename detail::get_header_holder_type + < value_traits, typename packed_options::header_holder_type >::type header_holder_type; - typedef avl_setopt + typedef avltree_impl < value_traits , typename packed_options::compare , typename packed_options::size_type , packed_options::constant_time_size - > type; -}; -/// @endcond - -//! Helper metafunction to define a \c avltree that yields to the same type when the -//! same options (either explicitly or implicitly) are used. -#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) -template -#else -template -#endif -struct make_avltree -{ - /// @cond - typedef avltree_impl - #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) - < typename make_avltree_opt::type - #else - < typename make_avltree_opt::type - #endif - > implementation_defined; + , header_holder_type + > implementation_defined; /// @endcond typedef implementation_defined type; }; + #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) -template +template #else template #endif class avltree - #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) - : public make_avltree::type - #else - : public make_avltree::type - #endif + : public make_avltree::type { typedef typename make_avltree - #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) - - #else - - #endif - ::type Base; - + ::type Base; BOOST_MOVABLE_BUT_NOT_COPYABLE(avltree) public: typedef typename Base::value_compare value_compare; typedef typename Base::value_traits value_traits; - typedef typename Base::real_value_traits real_value_traits; typedef typename Base::iterator iterator; typedef typename Base::const_iterator const_iterator; + typedef typename Base::reverse_iterator reverse_iterator; + typedef typename Base::const_reverse_iterator const_reverse_iterator; //Assert if passed value traits are compatible with the type - BOOST_STATIC_ASSERT((detail::is_same::value)); + BOOST_STATIC_ASSERT((detail::is_same::value)); - avltree( const value_compare &cmp = value_compare() - , const value_traits &v_traits = value_traits()) + explicit avltree( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) : Base(cmp, v_traits) {} @@ -1760,7 +524,7 @@ class avltree {} avltree& operator=(BOOST_RV_REF(avltree) x) - { this->Base::operator=(::boost::move(static_cast(x))); return *this; } + { return static_cast(this->Base::operator=(::boost::move(static_cast(x)))); } static avltree &container_from_end_iterator(iterator end_iterator) { return static_cast(Base::container_from_end_iterator(end_iterator)); } @@ -1777,7 +541,6 @@ class avltree #endif - } //namespace intrusive } //namespace boost diff --git a/boost/intrusive/avltree_algorithms.hpp b/boost/intrusive/avltree_algorithms.hpp index 11463c71fb..e5bcfde160 100644 --- a/boost/intrusive/avltree_algorithms.hpp +++ b/boost/intrusive/avltree_algorithms.hpp @@ -1,7 +1,7 @@ ///////////////////////////////////////////////////////////////////////////// // // (C) Copyright Daniel K. O. 2005. -// (C) Copyright Ion Gaztanaga 2007-2012 +// (C) Copyright Ion Gaztanaga 2007-2014 // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at @@ -14,27 +14,93 @@ #ifndef BOOST_INTRUSIVE_AVLTREE_ALGORITHMS_HPP #define BOOST_INTRUSIVE_AVLTREE_ALGORITHMS_HPP +#if defined(_MSC_VER) +# pragma once +#endif + #include +#include #include -#include #include -#include -#include -#include - +#include +#include +#include namespace boost { namespace intrusive { +/// @cond + +template +struct avltree_node_cloner + : private detail::ebo_functor_holder +{ + typedef typename NodeTraits::node_ptr node_ptr; + typedef detail::ebo_functor_holder base_t; + + avltree_node_cloner(F f) + : base_t(f) + {} + + node_ptr operator()(const node_ptr & p) + { + node_ptr n = base_t::get()(p); + NodeTraits::set_balance(n, NodeTraits::get_balance(p)); + return n; + } +}; + +namespace detail { + +template +struct avltree_node_checker + : public bstree_node_checker +{ + typedef bstree_node_checker base_checker_t; + typedef ValueTraits value_traits; + typedef typename value_traits::node_traits node_traits; + typedef typename node_traits::const_node_ptr const_node_ptr; + + struct return_type + : public base_checker_t::return_type + { + return_type() : height(0) {} + int height; + }; + + avltree_node_checker(const NodePtrCompare& comp, ExtraChecker extra_checker) + : base_checker_t(comp, extra_checker) + {} + + void operator () (const const_node_ptr& p, + const return_type& check_return_left, const return_type& check_return_right, + return_type& check_return) + { + const int height_diff = check_return_right.height - check_return_left.height; (void)height_diff; + BOOST_INTRUSIVE_INVARIANT_ASSERT( + (height_diff == -1 && node_traits::get_balance(p) == node_traits::negative()) || + (height_diff == 0 && node_traits::get_balance(p) == node_traits::zero()) || + (height_diff == 1 && node_traits::get_balance(p) == node_traits::positive()) + ); + check_return.height = 1 + + (check_return_left.height > check_return_right.height ? check_return_left.height : check_return_right.height); + base_checker_t::operator()(p, check_return_left, check_return_right, check_return); + } +}; + +} // namespace detail + +/// @endcond + //! avltree_algorithms is configured with a NodeTraits class, which encapsulates the //! information about the node to be manipulated. NodeTraits must support the //! following interface: //! //! Typedefs: //! -//! node: The type of the node that forms the circular list +//! node: The type of the node that forms the binary search tree //! //! node_ptr: A pointer to a node //! @@ -67,6 +133,9 @@ namespace intrusive { //! static balance positive(); template class avltree_algorithms + #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + : public bstree_algorithms + #endif { public: typedef typename NodeTraits::node node; @@ -77,161 +146,69 @@ class avltree_algorithms /// @cond private: - typedef detail::tree_algorithms tree_algorithms; - - template - struct avltree_node_cloner - : private detail::ebo_functor_holder - { - typedef detail::ebo_functor_holder base_t; - - avltree_node_cloner(F f) - : base_t(f) - {} - - node_ptr operator()(const 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()(const node_ptr &to_erase, const node_ptr &successor) - { NodeTraits::set_balance(successor, NodeTraits::get_balance(to_erase)); } - }; + typedef bstree_algorithms bstree_algo; - static node_ptr uncast(const const_node_ptr & ptr) - { return pointer_traits::const_cast_from(ptr); } /// @endcond public: - static node_ptr begin_node(const const_node_ptr & header) - { return tree_algorithms::begin_node(header); } - - static node_ptr end_node(const 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; + typedef typename bstree_algo::insert_commit_data insert_commit_data; - //! Requires: header1 and header2 must be the header nodes - //! of two trees. - //! - //! Effects: Swaps two trees. After the function header1 will contain - //! links to the second tree and header2 will have links to the first tree. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - static void swap_tree(const node_ptr & header1, const node_ptr & header2) - { return tree_algorithms::swap_tree(header1, header2); } + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED - //! Requires: node1 and node2 can't be header nodes - //! of two trees. - //! - //! Effects: 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. - //! - //! Complexity: Logarithmic. - //! - //! Throws: Nothing. - //! - //! Note: This function will break container ordering invariants if - //! node1 and node2 are not equivalent according to the ordering rules. - //! - //!Experimental function + //! @copydoc ::boost::intrusive::bstree_algorithms::get_header(const const_node_ptr&) + static node_ptr get_header(const const_node_ptr & n); + + //! @copydoc ::boost::intrusive::bstree_algorithms::begin_node + static node_ptr begin_node(const const_node_ptr & header); + + //! @copydoc ::boost::intrusive::bstree_algorithms::end_node + static node_ptr end_node(const const_node_ptr & header); + + //! @copydoc ::boost::intrusive::bstree_algorithms::swap_tree + static void swap_tree(const node_ptr & header1, const node_ptr & header2); + + #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(const node_ptr&,const node_ptr&) static void swap_nodes(const node_ptr & node1, const node_ptr & node2) { if(node1 == node2) return; - node_ptr header1(tree_algorithms::get_header(node1)), header2(tree_algorithms::get_header(node2)); + node_ptr header1(bstree_algo::get_header(node1)), header2(bstree_algo::get_header(node2)); swap_nodes(node1, header1, node2, header2); } - //! Requires: node1 and node2 can't be header nodes - //! of two trees with header header1 and header2. - //! - //! Effects: 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. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - //! - //! Note: This function will break container ordering invariants if - //! node1 and node2 are not equivalent according to the ordering rules. - //! - //!Experimental function + //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(const node_ptr&,const node_ptr&,const node_ptr&,const node_ptr&) static void swap_nodes(const node_ptr & node1, const node_ptr & header1, const node_ptr & node2, const node_ptr & header2) { if(node1 == node2) return; - tree_algorithms::swap_nodes(node1, header1, node2, header2); + bstree_algo::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); } - //! Requires: node_to_be_replaced must be inserted in a tree - //! and new_node must not be inserted in a tree. - //! - //! Effects: Replaces node_to_be_replaced in its position in the - //! tree with new_node. The tree does not need to be rebalanced - //! - //! Complexity: Logarithmic. - //! - //! Throws: Nothing. - //! - //! Note: 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 + //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(const node_ptr&,const node_ptr&) static void replace_node(const node_ptr & node_to_be_replaced, const 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); + replace_node(node_to_be_replaced, bstree_algo::get_header(node_to_be_replaced), new_node); } - //! Requires: node_to_be_replaced must be inserted in a tree - //! with header "header" and new_node must not be inserted in a tree. - //! - //! Effects: Replaces node_to_be_replaced in its position in the - //! tree with new_node. The tree does not need to be rebalanced - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - //! - //! Note: 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 + //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(const node_ptr&,const node_ptr&,const node_ptr&) static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & header, const node_ptr & new_node) { - tree_algorithms::replace_node(node_to_be_replaced, header, new_node); + bstree_algo::replace_node(node_to_be_replaced, header, new_node); NodeTraits::set_balance(new_node, NodeTraits::get_balance(node_to_be_replaced)); } - //! Requires: node is a tree node but not the header. - //! - //! Effects: Unlinks the node and rebalances the tree. - //! - //! Complexity: Average complexity is constant time. - //! - //! Throws: Nothing. + //! @copydoc ::boost::intrusive::bstree_algorithms::unlink(const node_ptr&) static void unlink(const node_ptr & node) { node_ptr x = NodeTraits::get_parent(node); @@ -242,84 +219,25 @@ class avltree_algorithms } } - //! Requires: header is the header of a tree. - //! - //! Effects: Unlinks the leftmost node from the tree, and - //! updates the header link to the new leftmost node. - //! - //! Complexity: Average complexity is constant time. - //! - //! Throws: Nothing. - //! - //! Notes: 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(const node_ptr & header) - { return tree_algorithms::unlink_leftmost_without_rebalance(header); } - - //! Requires: node is a node of the tree or an node initialized - //! by init(...). - //! - //! Effects: Returns true if the node is initialized by init(). - //! - //! Complexity: Constant time. - //! - //! Throws: Nothing. - static bool unique(const const_node_ptr & node) - { return tree_algorithms::unique(node); } + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + //! @copydoc ::boost::intrusive::bstree_algorithms::unlink_leftmost_without_rebalance + static node_ptr unlink_leftmost_without_rebalance(const node_ptr & header); - //! Requires: node is a node of the tree but it's not the header. - //! - //! Effects: Returns the number of nodes of the subtree. - //! - //! Complexity: Linear time. - //! - //! Throws: Nothing. - static std::size_t count(const const_node_ptr & node) - { return tree_algorithms::count(node); } + //! @copydoc ::boost::intrusive::bstree_algorithms::unique(const const_node_ptr&) + static bool unique(const const_node_ptr & node); - //! Requires: header is the header node of the tree. - //! - //! Effects: Returns the number of nodes above the header. - //! - //! Complexity: Linear time. - //! - //! Throws: Nothing. - static std::size_t size(const const_node_ptr & header) - { return tree_algorithms::size(header); } + //! @copydoc ::boost::intrusive::bstree_algorithms::size(const const_node_ptr&) + static std::size_t size(const const_node_ptr & header); - //! Requires: p is a node from the tree except the header. - //! - //! Effects: Returns the next node of the tree. - //! - //! Complexity: Average constant time. - //! - //! Throws: Nothing. - static node_ptr next_node(const node_ptr & p) - { return tree_algorithms::next_node(p); } + //! @copydoc ::boost::intrusive::bstree_algorithms::next_node(const node_ptr&) + static node_ptr next_node(const node_ptr & node); - //! Requires: p is a node from the tree except the leftmost node. - //! - //! Effects: Returns the previous node of the tree. - //! - //! Complexity: Average constant time. - //! - //! Throws: Nothing. - static node_ptr prev_node(const node_ptr & p) - { return tree_algorithms::prev_node(p); } + //! @copydoc ::boost::intrusive::bstree_algorithms::prev_node(const node_ptr&) + static node_ptr prev_node(const node_ptr & node); - //! Requires: node must not be part of any tree. - //! - //! Effects: After the function unique(node) == true. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - //! - //! Nodes: If node is inserted in a tree, this function corrupts the tree. - static void init(const node_ptr & node) - { tree_algorithms::init(node); } + //! @copydoc ::boost::intrusive::bstree_algorithms::init(const node_ptr&) + static void init(const node_ptr & node); + #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED //! Requires: node must not be part of any tree. //! @@ -333,451 +251,238 @@ class avltree_algorithms //! Nodes: If node is inserted in a tree, this function corrupts the tree. static void init_header(const node_ptr & header) { - tree_algorithms::init_header(header); + bstree_algo::init_header(header); NodeTraits::set_balance(header, NodeTraits::zero()); } - //! Requires: header must be the header of a tree, z a node - //! of that tree and z != header. - //! - //! Effects: Erases node "z" from the tree with header "header". - //! - //! Complexity: Amortized constant time. - //! - //! Throws: Nothing. + //! @copydoc ::boost::intrusive::bstree_algorithms::erase(const node_ptr&,const node_ptr&) static node_ptr erase(const node_ptr & header, const 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; - + typename bstree_algo::data_for_rebalance info; + bstree_algo::erase(header, z, info); + if(info.y != z){ + NodeTraits::set_balance(info.y, NodeTraits::get_balance(z)); + } //Rebalance avltree - rebalance_after_erasure(header, x, x_parent); + rebalance_after_erasure(header, info.x, info.x_parent); return z; } - //! Requires: "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. - //! - //! Effects: First empties target tree calling - //! void disposer::operator()(const node_ptr &) for every node of the tree - //! except the header. - //! - //! Then, duplicates the entire tree pointed by "source_header" cloning each - //! source node with node_ptr Cloner::operator()(const node_ptr &) to obtain - //! the nodes of the target tree. If "cloner" throws, the cloned target nodes - //! are disposed using void disposer(const node_ptr &). - //! - //! Complexity: Linear to the number of element of the source tree plus the. - //! number of elements of tree target tree when calling this function. - //! - //! Throws: If cloner functor throws. If this happens target nodes are disposed. + //! @copydoc ::boost::intrusive::bstree_algorithms::clone(const const_node_ptr&,const node_ptr&,Cloner,Disposer) template static void clone (const const_node_ptr & source_header, const node_ptr & target_header, Cloner cloner, Disposer disposer) { - avltree_node_cloner new_cloner(cloner); - tree_algorithms::clone(source_header, target_header, new_cloner, disposer); + avltree_node_cloner new_cloner(cloner); + bstree_algo::clone(source_header, target_header, new_cloner, disposer); } - //! Requires: "disposer" must be an object function - //! taking a node_ptr parameter and shouldn't throw. - //! - //! Effects: Empties the target tree calling - //! void disposer::operator()(const node_ptr &) for every node of the tree - //! except the header. - //! - //! Complexity: Linear to the number of element of the source tree plus the. - //! number of elements of tree target tree when calling this function. - //! - //! Throws: If cloner functor throws. If this happens target nodes are disposed. + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + //! @copydoc ::boost::intrusive::bstree_algorithms::clear_and_dispose(const node_ptr&,Disposer) template - static void clear_and_dispose(const node_ptr & header, Disposer disposer) - { tree_algorithms::clear_and_dispose(header, disposer); } + static void clear_and_dispose(const node_ptr & header, Disposer disposer); - //! Requires: "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. - //! - //! Effects: 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. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If "comp" throws. + //! @copydoc ::boost::intrusive::bstree_algorithms::lower_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare) template static node_ptr lower_bound - (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) - { return tree_algorithms::lower_bound(header, key, comp); } + (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp); - //! Requires: "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. - //! - //! Effects: Returns an node_ptr to the first element that is greater - //! than "key" according to "comp" or "header" if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If "comp" throws. + //! @copydoc ::boost::intrusive::bstree_algorithms::upper_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare) template static node_ptr upper_bound - (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) - { return tree_algorithms::upper_bound(header, key, comp); } + (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp); - //! Requires: "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. - //! - //! Effects: Returns an node_ptr to the element that is equivalent to - //! "key" according to "comp" or "header" if that element does not exist. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If "comp" throws. + //! @copydoc ::boost::intrusive::bstree_algorithms::find(const const_node_ptr&,const KeyType&,KeyNodePtrCompare) template static node_ptr find - (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) - { return tree_algorithms::find(header, key, comp); } + (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp); - //! Requires: "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. - //! - //! Effects: 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. - //! - //! Complexity: Logarithmic. - //! - //! Throws: If "comp" throws. + //! @copydoc ::boost::intrusive::bstree_algorithms::equal_range(const const_node_ptr&,const KeyType&,KeyNodePtrCompare) template static std::pair equal_range - (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) - { return tree_algorithms::equal_range(header, key, comp); } - - //! Requires: "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. - //! 'lower_key' must not be greater than 'upper_key' according to 'comp'. If - //! 'lower_key' == 'upper_key', ('left_closed' || 'right_closed') must be false. - //! - //! Effects: Returns an a pair with the following criteria: - //! - //! first = lower_bound(lower_key) if left_closed, upper_bound(lower_key) otherwise - //! - //! second = upper_bound(upper_key) if right_closed, lower_bound(upper_key) otherwise - //! - //! Complexity: Logarithmic. - //! - //! Throws: If "comp" throws. - //! - //! Note: This function can be more efficient than calling upper_bound - //! and lower_bound for lower_key and upper_key. + (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp); + + //! @copydoc ::boost::intrusive::bstree_algorithms::bounded_range(const const_node_ptr&,const KeyType&,const KeyType&,KeyNodePtrCompare,bool,bool) template static std::pair bounded_range (const const_node_ptr & header, const KeyType &lower_key, const KeyType &upper_key, KeyNodePtrCompare comp - , bool left_closed, bool right_closed) - { return tree_algorithms::bounded_range(header, lower_key, upper_key, comp, left_closed, right_closed); } + , bool left_closed, bool right_closed); - //! Requires: "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. - //! - //! Effects: Inserts new_node into the tree before the upper bound - //! according to "comp". - //! - //! Complexity: Average complexity for insert element is at - //! most logarithmic. - //! - //! Throws: If "comp" throws. + //! @copydoc ::boost::intrusive::bstree_algorithms::count(const const_node_ptr&,const KeyType&,KeyNodePtrCompare) + template + static std::size_t count(const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp); + + #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_upper_bound(const node_ptr&,const node_ptr&,NodePtrCompare) template static node_ptr insert_equal_upper_bound (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp) { - tree_algorithms::insert_equal_upper_bound(h, new_node, comp); + bstree_algo::insert_equal_upper_bound(h, new_node, comp); rebalance_after_insertion(h, new_node); return new_node; } - //! Requires: "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. - //! - //! Effects: Inserts new_node into the tree before the lower bound - //! according to "comp". - //! - //! Complexity: Average complexity for insert element is at - //! most logarithmic. - //! - //! Throws: If "comp" throws. + //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_lower_bound(const node_ptr&,const node_ptr&,NodePtrCompare) template static node_ptr insert_equal_lower_bound (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp) { - tree_algorithms::insert_equal_lower_bound(h, new_node, comp); + bstree_algo::insert_equal_lower_bound(h, new_node, comp); rebalance_after_insertion(h, new_node); return new_node; } - //! Requires: "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. - //! - //! Effects: 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). - //! - //! Complexity: Logarithmic in general, but it is amortized - //! constant time if new_node is inserted immediately before "hint". - //! - //! Throws: If "comp" throws. + //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal(const node_ptr&,const node_ptr&,const node_ptr&,NodePtrCompare) template static node_ptr insert_equal (const node_ptr & header, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp) { - tree_algorithms::insert_equal(header, hint, new_node, comp); + bstree_algo::insert_equal(header, hint, new_node, comp); rebalance_after_insertion(header, new_node); return new_node; } - //! Requires: "header" must be the header node of a tree. - //! "pos" must be a valid iterator or header (end) node. - //! "pos" must be an iterator pointing to the successor to "new_node" - //! once inserted according to the order of already inserted nodes. This function does not - //! check "pos" and this precondition must be guaranteed by the caller. - //! - //! Effects: Inserts new_node into the tree before "pos". - //! - //! Complexity: Constant-time. - //! - //! Throws: Nothing. - //! - //! Note: If "pos" is not the successor of the newly inserted "new_node" - //! tree invariants might be broken. + //! @copydoc ::boost::intrusive::bstree_algorithms::insert_before(const node_ptr&,const node_ptr&,const node_ptr&) static node_ptr insert_before (const node_ptr & header, const node_ptr & pos, const node_ptr & new_node) { - tree_algorithms::insert_before(header, pos, new_node); + bstree_algo::insert_before(header, pos, new_node); rebalance_after_insertion(header, new_node); return new_node; } - //! Requires: "header" must be the header node of a tree. - //! "new_node" must be, according to the used ordering no less than the - //! greatest inserted key. - //! - //! Effects: Inserts new_node into the tree before "pos". - //! - //! Complexity: Constant-time. - //! - //! Throws: Nothing. - //! - //! Note: If "new_node" is less than the greatest inserted key - //! tree invariants are broken. This function is slightly faster than - //! using "insert_before". + //! @copydoc ::boost::intrusive::bstree_algorithms::push_back(const node_ptr&,const node_ptr&) static void push_back(const node_ptr & header, const node_ptr & new_node) { - tree_algorithms::push_back(header, new_node); + bstree_algo::push_back(header, new_node); rebalance_after_insertion(header, new_node); } - //! Requires: "header" must be the header node of a tree. - //! "new_node" must be, according to the used ordering, no greater than the - //! lowest inserted key. - //! - //! Effects: Inserts new_node into the tree before "pos". - //! - //! Complexity: Constant-time. - //! - //! Throws: Nothing. - //! - //! Note: If "new_node" is greater than the lowest inserted key - //! tree invariants are broken. This function is slightly faster than - //! using "insert_before". + //! @copydoc ::boost::intrusive::bstree_algorithms::push_front(const node_ptr&,const node_ptr&) static void push_front(const node_ptr & header, const node_ptr & new_node) { - tree_algorithms::push_front(header, new_node); + bstree_algo::push_front(header, new_node); rebalance_after_insertion(header, new_node); } - //! Requires: "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. - //! - //! Effects: 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. - //! - //! Returns: 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. - //! - //! Complexity: Average complexity is at most logarithmic. - //! - //! Throws: If "comp" throws. - //! - //! Notes: 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. + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const const_node_ptr&,const KeyType&,KeyNodePtrCompare,insert_commit_data&) template static std::pair insert_unique_check (const 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); } - - //! Requires: "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. - //! - //! Effects: 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). - //! - //! Returns: 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. - //! - //! Complexity: Average complexity is at most logarithmic, but it is - //! amortized constant time if new_node should be inserted immediately before "hint". - //! - //! Throws: If "comp" throws. - //! - //! Notes: 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. + ,KeyNodePtrCompare comp, insert_commit_data &commit_data); + + //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const const_node_ptr&,const node_ptr&,const KeyType&,KeyNodePtrCompare,insert_commit_data&) template static std::pair insert_unique_check (const const_node_ptr & header, const 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); } - - //! Requires: "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". - //! - //! - //! Effects: Inserts new_node in the set using the information obtained - //! from the "commit_data" that a previous "insert_check" filled. - //! - //! Complexity: Constant time. - //! - //! Throws: Nothing. - //! - //! Notes: 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. + ,KeyNodePtrCompare comp, insert_commit_data &commit_data); + #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_commit(const node_ptr&,const node_ptr&,const insert_commit_data &) static void insert_unique_commit (const node_ptr & header, const node_ptr & new_value, const insert_commit_data &commit_data) { - tree_algorithms::insert_unique_commit(header, new_value, commit_data); + bstree_algo::insert_unique_commit(header, new_value, commit_data); rebalance_after_insertion(header, new_value); } - //! Requires: "n" must be a node inserted in a tree. - //! - //! Effects: Returns a pointer to the header node of the tree. - //! - //! Complexity: Logarithmic. - //! - //! Throws: Nothing. - static node_ptr get_header(const node_ptr & n) - { return tree_algorithms::get_header(n); } + //! @copydoc ::boost::intrusive::bstree_algorithms::is_header + static bool is_header(const const_node_ptr & p) + { return NodeTraits::get_balance(p) == NodeTraits::zero() && bstree_algo::is_header(p); } + /// @cond + static bool verify(const node_ptr &header) + { + std::size_t height; + std::size_t count; + return verify_recursion(NodeTraits::get_parent(header), count, height); + } + private: - //! Requires: p is a node of a tree. - //! - //! Effects: Returns true if p is the header of the tree. - //! - //! Complexity: Constant. - //! - //! Throws: Nothing. - static bool is_header(const const_node_ptr & p) - { return NodeTraits::get_balance(p) == NodeTraits::zero() && tree_algorithms::is_header(p); } + static bool verify_recursion(node_ptr n, std::size_t &count, std::size_t &height) + { + if (!n){ + count = 0; + height = 0; + return true; + } + std::size_t leftcount, rightcount; + std::size_t leftheight, rightheight; + if(!verify_recursion(NodeTraits::get_left (n), leftcount, leftheight) || + !verify_recursion(NodeTraits::get_right(n), rightcount, rightheight) ){ + return false; + } + count = 1u + leftcount + rightcount; + height = 1u + (leftheight > rightheight ? leftheight : rightheight); + + //If equal height, balance must be zero + if(rightheight == leftheight){ + if(NodeTraits::get_balance(n) != NodeTraits::zero()){ + BOOST_ASSERT(0); + return false; + } + } + //If right is taller than left, then the difference must be at least 1 and the balance positive + else if(rightheight > leftheight){ + if(rightheight - leftheight > 1 ){ + BOOST_ASSERT(0); + return false; + } + else if(NodeTraits::get_balance(n) != NodeTraits::positive()){ + BOOST_ASSERT(0); + return false; + } + } + //If left is taller than right, then the difference must be at least 1 and the balance negative + else{ + if(leftheight - rightheight > 1 ){ + BOOST_ASSERT(0); + return false; + } + else if(NodeTraits::get_balance(n) != NodeTraits::negative()){ + BOOST_ASSERT(0); + return false; + } + } + return true; + } - static void rebalance_after_erasure(const node_ptr & header, const node_ptr & xnode, const node_ptr & xnode_parent) + static void rebalance_after_erasure(const node_ptr & header, node_ptr x, node_ptr x_parent) { - node_ptr x(xnode), x_parent(xnode_parent); - for (node_ptr root = NodeTraits::get_parent(header); x != root; root = NodeTraits::get_parent(header)) { + for ( node_ptr root = NodeTraits::get_parent(header) + ; x != root + ; root = NodeTraits::get_parent(header), x_parent = NodeTraits::get_parent(x)) { const balance x_parent_balance = NodeTraits::get_balance(x_parent); + //Don't cache x_is_leftchild or similar because x can be null and + //equal to both x_parent_left and x_parent_right + const node_ptr x_parent_left (NodeTraits::get_left(x_parent)); + const node_ptr x_parent_right(NodeTraits::get_right(x_parent)); + if(x_parent_balance == NodeTraits::zero()){ - NodeTraits::set_balance(x_parent, - (x == NodeTraits::get_right(x_parent) ? NodeTraits::negative() : NodeTraits::positive())); + NodeTraits::set_balance( x_parent, x == x_parent_right ? 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)) { + if (x == x_parent_left) { ////x is left child or x and sibling are null 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); - BOOST_INTRUSIVE_INVARIANT_ASSERT(a); - if (NodeTraits::get_balance(a) == NodeTraits::positive()) { - // a MUST have a right child - BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_right(a)); - rotate_left_right(x_parent, header); - x = NodeTraits::get_parent(x_parent); - x_parent = NodeTraits::get_parent(x); + // x is right child (x_parent_left is the left child) + BOOST_INTRUSIVE_INVARIANT_ASSERT(x_parent_left); + if (NodeTraits::get_balance(x_parent_left) == NodeTraits::positive()) { + // x_parent_left MUST have a right child + BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_right(x_parent_left)); + x = avl_rotate_left_right(x_parent, x_parent_left, header); } else { - rotate_right(x_parent, header); - x = NodeTraits::get_parent(x_parent); - x_parent = NodeTraits::get_parent(x); + avl_rotate_right(x_parent, x_parent_left, header); + x = x_parent_left; } // if changed from negative to NodeTraits::positive(), no need to check above @@ -787,28 +492,21 @@ class avltree_algorithms } } else if(x_parent_balance == NodeTraits::positive()){ - if (x == NodeTraits::get_right(x_parent)) { + if (x == x_parent_right) { //x is right child or x and sibling are null 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); - BOOST_INTRUSIVE_INVARIANT_ASSERT(a); - if (NodeTraits::get_balance(a) == NodeTraits::negative()) { - // a MUST have then a left child - BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_left(a)); - rotate_right_left(x_parent, header); - - x = NodeTraits::get_parent(x_parent); - x_parent = NodeTraits::get_parent(x); + // x is left child (x_parent_right is the right child) + BOOST_INTRUSIVE_INVARIANT_ASSERT(x_parent_right); + if (NodeTraits::get_balance(x_parent_right) == NodeTraits::negative()) { + // x_parent_right MUST have then a left child + BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_left(x_parent_right)); + x = avl_rotate_right_left(x_parent, x_parent_right, header); } else { - rotate_left(x_parent, header); - x = NodeTraits::get_parent(x_parent); - x_parent = NodeTraits::get_parent(x); + avl_rotate_left(x_parent, x_parent_right, header); + x = x_parent_right; } // if changed from NodeTraits::positive() to negative, no need to check above if (NodeTraits::get_balance(x) == NodeTraits::negative()){ @@ -822,44 +520,43 @@ class avltree_algorithms } } - static void rebalance_after_insertion(const node_ptr & header, const node_ptr & xnode) + static void rebalance_after_insertion(const node_ptr & header, node_ptr x) { - node_ptr x(xnode); NodeTraits::set_balance(x, NodeTraits::zero()); // Rebalance. for(node_ptr root = NodeTraits::get_parent(header); x != root; root = NodeTraits::get_parent(header)){ - const balance x_parent_balance = NodeTraits::get_balance(NodeTraits::get_parent(x)); - + node_ptr const x_parent(NodeTraits::get_parent(x)); + node_ptr const x_parent_left(NodeTraits::get_left(x_parent)); + const balance x_parent_balance = NodeTraits::get_balance(x_parent); + const bool x_is_leftchild(x == x_parent_left); 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); + NodeTraits::set_balance( x_parent, x_is_leftchild ? NodeTraits::negative() : NodeTraits::positive() ); + x = x_parent; } 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()); + if (x_is_leftchild) + NodeTraits::set_balance(x_parent, NodeTraits::zero()); else{ // x is a right child, needs rebalancing if (NodeTraits::get_balance(x) == NodeTraits::negative()) - rotate_right_left(NodeTraits::get_parent(x), header); + avl_rotate_right_left(x_parent, x, header); else - rotate_left(NodeTraits::get_parent(x), header); + avl_rotate_left(x_parent, x, header); } 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 (x_is_leftchild) { if (NodeTraits::get_balance(x) == NodeTraits::positive()) - rotate_left_right(NodeTraits::get_parent(x), header); + avl_rotate_left_right(x_parent, x, header); else - rotate_right(NodeTraits::get_parent(x), header); + avl_rotate_right(x_parent, x, header); } else - NodeTraits::set_balance(NodeTraits::get_parent(x), NodeTraits::zero()); + NodeTraits::set_balance(x_parent, NodeTraits::zero()); break; } else{ @@ -873,26 +570,28 @@ class avltree_algorithms // balancing... const balance c_balance = NodeTraits::get_balance(c); const balance zero_balance = NodeTraits::zero(); + const balance posi_balance = NodeTraits::positive(); + const balance nega_balance = NodeTraits::negative(); NodeTraits::set_balance(c, zero_balance); - if(c_balance == NodeTraits::negative()){ - NodeTraits::set_balance(a, NodeTraits::positive()); + if(c_balance == nega_balance){ + NodeTraits::set_balance(a, posi_balance); NodeTraits::set_balance(b, zero_balance); } else if(c_balance == zero_balance){ NodeTraits::set_balance(a, zero_balance); NodeTraits::set_balance(b, zero_balance); } - else if(c_balance == NodeTraits::positive()){ + else if(c_balance == posi_balance){ NodeTraits::set_balance(a, zero_balance); - NodeTraits::set_balance(b, NodeTraits::negative()); + NodeTraits::set_balance(b, nega_balance); } else{ BOOST_INTRUSIVE_INVARIANT_ASSERT(false); // never reached } } - static void rotate_left_right(const node_ptr a, const node_ptr & hdr) - { + static node_ptr avl_rotate_left_right(const node_ptr a, const node_ptr a_oldleft, const node_ptr & hdr) + { // [note: 'a_oldleft' is 'b'] // | | // // a(-2) c // // / \ / \ // @@ -900,16 +599,19 @@ class avltree_algorithms // (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); - tree_algorithms::rotate_left(b, hdr); - tree_algorithms::rotate_right(a, hdr); - left_right_balancing(a, b, c); + // / \ // + // e f // + const node_ptr c = NodeTraits::get_right(a_oldleft); + bstree_algo::rotate_left_no_parent_fix(a_oldleft, c); + //No need to link c with a [NodeTraits::set_parent(c, a) + NodeTraits::set_left(a, c)] + //as c is not root and another rotation is coming + bstree_algo::rotate_right(a, c, NodeTraits::get_parent(a), hdr); + left_right_balancing(a, a_oldleft, c); + return c; } - static void rotate_right_left(const node_ptr a, const node_ptr & hdr) - { + static node_ptr avl_rotate_right_left(const node_ptr a, const node_ptr a_oldright, const node_ptr & hdr) + { // [note: 'a_oldright' is 'b'] // | | // // a(pos) c // // / \ / \ // @@ -919,47 +621,64 @@ class avltree_algorithms // c [g] [d] e f [g] // // / \ // // e f // - node_ptr b = NodeTraits::get_right(a), c = NodeTraits::get_left(b); - tree_algorithms::rotate_right(b, hdr); - tree_algorithms::rotate_left(a, hdr); - left_right_balancing(b, a, c); + const node_ptr c (NodeTraits::get_left(a_oldright)); + bstree_algo::rotate_right_no_parent_fix(a_oldright, c); + //No need to link c with a [NodeTraits::set_parent(c, a) + NodeTraits::set_right(a, c)] + //as c is not root and another rotation is coming. + bstree_algo::rotate_left(a, c, NodeTraits::get_parent(a), hdr); + left_right_balancing(a_oldright, a, c); + return c; } - static void rotate_left(const node_ptr x, const node_ptr & hdr) + static void avl_rotate_left(const node_ptr &x, const node_ptr &x_oldright, const node_ptr & hdr) { - const node_ptr y = NodeTraits::get_right(x); - tree_algorithms::rotate_left(x, hdr); + bstree_algo::rotate_left(x, x_oldright, NodeTraits::get_parent(x), hdr); // reset the balancing factor - if (NodeTraits::get_balance(y) == NodeTraits::positive()) { + if (NodeTraits::get_balance(x_oldright) == NodeTraits::positive()) { NodeTraits::set_balance(x, NodeTraits::zero()); - NodeTraits::set_balance(y, NodeTraits::zero()); + NodeTraits::set_balance(x_oldright, NodeTraits::zero()); } else { // this doesn't happen during insertions NodeTraits::set_balance(x, NodeTraits::positive()); - NodeTraits::set_balance(y, NodeTraits::negative()); + NodeTraits::set_balance(x_oldright, NodeTraits::negative()); } } - static void rotate_right(const node_ptr x, const node_ptr & hdr) + static void avl_rotate_right(const node_ptr &x, const node_ptr &x_oldleft, const node_ptr & hdr) { - const node_ptr y = NodeTraits::get_left(x); - tree_algorithms::rotate_right(x, hdr); + bstree_algo::rotate_right(x, x_oldleft, NodeTraits::get_parent(x), hdr); // reset the balancing factor - if (NodeTraits::get_balance(y) == NodeTraits::negative()) { + if (NodeTraits::get_balance(x_oldleft) == NodeTraits::negative()) { NodeTraits::set_balance(x, NodeTraits::zero()); - NodeTraits::set_balance(y, NodeTraits::zero()); + NodeTraits::set_balance(x_oldleft, NodeTraits::zero()); } else { // this doesn't happen during insertions NodeTraits::set_balance(x, NodeTraits::negative()); - NodeTraits::set_balance(y, NodeTraits::positive()); + NodeTraits::set_balance(x_oldleft, NodeTraits::positive()); } } /// @endcond }; +/// @cond + +template +struct get_algo +{ + typedef avltree_algorithms type; +}; + +template +struct get_node_checker +{ + typedef detail::avltree_node_checker type; +}; + +/// @endcond + } //namespace intrusive } //namespace boost diff --git a/boost/intrusive/bs_set.hpp b/boost/intrusive/bs_set.hpp new file mode 100644 index 0000000000..b87a543a5e --- /dev/null +++ b/boost/intrusive/bs_set.hpp @@ -0,0 +1,948 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2013-2014 +// +// 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_BS_SET_HPP +#define BOOST_INTRUSIVE_BS_SET_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace intrusive { + +//! The class template bs_set is an intrusive container, that mimics most of +//! the interface of std::set as described in the C++ standard. +//! +//! The template parameter \c T is the type to be managed by the container. +//! The user can specify additional options and if no options are provided +//! default options are used. +//! +//! The container supports the following options: +//! \c base_hook<>/member_hook<>/value_traits<>, +//! \c constant_time_size<>, \c size_type<> and +//! \c compare<>. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +template +#else +template +#endif +class bs_set_impl +#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + : public bstree_impl +#endif +{ + /// @cond + typedef bstree_impl tree_type; + BOOST_MOVABLE_BUT_NOT_COPYABLE(bs_set_impl) + + typedef tree_type implementation_defined; + /// @endcond + + public: + typedef typename implementation_defined::value_type value_type; + typedef typename implementation_defined::value_traits value_traits; + typedef typename implementation_defined::pointer pointer; + typedef typename implementation_defined::const_pointer const_pointer; + typedef typename implementation_defined::reference reference; + typedef typename implementation_defined::const_reference const_reference; + typedef typename implementation_defined::difference_type difference_type; + typedef typename implementation_defined::size_type size_type; + typedef typename implementation_defined::value_compare value_compare; + typedef typename implementation_defined::key_compare key_compare; + typedef typename implementation_defined::iterator iterator; + typedef typename implementation_defined::const_iterator const_iterator; + typedef typename implementation_defined::reverse_iterator reverse_iterator; + typedef typename implementation_defined::const_reverse_iterator const_reverse_iterator; + typedef typename implementation_defined::insert_commit_data insert_commit_data; + typedef typename implementation_defined::node_traits node_traits; + typedef typename implementation_defined::node node; + typedef typename implementation_defined::node_ptr node_ptr; + typedef typename implementation_defined::const_node_ptr const_node_ptr; + typedef typename implementation_defined::node_algorithms node_algorithms; + + static const bool constant_time_size = tree_type::constant_time_size; + + public: + //! @copydoc ::boost::intrusive::bstree::bstree(const value_compare &,const value_traits &) + explicit bs_set_impl( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : tree_type(cmp, v_traits) + {} + + //! @copydoc ::boost::intrusive::bstree::bstree(bool,Iterator,Iterator,const value_compare &,const value_traits &) + template + bs_set_impl( Iterator b, Iterator e + , const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : tree_type(true, b, e, cmp, v_traits) + {} + + //! @copydoc ::boost::intrusive::bstree::bstree(bstree &&) + bs_set_impl(BOOST_RV_REF(bs_set_impl) x) + : tree_type(::boost::move(static_cast(x))) + {} + + //! @copydoc ::boost::intrusive::bstree::operator=(bstree &&) + bs_set_impl& operator=(BOOST_RV_REF(bs_set_impl) x) + { return static_cast(tree_type::operator=(::boost::move(static_cast(x)))); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + //! @copydoc ::boost::intrusive::bstree::~bstree() + ~bs_set_impl(); + + //! @copydoc ::boost::intrusive::bstree::begin() + iterator begin(); + + //! @copydoc ::boost::intrusive::bstree::begin()const + const_iterator begin() const; + + //! @copydoc ::boost::intrusive::bstree::cbegin()const + const_iterator cbegin() const; + + //! @copydoc ::boost::intrusive::bstree::end() + iterator end(); + + //! @copydoc ::boost::intrusive::bstree::end()const + const_iterator end() const; + + //! @copydoc ::boost::intrusive::bstree::cend()const + const_iterator cend() const; + + //! @copydoc ::boost::intrusive::bstree::rbegin() + reverse_iterator rbegin(); + + //! @copydoc ::boost::intrusive::bstree::rbegin()const + const_reverse_iterator rbegin() const; + + //! @copydoc ::boost::intrusive::bstree::crbegin()const + const_reverse_iterator crbegin() const; + + //! @copydoc ::boost::intrusive::bstree::rend() + reverse_iterator rend(); + + //! @copydoc ::boost::intrusive::bstree::rend()const + const_reverse_iterator rend() const; + + //! @copydoc ::boost::intrusive::bstree::crend()const + const_reverse_iterator crend() const; + + //! @copydoc ::boost::intrusive::bstree::container_from_end_iterator(iterator) + static bs_set_impl &container_from_end_iterator(iterator end_iterator); + + //! @copydoc ::boost::intrusive::bstree::container_from_end_iterator(const_iterator) + static const bs_set_impl &container_from_end_iterator(const_iterator end_iterator); + + //! @copydoc ::boost::intrusive::bstree::container_from_iterator(iterator) + static bs_set_impl &container_from_iterator(iterator it); + + //! @copydoc ::boost::intrusive::bstree::container_from_iterator(const_iterator) + static const bs_set_impl &container_from_iterator(const_iterator it); + + //! @copydoc ::boost::intrusive::bstree::key_comp()const + key_compare key_comp() const; + + //! @copydoc ::boost::intrusive::bstree::value_comp()const + value_compare value_comp() const; + + //! @copydoc ::boost::intrusive::bstree::empty()const + bool empty() const; + + //! @copydoc ::boost::intrusive::bstree::size()const + size_type size() const; + + //! @copydoc ::boost::intrusive::bstree::swap + void swap(bs_set_impl& other); + + //! @copydoc ::boost::intrusive::bstree::clone_from + template + void clone_from(const bs_set_impl &src, Cloner cloner, Disposer disposer); + + #endif //#ifdef BOOST_iNTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::bstree::insert_unique(reference) + std::pair insert(reference value) + { return tree_type::insert_unique(value); } + + //! @copydoc ::boost::intrusive::bstree::insert_unique(const_iterator,reference) + iterator insert(const_iterator hint, reference value) + { return tree_type::insert_unique(hint, value); } + + //! @copydoc ::boost::intrusive::bstree::insert_unique_check(const KeyType&,KeyValueCompare,insert_commit_data&) + template + std::pair insert_check + (const KeyType &key, KeyValueCompare key_value_comp, insert_commit_data &commit_data) + { return tree_type::insert_unique_check(key, key_value_comp, commit_data); } + + //! @copydoc ::boost::intrusive::bstree::insert_unique_check(const_iterator,const KeyType&,KeyValueCompare,insert_commit_data&) + template + std::pair insert_check + (const_iterator hint, const KeyType &key + ,KeyValueCompare key_value_comp, insert_commit_data &commit_data) + { return tree_type::insert_unique_check(hint, key, key_value_comp, commit_data); } + + //! @copydoc ::boost::intrusive::bstree::insert_unique(Iterator,Iterator) + template + void insert(Iterator b, Iterator e) + { tree_type::insert_unique(b, e); } + + //! @copydoc ::boost::intrusive::bstree::insert_unique_commit + iterator insert_commit(reference value, const insert_commit_data &commit_data) + { return tree_type::insert_unique_commit(value, commit_data); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + //! @copydoc ::boost::intrusive::bstree::insert_before + iterator insert_before(const_iterator pos, reference value); + + //! @copydoc ::boost::intrusive::bstree::push_back + void push_back(reference value); + + //! @copydoc ::boost::intrusive::bstree::push_front + void push_front(reference value); + + //! @copydoc ::boost::intrusive::bstree::erase(const_iterator) + iterator erase(const_iterator i); + + //! @copydoc ::boost::intrusive::bstree::erase(const_iterator,const_iterator) + iterator erase(const_iterator b, const_iterator e); + + //! @copydoc ::boost::intrusive::bstree::erase(const_reference) + size_type erase(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::erase(const KeyType&,KeyValueCompare) + template + size_type erase(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const_iterator,Disposer) + template + iterator erase_and_dispose(const_iterator i, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const_iterator,const_iterator,Disposer) + template + iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const_reference, Disposer) + template + size_type erase_and_dispose(const_reference value, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const KeyType&,KeyValueCompare,Disposer) + template + size_type erase_and_dispose(const KeyType& key, KeyValueCompare comp, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::clear + void clear(); + + //! @copydoc ::boost::intrusive::bstree::clear_and_dispose + template + void clear_and_dispose(Disposer disposer); + + #endif // #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::bstree::count(const_reference)const + size_type count(const_reference value) const + { return static_cast(this->tree_type::find(value) == this->tree_type::cend()); } + + //! @copydoc ::boost::intrusive::bstree::count(const KeyType&,KeyValueCompare)const + template + size_type count(const KeyType& key, KeyValueCompare comp) const + { return static_cast(this->tree_type::find(key, comp) == this->tree_type::cend()); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::bstree::lower_bound(const_reference) + iterator lower_bound(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::lower_bound(const KeyType&,KeyValueCompare) + template + iterator lower_bound(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::lower_bound(const_reference)const + const_iterator lower_bound(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::lower_bound(const KeyType&,KeyValueCompare)const + template + const_iterator lower_bound(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::bstree::upper_bound(const_reference) + iterator upper_bound(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::upper_bound(const KeyType&,KeyValueCompare) + template + iterator upper_bound(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::upper_bound(const_reference)const + const_iterator upper_bound(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::upper_bound(const KeyType&,KeyValueCompare)const + template + const_iterator upper_bound(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::bstree::find(const_reference) + iterator find(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::find(const KeyType&,KeyValueCompare) + template + iterator find(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::find(const_reference)const + const_iterator find(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::find(const KeyType&,KeyValueCompare)const + template + const_iterator find(const KeyType& key, KeyValueCompare comp) const; + + #endif // #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::rbtree::equal_range(const_reference) + std::pair equal_range(const_reference value) + { return this->tree_type::lower_bound_range(value); } + + //! @copydoc ::boost::intrusive::rbtree::equal_range(const KeyType&,KeyValueCompare) + template + std::pair equal_range(const KeyType& key, KeyValueCompare comp) + { return this->tree_type::lower_bound_range(key, comp); } + + //! @copydoc ::boost::intrusive::rbtree::equal_range(const_reference)const + std::pair + equal_range(const_reference value) const + { return this->tree_type::lower_bound_range(value); } + + //! @copydoc ::boost::intrusive::rbtree::equal_range(const KeyType&,KeyValueCompare)const + template + std::pair + equal_range(const KeyType& key, KeyValueCompare comp) const + { return this->tree_type::lower_bound_range(key, comp); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::bstree::bounded_range(const_reference,const_reference,bool,bool) + std::pair bounded_range + (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed); + + //! @copydoc ::boost::intrusive::bstree::bounded_range(const KeyType&,const KeyType&,KeyValueCompare,bool,bool) + template + std::pair bounded_range + (const KeyType& lower_key, const KeyType& upper_key, KeyValueCompare comp, bool left_closed, bool right_closed); + + //! @copydoc ::boost::intrusive::bstree::bounded_range(const_reference,const_reference,bool,bool)const + std::pair + bounded_range(const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed) const; + + //! @copydoc ::boost::intrusive::bstree::bounded_range(const KeyType&,const KeyType&,KeyValueCompare,bool,bool)const + template + std::pair bounded_range + (const KeyType& lower_key, const KeyType& upper_key, KeyValueCompare comp, bool left_closed, bool right_closed) const; + + //! @copydoc ::boost::intrusive::bstree::s_iterator_to(reference) + static iterator s_iterator_to(reference value); + + //! @copydoc ::boost::intrusive::bstree::s_iterator_to(const_reference) + static const_iterator s_iterator_to(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::iterator_to(reference) + iterator iterator_to(reference value); + + //! @copydoc ::boost::intrusive::bstree::iterator_to(const_reference)const + const_iterator iterator_to(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::init_node(reference) + static void init_node(reference value); + + //! @copydoc ::boost::intrusive::bstree::unlink_leftmost_without_rebalance + pointer unlink_leftmost_without_rebalance(); + + //! @copydoc ::boost::intrusive::bstree::replace_node + void replace_node(iterator replace_this, reference with_this); + + //! @copydoc ::boost::intrusive::bstree::remove_node + void remove_node(reference value); + #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +}; + +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + +template +bool operator!= (const bs_set_impl &x, const bs_set_impl &y); + +template +bool operator>(const bs_set_impl &x, const bs_set_impl &y); + +template +bool operator<=(const bs_set_impl &x, const bs_set_impl &y); + +template +bool operator>=(const bs_set_impl &x, const bs_set_impl &y); + +template +void swap(bs_set_impl &x, bs_set_impl &y); + +#endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + +//! Helper metafunction to define a \c bs_set that yields to the same type when the +//! same options (either explicitly or implicitly) are used. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +struct make_bs_set +{ + /// @cond + typedef typename pack_options + < bstree_defaults, + #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) + O1, O2, O3, O4, O5 + #else + Options... + #endif + >::type packed_options; + + typedef typename detail::get_value_traits + ::type value_traits; + typedef typename detail::get_header_holder_type + < value_traits, typename packed_options::header_holder_type >::type header_holder_type; + + typedef bs_set_impl + < value_traits + , typename packed_options::compare + , typename packed_options::size_type + , packed_options::constant_time_size + , header_holder_type + > implementation_defined; + /// @endcond + typedef implementation_defined type; +}; + +#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED +#if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +class bs_set + : public make_bs_set::type +{ + typedef typename make_bs_set + ::type Base; + + BOOST_MOVABLE_BUT_NOT_COPYABLE(bs_set) + public: + typedef typename Base::value_compare value_compare; + typedef typename Base::value_traits value_traits; + typedef typename Base::iterator iterator; + typedef typename Base::const_iterator const_iterator; + + //Assert if passed value traits are compatible with the type + BOOST_STATIC_ASSERT((detail::is_same::value)); + + explicit bs_set( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : Base(cmp, v_traits) + {} + + template + bs_set( Iterator b, Iterator e + , const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : Base(b, e, cmp, v_traits) + {} + + bs_set(BOOST_RV_REF(bs_set) x) + : Base(::boost::move(static_cast(x))) + {} + + bs_set& operator=(BOOST_RV_REF(bs_set) x) + { return static_cast(this->Base::operator=(::boost::move(static_cast(x)))); } + + static bs_set &container_from_end_iterator(iterator end_iterator) + { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static const bs_set &container_from_end_iterator(const_iterator end_iterator) + { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static bs_set &container_from_iterator(iterator it) + { return static_cast(Base::container_from_iterator(it)); } + + static const bs_set &container_from_iterator(const_iterator it) + { return static_cast(Base::container_from_iterator(it)); } +}; + +#endif + +//! The class template bs_multiset is an intrusive container, that mimics most of +//! the interface of std::multiset as described in the C++ standard. +//! +//! The template parameter \c T is the type to be managed by the container. +//! The user can specify additional options and if no options are provided +//! default options are used. +//! +//! The container supports the following options: +//! \c base_hook<>/member_hook<>/value_traits<>, +//! \c constant_time_size<>, \c size_type<> and +//! \c compare<>. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +template +#else +template +#endif +class bs_multiset_impl +#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + : public bstree_impl +#endif +{ + /// @cond + typedef bstree_impl tree_type; + + BOOST_MOVABLE_BUT_NOT_COPYABLE(bs_multiset_impl) + typedef tree_type implementation_defined; + /// @endcond + + public: + typedef typename implementation_defined::value_type value_type; + typedef typename implementation_defined::value_traits value_traits; + typedef typename implementation_defined::pointer pointer; + typedef typename implementation_defined::const_pointer const_pointer; + typedef typename implementation_defined::reference reference; + typedef typename implementation_defined::const_reference const_reference; + typedef typename implementation_defined::difference_type difference_type; + typedef typename implementation_defined::size_type size_type; + typedef typename implementation_defined::value_compare value_compare; + typedef typename implementation_defined::key_compare key_compare; + typedef typename implementation_defined::iterator iterator; + typedef typename implementation_defined::const_iterator const_iterator; + typedef typename implementation_defined::reverse_iterator reverse_iterator; + typedef typename implementation_defined::const_reverse_iterator const_reverse_iterator; + typedef typename implementation_defined::insert_commit_data insert_commit_data; + typedef typename implementation_defined::node_traits node_traits; + typedef typename implementation_defined::node node; + typedef typename implementation_defined::node_ptr node_ptr; + typedef typename implementation_defined::const_node_ptr const_node_ptr; + typedef typename implementation_defined::node_algorithms node_algorithms; + + static const bool constant_time_size = tree_type::constant_time_size; + + public: + //! @copydoc ::boost::intrusive::bstree::bstree(const value_compare &,const value_traits &) + explicit bs_multiset_impl( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : tree_type(cmp, v_traits) + {} + + //! @copydoc ::boost::intrusive::bstree::bstree(bool,Iterator,Iterator,const value_compare &,const value_traits &) + template + bs_multiset_impl( Iterator b, Iterator e + , const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : tree_type(false, b, e, cmp, v_traits) + {} + + //! @copydoc ::boost::intrusive::bstree::bstree(bstree &&) + bs_multiset_impl(BOOST_RV_REF(bs_multiset_impl) x) + : tree_type(::boost::move(static_cast(x))) + {} + + //! @copydoc ::boost::intrusive::bstree::operator=(bstree &&) + bs_multiset_impl& operator=(BOOST_RV_REF(bs_multiset_impl) x) + { return static_cast(tree_type::operator=(::boost::move(static_cast(x)))); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + //! @copydoc ::boost::intrusive::bstree::~bstree() + ~bs_multiset_impl(); + + //! @copydoc ::boost::intrusive::bstree::begin() + iterator begin(); + + //! @copydoc ::boost::intrusive::bstree::begin()const + const_iterator begin() const; + + //! @copydoc ::boost::intrusive::bstree::cbegin()const + const_iterator cbegin() const; + + //! @copydoc ::boost::intrusive::bstree::end() + iterator end(); + + //! @copydoc ::boost::intrusive::bstree::end()const + const_iterator end() const; + + //! @copydoc ::boost::intrusive::bstree::cend()const + const_iterator cend() const; + + //! @copydoc ::boost::intrusive::bstree::rbegin() + reverse_iterator rbegin(); + + //! @copydoc ::boost::intrusive::bstree::rbegin()const + const_reverse_iterator rbegin() const; + + //! @copydoc ::boost::intrusive::bstree::crbegin()const + const_reverse_iterator crbegin() const; + + //! @copydoc ::boost::intrusive::bstree::rend() + reverse_iterator rend(); + + //! @copydoc ::boost::intrusive::bstree::rend()const + const_reverse_iterator rend() const; + + //! @copydoc ::boost::intrusive::bstree::crend()const + const_reverse_iterator crend() const; + + //! @copydoc ::boost::intrusive::bstree::container_from_end_iterator(iterator) + static bs_multiset_impl &container_from_end_iterator(iterator end_iterator); + + //! @copydoc ::boost::intrusive::bstree::container_from_end_iterator(const_iterator) + static const bs_multiset_impl &container_from_end_iterator(const_iterator end_iterator); + + //! @copydoc ::boost::intrusive::bstree::container_from_iterator(iterator) + static bs_multiset_impl &container_from_iterator(iterator it); + + //! @copydoc ::boost::intrusive::bstree::container_from_iterator(const_iterator) + static const bs_multiset_impl &container_from_iterator(const_iterator it); + + //! @copydoc ::boost::intrusive::bstree::key_comp()const + key_compare key_comp() const; + + //! @copydoc ::boost::intrusive::bstree::value_comp()const + value_compare value_comp() const; + + //! @copydoc ::boost::intrusive::bstree::empty()const + bool empty() const; + + //! @copydoc ::boost::intrusive::bstree::size()const + size_type size() const; + + //! @copydoc ::boost::intrusive::bstree::swap + void swap(bs_multiset_impl& other); + + //! @copydoc ::boost::intrusive::bstree::clone_from + template + void clone_from(const bs_multiset_impl &src, Cloner cloner, Disposer disposer); + + #endif //#ifdef BOOST_iNTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::bstree::insert_equal(reference) + iterator insert(reference value) + { return tree_type::insert_equal(value); } + + //! @copydoc ::boost::intrusive::bstree::insert_equal(const_iterator,reference) + iterator insert(const_iterator hint, reference value) + { return tree_type::insert_equal(hint, value); } + + //! @copydoc ::boost::intrusive::bstree::insert_equal(Iterator,Iterator) + template + void insert(Iterator b, Iterator e) + { tree_type::insert_equal(b, e); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + //! @copydoc ::boost::intrusive::bstree::insert_before + iterator insert_before(const_iterator pos, reference value); + + //! @copydoc ::boost::intrusive::bstree::push_back + void push_back(reference value); + + //! @copydoc ::boost::intrusive::bstree::push_front + void push_front(reference value); + + //! @copydoc ::boost::intrusive::bstree::erase(const_iterator) + iterator erase(const_iterator i); + + //! @copydoc ::boost::intrusive::bstree::erase(const_iterator,const_iterator) + iterator erase(const_iterator b, const_iterator e); + + //! @copydoc ::boost::intrusive::bstree::erase(const_reference) + size_type erase(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::erase(const KeyType&,KeyValueCompare) + template + size_type erase(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const_iterator,Disposer) + template + iterator erase_and_dispose(const_iterator i, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const_iterator,const_iterator,Disposer) + template + iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const_reference, Disposer) + template + size_type erase_and_dispose(const_reference value, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const KeyType&,KeyValueCompare,Disposer) + template + size_type erase_and_dispose(const KeyType& key, KeyValueCompare comp, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::clear + void clear(); + + //! @copydoc ::boost::intrusive::bstree::clear_and_dispose + template + void clear_and_dispose(Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::count(const_reference)const + size_type count(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::count(const KeyType&,KeyValueCompare)const + template + size_type count(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::bstree::lower_bound(const_reference) + iterator lower_bound(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::lower_bound(const KeyType&,KeyValueCompare) + template + iterator lower_bound(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::lower_bound(const_reference)const + const_iterator lower_bound(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::lower_bound(const KeyType&,KeyValueCompare)const + template + const_iterator lower_bound(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::bstree::upper_bound(const_reference) + iterator upper_bound(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::upper_bound(const KeyType&,KeyValueCompare) + template + iterator upper_bound(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::upper_bound(const_reference)const + const_iterator upper_bound(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::upper_bound(const KeyType&,KeyValueCompare)const + template + const_iterator upper_bound(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::bstree::find(const_reference) + iterator find(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::find(const KeyType&,KeyValueCompare) + template + iterator find(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::find(const_reference)const + const_iterator find(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::find(const KeyType&,KeyValueCompare)const + template + const_iterator find(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::bstree::equal_range(const_reference) + std::pair equal_range(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::equal_range(const KeyType&,KeyValueCompare) + template + std::pair equal_range(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::equal_range(const_reference)const + std::pair + equal_range(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::equal_range(const KeyType&,KeyValueCompare)const + template + std::pair + equal_range(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::bstree::bounded_range(const_reference,const_reference,bool,bool) + std::pair bounded_range + (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed); + + //! @copydoc ::boost::intrusive::bstree::bounded_range(const KeyType&,const KeyType&,KeyValueCompare,bool,bool) + template + std::pair bounded_range + (const KeyType& lower_key, const KeyType& upper_key, KeyValueCompare comp, bool left_closed, bool right_closed); + + //! @copydoc ::boost::intrusive::bstree::bounded_range(const_reference,const_reference,bool,bool)const + std::pair + bounded_range(const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed) const; + + //! @copydoc ::boost::intrusive::bstree::bounded_range(const KeyType&,const KeyType&,KeyValueCompare,bool,bool)const + template + std::pair bounded_range + (const KeyType& lower_key, const KeyType& upper_key, KeyValueCompare comp, bool left_closed, bool right_closed) const; + + //! @copydoc ::boost::intrusive::bstree::s_iterator_to(reference) + static iterator s_iterator_to(reference value); + + //! @copydoc ::boost::intrusive::bstree::s_iterator_to(const_reference) + static const_iterator s_iterator_to(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::iterator_to(reference) + iterator iterator_to(reference value); + + //! @copydoc ::boost::intrusive::bstree::iterator_to(const_reference)const + const_iterator iterator_to(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::init_node(reference) + static void init_node(reference value); + + //! @copydoc ::boost::intrusive::bstree::unlink_leftmost_without_rebalance + pointer unlink_leftmost_without_rebalance(); + + //! @copydoc ::boost::intrusive::bstree::replace_node + void replace_node(iterator replace_this, reference with_this); + + //! @copydoc ::boost::intrusive::bstree::remove_node + void remove_node(reference value); + #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +}; + +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + +template +bool operator!= (const bs_multiset_impl &x, const bs_multiset_impl &y); + +template +bool operator>(const bs_multiset_impl &x, const bs_multiset_impl &y); + +template +bool operator<=(const bs_multiset_impl &x, const bs_multiset_impl &y); + +template +bool operator>=(const bs_multiset_impl &x, const bs_multiset_impl &y); + +template +void swap(bs_multiset_impl &x, bs_multiset_impl &y); + +#endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + +//! Helper metafunction to define a \c bs_multiset that yields to the same type when the +//! same options (either explicitly or implicitly) are used. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +struct make_bs_multiset +{ + /// @cond + typedef typename pack_options + < bstree_defaults, + #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) + O1, O2, O3, O4, O5 + #else + Options... + #endif + >::type packed_options; + + typedef typename detail::get_value_traits + ::type value_traits; + typedef typename detail::get_header_holder_type + < value_traits, typename packed_options::header_holder_type >::type header_holder_type; + + typedef bs_multiset_impl + < value_traits + , typename packed_options::compare + , typename packed_options::size_type + , packed_options::constant_time_size + , header_holder_type + > implementation_defined; + /// @endcond + typedef implementation_defined type; +}; + +#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + +#if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +class bs_multiset + : public make_bs_multiset::type +{ + typedef typename make_bs_multiset::type Base; + + BOOST_MOVABLE_BUT_NOT_COPYABLE(bs_multiset) + + public: + typedef typename Base::value_compare value_compare; + typedef typename Base::value_traits value_traits; + typedef typename Base::iterator iterator; + typedef typename Base::const_iterator const_iterator; + + //Assert if passed value traits are compatible with the type + BOOST_STATIC_ASSERT((detail::is_same::value)); + + explicit bs_multiset( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : Base(cmp, v_traits) + {} + + template + bs_multiset( Iterator b, Iterator e + , const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : Base(b, e, cmp, v_traits) + {} + + bs_multiset(BOOST_RV_REF(bs_multiset) x) + : Base(::boost::move(static_cast(x))) + {} + + bs_multiset& operator=(BOOST_RV_REF(bs_multiset) x) + { return static_cast(this->Base::operator=(::boost::move(static_cast(x)))); } + + static bs_multiset &container_from_end_iterator(iterator end_iterator) + { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static const bs_multiset &container_from_end_iterator(const_iterator end_iterator) + { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static bs_multiset &container_from_iterator(iterator it) + { return static_cast(Base::container_from_iterator(it)); } + + static const bs_multiset &container_from_iterator(const_iterator it) + { return static_cast(Base::container_from_iterator(it)); } +}; + +#endif + +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_BS_SET_HPP diff --git a/boost/intrusive/bs_set_hook.hpp b/boost/intrusive/bs_set_hook.hpp index 682426a960..cb5af30d63 100644 --- a/boost/intrusive/bs_set_hook.hpp +++ b/boost/intrusive/bs_set_hook.hpp @@ -1,6 +1,6 @@ ///////////////////////////////////////////////////////////////////////////// // -// (C) Copyright Ion Gaztanaga 2007-2012 +// (C) Copyright Ion Gaztanaga 2007-2013 // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at @@ -13,31 +13,27 @@ #ifndef BOOST_INTRUSIVE_BS_SET_HOOK_HPP #define BOOST_INTRUSIVE_BS_SET_HOOK_HPP +#if defined(_MSC_VER) +# pragma once +#endif + #include #include -#include + #include -#include +#include #include #include namespace boost { namespace intrusive { -/// @cond -template -struct get_bs_set_node_algo -{ - typedef detail::tree_algorithms > type; -}; -/// @endcond - //! Helper metafunction to define a \c bs_set_base_hook that yields to the same //! type when the same options (either explicitly or implicitly) are used. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) template #else -template +template #endif struct make_bs_set_base_hook { @@ -50,14 +46,11 @@ struct make_bs_set_base_hook #endif ::type packed_options; - //Scapegoat trees can't be auto unlink trees - BOOST_STATIC_ASSERT(((int)packed_options::link_mode != (int)auto_unlink)); - - typedef detail::generic_hook - < get_bs_set_node_algo + typedef generic_hook + < bstree_algorithms > , typename packed_options::tag , packed_options::link_mode - , detail::BsSetBaseHook + , BsTreeBaseHookId > implementation_defined; /// @endcond typedef implementation_defined type; @@ -76,7 +69,7 @@ struct make_bs_set_base_hook //! unique tag. //! //! \c void_pointer<> is the pointer type that will be used internally in the hook -//! and the the container configured to use this hook. +//! and the container configured to use this hook. //! //! \c link_mode<> will specify the linking mode of the hook (\c normal_link, //! \c auto_unlink or \c safe_link). @@ -168,7 +161,7 @@ class bs_set_base_hook #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) template #else -template +template #endif struct make_bs_set_member_hook { @@ -182,14 +175,11 @@ struct make_bs_set_member_hook ::type packed_options; - //Scapegoat trees can't be auto unlink trees - BOOST_STATIC_ASSERT(((int)packed_options::link_mode != (int)auto_unlink)); - - typedef detail::generic_hook - < get_bs_set_node_algo + typedef generic_hook + < bstree_algorithms > , member_tag , packed_options::link_mode - , detail::NoBaseHook + , NoBaseHookId > implementation_defined; /// @endcond typedef implementation_defined type; @@ -202,7 +192,7 @@ struct make_bs_set_member_hook //! The hook admits the following options: \c void_pointer<>, \c link_mode<>. //! //! \c void_pointer<> is the pointer type that will be used internally in the hook -//! and the the container configured to use this hook. +//! and the container configured to use this hook. //! //! \c link_mode<> will specify the linking mode of the hook (\c normal_link, //! \c auto_unlink or \c safe_link). diff --git a/boost/intrusive/bstree.hpp b/boost/intrusive/bstree.hpp new file mode 100644 index 0000000000..44b02bb790 --- /dev/null +++ b/boost/intrusive/bstree.hpp @@ -0,0 +1,2177 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2013-2014 +// +// 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_BSTREE_HPP +#define BOOST_INTRUSIVE_BSTREE_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include //pair,lexicographical_compare +#include //swap +#include //size_t... +#include //less, equal_to + + +namespace boost { +namespace intrusive { + +/// @cond + +struct default_bstree_hook_applier +{ template struct apply{ typedef typename T::default_bstree_hook type; }; }; + +template<> +struct is_default_hook_tag +{ static const bool value = true; }; + +struct bstree_defaults +{ + typedef default_bstree_hook_applier proto_value_traits; + static const bool constant_time_size = true; + typedef std::size_t size_type; + typedef void compare; + static const bool floating_point = true; //For sgtree + typedef void priority; //For treap + typedef void header_holder_type; +}; + +template +struct bstbase3 +{ + typedef ValueTraits value_traits; + typedef typename value_traits::node_traits node_traits; + typedef typename node_traits::node node_type; + typedef typename get_algo::type node_algorithms; + typedef typename node_traits::node_ptr node_ptr; + typedef typename node_traits::const_node_ptr const_node_ptr; + typedef tree_iterator iterator; + typedef tree_iterator const_iterator; + typedef boost::intrusive::detail::reverse_iterator reverse_iterator; + typedef boost::intrusive::detail::reverse_iterator const_reverse_iterator; + typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::pointer) pointer; + typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::const_pointer) const_pointer; + typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::element_type) value_type; + typedef BOOST_INTRUSIVE_IMPDEF(value_type) key_type; + typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::reference) reference; + typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::reference) const_reference; + typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::difference_type) difference_type; + typedef HeaderHolder header_holder_type; + + static const bool safemode_or_autounlink = is_safe_autounlink::value; + static const bool stateful_value_traits = detail::is_stateful_value_traits::value; + static const bool has_container_from_iterator = + detail::is_same< header_holder_type, detail::default_header_holder< node_traits > >::value; + + struct holder_t : public ValueTraits + { + explicit holder_t(const ValueTraits &vtraits) + : ValueTraits(vtraits) + {} + header_holder_type root; + } holder; + + static bstbase3 &get_tree_base_from_end_iterator(const const_iterator &end_iterator) + { + BOOST_STATIC_ASSERT(has_container_from_iterator); + node_ptr p = end_iterator.pointed_node(); + header_holder_type* h = header_holder_type::get_holder(p); + holder_t *holder = get_parent_from_member(h, &holder_t::root); + bstbase3 *base = get_parent_from_member (holder, &bstbase3::holder); + return *base; + } + + bstbase3(const ValueTraits &vtraits) + : holder(vtraits) + { + node_algorithms::init_header(this->header_ptr()); + } + + node_ptr header_ptr() + { return holder.root.get_node(); } + + const_node_ptr header_ptr() const + { return holder.root.get_node(); } + + const value_traits &get_value_traits() const + { return this->holder; } + + value_traits &get_value_traits() + { return this->holder; } + + typedef typename boost::intrusive::value_traits_pointers + ::const_value_traits_ptr const_value_traits_ptr; + + const_value_traits_ptr priv_value_traits_ptr() const + { return pointer_traits::pointer_to(this->get_value_traits()); } + + iterator begin() + { return iterator(node_algorithms::begin_node(this->header_ptr()), this->priv_value_traits_ptr()); } + + const_iterator begin() const + { return cbegin(); } + + const_iterator cbegin() const + { return const_iterator(node_algorithms::begin_node(this->header_ptr()), this->priv_value_traits_ptr()); } + + iterator end() + { return iterator(node_algorithms::end_node(this->header_ptr()), this->priv_value_traits_ptr()); } + + const_iterator end() const + { return cend(); } + + const_iterator cend() const + { return const_iterator(node_algorithms::end_node(this->header_ptr()), this->priv_value_traits_ptr()); } + + iterator root() + { return iterator(node_algorithms::root_node(this->header_ptr()), this->priv_value_traits_ptr()); } + + const_iterator root() const + { return croot(); } + + const_iterator croot() const + { return const_iterator(node_algorithms::root_node(this->header_ptr()), this->priv_value_traits_ptr()); } + + reverse_iterator rbegin() + { return reverse_iterator(end()); } + + const_reverse_iterator rbegin() const + { return const_reverse_iterator(end()); } + + const_reverse_iterator crbegin() const + { return const_reverse_iterator(end()); } + + reverse_iterator rend() + { return reverse_iterator(begin()); } + + const_reverse_iterator rend() const + { return const_reverse_iterator(begin()); } + + const_reverse_iterator crend() const + { return const_reverse_iterator(begin()); } + + void replace_node(iterator replace_this, reference with_this) + { + node_algorithms::replace_node( get_value_traits().to_node_ptr(*replace_this) + , this->header_ptr() + , get_value_traits().to_node_ptr(with_this)); + if(safemode_or_autounlink) + node_algorithms::init(replace_this.pointed_node()); + } + + void rebalance() + { node_algorithms::rebalance(this->header_ptr()); } + + iterator rebalance_subtree(iterator root) + { return iterator(node_algorithms::rebalance_subtree(root.pointed_node()), this->priv_value_traits_ptr()); } + + static iterator s_iterator_to(reference value) + { + BOOST_STATIC_ASSERT((!stateful_value_traits)); + return iterator (value_traits::to_node_ptr(value), const_value_traits_ptr()); + } + + static const_iterator s_iterator_to(const_reference value) + { + BOOST_STATIC_ASSERT((!stateful_value_traits)); + return const_iterator (value_traits::to_node_ptr(*pointer_traits::const_cast_from(pointer_traits::pointer_to(value))), const_value_traits_ptr()); + } + + iterator iterator_to(reference value) + { return iterator (this->get_value_traits().to_node_ptr(value), this->priv_value_traits_ptr()); } + + const_iterator iterator_to(const_reference value) const + { return const_iterator (this->get_value_traits().to_node_ptr(*pointer_traits::const_cast_from(pointer_traits::pointer_to(value))), this->priv_value_traits_ptr()); } + + static void init_node(reference value) + { node_algorithms::init(value_traits::to_node_ptr(value)); } + +}; + +template +struct get_less +{ + typedef Less type; +}; + +template +struct get_less +{ + typedef ::std::less type; +}; + +template +struct bstbase2 + //Put the (possibly empty) functor in the first position to get EBO in MSVC + : public detail::ebo_functor_holder::type> + , public bstbase3 +{ + typedef bstbase3 treeheader_t; + typedef typename treeheader_t::value_traits value_traits; + typedef typename treeheader_t::node_algorithms node_algorithms; + typedef typename get_less + < VoidOrKeyComp, typename value_traits::value_type>::type value_compare; + typedef BOOST_INTRUSIVE_IMPDEF(value_compare) key_compare; + typedef typename treeheader_t::iterator iterator; + typedef typename treeheader_t::const_iterator const_iterator; + typedef typename treeheader_t::node_ptr node_ptr; + typedef typename treeheader_t::const_node_ptr const_node_ptr; + + bstbase2(const value_compare &comp, const ValueTraits &vtraits) + : detail::ebo_functor_holder(comp), treeheader_t(vtraits) + {} + + const value_compare &comp() const + { return this->get(); } + + value_compare &comp() + { return this->get(); } + + typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::pointer) pointer; + typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::const_pointer) const_pointer; + typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::element_type) value_type; + typedef BOOST_INTRUSIVE_IMPDEF(value_type) key_type; + typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::reference) reference; + typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::reference) const_reference; + typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::difference_type) difference_type; + typedef typename node_algorithms::insert_commit_data insert_commit_data; + + value_compare value_comp() const + { return this->comp(); } + + key_compare key_comp() const + { return this->comp(); } + + //lower_bound + iterator lower_bound(const_reference value) + { return this->lower_bound(value, this->comp()); } + + const_iterator lower_bound(const_reference value) const + { return this->lower_bound(value, this->comp()); } + + template + iterator lower_bound(const KeyType &key, KeyValueCompare comp) + { + detail::key_nodeptr_comp + key_node_comp(comp, &this->get_value_traits()); + return iterator(node_algorithms::lower_bound + (this->header_ptr(), key, key_node_comp), this->priv_value_traits_ptr()); + } + + template + const_iterator lower_bound(const KeyType &key, KeyValueCompare comp) const + { + detail::key_nodeptr_comp + key_node_comp(comp, &this->get_value_traits()); + return const_iterator(node_algorithms::lower_bound + (this->header_ptr(), key, key_node_comp), this->priv_value_traits_ptr()); + } + + //upper_bound + iterator upper_bound(const_reference value) + { return this->upper_bound(value, this->comp()); } + + template + iterator upper_bound(const KeyType &key, KeyValueCompare comp) + { + detail::key_nodeptr_comp + key_node_comp(comp, &this->get_value_traits()); + return iterator(node_algorithms::upper_bound + (this->header_ptr(), key, key_node_comp), this->priv_value_traits_ptr()); + } + + const_iterator upper_bound(const_reference value) const + { return this->upper_bound(value, this->comp()); } + + template + const_iterator upper_bound(const KeyType &key, KeyValueCompare comp) const + { + detail::key_nodeptr_comp + key_node_comp(comp, &this->get_value_traits()); + return const_iterator(node_algorithms::upper_bound + (this->header_ptr(), key, key_node_comp), this->priv_value_traits_ptr()); + } + + //find + iterator find(const_reference value) + { return this->find(value, this->comp()); } + + template + iterator find(const KeyType &key, KeyValueCompare comp) + { + detail::key_nodeptr_comp + key_node_comp(comp, &this->get_value_traits()); + return iterator + (node_algorithms::find(this->header_ptr(), key, key_node_comp), this->priv_value_traits_ptr()); + } + + const_iterator find(const_reference value) const + { return this->find(value, this->comp()); } + + template + const_iterator find(const KeyType &key, KeyValueCompare comp) const + { + detail::key_nodeptr_comp + key_node_comp(comp, &this->get_value_traits()); + return const_iterator + (node_algorithms::find(this->header_ptr(), key, key_node_comp), this->priv_value_traits_ptr()); + } + + //equal_range + std::pair equal_range(const_reference value) + { return this->equal_range(value, this->comp()); } + + template + std::pair equal_range(const KeyType &key, KeyValueCompare comp) + { + detail::key_nodeptr_comp + key_node_comp(comp, &this->get_value_traits()); + std::pair ret + (node_algorithms::equal_range(this->header_ptr(), key, key_node_comp)); + return std::pair( iterator(ret.first, this->priv_value_traits_ptr()) + , iterator(ret.second, this->priv_value_traits_ptr())); + } + + std::pair + equal_range(const_reference value) const + { return this->equal_range(value, this->comp()); } + + template + std::pair + equal_range(const KeyType &key, KeyValueCompare comp) const + { + detail::key_nodeptr_comp + key_node_comp(comp, &this->get_value_traits()); + std::pair ret + (node_algorithms::equal_range(this->header_ptr(), key, key_node_comp)); + return std::pair( const_iterator(ret.first, this->priv_value_traits_ptr()) + , const_iterator(ret.second, this->priv_value_traits_ptr())); + } + + //lower_bound_range + std::pair lower_bound_range(const_reference value) + { return this->lower_bound_range(value, this->comp()); } + + template + std::pair lower_bound_range(const KeyType &key, KeyValueCompare comp) + { + detail::key_nodeptr_comp + key_node_comp(comp, &this->get_value_traits()); + std::pair ret + (node_algorithms::lower_bound_range(this->header_ptr(), key, key_node_comp)); + return std::pair( iterator(ret.first, this->priv_value_traits_ptr()) + , iterator(ret.second, this->priv_value_traits_ptr())); + } + + std::pair + lower_bound_range(const_reference value) const + { return this->lower_bound_range(value, this->comp()); } + + template + std::pair + lower_bound_range(const KeyType &key, KeyValueCompare comp) const + { + detail::key_nodeptr_comp + key_node_comp(comp, &this->get_value_traits()); + std::pair ret + (node_algorithms::lower_bound_range(this->header_ptr(), key, key_node_comp)); + return std::pair( const_iterator(ret.first, this->priv_value_traits_ptr()) + , const_iterator(ret.second, this->priv_value_traits_ptr())); + } + + //bounded_range + std::pair bounded_range + (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed) + { return this->bounded_range(lower_value, upper_value, this->comp(), left_closed, right_closed); } + + template + std::pair bounded_range + (const KeyType &lower_key, const KeyType &upper_key, KeyValueCompare comp, bool left_closed, bool right_closed) + { + detail::key_nodeptr_comp + key_node_comp(comp, &this->get_value_traits()); + std::pair ret + (node_algorithms::bounded_range + (this->header_ptr(), lower_key, upper_key, key_node_comp, left_closed, right_closed)); + return std::pair( iterator(ret.first, this->priv_value_traits_ptr()) + , iterator(ret.second, this->priv_value_traits_ptr())); + } + + std::pair bounded_range + (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed) const + { return this->bounded_range(lower_value, upper_value, this->comp(), left_closed, right_closed); } + + template + std::pair bounded_range + (const KeyType &lower_key, const KeyType &upper_key, KeyValueCompare comp, bool left_closed, bool right_closed) const + { + detail::key_nodeptr_comp + key_node_comp(comp, &this->get_value_traits()); + std::pair ret + (node_algorithms::bounded_range + (this->header_ptr(), lower_key, upper_key, key_node_comp, left_closed, right_closed)); + return std::pair( const_iterator(ret.first, this->priv_value_traits_ptr()) + , const_iterator(ret.second, this->priv_value_traits_ptr())); + } + + //insert_unique_check + template + std::pair insert_unique_check + (const KeyType &key, KeyValueCompare key_value_comp, insert_commit_data &commit_data) + { + detail::key_nodeptr_comp + ocomp(key_value_comp, &this->get_value_traits()); + std::pair ret = + (node_algorithms::insert_unique_check + (this->header_ptr(), key, ocomp, commit_data)); + return std::pair(iterator(ret.first, this->priv_value_traits_ptr()), ret.second); + } + + template + std::pair insert_unique_check + (const_iterator hint, const KeyType &key + ,KeyValueCompare key_value_comp, insert_commit_data &commit_data) + { + detail::key_nodeptr_comp + ocomp(key_value_comp, &this->get_value_traits()); + std::pair ret = + (node_algorithms::insert_unique_check + (this->header_ptr(), hint.pointed_node(), key, ocomp, commit_data)); + return std::pair(iterator(ret.first, this->priv_value_traits_ptr()), ret.second); + } +}; + +//Due to MSVC's EBO implementation, to save space and maintain the ABI, we must put the non-empty size member +//in the first position, but if size is not going to be stored then we'll use an specialization +//that doesn't inherit from size_holder +template +struct bstbase_hack + : public detail::size_holder + , public bstbase2 < ValueTraits, VoidOrKeyComp, AlgoType, HeaderHolder> +{ + typedef bstbase2< ValueTraits, VoidOrKeyComp, AlgoType, HeaderHolder> base_type; + typedef typename base_type::value_compare value_compare; + typedef SizeType size_type; + typedef typename base_type::node_traits node_traits; + typedef typename get_algo + ::type algo_type; + + bstbase_hack(const value_compare & comp, const ValueTraits &vtraits) + : base_type(comp, vtraits) + { + this->sz_traits().set_size(size_type(0)); + } + + typedef detail::size_holder size_traits; + + size_traits &sz_traits() + { return static_cast(*this); } + + const size_traits &sz_traits() const + { return static_cast(*this); } +}; + +//Specialization for ConstantTimeSize == false +template +struct bstbase_hack + : public bstbase2 < ValueTraits, VoidOrKeyComp, AlgoType, HeaderHolder> +{ + typedef bstbase2< ValueTraits, VoidOrKeyComp, AlgoType, HeaderHolder> base_type; + typedef typename base_type::value_compare value_compare; + bstbase_hack(const value_compare & comp, const ValueTraits &vtraits) + : base_type(comp, vtraits) + {} + + typedef detail::size_holder size_traits; + + size_traits &sz_traits() + { return s_size_traits; } + + const size_traits &sz_traits() const + { return s_size_traits; } + + static size_traits s_size_traits; +}; + +template +detail::size_holder bstbase_hack::s_size_traits; + +//This class will +template +struct bstbase + : public bstbase_hack< ValueTraits, VoidOrKeyComp, ConstantTimeSize, SizeType, AlgoType, HeaderHolder> +{ + typedef bstbase_hack< ValueTraits, VoidOrKeyComp, ConstantTimeSize, SizeType, AlgoType, HeaderHolder> base_type; + typedef ValueTraits value_traits; + typedef typename base_type::value_compare value_compare; + typedef value_compare key_compare; + typedef typename base_type::const_reference const_reference; + typedef typename base_type::reference reference; + typedef typename base_type::iterator iterator; + typedef typename base_type::const_iterator const_iterator; + typedef typename base_type::node_traits node_traits; + typedef typename get_algo + ::type node_algorithms; + typedef SizeType size_type; + + bstbase(const value_compare & comp, const ValueTraits &vtraits) + : base_type(comp, vtraits) + {} + + //Detach all inserted nodes. This will add exception safety to bstree_impl + //constructors inserting elements. + ~bstbase() + { + if(is_safe_autounlink::value){ + node_algorithms::clear_and_dispose + ( this->header_ptr() + , detail::node_disposer + (detail::null_disposer(), &this->get_value_traits())); + node_algorithms::init(this->header_ptr()); + } + } +}; + + +/// @endcond + +//! The class template bstree is an unbalanced intrusive binary search tree +//! container. The no-throw guarantee holds only, if the value_compare object +//! doesn't throw. +//! +//! The complexity guarantees only hold if the tree is balanced, logarithmic +//! complexity would increase to linear if the tree is totally unbalanced. +//! +//! The template parameter \c T is the type to be managed by the container. +//! The user can specify additional options and if no options are provided +//! default options are used. +//! +//! The container supports the following options: +//! \c base_hook<>/member_hook<>/value_traits<>, +//! \c constant_time_size<>, \c size_type<> and +//! \c compare<>. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +template +#else +template +#endif +class bstree_impl + : public bstbase +{ + public: + /// @cond + typedef bstbase data_type; + typedef tree_iterator iterator_type; + typedef tree_iterator const_iterator_type; + /// @endcond + + typedef BOOST_INTRUSIVE_IMPDEF(ValueTraits) value_traits; + typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::pointer) pointer; + typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::const_pointer) const_pointer; + typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::element_type) value_type; + typedef BOOST_INTRUSIVE_IMPDEF(value_type) key_type; + typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::reference) reference; + typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::reference) const_reference; + typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::difference_type) difference_type; + typedef BOOST_INTRUSIVE_IMPDEF(SizeType) size_type; + typedef BOOST_INTRUSIVE_IMPDEF(typename data_type::value_compare) value_compare; + typedef BOOST_INTRUSIVE_IMPDEF(value_compare) key_compare; + typedef BOOST_INTRUSIVE_IMPDEF(iterator_type) iterator; + typedef BOOST_INTRUSIVE_IMPDEF(const_iterator_type) const_iterator; + typedef BOOST_INTRUSIVE_IMPDEF(boost::intrusive::detail::reverse_iterator) reverse_iterator; + typedef BOOST_INTRUSIVE_IMPDEF(boost::intrusive::detail::reverse_iterator) const_reverse_iterator; + typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::node_traits) node_traits; + typedef BOOST_INTRUSIVE_IMPDEF(typename node_traits::node) node; + typedef BOOST_INTRUSIVE_IMPDEF(typename node_traits::node_ptr) node_ptr; + typedef BOOST_INTRUSIVE_IMPDEF(typename node_traits::const_node_ptr) const_node_ptr; + /// @cond + typedef typename get_algo::type algo_type; + /// @endcond + typedef BOOST_INTRUSIVE_IMPDEF(algo_type) node_algorithms; + + static const bool constant_time_size = ConstantTimeSize; + static const bool stateful_value_traits = detail::is_stateful_value_traits::value; + /// @cond + private: + + //noncopyable + BOOST_MOVABLE_BUT_NOT_COPYABLE(bstree_impl) + + static const bool safemode_or_autounlink = is_safe_autounlink::value; + + //Constant-time size is incompatible with auto-unlink hooks! + BOOST_STATIC_ASSERT(!(constant_time_size && ((int)value_traits::link_mode == (int)auto_unlink))); + + + protected: + + + /// @endcond + + public: + + typedef typename node_algorithms::insert_commit_data insert_commit_data; + + //! Effects: Constructs an empty container. + //! + //! Complexity: Constant. + //! + //! Throws: If value_traits::node_traits::node + //! constructor throws (this does not happen with predefined Boost.Intrusive hooks) + //! or the copy constructor of the value_compare object throws. Basic guarantee. + explicit bstree_impl( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : data_type(cmp, v_traits) + {} + + //! Requires: Dereferencing iterator must yield an lvalue of type value_type. + //! cmp must be a comparison function that induces a strict weak ordering. + //! + //! Effects: Constructs an empty container and inserts elements from + //! [b, e). + //! + //! Complexity: Linear in N if [b, e) is already sorted using + //! comp and otherwise N * log N, where N is the distance between first and last. + //! + //! Throws: If value_traits::node_traits::node + //! constructor throws (this does not happen with predefined Boost.Intrusive hooks) + //! or the copy constructor/operator() of the value_compare object throws. Basic guarantee. + template + bstree_impl( bool unique, Iterator b, Iterator e + , const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : data_type(cmp, v_traits) + { + //bstbase releases elements in case of exceptions + if(unique) + this->insert_unique(b, e); + else + this->insert_equal(b, e); + } + + //! Effects: to-do + //! + bstree_impl(BOOST_RV_REF(bstree_impl) x) + : data_type(::boost::move(x.comp()), ::boost::move(x.get_value_traits())) + { + this->swap(x); + } + + //! Effects: to-do + //! + bstree_impl& operator=(BOOST_RV_REF(bstree_impl) x) + { this->swap(x); return *this; } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + //! Effects: Detaches all elements from this. The objects in the set + //! are not deleted (i.e. no destructors are called), but the nodes according to + //! the value_traits template parameter are reinitialized and thus can be reused. + //! + //! Complexity: Linear to elements contained in *this. + //! + //! Throws: Nothing. + ~bstree_impl() + {} + + //! Effects: Returns an iterator pointing to the beginning of the container. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + iterator begin(); + + //! Effects: Returns a const_iterator pointing to the beginning of the container. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_iterator begin() const; + + //! Effects: Returns a const_iterator pointing to the beginning of the container. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_iterator cbegin() const; + + //! Effects: Returns an iterator pointing to the end of the container. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + iterator end(); + + //! Effects: Returns a const_iterator pointing to the end of the container. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_iterator end() const; + + //! Effects: Returns a const_iterator pointing to the end of the container. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_iterator cend() const; + + //! Effects: Returns a reverse_iterator pointing to the beginning of the + //! reversed container. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + reverse_iterator rbegin(); + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed container. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_reverse_iterator rbegin() const; + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed container. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_reverse_iterator crbegin() const; + + //! Effects: Returns a reverse_iterator pointing to the end + //! of the reversed container. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + reverse_iterator rend(); + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed container. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_reverse_iterator rend() const; + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed container. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_reverse_iterator crend() const; + + #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! Precondition: end_iterator must be a valid end iterator + //! of the container. + //! + //! Effects: Returns a const reference to the container associated to the end iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + static bstree_impl &container_from_end_iterator(iterator end_iterator) + { + return static_cast + (data_type::get_tree_base_from_end_iterator(end_iterator)); + } + + //! Precondition: end_iterator must be a valid end const_iterator + //! of the container. + //! + //! Effects: Returns a const reference to the container associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + static const bstree_impl &container_from_end_iterator(const_iterator end_iterator) + { + return static_cast + (data_type::get_tree_base_from_end_iterator(end_iterator)); + } + + //! Precondition: it must be a valid iterator + //! of the container. + //! + //! Effects: Returns a const reference to the container associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Logarithmic. + static bstree_impl &container_from_iterator(iterator it) + { return container_from_end_iterator(it.end_iterator_from_it()); } + + //! Precondition: it must be a valid end const_iterator + //! of container. + //! + //! Effects: Returns a const reference to the container associated to the end iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Logarithmic. + static const bstree_impl &container_from_iterator(const_iterator it) + { return container_from_end_iterator(it.end_iterator_from_it()); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! Effects: Returns the key_compare object used by the container. + //! + //! Complexity: Constant. + //! + //! Throws: If value_compare copy-constructor throws. + key_compare key_comp() const; + + //! Effects: Returns the value_compare object used by the container. + //! + //! Complexity: Constant. + //! + //! Throws: If value_compare copy-constructor throws. + value_compare value_comp() const; + + #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! Effects: Returns true if the container is empty. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + bool empty() const + { + if(ConstantTimeSize){ + return !this->data_type::sz_traits().get_size(); + } + else{ + return algo_type::unique(this->header_ptr()); + } + } + + //! Effects: Returns the number of elements stored in the container. + //! + //! Complexity: Linear to elements contained in *this + //! if constant-time size option is disabled. Constant time otherwise. + //! + //! Throws: Nothing. + size_type size() const + { + if(constant_time_size) + return this->sz_traits().get_size(); + else{ + return (size_type)node_algorithms::size(this->header_ptr()); + } + } + + //! Effects: Swaps the contents of two containers. + //! + //! Complexity: Constant. + //! + //! Throws: If the comparison functor's swap call throws. + void swap(bstree_impl& other) + { + //This can throw + using std::swap; + swap(this->comp(), this->comp()); + //These can't throw + node_algorithms::swap_tree(this->header_ptr(), node_ptr(other.header_ptr())); + if(constant_time_size){ + size_type backup = this->sz_traits().get_size(); + this->sz_traits().set_size(other.sz_traits().get_size()); + other.sz_traits().set_size(backup); + } + } + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! Cloner should yield to nodes equivalent to the original nodes. + //! + //! Effects: 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. Copies the predicate from the source container. + //! + //! If cloner throws, all cloned elements are unlinked and disposed + //! calling Disposer::operator()(pointer). + //! + //! Complexity: Linear to erased plus inserted elements. + //! + //! Throws: If cloner throws or predicate copy assignment throws. Basic guarantee. + template + void clone_from(const bstree_impl &src, Cloner cloner, Disposer disposer) + { + this->clear_and_dispose(disposer); + if(!src.empty()){ + detail::exception_disposer + rollback(*this, disposer); + node_algorithms::clone + (const_node_ptr(src.header_ptr()) + ,node_ptr(this->header_ptr()) + ,detail::node_cloner (cloner, &this->get_value_traits()) + ,detail::node_disposer(disposer, &this->get_value_traits())); + this->sz_traits().set_size(src.sz_traits().get_size()); + this->comp() = src.comp(); + rollback.release(); + } + } + + //! Requires: value must be an lvalue + //! + //! Effects: Inserts value into the container before the upper bound. + //! + //! Complexity: Average complexity for insert element is at + //! most logarithmic. + //! + //! Throws: If the internal value_compare ordering function throws. Strong guarantee. + //! + //! Note: Does not affect the validity of iterators and references. + //! No copy-constructors are called. + iterator insert_equal(reference value) + { + detail::key_nodeptr_comp + key_node_comp(this->comp(), &this->get_value_traits()); + node_ptr to_insert(this->get_value_traits().to_node_ptr(value)); + if(safemode_or_autounlink) + BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert)); + iterator ret(node_algorithms::insert_equal_upper_bound + (this->header_ptr(), to_insert, key_node_comp), this->priv_value_traits_ptr()); + this->sz_traits().increment(); + return ret; + } + + //! Requires: value must be an lvalue, and "hint" must be + //! a valid iterator. + //! + //! Effects: Inserts x into the container, 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) + //! + //! Complexity: Logarithmic in general, but it is amortized + //! constant time if t is inserted immediately before hint. + //! + //! Throws: If the internal value_compare ordering function throws. Strong guarantee. + //! + //! Note: Does not affect the validity of iterators and references. + //! No copy-constructors are called. + iterator insert_equal(const_iterator hint, reference value) + { + detail::key_nodeptr_comp + key_node_comp(this->comp(), &this->get_value_traits()); + node_ptr to_insert(this->get_value_traits().to_node_ptr(value)); + if(safemode_or_autounlink) + BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert)); + iterator ret(node_algorithms::insert_equal + (this->header_ptr(), hint.pointed_node(), to_insert, key_node_comp), this->priv_value_traits_ptr()); + this->sz_traits().increment(); + return ret; + } + + //! Requires: Dereferencing iterator must yield an lvalue + //! of type value_type. + //! + //! Effects: Inserts a each element of a range into the container + //! before the upper bound of the key of each element. + //! + //! Complexity: Insert range is in general O(N * log(N)), where N is the + //! size of the range. However, it is linear in N if the range is already sorted + //! by value_comp(). + //! + //! Throws: Nothing. + //! + //! Note: Does not affect the validity of iterators and references. + //! No copy-constructors are called. + template + void insert_equal(Iterator b, Iterator e) + { + iterator iend(this->end()); + for (; b != e; ++b) + this->insert_equal(iend, *b); + } + + //! Requires: value must be an lvalue + //! + //! Effects: Inserts value into the container if the value + //! is not already present. + //! + //! Complexity: Average complexity for insert element is at + //! most logarithmic. + //! + //! Throws: Nothing. + //! + //! Note: Does not affect the validity of iterators and references. + //! No copy-constructors are called. + std::pair insert_unique(reference value) + { + insert_commit_data commit_data; + std::pair ret = this->insert_unique_check(value, this->comp(), commit_data); + if(!ret.second) + return ret; + return std::pair (this->insert_unique_commit(value, commit_data), true); + } + + //! Requires: value must be an lvalue, and "hint" must be + //! a valid iterator + //! + //! Effects: Tries to insert x into the container, using "hint" as a hint + //! to where it will be inserted. + //! + //! Complexity: Logarithmic in general, but it is amortized + //! constant time (two comparisons in the worst case) + //! if t is inserted immediately before hint. + //! + //! Throws: Nothing. + //! + //! Note: Does not affect the validity of iterators and references. + //! No copy-constructors are called. + iterator insert_unique(const_iterator hint, reference value) + { + insert_commit_data commit_data; + std::pair ret = this->insert_unique_check(hint, value, this->comp(), commit_data); + if(!ret.second) + return ret.first; + return this->insert_unique_commit(value, commit_data); + } + + //! Requires: Dereferencing iterator must yield an lvalue + //! of type value_type. + //! + //! Effects: Tries to insert each element of a range into the container. + //! + //! Complexity: Insert range is in general O(N * log(N)), where N is the + //! size of the range. However, it is linear in N if the range is already sorted + //! by value_comp(). + //! + //! Throws: Nothing. + //! + //! Note: Does not affect the validity of iterators and references. + //! No copy-constructors are called. + template + void insert_unique(Iterator b, Iterator e) + { + if(this->empty()){ + iterator iend(this->end()); + for (; b != e; ++b) + this->insert_unique(iend, *b); + } + else{ + for (; b != e; ++b) + this->insert_unique(*b); + } + } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! Requires: key_value_comp must be a comparison function that induces + //! the same strict weak ordering as value_compare. The difference is that + //! key_value_comp compares an arbitrary key with the contained values. + //! + //! Effects: Checks if a value can be inserted in the container, using + //! a user provided key instead of the value itself. + //! + //! Returns: If there is an equivalent value + //! returns a pair containing an iterator to the already present value + //! and false. If the value can be inserted returns true in the returned + //! pair boolean and fills "commit_data" that is meant to be used with + //! the "insert_commit" function. + //! + //! Complexity: Average complexity is at most logarithmic. + //! + //! Throws: If the key_value_comp ordering function throws. Strong guarantee. + //! + //! Notes: This function is used to improve performance when constructing + //! a value_type is expensive: 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 value_type 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 value_type and use + //! "insert_commit" to insert the object 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_commit" only if no more + //! objects are inserted or erased from the container. + template + std::pair insert_unique_check + (const KeyType &key, KeyValueCompare key_value_comp, insert_commit_data &commit_data); + + //! Requires: key_value_comp must be a comparison function that induces + //! the same strict weak ordering as value_compare. The difference is that + //! key_value_comp compares an arbitrary key with the contained values. + //! + //! Effects: Checks if a value can be inserted in the container, using + //! a user provided key instead of the value itself, using "hint" + //! as a hint to where it will be inserted. + //! + //! Returns: If there is an equivalent value + //! returns a pair containing an iterator to the already present value + //! and false. If the value can be inserted returns true in the returned + //! pair boolean and fills "commit_data" that is meant to be used with + //! the "insert_commit" function. + //! + //! Complexity: Logarithmic in general, but it's amortized + //! constant time if t is inserted immediately before hint. + //! + //! Throws: If the key_value_comp ordering function throws. Strong guarantee. + //! + //! Notes: This function is used to improve performance when constructing + //! a value_type is expensive: if there is an equivalent value + //! the constructed object must be discarded. Many times, the part of the + //! constructing that is used to impose the order is much cheaper to construct + //! than the value_type and this function offers the possibility to use that key + //! to check if the insertion will be successful. + //! + //! If the check is successful, the user can construct the value_type and use + //! "insert_commit" to insert the object in constant-time. This can give a total + //! constant-time complexity to the insertion: check(O(1)) + commit(O(1)). + //! + //! "commit_data" remains valid for a subsequent "insert_commit" only if no more + //! objects are inserted or erased from the container. + template + std::pair insert_unique_check + (const_iterator hint, const KeyType &key + ,KeyValueCompare key_value_comp, insert_commit_data &commit_data); + + #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! Requires: value must be an lvalue of type value_type. commit_data + //! must have been obtained from a previous call to "insert_check". + //! No objects should have been inserted or erased from the container between + //! the "insert_check" that filled "commit_data" and the call to "insert_commit". + //! + //! Effects: Inserts the value in the container using the information obtained + //! from the "commit_data" that a previous "insert_check" filled. + //! + //! Returns: An iterator to the newly inserted object. + //! + //! Complexity: Constant time. + //! + //! Throws: Nothing. + //! + //! Notes: This function has only sense if a "insert_check" has been + //! previously executed to fill "commit_data". No value should be inserted or + //! erased between the "insert_check" and "insert_commit" calls. + iterator insert_unique_commit(reference value, const insert_commit_data &commit_data) + { + node_ptr to_insert(this->get_value_traits().to_node_ptr(value)); + if(safemode_or_autounlink) + BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert)); + node_algorithms::insert_unique_commit + (this->header_ptr(), to_insert, commit_data); + this->sz_traits().increment(); + return iterator(to_insert, this->priv_value_traits_ptr()); + } + + //! Requires: value must be an lvalue, "pos" must be + //! a valid iterator (or end) and must be the succesor of value + //! once inserted according to the predicate + //! + //! Effects: Inserts x into the container before "pos". + //! + //! Complexity: Constant time. + //! + //! Throws: Nothing. + //! + //! Note: This function does not check preconditions so if "pos" is not + //! the successor of "value" container ordering invariant will be broken. + //! This is a low-level function to be used only for performance reasons + //! by advanced users. + iterator insert_before(const_iterator pos, reference value) + { + node_ptr to_insert(this->get_value_traits().to_node_ptr(value)); + if(safemode_or_autounlink) + BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert)); + this->sz_traits().increment(); + return iterator(node_algorithms::insert_before + (this->header_ptr(), pos.pointed_node(), to_insert), this->priv_value_traits_ptr()); + } + + //! Requires: value must be an lvalue, and it must be no less + //! than the greatest inserted key + //! + //! Effects: Inserts x into the container in the last position. + //! + //! Complexity: Constant time. + //! + //! Throws: Nothing. + //! + //! Note: This function does not check preconditions so if value is + //! less than the greatest inserted key container ordering invariant will be broken. + //! This function is slightly more efficient than using "insert_before". + //! This is a low-level function to be used only for performance reasons + //! by advanced users. + void push_back(reference value) + { + node_ptr to_insert(this->get_value_traits().to_node_ptr(value)); + if(safemode_or_autounlink) + BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert)); + this->sz_traits().increment(); + node_algorithms::push_back(this->header_ptr(), to_insert); + } + + //! Requires: value must be an lvalue, and it must be no greater + //! than the minimum inserted key + //! + //! Effects: Inserts x into the container in the first position. + //! + //! Complexity: Constant time. + //! + //! Throws: Nothing. + //! + //! Note: This function does not check preconditions so if value is + //! greater than the minimum inserted key container ordering invariant will be broken. + //! This function is slightly more efficient than using "insert_before". + //! This is a low-level function to be used only for performance reasons + //! by advanced users. + void push_front(reference value) + { + node_ptr to_insert(this->get_value_traits().to_node_ptr(value)); + if(safemode_or_autounlink) + BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert)); + this->sz_traits().increment(); + node_algorithms::push_front(this->header_ptr(), to_insert); + } + + //! Effects: Erases the element pointed to by pos. + //! + //! Complexity: Average complexity for erase element is constant time. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + iterator erase(const_iterator i) + { + const_iterator ret(i); + ++ret; + node_ptr to_erase(i.pointed_node()); + if(safemode_or_autounlink) + BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!node_algorithms::unique(to_erase)); + node_algorithms::erase(this->header_ptr(), to_erase); + this->sz_traits().decrement(); + if(safemode_or_autounlink) + node_algorithms::init(to_erase); + return ret.unconst(); + } + + //! Effects: Erases the range pointed to by b end e. + //! + //! Complexity: Average complexity for erase range is at most + //! O(log(size() + N)), where N is the number of elements in the range. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + iterator erase(const_iterator b, const_iterator e) + { size_type n; return this->private_erase(b, e, n); } + + //! Effects: Erases all the elements with the given value. + //! + //! Returns: The number of erased elements. + //! + //! Complexity: O(log(size() + N). + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + size_type erase(const_reference value) + { return this->erase(value, this->comp()); } + + //! Effects: Erases all the elements with the given key. + //! according to the comparison functor "comp". + //! + //! Returns: The number of erased elements. + //! + //! Complexity: O(log(size() + N). + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + template + size_type erase(const KeyType& key, KeyValueCompare comp + /// @cond + , typename detail::enable_if_c::value >::type * = 0 + /// @endcond + ) + { + std::pair p = this->equal_range(key, comp); + size_type n; + this->private_erase(p.first, p.second, n); + return n; + } + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! + //! Effects: Erases the element pointed to by pos. + //! Disposer::operator()(pointer) is called for the removed element. + //! + //! Complexity: Average complexity for erase element is constant time. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators + //! to the erased elements. + template + iterator erase_and_dispose(const_iterator i, Disposer disposer) + { + node_ptr to_erase(i.pointed_node()); + iterator ret(this->erase(i)); + disposer(this->get_value_traits().to_value_ptr(to_erase)); + return ret; + } + + #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + template + iterator erase_and_dispose(iterator i, Disposer disposer) + { return this->erase_and_dispose(const_iterator(i), disposer); } + #endif + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! + //! Effects: Erases all the elements with the given value. + //! Disposer::operator()(pointer) is called for the removed elements. + //! + //! Returns: The number of erased elements. + //! + //! Complexity: O(log(size() + N). + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + template + size_type erase_and_dispose(const_reference value, Disposer disposer) + { + std::pair p = this->equal_range(value); + size_type n; + this->private_erase(p.first, p.second, n, disposer); + return n; + } + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! + //! Effects: Erases the range pointed to by b end e. + //! Disposer::operator()(pointer) is called for the removed elements. + //! + //! Complexity: Average complexity for erase range is at most + //! O(log(size() + N)), where N is the number of elements in the range. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators + //! to the erased elements. + template + iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer) + { size_type n; return this->private_erase(b, e, n, disposer); } + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! + //! Effects: Erases all the elements with the given key. + //! according to the comparison functor "comp". + //! Disposer::operator()(pointer) is called for the removed elements. + //! + //! Returns: The number of erased elements. + //! + //! Complexity: O(log(size() + N). + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators + //! to the erased elements. + template + size_type erase_and_dispose(const KeyType& key, KeyValueCompare comp, Disposer disposer + /// @cond + , typename detail::enable_if_c::value >::type * = 0 + /// @endcond + ) + { + std::pair p = this->equal_range(key, comp); + size_type n; + this->private_erase(p.first, p.second, n, disposer); + return n; + } + + //! Effects: Erases all of the elements. + //! + //! Complexity: Linear to the number of elements on the container. + //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + void clear() + { + if(safemode_or_autounlink){ + this->clear_and_dispose(detail::null_disposer()); + } + else{ + node_algorithms::init_header(this->header_ptr()); + this->sz_traits().set_size(0); + } + } + + //! Effects: Erases all of the elements calling disposer(p) for + //! each node to be erased. + //! Complexity: Average complexity for is at most O(log(size() + N)), + //! where N is the number of elements in the container. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. Calls N times to disposer functor. + template + void clear_and_dispose(Disposer disposer) + { + node_algorithms::clear_and_dispose(this->header_ptr() + , detail::node_disposer(disposer, &this->get_value_traits())); + node_algorithms::init_header(this->header_ptr()); + this->sz_traits().set_size(0); + } + + //! Effects: Returns the number of contained elements with the given value + //! + //! Complexity: Logarithmic to the number of elements contained plus lineal + //! to number of objects with the given value. + //! + //! Throws: If `value_compare` throws. + size_type count(const_reference value) const + { return size_type(this->count(value, this->comp())); } + + //! Effects: Returns the number of contained elements with the given key + //! + //! Complexity: Logarithmic to the number of elements contained plus lineal + //! to number of objects with the given key. + //! + //! Throws: If `comp` throws. + template + size_type count(const KeyType &key, KeyValueCompare comp) const + { + std::pair ret = this->equal_range(key, comp); + size_type n = 0; + for(; ret.first != ret.second; ++ret.first){ ++n; } + return n; + } + + #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + + //Add non-const overloads to theoretically const members + //as some algorithms have different behavior when non-const versions are used (like splay trees). + size_type count(const_reference value) + { return size_type(this->count(value, this->comp())); } + + template + size_type count(const KeyType &key, KeyValueCompare comp) + { + std::pair ret = this->equal_range(key, comp); + size_type n = 0; + for(; ret.first != ret.second; ++ret.first){ ++n; } + return n; + } + + #else //defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + + //! Effects: Returns an iterator to the first element whose + //! key is not less than k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `value_compare` throws. + iterator lower_bound(const_reference value); + + //! Effects: Returns an iterator to the first element whose + //! key is not less than k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `value_compare` throws. + const_iterator lower_bound(const_reference value) const; + + //! Effects: Returns an iterator to the first element whose + //! key is not less than k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `comp` throws. + template + iterator lower_bound(const KeyType &key, KeyValueCompare comp); + + //! Effects: Returns a const iterator to the first element whose + //! key is not less than k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `comp` throws. + template + const_iterator lower_bound(const KeyType &key, KeyValueCompare comp) const; + + //! Effects: Returns an iterator to the first element whose + //! key is greater than k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `value_compare` throws. + iterator upper_bound(const_reference value); + + //! Effects: Returns an iterator to the first element whose + //! key is greater than k according to comp or end() if that element + //! does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `comp` throws. + template + iterator upper_bound(const KeyType &key, KeyValueCompare comp); + + //! Effects: Returns an iterator to the first element whose + //! key is greater than k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `value_compare` throws. + const_iterator upper_bound(const_reference value) const; + + //! Effects: Returns an iterator to the first element whose + //! key is greater than k according to comp or end() if that element + //! does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `comp` throws. + template + const_iterator upper_bound(const KeyType &key, KeyValueCompare comp) const; + + //! Effects: Finds an iterator to the first element whose key is + //! k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `value_compare` throws. + iterator find(const_reference value); + + //! Effects: Finds an iterator to the first element whose key is + //! k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `comp` throws. + template + iterator find(const KeyType &key, KeyValueCompare comp); + + //! Effects: Finds a const_iterator to the first element whose key is + //! k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `value_compare` throws. + const_iterator find(const_reference value) const; + + //! Effects: Finds a const_iterator to the first element whose key is + //! k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `comp` throws. + template + const_iterator find(const KeyType &key, KeyValueCompare comp) const; + + //! Effects: Finds a range containing all elements whose key is k or + //! an empty range that indicates the position where those elements would be + //! if they there is no elements with key k. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `value_compare` throws. + std::pair equal_range(const_reference value); + + //! Effects: Finds a range containing all elements whose key is k or + //! an empty range that indicates the position where those elements would be + //! if they there is no elements with key k. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `comp` throws. + template + std::pair equal_range(const KeyType &key, KeyValueCompare comp); + + //! Effects: Finds a range containing all elements whose key is k or + //! an empty range that indicates the position where those elements would be + //! if they there is no elements with key k. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `value_compare` throws. + std::pair + equal_range(const_reference value) const; + + //! Effects: Finds a range containing all elements whose key is k or + //! an empty range that indicates the position where those elements would be + //! if they there is no elements with key k. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `comp` throws. + template + std::pair + equal_range(const KeyType &key, KeyValueCompare comp) const; + + //! Requires: 'lower_value' must not be greater than 'upper_value'. If + //! 'lower_value' == 'upper_value', ('left_closed' || 'right_closed') must be false. + //! + //! Effects: Returns an a pair with the following criteria: + //! + //! first = lower_bound(lower_key) if left_closed, upper_bound(lower_key) otherwise + //! + //! second = upper_bound(upper_key) if right_closed, lower_bound(upper_key) otherwise + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `value_compare` throws. + //! + //! Note: This function can be more efficient than calling upper_bound + //! and lower_bound for lower_value and upper_value. + //! + //! Note: Experimental function, the interface might change in future releases. + std::pair bounded_range + (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed); + + //! Requires: KeyValueCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the container. + //! 'lower_key' must not be greater than 'upper_key' according to 'comp'. If + //! 'lower_key' == 'upper_key', ('left_closed' || 'right_closed') must be false. + //! + //! Effects: Returns an a pair with the following criteria: + //! + //! first = lower_bound(lower_key, comp) if left_closed, upper_bound(lower_key, comp) otherwise + //! + //! second = upper_bound(upper_key, comp) if right_closed, lower_bound(upper_key, comp) otherwise + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `comp` throws. + //! + //! Note: This function can be more efficient than calling upper_bound + //! and lower_bound for lower_key and upper_key. + //! + //! Note: Experimental function, the interface might change in future releases. + template + std::pair bounded_range + (const KeyType &lower_key, const KeyType &upper_key, KeyValueCompare comp, bool left_closed, bool right_closed); + + //! Requires: 'lower_value' must not be greater than 'upper_value'. If + //! 'lower_value' == 'upper_value', ('left_closed' || 'right_closed') must be false. + //! + //! Effects: Returns an a pair with the following criteria: + //! + //! first = lower_bound(lower_key) if left_closed, upper_bound(lower_key) otherwise + //! + //! second = upper_bound(upper_key) if right_closed, lower_bound(upper_key) otherwise + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `value_compare` throws. + //! + //! Note: This function can be more efficient than calling upper_bound + //! and lower_bound for lower_value and upper_value. + //! + //! Note: Experimental function, the interface might change in future releases. + std::pair bounded_range + (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed) const; + + //! Requires: KeyValueCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the container. + //! 'lower_key' must not be greater than 'upper_key' according to 'comp'. If + //! 'lower_key' == 'upper_key', ('left_closed' || 'right_closed') must be false. + //! + //! Effects: Returns an a pair with the following criteria: + //! + //! first = lower_bound(lower_key, comp) if left_closed, upper_bound(lower_key, comp) otherwise + //! + //! second = upper_bound(upper_key, comp) if right_closed, lower_bound(upper_key, comp) otherwise + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `comp` throws. + //! + //! Note: This function can be more efficient than calling upper_bound + //! and lower_bound for lower_key and upper_key. + //! + //! Note: Experimental function, the interface might change in future releases. + template + std::pair bounded_range + (const KeyType &lower_key, const KeyType &upper_key, KeyValueCompare comp, bool left_closed, bool right_closed) const; + + //! Requires: value must be an lvalue and shall be in a set of + //! appropriate type. Otherwise the behavior is undefined. + //! + //! Effects: Returns: a valid iterator i belonging to the set + //! that points to the value + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Note: This static function is available only if the value traits + //! is stateless. + static iterator s_iterator_to(reference value); + + //! Requires: value must be an lvalue and shall be in a set of + //! appropriate type. Otherwise the behavior is undefined. + //! + //! Effects: Returns: a valid const_iterator i belonging to the + //! set that points to the value + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Note: This static function is available only if the value traits + //! is stateless. + static const_iterator s_iterator_to(const_reference value); + + //! Requires: value must be an lvalue and shall be in a set of + //! appropriate type. Otherwise the behavior is undefined. + //! + //! Effects: Returns: a valid iterator i belonging to the set + //! that points to the value + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + iterator iterator_to(reference value); + + //! Requires: value must be an lvalue and shall be in a set of + //! appropriate type. Otherwise the behavior is undefined. + //! + //! Effects: Returns: a valid const_iterator i belonging to the + //! set that points to the value + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_iterator iterator_to(const_reference value) const; + + //! Requires: value shall not be in a container. + //! + //! Effects: init_node puts the hook of a value in a well-known default + //! state. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant time. + //! + //! Note: This function puts the hook in the well-known default state + //! used by auto_unlink and safe hooks. + static void init_node(reference value); + + #endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + + //! Effects: Unlinks the leftmost node from the container. + //! + //! Complexity: Average complexity is constant time. + //! + //! Throws: Nothing. + //! + //! Notes: This function breaks the container and the container 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 container. + pointer unlink_leftmost_without_rebalance() + { + node_ptr to_be_disposed(node_algorithms::unlink_leftmost_without_rebalance + (this->header_ptr())); + if(!to_be_disposed) + return 0; + this->sz_traits().decrement(); + if(safemode_or_autounlink)//If this is commented does not work with normal_link + node_algorithms::init(to_be_disposed); + return this->get_value_traits().to_value_ptr(to_be_disposed); + } + + #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + + //! Requires: replace_this must be a valid iterator of *this + //! and with_this must not be inserted in any container. + //! + //! Effects: Replaces replace_this in its position in the + //! container with with_this. The container does not need to be rebalanced. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Note: This function will break container ordering invariants if + //! with_this is not equivalent to *replace_this according to the + //! ordering rules. This function is faster than erasing and inserting + //! the node, since no rebalancing or comparison is needed. + void replace_node(iterator replace_this, reference with_this); + + //! Effects: Rebalances the tree. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear. + void rebalance(); + + //! Requires: old_root is a node of a tree. + //! + //! Effects: Rebalances the subtree rooted at old_root. + //! + //! Returns: The new root of the subtree. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the elements in the subtree. + iterator rebalance_subtree(iterator root); + + #endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + + //! Effects: removes "value" from the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Logarithmic time. + //! + //! Note: This static function is only usable with non-constant + //! time size containers that have stateless comparison functors. + //! + //! If the user calls + //! this function with a constant time size container or stateful comparison + //! functor a compilation error will be issued. + static void remove_node(reference value) + { + BOOST_STATIC_ASSERT((!constant_time_size)); + node_ptr to_remove(value_traits::to_node_ptr(value)); + node_algorithms::unlink(to_remove); + if(safemode_or_autounlink) + node_algorithms::init(to_remove); + } + + //! Effects: Asserts the integrity of the container with additional checks provided by the user. + //! + //! Complexity: Linear time. + //! + //! Note: The method might not have effect when asserts are turned off (e.g., with NDEBUG). + //! Experimental function, interface might change in future versions. + template + void check(ExtraChecker extra_checker) const + { + typedef detail::key_nodeptr_comp nodeptr_comp_t; + nodeptr_comp_t nodeptr_comp(this->comp(), &this->get_value_traits()); + typedef typename get_node_checker::type node_checker_t; + typename node_checker_t::return_type checker_return; + node_algorithms::check(this->header_ptr(), node_checker_t(nodeptr_comp, extra_checker), checker_return); + if (constant_time_size) + BOOST_INTRUSIVE_INVARIANT_ASSERT(this->sz_traits().get_size() == checker_return.node_count); + } + + //! Effects: Asserts the integrity of the container. + //! + //! Complexity: Linear time. + //! + //! Note: The method has no effect when asserts are turned off (e.g., with NDEBUG). + //! Experimental function, interface might change in future versions. + void check() const + { + check(detail::empty_node_checker()); + } + + /// @cond + private: + template + iterator private_erase(const_iterator b, const_iterator e, size_type &n, Disposer disposer) + { + for(n = 0; b != e; ++n) + this->erase_and_dispose(b++, disposer); + return b.unconst(); + } + + iterator private_erase(const_iterator b, const_iterator e, size_type &n) + { + for(n = 0; b != e; ++n) + this->erase(b++); + return b.unconst(); + } + /// @endcond +}; + +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +template +#else +template +#endif +inline bool operator< +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +(const bstree_impl &x, const bstree_impl &y) +#else +( const bstree_impl &x +, const bstree_impl &y) +#endif +{ return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); } + +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +template +#else +template +#endif +bool operator== +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +(const bstree_impl &x, const bstree_impl &y) +#else +( const bstree_impl &x +, const bstree_impl &y) +#endif +{ + typedef bstree_impl tree_type; + typedef typename tree_type::const_iterator const_iterator; + + if(tree_type::constant_time_size && x.size() != y.size()){ + return false; + } + const_iterator end1 = x.end(); + const_iterator i1 = x.begin(); + const_iterator i2 = y.begin(); + if(tree_type::constant_time_size){ + while (i1 != end1 && *i1 == *i2) { + ++i1; + ++i2; + } + return i1 == end1; + } + else{ + const_iterator end2 = y.end(); + while (i1 != end1 && i2 != end2 && *i1 == *i2) { + ++i1; + ++i2; + } + return i1 == end1 && i2 == end2; + } +} + +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +template +#else +template +#endif +inline bool operator!= +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +(const bstree_impl &x, const bstree_impl &y) +#else +( const bstree_impl &x +, const bstree_impl &y) +#endif +{ return !(x == y); } + +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +template +#else +template +#endif +inline bool operator> +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +(const bstree_impl &x, const bstree_impl &y) +#else +( const bstree_impl &x +, const bstree_impl &y) +#endif +{ return y < x; } + +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +template +#else +template +#endif +inline bool operator<= +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +(const bstree_impl &x, const bstree_impl &y) +#else +( const bstree_impl &x +, const bstree_impl &y) +#endif +{ return !(y < x); } + +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +template +#else +template +#endif +inline bool operator>= +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +(const bstree_impl &x, const bstree_impl &y) +#else +( const bstree_impl &x +, const bstree_impl &y) +#endif +{ return !(x < y); } + +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +template +#else +template +#endif +inline void swap +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +(bstree_impl &x, bstree_impl &y) +#else +( bstree_impl &x +, bstree_impl &y) +#endif +{ x.swap(y); } + +//! Helper metafunction to define a \c bstree that yields to the same type when the +//! same options (either explicitly or implicitly) are used. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +struct make_bstree +{ + /// @cond + typedef typename pack_options + < bstree_defaults, + #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) + O1, O2, O3, O4, O5 + #else + Options... + #endif + >::type packed_options; + + typedef typename detail::get_value_traits + ::type value_traits; + typedef typename detail::get_header_holder_type + < value_traits, typename packed_options::header_holder_type >::type header_holder_type; + + typedef bstree_impl + < value_traits + , typename packed_options::compare + , typename packed_options::size_type + , packed_options::constant_time_size + , BsTreeAlgorithms + , header_holder_type + > implementation_defined; + /// @endcond + typedef implementation_defined type; +}; + + +#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + +#if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +class bstree + : public make_bstree::type +{ + typedef typename make_bstree + ::type Base; + BOOST_MOVABLE_BUT_NOT_COPYABLE(bstree) + + public: + typedef typename Base::value_compare value_compare; + typedef typename Base::value_traits value_traits; + typedef typename Base::iterator iterator; + typedef typename Base::const_iterator const_iterator; + + //Assert if passed value traits are compatible with the type + BOOST_STATIC_ASSERT((detail::is_same::value)); + + bstree( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : Base(cmp, v_traits) + {} + + template + bstree( bool unique, Iterator b, Iterator e + , const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : Base(unique, b, e, cmp, v_traits) + {} + + bstree(BOOST_RV_REF(bstree) x) + : Base(::boost::move(static_cast(x))) + {} + + bstree& operator=(BOOST_RV_REF(bstree) x) + { return static_cast(this->Base::operator=(::boost::move(static_cast(x)))); } + + static bstree &container_from_end_iterator(iterator end_iterator) + { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static const bstree &container_from_end_iterator(const_iterator end_iterator) + { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static bstree &container_from_iterator(iterator it) + { return static_cast(Base::container_from_iterator(it)); } + + static const bstree &container_from_iterator(const_iterator it) + { return static_cast(Base::container_from_iterator(it)); } +}; + +#endif +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_BSTREE_HPP diff --git a/boost/intrusive/bstree_algorithms.hpp b/boost/intrusive/bstree_algorithms.hpp new file mode 100644 index 0000000000..de5445ec52 --- /dev/null +++ b/boost/intrusive/bstree_algorithms.hpp @@ -0,0 +1,2127 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2007-2014 +// +// 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_BSTREE_ALGORITHMS_HPP +#define BOOST_INTRUSIVE_BSTREE_ALGORITHMS_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace intrusive { + +/// @cond + +//! This type is the information that will be filled by insert_unique_check +template +struct insert_commit_data_t +{ + insert_commit_data_t() + : link_left(false) + , node() + {} + bool link_left; + NodePtr node; +}; + +template +struct data_for_rebalance_t +{ + NodePtr x; + NodePtr x_parent; + NodePtr y; +}; + +namespace detail { + +template +struct bstree_node_checker + : public ExtraChecker +{ + typedef ExtraChecker base_checker_t; + typedef ValueTraits value_traits; + typedef typename value_traits::node_traits node_traits; + typedef typename node_traits::const_node_ptr const_node_ptr; + + struct return_type + : public base_checker_t::return_type + { + return_type() : min_key_node_ptr(const_node_ptr()), max_key_node_ptr(const_node_ptr()), node_count(0) {} + + const_node_ptr min_key_node_ptr; + const_node_ptr max_key_node_ptr; + size_t node_count; + }; + + bstree_node_checker(const NodePtrCompare& comp, ExtraChecker extra_checker) + : base_checker_t(extra_checker), comp_(comp) + {} + + void operator () (const const_node_ptr& p, + const return_type& check_return_left, const return_type& check_return_right, + return_type& check_return) + { + if (check_return_left.max_key_node_ptr) + BOOST_INTRUSIVE_INVARIANT_ASSERT(!comp_(p, check_return_left.max_key_node_ptr)); + if (check_return_right.min_key_node_ptr) + BOOST_INTRUSIVE_INVARIANT_ASSERT(!comp_(check_return_right.min_key_node_ptr, p)); + check_return.min_key_node_ptr = node_traits::get_left(p)? check_return_left.min_key_node_ptr : p; + check_return.max_key_node_ptr = node_traits::get_right(p)? check_return_right.max_key_node_ptr : p; + check_return.node_count = check_return_left.node_count + check_return_right.node_count + 1; + base_checker_t::operator()(p, check_return_left, check_return_right, check_return); + } + + const NodePtrCompare comp_; +}; + +} // namespace detail + +/// @endcond + + + +//! This is an implementation of a binary search tree. +//! A node in the search tree has references to its children and its parent. This +//! is to allow traversal of the whole tree from a given node making the +//! implementation of iterator a pointer to a node. +//! At the top of the tree a node is used specially. This node's parent pointer +//! is pointing to the root of the tree. Its left pointer points to the +//! leftmost node in the tree and the right pointer to the rightmost one. +//! This node is used to represent the end-iterator. +//! +//! +---------+ +//! header------------------------------>| | +//! | | +//! +----------(left)--------| |--------(right)---------+ +//! | +---------+ | +//! | | | +//! | | (parent) | +//! | | | +//! | | | +//! | +---------+ | +//! root of tree ..|......................> | | | +//! | | D | | +//! | | | | +//! | +-------+---------+-------+ | +//! | | | | +//! | | | | +//! | | | | +//! | | | | +//! | | | | +//! | +---------+ +---------+ | +//! | | | | | | +//! | | B | | F | | +//! | | | | | | +//! | +--+---------+--+ +--+---------+--+ | +//! | | | | | | +//! | | | | | | +//! | | | | | | +//! | +---+-----+ +-----+---+ +---+-----+ +-----+---+ | +//! +-->| | | | | | | |<--+ +//! | A | | C | | E | | G | +//! | | | | | | | | +//! +---------+ +---------+ +---------+ +---------+ +//! +//! bstree_algorithms is configured with a NodeTraits class, which encapsulates the +//! information about the node to be manipulated. NodeTraits must support the +//! following interface: +//! +//! Typedefs: +//! +//! node: The type of the node that forms the binary search tree +//! +//! node_ptr: A pointer to a node +//! +//! const_node_ptr: A pointer to a const node +//! +//! Static functions: +//! +//! static node_ptr get_parent(const_node_ptr n); +//! +//! static void set_parent(node_ptr n, node_ptr parent); +//! +//! static node_ptr get_left(const_node_ptr n); +//! +//! static void set_left(node_ptr n, node_ptr left); +//! +//! static node_ptr get_right(const_node_ptr n); +//! +//! static void set_right(node_ptr n, node_ptr right); +template +class bstree_algorithms +{ + public: + typedef typename NodeTraits::node node; + typedef NodeTraits node_traits; + typedef typename NodeTraits::node_ptr node_ptr; + typedef typename NodeTraits::const_node_ptr const_node_ptr; + typedef insert_commit_data_t insert_commit_data; + typedef data_for_rebalance_t data_for_rebalance; + + /// @cond + + private: + template + struct dispose_subtree_disposer + { + dispose_subtree_disposer(Disposer &disp, const node_ptr & subtree) + : disposer_(&disp), subtree_(subtree) + {} + + void release() + { disposer_ = 0; } + + ~dispose_subtree_disposer() + { + if(disposer_){ + dispose_subtree(subtree_, *disposer_); + } + } + Disposer *disposer_; + const node_ptr subtree_; + }; + + /// @endcond + + public: + //! Requires: 'header' is the header node of a tree. + //! + //! Effects: Returns the first node of the tree, the header if the tree is empty. + //! + //! Complexity: Constant time. + //! + //! Throws: Nothing. + static node_ptr begin_node(const const_node_ptr & header) + { return node_traits::get_left(header); } + + //! Requires: 'header' is the header node of a tree. + //! + //! Effects: Returns the header of the tree. + //! + //! Complexity: Constant time. + //! + //! Throws: Nothing. + static node_ptr end_node(const const_node_ptr & header) + { return detail::uncast(header); } + + //! Requires: 'header' is the header node of a tree. + //! + //! Effects: Returns the root of the tree if any, header otherwise + //! + //! Complexity: Constant time. + //! + //! Throws: Nothing. + static node_ptr root_node(const const_node_ptr & header) + { + node_ptr p = node_traits::get_parent(header); + return p ? p : detail::uncast(header); + } + + //! Requires: 'node' is a node of the tree or a node initialized + //! by init(...) or init_node. + //! + //! Effects: Returns true if the node is initialized by init() or init_node(). + //! + //! Complexity: Constant time. + //! + //! Throws: Nothing. + static bool unique(const const_node_ptr & node) + { return !NodeTraits::get_parent(node); } + + //! Requires: 'node' is a node of the tree or a header node. + //! + //! Effects: Returns the header of the tree. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + static node_ptr get_header(const const_node_ptr & node) + { + node_ptr n(detail::uncast(node)); + node_ptr p(NodeTraits::get_parent(node)); + //If p is null, then n is the header of an empty tree + if(p){ + //Non-empty tree, check if n is neither root nor header + node_ptr pp(NodeTraits::get_parent(p)); + //If granparent is not equal to n, then n is neither root nor header, + //the try the fast path + if(n != pp){ + do{ + n = p; + p = pp; + pp = NodeTraits::get_parent(pp); + }while(n != pp); + n = p; + } + //Check if n is root or header when size() > 0 + else if(!is_header(n)){ + n = p; + } + } + return n; + /* + node_ptr h = detail::uncast(node); + node_ptr p = NodeTraits::get_parent(node); + if(p){ + while(!is_header(p)) + p = NodeTraits::get_parent(p); + return p; + } + else{ + return h; + }*/ + } + + //! Requires: node1 and node2 can't be header nodes + //! of two trees. + //! + //! Effects: 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. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + //! + //! Note: 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(const node_ptr & node1, const node_ptr & node2) + { + if(node1 == node2) + return; + + node_ptr header1(get_header(node1)), header2(get_header(node2)); + swap_nodes(node1, header1, node2, header2); + } + + //! Requires: node1 and node2 can't be header nodes + //! of two trees with header header1 and header2. + //! + //! Effects: 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. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Note: 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(const node_ptr & node1, const node_ptr & header1, const node_ptr & node2, const node_ptr & header2) + { + if(node1 == node2) + return; + + //node1 and node2 must not be header nodes + //BOOST_INTRUSIVE_INVARIANT_ASSERT((header1 != node1 && header2 != node2)); + if(header1 != header2){ + //Update header1 if necessary + if(node1 == NodeTraits::get_left(header1)){ + NodeTraits::set_left(header1, node2); + } + + if(node1 == NodeTraits::get_right(header1)){ + NodeTraits::set_right(header1, node2); + } + + if(node1 == NodeTraits::get_parent(header1)){ + NodeTraits::set_parent(header1, node2); + } + + //Update header2 if necessary + if(node2 == NodeTraits::get_left(header2)){ + NodeTraits::set_left(header2, node1); + } + + if(node2 == NodeTraits::get_right(header2)){ + NodeTraits::set_right(header2, node1); + } + + if(node2 == NodeTraits::get_parent(header2)){ + NodeTraits::set_parent(header2, node1); + } + } + else{ + //If both nodes are from the same tree + //Update header if necessary + if(node1 == NodeTraits::get_left(header1)){ + NodeTraits::set_left(header1, node2); + } + else if(node2 == NodeTraits::get_left(header2)){ + NodeTraits::set_left(header2, node1); + } + + if(node1 == NodeTraits::get_right(header1)){ + NodeTraits::set_right(header1, node2); + } + else if(node2 == NodeTraits::get_right(header2)){ + NodeTraits::set_right(header2, node1); + } + + if(node1 == NodeTraits::get_parent(header1)){ + NodeTraits::set_parent(header1, node2); + } + else if(node2 == NodeTraits::get_parent(header2)){ + NodeTraits::set_parent(header2, node1); + } + + //Adjust data in nodes to be swapped + //so that final link swap works as expected + if(node1 == NodeTraits::get_parent(node2)){ + NodeTraits::set_parent(node2, node2); + + if(node2 == NodeTraits::get_right(node1)){ + NodeTraits::set_right(node1, node1); + } + else{ + NodeTraits::set_left(node1, node1); + } + } + else if(node2 == NodeTraits::get_parent(node1)){ + NodeTraits::set_parent(node1, node1); + + if(node1 == NodeTraits::get_right(node2)){ + NodeTraits::set_right(node2, node2); + } + else{ + NodeTraits::set_left(node2, node2); + } + } + } + + //Now swap all the links + node_ptr temp; + //swap left link + temp = NodeTraits::get_left(node1); + NodeTraits::set_left(node1, NodeTraits::get_left(node2)); + NodeTraits::set_left(node2, temp); + //swap right link + temp = NodeTraits::get_right(node1); + NodeTraits::set_right(node1, NodeTraits::get_right(node2)); + NodeTraits::set_right(node2, temp); + //swap parent link + temp = NodeTraits::get_parent(node1); + NodeTraits::set_parent(node1, NodeTraits::get_parent(node2)); + NodeTraits::set_parent(node2, temp); + + //Now adjust adjacent nodes for newly inserted node 1 + if((temp = NodeTraits::get_left(node1))){ + NodeTraits::set_parent(temp, node1); + } + if((temp = NodeTraits::get_right(node1))){ + NodeTraits::set_parent(temp, node1); + } + if((temp = NodeTraits::get_parent(node1)) && + //The header has been already updated so avoid it + temp != header2){ + if(NodeTraits::get_left(temp) == node2){ + NodeTraits::set_left(temp, node1); + } + if(NodeTraits::get_right(temp) == node2){ + NodeTraits::set_right(temp, node1); + } + } + //Now adjust adjacent nodes for newly inserted node 2 + if((temp = NodeTraits::get_left(node2))){ + NodeTraits::set_parent(temp, node2); + } + if((temp = NodeTraits::get_right(node2))){ + NodeTraits::set_parent(temp, node2); + } + if((temp = NodeTraits::get_parent(node2)) && + //The header has been already updated so avoid it + temp != header1){ + if(NodeTraits::get_left(temp) == node1){ + NodeTraits::set_left(temp, node2); + } + if(NodeTraits::get_right(temp) == node1){ + NodeTraits::set_right(temp, node2); + } + } + } + + //! Requires: node_to_be_replaced must be inserted in a tree + //! and new_node must not be inserted in a tree. + //! + //! Effects: Replaces node_to_be_replaced in its position in the + //! tree with new_node. The tree does not need to be rebalanced + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + //! + //! Note: 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(const node_ptr & node_to_be_replaced, const node_ptr & new_node) + { + if(node_to_be_replaced == new_node) + return; + replace_node(node_to_be_replaced, get_header(node_to_be_replaced), new_node); + } + + //! Requires: node_to_be_replaced must be inserted in a tree + //! with header "header" and new_node must not be inserted in a tree. + //! + //! Effects: Replaces node_to_be_replaced in its position in the + //! tree with new_node. The tree does not need to be rebalanced + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Note: 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(const node_ptr & node_to_be_replaced, const node_ptr & header, const node_ptr & new_node) + { + if(node_to_be_replaced == new_node) + return; + + //Update header if necessary + if(node_to_be_replaced == NodeTraits::get_left(header)){ + NodeTraits::set_left(header, new_node); + } + + if(node_to_be_replaced == NodeTraits::get_right(header)){ + NodeTraits::set_right(header, new_node); + } + + if(node_to_be_replaced == NodeTraits::get_parent(header)){ + NodeTraits::set_parent(header, new_node); + } + + //Now set data from the original node + node_ptr temp; + NodeTraits::set_left(new_node, NodeTraits::get_left(node_to_be_replaced)); + NodeTraits::set_right(new_node, NodeTraits::get_right(node_to_be_replaced)); + NodeTraits::set_parent(new_node, NodeTraits::get_parent(node_to_be_replaced)); + + //Now adjust adjacent nodes for newly inserted node + if((temp = NodeTraits::get_left(new_node))){ + NodeTraits::set_parent(temp, new_node); + } + if((temp = NodeTraits::get_right(new_node))){ + NodeTraits::set_parent(temp, new_node); + } + if((temp = NodeTraits::get_parent(new_node)) && + //The header has been already updated so avoid it + temp != header){ + if(NodeTraits::get_left(temp) == node_to_be_replaced){ + NodeTraits::set_left(temp, new_node); + } + if(NodeTraits::get_right(temp) == node_to_be_replaced){ + NodeTraits::set_right(temp, new_node); + } + } + } + + //! Requires: 'node' is a node from the tree except the header. + //! + //! Effects: Returns the next node of the tree. + //! + //! Complexity: Average constant time. + //! + //! Throws: Nothing. + static node_ptr next_node(const node_ptr & node) + { + node_ptr const n_right(NodeTraits::get_right(node)); + if(n_right){ + return minimum(n_right); + } + else { + node_ptr n(node); + node_ptr p(NodeTraits::get_parent(n)); + while(n == NodeTraits::get_right(p)){ + n = p; + p = NodeTraits::get_parent(p); + } + return NodeTraits::get_right(n) != p ? p : n; + } + } + + //! Requires: 'node' is a node from the tree except the leftmost node. + //! + //! Effects: Returns the previous node of the tree. + //! + //! Complexity: Average constant time. + //! + //! Throws: Nothing. + static node_ptr prev_node(const node_ptr & node) + { + if(is_header(node)){ + return NodeTraits::get_right(node); + //return maximum(NodeTraits::get_parent(node)); + } + else if(NodeTraits::get_left(node)){ + return maximum(NodeTraits::get_left(node)); + } + else { + node_ptr p(node); + node_ptr x = NodeTraits::get_parent(p); + while(p == NodeTraits::get_left(x)){ + p = x; + x = NodeTraits::get_parent(x); + } + return x; + } + } + + //! Requires: 'node' is a node of a tree but not the header. + //! + //! Effects: Returns the minimum node of the subtree starting at p. + //! + //! Complexity: Logarithmic to the size of the subtree. + //! + //! Throws: Nothing. + static node_ptr minimum(node_ptr node) + { + for(node_ptr p_left = NodeTraits::get_left(node) + ;p_left + ;p_left = NodeTraits::get_left(node)){ + node = p_left; + } + return node; + } + + //! Requires: 'node' is a node of a tree but not the header. + //! + //! Effects: Returns the maximum node of the subtree starting at p. + //! + //! Complexity: Logarithmic to the size of the subtree. + //! + //! Throws: Nothing. + static node_ptr maximum(node_ptr node) + { + for(node_ptr p_right = NodeTraits::get_right(node) + ;p_right + ;p_right = NodeTraits::get_right(node)){ + node = p_right; + } + return node; + } + + //! Requires: 'node' must not be part of any tree. + //! + //! Effects: After the function unique(node) == true. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Nodes: If node is inserted in a tree, this function corrupts the tree. + static void init(const node_ptr & node) + { + NodeTraits::set_parent(node, node_ptr()); + NodeTraits::set_left(node, node_ptr()); + NodeTraits::set_right(node, node_ptr()); + }; + + //! Effects: Returns true if node is in the same state as if called init(node) + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + static bool inited(const const_node_ptr & node) + { + return !NodeTraits::get_parent(node) && + !NodeTraits::get_left(node) && + !NodeTraits::get_right(node) ; + }; + + //! Requires: node must not be part of any tree. + //! + //! Effects: Initializes the header to represent an empty tree. + //! unique(header) == true. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Nodes: If node is inserted in a tree, this function corrupts the tree. + static void init_header(const node_ptr & header) + { + NodeTraits::set_parent(header, node_ptr()); + NodeTraits::set_left(header, header); + NodeTraits::set_right(header, header); + } + + //! Requires: "disposer" must be an object function + //! taking a node_ptr parameter and shouldn't throw. + //! + //! Effects: Empties the target tree calling + //! void disposer::operator()(const node_ptr &) for every node of the tree + //! except the header. + //! + //! Complexity: Linear to the number of element of the source tree plus the. + //! number of elements of tree target tree when calling this function. + //! + //! Throws: If cloner functor throws. If this happens target nodes are disposed. + template + static void clear_and_dispose(const node_ptr & header, Disposer disposer) + { + node_ptr source_root = NodeTraits::get_parent(header); + if(!source_root) + return; + dispose_subtree(source_root, disposer); + init_header(header); + } + + //! Requires: header is the header of a tree. + //! + //! Effects: Unlinks the leftmost node from the tree, and + //! updates the header link to the new leftmost node. + //! + //! Complexity: Average complexity is constant time. + //! + //! Throws: Nothing. + //! + //! Notes: 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(const node_ptr & header) + { + node_ptr leftmost = NodeTraits::get_left(header); + if (leftmost == header) + return node_ptr(); + node_ptr leftmost_parent(NodeTraits::get_parent(leftmost)); + node_ptr leftmost_right (NodeTraits::get_right(leftmost)); + bool is_root = leftmost_parent == header; + + if (leftmost_right){ + NodeTraits::set_parent(leftmost_right, leftmost_parent); + NodeTraits::set_left(header, bstree_algorithms::minimum(leftmost_right)); + + if (is_root) + NodeTraits::set_parent(header, leftmost_right); + else + NodeTraits::set_left(NodeTraits::get_parent(header), leftmost_right); + } + else if (is_root){ + NodeTraits::set_parent(header, node_ptr()); + NodeTraits::set_left(header, header); + NodeTraits::set_right(header, header); + } + else{ + NodeTraits::set_left(leftmost_parent, node_ptr()); + NodeTraits::set_left(header, leftmost_parent); + } + return leftmost; + } + + //! Requires: node is a node of the tree but it's not the header. + //! + //! Effects: Returns the number of nodes of the subtree. + //! + //! Complexity: Linear time. + //! + //! Throws: Nothing. + static std::size_t size(const const_node_ptr & header) + { + node_ptr beg(begin_node(header)); + node_ptr end(end_node(header)); + std::size_t i = 0; + for(;beg != end; beg = next_node(beg)) ++i; + return i; + } + + //! Requires: header1 and header2 must be the header nodes + //! of two trees. + //! + //! Effects: Swaps two trees. After the function header1 will contain + //! links to the second tree and header2 will have links to the first tree. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + static void swap_tree(const node_ptr & header1, const node_ptr & header2) + { + if(header1 == header2) + return; + + node_ptr tmp; + + //Parent swap + tmp = NodeTraits::get_parent(header1); + NodeTraits::set_parent(header1, NodeTraits::get_parent(header2)); + NodeTraits::set_parent(header2, tmp); + //Left swap + tmp = NodeTraits::get_left(header1); + NodeTraits::set_left(header1, NodeTraits::get_left(header2)); + NodeTraits::set_left(header2, tmp); + //Right swap + tmp = NodeTraits::get_right(header1); + NodeTraits::set_right(header1, NodeTraits::get_right(header2)); + NodeTraits::set_right(header2, tmp); + + //Now test parent + node_ptr h1_parent(NodeTraits::get_parent(header1)); + if(h1_parent){ + NodeTraits::set_parent(h1_parent, header1); + } + else{ + NodeTraits::set_left(header1, header1); + NodeTraits::set_right(header1, header1); + } + + node_ptr h2_parent(NodeTraits::get_parent(header2)); + if(h2_parent){ + NodeTraits::set_parent(h2_parent, header2); + } + else{ + NodeTraits::set_left(header2, header2); + NodeTraits::set_right(header2, header2); + } + } + + //! Requires: p is a node of a tree. + //! + //! Effects: Returns true if p is the header of the tree. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + static bool is_header(const const_node_ptr & p) + { + node_ptr p_left (NodeTraits::get_left(p)); + node_ptr p_right(NodeTraits::get_right(p)); + if(!NodeTraits::get_parent(p) || //Header condition when empty tree + (p_left && p_right && //Header always has leftmost and rightmost + (p_left == p_right || //Header condition when only node + (NodeTraits::get_parent(p_left) != p || + NodeTraits::get_parent(p_right) != p )) + //When tree size > 1 headers can't be leftmost's + //and rightmost's parent + )){ + return true; + } + return false; + } + + //! Requires: "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. + //! + //! Effects: Returns a node_ptr to the first element that is equivalent to + //! "key" according to "comp" or "header" if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If "comp" throws. + template + static node_ptr find + (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) + { + node_ptr end = detail::uncast(header); + node_ptr y = lower_bound(header, key, comp); + return (y == end || comp(key, y)) ? end : y; + } + + //! Requires: "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. + //! 'lower_key' must not be greater than 'upper_key' according to 'comp'. If + //! 'lower_key' == 'upper_key', ('left_closed' || 'right_closed') must be false. + //! + //! Effects: Returns an a pair with the following criteria: + //! + //! first = lower_bound(lower_key) if left_closed, upper_bound(lower_key) otherwise + //! + //! second = upper_bound(upper_key) if right_closed, lower_bound(upper_key) otherwise + //! + //! Complexity: Logarithmic. + //! + //! Throws: If "comp" throws. + //! + //! Note: This function can be more efficient than calling upper_bound + //! and lower_bound for lower_key and upper_key. + //! + //! Note: Experimental function, the interface might change. + template< class KeyType, class KeyNodePtrCompare> + static std::pair bounded_range + ( const const_node_ptr & header + , const KeyType &lower_key + , const KeyType &upper_key + , KeyNodePtrCompare comp + , bool left_closed + , bool right_closed) + { + node_ptr y = detail::uncast(header); + node_ptr x = NodeTraits::get_parent(header); + + while(x){ + //If x is less than lower_key the target + //range is on the right part + if(comp(x, lower_key)){ + //Check for invalid input range + BOOST_INTRUSIVE_INVARIANT_ASSERT(comp(x, upper_key)); + x = NodeTraits::get_right(x); + } + //If the upper_key is less than x, the target + //range is on the left part + else if(comp(upper_key, x)){ + y = x; + x = NodeTraits::get_left(x); + } + else{ + //x is inside the bounded range( x >= lower_key && x <= upper_key), + //so we must split lower and upper searches + // + //Sanity check: if lower_key and upper_key are equal, then both left_closed and right_closed can't be false + BOOST_INTRUSIVE_INVARIANT_ASSERT(left_closed || right_closed || comp(lower_key, x) || comp(x, upper_key)); + return std::pair( + left_closed + //If left_closed, then comp(x, lower_key) is already the lower_bound + //condition so we save one comparison and go to the next level + //following traditional lower_bound algo + ? lower_bound_loop(NodeTraits::get_left(x), x, lower_key, comp) + //If left-open, comp(x, lower_key) is not the upper_bound algo + //condition so we must recheck current 'x' node with upper_bound algo + : upper_bound_loop(x, y, lower_key, comp) + , + right_closed + //If right_closed, then comp(upper_key, x) is already the upper_bound + //condition so we can save one comparison and go to the next level + //following lower_bound algo + ? upper_bound_loop(NodeTraits::get_right(x), y, upper_key, comp) + //If right-open, comp(upper_key, x) is not the lower_bound algo + //condition so we must recheck current 'x' node with lower_bound algo + : lower_bound_loop(x, y, upper_key, comp) + ); + } + } + return std::pair (y, y); + } + + //! Requires: "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. + //! + //! Effects: Returns the number of elements with a key equivalent to "key" + //! according to "comp". + //! + //! Complexity: Logarithmic. + //! + //! Throws: If "comp" throws. + template + static std::size_t count + (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) + { + std::pair ret = equal_range(header, key, comp); + std::size_t n = 0; + while(ret.first != ret.second){ + ++n; + ret.first = next_node(ret.first); + } + return n; + } + + //! Requires: "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. + //! + //! Effects: 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 there are no equivalent elements. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If "comp" throws. + template + static std::pair equal_range + (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) + { + return bounded_range(header, key, key, comp, true, true); + } + + //! Requires: "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. + //! + //! Effects: Returns an a pair of node_ptr delimiting a range containing + //! the first element that is equivalent to "key" according to "comp" or an + //! empty range that indicates the position where that element would be + //! if there are no equivalent elements. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If "comp" throws. + template + static std::pair lower_bound_range + (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) + { + node_ptr const lb(lower_bound(header, key, comp)); + std::pair ret_ii(lb, lb); + if(lb != header && !comp(key, lb)){ + ret_ii.second = next_node(ret_ii.second); + } + return ret_ii; + } + + //! Requires: "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. + //! + //! Effects: Returns a node_ptr to the first element that is + //! not less than "key" according to "comp" or "header" if that element does + //! not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If "comp" throws. + template + static node_ptr lower_bound + (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) + { + return lower_bound_loop(NodeTraits::get_parent(header), detail::uncast(header), key, comp); + } + + //! Requires: "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. + //! + //! Effects: Returns a node_ptr to the first element that is greater + //! than "key" according to "comp" or "header" if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If "comp" throws. + template + static node_ptr upper_bound + (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) + { + return upper_bound_loop(NodeTraits::get_parent(header), detail::uncast(header), key, comp); + } + + //! Requires: "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". + //! + //! + //! Effects: Inserts new_node in the set using the information obtained + //! from the "commit_data" that a previous "insert_check" filled. + //! + //! Complexity: Constant time. + //! + //! Throws: Nothing. + //! + //! Notes: 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 + (const node_ptr & header, const node_ptr & new_value, const insert_commit_data &commit_data) + { return insert_commit(header, new_value, commit_data); } + + //! Requires: "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. + //! + //! Effects: 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. + //! + //! Returns: 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. + //! + //! Complexity: Average complexity is at most logarithmic. + //! + //! Throws: If "comp" throws. + //! + //! Notes: 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 + static std::pair insert_unique_check + (const const_node_ptr & header, const KeyType &key + ,KeyNodePtrCompare comp, insert_commit_data &commit_data + #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + , std::size_t *pdepth = 0 + #endif + ) + { + std::size_t depth = 0; + node_ptr h(detail::uncast(header)); + node_ptr y(h); + node_ptr x(NodeTraits::get_parent(y)); + node_ptr prev = node_ptr(); + + //Find the upper bound, cache the previous value and if we should + //store it in the left or right node + bool left_child = true; + while(x){ + ++depth; + y = x; + x = (left_child = comp(key, x)) ? + NodeTraits::get_left(x) : (prev = y, NodeTraits::get_right(x)); + } + + if(pdepth) *pdepth = depth; + + //Since we've found the upper bound there is no other value with the same key if: + // - There is no previous node + // - The previous node is less than the key + const bool not_present = !prev || comp(prev, key); + if(not_present){ + commit_data.link_left = left_child; + commit_data.node = y; + } + return std::pair(prev, not_present); + } + + //! Requires: "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. + //! + //! Effects: 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). + //! + //! Returns: 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. + //! + //! Complexity: Average complexity is at most logarithmic, but it is + //! amortized constant time if new_node should be inserted immediately before "hint". + //! + //! Throws: If "comp" throws. + //! + //! Notes: 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 + static std::pair insert_unique_check + (const const_node_ptr & header, const node_ptr &hint, const KeyType &key + ,KeyNodePtrCompare comp, insert_commit_data &commit_data + #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + , std::size_t *pdepth = 0 + #endif + ) + { + //hint must be bigger than the key + if(hint == header || comp(key, hint)){ + node_ptr prev(hint); + //Previous value should be less than the key + if(hint == begin_node(header) || comp((prev = prev_node(hint)), key)){ + commit_data.link_left = unique(header) || !NodeTraits::get_left(hint); + commit_data.node = commit_data.link_left ? hint : prev; + if(pdepth){ + *pdepth = commit_data.node == header ? 0 : depth(commit_data.node) + 1; + } + return std::pair(node_ptr(), true); + } + } + //Hint was wrong, use hintless insertion + return insert_unique_check(header, key, comp, commit_data, pdepth); + } + + //! Requires: "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. + //! + //! Effects: 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). + //! + //! Complexity: Logarithmic in general, but it is amortized + //! constant time if new_node is inserted immediately before "hint". + //! + //! Throws: If "comp" throws. + template + static node_ptr insert_equal + (const node_ptr & h, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp + #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + , std::size_t *pdepth = 0 + #endif + ) + { + insert_commit_data commit_data; + insert_equal_check(h, hint, new_node, comp, commit_data, pdepth); + insert_commit(h, new_node, commit_data); + return new_node; + } + + //! Requires: "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. + //! + //! Effects: Inserts new_node into the tree before the upper bound + //! according to "comp". + //! + //! Complexity: Average complexity for insert element is at + //! most logarithmic. + //! + //! Throws: If "comp" throws. + template + static node_ptr insert_equal_upper_bound + (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp + #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + , std::size_t *pdepth = 0 + #endif + ) + { + insert_commit_data commit_data; + insert_equal_upper_bound_check(h, new_node, comp, commit_data, pdepth); + insert_commit(h, new_node, commit_data); + return new_node; + } + + //! Requires: "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. + //! + //! Effects: Inserts new_node into the tree before the lower bound + //! according to "comp". + //! + //! Complexity: Average complexity for insert element is at + //! most logarithmic. + //! + //! Throws: If "comp" throws. + template + static node_ptr insert_equal_lower_bound + (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp + #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + , std::size_t *pdepth = 0 + #endif + ) + { + insert_commit_data commit_data; + insert_equal_lower_bound_check(h, new_node, comp, commit_data, pdepth); + insert_commit(h, new_node, commit_data); + return new_node; + } + + //! Requires: "header" must be the header node of a tree. + //! "pos" must be a valid iterator or header (end) node. + //! "pos" must be an iterator pointing to the successor to "new_node" + //! once inserted according to the order of already inserted nodes. This function does not + //! check "pos" and this precondition must be guaranteed by the caller. + //! + //! Effects: Inserts new_node into the tree before "pos". + //! + //! Complexity: Constant-time. + //! + //! Throws: Nothing. + //! + //! Note: If "pos" is not the successor of the newly inserted "new_node" + //! tree invariants might be broken. + static node_ptr insert_before + (const node_ptr & header, const node_ptr & pos, const node_ptr & new_node + #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + , std::size_t *pdepth = 0 + #endif + ) + { + insert_commit_data commit_data; + insert_before_check(header, pos, commit_data, pdepth); + insert_commit(header, new_node, commit_data); + return new_node; + } + + //! Requires: "header" must be the header node of a tree. + //! "new_node" must be, according to the used ordering no less than the + //! greatest inserted key. + //! + //! Effects: Inserts new_node into the tree before "pos". + //! + //! Complexity: Constant-time. + //! + //! Throws: Nothing. + //! + //! Note: If "new_node" is less than the greatest inserted key + //! tree invariants are broken. This function is slightly faster than + //! using "insert_before". + static void push_back + (const node_ptr & header, const node_ptr & new_node + #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + , std::size_t *pdepth = 0 + #endif + ) + { + insert_commit_data commit_data; + push_back_check(header, commit_data, pdepth); + insert_commit(header, new_node, commit_data); + } + + //! Requires: "header" must be the header node of a tree. + //! "new_node" must be, according to the used ordering, no greater than the + //! lowest inserted key. + //! + //! Effects: Inserts new_node into the tree before "pos". + //! + //! Complexity: Constant-time. + //! + //! Throws: Nothing. + //! + //! Note: If "new_node" is greater than the lowest inserted key + //! tree invariants are broken. This function is slightly faster than + //! using "insert_before". + static void push_front + (const node_ptr & header, const node_ptr & new_node + #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + , std::size_t *pdepth = 0 + #endif + ) + { + insert_commit_data commit_data; + push_front_check(header, commit_data, pdepth); + insert_commit(header, new_node, commit_data); + } + + //! Requires: 'node' can't be a header node. + //! + //! Effects: Calculates the depth of a node: the depth of a + //! node is the length (number of edges) of the path from the root + //! to that node. (The root node is at depth 0.) + //! + //! Complexity: Logarithmic to the number of nodes in the tree. + //! + //! Throws: Nothing. + static std::size_t depth(const_node_ptr node) + { + std::size_t depth = 0; + node_ptr p_parent; + while(node != NodeTraits::get_parent(p_parent = NodeTraits::get_parent(node))){ + ++depth; + node = p_parent; + } + return depth; + } + + //! Requires: "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. + //! + //! Effects: First empties target tree calling + //! void disposer::operator()(const node_ptr &) for every node of the tree + //! except the header. + //! + //! Then, duplicates the entire tree pointed by "source_header" cloning each + //! source node with node_ptr Cloner::operator()(const node_ptr &) to obtain + //! the nodes of the target tree. If "cloner" throws, the cloned target nodes + //! are disposed using void disposer(const node_ptr &). + //! + //! Complexity: Linear to the number of element of the source tree plus the. + //! number of elements of tree target tree when calling this function. + //! + //! Throws: If cloner functor throws. If this happens target nodes are disposed. + template + static void clone + (const const_node_ptr & source_header, const node_ptr & target_header, Cloner cloner, Disposer disposer) + { + if(!unique(target_header)){ + clear_and_dispose(target_header, disposer); + } + + node_ptr leftmost, rightmost; + node_ptr new_root = clone_subtree + (source_header, target_header, cloner, disposer, leftmost, rightmost); + + //Now update header node + NodeTraits::set_parent(target_header, new_root); + NodeTraits::set_left (target_header, leftmost); + NodeTraits::set_right (target_header, rightmost); + } + + //! Requires: header must be the header of a tree, z a node + //! of that tree and z != header. + //! + //! Effects: Erases node "z" from the tree with header "header". + //! + //! Complexity: Amortized constant time. + //! + //! Throws: Nothing. + static void erase(const node_ptr & header, const node_ptr & z) + { + data_for_rebalance ignored; + erase(header, z, ignored); + } + + //! Requires: node is a tree node but not the header. + //! + //! Effects: Unlinks the node and rebalances the tree. + //! + //! Complexity: Average complexity is constant time. + //! + //! Throws: Nothing. + static void unlink(const node_ptr & node) + { + node_ptr x = NodeTraits::get_parent(node); + if(x){ + while(!is_header(x)) + x = NodeTraits::get_parent(x); + erase(x, node); + } + } + + //! Requires: header must be the header of a tree. + //! + //! Effects: Rebalances the tree. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear. + static void rebalance(const node_ptr & header) + { + node_ptr root = NodeTraits::get_parent(header); + if(root){ + rebalance_subtree(root); + } + } + + //! Requires: old_root is a node of a tree. It shall not be null. + //! + //! Effects: Rebalances the subtree rooted at old_root. + //! + //! Returns: The new root of the subtree. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear. + static node_ptr rebalance_subtree(const node_ptr & old_root) + { + //Taken from: + //"Tree rebalancing in optimal time and space" + //Quentin F. Stout and Bette L. Warren + + //To avoid irregularities in the algorithm (old_root can be a + //left or right child or even the root of the tree) just put the + //root as the right child of its parent. Before doing this backup + //information to restore the original relationship after + //the algorithm is applied. + node_ptr super_root = NodeTraits::get_parent(old_root); + BOOST_INTRUSIVE_INVARIANT_ASSERT(super_root); + + //Get root info + node_ptr super_root_right_backup = NodeTraits::get_right(super_root); + bool super_root_is_header = NodeTraits::get_parent(super_root) == old_root; + bool old_root_is_right = is_right_child(old_root); + NodeTraits::set_right(super_root, old_root); + + std::size_t size; + subtree_to_vine(super_root, size); + vine_to_subtree(super_root, size); + node_ptr new_root = NodeTraits::get_right(super_root); + + //Recover root + if(super_root_is_header){ + NodeTraits::set_right(super_root, super_root_right_backup); + NodeTraits::set_parent(super_root, new_root); + } + else if(old_root_is_right){ + NodeTraits::set_right(super_root, new_root); + } + else{ + NodeTraits::set_right(super_root, super_root_right_backup); + NodeTraits::set_left(super_root, new_root); + } + return new_root; + } + + //! Effects: Asserts the integrity of the container with additional checks provided by the user. + //! + //! Requires: header must be the header of a tree. + //! + //! Complexity: Linear time. + //! + //! Note: The method might not have effect when asserts are turned off (e.g., with NDEBUG). + //! Experimental function, interface might change in future versions. + template + static void check(const const_node_ptr& header, Checker checker, typename Checker::return_type& checker_return) + { + const_node_ptr root_node_ptr = NodeTraits::get_parent(header); + if (!root_node_ptr) + { + // check left&right header pointers + BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_left(header) == header); + BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_right(header) == header); + } + else + { + // check parent pointer of root node + BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_parent(root_node_ptr) == header); + // check subtree from root + check_subtree(root_node_ptr, checker, checker_return); + // check left&right header pointers + const_node_ptr p = root_node_ptr; + while (NodeTraits::get_left(p)) { p = NodeTraits::get_left(p); } + BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_left(header) == p); + p = root_node_ptr; + while (NodeTraits::get_right(p)) { p = NodeTraits::get_right(p); } + BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_right(header) == p); + } + } + + protected: + static void erase(const node_ptr & header, const node_ptr & z, data_for_rebalance &info) + { + node_ptr y(z); + node_ptr x; + const node_ptr z_left(NodeTraits::get_left(z)); + const node_ptr z_right(NodeTraits::get_right(z)); + + if(!z_left){ + x = z_right; // x might be null. + } + else if(!z_right){ // z has exactly one non-null child. y == z. + x = z_left; // x is not null. + BOOST_ASSERT(x); + } + else{ //make y != z + // y = find z's successor + y = bstree_algorithms::minimum(z_right); + x = NodeTraits::get_right(y); // x might be null. + } + + node_ptr x_parent; + const node_ptr z_parent(NodeTraits::get_parent(z)); + const bool z_is_leftchild(NodeTraits::get_left(z_parent) == z); + + if(y != z){ //has two children and y is the minimum of z + //y is z's successor and it has a null left child. + //x is the right child of y (it can be null) + //Relink y in place of z and link x with y's old parent + NodeTraits::set_parent(z_left, y); + NodeTraits::set_left(y, z_left); + if(y != z_right){ + //Link y with the right tree of z + NodeTraits::set_right(y, z_right); + NodeTraits::set_parent(z_right, y); + //Link x with y's old parent (y must be a left child) + x_parent = NodeTraits::get_parent(y); + BOOST_ASSERT(NodeTraits::get_left(x_parent) == y); + if(x) + NodeTraits::set_parent(x, x_parent); + //Since y was the successor and not the right child of z, it must be a left child + NodeTraits::set_left(x_parent, x); + } + else{ //y was the right child of y so no need to fix x's position + x_parent = y; + } + NodeTraits::set_parent(y, z_parent); + bstree_algorithms::set_child(header, y, z_parent, z_is_leftchild); + } + else { // z has zero or one child, x is one child (it can be null) + //Just link x to z's parent + x_parent = z_parent; + if(x) + NodeTraits::set_parent(x, z_parent); + bstree_algorithms::set_child(header, x, z_parent, z_is_leftchild); + + //Now update leftmost/rightmost in case z was one of them + if(NodeTraits::get_left(header) == z){ + //z_left must be null because z is the leftmost + BOOST_ASSERT(!z_left); + NodeTraits::set_left(header, !z_right ? + z_parent : // makes leftmost == header if z == root + bstree_algorithms::minimum(z_right)); + } + if(NodeTraits::get_right(header) == z){ + //z_right must be null because z is the rightmost + BOOST_ASSERT(!z_right); + NodeTraits::set_right(header, !z_left ? + z_parent : // makes rightmost == header if z == root + bstree_algorithms::maximum(z_left)); + } + } + + //If z had 0/1 child, y == z and one of its children (and maybe null) + //If z had 2 children, y is the successor of z and x is the right child of y + info.x = x; + info.y = y; + //If z had 0/1 child, x_parent is the new parent of the old right child of y (z's successor) + //If z had 2 children, x_parent is the new parent of y (z_parent) + BOOST_ASSERT(!x || NodeTraits::get_parent(x) == x_parent); + info.x_parent = x_parent; + } + + //! Requires: node is a node of the tree but it's not the header. + //! + //! Effects: Returns the number of nodes of the subtree. + //! + //! Complexity: Linear time. + //! + //! Throws: Nothing. + static std::size_t subtree_size(const const_node_ptr & subtree) + { + std::size_t count = 0; + if (subtree){ + node_ptr n = detail::uncast(subtree); + node_ptr m = NodeTraits::get_left(n); + while(m){ + n = m; + m = NodeTraits::get_left(n); + } + + while(1){ + ++count; + node_ptr n_right(NodeTraits::get_right(n)); + if(n_right){ + n = n_right; + m = NodeTraits::get_left(n); + while(m){ + n = m; + m = NodeTraits::get_left(n); + } + } + else { + do{ + if (n == subtree){ + return count; + } + m = n; + n = NodeTraits::get_parent(n); + }while(NodeTraits::get_left(n) != m); + } + } + } + return count; + } + + //! Requires: p is a node of a tree. + //! + //! Effects: Returns true if p is a left child. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + static bool is_left_child(const node_ptr & p) + { return NodeTraits::get_left(NodeTraits::get_parent(p)) == p; } + + //! Requires: p is a node of a tree. + //! + //! Effects: Returns true if p is a right child. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + static bool is_right_child(const node_ptr & p) + { return NodeTraits::get_right(NodeTraits::get_parent(p)) == p; } + + static void insert_before_check + (const node_ptr &header, const node_ptr & pos + , insert_commit_data &commit_data + #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + , std::size_t *pdepth = 0 + #endif + ) + { + node_ptr prev(pos); + if(pos != NodeTraits::get_left(header)) + prev = prev_node(pos); + bool link_left = unique(header) || !NodeTraits::get_left(pos); + commit_data.link_left = link_left; + commit_data.node = link_left ? pos : prev; + if(pdepth){ + *pdepth = commit_data.node == header ? 0 : depth(commit_data.node) + 1; + } + } + + static void push_back_check + (const node_ptr & header, insert_commit_data &commit_data + #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + , std::size_t *pdepth = 0 + #endif + ) + { + node_ptr prev(NodeTraits::get_right(header)); + if(pdepth){ + *pdepth = prev == header ? 0 : depth(prev) + 1; + } + commit_data.link_left = false; + commit_data.node = prev; + } + + static void push_front_check + (const node_ptr & header, insert_commit_data &commit_data + #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + , std::size_t *pdepth = 0 + #endif + ) + { + node_ptr pos(NodeTraits::get_left(header)); + if(pdepth){ + *pdepth = pos == header ? 0 : depth(pos) + 1; + } + commit_data.link_left = true; + commit_data.node = pos; + } + + template + static void insert_equal_check + (const node_ptr &header, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp + , insert_commit_data &commit_data + /// @cond + , std::size_t *pdepth = 0 + /// @endcond + ) + { + if(hint == header || !comp(hint, new_node)){ + node_ptr prev(hint); + if(hint == NodeTraits::get_left(header) || + !comp(new_node, (prev = prev_node(hint)))){ + bool link_left = unique(header) || !NodeTraits::get_left(hint); + commit_data.link_left = link_left; + commit_data.node = link_left ? hint : prev; + if(pdepth){ + *pdepth = commit_data.node == header ? 0 : depth(commit_data.node) + 1; + } + } + else{ + insert_equal_upper_bound_check(header, new_node, comp, commit_data, pdepth); + } + } + else{ + insert_equal_lower_bound_check(header, new_node, comp, commit_data, pdepth); + } + } + + template + static void insert_equal_upper_bound_check + (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp, insert_commit_data & commit_data, std::size_t *pdepth = 0) + { + std::size_t depth = 0; + node_ptr y(h); + node_ptr x(NodeTraits::get_parent(y)); + + while(x){ + ++depth; + y = x; + x = comp(new_node, x) ? + NodeTraits::get_left(x) : NodeTraits::get_right(x); + } + if(pdepth) *pdepth = depth; + commit_data.link_left = (y == h) || comp(new_node, y); + commit_data.node = y; + } + + template + static void insert_equal_lower_bound_check + (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp, insert_commit_data & commit_data, std::size_t *pdepth = 0) + { + std::size_t depth = 0; + node_ptr y(h); + node_ptr x(NodeTraits::get_parent(y)); + + while(x){ + ++depth; + y = x; + x = !comp(x, new_node) ? + NodeTraits::get_left(x) : NodeTraits::get_right(x); + } + if(pdepth) *pdepth = depth; + commit_data.link_left = (y == h) || !comp(y, new_node); + commit_data.node = y; + } + + static void insert_commit + (const node_ptr & header, const node_ptr & new_node, const insert_commit_data &commit_data) + { + //Check if commit_data has not been initialized by a insert_unique_check call. + BOOST_INTRUSIVE_INVARIANT_ASSERT(commit_data.node != node_ptr()); + node_ptr parent_node(commit_data.node); + if(parent_node == header){ + NodeTraits::set_parent(header, new_node); + NodeTraits::set_right(header, new_node); + NodeTraits::set_left(header, new_node); + } + else if(commit_data.link_left){ + NodeTraits::set_left(parent_node, new_node); + if(parent_node == NodeTraits::get_left(header)) + NodeTraits::set_left(header, new_node); + } + else{ + NodeTraits::set_right(parent_node, new_node); + if(parent_node == NodeTraits::get_right(header)) + NodeTraits::set_right(header, new_node); + } + NodeTraits::set_parent(new_node, parent_node); + NodeTraits::set_right(new_node, node_ptr()); + NodeTraits::set_left(new_node, node_ptr()); + } + + //Fix header and own's parent data when replacing x with own, providing own's old data with parent + static void set_child(const node_ptr & header, const node_ptr & new_child, const node_ptr & new_parent, const bool link_left) + { + if(new_parent == header) + NodeTraits::set_parent(header, new_child); + else if(link_left) + NodeTraits::set_left(new_parent, new_child); + else + NodeTraits::set_right(new_parent, new_child); + } + + // rotate p to left (no header and p's parent fixup) + static void rotate_left_no_parent_fix(const node_ptr & p, const node_ptr &p_right) + { + node_ptr p_right_left(NodeTraits::get_left(p_right)); + NodeTraits::set_right(p, p_right_left); + if(p_right_left){ + NodeTraits::set_parent(p_right_left, p); + } + NodeTraits::set_left(p_right, p); + NodeTraits::set_parent(p, p_right); + } + + // rotate p to left (with header and p's parent fixup) + static void rotate_left(const node_ptr & p, const node_ptr & p_right, const node_ptr & p_parent, const node_ptr & header) + { + const bool p_was_left(NodeTraits::get_left(p_parent) == p); + rotate_left_no_parent_fix(p, p_right); + NodeTraits::set_parent(p_right, p_parent); + set_child(header, p_right, p_parent, p_was_left); + } + + // rotate p to right (no header and p's parent fixup) + static void rotate_right_no_parent_fix(const node_ptr & p, const node_ptr &p_left) + { + node_ptr p_left_right(NodeTraits::get_right(p_left)); + NodeTraits::set_left(p, p_left_right); + if(p_left_right){ + NodeTraits::set_parent(p_left_right, p); + } + NodeTraits::set_right(p_left, p); + NodeTraits::set_parent(p, p_left); + } + + // rotate p to right (with header and p's parent fixup) + static void rotate_right(const node_ptr & p, const node_ptr & p_left, const node_ptr & p_parent, const node_ptr & header) + { + const bool p_was_left(NodeTraits::get_left(p_parent) == p); + rotate_right_no_parent_fix(p, p_left); + NodeTraits::set_parent(p_left, p_parent); + set_child(header, p_left, p_parent, p_was_left); + } + + private: + + static void subtree_to_vine(node_ptr vine_tail, std::size_t &size) + { + //Inspired by LibAVL: + //It uses a clever optimization for trees with parent pointers. + //No parent pointer is updated when transforming a tree to a vine as + //most of them will be overriten during compression rotations. + //A final pass must be made after the rebalancing to updated those + //pointers not updated by tree_to_vine + compression calls + std::size_t len = 0; + node_ptr remainder = NodeTraits::get_right(vine_tail); + while(remainder){ + node_ptr tempptr = NodeTraits::get_left(remainder); + if(!tempptr){ //move vine-tail down one + vine_tail = remainder; + remainder = NodeTraits::get_right(remainder); + ++len; + } + else{ //rotate + NodeTraits::set_left(remainder, NodeTraits::get_right(tempptr)); + NodeTraits::set_right(tempptr, remainder); + remainder = tempptr; + NodeTraits::set_right(vine_tail, tempptr); + } + } + size = len; + } + + static void compress_subtree(node_ptr scanner, std::size_t count) + { + while(count--){ //compress "count" spine nodes in the tree with pseudo-root scanner + node_ptr child = NodeTraits::get_right(scanner); + node_ptr child_right = NodeTraits::get_right(child); + NodeTraits::set_right(scanner, child_right); + //Avoid setting the parent of child_right + scanner = child_right; + node_ptr scanner_left = NodeTraits::get_left(scanner); + NodeTraits::set_right(child, scanner_left); + if(scanner_left) + NodeTraits::set_parent(scanner_left, child); + NodeTraits::set_left(scanner, child); + NodeTraits::set_parent(child, scanner); + } + } + + static void vine_to_subtree(const node_ptr & super_root, std::size_t count) + { + const std::size_t one_szt = 1u; + std::size_t leaf_nodes = count + one_szt - std::size_t(one_szt << detail::floor_log2(count + one_szt)); + compress_subtree(super_root, leaf_nodes); //create deepest leaves + std::size_t vine_nodes = count - leaf_nodes; + while(vine_nodes > 1){ + vine_nodes /= 2; + compress_subtree(super_root, vine_nodes); + } + + //Update parents of nodes still in the in the original vine line + //as those have not been updated by subtree_to_vine or compress_subtree + for ( node_ptr q = super_root, p = NodeTraits::get_right(super_root) + ; p + ; q = p, p = NodeTraits::get_right(p)){ + NodeTraits::set_parent(p, q); + } + } + + //! Requires: "n" must be a node inserted in a tree. + //! + //! Effects: Returns a pointer to the header node of the tree. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + static node_ptr get_root(const node_ptr & node) + { + BOOST_INTRUSIVE_INVARIANT_ASSERT((!inited(node))); + node_ptr x = NodeTraits::get_parent(node); + if(x){ + while(!is_header(x)){ + x = NodeTraits::get_parent(x); + } + return x; + } + else{ + return node; + } + } + + template + static node_ptr clone_subtree + (const const_node_ptr &source_parent, const node_ptr &target_parent + , Cloner cloner, Disposer disposer + , node_ptr &leftmost_out, node_ptr &rightmost_out + ) + { + node_ptr target_sub_root = target_parent; + node_ptr source_root = NodeTraits::get_parent(source_parent); + if(!source_root){ + leftmost_out = rightmost_out = source_root; + } + else{ + //We'll calculate leftmost and rightmost nodes while iterating + node_ptr current = source_root; + node_ptr insertion_point = target_sub_root = cloner(current); + + //We'll calculate leftmost and rightmost nodes while iterating + node_ptr leftmost = target_sub_root; + node_ptr rightmost = target_sub_root; + + //First set the subroot + NodeTraits::set_left(target_sub_root, node_ptr()); + NodeTraits::set_right(target_sub_root, node_ptr()); + NodeTraits::set_parent(target_sub_root, target_parent); + + dispose_subtree_disposer rollback(disposer, target_sub_root); + while(true) { + //First clone left nodes + if( NodeTraits::get_left(current) && + !NodeTraits::get_left(insertion_point)) { + current = NodeTraits::get_left(current); + node_ptr temp = insertion_point; + //Clone and mark as leaf + insertion_point = cloner(current); + NodeTraits::set_left (insertion_point, node_ptr()); + NodeTraits::set_right (insertion_point, node_ptr()); + //Insert left + NodeTraits::set_parent(insertion_point, temp); + NodeTraits::set_left (temp, insertion_point); + //Update leftmost + if(rightmost == target_sub_root) + leftmost = insertion_point; + } + //Then clone right nodes + else if( NodeTraits::get_right(current) && + !NodeTraits::get_right(insertion_point)){ + current = NodeTraits::get_right(current); + node_ptr temp = insertion_point; + //Clone and mark as leaf + insertion_point = cloner(current); + NodeTraits::set_left (insertion_point, node_ptr()); + NodeTraits::set_right (insertion_point, node_ptr()); + //Insert right + NodeTraits::set_parent(insertion_point, temp); + NodeTraits::set_right (temp, insertion_point); + //Update rightmost + rightmost = insertion_point; + } + //If not, go up + else if(current == source_root){ + break; + } + else{ + //Branch completed, go up searching more nodes to clone + current = NodeTraits::get_parent(current); + insertion_point = NodeTraits::get_parent(insertion_point); + } + } + rollback.release(); + leftmost_out = leftmost; + rightmost_out = rightmost; + } + return target_sub_root; + } + + template + static void dispose_subtree(node_ptr x, Disposer disposer) + { + while (x){ + node_ptr save(NodeTraits::get_left(x)); + if (save) { + // Right rotation + NodeTraits::set_left(x, NodeTraits::get_right(save)); + NodeTraits::set_right(save, x); + } + else { + save = NodeTraits::get_right(x); + init(x); + disposer(x); + } + x = save; + } + } + + template + static node_ptr lower_bound_loop + (node_ptr x, node_ptr y, const KeyType &key, KeyNodePtrCompare comp) + { + while(x){ + if(comp(x, key)){ + x = NodeTraits::get_right(x); + } + else{ + y = x; + x = NodeTraits::get_left(x); + } + } + return y; + } + + template + static node_ptr upper_bound_loop + (node_ptr x, node_ptr y, const KeyType &key, KeyNodePtrCompare comp) + { + while(x){ + if(comp(key, x)){ + y = x; + x = NodeTraits::get_left(x); + } + else{ + x = NodeTraits::get_right(x); + } + } + return y; + } + + template + static void check_subtree(const const_node_ptr& node, Checker checker, typename Checker::return_type& check_return) + { + const_node_ptr left = NodeTraits::get_left(node); + const_node_ptr right = NodeTraits::get_right(node); + typename Checker::return_type check_return_left; + typename Checker::return_type check_return_right; + if (left) + { + BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_parent(left) == node); + check_subtree(left, checker, check_return_left); + } + if (right) + { + BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_parent(right) == node); + check_subtree(right, checker, check_return_right); + } + checker(node, check_return_left, check_return_right, check_return); + } +}; + +/// @cond + +template +struct get_algo +{ + typedef bstree_algorithms type; +}; + +template +struct get_node_checker +{ + typedef detail::bstree_node_checker type; +}; + +/// @endcond + +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_BSTREE_ALGORITHMS_HPP diff --git a/boost/intrusive/circular_list_algorithms.hpp b/boost/intrusive/circular_list_algorithms.hpp index 282f4741a1..1e888a1cae 100644 --- a/boost/intrusive/circular_list_algorithms.hpp +++ b/boost/intrusive/circular_list_algorithms.hpp @@ -1,7 +1,7 @@ ///////////////////////////////////////////////////////////////////////////// // // (C) Copyright Olaf Krzikalla 2004-2006. -// (C) Copyright Ion Gaztanaga 2006-2012 +// (C) Copyright Ion Gaztanaga 2006-2014 // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at @@ -14,8 +14,14 @@ #ifndef BOOST_INTRUSIVE_CIRCULAR_LIST_ALGORITHMS_HPP #define BOOST_INTRUSIVE_CIRCULAR_LIST_ALGORITHMS_HPP +#if defined(_MSC_VER) +# pragma once +#endif + #include #include +#include +#include #include namespace boost { @@ -63,8 +69,9 @@ class circular_list_algorithms //! Throws: Nothing. static void init(const node_ptr &this_node) { - NodeTraits::set_next(this_node, node_ptr()); - NodeTraits::set_previous(this_node, node_ptr()); + const node_ptr null_node((node_ptr())); + NodeTraits::set_next(this_node, null_node); + NodeTraits::set_previous(this_node, null_node); } //! Effects: Returns true is "this_node" is in a non-used state @@ -134,15 +141,10 @@ class circular_list_algorithms static node_ptr unlink(const node_ptr &this_node) { node_ptr next(NodeTraits::get_next(this_node)); - if(next){ - node_ptr prev(NodeTraits::get_previous(this_node)); - NodeTraits::set_next(prev, next); - NodeTraits::set_previous(next, prev); - return next; - } - else{ - return this_node; - } + node_ptr prev(NodeTraits::get_previous(this_node)); + NodeTraits::set_next(prev, next); + NodeTraits::set_previous(next, prev); + return next; } //! Requires: b and e must be nodes of the same circular list or an empty range. @@ -174,7 +176,7 @@ class circular_list_algorithms NodeTraits::set_previous(this_node, prev); NodeTraits::set_next(this_node, nxt_node); //nxt_node might be an alias for prev->next_ - //so use it before update it before NodeTraits::set_next(prev, ...) + //so use it before NodeTraits::set_next(prev, ...) //is called and the reference changes it's value NodeTraits::set_previous(nxt_node, this_node); NodeTraits::set_next(prev, this_node); @@ -403,8 +405,100 @@ class circular_list_algorithms } link_after(last, p); } + + //! Requires: f and l must be in a circular list. + //! + //! Effects: Returns the number of nodes in the range [f, l). + //! + //! Complexity: Linear + //! + //! Throws: Nothing. + static std::size_t distance(const const_node_ptr &f, const const_node_ptr &l) + { + const_node_ptr i(f); + std::size_t result = 0; + while(i != l){ + i = NodeTraits::get_next(i); + ++result; + } + return result; + } + + struct stable_partition_info + { + std::size_t num_1st_partition; + std::size_t num_2nd_partition; + node_ptr beg_2st_partition; + }; + + template + static void stable_partition(node_ptr beg, const node_ptr &end, Pred pred, stable_partition_info &info) + { + node_ptr bcur = node_traits::get_previous(beg); + node_ptr cur = beg; + node_ptr new_f = end; + + std::size_t num1 = 0, num2 = 0; + while(cur != end){ + if(pred(cur)){ + ++num1; + bcur = cur; + cur = node_traits::get_next(cur); + } + else{ + ++num2; + node_ptr last_to_remove = bcur; + new_f = cur; + bcur = cur; + cur = node_traits::get_next(cur); + BOOST_TRY{ + //Main loop + while(cur != end){ + if(pred(cur)){ //Might throw + ++num1; + //Process current node + node_traits::set_next (last_to_remove, cur); + node_traits::set_previous(cur, last_to_remove); + last_to_remove = cur; + node_ptr nxt = node_traits::get_next(cur); + node_traits::set_next (bcur, nxt); + node_traits::set_previous(nxt, bcur); + cur = nxt; + } + else{ + ++num2; + bcur = cur; + cur = node_traits::get_next(cur); + } + } + } + BOOST_CATCH(...){ + node_traits::set_next (last_to_remove, new_f); + node_traits::set_previous(new_f, last_to_remove); + throw; + } + BOOST_CATCH_END + node_traits::set_next(last_to_remove, new_f); + node_traits::set_previous(new_f, last_to_remove); + break; + } + } + info.num_1st_partition = num1; + info.num_2nd_partition = num2; + info.beg_2st_partition = new_f; + } }; +/// @cond + +template +struct get_algo +{ + typedef circular_list_algorithms type; +}; + +/// @endcond + } //namespace intrusive } //namespace boost diff --git a/boost/intrusive/circular_slist_algorithms.hpp b/boost/intrusive/circular_slist_algorithms.hpp index c39b3d0c5a..3dd561df50 100644 --- a/boost/intrusive/circular_slist_algorithms.hpp +++ b/boost/intrusive/circular_slist_algorithms.hpp @@ -1,7 +1,7 @@ ///////////////////////////////////////////////////////////////////////////// // // (C) Copyright Olaf Krzikalla 2004-2006. -// (C) Copyright Ion Gaztanaga 2006-2012 +// (C) Copyright Ion Gaztanaga 2006-2014 // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at @@ -14,11 +14,16 @@ #ifndef BOOST_INTRUSIVE_CIRCULAR_SLIST_ALGORITHMS_HPP #define BOOST_INTRUSIVE_CIRCULAR_SLIST_ALGORITHMS_HPP +#if defined(_MSC_VER) +# pragma once +#endif + +#include #include #include #include -#include -#include +#include + namespace boost { namespace intrusive { @@ -172,18 +177,17 @@ class circular_slist_algorithms static node_ptr get_previous_previous_node(const node_ptr & this_node) { return get_previous_previous_node(this_node, this_node); } - //! Requires: this_node and prev_prev_init_node must be in the same circular list. + //! Requires: this_node and p must be in the same circular list. //! //! Effects: Returns the previous node of the previous node of this_node in the - //! circular list starting. the search from prev_init_node. The first node checked - //! for equality is NodeTraits::get_next((NodeTraits::get_next(prev_prev_init_node)). + //! circular list starting. the search from p. The first node checked + //! for equality is NodeTraits::get_next((NodeTraits::get_next(p)). //! //! Complexity: Linear to the number of elements in the circular list. //! //! Throws: Nothing. - static node_ptr get_previous_previous_node(const node_ptr & prev_prev_init_node, const node_ptr & this_node) + static node_ptr get_previous_previous_node(node_ptr p, const node_ptr & this_node) { - node_ptr p = prev_prev_init_node; node_ptr p_next = NodeTraits::get_next(p); node_ptr p_next_next = NodeTraits::get_next(p_next); while (this_node != p_next_next){ @@ -250,33 +254,21 @@ class circular_slist_algorithms { if (other_node == this_node) return; - bool this_inited = base_t::inited(this_node); - bool other_inited = base_t::inited(other_node); - if(this_inited){ - base_t::init_header(this_node); + const node_ptr this_next = NodeTraits::get_next(this_node); + const node_ptr other_next = NodeTraits::get_next(other_node); + const bool this_null = !this_next; + const bool other_null = !other_next; + const bool this_empty = this_next == this_node; + const bool other_empty = other_next == other_node; + + if(!(other_null || other_empty)){ + NodeTraits::set_next(this_next == other_node ? other_node : get_previous_node(other_node), this_node ); } - if(other_inited){ - base_t::init_header(other_node); - } - - bool empty1 = base_t::unique(this_node); - bool empty2 = base_t::unique(other_node); - node_ptr prev_this (get_previous_node(this_node)); - node_ptr prev_other(get_previous_node(other_node)); - - node_ptr this_next (NodeTraits::get_next(this_node)); - node_ptr other_next(NodeTraits::get_next(other_node)); - NodeTraits::set_next(this_node, other_next); - NodeTraits::set_next(other_node, this_next); - NodeTraits::set_next(empty1 ? other_node : prev_this, other_node); - NodeTraits::set_next(empty2 ? this_node : prev_other, this_node); - - if(this_inited){ - base_t::init(other_node); - } - if(other_inited){ - base_t::init(this_node); + if(!(this_null | this_empty)){ + NodeTraits::set_next(other_next == this_node ? this_node : get_previous_node(this_node), other_node ); } + NodeTraits::set_next(this_node, other_empty ? this_node : (other_next == this_node ? other_node : other_next) ); + NodeTraits::set_next(other_node, this_empty ? other_node : (this_next == other_node ? this_node : this_next ) ); } //! Effects: Reverses the order of elements in the list. @@ -397,6 +389,16 @@ class circular_slist_algorithms } }; +/// @cond + +template +struct get_algo +{ + typedef circular_slist_algorithms type; +}; + +/// @endcond + } //namespace intrusive } //namespace boost diff --git a/boost/intrusive/derivation_value_traits.hpp b/boost/intrusive/derivation_value_traits.hpp index 0aae01b619..87bc489cf5 100644 --- a/boost/intrusive/derivation_value_traits.hpp +++ b/boost/intrusive/derivation_value_traits.hpp @@ -1,6 +1,6 @@ ///////////////////////////////////////////////////////////////////////////// // -// (C) Copyright Ion Gaztanaga 2006-2012 +// (C) Copyright Ion Gaztanaga 2006-2013 // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at @@ -13,10 +13,14 @@ #ifndef BOOST_INTRUSIVE_DERIVATION_VALUE_TRAITS_HPP #define BOOST_INTRUSIVE_DERIVATION_VALUE_TRAITS_HPP +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include #include -#include -#include -#include +#include namespace boost { namespace intrusive { @@ -24,7 +28,12 @@ namespace intrusive { //!This value traits template is used to create value traits //!from user defined node traits where value_traits::value_type will //!derive from node_traits::node -template + +template struct derivation_value_traits { public: @@ -33,8 +42,10 @@ struct derivation_value_traits typedef typename node_traits::node node; typedef typename node_traits::node_ptr node_ptr; typedef typename node_traits::const_node_ptr const_node_ptr; - typedef typename boost::pointer_to_other::type pointer; - typedef typename boost::pointer_to_other::type const_pointer; + typedef typename pointer_traits:: + template rebind_pointer::type pointer; + typedef typename pointer_traits:: + template rebind_pointer::type const_pointer; typedef typename boost::intrusive:: pointer_traits::reference reference; typedef typename boost::intrusive:: @@ -49,22 +60,18 @@ struct derivation_value_traits static pointer to_value_ptr(const node_ptr &n) { -// This still fails in gcc < 4.4 so forget about it -// using ::boost::static_pointer_cast; -// return static_pointer_cast(n)); - return pointer(&static_cast(*n)); + return pointer_traits::pointer_to(static_cast(*n)); } static const_pointer to_value_ptr(const const_node_ptr &n) { -// This still fails in gcc < 4.4 so forget about it -// using ::boost::static_pointer_cast; -// return static_pointer_cast(n)); - return const_pointer(&static_cast(*n)); + return pointer_traits::pointer_to(static_cast(*n)); } }; } //namespace intrusive } //namespace boost +#include + #endif //BOOST_INTRUSIVE_DERIVATION_VALUE_TRAITS_HPP diff --git a/boost/intrusive/detail/algo_type.hpp b/boost/intrusive/detail/algo_type.hpp new file mode 100644 index 0000000000..e40cb43b4e --- /dev/null +++ b/boost/intrusive/detail/algo_type.hpp @@ -0,0 +1,46 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2014 +// +// 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_DETAIL_ALGO_TYPE_HPP +#define BOOST_INTRUSIVE_DETAIL_ALGO_TYPE_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +namespace boost { +namespace intrusive { + +enum algo_types +{ + CircularListAlgorithms, + CircularSListAlgorithms, + LinearSListAlgorithms, + CommonSListAlgorithms, + BsTreeAlgorithms, + RbTreeAlgorithms, + AvlTreeAlgorithms, + SgTreeAlgorithms, + SplayTreeAlgorithms, + TreapAlgorithms +}; + +template +struct get_algo; + +template +struct get_node_checker; + +} //namespace intrusive +} //namespace boost + +#endif //BOOST_INTRUSIVE_DETAIL_ALGO_TYPE_HPP diff --git a/boost/intrusive/detail/any_node_and_algorithms.hpp b/boost/intrusive/detail/any_node_and_algorithms.hpp index b274135a90..5a0e8c27fe 100644 --- a/boost/intrusive/detail/any_node_and_algorithms.hpp +++ b/boost/intrusive/detail/any_node_and_algorithms.hpp @@ -1,6 +1,6 @@ ///////////////////////////////////////////////////////////////////////////// // -// (C) Copyright Ion Gaztanaga 2006-2012 +// (C) Copyright Ion Gaztanaga 2006-2014 // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at @@ -13,13 +13,13 @@ #ifndef BOOST_INTRUSIVE_ANY_NODE_HPP #define BOOST_INTRUSIVE_ANY_NODE_HPP -#include -#include -#include -#include +#if defined(_MSC_VER) +# pragma once +#endif + +#include #include #include -#include namespace boost { namespace intrusive { @@ -27,8 +27,9 @@ namespace intrusive { template struct any_node { - typedef typename pointer_traits - ::template rebind_pointer::type node_ptr; + typedef any_node node; + typedef typename pointer_rebind::type node_ptr; + typedef typename pointer_rebind::type const_node_ptr; node_ptr node_ptr_1; node_ptr node_ptr_2; node_ptr node_ptr_3; @@ -38,11 +39,9 @@ struct any_node template struct any_list_node_traits { - typedef any_node node; - typedef typename pointer_traits - ::template rebind_pointer::type node_ptr; - typedef typename pointer_traits - ::template rebind_pointer::type const_node_ptr; + typedef any_node node; + typedef typename node::node_ptr node_ptr; + typedef typename node::const_node_ptr const_node_ptr; static const node_ptr &get_next(const const_node_ptr & n) { return n->node_ptr_1; } @@ -61,11 +60,9 @@ struct any_list_node_traits template struct any_slist_node_traits { - typedef any_node node; - typedef typename pointer_traits - ::template rebind_pointer::type node_ptr; - typedef typename pointer_traits - ::template rebind_pointer::type const_node_ptr; + typedef any_node node; + typedef typename node::node_ptr node_ptr; + typedef typename node::const_node_ptr const_node_ptr; static const node_ptr &get_next(const const_node_ptr & n) { return n->node_ptr_1; } @@ -110,12 +107,9 @@ struct any_unordered_node_traits template struct any_rbtree_node_traits { - typedef any_node node; - - typedef typename pointer_traits - ::template rebind_pointer::type node_ptr; - typedef typename pointer_traits - ::template rebind_pointer::type const_node_ptr; + typedef any_node node; + typedef typename node::node_ptr node_ptr; + typedef typename node::const_node_ptr const_node_ptr; typedef std::size_t color; @@ -154,12 +148,10 @@ struct any_rbtree_node_traits template struct any_avltree_node_traits { - typedef any_node node; + typedef any_node node; + typedef typename node::node_ptr node_ptr; + typedef typename node::const_node_ptr const_node_ptr; - typedef typename pointer_traits - ::template rebind_pointer::type node_ptr; - typedef typename pointer_traits - ::template rebind_pointer::type const_node_ptr; typedef std::size_t balance; static const node_ptr &get_parent(const const_node_ptr & n) @@ -200,12 +192,9 @@ struct any_avltree_node_traits template struct any_tree_node_traits { - typedef any_node node; - - typedef typename pointer_traits - ::template rebind_pointer::type node_ptr; - typedef typename pointer_traits - ::template rebind_pointer::type const_node_ptr; + typedef any_node node; + typedef typename node::node_ptr node_ptr; + typedef typename node::const_node_ptr const_node_ptr; static const node_ptr &get_parent(const const_node_ptr & n) { return n->node_ptr_1; } @@ -231,10 +220,8 @@ class any_node_traits { public: typedef any_node node; - typedef typename pointer_traits - ::template rebind_pointer::type node_ptr; - typedef typename pointer_traits - ::template rebind_pointer::type const_node_ptr; + typedef typename node::node_ptr node_ptr; + typedef typename node::const_node_ptr const_node_ptr; }; template @@ -245,12 +232,10 @@ class any_algorithms {} public: - typedef any_node node; - typedef typename pointer_traits - ::template rebind_pointer::type node_ptr; - typedef typename pointer_traits - ::template rebind_pointer::type const_node_ptr; - typedef any_node_traits node_traits; + typedef any_node node; + typedef typename node::node_ptr node_ptr; + typedef typename node::const_node_ptr const_node_ptr; + typedef any_node_traits node_traits; //! Requires: node must not be part of any tree. //! @@ -281,7 +266,7 @@ class any_algorithms any_algorithms::template function_not_available_for_any_hooks(); } - static void swap_nodes(const node_ptr & l, const node_ptr & r) + static void swap_nodes(const node_ptr &, const node_ptr &) { //Any nodes have no swap_nodes capability because they don't know //what algorithm they must use to unlink the node from the container @@ -292,6 +277,4 @@ class any_algorithms } //namespace intrusive } //namespace boost -#include - #endif //BOOST_INTRUSIVE_ANY_NODE_HPP diff --git a/boost/intrusive/detail/array_initializer.hpp b/boost/intrusive/detail/array_initializer.hpp new file mode 100644 index 0000000000..b2072a872d --- /dev/null +++ b/boost/intrusive/detail/array_initializer.hpp @@ -0,0 +1,90 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2014 +// +// 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_DETAIL_ARRAY_INITIALIZER_HPP +#define BOOST_INTRUSIVE_DETAIL_ARRAY_INITIALIZER_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include + +namespace boost { +namespace intrusive { +namespace detail { + +//This is not standard, but should work with all compilers +union max_align +{ + char char_; + short short_; + int int_; + long long_; + #ifdef BOOST_HAS_LONG_LONG + long long long_long_; + #endif + float float_; + double double_; + long double long_double_; + void * void_ptr_; +}; + +template +class array_initializer +{ + public: + template + array_initializer(const CommonInitializer &init) + { + char *init_buf = (char*)rawbuf; + std::size_t i = 0; + BOOST_TRY{ + for(; i != N; ++i){ + new(init_buf)T(init); + init_buf += sizeof(T); + } + } + BOOST_CATCH(...){ + while(i--){ + init_buf -= sizeof(T); + ((T*)init_buf)->~T(); + } + BOOST_RETHROW; + } + BOOST_CATCH_END + } + + operator T* () + { return (T*)(rawbuf); } + + operator const T*() const + { return (const T*)(rawbuf); } + + ~array_initializer() + { + char *init_buf = (char*)rawbuf + N*sizeof(T); + for(std::size_t i = 0; i != N; ++i){ + init_buf -= sizeof(T); + ((T*)init_buf)->~T(); + } + } + + private: + detail::max_align rawbuf[(N*sizeof(T)-1)/sizeof(detail::max_align)+1]; +}; + +} //namespace detail{ +} //namespace intrusive{ +} //namespace boost{ + +#endif //BOOST_INTRUSIVE_DETAIL_ARRAY_INITIALIZER_HPP diff --git a/boost/intrusive/detail/assert.hpp b/boost/intrusive/detail/assert.hpp index 33de97f701..d75d225ac4 100644 --- a/boost/intrusive/detail/assert.hpp +++ b/boost/intrusive/detail/assert.hpp @@ -1,6 +1,6 @@ ///////////////////////////////////////////////////////////////////////////// // -// (C) Copyright Ion Gaztanaga 2006-2012 +// (C) Copyright Ion Gaztanaga 2006-2013 // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at @@ -13,7 +13,7 @@ #ifndef BOOST_INTRUSIVE_DETAIL_ASSERT_HPP #define BOOST_INTRUSIVE_DETAIL_ASSERT_HPP -#if defined(_MSC_VER)&&(_MSC_VER>=1200) +#if defined(_MSC_VER) #pragma once #endif diff --git a/boost/intrusive/detail/avltree_node.hpp b/boost/intrusive/detail/avltree_node.hpp index aec0dabd4b..522806a89e 100644 --- a/boost/intrusive/detail/avltree_node.hpp +++ b/boost/intrusive/detail/avltree_node.hpp @@ -1,6 +1,6 @@ ///////////////////////////////////////////////////////////////////////////// // -// (C) Copyright Ion Gaztanaga 2007-2012 +// (C) Copyright Ion Gaztanaga 2007-2013 // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at @@ -13,9 +13,12 @@ #ifndef BOOST_INTRUSIVE_AVLTREE_NODE_HPP #define BOOST_INTRUSIVE_AVLTREE_NODE_HPP +#if defined(_MSC_VER) +# pragma once +#endif + #include -#include -#include +#include #include #include #include @@ -33,9 +36,8 @@ namespace intrusive { template struct compact_avltree_node { - typedef typename pointer_traits - ::template rebind_pointer - >::type node_ptr; + typedef typename pointer_rebind >::type node_ptr; + typedef typename pointer_rebind >::type const_node_ptr; enum balance { negative_t, zero_t, positive_t }; node_ptr parent_, left_, right_; }; @@ -44,9 +46,8 @@ struct compact_avltree_node template struct avltree_node { - typedef typename pointer_traits - ::template rebind_pointer - >::type node_ptr; + typedef typename pointer_rebind >::type node_ptr; + typedef typename pointer_rebind >::type const_node_ptr; enum balance { negative_t, zero_t, positive_t }; node_ptr parent_, left_, right_; balance balance_; @@ -58,29 +59,33 @@ template struct default_avltree_node_traits_impl { typedef avltree_node node; - - typedef typename pointer_traits - ::template rebind_pointer - ::type node_ptr; - typedef typename pointer_traits - ::template rebind_pointer - ::type const_node_ptr; + typedef typename node::node_ptr node_ptr; + typedef typename node::const_node_ptr const_node_ptr; typedef typename node::balance balance; - static const node_ptr & get_parent(const const_node_ptr & n) + static node_ptr get_parent(const const_node_ptr & n) + { return n->parent_; } + + static node_ptr get_parent(const node_ptr & n) { return n->parent_; } static void set_parent(const node_ptr & n, const node_ptr & p) { n->parent_ = p; } - static const node_ptr & get_left(const const_node_ptr & n) + static node_ptr get_left(const const_node_ptr & n) + { return n->left_; } + + static node_ptr get_left(const node_ptr & n) { return n->left_; } static void set_left(const node_ptr & n, const node_ptr & l) { n->left_ = l; } - static const node_ptr & get_right(const const_node_ptr & n) + static node_ptr get_right(const const_node_ptr & n) + { return n->right_; } + + static node_ptr get_right(const node_ptr & n) { return n->right_; } static void set_right(const node_ptr & n, const node_ptr & r) @@ -89,6 +94,9 @@ struct default_avltree_node_traits_impl static balance get_balance(const const_node_ptr & n) { return n->balance_; } + static balance get_balance(const node_ptr & n) + { return n->balance_; } + static void set_balance(const node_ptr & n, balance b) { n->balance_ = b; } @@ -108,13 +116,8 @@ template struct compact_avltree_node_traits_impl { typedef compact_avltree_node node; - - typedef typename pointer_traits - ::template rebind_pointer - ::type node_ptr; - typedef typename pointer_traits - ::template rebind_pointer - ::type const_node_ptr; + typedef typename node::node_ptr node_ptr; + typedef typename node::const_node_ptr const_node_ptr; typedef typename node::balance balance; typedef pointer_plus_bits ptr_bit; @@ -125,13 +128,13 @@ struct compact_avltree_node_traits_impl static void set_parent(const node_ptr & n, const node_ptr & p) { ptr_bit::set_pointer(n->parent_, p); } - static const node_ptr & get_left(const const_node_ptr & n) + static node_ptr get_left(const const_node_ptr & n) { return n->left_; } static void set_left(const node_ptr & n, const node_ptr & l) { n->left_ = l; } - static const node_ptr & get_right(const const_node_ptr & n) + static node_ptr get_right(const const_node_ptr & n) { return n->right_; } static void set_right(const node_ptr & n, const node_ptr & r) @@ -164,7 +167,7 @@ struct avltree_node_traits_dispatch : public compact_avltree_node_traits_impl {}; -//Inherit from the detail::link_dispatch depending on the embedding capabilities +//Inherit from rbtree_node_traits_dispatch depending on the embedding capabilities template struct avltree_node_traits : public avltree_node_traits_dispatch diff --git a/boost/intrusive/detail/clear_on_destructor_base.hpp b/boost/intrusive/detail/clear_on_destructor_base.hpp deleted file mode 100644 index 1b5c27fff4..0000000000 --- a/boost/intrusive/detail/clear_on_destructor_base.hpp +++ /dev/null @@ -1,36 +0,0 @@ -//////} // /////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2008-2012. 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_DETAIL_CLEAR_ON_DESTRUCTOR_HPP -#define BOOST_INTRUSIVE_DETAIL_CLEAR_ON_DESTRUCTOR_HPP - -#include - -namespace boost { -namespace intrusive { -namespace detail { - -template -class clear_on_destructor_base -{ - protected: - ~clear_on_destructor_base() - { - static_cast(this)->clear(); - } -}; - -} // namespace detail { -} // namespace intrusive { -} // namespace boost { - -#include - -#endif //#ifndef BOOST_INTRUSIVE_DETAIL_CLEAR_ON_DESTRUCTOR_HPP diff --git a/boost/intrusive/detail/common_slist_algorithms.hpp b/boost/intrusive/detail/common_slist_algorithms.hpp index 942b35a3f4..4c7f1a11e0 100644 --- a/boost/intrusive/detail/common_slist_algorithms.hpp +++ b/boost/intrusive/detail/common_slist_algorithms.hpp @@ -1,6 +1,6 @@ ///////////////////////////////////////////////////////////////////////////// // -// (C) Copyright Ion Gaztanaga 2007-2012 +// (C) Copyright Ion Gaztanaga 2007-2014 // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at @@ -13,9 +13,14 @@ #ifndef BOOST_INTRUSIVE_COMMON_SLIST_ALGORITHMS_HPP #define BOOST_INTRUSIVE_COMMON_SLIST_ALGORITHMS_HPP -#include +#if defined(_MSC_VER) +# pragma once +#endif + #include #include +#include +#include #include namespace boost { @@ -31,9 +36,8 @@ class common_slist_algorithms typedef typename NodeTraits::const_node_ptr const_node_ptr; typedef NodeTraits node_traits; - static node_ptr get_previous_node(const node_ptr & prev_init_node, const node_ptr & this_node) + static node_ptr get_previous_node(node_ptr p, const node_ptr & this_node) { - node_ptr p = prev_init_node; for( node_ptr p_next ; this_node != (p_next = NodeTraits::get_next(p)) ; p = p_next){ @@ -44,9 +48,6 @@ class common_slist_algorithms return p; } - static void init_header(const node_ptr & this_node) - { NodeTraits::set_next(this_node, this_node); } - static void init(const node_ptr & this_node) { NodeTraits::set_next(this_node, node_ptr()); } @@ -92,12 +93,102 @@ class common_slist_algorithms NodeTraits::set_next(bp, next_b); } } + + struct stable_partition_info + { + std::size_t num_1st_partition; + std::size_t num_2nd_partition; + node_ptr beg_2st_partition; + node_ptr new_last_node; + }; + + template + static void stable_partition(node_ptr before_beg, const node_ptr &end, Pred pred, stable_partition_info &info) + { + node_ptr bcur = before_beg; + node_ptr cur = node_traits::get_next(bcur); + node_ptr new_f = end; + + std::size_t num1 = 0, num2 = 0; + while(cur != end){ + if(pred(cur)){ + ++num1; + bcur = cur; + cur = node_traits::get_next(cur); + } + else{ + ++num2; + node_ptr last_to_remove = bcur; + new_f = cur; + bcur = cur; + cur = node_traits::get_next(cur); + BOOST_TRY{ + //Main loop + while(cur != end){ + if(pred(cur)){ //Might throw + ++num1; + //Process current node + node_traits::set_next(last_to_remove, cur); + last_to_remove = cur; + node_ptr nxt = node_traits::get_next(cur); + node_traits::set_next(bcur, nxt); + cur = nxt; + } + else{ + ++num2; + bcur = cur; + cur = node_traits::get_next(cur); + } + } + } + BOOST_CATCH(...){ + node_traits::set_next(last_to_remove, new_f); + throw; + } + BOOST_CATCH_END + node_traits::set_next(last_to_remove, new_f); + break; + } + } + info.num_1st_partition = num1; + info.num_2nd_partition = num2; + info.beg_2st_partition = new_f; + info.new_last_node = bcur; + } + + //! Requires: f and l must be in a circular list. + //! + //! Effects: Returns the number of nodes in the range [f, l). + //! + //! Complexity: Linear + //! + //! Throws: Nothing. + static std::size_t distance(const const_node_ptr &f, const const_node_ptr &l) + { + const_node_ptr i(f); + std::size_t result = 0; + while(i != l){ + i = NodeTraits::get_next(i); + ++result; + } + return result; + } }; +/// @endcond + } //namespace detail + +/// @cond + +template +struct get_algo +{ + typedef detail::common_slist_algorithms type; +}; + + } //namespace intrusive } //namespace boost -#include - #endif //BOOST_INTRUSIVE_COMMON_SLIST_ALGORITHMS_HPP diff --git a/boost/intrusive/detail/config_begin.hpp b/boost/intrusive/detail/config_begin.hpp index 7d153368a5..36d605d072 100644 --- a/boost/intrusive/detail/config_begin.hpp +++ b/boost/intrusive/detail/config_begin.hpp @@ -1,6 +1,6 @@ ///////////////////////////////////////////////////////////////////////////// // -// (C) Copyright Ion Gaztanaga 2006-2012 +// (C) Copyright Ion Gaztanaga 2006-2013 // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at @@ -10,8 +10,7 @@ // ///////////////////////////////////////////////////////////////////////////// -#ifndef BOOST_INTRUSIVE_CONFIG_INCLUDED -#define BOOST_INTRUSIVE_CONFIG_INCLUDED +#ifndef BOOST_CONFIG_HPP #include #endif diff --git a/boost/intrusive/detail/config_end.hpp b/boost/intrusive/detail/config_end.hpp index d653030daa..a081443e69 100644 --- a/boost/intrusive/detail/config_end.hpp +++ b/boost/intrusive/detail/config_end.hpp @@ -1,6 +1,6 @@ ///////////////////////////////////////////////////////////////////////////// // -// (C) Copyright Ion Gaztanaga 2006-2012 +// (C) Copyright Ion Gaztanaga 2006-2013 // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at diff --git a/boost/intrusive/detail/default_header_holder.hpp b/boost/intrusive/detail/default_header_holder.hpp new file mode 100644 index 0000000000..c471691640 --- /dev/null +++ b/boost/intrusive/detail/default_header_holder.hpp @@ -0,0 +1,65 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2014 +// +// 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_DETAIL_DEFAULT_HEADER_HOLDER_HPP +#define BOOST_INTRUSIVE_DETAIL_DEFAULT_HEADER_HOLDER_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +namespace boost { +namespace intrusive { +namespace detail { + +// trivial header node holder +template < typename NodeTraits > +struct default_header_holder : public NodeTraits::node +{ + typedef NodeTraits node_traits; + typedef typename node_traits::node node; + typedef typename node_traits::node_ptr node_ptr; + typedef typename node_traits::const_node_ptr const_node_ptr; + + default_header_holder() : node() {} + + const_node_ptr get_node() const + { return pointer_traits< const_node_ptr >::pointer_to(*static_cast< const node* >(this)); } + + node_ptr get_node() + { return pointer_traits< node_ptr >::pointer_to(*static_cast< node* >(this)); } + + // (unsafe) downcast used to implement container-from-iterator + static default_header_holder* get_holder(const node_ptr &p) + { return static_cast< default_header_holder* >(boost::intrusive::detail::to_raw_pointer(p)); } +}; + +// type function producing the header node holder +template < typename Value_Traits, typename HeaderHolder > +struct get_header_holder_type +{ + typedef HeaderHolder type; +}; +template < typename Value_Traits > +struct get_header_holder_type< Value_Traits, void > +{ + typedef default_header_holder< typename Value_Traits::node_traits > type; +}; + +} //namespace detail +} //namespace intrusive +} //namespace boost + +#endif //BOOST_INTRUSIVE_DETAIL_DEFAULT_HEADER_HOLDER_HPP diff --git a/boost/intrusive/detail/ebo_functor_holder.hpp b/boost/intrusive/detail/ebo_functor_holder.hpp index 850d074326..d2e8107b67 100644 --- a/boost/intrusive/detail/ebo_functor_holder.hpp +++ b/boost/intrusive/detail/ebo_functor_holder.hpp @@ -1,6 +1,7 @@ ///////////////////////////////////////////////////////////////////////////// // -// (C) Copyright Joaquin M Lopez Munoz 2006-2012 +// (C) Copyright Joaquin M Lopez Munoz 2006-2013 +// (C) Copyright Ion Gaztanaga 2014-2014 // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at @@ -13,13 +14,145 @@ #ifndef BOOST_INTRUSIVE_DETAIL_EBO_HOLDER_HPP #define BOOST_INTRUSIVE_DETAIL_EBO_HOLDER_HPP -#include -#include +#if defined(_MSC_VER) +# pragma once +#endif + +#include namespace boost { namespace intrusive { namespace detail { +#if defined(BOOST_MSVC) || defined(__BORLANDC_) +#define BOOST_INTRUSIVE_TT_DECL __cdecl +#else +#define BOOST_INTRUSIVE_TT_DECL +#endif + +#if defined(_MSC_EXTENSIONS) && !defined(__BORLAND__) && !defined(_WIN64) && !defined(_M_ARM) && !defined(UNDER_CE) +#define BOOST_INTRUSIVE_TT_TEST_MSC_FUNC_SIGS +#endif + +template +struct is_unary_or_binary_function_impl +{ static const bool value = false; }; + +// see boost ticket #4094 +// avoid duplicate definitions of is_unary_or_binary_function_impl +#ifndef BOOST_INTRUSIVE_TT_TEST_MSC_FUNC_SIGS + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +#else // BOOST_INTRUSIVE_TT_TEST_MSC_FUNC_SIGS + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +#ifndef _MANAGED + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +#endif + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +#endif + +// see boost ticket #4094 +// avoid duplicate definitions of is_unary_or_binary_function_impl +#ifndef BOOST_INTRUSIVE_TT_TEST_MSC_FUNC_SIGS + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +#else // BOOST_INTRUSIVE_TT_TEST_MSC_FUNC_SIGS + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +#ifndef _MANAGED + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +#endif + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +#endif + +// see boost ticket #4094 +// avoid duplicate definitions of is_unary_or_binary_function_impl +#ifndef BOOST_INTRUSIVE_TT_TEST_MSC_FUNC_SIGS + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +#else // BOOST_INTRUSIVE_TT_TEST_MSC_FUNC_SIGS + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +#ifndef _MANAGED + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +#endif + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; +#endif + +template +struct is_unary_or_binary_function_impl +{ static const bool value = false; }; + +template +struct is_unary_or_binary_function : is_unary_or_binary_function_impl +{}; + template class ebo_functor_holder_impl { @@ -90,6 +223,4 @@ class ebo_functor_holder } //namespace intrusive { } //namespace boost { -#include - #endif //#ifndef BOOST_INTRUSIVE_DETAIL_EBO_HOLDER_HPP diff --git a/boost/intrusive/detail/empty_node_checker.hpp b/boost/intrusive/detail/empty_node_checker.hpp new file mode 100644 index 0000000000..33d78a34ba --- /dev/null +++ b/boost/intrusive/detail/empty_node_checker.hpp @@ -0,0 +1,40 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2014 +// +// 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_DETAIL_EMPTY_NODE_CHECKER_HPP +#define BOOST_INTRUSIVE_DETAIL_EMPTY_NODE_CHECKER_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +namespace boost { +namespace intrusive { +namespace detail { + +template +struct empty_node_checker +{ + typedef ValueTraits value_traits; + typedef typename value_traits::node_traits node_traits; + typedef typename node_traits::const_node_ptr const_node_ptr; + + struct return_type {}; + + void operator () (const const_node_ptr&, const return_type&, const return_type&, return_type&) {} +}; + +} //namespace detail{ +} //namespace intrusive{ +} //namespace boost{ + +#endif //BOOST_INTRUSIVE_DETAIL_EMPTY_NODE_CHECKER_HPP diff --git a/boost/intrusive/detail/equal_to_value.hpp b/boost/intrusive/detail/equal_to_value.hpp new file mode 100644 index 0000000000..3ffd1c26cf --- /dev/null +++ b/boost/intrusive/detail/equal_to_value.hpp @@ -0,0 +1,44 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2014 +// +// 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_DETAIL_EQUAL_TO_VALUE_HPP +#define BOOST_INTRUSIVE_DETAIL_EQUAL_TO_VALUE_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +namespace boost { +namespace intrusive { +namespace detail { + +//This functor compares a stored value +//and the one passed as an argument +template +class equal_to_value +{ + ConstReference t_; + + public: + equal_to_value(ConstReference t) + : t_(t) + {} + + bool operator()(ConstReference t)const + { return t_ == t; } +}; + +} //namespace detail{ +} //namespace intrusive{ +} //namespace boost{ + +#endif //BOOST_INTRUSIVE_DETAIL_EQUAL_TO_VALUE_HPP diff --git a/boost/intrusive/detail/exception_disposer.hpp b/boost/intrusive/detail/exception_disposer.hpp new file mode 100644 index 0000000000..1226f5e525 --- /dev/null +++ b/boost/intrusive/detail/exception_disposer.hpp @@ -0,0 +1,84 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2014 +// +// 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_DETAIL_EXCEPTION_DISPOSER_HPP +#define BOOST_INTRUSIVE_DETAIL_EXCEPTION_DISPOSER_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +namespace boost { +namespace intrusive { +namespace detail { + +template +class exception_disposer +{ + Container *cont_; + Disposer &disp_; + + exception_disposer(const exception_disposer&); + exception_disposer &operator=(const exception_disposer&); + + public: + exception_disposer(Container &cont, Disposer &disp) + : cont_(&cont), disp_(disp) + {} + + void release() + { cont_ = 0; } + + ~exception_disposer() + { + if(cont_){ + cont_->clear_and_dispose(disp_); + } + } +}; + +template +class exception_array_disposer +{ + Container *cont_; + Disposer &disp_; + SizeType &constructed_; + + exception_array_disposer(const exception_array_disposer&); + exception_array_disposer &operator=(const exception_array_disposer&); + + public: + + exception_array_disposer + (Container &cont, Disposer &disp, SizeType &constructed) + : cont_(&cont), disp_(disp), constructed_(constructed) + {} + + void release() + { cont_ = 0; } + + ~exception_array_disposer() + { + SizeType n = constructed_; + if(cont_){ + while(n--){ + cont_[n].clear_and_dispose(disp_); + } + } + } +}; + +} //namespace detail{ +} //namespace intrusive{ +} //namespace boost{ + +#endif //BOOST_INTRUSIVE_DETAIL_EXCEPTION_DISPOSER_HPP diff --git a/boost/intrusive/detail/function_detector.hpp b/boost/intrusive/detail/function_detector.hpp index 08cee2d561..f72865df9b 100644 --- a/boost/intrusive/detail/function_detector.hpp +++ b/boost/intrusive/detail/function_detector.hpp @@ -1,6 +1,6 @@ ///////////////////////////////////////////////////////////////////////////// // -// (C) Copyright Ion Gaztanaga 2009-2012. +// (C) Copyright Ion Gaztanaga 2009-2013. // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at @@ -22,7 +22,9 @@ #ifndef BOOST_INTRUSIVE_DETAIL_FUNCTION_DETECTOR_HPP #define BOOST_INTRUSIVE_DETAIL_FUNCTION_DETECTOR_HPP -#include +#if defined(_MSC_VER) +# pragma once +#endif namespace boost { namespace intrusive { @@ -83,6 +85,4 @@ namespace function_detector { ReturnType (*)Params \ >::check -#include - #endif //@ifndef BOOST_INTRUSIVE_DETAIL_FUNCTION_DETECTOR_HPP diff --git a/boost/intrusive/detail/generic_hook.hpp b/boost/intrusive/detail/generic_hook.hpp index 5ddd52074e..c5af081d65 100644 --- a/boost/intrusive/detail/generic_hook.hpp +++ b/boost/intrusive/detail/generic_hook.hpp @@ -1,6 +1,6 @@ ///////////////////////////////////////////////////////////////////////////// // -// (C) Copyright Ion Gaztanaga 2007-2012 +// (C) Copyright Ion Gaztanaga 2007-2013 // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at @@ -13,139 +13,147 @@ #ifndef BOOST_INTRUSIVE_GENERIC_HOOK_HPP #define BOOST_INTRUSIVE_GENERIC_HOOK_HPP -#include -#include +#if defined(_MSC_VER) +# pragma once +#endif + #include #include -#include #include -#include +#include +#include #include namespace boost { namespace intrusive { -namespace detail { /// @cond -enum -{ NoBaseHook -, ListBaseHook -, SlistBaseHook -, SetBaseHook -, UsetBaseHook -, SplaySetBaseHook -, AvlSetBaseHook -, BsSetBaseHook -, AnyBaseHook -}; - -struct no_default_definer{}; +namespace detail { -template -struct default_definer; +template +struct link_dispatch +{}; + +template +void destructor_impl(Hook &hook, detail::link_dispatch) +{ //If this assertion raises, you might have destroyed an object + //while it was still inserted in a container that is alive. + //If so, remove the object from the container before destroying it. + (void)hook; BOOST_INTRUSIVE_SAFE_HOOK_DESTRUCTOR_ASSERT(!hook.is_linked()); +} + +template +void destructor_impl(Hook &hook, detail::link_dispatch) +{ hook.unlink(); } + +template +void destructor_impl(Hook &, detail::link_dispatch) +{} + +} //namespace detail { + +enum base_hook_type +{ NoBaseHookId +, ListBaseHookId +, SlistBaseHookId +, RbTreeBaseHookId +, HashBaseHookId +, AvlTreeBaseHookId +, BsTreeBaseHookId +, TreapTreeBaseHookId +, AnyBaseHookId +}; -template -struct default_definer -{ typedef Hook default_list_hook; }; -template -struct default_definer -{ typedef Hook default_slist_hook; }; +template +struct hook_tags_definer{}; -template -struct default_definer -{ typedef Hook default_set_hook; }; +template +struct hook_tags_definer +{ typedef HookTags default_list_hook; }; -template -struct default_definer -{ typedef Hook default_uset_hook; }; +template +struct hook_tags_definer +{ typedef HookTags default_slist_hook; }; -template -struct default_definer -{ typedef Hook default_splay_set_hook; }; +template +struct hook_tags_definer +{ typedef HookTags default_rbtree_hook; }; -template -struct default_definer -{ typedef Hook default_avl_set_hook; }; +template +struct hook_tags_definer +{ typedef HookTags default_hashtable_hook; }; -template -struct default_definer -{ typedef Hook default_bs_set_hook; }; +template +struct hook_tags_definer +{ typedef HookTags default_avltree_hook; }; -template -struct default_definer -{ typedef Hook default_any_hook; }; +template +struct hook_tags_definer +{ typedef HookTags default_bstree_hook; }; -template -struct make_default_definer -{ - typedef typename detail::if_c - < BaseHookType != 0 - , default_definer - , no_default_definer>::type type; -}; +template +struct hook_tags_definer +{ typedef HookTags default_any_hook; }; template - < class GetNodeAlgorithms + < class NodeTraits , class Tag , link_mode_type LinkMode - , int HookType + , base_hook_type BaseHookType > -struct make_node_holder +struct hooktags_impl { - typedef typename detail::if_c - ::value - , detail::node_holder - < typename GetNodeAlgorithms::type::node - , Tag - , LinkMode - , HookType> - , typename GetNodeAlgorithms::type::node - >::type type; + static const link_mode_type link_mode = LinkMode; + typedef Tag tag; + typedef NodeTraits node_traits; + static const bool is_base_hook = !detail::is_same::value; + static const bool safemode_or_autounlink = is_safe_autounlink::value; + static const unsigned int type = BaseHookType; }; /// @endcond template - < class GetNodeAlgorithms + < class NodeAlgorithms , class Tag , link_mode_type LinkMode - , int HookType + , base_hook_type BaseHookType > class generic_hook /// @cond - - //If the hook is a base hook, derive generic hook from detail::node_holder + //If the hook is a base hook, derive generic hook from node_holder //so that a unique base class is created to convert from the node - //to the type. This mechanism will be used by base_hook_traits. + //to the type. This mechanism will be used by bhtraits. // //If the hook is a member hook, generic hook will directly derive //from the hook. - : public make_default_definer - < generic_hook - , detail::is_same::value*HookType + : public detail::if_c + < detail::is_same::value + , typename NodeAlgorithms::node + , node_holder >::type - , public make_node_holder::type + //If this is the a default-tagged base hook derive from a class that + //will define an special internal typedef. Containers will be able to detect this + //special typedef and obtain generic_hook's internal types in order to deduce + //value_traits for this hook. + , public hook_tags_definer + < generic_hook + , detail::is_same::value*BaseHookType> /// @endcond { /// @cond - typedef typename GetNodeAlgorithms::type node_algorithms; + typedef NodeAlgorithms node_algorithms; typedef typename node_algorithms::node node; typedef typename node_algorithms::node_ptr node_ptr; typedef typename node_algorithms::const_node_ptr const_node_ptr; public: - struct boost_intrusive_tags - { - static const int hook_type = HookType; - static const link_mode_type link_mode = LinkMode; - typedef Tag tag; - typedef typename GetNodeAlgorithms::type::node_traits node_traits; - static const bool is_base_hook = !detail::is_same::value; - static const bool safemode_or_autounlink = - (int)link_mode == (int)auto_unlink || (int)link_mode == (int)safe_link; - }; + + typedef hooktags_impl + < typename NodeAlgorithms::node_traits + , Tag, LinkMode, BaseHookType> hooktags; node_ptr this_ptr() { return pointer_traits::pointer_to(static_cast(*this)); } @@ -158,14 +166,14 @@ class generic_hook generic_hook() { - if(boost_intrusive_tags::safemode_or_autounlink){ + if(hooktags::safemode_or_autounlink){ node_algorithms::init(this->this_ptr()); } } generic_hook(const generic_hook& ) { - if(boost_intrusive_tags::safemode_or_autounlink){ + if(hooktags::safemode_or_autounlink){ node_algorithms::init(this->this_ptr()); } } @@ -176,7 +184,7 @@ class generic_hook ~generic_hook() { destructor_impl - (*this, detail::link_dispatch()); + (*this, detail::link_dispatch()); } void swap_nodes(generic_hook &other) @@ -188,22 +196,22 @@ class generic_hook bool is_linked() const { //is_linked() can be only used in safe-mode or auto-unlink - BOOST_STATIC_ASSERT(( boost_intrusive_tags::safemode_or_autounlink )); + BOOST_STATIC_ASSERT(( hooktags::safemode_or_autounlink )); return !node_algorithms::unique(this->this_ptr()); } void unlink() { - BOOST_STATIC_ASSERT(( (int)boost_intrusive_tags::link_mode == (int)auto_unlink )); - node_algorithms::unlink(this->this_ptr()); - node_algorithms::init(this->this_ptr()); + BOOST_STATIC_ASSERT(( (int)hooktags::link_mode == (int)auto_unlink )); + node_ptr n(this->this_ptr()); + if(!node_algorithms::inited(n)){ + node_algorithms::unlink(n); + node_algorithms::init(n); + } } }; -} //namespace detail } //namespace intrusive } //namespace boost -#include - #endif //BOOST_INTRUSIVE_GENERIC_HOOK_HPP diff --git a/boost/intrusive/detail/get_value_traits.hpp b/boost/intrusive/detail/get_value_traits.hpp new file mode 100644 index 0000000000..6f5a9e3f04 --- /dev/null +++ b/boost/intrusive/detail/get_value_traits.hpp @@ -0,0 +1,218 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2014 +// +// 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_DETAIL_GET_VALUE_TRAITS_HPP +#define BOOST_INTRUSIVE_DETAIL_GET_VALUE_TRAITS_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include + +namespace boost { +namespace intrusive { + +#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + +template +struct is_default_hook_tag +{ static const bool value = false; }; + +namespace detail{ + +template +struct concrete_hook_base_value_traits +{ + typedef typename BaseHook::hooktags tags; + typedef bhtraits + < T + , typename tags::node_traits + , tags::link_mode + , typename tags::tag + , tags::type> type; +}; + +template +struct concrete_hook_base_value_traits +{ + typedef typename BaseHook::hooktags type; +}; + +template +struct any_hook_base_value_traits +{ + //AnyToSomeHook value_traits derive from a generic_hook + //The generic_hook is configured with any_node_traits + //and AnyToSomeHook::value_traits with the correct + //node traits for the container, so use node_traits + //from AnyToSomeHook_ProtoValueTraits and the rest of + //elements from the hooktags member of the generic_hook + + typedef typename AnyToSomeHook_ProtoValueTraits::basic_hook_t basic_hook_t; + typedef typename pointer_rebind + < typename basic_hook_t::hooktags::node_traits::node_ptr + , void>::type void_pointer; + typedef typename AnyToSomeHook_ProtoValueTraits::template + node_traits_from_voidptr::type node_traits; + + typedef bhtraits + < T + , node_traits + , basic_hook_t::hooktags::link_mode + , typename basic_hook_t::hooktags::tag + , basic_hook_t::hooktags::type + > type; +}; + +template +struct any_hook_base_value_traits +{ + typedef typename AnyToSomeHook_ProtoValueTraits::basic_hook_t basic_hook_t; + typedef typename pointer_rebind + < typename basic_hook_t::hooktags::node_traits::node_ptr + , void>::type void_pointer; + + struct type + { + typedef typename AnyToSomeHook_ProtoValueTraits::template + node_traits_from_voidptr::type node_traits; + }; +}; + +template +struct get_member_value_traits +{ + typedef typename MemberHook::member_value_traits type; +}; + +BOOST_INTRUSIVE_INTERNAL_STATIC_BOOL_IS_TRUE(internal_any_hook, is_any_hook) +BOOST_INTRUSIVE_INTERNAL_STATIC_BOOL_IS_TRUE(internal_base_hook, hooktags::is_base_hook) + +template +struct internal_member_value_traits +{ + template static one test(...); + template static two test(typename U::member_value_traits* = 0); + static const bool value = sizeof(test(0)) == sizeof(two); +}; + +template::value> +struct supposed_value_traits; + +template::value> +struct get_base_value_traits; + +template::value> +struct supposed_base_value_traits; + +template::value> +struct supposed_member_value_traits; + +template::value> +struct any_or_concrete_value_traits; + +//Base any hook +template +struct get_base_value_traits + : any_hook_base_value_traits +{}; + +//Non-any base hook +template +struct get_base_value_traits + : concrete_hook_base_value_traits +{}; + +//...It's a default hook +template +struct supposed_value_traits +{ typedef typename SupposedValueTraits::template apply::type type; }; + +//...Not a default hook +template +struct supposed_value_traits +{ typedef SupposedValueTraits type; }; + +//...It's a base hook +template +struct supposed_base_value_traits + : get_base_value_traits +{}; + +//...Not a base hook, try if it's a member or value_traits +template +struct supposed_base_value_traits + : supposed_member_value_traits +{}; + +//...It's a member hook +template +struct supposed_member_value_traits + : get_member_value_traits +{}; + +//...Not a member hook +template +struct supposed_member_value_traits + : any_or_concrete_value_traits +{}; + +template +struct any_or_concrete_value_traits +{ + //A hook node (non-base, e.g.: member or other value traits + typedef typename AnyToSomeHook_ProtoValueTraits::basic_hook_t basic_hook_t; + typedef typename pointer_rebind + ::type void_pointer; + typedef typename AnyToSomeHook_ProtoValueTraits::template + node_traits_from_voidptr::type any_node_traits; + + struct type : basic_hook_t + { + typedef any_node_traits node_traits; + }; +}; + +template +struct any_or_concrete_value_traits +{ + typedef SupposedValueTraits type; +}; + +//////////////////////////////////////// +// get_value_traits / get_node_traits +//////////////////////////////////////// + +template +struct get_value_traits + : supposed_base_value_traits::type, T> +{}; + +template +struct get_node_traits +{ + typedef typename get_value_traits::type::node_traits type; +}; + +} //namespace detail{ + +#endif //BOOST_INTRUSIVE_DOXYGEN_INVOKED + +} //namespace intrusive { +} //namespace boost { + +#include + +#endif //#ifndef BOOST_INTRUSIVE_DETAIL_GET_VALUE_TRAITS_HPP diff --git a/boost/intrusive/detail/has_member_function_callable_with.hpp b/boost/intrusive/detail/has_member_function_callable_with.hpp index 6516e28067..ca96f15a19 100644 --- a/boost/intrusive/detail/has_member_function_callable_with.hpp +++ b/boost/intrusive/detail/has_member_function_callable_with.hpp @@ -1,6 +1,6 @@ ////////////////////////////////////////////////////////////////////////////// // -// (C) Copyright Ion Gaztanaga 2011-2012. Distributed under the Boost +// (C) Copyright Ion Gaztanaga 2011-2014. 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) // @@ -15,11 +15,10 @@ #ifndef BOOST_INTRUSIVE_DETAIL_HAS_MEMBER_FUNCTION_CALLABLE_WITH_DETAILS_INCLUDED #define BOOST_INTRUSIVE_DETAIL_HAS_MEMBER_FUNCTION_CALLABLE_WITH_DETAILS_INCLUDED - #include - #include #include - #include - #include + #include + #include + //Mark that we don't support 0 arg calls due to compiler ICE in GCC 3.4/4.0/4.1 and //wrong SFINAE for GCC 4.2/4.3 @@ -36,6 +35,12 @@ dont_care(...); }; + template + struct make_dontcare + { + typedef boost_intrusive_has_member_function_callable_with::dont_care type; + }; + struct private_type { static private_type p; @@ -51,7 +56,9 @@ } //boost_intrusive_has_member_function_callable_with - #include + #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_SINGLE_ITERATION + #endif #endif //BOOST_INTRUSIVE_DETAIL_HAS_MEMBER_FUNCTION_CALLABLE_WITH_DETAILS_INCLUDED @@ -69,11 +76,11 @@ #error "BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END not defined!" #endif - #if BOOST_PP_ITERATION_START() != 0 - #error "BOOST_PP_ITERATION_START() must be zero (0)" + #if BOOST_PP_ITERATION_START() > BOOST_PP_ITERATION_FINISH() + #error "BOOST_PP_ITERATION_START() must be <= BOOST_PP_ITERATION_FINISH()" #endif - #if BOOST_PP_ITERATION() == 0 + #if BOOST_PP_ITERATION() == BOOST_PP_ITERATION_START() BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN @@ -85,7 +92,7 @@ void BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME(); }; - struct Base : public Type, public BaseMixin { Base(); }; + struct Base : public ::boost::intrusive::detail::remove_cv::type, public BaseMixin { Base(); }; template class Helper{}; template @@ -98,7 +105,7 @@ sizeof(boost_intrusive_has_member_function_callable_with::yes_type) == sizeof(deduce((Base*)(0))); }; - #if !defined(BOOST_INTRUSIVE_PERFECT_FORWARDING) + #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) template @@ -113,76 +120,7 @@ }; //! - #if !defined(_MSC_VER) || (_MSC_VER < 1600) - - #if defined(BOOST_INTRUSIVE_DETAIL_HAS_MEMBER_FUNCTION_CALLABLE_WITH_0_ARGS_UNSUPPORTED) - - template - struct BOOST_PP_CAT(BOOST_PP_CAT(has_member_function_callable_with_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME),_impl) - - { - //Mark that we don't support 0 arg calls due to compiler ICE in GCC 3.4/4.0/4.1 and - //wrong SFINAE for GCC 4.2/4.3 - static const bool value = true; - }; - - #else //defined(BOOST_INTRUSIVE_DETAIL_HAS_MEMBER_FUNCTION_CALLABLE_WITH_0_ARGS_UNSUPPORTED) - - //Special case for 0 args - template< class F - , std::size_t N = - sizeof((boost::move_detail::declval(). - BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME (), 0))> - struct BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME) - { - boost_intrusive_has_member_function_callable_with::yes_type dummy; - BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME)(int); - }; - - //For buggy compilers like MSVC 7.1+ ((F*)0)->func() does not - //SFINAE-out the zeroarg_checker_ instantiation but sizeof yields to 0. - template - struct BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME) - { - boost_intrusive_has_member_function_callable_with::no_type dummy; - BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME)(int); - }; - - template - struct BOOST_PP_CAT(BOOST_PP_CAT(has_member_function_callable_with_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME),_impl) - - { - template - static BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME) - Test(BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME)*); - - template - static boost_intrusive_has_member_function_callable_with::no_type Test(...); - - static const bool value = sizeof(Test< Fun >(0)) - == sizeof(boost_intrusive_has_member_function_callable_with::yes_type); - }; - #endif //defined(BOOST_INTRUSIVE_DETAIL_HAS_MEMBER_FUNCTION_CALLABLE_WITH_0_ARGS_UNSUPPORTED) - - #else //#if !defined(_MSC_VER) || (_MSC_VER < 1600) - template - struct BOOST_PP_CAT(BOOST_PP_CAT(has_member_function_callable_with_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME),_impl) - - { - template - static decltype( boost::move_detail::declval().BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME() - , boost_intrusive_has_member_function_callable_with::yes_type()) - Test(Fun*); - - template - static boost_intrusive_has_member_function_callable_with::no_type Test(...); - - static const bool value = sizeof(Test(0)) - == sizeof(boost_intrusive_has_member_function_callable_with::yes_type); - }; - #endif //#if !defined(_MSC_VER) || (_MSC_VER < 1600) - - #else //#if !defined(BOOST_INTRUSIVE_PERFECT_FORWARDING) + #else //!defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) template struct BOOST_PP_CAT(BOOST_PP_CAT(has_member_function_callable_with_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME),_impl); @@ -194,6 +132,8 @@ static const bool value = false; }; + #ifdef BOOST_NO_CXX11_DECLTYPE + //Special case for 0 args template< class F , std::size_t N = @@ -214,14 +154,21 @@ BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME)(int); }; + #endif //#ifdef BOOST_NO_CXX11_DECLTYPE + template struct BOOST_PP_CAT(BOOST_PP_CAT(has_member_function_callable_with_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME),_impl) { + #ifndef BOOST_NO_CXX11_DECLTYPE + template().BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME()) > + static boost_intrusive_has_member_function_callable_with::yes_type Test(U*); + #else template - static BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME) + static BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME) Test(BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME)*); - + #endif + template static boost_intrusive_has_member_function_callable_with::no_type Test(...); @@ -229,30 +176,23 @@ == sizeof(boost_intrusive_has_member_function_callable_with::yes_type); }; - template - struct BOOST_PP_CAT( funwrap_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME ) - : Fun - { - BOOST_PP_CAT( funwrap_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME )(); - using Fun::BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME; - - boost_intrusive_has_member_function_callable_with::private_type - BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME - ( DontCares...) const; - }; - template struct BOOST_PP_CAT( BOOST_PP_CAT(has_member_function_callable_with_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME), _impl) { - template - struct make_dontcare + + template + struct FunWrapTmpl : Fun { - typedef boost_intrusive_has_member_function_callable_with::dont_care type; + FunWrapTmpl(); + using Fun::BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME; + + boost_intrusive_has_member_function_callable_with::private_type + BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME + ( DontCares...) const; }; - typedef BOOST_PP_CAT( funwrap_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME ) - ::type...> FunWrap; + typedef FunWrapTmpl::type...> FunWrap; static bool const value = (sizeof(boost_intrusive_has_member_function_callable_with::no_type) == sizeof(boost_intrusive_has_member_function_callable_with::is_private_type @@ -274,32 +214,99 @@ , Args... > {}; - #endif //#if !defined(BOOST_INTRUSIVE_PERFECT_FORWARDING) + #endif //defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + + BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END + + #endif //BOOST_PP_ITERATION() == BOOST_PP_ITERATION_START() + + #if BOOST_PP_ITERATION() == 0 + + BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN + + #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + + #if !defined(_MSC_VER) || (_MSC_VER < 1600) + + #if defined(BOOST_INTRUSIVE_DETAIL_HAS_MEMBER_FUNCTION_CALLABLE_WITH_0_ARGS_UNSUPPORTED) + + template + struct BOOST_PP_CAT(BOOST_PP_CAT(has_member_function_callable_with_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME),_impl) + + { + //Mark that we don't support 0 arg calls due to compiler ICE in GCC 3.4/4.0/4.1 and + //wrong SFINAE for GCC 4.2/4.3 + static const bool value = true; + }; + + #else //defined(BOOST_INTRUSIVE_DETAIL_HAS_MEMBER_FUNCTION_CALLABLE_WITH_0_ARGS_UNSUPPORTED) + + //Special case for 0 args + template< class F + , std::size_t N = + sizeof((boost::move_detail::declval(). + BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME (), 0))> + struct BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME) + { + boost_intrusive_has_member_function_callable_with::yes_type dummy; + BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME)(int); + }; + + //For buggy compilers like MSVC 7.1+ ((F*)0)->func() does not + //SFINAE-out the zeroarg_checker_ instantiation but sizeof yields to 0. + template + struct BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME) + { + boost_intrusive_has_member_function_callable_with::no_type dummy; + BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME)(int); + }; + + template + struct BOOST_PP_CAT(BOOST_PP_CAT(has_member_function_callable_with_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME),_impl) + + { + template + static BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME) + Test(BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME)*); + + template + static boost_intrusive_has_member_function_callable_with::no_type Test(...); + + static const bool value = sizeof(Test< Fun >(0)) + == sizeof(boost_intrusive_has_member_function_callable_with::yes_type); + }; + #endif //defined(BOOST_INTRUSIVE_DETAIL_HAS_MEMBER_FUNCTION_CALLABLE_WITH_0_ARGS_UNSUPPORTED) + + #else //#if !defined(_MSC_VER) || (_MSC_VER < 1600) + template + struct BOOST_PP_CAT(BOOST_PP_CAT(has_member_function_callable_with_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME),_impl) + + { + template + static decltype( boost::move_detail::declval().BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME() + , boost_intrusive_has_member_function_callable_with::yes_type()) + Test(U*); + + template + static boost_intrusive_has_member_function_callable_with::no_type Test(...); + + static const bool value = sizeof(Test(0)) + == sizeof(boost_intrusive_has_member_function_callable_with::yes_type); + }; + #endif //#if !defined(_MSC_VER) || (_MSC_VER < 1600) + + #else //#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + + #endif //#if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END #else //BOOST_PP_ITERATION() == 0 - #if !defined(BOOST_INTRUSIVE_PERFECT_FORWARDING) + #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN - template - struct BOOST_PP_CAT( BOOST_PP_CAT(funwrap, BOOST_PP_ITERATION()) - , BOOST_PP_CAT(_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME)) - : Fun - { - BOOST_PP_CAT( BOOST_PP_CAT(funwrap, BOOST_PP_ITERATION()) - , BOOST_PP_CAT(_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME))(); - - using Fun::BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME; - boost_intrusive_has_member_function_callable_with::private_type - BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME - ( BOOST_PP_ENUM(BOOST_PP_ITERATION() - , BOOST_INTRUSIVE_PP_IDENTITY - , boost_intrusive_has_member_function_callable_with::dont_care)) const; - }; - template struct BOOST_PP_CAT( BOOST_PP_CAT(has_member_function_callable_with_ , BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME),_impl) @@ -309,9 +316,18 @@ , BOOST_INTRUSIVE_PP_IDENTITY , void)> { - typedef BOOST_PP_CAT( BOOST_PP_CAT(funwrap, BOOST_PP_ITERATION()) - , BOOST_PP_CAT(_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME)) - FunWrap; + struct FunWrap : Fun + { + FunWrap(); + + using Fun::BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME; + boost_intrusive_has_member_function_callable_with::private_type + BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME + ( BOOST_PP_ENUM(BOOST_PP_ITERATION() + , BOOST_INTRUSIVE_PP_IDENTITY + , boost_intrusive_has_member_function_callable_with::dont_care)) const; + }; + static bool const value = (sizeof(boost_intrusive_has_member_function_callable_with::no_type) == sizeof(boost_intrusive_has_member_function_callable_with::is_private_type @@ -325,13 +341,13 @@ }; BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END - #endif //#if !defined(BOOST_INTRUSIVE_PERFECT_FORWARDING) + #endif //#if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) #endif //BOOST_PP_ITERATION() == 0 #if BOOST_PP_ITERATION() == BOOST_PP_ITERATION_FINISH() - #if !defined(BOOST_INTRUSIVE_PERFECT_FORWARDING) + #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN @@ -345,7 +361,7 @@ BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END - #endif //#if !defined(BOOST_INTRUSIVE_PERFECT_FORWARDING) + #endif //#if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) #undef BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME #undef BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN diff --git a/boost/intrusive/detail/hashtable_node.hpp b/boost/intrusive/detail/hashtable_node.hpp index 86e607460c..6449fa0dd2 100644 --- a/boost/intrusive/detail/hashtable_node.hpp +++ b/boost/intrusive/detail/hashtable_node.hpp @@ -1,6 +1,6 @@ ///////////////////////////////////////////////////////////////////////////// // -// (C) Copyright Ion Gaztanaga 2007-2012 +// (C) Copyright Ion Gaztanaga 2007-2014 // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at @@ -13,45 +13,24 @@ #ifndef BOOST_INTRUSIVE_HASHTABLE_NODE_HPP #define BOOST_INTRUSIVE_HASHTABLE_NODE_HPP -#include -#include +#if defined(_MSC_VER) +# pragma once +#endif + #include #include -#include #include -#include -//#include //remove-me -#include +#include +#include //make_slist #include -#include -#include +#include +#include namespace boost { namespace intrusive { namespace detail { -template -struct prime_list_holder -{ - static const std::size_t prime_list[]; - static const std::size_t prime_list_size; -}; - -template -const std::size_t prime_list_holder::prime_list[] = { - 3ul, 7ul, 11ul, 17ul, 29ul, - 53ul, 97ul, 193ul, 389ul, 769ul, - 1543ul, 3079ul, 6151ul, 12289ul, 24593ul, - 49157ul, 98317ul, 196613ul, 393241ul, 786433ul, - 1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul, - 50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul, - 1610612741ul, 3221225473ul, 4294967291ul }; - -template -const std::size_t prime_list_holder::prime_list_size - = sizeof(prime_list)/sizeof(std::size_t); - template struct bucket_impl : public Slist { @@ -89,6 +68,7 @@ struct bucket_traits_impl typedef typename pointer_traits ::template rebind_pointer < bucket_impl >::type bucket_ptr; + typedef Slist slist; typedef typename Slist::size_type size_type; /// @endcond @@ -100,7 +80,6 @@ struct bucket_traits_impl : buckets_(x.buckets_), buckets_len_(x.buckets_len_) {} - bucket_traits_impl(BOOST_RV_REF(bucket_traits_impl) x) : buckets_(x.buckets_), buckets_len_(x.buckets_len_) { x.buckets_ = bucket_ptr(); x.buckets_len_ = 0; } @@ -127,57 +106,107 @@ struct bucket_traits_impl size_type buckets_len_; }; -template +template +struct hash_reduced_slist_node_traits +{ + template static detail::one test(...); + template static detail::two test(typename U::reduced_slist_node_traits* = 0); + static const bool value = sizeof(test(0)) == sizeof(detail::two); +}; + +template +struct apply_reduced_slist_node_traits +{ + typedef typename NodeTraits::reduced_slist_node_traits type; +}; + +template +struct reduced_slist_node_traits +{ + typedef typename detail::eval_if_c + < hash_reduced_slist_node_traits::value + , apply_reduced_slist_node_traits + , detail::identity + >::type type; +}; + +template +struct get_slist_impl +{ + typedef trivial_value_traits trivial_traits; + + //Reducing symbol length + struct type : make_slist + < typename NodeTraits::node + , boost::intrusive::value_traits + , boost::intrusive::constant_time_size + , boost::intrusive::size_type + >::type + {}; +}; + +} //namespace detail { + +template class hashtable_iterator - : public std::iterator +{ + typedef boost::intrusive::iterator < std::forward_iterator_tag - , typename Container::value_type - , typename pointer_traits::difference_type + , typename BucketValueTraits::value_traits::value_type + , typename pointer_traits::difference_type , typename detail::add_const_if_c - ::type * + ::type * , typename detail::add_const_if_c - ::type & - > -{ - typedef typename Container::real_value_traits real_value_traits; - typedef typename Container::siterator siterator; - typedef typename Container::const_siterator const_siterator; - typedef typename Container::bucket_type bucket_type; + ::type & + > iterator_traits; + + typedef typename BucketValueTraits::value_traits value_traits; + typedef typename BucketValueTraits::bucket_traits bucket_traits; + typedef typename value_traits::node_traits node_traits; + typedef typename detail::get_slist_impl + ::type + >::type slist_impl; + typedef typename slist_impl::iterator siterator; + typedef typename slist_impl::const_iterator const_siterator; + typedef detail::bucket_impl bucket_type; typedef typename pointer_traits - ::template rebind_pointer - < const Container >::type const_cont_ptr; - typedef typename Container::size_type size_type; + ::template rebind_pointer + < const BucketValueTraits >::type const_bucketvaltraits_ptr; + typedef typename slist_impl::size_type size_type; - static typename Container::node_ptr downcast_bucket(typename bucket_type::node_ptr p) + + static typename node_traits::node_ptr downcast_bucket(typename bucket_type::node_ptr p) { - return pointer_traits:: - pointer_to(static_cast(*p)); + return pointer_traits:: + pointer_to(static_cast(*p)); } public: - typedef typename Container::value_type value_type; - typedef typename detail::add_const_if_c - ::type *pointer; - typedef typename detail::add_const_if_c - ::type &reference; + typedef typename iterator_traits::difference_type difference_type; + typedef typename iterator_traits::value_type value_type; + typedef typename iterator_traits::pointer pointer; + typedef typename iterator_traits::reference reference; + typedef typename iterator_traits::iterator_category iterator_category; hashtable_iterator () + : slist_it_() //Value initialization to achieve "null iterators" (N3644) {} - explicit hashtable_iterator(siterator ptr, const Container *cont) - : slist_it_ (ptr), cont_ (cont ? pointer_traits::pointer_to(*cont) : const_cont_ptr() ) + explicit hashtable_iterator(siterator ptr, const BucketValueTraits *cont) + : slist_it_ (ptr), traitsptr_ (cont ? pointer_traits::pointer_to(*cont) : const_bucketvaltraits_ptr() ) {} - hashtable_iterator(const hashtable_iterator &other) - : slist_it_(other.slist_it()), cont_(other.get_container()) + hashtable_iterator(const hashtable_iterator &other) + : slist_it_(other.slist_it()), traitsptr_(other.get_bucket_value_traits()) {} const siterator &slist_it() const { return slist_it_; } - hashtable_iterator unconst() const - { return hashtable_iterator(this->slist_it(), this->get_container()); } + hashtable_iterator unconst() const + { return hashtable_iterator(this->slist_it(), this->get_bucket_value_traits()); } public: hashtable_iterator& operator++() @@ -201,48 +230,57 @@ class hashtable_iterator pointer operator->() const { - return boost::intrusive::detail::to_raw_pointer(this->get_real_value_traits()->to_value_ptr + return boost::intrusive::detail::to_raw_pointer(this->priv_value_traits().to_value_ptr (downcast_bucket(slist_it_.pointed_node()))); } - const const_cont_ptr &get_container() const - { return cont_; } + const const_bucketvaltraits_ptr &get_bucket_value_traits() const + { return traitsptr_; } + + const value_traits &priv_value_traits() const + { return traitsptr_->priv_value_traits(); } - const real_value_traits *get_real_value_traits() const - { return &this->get_container()->get_real_value_traits(); } + const bucket_traits &priv_bucket_traits() const + { return traitsptr_->priv_bucket_traits(); } private: void increment() { - const Container *cont = boost::intrusive::detail::to_raw_pointer(cont_); - bucket_type* buckets = boost::intrusive::detail::to_raw_pointer(cont->bucket_pointer()); - size_type buckets_len = cont->bucket_count(); + const bucket_traits &rbuck_traits = this->priv_bucket_traits(); + bucket_type* const buckets = boost::intrusive::detail::to_raw_pointer(rbuck_traits.bucket_begin()); + const size_type buckets_len = rbuck_traits.bucket_count(); ++slist_it_; - if(buckets[0].cend().pointed_node() <= slist_it_.pointed_node() && - slist_it_.pointed_node()<= buckets[buckets_len].cend().pointed_node() ){ - //Now get the bucket_impl from the iterator + const typename slist_impl::node_ptr n = slist_it_.pointed_node(); + const siterator first_bucket_bbegin = buckets->end(); + if(first_bucket_bbegin.pointed_node() <= n && n <= buckets[buckets_len-1].cend().pointed_node()){ + //If one-past the node is inside the bucket then look for the next non-empty bucket + //1. get the bucket_impl from the iterator const bucket_type &b = static_cast (bucket_type::slist_type::container_from_end_iterator(slist_it_)); - //Now just calculate the index b has in the bucket array - size_type n_bucket = static_cast(&b - &buckets[0]); + //2. Now just calculate the index b has in the bucket array + size_type n_bucket = static_cast(&b - buckets); + + //3. Iterate until a non-empty bucket is found do{ - if (++n_bucket == buckets_len){ - slist_it_ = (&buckets[0] + buckets_len)->end(); - break; + if (++n_bucket >= buckets_len){ //bucket overflow, return end() iterator + slist_it_ = buckets->before_begin(); + return; } - slist_it_ = buckets[n_bucket].begin(); } - while (slist_it_ == buckets[n_bucket].end()); + while (buckets[n_bucket].empty()); + slist_it_ = buckets[n_bucket].begin(); + } + else{ + //++slist_it_ yield to a valid object } } - siterator slist_it_; - const_cont_ptr cont_; + siterator slist_it_; + const_bucketvaltraits_ptr traitsptr_; }; -} //namespace detail { } //namespace intrusive { } //namespace boost { diff --git a/boost/intrusive/detail/hook_traits.hpp b/boost/intrusive/detail/hook_traits.hpp new file mode 100644 index 0000000000..c9c115b1de --- /dev/null +++ b/boost/intrusive/detail/hook_traits.hpp @@ -0,0 +1,182 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2006-2014 +// +// 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_DETAIL_HOOK_TRAITS_HPP +#define BOOST_INTRUSIVE_DETAIL_HOOK_TRAITS_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace intrusive { + +template +struct bhtraits_base +{ + public: + typedef NodePtr node_ptr; + typedef typename pointer_traits::element_type node; + typedef node_holder node_holder_type; + typedef T value_type; + typedef typename pointer_traits:: + template rebind_pointer::type const_node_ptr; + typedef typename pointer_traits:: + template rebind_pointer::type pointer; + typedef typename pointer_traits:: + template rebind_pointer::type const_pointer; + //typedef typename pointer_traits::reference reference; + //typedef typename pointer_traits::reference const_reference; + typedef T & reference; + typedef const T & const_reference; + typedef node_holder_type & node_holder_reference; + typedef const node_holder_type & const_node_holder_reference; + typedef node& node_reference; + typedef const node & const_node_reference; + + static pointer to_value_ptr(const node_ptr & n) + { + return pointer_traits::pointer_to + (static_cast(static_cast(*n))); + } + + static const_pointer to_value_ptr(const const_node_ptr & n) + { + return pointer_traits::pointer_to + (static_cast(static_cast(*n))); + } + + static node_ptr to_node_ptr(reference value) + { + return pointer_traits::pointer_to + (static_cast(static_cast(value))); + } + + static const_node_ptr to_node_ptr(const_reference value) + { + return pointer_traits::pointer_to + (static_cast(static_cast(value))); + } +}; + +template +struct bhtraits + : public bhtraits_base +{ + static const link_mode_type link_mode = LinkMode; + typedef NodeTraits node_traits; +}; + + +template +struct mhtraits +{ + public: + typedef Hook hook_type; + typedef typename hook_type::hooktags::node_traits node_traits; + typedef typename node_traits::node node; + typedef T value_type; + typedef typename node_traits::node_ptr node_ptr; + typedef typename node_traits::const_node_ptr const_node_ptr; + typedef typename pointer_traits:: + template rebind_pointer::type pointer; + typedef typename pointer_traits:: + template rebind_pointer::type const_pointer; + typedef T & reference; + typedef const T & const_reference; + typedef node& node_reference; + typedef const node & const_node_reference; + typedef hook_type& hook_reference; + typedef const hook_type & const_hook_reference; + + static const link_mode_type link_mode = Hook::hooktags::link_mode; + + static node_ptr to_node_ptr(reference value) + { + return pointer_traits::pointer_to + (static_cast(static_cast(value.*P))); + } + + static const_node_ptr to_node_ptr(const_reference value) + { + return pointer_traits::pointer_to + (static_cast(static_cast(value.*P))); + } + + static pointer to_value_ptr(const node_ptr & n) + { + return pointer_traits::pointer_to + (*detail::parent_from_member + (static_cast(boost::intrusive::detail::to_raw_pointer(n)), P)); + } + + static const_pointer to_value_ptr(const const_node_ptr & n) + { + return pointer_traits::pointer_to + (*detail::parent_from_member + (static_cast(boost::intrusive::detail::to_raw_pointer(n)), P)); + } +}; + + +template +struct fhtraits +{ + public: + typedef typename Functor::hook_type hook_type; + typedef typename Functor::hook_ptr hook_ptr; + typedef typename Functor::const_hook_ptr const_hook_ptr; + typedef typename hook_type::hooktags::node_traits node_traits; + typedef typename node_traits::node node; + typedef typename Functor::value_type value_type; + typedef typename node_traits::node_ptr node_ptr; + typedef typename node_traits::const_node_ptr const_node_ptr; + typedef typename pointer_traits:: + template rebind_pointer::type pointer; + typedef typename pointer_traits:: + template rebind_pointer::type const_pointer; + typedef value_type & reference; + typedef const value_type & const_reference; + static const link_mode_type link_mode = hook_type::hooktags::link_mode; + + static node_ptr to_node_ptr(reference value) + { return static_cast(boost::intrusive::detail::to_raw_pointer(Functor::to_hook_ptr(value))); } + + static const_node_ptr to_node_ptr(const_reference value) + { return static_cast(boost::intrusive::detail::to_raw_pointer(Functor::to_hook_ptr(value))); } + + static pointer to_value_ptr(const node_ptr & n) + { return Functor::to_value_ptr(to_hook_ptr(n)); } + + static const_pointer to_value_ptr(const const_node_ptr & n) + { return Functor::to_value_ptr(to_hook_ptr(n)); } + + private: + static hook_ptr to_hook_ptr(const node_ptr & n) + { return hook_ptr(&*static_cast(&*n)); } + + static const_hook_ptr to_hook_ptr(const const_node_ptr & n) + { return const_hook_ptr(&*static_cast(&*n)); } +}; + + +} //namespace intrusive +} //namespace boost + +#endif //BOOST_INTRUSIVE_DETAIL_HOOK_TRAITS_HPP diff --git a/boost/intrusive/detail/iiterator.hpp b/boost/intrusive/detail/iiterator.hpp new file mode 100644 index 0000000000..8378ead6ad --- /dev/null +++ b/boost/intrusive/detail/iiterator.hpp @@ -0,0 +1,227 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2006-2014 +// +// 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_DETAIL_IITERATOR_HPP +#define BOOST_INTRUSIVE_DETAIL_IITERATOR_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include +#include + +#include + + +namespace boost { +namespace intrusive { + +template +struct iterator +{ + typedef Category iterator_category; + typedef T value_type; + typedef Distance difference_type; + typedef Pointer pointer; + typedef Reference reference; +}; + +template +struct iterator_traits +{ + typedef typename Iterator::difference_type difference_type; + typedef typename Iterator::value_type value_type; + typedef typename Iterator::pointer pointer; + typedef typename Iterator::reference reference; + typedef typename Iterator::iterator_category iterator_category; +}; + +template +struct iterator_traits +{ + typedef std::ptrdiff_t difference_type; + typedef T value_type; + typedef T* pointer; + typedef T& reference; + typedef std::random_access_iterator_tag iterator_category; +}; + +template +struct iterator_traits +{ + typedef std::ptrdiff_t difference_type; + typedef T value_type; + typedef const T* pointer; + typedef const T& reference; + typedef std::random_access_iterator_tag iterator_category; +}; + +template +struct value_traits_pointers +{ + typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT + (boost::intrusive::detail:: + , ValueTraits, value_traits_ptr + , typename pointer_traits::template + rebind_pointer::type) value_traits_ptr; + + typedef typename pointer_traits::template + rebind_pointer::type const_value_traits_ptr; +}; + +template +struct iiterator +{ + typedef ValueTraits value_traits; + typedef typename value_traits::node_traits node_traits; + typedef typename node_traits::node node; + typedef typename node_traits::node_ptr node_ptr; + typedef ::boost::intrusive::pointer_traits nodepointer_traits_t; + typedef typename nodepointer_traits_t::template + rebind_pointer::type void_pointer; + typedef typename ValueTraits::value_type value_type; + typedef typename ValueTraits::pointer nonconst_pointer; + typedef typename ValueTraits::const_pointer yesconst_pointer; + typedef typename ::boost::intrusive::pointer_traits + ::reference nonconst_reference; + typedef typename ::boost::intrusive::pointer_traits + ::reference yesconst_reference; + typedef typename nodepointer_traits_t::difference_type difference_type; + typedef typename detail::if_c + ::type pointer; + typedef typename detail::if_c + ::type reference; + typedef iterator + < Category + , value_type + , difference_type + , pointer + , reference + > iterator_traits; + typedef typename value_traits_pointers + ::value_traits_ptr value_traits_ptr; + typedef typename value_traits_pointers + ::const_value_traits_ptr const_value_traits_ptr; + static const bool stateful_value_traits = + detail::is_stateful_value_traits::value; +}; + +template +struct iiterator_members +{ + + iiterator_members() + : nodeptr_()//Value initialization to achieve "null iterators" (N3644) + {} + + iiterator_members(const NodePtr &n_ptr, const StoredPointer &data) + : nodeptr_(n_ptr), ptr_(data) + {} + + StoredPointer get_ptr() const + { return ptr_; } + + NodePtr nodeptr_; + StoredPointer ptr_; +}; + +template +struct iiterator_members +{ + iiterator_members() + : nodeptr_()//Value initialization to achieve "null iterators" (N3644) + {} + + iiterator_members(const NodePtr &n_ptr, const StoredPointer &) + : nodeptr_(n_ptr) + {} + + StoredPointer get_ptr() const + { return StoredPointer(); } + + NodePtr nodeptr_; +}; + +namespace detail { + +template inline +void advance_impl(InputIt& it, Distance n, const std::input_iterator_tag&) +{ + while(n--) + ++it; +} + +template inline +void advance_impl(InputIt& it, Distance n, std::forward_iterator_tag &) +{ + while(n--) + ++it; +} + +template inline +void advance_impl(InputIt& it, Distance n, std::bidirectional_iterator_tag &) +{ + for (; 0 < n; --n) + ++it; + for (; n < 0; ++n) + --it; +} + +template +inline void advance_impl(InputIt& it, Distance n, const std::random_access_iterator_tag &) +{ + it += n; +} + +} //namespace detail + +template +inline void iterator_advance(InputIt& it, Distance n) +{ // increment iterator by offset, arbitrary iterators + boost::intrusive::detail::advance_impl(it, n, boost::intrusive::iterator_traits::iterator_category()); +} + +namespace detail{ + +template +inline void distance_impl(InputIt first, InputIt last, Distance& off, const Category &) +{ + while(first != last){ + ++off; + ++first; + } +} + +template inline +void distance_impl(InputIt first, InputIt last, Distance& off, const std::random_access_iterator_tag&) +{ + off += last - first; +} + +} //namespace detail + +template inline +typename iterator_traits::difference_type iterator_distance(InputIt first, InputIt last) +{ + typename iterator_traits::difference_type off = 0; + boost::intrusive::detail::distance_impl(first, last, off, boost::intrusive::iterator_traits::iterator_category()); + return off; +} + + +} //namespace intrusive +} //namespace boost + +#endif //BOOST_INTRUSIVE_DETAIL_IITERATOR_HPP diff --git a/boost/intrusive/detail/is_stateful_value_traits.hpp b/boost/intrusive/detail/is_stateful_value_traits.hpp index 8677c666d4..da2260edf6 100644 --- a/boost/intrusive/detail/is_stateful_value_traits.hpp +++ b/boost/intrusive/detail/is_stateful_value_traits.hpp @@ -1,6 +1,6 @@ ///////////////////////////////////////////////////////////////////////////// // -// (C) Copyright Ion Gaztanaga 2009-2012. +// (C) Copyright Ion Gaztanaga 2009-2013. // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at @@ -13,7 +13,9 @@ #ifndef BOOST_INTRUSIVE_DETAIL_IS_STATEFUL_VALUE_TRAITS_HPP #define BOOST_INTRUSIVE_DETAIL_IS_STATEFUL_VALUE_TRAITS_HPP -#include +#if defined(_MSC_VER) +# pragma once +#endif #if defined(_MSC_VER) && (_MSC_VER <= 1310) @@ -72,6 +74,4 @@ struct is_stateful_value_traits #endif -#include - #endif //@ifndef BOOST_INTRUSIVE_DETAIL_IS_STATEFUL_VALUE_TRAITS_HPP diff --git a/boost/intrusive/detail/key_nodeptr_comp.hpp b/boost/intrusive/detail/key_nodeptr_comp.hpp new file mode 100644 index 0000000000..dfee8b9bfc --- /dev/null +++ b/boost/intrusive/detail/key_nodeptr_comp.hpp @@ -0,0 +1,72 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2014 +// +// 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_DETAIL_KEY_NODEPTR_COMP_HPP +#define BOOST_INTRUSIVE_DETAIL_KEY_NODEPTR_COMP_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +namespace boost { +namespace intrusive { +namespace detail { + +template +struct key_nodeptr_comp + : private ebo_functor_holder +{ + typedef ValueTraits value_traits; + typedef typename value_traits::value_type value_type; + typedef typename value_traits::node_ptr node_ptr; + typedef typename value_traits::const_node_ptr const_node_ptr; + typedef ebo_functor_holder base_t; + + key_nodeptr_comp(KeyValueCompare kcomp, const ValueTraits *traits) + : base_t(kcomp), traits_(traits) + {} + + template + struct is_node_ptr + { + static const bool value = is_same::value || is_same::value; + }; + + template + const value_type & key_forward + (const T &node, typename enable_if_c::value>::type * = 0) const + { return *traits_->to_value_ptr(node); } + + template + const T & key_forward(const T &key, typename enable_if_c::value>::type* = 0) const + { return key; } + + + template + bool operator()(const KeyType &key1, const KeyType2 &key2) const + { return base_t::get()(this->key_forward(key1), this->key_forward(key2)); } + + template + bool operator()(const KeyType &key1) const + { return base_t::get()(this->key_forward(key1)); } + + const ValueTraits *const traits_; +}; + +} //namespace detail{ +} //namespace intrusive{ +} //namespace boost{ + +#endif //BOOST_INTRUSIVE_DETAIL_KEY_NODEPTR_COMP_HPP diff --git a/boost/intrusive/detail/list_iterator.hpp b/boost/intrusive/detail/list_iterator.hpp new file mode 100644 index 0000000000..efddfba3f1 --- /dev/null +++ b/boost/intrusive/detail/list_iterator.hpp @@ -0,0 +1,129 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Olaf Krzikalla 2004-2006. +// (C) Copyright Ion Gaztanaga 2006-2013 +// +// 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_LIST_ITERATOR_HPP +#define BOOST_INTRUSIVE_LIST_ITERATOR_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include + +namespace boost { +namespace intrusive { + +// list_iterator provides some basic functions for a +// node oriented bidirectional iterator: +template +class list_iterator +{ + protected: + typedef iiterator + types_t; + + static const bool stateful_value_traits = types_t::stateful_value_traits; + + typedef ValueTraits value_traits; + typedef typename types_t::node_traits node_traits; + + typedef typename types_t::node node; + typedef typename types_t::node_ptr node_ptr; + typedef typename types_t::const_value_traits_ptr const_value_traits_ptr; + + public: + typedef typename types_t::iterator_traits::difference_type difference_type; + typedef typename types_t::iterator_traits::value_type value_type; + typedef typename types_t::iterator_traits::pointer pointer; + typedef typename types_t::iterator_traits::reference reference; + typedef typename types_t::iterator_traits::iterator_category iterator_category; + + list_iterator() + {} + + explicit list_iterator(const node_ptr & nodeptr, const const_value_traits_ptr &traits_ptr) + : members_(nodeptr, traits_ptr) + {} + + list_iterator(list_iterator const& other) + : members_(other.pointed_node(), other.get_value_traits()) + {} + + const node_ptr &pointed_node() const + { return members_.nodeptr_; } + + list_iterator &operator=(const node_ptr &node) + { members_.nodeptr_ = node; return static_cast(*this); } + + const_value_traits_ptr get_value_traits() const + { return members_.get_ptr(); } + + public: + list_iterator& operator++() + { + node_ptr p = node_traits::get_next(members_.nodeptr_); + members_.nodeptr_ = p; + return static_cast (*this); + } + + list_iterator operator++(int) + { + list_iterator result (*this); + members_.nodeptr_ = node_traits::get_next(members_.nodeptr_); + return result; + } + + list_iterator& operator--() + { + members_.nodeptr_ = node_traits::get_previous(members_.nodeptr_); + return static_cast (*this); + } + + list_iterator operator--(int) + { + list_iterator result (*this); + members_.nodeptr_ = node_traits::get_previous(members_.nodeptr_); + return result; + } + + friend bool operator== (const list_iterator& l, const list_iterator& r) + { return l.pointed_node() == r.pointed_node(); } + + friend bool operator!= (const list_iterator& l, const list_iterator& r) + { return !(l == r); } + + reference operator*() const + { return *operator->(); } + + pointer operator->() const + { return this->operator_arrow(detail::bool_()); } + + list_iterator unconst() const + { return list_iterator(this->pointed_node(), this->get_value_traits()); } + + private: + pointer operator_arrow(detail::false_) const + { return ValueTraits::to_value_ptr(members_.nodeptr_); } + + pointer operator_arrow(detail::true_) const + { return this->get_value_traits()->to_value_ptr(members_.nodeptr_); } + + iiterator_members members_; +}; + +} //namespace intrusive +} //namespace boost + +#endif //BOOST_INTRUSIVE_LIST_ITERATOR_HPP diff --git a/boost/intrusive/detail/list_node.hpp b/boost/intrusive/detail/list_node.hpp index d406af60e4..ba97ece956 100644 --- a/boost/intrusive/detail/list_node.hpp +++ b/boost/intrusive/detail/list_node.hpp @@ -1,7 +1,7 @@ ///////////////////////////////////////////////////////////////////////////// // // (C) Copyright Olaf Krzikalla 2004-2006. -// (C) Copyright Ion Gaztanaga 2006-2012 +// (C) Copyright Ion Gaztanaga 2006-2013 // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at @@ -14,10 +14,11 @@ #ifndef BOOST_INTRUSIVE_LIST_NODE_HPP #define BOOST_INTRUSIVE_LIST_NODE_HPP -#include -#include -#include -#include +#if defined(_MSC_VER) +# pragma once +#endif + +#include namespace boost { namespace intrusive { @@ -29,8 +30,7 @@ namespace intrusive { template struct list_node { - typedef typename pointer_traits - :: template rebind_pointer::type node_ptr; + typedef typename pointer_rebind::type node_ptr; node_ptr next_; node_ptr prev_; }; @@ -38,153 +38,30 @@ struct list_node template struct list_node_traits { - typedef list_node node; - typedef typename pointer_traits - :: template rebind_pointer::type node_ptr; - typedef typename pointer_traits - :: template rebind_pointer::type const_node_ptr; + typedef list_node node; + typedef typename node::node_ptr node_ptr; + typedef typename pointer_rebind::type const_node_ptr; + + static node_ptr get_previous(const const_node_ptr & n) + { return n->prev_; } - static const node_ptr &get_previous(const const_node_ptr & n) + static node_ptr get_previous(const node_ptr & n) { return n->prev_; } static void set_previous(const node_ptr & n, const node_ptr & prev) { n->prev_ = prev; } - static const node_ptr &get_next(const const_node_ptr & n) + static node_ptr get_next(const const_node_ptr & n) + { return n->next_; } + + static node_ptr get_next(const node_ptr & n) { return n->next_; } static void set_next(const node_ptr & n, const node_ptr & next) { n->next_ = next; } }; -// list_iterator provides some basic functions for a -// node oriented bidirectional iterator: -template -class list_iterator - : public std::iterator - < std::bidirectional_iterator_tag - , typename Container::value_type - , typename Container::difference_type - , typename detail::if_c::type - , typename detail::if_c::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 typename pointer_traits:: - template rebind_pointer::type void_pointer; - static const bool store_container_ptr = - detail::store_cont_ptr_on_it::value; - - public: - typedef typename Container::value_type value_type; - typedef typename detail::if_c::type pointer; - typedef typename detail::if_c::type reference; - - list_iterator() - : members_ (node_ptr(), 0) - {} - - explicit list_iterator(const node_ptr & node, const Container *cont_ptr) - : members_ (node, cont_ptr) - {} - - list_iterator(list_iterator const& other) - : members_(other.pointed_node(), other.get_container()) - {} - - const node_ptr &pointed_node() const - { return members_.nodeptr_; } - - list_iterator &operator=(const node_ptr &node) - { members_.nodeptr_ = node; return static_cast(*this); } - - public: - list_iterator& operator++() - { - node_ptr p = node_traits::get_next(members_.nodeptr_); - members_.nodeptr_ = p; - //members_.nodeptr_ = node_traits::get_next(members_.nodeptr_); - return static_cast (*this); - } - - list_iterator operator++(int) - { - list_iterator result (*this); - members_.nodeptr_ = node_traits::get_next(members_.nodeptr_); - return result; - } - - list_iterator& operator--() - { - members_.nodeptr_ = node_traits::get_previous(members_.nodeptr_); - return static_cast (*this); - } - - list_iterator operator--(int) - { - list_iterator result (*this); - members_.nodeptr_ = node_traits::get_previous(members_.nodeptr_); - return result; - } - - friend bool operator== (const list_iterator& l, const list_iterator& r) - { return l.pointed_node() == r.pointed_node(); } - - friend bool operator!= (const list_iterator& l, const list_iterator& r) - { return !(l == r); } - - reference operator*() const - { return *operator->(); } - - pointer operator->() const - { return this->get_real_value_traits()->to_value_ptr(members_.nodeptr_); } - - const Container *get_container() const - { - if(store_container_ptr){ - const Container* c = static_cast(members_.get_ptr()); - BOOST_INTRUSIVE_INVARIANT_ASSERT(c != 0); - return c; - } - 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; - } - - list_iterator unconst() const - { return list_iterator(this->pointed_node(), this->get_container()); } - - private: - struct members - : public detail::select_constptr - ::type - { - typedef typename detail::select_constptr - ::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 - #endif //BOOST_INTRUSIVE_LIST_NODE_HPP diff --git a/boost/intrusive/detail/math.hpp b/boost/intrusive/detail/math.hpp new file mode 100644 index 0000000000..f212ae743f --- /dev/null +++ b/boost/intrusive/detail/math.hpp @@ -0,0 +1,271 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2014 +// +// 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_DETAIL_MATH_HPP +#define BOOST_INTRUSIVE_DETAIL_MATH_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +namespace boost { +namespace intrusive { +namespace detail { + +/////////////////////////// +// floor_log2 Dispatcher +//////////////////////////// + +#if defined(_MSC_VER) && (_MSC_VER >= 1300) + + }}} //namespace boost::intrusive::detail + + //Use _BitScanReverseXX intrinsics + + #if defined(_M_X64) || defined(_M_AMD64) || defined(_M_IA64) //64 bit target + #define BOOST_INTRUSIVE_BSR_INTRINSIC_64_BIT + #endif + + #ifndef __INTRIN_H_ // Avoid including any windows system header + #ifdef __cplusplus + extern "C" { + #endif // __cplusplus + + #if defined(BOOST_INTRUSIVE_BSR_INTRINSIC_64_BIT) //64 bit target + unsigned char _BitScanReverse64(unsigned long *index, unsigned __int64 mask); + #pragma intrinsic(_BitScanReverse64) + #else //32 bit target + unsigned char _BitScanReverse(unsigned long *index, unsigned long mask); + #pragma intrinsic(_BitScanReverse) + #endif + + #ifdef __cplusplus + } + #endif // __cplusplus + #endif // __INTRIN_H_ + + #ifdef BOOST_INTRUSIVE_BSR_INTRINSIC_64_BIT + #define BOOST_INTRUSIVE_BSR_INTRINSIC _BitScanReverse64 + #undef BOOST_INTRUSIVE_BSR_INTRINSIC_64_BIT + #else + #define BOOST_INTRUSIVE_BSR_INTRINSIC _BitScanReverse + #endif + + namespace boost { + namespace intrusive { + namespace detail { + + inline std::size_t floor_log2 (std::size_t x) + { + unsigned long log2; + BOOST_INTRUSIVE_BSR_INTRINSIC( &log2, (unsigned long)x ); + return log2; + } + + #undef BOOST_INTRUSIVE_BSR_INTRINSIC + +#elif defined(__GNUC__) && ((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) //GCC >=3.4 + + //Compile-time error in case of missing specialization + template + struct builtin_clz_dispatch; + + #if defined(BOOST_HAS_LONG_LONG) + template<> + struct builtin_clz_dispatch + { + static unsigned long long call(unsigned long long n) + { return __builtin_clzll(n); } + }; + #endif + + template<> + struct builtin_clz_dispatch + { + static unsigned long call(unsigned long n) + { return __builtin_clzl(n); } + }; + + template<> + struct builtin_clz_dispatch + { + static unsigned int call(unsigned int n) + { return __builtin_clz(n); } + }; + + inline std::size_t floor_log2(std::size_t n) + { + return sizeof(std::size_t)*CHAR_BIT - std::size_t(1) - builtin_clz_dispatch::call(n); + } + +#else //Portable methods + +//////////////////////////// +// Generic method +//////////////////////////// + + inline std::size_t floor_log2_get_shift(std::size_t n, true_ )//power of two size_t + { return n >> 1; } + + inline std::size_t floor_log2_get_shift(std::size_t n, false_ )//non-power of two size_t + { return (n >> 1) + ((n & 1u) & (n != 1)); } + + template + inline std::size_t floor_log2 (std::size_t x, integer) + { + const std::size_t Bits = N; + const bool Size_t_Bits_Power_2= !(Bits & (Bits-1)); + + std::size_t n = x; + std::size_t log2 = 0; + + std::size_t remaining_bits = Bits; + std::size_t shift = floor_log2_get_shift(remaining_bits, bool_()); + while(shift){ + std::size_t tmp = n >> shift; + if (tmp){ + log2 += shift, n = tmp; + } + shift = floor_log2_get_shift(shift, bool_()); + } + + return log2; + } + + //////////////////////////// + // DeBruijn method + //////////////////////////// + + //Taken from: + //http://stackoverflow.com/questions/11376288/fast-computing-of-log2-for-64-bit-integers + //Thanks to Desmond Hume + + inline std::size_t floor_log2 (std::size_t v, integer) + { + static const int MultiplyDeBruijnBitPosition[32] = + { + 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, + 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 + }; + + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + v |= v >> 16; + + return MultiplyDeBruijnBitPosition[(std::size_t)(v * 0x07C4ACDDU) >> 27]; + } + + inline std::size_t floor_log2 (std::size_t v, integer) + { + static const std::size_t MultiplyDeBruijnBitPosition[64] = { + 63, 0, 58, 1, 59, 47, 53, 2, + 60, 39, 48, 27, 54, 33, 42, 3, + 61, 51, 37, 40, 49, 18, 28, 20, + 55, 30, 34, 11, 43, 14, 22, 4, + 62, 57, 46, 52, 38, 26, 32, 41, + 50, 36, 17, 19, 29, 10, 13, 21, + 56, 45, 25, 31, 35, 16, 9, 12, + 44, 24, 15, 8, 23, 7, 6, 5}; + + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + v |= v >> 16; + v |= v >> 32; + return MultiplyDeBruijnBitPosition[((std::size_t)((v - (v >> 1))*0x07EDD5E59A4E28C2ULL)) >> 58]; + } + + + inline std::size_t floor_log2 (std::size_t x) + { + const std::size_t Bits = sizeof(std::size_t)*CHAR_BIT; + return floor_log2(x, integer()); + } + +#endif + +//Thanks to Laurent de Soras in +//http://www.flipcode.com/archives/Fast_log_Function.shtml +inline float fast_log2 (float val) +{ + union caster_t + { + unsigned x; + float val; + } caster; + + caster.val = val; + unsigned x = caster.x; + const int log_2 = int((x >> 23) & 255) - 128; + x &= ~(unsigned(255u) << 23u); + x += unsigned(127) << 23u; + caster.x = x; + val = caster.val; + //1+log2(m), m ranging from 1 to 2 + //3rd degree polynomial keeping first derivate continuity. + //For less precision the line can be commented out + val = ((-1.f/3.f) * val + 2.f) * val - (2.f/3.f); + return val + static_cast(log_2); +} + +inline std::size_t ceil_log2 (std::size_t x) +{ + return static_cast((x & (x-1)) != 0) + floor_log2(x); +} + +template +struct numbits_eq +{ + static const bool value = sizeof(SizeType)*CHAR_BIT == N; +}; + +template +struct sqrt2_pow_max; + +template +struct sqrt2_pow_max >::type> +{ + static const SizeType value = 0xb504f334; + static const std::size_t pow = 31; +}; + +#ifndef BOOST_NO_INT64_T + +template +struct sqrt2_pow_max >::type> +{ + static const SizeType value = 0xb504f333f9de6484ull; + static const std::size_t pow = 63; +}; + +#endif //BOOST_NO_INT64_T + +// Returns floor(pow(sqrt(2), x * 2 + 1)). +// Defined for X from 0 up to the number of bits in size_t minus 1. +inline std::size_t sqrt2_pow_2xplus1 (std::size_t x) +{ + const std::size_t value = (std::size_t)sqrt2_pow_max::value; + const std::size_t pow = (std::size_t)sqrt2_pow_max::pow; + return (value >> (pow - x)) + 1; +} + +} //namespace detail +} //namespace intrusive +} //namespace boost + +#endif //BOOST_INTRUSIVE_DETAIL_MATH_HPP diff --git a/boost/intrusive/detail/memory_util.hpp b/boost/intrusive/detail/memory_util.hpp index 1a6431b078..18a5d3e7e2 100644 --- a/boost/intrusive/detail/memory_util.hpp +++ b/boost/intrusive/detail/memory_util.hpp @@ -6,7 +6,7 @@ // ////////////////////////////////////////////////////////////////////////////// // -// (C) Copyright Ion Gaztanaga 2011-2012. Distributed under the Boost +// (C) Copyright Ion Gaztanaga 2011-2014. 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) // @@ -17,14 +17,14 @@ #ifndef BOOST_INTRUSIVE_ALLOCATOR_MEMORY_UTIL_HPP #define BOOST_INTRUSIVE_ALLOCATOR_MEMORY_UTIL_HPP -#if (defined _MSC_VER) && (_MSC_VER >= 1200) +#if defined(_MSC_VER) # pragma once #endif -#include #include #include #include +#include namespace boost { namespace intrusive { @@ -41,10 +41,6 @@ inline T* addressof(T& obj) ); } -template struct unvoid { typedef T type; }; -template <> struct unvoid { struct type { }; }; -template <> struct unvoid { struct type { }; }; - template struct LowPriorityConversion { @@ -52,61 +48,6 @@ struct LowPriorityConversion LowPriorityConversion(const T&) { } }; -// Infrastructure for providing a default type for T::TNAME if absent. -#define BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(TNAME) \ - template \ - struct boost_intrusive_default_type_ ## TNAME \ - { \ - template \ - static char test(int, typename X::TNAME*); \ - \ - template \ - static int test(boost::intrusive::detail:: \ - LowPriorityConversion, void*); \ - \ - struct DefaultWrap { typedef DefaultType TNAME; }; \ - \ - static const bool value = (1 == sizeof(test(0, 0))); \ - \ - typedef typename \ - ::boost::intrusive::detail::if_c \ - ::type::TNAME type; \ - }; \ - \ - template \ - struct boost_intrusive_eval_default_type_ ## TNAME \ - { \ - template \ - static char test(int, typename X::TNAME*); \ - \ - template \ - static int test(boost::intrusive::detail:: \ - LowPriorityConversion, void*); \ - \ - struct DefaultWrap \ - { typedef typename DefaultType::type TNAME; }; \ - \ - static const bool value = (1 == sizeof(test(0, 0))); \ - \ - typedef typename \ - ::boost::intrusive::detail::eval_if_c \ - < value \ - , ::boost::intrusive::detail::identity \ - , ::boost::intrusive::detail::identity \ - >::type::TNAME type; \ - }; \ -// - -#define BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT(INSTANTIATION_NS_PREFIX, T, TNAME, TIMPL) \ - typename INSTANTIATION_NS_PREFIX \ - boost_intrusive_default_type_ ## TNAME< T, TIMPL >::type \ -// - -#define BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_EVAL_DEFAULT(INSTANTIATION_NS_PREFIX, T, TNAME, TIMPL) \ - typename INSTANTIATION_NS_PREFIX \ - boost_intrusive_eval_default_type_ ## TNAME< T, TIMPL >::type \ -// - }}} //namespace boost::intrusive::detail #include @@ -114,25 +55,25 @@ struct LowPriorityConversion #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME pointer_to #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN namespace boost { namespace intrusive { namespace detail { #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END }}} -#define BOOST_PP_ITERATION_PARAMS_1 (3, (0, 1, )) +#define BOOST_PP_ITERATION_PARAMS_1 (3, (1, 1, )) #include BOOST_PP_ITERATE() #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME static_cast_from #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN namespace boost { namespace intrusive { namespace detail { #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END }}} -#define BOOST_PP_ITERATION_PARAMS_1 (3, (0, 1, )) +#define BOOST_PP_ITERATION_PARAMS_1 (3, (1, 1, )) #include BOOST_PP_ITERATE() #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME const_cast_from #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN namespace boost { namespace intrusive { namespace detail { #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END }}} -#define BOOST_PP_ITERATION_PARAMS_1 (3, (0, 1, )) +#define BOOST_PP_ITERATION_PARAMS_1 (3, (1, 1, )) #include BOOST_PP_ITERATE() #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME dynamic_cast_from #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN namespace boost { namespace intrusive { namespace detail { #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END }}} -#define BOOST_PP_ITERATION_PARAMS_1 (3, (0, 1, )) +#define BOOST_PP_ITERATION_PARAMS_1 (3, (1, 1, )) #include BOOST_PP_ITERATE() namespace boost { @@ -141,148 +82,11 @@ namespace detail { BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(element_type) BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(difference_type) - -////////////////////// -//struct first_param -////////////////////// - -template struct first_param -{ typedef void type; }; - -#if !defined(BOOST_NO_VARIADIC_TEMPLATES) - - template