1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/container for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_CONTAINER_CONTAINER_VECTOR_HPP
12 #define BOOST_CONTAINER_CONTAINER_VECTOR_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/container/detail/config_begin.hpp>
23 #include <boost/container/detail/workaround.hpp>
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>
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>
48 #include <boost/intrusive/pointer_traits.hpp>
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>
55 #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
56 #include <boost/move/detail/fwd_macros.hpp>
58 #include <boost/move/detail/move_helpers.hpp>
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>
65 #include <boost/core/no_exceptions_support.hpp>
66 #include <boost/assert.hpp>
67 #include <boost/cstdint.hpp>
70 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
71 #include <initializer_list> //for std::initializer_list
77 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
80 template <class Pointer, bool IsConst>
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
89 , typename boost::intrusive::pointer_traits<Pointer>::template
90 rebind_pointer<const value_type>::type
93 typedef typename boost::intrusive::pointer_traits<pointer> ptr_traits;
94 typedef typename ptr_traits::reference reference;
96 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
101 BOOST_CONTAINER_FORCEINLINE const Pointer &get_ptr() const BOOST_NOEXCEPT_OR_NOTHROW
104 BOOST_CONTAINER_FORCEINLINE Pointer &get_ptr() BOOST_NOEXCEPT_OR_NOTHROW
107 BOOST_CONTAINER_FORCEINLINE explicit vec_iterator(Pointer ptr) BOOST_NOEXCEPT_OR_NOTHROW
110 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
115 BOOST_CONTAINER_FORCEINLINE vec_iterator() BOOST_NOEXCEPT_OR_NOTHROW
116 : m_ptr() //Value initialization to achieve "null iterators" (N3644)
119 BOOST_CONTAINER_FORCEINLINE vec_iterator(vec_iterator<Pointer, false> const& other) BOOST_NOEXCEPT_OR_NOTHROW
120 : m_ptr(other.get_ptr())
123 //Pointer like operators
124 BOOST_CONTAINER_FORCEINLINE reference operator*() const BOOST_NOEXCEPT_OR_NOTHROW
127 BOOST_CONTAINER_FORCEINLINE pointer operator->() const BOOST_NOEXCEPT_OR_NOTHROW
128 { return ::boost::intrusive::pointer_traits<pointer>::pointer_to(this->operator*()); }
130 BOOST_CONTAINER_FORCEINLINE reference operator[](difference_type off) const BOOST_NOEXCEPT_OR_NOTHROW
131 { return m_ptr[off]; }
133 //Increment / Decrement
134 BOOST_CONTAINER_FORCEINLINE vec_iterator& operator++() BOOST_NOEXCEPT_OR_NOTHROW
135 { ++m_ptr; return *this; }
137 BOOST_CONTAINER_FORCEINLINE vec_iterator operator++(int) BOOST_NOEXCEPT_OR_NOTHROW
138 { return vec_iterator(m_ptr++); }
140 BOOST_CONTAINER_FORCEINLINE vec_iterator& operator--() BOOST_NOEXCEPT_OR_NOTHROW
141 { --m_ptr; return *this; }
143 BOOST_CONTAINER_FORCEINLINE vec_iterator operator--(int) BOOST_NOEXCEPT_OR_NOTHROW
144 { return vec_iterator(m_ptr--); }
147 BOOST_CONTAINER_FORCEINLINE vec_iterator& operator+=(difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
148 { m_ptr += off; return *this; }
150 BOOST_CONTAINER_FORCEINLINE vec_iterator& operator-=(difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
151 { m_ptr -= off; return *this; }
153 BOOST_CONTAINER_FORCEINLINE friend vec_iterator operator+(const vec_iterator &x, difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
154 { return vec_iterator(x.m_ptr+off); }
156 BOOST_CONTAINER_FORCEINLINE friend vec_iterator operator+(difference_type off, vec_iterator right) BOOST_NOEXCEPT_OR_NOTHROW
157 { right.m_ptr += off; return right; }
159 BOOST_CONTAINER_FORCEINLINE friend vec_iterator operator-(vec_iterator left, difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
160 { left.m_ptr -= off; return left; }
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; }
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; }
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; }
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; }
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; }
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; }
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; }
185 template<class BiDirPosConstIt, class BiDirValueIt>
186 struct vector_insert_ordered_cursor
188 typedef typename iterator_traits<BiDirPosConstIt>::value_type size_type;
189 typedef typename iterator_traits<BiDirValueIt>::reference reference;
191 BOOST_CONTAINER_FORCEINLINE vector_insert_ordered_cursor(BiDirPosConstIt posit, BiDirValueIt valueit)
192 : last_position_it(posit), last_value_it(valueit)
199 while(this->get_pos() == size_type(-1)){
205 BOOST_CONTAINER_FORCEINLINE size_type get_pos() const
206 { return *last_position_it; }
208 BOOST_CONTAINER_FORCEINLINE reference get_val()
209 { return *last_value_it; }
211 BiDirPosConstIt last_position_it;
212 BiDirValueIt last_value_it;
215 struct initial_capacity_t{};
217 template<class Pointer, bool IsConst>
218 BOOST_CONTAINER_FORCEINLINE const Pointer &vector_iterator_get_ptr(const vec_iterator<Pointer, IsConst> &it) BOOST_NOEXCEPT_OR_NOTHROW
219 { return it.get_ptr(); }
221 template<class Pointer, bool IsConst>
222 BOOST_CONTAINER_FORCEINLINE Pointer &get_ptr(vec_iterator<Pointer, IsConst> &it) BOOST_NOEXCEPT_OR_NOTHROW
223 { return it.get_ptr(); }
225 struct vector_uninitialized_size_t {};
226 static const vector_uninitialized_size_t vector_uninitialized_size = vector_uninitialized_size_t();
229 struct vector_value_traits_base
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;
240 template <class Allocator>
241 struct vector_value_traits
242 : public vector_value_traits_base<typename Allocator::value_type>
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;
256 //!This struct deallocates and allocated memory
257 template < class Allocator
258 , class StoredSizeType
259 , class AllocatorVersion = typename dtl::version<Allocator>::type
261 struct vector_alloc_holder
265 BOOST_MOVABLE_BUT_NOT_COPYABLE(vector_alloc_holder)
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;
275 static bool is_propagable_from(const allocator_type &from_alloc, pointer p, const allocator_type &to_alloc, bool const propagate_allocator)
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));
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)
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));
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()
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()
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))
308 //Size is initialized here so vector should only call uninitialized_xxx after this
309 , m_size(static_cast<stored_size_type>(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);
320 //Constructor, does not throw
321 vector_alloc_holder(vector_uninitialized_size_t, size_type initial_size)
324 //Size is initialized here so vector should only call uninitialized_xxx after this
325 , m_size(static_cast<stored_size_type>(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);
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)
342 holder.m_start = pointer();
343 holder.m_size = holder.m_capacity = 0;
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))
349 , m_size(holder.m_size)
350 , m_capacity(static_cast<stored_size_type>(capacity))
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);
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;
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;
375 vector_alloc_holder(initial_capacity_t, pointer p, size_type n)
376 BOOST_NOEXCEPT_IF(dtl::is_nothrow_default_constructible<Allocator>::value)
380 //n is guaranteed to fit into stored_size_type
381 , m_capacity(static_cast<stored_size_type>(n))
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))
392 BOOST_CONTAINER_FORCEINLINE ~vector_alloc_holder() BOOST_NOEXCEPT_OR_NOTHROW
394 if(this->m_capacity){
395 this->deallocate(this->m_start, this->m_capacity);
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)
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);
406 BOOST_CONTAINER_FORCEINLINE pointer allocate(size_type n)
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);
411 boost::container::throw_length_error("get_next_capacity, allocator's max size reached");
413 return allocator_traits_type::allocate(this->alloc(), n);
416 BOOST_CONTAINER_FORCEINLINE void deallocate(const pointer &p, size_type n)
418 allocator_traits_type::deallocate(this->alloc(), p, n);
421 bool try_expand_fwd(size_type at_least)
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
430 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
431 ++this->num_expand_fwd;
433 this->capacity(real_cap);
438 template<class GrowthFactorType>
439 size_type next_capacity(size_type additional_objects) const
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);
447 if ( remaining_cap < min_additional_cap )
448 boost::container::throw_length_error("get_next_capacity, allocator's max size reached");
450 return GrowthFactorType()( size_type(this->m_capacity), min_additional_cap, max);
454 stored_size_type m_size;
455 stored_size_type m_capacity;
457 void swap_resources(vector_alloc_holder &x) BOOST_NOEXCEPT_OR_NOTHROW
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);
464 void steal_resources(vector_alloc_holder &x) BOOST_NOEXCEPT_OR_NOTHROW
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;
473 BOOST_CONTAINER_FORCEINLINE Allocator &alloc() BOOST_NOEXCEPT_OR_NOTHROW
476 BOOST_CONTAINER_FORCEINLINE const Allocator &alloc() const BOOST_NOEXCEPT_OR_NOTHROW
479 BOOST_CONTAINER_FORCEINLINE const pointer &start() const BOOST_NOEXCEPT_OR_NOTHROW
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
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; }
489 void priv_first_allocation(size_type cap)
492 pointer reuse = pointer();
493 m_start = this->allocation_command(allocate_new, cap, cap, reuse);
495 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
501 BOOST_CONTAINER_FORCEINLINE static void clamp_by_stored_size_type(size_type &, size_type)
504 template<class SomeStoredSizeType>
505 BOOST_CONTAINER_FORCEINLINE static void clamp_by_stored_size_type(size_type &s, SomeStoredSizeType)
507 if (s >= SomeStoredSizeType(-1) )
508 s = SomeStoredSizeType(-1);
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,
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");
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);
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,
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");
538 (clamp_by_stored_size_type)(prefer_in_recvd_out_size, stored_size_type());
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());
547 //!This struct deallocates and allocated memory
548 template <class Allocator, class StoredSizeType>
549 struct vector_alloc_holder<Allocator, StoredSizeType, version_0>
553 BOOST_MOVABLE_BUT_NOT_COPYABLE(vector_alloc_holder)
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;
562 template <class OtherAllocator, class OtherStoredSizeType, class OtherAllocatorVersion>
563 friend struct vector_alloc_holder;
565 //Constructor, does not throw
566 vector_alloc_holder()
567 BOOST_NOEXCEPT_IF(dtl::is_nothrow_default_constructible<Allocator>::value)
568 : Allocator(), m_size()
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()
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...
583 //... and capacity here, so vector, must call uninitialized_xxx in the derived constructor
584 this->priv_first_allocation(initial_size);
587 //Constructor, does not throw
588 vector_alloc_holder(vector_uninitialized_size_t, size_type initial_size)
590 , m_size(initial_size) //Size is initialized here...
592 //... and capacity here, so vector, must call uninitialized_xxx in the derived constructor
593 this->priv_first_allocation(initial_size);
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
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()));
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)
607 , m_size(holder.m_size) //Initialize it to m_size as first_allocation can only succeed or abort
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()));
616 BOOST_CONTAINER_FORCEINLINE void priv_first_allocation(size_type cap)
618 if(cap > Allocator::internal_capacity){
623 BOOST_CONTAINER_FORCEINLINE void deep_swap(vector_alloc_holder &x)
625 this->priv_deep_swap(x);
628 template<class OtherAllocator, class OtherStoredSizeType, class OtherAllocatorVersion>
629 void deep_swap(vector_alloc_holder<OtherAllocator, OtherStoredSizeType, OtherAllocatorVersion> &x)
631 if(this->m_size > OtherAllocator::internal_capacity || x.m_size > Allocator::internal_capacity){
634 this->priv_deep_swap(x);
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
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
648 BOOST_CONTAINER_FORCEINLINE Allocator &alloc() BOOST_NOEXCEPT_OR_NOTHROW
651 BOOST_CONTAINER_FORCEINLINE const Allocator &alloc() const BOOST_NOEXCEPT_OR_NOTHROW
654 BOOST_CONTAINER_FORCEINLINE bool try_expand_fwd(size_type at_least)
655 { return !at_least; }
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;
663 template<class OtherAllocator, class OtherStoredSizeType, class OtherAllocatorVersion>
664 void priv_deep_swap(vector_alloc_holder<OtherAllocator, OtherStoredSizeType, OtherAllocatorVersion> &x)
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());
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);
674 boost::container::deep_swap_alloc_n<MaxTmpStorage>(this->alloc(), first_x, x.m_size, first_this, this->m_size);
676 boost::adl_move_swap(this->m_size, x.m_size);
680 struct growth_factor_60;
682 template<class T, class Default>
683 struct default_if_void
688 template<class Default>
689 struct default_if_void<void, Default>
691 typedef Default type;
694 template<class Options, class AllocatorSizeType>
695 struct get_vector_opt
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
702 template<class AllocatorSizeType>
703 struct get_vector_opt<void, AllocatorSizeType>
705 typedef vector_opt<growth_factor_60, AllocatorSizeType> type;
709 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
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.
716 //! \tparam T The type of object that is stored in the vector
717 //! \tparam Allocator The allocator used for all internal memory management
718 //! \tparam Options A type produced from \c boost::container::vector_options.
719 template <class T, class Allocator BOOST_CONTAINER_DOCONLY(= new_allocator<T>), class Options BOOST_CONTAINER_DOCONLY(= void) >
722 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
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;
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) );
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>
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;
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); }
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); }
753 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
755 //////////////////////////////////////////////
759 //////////////////////////////////////////////
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;
775 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
777 BOOST_COPYABLE_AND_MOVABLE(vector)
778 typedef vector_value_traits<Allocator> value_traits;
779 typedef constant_iterator<T, difference_type> cvalue_iterator;
783 BOOST_CONTAINER_FORCEINLINE void steal_resources(vector &x)
784 { return this->m_holder.steal_resources(x.m_holder); }
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))
791 BOOST_CONTAINER_FORCEINLINE vector(initial_capacity_t, pointer initial_memory, size_type capacity)
792 : m_holder(initial_capacity_t(), initial_memory, capacity)
795 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
798 //////////////////////////////////////////////
800 // construct/copy/destroy
802 //////////////////////////////////////////////
804 //! <b>Effects</b>: Constructs a vector taking the allocator as parameter.
806 //! <b>Throws</b>: Nothing.
808 //! <b>Complexity</b>: Constant.
809 vector() BOOST_NOEXCEPT_IF(dtl::is_nothrow_default_constructible<Allocator>::value)
813 //! <b>Effects</b>: Constructs a vector taking the allocator as parameter.
815 //! <b>Throws</b>: Nothing
817 //! <b>Complexity</b>: Constant.
818 explicit vector(const allocator_type& a) BOOST_NOEXCEPT_OR_NOTHROW
822 //! <b>Effects</b>: Constructs a vector and inserts n value initialized values.
824 //! <b>Throws</b>: If allocator_type's allocation
825 //! throws or T's value initialization throws.
827 //! <b>Complexity</b>: Linear to n.
828 explicit vector(size_type n)
829 : m_holder(vector_uninitialized_size, n)
831 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
832 this->num_alloc += n != 0;
834 boost::container::uninitialized_value_init_alloc_n
835 (this->m_holder.alloc(), n, this->priv_raw_begin());
838 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
839 //! and inserts n value initialized values.
841 //! <b>Throws</b>: If allocator_type's allocation
842 //! throws or T's value initialization throws.
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)
848 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
849 this->num_alloc += n != 0;
851 boost::container::uninitialized_value_init_alloc_n
852 (this->m_holder.alloc(), n, this->priv_raw_begin());
855 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
856 //! and inserts n default initialized values.
858 //! <b>Throws</b>: If allocator_type's allocation
859 //! throws or T's default initialization throws.
861 //! <b>Complexity</b>: Linear to n.
863 //! <b>Note</b>: Non-standard extension
864 vector(size_type n, default_init_t)
865 : m_holder(vector_uninitialized_size, n)
867 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
868 this->num_alloc += n != 0;
870 boost::container::uninitialized_default_init_alloc_n
871 (this->m_holder.alloc(), n, this->priv_raw_begin());
874 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
875 //! and inserts n default initialized values.
877 //! <b>Throws</b>: If allocator_type's allocation
878 //! throws or T's default initialization throws.
880 //! <b>Complexity</b>: Linear to n.
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)
886 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
887 this->num_alloc += n != 0;
889 boost::container::uninitialized_default_init_alloc_n
890 (this->m_holder.alloc(), n, this->priv_raw_begin());
893 //! <b>Effects</b>: Constructs a vector
894 //! and inserts n copies of value.
896 //! <b>Throws</b>: If allocator_type's allocation
897 //! throws or T's copy constructor throws.
899 //! <b>Complexity</b>: Linear to n.
900 vector(size_type n, const T& value)
901 : m_holder(vector_uninitialized_size, n)
903 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
904 this->num_alloc += n != 0;
906 boost::container::uninitialized_fill_alloc_n
907 (this->m_holder.alloc(), value, n, this->priv_raw_begin());
910 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
911 //! and inserts n copies of value.
913 //! <b>Throws</b>: If allocation
914 //! throws or T's copy constructor throws.
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)
920 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
921 this->num_alloc += n != 0;
923 boost::container::uninitialized_fill_alloc_n
924 (this->m_holder.alloc(), value, n, this->priv_raw_begin());
927 //! <b>Effects</b>: Constructs a vector
928 //! and inserts a copy of the range [first, last) in the vector.
930 //! <b>Throws</b>: If allocator_type's allocation
931 //! throws or T's constructor taking a dereferenced InIt throws.
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)
941 { this->assign(first, last); }
943 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
944 //! and inserts a copy of the range [first, last) in the vector.
946 //! <b>Throws</b>: If allocator_type's allocation
947 //! throws or T's constructor taking a dereferenced InIt throws.
949 //! <b>Complexity</b>: Linear to the range [first, last).
950 template <class InIt>
951 vector(InIt first, InIt last, const allocator_type& a
952 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_c
953 < dtl::is_convertible<InIt BOOST_MOVE_I size_type>::value
954 BOOST_MOVE_I dtl::nat >::type * = 0)
957 { this->assign(first, last); }
959 //! <b>Effects</b>: Copy constructs a vector.
961 //! <b>Postcondition</b>: x == *this.
963 //! <b>Throws</b>: If allocator_type's allocation
964 //! throws or T's copy constructor throws.
966 //! <b>Complexity</b>: Linear to the elements x contains.
967 vector(const vector &x)
968 : m_holder( vector_uninitialized_size
969 , allocator_traits_type::select_on_container_copy_construction(x.m_holder.alloc())
972 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
973 this->num_alloc += x.size() != 0;
975 ::boost::container::uninitialized_copy_alloc_n
976 ( this->m_holder.alloc(), x.priv_raw_begin()
977 , x.size(), this->priv_raw_begin());
980 //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
982 //! <b>Throws</b>: Nothing
984 //! <b>Complexity</b>: Constant.
985 vector(BOOST_RV_REF(vector) x) BOOST_NOEXCEPT_OR_NOTHROW
986 : m_holder(boost::move(x.m_holder))
987 { BOOST_STATIC_ASSERT((!allocator_traits_type::is_partially_propagable::value)); }
989 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
990 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
991 //! and inserts a copy of the range [il.begin(), il.last()) in the vector
993 //! <b>Throws</b>: If T's constructor taking a dereferenced initializer_list iterator throws.
995 //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
996 vector(std::initializer_list<value_type> il, const allocator_type& a = allocator_type())
999 this->assign(il.begin(), il.end());
1003 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1005 //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
1007 //! <b>Throws</b>: If T's move constructor or allocation throws
1009 //! <b>Complexity</b>: Linear.
1011 //! <b>Note</b>: Non-standard extension to support static_vector
1012 template<class OtherAllocator>
1013 vector(BOOST_RV_REF_BEG vector<T, OtherAllocator> BOOST_RV_REF_END x
1014 , typename dtl::enable_if_c
1015 < dtl::is_version<OtherAllocator, 0>::value>::type * = 0
1017 : m_holder(boost::move(x.m_holder))
1020 #endif //!defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1022 //! <b>Effects</b>: Copy constructs a vector using the specified allocator.
1024 //! <b>Postcondition</b>: x == *this.
1026 //! <b>Throws</b>: If allocation
1027 //! throws or T's copy constructor throws.
1029 //! <b>Complexity</b>: Linear to the elements x contains.
1030 vector(const vector &x, const allocator_type &a)
1031 : m_holder(vector_uninitialized_size, a, x.size())
1033 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
1034 this->num_alloc += x.size() != 0;
1036 ::boost::container::uninitialized_copy_alloc_n_source
1037 ( this->m_holder.alloc(), x.priv_raw_begin()
1038 , x.size(), this->priv_raw_begin());
1041 //! <b>Effects</b>: Move constructor using the specified allocator.
1042 //! Moves x's resources to *this if a == allocator_type().
1043 //! Otherwise copies values from x to *this.
1045 //! <b>Throws</b>: If allocation or T's copy constructor throws.
1047 //! <b>Complexity</b>: Constant if a == x.get_allocator(), linear otherwise.
1048 vector(BOOST_RV_REF(vector) x, const allocator_type &a)
1049 : m_holder( vector_uninitialized_size, a
1050 , is_propagable_from(x.get_stored_allocator(), x.m_holder.start(), a, true) ? 0 : x.size()
1053 if(is_propagable_from(x.get_stored_allocator(), x.m_holder.start(), a, true)){
1054 this->m_holder.steal_resources(x.m_holder);
1057 const size_type n = x.size();
1058 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
1059 this->num_alloc += n != 0;
1061 ::boost::container::uninitialized_move_alloc_n_source
1062 ( this->m_holder.alloc(), x.priv_raw_begin()
1063 , n, this->priv_raw_begin());
1067 //! <b>Effects</b>: Destroys the vector. All stored values are destroyed
1068 //! and used memory is deallocated.
1070 //! <b>Throws</b>: Nothing.
1072 //! <b>Complexity</b>: Linear to the number of elements.
1073 ~vector() BOOST_NOEXCEPT_OR_NOTHROW
1075 boost::container::destroy_alloc_n
1076 (this->get_stored_allocator(), this->priv_raw_begin(), this->m_holder.m_size);
1077 //vector_alloc_holder deallocates the data
1080 //! <b>Effects</b>: Makes *this contain the same elements as x.
1082 //! <b>Postcondition</b>: this->size() == x.size(). *this contains a copy
1083 //! of each of x's elements.
1085 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment throws.
1087 //! <b>Complexity</b>: Linear to the number of elements in x.
1088 BOOST_CONTAINER_FORCEINLINE vector& operator=(BOOST_COPY_ASSIGN_REF(vector) x)
1091 this->priv_copy_assign(x);
1096 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1097 //! <b>Effects</b>: Make *this container contains elements from il.
1099 //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
1100 BOOST_CONTAINER_FORCEINLINE vector& operator=(std::initializer_list<value_type> il)
1102 this->assign(il.begin(), il.end());
1107 //! <b>Effects</b>: Move assignment. All x's values are transferred to *this.
1109 //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
1110 //! before the function.
1112 //! <b>Throws</b>: If allocator_traits_type::propagate_on_container_move_assignment
1113 //! is false and (allocation throws or value_type's move constructor throws)
1115 //! <b>Complexity</b>: Constant if allocator_traits_type::
1116 //! propagate_on_container_move_assignment is true or
1117 //! this->get>allocator() == x.get_allocator(). Linear otherwise.
1118 BOOST_CONTAINER_FORCEINLINE vector& operator=(BOOST_RV_REF(vector) x)
1119 BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value
1120 || allocator_traits_type::is_always_equal::value)
1122 BOOST_ASSERT(&x != this);
1123 this->priv_move_assign(boost::move(x));
1127 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1129 //! <b>Effects</b>: Move assignment. All x's values are transferred to *this.
1131 //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
1132 //! before the function.
1134 //! <b>Throws</b>: If move constructor/assignment of T throws or allocation throws
1136 //! <b>Complexity</b>: Linear.
1138 //! <b>Note</b>: Non-standard extension to support static_vector
1139 template<class OtherAllocator>
1140 BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_and
1142 , dtl::is_version<OtherAllocator, 0>
1143 , dtl::is_different<OtherAllocator, allocator_type>
1145 operator=(BOOST_RV_REF_BEG vector<value_type, OtherAllocator> BOOST_RV_REF_END x)
1147 this->priv_move_assign(boost::move(x));
1151 //! <b>Effects</b>: Copy assignment. All x's values are copied to *this.
1153 //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
1154 //! before the function.
1156 //! <b>Throws</b>: If move constructor/assignment of T throws or allocation throws
1158 //! <b>Complexity</b>: Linear.
1160 //! <b>Note</b>: Non-standard extension to support static_vector
1161 template<class OtherAllocator>
1162 BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_and
1164 , dtl::is_version<OtherAllocator, 0>
1165 , dtl::is_different<OtherAllocator, allocator_type>
1167 operator=(const vector<value_type, OtherAllocator> &x)
1169 this->priv_copy_assign(x);
1175 //! <b>Effects</b>: Assigns the the range [first, last) to *this.
1177 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment or
1178 //! T's constructor/assignment from dereferencing InpIt throws.
1180 //! <b>Complexity</b>: Linear to n.
1181 template <class InIt>
1182 void assign(InIt first, InIt last
1183 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_or
1185 BOOST_MOVE_I dtl::is_convertible<InIt BOOST_MOVE_I size_type>
1186 BOOST_MOVE_I dtl::and_
1187 < dtl::is_different<alloc_version BOOST_MOVE_I version_0>
1188 BOOST_MOVE_I dtl::is_not_input_iterator<InIt>
1193 //Overwrite all elements we can from [first, last)
1194 iterator cur = this->begin();
1195 const iterator end_it = this->end();
1196 for ( ; first != last && cur != end_it; ++cur, ++first){
1201 //There are no more elements in the sequence, erase remaining
1202 T* const end_pos = this->priv_raw_end();
1203 const size_type n = static_cast<size_type>(end_pos - boost::movelib::iterator_to_raw_pointer(cur));
1204 this->priv_destroy_last_n(n);
1207 //There are more elements in the range, insert the remaining ones
1208 this->insert(this->cend(), first, last);
1212 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1213 //! <b>Effects</b>: Assigns the the range [il.begin(), il.end()) to *this.
1215 //! <b>Throws</b>: If memory allocation throws or
1216 //! T's constructor from dereferencing iniializer_list iterator throws.
1218 BOOST_CONTAINER_FORCEINLINE void assign(std::initializer_list<T> il)
1220 this->assign(il.begin(), il.end());
1224 //! <b>Effects</b>: Assigns the the range [first, last) to *this.
1226 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment or
1227 //! T's constructor/assignment from dereferencing InpIt throws.
1229 //! <b>Complexity</b>: Linear to n.
1230 template <class FwdIt>
1231 void assign(FwdIt first, FwdIt last
1232 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_or
1234 BOOST_MOVE_I dtl::is_same<alloc_version BOOST_MOVE_I version_0>
1235 BOOST_MOVE_I dtl::is_convertible<FwdIt BOOST_MOVE_I size_type>
1236 BOOST_MOVE_I dtl::is_input_iterator<FwdIt>
1240 //For Fwd iterators the standard only requires EmplaceConstructible and assignable from *first
1241 //so we can't do any backwards allocation
1242 const size_type input_sz = static_cast<size_type>(boost::container::iterator_distance(first, last));
1243 const size_type old_capacity = this->capacity();
1244 if(input_sz > old_capacity){ //If input range is too big, we need to reallocate
1245 size_type real_cap = 0;
1246 pointer reuse(this->m_holder.start());
1247 pointer const ret(this->m_holder.allocation_command(allocate_new|expand_fwd, input_sz, real_cap = input_sz, reuse));
1248 if(!reuse){ //New allocation, just emplace new values
1249 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
1252 pointer const old_p = this->m_holder.start();
1254 this->priv_destroy_all();
1255 this->m_holder.deallocate(old_p, old_capacity);
1257 this->m_holder.start(ret);
1258 this->m_holder.capacity(real_cap);
1259 this->m_holder.m_size = 0;
1260 this->priv_uninitialized_construct_at_end(first, last);
1264 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
1265 ++this->num_expand_fwd;
1267 this->m_holder.capacity(real_cap);
1268 //Forward expansion, use assignment + back deletion/construction that comes later
1271 //Overwrite all elements we can from [first, last)
1272 iterator cur = this->begin();
1273 const iterator end_it = this->end();
1274 for ( ; first != last && cur != end_it; ++cur, ++first){
1279 //There are no more elements in the sequence, erase remaining
1280 this->priv_destroy_last_n(this->size() - input_sz);
1283 //Uninitialized construct at end the remaining range
1284 this->priv_uninitialized_construct_at_end(first, last);
1288 //! <b>Effects</b>: Assigns the n copies of val to *this.
1290 //! <b>Throws</b>: If memory allocation throws or
1291 //! T's copy/move constructor/assignment throws.
1293 //! <b>Complexity</b>: Linear to n.
1294 BOOST_CONTAINER_FORCEINLINE void assign(size_type n, const value_type& val)
1295 { this->assign(cvalue_iterator(val, n), cvalue_iterator()); }
1297 //! <b>Effects</b>: Returns a copy of the internal allocator.
1299 //! <b>Throws</b>: If allocator's copy constructor throws.
1301 //! <b>Complexity</b>: Constant.
1302 allocator_type get_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
1303 { return this->m_holder.alloc(); }
1305 //! <b>Effects</b>: Returns a reference to the internal allocator.
1307 //! <b>Throws</b>: Nothing
1309 //! <b>Complexity</b>: Constant.
1311 //! <b>Note</b>: Non-standard extension.
1312 BOOST_CONTAINER_FORCEINLINE stored_allocator_type &get_stored_allocator() BOOST_NOEXCEPT_OR_NOTHROW
1313 { return this->m_holder.alloc(); }
1315 //! <b>Effects</b>: Returns a reference to the internal allocator.
1317 //! <b>Throws</b>: Nothing
1319 //! <b>Complexity</b>: Constant.
1321 //! <b>Note</b>: Non-standard extension.
1322 BOOST_CONTAINER_FORCEINLINE const stored_allocator_type &get_stored_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
1323 { return this->m_holder.alloc(); }
1325 //////////////////////////////////////////////
1329 //////////////////////////////////////////////
1331 //! <b>Effects</b>: Returns an iterator to the first element contained in the vector.
1333 //! <b>Throws</b>: Nothing.
1335 //! <b>Complexity</b>: Constant.
1336 BOOST_CONTAINER_FORCEINLINE iterator begin() BOOST_NOEXCEPT_OR_NOTHROW
1337 { return iterator(this->m_holder.start()); }
1339 //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
1341 //! <b>Throws</b>: Nothing.
1343 //! <b>Complexity</b>: Constant.
1344 BOOST_CONTAINER_FORCEINLINE const_iterator begin() const BOOST_NOEXCEPT_OR_NOTHROW
1345 { return const_iterator(this->m_holder.start()); }
1347 //! <b>Effects</b>: Returns an iterator to the end of the vector.
1349 //! <b>Throws</b>: Nothing.
1351 //! <b>Complexity</b>: Constant.
1352 BOOST_CONTAINER_FORCEINLINE iterator end() BOOST_NOEXCEPT_OR_NOTHROW
1353 { return iterator(this->m_holder.start() + this->m_holder.m_size); }
1355 //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
1357 //! <b>Throws</b>: Nothing.
1359 //! <b>Complexity</b>: Constant.
1360 BOOST_CONTAINER_FORCEINLINE const_iterator end() const BOOST_NOEXCEPT_OR_NOTHROW
1361 { return this->cend(); }
1363 //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
1364 //! of the reversed vector.
1366 //! <b>Throws</b>: Nothing.
1368 //! <b>Complexity</b>: Constant.
1369 BOOST_CONTAINER_FORCEINLINE reverse_iterator rbegin() BOOST_NOEXCEPT_OR_NOTHROW
1370 { return reverse_iterator(this->end()); }
1372 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
1373 //! of the reversed vector.
1375 //! <b>Throws</b>: Nothing.
1377 //! <b>Complexity</b>: Constant.
1378 BOOST_CONTAINER_FORCEINLINE const_reverse_iterator rbegin() const BOOST_NOEXCEPT_OR_NOTHROW
1379 { return this->crbegin(); }
1381 //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
1382 //! of the reversed vector.
1384 //! <b>Throws</b>: Nothing.
1386 //! <b>Complexity</b>: Constant.
1387 BOOST_CONTAINER_FORCEINLINE reverse_iterator rend() BOOST_NOEXCEPT_OR_NOTHROW
1388 { return reverse_iterator(this->begin()); }
1390 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
1391 //! of the reversed vector.
1393 //! <b>Throws</b>: Nothing.
1395 //! <b>Complexity</b>: Constant.
1396 BOOST_CONTAINER_FORCEINLINE const_reverse_iterator rend() const BOOST_NOEXCEPT_OR_NOTHROW
1397 { return this->crend(); }
1399 //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
1401 //! <b>Throws</b>: Nothing.
1403 //! <b>Complexity</b>: Constant.
1404 BOOST_CONTAINER_FORCEINLINE const_iterator cbegin() const BOOST_NOEXCEPT_OR_NOTHROW
1405 { return const_iterator(this->m_holder.start()); }
1407 //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
1409 //! <b>Throws</b>: Nothing.
1411 //! <b>Complexity</b>: Constant.
1412 BOOST_CONTAINER_FORCEINLINE const_iterator cend() const BOOST_NOEXCEPT_OR_NOTHROW
1413 { return const_iterator(this->m_holder.start() + this->m_holder.m_size); }
1415 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
1416 //! of the reversed vector.
1418 //! <b>Throws</b>: Nothing.
1420 //! <b>Complexity</b>: Constant.
1421 BOOST_CONTAINER_FORCEINLINE const_reverse_iterator crbegin() const BOOST_NOEXCEPT_OR_NOTHROW
1422 { return const_reverse_iterator(this->end());}
1424 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
1425 //! of the reversed vector.
1427 //! <b>Throws</b>: Nothing.
1429 //! <b>Complexity</b>: Constant.
1430 BOOST_CONTAINER_FORCEINLINE const_reverse_iterator crend() const BOOST_NOEXCEPT_OR_NOTHROW
1431 { return const_reverse_iterator(this->begin()); }
1433 //////////////////////////////////////////////
1437 //////////////////////////////////////////////
1439 //! <b>Effects</b>: Returns true if the vector contains no elements.
1441 //! <b>Throws</b>: Nothing.
1443 //! <b>Complexity</b>: Constant.
1444 BOOST_CONTAINER_FORCEINLINE bool empty() const BOOST_NOEXCEPT_OR_NOTHROW
1445 { return !this->m_holder.m_size; }
1447 //! <b>Effects</b>: Returns the number of the elements contained in the vector.
1449 //! <b>Throws</b>: Nothing.
1451 //! <b>Complexity</b>: Constant.
1452 BOOST_CONTAINER_FORCEINLINE size_type size() const BOOST_NOEXCEPT_OR_NOTHROW
1453 { return this->m_holder.m_size; }
1455 //! <b>Effects</b>: Returns the largest possible size of the vector.
1457 //! <b>Throws</b>: Nothing.
1459 //! <b>Complexity</b>: Constant.
1460 BOOST_CONTAINER_FORCEINLINE size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
1461 { return allocator_traits_type::max_size(this->m_holder.alloc()); }
1463 //! <b>Effects</b>: Inserts or erases elements at the end such that
1464 //! the size becomes n. New elements are value initialized.
1466 //! <b>Throws</b>: If memory allocation throws, or T's copy/move or value initialization throws.
1468 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1469 void resize(size_type new_size)
1470 { this->priv_resize(new_size, value_init); }
1472 //! <b>Effects</b>: Inserts or erases elements at the end such that
1473 //! the size becomes n. New elements are default initialized.
1475 //! <b>Throws</b>: If memory allocation throws, or T's copy/move or default initialization throws.
1477 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1479 //! <b>Note</b>: Non-standard extension
1480 void resize(size_type new_size, default_init_t)
1481 { this->priv_resize(new_size, default_init); }
1483 //! <b>Effects</b>: Inserts or erases elements at the end such that
1484 //! the size becomes n. New elements are copy constructed from x.
1486 //! <b>Throws</b>: If memory allocation throws, or T's copy/move constructor throws.
1488 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1489 void resize(size_type new_size, const T& x)
1490 { this->priv_resize(new_size, x); }
1492 //! <b>Effects</b>: Number of elements for which memory has been allocated.
1493 //! capacity() is always greater than or equal to size().
1495 //! <b>Throws</b>: Nothing.
1497 //! <b>Complexity</b>: Constant.
1498 BOOST_CONTAINER_FORCEINLINE size_type capacity() const BOOST_NOEXCEPT_OR_NOTHROW
1499 { return this->m_holder.capacity(); }
1501 //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
1502 //! effect. Otherwise, it is a request for allocation of additional memory.
1503 //! If the request is successful, then capacity() is greater than or equal to
1504 //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
1506 //! <b>Throws</b>: If memory allocation allocation throws or T's copy/move constructor throws.
1507 BOOST_CONTAINER_FORCEINLINE void reserve(size_type new_cap)
1509 if (this->capacity() < new_cap){
1510 this->priv_reserve_no_capacity(new_cap, alloc_version());
1514 //! <b>Effects</b>: Tries to deallocate the excess of memory created
1515 //! with previous allocations. The size of the vector is unchanged
1517 //! <b>Throws</b>: If memory allocation throws, or T's copy/move constructor throws.
1519 //! <b>Complexity</b>: Linear to size().
1520 BOOST_CONTAINER_FORCEINLINE void shrink_to_fit()
1521 { this->priv_shrink_to_fit(alloc_version()); }
1523 //////////////////////////////////////////////
1527 //////////////////////////////////////////////
1529 //! <b>Requires</b>: !empty()
1531 //! <b>Effects</b>: Returns a reference to the first
1532 //! element of the container.
1534 //! <b>Throws</b>: Nothing.
1536 //! <b>Complexity</b>: Constant.
1537 reference front() BOOST_NOEXCEPT_OR_NOTHROW
1539 BOOST_ASSERT(!this->empty());
1540 return *this->m_holder.start();
1543 //! <b>Requires</b>: !empty()
1545 //! <b>Effects</b>: Returns a const reference to the first
1546 //! element of the container.
1548 //! <b>Throws</b>: Nothing.
1550 //! <b>Complexity</b>: Constant.
1551 const_reference front() const BOOST_NOEXCEPT_OR_NOTHROW
1553 BOOST_ASSERT(!this->empty());
1554 return *this->m_holder.start();
1557 //! <b>Requires</b>: !empty()
1559 //! <b>Effects</b>: Returns a reference to the last
1560 //! element of the container.
1562 //! <b>Throws</b>: Nothing.
1564 //! <b>Complexity</b>: Constant.
1565 reference back() BOOST_NOEXCEPT_OR_NOTHROW
1567 BOOST_ASSERT(!this->empty());
1568 return this->m_holder.start()[this->m_holder.m_size - 1];
1571 //! <b>Requires</b>: !empty()
1573 //! <b>Effects</b>: Returns a const reference to the last
1574 //! element of the container.
1576 //! <b>Throws</b>: Nothing.
1578 //! <b>Complexity</b>: Constant.
1579 const_reference back() const BOOST_NOEXCEPT_OR_NOTHROW
1581 BOOST_ASSERT(!this->empty());
1582 return this->m_holder.start()[this->m_holder.m_size - 1];
1585 //! <b>Requires</b>: size() > n.
1587 //! <b>Effects</b>: Returns a reference to the nth element
1588 //! from the beginning of the container.
1590 //! <b>Throws</b>: Nothing.
1592 //! <b>Complexity</b>: Constant.
1593 reference operator[](size_type n) BOOST_NOEXCEPT_OR_NOTHROW
1595 BOOST_ASSERT(this->m_holder.m_size > n);
1596 return this->m_holder.start()[n];
1599 //! <b>Requires</b>: size() > n.
1601 //! <b>Effects</b>: Returns a const reference to the nth element
1602 //! from the beginning of the container.
1604 //! <b>Throws</b>: Nothing.
1606 //! <b>Complexity</b>: Constant.
1607 const_reference operator[](size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
1609 BOOST_ASSERT(this->m_holder.m_size > n);
1610 return this->m_holder.start()[n];
1613 //! <b>Requires</b>: size() >= n.
1615 //! <b>Effects</b>: Returns an iterator to the nth element
1616 //! from the beginning of the container. Returns end()
1619 //! <b>Throws</b>: Nothing.
1621 //! <b>Complexity</b>: Constant.
1623 //! <b>Note</b>: Non-standard extension
1624 iterator nth(size_type n) BOOST_NOEXCEPT_OR_NOTHROW
1626 BOOST_ASSERT(this->m_holder.m_size >= n);
1627 return iterator(this->m_holder.start()+n);
1630 //! <b>Requires</b>: size() >= n.
1632 //! <b>Effects</b>: Returns a const_iterator to the nth element
1633 //! from the beginning of the container. Returns end()
1636 //! <b>Throws</b>: Nothing.
1638 //! <b>Complexity</b>: Constant.
1640 //! <b>Note</b>: Non-standard extension
1641 const_iterator nth(size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
1643 BOOST_ASSERT(this->m_holder.m_size >= n);
1644 return const_iterator(this->m_holder.start()+n);
1647 //! <b>Requires</b>: begin() <= p <= end().
1649 //! <b>Effects</b>: Returns the index of the element pointed by p
1650 //! and size() if p == end().
1652 //! <b>Throws</b>: Nothing.
1654 //! <b>Complexity</b>: Constant.
1656 //! <b>Note</b>: Non-standard extension
1657 size_type index_of(iterator p) BOOST_NOEXCEPT_OR_NOTHROW
1659 //Range check assert done in priv_index_of
1660 return this->priv_index_of(vector_iterator_get_ptr(p));
1663 //! <b>Requires</b>: begin() <= p <= end().
1665 //! <b>Effects</b>: Returns the index of the element pointed by p
1666 //! and size() if p == end().
1668 //! <b>Throws</b>: Nothing.
1670 //! <b>Complexity</b>: Constant.
1672 //! <b>Note</b>: Non-standard extension
1673 size_type index_of(const_iterator p) const BOOST_NOEXCEPT_OR_NOTHROW
1675 //Range check assert done in priv_index_of
1676 return this->priv_index_of(vector_iterator_get_ptr(p));
1679 //! <b>Requires</b>: size() > n.
1681 //! <b>Effects</b>: Returns a reference to the nth element
1682 //! from the beginning of the container.
1684 //! <b>Throws</b>: std::range_error if n >= size()
1686 //! <b>Complexity</b>: Constant.
1687 reference at(size_type n)
1689 this->priv_throw_if_out_of_range(n);
1690 return this->m_holder.start()[n];
1693 //! <b>Requires</b>: size() > n.
1695 //! <b>Effects</b>: Returns a const reference to the nth element
1696 //! from the beginning of the container.
1698 //! <b>Throws</b>: std::range_error if n >= size()
1700 //! <b>Complexity</b>: Constant.
1701 const_reference at(size_type n) const
1703 this->priv_throw_if_out_of_range(n);
1704 return this->m_holder.start()[n];
1707 //////////////////////////////////////////////
1711 //////////////////////////////////////////////
1713 //! <b>Returns</b>: A pointer such that [data(),data() + size()) is a valid range.
1714 //! For a non-empty vector, data() == &front().
1716 //! <b>Throws</b>: Nothing.
1718 //! <b>Complexity</b>: Constant.
1719 T* data() BOOST_NOEXCEPT_OR_NOTHROW
1720 { return this->priv_raw_begin(); }
1722 //! <b>Returns</b>: A pointer such that [data(),data() + size()) is a valid range.
1723 //! For a non-empty vector, data() == &front().
1725 //! <b>Throws</b>: Nothing.
1727 //! <b>Complexity</b>: Constant.
1728 const T * data() const BOOST_NOEXCEPT_OR_NOTHROW
1729 { return this->priv_raw_begin(); }
1731 //////////////////////////////////////////////
1735 //////////////////////////////////////////////
1737 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1738 //! <b>Effects</b>: Inserts an object of type T constructed with
1739 //! std::forward<Args>(args)... in the end of the vector.
1741 //! <b>Returns</b>: A reference to the created object.
1743 //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws or
1744 //! T's copy/move constructor throws.
1746 //! <b>Complexity</b>: Amortized constant time.
1747 template<class ...Args>
1748 BOOST_CONTAINER_FORCEINLINE reference emplace_back(BOOST_FWD_REF(Args)...args)
1750 if (BOOST_LIKELY(this->room_enough())){
1751 //There is more memory, just construct a new object at the end
1752 T* const p = this->priv_raw_end();
1753 allocator_traits_type::construct(this->m_holder.alloc(), p, ::boost::forward<Args>(args)...);
1754 ++this->m_holder.m_size;
1758 typedef dtl::insert_emplace_proxy<Allocator, T*, Args...> type;
1759 return *this->priv_forward_range_insert_no_capacity
1760 (this->back_ptr(), 1, type(::boost::forward<Args>(args)...), alloc_version());
1764 //! <b>Effects</b>: Inserts an object of type T constructed with
1765 //! std::forward<Args>(args)... in the end of the vector.
1767 //! <b>Throws</b>: If the in-place constructor throws.
1769 //! <b>Complexity</b>: Constant time.
1771 //! <b>Note</b>: Non-standard extension.
1772 template<class ...Args>
1773 BOOST_CONTAINER_FORCEINLINE bool stable_emplace_back(BOOST_FWD_REF(Args)...args)
1775 const bool is_room_enough = this->room_enough() || (alloc_version::value == 2 && this->m_holder.try_expand_fwd(1u));
1776 if (BOOST_LIKELY(is_room_enough)){
1777 //There is more memory, just construct a new object at the end
1778 allocator_traits_type::construct(this->m_holder.alloc(), this->priv_raw_end(), ::boost::forward<Args>(args)...);
1779 ++this->m_holder.m_size;
1781 return is_room_enough;
1784 //! <b>Requires</b>: position must be a valid iterator of *this.
1786 //! <b>Effects</b>: Inserts an object of type T constructed with
1787 //! std::forward<Args>(args)... before position
1789 //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws or
1790 //! T's copy/move constructor/assignment throws.
1792 //! <b>Complexity</b>: If position is end(), amortized constant time
1793 //! Linear time otherwise.
1794 template<class ...Args>
1795 iterator emplace(const_iterator position, BOOST_FWD_REF(Args) ...args)
1797 BOOST_ASSERT(this->priv_in_range_or_end(position));
1798 //Just call more general insert(pos, size, value) and return iterator
1799 typedef dtl::insert_emplace_proxy<Allocator, T*, Args...> type;
1800 return this->priv_forward_range_insert( vector_iterator_get_ptr(position), 1
1801 , type(::boost::forward<Args>(args)...));
1804 #else // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
1806 #define BOOST_CONTAINER_VECTOR_EMPLACE_CODE(N) \
1807 BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
1808 BOOST_CONTAINER_FORCEINLINE reference emplace_back(BOOST_MOVE_UREF##N)\
1810 if (BOOST_LIKELY(this->room_enough())){\
1811 T* const p = this->priv_raw_end();\
1812 allocator_traits_type::construct (this->m_holder.alloc()\
1813 , this->priv_raw_end() BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
1814 ++this->m_holder.m_size;\
1818 typedef dtl::insert_emplace_proxy_arg##N<Allocator, T* BOOST_MOVE_I##N BOOST_MOVE_TARG##N> type;\
1819 return *this->priv_forward_range_insert_no_capacity\
1820 ( this->back_ptr(), 1, type(BOOST_MOVE_FWD##N), alloc_version());\
1824 BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
1825 BOOST_CONTAINER_FORCEINLINE bool stable_emplace_back(BOOST_MOVE_UREF##N)\
1827 const bool is_room_enough = this->room_enough() || (alloc_version::value == 2 && this->m_holder.try_expand_fwd(1u));\
1828 if (BOOST_LIKELY(is_room_enough)){\
1829 allocator_traits_type::construct (this->m_holder.alloc()\
1830 , this->priv_raw_end() BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
1831 ++this->m_holder.m_size;\
1833 return is_room_enough;\
1836 BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
1837 iterator emplace(const_iterator pos BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
1839 BOOST_ASSERT(this->priv_in_range_or_end(pos));\
1840 typedef dtl::insert_emplace_proxy_arg##N<Allocator, T* BOOST_MOVE_I##N BOOST_MOVE_TARG##N> type;\
1841 return this->priv_forward_range_insert(vector_iterator_get_ptr(pos), 1, type(BOOST_MOVE_FWD##N));\
1844 BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_VECTOR_EMPLACE_CODE)
1845 #undef BOOST_CONTAINER_VECTOR_EMPLACE_CODE
1849 #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1850 //! <b>Effects</b>: Inserts a copy of x at the end of the vector.
1852 //! <b>Throws</b>: If memory allocation throws or
1853 //! T's copy/move constructor throws.
1855 //! <b>Complexity</b>: Amortized constant time.
1856 void push_back(const T &x);
1858 //! <b>Effects</b>: Constructs a new element in the end of the vector
1859 //! and moves the resources of x to this new element.
1861 //! <b>Throws</b>: If memory allocation throws or
1862 //! T's copy/move constructor throws.
1864 //! <b>Complexity</b>: Amortized constant time.
1865 void push_back(T &&x);
1867 BOOST_MOVE_CONVERSION_AWARE_CATCH(push_back, T, void, priv_push_back)
1870 #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1871 //! <b>Requires</b>: position must be a valid iterator of *this.
1873 //! <b>Effects</b>: Insert a copy of x before position.
1875 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment throws.
1877 //! <b>Complexity</b>: If position is end(), amortized constant time
1878 //! Linear time otherwise.
1879 iterator insert(const_iterator position, const T &x);
1881 //! <b>Requires</b>: position must be a valid iterator of *this.
1883 //! <b>Effects</b>: Insert a new element before position with x's resources.
1885 //! <b>Throws</b>: If memory allocation throws.
1887 //! <b>Complexity</b>: If position is end(), amortized constant time
1888 //! Linear time otherwise.
1889 iterator insert(const_iterator position, T &&x);
1891 BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, T, iterator, priv_insert, const_iterator, const_iterator)
1894 //! <b>Requires</b>: p must be a valid iterator of *this.
1896 //! <b>Effects</b>: Insert n copies of x before pos.
1898 //! <b>Returns</b>: an iterator to the first inserted element or p if n is 0.
1900 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor throws.
1902 //! <b>Complexity</b>: Linear to n.
1903 iterator insert(const_iterator p, size_type n, const T& x)
1905 BOOST_ASSERT(this->priv_in_range_or_end(p));
1906 dtl::insert_n_copies_proxy<Allocator, T*> proxy(x);
1907 return this->priv_forward_range_insert(vector_iterator_get_ptr(p), n, proxy);
1910 //! <b>Requires</b>: p must be a valid iterator of *this.
1912 //! <b>Effects</b>: Insert a copy of the [first, last) range before pos.
1914 //! <b>Returns</b>: an iterator to the first inserted element or pos if first == last.
1916 //! <b>Throws</b>: If memory allocation throws, T's constructor from a
1917 //! dereferenced InpIt throws or T's copy/move constructor/assignment throws.
1919 //! <b>Complexity</b>: Linear to boost::container::iterator_distance [first, last).
1920 template <class InIt>
1921 iterator insert(const_iterator pos, InIt first, InIt last
1922 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1923 , typename dtl::disable_if_or
1925 , dtl::is_convertible<InIt, size_type>
1926 , dtl::is_not_input_iterator<InIt>
1931 BOOST_ASSERT(this->priv_in_range_or_end(pos));
1932 const size_type n_pos = pos - this->cbegin();
1933 iterator it(vector_iterator_get_ptr(pos));
1934 for(;first != last; ++first){
1935 it = this->emplace(it, *first);
1938 return iterator(this->m_holder.start() + n_pos);
1941 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1942 template <class FwdIt>
1943 iterator insert(const_iterator pos, FwdIt first, FwdIt last
1944 , typename dtl::disable_if_or
1946 , dtl::is_convertible<FwdIt, size_type>
1947 , dtl::is_input_iterator<FwdIt>
1951 BOOST_ASSERT(this->priv_in_range_or_end(pos));
1952 dtl::insert_range_proxy<Allocator, FwdIt, T*> proxy(first);
1953 return this->priv_forward_range_insert(vector_iterator_get_ptr(pos), boost::container::iterator_distance(first, last), proxy);
1957 //! <b>Requires</b>: p must be a valid iterator of *this. num, must
1958 //! be equal to boost::container::iterator_distance(first, last)
1960 //! <b>Effects</b>: Insert a copy of the [first, last) range before pos.
1962 //! <b>Returns</b>: an iterator to the first inserted element or pos if first == last.
1964 //! <b>Throws</b>: If memory allocation throws, T's constructor from a
1965 //! dereferenced InpIt throws or T's copy/move constructor/assignment throws.
1967 //! <b>Complexity</b>: Linear to boost::container::iterator_distance [first, last).
1969 //! <b>Note</b>: This function avoids a linear operation to calculate boost::container::iterator_distance[first, last)
1970 //! for forward and bidirectional iterators, and a one by one insertion for input iterators. This is a
1971 //! a non-standard extension.
1972 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1973 template <class InIt>
1974 iterator insert(const_iterator pos, size_type num, InIt first, InIt last)
1976 BOOST_ASSERT(this->priv_in_range_or_end(pos));
1977 BOOST_ASSERT(dtl::is_input_iterator<InIt>::value ||
1978 num == static_cast<size_type>(boost::container::iterator_distance(first, last)));
1980 dtl::insert_range_proxy<Allocator, InIt, T*> proxy(first);
1981 return this->priv_forward_range_insert(vector_iterator_get_ptr(pos), num, proxy);
1985 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1986 //! <b>Requires</b>: position must be a valid iterator of *this.
1988 //! <b>Effects</b>: Insert a copy of the [il.begin(), il.end()) range before position.
1990 //! <b>Returns</b>: an iterator to the first inserted element or position if first == last.
1992 //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
1993 iterator insert(const_iterator position, std::initializer_list<value_type> il)
1995 //Assertion done in insert()
1996 return this->insert(position, il.begin(), il.end());
2000 //! <b>Effects</b>: Removes the last element from the container.
2002 //! <b>Throws</b>: Nothing.
2004 //! <b>Complexity</b>: Constant time.
2005 void pop_back() BOOST_NOEXCEPT_OR_NOTHROW
2007 BOOST_ASSERT(!this->empty());
2008 //Destroy last element
2009 this->priv_destroy_last();
2012 //! <b>Effects</b>: Erases the element at position pos.
2014 //! <b>Throws</b>: Nothing.
2016 //! <b>Complexity</b>: Linear to the elements between pos and the
2017 //! last element. Constant if pos is the last element.
2018 iterator erase(const_iterator position)
2020 BOOST_ASSERT(this->priv_in_range(position));
2021 const pointer p = vector_iterator_get_ptr(position);
2022 T *const pos_ptr = boost::movelib::to_raw_pointer(p);
2023 T *const beg_ptr = this->priv_raw_begin();
2024 T *const new_end_ptr = ::boost::container::move(pos_ptr + 1, beg_ptr + this->m_holder.m_size, pos_ptr);
2025 //Move elements forward and destroy last
2026 this->priv_destroy_last(pos_ptr != new_end_ptr);
2030 //! <b>Effects</b>: Erases the elements pointed by [first, last).
2032 //! <b>Throws</b>: Nothing.
2034 //! <b>Complexity</b>: Linear to the distance between first and last
2035 //! plus linear to the elements between pos and the last element.
2036 iterator erase(const_iterator first, const_iterator last)
2038 BOOST_ASSERT(first == last ||
2039 (first < last && this->priv_in_range(first) && this->priv_in_range_or_end(last)));
2041 T* const old_end_ptr = this->priv_raw_end();
2042 T* const first_ptr = boost::movelib::to_raw_pointer(vector_iterator_get_ptr(first));
2043 T* const last_ptr = boost::movelib::to_raw_pointer(vector_iterator_get_ptr(last));
2044 T* const ptr = boost::movelib::to_raw_pointer(boost::container::move(last_ptr, old_end_ptr, first_ptr));
2045 this->priv_destroy_last_n(old_end_ptr - ptr);
2047 return iterator(vector_iterator_get_ptr(first));
2050 //! <b>Effects</b>: Swaps the contents of *this and x.
2052 //! <b>Throws</b>: Nothing.
2054 //! <b>Complexity</b>: Constant.
2055 BOOST_CONTAINER_FORCEINLINE void swap(vector& x)
2056 BOOST_NOEXCEPT_IF( ((allocator_traits_type::propagate_on_container_swap::value
2057 || allocator_traits_type::is_always_equal::value) &&
2058 !dtl::is_version<Allocator, 0>::value))
2060 this->priv_swap(x, dtl::bool_<dtl::is_version<Allocator, 0>::value>());
2063 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2065 //! <b>Effects</b>: Swaps the contents of *this and x.
2067 //! <b>Throws</b>: Nothing.
2069 //! <b>Complexity</b>: Linear
2071 //! <b>Note</b>: Non-standard extension to support static_vector
2072 template<class OtherAllocator>
2073 BOOST_CONTAINER_FORCEINLINE void swap(vector<T, OtherAllocator> & x
2074 , typename dtl::enable_if_and
2076 , dtl::is_version<OtherAllocator, 0>
2077 , dtl::is_different<OtherAllocator, allocator_type>
2080 { this->m_holder.deep_swap(x.m_holder); }
2082 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2084 //! <b>Effects</b>: Erases all the elements of the vector.
2086 //! <b>Throws</b>: Nothing.
2088 //! <b>Complexity</b>: Linear to the number of elements in the container.
2089 BOOST_CONTAINER_FORCEINLINE void clear() BOOST_NOEXCEPT_OR_NOTHROW
2090 { this->priv_destroy_all(); }
2092 //! <b>Effects</b>: Returns true if x and y are equal
2094 //! <b>Complexity</b>: Linear to the number of elements in the container.
2095 BOOST_CONTAINER_FORCEINLINE friend bool operator==(const vector& x, const vector& y)
2096 { return x.size() == y.size() && ::boost::container::algo_equal(x.begin(), x.end(), y.begin()); }
2098 //! <b>Effects</b>: Returns true if x and y are unequal
2100 //! <b>Complexity</b>: Linear to the number of elements in the container.
2101 BOOST_CONTAINER_FORCEINLINE friend bool operator!=(const vector& x, const vector& y)
2102 { return !(x == y); }
2104 //! <b>Effects</b>: Returns true if x is less than y
2106 //! <b>Complexity</b>: Linear to the number of elements in the container.
2107 friend bool operator<(const vector& x, const vector& y)
2109 const_iterator first1(x.cbegin()), first2(y.cbegin());
2110 const const_iterator last1(x.cend()), last2(y.cend());
2111 for ( ; (first1 != last1) && (first2 != last2); ++first1, ++first2 ) {
2112 if (*first1 < *first2) return true;
2113 if (*first2 < *first1) return false;
2115 return (first1 == last1) && (first2 != last2);
2118 //! <b>Effects</b>: Returns true if x is greater than y
2120 //! <b>Complexity</b>: Linear to the number of elements in the container.
2121 BOOST_CONTAINER_FORCEINLINE friend bool operator>(const vector& x, const vector& y)
2124 //! <b>Effects</b>: Returns true if x is equal or less than y
2126 //! <b>Complexity</b>: Linear to the number of elements in the container.
2127 BOOST_CONTAINER_FORCEINLINE friend bool operator<=(const vector& x, const vector& y)
2128 { return !(y < x); }
2130 //! <b>Effects</b>: Returns true if x is equal or greater than y
2132 //! <b>Complexity</b>: Linear to the number of elements in the container.
2133 BOOST_CONTAINER_FORCEINLINE friend bool operator>=(const vector& x, const vector& y)
2134 { return !(x < y); }
2136 //! <b>Effects</b>: x.swap(y)
2138 //! <b>Complexity</b>: Constant.
2139 BOOST_CONTAINER_FORCEINLINE friend void swap(vector& x, vector& y)
2142 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2143 //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
2144 //! effect. Otherwise, it is a request for allocation of additional memory
2145 //! (memory expansion) that will not invalidate iterators.
2146 //! If the request is successful, then capacity() is greater than or equal to
2147 //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
2149 //! <b>Throws</b>: If memory allocation allocation throws or T's copy/move constructor throws.
2151 //! <b>Note</b>: Non-standard extension.
2152 bool stable_reserve(size_type new_cap)
2154 const size_type cp = this->capacity();
2155 return cp >= new_cap || (alloc_version::value == 2 && this->m_holder.try_expand_fwd(new_cap - cp));
2158 //Absolutely experimental. This function might change, disappear or simply crash!
2159 template<class BiDirPosConstIt, class BiDirValueIt>
2160 BOOST_CONTAINER_FORCEINLINE void insert_ordered_at(const size_type element_count, BiDirPosConstIt last_position_it, BiDirValueIt last_value_it)
2162 typedef vector_insert_ordered_cursor<BiDirPosConstIt, BiDirValueIt> inserter_t;
2163 return this->priv_insert_ordered_at(element_count, inserter_t(last_position_it, last_value_it));
2166 template<class InputIt>
2167 BOOST_CONTAINER_FORCEINLINE void merge(InputIt first, InputIt last)
2168 { this->merge(first, last, value_less_t()); }
2170 template<class InputIt, class Compare>
2171 BOOST_CONTAINER_FORCEINLINE void merge(InputIt first, InputIt last, Compare comp)
2173 size_type const s = this->size();
2174 size_type const c = this->capacity();
2176 size_type const free_cap = c - s;
2177 //If not input iterator and new elements don't fit in the remaining capacity, merge in new buffer
2178 if(!dtl::is_input_iterator<InputIt>::value &&
2179 free_cap < (n = static_cast<size_type>(boost::container::iterator_distance(first, last)))){
2180 this->priv_merge_in_new_buffer(first, n, comp, alloc_version());
2183 iterator pos(this->insert(this->cend(), first, last));
2184 T *const raw_beg = this->priv_raw_begin();
2185 T *const raw_end = this->priv_raw_end();
2186 T *const raw_pos = raw_beg + s;
2187 boost::movelib::adaptive_merge(raw_beg, raw_pos, raw_end, comp, raw_end, free_cap - n);
2191 template<class InputIt>
2192 BOOST_CONTAINER_FORCEINLINE void merge_unique(InputIt first, InputIt last)
2193 { this->merge_unique(first, last, value_less_t()); }
2195 template<class InputIt, class Compare>
2196 BOOST_CONTAINER_FORCEINLINE void merge_unique(InputIt first, InputIt last, Compare comp)
2198 size_type const s = this->size();
2199 this->priv_set_difference_back(first, last, comp);
2200 T *const raw_beg = this->priv_raw_begin();
2201 T *const raw_end = this->priv_raw_end();
2202 T *raw_pos = raw_beg + s;
2203 boost::movelib::adaptive_merge(raw_beg, raw_pos, raw_end, comp, raw_end, this->capacity() - this->size());
2207 template<class PositionValue>
2208 void priv_insert_ordered_at(const size_type element_count, PositionValue position_value)
2210 const size_type old_size_pos = this->size();
2211 this->reserve(old_size_pos + element_count);
2212 T* const begin_ptr = this->priv_raw_begin();
2213 size_type insertions_left = element_count;
2214 size_type prev_pos = old_size_pos;
2215 size_type old_hole_size = element_count;
2217 //Exception rollback. If any copy throws before the hole is filled, values
2218 //already inserted/copied at the end of the buffer will be destroyed.
2219 typename value_traits::ArrayDestructor past_hole_values_destroyer
2220 (begin_ptr + old_size_pos + element_count, this->m_holder.alloc(), size_type(0u));
2221 //Loop for each insertion backwards, first moving the elements after the insertion point,
2222 //then inserting the element.
2223 while(insertions_left){
2225 size_type const pos = position_value.get_pos();
2226 BOOST_ASSERT(pos != size_type(-1) && pos <= old_size_pos && pos <= prev_pos);
2227 //If needed shift the range after the insertion point and the previous insertion point.
2228 //Function will take care if the shift crosses the size() boundary, using copy/move
2229 //or uninitialized copy/move if necessary.
2230 size_type new_hole_size = (pos != prev_pos)
2231 ? priv_insert_ordered_at_shift_range(pos, prev_pos, this->size(), insertions_left)
2235 //The hole was reduced by priv_insert_ordered_at_shift_range so expand exception rollback range backwards
2236 past_hole_values_destroyer.increment_size_backwards(prev_pos - pos);
2237 //Insert the new value in the hole
2238 allocator_traits_type::construct(this->m_holder.alloc(), begin_ptr + pos + insertions_left - 1, position_value.get_val());
2239 if(--new_hole_size){
2240 //The hole was reduced by the new insertion by one
2241 past_hole_values_destroyer.increment_size_backwards(size_type(1u));
2244 //Hole was just filled, disable exception rollback and change vector size
2245 past_hole_values_destroyer.release();
2246 this->m_holder.m_size += element_count;
2251 //Hole was just filled by priv_insert_ordered_at_shift_range, disable exception rollback and change vector size
2252 past_hole_values_destroyer.release();
2253 this->m_holder.m_size += element_count;
2255 //Insert the new value in the already constructed range
2256 begin_ptr[pos + insertions_left - 1] = position_value.get_val();
2259 old_hole_size = new_hole_size;
2264 template<class InputIt, class Compare>
2265 void priv_set_difference_back(InputIt first1, InputIt last1, Compare comp)
2267 T * old_first2 = this->priv_raw_begin();
2268 T * first2 = old_first2;
2269 T * last2 = this->priv_raw_end();
2271 while (first1 != last1) {
2272 if (first2 == last2){
2273 this->insert(this->cend(), first1, last1);
2277 if (comp(*first1, *first2)) {
2278 this->emplace_back(*first1);
2279 //Reallocation happened, update range
2280 T * const raw_begin = this->priv_raw_begin();
2281 if(old_first2 != raw_begin){
2282 first2 = raw_begin + (first2 - old_first2);
2283 last2 = first2 + (last2 - old_first2);
2284 old_first2 = raw_begin;
2290 if (!comp(*first2, *first1)) {
2298 template<class FwdIt, class Compare>
2299 BOOST_CONTAINER_FORCEINLINE void priv_merge_in_new_buffer(FwdIt, size_type, Compare, version_0)
2304 template<class FwdIt, class Compare, class Version>
2305 void priv_merge_in_new_buffer(FwdIt first, size_type n, Compare comp, Version)
2307 size_type const new_size = this->size() + n;
2308 size_type new_cap = new_size;
2309 pointer p = pointer();
2310 pointer const new_storage = this->m_holder.allocation_command(allocate_new, new_size, new_cap, p);
2312 BOOST_ASSERT((new_cap >= this->size() ) && (new_cap - this->size()) >= n);
2313 allocator_type &a = this->m_holder.alloc();
2314 typename value_traits::ArrayDeallocator new_buffer_deallocator(new_storage, a, new_cap);
2315 typename value_traits::ArrayDestructor new_values_destroyer(new_storage, a, 0u);
2316 T* pbeg = this->priv_raw_begin();
2317 size_type const old_size = this->size();
2318 T* const pend = pbeg + old_size;
2319 T* d_first = boost::movelib::to_raw_pointer(new_storage);
2320 size_type added = n;
2321 //Merge in new buffer loop
2324 ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), pbeg, pend, d_first);
2327 else if(pbeg == pend) {
2328 ::boost::container::uninitialized_move_alloc_n(this->m_holder.alloc(), first, n, d_first);
2331 //maintain stability moving external values only if they are strictly less
2332 else if(comp(*first, *pbeg)) {
2333 allocator_traits_type::construct( this->m_holder.alloc(), d_first, *first );
2334 new_values_destroyer.increment_size(1u);
2340 allocator_traits_type::construct( this->m_holder.alloc(), d_first, boost::move(*pbeg) );
2341 new_values_destroyer.increment_size(1u);
2347 //Nothrow operations
2348 pointer const old_p = this->m_holder.start();
2349 size_type const old_cap = this->m_holder.capacity();
2350 boost::container::destroy_alloc_n(a, boost::movelib::to_raw_pointer(old_p), old_size);
2351 this->m_holder.deallocate(old_p, old_cap);
2352 this->m_holder.m_size = old_size + added;
2353 this->m_holder.start(new_storage);
2354 this->m_holder.capacity(new_cap);
2355 new_buffer_deallocator.release();
2356 new_values_destroyer.release();
2359 BOOST_CONTAINER_FORCEINLINE bool room_enough() const
2360 { return this->m_holder.m_size < this->m_holder.capacity(); }
2362 BOOST_CONTAINER_FORCEINLINE pointer back_ptr() const
2363 { return this->m_holder.start() + this->m_holder.m_size; }
2365 size_type priv_index_of(pointer p) const
2367 BOOST_ASSERT(this->m_holder.start() <= p);
2368 BOOST_ASSERT(p <= (this->m_holder.start()+this->size()));
2369 return static_cast<size_type>(p - this->m_holder.start());
2372 template<class OtherAllocator>
2373 void priv_move_assign(BOOST_RV_REF_BEG vector<T, OtherAllocator> BOOST_RV_REF_END x
2374 , typename dtl::enable_if_c
2375 < dtl::is_version<OtherAllocator, 0>::value >::type * = 0)
2377 if(!dtl::is_same<OtherAllocator, allocator_type>::value &&
2378 this->capacity() < x.size()){
2381 T* const this_start = this->priv_raw_begin();
2382 T* const other_start = x.priv_raw_begin();
2383 const size_type this_sz = m_holder.m_size;
2384 const size_type other_sz = static_cast<size_type>(x.m_holder.m_size);
2385 boost::container::move_assign_range_alloc_n(this->m_holder.alloc(), other_start, other_sz, this_start, this_sz);
2386 this->m_holder.m_size = other_sz;
2389 template<class OtherAllocator>
2390 void priv_move_assign(BOOST_RV_REF_BEG vector<T, OtherAllocator> BOOST_RV_REF_END x
2391 , typename dtl::disable_if_or
2393 , dtl::is_version<OtherAllocator, 0>
2394 , dtl::is_different<OtherAllocator, allocator_type>
2397 //for move assignment, no aliasing (&x != this) is assummed.
2398 BOOST_ASSERT(this != &x);
2399 allocator_type &this_alloc = this->m_holder.alloc();
2400 allocator_type &x_alloc = x.m_holder.alloc();
2401 const bool propagate_alloc = allocator_traits_type::propagate_on_container_move_assignment::value;
2403 const bool is_propagable_from_x = is_propagable_from(x_alloc, x.m_holder.start(), this_alloc, propagate_alloc);
2404 const bool is_propagable_from_t = is_propagable_from(this_alloc, m_holder.start(), x_alloc, propagate_alloc);
2405 const bool are_both_propagable = is_propagable_from_x && is_propagable_from_t;
2407 //Resources can be transferred if both allocators are
2408 //going to be equal after this function (either propagated or already equal)
2409 if(are_both_propagable){
2410 //Destroy objects but retain memory in case x reuses it in the future
2412 this->m_holder.swap_resources(x.m_holder);
2414 else if(is_propagable_from_x){
2416 this->m_holder.deallocate(this->m_holder.m_start, this->m_holder.m_capacity);
2417 this->m_holder.steal_resources(x.m_holder);
2419 //Else do a one by one move
2421 this->assign( boost::make_move_iterator(boost::movelib::iterator_to_raw_pointer(x.begin()))
2422 , boost::make_move_iterator(boost::movelib::iterator_to_raw_pointer(x.end() ))
2425 //Move allocator if needed
2426 dtl::move_alloc(this_alloc, x_alloc, dtl::bool_<propagate_alloc>());
2429 template<class OtherAllocator>
2430 void priv_copy_assign(const vector<T, OtherAllocator> &x
2431 , typename dtl::enable_if_c
2432 < dtl::is_version<OtherAllocator, 0>::value >::type * = 0)
2434 if(!dtl::is_same<OtherAllocator, allocator_type>::value &&
2435 this->capacity() < x.size()){
2438 T* const this_start = this->priv_raw_begin();
2439 T* const other_start = x.priv_raw_begin();
2440 const size_type this_sz = m_holder.m_size;
2441 const size_type other_sz = static_cast<size_type>(x.m_holder.m_size);
2442 boost::container::copy_assign_range_alloc_n(this->m_holder.alloc(), other_start, other_sz, this_start, this_sz);
2443 this->m_holder.m_size = other_sz;
2446 template<class OtherAllocator>
2447 typename dtl::disable_if_or
2449 , dtl::is_version<OtherAllocator, 0>
2450 , dtl::is_different<OtherAllocator, allocator_type>
2452 priv_copy_assign(const vector<T, OtherAllocator> &x)
2454 allocator_type &this_alloc = this->m_holder.alloc();
2455 const allocator_type &x_alloc = x.m_holder.alloc();
2456 dtl::bool_<allocator_traits_type::
2457 propagate_on_container_copy_assignment::value> flag;
2458 if(flag && this_alloc != x_alloc){
2460 this->shrink_to_fit();
2462 dtl::assign_alloc(this_alloc, x_alloc, flag);
2463 this->assign( x.priv_raw_begin(), x.priv_raw_end() );
2466 template<class Vector> //Template it to avoid it in explicit instantiations
2467 void priv_swap(Vector &x, dtl::true_type) //version_0
2468 { this->m_holder.deep_swap(x.m_holder); }
2470 template<class Vector> //Template it to avoid it in explicit instantiations
2471 void priv_swap(Vector &x, dtl::false_type) //version_N
2473 const bool propagate_alloc = allocator_traits_type::propagate_on_container_swap::value;
2474 if(are_swap_propagable( this->get_stored_allocator(), this->m_holder.start()
2475 , x.get_stored_allocator(), x.m_holder.start(), propagate_alloc)){
2476 //Just swap internals
2477 this->m_holder.swap_resources(x.m_holder);
2480 //Else swap element by element...
2481 bool const t_smaller = this->size() < x.size();
2482 vector &sml = t_smaller ? *this : x;
2483 vector &big = t_smaller ? x : *this;
2485 size_type const common_elements = sml.size();
2486 for(size_type i = 0; i != common_elements; ++i){
2487 boost::adl_move_swap(sml[i], big[i]);
2489 //... and move-insert the remaining range
2490 sml.insert( sml.cend()
2491 , boost::make_move_iterator(boost::movelib::iterator_to_raw_pointer(big.nth(common_elements)))
2492 , boost::make_move_iterator(boost::movelib::iterator_to_raw_pointer(big.end()))
2494 //Destroy remaining elements
2495 big.erase(big.nth(common_elements), big.cend());
2497 //And now swap the allocator
2498 dtl::swap_alloc(this->m_holder.alloc(), x.m_holder.alloc(), dtl::bool_<propagate_alloc>());
2501 void priv_reserve_no_capacity(size_type, version_0)
2502 { throw_bad_alloc(); }
2504 dtl::insert_range_proxy<Allocator, boost::move_iterator<T*>, T*> priv_dummy_empty_proxy()
2506 return dtl::insert_range_proxy<Allocator, boost::move_iterator<T*>, T*>
2507 (::boost::make_move_iterator((T *)0));
2510 void priv_reserve_no_capacity(size_type new_cap, version_1)
2512 //There is not enough memory, allocate a new buffer
2513 //Pass the hint so that allocators can take advantage of this.
2514 pointer const p = this->m_holder.allocate(new_cap);
2515 //We will reuse insert code, so create a dummy input iterator
2516 this->priv_forward_range_insert_new_allocation
2517 ( boost::movelib::to_raw_pointer(p), new_cap, this->priv_raw_end(), 0, this->priv_dummy_empty_proxy());
2520 void priv_reserve_no_capacity(size_type new_cap, version_2)
2522 //There is not enough memory, allocate a new
2523 //buffer or expand the old one.
2524 bool same_buffer_start;
2525 size_type real_cap = 0;
2526 pointer reuse(this->m_holder.start());
2527 pointer const ret(this->m_holder.allocation_command(allocate_new | expand_fwd | expand_bwd, new_cap, real_cap = new_cap, reuse));
2529 //Check for forward expansion
2530 same_buffer_start = reuse && this->m_holder.start() == ret;
2531 if(same_buffer_start){
2532 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2533 ++this->num_expand_fwd;
2535 this->m_holder.capacity(real_cap);
2537 else{ //If there is no forward expansion, move objects, we will reuse insertion code
2538 T * const new_mem = boost::movelib::to_raw_pointer(ret);
2539 T * const ins_pos = this->priv_raw_end();
2540 if(reuse){ //Backwards (and possibly forward) expansion
2541 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2542 ++this->num_expand_bwd;
2544 this->priv_forward_range_insert_expand_backwards
2545 ( new_mem , real_cap, ins_pos, 0, this->priv_dummy_empty_proxy());
2548 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2551 this->priv_forward_range_insert_new_allocation
2552 ( new_mem, real_cap, ins_pos, 0, this->priv_dummy_empty_proxy());
2557 void priv_destroy_last(const bool moved = false) BOOST_NOEXCEPT_OR_NOTHROW
2560 const bool skip_destructor = value_traits::trivial_dctr || (value_traits::trivial_dctr_after_move && moved);
2561 if(!skip_destructor){
2562 value_type* const p = this->priv_raw_end() - 1;
2563 allocator_traits_type::destroy(this->get_stored_allocator(), p);
2565 --this->m_holder.m_size;
2568 void priv_destroy_last_n(const size_type n) BOOST_NOEXCEPT_OR_NOTHROW
2570 BOOST_ASSERT(n <= this->m_holder.m_size);
2571 if(!value_traits::trivial_dctr){
2572 T* const destroy_pos = this->priv_raw_begin() + (this->m_holder.m_size-n);
2573 boost::container::destroy_alloc_n(this->get_stored_allocator(), destroy_pos, n);
2575 this->m_holder.m_size -= n;
2578 template<class InpIt>
2579 void priv_uninitialized_construct_at_end(InpIt first, InpIt last)
2581 T* const old_end_pos = this->priv_raw_end();
2582 T* const new_end_pos = boost::container::uninitialized_copy_alloc(this->m_holder.alloc(), first, last, old_end_pos);
2583 this->m_holder.m_size += new_end_pos - old_end_pos;
2586 void priv_destroy_all() BOOST_NOEXCEPT_OR_NOTHROW
2588 boost::container::destroy_alloc_n
2589 (this->get_stored_allocator(), this->priv_raw_begin(), this->m_holder.m_size);
2590 this->m_holder.m_size = 0;
2594 iterator priv_insert(const const_iterator &p, BOOST_FWD_REF(U) x)
2596 BOOST_ASSERT(this->priv_in_range_or_end(p));
2597 return this->priv_forward_range_insert
2598 ( vector_iterator_get_ptr(p), 1, dtl::get_insert_value_proxy<T*, Allocator>(::boost::forward<U>(x)));
2601 dtl::insert_copy_proxy<Allocator, T*> priv_single_insert_proxy(const T &x)
2602 { return dtl::insert_copy_proxy<Allocator, T*> (x); }
2604 dtl::insert_move_proxy<Allocator, T*> priv_single_insert_proxy(BOOST_RV_REF(T) x)
2605 { return dtl::insert_move_proxy<Allocator, T*> (x); }
2608 void priv_push_back(BOOST_FWD_REF(U) u)
2610 if (BOOST_LIKELY(this->room_enough())){
2611 //There is more memory, just construct a new object at the end
2612 allocator_traits_type::construct
2613 ( this->m_holder.alloc(), this->priv_raw_end(), ::boost::forward<U>(u) );
2614 ++this->m_holder.m_size;
2617 this->priv_forward_range_insert_no_capacity
2618 ( this->back_ptr(), 1
2619 , this->priv_single_insert_proxy(::boost::forward<U>(u)), alloc_version());
2623 BOOST_CONTAINER_FORCEINLINE dtl::insert_n_copies_proxy<Allocator, T*> priv_resize_proxy(const T &x)
2624 { return dtl::insert_n_copies_proxy<Allocator, T*>(x); }
2626 BOOST_CONTAINER_FORCEINLINE dtl::insert_default_initialized_n_proxy<Allocator, T*> priv_resize_proxy(default_init_t)
2627 { return dtl::insert_default_initialized_n_proxy<Allocator, T*>(); }
2629 BOOST_CONTAINER_FORCEINLINE dtl::insert_value_initialized_n_proxy<Allocator, T*> priv_resize_proxy(value_init_t)
2630 { return dtl::insert_value_initialized_n_proxy<Allocator, T*>(); }
2633 void priv_resize(size_type new_size, const U& u)
2635 const size_type sz = this->size();
2637 //Destroy last elements
2638 this->priv_destroy_last_n(sz - new_size);
2641 const size_type n = new_size - this->size();
2642 this->priv_forward_range_insert_at_end(n, this->priv_resize_proxy(u), alloc_version());
2646 BOOST_CONTAINER_FORCEINLINE void priv_shrink_to_fit(version_0) BOOST_NOEXCEPT_OR_NOTHROW
2649 void priv_shrink_to_fit(version_1)
2651 const size_type cp = this->m_holder.capacity();
2653 const size_type sz = this->size();
2655 this->m_holder.deallocate(this->m_holder.m_start, cp);
2656 this->m_holder.m_start = pointer();
2657 this->m_holder.m_capacity = 0;
2660 //Allocate a new buffer.
2661 //Pass the hint so that allocators can take advantage of this.
2662 pointer const p = this->m_holder.allocate(sz);
2664 //We will reuse insert code, so create a dummy input iterator
2665 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2668 this->priv_forward_range_insert_new_allocation
2669 ( boost::movelib::to_raw_pointer(p), sz
2670 , this->priv_raw_begin(), 0, this->priv_dummy_empty_proxy());
2675 void priv_shrink_to_fit(version_2) BOOST_NOEXCEPT_OR_NOTHROW
2677 const size_type cp = this->m_holder.capacity();
2679 const size_type sz = this->size();
2681 this->m_holder.deallocate(this->m_holder.m_start, cp);
2682 this->m_holder.m_start = pointer();
2683 this->m_holder.m_capacity = 0;
2686 size_type received_size = sz;
2687 pointer reuse(this->m_holder.start());
2688 if(this->m_holder.allocation_command
2689 (shrink_in_place | nothrow_allocation, cp, received_size, reuse)){
2690 this->m_holder.capacity(received_size);
2691 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2699 template <class InsertionProxy>
2700 iterator priv_forward_range_insert_no_capacity
2701 (const pointer &pos, const size_type, const InsertionProxy , version_0)
2704 return iterator(pos);
2707 template <class InsertionProxy>
2708 iterator priv_forward_range_insert_no_capacity
2709 (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, version_1)
2711 //Check if we have enough memory or try to expand current memory
2712 const size_type n_pos = pos - this->m_holder.start();
2713 T *const raw_pos = boost::movelib::to_raw_pointer(pos);
2715 const size_type new_cap = this->m_holder.template next_capacity<growth_factor_type>(n);
2716 //Pass the hint so that allocators can take advantage of this.
2717 T * const new_buf = boost::movelib::to_raw_pointer(this->m_holder.allocate(new_cap));
2718 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2721 this->priv_forward_range_insert_new_allocation
2722 ( new_buf, new_cap, raw_pos, n, insert_range_proxy);
2723 return iterator(this->m_holder.start() + n_pos);
2726 template <class InsertionProxy>
2727 iterator priv_forward_range_insert_no_capacity
2728 (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, version_2)
2730 //Check if we have enough memory or try to expand current memory
2731 T *const raw_pos = boost::movelib::to_raw_pointer(pos);
2732 const size_type n_pos = raw_pos - this->priv_raw_begin();
2734 //There is not enough memory, allocate a new
2735 //buffer or expand the old one.
2736 size_type real_cap = this->m_holder.template next_capacity<growth_factor_type>(n);
2737 pointer reuse(this->m_holder.start());
2738 pointer const ret (this->m_holder.allocation_command
2739 (allocate_new | expand_fwd | expand_bwd, this->m_holder.m_size + n, real_cap, reuse));
2741 //Buffer reallocated
2743 //Forward expansion, delay insertion
2744 if(this->m_holder.start() == ret){
2745 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2746 ++this->num_expand_fwd;
2748 this->m_holder.capacity(real_cap);
2750 this->priv_forward_range_insert_expand_forward(raw_pos, n, insert_range_proxy);
2752 //Backwards (and possibly forward) expansion
2754 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2755 ++this->num_expand_bwd;
2757 this->priv_forward_range_insert_expand_backwards
2758 (boost::movelib::to_raw_pointer(ret), real_cap, raw_pos, n, insert_range_proxy);
2763 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2766 this->priv_forward_range_insert_new_allocation
2767 ( boost::movelib::to_raw_pointer(ret), real_cap, raw_pos, n, insert_range_proxy);
2770 return iterator(this->m_holder.start() + n_pos);
2773 template <class InsertionProxy>
2774 iterator priv_forward_range_insert
2775 (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy)
2777 BOOST_ASSERT(this->m_holder.capacity() >= this->m_holder.m_size);
2778 //Check if we have enough memory or try to expand current memory
2779 const size_type remaining = this->m_holder.capacity() - this->m_holder.m_size;
2781 bool same_buffer_start = n <= remaining;
2782 if (!same_buffer_start){
2783 return priv_forward_range_insert_no_capacity(pos, n, insert_range_proxy, alloc_version());
2787 T *const raw_pos = boost::movelib::to_raw_pointer(pos);
2788 const size_type n_pos = raw_pos - this->priv_raw_begin();
2789 this->priv_forward_range_insert_expand_forward(raw_pos, n, insert_range_proxy);
2790 return iterator(this->m_holder.start() + n_pos);
2794 template <class InsertionProxy>
2795 iterator priv_forward_range_insert_at_end
2796 (const size_type n, const InsertionProxy insert_range_proxy, version_0)
2798 //Check if we have enough memory or try to expand current memory
2799 const size_type remaining = this->m_holder.capacity() - this->m_holder.m_size;
2802 //This will trigger an error
2805 this->priv_forward_range_insert_at_end_expand_forward(n, insert_range_proxy);
2809 template <class InsertionProxy, class AllocVersion>
2810 BOOST_CONTAINER_FORCEINLINE iterator priv_forward_range_insert_at_end
2811 (const size_type n, const InsertionProxy insert_range_proxy, AllocVersion)
2813 return this->priv_forward_range_insert(this->back_ptr(), n, insert_range_proxy);
2816 //Takes the range pointed by [first_pos, last_pos) and shifts it to the right
2817 //by 'shift_count'. 'limit_pos' marks the end of constructed elements.
2819 //Precondition: first_pos <= last_pos <= limit_pos
2821 //The shift operation might cross limit_pos so elements to moved beyond limit_pos
2822 //are uninitialized_moved with an allocator. Other elements are moved.
2824 //The shift operation might left uninitialized elements after limit_pos
2825 //and the number of uninitialized elements is returned by the function.
2828 // first_pos last_pos old_limit
2830 // ____________V_______V__________________V_____________
2831 //| prefix | range | suffix |raw_mem ~
2832 //|____________|_______|__________________|_____________~
2834 //New situation in Case A (hole_size == 0):
2835 // range is moved through move assignments
2837 // first_pos last_pos limit_pos
2839 // ____________V_______V__________________V_____________
2840 //| prefix' | | | range |suffix'|raw_mem ~
2841 //|________________+______|___^___|_______|_____________~
2846 //New situation in Case B (hole_size >= 0):
2847 // range is moved through uninitialized moves
2849 // first_pos last_pos limit_pos
2851 // ____________V_______V__________________V________________
2852 //| prefix' | | | [hole] | range |
2853 //|_______________________________________|________|___^___|
2855 // |_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_^
2857 //New situation in Case C (hole_size == 0):
2858 // range is moved through move assignments and uninitialized moves
2860 // first_pos last_pos limit_pos
2862 // ____________V_______V__________________V___
2863 //| prefix' | | | range |
2864 //|___________________________________|___^___|
2866 // |_>_>_>_>_>_>_>_>_>_>_>^
2867 size_type priv_insert_ordered_at_shift_range
2868 (size_type first_pos, size_type last_pos, size_type limit_pos, size_type shift_count)
2870 BOOST_ASSERT(first_pos <= last_pos);
2871 BOOST_ASSERT(last_pos <= limit_pos);
2873 T* const begin_ptr = this->priv_raw_begin();
2874 T* const first_ptr = begin_ptr + first_pos;
2875 T* const last_ptr = begin_ptr + last_pos;
2877 size_type hole_size = 0;
2879 if((last_pos + shift_count) <= limit_pos){
2881 boost::container::move_backward(first_ptr, last_ptr, last_ptr + shift_count);
2884 else if((first_pos + shift_count) >= limit_pos){
2885 //All uninitialized_moved
2886 ::boost::container::uninitialized_move_alloc
2887 (this->m_holder.alloc(), first_ptr, last_ptr, first_ptr + shift_count);
2888 hole_size = first_pos + shift_count - limit_pos;
2892 //Some uninitialized_moved
2893 T* const limit_ptr = begin_ptr + limit_pos;
2894 T* const boundary_ptr = limit_ptr - shift_count;
2895 ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), boundary_ptr, last_ptr, limit_ptr);
2896 //The rest is move assigned
2897 boost::container::move_backward(first_ptr, boundary_ptr, limit_ptr);
2903 BOOST_CONTAINER_FORCEINLINE T *priv_raw_begin() const
2904 { return boost::movelib::to_raw_pointer(m_holder.start()); }
2906 BOOST_CONTAINER_FORCEINLINE T* priv_raw_end() const
2907 { return this->priv_raw_begin() + this->m_holder.m_size; }
2909 template <class InsertionProxy>
2910 void priv_forward_range_insert_at_end_expand_forward(const size_type n, InsertionProxy insert_range_proxy)
2912 T* const old_finish = this->priv_raw_end();
2913 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n);
2914 this->m_holder.m_size += n;
2917 template <class InsertionProxy>
2918 void priv_forward_range_insert_expand_forward(T* const pos, const size_type n, InsertionProxy insert_range_proxy)
2920 //n can't be 0, because there is nothing to do in that case
2921 if(BOOST_UNLIKELY(!n)) return;
2922 //There is enough memory
2923 T* const old_finish = this->priv_raw_end();
2924 const size_type elems_after = old_finish - pos;
2927 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n);
2928 this->m_holder.m_size += n;
2930 else if (elems_after >= n){
2931 //New elements can be just copied.
2932 //Move to uninitialized memory last objects
2933 ::boost::container::uninitialized_move_alloc
2934 (this->m_holder.alloc(), old_finish - n, old_finish, old_finish);
2935 this->m_holder.m_size += n;
2936 //Copy previous to last objects to the initialized end
2937 boost::container::move_backward(pos, old_finish - n, old_finish);
2938 //Insert new objects in the pos
2939 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, n);
2942 //The new elements don't fit in the [pos, end()) range.
2944 //Copy old [pos, end()) elements to the uninitialized memory (a gap is created)
2945 ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), pos, old_finish, pos + n);
2947 //Copy first new elements in pos (gap is still there)
2948 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, elems_after);
2949 //Copy to the beginning of the unallocated zone the last new elements (the gap is closed).
2950 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n - elems_after);
2951 this->m_holder.m_size += n;
2954 boost::container::destroy_alloc_n(this->get_stored_allocator(), pos + n, elems_after);
2961 template <class InsertionProxy>
2962 void priv_forward_range_insert_new_allocation
2963 (T* const new_start, size_type new_cap, T* const pos, const size_type n, InsertionProxy insert_range_proxy)
2965 //n can be zero, if we want to reallocate!
2966 T *new_finish = new_start;
2968 //Anti-exception rollbacks
2969 typename value_traits::ArrayDeallocator new_buffer_deallocator(new_start, this->m_holder.alloc(), new_cap);
2970 typename value_traits::ArrayDestructor new_values_destroyer(new_start, this->m_holder.alloc(), 0u);
2972 //Initialize with [begin(), pos) old buffer
2973 //the start of the new buffer
2974 T * const old_buffer = this->priv_raw_begin();
2976 new_finish = ::boost::container::uninitialized_move_alloc
2977 (this->m_holder.alloc(), this->priv_raw_begin(), pos, old_finish = new_finish);
2978 new_values_destroyer.increment_size(new_finish - old_finish);
2980 //Initialize new objects, starting from previous point
2981 old_finish = new_finish;
2982 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n);
2984 new_values_destroyer.increment_size(new_finish - old_finish);
2985 //Initialize from the rest of the old buffer,
2986 //starting from previous point
2988 new_finish = ::boost::container::uninitialized_move_alloc
2989 (this->m_holder.alloc(), pos, old_buffer + this->m_holder.m_size, new_finish);
2990 //Destroy and deallocate old elements
2991 //If there is allocated memory, destroy and deallocate
2992 if(!value_traits::trivial_dctr_after_move)
2993 boost::container::destroy_alloc_n(this->get_stored_allocator(), old_buffer, this->m_holder.m_size);
2994 this->m_holder.deallocate(this->m_holder.start(), this->m_holder.capacity());
2996 this->m_holder.start(new_start);
2997 this->m_holder.m_size = size_type(new_finish - new_start);
2998 this->m_holder.capacity(new_cap);
2999 //All construction successful, disable rollbacks
3000 new_values_destroyer.release();
3001 new_buffer_deallocator.release();
3004 template <class InsertionProxy>
3005 void priv_forward_range_insert_expand_backwards
3006 (T* const new_start, const size_type new_capacity,
3007 T* const pos, const size_type n, InsertionProxy insert_range_proxy)
3009 //n can be zero to just expand capacity
3011 T* const old_start = this->priv_raw_begin();
3012 const size_type old_size = this->m_holder.m_size;
3013 T* const old_finish = old_start + old_size;
3015 //We can have 8 possibilities:
3016 const size_type elemsbefore = static_cast<size_type>(pos - old_start);
3017 const size_type s_before = static_cast<size_type>(old_start - new_start);
3018 const size_type before_plus_new = elemsbefore + n;
3020 //Update the vector buffer information to a safe state
3021 this->m_holder.start(new_start);
3022 this->m_holder.capacity(new_capacity);
3023 this->m_holder.m_size = 0;
3025 //If anything goes wrong, this object will destroy
3026 //all the old objects to fulfill previous vector state
3027 typename value_traits::ArrayDestructor old_values_destroyer(old_start, this->m_holder.alloc(), old_size);
3028 //Check if s_before is big enough to hold the beginning of old data + new data
3029 if(s_before >= before_plus_new){
3030 //Copy first old values before pos, after that the new objects
3031 T *const new_elem_pos =
3032 ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), old_start, pos, new_start);
3033 this->m_holder.m_size = elemsbefore;
3034 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), new_elem_pos, n);
3035 this->m_holder.m_size = before_plus_new;
3036 const size_type new_size = old_size + n;
3037 //Check if s_before is so big that even copying the old data + new data
3038 //there is a gap between the new data and the old data
3039 if(s_before >= new_size){
3041 // _________________________________________________________
3042 //| raw_mem | old_begin | old_end |
3043 //| __________________________________|___________|_________|
3046 // _________________________________________________________
3047 //| old_begin | new | old_end | raw_mem |
3048 //|___________|__________|_________|________________________|
3050 //Now initialize the rest of memory with the last old values
3051 if(before_plus_new != new_size){ //Special case to avoid operations in back insertion
3052 ::boost::container::uninitialized_move_alloc
3053 (this->m_holder.alloc(), pos, old_finish, new_start + before_plus_new);
3054 //All new elements correctly constructed, avoid new element destruction
3055 this->m_holder.m_size = new_size;
3057 //Old values destroyed automatically with "old_values_destroyer"
3058 //when "old_values_destroyer" goes out of scope unless the have trivial
3059 //destructor after move.
3060 if(value_traits::trivial_dctr_after_move)
3061 old_values_destroyer.release();
3063 //s_before is so big that divides old_end
3066 // __________________________________________________
3067 //| raw_mem | old_begin | old_end |
3068 //| ___________________________|___________|_________|
3071 // __________________________________________________
3072 //| old_begin | new | old_end | raw_mem |
3073 //|___________|__________|_________|_________________|
3075 //Now initialize the rest of memory with the last old values
3076 //All new elements correctly constructed, avoid new element destruction
3077 const size_type raw_gap = s_before - before_plus_new;
3078 if(!value_traits::trivial_dctr){
3079 //Now initialize the rest of s_before memory with the
3080 //first of elements after new values
3081 ::boost::container::uninitialized_move_alloc_n
3082 (this->m_holder.alloc(), pos, raw_gap, new_start + before_plus_new);
3083 //Now we have a contiguous buffer so program trailing element destruction
3084 //and update size to the final size.
3085 old_values_destroyer.shrink_forward(new_size-s_before);
3086 this->m_holder.m_size = new_size;
3087 //Now move remaining last objects in the old buffer begin
3088 T * const remaining_pos = pos + raw_gap;
3089 if(remaining_pos != old_start){ //Make sure data has to be moved
3090 ::boost::container::move(remaining_pos, old_finish, old_start);
3092 //Once moved, avoid calling the destructors if trivial after move
3093 if(value_traits::trivial_dctr_after_move){
3094 old_values_destroyer.release();
3097 else{ //If trivial destructor, we can uninitialized copy + copy in a single uninitialized copy
3098 ::boost::container::uninitialized_move_alloc_n
3099 (this->m_holder.alloc(), pos, static_cast<size_type>(old_finish - pos), new_start + before_plus_new);
3100 this->m_holder.m_size = new_size;
3101 old_values_destroyer.release();
3106 //Check if we have to do the insertion in two phases
3107 //since maybe s_before is not big enough and
3108 //the buffer was expanded both sides
3111 // _________________________________________________
3112 //| raw_mem | old_begin + old_end | raw_mem |
3113 //|_________|_____________________|_________________|
3115 //New situation with do_after:
3116 // _________________________________________________
3117 //| old_begin + new + old_end | raw_mem |
3118 //|___________________________________|_____________|
3120 //New without do_after:
3121 // _________________________________________________
3122 //| old_begin + new + old_end | raw_mem |
3123 //|____________________________|____________________|
3125 const bool do_after = n > s_before;
3127 //Now we can have two situations: the raw_mem of the
3128 //beginning divides the old_begin, or the new elements:
3129 if (s_before <= elemsbefore) {
3130 //The raw memory divides the old_begin group:
3132 //If we need two phase construction (do_after)
3133 //new group is divided in new = new_beg + new_end groups
3134 //In this phase only new_beg will be inserted
3137 // _________________________________________________
3138 //| raw_mem | old_begin | old_end | raw_mem |
3139 //|_________|___________|_________|_________________|
3141 //New situation with do_after(1):
3142 //This is not definitive situation, the second phase
3144 // _________________________________________________
3145 //| old_begin | new_beg | old_end | raw_mem |
3146 //|___________|_________|_________|_________________|
3148 //New situation without do_after:
3149 // _________________________________________________
3150 //| old_begin | new | old_end | raw_mem |
3151 //|___________|_____|_________|_____________________|
3153 //Copy the first part of old_begin to raw_mem
3154 ::boost::container::uninitialized_move_alloc_n
3155 (this->m_holder.alloc(), old_start, s_before, new_start);
3156 //The buffer is all constructed until old_end,
3157 //so program trailing destruction and assign final size
3158 //if !do_after, s_before+n otherwise.
3159 size_type new_1st_range;
3161 new_1st_range = s_before;
3162 //release destroyer and update size
3163 old_values_destroyer.release();
3167 if(value_traits::trivial_dctr_after_move)
3168 old_values_destroyer.release();
3170 old_values_destroyer.shrink_forward(old_size - (s_before - n));
3173 this->m_holder.m_size = old_size + new_1st_range;
3174 //Now copy the second part of old_begin overwriting itself
3175 T *const next = ::boost::container::move(old_start + s_before, pos, old_start);
3176 //Now copy the new_beg elements
3177 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), next, new_1st_range);
3179 //If there is no after work and the last old part needs to be moved to front, do it
3180 if(!do_after && (n != s_before)){
3181 //Now displace old_end elements
3182 ::boost::container::move(pos, old_finish, next + new_1st_range);
3186 //If we have to expand both sides,
3187 //we will play if the first new values so
3188 //calculate the upper bound of new values
3190 //The raw memory divides the new elements
3192 //If we need two phase construction (do_after)
3193 //new group is divided in new = new_beg + new_end groups
3194 //In this phase only new_beg will be inserted
3197 // _______________________________________________________
3198 //| raw_mem | old_begin | old_end | raw_mem |
3199 //|_______________|___________|_________|_________________|
3201 //New situation with do_after():
3202 // ____________________________________________________
3203 //| old_begin | new_beg | old_end | raw_mem |
3204 //|___________|_______________|_________|______________|
3206 //New situation without do_after:
3207 // ______________________________________________________
3208 //| old_begin | new | old_end | raw_mem |
3209 //|___________|_____|_________|__________________________|
3211 //First copy whole old_begin and part of new to raw_mem
3212 T * const new_pos = ::boost::container::uninitialized_move_alloc
3213 (this->m_holder.alloc(), old_start, pos, new_start);
3214 this->m_holder.m_size = elemsbefore;
3215 const size_type mid_n = s_before - elemsbefore;
3216 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), new_pos, mid_n);
3217 //The buffer is all constructed until old_end,
3219 this->m_holder.m_size = old_size + s_before;
3220 old_values_destroyer.release();
3224 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), old_start, elemsbefore);
3227 //Copy all new elements
3228 const size_type rest_new = n - mid_n;
3229 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), old_start, rest_new);
3230 T* const move_start = old_start + rest_new;
3231 //Displace old_end, but make sure data has to be moved
3232 T* const move_end = move_start != pos ? ::boost::container::move(pos, old_finish, move_start)
3234 //Destroy remaining moved elements from old_end except if they
3235 //have trivial destructor after being moved
3236 size_type n_destroy = s_before - n;
3237 if(!value_traits::trivial_dctr_after_move)
3238 boost::container::destroy_alloc_n(this->get_stored_allocator(), move_end, n_destroy);
3239 this->m_holder.m_size -= n_destroy;
3243 //This is only executed if two phase construction is needed
3245 //The raw memory divides the new elements
3248 // ______________________________________________________
3249 //| raw_mem | old_begin | old_end | raw_mem |
3250 //|______________|___________|____________|______________|
3252 //New situation with do_after(1):
3253 // _______________________________________________________
3254 //| old_begin + new_beg | new_end |old_end | raw_mem |
3255 //|__________________________|_________|________|_________|
3257 //New situation with do_after(2):
3258 // ______________________________________________________
3259 //| old_begin + new | old_end |raw |
3260 //|_______________________________________|_________|____|
3262 const size_type n_after = n - s_before;
3263 const size_type elemsafter = old_size - elemsbefore;
3265 //We can have two situations:
3266 if (elemsafter >= n_after){
3267 //The raw_mem from end will divide displaced old_end
3270 // ______________________________________________________
3271 //| raw_mem | old_begin | old_end | raw_mem |
3272 //|______________|___________|____________|______________|
3274 //New situation with do_after(1):
3275 // _______________________________________________________
3276 //| old_begin + new_beg | new_end |old_end | raw_mem |
3277 //|__________________________|_________|________|_________|
3279 //First copy the part of old_end raw_mem
3280 T* finish_n = old_finish - n_after;
3281 ::boost::container::uninitialized_move_alloc
3282 (this->m_holder.alloc(), finish_n, old_finish, old_finish);
3283 this->m_holder.m_size += n_after;
3284 //Displace the rest of old_end to the new position
3285 boost::container::move_backward(pos, finish_n, old_finish);
3286 //Now overwrite with new_end
3287 //The new_end part is [first + (n - n_after), last)
3288 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, n_after);
3291 //The raw_mem from end will divide new_end part
3294 // _____________________________________________________________
3295 //| raw_mem | old_begin | old_end | raw_mem |
3296 //|______________|___________|____________|_____________________|
3298 //New situation with do_after(2):
3299 // _____________________________________________________________
3300 //| old_begin + new_beg | new_end |old_end | raw_mem |
3301 //|__________________________|_______________|________|_________|
3304 const size_type mid_last_dist = n_after - elemsafter;
3305 //First initialize data in raw memory
3307 //Copy to the old_end part to the uninitialized zone leaving a gap.
3308 ::boost::container::uninitialized_move_alloc
3309 (this->m_holder.alloc(), pos, old_finish, old_finish + mid_last_dist);
3311 typename value_traits::ArrayDestructor old_end_destroyer
3312 (old_finish + mid_last_dist, this->m_holder.alloc(), old_finish - pos);
3314 //Copy the first part to the already constructed old_end zone
3315 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, elemsafter);
3316 //Copy the rest to the uninitialized zone filling the gap
3317 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, mid_last_dist);
3318 this->m_holder.m_size += n_after;
3319 old_end_destroyer.release();
3325 void priv_throw_if_out_of_range(size_type n) const
3327 //If n is out of range, throw an out_of_range exception
3328 if (n >= this->size()){
3329 throw_out_of_range("vector::at out of range");
3333 BOOST_CONTAINER_FORCEINLINE bool priv_in_range(const_iterator pos) const
3335 return (this->begin() <= pos) && (pos < this->end());
3338 BOOST_CONTAINER_FORCEINLINE bool priv_in_range_or_end(const_iterator pos) const
3340 return (this->begin() <= pos) && (pos <= this->end());
3343 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
3345 unsigned int num_expand_fwd;
3346 unsigned int num_expand_bwd;
3347 unsigned int num_shrink;
3348 unsigned int num_alloc;
3349 void reset_alloc_stats()
3350 { num_expand_fwd = num_expand_bwd = num_alloc = 0, num_shrink = 0; }
3352 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3355 }} //namespace boost::container
3357 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3361 //!has_trivial_destructor_after_move<> == true_type
3362 //!specialization for optimizations
3363 template <class T, class Allocator>
3364 struct has_trivial_destructor_after_move<boost::container::vector<T, Allocator> >
3366 typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
3367 static const bool value = ::boost::has_trivial_destructor_after_move<Allocator>::value &&
3368 ::boost::has_trivial_destructor_after_move<pointer>::value;
3373 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3375 #include <boost/container/detail/config_end.hpp>
3377 #endif // #ifndef BOOST_CONTAINER_CONTAINER_VECTOR_HPP