xref: /reactos/sdk/include/c++/stlport/stl/_vector.h (revision 114556b1)
1 /*
2  *
3  * Copyright (c) 1994
4  * Hewlett-Packard Company
5  *
6  * Copyright (c) 1996,1997
7  * Silicon Graphics Computer Systems, Inc.
8  *
9  * Copyright (c) 1997
10  * Moscow Center for SPARC Technology
11  *
12  * Copyright (c) 1999
13  * Boris Fomitchev
14  *
15  * This material is provided "as is", with absolutely no warranty expressed
16  * or implied. Any use is at your own risk.
17  *
18  * Permission to use or copy this software for any purpose is hereby granted
19  * without fee, provided the above notices are retained on all copies.
20  * Permission to modify the code and to distribute modified code is granted,
21  * provided the above notices are retained, and a notice that the code was
22  * modified is included with the above copyright notice.
23  *
24  */
25 
26 /* NOTE: This is an internal header file, included by other STL headers.
27  *   You should not attempt to use it directly.
28  */
29 
30 #ifndef _STLP_INTERNAL_VECTOR_H
31 #define _STLP_INTERNAL_VECTOR_H
32 
33 #ifndef _STLP_INTERNAL_ALGOBASE_H
34 #  include <stl/_algobase.h>
35 #endif
36 
37 #ifndef _STLP_INTERNAL_ALLOC_H
38 #  include <stl/_alloc.h>
39 #endif
40 
41 #ifndef _STLP_INTERNAL_ITERATOR_H
42 #  include <stl/_iterator.h>
43 #endif
44 
45 #ifndef _STLP_INTERNAL_UNINITIALIZED_H
46 #  include <stl/_uninitialized.h>
47 #endif
48 
49 _STLP_BEGIN_NAMESPACE
50 
51 // The vector base class serves one purpose, its constructor and
52 // destructor allocate (but don't initialize) storage.  This makes
53 // exception safety easier.
54 
55 _STLP_MOVE_TO_PRIV_NAMESPACE
56 
57 template <class _Tp, class _Alloc>
58 class _Vector_base {
59 public:
60   typedef _Vector_base<_Tp, _Alloc> _Self;
61   _STLP_FORCE_ALLOCATORS(_Tp, _Alloc)
62   typedef _Alloc allocator_type;
63   typedef _Tp* pointer;
64   typedef _STLP_alloc_proxy<pointer, _Tp, allocator_type> _AllocProxy;
65 
_Vector_base(const _Alloc & __a)66   _Vector_base(const _Alloc& __a)
67     : _M_start(0), _M_finish(0), _M_end_of_storage(__a, 0) {}
68 
_Vector_base(size_t __n,const _Alloc & __a)69   _Vector_base(size_t __n, const _Alloc& __a)
70     : _M_start(0), _M_finish(0), _M_end_of_storage(__a, 0) {
71     _M_start = _M_end_of_storage.allocate(__n, __n);
72     _M_finish = _M_start;
73     _M_end_of_storage._M_data = _M_start + __n;
74     _STLP_MPWFIX_TRY _STLP_MPWFIX_CATCH
75   }
76 
77 #if !defined (_STLP_NO_MOVE_SEMANTIC)
_Vector_base(__move_source<_Self> src)78   _Vector_base(__move_source<_Self> src)
79     : _M_start(src.get()._M_start), _M_finish(src.get()._M_finish),
80       _M_end_of_storage(__move_source<_AllocProxy>(src.get()._M_end_of_storage)) {
81     //Set the source as empty:
82     src.get()._M_finish = src.get()._M_end_of_storage._M_data = src.get()._M_start = 0;
83   }
84 #endif
85 
~_Vector_base()86   ~_Vector_base() {
87     if (_M_start != _STLP_DEFAULT_CONSTRUCTED(pointer))
88       _M_end_of_storage.deallocate(_M_start, _M_end_of_storage._M_data - _M_start);
89   }
90 
91 protected:
92   void _STLP_FUNCTION_THROWS _M_throw_length_error() const;
93   void _STLP_FUNCTION_THROWS _M_throw_out_of_range() const;
94 
95   pointer _M_start;
96   pointer _M_finish;
97   _AllocProxy _M_end_of_storage;
98 };
99 
100 #if defined (_STLP_USE_PTR_SPECIALIZATIONS)
101 #  define vector _STLP_PTR_IMPL_NAME(vector)
102 #elif defined (_STLP_DEBUG)
103 #  define vector _STLP_NON_DBG_NAME(vector)
104 #else
105 _STLP_MOVE_TO_STD_NAMESPACE
106 #endif
107 
108 template <class _Tp, _STLP_DFL_TMPL_PARAM(_Alloc, allocator<_Tp>) >
109 class vector : protected _STLP_PRIV _Vector_base<_Tp, _Alloc>
110 #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (vector)
111              , public __stlport_class<vector<_Tp, _Alloc> >
112 #endif
113 {
114 private:
115   typedef _STLP_PRIV _Vector_base<_Tp, _Alloc> _Base;
116   typedef vector<_Tp, _Alloc> _Self;
117 public:
118   _STLP_FORCE_ALLOCATORS(_Tp, _Alloc)
119   typedef typename _Base::allocator_type allocator_type;
120 
121   typedef _Tp value_type;
122   typedef value_type* pointer;
123   typedef const value_type* const_pointer;
124   typedef value_type* iterator;
125   typedef const value_type* const_iterator;
126 
127   typedef value_type& reference;
128   typedef const value_type& const_reference;
129   typedef size_t size_type;
130   typedef ptrdiff_t difference_type;
131   typedef random_access_iterator_tag _Iterator_category;
132 
133   _STLP_DECLARE_RANDOM_ACCESS_REVERSE_ITERATORS;
134 
get_allocator()135   allocator_type get_allocator() const
136   { return _STLP_CONVERT_ALLOCATOR((const allocator_type&)this->_M_end_of_storage, _Tp); }
137 
138 private:
139 #if defined (_STLP_NO_MOVE_SEMANTIC)
140   typedef __false_type _Movable;
141 #endif
142 
143   // handles insertions on overflow
144   void _M_insert_overflow_aux(pointer __pos, const _Tp& __x, const __false_type& /*_Movable*/,
145                               size_type __fill_len, bool __atend);
_M_insert_overflow_aux(pointer __pos,const _Tp & __x,const __true_type &,size_type __fill_len,bool __atend)146   void _M_insert_overflow_aux(pointer __pos, const _Tp& __x, const __true_type& /*_Movable*/,
147                               size_type __fill_len, bool __atend) {
148     //We need to take care of self referencing here:
149     if (_M_is_inside(__x)) {
150       value_type __x_copy = __x;
151       _M_insert_overflow_aux(__pos, __x_copy, __false_type(), __fill_len, __atend);
152       return;
153     }
154     _M_insert_overflow_aux(__pos, __x, __false_type(), __fill_len, __atend);
155   }
156 
157   void _M_insert_overflow(pointer __pos, const _Tp& __x, const __false_type& /*_TrivialCopy*/,
158                           size_type __fill_len, bool __atend = false) {
159 #if !defined (_STLP_NO_MOVE_SEMANTIC)
160     typedef typename __move_traits<_Tp>::implemented _Movable;
161 #endif
162     _M_insert_overflow_aux(__pos, __x, _Movable(), __fill_len, __atend);
163   }
164   void _M_insert_overflow(pointer __pos, const _Tp& __x, const __true_type& /*_TrivialCopy*/,
165                           size_type __fill_len, bool __atend = false);
_M_range_check(size_type __n)166   void _M_range_check(size_type __n) const {
167     if (__n >= size_type(this->_M_finish - this->_M_start))
168       this->_M_throw_out_of_range();
169   }
170 
_M_compute_next_size(size_type __n)171   size_type _M_compute_next_size(size_type __n) {
172     const size_type __size = size();
173     if (__n > max_size() - __size)
174       this->_M_throw_length_error();
175     size_type __len = __size + (max)(__n, __size);
176     if (__len > max_size() || __len < __size)
177       __len = max_size(); // overflow
178     return __len;
179   }
180 
181 public:
begin()182   iterator begin()             { return this->_M_start; }
begin()183   const_iterator begin() const { return this->_M_start; }
end()184   iterator end()               { return this->_M_finish; }
end()185   const_iterator end() const   { return this->_M_finish; }
186 
rbegin()187   reverse_iterator rbegin()              { return reverse_iterator(end()); }
rbegin()188   const_reverse_iterator rbegin() const  { return const_reverse_iterator(end()); }
rend()189   reverse_iterator rend()                { return reverse_iterator(begin()); }
rend()190   const_reverse_iterator rend() const    { return const_reverse_iterator(begin()); }
191 
size()192   size_type size() const        { return size_type(this->_M_finish - this->_M_start); }
max_size()193   size_type max_size() const {
194     size_type __vector_max_size = size_type(-1) / sizeof(_Tp);
195     typename allocator_type::size_type __alloc_max_size = this->_M_end_of_storage.max_size();
196     return (__alloc_max_size < __vector_max_size)?__alloc_max_size:__vector_max_size;
197   }
198 
capacity()199   size_type capacity() const    { return size_type(this->_M_end_of_storage._M_data - this->_M_start); }
empty()200   bool empty() const            { return this->_M_start == this->_M_finish; }
201 
202   reference operator[](size_type __n) { return *(begin() + __n); }
203   const_reference operator[](size_type __n) const { return *(begin() + __n); }
204 
front()205   reference front()             { return *begin(); }
front()206   const_reference front() const { return *begin(); }
back()207   reference back()              { return *(end() - 1); }
back()208   const_reference back() const  { return *(end() - 1); }
209 
at(size_type __n)210   reference at(size_type __n) { _M_range_check(__n); return (*this)[__n]; }
at(size_type __n)211   const_reference at(size_type __n) const { _M_range_check(__n); return (*this)[__n]; }
212 
data()213   _Tp* data()                   { return this->_M_start; }
data()214   const _Tp* data() const       { return this->_M_start; }
215 
216 #if !defined (_STLP_DONT_SUP_DFLT_PARAM)
217   explicit vector(const allocator_type& __a = allocator_type())
218 #else
vector()219   vector()
220     : _STLP_PRIV _Vector_base<_Tp, _Alloc>(allocator_type()) {}
vector(const allocator_type & __a)221   vector(const allocator_type& __a)
222 #endif
223     : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__a) {}
224 
225 #if !defined (_STLP_DONT_SUP_DFLT_PARAM)
226 private:
227   //We always call _M_initialize with only 1 parameter. Default parameter
228   //is used to allow explicit instanciation of vector with types with no
229   //default constructor.
230   void _M_initialize(size_type __n, const _Tp& __val = _STLP_DEFAULT_CONSTRUCTED(_Tp))
231   { this->_M_finish = _STLP_PRIV __uninitialized_init(this->_M_start, __n, __val); }
232 public:
vector(size_type __n)233   explicit vector(size_type __n)
234     : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__n, allocator_type())
235   { _M_initialize(__n); }
236   vector(size_type __n, const _Tp& __val, const allocator_type& __a = allocator_type())
237 #else
vector(size_type __n)238   explicit vector(size_type __n)
239     : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__n, allocator_type())
240   { this->_M_finish = _STLP_PRIV __uninitialized_init(this->_M_start, __n, _STLP_DEFAULT_CONSTRUCTED(_Tp)); }
vector(size_type __n,const _Tp & __val)241   vector(size_type __n, const _Tp& __val)
242     : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__n, allocator_type())
243   { this->_M_finish = _STLP_PRIV __uninitialized_fill_n(this->_M_start, __n, __val); }
vector(size_type __n,const _Tp & __val,const allocator_type & __a)244   vector(size_type __n, const _Tp& __val, const allocator_type& __a)
245 #endif
246     : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__n, __a)
247   { this->_M_finish = _STLP_PRIV __uninitialized_fill_n(this->_M_start, __n, __val); }
248 
vector(const _Self & __x)249   vector(const _Self& __x)
250     : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__x.size(), __x.get_allocator()) {
251     typedef typename __type_traits<_Tp>::has_trivial_copy_constructor _TrivialUCopy;
252     this->_M_finish = _STLP_PRIV __ucopy_ptrs(__x.begin(), __x.end(), this->_M_start, _TrivialUCopy());
253   }
254 
255 #if !defined (_STLP_NO_MOVE_SEMANTIC)
vector(__move_source<_Self> src)256   vector(__move_source<_Self> src)
257     : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__move_source<_Base>(src.get()))
258   {}
259 #endif
260 
261 #if defined (_STLP_MEMBER_TEMPLATES)
262 private:
263   template <class _Integer>
_M_initialize_aux(_Integer __n,_Integer __val,const __true_type &)264   void _M_initialize_aux(_Integer __n, _Integer __val,
265                          const __true_type& /*_IsIntegral*/) {
266     size_type __real_n = __n;
267     this->_M_start = this->_M_end_of_storage.allocate(__n, __real_n);
268     this->_M_end_of_storage._M_data = this->_M_start + __real_n;
269     this->_M_finish = _STLP_PRIV __uninitialized_fill_n(this->_M_start, __n, __val);
270   }
271 
272   template <class _InputIterator>
_M_initialize_aux(_InputIterator __first,_InputIterator __last,const __false_type &)273   void _M_initialize_aux(_InputIterator __first, _InputIterator __last,
274                          const __false_type& /*_IsIntegral*/)
275   { _M_range_initialize(__first, __last, _STLP_ITERATOR_CATEGORY(__first, _InputIterator)); }
276 
277 public:
278   // Check whether it's an integral type.  If so, it's not an iterator.
279   template <class _InputIterator>
vector(_InputIterator __first,_InputIterator __last,const allocator_type & __a _STLP_ALLOCATOR_TYPE_DFL)280   vector(_InputIterator __first, _InputIterator __last,
281                const allocator_type& __a _STLP_ALLOCATOR_TYPE_DFL )
282     : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__a) {
283     typedef typename _IsIntegral<_InputIterator>::_Ret _Integral;
284     _M_initialize_aux(__first, __last, _Integral());
285   }
286 
287 #  if defined (_STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS)
288   template <class _InputIterator>
vector(_InputIterator __first,_InputIterator __last)289   vector(_InputIterator __first, _InputIterator __last)
290     : _STLP_PRIV _Vector_base<_Tp, _Alloc>(allocator_type()) {
291     typedef typename _IsIntegral<_InputIterator>::_Ret _Integral;
292     _M_initialize_aux(__first, __last, _Integral());
293   }
294 #  endif /* _STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS */
295 
296 #else /* _STLP_MEMBER_TEMPLATES */
297   vector(const _Tp* __first, const _Tp* __last,
298          const allocator_type& __a = allocator_type())
299     : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__last - __first, __a) {
300     typedef typename __type_traits<_Tp>::has_trivial_copy_constructor _TrivialUCopy;
301     this->_M_finish = _STLP_PRIV __ucopy_ptrs(__first, __last, this->_M_start, _TrivialUCopy());
302   }
303 #endif /* _STLP_MEMBER_TEMPLATES */
304 
305   //As the vector container is a back insert oriented container it
306   //seems rather logical to destroy elements in reverse order.
~vector()307   ~vector() { _STLP_STD::_Destroy_Range(rbegin(), rend()); }
308 
309   _Self& operator=(const _Self& __x);
310 
311   void reserve(size_type __n);
312 
313   // assign(), a generalized assignment member function.  Two
314   // versions: one that takes a count, and one that takes a range.
315   // The range version is a member template, so we dispatch on whether
316   // or not the type is an integer.
317 
assign(size_type __n,const _Tp & __val)318   void assign(size_type __n, const _Tp& __val) { _M_fill_assign(__n, __val); }
319   void _M_fill_assign(size_type __n, const _Tp& __val);
320 
321 #if defined (_STLP_MEMBER_TEMPLATES)
322   template <class _ForwardIter>
_M_assign_aux(_ForwardIter __first,_ForwardIter __last,const forward_iterator_tag &)323   void _M_assign_aux(_ForwardIter __first, _ForwardIter __last, const forward_iterator_tag &) {
324 #else
325   void assign(const_iterator __first, const_iterator __last) {
326     typedef const_iterator _ForwardIter;
327 #endif
328     const size_type __len = _STLP_STD::distance(__first, __last);
329     if (__len > capacity()) {
330       size_type __n = __len;
331       iterator __tmp = _M_allocate_and_copy(__n, __first, __last);
332       _M_clear();
333       _M_set(__tmp, __tmp + __len, __tmp + __n);
334     }
335     else if (size() >= __len) {
336       iterator __new_finish = copy(__first, __last, this->_M_start);
337       _STLP_STD::_Destroy_Range(__new_finish, this->_M_finish);
338       this->_M_finish = __new_finish;
339     }
340     else {
341       _ForwardIter __mid = __first;
342       _STLP_STD::advance(__mid, size());
343       _STLP_STD::copy(__first, __mid, this->_M_start);
344       this->_M_finish = _STLP_STD::uninitialized_copy(__mid, __last, this->_M_finish);
345     }
346   }
347 
348 #if defined (_STLP_MEMBER_TEMPLATES)
349   template <class _InputIter>
350   void _M_assign_aux(_InputIter __first, _InputIter __last,
351                      const input_iterator_tag &) {
352     iterator __cur = begin();
353     for ( ; __first != __last && __cur != end(); ++__cur, ++__first)
354       *__cur = *__first;
355     if (__first == __last)
356       erase(__cur, end());
357     else
358       insert(end(), __first, __last);
359   }
360 
361   template <class _Integer>
362   void _M_assign_dispatch(_Integer __n, _Integer __val,
363                           const __true_type& /*_IsIntegral*/)
364   { _M_fill_assign(__n, __val); }
365 
366   template <class _InputIter>
367   void _M_assign_dispatch(_InputIter __first, _InputIter __last,
368                           const __false_type& /*_IsIntegral*/)
369   { _M_assign_aux(__first, __last, _STLP_ITERATOR_CATEGORY(__first, _InputIter)); }
370 
371   template <class _InputIterator>
372   void assign(_InputIterator __first, _InputIterator __last) {
373     typedef typename _IsIntegral<_InputIterator>::_Ret _Integral;
374     _M_assign_dispatch(__first, __last, _Integral());
375   }
376 #endif
377 
378 #if !defined (_STLP_DONT_SUP_DFLT_PARAM) && !defined (_STLP_NO_ANACHRONISMS)
379   void push_back(const _Tp& __x = _STLP_DEFAULT_CONSTRUCTED(_Tp)) {
380 #else
381   void push_back(const _Tp& __x) {
382 #endif
383     if (this->_M_finish != this->_M_end_of_storage._M_data) {
384       _Copy_Construct(this->_M_finish, __x);
385       ++this->_M_finish;
386     }
387     else {
388       typedef typename __type_traits<_Tp>::has_trivial_assignment_operator _TrivialCopy;
389       _M_insert_overflow(this->_M_finish, __x, _TrivialCopy(), 1, true);
390     }
391   }
392 
393 #if !defined(_STLP_DONT_SUP_DFLT_PARAM) && !defined(_STLP_NO_ANACHRONISMS)
394   iterator insert(iterator __pos, const _Tp& __x = _STLP_DEFAULT_CONSTRUCTED(_Tp));
395 #else
396   iterator insert(iterator __pos, const _Tp& __x);
397 #endif
398 
399 #if defined(_STLP_DONT_SUP_DFLT_PARAM) && !defined(_STLP_NO_ANACHRONISMS)
400   void push_back() { push_back(_STLP_DEFAULT_CONSTRUCTED(_Tp)); }
401   iterator insert(iterator __pos) { return insert(__pos, _STLP_DEFAULT_CONSTRUCTED(_Tp)); }
402 #endif
403 
404   void swap(_Self& __x) {
405     _STLP_STD::swap(this->_M_start, __x._M_start);
406     _STLP_STD::swap(this->_M_finish, __x._M_finish);
407     this->_M_end_of_storage.swap(__x._M_end_of_storage);
408   }
409 #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
410   void _M_swap_workaround(_Self& __x) { swap(__x); }
411 #endif
412 
413 private:
414   void _M_fill_insert_aux (iterator __pos, size_type __n, const _Tp& __x, const __true_type& /*_Movable*/);
415   void _M_fill_insert_aux (iterator __pos, size_type __n, const _Tp& __x, const __false_type& /*_Movable*/);
416   void _M_fill_insert (iterator __pos, size_type __n, const _Tp& __x);
417 
418   bool _M_is_inside(const value_type& __x) const {
419     return (&__x >= this->_M_start && &__x < this->_M_finish);
420   }
421 
422 #if defined (_STLP_MEMBER_TEMPLATES)
423   template <class _ForwardIterator>
424   void _M_range_insert_realloc(iterator __pos,
425                                _ForwardIterator __first, _ForwardIterator __last,
426 #else
427   void _M_range_insert_realloc(iterator __pos,
428                                const_iterator __first, const_iterator __last,
429 #endif
430                                size_type __n) {
431     typedef typename __type_traits<_Tp>::has_trivial_copy_constructor _TrivialUCopy;
432 #if !defined (_STLP_NO_MOVE_SEMANTIC)
433     typedef typename __move_traits<_Tp>::implemented _Movable;
434 #endif
435     size_type __len = _M_compute_next_size(__n);
436     pointer __new_start = this->_M_end_of_storage.allocate(__len, __len);
437     pointer __new_finish = __new_start;
438     _STLP_TRY {
439       __new_finish = _STLP_PRIV __uninitialized_move(this->_M_start, __pos, __new_start, _TrivialUCopy(), _Movable());
440       __new_finish = uninitialized_copy(__first, __last, __new_finish);
441       __new_finish = _STLP_PRIV __uninitialized_move(__pos, this->_M_finish, __new_finish, _TrivialUCopy(), _Movable());
442     }
443     _STLP_UNWIND((_STLP_STD::_Destroy_Range(__new_start,__new_finish),
444                   this->_M_end_of_storage.deallocate(__new_start,__len)))
445     _M_clear_after_move();
446     _M_set(__new_start, __new_finish, __new_start + __len);
447   }
448 
449 #if defined (_STLP_MEMBER_TEMPLATES)
450   template <class _ForwardIterator>
451   void _M_range_insert_aux(iterator __pos,
452                            _ForwardIterator __first, _ForwardIterator __last,
453 #else
454   void _M_range_insert_aux(iterator __pos,
455                            const_iterator __first, const_iterator __last,
456 #endif
457                            size_type __n, const __true_type& /*_Movable*/) {
458     iterator __src = this->_M_finish - 1;
459     iterator __dst = __src + __n;
460     for (; __src >= __pos; --__dst, --__src) {
461       _STLP_STD::_Move_Construct(__dst, *__src);
462       _STLP_STD::_Destroy_Moved(__src);
463     }
464     uninitialized_copy(__first, __last, __pos);
465     this->_M_finish += __n;
466   }
467 
468 #if defined (_STLP_MEMBER_TEMPLATES)
469   template <class _ForwardIterator>
470   void _M_range_insert_aux(iterator __pos,
471                            _ForwardIterator __first, _ForwardIterator __last,
472 #else
473   void _M_range_insert_aux(iterator __pos,
474                            const_iterator __first, const_iterator __last,
475 #endif
476                            size_type __n, const __false_type& /*_Movable*/) {
477     typedef typename __type_traits<_Tp>::has_trivial_copy_constructor _TrivialUCopy;
478     typedef typename __type_traits<_Tp>::has_trivial_assignment_operator _TrivialCopy;
479     const size_type __elems_after = this->_M_finish - __pos;
480     pointer __old_finish = this->_M_finish;
481     if (__elems_after > __n) {
482       _STLP_PRIV __ucopy_ptrs(this->_M_finish - __n, this->_M_finish, this->_M_finish, _TrivialUCopy());
483       this->_M_finish += __n;
484       _STLP_PRIV __copy_backward_ptrs(__pos, __old_finish - __n, __old_finish, _TrivialCopy());
485       copy(__first, __last, __pos);
486     }
487     else {
488 #if defined ( _STLP_MEMBER_TEMPLATES )
489       _ForwardIterator __mid = __first;
490       _STLP_STD::advance(__mid, __elems_after);
491 #else
492       const_pointer __mid = __first + __elems_after;
493 #endif
494       uninitialized_copy(__mid, __last, this->_M_finish);
495       this->_M_finish += __n - __elems_after;
496       _STLP_PRIV __ucopy_ptrs(__pos, __old_finish, this->_M_finish, _TrivialUCopy());
497       this->_M_finish += __elems_after;
498       copy(__first, __mid, __pos);
499     } /* elems_after */
500   }
501 
502 
503 #if defined (_STLP_MEMBER_TEMPLATES)
504   template <class _Integer>
505   void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
506                           const __true_type&)
507   { _M_fill_insert(__pos, (size_type) __n, (_Tp) __val); }
508 
509   template <class _InputIterator>
510   void _M_insert_dispatch(iterator __pos,
511                           _InputIterator __first, _InputIterator __last,
512                           const __false_type&)
513   { _M_range_insert(__pos, __first, __last, _STLP_ITERATOR_CATEGORY(__first, _InputIterator)); }
514 
515 public:
516   // Check whether it's an integral type.  If so, it's not an iterator.
517   template <class _InputIterator>
518   void insert(iterator __pos, _InputIterator __first, _InputIterator __last) {
519     typedef typename _IsIntegral<_InputIterator>::_Ret _Integral;
520     _M_insert_dispatch(__pos, __first, __last, _Integral());
521   }
522 
523 private:
524   template <class _InputIterator>
525   void _M_range_insert(iterator __pos,
526                        _InputIterator __first, _InputIterator __last,
527                        const input_iterator_tag &) {
528     for ( ; __first != __last; ++__first) {
529       __pos = insert(__pos, *__first);
530       ++__pos;
531     }
532   }
533 
534   template <class _ForwardIterator>
535   void _M_range_insert(iterator __pos,
536                        _ForwardIterator __first, _ForwardIterator __last,
537                        const forward_iterator_tag &) {
538 #else
539 public:
540   void insert(iterator __pos,
541               const_iterator __first, const_iterator __last) {
542 #endif
543 #if !defined (_STLP_NO_MOVE_SEMANTIC)
544     typedef typename __move_traits<_Tp>::implemented _Movable;
545 #endif
546     /* This method do not check self referencing.
547      * Standard forbids it, checked by the debug mode.
548      */
549     if (__first != __last) {
550       size_type __n = _STLP_STD::distance(__first, __last);
551 
552       if (size_type(this->_M_end_of_storage._M_data - this->_M_finish) >= __n) {
553         _M_range_insert_aux(__pos, __first, __last, __n, _Movable());
554       }
555       else {
556         _M_range_insert_realloc(__pos, __first, __last, __n);
557       }
558     }
559   }
560 
561 public:
562   void insert (iterator __pos, size_type __n, const _Tp& __x)
563   { _M_fill_insert(__pos, __n, __x); }
564 
565   void pop_back() {
566     --this->_M_finish;
567     _STLP_STD::_Destroy(this->_M_finish);
568   }
569 
570 private:
571   iterator _M_erase(iterator __pos, const __true_type& /*_Movable*/) {
572     _STLP_STD::_Destroy(__pos);
573     iterator __dst = __pos, __src = __dst + 1;
574     iterator __end = end();
575     for (; __src != __end; ++__dst, ++__src) {
576       _STLP_STD::_Move_Construct(__dst, *__src);
577       _STLP_STD::_Destroy_Moved(__src);
578     }
579     this->_M_finish = __dst;
580     return __pos;
581   }
582   iterator _M_erase(iterator __pos, const __false_type& /*_Movable*/) {
583     if (__pos + 1 != end()) {
584       typedef typename __type_traits<_Tp>::has_trivial_assignment_operator _TrivialCopy;
585       _STLP_PRIV __copy_ptrs(__pos + 1, this->_M_finish, __pos, _TrivialCopy());
586     }
587     --this->_M_finish;
588     _STLP_STD::_Destroy(this->_M_finish);
589     return __pos;
590   }
591   iterator _M_erase(iterator __first, iterator __last, const __true_type& /*_Movable*/) {
592     iterator __dst = __first, __src = __last;
593     iterator __end = end();
594     for (; __dst != __last && __src != __end; ++__dst, ++__src) {
595       _STLP_STD::_Destroy(__dst);
596       _STLP_STD::_Move_Construct(__dst, *__src);
597     }
598     if (__dst != __last) {
599       //There is more elements to erase than element to move:
600       _STLP_STD::_Destroy_Range(__dst, __last);
601       _STLP_STD::_Destroy_Moved_Range(__last, __end);
602     }
603     else {
604       //There is more element to move than element to erase:
605       for (; __src != __end; ++__dst, ++__src) {
606         _STLP_STD::_Destroy_Moved(__dst);
607         _STLP_STD::_Move_Construct(__dst, *__src);
608       }
609       _STLP_STD::_Destroy_Moved_Range(__dst, __end);
610     }
611     this->_M_finish = __dst;
612     return __first;
613   }
614   iterator _M_erase(iterator __first, iterator __last, const __false_type& /*_Movable*/) {
615     typedef typename __type_traits<_Tp>::has_trivial_assignment_operator _TrivialCopy;
616     pointer __i = _STLP_PRIV __copy_ptrs(__last, this->_M_finish, __first, _TrivialCopy());
617     _STLP_STD::_Destroy_Range(__i, this->_M_finish);
618     this->_M_finish = __i;
619     return __first;
620   }
621 
622 public:
623   iterator erase(iterator __pos) {
624 #if !defined (_STLP_NO_MOVE_SEMANTIC)
625     typedef typename __move_traits<_Tp>::implemented _Movable;
626 #endif
627     return _M_erase(__pos, _Movable());
628   }
629   iterator erase(iterator __first, iterator __last) {
630 #if !defined (_STLP_NO_MOVE_SEMANTIC)
631     typedef typename __move_traits<_Tp>::implemented _Movable;
632 #endif
633     if (__first == __last)
634       return __first;
635     return _M_erase(__first, __last, _Movable());
636   }
637 
638 #if !defined (_STLP_DONT_SUP_DFLT_PARAM)
639   void resize(size_type __new_size, const _Tp& __x = _STLP_DEFAULT_CONSTRUCTED(_Tp)) {
640 #else
641   void resize(size_type __new_size, const _Tp& __x) {
642 #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
643     if (__new_size < size())
644       erase(begin() + __new_size, end());
645     else
646       insert(end(), __new_size - size(), __x);
647   }
648 
649 #if defined (_STLP_DONT_SUP_DFLT_PARAM)
650   void resize(size_type __new_size) { resize(__new_size, _STLP_DEFAULT_CONSTRUCTED(_Tp)); }
651 #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
652 
653   void clear() {
654     erase(begin(), end());
655   }
656 
657 private:
658   void _M_clear() {
659     _STLP_STD::_Destroy_Range(rbegin(), rend());
660     this->_M_end_of_storage.deallocate(this->_M_start, this->_M_end_of_storage._M_data - this->_M_start);
661   }
662 
663   void _M_clear_after_move() {
664     _STLP_STD::_Destroy_Moved_Range(rbegin(), rend());
665     this->_M_end_of_storage.deallocate(this->_M_start, this->_M_end_of_storage._M_data - this->_M_start);
666   }
667 
668   void _M_set(pointer __s, pointer __f, pointer __e) {
669     this->_M_start = __s;
670     this->_M_finish = __f;
671     this->_M_end_of_storage._M_data = __e;
672   }
673 
674 #if defined (_STLP_MEMBER_TEMPLATES)
675   template <class _ForwardIterator>
676   pointer _M_allocate_and_copy(size_type& __n,
677                                _ForwardIterator __first, _ForwardIterator __last)
678 #else /* _STLP_MEMBER_TEMPLATES */
679   pointer _M_allocate_and_copy(size_type& __n,
680                                const_pointer __first, const_pointer __last)
681 #endif /* _STLP_MEMBER_TEMPLATES */
682   {
683     pointer __result = this->_M_end_of_storage.allocate(__n, __n);
684     _STLP_TRY {
685       uninitialized_copy(__first, __last, __result);
686       return __result;
687     }
688     _STLP_UNWIND(this->_M_end_of_storage.deallocate(__result, __n))
689     _STLP_RET_AFTER_THROW(__result)
690   }
691 
692 
693 #if defined (_STLP_MEMBER_TEMPLATES)
694   template <class _InputIterator>
695   void _M_range_initialize(_InputIterator __first, _InputIterator __last,
696                            const input_iterator_tag &) {
697     for ( ; __first != __last; ++__first)
698       push_back(*__first);
699   }
700   // This function is only called by the constructor.
701   template <class _ForwardIterator>
702   void _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
703                            const forward_iterator_tag &) {
704     size_type __n = _STLP_STD::distance(__first, __last);
705     this->_M_start = this->_M_end_of_storage.allocate(__n, __n);
706     this->_M_end_of_storage._M_data = this->_M_start + __n;
707     this->_M_finish = uninitialized_copy(__first, __last, this->_M_start);
708   }
709 #endif /* _STLP_MEMBER_TEMPLATES */
710 };
711 
712 #if defined (vector)
713 #  undef vector
714 _STLP_MOVE_TO_STD_NAMESPACE
715 #endif
716 
717 _STLP_END_NAMESPACE
718 
719 #if !defined (_STLP_LINK_TIME_INSTANTIATION)
720 #  include <stl/_vector.c>
721 #endif
722 
723 #if defined (_STLP_USE_PTR_SPECIALIZATIONS)
724 #  include <stl/pointers/_vector.h>
725 #endif
726 
727 //We define the bool specialization before the debug interfave
728 //to benefit of the debug version of vector even for the bool
729 //specialization.
730 #if !defined (_STLP_NO_BOOL) || !defined (_STLP_NO_EXTENSIONS)
731 #  if !defined (_STLP_INTERNAL_BVECTOR_H)
732 #    include <stl/_bvector.h>
733 #  endif
734 #endif
735 
736 #if defined (_STLP_DEBUG)
737 #  include <stl/debug/_vector.h>
738 #endif
739 
740 _STLP_BEGIN_NAMESPACE
741 
742 #if !defined (_STLP_NO_BOOL) && !defined (_STLP_NO_EXTENSIONS)
743 // This typedef is non-standard.  It is provided for backward compatibility.
744 typedef vector<bool, allocator<bool> > bit_vector;
745 #endif
746 
747 #define _STLP_TEMPLATE_HEADER template <class _Tp, class _Alloc>
748 #define _STLP_TEMPLATE_CONTAINER vector<_Tp, _Alloc>
749 #include <stl/_relops_cont.h>
750 #undef _STLP_TEMPLATE_CONTAINER
751 #undef _STLP_TEMPLATE_HEADER
752 
753 #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
754 #  if !defined (_STLP_NO_MOVE_SEMANTIC)
755 template <class _Tp, class _Alloc>
756 struct __move_traits<vector<_Tp, _Alloc> > {
757   typedef __true_type implemented;
758   typedef typename __move_traits<_Alloc>::complete complete;
759 };
760 #  endif
761 
762 #  if !defined (_STLP_DEBUG)
763 template <class _Tp, class _Alloc>
764 struct _DefaultZeroValue<vector<_Tp, _Alloc> >
765 { typedef typename __type_traits<_Alloc>::has_trivial_default_constructor _Ret; };
766 #  endif
767 
768 #endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
769 
770 _STLP_END_NAMESPACE
771 
772 #endif /* _STLP_VECTOR_H */
773 
774 // Local Variables:
775 // mode:C++
776 // End:
777