Chris@16: ///////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@101: // (C) Copyright Ion Gaztanaga 2013-2014 Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. Chris@16: // (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org/libs/intrusive for documentation. Chris@16: // Chris@16: ///////////////////////////////////////////////////////////////////////////// Chris@16: #ifndef BOOST_INTRUSIVE_BSTREE_HPP Chris@16: #define BOOST_INTRUSIVE_BSTREE_HPP Chris@16: Chris@16: #include Chris@101: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: #include Chris@101: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: Chris@101: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: Chris@101: #include Chris@101: #include //size_t... Chris@101: #include //less, equal_to Chris@101: Chris@101: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@101: # pragma once Chris@101: #endif Chris@16: Chris@16: namespace boost { Chris@16: namespace intrusive { Chris@16: Chris@16: /// @cond Chris@16: Chris@101: struct default_bstree_hook_applier Chris@101: { template struct apply{ typedef typename T::default_bstree_hook type; }; }; Chris@101: Chris@101: template<> Chris@101: struct is_default_hook_tag Chris@101: { static const bool value = true; }; Chris@101: Chris@16: struct bstree_defaults Chris@16: { Chris@101: typedef default_bstree_hook_applier proto_value_traits; Chris@16: static const bool constant_time_size = true; Chris@16: typedef std::size_t size_type; Chris@16: typedef void compare; Chris@16: static const bool floating_point = true; //For sgtree Chris@16: typedef void priority; //For treap Chris@101: typedef void header_holder_type; Chris@16: }; Chris@16: Chris@101: template Chris@16: struct bstbase3 Chris@16: { Chris@16: typedef ValueTraits value_traits; Chris@101: typedef typename value_traits::node_traits node_traits; Chris@16: typedef typename node_traits::node node_type; Chris@16: typedef typename get_algo::type node_algorithms; Chris@16: typedef typename node_traits::node_ptr node_ptr; Chris@16: typedef typename node_traits::const_node_ptr const_node_ptr; Chris@101: typedef tree_iterator iterator; Chris@101: typedef tree_iterator const_iterator; Chris@101: typedef boost::intrusive::reverse_iterator reverse_iterator; Chris@101: typedef boost::intrusive::reverse_iterator const_reverse_iterator; Chris@101: typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::pointer) pointer; Chris@101: typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::const_pointer) const_pointer; Chris@16: typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::element_type) value_type; Chris@16: typedef BOOST_INTRUSIVE_IMPDEF(value_type) key_type; Chris@16: typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::reference) reference; Chris@16: typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::reference) const_reference; Chris@16: typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::difference_type) difference_type; Chris@101: typedef typename detail::get_header_holder_type Chris@101: < value_traits,HeaderHolder >::type header_holder_type; Chris@101: Chris@101: static const bool safemode_or_autounlink = is_safe_autounlink::value; Chris@101: static const bool stateful_value_traits = detail::is_stateful_value_traits::value; Chris@101: static const bool has_container_from_iterator = Chris@101: detail::is_same< header_holder_type, detail::default_header_holder< node_traits > >::value; Chris@101: Chris@101: struct holder_t : public ValueTraits Chris@101: { Chris@101: explicit holder_t(const ValueTraits &vtraits) Chris@101: : ValueTraits(vtraits) Chris@101: {} Chris@101: header_holder_type root; Chris@101: } holder; Chris@101: Chris@101: static bstbase3 &get_tree_base_from_end_iterator(const const_iterator &end_iterator) Chris@101: { Chris@101: BOOST_STATIC_ASSERT(has_container_from_iterator); Chris@101: node_ptr p = end_iterator.pointed_node(); Chris@101: header_holder_type* h = header_holder_type::get_holder(p); Chris@101: holder_t *holder = get_parent_from_member(h, &holder_t::root); Chris@101: bstbase3 *base = get_parent_from_member (holder, &bstbase3::holder); Chris@101: return *base; Chris@101: } Chris@101: Chris@101: bstbase3(const ValueTraits &vtraits) Chris@101: : holder(vtraits) Chris@101: { Chris@101: node_algorithms::init_header(this->header_ptr()); Chris@101: } Chris@101: Chris@101: node_ptr header_ptr() Chris@101: { return holder.root.get_node(); } Chris@101: Chris@101: const_node_ptr header_ptr() const Chris@101: { return holder.root.get_node(); } Chris@101: Chris@101: const value_traits &get_value_traits() const Chris@101: { return this->holder; } Chris@101: Chris@101: value_traits &get_value_traits() Chris@101: { return this->holder; } Chris@101: Chris@101: typedef typename boost::intrusive::value_traits_pointers Chris@101: ::const_value_traits_ptr const_value_traits_ptr; Chris@101: Chris@101: const_value_traits_ptr priv_value_traits_ptr() const Chris@101: { return pointer_traits::pointer_to(this->get_value_traits()); } Chris@16: Chris@16: iterator begin() Chris@101: { return iterator(node_algorithms::begin_node(this->header_ptr()), this->priv_value_traits_ptr()); } Chris@16: Chris@16: const_iterator begin() const Chris@16: { return cbegin(); } Chris@16: Chris@16: const_iterator cbegin() const Chris@101: { return const_iterator(node_algorithms::begin_node(this->header_ptr()), this->priv_value_traits_ptr()); } Chris@16: Chris@16: iterator end() Chris@101: { return iterator(node_algorithms::end_node(this->header_ptr()), this->priv_value_traits_ptr()); } Chris@16: Chris@16: const_iterator end() const Chris@16: { return cend(); } Chris@16: Chris@16: const_iterator cend() const Chris@101: { return const_iterator(node_algorithms::end_node(this->header_ptr()), this->priv_value_traits_ptr()); } Chris@101: Chris@101: iterator root() Chris@101: { return iterator(node_algorithms::root_node(this->header_ptr()), this->priv_value_traits_ptr()); } Chris@101: Chris@101: const_iterator root() const Chris@101: { return croot(); } Chris@101: Chris@101: const_iterator croot() const Chris@101: { return const_iterator(node_algorithms::root_node(this->header_ptr()), this->priv_value_traits_ptr()); } Chris@16: Chris@16: reverse_iterator rbegin() Chris@16: { return reverse_iterator(end()); } Chris@16: Chris@16: const_reverse_iterator rbegin() const Chris@16: { return const_reverse_iterator(end()); } Chris@16: Chris@16: const_reverse_iterator crbegin() const Chris@16: { return const_reverse_iterator(end()); } Chris@16: Chris@16: reverse_iterator rend() Chris@16: { return reverse_iterator(begin()); } Chris@16: Chris@16: const_reverse_iterator rend() const Chris@16: { return const_reverse_iterator(begin()); } Chris@16: Chris@16: const_reverse_iterator crend() const Chris@16: { return const_reverse_iterator(begin()); } Chris@16: Chris@16: void replace_node(iterator replace_this, reference with_this) Chris@16: { Chris@101: node_algorithms::replace_node( get_value_traits().to_node_ptr(*replace_this) Chris@16: , this->header_ptr() Chris@101: , get_value_traits().to_node_ptr(with_this)); Chris@16: if(safemode_or_autounlink) Chris@16: node_algorithms::init(replace_this.pointed_node()); Chris@16: } Chris@16: Chris@16: void rebalance() Chris@16: { node_algorithms::rebalance(this->header_ptr()); } Chris@16: Chris@16: iterator rebalance_subtree(iterator root) Chris@101: { return iterator(node_algorithms::rebalance_subtree(root.pointed_node()), this->priv_value_traits_ptr()); } Chris@16: Chris@16: static iterator s_iterator_to(reference value) Chris@16: { Chris@16: BOOST_STATIC_ASSERT((!stateful_value_traits)); Chris@101: return iterator (value_traits::to_node_ptr(value), const_value_traits_ptr()); Chris@16: } Chris@16: Chris@16: static const_iterator s_iterator_to(const_reference value) Chris@16: { Chris@16: BOOST_STATIC_ASSERT((!stateful_value_traits)); Chris@101: return const_iterator (value_traits::to_node_ptr(*pointer_traits::const_cast_from(pointer_traits::pointer_to(value))), const_value_traits_ptr()); Chris@16: } Chris@16: Chris@16: iterator iterator_to(reference value) Chris@101: { return iterator (this->get_value_traits().to_node_ptr(value), this->priv_value_traits_ptr()); } Chris@16: Chris@16: const_iterator iterator_to(const_reference value) const Chris@101: { return const_iterator (this->get_value_traits().to_node_ptr(*pointer_traits::const_cast_from(pointer_traits::pointer_to(value))), this->priv_value_traits_ptr()); } Chris@16: Chris@16: static void init_node(reference value) Chris@16: { node_algorithms::init(value_traits::to_node_ptr(value)); } Chris@16: Chris@16: }; Chris@16: Chris@101: template Chris@101: struct get_compare Chris@101: { Chris@101: typedef Less type; Chris@101: }; Chris@101: Chris@101: template Chris@101: struct get_compare Chris@101: { Chris@101: typedef ::std::less type; Chris@101: }; Chris@101: Chris@101: template Chris@16: struct bstbase2 Chris@101: //Put the (possibly empty) functor in the first position to get EBO in MSVC Chris@101: //Use public inheritance to avoid MSVC bugs with closures Chris@101: : public detail::ebo_functor_holder::type> Chris@101: , public bstbase3 Chris@16: { Chris@101: typedef bstbase3 treeheader_t; Chris@101: typedef typename treeheader_t::value_traits value_traits; Chris@16: typedef typename treeheader_t::node_algorithms node_algorithms; Chris@101: typedef typename get_compare Chris@101: < VoidOrKeyComp, typename value_traits::value_type>::type value_compare; Chris@16: typedef BOOST_INTRUSIVE_IMPDEF(value_compare) key_compare; Chris@16: typedef typename treeheader_t::iterator iterator; Chris@16: typedef typename treeheader_t::const_iterator const_iterator; Chris@16: typedef typename treeheader_t::node_ptr node_ptr; Chris@16: typedef typename treeheader_t::const_node_ptr const_node_ptr; Chris@16: Chris@16: bstbase2(const value_compare &comp, const ValueTraits &vtraits) Chris@101: : detail::ebo_functor_holder(comp), treeheader_t(vtraits) Chris@16: {} Chris@16: Chris@16: const value_compare &comp() const Chris@16: { return this->get(); } Chris@101: Chris@16: value_compare &comp() Chris@16: { return this->get(); } Chris@16: Chris@101: typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::pointer) pointer; Chris@101: typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::const_pointer) const_pointer; Chris@16: typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::element_type) value_type; Chris@16: typedef BOOST_INTRUSIVE_IMPDEF(value_type) key_type; Chris@16: typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::reference) reference; Chris@16: typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::reference) const_reference; Chris@16: typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::difference_type) difference_type; Chris@16: typedef typename node_algorithms::insert_commit_data insert_commit_data; Chris@16: Chris@16: value_compare value_comp() const Chris@16: { return this->comp(); } Chris@16: Chris@16: key_compare key_comp() const Chris@16: { return this->comp(); } Chris@16: Chris@101: //lower_bound Chris@16: iterator lower_bound(const_reference value) Chris@16: { return this->lower_bound(value, this->comp()); } Chris@16: Chris@16: const_iterator lower_bound(const_reference value) const Chris@16: { return this->lower_bound(value, this->comp()); } Chris@16: Chris@16: template Chris@16: iterator lower_bound(const KeyType &key, KeyValueCompare comp) Chris@16: { Chris@101: detail::key_nodeptr_comp Chris@101: key_node_comp(comp, &this->get_value_traits()); Chris@16: return iterator(node_algorithms::lower_bound Chris@101: (this->header_ptr(), key, key_node_comp), this->priv_value_traits_ptr()); Chris@16: } Chris@16: Chris@16: template Chris@16: const_iterator lower_bound(const KeyType &key, KeyValueCompare comp) const Chris@16: { Chris@101: detail::key_nodeptr_comp Chris@101: key_node_comp(comp, &this->get_value_traits()); Chris@16: return const_iterator(node_algorithms::lower_bound Chris@101: (this->header_ptr(), key, key_node_comp), this->priv_value_traits_ptr()); Chris@16: } Chris@16: Chris@101: //upper_bound Chris@16: iterator upper_bound(const_reference value) Chris@16: { return this->upper_bound(value, this->comp()); } Chris@16: Chris@16: template Chris@16: iterator upper_bound(const KeyType &key, KeyValueCompare comp) Chris@16: { Chris@101: detail::key_nodeptr_comp Chris@101: key_node_comp(comp, &this->get_value_traits()); Chris@16: return iterator(node_algorithms::upper_bound Chris@101: (this->header_ptr(), key, key_node_comp), this->priv_value_traits_ptr()); Chris@16: } Chris@16: Chris@16: const_iterator upper_bound(const_reference value) const Chris@16: { return this->upper_bound(value, this->comp()); } Chris@16: Chris@16: template Chris@16: const_iterator upper_bound(const KeyType &key, KeyValueCompare comp) const Chris@16: { Chris@101: detail::key_nodeptr_comp Chris@101: key_node_comp(comp, &this->get_value_traits()); Chris@16: return const_iterator(node_algorithms::upper_bound Chris@101: (this->header_ptr(), key, key_node_comp), this->priv_value_traits_ptr()); Chris@16: } Chris@16: Chris@101: //find Chris@16: iterator find(const_reference value) Chris@16: { return this->find(value, this->comp()); } Chris@16: Chris@16: template Chris@16: iterator find(const KeyType &key, KeyValueCompare comp) Chris@16: { Chris@101: detail::key_nodeptr_comp Chris@101: key_node_comp(comp, &this->get_value_traits()); Chris@16: return iterator Chris@101: (node_algorithms::find(this->header_ptr(), key, key_node_comp), this->priv_value_traits_ptr()); Chris@16: } Chris@16: Chris@16: const_iterator find(const_reference value) const Chris@16: { return this->find(value, this->comp()); } Chris@16: Chris@16: template Chris@16: const_iterator find(const KeyType &key, KeyValueCompare comp) const Chris@16: { Chris@101: detail::key_nodeptr_comp Chris@101: key_node_comp(comp, &this->get_value_traits()); Chris@16: return const_iterator Chris@101: (node_algorithms::find(this->header_ptr(), key, key_node_comp), this->priv_value_traits_ptr()); Chris@16: } Chris@16: Chris@101: //equal_range Chris@16: std::pair equal_range(const_reference value) Chris@16: { return this->equal_range(value, this->comp()); } Chris@16: Chris@16: template Chris@16: std::pair equal_range(const KeyType &key, KeyValueCompare comp) Chris@16: { Chris@101: detail::key_nodeptr_comp Chris@101: key_node_comp(comp, &this->get_value_traits()); Chris@16: std::pair ret Chris@16: (node_algorithms::equal_range(this->header_ptr(), key, key_node_comp)); Chris@101: return std::pair( iterator(ret.first, this->priv_value_traits_ptr()) Chris@101: , iterator(ret.second, this->priv_value_traits_ptr())); Chris@16: } Chris@16: Chris@16: std::pair Chris@16: equal_range(const_reference value) const Chris@16: { return this->equal_range(value, this->comp()); } Chris@16: Chris@16: template Chris@16: std::pair Chris@16: equal_range(const KeyType &key, KeyValueCompare comp) const Chris@16: { Chris@101: detail::key_nodeptr_comp Chris@101: key_node_comp(comp, &this->get_value_traits()); Chris@16: std::pair ret Chris@16: (node_algorithms::equal_range(this->header_ptr(), key, key_node_comp)); Chris@101: return std::pair( const_iterator(ret.first, this->priv_value_traits_ptr()) Chris@101: , const_iterator(ret.second, this->priv_value_traits_ptr())); Chris@16: } Chris@16: Chris@101: //lower_bound_range Chris@101: std::pair lower_bound_range(const_reference value) Chris@101: { return this->lower_bound_range(value, this->comp()); } Chris@101: Chris@101: template Chris@101: std::pair lower_bound_range(const KeyType &key, KeyValueCompare comp) Chris@101: { Chris@101: detail::key_nodeptr_comp Chris@101: key_node_comp(comp, &this->get_value_traits()); Chris@101: std::pair ret Chris@101: (node_algorithms::lower_bound_range(this->header_ptr(), key, key_node_comp)); Chris@101: return std::pair( iterator(ret.first, this->priv_value_traits_ptr()) Chris@101: , iterator(ret.second, this->priv_value_traits_ptr())); Chris@101: } Chris@101: Chris@101: std::pair Chris@101: lower_bound_range(const_reference value) const Chris@101: { return this->lower_bound_range(value, this->comp()); } Chris@101: Chris@101: template Chris@101: std::pair Chris@101: lower_bound_range(const KeyType &key, KeyValueCompare comp) const Chris@101: { Chris@101: detail::key_nodeptr_comp Chris@101: key_node_comp(comp, &this->get_value_traits()); Chris@101: std::pair ret Chris@101: (node_algorithms::lower_bound_range(this->header_ptr(), key, key_node_comp)); Chris@101: return std::pair( const_iterator(ret.first, this->priv_value_traits_ptr()) Chris@101: , const_iterator(ret.second, this->priv_value_traits_ptr())); Chris@101: } Chris@101: Chris@101: //bounded_range Chris@16: std::pair bounded_range Chris@16: (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed) Chris@16: { return this->bounded_range(lower_value, upper_value, this->comp(), left_closed, right_closed); } Chris@16: Chris@16: template Chris@16: std::pair bounded_range Chris@16: (const KeyType &lower_key, const KeyType &upper_key, KeyValueCompare comp, bool left_closed, bool right_closed) Chris@16: { Chris@101: detail::key_nodeptr_comp Chris@101: key_node_comp(comp, &this->get_value_traits()); Chris@16: std::pair ret Chris@16: (node_algorithms::bounded_range Chris@16: (this->header_ptr(), lower_key, upper_key, key_node_comp, left_closed, right_closed)); Chris@101: return std::pair( iterator(ret.first, this->priv_value_traits_ptr()) Chris@101: , iterator(ret.second, this->priv_value_traits_ptr())); Chris@16: } Chris@16: Chris@16: std::pair bounded_range Chris@16: (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed) const Chris@16: { return this->bounded_range(lower_value, upper_value, this->comp(), left_closed, right_closed); } Chris@16: Chris@16: template Chris@16: std::pair bounded_range Chris@16: (const KeyType &lower_key, const KeyType &upper_key, KeyValueCompare comp, bool left_closed, bool right_closed) const Chris@16: { Chris@101: detail::key_nodeptr_comp Chris@101: key_node_comp(comp, &this->get_value_traits()); Chris@16: std::pair ret Chris@16: (node_algorithms::bounded_range Chris@16: (this->header_ptr(), lower_key, upper_key, key_node_comp, left_closed, right_closed)); Chris@101: return std::pair( const_iterator(ret.first, this->priv_value_traits_ptr()) Chris@101: , const_iterator(ret.second, this->priv_value_traits_ptr())); Chris@16: } Chris@16: Chris@101: //insert_unique_check Chris@16: template Chris@16: std::pair insert_unique_check Chris@16: (const KeyType &key, KeyValueCompare key_value_comp, insert_commit_data &commit_data) Chris@16: { Chris@101: detail::key_nodeptr_comp Chris@101: ocomp(key_value_comp, &this->get_value_traits()); Chris@16: std::pair ret = Chris@16: (node_algorithms::insert_unique_check Chris@16: (this->header_ptr(), key, ocomp, commit_data)); Chris@101: return std::pair(iterator(ret.first, this->priv_value_traits_ptr()), ret.second); Chris@16: } Chris@16: Chris@16: template Chris@16: std::pair insert_unique_check Chris@16: (const_iterator hint, const KeyType &key Chris@16: ,KeyValueCompare key_value_comp, insert_commit_data &commit_data) Chris@16: { Chris@101: detail::key_nodeptr_comp Chris@101: ocomp(key_value_comp, &this->get_value_traits()); Chris@16: std::pair ret = Chris@16: (node_algorithms::insert_unique_check Chris@16: (this->header_ptr(), hint.pointed_node(), key, ocomp, commit_data)); Chris@101: return std::pair(iterator(ret.first, this->priv_value_traits_ptr()), ret.second); Chris@16: } Chris@16: }; Chris@16: Chris@101: //Due to MSVC's EBO implementation, to save space and maintain the ABI, we must put the non-empty size member Chris@101: //in the first position, but if size is not going to be stored then we'll use an specialization Chris@101: //that doesn't inherit from size_holder Chris@101: template Chris@101: struct bstbase_hack Chris@101: : public detail::size_holder Chris@101: , public bstbase2 < ValueTraits, VoidOrKeyComp, AlgoType, HeaderHolder> Chris@101: { Chris@101: typedef bstbase2< ValueTraits, VoidOrKeyComp, AlgoType, HeaderHolder> base_type; Chris@101: typedef typename base_type::value_compare value_compare; Chris@101: typedef SizeType size_type; Chris@101: typedef typename base_type::node_traits node_traits; Chris@101: typedef typename get_algo Chris@101: ::type algo_type; Chris@101: Chris@101: bstbase_hack(const value_compare & comp, const ValueTraits &vtraits) Chris@101: : base_type(comp, vtraits) Chris@101: { Chris@101: this->sz_traits().set_size(size_type(0)); Chris@101: } Chris@101: Chris@101: typedef detail::size_holder size_traits; Chris@101: Chris@101: size_traits &sz_traits() Chris@101: { return static_cast(*this); } Chris@101: Chris@101: const size_traits &sz_traits() const Chris@101: { return static_cast(*this); } Chris@101: }; Chris@101: Chris@101: //Specialization for ConstantTimeSize == false Chris@101: template Chris@101: struct bstbase_hack Chris@101: : public bstbase2 < ValueTraits, VoidOrKeyComp, AlgoType, HeaderHolder> Chris@101: { Chris@101: typedef bstbase2< ValueTraits, VoidOrKeyComp, AlgoType, HeaderHolder> base_type; Chris@101: typedef typename base_type::value_compare value_compare; Chris@101: bstbase_hack(const value_compare & comp, const ValueTraits &vtraits) Chris@101: : base_type(comp, vtraits) Chris@101: {} Chris@101: Chris@101: typedef detail::size_holder size_traits; Chris@101: Chris@101: size_traits &sz_traits() Chris@101: { return s_size_traits; } Chris@101: Chris@101: const size_traits &sz_traits() const Chris@101: { return s_size_traits; } Chris@101: Chris@101: static size_traits s_size_traits; Chris@101: }; Chris@101: Chris@101: template Chris@101: detail::size_holder bstbase_hack::s_size_traits; Chris@101: Chris@101: //This class will Chris@101: template Chris@16: struct bstbase Chris@101: : public bstbase_hack< ValueTraits, VoidOrKeyComp, ConstantTimeSize, SizeType, AlgoType, HeaderHolder> Chris@16: { Chris@101: typedef bstbase_hack< ValueTraits, VoidOrKeyComp, ConstantTimeSize, SizeType, AlgoType, HeaderHolder> base_type; Chris@101: typedef ValueTraits value_traits; Chris@16: typedef typename base_type::value_compare value_compare; Chris@101: typedef value_compare key_compare; Chris@16: typedef typename base_type::const_reference const_reference; Chris@16: typedef typename base_type::reference reference; Chris@16: typedef typename base_type::iterator iterator; Chris@16: typedef typename base_type::const_iterator const_iterator; Chris@16: typedef typename base_type::node_traits node_traits; Chris@16: typedef typename get_algo Chris@101: ::type node_algorithms; Chris@16: typedef SizeType size_type; Chris@16: Chris@16: bstbase(const value_compare & comp, const ValueTraits &vtraits) Chris@16: : base_type(comp, vtraits) Chris@16: {} Chris@16: Chris@101: //Detach all inserted nodes. This will add exception safety to bstree_impl Chris@101: //constructors inserting elements. Chris@101: ~bstbase() Chris@16: { Chris@101: if(is_safe_autounlink::value){ Chris@101: node_algorithms::clear_and_dispose Chris@101: ( this->header_ptr() Chris@101: , detail::node_disposer Chris@101: (detail::null_disposer(), &this->get_value_traits())); Chris@101: node_algorithms::init(this->header_ptr()); Chris@16: } Chris@16: } Chris@16: }; Chris@16: Chris@16: Chris@16: /// @endcond Chris@16: Chris@16: //! The class template bstree is an unbalanced intrusive binary search tree Chris@16: //! container. The no-throw guarantee holds only, if the value_compare object Chris@16: //! doesn't throw. Chris@16: //! Chris@16: //! The complexity guarantees only hold if the tree is balanced, logarithmic Chris@16: //! complexity would increase to linear if the tree is totally unbalanced. Chris@16: //! Chris@16: //! The template parameter \c T is the type to be managed by the container. Chris@16: //! The user can specify additional options and if no options are provided Chris@16: //! default options are used. Chris@16: //! Chris@16: //! The container supports the following options: Chris@16: //! \c base_hook<>/member_hook<>/value_traits<>, Chris@16: //! \c constant_time_size<>, \c size_type<> and Chris@16: //! \c compare<>. Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: template Chris@16: #else Chris@101: template Chris@16: #endif Chris@16: class bstree_impl Chris@101: : public bstbase Chris@16: { Chris@16: public: Chris@16: /// @cond Chris@101: typedef bstbase data_type; Chris@101: typedef tree_iterator iterator_type; Chris@101: typedef tree_iterator const_iterator_type; Chris@16: /// @endcond Chris@16: Chris@101: typedef BOOST_INTRUSIVE_IMPDEF(ValueTraits) value_traits; Chris@101: typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::pointer) pointer; Chris@101: typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::const_pointer) const_pointer; Chris@16: typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::element_type) value_type; Chris@16: typedef BOOST_INTRUSIVE_IMPDEF(value_type) key_type; Chris@16: typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::reference) reference; Chris@16: typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::reference) const_reference; Chris@16: typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::difference_type) difference_type; Chris@16: typedef BOOST_INTRUSIVE_IMPDEF(SizeType) size_type; Chris@16: typedef BOOST_INTRUSIVE_IMPDEF(typename data_type::value_compare) value_compare; Chris@16: typedef BOOST_INTRUSIVE_IMPDEF(value_compare) key_compare; Chris@16: typedef BOOST_INTRUSIVE_IMPDEF(iterator_type) iterator; Chris@16: typedef BOOST_INTRUSIVE_IMPDEF(const_iterator_type) const_iterator; Chris@101: typedef BOOST_INTRUSIVE_IMPDEF(boost::intrusive::reverse_iterator) reverse_iterator; Chris@101: typedef BOOST_INTRUSIVE_IMPDEF(boost::intrusive::reverse_iterator) const_reverse_iterator; Chris@101: typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::node_traits) node_traits; Chris@16: typedef BOOST_INTRUSIVE_IMPDEF(typename node_traits::node) node; Chris@16: typedef BOOST_INTRUSIVE_IMPDEF(typename node_traits::node_ptr) node_ptr; Chris@16: typedef BOOST_INTRUSIVE_IMPDEF(typename node_traits::const_node_ptr) const_node_ptr; Chris@16: /// @cond Chris@16: typedef typename get_algo::type algo_type; Chris@16: /// @endcond Chris@16: typedef BOOST_INTRUSIVE_IMPDEF(algo_type) node_algorithms; Chris@16: Chris@16: static const bool constant_time_size = ConstantTimeSize; Chris@101: static const bool stateful_value_traits = detail::is_stateful_value_traits::value; Chris@16: /// @cond Chris@16: private: Chris@16: Chris@16: //noncopyable Chris@16: BOOST_MOVABLE_BUT_NOT_COPYABLE(bstree_impl) Chris@16: Chris@101: static const bool safemode_or_autounlink = is_safe_autounlink::value; Chris@16: Chris@16: //Constant-time size is incompatible with auto-unlink hooks! Chris@101: BOOST_STATIC_ASSERT(!(constant_time_size && ((int)value_traits::link_mode == (int)auto_unlink))); Chris@16: Chris@16: Chris@16: protected: Chris@16: Chris@16: Chris@16: /// @endcond Chris@16: Chris@16: public: Chris@16: Chris@16: typedef typename node_algorithms::insert_commit_data insert_commit_data; Chris@16: Chris@16: //! Effects: Constructs an empty container. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: If value_traits::node_traits::node Chris@16: //! constructor throws (this does not happen with predefined Boost.Intrusive hooks) Chris@101: //! or the copy constructor of the value_compare object throws. Basic guarantee. Chris@16: explicit bstree_impl( const value_compare &cmp = value_compare() Chris@16: , const value_traits &v_traits = value_traits()) Chris@16: : data_type(cmp, v_traits) Chris@101: {} Chris@16: Chris@16: //! Requires: Dereferencing iterator must yield an lvalue of type value_type. Chris@16: //! cmp must be a comparison function that induces a strict weak ordering. Chris@16: //! Chris@16: //! Effects: Constructs an empty container and inserts elements from Chris@16: //! [b, e). Chris@16: //! Chris@16: //! Complexity: Linear in N if [b, e) is already sorted using Chris@16: //! comp and otherwise N * log N, where N is the distance between first and last. Chris@16: //! Chris@16: //! Throws: If value_traits::node_traits::node Chris@16: //! constructor throws (this does not happen with predefined Boost.Intrusive hooks) Chris@16: //! or the copy constructor/operator() of the value_compare object throws. Basic guarantee. Chris@16: template Chris@16: bstree_impl( bool unique, Iterator b, Iterator e Chris@16: , const value_compare &cmp = value_compare() Chris@16: , const value_traits &v_traits = value_traits()) Chris@16: : data_type(cmp, v_traits) Chris@16: { Chris@101: //bstbase releases elements in case of exceptions Chris@16: if(unique) Chris@16: this->insert_unique(b, e); Chris@16: else Chris@16: this->insert_equal(b, e); Chris@16: } Chris@16: Chris@16: //! Effects: to-do Chris@16: //! Chris@16: bstree_impl(BOOST_RV_REF(bstree_impl) x) Chris@101: : data_type(::boost::move(x.comp()), ::boost::move(x.get_value_traits())) Chris@16: { Chris@16: this->swap(x); Chris@16: } Chris@16: Chris@16: //! Effects: to-do Chris@16: //! Chris@16: bstree_impl& operator=(BOOST_RV_REF(bstree_impl) x) Chris@16: { this->swap(x); return *this; } Chris@16: Chris@16: #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@16: //! Effects: Detaches all elements from this. The objects in the set Chris@16: //! are not deleted (i.e. no destructors are called), but the nodes according to Chris@16: //! the value_traits template parameter are reinitialized and thus can be reused. Chris@16: //! Chris@16: //! Complexity: Linear to elements contained in *this. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: ~bstree_impl() Chris@16: {} Chris@16: Chris@16: //! Effects: Returns an iterator pointing to the beginning of the container. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: iterator begin(); Chris@16: Chris@16: //! Effects: Returns a const_iterator pointing to the beginning of the container. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: const_iterator begin() const; Chris@16: Chris@16: //! Effects: Returns a const_iterator pointing to the beginning of the container. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: const_iterator cbegin() const; Chris@16: Chris@16: //! Effects: Returns an iterator pointing to the end of the container. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: iterator end(); Chris@16: Chris@16: //! Effects: Returns a const_iterator pointing to the end of the container. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: const_iterator end() const; Chris@16: Chris@16: //! Effects: Returns a const_iterator pointing to the end of the container. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: const_iterator cend() const; Chris@16: Chris@16: //! Effects: Returns a reverse_iterator pointing to the beginning of the Chris@16: //! reversed container. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: reverse_iterator rbegin(); Chris@16: Chris@16: //! Effects: Returns a const_reverse_iterator pointing to the beginning Chris@16: //! of the reversed container. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: const_reverse_iterator rbegin() const; Chris@16: Chris@16: //! Effects: Returns a const_reverse_iterator pointing to the beginning Chris@16: //! of the reversed container. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: const_reverse_iterator crbegin() const; Chris@16: Chris@16: //! Effects: Returns a reverse_iterator pointing to the end Chris@16: //! of the reversed container. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: reverse_iterator rend(); Chris@16: Chris@16: //! Effects: Returns a const_reverse_iterator pointing to the end Chris@16: //! of the reversed container. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: const_reverse_iterator rend() const; Chris@16: Chris@16: //! Effects: Returns a const_reverse_iterator pointing to the end Chris@16: //! of the reversed container. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: const_reverse_iterator crend() const; Chris@16: Chris@16: #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@16: Chris@16: //! Precondition: end_iterator must be a valid end iterator Chris@16: //! of the container. Chris@16: //! Chris@16: //! Effects: Returns a const reference to the container associated to the end iterator Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: static bstree_impl &container_from_end_iterator(iterator end_iterator) Chris@16: { Chris@101: return static_cast Chris@101: (data_type::get_tree_base_from_end_iterator(end_iterator)); Chris@16: } Chris@16: Chris@16: //! Precondition: end_iterator must be a valid end const_iterator Chris@16: //! of the container. Chris@16: //! Chris@16: //! Effects: Returns a const reference to the container associated to the iterator Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: static const bstree_impl &container_from_end_iterator(const_iterator end_iterator) Chris@16: { Chris@101: return static_cast Chris@101: (data_type::get_tree_base_from_end_iterator(end_iterator)); Chris@16: } Chris@16: Chris@16: //! Precondition: it must be a valid iterator Chris@16: //! of the container. Chris@16: //! Chris@16: //! Effects: Returns a const reference to the container associated to the iterator Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: static bstree_impl &container_from_iterator(iterator it) Chris@16: { return container_from_end_iterator(it.end_iterator_from_it()); } Chris@16: Chris@16: //! Precondition: it must be a valid end const_iterator Chris@16: //! of container. Chris@16: //! Chris@16: //! Effects: Returns a const reference to the container associated to the end iterator Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: static const bstree_impl &container_from_iterator(const_iterator it) Chris@16: { return container_from_end_iterator(it.end_iterator_from_it()); } Chris@16: Chris@16: #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@16: Chris@16: //! Effects: Returns the key_compare object used by the container. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: If value_compare copy-constructor throws. Chris@16: key_compare key_comp() const; Chris@101: Chris@16: //! Effects: Returns the value_compare object used by the container. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: If value_compare copy-constructor throws. Chris@16: value_compare value_comp() const; Chris@16: Chris@101: #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@101: Chris@16: //! Effects: Returns true if the container is empty. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@101: bool empty() const Chris@101: { Chris@101: if(ConstantTimeSize){ Chris@101: return !this->data_type::sz_traits().get_size(); Chris@101: } Chris@101: else{ Chris@101: return algo_type::unique(this->header_ptr()); Chris@101: } Chris@101: } Chris@16: Chris@16: //! Effects: Returns the number of elements stored in the container. Chris@16: //! Chris@16: //! Complexity: Linear to elements contained in *this Chris@16: //! if constant-time size option is disabled. Constant time otherwise. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: size_type size() const Chris@16: { Chris@16: if(constant_time_size) Chris@16: return this->sz_traits().get_size(); Chris@16: else{ Chris@16: return (size_type)node_algorithms::size(this->header_ptr()); Chris@16: } Chris@16: } Chris@16: Chris@16: //! Effects: Swaps the contents of two containers. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: If the comparison functor's swap call throws. Chris@16: void swap(bstree_impl& other) Chris@16: { Chris@16: //This can throw Chris@101: ::boost::adl_move_swap(this->comp(), this->comp()); Chris@16: //These can't throw Chris@16: node_algorithms::swap_tree(this->header_ptr(), node_ptr(other.header_ptr())); Chris@16: if(constant_time_size){ Chris@16: size_type backup = this->sz_traits().get_size(); Chris@16: this->sz_traits().set_size(other.sz_traits().get_size()); Chris@16: other.sz_traits().set_size(backup); Chris@16: } Chris@16: } Chris@16: Chris@16: //! Requires: Disposer::operator()(pointer) shouldn't throw. Chris@16: //! Cloner should yield to nodes equivalent to the original nodes. Chris@16: //! Chris@16: //! Effects: Erases all the elements from *this Chris@16: //! calling Disposer::operator()(pointer), clones all the Chris@16: //! elements from src calling Cloner::operator()(const_reference ) Chris@16: //! and inserts them on *this. Copies the predicate from the source container. Chris@16: //! Chris@16: //! If cloner throws, all cloned elements are unlinked and disposed Chris@16: //! calling Disposer::operator()(pointer). Chris@16: //! Chris@16: //! Complexity: Linear to erased plus inserted elements. Chris@16: //! Chris@16: //! Throws: If cloner throws or predicate copy assignment throws. Basic guarantee. Chris@16: template Chris@16: void clone_from(const bstree_impl &src, Cloner cloner, Disposer disposer) Chris@16: { Chris@16: this->clear_and_dispose(disposer); Chris@16: if(!src.empty()){ Chris@16: detail::exception_disposer Chris@16: rollback(*this, disposer); Chris@16: node_algorithms::clone Chris@101: (src.header_ptr() Chris@101: ,this->header_ptr() Chris@101: ,detail::node_cloner (cloner, &this->get_value_traits()) Chris@101: ,detail::node_disposer(disposer, &this->get_value_traits())); Chris@101: this->sz_traits().set_size(src.sz_traits().get_size()); Chris@101: this->comp() = src.comp(); Chris@101: rollback.release(); Chris@101: } Chris@101: } Chris@101: Chris@101: //! Requires: Disposer::operator()(pointer) shouldn't throw. Chris@101: //! Cloner should yield to nodes equivalent to the original nodes. Chris@101: //! Chris@101: //! Effects: Erases all the elements from *this Chris@101: //! calling Disposer::operator()(pointer), clones all the Chris@101: //! elements from src calling Cloner::operator()(const_reference ) Chris@101: //! and inserts them on *this. Copies the predicate from the source container. Chris@101: //! Chris@101: //! If cloner throws, all cloned elements are unlinked and disposed Chris@101: //! calling Disposer::operator()(pointer). Chris@101: //! Chris@101: //! Complexity: Linear to erased plus inserted elements. Chris@101: //! Chris@101: //! Throws: If cloner throws or predicate copy assignment throws. Basic guarantee. Chris@101: //! Chris@101: //! Note: This version can modify the source container, useful to implement Chris@101: //! move semantics. Chris@101: template Chris@101: void clone_from(bstree_impl &src, Cloner cloner, Disposer disposer) Chris@101: { Chris@101: this->clear_and_dispose(disposer); Chris@101: if(!src.empty()){ Chris@101: detail::exception_disposer Chris@101: rollback(*this, disposer); Chris@101: node_algorithms::clone Chris@101: (src.header_ptr() Chris@101: ,this->header_ptr() Chris@101: ,detail::node_cloner (cloner, &this->get_value_traits()) Chris@101: ,detail::node_disposer(disposer, &this->get_value_traits())); Chris@16: this->sz_traits().set_size(src.sz_traits().get_size()); Chris@16: this->comp() = src.comp(); Chris@16: rollback.release(); Chris@16: } Chris@16: } Chris@16: Chris@16: //! Requires: value must be an lvalue Chris@16: //! Chris@16: //! Effects: Inserts value into the container before the upper bound. Chris@16: //! Chris@16: //! Complexity: Average complexity for insert element is at Chris@16: //! most logarithmic. Chris@16: //! Chris@16: //! Throws: If the internal value_compare ordering function throws. Strong guarantee. Chris@16: //! Chris@16: //! Note: Does not affect the validity of iterators and references. Chris@16: //! No copy-constructors are called. Chris@16: iterator insert_equal(reference value) Chris@16: { Chris@101: detail::key_nodeptr_comp Chris@101: key_node_comp(this->comp(), &this->get_value_traits()); Chris@101: node_ptr to_insert(this->get_value_traits().to_node_ptr(value)); Chris@16: if(safemode_or_autounlink) Chris@16: BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert)); Chris@16: iterator ret(node_algorithms::insert_equal_upper_bound Chris@101: (this->header_ptr(), to_insert, key_node_comp), this->priv_value_traits_ptr()); Chris@16: this->sz_traits().increment(); Chris@16: return ret; Chris@16: } Chris@16: Chris@16: //! Requires: value must be an lvalue, and "hint" must be Chris@16: //! a valid iterator. Chris@16: //! Chris@16: //! Effects: Inserts x into the container, using "hint" as a hint to Chris@16: //! where it will be inserted. If "hint" is the upper_bound Chris@16: //! the insertion takes constant time (two comparisons in the worst case) Chris@16: //! Chris@16: //! Complexity: Logarithmic in general, but it is amortized Chris@16: //! constant time if t is inserted immediately before hint. Chris@16: //! Chris@16: //! Throws: If the internal value_compare ordering function throws. Strong guarantee. Chris@16: //! Chris@16: //! Note: Does not affect the validity of iterators and references. Chris@16: //! No copy-constructors are called. Chris@16: iterator insert_equal(const_iterator hint, reference value) Chris@16: { Chris@101: detail::key_nodeptr_comp Chris@101: key_node_comp(this->comp(), &this->get_value_traits()); Chris@101: node_ptr to_insert(this->get_value_traits().to_node_ptr(value)); Chris@16: if(safemode_or_autounlink) Chris@16: BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert)); Chris@16: iterator ret(node_algorithms::insert_equal Chris@101: (this->header_ptr(), hint.pointed_node(), to_insert, key_node_comp), this->priv_value_traits_ptr()); Chris@16: this->sz_traits().increment(); Chris@16: return ret; Chris@16: } Chris@16: Chris@16: //! Requires: Dereferencing iterator must yield an lvalue Chris@16: //! of type value_type. Chris@16: //! Chris@16: //! Effects: Inserts a each element of a range into the container Chris@16: //! before the upper bound of the key of each element. Chris@16: //! Chris@16: //! Complexity: Insert range is in general O(N * log(N)), where N is the Chris@16: //! size of the range. However, it is linear in N if the range is already sorted Chris@16: //! by value_comp(). Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: Does not affect the validity of iterators and references. Chris@16: //! No copy-constructors are called. Chris@16: template Chris@16: void insert_equal(Iterator b, Iterator e) Chris@16: { Chris@16: iterator iend(this->end()); Chris@16: for (; b != e; ++b) Chris@16: this->insert_equal(iend, *b); Chris@16: } Chris@16: Chris@16: //! Requires: value must be an lvalue Chris@16: //! Chris@16: //! Effects: Inserts value into the container if the value Chris@16: //! is not already present. Chris@16: //! Chris@16: //! Complexity: Average complexity for insert element is at Chris@16: //! most logarithmic. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: Does not affect the validity of iterators and references. Chris@16: //! No copy-constructors are called. Chris@16: std::pair insert_unique(reference value) Chris@16: { Chris@16: insert_commit_data commit_data; Chris@16: std::pair ret = this->insert_unique_check(value, this->comp(), commit_data); Chris@16: if(!ret.second) Chris@16: return ret; Chris@16: return std::pair (this->insert_unique_commit(value, commit_data), true); Chris@16: } Chris@16: Chris@16: //! Requires: value must be an lvalue, and "hint" must be Chris@16: //! a valid iterator Chris@16: //! Chris@16: //! Effects: Tries to insert x into the container, using "hint" as a hint Chris@16: //! to where it will be inserted. Chris@16: //! Chris@16: //! Complexity: Logarithmic in general, but it is amortized Chris@16: //! constant time (two comparisons in the worst case) Chris@16: //! if t is inserted immediately before hint. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: Does not affect the validity of iterators and references. Chris@16: //! No copy-constructors are called. Chris@16: iterator insert_unique(const_iterator hint, reference value) Chris@16: { Chris@16: insert_commit_data commit_data; Chris@16: std::pair ret = this->insert_unique_check(hint, value, this->comp(), commit_data); Chris@16: if(!ret.second) Chris@16: return ret.first; Chris@16: return this->insert_unique_commit(value, commit_data); Chris@16: } Chris@16: Chris@16: //! Requires: Dereferencing iterator must yield an lvalue Chris@16: //! of type value_type. Chris@16: //! Chris@16: //! Effects: Tries to insert each element of a range into the container. Chris@16: //! Chris@16: //! Complexity: Insert range is in general O(N * log(N)), where N is the Chris@16: //! size of the range. However, it is linear in N if the range is already sorted Chris@16: //! by value_comp(). Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: Does not affect the validity of iterators and references. Chris@16: //! No copy-constructors are called. Chris@16: template Chris@16: void insert_unique(Iterator b, Iterator e) Chris@16: { Chris@16: if(this->empty()){ Chris@16: iterator iend(this->end()); Chris@16: for (; b != e; ++b) Chris@16: this->insert_unique(iend, *b); Chris@16: } Chris@16: else{ Chris@16: for (; b != e; ++b) Chris@16: this->insert_unique(*b); Chris@16: } Chris@16: } Chris@16: Chris@16: #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@16: Chris@16: //! Requires: key_value_comp must be a comparison function that induces Chris@16: //! the same strict weak ordering as value_compare. The difference is that Chris@16: //! key_value_comp compares an arbitrary key with the contained values. Chris@16: //! Chris@16: //! Effects: Checks if a value can be inserted in the container, using Chris@16: //! a user provided key instead of the value itself. Chris@16: //! Chris@16: //! Returns: If there is an equivalent value Chris@16: //! returns a pair containing an iterator to the already present value Chris@16: //! and false. If the value can be inserted returns true in the returned Chris@16: //! pair boolean and fills "commit_data" that is meant to be used with Chris@16: //! the "insert_commit" function. Chris@16: //! Chris@16: //! Complexity: Average complexity is at most logarithmic. Chris@16: //! Chris@16: //! Throws: If the key_value_comp ordering function throws. Strong guarantee. Chris@16: //! Chris@16: //! Notes: This function is used to improve performance when constructing Chris@16: //! a value_type is expensive: if there is an equivalent value Chris@16: //! the constructed object must be discarded. Many times, the part of the Chris@16: //! node that is used to impose the order is much cheaper to construct Chris@16: //! than the value_type and this function offers the possibility to use that Chris@16: //! part to check if the insertion will be successful. Chris@16: //! Chris@16: //! If the check is successful, the user can construct the value_type and use Chris@16: //! "insert_commit" to insert the object in constant-time. This gives a total Chris@16: //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)). Chris@16: //! Chris@16: //! "commit_data" remains valid for a subsequent "insert_commit" only if no more Chris@16: //! objects are inserted or erased from the container. Chris@16: template Chris@16: std::pair insert_unique_check Chris@16: (const KeyType &key, KeyValueCompare key_value_comp, insert_commit_data &commit_data); Chris@16: Chris@16: //! Requires: key_value_comp must be a comparison function that induces Chris@16: //! the same strict weak ordering as value_compare. The difference is that Chris@16: //! key_value_comp compares an arbitrary key with the contained values. Chris@16: //! Chris@16: //! Effects: Checks if a value can be inserted in the container, using Chris@16: //! a user provided key instead of the value itself, using "hint" Chris@16: //! as a hint to where it will be inserted. Chris@16: //! Chris@16: //! Returns: If there is an equivalent value Chris@16: //! returns a pair containing an iterator to the already present value Chris@16: //! and false. If the value can be inserted returns true in the returned Chris@16: //! pair boolean and fills "commit_data" that is meant to be used with Chris@16: //! the "insert_commit" function. Chris@16: //! Chris@16: //! Complexity: Logarithmic in general, but it's amortized Chris@16: //! constant time if t is inserted immediately before hint. Chris@16: //! Chris@16: //! Throws: If the key_value_comp ordering function throws. Strong guarantee. Chris@16: //! Chris@16: //! Notes: This function is used to improve performance when constructing Chris@16: //! a value_type is expensive: if there is an equivalent value Chris@16: //! the constructed object must be discarded. Many times, the part of the Chris@16: //! constructing that is used to impose the order is much cheaper to construct Chris@16: //! than the value_type and this function offers the possibility to use that key Chris@16: //! to check if the insertion will be successful. Chris@16: //! Chris@16: //! If the check is successful, the user can construct the value_type and use Chris@16: //! "insert_commit" to insert the object in constant-time. This can give a total Chris@16: //! constant-time complexity to the insertion: check(O(1)) + commit(O(1)). Chris@16: //! Chris@16: //! "commit_data" remains valid for a subsequent "insert_commit" only if no more Chris@16: //! objects are inserted or erased from the container. Chris@16: template Chris@16: std::pair insert_unique_check Chris@16: (const_iterator hint, const KeyType &key Chris@16: ,KeyValueCompare key_value_comp, insert_commit_data &commit_data); Chris@16: Chris@16: #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@16: Chris@16: //! Requires: value must be an lvalue of type value_type. commit_data Chris@16: //! must have been obtained from a previous call to "insert_check". Chris@16: //! No objects should have been inserted or erased from the container between Chris@16: //! the "insert_check" that filled "commit_data" and the call to "insert_commit". Chris@16: //! Chris@16: //! Effects: Inserts the value in the container using the information obtained Chris@16: //! from the "commit_data" that a previous "insert_check" filled. Chris@16: //! Chris@16: //! Returns: An iterator to the newly inserted object. Chris@16: //! Chris@16: //! Complexity: Constant time. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Notes: This function has only sense if a "insert_check" has been Chris@16: //! previously executed to fill "commit_data". No value should be inserted or Chris@16: //! erased between the "insert_check" and "insert_commit" calls. Chris@16: iterator insert_unique_commit(reference value, const insert_commit_data &commit_data) Chris@16: { Chris@101: node_ptr to_insert(this->get_value_traits().to_node_ptr(value)); Chris@16: if(safemode_or_autounlink) Chris@16: BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert)); Chris@16: node_algorithms::insert_unique_commit Chris@16: (this->header_ptr(), to_insert, commit_data); Chris@16: this->sz_traits().increment(); Chris@101: return iterator(to_insert, this->priv_value_traits_ptr()); Chris@16: } Chris@16: Chris@16: //! Requires: value must be an lvalue, "pos" must be Chris@16: //! a valid iterator (or end) and must be the succesor of value Chris@16: //! once inserted according to the predicate Chris@16: //! Chris@16: //! Effects: Inserts x into the container before "pos". Chris@16: //! Chris@16: //! Complexity: Constant time. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: This function does not check preconditions so if "pos" is not Chris@16: //! the successor of "value" container ordering invariant will be broken. Chris@16: //! This is a low-level function to be used only for performance reasons Chris@16: //! by advanced users. Chris@16: iterator insert_before(const_iterator pos, reference value) Chris@16: { Chris@101: node_ptr to_insert(this->get_value_traits().to_node_ptr(value)); Chris@16: if(safemode_or_autounlink) Chris@16: BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert)); Chris@16: this->sz_traits().increment(); Chris@16: return iterator(node_algorithms::insert_before Chris@101: (this->header_ptr(), pos.pointed_node(), to_insert), this->priv_value_traits_ptr()); Chris@16: } Chris@16: Chris@16: //! Requires: value must be an lvalue, and it must be no less Chris@16: //! than the greatest inserted key Chris@16: //! Chris@16: //! Effects: Inserts x into the container in the last position. Chris@16: //! Chris@16: //! Complexity: Constant time. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: This function does not check preconditions so if value is Chris@16: //! less than the greatest inserted key container ordering invariant will be broken. Chris@16: //! This function is slightly more efficient than using "insert_before". Chris@16: //! This is a low-level function to be used only for performance reasons Chris@16: //! by advanced users. Chris@16: void push_back(reference value) Chris@16: { Chris@101: node_ptr to_insert(this->get_value_traits().to_node_ptr(value)); Chris@16: if(safemode_or_autounlink) Chris@16: BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert)); Chris@16: this->sz_traits().increment(); Chris@16: node_algorithms::push_back(this->header_ptr(), to_insert); Chris@16: } Chris@16: Chris@16: //! Requires: value must be an lvalue, and it must be no greater Chris@16: //! than the minimum inserted key Chris@16: //! Chris@16: //! Effects: Inserts x into the container in the first position. Chris@16: //! Chris@16: //! Complexity: Constant time. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: This function does not check preconditions so if value is Chris@16: //! greater than the minimum inserted key container ordering invariant will be broken. Chris@16: //! This function is slightly more efficient than using "insert_before". Chris@16: //! This is a low-level function to be used only for performance reasons Chris@16: //! by advanced users. Chris@16: void push_front(reference value) Chris@16: { Chris@101: node_ptr to_insert(this->get_value_traits().to_node_ptr(value)); Chris@16: if(safemode_or_autounlink) Chris@16: BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert)); Chris@16: this->sz_traits().increment(); Chris@16: node_algorithms::push_front(this->header_ptr(), to_insert); Chris@16: } Chris@16: Chris@101: //! Effects: Erases the element pointed to by i. Chris@16: //! Chris@16: //! Complexity: Average complexity for erase element is constant time. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: Invalidates the iterators (but not the references) Chris@16: //! to the erased elements. No destructors are called. Chris@16: iterator erase(const_iterator i) Chris@16: { Chris@16: const_iterator ret(i); Chris@16: ++ret; Chris@16: node_ptr to_erase(i.pointed_node()); Chris@16: if(safemode_or_autounlink) Chris@16: BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!node_algorithms::unique(to_erase)); Chris@16: node_algorithms::erase(this->header_ptr(), to_erase); Chris@16: this->sz_traits().decrement(); Chris@16: if(safemode_or_autounlink) Chris@16: node_algorithms::init(to_erase); Chris@16: return ret.unconst(); Chris@16: } Chris@16: Chris@16: //! Effects: Erases the range pointed to by b end e. Chris@16: //! Chris@16: //! Complexity: Average complexity for erase range is at most Chris@16: //! O(log(size() + N)), where N is the number of elements in the range. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: Invalidates the iterators (but not the references) Chris@16: //! to the erased elements. No destructors are called. Chris@16: iterator erase(const_iterator b, const_iterator e) Chris@16: { size_type n; return this->private_erase(b, e, n); } Chris@16: Chris@16: //! Effects: Erases all the elements with the given value. Chris@16: //! Chris@16: //! Returns: The number of erased elements. Chris@16: //! Chris@16: //! Complexity: O(log(size() + N). Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: Invalidates the iterators (but not the references) Chris@16: //! to the erased elements. No destructors are called. Chris@16: size_type erase(const_reference value) Chris@16: { return this->erase(value, this->comp()); } Chris@16: Chris@16: //! Effects: Erases all the elements with the given key. Chris@16: //! according to the comparison functor "comp". Chris@16: //! Chris@16: //! Returns: The number of erased elements. Chris@16: //! Chris@16: //! Complexity: O(log(size() + N). Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: Invalidates the iterators (but not the references) Chris@16: //! to the erased elements. No destructors are called. Chris@16: template Chris@16: size_type erase(const KeyType& key, KeyValueCompare comp Chris@16: /// @cond Chris@16: , typename detail::enable_if_c::value >::type * = 0 Chris@16: /// @endcond Chris@16: ) Chris@16: { Chris@16: std::pair p = this->equal_range(key, comp); Chris@16: size_type n; Chris@16: this->private_erase(p.first, p.second, n); Chris@16: return n; Chris@16: } Chris@16: Chris@16: //! Requires: Disposer::operator()(pointer) shouldn't throw. Chris@16: //! Chris@101: //! Effects: Erases the element pointed to by i. Chris@16: //! Disposer::operator()(pointer) is called for the removed element. Chris@16: //! Chris@16: //! Complexity: Average complexity for erase element is constant time. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: Invalidates the iterators Chris@16: //! to the erased elements. Chris@16: template Chris@16: iterator erase_and_dispose(const_iterator i, Disposer disposer) Chris@16: { Chris@16: node_ptr to_erase(i.pointed_node()); Chris@16: iterator ret(this->erase(i)); Chris@101: disposer(this->get_value_traits().to_value_ptr(to_erase)); Chris@16: return ret; Chris@16: } Chris@16: Chris@16: #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: template Chris@16: iterator erase_and_dispose(iterator i, Disposer disposer) Chris@16: { return this->erase_and_dispose(const_iterator(i), disposer); } Chris@16: #endif Chris@16: Chris@16: //! Requires: Disposer::operator()(pointer) shouldn't throw. Chris@16: //! Chris@16: //! Effects: Erases all the elements with the given value. Chris@16: //! Disposer::operator()(pointer) is called for the removed elements. Chris@16: //! Chris@16: //! Returns: The number of erased elements. Chris@16: //! Chris@16: //! Complexity: O(log(size() + N). Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: Invalidates the iterators (but not the references) Chris@16: //! to the erased elements. No destructors are called. Chris@16: template Chris@16: size_type erase_and_dispose(const_reference value, Disposer disposer) Chris@16: { Chris@16: std::pair p = this->equal_range(value); Chris@16: size_type n; Chris@16: this->private_erase(p.first, p.second, n, disposer); Chris@16: return n; Chris@16: } Chris@16: Chris@16: //! Requires: Disposer::operator()(pointer) shouldn't throw. Chris@16: //! Chris@16: //! Effects: Erases the range pointed to by b end e. Chris@16: //! Disposer::operator()(pointer) is called for the removed elements. Chris@16: //! Chris@16: //! Complexity: Average complexity for erase range is at most Chris@16: //! O(log(size() + N)), where N is the number of elements in the range. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: Invalidates the iterators Chris@16: //! to the erased elements. Chris@16: template Chris@16: iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer) Chris@16: { size_type n; return this->private_erase(b, e, n, disposer); } Chris@16: Chris@16: //! Requires: Disposer::operator()(pointer) shouldn't throw. Chris@16: //! Chris@16: //! Effects: Erases all the elements with the given key. Chris@16: //! according to the comparison functor "comp". Chris@16: //! Disposer::operator()(pointer) is called for the removed elements. Chris@16: //! Chris@16: //! Returns: The number of erased elements. Chris@16: //! Chris@16: //! Complexity: O(log(size() + N). Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: Invalidates the iterators Chris@16: //! to the erased elements. Chris@16: template Chris@16: size_type erase_and_dispose(const KeyType& key, KeyValueCompare comp, Disposer disposer Chris@16: /// @cond Chris@16: , typename detail::enable_if_c::value >::type * = 0 Chris@16: /// @endcond Chris@16: ) Chris@16: { Chris@16: std::pair p = this->equal_range(key, comp); Chris@16: size_type n; Chris@16: this->private_erase(p.first, p.second, n, disposer); Chris@16: return n; Chris@16: } Chris@16: Chris@16: //! Effects: Erases all of the elements. Chris@16: //! Chris@16: //! Complexity: Linear to the number of elements on the container. Chris@16: //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: Invalidates the iterators (but not the references) Chris@16: //! to the erased elements. No destructors are called. Chris@16: void clear() Chris@16: { Chris@16: if(safemode_or_autounlink){ Chris@16: this->clear_and_dispose(detail::null_disposer()); Chris@16: } Chris@16: else{ Chris@16: node_algorithms::init_header(this->header_ptr()); Chris@16: this->sz_traits().set_size(0); Chris@16: } Chris@16: } Chris@16: Chris@16: //! Effects: Erases all of the elements calling disposer(p) for Chris@16: //! each node to be erased. Chris@16: //! Complexity: Average complexity for is at most O(log(size() + N)), Chris@16: //! where N is the number of elements in the container. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: Invalidates the iterators (but not the references) Chris@16: //! to the erased elements. Calls N times to disposer functor. Chris@16: template Chris@16: void clear_and_dispose(Disposer disposer) Chris@16: { Chris@16: node_algorithms::clear_and_dispose(this->header_ptr() Chris@101: , detail::node_disposer(disposer, &this->get_value_traits())); Chris@16: node_algorithms::init_header(this->header_ptr()); Chris@16: this->sz_traits().set_size(0); Chris@16: } Chris@16: Chris@16: //! Effects: Returns the number of contained elements with the given value Chris@16: //! Chris@16: //! Complexity: Logarithmic to the number of elements contained plus lineal Chris@16: //! to number of objects with the given value. Chris@16: //! Chris@101: //! Throws: If `value_compare` throws. Chris@101: size_type count(const_reference value) const Chris@101: { return size_type(this->count(value, this->comp())); } Chris@16: Chris@16: //! Effects: Returns the number of contained elements with the given key Chris@16: //! Chris@16: //! Complexity: Logarithmic to the number of elements contained plus lineal Chris@16: //! to number of objects with the given key. Chris@16: //! Chris@101: //! Throws: If `comp` throws. Chris@16: template Chris@101: size_type count(const KeyType &key, KeyValueCompare comp) const Chris@101: { Chris@101: std::pair ret = this->equal_range(key, comp); Chris@101: size_type n = 0; Chris@101: for(; ret.first != ret.second; ++ret.first){ ++n; } Chris@101: return n; Chris@101: } Chris@101: Chris@101: #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@101: Chris@101: //Add non-const overloads to theoretically const members Chris@101: //as some algorithms have different behavior when non-const versions are used (like splay trees). Chris@101: size_type count(const_reference value) Chris@101: { return size_type(this->count(value, this->comp())); } Chris@101: Chris@101: template Chris@101: size_type count(const KeyType &key, KeyValueCompare comp) Chris@101: { Chris@101: std::pair ret = this->equal_range(key, comp); Chris@101: size_type n = 0; Chris@101: for(; ret.first != ret.second; ++ret.first){ ++n; } Chris@101: return n; Chris@101: } Chris@101: Chris@101: #else //defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: Chris@16: //! Effects: Returns an iterator to the first element whose Chris@16: //! key is not less than k or end() if that element does not exist. Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@101: //! Throws: If `value_compare` throws. Chris@16: iterator lower_bound(const_reference value); Chris@16: Chris@16: //! Effects: Returns an iterator to the first element whose Chris@16: //! key is not less than k or end() if that element does not exist. Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@101: //! Throws: If `value_compare` throws. Chris@16: const_iterator lower_bound(const_reference value) const; Chris@16: Chris@16: //! Effects: Returns an iterator to the first element whose Chris@16: //! key is not less than k or end() if that element does not exist. Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@101: //! Throws: If `comp` throws. Chris@16: template Chris@16: iterator lower_bound(const KeyType &key, KeyValueCompare comp); Chris@101: Chris@16: //! Effects: Returns a const iterator to the first element whose Chris@16: //! key is not less than k or end() if that element does not exist. Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@101: //! Throws: If `comp` throws. Chris@16: template Chris@16: const_iterator lower_bound(const KeyType &key, KeyValueCompare comp) const; Chris@16: Chris@16: //! Effects: Returns an iterator to the first element whose Chris@16: //! key is greater than k or end() if that element does not exist. Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@101: //! Throws: If `value_compare` throws. Chris@16: iterator upper_bound(const_reference value); Chris@16: Chris@16: //! Effects: Returns an iterator to the first element whose Chris@16: //! key is greater than k according to comp or end() if that element Chris@16: //! does not exist. Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@101: //! Throws: If `comp` throws. Chris@16: template Chris@16: iterator upper_bound(const KeyType &key, KeyValueCompare comp); Chris@16: Chris@16: //! Effects: Returns an iterator to the first element whose Chris@16: //! key is greater than k or end() if that element does not exist. Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@101: //! Throws: If `value_compare` throws. Chris@16: const_iterator upper_bound(const_reference value) const; Chris@16: Chris@16: //! Effects: Returns an iterator to the first element whose Chris@16: //! key is greater than k according to comp or end() if that element Chris@16: //! does not exist. Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@101: //! Throws: If `comp` throws. Chris@16: template Chris@16: const_iterator upper_bound(const KeyType &key, KeyValueCompare comp) const; Chris@16: Chris@16: //! Effects: Finds an iterator to the first element whose key is Chris@16: //! k or end() if that element does not exist. Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@101: //! Throws: If `value_compare` throws. Chris@16: iterator find(const_reference value); Chris@16: Chris@16: //! Effects: Finds an iterator to the first element whose key is Chris@16: //! k or end() if that element does not exist. Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@101: //! Throws: If `comp` throws. Chris@16: template Chris@16: iterator find(const KeyType &key, KeyValueCompare comp); Chris@16: Chris@16: //! Effects: Finds a const_iterator to the first element whose key is Chris@16: //! k or end() if that element does not exist. Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@101: //! Throws: If `value_compare` throws. Chris@16: const_iterator find(const_reference value) const; Chris@16: Chris@16: //! Effects: Finds a const_iterator to the first element whose key is Chris@16: //! k or end() if that element does not exist. Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@101: //! Throws: If `comp` throws. Chris@16: template Chris@16: const_iterator find(const KeyType &key, KeyValueCompare comp) const; Chris@16: Chris@16: //! Effects: Finds a range containing all elements whose key is k or Chris@16: //! an empty range that indicates the position where those elements would be Chris@16: //! if they there is no elements with key k. Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@101: //! Throws: If `value_compare` throws. Chris@16: std::pair equal_range(const_reference value); Chris@16: Chris@16: //! Effects: Finds a range containing all elements whose key is k or Chris@16: //! an empty range that indicates the position where those elements would be Chris@16: //! if they there is no elements with key k. Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@101: //! Throws: If `comp` throws. Chris@16: template Chris@16: std::pair equal_range(const KeyType &key, KeyValueCompare comp); Chris@16: Chris@16: //! Effects: Finds a range containing all elements whose key is k or Chris@16: //! an empty range that indicates the position where those elements would be Chris@16: //! if they there is no elements with key k. Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@101: //! Throws: If `value_compare` throws. Chris@16: std::pair Chris@16: equal_range(const_reference value) const; Chris@16: Chris@16: //! Effects: Finds a range containing all elements whose key is k or Chris@16: //! an empty range that indicates the position where those elements would be Chris@16: //! if they there is no elements with key k. Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@101: //! Throws: If `comp` throws. Chris@16: template Chris@16: std::pair Chris@16: equal_range(const KeyType &key, KeyValueCompare comp) const; Chris@16: Chris@16: //! Requires: 'lower_value' must not be greater than 'upper_value'. If Chris@16: //! 'lower_value' == 'upper_value', ('left_closed' || 'right_closed') must be false. Chris@16: //! Chris@16: //! Effects: Returns an a pair with the following criteria: Chris@16: //! Chris@16: //! first = lower_bound(lower_key) if left_closed, upper_bound(lower_key) otherwise Chris@16: //! Chris@16: //! second = upper_bound(upper_key) if right_closed, lower_bound(upper_key) otherwise Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@101: //! Throws: If `value_compare` throws. Chris@16: //! Chris@16: //! Note: This function can be more efficient than calling upper_bound Chris@16: //! and lower_bound for lower_value and upper_value. Chris@16: //! Chris@16: //! Note: Experimental function, the interface might change in future releases. Chris@16: std::pair bounded_range Chris@16: (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed); Chris@16: Chris@16: //! Requires: KeyValueCompare is a function object that induces a strict weak Chris@16: //! ordering compatible with the strict weak ordering used to create the Chris@101: //! the container. Chris@16: //! 'lower_key' must not be greater than 'upper_key' according to 'comp'. If Chris@16: //! 'lower_key' == 'upper_key', ('left_closed' || 'right_closed') must be false. Chris@16: //! Chris@16: //! Effects: Returns an a pair with the following criteria: Chris@16: //! Chris@16: //! first = lower_bound(lower_key, comp) if left_closed, upper_bound(lower_key, comp) otherwise Chris@16: //! Chris@16: //! second = upper_bound(upper_key, comp) if right_closed, lower_bound(upper_key, comp) otherwise Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@101: //! Throws: If `comp` throws. Chris@16: //! Chris@16: //! Note: This function can be more efficient than calling upper_bound Chris@16: //! and lower_bound for lower_key and upper_key. Chris@16: //! Chris@16: //! Note: Experimental function, the interface might change in future releases. Chris@16: template Chris@16: std::pair bounded_range Chris@16: (const KeyType &lower_key, const KeyType &upper_key, KeyValueCompare comp, bool left_closed, bool right_closed); Chris@16: Chris@16: //! Requires: 'lower_value' must not be greater than 'upper_value'. If Chris@16: //! 'lower_value' == 'upper_value', ('left_closed' || 'right_closed') must be false. Chris@16: //! Chris@16: //! Effects: Returns an a pair with the following criteria: Chris@16: //! Chris@16: //! first = lower_bound(lower_key) if left_closed, upper_bound(lower_key) otherwise Chris@16: //! Chris@16: //! second = upper_bound(upper_key) if right_closed, lower_bound(upper_key) otherwise Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@101: //! Throws: If `value_compare` throws. Chris@16: //! Chris@16: //! Note: This function can be more efficient than calling upper_bound Chris@16: //! and lower_bound for lower_value and upper_value. Chris@16: //! Chris@16: //! Note: Experimental function, the interface might change in future releases. Chris@16: std::pair bounded_range Chris@16: (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed) const; Chris@16: Chris@16: //! Requires: KeyValueCompare is a function object that induces a strict weak Chris@16: //! ordering compatible with the strict weak ordering used to create the Chris@101: //! the container. Chris@16: //! 'lower_key' must not be greater than 'upper_key' according to 'comp'. If Chris@16: //! 'lower_key' == 'upper_key', ('left_closed' || 'right_closed') must be false. Chris@16: //! Chris@16: //! Effects: Returns an a pair with the following criteria: Chris@16: //! Chris@16: //! first = lower_bound(lower_key, comp) if left_closed, upper_bound(lower_key, comp) otherwise Chris@16: //! Chris@16: //! second = upper_bound(upper_key, comp) if right_closed, lower_bound(upper_key, comp) otherwise Chris@16: //! Chris@16: //! Complexity: Logarithmic. Chris@16: //! Chris@101: //! Throws: If `comp` throws. Chris@16: //! Chris@16: //! Note: This function can be more efficient than calling upper_bound Chris@16: //! and lower_bound for lower_key and upper_key. Chris@16: //! Chris@16: //! Note: Experimental function, the interface might change in future releases. Chris@16: template Chris@16: std::pair bounded_range Chris@16: (const KeyType &lower_key, const KeyType &upper_key, KeyValueCompare comp, bool left_closed, bool right_closed) const; Chris@16: Chris@16: //! Requires: value must be an lvalue and shall be in a set of Chris@16: //! appropriate type. Otherwise the behavior is undefined. Chris@16: //! Chris@16: //! Effects: Returns: a valid iterator i belonging to the set Chris@16: //! that points to the value Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: This static function is available only if the value traits Chris@16: //! is stateless. Chris@16: static iterator s_iterator_to(reference value); Chris@16: Chris@16: //! Requires: value must be an lvalue and shall be in a set of Chris@16: //! appropriate type. Otherwise the behavior is undefined. Chris@16: //! Chris@16: //! Effects: Returns: a valid const_iterator i belonging to the Chris@16: //! set that points to the value Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: This static function is available only if the value traits Chris@16: //! is stateless. Chris@16: static const_iterator s_iterator_to(const_reference value); Chris@16: Chris@16: //! Requires: value must be an lvalue and shall be in a set of Chris@16: //! appropriate type. Otherwise the behavior is undefined. Chris@16: //! Chris@16: //! Effects: Returns: a valid iterator i belonging to the set Chris@16: //! that points to the value Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: iterator iterator_to(reference value); Chris@16: Chris@16: //! Requires: value must be an lvalue and shall be in a set of Chris@16: //! appropriate type. Otherwise the behavior is undefined. Chris@16: //! Chris@16: //! Effects: Returns: a valid const_iterator i belonging to the Chris@16: //! set that points to the value Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: const_iterator iterator_to(const_reference value) const; Chris@16: Chris@16: //! Requires: value shall not be in a container. Chris@16: //! Chris@16: //! Effects: init_node puts the hook of a value in a well-known default Chris@16: //! state. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant time. Chris@16: //! Chris@16: //! Note: This function puts the hook in the well-known default state Chris@16: //! used by auto_unlink and safe hooks. Chris@16: static void init_node(reference value); Chris@16: Chris@16: #endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: Chris@16: //! Effects: Unlinks the leftmost node from the container. Chris@16: //! Chris@16: //! Complexity: Average complexity is constant time. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Notes: This function breaks the container and the container can Chris@16: //! only be used for more unlink_leftmost_without_rebalance calls. Chris@16: //! This function is normally used to achieve a step by step Chris@16: //! controlled destruction of the container. Chris@16: pointer unlink_leftmost_without_rebalance() Chris@16: { Chris@16: node_ptr to_be_disposed(node_algorithms::unlink_leftmost_without_rebalance Chris@16: (this->header_ptr())); Chris@16: if(!to_be_disposed) Chris@16: return 0; Chris@16: this->sz_traits().decrement(); Chris@16: if(safemode_or_autounlink)//If this is commented does not work with normal_link Chris@16: node_algorithms::init(to_be_disposed); Chris@101: return this->get_value_traits().to_value_ptr(to_be_disposed); Chris@16: } Chris@16: Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: Chris@16: //! Requires: replace_this must be a valid iterator of *this Chris@16: //! and with_this must not be inserted in any container. Chris@16: //! Chris@16: //! Effects: Replaces replace_this in its position in the Chris@16: //! container with with_this. The container does not need to be rebalanced. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Note: This function will break container ordering invariants if Chris@16: //! with_this is not equivalent to *replace_this according to the Chris@16: //! ordering rules. This function is faster than erasing and inserting Chris@16: //! the node, since no rebalancing or comparison is needed. Chris@16: void replace_node(iterator replace_this, reference with_this); Chris@16: Chris@16: //! Effects: Rebalances the tree. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Linear. Chris@16: void rebalance(); Chris@16: Chris@16: //! Requires: old_root is a node of a tree. Chris@16: //! Chris@16: //! Effects: Rebalances the subtree rooted at old_root. Chris@16: //! Chris@16: //! Returns: The new root of the subtree. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Linear to the elements in the subtree. Chris@16: iterator rebalance_subtree(iterator root); Chris@16: Chris@16: #endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: Chris@16: //! Effects: removes "value" from the container. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Logarithmic time. Chris@16: //! Chris@16: //! Note: This static function is only usable with non-constant Chris@16: //! time size containers that have stateless comparison functors. Chris@16: //! Chris@16: //! If the user calls Chris@16: //! this function with a constant time size container or stateful comparison Chris@16: //! functor a compilation error will be issued. Chris@16: static void remove_node(reference value) Chris@16: { Chris@16: BOOST_STATIC_ASSERT((!constant_time_size)); Chris@16: node_ptr to_remove(value_traits::to_node_ptr(value)); Chris@16: node_algorithms::unlink(to_remove); Chris@16: if(safemode_or_autounlink) Chris@16: node_algorithms::init(to_remove); Chris@16: } Chris@16: Chris@101: //! Effects: Asserts the integrity of the container with additional checks provided by the user. Chris@101: //! Chris@101: //! Complexity: Linear time. Chris@101: //! Chris@101: //! Note: The method might not have effect when asserts are turned off (e.g., with NDEBUG). Chris@101: //! Experimental function, interface might change in future versions. Chris@101: template Chris@101: void check(ExtraChecker extra_checker) const Chris@101: { Chris@101: typedef detail::key_nodeptr_comp nodeptr_comp_t; Chris@101: nodeptr_comp_t nodeptr_comp(this->comp(), &this->get_value_traits()); Chris@101: typedef typename get_node_checker::type node_checker_t; Chris@101: typename node_checker_t::return_type checker_return; Chris@101: node_algorithms::check(this->header_ptr(), node_checker_t(nodeptr_comp, extra_checker), checker_return); Chris@101: if (constant_time_size) Chris@101: BOOST_INTRUSIVE_INVARIANT_ASSERT(this->sz_traits().get_size() == checker_return.node_count); Chris@101: } Chris@101: Chris@101: //! Effects: Asserts the integrity of the container. Chris@101: //! Chris@101: //! Complexity: Linear time. Chris@101: //! Chris@101: //! Note: The method has no effect when asserts are turned off (e.g., with NDEBUG). Chris@101: //! Experimental function, interface might change in future versions. Chris@101: void check() const Chris@101: { Chris@101: check(detail::empty_node_checker()); Chris@101: } Chris@101: Chris@16: /// @cond Chris@16: private: Chris@16: template Chris@16: iterator private_erase(const_iterator b, const_iterator e, size_type &n, Disposer disposer) Chris@16: { Chris@16: for(n = 0; b != e; ++n) Chris@16: this->erase_and_dispose(b++, disposer); Chris@16: return b.unconst(); Chris@16: } Chris@16: Chris@16: iterator private_erase(const_iterator b, const_iterator e, size_type &n) Chris@16: { Chris@16: for(n = 0; b != e; ++n) Chris@16: this->erase(b++); Chris@16: return b.unconst(); Chris@16: } Chris@16: /// @endcond Chris@16: }; Chris@16: Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: template Chris@16: #else Chris@101: template Chris@16: #endif Chris@16: inline bool operator< Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: (const bstree_impl &x, const bstree_impl &y) Chris@16: #else Chris@101: ( const bstree_impl &x Chris@101: , const bstree_impl &y) Chris@16: #endif Chris@101: { return ::boost::intrusive::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); } Chris@16: Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: template Chris@16: #else Chris@101: template Chris@16: #endif Chris@16: bool operator== Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: (const bstree_impl &x, const bstree_impl &y) Chris@16: #else Chris@101: ( const bstree_impl &x Chris@101: , const bstree_impl &y) Chris@16: #endif Chris@16: { Chris@101: typedef bstree_impl tree_type; Chris@16: Chris@16: if(tree_type::constant_time_size && x.size() != y.size()){ Chris@16: return false; Chris@16: } Chris@101: return boost::intrusive::algo_equal(x.cbegin(), x.cend(), y.cbegin(), y.cend()); Chris@16: } Chris@16: Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: template Chris@16: #else Chris@101: template Chris@16: #endif Chris@16: inline bool operator!= Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: (const bstree_impl &x, const bstree_impl &y) Chris@16: #else Chris@101: ( const bstree_impl &x Chris@101: , const bstree_impl &y) Chris@16: #endif Chris@16: { return !(x == y); } Chris@16: Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: template Chris@16: #else Chris@101: template Chris@16: #endif Chris@16: inline bool operator> Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: (const bstree_impl &x, const bstree_impl &y) Chris@16: #else Chris@101: ( const bstree_impl &x Chris@101: , const bstree_impl &y) Chris@16: #endif Chris@16: { return y < x; } Chris@16: Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: template Chris@16: #else Chris@101: template Chris@16: #endif Chris@16: inline bool operator<= Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: (const bstree_impl &x, const bstree_impl &y) Chris@16: #else Chris@101: ( const bstree_impl &x Chris@101: , const bstree_impl &y) Chris@16: #endif Chris@16: { return !(y < x); } Chris@16: Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: template Chris@16: #else Chris@101: template Chris@16: #endif Chris@16: inline bool operator>= Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: (const bstree_impl &x, const bstree_impl &y) Chris@16: #else Chris@101: ( const bstree_impl &x Chris@101: , const bstree_impl &y) Chris@16: #endif Chris@16: { return !(x < y); } Chris@16: Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: template Chris@16: #else Chris@101: template Chris@16: #endif Chris@16: inline void swap Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: (bstree_impl &x, bstree_impl &y) Chris@16: #else Chris@101: ( bstree_impl &x Chris@101: , bstree_impl &y) Chris@16: #endif Chris@16: { x.swap(y); } Chris@16: Chris@16: //! Helper metafunction to define a \c bstree that yields to the same type when the Chris@16: //! same options (either explicitly or implicitly) are used. Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) Chris@16: template Chris@16: #else Chris@16: template Chris@16: #endif Chris@16: struct make_bstree Chris@16: { Chris@16: /// @cond Chris@16: typedef typename pack_options Chris@16: < bstree_defaults, Chris@16: #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) Chris@101: O1, O2, O3, O4, O5 Chris@16: #else Chris@16: Options... Chris@16: #endif Chris@16: >::type packed_options; Chris@16: Chris@16: typedef typename detail::get_value_traits Chris@16: ::type value_traits; Chris@16: Chris@16: typedef bstree_impl Chris@16: < value_traits Chris@16: , typename packed_options::compare Chris@16: , typename packed_options::size_type Chris@16: , packed_options::constant_time_size Chris@16: , BsTreeAlgorithms Chris@101: , typename packed_options::header_holder_type Chris@16: > implementation_defined; Chris@16: /// @endcond Chris@16: typedef implementation_defined type; Chris@16: }; Chris@16: Chris@16: Chris@16: #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@16: Chris@16: #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) Chris@101: template Chris@16: #else Chris@16: template Chris@16: #endif Chris@16: class bstree Chris@16: : public make_bstree::type Chris@16: { Chris@16: typedef typename make_bstree Chris@16: ::type Base; Chris@16: BOOST_MOVABLE_BUT_NOT_COPYABLE(bstree) Chris@16: Chris@16: public: Chris@16: typedef typename Base::value_compare value_compare; Chris@16: typedef typename Base::value_traits value_traits; Chris@16: typedef typename Base::iterator iterator; Chris@16: typedef typename Base::const_iterator const_iterator; Chris@16: Chris@16: //Assert if passed value traits are compatible with the type Chris@101: BOOST_STATIC_ASSERT((detail::is_same::value)); Chris@16: Chris@16: bstree( const value_compare &cmp = value_compare() Chris@16: , const value_traits &v_traits = value_traits()) Chris@16: : Base(cmp, v_traits) Chris@16: {} Chris@16: Chris@16: template Chris@16: bstree( bool unique, Iterator b, Iterator e Chris@16: , const value_compare &cmp = value_compare() Chris@16: , const value_traits &v_traits = value_traits()) Chris@16: : Base(unique, b, e, cmp, v_traits) Chris@16: {} Chris@16: Chris@16: bstree(BOOST_RV_REF(bstree) x) Chris@101: : Base(BOOST_MOVE_BASE(Base, x)) Chris@16: {} Chris@16: Chris@16: bstree& operator=(BOOST_RV_REF(bstree) x) Chris@101: { return static_cast(this->Base::operator=(BOOST_MOVE_BASE(Base, x))); } Chris@16: Chris@16: static bstree &container_from_end_iterator(iterator end_iterator) Chris@16: { return static_cast(Base::container_from_end_iterator(end_iterator)); } Chris@16: Chris@16: static const bstree &container_from_end_iterator(const_iterator end_iterator) Chris@16: { return static_cast(Base::container_from_end_iterator(end_iterator)); } Chris@16: Chris@16: static bstree &container_from_iterator(iterator it) Chris@16: { return static_cast(Base::container_from_iterator(it)); } Chris@16: Chris@16: static const bstree &container_from_iterator(const_iterator it) Chris@16: { return static_cast(Base::container_from_iterator(it)); } Chris@16: }; Chris@16: Chris@16: #endif Chris@16: } //namespace intrusive Chris@16: } //namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif //BOOST_INTRUSIVE_BSTREE_HPP