Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:39

0001 /////////////////////////////////////////////////////////////////////////////
0002 //
0003 // (C) Copyright Ion Gaztanaga  2013-2014
0004 //
0005 // Distributed under the Boost Software License, Version 1.0.
0006 //    (See accompanying file LICENSE_1_0.txt or copy at
0007 //          http://www.boost.org/LICENSE_1_0.txt)
0008 //
0009 // See http://www.boost.org/libs/intrusive for documentation.
0010 //
0011 /////////////////////////////////////////////////////////////////////////////
0012 #ifndef BOOST_INTRUSIVE_BSTREE_HPP
0013 #define BOOST_INTRUSIVE_BSTREE_HPP
0014 
0015 #include <boost/intrusive/detail/config_begin.hpp>
0016 #include <boost/intrusive/intrusive_fwd.hpp>
0017 
0018 #include <boost/intrusive/detail/assert.hpp>
0019 #include <boost/static_assert.hpp>
0020 #include <boost/intrusive/intrusive_fwd.hpp>
0021 #include <boost/intrusive/bs_set_hook.hpp>
0022 #include <boost/intrusive/detail/tree_node.hpp>
0023 #include <boost/intrusive/detail/tree_iterator.hpp>
0024 #include <boost/intrusive/detail/ebo_functor_holder.hpp>
0025 #include <boost/intrusive/detail/mpl.hpp>
0026 #include <boost/intrusive/pointer_traits.hpp>
0027 #include <boost/intrusive/detail/is_stateful_value_traits.hpp>
0028 #include <boost/intrusive/detail/empty_node_checker.hpp>
0029 #include <boost/intrusive/detail/default_header_holder.hpp>
0030 #include <boost/intrusive/detail/reverse_iterator.hpp>
0031 #include <boost/intrusive/detail/exception_disposer.hpp>
0032 #include <boost/intrusive/detail/node_cloner_disposer.hpp>
0033 #include <boost/intrusive/detail/key_nodeptr_comp.hpp>
0034 #include <boost/intrusive/detail/simple_disposers.hpp>
0035 #include <boost/intrusive/detail/size_holder.hpp>
0036 #include <boost/intrusive/detail/algo_type.hpp>
0037 #include <boost/intrusive/detail/algorithm.hpp>
0038 #include <boost/intrusive/detail/tree_value_compare.hpp>
0039 
0040 #include <boost/intrusive/detail/get_value_traits.hpp>
0041 #include <boost/intrusive/bstree_algorithms.hpp>
0042 #include <boost/intrusive/link_mode.hpp>
0043 #include <boost/intrusive/parent_from_member.hpp>
0044 #include <boost/move/utility_core.hpp>
0045 #include <boost/move/adl_move_swap.hpp>
0046 
0047 #include <boost/intrusive/detail/minimal_pair_header.hpp>
0048 #include <cstddef>   //size_t...
0049 #include <boost/intrusive/detail/minimal_less_equal_header.hpp>//less, equal_to
0050 
0051 #if defined(BOOST_HAS_PRAGMA_ONCE)
0052 #  pragma once
0053 #endif
0054 
0055 namespace boost {
0056 namespace intrusive {
0057 
0058 /// @cond
0059 
0060 struct default_bstree_hook_applier
0061 {  template <class T> struct apply{ typedef typename T::default_bstree_hook type;  };  };
0062 
0063 template<>
0064 struct is_default_hook_tag<default_bstree_hook_applier>
0065 {  static const bool value = true;  };
0066 
0067 struct bstree_defaults
0068 {
0069    typedef default_bstree_hook_applier proto_value_traits;
0070    static const bool constant_time_size = true;
0071    typedef std::size_t size_type;
0072    typedef void compare;
0073    typedef void key_of_value;
0074    static const bool floating_point = true;  //For sgtree
0075    typedef void priority;  //For treap
0076    typedef void header_holder_type;
0077 };
0078 
0079 template<class ValueTraits, algo_types AlgoType, typename HeaderHolder>
0080 struct bstbase3
0081 {
0082    typedef ValueTraits                                               value_traits;
0083    typedef typename value_traits::node_traits                        node_traits;
0084    typedef typename node_traits::node                                node_type;
0085    typedef typename get_algo<AlgoType, node_traits>::type            node_algorithms;
0086    typedef typename node_traits::node_ptr                            node_ptr;
0087    typedef typename node_traits::const_node_ptr                      const_node_ptr;
0088    typedef tree_iterator<value_traits, false>                                                   iterator;
0089    typedef tree_iterator<value_traits, true>                                                    const_iterator;
0090    typedef boost::intrusive::reverse_iterator<iterator>                                         reverse_iterator;
0091    typedef boost::intrusive::reverse_iterator<const_iterator>                                   const_reverse_iterator;
0092    typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::pointer)                               pointer;
0093    typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::const_pointer)                         const_pointer;
0094    typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<pointer>::element_type)               value_type;
0095    typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<pointer>::reference)                  reference;
0096    typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<const_pointer>::reference)            const_reference;
0097    typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<const_pointer>::difference_type)      difference_type;
0098    typedef typename detail::get_header_holder_type
0099       < value_traits,HeaderHolder >::type                                                       header_holder_type;
0100 
0101    static const bool safemode_or_autounlink = is_safe_autounlink<value_traits::link_mode>::value;
0102    static const bool stateful_value_traits = detail::is_stateful_value_traits<value_traits>::value;
0103    static const bool has_container_from_iterator =
0104         detail::is_same< header_holder_type, detail::default_header_holder< node_traits > >::value;
0105 
0106    struct holder_t : public ValueTraits
0107    {
0108       BOOST_INTRUSIVE_FORCEINLINE explicit holder_t(const ValueTraits &vtraits)
0109          : ValueTraits(vtraits)
0110       {}
0111       header_holder_type root;
0112    } holder;
0113 
0114    static bstbase3 &get_tree_base_from_end_iterator(const const_iterator &end_iterator)
0115    {
0116       BOOST_STATIC_ASSERT(has_container_from_iterator);
0117       node_ptr p = end_iterator.pointed_node();
0118       header_holder_type* h = header_holder_type::get_holder(p);
0119       holder_t *holder = get_parent_from_member<holder_t, header_holder_type>(h, &holder_t::root);
0120       bstbase3 *base   = get_parent_from_member<bstbase3, holder_t> (holder, &bstbase3::holder);
0121       return *base;
0122    }
0123 
0124    BOOST_INTRUSIVE_FORCEINLINE bstbase3(const ValueTraits &vtraits)
0125       : holder(vtraits)
0126    {
0127       node_algorithms::init_header(this->header_ptr());
0128    }
0129 
0130    BOOST_INTRUSIVE_FORCEINLINE node_ptr header_ptr()
0131    { return holder.root.get_node(); }
0132 
0133    BOOST_INTRUSIVE_FORCEINLINE const_node_ptr header_ptr() const
0134    { return holder.root.get_node(); }
0135 
0136    BOOST_INTRUSIVE_FORCEINLINE const value_traits &get_value_traits() const
0137    {  return this->holder;  }
0138 
0139    BOOST_INTRUSIVE_FORCEINLINE value_traits &get_value_traits()
0140    {  return this->holder;  }
0141 
0142    typedef typename boost::intrusive::value_traits_pointers
0143       <ValueTraits>::const_value_traits_ptr const_value_traits_ptr;
0144 
0145    BOOST_INTRUSIVE_FORCEINLINE const_value_traits_ptr priv_value_traits_ptr() const
0146    {  return pointer_traits<const_value_traits_ptr>::pointer_to(this->get_value_traits());  }
0147 
0148    BOOST_INTRUSIVE_FORCEINLINE iterator begin() BOOST_NOEXCEPT
0149    {  return iterator(node_algorithms::begin_node(this->header_ptr()), this->priv_value_traits_ptr());   }
0150 
0151    BOOST_INTRUSIVE_FORCEINLINE const_iterator begin() const BOOST_NOEXCEPT
0152    {  return cbegin();   }
0153 
0154    BOOST_INTRUSIVE_FORCEINLINE const_iterator cbegin() const BOOST_NOEXCEPT
0155    {  return const_iterator(node_algorithms::begin_node(this->header_ptr()), this->priv_value_traits_ptr());   }
0156 
0157    BOOST_INTRUSIVE_FORCEINLINE iterator end() BOOST_NOEXCEPT
0158    {  return iterator(node_algorithms::end_node(this->header_ptr()), this->priv_value_traits_ptr());   }
0159 
0160    BOOST_INTRUSIVE_FORCEINLINE const_iterator end() const BOOST_NOEXCEPT
0161    {  return cend();  }
0162 
0163    BOOST_INTRUSIVE_FORCEINLINE const_iterator cend() const BOOST_NOEXCEPT
0164    {  return const_iterator(node_algorithms::end_node(this->header_ptr()), this->priv_value_traits_ptr());   }
0165 
0166    BOOST_INTRUSIVE_FORCEINLINE iterator root()
0167    {  return iterator(node_algorithms::root_node(this->header_ptr()), this->priv_value_traits_ptr());   }
0168 
0169    BOOST_INTRUSIVE_FORCEINLINE const_iterator root() const
0170    {  return croot();   }
0171 
0172    BOOST_INTRUSIVE_FORCEINLINE const_iterator croot() const
0173    {  return const_iterator(node_algorithms::root_node(this->header_ptr()), this->priv_value_traits_ptr());   }
0174 
0175    BOOST_INTRUSIVE_FORCEINLINE reverse_iterator rbegin()
0176    {  return reverse_iterator(end());  }
0177 
0178    BOOST_INTRUSIVE_FORCEINLINE const_reverse_iterator rbegin() const
0179    {  return const_reverse_iterator(end());  }
0180 
0181    BOOST_INTRUSIVE_FORCEINLINE const_reverse_iterator crbegin() const
0182    {  return const_reverse_iterator(end());  }
0183 
0184    BOOST_INTRUSIVE_FORCEINLINE reverse_iterator rend()
0185    {  return reverse_iterator(begin());   }
0186 
0187    BOOST_INTRUSIVE_FORCEINLINE const_reverse_iterator rend() const
0188    {  return const_reverse_iterator(begin());   }
0189 
0190    BOOST_INTRUSIVE_FORCEINLINE const_reverse_iterator crend() const
0191    {  return const_reverse_iterator(begin());   }
0192 
0193    void replace_node(iterator replace_this, reference with_this)
0194    {
0195       node_algorithms::replace_node( get_value_traits().to_node_ptr(*replace_this)
0196                                    , this->header_ptr()
0197                                    , get_value_traits().to_node_ptr(with_this));
0198       BOOST_IF_CONSTEXPR(safemode_or_autounlink)
0199          node_algorithms::init(replace_this.pointed_node());
0200    }
0201 
0202    BOOST_INTRUSIVE_FORCEINLINE void rebalance() BOOST_NOEXCEPT
0203    {  node_algorithms::rebalance(this->header_ptr()); }
0204 
0205    iterator rebalance_subtree(iterator r) BOOST_NOEXCEPT
0206    {  return iterator(node_algorithms::rebalance_subtree(r.pointed_node()), this->priv_value_traits_ptr()); }
0207 
0208    static iterator s_iterator_to(reference value) BOOST_NOEXCEPT
0209    {
0210       BOOST_STATIC_ASSERT((!stateful_value_traits));
0211       return iterator (value_traits::to_node_ptr(value), const_value_traits_ptr());
0212    }
0213 
0214    static const_iterator s_iterator_to(const_reference value) BOOST_NOEXCEPT
0215    {
0216       BOOST_STATIC_ASSERT((!stateful_value_traits));
0217       return const_iterator (value_traits::to_node_ptr(*pointer_traits<pointer>::const_cast_from(pointer_traits<const_pointer>::pointer_to(value))), const_value_traits_ptr());
0218    }
0219 
0220    iterator iterator_to(reference value) BOOST_NOEXCEPT
0221    {  return iterator (this->get_value_traits().to_node_ptr(value), this->priv_value_traits_ptr()); }
0222 
0223    const_iterator iterator_to(const_reference value) const BOOST_NOEXCEPT
0224    {  return const_iterator (this->get_value_traits().to_node_ptr(*pointer_traits<pointer>::const_cast_from(pointer_traits<const_pointer>::pointer_to(value))), this->priv_value_traits_ptr()); }
0225 
0226    BOOST_INTRUSIVE_FORCEINLINE static void init_node(reference value)
0227    { node_algorithms::init(value_traits::to_node_ptr(value)); }
0228 
0229 };
0230 
0231 template<class Less, class T>
0232 struct get_compare
0233 {
0234    typedef Less type;
0235 };
0236 
0237 template<class T>
0238 struct get_compare<void, T>
0239 {
0240    typedef ::std::less<T> type;
0241 };
0242 
0243 template<class KeyOfValue, class T>
0244 struct get_key_of_value
0245 {
0246    typedef KeyOfValue type;
0247 };
0248 
0249 template<class T>
0250 struct get_key_of_value<void, T>
0251 {
0252    typedef ::boost::intrusive::detail::identity<T> type;
0253 };
0254 
0255 template<class ValuePtr, class VoidOrKeyOfValue, class VoidOrKeyComp>
0256 struct bst_key_types
0257 {
0258    typedef typename
0259       boost::movelib::pointer_element<ValuePtr>::type value_type;
0260    typedef typename get_key_of_value
0261       < VoidOrKeyOfValue, value_type>::type           key_of_value;
0262    typedef typename key_of_value::type                key_type;
0263    typedef typename get_compare< VoidOrKeyComp
0264                       , key_type
0265                       >::type                         key_compare;
0266    typedef tree_value_compare
0267       <ValuePtr, key_compare, key_of_value>           value_compare;
0268 };
0269 
0270 template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyComp, algo_types AlgoType, typename HeaderHolder>
0271 struct bstbase2
0272    //Put the (possibly empty) functor in the first position to get EBO in MSVC
0273    //Use public inheritance to avoid MSVC bugs with closures
0274    : public detail::ebo_functor_holder
0275             < typename bst_key_types
0276                < typename ValueTraits::pointer
0277                , VoidOrKeyOfValue
0278                , VoidOrKeyComp
0279                
0280                >::value_compare
0281             >
0282    , public bstbase3<ValueTraits, AlgoType, HeaderHolder>
0283 {
0284    typedef bstbase3<ValueTraits, AlgoType, HeaderHolder>             treeheader_t;
0285    typedef bst_key_types< typename ValueTraits::pointer
0286                         , VoidOrKeyOfValue
0287                         , VoidOrKeyComp>                             key_types;
0288    typedef typename treeheader_t::value_traits                       value_traits;
0289    typedef typename treeheader_t::node_algorithms                    node_algorithms;
0290    typedef typename ValueTraits::value_type                          value_type;
0291    typedef typename key_types::key_type                              key_type;
0292    typedef typename key_types::key_of_value                          key_of_value;
0293    typedef typename key_types::key_compare                           key_compare;
0294    typedef typename key_types::value_compare                         value_compare;
0295    typedef typename treeheader_t::iterator                           iterator;
0296    typedef typename treeheader_t::const_iterator                     const_iterator;
0297    typedef typename treeheader_t::node_ptr                           node_ptr;
0298    typedef typename treeheader_t::const_node_ptr                     const_node_ptr;
0299 
0300    bstbase2(const key_compare &comp, const ValueTraits &vtraits)
0301       : detail::ebo_functor_holder<value_compare>(value_compare(comp)), treeheader_t(vtraits)
0302    {}
0303 
0304    const value_compare &get_comp() const
0305    {  return this->get();  }
0306 
0307    value_compare &get_comp()
0308    {  return this->get();  }
0309 
0310    typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::pointer)                               pointer;
0311    typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::const_pointer)                         const_pointer;
0312    typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<pointer>::reference)                  reference;
0313    typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<const_pointer>::reference)            const_reference;
0314    typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<const_pointer>::difference_type)      difference_type;
0315    typedef typename node_algorithms::insert_commit_data insert_commit_data;
0316 
0317    BOOST_INTRUSIVE_FORCEINLINE value_compare value_comp() const
0318    {  return this->get_comp();   }
0319 
0320    BOOST_INTRUSIVE_FORCEINLINE key_compare key_comp() const
0321    {  return this->get_comp().key_comp();   }
0322 
0323    //lower_bound
0324    BOOST_INTRUSIVE_FORCEINLINE iterator lower_bound(const key_type &key)
0325    {  return this->lower_bound(key, this->key_comp());   }
0326 
0327    BOOST_INTRUSIVE_FORCEINLINE const_iterator lower_bound(const key_type &key) const
0328    {  return this->lower_bound(key, this->key_comp());   }
0329 
0330    template<class KeyType, class KeyTypeKeyCompare>
0331    iterator lower_bound(const KeyType &key, KeyTypeKeyCompare comp)
0332    {
0333       return iterator(node_algorithms::lower_bound
0334          (this->header_ptr(), key, this->key_node_comp(comp)), this->priv_value_traits_ptr());
0335    }
0336 
0337    template<class KeyType, class KeyTypeKeyCompare>
0338    const_iterator lower_bound(const KeyType &key, KeyTypeKeyCompare comp) const
0339    {
0340       return const_iterator(node_algorithms::lower_bound
0341          (this->header_ptr(), key, this->key_node_comp(comp)), this->priv_value_traits_ptr());
0342    }
0343 
0344    //upper_bound
0345    BOOST_INTRUSIVE_FORCEINLINE iterator upper_bound(const key_type &key)
0346    {  return this->upper_bound(key, this->key_comp());   }
0347 
0348    template<class KeyType, class KeyTypeKeyCompare>
0349    iterator upper_bound(const KeyType &key, KeyTypeKeyCompare comp)
0350    {
0351       return iterator(node_algorithms::upper_bound
0352          (this->header_ptr(), key, this->key_node_comp(comp)), this->priv_value_traits_ptr());
0353    }
0354 
0355    BOOST_INTRUSIVE_FORCEINLINE const_iterator upper_bound(const key_type &key) const
0356    {  return this->upper_bound(key, this->key_comp());   }
0357 
0358    template<class KeyType, class KeyTypeKeyCompare>
0359    const_iterator upper_bound(const KeyType &key, KeyTypeKeyCompare comp) const
0360    {
0361       return const_iterator(node_algorithms::upper_bound
0362          (this->header_ptr(), key, this->key_node_comp(comp)), this->priv_value_traits_ptr());
0363    }
0364 
0365    template<class KeyTypeKeyCompare>
0366    struct key_node_comp_ret
0367    {  typedef detail::key_nodeptr_comp<KeyTypeKeyCompare, value_traits, key_of_value> type;  };
0368 
0369    template<class KeyTypeKeyCompare>
0370    BOOST_INTRUSIVE_FORCEINLINE typename key_node_comp_ret<KeyTypeKeyCompare>::type key_node_comp(KeyTypeKeyCompare comp) const
0371    {
0372       return detail::key_nodeptr_comp<KeyTypeKeyCompare, value_traits, key_of_value>(comp, &this->get_value_traits());
0373    }
0374 
0375    //find
0376    BOOST_INTRUSIVE_FORCEINLINE iterator find(const key_type &key)
0377    {  return this->find(key, this->key_comp()); }
0378 
0379    template<class KeyType, class KeyTypeKeyCompare>
0380    iterator find(const KeyType &key, KeyTypeKeyCompare comp)
0381    {
0382       return iterator
0383          (node_algorithms::find(this->header_ptr(), key, this->key_node_comp(comp)), this->priv_value_traits_ptr());
0384    }
0385 
0386    BOOST_INTRUSIVE_FORCEINLINE const_iterator find(const key_type &key) const
0387    {  return this->find(key, this->key_comp()); }
0388 
0389    template<class KeyType, class KeyTypeKeyCompare>
0390    const_iterator find(const KeyType &key, KeyTypeKeyCompare comp) const
0391    {
0392       return const_iterator
0393          (node_algorithms::find(this->header_ptr(), key, this->key_node_comp(comp)), this->priv_value_traits_ptr());
0394    }
0395 
0396    //equal_range
0397    BOOST_INTRUSIVE_FORCEINLINE std::pair<iterator,iterator> equal_range(const key_type &key)
0398    {  return this->equal_range(key, this->key_comp());   }
0399 
0400    template<class KeyType, class KeyTypeKeyCompare>
0401    std::pair<iterator,iterator> equal_range(const KeyType &key, KeyTypeKeyCompare comp)
0402    {
0403       std::pair<node_ptr, node_ptr> ret = 
0404          node_algorithms::equal_range(this->header_ptr(), key, this->key_node_comp(comp));
0405       return std::pair<iterator, iterator>( iterator(ret.first, this->priv_value_traits_ptr())
0406                                           , iterator(ret.second, this->priv_value_traits_ptr()));
0407    }
0408 
0409    BOOST_INTRUSIVE_FORCEINLINE std::pair<const_iterator, const_iterator>
0410       equal_range(const key_type &key) const
0411    {  return this->equal_range(key, this->key_comp());   }
0412 
0413    template<class KeyType, class KeyTypeKeyCompare>
0414    std::pair<const_iterator, const_iterator>
0415       equal_range(const KeyType &key, KeyTypeKeyCompare comp) const
0416    {
0417       std::pair<node_ptr, node_ptr> ret =
0418          node_algorithms::equal_range(this->header_ptr(), key, this->key_node_comp(comp));
0419       return std::pair<const_iterator, const_iterator>( const_iterator(ret.first, this->priv_value_traits_ptr())
0420                                                       , const_iterator(ret.second, this->priv_value_traits_ptr()));
0421    }
0422 
0423    //lower_bound_range
0424    BOOST_INTRUSIVE_FORCEINLINE std::pair<iterator,iterator> lower_bound_range(const key_type &key)
0425    {  return this->lower_bound_range(key, this->key_comp());   }
0426 
0427    template<class KeyType, class KeyTypeKeyCompare>
0428    std::pair<iterator,iterator> lower_bound_range(const KeyType &key, KeyTypeKeyCompare comp)
0429    {
0430       std::pair<node_ptr, node_ptr> ret =
0431          node_algorithms::lower_bound_range(this->header_ptr(), key, this->key_node_comp(comp));
0432       return std::pair<iterator, iterator>( iterator(ret.first, this->priv_value_traits_ptr())
0433                                           , iterator(ret.second, this->priv_value_traits_ptr()));
0434    }
0435 
0436    BOOST_INTRUSIVE_FORCEINLINE std::pair<const_iterator, const_iterator>
0437       lower_bound_range(const key_type &key) const
0438    {  return this->lower_bound_range(key, this->key_comp());   }
0439 
0440    template<class KeyType, class KeyTypeKeyCompare>
0441    std::pair<const_iterator, const_iterator>
0442       lower_bound_range(const KeyType &key, KeyTypeKeyCompare comp) const
0443    {
0444       std::pair<node_ptr, node_ptr> ret =
0445          node_algorithms::lower_bound_range(this->header_ptr(), key, this->key_node_comp(comp));
0446       return std::pair<const_iterator, const_iterator>( const_iterator(ret.first, this->priv_value_traits_ptr())
0447                                                       , const_iterator(ret.second, this->priv_value_traits_ptr()));
0448    }
0449 
0450    //bounded_range
0451    BOOST_INTRUSIVE_FORCEINLINE std::pair<iterator,iterator> bounded_range
0452       (const key_type &lower_key, const key_type &upper_key, bool left_closed, bool right_closed)
0453    {  return this->bounded_range(lower_key, upper_key, this->key_comp(), left_closed, right_closed);   }
0454 
0455    template<class KeyType, class KeyTypeKeyCompare>
0456    std::pair<iterator,iterator> bounded_range
0457       (const KeyType &lower_key, const KeyType &upper_key, KeyTypeKeyCompare comp, bool left_closed, bool right_closed)
0458    {
0459       std::pair<node_ptr, node_ptr> ret =
0460          node_algorithms::bounded_range
0461             (this->header_ptr(), lower_key, upper_key, this->key_node_comp(comp), left_closed, right_closed);
0462       return std::pair<iterator, iterator>( iterator(ret.first, this->priv_value_traits_ptr())
0463                                           , iterator(ret.second, this->priv_value_traits_ptr()));
0464    }
0465 
0466    BOOST_INTRUSIVE_FORCEINLINE std::pair<const_iterator,const_iterator> bounded_range
0467       (const key_type &lower_key, const key_type &upper_key, bool left_closed, bool right_closed) const
0468    {  return this->bounded_range(lower_key, upper_key, this->key_comp(), left_closed, right_closed);   }
0469 
0470    template<class KeyType, class KeyTypeKeyCompare>
0471    std::pair<const_iterator,const_iterator> bounded_range
0472       (const KeyType &lower_key, const KeyType &upper_key, KeyTypeKeyCompare comp, bool left_closed, bool right_closed) const
0473    {
0474       std::pair<node_ptr, node_ptr> ret =
0475          node_algorithms::bounded_range
0476             (this->header_ptr(), lower_key, upper_key, this->key_node_comp(comp), left_closed, right_closed);
0477       return std::pair<const_iterator, const_iterator>( const_iterator(ret.first, this->priv_value_traits_ptr())
0478                                                       , const_iterator(ret.second, this->priv_value_traits_ptr()));
0479    }
0480 
0481    //insert_unique_check
0482    BOOST_INTRUSIVE_FORCEINLINE std::pair<iterator, bool> insert_unique_check
0483       (const key_type &key, insert_commit_data &commit_data)
0484    {  return this->insert_unique_check(key, this->key_comp(), commit_data);   }
0485 
0486    BOOST_INTRUSIVE_FORCEINLINE std::pair<iterator, bool> insert_unique_check
0487       (const_iterator hint, const key_type &key, insert_commit_data &commit_data)
0488    {  return this->insert_unique_check(hint, key, this->key_comp(), commit_data);   }
0489 
0490    template<class KeyType, class KeyTypeKeyCompare>
0491    BOOST_INTRUSIVE_DOC1ST(std::pair<iterator BOOST_INTRUSIVE_I bool>
0492       , typename detail::disable_if_convertible
0493          <KeyType BOOST_INTRUSIVE_I const_iterator BOOST_INTRUSIVE_I 
0494          std::pair<iterator BOOST_INTRUSIVE_I bool> >::type)
0495       insert_unique_check
0496       (const KeyType &key, KeyTypeKeyCompare comp, insert_commit_data &commit_data)
0497    {
0498       std::pair<node_ptr, bool> ret =
0499          (node_algorithms::insert_unique_check
0500             (this->header_ptr(), key, this->key_node_comp(comp), commit_data));
0501       return std::pair<iterator, bool>(iterator(ret.first, this->priv_value_traits_ptr()), ret.second);
0502    }
0503 
0504    template<class KeyType, class KeyTypeKeyCompare>
0505    std::pair<iterator, bool> insert_unique_check
0506       (const_iterator hint, const KeyType &key, KeyTypeKeyCompare comp, insert_commit_data &commit_data)
0507    {
0508       std::pair<node_ptr, bool> ret =
0509          (node_algorithms::insert_unique_check
0510             (this->header_ptr(), hint.pointed_node(), key, this->key_node_comp(comp), commit_data));
0511       return std::pair<iterator, bool>(iterator(ret.first, this->priv_value_traits_ptr()), ret.second);
0512    }
0513 };
0514 
0515 //Due to MSVC's EBO implementation, to save space and maintain the ABI, we must put the non-empty size member
0516 //in the first position, but if size is not going to be stored then we'll use an specialization
0517 //that doesn't inherit from size_holder
0518 template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyComp, bool ConstantTimeSize, class SizeType, algo_types AlgoType, typename HeaderHolder>
0519 struct bstbase_hack
0520    : public detail::size_holder<ConstantTimeSize, SizeType>
0521    , public bstbase2 < ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, AlgoType, HeaderHolder>
0522 {
0523    typedef bstbase2< ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, AlgoType, HeaderHolder> base_type;
0524    typedef typename base_type::key_compare         key_compare;
0525    typedef typename base_type::value_compare       value_compare;
0526    typedef SizeType                                size_type;
0527    typedef typename base_type::node_traits         node_traits;
0528    typedef typename get_algo
0529       <AlgoType, node_traits>::type                algo_type;
0530 
0531    BOOST_INTRUSIVE_FORCEINLINE bstbase_hack(const key_compare & comp, const ValueTraits &vtraits)
0532       : base_type(comp, vtraits)
0533    {
0534       this->sz_traits().set_size(size_type(0));
0535    }
0536 
0537    typedef detail::size_holder<ConstantTimeSize, SizeType>     size_traits;
0538 
0539    BOOST_INTRUSIVE_FORCEINLINE size_traits &sz_traits()
0540    {  return static_cast<size_traits &>(*this);  }
0541 
0542    BOOST_INTRUSIVE_FORCEINLINE const size_traits &sz_traits() const
0543    {  return static_cast<const size_traits &>(*this);  }
0544 };
0545 
0546 //Specialization for ConstantTimeSize == false
0547 template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyComp, class SizeType, algo_types AlgoType, typename HeaderHolder>
0548 struct bstbase_hack<ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, false, SizeType, AlgoType, HeaderHolder>
0549    : public bstbase2 < ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, AlgoType, HeaderHolder>
0550 {
0551    typedef bstbase2< ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, AlgoType, HeaderHolder> base_type;
0552    typedef typename base_type::value_compare       value_compare;
0553    typedef typename base_type::key_compare         key_compare;
0554    BOOST_INTRUSIVE_FORCEINLINE bstbase_hack(const key_compare & comp, const ValueTraits &vtraits)
0555       : base_type(comp, vtraits)
0556    {}
0557 
0558    typedef detail::size_holder<false, SizeType>     size_traits;
0559 
0560    BOOST_INTRUSIVE_FORCEINLINE size_traits sz_traits() const
0561    {  return size_traits();  }
0562 };
0563 
0564 //This class will
0565 template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyComp, bool ConstantTimeSize, class SizeType, algo_types AlgoType, typename HeaderHolder>
0566 struct bstbase
0567    : public bstbase_hack< ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, ConstantTimeSize, SizeType, AlgoType, HeaderHolder>
0568 {
0569    typedef bstbase_hack< ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, ConstantTimeSize, SizeType, AlgoType, HeaderHolder> base_type;
0570    typedef ValueTraits                             value_traits;
0571    typedef typename base_type::value_compare       value_compare;
0572    typedef typename base_type::key_compare         key_compare;
0573    typedef typename base_type::const_reference     const_reference;
0574    typedef typename base_type::reference           reference;
0575    typedef typename base_type::iterator            iterator;
0576    typedef typename base_type::const_iterator      const_iterator;
0577    typedef typename base_type::node_traits         node_traits;
0578    typedef typename get_algo
0579       <AlgoType, node_traits>::type                node_algorithms;
0580    typedef SizeType                                size_type;
0581 
0582    BOOST_INTRUSIVE_FORCEINLINE bstbase(const key_compare & comp, const ValueTraits &vtraits)
0583       : base_type(comp, vtraits)
0584    {}
0585 
0586    //Detach all inserted nodes. This will add exception safety to bstree_impl
0587    //constructors inserting elements.
0588    ~bstbase()
0589    {
0590       if(is_safe_autounlink<value_traits::link_mode>::value){
0591          node_algorithms::clear_and_dispose
0592             ( this->header_ptr()
0593             , detail::node_disposer<detail::null_disposer, value_traits, AlgoType>
0594                (detail::null_disposer(), &this->get_value_traits()));
0595          node_algorithms::init(this->header_ptr());
0596       }
0597    }
0598 };
0599 
0600 
0601 /// @endcond
0602 
0603 //! The class template bstree is an unbalanced intrusive binary search tree
0604 //! container. The no-throw guarantee holds only, if the key_compare object
0605 //! doesn't throw.
0606 //!
0607 //! The complexity guarantees only hold if the tree is balanced, logarithmic
0608 //! complexity would increase to linear if the tree is totally unbalanced.
0609 //!
0610 //! The template parameter \c T is the type to be managed by the container.
0611 //! The user can specify additional options and if no options are provided
0612 //! default options are used.
0613 //!
0614 //! The container supports the following options:
0615 //! \c base_hook<>/member_hook<>/value_traits<>,
0616 //! \c constant_time_size<>, \c size_type<> and
0617 //! \c compare<>.
0618 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
0619 template<class T, class ...Options>
0620 #else
0621 template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyComp, class SizeType, bool ConstantTimeSize, algo_types AlgoType, typename HeaderHolder>
0622 #endif
0623 class bstree_impl
0624    :  public bstbase<ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, ConstantTimeSize, SizeType, AlgoType, HeaderHolder>
0625 {
0626    public:
0627    /// @cond
0628    typedef bstbase<ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, ConstantTimeSize, SizeType, AlgoType, HeaderHolder> data_type;
0629    typedef tree_iterator<ValueTraits, false> iterator_type;
0630    typedef tree_iterator<ValueTraits, true>  const_iterator_type;
0631    /// @endcond
0632 
0633    typedef BOOST_INTRUSIVE_IMPDEF(ValueTraits)                                                  value_traits;
0634    typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::pointer)                               pointer;
0635    typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::const_pointer)                         const_pointer;
0636    typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<pointer>::element_type)               value_type;
0637    typedef BOOST_INTRUSIVE_IMPDEF(typename data_type::key_type)                                 key_type;
0638    typedef BOOST_INTRUSIVE_IMPDEF(typename data_type::key_of_value)                             key_of_value;
0639    typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<pointer>::reference)                  reference;
0640    typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<const_pointer>::reference)            const_reference;
0641    typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<const_pointer>::difference_type)      difference_type;
0642    typedef BOOST_INTRUSIVE_IMPDEF(SizeType)                                                     size_type;
0643    typedef BOOST_INTRUSIVE_IMPDEF(typename data_type::value_compare)                            value_compare;
0644    typedef BOOST_INTRUSIVE_IMPDEF(typename data_type::key_compare)                              key_compare;
0645    typedef BOOST_INTRUSIVE_IMPDEF(iterator_type)                                                iterator;
0646    typedef BOOST_INTRUSIVE_IMPDEF(const_iterator_type)                                          const_iterator;
0647    typedef BOOST_INTRUSIVE_IMPDEF(boost::intrusive::reverse_iterator<iterator>)                 reverse_iterator;
0648    typedef BOOST_INTRUSIVE_IMPDEF(boost::intrusive::reverse_iterator<const_iterator>)           const_reverse_iterator;
0649    typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::node_traits)                           node_traits;
0650    typedef BOOST_INTRUSIVE_IMPDEF(typename node_traits::node)                                   node;
0651    typedef BOOST_INTRUSIVE_IMPDEF(typename node_traits::node_ptr)                               node_ptr;
0652    typedef BOOST_INTRUSIVE_IMPDEF(typename node_traits::const_node_ptr)                         const_node_ptr;
0653    /// @cond
0654    typedef typename get_algo<AlgoType, node_traits>::type                                       algo_type;
0655    /// @endcond
0656    typedef BOOST_INTRUSIVE_IMPDEF(algo_type)                                                    node_algorithms;
0657 
0658    static const bool constant_time_size = ConstantTimeSize;
0659    static const bool stateful_value_traits = detail::is_stateful_value_traits<value_traits>::value;
0660    /// @cond
0661    private:
0662 
0663    //noncopyable
0664    BOOST_MOVABLE_BUT_NOT_COPYABLE(bstree_impl)
0665 
0666    static const bool safemode_or_autounlink = is_safe_autounlink<value_traits::link_mode>::value;
0667 
0668    //Constant-time size is incompatible with auto-unlink hooks!
0669    BOOST_STATIC_ASSERT(!(constant_time_size && ((int)value_traits::link_mode == (int)auto_unlink)));
0670 
0671 
0672    protected:
0673 
0674 
0675    /// @endcond
0676 
0677    public:
0678 
0679    typedef typename node_algorithms::insert_commit_data insert_commit_data;
0680 
0681    //! <b>Effects</b>: Constructs an empty container.
0682    //!
0683    //! <b>Complexity</b>: Constant.
0684    //!
0685    //! <b>Throws</b>: If value_traits::node_traits::node
0686    //!   constructor throws (this does not happen with predefined Boost.Intrusive hooks)
0687    //!   or the copy constructor of the key_compare object throws. Basic guarantee.
0688    bstree_impl()
0689       :  data_type(key_compare(), value_traits())
0690    {}
0691 
0692    //! <b>Effects</b>: Constructs an empty container with given comparison and traits.
0693    //!
0694    //! <b>Complexity</b>: Constant.
0695    //!
0696    //! <b>Throws</b>: If value_traits::node_traits::node
0697    //!   constructor throws (this does not happen with predefined Boost.Intrusive hooks)
0698    //!   or the copy constructor of the key_compare object throws. Basic guarantee.
0699    explicit bstree_impl( const key_compare &cmp, const value_traits &v_traits = value_traits())
0700       :  data_type(cmp, v_traits)
0701    {}
0702 
0703    //! <b>Requires</b>: Dereferencing iterator must yield an lvalue of type value_type.
0704    //!   cmp must be a comparison function that induces a strict weak ordering.
0705    //!
0706    //! <b>Effects</b>: Constructs an empty container and inserts elements from
0707    //!   [b, e).
0708    //!
0709    //! <b>Complexity</b>: Linear in N if [b, e) is already sorted using
0710    //!   comp and otherwise N * log N, where N is the distance between first and last.
0711    //!
0712    //! <b>Throws</b>: If value_traits::node_traits::node
0713    //!   constructor throws (this does not happen with predefined Boost.Intrusive hooks)
0714    //!   or the copy constructor/operator() of the key_compare object throws. Basic guarantee.
0715    template<class Iterator>
0716    bstree_impl( bool unique, Iterator b, Iterator e
0717               , const key_compare &cmp     = key_compare()
0718               , const value_traits &v_traits = value_traits())
0719       : data_type(cmp, v_traits)
0720    {
0721       //bstbase releases elements in case of exceptions
0722       if(unique)
0723          this->insert_unique(b, e);
0724       else
0725          this->insert_equal(b, e);
0726    }
0727 
0728    //! <b>Effects</b>: Constructs a container moving resources from another container.
0729    //!   Internal comparison object and value traits are move constructed and
0730    //!   nodes belonging to x (except the node representing the "end") are linked to *this.
0731    //!
0732    //! <b>Complexity</b>: Constant.
0733    //!
0734    //! <b>Throws</b>: If value_traits::node_traits::node's
0735    //!   move constructor throws (this does not happen with predefined Boost.Intrusive hooks)
0736    //!   or the move constructor of the comparison objet throws.
0737    bstree_impl(BOOST_RV_REF(bstree_impl) x)
0738       : data_type(::boost::move(x.get_comp()), ::boost::move(x.get_value_traits()))
0739    {
0740       this->swap(x);
0741    }
0742 
0743    //! <b>Effects</b>: Equivalent to swap
0744    //!
0745    BOOST_INTRUSIVE_FORCEINLINE bstree_impl& operator=(BOOST_RV_REF(bstree_impl) x)
0746    {  this->swap(x); return *this;  }
0747 
0748    #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
0749    //! <b>Effects</b>: Detaches all elements from this. The objects in the set
0750    //!   are not deleted (i.e. no destructors are called), but the nodes according to
0751    //!   the value_traits template parameter are reinitialized and thus can be reused.
0752    //!
0753    //! <b>Complexity</b>: Linear to elements contained in *this.
0754    //!
0755    //! <b>Throws</b>: Nothing.
0756    ~bstree_impl()
0757    {}
0758 
0759    //! <b>Effects</b>: Returns an iterator pointing to the beginning of the container.
0760    //!
0761    //! <b>Complexity</b>: Constant.
0762    //!
0763    //! <b>Throws</b>: Nothing.
0764    iterator begin() BOOST_NOEXCEPT;
0765 
0766    //! <b>Effects</b>: Returns a const_iterator pointing to the beginning of the container.
0767    //!
0768    //! <b>Complexity</b>: Constant.
0769    //!
0770    //! <b>Throws</b>: Nothing.
0771    const_iterator begin() const BOOST_NOEXCEPT;
0772 
0773    //! <b>Effects</b>: Returns a const_iterator pointing to the beginning of the container.
0774    //!
0775    //! <b>Complexity</b>: Constant.
0776    //!
0777    //! <b>Throws</b>: Nothing.
0778    const_iterator cbegin() const BOOST_NOEXCEPT;
0779 
0780    //! <b>Effects</b>: Returns an iterator pointing to the end of the container.
0781    //!
0782    //! <b>Complexity</b>: Constant.
0783    //!
0784    //! <b>Throws</b>: Nothing.
0785    iterator end() BOOST_NOEXCEPT;
0786 
0787    //! <b>Effects</b>: Returns a const_iterator pointing to the end of the container.
0788    //!
0789    //! <b>Complexity</b>: Constant.
0790    //!
0791    //! <b>Throws</b>: Nothing.
0792    const_iterator end() const BOOST_NOEXCEPT;
0793 
0794    //! <b>Effects</b>: Returns a const_iterator pointing to the end of the container.
0795    //!
0796    //! <b>Complexity</b>: Constant.
0797    //!
0798    //! <b>Throws</b>: Nothing.
0799    const_iterator cend() const BOOST_NOEXCEPT;
0800 
0801    //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning of the
0802    //!    reversed container.
0803    //!
0804    //! <b>Complexity</b>: Constant.
0805    //!
0806    //! <b>Throws</b>: Nothing.
0807    reverse_iterator rbegin() BOOST_NOEXCEPT;
0808 
0809    //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
0810    //!    of the reversed container.
0811    //!
0812    //! <b>Complexity</b>: Constant.
0813    //!
0814    //! <b>Throws</b>: Nothing.
0815    const_reverse_iterator rbegin() const BOOST_NOEXCEPT;
0816 
0817    //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
0818    //!    of the reversed container.
0819    //!
0820    //! <b>Complexity</b>: Constant.
0821    //!
0822    //! <b>Throws</b>: Nothing.
0823    const_reverse_iterator crbegin() const BOOST_NOEXCEPT;
0824 
0825    //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
0826    //!    of the reversed container.
0827    //!
0828    //! <b>Complexity</b>: Constant.
0829    //!
0830    //! <b>Throws</b>: Nothing.
0831    reverse_iterator rend() BOOST_NOEXCEPT;
0832 
0833    //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
0834    //!    of the reversed container.
0835    //!
0836    //! <b>Complexity</b>: Constant.
0837    //!
0838    //! <b>Throws</b>: Nothing.
0839    const_reverse_iterator rend() const BOOST_NOEXCEPT;
0840 
0841    //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
0842    //!    of the reversed container.
0843    //!
0844    //! <b>Complexity</b>: Constant.
0845    //!
0846    //! <b>Throws</b>: Nothing.
0847    const_reverse_iterator crend() const BOOST_NOEXCEPT;
0848 
0849    //! <b>Effects</b>: Returns a iterator pointing to the root node of the container or end() if not present.
0850    //!
0851    //! <b>Complexity</b>: Constant.
0852    //!
0853    //! <b>Throws</b>: Nothing.
0854    iterator root() BOOST_NOEXCEPT;
0855 
0856    //! <b>Effects</b>: Returns a const_iterator pointing to the root node of the container or cend() if not present.
0857    //!
0858    //! <b>Complexity</b>: Constant.
0859    //!
0860    //! <b>Throws</b>: Nothing.
0861    const_iterator root() const BOOST_NOEXCEPT;
0862 
0863    //! <b>Effects</b>: Returns a const_iterator pointing to the root node of the container or cend() if not present.
0864    //!
0865    //! <b>Complexity</b>: Constant.
0866    //!
0867    //! <b>Throws</b>: Nothing.
0868    const_iterator croot() const BOOST_NOEXCEPT;
0869 
0870    #endif   //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
0871 
0872    //! <b>Precondition</b>: end_iterator must be a valid end iterator
0873    //!   of the container.
0874    //!
0875    //! <b>Effects</b>: Returns a const reference to the container associated to the end iterator
0876    //!
0877    //! <b>Throws</b>: Nothing.
0878    //!
0879    //! <b>Complexity</b>: Constant.
0880    static bstree_impl &container_from_end_iterator(iterator end_iterator) BOOST_NOEXCEPT
0881    {
0882       return static_cast<bstree_impl&>
0883                (data_type::get_tree_base_from_end_iterator(end_iterator));
0884    }
0885 
0886    //! <b>Precondition</b>: end_iterator must be a valid end const_iterator
0887    //!   of the container.
0888    //!
0889    //! <b>Effects</b>: Returns a const reference to the container associated to the iterator
0890    //!
0891    //! <b>Throws</b>: Nothing.
0892    //!
0893    //! <b>Complexity</b>: Constant.
0894    static const bstree_impl &container_from_end_iterator(const_iterator end_iterator) BOOST_NOEXCEPT
0895    {
0896       return static_cast<bstree_impl&>
0897                (data_type::get_tree_base_from_end_iterator(end_iterator));
0898    }
0899 
0900    //! <b>Precondition</b>: it must be a valid iterator
0901    //!   of the container.
0902    //!
0903    //! <b>Effects</b>: Returns a const reference to the container associated to the iterator
0904    //!
0905    //! <b>Throws</b>: Nothing.
0906    //!
0907    //! <b>Complexity</b>: Logarithmic.
0908    static bstree_impl &container_from_iterator(iterator it) BOOST_NOEXCEPT
0909    {  return container_from_end_iterator(it.end_iterator_from_it());   }
0910 
0911    //! <b>Precondition</b>: it must be a valid end const_iterator
0912    //!   of container.
0913    //!
0914    //! <b>Effects</b>: Returns a const reference to the container associated to the end iterator
0915    //!
0916    //! <b>Throws</b>: Nothing.
0917    //!
0918    //! <b>Complexity</b>: Logarithmic.
0919    static const bstree_impl &container_from_iterator(const_iterator it) BOOST_NOEXCEPT
0920    {  return container_from_end_iterator(it.end_iterator_from_it());   }
0921 
0922    #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
0923 
0924    //! <b>Effects</b>: Returns the key_compare object used by the container.
0925    //!
0926    //! <b>Complexity</b>: Constant.
0927    //!
0928    //! <b>Throws</b>: If key_compare copy-constructor throws.
0929    key_compare key_comp() const;
0930 
0931    //! <b>Effects</b>: Returns the value_compare object used by the container.
0932    //!
0933    //! <b>Complexity</b>: Constant.
0934    //!
0935    //! <b>Throws</b>: If value_compare copy-constructor throws.
0936    value_compare value_comp() const;
0937 
0938    #endif   //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
0939 
0940    //! <b>Effects</b>: Returns true if the container is empty.
0941    //!
0942    //! <b>Complexity</b>: Constant.
0943    //!
0944    //! <b>Throws</b>: Nothing.
0945    bool empty() const BOOST_NOEXCEPT
0946    {
0947       if(ConstantTimeSize){
0948          return !this->data_type::sz_traits().get_size();
0949       }
0950       else{
0951          return algo_type::unique(this->header_ptr());
0952       }
0953    }
0954 
0955    //! <b>Effects</b>: Returns the number of elements stored in the container.
0956    //!
0957    //! <b>Complexity</b>: Linear to elements contained in *this
0958    //!   if constant-time size option is disabled. Constant time otherwise.
0959    //!
0960    //! <b>Throws</b>: Nothing.
0961    size_type size() const BOOST_NOEXCEPT
0962    {
0963       BOOST_IF_CONSTEXPR(constant_time_size)
0964          return this->sz_traits().get_size();
0965       else{
0966          return (size_type)node_algorithms::size(this->header_ptr());
0967       }
0968    }
0969 
0970    //! <b>Effects</b>: Swaps the contents of two containers.
0971    //!
0972    //! <b>Complexity</b>: Constant.
0973    //!
0974    //! <b>Throws</b>: If the comparison functor's swap call throws.
0975    void swap(bstree_impl& other)
0976    {
0977       //This can throw
0978       ::boost::adl_move_swap(this->get_comp(), other.get_comp());
0979       //These can't throw
0980       node_algorithms::swap_tree(this->header_ptr(), node_ptr(other.header_ptr()));
0981       this->sz_traits().swap(other.sz_traits());
0982    }
0983 
0984    //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
0985    //!   Cloner should yield to nodes equivalent to the original nodes.
0986    //!
0987    //! <b>Effects</b>: Erases all the elements from *this
0988    //!   calling Disposer::operator()(pointer), clones all the
0989    //!   elements from src calling Cloner::operator()(const_reference )
0990    //!   and inserts them on *this. Copies the predicate from the source container.
0991    //!
0992    //!   If cloner throws, all cloned elements are unlinked and disposed
0993    //!   calling Disposer::operator()(pointer).
0994    //!
0995    //! <b>Complexity</b>: Linear to erased plus inserted elements.
0996    //!
0997    //! <b>Throws</b>: If cloner throws or predicate copy assignment throws. Basic guarantee.
0998    template <class Cloner, class Disposer>
0999    void clone_from(const bstree_impl &src, Cloner cloner, Disposer disposer)
1000    {
1001       this->clear_and_dispose(disposer);
1002       if(!src.empty()){
1003          detail::exception_disposer<bstree_impl, Disposer>
1004             rollback(*this, disposer);
1005          node_algorithms::clone
1006             (src.header_ptr()
1007             ,this->header_ptr()
1008             ,detail::node_cloner <Cloner,    value_traits, AlgoType>(cloner,   &this->get_value_traits())
1009             ,detail::node_disposer<Disposer, value_traits, AlgoType>(disposer, &this->get_value_traits()));
1010          this->sz_traits().set_size(src.sz_traits().get_size());
1011          this->get_comp() = src.get_comp();
1012          rollback.release();
1013       }
1014    }
1015 
1016    //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1017    //!   Cloner should yield to nodes equivalent to the original nodes.
1018    //!
1019    //! <b>Effects</b>: Erases all the elements from *this
1020    //!   calling Disposer::operator()(pointer), clones all the
1021    //!   elements from src calling Cloner::operator()(reference)
1022    //!   and inserts them on *this. Copies the predicate from the source container.
1023    //!
1024    //!   If cloner throws, all cloned elements are unlinked and disposed
1025    //!   calling Disposer::operator()(pointer).
1026    //!
1027    //! <b>Complexity</b>: Linear to erased plus inserted elements.
1028    //!
1029    //! <b>Throws</b>: If cloner throws or predicate copy assignment throws. Basic guarantee.
1030    //!
1031    //! <b>Note</b>: This version can modify the source container, useful to implement
1032    //!    move semantics.
1033    template <class Cloner, class Disposer>
1034    void clone_from(BOOST_RV_REF(bstree_impl) src, Cloner cloner, Disposer disposer)
1035    {
1036       this->clear_and_dispose(disposer);
1037       if(!src.empty()){
1038          detail::exception_disposer<bstree_impl, Disposer>
1039             rollback(*this, disposer);
1040          node_algorithms::clone
1041             (src.header_ptr()
1042             ,this->header_ptr()
1043             ,detail::node_cloner <Cloner,    value_traits, AlgoType, false>(cloner,   &this->get_value_traits())
1044             ,detail::node_disposer<Disposer, value_traits, AlgoType>(disposer, &this->get_value_traits()));
1045          this->sz_traits().set_size(src.sz_traits().get_size());
1046          this->get_comp() = src.get_comp();
1047          rollback.release();
1048       }
1049    }
1050 
1051    //! <b>Requires</b>: value must be an lvalue
1052    //!
1053    //! <b>Effects</b>: Inserts value into the container before the upper bound.
1054    //!
1055    //! <b>Complexity</b>: Average complexity for insert element is at
1056    //!   most logarithmic.
1057    //!
1058    //! <b>Throws</b>: If the internal key_compare ordering function throws. Strong guarantee.
1059    //!
1060    //! <b>Note</b>: Does not affect the validity of iterators and references.
1061    //!   No copy-constructors are called.
1062    iterator insert_equal(reference value)
1063    {
1064       node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
1065       BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(to_insert));
1066       iterator ret(node_algorithms::insert_equal_upper_bound
1067          (this->header_ptr(), to_insert, this->key_node_comp(this->key_comp())), this->priv_value_traits_ptr());
1068       this->sz_traits().increment();
1069       return ret;
1070    }
1071 
1072    //! <b>Requires</b>: value must be an lvalue, and "hint" must be
1073    //!   a valid iterator.
1074    //!
1075    //! <b>Effects</b>: Inserts x into the container, using "hint" as a hint to
1076    //!   where it will be inserted. If "hint" is the upper_bound
1077    //!   the insertion takes constant time (two comparisons in the worst case)
1078    //!
1079    //! <b>Complexity</b>: Logarithmic in general, but it is amortized
1080    //!   constant time if t is inserted immediately before hint.
1081    //!
1082    //! <b>Throws</b>: If the internal key_compare ordering function throws. Strong guarantee.
1083    //!
1084    //! <b>Note</b>: Does not affect the validity of iterators and references.
1085    //!   No copy-constructors are called.
1086    iterator insert_equal(const_iterator hint, reference value)
1087    {
1088       node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
1089       BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(to_insert));
1090       iterator ret(node_algorithms::insert_equal
1091          (this->header_ptr(), hint.pointed_node(), to_insert, this->key_node_comp(this->key_comp())), this->priv_value_traits_ptr());
1092       this->sz_traits().increment();
1093       return ret;
1094    }
1095 
1096    //! <b>Requires</b>: Dereferencing iterator must yield an lvalue
1097    //!   of type value_type.
1098    //!
1099    //! <b>Effects</b>: Inserts a each element of a range into the container
1100    //!   before the upper bound of the key of each element.
1101    //!
1102    //! <b>Complexity</b>: Insert range is in general O(N * log(N)), where N is the
1103    //!   size of the range. However, it is linear in N if the range is already sorted
1104    //!   by value_comp().
1105    //!
1106    //! <b>Throws</b>: If the comparison functor call throws.
1107    //!
1108    //! <b>Note</b>: Does not affect the validity of iterators and references.
1109    //!   No copy-constructors are called.
1110    template<class Iterator>
1111    void insert_equal(Iterator b, Iterator e)
1112    {
1113       iterator iend(this->end());
1114       for (; b != e; ++b)
1115          this->insert_equal(iend, *b);
1116    }
1117 
1118    //! <b>Requires</b>: value must be an lvalue
1119    //!
1120    //! <b>Effects</b>: Inserts value into the container if the value
1121    //!   is not already present.
1122    //!
1123    //! <b>Complexity</b>: Average complexity for insert element is at
1124    //!   most logarithmic.
1125    //!
1126    //! <b>Throws</b>: If the comparison functor call throws.
1127    //!
1128    //! <b>Note</b>: Does not affect the validity of iterators and references.
1129    //!   No copy-constructors are called.
1130    std::pair<iterator, bool> insert_unique(reference value)
1131    {
1132       insert_commit_data commit_data;
1133       std::pair<node_ptr, bool> ret =
1134          (node_algorithms::insert_unique_check
1135             (this->header_ptr(), key_of_value()(value), this->key_node_comp(this->key_comp()), commit_data));
1136       return std::pair<iterator, bool>
1137          ( ret.second ? this->insert_unique_commit(value, commit_data)
1138                       : iterator(ret.first, this->priv_value_traits_ptr())
1139          , ret.second);
1140    }
1141 
1142    //! <b>Requires</b>: value must be an lvalue, and "hint" must be
1143    //!   a valid iterator
1144    //!
1145    //! <b>Effects</b>: Tries to insert x into the container, using "hint" as a hint
1146    //!   to where it will be inserted.
1147    //!
1148    //! <b>Complexity</b>: Logarithmic in general, but it is amortized
1149    //!   constant time (two comparisons in the worst case)
1150    //!   if t is inserted immediately before hint.
1151    //!
1152    //! <b>Throws</b>: If the comparison functor call throws.
1153    //!
1154    //! <b>Note</b>: Does not affect the validity of iterators and references.
1155    //!   No copy-constructors are called.
1156    iterator insert_unique(const_iterator hint, reference value)
1157    {
1158       insert_commit_data commit_data;
1159       std::pair<node_ptr, bool> ret =
1160          (node_algorithms::insert_unique_check
1161             (this->header_ptr(), hint.pointed_node(), key_of_value()(value), this->key_node_comp(this->key_comp()), commit_data));
1162       return ret.second ? this->insert_unique_commit(value, commit_data)
1163                         : iterator(ret.first, this->priv_value_traits_ptr());
1164    }
1165 
1166    //! <b>Requires</b>: Dereferencing iterator must yield an lvalue
1167    //!   of type value_type.
1168    //!
1169    //! <b>Effects</b>: Tries to insert each element of a range into the container.
1170    //!
1171    //! <b>Complexity</b>: Insert range is in general O(N * log(N)), where N is the
1172    //!   size of the range. However, it is linear in N if the range is already sorted
1173    //!   by value_comp().
1174    //!
1175    //! <b>Throws</b>: If the comparison functor call throws.
1176    //!
1177    //! <b>Note</b>: Does not affect the validity of iterators and references.
1178    //!   No copy-constructors are called.
1179    template<class Iterator>
1180    void insert_unique(Iterator b, Iterator e)
1181    {
1182       if(this->empty()){
1183          iterator iend(this->end());
1184          for (; b != e; ++b)
1185             this->insert_unique(iend, *b);
1186       }
1187       else{
1188          for (; b != e; ++b)
1189             this->insert_unique(*b);
1190       }
1191    }
1192 
1193    #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
1194 
1195    //! <b>Effects</b>: Checks if a value can be inserted in the container, using
1196    //!   a user provided key instead of the value itself.
1197    //!
1198    //! <b>Returns</b>: If there is an equivalent value
1199    //!   returns a pair containing an iterator to the already present value
1200    //!   and false. If the value can be inserted returns true in the returned
1201    //!   pair boolean and fills "commit_data" that is meant to be used with
1202    //!   the "insert_commit" function.
1203    //!
1204    //! <b>Complexity</b>: Average complexity is at most logarithmic.
1205    //!
1206    //! <b>Throws</b>: If the comp ordering function throws. Strong guarantee.
1207    std::pair<iterator, bool> insert_unique_check(const key_type &key, insert_commit_data &commit_data);
1208 
1209    //! <b>Effects</b>: Checks if a value can be inserted in the container, using
1210    //!   a user provided key instead of the value itself, using "hint"
1211    //!   as a hint to where it will be inserted.
1212    //!
1213    //! <b>Returns</b>: If there is an equivalent value
1214    //!   returns a pair containing an iterator to the already present value
1215    //!   and false. If the value can be inserted returns true in the returned
1216    //!   pair boolean and fills "commit_data" that is meant to be used with
1217    //!   the "insert_commit" function.
1218    //!
1219    //! <b>Complexity</b>: Logarithmic in general, but it's amortized
1220    //!   constant time if t is inserted immediately before hint.
1221    //!
1222    //! <b>Throws</b>: If the comp ordering function throws. Strong guarantee.
1223    std::pair<iterator, bool> insert_unique_check(const_iterator hint, const key_type &key, insert_commit_data &commit_data);
1224 
1225    //! <b>Requires</b>: comp must be a comparison function that induces
1226    //!   the same strict weak ordering as key_compare. The difference is that
1227    //!   comp compares an arbitrary key with the contained values.
1228    //!
1229    //! <b>Effects</b>: Checks if a value can be inserted in the container, using
1230    //!   a user provided key instead of the value itself.
1231    //!
1232    //! <b>Returns</b>: If there is an equivalent value
1233    //!   returns a pair containing an iterator to the already present value
1234    //!   and false. If the value can be inserted returns true in the returned
1235    //!   pair boolean and fills "commit_data" that is meant to be used with
1236    //!   the "insert_commit" function.
1237    //!
1238    //! <b>Complexity</b>: Average complexity is at most logarithmic.
1239    //!
1240    //! <b>Throws</b>: If the comp ordering function throws. Strong guarantee.
1241    //!
1242    //! <b>Notes</b>: This function is used to improve performance when constructing
1243    //!   a value_type is expensive: if there is an equivalent value
1244    //!   the constructed object must be discarded. Many times, the part of the
1245    //!   node that is used to impose the order is much cheaper to construct
1246    //!   than the value_type and this function offers the possibility to use that
1247    //!   part to check if the insertion will be successful.
1248    //!
1249    //!   If the check is successful, the user can construct the value_type and use
1250    //!   "insert_commit" to insert the object in constant-time. This gives a total
1251    //!   logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
1252    //!
1253    //!   "commit_data" remains valid for a subsequent "insert_commit" only if no more
1254    //!   objects are inserted or erased from the container.
1255    template<class KeyType, class KeyTypeKeyCompare>
1256    std::pair<iterator, bool> insert_unique_check
1257       (const KeyType &key, KeyTypeKeyCompare comp, insert_commit_data &commit_data);
1258 
1259    //! <b>Requires</b>: comp must be a comparison function that induces
1260    //!   the same strict weak ordering as key_compare. The difference is that
1261    //!   comp compares an arbitrary key with the contained values.
1262    //!
1263    //! <b>Effects</b>: Checks if a value can be inserted in the container, using
1264    //!   a user provided key instead of the value itself, using "hint"
1265    //!   as a hint to where it will be inserted.
1266    //!
1267    //! <b>Returns</b>: If there is an equivalent value
1268    //!   returns a pair containing an iterator to the already present value
1269    //!   and false. If the value can be inserted returns true in the returned
1270    //!   pair boolean and fills "commit_data" that is meant to be used with
1271    //!   the "insert_commit" function.
1272    //!
1273    //! <b>Complexity</b>: Logarithmic in general, but it's amortized
1274    //!   constant time if t is inserted immediately before hint.
1275    //!
1276    //! <b>Throws</b>: If the comp ordering function throws. Strong guarantee.
1277    //!
1278    //! <b>Notes</b>: This function is used to improve performance when constructing
1279    //!   a value_type is expensive: if there is an equivalent value
1280    //!   the constructed object must be discarded. Many times, the part of the
1281    //!   constructing that is used to impose the order is much cheaper to construct
1282    //!   than the value_type and this function offers the possibility to use that key
1283    //!   to check if the insertion will be successful.
1284    //!
1285    //!   If the check is successful, the user can construct the value_type and use
1286    //!   "insert_commit" to insert the object in constant-time. This can give a total
1287    //!   constant-time complexity to the insertion: check(O(1)) + commit(O(1)).
1288    //!
1289    //!   "commit_data" remains valid for a subsequent "insert_commit" only if no more
1290    //!   objects are inserted or erased from the container.
1291    template<class KeyType, class KeyTypeKeyCompare>
1292    std::pair<iterator, bool> insert_unique_check
1293       (const_iterator hint, const KeyType &key
1294       ,KeyTypeKeyCompare comp, insert_commit_data &commit_data);
1295 
1296    #endif   //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
1297 
1298    //! <b>Requires</b>: value must be an lvalue of type value_type. commit_data
1299    //!   must have been obtained from a previous call to "insert_check".
1300    //!   No objects should have been inserted or erased from the container between
1301    //!   the "insert_check" that filled "commit_data" and the call to "insert_commit".
1302    //!
1303    //! <b>Effects</b>: Inserts the value in the container using the information obtained
1304    //!   from the "commit_data" that a previous "insert_check" filled.
1305    //!
1306    //! <b>Returns</b>: An iterator to the newly inserted object.
1307    //!
1308    //! <b>Complexity</b>: Constant time.
1309    //!
1310    //! <b>Throws</b>: Nothing.
1311    //!
1312    //! <b>Notes</b>: This function has only sense if a "insert_check" has been
1313    //!   previously executed to fill "commit_data". No value should be inserted or
1314    //!   erased between the "insert_check" and "insert_commit" calls.
1315    iterator insert_unique_commit(reference value, const insert_commit_data &commit_data) BOOST_NOEXCEPT
1316    {
1317       node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
1318       BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(to_insert));
1319 
1320       #if !(defined(BOOST_DISABLE_ASSERTS) || ( defined(BOOST_ENABLE_ASSERT_DEBUG_HANDLER) && defined(NDEBUG) ))
1321       //Test insertion position is correct
1322       iterator p(commit_data.node, this->priv_value_traits_ptr());
1323       if(!commit_data.link_left){
1324          ++p;
1325       }
1326       //Check if the insertion point is correct to detect wrong
1327       //uses insert_unique_check
1328       BOOST_ASSERT(( p == this->end()   || !this->get_comp()(*p, value)   ));
1329       BOOST_ASSERT(( p == this->begin() || !this->get_comp()(value, *--p) ));
1330       #endif
1331 
1332       node_algorithms::insert_unique_commit
1333                (this->header_ptr(), to_insert, commit_data);
1334       this->sz_traits().increment();
1335       return iterator(to_insert, this->priv_value_traits_ptr());
1336    }
1337 
1338    //! <b>Requires</b>: value must be an lvalue, "pos" must be
1339    //!   a valid iterator (or end) and must be the succesor of value
1340    //!   once inserted according to the predicate
1341    //!
1342    //! <b>Effects</b>: Inserts x into the container before "pos".
1343    //!
1344    //! <b>Complexity</b>: Constant time.
1345    //!
1346    //! <b>Throws</b>: Nothing.
1347    //!
1348    //! <b>Note</b>: This function does not check preconditions so if "pos" is not
1349    //! the successor of "value" container ordering invariant will be broken.
1350    //! This is a low-level function to be used only for performance reasons
1351    //! by advanced users.
1352    iterator insert_before(const_iterator pos, reference value) BOOST_NOEXCEPT
1353    {
1354       node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
1355       BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(to_insert));
1356       this->sz_traits().increment();
1357       return iterator(node_algorithms::insert_before
1358          (this->header_ptr(), pos.pointed_node(), to_insert), this->priv_value_traits_ptr());
1359    }
1360 
1361    //! <b>Requires</b>: value must be an lvalue, and it must be no less
1362    //!   than the greatest inserted key
1363    //!
1364    //! <b>Effects</b>: Inserts x into the container in the last position.
1365    //!
1366    //! <b>Complexity</b>: Constant time.
1367    //!
1368    //! <b>Throws</b>: Nothing.
1369    //!
1370    //! <b>Note</b>: This function does not check preconditions so if value is
1371    //!   less than the greatest inserted key container ordering invariant will be broken.
1372    //!   This function is slightly more efficient than using "insert_before".
1373    //!   This is a low-level function to be used only for performance reasons
1374    //!   by advanced users.
1375    void push_back(reference value) BOOST_NOEXCEPT
1376    {
1377       node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
1378       BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(to_insert));
1379       this->sz_traits().increment();
1380       node_algorithms::push_back(this->header_ptr(), to_insert);
1381    }
1382 
1383    //! <b>Requires</b>: value must be an lvalue, and it must be no greater
1384    //!   than the minimum inserted key
1385    //!
1386    //! <b>Effects</b>: Inserts x into the container in the first position.
1387    //!
1388    //! <b>Complexity</b>: Constant time.
1389    //!
1390    //! <b>Throws</b>: Nothing.
1391    //!
1392    //! <b>Note</b>: This function does not check preconditions so if value is
1393    //!   greater than the minimum inserted key container ordering invariant will be broken.
1394    //!   This function is slightly more efficient than using "insert_before".
1395    //!   This is a low-level function to be used only for performance reasons
1396    //!   by advanced users.
1397    void push_front(reference value) BOOST_NOEXCEPT
1398    {
1399       node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
1400       BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(to_insert));
1401       this->sz_traits().increment();
1402       node_algorithms::push_front(this->header_ptr(), to_insert);
1403    }
1404 
1405    //! <b>Effects</b>: Erases the element pointed to by i.
1406    //!
1407    //! <b>Complexity</b>: Average complexity for erase element is constant time.
1408    //!
1409    //! <b>Throws</b>: Nothing.
1410    //!
1411    //! <b>Note</b>: Invalidates the iterators (but not the references)
1412    //!    to the erased elements. No destructors are called.
1413    iterator erase(const_iterator i) BOOST_NOEXCEPT
1414    {
1415       const_iterator ret(i);
1416       ++ret;
1417       node_ptr to_erase(i.pointed_node());
1418       BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || !node_algorithms::unique(to_erase));
1419       node_algorithms::erase(this->header_ptr(), to_erase);
1420       this->sz_traits().decrement();
1421       BOOST_IF_CONSTEXPR(safemode_or_autounlink)
1422          node_algorithms::init(to_erase);
1423       return ret.unconst();
1424    }
1425 
1426    //! <b>Effects</b>: Erases the range pointed to by b end e.
1427    //!
1428    //! <b>Complexity</b>: Average complexity for erase range is at most
1429    //!   O(log(size() + N)), where N is the number of elements in the range.
1430    //!
1431    //! <b>Throws</b>: Nothing.
1432    //!
1433    //! <b>Note</b>: Invalidates the iterators (but not the references)
1434    //!    to the erased elements. No destructors are called.
1435    iterator erase(const_iterator b, const_iterator e) BOOST_NOEXCEPT
1436    {  size_type n;   return this->private_erase(b, e, n);   }
1437 
1438    //! <b>Effects</b>: Erases all the elements with the given value.
1439    //!
1440    //! <b>Returns</b>: The number of erased elements.
1441    //!
1442    //! <b>Complexity</b>: O(log(size() + N).
1443    //!
1444    //! <b>Throws</b>: Nothing.
1445    //!
1446    //! <b>Note</b>: Invalidates the iterators (but not the references)
1447    //!    to the erased elements. No destructors are called.
1448    size_type erase(const key_type &key) BOOST_NOEXCEPT
1449    {  return this->erase(key, this->key_comp());   }
1450 
1451    //! <b>Requires</b>: key is a value such that `*this` is partitioned with respect to
1452    //!   comp(nk, key) and !comp(key, nk), with comp(nk, key) implying !comp(key, nk),
1453    //!   with nk the key_type of a value_type inserted into `*this`.
1454    //!
1455    //! <b>Effects</b>: Erases all the elements with the given key.
1456    //!   according to the comparison functor "comp".
1457    //!
1458    //! <b>Returns</b>: The number of erased elements.
1459    //!
1460    //! <b>Complexity</b>: O(log(size() + N).
1461    //!
1462    //! <b>Throws</b>: Nothing.
1463    //!
1464    //! <b>Note</b>: Invalidates the iterators (but not the references)
1465    //!    to the erased elements. No destructors are called.
1466    template<class KeyType, class KeyTypeKeyCompare>
1467    BOOST_INTRUSIVE_DOC1ST(size_type
1468       , typename detail::disable_if_convertible<KeyTypeKeyCompare BOOST_INTRUSIVE_I const_iterator BOOST_INTRUSIVE_I size_type>::type)
1469       erase(const KeyType& key, KeyTypeKeyCompare comp)
1470    {
1471       std::pair<iterator,iterator> p = this->equal_range(key, comp);
1472       size_type n;
1473       this->private_erase(p.first, p.second, n);
1474       return n;
1475    }
1476 
1477    //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1478    //!
1479    //! <b>Effects</b>: Erases the element pointed to by i.
1480    //!   Disposer::operator()(pointer) is called for the removed element.
1481    //!
1482    //! <b>Complexity</b>: Average complexity for erase element is constant time.
1483    //!
1484    //! <b>Throws</b>: Nothing.
1485    //!
1486    //! <b>Note</b>: Invalidates the iterators
1487    //!    to the erased elements.
1488    template<class Disposer>
1489    iterator erase_and_dispose(const_iterator i, Disposer disposer) BOOST_NOEXCEPT
1490    {
1491       node_ptr to_erase(i.pointed_node());
1492       iterator ret(this->erase(i));
1493       disposer(this->get_value_traits().to_value_ptr(to_erase));
1494       return ret;
1495    }
1496 
1497    //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1498    //!
1499    //! <b>Effects</b>: Erases all the elements with the given value.
1500    //!   Disposer::operator()(pointer) is called for the removed elements.
1501    //!
1502    //! <b>Returns</b>: The number of erased elements.
1503    //!
1504    //! <b>Complexity</b>: O(log(size() + N).
1505    //!
1506    //! <b>Throws</b>: Nothing.
1507    //!
1508    //! <b>Note</b>: Invalidates the iterators (but not the references)
1509    //!    to the erased elements. No destructors are called.
1510    template<class Disposer>
1511    size_type erase_and_dispose(const key_type &key, Disposer disposer)
1512    {
1513       std::pair<iterator,iterator> p = this->equal_range(key);
1514       size_type n;
1515       this->private_erase(p.first, p.second, n, disposer);
1516       return n;
1517    }
1518 
1519    //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1520    //!
1521    //! <b>Effects</b>: Erases the range pointed to by b end e.
1522    //!   Disposer::operator()(pointer) is called for the removed elements.
1523    //!
1524    //! <b>Complexity</b>: Average complexity for erase range is at most
1525    //!   O(log(size() + N)), where N is the number of elements in the range.
1526    //!
1527    //! <b>Throws</b>: Nothing.
1528    //!
1529    //! <b>Note</b>: Invalidates the iterators
1530    //!    to the erased elements.
1531    template<class Disposer>
1532    iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer) BOOST_NOEXCEPT
1533    {  size_type n;   return this->private_erase(b, e, n, disposer);   }
1534 
1535    //! <b>Requires</b>: key is a value such that `*this` is partitioned with respect to
1536    //!   comp(nk, key) and !comp(key, nk), with comp(nk, key) implying !comp(key, nk)
1537    //!   and nk the key_type of a value_type inserted into `*this`.
1538    //!
1539    //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1540    //!
1541    //! <b>Effects</b>: Erases all the elements with the given key.
1542    //!   according to the comparison functor "comp".
1543    //!   Disposer::operator()(pointer) is called for the removed elements.
1544    //!
1545    //! <b>Returns</b>: The number of erased elements.
1546    //!
1547    //! <b>Complexity</b>: O(log(size() + N).
1548    //!
1549    //! <b>Throws</b>: Nothing.
1550    //!
1551    //! <b>Note</b>: Invalidates the iterators
1552    //!    to the erased elements.
1553    template<class KeyType, class KeyTypeKeyCompare, class Disposer>
1554    BOOST_INTRUSIVE_DOC1ST(size_type
1555       , typename detail::disable_if_convertible<KeyTypeKeyCompare BOOST_INTRUSIVE_I const_iterator BOOST_INTRUSIVE_I size_type>::type)
1556       erase_and_dispose(const KeyType& key, KeyTypeKeyCompare comp, Disposer disposer)
1557    {
1558       std::pair<iterator,iterator> p = this->equal_range(key, comp);
1559       size_type n;
1560       this->private_erase(p.first, p.second, n, disposer);
1561       return n;
1562    }
1563 
1564    //! <b>Effects</b>: Erases all of the elements.
1565    //!
1566    //! <b>Complexity</b>: Linear to the number of elements on the container.
1567    //!   if it's a safe-mode or auto-unlink value_type. Constant time otherwise.
1568    //!
1569    //! <b>Throws</b>: Nothing.
1570    //!
1571    //! <b>Note</b>: Invalidates the iterators (but not the references)
1572    //!    to the erased elements. No destructors are called.
1573    void clear() BOOST_NOEXCEPT
1574    {
1575       BOOST_IF_CONSTEXPR(safemode_or_autounlink){
1576          this->clear_and_dispose(detail::null_disposer());
1577       }
1578       else{
1579          node_algorithms::init_header(this->header_ptr());
1580          this->sz_traits().set_size(0);
1581       }
1582    }
1583 
1584    //! <b>Effects</b>: Erases all of the elements calling disposer(p) for
1585    //!   each node to be erased.
1586    //! <b>Complexity</b>: Average complexity for is at most O(log(size() + N)),
1587    //!   where N is the number of elements in the container.
1588    //!
1589    //! <b>Throws</b>: Nothing.
1590    //!
1591    //! <b>Note</b>: Invalidates the iterators (but not the references)
1592    //!    to the erased elements. Calls N times to disposer functor.
1593    template<class Disposer>
1594    void clear_and_dispose(Disposer disposer) BOOST_NOEXCEPT
1595    {
1596       node_algorithms::clear_and_dispose(this->header_ptr()
1597          , detail::node_disposer<Disposer, value_traits, AlgoType>(disposer, &this->get_value_traits()));
1598       node_algorithms::init_header(this->header_ptr());
1599       this->sz_traits().set_size(0);
1600    }
1601 
1602    //! <b>Effects</b>: Returns the number of contained elements with the given value
1603    //!
1604    //! <b>Complexity</b>: Logarithmic to the number of elements contained plus lineal
1605    //!   to number of objects with the given value.
1606    //!
1607    //! <b>Throws</b>: If `key_compare` throws.
1608    size_type count(const key_type &key) const
1609    {  return size_type(this->count(key, this->key_comp()));   }
1610 
1611    //! <b>Requires</b>: key is a value such that `*this` is partitioned with respect to
1612    //!   comp(nk, key) and !comp(key, nk), with comp(nk, key) implying !comp(key, nk),
1613    //!   and nk the key_type of a value_type inserted into `*this`.
1614    //!
1615    //! <b>Effects</b>: Returns the number of contained elements with the given key
1616    //!
1617    //! <b>Complexity</b>: Logarithmic to the number of elements contained plus lineal
1618    //!   to number of objects with the given key.
1619    //!
1620    //! <b>Throws</b>: If `comp` throws.
1621    template<class KeyType, class KeyTypeKeyCompare>
1622    size_type count(const KeyType &key, KeyTypeKeyCompare comp) const
1623    {
1624       std::pair<const_iterator, const_iterator> ret = this->equal_range(key, comp);
1625       size_type n = 0;
1626       for(; ret.first != ret.second; ++ret.first){ ++n; }
1627       return n;
1628    }
1629 
1630    #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1631 
1632    //Add non-const overloads to theoretically const members
1633    //as some algorithms have different behavior when non-const versions are used (like splay trees).
1634    size_type count(const key_type &key)
1635    {  return size_type(this->count(key, this->key_comp()));   }
1636 
1637    template<class KeyType, class KeyTypeKeyCompare>
1638    size_type count(const KeyType &key, KeyTypeKeyCompare comp)
1639    {
1640       std::pair<const_iterator, const_iterator> ret = this->equal_range(key, comp);
1641       size_type n = 0;
1642       for(; ret.first != ret.second; ++ret.first){ ++n; }
1643       return n;
1644    }
1645 
1646    #else //defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1647 
1648    //! <b>Effects</b>: Returns an iterator to the first element whose
1649    //!   key is not less than k or end() if that element does not exist.
1650    //!
1651    //! <b>Complexity</b>: Logarithmic.
1652    //!
1653    //! <b>Throws</b>: If `key_compare` throws.
1654    iterator lower_bound(const key_type &key);
1655 
1656    //! <b>Effects</b>: Returns an iterator to the first element whose
1657    //!   key is not less than k or end() if that element does not exist.
1658    //!
1659    //! <b>Complexity</b>: Logarithmic.
1660    //!
1661    //! <b>Throws</b>: If `key_compare` throws.
1662    const_iterator lower_bound(const key_type &key) const;
1663 
1664    //! @copydoc ::boost::intrusive::bstree::lower_bound(const key_type &)
1665    template<class KeyType, class KeyTypeKeyCompare>
1666    iterator lower_bound(const KeyType &key, KeyTypeKeyCompare comp);
1667 
1668    //! @copydoc ::boost::intrusive::bstree::lower_bound(const KeyType&,KeyTypeKeyCompare)
1669    template<class KeyType, class KeyTypeKeyCompare>
1670    const_iterator lower_bound(const KeyType &key, KeyTypeKeyCompare comp) const;
1671 
1672    //! <b>Effects</b>: Returns an iterator to the first element whose
1673    //!   key is greater than k or end() if that element does not exist.
1674    //!
1675    //! <b>Complexity</b>: Logarithmic.
1676    //!
1677    //! <b>Throws</b>: If `key_compare` throws.
1678    iterator upper_bound(const key_type &key);
1679 
1680    //! <b>Requires</b>: key is a value such that `*this` is partitioned with respect to
1681    //!   !comp(key, nk), with nk the key_type of a value_type inserted into `*this`.
1682    //!
1683    //! <b>Effects</b>: Returns an iterator to the first element whose
1684    //!   key is greater than k according to comp or end() if that element
1685    //!   does not exist.
1686    //!
1687    //! <b>Complexity</b>: Logarithmic.
1688    //!
1689    //! <b>Throws</b>: If `comp` throws.
1690    template<class KeyType, class KeyTypeKeyCompare>
1691    iterator upper_bound(const KeyType &key, KeyTypeKeyCompare comp);
1692 
1693    //! @copydoc ::boost::intrusive::bstree::upper_bound(const key_type &)
1694    const_iterator upper_bound(const key_type &key) const;
1695 
1696    //! @copydoc ::boost::intrusive::bstree::upper_bound(const KeyType&,KeyTypeKeyCompare)
1697    template<class KeyType, class KeyTypeKeyCompare>
1698    const_iterator upper_bound(const KeyType &key, KeyTypeKeyCompare comp) const;
1699 
1700    //! <b>Effects</b>: Finds an iterator to the first element whose key is
1701    //!   k or end() if that element does not exist.
1702    //!
1703    //! <b>Complexity</b>: Logarithmic.
1704    //!
1705    //! <b>Throws</b>: If `key_compare` throws.
1706    iterator find(const key_type &key);
1707 
1708    //! <b>Requires</b>: key is a value such that `*this` is partitioned with respect to
1709    //!   comp(nk, key) and !comp(key, nk), with comp(nk, key) implying !comp(key, nk),
1710    //!   and nk the key_type of a value_type inserted into `*this`.
1711    //!
1712    //! <b>Effects</b>: Finds an iterator to the first element whose key is
1713    //!   k or end() if that element does not exist.
1714    //!
1715    //! <b>Complexity</b>: Logarithmic.
1716    //!
1717    //! <b>Throws</b>: If `comp` throws.
1718    template<class KeyType, class KeyTypeKeyCompare>
1719    iterator find(const KeyType &key, KeyTypeKeyCompare comp);
1720 
1721    //! @copydoc ::boost::intrusive::bstree::find(const key_type &)
1722    const_iterator find(const key_type &key) const;
1723 
1724    //! @copydoc ::boost::intrusive::bstree::find(const KeyType&,KeyTypeKeyCompare)
1725    template<class KeyType, class KeyTypeKeyCompare>
1726    const_iterator find(const KeyType &key, KeyTypeKeyCompare comp) const;
1727 
1728    //! <b>Effects</b>: Finds a range containing all elements whose key is k or
1729    //!   an empty range that indicates the position where those elements would be
1730    //!   if they there is no elements with key k.
1731    //!
1732    //! <b>Complexity</b>: Logarithmic.
1733    //!
1734    //! <b>Throws</b>: If `key_compare` throws.
1735    std::pair<iterator,iterator> equal_range(const key_type &key);
1736 
1737    //! <b>Requires</b>: key is a value such that `*this` is partitioned with respect to
1738    //!   comp(nk, key) and !comp(key, nk), with comp(nk, key) implying !comp(key, nk),
1739    //!   with nk the key_type of a value_type inserted into `*this`.
1740    //!
1741    //! <b>Effects</b>: Finds a range containing all elements whose key is k or
1742    //!   an empty range that indicates the position where those elements would be
1743    //!   if they there is no elements with key k.
1744    //!
1745    //! <b>Complexity</b>: Logarithmic.
1746    //!
1747    //! <b>Throws</b>: If `comp` throws.
1748    template<class KeyType, class KeyTypeKeyCompare>
1749    std::pair<iterator,iterator> equal_range(const KeyType &key, KeyTypeKeyCompare comp);
1750 
1751    //! @copydoc ::boost::intrusive::bstree::equal_range(const key_type &)
1752    std::pair<const_iterator, const_iterator> equal_range(const key_type &key) const;
1753 
1754    //! @copydoc ::boost::intrusive::bstree::equal_range(const KeyType&,KeyTypeKeyCompare)
1755    template<class KeyType, class KeyTypeKeyCompare>
1756    std::pair<const_iterator, const_iterator>
1757       equal_range(const KeyType &key, KeyTypeKeyCompare comp) const;
1758 
1759    //! <b>Requires</b>:
1760    //!   `upper_key` shall not precede `lower_key` according to key_compare.
1761    //!   [key_comp()(upper_key, lower_key) shall be false]
1762    //!
1763    //!   If `lower_key` is equivalent to `upper_key`
1764    //!   [!key_comp()(upper_key, lower_key) && !key_comp()(lower_key, upper_key)] then
1765    //!   ('left_closed' || 'right_closed') must be false.
1766    //!
1767    //! <b>Effects</b>: Returns an a pair with the following criteria:
1768    //!
1769    //!   first = lower_bound(lower_key) if left_closed, upper_bound(lower_key) otherwise
1770    //!
1771    //!   second = upper_bound(upper_key) if right_closed, lower_bound(upper_key) otherwise
1772    //!
1773    //! <b>Complexity</b>: Logarithmic.
1774    //!
1775    //! <b>Throws</b>: If `key_compare` throws.
1776    //!
1777    //! <b>Note</b>: This function can be more efficient than calling upper_bound
1778    //!   and lower_bound for lower_value and upper_value.
1779    //!
1780    //! <b>Note</b>: Experimental function, the interface might change in future releases.
1781    std::pair<iterator,iterator> bounded_range
1782       (const key_type &lower_key, const key_type &upper_value, bool left_closed, bool right_closed);
1783 
1784    //! <b>Requires</b>:
1785    //!   `lower_key` is a value such that `*this` is partitioned with respect to
1786    //!   comp(nk, lower_key) if left_closed is true, with respect to !comp(lower_key, nk) otherwise.
1787    //!
1788    //! `upper_key` is a value such that `*this` is partitioned with respect to
1789    //!   !comp(upper_key, nk) if right_closed is true, with respect to comp(nk, upper_key) otherwise.
1790    //!
1791    //!   `upper_key` shall not precede `lower_key` according to comp
1792    //!   [comp(upper_key, lower_key) shall be false]
1793    //!
1794    //!   If `lower_key` is equivalent to `upper_key`
1795    //!   [!comp(upper_key, lower_key) && !comp(lower_key, upper_key)] then
1796    //!   ('left_closed' || 'right_closed') must be false.
1797    //!
1798    //! <b>Effects</b>: Returns an a pair with the following criteria:
1799    //!
1800    //!   first = lower_bound(lower_key, comp) if left_closed, upper_bound(lower_key, comp) otherwise
1801    //!
1802    //!   second = upper_bound(upper_key, comp) if right_closed, lower_bound(upper_key, comp) otherwise
1803    //!
1804    //! <b>Complexity</b>: Logarithmic.
1805    //!
1806    //! <b>Throws</b>: If `comp` throws.
1807    //!
1808    //! <b>Note</b>: This function can be more efficient than calling upper_bound
1809    //!   and lower_bound for lower_key and upper_key.
1810    //!
1811    //! <b>Note</b>: Experimental function, the interface might change in future releases.
1812    template<class KeyType, class KeyTypeKeyCompare>
1813    std::pair<iterator,iterator> bounded_range
1814       (const KeyType &lower_key, const KeyType &upper_key, KeyTypeKeyCompare comp, bool left_closed, bool right_closed);
1815    
1816    //! @copydoc ::boost::intrusive::bstree::bounded_range(const key_type &,const key_type &,bool,bool)
1817    std::pair<const_iterator,const_iterator> bounded_range
1818       (const key_type &lower_key, const key_type &upper_key, bool left_closed, bool right_closed) const;
1819 
1820    //! @copydoc ::boost::intrusive::bstree::bounded_range(const KeyType&,const KeyType&,KeyTypeKeyCompare,bool,bool)
1821    template<class KeyType, class KeyTypeKeyCompare>
1822    std::pair<const_iterator,const_iterator> bounded_range
1823       (const KeyType &lower_key, const KeyType &upper_key, KeyTypeKeyCompare comp, bool left_closed, bool right_closed) const;
1824 
1825    //! <b>Requires</b>: value must be an lvalue and shall be in a set of
1826    //!   appropriate type. Otherwise the behavior is undefined.
1827    //!
1828    //! <b>Effects</b>: Returns: a valid iterator i belonging to the set
1829    //!   that points to the value
1830    //!
1831    //! <b>Complexity</b>: Constant.
1832    //!
1833    //! <b>Throws</b>: Nothing.
1834    //!
1835    //! <b>Note</b>: This static function is available only if the <i>value traits</i>
1836    //!   is stateless.
1837    static iterator s_iterator_to(reference value) BOOST_NOEXCEPT;
1838 
1839    //! <b>Requires</b>: value must be an lvalue and shall be in a set of
1840    //!   appropriate type. Otherwise the behavior is undefined.
1841    //!
1842    //! <b>Effects</b>: Returns: a valid const_iterator i belonging to the
1843    //!   set that points to the value
1844    //!
1845    //! <b>Complexity</b>: Constant.
1846    //!
1847    //! <b>Throws</b>: Nothing.
1848    //!
1849    //! <b>Note</b>: This static function is available only if the <i>value traits</i>
1850    //!   is stateless.
1851    static const_iterator s_iterator_to(const_reference value) BOOST_NOEXCEPT;
1852 
1853    //! <b>Requires</b>: value must be an lvalue and shall be in a set of
1854    //!   appropriate type. Otherwise the behavior is undefined.
1855    //!
1856    //! <b>Effects</b>: Returns: a valid iterator i belonging to the set
1857    //!   that points to the value
1858    //!
1859    //! <b>Complexity</b>: Constant.
1860    //!
1861    //! <b>Throws</b>: Nothing.
1862    iterator iterator_to(reference value) BOOST_NOEXCEPT;
1863 
1864    //! <b>Requires</b>: value must be an lvalue and shall be in a set of
1865    //!   appropriate type. Otherwise the behavior is undefined.
1866    //!
1867    //! <b>Effects</b>: Returns: a valid const_iterator i belonging to the
1868    //!   set that points to the value
1869    //!
1870    //! <b>Complexity</b>: Constant.
1871    //!
1872    //! <b>Throws</b>: Nothing.
1873    const_iterator iterator_to(const_reference value) const BOOST_NOEXCEPT;
1874 
1875    //! <b>Requires</b>: value shall not be in a container.
1876    //!
1877    //! <b>Effects</b>: init_node puts the hook of a value in a well-known default
1878    //!   state.
1879    //!
1880    //! <b>Throws</b>: Nothing.
1881    //!
1882    //! <b>Complexity</b>: Constant time.
1883    //!
1884    //! <b>Note</b>: This function puts the hook in the well-known default state
1885    //!   used by auto_unlink and safe hooks.
1886    static void init_node(reference value) BOOST_NOEXCEPT;
1887 
1888    #endif   //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1889 
1890    //! <b>Effects</b>: Unlinks the leftmost node from the container.
1891    //!
1892    //! <b>Complexity</b>: Average complexity is constant time.
1893    //!
1894    //! <b>Throws</b>: Nothing.
1895    //!
1896    //! <b>Notes</b>: This function breaks the container and the container can
1897    //!   only be used for more unlink_leftmost_without_rebalance calls.
1898    //!   This function is normally used to achieve a step by step
1899    //!   controlled destruction of the container.
1900    pointer unlink_leftmost_without_rebalance() BOOST_NOEXCEPT
1901    {
1902       node_ptr to_be_disposed(node_algorithms::unlink_leftmost_without_rebalance
1903                            (this->header_ptr()));
1904       if(!to_be_disposed)
1905          return 0;
1906       this->sz_traits().decrement();
1907       BOOST_IF_CONSTEXPR(safemode_or_autounlink)//If this is commented does not work with normal_link
1908          node_algorithms::init(to_be_disposed);
1909       return this->get_value_traits().to_value_ptr(to_be_disposed);
1910    }
1911 
1912    #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1913 
1914    //! <b>Requires</b>: replace_this must be a valid iterator of *this
1915    //!   and with_this must not be inserted in any container.
1916    //!
1917    //! <b>Effects</b>: Replaces replace_this in its position in the
1918    //!   container with with_this. The container does not need to be rebalanced.
1919    //!
1920    //! <b>Complexity</b>: Constant.
1921    //!
1922    //! <b>Throws</b>: Nothing.
1923    //!
1924    //! <b>Note</b>: This function will break container ordering invariants if
1925    //!   with_this is not equivalent to *replace_this according to the
1926    //!   ordering rules. This function is faster than erasing and inserting
1927    //!   the node, since no rebalancing or comparison is needed.
1928    void replace_node(iterator replace_this, reference with_this) BOOST_NOEXCEPT;
1929 
1930    //! <b>Effects</b>: Rebalances the tree.
1931    //!
1932    //! <b>Throws</b>: Nothing.
1933    //!
1934    //! <b>Complexity</b>: Linear.
1935    void rebalance() BOOST_NOEXCEPT;
1936 
1937    //! <b>Requires</b>: old_root is a node of a tree.
1938    //!
1939    //! <b>Effects</b>: Rebalances the subtree rooted at old_root.
1940    //!
1941    //! <b>Returns</b>: The new root of the subtree.
1942    //!
1943    //! <b>Throws</b>: Nothing.
1944    //!
1945    //! <b>Complexity</b>: Linear to the elements in the subtree.
1946    iterator rebalance_subtree(iterator r) BOOST_NOEXCEPT;
1947 
1948    #endif   //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1949 
1950    //! <b>Effects</b>: removes "value" from the container.
1951    //!
1952    //! <b>Throws</b>: Nothing.
1953    //!
1954    //! <b>Complexity</b>: Logarithmic time.
1955    //!
1956    //! <b>Note</b>: This static function is only usable with non-constant
1957    //! time size containers that have stateless comparison functors.
1958    //!
1959    //! If the user calls
1960    //! this function with a constant time size container or stateful comparison
1961    //! functor a compilation error will be issued.
1962    static void remove_node(reference value) BOOST_NOEXCEPT
1963    {
1964       BOOST_STATIC_ASSERT((!constant_time_size));
1965       node_ptr to_remove(value_traits::to_node_ptr(value));
1966       node_algorithms::unlink(to_remove);
1967       BOOST_IF_CONSTEXPR(safemode_or_autounlink)
1968          node_algorithms::init(to_remove);
1969    }
1970 
1971    //! <b>Requires</b>: "source" container's Options can only can differ in the comparison
1972    //!   function from *this.
1973    //! 
1974    //! <b>Effects</b>: Attempts to extract each element in source and insert it into a using
1975    //!   the comparison object of *this. If there is an element in a with key equivalent to the
1976    //!   key of an element from source, then that element is not extracted from source.
1977    //! 
1978    //! <b>Postcondition</b>: Pointers and references to the transferred elements of source refer
1979    //!   to those same elements but as members of *this. Iterators referring to the transferred
1980    //!   elements will continue to refer to their elements, but they now behave as iterators into *this,
1981    //!   not into source.
1982    //!
1983    //! <b>Throws</b>: Nothing unless the comparison object throws.
1984    //!
1985    //! <b>Complexity</b>: N log(a.size() + N) (N has the value source.size())
1986    #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1987    template<class T, class ...Options2> void merge_unique(bstree<T, Options2...> &);
1988    #else
1989    template<class Compare2>
1990    void merge_unique(bstree_impl
1991       <ValueTraits, VoidOrKeyOfValue, Compare2, SizeType, ConstantTimeSize, AlgoType, HeaderHolder> &source)
1992    #endif
1993    {
1994       node_ptr it   (node_algorithms::begin_node(source.header_ptr()))
1995              , itend(node_algorithms::end_node  (source.header_ptr()));
1996 
1997       while(it != itend){
1998          node_ptr const p(it);
1999          BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || !node_algorithms::unique(p));
2000          it = node_algorithms::next_node(it);
2001          if( node_algorithms::transfer_unique(this->header_ptr(), this->key_node_comp(this->key_comp()), source.header_ptr(), p) ){
2002             source.sz_traits().decrement();
2003             this->sz_traits().increment();
2004          }
2005       }
2006    }
2007 
2008    //! <b>Requires</b>: "source" container's Options can only can differ in the comparison
2009    //!   function from *this.
2010    //! 
2011    //! <b>Effects</b>: Extracts each element in source and insert it into a using
2012    //!   the comparison object of *this.
2013    //! 
2014    //! <b>Postcondition</b>: Pointers and references to the transferred elements of source refer
2015    //!   to those same elements but as members of *this. Iterators referring to the transferred
2016    //!   elements will continue to refer to their elements, but they now behave as iterators into *this,
2017    //!   not into source.
2018    //!
2019    //! <b>Throws</b>: Nothing unless the comparison object throws.
2020    //!
2021    //! <b>Complexity</b>: N log(a.size() + N) (N has the value source.size())
2022    #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
2023    template<class T, class ...Options2> void merge_equal(bstree<T, Options2...> &);
2024    #else
2025    template<class Compare2>
2026    void merge_equal(bstree_impl
2027       <ValueTraits, VoidOrKeyOfValue, Compare2, SizeType, ConstantTimeSize, AlgoType, HeaderHolder> &source)
2028    #endif
2029    {
2030       node_ptr it   (node_algorithms::begin_node(source.header_ptr()))
2031              , itend(node_algorithms::end_node  (source.header_ptr()));
2032 
2033       while(it != itend){
2034          node_ptr const p(it);
2035          BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || !node_algorithms::unique(p));
2036          it = node_algorithms::next_node(it);
2037          node_algorithms::transfer_equal(this->header_ptr(), this->key_node_comp(this->key_comp()), source.header_ptr(), p);
2038          source.sz_traits().decrement();
2039          this->sz_traits().increment();
2040       }
2041    }
2042 
2043    //! <b>Effects</b>: Asserts the integrity of the container with additional checks provided by the user.
2044    //!
2045    //! <b>Complexity</b>: Linear time.
2046    //!
2047    //! <b>Note</b>: The method might not have effect when asserts are turned off (e.g., with NDEBUG).
2048    //!   Experimental function, interface might change in future versions.
2049    template <class ExtraChecker>
2050    void check(ExtraChecker extra_checker) const
2051    {
2052       typedef detail::key_nodeptr_comp<key_compare, value_traits, key_of_value> nodeptr_comp_t;
2053       nodeptr_comp_t nodeptr_comp(this->key_comp(), &this->get_value_traits());
2054       typedef typename get_node_checker<AlgoType, ValueTraits, nodeptr_comp_t, ExtraChecker>::type node_checker_t;
2055       typename node_checker_t::return_type checker_return;
2056       node_algorithms::check(this->header_ptr(), node_checker_t(nodeptr_comp, extra_checker), checker_return);
2057       BOOST_INTRUSIVE_INVARIANT_ASSERT(!constant_time_size || this->sz_traits().get_size() == checker_return.node_count);
2058    }
2059 
2060    //! <b>Effects</b>: Asserts the integrity of the container.
2061    //!
2062    //! <b>Complexity</b>: Linear time.
2063    //!
2064    //! <b>Note</b>: The method has no effect when asserts are turned off (e.g., with NDEBUG).
2065    //!   Experimental function, interface might change in future versions.
2066    void check() const
2067    {
2068       check(detail::empty_node_checker<ValueTraits>());
2069    }
2070 
2071    friend bool operator==(const bstree_impl &x, const bstree_impl &y)
2072    {
2073       if(constant_time_size && x.size() != y.size()){
2074          return false;
2075       }
2076       return boost::intrusive::algo_equal(x.cbegin(), x.cend(), y.cbegin(), y.cend());
2077    }
2078 
2079    friend bool operator!=(const bstree_impl &x, const bstree_impl &y)
2080    {  return !(x == y); }
2081 
2082    friend bool operator<(const bstree_impl &x, const bstree_impl &y)
2083    {  return ::boost::intrusive::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());  }
2084 
2085    friend bool operator>(const bstree_impl &x, const bstree_impl &y)
2086    {  return y < x;  }
2087 
2088    friend bool operator<=(const bstree_impl &x, const bstree_impl &y)
2089    {  return !(x > y);  }
2090 
2091    friend bool operator>=(const bstree_impl &x, const bstree_impl &y)
2092    {  return !(x < y);  }
2093 
2094    friend void swap(bstree_impl &x, bstree_impl &y)
2095    {  x.swap(y);  }
2096 
2097    /// @cond
2098    private:
2099    template<class Disposer>
2100    iterator private_erase(const_iterator b, const_iterator e, size_type &n, Disposer disposer)
2101    {
2102       for(n = 0; b != e; ++n)
2103         this->erase_and_dispose(b++, disposer);
2104       return b.unconst();
2105    }
2106 
2107    iterator private_erase(const_iterator b, const_iterator e, size_type &n)
2108    {
2109       for(n = 0; b != e; ++n)
2110         this->erase(b++);
2111       return b.unconst();
2112    }
2113    /// @endcond
2114 };
2115 
2116 //! Helper metafunction to define a \c bstree that yields to the same type when the
2117 //! same options (either explicitly or implicitly) are used.
2118 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
2119 template<class T, class ...Options>
2120 #else
2121 template<class T, class O1 = void, class O2 = void
2122                 , class O3 = void, class O4 = void
2123                 , class O5 = void, class O6 = void>
2124 #endif
2125 struct make_bstree
2126 {
2127    /// @cond
2128    typedef typename pack_options
2129       < bstree_defaults,
2130       #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
2131       O1, O2, O3, O4, O5, O6
2132       #else
2133       Options...
2134       #endif
2135       >::type packed_options;
2136 
2137    typedef typename detail::get_value_traits
2138       <T, typename packed_options::proto_value_traits>::type value_traits;
2139 
2140    typedef bstree_impl
2141          < value_traits
2142          , typename packed_options::key_of_value
2143          , typename packed_options::compare
2144          , typename packed_options::size_type
2145          , packed_options::constant_time_size
2146          , BsTreeAlgorithms
2147          , typename packed_options::header_holder_type
2148          > implementation_defined;
2149    /// @endcond
2150    typedef implementation_defined type;
2151 };
2152 
2153 
2154 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
2155 
2156 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
2157 template<class T, class O1, class O2, class O3, class O4, class O5, class O6>
2158 #else
2159 template<class T, class ...Options>
2160 #endif
2161 class bstree
2162    :  public make_bstree<T,
2163       #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
2164       O1, O2, O3, O4, O5, O6
2165       #else
2166       Options...
2167       #endif
2168       >::type
2169 {
2170    typedef typename make_bstree
2171       <T,
2172       #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
2173       O1, O2, O3, O4, O5, O6
2174       #else
2175       Options...
2176       #endif
2177       >::type   Base;
2178    BOOST_MOVABLE_BUT_NOT_COPYABLE(bstree)
2179 
2180    public:
2181    typedef typename Base::key_compare        key_compare;
2182    typedef typename Base::value_traits       value_traits;
2183    typedef typename Base::iterator           iterator;
2184    typedef typename Base::const_iterator     const_iterator;
2185 
2186    //Assert if passed value traits are compatible with the type
2187    BOOST_STATIC_ASSERT((detail::is_same<typename value_traits::value_type, T>::value));
2188 
2189    BOOST_INTRUSIVE_FORCEINLINE bstree()
2190       :  Base()
2191    {}
2192 
2193    BOOST_INTRUSIVE_FORCEINLINE explicit bstree( const key_compare &cmp, const value_traits &v_traits = value_traits())
2194       :  Base(cmp, v_traits)
2195    {}
2196 
2197    template<class Iterator>
2198    BOOST_INTRUSIVE_FORCEINLINE bstree( bool unique, Iterator b, Iterator e
2199          , const key_compare &cmp = key_compare()
2200          , const value_traits &v_traits = value_traits())
2201       :  Base(unique, b, e, cmp, v_traits)
2202    {}
2203 
2204    BOOST_INTRUSIVE_FORCEINLINE bstree(BOOST_RV_REF(bstree) x)
2205       :  Base(BOOST_MOVE_BASE(Base, x))
2206    {}
2207 
2208    BOOST_INTRUSIVE_FORCEINLINE bstree& operator=(BOOST_RV_REF(bstree) x)
2209    {  return static_cast<bstree &>(this->Base::operator=(BOOST_MOVE_BASE(Base, x)));  }
2210 
2211    template <class Cloner, class Disposer>
2212    BOOST_INTRUSIVE_FORCEINLINE void clone_from(const bstree &src, Cloner cloner, Disposer disposer)
2213    {  Base::clone_from(src, cloner, disposer);  }
2214 
2215    template <class Cloner, class Disposer>
2216    BOOST_INTRUSIVE_FORCEINLINE void clone_from(BOOST_RV_REF(bstree) src, Cloner cloner, Disposer disposer)
2217    {  Base::clone_from(BOOST_MOVE_BASE(Base, src), cloner, disposer);  }
2218 
2219    BOOST_INTRUSIVE_FORCEINLINE static bstree &container_from_end_iterator(iterator end_iterator) BOOST_NOEXCEPT
2220    {  return static_cast<bstree &>(Base::container_from_end_iterator(end_iterator));   }
2221 
2222    BOOST_INTRUSIVE_FORCEINLINE static const bstree &container_from_end_iterator(const_iterator end_iterator) BOOST_NOEXCEPT
2223    {  return static_cast<const bstree &>(Base::container_from_end_iterator(end_iterator));   }
2224 
2225    BOOST_INTRUSIVE_FORCEINLINE static bstree &container_from_iterator(iterator it) BOOST_NOEXCEPT
2226    {  return static_cast<bstree &>(Base::container_from_iterator(it));   }
2227 
2228    BOOST_INTRUSIVE_FORCEINLINE static const bstree &container_from_iterator(const_iterator it) BOOST_NOEXCEPT
2229    {  return static_cast<const bstree &>(Base::container_from_iterator(it));   }
2230 };
2231 
2232 #endif
2233 } //namespace intrusive
2234 } //namespace boost
2235 
2236 #include <boost/intrusive/detail/config_end.hpp>
2237 
2238 #endif //BOOST_INTRUSIVE_BSTREE_HPP