1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2015. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/container for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_CONTAINER_CONTAINER_VECTOR_HPP
12 #define BOOST_CONTAINER_CONTAINER_VECTOR_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/container/detail/config_begin.hpp>
23 #include <boost/container/detail/workaround.hpp>
26 #include <boost/container/container_fwd.hpp>
27 #include <boost/container/allocator_traits.hpp>
28 #include <boost/container/new_allocator.hpp> //new_allocator
29 #include <boost/container/throw_exception.hpp>
31 #include <boost/container/detail/advanced_insert_int.hpp>
32 #include <boost/container/detail/algorithm.hpp> //equal()
33 #include <boost/container/detail/alloc_helpers.hpp>
34 #include <boost/container/detail/allocation_type.hpp>
35 #include <boost/container/detail/copy_move_algo.hpp>
36 #include <boost/container/detail/destroyers.hpp>
37 #include <boost/container/detail/iterator.hpp>
38 #include <boost/container/detail/iterators.hpp>
39 #include <boost/move/detail/iterator_to_raw_pointer.hpp>
40 #include <boost/container/detail/mpl.hpp>
41 #include <boost/container/detail/next_capacity.hpp>
42 #include <boost/move/detail/to_raw_pointer.hpp>
43 #include <boost/container/detail/type_traits.hpp>
44 #include <boost/container/detail/version_type.hpp>
46 #include <boost/intrusive/pointer_traits.hpp>
48 #include <boost/move/adl_move_swap.hpp>
49 #include <boost/move/iterator.hpp>
50 #include <boost/move/traits.hpp>
51 #include <boost/move/utility_core.hpp>
53 #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
54 #include <boost/move/detail/fwd_macros.hpp>
56 #include <boost/move/detail/move_helpers.hpp>
58 #include <boost/move/algo/adaptive_merge.hpp>
59 #include <boost/move/algo/unique.hpp>
60 #include <boost/move/algo/predicate.hpp>
62 #include <boost/core/no_exceptions_support.hpp>
63 #include <boost/assert.hpp>
64 #include <boost/cstdint.hpp>
67 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
68 #include <initializer_list> //for std::initializer_list
74 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
76 namespace container_detail {
78 template <class Pointer, bool IsConst>
82 typedef std::random_access_iterator_tag iterator_category;
83 typedef typename boost::intrusive::pointer_traits<Pointer>::element_type value_type;
84 typedef typename boost::intrusive::pointer_traits<Pointer>::difference_type difference_type;
87 , typename boost::intrusive::pointer_traits<Pointer>::template
88 rebind_pointer<const value_type>::type
91 typedef typename boost::intrusive::pointer_traits<pointer> ptr_traits;
92 typedef typename ptr_traits::reference reference;
94 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
99 BOOST_CONTAINER_FORCEINLINE const Pointer &get_ptr() const BOOST_NOEXCEPT_OR_NOTHROW
102 BOOST_CONTAINER_FORCEINLINE Pointer &get_ptr() BOOST_NOEXCEPT_OR_NOTHROW
105 BOOST_CONTAINER_FORCEINLINE explicit vec_iterator(Pointer ptr) BOOST_NOEXCEPT_OR_NOTHROW
108 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
113 BOOST_CONTAINER_FORCEINLINE vec_iterator() BOOST_NOEXCEPT_OR_NOTHROW
114 : m_ptr() //Value initialization to achieve "null iterators" (N3644)
117 BOOST_CONTAINER_FORCEINLINE vec_iterator(vec_iterator<Pointer, false> const& other) BOOST_NOEXCEPT_OR_NOTHROW
118 : m_ptr(other.get_ptr())
121 //Pointer like operators
122 BOOST_CONTAINER_FORCEINLINE reference operator*() const BOOST_NOEXCEPT_OR_NOTHROW
125 BOOST_CONTAINER_FORCEINLINE pointer operator->() const BOOST_NOEXCEPT_OR_NOTHROW
126 { return ::boost::intrusive::pointer_traits<pointer>::pointer_to(this->operator*()); }
128 BOOST_CONTAINER_FORCEINLINE reference operator[](difference_type off) const BOOST_NOEXCEPT_OR_NOTHROW
129 { return m_ptr[off]; }
131 //Increment / Decrement
132 BOOST_CONTAINER_FORCEINLINE vec_iterator& operator++() BOOST_NOEXCEPT_OR_NOTHROW
133 { ++m_ptr; return *this; }
135 BOOST_CONTAINER_FORCEINLINE vec_iterator operator++(int) BOOST_NOEXCEPT_OR_NOTHROW
136 { return vec_iterator(m_ptr++); }
138 BOOST_CONTAINER_FORCEINLINE vec_iterator& operator--() BOOST_NOEXCEPT_OR_NOTHROW
139 { --m_ptr; return *this; }
141 BOOST_CONTAINER_FORCEINLINE vec_iterator operator--(int) BOOST_NOEXCEPT_OR_NOTHROW
142 { return vec_iterator(m_ptr--); }
145 BOOST_CONTAINER_FORCEINLINE vec_iterator& operator+=(difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
146 { m_ptr += off; return *this; }
148 BOOST_CONTAINER_FORCEINLINE vec_iterator& operator-=(difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
149 { m_ptr -= off; return *this; }
151 BOOST_CONTAINER_FORCEINLINE friend vec_iterator operator+(const vec_iterator &x, difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
152 { return vec_iterator(x.m_ptr+off); }
154 BOOST_CONTAINER_FORCEINLINE friend vec_iterator operator+(difference_type off, vec_iterator right) BOOST_NOEXCEPT_OR_NOTHROW
155 { right.m_ptr += off; return right; }
157 BOOST_CONTAINER_FORCEINLINE friend vec_iterator operator-(vec_iterator left, difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
158 { left.m_ptr -= off; return left; }
160 BOOST_CONTAINER_FORCEINLINE friend difference_type operator-(const vec_iterator &left, const vec_iterator& right) BOOST_NOEXCEPT_OR_NOTHROW
161 { return left.m_ptr - right.m_ptr; }
163 //Comparison operators
164 BOOST_CONTAINER_FORCEINLINE friend bool operator== (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
165 { return l.m_ptr == r.m_ptr; }
167 BOOST_CONTAINER_FORCEINLINE friend bool operator!= (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
168 { return l.m_ptr != r.m_ptr; }
170 BOOST_CONTAINER_FORCEINLINE friend bool operator< (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
171 { return l.m_ptr < r.m_ptr; }
173 BOOST_CONTAINER_FORCEINLINE friend bool operator<= (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
174 { return l.m_ptr <= r.m_ptr; }
176 BOOST_CONTAINER_FORCEINLINE friend bool operator> (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
177 { return l.m_ptr > r.m_ptr; }
179 BOOST_CONTAINER_FORCEINLINE friend bool operator>= (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
180 { return l.m_ptr >= r.m_ptr; }
183 template<class BiDirPosConstIt, class BiDirValueIt>
184 struct vector_insert_ordered_cursor
186 typedef typename iterator_traits<BiDirPosConstIt>::value_type size_type;
187 typedef typename iterator_traits<BiDirValueIt>::reference reference;
189 BOOST_CONTAINER_FORCEINLINE vector_insert_ordered_cursor(BiDirPosConstIt posit, BiDirValueIt valueit)
190 : last_position_it(posit), last_value_it(valueit)
197 while(this->get_pos() == size_type(-1)){
203 BOOST_CONTAINER_FORCEINLINE size_type get_pos() const
204 { return *last_position_it; }
206 BOOST_CONTAINER_FORCEINLINE reference get_val()
207 { return *last_value_it; }
209 BiDirPosConstIt last_position_it;
210 BiDirValueIt last_value_it;
213 template<class T, class SizeType, class BiDirValueIt, class Comp>
214 struct vector_merge_cursor
216 typedef SizeType size_type;
217 typedef typename iterator_traits<BiDirValueIt>::reference reference;
219 BOOST_CONTAINER_FORCEINLINE vector_merge_cursor(T *pbeg, T *plast, BiDirValueIt valueit, Comp &cmp)
220 : m_pbeg(pbeg), m_pcur(--plast), m_valueit(valueit), m_cmp(cmp)
226 const T &t = *m_valueit;
227 while((m_pcur + 1) != m_pbeg){
228 if(!m_cmp(t, *m_pcur)){
235 BOOST_CONTAINER_FORCEINLINE size_type get_pos() const
236 { return static_cast<size_type>((m_pcur + 1) - m_pbeg); }
238 BOOST_CONTAINER_FORCEINLINE reference get_val()
239 { return *m_valueit; }
243 BiDirValueIt m_valueit;
247 } //namespace container_detail {
249 template<class Pointer, bool IsConst>
250 BOOST_CONTAINER_FORCEINLINE const Pointer &vector_iterator_get_ptr(const container_detail::vec_iterator<Pointer, IsConst> &it) BOOST_NOEXCEPT_OR_NOTHROW
251 { return it.get_ptr(); }
253 template<class Pointer, bool IsConst>
254 BOOST_CONTAINER_FORCEINLINE Pointer &get_ptr(container_detail::vec_iterator<Pointer, IsConst> &it) BOOST_NOEXCEPT_OR_NOTHROW
255 { return it.get_ptr(); }
257 namespace container_detail {
259 struct uninitialized_size_t {};
260 static const uninitialized_size_t uninitialized_size = uninitialized_size_t();
263 struct vector_value_traits_base
265 static const bool trivial_dctr = is_trivially_destructible<T>::value;
266 static const bool trivial_dctr_after_move = has_trivial_destructor_after_move<T>::value;
267 static const bool trivial_copy = is_trivially_copy_constructible<T>::value;
268 static const bool nothrow_copy = is_nothrow_copy_constructible<T>::value || trivial_copy;
269 static const bool trivial_assign = is_trivially_copy_assignable<T>::value;
270 static const bool nothrow_assign = is_nothrow_copy_assignable<T>::value || trivial_assign;
274 template <class Allocator>
275 struct vector_value_traits
276 : public vector_value_traits_base<typename Allocator::value_type>
278 typedef vector_value_traits_base<typename Allocator::value_type> base_t;
279 //This is the anti-exception array destructor
280 //to deallocate values already constructed
281 typedef typename container_detail::if_c
282 <base_t::trivial_dctr
283 ,container_detail::null_scoped_destructor_n<Allocator>
284 ,container_detail::scoped_destructor_n<Allocator>
285 >::type ArrayDestructor;
286 //This is the anti-exception array deallocator
287 typedef container_detail::scoped_array_deallocator<Allocator> ArrayDeallocator;
290 //!This struct deallocates and allocated memory
291 template < class Allocator
292 , class AllocatorVersion = typename container_detail::version<Allocator>::type
294 struct vector_alloc_holder
298 BOOST_MOVABLE_BUT_NOT_COPYABLE(vector_alloc_holder)
301 typedef Allocator allocator_type;
302 typedef boost::container::allocator_traits<Allocator> allocator_traits_type;
303 typedef typename allocator_traits_type::pointer pointer;
304 typedef typename allocator_traits_type::size_type size_type;
305 typedef typename allocator_traits_type::value_type value_type;
307 static bool is_propagable_from(const allocator_type &from_alloc, pointer p, const allocator_type &to_alloc, bool const propagate_allocator)
309 (void)propagate_allocator; (void)p; (void)to_alloc; (void)from_alloc;
310 const bool all_storage_propagable = !allocator_traits_type::is_partially_propagable::value ||
311 !allocator_traits_type::storage_is_unpropagable(from_alloc, p);
312 return all_storage_propagable && (propagate_allocator || allocator_traits_type::equal(from_alloc, to_alloc));
315 static bool are_swap_propagable(const allocator_type &l_a, pointer l_p, const allocator_type &r_a, pointer r_p, bool const propagate_allocator)
317 (void)propagate_allocator; (void)l_p; (void)r_p; (void)l_a; (void)r_a;
318 const bool all_storage_propagable = !allocator_traits_type::is_partially_propagable::value ||
319 !(allocator_traits_type::storage_is_unpropagable(l_a, l_p) || allocator_traits_type::storage_is_unpropagable(r_a, r_p));
320 return all_storage_propagable && (propagate_allocator || allocator_traits_type::equal(l_a, r_a));
323 //Constructor, does not throw
324 vector_alloc_holder()
325 BOOST_NOEXCEPT_IF(container_detail::is_nothrow_default_constructible<Allocator>::value)
326 : Allocator(), m_start(), m_size(), m_capacity()
329 //Constructor, does not throw
330 template<class AllocConvertible>
331 explicit vector_alloc_holder(BOOST_FWD_REF(AllocConvertible) a) BOOST_NOEXCEPT_OR_NOTHROW
332 : Allocator(boost::forward<AllocConvertible>(a)), m_start(), m_size(), m_capacity()
335 //Constructor, does not throw
336 template<class AllocConvertible>
337 vector_alloc_holder(uninitialized_size_t, BOOST_FWD_REF(AllocConvertible) a, size_type initial_size)
338 : Allocator(boost::forward<AllocConvertible>(a))
340 , m_size(initial_size) //Size is initialized here so vector should only call uninitialized_xxx after this
344 pointer reuse = pointer();
345 m_start = this->allocation_command(allocate_new, initial_size, m_capacity = initial_size, reuse);
349 //Constructor, does not throw
350 vector_alloc_holder(uninitialized_size_t, size_type initial_size)
353 , m_size(initial_size) //Size is initialized here so vector should only call uninitialized_xxx after this
357 pointer reuse = pointer();
358 m_start = this->allocation_command(allocate_new, initial_size, m_capacity = initial_size, reuse);
362 vector_alloc_holder(BOOST_RV_REF(vector_alloc_holder) holder) BOOST_NOEXCEPT_OR_NOTHROW
363 : Allocator(BOOST_MOVE_BASE(Allocator, holder))
364 , m_start(holder.m_start)
365 , m_size(holder.m_size)
366 , m_capacity(holder.m_capacity)
368 holder.m_start = pointer();
369 holder.m_size = holder.m_capacity = 0;
372 vector_alloc_holder(pointer p, size_type capacity, BOOST_RV_REF(vector_alloc_holder) holder)
373 : Allocator(BOOST_MOVE_BASE(Allocator, holder))
375 , m_size(holder.m_size)
376 , m_capacity(capacity)
378 allocator_type &this_alloc = this->alloc();
379 allocator_type &x_alloc = holder.alloc();
380 if(this->is_propagable_from(x_alloc, holder.start(), this_alloc, true)){
381 if(this->m_capacity){
382 allocator_traits_type::deallocate(this->alloc(), this->m_start, this->m_capacity);
384 m_start = holder.m_start;
385 m_capacity = holder.m_capacity;
386 holder.m_start = pointer();
387 holder.m_capacity = holder.m_size = 0;
389 else if(this->m_capacity < holder.m_size){
390 size_type const n = holder.m_size;
391 pointer reuse = pointer();
392 m_start = this->allocation_command(allocate_new, n, m_capacity = n, reuse);
393 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
394 this->num_alloc += n != 0;
399 vector_alloc_holder(pointer p, size_type n)
400 BOOST_NOEXCEPT_IF(container_detail::is_nothrow_default_constructible<Allocator>::value)
407 template<class AllocFwd>
408 vector_alloc_holder(pointer p, size_type n, BOOST_FWD_REF(AllocFwd) a)
409 : Allocator(::boost::forward<AllocFwd>(a))
415 BOOST_CONTAINER_FORCEINLINE ~vector_alloc_holder() BOOST_NOEXCEPT_OR_NOTHROW
417 if(this->m_capacity){
418 allocator_traits_type::deallocate(this->alloc(), this->m_start, this->m_capacity);
422 BOOST_CONTAINER_FORCEINLINE pointer allocation_command(boost::container::allocation_type command,
423 size_type limit_size, size_type &prefer_in_recvd_out_size, pointer &reuse)
425 typedef typename container_detail::version<Allocator>::type alloc_version;
426 return this->priv_allocation_command(alloc_version(), command, limit_size, prefer_in_recvd_out_size, reuse);
429 bool try_expand_fwd(size_type at_least)
431 //There is not enough memory, try to expand the old one
432 const size_type new_cap = this->capacity() + at_least;
433 size_type real_cap = new_cap;
434 pointer reuse = this->start();
435 bool const success = !!this->allocation_command(expand_fwd, new_cap, real_cap, reuse);
436 //Check for forward expansion
438 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
439 ++this->num_expand_fwd;
441 this->capacity(real_cap);
446 BOOST_CONTAINER_FORCEINLINE size_type next_capacity(size_type additional_objects) const
448 return next_capacity_calculator
449 <size_type, NextCapacityDouble //NextCapacity60Percent
450 >::get( allocator_traits_type::max_size(this->alloc())
451 , this->m_capacity, additional_objects );
456 size_type m_capacity;
458 void swap_resources(vector_alloc_holder &x) BOOST_NOEXCEPT_OR_NOTHROW
460 boost::adl_move_swap(this->m_start, x.m_start);
461 boost::adl_move_swap(this->m_size, x.m_size);
462 boost::adl_move_swap(this->m_capacity, x.m_capacity);
465 void steal_resources(vector_alloc_holder &x) BOOST_NOEXCEPT_OR_NOTHROW
467 this->m_start = x.m_start;
468 this->m_size = x.m_size;
469 this->m_capacity = x.m_capacity;
470 x.m_start = pointer();
471 x.m_size = x.m_capacity = 0;
474 BOOST_CONTAINER_FORCEINLINE Allocator &alloc() BOOST_NOEXCEPT_OR_NOTHROW
477 BOOST_CONTAINER_FORCEINLINE const Allocator &alloc() const BOOST_NOEXCEPT_OR_NOTHROW
480 const pointer &start() const BOOST_NOEXCEPT_OR_NOTHROW { return m_start; }
481 const size_type &capacity() const BOOST_NOEXCEPT_OR_NOTHROW { return m_capacity; }
482 void start(const pointer &p) BOOST_NOEXCEPT_OR_NOTHROW { m_start = p; }
483 void capacity(const size_type &c) BOOST_NOEXCEPT_OR_NOTHROW { m_capacity = c; }
486 void priv_first_allocation(size_type cap)
489 pointer reuse = pointer();
490 m_start = this->allocation_command(allocate_new, cap, cap, reuse);
492 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
498 BOOST_CONTAINER_FORCEINLINE pointer priv_allocation_command(version_1, boost::container::allocation_type command,
500 size_type &prefer_in_recvd_out_size,
504 BOOST_ASSERT( (command & allocate_new));
505 BOOST_ASSERT(!(command & nothrow_allocation));
506 pointer const p = allocator_traits_type::allocate(this->alloc(), prefer_in_recvd_out_size, reuse);
511 pointer priv_allocation_command(version_2, boost::container::allocation_type command,
512 size_type limit_size,
513 size_type &prefer_in_recvd_out_size,
516 return this->alloc().allocation_command(command, limit_size, prefer_in_recvd_out_size, reuse);
520 //!This struct deallocates and allocated memory
521 template <class Allocator>
522 struct vector_alloc_holder<Allocator, version_0>
526 BOOST_MOVABLE_BUT_NOT_COPYABLE(vector_alloc_holder)
529 typedef boost::container::allocator_traits<Allocator> allocator_traits_type;
530 typedef typename allocator_traits_type::pointer pointer;
531 typedef typename allocator_traits_type::size_type size_type;
532 typedef typename allocator_traits_type::value_type value_type;
534 template <class OtherAllocator, class OtherAllocatorVersion>
535 friend struct vector_alloc_holder;
537 //Constructor, does not throw
538 vector_alloc_holder()
539 BOOST_NOEXCEPT_IF(container_detail::is_nothrow_default_constructible<Allocator>::value)
540 : Allocator(), m_size()
543 //Constructor, does not throw
544 template<class AllocConvertible>
545 explicit vector_alloc_holder(BOOST_FWD_REF(AllocConvertible) a) BOOST_NOEXCEPT_OR_NOTHROW
546 : Allocator(boost::forward<AllocConvertible>(a)), m_size()
549 //Constructor, does not throw
550 template<class AllocConvertible>
551 vector_alloc_holder(uninitialized_size_t, BOOST_FWD_REF(AllocConvertible) a, size_type initial_size)
552 : Allocator(boost::forward<AllocConvertible>(a))
553 , m_size(initial_size) //Size is initialized here...
555 //... and capacity here, so vector, must call uninitialized_xxx in the derived constructor
556 this->priv_first_allocation(initial_size);
559 //Constructor, does not throw
560 vector_alloc_holder(uninitialized_size_t, size_type initial_size)
562 , m_size(initial_size) //Size is initialized here...
564 //... and capacity here, so vector, must call uninitialized_xxx in the derived constructor
565 this->priv_first_allocation(initial_size);
568 vector_alloc_holder(BOOST_RV_REF(vector_alloc_holder) holder)
569 : Allocator(BOOST_MOVE_BASE(Allocator, holder))
570 , m_size(holder.m_size) //Size is initialized here so vector should only call uninitialized_xxx after this
572 ::boost::container::uninitialized_move_alloc_n
573 (this->alloc(), boost::movelib::to_raw_pointer(holder.start()), m_size, boost::movelib::to_raw_pointer(this->start()));
576 template<class OtherAllocator, class OtherAllocatorVersion>
577 vector_alloc_holder(BOOST_RV_REF_BEG vector_alloc_holder<OtherAllocator, OtherAllocatorVersion> BOOST_RV_REF_END holder)
579 , m_size(holder.m_size) //Initialize it to m_size as first_allocation can only succeed or abort
581 //Different allocator type so we must check we have enough storage
582 const size_type n = holder.m_size;
583 this->priv_first_allocation(n);
584 ::boost::container::uninitialized_move_alloc_n
585 (this->alloc(), boost::movelib::to_raw_pointer(holder.start()), n, boost::movelib::to_raw_pointer(this->start()));
588 BOOST_CONTAINER_FORCEINLINE void priv_first_allocation(size_type cap)
590 if(cap > Allocator::internal_capacity){
595 BOOST_CONTAINER_FORCEINLINE void deep_swap(vector_alloc_holder &x)
597 this->priv_deep_swap(x);
600 template<class OtherAllocator, class OtherAllocatorVersion>
601 void deep_swap(vector_alloc_holder<OtherAllocator, OtherAllocatorVersion> &x)
603 if(this->m_size > OtherAllocator::internal_capacity || x.m_size > Allocator::internal_capacity){
606 this->priv_deep_swap(x);
609 BOOST_CONTAINER_FORCEINLINE void swap_resources(vector_alloc_holder &) BOOST_NOEXCEPT_OR_NOTHROW
610 { //Containers with version 0 allocators can't be moved without moving elements one by one
615 BOOST_CONTAINER_FORCEINLINE void steal_resources(vector_alloc_holder &)
616 { //Containers with version 0 allocators can't be moved without moving elements one by one
620 BOOST_CONTAINER_FORCEINLINE Allocator &alloc() BOOST_NOEXCEPT_OR_NOTHROW
623 BOOST_CONTAINER_FORCEINLINE const Allocator &alloc() const BOOST_NOEXCEPT_OR_NOTHROW
626 BOOST_CONTAINER_FORCEINLINE bool try_expand_fwd(size_type at_least)
627 { return !at_least; }
629 BOOST_CONTAINER_FORCEINLINE pointer start() const BOOST_NOEXCEPT_OR_NOTHROW { return Allocator::internal_storage(); }
630 BOOST_CONTAINER_FORCEINLINE size_type capacity() const BOOST_NOEXCEPT_OR_NOTHROW { return Allocator::internal_capacity; }
635 template<class OtherAllocator, class OtherAllocatorVersion>
636 void priv_deep_swap(vector_alloc_holder<OtherAllocator, OtherAllocatorVersion> &x)
638 const size_type MaxTmpStorage = sizeof(value_type)*Allocator::internal_capacity;
639 value_type *const first_this = boost::movelib::to_raw_pointer(this->start());
640 value_type *const first_x = boost::movelib::to_raw_pointer(x.start());
642 if(this->m_size < x.m_size){
643 boost::container::deep_swap_alloc_n<MaxTmpStorage>(this->alloc(), first_this, this->m_size, first_x, x.m_size);
646 boost::container::deep_swap_alloc_n<MaxTmpStorage>(this->alloc(), first_x, x.m_size, first_this, this->m_size);
648 boost::adl_move_swap(this->m_size, x.m_size);
652 } //namespace container_detail {
654 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
656 //! A vector is a sequence that supports random access to elements, constant
657 //! time insertion and removal of elements at the end, and linear time insertion
658 //! and removal of elements at the beginning or in the middle. The number of
659 //! elements in a vector may vary dynamically; memory management is automatic.
661 //! \tparam T The type of object that is stored in the vector
662 //! \tparam Allocator The allocator used for all internal memory management
663 template <class T, class Allocator BOOST_CONTAINER_DOCONLY(= new_allocator<T>) >
666 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
670 typedef typename boost::container::allocator_traits<Allocator>::value_type value_type;
671 bool operator()(const value_type &a, const value_type &b) const
675 typedef typename container_detail::version<Allocator>::type alloc_version;
676 typedef boost::container::container_detail::vector_alloc_holder<Allocator> alloc_holder_t;
677 alloc_holder_t m_holder;
678 typedef allocator_traits<Allocator> allocator_traits_type;
679 template <class U, class UAllocator>
682 typedef typename allocator_traits_type::pointer pointer_impl;
683 typedef container_detail::vec_iterator<pointer_impl, false> iterator_impl;
684 typedef container_detail::vec_iterator<pointer_impl, true > const_iterator_impl;
687 static bool is_propagable_from(const Allocator &from_alloc, pointer_impl p, const Allocator &to_alloc, bool const propagate_allocator)
688 { return alloc_holder_t::is_propagable_from(from_alloc, p, to_alloc, propagate_allocator); }
690 static bool are_swap_propagable( const Allocator &l_a, pointer_impl l_p
691 , const Allocator &r_a, pointer_impl r_p, bool const propagate_allocator)
692 { return alloc_holder_t::are_swap_propagable(l_a, l_p, r_a, r_p, propagate_allocator); }
694 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
696 //////////////////////////////////////////////
700 //////////////////////////////////////////////
702 typedef T value_type;
703 typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
704 typedef typename ::boost::container::allocator_traits<Allocator>::const_pointer const_pointer;
705 typedef typename ::boost::container::allocator_traits<Allocator>::reference reference;
706 typedef typename ::boost::container::allocator_traits<Allocator>::const_reference const_reference;
707 typedef typename ::boost::container::allocator_traits<Allocator>::size_type size_type;
708 typedef typename ::boost::container::allocator_traits<Allocator>::difference_type difference_type;
709 typedef Allocator allocator_type;
710 typedef Allocator stored_allocator_type;
711 typedef BOOST_CONTAINER_IMPDEF(iterator_impl) iterator;
712 typedef BOOST_CONTAINER_IMPDEF(const_iterator_impl) const_iterator;
713 typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<iterator>) reverse_iterator;
714 typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<const_iterator>) const_reverse_iterator;
716 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
718 BOOST_COPYABLE_AND_MOVABLE(vector)
719 typedef container_detail::vector_value_traits<Allocator> value_traits;
720 typedef constant_iterator<T, difference_type> cvalue_iterator;
724 BOOST_CONTAINER_FORCEINLINE void steal_resources(vector &x)
725 { return this->m_holder.steal_resources(x.m_holder); }
727 struct initial_capacity_t{};
728 template<class AllocFwd>
729 BOOST_CONTAINER_FORCEINLINE vector(initial_capacity_t, pointer initial_memory, size_type capacity, BOOST_FWD_REF(AllocFwd) a)
730 : m_holder(initial_memory, capacity, ::boost::forward<AllocFwd>(a))
733 BOOST_CONTAINER_FORCEINLINE vector(initial_capacity_t, pointer initial_memory, size_type capacity)
734 : m_holder(initial_memory, capacity)
737 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
740 //////////////////////////////////////////////
742 // construct/copy/destroy
744 //////////////////////////////////////////////
746 //! <b>Effects</b>: Constructs a vector taking the allocator as parameter.
748 //! <b>Throws</b>: Nothing.
750 //! <b>Complexity</b>: Constant.
751 vector() BOOST_NOEXCEPT_IF(container_detail::is_nothrow_default_constructible<Allocator>::value)
755 //! <b>Effects</b>: Constructs a vector taking the allocator as parameter.
757 //! <b>Throws</b>: Nothing
759 //! <b>Complexity</b>: Constant.
760 explicit vector(const allocator_type& a) BOOST_NOEXCEPT_OR_NOTHROW
764 //! <b>Effects</b>: Constructs a vector and inserts n value initialized values.
766 //! <b>Throws</b>: If allocator_type's allocation
767 //! throws or T's value initialization throws.
769 //! <b>Complexity</b>: Linear to n.
770 explicit vector(size_type n)
771 : m_holder(container_detail::uninitialized_size, n)
773 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
774 this->num_alloc += n != 0;
776 boost::container::uninitialized_value_init_alloc_n
777 (this->m_holder.alloc(), n, this->priv_raw_begin());
780 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
781 //! and inserts n value initialized values.
783 //! <b>Throws</b>: If allocator_type's allocation
784 //! throws or T's value initialization throws.
786 //! <b>Complexity</b>: Linear to n.
787 explicit vector(size_type n, const allocator_type &a)
788 : m_holder(container_detail::uninitialized_size, a, n)
790 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
791 this->num_alloc += n != 0;
793 boost::container::uninitialized_value_init_alloc_n
794 (this->m_holder.alloc(), n, this->priv_raw_begin());
797 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
798 //! and inserts n default initialized values.
800 //! <b>Throws</b>: If allocator_type's allocation
801 //! throws or T's default initialization throws.
803 //! <b>Complexity</b>: Linear to n.
805 //! <b>Note</b>: Non-standard extension
806 vector(size_type n, default_init_t)
807 : m_holder(container_detail::uninitialized_size, n)
809 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
810 this->num_alloc += n != 0;
812 boost::container::uninitialized_default_init_alloc_n
813 (this->m_holder.alloc(), n, this->priv_raw_begin());
816 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
817 //! and inserts n default initialized values.
819 //! <b>Throws</b>: If allocator_type's allocation
820 //! throws or T's default initialization throws.
822 //! <b>Complexity</b>: Linear to n.
824 //! <b>Note</b>: Non-standard extension
825 vector(size_type n, default_init_t, const allocator_type &a)
826 : m_holder(container_detail::uninitialized_size, a, n)
828 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
829 this->num_alloc += n != 0;
831 boost::container::uninitialized_default_init_alloc_n
832 (this->m_holder.alloc(), n, this->priv_raw_begin());
835 //! <b>Effects</b>: Constructs a vector
836 //! and inserts n copies of value.
838 //! <b>Throws</b>: If allocator_type's allocation
839 //! throws or T's copy constructor throws.
841 //! <b>Complexity</b>: Linear to n.
842 vector(size_type n, const T& value)
843 : m_holder(container_detail::uninitialized_size, n)
845 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
846 this->num_alloc += n != 0;
848 boost::container::uninitialized_fill_alloc_n
849 (this->m_holder.alloc(), value, n, this->priv_raw_begin());
852 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
853 //! and inserts n copies of value.
855 //! <b>Throws</b>: If allocation
856 //! throws or T's copy constructor throws.
858 //! <b>Complexity</b>: Linear to n.
859 vector(size_type n, const T& value, const allocator_type& a)
860 : m_holder(container_detail::uninitialized_size, a, n)
862 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
863 this->num_alloc += n != 0;
865 boost::container::uninitialized_fill_alloc_n
866 (this->m_holder.alloc(), value, n, this->priv_raw_begin());
869 //! <b>Effects</b>: Constructs a vector
870 //! and inserts a copy of the range [first, last) in the vector.
872 //! <b>Throws</b>: If allocator_type's allocation
873 //! throws or T's constructor taking a dereferenced InIt throws.
875 //! <b>Complexity</b>: Linear to the range [first, last).
876 template <class InIt>
877 vector(InIt first, InIt last
878 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename container_detail::disable_if_c
879 < container_detail::is_convertible<InIt BOOST_MOVE_I size_type>::value
880 BOOST_MOVE_I container_detail::nat >::type * = 0)
883 { this->assign(first, last); }
885 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
886 //! and inserts a copy of the range [first, last) in the vector.
888 //! <b>Throws</b>: If allocator_type's allocation
889 //! throws or T's constructor taking a dereferenced InIt throws.
891 //! <b>Complexity</b>: Linear to the range [first, last).
892 template <class InIt>
893 vector(InIt first, InIt last, const allocator_type& a
894 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename container_detail::disable_if_c
895 < container_detail::is_convertible<InIt BOOST_MOVE_I size_type>::value
896 BOOST_MOVE_I container_detail::nat >::type * = 0)
899 { this->assign(first, last); }
901 //! <b>Effects</b>: Copy constructs a vector.
903 //! <b>Postcondition</b>: x == *this.
905 //! <b>Throws</b>: If allocator_type's allocation
906 //! throws or T's copy constructor throws.
908 //! <b>Complexity</b>: Linear to the elements x contains.
909 vector(const vector &x)
910 : m_holder( container_detail::uninitialized_size
911 , allocator_traits_type::select_on_container_copy_construction(x.m_holder.alloc())
914 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
915 this->num_alloc += x.size() != 0;
917 ::boost::container::uninitialized_copy_alloc_n
918 ( this->m_holder.alloc(), x.priv_raw_begin()
919 , x.size(), this->priv_raw_begin());
922 //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
924 //! <b>Throws</b>: Nothing
926 //! <b>Complexity</b>: Constant.
927 vector(BOOST_RV_REF(vector) x) BOOST_NOEXCEPT_OR_NOTHROW
928 : m_holder(boost::move(x.m_holder))
929 { BOOST_STATIC_ASSERT((!allocator_traits_type::is_partially_propagable::value)); }
931 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
932 //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
933 //! and inserts a copy of the range [il.begin(), il.last()) in the vector
935 //! <b>Throws</b>: If T's constructor taking a dereferenced initializer_list iterator throws.
937 //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
938 vector(std::initializer_list<value_type> il, const allocator_type& a = allocator_type())
941 this->assign(il.begin(), il.end());
945 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
947 //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
949 //! <b>Throws</b>: If T's move constructor or allocation throws
951 //! <b>Complexity</b>: Linear.
953 //! <b>Note</b>: Non-standard extension to support static_vector
954 template<class OtherAllocator>
955 vector(BOOST_RV_REF_BEG vector<T, OtherAllocator> BOOST_RV_REF_END x
956 , typename container_detail::enable_if_c
957 < container_detail::is_version<OtherAllocator, 0>::value>::type * = 0
959 : m_holder(boost::move(x.m_holder))
962 #endif //!defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
964 //! <b>Effects</b>: Copy constructs a vector using the specified allocator.
966 //! <b>Postcondition</b>: x == *this.
968 //! <b>Throws</b>: If allocation
969 //! throws or T's copy constructor throws.
971 //! <b>Complexity</b>: Linear to the elements x contains.
972 vector(const vector &x, const allocator_type &a)
973 : m_holder(container_detail::uninitialized_size, a, x.size())
975 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
976 this->num_alloc += x.size() != 0;
978 ::boost::container::uninitialized_copy_alloc_n_source
979 ( this->m_holder.alloc(), x.priv_raw_begin()
980 , x.size(), this->priv_raw_begin());
983 //! <b>Effects</b>: Move constructor using the specified allocator.
984 //! Moves x's resources to *this if a == allocator_type().
985 //! Otherwise copies values from x to *this.
987 //! <b>Throws</b>: If allocation or T's copy constructor throws.
989 //! <b>Complexity</b>: Constant if a == x.get_allocator(), linear otherwise.
990 vector(BOOST_RV_REF(vector) x, const allocator_type &a)
991 : m_holder( container_detail::uninitialized_size, a
992 , is_propagable_from(x.get_stored_allocator(), x.m_holder.start(), a, true) ? 0 : x.size()
995 if(is_propagable_from(x.get_stored_allocator(), x.m_holder.start(), a, true)){
996 this->m_holder.steal_resources(x.m_holder);
999 const size_type n = x.size();
1000 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
1001 this->num_alloc += n != 0;
1003 ::boost::container::uninitialized_move_alloc_n_source
1004 ( this->m_holder.alloc(), x.priv_raw_begin()
1005 , n, this->priv_raw_begin());
1009 //! <b>Effects</b>: Destroys the vector. All stored values are destroyed
1010 //! and used memory is deallocated.
1012 //! <b>Throws</b>: Nothing.
1014 //! <b>Complexity</b>: Linear to the number of elements.
1015 ~vector() BOOST_NOEXCEPT_OR_NOTHROW
1017 boost::container::destroy_alloc_n
1018 (this->get_stored_allocator(), this->priv_raw_begin(), this->m_holder.m_size);
1019 //vector_alloc_holder deallocates the data
1022 //! <b>Effects</b>: Makes *this contain the same elements as x.
1024 //! <b>Postcondition</b>: this->size() == x.size(). *this contains a copy
1025 //! of each of x's elements.
1027 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment throws.
1029 //! <b>Complexity</b>: Linear to the number of elements in x.
1030 BOOST_CONTAINER_FORCEINLINE vector& operator=(BOOST_COPY_ASSIGN_REF(vector) x)
1033 this->priv_copy_assign(x);
1038 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1039 //! <b>Effects</b>: Make *this container contains elements from il.
1041 //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
1042 BOOST_CONTAINER_FORCEINLINE vector& operator=(std::initializer_list<value_type> il)
1044 this->assign(il.begin(), il.end());
1049 //! <b>Effects</b>: Move assignment. All x's values are transferred to *this.
1051 //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
1052 //! before the function.
1054 //! <b>Throws</b>: If allocator_traits_type::propagate_on_container_move_assignment
1055 //! is false and (allocation throws or value_type's move constructor throws)
1057 //! <b>Complexity</b>: Constant if allocator_traits_type::
1058 //! propagate_on_container_move_assignment is true or
1059 //! this->get>allocator() == x.get_allocator(). Linear otherwise.
1060 BOOST_CONTAINER_FORCEINLINE vector& operator=(BOOST_RV_REF(vector) x)
1061 BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value
1062 || allocator_traits_type::is_always_equal::value)
1064 BOOST_ASSERT(&x != this);
1065 this->priv_move_assign(boost::move(x));
1069 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1071 //! <b>Effects</b>: Move assignment. All x's values are transferred to *this.
1073 //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
1074 //! before the function.
1076 //! <b>Throws</b>: If move constructor/assignment of T throws or allocation throws
1078 //! <b>Complexity</b>: Linear.
1080 //! <b>Note</b>: Non-standard extension to support static_vector
1081 template<class OtherAllocator>
1082 BOOST_CONTAINER_FORCEINLINE typename container_detail::enable_if_and
1084 , container_detail::is_version<OtherAllocator, 0>
1085 , container_detail::is_different<OtherAllocator, allocator_type>
1087 operator=(BOOST_RV_REF_BEG vector<value_type, OtherAllocator> BOOST_RV_REF_END x)
1089 this->priv_move_assign(boost::move(x));
1093 //! <b>Effects</b>: Copy assignment. All x's values are copied to *this.
1095 //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
1096 //! before the function.
1098 //! <b>Throws</b>: If move constructor/assignment of T throws or allocation throws
1100 //! <b>Complexity</b>: Linear.
1102 //! <b>Note</b>: Non-standard extension to support static_vector
1103 template<class OtherAllocator>
1104 BOOST_CONTAINER_FORCEINLINE typename container_detail::enable_if_and
1106 , container_detail::is_version<OtherAllocator, 0>
1107 , container_detail::is_different<OtherAllocator, allocator_type>
1109 operator=(const vector<value_type, OtherAllocator> &x)
1111 this->priv_copy_assign(x);
1117 //! <b>Effects</b>: Assigns the the range [first, last) to *this.
1119 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment or
1120 //! T's constructor/assignment from dereferencing InpIt throws.
1122 //! <b>Complexity</b>: Linear to n.
1123 template <class InIt>
1124 void assign(InIt first, InIt last
1125 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename container_detail::disable_if_or
1127 BOOST_MOVE_I container_detail::is_convertible<InIt BOOST_MOVE_I size_type>
1128 BOOST_MOVE_I container_detail::and_
1129 < container_detail::is_different<alloc_version BOOST_MOVE_I version_0>
1130 BOOST_MOVE_I container_detail::is_not_input_iterator<InIt>
1135 //Overwrite all elements we can from [first, last)
1136 iterator cur = this->begin();
1137 const iterator end_it = this->end();
1138 for ( ; first != last && cur != end_it; ++cur, ++first){
1143 //There are no more elements in the sequence, erase remaining
1144 T* const end_pos = this->priv_raw_end();
1145 const size_type n = static_cast<size_type>(end_pos - boost::movelib::iterator_to_raw_pointer(cur));
1146 this->priv_destroy_last_n(n);
1149 //There are more elements in the range, insert the remaining ones
1150 this->insert(this->cend(), first, last);
1154 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1155 //! <b>Effects</b>: Assigns the the range [il.begin(), il.end()) to *this.
1157 //! <b>Throws</b>: If memory allocation throws or
1158 //! T's constructor from dereferencing iniializer_list iterator throws.
1160 BOOST_CONTAINER_FORCEINLINE void assign(std::initializer_list<T> il)
1162 this->assign(il.begin(), il.end());
1166 //! <b>Effects</b>: Assigns the the range [first, last) to *this.
1168 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment or
1169 //! T's constructor/assignment from dereferencing InpIt throws.
1171 //! <b>Complexity</b>: Linear to n.
1172 template <class FwdIt>
1173 void assign(FwdIt first, FwdIt last
1174 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename container_detail::disable_if_or
1176 BOOST_MOVE_I container_detail::is_same<alloc_version BOOST_MOVE_I version_0>
1177 BOOST_MOVE_I container_detail::is_convertible<FwdIt BOOST_MOVE_I size_type>
1178 BOOST_MOVE_I container_detail::is_input_iterator<FwdIt>
1182 //For Fwd iterators the standard only requires EmplaceConstructible and assignable from *first
1183 //so we can't do any backwards allocation
1184 const size_type input_sz = static_cast<size_type>(boost::container::iterator_distance(first, last));
1185 const size_type old_capacity = this->capacity();
1186 if(input_sz > old_capacity){ //If input range is too big, we need to reallocate
1187 size_type real_cap = 0;
1188 pointer reuse(this->m_holder.start());
1189 pointer const ret(this->m_holder.allocation_command(allocate_new|expand_fwd, input_sz, real_cap = input_sz, reuse));
1190 if(!reuse){ //New allocation, just emplace new values
1191 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
1194 pointer const old_p = this->m_holder.start();
1196 this->priv_destroy_all();
1197 allocator_traits_type::deallocate(this->m_holder.alloc(), old_p, old_capacity);
1199 this->m_holder.start(ret);
1200 this->m_holder.capacity(real_cap);
1201 this->m_holder.m_size = 0;
1202 this->priv_uninitialized_construct_at_end(first, last);
1206 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
1207 ++this->num_expand_fwd;
1209 this->m_holder.capacity(real_cap);
1210 //Forward expansion, use assignment + back deletion/construction that comes later
1213 //Overwrite all elements we can from [first, last)
1214 iterator cur = this->begin();
1215 const iterator end_it = this->end();
1216 for ( ; first != last && cur != end_it; ++cur, ++first){
1221 //There are no more elements in the sequence, erase remaining
1222 this->priv_destroy_last_n(this->size() - input_sz);
1225 //Uninitialized construct at end the remaining range
1226 this->priv_uninitialized_construct_at_end(first, last);
1230 //! <b>Effects</b>: Assigns the n copies of val to *this.
1232 //! <b>Throws</b>: If memory allocation throws or
1233 //! T's copy/move constructor/assignment throws.
1235 //! <b>Complexity</b>: Linear to n.
1236 BOOST_CONTAINER_FORCEINLINE void assign(size_type n, const value_type& val)
1237 { this->assign(cvalue_iterator(val, n), cvalue_iterator()); }
1239 //! <b>Effects</b>: Returns a copy of the internal allocator.
1241 //! <b>Throws</b>: If allocator's copy constructor throws.
1243 //! <b>Complexity</b>: Constant.
1244 allocator_type get_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
1245 { return this->m_holder.alloc(); }
1247 //! <b>Effects</b>: Returns a reference to the internal allocator.
1249 //! <b>Throws</b>: Nothing
1251 //! <b>Complexity</b>: Constant.
1253 //! <b>Note</b>: Non-standard extension.
1254 BOOST_CONTAINER_FORCEINLINE stored_allocator_type &get_stored_allocator() BOOST_NOEXCEPT_OR_NOTHROW
1255 { return this->m_holder.alloc(); }
1257 //! <b>Effects</b>: Returns a reference to the internal allocator.
1259 //! <b>Throws</b>: Nothing
1261 //! <b>Complexity</b>: Constant.
1263 //! <b>Note</b>: Non-standard extension.
1264 BOOST_CONTAINER_FORCEINLINE const stored_allocator_type &get_stored_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
1265 { return this->m_holder.alloc(); }
1267 //////////////////////////////////////////////
1271 //////////////////////////////////////////////
1273 //! <b>Effects</b>: Returns an iterator to the first element contained in the vector.
1275 //! <b>Throws</b>: Nothing.
1277 //! <b>Complexity</b>: Constant.
1278 BOOST_CONTAINER_FORCEINLINE iterator begin() BOOST_NOEXCEPT_OR_NOTHROW
1279 { return iterator(this->m_holder.start()); }
1281 //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
1283 //! <b>Throws</b>: Nothing.
1285 //! <b>Complexity</b>: Constant.
1286 BOOST_CONTAINER_FORCEINLINE const_iterator begin() const BOOST_NOEXCEPT_OR_NOTHROW
1287 { return const_iterator(this->m_holder.start()); }
1289 //! <b>Effects</b>: Returns an iterator to the end of the vector.
1291 //! <b>Throws</b>: Nothing.
1293 //! <b>Complexity</b>: Constant.
1294 BOOST_CONTAINER_FORCEINLINE iterator end() BOOST_NOEXCEPT_OR_NOTHROW
1295 { return iterator(this->m_holder.start() + this->m_holder.m_size); }
1297 //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
1299 //! <b>Throws</b>: Nothing.
1301 //! <b>Complexity</b>: Constant.
1302 BOOST_CONTAINER_FORCEINLINE const_iterator end() const BOOST_NOEXCEPT_OR_NOTHROW
1303 { return this->cend(); }
1305 //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
1306 //! of the reversed vector.
1308 //! <b>Throws</b>: Nothing.
1310 //! <b>Complexity</b>: Constant.
1311 BOOST_CONTAINER_FORCEINLINE reverse_iterator rbegin() BOOST_NOEXCEPT_OR_NOTHROW
1312 { return reverse_iterator(this->end()); }
1314 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
1315 //! of the reversed vector.
1317 //! <b>Throws</b>: Nothing.
1319 //! <b>Complexity</b>: Constant.
1320 BOOST_CONTAINER_FORCEINLINE const_reverse_iterator rbegin() const BOOST_NOEXCEPT_OR_NOTHROW
1321 { return this->crbegin(); }
1323 //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
1324 //! of the reversed vector.
1326 //! <b>Throws</b>: Nothing.
1328 //! <b>Complexity</b>: Constant.
1329 BOOST_CONTAINER_FORCEINLINE reverse_iterator rend() BOOST_NOEXCEPT_OR_NOTHROW
1330 { return reverse_iterator(this->begin()); }
1332 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
1333 //! of the reversed vector.
1335 //! <b>Throws</b>: Nothing.
1337 //! <b>Complexity</b>: Constant.
1338 BOOST_CONTAINER_FORCEINLINE const_reverse_iterator rend() const BOOST_NOEXCEPT_OR_NOTHROW
1339 { return this->crend(); }
1341 //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
1343 //! <b>Throws</b>: Nothing.
1345 //! <b>Complexity</b>: Constant.
1346 BOOST_CONTAINER_FORCEINLINE const_iterator cbegin() const BOOST_NOEXCEPT_OR_NOTHROW
1347 { return const_iterator(this->m_holder.start()); }
1349 //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
1351 //! <b>Throws</b>: Nothing.
1353 //! <b>Complexity</b>: Constant.
1354 BOOST_CONTAINER_FORCEINLINE const_iterator cend() const BOOST_NOEXCEPT_OR_NOTHROW
1355 { return const_iterator(this->m_holder.start() + this->m_holder.m_size); }
1357 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
1358 //! of the reversed vector.
1360 //! <b>Throws</b>: Nothing.
1362 //! <b>Complexity</b>: Constant.
1363 BOOST_CONTAINER_FORCEINLINE const_reverse_iterator crbegin() const BOOST_NOEXCEPT_OR_NOTHROW
1364 { return const_reverse_iterator(this->end());}
1366 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
1367 //! of the reversed vector.
1369 //! <b>Throws</b>: Nothing.
1371 //! <b>Complexity</b>: Constant.
1372 BOOST_CONTAINER_FORCEINLINE const_reverse_iterator crend() const BOOST_NOEXCEPT_OR_NOTHROW
1373 { return const_reverse_iterator(this->begin()); }
1375 //////////////////////////////////////////////
1379 //////////////////////////////////////////////
1381 //! <b>Effects</b>: Returns true if the vector contains no elements.
1383 //! <b>Throws</b>: Nothing.
1385 //! <b>Complexity</b>: Constant.
1386 BOOST_CONTAINER_FORCEINLINE bool empty() const BOOST_NOEXCEPT_OR_NOTHROW
1387 { return !this->m_holder.m_size; }
1389 //! <b>Effects</b>: Returns the number of the elements contained in the vector.
1391 //! <b>Throws</b>: Nothing.
1393 //! <b>Complexity</b>: Constant.
1394 BOOST_CONTAINER_FORCEINLINE size_type size() const BOOST_NOEXCEPT_OR_NOTHROW
1395 { return this->m_holder.m_size; }
1397 //! <b>Effects</b>: Returns the largest possible size of the vector.
1399 //! <b>Throws</b>: Nothing.
1401 //! <b>Complexity</b>: Constant.
1402 BOOST_CONTAINER_FORCEINLINE size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
1403 { return allocator_traits_type::max_size(this->m_holder.alloc()); }
1405 //! <b>Effects</b>: Inserts or erases elements at the end such that
1406 //! the size becomes n. New elements are value initialized.
1408 //! <b>Throws</b>: If memory allocation throws, or T's copy/move or value initialization throws.
1410 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1411 void resize(size_type new_size)
1412 { this->priv_resize(new_size, value_init); }
1414 //! <b>Effects</b>: Inserts or erases elements at the end such that
1415 //! the size becomes n. New elements are default initialized.
1417 //! <b>Throws</b>: If memory allocation throws, or T's copy/move or default initialization throws.
1419 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1421 //! <b>Note</b>: Non-standard extension
1422 void resize(size_type new_size, default_init_t)
1423 { this->priv_resize(new_size, default_init); }
1425 //! <b>Effects</b>: Inserts or erases elements at the end such that
1426 //! the size becomes n. New elements are copy constructed from x.
1428 //! <b>Throws</b>: If memory allocation throws, or T's copy/move constructor throws.
1430 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1431 void resize(size_type new_size, const T& x)
1432 { this->priv_resize(new_size, x); }
1434 //! <b>Effects</b>: Number of elements for which memory has been allocated.
1435 //! capacity() is always greater than or equal to size().
1437 //! <b>Throws</b>: Nothing.
1439 //! <b>Complexity</b>: Constant.
1440 BOOST_CONTAINER_FORCEINLINE size_type capacity() const BOOST_NOEXCEPT_OR_NOTHROW
1441 { return this->m_holder.capacity(); }
1443 //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
1444 //! effect. Otherwise, it is a request for allocation of additional memory.
1445 //! If the request is successful, then capacity() is greater than or equal to
1446 //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
1448 //! <b>Throws</b>: If memory allocation allocation throws or T's copy/move constructor throws.
1449 BOOST_CONTAINER_FORCEINLINE void reserve(size_type new_cap)
1451 if (this->capacity() < new_cap){
1452 this->priv_reserve_no_capacity(new_cap, alloc_version());
1456 //! <b>Effects</b>: Tries to deallocate the excess of memory created
1457 //! with previous allocations. The size of the vector is unchanged
1459 //! <b>Throws</b>: If memory allocation throws, or T's copy/move constructor throws.
1461 //! <b>Complexity</b>: Linear to size().
1462 BOOST_CONTAINER_FORCEINLINE void shrink_to_fit()
1463 { this->priv_shrink_to_fit(alloc_version()); }
1465 //////////////////////////////////////////////
1469 //////////////////////////////////////////////
1471 //! <b>Requires</b>: !empty()
1473 //! <b>Effects</b>: Returns a reference to the first
1474 //! element of the container.
1476 //! <b>Throws</b>: Nothing.
1478 //! <b>Complexity</b>: Constant.
1479 reference front() BOOST_NOEXCEPT_OR_NOTHROW
1481 BOOST_ASSERT(!this->empty());
1482 return *this->m_holder.start();
1485 //! <b>Requires</b>: !empty()
1487 //! <b>Effects</b>: Returns a const reference to the first
1488 //! element of the container.
1490 //! <b>Throws</b>: Nothing.
1492 //! <b>Complexity</b>: Constant.
1493 const_reference front() const BOOST_NOEXCEPT_OR_NOTHROW
1495 BOOST_ASSERT(!this->empty());
1496 return *this->m_holder.start();
1499 //! <b>Requires</b>: !empty()
1501 //! <b>Effects</b>: Returns a reference to the last
1502 //! element of the container.
1504 //! <b>Throws</b>: Nothing.
1506 //! <b>Complexity</b>: Constant.
1507 reference back() BOOST_NOEXCEPT_OR_NOTHROW
1509 BOOST_ASSERT(!this->empty());
1510 return this->m_holder.start()[this->m_holder.m_size - 1];
1513 //! <b>Requires</b>: !empty()
1515 //! <b>Effects</b>: Returns a const reference to the last
1516 //! element of the container.
1518 //! <b>Throws</b>: Nothing.
1520 //! <b>Complexity</b>: Constant.
1521 const_reference back() const BOOST_NOEXCEPT_OR_NOTHROW
1523 BOOST_ASSERT(!this->empty());
1524 return this->m_holder.start()[this->m_holder.m_size - 1];
1527 //! <b>Requires</b>: size() > n.
1529 //! <b>Effects</b>: Returns a reference to the nth element
1530 //! from the beginning of the container.
1532 //! <b>Throws</b>: Nothing.
1534 //! <b>Complexity</b>: Constant.
1535 reference operator[](size_type n) BOOST_NOEXCEPT_OR_NOTHROW
1537 BOOST_ASSERT(this->m_holder.m_size > n);
1538 return this->m_holder.start()[n];
1541 //! <b>Requires</b>: size() > n.
1543 //! <b>Effects</b>: Returns a const reference to the nth element
1544 //! from the beginning of the container.
1546 //! <b>Throws</b>: Nothing.
1548 //! <b>Complexity</b>: Constant.
1549 const_reference operator[](size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
1551 BOOST_ASSERT(this->m_holder.m_size > n);
1552 return this->m_holder.start()[n];
1555 //! <b>Requires</b>: size() >= n.
1557 //! <b>Effects</b>: Returns an iterator to the nth element
1558 //! from the beginning of the container. Returns end()
1561 //! <b>Throws</b>: Nothing.
1563 //! <b>Complexity</b>: Constant.
1565 //! <b>Note</b>: Non-standard extension
1566 iterator nth(size_type n) BOOST_NOEXCEPT_OR_NOTHROW
1568 BOOST_ASSERT(this->m_holder.m_size >= n);
1569 return iterator(this->m_holder.start()+n);
1572 //! <b>Requires</b>: size() >= n.
1574 //! <b>Effects</b>: Returns a const_iterator to the nth element
1575 //! from the beginning of the container. Returns end()
1578 //! <b>Throws</b>: Nothing.
1580 //! <b>Complexity</b>: Constant.
1582 //! <b>Note</b>: Non-standard extension
1583 const_iterator nth(size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
1585 BOOST_ASSERT(this->m_holder.m_size >= n);
1586 return const_iterator(this->m_holder.start()+n);
1589 //! <b>Requires</b>: begin() <= p <= end().
1591 //! <b>Effects</b>: Returns the index of the element pointed by p
1592 //! and size() if p == end().
1594 //! <b>Throws</b>: Nothing.
1596 //! <b>Complexity</b>: Constant.
1598 //! <b>Note</b>: Non-standard extension
1599 size_type index_of(iterator p) BOOST_NOEXCEPT_OR_NOTHROW
1601 //Range check assert done in priv_index_of
1602 return this->priv_index_of(vector_iterator_get_ptr(p));
1605 //! <b>Requires</b>: begin() <= p <= end().
1607 //! <b>Effects</b>: Returns the index of the element pointed by p
1608 //! and size() if p == end().
1610 //! <b>Throws</b>: Nothing.
1612 //! <b>Complexity</b>: Constant.
1614 //! <b>Note</b>: Non-standard extension
1615 size_type index_of(const_iterator p) const BOOST_NOEXCEPT_OR_NOTHROW
1617 //Range check assert done in priv_index_of
1618 return this->priv_index_of(vector_iterator_get_ptr(p));
1621 //! <b>Requires</b>: size() > n.
1623 //! <b>Effects</b>: Returns a reference to the nth element
1624 //! from the beginning of the container.
1626 //! <b>Throws</b>: std::range_error if n >= size()
1628 //! <b>Complexity</b>: Constant.
1629 reference at(size_type n)
1631 this->priv_throw_if_out_of_range(n);
1632 return this->m_holder.start()[n];
1635 //! <b>Requires</b>: size() > n.
1637 //! <b>Effects</b>: Returns a const reference to the nth element
1638 //! from the beginning of the container.
1640 //! <b>Throws</b>: std::range_error if n >= size()
1642 //! <b>Complexity</b>: Constant.
1643 const_reference at(size_type n) const
1645 this->priv_throw_if_out_of_range(n);
1646 return this->m_holder.start()[n];
1649 //////////////////////////////////////////////
1653 //////////////////////////////////////////////
1655 //! <b>Returns</b>: A pointer such that [data(),data() + size()) is a valid range.
1656 //! For a non-empty vector, data() == &front().
1658 //! <b>Throws</b>: Nothing.
1660 //! <b>Complexity</b>: Constant.
1661 T* data() BOOST_NOEXCEPT_OR_NOTHROW
1662 { return this->priv_raw_begin(); }
1664 //! <b>Returns</b>: A pointer such that [data(),data() + size()) is a valid range.
1665 //! For a non-empty vector, data() == &front().
1667 //! <b>Throws</b>: Nothing.
1669 //! <b>Complexity</b>: Constant.
1670 const T * data() const BOOST_NOEXCEPT_OR_NOTHROW
1671 { return this->priv_raw_begin(); }
1673 //////////////////////////////////////////////
1677 //////////////////////////////////////////////
1679 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1680 //! <b>Effects</b>: Inserts an object of type T constructed with
1681 //! std::forward<Args>(args)... in the end of the vector.
1683 //! <b>Returns</b>: A reference to the created object.
1685 //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws or
1686 //! T's copy/move constructor throws.
1688 //! <b>Complexity</b>: Amortized constant time.
1689 template<class ...Args>
1690 BOOST_CONTAINER_FORCEINLINE reference emplace_back(BOOST_FWD_REF(Args)...args)
1692 if (BOOST_LIKELY(this->room_enough())){
1693 //There is more memory, just construct a new object at the end
1694 T* const p = this->priv_raw_end();
1695 allocator_traits_type::construct(this->m_holder.alloc(), p, ::boost::forward<Args>(args)...);
1696 ++this->m_holder.m_size;
1700 typedef container_detail::insert_emplace_proxy<Allocator, T*, Args...> type;
1701 return *this->priv_forward_range_insert_no_capacity
1702 (this->back_ptr(), 1, type(::boost::forward<Args>(args)...), alloc_version());
1706 //! <b>Effects</b>: Inserts an object of type T constructed with
1707 //! std::forward<Args>(args)... in the end of the vector.
1709 //! <b>Throws</b>: If the in-place constructor throws.
1711 //! <b>Complexity</b>: Constant time.
1713 //! <b>Note</b>: Non-standard extension.
1714 template<class ...Args>
1715 BOOST_CONTAINER_FORCEINLINE bool stable_emplace_back(BOOST_FWD_REF(Args)...args)
1717 const bool is_room_enough = this->room_enough() || (alloc_version::value == 2 && this->m_holder.try_expand_fwd(1u));
1718 if (BOOST_LIKELY(is_room_enough)){
1719 //There is more memory, just construct a new object at the end
1720 allocator_traits_type::construct(this->m_holder.alloc(), this->priv_raw_end(), ::boost::forward<Args>(args)...);
1721 ++this->m_holder.m_size;
1723 return is_room_enough;
1726 //! <b>Requires</b>: position must be a valid iterator of *this.
1728 //! <b>Effects</b>: Inserts an object of type T constructed with
1729 //! std::forward<Args>(args)... before position
1731 //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws or
1732 //! T's copy/move constructor/assignment throws.
1734 //! <b>Complexity</b>: If position is end(), amortized constant time
1735 //! Linear time otherwise.
1736 template<class ...Args>
1737 iterator emplace(const_iterator position, BOOST_FWD_REF(Args) ...args)
1739 BOOST_ASSERT(this->priv_in_range_or_end(position));
1740 //Just call more general insert(pos, size, value) and return iterator
1741 typedef container_detail::insert_emplace_proxy<Allocator, T*, Args...> type;
1742 return this->priv_forward_range_insert( vector_iterator_get_ptr(position), 1
1743 , type(::boost::forward<Args>(args)...));
1746 #else // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
1748 #define BOOST_CONTAINER_VECTOR_EMPLACE_CODE(N) \
1749 BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
1750 BOOST_CONTAINER_FORCEINLINE reference emplace_back(BOOST_MOVE_UREF##N)\
1752 if (BOOST_LIKELY(this->room_enough())){\
1753 T* const p = this->priv_raw_end();\
1754 allocator_traits_type::construct (this->m_holder.alloc()\
1755 , this->priv_raw_end() BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
1756 ++this->m_holder.m_size;\
1760 typedef container_detail::insert_emplace_proxy_arg##N<Allocator, T* BOOST_MOVE_I##N BOOST_MOVE_TARG##N> type;\
1761 return *this->priv_forward_range_insert_no_capacity\
1762 ( this->back_ptr(), 1, type(BOOST_MOVE_FWD##N), alloc_version());\
1766 BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
1767 BOOST_CONTAINER_FORCEINLINE bool stable_emplace_back(BOOST_MOVE_UREF##N)\
1769 const bool is_room_enough = this->room_enough() || (alloc_version::value == 2 && this->m_holder.try_expand_fwd(1u));\
1770 if (BOOST_LIKELY(is_room_enough)){\
1771 allocator_traits_type::construct (this->m_holder.alloc()\
1772 , this->priv_raw_end() BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
1773 ++this->m_holder.m_size;\
1775 return is_room_enough;\
1778 BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
1779 iterator emplace(const_iterator pos BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
1781 BOOST_ASSERT(this->priv_in_range_or_end(pos));\
1782 typedef container_detail::insert_emplace_proxy_arg##N<Allocator, T* BOOST_MOVE_I##N BOOST_MOVE_TARG##N> type;\
1783 return this->priv_forward_range_insert(vector_iterator_get_ptr(pos), 1, type(BOOST_MOVE_FWD##N));\
1786 BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_VECTOR_EMPLACE_CODE)
1787 #undef BOOST_CONTAINER_VECTOR_EMPLACE_CODE
1791 #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1792 //! <b>Effects</b>: Inserts a copy of x at the end of the vector.
1794 //! <b>Throws</b>: If memory allocation throws or
1795 //! T's copy/move constructor throws.
1797 //! <b>Complexity</b>: Amortized constant time.
1798 void push_back(const T &x);
1800 //! <b>Effects</b>: Constructs a new element in the end of the vector
1801 //! and moves the resources of x to this new element.
1803 //! <b>Throws</b>: If memory allocation throws or
1804 //! T's copy/move constructor throws.
1806 //! <b>Complexity</b>: Amortized constant time.
1807 void push_back(T &&x);
1809 BOOST_MOVE_CONVERSION_AWARE_CATCH(push_back, T, void, priv_push_back)
1812 #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1813 //! <b>Requires</b>: position must be a valid iterator of *this.
1815 //! <b>Effects</b>: Insert a copy of x before position.
1817 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment throws.
1819 //! <b>Complexity</b>: If position is end(), amortized constant time
1820 //! Linear time otherwise.
1821 iterator insert(const_iterator position, const T &x);
1823 //! <b>Requires</b>: position must be a valid iterator of *this.
1825 //! <b>Effects</b>: Insert a new element before position with x's resources.
1827 //! <b>Throws</b>: If memory allocation throws.
1829 //! <b>Complexity</b>: If position is end(), amortized constant time
1830 //! Linear time otherwise.
1831 iterator insert(const_iterator position, T &&x);
1833 BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, T, iterator, priv_insert, const_iterator, const_iterator)
1836 //! <b>Requires</b>: p must be a valid iterator of *this.
1838 //! <b>Effects</b>: Insert n copies of x before pos.
1840 //! <b>Returns</b>: an iterator to the first inserted element or p if n is 0.
1842 //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor throws.
1844 //! <b>Complexity</b>: Linear to n.
1845 iterator insert(const_iterator p, size_type n, const T& x)
1847 BOOST_ASSERT(this->priv_in_range_or_end(p));
1848 container_detail::insert_n_copies_proxy<Allocator, T*> proxy(x);
1849 return this->priv_forward_range_insert(vector_iterator_get_ptr(p), n, proxy);
1852 //! <b>Requires</b>: p must be a valid iterator of *this.
1854 //! <b>Effects</b>: Insert a copy of the [first, last) range before pos.
1856 //! <b>Returns</b>: an iterator to the first inserted element or pos if first == last.
1858 //! <b>Throws</b>: If memory allocation throws, T's constructor from a
1859 //! dereferenced InpIt throws or T's copy/move constructor/assignment throws.
1861 //! <b>Complexity</b>: Linear to boost::container::iterator_distance [first, last).
1862 template <class InIt>
1863 iterator insert(const_iterator pos, InIt first, InIt last
1864 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1865 , typename container_detail::disable_if_or
1867 , container_detail::is_convertible<InIt, size_type>
1868 , container_detail::is_not_input_iterator<InIt>
1873 BOOST_ASSERT(this->priv_in_range_or_end(pos));
1874 const size_type n_pos = pos - this->cbegin();
1875 iterator it(vector_iterator_get_ptr(pos));
1876 for(;first != last; ++first){
1877 it = this->emplace(it, *first);
1880 return iterator(this->m_holder.start() + n_pos);
1883 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1884 template <class FwdIt>
1885 iterator insert(const_iterator pos, FwdIt first, FwdIt last
1886 , typename container_detail::disable_if_or
1888 , container_detail::is_convertible<FwdIt, size_type>
1889 , container_detail::is_input_iterator<FwdIt>
1893 BOOST_ASSERT(this->priv_in_range_or_end(pos));
1894 container_detail::insert_range_proxy<Allocator, FwdIt, T*> proxy(first);
1895 return this->priv_forward_range_insert(vector_iterator_get_ptr(pos), boost::container::iterator_distance(first, last), proxy);
1899 //! <b>Requires</b>: p must be a valid iterator of *this. num, must
1900 //! be equal to boost::container::iterator_distance(first, last)
1902 //! <b>Effects</b>: Insert a copy of the [first, last) range before pos.
1904 //! <b>Returns</b>: an iterator to the first inserted element or pos if first == last.
1906 //! <b>Throws</b>: If memory allocation throws, T's constructor from a
1907 //! dereferenced InpIt throws or T's copy/move constructor/assignment throws.
1909 //! <b>Complexity</b>: Linear to boost::container::iterator_distance [first, last).
1911 //! <b>Note</b>: This function avoids a linear operation to calculate boost::container::iterator_distance[first, last)
1912 //! for forward and bidirectional iterators, and a one by one insertion for input iterators. This is a
1913 //! a non-standard extension.
1914 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1915 template <class InIt>
1916 iterator insert(const_iterator pos, size_type num, InIt first, InIt last)
1918 BOOST_ASSERT(this->priv_in_range_or_end(pos));
1919 BOOST_ASSERT(container_detail::is_input_iterator<InIt>::value ||
1920 num == static_cast<size_type>(boost::container::iterator_distance(first, last)));
1922 container_detail::insert_range_proxy<Allocator, InIt, T*> proxy(first);
1923 return this->priv_forward_range_insert(vector_iterator_get_ptr(pos), num, proxy);
1927 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1928 //! <b>Requires</b>: position must be a valid iterator of *this.
1930 //! <b>Effects</b>: Insert a copy of the [il.begin(), il.end()) range before position.
1932 //! <b>Returns</b>: an iterator to the first inserted element or position if first == last.
1934 //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
1935 iterator insert(const_iterator position, std::initializer_list<value_type> il)
1937 //Assertion done in insert()
1938 return this->insert(position, il.begin(), il.end());
1942 //! <b>Effects</b>: Removes the last element from the container.
1944 //! <b>Throws</b>: Nothing.
1946 //! <b>Complexity</b>: Constant time.
1947 void pop_back() BOOST_NOEXCEPT_OR_NOTHROW
1949 BOOST_ASSERT(!this->empty());
1950 //Destroy last element
1951 this->priv_destroy_last();
1954 //! <b>Effects</b>: Erases the element at position pos.
1956 //! <b>Throws</b>: Nothing.
1958 //! <b>Complexity</b>: Linear to the elements between pos and the
1959 //! last element. Constant if pos is the last element.
1960 iterator erase(const_iterator position)
1962 BOOST_ASSERT(this->priv_in_range(position));
1963 const pointer p = vector_iterator_get_ptr(position);
1964 T *const pos_ptr = boost::movelib::to_raw_pointer(p);
1965 T *const beg_ptr = this->priv_raw_begin();
1966 T *const new_end_ptr = ::boost::container::move(pos_ptr + 1, beg_ptr + this->m_holder.m_size, pos_ptr);
1967 //Move elements forward and destroy last
1968 this->priv_destroy_last(pos_ptr == new_end_ptr);
1972 //! <b>Effects</b>: Erases the elements pointed by [first, last).
1974 //! <b>Throws</b>: Nothing.
1976 //! <b>Complexity</b>: Linear to the distance between first and last
1977 //! plus linear to the elements between pos and the last element.
1978 iterator erase(const_iterator first, const_iterator last)
1980 BOOST_ASSERT(first == last ||
1981 (first < last && this->priv_in_range(first) && this->priv_in_range_or_end(last)));
1983 T* const old_end_ptr = this->priv_raw_end();
1984 T* const first_ptr = boost::movelib::to_raw_pointer(vector_iterator_get_ptr(first));
1985 T* const last_ptr = boost::movelib::to_raw_pointer(vector_iterator_get_ptr(last));
1986 T* const ptr = boost::movelib::to_raw_pointer(boost::container::move(last_ptr, old_end_ptr, first_ptr));
1987 this->priv_destroy_last_n(old_end_ptr - ptr);
1989 return iterator(vector_iterator_get_ptr(first));
1992 //! <b>Effects</b>: Swaps the contents of *this and x.
1994 //! <b>Throws</b>: Nothing.
1996 //! <b>Complexity</b>: Constant.
1997 void swap(vector& x)
1998 BOOST_NOEXCEPT_IF( ((allocator_traits_type::propagate_on_container_swap::value
1999 || allocator_traits_type::is_always_equal::value) &&
2000 !container_detail::is_version<Allocator, 0>::value))
2002 this->priv_swap(x, container_detail::bool_<container_detail::is_version<Allocator, 0>::value>());
2005 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2007 //! <b>Effects</b>: Swaps the contents of *this and x.
2009 //! <b>Throws</b>: Nothing.
2011 //! <b>Complexity</b>: Linear
2013 //! <b>Note</b>: Non-standard extension to support static_vector
2014 template<class OtherAllocator>
2015 void swap(vector<T, OtherAllocator> & x
2016 , typename container_detail::enable_if_and
2018 , container_detail::is_version<OtherAllocator, 0>
2019 , container_detail::is_different<OtherAllocator, allocator_type>
2022 { this->m_holder.deep_swap(x.m_holder); }
2024 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2026 //! <b>Effects</b>: Erases all the elements of the vector.
2028 //! <b>Throws</b>: Nothing.
2030 //! <b>Complexity</b>: Linear to the number of elements in the container.
2031 void clear() BOOST_NOEXCEPT_OR_NOTHROW
2032 { this->priv_destroy_all(); }
2034 //! <b>Effects</b>: Returns true if x and y are equal
2036 //! <b>Complexity</b>: Linear to the number of elements in the container.
2037 friend bool operator==(const vector& x, const vector& y)
2038 { return x.size() == y.size() && ::boost::container::algo_equal(x.begin(), x.end(), y.begin()); }
2040 //! <b>Effects</b>: Returns true if x and y are unequal
2042 //! <b>Complexity</b>: Linear to the number of elements in the container.
2043 friend bool operator!=(const vector& x, const vector& y)
2044 { return !(x == y); }
2046 //! <b>Effects</b>: Returns true if x is less than y
2048 //! <b>Complexity</b>: Linear to the number of elements in the container.
2049 friend bool operator<(const vector& x, const vector& y)
2051 const_iterator first1(x.cbegin()), first2(y.cbegin());
2052 const const_iterator last1(x.cend()), last2(y.cend());
2053 for ( ; (first1 != last1) && (first2 != last2); ++first1, ++first2 ) {
2054 if (*first1 < *first2) return true;
2055 if (*first2 < *first1) return false;
2057 return (first1 == last1) && (first2 != last2);
2060 //! <b>Effects</b>: Returns true if x is greater than y
2062 //! <b>Complexity</b>: Linear to the number of elements in the container.
2063 friend bool operator>(const vector& x, const vector& y)
2066 //! <b>Effects</b>: Returns true if x is equal or less than y
2068 //! <b>Complexity</b>: Linear to the number of elements in the container.
2069 friend bool operator<=(const vector& x, const vector& y)
2070 { return !(y < x); }
2072 //! <b>Effects</b>: Returns true if x is equal or greater than y
2074 //! <b>Complexity</b>: Linear to the number of elements in the container.
2075 friend bool operator>=(const vector& x, const vector& y)
2076 { return !(x < y); }
2078 //! <b>Effects</b>: x.swap(y)
2080 //! <b>Complexity</b>: Constant.
2081 friend void swap(vector& x, vector& y)
2084 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2085 //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
2086 //! effect. Otherwise, it is a request for allocation of additional memory
2087 //! (memory expansion) that will not invalidate iterators.
2088 //! If the request is successful, then capacity() is greater than or equal to
2089 //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
2091 //! <b>Throws</b>: If memory allocation allocation throws or T's copy/move constructor throws.
2093 //! <b>Note</b>: Non-standard extension.
2094 bool stable_reserve(size_type new_cap)
2096 const size_type cp = this->capacity();
2097 return cp >= new_cap || (alloc_version::value == 2 && this->m_holder.try_expand_fwd(new_cap - cp));
2100 //Absolutely experimental. This function might change, disappear or simply crash!
2101 template<class BiDirPosConstIt, class BiDirValueIt>
2102 void insert_ordered_at(const size_type element_count, BiDirPosConstIt last_position_it, BiDirValueIt last_value_it)
2104 typedef container_detail::vector_insert_ordered_cursor<BiDirPosConstIt, BiDirValueIt> inserter_t;
2105 return this->priv_insert_ordered_at(element_count, inserter_t(last_position_it, last_value_it));
2108 template<class BidirIt>
2109 void merge(BidirIt first, BidirIt last)
2110 { this->merge(first, last, value_less()); }
2112 template<class BidirIt, class Compare>
2113 void merge(BidirIt first, BidirIt last, Compare comp)
2114 { this->priv_merge(container_detail::false_type(), first, last, comp); }
2116 template<class BidirIt>
2117 void merge_unique(BidirIt first, BidirIt last)
2118 { this->priv_merge(container_detail::true_type(), first, last, value_less()); }
2120 template<class BidirIt, class Compare>
2121 void merge_unique(BidirIt first, BidirIt last, Compare comp)
2122 { this->priv_merge(container_detail::true_type(), first, last, comp); }
2125 template<class PositionValue>
2126 void priv_insert_ordered_at(const size_type element_count, PositionValue position_value)
2128 const size_type old_size_pos = this->size();
2129 this->reserve(old_size_pos + element_count);
2130 T* const begin_ptr = this->priv_raw_begin();
2131 size_type insertions_left = element_count;
2132 size_type prev_pos = old_size_pos;
2133 size_type old_hole_size = element_count;
2135 //Exception rollback. If any copy throws before the hole is filled, values
2136 //already inserted/copied at the end of the buffer will be destroyed.
2137 typename value_traits::ArrayDestructor past_hole_values_destroyer
2138 (begin_ptr + old_size_pos + element_count, this->m_holder.alloc(), size_type(0u));
2139 //Loop for each insertion backwards, first moving the elements after the insertion point,
2140 //then inserting the element.
2141 while(insertions_left){
2143 size_type const pos = position_value.get_pos();
2144 BOOST_ASSERT(pos != size_type(-1) && pos <= old_size_pos && pos <= prev_pos);
2145 //If needed shift the range after the insertion point and the previous insertion point.
2146 //Function will take care if the shift crosses the size() boundary, using copy/move
2147 //or uninitialized copy/move if necessary.
2148 size_type new_hole_size = (pos != prev_pos)
2149 ? priv_insert_ordered_at_shift_range(pos, prev_pos, this->size(), insertions_left)
2153 //The hole was reduced by priv_insert_ordered_at_shift_range so expand exception rollback range backwards
2154 past_hole_values_destroyer.increment_size_backwards(prev_pos - pos);
2155 //Insert the new value in the hole
2156 allocator_traits_type::construct(this->m_holder.alloc(), begin_ptr + pos + insertions_left - 1, position_value.get_val());
2157 if(--new_hole_size){
2158 //The hole was reduced by the new insertion by one
2159 past_hole_values_destroyer.increment_size_backwards(size_type(1u));
2162 //Hole was just filled, disable exception rollback and change vector size
2163 past_hole_values_destroyer.release();
2164 this->m_holder.m_size += element_count;
2169 //Hole was just filled by priv_insert_ordered_at_shift_range, disable exception rollback and change vector size
2170 past_hole_values_destroyer.release();
2171 this->m_holder.m_size += element_count;
2173 //Insert the new value in the already constructed range
2174 begin_ptr[pos + insertions_left - 1] = position_value.get_val();
2177 old_hole_size = new_hole_size;
2182 template<class UniqueBool, class BidirIt, class Compare>
2183 void priv_merge(UniqueBool, BidirIt first, BidirIt last, Compare comp)
2185 size_type const n = static_cast<size_type>(boost::container::iterator_distance(first, last));
2186 size_type const s = this->size();
2187 if(BOOST_LIKELY(s)){
2188 size_type const c = this->capacity();
2189 size_type const free_c = (c - s);
2190 //Use a new buffer if current one is too small for new elements
2192 this->priv_merge_in_new_buffer(UniqueBool(), first, n, comp, alloc_version());
2195 T *raw_pos = boost::movelib::iterator_to_raw_pointer(this->insert(this->cend(), first, last));
2196 T *raw_beg = this->priv_raw_begin();
2197 T *raw_end = this->priv_raw_end();
2198 boost::movelib::adaptive_merge(raw_beg, raw_pos, raw_end, comp, raw_end, free_c - n);
2199 if(UniqueBool::value){
2200 size_type const count =
2201 static_cast<size_type>(raw_end - boost::movelib::unique(raw_beg, raw_end, boost::movelib::negate<Compare>(comp)));
2202 this->priv_destroy_last_n(count);
2207 this->insert(this->cend(), n, first, last);
2211 template<class UniqueBool, class FwdIt, class Compare>
2212 void priv_merge_in_new_buffer(UniqueBool, FwdIt, size_type, Compare, version_0)
2217 template<class UniqueBool, class FwdIt, class Compare, class Version>
2218 void priv_merge_in_new_buffer(UniqueBool, FwdIt first, size_type n, Compare comp, Version)
2220 size_type const new_size = this->size() + n;
2221 size_type new_cap = new_size;
2222 pointer p = pointer();
2223 pointer const new_storage = this->m_holder.allocation_command(allocate_new, new_size, new_cap, p);
2225 BOOST_ASSERT((new_cap >= this->size() ) && (new_cap - this->size()) >= n);
2226 allocator_type &a = this->m_holder.alloc();
2227 typename value_traits::ArrayDeallocator new_buffer_deallocator(new_storage, a, new_cap);
2228 typename value_traits::ArrayDestructor new_values_destroyer(new_storage, a, 0u);
2229 T* pbeg = this->priv_raw_begin();
2230 size_type const old_size = this->size();
2231 T* const pend = pbeg + old_size;
2232 T* d_first = boost::movelib::to_raw_pointer(new_storage);
2233 size_type added = n;
2234 //Merge in new buffer loop
2237 ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), pbeg, pend, d_first);
2240 else if(pbeg == pend) {
2241 ::boost::container::uninitialized_move_alloc_n(this->m_holder.alloc(), first, n, d_first);
2244 //maintain stability moving external values only if they are strictly less
2245 else if(comp(*first, *pbeg)) {
2246 allocator_traits_type::construct( this->m_holder.alloc(), d_first, *first );
2247 new_values_destroyer.increment_size(1u);
2252 else if(UniqueBool::value && !comp(*pbeg, *first)){
2258 allocator_traits_type::construct( this->m_holder.alloc(), d_first, boost::move(*pbeg) );
2259 new_values_destroyer.increment_size(1u);
2265 //Nothrow operations
2266 pointer const old_p = this->m_holder.start();
2267 size_type const old_cap = this->m_holder.capacity();
2268 boost::container::destroy_alloc_n(a, boost::movelib::to_raw_pointer(old_p), old_size);
2269 allocator_traits_type::deallocate(a, old_p, old_cap);
2270 this->m_holder.m_size = old_size + added;
2271 this->m_holder.start(new_storage);
2272 this->m_holder.capacity(new_cap);
2273 new_buffer_deallocator.release();
2274 new_values_destroyer.release();
2277 bool room_enough() const
2278 { return this->m_holder.m_size < this->m_holder.capacity(); }
2280 pointer back_ptr() const
2281 { return this->m_holder.start() + this->m_holder.m_size; }
2283 size_type priv_index_of(pointer p) const
2285 BOOST_ASSERT(this->m_holder.start() <= p);
2286 BOOST_ASSERT(p <= (this->m_holder.start()+this->size()));
2287 return static_cast<size_type>(p - this->m_holder.start());
2290 template<class OtherAllocator>
2291 void priv_move_assign(BOOST_RV_REF_BEG vector<T, OtherAllocator> BOOST_RV_REF_END x
2292 , typename container_detail::enable_if_c
2293 < container_detail::is_version<OtherAllocator, 0>::value >::type * = 0)
2295 if(!container_detail::is_same<OtherAllocator, allocator_type>::value &&
2296 this->capacity() < x.size()){
2299 T* const this_start = this->priv_raw_begin();
2300 T* const other_start = x.priv_raw_begin();
2301 const size_type this_sz = m_holder.m_size;
2302 const size_type other_sz = static_cast<size_type>(x.m_holder.m_size);
2303 boost::container::move_assign_range_alloc_n(this->m_holder.alloc(), other_start, other_sz, this_start, this_sz);
2304 this->m_holder.m_size = other_sz;
2307 template<class OtherAllocator>
2308 void priv_move_assign(BOOST_RV_REF_BEG vector<T, OtherAllocator> BOOST_RV_REF_END x
2309 , typename container_detail::disable_if_or
2311 , container_detail::is_version<OtherAllocator, 0>
2312 , container_detail::is_different<OtherAllocator, allocator_type>
2315 //for move assignment, no aliasing (&x != this) is assummed.
2316 BOOST_ASSERT(this != &x);
2317 allocator_type &this_alloc = this->m_holder.alloc();
2318 allocator_type &x_alloc = x.m_holder.alloc();
2319 const bool propagate_alloc = allocator_traits_type::propagate_on_container_move_assignment::value;
2321 const bool is_propagable_from_x = is_propagable_from(x_alloc, x.m_holder.start(), this_alloc, propagate_alloc);
2322 const bool is_propagable_from_t = is_propagable_from(this_alloc, m_holder.start(), x_alloc, propagate_alloc);
2323 const bool are_both_propagable = is_propagable_from_x && is_propagable_from_t;
2325 //Resources can be transferred if both allocators are
2326 //going to be equal after this function (either propagated or already equal)
2327 if(are_both_propagable){
2328 //Destroy objects but retain memory in case x reuses it in the future
2330 this->m_holder.swap_resources(x.m_holder);
2332 else if(is_propagable_from_x){
2334 allocator_traits_type::deallocate(this->m_holder.alloc(), this->m_holder.m_start, this->m_holder.m_capacity);
2335 this->m_holder.steal_resources(x.m_holder);
2337 //Else do a one by one move
2339 this->assign( boost::make_move_iterator(boost::movelib::iterator_to_raw_pointer(x.begin()))
2340 , boost::make_move_iterator(boost::movelib::iterator_to_raw_pointer(x.end() ))
2343 //Move allocator if needed
2344 container_detail::move_alloc(this_alloc, x_alloc, container_detail::bool_<propagate_alloc>());
2347 template<class OtherAllocator>
2348 void priv_copy_assign(const vector<T, OtherAllocator> &x
2349 , typename container_detail::enable_if_c
2350 < container_detail::is_version<OtherAllocator, 0>::value >::type * = 0)
2352 if(!container_detail::is_same<OtherAllocator, allocator_type>::value &&
2353 this->capacity() < x.size()){
2356 T* const this_start = this->priv_raw_begin();
2357 T* const other_start = x.priv_raw_begin();
2358 const size_type this_sz = m_holder.m_size;
2359 const size_type other_sz = static_cast<size_type>(x.m_holder.m_size);
2360 boost::container::copy_assign_range_alloc_n(this->m_holder.alloc(), other_start, other_sz, this_start, this_sz);
2361 this->m_holder.m_size = other_sz;
2364 template<class OtherAllocator>
2365 typename container_detail::disable_if_or
2367 , container_detail::is_version<OtherAllocator, 0>
2368 , container_detail::is_different<OtherAllocator, allocator_type>
2370 priv_copy_assign(const vector<T, OtherAllocator> &x)
2372 allocator_type &this_alloc = this->m_holder.alloc();
2373 const allocator_type &x_alloc = x.m_holder.alloc();
2374 container_detail::bool_<allocator_traits_type::
2375 propagate_on_container_copy_assignment::value> flag;
2376 if(flag && this_alloc != x_alloc){
2378 this->shrink_to_fit();
2380 container_detail::assign_alloc(this_alloc, x_alloc, flag);
2381 this->assign( x.priv_raw_begin(), x.priv_raw_end() );
2384 template<class Vector> //Template it to avoid it in explicit instantiations
2385 void priv_swap(Vector &x, container_detail::true_type) //version_0
2386 { this->m_holder.deep_swap(x.m_holder); }
2388 template<class Vector> //Template it to avoid it in explicit instantiations
2389 void priv_swap(Vector &x, container_detail::false_type) //version_N
2391 const bool propagate_alloc = allocator_traits_type::propagate_on_container_swap::value;
2392 if(are_swap_propagable( this->get_stored_allocator(), this->m_holder.start()
2393 , x.get_stored_allocator(), x.m_holder.start(), propagate_alloc)){
2394 //Just swap internals
2395 this->m_holder.swap_resources(x.m_holder);
2398 //Else swap element by element...
2399 bool const t_smaller = this->size() < x.size();
2400 vector &sml = t_smaller ? *this : x;
2401 vector &big = t_smaller ? x : *this;
2403 size_type const common_elements = sml.size();
2404 for(size_type i = 0; i != common_elements; ++i){
2405 boost::adl_move_swap(sml[i], big[i]);
2407 //... and move-insert the remaining range
2408 sml.insert( sml.cend()
2409 , boost::make_move_iterator(boost::movelib::iterator_to_raw_pointer(big.nth(common_elements)))
2410 , boost::make_move_iterator(boost::movelib::iterator_to_raw_pointer(big.end()))
2412 //Destroy remaining elements
2413 big.erase(big.nth(common_elements), big.cend());
2415 //And now swap the allocator
2416 container_detail::swap_alloc(this->m_holder.alloc(), x.m_holder.alloc(), container_detail::bool_<propagate_alloc>());
2419 void priv_reserve_no_capacity(size_type, version_0)
2420 { throw_bad_alloc(); }
2422 container_detail::insert_range_proxy<Allocator, boost::move_iterator<T*>, T*> priv_dummy_empty_proxy()
2424 return container_detail::insert_range_proxy<Allocator, boost::move_iterator<T*>, T*>
2425 (::boost::make_move_iterator((T *)0));
2428 void priv_reserve_no_capacity(size_type new_cap, version_1)
2430 //There is not enough memory, allocate a new buffer
2431 //Pass the hint so that allocators can take advantage of this.
2432 pointer const p = allocator_traits_type::allocate(this->m_holder.alloc(), new_cap, this->m_holder.m_start);
2433 //We will reuse insert code, so create a dummy input iterator
2434 this->priv_forward_range_insert_new_allocation
2435 ( boost::movelib::to_raw_pointer(p), new_cap, this->priv_raw_end(), 0, this->priv_dummy_empty_proxy());
2438 void priv_reserve_no_capacity(size_type new_cap, version_2)
2440 //There is not enough memory, allocate a new
2441 //buffer or expand the old one.
2442 bool same_buffer_start;
2443 size_type real_cap = 0;
2444 pointer reuse(this->m_holder.start());
2445 pointer const ret(this->m_holder.allocation_command(allocate_new | expand_fwd | expand_bwd, new_cap, real_cap = new_cap, reuse));
2447 //Check for forward expansion
2448 same_buffer_start = reuse && this->m_holder.start() == ret;
2449 if(same_buffer_start){
2450 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2451 ++this->num_expand_fwd;
2453 this->m_holder.capacity(real_cap);
2455 else{ //If there is no forward expansion, move objects, we will reuse insertion code
2456 T * const new_mem = boost::movelib::to_raw_pointer(ret);
2457 T * const ins_pos = this->priv_raw_end();
2458 if(reuse){ //Backwards (and possibly forward) expansion
2459 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2460 ++this->num_expand_bwd;
2462 this->priv_forward_range_insert_expand_backwards
2463 ( new_mem , real_cap, ins_pos, 0, this->priv_dummy_empty_proxy());
2466 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2469 this->priv_forward_range_insert_new_allocation
2470 ( new_mem, real_cap, ins_pos, 0, this->priv_dummy_empty_proxy());
2475 void priv_destroy_last(const bool moved = false) BOOST_NOEXCEPT_OR_NOTHROW
2478 if(!(value_traits::trivial_dctr || (value_traits::trivial_dctr_after_move && moved))){
2479 value_type* const p = this->priv_raw_end() - 1;
2480 allocator_traits_type::destroy(this->get_stored_allocator(), p);
2482 --this->m_holder.m_size;
2485 void priv_destroy_last_n(const size_type n) BOOST_NOEXCEPT_OR_NOTHROW
2487 BOOST_ASSERT(n <= this->m_holder.m_size);
2488 if(!value_traits::trivial_dctr){
2489 T* const destroy_pos = this->priv_raw_begin() + (this->m_holder.m_size-n);
2490 boost::container::destroy_alloc_n(this->get_stored_allocator(), destroy_pos, n);
2492 this->m_holder.m_size -= n;
2495 template<class InpIt>
2496 void priv_uninitialized_construct_at_end(InpIt first, InpIt last)
2498 T* const old_end_pos = this->priv_raw_end();
2499 T* const new_end_pos = boost::container::uninitialized_copy_alloc(this->m_holder.alloc(), first, last, old_end_pos);
2500 this->m_holder.m_size += new_end_pos - old_end_pos;
2503 void priv_destroy_all() BOOST_NOEXCEPT_OR_NOTHROW
2505 boost::container::destroy_alloc_n
2506 (this->get_stored_allocator(), this->priv_raw_begin(), this->m_holder.m_size);
2507 this->m_holder.m_size = 0;
2511 iterator priv_insert(const const_iterator &p, BOOST_FWD_REF(U) x)
2513 BOOST_ASSERT(this->priv_in_range_or_end(p));
2514 return this->priv_forward_range_insert
2515 ( vector_iterator_get_ptr(p), 1, container_detail::get_insert_value_proxy<T*, Allocator>(::boost::forward<U>(x)));
2518 container_detail::insert_copy_proxy<Allocator, T*> priv_single_insert_proxy(const T &x)
2519 { return container_detail::insert_copy_proxy<Allocator, T*> (x); }
2521 container_detail::insert_move_proxy<Allocator, T*> priv_single_insert_proxy(BOOST_RV_REF(T) x)
2522 { return container_detail::insert_move_proxy<Allocator, T*> (x); }
2525 void priv_push_back(BOOST_FWD_REF(U) u)
2527 if (BOOST_LIKELY(this->room_enough())){
2528 //There is more memory, just construct a new object at the end
2529 allocator_traits_type::construct
2530 ( this->m_holder.alloc(), this->priv_raw_end(), ::boost::forward<U>(u) );
2531 ++this->m_holder.m_size;
2534 this->priv_forward_range_insert_no_capacity
2535 ( this->back_ptr(), 1
2536 , this->priv_single_insert_proxy(::boost::forward<U>(u)), alloc_version());
2540 container_detail::insert_n_copies_proxy<Allocator, T*> priv_resize_proxy(const T &x)
2541 { return container_detail::insert_n_copies_proxy<Allocator, T*>(x); }
2543 container_detail::insert_default_initialized_n_proxy<Allocator, T*> priv_resize_proxy(default_init_t)
2544 { return container_detail::insert_default_initialized_n_proxy<Allocator, T*>(); }
2546 container_detail::insert_value_initialized_n_proxy<Allocator, T*> priv_resize_proxy(value_init_t)
2547 { return container_detail::insert_value_initialized_n_proxy<Allocator, T*>(); }
2550 void priv_resize(size_type new_size, const U& u)
2552 const size_type sz = this->size();
2554 //Destroy last elements
2555 this->priv_destroy_last_n(sz - new_size);
2558 const size_type n = new_size - this->size();
2559 this->priv_forward_range_insert_at_end(n, this->priv_resize_proxy(u), alloc_version());
2563 void priv_shrink_to_fit(version_0) BOOST_NOEXCEPT_OR_NOTHROW
2566 void priv_shrink_to_fit(version_1)
2568 const size_type cp = this->m_holder.capacity();
2570 const size_type sz = this->size();
2572 allocator_traits_type::deallocate(this->m_holder.alloc(), this->m_holder.m_start, cp);
2573 this->m_holder.m_start = pointer();
2574 this->m_holder.m_capacity = 0;
2577 //Allocate a new buffer.
2578 //Pass the hint so that allocators can take advantage of this.
2579 pointer const p = allocator_traits_type::allocate(this->m_holder.alloc(), sz, this->m_holder.m_start);
2581 //We will reuse insert code, so create a dummy input iterator
2582 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2585 this->priv_forward_range_insert_new_allocation
2586 ( boost::movelib::to_raw_pointer(p), sz
2587 , this->priv_raw_begin(), 0, this->priv_dummy_empty_proxy());
2592 void priv_shrink_to_fit(version_2) BOOST_NOEXCEPT_OR_NOTHROW
2594 const size_type cp = this->m_holder.capacity();
2596 const size_type sz = this->size();
2598 allocator_traits_type::deallocate(this->m_holder.alloc(), this->m_holder.m_start, cp);
2599 this->m_holder.m_start = pointer();
2600 this->m_holder.m_capacity = 0;
2603 size_type received_size = sz;
2604 pointer reuse(this->m_holder.start());
2605 if(this->m_holder.allocation_command
2606 (shrink_in_place | nothrow_allocation, cp, received_size, reuse)){
2607 this->m_holder.capacity(received_size);
2608 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2616 template <class InsertionProxy>
2617 iterator priv_forward_range_insert_no_capacity
2618 (const pointer &pos, const size_type, const InsertionProxy , version_0)
2621 return iterator(pos);
2624 template <class InsertionProxy>
2625 iterator priv_forward_range_insert_no_capacity
2626 (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, version_1)
2628 //Check if we have enough memory or try to expand current memory
2629 const size_type n_pos = pos - this->m_holder.start();
2630 T *const raw_pos = boost::movelib::to_raw_pointer(pos);
2632 const size_type new_cap = this->m_holder.next_capacity(n);
2633 //Pass the hint so that allocators can take advantage of this.
2634 T * const new_buf = boost::movelib::to_raw_pointer
2635 (allocator_traits_type::allocate(this->m_holder.alloc(), new_cap, this->m_holder.m_start));
2636 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2639 this->priv_forward_range_insert_new_allocation
2640 ( new_buf, new_cap, raw_pos, n, insert_range_proxy);
2641 return iterator(this->m_holder.start() + n_pos);
2644 template <class InsertionProxy>
2645 iterator priv_forward_range_insert_no_capacity
2646 (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, version_2)
2648 //Check if we have enough memory or try to expand current memory
2649 T *const raw_pos = boost::movelib::to_raw_pointer(pos);
2650 const size_type n_pos = raw_pos - this->priv_raw_begin();
2652 //There is not enough memory, allocate a new
2653 //buffer or expand the old one.
2654 size_type real_cap = this->m_holder.next_capacity(n);
2655 pointer reuse(this->m_holder.start());
2656 pointer const ret (this->m_holder.allocation_command
2657 (allocate_new | expand_fwd | expand_bwd, this->m_holder.m_size + n, real_cap, reuse));
2659 //Buffer reallocated
2661 //Forward expansion, delay insertion
2662 if(this->m_holder.start() == ret){
2663 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2664 ++this->num_expand_fwd;
2666 this->m_holder.capacity(real_cap);
2668 this->priv_forward_range_insert_expand_forward(raw_pos, n, insert_range_proxy);
2670 //Backwards (and possibly forward) expansion
2672 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2673 ++this->num_expand_bwd;
2675 this->priv_forward_range_insert_expand_backwards
2676 (boost::movelib::to_raw_pointer(ret), real_cap, raw_pos, n, insert_range_proxy);
2681 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2684 this->priv_forward_range_insert_new_allocation
2685 ( boost::movelib::to_raw_pointer(ret), real_cap, raw_pos, n, insert_range_proxy);
2688 return iterator(this->m_holder.start() + n_pos);
2691 template <class InsertionProxy>
2692 iterator priv_forward_range_insert
2693 (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy)
2695 BOOST_ASSERT(this->m_holder.capacity() >= this->m_holder.m_size);
2696 //Check if we have enough memory or try to expand current memory
2697 const size_type remaining = this->m_holder.capacity() - this->m_holder.m_size;
2699 bool same_buffer_start = n <= remaining;
2700 if (!same_buffer_start){
2701 return priv_forward_range_insert_no_capacity(pos, n, insert_range_proxy, alloc_version());
2705 T *const raw_pos = boost::movelib::to_raw_pointer(pos);
2706 const size_type n_pos = raw_pos - this->priv_raw_begin();
2707 this->priv_forward_range_insert_expand_forward(raw_pos, n, insert_range_proxy);
2708 return iterator(this->m_holder.start() + n_pos);
2712 template <class InsertionProxy>
2713 iterator priv_forward_range_insert_at_end
2714 (const size_type n, const InsertionProxy insert_range_proxy, version_0)
2716 //Check if we have enough memory or try to expand current memory
2717 const size_type remaining = this->m_holder.capacity() - this->m_holder.m_size;
2720 //This will trigger an error
2723 this->priv_forward_range_insert_at_end_expand_forward(n, insert_range_proxy);
2727 template <class InsertionProxy, class AllocVersion>
2728 iterator priv_forward_range_insert_at_end
2729 (const size_type n, const InsertionProxy insert_range_proxy, AllocVersion)
2731 return this->priv_forward_range_insert(this->back_ptr(), n, insert_range_proxy);
2734 //Takes the range pointed by [first_pos, last_pos) and shifts it to the right
2735 //by 'shift_count'. 'limit_pos' marks the end of constructed elements.
2737 //Precondition: first_pos <= last_pos <= limit_pos
2739 //The shift operation might cross limit_pos so elements to moved beyond limit_pos
2740 //are uninitialized_moved with an allocator. Other elements are moved.
2742 //The shift operation might left uninitialized elements after limit_pos
2743 //and the number of uninitialized elements is returned by the function.
2746 // first_pos last_pos old_limit
2748 // ____________V_______V__________________V_____________
2749 //| prefix | range | suffix |raw_mem ~
2750 //|____________|_______|__________________|_____________~
2752 //New situation in Case A (hole_size == 0):
2753 // range is moved through move assignments
2755 // first_pos last_pos limit_pos
2757 // ____________V_______V__________________V_____________
2758 //| prefix' | | | range |suffix'|raw_mem ~
2759 //|________________+______|___^___|_______|_____________~
2764 //New situation in Case B (hole_size >= 0):
2765 // range is moved through uninitialized moves
2767 // first_pos last_pos limit_pos
2769 // ____________V_______V__________________V________________
2770 //| prefix' | | | [hole] | range |
2771 //|_______________________________________|________|___^___|
2773 // |_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_^
2775 //New situation in Case C (hole_size == 0):
2776 // range is moved through move assignments and uninitialized moves
2778 // first_pos last_pos limit_pos
2780 // ____________V_______V__________________V___
2781 //| prefix' | | | range |
2782 //|___________________________________|___^___|
2784 // |_>_>_>_>_>_>_>_>_>_>_>^
2785 size_type priv_insert_ordered_at_shift_range
2786 (size_type first_pos, size_type last_pos, size_type limit_pos, size_type shift_count)
2788 BOOST_ASSERT(first_pos <= last_pos);
2789 BOOST_ASSERT(last_pos <= limit_pos);
2791 T* const begin_ptr = this->priv_raw_begin();
2792 T* const first_ptr = begin_ptr + first_pos;
2793 T* const last_ptr = begin_ptr + last_pos;
2795 size_type hole_size = 0;
2797 if((last_pos + shift_count) <= limit_pos){
2799 boost::container::move_backward(first_ptr, last_ptr, last_ptr + shift_count);
2802 else if((first_pos + shift_count) >= limit_pos){
2803 //All uninitialized_moved
2804 ::boost::container::uninitialized_move_alloc
2805 (this->m_holder.alloc(), first_ptr, last_ptr, first_ptr + shift_count);
2806 hole_size = first_pos + shift_count - limit_pos;
2810 //Some uninitialized_moved
2811 T* const limit_ptr = begin_ptr + limit_pos;
2812 T* const boundary_ptr = limit_ptr - shift_count;
2813 ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), boundary_ptr, last_ptr, limit_ptr);
2814 //The rest is move assigned
2815 boost::container::move_backward(first_ptr, boundary_ptr, limit_ptr);
2821 BOOST_CONTAINER_FORCEINLINE T *priv_raw_begin() const
2822 { return boost::movelib::to_raw_pointer(m_holder.start()); }
2824 BOOST_CONTAINER_FORCEINLINE T* priv_raw_end() const
2825 { return this->priv_raw_begin() + this->m_holder.m_size; }
2827 template <class InsertionProxy>
2828 void priv_forward_range_insert_at_end_expand_forward(const size_type n, InsertionProxy insert_range_proxy)
2830 T* const old_finish = this->priv_raw_end();
2831 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n);
2832 this->m_holder.m_size += n;
2835 template <class InsertionProxy>
2836 void priv_forward_range_insert_expand_forward(T* const pos, const size_type n, InsertionProxy insert_range_proxy)
2838 //n can't be 0, because there is nothing to do in that case
2839 if(BOOST_UNLIKELY(!n)) return;
2840 //There is enough memory
2841 T* const old_finish = this->priv_raw_end();
2842 const size_type elems_after = old_finish - pos;
2845 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n);
2846 this->m_holder.m_size += n;
2848 else if (elems_after >= n){
2849 //New elements can be just copied.
2850 //Move to uninitialized memory last objects
2851 ::boost::container::uninitialized_move_alloc
2852 (this->m_holder.alloc(), old_finish - n, old_finish, old_finish);
2853 this->m_holder.m_size += n;
2854 //Copy previous to last objects to the initialized end
2855 boost::container::move_backward(pos, old_finish - n, old_finish);
2856 //Insert new objects in the pos
2857 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, n);
2860 //The new elements don't fit in the [pos, end()) range.
2862 //Copy old [pos, end()) elements to the uninitialized memory (a gap is created)
2863 ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), pos, old_finish, pos + n);
2865 //Copy first new elements in pos (gap is still there)
2866 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, elems_after);
2867 //Copy to the beginning of the unallocated zone the last new elements (the gap is closed).
2868 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n - elems_after);
2869 this->m_holder.m_size += n;
2872 boost::container::destroy_alloc_n(this->get_stored_allocator(), pos + n, elems_after);
2879 template <class InsertionProxy>
2880 void priv_forward_range_insert_new_allocation
2881 (T* const new_start, size_type new_cap, T* const pos, const size_type n, InsertionProxy insert_range_proxy)
2883 //n can be zero, if we want to reallocate!
2884 T *new_finish = new_start;
2886 //Anti-exception rollbacks
2887 typename value_traits::ArrayDeallocator new_buffer_deallocator(new_start, this->m_holder.alloc(), new_cap);
2888 typename value_traits::ArrayDestructor new_values_destroyer(new_start, this->m_holder.alloc(), 0u);
2890 //Initialize with [begin(), pos) old buffer
2891 //the start of the new buffer
2892 T * const old_buffer = this->priv_raw_begin();
2894 new_finish = ::boost::container::uninitialized_move_alloc
2895 (this->m_holder.alloc(), this->priv_raw_begin(), pos, old_finish = new_finish);
2896 new_values_destroyer.increment_size(new_finish - old_finish);
2898 //Initialize new objects, starting from previous point
2899 old_finish = new_finish;
2900 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n);
2902 new_values_destroyer.increment_size(new_finish - old_finish);
2903 //Initialize from the rest of the old buffer,
2904 //starting from previous point
2906 new_finish = ::boost::container::uninitialized_move_alloc
2907 (this->m_holder.alloc(), pos, old_buffer + this->m_holder.m_size, new_finish);
2908 //Destroy and deallocate old elements
2909 //If there is allocated memory, destroy and deallocate
2910 if(!value_traits::trivial_dctr_after_move)
2911 boost::container::destroy_alloc_n(this->get_stored_allocator(), old_buffer, this->m_holder.m_size);
2912 allocator_traits_type::deallocate(this->m_holder.alloc(), this->m_holder.start(), this->m_holder.capacity());
2914 this->m_holder.start(new_start);
2915 this->m_holder.m_size = new_finish - new_start;
2916 this->m_holder.capacity(new_cap);
2917 //All construction successful, disable rollbacks
2918 new_values_destroyer.release();
2919 new_buffer_deallocator.release();
2922 template <class InsertionProxy>
2923 void priv_forward_range_insert_expand_backwards
2924 (T* const new_start, const size_type new_capacity,
2925 T* const pos, const size_type n, InsertionProxy insert_range_proxy)
2927 //n can be zero to just expand capacity
2929 T* const old_start = this->priv_raw_begin();
2930 const size_type old_size = this->m_holder.m_size;
2931 T* const old_finish = old_start + old_size;
2933 //We can have 8 possibilities:
2934 const size_type elemsbefore = static_cast<size_type>(pos - old_start);
2935 const size_type s_before = static_cast<size_type>(old_start - new_start);
2936 const size_type before_plus_new = elemsbefore + n;
2938 //Update the vector buffer information to a safe state
2939 this->m_holder.start(new_start);
2940 this->m_holder.capacity(new_capacity);
2941 this->m_holder.m_size = 0;
2943 //If anything goes wrong, this object will destroy
2944 //all the old objects to fulfill previous vector state
2945 typename value_traits::ArrayDestructor old_values_destroyer(old_start, this->m_holder.alloc(), old_size);
2946 //Check if s_before is big enough to hold the beginning of old data + new data
2947 if(s_before >= before_plus_new){
2948 //Copy first old values before pos, after that the new objects
2949 T *const new_elem_pos =
2950 ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), old_start, pos, new_start);
2951 this->m_holder.m_size = elemsbefore;
2952 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), new_elem_pos, n);
2953 this->m_holder.m_size = before_plus_new;
2954 const size_type new_size = old_size + n;
2955 //Check if s_before is so big that even copying the old data + new data
2956 //there is a gap between the new data and the old data
2957 if(s_before >= new_size){
2959 // _________________________________________________________
2960 //| raw_mem | old_begin | old_end |
2961 //| __________________________________|___________|_________|
2964 // _________________________________________________________
2965 //| old_begin | new | old_end | raw_mem |
2966 //|___________|__________|_________|________________________|
2968 //Now initialize the rest of memory with the last old values
2969 if(before_plus_new != new_size){ //Special case to avoid operations in back insertion
2970 ::boost::container::uninitialized_move_alloc
2971 (this->m_holder.alloc(), pos, old_finish, new_start + before_plus_new);
2972 //All new elements correctly constructed, avoid new element destruction
2973 this->m_holder.m_size = new_size;
2975 //Old values destroyed automatically with "old_values_destroyer"
2976 //when "old_values_destroyer" goes out of scope unless the have trivial
2977 //destructor after move.
2978 if(value_traits::trivial_dctr_after_move)
2979 old_values_destroyer.release();
2981 //s_before is so big that divides old_end
2984 // __________________________________________________
2985 //| raw_mem | old_begin | old_end |
2986 //| ___________________________|___________|_________|
2989 // __________________________________________________
2990 //| old_begin | new | old_end | raw_mem |
2991 //|___________|__________|_________|_________________|
2993 //Now initialize the rest of memory with the last old values
2994 //All new elements correctly constructed, avoid new element destruction
2995 const size_type raw_gap = s_before - before_plus_new;
2996 if(!value_traits::trivial_dctr){
2997 //Now initialize the rest of s_before memory with the
2998 //first of elements after new values
2999 ::boost::container::uninitialized_move_alloc_n
3000 (this->m_holder.alloc(), pos, raw_gap, new_start + before_plus_new);
3001 //Now we have a contiguous buffer so program trailing element destruction
3002 //and update size to the final size.
3003 old_values_destroyer.shrink_forward(new_size-s_before);
3004 this->m_holder.m_size = new_size;
3005 //Now move remaining last objects in the old buffer begin
3006 T * const remaining_pos = pos + raw_gap;
3007 if(remaining_pos != old_start){ //Make sure data has to be moved
3008 ::boost::container::move(remaining_pos, old_finish, old_start);
3010 //Once moved, avoid calling the destructors if trivial after move
3011 if(value_traits::trivial_dctr_after_move){
3012 old_values_destroyer.release();
3015 else{ //If trivial destructor, we can uninitialized copy + copy in a single uninitialized copy
3016 ::boost::container::uninitialized_move_alloc_n
3017 (this->m_holder.alloc(), pos, static_cast<size_type>(old_finish - pos), new_start + before_plus_new);
3018 this->m_holder.m_size = new_size;
3019 old_values_destroyer.release();
3024 //Check if we have to do the insertion in two phases
3025 //since maybe s_before is not big enough and
3026 //the buffer was expanded both sides
3029 // _________________________________________________
3030 //| raw_mem | old_begin + old_end | raw_mem |
3031 //|_________|_____________________|_________________|
3033 //New situation with do_after:
3034 // _________________________________________________
3035 //| old_begin + new + old_end | raw_mem |
3036 //|___________________________________|_____________|
3038 //New without do_after:
3039 // _________________________________________________
3040 //| old_begin + new + old_end | raw_mem |
3041 //|____________________________|____________________|
3043 const bool do_after = n > s_before;
3045 //Now we can have two situations: the raw_mem of the
3046 //beginning divides the old_begin, or the new elements:
3047 if (s_before <= elemsbefore) {
3048 //The raw memory divides the old_begin group:
3050 //If we need two phase construction (do_after)
3051 //new group is divided in new = new_beg + new_end groups
3052 //In this phase only new_beg will be inserted
3055 // _________________________________________________
3056 //| raw_mem | old_begin | old_end | raw_mem |
3057 //|_________|___________|_________|_________________|
3059 //New situation with do_after(1):
3060 //This is not definitive situation, the second phase
3062 // _________________________________________________
3063 //| old_begin | new_beg | old_end | raw_mem |
3064 //|___________|_________|_________|_________________|
3066 //New situation without do_after:
3067 // _________________________________________________
3068 //| old_begin | new | old_end | raw_mem |
3069 //|___________|_____|_________|_____________________|
3071 //Copy the first part of old_begin to raw_mem
3072 ::boost::container::uninitialized_move_alloc_n
3073 (this->m_holder.alloc(), old_start, s_before, new_start);
3074 //The buffer is all constructed until old_end,
3075 //so program trailing destruction and assign final size
3076 //if !do_after, s_before+n otherwise.
3077 size_type new_1st_range;
3079 new_1st_range = s_before;
3080 //release destroyer and update size
3081 old_values_destroyer.release();
3085 if(value_traits::trivial_dctr_after_move)
3086 old_values_destroyer.release();
3088 old_values_destroyer.shrink_forward(old_size - (s_before - n));
3091 this->m_holder.m_size = old_size + new_1st_range;
3092 //Now copy the second part of old_begin overwriting itself
3093 T *const next = ::boost::container::move(old_start + s_before, pos, old_start);
3094 //Now copy the new_beg elements
3095 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), next, new_1st_range);
3097 //If there is no after work and the last old part needs to be moved to front, do it
3098 if(!do_after && (n != s_before)){
3099 //Now displace old_end elements
3100 ::boost::container::move(pos, old_finish, next + new_1st_range);
3104 //If we have to expand both sides,
3105 //we will play if the first new values so
3106 //calculate the upper bound of new values
3108 //The raw memory divides the new elements
3110 //If we need two phase construction (do_after)
3111 //new group is divided in new = new_beg + new_end groups
3112 //In this phase only new_beg will be inserted
3115 // _______________________________________________________
3116 //| raw_mem | old_begin | old_end | raw_mem |
3117 //|_______________|___________|_________|_________________|
3119 //New situation with do_after():
3120 // ____________________________________________________
3121 //| old_begin | new_beg | old_end | raw_mem |
3122 //|___________|_______________|_________|______________|
3124 //New situation without do_after:
3125 // ______________________________________________________
3126 //| old_begin | new | old_end | raw_mem |
3127 //|___________|_____|_________|__________________________|
3129 //First copy whole old_begin and part of new to raw_mem
3130 T * const new_pos = ::boost::container::uninitialized_move_alloc
3131 (this->m_holder.alloc(), old_start, pos, new_start);
3132 this->m_holder.m_size = elemsbefore;
3133 const size_type mid_n = s_before - elemsbefore;
3134 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), new_pos, mid_n);
3135 //The buffer is all constructed until old_end,
3137 this->m_holder.m_size = old_size + s_before;
3138 old_values_destroyer.release();
3142 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), old_start, elemsbefore);
3145 //Copy all new elements
3146 const size_type rest_new = n - mid_n;
3147 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), old_start, rest_new);
3148 T* const move_start = old_start + rest_new;
3149 //Displace old_end, but make sure data has to be moved
3150 T* const move_end = move_start != pos ? ::boost::container::move(pos, old_finish, move_start)
3152 //Destroy remaining moved elements from old_end except if they
3153 //have trivial destructor after being moved
3154 size_type n_destroy = s_before - n;
3155 if(!value_traits::trivial_dctr_after_move)
3156 boost::container::destroy_alloc_n(this->get_stored_allocator(), move_end, n_destroy);
3157 this->m_holder.m_size -= n_destroy;
3161 //This is only executed if two phase construction is needed
3163 //The raw memory divides the new elements
3166 // ______________________________________________________
3167 //| raw_mem | old_begin | old_end | raw_mem |
3168 //|______________|___________|____________|______________|
3170 //New situation with do_after(1):
3171 // _______________________________________________________
3172 //| old_begin + new_beg | new_end |old_end | raw_mem |
3173 //|__________________________|_________|________|_________|
3175 //New situation with do_after(2):
3176 // ______________________________________________________
3177 //| old_begin + new | old_end |raw |
3178 //|_______________________________________|_________|____|
3180 const size_type n_after = n - s_before;
3181 const size_type elemsafter = old_size - elemsbefore;
3183 //We can have two situations:
3184 if (elemsafter >= n_after){
3185 //The raw_mem from end will divide displaced old_end
3188 // ______________________________________________________
3189 //| raw_mem | old_begin | old_end | raw_mem |
3190 //|______________|___________|____________|______________|
3192 //New situation with do_after(1):
3193 // _______________________________________________________
3194 //| old_begin + new_beg | new_end |old_end | raw_mem |
3195 //|__________________________|_________|________|_________|
3197 //First copy the part of old_end raw_mem
3198 T* finish_n = old_finish - n_after;
3199 ::boost::container::uninitialized_move_alloc
3200 (this->m_holder.alloc(), finish_n, old_finish, old_finish);
3201 this->m_holder.m_size += n_after;
3202 //Displace the rest of old_end to the new position
3203 boost::container::move_backward(pos, finish_n, old_finish);
3204 //Now overwrite with new_end
3205 //The new_end part is [first + (n - n_after), last)
3206 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, n_after);
3209 //The raw_mem from end will divide new_end part
3212 // _____________________________________________________________
3213 //| raw_mem | old_begin | old_end | raw_mem |
3214 //|______________|___________|____________|_____________________|
3216 //New situation with do_after(2):
3217 // _____________________________________________________________
3218 //| old_begin + new_beg | new_end |old_end | raw_mem |
3219 //|__________________________|_______________|________|_________|
3222 const size_type mid_last_dist = n_after - elemsafter;
3223 //First initialize data in raw memory
3225 //Copy to the old_end part to the uninitialized zone leaving a gap.
3226 ::boost::container::uninitialized_move_alloc
3227 (this->m_holder.alloc(), pos, old_finish, old_finish + mid_last_dist);
3229 typename value_traits::ArrayDestructor old_end_destroyer
3230 (old_finish + mid_last_dist, this->m_holder.alloc(), old_finish - pos);
3232 //Copy the first part to the already constructed old_end zone
3233 insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, elemsafter);
3234 //Copy the rest to the uninitialized zone filling the gap
3235 insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, mid_last_dist);
3236 this->m_holder.m_size += n_after;
3237 old_end_destroyer.release();
3243 void priv_throw_if_out_of_range(size_type n) const
3245 //If n is out of range, throw an out_of_range exception
3246 if (n >= this->size()){
3247 throw_out_of_range("vector::at out of range");
3251 bool priv_in_range(const_iterator pos) const
3253 return (this->begin() <= pos) && (pos < this->end());
3256 bool priv_in_range_or_end(const_iterator pos) const
3258 return (this->begin() <= pos) && (pos <= this->end());
3261 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
3263 unsigned int num_expand_fwd;
3264 unsigned int num_expand_bwd;
3265 unsigned int num_shrink;
3266 unsigned int num_alloc;
3267 void reset_alloc_stats()
3268 { num_expand_fwd = num_expand_bwd = num_alloc = 0, num_shrink = 0; }
3270 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3273 }} //namespace boost::container
3275 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3279 //!has_trivial_destructor_after_move<> == true_type
3280 //!specialization for optimizations
3281 template <class T, class Allocator>
3282 struct has_trivial_destructor_after_move<boost::container::vector<T, Allocator> >
3284 typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
3285 static const bool value = ::boost::has_trivial_destructor_after_move<Allocator>::value &&
3286 ::boost::has_trivial_destructor_after_move<pointer>::value;
3291 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3293 #include <boost/container/detail/config_end.hpp>
3295 #endif // #ifndef BOOST_CONTAINER_CONTAINER_VECTOR_HPP