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