blob: c3baebc333fc3aec3104a96b4fe14b0656a2f3db [file] [log] [blame]
Brian Silvermanfad8f552018-08-04 23:36:19 -07001//////////////////////////////////////////////////////////////////////////////
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
74namespace boost {
75namespace container {
76
77#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
78
79
80template <class Pointer, bool IsConst>
81class 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 { BOOST_ASSERT(!!m_ptr); return *m_ptr; }
126
127 BOOST_CONTAINER_FORCEINLINE pointer operator->() const BOOST_NOEXCEPT_OR_NOTHROW
128 { return m_ptr; }
129
130 BOOST_CONTAINER_FORCEINLINE reference operator[](difference_type off) const BOOST_NOEXCEPT_OR_NOTHROW
131 { BOOST_ASSERT(!!m_ptr); return m_ptr[off]; }
132
133 //Increment / Decrement
134 BOOST_CONTAINER_FORCEINLINE vec_iterator& operator++() BOOST_NOEXCEPT_OR_NOTHROW
135 { BOOST_ASSERT(!!m_ptr); ++m_ptr; return *this; }
136
137 BOOST_CONTAINER_FORCEINLINE vec_iterator operator++(int) BOOST_NOEXCEPT_OR_NOTHROW
138 { BOOST_ASSERT(!!m_ptr); return vec_iterator(m_ptr++); }
139
140 BOOST_CONTAINER_FORCEINLINE vec_iterator& operator--() BOOST_NOEXCEPT_OR_NOTHROW
141 { BOOST_ASSERT(!!m_ptr); --m_ptr; return *this; }
142
143 BOOST_CONTAINER_FORCEINLINE vec_iterator operator--(int) BOOST_NOEXCEPT_OR_NOTHROW
144 { BOOST_ASSERT(!!m_ptr); return vec_iterator(m_ptr--); }
145
146 //Arithmetic
147 BOOST_CONTAINER_FORCEINLINE vec_iterator& operator+=(difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
148 { BOOST_ASSERT(!!m_ptr); m_ptr += off; return *this; }
149
150 BOOST_CONTAINER_FORCEINLINE vec_iterator& operator-=(difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
151 { BOOST_ASSERT(!!m_ptr); 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 { BOOST_ASSERT(!!x.m_ptr); 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 { BOOST_ASSERT(!!right.m_ptr); 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 { BOOST_ASSERT(!!left.m_ptr); 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
185template<class BiDirPosConstIt, class BiDirValueIt>
186struct 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
215struct initial_capacity_t{};
216
217template<class Pointer, bool IsConst>
218BOOST_CONTAINER_FORCEINLINE const Pointer &vector_iterator_get_ptr(const vec_iterator<Pointer, IsConst> &it) BOOST_NOEXCEPT_OR_NOTHROW
219{ return it.get_ptr(); }
220
221template<class Pointer, bool IsConst>
222BOOST_CONTAINER_FORCEINLINE Pointer &get_ptr(vec_iterator<Pointer, IsConst> &it) BOOST_NOEXCEPT_OR_NOTHROW
223{ return it.get_ptr(); }
224
225struct vector_uninitialized_size_t {};
226static const vector_uninitialized_size_t vector_uninitialized_size = vector_uninitialized_size_t();
227
228template <class T>
229struct 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
240template <class Allocator>
241struct 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
257template < class Allocator
258 , class StoredSizeType
259 , class AllocatorVersion = typename dtl::version<Allocator>::type
260 >
261struct 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
548template <class Allocator, class StoredSizeType>
549struct 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
680struct growth_factor_60;
681
682template<class T, class Default>
683struct default_if_void
684{
685 typedef T type;
686};
687
688template<class Default>
689struct default_if_void<void, Default>
690{
691 typedef Default type;
692};
693
694template<class Options, class AllocatorSizeType>
695struct 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
702template<class AllocatorSizeType>
703struct 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.
719template <class T, class Allocator BOOST_CONTAINER_DOCONLY(= new_allocator<T>), class Options BOOST_CONTAINER_DOCONLY(= void) >
720class 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// ) -> vector<typename iterator_traits<InIt>::value_type, new_allocator<typename iterator_traits<InIt>::value_type>>;
940 template <class InIt>
941 vector(InIt first, InIt last
942 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_c
943 < dtl::is_convertible<InIt BOOST_MOVE_I size_type>::value
944 BOOST_MOVE_I dtl::nat >::type * = 0)
945 )
946 : m_holder()
947 { this->assign(first, last); }
948
949 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
950 //! and inserts a copy of the range [first, last) in the vector.
951 //!
952 //! <b>Throws</b>: If allocator_type's allocation
953 //! throws or T's constructor taking a dereferenced InIt throws.
954 //!
955 //! <b>Complexity</b>: Linear to the range [first, last).
956// template <class InIt>
957// vector(InIt first, InIt last, const allocator_type& a
958// BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_c
959// < dtl::is_convertible<InIt BOOST_MOVE_I size_type>::value
960// BOOST_MOVE_I dtl::nat >::type * = 0)
961// ) -> vector<typename iterator_traits<InIt>::value_type, new_allocator<typename iterator_traits<InIt>::value_type>>;
962 template <class InIt>
963 vector(InIt first, InIt last, const allocator_type& a
964 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_c
965 < dtl::is_convertible<InIt BOOST_MOVE_I size_type>::value
966 BOOST_MOVE_I dtl::nat >::type * = 0)
967 )
968 : m_holder(a)
969 { this->assign(first, last); }
970
971 //! <b>Effects</b>: Copy constructs a vector.
972 //!
973 //! <b>Postcondition</b>: x == *this.
974 //!
975 //! <b>Throws</b>: If allocator_type's allocation
976 //! throws or T's copy constructor throws.
977 //!
978 //! <b>Complexity</b>: Linear to the elements x contains.
979 vector(const vector &x)
980 : m_holder( vector_uninitialized_size
981 , allocator_traits_type::select_on_container_copy_construction(x.m_holder.alloc())
982 , x.size())
983 {
984 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
985 this->num_alloc += x.size() != 0;
986 #endif
987 ::boost::container::uninitialized_copy_alloc_n
988 ( this->m_holder.alloc(), x.priv_raw_begin()
989 , x.size(), this->priv_raw_begin());
990 }
991
992 //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
993 //!
994 //! <b>Throws</b>: Nothing
995 //!
996 //! <b>Complexity</b>: Constant.
997 vector(BOOST_RV_REF(vector) x) BOOST_NOEXCEPT_OR_NOTHROW
998 : m_holder(boost::move(x.m_holder))
999 { BOOST_STATIC_ASSERT((!allocator_traits_type::is_partially_propagable::value)); }
1000
1001 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1002 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
1003 //! and inserts a copy of the range [il.begin(), il.last()) in the vector
1004 //!
1005 //! <b>Throws</b>: If T's constructor taking a dereferenced initializer_list iterator throws.
1006 //!
1007 //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
1008 vector(std::initializer_list<value_type> il, const allocator_type& a = allocator_type())
1009 : m_holder(a)
1010 {
1011 this->assign(il.begin(), il.end());
1012 }
1013 #endif
1014
1015 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1016
1017 //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
1018 //!
1019 //! <b>Throws</b>: If T's move constructor or allocation throws
1020 //!
1021 //! <b>Complexity</b>: Linear.
1022 //!
1023 //! <b>Note</b>: Non-standard extension to support static_vector
1024 template<class OtherAllocator>
1025 vector(BOOST_RV_REF_BEG vector<T, OtherAllocator> BOOST_RV_REF_END x
1026 , typename dtl::enable_if_c
1027 < dtl::is_version<OtherAllocator, 0>::value>::type * = 0
1028 )
1029 : m_holder(boost::move(x.m_holder))
1030 {}
1031
1032 #endif //!defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1033
1034 //! <b>Effects</b>: Copy constructs a vector using the specified allocator.
1035 //!
1036 //! <b>Postcondition</b>: x == *this.
1037 //!
1038 //! <b>Throws</b>: If allocation
1039 //! throws or T's copy constructor throws.
1040 //!
1041 //! <b>Complexity</b>: Linear to the elements x contains.
1042 vector(const vector &x, const allocator_type &a)
1043 : m_holder(vector_uninitialized_size, a, x.size())
1044 {
1045 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
1046 this->num_alloc += x.size() != 0;
1047 #endif
1048 ::boost::container::uninitialized_copy_alloc_n_source
1049 ( this->m_holder.alloc(), x.priv_raw_begin()
1050 , x.size(), this->priv_raw_begin());
1051 }
1052
1053 //! <b>Effects</b>: Move constructor using the specified allocator.
1054 //! Moves x's resources to *this if a == allocator_type().
1055 //! Otherwise copies values from x to *this.
1056 //!
1057 //! <b>Throws</b>: If allocation or T's copy constructor throws.
1058 //!
1059 //! <b>Complexity</b>: Constant if a == x.get_allocator(), linear otherwise.
1060 vector(BOOST_RV_REF(vector) x, const allocator_type &a)
1061 : m_holder( vector_uninitialized_size, a
1062 , is_propagable_from(x.get_stored_allocator(), x.m_holder.start(), a, true) ? 0 : x.size()
1063 )
1064 {
1065 if(is_propagable_from(x.get_stored_allocator(), x.m_holder.start(), a, true)){
1066 this->m_holder.steal_resources(x.m_holder);
1067 }
1068 else{
1069 const size_type n = x.size();
1070 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
1071 this->num_alloc += n != 0;
1072 #endif
1073 ::boost::container::uninitialized_move_alloc_n_source
1074 ( this->m_holder.alloc(), x.priv_raw_begin()
1075 , n, this->priv_raw_begin());
1076 }
1077 }
1078
1079 //! <b>Effects</b>: Destroys the vector. All stored values are destroyed
1080 //! and used memory is deallocated.
1081 //!
1082 //! <b>Throws</b>: Nothing.
1083 //!
1084 //! <b>Complexity</b>: Linear to the number of elements.
1085 ~vector() BOOST_NOEXCEPT_OR_NOTHROW
1086 {
1087 boost::container::destroy_alloc_n
1088 (this->get_stored_allocator(), this->priv_raw_begin(), this->m_holder.m_size);
1089 //vector_alloc_holder deallocates the data
1090 }
1091
1092 //! <b>Effects</b>: Makes *this contain the same elements as x.
1093 //!
1094 //! <b>Postcondition</b>: this->size() == x.size(). *this contains a copy
1095 //! of each of x's elements.
1096 //!
1097 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment throws.
1098 //!
1099 //! <b>Complexity</b>: Linear to the number of elements in x.
1100 BOOST_CONTAINER_FORCEINLINE vector& operator=(BOOST_COPY_ASSIGN_REF(vector) x)
1101 {
1102 if (&x != this){
1103 this->priv_copy_assign(x);
1104 }
1105 return *this;
1106 }
1107
1108 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1109 //! <b>Effects</b>: Make *this container contains elements from il.
1110 //!
1111 //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
1112 BOOST_CONTAINER_FORCEINLINE vector& operator=(std::initializer_list<value_type> il)
1113 {
1114 this->assign(il.begin(), il.end());
1115 return *this;
1116 }
1117 #endif
1118
1119 //! <b>Effects</b>: Move assignment. All x's values are transferred to *this.
1120 //!
1121 //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
1122 //! before the function.
1123 //!
1124 //! <b>Throws</b>: If allocator_traits_type::propagate_on_container_move_assignment
1125 //! is false and (allocation throws or value_type's move constructor throws)
1126 //!
1127 //! <b>Complexity</b>: Constant if allocator_traits_type::
1128 //! propagate_on_container_move_assignment is true or
1129 //! this->get>allocator() == x.get_allocator(). Linear otherwise.
1130 BOOST_CONTAINER_FORCEINLINE vector& operator=(BOOST_RV_REF(vector) x)
1131 BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value
1132 || allocator_traits_type::is_always_equal::value)
1133 {
1134 BOOST_ASSERT(&x != this);
1135 this->priv_move_assign(boost::move(x));
1136 return *this;
1137 }
1138
1139 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1140
1141 //! <b>Effects</b>: Move assignment. All x's values are transferred to *this.
1142 //!
1143 //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
1144 //! before the function.
1145 //!
1146 //! <b>Throws</b>: If move constructor/assignment of T throws or allocation throws
1147 //!
1148 //! <b>Complexity</b>: Linear.
1149 //!
1150 //! <b>Note</b>: Non-standard extension to support static_vector
1151 template<class OtherAllocator>
1152 BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_and
1153 < vector&
1154 , dtl::is_version<OtherAllocator, 0>
1155 , dtl::is_different<OtherAllocator, allocator_type>
1156 >::type
1157 operator=(BOOST_RV_REF_BEG vector<value_type, OtherAllocator> BOOST_RV_REF_END x)
1158 {
1159 this->priv_move_assign(boost::move(x));
1160 return *this;
1161 }
1162
1163 //! <b>Effects</b>: Copy assignment. All x's values are copied to *this.
1164 //!
1165 //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
1166 //! before the function.
1167 //!
1168 //! <b>Throws</b>: If move constructor/assignment of T throws or allocation throws
1169 //!
1170 //! <b>Complexity</b>: Linear.
1171 //!
1172 //! <b>Note</b>: Non-standard extension to support static_vector
1173 template<class OtherAllocator>
1174 BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_and
1175 < vector&
1176 , dtl::is_version<OtherAllocator, 0>
1177 , dtl::is_different<OtherAllocator, allocator_type>
1178 >::type
1179 operator=(const vector<value_type, OtherAllocator> &x)
1180 {
1181 this->priv_copy_assign(x);
1182 return *this;
1183 }
1184
1185 #endif
1186
1187 //! <b>Effects</b>: Assigns the the range [first, last) to *this.
1188 //!
1189 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment or
1190 //! T's constructor/assignment from dereferencing InpIt throws.
1191 //!
1192 //! <b>Complexity</b>: Linear to n.
1193 template <class InIt>
1194 void assign(InIt first, InIt last
1195 //Input iterators or version 0 allocator
1196 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_or
1197 < void
1198 BOOST_MOVE_I dtl::is_convertible<InIt BOOST_MOVE_I size_type>
1199 BOOST_MOVE_I dtl::and_
1200 < dtl::is_different<alloc_version BOOST_MOVE_I version_0>
1201 BOOST_MOVE_I dtl::is_not_input_iterator<InIt>
1202 >
1203 >::type * = 0)
1204 )
1205 {
1206 //Overwrite all elements we can from [first, last)
1207 iterator cur = this->begin();
1208 const iterator end_it = this->end();
1209 for ( ; first != last && cur != end_it; ++cur, ++first){
1210 *cur = *first;
1211 }
1212
1213 if (first == last){
1214 //There are no more elements in the sequence, erase remaining
1215 T* const end_pos = this->priv_raw_end();
1216 const size_type n = static_cast<size_type>(end_pos - boost::movelib::iterator_to_raw_pointer(cur));
1217 this->priv_destroy_last_n(n);
1218 }
1219 else{
1220 //There are more elements in the range, insert the remaining ones
1221 this->insert(this->cend(), first, last);
1222 }
1223 }
1224
1225 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1226 //! <b>Effects</b>: Assigns the the range [il.begin(), il.end()) to *this.
1227 //!
1228 //! <b>Throws</b>: If memory allocation throws or
1229 //! T's constructor from dereferencing iniializer_list iterator throws.
1230 //!
1231 BOOST_CONTAINER_FORCEINLINE void assign(std::initializer_list<T> il)
1232 {
1233 this->assign(il.begin(), il.end());
1234 }
1235 #endif
1236
1237 //! <b>Effects</b>: Assigns the the range [first, last) to *this.
1238 //!
1239 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment or
1240 //! T's constructor/assignment from dereferencing InpIt throws.
1241 //!
1242 //! <b>Complexity</b>: Linear to n.
1243 template <class FwdIt>
1244 void assign(FwdIt first, FwdIt last
1245 //Forward iterators and version > 0 allocator
1246 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_or
1247 < void
1248 BOOST_MOVE_I dtl::is_same<alloc_version BOOST_MOVE_I version_0>
1249 BOOST_MOVE_I dtl::is_convertible<FwdIt BOOST_MOVE_I size_type>
1250 BOOST_MOVE_I dtl::is_input_iterator<FwdIt>
1251 >::type * = 0)
1252 )
1253 {
1254 //For Fwd iterators the standard only requires EmplaceConstructible and assignable from *first
1255 //so we can't do any backwards allocation
1256 const size_type input_sz = static_cast<size_type>(boost::container::iterator_distance(first, last));
1257 const size_type old_capacity = this->capacity();
1258 if(input_sz > old_capacity){ //If input range is too big, we need to reallocate
1259 size_type real_cap = 0;
1260 pointer reuse(this->m_holder.start());
1261 pointer const ret(this->m_holder.allocation_command(allocate_new|expand_fwd, input_sz, real_cap = input_sz, reuse));
1262 if(!reuse){ //New allocation, just emplace new values
1263 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
1264 ++this->num_alloc;
1265 #endif
1266 pointer const old_p = this->m_holder.start();
1267 if(old_p){
1268 this->priv_destroy_all();
1269 this->m_holder.deallocate(old_p, old_capacity);
1270 }
1271 this->m_holder.start(ret);
1272 this->m_holder.capacity(real_cap);
1273 this->m_holder.m_size = 0;
1274 this->priv_uninitialized_construct_at_end(first, last);
1275 return;
1276 }
1277 else{
1278 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
1279 ++this->num_expand_fwd;
1280 #endif
1281 this->m_holder.capacity(real_cap);
1282 //Forward expansion, use assignment + back deletion/construction that comes later
1283 }
1284 }
1285
1286 boost::container::copy_assign_range_alloc_n(this->m_holder.alloc(), first, input_sz, this->priv_raw_begin(), this->size());
1287 this->m_holder.m_size = input_sz;
1288 }
1289
1290 //! <b>Effects</b>: Assigns the n copies of val to *this.
1291 //!
1292 //! <b>Throws</b>: If memory allocation throws or
1293 //! T's copy/move constructor/assignment throws.
1294 //!
1295 //! <b>Complexity</b>: Linear to n.
1296 BOOST_CONTAINER_FORCEINLINE void assign(size_type n, const value_type& val)
1297 { this->assign(cvalue_iterator(val, n), cvalue_iterator()); }
1298
1299 //! <b>Effects</b>: Returns a copy of the internal allocator.
1300 //!
1301 //! <b>Throws</b>: If allocator's copy constructor throws.
1302 //!
1303 //! <b>Complexity</b>: Constant.
1304 allocator_type get_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
1305 { return this->m_holder.alloc(); }
1306
1307 //! <b>Effects</b>: Returns a reference to the internal allocator.
1308 //!
1309 //! <b>Throws</b>: Nothing
1310 //!
1311 //! <b>Complexity</b>: Constant.
1312 //!
1313 //! <b>Note</b>: Non-standard extension.
1314 BOOST_CONTAINER_FORCEINLINE stored_allocator_type &get_stored_allocator() BOOST_NOEXCEPT_OR_NOTHROW
1315 { return this->m_holder.alloc(); }
1316
1317 //! <b>Effects</b>: Returns a reference to the internal allocator.
1318 //!
1319 //! <b>Throws</b>: Nothing
1320 //!
1321 //! <b>Complexity</b>: Constant.
1322 //!
1323 //! <b>Note</b>: Non-standard extension.
1324 BOOST_CONTAINER_FORCEINLINE const stored_allocator_type &get_stored_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
1325 { return this->m_holder.alloc(); }
1326
1327 //////////////////////////////////////////////
1328 //
1329 // iterators
1330 //
1331 //////////////////////////////////////////////
1332
1333 //! <b>Effects</b>: Returns an iterator to the first element contained in the vector.
1334 //!
1335 //! <b>Throws</b>: Nothing.
1336 //!
1337 //! <b>Complexity</b>: Constant.
1338 BOOST_CONTAINER_FORCEINLINE iterator begin() BOOST_NOEXCEPT_OR_NOTHROW
1339 { return iterator(this->m_holder.start()); }
1340
1341 //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
1342 //!
1343 //! <b>Throws</b>: Nothing.
1344 //!
1345 //! <b>Complexity</b>: Constant.
1346 BOOST_CONTAINER_FORCEINLINE const_iterator begin() const BOOST_NOEXCEPT_OR_NOTHROW
1347 { return const_iterator(this->m_holder.start()); }
1348
1349 //! <b>Effects</b>: Returns an iterator to the end of the vector.
1350 //!
1351 //! <b>Throws</b>: Nothing.
1352 //!
1353 //! <b>Complexity</b>: Constant.
1354 BOOST_CONTAINER_FORCEINLINE iterator end() BOOST_NOEXCEPT_OR_NOTHROW
1355 { return iterator(this->m_holder.start() + this->m_holder.m_size); }
1356
1357 //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
1358 //!
1359 //! <b>Throws</b>: Nothing.
1360 //!
1361 //! <b>Complexity</b>: Constant.
1362 BOOST_CONTAINER_FORCEINLINE const_iterator end() const BOOST_NOEXCEPT_OR_NOTHROW
1363 { return this->cend(); }
1364
1365 //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
1366 //! of the reversed vector.
1367 //!
1368 //! <b>Throws</b>: Nothing.
1369 //!
1370 //! <b>Complexity</b>: Constant.
1371 BOOST_CONTAINER_FORCEINLINE reverse_iterator rbegin() BOOST_NOEXCEPT_OR_NOTHROW
1372 { return reverse_iterator(this->end()); }
1373
1374 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
1375 //! of the reversed vector.
1376 //!
1377 //! <b>Throws</b>: Nothing.
1378 //!
1379 //! <b>Complexity</b>: Constant.
1380 BOOST_CONTAINER_FORCEINLINE const_reverse_iterator rbegin() const BOOST_NOEXCEPT_OR_NOTHROW
1381 { return this->crbegin(); }
1382
1383 //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
1384 //! of the reversed vector.
1385 //!
1386 //! <b>Throws</b>: Nothing.
1387 //!
1388 //! <b>Complexity</b>: Constant.
1389 BOOST_CONTAINER_FORCEINLINE reverse_iterator rend() BOOST_NOEXCEPT_OR_NOTHROW
1390 { return reverse_iterator(this->begin()); }
1391
1392 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
1393 //! of the reversed vector.
1394 //!
1395 //! <b>Throws</b>: Nothing.
1396 //!
1397 //! <b>Complexity</b>: Constant.
1398 BOOST_CONTAINER_FORCEINLINE const_reverse_iterator rend() const BOOST_NOEXCEPT_OR_NOTHROW
1399 { return this->crend(); }
1400
1401 //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
1402 //!
1403 //! <b>Throws</b>: Nothing.
1404 //!
1405 //! <b>Complexity</b>: Constant.
1406 BOOST_CONTAINER_FORCEINLINE const_iterator cbegin() const BOOST_NOEXCEPT_OR_NOTHROW
1407 { return const_iterator(this->m_holder.start()); }
1408
1409 //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
1410 //!
1411 //! <b>Throws</b>: Nothing.
1412 //!
1413 //! <b>Complexity</b>: Constant.
1414 BOOST_CONTAINER_FORCEINLINE const_iterator cend() const BOOST_NOEXCEPT_OR_NOTHROW
1415 { return const_iterator(this->m_holder.start() + this->m_holder.m_size); }
1416
1417 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
1418 //! of the reversed vector.
1419 //!
1420 //! <b>Throws</b>: Nothing.
1421 //!
1422 //! <b>Complexity</b>: Constant.
1423 BOOST_CONTAINER_FORCEINLINE const_reverse_iterator crbegin() const BOOST_NOEXCEPT_OR_NOTHROW
1424 { return const_reverse_iterator(this->end());}
1425
1426 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
1427 //! of the reversed vector.
1428 //!
1429 //! <b>Throws</b>: Nothing.
1430 //!
1431 //! <b>Complexity</b>: Constant.
1432 BOOST_CONTAINER_FORCEINLINE const_reverse_iterator crend() const BOOST_NOEXCEPT_OR_NOTHROW
1433 { return const_reverse_iterator(this->begin()); }
1434
1435 //////////////////////////////////////////////
1436 //
1437 // capacity
1438 //
1439 //////////////////////////////////////////////
1440
1441 //! <b>Effects</b>: Returns true if the vector contains no elements.
1442 //!
1443 //! <b>Throws</b>: Nothing.
1444 //!
1445 //! <b>Complexity</b>: Constant.
1446 BOOST_CONTAINER_FORCEINLINE bool empty() const BOOST_NOEXCEPT_OR_NOTHROW
1447 { return !this->m_holder.m_size; }
1448
1449 //! <b>Effects</b>: Returns the number of the elements contained in the vector.
1450 //!
1451 //! <b>Throws</b>: Nothing.
1452 //!
1453 //! <b>Complexity</b>: Constant.
1454 BOOST_CONTAINER_FORCEINLINE size_type size() const BOOST_NOEXCEPT_OR_NOTHROW
1455 { return this->m_holder.m_size; }
1456
1457 //! <b>Effects</b>: Returns the largest possible size of the vector.
1458 //!
1459 //! <b>Throws</b>: Nothing.
1460 //!
1461 //! <b>Complexity</b>: Constant.
1462 BOOST_CONTAINER_FORCEINLINE size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
1463 { return allocator_traits_type::max_size(this->m_holder.alloc()); }
1464
1465 //! <b>Effects</b>: Inserts or erases elements at the end such that
1466 //! the size becomes n. New elements are value initialized.
1467 //!
1468 //! <b>Throws</b>: If memory allocation throws, or T's copy/move or value initialization throws.
1469 //!
1470 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1471 void resize(size_type new_size)
1472 { this->priv_resize(new_size, value_init); }
1473
1474 //! <b>Effects</b>: Inserts or erases elements at the end such that
1475 //! the size becomes n. New elements are default initialized.
1476 //!
1477 //! <b>Throws</b>: If memory allocation throws, or T's copy/move or default initialization throws.
1478 //!
1479 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1480 //!
1481 //! <b>Note</b>: Non-standard extension
1482 void resize(size_type new_size, default_init_t)
1483 { this->priv_resize(new_size, default_init); }
1484
1485 //! <b>Effects</b>: Inserts or erases elements at the end such that
1486 //! the size becomes n. New elements are copy constructed from x.
1487 //!
1488 //! <b>Throws</b>: If memory allocation throws, or T's copy/move constructor throws.
1489 //!
1490 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1491 void resize(size_type new_size, const T& x)
1492 { this->priv_resize(new_size, x); }
1493
1494 //! <b>Effects</b>: Number of elements for which memory has been allocated.
1495 //! capacity() is always greater than or equal to size().
1496 //!
1497 //! <b>Throws</b>: Nothing.
1498 //!
1499 //! <b>Complexity</b>: Constant.
1500 BOOST_CONTAINER_FORCEINLINE size_type capacity() const BOOST_NOEXCEPT_OR_NOTHROW
1501 { return this->m_holder.capacity(); }
1502
1503 //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
1504 //! effect. Otherwise, it is a request for allocation of additional memory.
1505 //! If the request is successful, then capacity() is greater than or equal to
1506 //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
1507 //!
1508 //! <b>Throws</b>: If memory allocation allocation throws or T's copy/move constructor throws.
1509 BOOST_CONTAINER_FORCEINLINE void reserve(size_type new_cap)
1510 {
1511 if (this->capacity() < new_cap){
1512 this->priv_reserve_no_capacity(new_cap, alloc_version());
1513 }
1514 }
1515
1516 //! <b>Effects</b>: Tries to deallocate the excess of memory created
1517 //! with previous allocations. The size of the vector is unchanged
1518 //!
1519 //! <b>Throws</b>: If memory allocation throws, or T's copy/move constructor throws.
1520 //!
1521 //! <b>Complexity</b>: Linear to size().
1522 BOOST_CONTAINER_FORCEINLINE void shrink_to_fit()
1523 { this->priv_shrink_to_fit(alloc_version()); }
1524
1525 //////////////////////////////////////////////
1526 //
1527 // element access
1528 //
1529 //////////////////////////////////////////////
1530
1531 //! <b>Requires</b>: !empty()
1532 //!
1533 //! <b>Effects</b>: Returns a reference to the first
1534 //! element of the container.
1535 //!
1536 //! <b>Throws</b>: Nothing.
1537 //!
1538 //! <b>Complexity</b>: Constant.
1539 reference front() BOOST_NOEXCEPT_OR_NOTHROW
1540 {
1541 BOOST_ASSERT(!this->empty());
1542 return *this->m_holder.start();
1543 }
1544
1545 //! <b>Requires</b>: !empty()
1546 //!
1547 //! <b>Effects</b>: Returns a const reference to the first
1548 //! element of the container.
1549 //!
1550 //! <b>Throws</b>: Nothing.
1551 //!
1552 //! <b>Complexity</b>: Constant.
1553 const_reference front() const BOOST_NOEXCEPT_OR_NOTHROW
1554 {
1555 BOOST_ASSERT(!this->empty());
1556 return *this->m_holder.start();
1557 }
1558
1559 //! <b>Requires</b>: !empty()
1560 //!
1561 //! <b>Effects</b>: Returns a reference to the last
1562 //! element of the container.
1563 //!
1564 //! <b>Throws</b>: Nothing.
1565 //!
1566 //! <b>Complexity</b>: Constant.
1567 reference back() BOOST_NOEXCEPT_OR_NOTHROW
1568 {
1569 BOOST_ASSERT(!this->empty());
1570 return this->m_holder.start()[this->m_holder.m_size - 1];
1571 }
1572
1573 //! <b>Requires</b>: !empty()
1574 //!
1575 //! <b>Effects</b>: Returns a const reference to the last
1576 //! element of the container.
1577 //!
1578 //! <b>Throws</b>: Nothing.
1579 //!
1580 //! <b>Complexity</b>: Constant.
1581 const_reference back() const BOOST_NOEXCEPT_OR_NOTHROW
1582 {
1583 BOOST_ASSERT(!this->empty());
1584 return this->m_holder.start()[this->m_holder.m_size - 1];
1585 }
1586
1587 //! <b>Requires</b>: size() > n.
1588 //!
1589 //! <b>Effects</b>: Returns a reference to the nth element
1590 //! from the beginning of the container.
1591 //!
1592 //! <b>Throws</b>: Nothing.
1593 //!
1594 //! <b>Complexity</b>: Constant.
1595 reference operator[](size_type n) BOOST_NOEXCEPT_OR_NOTHROW
1596 {
1597 BOOST_ASSERT(this->m_holder.m_size > n);
1598 return this->m_holder.start()[n];
1599 }
1600
1601 //! <b>Requires</b>: size() > n.
1602 //!
1603 //! <b>Effects</b>: Returns a const reference to the nth element
1604 //! from the beginning of the container.
1605 //!
1606 //! <b>Throws</b>: Nothing.
1607 //!
1608 //! <b>Complexity</b>: Constant.
1609 const_reference operator[](size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
1610 {
1611 BOOST_ASSERT(this->m_holder.m_size > n);
1612 return this->m_holder.start()[n];
1613 }
1614
1615 //! <b>Requires</b>: size() >= n.
1616 //!
1617 //! <b>Effects</b>: Returns an iterator to the nth element
1618 //! from the beginning of the container. Returns end()
1619 //! if n == size().
1620 //!
1621 //! <b>Throws</b>: Nothing.
1622 //!
1623 //! <b>Complexity</b>: Constant.
1624 //!
1625 //! <b>Note</b>: Non-standard extension
1626 iterator nth(size_type n) BOOST_NOEXCEPT_OR_NOTHROW
1627 {
1628 BOOST_ASSERT(this->m_holder.m_size >= n);
1629 return iterator(this->m_holder.start()+n);
1630 }
1631
1632 //! <b>Requires</b>: size() >= n.
1633 //!
1634 //! <b>Effects</b>: Returns a const_iterator to the nth element
1635 //! from the beginning of the container. Returns end()
1636 //! if n == size().
1637 //!
1638 //! <b>Throws</b>: Nothing.
1639 //!
1640 //! <b>Complexity</b>: Constant.
1641 //!
1642 //! <b>Note</b>: Non-standard extension
1643 const_iterator nth(size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
1644 {
1645 BOOST_ASSERT(this->m_holder.m_size >= n);
1646 return const_iterator(this->m_holder.start()+n);
1647 }
1648
1649 //! <b>Requires</b>: begin() <= p <= end().
1650 //!
1651 //! <b>Effects</b>: Returns the index of the element pointed by p
1652 //! and size() if p == end().
1653 //!
1654 //! <b>Throws</b>: Nothing.
1655 //!
1656 //! <b>Complexity</b>: Constant.
1657 //!
1658 //! <b>Note</b>: Non-standard extension
1659 size_type index_of(iterator p) BOOST_NOEXCEPT_OR_NOTHROW
1660 {
1661 //Range check assert done in priv_index_of
1662 return this->priv_index_of(vector_iterator_get_ptr(p));
1663 }
1664
1665 //! <b>Requires</b>: begin() <= p <= end().
1666 //!
1667 //! <b>Effects</b>: Returns the index of the element pointed by p
1668 //! and size() if p == end().
1669 //!
1670 //! <b>Throws</b>: Nothing.
1671 //!
1672 //! <b>Complexity</b>: Constant.
1673 //!
1674 //! <b>Note</b>: Non-standard extension
1675 size_type index_of(const_iterator p) const BOOST_NOEXCEPT_OR_NOTHROW
1676 {
1677 //Range check assert done in priv_index_of
1678 return this->priv_index_of(vector_iterator_get_ptr(p));
1679 }
1680
1681 //! <b>Requires</b>: size() > n.
1682 //!
1683 //! <b>Effects</b>: Returns a reference to the nth element
1684 //! from the beginning of the container.
1685 //!
1686 //! <b>Throws</b>: std::range_error if n >= size()
1687 //!
1688 //! <b>Complexity</b>: Constant.
1689 reference at(size_type n)
1690 {
1691 this->priv_throw_if_out_of_range(n);
1692 return this->m_holder.start()[n];
1693 }
1694
1695 //! <b>Requires</b>: size() > n.
1696 //!
1697 //! <b>Effects</b>: Returns a const reference to the nth element
1698 //! from the beginning of the container.
1699 //!
1700 //! <b>Throws</b>: std::range_error if n >= size()
1701 //!
1702 //! <b>Complexity</b>: Constant.
1703 const_reference at(size_type n) const
1704 {
1705 this->priv_throw_if_out_of_range(n);
1706 return this->m_holder.start()[n];
1707 }
1708
1709 //////////////////////////////////////////////
1710 //
1711 // data access
1712 //
1713 //////////////////////////////////////////////
1714
1715 //! <b>Returns</b>: A pointer such that [data(),data() + size()) is a valid range.
1716 //! For a non-empty vector, data() == &front().
1717 //!
1718 //! <b>Throws</b>: Nothing.
1719 //!
1720 //! <b>Complexity</b>: Constant.
1721 T* data() BOOST_NOEXCEPT_OR_NOTHROW
1722 { return this->priv_raw_begin(); }
1723
1724 //! <b>Returns</b>: A pointer such that [data(),data() + size()) is a valid range.
1725 //! For a non-empty vector, data() == &front().
1726 //!
1727 //! <b>Throws</b>: Nothing.
1728 //!
1729 //! <b>Complexity</b>: Constant.
1730 const T * data() const BOOST_NOEXCEPT_OR_NOTHROW
1731 { return this->priv_raw_begin(); }
1732
1733 //////////////////////////////////////////////
1734 //
1735 // modifiers
1736 //
1737 //////////////////////////////////////////////
1738
1739 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1740 //! <b>Effects</b>: Inserts an object of type T constructed with
1741 //! std::forward<Args>(args)... in the end of the vector.
1742 //!
1743 //! <b>Returns</b>: A reference to the created object.
1744 //!
1745 //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws or
1746 //! T's copy/move constructor throws.
1747 //!
1748 //! <b>Complexity</b>: Amortized constant time.
1749 template<class ...Args>
1750 BOOST_CONTAINER_FORCEINLINE reference emplace_back(BOOST_FWD_REF(Args)...args)
1751 {
1752 if (BOOST_LIKELY(this->room_enough())){
1753 //There is more memory, just construct a new object at the end
1754 T* const p = this->priv_raw_end();
1755 allocator_traits_type::construct(this->m_holder.alloc(), p, ::boost::forward<Args>(args)...);
1756 ++this->m_holder.m_size;
1757 return *p;
1758 }
1759 else{
1760 typedef dtl::insert_emplace_proxy<Allocator, T*, Args...> type;
1761 return *this->priv_forward_range_insert_no_capacity
1762 (this->back_ptr(), 1, type(::boost::forward<Args>(args)...), alloc_version());
1763 }
1764 }
1765
1766 //! <b>Effects</b>: Inserts an object of type T constructed with
1767 //! std::forward<Args>(args)... in the end of the vector.
1768 //!
1769 //! <b>Throws</b>: If the in-place constructor throws.
1770 //!
1771 //! <b>Complexity</b>: Constant time.
1772 //!
1773 //! <b>Note</b>: Non-standard extension.
1774 template<class ...Args>
1775 BOOST_CONTAINER_FORCEINLINE bool stable_emplace_back(BOOST_FWD_REF(Args)...args)
1776 {
1777 const bool is_room_enough = this->room_enough() || (alloc_version::value == 2 && this->m_holder.try_expand_fwd(1u));
1778 if (BOOST_LIKELY(is_room_enough)){
1779 //There is more memory, just construct a new object at the end
1780 allocator_traits_type::construct(this->m_holder.alloc(), this->priv_raw_end(), ::boost::forward<Args>(args)...);
1781 ++this->m_holder.m_size;
1782 }
1783 return is_room_enough;
1784 }
1785
1786 //! <b>Requires</b>: position must be a valid iterator of *this.
1787 //!
1788 //! <b>Effects</b>: Inserts an object of type T constructed with
1789 //! std::forward<Args>(args)... before position
1790 //!
1791 //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws or
1792 //! T's copy/move constructor/assignment throws.
1793 //!
1794 //! <b>Complexity</b>: If position is end(), amortized constant time
1795 //! Linear time otherwise.
1796 template<class ...Args>
1797 iterator emplace(const_iterator position, BOOST_FWD_REF(Args) ...args)
1798 {
1799 BOOST_ASSERT(this->priv_in_range_or_end(position));
1800 //Just call more general insert(pos, size, value) and return iterator
1801 typedef dtl::insert_emplace_proxy<Allocator, T*, Args...> type;
1802 return this->priv_forward_range_insert( vector_iterator_get_ptr(position), 1
1803 , type(::boost::forward<Args>(args)...));
1804 }
1805
1806 #else // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
1807
1808 #define BOOST_CONTAINER_VECTOR_EMPLACE_CODE(N) \
1809 BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
1810 BOOST_CONTAINER_FORCEINLINE reference emplace_back(BOOST_MOVE_UREF##N)\
1811 {\
1812 if (BOOST_LIKELY(this->room_enough())){\
1813 T* const p = this->priv_raw_end();\
1814 allocator_traits_type::construct (this->m_holder.alloc()\
1815 , this->priv_raw_end() BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
1816 ++this->m_holder.m_size;\
1817 return *p;\
1818 }\
1819 else{\
1820 typedef dtl::insert_emplace_proxy_arg##N<Allocator, T* BOOST_MOVE_I##N BOOST_MOVE_TARG##N> type;\
1821 return *this->priv_forward_range_insert_no_capacity\
1822 ( this->back_ptr(), 1, type(BOOST_MOVE_FWD##N), alloc_version());\
1823 }\
1824 }\
1825 \
1826 BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
1827 BOOST_CONTAINER_FORCEINLINE bool stable_emplace_back(BOOST_MOVE_UREF##N)\
1828 {\
1829 const bool is_room_enough = this->room_enough() || (alloc_version::value == 2 && this->m_holder.try_expand_fwd(1u));\
1830 if (BOOST_LIKELY(is_room_enough)){\
1831 allocator_traits_type::construct (this->m_holder.alloc()\
1832 , this->priv_raw_end() BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
1833 ++this->m_holder.m_size;\
1834 }\
1835 return is_room_enough;\
1836 }\
1837 \
1838 BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
1839 iterator emplace(const_iterator pos BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
1840 {\
1841 BOOST_ASSERT(this->priv_in_range_or_end(pos));\
1842 typedef dtl::insert_emplace_proxy_arg##N<Allocator, T* BOOST_MOVE_I##N BOOST_MOVE_TARG##N> type;\
1843 return this->priv_forward_range_insert(vector_iterator_get_ptr(pos), 1, type(BOOST_MOVE_FWD##N));\
1844 }\
1845 //
1846 BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_VECTOR_EMPLACE_CODE)
1847 #undef BOOST_CONTAINER_VECTOR_EMPLACE_CODE
1848
1849 #endif
1850
1851 #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1852 //! <b>Effects</b>: Inserts a copy of x at the end of the vector.
1853 //!
1854 //! <b>Throws</b>: If memory allocation throws or
1855 //! T's copy/move constructor throws.
1856 //!
1857 //! <b>Complexity</b>: Amortized constant time.
1858 void push_back(const T &x);
1859
1860 //! <b>Effects</b>: Constructs a new element in the end of the vector
1861 //! and moves the resources of x to this new element.
1862 //!
1863 //! <b>Throws</b>: If memory allocation throws or
1864 //! T's copy/move constructor throws.
1865 //!
1866 //! <b>Complexity</b>: Amortized constant time.
1867 void push_back(T &&x);
1868 #else
1869 BOOST_MOVE_CONVERSION_AWARE_CATCH(push_back, T, void, priv_push_back)
1870 #endif
1871
1872 #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1873 //! <b>Requires</b>: position must be a valid iterator of *this.
1874 //!
1875 //! <b>Effects</b>: Insert a copy of x before position.
1876 //!
1877 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment throws.
1878 //!
1879 //! <b>Complexity</b>: If position is end(), amortized constant time
1880 //! Linear time otherwise.
1881 iterator insert(const_iterator position, const T &x);
1882
1883 //! <b>Requires</b>: position must be a valid iterator of *this.
1884 //!
1885 //! <b>Effects</b>: Insert a new element before position with x's resources.
1886 //!
1887 //! <b>Throws</b>: If memory allocation throws.
1888 //!
1889 //! <b>Complexity</b>: If position is end(), amortized constant time
1890 //! Linear time otherwise.
1891 iterator insert(const_iterator position, T &&x);
1892 #else
1893 BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, T, iterator, priv_insert, const_iterator, const_iterator)
1894 #endif
1895
1896 //! <b>Requires</b>: p must be a valid iterator of *this.
1897 //!
1898 //! <b>Effects</b>: Insert n copies of x before pos.
1899 //!
1900 //! <b>Returns</b>: an iterator to the first inserted element or p if n is 0.
1901 //!
1902 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor throws.
1903 //!
1904 //! <b>Complexity</b>: Linear to n.
1905 iterator insert(const_iterator p, size_type n, const T& x)
1906 {
1907 BOOST_ASSERT(this->priv_in_range_or_end(p));
1908 dtl::insert_n_copies_proxy<Allocator, T*> proxy(x);
1909 return this->priv_forward_range_insert(vector_iterator_get_ptr(p), n, proxy);
1910 }
1911
1912 //! <b>Requires</b>: p must be a valid iterator of *this.
1913 //!
1914 //! <b>Effects</b>: Insert a copy of the [first, last) range before pos.
1915 //!
1916 //! <b>Returns</b>: an iterator to the first inserted element or pos if first == last.
1917 //!
1918 //! <b>Throws</b>: If memory allocation throws, T's constructor from a
1919 //! dereferenced InpIt throws or T's copy/move constructor/assignment throws.
1920 //!
1921 //! <b>Complexity</b>: Linear to boost::container::iterator_distance [first, last).
1922 template <class InIt>
1923 iterator insert(const_iterator pos, InIt first, InIt last
1924 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1925 , typename dtl::disable_if_or
1926 < void
1927 , dtl::is_convertible<InIt, size_type>
1928 , dtl::is_not_input_iterator<InIt>
1929 >::type * = 0
1930 #endif
1931 )
1932 {
1933 BOOST_ASSERT(this->priv_in_range_or_end(pos));
1934 const size_type n_pos = pos - this->cbegin();
1935 iterator it(vector_iterator_get_ptr(pos));
1936 for(;first != last; ++first){
1937 it = this->emplace(it, *first);
1938 ++it;
1939 }
1940 return iterator(this->m_holder.start() + n_pos);
1941 }
1942
1943 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1944 template <class FwdIt>
1945 iterator insert(const_iterator pos, FwdIt first, FwdIt last
1946 , typename dtl::disable_if_or
1947 < void
1948 , dtl::is_convertible<FwdIt, size_type>
1949 , dtl::is_input_iterator<FwdIt>
1950 >::type * = 0
1951 )
1952 {
1953 BOOST_ASSERT(this->priv_in_range_or_end(pos));
1954 dtl::insert_range_proxy<Allocator, FwdIt, T*> proxy(first);
1955 return this->priv_forward_range_insert(vector_iterator_get_ptr(pos), boost::container::iterator_distance(first, last), proxy);
1956 }
1957 #endif
1958
1959 //! <b>Requires</b>: p must be a valid iterator of *this. num, must
1960 //! be equal to boost::container::iterator_distance(first, last)
1961 //!
1962 //! <b>Effects</b>: Insert a copy of the [first, last) range before pos.
1963 //!
1964 //! <b>Returns</b>: an iterator to the first inserted element or pos if first == last.
1965 //!
1966 //! <b>Throws</b>: If memory allocation throws, T's constructor from a
1967 //! dereferenced InpIt throws or T's copy/move constructor/assignment throws.
1968 //!
1969 //! <b>Complexity</b>: Linear to boost::container::iterator_distance [first, last).
1970 //!
1971 //! <b>Note</b>: This function avoids a linear operation to calculate boost::container::iterator_distance[first, last)
1972 //! for forward and bidirectional iterators, and a one by one insertion for input iterators. This is a
1973 //! a non-standard extension.
1974 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1975 template <class InIt>
1976 iterator insert(const_iterator pos, size_type num, InIt first, InIt last)
1977 {
1978 BOOST_ASSERT(this->priv_in_range_or_end(pos));
1979 BOOST_ASSERT(dtl::is_input_iterator<InIt>::value ||
1980 num == static_cast<size_type>(boost::container::iterator_distance(first, last)));
1981 (void)last;
1982 dtl::insert_range_proxy<Allocator, InIt, T*> proxy(first);
1983 return this->priv_forward_range_insert(vector_iterator_get_ptr(pos), num, proxy);
1984 }
1985 #endif
1986
1987 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1988 //! <b>Requires</b>: position must be a valid iterator of *this.
1989 //!
1990 //! <b>Effects</b>: Insert a copy of the [il.begin(), il.end()) range before position.
1991 //!
1992 //! <b>Returns</b>: an iterator to the first inserted element or position if first == last.
1993 //!
1994 //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
1995 iterator insert(const_iterator position, std::initializer_list<value_type> il)
1996 {
1997 //Assertion done in insert()
1998 return this->insert(position, il.begin(), il.end());
1999 }
2000 #endif
2001
2002 //! <b>Effects</b>: Removes the last element from the container.
2003 //!
2004 //! <b>Throws</b>: Nothing.
2005 //!
2006 //! <b>Complexity</b>: Constant time.
2007 void pop_back() BOOST_NOEXCEPT_OR_NOTHROW
2008 {
2009 BOOST_ASSERT(!this->empty());
2010 //Destroy last element
2011 this->priv_destroy_last();
2012 }
2013
2014 //! <b>Effects</b>: Erases the element at position pos.
2015 //!
2016 //! <b>Throws</b>: Nothing.
2017 //!
2018 //! <b>Complexity</b>: Linear to the elements between pos and the
2019 //! last element. Constant if pos is the last element.
2020 iterator erase(const_iterator position)
2021 {
2022 BOOST_ASSERT(this->priv_in_range(position));
2023 const pointer p = vector_iterator_get_ptr(position);
2024 T *const pos_ptr = boost::movelib::to_raw_pointer(p);
2025 T *const beg_ptr = this->priv_raw_begin();
2026 T *const new_end_ptr = ::boost::container::move(pos_ptr + 1, beg_ptr + this->m_holder.m_size, pos_ptr);
2027 //Move elements forward and destroy last
2028 this->priv_destroy_last(pos_ptr != new_end_ptr);
2029 return iterator(p);
2030 }
2031
2032 //! <b>Effects</b>: Erases the elements pointed by [first, last).
2033 //!
2034 //! <b>Throws</b>: Nothing.
2035 //!
2036 //! <b>Complexity</b>: Linear to the distance between first and last
2037 //! plus linear to the elements between pos and the last element.
2038 iterator erase(const_iterator first, const_iterator last)
2039 {
2040 BOOST_ASSERT(first == last ||
2041 (first < last && this->priv_in_range(first) && this->priv_in_range_or_end(last)));
2042 if (first != last){
2043 T* const old_end_ptr = this->priv_raw_end();
2044 T* const first_ptr = boost::movelib::to_raw_pointer(vector_iterator_get_ptr(first));
2045 T* const last_ptr = boost::movelib::to_raw_pointer(vector_iterator_get_ptr(last));
2046 T* const ptr = boost::movelib::to_raw_pointer(boost::container::move(last_ptr, old_end_ptr, first_ptr));
2047 this->priv_destroy_last_n(old_end_ptr - ptr);
2048 }
2049 return iterator(vector_iterator_get_ptr(first));
2050 }
2051
2052 //! <b>Effects</b>: Swaps the contents of *this and x.
2053 //!
2054 //! <b>Throws</b>: Nothing.
2055 //!
2056 //! <b>Complexity</b>: Constant.
2057 BOOST_CONTAINER_FORCEINLINE void swap(vector& x)
2058 BOOST_NOEXCEPT_IF( ((allocator_traits_type::propagate_on_container_swap::value
2059 || allocator_traits_type::is_always_equal::value) &&
2060 !dtl::is_version<Allocator, 0>::value))
2061 {
2062 this->priv_swap(x, dtl::bool_<dtl::is_version<Allocator, 0>::value>());
2063 }
2064
2065 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2066
2067 //! <b>Effects</b>: Swaps the contents of *this and x.
2068 //!
2069 //! <b>Throws</b>: Nothing.
2070 //!
2071 //! <b>Complexity</b>: Linear
2072 //!
2073 //! <b>Note</b>: Non-standard extension to support static_vector
2074 template<class OtherAllocator>
2075 BOOST_CONTAINER_FORCEINLINE void swap(vector<T, OtherAllocator> & x
2076 , typename dtl::enable_if_and
2077 < void
2078 , dtl::is_version<OtherAllocator, 0>
2079 , dtl::is_different<OtherAllocator, allocator_type>
2080 >::type * = 0
2081 )
2082 { this->m_holder.deep_swap(x.m_holder); }
2083
2084 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2085
2086 //! <b>Effects</b>: Erases all the elements of the vector.
2087 //!
2088 //! <b>Throws</b>: Nothing.
2089 //!
2090 //! <b>Complexity</b>: Linear to the number of elements in the container.
2091 BOOST_CONTAINER_FORCEINLINE void clear() BOOST_NOEXCEPT_OR_NOTHROW
2092 { this->priv_destroy_all(); }
2093
2094 //! <b>Effects</b>: Returns true if x and y are equal
2095 //!
2096 //! <b>Complexity</b>: Linear to the number of elements in the container.
2097 BOOST_CONTAINER_FORCEINLINE friend bool operator==(const vector& x, const vector& y)
2098 { return x.size() == y.size() && ::boost::container::algo_equal(x.begin(), x.end(), y.begin()); }
2099
2100 //! <b>Effects</b>: Returns true if x and y are unequal
2101 //!
2102 //! <b>Complexity</b>: Linear to the number of elements in the container.
2103 BOOST_CONTAINER_FORCEINLINE friend bool operator!=(const vector& x, const vector& y)
2104 { return !(x == y); }
2105
2106 //! <b>Effects</b>: Returns true if x is less than y
2107 //!
2108 //! <b>Complexity</b>: Linear to the number of elements in the container.
2109 friend bool operator<(const vector& x, const vector& y)
2110 {
2111 const_iterator first1(x.cbegin()), first2(y.cbegin());
2112 const const_iterator last1(x.cend()), last2(y.cend());
2113 for ( ; (first1 != last1) && (first2 != last2); ++first1, ++first2 ) {
2114 if (*first1 < *first2) return true;
2115 if (*first2 < *first1) return false;
2116 }
2117 return (first1 == last1) && (first2 != last2);
2118 }
2119
2120 //! <b>Effects</b>: Returns true if x is greater than y
2121 //!
2122 //! <b>Complexity</b>: Linear to the number of elements in the container.
2123 BOOST_CONTAINER_FORCEINLINE friend bool operator>(const vector& x, const vector& y)
2124 { return y < x; }
2125
2126 //! <b>Effects</b>: Returns true if x is equal or less than y
2127 //!
2128 //! <b>Complexity</b>: Linear to the number of elements in the container.
2129 BOOST_CONTAINER_FORCEINLINE friend bool operator<=(const vector& x, const vector& y)
2130 { return !(y < x); }
2131
2132 //! <b>Effects</b>: Returns true if x is equal or greater than y
2133 //!
2134 //! <b>Complexity</b>: Linear to the number of elements in the container.
2135 BOOST_CONTAINER_FORCEINLINE friend bool operator>=(const vector& x, const vector& y)
2136 { return !(x < y); }
2137
2138 //! <b>Effects</b>: x.swap(y)
2139 //!
2140 //! <b>Complexity</b>: Constant.
2141 BOOST_CONTAINER_FORCEINLINE friend void swap(vector& x, vector& y)
2142 { x.swap(y); }
2143
2144 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2145 //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
2146 //! effect. Otherwise, it is a request for allocation of additional memory
2147 //! (memory expansion) that will not invalidate iterators.
2148 //! If the request is successful, then capacity() is greater than or equal to
2149 //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
2150 //!
2151 //! <b>Throws</b>: If memory allocation allocation throws or T's copy/move constructor throws.
2152 //!
2153 //! <b>Note</b>: Non-standard extension.
2154 bool stable_reserve(size_type new_cap)
2155 {
2156 const size_type cp = this->capacity();
2157 return cp >= new_cap || (alloc_version::value == 2 && this->m_holder.try_expand_fwd(new_cap - cp));
2158 }
2159
2160 //Absolutely experimental. This function might change, disappear or simply crash!
2161 template<class BiDirPosConstIt, class BiDirValueIt>
2162 BOOST_CONTAINER_FORCEINLINE void insert_ordered_at(const size_type element_count, BiDirPosConstIt last_position_it, BiDirValueIt last_value_it)
2163 {
2164 typedef vector_insert_ordered_cursor<BiDirPosConstIt, BiDirValueIt> inserter_t;
2165 return this->priv_insert_ordered_at(element_count, inserter_t(last_position_it, last_value_it));
2166 }
2167
2168 template<class InputIt>
2169 BOOST_CONTAINER_FORCEINLINE void merge(InputIt first, InputIt last)
2170 { this->merge(first, last, value_less_t()); }
2171
2172 template<class InputIt, class Compare>
2173 BOOST_CONTAINER_FORCEINLINE void merge(InputIt first, InputIt last, Compare comp)
2174 {
2175 size_type const s = this->size();
2176 size_type const c = this->capacity();
2177 size_type n = 0;
2178 size_type const free_cap = c - s;
2179 //If not input iterator and new elements don't fit in the remaining capacity, merge in new buffer
2180 if(!dtl::is_input_iterator<InputIt>::value &&
2181 free_cap < (n = static_cast<size_type>(boost::container::iterator_distance(first, last)))){
2182 this->priv_merge_in_new_buffer(first, n, comp, alloc_version());
2183 }
2184 else{
2185 iterator pos(this->insert(this->cend(), first, last));
2186 T *const raw_beg = this->priv_raw_begin();
2187 T *const raw_end = this->priv_raw_end();
2188 T *const raw_pos = raw_beg + s;
2189 boost::movelib::adaptive_merge(raw_beg, raw_pos, raw_end, comp, raw_end, free_cap - n);
2190 }
2191 }
2192
2193 template<class InputIt>
2194 BOOST_CONTAINER_FORCEINLINE void merge_unique(InputIt first, InputIt last)
2195 { this->merge_unique(first, last, value_less_t()); }
2196
2197 template<class InputIt, class Compare>
2198 BOOST_CONTAINER_FORCEINLINE void merge_unique(InputIt first, InputIt last, Compare comp)
2199 {
2200 size_type const old_size = this->size();
2201 this->priv_set_difference_back(first, last, comp);
2202 T *const raw_beg = this->priv_raw_begin();
2203 T *const raw_end = this->priv_raw_end();
2204 T *raw_pos = raw_beg + old_size;
2205 boost::movelib::adaptive_merge(raw_beg, raw_pos, raw_end, comp, raw_end, this->capacity() - this->size());
2206 }
2207
2208 private:
2209 template<class PositionValue>
2210 void priv_insert_ordered_at(const size_type element_count, PositionValue position_value)
2211 {
2212 const size_type old_size_pos = this->size();
2213 this->reserve(old_size_pos + element_count);
2214 T* const begin_ptr = this->priv_raw_begin();
2215 size_type insertions_left = element_count;
2216 size_type prev_pos = old_size_pos;
2217 size_type old_hole_size = element_count;
2218
2219 //Exception rollback. If any copy throws before the hole is filled, values
2220 //already inserted/copied at the end of the buffer will be destroyed.
2221 typename value_traits::ArrayDestructor past_hole_values_destroyer
2222 (begin_ptr + old_size_pos + element_count, this->m_holder.alloc(), size_type(0u));
2223 //Loop for each insertion backwards, first moving the elements after the insertion point,
2224 //then inserting the element.
2225 while(insertions_left){
2226 --position_value;
2227 size_type const pos = position_value.get_pos();
2228 BOOST_ASSERT(pos != size_type(-1) && pos <= old_size_pos && pos <= prev_pos);
2229 //If needed shift the range after the insertion point and the previous insertion point.
2230 //Function will take care if the shift crosses the size() boundary, using copy/move
2231 //or uninitialized copy/move if necessary.
2232 size_type new_hole_size = (pos != prev_pos)
2233 ? priv_insert_ordered_at_shift_range(pos, prev_pos, this->size(), insertions_left)
2234 : old_hole_size
2235 ;
2236 if(new_hole_size){
2237 //The hole was reduced by priv_insert_ordered_at_shift_range so expand exception rollback range backwards
2238 past_hole_values_destroyer.increment_size_backwards(prev_pos - pos);
2239 //Insert the new value in the hole
2240 allocator_traits_type::construct(this->m_holder.alloc(), begin_ptr + pos + insertions_left - 1, position_value.get_val());
2241 if(--new_hole_size){
2242 //The hole was reduced by the new insertion by one
2243 past_hole_values_destroyer.increment_size_backwards(size_type(1u));
2244 }
2245 else{
2246 //Hole was just filled, disable exception rollback and change vector size
2247 past_hole_values_destroyer.release();
2248 this->m_holder.m_size += element_count;
2249 }
2250 }
2251 else{
2252 if(old_hole_size){
2253 //Hole was just filled by priv_insert_ordered_at_shift_range, disable exception rollback and change vector size
2254 past_hole_values_destroyer.release();
2255 this->m_holder.m_size += element_count;
2256 }
2257 //Insert the new value in the already constructed range
2258 begin_ptr[pos + insertions_left - 1] = position_value.get_val();
2259 }
2260 --insertions_left;
2261 old_hole_size = new_hole_size;
2262 prev_pos = pos;
2263 }
2264 }
2265
2266 template<class InputIt, class Compare>
2267 void priv_set_difference_back(InputIt first1, InputIt last1, Compare comp)
2268 {
2269 T * old_first2 = this->priv_raw_begin();
2270 T * first2 = old_first2;
2271 T * last2 = this->priv_raw_end();
2272
2273 while (first1 != last1) {
2274 if (first2 == last2){
2275 this->insert(this->cend(), first1, last1);
2276 return;
2277 }
2278
2279 if (comp(*first1, *first2)) {
2280 this->emplace_back(*first1);
2281 T * const raw_begin = this->priv_raw_begin();
2282 if(old_first2 != raw_begin)
2283 {
2284 //Reallocation happened, update range
2285 first2 = raw_begin + (first2 - old_first2);
2286 last2 = raw_begin + (last2 - old_first2);
2287 old_first2 = raw_begin;
2288 }
2289 ++first1;
2290 }
2291 else {
2292 if (!comp(*first2, *first1)) {
2293 ++first1;
2294 }
2295 ++first2;
2296 }
2297 }
2298 }
2299
2300 template<class FwdIt, class Compare>
2301 BOOST_CONTAINER_FORCEINLINE void priv_merge_in_new_buffer(FwdIt, size_type, Compare, version_0)
2302 {
2303 throw_bad_alloc();
2304 }
2305
2306 template<class FwdIt, class Compare, class Version>
2307 void priv_merge_in_new_buffer(FwdIt first, size_type n, Compare comp, Version)
2308 {
2309 size_type const new_size = this->size() + n;
2310 size_type new_cap = new_size;
2311 pointer p = pointer();
2312 pointer const new_storage = this->m_holder.allocation_command(allocate_new, new_size, new_cap, p);
2313
2314 BOOST_ASSERT((new_cap >= this->size() ) && (new_cap - this->size()) >= n);
2315 allocator_type &a = this->m_holder.alloc();
2316 typename value_traits::ArrayDeallocator new_buffer_deallocator(new_storage, a, new_cap);
2317 typename value_traits::ArrayDestructor new_values_destroyer(new_storage, a, 0u);
2318 T* pbeg = this->priv_raw_begin();
2319 size_type const old_size = this->size();
2320 T* const pend = pbeg + old_size;
2321 T* d_first = boost::movelib::to_raw_pointer(new_storage);
2322 size_type added = n;
2323 //Merge in new buffer loop
2324 while(1){
2325 if(!n) {
2326 ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), pbeg, pend, d_first);
2327 break;
2328 }
2329 else if(pbeg == pend) {
2330 ::boost::container::uninitialized_move_alloc_n(this->m_holder.alloc(), first, n, d_first);
2331 break;
2332 }
2333 //maintain stability moving external values only if they are strictly less
2334 else if(comp(*first, *pbeg)) {
2335 allocator_traits_type::construct( this->m_holder.alloc(), d_first, *first );
2336 new_values_destroyer.increment_size(1u);
2337 ++first;
2338 --n;
2339 ++d_first;
2340 }
2341 else{
2342 allocator_traits_type::construct( this->m_holder.alloc(), d_first, boost::move(*pbeg) );
2343 new_values_destroyer.increment_size(1u);
2344 ++pbeg;
2345 ++d_first;
2346 }
2347 }
2348
2349 //Nothrow operations
2350 pointer const old_p = this->m_holder.start();
2351 size_type const old_cap = this->m_holder.capacity();
2352 boost::container::destroy_alloc_n(a, boost::movelib::to_raw_pointer(old_p), old_size);
2353 this->m_holder.deallocate(old_p, old_cap);
2354 this->m_holder.m_size = old_size + added;
2355 this->m_holder.start(new_storage);
2356 this->m_holder.capacity(new_cap);
2357 new_buffer_deallocator.release();
2358 new_values_destroyer.release();
2359 }
2360
2361 BOOST_CONTAINER_FORCEINLINE bool room_enough() const
2362 { return this->m_holder.m_size < this->m_holder.capacity(); }
2363
2364 BOOST_CONTAINER_FORCEINLINE pointer back_ptr() const
2365 { return this->m_holder.start() + this->m_holder.m_size; }
2366
2367 size_type priv_index_of(pointer p) const
2368 {
2369 BOOST_ASSERT(this->m_holder.start() <= p);
2370 BOOST_ASSERT(p <= (this->m_holder.start()+this->size()));
2371 return static_cast<size_type>(p - this->m_holder.start());
2372 }
2373
2374 template<class OtherAllocator>
2375 void priv_move_assign(BOOST_RV_REF_BEG vector<T, OtherAllocator> BOOST_RV_REF_END x
2376 , typename dtl::enable_if_c
2377 < dtl::is_version<OtherAllocator, 0>::value >::type * = 0)
2378 {
2379 if(!dtl::is_same<OtherAllocator, allocator_type>::value &&
2380 this->capacity() < x.size()){
2381 throw_bad_alloc();
2382 }
2383 T* const this_start = this->priv_raw_begin();
2384 T* const other_start = x.priv_raw_begin();
2385 const size_type this_sz = m_holder.m_size;
2386 const size_type other_sz = static_cast<size_type>(x.m_holder.m_size);
2387 boost::container::move_assign_range_alloc_n(this->m_holder.alloc(), other_start, other_sz, this_start, this_sz);
2388 this->m_holder.m_size = other_sz;
2389 }
2390
2391 template<class OtherAllocator>
2392 void priv_move_assign(BOOST_RV_REF_BEG vector<T, OtherAllocator> BOOST_RV_REF_END x
2393 , typename dtl::disable_if_or
2394 < void
2395 , dtl::is_version<OtherAllocator, 0>
2396 , dtl::is_different<OtherAllocator, allocator_type>
2397 >::type * = 0)
2398 {
2399 //for move assignment, no aliasing (&x != this) is assummed.
2400 BOOST_ASSERT(this != &x);
2401 allocator_type &this_alloc = this->m_holder.alloc();
2402 allocator_type &x_alloc = x.m_holder.alloc();
2403 const bool propagate_alloc = allocator_traits_type::propagate_on_container_move_assignment::value;
2404
2405 const bool is_propagable_from_x = is_propagable_from(x_alloc, x.m_holder.start(), this_alloc, propagate_alloc);
2406 const bool is_propagable_from_t = is_propagable_from(this_alloc, m_holder.start(), x_alloc, propagate_alloc);
2407 const bool are_both_propagable = is_propagable_from_x && is_propagable_from_t;
2408
2409 //Resources can be transferred if both allocators are
2410 //going to be equal after this function (either propagated or already equal)
2411 if(are_both_propagable){
2412 //Destroy objects but retain memory in case x reuses it in the future
2413 this->clear();
2414 this->m_holder.swap_resources(x.m_holder);
2415 }
2416 else if(is_propagable_from_x){
2417 this->clear();
2418 this->m_holder.deallocate(this->m_holder.m_start, this->m_holder.m_capacity);
2419 this->m_holder.steal_resources(x.m_holder);
2420 }
2421 //Else do a one by one move
2422 else{
2423 this->assign( boost::make_move_iterator(boost::movelib::iterator_to_raw_pointer(x.begin()))
2424 , boost::make_move_iterator(boost::movelib::iterator_to_raw_pointer(x.end() ))
2425 );
2426 }
2427 //Move allocator if needed
2428 dtl::move_alloc(this_alloc, x_alloc, dtl::bool_<propagate_alloc>());
2429 }
2430
2431 template<class OtherAllocator>
2432 void priv_copy_assign(const vector<T, OtherAllocator> &x
2433 , typename dtl::enable_if_c
2434 < dtl::is_version<OtherAllocator, 0>::value >::type * = 0)
2435 {
2436 if(!dtl::is_same<OtherAllocator, allocator_type>::value &&
2437 this->capacity() < x.size()){
2438 throw_bad_alloc();
2439 }
2440 T* const this_start = this->priv_raw_begin();
2441 T* const other_start = x.priv_raw_begin();
2442 const size_type this_sz = m_holder.m_size;
2443 const size_type other_sz = static_cast<size_type>(x.m_holder.m_size);
2444 boost::container::copy_assign_range_alloc_n(this->m_holder.alloc(), other_start, other_sz, this_start, this_sz);
2445 this->m_holder.m_size = other_sz;
2446 }
2447
2448 template<class OtherAllocator>
2449 typename dtl::disable_if_or
2450 < void
2451 , dtl::is_version<OtherAllocator, 0>
2452 , dtl::is_different<OtherAllocator, allocator_type>
2453 >::type
2454 priv_copy_assign(const vector<T, OtherAllocator> &x)
2455 {
2456 allocator_type &this_alloc = this->m_holder.alloc();
2457 const allocator_type &x_alloc = x.m_holder.alloc();
2458 dtl::bool_<allocator_traits_type::
2459 propagate_on_container_copy_assignment::value> flag;
2460 if(flag && this_alloc != x_alloc){
2461 this->clear();
2462 this->shrink_to_fit();
2463 }
2464 dtl::assign_alloc(this_alloc, x_alloc, flag);
2465 this->assign( x.priv_raw_begin(), x.priv_raw_end() );
2466 }
2467
2468 template<class Vector> //Template it to avoid it in explicit instantiations
2469 void priv_swap(Vector &x, dtl::true_type) //version_0
2470 { this->m_holder.deep_swap(x.m_holder); }
2471
2472 template<class Vector> //Template it to avoid it in explicit instantiations
2473 void priv_swap(Vector &x, dtl::false_type) //version_N
2474 {
2475 const bool propagate_alloc = allocator_traits_type::propagate_on_container_swap::value;
2476 if(are_swap_propagable( this->get_stored_allocator(), this->m_holder.start()
2477 , x.get_stored_allocator(), x.m_holder.start(), propagate_alloc)){
2478 //Just swap internals
2479 this->m_holder.swap_resources(x.m_holder);
2480 }
2481 else{
2482 //Else swap element by element...
2483 bool const t_smaller = this->size() < x.size();
2484 vector &sml = t_smaller ? *this : x;
2485 vector &big = t_smaller ? x : *this;
2486
2487 size_type const common_elements = sml.size();
2488 for(size_type i = 0; i != common_elements; ++i){
2489 boost::adl_move_swap(sml[i], big[i]);
2490 }
2491 //... and move-insert the remaining range
2492 sml.insert( sml.cend()
2493 , boost::make_move_iterator(boost::movelib::iterator_to_raw_pointer(big.nth(common_elements)))
2494 , boost::make_move_iterator(boost::movelib::iterator_to_raw_pointer(big.end()))
2495 );
2496 //Destroy remaining elements
2497 big.erase(big.nth(common_elements), big.cend());
2498 }
2499 //And now swap the allocator
2500 dtl::swap_alloc(this->m_holder.alloc(), x.m_holder.alloc(), dtl::bool_<propagate_alloc>());
2501 }
2502
2503 void priv_reserve_no_capacity(size_type, version_0)
2504 { throw_bad_alloc(); }
2505
2506 dtl::insert_range_proxy<Allocator, boost::move_iterator<T*>, T*> priv_dummy_empty_proxy()
2507 {
2508 return dtl::insert_range_proxy<Allocator, boost::move_iterator<T*>, T*>
2509 (::boost::make_move_iterator((T *)0));
2510 }
2511
2512 void priv_reserve_no_capacity(size_type new_cap, version_1)
2513 {
2514 //There is not enough memory, allocate a new buffer
2515 //Pass the hint so that allocators can take advantage of this.
2516 pointer const p = this->m_holder.allocate(new_cap);
2517 //We will reuse insert code, so create a dummy input iterator
2518 this->priv_forward_range_insert_new_allocation
2519 ( boost::movelib::to_raw_pointer(p), new_cap, this->priv_raw_end(), 0, this->priv_dummy_empty_proxy());
2520 }
2521
2522 void priv_reserve_no_capacity(size_type new_cap, version_2)
2523 {
2524 //There is not enough memory, allocate a new
2525 //buffer or expand the old one.
2526 bool same_buffer_start;
2527 size_type real_cap = 0;
2528 pointer reuse(this->m_holder.start());
2529 pointer const ret(this->m_holder.allocation_command(allocate_new | expand_fwd | expand_bwd, new_cap, real_cap = new_cap, reuse));
2530
2531 //Check for forward expansion
2532 same_buffer_start = reuse && this->m_holder.start() == ret;
2533 if(same_buffer_start){
2534 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2535 ++this->num_expand_fwd;
2536 #endif
2537 this->m_holder.capacity(real_cap);
2538 }
2539 else{ //If there is no forward expansion, move objects, we will reuse insertion code
2540 T * const new_mem = boost::movelib::to_raw_pointer(ret);
2541 T * const ins_pos = this->priv_raw_end();
2542 if(reuse){ //Backwards (and possibly forward) expansion
2543 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2544 ++this->num_expand_bwd;
2545 #endif
2546 this->priv_forward_range_insert_expand_backwards
2547 ( new_mem , real_cap, ins_pos, 0, this->priv_dummy_empty_proxy());
2548 }
2549 else{ //New buffer
2550 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2551 ++this->num_alloc;
2552 #endif
2553 this->priv_forward_range_insert_new_allocation
2554 ( new_mem, real_cap, ins_pos, 0, this->priv_dummy_empty_proxy());
2555 }
2556 }
2557 }
2558
2559 void priv_destroy_last(const bool moved = false) BOOST_NOEXCEPT_OR_NOTHROW
2560 {
2561 (void)moved;
2562 const bool skip_destructor = value_traits::trivial_dctr || (value_traits::trivial_dctr_after_move && moved);
2563 if(!skip_destructor){
2564 value_type* const p = this->priv_raw_end() - 1;
2565 allocator_traits_type::destroy(this->get_stored_allocator(), p);
2566 }
2567 --this->m_holder.m_size;
2568 }
2569
2570 void priv_destroy_last_n(const size_type n) BOOST_NOEXCEPT_OR_NOTHROW
2571 {
2572 BOOST_ASSERT(n <= this->m_holder.m_size);
2573 if(!value_traits::trivial_dctr){
2574 T* const destroy_pos = this->priv_raw_begin() + (this->m_holder.m_size-n);
2575 boost::container::destroy_alloc_n(this->get_stored_allocator(), destroy_pos, n);
2576 }
2577 this->m_holder.m_size -= n;
2578 }
2579
2580 template<class InpIt>
2581 void priv_uninitialized_construct_at_end(InpIt first, InpIt last)
2582 {
2583 T* const old_end_pos = this->priv_raw_end();
2584 T* const new_end_pos = boost::container::uninitialized_copy_alloc(this->m_holder.alloc(), first, last, old_end_pos);
2585 this->m_holder.m_size += new_end_pos - old_end_pos;
2586 }
2587
2588 void priv_destroy_all() BOOST_NOEXCEPT_OR_NOTHROW
2589 {
2590 boost::container::destroy_alloc_n
2591 (this->get_stored_allocator(), this->priv_raw_begin(), this->m_holder.m_size);
2592 this->m_holder.m_size = 0;
2593 }
2594
2595 template<class U>
2596 iterator priv_insert(const const_iterator &p, BOOST_FWD_REF(U) x)
2597 {
2598 BOOST_ASSERT(this->priv_in_range_or_end(p));
2599 return this->priv_forward_range_insert
2600 ( vector_iterator_get_ptr(p), 1, dtl::get_insert_value_proxy<T*, Allocator>(::boost::forward<U>(x)));
2601 }
2602
2603 dtl::insert_copy_proxy<Allocator, T*> priv_single_insert_proxy(const T &x)
2604 { return dtl::insert_copy_proxy<Allocator, T*> (x); }
2605
2606 dtl::insert_move_proxy<Allocator, T*> priv_single_insert_proxy(BOOST_RV_REF(T) x)
2607 { return dtl::insert_move_proxy<Allocator, T*> (x); }
2608
2609 template <class U>
2610 void priv_push_back(BOOST_FWD_REF(U) u)
2611 {
2612 if (BOOST_LIKELY(this->room_enough())){
2613 //There is more memory, just construct a new object at the end
2614 allocator_traits_type::construct
2615 ( this->m_holder.alloc(), this->priv_raw_end(), ::boost::forward<U>(u) );
2616 ++this->m_holder.m_size;
2617 }
2618 else{
2619 this->priv_forward_range_insert_no_capacity
2620 ( this->back_ptr(), 1
2621 , this->priv_single_insert_proxy(::boost::forward<U>(u)), alloc_version());
2622 }
2623 }
2624
2625 BOOST_CONTAINER_FORCEINLINE dtl::insert_n_copies_proxy<Allocator, T*> priv_resize_proxy(const T &x)
2626 { return dtl::insert_n_copies_proxy<Allocator, T*>(x); }
2627
2628 BOOST_CONTAINER_FORCEINLINE dtl::insert_default_initialized_n_proxy<Allocator, T*> priv_resize_proxy(default_init_t)
2629 { return dtl::insert_default_initialized_n_proxy<Allocator, T*>(); }
2630
2631 BOOST_CONTAINER_FORCEINLINE dtl::insert_value_initialized_n_proxy<Allocator, T*> priv_resize_proxy(value_init_t)
2632 { return dtl::insert_value_initialized_n_proxy<Allocator, T*>(); }
2633
2634 template <class U>
2635 void priv_resize(size_type new_size, const U& u)
2636 {
2637 const size_type sz = this->size();
2638 if (new_size < sz){
2639 //Destroy last elements
2640 this->priv_destroy_last_n(sz - new_size);
2641 }
2642 else{
2643 const size_type n = new_size - this->size();
2644 this->priv_forward_range_insert_at_end(n, this->priv_resize_proxy(u), alloc_version());
2645 }
2646 }
2647
2648 BOOST_CONTAINER_FORCEINLINE void priv_shrink_to_fit(version_0) BOOST_NOEXCEPT_OR_NOTHROW
2649 {}
2650
2651 void priv_shrink_to_fit(version_1)
2652 {
2653 const size_type cp = this->m_holder.capacity();
2654 if(cp){
2655 const size_type sz = this->size();
2656 if(!sz){
2657 this->m_holder.deallocate(this->m_holder.m_start, cp);
2658 this->m_holder.m_start = pointer();
2659 this->m_holder.m_capacity = 0;
2660 }
2661 else if(sz < cp){
2662 //Allocate a new buffer.
2663 //Pass the hint so that allocators can take advantage of this.
2664 pointer const p = this->m_holder.allocate(sz);
2665
2666 //We will reuse insert code, so create a dummy input iterator
2667 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2668 ++this->num_alloc;
2669 #endif
2670 this->priv_forward_range_insert_new_allocation
2671 ( boost::movelib::to_raw_pointer(p), sz
2672 , this->priv_raw_begin(), 0, this->priv_dummy_empty_proxy());
2673 }
2674 }
2675 }
2676
2677 void priv_shrink_to_fit(version_2) BOOST_NOEXCEPT_OR_NOTHROW
2678 {
2679 const size_type cp = this->m_holder.capacity();
2680 if(cp){
2681 const size_type sz = this->size();
2682 if(!sz){
2683 this->m_holder.deallocate(this->m_holder.m_start, cp);
2684 this->m_holder.m_start = pointer();
2685 this->m_holder.m_capacity = 0;
2686 }
2687 else{
2688 size_type received_size = sz;
2689 pointer reuse(this->m_holder.start());
2690 if(this->m_holder.allocation_command
2691 (shrink_in_place | nothrow_allocation, cp, received_size, reuse)){
2692 this->m_holder.capacity(received_size);
2693 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2694 ++this->num_shrink;
2695 #endif
2696 }
2697 }
2698 }
2699 }
2700
2701 template <class InsertionProxy>
2702 iterator priv_forward_range_insert_no_capacity
2703 (const pointer &pos, const size_type, const InsertionProxy , version_0)
2704 {
2705 throw_bad_alloc();
2706 return iterator(pos);
2707 }
2708
2709 template <class InsertionProxy>
2710 iterator priv_forward_range_insert_no_capacity
2711 (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, version_1)
2712 {
2713 //Check if we have enough memory or try to expand current memory
2714 const size_type n_pos = pos - this->m_holder.start();
2715 T *const raw_pos = boost::movelib::to_raw_pointer(pos);
2716
2717 const size_type new_cap = this->m_holder.template next_capacity<growth_factor_type>(n);
2718 //Pass the hint so that allocators can take advantage of this.
2719 T * const new_buf = boost::movelib::to_raw_pointer(this->m_holder.allocate(new_cap));
2720 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2721 ++this->num_alloc;
2722 #endif
2723 this->priv_forward_range_insert_new_allocation
2724 ( new_buf, new_cap, raw_pos, n, insert_range_proxy);
2725 return iterator(this->m_holder.start() + n_pos);
2726 }
2727
2728 template <class InsertionProxy>
2729 iterator priv_forward_range_insert_no_capacity
2730 (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, version_2)
2731 {
2732 //Check if we have enough memory or try to expand current memory
2733 T *const raw_pos = boost::movelib::to_raw_pointer(pos);
2734 const size_type n_pos = raw_pos - this->priv_raw_begin();
2735
2736 //There is not enough memory, allocate a new
2737 //buffer or expand the old one.
2738 size_type real_cap = this->m_holder.template next_capacity<growth_factor_type>(n);
2739 pointer reuse(this->m_holder.start());
2740 pointer const ret (this->m_holder.allocation_command
2741 (allocate_new | expand_fwd | expand_bwd, this->m_holder.m_size + n, real_cap, reuse));
2742
2743 //Buffer reallocated
2744 if(reuse){
2745 //Forward expansion, delay insertion
2746 if(this->m_holder.start() == ret){
2747 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2748 ++this->num_expand_fwd;
2749 #endif
2750 this->m_holder.capacity(real_cap);
2751 //Expand forward
2752 this->priv_forward_range_insert_expand_forward(raw_pos, n, insert_range_proxy);
2753 }
2754 //Backwards (and possibly forward) expansion
2755 else{
2756 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2757 ++this->num_expand_bwd;
2758 #endif
2759 this->priv_forward_range_insert_expand_backwards
2760 (boost::movelib::to_raw_pointer(ret), real_cap, raw_pos, n, insert_range_proxy);
2761 }
2762 }
2763 //New buffer
2764 else{
2765 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2766 ++this->num_alloc;
2767 #endif
2768 this->priv_forward_range_insert_new_allocation
2769 ( boost::movelib::to_raw_pointer(ret), real_cap, raw_pos, n, insert_range_proxy);
2770 }
2771
2772 return iterator(this->m_holder.start() + n_pos);
2773 }
2774
2775 template <class InsertionProxy>
2776 iterator priv_forward_range_insert
2777 (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy)
2778 {
2779 BOOST_ASSERT(this->m_holder.capacity() >= this->m_holder.m_size);
2780 //Check if we have enough memory or try to expand current memory
2781 const size_type remaining = this->m_holder.capacity() - this->m_holder.m_size;
2782
2783 bool same_buffer_start = n <= remaining;
2784 if (!same_buffer_start){
2785 return priv_forward_range_insert_no_capacity(pos, n, insert_range_proxy, alloc_version());
2786 }
2787 else{
2788 //Expand forward
2789 T *const raw_pos = boost::movelib::to_raw_pointer(pos);
2790 const size_type n_pos = raw_pos - this->priv_raw_begin();
2791 this->priv_forward_range_insert_expand_forward(raw_pos, n, insert_range_proxy);
2792 return iterator(this->m_holder.start() + n_pos);
2793 }
2794 }
2795
2796 template <class InsertionProxy>
2797 iterator priv_forward_range_insert_at_end
2798 (const size_type n, const InsertionProxy insert_range_proxy, version_0)
2799 {
2800 //Check if we have enough memory or try to expand current memory
2801 const size_type remaining = this->m_holder.capacity() - this->m_holder.m_size;
2802
2803 if (n > remaining){
2804 //This will trigger an error
2805 throw_bad_alloc();
2806 }
2807 this->priv_forward_range_insert_at_end_expand_forward(n, insert_range_proxy);
2808 return this->end();
2809 }
2810
2811 template <class InsertionProxy, class AllocVersion>
2812 BOOST_CONTAINER_FORCEINLINE iterator priv_forward_range_insert_at_end
2813 (const size_type n, const InsertionProxy insert_range_proxy, AllocVersion)
2814 {
2815 return this->priv_forward_range_insert(this->back_ptr(), n, insert_range_proxy);
2816 }
2817
2818 //Takes the range pointed by [first_pos, last_pos) and shifts it to the right
2819 //by 'shift_count'. 'limit_pos' marks the end of constructed elements.
2820 //
2821 //Precondition: first_pos <= last_pos <= limit_pos
2822 //
2823 //The shift operation might cross limit_pos so elements to moved beyond limit_pos
2824 //are uninitialized_moved with an allocator. Other elements are moved.
2825 //
2826 //The shift operation might left uninitialized elements after limit_pos
2827 //and the number of uninitialized elements is returned by the function.
2828 //
2829 //Old situation:
2830 // first_pos last_pos old_limit
2831 // | | |
2832 // ____________V_______V__________________V_____________
2833 //| prefix | range | suffix |raw_mem ~
2834 //|____________|_______|__________________|_____________~
2835 //
2836 //New situation in Case A (hole_size == 0):
2837 // range is moved through move assignments
2838 //
2839 // first_pos last_pos limit_pos
2840 // | | |
2841 // ____________V_______V__________________V_____________
2842 //| prefix' | | | range |suffix'|raw_mem ~
2843 //|________________+______|___^___|_______|_____________~
2844 // | |
2845 // |_>_>_>_>_>^
2846 //
2847 //
2848 //New situation in Case B (hole_size >= 0):
2849 // range is moved through uninitialized moves
2850 //
2851 // first_pos last_pos limit_pos
2852 // | | |
2853 // ____________V_______V__________________V________________
2854 //| prefix' | | | [hole] | range |
2855 //|_______________________________________|________|___^___|
2856 // | |
2857 // |_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_^
2858 //
2859 //New situation in Case C (hole_size == 0):
2860 // range is moved through move assignments and uninitialized moves
2861 //
2862 // first_pos last_pos limit_pos
2863 // | | |
2864 // ____________V_______V__________________V___
2865 //| prefix' | | | range |
2866 //|___________________________________|___^___|
2867 // | |
2868 // |_>_>_>_>_>_>_>_>_>_>_>^
2869 size_type priv_insert_ordered_at_shift_range
2870 (size_type first_pos, size_type last_pos, size_type limit_pos, size_type shift_count)
2871 {
2872 BOOST_ASSERT(first_pos <= last_pos);
2873 BOOST_ASSERT(last_pos <= limit_pos);
2874 //
2875 T* const begin_ptr = this->priv_raw_begin();
2876 T* const first_ptr = begin_ptr + first_pos;
2877 T* const last_ptr = begin_ptr + last_pos;
2878
2879 size_type hole_size = 0;
2880 //Case A:
2881 if((last_pos + shift_count) <= limit_pos){
2882 //All move assigned
2883 boost::container::move_backward(first_ptr, last_ptr, last_ptr + shift_count);
2884 }
2885 //Case B:
2886 else if((first_pos + shift_count) >= limit_pos){
2887 //All uninitialized_moved
2888 ::boost::container::uninitialized_move_alloc
2889 (this->m_holder.alloc(), first_ptr, last_ptr, first_ptr + shift_count);
2890 hole_size = first_pos + shift_count - limit_pos;
2891 }
2892 //Case C:
2893 else{
2894 //Some uninitialized_moved
2895 T* const limit_ptr = begin_ptr + limit_pos;
2896 T* const boundary_ptr = limit_ptr - shift_count;
2897 ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), boundary_ptr, last_ptr, limit_ptr);
2898 //The rest is move assigned
2899 boost::container::move_backward(first_ptr, boundary_ptr, limit_ptr);
2900 }
2901 return hole_size;
2902 }
2903
2904 private:
2905 BOOST_CONTAINER_FORCEINLINE T *priv_raw_begin() const
2906 { return boost::movelib::to_raw_pointer(m_holder.start()); }
2907
2908 BOOST_CONTAINER_FORCEINLINE T* priv_raw_end() const
2909 { return this->priv_raw_begin() + this->m_holder.m_size; }
2910
2911 template <class InsertionProxy>
2912 void priv_forward_range_insert_at_end_expand_forward(const size_type n, InsertionProxy insert_range_proxy)
2913 {
2914 T* const old_finish = this->priv_raw_end();
2915 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n);
2916 this->m_holder.m_size += n;
2917 }
2918
2919 template <class InsertionProxy>
2920 void priv_forward_range_insert_expand_forward(T* const pos, const size_type n, InsertionProxy insert_range_proxy)
2921 {
2922 //n can't be 0, because there is nothing to do in that case
2923 if(BOOST_UNLIKELY(!n)) return;
2924 //There is enough memory
2925 T* const old_finish = this->priv_raw_end();
2926 const size_type elems_after = old_finish - pos;
2927
2928 if (!elems_after){
2929 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n);
2930 this->m_holder.m_size += n;
2931 }
2932 else if (elems_after >= n){
2933 //New elements can be just copied.
2934 //Move to uninitialized memory last objects
2935 ::boost::container::uninitialized_move_alloc
2936 (this->m_holder.alloc(), old_finish - n, old_finish, old_finish);
2937 this->m_holder.m_size += n;
2938 //Copy previous to last objects to the initialized end
2939 boost::container::move_backward(pos, old_finish - n, old_finish);
2940 //Insert new objects in the pos
2941 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, n);
2942 }
2943 else {
2944 //The new elements don't fit in the [pos, end()) range.
2945
2946 //Copy old [pos, end()) elements to the uninitialized memory (a gap is created)
2947 ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), pos, old_finish, pos + n);
2948 BOOST_TRY{
2949 //Copy first new elements in pos (gap is still there)
2950 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, elems_after);
2951 //Copy to the beginning of the unallocated zone the last new elements (the gap is closed).
2952 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n - elems_after);
2953 this->m_holder.m_size += n;
2954 }
2955 BOOST_CATCH(...){
2956 boost::container::destroy_alloc_n(this->get_stored_allocator(), pos + n, elems_after);
2957 BOOST_RETHROW
2958 }
2959 BOOST_CATCH_END
2960 }
2961 }
2962
2963 template <class InsertionProxy>
2964 void priv_forward_range_insert_new_allocation
2965 (T* const new_start, size_type new_cap, T* const pos, const size_type n, InsertionProxy insert_range_proxy)
2966 {
2967 //n can be zero, if we want to reallocate!
2968 T *new_finish = new_start;
2969 T *old_finish;
2970 //Anti-exception rollbacks
2971 typename value_traits::ArrayDeallocator new_buffer_deallocator(new_start, this->m_holder.alloc(), new_cap);
2972 typename value_traits::ArrayDestructor new_values_destroyer(new_start, this->m_holder.alloc(), 0u);
2973
2974 //Initialize with [begin(), pos) old buffer
2975 //the start of the new buffer
2976 T * const old_buffer = this->priv_raw_begin();
2977 if(old_buffer){
2978 new_finish = ::boost::container::uninitialized_move_alloc
2979 (this->m_holder.alloc(), this->priv_raw_begin(), pos, old_finish = new_finish);
2980 new_values_destroyer.increment_size(new_finish - old_finish);
2981 }
2982 //Initialize new objects, starting from previous point
2983 old_finish = new_finish;
2984 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n);
2985 new_finish += n;
2986 new_values_destroyer.increment_size(new_finish - old_finish);
2987 //Initialize from the rest of the old buffer,
2988 //starting from previous point
2989 if(old_buffer){
2990 new_finish = ::boost::container::uninitialized_move_alloc
2991 (this->m_holder.alloc(), pos, old_buffer + this->m_holder.m_size, new_finish);
2992 //Destroy and deallocate old elements
2993 //If there is allocated memory, destroy and deallocate
2994 if(!value_traits::trivial_dctr_after_move)
2995 boost::container::destroy_alloc_n(this->get_stored_allocator(), old_buffer, this->m_holder.m_size);
2996 this->m_holder.deallocate(this->m_holder.start(), this->m_holder.capacity());
2997 }
2998 this->m_holder.start(new_start);
2999 this->m_holder.m_size = size_type(new_finish - new_start);
3000 this->m_holder.capacity(new_cap);
3001 //All construction successful, disable rollbacks
3002 new_values_destroyer.release();
3003 new_buffer_deallocator.release();
3004 }
3005
3006 template <class InsertionProxy>
3007 void priv_forward_range_insert_expand_backwards
3008 (T* const new_start, const size_type new_capacity,
3009 T* const pos, const size_type n, InsertionProxy insert_range_proxy)
3010 {
3011 //n can be zero to just expand capacity
3012 //Backup old data
3013 T* const old_start = this->priv_raw_begin();
3014 const size_type old_size = this->m_holder.m_size;
3015 T* const old_finish = old_start + old_size;
3016
3017 //We can have 8 possibilities:
3018 const size_type elemsbefore = static_cast<size_type>(pos - old_start);
3019 const size_type s_before = static_cast<size_type>(old_start - new_start);
3020 const size_type before_plus_new = elemsbefore + n;
3021
3022 //Update the vector buffer information to a safe state
3023 this->m_holder.start(new_start);
3024 this->m_holder.capacity(new_capacity);
3025 this->m_holder.m_size = 0;
3026
3027 //If anything goes wrong, this object will destroy
3028 //all the old objects to fulfill previous vector state
3029 typename value_traits::ArrayDestructor old_values_destroyer(old_start, this->m_holder.alloc(), old_size);
3030 //Check if s_before is big enough to hold the beginning of old data + new data
3031 if(s_before >= before_plus_new){
3032 //Copy first old values before pos, after that the new objects
3033 T *const new_elem_pos =
3034 ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), old_start, pos, new_start);
3035 this->m_holder.m_size = elemsbefore;
3036 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), new_elem_pos, n);
3037 this->m_holder.m_size = before_plus_new;
3038 const size_type new_size = old_size + n;
3039 //Check if s_before is so big that even copying the old data + new data
3040 //there is a gap between the new data and the old data
3041 if(s_before >= new_size){
3042 //Old situation:
3043 // _________________________________________________________
3044 //| raw_mem | old_begin | old_end |
3045 //| __________________________________|___________|_________|
3046 //
3047 //New situation:
3048 // _________________________________________________________
3049 //| old_begin | new | old_end | raw_mem |
3050 //|___________|__________|_________|________________________|
3051 //
3052 //Now initialize the rest of memory with the last old values
3053 if(before_plus_new != new_size){ //Special case to avoid operations in back insertion
3054 ::boost::container::uninitialized_move_alloc
3055 (this->m_holder.alloc(), pos, old_finish, new_start + before_plus_new);
3056 //All new elements correctly constructed, avoid new element destruction
3057 this->m_holder.m_size = new_size;
3058 }
3059 //Old values destroyed automatically with "old_values_destroyer"
3060 //when "old_values_destroyer" goes out of scope unless the have trivial
3061 //destructor after move.
3062 if(value_traits::trivial_dctr_after_move)
3063 old_values_destroyer.release();
3064 }
3065 //s_before is so big that divides old_end
3066 else{
3067 //Old situation:
3068 // __________________________________________________
3069 //| raw_mem | old_begin | old_end |
3070 //| ___________________________|___________|_________|
3071 //
3072 //New situation:
3073 // __________________________________________________
3074 //| old_begin | new | old_end | raw_mem |
3075 //|___________|__________|_________|_________________|
3076 //
3077 //Now initialize the rest of memory with the last old values
3078 //All new elements correctly constructed, avoid new element destruction
3079 const size_type raw_gap = s_before - before_plus_new;
3080 if(!value_traits::trivial_dctr){
3081 //Now initialize the rest of s_before memory with the
3082 //first of elements after new values
3083 ::boost::container::uninitialized_move_alloc_n
3084 (this->m_holder.alloc(), pos, raw_gap, new_start + before_plus_new);
3085 //Now we have a contiguous buffer so program trailing element destruction
3086 //and update size to the final size.
3087 old_values_destroyer.shrink_forward(new_size-s_before);
3088 this->m_holder.m_size = new_size;
3089 //Now move remaining last objects in the old buffer begin
3090 T * const remaining_pos = pos + raw_gap;
3091 if(remaining_pos != old_start){ //Make sure data has to be moved
3092 ::boost::container::move(remaining_pos, old_finish, old_start);
3093 }
3094 //Once moved, avoid calling the destructors if trivial after move
3095 if(value_traits::trivial_dctr_after_move){
3096 old_values_destroyer.release();
3097 }
3098 }
3099 else{ //If trivial destructor, we can uninitialized copy + copy in a single uninitialized copy
3100 ::boost::container::uninitialized_move_alloc_n
3101 (this->m_holder.alloc(), pos, static_cast<size_type>(old_finish - pos), new_start + before_plus_new);
3102 this->m_holder.m_size = new_size;
3103 old_values_destroyer.release();
3104 }
3105 }
3106 }
3107 else{
3108 //Check if we have to do the insertion in two phases
3109 //since maybe s_before is not big enough and
3110 //the buffer was expanded both sides
3111 //
3112 //Old situation:
3113 // _________________________________________________
3114 //| raw_mem | old_begin + old_end | raw_mem |
3115 //|_________|_____________________|_________________|
3116 //
3117 //New situation with do_after:
3118 // _________________________________________________
3119 //| old_begin + new + old_end | raw_mem |
3120 //|___________________________________|_____________|
3121 //
3122 //New without do_after:
3123 // _________________________________________________
3124 //| old_begin + new + old_end | raw_mem |
3125 //|____________________________|____________________|
3126 //
3127 const bool do_after = n > s_before;
3128
3129 //Now we can have two situations: the raw_mem of the
3130 //beginning divides the old_begin, or the new elements:
3131 if (s_before <= elemsbefore) {
3132 //The raw memory divides the old_begin group:
3133 //
3134 //If we need two phase construction (do_after)
3135 //new group is divided in new = new_beg + new_end groups
3136 //In this phase only new_beg will be inserted
3137 //
3138 //Old situation:
3139 // _________________________________________________
3140 //| raw_mem | old_begin | old_end | raw_mem |
3141 //|_________|___________|_________|_________________|
3142 //
3143 //New situation with do_after(1):
3144 //This is not definitive situation, the second phase
3145 //will include
3146 // _________________________________________________
3147 //| old_begin | new_beg | old_end | raw_mem |
3148 //|___________|_________|_________|_________________|
3149 //
3150 //New situation without do_after:
3151 // _________________________________________________
3152 //| old_begin | new | old_end | raw_mem |
3153 //|___________|_____|_________|_____________________|
3154 //
3155 //Copy the first part of old_begin to raw_mem
3156 ::boost::container::uninitialized_move_alloc_n
3157 (this->m_holder.alloc(), old_start, s_before, new_start);
3158 //The buffer is all constructed until old_end,
3159 //so program trailing destruction and assign final size
3160 //if !do_after, s_before+n otherwise.
3161 size_type new_1st_range;
3162 if(do_after){
3163 new_1st_range = s_before;
3164 //release destroyer and update size
3165 old_values_destroyer.release();
3166 }
3167 else{
3168 new_1st_range = n;
3169 if(value_traits::trivial_dctr_after_move)
3170 old_values_destroyer.release();
3171 else{
3172 old_values_destroyer.shrink_forward(old_size - (s_before - n));
3173 }
3174 }
3175 this->m_holder.m_size = old_size + new_1st_range;
3176 //Now copy the second part of old_begin overwriting itself
3177 T *const next = ::boost::container::move(old_start + s_before, pos, old_start);
3178 //Now copy the new_beg elements
3179 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), next, new_1st_range);
3180
3181 //If there is no after work and the last old part needs to be moved to front, do it
3182 if(!do_after && (n != s_before)){
3183 //Now displace old_end elements
3184 ::boost::container::move(pos, old_finish, next + new_1st_range);
3185 }
3186 }
3187 else {
3188 //If we have to expand both sides,
3189 //we will play if the first new values so
3190 //calculate the upper bound of new values
3191
3192 //The raw memory divides the new elements
3193 //
3194 //If we need two phase construction (do_after)
3195 //new group is divided in new = new_beg + new_end groups
3196 //In this phase only new_beg will be inserted
3197 //
3198 //Old situation:
3199 // _______________________________________________________
3200 //| raw_mem | old_begin | old_end | raw_mem |
3201 //|_______________|___________|_________|_________________|
3202 //
3203 //New situation with do_after():
3204 // ____________________________________________________
3205 //| old_begin | new_beg | old_end | raw_mem |
3206 //|___________|_______________|_________|______________|
3207 //
3208 //New situation without do_after:
3209 // ______________________________________________________
3210 //| old_begin | new | old_end | raw_mem |
3211 //|___________|_____|_________|__________________________|
3212 //
3213 //First copy whole old_begin and part of new to raw_mem
3214 T * const new_pos = ::boost::container::uninitialized_move_alloc
3215 (this->m_holder.alloc(), old_start, pos, new_start);
3216 this->m_holder.m_size = elemsbefore;
3217 const size_type mid_n = s_before - elemsbefore;
3218 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), new_pos, mid_n);
3219 //The buffer is all constructed until old_end,
3220 //release destroyer
3221 this->m_holder.m_size = old_size + s_before;
3222 old_values_destroyer.release();
3223
3224 if(do_after){
3225 //Copy new_beg part
3226 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), old_start, elemsbefore);
3227 }
3228 else{
3229 //Copy all new elements
3230 const size_type rest_new = n - mid_n;
3231 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), old_start, rest_new);
3232 T* const move_start = old_start + rest_new;
3233 //Displace old_end, but make sure data has to be moved
3234 T* const move_end = move_start != pos ? ::boost::container::move(pos, old_finish, move_start)
3235 : old_finish;
3236 //Destroy remaining moved elements from old_end except if they
3237 //have trivial destructor after being moved
3238 size_type n_destroy = s_before - n;
3239 if(!value_traits::trivial_dctr_after_move)
3240 boost::container::destroy_alloc_n(this->get_stored_allocator(), move_end, n_destroy);
3241 this->m_holder.m_size -= n_destroy;
3242 }
3243 }
3244
3245 //This is only executed if two phase construction is needed
3246 if(do_after){
3247 //The raw memory divides the new elements
3248 //
3249 //Old situation:
3250 // ______________________________________________________
3251 //| raw_mem | old_begin | old_end | raw_mem |
3252 //|______________|___________|____________|______________|
3253 //
3254 //New situation with do_after(1):
3255 // _______________________________________________________
3256 //| old_begin + new_beg | new_end |old_end | raw_mem |
3257 //|__________________________|_________|________|_________|
3258 //
3259 //New situation with do_after(2):
3260 // ______________________________________________________
3261 //| old_begin + new | old_end |raw |
3262 //|_______________________________________|_________|____|
3263 //
3264 const size_type n_after = n - s_before;
3265 const size_type elemsafter = old_size - elemsbefore;
3266
3267 //We can have two situations:
3268 if (elemsafter >= n_after){
3269 //The raw_mem from end will divide displaced old_end
3270 //
3271 //Old situation:
3272 // ______________________________________________________
3273 //| raw_mem | old_begin | old_end | raw_mem |
3274 //|______________|___________|____________|______________|
3275 //
3276 //New situation with do_after(1):
3277 // _______________________________________________________
3278 //| old_begin + new_beg | new_end |old_end | raw_mem |
3279 //|__________________________|_________|________|_________|
3280 //
3281 //First copy the part of old_end raw_mem
3282 T* finish_n = old_finish - n_after;
3283 ::boost::container::uninitialized_move_alloc
3284 (this->m_holder.alloc(), finish_n, old_finish, old_finish);
3285 this->m_holder.m_size += n_after;
3286 //Displace the rest of old_end to the new position
3287 boost::container::move_backward(pos, finish_n, old_finish);
3288 //Now overwrite with new_end
3289 //The new_end part is [first + (n - n_after), last)
3290 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, n_after);
3291 }
3292 else {
3293 //The raw_mem from end will divide new_end part
3294 //
3295 //Old situation:
3296 // _____________________________________________________________
3297 //| raw_mem | old_begin | old_end | raw_mem |
3298 //|______________|___________|____________|_____________________|
3299 //
3300 //New situation with do_after(2):
3301 // _____________________________________________________________
3302 //| old_begin + new_beg | new_end |old_end | raw_mem |
3303 //|__________________________|_______________|________|_________|
3304 //
3305
3306 const size_type mid_last_dist = n_after - elemsafter;
3307 //First initialize data in raw memory
3308
3309 //Copy to the old_end part to the uninitialized zone leaving a gap.
3310 ::boost::container::uninitialized_move_alloc
3311 (this->m_holder.alloc(), pos, old_finish, old_finish + mid_last_dist);
3312
3313 typename value_traits::ArrayDestructor old_end_destroyer
3314 (old_finish + mid_last_dist, this->m_holder.alloc(), old_finish - pos);
3315
3316 //Copy the first part to the already constructed old_end zone
3317 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, elemsafter);
3318 //Copy the rest to the uninitialized zone filling the gap
3319 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, mid_last_dist);
3320 this->m_holder.m_size += n_after;
3321 old_end_destroyer.release();
3322 }
3323 }
3324 }
3325 }
3326
3327 void priv_throw_if_out_of_range(size_type n) const
3328 {
3329 //If n is out of range, throw an out_of_range exception
3330 if (n >= this->size()){
3331 throw_out_of_range("vector::at out of range");
3332 }
3333 }
3334
3335 BOOST_CONTAINER_FORCEINLINE bool priv_in_range(const_iterator pos) const
3336 {
3337 return (this->begin() <= pos) && (pos < this->end());
3338 }
3339
3340 BOOST_CONTAINER_FORCEINLINE bool priv_in_range_or_end(const_iterator pos) const
3341 {
3342 return (this->begin() <= pos) && (pos <= this->end());
3343 }
3344
3345 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
3346 public:
3347 unsigned int num_expand_fwd;
3348 unsigned int num_expand_bwd;
3349 unsigned int num_shrink;
3350 unsigned int num_alloc;
3351 void reset_alloc_stats()
3352 { num_expand_fwd = num_expand_bwd = num_alloc = 0, num_shrink = 0; }
3353 #endif
3354 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3355};
3356
3357#if __cplusplus >= 201703L
3358
3359template <typename InputIterator>
3360vector(InputIterator, InputIterator) ->
3361 vector<typename iterator_traits<InputIterator>::value_type>;
3362
3363template <typename InputIterator, typename Allocator>
3364vector(InputIterator, InputIterator, Allocator const&) ->
3365 vector<typename iterator_traits<InputIterator>::value_type, Allocator>;
3366
3367#endif
3368
3369
3370}} //namespace boost::container
3371
3372#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3373
3374namespace boost {
3375
3376//!has_trivial_destructor_after_move<> == true_type
3377//!specialization for optimizations
3378template <class T, class Allocator>
3379struct has_trivial_destructor_after_move<boost::container::vector<T, Allocator> >
3380{
3381 typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
3382 static const bool value = ::boost::has_trivial_destructor_after_move<Allocator>::value &&
3383 ::boost::has_trivial_destructor_after_move<pointer>::value;
3384};
3385
3386}
3387
3388#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3389
3390#include <boost/container/detail/config_end.hpp>
3391
3392#endif // #ifndef BOOST_CONTAINER_CONTAINER_VECTOR_HPP