1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2014. 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
18 #include <boost/container/detail/config_begin.hpp>
19 #include <boost/container/detail/workaround.hpp>
20 #include <boost/container/container_fwd.hpp>
22 //#include <cstddef> //Already included by container_fwd.hpp
23 #include <memory> //for std::allocator
24 #include <iterator> //for std::random_access_iterator_tag
25 #include <utility> //for std::pair,std::distance
26 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
27 #include <initializer_list> //for std::initializer_list
30 #include <boost/core/no_exceptions_support.hpp>
31 #include <boost/assert.hpp>
32 #include <boost/move/utility_core.hpp>
33 #include <boost/move/iterator.hpp>
34 #include <boost/move/algorithm.hpp>
35 #include <boost/move/detail/move_helpers.hpp>
36 #include <boost/move/traits.hpp>
38 #include <boost/container/detail/version_type.hpp>
39 #include <boost/container/detail/allocation_type.hpp>
40 #include <boost/container/detail/utilities.hpp>
41 #include <boost/container/detail/iterators.hpp>
42 #include <boost/container/detail/algorithms.hpp>
43 #include <boost/container/detail/destroyers.hpp>
44 #include <boost/container/allocator_traits.hpp>
45 #include <boost/container/detail/allocator_version_traits.hpp>
46 #include <boost/container/throw_exception.hpp>
47 #include <boost/container/detail/mpl.hpp>
48 #include <boost/container/detail/type_traits.hpp>
49 #include <boost/container/detail/advanced_insert_int.hpp>
51 #include <boost/intrusive/pointer_traits.hpp>
53 #include <boost/type_traits/has_trivial_destructor.hpp>
54 #include <boost/type_traits/has_trivial_copy.hpp>
55 #include <boost/type_traits/has_trivial_assign.hpp>
56 #include <boost/type_traits/has_nothrow_copy.hpp>
57 #include <boost/type_traits/has_nothrow_assign.hpp>
58 #include <boost/type_traits/has_nothrow_constructor.hpp>
63 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
65 //#define BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
67 namespace container_detail {
69 #ifndef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
71 template <class Pointer, bool IsConst>
75 typedef std::random_access_iterator_tag iterator_category;
76 typedef typename boost::intrusive::pointer_traits<Pointer>::element_type value_type;
77 typedef typename boost::intrusive::pointer_traits<Pointer>::difference_type difference_type;
80 , typename boost::intrusive::pointer_traits<Pointer>::template
81 rebind_pointer<const value_type>::type
84 typedef typename boost::intrusive::pointer_traits<Pointer> ptr_traits;
85 typedef typename ptr_traits::reference reference;
87 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
92 const Pointer &get_ptr() const BOOST_CONTAINER_NOEXCEPT
95 Pointer &get_ptr() BOOST_CONTAINER_NOEXCEPT
98 explicit vec_iterator(Pointer ptr) BOOST_CONTAINER_NOEXCEPT
101 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
106 vec_iterator() BOOST_CONTAINER_NOEXCEPT
107 : m_ptr() //Value initialization to achieve "null iterators" (N3644)
110 vec_iterator(vec_iterator<Pointer, false> const& other) BOOST_CONTAINER_NOEXCEPT
111 : m_ptr(other.get_ptr())
114 //Pointer like operators
115 reference operator*() const BOOST_CONTAINER_NOEXCEPT
118 pointer operator->() const BOOST_CONTAINER_NOEXCEPT
119 { return ::boost::intrusive::pointer_traits<pointer>::pointer_to(this->operator*()); }
121 reference operator[](difference_type off) const BOOST_CONTAINER_NOEXCEPT
122 { return m_ptr[off]; }
124 //Increment / Decrement
125 vec_iterator& operator++() BOOST_CONTAINER_NOEXCEPT
126 { ++m_ptr; return *this; }
128 vec_iterator operator++(int) BOOST_CONTAINER_NOEXCEPT
129 { return vec_iterator(m_ptr++); }
131 vec_iterator& operator--() BOOST_CONTAINER_NOEXCEPT
132 { --m_ptr; return *this; }
134 vec_iterator operator--(int) BOOST_CONTAINER_NOEXCEPT
135 { return vec_iterator(m_ptr--); }
138 vec_iterator& operator+=(difference_type off) BOOST_CONTAINER_NOEXCEPT
139 { m_ptr += off; return *this; }
141 vec_iterator& operator-=(difference_type off) BOOST_CONTAINER_NOEXCEPT
142 { m_ptr -= off; return *this; }
144 friend vec_iterator operator+(const vec_iterator &x, difference_type off) BOOST_CONTAINER_NOEXCEPT
145 { return vec_iterator(x.m_ptr+off); }
147 friend vec_iterator operator+(difference_type off, vec_iterator right) BOOST_CONTAINER_NOEXCEPT
148 { right.m_ptr += off; return right; }
150 friend vec_iterator operator-(vec_iterator left, difference_type off) BOOST_CONTAINER_NOEXCEPT
151 { left.m_ptr -= off; return left; }
153 friend difference_type operator-(const vec_iterator &left, const vec_iterator& right) BOOST_CONTAINER_NOEXCEPT
154 { return left.m_ptr - right.m_ptr; }
156 //Comparison operators
157 friend bool operator== (const vec_iterator& l, const vec_iterator& r) BOOST_CONTAINER_NOEXCEPT
158 { return l.m_ptr == r.m_ptr; }
160 friend bool operator!= (const vec_iterator& l, const vec_iterator& r) BOOST_CONTAINER_NOEXCEPT
161 { return l.m_ptr != r.m_ptr; }
163 friend bool operator< (const vec_iterator& l, const vec_iterator& r) BOOST_CONTAINER_NOEXCEPT
164 { return l.m_ptr < r.m_ptr; }
166 friend bool operator<= (const vec_iterator& l, const vec_iterator& r) BOOST_CONTAINER_NOEXCEPT
167 { return l.m_ptr <= r.m_ptr; }
169 friend bool operator> (const vec_iterator& l, const vec_iterator& r) BOOST_CONTAINER_NOEXCEPT
170 { return l.m_ptr > r.m_ptr; }
172 friend bool operator>= (const vec_iterator& l, const vec_iterator& r) BOOST_CONTAINER_NOEXCEPT
173 { return l.m_ptr >= r.m_ptr; }
176 } //namespace container_detail {
178 template<class Pointer, bool IsConst>
179 const Pointer &vector_iterator_get_ptr(const container_detail::vec_iterator<Pointer, IsConst> &it) BOOST_CONTAINER_NOEXCEPT
180 { return it.get_ptr(); }
182 template<class Pointer, bool IsConst>
183 Pointer &get_ptr(container_detail::vec_iterator<Pointer, IsConst> &it) BOOST_CONTAINER_NOEXCEPT
184 { return it.get_ptr(); }
186 namespace container_detail {
188 #else //ifndef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
190 template< class MaybeConstPointer
191 , bool ElementTypeIsConst
192 = is_const< typename boost::intrusive::pointer_traits<MaybeConstPointer>::element_type>::value >
193 struct vector_get_ptr_pointer_to_non_const
195 typedef MaybeConstPointer const_pointer;
196 typedef boost::intrusive::pointer_traits<const_pointer> pointer_traits_t;
197 typedef typename pointer_traits_t::element_type element_type;
198 typedef typename remove_const<element_type>::type non_const_element_type;
199 typedef typename pointer_traits_t
200 ::template rebind_pointer<non_const_element_type>::type return_type;
202 static return_type get_ptr(const const_pointer &ptr) BOOST_CONTAINER_NOEXCEPT
203 { return boost::intrusive::pointer_traits<return_type>::const_cast_from(ptr); }
206 template<class Pointer>
207 struct vector_get_ptr_pointer_to_non_const<Pointer, false>
209 typedef const Pointer & return_type;
210 static return_type get_ptr(const Pointer &ptr) BOOST_CONTAINER_NOEXCEPT
214 } //namespace container_detail {
216 template<class MaybeConstPointer>
217 typename container_detail::vector_get_ptr_pointer_to_non_const<MaybeConstPointer>::return_type
218 vector_iterator_get_ptr(const MaybeConstPointer &ptr) BOOST_CONTAINER_NOEXCEPT
220 return container_detail::vector_get_ptr_pointer_to_non_const<MaybeConstPointer>::get_ptr(ptr);
223 namespace container_detail {
225 #endif //#ifndef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
227 struct uninitialized_size_t {};
228 static const uninitialized_size_t uninitialized_size = uninitialized_size_t();
231 struct vector_value_traits_base
233 static const bool trivial_dctr = boost::has_trivial_destructor<T>::value;
234 static const bool trivial_dctr_after_move = ::boost::has_trivial_destructor_after_move<T>::value;
235 static const bool trivial_copy = has_trivial_copy<T>::value;
236 static const bool nothrow_copy = has_nothrow_copy<T>::value || trivial_copy;
237 static const bool trivial_assign = has_trivial_assign<T>::value;
238 static const bool nothrow_assign = has_nothrow_assign<T>::value || trivial_assign;
242 template <class Allocator>
243 struct vector_value_traits
244 : public vector_value_traits_base<typename Allocator::value_type>
246 typedef vector_value_traits_base<typename Allocator::value_type> base_t;
247 //This is the anti-exception array destructor
248 //to deallocate values already constructed
249 typedef typename container_detail::if_c
250 <base_t::trivial_dctr
251 ,container_detail::null_scoped_destructor_n<Allocator>
252 ,container_detail::scoped_destructor_n<Allocator>
253 >::type ArrayDestructor;
254 //This is the anti-exception array deallocator
255 typedef container_detail::scoped_array_deallocator<Allocator> ArrayDeallocator;
258 //!This struct deallocates and allocated memory
259 template < class Allocator
260 , class AllocatorVersion = typename container_detail::version<Allocator>::type
262 struct vector_alloc_holder
266 BOOST_MOVABLE_BUT_NOT_COPYABLE(vector_alloc_holder)
269 typedef boost::container::allocator_traits<Allocator> allocator_traits_type;
270 typedef typename allocator_traits_type::pointer pointer;
271 typedef typename allocator_traits_type::size_type size_type;
272 typedef typename allocator_traits_type::value_type value_type;
274 //Constructor, does not throw
275 vector_alloc_holder()
276 BOOST_CONTAINER_NOEXCEPT_IF(::boost::has_nothrow_default_constructor<Allocator>::value)
277 : Allocator(), m_start(), m_size(), m_capacity()
280 //Constructor, does not throw
281 template<class AllocConvertible>
282 explicit vector_alloc_holder(BOOST_FWD_REF(AllocConvertible) a) BOOST_CONTAINER_NOEXCEPT
283 : Allocator(boost::forward<AllocConvertible>(a)), m_start(), m_size(), m_capacity()
286 //Constructor, does not throw
287 template<class AllocConvertible>
288 vector_alloc_holder(uninitialized_size_t, BOOST_FWD_REF(AllocConvertible) a, size_type initial_size)
289 : Allocator(boost::forward<AllocConvertible>(a))
291 , m_size(initial_size) //Size is initialized here so vector should only call uninitialized_xxx after this
295 m_start = this->allocation_command(allocate_new, initial_size, initial_size, m_capacity, m_start).first;
299 //Constructor, does not throw
300 vector_alloc_holder(uninitialized_size_t, size_type initial_size)
303 , m_size(initial_size) //Size is initialized here so vector should only call uninitialized_xxx after this
307 m_start = this->allocation_command
308 (allocate_new, initial_size, initial_size, m_capacity, m_start).first;
312 vector_alloc_holder(BOOST_RV_REF(vector_alloc_holder) holder) BOOST_CONTAINER_NOEXCEPT
313 : Allocator(boost::move(static_cast<Allocator&>(holder)))
314 , m_start(holder.m_start)
315 , m_size(holder.m_size)
316 , m_capacity(holder.m_capacity)
318 holder.m_start = pointer();
319 holder.m_size = holder.m_capacity = 0;
322 void first_allocation(size_type cap)
325 m_start = this->allocation_command
326 (allocate_new, cap, cap, m_capacity, m_start).first;
327 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
333 void first_allocation_same_allocator_type(size_type cap)
334 { this->first_allocation(cap); }
336 ~vector_alloc_holder() BOOST_CONTAINER_NOEXCEPT
338 if(this->m_capacity){
339 this->alloc().deallocate(this->m_start, this->m_capacity);
343 std::pair<pointer, bool>
344 allocation_command(boost::container::allocation_type command,
345 size_type limit_size,
346 size_type preferred_size,
347 size_type &received_size, const pointer &reuse = pointer())
349 return allocator_version_traits<Allocator>::allocation_command
350 (this->alloc(), command, limit_size, preferred_size, received_size, reuse);
353 size_type next_capacity(size_type additional_objects) const
355 return next_capacity_calculator
356 <size_type, NextCapacityDouble //NextCapacity60Percent
357 >::get( allocator_traits_type::max_size(this->alloc())
358 , this->m_capacity, additional_objects );
363 size_type m_capacity;
365 void swap(vector_alloc_holder &x) BOOST_CONTAINER_NOEXCEPT
367 boost::container::swap_dispatch(this->m_start, x.m_start);
368 boost::container::swap_dispatch(this->m_size, x.m_size);
369 boost::container::swap_dispatch(this->m_capacity, x.m_capacity);
372 void move_from_empty(vector_alloc_holder &x) BOOST_CONTAINER_NOEXCEPT
374 //this->m_size was previously initialized
375 this->m_start = x.m_start;
376 this->m_capacity = x.m_capacity;
377 x.m_start = pointer();
378 x.m_size = x.m_capacity = 0;
381 Allocator &alloc() BOOST_CONTAINER_NOEXCEPT
384 const Allocator &alloc() const BOOST_CONTAINER_NOEXCEPT
387 const pointer &start() const BOOST_CONTAINER_NOEXCEPT { return m_start; }
388 const size_type &capacity() const BOOST_CONTAINER_NOEXCEPT { return m_capacity; }
389 void start(const pointer &p) BOOST_CONTAINER_NOEXCEPT { m_start = p; }
390 void capacity(const size_type &c) BOOST_CONTAINER_NOEXCEPT { m_capacity = c; }
393 //!This struct deallocates and allocated memory
394 template <class Allocator>
395 struct vector_alloc_holder<Allocator, container_detail::integral_constant<unsigned, 0> >
399 BOOST_MOVABLE_BUT_NOT_COPYABLE(vector_alloc_holder)
402 typedef boost::container::allocator_traits<Allocator> allocator_traits_type;
403 typedef typename allocator_traits_type::pointer pointer;
404 typedef typename allocator_traits_type::size_type size_type;
405 typedef typename allocator_traits_type::value_type value_type;
407 template <class OtherAllocator, class OtherAllocatorVersion>
408 friend struct vector_alloc_holder;
410 //Constructor, does not throw
411 vector_alloc_holder()
412 BOOST_CONTAINER_NOEXCEPT_IF(::boost::has_nothrow_default_constructor<Allocator>::value)
413 : Allocator(), m_size()
416 //Constructor, does not throw
417 template<class AllocConvertible>
418 explicit vector_alloc_holder(BOOST_FWD_REF(AllocConvertible) a) BOOST_CONTAINER_NOEXCEPT
419 : Allocator(boost::forward<AllocConvertible>(a)), m_size()
422 //Constructor, does not throw
423 template<class AllocConvertible>
424 vector_alloc_holder(uninitialized_size_t, BOOST_FWD_REF(AllocConvertible) a, size_type initial_size)
425 : Allocator(boost::forward<AllocConvertible>(a))
426 , m_size(initial_size) //Size is initialized here...
428 //... and capacity here, so vector, must call uninitialized_xxx in the derived constructor
429 this->first_allocation(initial_size);
432 //Constructor, does not throw
433 vector_alloc_holder(uninitialized_size_t, size_type initial_size)
435 , m_size(initial_size) //Size is initialized here...
437 //... and capacity here, so vector, must call uninitialized_xxx in the derived constructor
438 this->first_allocation(initial_size);
441 vector_alloc_holder(BOOST_RV_REF(vector_alloc_holder) holder)
442 : Allocator(boost::move(static_cast<Allocator&>(holder)))
443 , m_size(holder.m_size) //Size is initialized here so vector should only call uninitialized_xxx after this
445 ::boost::container::uninitialized_move_alloc_n
446 (this->alloc(), container_detail::to_raw_pointer(holder.start()), m_size, container_detail::to_raw_pointer(this->start()));
449 template<class OtherAllocator, class OtherAllocatorVersion>
450 vector_alloc_holder(BOOST_RV_REF_BEG vector_alloc_holder<OtherAllocator, OtherAllocatorVersion> BOOST_RV_REF_END holder)
452 , m_size(holder.m_size) //Initialize it to m_size as first_allocation can only succeed or abort
454 //Different allocator type so we must check we have enough storage
455 const size_type n = holder.m_size;
456 this->first_allocation(n);
457 ::boost::container::uninitialized_move_alloc_n
458 (this->alloc(), container_detail::to_raw_pointer(holder.start()), n, container_detail::to_raw_pointer(this->start()));
461 void first_allocation(size_type cap)
463 if(cap > Allocator::internal_capacity){
468 void first_allocation_same_allocator_type(size_type) BOOST_CONTAINER_NOEXCEPT
472 ~vector_alloc_holder() BOOST_CONTAINER_NOEXCEPT
475 void swap(vector_alloc_holder &x)
477 this->priv_swap_members_impl(x);
480 template<class OtherAllocator, class OtherAllocatorVersion>
481 void swap(vector_alloc_holder<OtherAllocator, OtherAllocatorVersion> &x)
483 if(this->m_size > OtherAllocator::internal_capacity || x.m_size > Allocator::internal_capacity){
486 this->priv_swap_members_impl(x);
489 void move_from_empty(vector_alloc_holder &)
490 { //Containers with version 0 allocators can't be moved without move elements one by one
494 Allocator &alloc() BOOST_CONTAINER_NOEXCEPT
497 const Allocator &alloc() const BOOST_CONTAINER_NOEXCEPT
500 pointer start() const BOOST_CONTAINER_NOEXCEPT { return Allocator::internal_storage(); }
501 size_type capacity() const BOOST_CONTAINER_NOEXCEPT { return Allocator::internal_capacity; }
506 template<class OtherAllocator, class OtherAllocatorVersion>
507 void priv_swap_members_impl(vector_alloc_holder<OtherAllocator, OtherAllocatorVersion> &x)
509 const std::size_t MaxTmpStorage = sizeof(value_type)*Allocator::internal_capacity;
510 value_type *const first_this = container_detail::to_raw_pointer(this->start());
511 value_type *const first_x = container_detail::to_raw_pointer(x.start());
513 if(this->m_size < x.m_size){
514 boost::container::deep_swap_alloc_n<MaxTmpStorage>(this->alloc(), first_this, this->m_size, first_x, x.m_size);
517 boost::container::deep_swap_alloc_n<MaxTmpStorage>(this->alloc(), first_x, x.m_size, first_this, this->m_size);
519 boost::container::swap_dispatch(this->m_size, x.m_size);
523 } //namespace container_detail {
525 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
527 //! A vector is a sequence that supports random access to elements, constant
528 //! time insertion and removal of elements at the end, and linear time insertion
529 //! and removal of elements at the beginning or in the middle. The number of
530 //! elements in a vector may vary dynamically; memory management is automatic.
532 //! \tparam T The type of object that is stored in the vector
533 //! \tparam Allocator The allocator used for all internal memory management
534 template <class T, class Allocator BOOST_CONTAINER_DOCONLY(= std::allocator<T>) >
537 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
539 typedef typename container_detail::version<Allocator>::type alloc_version;
540 boost::container::container_detail::vector_alloc_holder
541 <Allocator, alloc_version> m_holder;
542 typedef allocator_traits<Allocator> allocator_traits_type;
543 template <class U, class UAllocator>
546 typedef typename ::boost::container::allocator_traits
547 <Allocator>::pointer pointer_impl;
548 typedef container_detail::vec_iterator<pointer_impl, false> iterator_impl;
549 typedef container_detail::vec_iterator<pointer_impl, true > const_iterator_impl;
551 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
553 //////////////////////////////////////////////
557 //////////////////////////////////////////////
559 typedef T value_type;
560 typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
561 typedef typename ::boost::container::allocator_traits<Allocator>::const_pointer const_pointer;
562 typedef typename ::boost::container::allocator_traits<Allocator>::reference reference;
563 typedef typename ::boost::container::allocator_traits<Allocator>::const_reference const_reference;
564 typedef typename ::boost::container::allocator_traits<Allocator>::size_type size_type;
565 typedef typename ::boost::container::allocator_traits<Allocator>::difference_type difference_type;
566 typedef Allocator allocator_type;
567 typedef Allocator stored_allocator_type;
568 #if defined BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
569 typedef BOOST_CONTAINER_IMPDEF(pointer) iterator;
570 typedef BOOST_CONTAINER_IMPDEF(const_pointer) const_iterator;
572 typedef BOOST_CONTAINER_IMPDEF(iterator_impl) iterator;
573 typedef BOOST_CONTAINER_IMPDEF(const_iterator_impl) const_iterator;
575 typedef BOOST_CONTAINER_IMPDEF(container_detail::reverse_iterator<iterator>) reverse_iterator;
576 typedef BOOST_CONTAINER_IMPDEF(container_detail::reverse_iterator<const_iterator>) const_reverse_iterator;
578 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
580 BOOST_COPYABLE_AND_MOVABLE(vector)
581 typedef container_detail::vector_value_traits<Allocator> value_traits;
583 typedef container_detail::integral_constant<unsigned, 0> allocator_v0;
584 typedef container_detail::integral_constant<unsigned, 1> allocator_v1;
585 typedef container_detail::integral_constant<unsigned, 2> allocator_v2;
587 typedef constant_iterator<T, difference_type> cvalue_iterator;
588 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
591 //////////////////////////////////////////////
593 // construct/copy/destroy
595 //////////////////////////////////////////////
597 //! <b>Effects</b>: Constructs a vector taking the allocator as parameter.
599 //! <b>Throws</b>: If allocator_type's default constructor throws.
601 //! <b>Complexity</b>: Constant.
603 BOOST_CONTAINER_NOEXCEPT_IF(::boost::has_nothrow_default_constructor<Allocator>::value)
607 //! <b>Effects</b>: Constructs a vector taking the allocator as parameter.
609 //! <b>Throws</b>: Nothing
611 //! <b>Complexity</b>: Constant.
612 explicit vector(const Allocator& a) BOOST_CONTAINER_NOEXCEPT
616 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
617 //! and inserts n value initialized values.
619 //! <b>Throws</b>: If allocator_type's default constructor or allocation
620 //! throws or T's value initialization throws.
622 //! <b>Complexity</b>: Linear to n.
623 explicit vector(size_type n)
624 : m_holder(container_detail::uninitialized_size, n)
626 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
627 this->num_alloc += n != 0;
629 boost::container::uninitialized_value_init_alloc_n
630 (this->m_holder.alloc(), n, container_detail::to_raw_pointer(this->m_holder.start()));
633 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
634 //! and inserts n default initialized values.
636 //! <b>Throws</b>: If allocator_type's default constructor or allocation
637 //! throws or T's default initialization throws.
639 //! <b>Complexity</b>: Linear to n.
641 //! <b>Note</b>: Non-standard extension
642 vector(size_type n, default_init_t)
643 : m_holder(container_detail::uninitialized_size, n)
645 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
646 this->num_alloc += n != 0;
648 boost::container::uninitialized_default_init_alloc_n
649 (this->m_holder.alloc(), n, container_detail::to_raw_pointer(this->m_holder.start()));
652 //! <b>Effects</b>: Constructs a vector
653 //! and inserts n copies of value.
655 //! <b>Throws</b>: If allocator_type's default constructor or allocation
656 //! throws or T's copy constructor throws.
658 //! <b>Complexity</b>: Linear to n.
659 vector(size_type n, const T& value)
660 : m_holder(container_detail::uninitialized_size, n)
662 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
663 this->num_alloc += n != 0;
665 boost::container::uninitialized_fill_alloc_n
666 (this->m_holder.alloc(), value, n, container_detail::to_raw_pointer(this->m_holder.start()));
669 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
670 //! and inserts n copies of value.
672 //! <b>Throws</b>: If allocation
673 //! throws or T's copy constructor throws.
675 //! <b>Complexity</b>: Linear to n.
676 vector(size_type n, const T& value, const allocator_type& a)
677 : m_holder(container_detail::uninitialized_size, a, n)
679 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
680 this->num_alloc += n != 0;
682 boost::container::uninitialized_fill_alloc_n
683 (this->m_holder.alloc(), value, n, container_detail::to_raw_pointer(this->m_holder.start()));
686 //! <b>Effects</b>: Constructs a vector
687 //! and inserts a copy of the range [first, last) in the vector.
689 //! <b>Throws</b>: If allocator_type's default constructor or allocation
690 //! throws or T's constructor taking a dereferenced InIt throws.
692 //! <b>Complexity</b>: Linear to the range [first, last).
693 template <class InIt>
694 vector(InIt first, InIt last)
696 { this->insert(this->cend(), first, last); }
698 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
699 //! and inserts a copy of the range [first, last) in the vector.
701 //! <b>Throws</b>: If allocator_type's default constructor or allocation
702 //! throws or T's constructor taking a dereferenced InIt throws.
704 //! <b>Complexity</b>: Linear to the range [first, last).
705 template <class InIt>
706 vector(InIt first, InIt last, const allocator_type& a)
708 { this->insert(this->cend(), first, last); }
710 //! <b>Effects</b>: Copy constructs a vector.
712 //! <b>Postcondition</b>: x == *this.
714 //! <b>Throws</b>: If allocator_type's default constructor or allocation
715 //! throws or T's copy constructor throws.
717 //! <b>Complexity</b>: Linear to the elements x contains.
718 vector(const vector &x)
719 : m_holder( container_detail::uninitialized_size
720 , allocator_traits_type::select_on_container_copy_construction(x.m_holder.alloc())
723 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
724 this->num_alloc += x.size() != 0;
726 ::boost::container::uninitialized_copy_alloc_n
727 ( this->m_holder.alloc(), container_detail::to_raw_pointer(x.m_holder.start())
728 , x.size(), container_detail::to_raw_pointer(this->m_holder.start()));
731 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
732 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
733 //! and inserts a copy of the range [il.begin(), il.last()) in the vector
735 //! <b>Throws</b>: If allocator_type's default constructor
736 //! throws or T's constructor taking a dereferenced initializer_list iterator throws.
738 //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
739 vector(std::initializer_list<value_type> il, const allocator_type& a = allocator_type())
742 insert(cend(), il.begin(), il.end());
747 //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
749 //! <b>Throws</b>: Nothing
751 //! <b>Complexity</b>: Constant.
752 vector(BOOST_RV_REF(vector) x) BOOST_CONTAINER_NOEXCEPT
753 : m_holder(boost::move(x.m_holder))
756 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
758 //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
760 //! <b>Throws</b>: If T's move constructor or allocation throws
762 //! <b>Complexity</b>: Linear.
764 //! <b>Note</b>: Non-standard extension to support static_vector
765 template<class OtherAllocator>
766 vector(BOOST_RV_REF_BEG vector<T, OtherAllocator> BOOST_RV_REF_END x
767 , typename container_detail::enable_if_c
768 < container_detail::is_version<OtherAllocator, 0>::value>::type * = 0
770 : m_holder(boost::move(x.m_holder))
773 #endif //!defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
775 //! <b>Effects</b>: Copy constructs a vector using the specified allocator.
777 //! <b>Postcondition</b>: x == *this.
779 //! <b>Throws</b>: If allocation
780 //! throws or T's copy constructor throws.
782 //! <b>Complexity</b>: Linear to the elements x contains.
783 vector(const vector &x, const allocator_type &a)
784 : m_holder(container_detail::uninitialized_size, a, x.size())
786 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
787 this->num_alloc += x.size() != 0;
789 ::boost::container::uninitialized_copy_alloc_n_source
790 ( this->m_holder.alloc(), container_detail::to_raw_pointer(x.m_holder.start())
791 , x.size(), container_detail::to_raw_pointer(this->m_holder.start()));
794 //! <b>Effects</b>: Move constructor using the specified allocator.
795 //! Moves x's resources to *this if a == allocator_type().
796 //! Otherwise copies values from x to *this.
798 //! <b>Throws</b>: If allocation or T's copy constructor throws.
800 //! <b>Complexity</b>: Constant if a == x.get_allocator(), linear otherwise.
801 vector(BOOST_RV_REF(vector) x, const allocator_type &a)
802 : m_holder(container_detail::uninitialized_size, a, x.size())
804 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
805 this->num_alloc += x.size() != 0;
807 if(x.m_holder.alloc() == a){
808 this->m_holder.move_from_empty(x.m_holder);
811 const size_type n = x.size();
812 this->m_holder.first_allocation_same_allocator_type(n);
813 ::boost::container::uninitialized_move_alloc_n_source
814 ( this->m_holder.alloc(), container_detail::to_raw_pointer(x.m_holder.start())
815 , n, container_detail::to_raw_pointer(this->m_holder.start()));
819 //! <b>Effects</b>: Destroys the vector. All stored values are destroyed
820 //! and used memory is deallocated.
822 //! <b>Throws</b>: Nothing.
824 //! <b>Complexity</b>: Linear to the number of elements.
825 ~vector() BOOST_CONTAINER_NOEXCEPT
827 boost::container::destroy_alloc_n
828 (this->get_stored_allocator(), container_detail::to_raw_pointer(this->m_holder.start()), this->m_holder.m_size);
829 //vector_alloc_holder deallocates the data
832 //! <b>Effects</b>: Makes *this contain the same elements as x.
834 //! <b>Postcondition</b>: this->size() == x.size(). *this contains a copy
835 //! of each of x's elements.
837 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment throws.
839 //! <b>Complexity</b>: Linear to the number of elements in x.
840 vector& operator=(BOOST_COPY_ASSIGN_REF(vector) x)
843 this->priv_copy_assign(x);
848 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
849 //! <b>Effects</b>: Make *this container contains elements from il.
851 //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
852 vector& operator=(std::initializer_list<value_type> il)
854 assign(il.begin(), il.end());
859 //! <b>Effects</b>: Move assignment. All x's values are transferred to *this.
861 //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
862 //! before the function.
864 //! <b>Throws</b>: If allocator_traits_type::propagate_on_container_move_assignment
865 //! is false and (allocation throws or value_type's move constructor throws)
867 //! <b>Complexity</b>: Constant if allocator_traits_type::
868 //! propagate_on_container_move_assignment is true or
869 //! this->get>allocator() == x.get_allocator(). Linear otherwise.
870 vector& operator=(BOOST_RV_REF(vector) x)
871 BOOST_CONTAINER_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value)
873 this->priv_move_assign(boost::move(x));
877 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
879 //! <b>Effects</b>: Move assignment. All x's values are transferred to *this.
881 //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
882 //! before the function.
884 //! <b>Throws</b>: If move constructor/assignment of T throws or allocation throws
886 //! <b>Complexity</b>: Linear.
888 //! <b>Note</b>: Non-standard extension to support static_vector
889 template<class OtherAllocator>
890 typename container_detail::enable_if_c
891 < container_detail::is_version<OtherAllocator, 0>::value &&
892 !container_detail::is_same<OtherAllocator, allocator_type>::value
894 operator=(BOOST_RV_REF_BEG vector<value_type, OtherAllocator> BOOST_RV_REF_END x)
896 this->priv_move_assign(boost::move(x));
900 //! <b>Effects</b>: Copy assignment. All x's values are copied to *this.
902 //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
903 //! before the function.
905 //! <b>Throws</b>: If move constructor/assignment of T throws or allocation throws
907 //! <b>Complexity</b>: Linear.
909 //! <b>Note</b>: Non-standard extension to support static_vector
910 template<class OtherAllocator>
911 typename container_detail::enable_if_c
912 < container_detail::is_version<OtherAllocator, 0>::value &&
913 !container_detail::is_same<OtherAllocator, allocator_type>::value
915 operator=(const vector<value_type, OtherAllocator> &x)
917 this->priv_copy_assign(x);
923 //! <b>Effects</b>: Assigns the the range [first, last) to *this.
925 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment or
926 //! T's constructor/assignment from dereferencing InpIt throws.
928 //! <b>Complexity</b>: Linear to n.
929 template <class InIt>
930 void assign(InIt first, InIt last
931 BOOST_CONTAINER_DOCIGN(BOOST_CONTAINER_I typename container_detail::enable_if_c
932 < !container_detail::is_convertible<InIt BOOST_CONTAINER_I size_type>::value &&
933 ( container_detail::is_input_iterator<InIt>::value ||
934 container_detail::is_same<alloc_version BOOST_CONTAINER_I allocator_v0>::value )
937 //Overwrite all elements we can from [first, last)
938 iterator cur = this->begin();
939 const iterator end_it = this->end();
940 for ( ; first != last && cur != end_it; ++cur, ++first){
945 //There are no more elements in the sequence, erase remaining
946 T* const end_pos = container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size;
947 const size_type n = static_cast<size_type>(end_pos - container_detail::iterator_to_raw_pointer(cur));
948 this->priv_destroy_last_n(n);
951 //There are more elements in the range, insert the remaining ones
952 this->insert(this->cend(), first, last);
956 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
957 //! <b>Effects</b>: Assigns the the range [il.begin(), il.end()) to *this.
959 //! <b>Throws</b>: If memory allocation throws or
960 //! T's constructor from dereferencing iniializer_list iterator throws.
962 void assign(std::initializer_list<T> il)
964 assign(il.begin(), il.end());
968 //! <b>Effects</b>: Assigns the the range [first, last) to *this.
970 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment or
971 //! T's constructor/assignment from dereferencing InpIt throws.
973 //! <b>Complexity</b>: Linear to n.
974 template <class FwdIt>
975 void assign(FwdIt first, FwdIt last
976 BOOST_CONTAINER_DOCIGN(BOOST_CONTAINER_I typename container_detail::enable_if_c
977 < !container_detail::is_convertible<FwdIt BOOST_CONTAINER_I size_type>::value &&
978 ( !container_detail::is_input_iterator<FwdIt>::value &&
979 !container_detail::is_same<alloc_version BOOST_CONTAINER_I allocator_v0>::value )
983 //For Fwd iterators the standard only requires EmplaceConstructible and assignable from *first
984 //so we can't do any backwards allocation
985 const size_type input_sz = static_cast<size_type>(std::distance(first, last));
986 const size_type old_capacity = this->capacity();
987 if(input_sz > old_capacity){ //If input range is too big, we need to reallocate
988 size_type real_cap = 0;
989 std::pair<pointer, bool> ret =
990 this->m_holder.allocation_command(allocate_new|expand_fwd, input_sz, input_sz, real_cap, this->m_holder.start());
991 if(!ret.second){ //New allocation, just emplace new values
992 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
995 pointer const old_p = this->m_holder.start();
997 this->priv_destroy_all();
998 this->m_holder.alloc().deallocate(old_p, old_capacity);
1000 this->m_holder.start(ret.first);
1001 this->m_holder.capacity(real_cap);
1002 this->m_holder.m_size = 0;
1003 this->priv_uninitialized_construct_at_end(first, last);
1007 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
1008 ++this->num_expand_fwd;
1010 this->m_holder.capacity(real_cap);
1011 //Forward expansion, use assignment + back deletion/construction that comes later
1014 //Overwrite all elements we can from [first, last)
1015 iterator cur = this->begin();
1016 const iterator end_it = this->end();
1017 for ( ; first != last && cur != end_it; ++cur, ++first){
1022 //There are no more elements in the sequence, erase remaining
1023 this->priv_destroy_last_n(this->size() - input_sz);
1026 //Uninitialized construct at end the remaining range
1027 this->priv_uninitialized_construct_at_end(first, last);
1031 //! <b>Effects</b>: Assigns the n copies of val to *this.
1033 //! <b>Throws</b>: If memory allocation throws or
1034 //! T's copy/move constructor/assignment throws.
1036 //! <b>Complexity</b>: Linear to n.
1037 void assign(size_type n, const value_type& val)
1038 { this->assign(cvalue_iterator(val, n), cvalue_iterator()); }
1040 //! <b>Effects</b>: Returns a copy of the internal allocator.
1042 //! <b>Throws</b>: If allocator's copy constructor throws.
1044 //! <b>Complexity</b>: Constant.
1045 allocator_type get_allocator() const BOOST_CONTAINER_NOEXCEPT
1046 { return this->m_holder.alloc(); }
1048 //! <b>Effects</b>: Returns a reference to the internal allocator.
1050 //! <b>Throws</b>: Nothing
1052 //! <b>Complexity</b>: Constant.
1054 //! <b>Note</b>: Non-standard extension.
1055 stored_allocator_type &get_stored_allocator() BOOST_CONTAINER_NOEXCEPT
1056 { return this->m_holder.alloc(); }
1058 //! <b>Effects</b>: Returns a reference to the internal allocator.
1060 //! <b>Throws</b>: Nothing
1062 //! <b>Complexity</b>: Constant.
1064 //! <b>Note</b>: Non-standard extension.
1065 const stored_allocator_type &get_stored_allocator() const BOOST_CONTAINER_NOEXCEPT
1066 { return this->m_holder.alloc(); }
1068 //////////////////////////////////////////////
1072 //////////////////////////////////////////////
1074 //! <b>Effects</b>: Returns an iterator to the first element contained in the vector.
1076 //! <b>Throws</b>: Nothing.
1078 //! <b>Complexity</b>: Constant.
1079 iterator begin() BOOST_CONTAINER_NOEXCEPT
1080 { return iterator(this->m_holder.start()); }
1082 //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
1084 //! <b>Throws</b>: Nothing.
1086 //! <b>Complexity</b>: Constant.
1087 const_iterator begin() const BOOST_CONTAINER_NOEXCEPT
1088 { return const_iterator(this->m_holder.start()); }
1090 //! <b>Effects</b>: Returns an iterator to the end of the vector.
1092 //! <b>Throws</b>: Nothing.
1094 //! <b>Complexity</b>: Constant.
1095 iterator end() BOOST_CONTAINER_NOEXCEPT
1096 { return iterator(this->m_holder.start() + this->m_holder.m_size); }
1098 //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
1100 //! <b>Throws</b>: Nothing.
1102 //! <b>Complexity</b>: Constant.
1103 const_iterator end() const BOOST_CONTAINER_NOEXCEPT
1104 { return this->cend(); }
1106 //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
1107 //! of the reversed vector.
1109 //! <b>Throws</b>: Nothing.
1111 //! <b>Complexity</b>: Constant.
1112 reverse_iterator rbegin() BOOST_CONTAINER_NOEXCEPT
1113 { return reverse_iterator(this->end()); }
1115 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
1116 //! of the reversed vector.
1118 //! <b>Throws</b>: Nothing.
1120 //! <b>Complexity</b>: Constant.
1121 const_reverse_iterator rbegin() const BOOST_CONTAINER_NOEXCEPT
1122 { return this->crbegin(); }
1124 //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
1125 //! of the reversed vector.
1127 //! <b>Throws</b>: Nothing.
1129 //! <b>Complexity</b>: Constant.
1130 reverse_iterator rend() BOOST_CONTAINER_NOEXCEPT
1131 { return reverse_iterator(this->begin()); }
1133 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
1134 //! of the reversed vector.
1136 //! <b>Throws</b>: Nothing.
1138 //! <b>Complexity</b>: Constant.
1139 const_reverse_iterator rend() const BOOST_CONTAINER_NOEXCEPT
1140 { return this->crend(); }
1142 //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
1144 //! <b>Throws</b>: Nothing.
1146 //! <b>Complexity</b>: Constant.
1147 const_iterator cbegin() const BOOST_CONTAINER_NOEXCEPT
1148 { return const_iterator(this->m_holder.start()); }
1150 //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
1152 //! <b>Throws</b>: Nothing.
1154 //! <b>Complexity</b>: Constant.
1155 const_iterator cend() const BOOST_CONTAINER_NOEXCEPT
1156 { return const_iterator(this->m_holder.start() + this->m_holder.m_size); }
1158 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
1159 //! of the reversed vector.
1161 //! <b>Throws</b>: Nothing.
1163 //! <b>Complexity</b>: Constant.
1164 const_reverse_iterator crbegin() const BOOST_CONTAINER_NOEXCEPT
1165 { return const_reverse_iterator(this->end());}
1167 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
1168 //! of the reversed vector.
1170 //! <b>Throws</b>: Nothing.
1172 //! <b>Complexity</b>: Constant.
1173 const_reverse_iterator crend() const BOOST_CONTAINER_NOEXCEPT
1174 { return const_reverse_iterator(this->begin()); }
1176 //////////////////////////////////////////////
1180 //////////////////////////////////////////////
1182 //! <b>Effects</b>: Returns true if the vector contains no elements.
1184 //! <b>Throws</b>: Nothing.
1186 //! <b>Complexity</b>: Constant.
1187 bool empty() const BOOST_CONTAINER_NOEXCEPT
1188 { return !this->m_holder.m_size; }
1190 //! <b>Effects</b>: Returns the number of the elements contained in the vector.
1192 //! <b>Throws</b>: Nothing.
1194 //! <b>Complexity</b>: Constant.
1195 size_type size() const BOOST_CONTAINER_NOEXCEPT
1196 { return this->m_holder.m_size; }
1198 //! <b>Effects</b>: Returns the largest possible size of the vector.
1200 //! <b>Throws</b>: Nothing.
1202 //! <b>Complexity</b>: Constant.
1203 size_type max_size() const BOOST_CONTAINER_NOEXCEPT
1204 { return allocator_traits_type::max_size(this->m_holder.alloc()); }
1206 //! <b>Effects</b>: Inserts or erases elements at the end such that
1207 //! the size becomes n. New elements are value initialized.
1209 //! <b>Throws</b>: If memory allocation throws, or T's copy/move or value initialization throws.
1211 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1212 void resize(size_type new_size)
1213 { this->priv_resize(new_size, value_init); }
1215 //! <b>Effects</b>: Inserts or erases elements at the end such that
1216 //! the size becomes n. New elements are default initialized.
1218 //! <b>Throws</b>: If memory allocation throws, or T's copy/move or default initialization throws.
1220 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1222 //! <b>Note</b>: Non-standard extension
1223 void resize(size_type new_size, default_init_t)
1224 { this->priv_resize(new_size, default_init); }
1226 //! <b>Effects</b>: Inserts or erases elements at the end such that
1227 //! the size becomes n. New elements are copy constructed from x.
1229 //! <b>Throws</b>: If memory allocation throws, or T's copy/move constructor throws.
1231 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1232 void resize(size_type new_size, const T& x)
1233 { this->priv_resize(new_size, x); }
1235 //! <b>Effects</b>: Number of elements for which memory has been allocated.
1236 //! capacity() is always greater than or equal to size().
1238 //! <b>Throws</b>: Nothing.
1240 //! <b>Complexity</b>: Constant.
1241 size_type capacity() const BOOST_CONTAINER_NOEXCEPT
1242 { return this->m_holder.capacity(); }
1244 //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
1245 //! effect. Otherwise, it is a request for allocation of additional memory.
1246 //! If the request is successful, then capacity() is greater than or equal to
1247 //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
1249 //! <b>Throws</b>: If memory allocation allocation throws or T's copy/move constructor throws.
1250 void reserve(size_type new_cap)
1252 if (this->capacity() < new_cap){
1253 this->priv_reserve(new_cap, alloc_version());
1257 //! <b>Effects</b>: Tries to deallocate the excess of memory created
1258 //! with previous allocations. The size of the vector is unchanged
1260 //! <b>Throws</b>: If memory allocation throws, or T's copy/move constructor throws.
1262 //! <b>Complexity</b>: Linear to size().
1263 void shrink_to_fit()
1264 { this->priv_shrink_to_fit(alloc_version()); }
1266 //////////////////////////////////////////////
1270 //////////////////////////////////////////////
1272 //! <b>Requires</b>: !empty()
1274 //! <b>Effects</b>: Returns a reference to the first
1275 //! element of the container.
1277 //! <b>Throws</b>: Nothing.
1279 //! <b>Complexity</b>: Constant.
1280 reference front() BOOST_CONTAINER_NOEXCEPT
1281 { return *this->m_holder.start(); }
1283 //! <b>Requires</b>: !empty()
1285 //! <b>Effects</b>: Returns a const reference to the first
1286 //! element of the container.
1288 //! <b>Throws</b>: Nothing.
1290 //! <b>Complexity</b>: Constant.
1291 const_reference front() const BOOST_CONTAINER_NOEXCEPT
1292 { return *this->m_holder.start(); }
1294 //! <b>Requires</b>: !empty()
1296 //! <b>Effects</b>: Returns a reference to the last
1297 //! element of the container.
1299 //! <b>Throws</b>: Nothing.
1301 //! <b>Complexity</b>: Constant.
1302 reference back() BOOST_CONTAINER_NOEXCEPT
1303 { return this->m_holder.start()[this->m_holder.m_size - 1]; }
1305 //! <b>Requires</b>: !empty()
1307 //! <b>Effects</b>: Returns a const reference to the last
1308 //! element of the container.
1310 //! <b>Throws</b>: Nothing.
1312 //! <b>Complexity</b>: Constant.
1313 const_reference back() const BOOST_CONTAINER_NOEXCEPT
1314 { return this->m_holder.start()[this->m_holder.m_size - 1]; }
1316 //! <b>Requires</b>: size() > n.
1318 //! <b>Effects</b>: Returns a reference to the nth element
1319 //! from the beginning of the container.
1321 //! <b>Throws</b>: Nothing.
1323 //! <b>Complexity</b>: Constant.
1324 reference operator[](size_type n) BOOST_CONTAINER_NOEXCEPT
1325 { return this->m_holder.start()[n]; }
1327 //! <b>Requires</b>: size() > n.
1329 //! <b>Effects</b>: Returns a const reference to the nth element
1330 //! from the beginning of the container.
1332 //! <b>Throws</b>: Nothing.
1334 //! <b>Complexity</b>: Constant.
1335 const_reference operator[](size_type n) const BOOST_CONTAINER_NOEXCEPT
1336 { return this->m_holder.start()[n]; }
1338 //! <b>Requires</b>: size() > n.
1340 //! <b>Effects</b>: Returns a reference to the nth element
1341 //! from the beginning of the container.
1343 //! <b>Throws</b>: std::range_error if n >= size()
1345 //! <b>Complexity</b>: Constant.
1346 reference at(size_type n)
1347 { this->priv_check_range(n); return this->m_holder.start()[n]; }
1349 //! <b>Requires</b>: size() > n.
1351 //! <b>Effects</b>: Returns a const reference to the nth element
1352 //! from the beginning of the container.
1354 //! <b>Throws</b>: std::range_error if n >= size()
1356 //! <b>Complexity</b>: Constant.
1357 const_reference at(size_type n) const
1358 { this->priv_check_range(n); return this->m_holder.start()[n]; }
1360 //////////////////////////////////////////////
1364 //////////////////////////////////////////////
1366 //! <b>Returns</b>: Allocator pointer such that [data(),data() + size()) is a valid range.
1367 //! For a non-empty vector, data() == &front().
1369 //! <b>Throws</b>: Nothing.
1371 //! <b>Complexity</b>: Constant.
1372 T* data() BOOST_CONTAINER_NOEXCEPT
1373 { return container_detail::to_raw_pointer(this->m_holder.start()); }
1375 //! <b>Returns</b>: Allocator pointer such that [data(),data() + size()) is a valid range.
1376 //! For a non-empty vector, data() == &front().
1378 //! <b>Throws</b>: Nothing.
1380 //! <b>Complexity</b>: Constant.
1381 const T * data() const BOOST_CONTAINER_NOEXCEPT
1382 { return container_detail::to_raw_pointer(this->m_holder.start()); }
1384 //////////////////////////////////////////////
1388 //////////////////////////////////////////////
1390 #if defined(BOOST_CONTAINER_PERFECT_FORWARDING) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1391 //! <b>Effects</b>: Inserts an object of type T constructed with
1392 //! std::forward<Args>(args)... in the end of the vector.
1394 //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws or
1395 //! T's copy/move constructor throws.
1397 //! <b>Complexity</b>: Amortized constant time.
1398 template<class ...Args>
1399 void emplace_back(Args &&...args)
1401 if (BOOST_LIKELY(this->m_holder.m_size < this->m_holder.capacity())){
1402 T* const back_pos = container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size;
1403 //There is more memory, just construct a new object at the end
1404 allocator_traits_type::construct(this->m_holder.alloc(), back_pos, ::boost::forward<Args>(args)...);
1405 ++this->m_holder.m_size;
1408 typedef container_detail::insert_emplace_proxy<Allocator, T*, Args...> type;
1409 this->priv_forward_range_insert_no_capacity
1410 (vector_iterator_get_ptr(this->cend()), 1, type(::boost::forward<Args>(args)...), alloc_version());
1414 //! <b>Requires</b>: position must be a valid iterator of *this.
1416 //! <b>Effects</b>: Inserts an object of type T constructed with
1417 //! std::forward<Args>(args)... before position
1419 //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws or
1420 //! T's copy/move constructor/assignment throws.
1422 //! <b>Complexity</b>: If position is end(), amortized constant time
1423 //! Linear time otherwise.
1424 template<class ...Args>
1425 iterator emplace(const_iterator position, Args && ...args)
1427 //Just call more general insert(pos, size, value) and return iterator
1428 typedef container_detail::insert_emplace_proxy<Allocator, T*, Args...> type;
1429 return this->priv_forward_range_insert( vector_iterator_get_ptr(position), 1
1430 , type(::boost::forward<Args>(args)...), alloc_version());
1435 #define BOOST_PP_LOCAL_MACRO(n) \
1436 BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \
1437 void emplace_back(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \
1439 T* const back_pos = container_detail::to_raw_pointer \
1440 (this->m_holder.start()) + this->m_holder.m_size; \
1441 if (BOOST_LIKELY(this->m_holder.m_size < this->m_holder.capacity())){ \
1442 allocator_traits_type::construct (this->m_holder.alloc() \
1443 , back_pos BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _) ); \
1444 ++this->m_holder.m_size; \
1447 typedef container_detail::BOOST_PP_CAT(insert_emplace_proxy_arg, n) \
1448 <Allocator, T* BOOST_PP_ENUM_TRAILING_PARAMS(n, P)> type; \
1449 this->priv_forward_range_insert_no_capacity \
1450 ( vector_iterator_get_ptr(this->cend()), 1 \
1451 , type(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)), alloc_version()); \
1455 BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \
1456 iterator emplace(const_iterator pos \
1457 BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \
1459 typedef container_detail::BOOST_PP_CAT(insert_emplace_proxy_arg, n) \
1460 <Allocator, T* BOOST_PP_ENUM_TRAILING_PARAMS(n, P)> type; \
1461 return this->priv_forward_range_insert \
1462 ( container_detail::to_raw_pointer(vector_iterator_get_ptr(pos)), 1 \
1463 , type(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)), alloc_version()); \
1466 #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS)
1467 #include BOOST_PP_LOCAL_ITERATE()
1469 #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING
1471 #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1472 //! <b>Effects</b>: Inserts a copy of x at the end of the vector.
1474 //! <b>Throws</b>: If memory allocation throws or
1475 //! T's copy/move constructor throws.
1477 //! <b>Complexity</b>: Amortized constant time.
1478 void push_back(const T &x);
1480 //! <b>Effects</b>: Constructs a new element in the end of the vector
1481 //! and moves the resources of x to this new element.
1483 //! <b>Throws</b>: If memory allocation throws or
1484 //! T's copy/move constructor throws.
1486 //! <b>Complexity</b>: Amortized constant time.
1487 void push_back(T &&x);
1489 BOOST_MOVE_CONVERSION_AWARE_CATCH(push_back, T, void, priv_push_back)
1492 #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1493 //! <b>Requires</b>: position must be a valid iterator of *this.
1495 //! <b>Effects</b>: Insert a copy of x before position.
1497 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment throws.
1499 //! <b>Complexity</b>: If position is end(), amortized constant time
1500 //! Linear time otherwise.
1501 iterator insert(const_iterator position, const T &x);
1503 //! <b>Requires</b>: position must be a valid iterator of *this.
1505 //! <b>Effects</b>: Insert a new element before position with x's resources.
1507 //! <b>Throws</b>: If memory allocation throws.
1509 //! <b>Complexity</b>: If position is end(), amortized constant time
1510 //! Linear time otherwise.
1511 iterator insert(const_iterator position, T &&x);
1513 BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, T, iterator, priv_insert, const_iterator, const_iterator)
1516 //! <b>Requires</b>: p must be a valid iterator of *this.
1518 //! <b>Effects</b>: Insert n copies of x before pos.
1520 //! <b>Returns</b>: an iterator to the first inserted element or p if n is 0.
1522 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor throws.
1524 //! <b>Complexity</b>: Linear to n.
1525 iterator insert(const_iterator p, size_type n, const T& x)
1527 container_detail::insert_n_copies_proxy<Allocator, T*> proxy(x);
1528 return this->priv_forward_range_insert(vector_iterator_get_ptr(p), n, proxy, alloc_version());
1531 //! <b>Requires</b>: p must be a valid iterator of *this.
1533 //! <b>Effects</b>: Insert a copy of the [first, last) range before pos.
1535 //! <b>Returns</b>: an iterator to the first inserted element or pos if first == last.
1537 //! <b>Throws</b>: If memory allocation throws, T's constructor from a
1538 //! dereferenced InpIt throws or T's copy/move constructor/assignment throws.
1540 //! <b>Complexity</b>: Linear to std::distance [first, last).
1541 template <class InIt>
1542 iterator insert(const_iterator pos, InIt first, InIt last
1543 BOOST_CONTAINER_DOCIGN(BOOST_CONTAINER_I typename container_detail::enable_if_c
1544 < !container_detail::is_convertible<InIt BOOST_CONTAINER_I size_type>::value
1545 && container_detail::is_input_iterator<InIt>::value
1549 const size_type n_pos = pos - this->cbegin();
1550 iterator it(vector_iterator_get_ptr(pos));
1551 for(;first != last; ++first){
1552 it = this->emplace(it, *first);
1555 return iterator(this->m_holder.start() + n_pos);
1558 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1559 template <class FwdIt>
1560 iterator insert(const_iterator pos, FwdIt first, FwdIt last
1561 , typename container_detail::enable_if_c
1562 < !container_detail::is_convertible<FwdIt, size_type>::value
1563 && !container_detail::is_input_iterator<FwdIt>::value
1567 container_detail::insert_range_proxy<Allocator, FwdIt, T*> proxy(first);
1568 return this->priv_forward_range_insert(vector_iterator_get_ptr(pos), std::distance(first, last), proxy, alloc_version());
1572 //! <b>Requires</b>: p must be a valid iterator of *this. num, must
1573 //! be equal to std::distance(first, last)
1575 //! <b>Effects</b>: Insert a copy of the [first, last) range before pos.
1577 //! <b>Returns</b>: an iterator to the first inserted element or pos if first == last.
1579 //! <b>Throws</b>: If memory allocation throws, T's constructor from a
1580 //! dereferenced InpIt throws or T's copy/move constructor/assignment throws.
1582 //! <b>Complexity</b>: Linear to std::distance [first, last).
1584 //! <b>Note</b>: This function avoids a linear operation to calculate std::distance[first, last)
1585 //! for forward and bidirectional iterators, and a one by one insertion for input iterators. This is a
1586 //! a non-standard extension.
1587 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1588 template <class InIt>
1589 iterator insert(const_iterator pos, size_type num, InIt first, InIt last)
1591 BOOST_ASSERT(container_detail::is_input_iterator<InIt>::value ||
1592 num == static_cast<size_type>(std::distance(first, last)));
1594 container_detail::insert_range_proxy<Allocator, InIt, T*> proxy(first);
1595 return this->priv_forward_range_insert(vector_iterator_get_ptr(pos), num, proxy, alloc_version());
1599 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1600 //! <b>Requires</b>: position must be a valid iterator of *this.
1602 //! <b>Effects</b>: Insert a copy of the [il.begin(), il.end()) range before position.
1604 //! <b>Returns</b>: an iterator to the first inserted element or position if first == last.
1606 //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
1607 iterator insert(const_iterator position, std::initializer_list<value_type> il)
1609 return insert(position, il.begin(), il.end());
1613 //! <b>Effects</b>: Removes the last element from the vector.
1615 //! <b>Throws</b>: Nothing.
1617 //! <b>Complexity</b>: Constant time.
1618 void pop_back() BOOST_CONTAINER_NOEXCEPT
1620 //Destroy last element
1621 this->priv_destroy_last();
1624 //! <b>Effects</b>: Erases the element at position pos.
1626 //! <b>Throws</b>: Nothing.
1628 //! <b>Complexity</b>: Linear to the elements between pos and the
1629 //! last element. Constant if pos is the last element.
1630 iterator erase(const_iterator position)
1632 const pointer p = vector_iterator_get_ptr(position);
1633 T *const pos_ptr = container_detail::to_raw_pointer(p);
1634 T *const beg_ptr = container_detail::to_raw_pointer(this->m_holder.start());
1635 T *const new_end_ptr = ::boost::move(pos_ptr + 1, beg_ptr + this->m_holder.m_size, pos_ptr);
1636 //Move elements forward and destroy last
1637 this->priv_destroy_last(pos_ptr == new_end_ptr);
1641 //! <b>Effects</b>: Erases the elements pointed by [first, last).
1643 //! <b>Throws</b>: Nothing.
1645 //! <b>Complexity</b>: Linear to the distance between first and last
1646 //! plus linear to the elements between pos and the last element.
1647 iterator erase(const_iterator first, const_iterator last)
1650 T* const old_end_ptr = container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size;
1651 T* const first_ptr = container_detail::to_raw_pointer(vector_iterator_get_ptr(first));
1652 T* const last_ptr = container_detail::to_raw_pointer(vector_iterator_get_ptr(last));
1653 T* const ptr = container_detail::to_raw_pointer(boost::move(last_ptr, old_end_ptr, first_ptr));
1654 this->priv_destroy_last_n(old_end_ptr - ptr, last_ptr == old_end_ptr);
1656 return iterator(vector_iterator_get_ptr(first));
1659 //! <b>Effects</b>: Swaps the contents of *this and x.
1661 //! <b>Throws</b>: Nothing.
1663 //! <b>Complexity</b>: Constant.
1664 void swap(vector& x) BOOST_CONTAINER_NOEXCEPT_IF((!container_detail::is_version<Allocator, 0>::value))
1666 //Just swap internals in case of !allocator_v0. Otherwise, deep swap
1667 this->m_holder.swap(x.m_holder);
1668 //And now the allocator
1669 container_detail::bool_<allocator_traits_type::propagate_on_container_swap::value> flag;
1670 container_detail::swap_alloc(this->m_holder.alloc(), x.m_holder.alloc(), flag);
1673 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
1675 //! <b>Effects</b>: Swaps the contents of *this and x.
1677 //! <b>Throws</b>: Nothing.
1679 //! <b>Complexity</b>: Linear
1681 //! <b>Note</b>: Non-standard extension to support static_vector
1682 template<class OtherAllocator>
1683 void swap(vector<T, OtherAllocator> & x
1684 , typename container_detail::enable_if_c
1685 < container_detail::is_version<OtherAllocator, 0>::value &&
1686 !container_detail::is_same<OtherAllocator, allocator_type>::value >::type * = 0
1688 { this->m_holder.swap(x.m_holder); }
1690 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
1692 //! <b>Effects</b>: Erases all the elements of the vector.
1694 //! <b>Throws</b>: Nothing.
1696 //! <b>Complexity</b>: Linear to the number of elements in the container.
1697 void clear() BOOST_CONTAINER_NOEXCEPT
1698 { this->priv_destroy_all(); }
1700 //! <b>Effects</b>: Returns true if x and y are equal
1702 //! <b>Complexity</b>: Linear to the number of elements in the container.
1703 friend bool operator==(const vector& x, const vector& y)
1705 if(x.size() != y.size()){
1709 const_iterator first1(x.cbegin()), first2(y.cbegin());
1710 const const_iterator last1(x.cend());
1711 for (; first1 != last1; ++first1, ++first2) {
1712 if (!(*first1 != *first2)) {
1720 //! <b>Effects</b>: Returns true if x and y are unequal
1722 //! <b>Complexity</b>: Linear to the number of elements in the container.
1723 friend bool operator!=(const vector& x, const vector& y)
1724 { return !(x == y); }
1726 //! <b>Effects</b>: Returns true if x is less than y
1728 //! <b>Complexity</b>: Linear to the number of elements in the container.
1729 friend bool operator<(const vector& x, const vector& y)
1731 const_iterator first1(x.cbegin()), first2(y.cbegin());
1732 const const_iterator last1(x.cend()), last2(y.cend());
1733 for ( ; (first1 != last1) && (first2 != last2); ++first1, ++first2 ) {
1734 if (*first1 < *first2) return true;
1735 if (*first2 < *first1) return false;
1737 return (first1 == last1) && (first2 != last2);
1740 //! <b>Effects</b>: Returns true if x is greater than y
1742 //! <b>Complexity</b>: Linear to the number of elements in the container.
1743 friend bool operator>(const vector& x, const vector& y)
1746 //! <b>Effects</b>: Returns true if x is equal or less than y
1748 //! <b>Complexity</b>: Linear to the number of elements in the container.
1749 friend bool operator<=(const vector& x, const vector& y)
1750 { return !(y < x); }
1752 //! <b>Effects</b>: Returns true if x is equal or greater than y
1754 //! <b>Complexity</b>: Linear to the number of elements in the container.
1755 friend bool operator>=(const vector& x, const vector& y)
1756 { return !(x < y); }
1758 //! <b>Effects</b>: x.swap(y)
1760 //! <b>Complexity</b>: Constant.
1761 friend void swap(vector& x, vector& y)
1764 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
1765 //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
1766 //! effect. Otherwise, it is a request for allocation of additional memory
1767 //! (memory expansion) that will not invalidate iterators.
1768 //! If the request is successful, then capacity() is greater than or equal to
1769 //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
1771 //! <b>Throws</b>: If memory allocation allocation throws or T's copy/move constructor throws.
1773 //! <b>Note</b>: Non-standard extension.
1774 bool stable_reserve(size_type new_cap)
1776 const bool room_enough = this->capacity() < new_cap;
1777 if(!room_enough && alloc_version::value < 2){
1781 //There is not enough memory, try to expand the old one
1782 size_type real_cap = 0;
1783 std::pair<pointer, bool> ret = this->m_holder.allocation_command
1784 (expand_fwd, new_cap, new_cap, real_cap, this->m_holder.start());
1785 //Check for forward expansion
1787 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
1788 ++this->num_expand_fwd;
1790 this->m_holder.capacity(real_cap);
1796 //Absolutely experimental. This function might change, disappear or simply crash!
1797 template<class BiDirPosConstIt, class BiDirValueIt>
1798 void insert_ordered_at(const size_type element_count, BiDirPosConstIt last_position_it, BiDirValueIt last_value_it)
1800 const size_type old_size_pos = this->size();
1801 this->reserve(old_size_pos + element_count);
1802 T* const begin_ptr = container_detail::to_raw_pointer(this->m_holder.start());
1803 size_type insertions_left = element_count;
1804 size_type next_pos = old_size_pos;
1805 size_type hole_size = element_count;
1807 //Exception rollback. If any copy throws before the hole is filled, values
1808 //already inserted/copied at the end of the buffer will be destroyed.
1809 typename value_traits::ArrayDestructor past_hole_values_destroyer
1810 (begin_ptr + old_size_pos + element_count, this->m_holder.alloc(), size_type(0u));
1811 //Loop for each insertion backwards, first moving the elements after the insertion point,
1812 //then inserting the element.
1813 while(insertions_left){
1814 size_type pos = static_cast<size_type>(*(--last_position_it));
1815 while(pos == size_type(-1)){
1817 pos = static_cast<size_type>(*(--last_position_it));
1820 BOOST_ASSERT(pos != size_type(-1) && pos <= old_size_pos);
1821 //If needed shift the range after the insertion point and the previous insertion point.
1822 //Function will take care if the shift crosses the size() boundary, using copy/move
1823 //or uninitialized copy/move if necessary.
1824 size_type new_hole_size = (pos != next_pos)
1825 ? priv_insert_ordered_at_shift_range(pos, next_pos, this->size(), insertions_left)
1828 if(new_hole_size > 0){
1829 //The hole was reduced by priv_insert_ordered_at_shift_range so expand exception rollback range backwards
1830 past_hole_values_destroyer.increment_size_backwards(next_pos - pos);
1831 //Insert the new value in the hole
1832 allocator_traits_type::construct(this->m_holder.alloc(), begin_ptr + pos + insertions_left - 1, *(--last_value_it));
1834 if(new_hole_size == 0){
1835 //Hole was just filled, disable exception rollback and change vector size
1836 past_hole_values_destroyer.release();
1837 this->m_holder.m_size += element_count;
1840 //The hole was reduced by the new insertion by one
1841 past_hole_values_destroyer.increment_size_backwards(size_type(1u));
1846 //Hole was just filled by priv_insert_ordered_at_shift_range, disable exception rollback and change vector size
1847 past_hole_values_destroyer.release();
1848 this->m_holder.m_size += element_count;
1850 //Insert the new value in the already constructed range
1851 begin_ptr[pos + insertions_left - 1] = *(--last_value_it);
1854 hole_size = new_hole_size;
1859 #if defined(BOOST_CONTAINER_PERFECT_FORWARDING) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1860 //! <b>Effects</b>: Inserts an object of type T constructed with
1861 //! std::forward<Args>(args)... in the end of the vector.
1863 //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws or
1864 //! T's copy/move constructor throws.
1866 //! <b>Complexity</b>: Amortized constant time.
1867 template<class ...Args>
1868 bool stable_emplace_back(Args &&...args)
1870 const bool room_enough = this->m_holder.m_size < this->m_holder.capacity();
1871 if (BOOST_LIKELY(room_enough)){
1872 T* const back_pos = container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size;
1873 //There is more memory, just construct a new object at the end
1874 allocator_traits_type::construct(this->m_holder.alloc(), back_pos, ::boost::forward<Args>(args)...);
1875 ++this->m_holder.m_size;
1882 #define BOOST_PP_LOCAL_MACRO(n) \
1883 BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \
1884 bool stable_emplace_back(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \
1886 const bool room_enough = this->m_holder.m_size < this->m_holder.capacity(); \
1887 if (BOOST_LIKELY(room_enough)){ \
1888 T* const back_pos = container_detail::to_raw_pointer \
1889 (this->m_holder.start()) + this->m_holder.m_size; \
1890 allocator_traits_type::construct (this->m_holder.alloc() \
1891 , back_pos BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _) ); \
1892 ++this->m_holder.m_size; \
1894 return room_enough; \
1897 #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS)
1898 #include BOOST_PP_LOCAL_ITERATE()
1900 #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING
1904 template<class OtherAllocator>
1905 void priv_move_assign(BOOST_RV_REF_BEG vector<T, OtherAllocator> BOOST_RV_REF_END x
1906 , typename container_detail::enable_if_c
1907 < container_detail::is_version<OtherAllocator, 0>::value >::type * = 0)
1909 if(!container_detail::is_same<OtherAllocator, allocator_type>::value &&
1910 this->capacity() < x.size()){
1913 T* const this_start = container_detail::to_raw_pointer(m_holder.start());
1914 T* const other_start = container_detail::to_raw_pointer(x.m_holder.start());
1915 const size_type this_sz = m_holder.m_size;
1916 const size_type other_sz = static_cast<size_type>(x.m_holder.m_size);
1917 boost::container::move_assign_range_alloc_n(this->m_holder.alloc(), other_start, other_sz, this_start, this_sz);
1918 this->m_holder.m_size = other_sz;
1921 template<class OtherAllocator>
1922 void priv_move_assign(BOOST_RV_REF_BEG vector<T, OtherAllocator> BOOST_RV_REF_END x
1923 , typename container_detail::enable_if_c
1924 < !container_detail::is_version<OtherAllocator, 0>::value &&
1925 container_detail::is_same<OtherAllocator, allocator_type>::value>::type * = 0)
1927 //for move constructor, no aliasing (&x != this) is assummed.
1928 BOOST_ASSERT(this != &x);
1929 allocator_type &this_alloc = this->m_holder.alloc();
1930 allocator_type &x_alloc = x.m_holder.alloc();
1931 const bool propagate_alloc = allocator_traits_type::
1932 propagate_on_container_move_assignment::value;
1933 container_detail::bool_<propagate_alloc> flag;
1934 const bool allocators_equal = this_alloc == x_alloc; (void)allocators_equal;
1935 //Resources can be transferred if both allocators are
1936 //going to be equal after this function (either propagated or already equal)
1937 if(propagate_alloc || allocators_equal){
1938 //Destroy objects but retain memory in case x reuses it in the future
1940 //Move allocator if needed
1941 container_detail::move_alloc(this_alloc, x_alloc, flag);
1943 this->m_holder.swap(x.m_holder);
1945 //Else do a one by one move
1947 this->assign( boost::make_move_iterator(x.begin())
1948 , boost::make_move_iterator(x.end()));
1952 template<class OtherAllocator>
1953 void priv_copy_assign(const vector<T, OtherAllocator> &x
1954 , typename container_detail::enable_if_c
1955 < container_detail::is_version<OtherAllocator, 0>::value >::type * = 0)
1957 if(!container_detail::is_same<OtherAllocator, allocator_type>::value &&
1958 this->capacity() < x.size()){
1961 T* const this_start = container_detail::to_raw_pointer(m_holder.start());
1962 T* const other_start = container_detail::to_raw_pointer(x.m_holder.start());
1963 const size_type this_sz = m_holder.m_size;
1964 const size_type other_sz = static_cast<size_type>(x.m_holder.m_size);
1965 boost::container::copy_assign_range_alloc_n(this->m_holder.alloc(), other_start, other_sz, this_start, this_sz);
1966 this->m_holder.m_size = other_sz;
1969 template<class OtherAllocator>
1970 void priv_copy_assign(const vector<T, OtherAllocator> &x
1971 , typename container_detail::enable_if_c
1972 < !container_detail::is_version<OtherAllocator, 0>::value &&
1973 container_detail::is_same<OtherAllocator, allocator_type>::value >::type * = 0)
1975 allocator_type &this_alloc = this->m_holder.alloc();
1976 const allocator_type &x_alloc = x.m_holder.alloc();
1977 container_detail::bool_<allocator_traits_type::
1978 propagate_on_container_copy_assignment::value> flag;
1979 if(flag && this_alloc != x_alloc){
1981 this->shrink_to_fit();
1983 container_detail::assign_alloc(this_alloc, x_alloc, flag);
1984 this->assign( container_detail::to_raw_pointer(x.m_holder.start())
1985 , container_detail::to_raw_pointer(x.m_holder.start() + x.m_holder.m_size));
1988 void priv_reserve(size_type, allocator_v0)
1989 { throw_bad_alloc(); }
1991 container_detail::insert_range_proxy<Allocator, boost::move_iterator<T*>, T*> priv_dummy_empty_proxy()
1993 return container_detail::insert_range_proxy<Allocator, boost::move_iterator<T*>, T*>
1994 (::boost::make_move_iterator((T *)0));
1997 void priv_reserve(size_type new_cap, allocator_v1)
1999 //There is not enough memory, allocate a new buffer
2000 pointer p = this->m_holder.allocate(new_cap);
2001 //We will reuse insert code, so create a dummy input iterator
2002 this->priv_forward_range_insert_new_allocation
2003 ( container_detail::to_raw_pointer(p), new_cap
2004 , container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size
2005 , 0, this->priv_dummy_empty_proxy());
2008 void priv_reserve(size_type new_cap, allocator_v2)
2010 //There is not enough memory, allocate a new
2011 //buffer or expand the old one.
2012 bool same_buffer_start;
2013 size_type real_cap = 0;
2014 std::pair<pointer, bool> ret = this->m_holder.allocation_command
2015 (allocate_new | expand_fwd | expand_bwd, new_cap, new_cap, real_cap, this->m_holder.start());
2017 //Check for forward expansion
2018 same_buffer_start = ret.second && this->m_holder.start() == ret.first;
2019 if(same_buffer_start){
2020 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2021 ++this->num_expand_fwd;
2023 this->m_holder.capacity(real_cap);
2025 else{ //If there is no forward expansion, move objects, we will reuse insertion code
2026 T * const new_mem = container_detail::to_raw_pointer(ret.first);
2027 T * const ins_pos = container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size;
2028 if(ret.second){ //Backwards (and possibly forward) expansion
2029 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2030 ++this->num_expand_bwd;
2032 this->priv_forward_range_insert_expand_backwards
2033 ( new_mem , real_cap, ins_pos, 0, this->priv_dummy_empty_proxy());
2036 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2039 this->priv_forward_range_insert_new_allocation
2040 ( new_mem, real_cap, ins_pos, 0, this->priv_dummy_empty_proxy());
2045 void priv_destroy_last() BOOST_CONTAINER_NOEXCEPT
2047 if(!value_traits::trivial_dctr){
2048 value_type* const p = container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size - 1;
2049 allocator_traits_type::destroy(this->get_stored_allocator(), p);
2051 --this->m_holder.m_size;
2054 void priv_destroy_last(const bool moved) BOOST_CONTAINER_NOEXCEPT
2057 if(!(value_traits::trivial_dctr || (value_traits::trivial_dctr_after_move && moved))){
2058 value_type* const p = container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size - 1;
2059 allocator_traits_type::destroy(this->get_stored_allocator(), p);
2061 --this->m_holder.m_size;
2064 void priv_destroy_last_n(const size_type n) BOOST_CONTAINER_NOEXCEPT
2066 BOOST_ASSERT(n <= this->m_holder.m_size);
2067 if(!value_traits::trivial_dctr){
2068 T* const destroy_pos = container_detail::to_raw_pointer(this->m_holder.start()) + (this->m_holder.m_size-n);
2069 boost::container::destroy_alloc_n(this->get_stored_allocator(), destroy_pos, n);
2071 this->m_holder.m_size -= n;
2074 void priv_destroy_last_n(const size_type n, const bool moved) BOOST_CONTAINER_NOEXCEPT
2076 BOOST_ASSERT(n <= this->m_holder.m_size);
2078 if(!(value_traits::trivial_dctr || (value_traits::trivial_dctr_after_move && moved))){
2079 T* const destroy_pos = container_detail::to_raw_pointer(this->m_holder.start()) + (this->m_holder.m_size-n);
2080 boost::container::destroy_alloc_n(this->get_stored_allocator(), destroy_pos, n);
2082 this->m_holder.m_size -= n;
2085 template<class InpIt>
2086 void priv_uninitialized_construct_at_end(InpIt first, InpIt last)
2088 T* const old_end_pos = container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size;
2089 T* const new_end_pos = boost::container::uninitialized_copy_alloc(this->m_holder.alloc(), first, last, old_end_pos);
2090 this->m_holder.m_size += new_end_pos - old_end_pos;
2093 void priv_destroy_all() BOOST_CONTAINER_NOEXCEPT
2095 boost::container::destroy_alloc_n
2096 (this->get_stored_allocator(), container_detail::to_raw_pointer(this->m_holder.start()), this->m_holder.m_size);
2097 this->m_holder.m_size = 0;
2101 iterator priv_insert(const const_iterator &p, BOOST_FWD_REF(U) x)
2103 return this->priv_forward_range_insert
2104 ( vector_iterator_get_ptr(p), 1, container_detail::get_insert_value_proxy<T*, Allocator>
2105 (::boost::forward<U>(x)), alloc_version());
2108 container_detail::insert_copy_proxy<Allocator, T*> priv_single_insert_proxy(const T &x)
2109 { return container_detail::insert_copy_proxy<Allocator, T*> (x); }
2111 container_detail::insert_move_proxy<Allocator, T*> priv_single_insert_proxy(BOOST_RV_REF(T) x)
2112 { return container_detail::insert_move_proxy<Allocator, T*> (x); }
2115 void priv_push_back(BOOST_FWD_REF(U) u)
2117 if (BOOST_LIKELY(this->m_holder.m_size < this->m_holder.capacity())){
2118 //There is more memory, just construct a new object at the end
2119 allocator_traits_type::construct
2120 ( this->m_holder.alloc()
2121 , container_detail::to_raw_pointer(this->m_holder.start() + this->m_holder.m_size)
2122 , ::boost::forward<U>(u) );
2123 ++this->m_holder.m_size;
2126 this->priv_forward_range_insert_no_capacity
2127 ( vector_iterator_get_ptr(this->cend()), 1
2128 , this->priv_single_insert_proxy(::boost::forward<U>(u)), alloc_version());
2132 container_detail::insert_n_copies_proxy<Allocator, T*> priv_resize_proxy(const T &x)
2133 { return container_detail::insert_n_copies_proxy<Allocator, T*>(x); }
2135 container_detail::insert_default_initialized_n_proxy<Allocator, T*> priv_resize_proxy(default_init_t)
2136 { return container_detail::insert_default_initialized_n_proxy<Allocator, T*>(); }
2138 container_detail::insert_value_initialized_n_proxy<Allocator, T*> priv_resize_proxy(value_init_t)
2139 { return container_detail::insert_value_initialized_n_proxy<Allocator, T*>(); }
2142 void priv_resize(size_type new_size, const U& u)
2144 const size_type sz = this->size();
2146 //Destroy last elements
2147 this->priv_destroy_last_n(sz - new_size);
2150 const size_type n = new_size - this->size();
2151 this->priv_forward_range_insert_at_end(n, this->priv_resize_proxy(u), alloc_version());
2155 void priv_shrink_to_fit(allocator_v0) BOOST_CONTAINER_NOEXCEPT
2158 void priv_shrink_to_fit(allocator_v1)
2160 const size_type cp = this->m_holder.capacity();
2162 const size_type sz = this->size();
2164 this->m_holder.alloc().deallocate(this->m_holder.m_start, cp);
2165 this->m_holder.m_start = pointer();
2166 this->m_holder.m_capacity = 0;
2169 //Allocate a new buffer.
2170 pointer p = this->m_holder.allocate(sz);
2172 //We will reuse insert code, so create a dummy input iterator
2173 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2176 this->priv_forward_range_insert_new_allocation
2177 ( container_detail::to_raw_pointer(p), sz
2178 , container_detail::to_raw_pointer(this->m_holder.start())
2179 , 0, this->priv_dummy_empty_proxy());
2184 void priv_shrink_to_fit(allocator_v2) BOOST_CONTAINER_NOEXCEPT
2186 const size_type cp = this->m_holder.capacity();
2188 const size_type sz = this->size();
2190 this->m_holder.alloc().deallocate(this->m_holder.m_start, cp);
2191 this->m_holder.m_start = pointer();
2192 this->m_holder.m_capacity = 0;
2195 size_type received_size;
2196 if(this->m_holder.allocation_command
2197 ( shrink_in_place | nothrow_allocation
2198 , cp, sz, received_size, this->m_holder.start()).first){
2199 this->m_holder.capacity(received_size);
2200 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2208 template <class InsertionProxy>
2209 iterator priv_forward_range_insert_no_capacity
2210 (const pointer &pos, const size_type, const InsertionProxy , allocator_v0)
2213 return iterator(pos);
2216 template <class InsertionProxy>
2217 iterator priv_forward_range_insert_no_capacity
2218 (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, allocator_v1)
2220 //Check if we have enough memory or try to expand current memory
2221 const size_type n_pos = pos - this->m_holder.start();
2222 T *const raw_pos = container_detail::to_raw_pointer(pos);
2224 const size_type new_cap = this->m_holder.next_capacity(n);
2225 T * new_buf = container_detail::to_raw_pointer(this->m_holder.alloc().allocate(new_cap));
2226 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2229 this->priv_forward_range_insert_new_allocation
2230 ( new_buf, new_cap, raw_pos, n, insert_range_proxy);
2231 return iterator(this->m_holder.start() + n_pos);
2234 template <class InsertionProxy>
2235 iterator priv_forward_range_insert_no_capacity
2236 (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, allocator_v2)
2238 //Check if we have enough memory or try to expand current memory
2239 T *const raw_pos = container_detail::to_raw_pointer(pos);
2240 const size_type n_pos = raw_pos - container_detail::to_raw_pointer(this->m_holder.start());
2242 size_type real_cap = 0;
2243 //There is not enough memory, allocate a new
2244 //buffer or expand the old one.
2245 std::pair<pointer, bool> ret = (this->m_holder.allocation_command
2246 (allocate_new | expand_fwd | expand_bwd,
2247 this->m_holder.m_size + n, this->m_holder.next_capacity(n), real_cap, this->m_holder.start()));
2249 //Buffer reallocated
2251 //Forward expansion, delay insertion
2252 if(this->m_holder.start() == ret.first){
2253 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2254 ++this->num_expand_fwd;
2256 this->m_holder.capacity(real_cap);
2258 this->priv_forward_range_insert_expand_forward(raw_pos, n, insert_range_proxy);
2260 //Backwards (and possibly forward) expansion
2262 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2263 ++this->num_expand_bwd;
2265 this->priv_forward_range_insert_expand_backwards
2266 ( container_detail::to_raw_pointer(ret.first)
2267 , real_cap, raw_pos, n, insert_range_proxy);
2272 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2275 this->priv_forward_range_insert_new_allocation
2276 ( container_detail::to_raw_pointer(ret.first)
2277 , real_cap, raw_pos, n, insert_range_proxy);
2280 return iterator(this->m_holder.start() + n_pos);
2283 template <class InsertionProxy>
2284 iterator priv_forward_range_insert
2285 (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, allocator_v0)
2287 //Check if we have enough memory or try to expand current memory
2288 const size_type remaining = this->m_holder.capacity() - this->m_holder.m_size;
2291 //This will trigger an error
2294 const size_type n_pos = pos - this->m_holder.start();
2295 T *const raw_pos = container_detail::to_raw_pointer(pos);
2296 this->priv_forward_range_insert_expand_forward(raw_pos, n, insert_range_proxy);
2297 return iterator(this->m_holder.start() + n_pos);
2300 template <class InsertionProxy>
2301 iterator priv_forward_range_insert
2302 (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, allocator_v1)
2304 //Check if we have enough memory or try to expand current memory
2305 const size_type remaining = this->m_holder.capacity() - this->m_holder.m_size;
2306 T *const raw_pos = container_detail::to_raw_pointer(pos);
2308 if (n <= remaining){
2309 const size_type n_pos = raw_pos - container_detail::to_raw_pointer(this->m_holder.start());
2310 this->priv_forward_range_insert_expand_forward(raw_pos, n, insert_range_proxy);
2311 return iterator(this->m_holder.start() + n_pos);
2314 return this->priv_forward_range_insert_no_capacity(pos, n, insert_range_proxy, alloc_version());
2318 template <class InsertionProxy>
2319 iterator priv_forward_range_insert
2320 (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, allocator_v2)
2322 BOOST_ASSERT(this->m_holder.capacity() >= this->m_holder.m_size);
2323 //Check if we have enough memory or try to expand current memory
2324 const size_type remaining = this->m_holder.capacity() - this->m_holder.m_size;
2326 bool same_buffer_start = n <= remaining;
2327 if (!same_buffer_start){
2328 return priv_forward_range_insert_no_capacity(pos, n, insert_range_proxy, alloc_version());
2332 T *const raw_pos = container_detail::to_raw_pointer(pos);
2333 const size_type n_pos = raw_pos - container_detail::to_raw_pointer(this->m_holder.start());
2334 this->priv_forward_range_insert_expand_forward(raw_pos, n, insert_range_proxy);
2335 return iterator(this->m_holder.start() + n_pos);
2339 template <class InsertionProxy>
2340 iterator priv_forward_range_insert_at_end
2341 (const size_type n, const InsertionProxy insert_range_proxy, allocator_v0)
2343 //Check if we have enough memory or try to expand current memory
2344 const size_type remaining = this->m_holder.capacity() - this->m_holder.m_size;
2347 //This will trigger an error
2350 this->priv_forward_range_insert_at_end_expand_forward(n, insert_range_proxy);
2354 template <class InsertionProxy>
2355 iterator priv_forward_range_insert_at_end
2356 (const size_type n, const InsertionProxy insert_range_proxy, allocator_v1)
2358 return this->priv_forward_range_insert(vector_iterator_get_ptr(this->cend()), n, insert_range_proxy, allocator_v1());
2361 template <class InsertionProxy>
2362 iterator priv_forward_range_insert_at_end
2363 (const size_type n, const InsertionProxy insert_range_proxy, allocator_v2)
2365 return this->priv_forward_range_insert(vector_iterator_get_ptr(this->cend()), n, insert_range_proxy, allocator_v2());
2368 //Absolutely experimental. This function might change, disappear or simply crash!
2369 template<class BiDirPosConstIt, class BiDirSkipConstIt, class BiDirValueIt>
2370 void priv_insert_ordered_at( size_type element_count, BiDirPosConstIt last_position_it
2371 , bool do_skip, BiDirSkipConstIt last_skip_it, BiDirValueIt last_value_it)
2373 const size_type old_size_pos = this->size();
2374 this->reserve(old_size_pos + element_count);
2375 T* const begin_ptr = container_detail::to_raw_pointer(this->m_holder.start());
2376 size_type insertions_left = element_count;
2377 size_type next_pos = old_size_pos;
2378 size_type hole_size = element_count;
2380 //Exception rollback. If any copy throws before the hole is filled, values
2381 //already inserted/copied at the end of the buffer will be destroyed.
2382 typename value_traits::ArrayDestructor past_hole_values_destroyer
2383 (begin_ptr + old_size_pos + element_count, this->m_holder.alloc(), size_type(0u));
2384 //Loop for each insertion backwards, first moving the elements after the insertion point,
2385 //then inserting the element.
2386 while(insertions_left){
2388 size_type n = *(--last_skip_it);
2389 std::advance(last_value_it, -difference_type(n));
2391 const size_type pos = static_cast<size_type>(*(--last_position_it));
2392 BOOST_ASSERT(pos <= old_size_pos);
2393 //If needed shift the range after the insertion point and the previous insertion point.
2394 //Function will take care if the shift crosses the size() boundary, using copy/move
2395 //or uninitialized copy/move if necessary.
2396 size_type new_hole_size = (pos != next_pos)
2397 ? priv_insert_ordered_at_shift_range(pos, next_pos, this->size(), insertions_left)
2400 if(new_hole_size > 0){
2401 //The hole was reduced by priv_insert_ordered_at_shift_range so expand exception rollback range backwards
2402 past_hole_values_destroyer.increment_size_backwards(next_pos - pos);
2403 //Insert the new value in the hole
2404 allocator_traits_type::construct(this->m_holder.alloc(), begin_ptr + pos + insertions_left - 1, *(--last_value_it));
2406 if(new_hole_size == 0){
2407 //Hole was just filled, disable exception rollback and change vector size
2408 past_hole_values_destroyer.release();
2409 this->m_holder.m_size += element_count;
2412 //The hole was reduced by the new insertion by one
2413 past_hole_values_destroyer.increment_size_backwards(size_type(1u));
2418 //Hole was just filled by priv_insert_ordered_at_shift_range, disable exception rollback and change vector size
2419 past_hole_values_destroyer.release();
2420 this->m_holder.m_size += element_count;
2422 //Insert the new value in the already constructed range
2423 begin_ptr[pos + insertions_left - 1] = *(--last_value_it);
2426 hole_size = new_hole_size;
2431 //Takes the range pointed by [first_pos, last_pos) and shifts it to the right
2432 //by 'shift_count'. 'limit_pos' marks the end of constructed elements.
2434 //Precondition: first_pos <= last_pos <= limit_pos
2436 //The shift operation might cross limit_pos so elements to moved beyond limit_pos
2437 //are uninitialized_moved with an allocator. Other elements are moved.
2439 //The shift operation might left uninitialized elements after limit_pos
2440 //and the number of uninitialized elements is returned by the function.
2443 // first_pos last_pos old_limit
2445 // ____________V_______V__________________V_____________
2446 //| prefix | range | suffix |raw_mem ~
2447 //|____________|_______|__________________|_____________~
2449 //New situation in Case Allocator (hole_size == 0):
2450 // range is moved through move assignments
2452 // first_pos last_pos limit_pos
2454 // ____________V_______V__________________V_____________
2455 //| prefix' | | | range |suffix'|raw_mem ~
2456 //|________________+______|___^___|_______|_____________~
2461 //New situation in Case B (hole_size > 0):
2462 // range is moved through uninitialized moves
2464 // first_pos last_pos limit_pos
2466 // ____________V_______V__________________V________________
2467 //| prefix' | | | [hole] | range |
2468 //|_______________________________________|________|___^___|
2470 // |_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_^
2472 //New situation in Case C (hole_size == 0):
2473 // range is moved through move assignments and uninitialized moves
2475 // first_pos last_pos limit_pos
2477 // ____________V_______V__________________V___
2478 //| prefix' | | | range |
2479 //|___________________________________|___^___|
2481 // |_>_>_>_>_>_>_>_>_>_>_>^
2482 size_type priv_insert_ordered_at_shift_range
2483 (size_type first_pos, size_type last_pos, size_type limit_pos, size_type shift_count)
2485 BOOST_ASSERT(first_pos <= last_pos);
2486 BOOST_ASSERT(last_pos <= limit_pos);
2488 T* const begin_ptr = container_detail::to_raw_pointer(this->m_holder.start());
2489 T* const first_ptr = begin_ptr + first_pos;
2490 T* const last_ptr = begin_ptr + last_pos;
2492 size_type hole_size = 0;
2494 if((last_pos + shift_count) <= limit_pos){
2496 boost::move_backward(first_ptr, last_ptr, last_ptr + shift_count);
2499 else if((first_pos + shift_count) >= limit_pos){
2500 //All uninitialized_moved
2501 ::boost::container::uninitialized_move_alloc
2502 (this->m_holder.alloc(), first_ptr, last_ptr, first_ptr + shift_count);
2503 hole_size = last_pos + shift_count - limit_pos;
2507 //Some uninitialized_moved
2508 T* const limit_ptr = begin_ptr + limit_pos;
2509 T* const boundary_ptr = limit_ptr - shift_count;
2510 ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), boundary_ptr, last_ptr, limit_ptr);
2511 //The rest is move assigned
2512 boost::move_backward(first_ptr, boundary_ptr, limit_ptr);
2518 template <class InsertionProxy>
2519 void priv_forward_range_insert_at_end_expand_forward(const size_type n, InsertionProxy insert_range_proxy)
2521 T* const old_finish = container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size;
2522 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n);
2523 this->m_holder.m_size += n;
2526 template <class InsertionProxy>
2527 void priv_forward_range_insert_expand_forward(T* const pos, const size_type n, InsertionProxy insert_range_proxy)
2529 //n can't be 0, because there is nothing to do in that case
2531 //There is enough memory
2532 T* const old_finish = container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size;
2533 const size_type elems_after = old_finish - pos;
2536 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n);
2537 this->m_holder.m_size += n;
2539 else if (elems_after >= n){
2540 //New elements can be just copied.
2541 //Move to uninitialized memory last objects
2542 ::boost::container::uninitialized_move_alloc
2543 (this->m_holder.alloc(), old_finish - n, old_finish, old_finish);
2544 this->m_holder.m_size += n;
2545 //Copy previous to last objects to the initialized end
2546 boost::move_backward(pos, old_finish - n, old_finish);
2547 //Insert new objects in the pos
2548 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, n);
2551 //The new elements don't fit in the [pos, end()) range.
2553 //Copy old [pos, end()) elements to the uninitialized memory (a gap is created)
2554 ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), pos, old_finish, pos + n);
2556 //Copy first new elements in pos (gap is still there)
2557 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, elems_after);
2558 //Copy to the beginning of the unallocated zone the last new elements (the gap is closed).
2559 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n - elems_after);
2560 this->m_holder.m_size += n;
2563 boost::container::destroy_alloc_n(this->get_stored_allocator(), pos + n, elems_after);
2570 template <class InsertionProxy>
2571 void priv_forward_range_insert_new_allocation
2572 (T* const new_start, size_type new_cap, T* const pos, const size_type n, InsertionProxy insert_range_proxy)
2574 //n can be zero, if we want to reallocate!
2575 T *new_finish = new_start;
2577 //Anti-exception rollbacks
2578 typename value_traits::ArrayDeallocator new_buffer_deallocator(new_start, this->m_holder.alloc(), new_cap);
2579 typename value_traits::ArrayDestructor new_values_destroyer(new_start, this->m_holder.alloc(), 0u);
2581 //Initialize with [begin(), pos) old buffer
2582 //the start of the new buffer
2583 T * const old_buffer = container_detail::to_raw_pointer(this->m_holder.start());
2585 new_finish = ::boost::container::uninitialized_move_alloc
2586 (this->m_holder.alloc(), container_detail::to_raw_pointer(this->m_holder.start()), pos, old_finish = new_finish);
2587 new_values_destroyer.increment_size(new_finish - old_finish);
2589 //Initialize new objects, starting from previous point
2590 old_finish = new_finish;
2591 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n);
2593 new_values_destroyer.increment_size(new_finish - old_finish);
2594 //Initialize from the rest of the old buffer,
2595 //starting from previous point
2597 new_finish = ::boost::container::uninitialized_move_alloc
2598 (this->m_holder.alloc(), pos, old_buffer + this->m_holder.m_size, new_finish);
2599 //Destroy and deallocate old elements
2600 //If there is allocated memory, destroy and deallocate
2601 if(!value_traits::trivial_dctr_after_move)
2602 boost::container::destroy_alloc_n(this->get_stored_allocator(), old_buffer, this->m_holder.m_size);
2603 this->m_holder.alloc().deallocate(this->m_holder.start(), this->m_holder.capacity());
2605 this->m_holder.start(new_start);
2606 this->m_holder.m_size = new_finish - new_start;
2607 this->m_holder.capacity(new_cap);
2608 //All construction successful, disable rollbacks
2609 new_values_destroyer.release();
2610 new_buffer_deallocator.release();
2613 template <class InsertionProxy>
2614 void priv_forward_range_insert_expand_backwards
2615 (T* const new_start, const size_type new_capacity,
2616 T* const pos, const size_type n, InsertionProxy insert_range_proxy)
2618 //n can be zero to just expand capacity
2620 T* const old_start = container_detail::to_raw_pointer(this->m_holder.start());
2621 const size_type old_size = this->m_holder.m_size;
2622 T* const old_finish = old_start + old_size;
2624 //We can have 8 possibilities:
2625 const size_type elemsbefore = static_cast<size_type>(pos - old_start);
2626 const size_type s_before = static_cast<size_type>(old_start - new_start);
2627 const size_type before_plus_new = elemsbefore + n;
2629 //Update the vector buffer information to a safe state
2630 this->m_holder.start(new_start);
2631 this->m_holder.capacity(new_capacity);
2632 this->m_holder.m_size = 0;
2634 //If anything goes wrong, this object will destroy
2635 //all the old objects to fulfill previous vector state
2636 typename value_traits::ArrayDestructor old_values_destroyer(old_start, this->m_holder.alloc(), old_size);
2637 //Check if s_before is big enough to hold the beginning of old data + new data
2638 if(s_before >= before_plus_new){
2639 //Copy first old values before pos, after that the new objects
2640 T *const new_elem_pos =
2641 ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), old_start, pos, new_start);
2642 this->m_holder.m_size = elemsbefore;
2643 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), new_elem_pos, n);
2644 this->m_holder.m_size = before_plus_new;
2645 const size_type new_size = old_size + n;
2646 //Check if s_before is so big that even copying the old data + new data
2647 //there is a gap between the new data and the old data
2648 if(s_before >= new_size){
2650 // _________________________________________________________
2651 //| raw_mem | old_begin | old_end |
2652 //| __________________________________|___________|_________|
2655 // _________________________________________________________
2656 //| old_begin | new | old_end | raw_mem |
2657 //|___________|__________|_________|________________________|
2659 //Now initialize the rest of memory with the last old values
2660 if(before_plus_new != new_size){ //Special case to avoid operations in back insertion
2661 ::boost::container::uninitialized_move_alloc
2662 (this->m_holder.alloc(), pos, old_finish, new_start + before_plus_new);
2663 //All new elements correctly constructed, avoid new element destruction
2664 this->m_holder.m_size = new_size;
2666 //Old values destroyed automatically with "old_values_destroyer"
2667 //when "old_values_destroyer" goes out of scope unless the have trivial
2668 //destructor after move.
2669 if(value_traits::trivial_dctr_after_move)
2670 old_values_destroyer.release();
2672 //s_before is so big that divides old_end
2675 // __________________________________________________
2676 //| raw_mem | old_begin | old_end |
2677 //| ___________________________|___________|_________|
2680 // __________________________________________________
2681 //| old_begin | new | old_end | raw_mem |
2682 //|___________|__________|_________|_________________|
2684 //Now initialize the rest of memory with the last old values
2685 //All new elements correctly constructed, avoid new element destruction
2686 const size_type raw_gap = s_before - before_plus_new;
2687 if(!value_traits::trivial_dctr){
2688 //Now initialize the rest of s_before memory with the
2689 //first of elements after new values
2690 ::boost::container::uninitialized_move_alloc_n
2691 (this->m_holder.alloc(), pos, raw_gap, new_start + before_plus_new);
2692 //Now we have a contiguous buffer so program trailing element destruction
2693 //and update size to the final size.
2694 old_values_destroyer.shrink_forward(new_size-s_before);
2695 this->m_holder.m_size = new_size;
2696 //Now move remaining last objects in the old buffer begin
2697 ::boost::move(pos + raw_gap, old_finish, old_start);
2698 //Once moved, avoid calling the destructors if trivial after move
2699 if(value_traits::trivial_dctr_after_move){
2700 old_values_destroyer.release();
2703 else{ //If trivial destructor, we can uninitialized copy + copy in a single uninitialized copy
2704 ::boost::container::uninitialized_move_alloc_n
2705 (this->m_holder.alloc(), pos, old_finish - pos, new_start + before_plus_new);
2706 this->m_holder.m_size = new_size;
2707 old_values_destroyer.release();
2712 //Check if we have to do the insertion in two phases
2713 //since maybe s_before is not big enough and
2714 //the buffer was expanded both sides
2717 // _________________________________________________
2718 //| raw_mem | old_begin + old_end | raw_mem |
2719 //|_________|_____________________|_________________|
2721 //New situation with do_after:
2722 // _________________________________________________
2723 //| old_begin + new + old_end | raw_mem |
2724 //|___________________________________|_____________|
2726 //New without do_after:
2727 // _________________________________________________
2728 //| old_begin + new + old_end | raw_mem |
2729 //|____________________________|____________________|
2731 const bool do_after = n > s_before;
2733 //Now we can have two situations: the raw_mem of the
2734 //beginning divides the old_begin, or the new elements:
2735 if (s_before <= elemsbefore) {
2736 //The raw memory divides the old_begin group:
2738 //If we need two phase construction (do_after)
2739 //new group is divided in new = new_beg + new_end groups
2740 //In this phase only new_beg will be inserted
2743 // _________________________________________________
2744 //| raw_mem | old_begin | old_end | raw_mem |
2745 //|_________|___________|_________|_________________|
2747 //New situation with do_after(1):
2748 //This is not definitive situation, the second phase
2750 // _________________________________________________
2751 //| old_begin | new_beg | old_end | raw_mem |
2752 //|___________|_________|_________|_________________|
2754 //New situation without do_after:
2755 // _________________________________________________
2756 //| old_begin | new | old_end | raw_mem |
2757 //|___________|_____|_________|_____________________|
2759 //Copy the first part of old_begin to raw_mem
2760 ::boost::container::uninitialized_move_alloc_n
2761 (this->m_holder.alloc(), old_start, s_before, new_start);
2762 //The buffer is all constructed until old_end,
2763 //so program trailing destruction and assign final size
2764 //if !do_after, s_before+n otherwise.
2765 size_type new_1st_range;
2767 new_1st_range = s_before;
2768 //release destroyer and update size
2769 old_values_destroyer.release();
2773 if(value_traits::trivial_dctr_after_move)
2774 old_values_destroyer.release();
2776 old_values_destroyer.shrink_forward(old_size - (s_before - n));
2779 this->m_holder.m_size = old_size + new_1st_range;
2780 //Now copy the second part of old_begin overwriting itself
2781 T *const next = ::boost::move(old_start + s_before, pos, old_start);
2782 //Now copy the new_beg elements
2783 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), next, new_1st_range);
2785 //If there is no after work and the last old part needs to be moved to front, do it
2786 if(!do_after && (n != s_before)){
2787 //Now displace old_end elements
2788 ::boost::move(pos, old_finish, next + new_1st_range);
2792 //If we have to expand both sides,
2793 //we will play if the first new values so
2794 //calculate the upper bound of new values
2796 //The raw memory divides the new elements
2798 //If we need two phase construction (do_after)
2799 //new group is divided in new = new_beg + new_end groups
2800 //In this phase only new_beg will be inserted
2803 // _______________________________________________________
2804 //| raw_mem | old_begin | old_end | raw_mem |
2805 //|_______________|___________|_________|_________________|
2807 //New situation with do_after():
2808 // ____________________________________________________
2809 //| old_begin | new_beg | old_end | raw_mem |
2810 //|___________|_______________|_________|______________|
2812 //New situation without do_after:
2813 // ______________________________________________________
2814 //| old_begin | new | old_end | raw_mem |
2815 //|___________|_____|_________|__________________________|
2817 //First copy whole old_begin and part of new to raw_mem
2818 T * const new_pos = ::boost::container::uninitialized_move_alloc
2819 (this->m_holder.alloc(), old_start, pos, new_start);
2820 this->m_holder.m_size = elemsbefore;
2821 const size_type mid_n = s_before - elemsbefore;
2822 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), new_pos, mid_n);
2823 //The buffer is all constructed until old_end,
2825 this->m_holder.m_size = old_size + s_before;
2826 old_values_destroyer.release();
2830 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), old_start, elemsbefore);
2833 //Copy all new elements
2834 const size_type rest_new = n - mid_n;
2835 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), old_start, rest_new);
2836 T* const move_start = old_start + rest_new;
2838 T* const move_end = ::boost::move(pos, old_finish, move_start);
2839 //Destroy remaining moved elements from old_end except if they
2840 //have trivial destructor after being moved
2841 size_type n_destroy = s_before - n;
2842 if(!value_traits::trivial_dctr_after_move)
2843 boost::container::destroy_alloc_n(this->get_stored_allocator(), move_end, n_destroy);
2844 this->m_holder.m_size -= n_destroy;
2848 //This is only executed if two phase construction is needed
2850 //The raw memory divides the new elements
2853 // ______________________________________________________
2854 //| raw_mem | old_begin | old_end | raw_mem |
2855 //|______________|___________|____________|______________|
2857 //New situation with do_after(1):
2858 // _______________________________________________________
2859 //| old_begin + new_beg | new_end |old_end | raw_mem |
2860 //|__________________________|_________|________|_________|
2862 //New situation with do_after(2):
2863 // ______________________________________________________
2864 //| old_begin + new | old_end |raw |
2865 //|_______________________________________|_________|____|
2867 const size_type n_after = n - s_before;
2868 const size_type elemsafter = old_size - elemsbefore;
2870 //We can have two situations:
2871 if (elemsafter >= n_after){
2872 //The raw_mem from end will divide displaced old_end
2875 // ______________________________________________________
2876 //| raw_mem | old_begin | old_end | raw_mem |
2877 //|______________|___________|____________|______________|
2879 //New situation with do_after(1):
2880 // _______________________________________________________
2881 //| old_begin + new_beg | new_end |old_end | raw_mem |
2882 //|__________________________|_________|________|_________|
2884 //First copy the part of old_end raw_mem
2885 T* finish_n = old_finish - n_after;
2886 ::boost::container::uninitialized_move_alloc
2887 (this->m_holder.alloc(), finish_n, old_finish, old_finish);
2888 this->m_holder.m_size += n_after;
2889 //Displace the rest of old_end to the new position
2890 boost::move_backward(pos, finish_n, old_finish);
2891 //Now overwrite with new_end
2892 //The new_end part is [first + (n - n_after), last)
2893 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, n_after);
2896 //The raw_mem from end will divide new_end part
2899 // _____________________________________________________________
2900 //| raw_mem | old_begin | old_end | raw_mem |
2901 //|______________|___________|____________|_____________________|
2903 //New situation with do_after(2):
2904 // _____________________________________________________________
2905 //| old_begin + new_beg | new_end |old_end | raw_mem |
2906 //|__________________________|_______________|________|_________|
2909 const size_type mid_last_dist = n_after - elemsafter;
2910 //First initialize data in raw memory
2912 //Copy to the old_end part to the uninitialized zone leaving a gap.
2913 ::boost::container::uninitialized_move_alloc
2914 (this->m_holder.alloc(), pos, old_finish, old_finish + mid_last_dist);
2916 typename value_traits::ArrayDestructor old_end_destroyer
2917 (old_finish + mid_last_dist, this->m_holder.alloc(), old_finish - pos);
2919 //Copy the first part to the already constructed old_end zone
2920 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, elemsafter);
2921 //Copy the rest to the uninitialized zone filling the gap
2922 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, mid_last_dist);
2923 this->m_holder.m_size += n_after;
2924 old_end_destroyer.release();
2930 void priv_check_range(size_type n) const
2932 //If n is out of range, throw an out_of_range exception
2933 if (n >= this->size()){
2934 throw_out_of_range("vector::at out of range");
2938 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2940 unsigned int num_expand_fwd;
2941 unsigned int num_expand_bwd;
2942 unsigned int num_shrink;
2943 unsigned int num_alloc;
2944 void reset_alloc_stats()
2945 { num_expand_fwd = num_expand_bwd = num_alloc = 0, num_shrink = 0; }
2947 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2952 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2957 //!has_trivial_destructor_after_move<> == true_type
2958 //!specialization for optimizations
2959 template <class T, class Allocator>
2960 struct has_trivial_destructor_after_move<boost::container::vector<T, Allocator> >
2961 : public ::boost::has_trivial_destructor_after_move<Allocator>
2966 //#define BOOST_CONTAINER_PUT_SWAP_OVERLOAD_IN_NAMESPACE_STD
2968 #ifdef BOOST_CONTAINER_PUT_SWAP_OVERLOAD_IN_NAMESPACE_STD
2972 template <class T, class Allocator>
2973 inline void swap(boost::container::vector<T, Allocator>& x, boost::container::vector<T, Allocator>& y)
2980 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2982 #include <boost/container/detail/config_end.hpp>
2984 #endif // #ifndef BOOST_CONTAINER_CONTAINER_VECTOR_HPP