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>
31 #include <boost/container/detail/advanced_insert_int.hpp>
32 #include <boost/container/detail/algorithm.hpp> //equal()
33 #include <boost/container/detail/alloc_helpers.hpp>
34 #include <boost/container/detail/allocation_type.hpp>
35 #include <boost/container/detail/copy_move_algo.hpp>
36 #include <boost/container/detail/destroyers.hpp>
37 #include <boost/container/detail/iterator.hpp>
38 #include <boost/container/detail/iterators.hpp>
39 #include <boost/container/detail/iterator_to_raw_pointer.hpp>
40 #include <boost/container/detail/mpl.hpp>
41 #include <boost/container/detail/next_capacity.hpp>
42 #include <boost/container/detail/to_raw_pointer.hpp>
43 #include <boost/container/detail/type_traits.hpp>
44 #include <boost/container/detail/version_type.hpp>
46 #include <boost/intrusive/pointer_traits.hpp>
48 #include <boost/move/adl_move_swap.hpp>
49 #include <boost/move/iterator.hpp>
50 #include <boost/move/traits.hpp>
51 #include <boost/move/utility_core.hpp>
53 #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
54 #include <boost/move/detail/fwd_macros.hpp>
56 #include <boost/move/detail/move_helpers.hpp>
58 #include <boost/core/no_exceptions_support.hpp>
59 #include <boost/assert.hpp>
60 #include <boost/cstdint.hpp>
63 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
64 #include <initializer_list> //for std::initializer_list
70 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
72 //#define BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
74 namespace container_detail {
76 #ifndef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
78 template <class Pointer, bool IsConst>
82 typedef std::random_access_iterator_tag iterator_category;
83 typedef typename boost::intrusive::pointer_traits<Pointer>::element_type value_type;
84 typedef typename boost::intrusive::pointer_traits<Pointer>::difference_type difference_type;
87 , typename boost::intrusive::pointer_traits<Pointer>::template
88 rebind_pointer<const value_type>::type
91 typedef typename boost::intrusive::pointer_traits<pointer> ptr_traits;
92 typedef typename ptr_traits::reference reference;
94 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
99 BOOST_CONTAINER_FORCEINLINE const Pointer &get_ptr() const BOOST_NOEXCEPT_OR_NOTHROW
102 BOOST_CONTAINER_FORCEINLINE Pointer &get_ptr() BOOST_NOEXCEPT_OR_NOTHROW
105 BOOST_CONTAINER_FORCEINLINE explicit vec_iterator(Pointer ptr) BOOST_NOEXCEPT_OR_NOTHROW
108 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
113 BOOST_CONTAINER_FORCEINLINE vec_iterator() BOOST_NOEXCEPT_OR_NOTHROW
114 : m_ptr() //Value initialization to achieve "null iterators" (N3644)
117 BOOST_CONTAINER_FORCEINLINE vec_iterator(vec_iterator<Pointer, false> const& other) BOOST_NOEXCEPT_OR_NOTHROW
118 : m_ptr(other.get_ptr())
121 //Pointer like operators
122 BOOST_CONTAINER_FORCEINLINE reference operator*() const BOOST_NOEXCEPT_OR_NOTHROW
125 BOOST_CONTAINER_FORCEINLINE pointer operator->() const BOOST_NOEXCEPT_OR_NOTHROW
126 { return ::boost::intrusive::pointer_traits<pointer>::pointer_to(this->operator*()); }
128 BOOST_CONTAINER_FORCEINLINE reference operator[](difference_type off) const BOOST_NOEXCEPT_OR_NOTHROW
129 { return m_ptr[off]; }
131 //Increment / Decrement
132 BOOST_CONTAINER_FORCEINLINE vec_iterator& operator++() BOOST_NOEXCEPT_OR_NOTHROW
133 { ++m_ptr; return *this; }
135 BOOST_CONTAINER_FORCEINLINE vec_iterator operator++(int) BOOST_NOEXCEPT_OR_NOTHROW
136 { return vec_iterator(m_ptr++); }
138 BOOST_CONTAINER_FORCEINLINE vec_iterator& operator--() BOOST_NOEXCEPT_OR_NOTHROW
139 { --m_ptr; return *this; }
141 BOOST_CONTAINER_FORCEINLINE vec_iterator operator--(int) BOOST_NOEXCEPT_OR_NOTHROW
142 { return vec_iterator(m_ptr--); }
145 BOOST_CONTAINER_FORCEINLINE vec_iterator& operator+=(difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
146 { m_ptr += off; return *this; }
148 BOOST_CONTAINER_FORCEINLINE vec_iterator& operator-=(difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
149 { m_ptr -= off; return *this; }
151 BOOST_CONTAINER_FORCEINLINE friend vec_iterator operator+(const vec_iterator &x, difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
152 { return vec_iterator(x.m_ptr+off); }
154 BOOST_CONTAINER_FORCEINLINE friend vec_iterator operator+(difference_type off, vec_iterator right) BOOST_NOEXCEPT_OR_NOTHROW
155 { right.m_ptr += off; return right; }
157 BOOST_CONTAINER_FORCEINLINE friend vec_iterator operator-(vec_iterator left, difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
158 { left.m_ptr -= off; return left; }
160 BOOST_CONTAINER_FORCEINLINE friend difference_type operator-(const vec_iterator &left, const vec_iterator& right) BOOST_NOEXCEPT_OR_NOTHROW
161 { return left.m_ptr - right.m_ptr; }
163 //Comparison operators
164 BOOST_CONTAINER_FORCEINLINE friend bool operator== (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
165 { return l.m_ptr == r.m_ptr; }
167 BOOST_CONTAINER_FORCEINLINE friend bool operator!= (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
168 { return l.m_ptr != r.m_ptr; }
170 BOOST_CONTAINER_FORCEINLINE friend bool operator< (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
171 { return l.m_ptr < r.m_ptr; }
173 BOOST_CONTAINER_FORCEINLINE friend bool operator<= (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
174 { return l.m_ptr <= r.m_ptr; }
176 BOOST_CONTAINER_FORCEINLINE friend bool operator> (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
177 { return l.m_ptr > r.m_ptr; }
179 BOOST_CONTAINER_FORCEINLINE friend bool operator>= (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
180 { return l.m_ptr >= r.m_ptr; }
183 template<class BiDirPosConstIt, class BiDirValueIt>
184 struct vector_insert_ordered_cursor
186 typedef typename iterator_traits<BiDirPosConstIt>::value_type size_type;
187 typedef typename iterator_traits<BiDirValueIt>::reference reference;
189 BOOST_CONTAINER_FORCEINLINE vector_insert_ordered_cursor(BiDirPosConstIt posit, BiDirValueIt valueit)
190 : last_position_it(posit), last_value_it(valueit)
197 while(this->get_pos() == size_type(-1)){
203 BOOST_CONTAINER_FORCEINLINE size_type get_pos() const
204 { return *last_position_it; }
206 BOOST_CONTAINER_FORCEINLINE reference get_val()
207 { return *last_value_it; }
209 BiDirPosConstIt last_position_it;
210 BiDirValueIt last_value_it;
213 template<class T, class SizeType, class BiDirValueIt, class Comp>
214 struct vector_merge_cursor
216 typedef SizeType size_type;
217 typedef typename iterator_traits<BiDirValueIt>::reference reference;
219 BOOST_CONTAINER_FORCEINLINE vector_merge_cursor(T *pbeg, T *plast, BiDirValueIt valueit, Comp &cmp)
220 : m_pbeg(pbeg), m_pcur(--plast), m_valueit(valueit), m_cmp(cmp)
226 const T &t = *m_valueit;
227 while((m_pcur + 1) != m_pbeg){
228 if(!m_cmp(t, *m_pcur)){
235 BOOST_CONTAINER_FORCEINLINE size_type get_pos() const
236 { return static_cast<size_type>((m_pcur + 1) - m_pbeg); }
238 BOOST_CONTAINER_FORCEINLINE reference get_val()
239 { return *m_valueit; }
243 BiDirValueIt m_valueit;
247 } //namespace container_detail {
249 template<class Pointer, bool IsConst>
250 BOOST_CONTAINER_FORCEINLINE const Pointer &vector_iterator_get_ptr(const container_detail::vec_iterator<Pointer, IsConst> &it) BOOST_NOEXCEPT_OR_NOTHROW
251 { return it.get_ptr(); }
253 template<class Pointer, bool IsConst>
254 BOOST_CONTAINER_FORCEINLINE Pointer &get_ptr(container_detail::vec_iterator<Pointer, IsConst> &it) BOOST_NOEXCEPT_OR_NOTHROW
255 { return it.get_ptr(); }
257 namespace container_detail {
259 #else //ifndef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
261 template< class MaybeConstPointer
262 , bool ElementTypeIsConst
263 = is_const< typename boost::intrusive::pointer_traits<MaybeConstPointer>::element_type>::value >
264 struct vector_get_ptr_pointer_to_non_const
266 typedef MaybeConstPointer const_pointer;
267 typedef boost::intrusive::pointer_traits<const_pointer> pointer_traits_t;
268 typedef typename pointer_traits_t::element_type element_type;
269 typedef typename remove_const<element_type>::type non_const_element_type;
270 typedef typename pointer_traits_t
271 ::template rebind_pointer<non_const_element_type>::type return_type;
273 BOOST_CONTAINER_FORCEINLINE static return_type get_ptr(const const_pointer &ptr) BOOST_NOEXCEPT_OR_NOTHROW
274 { return boost::intrusive::pointer_traits<return_type>::const_cast_from(ptr); }
277 template<class Pointer>
278 struct vector_get_ptr_pointer_to_non_const<Pointer, false>
280 typedef const Pointer & return_type;
281 BOOST_CONTAINER_FORCEINLINE static return_type get_ptr(const Pointer &ptr) BOOST_NOEXCEPT_OR_NOTHROW
285 } //namespace container_detail {
287 template<class MaybeConstPointer>
288 BOOST_CONTAINER_FORCEINLINE typename container_detail::vector_get_ptr_pointer_to_non_const<MaybeConstPointer>::return_type
289 vector_iterator_get_ptr(const MaybeConstPointer &ptr) BOOST_NOEXCEPT_OR_NOTHROW
291 return container_detail::vector_get_ptr_pointer_to_non_const<MaybeConstPointer>::get_ptr(ptr);
294 namespace container_detail {
296 #endif //#ifndef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
298 struct uninitialized_size_t {};
299 static const uninitialized_size_t uninitialized_size = uninitialized_size_t();
302 struct vector_value_traits_base
304 static const bool trivial_dctr = is_trivially_destructible<T>::value;
305 static const bool trivial_dctr_after_move = has_trivial_destructor_after_move<T>::value;
306 static const bool trivial_copy = is_trivially_copy_constructible<T>::value;
307 static const bool nothrow_copy = is_nothrow_copy_constructible<T>::value || trivial_copy;
308 static const bool trivial_assign = is_trivially_copy_assignable<T>::value;
309 static const bool nothrow_assign = is_nothrow_copy_assignable<T>::value || trivial_assign;
313 template <class Allocator>
314 struct vector_value_traits
315 : public vector_value_traits_base<typename Allocator::value_type>
317 typedef vector_value_traits_base<typename Allocator::value_type> base_t;
318 //This is the anti-exception array destructor
319 //to deallocate values already constructed
320 typedef typename container_detail::if_c
321 <base_t::trivial_dctr
322 ,container_detail::null_scoped_destructor_n<Allocator>
323 ,container_detail::scoped_destructor_n<Allocator>
324 >::type ArrayDestructor;
325 //This is the anti-exception array deallocator
326 typedef container_detail::scoped_array_deallocator<Allocator> ArrayDeallocator;
329 //!This struct deallocates and allocated memory
330 template < class Allocator
331 , class AllocatorVersion = typename container_detail::version<Allocator>::type
333 struct vector_alloc_holder
337 BOOST_MOVABLE_BUT_NOT_COPYABLE(vector_alloc_holder)
340 typedef Allocator allocator_type;
341 typedef boost::container::allocator_traits<Allocator> allocator_traits_type;
342 typedef typename allocator_traits_type::pointer pointer;
343 typedef typename allocator_traits_type::size_type size_type;
344 typedef typename allocator_traits_type::value_type value_type;
346 static bool is_propagable_from(const allocator_type &from_alloc, pointer p, const allocator_type &to_alloc, bool const propagate_allocator)
348 (void)propagate_allocator; (void)p; (void)to_alloc; (void)from_alloc;
349 const bool all_storage_propagable = !allocator_traits_type::is_partially_propagable::value ||
350 !allocator_traits_type::storage_is_unpropagable(from_alloc, p);
351 return all_storage_propagable && (propagate_allocator || allocator_traits_type::equal(from_alloc, to_alloc));
354 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)
356 (void)propagate_allocator; (void)l_p; (void)r_p; (void)l_a; (void)r_a;
357 const bool all_storage_propagable = !allocator_traits_type::is_partially_propagable::value ||
358 !(allocator_traits_type::storage_is_unpropagable(l_a, l_p) || allocator_traits_type::storage_is_unpropagable(r_a, r_p));
359 return all_storage_propagable && (propagate_allocator || allocator_traits_type::equal(l_a, r_a));
362 //Constructor, does not throw
363 vector_alloc_holder()
364 BOOST_NOEXCEPT_IF(container_detail::is_nothrow_default_constructible<Allocator>::value)
365 : Allocator(), m_start(), m_size(), m_capacity()
368 //Constructor, does not throw
369 template<class AllocConvertible>
370 explicit vector_alloc_holder(BOOST_FWD_REF(AllocConvertible) a) BOOST_NOEXCEPT_OR_NOTHROW
371 : Allocator(boost::forward<AllocConvertible>(a)), m_start(), m_size(), m_capacity()
374 //Constructor, does not throw
375 template<class AllocConvertible>
376 vector_alloc_holder(uninitialized_size_t, BOOST_FWD_REF(AllocConvertible) a, size_type initial_size)
377 : Allocator(boost::forward<AllocConvertible>(a))
379 , m_size(initial_size) //Size is initialized here so vector should only call uninitialized_xxx after this
383 pointer reuse = pointer();
384 m_start = this->allocation_command(allocate_new, initial_size, m_capacity = initial_size, reuse);
388 //Constructor, does not throw
389 vector_alloc_holder(uninitialized_size_t, size_type initial_size)
392 , m_size(initial_size) //Size is initialized here so vector should only call uninitialized_xxx after this
396 pointer reuse = pointer();
397 m_start = this->allocation_command(allocate_new, initial_size, m_capacity = initial_size, reuse);
401 vector_alloc_holder(BOOST_RV_REF(vector_alloc_holder) holder) BOOST_NOEXCEPT_OR_NOTHROW
402 : Allocator(BOOST_MOVE_BASE(Allocator, holder))
403 , m_start(holder.m_start)
404 , m_size(holder.m_size)
405 , m_capacity(holder.m_capacity)
407 holder.m_start = pointer();
408 holder.m_size = holder.m_capacity = 0;
411 vector_alloc_holder(pointer p, size_type capacity, BOOST_RV_REF(vector_alloc_holder) holder)
412 : Allocator(BOOST_MOVE_BASE(Allocator, holder))
414 , m_size(holder.m_size)
415 , m_capacity(capacity)
417 allocator_type &this_alloc = this->alloc();
418 allocator_type &x_alloc = holder.alloc();
419 if(this->is_propagable_from(x_alloc, holder.start(), this_alloc, true)){
420 if(this->m_capacity){
421 this->alloc().deallocate(this->m_start, this->m_capacity);
423 m_start = holder.m_start;
424 m_capacity = holder.m_capacity;
425 holder.m_start = pointer();
426 holder.m_capacity = holder.m_size = 0;
428 else if(this->m_capacity < holder.m_size){
429 size_type const n = holder.m_size;
430 pointer reuse = pointer();
431 m_start = this->allocation_command(allocate_new, n, m_capacity = n, reuse);
432 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
433 this->num_alloc += n != 0;
438 vector_alloc_holder(pointer p, size_type n)
439 BOOST_NOEXCEPT_IF(container_detail::is_nothrow_default_constructible<Allocator>::value)
446 template<class AllocFwd>
447 vector_alloc_holder(pointer p, size_type n, BOOST_FWD_REF(AllocFwd) a)
448 : Allocator(::boost::forward<AllocFwd>(a))
454 BOOST_CONTAINER_FORCEINLINE ~vector_alloc_holder() BOOST_NOEXCEPT_OR_NOTHROW
456 if(this->m_capacity){
457 this->alloc().deallocate(this->m_start, this->m_capacity);
461 BOOST_CONTAINER_FORCEINLINE pointer allocation_command(boost::container::allocation_type command,
462 size_type limit_size, size_type &prefer_in_recvd_out_size, pointer &reuse)
464 typedef typename container_detail::version<Allocator>::type alloc_version;
465 return this->priv_allocation_command(alloc_version(), command, limit_size, prefer_in_recvd_out_size, reuse);
468 bool try_expand_fwd(size_type at_least)
470 //There is not enough memory, try to expand the old one
471 const size_type new_cap = this->capacity() + at_least;
472 size_type real_cap = new_cap;
473 pointer reuse = this->start();
474 bool const success = !!this->allocation_command(expand_fwd, new_cap, real_cap, reuse);
475 //Check for forward expansion
477 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
478 ++this->num_expand_fwd;
480 this->capacity(real_cap);
485 BOOST_CONTAINER_FORCEINLINE size_type next_capacity(size_type additional_objects) const
487 return next_capacity_calculator
488 <size_type, NextCapacityDouble //NextCapacity60Percent
489 >::get( allocator_traits_type::max_size(this->alloc())
490 , this->m_capacity, additional_objects );
495 size_type m_capacity;
497 void swap_resources(vector_alloc_holder &x) BOOST_NOEXCEPT_OR_NOTHROW
499 boost::adl_move_swap(this->m_start, x.m_start);
500 boost::adl_move_swap(this->m_size, x.m_size);
501 boost::adl_move_swap(this->m_capacity, x.m_capacity);
504 void steal_resources(vector_alloc_holder &x) BOOST_NOEXCEPT_OR_NOTHROW
506 this->m_start = x.m_start;
507 this->m_size = x.m_size;
508 this->m_capacity = x.m_capacity;
509 x.m_start = pointer();
510 x.m_size = x.m_capacity = 0;
513 BOOST_CONTAINER_FORCEINLINE Allocator &alloc() BOOST_NOEXCEPT_OR_NOTHROW
516 BOOST_CONTAINER_FORCEINLINE const Allocator &alloc() const BOOST_NOEXCEPT_OR_NOTHROW
519 const pointer &start() const BOOST_NOEXCEPT_OR_NOTHROW { return m_start; }
520 const size_type &capacity() const BOOST_NOEXCEPT_OR_NOTHROW { return m_capacity; }
521 void start(const pointer &p) BOOST_NOEXCEPT_OR_NOTHROW { m_start = p; }
522 void capacity(const size_type &c) BOOST_NOEXCEPT_OR_NOTHROW { m_capacity = c; }
525 void priv_first_allocation(size_type cap)
528 pointer reuse = pointer();
529 m_start = this->allocation_command(allocate_new, cap, cap, reuse);
531 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
537 BOOST_CONTAINER_FORCEINLINE pointer priv_allocation_command(version_1, boost::container::allocation_type command,
539 size_type &prefer_in_recvd_out_size,
543 BOOST_ASSERT( (command & allocate_new));
544 BOOST_ASSERT(!(command & nothrow_allocation));
545 pointer const p = allocator_traits_type::allocate(this->alloc(), prefer_in_recvd_out_size, reuse);
550 pointer priv_allocation_command(version_2, boost::container::allocation_type command,
551 size_type limit_size,
552 size_type &prefer_in_recvd_out_size,
555 return this->alloc().allocation_command(command, limit_size, prefer_in_recvd_out_size, reuse);
559 //!This struct deallocates and allocated memory
560 template <class Allocator>
561 struct vector_alloc_holder<Allocator, version_0>
565 BOOST_MOVABLE_BUT_NOT_COPYABLE(vector_alloc_holder)
568 typedef boost::container::allocator_traits<Allocator> allocator_traits_type;
569 typedef typename allocator_traits_type::pointer pointer;
570 typedef typename allocator_traits_type::size_type size_type;
571 typedef typename allocator_traits_type::value_type value_type;
573 template <class OtherAllocator, class OtherAllocatorVersion>
574 friend struct vector_alloc_holder;
576 //Constructor, does not throw
577 vector_alloc_holder()
578 BOOST_NOEXCEPT_IF(container_detail::is_nothrow_default_constructible<Allocator>::value)
579 : Allocator(), m_size()
582 //Constructor, does not throw
583 template<class AllocConvertible>
584 explicit vector_alloc_holder(BOOST_FWD_REF(AllocConvertible) a) BOOST_NOEXCEPT_OR_NOTHROW
585 : Allocator(boost::forward<AllocConvertible>(a)), m_size()
588 //Constructor, does not throw
589 template<class AllocConvertible>
590 vector_alloc_holder(uninitialized_size_t, BOOST_FWD_REF(AllocConvertible) a, size_type initial_size)
591 : Allocator(boost::forward<AllocConvertible>(a))
592 , m_size(initial_size) //Size is initialized here...
594 //... and capacity here, so vector, must call uninitialized_xxx in the derived constructor
595 this->priv_first_allocation(initial_size);
598 //Constructor, does not throw
599 vector_alloc_holder(uninitialized_size_t, size_type initial_size)
601 , m_size(initial_size) //Size is initialized here...
603 //... and capacity here, so vector, must call uninitialized_xxx in the derived constructor
604 this->priv_first_allocation(initial_size);
607 vector_alloc_holder(BOOST_RV_REF(vector_alloc_holder) holder)
608 : Allocator(BOOST_MOVE_BASE(Allocator, holder))
609 , m_size(holder.m_size) //Size is initialized here so vector should only call uninitialized_xxx after this
611 ::boost::container::uninitialized_move_alloc_n
612 (this->alloc(), container_detail::to_raw_pointer(holder.start()), m_size, container_detail::to_raw_pointer(this->start()));
615 template<class OtherAllocator, class OtherAllocatorVersion>
616 vector_alloc_holder(BOOST_RV_REF_BEG vector_alloc_holder<OtherAllocator, OtherAllocatorVersion> BOOST_RV_REF_END holder)
618 , m_size(holder.m_size) //Initialize it to m_size as first_allocation can only succeed or abort
620 //Different allocator type so we must check we have enough storage
621 const size_type n = holder.m_size;
622 this->priv_first_allocation(n);
623 ::boost::container::uninitialized_move_alloc_n
624 (this->alloc(), container_detail::to_raw_pointer(holder.start()), n, container_detail::to_raw_pointer(this->start()));
627 BOOST_CONTAINER_FORCEINLINE void priv_first_allocation(size_type cap)
629 if(cap > Allocator::internal_capacity){
634 BOOST_CONTAINER_FORCEINLINE void deep_swap(vector_alloc_holder &x)
636 this->priv_deep_swap(x);
639 template<class OtherAllocator, class OtherAllocatorVersion>
640 void deep_swap(vector_alloc_holder<OtherAllocator, OtherAllocatorVersion> &x)
642 if(this->m_size > OtherAllocator::internal_capacity || x.m_size > Allocator::internal_capacity){
645 this->priv_deep_swap(x);
648 BOOST_CONTAINER_FORCEINLINE void swap_resources(vector_alloc_holder &) BOOST_NOEXCEPT_OR_NOTHROW
649 { //Containers with version 0 allocators can't be moved without moving elements one by one
654 BOOST_CONTAINER_FORCEINLINE void steal_resources(vector_alloc_holder &)
655 { //Containers with version 0 allocators can't be moved without moving elements one by one
659 BOOST_CONTAINER_FORCEINLINE Allocator &alloc() BOOST_NOEXCEPT_OR_NOTHROW
662 BOOST_CONTAINER_FORCEINLINE const Allocator &alloc() const BOOST_NOEXCEPT_OR_NOTHROW
665 BOOST_CONTAINER_FORCEINLINE bool try_expand_fwd(size_type at_least)
666 { return !at_least; }
668 BOOST_CONTAINER_FORCEINLINE pointer start() const BOOST_NOEXCEPT_OR_NOTHROW { return Allocator::internal_storage(); }
669 BOOST_CONTAINER_FORCEINLINE size_type capacity() const BOOST_NOEXCEPT_OR_NOTHROW { return Allocator::internal_capacity; }
674 template<class OtherAllocator, class OtherAllocatorVersion>
675 void priv_deep_swap(vector_alloc_holder<OtherAllocator, OtherAllocatorVersion> &x)
677 const size_type MaxTmpStorage = sizeof(value_type)*Allocator::internal_capacity;
678 value_type *const first_this = container_detail::to_raw_pointer(this->start());
679 value_type *const first_x = container_detail::to_raw_pointer(x.start());
681 if(this->m_size < x.m_size){
682 boost::container::deep_swap_alloc_n<MaxTmpStorage>(this->alloc(), first_this, this->m_size, first_x, x.m_size);
685 boost::container::deep_swap_alloc_n<MaxTmpStorage>(this->alloc(), first_x, x.m_size, first_this, this->m_size);
687 boost::adl_move_swap(this->m_size, x.m_size);
691 } //namespace container_detail {
693 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
695 //! A vector is a sequence that supports random access to elements, constant
696 //! time insertion and removal of elements at the end, and linear time insertion
697 //! and removal of elements at the beginning or in the middle. The number of
698 //! elements in a vector may vary dynamically; memory management is automatic.
700 //! \tparam T The type of object that is stored in the vector
701 //! \tparam Allocator The allocator used for all internal memory management
702 template <class T, class Allocator BOOST_CONTAINER_DOCONLY(= new_allocator<T>) >
705 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
709 typedef typename boost::container::allocator_traits<Allocator>::value_type value_type;
710 bool operator()(const value_type &a, const value_type &b) const
714 typedef typename container_detail::version<Allocator>::type alloc_version;
715 typedef boost::container::container_detail::vector_alloc_holder<Allocator> alloc_holder_t;
716 alloc_holder_t m_holder;
717 typedef allocator_traits<Allocator> allocator_traits_type;
718 template <class U, class UAllocator>
721 typedef typename allocator_traits_type::pointer pointer_impl;
722 typedef container_detail::vec_iterator<pointer_impl, false> iterator_impl;
723 typedef container_detail::vec_iterator<pointer_impl, true > const_iterator_impl;
726 static bool is_propagable_from(const Allocator &from_alloc, pointer_impl p, const Allocator &to_alloc, bool const propagate_allocator)
727 { return alloc_holder_t::is_propagable_from(from_alloc, p, to_alloc, propagate_allocator); }
729 static bool are_swap_propagable( const Allocator &l_a, pointer_impl l_p
730 , const Allocator &r_a, pointer_impl r_p, bool const propagate_allocator)
731 { return alloc_holder_t::are_swap_propagable(l_a, l_p, r_a, r_p, propagate_allocator); }
733 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
735 //////////////////////////////////////////////
739 //////////////////////////////////////////////
741 typedef T value_type;
742 typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
743 typedef typename ::boost::container::allocator_traits<Allocator>::const_pointer const_pointer;
744 typedef typename ::boost::container::allocator_traits<Allocator>::reference reference;
745 typedef typename ::boost::container::allocator_traits<Allocator>::const_reference const_reference;
746 typedef typename ::boost::container::allocator_traits<Allocator>::size_type size_type;
747 typedef typename ::boost::container::allocator_traits<Allocator>::difference_type difference_type;
748 typedef Allocator allocator_type;
749 typedef Allocator stored_allocator_type;
750 #if defined BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
751 typedef BOOST_CONTAINER_IMPDEF(pointer) iterator;
752 typedef BOOST_CONTAINER_IMPDEF(const_pointer) const_iterator;
754 typedef BOOST_CONTAINER_IMPDEF(iterator_impl) iterator;
755 typedef BOOST_CONTAINER_IMPDEF(const_iterator_impl) const_iterator;
757 typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<iterator>) reverse_iterator;
758 typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<const_iterator>) const_reverse_iterator;
760 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
762 BOOST_COPYABLE_AND_MOVABLE(vector)
763 typedef container_detail::vector_value_traits<Allocator> value_traits;
764 typedef constant_iterator<T, difference_type> cvalue_iterator;
768 BOOST_CONTAINER_FORCEINLINE void steal_resources(vector &x)
769 { return this->m_holder.steal_resources(x.m_holder); }
771 struct initial_capacity_t{};
772 template<class AllocFwd>
773 BOOST_CONTAINER_FORCEINLINE vector(initial_capacity_t, pointer initial_memory, size_type capacity, BOOST_FWD_REF(AllocFwd) a)
774 : m_holder(initial_memory, capacity, ::boost::forward<AllocFwd>(a))
777 BOOST_CONTAINER_FORCEINLINE vector(initial_capacity_t, pointer initial_memory, size_type capacity)
778 : m_holder(initial_memory, capacity)
781 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
784 //////////////////////////////////////////////
786 // construct/copy/destroy
788 //////////////////////////////////////////////
790 //! <b>Effects</b>: Constructs a vector taking the allocator as parameter.
792 //! <b>Throws</b>: Nothing.
794 //! <b>Complexity</b>: Constant.
795 vector() BOOST_NOEXCEPT_IF(container_detail::is_nothrow_default_constructible<Allocator>::value)
799 //! <b>Effects</b>: Constructs a vector taking the allocator as parameter.
801 //! <b>Throws</b>: Nothing
803 //! <b>Complexity</b>: Constant.
804 explicit vector(const allocator_type& a) BOOST_NOEXCEPT_OR_NOTHROW
808 //! <b>Effects</b>: Constructs a vector and inserts n value initialized values.
810 //! <b>Throws</b>: If allocator_type's allocation
811 //! throws or T's value initialization throws.
813 //! <b>Complexity</b>: Linear to n.
814 explicit vector(size_type n)
815 : m_holder(container_detail::uninitialized_size, n)
817 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
818 this->num_alloc += n != 0;
820 boost::container::uninitialized_value_init_alloc_n
821 (this->m_holder.alloc(), n, this->priv_raw_begin());
824 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
825 //! and inserts n value initialized values.
827 //! <b>Throws</b>: If allocator_type's allocation
828 //! throws or T's value initialization throws.
830 //! <b>Complexity</b>: Linear to n.
831 explicit vector(size_type n, const allocator_type &a)
832 : m_holder(container_detail::uninitialized_size, a, n)
834 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
835 this->num_alloc += n != 0;
837 boost::container::uninitialized_value_init_alloc_n
838 (this->m_holder.alloc(), n, this->priv_raw_begin());
841 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
842 //! and inserts n default initialized values.
844 //! <b>Throws</b>: If allocator_type's allocation
845 //! throws or T's default initialization throws.
847 //! <b>Complexity</b>: Linear to n.
849 //! <b>Note</b>: Non-standard extension
850 vector(size_type n, default_init_t)
851 : m_holder(container_detail::uninitialized_size, n)
853 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
854 this->num_alloc += n != 0;
856 boost::container::uninitialized_default_init_alloc_n
857 (this->m_holder.alloc(), n, this->priv_raw_begin());
860 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
861 //! and inserts n default initialized values.
863 //! <b>Throws</b>: If allocator_type's allocation
864 //! throws or T's default initialization throws.
866 //! <b>Complexity</b>: Linear to n.
868 //! <b>Note</b>: Non-standard extension
869 vector(size_type n, default_init_t, const allocator_type &a)
870 : m_holder(container_detail::uninitialized_size, a, n)
872 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
873 this->num_alloc += n != 0;
875 boost::container::uninitialized_default_init_alloc_n
876 (this->m_holder.alloc(), n, this->priv_raw_begin());
879 //! <b>Effects</b>: Constructs a vector
880 //! and inserts n copies of value.
882 //! <b>Throws</b>: If allocator_type's allocation
883 //! throws or T's copy constructor throws.
885 //! <b>Complexity</b>: Linear to n.
886 vector(size_type n, const T& value)
887 : m_holder(container_detail::uninitialized_size, n)
889 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
890 this->num_alloc += n != 0;
892 boost::container::uninitialized_fill_alloc_n
893 (this->m_holder.alloc(), value, n, this->priv_raw_begin());
896 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
897 //! and inserts n copies of value.
899 //! <b>Throws</b>: If allocation
900 //! throws or T's copy constructor throws.
902 //! <b>Complexity</b>: Linear to n.
903 vector(size_type n, const T& value, const allocator_type& a)
904 : m_holder(container_detail::uninitialized_size, a, n)
906 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
907 this->num_alloc += n != 0;
909 boost::container::uninitialized_fill_alloc_n
910 (this->m_holder.alloc(), value, n, this->priv_raw_begin());
913 //! <b>Effects</b>: Constructs a vector
914 //! and inserts a copy of the range [first, last) in the vector.
916 //! <b>Throws</b>: If allocator_type's allocation
917 //! throws or T's constructor taking a dereferenced InIt throws.
919 //! <b>Complexity</b>: Linear to the range [first, last).
920 template <class InIt>
921 vector(InIt first, InIt last
922 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename container_detail::disable_if_c
923 < container_detail::is_convertible<InIt BOOST_MOVE_I size_type>::value
924 BOOST_MOVE_I container_detail::nat >::type * = 0)
927 { this->assign(first, last); }
929 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
930 //! and inserts a copy of the range [first, last) in the vector.
932 //! <b>Throws</b>: If allocator_type's allocation
933 //! throws or T's constructor taking a dereferenced InIt throws.
935 //! <b>Complexity</b>: Linear to the range [first, last).
936 template <class InIt>
937 vector(InIt first, InIt last, const allocator_type& a
938 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename container_detail::disable_if_c
939 < container_detail::is_convertible<InIt BOOST_MOVE_I size_type>::value
940 BOOST_MOVE_I container_detail::nat >::type * = 0)
943 { this->assign(first, last); }
945 //! <b>Effects</b>: Copy constructs a vector.
947 //! <b>Postcondition</b>: x == *this.
949 //! <b>Throws</b>: If allocator_type's allocation
950 //! throws or T's copy constructor throws.
952 //! <b>Complexity</b>: Linear to the elements x contains.
953 vector(const vector &x)
954 : m_holder( container_detail::uninitialized_size
955 , allocator_traits_type::select_on_container_copy_construction(x.m_holder.alloc())
958 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
959 this->num_alloc += x.size() != 0;
961 ::boost::container::uninitialized_copy_alloc_n
962 ( this->m_holder.alloc(), x.priv_raw_begin()
963 , x.size(), this->priv_raw_begin());
966 //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
968 //! <b>Throws</b>: Nothing
970 //! <b>Complexity</b>: Constant.
971 vector(BOOST_RV_REF(vector) x) BOOST_NOEXCEPT_OR_NOTHROW
972 : m_holder(boost::move(x.m_holder))
973 { BOOST_STATIC_ASSERT((!allocator_traits_type::is_partially_propagable::value)); }
975 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
976 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
977 //! and inserts a copy of the range [il.begin(), il.last()) in the vector
979 //! <b>Throws</b>: If T's constructor taking a dereferenced initializer_list iterator throws.
981 //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
982 vector(std::initializer_list<value_type> il, const allocator_type& a = allocator_type())
985 this->assign(il.begin(), il.end());
989 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
991 //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
993 //! <b>Throws</b>: If T's move constructor or allocation throws
995 //! <b>Complexity</b>: Linear.
997 //! <b>Note</b>: Non-standard extension to support static_vector
998 template<class OtherAllocator>
999 vector(BOOST_RV_REF_BEG vector<T, OtherAllocator> BOOST_RV_REF_END x
1000 , typename container_detail::enable_if_c
1001 < container_detail::is_version<OtherAllocator, 0>::value>::type * = 0
1003 : m_holder(boost::move(x.m_holder))
1006 #endif //!defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1008 //! <b>Effects</b>: Copy constructs a vector using the specified allocator.
1010 //! <b>Postcondition</b>: x == *this.
1012 //! <b>Throws</b>: If allocation
1013 //! throws or T's copy constructor throws.
1015 //! <b>Complexity</b>: Linear to the elements x contains.
1016 vector(const vector &x, const allocator_type &a)
1017 : m_holder(container_detail::uninitialized_size, a, x.size())
1019 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
1020 this->num_alloc += x.size() != 0;
1022 ::boost::container::uninitialized_copy_alloc_n_source
1023 ( this->m_holder.alloc(), x.priv_raw_begin()
1024 , x.size(), this->priv_raw_begin());
1027 //! <b>Effects</b>: Move constructor using the specified allocator.
1028 //! Moves x's resources to *this if a == allocator_type().
1029 //! Otherwise copies values from x to *this.
1031 //! <b>Throws</b>: If allocation or T's copy constructor throws.
1033 //! <b>Complexity</b>: Constant if a == x.get_allocator(), linear otherwise.
1034 vector(BOOST_RV_REF(vector) x, const allocator_type &a)
1035 : m_holder( container_detail::uninitialized_size, a
1036 , is_propagable_from(x.get_stored_allocator(), x.m_holder.start(), a, true) ? 0 : x.size()
1039 if(is_propagable_from(x.get_stored_allocator(), x.m_holder.start(), a, true)){
1040 this->m_holder.steal_resources(x.m_holder);
1043 const size_type n = x.size();
1044 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
1045 this->num_alloc += n != 0;
1047 ::boost::container::uninitialized_move_alloc_n_source
1048 ( this->m_holder.alloc(), x.priv_raw_begin()
1049 , n, this->priv_raw_begin());
1053 //! <b>Effects</b>: Destroys the vector. All stored values are destroyed
1054 //! and used memory is deallocated.
1056 //! <b>Throws</b>: Nothing.
1058 //! <b>Complexity</b>: Linear to the number of elements.
1059 ~vector() BOOST_NOEXCEPT_OR_NOTHROW
1061 boost::container::destroy_alloc_n
1062 (this->get_stored_allocator(), this->priv_raw_begin(), this->m_holder.m_size);
1063 //vector_alloc_holder deallocates the data
1066 //! <b>Effects</b>: Makes *this contain the same elements as x.
1068 //! <b>Postcondition</b>: this->size() == x.size(). *this contains a copy
1069 //! of each of x's elements.
1071 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment throws.
1073 //! <b>Complexity</b>: Linear to the number of elements in x.
1074 BOOST_CONTAINER_FORCEINLINE vector& operator=(BOOST_COPY_ASSIGN_REF(vector) x)
1077 this->priv_copy_assign(x);
1082 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1083 //! <b>Effects</b>: Make *this container contains elements from il.
1085 //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
1086 BOOST_CONTAINER_FORCEINLINE vector& operator=(std::initializer_list<value_type> il)
1088 this->assign(il.begin(), il.end());
1093 //! <b>Effects</b>: Move assignment. All x's values are transferred to *this.
1095 //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
1096 //! before the function.
1098 //! <b>Throws</b>: If allocator_traits_type::propagate_on_container_move_assignment
1099 //! is false and (allocation throws or value_type's move constructor throws)
1101 //! <b>Complexity</b>: Constant if allocator_traits_type::
1102 //! propagate_on_container_move_assignment is true or
1103 //! this->get>allocator() == x.get_allocator(). Linear otherwise.
1104 BOOST_CONTAINER_FORCEINLINE vector& operator=(BOOST_RV_REF(vector) x)
1105 BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value
1106 || allocator_traits_type::is_always_equal::value)
1108 BOOST_ASSERT(&x != this);
1109 this->priv_move_assign(boost::move(x));
1113 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1115 //! <b>Effects</b>: Move assignment. All x's values are transferred to *this.
1117 //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
1118 //! before the function.
1120 //! <b>Throws</b>: If move constructor/assignment of T throws or allocation throws
1122 //! <b>Complexity</b>: Linear.
1124 //! <b>Note</b>: Non-standard extension to support static_vector
1125 template<class OtherAllocator>
1126 BOOST_CONTAINER_FORCEINLINE typename container_detail::enable_if_and
1128 , container_detail::is_version<OtherAllocator, 0>
1129 , container_detail::is_different<OtherAllocator, allocator_type>
1131 operator=(BOOST_RV_REF_BEG vector<value_type, OtherAllocator> BOOST_RV_REF_END x)
1133 this->priv_move_assign(boost::move(x));
1137 //! <b>Effects</b>: Copy assignment. All x's values are copied to *this.
1139 //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
1140 //! before the function.
1142 //! <b>Throws</b>: If move constructor/assignment of T throws or allocation throws
1144 //! <b>Complexity</b>: Linear.
1146 //! <b>Note</b>: Non-standard extension to support static_vector
1147 template<class OtherAllocator>
1148 BOOST_CONTAINER_FORCEINLINE typename container_detail::enable_if_and
1150 , container_detail::is_version<OtherAllocator, 0>
1151 , container_detail::is_different<OtherAllocator, allocator_type>
1153 operator=(const vector<value_type, OtherAllocator> &x)
1155 this->priv_copy_assign(x);
1161 //! <b>Effects</b>: Assigns the the range [first, last) to *this.
1163 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment or
1164 //! T's constructor/assignment from dereferencing InpIt throws.
1166 //! <b>Complexity</b>: Linear to n.
1167 template <class InIt>
1168 void assign(InIt first, InIt last
1169 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename container_detail::disable_if_or
1171 BOOST_MOVE_I container_detail::is_convertible<InIt BOOST_MOVE_I size_type>
1172 BOOST_MOVE_I container_detail::and_
1173 < container_detail::is_different<alloc_version BOOST_MOVE_I version_0>
1174 BOOST_MOVE_I container_detail::is_not_input_iterator<InIt>
1179 //Overwrite all elements we can from [first, last)
1180 iterator cur = this->begin();
1181 const iterator end_it = this->end();
1182 for ( ; first != last && cur != end_it; ++cur, ++first){
1187 //There are no more elements in the sequence, erase remaining
1188 T* const end_pos = this->priv_raw_end();
1189 const size_type n = static_cast<size_type>(end_pos - container_detail::iterator_to_raw_pointer(cur));
1190 this->priv_destroy_last_n(n);
1193 //There are more elements in the range, insert the remaining ones
1194 this->insert(this->cend(), first, last);
1198 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1199 //! <b>Effects</b>: Assigns the the range [il.begin(), il.end()) to *this.
1201 //! <b>Throws</b>: If memory allocation throws or
1202 //! T's constructor from dereferencing iniializer_list iterator throws.
1204 BOOST_CONTAINER_FORCEINLINE void assign(std::initializer_list<T> il)
1206 this->assign(il.begin(), il.end());
1210 //! <b>Effects</b>: Assigns the the range [first, last) to *this.
1212 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment or
1213 //! T's constructor/assignment from dereferencing InpIt throws.
1215 //! <b>Complexity</b>: Linear to n.
1216 template <class FwdIt>
1217 void assign(FwdIt first, FwdIt last
1218 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename container_detail::disable_if_or
1220 BOOST_MOVE_I container_detail::is_same<alloc_version BOOST_MOVE_I version_0>
1221 BOOST_MOVE_I container_detail::is_convertible<FwdIt BOOST_MOVE_I size_type>
1222 BOOST_MOVE_I container_detail::is_input_iterator<FwdIt>
1226 //For Fwd iterators the standard only requires EmplaceConstructible and assignable from *first
1227 //so we can't do any backwards allocation
1228 const size_type input_sz = static_cast<size_type>(boost::container::iterator_distance(first, last));
1229 const size_type old_capacity = this->capacity();
1230 if(input_sz > old_capacity){ //If input range is too big, we need to reallocate
1231 size_type real_cap = 0;
1232 pointer reuse(this->m_holder.start());
1233 pointer const ret(this->m_holder.allocation_command(allocate_new|expand_fwd, input_sz, real_cap = input_sz, reuse));
1234 if(!reuse){ //New allocation, just emplace new values
1235 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
1238 pointer const old_p = this->m_holder.start();
1240 this->priv_destroy_all();
1241 this->m_holder.alloc().deallocate(old_p, old_capacity);
1243 this->m_holder.start(ret);
1244 this->m_holder.capacity(real_cap);
1245 this->m_holder.m_size = 0;
1246 this->priv_uninitialized_construct_at_end(first, last);
1250 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
1251 ++this->num_expand_fwd;
1253 this->m_holder.capacity(real_cap);
1254 //Forward expansion, use assignment + back deletion/construction that comes later
1257 //Overwrite all elements we can from [first, last)
1258 iterator cur = this->begin();
1259 const iterator end_it = this->end();
1260 for ( ; first != last && cur != end_it; ++cur, ++first){
1265 //There are no more elements in the sequence, erase remaining
1266 this->priv_destroy_last_n(this->size() - input_sz);
1269 //Uninitialized construct at end the remaining range
1270 this->priv_uninitialized_construct_at_end(first, last);
1274 //! <b>Effects</b>: Assigns the n copies of val to *this.
1276 //! <b>Throws</b>: If memory allocation throws or
1277 //! T's copy/move constructor/assignment throws.
1279 //! <b>Complexity</b>: Linear to n.
1280 BOOST_CONTAINER_FORCEINLINE void assign(size_type n, const value_type& val)
1281 { this->assign(cvalue_iterator(val, n), cvalue_iterator()); }
1283 //! <b>Effects</b>: Returns a copy of the internal allocator.
1285 //! <b>Throws</b>: If allocator's copy constructor throws.
1287 //! <b>Complexity</b>: Constant.
1288 allocator_type get_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
1289 { return this->m_holder.alloc(); }
1291 //! <b>Effects</b>: Returns a reference to the internal allocator.
1293 //! <b>Throws</b>: Nothing
1295 //! <b>Complexity</b>: Constant.
1297 //! <b>Note</b>: Non-standard extension.
1298 BOOST_CONTAINER_FORCEINLINE stored_allocator_type &get_stored_allocator() BOOST_NOEXCEPT_OR_NOTHROW
1299 { return this->m_holder.alloc(); }
1301 //! <b>Effects</b>: Returns a reference to the internal allocator.
1303 //! <b>Throws</b>: Nothing
1305 //! <b>Complexity</b>: Constant.
1307 //! <b>Note</b>: Non-standard extension.
1308 BOOST_CONTAINER_FORCEINLINE const stored_allocator_type &get_stored_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
1309 { return this->m_holder.alloc(); }
1311 //////////////////////////////////////////////
1315 //////////////////////////////////////////////
1317 //! <b>Effects</b>: Returns an iterator to the first element contained in the vector.
1319 //! <b>Throws</b>: Nothing.
1321 //! <b>Complexity</b>: Constant.
1322 BOOST_CONTAINER_FORCEINLINE iterator begin() BOOST_NOEXCEPT_OR_NOTHROW
1323 { return iterator(this->m_holder.start()); }
1325 //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
1327 //! <b>Throws</b>: Nothing.
1329 //! <b>Complexity</b>: Constant.
1330 BOOST_CONTAINER_FORCEINLINE const_iterator begin() const BOOST_NOEXCEPT_OR_NOTHROW
1331 { return const_iterator(this->m_holder.start()); }
1333 //! <b>Effects</b>: Returns an iterator to the end of the vector.
1335 //! <b>Throws</b>: Nothing.
1337 //! <b>Complexity</b>: Constant.
1338 BOOST_CONTAINER_FORCEINLINE iterator end() BOOST_NOEXCEPT_OR_NOTHROW
1339 { return iterator(this->m_holder.start() + this->m_holder.m_size); }
1341 //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
1343 //! <b>Throws</b>: Nothing.
1345 //! <b>Complexity</b>: Constant.
1346 BOOST_CONTAINER_FORCEINLINE const_iterator end() const BOOST_NOEXCEPT_OR_NOTHROW
1347 { return this->cend(); }
1349 //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
1350 //! of the reversed vector.
1352 //! <b>Throws</b>: Nothing.
1354 //! <b>Complexity</b>: Constant.
1355 BOOST_CONTAINER_FORCEINLINE reverse_iterator rbegin() BOOST_NOEXCEPT_OR_NOTHROW
1356 { return reverse_iterator(this->end()); }
1358 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
1359 //! of the reversed vector.
1361 //! <b>Throws</b>: Nothing.
1363 //! <b>Complexity</b>: Constant.
1364 BOOST_CONTAINER_FORCEINLINE const_reverse_iterator rbegin() const BOOST_NOEXCEPT_OR_NOTHROW
1365 { return this->crbegin(); }
1367 //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
1368 //! of the reversed vector.
1370 //! <b>Throws</b>: Nothing.
1372 //! <b>Complexity</b>: Constant.
1373 BOOST_CONTAINER_FORCEINLINE reverse_iterator rend() BOOST_NOEXCEPT_OR_NOTHROW
1374 { return reverse_iterator(this->begin()); }
1376 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
1377 //! of the reversed vector.
1379 //! <b>Throws</b>: Nothing.
1381 //! <b>Complexity</b>: Constant.
1382 BOOST_CONTAINER_FORCEINLINE const_reverse_iterator rend() const BOOST_NOEXCEPT_OR_NOTHROW
1383 { return this->crend(); }
1385 //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
1387 //! <b>Throws</b>: Nothing.
1389 //! <b>Complexity</b>: Constant.
1390 BOOST_CONTAINER_FORCEINLINE const_iterator cbegin() const BOOST_NOEXCEPT_OR_NOTHROW
1391 { return const_iterator(this->m_holder.start()); }
1393 //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
1395 //! <b>Throws</b>: Nothing.
1397 //! <b>Complexity</b>: Constant.
1398 BOOST_CONTAINER_FORCEINLINE const_iterator cend() const BOOST_NOEXCEPT_OR_NOTHROW
1399 { return const_iterator(this->m_holder.start() + this->m_holder.m_size); }
1401 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
1402 //! of the reversed vector.
1404 //! <b>Throws</b>: Nothing.
1406 //! <b>Complexity</b>: Constant.
1407 BOOST_CONTAINER_FORCEINLINE const_reverse_iterator crbegin() const BOOST_NOEXCEPT_OR_NOTHROW
1408 { return const_reverse_iterator(this->end());}
1410 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
1411 //! of the reversed vector.
1413 //! <b>Throws</b>: Nothing.
1415 //! <b>Complexity</b>: Constant.
1416 BOOST_CONTAINER_FORCEINLINE const_reverse_iterator crend() const BOOST_NOEXCEPT_OR_NOTHROW
1417 { return const_reverse_iterator(this->begin()); }
1419 //////////////////////////////////////////////
1423 //////////////////////////////////////////////
1425 //! <b>Effects</b>: Returns true if the vector contains no elements.
1427 //! <b>Throws</b>: Nothing.
1429 //! <b>Complexity</b>: Constant.
1430 BOOST_CONTAINER_FORCEINLINE bool empty() const BOOST_NOEXCEPT_OR_NOTHROW
1431 { return !this->m_holder.m_size; }
1433 //! <b>Effects</b>: Returns the number of the elements contained in the vector.
1435 //! <b>Throws</b>: Nothing.
1437 //! <b>Complexity</b>: Constant.
1438 BOOST_CONTAINER_FORCEINLINE size_type size() const BOOST_NOEXCEPT_OR_NOTHROW
1439 { return this->m_holder.m_size; }
1441 //! <b>Effects</b>: Returns the largest possible size of the vector.
1443 //! <b>Throws</b>: Nothing.
1445 //! <b>Complexity</b>: Constant.
1446 BOOST_CONTAINER_FORCEINLINE size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
1447 { return allocator_traits_type::max_size(this->m_holder.alloc()); }
1449 //! <b>Effects</b>: Inserts or erases elements at the end such that
1450 //! the size becomes n. New elements are value initialized.
1452 //! <b>Throws</b>: If memory allocation throws, or T's copy/move or value initialization throws.
1454 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1455 void resize(size_type new_size)
1456 { this->priv_resize(new_size, value_init); }
1458 //! <b>Effects</b>: Inserts or erases elements at the end such that
1459 //! the size becomes n. New elements are default initialized.
1461 //! <b>Throws</b>: If memory allocation throws, or T's copy/move or default initialization throws.
1463 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1465 //! <b>Note</b>: Non-standard extension
1466 void resize(size_type new_size, default_init_t)
1467 { this->priv_resize(new_size, default_init); }
1469 //! <b>Effects</b>: Inserts or erases elements at the end such that
1470 //! the size becomes n. New elements are copy constructed from x.
1472 //! <b>Throws</b>: If memory allocation throws, or T's copy/move constructor throws.
1474 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1475 void resize(size_type new_size, const T& x)
1476 { this->priv_resize(new_size, x); }
1478 //! <b>Effects</b>: Number of elements for which memory has been allocated.
1479 //! capacity() is always greater than or equal to size().
1481 //! <b>Throws</b>: Nothing.
1483 //! <b>Complexity</b>: Constant.
1484 BOOST_CONTAINER_FORCEINLINE size_type capacity() const BOOST_NOEXCEPT_OR_NOTHROW
1485 { return this->m_holder.capacity(); }
1487 //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
1488 //! effect. Otherwise, it is a request for allocation of additional memory.
1489 //! If the request is successful, then capacity() is greater than or equal to
1490 //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
1492 //! <b>Throws</b>: If memory allocation allocation throws or T's copy/move constructor throws.
1493 BOOST_CONTAINER_FORCEINLINE void reserve(size_type new_cap)
1495 if (this->capacity() < new_cap){
1496 this->priv_reserve_no_capacity(new_cap, alloc_version());
1500 //! <b>Effects</b>: Tries to deallocate the excess of memory created
1501 //! with previous allocations. The size of the vector is unchanged
1503 //! <b>Throws</b>: If memory allocation throws, or T's copy/move constructor throws.
1505 //! <b>Complexity</b>: Linear to size().
1506 BOOST_CONTAINER_FORCEINLINE void shrink_to_fit()
1507 { this->priv_shrink_to_fit(alloc_version()); }
1509 //////////////////////////////////////////////
1513 //////////////////////////////////////////////
1515 //! <b>Requires</b>: !empty()
1517 //! <b>Effects</b>: Returns a reference to the first
1518 //! element of the container.
1520 //! <b>Throws</b>: Nothing.
1522 //! <b>Complexity</b>: Constant.
1523 reference front() BOOST_NOEXCEPT_OR_NOTHROW
1525 BOOST_ASSERT(!this->empty());
1526 return *this->m_holder.start();
1529 //! <b>Requires</b>: !empty()
1531 //! <b>Effects</b>: Returns a const reference to the first
1532 //! element of the container.
1534 //! <b>Throws</b>: Nothing.
1536 //! <b>Complexity</b>: Constant.
1537 const_reference front() const 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 reference to the last
1546 //! element of the container.
1548 //! <b>Throws</b>: Nothing.
1550 //! <b>Complexity</b>: Constant.
1551 reference back() BOOST_NOEXCEPT_OR_NOTHROW
1553 BOOST_ASSERT(!this->empty());
1554 return this->m_holder.start()[this->m_holder.m_size - 1];
1557 //! <b>Requires</b>: !empty()
1559 //! <b>Effects</b>: Returns a const reference to the last
1560 //! element of the container.
1562 //! <b>Throws</b>: Nothing.
1564 //! <b>Complexity</b>: Constant.
1565 const_reference back() const 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>: size() > n.
1573 //! <b>Effects</b>: Returns a reference to the nth element
1574 //! from the beginning of the container.
1576 //! <b>Throws</b>: Nothing.
1578 //! <b>Complexity</b>: Constant.
1579 reference operator[](size_type n) BOOST_NOEXCEPT_OR_NOTHROW
1581 BOOST_ASSERT(this->m_holder.m_size > n);
1582 return this->m_holder.start()[n];
1585 //! <b>Requires</b>: size() > n.
1587 //! <b>Effects</b>: Returns a const reference to the nth element
1588 //! from the beginning of the container.
1590 //! <b>Throws</b>: Nothing.
1592 //! <b>Complexity</b>: Constant.
1593 const_reference operator[](size_type n) const 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 an iterator to the nth element
1602 //! from the beginning of the container. Returns end()
1605 //! <b>Throws</b>: Nothing.
1607 //! <b>Complexity</b>: Constant.
1609 //! <b>Note</b>: Non-standard extension
1610 iterator nth(size_type n) BOOST_NOEXCEPT_OR_NOTHROW
1612 BOOST_ASSERT(this->m_holder.m_size >= n);
1613 return iterator(this->m_holder.start()+n);
1616 //! <b>Requires</b>: size() >= n.
1618 //! <b>Effects</b>: Returns a const_iterator to the nth element
1619 //! from the beginning of the container. Returns end()
1622 //! <b>Throws</b>: Nothing.
1624 //! <b>Complexity</b>: Constant.
1626 //! <b>Note</b>: Non-standard extension
1627 const_iterator nth(size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
1629 BOOST_ASSERT(this->m_holder.m_size >= n);
1630 return const_iterator(this->m_holder.start()+n);
1633 //! <b>Requires</b>: begin() <= p <= end().
1635 //! <b>Effects</b>: Returns the index of the element pointed by p
1636 //! and size() if p == end().
1638 //! <b>Throws</b>: Nothing.
1640 //! <b>Complexity</b>: Constant.
1642 //! <b>Note</b>: Non-standard extension
1643 size_type index_of(iterator p) BOOST_NOEXCEPT_OR_NOTHROW
1645 //Range check assert done in priv_index_of
1646 return this->priv_index_of(vector_iterator_get_ptr(p));
1649 //! <b>Requires</b>: begin() <= p <= end().
1651 //! <b>Effects</b>: Returns the index of the element pointed by p
1652 //! and size() if p == end().
1654 //! <b>Throws</b>: Nothing.
1656 //! <b>Complexity</b>: Constant.
1658 //! <b>Note</b>: Non-standard extension
1659 size_type index_of(const_iterator p) const BOOST_NOEXCEPT_OR_NOTHROW
1661 //Range check assert done in priv_index_of
1662 return this->priv_index_of(vector_iterator_get_ptr(p));
1665 //! <b>Requires</b>: size() > n.
1667 //! <b>Effects</b>: Returns a reference to the nth element
1668 //! from the beginning of the container.
1670 //! <b>Throws</b>: std::range_error if n >= size()
1672 //! <b>Complexity</b>: Constant.
1673 reference at(size_type n)
1675 this->priv_throw_if_out_of_range(n);
1676 return this->m_holder.start()[n];
1679 //! <b>Requires</b>: size() > n.
1681 //! <b>Effects</b>: Returns a const 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 const_reference at(size_type n) const
1689 this->priv_throw_if_out_of_range(n);
1690 return this->m_holder.start()[n];
1693 //////////////////////////////////////////////
1697 //////////////////////////////////////////////
1699 //! <b>Returns</b>: A pointer such that [data(),data() + size()) is a valid range.
1700 //! For a non-empty vector, data() == &front().
1702 //! <b>Throws</b>: Nothing.
1704 //! <b>Complexity</b>: Constant.
1705 T* data() BOOST_NOEXCEPT_OR_NOTHROW
1706 { return this->priv_raw_begin(); }
1708 //! <b>Returns</b>: A pointer such that [data(),data() + size()) is a valid range.
1709 //! For a non-empty vector, data() == &front().
1711 //! <b>Throws</b>: Nothing.
1713 //! <b>Complexity</b>: Constant.
1714 const T * data() const BOOST_NOEXCEPT_OR_NOTHROW
1715 { return this->priv_raw_begin(); }
1717 //////////////////////////////////////////////
1721 //////////////////////////////////////////////
1723 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1724 //! <b>Effects</b>: Inserts an object of type T constructed with
1725 //! std::forward<Args>(args)... in the end of the vector.
1727 //! <b>Returns</b>: A reference to the created object.
1729 //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws or
1730 //! T's copy/move constructor throws.
1732 //! <b>Complexity</b>: Amortized constant time.
1733 template<class ...Args>
1734 BOOST_CONTAINER_FORCEINLINE reference emplace_back(BOOST_FWD_REF(Args)...args)
1736 if (BOOST_LIKELY(this->room_enough())){
1737 //There is more memory, just construct a new object at the end
1738 T* const p = this->priv_raw_end();
1739 allocator_traits_type::construct(this->m_holder.alloc(), p, ::boost::forward<Args>(args)...);
1740 ++this->m_holder.m_size;
1744 typedef container_detail::insert_emplace_proxy<Allocator, T*, Args...> type;
1745 return *this->priv_forward_range_insert_no_capacity
1746 (this->back_ptr(), 1, type(::boost::forward<Args>(args)...), alloc_version());
1750 //! <b>Effects</b>: Inserts an object of type T constructed with
1751 //! std::forward<Args>(args)... in the end of the vector.
1753 //! <b>Throws</b>: If the in-place constructor throws.
1755 //! <b>Complexity</b>: Constant time.
1757 //! <b>Note</b>: Non-standard extension.
1758 template<class ...Args>
1759 BOOST_CONTAINER_FORCEINLINE bool stable_emplace_back(BOOST_FWD_REF(Args)...args)
1761 const bool is_room_enough = this->room_enough() || (alloc_version::value == 2 && this->m_holder.try_expand_fwd(1u));
1762 if (BOOST_LIKELY(is_room_enough)){
1763 //There is more memory, just construct a new object at the end
1764 allocator_traits_type::construct(this->m_holder.alloc(), this->priv_raw_end(), ::boost::forward<Args>(args)...);
1765 ++this->m_holder.m_size;
1767 return is_room_enough;
1770 //! <b>Requires</b>: position must be a valid iterator of *this.
1772 //! <b>Effects</b>: Inserts an object of type T constructed with
1773 //! std::forward<Args>(args)... before position
1775 //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws or
1776 //! T's copy/move constructor/assignment throws.
1778 //! <b>Complexity</b>: If position is end(), amortized constant time
1779 //! Linear time otherwise.
1780 template<class ...Args>
1781 iterator emplace(const_iterator position, BOOST_FWD_REF(Args) ...args)
1783 BOOST_ASSERT(this->priv_in_range_or_end(position));
1784 //Just call more general insert(pos, size, value) and return iterator
1785 typedef container_detail::insert_emplace_proxy<Allocator, T*, Args...> type;
1786 return this->priv_forward_range_insert( vector_iterator_get_ptr(position), 1
1787 , type(::boost::forward<Args>(args)...));
1790 #else // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
1792 #define BOOST_CONTAINER_VECTOR_EMPLACE_CODE(N) \
1793 BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
1794 BOOST_CONTAINER_FORCEINLINE reference emplace_back(BOOST_MOVE_UREF##N)\
1796 if (BOOST_LIKELY(this->room_enough())){\
1797 T* const p = this->priv_raw_end();\
1798 allocator_traits_type::construct (this->m_holder.alloc()\
1799 , this->priv_raw_end() BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
1800 ++this->m_holder.m_size;\
1804 typedef container_detail::insert_emplace_proxy_arg##N<Allocator, T* BOOST_MOVE_I##N BOOST_MOVE_TARG##N> type;\
1805 return *this->priv_forward_range_insert_no_capacity\
1806 ( this->back_ptr(), 1, type(BOOST_MOVE_FWD##N), alloc_version());\
1810 BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
1811 BOOST_CONTAINER_FORCEINLINE bool stable_emplace_back(BOOST_MOVE_UREF##N)\
1813 const bool is_room_enough = this->room_enough() || (alloc_version::value == 2 && this->m_holder.try_expand_fwd(1u));\
1814 if (BOOST_LIKELY(is_room_enough)){\
1815 allocator_traits_type::construct (this->m_holder.alloc()\
1816 , this->priv_raw_end() BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
1817 ++this->m_holder.m_size;\
1819 return is_room_enough;\
1822 BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
1823 iterator emplace(const_iterator pos BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
1825 BOOST_ASSERT(this->priv_in_range_or_end(pos));\
1826 typedef container_detail::insert_emplace_proxy_arg##N<Allocator, T* BOOST_MOVE_I##N BOOST_MOVE_TARG##N> type;\
1827 return this->priv_forward_range_insert(vector_iterator_get_ptr(pos), 1, type(BOOST_MOVE_FWD##N));\
1830 BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_VECTOR_EMPLACE_CODE)
1831 #undef BOOST_CONTAINER_VECTOR_EMPLACE_CODE
1835 #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1836 //! <b>Effects</b>: Inserts a copy of x at the end of the vector.
1838 //! <b>Throws</b>: If memory allocation throws or
1839 //! T's copy/move constructor throws.
1841 //! <b>Complexity</b>: Amortized constant time.
1842 void push_back(const T &x);
1844 //! <b>Effects</b>: Constructs a new element in the end of the vector
1845 //! and moves the resources of x to this new element.
1847 //! <b>Throws</b>: If memory allocation throws or
1848 //! T's copy/move constructor throws.
1850 //! <b>Complexity</b>: Amortized constant time.
1851 void push_back(T &&x);
1853 BOOST_MOVE_CONVERSION_AWARE_CATCH(push_back, T, void, priv_push_back)
1856 #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1857 //! <b>Requires</b>: position must be a valid iterator of *this.
1859 //! <b>Effects</b>: Insert a copy of x before position.
1861 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment throws.
1863 //! <b>Complexity</b>: If position is end(), amortized constant time
1864 //! Linear time otherwise.
1865 iterator insert(const_iterator position, const T &x);
1867 //! <b>Requires</b>: position must be a valid iterator of *this.
1869 //! <b>Effects</b>: Insert a new element before position with x's resources.
1871 //! <b>Throws</b>: If memory allocation throws.
1873 //! <b>Complexity</b>: If position is end(), amortized constant time
1874 //! Linear time otherwise.
1875 iterator insert(const_iterator position, T &&x);
1877 BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, T, iterator, priv_insert, const_iterator, const_iterator)
1880 //! <b>Requires</b>: p must be a valid iterator of *this.
1882 //! <b>Effects</b>: Insert n copies of x before pos.
1884 //! <b>Returns</b>: an iterator to the first inserted element or p if n is 0.
1886 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor throws.
1888 //! <b>Complexity</b>: Linear to n.
1889 iterator insert(const_iterator p, size_type n, const T& x)
1891 BOOST_ASSERT(this->priv_in_range_or_end(p));
1892 container_detail::insert_n_copies_proxy<Allocator, T*> proxy(x);
1893 return this->priv_forward_range_insert(vector_iterator_get_ptr(p), n, proxy);
1896 //! <b>Requires</b>: p must be a valid iterator of *this.
1898 //! <b>Effects</b>: Insert a copy of the [first, last) range before pos.
1900 //! <b>Returns</b>: an iterator to the first inserted element or pos if first == last.
1902 //! <b>Throws</b>: If memory allocation throws, T's constructor from a
1903 //! dereferenced InpIt throws or T's copy/move constructor/assignment throws.
1905 //! <b>Complexity</b>: Linear to boost::container::iterator_distance [first, last).
1906 template <class InIt>
1907 iterator insert(const_iterator pos, InIt first, InIt last
1908 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1909 , typename container_detail::disable_if_or
1911 , container_detail::is_convertible<InIt, size_type>
1912 , container_detail::is_not_input_iterator<InIt>
1917 BOOST_ASSERT(this->priv_in_range_or_end(pos));
1918 const size_type n_pos = pos - this->cbegin();
1919 iterator it(vector_iterator_get_ptr(pos));
1920 for(;first != last; ++first){
1921 it = this->emplace(it, *first);
1924 return iterator(this->m_holder.start() + n_pos);
1927 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1928 template <class FwdIt>
1929 iterator insert(const_iterator pos, FwdIt first, FwdIt last
1930 , typename container_detail::disable_if_or
1932 , container_detail::is_convertible<FwdIt, size_type>
1933 , container_detail::is_input_iterator<FwdIt>
1937 BOOST_ASSERT(this->priv_in_range_or_end(pos));
1938 container_detail::insert_range_proxy<Allocator, FwdIt, T*> proxy(first);
1939 return this->priv_forward_range_insert(vector_iterator_get_ptr(pos), boost::container::iterator_distance(first, last), proxy);
1943 //! <b>Requires</b>: p must be a valid iterator of *this. num, must
1944 //! be equal to boost::container::iterator_distance(first, last)
1946 //! <b>Effects</b>: Insert a copy of the [first, last) range before pos.
1948 //! <b>Returns</b>: an iterator to the first inserted element or pos if first == last.
1950 //! <b>Throws</b>: If memory allocation throws, T's constructor from a
1951 //! dereferenced InpIt throws or T's copy/move constructor/assignment throws.
1953 //! <b>Complexity</b>: Linear to boost::container::iterator_distance [first, last).
1955 //! <b>Note</b>: This function avoids a linear operation to calculate boost::container::iterator_distance[first, last)
1956 //! for forward and bidirectional iterators, and a one by one insertion for input iterators. This is a
1957 //! a non-standard extension.
1958 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1959 template <class InIt>
1960 iterator insert(const_iterator pos, size_type num, InIt first, InIt last)
1962 BOOST_ASSERT(this->priv_in_range_or_end(pos));
1963 BOOST_ASSERT(container_detail::is_input_iterator<InIt>::value ||
1964 num == static_cast<size_type>(boost::container::iterator_distance(first, last)));
1966 container_detail::insert_range_proxy<Allocator, InIt, T*> proxy(first);
1967 return this->priv_forward_range_insert(vector_iterator_get_ptr(pos), num, proxy);
1971 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1972 //! <b>Requires</b>: position must be a valid iterator of *this.
1974 //! <b>Effects</b>: Insert a copy of the [il.begin(), il.end()) range before position.
1976 //! <b>Returns</b>: an iterator to the first inserted element or position if first == last.
1978 //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
1979 iterator insert(const_iterator position, std::initializer_list<value_type> il)
1981 //Assertion done in insert()
1982 return this->insert(position, il.begin(), il.end());
1986 //! <b>Effects</b>: Removes the last element from the container.
1988 //! <b>Throws</b>: Nothing.
1990 //! <b>Complexity</b>: Constant time.
1991 void pop_back() BOOST_NOEXCEPT_OR_NOTHROW
1993 BOOST_ASSERT(!this->empty());
1994 //Destroy last element
1995 this->priv_destroy_last();
1998 //! <b>Effects</b>: Erases the element at position pos.
2000 //! <b>Throws</b>: Nothing.
2002 //! <b>Complexity</b>: Linear to the elements between pos and the
2003 //! last element. Constant if pos is the last element.
2004 iterator erase(const_iterator position)
2006 BOOST_ASSERT(this->priv_in_range(position));
2007 const pointer p = vector_iterator_get_ptr(position);
2008 T *const pos_ptr = container_detail::to_raw_pointer(p);
2009 T *const beg_ptr = this->priv_raw_begin();
2010 T *const new_end_ptr = ::boost::container::move(pos_ptr + 1, beg_ptr + this->m_holder.m_size, pos_ptr);
2011 //Move elements forward and destroy last
2012 this->priv_destroy_last(pos_ptr == new_end_ptr);
2016 //! <b>Effects</b>: Erases the elements pointed by [first, last).
2018 //! <b>Throws</b>: Nothing.
2020 //! <b>Complexity</b>: Linear to the distance between first and last
2021 //! plus linear to the elements between pos and the last element.
2022 iterator erase(const_iterator first, const_iterator last)
2024 BOOST_ASSERT(first == last ||
2025 (first < last && this->priv_in_range(first) && this->priv_in_range_or_end(last)));
2027 T* const old_end_ptr = this->priv_raw_end();
2028 T* const first_ptr = container_detail::to_raw_pointer(vector_iterator_get_ptr(first));
2029 T* const last_ptr = container_detail::to_raw_pointer(vector_iterator_get_ptr(last));
2030 T* const ptr = container_detail::to_raw_pointer(boost::container::move(last_ptr, old_end_ptr, first_ptr));
2031 this->priv_destroy_last_n(old_end_ptr - ptr);
2033 return iterator(vector_iterator_get_ptr(first));
2036 //! <b>Effects</b>: Swaps the contents of *this and x.
2038 //! <b>Throws</b>: Nothing.
2040 //! <b>Complexity</b>: Constant.
2041 void swap(vector& x)
2042 BOOST_NOEXCEPT_IF( ((allocator_traits_type::propagate_on_container_swap::value
2043 || allocator_traits_type::is_always_equal::value) &&
2044 !container_detail::is_version<Allocator, 0>::value))
2046 this->priv_swap(x, container_detail::bool_<container_detail::is_version<Allocator, 0>::value>());
2049 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2051 //! <b>Effects</b>: Swaps the contents of *this and x.
2053 //! <b>Throws</b>: Nothing.
2055 //! <b>Complexity</b>: Linear
2057 //! <b>Note</b>: Non-standard extension to support static_vector
2058 template<class OtherAllocator>
2059 void swap(vector<T, OtherAllocator> & x
2060 , typename container_detail::enable_if_and
2062 , container_detail::is_version<OtherAllocator, 0>
2063 , container_detail::is_different<OtherAllocator, allocator_type>
2066 { this->m_holder.deep_swap(x.m_holder); }
2068 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2070 //! <b>Effects</b>: Erases all the elements of the vector.
2072 //! <b>Throws</b>: Nothing.
2074 //! <b>Complexity</b>: Linear to the number of elements in the container.
2075 void clear() BOOST_NOEXCEPT_OR_NOTHROW
2076 { this->priv_destroy_all(); }
2078 //! <b>Effects</b>: Returns true if x and y are equal
2080 //! <b>Complexity</b>: Linear to the number of elements in the container.
2081 friend bool operator==(const vector& x, const vector& y)
2082 { return x.size() == y.size() && ::boost::container::algo_equal(x.begin(), x.end(), y.begin()); }
2084 //! <b>Effects</b>: Returns true if x and y are unequal
2086 //! <b>Complexity</b>: Linear to the number of elements in the container.
2087 friend bool operator!=(const vector& x, const vector& y)
2088 { return !(x == y); }
2090 //! <b>Effects</b>: Returns true if x is less than y
2092 //! <b>Complexity</b>: Linear to the number of elements in the container.
2093 friend bool operator<(const vector& x, const vector& y)
2095 const_iterator first1(x.cbegin()), first2(y.cbegin());
2096 const const_iterator last1(x.cend()), last2(y.cend());
2097 for ( ; (first1 != last1) && (first2 != last2); ++first1, ++first2 ) {
2098 if (*first1 < *first2) return true;
2099 if (*first2 < *first1) return false;
2101 return (first1 == last1) && (first2 != last2);
2104 //! <b>Effects</b>: Returns true if x is greater 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)
2110 //! <b>Effects</b>: Returns true if x is equal or less than y
2112 //! <b>Complexity</b>: Linear to the number of elements in the container.
2113 friend bool operator<=(const vector& x, const vector& y)
2114 { return !(y < x); }
2116 //! <b>Effects</b>: Returns true if x is equal or greater than y
2118 //! <b>Complexity</b>: Linear to the number of elements in the container.
2119 friend bool operator>=(const vector& x, const vector& y)
2120 { return !(x < y); }
2122 //! <b>Effects</b>: x.swap(y)
2124 //! <b>Complexity</b>: Constant.
2125 friend void swap(vector& x, vector& y)
2128 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2129 //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
2130 //! effect. Otherwise, it is a request for allocation of additional memory
2131 //! (memory expansion) that will not invalidate iterators.
2132 //! If the request is successful, then capacity() is greater than or equal to
2133 //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
2135 //! <b>Throws</b>: If memory allocation allocation throws or T's copy/move constructor throws.
2137 //! <b>Note</b>: Non-standard extension.
2138 bool stable_reserve(size_type new_cap)
2140 const size_type cp = this->capacity();
2141 return cp >= new_cap || (alloc_version::value == 2 && this->m_holder.try_expand_fwd(new_cap - cp));
2144 //Absolutely experimental. This function might change, disappear or simply crash!
2145 template<class BiDirPosConstIt, class BiDirValueIt>
2146 void insert_ordered_at(const size_type element_count, BiDirPosConstIt last_position_it, BiDirValueIt last_value_it)
2148 typedef container_detail::vector_insert_ordered_cursor<BiDirPosConstIt, BiDirValueIt> inserter_t;
2149 return this->priv_insert_ordered_at(element_count, inserter_t(last_position_it, last_value_it));
2152 template<class BidirIt>
2153 void merge(BidirIt first, BidirIt last)
2154 { this->merge(first, last, value_less()); }
2156 template<class BidirIt, class Compare>
2157 void merge(BidirIt first, BidirIt last, Compare comp)
2158 { this->priv_merge(container_detail::false_type(), first, last, comp); }
2160 template<class BidirIt>
2161 void merge_unique(BidirIt first, BidirIt last)
2162 { this->priv_merge(container_detail::true_type(), first, last, value_less()); }
2164 template<class BidirIt, class Compare>
2165 void merge_unique(BidirIt first, BidirIt last, Compare comp)
2166 { this->priv_merge(container_detail::true_type(), first, last, comp); }
2169 template<class PositionValue>
2170 void priv_insert_ordered_at(const size_type element_count, PositionValue position_value)
2172 const size_type old_size_pos = this->size();
2173 this->reserve(old_size_pos + element_count);
2174 T* const begin_ptr = this->priv_raw_begin();
2175 size_type insertions_left = element_count;
2176 size_type prev_pos = old_size_pos;
2177 size_type old_hole_size = element_count;
2179 //Exception rollback. If any copy throws before the hole is filled, values
2180 //already inserted/copied at the end of the buffer will be destroyed.
2181 typename value_traits::ArrayDestructor past_hole_values_destroyer
2182 (begin_ptr + old_size_pos + element_count, this->m_holder.alloc(), size_type(0u));
2183 //Loop for each insertion backwards, first moving the elements after the insertion point,
2184 //then inserting the element.
2185 while(insertions_left){
2187 size_type const pos = position_value.get_pos();
2188 BOOST_ASSERT(pos != size_type(-1) && pos <= old_size_pos && pos <= prev_pos);
2189 //If needed shift the range after the insertion point and the previous insertion point.
2190 //Function will take care if the shift crosses the size() boundary, using copy/move
2191 //or uninitialized copy/move if necessary.
2192 size_type new_hole_size = (pos != prev_pos)
2193 ? priv_insert_ordered_at_shift_range(pos, prev_pos, this->size(), insertions_left)
2197 //The hole was reduced by priv_insert_ordered_at_shift_range so expand exception rollback range backwards
2198 past_hole_values_destroyer.increment_size_backwards(prev_pos - pos);
2199 //Insert the new value in the hole
2200 allocator_traits_type::construct(this->m_holder.alloc(), begin_ptr + pos + insertions_left - 1, position_value.get_val());
2201 if(--new_hole_size){
2202 //The hole was reduced by the new insertion by one
2203 past_hole_values_destroyer.increment_size_backwards(size_type(1u));
2206 //Hole was just filled, disable exception rollback and change vector size
2207 past_hole_values_destroyer.release();
2208 this->m_holder.m_size += element_count;
2213 //Hole was just filled by priv_insert_ordered_at_shift_range, disable exception rollback and change vector size
2214 past_hole_values_destroyer.release();
2215 this->m_holder.m_size += element_count;
2217 //Insert the new value in the already constructed range
2218 begin_ptr[pos + insertions_left - 1] = position_value.get_val();
2221 old_hole_size = new_hole_size;
2226 template<class UniqueBool, class BidirIt, class Compare>
2227 void priv_merge(UniqueBool, BidirIt first, BidirIt last, Compare comp)
2229 size_type const n = static_cast<size_type>(boost::container::iterator_distance(first, last));
2230 size_type const s = this->size();
2231 if(BOOST_LIKELY(s)){
2232 size_type const c = this->capacity();
2233 size_type const free_c = (c - s);
2234 //Use a new buffer if current one is too small for new elements,
2235 //or there is no room for position indexes
2237 size_type const new_size = s + n;
2238 size_type new_cap = new_size;
2239 pointer p = pointer();
2240 p = this->m_holder.allocation_command(allocate_new, new_size, new_cap, p);
2241 this->priv_merge_in_new_buffer(UniqueBool(), first, n, comp, p, new_cap);
2243 else if(!UniqueBool::value && free_c >= n){
2244 typedef container_detail::vector_merge_cursor<T, size_type, BidirIt, Compare> inserter_t;
2245 T* const pbeg = this->priv_raw_begin();
2246 return this->priv_insert_ordered_at(n, inserter_t(pbeg, pbeg + s, last, comp));
2248 else{ //UniqueBool::value == true and free_c >= n
2249 std::size_t remaining = n;
2250 static const std::size_t PosCount = 64u;
2251 size_type positions[PosCount];
2252 size_type *indexes = 0;
2254 //Query for room to store indexes in the remaining buffer
2255 boost::uintptr_t const szt_align_mask = container_detail::alignment_of<size_type>::value - 1;
2256 boost::uintptr_t const addr = boost::uintptr_t(this->priv_raw_begin() + s + n);
2257 boost::uintptr_t const capaddr = boost::uintptr_t(this->priv_raw_begin() + c);
2258 boost::uintptr_t const aligned_addr = (addr + szt_align_mask) & ~szt_align_mask;
2259 indexes = reinterpret_cast<size_type *>(aligned_addr);
2260 std::size_t index_capacity = (aligned_addr >= capaddr) ? 0u : (capaddr - aligned_addr)/sizeof(size_type);
2262 //Capacity is constant, we're not going to change it
2263 if(index_capacity < PosCount){
2264 indexes = positions;
2265 index_capacity = PosCount;
2267 if(index_capacity > remaining)
2268 index_capacity = remaining;
2269 BidirIt limit = first;
2270 boost::container::iterator_advance(limit, index_capacity);
2271 this->priv_insert_ordered_range(UniqueBool(), index_capacity, first, limit, indexes, comp);
2273 remaining -= index_capacity;
2278 this->insert(this->cend(), n, first, last);
2282 template <class UniqueBool, class BidirIt, class Compare>
2283 void priv_insert_ordered_range
2284 (UniqueBool, size_type const n, BidirIt first, BidirIt const last, size_type positions[], Compare comp)
2286 //Linear: at most N + M -1 comparisons
2295 //bool const linear = !s || !n || (s <= n) || ((s+n)/n/2 < logN);
2296 size_type const s = this->size();
2297 size_type remaining = n;
2298 T* const pbeg = this->priv_raw_begin();
2299 T* const pend = pbeg + s;
2301 size_type *position = positions;
2302 size_type added_in_middle = 0;
2303 if(first != last && pcur != pend){
2305 //maintain stability moving external values only if they are strictly less
2306 if(comp(*first, *pcur)) {
2307 *position = static_cast<size_type>(pcur - pbeg);
2308 BOOST_ASSERT((position == positions) || (*(position-1) == size_type(-1)) || (*(position-1) <= *position));
2312 if(++first == last) break;
2314 else if(UniqueBool::value && !comp(*pcur, *first)){
2315 *position = size_type(-1);
2318 if(++first == last) break;
2321 if(++pcur == pend) break;
2325 this->insert_ordered_at(added_in_middle, position, first);
2326 this->insert(this->cend(), remaining, first, last);
2329 template<class UniqueBool, class FwdIt, class Compare>
2330 void priv_merge_in_new_buffer
2331 (UniqueBool, FwdIt first, size_type n, Compare comp, pointer new_storage, size_type const new_cap)
2333 BOOST_ASSERT((new_cap >= this->size() ) && (new_cap - this->size()) >= n);
2334 allocator_type &a = this->m_holder.alloc();
2335 typename value_traits::ArrayDeallocator new_buffer_deallocator(new_storage, a, new_cap);
2336 typename value_traits::ArrayDestructor new_values_destroyer(new_storage, a, 0u);
2337 T* pbeg = this->priv_raw_begin();
2338 size_type const old_size = this->size();
2339 T* const pend = pbeg + old_size;
2340 T* d_first = container_detail::to_raw_pointer(new_storage);
2341 size_type added = n;
2342 //Merge in new buffer loop
2345 ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), pbeg, pend, d_first);
2348 else if(pbeg == pend) {
2349 ::boost::container::uninitialized_move_alloc_n(this->m_holder.alloc(), first, n, d_first);
2352 //maintain stability moving external values only if they are strictly less
2353 else if(comp(*first, *pbeg)) {
2354 allocator_traits_type::construct( this->m_holder.alloc(), d_first, ::boost::move(*first) );
2355 new_values_destroyer.increment_size(1u);
2360 else if(UniqueBool::value && !comp(*pbeg, *first)){
2366 allocator_traits_type::construct( this->m_holder.alloc(), d_first, ::boost::move(*pbeg) );
2367 new_values_destroyer.increment_size(1u);
2373 //Nothrow operations
2374 pointer const old_p = this->m_holder.start();
2375 size_type const old_cap = this->m_holder.capacity();
2376 boost::container::destroy_alloc_n(a, container_detail::to_raw_pointer(old_p), old_size);
2377 a.deallocate(old_p, old_cap);
2378 this->m_holder.m_size = old_size + added;
2379 this->m_holder.start(new_storage);
2380 this->m_holder.capacity(new_cap);
2381 new_buffer_deallocator.release();
2382 new_values_destroyer.release();
2385 bool room_enough() const
2386 { return this->m_holder.m_size < this->m_holder.capacity(); }
2388 pointer back_ptr() const
2389 { return this->m_holder.start() + this->m_holder.m_size; }
2391 size_type priv_index_of(pointer p) const
2393 BOOST_ASSERT(this->m_holder.start() <= p);
2394 BOOST_ASSERT(p <= (this->m_holder.start()+this->size()));
2395 return static_cast<size_type>(p - this->m_holder.start());
2398 template<class OtherAllocator>
2399 void priv_move_assign(BOOST_RV_REF_BEG vector<T, OtherAllocator> BOOST_RV_REF_END x
2400 , typename container_detail::enable_if_c
2401 < container_detail::is_version<OtherAllocator, 0>::value >::type * = 0)
2403 if(!container_detail::is_same<OtherAllocator, allocator_type>::value &&
2404 this->capacity() < x.size()){
2407 T* const this_start = this->priv_raw_begin();
2408 T* const other_start = x.priv_raw_begin();
2409 const size_type this_sz = m_holder.m_size;
2410 const size_type other_sz = static_cast<size_type>(x.m_holder.m_size);
2411 boost::container::move_assign_range_alloc_n(this->m_holder.alloc(), other_start, other_sz, this_start, this_sz);
2412 this->m_holder.m_size = other_sz;
2415 template<class OtherAllocator>
2416 void priv_move_assign(BOOST_RV_REF_BEG vector<T, OtherAllocator> BOOST_RV_REF_END x
2417 , typename container_detail::disable_if_or
2419 , container_detail::is_version<OtherAllocator, 0>
2420 , container_detail::is_different<OtherAllocator, allocator_type>
2423 //for move assignment, no aliasing (&x != this) is assummed.
2424 BOOST_ASSERT(this != &x);
2425 allocator_type &this_alloc = this->m_holder.alloc();
2426 allocator_type &x_alloc = x.m_holder.alloc();
2427 const bool propagate_alloc = allocator_traits_type::propagate_on_container_move_assignment::value;
2429 const bool is_propagable_from_x = is_propagable_from(x_alloc, x.m_holder.start(), this_alloc, propagate_alloc);
2430 const bool is_propagable_from_t = is_propagable_from(this_alloc, m_holder.start(), x_alloc, propagate_alloc);
2431 const bool are_both_propagable = is_propagable_from_x && is_propagable_from_t;
2433 //Resources can be transferred if both allocators are
2434 //going to be equal after this function (either propagated or already equal)
2435 if(are_both_propagable){
2436 //Destroy objects but retain memory in case x reuses it in the future
2438 this->m_holder.swap_resources(x.m_holder);
2440 else if(is_propagable_from_x){
2442 this->m_holder.alloc().deallocate(this->m_holder.m_start, this->m_holder.m_capacity);
2443 this->m_holder.steal_resources(x.m_holder);
2445 //Else do a one by one move
2447 this->assign( boost::make_move_iterator(container_detail::iterator_to_raw_pointer(x.begin()))
2448 , boost::make_move_iterator(container_detail::iterator_to_raw_pointer(x.end() ))
2451 //Move allocator if needed
2452 container_detail::move_alloc(this_alloc, x_alloc, container_detail::bool_<propagate_alloc>());
2455 template<class OtherAllocator>
2456 void priv_copy_assign(const vector<T, OtherAllocator> &x
2457 , typename container_detail::enable_if_c
2458 < container_detail::is_version<OtherAllocator, 0>::value >::type * = 0)
2460 if(!container_detail::is_same<OtherAllocator, allocator_type>::value &&
2461 this->capacity() < x.size()){
2464 T* const this_start = this->priv_raw_begin();
2465 T* const other_start = x.priv_raw_begin();
2466 const size_type this_sz = m_holder.m_size;
2467 const size_type other_sz = static_cast<size_type>(x.m_holder.m_size);
2468 boost::container::copy_assign_range_alloc_n(this->m_holder.alloc(), other_start, other_sz, this_start, this_sz);
2469 this->m_holder.m_size = other_sz;
2472 template<class OtherAllocator>
2473 typename container_detail::disable_if_or
2475 , container_detail::is_version<OtherAllocator, 0>
2476 , container_detail::is_different<OtherAllocator, allocator_type>
2478 priv_copy_assign(const vector<T, OtherAllocator> &x)
2480 allocator_type &this_alloc = this->m_holder.alloc();
2481 const allocator_type &x_alloc = x.m_holder.alloc();
2482 container_detail::bool_<allocator_traits_type::
2483 propagate_on_container_copy_assignment::value> flag;
2484 if(flag && this_alloc != x_alloc){
2486 this->shrink_to_fit();
2488 container_detail::assign_alloc(this_alloc, x_alloc, flag);
2489 this->assign( x.priv_raw_begin(), x.priv_raw_end() );
2492 template<class Vector> //Template it to avoid it in explicit instantiations
2493 void priv_swap(Vector &x, container_detail::true_type) //version_0
2494 { this->m_holder.deep_swap(x.m_holder); }
2496 template<class Vector> //Template it to avoid it in explicit instantiations
2497 void priv_swap(Vector &x, container_detail::false_type) //version_N
2499 const bool propagate_alloc = allocator_traits_type::propagate_on_container_swap::value;
2500 if(are_swap_propagable( this->get_stored_allocator(), this->m_holder.start()
2501 , x.get_stored_allocator(), x.m_holder.start(), propagate_alloc)){
2502 //Just swap internals
2503 this->m_holder.swap_resources(x.m_holder);
2506 //Else swap element by element...
2507 bool const t_smaller = this->size() < x.size();
2508 vector &sml = t_smaller ? *this : x;
2509 vector &big = t_smaller ? x : *this;
2511 size_type const common_elements = sml.size();
2512 for(size_type i = 0; i != common_elements; ++i){
2513 boost::adl_move_swap(sml[i], big[i]);
2515 //... and move-insert the remaining range
2516 sml.insert( sml.cend()
2517 , boost::make_move_iterator(container_detail::iterator_to_raw_pointer(big.nth(common_elements)))
2518 , boost::make_move_iterator(container_detail::iterator_to_raw_pointer(big.end()))
2520 //Destroy remaining elements
2521 big.erase(big.nth(common_elements), big.cend());
2523 //And now swap the allocator
2524 container_detail::swap_alloc(this->m_holder.alloc(), x.m_holder.alloc(), container_detail::bool_<propagate_alloc>());
2527 void priv_reserve_no_capacity(size_type, version_0)
2528 { throw_bad_alloc(); }
2530 container_detail::insert_range_proxy<Allocator, boost::move_iterator<T*>, T*> priv_dummy_empty_proxy()
2532 return container_detail::insert_range_proxy<Allocator, boost::move_iterator<T*>, T*>
2533 (::boost::make_move_iterator((T *)0));
2536 void priv_reserve_no_capacity(size_type new_cap, version_1)
2538 //There is not enough memory, allocate a new buffer
2539 //Pass the hint so that allocators can take advantage of this.
2540 pointer const p = allocator_traits_type::allocate(this->m_holder.alloc(), new_cap, this->m_holder.m_start);
2541 //We will reuse insert code, so create a dummy input iterator
2542 this->priv_forward_range_insert_new_allocation
2543 ( container_detail::to_raw_pointer(p), new_cap, this->priv_raw_end(), 0, this->priv_dummy_empty_proxy());
2546 void priv_reserve_no_capacity(size_type new_cap, version_2)
2548 //There is not enough memory, allocate a new
2549 //buffer or expand the old one.
2550 bool same_buffer_start;
2551 size_type real_cap = 0;
2552 pointer reuse(this->m_holder.start());
2553 pointer const ret(this->m_holder.allocation_command(allocate_new | expand_fwd | expand_bwd, new_cap, real_cap = new_cap, reuse));
2555 //Check for forward expansion
2556 same_buffer_start = reuse && this->m_holder.start() == ret;
2557 if(same_buffer_start){
2558 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2559 ++this->num_expand_fwd;
2561 this->m_holder.capacity(real_cap);
2563 else{ //If there is no forward expansion, move objects, we will reuse insertion code
2564 T * const new_mem = container_detail::to_raw_pointer(ret);
2565 T * const ins_pos = this->priv_raw_end();
2566 if(reuse){ //Backwards (and possibly forward) expansion
2567 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2568 ++this->num_expand_bwd;
2570 this->priv_forward_range_insert_expand_backwards
2571 ( new_mem , real_cap, ins_pos, 0, this->priv_dummy_empty_proxy());
2574 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2577 this->priv_forward_range_insert_new_allocation
2578 ( new_mem, real_cap, ins_pos, 0, this->priv_dummy_empty_proxy());
2583 void priv_destroy_last(const bool moved = false) BOOST_NOEXCEPT_OR_NOTHROW
2586 if(!(value_traits::trivial_dctr || (value_traits::trivial_dctr_after_move && moved))){
2587 value_type* const p = this->priv_raw_end() - 1;
2588 allocator_traits_type::destroy(this->get_stored_allocator(), p);
2590 --this->m_holder.m_size;
2593 void priv_destroy_last_n(const size_type n) BOOST_NOEXCEPT_OR_NOTHROW
2595 BOOST_ASSERT(n <= this->m_holder.m_size);
2596 if(!value_traits::trivial_dctr){
2597 T* const destroy_pos = this->priv_raw_begin() + (this->m_holder.m_size-n);
2598 boost::container::destroy_alloc_n(this->get_stored_allocator(), destroy_pos, n);
2600 this->m_holder.m_size -= n;
2603 template<class InpIt>
2604 void priv_uninitialized_construct_at_end(InpIt first, InpIt last)
2606 T* const old_end_pos = this->priv_raw_end();
2607 T* const new_end_pos = boost::container::uninitialized_copy_alloc(this->m_holder.alloc(), first, last, old_end_pos);
2608 this->m_holder.m_size += new_end_pos - old_end_pos;
2611 void priv_destroy_all() BOOST_NOEXCEPT_OR_NOTHROW
2613 boost::container::destroy_alloc_n
2614 (this->get_stored_allocator(), this->priv_raw_begin(), this->m_holder.m_size);
2615 this->m_holder.m_size = 0;
2619 iterator priv_insert(const const_iterator &p, BOOST_FWD_REF(U) x)
2621 BOOST_ASSERT(this->priv_in_range_or_end(p));
2622 return this->priv_forward_range_insert
2623 ( vector_iterator_get_ptr(p), 1, container_detail::get_insert_value_proxy<T*, Allocator>(::boost::forward<U>(x)));
2626 container_detail::insert_copy_proxy<Allocator, T*> priv_single_insert_proxy(const T &x)
2627 { return container_detail::insert_copy_proxy<Allocator, T*> (x); }
2629 container_detail::insert_move_proxy<Allocator, T*> priv_single_insert_proxy(BOOST_RV_REF(T) x)
2630 { return container_detail::insert_move_proxy<Allocator, T*> (x); }
2633 void priv_push_back(BOOST_FWD_REF(U) u)
2635 if (BOOST_LIKELY(this->room_enough())){
2636 //There is more memory, just construct a new object at the end
2637 allocator_traits_type::construct
2638 ( this->m_holder.alloc(), this->priv_raw_end(), ::boost::forward<U>(u) );
2639 ++this->m_holder.m_size;
2642 this->priv_forward_range_insert_no_capacity
2643 ( this->back_ptr(), 1
2644 , this->priv_single_insert_proxy(::boost::forward<U>(u)), alloc_version());
2648 container_detail::insert_n_copies_proxy<Allocator, T*> priv_resize_proxy(const T &x)
2649 { return container_detail::insert_n_copies_proxy<Allocator, T*>(x); }
2651 container_detail::insert_default_initialized_n_proxy<Allocator, T*> priv_resize_proxy(default_init_t)
2652 { return container_detail::insert_default_initialized_n_proxy<Allocator, T*>(); }
2654 container_detail::insert_value_initialized_n_proxy<Allocator, T*> priv_resize_proxy(value_init_t)
2655 { return container_detail::insert_value_initialized_n_proxy<Allocator, T*>(); }
2658 void priv_resize(size_type new_size, const U& u)
2660 const size_type sz = this->size();
2662 //Destroy last elements
2663 this->priv_destroy_last_n(sz - new_size);
2666 const size_type n = new_size - this->size();
2667 this->priv_forward_range_insert_at_end(n, this->priv_resize_proxy(u), alloc_version());
2671 void priv_shrink_to_fit(version_0) BOOST_NOEXCEPT_OR_NOTHROW
2674 void priv_shrink_to_fit(version_1)
2676 const size_type cp = this->m_holder.capacity();
2678 const size_type sz = this->size();
2680 this->m_holder.alloc().deallocate(this->m_holder.m_start, cp);
2681 this->m_holder.m_start = pointer();
2682 this->m_holder.m_capacity = 0;
2685 //Allocate a new buffer.
2686 //Pass the hint so that allocators can take advantage of this.
2687 pointer const p = allocator_traits_type::allocate(this->m_holder.alloc(), sz, this->m_holder.m_start);
2689 //We will reuse insert code, so create a dummy input iterator
2690 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2693 this->priv_forward_range_insert_new_allocation
2694 ( container_detail::to_raw_pointer(p), sz
2695 , this->priv_raw_begin(), 0, this->priv_dummy_empty_proxy());
2700 void priv_shrink_to_fit(version_2) BOOST_NOEXCEPT_OR_NOTHROW
2702 const size_type cp = this->m_holder.capacity();
2704 const size_type sz = this->size();
2706 this->m_holder.alloc().deallocate(this->m_holder.m_start, cp);
2707 this->m_holder.m_start = pointer();
2708 this->m_holder.m_capacity = 0;
2711 size_type received_size = sz;
2712 pointer reuse(this->m_holder.start());
2713 if(this->m_holder.allocation_command
2714 (shrink_in_place | nothrow_allocation, cp, received_size, reuse)){
2715 this->m_holder.capacity(received_size);
2716 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2724 template <class InsertionProxy>
2725 iterator priv_forward_range_insert_no_capacity
2726 (const pointer &pos, const size_type, const InsertionProxy , version_0)
2729 return iterator(pos);
2732 template <class InsertionProxy>
2733 iterator priv_forward_range_insert_no_capacity
2734 (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, version_1)
2736 //Check if we have enough memory or try to expand current memory
2737 const size_type n_pos = pos - this->m_holder.start();
2738 T *const raw_pos = container_detail::to_raw_pointer(pos);
2740 const size_type new_cap = this->m_holder.next_capacity(n);
2741 //Pass the hint so that allocators can take advantage of this.
2742 T * const new_buf = container_detail::to_raw_pointer
2743 (allocator_traits_type::allocate(this->m_holder.alloc(), new_cap, this->m_holder.m_start));
2744 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2747 this->priv_forward_range_insert_new_allocation
2748 ( new_buf, new_cap, raw_pos, n, insert_range_proxy);
2749 return iterator(this->m_holder.start() + n_pos);
2752 template <class InsertionProxy>
2753 iterator priv_forward_range_insert_no_capacity
2754 (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, version_2)
2756 //Check if we have enough memory or try to expand current memory
2757 T *const raw_pos = container_detail::to_raw_pointer(pos);
2758 const size_type n_pos = raw_pos - this->priv_raw_begin();
2760 //There is not enough memory, allocate a new
2761 //buffer or expand the old one.
2762 size_type real_cap = this->m_holder.next_capacity(n);
2763 pointer reuse(this->m_holder.start());
2764 pointer const ret (this->m_holder.allocation_command
2765 (allocate_new | expand_fwd | expand_bwd, this->m_holder.m_size + n, real_cap, reuse));
2767 //Buffer reallocated
2769 //Forward expansion, delay insertion
2770 if(this->m_holder.start() == ret){
2771 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2772 ++this->num_expand_fwd;
2774 this->m_holder.capacity(real_cap);
2776 this->priv_forward_range_insert_expand_forward(raw_pos, n, insert_range_proxy);
2778 //Backwards (and possibly forward) expansion
2780 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2781 ++this->num_expand_bwd;
2783 this->priv_forward_range_insert_expand_backwards
2784 (container_detail::to_raw_pointer(ret), real_cap, raw_pos, n, insert_range_proxy);
2789 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2792 this->priv_forward_range_insert_new_allocation
2793 ( container_detail::to_raw_pointer(ret), real_cap, raw_pos, n, insert_range_proxy);
2796 return iterator(this->m_holder.start() + n_pos);
2799 template <class InsertionProxy>
2800 iterator priv_forward_range_insert
2801 (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy)
2803 BOOST_ASSERT(this->m_holder.capacity() >= this->m_holder.m_size);
2804 //Check if we have enough memory or try to expand current memory
2805 const size_type remaining = this->m_holder.capacity() - this->m_holder.m_size;
2807 bool same_buffer_start = n <= remaining;
2808 if (!same_buffer_start){
2809 return priv_forward_range_insert_no_capacity(pos, n, insert_range_proxy, alloc_version());
2813 T *const raw_pos = container_detail::to_raw_pointer(pos);
2814 const size_type n_pos = raw_pos - this->priv_raw_begin();
2815 this->priv_forward_range_insert_expand_forward(raw_pos, n, insert_range_proxy);
2816 return iterator(this->m_holder.start() + n_pos);
2820 template <class InsertionProxy>
2821 iterator priv_forward_range_insert_at_end
2822 (const size_type n, const InsertionProxy insert_range_proxy, version_0)
2824 //Check if we have enough memory or try to expand current memory
2825 const size_type remaining = this->m_holder.capacity() - this->m_holder.m_size;
2828 //This will trigger an error
2831 this->priv_forward_range_insert_at_end_expand_forward(n, insert_range_proxy);
2835 template <class InsertionProxy, class AllocVersion>
2836 iterator priv_forward_range_insert_at_end
2837 (const size_type n, const InsertionProxy insert_range_proxy, AllocVersion)
2839 return this->priv_forward_range_insert(this->back_ptr(), n, insert_range_proxy);
2842 //Takes the range pointed by [first_pos, last_pos) and shifts it to the right
2843 //by 'shift_count'. 'limit_pos' marks the end of constructed elements.
2845 //Precondition: first_pos <= last_pos <= limit_pos
2847 //The shift operation might cross limit_pos so elements to moved beyond limit_pos
2848 //are uninitialized_moved with an allocator. Other elements are moved.
2850 //The shift operation might left uninitialized elements after limit_pos
2851 //and the number of uninitialized elements is returned by the function.
2854 // first_pos last_pos old_limit
2856 // ____________V_______V__________________V_____________
2857 //| prefix | range | suffix |raw_mem ~
2858 //|____________|_______|__________________|_____________~
2860 //New situation in Case A (hole_size == 0):
2861 // range is moved through move assignments
2863 // first_pos last_pos limit_pos
2865 // ____________V_______V__________________V_____________
2866 //| prefix' | | | range |suffix'|raw_mem ~
2867 //|________________+______|___^___|_______|_____________~
2872 //New situation in Case B (hole_size >= 0):
2873 // range is moved through uninitialized moves
2875 // first_pos last_pos limit_pos
2877 // ____________V_______V__________________V________________
2878 //| prefix' | | | [hole] | range |
2879 //|_______________________________________|________|___^___|
2881 // |_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_^
2883 //New situation in Case C (hole_size == 0):
2884 // range is moved through move assignments and uninitialized moves
2886 // first_pos last_pos limit_pos
2888 // ____________V_______V__________________V___
2889 //| prefix' | | | range |
2890 //|___________________________________|___^___|
2892 // |_>_>_>_>_>_>_>_>_>_>_>^
2893 size_type priv_insert_ordered_at_shift_range
2894 (size_type first_pos, size_type last_pos, size_type limit_pos, size_type shift_count)
2896 BOOST_ASSERT(first_pos <= last_pos);
2897 BOOST_ASSERT(last_pos <= limit_pos);
2899 T* const begin_ptr = this->priv_raw_begin();
2900 T* const first_ptr = begin_ptr + first_pos;
2901 T* const last_ptr = begin_ptr + last_pos;
2903 size_type hole_size = 0;
2905 if((last_pos + shift_count) <= limit_pos){
2907 boost::container::move_backward(first_ptr, last_ptr, last_ptr + shift_count);
2910 else if((first_pos + shift_count) >= limit_pos){
2911 //All uninitialized_moved
2912 ::boost::container::uninitialized_move_alloc
2913 (this->m_holder.alloc(), first_ptr, last_ptr, first_ptr + shift_count);
2914 hole_size = first_pos + shift_count - limit_pos;
2918 //Some uninitialized_moved
2919 T* const limit_ptr = begin_ptr + limit_pos;
2920 T* const boundary_ptr = limit_ptr - shift_count;
2921 ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), boundary_ptr, last_ptr, limit_ptr);
2922 //The rest is move assigned
2923 boost::container::move_backward(first_ptr, boundary_ptr, limit_ptr);
2929 T *priv_raw_begin() const
2930 { return container_detail::to_raw_pointer(m_holder.start()); }
2932 T* priv_raw_end() const
2933 { return this->priv_raw_begin() + this->m_holder.m_size; }
2935 template <class InsertionProxy>
2936 void priv_forward_range_insert_at_end_expand_forward(const size_type n, InsertionProxy insert_range_proxy)
2938 T* const old_finish = this->priv_raw_end();
2939 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n);
2940 this->m_holder.m_size += n;
2943 template <class InsertionProxy>
2944 void priv_forward_range_insert_expand_forward(T* const pos, const size_type n, InsertionProxy insert_range_proxy)
2946 //n can't be 0, because there is nothing to do in that case
2947 if(BOOST_UNLIKELY(!n)) return;
2948 //There is enough memory
2949 T* const old_finish = this->priv_raw_end();
2950 const size_type elems_after = old_finish - pos;
2953 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n);
2954 this->m_holder.m_size += n;
2956 else if (elems_after >= n){
2957 //New elements can be just copied.
2958 //Move to uninitialized memory last objects
2959 ::boost::container::uninitialized_move_alloc
2960 (this->m_holder.alloc(), old_finish - n, old_finish, old_finish);
2961 this->m_holder.m_size += n;
2962 //Copy previous to last objects to the initialized end
2963 boost::container::move_backward(pos, old_finish - n, old_finish);
2964 //Insert new objects in the pos
2965 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, n);
2968 //The new elements don't fit in the [pos, end()) range.
2970 //Copy old [pos, end()) elements to the uninitialized memory (a gap is created)
2971 ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), pos, old_finish, pos + n);
2973 //Copy first new elements in pos (gap is still there)
2974 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, elems_after);
2975 //Copy to the beginning of the unallocated zone the last new elements (the gap is closed).
2976 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n - elems_after);
2977 this->m_holder.m_size += n;
2980 boost::container::destroy_alloc_n(this->get_stored_allocator(), pos + n, elems_after);
2987 template <class InsertionProxy>
2988 void priv_forward_range_insert_new_allocation
2989 (T* const new_start, size_type new_cap, T* const pos, const size_type n, InsertionProxy insert_range_proxy)
2991 //n can be zero, if we want to reallocate!
2992 T *new_finish = new_start;
2994 //Anti-exception rollbacks
2995 typename value_traits::ArrayDeallocator new_buffer_deallocator(new_start, this->m_holder.alloc(), new_cap);
2996 typename value_traits::ArrayDestructor new_values_destroyer(new_start, this->m_holder.alloc(), 0u);
2998 //Initialize with [begin(), pos) old buffer
2999 //the start of the new buffer
3000 T * const old_buffer = this->priv_raw_begin();
3002 new_finish = ::boost::container::uninitialized_move_alloc
3003 (this->m_holder.alloc(), this->priv_raw_begin(), pos, old_finish = new_finish);
3004 new_values_destroyer.increment_size(new_finish - old_finish);
3006 //Initialize new objects, starting from previous point
3007 old_finish = new_finish;
3008 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n);
3010 new_values_destroyer.increment_size(new_finish - old_finish);
3011 //Initialize from the rest of the old buffer,
3012 //starting from previous point
3014 new_finish = ::boost::container::uninitialized_move_alloc
3015 (this->m_holder.alloc(), pos, old_buffer + this->m_holder.m_size, new_finish);
3016 //Destroy and deallocate old elements
3017 //If there is allocated memory, destroy and deallocate
3018 if(!value_traits::trivial_dctr_after_move)
3019 boost::container::destroy_alloc_n(this->get_stored_allocator(), old_buffer, this->m_holder.m_size);
3020 this->m_holder.alloc().deallocate(this->m_holder.start(), this->m_holder.capacity());
3022 this->m_holder.start(new_start);
3023 this->m_holder.m_size = new_finish - new_start;
3024 this->m_holder.capacity(new_cap);
3025 //All construction successful, disable rollbacks
3026 new_values_destroyer.release();
3027 new_buffer_deallocator.release();
3030 template <class InsertionProxy>
3031 void priv_forward_range_insert_expand_backwards
3032 (T* const new_start, const size_type new_capacity,
3033 T* const pos, const size_type n, InsertionProxy insert_range_proxy)
3035 //n can be zero to just expand capacity
3037 T* const old_start = this->priv_raw_begin();
3038 const size_type old_size = this->m_holder.m_size;
3039 T* const old_finish = old_start + old_size;
3041 //We can have 8 possibilities:
3042 const size_type elemsbefore = static_cast<size_type>(pos - old_start);
3043 const size_type s_before = static_cast<size_type>(old_start - new_start);
3044 const size_type before_plus_new = elemsbefore + n;
3046 //Update the vector buffer information to a safe state
3047 this->m_holder.start(new_start);
3048 this->m_holder.capacity(new_capacity);
3049 this->m_holder.m_size = 0;
3051 //If anything goes wrong, this object will destroy
3052 //all the old objects to fulfill previous vector state
3053 typename value_traits::ArrayDestructor old_values_destroyer(old_start, this->m_holder.alloc(), old_size);
3054 //Check if s_before is big enough to hold the beginning of old data + new data
3055 if(s_before >= before_plus_new){
3056 //Copy first old values before pos, after that the new objects
3057 T *const new_elem_pos =
3058 ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), old_start, pos, new_start);
3059 this->m_holder.m_size = elemsbefore;
3060 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), new_elem_pos, n);
3061 this->m_holder.m_size = before_plus_new;
3062 const size_type new_size = old_size + n;
3063 //Check if s_before is so big that even copying the old data + new data
3064 //there is a gap between the new data and the old data
3065 if(s_before >= new_size){
3067 // _________________________________________________________
3068 //| raw_mem | old_begin | old_end |
3069 //| __________________________________|___________|_________|
3072 // _________________________________________________________
3073 //| old_begin | new | old_end | raw_mem |
3074 //|___________|__________|_________|________________________|
3076 //Now initialize the rest of memory with the last old values
3077 if(before_plus_new != new_size){ //Special case to avoid operations in back insertion
3078 ::boost::container::uninitialized_move_alloc
3079 (this->m_holder.alloc(), pos, old_finish, new_start + before_plus_new);
3080 //All new elements correctly constructed, avoid new element destruction
3081 this->m_holder.m_size = new_size;
3083 //Old values destroyed automatically with "old_values_destroyer"
3084 //when "old_values_destroyer" goes out of scope unless the have trivial
3085 //destructor after move.
3086 if(value_traits::trivial_dctr_after_move)
3087 old_values_destroyer.release();
3089 //s_before is so big that divides old_end
3092 // __________________________________________________
3093 //| raw_mem | old_begin | old_end |
3094 //| ___________________________|___________|_________|
3097 // __________________________________________________
3098 //| old_begin | new | old_end | raw_mem |
3099 //|___________|__________|_________|_________________|
3101 //Now initialize the rest of memory with the last old values
3102 //All new elements correctly constructed, avoid new element destruction
3103 const size_type raw_gap = s_before - before_plus_new;
3104 if(!value_traits::trivial_dctr){
3105 //Now initialize the rest of s_before memory with the
3106 //first of elements after new values
3107 ::boost::container::uninitialized_move_alloc_n
3108 (this->m_holder.alloc(), pos, raw_gap, new_start + before_plus_new);
3109 //Now we have a contiguous buffer so program trailing element destruction
3110 //and update size to the final size.
3111 old_values_destroyer.shrink_forward(new_size-s_before);
3112 this->m_holder.m_size = new_size;
3113 //Now move remaining last objects in the old buffer begin
3114 T * const remaining_pos = pos + raw_gap;
3115 if(remaining_pos != old_start){ //Make sure data has to be moved
3116 ::boost::container::move(remaining_pos, old_finish, old_start);
3118 //Once moved, avoid calling the destructors if trivial after move
3119 if(value_traits::trivial_dctr_after_move){
3120 old_values_destroyer.release();
3123 else{ //If trivial destructor, we can uninitialized copy + copy in a single uninitialized copy
3124 ::boost::container::uninitialized_move_alloc_n
3125 (this->m_holder.alloc(), pos, static_cast<size_type>(old_finish - pos), new_start + before_plus_new);
3126 this->m_holder.m_size = new_size;
3127 old_values_destroyer.release();
3132 //Check if we have to do the insertion in two phases
3133 //since maybe s_before is not big enough and
3134 //the buffer was expanded both sides
3137 // _________________________________________________
3138 //| raw_mem | old_begin + old_end | raw_mem |
3139 //|_________|_____________________|_________________|
3141 //New situation with do_after:
3142 // _________________________________________________
3143 //| old_begin + new + old_end | raw_mem |
3144 //|___________________________________|_____________|
3146 //New without do_after:
3147 // _________________________________________________
3148 //| old_begin + new + old_end | raw_mem |
3149 //|____________________________|____________________|
3151 const bool do_after = n > s_before;
3153 //Now we can have two situations: the raw_mem of the
3154 //beginning divides the old_begin, or the new elements:
3155 if (s_before <= elemsbefore) {
3156 //The raw memory divides the old_begin group:
3158 //If we need two phase construction (do_after)
3159 //new group is divided in new = new_beg + new_end groups
3160 //In this phase only new_beg will be inserted
3163 // _________________________________________________
3164 //| raw_mem | old_begin | old_end | raw_mem |
3165 //|_________|___________|_________|_________________|
3167 //New situation with do_after(1):
3168 //This is not definitive situation, the second phase
3170 // _________________________________________________
3171 //| old_begin | new_beg | old_end | raw_mem |
3172 //|___________|_________|_________|_________________|
3174 //New situation without do_after:
3175 // _________________________________________________
3176 //| old_begin | new | old_end | raw_mem |
3177 //|___________|_____|_________|_____________________|
3179 //Copy the first part of old_begin to raw_mem
3180 ::boost::container::uninitialized_move_alloc_n
3181 (this->m_holder.alloc(), old_start, s_before, new_start);
3182 //The buffer is all constructed until old_end,
3183 //so program trailing destruction and assign final size
3184 //if !do_after, s_before+n otherwise.
3185 size_type new_1st_range;
3187 new_1st_range = s_before;
3188 //release destroyer and update size
3189 old_values_destroyer.release();
3193 if(value_traits::trivial_dctr_after_move)
3194 old_values_destroyer.release();
3196 old_values_destroyer.shrink_forward(old_size - (s_before - n));
3199 this->m_holder.m_size = old_size + new_1st_range;
3200 //Now copy the second part of old_begin overwriting itself
3201 T *const next = ::boost::container::move(old_start + s_before, pos, old_start);
3202 //Now copy the new_beg elements
3203 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), next, new_1st_range);
3205 //If there is no after work and the last old part needs to be moved to front, do it
3206 if(!do_after && (n != s_before)){
3207 //Now displace old_end elements
3208 ::boost::container::move(pos, old_finish, next + new_1st_range);
3212 //If we have to expand both sides,
3213 //we will play if the first new values so
3214 //calculate the upper bound of new values
3216 //The raw memory divides the new elements
3218 //If we need two phase construction (do_after)
3219 //new group is divided in new = new_beg + new_end groups
3220 //In this phase only new_beg will be inserted
3223 // _______________________________________________________
3224 //| raw_mem | old_begin | old_end | raw_mem |
3225 //|_______________|___________|_________|_________________|
3227 //New situation with do_after():
3228 // ____________________________________________________
3229 //| old_begin | new_beg | old_end | raw_mem |
3230 //|___________|_______________|_________|______________|
3232 //New situation without do_after:
3233 // ______________________________________________________
3234 //| old_begin | new | old_end | raw_mem |
3235 //|___________|_____|_________|__________________________|
3237 //First copy whole old_begin and part of new to raw_mem
3238 T * const new_pos = ::boost::container::uninitialized_move_alloc
3239 (this->m_holder.alloc(), old_start, pos, new_start);
3240 this->m_holder.m_size = elemsbefore;
3241 const size_type mid_n = s_before - elemsbefore;
3242 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), new_pos, mid_n);
3243 //The buffer is all constructed until old_end,
3245 this->m_holder.m_size = old_size + s_before;
3246 old_values_destroyer.release();
3250 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), old_start, elemsbefore);
3253 //Copy all new elements
3254 const size_type rest_new = n - mid_n;
3255 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), old_start, rest_new);
3256 T* const move_start = old_start + rest_new;
3257 //Displace old_end, but make sure data has to be moved
3258 T* const move_end = move_start != pos ? ::boost::container::move(pos, old_finish, move_start)
3260 //Destroy remaining moved elements from old_end except if they
3261 //have trivial destructor after being moved
3262 size_type n_destroy = s_before - n;
3263 if(!value_traits::trivial_dctr_after_move)
3264 boost::container::destroy_alloc_n(this->get_stored_allocator(), move_end, n_destroy);
3265 this->m_holder.m_size -= n_destroy;
3269 //This is only executed if two phase construction is needed
3271 //The raw memory divides the new elements
3274 // ______________________________________________________
3275 //| raw_mem | old_begin | old_end | raw_mem |
3276 //|______________|___________|____________|______________|
3278 //New situation with do_after(1):
3279 // _______________________________________________________
3280 //| old_begin + new_beg | new_end |old_end | raw_mem |
3281 //|__________________________|_________|________|_________|
3283 //New situation with do_after(2):
3284 // ______________________________________________________
3285 //| old_begin + new | old_end |raw |
3286 //|_______________________________________|_________|____|
3288 const size_type n_after = n - s_before;
3289 const size_type elemsafter = old_size - elemsbefore;
3291 //We can have two situations:
3292 if (elemsafter >= n_after){
3293 //The raw_mem from end will divide displaced old_end
3296 // ______________________________________________________
3297 //| raw_mem | old_begin | old_end | raw_mem |
3298 //|______________|___________|____________|______________|
3300 //New situation with do_after(1):
3301 // _______________________________________________________
3302 //| old_begin + new_beg | new_end |old_end | raw_mem |
3303 //|__________________________|_________|________|_________|
3305 //First copy the part of old_end raw_mem
3306 T* finish_n = old_finish - n_after;
3307 ::boost::container::uninitialized_move_alloc
3308 (this->m_holder.alloc(), finish_n, old_finish, old_finish);
3309 this->m_holder.m_size += n_after;
3310 //Displace the rest of old_end to the new position
3311 boost::container::move_backward(pos, finish_n, old_finish);
3312 //Now overwrite with new_end
3313 //The new_end part is [first + (n - n_after), last)
3314 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, n_after);
3317 //The raw_mem from end will divide new_end part
3320 // _____________________________________________________________
3321 //| raw_mem | old_begin | old_end | raw_mem |
3322 //|______________|___________|____________|_____________________|
3324 //New situation with do_after(2):
3325 // _____________________________________________________________
3326 //| old_begin + new_beg | new_end |old_end | raw_mem |
3327 //|__________________________|_______________|________|_________|
3330 const size_type mid_last_dist = n_after - elemsafter;
3331 //First initialize data in raw memory
3333 //Copy to the old_end part to the uninitialized zone leaving a gap.
3334 ::boost::container::uninitialized_move_alloc
3335 (this->m_holder.alloc(), pos, old_finish, old_finish + mid_last_dist);
3337 typename value_traits::ArrayDestructor old_end_destroyer
3338 (old_finish + mid_last_dist, this->m_holder.alloc(), old_finish - pos);
3340 //Copy the first part to the already constructed old_end zone
3341 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, elemsafter);
3342 //Copy the rest to the uninitialized zone filling the gap
3343 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, mid_last_dist);
3344 this->m_holder.m_size += n_after;
3345 old_end_destroyer.release();
3351 void priv_throw_if_out_of_range(size_type n) const
3353 //If n is out of range, throw an out_of_range exception
3354 if (n >= this->size()){
3355 throw_out_of_range("vector::at out of range");
3359 bool priv_in_range(const_iterator pos) const
3361 return (this->begin() <= pos) && (pos < this->end());
3364 bool priv_in_range_or_end(const_iterator pos) const
3366 return (this->begin() <= pos) && (pos <= this->end());
3369 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
3371 unsigned int num_expand_fwd;
3372 unsigned int num_expand_bwd;
3373 unsigned int num_shrink;
3374 unsigned int num_alloc;
3375 void reset_alloc_stats()
3376 { num_expand_fwd = num_expand_bwd = num_alloc = 0, num_shrink = 0; }
3378 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3381 }} //namespace boost::container
3383 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3387 //!has_trivial_destructor_after_move<> == true_type
3388 //!specialization for optimizations
3389 template <class T, class Allocator>
3390 struct has_trivial_destructor_after_move<boost::container::vector<T, Allocator> >
3392 typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
3393 static const bool value = ::boost::has_trivial_destructor_after_move<Allocator>::value &&
3394 ::boost::has_trivial_destructor_after_move<pointer>::value;
3399 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3401 #include <boost/container/detail/config_end.hpp>
3403 #endif // #ifndef BOOST_CONTAINER_CONTAINER_VECTOR_HPP