1 /////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2013-2014
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // See http://www.boost.org/libs/intrusive for documentation.
11 /////////////////////////////////////////////////////////////////////////////
12 #ifndef BOOST_INTRUSIVE_BSTREE_HPP
13 #define BOOST_INTRUSIVE_BSTREE_HPP
15 #include <boost/intrusive/detail/config_begin.hpp>
16 #include <boost/intrusive/intrusive_fwd.hpp>
18 #include <boost/intrusive/detail/assert.hpp>
19 #include <boost/static_assert.hpp>
20 #include <boost/intrusive/intrusive_fwd.hpp>
21 #include <boost/intrusive/bs_set_hook.hpp>
22 #include <boost/intrusive/detail/tree_node.hpp>
23 #include <boost/intrusive/detail/tree_iterator.hpp>
24 #include <boost/intrusive/detail/ebo_functor_holder.hpp>
25 #include <boost/intrusive/detail/mpl.hpp>
26 #include <boost/intrusive/pointer_traits.hpp>
27 #include <boost/intrusive/detail/is_stateful_value_traits.hpp>
28 #include <boost/intrusive/detail/empty_node_checker.hpp>
29 #include <boost/intrusive/detail/default_header_holder.hpp>
30 #include <boost/intrusive/detail/reverse_iterator.hpp>
31 #include <boost/intrusive/detail/exception_disposer.hpp>
32 #include <boost/intrusive/detail/node_cloner_disposer.hpp>
33 #include <boost/intrusive/detail/key_nodeptr_comp.hpp>
34 #include <boost/intrusive/detail/simple_disposers.hpp>
35 #include <boost/intrusive/detail/size_holder.hpp>
36 #include <boost/intrusive/detail/algo_type.hpp>
37 #include <boost/intrusive/detail/algorithm.hpp>
38 #include <boost/intrusive/detail/tree_value_compare.hpp>
40 #include <boost/intrusive/detail/get_value_traits.hpp>
41 #include <boost/intrusive/bstree_algorithms.hpp>
42 #include <boost/intrusive/link_mode.hpp>
43 #include <boost/intrusive/parent_from_member.hpp>
44 #include <boost/move/utility_core.hpp>
45 #include <boost/move/adl_move_swap.hpp>
47 #include <boost/intrusive/detail/minimal_pair_header.hpp>
48 #include <cstddef> //size_t...
49 #include <boost/intrusive/detail/minimal_less_equal_header.hpp>//less, equal_to
51 #if defined(BOOST_HAS_PRAGMA_ONCE)
60 struct default_bstree_hook_applier
61 { template <class T> struct apply{ typedef typename T::default_bstree_hook type; }; };
64 struct is_default_hook_tag<default_bstree_hook_applier>
65 { static const bool value = true; };
67 struct bstree_defaults
69 typedef default_bstree_hook_applier proto_value_traits;
70 static const bool constant_time_size = true;
71 typedef std::size_t size_type;
73 typedef void key_of_value;
74 static const bool floating_point = true; //For sgtree
75 typedef void priority; //For treap
76 typedef void header_holder_type;
79 template<class ValueTraits, algo_types AlgoType, typename HeaderHolder>
82 typedef ValueTraits value_traits;
83 typedef typename value_traits::node_traits node_traits;
84 typedef typename node_traits::node node_type;
85 typedef typename get_algo<AlgoType, node_traits>::type node_algorithms;
86 typedef typename node_traits::node_ptr node_ptr;
87 typedef typename node_traits::const_node_ptr const_node_ptr;
88 typedef tree_iterator<value_traits, false> iterator;
89 typedef tree_iterator<value_traits, true> const_iterator;
90 typedef boost::intrusive::reverse_iterator<iterator> reverse_iterator;
91 typedef boost::intrusive::reverse_iterator<const_iterator> const_reverse_iterator;
92 typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::pointer) pointer;
93 typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::const_pointer) const_pointer;
94 typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<pointer>::element_type) value_type;
95 typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<pointer>::reference) reference;
96 typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<const_pointer>::reference) const_reference;
97 typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<const_pointer>::difference_type) difference_type;
98 typedef typename detail::get_header_holder_type
99 < value_traits,HeaderHolder >::type header_holder_type;
101 static const bool safemode_or_autounlink = is_safe_autounlink<value_traits::link_mode>::value;
102 static const bool stateful_value_traits = detail::is_stateful_value_traits<value_traits>::value;
103 static const bool has_container_from_iterator =
104 detail::is_same< header_holder_type, detail::default_header_holder< node_traits > >::value;
106 struct holder_t : public ValueTraits
108 BOOST_INTRUSIVE_FORCEINLINE explicit holder_t(const ValueTraits &vtraits)
109 : ValueTraits(vtraits)
111 header_holder_type root;
114 static bstbase3 &get_tree_base_from_end_iterator(const const_iterator &end_iterator)
116 BOOST_STATIC_ASSERT(has_container_from_iterator);
117 node_ptr p = end_iterator.pointed_node();
118 header_holder_type* h = header_holder_type::get_holder(p);
119 holder_t *holder = get_parent_from_member<holder_t, header_holder_type>(h, &holder_t::root);
120 bstbase3 *base = get_parent_from_member<bstbase3, holder_t> (holder, &bstbase3::holder);
124 BOOST_INTRUSIVE_FORCEINLINE bstbase3(const ValueTraits &vtraits)
127 node_algorithms::init_header(this->header_ptr());
130 BOOST_INTRUSIVE_FORCEINLINE node_ptr header_ptr()
131 { return holder.root.get_node(); }
133 BOOST_INTRUSIVE_FORCEINLINE const_node_ptr header_ptr() const
134 { return holder.root.get_node(); }
136 BOOST_INTRUSIVE_FORCEINLINE const value_traits &get_value_traits() const
137 { return this->holder; }
139 BOOST_INTRUSIVE_FORCEINLINE value_traits &get_value_traits()
140 { return this->holder; }
142 typedef typename boost::intrusive::value_traits_pointers
143 <ValueTraits>::const_value_traits_ptr const_value_traits_ptr;
145 BOOST_INTRUSIVE_FORCEINLINE const_value_traits_ptr priv_value_traits_ptr() const
146 { return pointer_traits<const_value_traits_ptr>::pointer_to(this->get_value_traits()); }
149 { return iterator(node_algorithms::begin_node(this->header_ptr()), this->priv_value_traits_ptr()); }
151 BOOST_INTRUSIVE_FORCEINLINE const_iterator begin() const
154 const_iterator cbegin() const
155 { return const_iterator(node_algorithms::begin_node(this->header_ptr()), this->priv_value_traits_ptr()); }
158 { return iterator(node_algorithms::end_node(this->header_ptr()), this->priv_value_traits_ptr()); }
160 BOOST_INTRUSIVE_FORCEINLINE const_iterator end() const
163 BOOST_INTRUSIVE_FORCEINLINE const_iterator cend() const
164 { return const_iterator(node_algorithms::end_node(this->header_ptr()), this->priv_value_traits_ptr()); }
166 BOOST_INTRUSIVE_FORCEINLINE iterator root()
167 { return iterator(node_algorithms::root_node(this->header_ptr()), this->priv_value_traits_ptr()); }
169 BOOST_INTRUSIVE_FORCEINLINE const_iterator root() const
172 BOOST_INTRUSIVE_FORCEINLINE const_iterator croot() const
173 { return const_iterator(node_algorithms::root_node(this->header_ptr()), this->priv_value_traits_ptr()); }
175 BOOST_INTRUSIVE_FORCEINLINE reverse_iterator rbegin()
176 { return reverse_iterator(end()); }
178 BOOST_INTRUSIVE_FORCEINLINE const_reverse_iterator rbegin() const
179 { return const_reverse_iterator(end()); }
181 BOOST_INTRUSIVE_FORCEINLINE const_reverse_iterator crbegin() const
182 { return const_reverse_iterator(end()); }
184 BOOST_INTRUSIVE_FORCEINLINE reverse_iterator rend()
185 { return reverse_iterator(begin()); }
187 BOOST_INTRUSIVE_FORCEINLINE const_reverse_iterator rend() const
188 { return const_reverse_iterator(begin()); }
190 BOOST_INTRUSIVE_FORCEINLINE const_reverse_iterator crend() const
191 { return const_reverse_iterator(begin()); }
193 void replace_node(iterator replace_this, reference with_this)
195 node_algorithms::replace_node( get_value_traits().to_node_ptr(*replace_this)
197 , get_value_traits().to_node_ptr(with_this));
198 if(safemode_or_autounlink)
199 node_algorithms::init(replace_this.pointed_node());
202 BOOST_INTRUSIVE_FORCEINLINE void rebalance()
203 { node_algorithms::rebalance(this->header_ptr()); }
205 iterator rebalance_subtree(iterator root)
206 { return iterator(node_algorithms::rebalance_subtree(root.pointed_node()), this->priv_value_traits_ptr()); }
208 static iterator s_iterator_to(reference value)
210 BOOST_STATIC_ASSERT((!stateful_value_traits));
211 return iterator (value_traits::to_node_ptr(value), const_value_traits_ptr());
214 static const_iterator s_iterator_to(const_reference value)
216 BOOST_STATIC_ASSERT((!stateful_value_traits));
217 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());
220 iterator iterator_to(reference value)
221 { return iterator (this->get_value_traits().to_node_ptr(value), this->priv_value_traits_ptr()); }
223 const_iterator iterator_to(const_reference value) const
224 { 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()); }
226 BOOST_INTRUSIVE_FORCEINLINE static void init_node(reference value)
227 { node_algorithms::init(value_traits::to_node_ptr(value)); }
231 template<class Less, class T>
238 struct get_compare<void, T>
240 typedef ::std::less<T> type;
243 template<class KeyOfValue, class T>
244 struct get_key_of_value
246 typedef KeyOfValue type;
250 struct get_key_of_value<void, T>
252 typedef ::boost::intrusive::detail::identity<T> type;
255 template<class ValuePtr, class VoidOrKeyOfValue, class VoidOrKeyComp>
258 typedef typename pointer_element<ValuePtr>::type value_type;
259 typedef typename get_key_of_value
260 < VoidOrKeyOfValue, value_type>::type key_of_value;
261 typedef typename key_of_value::type key_type;
262 typedef typename get_compare< VoidOrKeyComp
265 typedef tree_value_compare
266 <ValuePtr, key_compare, key_of_value> value_compare;
269 template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyComp, algo_types AlgoType, typename HeaderHolder>
271 //Put the (possibly empty) functor in the first position to get EBO in MSVC
272 //Use public inheritance to avoid MSVC bugs with closures
273 : public detail::ebo_functor_holder
274 < typename bst_key_types
275 < typename ValueTraits::pointer
281 , public bstbase3<ValueTraits, AlgoType, HeaderHolder>
283 typedef bstbase3<ValueTraits, AlgoType, HeaderHolder> treeheader_t;
284 typedef bst_key_types< typename ValueTraits::pointer
286 , VoidOrKeyComp> key_types;
287 typedef typename treeheader_t::value_traits value_traits;
288 typedef typename treeheader_t::node_algorithms node_algorithms;
289 typedef typename ValueTraits::value_type value_type;
290 typedef typename key_types::key_type key_type;
291 typedef typename key_types::key_of_value key_of_value;
292 typedef typename key_types::key_compare key_compare;
293 typedef typename key_types::value_compare value_compare;
294 typedef typename treeheader_t::iterator iterator;
295 typedef typename treeheader_t::const_iterator const_iterator;
296 typedef typename treeheader_t::node_ptr node_ptr;
297 typedef typename treeheader_t::const_node_ptr const_node_ptr;
299 bstbase2(const key_compare &comp, const ValueTraits &vtraits)
300 : detail::ebo_functor_holder<value_compare>(value_compare(comp)), treeheader_t(vtraits)
303 const value_compare &comp() const
304 { return this->get(); }
306 value_compare &comp()
307 { return this->get(); }
309 typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::pointer) pointer;
310 typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::const_pointer) const_pointer;
311 typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<pointer>::reference) reference;
312 typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<const_pointer>::reference) const_reference;
313 typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<const_pointer>::difference_type) difference_type;
314 typedef typename node_algorithms::insert_commit_data insert_commit_data;
316 BOOST_INTRUSIVE_FORCEINLINE value_compare value_comp() const
317 { return this->comp(); }
319 BOOST_INTRUSIVE_FORCEINLINE key_compare key_comp() const
320 { return this->comp().key_comp(); }
323 BOOST_INTRUSIVE_FORCEINLINE iterator lower_bound(const key_type &key)
324 { return this->lower_bound(key, this->key_comp()); }
326 BOOST_INTRUSIVE_FORCEINLINE const_iterator lower_bound(const key_type &key) const
327 { return this->lower_bound(key, this->key_comp()); }
329 template<class KeyType, class KeyTypeKeyCompare>
330 iterator lower_bound(const KeyType &key, KeyTypeKeyCompare comp)
332 return iterator(node_algorithms::lower_bound
333 (this->header_ptr(), key, this->key_node_comp(comp)), this->priv_value_traits_ptr());
336 template<class KeyType, class KeyTypeKeyCompare>
337 const_iterator lower_bound(const KeyType &key, KeyTypeKeyCompare comp) const
339 return const_iterator(node_algorithms::lower_bound
340 (this->header_ptr(), key, this->key_node_comp(comp)), this->priv_value_traits_ptr());
344 BOOST_INTRUSIVE_FORCEINLINE iterator upper_bound(const key_type &key)
345 { return this->upper_bound(key, this->key_comp()); }
347 template<class KeyType, class KeyTypeKeyCompare>
348 iterator upper_bound(const KeyType &key, KeyTypeKeyCompare comp)
350 return iterator(node_algorithms::upper_bound
351 (this->header_ptr(), key, this->key_node_comp(comp)), this->priv_value_traits_ptr());
354 BOOST_INTRUSIVE_FORCEINLINE const_iterator upper_bound(const key_type &key) const
355 { return this->upper_bound(key, this->key_comp()); }
357 template<class KeyType, class KeyTypeKeyCompare>
358 const_iterator upper_bound(const KeyType &key, KeyTypeKeyCompare comp) const
360 return const_iterator(node_algorithms::upper_bound
361 (this->header_ptr(), key, this->key_node_comp(comp)), this->priv_value_traits_ptr());
364 template<class KeyTypeKeyCompare>
365 struct key_node_comp_ret
366 { typedef detail::key_nodeptr_comp<KeyTypeKeyCompare, value_traits, key_of_value> type; };
368 template<class KeyTypeKeyCompare>
369 BOOST_INTRUSIVE_FORCEINLINE typename key_node_comp_ret<KeyTypeKeyCompare>::type key_node_comp(KeyTypeKeyCompare comp) const
371 return detail::key_nodeptr_comp<KeyTypeKeyCompare, value_traits, key_of_value>(comp, &this->get_value_traits());
375 BOOST_INTRUSIVE_FORCEINLINE iterator find(const key_type &key)
376 { return this->find(key, this->key_comp()); }
378 template<class KeyType, class KeyTypeKeyCompare>
379 iterator find(const KeyType &key, KeyTypeKeyCompare comp)
382 (node_algorithms::find(this->header_ptr(), key, this->key_node_comp(comp)), this->priv_value_traits_ptr());
385 BOOST_INTRUSIVE_FORCEINLINE const_iterator find(const key_type &key) const
386 { return this->find(key, this->key_comp()); }
388 template<class KeyType, class KeyTypeKeyCompare>
389 const_iterator find(const KeyType &key, KeyTypeKeyCompare comp) const
391 return const_iterator
392 (node_algorithms::find(this->header_ptr(), key, this->key_node_comp(comp)), this->priv_value_traits_ptr());
396 BOOST_INTRUSIVE_FORCEINLINE std::pair<iterator,iterator> equal_range(const key_type &key)
397 { return this->equal_range(key, this->key_comp()); }
399 template<class KeyType, class KeyTypeKeyCompare>
400 std::pair<iterator,iterator> equal_range(const KeyType &key, KeyTypeKeyCompare comp)
402 std::pair<node_ptr, node_ptr> ret
403 (node_algorithms::equal_range(this->header_ptr(), key, this->key_node_comp(comp)));
404 return std::pair<iterator, iterator>( iterator(ret.first, this->priv_value_traits_ptr())
405 , iterator(ret.second, this->priv_value_traits_ptr()));
408 BOOST_INTRUSIVE_FORCEINLINE std::pair<const_iterator, const_iterator>
409 equal_range(const key_type &key) const
410 { return this->equal_range(key, this->key_comp()); }
412 template<class KeyType, class KeyTypeKeyCompare>
413 std::pair<const_iterator, const_iterator>
414 equal_range(const KeyType &key, KeyTypeKeyCompare comp) const
416 std::pair<node_ptr, node_ptr> ret
417 (node_algorithms::equal_range(this->header_ptr(), key, this->key_node_comp(comp)));
418 return std::pair<const_iterator, const_iterator>( const_iterator(ret.first, this->priv_value_traits_ptr())
419 , const_iterator(ret.second, this->priv_value_traits_ptr()));
423 BOOST_INTRUSIVE_FORCEINLINE std::pair<iterator,iterator> lower_bound_range(const key_type &key)
424 { return this->lower_bound_range(key, this->key_comp()); }
426 template<class KeyType, class KeyTypeKeyCompare>
427 std::pair<iterator,iterator> lower_bound_range(const KeyType &key, KeyTypeKeyCompare comp)
429 std::pair<node_ptr, node_ptr> ret
430 (node_algorithms::lower_bound_range(this->header_ptr(), key, this->key_node_comp(comp)));
431 return std::pair<iterator, iterator>( iterator(ret.first, this->priv_value_traits_ptr())
432 , iterator(ret.second, this->priv_value_traits_ptr()));
435 BOOST_INTRUSIVE_FORCEINLINE std::pair<const_iterator, const_iterator>
436 lower_bound_range(const key_type &key) const
437 { return this->lower_bound_range(key, this->key_comp()); }
439 template<class KeyType, class KeyTypeKeyCompare>
440 std::pair<const_iterator, const_iterator>
441 lower_bound_range(const KeyType &key, KeyTypeKeyCompare comp) const
443 std::pair<node_ptr, node_ptr> ret
444 (node_algorithms::lower_bound_range(this->header_ptr(), key, this->key_node_comp(comp)));
445 return std::pair<const_iterator, const_iterator>( const_iterator(ret.first, this->priv_value_traits_ptr())
446 , const_iterator(ret.second, this->priv_value_traits_ptr()));
450 BOOST_INTRUSIVE_FORCEINLINE std::pair<iterator,iterator> bounded_range
451 (const key_type &lower_key, const key_type &upper_key, bool left_closed, bool right_closed)
452 { return this->bounded_range(lower_key, upper_key, this->key_comp(), left_closed, right_closed); }
454 template<class KeyType, class KeyTypeKeyCompare>
455 std::pair<iterator,iterator> bounded_range
456 (const KeyType &lower_key, const KeyType &upper_key, KeyTypeKeyCompare comp, bool left_closed, bool right_closed)
458 std::pair<node_ptr, node_ptr> ret
459 (node_algorithms::bounded_range
460 (this->header_ptr(), lower_key, upper_key, this->key_node_comp(comp), left_closed, right_closed));
461 return std::pair<iterator, iterator>( iterator(ret.first, this->priv_value_traits_ptr())
462 , iterator(ret.second, this->priv_value_traits_ptr()));
465 BOOST_INTRUSIVE_FORCEINLINE std::pair<const_iterator,const_iterator> bounded_range
466 (const key_type &lower_key, const key_type &upper_key, bool left_closed, bool right_closed) const
467 { return this->bounded_range(lower_key, upper_key, this->key_comp(), left_closed, right_closed); }
469 template<class KeyType, class KeyTypeKeyCompare>
470 std::pair<const_iterator,const_iterator> bounded_range
471 (const KeyType &lower_key, const KeyType &upper_key, KeyTypeKeyCompare comp, bool left_closed, bool right_closed) const
473 std::pair<node_ptr, node_ptr> ret
474 (node_algorithms::bounded_range
475 (this->header_ptr(), lower_key, upper_key, this->key_node_comp(comp), left_closed, right_closed));
476 return std::pair<const_iterator, const_iterator>( const_iterator(ret.first, this->priv_value_traits_ptr())
477 , const_iterator(ret.second, this->priv_value_traits_ptr()));
480 //insert_unique_check
481 BOOST_INTRUSIVE_FORCEINLINE std::pair<iterator, bool> insert_unique_check
482 (const key_type &key, insert_commit_data &commit_data)
483 { return this->insert_unique_check(key, this->key_comp(), commit_data); }
485 BOOST_INTRUSIVE_FORCEINLINE std::pair<iterator, bool> insert_unique_check
486 (const_iterator hint, const key_type &key, insert_commit_data &commit_data)
487 { return this->insert_unique_check(hint, key, this->key_comp(), commit_data); }
489 template<class KeyType, class KeyTypeKeyCompare>
490 BOOST_INTRUSIVE_DOC1ST(std::pair<iterator BOOST_INTRUSIVE_I bool>
491 , typename detail::disable_if_convertible
492 <KeyType BOOST_INTRUSIVE_I const_iterator BOOST_INTRUSIVE_I
493 std::pair<iterator BOOST_INTRUSIVE_I bool> >::type)
495 (const KeyType &key, KeyTypeKeyCompare comp, insert_commit_data &commit_data)
497 std::pair<node_ptr, bool> ret =
498 (node_algorithms::insert_unique_check
499 (this->header_ptr(), key, this->key_node_comp(comp), commit_data));
500 return std::pair<iterator, bool>(iterator(ret.first, this->priv_value_traits_ptr()), ret.second);
503 template<class KeyType, class KeyTypeKeyCompare>
504 std::pair<iterator, bool> insert_unique_check
505 (const_iterator hint, const KeyType &key, KeyTypeKeyCompare comp, insert_commit_data &commit_data)
507 std::pair<node_ptr, bool> ret =
508 (node_algorithms::insert_unique_check
509 (this->header_ptr(), hint.pointed_node(), key, this->key_node_comp(comp), commit_data));
510 return std::pair<iterator, bool>(iterator(ret.first, this->priv_value_traits_ptr()), ret.second);
514 //Due to MSVC's EBO implementation, to save space and maintain the ABI, we must put the non-empty size member
515 //in the first position, but if size is not going to be stored then we'll use an specialization
516 //that doesn't inherit from size_holder
517 template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyComp, bool ConstantTimeSize, class SizeType, algo_types AlgoType, typename HeaderHolder>
519 : public detail::size_holder<ConstantTimeSize, SizeType>
520 , public bstbase2 < ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, AlgoType, HeaderHolder>
522 typedef bstbase2< ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, AlgoType, HeaderHolder> base_type;
523 typedef typename base_type::key_compare key_compare;
524 typedef typename base_type::value_compare value_compare;
525 typedef SizeType size_type;
526 typedef typename base_type::node_traits node_traits;
527 typedef typename get_algo
528 <AlgoType, node_traits>::type algo_type;
530 BOOST_INTRUSIVE_FORCEINLINE bstbase_hack(const key_compare & comp, const ValueTraits &vtraits)
531 : base_type(comp, vtraits)
533 this->sz_traits().set_size(size_type(0));
536 typedef detail::size_holder<ConstantTimeSize, SizeType> size_traits;
538 BOOST_INTRUSIVE_FORCEINLINE size_traits &sz_traits()
539 { return static_cast<size_traits &>(*this); }
541 BOOST_INTRUSIVE_FORCEINLINE const size_traits &sz_traits() const
542 { return static_cast<const size_traits &>(*this); }
545 //Specialization for ConstantTimeSize == false
546 template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyComp, class SizeType, algo_types AlgoType, typename HeaderHolder>
547 struct bstbase_hack<ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, false, SizeType, AlgoType, HeaderHolder>
548 : public bstbase2 < ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, AlgoType, HeaderHolder>
550 typedef bstbase2< ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, AlgoType, HeaderHolder> base_type;
551 typedef typename base_type::value_compare value_compare;
552 typedef typename base_type::key_compare key_compare;
553 BOOST_INTRUSIVE_FORCEINLINE bstbase_hack(const key_compare & comp, const ValueTraits &vtraits)
554 : base_type(comp, vtraits)
557 typedef detail::size_holder<false, SizeType> size_traits;
559 BOOST_INTRUSIVE_FORCEINLINE size_traits sz_traits() const
560 { return size_traits(); }
564 template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyComp, bool ConstantTimeSize, class SizeType, algo_types AlgoType, typename HeaderHolder>
566 : public bstbase_hack< ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, ConstantTimeSize, SizeType, AlgoType, HeaderHolder>
568 typedef bstbase_hack< ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, ConstantTimeSize, SizeType, AlgoType, HeaderHolder> base_type;
569 typedef ValueTraits value_traits;
570 typedef typename base_type::value_compare value_compare;
571 typedef typename base_type::key_compare key_compare;
572 typedef typename base_type::const_reference const_reference;
573 typedef typename base_type::reference reference;
574 typedef typename base_type::iterator iterator;
575 typedef typename base_type::const_iterator const_iterator;
576 typedef typename base_type::node_traits node_traits;
577 typedef typename get_algo
578 <AlgoType, node_traits>::type node_algorithms;
579 typedef SizeType size_type;
581 BOOST_INTRUSIVE_FORCEINLINE bstbase(const key_compare & comp, const ValueTraits &vtraits)
582 : base_type(comp, vtraits)
585 //Detach all inserted nodes. This will add exception safety to bstree_impl
586 //constructors inserting elements.
589 if(is_safe_autounlink<value_traits::link_mode>::value){
590 node_algorithms::clear_and_dispose
592 , detail::node_disposer<detail::null_disposer, value_traits, AlgoType>
593 (detail::null_disposer(), &this->get_value_traits()));
594 node_algorithms::init(this->header_ptr());
602 //! The class template bstree is an unbalanced intrusive binary search tree
603 //! container. The no-throw guarantee holds only, if the key_compare object
606 //! The complexity guarantees only hold if the tree is balanced, logarithmic
607 //! complexity would increase to linear if the tree is totally unbalanced.
609 //! The template parameter \c T is the type to be managed by the container.
610 //! The user can specify additional options and if no options are provided
611 //! default options are used.
613 //! The container supports the following options:
614 //! \c base_hook<>/member_hook<>/value_traits<>,
615 //! \c constant_time_size<>, \c size_type<> and
617 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
618 template<class T, class ...Options>
620 template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyComp, class SizeType, bool ConstantTimeSize, algo_types AlgoType, typename HeaderHolder>
623 : public bstbase<ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, ConstantTimeSize, SizeType, AlgoType, HeaderHolder>
627 typedef bstbase<ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, ConstantTimeSize, SizeType, AlgoType, HeaderHolder> data_type;
628 typedef tree_iterator<ValueTraits, false> iterator_type;
629 typedef tree_iterator<ValueTraits, true> const_iterator_type;
632 typedef BOOST_INTRUSIVE_IMPDEF(ValueTraits) value_traits;
633 typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::pointer) pointer;
634 typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::const_pointer) const_pointer;
635 typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<pointer>::element_type) value_type;
636 typedef BOOST_INTRUSIVE_IMPDEF(typename data_type::key_type) key_type;
637 typedef BOOST_INTRUSIVE_IMPDEF(typename data_type::key_of_value) key_of_value;
638 typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<pointer>::reference) reference;
639 typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<const_pointer>::reference) const_reference;
640 typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<const_pointer>::difference_type) difference_type;
641 typedef BOOST_INTRUSIVE_IMPDEF(SizeType) size_type;
642 typedef BOOST_INTRUSIVE_IMPDEF(typename data_type::value_compare) value_compare;
643 typedef BOOST_INTRUSIVE_IMPDEF(typename data_type::key_compare) key_compare;
644 typedef BOOST_INTRUSIVE_IMPDEF(iterator_type) iterator;
645 typedef BOOST_INTRUSIVE_IMPDEF(const_iterator_type) const_iterator;
646 typedef BOOST_INTRUSIVE_IMPDEF(boost::intrusive::reverse_iterator<iterator>) reverse_iterator;
647 typedef BOOST_INTRUSIVE_IMPDEF(boost::intrusive::reverse_iterator<const_iterator>) const_reverse_iterator;
648 typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::node_traits) node_traits;
649 typedef BOOST_INTRUSIVE_IMPDEF(typename node_traits::node) node;
650 typedef BOOST_INTRUSIVE_IMPDEF(typename node_traits::node_ptr) node_ptr;
651 typedef BOOST_INTRUSIVE_IMPDEF(typename node_traits::const_node_ptr) const_node_ptr;
653 typedef typename get_algo<AlgoType, node_traits>::type algo_type;
655 typedef BOOST_INTRUSIVE_IMPDEF(algo_type) node_algorithms;
657 static const bool constant_time_size = ConstantTimeSize;
658 static const bool stateful_value_traits = detail::is_stateful_value_traits<value_traits>::value;
663 BOOST_MOVABLE_BUT_NOT_COPYABLE(bstree_impl)
665 static const bool safemode_or_autounlink = is_safe_autounlink<value_traits::link_mode>::value;
667 //Constant-time size is incompatible with auto-unlink hooks!
668 BOOST_STATIC_ASSERT(!(constant_time_size && ((int)value_traits::link_mode == (int)auto_unlink)));
678 typedef typename node_algorithms::insert_commit_data insert_commit_data;
680 //! <b>Effects</b>: Constructs an empty container.
682 //! <b>Complexity</b>: Constant.
684 //! <b>Throws</b>: If value_traits::node_traits::node
685 //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
686 //! or the copy constructor of the key_compare object throws. Basic guarantee.
688 : data_type(key_compare(), value_traits())
691 //! <b>Effects</b>: Constructs an empty container with given comparison and traits.
693 //! <b>Complexity</b>: Constant.
695 //! <b>Throws</b>: If value_traits::node_traits::node
696 //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
697 //! or the copy constructor of the key_compare object throws. Basic guarantee.
698 explicit bstree_impl( const key_compare &cmp, const value_traits &v_traits = value_traits())
699 : data_type(cmp, v_traits)
702 //! <b>Requires</b>: Dereferencing iterator must yield an lvalue of type value_type.
703 //! cmp must be a comparison function that induces a strict weak ordering.
705 //! <b>Effects</b>: Constructs an empty container and inserts elements from
708 //! <b>Complexity</b>: Linear in N if [b, e) is already sorted using
709 //! comp and otherwise N * log N, where N is the distance between first and last.
711 //! <b>Throws</b>: If value_traits::node_traits::node
712 //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
713 //! or the copy constructor/operator() of the key_compare object throws. Basic guarantee.
714 template<class Iterator>
715 bstree_impl( bool unique, Iterator b, Iterator e
716 , const key_compare &cmp = key_compare()
717 , const value_traits &v_traits = value_traits())
718 : data_type(cmp, v_traits)
720 //bstbase releases elements in case of exceptions
722 this->insert_unique(b, e);
724 this->insert_equal(b, e);
727 //! <b>Effects</b>: to-do
729 bstree_impl(BOOST_RV_REF(bstree_impl) x)
730 : data_type(::boost::move(x.comp()), ::boost::move(x.get_value_traits()))
735 //! <b>Effects</b>: to-do
737 BOOST_INTRUSIVE_FORCEINLINE bstree_impl& operator=(BOOST_RV_REF(bstree_impl) x)
738 { this->swap(x); return *this; }
740 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
741 //! <b>Effects</b>: Detaches all elements from this. The objects in the set
742 //! are not deleted (i.e. no destructors are called), but the nodes according to
743 //! the value_traits template parameter are reinitialized and thus can be reused.
745 //! <b>Complexity</b>: Linear to elements contained in *this.
747 //! <b>Throws</b>: Nothing.
751 //! <b>Effects</b>: Returns an iterator pointing to the beginning of the container.
753 //! <b>Complexity</b>: Constant.
755 //! <b>Throws</b>: Nothing.
758 //! <b>Effects</b>: Returns a const_iterator pointing to the beginning of the container.
760 //! <b>Complexity</b>: Constant.
762 //! <b>Throws</b>: Nothing.
763 const_iterator begin() const;
765 //! <b>Effects</b>: Returns a const_iterator pointing to the beginning of the container.
767 //! <b>Complexity</b>: Constant.
769 //! <b>Throws</b>: Nothing.
770 const_iterator cbegin() const;
772 //! <b>Effects</b>: Returns an iterator pointing to the end of the container.
774 //! <b>Complexity</b>: Constant.
776 //! <b>Throws</b>: Nothing.
779 //! <b>Effects</b>: Returns a const_iterator pointing to the end of the container.
781 //! <b>Complexity</b>: Constant.
783 //! <b>Throws</b>: Nothing.
784 const_iterator end() const;
786 //! <b>Effects</b>: Returns a const_iterator pointing to the end of the container.
788 //! <b>Complexity</b>: Constant.
790 //! <b>Throws</b>: Nothing.
791 const_iterator cend() const;
793 //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning of the
794 //! reversed container.
796 //! <b>Complexity</b>: Constant.
798 //! <b>Throws</b>: Nothing.
799 reverse_iterator rbegin();
801 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
802 //! of the reversed container.
804 //! <b>Complexity</b>: Constant.
806 //! <b>Throws</b>: Nothing.
807 const_reverse_iterator rbegin() const;
809 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
810 //! of the reversed container.
812 //! <b>Complexity</b>: Constant.
814 //! <b>Throws</b>: Nothing.
815 const_reverse_iterator crbegin() const;
817 //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
818 //! of the reversed container.
820 //! <b>Complexity</b>: Constant.
822 //! <b>Throws</b>: Nothing.
823 reverse_iterator rend();
825 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
826 //! of the reversed container.
828 //! <b>Complexity</b>: Constant.
830 //! <b>Throws</b>: Nothing.
831 const_reverse_iterator rend() const;
833 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
834 //! of the reversed container.
836 //! <b>Complexity</b>: Constant.
838 //! <b>Throws</b>: Nothing.
839 const_reverse_iterator crend() const;
841 //! <b>Effects</b>: Returns a iterator pointing to the root node of the container or end() if not present.
843 //! <b>Complexity</b>: Constant.
845 //! <b>Throws</b>: Nothing.
848 //! <b>Effects</b>: Returns a const_iterator pointing to the root node of the container or cend() if not present.
850 //! <b>Complexity</b>: Constant.
852 //! <b>Throws</b>: Nothing.
853 const_iterator root() const;
855 //! <b>Effects</b>: Returns a const_iterator pointing to the root node of the container or cend() if not present.
857 //! <b>Complexity</b>: Constant.
859 //! <b>Throws</b>: Nothing.
860 const_iterator croot() const;
862 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
864 //! <b>Precondition</b>: end_iterator must be a valid end iterator
865 //! of the container.
867 //! <b>Effects</b>: Returns a const reference to the container associated to the end iterator
869 //! <b>Throws</b>: Nothing.
871 //! <b>Complexity</b>: Constant.
872 static bstree_impl &container_from_end_iterator(iterator end_iterator)
874 return static_cast<bstree_impl&>
875 (data_type::get_tree_base_from_end_iterator(end_iterator));
878 //! <b>Precondition</b>: end_iterator must be a valid end const_iterator
879 //! of the container.
881 //! <b>Effects</b>: Returns a const reference to the container associated to the iterator
883 //! <b>Throws</b>: Nothing.
885 //! <b>Complexity</b>: Constant.
886 static const bstree_impl &container_from_end_iterator(const_iterator end_iterator)
888 return static_cast<bstree_impl&>
889 (data_type::get_tree_base_from_end_iterator(end_iterator));
892 //! <b>Precondition</b>: it must be a valid iterator
893 //! of the container.
895 //! <b>Effects</b>: Returns a const reference to the container associated to the iterator
897 //! <b>Throws</b>: Nothing.
899 //! <b>Complexity</b>: Logarithmic.
900 static bstree_impl &container_from_iterator(iterator it)
901 { return container_from_end_iterator(it.end_iterator_from_it()); }
903 //! <b>Precondition</b>: it must be a valid end const_iterator
906 //! <b>Effects</b>: Returns a const reference to the container associated to the end iterator
908 //! <b>Throws</b>: Nothing.
910 //! <b>Complexity</b>: Logarithmic.
911 static const bstree_impl &container_from_iterator(const_iterator it)
912 { return container_from_end_iterator(it.end_iterator_from_it()); }
914 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
916 //! <b>Effects</b>: Returns the key_compare object used by the container.
918 //! <b>Complexity</b>: Constant.
920 //! <b>Throws</b>: If key_compare copy-constructor throws.
921 key_compare key_comp() const;
923 //! <b>Effects</b>: Returns the value_compare object used by the container.
925 //! <b>Complexity</b>: Constant.
927 //! <b>Throws</b>: If value_compare copy-constructor throws.
928 value_compare value_comp() const;
930 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
932 //! <b>Effects</b>: Returns true if the container is empty.
934 //! <b>Complexity</b>: Constant.
936 //! <b>Throws</b>: Nothing.
939 if(ConstantTimeSize){
940 return !this->data_type::sz_traits().get_size();
943 return algo_type::unique(this->header_ptr());
947 //! <b>Effects</b>: Returns the number of elements stored in the container.
949 //! <b>Complexity</b>: Linear to elements contained in *this
950 //! if constant-time size option is disabled. Constant time otherwise.
952 //! <b>Throws</b>: Nothing.
953 size_type size() const
955 if(constant_time_size)
956 return this->sz_traits().get_size();
958 return (size_type)node_algorithms::size(this->header_ptr());
962 //! <b>Effects</b>: Swaps the contents of two containers.
964 //! <b>Complexity</b>: Constant.
966 //! <b>Throws</b>: If the comparison functor's swap call throws.
967 void swap(bstree_impl& other)
970 ::boost::adl_move_swap(this->comp(), this->comp());
972 node_algorithms::swap_tree(this->header_ptr(), node_ptr(other.header_ptr()));
973 this->sz_traits().swap(other.sz_traits());
976 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
977 //! Cloner should yield to nodes equivalent to the original nodes.
979 //! <b>Effects</b>: Erases all the elements from *this
980 //! calling Disposer::operator()(pointer), clones all the
981 //! elements from src calling Cloner::operator()(const_reference )
982 //! and inserts them on *this. Copies the predicate from the source container.
984 //! If cloner throws, all cloned elements are unlinked and disposed
985 //! calling Disposer::operator()(pointer).
987 //! <b>Complexity</b>: Linear to erased plus inserted elements.
989 //! <b>Throws</b>: If cloner throws or predicate copy assignment throws. Basic guarantee.
990 template <class Cloner, class Disposer>
991 void clone_from(const bstree_impl &src, Cloner cloner, Disposer disposer)
993 this->clear_and_dispose(disposer);
995 detail::exception_disposer<bstree_impl, Disposer>
996 rollback(*this, disposer);
997 node_algorithms::clone
1000 ,detail::node_cloner <Cloner, value_traits, AlgoType>(cloner, &this->get_value_traits())
1001 ,detail::node_disposer<Disposer, value_traits, AlgoType>(disposer, &this->get_value_traits()));
1002 this->sz_traits().set_size(src.sz_traits().get_size());
1003 this->comp() = src.comp();
1008 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1009 //! Cloner should yield to nodes equivalent to the original nodes.
1011 //! <b>Effects</b>: Erases all the elements from *this
1012 //! calling Disposer::operator()(pointer), clones all the
1013 //! elements from src calling Cloner::operator()(reference)
1014 //! and inserts them on *this. Copies the predicate from the source container.
1016 //! If cloner throws, all cloned elements are unlinked and disposed
1017 //! calling Disposer::operator()(pointer).
1019 //! <b>Complexity</b>: Linear to erased plus inserted elements.
1021 //! <b>Throws</b>: If cloner throws or predicate copy assignment throws. Basic guarantee.
1023 //! <b>Note</b>: This version can modify the source container, useful to implement
1025 template <class Cloner, class Disposer>
1026 void clone_from(BOOST_RV_REF(bstree_impl) src, Cloner cloner, Disposer disposer)
1028 this->clear_and_dispose(disposer);
1030 detail::exception_disposer<bstree_impl, Disposer>
1031 rollback(*this, disposer);
1032 node_algorithms::clone
1035 ,detail::node_cloner <Cloner, value_traits, AlgoType, false>(cloner, &this->get_value_traits())
1036 ,detail::node_disposer<Disposer, value_traits, AlgoType>(disposer, &this->get_value_traits()));
1037 this->sz_traits().set_size(src.sz_traits().get_size());
1038 this->comp() = src.comp();
1043 //! <b>Requires</b>: value must be an lvalue
1045 //! <b>Effects</b>: Inserts value into the container before the upper bound.
1047 //! <b>Complexity</b>: Average complexity for insert element is at
1048 //! most logarithmic.
1050 //! <b>Throws</b>: If the internal key_compare ordering function throws. Strong guarantee.
1052 //! <b>Note</b>: Does not affect the validity of iterators and references.
1053 //! No copy-constructors are called.
1054 iterator insert_equal(reference value)
1056 node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
1057 if(safemode_or_autounlink)
1058 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert));
1059 iterator ret(node_algorithms::insert_equal_upper_bound
1060 (this->header_ptr(), to_insert, this->key_node_comp(this->key_comp())), this->priv_value_traits_ptr());
1061 this->sz_traits().increment();
1065 //! <b>Requires</b>: value must be an lvalue, and "hint" must be
1066 //! a valid iterator.
1068 //! <b>Effects</b>: Inserts x into the container, using "hint" as a hint to
1069 //! where it will be inserted. If "hint" is the upper_bound
1070 //! the insertion takes constant time (two comparisons in the worst case)
1072 //! <b>Complexity</b>: Logarithmic in general, but it is amortized
1073 //! constant time if t is inserted immediately before hint.
1075 //! <b>Throws</b>: If the internal key_compare ordering function throws. Strong guarantee.
1077 //! <b>Note</b>: Does not affect the validity of iterators and references.
1078 //! No copy-constructors are called.
1079 iterator insert_equal(const_iterator hint, reference value)
1081 node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
1082 if(safemode_or_autounlink)
1083 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert));
1084 iterator ret(node_algorithms::insert_equal
1085 (this->header_ptr(), hint.pointed_node(), to_insert, this->key_node_comp(this->key_comp())), this->priv_value_traits_ptr());
1086 this->sz_traits().increment();
1090 //! <b>Requires</b>: Dereferencing iterator must yield an lvalue
1091 //! of type value_type.
1093 //! <b>Effects</b>: Inserts a each element of a range into the container
1094 //! before the upper bound of the key of each element.
1096 //! <b>Complexity</b>: Insert range is in general O(N * log(N)), where N is the
1097 //! size of the range. However, it is linear in N if the range is already sorted
1098 //! by value_comp().
1100 //! <b>Throws</b>: Nothing.
1102 //! <b>Note</b>: Does not affect the validity of iterators and references.
1103 //! No copy-constructors are called.
1104 template<class Iterator>
1105 void insert_equal(Iterator b, Iterator e)
1107 iterator iend(this->end());
1109 this->insert_equal(iend, *b);
1112 //! <b>Requires</b>: value must be an lvalue
1114 //! <b>Effects</b>: Inserts value into the container if the value
1115 //! is not already present.
1117 //! <b>Complexity</b>: Average complexity for insert element is at
1118 //! most logarithmic.
1120 //! <b>Throws</b>: Nothing.
1122 //! <b>Note</b>: Does not affect the validity of iterators and references.
1123 //! No copy-constructors are called.
1124 std::pair<iterator, bool> insert_unique(reference value)
1126 insert_commit_data commit_data;
1127 std::pair<node_ptr, bool> ret =
1128 (node_algorithms::insert_unique_check
1129 (this->header_ptr(), key_of_value()(value), this->key_node_comp(this->key_comp()), commit_data));
1130 return std::pair<iterator, bool>
1131 ( ret.second ? this->insert_unique_commit(value, commit_data)
1132 : iterator(ret.first, this->priv_value_traits_ptr())
1136 //! <b>Requires</b>: value must be an lvalue, and "hint" must be
1137 //! a valid iterator
1139 //! <b>Effects</b>: Tries to insert x into the container, using "hint" as a hint
1140 //! to where it will be inserted.
1142 //! <b>Complexity</b>: Logarithmic in general, but it is amortized
1143 //! constant time (two comparisons in the worst case)
1144 //! if t is inserted immediately before hint.
1146 //! <b>Throws</b>: Nothing.
1148 //! <b>Note</b>: Does not affect the validity of iterators and references.
1149 //! No copy-constructors are called.
1150 iterator insert_unique(const_iterator hint, reference value)
1152 insert_commit_data commit_data;
1153 std::pair<node_ptr, bool> ret =
1154 (node_algorithms::insert_unique_check
1155 (this->header_ptr(), hint.pointed_node(), key_of_value()(value), this->key_node_comp(this->key_comp()), commit_data));
1156 return ret.second ? this->insert_unique_commit(value, commit_data)
1157 : iterator(ret.first, this->priv_value_traits_ptr());
1160 //! <b>Requires</b>: Dereferencing iterator must yield an lvalue
1161 //! of type value_type.
1163 //! <b>Effects</b>: Tries to insert each element of a range into the container.
1165 //! <b>Complexity</b>: Insert range is in general O(N * log(N)), where N is the
1166 //! size of the range. However, it is linear in N if the range is already sorted
1167 //! by value_comp().
1169 //! <b>Throws</b>: Nothing.
1171 //! <b>Note</b>: Does not affect the validity of iterators and references.
1172 //! No copy-constructors are called.
1173 template<class Iterator>
1174 void insert_unique(Iterator b, Iterator e)
1177 iterator iend(this->end());
1179 this->insert_unique(iend, *b);
1183 this->insert_unique(*b);
1187 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
1189 //! <b>Effects</b>: Checks if a value can be inserted in the container, using
1190 //! a user provided key instead of the value itself.
1192 //! <b>Returns</b>: If there is an equivalent value
1193 //! returns a pair containing an iterator to the already present value
1194 //! and false. If the value can be inserted returns true in the returned
1195 //! pair boolean and fills "commit_data" that is meant to be used with
1196 //! the "insert_commit" function.
1198 //! <b>Complexity</b>: Average complexity is at most logarithmic.
1200 //! <b>Throws</b>: If the comp ordering function throws. Strong guarantee.
1201 std::pair<iterator, bool> insert_unique_check(const key_type &key, insert_commit_data &commit_data);
1203 //! <b>Effects</b>: Checks if a value can be inserted in the container, using
1204 //! a user provided key instead of the value itself, using "hint"
1205 //! as a hint to where it will be inserted.
1207 //! <b>Returns</b>: If there is an equivalent value
1208 //! returns a pair containing an iterator to the already present value
1209 //! and false. If the value can be inserted returns true in the returned
1210 //! pair boolean and fills "commit_data" that is meant to be used with
1211 //! the "insert_commit" function.
1213 //! <b>Complexity</b>: Logarithmic in general, but it's amortized
1214 //! constant time if t is inserted immediately before hint.
1216 //! <b>Throws</b>: If the comp ordering function throws. Strong guarantee.
1217 std::pair<iterator, bool> insert_unique_check(const_iterator hint, const key_type &key, insert_commit_data &commit_data);
1219 //! <b>Requires</b>: comp must be a comparison function that induces
1220 //! the same strict weak ordering as key_compare. The difference is that
1221 //! comp compares an arbitrary key with the contained values.
1223 //! <b>Effects</b>: Checks if a value can be inserted in the container, using
1224 //! a user provided key instead of the value itself.
1226 //! <b>Returns</b>: If there is an equivalent value
1227 //! returns a pair containing an iterator to the already present value
1228 //! and false. If the value can be inserted returns true in the returned
1229 //! pair boolean and fills "commit_data" that is meant to be used with
1230 //! the "insert_commit" function.
1232 //! <b>Complexity</b>: Average complexity is at most logarithmic.
1234 //! <b>Throws</b>: If the comp ordering function throws. Strong guarantee.
1236 //! <b>Notes</b>: This function is used to improve performance when constructing
1237 //! a value_type is expensive: if there is an equivalent value
1238 //! the constructed object must be discarded. Many times, the part of the
1239 //! node that is used to impose the order is much cheaper to construct
1240 //! than the value_type and this function offers the possibility to use that
1241 //! part to check if the insertion will be successful.
1243 //! If the check is successful, the user can construct the value_type and use
1244 //! "insert_commit" to insert the object in constant-time. This gives a total
1245 //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
1247 //! "commit_data" remains valid for a subsequent "insert_commit" only if no more
1248 //! objects are inserted or erased from the container.
1249 template<class KeyType, class KeyTypeKeyCompare>
1250 std::pair<iterator, bool> insert_unique_check
1251 (const KeyType &key, KeyTypeKeyCompare comp, insert_commit_data &commit_data);
1253 //! <b>Requires</b>: comp must be a comparison function that induces
1254 //! the same strict weak ordering as key_compare. The difference is that
1255 //! comp compares an arbitrary key with the contained values.
1257 //! <b>Effects</b>: Checks if a value can be inserted in the container, using
1258 //! a user provided key instead of the value itself, using "hint"
1259 //! as a hint to where it will be inserted.
1261 //! <b>Returns</b>: If there is an equivalent value
1262 //! returns a pair containing an iterator to the already present value
1263 //! and false. If the value can be inserted returns true in the returned
1264 //! pair boolean and fills "commit_data" that is meant to be used with
1265 //! the "insert_commit" function.
1267 //! <b>Complexity</b>: Logarithmic in general, but it's amortized
1268 //! constant time if t is inserted immediately before hint.
1270 //! <b>Throws</b>: If the comp ordering function throws. Strong guarantee.
1272 //! <b>Notes</b>: This function is used to improve performance when constructing
1273 //! a value_type is expensive: if there is an equivalent value
1274 //! the constructed object must be discarded. Many times, the part of the
1275 //! constructing that is used to impose the order is much cheaper to construct
1276 //! than the value_type and this function offers the possibility to use that key
1277 //! to check if the insertion will be successful.
1279 //! If the check is successful, the user can construct the value_type and use
1280 //! "insert_commit" to insert the object in constant-time. This can give a total
1281 //! constant-time complexity to the insertion: check(O(1)) + commit(O(1)).
1283 //! "commit_data" remains valid for a subsequent "insert_commit" only if no more
1284 //! objects are inserted or erased from the container.
1285 template<class KeyType, class KeyTypeKeyCompare>
1286 std::pair<iterator, bool> insert_unique_check
1287 (const_iterator hint, const KeyType &key
1288 ,KeyTypeKeyCompare comp, insert_commit_data &commit_data);
1290 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
1292 //! <b>Requires</b>: value must be an lvalue of type value_type. commit_data
1293 //! must have been obtained from a previous call to "insert_check".
1294 //! No objects should have been inserted or erased from the container between
1295 //! the "insert_check" that filled "commit_data" and the call to "insert_commit".
1297 //! <b>Effects</b>: Inserts the value in the container using the information obtained
1298 //! from the "commit_data" that a previous "insert_check" filled.
1300 //! <b>Returns</b>: An iterator to the newly inserted object.
1302 //! <b>Complexity</b>: Constant time.
1304 //! <b>Throws</b>: Nothing.
1306 //! <b>Notes</b>: This function has only sense if a "insert_check" has been
1307 //! previously executed to fill "commit_data". No value should be inserted or
1308 //! erased between the "insert_check" and "insert_commit" calls.
1309 iterator insert_unique_commit(reference value, const insert_commit_data &commit_data)
1311 node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
1312 if(safemode_or_autounlink)
1313 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert));
1315 #if !(defined(BOOST_DISABLE_ASSERTS) || ( defined(BOOST_ENABLE_ASSERT_DEBUG_HANDLER) && defined(NDEBUG) ))
1316 //Test insertion position is correct
1317 iterator p(commit_data.node, this->priv_value_traits_ptr());
1318 if(!commit_data.link_left){
1321 //Check if the insertion point is correct to detect wrong
1322 //uses insert_unique_check
1323 BOOST_ASSERT(( p == this->end() || !this->comp()(*p, value) ));
1324 BOOST_ASSERT(( p == this->begin() || !this->comp()(value, *--p) ));
1327 node_algorithms::insert_unique_commit
1328 (this->header_ptr(), to_insert, commit_data);
1329 this->sz_traits().increment();
1330 return iterator(to_insert, this->priv_value_traits_ptr());
1333 //! <b>Requires</b>: value must be an lvalue, "pos" must be
1334 //! a valid iterator (or end) and must be the succesor of value
1335 //! once inserted according to the predicate
1337 //! <b>Effects</b>: Inserts x into the container before "pos".
1339 //! <b>Complexity</b>: Constant time.
1341 //! <b>Throws</b>: Nothing.
1343 //! <b>Note</b>: This function does not check preconditions so if "pos" is not
1344 //! the successor of "value" container ordering invariant will be broken.
1345 //! This is a low-level function to be used only for performance reasons
1346 //! by advanced users.
1347 iterator insert_before(const_iterator pos, reference value)
1349 node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
1350 if(safemode_or_autounlink)
1351 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert));
1352 this->sz_traits().increment();
1353 return iterator(node_algorithms::insert_before
1354 (this->header_ptr(), pos.pointed_node(), to_insert), this->priv_value_traits_ptr());
1357 //! <b>Requires</b>: value must be an lvalue, and it must be no less
1358 //! than the greatest inserted key
1360 //! <b>Effects</b>: Inserts x into the container in the last position.
1362 //! <b>Complexity</b>: Constant time.
1364 //! <b>Throws</b>: Nothing.
1366 //! <b>Note</b>: This function does not check preconditions so if value is
1367 //! less than the greatest inserted key container ordering invariant will be broken.
1368 //! This function is slightly more efficient than using "insert_before".
1369 //! This is a low-level function to be used only for performance reasons
1370 //! by advanced users.
1371 void push_back(reference value)
1373 node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
1374 if(safemode_or_autounlink)
1375 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert));
1376 this->sz_traits().increment();
1377 node_algorithms::push_back(this->header_ptr(), to_insert);
1380 //! <b>Requires</b>: value must be an lvalue, and it must be no greater
1381 //! than the minimum inserted key
1383 //! <b>Effects</b>: Inserts x into the container in the first position.
1385 //! <b>Complexity</b>: Constant time.
1387 //! <b>Throws</b>: Nothing.
1389 //! <b>Note</b>: This function does not check preconditions so if value is
1390 //! greater than the minimum inserted key container ordering invariant will be broken.
1391 //! This function is slightly more efficient than using "insert_before".
1392 //! This is a low-level function to be used only for performance reasons
1393 //! by advanced users.
1394 void push_front(reference value)
1396 node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
1397 if(safemode_or_autounlink)
1398 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert));
1399 this->sz_traits().increment();
1400 node_algorithms::push_front(this->header_ptr(), to_insert);
1403 //! <b>Effects</b>: Erases the element pointed to by i.
1405 //! <b>Complexity</b>: Average complexity for erase element is constant time.
1407 //! <b>Throws</b>: Nothing.
1409 //! <b>Note</b>: Invalidates the iterators (but not the references)
1410 //! to the erased elements. No destructors are called.
1411 iterator erase(const_iterator i)
1413 const_iterator ret(i);
1415 node_ptr to_erase(i.pointed_node());
1416 if(safemode_or_autounlink)
1417 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!node_algorithms::unique(to_erase));
1418 node_algorithms::erase(this->header_ptr(), to_erase);
1419 this->sz_traits().decrement();
1420 if(safemode_or_autounlink)
1421 node_algorithms::init(to_erase);
1422 return ret.unconst();
1425 //! <b>Effects</b>: Erases the range pointed to by b end e.
1427 //! <b>Complexity</b>: Average complexity for erase range is at most
1428 //! O(log(size() + N)), where N is the number of elements in the range.
1430 //! <b>Throws</b>: Nothing.
1432 //! <b>Note</b>: Invalidates the iterators (but not the references)
1433 //! to the erased elements. No destructors are called.
1434 iterator erase(const_iterator b, const_iterator e)
1435 { size_type n; return this->private_erase(b, e, n); }
1437 //! <b>Effects</b>: Erases all the elements with the given value.
1439 //! <b>Returns</b>: The number of erased elements.
1441 //! <b>Complexity</b>: O(log(size() + N).
1443 //! <b>Throws</b>: Nothing.
1445 //! <b>Note</b>: Invalidates the iterators (but not the references)
1446 //! to the erased elements. No destructors are called.
1447 size_type erase(const key_type &key)
1448 { return this->erase(key, this->key_comp()); }
1450 //! <b>Requires</b>: key is a value such that `*this` is partitioned with respect to
1451 //! comp(nk, key) and !comp(key, nk), with comp(nk, key) implying !comp(key, nk),
1452 //! with nk the key_type of a value_type inserted into `*this`.
1454 //! <b>Effects</b>: Erases all the elements with the given key.
1455 //! according to the comparison functor "comp".
1457 //! <b>Returns</b>: The number of erased elements.
1459 //! <b>Complexity</b>: O(log(size() + N).
1461 //! <b>Throws</b>: Nothing.
1463 //! <b>Note</b>: Invalidates the iterators (but not the references)
1464 //! to the erased elements. No destructors are called.
1465 template<class KeyType, class KeyTypeKeyCompare>
1466 BOOST_INTRUSIVE_DOC1ST(size_type
1467 , typename detail::disable_if_convertible<KeyTypeKeyCompare BOOST_INTRUSIVE_I const_iterator BOOST_INTRUSIVE_I size_type>::type)
1468 erase(const KeyType& key, KeyTypeKeyCompare comp)
1470 std::pair<iterator,iterator> p = this->equal_range(key, comp);
1472 this->private_erase(p.first, p.second, n);
1476 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1478 //! <b>Effects</b>: Erases the element pointed to by i.
1479 //! Disposer::operator()(pointer) is called for the removed element.
1481 //! <b>Complexity</b>: Average complexity for erase element is constant time.
1483 //! <b>Throws</b>: Nothing.
1485 //! <b>Note</b>: Invalidates the iterators
1486 //! to the erased elements.
1487 template<class Disposer>
1488 iterator erase_and_dispose(const_iterator i, Disposer disposer)
1490 node_ptr to_erase(i.pointed_node());
1491 iterator ret(this->erase(i));
1492 disposer(this->get_value_traits().to_value_ptr(to_erase));
1496 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1498 //! <b>Effects</b>: Erases all the elements with the given value.
1499 //! Disposer::operator()(pointer) is called for the removed elements.
1501 //! <b>Returns</b>: The number of erased elements.
1503 //! <b>Complexity</b>: O(log(size() + N).
1505 //! <b>Throws</b>: Nothing.
1507 //! <b>Note</b>: Invalidates the iterators (but not the references)
1508 //! to the erased elements. No destructors are called.
1509 template<class Disposer>
1510 size_type erase_and_dispose(const key_type &key, Disposer disposer)
1512 std::pair<iterator,iterator> p = this->equal_range(key);
1514 this->private_erase(p.first, p.second, n, disposer);
1518 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1520 //! <b>Effects</b>: Erases the range pointed to by b end e.
1521 //! Disposer::operator()(pointer) is called for the removed elements.
1523 //! <b>Complexity</b>: Average complexity for erase range is at most
1524 //! O(log(size() + N)), where N is the number of elements in the range.
1526 //! <b>Throws</b>: Nothing.
1528 //! <b>Note</b>: Invalidates the iterators
1529 //! to the erased elements.
1530 template<class Disposer>
1531 iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer)
1532 { size_type n; return this->private_erase(b, e, n, disposer); }
1534 //! <b>Requires</b>: key is a value such that `*this` is partitioned with respect to
1535 //! comp(nk, key) and !comp(key, nk), with comp(nk, key) implying !comp(key, nk)
1536 //! and nk the key_type of a value_type inserted into `*this`.
1538 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1540 //! <b>Effects</b>: Erases all the elements with the given key.
1541 //! according to the comparison functor "comp".
1542 //! Disposer::operator()(pointer) is called for the removed elements.
1544 //! <b>Returns</b>: The number of erased elements.
1546 //! <b>Complexity</b>: O(log(size() + N).
1548 //! <b>Throws</b>: Nothing.
1550 //! <b>Note</b>: Invalidates the iterators
1551 //! to the erased elements.
1552 template<class KeyType, class KeyTypeKeyCompare, class Disposer>
1553 BOOST_INTRUSIVE_DOC1ST(size_type
1554 , typename detail::disable_if_convertible<KeyTypeKeyCompare BOOST_INTRUSIVE_I const_iterator BOOST_INTRUSIVE_I size_type>::type)
1555 erase_and_dispose(const KeyType& key, KeyTypeKeyCompare comp, Disposer disposer)
1557 std::pair<iterator,iterator> p = this->equal_range(key, comp);
1559 this->private_erase(p.first, p.second, n, disposer);
1563 //! <b>Effects</b>: Erases all of the elements.
1565 //! <b>Complexity</b>: Linear to the number of elements on the container.
1566 //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise.
1568 //! <b>Throws</b>: Nothing.
1570 //! <b>Note</b>: Invalidates the iterators (but not the references)
1571 //! to the erased elements. No destructors are called.
1574 if(safemode_or_autounlink){
1575 this->clear_and_dispose(detail::null_disposer());
1578 node_algorithms::init_header(this->header_ptr());
1579 this->sz_traits().set_size(0);
1583 //! <b>Effects</b>: Erases all of the elements calling disposer(p) for
1584 //! each node to be erased.
1585 //! <b>Complexity</b>: Average complexity for is at most O(log(size() + N)),
1586 //! where N is the number of elements in the container.
1588 //! <b>Throws</b>: Nothing.
1590 //! <b>Note</b>: Invalidates the iterators (but not the references)
1591 //! to the erased elements. Calls N times to disposer functor.
1592 template<class Disposer>
1593 void clear_and_dispose(Disposer disposer)
1595 node_algorithms::clear_and_dispose(this->header_ptr()
1596 , detail::node_disposer<Disposer, value_traits, AlgoType>(disposer, &this->get_value_traits()));
1597 node_algorithms::init_header(this->header_ptr());
1598 this->sz_traits().set_size(0);
1601 //! <b>Effects</b>: Returns the number of contained elements with the given value
1603 //! <b>Complexity</b>: Logarithmic to the number of elements contained plus lineal
1604 //! to number of objects with the given value.
1606 //! <b>Throws</b>: If `key_compare` throws.
1607 size_type count(const key_type &key) const
1608 { return size_type(this->count(key, this->key_comp())); }
1610 //! <b>Requires</b>: key is a value such that `*this` is partitioned with respect to
1611 //! comp(nk, key) and !comp(key, nk), with comp(nk, key) implying !comp(key, nk),
1612 //! and nk the key_type of a value_type inserted into `*this`.
1614 //! <b>Effects</b>: Returns the number of contained elements with the given key
1616 //! <b>Complexity</b>: Logarithmic to the number of elements contained plus lineal
1617 //! to number of objects with the given key.
1619 //! <b>Throws</b>: If `comp` throws.
1620 template<class KeyType, class KeyTypeKeyCompare>
1621 size_type count(const KeyType &key, KeyTypeKeyCompare comp) const
1623 std::pair<const_iterator, const_iterator> ret = this->equal_range(key, comp);
1625 for(; ret.first != ret.second; ++ret.first){ ++n; }
1629 #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1631 //Add non-const overloads to theoretically const members
1632 //as some algorithms have different behavior when non-const versions are used (like splay trees).
1633 size_type count(const key_type &key)
1634 { return size_type(this->count(key, this->key_comp())); }
1636 template<class KeyType, class KeyTypeKeyCompare>
1637 size_type count(const KeyType &key, KeyTypeKeyCompare comp)
1639 std::pair<const_iterator, const_iterator> ret = this->equal_range(key, comp);
1641 for(; ret.first != ret.second; ++ret.first){ ++n; }
1645 #else //defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1647 //! <b>Effects</b>: Returns an iterator to the first element whose
1648 //! key is not less than k or end() if that element does not exist.
1650 //! <b>Complexity</b>: Logarithmic.
1652 //! <b>Throws</b>: If `key_compare` throws.
1653 iterator lower_bound(const key_type &key);
1655 //! <b>Effects</b>: Returns an iterator to the first element whose
1656 //! key is not less than k or end() if that element does not exist.
1658 //! <b>Complexity</b>: Logarithmic.
1660 //! <b>Throws</b>: If `key_compare` throws.
1661 const_iterator lower_bound(const key_type &key) const;
1663 //! @copydoc ::boost::intrusive::bstree::lower_bound(const key_type &)
1664 template<class KeyType, class KeyTypeKeyCompare>
1665 iterator lower_bound(const KeyType &key, KeyTypeKeyCompare comp);
1667 //! @copydoc ::boost::intrusive::bstree::lower_bound(const KeyType&,KeyTypeKeyCompare)
1668 template<class KeyType, class KeyTypeKeyCompare>
1669 const_iterator lower_bound(const KeyType &key, KeyTypeKeyCompare comp) const;
1671 //! <b>Effects</b>: Returns an iterator to the first element whose
1672 //! key is greater than k or end() if that element does not exist.
1674 //! <b>Complexity</b>: Logarithmic.
1676 //! <b>Throws</b>: If `key_compare` throws.
1677 iterator upper_bound(const key_type &key);
1679 //! <b>Requires</b>: key is a value such that `*this` is partitioned with respect to
1680 //! !comp(key, nk), with nk the key_type of a value_type inserted into `*this`.
1682 //! <b>Effects</b>: Returns an iterator to the first element whose
1683 //! key is greater than k according to comp or end() if that element
1686 //! <b>Complexity</b>: Logarithmic.
1688 //! <b>Throws</b>: If `comp` throws.
1689 template<class KeyType, class KeyTypeKeyCompare>
1690 iterator upper_bound(const KeyType &key, KeyTypeKeyCompare comp);
1692 //! @copydoc ::boost::intrusive::bstree::upper_bound(const key_type &)
1693 const_iterator upper_bound(const key_type &key) const;
1695 //! @copydoc ::boost::intrusive::bstree::upper_bound(const KeyType&,KeyTypeKeyCompare)
1696 template<class KeyType, class KeyTypeKeyCompare>
1697 const_iterator upper_bound(const KeyType &key, KeyTypeKeyCompare comp) const;
1699 //! <b>Effects</b>: Finds an iterator to the first element whose key is
1700 //! k or end() if that element does not exist.
1702 //! <b>Complexity</b>: Logarithmic.
1704 //! <b>Throws</b>: If `key_compare` throws.
1705 iterator find(const key_type &key);
1707 //! <b>Requires</b>: key is a value such that `*this` is partitioned with respect to
1708 //! comp(nk, key) and !comp(key, nk), with comp(nk, key) implying !comp(key, nk),
1709 //! and nk the key_type of a value_type inserted into `*this`.
1711 //! <b>Effects</b>: Finds an iterator to the first element whose key is
1712 //! k or end() if that element does not exist.
1714 //! <b>Complexity</b>: Logarithmic.
1716 //! <b>Throws</b>: If `comp` throws.
1717 template<class KeyType, class KeyTypeKeyCompare>
1718 iterator find(const KeyType &key, KeyTypeKeyCompare comp);
1720 //! @copydoc ::boost::intrusive::bstree::find(const key_type &)
1721 const_iterator find(const key_type &key) const;
1723 //! @copydoc ::boost::intrusive::bstree::find(const KeyType&,KeyTypeKeyCompare)
1724 template<class KeyType, class KeyTypeKeyCompare>
1725 const_iterator find(const KeyType &key, KeyTypeKeyCompare comp) const;
1727 //! <b>Effects</b>: Finds a range containing all elements whose key is k or
1728 //! an empty range that indicates the position where those elements would be
1729 //! if they there is no elements with key k.
1731 //! <b>Complexity</b>: Logarithmic.
1733 //! <b>Throws</b>: If `key_compare` throws.
1734 std::pair<iterator,iterator> equal_range(const key_type &key);
1736 //! <b>Requires</b>: key is a value such that `*this` is partitioned with respect to
1737 //! comp(nk, key) and !comp(key, nk), with comp(nk, key) implying !comp(key, nk),
1738 //! with nk the key_type of a value_type inserted into `*this`.
1740 //! <b>Effects</b>: Finds a range containing all elements whose key is k or
1741 //! an empty range that indicates the position where those elements would be
1742 //! if they there is no elements with key k.
1744 //! <b>Complexity</b>: Logarithmic.
1746 //! <b>Throws</b>: If `comp` throws.
1747 template<class KeyType, class KeyTypeKeyCompare>
1748 std::pair<iterator,iterator> equal_range(const KeyType &key, KeyTypeKeyCompare comp);
1750 //! @copydoc ::boost::intrusive::bstree::equal_range(const key_type &)
1751 std::pair<const_iterator, const_iterator> equal_range(const key_type &key) const;
1753 //! @copydoc ::boost::intrusive::bstree::equal_range(const KeyType&,KeyTypeKeyCompare)
1754 template<class KeyType, class KeyTypeKeyCompare>
1755 std::pair<const_iterator, const_iterator>
1756 equal_range(const KeyType &key, KeyTypeKeyCompare comp) const;
1758 //! <b>Requires</b>:
1759 //! `upper_key` shall not precede `lower_key` according to key_compare.
1760 //! [key_comp()(upper_key, lower_key) shall be false]
1762 //! If `lower_key` is equivalent to `upper_key`
1763 //! [!key_comp()(upper_key, lower_key) && !key_comp()(lower_key, upper_key)] then
1764 //! ('left_closed' || 'right_closed') must be false.
1766 //! <b>Effects</b>: Returns an a pair with the following criteria:
1768 //! first = lower_bound(lower_key) if left_closed, upper_bound(lower_key) otherwise
1770 //! second = upper_bound(upper_key) if right_closed, lower_bound(upper_key) otherwise
1772 //! <b>Complexity</b>: Logarithmic.
1774 //! <b>Throws</b>: If `key_compare` throws.
1776 //! <b>Note</b>: This function can be more efficient than calling upper_bound
1777 //! and lower_bound for lower_value and upper_value.
1779 //! <b>Note</b>: Experimental function, the interface might change in future releases.
1780 std::pair<iterator,iterator> bounded_range
1781 (const key_type &lower_key, const key_type &upper_value, bool left_closed, bool right_closed);
1783 //! <b>Requires</b>:
1784 //! `lower_key` is a value such that `*this` is partitioned with respect to
1785 //! comp(nk, lower_key) if left_closed is true, with respect to !comp(lower_key, nk) otherwise.
1787 //! `upper_key` is a value such that `*this` is partitioned with respect to
1788 //! !comp(upper_key, nk) if right_closed is true, with respect to comp(nk, upper_key) otherwise.
1790 //! `upper_key` shall not precede `lower_key` according to comp
1791 //! [comp(upper_key, lower_key) shall be false]
1793 //! If `lower_key` is equivalent to `upper_key`
1794 //! [!comp(upper_key, lower_key) && !comp(lower_key, upper_key)] then
1795 //! ('left_closed' || 'right_closed') must be false.
1797 //! <b>Effects</b>: Returns an a pair with the following criteria:
1799 //! first = lower_bound(lower_key, comp) if left_closed, upper_bound(lower_key, comp) otherwise
1801 //! second = upper_bound(upper_key, comp) if right_closed, lower_bound(upper_key, comp) otherwise
1803 //! <b>Complexity</b>: Logarithmic.
1805 //! <b>Throws</b>: If `comp` throws.
1807 //! <b>Note</b>: This function can be more efficient than calling upper_bound
1808 //! and lower_bound for lower_key and upper_key.
1810 //! <b>Note</b>: Experimental function, the interface might change in future releases.
1811 template<class KeyType, class KeyTypeKeyCompare>
1812 std::pair<iterator,iterator> bounded_range
1813 (const KeyType &lower_key, const KeyType &upper_key, KeyTypeKeyCompare comp, bool left_closed, bool right_closed);
1815 //! @copydoc ::boost::intrusive::bstree::bounded_range(const key_type &,const key_type &,bool,bool)
1816 std::pair<const_iterator,const_iterator> bounded_range
1817 (const key_type &lower_key, const key_type &upper_key, bool left_closed, bool right_closed) const;
1819 //! @copydoc ::boost::intrusive::bstree::bounded_range(const KeyType&,const KeyType&,KeyTypeKeyCompare,bool,bool)
1820 template<class KeyType, class KeyTypeKeyCompare>
1821 std::pair<const_iterator,const_iterator> bounded_range
1822 (const KeyType &lower_key, const KeyType &upper_key, KeyTypeKeyCompare comp, bool left_closed, bool right_closed) const;
1824 //! <b>Requires</b>: value must be an lvalue and shall be in a set of
1825 //! appropriate type. Otherwise the behavior is undefined.
1827 //! <b>Effects</b>: Returns: a valid iterator i belonging to the set
1828 //! that points to the value
1830 //! <b>Complexity</b>: Constant.
1832 //! <b>Throws</b>: Nothing.
1834 //! <b>Note</b>: This static function is available only if the <i>value traits</i>
1836 static iterator s_iterator_to(reference value);
1838 //! <b>Requires</b>: value must be an lvalue and shall be in a set of
1839 //! appropriate type. Otherwise the behavior is undefined.
1841 //! <b>Effects</b>: Returns: a valid const_iterator i belonging to the
1842 //! set that points to the value
1844 //! <b>Complexity</b>: Constant.
1846 //! <b>Throws</b>: Nothing.
1848 //! <b>Note</b>: This static function is available only if the <i>value traits</i>
1850 static const_iterator s_iterator_to(const_reference value);
1852 //! <b>Requires</b>: value must be an lvalue and shall be in a set of
1853 //! appropriate type. Otherwise the behavior is undefined.
1855 //! <b>Effects</b>: Returns: a valid iterator i belonging to the set
1856 //! that points to the value
1858 //! <b>Complexity</b>: Constant.
1860 //! <b>Throws</b>: Nothing.
1861 iterator iterator_to(reference value);
1863 //! <b>Requires</b>: value must be an lvalue and shall be in a set of
1864 //! appropriate type. Otherwise the behavior is undefined.
1866 //! <b>Effects</b>: Returns: a valid const_iterator i belonging to the
1867 //! set that points to the value
1869 //! <b>Complexity</b>: Constant.
1871 //! <b>Throws</b>: Nothing.
1872 const_iterator iterator_to(const_reference value) const;
1874 //! <b>Requires</b>: value shall not be in a container.
1876 //! <b>Effects</b>: init_node puts the hook of a value in a well-known default
1879 //! <b>Throws</b>: Nothing.
1881 //! <b>Complexity</b>: Constant time.
1883 //! <b>Note</b>: This function puts the hook in the well-known default state
1884 //! used by auto_unlink and safe hooks.
1885 static void init_node(reference value);
1887 #endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1889 //! <b>Effects</b>: Unlinks the leftmost node from the container.
1891 //! <b>Complexity</b>: Average complexity is constant time.
1893 //! <b>Throws</b>: Nothing.
1895 //! <b>Notes</b>: This function breaks the container and the container can
1896 //! only be used for more unlink_leftmost_without_rebalance calls.
1897 //! This function is normally used to achieve a step by step
1898 //! controlled destruction of the container.
1899 pointer unlink_leftmost_without_rebalance()
1901 node_ptr to_be_disposed(node_algorithms::unlink_leftmost_without_rebalance
1902 (this->header_ptr()));
1905 this->sz_traits().decrement();
1906 if(safemode_or_autounlink)//If this is commented does not work with normal_link
1907 node_algorithms::init(to_be_disposed);
1908 return this->get_value_traits().to_value_ptr(to_be_disposed);
1911 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1913 //! <b>Requires</b>: replace_this must be a valid iterator of *this
1914 //! and with_this must not be inserted in any container.
1916 //! <b>Effects</b>: Replaces replace_this in its position in the
1917 //! container with with_this. The container does not need to be rebalanced.
1919 //! <b>Complexity</b>: Constant.
1921 //! <b>Throws</b>: Nothing.
1923 //! <b>Note</b>: This function will break container ordering invariants if
1924 //! with_this is not equivalent to *replace_this according to the
1925 //! ordering rules. This function is faster than erasing and inserting
1926 //! the node, since no rebalancing or comparison is needed.
1927 void replace_node(iterator replace_this, reference with_this);
1929 //! <b>Effects</b>: Rebalances the tree.
1931 //! <b>Throws</b>: Nothing.
1933 //! <b>Complexity</b>: Linear.
1936 //! <b>Requires</b>: old_root is a node of a tree.
1938 //! <b>Effects</b>: Rebalances the subtree rooted at old_root.
1940 //! <b>Returns</b>: The new root of the subtree.
1942 //! <b>Throws</b>: Nothing.
1944 //! <b>Complexity</b>: Linear to the elements in the subtree.
1945 iterator rebalance_subtree(iterator root);
1947 #endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1949 //! <b>Effects</b>: removes "value" from the container.
1951 //! <b>Throws</b>: Nothing.
1953 //! <b>Complexity</b>: Logarithmic time.
1955 //! <b>Note</b>: This static function is only usable with non-constant
1956 //! time size containers that have stateless comparison functors.
1958 //! If the user calls
1959 //! this function with a constant time size container or stateful comparison
1960 //! functor a compilation error will be issued.
1961 static void remove_node(reference value)
1963 BOOST_STATIC_ASSERT((!constant_time_size));
1964 node_ptr to_remove(value_traits::to_node_ptr(value));
1965 node_algorithms::unlink(to_remove);
1966 if(safemode_or_autounlink)
1967 node_algorithms::init(to_remove);
1970 //! <b>Requires</b>: "source" container's Options can only can differ in the comparison
1971 //! function from *this.
1973 //! <b>Effects</b>: Attempts to extract each element in source and insert it into a using
1974 //! the comparison object of *this. If there is an element in a with key equivalent to the
1975 //! key of an element from source, then that element is not extracted from source.
1977 //! <b>Postcondition</b>: Pointers and references to the transferred elements of source refer
1978 //! to those same elements but as members of *this. Iterators referring to the transferred
1979 //! elements will continue to refer to their elements, but they now behave as iterators into *this,
1980 //! not into source.
1982 //! <b>Throws</b>: Nothing unless the comparison object throws.
1984 //! <b>Complexity</b>: N log(a.size() + N) (N has the value source.size())
1985 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1986 template<class T, class ...Options2> void merge_unique(bstree<T, Options2...> &);
1988 template<class Compare2>
1989 void merge_unique(bstree_impl
1990 <ValueTraits, VoidOrKeyOfValue, Compare2, SizeType, ConstantTimeSize, AlgoType, HeaderHolder> &source)
1993 node_ptr it (node_algorithms::begin_node(source.header_ptr()))
1994 , itend(node_algorithms::end_node (source.header_ptr()));
1997 node_ptr const p(it);
1998 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || !node_algorithms::unique(p));
1999 it = node_algorithms::next_node(it);
2000 if( node_algorithms::transfer_unique(this->header_ptr(), this->key_node_comp(this->key_comp()), source.header_ptr(), p) ){
2001 source.sz_traits().decrement();
2002 this->sz_traits().increment();
2007 //! <b>Requires</b>: "source" container's Options can only can differ in the comparison
2008 //! function from *this.
2010 //! <b>Effects</b>: Extracts each element in source and insert it into a using
2011 //! the comparison object of *this.
2013 //! <b>Postcondition</b>: Pointers and references to the transferred elements of source refer
2014 //! to those same elements but as members of *this. Iterators referring to the transferred
2015 //! elements will continue to refer to their elements, but they now behave as iterators into *this,
2016 //! not into source.
2018 //! <b>Throws</b>: Nothing unless the comparison object throws.
2020 //! <b>Complexity</b>: N log(a.size() + N) (N has the value source.size())
2021 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
2022 template<class T, class ...Options2> void merge_equal(bstree<T, Options2...> &);
2024 template<class Compare2>
2025 void merge_equal(bstree_impl
2026 <ValueTraits, VoidOrKeyOfValue, Compare2, SizeType, ConstantTimeSize, AlgoType, HeaderHolder> &source)
2029 node_ptr it (node_algorithms::begin_node(source.header_ptr()))
2030 , itend(node_algorithms::end_node (source.header_ptr()));
2033 node_ptr const p(it);
2034 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || !node_algorithms::unique(p));
2035 it = node_algorithms::next_node(it);
2036 node_algorithms::transfer_equal(this->header_ptr(), this->key_node_comp(this->key_comp()), source.header_ptr(), p);
2037 source.sz_traits().decrement();
2038 this->sz_traits().increment();
2042 //! <b>Effects</b>: Asserts the integrity of the container with additional checks provided by the user.
2044 //! <b>Complexity</b>: Linear time.
2046 //! <b>Note</b>: The method might not have effect when asserts are turned off (e.g., with NDEBUG).
2047 //! Experimental function, interface might change in future versions.
2048 template <class ExtraChecker>
2049 void check(ExtraChecker extra_checker) const
2051 typedef detail::key_nodeptr_comp<key_compare, value_traits, key_of_value> nodeptr_comp_t;
2052 nodeptr_comp_t nodeptr_comp(this->key_comp(), &this->get_value_traits());
2053 typedef typename get_node_checker<AlgoType, ValueTraits, nodeptr_comp_t, ExtraChecker>::type node_checker_t;
2054 typename node_checker_t::return_type checker_return;
2055 node_algorithms::check(this->header_ptr(), node_checker_t(nodeptr_comp, extra_checker), checker_return);
2056 if (constant_time_size)
2057 BOOST_INTRUSIVE_INVARIANT_ASSERT(this->sz_traits().get_size() == checker_return.node_count);
2060 //! <b>Effects</b>: Asserts the integrity of the container.
2062 //! <b>Complexity</b>: Linear time.
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.
2068 check(detail::empty_node_checker<ValueTraits>());
2071 friend bool operator==(const bstree_impl &x, const bstree_impl &y)
2073 if(constant_time_size && x.size() != y.size()){
2076 return boost::intrusive::algo_equal(x.cbegin(), x.cend(), y.cbegin(), y.cend());
2079 friend bool operator!=(const bstree_impl &x, const bstree_impl &y)
2080 { return !(x == y); }
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()); }
2085 friend bool operator>(const bstree_impl &x, const bstree_impl &y)
2088 friend bool operator<=(const bstree_impl &x, const bstree_impl &y)
2089 { return !(x > y); }
2091 friend bool operator>=(const bstree_impl &x, const bstree_impl &y)
2092 { return !(x < y); }
2094 friend void swap(bstree_impl &x, bstree_impl &y)
2099 template<class Disposer>
2100 iterator private_erase(const_iterator b, const_iterator e, size_type &n, Disposer disposer)
2102 for(n = 0; b != e; ++n)
2103 this->erase_and_dispose(b++, disposer);
2107 iterator private_erase(const_iterator b, const_iterator e, size_type &n)
2109 for(n = 0; b != e; ++n)
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>
2121 template<class T, class O1 = void, class O2 = void
2122 , class O3 = void, class O4 = void
2123 , class O5 = void, class O6 = void>
2128 typedef typename pack_options
2130 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
2131 O1, O2, O3, O4, O5, O6
2135 >::type packed_options;
2137 typedef typename detail::get_value_traits
2138 <T, typename packed_options::proto_value_traits>::type 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
2147 , typename packed_options::header_holder_type
2148 > implementation_defined;
2150 typedef implementation_defined type;
2154 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
2156 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
2157 template<class T, class O1, class O2, class O3, class O4, class O5, class O6>
2159 template<class T, class ...Options>
2162 : public make_bstree<T,
2163 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
2164 O1, O2, O3, O4, O5, O6
2170 typedef typename make_bstree
2172 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
2173 O1, O2, O3, O4, O5, O6
2178 BOOST_MOVABLE_BUT_NOT_COPYABLE(bstree)
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;
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));
2193 explicit bstree( const key_compare &cmp, const value_traits &v_traits = value_traits())
2194 : Base(cmp, v_traits)
2197 template<class Iterator>
2198 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)
2204 bstree(BOOST_RV_REF(bstree) x)
2205 : Base(BOOST_MOVE_BASE(Base, x))
2208 bstree& operator=(BOOST_RV_REF(bstree) x)
2209 { return static_cast<bstree &>(this->Base::operator=(BOOST_MOVE_BASE(Base, x))); }
2211 template <class Cloner, class Disposer>
2212 void clone_from(const bstree &src, Cloner cloner, Disposer disposer)
2213 { Base::clone_from(src, cloner, disposer); }
2215 template <class Cloner, class Disposer>
2216 void clone_from(BOOST_RV_REF(bstree) src, Cloner cloner, Disposer disposer)
2217 { Base::clone_from(BOOST_MOVE_BASE(Base, src), cloner, disposer); }
2219 static bstree &container_from_end_iterator(iterator end_iterator)
2220 { return static_cast<bstree &>(Base::container_from_end_iterator(end_iterator)); }
2222 static const bstree &container_from_end_iterator(const_iterator end_iterator)
2223 { return static_cast<const bstree &>(Base::container_from_end_iterator(end_iterator)); }
2225 static bstree &container_from_iterator(iterator it)
2226 { return static_cast<bstree &>(Base::container_from_iterator(it)); }
2228 static const bstree &container_from_iterator(const_iterator it)
2229 { return static_cast<const bstree &>(Base::container_from_iterator(it)); }
2233 } //namespace intrusive
2236 #include <boost/intrusive/detail/config_end.hpp>
2238 #endif //BOOST_INTRUSIVE_BSTREE_HPP