1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2005-2014. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://www.boost.org/libs/container for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10 
11 #ifndef BOOST_CONTAINER_CONTAINER_VECTOR_HPP
12 #define BOOST_CONTAINER_CONTAINER_VECTOR_HPP
13 
14 #ifndef BOOST_CONFIG_HPP
15 #  include <boost/config.hpp>
16 #endif
17 
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
19 #  pragma once
20 #endif
21 
22 #include <boost/container/detail/config_begin.hpp>
23 #include <boost/container/detail/workaround.hpp>
24 
25 // container
26 #include <boost/container/container_fwd.hpp>
27 #include <boost/container/allocator_traits.hpp>
28 #include <boost/container/new_allocator.hpp> //new_allocator
29 #include <boost/container/throw_exception.hpp>
30 // container detail
31 #include <boost/container/detail/advanced_insert_int.hpp>
32 #include <boost/container/detail/algorithm.hpp> //equal()
33 #include <boost/container/detail/alloc_helpers.hpp>
34 #include <boost/container/detail/allocation_type.hpp>
35 #include <boost/container/detail/copy_move_algo.hpp>
36 #include <boost/container/detail/destroyers.hpp>
37 #include <boost/container/detail/iterator.hpp>
38 #include <boost/container/detail/iterators.hpp>
39 #include <boost/container/detail/iterator_to_raw_pointer.hpp>
40 #include <boost/container/detail/mpl.hpp>
41 #include <boost/container/detail/next_capacity.hpp>
42 #include <boost/container/detail/to_raw_pointer.hpp>
43 #include <boost/container/detail/type_traits.hpp>
44 #include <boost/container/detail/version_type.hpp>
45 // intrusive
46 #include <boost/intrusive/pointer_traits.hpp>
47 // move
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>
52 // move/detail
53 #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
54 #include <boost/move/detail/fwd_macros.hpp>
55 #endif
56 #include <boost/move/detail/move_helpers.hpp>
57 // other
58 #include <boost/core/no_exceptions_support.hpp>
59 #include <boost/assert.hpp>
60 #include <boost/cstdint.hpp>
61 
62 //std
63 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
64 #include <initializer_list>   //for std::initializer_list
65 #endif
66 
67 namespace boost {
68 namespace container {
69 
70 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
71 
72 //#define BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
73 
74 namespace container_detail {
75 
76 #ifndef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
77 
78 template <class Pointer, bool IsConst>
79 class vec_iterator
80 {
81    public:
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;
85    typedef typename if_c
86       < IsConst
87       , typename boost::intrusive::pointer_traits<Pointer>::template
88                                  rebind_pointer<const value_type>::type
89       , Pointer
90       >::type                                                                       pointer;
91    typedef typename boost::intrusive::pointer_traits<pointer>                       ptr_traits;
92    typedef typename ptr_traits::reference                                           reference;
93 
94    #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
95    private:
96    Pointer m_ptr;
97 
98    public:
get_ptr() const99    const Pointer &get_ptr() const BOOST_NOEXCEPT_OR_NOTHROW
100    {  return   m_ptr;  }
101 
get_ptr()102    Pointer &get_ptr() BOOST_NOEXCEPT_OR_NOTHROW
103    {  return   m_ptr;  }
104 
vec_iterator(Pointer ptr)105    explicit vec_iterator(Pointer ptr) BOOST_NOEXCEPT_OR_NOTHROW
106       : m_ptr(ptr)
107    {}
108    #endif   //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
109 
110    public:
111 
112    //Constructors
vec_iterator()113    vec_iterator() BOOST_NOEXCEPT_OR_NOTHROW
114       : m_ptr()   //Value initialization to achieve "null iterators" (N3644)
115    {}
116 
vec_iterator(vec_iterator<Pointer,false> const & other)117    vec_iterator(vec_iterator<Pointer, false> const& other) BOOST_NOEXCEPT_OR_NOTHROW
118       :  m_ptr(other.get_ptr())
119    {}
120 
121    //Pointer like operators
operator *() const122    reference operator*()   const BOOST_NOEXCEPT_OR_NOTHROW
123    {  return *m_ptr;  }
124 
operator ->() const125    pointer operator->()  const BOOST_NOEXCEPT_OR_NOTHROW
126    {  return ::boost::intrusive::pointer_traits<pointer>::pointer_to(this->operator*());  }
127 
operator [](difference_type off) const128    reference operator[](difference_type off) const BOOST_NOEXCEPT_OR_NOTHROW
129    {  return m_ptr[off];   }
130 
131    //Increment / Decrement
operator ++()132    vec_iterator& operator++() BOOST_NOEXCEPT_OR_NOTHROW
133    { ++m_ptr;  return *this; }
134 
operator ++(int)135    vec_iterator operator++(int) BOOST_NOEXCEPT_OR_NOTHROW
136    {  return vec_iterator(m_ptr++); }
137 
operator --()138    vec_iterator& operator--() BOOST_NOEXCEPT_OR_NOTHROW
139    {  --m_ptr; return *this;  }
140 
operator --(int)141    vec_iterator operator--(int) BOOST_NOEXCEPT_OR_NOTHROW
142    {  return vec_iterator(m_ptr--); }
143 
144    //Arithmetic
operator +=(difference_type off)145    vec_iterator& operator+=(difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
146    {  m_ptr += off; return *this;   }
147 
operator -=(difference_type off)148    vec_iterator& operator-=(difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
149    {  m_ptr -= off; return *this;   }
150 
operator +(const vec_iterator & x,difference_type off)151    friend vec_iterator operator+(const vec_iterator &x, difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
152    {  return vec_iterator(x.m_ptr+off);  }
153 
operator +(difference_type off,vec_iterator right)154    friend vec_iterator operator+(difference_type off, vec_iterator right) BOOST_NOEXCEPT_OR_NOTHROW
155    {  right.m_ptr += off;  return right; }
156 
operator -(vec_iterator left,difference_type off)157    friend vec_iterator operator-(vec_iterator left, difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
158    {  left.m_ptr -= off;  return left; }
159 
operator -(const vec_iterator & left,const vec_iterator & right)160    friend difference_type operator-(const vec_iterator &left, const vec_iterator& right) BOOST_NOEXCEPT_OR_NOTHROW
161    {  return left.m_ptr - right.m_ptr;   }
162 
163    //Comparison operators
operator ==(const vec_iterator & l,const vec_iterator & r)164    friend bool operator==   (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
165    {  return l.m_ptr == r.m_ptr;  }
166 
operator !=(const vec_iterator & l,const vec_iterator & r)167    friend bool operator!=   (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
168    {  return l.m_ptr != r.m_ptr;  }
169 
operator <(const vec_iterator & l,const vec_iterator & r)170    friend bool operator<    (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
171    {  return l.m_ptr < r.m_ptr;  }
172 
operator <=(const vec_iterator & l,const vec_iterator & r)173    friend bool operator<=   (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
174    {  return l.m_ptr <= r.m_ptr;  }
175 
operator >(const vec_iterator & l,const vec_iterator & r)176    friend bool operator>    (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
177    {  return l.m_ptr > r.m_ptr;  }
178 
operator >=(const vec_iterator & l,const vec_iterator & r)179    friend bool operator>=   (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
180    {  return l.m_ptr >= r.m_ptr;  }
181 };
182 
183 template<class BiDirPosConstIt, class BiDirValueIt>
184 struct vector_insert_ordered_cursor
185 {
186    typedef typename iterator_traits<BiDirPosConstIt>::value_type  size_type;
187    typedef typename iterator_traits<BiDirValueIt>::reference      reference;
188 
vector_insert_ordered_cursorboost::container::container_detail::vector_insert_ordered_cursor189    vector_insert_ordered_cursor(BiDirPosConstIt posit, BiDirValueIt valueit)
190       : last_position_it(posit), last_value_it(valueit)
191    {}
192 
operator --boost::container::container_detail::vector_insert_ordered_cursor193    void operator --()
194    {
195       --last_value_it;
196       --last_position_it;
197       while(this->get_pos() == size_type(-1)){
198          --last_value_it;
199          --last_position_it;
200       }
201    }
202 
get_posboost::container::container_detail::vector_insert_ordered_cursor203    size_type get_pos() const
204    {  return *last_position_it;  }
205 
get_valboost::container::container_detail::vector_insert_ordered_cursor206    reference get_val()
207    {  return *last_value_it;  }
208 
209    BiDirPosConstIt last_position_it;
210    BiDirValueIt last_value_it;
211 };
212 
213 template<class T, class SizeType, class BiDirValueIt, class Comp>
214 struct vector_merge_cursor
215 {
216    typedef SizeType  size_type;
217    typedef typename iterator_traits<BiDirValueIt>::reference      reference;
218 
vector_merge_cursorboost::container::container_detail::vector_merge_cursor219    vector_merge_cursor(T *pbeg, T *plast, BiDirValueIt valueit, Comp cmp)
220       : m_pbeg(pbeg), m_pcur(--plast), m_valueit(valueit), m_cmp(cmp)
221    {}
222 
operator --boost::container::container_detail::vector_merge_cursor223    void operator --()
224    {
225       --m_valueit;
226       const T &t = *m_valueit;
227       while((m_pcur + 1) != m_pbeg){
228          if(!m_cmp(t, *m_pcur)){
229             break;
230          }
231          --m_pcur;
232       }
233    }
234 
get_posboost::container::container_detail::vector_merge_cursor235    size_type get_pos() const
236    {  return static_cast<size_type>((m_pcur + 1) - m_pbeg);  }
237 
get_valboost::container::container_detail::vector_merge_cursor238    reference get_val()
239    {  return *m_valueit;  }
240 
241    T *const m_pbeg;
242    T *m_pcur;
243    BiDirValueIt m_valueit;
244    Comp m_cmp;
245 };
246 
247 }  //namespace container_detail {
248 
249 template<class Pointer, bool IsConst>
vector_iterator_get_ptr(const container_detail::vec_iterator<Pointer,IsConst> & it)250 const Pointer &vector_iterator_get_ptr(const container_detail::vec_iterator<Pointer, IsConst> &it) BOOST_NOEXCEPT_OR_NOTHROW
251 {  return   it.get_ptr();  }
252 
253 template<class Pointer, bool IsConst>
254 Pointer &get_ptr(container_detail::vec_iterator<Pointer, IsConst> &it) BOOST_NOEXCEPT_OR_NOTHROW
255 {  return  it.get_ptr();  }
256 
257 namespace container_detail {
258 
259 #else //ifndef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
260 
261 template< class MaybeConstPointer
262         , bool ElementTypeIsConst
263             = is_const< typename boost::intrusive::pointer_traits<MaybeConstPointer>::element_type>::value >
264 struct vector_get_ptr_pointer_to_non_const
265 {
266    typedef MaybeConstPointer                                         const_pointer;
267    typedef boost::intrusive::pointer_traits<const_pointer>           pointer_traits_t;
268    typedef typename pointer_traits_t::element_type                   element_type;
269    typedef typename remove_const<element_type>::type                 non_const_element_type;
270    typedef typename pointer_traits_t
271       ::template rebind_pointer<non_const_element_type>::type        return_type;
272 
273    static return_type get_ptr(const const_pointer &ptr) BOOST_NOEXCEPT_OR_NOTHROW
274    {  return boost::intrusive::pointer_traits<return_type>::const_cast_from(ptr);  }
275 };
276 
277 template<class Pointer>
278 struct vector_get_ptr_pointer_to_non_const<Pointer, false>
279 {
280    typedef const Pointer & return_type;
281    static return_type get_ptr(const Pointer &ptr) BOOST_NOEXCEPT_OR_NOTHROW
282    {  return ptr;  }
283 };
284 
285 }  //namespace container_detail {
286 
287 template<class MaybeConstPointer>
288 typename container_detail::vector_get_ptr_pointer_to_non_const<MaybeConstPointer>::return_type
289    vector_iterator_get_ptr(const MaybeConstPointer &ptr) BOOST_NOEXCEPT_OR_NOTHROW
290 {
291    return container_detail::vector_get_ptr_pointer_to_non_const<MaybeConstPointer>::get_ptr(ptr);
292 }
293 
294 namespace container_detail {
295 
296 #endif   //#ifndef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
297 
298 struct uninitialized_size_t {};
299 static const uninitialized_size_t uninitialized_size = uninitialized_size_t();
300 
301 template <class T>
302 struct vector_value_traits_base
303 {
304    static const bool trivial_dctr = is_trivially_destructible<T>::value;
305    static const bool trivial_dctr_after_move = has_trivial_destructor_after_move<T>::value;
306    static const bool trivial_copy = is_trivially_copy_constructible<T>::value;
307    static const bool nothrow_copy = is_nothrow_copy_constructible<T>::value || trivial_copy;
308    static const bool trivial_assign = is_trivially_copy_assignable<T>::value;
309    static const bool nothrow_assign = is_nothrow_copy_assignable<T>::value || trivial_assign;
310 };
311 
312 
313 template <class Allocator>
314 struct vector_value_traits
315    : public vector_value_traits_base<typename Allocator::value_type>
316 {
317    typedef vector_value_traits_base<typename Allocator::value_type> base_t;
318    //This is the anti-exception array destructor
319    //to deallocate values already constructed
320    typedef typename container_detail::if_c
321       <base_t::trivial_dctr
322       ,container_detail::null_scoped_destructor_n<Allocator>
323       ,container_detail::scoped_destructor_n<Allocator>
324       >::type   ArrayDestructor;
325    //This is the anti-exception array deallocator
326    typedef container_detail::scoped_array_deallocator<Allocator> ArrayDeallocator;
327 };
328 
329 //!This struct deallocates and allocated memory
330 template < class Allocator
331          , class AllocatorVersion = typename container_detail::version<Allocator>::type
332          >
333 struct vector_alloc_holder
334    : public Allocator
335 {
336    private:
337    BOOST_MOVABLE_BUT_NOT_COPYABLE(vector_alloc_holder)
338 
339    public:
340    typedef Allocator allocator_type;
341    typedef boost::container::allocator_traits<Allocator> allocator_traits_type;
342    typedef typename allocator_traits_type::pointer       pointer;
343    typedef typename allocator_traits_type::size_type     size_type;
344    typedef typename allocator_traits_type::value_type    value_type;
345 
is_propagable_fromboost::container::container_detail::vector_alloc_holder346    static bool is_propagable_from(const allocator_type &from_alloc, pointer p, const allocator_type &to_alloc, bool const propagate_allocator)
347    {
348       (void)propagate_allocator; (void)p; (void)to_alloc; (void)from_alloc;
349       return (!allocator_traits_type::is_partially_propagable::value ||
350               !allocator_traits_type::storage_is_unpropagable(from_alloc, p)) &&
351               (propagate_allocator || allocator_traits_type::equal(from_alloc, to_alloc));
352    }
353 
are_swap_propagableboost::container::container_detail::vector_alloc_holder354    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)
355    {
356       (void)propagate_allocator; (void)l_p; (void)r_p; (void)l_a; (void)r_a;
357       return (!allocator_traits_type::is_partially_propagable::value ||
358               (!allocator_traits_type::storage_is_unpropagable(r_a, r_p) &&
359                !allocator_traits_type::storage_is_unpropagable(l_a, l_p))
360              ) && (propagate_allocator || allocator_traits_type::equal(l_a, r_a));
361    }
362 
363    //Constructor, does not throw
364    vector_alloc_holder()
BOOST_NOEXCEPT_IFboost::container::container_detail::vector_alloc_holder365       BOOST_NOEXCEPT_IF(container_detail::is_nothrow_default_constructible<Allocator>::value)
366       : Allocator(), m_start(), m_size(), m_capacity()
367    {}
368 
369    //Constructor, does not throw
370    template<class AllocConvertible>
vector_alloc_holderboost::container::container_detail::vector_alloc_holder371    explicit vector_alloc_holder(BOOST_FWD_REF(AllocConvertible) a) BOOST_NOEXCEPT_OR_NOTHROW
372       : Allocator(boost::forward<AllocConvertible>(a)), m_start(), m_size(), m_capacity()
373    {}
374 
375    //Constructor, does not throw
376    template<class AllocConvertible>
vector_alloc_holderboost::container::container_detail::vector_alloc_holder377    vector_alloc_holder(uninitialized_size_t, BOOST_FWD_REF(AllocConvertible) a, size_type initial_size)
378       : Allocator(boost::forward<AllocConvertible>(a))
379       , m_start()
380       , m_size(initial_size)  //Size is initialized here so vector should only call uninitialized_xxx after this
381       , m_capacity()
382    {
383       if(initial_size){
384          pointer reuse = 0;
385          m_start = this->allocation_command(allocate_new, initial_size, m_capacity = initial_size, reuse);
386       }
387    }
388 
389    //Constructor, does not throw
vector_alloc_holderboost::container::container_detail::vector_alloc_holder390    vector_alloc_holder(uninitialized_size_t, size_type initial_size)
391       : Allocator()
392       , m_start()
393       , m_size(initial_size)  //Size is initialized here so vector should only call uninitialized_xxx after this
394       , m_capacity()
395    {
396       if(initial_size){
397          pointer reuse = 0;
398          m_start = this->allocation_command(allocate_new, initial_size, m_capacity = initial_size, reuse);
399       }
400    }
401 
vector_alloc_holderboost::container::container_detail::vector_alloc_holder402    vector_alloc_holder(BOOST_RV_REF(vector_alloc_holder) holder) BOOST_NOEXCEPT_OR_NOTHROW
403       : Allocator(BOOST_MOVE_BASE(Allocator, holder))
404       , m_start(holder.m_start)
405       , m_size(holder.m_size)
406       , m_capacity(holder.m_capacity)
407    {
408       holder.m_start = pointer();
409       holder.m_size = holder.m_capacity = 0;
410    }
411 
vector_alloc_holderboost::container::container_detail::vector_alloc_holder412    vector_alloc_holder(pointer p, size_type capacity, BOOST_RV_REF(vector_alloc_holder) holder)
413       : Allocator(BOOST_MOVE_BASE(Allocator, holder))
414       , m_start(p)
415       , m_size(holder.m_size)
416       , m_capacity(capacity)
417    {
418       allocator_type &this_alloc = this->alloc();
419       allocator_type &x_alloc = holder.alloc();
420       if(this->is_propagable_from(x_alloc, holder.start(), this_alloc, true)){
421          if(this->m_capacity){
422             this->alloc().deallocate(this->m_start, this->m_capacity);
423          }
424          m_start = holder.m_start;
425          m_capacity = holder.m_capacity;
426          holder.m_start = pointer();
427          holder.m_capacity = holder.m_size = 0;
428       }
429       else if(this->m_capacity < holder.m_size){
430          size_type const n = holder.m_size;
431          pointer reuse = pointer();
432          m_start = this->allocation_command(allocate_new, n, m_capacity = n, reuse);
433          #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
434          this->num_alloc += n != 0;
435          #endif
436       }
437    }
438 
vector_alloc_holderboost::container::container_detail::vector_alloc_holder439    vector_alloc_holder(pointer p, size_type n)
440       BOOST_NOEXCEPT_IF(container_detail::is_nothrow_default_constructible<Allocator>::value)
441       : Allocator()
442       , m_start(p)
443       , m_size()
444       , m_capacity(n)
445    {}
446 
447    template<class AllocFwd>
vector_alloc_holderboost::container::container_detail::vector_alloc_holder448    vector_alloc_holder(pointer p, size_type n, BOOST_FWD_REF(AllocFwd) a)
449       : Allocator(::boost::forward<AllocFwd>(a))
450       , m_start(p)
451       , m_size()
452       , m_capacity(n)
453    {}
454 
~vector_alloc_holderboost::container::container_detail::vector_alloc_holder455    ~vector_alloc_holder() BOOST_NOEXCEPT_OR_NOTHROW
456    {
457       if(this->m_capacity){
458          this->alloc().deallocate(this->m_start, this->m_capacity);
459       }
460    }
461 
allocation_commandboost::container::container_detail::vector_alloc_holder462    pointer allocation_command(boost::container::allocation_type command,
463                               size_type limit_size, size_type &prefer_in_recvd_out_size, pointer &reuse)
464    {
465       typedef typename container_detail::version<Allocator>::type alloc_version;
466       return this->priv_allocation_command(alloc_version(), command, limit_size, prefer_in_recvd_out_size, reuse);
467    }
468 
try_expand_fwdboost::container::container_detail::vector_alloc_holder469    bool try_expand_fwd(size_type at_least)
470    {
471       //There is not enough memory, try to expand the old one
472       const size_type new_cap = this->capacity() + at_least;
473       size_type real_cap = new_cap;
474       pointer reuse = this->start();
475       bool const success = !!this->allocation_command(expand_fwd, new_cap, real_cap, reuse);
476       //Check for forward expansion
477       if(success){
478          #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
479          ++this->num_expand_fwd;
480          #endif
481          this->capacity(real_cap);
482       }
483       return success;
484    }
485 
next_capacityboost::container::container_detail::vector_alloc_holder486    size_type next_capacity(size_type additional_objects) const
487    {
488       return next_capacity_calculator
489          <size_type, NextCapacityDouble //NextCapacity60Percent
490          >::get( allocator_traits_type::max_size(this->alloc())
491                , this->m_capacity, additional_objects );
492    }
493 
494    pointer     m_start;
495    size_type   m_size;
496    size_type   m_capacity;
497 
swap_resourcesboost::container::container_detail::vector_alloc_holder498    void swap_resources(vector_alloc_holder &x) BOOST_NOEXCEPT_OR_NOTHROW
499    {
500       boost::adl_move_swap(this->m_start, x.m_start);
501       boost::adl_move_swap(this->m_size, x.m_size);
502       boost::adl_move_swap(this->m_capacity, x.m_capacity);
503    }
504 
steal_resourcesboost::container::container_detail::vector_alloc_holder505    void steal_resources(vector_alloc_holder &x) BOOST_NOEXCEPT_OR_NOTHROW
506    {
507       this->m_start     = x.m_start;
508       this->m_size      = x.m_size;
509       this->m_capacity  = x.m_capacity;
510       x.m_start = pointer();
511       x.m_size = x.m_capacity = 0;
512    }
513 
allocboost::container::container_detail::vector_alloc_holder514    Allocator &alloc() BOOST_NOEXCEPT_OR_NOTHROW
515    {  return *this;  }
516 
allocboost::container::container_detail::vector_alloc_holder517    const Allocator &alloc() const BOOST_NOEXCEPT_OR_NOTHROW
518    {  return *this;  }
519 
startboost::container::container_detail::vector_alloc_holder520    const pointer   &start() const     BOOST_NOEXCEPT_OR_NOTHROW {  return m_start;  }
capacityboost::container::container_detail::vector_alloc_holder521    const size_type &capacity() const  BOOST_NOEXCEPT_OR_NOTHROW {  return m_capacity;  }
startboost::container::container_detail::vector_alloc_holder522    void start(const pointer &p)       BOOST_NOEXCEPT_OR_NOTHROW {  m_start = p;  }
capacityboost::container::container_detail::vector_alloc_holder523    void capacity(const size_type &c)  BOOST_NOEXCEPT_OR_NOTHROW {  m_capacity = c;  }
524 
525    private:
priv_first_allocationboost::container::container_detail::vector_alloc_holder526    void priv_first_allocation(size_type cap)
527    {
528       if(cap){
529          pointer reuse = 0;
530          m_start = this->allocation_command(allocate_new, cap, cap, reuse);
531          m_capacity = cap;
532          #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
533          ++this->num_alloc;
534          #endif
535       }
536    }
537 
priv_allocation_commandboost::container::container_detail::vector_alloc_holder538    pointer priv_allocation_command(version_1, boost::container::allocation_type command,
539                          size_type ,
540                          size_type &prefer_in_recvd_out_size,
541                          pointer &reuse)
542    {
543       (void)command;
544       BOOST_ASSERT( (command & allocate_new));
545       BOOST_ASSERT(!(command & nothrow_allocation));
546       pointer const p = allocator_traits_type::allocate(this->alloc(), prefer_in_recvd_out_size, reuse);
547       reuse = pointer();
548       return p;
549    }
550 
priv_allocation_commandboost::container::container_detail::vector_alloc_holder551    pointer priv_allocation_command(version_2, boost::container::allocation_type command,
552                          size_type limit_size,
553                          size_type &prefer_in_recvd_out_size,
554                          pointer &reuse)
555    {
556       return this->alloc().allocation_command(command, limit_size, prefer_in_recvd_out_size, reuse);
557    }
558 };
559 
560 //!This struct deallocates and allocated memory
561 template <class Allocator>
562 struct vector_alloc_holder<Allocator, version_0>
563    : public Allocator
564 {
565    private:
566    BOOST_MOVABLE_BUT_NOT_COPYABLE(vector_alloc_holder)
567 
568    public:
569    typedef boost::container::allocator_traits<Allocator> allocator_traits_type;
570    typedef typename allocator_traits_type::pointer       pointer;
571    typedef typename allocator_traits_type::size_type     size_type;
572    typedef typename allocator_traits_type::value_type    value_type;
573 
574    template <class OtherAllocator, class OtherAllocatorVersion>
575    friend struct vector_alloc_holder;
576 
577    //Constructor, does not throw
578    vector_alloc_holder()
BOOST_NOEXCEPT_IFboost::container::container_detail::vector_alloc_holder579       BOOST_NOEXCEPT_IF(container_detail::is_nothrow_default_constructible<Allocator>::value)
580       : Allocator(), m_size()
581    {}
582 
583    //Constructor, does not throw
584    template<class AllocConvertible>
vector_alloc_holderboost::container::container_detail::vector_alloc_holder585    explicit vector_alloc_holder(BOOST_FWD_REF(AllocConvertible) a) BOOST_NOEXCEPT_OR_NOTHROW
586       : Allocator(boost::forward<AllocConvertible>(a)), m_size()
587    {}
588 
589    //Constructor, does not throw
590    template<class AllocConvertible>
vector_alloc_holderboost::container::container_detail::vector_alloc_holder591    vector_alloc_holder(uninitialized_size_t, BOOST_FWD_REF(AllocConvertible) a, size_type initial_size)
592       : Allocator(boost::forward<AllocConvertible>(a))
593       , m_size(initial_size)  //Size is initialized here...
594    {
595       //... and capacity here, so vector, must call uninitialized_xxx in the derived constructor
596       this->priv_first_allocation(initial_size);
597    }
598 
599    //Constructor, does not throw
vector_alloc_holderboost::container::container_detail::vector_alloc_holder600    vector_alloc_holder(uninitialized_size_t, size_type initial_size)
601       : Allocator()
602       , m_size(initial_size)  //Size is initialized here...
603    {
604       //... and capacity here, so vector, must call uninitialized_xxx in the derived constructor
605       this->priv_first_allocation(initial_size);
606    }
607 
vector_alloc_holderboost::container::container_detail::vector_alloc_holder608    vector_alloc_holder(BOOST_RV_REF(vector_alloc_holder) holder)
609       : Allocator(BOOST_MOVE_BASE(Allocator, holder))
610       , m_size(holder.m_size) //Size is initialized here so vector should only call uninitialized_xxx after this
611    {
612       ::boost::container::uninitialized_move_alloc_n
613          (this->alloc(), container_detail::to_raw_pointer(holder.start()), m_size, container_detail::to_raw_pointer(this->start()));
614    }
615 
616    template<class OtherAllocator, class OtherAllocatorVersion>
vector_alloc_holderboost::container::container_detail::vector_alloc_holder617    vector_alloc_holder(BOOST_RV_REF_BEG vector_alloc_holder<OtherAllocator, OtherAllocatorVersion> BOOST_RV_REF_END holder)
618       : Allocator()
619       , m_size(holder.m_size) //Initialize it to m_size as first_allocation can only succeed or abort
620    {
621       //Different allocator type so we must check we have enough storage
622       const size_type n = holder.m_size;
623       this->priv_first_allocation(n);
624       ::boost::container::uninitialized_move_alloc_n
625          (this->alloc(), container_detail::to_raw_pointer(holder.start()), n, container_detail::to_raw_pointer(this->start()));
626    }
627 
priv_first_allocationboost::container::container_detail::vector_alloc_holder628    void priv_first_allocation(size_type cap)
629    {
630       if(cap > Allocator::internal_capacity){
631          throw_bad_alloc();
632       }
633    }
634 
deep_swapboost::container::container_detail::vector_alloc_holder635    void deep_swap(vector_alloc_holder &x)
636    {
637       this->priv_deep_swap(x);
638    }
639 
640    template<class OtherAllocator, class OtherAllocatorVersion>
deep_swapboost::container::container_detail::vector_alloc_holder641    void deep_swap(vector_alloc_holder<OtherAllocator, OtherAllocatorVersion> &x)
642    {
643       if(this->m_size > OtherAllocator::internal_capacity || x.m_size > Allocator::internal_capacity){
644          throw_bad_alloc();
645       }
646       this->priv_deep_swap(x);
647    }
648 
swap_resourcesboost::container::container_detail::vector_alloc_holder649    void swap_resources(vector_alloc_holder &x) BOOST_NOEXCEPT_OR_NOTHROW
650    {  //Containers with version 0 allocators can't be moved without moving elements one by one
651       throw_bad_alloc();
652    }
653 
654 
steal_resourcesboost::container::container_detail::vector_alloc_holder655    void steal_resources(vector_alloc_holder &)
656    {  //Containers with version 0 allocators can't be moved without moving elements one by one
657       throw_bad_alloc();
658    }
659 
allocboost::container::container_detail::vector_alloc_holder660    Allocator &alloc() BOOST_NOEXCEPT_OR_NOTHROW
661    {  return *this;  }
662 
allocboost::container::container_detail::vector_alloc_holder663    const Allocator &alloc() const BOOST_NOEXCEPT_OR_NOTHROW
664    {  return *this;  }
665 
try_expand_fwdboost::container::container_detail::vector_alloc_holder666    bool try_expand_fwd(size_type at_least)
667    {  return !at_least;  }
668 
startboost::container::container_detail::vector_alloc_holder669    pointer start() const       BOOST_NOEXCEPT_OR_NOTHROW {  return Allocator::internal_storage();  }
capacityboost::container::container_detail::vector_alloc_holder670    size_type  capacity() const BOOST_NOEXCEPT_OR_NOTHROW {  return Allocator::internal_capacity;  }
671    size_type   m_size;
672 
673    private:
674 
675    template<class OtherAllocator, class OtherAllocatorVersion>
priv_deep_swapboost::container::container_detail::vector_alloc_holder676    void priv_deep_swap(vector_alloc_holder<OtherAllocator, OtherAllocatorVersion> &x)
677    {
678       const size_type MaxTmpStorage = sizeof(value_type)*Allocator::internal_capacity;
679       value_type *const first_this = container_detail::to_raw_pointer(this->start());
680       value_type *const first_x = container_detail::to_raw_pointer(x.start());
681 
682       if(this->m_size < x.m_size){
683          boost::container::deep_swap_alloc_n<MaxTmpStorage>(this->alloc(), first_this, this->m_size, first_x, x.m_size);
684       }
685       else{
686          boost::container::deep_swap_alloc_n<MaxTmpStorage>(this->alloc(), first_x, x.m_size, first_this, this->m_size);
687       }
688       boost::adl_move_swap(this->m_size, x.m_size);
689    }
690 };
691 
692 }  //namespace container_detail {
693 
694 #endif   //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
695 
696 //! A vector is a sequence that supports random access to elements, constant
697 //! time insertion and removal of elements at the end, and linear time insertion
698 //! and removal of elements at the beginning or in the middle. The number of
699 //! elements in a vector may vary dynamically; memory management is automatic.
700 //!
701 //! \tparam T The type of object that is stored in the vector
702 //! \tparam Allocator The allocator used for all internal memory management
703 template <class T, class Allocator BOOST_CONTAINER_DOCONLY(= new_allocator<T>) >
704 class vector
705 {
706    #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
707 
708    struct value_less
709    {
710       typedef typename boost::container::allocator_traits<Allocator>::value_type value_type;
operator ()boost::container::vector::value_less711       bool operator()(const value_type &a, const value_type &b) const
712          {  return a < b;  }
713    };
714 
715    typedef typename container_detail::version<Allocator>::type alloc_version;
716    typedef boost::container::container_detail::vector_alloc_holder<Allocator> alloc_holder_t;
717    alloc_holder_t m_holder;
718    typedef allocator_traits<Allocator>                      allocator_traits_type;
719    template <class U, class UAllocator>
720    friend class vector;
721 
722    typedef typename allocator_traits_type::pointer  pointer_impl;
723    typedef container_detail::vec_iterator<pointer_impl, false> iterator_impl;
724    typedef container_detail::vec_iterator<pointer_impl, true > const_iterator_impl;
725 
726    protected:
is_propagable_from(const Allocator & from_alloc,pointer_impl p,const Allocator & to_alloc,bool const propagate_allocator)727    static bool is_propagable_from(const Allocator &from_alloc, pointer_impl p, const Allocator &to_alloc, bool const propagate_allocator)
728    {  return alloc_holder_t::is_propagable_from(from_alloc, p, to_alloc, propagate_allocator);  }
729 
are_swap_propagable(const Allocator & l_a,pointer_impl l_p,const Allocator & r_a,pointer_impl r_p,bool const propagate_allocator)730    static bool are_swap_propagable( const Allocator &l_a, pointer_impl l_p
731                                   , const Allocator &r_a, pointer_impl r_p, bool const propagate_allocator)
732    {  return alloc_holder_t::are_swap_propagable(l_a, l_p, r_a, r_p, propagate_allocator);  }
733 
734    #endif   //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
735    public:
736    //////////////////////////////////////////////
737    //
738    //                    types
739    //
740    //////////////////////////////////////////////
741 
742    typedef T                                                                           value_type;
743    typedef typename ::boost::container::allocator_traits<Allocator>::pointer           pointer;
744    typedef typename ::boost::container::allocator_traits<Allocator>::const_pointer     const_pointer;
745    typedef typename ::boost::container::allocator_traits<Allocator>::reference         reference;
746    typedef typename ::boost::container::allocator_traits<Allocator>::const_reference   const_reference;
747    typedef typename ::boost::container::allocator_traits<Allocator>::size_type         size_type;
748    typedef typename ::boost::container::allocator_traits<Allocator>::difference_type   difference_type;
749    typedef Allocator                                                                   allocator_type;
750    typedef Allocator                                                                   stored_allocator_type;
751    #if defined BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
752    typedef BOOST_CONTAINER_IMPDEF(pointer)                                             iterator;
753    typedef BOOST_CONTAINER_IMPDEF(const_pointer)                                       const_iterator;
754    #else
755    typedef BOOST_CONTAINER_IMPDEF(iterator_impl)                                       iterator;
756    typedef BOOST_CONTAINER_IMPDEF(const_iterator_impl)                                 const_iterator;
757    #endif
758    typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<iterator>)        reverse_iterator;
759    typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<const_iterator>)  const_reverse_iterator;
760 
761    #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
762    private:
763    BOOST_COPYABLE_AND_MOVABLE(vector)
764    typedef container_detail::vector_value_traits<Allocator> value_traits;
765    typedef constant_iterator<T, difference_type>            cvalue_iterator;
766 
767    protected:
768 
steal_resources(vector & x)769    void steal_resources(vector &x)
770    {  return this->m_holder.steal_resources(x.m_holder);   }
771 
772    struct initial_capacity_t{};
773    template<class AllocFwd>
vector(initial_capacity_t,pointer initial_memory,size_type capacity,BOOST_FWD_REF (AllocFwd)a)774    vector(initial_capacity_t, pointer initial_memory, size_type capacity, BOOST_FWD_REF(AllocFwd) a)
775       : m_holder(initial_memory, capacity, ::boost::forward<AllocFwd>(a))
776    {}
777 
vector(initial_capacity_t,pointer initial_memory,size_type capacity)778    vector(initial_capacity_t, pointer initial_memory, size_type capacity)
779       : m_holder(initial_memory, capacity)
780    {}
781 
782    #endif   //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
783 
784    public:
785    //////////////////////////////////////////////
786    //
787    //          construct/copy/destroy
788    //
789    //////////////////////////////////////////////
790 
791    //! <b>Effects</b>: Constructs a vector taking the allocator as parameter.
792    //!
793    //! <b>Throws</b>: Nothing.
794    //!
795    //! <b>Complexity</b>: Constant.
vector()796    vector() BOOST_NOEXCEPT_OR_NOTHROW
797       : m_holder()
798    {}
799 
800    //! <b>Effects</b>: Constructs a vector taking the allocator as parameter.
801    //!
802    //! <b>Throws</b>: Nothing
803    //!
804    //! <b>Complexity</b>: Constant.
vector(const allocator_type & a)805    explicit vector(const allocator_type& a) BOOST_NOEXCEPT_OR_NOTHROW
806       : m_holder(a)
807    {}
808 
809    //! <b>Effects</b>: Constructs a vector and inserts n value initialized values.
810    //!
811    //! <b>Throws</b>: If allocator_type's allocation
812    //!   throws or T's value initialization throws.
813    //!
814    //! <b>Complexity</b>: Linear to n.
vector(size_type n)815    explicit vector(size_type n)
816       :  m_holder(container_detail::uninitialized_size, n)
817    {
818       #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
819       this->num_alloc += n != 0;
820       #endif
821       boost::container::uninitialized_value_init_alloc_n
822          (this->m_holder.alloc(), n, container_detail::to_raw_pointer(this->m_holder.start()));
823    }
824 
825    //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
826    //!   and inserts n default initialized values.
827    //!
828    //! <b>Throws</b>: If allocator_type's allocation
829    //!   throws or T's default initialization throws.
830    //!
831    //! <b>Complexity</b>: Linear to n.
832    //!
833    //! <b>Note</b>: Non-standard extension
vector(size_type n,default_init_t)834    vector(size_type n, default_init_t)
835       :  m_holder(container_detail::uninitialized_size, n)
836    {
837       #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
838       this->num_alloc += n != 0;
839       #endif
840       boost::container::uninitialized_default_init_alloc_n
841          (this->m_holder.alloc(), n, container_detail::to_raw_pointer(this->m_holder.start()));
842    }
843 
844    //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
845    //!   and inserts n value initialized values.
846    //!
847    //! <b>Throws</b>: If allocator_type's allocation
848    //!   throws or T's value initialization throws.
849    //!
850    //! <b>Complexity</b>: Linear to n.
vector(size_type n,const allocator_type & a)851    explicit vector(size_type n, const allocator_type &a)
852       :  m_holder(container_detail::uninitialized_size, a, n)
853    {
854       #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
855       this->num_alloc += n != 0;
856       #endif
857       boost::container::uninitialized_value_init_alloc_n
858          (this->m_holder.alloc(), n, container_detail::to_raw_pointer(this->m_holder.start()));
859    }
860 
861    //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
862    //!   and inserts n default initialized values.
863    //!
864    //! <b>Throws</b>: If allocator_type's allocation
865    //!   throws or T's default initialization throws.
866    //!
867    //! <b>Complexity</b>: Linear to n.
868    //!
869    //! <b>Note</b>: Non-standard extension
vector(size_type n,default_init_t,const allocator_type & a)870    vector(size_type n, default_init_t, const allocator_type &a)
871       :  m_holder(container_detail::uninitialized_size, a, n)
872    {
873       #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
874       this->num_alloc += n != 0;
875       #endif
876       boost::container::uninitialized_default_init_alloc_n
877          (this->m_holder.alloc(), n, container_detail::to_raw_pointer(this->m_holder.start()));
878    }
879 
880    //! <b>Effects</b>: Constructs a vector
881    //!   and inserts n copies of value.
882    //!
883    //! <b>Throws</b>: If allocator_type's allocation
884    //!   throws or T's copy constructor throws.
885    //!
886    //! <b>Complexity</b>: Linear to n.
vector(size_type n,const T & value)887    vector(size_type n, const T& value)
888       :  m_holder(container_detail::uninitialized_size, n)
889    {
890       #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
891       this->num_alloc += n != 0;
892       #endif
893       boost::container::uninitialized_fill_alloc_n
894          (this->m_holder.alloc(), value, n, container_detail::to_raw_pointer(this->m_holder.start()));
895    }
896 
897    //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
898    //!   and inserts n copies of value.
899    //!
900    //! <b>Throws</b>: If allocation
901    //!   throws or T's copy constructor throws.
902    //!
903    //! <b>Complexity</b>: Linear to n.
vector(size_type n,const T & value,const allocator_type & a)904    vector(size_type n, const T& value, const allocator_type& a)
905       :  m_holder(container_detail::uninitialized_size, a, n)
906    {
907       #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
908       this->num_alloc += n != 0;
909       #endif
910       boost::container::uninitialized_fill_alloc_n
911          (this->m_holder.alloc(), value, n, container_detail::to_raw_pointer(this->m_holder.start()));
912    }
913 
914    //! <b>Effects</b>: Constructs a vector
915    //!   and inserts a copy of the range [first, last) in the vector.
916    //!
917    //! <b>Throws</b>: If allocator_type's allocation
918    //!   throws or T's constructor taking a dereferenced InIt throws.
919    //!
920    //! <b>Complexity</b>: Linear to the range [first, last).
921    template <class InIt>
vector(InIt first,InIt last)922    vector(InIt first, InIt last)
923       :  m_holder()
924    {  this->assign(first, last); }
925 
926    //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
927    //!   and inserts a copy of the range [first, last) in the vector.
928    //!
929    //! <b>Throws</b>: If allocator_type's allocation
930    //!   throws or T's constructor taking a dereferenced InIt throws.
931    //!
932    //! <b>Complexity</b>: Linear to the range [first, last).
933    template <class InIt>
vector(InIt first,InIt last,const allocator_type & a)934    vector(InIt first, InIt last, const allocator_type& a)
935       :  m_holder(a)
936    {  this->assign(first, last); }
937 
938    //! <b>Effects</b>: Copy constructs a vector.
939    //!
940    //! <b>Postcondition</b>: x == *this.
941    //!
942    //! <b>Throws</b>: If allocator_type's allocation
943    //!   throws or T's copy constructor throws.
944    //!
945    //! <b>Complexity</b>: Linear to the elements x contains.
vector(const vector & x)946    vector(const vector &x)
947       :  m_holder( container_detail::uninitialized_size
948                  , allocator_traits_type::select_on_container_copy_construction(x.m_holder.alloc())
949                  , x.size())
950    {
951       #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
952       this->num_alloc += x.size() != 0;
953       #endif
954       ::boost::container::uninitialized_copy_alloc_n
955          ( this->m_holder.alloc(), container_detail::to_raw_pointer(x.m_holder.start())
956          , x.size(), container_detail::to_raw_pointer(this->m_holder.start()));
957    }
958 
959    //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
960    //!
961    //! <b>Throws</b>: Nothing
962    //!
963    //! <b>Complexity</b>: Constant.
vector(BOOST_RV_REF (vector)x)964    vector(BOOST_RV_REF(vector) x) BOOST_NOEXCEPT_OR_NOTHROW
965       :  m_holder(boost::move(x.m_holder))
966    {  BOOST_STATIC_ASSERT((!allocator_traits_type::is_partially_propagable::value));  }
967 
968    #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
969    //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
970    //!  and inserts a copy of the range [il.begin(), il.last()) in the vector
971    //!
972    //! <b>Throws</b>: If T's constructor taking a dereferenced initializer_list iterator throws.
973    //!
974    //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
vector(std::initializer_list<value_type> il,const allocator_type & a=allocator_type ())975    vector(std::initializer_list<value_type> il, const allocator_type& a = allocator_type())
976       : m_holder(a)
977    {
978       this->assign(il.begin(), il.end());
979    }
980    #endif
981 
982    #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
983 
984    //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
985    //!
986    //! <b>Throws</b>: If T's move constructor or allocation throws
987    //!
988    //! <b>Complexity</b>: Linear.
989    //!
990    //! <b>Note</b>: Non-standard extension to support static_vector
991    template<class OtherAllocator>
vector(BOOST_RV_REF_BEG vector<T,OtherAllocator> BOOST_RV_REF_END x,typename container_detail::enable_if_c<container_detail::is_version<OtherAllocator,0>::value>::type * =0)992    vector(BOOST_RV_REF_BEG vector<T, OtherAllocator> BOOST_RV_REF_END x
993          , typename container_detail::enable_if_c
994             < container_detail::is_version<OtherAllocator, 0>::value>::type * = 0
995          )
996       :  m_holder(boost::move(x.m_holder))
997    {}
998 
999    #endif   //!defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1000 
1001    //! <b>Effects</b>: Copy constructs a vector using the specified allocator.
1002    //!
1003    //! <b>Postcondition</b>: x == *this.
1004    //!
1005    //! <b>Throws</b>: If allocation
1006    //!   throws or T's copy constructor throws.
1007    //!
1008    //! <b>Complexity</b>: Linear to the elements x contains.
vector(const vector & x,const allocator_type & a)1009    vector(const vector &x, const allocator_type &a)
1010       :  m_holder(container_detail::uninitialized_size, a, x.size())
1011    {
1012       #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
1013       this->num_alloc += x.size() != 0;
1014       #endif
1015       ::boost::container::uninitialized_copy_alloc_n_source
1016          ( this->m_holder.alloc(), container_detail::to_raw_pointer(x.m_holder.start())
1017          , x.size(), container_detail::to_raw_pointer(this->m_holder.start()));
1018    }
1019 
1020    //! <b>Effects</b>: Move constructor using the specified allocator.
1021    //!                 Moves x's resources to *this if a == allocator_type().
1022    //!                 Otherwise copies values from x to *this.
1023    //!
1024    //! <b>Throws</b>: If allocation or T's copy constructor throws.
1025    //!
1026    //! <b>Complexity</b>: Constant if a == x.get_allocator(), linear otherwise.
vector(BOOST_RV_REF (vector)x,const allocator_type & a)1027    vector(BOOST_RV_REF(vector) x, const allocator_type &a)
1028       :  m_holder( container_detail::uninitialized_size, a
1029                  , is_propagable_from(x.get_stored_allocator(), x.m_holder.start(), a, true) ? 0 : x.size()
1030                  )
1031    {
1032       if(is_propagable_from(x.get_stored_allocator(), x.m_holder.start(), a, true)){
1033          this->m_holder.steal_resources(x.m_holder);
1034       }
1035       else{
1036          const size_type n = x.size();
1037          #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
1038          this->num_alloc += n != 0;
1039          #endif
1040          ::boost::container::uninitialized_move_alloc_n_source
1041             ( this->m_holder.alloc(), container_detail::to_raw_pointer(x.m_holder.start())
1042             , n, container_detail::to_raw_pointer(this->m_holder.start()));
1043       }
1044    }
1045 
1046    //! <b>Effects</b>: Destroys the vector. All stored values are destroyed
1047    //!   and used memory is deallocated.
1048    //!
1049    //! <b>Throws</b>: Nothing.
1050    //!
1051    //! <b>Complexity</b>: Linear to the number of elements.
~vector()1052    ~vector() BOOST_NOEXCEPT_OR_NOTHROW
1053    {
1054       boost::container::destroy_alloc_n
1055          (this->get_stored_allocator(), container_detail::to_raw_pointer(this->m_holder.start()), this->m_holder.m_size);
1056       //vector_alloc_holder deallocates the data
1057    }
1058 
1059    //! <b>Effects</b>: Makes *this contain the same elements as x.
1060    //!
1061    //! <b>Postcondition</b>: this->size() == x.size(). *this contains a copy
1062    //! of each of x's elements.
1063    //!
1064    //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment throws.
1065    //!
1066    //! <b>Complexity</b>: Linear to the number of elements in x.
operator =(BOOST_COPY_ASSIGN_REF (vector)x)1067    vector& operator=(BOOST_COPY_ASSIGN_REF(vector) x)
1068    {
1069       if (&x != this){
1070          this->priv_copy_assign(x);
1071       }
1072       return *this;
1073    }
1074 
1075    #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1076    //! <b>Effects</b>: Make *this container contains elements from il.
1077    //!
1078    //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
operator =(std::initializer_list<value_type> il)1079    vector& operator=(std::initializer_list<value_type> il)
1080    {
1081       this->assign(il.begin(), il.end());
1082       return *this;
1083    }
1084    #endif
1085 
1086    //! <b>Effects</b>: Move assignment. All x's values are transferred to *this.
1087    //!
1088    //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
1089    //!   before the function.
1090    //!
1091    //! <b>Throws</b>: If allocator_traits_type::propagate_on_container_move_assignment
1092    //!   is false and (allocation throws or value_type's move constructor throws)
1093    //!
1094    //! <b>Complexity</b>: Constant if allocator_traits_type::
1095    //!   propagate_on_container_move_assignment is true or
1096    //!   this->get>allocator() == x.get_allocator(). Linear otherwise.
operator =(BOOST_RV_REF (vector)x)1097    vector& operator=(BOOST_RV_REF(vector) x)
1098       BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value
1099                                   || allocator_traits_type::is_always_equal::value)
1100    {
1101       this->priv_move_assign(boost::move(x));
1102       return *this;
1103    }
1104 
1105    #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1106 
1107    //! <b>Effects</b>: Move assignment. All x's values are transferred to *this.
1108    //!
1109    //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
1110    //!   before the function.
1111    //!
1112    //! <b>Throws</b>: If move constructor/assignment of T throws or allocation throws
1113    //!
1114    //! <b>Complexity</b>: Linear.
1115    //!
1116    //! <b>Note</b>: Non-standard extension to support static_vector
1117    template<class OtherAllocator>
1118    typename container_detail::enable_if_and
1119                            < vector&
1120                            , container_detail::is_version<OtherAllocator, 0>
1121                            , container_detail::is_different<OtherAllocator, allocator_type>
1122                            >::type
operator =(BOOST_RV_REF_BEG vector<value_type,OtherAllocator> BOOST_RV_REF_END x)1123       operator=(BOOST_RV_REF_BEG vector<value_type, OtherAllocator> BOOST_RV_REF_END x)
1124    {
1125       this->priv_move_assign(boost::move(x));
1126       return *this;
1127    }
1128 
1129    //! <b>Effects</b>: Copy assignment. All x's values are copied to *this.
1130    //!
1131    //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
1132    //!   before the function.
1133    //!
1134    //! <b>Throws</b>: If move constructor/assignment of T throws or allocation throws
1135    //!
1136    //! <b>Complexity</b>: Linear.
1137    //!
1138    //! <b>Note</b>: Non-standard extension to support static_vector
1139    template<class OtherAllocator>
1140    typename container_detail::enable_if_and
1141                            < vector&
1142                            , container_detail::is_version<OtherAllocator, 0>
1143                            , container_detail::is_different<OtherAllocator, allocator_type>
1144                            >::type
operator =(const vector<value_type,OtherAllocator> & x)1145       operator=(const vector<value_type, OtherAllocator> &x)
1146    {
1147       this->priv_copy_assign(x);
1148       return *this;
1149    }
1150 
1151    #endif
1152 
1153    //! <b>Effects</b>: Assigns the the range [first, last) to *this.
1154    //!
1155    //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment or
1156    //!   T's constructor/assignment from dereferencing InpIt throws.
1157    //!
1158    //! <b>Complexity</b>: Linear to n.
1159    template <class InIt>
1160    void assign(InIt first, InIt last
1161       BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename container_detail::disable_if_or
1162          < void
1163          BOOST_MOVE_I container_detail::is_convertible<InIt BOOST_MOVE_I size_type>
1164          BOOST_MOVE_I container_detail::and_
1165             < container_detail::is_different<alloc_version BOOST_MOVE_I version_0>
1166             BOOST_MOVE_I container_detail::is_not_input_iterator<InIt>
1167             >
1168          >::type * = 0)
1169       )
1170    {
1171       //Overwrite all elements we can from [first, last)
1172       iterator cur = this->begin();
1173       const iterator end_it = this->end();
1174       for ( ; first != last && cur != end_it; ++cur, ++first){
1175          *cur = *first;
1176       }
1177 
1178       if (first == last){
1179          //There are no more elements in the sequence, erase remaining
1180          T* const end_pos = this->back_raw();
1181          const size_type n = static_cast<size_type>(end_pos - container_detail::iterator_to_raw_pointer(cur));
1182          this->priv_destroy_last_n(n);
1183       }
1184       else{
1185          //There are more elements in the range, insert the remaining ones
1186          this->insert(this->cend(), first, last);
1187       }
1188    }
1189 
1190    #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1191    //! <b>Effects</b>: Assigns the the range [il.begin(), il.end()) to *this.
1192    //!
1193    //! <b>Throws</b>: If memory allocation throws or
1194    //!   T's constructor from dereferencing iniializer_list iterator throws.
1195    //!
assign(std::initializer_list<T> il)1196    void assign(std::initializer_list<T> il)
1197    {
1198       this->assign(il.begin(), il.end());
1199    }
1200    #endif
1201 
1202    //! <b>Effects</b>: Assigns the the range [first, last) to *this.
1203    //!
1204    //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment or
1205    //!   T's constructor/assignment from dereferencing InpIt throws.
1206    //!
1207    //! <b>Complexity</b>: Linear to n.
1208    template <class FwdIt>
1209    void assign(FwdIt first, FwdIt last
1210       BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename container_detail::disable_if_or
1211          < void
1212          BOOST_MOVE_I container_detail::is_same<alloc_version BOOST_MOVE_I version_0>
1213          BOOST_MOVE_I container_detail::is_convertible<FwdIt BOOST_MOVE_I size_type>
1214          BOOST_MOVE_I container_detail::is_input_iterator<FwdIt>
1215          >::type * = 0)
1216       )
1217    {
1218       //For Fwd iterators the standard only requires EmplaceConstructible and assignable from *first
1219       //so we can't do any backwards allocation
1220       const size_type input_sz = static_cast<size_type>(boost::container::iterator_distance(first, last));
1221       const size_type old_capacity = this->capacity();
1222       if(input_sz > old_capacity){  //If input range is too big, we need to reallocate
1223          size_type real_cap = 0;
1224          pointer reuse(this->m_holder.start());
1225          pointer const ret(this->m_holder.allocation_command(allocate_new|expand_fwd, input_sz, real_cap = input_sz, reuse));
1226          if(!reuse){  //New allocation, just emplace new values
1227             #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
1228             ++this->num_alloc;
1229             #endif
1230             pointer const old_p = this->m_holder.start();
1231             if(old_p){
1232                this->priv_destroy_all();
1233                this->m_holder.alloc().deallocate(old_p, old_capacity);
1234             }
1235             this->m_holder.start(ret);
1236             this->m_holder.capacity(real_cap);
1237             this->m_holder.m_size = 0;
1238             this->priv_uninitialized_construct_at_end(first, last);
1239             return;
1240          }
1241          else{
1242             #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
1243             ++this->num_expand_fwd;
1244             #endif
1245             this->m_holder.capacity(real_cap);
1246             //Forward expansion, use assignment + back deletion/construction that comes later
1247          }
1248       }
1249       //Overwrite all elements we can from [first, last)
1250       iterator cur = this->begin();
1251       const iterator end_it = this->end();
1252       for ( ; first != last && cur != end_it; ++cur, ++first){
1253          *cur = *first;
1254       }
1255 
1256       if (first == last){
1257          //There are no more elements in the sequence, erase remaining
1258          this->priv_destroy_last_n(this->size() - input_sz);
1259       }
1260       else{
1261          //Uninitialized construct at end the remaining range
1262          this->priv_uninitialized_construct_at_end(first, last);
1263       }
1264    }
1265 
1266    //! <b>Effects</b>: Assigns the n copies of val to *this.
1267    //!
1268    //! <b>Throws</b>: If memory allocation throws or
1269    //!   T's copy/move constructor/assignment throws.
1270    //!
1271    //! <b>Complexity</b>: Linear to n.
assign(size_type n,const value_type & val)1272    void assign(size_type n, const value_type& val)
1273    {  this->assign(cvalue_iterator(val, n), cvalue_iterator());   }
1274 
1275    //! <b>Effects</b>: Returns a copy of the internal allocator.
1276    //!
1277    //! <b>Throws</b>: If allocator's copy constructor throws.
1278    //!
1279    //! <b>Complexity</b>: Constant.
get_allocator() const1280    allocator_type get_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
1281    { return this->m_holder.alloc();  }
1282 
1283    //! <b>Effects</b>: Returns a reference to the internal allocator.
1284    //!
1285    //! <b>Throws</b>: Nothing
1286    //!
1287    //! <b>Complexity</b>: Constant.
1288    //!
1289    //! <b>Note</b>: Non-standard extension.
get_stored_allocator()1290    stored_allocator_type &get_stored_allocator() BOOST_NOEXCEPT_OR_NOTHROW
1291    {  return this->m_holder.alloc(); }
1292 
1293    //! <b>Effects</b>: Returns a reference to the internal allocator.
1294    //!
1295    //! <b>Throws</b>: Nothing
1296    //!
1297    //! <b>Complexity</b>: Constant.
1298    //!
1299    //! <b>Note</b>: Non-standard extension.
get_stored_allocator() const1300    const stored_allocator_type &get_stored_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
1301    {  return this->m_holder.alloc(); }
1302 
1303    //////////////////////////////////////////////
1304    //
1305    //                iterators
1306    //
1307    //////////////////////////////////////////////
1308 
1309    //! <b>Effects</b>: Returns an iterator to the first element contained in the vector.
1310    //!
1311    //! <b>Throws</b>: Nothing.
1312    //!
1313    //! <b>Complexity</b>: Constant.
begin()1314    iterator begin() BOOST_NOEXCEPT_OR_NOTHROW
1315    { return iterator(this->m_holder.start()); }
1316 
1317    //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
1318    //!
1319    //! <b>Throws</b>: Nothing.
1320    //!
1321    //! <b>Complexity</b>: Constant.
begin() const1322    const_iterator begin() const BOOST_NOEXCEPT_OR_NOTHROW
1323    { return const_iterator(this->m_holder.start()); }
1324 
1325    //! <b>Effects</b>: Returns an iterator to the end of the vector.
1326    //!
1327    //! <b>Throws</b>: Nothing.
1328    //!
1329    //! <b>Complexity</b>: Constant.
end()1330    iterator end() BOOST_NOEXCEPT_OR_NOTHROW
1331    { return iterator(this->m_holder.start() + this->m_holder.m_size); }
1332 
1333    //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
1334    //!
1335    //! <b>Throws</b>: Nothing.
1336    //!
1337    //! <b>Complexity</b>: Constant.
end() const1338    const_iterator end() const BOOST_NOEXCEPT_OR_NOTHROW
1339    { return this->cend(); }
1340 
1341    //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
1342    //! of the reversed vector.
1343    //!
1344    //! <b>Throws</b>: Nothing.
1345    //!
1346    //! <b>Complexity</b>: Constant.
rbegin()1347    reverse_iterator rbegin() BOOST_NOEXCEPT_OR_NOTHROW
1348    { return reverse_iterator(this->end());      }
1349 
1350    //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
1351    //! of the reversed vector.
1352    //!
1353    //! <b>Throws</b>: Nothing.
1354    //!
1355    //! <b>Complexity</b>: Constant.
rbegin() const1356    const_reverse_iterator rbegin() const BOOST_NOEXCEPT_OR_NOTHROW
1357    { return this->crbegin(); }
1358 
1359    //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
1360    //! of the reversed vector.
1361    //!
1362    //! <b>Throws</b>: Nothing.
1363    //!
1364    //! <b>Complexity</b>: Constant.
rend()1365    reverse_iterator rend() BOOST_NOEXCEPT_OR_NOTHROW
1366    { return reverse_iterator(this->begin());       }
1367 
1368    //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
1369    //! of the reversed vector.
1370    //!
1371    //! <b>Throws</b>: Nothing.
1372    //!
1373    //! <b>Complexity</b>: Constant.
rend() const1374    const_reverse_iterator rend() const BOOST_NOEXCEPT_OR_NOTHROW
1375    { return this->crend(); }
1376 
1377    //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
1378    //!
1379    //! <b>Throws</b>: Nothing.
1380    //!
1381    //! <b>Complexity</b>: Constant.
cbegin() const1382    const_iterator cbegin() const BOOST_NOEXCEPT_OR_NOTHROW
1383    { return const_iterator(this->m_holder.start()); }
1384 
1385    //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
1386    //!
1387    //! <b>Throws</b>: Nothing.
1388    //!
1389    //! <b>Complexity</b>: Constant.
cend() const1390    const_iterator cend() const BOOST_NOEXCEPT_OR_NOTHROW
1391    { return const_iterator(this->m_holder.start() + this->m_holder.m_size); }
1392 
1393    //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
1394    //! of the reversed vector.
1395    //!
1396    //! <b>Throws</b>: Nothing.
1397    //!
1398    //! <b>Complexity</b>: Constant.
crbegin() const1399    const_reverse_iterator crbegin() const BOOST_NOEXCEPT_OR_NOTHROW
1400    { return const_reverse_iterator(this->end());}
1401 
1402    //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
1403    //! of the reversed vector.
1404    //!
1405    //! <b>Throws</b>: Nothing.
1406    //!
1407    //! <b>Complexity</b>: Constant.
crend() const1408    const_reverse_iterator crend() const BOOST_NOEXCEPT_OR_NOTHROW
1409    { return const_reverse_iterator(this->begin()); }
1410 
1411    //////////////////////////////////////////////
1412    //
1413    //                capacity
1414    //
1415    //////////////////////////////////////////////
1416 
1417    //! <b>Effects</b>: Returns true if the vector contains no elements.
1418    //!
1419    //! <b>Throws</b>: Nothing.
1420    //!
1421    //! <b>Complexity</b>: Constant.
empty() const1422    bool empty() const BOOST_NOEXCEPT_OR_NOTHROW
1423    { return !this->m_holder.m_size; }
1424 
1425    //! <b>Effects</b>: Returns the number of the elements contained in the vector.
1426    //!
1427    //! <b>Throws</b>: Nothing.
1428    //!
1429    //! <b>Complexity</b>: Constant.
size() const1430    size_type size() const BOOST_NOEXCEPT_OR_NOTHROW
1431    { return this->m_holder.m_size; }
1432 
1433    //! <b>Effects</b>: Returns the largest possible size of the vector.
1434    //!
1435    //! <b>Throws</b>: Nothing.
1436    //!
1437    //! <b>Complexity</b>: Constant.
max_size() const1438    size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
1439    { return allocator_traits_type::max_size(this->m_holder.alloc()); }
1440 
1441    //! <b>Effects</b>: Inserts or erases elements at the end such that
1442    //!   the size becomes n. New elements are value initialized.
1443    //!
1444    //! <b>Throws</b>: If memory allocation throws, or T's copy/move or value initialization throws.
1445    //!
1446    //! <b>Complexity</b>: Linear to the difference between size() and new_size.
resize(size_type new_size)1447    void resize(size_type new_size)
1448    {  this->priv_resize(new_size, value_init);  }
1449 
1450    //! <b>Effects</b>: Inserts or erases elements at the end such that
1451    //!   the size becomes n. New elements are default initialized.
1452    //!
1453    //! <b>Throws</b>: If memory allocation throws, or T's copy/move or default initialization throws.
1454    //!
1455    //! <b>Complexity</b>: Linear to the difference between size() and new_size.
1456    //!
1457    //! <b>Note</b>: Non-standard extension
resize(size_type new_size,default_init_t)1458    void resize(size_type new_size, default_init_t)
1459    {  this->priv_resize(new_size, default_init);  }
1460 
1461    //! <b>Effects</b>: Inserts or erases elements at the end such that
1462    //!   the size becomes n. New elements are copy constructed from x.
1463    //!
1464    //! <b>Throws</b>: If memory allocation throws, or T's copy/move constructor throws.
1465    //!
1466    //! <b>Complexity</b>: Linear to the difference between size() and new_size.
resize(size_type new_size,const T & x)1467    void resize(size_type new_size, const T& x)
1468    {  this->priv_resize(new_size, x);  }
1469 
1470    //! <b>Effects</b>: Number of elements for which memory has been allocated.
1471    //!   capacity() is always greater than or equal to size().
1472    //!
1473    //! <b>Throws</b>: Nothing.
1474    //!
1475    //! <b>Complexity</b>: Constant.
capacity() const1476    size_type capacity() const BOOST_NOEXCEPT_OR_NOTHROW
1477    { return this->m_holder.capacity(); }
1478 
1479    //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
1480    //!   effect. Otherwise, it is a request for allocation of additional memory.
1481    //!   If the request is successful, then capacity() is greater than or equal to
1482    //!   n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
1483    //!
1484    //! <b>Throws</b>: If memory allocation allocation throws or T's copy/move constructor throws.
reserve(size_type new_cap)1485    void reserve(size_type new_cap)
1486    {
1487       if (this->capacity() < new_cap){
1488          this->priv_reserve_no_capacity(new_cap, alloc_version());
1489       }
1490    }
1491 
1492    //! <b>Effects</b>: Tries to deallocate the excess of memory created
1493    //!   with previous allocations. The size of the vector is unchanged
1494    //!
1495    //! <b>Throws</b>: If memory allocation throws, or T's copy/move constructor throws.
1496    //!
1497    //! <b>Complexity</b>: Linear to size().
shrink_to_fit()1498    void shrink_to_fit()
1499    {  this->priv_shrink_to_fit(alloc_version());   }
1500 
1501    //////////////////////////////////////////////
1502    //
1503    //               element access
1504    //
1505    //////////////////////////////////////////////
1506 
1507    //! <b>Requires</b>: !empty()
1508    //!
1509    //! <b>Effects</b>: Returns a reference to the first
1510    //!   element of the container.
1511    //!
1512    //! <b>Throws</b>: Nothing.
1513    //!
1514    //! <b>Complexity</b>: Constant.
front()1515    reference         front() BOOST_NOEXCEPT_OR_NOTHROW
1516    { return *this->m_holder.start(); }
1517 
1518    //! <b>Requires</b>: !empty()
1519    //!
1520    //! <b>Effects</b>: Returns a const reference to the first
1521    //!   element of the container.
1522    //!
1523    //! <b>Throws</b>: Nothing.
1524    //!
1525    //! <b>Complexity</b>: Constant.
front() const1526    const_reference   front() const BOOST_NOEXCEPT_OR_NOTHROW
1527    { return *this->m_holder.start(); }
1528 
1529    //! <b>Requires</b>: !empty()
1530    //!
1531    //! <b>Effects</b>: Returns a reference to the last
1532    //!   element of the container.
1533    //!
1534    //! <b>Throws</b>: Nothing.
1535    //!
1536    //! <b>Complexity</b>: Constant.
back()1537    reference         back() BOOST_NOEXCEPT_OR_NOTHROW
1538    {
1539       BOOST_ASSERT(this->m_holder.m_size > 0);
1540       return this->m_holder.start()[this->m_holder.m_size - 1];
1541    }
1542 
1543    //! <b>Requires</b>: !empty()
1544    //!
1545    //! <b>Effects</b>: Returns a const reference to the last
1546    //!   element of the container.
1547    //!
1548    //! <b>Throws</b>: Nothing.
1549    //!
1550    //! <b>Complexity</b>: Constant.
back() const1551    const_reference   back()  const BOOST_NOEXCEPT_OR_NOTHROW
1552    {
1553       BOOST_ASSERT(this->m_holder.m_size > 0);
1554       return this->m_holder.start()[this->m_holder.m_size - 1];
1555    }
1556 
1557    //! <b>Requires</b>: size() > n.
1558    //!
1559    //! <b>Effects</b>: Returns a reference to the nth element
1560    //!   from the beginning of the container.
1561    //!
1562    //! <b>Throws</b>: Nothing.
1563    //!
1564    //! <b>Complexity</b>: Constant.
operator [](size_type n)1565    reference operator[](size_type n) BOOST_NOEXCEPT_OR_NOTHROW
1566    {
1567       BOOST_ASSERT(this->m_holder.m_size > n);
1568       return this->m_holder.start()[n];
1569    }
1570 
1571    //! <b>Requires</b>: size() > n.
1572    //!
1573    //! <b>Effects</b>: Returns a const reference to the nth element
1574    //!   from the beginning of the container.
1575    //!
1576    //! <b>Throws</b>: Nothing.
1577    //!
1578    //! <b>Complexity</b>: Constant.
operator [](size_type n) const1579    const_reference operator[](size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
1580    {
1581        return this->m_holder.start()[n];
1582    }
1583 
1584    //! <b>Requires</b>: size() >= n.
1585    //!
1586    //! <b>Effects</b>: Returns an iterator to the nth element
1587    //!   from the beginning of the container. Returns end()
1588    //!   if n == size().
1589    //!
1590    //! <b>Throws</b>: Nothing.
1591    //!
1592    //! <b>Complexity</b>: Constant.
1593    //!
1594    //! <b>Note</b>: Non-standard extension
nth(size_type n)1595    iterator nth(size_type n) BOOST_NOEXCEPT_OR_NOTHROW
1596    {
1597       BOOST_ASSERT(this->m_holder.m_size >= n);
1598       return iterator(this->m_holder.start()+n);
1599    }
1600 
1601    //! <b>Requires</b>: size() >= n.
1602    //!
1603    //! <b>Effects</b>: Returns a const_iterator to the nth element
1604    //!   from the beginning of the container. Returns end()
1605    //!   if n == size().
1606    //!
1607    //! <b>Throws</b>: Nothing.
1608    //!
1609    //! <b>Complexity</b>: Constant.
1610    //!
1611    //! <b>Note</b>: Non-standard extension
nth(size_type n) const1612    const_iterator nth(size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
1613    {
1614       BOOST_ASSERT(this->m_holder.m_size >= n);
1615       return const_iterator(this->m_holder.start()+n);
1616    }
1617 
1618    //! <b>Requires</b>: size() >= n.
1619    //!
1620    //! <b>Effects</b>: Returns an iterator to the nth element
1621    //!   from the beginning of the container. Returns end()
1622    //!   if n == size().
1623    //!
1624    //! <b>Throws</b>: Nothing.
1625    //!
1626    //! <b>Complexity</b>: Constant.
1627    //!
1628    //! <b>Note</b>: Non-standard extension
index_of(iterator p)1629    size_type index_of(iterator p) BOOST_NOEXCEPT_OR_NOTHROW
1630    {  return this->priv_index_of(vector_iterator_get_ptr(p));  }
1631 
1632    //! <b>Requires</b>: begin() <= p <= end().
1633    //!
1634    //! <b>Effects</b>: Returns the index of the element pointed by p
1635    //!   and size() if p == end().
1636    //!
1637    //! <b>Throws</b>: Nothing.
1638    //!
1639    //! <b>Complexity</b>: Constant.
1640    //!
1641    //! <b>Note</b>: Non-standard extension
index_of(const_iterator p) const1642    size_type index_of(const_iterator p) const BOOST_NOEXCEPT_OR_NOTHROW
1643    {  return this->priv_index_of(vector_iterator_get_ptr(p));  }
1644 
1645    //! <b>Requires</b>: size() > n.
1646    //!
1647    //! <b>Effects</b>: Returns a reference to the nth element
1648    //!   from the beginning of the container.
1649    //!
1650    //! <b>Throws</b>: std::range_error if n >= size()
1651    //!
1652    //! <b>Complexity</b>: Constant.
at(size_type n)1653    reference at(size_type n)
1654    { this->priv_check_range(n); return this->m_holder.start()[n]; }
1655 
1656    //! <b>Requires</b>: size() > n.
1657    //!
1658    //! <b>Effects</b>: Returns a const reference to the nth element
1659    //!   from the beginning of the container.
1660    //!
1661    //! <b>Throws</b>: std::range_error if n >= size()
1662    //!
1663    //! <b>Complexity</b>: Constant.
at(size_type n) const1664    const_reference at(size_type n) const
1665    { this->priv_check_range(n); return this->m_holder.start()[n]; }
1666 
1667    //////////////////////////////////////////////
1668    //
1669    //                 data access
1670    //
1671    //////////////////////////////////////////////
1672 
1673    //! <b>Returns</b>: A pointer such that [data(),data() + size()) is a valid range.
1674    //!   For a non-empty vector, data() == &front().
1675    //!
1676    //! <b>Throws</b>: Nothing.
1677    //!
1678    //! <b>Complexity</b>: Constant.
data()1679    T* data() BOOST_NOEXCEPT_OR_NOTHROW
1680    { return container_detail::to_raw_pointer(this->m_holder.start()); }
1681 
1682    //! <b>Returns</b>: A pointer such that [data(),data() + size()) is a valid range.
1683    //!   For a non-empty vector, data() == &front().
1684    //!
1685    //! <b>Throws</b>: Nothing.
1686    //!
1687    //! <b>Complexity</b>: Constant.
data() const1688    const T * data()  const BOOST_NOEXCEPT_OR_NOTHROW
1689    { return container_detail::to_raw_pointer(this->m_holder.start()); }
1690 
1691    //////////////////////////////////////////////
1692    //
1693    //                modifiers
1694    //
1695    //////////////////////////////////////////////
1696 
1697    #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1698    //! <b>Effects</b>: Inserts an object of type T constructed with
1699    //!   std::forward<Args>(args)... in the end of the vector.
1700    //!
1701    //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws or
1702    //!   T's copy/move constructor throws.
1703    //!
1704    //! <b>Complexity</b>: Amortized constant time.
1705    template<class ...Args>
emplace_back(BOOST_FWD_REF (Args)...args)1706    void emplace_back(BOOST_FWD_REF(Args)...args)
1707    {
1708       if (BOOST_LIKELY(this->room_enough())){
1709          //There is more memory, just construct a new object at the end
1710          allocator_traits_type::construct(this->m_holder.alloc(), this->back_raw(), ::boost::forward<Args>(args)...);
1711          ++this->m_holder.m_size;
1712       }
1713       else{
1714          typedef container_detail::insert_emplace_proxy<Allocator, T*, Args...> type;
1715          this->priv_forward_range_insert_no_capacity
1716             (this->back_ptr(), 1, type(::boost::forward<Args>(args)...), alloc_version());
1717       }
1718    }
1719 
1720    //! <b>Effects</b>: Inserts an object of type T constructed with
1721    //!   std::forward<Args>(args)... in the end of the vector.
1722    //!
1723    //! <b>Throws</b>: If the in-place constructor throws.
1724    //!
1725    //! <b>Complexity</b>: Constant time.
1726    //!
1727    //! <b>Note</b>: Non-standard extension.
1728    template<class ...Args>
stable_emplace_back(BOOST_FWD_REF (Args)...args)1729    bool stable_emplace_back(BOOST_FWD_REF(Args)...args)
1730    {
1731       const bool is_room_enough = this->room_enough() || (alloc_version::value == 2 && this->m_holder.try_expand_fwd(1u));
1732       if (BOOST_LIKELY(is_room_enough)){
1733          //There is more memory, just construct a new object at the end
1734          allocator_traits_type::construct(this->m_holder.alloc(), this->back_raw(), ::boost::forward<Args>(args)...);
1735          ++this->m_holder.m_size;
1736       }
1737       return is_room_enough;
1738    }
1739 
1740    //! <b>Requires</b>: position must be a valid iterator of *this.
1741    //!
1742    //! <b>Effects</b>: Inserts an object of type T constructed with
1743    //!   std::forward<Args>(args)... before position
1744    //!
1745    //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws or
1746    //!   T's copy/move constructor/assignment throws.
1747    //!
1748    //! <b>Complexity</b>: If position is end(), amortized constant time
1749    //!   Linear time otherwise.
1750    template<class ...Args>
emplace(const_iterator position,BOOST_FWD_REF (Args)...args)1751    iterator emplace(const_iterator position, BOOST_FWD_REF(Args) ...args)
1752    {
1753       //Just call more general insert(pos, size, value) and return iterator
1754       typedef container_detail::insert_emplace_proxy<Allocator, T*, Args...> type;
1755       return this->priv_forward_range_insert( vector_iterator_get_ptr(position), 1
1756                                             , type(::boost::forward<Args>(args)...));
1757    }
1758 
1759    #else // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
1760 
1761    #define BOOST_CONTAINER_VECTOR_EMPLACE_CODE(N) \
1762    BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
1763    void emplace_back(BOOST_MOVE_UREF##N)\
1764    {\
1765       if (BOOST_LIKELY(this->room_enough())){\
1766          allocator_traits_type::construct (this->m_holder.alloc()\
1767             , this->back_raw() BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
1768          ++this->m_holder.m_size;\
1769       }\
1770       else{\
1771          typedef container_detail::insert_emplace_proxy_arg##N<Allocator, T* BOOST_MOVE_I##N BOOST_MOVE_TARG##N> type;\
1772          this->priv_forward_range_insert_no_capacity\
1773             ( this->back_ptr(), 1, type(BOOST_MOVE_FWD##N), alloc_version());\
1774       }\
1775    }\
1776    \
1777    BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
1778    bool stable_emplace_back(BOOST_MOVE_UREF##N)\
1779    {\
1780       const bool is_room_enough = this->room_enough() || (alloc_version::value == 2 && this->m_holder.try_expand_fwd(1u));\
1781       if (BOOST_LIKELY(is_room_enough)){\
1782          allocator_traits_type::construct (this->m_holder.alloc()\
1783             , this->back_raw() BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
1784          ++this->m_holder.m_size;\
1785       }\
1786       return is_room_enough;\
1787    }\
1788    \
1789    BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
1790    iterator emplace(const_iterator pos BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
1791    {\
1792       typedef container_detail::insert_emplace_proxy_arg##N<Allocator, T* BOOST_MOVE_I##N BOOST_MOVE_TARG##N> type;\
1793       return this->priv_forward_range_insert(vector_iterator_get_ptr(pos), 1, type(BOOST_MOVE_FWD##N));\
1794    }\
1795    //
1796    BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_VECTOR_EMPLACE_CODE)
1797    #undef BOOST_CONTAINER_VECTOR_EMPLACE_CODE
1798 
1799    #endif
1800 
1801    #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1802    //! <b>Effects</b>: Inserts a copy of x at the end of the vector.
1803    //!
1804    //! <b>Throws</b>: If memory allocation throws or
1805    //!   T's copy/move constructor throws.
1806    //!
1807    //! <b>Complexity</b>: Amortized constant time.
1808    void push_back(const T &x);
1809 
1810    //! <b>Effects</b>: Constructs a new element in the end of the vector
1811    //!   and moves the resources of x to this new element.
1812    //!
1813    //! <b>Throws</b>: If memory allocation throws or
1814    //!   T's copy/move constructor throws.
1815    //!
1816    //! <b>Complexity</b>: Amortized constant time.
1817    void push_back(T &&x);
1818    #else
1819    BOOST_MOVE_CONVERSION_AWARE_CATCH(push_back, T, void, priv_push_back)
1820    #endif
1821 
1822    #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1823    //! <b>Requires</b>: position must be a valid iterator of *this.
1824    //!
1825    //! <b>Effects</b>: Insert a copy of x before position.
1826    //!
1827    //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment throws.
1828    //!
1829    //! <b>Complexity</b>: If position is end(), amortized constant time
1830    //!   Linear time otherwise.
1831    iterator insert(const_iterator position, const T &x);
1832 
1833    //! <b>Requires</b>: position must be a valid iterator of *this.
1834    //!
1835    //! <b>Effects</b>: Insert a new element before position with x's resources.
1836    //!
1837    //! <b>Throws</b>: If memory allocation throws.
1838    //!
1839    //! <b>Complexity</b>: If position is end(), amortized constant time
1840    //!   Linear time otherwise.
1841    iterator insert(const_iterator position, T &&x);
1842    #else
BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert,T,iterator,priv_insert,const_iterator,const_iterator)1843    BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, T, iterator, priv_insert, const_iterator, const_iterator)
1844    #endif
1845 
1846    //! <b>Requires</b>: p must be a valid iterator of *this.
1847    //!
1848    //! <b>Effects</b>: Insert n copies of x before pos.
1849    //!
1850    //! <b>Returns</b>: an iterator to the first inserted element or p if n is 0.
1851    //!
1852    //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor throws.
1853    //!
1854    //! <b>Complexity</b>: Linear to n.
1855    iterator insert(const_iterator p, size_type n, const T& x)
1856    {
1857       container_detail::insert_n_copies_proxy<Allocator, T*> proxy(x);
1858       return this->priv_forward_range_insert(vector_iterator_get_ptr(p), n, proxy);
1859    }
1860 
1861    //! <b>Requires</b>: p must be a valid iterator of *this.
1862    //!
1863    //! <b>Effects</b>: Insert a copy of the [first, last) range before pos.
1864    //!
1865    //! <b>Returns</b>: an iterator to the first inserted element or pos if first == last.
1866    //!
1867    //! <b>Throws</b>: If memory allocation throws, T's constructor from a
1868    //!   dereferenced InpIt throws or T's copy/move constructor/assignment throws.
1869    //!
1870    //! <b>Complexity</b>: Linear to boost::container::iterator_distance [first, last).
1871    template <class InIt>
insert(const_iterator pos,InIt first,InIt last,typename container_detail::disable_if_or<void,container_detail::is_convertible<InIt,size_type>,container_detail::is_not_input_iterator<InIt>>::type * =0)1872    iterator insert(const_iterator pos, InIt first, InIt last
1873       #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1874       , typename container_detail::disable_if_or
1875          < void
1876          , container_detail::is_convertible<InIt, size_type>
1877          , container_detail::is_not_input_iterator<InIt>
1878          >::type * = 0
1879       #endif
1880       )
1881    {
1882       const size_type n_pos = pos - this->cbegin();
1883       iterator it(vector_iterator_get_ptr(pos));
1884       for(;first != last; ++first){
1885          it = this->emplace(it, *first);
1886          ++it;
1887       }
1888       return iterator(this->m_holder.start() + n_pos);
1889    }
1890 
1891    #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1892    template <class FwdIt>
insert(const_iterator pos,FwdIt first,FwdIt last,typename container_detail::disable_if_or<void,container_detail::is_convertible<FwdIt,size_type>,container_detail::is_input_iterator<FwdIt>>::type * =0)1893    iterator insert(const_iterator pos, FwdIt first, FwdIt last
1894       , typename container_detail::disable_if_or
1895          < void
1896          , container_detail::is_convertible<FwdIt, size_type>
1897          , container_detail::is_input_iterator<FwdIt>
1898          >::type * = 0
1899       )
1900    {
1901       container_detail::insert_range_proxy<Allocator, FwdIt, T*> proxy(first);
1902       return this->priv_forward_range_insert(vector_iterator_get_ptr(pos), boost::container::iterator_distance(first, last), proxy);
1903    }
1904    #endif
1905 
1906    //! <b>Requires</b>: p must be a valid iterator of *this. num, must
1907    //!   be equal to boost::container::iterator_distance(first, last)
1908    //!
1909    //! <b>Effects</b>: Insert a copy of the [first, last) range before pos.
1910    //!
1911    //! <b>Returns</b>: an iterator to the first inserted element or pos if first == last.
1912    //!
1913    //! <b>Throws</b>: If memory allocation throws, T's constructor from a
1914    //!   dereferenced InpIt throws or T's copy/move constructor/assignment throws.
1915    //!
1916    //! <b>Complexity</b>: Linear to boost::container::iterator_distance [first, last).
1917    //!
1918    //! <b>Note</b>: This function avoids a linear operation to calculate boost::container::iterator_distance[first, last)
1919    //!   for forward and bidirectional iterators, and a one by one insertion for input iterators. This is a
1920    //!   a non-standard extension.
1921    #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
1922    template <class InIt>
insert(const_iterator pos,size_type num,InIt first,InIt last)1923    iterator insert(const_iterator pos, size_type num, InIt first, InIt last)
1924    {
1925       BOOST_ASSERT(container_detail::is_input_iterator<InIt>::value ||
1926                    num == static_cast<size_type>(boost::container::iterator_distance(first, last)));
1927       (void)last;
1928       container_detail::insert_range_proxy<Allocator, InIt, T*> proxy(first);
1929       return this->priv_forward_range_insert(vector_iterator_get_ptr(pos), num, proxy);
1930    }
1931    #endif
1932 
1933    #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
1934    //! <b>Requires</b>: position must be a valid iterator of *this.
1935    //!
1936    //! <b>Effects</b>: Insert a copy of the [il.begin(), il.end()) range before position.
1937    //!
1938    //! <b>Returns</b>: an iterator to the first inserted element or position if first == last.
1939    //!
1940    //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
insert(const_iterator position,std::initializer_list<value_type> il)1941    iterator insert(const_iterator position, std::initializer_list<value_type> il)
1942    {
1943       return this->insert(position, il.begin(), il.end());
1944    }
1945    #endif
1946 
1947    //! <b>Effects</b>: Removes the last element from the vector.
1948    //!
1949    //! <b>Throws</b>: Nothing.
1950    //!
1951    //! <b>Complexity</b>: Constant time.
pop_back()1952    void pop_back() BOOST_NOEXCEPT_OR_NOTHROW
1953    {
1954       //Destroy last element
1955       this->priv_destroy_last();
1956    }
1957 
1958    //! <b>Effects</b>: Erases the element at position pos.
1959    //!
1960    //! <b>Throws</b>: Nothing.
1961    //!
1962    //! <b>Complexity</b>: Linear to the elements between pos and the
1963    //!   last element. Constant if pos is the last element.
erase(const_iterator position)1964    iterator erase(const_iterator position)
1965    {
1966       const pointer p = vector_iterator_get_ptr(position);
1967       T *const pos_ptr = container_detail::to_raw_pointer(p);
1968       T *const beg_ptr = container_detail::to_raw_pointer(this->m_holder.start());
1969       T *const new_end_ptr = ::boost::container::move(pos_ptr + 1, beg_ptr + this->m_holder.m_size, pos_ptr);
1970       //Move elements forward and destroy last
1971       this->priv_destroy_last(pos_ptr == new_end_ptr);
1972       return iterator(p);
1973    }
1974 
1975    //! <b>Effects</b>: Erases the elements pointed by [first, last).
1976    //!
1977    //! <b>Throws</b>: Nothing.
1978    //!
1979    //! <b>Complexity</b>: Linear to the distance between first and last
1980    //!   plus linear to the elements between pos and the last element.
erase(const_iterator first,const_iterator last)1981    iterator erase(const_iterator first, const_iterator last)
1982    {
1983       if (first != last){
1984          T* const old_end_ptr = this->back_raw();
1985          T* const first_ptr = container_detail::to_raw_pointer(vector_iterator_get_ptr(first));
1986          T* const last_ptr  = container_detail::to_raw_pointer(vector_iterator_get_ptr(last));
1987          T* const ptr = container_detail::to_raw_pointer(boost::container::move(last_ptr, old_end_ptr, first_ptr));
1988          this->priv_destroy_last_n(old_end_ptr - ptr);
1989       }
1990       return iterator(vector_iterator_get_ptr(first));
1991    }
1992 
1993    //! <b>Effects</b>: Swaps the contents of *this and x.
1994    //!
1995    //! <b>Throws</b>: Nothing.
1996    //!
1997    //! <b>Complexity</b>: Constant.
swap(vector & x)1998    void swap(vector& x)
1999       BOOST_NOEXCEPT_IF( ((allocator_traits_type::propagate_on_container_swap::value
2000                                     || allocator_traits_type::is_always_equal::value) &&
2001                                     !container_detail::is_version<Allocator, 0>::value))
2002    {
2003       this->priv_swap(x, container_detail::bool_<container_detail::is_version<Allocator, 0>::value>());
2004    }
2005 
2006    #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2007 
2008    //! <b>Effects</b>: Swaps the contents of *this and x.
2009    //!
2010    //! <b>Throws</b>: Nothing.
2011    //!
2012    //! <b>Complexity</b>: Linear
2013    //!
2014    //! <b>Note</b>: Non-standard extension to support static_vector
2015    template<class OtherAllocator>
swap(vector<T,OtherAllocator> & x,typename container_detail::enable_if_and<void,container_detail::is_version<OtherAllocator,0>,container_detail::is_different<OtherAllocator,allocator_type>>::type * =0)2016    void swap(vector<T, OtherAllocator> & x
2017             , typename container_detail::enable_if_and
2018                      < void
2019                      , container_detail::is_version<OtherAllocator, 0>
2020                      , container_detail::is_different<OtherAllocator, allocator_type>
2021                      >::type * = 0
2022             )
2023    {  this->m_holder.deep_swap(x.m_holder); }
2024 
2025    #endif   //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2026 
2027    //! <b>Effects</b>: Erases all the elements of the vector.
2028    //!
2029    //! <b>Throws</b>: Nothing.
2030    //!
2031    //! <b>Complexity</b>: Linear to the number of elements in the container.
clear()2032    void clear() BOOST_NOEXCEPT_OR_NOTHROW
2033    {  this->priv_destroy_all();  }
2034 
2035    //! <b>Effects</b>: Returns true if x and y are equal
2036    //!
2037    //! <b>Complexity</b>: Linear to the number of elements in the container.
operator ==(const vector & x,const vector & y)2038    friend bool operator==(const vector& x, const vector& y)
2039    {  return x.size() == y.size() && ::boost::container::algo_equal(x.begin(), x.end(), y.begin());  }
2040 
2041    //! <b>Effects</b>: Returns true if x and y are unequal
2042    //!
2043    //! <b>Complexity</b>: Linear to the number of elements in the container.
operator !=(const vector & x,const vector & y)2044    friend bool operator!=(const vector& x, const vector& y)
2045    {  return !(x == y); }
2046 
2047    //! <b>Effects</b>: Returns true if x is less than y
2048    //!
2049    //! <b>Complexity</b>: Linear to the number of elements in the container.
operator <(const vector & x,const vector & y)2050    friend bool operator<(const vector& x, const vector& y)
2051    {
2052       const_iterator first1(x.cbegin()), first2(y.cbegin());
2053       const const_iterator last1(x.cend()), last2(y.cend());
2054       for ( ; (first1 != last1) && (first2 != last2); ++first1, ++first2 ) {
2055          if (*first1 < *first2) return true;
2056          if (*first2 < *first1) return false;
2057       }
2058       return (first1 == last1) && (first2 != last2);
2059    }
2060 
2061    //! <b>Effects</b>: Returns true if x is greater than y
2062    //!
2063    //! <b>Complexity</b>: Linear to the number of elements in the container.
operator >(const vector & x,const vector & y)2064    friend bool operator>(const vector& x, const vector& y)
2065    {  return y < x;  }
2066 
2067    //! <b>Effects</b>: Returns true if x is equal or less than y
2068    //!
2069    //! <b>Complexity</b>: Linear to the number of elements in the container.
operator <=(const vector & x,const vector & y)2070    friend bool operator<=(const vector& x, const vector& y)
2071    {  return !(y < x);  }
2072 
2073    //! <b>Effects</b>: Returns true if x is equal or greater than y
2074    //!
2075    //! <b>Complexity</b>: Linear to the number of elements in the container.
operator >=(const vector & x,const vector & y)2076    friend bool operator>=(const vector& x, const vector& y)
2077    {  return !(x < y);  }
2078 
2079    //! <b>Effects</b>: x.swap(y)
2080    //!
2081    //! <b>Complexity</b>: Constant.
swap(vector & x,vector & y)2082    friend void swap(vector& x, vector& y)
2083    {  x.swap(y);  }
2084 
2085    #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
2086    //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
2087    //!   effect. Otherwise, it is a request for allocation of additional memory
2088    //!   (memory expansion) that will not invalidate iterators.
2089    //!   If the request is successful, then capacity() is greater than or equal to
2090    //!   n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
2091    //!
2092    //! <b>Throws</b>: If memory allocation allocation throws or T's copy/move constructor throws.
2093    //!
2094    //! <b>Note</b>: Non-standard extension.
stable_reserve(size_type new_cap)2095    bool stable_reserve(size_type new_cap)
2096    {
2097       const size_type cp = this->capacity();
2098       return cp >= new_cap || (alloc_version::value == 2 && this->m_holder.try_expand_fwd(new_cap - cp));
2099    }
2100 
2101    //Absolutely experimental. This function might change, disappear or simply crash!
2102    template<class BiDirPosConstIt, class BiDirValueIt>
insert_ordered_at(const size_type element_count,BiDirPosConstIt last_position_it,BiDirValueIt last_value_it)2103    void insert_ordered_at(const size_type element_count, BiDirPosConstIt last_position_it, BiDirValueIt last_value_it)
2104    {
2105       typedef container_detail::vector_insert_ordered_cursor<BiDirPosConstIt, BiDirValueIt> inserter_t;
2106       return this->priv_insert_ordered_at(element_count, inserter_t(last_position_it, last_value_it));
2107    }
2108 
2109    template<class BidirIt>
merge(BidirIt first,BidirIt last)2110    void merge(BidirIt first, BidirIt last)
2111    {  this->merge(first, last, value_less());  }
2112 
2113    template<class BidirIt, class Compare>
merge(BidirIt first,BidirIt last,Compare comp)2114    void merge(BidirIt first, BidirIt last, Compare comp)
2115    {  this->priv_merge(container_detail::false_type(), first, last, comp);  }
2116 
2117    template<class BidirIt>
merge_unique(BidirIt first,BidirIt last)2118    void merge_unique(BidirIt first, BidirIt last)
2119    {  this->priv_merge(container_detail::true_type(),  first, last, value_less());  }
2120 
2121    template<class BidirIt, class Compare>
merge_unique(BidirIt first,BidirIt last,Compare comp)2122    void merge_unique(BidirIt first, BidirIt last, Compare comp)
2123    {  this->priv_merge(container_detail::true_type(),  first, last, comp);  }
2124 
2125    private:
2126    template<class PositionValue>
priv_insert_ordered_at(const size_type element_count,PositionValue position_value)2127    void priv_insert_ordered_at(const size_type element_count, PositionValue position_value)
2128    {
2129       const size_type old_size_pos = this->size();
2130       this->reserve(old_size_pos + element_count);
2131       T* const begin_ptr = container_detail::to_raw_pointer(this->m_holder.start());
2132       size_type insertions_left = element_count;
2133       size_type prev_pos = old_size_pos;
2134       size_type old_hole_size = element_count;
2135 
2136       //Exception rollback. If any copy throws before the hole is filled, values
2137       //already inserted/copied at the end of the buffer will be destroyed.
2138       typename value_traits::ArrayDestructor past_hole_values_destroyer
2139          (begin_ptr + old_size_pos + element_count, this->m_holder.alloc(), size_type(0u));
2140       //Loop for each insertion backwards, first moving the elements after the insertion point,
2141       //then inserting the element.
2142       while(insertions_left){
2143          --position_value;
2144          size_type const pos = position_value.get_pos();
2145          BOOST_ASSERT(pos != size_type(-1) && pos <= old_size_pos && pos <= prev_pos);
2146          //If needed shift the range after the insertion point and the previous insertion point.
2147          //Function will take care if the shift crosses the size() boundary, using copy/move
2148          //or uninitialized copy/move if necessary.
2149          size_type new_hole_size = (pos != prev_pos)
2150             ? priv_insert_ordered_at_shift_range(pos, prev_pos, this->size(), insertions_left)
2151             : old_hole_size
2152             ;
2153          if(new_hole_size){
2154             //The hole was reduced by priv_insert_ordered_at_shift_range so expand exception rollback range backwards
2155             past_hole_values_destroyer.increment_size_backwards(prev_pos - pos);
2156             //Insert the new value in the hole
2157             allocator_traits_type::construct(this->m_holder.alloc(), begin_ptr + pos + insertions_left - 1, position_value.get_val());
2158             if(--new_hole_size){
2159                //The hole was reduced by the new insertion by one
2160                past_hole_values_destroyer.increment_size_backwards(size_type(1u));
2161             }
2162             else{
2163                //Hole was just filled, disable exception rollback and change vector size
2164                past_hole_values_destroyer.release();
2165                this->m_holder.m_size += element_count;
2166             }
2167          }
2168          else{
2169             if(old_hole_size){
2170                //Hole was just filled by priv_insert_ordered_at_shift_range, disable exception rollback and change vector size
2171                past_hole_values_destroyer.release();
2172                this->m_holder.m_size += element_count;
2173             }
2174             //Insert the new value in the already constructed range
2175             begin_ptr[pos + insertions_left - 1] = position_value.get_val();
2176          }
2177          --insertions_left;
2178          old_hole_size = new_hole_size;
2179          prev_pos = pos;
2180       }
2181    }
2182 
2183    template<class UniqueBool, class BidirIt, class Compare>
priv_merge(UniqueBool,BidirIt first,BidirIt last,Compare comp)2184    void priv_merge(UniqueBool, BidirIt first, BidirIt last, Compare comp)
2185    {
2186       size_type const n = static_cast<size_type>(boost::container::iterator_distance(first, last));
2187       if(BOOST_LIKELY(n)){
2188          size_type const s = this->size();
2189          if(BOOST_LIKELY(s)){
2190             size_type const c = this->capacity();
2191             size_type const free_c = (c - s);
2192             //Use a new buffer if current one is too small for new elements,
2193             //or there is no room for position indexes
2194             bool new_buffer = false;
2195             if(free_c < n){
2196                new_buffer = true;
2197             }
2198             else if(!UniqueBool::value && free_c >= n){
2199                typedef container_detail::vector_merge_cursor<T, size_type, BidirIt, Compare> inserter_t;
2200                T* const pbeg = container_detail::to_raw_pointer(m_holder.start());
2201                return this->priv_insert_ordered_at(n, inserter_t(pbeg, pbeg + s, last, comp));
2202             }
2203             else if(UniqueBool::value){ //Query for room to store n + 1 indexes (+1 to guarantee potential alignment overhead).
2204                   //No need to destroy them as they are integral types, which simplifies things a lot.
2205                std::size_t const sz_vlt = sizeof(value_type);
2206                std::size_t const sz_szt = sizeof(size_type);
2207                new_buffer = (c-s-n)*sz_vlt/sz_szt < (n+1);
2208             }
2209 
2210             if(new_buffer){
2211                size_type const new_size = s + n;
2212                size_type new_cap = new_size;
2213                pointer p = pointer();
2214                p = this->m_holder.allocation_command(allocate_new, new_size, new_cap, p);
2215                this->priv_merge_in_new_buffer(UniqueBool(), first, n, comp, p, new_cap);
2216             }
2217             else{
2218                //Use trailing memory to store position offsets
2219                uintptr_t const szt_align_mask = container_detail::alignment_of<size_type>::value - 1;
2220                boost::uintptr_t const addr = boost::uintptr_t(container_detail::to_raw_pointer(m_holder.start()) + s + n);
2221                //Align memory before casting to address
2222                size_type *const paddr = reinterpret_cast<size_type *>((addr + szt_align_mask) & ~szt_align_mask);
2223                this->priv_insert_ordered_range(UniqueBool(), n, first, last, paddr, comp);
2224             }
2225          }
2226          else{
2227             this->insert(this->cend(), n, first, last);
2228          }
2229       }
2230    }
2231 
2232    template <class UniqueBool, class BidirIt, class Compare>
priv_insert_ordered_range(UniqueBool,size_type const n,BidirIt first,BidirIt const last,size_type positions[],Compare comp)2233    void priv_insert_ordered_range
2234       (UniqueBool, size_type const n, BidirIt first, BidirIt const last, size_type positions[], Compare comp)
2235    {
2236       //Linear: at most N + M -1 comparisons
2237       //Log: MlogN
2238       //Average
2239       //Linear: N + M - 2
2240       //Log: MlogN
2241       //N+M - 2
2242       //N
2243       //(N+M)/2 < MlogN
2244       //(N/M+1)/2 <= logN
2245       //bool const linear = !s || !n || (s <= n) || ((s+n)/n/2 < logN);
2246       size_type const s = this->size();
2247       size_type remaining = n;
2248       T* const pbeg = container_detail::to_raw_pointer(m_holder.start());
2249       T* const pend = pbeg + s;
2250       T* pcur = pbeg;
2251       size_type *position = positions;
2252       size_type added_in_middle = 0;
2253       if(first != last && pcur != pend){
2254          while(1){
2255             //maintain stability moving external values only if they are strictly less
2256             if(comp(*first, *pcur)) {
2257                *position = static_cast<size_type>(pcur - pbeg);
2258                BOOST_ASSERT((position == positions) || (*(position-1) == size_type(-1)) || (*(position-1) <= *position));
2259                ++position;
2260                ++added_in_middle;
2261                --remaining;
2262                if(++first == last)  break;
2263             }
2264             else if(UniqueBool::value && !comp(*pcur, *first)){
2265                *position = size_type(-1);
2266                ++position;
2267                --remaining;
2268                if(++first == last)  break;
2269             }
2270             else{
2271                if(++pcur == pend)   break;
2272             }
2273          }
2274       }
2275       this->insert_ordered_at(added_in_middle, position, first);
2276       this->insert(this->cend(), remaining, first, last);
2277    }
2278 
2279    template<class UniqueBool, class FwdIt, class Compare>
priv_merge_in_new_buffer(UniqueBool,FwdIt first,size_type n,Compare comp,pointer new_storage,size_type const new_cap)2280    void priv_merge_in_new_buffer
2281       (UniqueBool, FwdIt first, size_type n, Compare comp, pointer new_storage, size_type const new_cap)
2282    {
2283       BOOST_ASSERT((new_cap >= this->size() ) && (new_cap - this->size()) >= n);
2284       allocator_type &a = this->m_holder.alloc();
2285       typename value_traits::ArrayDeallocator new_buffer_deallocator(new_storage, a, new_cap);
2286       typename value_traits::ArrayDestructor  new_values_destroyer(new_storage, a, 0u);
2287       T* pbeg  = container_detail::to_raw_pointer(m_holder.start());
2288       size_type const old_size = this->size();
2289       T* const pend = pbeg + old_size;
2290       T* d_first = container_detail::to_raw_pointer(new_storage);
2291       size_type added = n;
2292       //Merge in new buffer loop
2293       while(1){
2294          if(!n) {
2295             ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), pbeg, pend, d_first);
2296             break;
2297          }
2298          else if(pbeg == pend) {
2299             ::boost::container::uninitialized_move_alloc_n(this->m_holder.alloc(), first, n, d_first);
2300             break;
2301          }
2302          //maintain stability moving external values only if they are strictly less
2303          else if(comp(*first, *pbeg)) {
2304             allocator_traits_type::construct( this->m_holder.alloc(), d_first, ::boost::move(*first) );
2305             new_values_destroyer.increment_size(1u);
2306             ++first;
2307             --n;
2308             ++d_first;
2309          }
2310          else if(UniqueBool::value && !comp(*pbeg, *first)){
2311             ++first;
2312             --n;
2313             --added;
2314          }
2315          else{
2316             allocator_traits_type::construct( this->m_holder.alloc(), d_first, ::boost::move(*pbeg) );
2317             new_values_destroyer.increment_size(1u);
2318             ++pbeg;
2319             ++d_first;
2320          }
2321       }
2322 
2323       //Nothrow operations
2324       pointer const old_p     = this->m_holder.start();
2325       size_type const old_cap = this->m_holder.capacity();
2326       boost::container::destroy_alloc_n(a, container_detail::to_raw_pointer(old_p), old_size);
2327       a.deallocate(old_p, old_cap);
2328       this->m_holder.m_size = old_size + added;
2329       this->m_holder.start(new_storage);
2330       this->m_holder.capacity(new_cap);
2331       new_buffer_deallocator.release();
2332       new_values_destroyer.release();
2333    }
2334 
room_enough() const2335    bool room_enough() const
2336    {  return this->m_holder.m_size < this->m_holder.capacity();   }
2337 
back_ptr() const2338    pointer back_ptr() const
2339    {  return this->m_holder.start() + this->m_holder.m_size;  }
2340 
back_raw() const2341    T* back_raw() const
2342    {  return container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size;  }
2343 
priv_index_of(pointer p) const2344    size_type priv_index_of(pointer p) const
2345    {
2346       BOOST_ASSERT(this->m_holder.start() <= p);
2347       BOOST_ASSERT(p <= (this->m_holder.start()+this->size()));
2348       return static_cast<size_type>(p - this->m_holder.start());
2349    }
2350 
2351    template<class OtherAllocator>
priv_move_assign(BOOST_RV_REF_BEG vector<T,OtherAllocator> BOOST_RV_REF_END x,typename container_detail::enable_if_c<container_detail::is_version<OtherAllocator,0>::value>::type * =0)2352    void priv_move_assign(BOOST_RV_REF_BEG vector<T, OtherAllocator> BOOST_RV_REF_END x
2353       , typename container_detail::enable_if_c
2354          < container_detail::is_version<OtherAllocator, 0>::value >::type * = 0)
2355    {
2356       if(!container_detail::is_same<OtherAllocator, allocator_type>::value &&
2357           this->capacity() < x.size()){
2358          throw_bad_alloc();
2359       }
2360       T* const this_start  = container_detail::to_raw_pointer(m_holder.start());
2361       T* const other_start = container_detail::to_raw_pointer(x.m_holder.start());
2362       const size_type this_sz  = m_holder.m_size;
2363       const size_type other_sz = static_cast<size_type>(x.m_holder.m_size);
2364       boost::container::move_assign_range_alloc_n(this->m_holder.alloc(), other_start, other_sz, this_start, this_sz);
2365       this->m_holder.m_size = other_sz;
2366    }
2367 
2368    template<class OtherAllocator>
priv_move_assign(BOOST_RV_REF_BEG vector<T,OtherAllocator> BOOST_RV_REF_END x,typename container_detail::disable_if_or<void,container_detail::is_version<OtherAllocator,0>,container_detail::is_different<OtherAllocator,allocator_type>>::type * =0)2369    void priv_move_assign(BOOST_RV_REF_BEG vector<T, OtherAllocator> BOOST_RV_REF_END x
2370       , typename container_detail::disable_if_or
2371          < void
2372          , container_detail::is_version<OtherAllocator, 0>
2373          , container_detail::is_different<OtherAllocator, allocator_type>
2374          >::type * = 0)
2375    {
2376       //for move constructor, no aliasing (&x != this) is assummed.
2377       BOOST_ASSERT(this != &x);
2378       allocator_type &this_alloc = this->m_holder.alloc();
2379       allocator_type &x_alloc    = x.m_holder.alloc();
2380       const bool propagate_alloc = allocator_traits_type::propagate_on_container_move_assignment::value;
2381 
2382       const bool is_propagable_from_x = is_propagable_from(x_alloc, x.m_holder.start(), this_alloc, propagate_alloc);
2383       const bool is_propagable_from_t = is_propagable_from(this_alloc, m_holder.start(), x_alloc,   propagate_alloc);
2384       const bool are_both_propagable  = is_propagable_from_x && is_propagable_from_t;
2385 
2386       //Resources can be transferred if both allocators are
2387       //going to be equal after this function (either propagated or already equal)
2388       if(are_both_propagable){
2389          //Destroy objects but retain memory in case x reuses it in the future
2390          this->clear();
2391          this->m_holder.swap_resources(x.m_holder);
2392       }
2393       else if(is_propagable_from_x){
2394          this->clear();
2395          this->m_holder.alloc().deallocate(this->m_holder.m_start, this->m_holder.m_capacity);
2396          this->m_holder.steal_resources(x.m_holder);
2397       }
2398       //Else do a one by one move
2399       else{
2400          this->assign( boost::make_move_iterator(container_detail::iterator_to_raw_pointer(x.begin()))
2401                      , boost::make_move_iterator(container_detail::iterator_to_raw_pointer(x.end()  ))
2402                      );
2403       }
2404       //Move allocator if needed
2405       container_detail::move_alloc(this_alloc, x_alloc, container_detail::bool_<propagate_alloc>());
2406    }
2407 
2408    template<class OtherAllocator>
priv_copy_assign(const vector<T,OtherAllocator> & x,typename container_detail::enable_if_c<container_detail::is_version<OtherAllocator,0>::value>::type * =0)2409    void priv_copy_assign(const vector<T, OtherAllocator> &x
2410       , typename container_detail::enable_if_c
2411          < container_detail::is_version<OtherAllocator, 0>::value >::type * = 0)
2412    {
2413       if(!container_detail::is_same<OtherAllocator, allocator_type>::value &&
2414          this->capacity() < x.size()){
2415          throw_bad_alloc();
2416       }
2417       T* const this_start  = container_detail::to_raw_pointer(m_holder.start());
2418       T* const other_start = container_detail::to_raw_pointer(x.m_holder.start());
2419       const size_type this_sz  = m_holder.m_size;
2420       const size_type other_sz = static_cast<size_type>(x.m_holder.m_size);
2421       boost::container::copy_assign_range_alloc_n(this->m_holder.alloc(), other_start, other_sz, this_start, this_sz);
2422       this->m_holder.m_size = other_sz;
2423    }
2424 
2425    template<class OtherAllocator>
2426    typename container_detail::disable_if_or
2427       < void
2428       , container_detail::is_version<OtherAllocator, 0>
2429       , container_detail::is_different<OtherAllocator, allocator_type>
2430       >::type
priv_copy_assign(const vector<T,OtherAllocator> & x)2431       priv_copy_assign(const vector<T, OtherAllocator> &x)
2432    {
2433       allocator_type &this_alloc     = this->m_holder.alloc();
2434       const allocator_type &x_alloc  = x.m_holder.alloc();
2435       container_detail::bool_<allocator_traits_type::
2436          propagate_on_container_copy_assignment::value> flag;
2437       if(flag && this_alloc != x_alloc){
2438          this->clear();
2439          this->shrink_to_fit();
2440       }
2441       container_detail::assign_alloc(this_alloc, x_alloc, flag);
2442       this->assign( container_detail::to_raw_pointer(x.m_holder.start())
2443                   , container_detail::to_raw_pointer(x.m_holder.start() + x.m_holder.m_size));
2444    }
2445 
2446    template<class Vector>  //Template it to avoid it in explicit instantiations
priv_swap(Vector & x,container_detail::true_type)2447    void priv_swap(Vector &x, container_detail::true_type)   //version_0
2448    {  this->m_holder.deep_swap(x.m_holder);  }
2449 
2450    template<class Vector>  //Template it to avoid it in explicit instantiations
priv_swap(Vector & x,container_detail::false_type)2451    void priv_swap(Vector &x, container_detail::false_type)  //version_N
2452    {
2453       const bool propagate_alloc = allocator_traits_type::propagate_on_container_swap::value;
2454       if(are_swap_propagable( this->get_stored_allocator(), this->m_holder.start()
2455                             , x.get_stored_allocator(), this->m_holder.start(), propagate_alloc)){
2456          //Just swap internals
2457          this->m_holder.swap_resources(x.m_holder);
2458       }
2459       else{
2460          //Else swap element by element...
2461          bool const t_smaller = this->size() < x.size();
2462          vector &sml = t_smaller ? *this : x;
2463          vector &big = t_smaller ? x : *this;
2464 
2465          size_type const common_elements = sml.size();
2466          for(size_type i = 0; i != common_elements; ++i){
2467             boost::adl_move_swap(sml[i], big[i]);
2468          }
2469          //... and move-insert the remaining range
2470          sml.insert( sml.cend()
2471                    , boost::make_move_iterator(container_detail::iterator_to_raw_pointer(big.nth(common_elements)))
2472                    , boost::make_move_iterator(container_detail::iterator_to_raw_pointer(big.end()))
2473                    );
2474       }
2475       //And now swap the allocator
2476       container_detail::swap_alloc(this->m_holder.alloc(), x.m_holder.alloc(), container_detail::bool_<propagate_alloc>());
2477    }
2478 
priv_reserve_no_capacity(size_type,version_0)2479    void priv_reserve_no_capacity(size_type, version_0)
2480    {  throw_bad_alloc();  }
2481 
priv_dummy_empty_proxy()2482    container_detail::insert_range_proxy<Allocator, boost::move_iterator<T*>, T*> priv_dummy_empty_proxy()
2483    {
2484       return container_detail::insert_range_proxy<Allocator, boost::move_iterator<T*>, T*>
2485          (::boost::make_move_iterator((T *)0));
2486    }
2487 
priv_reserve_no_capacity(size_type new_cap,version_1)2488    void priv_reserve_no_capacity(size_type new_cap, version_1)
2489    {
2490       //There is not enough memory, allocate a new buffer
2491       //Pass the hint so that allocators can take advantage of this.
2492       pointer const p = allocator_traits_type::allocate(this->m_holder.alloc(), new_cap, this->m_holder.m_start);
2493       //We will reuse insert code, so create a dummy input iterator
2494       this->priv_forward_range_insert_new_allocation
2495          ( container_detail::to_raw_pointer(p), new_cap, this->back_raw(), 0, this->priv_dummy_empty_proxy());
2496    }
2497 
priv_reserve_no_capacity(size_type new_cap,version_2)2498    void priv_reserve_no_capacity(size_type new_cap, version_2)
2499    {
2500       //There is not enough memory, allocate a new
2501       //buffer or expand the old one.
2502       bool same_buffer_start;
2503       size_type real_cap = 0;
2504       pointer reuse = 0;
2505       pointer const ret(this->m_holder.allocation_command(allocate_new | expand_fwd | expand_bwd, new_cap, real_cap = new_cap, reuse));
2506 
2507       //Check for forward expansion
2508       same_buffer_start = reuse && this->m_holder.start() == ret;
2509       if(same_buffer_start){
2510          #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2511          ++this->num_expand_fwd;
2512          #endif
2513          this->m_holder.capacity(real_cap);
2514       }
2515       else{ //If there is no forward expansion, move objects, we will reuse insertion code
2516          T * const new_mem = container_detail::to_raw_pointer(ret);
2517          T * const ins_pos = this->back_raw();
2518          if(reuse){   //Backwards (and possibly forward) expansion
2519             #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2520             ++this->num_expand_bwd;
2521             #endif
2522             this->priv_forward_range_insert_expand_backwards
2523                ( new_mem , real_cap, ins_pos, 0, this->priv_dummy_empty_proxy());
2524          }
2525          else{ //New buffer
2526             #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2527             ++this->num_alloc;
2528             #endif
2529             this->priv_forward_range_insert_new_allocation
2530                ( new_mem, real_cap, ins_pos, 0, this->priv_dummy_empty_proxy());
2531          }
2532       }
2533    }
2534 
priv_destroy_last(const bool moved=false)2535    void priv_destroy_last(const bool moved = false) BOOST_NOEXCEPT_OR_NOTHROW
2536    {
2537       (void)moved;
2538       if(!(value_traits::trivial_dctr || (value_traits::trivial_dctr_after_move && moved))){
2539          value_type* const p = this->back_raw() - 1;
2540          allocator_traits_type::destroy(this->get_stored_allocator(), p);
2541       }
2542       --this->m_holder.m_size;
2543    }
2544 
priv_destroy_last_n(const size_type n)2545    void priv_destroy_last_n(const size_type n) BOOST_NOEXCEPT_OR_NOTHROW
2546    {
2547       BOOST_ASSERT(n <= this->m_holder.m_size);
2548       if(!value_traits::trivial_dctr){
2549          T* const destroy_pos = container_detail::to_raw_pointer(this->m_holder.start()) + (this->m_holder.m_size-n);
2550          boost::container::destroy_alloc_n(this->get_stored_allocator(), destroy_pos, n);
2551       }
2552       this->m_holder.m_size -= n;
2553    }
2554 
2555    template<class InpIt>
priv_uninitialized_construct_at_end(InpIt first,InpIt last)2556    void priv_uninitialized_construct_at_end(InpIt first, InpIt last)
2557    {
2558       T* const old_end_pos = this->back_raw();
2559       T* const new_end_pos = boost::container::uninitialized_copy_alloc(this->m_holder.alloc(), first, last, old_end_pos);
2560       this->m_holder.m_size += new_end_pos - old_end_pos;
2561    }
2562 
priv_destroy_all()2563    void priv_destroy_all() BOOST_NOEXCEPT_OR_NOTHROW
2564    {
2565       boost::container::destroy_alloc_n
2566          (this->get_stored_allocator(), container_detail::to_raw_pointer(this->m_holder.start()), this->m_holder.m_size);
2567       this->m_holder.m_size = 0;
2568    }
2569 
2570    template<class U>
priv_insert(const const_iterator & p,BOOST_FWD_REF (U)x)2571    iterator priv_insert(const const_iterator &p, BOOST_FWD_REF(U) x)
2572    {
2573       return this->priv_forward_range_insert
2574          ( vector_iterator_get_ptr(p), 1, container_detail::get_insert_value_proxy<T*, Allocator>(::boost::forward<U>(x)));
2575    }
2576 
priv_single_insert_proxy(const T & x)2577    container_detail::insert_copy_proxy<Allocator, T*> priv_single_insert_proxy(const T &x)
2578    {  return container_detail::insert_copy_proxy<Allocator, T*> (x);  }
2579 
priv_single_insert_proxy(BOOST_RV_REF (T)x)2580    container_detail::insert_move_proxy<Allocator, T*> priv_single_insert_proxy(BOOST_RV_REF(T) x)
2581    {  return container_detail::insert_move_proxy<Allocator, T*> (x);  }
2582 
2583    template <class U>
priv_push_back(BOOST_FWD_REF (U)u)2584    void priv_push_back(BOOST_FWD_REF(U) u)
2585    {
2586       if (BOOST_LIKELY(this->room_enough())){
2587          //There is more memory, just construct a new object at the end
2588          allocator_traits_type::construct
2589             ( this->m_holder.alloc()
2590             , container_detail::to_raw_pointer(this->m_holder.start() + this->m_holder.m_size)
2591             , ::boost::forward<U>(u) );
2592          ++this->m_holder.m_size;
2593       }
2594       else{
2595          this->priv_forward_range_insert_no_capacity
2596             ( this->back_ptr(), 1
2597             , this->priv_single_insert_proxy(::boost::forward<U>(u)), alloc_version());
2598       }
2599    }
2600 
priv_resize_proxy(const T & x)2601    container_detail::insert_n_copies_proxy<Allocator, T*> priv_resize_proxy(const T &x)
2602    {  return container_detail::insert_n_copies_proxy<Allocator, T*>(x);   }
2603 
priv_resize_proxy(default_init_t)2604    container_detail::insert_default_initialized_n_proxy<Allocator, T*> priv_resize_proxy(default_init_t)
2605    {  return container_detail::insert_default_initialized_n_proxy<Allocator, T*>();  }
2606 
priv_resize_proxy(value_init_t)2607    container_detail::insert_value_initialized_n_proxy<Allocator, T*> priv_resize_proxy(value_init_t)
2608    {  return container_detail::insert_value_initialized_n_proxy<Allocator, T*>(); }
2609 
2610    template <class U>
priv_resize(size_type new_size,const U & u)2611    void priv_resize(size_type new_size, const U& u)
2612    {
2613       const size_type sz = this->size();
2614       if (new_size < sz){
2615          //Destroy last elements
2616          this->priv_destroy_last_n(sz - new_size);
2617       }
2618       else{
2619          const size_type n = new_size - this->size();
2620          this->priv_forward_range_insert_at_end(n, this->priv_resize_proxy(u), alloc_version());
2621       }
2622    }
2623 
priv_shrink_to_fit(version_0)2624    void priv_shrink_to_fit(version_0) BOOST_NOEXCEPT_OR_NOTHROW
2625    {}
2626 
priv_shrink_to_fit(version_1)2627    void priv_shrink_to_fit(version_1)
2628    {
2629       const size_type cp = this->m_holder.capacity();
2630       if(cp){
2631          const size_type sz = this->size();
2632          if(!sz){
2633             this->m_holder.alloc().deallocate(this->m_holder.m_start, cp);
2634             this->m_holder.m_start     = pointer();
2635             this->m_holder.m_capacity  = 0;
2636          }
2637          else if(sz < cp){
2638             //Allocate a new buffer.
2639             //Pass the hint so that allocators can take advantage of this.
2640             pointer const p = allocator_traits_type::allocate(this->m_holder.alloc(), sz, this->m_holder.m_start);
2641 
2642             //We will reuse insert code, so create a dummy input iterator
2643             #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2644             ++this->num_alloc;
2645             #endif
2646             this->priv_forward_range_insert_new_allocation
2647                ( container_detail::to_raw_pointer(p), sz
2648                , container_detail::to_raw_pointer(this->m_holder.start())
2649                , 0, this->priv_dummy_empty_proxy());
2650          }
2651       }
2652    }
2653 
priv_shrink_to_fit(version_2)2654    void priv_shrink_to_fit(version_2) BOOST_NOEXCEPT_OR_NOTHROW
2655    {
2656       const size_type cp = this->m_holder.capacity();
2657       if(cp){
2658          const size_type sz = this->size();
2659          if(!sz){
2660             this->m_holder.alloc().deallocate(this->m_holder.m_start, cp);
2661             this->m_holder.m_start     = pointer();
2662             this->m_holder.m_capacity  = 0;
2663          }
2664          else{
2665             size_type received_size = sz;
2666             pointer reuse(this->m_holder.start());
2667             if(this->m_holder.allocation_command
2668                (shrink_in_place | nothrow_allocation, cp, received_size, reuse)){
2669                this->m_holder.capacity(received_size);
2670                #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2671                ++this->num_shrink;
2672                #endif
2673             }
2674          }
2675       }
2676    }
2677 
2678    template <class InsertionProxy>
priv_forward_range_insert_no_capacity(const pointer & pos,const size_type,const InsertionProxy,version_0)2679    iterator priv_forward_range_insert_no_capacity
2680       (const pointer &pos, const size_type, const InsertionProxy , version_0)
2681    {
2682       throw_bad_alloc();
2683       return iterator(pos);
2684    }
2685 
2686    template <class InsertionProxy>
priv_forward_range_insert_no_capacity(const pointer & pos,const size_type n,const InsertionProxy insert_range_proxy,version_1)2687    iterator priv_forward_range_insert_no_capacity
2688       (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, version_1)
2689    {
2690       //Check if we have enough memory or try to expand current memory
2691       const size_type n_pos = pos - this->m_holder.start();
2692       T *const raw_pos = container_detail::to_raw_pointer(pos);
2693 
2694       const size_type new_cap = this->m_holder.next_capacity(n);
2695       //Pass the hint so that allocators can take advantage of this.
2696       T * const new_buf = container_detail::to_raw_pointer(allocator_traits_type::allocate(this->m_holder.alloc(), new_cap, this->m_holder.m_start));
2697       #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2698       ++this->num_alloc;
2699       #endif
2700       this->priv_forward_range_insert_new_allocation
2701          ( new_buf, new_cap, raw_pos, n, insert_range_proxy);
2702       return iterator(this->m_holder.start() + n_pos);
2703    }
2704 
2705    template <class InsertionProxy>
priv_forward_range_insert_no_capacity(const pointer & pos,const size_type n,const InsertionProxy insert_range_proxy,version_2)2706    iterator priv_forward_range_insert_no_capacity
2707       (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, version_2)
2708    {
2709       //Check if we have enough memory or try to expand current memory
2710       T *const raw_pos = container_detail::to_raw_pointer(pos);
2711       const size_type n_pos = raw_pos - container_detail::to_raw_pointer(this->m_holder.start());
2712 
2713       //There is not enough memory, allocate a new
2714       //buffer or expand the old one.
2715       size_type real_cap = this->m_holder.next_capacity(n);
2716       pointer reuse(this->m_holder.start());
2717       pointer const ret (this->m_holder.allocation_command
2718          (allocate_new | expand_fwd | expand_bwd, this->m_holder.m_size + n, real_cap, reuse));
2719 
2720       //Buffer reallocated
2721       if(reuse){
2722          //Forward expansion, delay insertion
2723          if(this->m_holder.start() == ret){
2724             #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2725             ++this->num_expand_fwd;
2726             #endif
2727             this->m_holder.capacity(real_cap);
2728             //Expand forward
2729             this->priv_forward_range_insert_expand_forward(raw_pos, n, insert_range_proxy);
2730          }
2731          //Backwards (and possibly forward) expansion
2732          else{
2733             #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2734             ++this->num_expand_bwd;
2735             #endif
2736             this->priv_forward_range_insert_expand_backwards
2737                (container_detail::to_raw_pointer(ret), real_cap, raw_pos, n, insert_range_proxy);
2738          }
2739       }
2740       //New buffer
2741       else{
2742          #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
2743          ++this->num_alloc;
2744          #endif
2745          this->priv_forward_range_insert_new_allocation
2746             ( container_detail::to_raw_pointer(ret), real_cap, raw_pos, n, insert_range_proxy);
2747       }
2748 
2749       return iterator(this->m_holder.start() + n_pos);
2750    }
2751 
2752    template <class InsertionProxy>
priv_forward_range_insert(const pointer & pos,const size_type n,const InsertionProxy insert_range_proxy)2753    iterator priv_forward_range_insert
2754       (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy)
2755    {
2756       BOOST_ASSERT(this->m_holder.capacity() >= this->m_holder.m_size);
2757       //Check if we have enough memory or try to expand current memory
2758       const size_type remaining = this->m_holder.capacity() - this->m_holder.m_size;
2759 
2760       bool same_buffer_start = n <= remaining;
2761       if (!same_buffer_start){
2762          return priv_forward_range_insert_no_capacity(pos, n, insert_range_proxy, alloc_version());
2763       }
2764       else{
2765          //Expand forward
2766          T *const raw_pos = container_detail::to_raw_pointer(pos);
2767          const size_type n_pos = raw_pos - container_detail::to_raw_pointer(this->m_holder.start());
2768          this->priv_forward_range_insert_expand_forward(raw_pos, n, insert_range_proxy);
2769          return iterator(this->m_holder.start() + n_pos);
2770       }
2771    }
2772 
2773    template <class InsertionProxy>
priv_forward_range_insert_at_end(const size_type n,const InsertionProxy insert_range_proxy,version_0)2774    iterator priv_forward_range_insert_at_end
2775       (const size_type n, const InsertionProxy insert_range_proxy, version_0)
2776    {
2777       //Check if we have enough memory or try to expand current memory
2778       const size_type remaining = this->m_holder.capacity() - this->m_holder.m_size;
2779 
2780       if (n > remaining){
2781          //This will trigger an error
2782          throw_bad_alloc();
2783       }
2784       this->priv_forward_range_insert_at_end_expand_forward(n, insert_range_proxy);
2785       return this->end();
2786    }
2787 
2788    template <class InsertionProxy, class AllocVersion>
priv_forward_range_insert_at_end(const size_type n,const InsertionProxy insert_range_proxy,AllocVersion)2789    iterator priv_forward_range_insert_at_end
2790       (const size_type n, const InsertionProxy insert_range_proxy, AllocVersion)
2791    {
2792       return this->priv_forward_range_insert(this->back_ptr(), n, insert_range_proxy);
2793    }
2794 
2795    //Takes the range pointed by [first_pos, last_pos) and shifts it to the right
2796    //by 'shift_count'. 'limit_pos' marks the end of constructed elements.
2797    //
2798    //Precondition: first_pos <= last_pos <= limit_pos
2799    //
2800    //The shift operation might cross limit_pos so elements to moved beyond limit_pos
2801    //are uninitialized_moved with an allocator. Other elements are moved.
2802    //
2803    //The shift operation might left uninitialized elements after limit_pos
2804    //and the number of uninitialized elements is returned by the function.
2805    //
2806    //Old situation:
2807    //       first_pos   last_pos         old_limit
2808    //             |       |                  |
2809    // ____________V_______V__________________V_____________
2810    //|   prefix   | range |     suffix       |raw_mem      ~
2811    //|____________|_______|__________________|_____________~
2812    //
2813    //New situation in Case A (hole_size == 0):
2814    // range is moved through move assignments
2815    //
2816    //       first_pos   last_pos         limit_pos
2817    //             |       |                  |
2818    // ____________V_______V__________________V_____________
2819    //|   prefix'  |       |  | range |suffix'|raw_mem      ~
2820    //|________________+______|___^___|_______|_____________~
2821    //                 |          |
2822    //                 |_>_>_>_>_>^
2823    //
2824    //
2825    //New situation in Case B (hole_size >= 0):
2826    // range is moved through uninitialized moves
2827    //
2828    //       first_pos   last_pos         limit_pos
2829    //             |       |                  |
2830    // ____________V_______V__________________V________________
2831    //|    prefix' |       |                  | [hole] | range |
2832    //|_______________________________________|________|___^___|
2833    //                 |                                   |
2834    //                 |_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_^
2835    //
2836    //New situation in Case C (hole_size == 0):
2837    // range is moved through move assignments and uninitialized moves
2838    //
2839    //       first_pos   last_pos         limit_pos
2840    //             |       |                  |
2841    // ____________V_______V__________________V___
2842    //|   prefix'  |       |              | range |
2843    //|___________________________________|___^___|
2844    //                 |                      |
2845    //                 |_>_>_>_>_>_>_>_>_>_>_>^
priv_insert_ordered_at_shift_range(size_type first_pos,size_type last_pos,size_type limit_pos,size_type shift_count)2846    size_type priv_insert_ordered_at_shift_range
2847       (size_type first_pos, size_type last_pos, size_type limit_pos, size_type shift_count)
2848    {
2849       BOOST_ASSERT(first_pos <= last_pos);
2850       BOOST_ASSERT(last_pos <= limit_pos);
2851       //
2852       T* const begin_ptr = container_detail::to_raw_pointer(this->m_holder.start());
2853       T* const first_ptr = begin_ptr + first_pos;
2854       T* const last_ptr  = begin_ptr + last_pos;
2855 
2856       size_type hole_size = 0;
2857       //Case A:
2858       if((last_pos + shift_count) <= limit_pos){
2859          //All move assigned
2860          boost::container::move_backward(first_ptr, last_ptr, last_ptr + shift_count);
2861       }
2862       //Case B:
2863       else if((first_pos + shift_count) >= limit_pos){
2864          //All uninitialized_moved
2865          ::boost::container::uninitialized_move_alloc
2866             (this->m_holder.alloc(), first_ptr, last_ptr, first_ptr + shift_count);
2867          hole_size = first_pos + shift_count - limit_pos;
2868       }
2869       //Case C:
2870       else{
2871          //Some uninitialized_moved
2872          T* const limit_ptr    = begin_ptr + limit_pos;
2873          T* const boundary_ptr = limit_ptr - shift_count;
2874          ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), boundary_ptr, last_ptr, limit_ptr);
2875          //The rest is move assigned
2876          boost::container::move_backward(first_ptr, boundary_ptr, limit_ptr);
2877       }
2878       return hole_size;
2879    }
2880 
2881    private:
2882    template <class InsertionProxy>
priv_forward_range_insert_at_end_expand_forward(const size_type n,InsertionProxy insert_range_proxy)2883    void priv_forward_range_insert_at_end_expand_forward(const size_type n, InsertionProxy insert_range_proxy)
2884    {
2885       T* const old_finish = this->back_raw();
2886       insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n);
2887       this->m_holder.m_size += n;
2888    }
2889 
2890    template <class InsertionProxy>
priv_forward_range_insert_expand_forward(T * const pos,const size_type n,InsertionProxy insert_range_proxy)2891    void priv_forward_range_insert_expand_forward(T* const pos, const size_type n, InsertionProxy insert_range_proxy)
2892    {
2893       //n can't be 0, because there is nothing to do in that case
2894       if(BOOST_UNLIKELY(!n)) return;
2895       //There is enough memory
2896       T* const old_finish = this->back_raw();
2897       const size_type elems_after = old_finish - pos;
2898 
2899       if (!elems_after){
2900          insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n);
2901          this->m_holder.m_size += n;
2902       }
2903       else if (elems_after >= n){
2904          //New elements can be just copied.
2905          //Move to uninitialized memory last objects
2906          ::boost::container::uninitialized_move_alloc
2907             (this->m_holder.alloc(), old_finish - n, old_finish, old_finish);
2908          this->m_holder.m_size += n;
2909          //Copy previous to last objects to the initialized end
2910          boost::container::move_backward(pos, old_finish - n, old_finish);
2911          //Insert new objects in the pos
2912          insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, n);
2913       }
2914       else {
2915          //The new elements don't fit in the [pos, end()) range.
2916 
2917          //Copy old [pos, end()) elements to the uninitialized memory (a gap is created)
2918          ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), pos, old_finish, pos + n);
2919          BOOST_TRY{
2920             //Copy first new elements in pos (gap is still there)
2921             insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, elems_after);
2922             //Copy to the beginning of the unallocated zone the last new elements (the gap is closed).
2923             insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n - elems_after);
2924             this->m_holder.m_size += n;
2925          }
2926          BOOST_CATCH(...){
2927             boost::container::destroy_alloc_n(this->get_stored_allocator(), pos + n, elems_after);
2928             BOOST_RETHROW
2929          }
2930          BOOST_CATCH_END
2931       }
2932    }
2933 
2934    template <class InsertionProxy>
priv_forward_range_insert_new_allocation(T * const new_start,size_type new_cap,T * const pos,const size_type n,InsertionProxy insert_range_proxy)2935    void priv_forward_range_insert_new_allocation
2936       (T* const new_start, size_type new_cap, T* const pos, const size_type n, InsertionProxy insert_range_proxy)
2937    {
2938       //n can be zero, if we want to reallocate!
2939       T *new_finish = new_start;
2940       T *old_finish;
2941       //Anti-exception rollbacks
2942       typename value_traits::ArrayDeallocator new_buffer_deallocator(new_start, this->m_holder.alloc(), new_cap);
2943       typename value_traits::ArrayDestructor  new_values_destroyer(new_start, this->m_holder.alloc(), 0u);
2944 
2945       //Initialize with [begin(), pos) old buffer
2946       //the start of the new buffer
2947       T * const old_buffer = container_detail::to_raw_pointer(this->m_holder.start());
2948       if(old_buffer){
2949          new_finish = ::boost::container::uninitialized_move_alloc
2950             (this->m_holder.alloc(), container_detail::to_raw_pointer(this->m_holder.start()), pos, old_finish = new_finish);
2951          new_values_destroyer.increment_size(new_finish - old_finish);
2952       }
2953       //Initialize new objects, starting from previous point
2954       old_finish = new_finish;
2955       insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n);
2956       new_finish += n;
2957       new_values_destroyer.increment_size(new_finish - old_finish);
2958       //Initialize from the rest of the old buffer,
2959       //starting from previous point
2960       if(old_buffer){
2961          new_finish = ::boost::container::uninitialized_move_alloc
2962             (this->m_holder.alloc(), pos, old_buffer + this->m_holder.m_size, new_finish);
2963          //Destroy and deallocate old elements
2964          //If there is allocated memory, destroy and deallocate
2965          if(!value_traits::trivial_dctr_after_move)
2966             boost::container::destroy_alloc_n(this->get_stored_allocator(), old_buffer, this->m_holder.m_size);
2967          this->m_holder.alloc().deallocate(this->m_holder.start(), this->m_holder.capacity());
2968       }
2969       this->m_holder.start(new_start);
2970       this->m_holder.m_size = new_finish - new_start;
2971       this->m_holder.capacity(new_cap);
2972       //All construction successful, disable rollbacks
2973       new_values_destroyer.release();
2974       new_buffer_deallocator.release();
2975    }
2976 
2977    template <class InsertionProxy>
priv_forward_range_insert_expand_backwards(T * const new_start,const size_type new_capacity,T * const pos,const size_type n,InsertionProxy insert_range_proxy)2978    void priv_forward_range_insert_expand_backwards
2979          (T* const new_start, const size_type new_capacity,
2980           T* const pos, const size_type n, InsertionProxy insert_range_proxy)
2981    {
2982       //n can be zero to just expand capacity
2983       //Backup old data
2984       T* const old_start  = container_detail::to_raw_pointer(this->m_holder.start());
2985       const size_type old_size = this->m_holder.m_size;
2986       T* const old_finish = old_start + old_size;
2987 
2988       //We can have 8 possibilities:
2989       const size_type elemsbefore = static_cast<size_type>(pos - old_start);
2990       const size_type s_before    = static_cast<size_type>(old_start - new_start);
2991       const size_type before_plus_new = elemsbefore + n;
2992 
2993       //Update the vector buffer information to a safe state
2994       this->m_holder.start(new_start);
2995       this->m_holder.capacity(new_capacity);
2996       this->m_holder.m_size = 0;
2997 
2998       //If anything goes wrong, this object will destroy
2999       //all the old objects to fulfill previous vector state
3000       typename value_traits::ArrayDestructor old_values_destroyer(old_start, this->m_holder.alloc(), old_size);
3001       //Check if s_before is big enough to hold the beginning of old data + new data
3002       if(s_before >= before_plus_new){
3003          //Copy first old values before pos, after that the new objects
3004          T *const new_elem_pos =
3005             ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), old_start, pos, new_start);
3006          this->m_holder.m_size = elemsbefore;
3007          insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), new_elem_pos, n);
3008          this->m_holder.m_size = before_plus_new;
3009          const size_type new_size = old_size + n;
3010          //Check if s_before is so big that even copying the old data + new data
3011          //there is a gap between the new data and the old data
3012          if(s_before >= new_size){
3013             //Old situation:
3014             // _________________________________________________________
3015             //|            raw_mem                | old_begin | old_end |
3016             //| __________________________________|___________|_________|
3017             //
3018             //New situation:
3019             // _________________________________________________________
3020             //| old_begin |    new   | old_end |         raw_mem        |
3021             //|___________|__________|_________|________________________|
3022             //
3023             //Now initialize the rest of memory with the last old values
3024             if(before_plus_new != new_size){ //Special case to avoid operations in back insertion
3025                ::boost::container::uninitialized_move_alloc
3026                   (this->m_holder.alloc(), pos, old_finish, new_start + before_plus_new);
3027                //All new elements correctly constructed, avoid new element destruction
3028                this->m_holder.m_size = new_size;
3029             }
3030             //Old values destroyed automatically with "old_values_destroyer"
3031             //when "old_values_destroyer" goes out of scope unless the have trivial
3032             //destructor after move.
3033             if(value_traits::trivial_dctr_after_move)
3034                old_values_destroyer.release();
3035          }
3036          //s_before is so big that divides old_end
3037          else{
3038             //Old situation:
3039             // __________________________________________________
3040             //|            raw_mem         | old_begin | old_end |
3041             //| ___________________________|___________|_________|
3042             //
3043             //New situation:
3044             // __________________________________________________
3045             //| old_begin |   new    | old_end |  raw_mem        |
3046             //|___________|__________|_________|_________________|
3047             //
3048             //Now initialize the rest of memory with the last old values
3049             //All new elements correctly constructed, avoid new element destruction
3050             const size_type raw_gap = s_before - before_plus_new;
3051             if(!value_traits::trivial_dctr){
3052                //Now initialize the rest of s_before memory with the
3053                //first of elements after new values
3054                ::boost::container::uninitialized_move_alloc_n
3055                   (this->m_holder.alloc(), pos, raw_gap, new_start + before_plus_new);
3056                //Now we have a contiguous buffer so program trailing element destruction
3057                //and update size to the final size.
3058                old_values_destroyer.shrink_forward(new_size-s_before);
3059                this->m_holder.m_size = new_size;
3060                //Now move remaining last objects in the old buffer begin
3061                ::boost::container::move(pos + raw_gap, old_finish, old_start);
3062                //Once moved, avoid calling the destructors if trivial after move
3063                if(value_traits::trivial_dctr_after_move){
3064                   old_values_destroyer.release();
3065                }
3066             }
3067             else{ //If trivial destructor, we can uninitialized copy + copy in a single uninitialized copy
3068                ::boost::container::uninitialized_move_alloc_n
3069                   (this->m_holder.alloc(), pos, old_finish - pos, new_start + before_plus_new);
3070                this->m_holder.m_size = new_size;
3071                old_values_destroyer.release();
3072             }
3073          }
3074       }
3075       else{
3076          //Check if we have to do the insertion in two phases
3077          //since maybe s_before is not big enough and
3078          //the buffer was expanded both sides
3079          //
3080          //Old situation:
3081          // _________________________________________________
3082          //| raw_mem | old_begin + old_end |  raw_mem        |
3083          //|_________|_____________________|_________________|
3084          //
3085          //New situation with do_after:
3086          // _________________________________________________
3087          //|     old_begin + new + old_end     |  raw_mem    |
3088          //|___________________________________|_____________|
3089          //
3090          //New without do_after:
3091          // _________________________________________________
3092          //| old_begin + new + old_end  |  raw_mem           |
3093          //|____________________________|____________________|
3094          //
3095          const bool do_after = n > s_before;
3096 
3097          //Now we can have two situations: the raw_mem of the
3098          //beginning divides the old_begin, or the new elements:
3099          if (s_before <= elemsbefore) {
3100             //The raw memory divides the old_begin group:
3101             //
3102             //If we need two phase construction (do_after)
3103             //new group is divided in new = new_beg + new_end groups
3104             //In this phase only new_beg will be inserted
3105             //
3106             //Old situation:
3107             // _________________________________________________
3108             //| raw_mem | old_begin | old_end |  raw_mem        |
3109             //|_________|___________|_________|_________________|
3110             //
3111             //New situation with do_after(1):
3112             //This is not definitive situation, the second phase
3113             //will include
3114             // _________________________________________________
3115             //| old_begin | new_beg | old_end |  raw_mem        |
3116             //|___________|_________|_________|_________________|
3117             //
3118             //New situation without do_after:
3119             // _________________________________________________
3120             //| old_begin | new | old_end |  raw_mem            |
3121             //|___________|_____|_________|_____________________|
3122             //
3123             //Copy the first part of old_begin to raw_mem
3124             ::boost::container::uninitialized_move_alloc_n
3125                (this->m_holder.alloc(), old_start, s_before, new_start);
3126             //The buffer is all constructed until old_end,
3127             //so program trailing destruction and assign final size
3128             //if !do_after, s_before+n otherwise.
3129             size_type new_1st_range;
3130             if(do_after){
3131                new_1st_range = s_before;
3132                //release destroyer and update size
3133                old_values_destroyer.release();
3134             }
3135             else{
3136                new_1st_range = n;
3137                if(value_traits::trivial_dctr_after_move)
3138                   old_values_destroyer.release();
3139                else{
3140                   old_values_destroyer.shrink_forward(old_size - (s_before - n));
3141                }
3142             }
3143             this->m_holder.m_size = old_size + new_1st_range;
3144             //Now copy the second part of old_begin overwriting itself
3145             T *const next = ::boost::container::move(old_start + s_before, pos, old_start);
3146             //Now copy the new_beg elements
3147             insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), next, new_1st_range);
3148 
3149             //If there is no after work and the last old part needs to be moved to front, do it
3150             if(!do_after && (n != s_before)){
3151                //Now displace old_end elements
3152                ::boost::container::move(pos, old_finish, next + new_1st_range);
3153             }
3154          }
3155          else {
3156             //If we have to expand both sides,
3157             //we will play if the first new values so
3158             //calculate the upper bound of new values
3159 
3160             //The raw memory divides the new elements
3161             //
3162             //If we need two phase construction (do_after)
3163             //new group is divided in new = new_beg + new_end groups
3164             //In this phase only new_beg will be inserted
3165             //
3166             //Old situation:
3167             // _______________________________________________________
3168             //|   raw_mem     | old_begin | old_end |  raw_mem        |
3169             //|_______________|___________|_________|_________________|
3170             //
3171             //New situation with do_after():
3172             // ____________________________________________________
3173             //| old_begin |    new_beg    | old_end |  raw_mem     |
3174             //|___________|_______________|_________|______________|
3175             //
3176             //New situation without do_after:
3177             // ______________________________________________________
3178             //| old_begin | new | old_end |  raw_mem                 |
3179             //|___________|_____|_________|__________________________|
3180             //
3181             //First copy whole old_begin and part of new to raw_mem
3182             T * const new_pos = ::boost::container::uninitialized_move_alloc
3183                (this->m_holder.alloc(), old_start, pos, new_start);
3184             this->m_holder.m_size = elemsbefore;
3185             const size_type mid_n = s_before - elemsbefore;
3186             insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), new_pos, mid_n);
3187             //The buffer is all constructed until old_end,
3188             //release destroyer
3189             this->m_holder.m_size = old_size + s_before;
3190             old_values_destroyer.release();
3191 
3192             if(do_after){
3193                //Copy new_beg part
3194                insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), old_start, elemsbefore);
3195             }
3196             else{
3197                //Copy all new elements
3198                const size_type rest_new = n - mid_n;
3199                insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), old_start, rest_new);
3200                T* const move_start = old_start + rest_new;
3201                //Displace old_end
3202                T* const move_end = ::boost::container::move(pos, old_finish, move_start);
3203                //Destroy remaining moved elements from old_end except if they
3204                //have trivial destructor after being moved
3205                size_type n_destroy = s_before - n;
3206                if(!value_traits::trivial_dctr_after_move)
3207                   boost::container::destroy_alloc_n(this->get_stored_allocator(), move_end, n_destroy);
3208                this->m_holder.m_size -= n_destroy;
3209             }
3210          }
3211 
3212          //This is only executed if two phase construction is needed
3213          if(do_after){
3214             //The raw memory divides the new elements
3215             //
3216             //Old situation:
3217             // ______________________________________________________
3218             //|   raw_mem    | old_begin |  old_end   |  raw_mem     |
3219             //|______________|___________|____________|______________|
3220             //
3221             //New situation with do_after(1):
3222             // _______________________________________________________
3223             //| old_begin   +   new_beg  | new_end |old_end | raw_mem |
3224             //|__________________________|_________|________|_________|
3225             //
3226             //New situation with do_after(2):
3227             // ______________________________________________________
3228             //| old_begin      +       new            | old_end |raw |
3229             //|_______________________________________|_________|____|
3230             //
3231             const size_type n_after    = n - s_before;
3232             const size_type elemsafter = old_size - elemsbefore;
3233 
3234             //We can have two situations:
3235             if (elemsafter >= n_after){
3236                //The raw_mem from end will divide displaced old_end
3237                //
3238                //Old situation:
3239                // ______________________________________________________
3240                //|   raw_mem    | old_begin |  old_end   |  raw_mem     |
3241                //|______________|___________|____________|______________|
3242                //
3243                //New situation with do_after(1):
3244                // _______________________________________________________
3245                //| old_begin   +   new_beg  | new_end |old_end | raw_mem |
3246                //|__________________________|_________|________|_________|
3247                //
3248                //First copy the part of old_end raw_mem
3249                T* finish_n = old_finish - n_after;
3250                ::boost::container::uninitialized_move_alloc
3251                   (this->m_holder.alloc(), finish_n, old_finish, old_finish);
3252                this->m_holder.m_size += n_after;
3253                //Displace the rest of old_end to the new position
3254                boost::container::move_backward(pos, finish_n, old_finish);
3255                //Now overwrite with new_end
3256                //The new_end part is [first + (n - n_after), last)
3257                insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, n_after);
3258             }
3259             else {
3260                //The raw_mem from end will divide new_end part
3261                //
3262                //Old situation:
3263                // _____________________________________________________________
3264                //|   raw_mem    | old_begin |  old_end   |  raw_mem            |
3265                //|______________|___________|____________|_____________________|
3266                //
3267                //New situation with do_after(2):
3268                // _____________________________________________________________
3269                //| old_begin   +   new_beg  |     new_end   |old_end | raw_mem |
3270                //|__________________________|_______________|________|_________|
3271                //
3272 
3273                const size_type mid_last_dist = n_after - elemsafter;
3274                //First initialize data in raw memory
3275 
3276                //Copy to the old_end part to the uninitialized zone leaving a gap.
3277                ::boost::container::uninitialized_move_alloc
3278                   (this->m_holder.alloc(), pos, old_finish, old_finish + mid_last_dist);
3279 
3280                typename value_traits::ArrayDestructor old_end_destroyer
3281                   (old_finish + mid_last_dist, this->m_holder.alloc(), old_finish - pos);
3282 
3283                //Copy the first part to the already constructed old_end zone
3284                insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, elemsafter);
3285                //Copy the rest to the uninitialized zone filling the gap
3286                insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, mid_last_dist);
3287                this->m_holder.m_size += n_after;
3288                old_end_destroyer.release();
3289             }
3290          }
3291       }
3292    }
3293 
priv_check_range(size_type n) const3294    void priv_check_range(size_type n) const
3295    {
3296       //If n is out of range, throw an out_of_range exception
3297       if (n >= this->size()){
3298          throw_out_of_range("vector::at out of range");
3299       }
3300    }
3301 
3302    #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
3303    public:
3304    unsigned int num_expand_fwd;
3305    unsigned int num_expand_bwd;
3306    unsigned int num_shrink;
3307    unsigned int num_alloc;
reset_alloc_stats()3308    void reset_alloc_stats()
3309    {  num_expand_fwd = num_expand_bwd = num_alloc = 0, num_shrink = 0;   }
3310    #endif
3311    #endif   //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3312 };
3313 
3314 }} //namespace boost::container
3315 
3316 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3317 
3318 namespace boost {
3319 
3320 //!has_trivial_destructor_after_move<> == true_type
3321 //!specialization for optimizations
3322 template <class T, class Allocator>
3323 struct has_trivial_destructor_after_move<boost::container::vector<T, Allocator> >
3324 {
3325    typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
3326    static const bool value = ::boost::has_trivial_destructor_after_move<Allocator>::value &&
3327                              ::boost::has_trivial_destructor_after_move<pointer>::value;
3328 };
3329 
3330 }
3331 
3332 #endif   //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
3333 
3334 #include <boost/container/detail/config_end.hpp>
3335 
3336 #endif //   #ifndef  BOOST_CONTAINER_CONTAINER_VECTOR_HPP
3337