Chris@16
|
1 ////////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 //
|
Chris@101
|
3 // (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost
|
Chris@16
|
4 // Software License, Version 1.0. (See accompanying file
|
Chris@16
|
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 //
|
Chris@16
|
7 // See http://www.boost.org/libs/container for documentation.
|
Chris@16
|
8 //
|
Chris@16
|
9 ////////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_CONTAINER_FLAT_TREE_HPP
|
Chris@16
|
12 #define BOOST_CONTAINER_FLAT_TREE_HPP
|
Chris@16
|
13
|
Chris@101
|
14 #ifndef BOOST_CONFIG_HPP
|
Chris@101
|
15 # include <boost/config.hpp>
|
Chris@101
|
16 #endif
|
Chris@101
|
17
|
Chris@101
|
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
|
Chris@16
|
19 # pragma once
|
Chris@16
|
20 #endif
|
Chris@16
|
21
|
Chris@101
|
22 #include <boost/container/detail/config_begin.hpp>
|
Chris@16
|
23 #include <boost/container/detail/workaround.hpp>
|
Chris@16
|
24
|
Chris@16
|
25 #include <boost/container/container_fwd.hpp>
|
Chris@16
|
26
|
Chris@101
|
27 #include <boost/move/utility_core.hpp>
|
Chris@16
|
28
|
Chris@16
|
29 #include <boost/container/detail/pair.hpp>
|
Chris@16
|
30 #include <boost/container/vector.hpp>
|
Chris@16
|
31 #include <boost/container/detail/value_init.hpp>
|
Chris@16
|
32 #include <boost/container/detail/destroyers.hpp>
|
Chris@101
|
33 #include <boost/container/detail/algorithm.hpp> //algo_equal(), algo_lexicographical_compare
|
Chris@101
|
34 #include <boost/container/detail/iterator.hpp>
|
Chris@16
|
35 #include <boost/container/allocator_traits.hpp>
|
Chris@16
|
36 #ifdef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
|
Chris@16
|
37 #include <boost/intrusive/pointer_traits.hpp>
|
Chris@16
|
38 #endif
|
Chris@101
|
39 #include <boost/container/detail/type_traits.hpp>
|
Chris@101
|
40 #include <boost/move/make_unique.hpp>
|
Chris@101
|
41 #include <boost/move/adl_move_swap.hpp>
|
Chris@101
|
42 #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
|
Chris@101
|
43 #include <boost/move/detail/fwd_macros.hpp>
|
Chris@101
|
44 #endif
|
Chris@101
|
45
|
Chris@101
|
46 #include <boost/intrusive/detail/minimal_pair_header.hpp> //pair
|
Chris@16
|
47
|
Chris@16
|
48 namespace boost {
|
Chris@16
|
49 namespace container {
|
Chris@16
|
50 namespace container_detail {
|
Chris@16
|
51
|
Chris@16
|
52 template<class Compare, class Value, class KeyOfValue>
|
Chris@16
|
53 class flat_tree_value_compare
|
Chris@16
|
54 : private Compare
|
Chris@16
|
55 {
|
Chris@16
|
56 typedef Value first_argument_type;
|
Chris@16
|
57 typedef Value second_argument_type;
|
Chris@16
|
58 typedef bool return_type;
|
Chris@101
|
59 public:
|
Chris@16
|
60 flat_tree_value_compare()
|
Chris@16
|
61 : Compare()
|
Chris@16
|
62 {}
|
Chris@16
|
63
|
Chris@16
|
64 flat_tree_value_compare(const Compare &pred)
|
Chris@16
|
65 : Compare(pred)
|
Chris@16
|
66 {}
|
Chris@16
|
67
|
Chris@16
|
68 bool operator()(const Value& lhs, const Value& rhs) const
|
Chris@16
|
69 {
|
Chris@16
|
70 KeyOfValue key_extract;
|
Chris@16
|
71 return Compare::operator()(key_extract(lhs), key_extract(rhs));
|
Chris@16
|
72 }
|
Chris@16
|
73
|
Chris@16
|
74 const Compare &get_comp() const
|
Chris@16
|
75 { return *this; }
|
Chris@101
|
76
|
Chris@16
|
77 Compare &get_comp()
|
Chris@16
|
78 { return *this; }
|
Chris@16
|
79 };
|
Chris@16
|
80
|
Chris@16
|
81 template<class Pointer>
|
Chris@16
|
82 struct get_flat_tree_iterators
|
Chris@16
|
83 {
|
Chris@16
|
84 #ifdef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
|
Chris@101
|
85 typedef Pointer iterator;
|
Chris@16
|
86 typedef typename boost::intrusive::
|
Chris@101
|
87 pointer_traits<Pointer>::element_type iterator_element_type;
|
Chris@16
|
88 typedef typename boost::intrusive::
|
Chris@16
|
89 pointer_traits<Pointer>:: template
|
Chris@101
|
90 rebind_pointer<const iterator_element_type>::type const_iterator;
|
Chris@16
|
91 #else //BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
|
Chris@101
|
92 typedef typename boost::container::container_detail::
|
Chris@101
|
93 vec_iterator<Pointer, false> iterator;
|
Chris@101
|
94 typedef typename boost::container::container_detail::
|
Chris@101
|
95 vec_iterator<Pointer, true > const_iterator;
|
Chris@16
|
96 #endif //BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
|
Chris@101
|
97 typedef boost::container::reverse_iterator<iterator> reverse_iterator;
|
Chris@101
|
98 typedef boost::container::reverse_iterator<const_iterator> const_reverse_iterator;
|
Chris@16
|
99 };
|
Chris@16
|
100
|
Chris@16
|
101 template <class Key, class Value, class KeyOfValue,
|
Chris@101
|
102 class Compare, class Allocator>
|
Chris@16
|
103 class flat_tree
|
Chris@16
|
104 {
|
Chris@101
|
105 typedef boost::container::vector<Value, Allocator> vector_t;
|
Chris@101
|
106 typedef Allocator allocator_t;
|
Chris@101
|
107 typedef allocator_traits<Allocator> allocator_traits_type;
|
Chris@16
|
108
|
Chris@16
|
109 public:
|
Chris@16
|
110 typedef flat_tree_value_compare<Compare, Value, KeyOfValue> value_compare;
|
Chris@16
|
111
|
Chris@16
|
112 private:
|
Chris@16
|
113 struct Data
|
Chris@16
|
114 //Inherit from value_compare to do EBO
|
Chris@16
|
115 : public value_compare
|
Chris@16
|
116 {
|
Chris@16
|
117 BOOST_COPYABLE_AND_MOVABLE(Data)
|
Chris@16
|
118
|
Chris@16
|
119 public:
|
Chris@16
|
120 Data()
|
Chris@16
|
121 : value_compare(), m_vect()
|
Chris@16
|
122 {}
|
Chris@16
|
123
|
Chris@16
|
124 explicit Data(const Data &d)
|
Chris@16
|
125 : value_compare(static_cast<const value_compare&>(d)), m_vect(d.m_vect)
|
Chris@16
|
126 {}
|
Chris@16
|
127
|
Chris@16
|
128 Data(BOOST_RV_REF(Data) d)
|
Chris@16
|
129 : value_compare(boost::move(static_cast<value_compare&>(d))), m_vect(boost::move(d.m_vect))
|
Chris@16
|
130 {}
|
Chris@16
|
131
|
Chris@101
|
132 Data(const Data &d, const Allocator &a)
|
Chris@16
|
133 : value_compare(static_cast<const value_compare&>(d)), m_vect(d.m_vect, a)
|
Chris@16
|
134 {}
|
Chris@16
|
135
|
Chris@101
|
136 Data(BOOST_RV_REF(Data) d, const Allocator &a)
|
Chris@16
|
137 : value_compare(boost::move(static_cast<value_compare&>(d))), m_vect(boost::move(d.m_vect), a)
|
Chris@16
|
138 {}
|
Chris@16
|
139
|
Chris@16
|
140 explicit Data(const Compare &comp)
|
Chris@16
|
141 : value_compare(comp), m_vect()
|
Chris@16
|
142 {}
|
Chris@16
|
143
|
Chris@16
|
144 Data(const Compare &comp, const allocator_t &alloc)
|
Chris@16
|
145 : value_compare(comp), m_vect(alloc)
|
Chris@16
|
146 {}
|
Chris@16
|
147
|
Chris@16
|
148 explicit Data(const allocator_t &alloc)
|
Chris@16
|
149 : value_compare(), m_vect(alloc)
|
Chris@16
|
150 {}
|
Chris@16
|
151
|
Chris@16
|
152 Data& operator=(BOOST_COPY_ASSIGN_REF(Data) d)
|
Chris@16
|
153 {
|
Chris@16
|
154 this->value_compare::operator=(d);
|
Chris@16
|
155 m_vect = d.m_vect;
|
Chris@16
|
156 return *this;
|
Chris@16
|
157 }
|
Chris@16
|
158
|
Chris@16
|
159 Data& operator=(BOOST_RV_REF(Data) d)
|
Chris@16
|
160 {
|
Chris@16
|
161 this->value_compare::operator=(boost::move(static_cast<value_compare &>(d)));
|
Chris@16
|
162 m_vect = boost::move(d.m_vect);
|
Chris@16
|
163 return *this;
|
Chris@16
|
164 }
|
Chris@16
|
165
|
Chris@16
|
166 void swap(Data &d)
|
Chris@16
|
167 {
|
Chris@16
|
168 value_compare& mycomp = *this, & othercomp = d;
|
Chris@101
|
169 boost::adl_move_swap(mycomp, othercomp);
|
Chris@16
|
170 this->m_vect.swap(d.m_vect);
|
Chris@16
|
171 }
|
Chris@16
|
172
|
Chris@16
|
173 vector_t m_vect;
|
Chris@16
|
174 };
|
Chris@16
|
175
|
Chris@16
|
176 Data m_data;
|
Chris@16
|
177 BOOST_COPYABLE_AND_MOVABLE(flat_tree)
|
Chris@16
|
178
|
Chris@16
|
179 public:
|
Chris@16
|
180
|
Chris@16
|
181 typedef typename vector_t::value_type value_type;
|
Chris@16
|
182 typedef typename vector_t::pointer pointer;
|
Chris@16
|
183 typedef typename vector_t::const_pointer const_pointer;
|
Chris@16
|
184 typedef typename vector_t::reference reference;
|
Chris@16
|
185 typedef typename vector_t::const_reference const_reference;
|
Chris@16
|
186 typedef Key key_type;
|
Chris@16
|
187 typedef Compare key_compare;
|
Chris@16
|
188 typedef typename vector_t::allocator_type allocator_type;
|
Chris@16
|
189 typedef typename vector_t::size_type size_type;
|
Chris@16
|
190 typedef typename vector_t::difference_type difference_type;
|
Chris@16
|
191 typedef typename vector_t::iterator iterator;
|
Chris@16
|
192 typedef typename vector_t::const_iterator const_iterator;
|
Chris@16
|
193 typedef typename vector_t::reverse_iterator reverse_iterator;
|
Chris@16
|
194 typedef typename vector_t::const_reverse_iterator const_reverse_iterator;
|
Chris@16
|
195
|
Chris@16
|
196 //!Standard extension
|
Chris@16
|
197 typedef allocator_type stored_allocator_type;
|
Chris@16
|
198
|
Chris@16
|
199 private:
|
Chris@16
|
200 typedef allocator_traits<stored_allocator_type> stored_allocator_traits;
|
Chris@16
|
201
|
Chris@16
|
202 public:
|
Chris@16
|
203 flat_tree()
|
Chris@16
|
204 : m_data()
|
Chris@16
|
205 { }
|
Chris@16
|
206
|
Chris@16
|
207 explicit flat_tree(const Compare& comp)
|
Chris@16
|
208 : m_data(comp)
|
Chris@16
|
209 { }
|
Chris@16
|
210
|
Chris@16
|
211 flat_tree(const Compare& comp, const allocator_type& a)
|
Chris@16
|
212 : m_data(comp, a)
|
Chris@16
|
213 { }
|
Chris@16
|
214
|
Chris@16
|
215 explicit flat_tree(const allocator_type& a)
|
Chris@16
|
216 : m_data(a)
|
Chris@16
|
217 { }
|
Chris@16
|
218
|
Chris@16
|
219 flat_tree(const flat_tree& x)
|
Chris@16
|
220 : m_data(x.m_data)
|
Chris@16
|
221 { }
|
Chris@16
|
222
|
Chris@16
|
223 flat_tree(BOOST_RV_REF(flat_tree) x)
|
Chris@16
|
224 : m_data(boost::move(x.m_data))
|
Chris@16
|
225 { }
|
Chris@16
|
226
|
Chris@16
|
227 flat_tree(const flat_tree& x, const allocator_type &a)
|
Chris@16
|
228 : m_data(x.m_data, a)
|
Chris@16
|
229 { }
|
Chris@16
|
230
|
Chris@16
|
231 flat_tree(BOOST_RV_REF(flat_tree) x, const allocator_type &a)
|
Chris@16
|
232 : m_data(boost::move(x.m_data), a)
|
Chris@16
|
233 { }
|
Chris@16
|
234
|
Chris@16
|
235 template <class InputIterator>
|
Chris@16
|
236 flat_tree( ordered_range_t, InputIterator first, InputIterator last
|
Chris@16
|
237 , const Compare& comp = Compare()
|
Chris@16
|
238 , const allocator_type& a = allocator_type())
|
Chris@16
|
239 : m_data(comp, a)
|
Chris@16
|
240 { this->m_data.m_vect.insert(this->m_data.m_vect.end(), first, last); }
|
Chris@16
|
241
|
Chris@16
|
242 template <class InputIterator>
|
Chris@16
|
243 flat_tree( bool unique_insertion
|
Chris@16
|
244 , InputIterator first, InputIterator last
|
Chris@16
|
245 , const Compare& comp = Compare()
|
Chris@16
|
246 , const allocator_type& a = allocator_type())
|
Chris@16
|
247 : m_data(comp, a)
|
Chris@16
|
248 {
|
Chris@16
|
249 //Use cend() as hint to achieve linear time for
|
Chris@16
|
250 //ordered ranges as required by the standard
|
Chris@16
|
251 //for the constructor
|
Chris@16
|
252 //Call end() every iteration as reallocation might have invalidated iterators
|
Chris@16
|
253 if(unique_insertion){
|
Chris@16
|
254 for ( ; first != last; ++first){
|
Chris@16
|
255 this->insert_unique(this->cend(), *first);
|
Chris@16
|
256 }
|
Chris@16
|
257 }
|
Chris@16
|
258 else{
|
Chris@16
|
259 for ( ; first != last; ++first){
|
Chris@16
|
260 this->insert_equal(this->cend(), *first);
|
Chris@16
|
261 }
|
Chris@16
|
262 }
|
Chris@16
|
263 }
|
Chris@16
|
264
|
Chris@16
|
265 ~flat_tree()
|
Chris@16
|
266 {}
|
Chris@16
|
267
|
Chris@16
|
268 flat_tree& operator=(BOOST_COPY_ASSIGN_REF(flat_tree) x)
|
Chris@16
|
269 { m_data = x.m_data; return *this; }
|
Chris@16
|
270
|
Chris@101
|
271 flat_tree& operator=(BOOST_RV_REF(flat_tree) x)
|
Chris@101
|
272 BOOST_NOEXCEPT_IF( allocator_traits_type::is_always_equal::value
|
Chris@101
|
273 && boost::container::container_detail::is_nothrow_move_assignable<Compare>::value )
|
Chris@101
|
274 { m_data = boost::move(x.m_data); return *this; }
|
Chris@16
|
275
|
Chris@101
|
276 public:
|
Chris@16
|
277 // accessors:
|
Chris@16
|
278 Compare key_comp() const
|
Chris@16
|
279 { return this->m_data.get_comp(); }
|
Chris@16
|
280
|
Chris@101
|
281 value_compare value_comp() const
|
Chris@101
|
282 { return this->m_data; }
|
Chris@101
|
283
|
Chris@16
|
284 allocator_type get_allocator() const
|
Chris@16
|
285 { return this->m_data.m_vect.get_allocator(); }
|
Chris@16
|
286
|
Chris@16
|
287 const stored_allocator_type &get_stored_allocator() const
|
Chris@16
|
288 { return this->m_data.m_vect.get_stored_allocator(); }
|
Chris@16
|
289
|
Chris@16
|
290 stored_allocator_type &get_stored_allocator()
|
Chris@16
|
291 { return this->m_data.m_vect.get_stored_allocator(); }
|
Chris@16
|
292
|
Chris@16
|
293 iterator begin()
|
Chris@16
|
294 { return this->m_data.m_vect.begin(); }
|
Chris@16
|
295
|
Chris@16
|
296 const_iterator begin() const
|
Chris@16
|
297 { return this->cbegin(); }
|
Chris@16
|
298
|
Chris@16
|
299 const_iterator cbegin() const
|
Chris@16
|
300 { return this->m_data.m_vect.begin(); }
|
Chris@16
|
301
|
Chris@16
|
302 iterator end()
|
Chris@16
|
303 { return this->m_data.m_vect.end(); }
|
Chris@16
|
304
|
Chris@16
|
305 const_iterator end() const
|
Chris@16
|
306 { return this->cend(); }
|
Chris@16
|
307
|
Chris@16
|
308 const_iterator cend() const
|
Chris@16
|
309 { return this->m_data.m_vect.end(); }
|
Chris@16
|
310
|
Chris@16
|
311 reverse_iterator rbegin()
|
Chris@16
|
312 { return reverse_iterator(this->end()); }
|
Chris@16
|
313
|
Chris@16
|
314 const_reverse_iterator rbegin() const
|
Chris@16
|
315 { return this->crbegin(); }
|
Chris@16
|
316
|
Chris@16
|
317 const_reverse_iterator crbegin() const
|
Chris@16
|
318 { return const_reverse_iterator(this->cend()); }
|
Chris@16
|
319
|
Chris@16
|
320 reverse_iterator rend()
|
Chris@16
|
321 { return reverse_iterator(this->begin()); }
|
Chris@16
|
322
|
Chris@16
|
323 const_reverse_iterator rend() const
|
Chris@16
|
324 { return this->crend(); }
|
Chris@16
|
325
|
Chris@16
|
326 const_reverse_iterator crend() const
|
Chris@16
|
327 { return const_reverse_iterator(this->cbegin()); }
|
Chris@16
|
328
|
Chris@16
|
329 bool empty() const
|
Chris@16
|
330 { return this->m_data.m_vect.empty(); }
|
Chris@16
|
331
|
Chris@16
|
332 size_type size() const
|
Chris@16
|
333 { return this->m_data.m_vect.size(); }
|
Chris@16
|
334
|
Chris@16
|
335 size_type max_size() const
|
Chris@16
|
336 { return this->m_data.m_vect.max_size(); }
|
Chris@16
|
337
|
Chris@16
|
338 void swap(flat_tree& other)
|
Chris@101
|
339 BOOST_NOEXCEPT_IF( allocator_traits_type::is_always_equal::value
|
Chris@101
|
340 && boost::container::container_detail::is_nothrow_swappable<Compare>::value )
|
Chris@16
|
341 { this->m_data.swap(other.m_data); }
|
Chris@16
|
342
|
Chris@16
|
343 public:
|
Chris@16
|
344 // insert/erase
|
Chris@16
|
345 std::pair<iterator,bool> insert_unique(const value_type& val)
|
Chris@16
|
346 {
|
Chris@16
|
347 std::pair<iterator,bool> ret;
|
Chris@16
|
348 insert_commit_data data;
|
Chris@16
|
349 ret.second = this->priv_insert_unique_prepare(val, data);
|
Chris@16
|
350 ret.first = ret.second ? this->priv_insert_commit(data, val)
|
Chris@16
|
351 : iterator(vector_iterator_get_ptr(data.position));
|
Chris@16
|
352 return ret;
|
Chris@16
|
353 }
|
Chris@16
|
354
|
Chris@16
|
355 std::pair<iterator,bool> insert_unique(BOOST_RV_REF(value_type) val)
|
Chris@16
|
356 {
|
Chris@16
|
357 std::pair<iterator,bool> ret;
|
Chris@16
|
358 insert_commit_data data;
|
Chris@16
|
359 ret.second = this->priv_insert_unique_prepare(val, data);
|
Chris@16
|
360 ret.first = ret.second ? this->priv_insert_commit(data, boost::move(val))
|
Chris@16
|
361 : iterator(vector_iterator_get_ptr(data.position));
|
Chris@16
|
362 return ret;
|
Chris@16
|
363 }
|
Chris@16
|
364
|
Chris@16
|
365 iterator insert_equal(const value_type& val)
|
Chris@16
|
366 {
|
Chris@16
|
367 iterator i = this->upper_bound(KeyOfValue()(val));
|
Chris@16
|
368 i = this->m_data.m_vect.insert(i, val);
|
Chris@16
|
369 return i;
|
Chris@16
|
370 }
|
Chris@16
|
371
|
Chris@16
|
372 iterator insert_equal(BOOST_RV_REF(value_type) mval)
|
Chris@16
|
373 {
|
Chris@16
|
374 iterator i = this->upper_bound(KeyOfValue()(mval));
|
Chris@16
|
375 i = this->m_data.m_vect.insert(i, boost::move(mval));
|
Chris@16
|
376 return i;
|
Chris@16
|
377 }
|
Chris@16
|
378
|
Chris@16
|
379 iterator insert_unique(const_iterator pos, const value_type& val)
|
Chris@16
|
380 {
|
Chris@16
|
381 std::pair<iterator,bool> ret;
|
Chris@16
|
382 insert_commit_data data;
|
Chris@16
|
383 return this->priv_insert_unique_prepare(pos, val, data)
|
Chris@16
|
384 ? this->priv_insert_commit(data, val)
|
Chris@16
|
385 : iterator(vector_iterator_get_ptr(data.position));
|
Chris@16
|
386 }
|
Chris@16
|
387
|
Chris@16
|
388 iterator insert_unique(const_iterator pos, BOOST_RV_REF(value_type) val)
|
Chris@16
|
389 {
|
Chris@16
|
390 std::pair<iterator,bool> ret;
|
Chris@16
|
391 insert_commit_data data;
|
Chris@16
|
392 return this->priv_insert_unique_prepare(pos, val, data)
|
Chris@16
|
393 ? this->priv_insert_commit(data, boost::move(val))
|
Chris@16
|
394 : iterator(vector_iterator_get_ptr(data.position));
|
Chris@16
|
395 }
|
Chris@16
|
396
|
Chris@16
|
397 iterator insert_equal(const_iterator pos, const value_type& val)
|
Chris@16
|
398 {
|
Chris@16
|
399 insert_commit_data data;
|
Chris@16
|
400 this->priv_insert_equal_prepare(pos, val, data);
|
Chris@16
|
401 return this->priv_insert_commit(data, val);
|
Chris@16
|
402 }
|
Chris@16
|
403
|
Chris@16
|
404 iterator insert_equal(const_iterator pos, BOOST_RV_REF(value_type) mval)
|
Chris@16
|
405 {
|
Chris@16
|
406 insert_commit_data data;
|
Chris@16
|
407 this->priv_insert_equal_prepare(pos, mval, data);
|
Chris@16
|
408 return this->priv_insert_commit(data, boost::move(mval));
|
Chris@16
|
409 }
|
Chris@16
|
410
|
Chris@16
|
411 template <class InIt>
|
Chris@16
|
412 void insert_unique(InIt first, InIt last)
|
Chris@16
|
413 {
|
Chris@16
|
414 for ( ; first != last; ++first){
|
Chris@16
|
415 this->insert_unique(*first);
|
Chris@16
|
416 }
|
Chris@16
|
417 }
|
Chris@16
|
418
|
Chris@16
|
419 template <class InIt>
|
Chris@16
|
420 void insert_equal(InIt first, InIt last
|
Chris@16
|
421 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
|
Chris@16
|
422 , typename container_detail::enable_if_c
|
Chris@16
|
423 < container_detail::is_input_iterator<InIt>::value
|
Chris@16
|
424 >::type * = 0
|
Chris@16
|
425 #endif
|
Chris@16
|
426 )
|
Chris@16
|
427 { this->priv_insert_equal_loop(first, last); }
|
Chris@16
|
428
|
Chris@16
|
429 template <class InIt>
|
Chris@16
|
430 void insert_equal(InIt first, InIt last
|
Chris@16
|
431 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
|
Chris@16
|
432 , typename container_detail::enable_if_c
|
Chris@16
|
433 < !container_detail::is_input_iterator<InIt>::value
|
Chris@16
|
434 >::type * = 0
|
Chris@16
|
435 #endif
|
Chris@16
|
436 )
|
Chris@16
|
437 {
|
Chris@101
|
438 const size_type len = static_cast<size_type>(boost::container::iterator_distance(first, last));
|
Chris@16
|
439 this->reserve(this->size()+len);
|
Chris@16
|
440 this->priv_insert_equal_loop(first, last);
|
Chris@16
|
441 }
|
Chris@16
|
442
|
Chris@16
|
443 //Ordered
|
Chris@16
|
444
|
Chris@16
|
445 template <class InIt>
|
Chris@16
|
446 void insert_equal(ordered_range_t, InIt first, InIt last
|
Chris@16
|
447 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
|
Chris@16
|
448 , typename container_detail::enable_if_c
|
Chris@16
|
449 < container_detail::is_input_iterator<InIt>::value
|
Chris@16
|
450 >::type * = 0
|
Chris@16
|
451 #endif
|
Chris@16
|
452 )
|
Chris@16
|
453 { this->priv_insert_equal_loop_ordered(first, last); }
|
Chris@16
|
454
|
Chris@16
|
455 template <class FwdIt>
|
Chris@16
|
456 void insert_equal(ordered_range_t, FwdIt first, FwdIt last
|
Chris@16
|
457 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
|
Chris@16
|
458 , typename container_detail::enable_if_c
|
Chris@16
|
459 < !container_detail::is_input_iterator<FwdIt>::value &&
|
Chris@101
|
460 container_detail::is_forward_iterator<FwdIt>::value
|
Chris@16
|
461 >::type * = 0
|
Chris@16
|
462 #endif
|
Chris@16
|
463 )
|
Chris@16
|
464 {
|
Chris@101
|
465 const size_type len = static_cast<size_type>(boost::container::iterator_distance(first, last));
|
Chris@16
|
466 this->reserve(this->size()+len);
|
Chris@16
|
467 this->priv_insert_equal_loop_ordered(first, last);
|
Chris@16
|
468 }
|
Chris@16
|
469
|
Chris@16
|
470 template <class BidirIt>
|
Chris@16
|
471 void insert_equal(ordered_range_t, BidirIt first, BidirIt last
|
Chris@16
|
472 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
|
Chris@16
|
473 , typename container_detail::enable_if_c
|
Chris@16
|
474 < !container_detail::is_input_iterator<BidirIt>::value &&
|
Chris@16
|
475 !container_detail::is_forward_iterator<BidirIt>::value
|
Chris@16
|
476 >::type * = 0
|
Chris@16
|
477 #endif
|
Chris@16
|
478 )
|
Chris@101
|
479 { this->priv_insert_ordered_range(false, first, last); }
|
Chris@16
|
480
|
Chris@16
|
481 template <class InIt>
|
Chris@16
|
482 void insert_unique(ordered_unique_range_t, InIt first, InIt last
|
Chris@16
|
483 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
|
Chris@16
|
484 , typename container_detail::enable_if_c
|
Chris@16
|
485 < container_detail::is_input_iterator<InIt>::value ||
|
Chris@16
|
486 container_detail::is_forward_iterator<InIt>::value
|
Chris@16
|
487 >::type * = 0
|
Chris@16
|
488 #endif
|
Chris@16
|
489 )
|
Chris@16
|
490 {
|
Chris@16
|
491 const_iterator pos(this->cend());
|
Chris@16
|
492 for ( ; first != last; ++first){
|
Chris@16
|
493 pos = this->insert_unique(pos, *first);
|
Chris@16
|
494 ++pos;
|
Chris@16
|
495 }
|
Chris@16
|
496 }
|
Chris@16
|
497
|
Chris@16
|
498 template <class BidirIt>
|
Chris@16
|
499 void insert_unique(ordered_unique_range_t, BidirIt first, BidirIt last
|
Chris@16
|
500 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
|
Chris@16
|
501 , typename container_detail::enable_if_c
|
Chris@16
|
502 < !(container_detail::is_input_iterator<BidirIt>::value ||
|
Chris@16
|
503 container_detail::is_forward_iterator<BidirIt>::value)
|
Chris@16
|
504 >::type * = 0
|
Chris@16
|
505 #endif
|
Chris@16
|
506 )
|
Chris@101
|
507 { this->priv_insert_ordered_range(true, first, last); }
|
Chris@16
|
508
|
Chris@101
|
509 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
|
Chris@16
|
510
|
Chris@16
|
511 template <class... Args>
|
Chris@101
|
512 std::pair<iterator, bool> emplace_unique(BOOST_FWD_REF(Args)... args)
|
Chris@16
|
513 {
|
Chris@101
|
514 typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;
|
Chris@16
|
515 value_type &val = *static_cast<value_type *>(static_cast<void *>(&v));
|
Chris@16
|
516 stored_allocator_type &a = this->get_stored_allocator();
|
Chris@16
|
517 stored_allocator_traits::construct(a, &val, ::boost::forward<Args>(args)... );
|
Chris@16
|
518 value_destructor<stored_allocator_type> d(a, val);
|
Chris@16
|
519 return this->insert_unique(::boost::move(val));
|
Chris@16
|
520 }
|
Chris@16
|
521
|
Chris@16
|
522 template <class... Args>
|
Chris@101
|
523 iterator emplace_hint_unique(const_iterator hint, BOOST_FWD_REF(Args)... args)
|
Chris@16
|
524 {
|
Chris@101
|
525 typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;
|
Chris@16
|
526 value_type &val = *static_cast<value_type *>(static_cast<void *>(&v));
|
Chris@16
|
527 stored_allocator_type &a = this->get_stored_allocator();
|
Chris@16
|
528 stored_allocator_traits::construct(a, &val, ::boost::forward<Args>(args)... );
|
Chris@16
|
529 value_destructor<stored_allocator_type> d(a, val);
|
Chris@16
|
530 return this->insert_unique(hint, ::boost::move(val));
|
Chris@16
|
531 }
|
Chris@16
|
532
|
Chris@16
|
533 template <class... Args>
|
Chris@101
|
534 iterator emplace_equal(BOOST_FWD_REF(Args)... args)
|
Chris@16
|
535 {
|
Chris@101
|
536 typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;
|
Chris@16
|
537 value_type &val = *static_cast<value_type *>(static_cast<void *>(&v));
|
Chris@16
|
538 stored_allocator_type &a = this->get_stored_allocator();
|
Chris@16
|
539 stored_allocator_traits::construct(a, &val, ::boost::forward<Args>(args)... );
|
Chris@16
|
540 value_destructor<stored_allocator_type> d(a, val);
|
Chris@16
|
541 return this->insert_equal(::boost::move(val));
|
Chris@16
|
542 }
|
Chris@16
|
543
|
Chris@16
|
544 template <class... Args>
|
Chris@101
|
545 iterator emplace_hint_equal(const_iterator hint, BOOST_FWD_REF(Args)... args)
|
Chris@16
|
546 {
|
Chris@101
|
547 typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;
|
Chris@16
|
548 value_type &val = *static_cast<value_type *>(static_cast<void *>(&v));
|
Chris@16
|
549 stored_allocator_type &a = this->get_stored_allocator();
|
Chris@16
|
550 stored_allocator_traits::construct(a, &val, ::boost::forward<Args>(args)... );
|
Chris@16
|
551 value_destructor<stored_allocator_type> d(a, val);
|
Chris@16
|
552 return this->insert_equal(hint, ::boost::move(val));
|
Chris@16
|
553 }
|
Chris@16
|
554
|
Chris@101
|
555 #else // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
|
Chris@16
|
556
|
Chris@101
|
557 #define BOOST_CONTAINER_FLAT_TREE_EMPLACE_CODE(N) \
|
Chris@101
|
558 BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
|
Chris@101
|
559 std::pair<iterator, bool> emplace_unique(BOOST_MOVE_UREF##N)\
|
Chris@101
|
560 {\
|
Chris@101
|
561 typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;\
|
Chris@101
|
562 value_type &val = *static_cast<value_type *>(static_cast<void *>(&v));\
|
Chris@101
|
563 stored_allocator_type &a = this->get_stored_allocator();\
|
Chris@101
|
564 stored_allocator_traits::construct(a, &val BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
|
Chris@101
|
565 value_destructor<stored_allocator_type> d(a, val);\
|
Chris@101
|
566 return this->insert_unique(::boost::move(val));\
|
Chris@101
|
567 }\
|
Chris@101
|
568 \
|
Chris@101
|
569 BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
|
Chris@101
|
570 iterator emplace_hint_unique(const_iterator hint BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
|
Chris@101
|
571 {\
|
Chris@101
|
572 typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;\
|
Chris@101
|
573 value_type &val = *static_cast<value_type *>(static_cast<void *>(&v));\
|
Chris@101
|
574 stored_allocator_type &a = this->get_stored_allocator();\
|
Chris@101
|
575 stored_allocator_traits::construct(a, &val BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
|
Chris@101
|
576 value_destructor<stored_allocator_type> d(a, val);\
|
Chris@101
|
577 return this->insert_unique(hint, ::boost::move(val));\
|
Chris@101
|
578 }\
|
Chris@101
|
579 \
|
Chris@101
|
580 BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
|
Chris@101
|
581 iterator emplace_equal(BOOST_MOVE_UREF##N)\
|
Chris@101
|
582 {\
|
Chris@101
|
583 typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;\
|
Chris@101
|
584 value_type &val = *static_cast<value_type *>(static_cast<void *>(&v));\
|
Chris@101
|
585 stored_allocator_type &a = this->get_stored_allocator();\
|
Chris@101
|
586 stored_allocator_traits::construct(a, &val BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
|
Chris@101
|
587 value_destructor<stored_allocator_type> d(a, val);\
|
Chris@101
|
588 return this->insert_equal(::boost::move(val));\
|
Chris@101
|
589 }\
|
Chris@101
|
590 \
|
Chris@101
|
591 BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
|
Chris@101
|
592 iterator emplace_hint_equal(const_iterator hint BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
|
Chris@101
|
593 {\
|
Chris@101
|
594 typename aligned_storage <sizeof(value_type), alignment_of<value_type>::value>::type v;\
|
Chris@101
|
595 value_type &val = *static_cast<value_type *>(static_cast<void *>(&v));\
|
Chris@101
|
596 stored_allocator_type &a = this->get_stored_allocator();\
|
Chris@101
|
597 stored_allocator_traits::construct(a, &val BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
|
Chris@101
|
598 value_destructor<stored_allocator_type> d(a, val);\
|
Chris@101
|
599 return this->insert_equal(hint, ::boost::move(val));\
|
Chris@101
|
600 }\
|
Chris@101
|
601 //
|
Chris@101
|
602 BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_FLAT_TREE_EMPLACE_CODE)
|
Chris@101
|
603 #undef BOOST_CONTAINER_FLAT_TREE_EMPLACE_CODE
|
Chris@16
|
604
|
Chris@101
|
605 #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
|
Chris@16
|
606
|
Chris@16
|
607 iterator erase(const_iterator position)
|
Chris@16
|
608 { return this->m_data.m_vect.erase(position); }
|
Chris@16
|
609
|
Chris@16
|
610 size_type erase(const key_type& k)
|
Chris@16
|
611 {
|
Chris@16
|
612 std::pair<iterator,iterator > itp = this->equal_range(k);
|
Chris@16
|
613 size_type ret = static_cast<size_type>(itp.second-itp.first);
|
Chris@16
|
614 if (ret){
|
Chris@16
|
615 this->m_data.m_vect.erase(itp.first, itp.second);
|
Chris@16
|
616 }
|
Chris@16
|
617 return ret;
|
Chris@16
|
618 }
|
Chris@16
|
619
|
Chris@16
|
620 iterator erase(const_iterator first, const_iterator last)
|
Chris@16
|
621 { return this->m_data.m_vect.erase(first, last); }
|
Chris@16
|
622
|
Chris@16
|
623 void clear()
|
Chris@16
|
624 { this->m_data.m_vect.clear(); }
|
Chris@16
|
625
|
Chris@16
|
626 //! <b>Effects</b>: Tries to deallocate the excess of memory created
|
Chris@16
|
627 // with previous allocations. The size of the vector is unchanged
|
Chris@16
|
628 //!
|
Chris@16
|
629 //! <b>Throws</b>: If memory allocation throws, or T's copy constructor throws.
|
Chris@16
|
630 //!
|
Chris@16
|
631 //! <b>Complexity</b>: Linear to size().
|
Chris@16
|
632 void shrink_to_fit()
|
Chris@16
|
633 { this->m_data.m_vect.shrink_to_fit(); }
|
Chris@16
|
634
|
Chris@101
|
635 iterator nth(size_type n) BOOST_NOEXCEPT_OR_NOTHROW
|
Chris@101
|
636 { return this->m_data.m_vect.nth(n); }
|
Chris@101
|
637
|
Chris@101
|
638 const_iterator nth(size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
|
Chris@101
|
639 { return this->m_data.m_vect.nth(n); }
|
Chris@101
|
640
|
Chris@101
|
641 size_type index_of(iterator p) BOOST_NOEXCEPT_OR_NOTHROW
|
Chris@101
|
642 { return this->m_data.m_vect.index_of(p); }
|
Chris@101
|
643
|
Chris@101
|
644 size_type index_of(const_iterator p) const BOOST_NOEXCEPT_OR_NOTHROW
|
Chris@101
|
645 { return this->m_data.m_vect.index_of(p); }
|
Chris@101
|
646
|
Chris@16
|
647 // set operations:
|
Chris@16
|
648 iterator find(const key_type& k)
|
Chris@16
|
649 {
|
Chris@16
|
650 iterator i = this->lower_bound(k);
|
Chris@16
|
651 iterator end_it = this->end();
|
Chris@101
|
652 if (i != end_it && this->m_data.get_comp()(k, KeyOfValue()(*i))){
|
Chris@101
|
653 i = end_it;
|
Chris@16
|
654 }
|
Chris@16
|
655 return i;
|
Chris@16
|
656 }
|
Chris@16
|
657
|
Chris@16
|
658 const_iterator find(const key_type& k) const
|
Chris@16
|
659 {
|
Chris@16
|
660 const_iterator i = this->lower_bound(k);
|
Chris@16
|
661
|
Chris@16
|
662 const_iterator end_it = this->cend();
|
Chris@101
|
663 if (i != end_it && this->m_data.get_comp()(k, KeyOfValue()(*i))){
|
Chris@16
|
664 i = end_it;
|
Chris@16
|
665 }
|
Chris@16
|
666 return i;
|
Chris@16
|
667 }
|
Chris@16
|
668
|
Chris@101
|
669 // set operations:
|
Chris@16
|
670 size_type count(const key_type& k) const
|
Chris@16
|
671 {
|
Chris@16
|
672 std::pair<const_iterator, const_iterator> p = this->equal_range(k);
|
Chris@16
|
673 size_type n = p.second - p.first;
|
Chris@16
|
674 return n;
|
Chris@16
|
675 }
|
Chris@16
|
676
|
Chris@16
|
677 iterator lower_bound(const key_type& k)
|
Chris@16
|
678 { return this->priv_lower_bound(this->begin(), this->end(), k); }
|
Chris@16
|
679
|
Chris@16
|
680 const_iterator lower_bound(const key_type& k) const
|
Chris@16
|
681 { return this->priv_lower_bound(this->cbegin(), this->cend(), k); }
|
Chris@16
|
682
|
Chris@16
|
683 iterator upper_bound(const key_type& k)
|
Chris@16
|
684 { return this->priv_upper_bound(this->begin(), this->end(), k); }
|
Chris@16
|
685
|
Chris@16
|
686 const_iterator upper_bound(const key_type& k) const
|
Chris@16
|
687 { return this->priv_upper_bound(this->cbegin(), this->cend(), k); }
|
Chris@16
|
688
|
Chris@16
|
689 std::pair<iterator,iterator> equal_range(const key_type& k)
|
Chris@16
|
690 { return this->priv_equal_range(this->begin(), this->end(), k); }
|
Chris@16
|
691
|
Chris@16
|
692 std::pair<const_iterator, const_iterator> equal_range(const key_type& k) const
|
Chris@16
|
693 { return this->priv_equal_range(this->cbegin(), this->cend(), k); }
|
Chris@16
|
694
|
Chris@101
|
695 std::pair<iterator, iterator> lower_bound_range(const key_type& k)
|
Chris@101
|
696 { return this->priv_lower_bound_range(this->begin(), this->end(), k); }
|
Chris@101
|
697
|
Chris@101
|
698 std::pair<const_iterator, const_iterator> lower_bound_range(const key_type& k) const
|
Chris@101
|
699 { return this->priv_lower_bound_range(this->cbegin(), this->cend(), k); }
|
Chris@101
|
700
|
Chris@101
|
701 size_type capacity() const
|
Chris@16
|
702 { return this->m_data.m_vect.capacity(); }
|
Chris@16
|
703
|
Chris@101
|
704 void reserve(size_type cnt)
|
Chris@16
|
705 { this->m_data.m_vect.reserve(cnt); }
|
Chris@16
|
706
|
Chris@101
|
707 friend bool operator==(const flat_tree& x, const flat_tree& y)
|
Chris@101
|
708 {
|
Chris@101
|
709 return x.size() == y.size() && ::boost::container::algo_equal(x.begin(), x.end(), y.begin());
|
Chris@101
|
710 }
|
Chris@101
|
711
|
Chris@101
|
712 friend bool operator<(const flat_tree& x, const flat_tree& y)
|
Chris@101
|
713 {
|
Chris@101
|
714 return ::boost::container::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
|
Chris@101
|
715 }
|
Chris@101
|
716
|
Chris@101
|
717 friend bool operator!=(const flat_tree& x, const flat_tree& y)
|
Chris@101
|
718 { return !(x == y); }
|
Chris@101
|
719
|
Chris@101
|
720 friend bool operator>(const flat_tree& x, const flat_tree& y)
|
Chris@101
|
721 { return y < x; }
|
Chris@101
|
722
|
Chris@101
|
723 friend bool operator<=(const flat_tree& x, const flat_tree& y)
|
Chris@101
|
724 { return !(y < x); }
|
Chris@101
|
725
|
Chris@101
|
726 friend bool operator>=(const flat_tree& x, const flat_tree& y)
|
Chris@101
|
727 { return !(x < y); }
|
Chris@101
|
728
|
Chris@101
|
729 friend void swap(flat_tree& x, flat_tree& y)
|
Chris@101
|
730 { x.swap(y); }
|
Chris@101
|
731
|
Chris@16
|
732 private:
|
Chris@16
|
733 struct insert_commit_data
|
Chris@16
|
734 {
|
Chris@16
|
735 const_iterator position;
|
Chris@16
|
736 };
|
Chris@16
|
737
|
Chris@16
|
738 // insert/erase
|
Chris@16
|
739 void priv_insert_equal_prepare
|
Chris@16
|
740 (const_iterator pos, const value_type& val, insert_commit_data &data)
|
Chris@16
|
741 {
|
Chris@16
|
742 // N1780
|
Chris@16
|
743 // To insert val at pos:
|
Chris@16
|
744 // if pos == end || val <= *pos
|
Chris@16
|
745 // if pos == begin || val >= *(pos-1)
|
Chris@16
|
746 // insert val before pos
|
Chris@16
|
747 // else
|
Chris@16
|
748 // insert val before upper_bound(val)
|
Chris@16
|
749 // else
|
Chris@16
|
750 // insert val before lower_bound(val)
|
Chris@101
|
751 const value_compare &val_cmp = this->m_data;
|
Chris@16
|
752
|
Chris@101
|
753 if(pos == this->cend() || !val_cmp(*pos, val)){
|
Chris@101
|
754 if (pos == this->cbegin() || !val_cmp(val, pos[-1])){
|
Chris@16
|
755 data.position = pos;
|
Chris@16
|
756 }
|
Chris@16
|
757 else{
|
Chris@16
|
758 data.position =
|
Chris@16
|
759 this->priv_upper_bound(this->cbegin(), pos, KeyOfValue()(val));
|
Chris@16
|
760 }
|
Chris@16
|
761 }
|
Chris@16
|
762 else{
|
Chris@16
|
763 data.position =
|
Chris@16
|
764 this->priv_lower_bound(pos, this->cend(), KeyOfValue()(val));
|
Chris@16
|
765 }
|
Chris@16
|
766 }
|
Chris@16
|
767
|
Chris@16
|
768 bool priv_insert_unique_prepare
|
Chris@16
|
769 (const_iterator b, const_iterator e, const value_type& val, insert_commit_data &commit_data)
|
Chris@16
|
770 {
|
Chris@101
|
771 const value_compare &val_cmp = this->m_data;
|
Chris@16
|
772 commit_data.position = this->priv_lower_bound(b, e, KeyOfValue()(val));
|
Chris@101
|
773 return commit_data.position == e || val_cmp(val, *commit_data.position);
|
Chris@16
|
774 }
|
Chris@16
|
775
|
Chris@16
|
776 bool priv_insert_unique_prepare
|
Chris@16
|
777 (const value_type& val, insert_commit_data &commit_data)
|
Chris@16
|
778 { return this->priv_insert_unique_prepare(this->cbegin(), this->cend(), val, commit_data); }
|
Chris@16
|
779
|
Chris@16
|
780 bool priv_insert_unique_prepare
|
Chris@16
|
781 (const_iterator pos, const value_type& val, insert_commit_data &commit_data)
|
Chris@16
|
782 {
|
Chris@16
|
783 //N1780. Props to Howard Hinnant!
|
Chris@16
|
784 //To insert val at pos:
|
Chris@16
|
785 //if pos == end || val <= *pos
|
Chris@16
|
786 // if pos == begin || val >= *(pos-1)
|
Chris@16
|
787 // insert val before pos
|
Chris@16
|
788 // else
|
Chris@16
|
789 // insert val before upper_bound(val)
|
Chris@16
|
790 //else if pos+1 == end || val <= *(pos+1)
|
Chris@16
|
791 // insert val after pos
|
Chris@16
|
792 //else
|
Chris@16
|
793 // insert val before lower_bound(val)
|
Chris@101
|
794 const value_compare &val_cmp = this->m_data;
|
Chris@16
|
795 const const_iterator cend_it = this->cend();
|
Chris@101
|
796 if(pos == cend_it || val_cmp(val, *pos)){ //Check if val should go before end
|
Chris@16
|
797 const const_iterator cbeg = this->cbegin();
|
Chris@16
|
798 commit_data.position = pos;
|
Chris@16
|
799 if(pos == cbeg){ //If container is empty then insert it in the beginning
|
Chris@16
|
800 return true;
|
Chris@16
|
801 }
|
Chris@16
|
802 const_iterator prev(pos);
|
Chris@16
|
803 --prev;
|
Chris@101
|
804 if(val_cmp(*prev, val)){ //If previous element was less, then it should go between prev and pos
|
Chris@16
|
805 return true;
|
Chris@16
|
806 }
|
Chris@101
|
807 else if(!val_cmp(val, *prev)){ //If previous was equal then insertion should fail
|
Chris@16
|
808 commit_data.position = prev;
|
Chris@16
|
809 return false;
|
Chris@16
|
810 }
|
Chris@16
|
811 else{ //Previous was bigger so insertion hint was pointless, dispatch to hintless insertion
|
Chris@16
|
812 //but reduce the search between beg and prev as prev is bigger than val
|
Chris@16
|
813 return this->priv_insert_unique_prepare(cbeg, prev, val, commit_data);
|
Chris@16
|
814 }
|
Chris@16
|
815 }
|
Chris@16
|
816 else{
|
Chris@16
|
817 //The hint is before the insertion position, so insert it
|
Chris@16
|
818 //in the remaining range [pos, end)
|
Chris@16
|
819 return this->priv_insert_unique_prepare(pos, cend_it, val, commit_data);
|
Chris@16
|
820 }
|
Chris@16
|
821 }
|
Chris@16
|
822
|
Chris@16
|
823 template<class Convertible>
|
Chris@16
|
824 iterator priv_insert_commit
|
Chris@16
|
825 (insert_commit_data &commit_data, BOOST_FWD_REF(Convertible) convertible)
|
Chris@16
|
826 {
|
Chris@16
|
827 return this->m_data.m_vect.insert
|
Chris@16
|
828 ( commit_data.position
|
Chris@16
|
829 , boost::forward<Convertible>(convertible));
|
Chris@16
|
830 }
|
Chris@16
|
831
|
Chris@16
|
832 template <class RanIt>
|
Chris@101
|
833 RanIt priv_lower_bound(RanIt first, const RanIt last,
|
Chris@16
|
834 const key_type & key) const
|
Chris@16
|
835 {
|
Chris@16
|
836 const Compare &key_cmp = this->m_data.get_comp();
|
Chris@16
|
837 KeyOfValue key_extract;
|
Chris@16
|
838 size_type len = static_cast<size_type>(last - first);
|
Chris@16
|
839 RanIt middle;
|
Chris@16
|
840
|
Chris@16
|
841 while (len) {
|
Chris@101
|
842 size_type step = len >> 1;
|
Chris@16
|
843 middle = first;
|
Chris@101
|
844 middle += step;
|
Chris@16
|
845
|
Chris@16
|
846 if (key_cmp(key_extract(*middle), key)) {
|
Chris@101
|
847 first = ++middle;
|
Chris@101
|
848 len -= step + 1;
|
Chris@16
|
849 }
|
Chris@101
|
850 else{
|
Chris@101
|
851 len = step;
|
Chris@101
|
852 }
|
Chris@16
|
853 }
|
Chris@16
|
854 return first;
|
Chris@16
|
855 }
|
Chris@16
|
856
|
Chris@16
|
857 template <class RanIt>
|
Chris@101
|
858 RanIt priv_upper_bound(RanIt first, const RanIt last,
|
Chris@16
|
859 const key_type & key) const
|
Chris@16
|
860 {
|
Chris@16
|
861 const Compare &key_cmp = this->m_data.get_comp();
|
Chris@16
|
862 KeyOfValue key_extract;
|
Chris@16
|
863 size_type len = static_cast<size_type>(last - first);
|
Chris@16
|
864 RanIt middle;
|
Chris@16
|
865
|
Chris@16
|
866 while (len) {
|
Chris@101
|
867 size_type step = len >> 1;
|
Chris@16
|
868 middle = first;
|
Chris@101
|
869 middle += step;
|
Chris@16
|
870
|
Chris@16
|
871 if (key_cmp(key, key_extract(*middle))) {
|
Chris@101
|
872 len = step;
|
Chris@16
|
873 }
|
Chris@16
|
874 else{
|
Chris@16
|
875 first = ++middle;
|
Chris@101
|
876 len -= step + 1;
|
Chris@16
|
877 }
|
Chris@16
|
878 }
|
Chris@16
|
879 return first;
|
Chris@16
|
880 }
|
Chris@16
|
881
|
Chris@16
|
882 template <class RanIt>
|
Chris@16
|
883 std::pair<RanIt, RanIt>
|
Chris@16
|
884 priv_equal_range(RanIt first, RanIt last, const key_type& key) const
|
Chris@16
|
885 {
|
Chris@16
|
886 const Compare &key_cmp = this->m_data.get_comp();
|
Chris@16
|
887 KeyOfValue key_extract;
|
Chris@16
|
888 size_type len = static_cast<size_type>(last - first);
|
Chris@16
|
889 RanIt middle;
|
Chris@16
|
890
|
Chris@16
|
891 while (len) {
|
Chris@101
|
892 size_type step = len >> 1;
|
Chris@16
|
893 middle = first;
|
Chris@101
|
894 middle += step;
|
Chris@16
|
895
|
Chris@16
|
896 if (key_cmp(key_extract(*middle), key)){
|
Chris@101
|
897 first = ++middle;
|
Chris@101
|
898 len -= step + 1;
|
Chris@16
|
899 }
|
Chris@16
|
900 else if (key_cmp(key, key_extract(*middle))){
|
Chris@101
|
901 len = step;
|
Chris@16
|
902 }
|
Chris@16
|
903 else {
|
Chris@16
|
904 //Middle is equal to key
|
Chris@16
|
905 last = first;
|
Chris@16
|
906 last += len;
|
Chris@101
|
907 return std::pair<RanIt, RanIt>
|
Chris@101
|
908 ( this->priv_lower_bound(first, middle, key)
|
Chris@101
|
909 , this->priv_upper_bound(++middle, last, key));
|
Chris@16
|
910 }
|
Chris@16
|
911 }
|
Chris@16
|
912 return std::pair<RanIt, RanIt>(first, first);
|
Chris@16
|
913 }
|
Chris@16
|
914
|
Chris@101
|
915 template<class RanIt>
|
Chris@101
|
916 std::pair<RanIt, RanIt> priv_lower_bound_range(RanIt first, RanIt last, const key_type& k) const
|
Chris@101
|
917 {
|
Chris@101
|
918 const Compare &key_cmp = this->m_data.get_comp();
|
Chris@101
|
919 KeyOfValue key_extract;
|
Chris@101
|
920 RanIt lb(this->priv_lower_bound(first, last, k)), ub(lb);
|
Chris@101
|
921 if(lb != last && static_cast<difference_type>(!key_cmp(k, key_extract(*lb)))){
|
Chris@101
|
922 ++ub;
|
Chris@101
|
923 }
|
Chris@101
|
924 return std::pair<RanIt, RanIt>(lb, ub);
|
Chris@101
|
925 }
|
Chris@101
|
926
|
Chris@16
|
927 template<class InIt>
|
Chris@16
|
928 void priv_insert_equal_loop(InIt first, InIt last)
|
Chris@16
|
929 {
|
Chris@16
|
930 for ( ; first != last; ++first){
|
Chris@16
|
931 this->insert_equal(*first);
|
Chris@16
|
932 }
|
Chris@16
|
933 }
|
Chris@16
|
934
|
Chris@16
|
935 template<class InIt>
|
Chris@16
|
936 void priv_insert_equal_loop_ordered(InIt first, InIt last)
|
Chris@16
|
937 {
|
Chris@16
|
938 const_iterator pos(this->cend());
|
Chris@16
|
939 for ( ; first != last; ++first){
|
Chris@16
|
940 //If ordered, then try hint version
|
Chris@16
|
941 //to achieve constant-time complexity per insertion
|
Chris@16
|
942 pos = this->insert_equal(pos, *first);
|
Chris@16
|
943 ++pos;
|
Chris@16
|
944 }
|
Chris@16
|
945 }
|
Chris@101
|
946
|
Chris@101
|
947 template <class BidirIt>
|
Chris@101
|
948 void priv_insert_ordered_range(const bool unique_values, BidirIt first, BidirIt last)
|
Chris@101
|
949 {
|
Chris@101
|
950 size_type len = static_cast<size_type>(boost::container::iterator_distance(first, last));
|
Chris@101
|
951 //Prereserve all memory so that iterators are not invalidated
|
Chris@101
|
952 this->reserve(this->size()+len);
|
Chris@101
|
953 //Auxiliary data for insertion positions.
|
Chris@101
|
954 const size_type BurstSize = len;
|
Chris@101
|
955 const ::boost::movelib::unique_ptr<size_type[]> positions =
|
Chris@101
|
956 ::boost::movelib::make_unique_definit<size_type[]>(BurstSize);
|
Chris@101
|
957
|
Chris@101
|
958 const const_iterator b(this->cbegin());
|
Chris@101
|
959 const const_iterator ce(this->cend());
|
Chris@101
|
960 const_iterator pos(b);
|
Chris@101
|
961 const value_compare &val_cmp = this->m_data;
|
Chris@101
|
962 //Loop in burst sizes
|
Chris@101
|
963 bool back_insert = false;
|
Chris@101
|
964 while(len && !back_insert){
|
Chris@101
|
965 const size_type burst = len < BurstSize ? len : BurstSize;
|
Chris@101
|
966 size_type unique_burst = 0u;
|
Chris@101
|
967 size_type checked = 0;
|
Chris@101
|
968 for(; checked != burst; ++checked){
|
Chris@101
|
969 //Get the insertion position for each key, use iterator_traits<BidirIt>::value_type
|
Chris@101
|
970 //because it can be different from container::value_type
|
Chris@101
|
971 //(e.g. conversion between std::pair<T1, T2> -> boost::container::pair<T1, T2>
|
Chris@101
|
972 const typename boost::container::iterator_traits<BidirIt>::value_type & val = *first;
|
Chris@101
|
973 pos = const_cast<const flat_tree&>(*this).priv_lower_bound(pos, ce, KeyOfValue()(val));
|
Chris@101
|
974 //Check if already present
|
Chris@101
|
975 if (pos != ce){
|
Chris@101
|
976 ++first;
|
Chris@101
|
977 --len;
|
Chris@101
|
978 positions[checked] = (unique_values && !val_cmp(val, *pos)) ?
|
Chris@101
|
979 size_type(-1) : (++unique_burst, static_cast<size_type>(pos - b));
|
Chris@101
|
980 }
|
Chris@101
|
981 else{ //this element and the remaining should be back inserted
|
Chris@101
|
982 back_insert = true;
|
Chris@101
|
983 break;
|
Chris@101
|
984 }
|
Chris@101
|
985 }
|
Chris@101
|
986 if(unique_burst){
|
Chris@101
|
987 //Insert all in a single step in the precalculated positions
|
Chris@101
|
988 this->m_data.m_vect.insert_ordered_at(unique_burst, positions.get() + checked, first);
|
Chris@101
|
989 //Next search position updated, iterator still valid because we've preserved the vector
|
Chris@101
|
990 pos += unique_burst;
|
Chris@101
|
991 }
|
Chris@101
|
992 }
|
Chris@101
|
993 //The remaining range should be back inserted
|
Chris@101
|
994 if(unique_values){
|
Chris@101
|
995 while(len--){
|
Chris@101
|
996 BidirIt next(first);
|
Chris@101
|
997 ++next;
|
Chris@101
|
998 //Use iterator_traits<BidirIt>::value_type
|
Chris@101
|
999 //because it can be different from container::value_type
|
Chris@101
|
1000 //(e.g. conversion between std::pair<T1, T2> -> boost::container::pair<T1, T2>
|
Chris@101
|
1001 const typename boost::container::iterator_traits<BidirIt>::value_type & val = *first;
|
Chris@101
|
1002 if (next == last || val_cmp(val, *next)){
|
Chris@101
|
1003 const bool room = this->m_data.m_vect.stable_emplace_back(*first);
|
Chris@101
|
1004 (void)room;
|
Chris@101
|
1005 BOOST_ASSERT(room);
|
Chris@101
|
1006 }
|
Chris@101
|
1007 first = next;
|
Chris@101
|
1008 }
|
Chris@101
|
1009 BOOST_ASSERT(first == last);
|
Chris@101
|
1010 }
|
Chris@101
|
1011 else{
|
Chris@101
|
1012 BOOST_ASSERT(size_type(boost::container::iterator_distance(first, last)) == len);
|
Chris@101
|
1013 if(len)
|
Chris@101
|
1014 this->m_data.m_vect.insert(this->m_data.m_vect.cend(), len, first, last);
|
Chris@101
|
1015 }
|
Chris@101
|
1016 }
|
Chris@16
|
1017 };
|
Chris@16
|
1018
|
Chris@16
|
1019 } //namespace container_detail {
|
Chris@16
|
1020
|
Chris@16
|
1021 } //namespace container {
|
Chris@101
|
1022
|
Chris@16
|
1023 //!has_trivial_destructor_after_move<> == true_type
|
Chris@16
|
1024 //!specialization for optimizations
|
Chris@101
|
1025 template <class Key, class T, class KeyOfValue,
|
Chris@101
|
1026 class Compare, class Allocator>
|
Chris@101
|
1027 struct has_trivial_destructor_after_move<boost::container::container_detail::flat_tree<Key, T, KeyOfValue, Compare, Allocator> >
|
Chris@16
|
1028 {
|
Chris@101
|
1029 typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
|
Chris@101
|
1030 static const bool value = ::boost::has_trivial_destructor_after_move<Allocator>::value &&
|
Chris@101
|
1031 ::boost::has_trivial_destructor_after_move<pointer>::value;
|
Chris@16
|
1032 };
|
Chris@101
|
1033
|
Chris@16
|
1034 } //namespace boost {
|
Chris@16
|
1035
|
Chris@16
|
1036 #include <boost/container/detail/config_end.hpp>
|
Chris@16
|
1037
|
Chris@16
|
1038 #endif // BOOST_CONTAINER_FLAT_TREE_HPP
|