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