]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/container/vector.hpp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / boost / container / vector.hpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2005-2015. 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)
6 //
7 // See http://www.boost.org/libs/container for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10
11 #ifndef BOOST_CONTAINER_CONTAINER_VECTOR_HPP
12 #define BOOST_CONTAINER_CONTAINER_VECTOR_HPP
13
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
16 #endif
17
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
19 # pragma once
20 #endif
21
22 #include <boost/container/detail/config_begin.hpp>
23 #include <boost/container/detail/workaround.hpp>
24
25 // container
26 #include <boost/container/container_fwd.hpp>
27 #include <boost/container/allocator_traits.hpp>
28 #include <boost/container/new_allocator.hpp> //new_allocator
29 #include <boost/container/throw_exception.hpp>
30 #include <boost/container/options.hpp>
31 // container detail
32 #include <boost/container/detail/advanced_insert_int.hpp>
33 #include <boost/container/detail/algorithm.hpp> //equal()
34 #include <boost/container/detail/alloc_helpers.hpp>
35 #include <boost/container/detail/allocation_type.hpp>
36 #include <boost/container/detail/copy_move_algo.hpp>
37 #include <boost/container/detail/destroyers.hpp>
38 #include <boost/container/detail/iterator.hpp>
39 #include <boost/container/detail/iterators.hpp>
40 #include <boost/move/detail/iterator_to_raw_pointer.hpp>
41 #include <boost/container/detail/mpl.hpp>
42 #include <boost/container/detail/next_capacity.hpp>
43 #include <boost/container/detail/value_functors.hpp>
44 #include <boost/move/detail/to_raw_pointer.hpp>
45 #include <boost/container/detail/type_traits.hpp>
46 #include <boost/container/detail/version_type.hpp>
47 // intrusive
48 #include <boost/intrusive/pointer_traits.hpp>
49 // move
50 #include <boost/move/adl_move_swap.hpp>
51 #include <boost/move/iterator.hpp>
52 #include <boost/move/traits.hpp>
53 #include <boost/move/utility_core.hpp>
54 // move/detail
55 #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
56 #include <boost/move/detail/fwd_macros.hpp>
57 #endif
58 #include <boost/move/detail/move_helpers.hpp>
59 // move/algo
60 #include <boost/move/algo/adaptive_merge.hpp>
61 #include <boost/move/algo/unique.hpp>
62 #include <boost/move/algo/predicate.hpp>
63 #include <boost/move/algo/detail/set_difference.hpp>
64 // other
65 #include <boost/core/no_exceptions_support.hpp>
66 #include <boost/assert.hpp>
67 #include <boost/cstdint.hpp>
68
69 //std
70 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
71 #include <initializer_list> //for std::initializer_list
72 #endif
73
74 namespace boost {
75 namespace container {
76
77 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
78
79
80 template <class Pointer, bool IsConst>
81 class vec_iterator
82 {
83 public:
84 typedef std::random_access_iterator_tag iterator_category;
85 typedef typename boost::intrusive::pointer_traits<Pointer>::element_type value_type;
86 typedef typename boost::intrusive::pointer_traits<Pointer>::difference_type difference_type;
87 typedef typename dtl::if_c
88 < IsConst
89 , typename boost::intrusive::pointer_traits<Pointer>::template
90 rebind_pointer<const value_type>::type
91 , Pointer
92 >::type pointer;
93 typedef typename boost::intrusive::pointer_traits<pointer> ptr_traits;
94 typedef typename ptr_traits::reference reference;
95
96 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
97 private:
98 Pointer m_ptr;
99
100 public:
101 BOOST_CONTAINER_FORCEINLINE const Pointer &get_ptr() const BOOST_NOEXCEPT_OR_NOTHROW
102 { return m_ptr; }
103
104 BOOST_CONTAINER_FORCEINLINE Pointer &get_ptr() BOOST_NOEXCEPT_OR_NOTHROW
105 { return m_ptr; }
106
107 BOOST_CONTAINER_FORCEINLINE explicit vec_iterator(Pointer ptr) BOOST_NOEXCEPT_OR_NOTHROW
108 : m_ptr(ptr)
109 {}
110 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
111
112 public:
113
114 //Constructors
115 BOOST_CONTAINER_FORCEINLINE vec_iterator() BOOST_NOEXCEPT_OR_NOTHROW
116 : m_ptr() //Value initialization to achieve "null iterators" (N3644)
117 {}
118
119 BOOST_CONTAINER_FORCEINLINE vec_iterator(vec_iterator<Pointer, false> const& other) BOOST_NOEXCEPT_OR_NOTHROW
120 : m_ptr(other.get_ptr())
121 {}
122
123 //Pointer like operators
124 BOOST_CONTAINER_FORCEINLINE reference operator*() const BOOST_NOEXCEPT_OR_NOTHROW
125 { return *m_ptr; }
126
127 BOOST_CONTAINER_FORCEINLINE pointer operator->() const BOOST_NOEXCEPT_OR_NOTHROW
128 { return ::boost::intrusive::pointer_traits<pointer>::pointer_to(this->operator*()); }
129
130 BOOST_CONTAINER_FORCEINLINE reference operator[](difference_type off) const BOOST_NOEXCEPT_OR_NOTHROW
131 { return m_ptr[off]; }
132
133 //Increment / Decrement
134 BOOST_CONTAINER_FORCEINLINE vec_iterator& operator++() BOOST_NOEXCEPT_OR_NOTHROW
135 { ++m_ptr; return *this; }
136
137 BOOST_CONTAINER_FORCEINLINE vec_iterator operator++(int) BOOST_NOEXCEPT_OR_NOTHROW
138 { return vec_iterator(m_ptr++); }
139
140 BOOST_CONTAINER_FORCEINLINE vec_iterator& operator--() BOOST_NOEXCEPT_OR_NOTHROW
141 { --m_ptr; return *this; }
142
143 BOOST_CONTAINER_FORCEINLINE vec_iterator operator--(int) BOOST_NOEXCEPT_OR_NOTHROW
144 { return vec_iterator(m_ptr--); }
145
146 //Arithmetic
147 BOOST_CONTAINER_FORCEINLINE vec_iterator& operator+=(difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
148 { m_ptr += off; return *this; }
149
150 BOOST_CONTAINER_FORCEINLINE vec_iterator& operator-=(difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
151 { m_ptr -= off; return *this; }
152
153 BOOST_CONTAINER_FORCEINLINE friend vec_iterator operator+(const vec_iterator &x, difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
154 { return vec_iterator(x.m_ptr+off); }
155
156 BOOST_CONTAINER_FORCEINLINE friend vec_iterator operator+(difference_type off, vec_iterator right) BOOST_NOEXCEPT_OR_NOTHROW
157 { right.m_ptr += off; return right; }
158
159 BOOST_CONTAINER_FORCEINLINE friend vec_iterator operator-(vec_iterator left, difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
160 { left.m_ptr -= off; return left; }
161
162 BOOST_CONTAINER_FORCEINLINE friend difference_type operator-(const vec_iterator &left, const vec_iterator& right) BOOST_NOEXCEPT_OR_NOTHROW
163 { return left.m_ptr - right.m_ptr; }
164
165 //Comparison operators
166 BOOST_CONTAINER_FORCEINLINE friend bool operator== (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
167 { return l.m_ptr == r.m_ptr; }
168
169 BOOST_CONTAINER_FORCEINLINE friend bool operator!= (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
170 { return l.m_ptr != r.m_ptr; }
171
172 BOOST_CONTAINER_FORCEINLINE friend bool operator< (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
173 { return l.m_ptr < r.m_ptr; }
174
175 BOOST_CONTAINER_FORCEINLINE friend bool operator<= (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
176 { return l.m_ptr <= r.m_ptr; }
177
178 BOOST_CONTAINER_FORCEINLINE friend bool operator> (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
179 { return l.m_ptr > r.m_ptr; }
180
181 BOOST_CONTAINER_FORCEINLINE friend bool operator>= (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
182 { return l.m_ptr >= r.m_ptr; }
183 };
184
185 template<class BiDirPosConstIt, class BiDirValueIt>
186 struct vector_insert_ordered_cursor
187 {
188 typedef typename iterator_traits<BiDirPosConstIt>::value_type size_type;
189 typedef typename iterator_traits<BiDirValueIt>::reference reference;
190
191 BOOST_CONTAINER_FORCEINLINE vector_insert_ordered_cursor(BiDirPosConstIt posit, BiDirValueIt valueit)
192 : last_position_it(posit), last_value_it(valueit)
193 {}
194
195 void operator --()
196 {
197 --last_value_it;
198 --last_position_it;
199 while(this->get_pos() == size_type(-1)){
200 --last_value_it;
201 --last_position_it;
202 }
203 }
204
205 BOOST_CONTAINER_FORCEINLINE size_type get_pos() const
206 { return *last_position_it; }
207
208 BOOST_CONTAINER_FORCEINLINE reference get_val()
209 { return *last_value_it; }
210
211 BiDirPosConstIt last_position_it;
212 BiDirValueIt last_value_it;
213 };
214
215 struct initial_capacity_t{};
216
217 template<class Pointer, bool IsConst>
218 BOOST_CONTAINER_FORCEINLINE const Pointer &vector_iterator_get_ptr(const vec_iterator<Pointer, IsConst> &it) BOOST_NOEXCEPT_OR_NOTHROW
219 { return it.get_ptr(); }
220
221 template<class Pointer, bool IsConst>
222 BOOST_CONTAINER_FORCEINLINE Pointer &get_ptr(vec_iterator<Pointer, IsConst> &it) BOOST_NOEXCEPT_OR_NOTHROW
223 { return it.get_ptr(); }
224
225 struct vector_uninitialized_size_t {};
226 static const vector_uninitialized_size_t vector_uninitialized_size = vector_uninitialized_size_t();
227
228 template <class T>
229 struct vector_value_traits_base
230 {
231 static const bool trivial_dctr = dtl::is_trivially_destructible<T>::value;
232 static const bool trivial_dctr_after_move = has_trivial_destructor_after_move<T>::value;
233 static const bool trivial_copy = dtl::is_trivially_copy_constructible<T>::value;
234 static const bool nothrow_copy = dtl::is_nothrow_copy_constructible<T>::value || trivial_copy;
235 static const bool trivial_assign = dtl::is_trivially_copy_assignable<T>::value;
236 static const bool nothrow_assign = dtl::is_nothrow_copy_assignable<T>::value || trivial_assign;
237 };
238
239
240 template <class Allocator>
241 struct vector_value_traits
242 : public vector_value_traits_base<typename Allocator::value_type>
243 {
244 typedef vector_value_traits_base<typename Allocator::value_type> base_t;
245 //This is the anti-exception array destructor
246 //to deallocate values already constructed
247 typedef typename dtl::if_c
248 <base_t::trivial_dctr
249 ,dtl::null_scoped_destructor_n<Allocator>
250 ,dtl::scoped_destructor_n<Allocator>
251 >::type ArrayDestructor;
252 //This is the anti-exception array deallocator
253 typedef dtl::scoped_array_deallocator<Allocator> ArrayDeallocator;
254 };
255
256 //!This struct deallocates and allocated memory
257 template < class Allocator
258 , class StoredSizeType
259 , class AllocatorVersion = typename dtl::version<Allocator>::type
260 >
261 struct vector_alloc_holder
262 : public Allocator
263 {
264 private:
265 BOOST_MOVABLE_BUT_NOT_COPYABLE(vector_alloc_holder)
266
267 public:
268 typedef Allocator allocator_type;
269 typedef StoredSizeType stored_size_type;
270 typedef boost::container::allocator_traits<Allocator> allocator_traits_type;
271 typedef typename allocator_traits_type::pointer pointer;
272 typedef typename allocator_traits_type::size_type size_type;
273 typedef typename allocator_traits_type::value_type value_type;
274
275 static bool is_propagable_from(const allocator_type &from_alloc, pointer p, const allocator_type &to_alloc, bool const propagate_allocator)
276 {
277 (void)propagate_allocator; (void)p; (void)to_alloc; (void)from_alloc;
278 const bool all_storage_propagable = !allocator_traits_type::is_partially_propagable::value ||
279 !allocator_traits_type::storage_is_unpropagable(from_alloc, p);
280 return all_storage_propagable && (propagate_allocator || allocator_traits_type::equal(from_alloc, to_alloc));
281 }
282
283 static bool are_swap_propagable(const allocator_type &l_a, pointer l_p, const allocator_type &r_a, pointer r_p, bool const propagate_allocator)
284 {
285 (void)propagate_allocator; (void)l_p; (void)r_p; (void)l_a; (void)r_a;
286 const bool all_storage_propagable = !allocator_traits_type::is_partially_propagable::value ||
287 !(allocator_traits_type::storage_is_unpropagable(l_a, l_p) || allocator_traits_type::storage_is_unpropagable(r_a, r_p));
288 return all_storage_propagable && (propagate_allocator || allocator_traits_type::equal(l_a, r_a));
289 }
290
291 //Constructor, does not throw
292 vector_alloc_holder()
293 BOOST_NOEXCEPT_IF(dtl::is_nothrow_default_constructible<Allocator>::value)
294 : Allocator(), m_start(), m_size(), m_capacity()
295 {}
296
297 //Constructor, does not throw
298 template<class AllocConvertible>
299 explicit vector_alloc_holder(BOOST_FWD_REF(AllocConvertible) a) BOOST_NOEXCEPT_OR_NOTHROW
300 : Allocator(boost::forward<AllocConvertible>(a)), m_start(), m_size(), m_capacity()
301 {}
302
303 //Constructor, does not throw
304 template<class AllocConvertible>
305 vector_alloc_holder(vector_uninitialized_size_t, BOOST_FWD_REF(AllocConvertible) a, size_type initial_size)
306 : Allocator(boost::forward<AllocConvertible>(a))
307 , m_start()
308 //Size is initialized here so vector should only call uninitialized_xxx after this
309 , m_size(static_cast<stored_size_type>(initial_size))
310 , m_capacity()
311 {
312 if(initial_size){
313 pointer reuse = pointer();
314 size_type final_cap = initial_size;
315 m_start = this->allocation_command(allocate_new, initial_size, final_cap, reuse);
316 m_capacity = static_cast<stored_size_type>(final_cap);
317 }
318 }
319
320 //Constructor, does not throw
321 vector_alloc_holder(vector_uninitialized_size_t, size_type initial_size)
322 : Allocator()
323 , m_start()
324 //Size is initialized here so vector should only call uninitialized_xxx after this
325 , m_size(static_cast<stored_size_type>(initial_size))
326 , m_capacity()
327 {
328 if(initial_size){
329 pointer reuse = pointer();
330 size_type final_cap = initial_size;
331 m_start = this->allocation_command(allocate_new, initial_size, final_cap, reuse);
332 m_capacity = static_cast<stored_size_type>(final_cap);
333 }
334 }
335
336 vector_alloc_holder(BOOST_RV_REF(vector_alloc_holder) holder) BOOST_NOEXCEPT_OR_NOTHROW
337 : Allocator(BOOST_MOVE_BASE(Allocator, holder))
338 , m_start(holder.m_start)
339 , m_size(holder.m_size)
340 , m_capacity(holder.m_capacity)
341 {
342 holder.m_start = pointer();
343 holder.m_size = holder.m_capacity = 0;
344 }
345
346 vector_alloc_holder(initial_capacity_t, pointer p, size_type capacity, BOOST_RV_REF(vector_alloc_holder) holder)
347 : Allocator(BOOST_MOVE_BASE(Allocator, holder))
348 , m_start(p)
349 , m_size(holder.m_size)
350 , m_capacity(static_cast<stored_size_type>(capacity))
351 {
352 allocator_type &this_alloc = this->alloc();
353 allocator_type &x_alloc = holder.alloc();
354 if(this->is_propagable_from(x_alloc, holder.start(), this_alloc, true)){
355 if(this->m_capacity){
356 this->deallocate(this->m_start, this->m_capacity);
357 }
358 m_start = holder.m_start;
359 m_capacity = holder.m_capacity;
360 holder.m_start = pointer();
361 holder.m_capacity = holder.m_size = 0;
362 }
363 else if(this->m_capacity < holder.m_size){
364 size_type const n = holder.m_size;
365 pointer reuse = pointer();
366 size_type final_cap = n;
367 m_start = this->allocation_command(allocate_new, n, final_cap, reuse);
368 m_capacity = static_cast<stored_size_type>(final_cap);
369 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
370 this->num_alloc += n != 0;
371 #endif
372 }
373 }
374
375 vector_alloc_holder(initial_capacity_t, pointer p, size_type n)
376 BOOST_NOEXCEPT_IF(dtl::is_nothrow_default_constructible<Allocator>::value)
377 : Allocator()
378 , m_start(p)
379 , m_size()
380 //n is guaranteed to fit into stored_size_type
381 , m_capacity(static_cast<stored_size_type>(n))
382 {}
383
384 template<class AllocFwd>
385 vector_alloc_holder(initial_capacity_t, pointer p, size_type n, BOOST_FWD_REF(AllocFwd) a)
386 : Allocator(::boost::forward<AllocFwd>(a))
387 , m_start(p)
388 , m_size()
389 , m_capacity(n)
390 {}
391
392 BOOST_CONTAINER_FORCEINLINE ~vector_alloc_holder() BOOST_NOEXCEPT_OR_NOTHROW
393 {
394 if(this->m_capacity){
395 this->deallocate(this->m_start, this->m_capacity);
396 }
397 }
398
399 BOOST_CONTAINER_FORCEINLINE pointer allocation_command(boost::container::allocation_type command,
400 size_type limit_size, size_type &prefer_in_recvd_out_size, pointer &reuse)
401 {
402 typedef typename dtl::version<Allocator>::type alloc_version;
403 return this->priv_allocation_command(alloc_version(), command, limit_size, prefer_in_recvd_out_size, reuse);
404 }
405
406 BOOST_CONTAINER_FORCEINLINE pointer allocate(size_type n)
407 {
408 const size_type max_alloc = allocator_traits_type::max_size(this->alloc());
409 const size_type max = max_alloc <= stored_size_type(-1) ? max_alloc : stored_size_type(-1);
410 if ( max < n )
411 boost::container::throw_length_error("get_next_capacity, allocator's max size reached");
412
413 return allocator_traits_type::allocate(this->alloc(), n);
414 }
415
416 BOOST_CONTAINER_FORCEINLINE void deallocate(const pointer &p, size_type n)
417 {
418 allocator_traits_type::deallocate(this->alloc(), p, n);
419 }
420
421 bool try_expand_fwd(size_type at_least)
422 {
423 //There is not enough memory, try to expand the old one
424 const size_type new_cap = this->capacity() + at_least;
425 size_type real_cap = new_cap;
426 pointer reuse = this->start();
427 bool const success = !!this->allocation_command(expand_fwd, new_cap, real_cap, reuse);
428 //Check for forward expansion
429 if(success){
430 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
431 ++this->num_expand_fwd;
432 #endif
433 this->capacity(real_cap);
434 }
435 return success;
436 }
437
438 template<class GrowthFactorType>
439 size_type next_capacity(size_type additional_objects) const
440 {
441 BOOST_ASSERT(additional_objects > size_type(this->m_capacity - this->m_size));
442 size_type max = allocator_traits_type::max_size(this->alloc());
443 (clamp_by_stored_size_type)(max, stored_size_type());
444 const size_type remaining_cap = max - size_type(this->m_capacity);
445 const size_type min_additional_cap = additional_objects - size_type(this->m_capacity - this->m_size);
446
447 if ( remaining_cap < min_additional_cap )
448 boost::container::throw_length_error("get_next_capacity, allocator's max size reached");
449
450 return GrowthFactorType()( size_type(this->m_capacity), min_additional_cap, max);
451 }
452
453 pointer m_start;
454 stored_size_type m_size;
455 stored_size_type m_capacity;
456
457 void swap_resources(vector_alloc_holder &x) BOOST_NOEXCEPT_OR_NOTHROW
458 {
459 boost::adl_move_swap(this->m_start, x.m_start);
460 boost::adl_move_swap(this->m_size, x.m_size);
461 boost::adl_move_swap(this->m_capacity, x.m_capacity);
462 }
463
464 void steal_resources(vector_alloc_holder &x) BOOST_NOEXCEPT_OR_NOTHROW
465 {
466 this->m_start = x.m_start;
467 this->m_size = x.m_size;
468 this->m_capacity = x.m_capacity;
469 x.m_start = pointer();
470 x.m_size = x.m_capacity = 0;
471 }
472
473 BOOST_CONTAINER_FORCEINLINE Allocator &alloc() BOOST_NOEXCEPT_OR_NOTHROW
474 { return *this; }
475
476 BOOST_CONTAINER_FORCEINLINE const Allocator &alloc() const BOOST_NOEXCEPT_OR_NOTHROW
477 { return *this; }
478
479 BOOST_CONTAINER_FORCEINLINE const pointer &start() const BOOST_NOEXCEPT_OR_NOTHROW
480 { return m_start; }
481 BOOST_CONTAINER_FORCEINLINE size_type capacity() const BOOST_NOEXCEPT_OR_NOTHROW
482 { return m_capacity; }
483 BOOST_CONTAINER_FORCEINLINE void start(const pointer &p) BOOST_NOEXCEPT_OR_NOTHROW
484 { m_start = p; }
485 BOOST_CONTAINER_FORCEINLINE void capacity(const size_type &c) BOOST_NOEXCEPT_OR_NOTHROW
486 { BOOST_ASSERT( c <= stored_size_type(-1)); m_capacity = c; }
487
488 private:
489 void priv_first_allocation(size_type cap)
490 {
491 if(cap){
492 pointer reuse = pointer();
493 m_start = this->allocation_command(allocate_new, cap, cap, reuse);
494 m_capacity = cap;
495 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
496 ++this->num_alloc;
497 #endif
498 }
499 }
500
501 BOOST_CONTAINER_FORCEINLINE static void clamp_by_stored_size_type(size_type &, size_type)
502 {}
503
504 template<class SomeStoredSizeType>
505 BOOST_CONTAINER_FORCEINLINE static void clamp_by_stored_size_type(size_type &s, SomeStoredSizeType)
506 {
507 if (s >= SomeStoredSizeType(-1) )
508 s = SomeStoredSizeType(-1);
509 }
510
511 BOOST_CONTAINER_FORCEINLINE pointer priv_allocation_command(version_1, boost::container::allocation_type command,
512 size_type limit_size,
513 size_type &prefer_in_recvd_out_size,
514 pointer &reuse)
515 {
516 (void)command;
517 BOOST_ASSERT( (command & allocate_new));
518 BOOST_ASSERT(!(command & nothrow_allocation));
519 //First detect overflow on smaller stored_size_types
520 if (limit_size > stored_size_type(-1)){
521 boost::container::throw_length_error("get_next_capacity, allocator's max size reached");
522 }
523 (clamp_by_stored_size_type)(prefer_in_recvd_out_size, stored_size_type());
524 pointer const p = this->allocate(prefer_in_recvd_out_size);
525 reuse = pointer();
526 return p;
527 }
528
529 pointer priv_allocation_command(version_2, boost::container::allocation_type command,
530 size_type limit_size,
531 size_type &prefer_in_recvd_out_size,
532 pointer &reuse)
533 {
534 //First detect overflow on smaller stored_size_types
535 if (limit_size > stored_size_type(-1)){
536 boost::container::throw_length_error("get_next_capacity, allocator's max size reached");
537 }
538 (clamp_by_stored_size_type)(prefer_in_recvd_out_size, stored_size_type());
539 //Allocate memory
540 pointer p = this->alloc().allocation_command(command, limit_size, prefer_in_recvd_out_size, reuse);
541 //If after allocation prefer_in_recvd_out_size is not representable by stored_size_type, truncate it.
542 (clamp_by_stored_size_type)(prefer_in_recvd_out_size, stored_size_type());
543 return p;
544 }
545 };
546
547 //!This struct deallocates and allocated memory
548 template <class Allocator, class StoredSizeType>
549 struct vector_alloc_holder<Allocator, StoredSizeType, version_0>
550 : public Allocator
551 {
552 private:
553 BOOST_MOVABLE_BUT_NOT_COPYABLE(vector_alloc_holder)
554
555 public:
556 typedef boost::container::allocator_traits<Allocator> allocator_traits_type;
557 typedef typename allocator_traits_type::pointer pointer;
558 typedef typename allocator_traits_type::size_type size_type;
559 typedef typename allocator_traits_type::value_type value_type;
560 typedef StoredSizeType stored_size_type;
561
562 template <class OtherAllocator, class OtherStoredSizeType, class OtherAllocatorVersion>
563 friend struct vector_alloc_holder;
564
565 //Constructor, does not throw
566 vector_alloc_holder()
567 BOOST_NOEXCEPT_IF(dtl::is_nothrow_default_constructible<Allocator>::value)
568 : Allocator(), m_size()
569 {}
570
571 //Constructor, does not throw
572 template<class AllocConvertible>
573 explicit vector_alloc_holder(BOOST_FWD_REF(AllocConvertible) a) BOOST_NOEXCEPT_OR_NOTHROW
574 : Allocator(boost::forward<AllocConvertible>(a)), m_size()
575 {}
576
577 //Constructor, does not throw
578 template<class AllocConvertible>
579 vector_alloc_holder(vector_uninitialized_size_t, BOOST_FWD_REF(AllocConvertible) a, size_type initial_size)
580 : Allocator(boost::forward<AllocConvertible>(a))
581 , m_size(initial_size) //Size is initialized here...
582 {
583 //... and capacity here, so vector, must call uninitialized_xxx in the derived constructor
584 this->priv_first_allocation(initial_size);
585 }
586
587 //Constructor, does not throw
588 vector_alloc_holder(vector_uninitialized_size_t, size_type initial_size)
589 : Allocator()
590 , m_size(initial_size) //Size is initialized here...
591 {
592 //... and capacity here, so vector, must call uninitialized_xxx in the derived constructor
593 this->priv_first_allocation(initial_size);
594 }
595
596 vector_alloc_holder(BOOST_RV_REF(vector_alloc_holder) holder)
597 : Allocator(BOOST_MOVE_BASE(Allocator, holder))
598 , m_size(holder.m_size) //Size is initialized here so vector should only call uninitialized_xxx after this
599 {
600 ::boost::container::uninitialized_move_alloc_n
601 (this->alloc(), boost::movelib::to_raw_pointer(holder.start()), m_size, boost::movelib::to_raw_pointer(this->start()));
602 }
603
604 template<class OtherAllocator, class OtherStoredSizeType, class OtherAllocatorVersion>
605 vector_alloc_holder(BOOST_RV_REF_BEG vector_alloc_holder<OtherAllocator, OtherStoredSizeType, OtherAllocatorVersion> BOOST_RV_REF_END holder)
606 : Allocator()
607 , m_size(holder.m_size) //Initialize it to m_size as first_allocation can only succeed or abort
608 {
609 //Different allocator type so we must check we have enough storage
610 const size_type n = holder.m_size;
611 this->priv_first_allocation(n);
612 ::boost::container::uninitialized_move_alloc_n
613 (this->alloc(), boost::movelib::to_raw_pointer(holder.start()), n, boost::movelib::to_raw_pointer(this->start()));
614 }
615
616 BOOST_CONTAINER_FORCEINLINE void priv_first_allocation(size_type cap)
617 {
618 if(cap > Allocator::internal_capacity){
619 throw_bad_alloc();
620 }
621 }
622
623 BOOST_CONTAINER_FORCEINLINE void deep_swap(vector_alloc_holder &x)
624 {
625 this->priv_deep_swap(x);
626 }
627
628 template<class OtherAllocator, class OtherStoredSizeType, class OtherAllocatorVersion>
629 void deep_swap(vector_alloc_holder<OtherAllocator, OtherStoredSizeType, OtherAllocatorVersion> &x)
630 {
631 if(this->m_size > OtherAllocator::internal_capacity || x.m_size > Allocator::internal_capacity){
632 throw_bad_alloc();
633 }
634 this->priv_deep_swap(x);
635 }
636
637 BOOST_CONTAINER_FORCEINLINE void swap_resources(vector_alloc_holder &) BOOST_NOEXCEPT_OR_NOTHROW
638 { //Containers with version 0 allocators can't be moved without moving elements one by one
639 throw_bad_alloc();
640 }
641
642
643 BOOST_CONTAINER_FORCEINLINE void steal_resources(vector_alloc_holder &)
644 { //Containers with version 0 allocators can't be moved without moving elements one by one
645 throw_bad_alloc();
646 }
647
648 BOOST_CONTAINER_FORCEINLINE Allocator &alloc() BOOST_NOEXCEPT_OR_NOTHROW
649 { return *this; }
650
651 BOOST_CONTAINER_FORCEINLINE const Allocator &alloc() const BOOST_NOEXCEPT_OR_NOTHROW
652 { return *this; }
653
654 BOOST_CONTAINER_FORCEINLINE bool try_expand_fwd(size_type at_least)
655 { return !at_least; }
656
657 BOOST_CONTAINER_FORCEINLINE pointer start() const BOOST_NOEXCEPT_OR_NOTHROW { return Allocator::internal_storage(); }
658 BOOST_CONTAINER_FORCEINLINE size_type capacity() const BOOST_NOEXCEPT_OR_NOTHROW { return Allocator::internal_capacity; }
659 stored_size_type m_size;
660
661 private:
662
663 template<class OtherAllocator, class OtherStoredSizeType, class OtherAllocatorVersion>
664 void priv_deep_swap(vector_alloc_holder<OtherAllocator, OtherStoredSizeType, OtherAllocatorVersion> &x)
665 {
666 const size_type MaxTmpStorage = sizeof(value_type)*Allocator::internal_capacity;
667 value_type *const first_this = boost::movelib::to_raw_pointer(this->start());
668 value_type *const first_x = boost::movelib::to_raw_pointer(x.start());
669
670 if(this->m_size < x.m_size){
671 boost::container::deep_swap_alloc_n<MaxTmpStorage>(this->alloc(), first_this, this->m_size, first_x, x.m_size);
672 }
673 else{
674 boost::container::deep_swap_alloc_n<MaxTmpStorage>(this->alloc(), first_x, x.m_size, first_this, this->m_size);
675 }
676 boost::adl_move_swap(this->m_size, x.m_size);
677 }
678 };
679
680 struct growth_factor_60;
681
682 template<class T, class Default>
683 struct default_if_void
684 {
685 typedef T type;
686 };
687
688 template<class Default>
689 struct default_if_void<void, Default>
690 {
691 typedef Default type;
692 };
693
694 template<class Options, class AllocatorSizeType>
695 struct get_vector_opt
696 {
697 typedef vector_opt< typename default_if_void<typename Options::growth_factor_type, growth_factor_60>::type
698 , typename default_if_void<typename Options::stored_size_type, AllocatorSizeType>::type
699 > type;
700 };
701
702 template<class AllocatorSizeType>
703 struct get_vector_opt<void, AllocatorSizeType>
704 {
705 typedef vector_opt<growth_factor_60, AllocatorSizeType> type;
706 };
707
708
709 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
710
711 //! A vector is a sequence that supports random access to elements, constant
712 //! time insertion and removal of elements at the end, and linear time insertion
713 //! and removal of elements at the beginning or in the middle. The number of
714 //! elements in a vector may vary dynamically; memory management is automatic.
715 //!
716 //! \tparam T The type of object that is stored in the vector
717 //! \tparam Allocator The allocator used for all internal memory management
718 //! \tparam Options A type produced from \c boost::container::vector_options.
719 template <class T, class Allocator BOOST_CONTAINER_DOCONLY(= new_allocator<T>), class Options BOOST_CONTAINER_DOCONLY(= void) >
720 class vector
721 {
722 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
723
724 typedef typename boost::container::allocator_traits<Allocator>::size_type alloc_size_type;
725 typedef typename get_vector_opt<Options, alloc_size_type>::type options_type;
726 typedef typename options_type::growth_factor_type growth_factor_type;
727 typedef typename options_type::stored_size_type stored_size_type;
728 typedef value_less<T> value_less_t;
729
730 //If provided the stored_size option must specify a type that is equal or a type that is smaller.
731 BOOST_STATIC_ASSERT( (sizeof(stored_size_type) < sizeof(alloc_size_type) ||
732 dtl::is_same<stored_size_type, alloc_size_type>::value) );
733
734 typedef typename dtl::version<Allocator>::type alloc_version;
735 typedef boost::container::vector_alloc_holder<Allocator, stored_size_type> alloc_holder_t;
736 alloc_holder_t m_holder;
737 typedef allocator_traits<Allocator> allocator_traits_type;
738 template <class U, class UAllocator, class UOptions>
739 friend class vector;
740
741 typedef typename allocator_traits_type::pointer pointer_impl;
742 typedef vec_iterator<pointer_impl, false> iterator_impl;
743 typedef vec_iterator<pointer_impl, true > const_iterator_impl;
744
745 protected:
746 static bool is_propagable_from(const Allocator &from_alloc, pointer_impl p, const Allocator &to_alloc, bool const propagate_allocator)
747 { return alloc_holder_t::is_propagable_from(from_alloc, p, to_alloc, propagate_allocator); }
748
749 static bool are_swap_propagable( const Allocator &l_a, pointer_impl l_p
750 , const Allocator &r_a, pointer_impl r_p, bool const propagate_allocator)
751 { return alloc_holder_t::are_swap_propagable(l_a, l_p, r_a, r_p, propagate_allocator); }
752
753 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
754 public:
755 //////////////////////////////////////////////
756 //
757 // types
758 //
759 //////////////////////////////////////////////
760
761 typedef T value_type;
762 typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
763 typedef typename ::boost::container::allocator_traits<Allocator>::const_pointer const_pointer;
764 typedef typename ::boost::container::allocator_traits<Allocator>::reference reference;
765 typedef typename ::boost::container::allocator_traits<Allocator>::const_reference const_reference;
766 typedef typename ::boost::container::allocator_traits<Allocator>::size_type size_type;
767 typedef typename ::boost::container::allocator_traits<Allocator>::difference_type difference_type;
768 typedef Allocator allocator_type;
769 typedef Allocator stored_allocator_type;
770 typedef BOOST_CONTAINER_IMPDEF(iterator_impl) iterator;
771 typedef BOOST_CONTAINER_IMPDEF(const_iterator_impl) const_iterator;
772 typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<iterator>) reverse_iterator;
773 typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<const_iterator>) const_reverse_iterator;
774
775 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
776 private:
777 BOOST_COPYABLE_AND_MOVABLE(vector)
778 typedef vector_value_traits<Allocator> value_traits;
779 typedef constant_iterator<T, difference_type> cvalue_iterator;
780
781 protected:
782
783 BOOST_CONTAINER_FORCEINLINE void steal_resources(vector &x)
784 { return this->m_holder.steal_resources(x.m_holder); }
785
786 template<class AllocFwd>
787 BOOST_CONTAINER_FORCEINLINE vector(initial_capacity_t, pointer initial_memory, size_type capacity, BOOST_FWD_REF(AllocFwd) a)
788 : m_holder(initial_capacity_t(), initial_memory, capacity, ::boost::forward<AllocFwd>(a))
789 {}
790
791 BOOST_CONTAINER_FORCEINLINE vector(initial_capacity_t, pointer initial_memory, size_type capacity)
792 : m_holder(initial_capacity_t(), initial_memory, capacity)
793 {}
794
795 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
796
797 public:
798 //////////////////////////////////////////////
799 //
800 // construct/copy/destroy
801 //
802 //////////////////////////////////////////////
803
804 //! <b>Effects</b>: Constructs a vector taking the allocator as parameter.
805 //!
806 //! <b>Throws</b>: Nothing.
807 //!
808 //! <b>Complexity</b>: Constant.
809 vector() BOOST_NOEXCEPT_IF(dtl::is_nothrow_default_constructible<Allocator>::value)
810 : m_holder()
811 {}
812
813 //! <b>Effects</b>: Constructs a vector taking the allocator as parameter.
814 //!
815 //! <b>Throws</b>: Nothing
816 //!
817 //! <b>Complexity</b>: Constant.
818 explicit vector(const allocator_type& a) BOOST_NOEXCEPT_OR_NOTHROW
819 : m_holder(a)
820 {}
821
822 //! <b>Effects</b>: Constructs a vector and inserts n value initialized values.
823 //!
824 //! <b>Throws</b>: If allocator_type's allocation
825 //! throws or T's value initialization throws.
826 //!
827 //! <b>Complexity</b>: Linear to n.
828 explicit vector(size_type n)
829 : m_holder(vector_uninitialized_size, n)
830 {
831 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
832 this->num_alloc += n != 0;
833 #endif
834 boost::container::uninitialized_value_init_alloc_n
835 (this->m_holder.alloc(), n, this->priv_raw_begin());
836 }
837
838 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
839 //! and inserts n value initialized values.
840 //!
841 //! <b>Throws</b>: If allocator_type's allocation
842 //! throws or T's value initialization throws.
843 //!
844 //! <b>Complexity</b>: Linear to n.
845 explicit vector(size_type n, const allocator_type &a)
846 : m_holder(vector_uninitialized_size, a, n)
847 {
848 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
849 this->num_alloc += n != 0;
850 #endif
851 boost::container::uninitialized_value_init_alloc_n
852 (this->m_holder.alloc(), n, this->priv_raw_begin());
853 }
854
855 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
856 //! and inserts n default initialized values.
857 //!
858 //! <b>Throws</b>: If allocator_type's allocation
859 //! throws or T's default initialization throws.
860 //!
861 //! <b>Complexity</b>: Linear to n.
862 //!
863 //! <b>Note</b>: Non-standard extension
864 vector(size_type n, default_init_t)
865 : m_holder(vector_uninitialized_size, n)
866 {
867 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
868 this->num_alloc += n != 0;
869 #endif
870 boost::container::uninitialized_default_init_alloc_n
871 (this->m_holder.alloc(), n, this->priv_raw_begin());
872 }
873
874 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
875 //! and inserts n default initialized values.
876 //!
877 //! <b>Throws</b>: If allocator_type's allocation
878 //! throws or T's default initialization throws.
879 //!
880 //! <b>Complexity</b>: Linear to n.
881 //!
882 //! <b>Note</b>: Non-standard extension
883 vector(size_type n, default_init_t, const allocator_type &a)
884 : m_holder(vector_uninitialized_size, a, n)
885 {
886 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
887 this->num_alloc += n != 0;
888 #endif
889 boost::container::uninitialized_default_init_alloc_n
890 (this->m_holder.alloc(), n, this->priv_raw_begin());
891 }
892
893 //! <b>Effects</b>: Constructs a vector
894 //! and inserts n copies of value.
895 //!
896 //! <b>Throws</b>: If allocator_type's allocation
897 //! throws or T's copy constructor throws.
898 //!
899 //! <b>Complexity</b>: Linear to n.
900 vector(size_type n, const T& value)
901 : m_holder(vector_uninitialized_size, n)
902 {
903 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
904 this->num_alloc += n != 0;
905 #endif
906 boost::container::uninitialized_fill_alloc_n
907 (this->m_holder.alloc(), value, n, this->priv_raw_begin());
908 }
909
910 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
911 //! and inserts n copies of value.
912 //!
913 //! <b>Throws</b>: If allocation
914 //! throws or T's copy constructor throws.
915 //!
916 //! <b>Complexity</b>: Linear to n.
917 vector(size_type n, const T& value, const allocator_type& a)
918 : m_holder(vector_uninitialized_size, a, n)
919 {
920 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
921 this->num_alloc += n != 0;
922 #endif
923 boost::container::uninitialized_fill_alloc_n
924 (this->m_holder.alloc(), value, n, this->priv_raw_begin());
925 }
926
927 //! <b>Effects</b>: Constructs a vector
928 //! and inserts a copy of the range [first, last) in the vector.
929 //!
930 //! <b>Throws</b>: If allocator_type's allocation
931 //! throws or T's constructor taking a dereferenced InIt throws.
932 //!
933 //! <b>Complexity</b>: Linear to the range [first, last).
934 template <class InIt>
935 vector(InIt first, InIt last
936 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_c
937 < dtl::is_convertible<InIt BOOST_MOVE_I size_type>::value
938 BOOST_MOVE_I dtl::nat >::type * = 0)
939 )
940 : m_holder()
941 { this->assign(first, last); }
942
943 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
944 //! and inserts a copy of the range [first, last) in the vector.
945 //!
946 //! <b>Throws</b>: If allocator_type's allocation
947 //! throws or T's constructor taking a dereferenced InIt throws.
948 //!
949 //! <b>Complexity</b>: Linear to the range [first, last).
950 template <class InIt>
951 vector(InIt first, InIt last, const allocator_type& a
952 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_c
953 < dtl::is_convertible<InIt BOOST_MOVE_I size_type>::value
954 BOOST_MOVE_I dtl::nat >::type * = 0)
955 )
956 : m_holder(a)
957 { this->assign(first, last); }
958
959 //! <b>Effects</b>: Copy constructs a vector.
960 //!
961 //! <b>Postcondition</b>: x == *this.
962 //!
963 //! <b>Throws</b>: If allocator_type's allocation
964 //! throws or T's copy constructor throws.
965 //!
966 //! <b>Complexity</b>: Linear to the elements x contains.
967 vector(const vector &x)
968 : m_holder( vector_uninitialized_size
969 , allocator_traits_type::select_on_container_copy_construction(x.m_holder.alloc())
970 , x.size())
971 {
972 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
973 this->num_alloc += x.size() != 0;
974 #endif
975 ::boost::container::uninitialized_copy_alloc_n
976 ( this->m_holder.alloc(), x.priv_raw_begin()
977 , x.size(), this->priv_raw_begin());
978 }
979
980 //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
981 //!
982 //! <b>Throws</b>: Nothing
983 //!
984 //! <b>Complexity</b>: Constant.
985 vector(BOOST_RV_REF(vector) x) BOOST_NOEXCEPT_OR_NOTHROW
986 : m_holder(boost::move(x.m_holder))
987 { BOOST_STATIC_ASSERT((!allocator_traits_type::is_partially_propagable::value)); }
988
989 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
990 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
991 //! and inserts a copy of the range [il.begin(), il.last()) in the vector
992 //!
993 //! <b>Throws</b>: If T's constructor taking a dereferenced initializer_list iterator throws.
994 //!
995 //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
996 vector(std::initializer_list<value_type> il, const allocator_type& a = allocator_type())
997 : m_holder(a)
998 {
999 this->assign(il.begin(), il.end());
1000 }
1001 #endif
1002
1003 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1004
1005 //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
1006 //!
1007 //! <b>Throws</b>: If T's move constructor or allocation throws
1008 //!
1009 //! <b>Complexity</b>: Linear.
1010 //!
1011 //! <b>Note</b>: Non-standard extension to support static_vector
1012 template<class OtherAllocator>
1013 vector(BOOST_RV_REF_BEG vector<T, OtherAllocator> BOOST_RV_REF_END x
1014 , typename dtl::enable_if_c
1015 < dtl::is_version<OtherAllocator, 0>::value>::type * = 0
1016 )
1017 : m_holder(boost::move(x.m_holder))
1018 {}
1019
1020 #endif //!defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1021
1022 //! <b>Effects</b>: Copy constructs a vector using the specified allocator.
1023 //!
1024 //! <b>Postcondition</b>: x == *this.
1025 //!
1026 //! <b>Throws</b>: If allocation
1027 //! throws or T's copy constructor throws.
1028 //!
1029 //! <b>Complexity</b>: Linear to the elements x contains.
1030 vector(const vector &x, const allocator_type &a)
1031 : m_holder(vector_uninitialized_size, a, x.size())
1032 {
1033 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
1034 this->num_alloc += x.size() != 0;
1035 #endif
1036 ::boost::container::uninitialized_copy_alloc_n_source
1037 ( this->m_holder.alloc(), x.priv_raw_begin()
1038 , x.size(), this->priv_raw_begin());
1039 }
1040
1041 //! <b>Effects</b>: Move constructor using the specified allocator.
1042 //! Moves x's resources to *this if a == allocator_type().
1043 //! Otherwise copies values from x to *this.
1044 //!
1045 //! <b>Throws</b>: If allocation or T's copy constructor throws.
1046 //!
1047 //! <b>Complexity</b>: Constant if a == x.get_allocator(), linear otherwise.
1048 vector(BOOST_RV_REF(vector) x, const allocator_type &a)
1049 : m_holder( vector_uninitialized_size, a
1050 , is_propagable_from(x.get_stored_allocator(), x.m_holder.start(), a, true) ? 0 : x.size()
1051 )
1052 {
1053 if(is_propagable_from(x.get_stored_allocator(), x.m_holder.start(), a, true)){
1054 this->m_holder.steal_resources(x.m_holder);
1055 }
1056 else{
1057 const size_type n = x.size();
1058 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
1059 this->num_alloc += n != 0;
1060 #endif
1061 ::boost::container::uninitialized_move_alloc_n_source
1062 ( this->m_holder.alloc(), x.priv_raw_begin()
1063 , n, this->priv_raw_begin());
1064 }
1065 }
1066
1067 //! <b>Effects</b>: Destroys the vector. All stored values are destroyed
1068 //! and used memory is deallocated.
1069 //!
1070 //! <b>Throws</b>: Nothing.
1071 //!
1072 //! <b>Complexity</b>: Linear to the number of elements.
1073 ~vector() BOOST_NOEXCEPT_OR_NOTHROW
1074 {
1075 boost::container::destroy_alloc_n
1076 (this->get_stored_allocator(), this->priv_raw_begin(), this->m_holder.m_size);
1077 //vector_alloc_holder deallocates the data
1078 }
1079
1080 //! <b>Effects</b>: Makes *this contain the same elements as x.
1081 //!
1082 //! <b>Postcondition</b>: this->size() == x.size(). *this contains a copy
1083 //! of each of x's elements.
1084 //!
1085 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment throws.
1086 //!
1087 //! <b>Complexity</b>: Linear to the number of elements in x.
1088 BOOST_CONTAINER_FORCEINLINE vector& operator=(BOOST_COPY_ASSIGN_REF(vector) x)
1089 {
1090 if (&x != this){
1091 this->priv_copy_assign(x);
1092 }
1093 return *this;
1094 }
1095
1096 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1097 //! <b>Effects</b>: Make *this container contains elements from il.
1098 //!
1099 //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
1100 BOOST_CONTAINER_FORCEINLINE vector& operator=(std::initializer_list<value_type> il)
1101 {
1102 this->assign(il.begin(), il.end());
1103 return *this;
1104 }
1105 #endif
1106
1107 //! <b>Effects</b>: Move assignment. All x's values are transferred to *this.
1108 //!
1109 //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
1110 //! before the function.
1111 //!
1112 //! <b>Throws</b>: If allocator_traits_type::propagate_on_container_move_assignment
1113 //! is false and (allocation throws or value_type's move constructor throws)
1114 //!
1115 //! <b>Complexity</b>: Constant if allocator_traits_type::
1116 //! propagate_on_container_move_assignment is true or
1117 //! this->get>allocator() == x.get_allocator(). Linear otherwise.
1118 BOOST_CONTAINER_FORCEINLINE vector& operator=(BOOST_RV_REF(vector) x)
1119 BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value
1120 || allocator_traits_type::is_always_equal::value)
1121 {
1122 BOOST_ASSERT(&x != this);
1123 this->priv_move_assign(boost::move(x));
1124 return *this;
1125 }
1126
1127 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1128
1129 //! <b>Effects</b>: Move assignment. All x's values are transferred to *this.
1130 //!
1131 //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
1132 //! before the function.
1133 //!
1134 //! <b>Throws</b>: If move constructor/assignment of T throws or allocation throws
1135 //!
1136 //! <b>Complexity</b>: Linear.
1137 //!
1138 //! <b>Note</b>: Non-standard extension to support static_vector
1139 template<class OtherAllocator>
1140 BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_and
1141 < vector&
1142 , dtl::is_version<OtherAllocator, 0>
1143 , dtl::is_different<OtherAllocator, allocator_type>
1144 >::type
1145 operator=(BOOST_RV_REF_BEG vector<value_type, OtherAllocator> BOOST_RV_REF_END x)
1146 {
1147 this->priv_move_assign(boost::move(x));
1148 return *this;
1149 }
1150
1151 //! <b>Effects</b>: Copy assignment. All x's values are copied to *this.
1152 //!
1153 //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
1154 //! before the function.
1155 //!
1156 //! <b>Throws</b>: If move constructor/assignment of T throws or allocation throws
1157 //!
1158 //! <b>Complexity</b>: Linear.
1159 //!
1160 //! <b>Note</b>: Non-standard extension to support static_vector
1161 template<class OtherAllocator>
1162 BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_and
1163 < vector&
1164 , dtl::is_version<OtherAllocator, 0>
1165 , dtl::is_different<OtherAllocator, allocator_type>
1166 >::type
1167 operator=(const vector<value_type, OtherAllocator> &x)
1168 {
1169 this->priv_copy_assign(x);
1170 return *this;
1171 }
1172
1173 #endif
1174
1175 //! <b>Effects</b>: Assigns the the range [first, last) to *this.
1176 //!
1177 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment or
1178 //! T's constructor/assignment from dereferencing InpIt throws.
1179 //!
1180 //! <b>Complexity</b>: Linear to n.
1181 template <class InIt>
1182 void assign(InIt first, InIt last
1183 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_or
1184 < void
1185 BOOST_MOVE_I dtl::is_convertible<InIt BOOST_MOVE_I size_type>
1186 BOOST_MOVE_I dtl::and_
1187 < dtl::is_different<alloc_version BOOST_MOVE_I version_0>
1188 BOOST_MOVE_I dtl::is_not_input_iterator<InIt>
1189 >
1190 >::type * = 0)
1191 )
1192 {
1193 //Overwrite all elements we can from [first, last)
1194 iterator cur = this->begin();
1195 const iterator end_it = this->end();
1196 for ( ; first != last && cur != end_it; ++cur, ++first){
1197 *cur = *first;
1198 }
1199
1200 if (first == last){
1201 //There are no more elements in the sequence, erase remaining
1202 T* const end_pos = this->priv_raw_end();
1203 const size_type n = static_cast<size_type>(end_pos - boost::movelib::iterator_to_raw_pointer(cur));
1204 this->priv_destroy_last_n(n);
1205 }
1206 else{
1207 //There are more elements in the range, insert the remaining ones
1208 this->insert(this->cend(), first, last);
1209 }
1210 }
1211
1212 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1213 //! <b>Effects</b>: Assigns the the range [il.begin(), il.end()) to *this.
1214 //!
1215 //! <b>Throws</b>: If memory allocation throws or
1216 //! T's constructor from dereferencing iniializer_list iterator throws.
1217 //!
1218 BOOST_CONTAINER_FORCEINLINE void assign(std::initializer_list<T> il)
1219 {
1220 this->assign(il.begin(), il.end());
1221 }
1222 #endif
1223
1224 //! <b>Effects</b>: Assigns the the range [first, last) to *this.
1225 //!
1226 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment or
1227 //! T's constructor/assignment from dereferencing InpIt throws.
1228 //!
1229 //! <b>Complexity</b>: Linear to n.
1230 template <class FwdIt>
1231 void assign(FwdIt first, FwdIt last
1232 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_or
1233 < void
1234 BOOST_MOVE_I dtl::is_same<alloc_version BOOST_MOVE_I version_0>
1235 BOOST_MOVE_I dtl::is_convertible<FwdIt BOOST_MOVE_I size_type>
1236 BOOST_MOVE_I dtl::is_input_iterator<FwdIt>
1237 >::type * = 0)
1238 )
1239 {
1240 //For Fwd iterators the standard only requires EmplaceConstructible and assignable from *first
1241 //so we can't do any backwards allocation
1242 const size_type input_sz = static_cast<size_type>(boost::container::iterator_distance(first, last));
1243 const size_type old_capacity = this->capacity();
1244 if(input_sz > old_capacity){ //If input range is too big, we need to reallocate
1245 size_type real_cap = 0;
1246 pointer reuse(this->m_holder.start());
1247 pointer const ret(this->m_holder.allocation_command(allocate_new|expand_fwd, input_sz, real_cap = input_sz, reuse));
1248 if(!reuse){ //New allocation, just emplace new values
1249 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
1250 ++this->num_alloc;
1251 #endif
1252 pointer const old_p = this->m_holder.start();
1253 if(old_p){
1254 this->priv_destroy_all();
1255 this->m_holder.deallocate(old_p, old_capacity);
1256 }
1257 this->m_holder.start(ret);
1258 this->m_holder.capacity(real_cap);
1259 this->m_holder.m_size = 0;
1260 this->priv_uninitialized_construct_at_end(first, last);
1261 return;
1262 }
1263 else{
1264 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
1265 ++this->num_expand_fwd;
1266 #endif
1267 this->m_holder.capacity(real_cap);
1268 //Forward expansion, use assignment + back deletion/construction that comes later
1269 }
1270 }
1271 //Overwrite all elements we can from [first, last)
1272 iterator cur = this->begin();
1273 const iterator end_it = this->end();
1274 for ( ; first != last && cur != end_it; ++cur, ++first){
1275 *cur = *first;
1276 }
1277
1278 if (first == last){
1279 //There are no more elements in the sequence, erase remaining
1280 this->priv_destroy_last_n(this->size() - input_sz);
1281 }
1282 else{
1283 //Uninitialized construct at end the remaining range
1284 this->priv_uninitialized_construct_at_end(first, last);
1285 }
1286 }
1287
1288 //! <b>Effects</b>: Assigns the n copies of val to *this.
1289 //!
1290 //! <b>Throws</b>: If memory allocation throws or
1291 //! T's copy/move constructor/assignment throws.
1292 //!
1293 //! <b>Complexity</b>: Linear to n.
1294 BOOST_CONTAINER_FORCEINLINE void assign(size_type n, const value_type& val)
1295 { this->assign(cvalue_iterator(val, n), cvalue_iterator()); }
1296
1297 //! <b>Effects</b>: Returns a copy of the internal allocator.
1298 //!
1299 //! <b>Throws</b>: If allocator's copy constructor throws.
1300 //!
1301 //! <b>Complexity</b>: Constant.
1302 allocator_type get_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
1303 { return this->m_holder.alloc(); }
1304
1305 //! <b>Effects</b>: Returns a reference to the internal allocator.
1306 //!
1307 //! <b>Throws</b>: Nothing
1308 //!
1309 //! <b>Complexity</b>: Constant.
1310 //!
1311 //! <b>Note</b>: Non-standard extension.
1312 BOOST_CONTAINER_FORCEINLINE stored_allocator_type &get_stored_allocator() BOOST_NOEXCEPT_OR_NOTHROW
1313 { return this->m_holder.alloc(); }
1314
1315 //! <b>Effects</b>: Returns a reference to the internal allocator.
1316 //!
1317 //! <b>Throws</b>: Nothing
1318 //!
1319 //! <b>Complexity</b>: Constant.
1320 //!
1321 //! <b>Note</b>: Non-standard extension.
1322 BOOST_CONTAINER_FORCEINLINE const stored_allocator_type &get_stored_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
1323 { return this->m_holder.alloc(); }
1324
1325 //////////////////////////////////////////////
1326 //
1327 // iterators
1328 //
1329 //////////////////////////////////////////////
1330
1331 //! <b>Effects</b>: Returns an iterator to the first element contained in the vector.
1332 //!
1333 //! <b>Throws</b>: Nothing.
1334 //!
1335 //! <b>Complexity</b>: Constant.
1336 BOOST_CONTAINER_FORCEINLINE iterator begin() BOOST_NOEXCEPT_OR_NOTHROW
1337 { return iterator(this->m_holder.start()); }
1338
1339 //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
1340 //!
1341 //! <b>Throws</b>: Nothing.
1342 //!
1343 //! <b>Complexity</b>: Constant.
1344 BOOST_CONTAINER_FORCEINLINE const_iterator begin() const BOOST_NOEXCEPT_OR_NOTHROW
1345 { return const_iterator(this->m_holder.start()); }
1346
1347 //! <b>Effects</b>: Returns an iterator to the end of the vector.
1348 //!
1349 //! <b>Throws</b>: Nothing.
1350 //!
1351 //! <b>Complexity</b>: Constant.
1352 BOOST_CONTAINER_FORCEINLINE iterator end() BOOST_NOEXCEPT_OR_NOTHROW
1353 { return iterator(this->m_holder.start() + this->m_holder.m_size); }
1354
1355 //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
1356 //!
1357 //! <b>Throws</b>: Nothing.
1358 //!
1359 //! <b>Complexity</b>: Constant.
1360 BOOST_CONTAINER_FORCEINLINE const_iterator end() const BOOST_NOEXCEPT_OR_NOTHROW
1361 { return this->cend(); }
1362
1363 //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
1364 //! of the reversed vector.
1365 //!
1366 //! <b>Throws</b>: Nothing.
1367 //!
1368 //! <b>Complexity</b>: Constant.
1369 BOOST_CONTAINER_FORCEINLINE reverse_iterator rbegin() BOOST_NOEXCEPT_OR_NOTHROW
1370 { return reverse_iterator(this->end()); }
1371
1372 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
1373 //! of the reversed vector.
1374 //!
1375 //! <b>Throws</b>: Nothing.
1376 //!
1377 //! <b>Complexity</b>: Constant.
1378 BOOST_CONTAINER_FORCEINLINE const_reverse_iterator rbegin() const BOOST_NOEXCEPT_OR_NOTHROW
1379 { return this->crbegin(); }
1380
1381 //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
1382 //! of the reversed vector.
1383 //!
1384 //! <b>Throws</b>: Nothing.
1385 //!
1386 //! <b>Complexity</b>: Constant.
1387 BOOST_CONTAINER_FORCEINLINE reverse_iterator rend() BOOST_NOEXCEPT_OR_NOTHROW
1388 { return reverse_iterator(this->begin()); }
1389
1390 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
1391 //! of the reversed vector.
1392 //!
1393 //! <b>Throws</b>: Nothing.
1394 //!
1395 //! <b>Complexity</b>: Constant.
1396 BOOST_CONTAINER_FORCEINLINE const_reverse_iterator rend() const BOOST_NOEXCEPT_OR_NOTHROW
1397 { return this->crend(); }
1398
1399 //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
1400 //!
1401 //! <b>Throws</b>: Nothing.
1402 //!
1403 //! <b>Complexity</b>: Constant.
1404 BOOST_CONTAINER_FORCEINLINE const_iterator cbegin() const BOOST_NOEXCEPT_OR_NOTHROW
1405 { return const_iterator(this->m_holder.start()); }
1406
1407 //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
1408 //!
1409 //! <b>Throws</b>: Nothing.
1410 //!
1411 //! <b>Complexity</b>: Constant.
1412 BOOST_CONTAINER_FORCEINLINE const_iterator cend() const BOOST_NOEXCEPT_OR_NOTHROW
1413 { return const_iterator(this->m_holder.start() + this->m_holder.m_size); }
1414
1415 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
1416 //! of the reversed vector.
1417 //!
1418 //! <b>Throws</b>: Nothing.
1419 //!
1420 //! <b>Complexity</b>: Constant.
1421 BOOST_CONTAINER_FORCEINLINE const_reverse_iterator crbegin() const BOOST_NOEXCEPT_OR_NOTHROW
1422 { return const_reverse_iterator(this->end());}
1423
1424 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
1425 //! of the reversed vector.
1426 //!
1427 //! <b>Throws</b>: Nothing.
1428 //!
1429 //! <b>Complexity</b>: Constant.
1430 BOOST_CONTAINER_FORCEINLINE const_reverse_iterator crend() const BOOST_NOEXCEPT_OR_NOTHROW
1431 { return const_reverse_iterator(this->begin()); }
1432
1433 //////////////////////////////////////////////
1434 //
1435 // capacity
1436 //
1437 //////////////////////////////////////////////
1438
1439 //! <b>Effects</b>: Returns true if the vector contains no elements.
1440 //!
1441 //! <b>Throws</b>: Nothing.
1442 //!
1443 //! <b>Complexity</b>: Constant.
1444 BOOST_CONTAINER_FORCEINLINE bool empty() const BOOST_NOEXCEPT_OR_NOTHROW
1445 { return !this->m_holder.m_size; }
1446
1447 //! <b>Effects</b>: Returns the number of the elements contained in the vector.
1448 //!
1449 //! <b>Throws</b>: Nothing.
1450 //!
1451 //! <b>Complexity</b>: Constant.
1452 BOOST_CONTAINER_FORCEINLINE size_type size() const BOOST_NOEXCEPT_OR_NOTHROW
1453 { return this->m_holder.m_size; }
1454
1455 //! <b>Effects</b>: Returns the largest possible size of the vector.
1456 //!
1457 //! <b>Throws</b>: Nothing.
1458 //!
1459 //! <b>Complexity</b>: Constant.
1460 BOOST_CONTAINER_FORCEINLINE size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
1461 { return allocator_traits_type::max_size(this->m_holder.alloc()); }
1462
1463 //! <b>Effects</b>: Inserts or erases elements at the end such that
1464 //! the size becomes n. New elements are value initialized.
1465 //!
1466 //! <b>Throws</b>: If memory allocation throws, or T's copy/move or value initialization throws.
1467 //!
1468 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1469 void resize(size_type new_size)
1470 { this->priv_resize(new_size, value_init); }
1471
1472 //! <b>Effects</b>: Inserts or erases elements at the end such that
1473 //! the size becomes n. New elements are default initialized.
1474 //!
1475 //! <b>Throws</b>: If memory allocation throws, or T's copy/move or default initialization throws.
1476 //!
1477 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1478 //!
1479 //! <b>Note</b>: Non-standard extension
1480 void resize(size_type new_size, default_init_t)
1481 { this->priv_resize(new_size, default_init); }
1482
1483 //! <b>Effects</b>: Inserts or erases elements at the end such that
1484 //! the size becomes n. New elements are copy constructed from x.
1485 //!
1486 //! <b>Throws</b>: If memory allocation throws, or T's copy/move constructor throws.
1487 //!
1488 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1489 void resize(size_type new_size, const T& x)
1490 { this->priv_resize(new_size, x); }
1491
1492 //! <b>Effects</b>: Number of elements for which memory has been allocated.
1493 //! capacity() is always greater than or equal to size().
1494 //!
1495 //! <b>Throws</b>: Nothing.
1496 //!
1497 //! <b>Complexity</b>: Constant.
1498 BOOST_CONTAINER_FORCEINLINE size_type capacity() const BOOST_NOEXCEPT_OR_NOTHROW
1499 { return this->m_holder.capacity(); }
1500
1501 //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
1502 //! effect. Otherwise, it is a request for allocation of additional memory.
1503 //! If the request is successful, then capacity() is greater than or equal to
1504 //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
1505 //!
1506 //! <b>Throws</b>: If memory allocation allocation throws or T's copy/move constructor throws.
1507 BOOST_CONTAINER_FORCEINLINE void reserve(size_type new_cap)
1508 {
1509 if (this->capacity() < new_cap){
1510 this->priv_reserve_no_capacity(new_cap, alloc_version());
1511 }
1512 }
1513
1514 //! <b>Effects</b>: Tries to deallocate the excess of memory created
1515 //! with previous allocations. The size of the vector is unchanged
1516 //!
1517 //! <b>Throws</b>: If memory allocation throws, or T's copy/move constructor throws.
1518 //!
1519 //! <b>Complexity</b>: Linear to size().
1520 BOOST_CONTAINER_FORCEINLINE void shrink_to_fit()
1521 { this->priv_shrink_to_fit(alloc_version()); }
1522
1523 //////////////////////////////////////////////
1524 //
1525 // element access
1526 //
1527 //////////////////////////////////////////////
1528
1529 //! <b>Requires</b>: !empty()
1530 //!
1531 //! <b>Effects</b>: Returns a reference to the first
1532 //! element of the container.
1533 //!
1534 //! <b>Throws</b>: Nothing.
1535 //!
1536 //! <b>Complexity</b>: Constant.
1537 reference front() BOOST_NOEXCEPT_OR_NOTHROW
1538 {
1539 BOOST_ASSERT(!this->empty());
1540 return *this->m_holder.start();
1541 }
1542
1543 //! <b>Requires</b>: !empty()
1544 //!
1545 //! <b>Effects</b>: Returns a const reference to the first
1546 //! element of the container.
1547 //!
1548 //! <b>Throws</b>: Nothing.
1549 //!
1550 //! <b>Complexity</b>: Constant.
1551 const_reference front() const BOOST_NOEXCEPT_OR_NOTHROW
1552 {
1553 BOOST_ASSERT(!this->empty());
1554 return *this->m_holder.start();
1555 }
1556
1557 //! <b>Requires</b>: !empty()
1558 //!
1559 //! <b>Effects</b>: Returns a reference to the last
1560 //! element of the container.
1561 //!
1562 //! <b>Throws</b>: Nothing.
1563 //!
1564 //! <b>Complexity</b>: Constant.
1565 reference back() BOOST_NOEXCEPT_OR_NOTHROW
1566 {
1567 BOOST_ASSERT(!this->empty());
1568 return this->m_holder.start()[this->m_holder.m_size - 1];
1569 }
1570
1571 //! <b>Requires</b>: !empty()
1572 //!
1573 //! <b>Effects</b>: Returns a const reference to the last
1574 //! element of the container.
1575 //!
1576 //! <b>Throws</b>: Nothing.
1577 //!
1578 //! <b>Complexity</b>: Constant.
1579 const_reference back() const BOOST_NOEXCEPT_OR_NOTHROW
1580 {
1581 BOOST_ASSERT(!this->empty());
1582 return this->m_holder.start()[this->m_holder.m_size - 1];
1583 }
1584
1585 //! <b>Requires</b>: size() > n.
1586 //!
1587 //! <b>Effects</b>: Returns a reference to the nth element
1588 //! from the beginning of the container.
1589 //!
1590 //! <b>Throws</b>: Nothing.
1591 //!
1592 //! <b>Complexity</b>: Constant.
1593 reference operator[](size_type n) BOOST_NOEXCEPT_OR_NOTHROW
1594 {
1595 BOOST_ASSERT(this->m_holder.m_size > n);
1596 return this->m_holder.start()[n];
1597 }
1598
1599 //! <b>Requires</b>: size() > n.
1600 //!
1601 //! <b>Effects</b>: Returns a const reference to the nth element
1602 //! from the beginning of the container.
1603 //!
1604 //! <b>Throws</b>: Nothing.
1605 //!
1606 //! <b>Complexity</b>: Constant.
1607 const_reference operator[](size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
1608 {
1609 BOOST_ASSERT(this->m_holder.m_size > n);
1610 return this->m_holder.start()[n];
1611 }
1612
1613 //! <b>Requires</b>: size() >= n.
1614 //!
1615 //! <b>Effects</b>: Returns an iterator to the nth element
1616 //! from the beginning of the container. Returns end()
1617 //! if n == size().
1618 //!
1619 //! <b>Throws</b>: Nothing.
1620 //!
1621 //! <b>Complexity</b>: Constant.
1622 //!
1623 //! <b>Note</b>: Non-standard extension
1624 iterator nth(size_type n) BOOST_NOEXCEPT_OR_NOTHROW
1625 {
1626 BOOST_ASSERT(this->m_holder.m_size >= n);
1627 return iterator(this->m_holder.start()+n);
1628 }
1629
1630 //! <b>Requires</b>: size() >= n.
1631 //!
1632 //! <b>Effects</b>: Returns a const_iterator to the nth element
1633 //! from the beginning of the container. Returns end()
1634 //! if n == size().
1635 //!
1636 //! <b>Throws</b>: Nothing.
1637 //!
1638 //! <b>Complexity</b>: Constant.
1639 //!
1640 //! <b>Note</b>: Non-standard extension
1641 const_iterator nth(size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
1642 {
1643 BOOST_ASSERT(this->m_holder.m_size >= n);
1644 return const_iterator(this->m_holder.start()+n);
1645 }
1646
1647 //! <b>Requires</b>: begin() <= p <= end().
1648 //!
1649 //! <b>Effects</b>: Returns the index of the element pointed by p
1650 //! and size() if p == end().
1651 //!
1652 //! <b>Throws</b>: Nothing.
1653 //!
1654 //! <b>Complexity</b>: Constant.
1655 //!
1656 //! <b>Note</b>: Non-standard extension
1657 size_type index_of(iterator p) BOOST_NOEXCEPT_OR_NOTHROW
1658 {
1659 //Range check assert done in priv_index_of
1660 return this->priv_index_of(vector_iterator_get_ptr(p));
1661 }
1662
1663 //! <b>Requires</b>: begin() <= p <= end().
1664 //!
1665 //! <b>Effects</b>: Returns the index of the element pointed by p
1666 //! and size() if p == end().
1667 //!
1668 //! <b>Throws</b>: Nothing.
1669 //!
1670 //! <b>Complexity</b>: Constant.
1671 //!
1672 //! <b>Note</b>: Non-standard extension
1673 size_type index_of(const_iterator p) const BOOST_NOEXCEPT_OR_NOTHROW
1674 {
1675 //Range check assert done in priv_index_of
1676 return this->priv_index_of(vector_iterator_get_ptr(p));
1677 }
1678
1679 //! <b>Requires</b>: size() > n.
1680 //!
1681 //! <b>Effects</b>: Returns a reference to the nth element
1682 //! from the beginning of the container.
1683 //!
1684 //! <b>Throws</b>: std::range_error if n >= size()
1685 //!
1686 //! <b>Complexity</b>: Constant.
1687 reference at(size_type n)
1688 {
1689 this->priv_throw_if_out_of_range(n);
1690 return this->m_holder.start()[n];
1691 }
1692
1693 //! <b>Requires</b>: size() > n.
1694 //!
1695 //! <b>Effects</b>: Returns a const reference to the nth element
1696 //! from the beginning of the container.
1697 //!
1698 //! <b>Throws</b>: std::range_error if n >= size()
1699 //!
1700 //! <b>Complexity</b>: Constant.
1701 const_reference at(size_type n) const
1702 {
1703 this->priv_throw_if_out_of_range(n);
1704 return this->m_holder.start()[n];
1705 }
1706
1707 //////////////////////////////////////////////
1708 //
1709 // data access
1710 //
1711 //////////////////////////////////////////////
1712
1713 //! <b>Returns</b>: A pointer such that [data(),data() + size()) is a valid range.
1714 //! For a non-empty vector, data() == &front().
1715 //!
1716 //! <b>Throws</b>: Nothing.
1717 //!
1718 //! <b>Complexity</b>: Constant.
1719 T* data() BOOST_NOEXCEPT_OR_NOTHROW
1720 { return this->priv_raw_begin(); }
1721
1722 //! <b>Returns</b>: A pointer such that [data(),data() + size()) is a valid range.
1723 //! For a non-empty vector, data() == &front().
1724 //!
1725 //! <b>Throws</b>: Nothing.
1726 //!
1727 //! <b>Complexity</b>: Constant.
1728 const T * data() const BOOST_NOEXCEPT_OR_NOTHROW
1729 { return this->priv_raw_begin(); }
1730
1731 //////////////////////////////////////////////
1732 //
1733 // modifiers
1734 //
1735 //////////////////////////////////////////////
1736
1737 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1738 //! <b>Effects</b>: Inserts an object of type T constructed with
1739 //! std::forward<Args>(args)... in the end of the vector.
1740 //!
1741 //! <b>Returns</b>: A reference to the created object.
1742 //!
1743 //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws or
1744 //! T's copy/move constructor throws.
1745 //!
1746 //! <b>Complexity</b>: Amortized constant time.
1747 template<class ...Args>
1748 BOOST_CONTAINER_FORCEINLINE reference emplace_back(BOOST_FWD_REF(Args)...args)
1749 {
1750 if (BOOST_LIKELY(this->room_enough())){
1751 //There is more memory, just construct a new object at the end
1752 T* const p = this->priv_raw_end();
1753 allocator_traits_type::construct(this->m_holder.alloc(), p, ::boost::forward<Args>(args)...);
1754 ++this->m_holder.m_size;
1755 return *p;
1756 }
1757 else{
1758 typedef dtl::insert_emplace_proxy<Allocator, T*, Args...> type;
1759 return *this->priv_forward_range_insert_no_capacity
1760 (this->back_ptr(), 1, type(::boost::forward<Args>(args)...), alloc_version());
1761 }
1762 }
1763
1764 //! <b>Effects</b>: Inserts an object of type T constructed with
1765 //! std::forward<Args>(args)... in the end of the vector.
1766 //!
1767 //! <b>Throws</b>: If the in-place constructor throws.
1768 //!
1769 //! <b>Complexity</b>: Constant time.
1770 //!
1771 //! <b>Note</b>: Non-standard extension.
1772 template<class ...Args>
1773 BOOST_CONTAINER_FORCEINLINE bool stable_emplace_back(BOOST_FWD_REF(Args)...args)
1774 {
1775 const bool is_room_enough = this->room_enough() || (alloc_version::value == 2 && this->m_holder.try_expand_fwd(1u));
1776 if (BOOST_LIKELY(is_room_enough)){
1777 //There is more memory, just construct a new object at the end
1778 allocator_traits_type::construct(this->m_holder.alloc(), this->priv_raw_end(), ::boost::forward<Args>(args)...);
1779 ++this->m_holder.m_size;
1780 }
1781 return is_room_enough;
1782 }
1783
1784 //! <b>Requires</b>: position must be a valid iterator of *this.
1785 //!
1786 //! <b>Effects</b>: Inserts an object of type T constructed with
1787 //! std::forward<Args>(args)... before position
1788 //!
1789 //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws or
1790 //! T's copy/move constructor/assignment throws.
1791 //!
1792 //! <b>Complexity</b>: If position is end(), amortized constant time
1793 //! Linear time otherwise.
1794 template<class ...Args>
1795 iterator emplace(const_iterator position, BOOST_FWD_REF(Args) ...args)
1796 {
1797 BOOST_ASSERT(this->priv_in_range_or_end(position));
1798 //Just call more general insert(pos, size, value) and return iterator
1799 typedef dtl::insert_emplace_proxy<Allocator, T*, Args...> type;
1800 return this->priv_forward_range_insert( vector_iterator_get_ptr(position), 1
1801 , type(::boost::forward<Args>(args)...));
1802 }
1803
1804 #else // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
1805
1806 #define BOOST_CONTAINER_VECTOR_EMPLACE_CODE(N) \
1807 BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
1808 BOOST_CONTAINER_FORCEINLINE reference emplace_back(BOOST_MOVE_UREF##N)\
1809 {\
1810 if (BOOST_LIKELY(this->room_enough())){\
1811 T* const p = this->priv_raw_end();\
1812 allocator_traits_type::construct (this->m_holder.alloc()\
1813 , this->priv_raw_end() BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
1814 ++this->m_holder.m_size;\
1815 return *p;\
1816 }\
1817 else{\
1818 typedef dtl::insert_emplace_proxy_arg##N<Allocator, T* BOOST_MOVE_I##N BOOST_MOVE_TARG##N> type;\
1819 return *this->priv_forward_range_insert_no_capacity\
1820 ( this->back_ptr(), 1, type(BOOST_MOVE_FWD##N), alloc_version());\
1821 }\
1822 }\
1823 \
1824 BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
1825 BOOST_CONTAINER_FORCEINLINE bool stable_emplace_back(BOOST_MOVE_UREF##N)\
1826 {\
1827 const bool is_room_enough = this->room_enough() || (alloc_version::value == 2 && this->m_holder.try_expand_fwd(1u));\
1828 if (BOOST_LIKELY(is_room_enough)){\
1829 allocator_traits_type::construct (this->m_holder.alloc()\
1830 , this->priv_raw_end() BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
1831 ++this->m_holder.m_size;\
1832 }\
1833 return is_room_enough;\
1834 }\
1835 \
1836 BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
1837 iterator emplace(const_iterator pos BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
1838 {\
1839 BOOST_ASSERT(this->priv_in_range_or_end(pos));\
1840 typedef dtl::insert_emplace_proxy_arg##N<Allocator, T* BOOST_MOVE_I##N BOOST_MOVE_TARG##N> type;\
1841 return this->priv_forward_range_insert(vector_iterator_get_ptr(pos), 1, type(BOOST_MOVE_FWD##N));\
1842 }\
1843 //
1844 BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_VECTOR_EMPLACE_CODE)
1845 #undef BOOST_CONTAINER_VECTOR_EMPLACE_CODE
1846
1847 #endif
1848
1849 #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1850 //! <b>Effects</b>: Inserts a copy of x at the end of the vector.
1851 //!
1852 //! <b>Throws</b>: If memory allocation throws or
1853 //! T's copy/move constructor throws.
1854 //!
1855 //! <b>Complexity</b>: Amortized constant time.
1856 void push_back(const T &x);
1857
1858 //! <b>Effects</b>: Constructs a new element in the end of the vector
1859 //! and moves the resources of x to this new element.
1860 //!
1861 //! <b>Throws</b>: If memory allocation throws or
1862 //! T's copy/move constructor throws.
1863 //!
1864 //! <b>Complexity</b>: Amortized constant time.
1865 void push_back(T &&x);
1866 #else
1867 BOOST_MOVE_CONVERSION_AWARE_CATCH(push_back, T, void, priv_push_back)
1868 #endif
1869
1870 #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1871 //! <b>Requires</b>: position must be a valid iterator of *this.
1872 //!
1873 //! <b>Effects</b>: Insert a copy of x before position.
1874 //!
1875 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment throws.
1876 //!
1877 //! <b>Complexity</b>: If position is end(), amortized constant time
1878 //! Linear time otherwise.
1879 iterator insert(const_iterator position, const T &x);
1880
1881 //! <b>Requires</b>: position must be a valid iterator of *this.
1882 //!
1883 //! <b>Effects</b>: Insert a new element before position with x's resources.
1884 //!
1885 //! <b>Throws</b>: If memory allocation throws.
1886 //!
1887 //! <b>Complexity</b>: If position is end(), amortized constant time
1888 //! Linear time otherwise.
1889 iterator insert(const_iterator position, T &&x);
1890 #else
1891 BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, T, iterator, priv_insert, const_iterator, const_iterator)
1892 #endif
1893
1894 //! <b>Requires</b>: p must be a valid iterator of *this.
1895 //!
1896 //! <b>Effects</b>: Insert n copies of x before pos.
1897 //!
1898 //! <b>Returns</b>: an iterator to the first inserted element or p if n is 0.
1899 //!
1900 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor throws.
1901 //!
1902 //! <b>Complexity</b>: Linear to n.
1903 iterator insert(const_iterator p, size_type n, const T& x)
1904 {
1905 BOOST_ASSERT(this->priv_in_range_or_end(p));
1906 dtl::insert_n_copies_proxy<Allocator, T*> proxy(x);
1907 return this->priv_forward_range_insert(vector_iterator_get_ptr(p), n, proxy);
1908 }
1909
1910 //! <b>Requires</b>: p must be a valid iterator of *this.
1911 //!
1912 //! <b>Effects</b>: Insert a copy of the [first, last) range before pos.
1913 //!
1914 //! <b>Returns</b>: an iterator to the first inserted element or pos if first == last.
1915 //!
1916 //! <b>Throws</b>: If memory allocation throws, T's constructor from a
1917 //! dereferenced InpIt throws or T's copy/move constructor/assignment throws.
1918 //!
1919 //! <b>Complexity</b>: Linear to boost::container::iterator_distance [first, last).
1920 template <class InIt>
1921 iterator insert(const_iterator pos, InIt first, InIt last
1922 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1923 , typename dtl::disable_if_or
1924 < void
1925 , dtl::is_convertible<InIt, size_type>
1926 , dtl::is_not_input_iterator<InIt>
1927 >::type * = 0
1928 #endif
1929 )
1930 {
1931 BOOST_ASSERT(this->priv_in_range_or_end(pos));
1932 const size_type n_pos = pos - this->cbegin();
1933 iterator it(vector_iterator_get_ptr(pos));
1934 for(;first != last; ++first){
1935 it = this->emplace(it, *first);
1936 ++it;
1937 }
1938 return iterator(this->m_holder.start() + n_pos);
1939 }
1940
1941 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1942 template <class FwdIt>
1943 iterator insert(const_iterator pos, FwdIt first, FwdIt last
1944 , typename dtl::disable_if_or
1945 < void
1946 , dtl::is_convertible<FwdIt, size_type>
1947 , dtl::is_input_iterator<FwdIt>
1948 >::type * = 0
1949 )
1950 {
1951 BOOST_ASSERT(this->priv_in_range_or_end(pos));
1952 dtl::insert_range_proxy<Allocator, FwdIt, T*> proxy(first);
1953 return this->priv_forward_range_insert(vector_iterator_get_ptr(pos), boost::container::iterator_distance(first, last), proxy);
1954 }
1955 #endif
1956
1957 //! <b>Requires</b>: p must be a valid iterator of *this. num, must
1958 //! be equal to boost::container::iterator_distance(first, last)
1959 //!
1960 //! <b>Effects</b>: Insert a copy of the [first, last) range before pos.
1961 //!
1962 //! <b>Returns</b>: an iterator to the first inserted element or pos if first == last.
1963 //!
1964 //! <b>Throws</b>: If memory allocation throws, T's constructor from a
1965 //! dereferenced InpIt throws or T's copy/move constructor/assignment throws.
1966 //!
1967 //! <b>Complexity</b>: Linear to boost::container::iterator_distance [first, last).
1968 //!
1969 //! <b>Note</b>: This function avoids a linear operation to calculate boost::container::iterator_distance[first, last)
1970 //! for forward and bidirectional iterators, and a one by one insertion for input iterators. This is a
1971 //! a non-standard extension.
1972 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1973 template <class InIt>
1974 iterator insert(const_iterator pos, size_type num, InIt first, InIt last)
1975 {
1976 BOOST_ASSERT(this->priv_in_range_or_end(pos));
1977 BOOST_ASSERT(dtl::is_input_iterator<InIt>::value ||
1978 num == static_cast<size_type>(boost::container::iterator_distance(first, last)));
1979 (void)last;
1980 dtl::insert_range_proxy<Allocator, InIt, T*> proxy(first);
1981 return this->priv_forward_range_insert(vector_iterator_get_ptr(pos), num, proxy);
1982 }
1983 #endif
1984
1985 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1986 //! <b>Requires</b>: position must be a valid iterator of *this.
1987 //!
1988 //! <b>Effects</b>: Insert a copy of the [il.begin(), il.end()) range before position.
1989 //!
1990 //! <b>Returns</b>: an iterator to the first inserted element or position if first == last.
1991 //!
1992 //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
1993 iterator insert(const_iterator position, std::initializer_list<value_type> il)
1994 {
1995 //Assertion done in insert()
1996 return this->insert(position, il.begin(), il.end());
1997 }
1998 #endif
1999
2000 //! <b>Effects</b>: Removes the last element from the container.
2001 //!
2002 //! <b>Throws</b>: Nothing.
2003 //!
2004 //! <b>Complexity</b>: Constant time.
2005 void pop_back() BOOST_NOEXCEPT_OR_NOTHROW
2006 {
2007 BOOST_ASSERT(!this->empty());
2008 //Destroy last element
2009 this->priv_destroy_last();
2010 }
2011
2012 //! <b>Effects</b>: Erases the element at position pos.
2013 //!
2014 //! <b>Throws</b>: Nothing.
2015 //!
2016 //! <b>Complexity</b>: Linear to the elements between pos and the
2017 //! last element. Constant if pos is the last element.
2018 iterator erase(const_iterator position)
2019 {
2020 BOOST_ASSERT(this->priv_in_range(position));
2021 const pointer p = vector_iterator_get_ptr(position);
2022 T *const pos_ptr = boost::movelib::to_raw_pointer(p);
2023 T *const beg_ptr = this->priv_raw_begin();
2024 T *const new_end_ptr = ::boost::container::move(pos_ptr + 1, beg_ptr + this->m_holder.m_size, pos_ptr);
2025 //Move elements forward and destroy last
2026 this->priv_destroy_last(pos_ptr != new_end_ptr);
2027 return iterator(p);
2028 }
2029
2030 //! <b>Effects</b>: Erases the elements pointed by [first, last).
2031 //!
2032 //! <b>Throws</b>: Nothing.
2033 //!
2034 //! <b>Complexity</b>: Linear to the distance between first and last
2035 //! plus linear to the elements between pos and the last element.
2036 iterator erase(const_iterator first, const_iterator last)
2037 {
2038 BOOST_ASSERT(first == last ||
2039 (first < last && this->priv_in_range(first) && this->priv_in_range_or_end(last)));
2040 if (first != last){
2041 T* const old_end_ptr = this->priv_raw_end();
2042 T* const first_ptr = boost::movelib::to_raw_pointer(vector_iterator_get_ptr(first));
2043 T* const last_ptr = boost::movelib::to_raw_pointer(vector_iterator_get_ptr(last));
2044 T* const ptr = boost::movelib::to_raw_pointer(boost::container::move(last_ptr, old_end_ptr, first_ptr));
2045 this->priv_destroy_last_n(old_end_ptr - ptr);
2046 }
2047 return iterator(vector_iterator_get_ptr(first));
2048 }
2049
2050 //! <b>Effects</b>: Swaps the contents of *this and x.
2051 //!
2052 //! <b>Throws</b>: Nothing.
2053 //!
2054 //! <b>Complexity</b>: Constant.
2055 BOOST_CONTAINER_FORCEINLINE void swap(vector& x)
2056 BOOST_NOEXCEPT_IF( ((allocator_traits_type::propagate_on_container_swap::value
2057 || allocator_traits_type::is_always_equal::value) &&
2058 !dtl::is_version<Allocator, 0>::value))
2059 {
2060 this->priv_swap(x, dtl::bool_<dtl::is_version<Allocator, 0>::value>());
2061 }
2062
2063 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2064
2065 //! <b>Effects</b>: Swaps the contents of *this and x.
2066 //!
2067 //! <b>Throws</b>: Nothing.
2068 //!
2069 //! <b>Complexity</b>: Linear
2070 //!
2071 //! <b>Note</b>: Non-standard extension to support static_vector
2072 template<class OtherAllocator>
2073 BOOST_CONTAINER_FORCEINLINE void swap(vector<T, OtherAllocator> & x
2074 , typename dtl::enable_if_and
2075 < void
2076 , dtl::is_version<OtherAllocator, 0>
2077 , dtl::is_different<OtherAllocator, allocator_type>
2078 >::type * = 0
2079 )
2080 { this->m_holder.deep_swap(x.m_holder); }
2081
2082 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2083
2084 //! <b>Effects</b>: Erases all the elements of the vector.
2085 //!
2086 //! <b>Throws</b>: Nothing.
2087 //!
2088 //! <b>Complexity</b>: Linear to the number of elements in the container.
2089 BOOST_CONTAINER_FORCEINLINE void clear() BOOST_NOEXCEPT_OR_NOTHROW
2090 { this->priv_destroy_all(); }
2091
2092 //! <b>Effects</b>: Returns true if x and y are equal
2093 //!
2094 //! <b>Complexity</b>: Linear to the number of elements in the container.
2095 BOOST_CONTAINER_FORCEINLINE friend bool operator==(const vector& x, const vector& y)
2096 { return x.size() == y.size() && ::boost::container::algo_equal(x.begin(), x.end(), y.begin()); }
2097
2098 //! <b>Effects</b>: Returns true if x and y are unequal
2099 //!
2100 //! <b>Complexity</b>: Linear to the number of elements in the container.
2101 BOOST_CONTAINER_FORCEINLINE friend bool operator!=(const vector& x, const vector& y)
2102 { return !(x == y); }
2103
2104 //! <b>Effects</b>: Returns true if x is less than y
2105 //!
2106 //! <b>Complexity</b>: Linear to the number of elements in the container.
2107 friend bool operator<(const vector& x, const vector& y)
2108 {
2109 const_iterator first1(x.cbegin()), first2(y.cbegin());
2110 const const_iterator last1(x.cend()), last2(y.cend());
2111 for ( ; (first1 != last1) && (first2 != last2); ++first1, ++first2 ) {
2112 if (*first1 < *first2) return true;
2113 if (*first2 < *first1) return false;
2114 }
2115 return (first1 == last1) && (first2 != last2);
2116 }
2117
2118 //! <b>Effects</b>: Returns true if x is greater than y
2119 //!
2120 //! <b>Complexity</b>: Linear to the number of elements in the container.
2121 BOOST_CONTAINER_FORCEINLINE friend bool operator>(const vector& x, const vector& y)
2122 { return y < x; }
2123
2124 //! <b>Effects</b>: Returns true if x is equal or less than y
2125 //!
2126 //! <b>Complexity</b>: Linear to the number of elements in the container.
2127 BOOST_CONTAINER_FORCEINLINE friend bool operator<=(const vector& x, const vector& y)
2128 { return !(y < x); }
2129
2130 //! <b>Effects</b>: Returns true if x is equal or greater than y
2131 //!
2132 //! <b>Complexity</b>: Linear to the number of elements in the container.
2133 BOOST_CONTAINER_FORCEINLINE friend bool operator>=(const vector& x, const vector& y)
2134 { return !(x < y); }
2135
2136 //! <b>Effects</b>: x.swap(y)
2137 //!
2138 //! <b>Complexity</b>: Constant.
2139 BOOST_CONTAINER_FORCEINLINE friend void swap(vector& x, vector& y)
2140 { x.swap(y); }
2141
2142 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2143 //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
2144 //! effect. Otherwise, it is a request for allocation of additional memory
2145 //! (memory expansion) that will not invalidate iterators.
2146 //! If the request is successful, then capacity() is greater than or equal to
2147 //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
2148 //!
2149 //! <b>Throws</b>: If memory allocation allocation throws or T's copy/move constructor throws.
2150 //!
2151 //! <b>Note</b>: Non-standard extension.
2152 bool stable_reserve(size_type new_cap)
2153 {
2154 const size_type cp = this->capacity();
2155 return cp >= new_cap || (alloc_version::value == 2 && this->m_holder.try_expand_fwd(new_cap - cp));
2156 }
2157
2158 //Absolutely experimental. This function might change, disappear or simply crash!
2159 template<class BiDirPosConstIt, class BiDirValueIt>
2160 BOOST_CONTAINER_FORCEINLINE void insert_ordered_at(const size_type element_count, BiDirPosConstIt last_position_it, BiDirValueIt last_value_it)
2161 {
2162 typedef vector_insert_ordered_cursor<BiDirPosConstIt, BiDirValueIt> inserter_t;
2163 return this->priv_insert_ordered_at(element_count, inserter_t(last_position_it, last_value_it));
2164 }
2165
2166 template<class InputIt>
2167 BOOST_CONTAINER_FORCEINLINE void merge(InputIt first, InputIt last)
2168 { this->merge(first, last, value_less_t()); }
2169
2170 template<class InputIt, class Compare>
2171 BOOST_CONTAINER_FORCEINLINE void merge(InputIt first, InputIt last, Compare comp)
2172 {
2173 size_type const s = this->size();
2174 size_type const c = this->capacity();
2175 size_type n = 0;
2176 size_type const free_cap = c - s;
2177 //If not input iterator and new elements don't fit in the remaining capacity, merge in new buffer
2178 if(!dtl::is_input_iterator<InputIt>::value &&
2179 free_cap < (n = static_cast<size_type>(boost::container::iterator_distance(first, last)))){
2180 this->priv_merge_in_new_buffer(first, n, comp, alloc_version());
2181 }
2182 else{
2183 iterator pos(this->insert(this->cend(), first, last));
2184 T *const raw_beg = this->priv_raw_begin();
2185 T *const raw_end = this->priv_raw_end();
2186 T *const raw_pos = raw_beg + s;
2187 boost::movelib::adaptive_merge(raw_beg, raw_pos, raw_end, comp, raw_end, free_cap - n);
2188 }
2189 }
2190
2191 template<class InputIt>
2192 BOOST_CONTAINER_FORCEINLINE void merge_unique(InputIt first, InputIt last)
2193 { this->merge_unique(first, last, value_less_t()); }
2194
2195 template<class InputIt, class Compare>
2196 BOOST_CONTAINER_FORCEINLINE void merge_unique(InputIt first, InputIt last, Compare comp)
2197 {
2198 size_type const s = this->size();
2199 this->priv_set_difference_back(first, last, comp);
2200 T *const raw_beg = this->priv_raw_begin();
2201 T *const raw_end = this->priv_raw_end();
2202 T *raw_pos = raw_beg + s;
2203 boost::movelib::adaptive_merge(raw_beg, raw_pos, raw_end, comp, raw_end, this->capacity() - this->size());
2204 }
2205
2206 private:
2207 template<class PositionValue>
2208 void priv_insert_ordered_at(const size_type element_count, PositionValue position_value)
2209 {
2210 const size_type old_size_pos = this->size();
2211 this->reserve(old_size_pos + element_count);
2212 T* const begin_ptr = this->priv_raw_begin();
2213 size_type insertions_left = element_count;
2214 size_type prev_pos = old_size_pos;
2215 size_type old_hole_size = element_count;
2216
2217 //Exception rollback. If any copy throws before the hole is filled, values
2218 //already inserted/copied at the end of the buffer will be destroyed.
2219 typename value_traits::ArrayDestructor past_hole_values_destroyer
2220 (begin_ptr + old_size_pos + element_count, this->m_holder.alloc(), size_type(0u));
2221 //Loop for each insertion backwards, first moving the elements after the insertion point,
2222 //then inserting the element.
2223 while(insertions_left){
2224 --position_value;
2225 size_type const pos = position_value.get_pos();
2226 BOOST_ASSERT(pos != size_type(-1) && pos <= old_size_pos && pos <= prev_pos);
2227 //If needed shift the range after the insertion point and the previous insertion point.
2228 //Function will take care if the shift crosses the size() boundary, using copy/move
2229 //or uninitialized copy/move if necessary.
2230 size_type new_hole_size = (pos != prev_pos)
2231 ? priv_insert_ordered_at_shift_range(pos, prev_pos, this->size(), insertions_left)
2232 : old_hole_size
2233 ;
2234 if(new_hole_size){
2235 //The hole was reduced by priv_insert_ordered_at_shift_range so expand exception rollback range backwards
2236 past_hole_values_destroyer.increment_size_backwards(prev_pos - pos);
2237 //Insert the new value in the hole
2238 allocator_traits_type::construct(this->m_holder.alloc(), begin_ptr + pos + insertions_left - 1, position_value.get_val());
2239 if(--new_hole_size){
2240 //The hole was reduced by the new insertion by one
2241 past_hole_values_destroyer.increment_size_backwards(size_type(1u));
2242 }
2243 else{
2244 //Hole was just filled, disable exception rollback and change vector size
2245 past_hole_values_destroyer.release();
2246 this->m_holder.m_size += element_count;
2247 }
2248 }
2249 else{
2250 if(old_hole_size){
2251 //Hole was just filled by priv_insert_ordered_at_shift_range, disable exception rollback and change vector size
2252 past_hole_values_destroyer.release();
2253 this->m_holder.m_size += element_count;
2254 }
2255 //Insert the new value in the already constructed range
2256 begin_ptr[pos + insertions_left - 1] = position_value.get_val();
2257 }
2258 --insertions_left;
2259 old_hole_size = new_hole_size;
2260 prev_pos = pos;
2261 }
2262 }
2263
2264 template<class InputIt, class Compare>
2265 void priv_set_difference_back(InputIt first1, InputIt last1, Compare comp)
2266 {
2267 T * old_first2 = this->priv_raw_begin();
2268 T * first2 = old_first2;
2269 T * last2 = this->priv_raw_end();
2270
2271 while (first1 != last1) {
2272 if (first2 == last2){
2273 this->insert(this->cend(), first1, last1);
2274 return;
2275 }
2276
2277 if (comp(*first1, *first2)) {
2278 this->emplace_back(*first1);
2279 //Reallocation happened, update range
2280 T * const raw_begin = this->priv_raw_begin();
2281 if(old_first2 != raw_begin){
2282 first2 = raw_begin + (first2 - old_first2);
2283 last2 = first2 + (last2 - old_first2);
2284 old_first2 = raw_begin;
2285 }
2286
2287 ++first1;
2288 }
2289 else {
2290 if (!comp(*first2, *first1)) {
2291 ++first1;
2292 }
2293 ++first2;
2294 }
2295 }
2296 }
2297
2298 template<class FwdIt, class Compare>
2299 BOOST_CONTAINER_FORCEINLINE void priv_merge_in_new_buffer(FwdIt, size_type, Compare, version_0)
2300 {
2301 throw_bad_alloc();
2302 }
2303
2304 template<class FwdIt, class Compare, class Version>
2305 void priv_merge_in_new_buffer(FwdIt first, size_type n, Compare comp, Version)
2306 {
2307 size_type const new_size = this->size() + n;
2308 size_type new_cap = new_size;
2309 pointer p = pointer();
2310 pointer const new_storage = this->m_holder.allocation_command(allocate_new, new_size, new_cap, p);
2311
2312 BOOST_ASSERT((new_cap >= this->size() ) && (new_cap - this->size()) >= n);
2313 allocator_type &a = this->m_holder.alloc();
2314 typename value_traits::ArrayDeallocator new_buffer_deallocator(new_storage, a, new_cap);
2315 typename value_traits::ArrayDestructor new_values_destroyer(new_storage, a, 0u);
2316 T* pbeg = this->priv_raw_begin();
2317 size_type const old_size = this->size();
2318 T* const pend = pbeg + old_size;
2319 T* d_first = boost::movelib::to_raw_pointer(new_storage);
2320 size_type added = n;
2321 //Merge in new buffer loop
2322 while(1){
2323 if(!n) {
2324 ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), pbeg, pend, d_first);
2325 break;
2326 }
2327 else if(pbeg == pend) {
2328 ::boost::container::uninitialized_move_alloc_n(this->m_holder.alloc(), first, n, d_first);
2329 break;
2330 }
2331 //maintain stability moving external values only if they are strictly less
2332 else if(comp(*first, *pbeg)) {
2333 allocator_traits_type::construct( this->m_holder.alloc(), d_first, *first );
2334 new_values_destroyer.increment_size(1u);
2335 ++first;
2336 --n;
2337 ++d_first;
2338 }
2339 else{
2340 allocator_traits_type::construct( this->m_holder.alloc(), d_first, boost::move(*pbeg) );
2341 new_values_destroyer.increment_size(1u);
2342 ++pbeg;
2343 ++d_first;
2344 }
2345 }
2346
2347 //Nothrow operations
2348 pointer const old_p = this->m_holder.start();
2349 size_type const old_cap = this->m_holder.capacity();
2350 boost::container::destroy_alloc_n(a, boost::movelib::to_raw_pointer(old_p), old_size);
2351 this->m_holder.deallocate(old_p, old_cap);
2352 this->m_holder.m_size = old_size + added;
2353 this->m_holder.start(new_storage);
2354 this->m_holder.capacity(new_cap);
2355 new_buffer_deallocator.release();
2356 new_values_destroyer.release();
2357 }
2358
2359 BOOST_CONTAINER_FORCEINLINE bool room_enough() const
2360 { return this->m_holder.m_size < this->m_holder.capacity(); }
2361
2362 BOOST_CONTAINER_FORCEINLINE pointer back_ptr() const
2363 { return this->m_holder.start() + this->m_holder.m_size; }
2364
2365 size_type priv_index_of(pointer p) const
2366 {
2367 BOOST_ASSERT(this->m_holder.start() <= p);
2368 BOOST_ASSERT(p <= (this->m_holder.start()+this->size()));
2369 return static_cast<size_type>(p - this->m_holder.start());
2370 }
2371
2372 template<class OtherAllocator>
2373 void priv_move_assign(BOOST_RV_REF_BEG vector<T, OtherAllocator> BOOST_RV_REF_END x
2374 , typename dtl::enable_if_c
2375 < dtl::is_version<OtherAllocator, 0>::value >::type * = 0)
2376 {
2377 if(!dtl::is_same<OtherAllocator, allocator_type>::value &&
2378 this->capacity() < x.size()){
2379 throw_bad_alloc();
2380 }
2381 T* const this_start = this->priv_raw_begin();
2382 T* const other_start = x.priv_raw_begin();
2383 const size_type this_sz = m_holder.m_size;
2384 const size_type other_sz = static_cast<size_type>(x.m_holder.m_size);
2385 boost::container::move_assign_range_alloc_n(this->m_holder.alloc(), other_start, other_sz, this_start, this_sz);
2386 this->m_holder.m_size = other_sz;
2387 }
2388
2389 template<class OtherAllocator>
2390 void priv_move_assign(BOOST_RV_REF_BEG vector<T, OtherAllocator> BOOST_RV_REF_END x
2391 , typename dtl::disable_if_or
2392 < void
2393 , dtl::is_version<OtherAllocator, 0>
2394 , dtl::is_different<OtherAllocator, allocator_type>
2395 >::type * = 0)
2396 {
2397 //for move assignment, no aliasing (&x != this) is assummed.
2398 BOOST_ASSERT(this != &x);
2399 allocator_type &this_alloc = this->m_holder.alloc();
2400 allocator_type &x_alloc = x.m_holder.alloc();
2401 const bool propagate_alloc = allocator_traits_type::propagate_on_container_move_assignment::value;
2402
2403 const bool is_propagable_from_x = is_propagable_from(x_alloc, x.m_holder.start(), this_alloc, propagate_alloc);
2404 const bool is_propagable_from_t = is_propagable_from(this_alloc, m_holder.start(), x_alloc, propagate_alloc);
2405 const bool are_both_propagable = is_propagable_from_x && is_propagable_from_t;
2406
2407 //Resources can be transferred if both allocators are
2408 //going to be equal after this function (either propagated or already equal)
2409 if(are_both_propagable){
2410 //Destroy objects but retain memory in case x reuses it in the future
2411 this->clear();
2412 this->m_holder.swap_resources(x.m_holder);
2413 }
2414 else if(is_propagable_from_x){
2415 this->clear();
2416 this->m_holder.deallocate(this->m_holder.m_start, this->m_holder.m_capacity);
2417 this->m_holder.steal_resources(x.m_holder);
2418 }
2419 //Else do a one by one move
2420 else{
2421 this->assign( boost::make_move_iterator(boost::movelib::iterator_to_raw_pointer(x.begin()))
2422 , boost::make_move_iterator(boost::movelib::iterator_to_raw_pointer(x.end() ))
2423 );
2424 }
2425 //Move allocator if needed
2426 dtl::move_alloc(this_alloc, x_alloc, dtl::bool_<propagate_alloc>());
2427 }
2428
2429 template<class OtherAllocator>
2430 void priv_copy_assign(const vector<T, OtherAllocator> &x
2431 , typename dtl::enable_if_c
2432 < dtl::is_version<OtherAllocator, 0>::value >::type * = 0)
2433 {
2434 if(!dtl::is_same<OtherAllocator, allocator_type>::value &&
2435 this->capacity() < x.size()){
2436 throw_bad_alloc();
2437 }
2438 T* const this_start = this->priv_raw_begin();
2439 T* const other_start = x.priv_raw_begin();
2440 const size_type this_sz = m_holder.m_size;
2441 const size_type other_sz = static_cast<size_type>(x.m_holder.m_size);
2442 boost::container::copy_assign_range_alloc_n(this->m_holder.alloc(), other_start, other_sz, this_start, this_sz);
2443 this->m_holder.m_size = other_sz;
2444 }
2445
2446 template<class OtherAllocator>
2447 typename dtl::disable_if_or
2448 < void
2449 , dtl::is_version<OtherAllocator, 0>
2450 , dtl::is_different<OtherAllocator, allocator_type>
2451 >::type
2452 priv_copy_assign(const vector<T, OtherAllocator> &x)
2453 {
2454 allocator_type &this_alloc = this->m_holder.alloc();
2455 const allocator_type &x_alloc = x.m_holder.alloc();
2456 dtl::bool_<allocator_traits_type::
2457 propagate_on_container_copy_assignment::value> flag;
2458 if(flag && this_alloc != x_alloc){
2459 this->clear();
2460 this->shrink_to_fit();
2461 }
2462 dtl::assign_alloc(this_alloc, x_alloc, flag);
2463 this->assign( x.priv_raw_begin(), x.priv_raw_end() );
2464 }
2465
2466 template<class Vector> //Template it to avoid it in explicit instantiations
2467 void priv_swap(Vector &x, dtl::true_type) //version_0
2468 { this->m_holder.deep_swap(x.m_holder); }
2469
2470 template<class Vector> //Template it to avoid it in explicit instantiations
2471 void priv_swap(Vector &x, dtl::false_type) //version_N
2472 {
2473 const bool propagate_alloc = allocator_traits_type::propagate_on_container_swap::value;
2474 if(are_swap_propagable( this->get_stored_allocator(), this->m_holder.start()
2475 , x.get_stored_allocator(), x.m_holder.start(), propagate_alloc)){
2476 //Just swap internals
2477 this->m_holder.swap_resources(x.m_holder);
2478 }
2479 else{
2480 //Else swap element by element...
2481 bool const t_smaller = this->size() < x.size();
2482 vector &sml = t_smaller ? *this : x;
2483 vector &big = t_smaller ? x : *this;
2484
2485 size_type const common_elements = sml.size();
2486 for(size_type i = 0; i != common_elements; ++i){
2487 boost::adl_move_swap(sml[i], big[i]);
2488 }
2489 //... and move-insert the remaining range
2490 sml.insert( sml.cend()
2491 , boost::make_move_iterator(boost::movelib::iterator_to_raw_pointer(big.nth(common_elements)))
2492 , boost::make_move_iterator(boost::movelib::iterator_to_raw_pointer(big.end()))
2493 );
2494 //Destroy remaining elements
2495 big.erase(big.nth(common_elements), big.cend());
2496 }
2497 //And now swap the allocator
2498 dtl::swap_alloc(this->m_holder.alloc(), x.m_holder.alloc(), dtl::bool_<propagate_alloc>());
2499 }
2500
2501 void priv_reserve_no_capacity(size_type, version_0)
2502 { throw_bad_alloc(); }
2503
2504 dtl::insert_range_proxy<Allocator, boost::move_iterator<T*>, T*> priv_dummy_empty_proxy()
2505 {
2506 return dtl::insert_range_proxy<Allocator, boost::move_iterator<T*>, T*>
2507 (::boost::make_move_iterator((T *)0));
2508 }
2509
2510 void priv_reserve_no_capacity(size_type new_cap, version_1)
2511 {
2512 //There is not enough memory, allocate a new buffer
2513 //Pass the hint so that allocators can take advantage of this.
2514 pointer const p = this->m_holder.allocate(new_cap);
2515 //We will reuse insert code, so create a dummy input iterator
2516 this->priv_forward_range_insert_new_allocation
2517 ( boost::movelib::to_raw_pointer(p), new_cap, this->priv_raw_end(), 0, this->priv_dummy_empty_proxy());
2518 }
2519
2520 void priv_reserve_no_capacity(size_type new_cap, version_2)
2521 {
2522 //There is not enough memory, allocate a new
2523 //buffer or expand the old one.
2524 bool same_buffer_start;
2525 size_type real_cap = 0;
2526 pointer reuse(this->m_holder.start());
2527 pointer const ret(this->m_holder.allocation_command(allocate_new | expand_fwd | expand_bwd, new_cap, real_cap = new_cap, reuse));
2528
2529 //Check for forward expansion
2530 same_buffer_start = reuse && this->m_holder.start() == ret;
2531 if(same_buffer_start){
2532 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2533 ++this->num_expand_fwd;
2534 #endif
2535 this->m_holder.capacity(real_cap);
2536 }
2537 else{ //If there is no forward expansion, move objects, we will reuse insertion code
2538 T * const new_mem = boost::movelib::to_raw_pointer(ret);
2539 T * const ins_pos = this->priv_raw_end();
2540 if(reuse){ //Backwards (and possibly forward) expansion
2541 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2542 ++this->num_expand_bwd;
2543 #endif
2544 this->priv_forward_range_insert_expand_backwards
2545 ( new_mem , real_cap, ins_pos, 0, this->priv_dummy_empty_proxy());
2546 }
2547 else{ //New buffer
2548 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2549 ++this->num_alloc;
2550 #endif
2551 this->priv_forward_range_insert_new_allocation
2552 ( new_mem, real_cap, ins_pos, 0, this->priv_dummy_empty_proxy());
2553 }
2554 }
2555 }
2556
2557 void priv_destroy_last(const bool moved = false) BOOST_NOEXCEPT_OR_NOTHROW
2558 {
2559 (void)moved;
2560 const bool skip_destructor = value_traits::trivial_dctr || (value_traits::trivial_dctr_after_move && moved);
2561 if(!skip_destructor){
2562 value_type* const p = this->priv_raw_end() - 1;
2563 allocator_traits_type::destroy(this->get_stored_allocator(), p);
2564 }
2565 --this->m_holder.m_size;
2566 }
2567
2568 void priv_destroy_last_n(const size_type n) BOOST_NOEXCEPT_OR_NOTHROW
2569 {
2570 BOOST_ASSERT(n <= this->m_holder.m_size);
2571 if(!value_traits::trivial_dctr){
2572 T* const destroy_pos = this->priv_raw_begin() + (this->m_holder.m_size-n);
2573 boost::container::destroy_alloc_n(this->get_stored_allocator(), destroy_pos, n);
2574 }
2575 this->m_holder.m_size -= n;
2576 }
2577
2578 template<class InpIt>
2579 void priv_uninitialized_construct_at_end(InpIt first, InpIt last)
2580 {
2581 T* const old_end_pos = this->priv_raw_end();
2582 T* const new_end_pos = boost::container::uninitialized_copy_alloc(this->m_holder.alloc(), first, last, old_end_pos);
2583 this->m_holder.m_size += new_end_pos - old_end_pos;
2584 }
2585
2586 void priv_destroy_all() BOOST_NOEXCEPT_OR_NOTHROW
2587 {
2588 boost::container::destroy_alloc_n
2589 (this->get_stored_allocator(), this->priv_raw_begin(), this->m_holder.m_size);
2590 this->m_holder.m_size = 0;
2591 }
2592
2593 template<class U>
2594 iterator priv_insert(const const_iterator &p, BOOST_FWD_REF(U) x)
2595 {
2596 BOOST_ASSERT(this->priv_in_range_or_end(p));
2597 return this->priv_forward_range_insert
2598 ( vector_iterator_get_ptr(p), 1, dtl::get_insert_value_proxy<T*, Allocator>(::boost::forward<U>(x)));
2599 }
2600
2601 dtl::insert_copy_proxy<Allocator, T*> priv_single_insert_proxy(const T &x)
2602 { return dtl::insert_copy_proxy<Allocator, T*> (x); }
2603
2604 dtl::insert_move_proxy<Allocator, T*> priv_single_insert_proxy(BOOST_RV_REF(T) x)
2605 { return dtl::insert_move_proxy<Allocator, T*> (x); }
2606
2607 template <class U>
2608 void priv_push_back(BOOST_FWD_REF(U) u)
2609 {
2610 if (BOOST_LIKELY(this->room_enough())){
2611 //There is more memory, just construct a new object at the end
2612 allocator_traits_type::construct
2613 ( this->m_holder.alloc(), this->priv_raw_end(), ::boost::forward<U>(u) );
2614 ++this->m_holder.m_size;
2615 }
2616 else{
2617 this->priv_forward_range_insert_no_capacity
2618 ( this->back_ptr(), 1
2619 , this->priv_single_insert_proxy(::boost::forward<U>(u)), alloc_version());
2620 }
2621 }
2622
2623 BOOST_CONTAINER_FORCEINLINE dtl::insert_n_copies_proxy<Allocator, T*> priv_resize_proxy(const T &x)
2624 { return dtl::insert_n_copies_proxy<Allocator, T*>(x); }
2625
2626 BOOST_CONTAINER_FORCEINLINE dtl::insert_default_initialized_n_proxy<Allocator, T*> priv_resize_proxy(default_init_t)
2627 { return dtl::insert_default_initialized_n_proxy<Allocator, T*>(); }
2628
2629 BOOST_CONTAINER_FORCEINLINE dtl::insert_value_initialized_n_proxy<Allocator, T*> priv_resize_proxy(value_init_t)
2630 { return dtl::insert_value_initialized_n_proxy<Allocator, T*>(); }
2631
2632 template <class U>
2633 void priv_resize(size_type new_size, const U& u)
2634 {
2635 const size_type sz = this->size();
2636 if (new_size < sz){
2637 //Destroy last elements
2638 this->priv_destroy_last_n(sz - new_size);
2639 }
2640 else{
2641 const size_type n = new_size - this->size();
2642 this->priv_forward_range_insert_at_end(n, this->priv_resize_proxy(u), alloc_version());
2643 }
2644 }
2645
2646 BOOST_CONTAINER_FORCEINLINE void priv_shrink_to_fit(version_0) BOOST_NOEXCEPT_OR_NOTHROW
2647 {}
2648
2649 void priv_shrink_to_fit(version_1)
2650 {
2651 const size_type cp = this->m_holder.capacity();
2652 if(cp){
2653 const size_type sz = this->size();
2654 if(!sz){
2655 this->m_holder.deallocate(this->m_holder.m_start, cp);
2656 this->m_holder.m_start = pointer();
2657 this->m_holder.m_capacity = 0;
2658 }
2659 else if(sz < cp){
2660 //Allocate a new buffer.
2661 //Pass the hint so that allocators can take advantage of this.
2662 pointer const p = this->m_holder.allocate(sz);
2663
2664 //We will reuse insert code, so create a dummy input iterator
2665 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2666 ++this->num_alloc;
2667 #endif
2668 this->priv_forward_range_insert_new_allocation
2669 ( boost::movelib::to_raw_pointer(p), sz
2670 , this->priv_raw_begin(), 0, this->priv_dummy_empty_proxy());
2671 }
2672 }
2673 }
2674
2675 void priv_shrink_to_fit(version_2) BOOST_NOEXCEPT_OR_NOTHROW
2676 {
2677 const size_type cp = this->m_holder.capacity();
2678 if(cp){
2679 const size_type sz = this->size();
2680 if(!sz){
2681 this->m_holder.deallocate(this->m_holder.m_start, cp);
2682 this->m_holder.m_start = pointer();
2683 this->m_holder.m_capacity = 0;
2684 }
2685 else{
2686 size_type received_size = sz;
2687 pointer reuse(this->m_holder.start());
2688 if(this->m_holder.allocation_command
2689 (shrink_in_place | nothrow_allocation, cp, received_size, reuse)){
2690 this->m_holder.capacity(received_size);
2691 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2692 ++this->num_shrink;
2693 #endif
2694 }
2695 }
2696 }
2697 }
2698
2699 template <class InsertionProxy>
2700 iterator priv_forward_range_insert_no_capacity
2701 (const pointer &pos, const size_type, const InsertionProxy , version_0)
2702 {
2703 throw_bad_alloc();
2704 return iterator(pos);
2705 }
2706
2707 template <class InsertionProxy>
2708 iterator priv_forward_range_insert_no_capacity
2709 (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, version_1)
2710 {
2711 //Check if we have enough memory or try to expand current memory
2712 const size_type n_pos = pos - this->m_holder.start();
2713 T *const raw_pos = boost::movelib::to_raw_pointer(pos);
2714
2715 const size_type new_cap = this->m_holder.template next_capacity<growth_factor_type>(n);
2716 //Pass the hint so that allocators can take advantage of this.
2717 T * const new_buf = boost::movelib::to_raw_pointer(this->m_holder.allocate(new_cap));
2718 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2719 ++this->num_alloc;
2720 #endif
2721 this->priv_forward_range_insert_new_allocation
2722 ( new_buf, new_cap, raw_pos, n, insert_range_proxy);
2723 return iterator(this->m_holder.start() + n_pos);
2724 }
2725
2726 template <class InsertionProxy>
2727 iterator priv_forward_range_insert_no_capacity
2728 (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, version_2)
2729 {
2730 //Check if we have enough memory or try to expand current memory
2731 T *const raw_pos = boost::movelib::to_raw_pointer(pos);
2732 const size_type n_pos = raw_pos - this->priv_raw_begin();
2733
2734 //There is not enough memory, allocate a new
2735 //buffer or expand the old one.
2736 size_type real_cap = this->m_holder.template next_capacity<growth_factor_type>(n);
2737 pointer reuse(this->m_holder.start());
2738 pointer const ret (this->m_holder.allocation_command
2739 (allocate_new | expand_fwd | expand_bwd, this->m_holder.m_size + n, real_cap, reuse));
2740
2741 //Buffer reallocated
2742 if(reuse){
2743 //Forward expansion, delay insertion
2744 if(this->m_holder.start() == ret){
2745 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2746 ++this->num_expand_fwd;
2747 #endif
2748 this->m_holder.capacity(real_cap);
2749 //Expand forward
2750 this->priv_forward_range_insert_expand_forward(raw_pos, n, insert_range_proxy);
2751 }
2752 //Backwards (and possibly forward) expansion
2753 else{
2754 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2755 ++this->num_expand_bwd;
2756 #endif
2757 this->priv_forward_range_insert_expand_backwards
2758 (boost::movelib::to_raw_pointer(ret), real_cap, raw_pos, n, insert_range_proxy);
2759 }
2760 }
2761 //New buffer
2762 else{
2763 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2764 ++this->num_alloc;
2765 #endif
2766 this->priv_forward_range_insert_new_allocation
2767 ( boost::movelib::to_raw_pointer(ret), real_cap, raw_pos, n, insert_range_proxy);
2768 }
2769
2770 return iterator(this->m_holder.start() + n_pos);
2771 }
2772
2773 template <class InsertionProxy>
2774 iterator priv_forward_range_insert
2775 (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy)
2776 {
2777 BOOST_ASSERT(this->m_holder.capacity() >= this->m_holder.m_size);
2778 //Check if we have enough memory or try to expand current memory
2779 const size_type remaining = this->m_holder.capacity() - this->m_holder.m_size;
2780
2781 bool same_buffer_start = n <= remaining;
2782 if (!same_buffer_start){
2783 return priv_forward_range_insert_no_capacity(pos, n, insert_range_proxy, alloc_version());
2784 }
2785 else{
2786 //Expand forward
2787 T *const raw_pos = boost::movelib::to_raw_pointer(pos);
2788 const size_type n_pos = raw_pos - this->priv_raw_begin();
2789 this->priv_forward_range_insert_expand_forward(raw_pos, n, insert_range_proxy);
2790 return iterator(this->m_holder.start() + n_pos);
2791 }
2792 }
2793
2794 template <class InsertionProxy>
2795 iterator priv_forward_range_insert_at_end
2796 (const size_type n, const InsertionProxy insert_range_proxy, version_0)
2797 {
2798 //Check if we have enough memory or try to expand current memory
2799 const size_type remaining = this->m_holder.capacity() - this->m_holder.m_size;
2800
2801 if (n > remaining){
2802 //This will trigger an error
2803 throw_bad_alloc();
2804 }
2805 this->priv_forward_range_insert_at_end_expand_forward(n, insert_range_proxy);
2806 return this->end();
2807 }
2808
2809 template <class InsertionProxy, class AllocVersion>
2810 BOOST_CONTAINER_FORCEINLINE iterator priv_forward_range_insert_at_end
2811 (const size_type n, const InsertionProxy insert_range_proxy, AllocVersion)
2812 {
2813 return this->priv_forward_range_insert(this->back_ptr(), n, insert_range_proxy);
2814 }
2815
2816 //Takes the range pointed by [first_pos, last_pos) and shifts it to the right
2817 //by 'shift_count'. 'limit_pos' marks the end of constructed elements.
2818 //
2819 //Precondition: first_pos <= last_pos <= limit_pos
2820 //
2821 //The shift operation might cross limit_pos so elements to moved beyond limit_pos
2822 //are uninitialized_moved with an allocator. Other elements are moved.
2823 //
2824 //The shift operation might left uninitialized elements after limit_pos
2825 //and the number of uninitialized elements is returned by the function.
2826 //
2827 //Old situation:
2828 // first_pos last_pos old_limit
2829 // | | |
2830 // ____________V_______V__________________V_____________
2831 //| prefix | range | suffix |raw_mem ~
2832 //|____________|_______|__________________|_____________~
2833 //
2834 //New situation in Case A (hole_size == 0):
2835 // range is moved through move assignments
2836 //
2837 // first_pos last_pos limit_pos
2838 // | | |
2839 // ____________V_______V__________________V_____________
2840 //| prefix' | | | range |suffix'|raw_mem ~
2841 //|________________+______|___^___|_______|_____________~
2842 // | |
2843 // |_>_>_>_>_>^
2844 //
2845 //
2846 //New situation in Case B (hole_size >= 0):
2847 // range is moved through uninitialized moves
2848 //
2849 // first_pos last_pos limit_pos
2850 // | | |
2851 // ____________V_______V__________________V________________
2852 //| prefix' | | | [hole] | range |
2853 //|_______________________________________|________|___^___|
2854 // | |
2855 // |_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_^
2856 //
2857 //New situation in Case C (hole_size == 0):
2858 // range is moved through move assignments and uninitialized moves
2859 //
2860 // first_pos last_pos limit_pos
2861 // | | |
2862 // ____________V_______V__________________V___
2863 //| prefix' | | | range |
2864 //|___________________________________|___^___|
2865 // | |
2866 // |_>_>_>_>_>_>_>_>_>_>_>^
2867 size_type priv_insert_ordered_at_shift_range
2868 (size_type first_pos, size_type last_pos, size_type limit_pos, size_type shift_count)
2869 {
2870 BOOST_ASSERT(first_pos <= last_pos);
2871 BOOST_ASSERT(last_pos <= limit_pos);
2872 //
2873 T* const begin_ptr = this->priv_raw_begin();
2874 T* const first_ptr = begin_ptr + first_pos;
2875 T* const last_ptr = begin_ptr + last_pos;
2876
2877 size_type hole_size = 0;
2878 //Case A:
2879 if((last_pos + shift_count) <= limit_pos){
2880 //All move assigned
2881 boost::container::move_backward(first_ptr, last_ptr, last_ptr + shift_count);
2882 }
2883 //Case B:
2884 else if((first_pos + shift_count) >= limit_pos){
2885 //All uninitialized_moved
2886 ::boost::container::uninitialized_move_alloc
2887 (this->m_holder.alloc(), first_ptr, last_ptr, first_ptr + shift_count);
2888 hole_size = first_pos + shift_count - limit_pos;
2889 }
2890 //Case C:
2891 else{
2892 //Some uninitialized_moved
2893 T* const limit_ptr = begin_ptr + limit_pos;
2894 T* const boundary_ptr = limit_ptr - shift_count;
2895 ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), boundary_ptr, last_ptr, limit_ptr);
2896 //The rest is move assigned
2897 boost::container::move_backward(first_ptr, boundary_ptr, limit_ptr);
2898 }
2899 return hole_size;
2900 }
2901
2902 private:
2903 BOOST_CONTAINER_FORCEINLINE T *priv_raw_begin() const
2904 { return boost::movelib::to_raw_pointer(m_holder.start()); }
2905
2906 BOOST_CONTAINER_FORCEINLINE T* priv_raw_end() const
2907 { return this->priv_raw_begin() + this->m_holder.m_size; }
2908
2909 template <class InsertionProxy>
2910 void priv_forward_range_insert_at_end_expand_forward(const size_type n, InsertionProxy insert_range_proxy)
2911 {
2912 T* const old_finish = this->priv_raw_end();
2913 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n);
2914 this->m_holder.m_size += n;
2915 }
2916
2917 template <class InsertionProxy>
2918 void priv_forward_range_insert_expand_forward(T* const pos, const size_type n, InsertionProxy insert_range_proxy)
2919 {
2920 //n can't be 0, because there is nothing to do in that case
2921 if(BOOST_UNLIKELY(!n)) return;
2922 //There is enough memory
2923 T* const old_finish = this->priv_raw_end();
2924 const size_type elems_after = old_finish - pos;
2925
2926 if (!elems_after){
2927 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n);
2928 this->m_holder.m_size += n;
2929 }
2930 else if (elems_after >= n){
2931 //New elements can be just copied.
2932 //Move to uninitialized memory last objects
2933 ::boost::container::uninitialized_move_alloc
2934 (this->m_holder.alloc(), old_finish - n, old_finish, old_finish);
2935 this->m_holder.m_size += n;
2936 //Copy previous to last objects to the initialized end
2937 boost::container::move_backward(pos, old_finish - n, old_finish);
2938 //Insert new objects in the pos
2939 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, n);
2940 }
2941 else {
2942 //The new elements don't fit in the [pos, end()) range.
2943
2944 //Copy old [pos, end()) elements to the uninitialized memory (a gap is created)
2945 ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), pos, old_finish, pos + n);
2946 BOOST_TRY{
2947 //Copy first new elements in pos (gap is still there)
2948 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, elems_after);
2949 //Copy to the beginning of the unallocated zone the last new elements (the gap is closed).
2950 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n - elems_after);
2951 this->m_holder.m_size += n;
2952 }
2953 BOOST_CATCH(...){
2954 boost::container::destroy_alloc_n(this->get_stored_allocator(), pos + n, elems_after);
2955 BOOST_RETHROW
2956 }
2957 BOOST_CATCH_END
2958 }
2959 }
2960
2961 template <class InsertionProxy>
2962 void priv_forward_range_insert_new_allocation
2963 (T* const new_start, size_type new_cap, T* const pos, const size_type n, InsertionProxy insert_range_proxy)
2964 {
2965 //n can be zero, if we want to reallocate!
2966 T *new_finish = new_start;
2967 T *old_finish;
2968 //Anti-exception rollbacks
2969 typename value_traits::ArrayDeallocator new_buffer_deallocator(new_start, this->m_holder.alloc(), new_cap);
2970 typename value_traits::ArrayDestructor new_values_destroyer(new_start, this->m_holder.alloc(), 0u);
2971
2972 //Initialize with [begin(), pos) old buffer
2973 //the start of the new buffer
2974 T * const old_buffer = this->priv_raw_begin();
2975 if(old_buffer){
2976 new_finish = ::boost::container::uninitialized_move_alloc
2977 (this->m_holder.alloc(), this->priv_raw_begin(), pos, old_finish = new_finish);
2978 new_values_destroyer.increment_size(new_finish - old_finish);
2979 }
2980 //Initialize new objects, starting from previous point
2981 old_finish = new_finish;
2982 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n);
2983 new_finish += n;
2984 new_values_destroyer.increment_size(new_finish - old_finish);
2985 //Initialize from the rest of the old buffer,
2986 //starting from previous point
2987 if(old_buffer){
2988 new_finish = ::boost::container::uninitialized_move_alloc
2989 (this->m_holder.alloc(), pos, old_buffer + this->m_holder.m_size, new_finish);
2990 //Destroy and deallocate old elements
2991 //If there is allocated memory, destroy and deallocate
2992 if(!value_traits::trivial_dctr_after_move)
2993 boost::container::destroy_alloc_n(this->get_stored_allocator(), old_buffer, this->m_holder.m_size);
2994 this->m_holder.deallocate(this->m_holder.start(), this->m_holder.capacity());
2995 }
2996 this->m_holder.start(new_start);
2997 this->m_holder.m_size = size_type(new_finish - new_start);
2998 this->m_holder.capacity(new_cap);
2999 //All construction successful, disable rollbacks
3000 new_values_destroyer.release();
3001 new_buffer_deallocator.release();
3002 }
3003
3004 template <class InsertionProxy>
3005 void priv_forward_range_insert_expand_backwards
3006 (T* const new_start, const size_type new_capacity,
3007 T* const pos, const size_type n, InsertionProxy insert_range_proxy)
3008 {
3009 //n can be zero to just expand capacity
3010 //Backup old data
3011 T* const old_start = this->priv_raw_begin();
3012 const size_type old_size = this->m_holder.m_size;
3013 T* const old_finish = old_start + old_size;
3014
3015 //We can have 8 possibilities:
3016 const size_type elemsbefore = static_cast<size_type>(pos - old_start);
3017 const size_type s_before = static_cast<size_type>(old_start - new_start);
3018 const size_type before_plus_new = elemsbefore + n;
3019
3020 //Update the vector buffer information to a safe state
3021 this->m_holder.start(new_start);
3022 this->m_holder.capacity(new_capacity);
3023 this->m_holder.m_size = 0;
3024
3025 //If anything goes wrong, this object will destroy
3026 //all the old objects to fulfill previous vector state
3027 typename value_traits::ArrayDestructor old_values_destroyer(old_start, this->m_holder.alloc(), old_size);
3028 //Check if s_before is big enough to hold the beginning of old data + new data
3029 if(s_before >= before_plus_new){
3030 //Copy first old values before pos, after that the new objects
3031 T *const new_elem_pos =
3032 ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), old_start, pos, new_start);
3033 this->m_holder.m_size = elemsbefore;
3034 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), new_elem_pos, n);
3035 this->m_holder.m_size = before_plus_new;
3036 const size_type new_size = old_size + n;
3037 //Check if s_before is so big that even copying the old data + new data
3038 //there is a gap between the new data and the old data
3039 if(s_before >= new_size){
3040 //Old situation:
3041 // _________________________________________________________
3042 //| raw_mem | old_begin | old_end |
3043 //| __________________________________|___________|_________|
3044 //
3045 //New situation:
3046 // _________________________________________________________
3047 //| old_begin | new | old_end | raw_mem |
3048 //|___________|__________|_________|________________________|
3049 //
3050 //Now initialize the rest of memory with the last old values
3051 if(before_plus_new != new_size){ //Special case to avoid operations in back insertion
3052 ::boost::container::uninitialized_move_alloc
3053 (this->m_holder.alloc(), pos, old_finish, new_start + before_plus_new);
3054 //All new elements correctly constructed, avoid new element destruction
3055 this->m_holder.m_size = new_size;
3056 }
3057 //Old values destroyed automatically with "old_values_destroyer"
3058 //when "old_values_destroyer" goes out of scope unless the have trivial
3059 //destructor after move.
3060 if(value_traits::trivial_dctr_after_move)
3061 old_values_destroyer.release();
3062 }
3063 //s_before is so big that divides old_end
3064 else{
3065 //Old situation:
3066 // __________________________________________________
3067 //| raw_mem | old_begin | old_end |
3068 //| ___________________________|___________|_________|
3069 //
3070 //New situation:
3071 // __________________________________________________
3072 //| old_begin | new | old_end | raw_mem |
3073 //|___________|__________|_________|_________________|
3074 //
3075 //Now initialize the rest of memory with the last old values
3076 //All new elements correctly constructed, avoid new element destruction
3077 const size_type raw_gap = s_before - before_plus_new;
3078 if(!value_traits::trivial_dctr){
3079 //Now initialize the rest of s_before memory with the
3080 //first of elements after new values
3081 ::boost::container::uninitialized_move_alloc_n
3082 (this->m_holder.alloc(), pos, raw_gap, new_start + before_plus_new);
3083 //Now we have a contiguous buffer so program trailing element destruction
3084 //and update size to the final size.
3085 old_values_destroyer.shrink_forward(new_size-s_before);
3086 this->m_holder.m_size = new_size;
3087 //Now move remaining last objects in the old buffer begin
3088 T * const remaining_pos = pos + raw_gap;
3089 if(remaining_pos != old_start){ //Make sure data has to be moved
3090 ::boost::container::move(remaining_pos, old_finish, old_start);
3091 }
3092 //Once moved, avoid calling the destructors if trivial after move
3093 if(value_traits::trivial_dctr_after_move){
3094 old_values_destroyer.release();
3095 }
3096 }
3097 else{ //If trivial destructor, we can uninitialized copy + copy in a single uninitialized copy
3098 ::boost::container::uninitialized_move_alloc_n
3099 (this->m_holder.alloc(), pos, static_cast<size_type>(old_finish - pos), new_start + before_plus_new);
3100 this->m_holder.m_size = new_size;
3101 old_values_destroyer.release();
3102 }
3103 }
3104 }
3105 else{
3106 //Check if we have to do the insertion in two phases
3107 //since maybe s_before is not big enough and
3108 //the buffer was expanded both sides
3109 //
3110 //Old situation:
3111 // _________________________________________________
3112 //| raw_mem | old_begin + old_end | raw_mem |
3113 //|_________|_____________________|_________________|
3114 //
3115 //New situation with do_after:
3116 // _________________________________________________
3117 //| old_begin + new + old_end | raw_mem |
3118 //|___________________________________|_____________|
3119 //
3120 //New without do_after:
3121 // _________________________________________________
3122 //| old_begin + new + old_end | raw_mem |
3123 //|____________________________|____________________|
3124 //
3125 const bool do_after = n > s_before;
3126
3127 //Now we can have two situations: the raw_mem of the
3128 //beginning divides the old_begin, or the new elements:
3129 if (s_before <= elemsbefore) {
3130 //The raw memory divides the old_begin group:
3131 //
3132 //If we need two phase construction (do_after)
3133 //new group is divided in new = new_beg + new_end groups
3134 //In this phase only new_beg will be inserted
3135 //
3136 //Old situation:
3137 // _________________________________________________
3138 //| raw_mem | old_begin | old_end | raw_mem |
3139 //|_________|___________|_________|_________________|
3140 //
3141 //New situation with do_after(1):
3142 //This is not definitive situation, the second phase
3143 //will include
3144 // _________________________________________________
3145 //| old_begin | new_beg | old_end | raw_mem |
3146 //|___________|_________|_________|_________________|
3147 //
3148 //New situation without do_after:
3149 // _________________________________________________
3150 //| old_begin | new | old_end | raw_mem |
3151 //|___________|_____|_________|_____________________|
3152 //
3153 //Copy the first part of old_begin to raw_mem
3154 ::boost::container::uninitialized_move_alloc_n
3155 (this->m_holder.alloc(), old_start, s_before, new_start);
3156 //The buffer is all constructed until old_end,
3157 //so program trailing destruction and assign final size
3158 //if !do_after, s_before+n otherwise.
3159 size_type new_1st_range;
3160 if(do_after){
3161 new_1st_range = s_before;
3162 //release destroyer and update size
3163 old_values_destroyer.release();
3164 }
3165 else{
3166 new_1st_range = n;
3167 if(value_traits::trivial_dctr_after_move)
3168 old_values_destroyer.release();
3169 else{
3170 old_values_destroyer.shrink_forward(old_size - (s_before - n));
3171 }
3172 }
3173 this->m_holder.m_size = old_size + new_1st_range;
3174 //Now copy the second part of old_begin overwriting itself
3175 T *const next = ::boost::container::move(old_start + s_before, pos, old_start);
3176 //Now copy the new_beg elements
3177 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), next, new_1st_range);
3178
3179 //If there is no after work and the last old part needs to be moved to front, do it
3180 if(!do_after && (n != s_before)){
3181 //Now displace old_end elements
3182 ::boost::container::move(pos, old_finish, next + new_1st_range);
3183 }
3184 }
3185 else {
3186 //If we have to expand both sides,
3187 //we will play if the first new values so
3188 //calculate the upper bound of new values
3189
3190 //The raw memory divides the new elements
3191 //
3192 //If we need two phase construction (do_after)
3193 //new group is divided in new = new_beg + new_end groups
3194 //In this phase only new_beg will be inserted
3195 //
3196 //Old situation:
3197 // _______________________________________________________
3198 //| raw_mem | old_begin | old_end | raw_mem |
3199 //|_______________|___________|_________|_________________|
3200 //
3201 //New situation with do_after():
3202 // ____________________________________________________
3203 //| old_begin | new_beg | old_end | raw_mem |
3204 //|___________|_______________|_________|______________|
3205 //
3206 //New situation without do_after:
3207 // ______________________________________________________
3208 //| old_begin | new | old_end | raw_mem |
3209 //|___________|_____|_________|__________________________|
3210 //
3211 //First copy whole old_begin and part of new to raw_mem
3212 T * const new_pos = ::boost::container::uninitialized_move_alloc
3213 (this->m_holder.alloc(), old_start, pos, new_start);
3214 this->m_holder.m_size = elemsbefore;
3215 const size_type mid_n = s_before - elemsbefore;
3216 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), new_pos, mid_n);
3217 //The buffer is all constructed until old_end,
3218 //release destroyer
3219 this->m_holder.m_size = old_size + s_before;
3220 old_values_destroyer.release();
3221
3222 if(do_after){
3223 //Copy new_beg part
3224 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), old_start, elemsbefore);
3225 }
3226 else{
3227 //Copy all new elements
3228 const size_type rest_new = n - mid_n;
3229 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), old_start, rest_new);
3230 T* const move_start = old_start + rest_new;
3231 //Displace old_end, but make sure data has to be moved
3232 T* const move_end = move_start != pos ? ::boost::container::move(pos, old_finish, move_start)
3233 : old_finish;
3234 //Destroy remaining moved elements from old_end except if they
3235 //have trivial destructor after being moved
3236 size_type n_destroy = s_before - n;
3237 if(!value_traits::trivial_dctr_after_move)
3238 boost::container::destroy_alloc_n(this->get_stored_allocator(), move_end, n_destroy);
3239 this->m_holder.m_size -= n_destroy;
3240 }
3241 }
3242
3243 //This is only executed if two phase construction is needed
3244 if(do_after){
3245 //The raw memory divides the new elements
3246 //
3247 //Old situation:
3248 // ______________________________________________________
3249 //| raw_mem | old_begin | old_end | raw_mem |
3250 //|______________|___________|____________|______________|
3251 //
3252 //New situation with do_after(1):
3253 // _______________________________________________________
3254 //| old_begin + new_beg | new_end |old_end | raw_mem |
3255 //|__________________________|_________|________|_________|
3256 //
3257 //New situation with do_after(2):
3258 // ______________________________________________________
3259 //| old_begin + new | old_end |raw |
3260 //|_______________________________________|_________|____|
3261 //
3262 const size_type n_after = n - s_before;
3263 const size_type elemsafter = old_size - elemsbefore;
3264
3265 //We can have two situations:
3266 if (elemsafter >= n_after){
3267 //The raw_mem from end will divide displaced old_end
3268 //
3269 //Old situation:
3270 // ______________________________________________________
3271 //| raw_mem | old_begin | old_end | raw_mem |
3272 //|______________|___________|____________|______________|
3273 //
3274 //New situation with do_after(1):
3275 // _______________________________________________________
3276 //| old_begin + new_beg | new_end |old_end | raw_mem |
3277 //|__________________________|_________|________|_________|
3278 //
3279 //First copy the part of old_end raw_mem
3280 T* finish_n = old_finish - n_after;
3281 ::boost::container::uninitialized_move_alloc
3282 (this->m_holder.alloc(), finish_n, old_finish, old_finish);
3283 this->m_holder.m_size += n_after;
3284 //Displace the rest of old_end to the new position
3285 boost::container::move_backward(pos, finish_n, old_finish);
3286 //Now overwrite with new_end
3287 //The new_end part is [first + (n - n_after), last)
3288 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, n_after);
3289 }
3290 else {
3291 //The raw_mem from end will divide new_end part
3292 //
3293 //Old situation:
3294 // _____________________________________________________________
3295 //| raw_mem | old_begin | old_end | raw_mem |
3296 //|______________|___________|____________|_____________________|
3297 //
3298 //New situation with do_after(2):
3299 // _____________________________________________________________
3300 //| old_begin + new_beg | new_end |old_end | raw_mem |
3301 //|__________________________|_______________|________|_________|
3302 //
3303
3304 const size_type mid_last_dist = n_after - elemsafter;
3305 //First initialize data in raw memory
3306
3307 //Copy to the old_end part to the uninitialized zone leaving a gap.
3308 ::boost::container::uninitialized_move_alloc
3309 (this->m_holder.alloc(), pos, old_finish, old_finish + mid_last_dist);
3310
3311 typename value_traits::ArrayDestructor old_end_destroyer
3312 (old_finish + mid_last_dist, this->m_holder.alloc(), old_finish - pos);
3313
3314 //Copy the first part to the already constructed old_end zone
3315 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, elemsafter);
3316 //Copy the rest to the uninitialized zone filling the gap
3317 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, mid_last_dist);
3318 this->m_holder.m_size += n_after;
3319 old_end_destroyer.release();
3320 }
3321 }
3322 }
3323 }
3324
3325 void priv_throw_if_out_of_range(size_type n) const
3326 {
3327 //If n is out of range, throw an out_of_range exception
3328 if (n >= this->size()){
3329 throw_out_of_range("vector::at out of range");
3330 }
3331 }
3332
3333 BOOST_CONTAINER_FORCEINLINE bool priv_in_range(const_iterator pos) const
3334 {
3335 return (this->begin() <= pos) && (pos < this->end());
3336 }
3337
3338 BOOST_CONTAINER_FORCEINLINE bool priv_in_range_or_end(const_iterator pos) const
3339 {
3340 return (this->begin() <= pos) && (pos <= this->end());
3341 }
3342
3343 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
3344 public:
3345 unsigned int num_expand_fwd;
3346 unsigned int num_expand_bwd;
3347 unsigned int num_shrink;
3348 unsigned int num_alloc;
3349 void reset_alloc_stats()
3350 { num_expand_fwd = num_expand_bwd = num_alloc = 0, num_shrink = 0; }
3351 #endif
3352 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3353 };
3354
3355 }} //namespace boost::container
3356
3357 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3358
3359 namespace boost {
3360
3361 //!has_trivial_destructor_after_move<> == true_type
3362 //!specialization for optimizations
3363 template <class T, class Allocator>
3364 struct has_trivial_destructor_after_move<boost::container::vector<T, Allocator> >
3365 {
3366 typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
3367 static const bool value = ::boost::has_trivial_destructor_after_move<Allocator>::value &&
3368 ::boost::has_trivial_destructor_after_move<pointer>::value;
3369 };
3370
3371 }
3372
3373 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3374
3375 #include <boost/container/detail/config_end.hpp>
3376
3377 #endif // #ifndef BOOST_CONTAINER_CONTAINER_VECTOR_HPP