1 ////////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/container for documentation.
9 ////////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_CONTAINER_FLAT_TREE_HPP
12 #define BOOST_CONTAINER_FLAT_TREE_HPP
18 #include <boost/container/detail/config_begin.hpp>
19 #include <boost/container/detail/workaround.hpp>
21 #include <boost/container/container_fwd.hpp>
27 #include <boost/type_traits/has_trivial_destructor.hpp>
28 #include <boost/move/utility_core.hpp>
30 #include <boost/container/detail/utilities.hpp>
31 #include <boost/container/detail/pair.hpp>
32 #include <boost/container/vector.hpp>
33 #include <boost/container/detail/value_init.hpp>
34 #include <boost/container/detail/destroyers.hpp>
35 #include <boost/container/allocator_traits.hpp>
36 #ifdef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
37 #include <boost/intrusive/pointer_traits.hpp>
39 #include <boost/aligned_storage.hpp>
40 #include <boost/move/make_unique.hpp>
46 namespace container_detail {
48 template<class Compare, class Value, class KeyOfValue>
49 class flat_tree_value_compare
52 typedef Value first_argument_type;
53 typedef Value second_argument_type;
54 typedef bool return_type;
56 flat_tree_value_compare()
60 flat_tree_value_compare(const Compare &pred)
64 bool operator()(const Value& lhs, const Value& rhs) const
66 KeyOfValue key_extract;
67 return Compare::operator()(key_extract(lhs), key_extract(rhs));
70 const Compare &get_comp() const
77 template<class Pointer>
78 struct get_flat_tree_iterators
80 #ifdef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
81 typedef Pointer iterator;
82 typedef typename boost::intrusive::
83 pointer_traits<Pointer>::element_type iterator_element_type;
84 typedef typename boost::intrusive::
85 pointer_traits<Pointer>:: template
86 rebind_pointer<const iterator_element_type>::type const_iterator;
87 #else //BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
88 typedef typename boost::container::container_detail::
89 vec_iterator<Pointer, false> iterator;
90 typedef typename boost::container::container_detail::
91 vec_iterator<Pointer, true > const_iterator;
92 #endif //BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
93 typedef boost::container::container_detail::
94 reverse_iterator<iterator> reverse_iterator;
95 typedef boost::container::container_detail::
96 reverse_iterator<const_iterator> const_reverse_iterator;
99 template <class Key, class Value, class KeyOfValue,
100 class Compare, class A>
103 typedef boost::container::vector<Value, A> vector_t;
104 typedef A allocator_t;
107 typedef flat_tree_value_compare<Compare, Value, KeyOfValue> value_compare;
111 //Inherit from value_compare to do EBO
112 : public value_compare
114 BOOST_COPYABLE_AND_MOVABLE(Data)
118 : value_compare(), m_vect()
121 explicit Data(const Data &d)
122 : value_compare(static_cast<const value_compare&>(d)), m_vect(d.m_vect)
125 Data(BOOST_RV_REF(Data) d)
126 : value_compare(boost::move(static_cast<value_compare&>(d))), m_vect(boost::move(d.m_vect))
129 Data(const Data &d, const A &a)
130 : value_compare(static_cast<const value_compare&>(d)), m_vect(d.m_vect, a)
133 Data(BOOST_RV_REF(Data) d, const A &a)
134 : value_compare(boost::move(static_cast<value_compare&>(d))), m_vect(boost::move(d.m_vect), a)
137 explicit Data(const Compare &comp)
138 : value_compare(comp), m_vect()
141 Data(const Compare &comp, const allocator_t &alloc)
142 : value_compare(comp), m_vect(alloc)
145 explicit Data(const allocator_t &alloc)
146 : value_compare(), m_vect(alloc)
149 Data& operator=(BOOST_COPY_ASSIGN_REF(Data) d)
151 this->value_compare::operator=(d);
156 Data& operator=(BOOST_RV_REF(Data) d)
158 this->value_compare::operator=(boost::move(static_cast<value_compare &>(d)));
159 m_vect = boost::move(d.m_vect);
165 value_compare& mycomp = *this, & othercomp = d;
166 boost::container::swap_dispatch(mycomp, othercomp);
167 this->m_vect.swap(d.m_vect);
174 BOOST_COPYABLE_AND_MOVABLE(flat_tree)
178 typedef typename vector_t::value_type value_type;
179 typedef typename vector_t::pointer pointer;
180 typedef typename vector_t::const_pointer const_pointer;
181 typedef typename vector_t::reference reference;
182 typedef typename vector_t::const_reference const_reference;
183 typedef Key key_type;
184 typedef Compare key_compare;
185 typedef typename vector_t::allocator_type allocator_type;
186 typedef typename vector_t::size_type size_type;
187 typedef typename vector_t::difference_type difference_type;
188 typedef typename vector_t::iterator iterator;
189 typedef typename vector_t::const_iterator const_iterator;
190 typedef typename vector_t::reverse_iterator reverse_iterator;
191 typedef typename vector_t::const_reverse_iterator const_reverse_iterator;
193 //!Standard extension
194 typedef allocator_type stored_allocator_type;
197 typedef allocator_traits<stored_allocator_type> stored_allocator_traits;
204 explicit flat_tree(const Compare& comp)
208 flat_tree(const Compare& comp, const allocator_type& a)
212 explicit flat_tree(const allocator_type& a)
216 flat_tree(const flat_tree& x)
220 flat_tree(BOOST_RV_REF(flat_tree) x)
221 : m_data(boost::move(x.m_data))
224 flat_tree(const flat_tree& x, const allocator_type &a)
225 : m_data(x.m_data, a)
228 flat_tree(BOOST_RV_REF(flat_tree) x, const allocator_type &a)
229 : m_data(boost::move(x.m_data), a)
232 template <class InputIterator>
233 flat_tree( ordered_range_t, InputIterator first, InputIterator last
234 , const Compare& comp = Compare()
235 , const allocator_type& a = allocator_type())
237 { this->m_data.m_vect.insert(this->m_data.m_vect.end(), first, last); }
239 template <class InputIterator>
240 flat_tree( bool unique_insertion
241 , InputIterator first, InputIterator last
242 , const Compare& comp = Compare()
243 , const allocator_type& a = allocator_type())
246 //Use cend() as hint to achieve linear time for
247 //ordered ranges as required by the standard
248 //for the constructor
249 //Call end() every iteration as reallocation might have invalidated iterators
250 if(unique_insertion){
251 for ( ; first != last; ++first){
252 this->insert_unique(this->cend(), *first);
256 for ( ; first != last; ++first){
257 this->insert_equal(this->cend(), *first);
265 flat_tree& operator=(BOOST_COPY_ASSIGN_REF(flat_tree) x)
266 { m_data = x.m_data; return *this; }
268 flat_tree& operator=(BOOST_RV_REF(flat_tree) mx)
269 { m_data = boost::move(mx.m_data); return *this; }
273 Compare key_comp() const
274 { return this->m_data.get_comp(); }
276 value_compare value_comp() const
277 { return this->m_data; }
279 allocator_type get_allocator() const
280 { return this->m_data.m_vect.get_allocator(); }
282 const stored_allocator_type &get_stored_allocator() const
283 { return this->m_data.m_vect.get_stored_allocator(); }
285 stored_allocator_type &get_stored_allocator()
286 { return this->m_data.m_vect.get_stored_allocator(); }
289 { return this->m_data.m_vect.begin(); }
291 const_iterator begin() const
292 { return this->cbegin(); }
294 const_iterator cbegin() const
295 { return this->m_data.m_vect.begin(); }
298 { return this->m_data.m_vect.end(); }
300 const_iterator end() const
301 { return this->cend(); }
303 const_iterator cend() const
304 { return this->m_data.m_vect.end(); }
306 reverse_iterator rbegin()
307 { return reverse_iterator(this->end()); }
309 const_reverse_iterator rbegin() const
310 { return this->crbegin(); }
312 const_reverse_iterator crbegin() const
313 { return const_reverse_iterator(this->cend()); }
315 reverse_iterator rend()
316 { return reverse_iterator(this->begin()); }
318 const_reverse_iterator rend() const
319 { return this->crend(); }
321 const_reverse_iterator crend() const
322 { return const_reverse_iterator(this->cbegin()); }
325 { return this->m_data.m_vect.empty(); }
327 size_type size() const
328 { return this->m_data.m_vect.size(); }
330 size_type max_size() const
331 { return this->m_data.m_vect.max_size(); }
333 void swap(flat_tree& other)
334 { this->m_data.swap(other.m_data); }
338 std::pair<iterator,bool> insert_unique(const value_type& val)
340 std::pair<iterator,bool> ret;
341 insert_commit_data data;
342 ret.second = this->priv_insert_unique_prepare(val, data);
343 ret.first = ret.second ? this->priv_insert_commit(data, val)
344 : iterator(vector_iterator_get_ptr(data.position));
348 std::pair<iterator,bool> insert_unique(BOOST_RV_REF(value_type) val)
350 std::pair<iterator,bool> ret;
351 insert_commit_data data;
352 ret.second = this->priv_insert_unique_prepare(val, data);
353 ret.first = ret.second ? this->priv_insert_commit(data, boost::move(val))
354 : iterator(vector_iterator_get_ptr(data.position));
358 iterator insert_equal(const value_type& val)
360 iterator i = this->upper_bound(KeyOfValue()(val));
361 i = this->m_data.m_vect.insert(i, val);
365 iterator insert_equal(BOOST_RV_REF(value_type) mval)
367 iterator i = this->upper_bound(KeyOfValue()(mval));
368 i = this->m_data.m_vect.insert(i, boost::move(mval));
372 iterator insert_unique(const_iterator pos, const value_type& val)
374 std::pair<iterator,bool> ret;
375 insert_commit_data data;
376 return this->priv_insert_unique_prepare(pos, val, data)
377 ? this->priv_insert_commit(data, val)
378 : iterator(vector_iterator_get_ptr(data.position));
381 iterator insert_unique(const_iterator pos, BOOST_RV_REF(value_type) val)
383 std::pair<iterator,bool> ret;
384 insert_commit_data data;
385 return this->priv_insert_unique_prepare(pos, val, data)
386 ? this->priv_insert_commit(data, boost::move(val))
387 : iterator(vector_iterator_get_ptr(data.position));
390 iterator insert_equal(const_iterator pos, const value_type& val)
392 insert_commit_data data;
393 this->priv_insert_equal_prepare(pos, val, data);
394 return this->priv_insert_commit(data, val);
397 iterator insert_equal(const_iterator pos, BOOST_RV_REF(value_type) mval)
399 insert_commit_data data;
400 this->priv_insert_equal_prepare(pos, mval, data);
401 return this->priv_insert_commit(data, boost::move(mval));
404 template <class InIt>
405 void insert_unique(InIt first, InIt last)
407 for ( ; first != last; ++first){
408 this->insert_unique(*first);
412 template <class InIt>
413 void insert_equal(InIt first, InIt last
414 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
415 , typename container_detail::enable_if_c
416 < container_detail::is_input_iterator<InIt>::value
420 { this->priv_insert_equal_loop(first, last); }
422 template <class InIt>
423 void insert_equal(InIt first, InIt last
424 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
425 , typename container_detail::enable_if_c
426 < !container_detail::is_input_iterator<InIt>::value
431 const size_type len = static_cast<size_type>(std::distance(first, last));
432 this->reserve(this->size()+len);
433 this->priv_insert_equal_loop(first, last);
438 template <class InIt>
439 void insert_equal(ordered_range_t, InIt first, InIt last
440 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
441 , typename container_detail::enable_if_c
442 < container_detail::is_input_iterator<InIt>::value
446 { this->priv_insert_equal_loop_ordered(first, last); }
448 template <class FwdIt>
449 void insert_equal(ordered_range_t, FwdIt first, FwdIt last
450 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
451 , typename container_detail::enable_if_c
452 < !container_detail::is_input_iterator<FwdIt>::value &&
453 container_detail::is_forward_iterator<FwdIt>::value
458 const size_type len = static_cast<size_type>(std::distance(first, last));
459 this->reserve(this->size()+len);
460 this->priv_insert_equal_loop_ordered(first, last);
463 template <class BidirIt>
464 void insert_equal(ordered_range_t, BidirIt first, BidirIt last
465 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
466 , typename container_detail::enable_if_c
467 < !container_detail::is_input_iterator<BidirIt>::value &&
468 !container_detail::is_forward_iterator<BidirIt>::value
472 { this->priv_insert_ordered_range(false, first, last); }
474 template <class InIt>
475 void insert_unique(ordered_unique_range_t, InIt first, InIt last
476 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
477 , typename container_detail::enable_if_c
478 < container_detail::is_input_iterator<InIt>::value ||
479 container_detail::is_forward_iterator<InIt>::value
484 const_iterator pos(this->cend());
485 for ( ; first != last; ++first){
486 pos = this->insert_unique(pos, *first);
491 template <class BidirIt>
492 void insert_unique(ordered_unique_range_t, BidirIt first, BidirIt last
493 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
494 , typename container_detail::enable_if_c
495 < !(container_detail::is_input_iterator<BidirIt>::value ||
496 container_detail::is_forward_iterator<BidirIt>::value)
500 { this->priv_insert_ordered_range(true, first, last); }
502 #ifdef BOOST_CONTAINER_PERFECT_FORWARDING
504 template <class... Args>
505 std::pair<iterator, bool> emplace_unique(Args&&... args)
507 aligned_storage<sizeof(value_type), alignment_of<value_type>::value> v;
508 value_type &val = *static_cast<value_type *>(static_cast<void *>(&v));
509 stored_allocator_type &a = this->get_stored_allocator();
510 stored_allocator_traits::construct(a, &val, ::boost::forward<Args>(args)... );
511 value_destructor<stored_allocator_type> d(a, val);
512 return this->insert_unique(::boost::move(val));
515 template <class... Args>
516 iterator emplace_hint_unique(const_iterator hint, Args&&... args)
518 aligned_storage<sizeof(value_type), alignment_of<value_type>::value> v;
519 value_type &val = *static_cast<value_type *>(static_cast<void *>(&v));
520 stored_allocator_type &a = this->get_stored_allocator();
521 stored_allocator_traits::construct(a, &val, ::boost::forward<Args>(args)... );
522 value_destructor<stored_allocator_type> d(a, val);
523 return this->insert_unique(hint, ::boost::move(val));
526 template <class... Args>
527 iterator emplace_equal(Args&&... args)
529 aligned_storage<sizeof(value_type), alignment_of<value_type>::value> v;
530 value_type &val = *static_cast<value_type *>(static_cast<void *>(&v));
531 stored_allocator_type &a = this->get_stored_allocator();
532 stored_allocator_traits::construct(a, &val, ::boost::forward<Args>(args)... );
533 value_destructor<stored_allocator_type> d(a, val);
534 return this->insert_equal(::boost::move(val));
537 template <class... Args>
538 iterator emplace_hint_equal(const_iterator hint, Args&&... args)
540 aligned_storage<sizeof(value_type), alignment_of<value_type>::value> v;
541 value_type &val = *static_cast<value_type *>(static_cast<void *>(&v));
542 stored_allocator_type &a = this->get_stored_allocator();
543 stored_allocator_traits::construct(a, &val, ::boost::forward<Args>(args)... );
544 value_destructor<stored_allocator_type> d(a, val);
545 return this->insert_equal(hint, ::boost::move(val));
548 #else //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING
550 #define BOOST_PP_LOCAL_MACRO(n) \
551 BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \
552 std::pair<iterator, bool> \
553 emplace_unique(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \
555 aligned_storage<sizeof(value_type), alignment_of<value_type>::value> v; \
556 value_type &val = *static_cast<value_type *>(static_cast<void *>(&v)); \
557 stored_allocator_type &a = this->get_stored_allocator(); \
558 stored_allocator_traits::construct(a, &val \
559 BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _) ); \
560 value_destructor<stored_allocator_type> d(a, val); \
561 return this->insert_unique(::boost::move(val)); \
564 BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \
565 iterator emplace_hint_unique(const_iterator hint \
566 BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \
568 aligned_storage<sizeof(value_type), alignment_of<value_type>::value> v; \
569 value_type &val = *static_cast<value_type *>(static_cast<void *>(&v)); \
570 stored_allocator_type &a = this->get_stored_allocator(); \
571 stored_allocator_traits::construct(a, &val \
572 BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _) ); \
573 value_destructor<stored_allocator_type> d(a, val); \
574 return this->insert_unique(hint, ::boost::move(val)); \
577 BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \
578 iterator emplace_equal(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \
580 aligned_storage<sizeof(value_type), alignment_of<value_type>::value> v; \
581 value_type &val = *static_cast<value_type *>(static_cast<void *>(&v)); \
582 stored_allocator_type &a = this->get_stored_allocator(); \
583 stored_allocator_traits::construct(a, &val \
584 BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _) ); \
585 value_destructor<stored_allocator_type> d(a, val); \
586 return this->insert_equal(::boost::move(val)); \
589 BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \
590 iterator emplace_hint_equal(const_iterator hint \
591 BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \
593 aligned_storage<sizeof(value_type), alignment_of<value_type>::value> v; \
594 value_type &val = *static_cast<value_type *>(static_cast<void *>(&v)); \
595 stored_allocator_type &a = this->get_stored_allocator(); \
596 stored_allocator_traits::construct(a, &val \
597 BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _) ); \
598 value_destructor<stored_allocator_type> d(a, val); \
599 return this->insert_equal(hint, ::boost::move(val)); \
602 #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS)
603 #include BOOST_PP_LOCAL_ITERATE()
605 #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING
607 iterator erase(const_iterator position)
608 { return this->m_data.m_vect.erase(position); }
610 size_type erase(const key_type& k)
612 std::pair<iterator,iterator > itp = this->equal_range(k);
613 size_type ret = static_cast<size_type>(itp.second-itp.first);
615 this->m_data.m_vect.erase(itp.first, itp.second);
620 iterator erase(const_iterator first, const_iterator last)
621 { return this->m_data.m_vect.erase(first, last); }
624 { this->m_data.m_vect.clear(); }
626 //! <b>Effects</b>: Tries to deallocate the excess of memory created
627 // with previous allocations. The size of the vector is unchanged
629 //! <b>Throws</b>: If memory allocation throws, or T's copy constructor throws.
631 //! <b>Complexity</b>: Linear to size().
633 { this->m_data.m_vect.shrink_to_fit(); }
636 iterator find(const key_type& k)
638 iterator i = this->lower_bound(k);
639 iterator end_it = this->end();
640 if (i != end_it && this->m_data.get_comp()(k, KeyOfValue()(*i))){
646 const_iterator find(const key_type& k) const
648 const_iterator i = this->lower_bound(k);
650 const_iterator end_it = this->cend();
651 if (i != end_it && this->m_data.get_comp()(k, KeyOfValue()(*i))){
658 size_type count(const key_type& k) const
660 std::pair<const_iterator, const_iterator> p = this->equal_range(k);
661 size_type n = p.second - p.first;
665 iterator lower_bound(const key_type& k)
666 { return this->priv_lower_bound(this->begin(), this->end(), k); }
668 const_iterator lower_bound(const key_type& k) const
669 { return this->priv_lower_bound(this->cbegin(), this->cend(), k); }
671 iterator upper_bound(const key_type& k)
672 { return this->priv_upper_bound(this->begin(), this->end(), k); }
674 const_iterator upper_bound(const key_type& k) const
675 { return this->priv_upper_bound(this->cbegin(), this->cend(), k); }
677 std::pair<iterator,iterator> equal_range(const key_type& k)
678 { return this->priv_equal_range(this->begin(), this->end(), k); }
680 std::pair<const_iterator, const_iterator> equal_range(const key_type& k) const
681 { return this->priv_equal_range(this->cbegin(), this->cend(), k); }
683 std::pair<iterator, iterator> lower_bound_range(const key_type& k)
684 { return this->priv_lower_bound_range(this->begin(), this->end(), k); }
686 std::pair<const_iterator, const_iterator> lower_bound_range(const key_type& k) const
687 { return this->priv_lower_bound_range(this->cbegin(), this->cend(), k); }
689 size_type capacity() const
690 { return this->m_data.m_vect.capacity(); }
692 void reserve(size_type cnt)
693 { this->m_data.m_vect.reserve(cnt); }
695 friend bool operator==(const flat_tree& x, const flat_tree& y)
697 return x.size() == y.size() && std::equal(x.begin(), x.end(), y.begin());
700 friend bool operator<(const flat_tree& x, const flat_tree& y)
702 return std::lexicographical_compare(x.begin(), x.end(),
706 friend bool operator!=(const flat_tree& x, const flat_tree& y)
707 { return !(x == y); }
709 friend bool operator>(const flat_tree& x, const flat_tree& y)
712 friend bool operator<=(const flat_tree& x, const flat_tree& y)
715 friend bool operator>=(const flat_tree& x, const flat_tree& y)
718 friend void swap(flat_tree& x, flat_tree& y)
722 struct insert_commit_data
724 const_iterator position;
728 void priv_insert_equal_prepare
729 (const_iterator pos, const value_type& val, insert_commit_data &data)
732 // To insert val at pos:
733 // if pos == end || val <= *pos
734 // if pos == begin || val >= *(pos-1)
735 // insert val before pos
737 // insert val before upper_bound(val)
739 // insert val before lower_bound(val)
740 const value_compare &val_cmp = this->m_data;
742 if(pos == this->cend() || !val_cmp(*pos, val)){
743 if (pos == this->cbegin() || !val_cmp(val, pos[-1])){
748 this->priv_upper_bound(this->cbegin(), pos, KeyOfValue()(val));
753 this->priv_lower_bound(pos, this->cend(), KeyOfValue()(val));
757 bool priv_insert_unique_prepare
758 (const_iterator b, const_iterator e, const value_type& val, insert_commit_data &commit_data)
760 const value_compare &val_cmp = this->m_data;
761 commit_data.position = this->priv_lower_bound(b, e, KeyOfValue()(val));
762 return commit_data.position == e || val_cmp(val, *commit_data.position);
765 bool priv_insert_unique_prepare
766 (const value_type& val, insert_commit_data &commit_data)
767 { return this->priv_insert_unique_prepare(this->cbegin(), this->cend(), val, commit_data); }
769 bool priv_insert_unique_prepare
770 (const_iterator pos, const value_type& val, insert_commit_data &commit_data)
772 //N1780. Props to Howard Hinnant!
773 //To insert val at pos:
774 //if pos == end || val <= *pos
775 // if pos == begin || val >= *(pos-1)
776 // insert val before pos
778 // insert val before upper_bound(val)
779 //else if pos+1 == end || val <= *(pos+1)
780 // insert val after pos
782 // insert val before lower_bound(val)
783 const value_compare &val_cmp = this->m_data;
784 const const_iterator cend_it = this->cend();
785 if(pos == cend_it || val_cmp(val, *pos)){ //Check if val should go before end
786 const const_iterator cbeg = this->cbegin();
787 commit_data.position = pos;
788 if(pos == cbeg){ //If container is empty then insert it in the beginning
791 const_iterator prev(pos);
793 if(val_cmp(*prev, val)){ //If previous element was less, then it should go between prev and pos
796 else if(!val_cmp(val, *prev)){ //If previous was equal then insertion should fail
797 commit_data.position = prev;
800 else{ //Previous was bigger so insertion hint was pointless, dispatch to hintless insertion
801 //but reduce the search between beg and prev as prev is bigger than val
802 return this->priv_insert_unique_prepare(cbeg, prev, val, commit_data);
806 //The hint is before the insertion position, so insert it
807 //in the remaining range [pos, end)
808 return this->priv_insert_unique_prepare(pos, cend_it, val, commit_data);
812 template<class Convertible>
813 iterator priv_insert_commit
814 (insert_commit_data &commit_data, BOOST_FWD_REF(Convertible) convertible)
816 return this->m_data.m_vect.insert
817 ( commit_data.position
818 , boost::forward<Convertible>(convertible));
821 template <class RanIt>
822 RanIt priv_lower_bound(RanIt first, const RanIt last,
823 const key_type & key) const
825 const Compare &key_cmp = this->m_data.get_comp();
826 KeyOfValue key_extract;
827 size_type len = static_cast<size_type>(last - first);
831 size_type step = len >> 1;
835 if (key_cmp(key_extract(*middle), key)) {
846 template <class RanIt>
847 RanIt priv_upper_bound(RanIt first, const RanIt last,
848 const key_type & key) const
850 const Compare &key_cmp = this->m_data.get_comp();
851 KeyOfValue key_extract;
852 size_type len = static_cast<size_type>(last - first);
856 size_type step = len >> 1;
860 if (key_cmp(key, key_extract(*middle))) {
871 template <class RanIt>
872 std::pair<RanIt, RanIt>
873 priv_equal_range(RanIt first, RanIt last, const key_type& key) const
875 const Compare &key_cmp = this->m_data.get_comp();
876 KeyOfValue key_extract;
877 size_type len = static_cast<size_type>(last - first);
881 size_type step = len >> 1;
885 if (key_cmp(key_extract(*middle), key)){
889 else if (key_cmp(key, key_extract(*middle))){
893 //Middle is equal to key
896 return std::pair<RanIt, RanIt>
897 ( this->priv_lower_bound(first, middle, key)
898 , this->priv_upper_bound(++middle, last, key));
901 return std::pair<RanIt, RanIt>(first, first);
904 template<class RanIt>
905 std::pair<RanIt, RanIt> priv_lower_bound_range(RanIt first, RanIt last, const key_type& k) const
907 const Compare &key_cmp = this->m_data.get_comp();
908 KeyOfValue key_extract;
909 RanIt lb(this->priv_lower_bound(first, last, k)), ub(lb);
910 if(lb != last && static_cast<difference_type>(!key_cmp(k, key_extract(*lb)))){
913 return std::pair<RanIt, RanIt>(lb, ub);
917 void priv_insert_equal_loop(InIt first, InIt last)
919 for ( ; first != last; ++first){
920 this->insert_equal(*first);
925 void priv_insert_equal_loop_ordered(InIt first, InIt last)
927 const_iterator pos(this->cend());
928 for ( ; first != last; ++first){
929 //If ordered, then try hint version
930 //to achieve constant-time complexity per insertion
931 pos = this->insert_equal(pos, *first);
936 template <class BidirIt>
937 void priv_insert_ordered_range(const bool unique_values, BidirIt first, BidirIt last)
939 size_type len = static_cast<size_type>(std::distance(first, last));
940 //Prereserve all memory so that iterators are not invalidated
941 this->reserve(this->size()+len);
942 //Auxiliary data for insertion positions.
943 const size_type BurstSize = len;
944 const ::boost::movelib::unique_ptr<size_type[]> positions =
945 ::boost::movelib::make_unique_definit<size_type[]>(BurstSize);
947 const const_iterator b(this->cbegin());
948 const const_iterator ce(this->cend());
949 const_iterator pos(b);
950 const value_compare &val_cmp = this->m_data;
951 //Loop in burst sizes
952 bool back_insert = false;
953 while(len && !back_insert){
954 const size_type burst = len < BurstSize ? len : BurstSize;
955 size_type unique_burst = 0u;
956 size_type checked = 0;
957 for(; checked != burst; ++checked){
958 //Get the insertion position for each key, use std::iterator_traits<BidirIt>::value_type
959 //because it can be different from container::value_type
960 //(e.g. conversion between std::pair<A, B> -> boost::container::pair<A, B>
961 const typename std::iterator_traits<BidirIt>::value_type & val = *first;
962 pos = const_cast<const flat_tree&>(*this).priv_lower_bound(pos, ce, KeyOfValue()(val));
963 //Check if already present
967 positions[checked] = (unique_values && !val_cmp(val, *pos)) ?
968 size_type(-1) : (++unique_burst, static_cast<size_type>(pos - b));
970 else{ //this element and the remaining should be back inserted
976 //Insert all in a single step in the precalculated positions
977 this->m_data.m_vect.insert_ordered_at(unique_burst, positions.get() + checked, first);
978 //Next search position updated, iterator still valid because we've preserved the vector
982 //The remaining range should be back inserted
987 if(next == last || val_cmp(*first, *next)){
988 const bool room = this->m_data.m_vect.stable_emplace_back(*first);
994 BOOST_ASSERT(first == last);
997 BOOST_ASSERT(size_type(std::distance(first, last)) == len);
999 this->m_data.m_vect.insert(this->m_data.m_vect.cend(), len, first, last);
1004 } //namespace container_detail {
1006 } //namespace container {
1008 //!has_trivial_destructor_after_move<> == true_type
1009 //!specialization for optimizations
1010 template <class K, class V, class KOV,
1012 struct has_trivial_destructor_after_move<boost::container::container_detail::flat_tree<K, V, KOV, C, A> >
1014 static const bool value = has_trivial_destructor_after_move<A>::value && has_trivial_destructor_after_move<C>::value;
1017 } //namespace boost {
1019 #include <boost/container/detail/config_end.hpp>
1021 #endif // BOOST_CONTAINER_FLAT_TREE_HPP