1 /////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2007-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 /////////////////////////////////////////////////////////////////////////////
13 // The option that yields to non-floating point 1/sqrt(2) alpha is taken
14 // from the scapegoat tree implementation of the PSPP library.
16 /////////////////////////////////////////////////////////////////////////////
18 #ifndef BOOST_INTRUSIVE_SGTREE_HPP
19 #define BOOST_INTRUSIVE_SGTREE_HPP
21 #include <boost/intrusive/detail/config_begin.hpp>
22 #include <boost/intrusive/intrusive_fwd.hpp>
23 #include <boost/intrusive/detail/assert.hpp>
24 #include <boost/static_assert.hpp>
25 #include <boost/intrusive/bs_set_hook.hpp>
26 #include <boost/intrusive/bstree.hpp>
27 #include <boost/intrusive/detail/tree_node.hpp>
28 #include <boost/intrusive/pointer_traits.hpp>
29 #include <boost/intrusive/detail/mpl.hpp>
30 #include <boost/intrusive/detail/math.hpp>
31 #include <boost/intrusive/detail/get_value_traits.hpp>
32 #include <boost/intrusive/sgtree_algorithms.hpp>
33 #include <boost/intrusive/detail/key_nodeptr_comp.hpp>
34 #include <boost/intrusive/link_mode.hpp>
36 #include <boost/move/utility_core.hpp>
37 #include <boost/move/adl_move_swap.hpp>
40 #include <boost/intrusive/detail/minimal_less_equal_header.hpp>
41 #include <boost/intrusive/detail/minimal_pair_header.hpp> //std::pair
45 #if defined(BOOST_HAS_PRAGMA_ONCE)
56 /////////////////////////////////////////////////////////////
58 // Halpha for fixed floating_point<false> option
60 /////////////////////////////////////////////////////////////
62 //! Returns floor(log2(n)/log2(sqrt(2))) -> floor(2*log2(n))
63 //! Undefined if N is 0.
65 //! This function does not use float point operations.
66 inline std::size_t calculate_h_sqrt2 (std::size_t n)
68 std::size_t f_log2 = detail::floor_log2(n);
69 return (2*f_log2) + static_cast<std::size_t>(n >= detail::sqrt2_pow_2xplus1(f_log2));
72 struct h_alpha_sqrt2_t
74 h_alpha_sqrt2_t(void){}
75 std::size_t operator()(std::size_t n) const
76 { return calculate_h_sqrt2(n); }
79 struct alpha_0_75_by_max_size_t
81 alpha_0_75_by_max_size_t(void){}
83 std::size_t operator()(std::size_t max_tree_size) const
85 const std::size_t max_tree_size_limit = ((~std::size_t(0))/std::size_t(3));
86 return max_tree_size > max_tree_size_limit ? max_tree_size/4*3 : max_tree_size*3/4;
90 /////////////////////////////////////////////////////////////
92 // Halpha for fixed floating_point<true> option
94 /////////////////////////////////////////////////////////////
98 explicit h_alpha_t(float inv_minus_logalpha)
99 : inv_minus_logalpha_(inv_minus_logalpha)
102 std::size_t operator()(std::size_t n) const
104 ////////////////////////////////////////////////////////////
105 // This function must return "floor(log2(1/alpha(n)))" ->
106 // floor(log2(n)/log(1/alpha)) ->
107 // floor(log2(n)/-log2(alpha))
108 // floor(log2(n)*(1/-log2(alpha)))
109 ////////////////////////////////////////////////////////////
110 return static_cast<std::size_t>(detail::fast_log2(float(n))*inv_minus_logalpha_);
114 //Since the function will be repeatedly called
115 //precalculate constant data to avoid repeated
116 //calls to log and division.
117 //This will store 1/(-std::log2(alpha_))
118 float inv_minus_logalpha_;
121 struct alpha_by_max_size_t
123 explicit alpha_by_max_size_t(float alpha)
127 float operator()(std::size_t max_tree_size) const
128 { return float(max_tree_size)*alpha_; }
134 template<bool Activate, class SizeType>
137 typedef boost::intrusive::detail::h_alpha_t h_alpha_t;
138 typedef boost::intrusive::detail::alpha_by_max_size_t multiply_by_alpha_t;
142 { set_alpha(0.70711f); } // ~1/sqrt(2)
144 float get_alpha() const
147 void set_alpha(float alpha)
150 inv_minus_logalpha_ = 1/(-detail::fast_log2(alpha));
153 h_alpha_t get_h_alpha_t() const
154 { return h_alpha_t(inv_minus_logalpha_); }
156 multiply_by_alpha_t get_multiply_by_alpha_t() const
157 { return multiply_by_alpha_t(alpha_); }
159 SizeType &get_max_tree_size()
160 { return max_tree_size_; }
164 float inv_minus_logalpha_;
165 SizeType max_tree_size_;
168 template<class SizeType>
169 struct alpha_holder<false, SizeType>
171 //This specialization uses alpha = 1/sqrt(2)
172 //without using floating point operations
173 //Downside: alpha CAN't be changed.
174 typedef boost::intrusive::detail::h_alpha_sqrt2_t h_alpha_t;
175 typedef boost::intrusive::detail::alpha_0_75_by_max_size_t multiply_by_alpha_t;
181 float get_alpha() const
182 { return 0.70710677f; }
184 void set_alpha(float)
185 { //alpha CAN't be changed.
186 BOOST_INTRUSIVE_INVARIANT_ASSERT(0);
189 h_alpha_t get_h_alpha_t() const
190 { return h_alpha_t(); }
192 multiply_by_alpha_t get_multiply_by_alpha_t() const
193 { return multiply_by_alpha_t(); }
195 SizeType &get_max_tree_size()
196 { return max_tree_size_; }
199 SizeType max_tree_size_;
202 } //namespace detail{
204 struct sgtree_defaults
207 static const bool floating_point = true;
212 //! The class template sgtree is an intrusive scapegoat tree container, that
213 //! is used to construct intrusive sg_set and sg_multiset containers.
214 //! The no-throw guarantee holds only, if the value_compare object
217 //! The template parameter \c T is the type to be managed by the container.
218 //! The user can specify additional options and if no options are provided
219 //! default options are used.
221 //! The container supports the following options:
222 //! \c base_hook<>/member_hook<>/value_traits<>,
223 //! \c floating_point<>, \c size_type<> and
225 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
226 template<class T, class ...Options>
228 template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyComp, class SizeType, bool FloatingPoint, typename HeaderHolder>
232 : public bstree_impl<ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, SizeType, true, SgTreeAlgorithms, HeaderHolder>
233 , public detail::alpha_holder<FloatingPoint, SizeType>
237 typedef ValueTraits value_traits;
239 typedef bstree_impl< ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, SizeType
240 , true, SgTreeAlgorithms, HeaderHolder> tree_type;
241 typedef tree_type implementation_defined;
245 typedef typename implementation_defined::pointer pointer;
246 typedef typename implementation_defined::const_pointer const_pointer;
247 typedef typename implementation_defined::value_type value_type;
248 typedef typename implementation_defined::key_type key_type;
249 typedef typename implementation_defined::key_of_value key_of_value;
250 typedef typename implementation_defined::reference reference;
251 typedef typename implementation_defined::const_reference const_reference;
252 typedef typename implementation_defined::difference_type difference_type;
253 typedef typename implementation_defined::size_type size_type;
254 typedef typename implementation_defined::value_compare value_compare;
255 typedef typename implementation_defined::key_compare key_compare;
256 typedef typename implementation_defined::iterator iterator;
257 typedef typename implementation_defined::const_iterator const_iterator;
258 typedef typename implementation_defined::reverse_iterator reverse_iterator;
259 typedef typename implementation_defined::const_reverse_iterator const_reverse_iterator;
260 typedef typename implementation_defined::node_traits node_traits;
261 typedef typename implementation_defined::node node;
262 typedef typename implementation_defined::node_ptr node_ptr;
263 typedef typename implementation_defined::const_node_ptr const_node_ptr;
264 typedef BOOST_INTRUSIVE_IMPDEF(sgtree_algorithms<node_traits>) node_algorithms;
266 static const bool constant_time_size = implementation_defined::constant_time_size;
267 static const bool floating_point = FloatingPoint;
268 static const bool stateful_value_traits = implementation_defined::stateful_value_traits;
274 typedef detail::alpha_holder<FloatingPoint, SizeType> alpha_traits;
275 typedef typename alpha_traits::h_alpha_t h_alpha_t;
276 typedef typename alpha_traits::multiply_by_alpha_t multiply_by_alpha_t;
278 BOOST_MOVABLE_BUT_NOT_COPYABLE(sgtree_impl)
279 BOOST_STATIC_ASSERT(((int)value_traits::link_mode != (int)auto_unlink));
281 enum { safemode_or_autounlink =
282 (int)value_traits::link_mode == (int)auto_unlink ||
283 (int)value_traits::link_mode == (int)safe_link };
289 typedef BOOST_INTRUSIVE_IMPDEF(typename node_algorithms::insert_commit_data) insert_commit_data;
291 //! @copydoc ::boost::intrusive::bstree::bstree()
296 //! @copydoc ::boost::intrusive::bstree::bstree(const key_compare &,const value_traits &)
297 explicit sgtree_impl( const key_compare &cmp, const value_traits &v_traits = value_traits())
298 : tree_type(cmp, v_traits)
301 //! @copydoc ::boost::intrusive::bstree::bstree(bool,Iterator,Iterator,const key_compare &,const value_traits &)
302 template<class Iterator>
303 sgtree_impl( bool unique, Iterator b, Iterator e
304 , const key_compare &cmp = key_compare()
305 , const value_traits &v_traits = value_traits())
306 : tree_type(cmp, v_traits)
309 this->insert_unique(b, e);
311 this->insert_equal(b, e);
314 //! @copydoc ::boost::intrusive::bstree::bstree(bstree &&)
315 sgtree_impl(BOOST_RV_REF(sgtree_impl) x)
316 : tree_type(BOOST_MOVE_BASE(tree_type, x)), alpha_traits(x.get_alpha_traits())
317 { ::boost::adl_move_swap(this->get_alpha_traits(), x.get_alpha_traits()); }
319 //! @copydoc ::boost::intrusive::bstree::operator=(bstree &&)
320 sgtree_impl& operator=(BOOST_RV_REF(sgtree_impl) x)
322 this->get_alpha_traits() = x.get_alpha_traits();
323 return static_cast<sgtree_impl&>(tree_type::operator=(BOOST_MOVE_BASE(tree_type, x)));
329 const alpha_traits &get_alpha_traits() const
332 alpha_traits &get_alpha_traits()
335 h_alpha_t get_h_alpha_func() const
336 { return this->get_alpha_traits().get_h_alpha_t(); }
338 multiply_by_alpha_t get_alpha_by_max_size_func() const
339 { return this->get_alpha_traits().get_multiply_by_alpha_t(); }
345 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
346 //! @copydoc ::boost::intrusive::bstree::~bstree()
349 //! @copydoc ::boost::intrusive::bstree::begin()
352 //! @copydoc ::boost::intrusive::bstree::begin()const
353 const_iterator begin() const;
355 //! @copydoc ::boost::intrusive::bstree::cbegin()const
356 const_iterator cbegin() const;
358 //! @copydoc ::boost::intrusive::bstree::end()
361 //! @copydoc ::boost::intrusive::bstree::end()const
362 const_iterator end() const;
364 //! @copydoc ::boost::intrusive::bstree::cend()const
365 const_iterator cend() const;
367 //! @copydoc ::boost::intrusive::bstree::rbegin()
368 reverse_iterator rbegin();
370 //! @copydoc ::boost::intrusive::bstree::rbegin()const
371 const_reverse_iterator rbegin() const;
373 //! @copydoc ::boost::intrusive::bstree::crbegin()const
374 const_reverse_iterator crbegin() const;
376 //! @copydoc ::boost::intrusive::bstree::rend()
377 reverse_iterator rend();
379 //! @copydoc ::boost::intrusive::bstree::rend()const
380 const_reverse_iterator rend() const;
382 //! @copydoc ::boost::intrusive::bstree::crend()const
383 const_reverse_iterator crend() const;
385 //! @copydoc ::boost::intrusive::bstree::root()
388 //! @copydoc ::boost::intrusive::bstree::root()const
389 const_iterator root() const;
391 //! @copydoc ::boost::intrusive::bstree::croot()const
392 const_iterator croot() const;
394 //! @copydoc ::boost::intrusive::bstree::container_from_end_iterator(iterator)
395 static sgtree_impl &container_from_end_iterator(iterator end_iterator);
397 //! @copydoc ::boost::intrusive::bstree::container_from_end_iterator(const_iterator)
398 static const sgtree_impl &container_from_end_iterator(const_iterator end_iterator);
400 //! @copydoc ::boost::intrusive::bstree::container_from_iterator(iterator)
401 static sgtree_impl &container_from_iterator(iterator it);
403 //! @copydoc ::boost::intrusive::bstree::container_from_iterator(const_iterator)
404 static const sgtree_impl &container_from_iterator(const_iterator it);
406 //! @copydoc ::boost::intrusive::bstree::key_comp()const
407 key_compare key_comp() const;
409 //! @copydoc ::boost::intrusive::bstree::value_comp()const
410 value_compare value_comp() const;
412 //! @copydoc ::boost::intrusive::bstree::empty()const
415 //! @copydoc ::boost::intrusive::bstree::size()const
416 size_type size() const;
418 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
420 //! @copydoc ::boost::intrusive::bstree::swap
421 void swap(sgtree_impl& other)
424 this->tree_type::swap(static_cast<tree_type&>(other));
425 ::boost::adl_move_swap(this->get_alpha_traits(), other.get_alpha_traits());
428 //! @copydoc ::boost::intrusive::bstree::clone_from(const bstree&,Cloner,Disposer)
429 //! Additional notes: it also copies the alpha factor from the source container.
430 template <class Cloner, class Disposer>
431 void clone_from(const sgtree_impl &src, Cloner cloner, Disposer disposer)
433 tree_type::clone_from(src, cloner, disposer);
434 this->get_alpha_traits() = src.get_alpha_traits();
437 //! @copydoc ::boost::intrusive::bstree::clone_from(bstree&&,Cloner,Disposer)
438 //! Additional notes: it also copies the alpha factor from the source container.
439 template <class Cloner, class Disposer>
440 void clone_from(BOOST_RV_REF(sgtree_impl) src, Cloner cloner, Disposer disposer)
442 tree_type::clone_from(BOOST_MOVE_BASE(tree_type, src), cloner, disposer);
443 this->get_alpha_traits() = ::boost::move(src.get_alpha_traits());
446 //! @copydoc ::boost::intrusive::bstree::insert_equal(reference)
447 iterator insert_equal(reference value)
449 node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
450 if(safemode_or_autounlink)
451 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert));
452 std::size_t max_tree_size = (std::size_t)this->max_tree_size_;
453 node_ptr p = node_algorithms::insert_equal_upper_bound
454 (this->tree_type::header_ptr(), to_insert, this->key_node_comp(this->key_comp())
455 , (size_type)this->size(), this->get_h_alpha_func(), max_tree_size);
456 this->tree_type::sz_traits().increment();
457 this->max_tree_size_ = (size_type)max_tree_size;
458 return iterator(p, this->priv_value_traits_ptr());
461 //! @copydoc ::boost::intrusive::bstree::insert_equal(const_iterator,reference)
462 iterator insert_equal(const_iterator hint, reference value)
464 node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
465 if(safemode_or_autounlink)
466 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert));
467 std::size_t max_tree_size = (std::size_t)this->max_tree_size_;
468 node_ptr p = node_algorithms::insert_equal
469 ( this->tree_type::header_ptr(), hint.pointed_node(), to_insert, this->key_node_comp(this->key_comp())
470 , (std::size_t)this->size(), this->get_h_alpha_func(), max_tree_size);
471 this->tree_type::sz_traits().increment();
472 this->max_tree_size_ = (size_type)max_tree_size;
473 return iterator(p, this->priv_value_traits_ptr());
476 //! @copydoc ::boost::intrusive::bstree::insert_equal(Iterator,Iterator)
477 template<class Iterator>
478 void insert_equal(Iterator b, Iterator e)
480 iterator iend(this->end());
482 this->insert_equal(iend, *b);
485 //! @copydoc ::boost::intrusive::bstree::insert_unique(reference)
486 std::pair<iterator, bool> insert_unique(reference value)
488 insert_commit_data commit_data;
489 std::pair<iterator, bool> ret = this->insert_unique_check
490 (key_of_value()(value), this->key_comp(), commit_data);
493 return std::pair<iterator, bool> (this->insert_unique_commit(value, commit_data), true);
496 //! @copydoc ::boost::intrusive::bstree::insert_unique(const_iterator,reference)
497 iterator insert_unique(const_iterator hint, reference value)
499 insert_commit_data commit_data;
500 std::pair<iterator, bool> ret = this->insert_unique_check
501 (hint, key_of_value()(value), this->key_comp(), commit_data);
504 return this->insert_unique_commit(value, commit_data);
507 //! @copydoc ::boost::intrusive::bstree::insert_unique_check(const KeyType&,KeyTypeKeyCompare,insert_commit_data&)
508 template<class KeyType, class KeyTypeKeyCompare>
509 BOOST_INTRUSIVE_DOC1ST(std::pair<iterator BOOST_INTRUSIVE_I bool>
510 , typename detail::disable_if_convertible
511 <KeyType BOOST_INTRUSIVE_I const_iterator BOOST_INTRUSIVE_I
512 std::pair<iterator BOOST_INTRUSIVE_I bool> >::type)
514 (const KeyType &key, KeyTypeKeyCompare comp, insert_commit_data &commit_data)
516 std::pair<node_ptr, bool> ret =
517 node_algorithms::insert_unique_check
518 (this->tree_type::header_ptr(), key, this->key_node_comp(comp), commit_data);
519 return std::pair<iterator, bool>(iterator(ret.first, this->priv_value_traits_ptr()), ret.second);
522 //! @copydoc ::boost::intrusive::bstree::insert_unique_check(const_iterator,const KeyType&,KeyTypeKeyCompare,insert_commit_data&)
523 template<class KeyType, class KeyTypeKeyCompare>
524 std::pair<iterator, bool> insert_unique_check
525 (const_iterator hint, const KeyType &key
526 ,KeyTypeKeyCompare comp, insert_commit_data &commit_data)
528 std::pair<node_ptr, bool> ret =
529 node_algorithms::insert_unique_check
530 (this->tree_type::header_ptr(), hint.pointed_node(), key, this->key_node_comp(comp), commit_data);
531 return std::pair<iterator, bool>(iterator(ret.first, this->priv_value_traits_ptr()), ret.second);
534 //! @copydoc ::boost::intrusive::bstree::insert_unique_check(const key_type&,insert_commit_data&)
535 std::pair<iterator, bool> insert_unique_check
536 (const key_type &key, insert_commit_data &commit_data)
537 { return this->insert_unique_check(key, this->key_comp(), commit_data); }
539 //! @copydoc ::boost::intrusive::bstree::insert_unique_check(const_iterator,const key_type&,insert_commit_data&)
540 std::pair<iterator, bool> insert_unique_check
541 (const_iterator hint, const key_type &key, insert_commit_data &commit_data)
542 { return this->insert_unique_check(hint, key, this->key_comp(), commit_data); }
544 //! @copydoc ::boost::intrusive::bstree::insert_unique_commit
545 iterator insert_unique_commit(reference value, const insert_commit_data &commit_data)
547 node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
548 if(safemode_or_autounlink)
549 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert));
550 std::size_t max_tree_size = (std::size_t)this->max_tree_size_;
551 node_algorithms::insert_unique_commit
552 ( this->tree_type::header_ptr(), to_insert, commit_data
553 , (std::size_t)this->size(), this->get_h_alpha_func(), max_tree_size);
554 this->tree_type::sz_traits().increment();
555 this->max_tree_size_ = (size_type)max_tree_size;
556 return iterator(to_insert, this->priv_value_traits_ptr());
559 //! @copydoc ::boost::intrusive::bstree::insert_unique(Iterator,Iterator)
560 template<class Iterator>
561 void insert_unique(Iterator b, Iterator e)
564 iterator iend(this->end());
566 this->insert_unique(iend, *b);
570 this->insert_unique(*b);
574 //! @copydoc ::boost::intrusive::bstree::insert_before
575 iterator insert_before(const_iterator pos, reference value)
577 node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
578 if(safemode_or_autounlink)
579 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert));
580 std::size_t max_tree_size = (std::size_t)this->max_tree_size_;
581 node_ptr p = node_algorithms::insert_before
582 ( this->tree_type::header_ptr(), pos.pointed_node(), to_insert
583 , (size_type)this->size(), this->get_h_alpha_func(), max_tree_size);
584 this->tree_type::sz_traits().increment();
585 this->max_tree_size_ = (size_type)max_tree_size;
586 return iterator(p, this->priv_value_traits_ptr());
589 //! @copydoc ::boost::intrusive::bstree::push_back
590 void push_back(reference value)
592 node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
593 if(safemode_or_autounlink)
594 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert));
595 std::size_t max_tree_size = (std::size_t)this->max_tree_size_;
596 node_algorithms::push_back
597 ( this->tree_type::header_ptr(), to_insert
598 , (size_type)this->size(), this->get_h_alpha_func(), max_tree_size);
599 this->tree_type::sz_traits().increment();
600 this->max_tree_size_ = (size_type)max_tree_size;
603 //! @copydoc ::boost::intrusive::bstree::push_front
604 void push_front(reference value)
606 node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
607 if(safemode_or_autounlink)
608 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert));
609 std::size_t max_tree_size = (std::size_t)this->max_tree_size_;
610 node_algorithms::push_front
611 ( this->tree_type::header_ptr(), to_insert
612 , (size_type)this->size(), this->get_h_alpha_func(), max_tree_size);
613 this->tree_type::sz_traits().increment();
614 this->max_tree_size_ = (size_type)max_tree_size;
618 //! @copydoc ::boost::intrusive::bstree::erase(const_iterator)
619 iterator erase(const_iterator i)
621 const_iterator ret(i);
623 node_ptr to_erase(i.pointed_node());
624 if(safemode_or_autounlink)
625 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!node_algorithms::unique(to_erase));
626 std::size_t max_tree_size = this->max_tree_size_;
627 node_algorithms::erase
628 ( this->tree_type::header_ptr(), to_erase, (std::size_t)this->size()
629 , max_tree_size, this->get_alpha_by_max_size_func());
630 this->max_tree_size_ = (size_type)max_tree_size;
631 this->tree_type::sz_traits().decrement();
632 if(safemode_or_autounlink)
633 node_algorithms::init(to_erase);
634 return ret.unconst();
637 //! @copydoc ::boost::intrusive::bstree::erase(const_iterator,const_iterator)
638 iterator erase(const_iterator b, const_iterator e)
639 { size_type n; return private_erase(b, e, n); }
641 //! @copydoc ::boost::intrusive::bstree::erase(const key_type &)
642 size_type erase(const key_type &key)
643 { return this->erase(key, this->key_comp()); }
645 //! @copydoc ::boost::intrusive::bstree::erase(const KeyType&,KeyTypeKeyCompare)
646 template<class KeyType, class KeyTypeKeyCompare>
647 BOOST_INTRUSIVE_DOC1ST(size_type
648 , typename detail::disable_if_convertible<KeyTypeKeyCompare BOOST_INTRUSIVE_I const_iterator BOOST_INTRUSIVE_I size_type>::type)
649 erase(const KeyType& key, KeyTypeKeyCompare comp)
651 std::pair<iterator,iterator> p = this->equal_range(key, comp);
653 private_erase(p.first, p.second, n);
657 //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const_iterator,Disposer)
658 template<class Disposer>
659 iterator erase_and_dispose(const_iterator i, Disposer disposer)
661 node_ptr to_erase(i.pointed_node());
662 iterator ret(this->erase(i));
663 disposer(this->get_value_traits().to_value_ptr(to_erase));
667 #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
668 template<class Disposer>
669 iterator erase_and_dispose(iterator i, Disposer disposer)
670 { return this->erase_and_dispose(const_iterator(i), disposer); }
673 //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const_iterator,const_iterator,Disposer)
674 template<class Disposer>
675 iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer)
676 { size_type n; return private_erase(b, e, n, disposer); }
678 //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const key_type &, Disposer)
679 template<class Disposer>
680 size_type erase_and_dispose(const key_type &key, Disposer disposer)
682 std::pair<iterator,iterator> p = this->equal_range(key);
684 private_erase(p.first, p.second, n, disposer);
688 //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const KeyType&,KeyTypeKeyCompare,Disposer)
689 template<class KeyType, class KeyTypeKeyCompare, class Disposer>
690 BOOST_INTRUSIVE_DOC1ST(size_type
691 , typename detail::disable_if_convertible<KeyTypeKeyCompare BOOST_INTRUSIVE_I const_iterator BOOST_INTRUSIVE_I size_type>::type)
692 erase_and_dispose(const KeyType& key, KeyTypeKeyCompare comp, Disposer disposer)
694 std::pair<iterator,iterator> p = this->equal_range(key, comp);
696 private_erase(p.first, p.second, n, disposer);
700 //! @copydoc ::boost::intrusive::bstree::clear
704 this->max_tree_size_ = 0;
707 //! @copydoc ::boost::intrusive::bstree::clear_and_dispose
708 template<class Disposer>
709 void clear_and_dispose(Disposer disposer)
711 tree_type::clear_and_dispose(disposer);
712 this->max_tree_size_ = 0;
715 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
716 //! @copydoc ::boost::intrusive::bstree::merge_unique
717 template<class T, class ...Options2> void merge_unique(sgtree<T, Options2...> &);
719 template<class Compare2>
720 void merge_unique(sgtree_impl
721 <ValueTraits, VoidOrKeyOfValue, Compare2, SizeType, FloatingPoint, HeaderHolder> &source)
724 node_ptr it (node_algorithms::begin_node(source.header_ptr()))
725 , itend(node_algorithms::end_node (source.header_ptr()));
728 node_ptr const p(it);
729 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || !node_algorithms::unique(p));
730 it = node_algorithms::next_node(it);
732 std::size_t max_tree1_size = this->max_tree_size_;
733 std::size_t max_tree2_size = source.get_max_tree_size();
734 if( node_algorithms::transfer_unique
735 ( this->header_ptr(), this->key_node_comp(this->key_comp()), this->size(), max_tree1_size
736 , source.header_ptr(), p, source.size(), max_tree2_size
737 , this->get_h_alpha_func(), this->get_alpha_by_max_size_func()) ){
738 this->max_tree_size_ = (size_type)max_tree1_size;
739 this->sz_traits().increment();
740 source.get_max_tree_size() = (size_type)max_tree2_size;
741 source.sz_traits().decrement();
746 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
747 //! @copydoc ::boost::intrusive::bstree::merge_equal
748 template<class T, class ...Options2> void merge_equal(sgtree<T, Options2...> &);
750 template<class Compare2>
751 void merge_equal(sgtree_impl
752 <ValueTraits, VoidOrKeyOfValue, Compare2, SizeType, FloatingPoint, HeaderHolder> &source)
755 node_ptr it (node_algorithms::begin_node(source.header_ptr()))
756 , itend(node_algorithms::end_node (source.header_ptr()));
759 node_ptr const p(it);
760 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || !node_algorithms::unique(p));
761 it = node_algorithms::next_node(it);
762 std::size_t max_tree1_size = this->max_tree_size_;
763 std::size_t max_tree2_size = source.get_max_tree_size();
764 node_algorithms::transfer_equal
765 ( this->header_ptr(), this->key_node_comp(this->key_comp()), this->size(), max_tree1_size
766 , source.header_ptr(), p, source.size(), max_tree2_size
767 , this->get_h_alpha_func(), this->get_alpha_by_max_size_func());
768 this->max_tree_size_ = (size_type)max_tree1_size;
769 this->sz_traits().increment();
770 source.get_max_tree_size() = (size_type)max_tree2_size;
771 source.sz_traits().decrement();
775 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
776 //! @copydoc ::boost::intrusive::bstree::count(const key_type &)const
777 size_type count(const key_type &key) const;
779 //! @copydoc ::boost::intrusive::bstree::count(const KeyType&,KeyTypeKeyCompare)const
780 template<class KeyType, class KeyTypeKeyCompare>
781 size_type count(const KeyType& key, KeyTypeKeyCompare comp) const;
783 //! @copydoc ::boost::intrusive::bstree::lower_bound(const key_type &)
784 iterator lower_bound(const key_type &key);
786 //! @copydoc ::boost::intrusive::bstree::lower_bound(const KeyType&,KeyTypeKeyCompare)
787 template<class KeyType, class KeyTypeKeyCompare>
788 iterator lower_bound(const KeyType& key, KeyTypeKeyCompare comp);
790 //! @copydoc ::boost::intrusive::bstree::lower_bound(const key_type &)const
791 const_iterator lower_bound(const key_type &key) const;
793 //! @copydoc ::boost::intrusive::bstree::lower_bound(const KeyType&,KeyTypeKeyCompare)const
794 template<class KeyType, class KeyTypeKeyCompare>
795 const_iterator lower_bound(const KeyType& key, KeyTypeKeyCompare comp) const;
797 //! @copydoc ::boost::intrusive::bstree::upper_bound(const key_type &)
798 iterator upper_bound(const key_type &key);
800 //! @copydoc ::boost::intrusive::bstree::upper_bound(const KeyType&,KeyTypeKeyCompare)
801 template<class KeyType, class KeyTypeKeyCompare>
802 iterator upper_bound(const KeyType& key, KeyTypeKeyCompare comp);
804 //! @copydoc ::boost::intrusive::bstree::upper_bound(const key_type &)const
805 const_iterator upper_bound(const key_type &key) const;
807 //! @copydoc ::boost::intrusive::bstree::upper_bound(const KeyType&,KeyTypeKeyCompare)const
808 template<class KeyType, class KeyTypeKeyCompare>
809 const_iterator upper_bound(const KeyType& key, KeyTypeKeyCompare comp) const;
811 //! @copydoc ::boost::intrusive::bstree::find(const key_type &)
812 iterator find(const key_type &key);
814 //! @copydoc ::boost::intrusive::bstree::find(const KeyType&,KeyTypeKeyCompare)
815 template<class KeyType, class KeyTypeKeyCompare>
816 iterator find(const KeyType& key, KeyTypeKeyCompare comp);
818 //! @copydoc ::boost::intrusive::bstree::find(const key_type &)const
819 const_iterator find(const key_type &key) const;
821 //! @copydoc ::boost::intrusive::bstree::find(const KeyType&,KeyTypeKeyCompare)const
822 template<class KeyType, class KeyTypeKeyCompare>
823 const_iterator find(const KeyType& key, KeyTypeKeyCompare comp) const;
825 //! @copydoc ::boost::intrusive::bstree::equal_range(const key_type &)
826 std::pair<iterator,iterator> equal_range(const key_type &key);
828 //! @copydoc ::boost::intrusive::bstree::equal_range(const KeyType&,KeyTypeKeyCompare)
829 template<class KeyType, class KeyTypeKeyCompare>
830 std::pair<iterator,iterator> equal_range(const KeyType& key, KeyTypeKeyCompare comp);
832 //! @copydoc ::boost::intrusive::bstree::equal_range(const key_type &)const
833 std::pair<const_iterator, const_iterator>
834 equal_range(const key_type &key) const;
836 //! @copydoc ::boost::intrusive::bstree::equal_range(const KeyType&,KeyTypeKeyCompare)const
837 template<class KeyType, class KeyTypeKeyCompare>
838 std::pair<const_iterator, const_iterator>
839 equal_range(const KeyType& key, KeyTypeKeyCompare comp) const;
841 //! @copydoc ::boost::intrusive::bstree::bounded_range(const key_type &,const key_type &,bool,bool)
842 std::pair<iterator,iterator> bounded_range
843 (const key_type &lower_key, const key_type &upper_key, bool left_closed, bool right_closed);
845 //! @copydoc ::boost::intrusive::bstree::bounded_range(const KeyType&,const KeyType&,KeyTypeKeyCompare,bool,bool)
846 template<class KeyType, class KeyTypeKeyCompare>
847 std::pair<iterator,iterator> bounded_range
848 (const KeyType& lower_key, const KeyType& upper_key, KeyTypeKeyCompare comp, bool left_closed, bool right_closed);
850 //! @copydoc ::boost::intrusive::bstree::bounded_range(const key_type &,const key_type &,bool,bool)const
851 std::pair<const_iterator, const_iterator>
852 bounded_range(const key_type &lower_key, const key_type &upper_key, bool left_closed, bool right_closed) const;
854 //! @copydoc ::boost::intrusive::bstree::bounded_range(const KeyType&,const KeyType&,KeyTypeKeyCompare,bool,bool)const
855 template<class KeyType, class KeyTypeKeyCompare>
856 std::pair<const_iterator, const_iterator> bounded_range
857 (const KeyType& lower_key, const KeyType& upper_key, KeyTypeKeyCompare comp, bool left_closed, bool right_closed) const;
859 //! @copydoc ::boost::intrusive::bstree::s_iterator_to(reference)
860 static iterator s_iterator_to(reference value);
862 //! @copydoc ::boost::intrusive::bstree::s_iterator_to(const_reference)
863 static const_iterator s_iterator_to(const_reference value);
865 //! @copydoc ::boost::intrusive::bstree::iterator_to(reference)
866 iterator iterator_to(reference value);
868 //! @copydoc ::boost::intrusive::bstree::iterator_to(const_reference)const
869 const_iterator iterator_to(const_reference value) const;
871 //! @copydoc ::boost::intrusive::bstree::init_node(reference)
872 static void init_node(reference value);
874 //! @copydoc ::boost::intrusive::bstree::unlink_leftmost_without_rebalance
875 pointer unlink_leftmost_without_rebalance();
877 //! @copydoc ::boost::intrusive::bstree::replace_node
878 void replace_node(iterator replace_this, reference with_this);
880 //! @copydoc ::boost::intrusive::bstree::remove_node
881 void remove_node(reference value);
883 //! @copydoc ::boost::intrusive::bstree::rebalance
886 //! @copydoc ::boost::intrusive::bstree::rebalance_subtree
887 iterator rebalance_subtree(iterator root);
889 friend bool operator< (const sgtree_impl &x, const sgtree_impl &y);
891 friend bool operator==(const sgtree_impl &x, const sgtree_impl &y);
893 friend bool operator!= (const sgtree_impl &x, const sgtree_impl &y);
895 friend bool operator>(const sgtree_impl &x, const sgtree_impl &y);
897 friend bool operator<=(const sgtree_impl &x, const sgtree_impl &y);
899 friend bool operator>=(const sgtree_impl &x, const sgtree_impl &y);
901 friend void swap(sgtree_impl &x, sgtree_impl &y);
903 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
905 //! <b>Returns</b>: The balance factor (alpha) used in this tree
907 //! <b>Throws</b>: Nothing.
909 //! <b>Complexity</b>: Constant.
910 float balance_factor() const
911 { return this->get_alpha_traits().get_alpha(); }
913 //! <b>Requires</b>: new_alpha must be a value between 0.5 and 1.0
915 //! <b>Effects</b>: Establishes a new balance factor (alpha) and rebalances
916 //! the tree if the new balance factor is stricter (less) than the old factor.
918 //! <b>Throws</b>: Nothing.
920 //! <b>Complexity</b>: Linear to the elements in the subtree.
921 void balance_factor(float new_alpha)
923 //The alpha factor CAN't be changed if the fixed, floating operation-less
924 //1/sqrt(2) alpha factor option is activated
925 BOOST_STATIC_ASSERT((floating_point));
926 BOOST_INTRUSIVE_INVARIANT_ASSERT((new_alpha > 0.5f && new_alpha < 1.0f));
927 if(new_alpha >= 0.5f && new_alpha < 1.0f){
928 float old_alpha = this->get_alpha_traits().get_alpha();
929 this->get_alpha_traits().set_alpha(new_alpha);
930 if(new_alpha < old_alpha){
931 this->max_tree_size_ = this->size();
939 template<class Disposer>
940 iterator private_erase(const_iterator b, const_iterator e, size_type &n, Disposer disposer)
942 for(n = 0; b != e; ++n)
943 this->erase_and_dispose(b++, disposer);
947 iterator private_erase(const_iterator b, const_iterator e, size_type &n)
949 for(n = 0; b != e; ++n)
957 //! Helper metafunction to define a \c sgtree that yields to the same type when the
958 //! same options (either explicitly or implicitly) are used.
959 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
960 template<class T, class ...Options>
962 template<class T, class O1 = void, class O2 = void
963 , class O3 = void, class O4 = void
964 , class O5 = void, class O6 = void>
969 typedef typename pack_options
971 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
972 O1, O2, O3, O4, O5, O6
976 >::type packed_options;
978 typedef typename detail::get_value_traits
979 <T, typename packed_options::proto_value_traits>::type value_traits;
983 , typename packed_options::key_of_value
984 , typename packed_options::compare
985 , typename packed_options::size_type
986 , packed_options::floating_point
987 , typename packed_options::header_holder_type
988 > implementation_defined;
990 typedef implementation_defined type;
994 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
996 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
997 template<class T, class O1, class O2, class O3, class O4, class O5, class O6>
999 template<class T, class ...Options>
1002 : public make_sgtree<T,
1003 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
1004 O1, O2, O3, O4, O5, O6
1010 typedef typename make_sgtree
1012 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
1013 O1, O2, O3, O4, O5, O6
1018 BOOST_MOVABLE_BUT_NOT_COPYABLE(sgtree)
1021 typedef typename Base::key_compare key_compare;
1022 typedef typename Base::value_traits value_traits;
1023 typedef typename Base::iterator iterator;
1024 typedef typename Base::const_iterator const_iterator;
1025 typedef typename Base::reverse_iterator reverse_iterator;
1026 typedef typename Base::const_reverse_iterator const_reverse_iterator;
1028 //Assert if passed value traits are compatible with the type
1029 BOOST_STATIC_ASSERT((detail::is_same<typename value_traits::value_type, T>::value));
1035 explicit sgtree(const key_compare &cmp, const value_traits &v_traits = value_traits())
1036 : Base(cmp, v_traits)
1039 template<class Iterator>
1040 sgtree( bool unique, Iterator b, Iterator e
1041 , const key_compare &cmp = key_compare()
1042 , const value_traits &v_traits = value_traits())
1043 : Base(unique, b, e, cmp, v_traits)
1046 sgtree(BOOST_RV_REF(sgtree) x)
1047 : Base(BOOST_MOVE_BASE(Base, x))
1050 sgtree& operator=(BOOST_RV_REF(sgtree) x)
1051 { return static_cast<sgtree &>(this->Base::operator=(BOOST_MOVE_BASE(Base, x))); }
1053 template <class Cloner, class Disposer>
1054 void clone_from(const sgtree &src, Cloner cloner, Disposer disposer)
1055 { Base::clone_from(src, cloner, disposer); }
1057 template <class Cloner, class Disposer>
1058 void clone_from(BOOST_RV_REF(sgtree) src, Cloner cloner, Disposer disposer)
1059 { Base::clone_from(BOOST_MOVE_BASE(Base, src), cloner, disposer); }
1061 static sgtree &container_from_end_iterator(iterator end_iterator)
1062 { return static_cast<sgtree &>(Base::container_from_end_iterator(end_iterator)); }
1064 static const sgtree &container_from_end_iterator(const_iterator end_iterator)
1065 { return static_cast<const sgtree &>(Base::container_from_end_iterator(end_iterator)); }
1067 static sgtree &container_from_iterator(iterator it)
1068 { return static_cast<sgtree &>(Base::container_from_iterator(it)); }
1070 static const sgtree &container_from_iterator(const_iterator it)
1071 { return static_cast<const sgtree &>(Base::container_from_iterator(it)); }
1076 } //namespace intrusive
1079 #include <boost/intrusive/detail/config_end.hpp>
1081 #endif //BOOST_INTRUSIVE_SGTREE_HPP