OSDN Git Service

2010-08-05 Paolo Carlini <paolo.carlini@oracle.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / stl_vector.h
1 // Vector implementation -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library.  This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 // <http://www.gnu.org/licenses/>.
25
26 /*
27  *
28  * Copyright (c) 1994
29  * Hewlett-Packard Company
30  *
31  * Permission to use, copy, modify, distribute and sell this software
32  * and its documentation for any purpose is hereby granted without fee,
33  * provided that the above copyright notice appear in all copies and
34  * that both that copyright notice and this permission notice appear
35  * in supporting documentation.  Hewlett-Packard Company makes no
36  * representations about the suitability of this software for any
37  * purpose.  It is provided "as is" without express or implied warranty.
38  *
39  *
40  * Copyright (c) 1996
41  * Silicon Graphics Computer Systems, Inc.
42  *
43  * Permission to use, copy, modify, distribute and sell this software
44  * and its documentation for any purpose is hereby granted without fee,
45  * provided that the above copyright notice appear in all copies and
46  * that both that copyright notice and this permission notice appear
47  * in supporting documentation.  Silicon Graphics makes no
48  * representations about the suitability of this  software for any
49  * purpose.  It is provided "as is" without express or implied warranty.
50  */
51
52 /** @file stl_vector.h
53  *  This is an internal header file, included by other library headers.
54  *  You should not attempt to use it directly.
55  */
56
57 #ifndef _STL_VECTOR_H
58 #define _STL_VECTOR_H 1
59
60 #include <bits/stl_iterator_base_funcs.h>
61 #include <bits/functexcept.h>
62 #include <bits/concept_check.h>
63 #include <initializer_list>
64
65 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
66
67   /// See bits/stl_deque.h's _Deque_base for an explanation.
68   template<typename _Tp, typename _Alloc>
69     struct _Vector_base
70     {
71       typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
72
73       struct _Vector_impl 
74       : public _Tp_alloc_type
75       {
76         typename _Tp_alloc_type::pointer _M_start;
77         typename _Tp_alloc_type::pointer _M_finish;
78         typename _Tp_alloc_type::pointer _M_end_of_storage;
79
80         _Vector_impl()
81         : _Tp_alloc_type(), _M_start(0), _M_finish(0), _M_end_of_storage(0)
82         { }
83
84         _Vector_impl(_Tp_alloc_type const& __a)
85         : _Tp_alloc_type(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0)
86         { }
87       };
88       
89     public:
90       typedef _Alloc allocator_type;
91
92       _Tp_alloc_type&
93       _M_get_Tp_allocator()
94       { return *static_cast<_Tp_alloc_type*>(&this->_M_impl); }
95
96       const _Tp_alloc_type&
97       _M_get_Tp_allocator() const
98       { return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); }
99
100       allocator_type
101       get_allocator() const
102       { return allocator_type(_M_get_Tp_allocator()); }
103
104       _Vector_base()
105       : _M_impl() { }
106
107       _Vector_base(const allocator_type& __a)
108       : _M_impl(__a) { }
109
110       _Vector_base(size_t __n)
111       : _M_impl()
112       {
113         this->_M_impl._M_start = this->_M_allocate(__n);
114         this->_M_impl._M_finish = this->_M_impl._M_start;
115         this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
116       }
117
118       _Vector_base(size_t __n, const allocator_type& __a)
119       : _M_impl(__a)
120       {
121         this->_M_impl._M_start = this->_M_allocate(__n);
122         this->_M_impl._M_finish = this->_M_impl._M_start;
123         this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
124       }
125
126 #ifdef __GXX_EXPERIMENTAL_CXX0X__
127       _Vector_base(_Vector_base&& __x)
128       : _M_impl(__x._M_get_Tp_allocator())
129       {
130         this->_M_impl._M_start = __x._M_impl._M_start;
131         this->_M_impl._M_finish = __x._M_impl._M_finish;
132         this->_M_impl._M_end_of_storage = __x._M_impl._M_end_of_storage;
133         __x._M_impl._M_start = 0;
134         __x._M_impl._M_finish = 0;
135         __x._M_impl._M_end_of_storage = 0;
136       }
137 #endif
138
139       ~_Vector_base()
140       { _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage
141                       - this->_M_impl._M_start); }
142
143     public:
144       _Vector_impl _M_impl;
145
146       typename _Tp_alloc_type::pointer
147       _M_allocate(size_t __n)
148       { return __n != 0 ? _M_impl.allocate(__n) : 0; }
149
150       void
151       _M_deallocate(typename _Tp_alloc_type::pointer __p, size_t __n)
152       {
153         if (__p)
154           _M_impl.deallocate(__p, __n);
155       }
156     };
157
158
159   /**
160    *  @brief A standard container which offers fixed time access to
161    *  individual elements in any order.
162    *
163    *  @ingroup sequences
164    *
165    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
166    *  <a href="tables.html#66">reversible container</a>, and a
167    *  <a href="tables.html#67">sequence</a>, including the
168    *  <a href="tables.html#68">optional sequence requirements</a> with the
169    *  %exception of @c push_front and @c pop_front.
170    *
171    *  In some terminology a %vector can be described as a dynamic
172    *  C-style array, it offers fast and efficient access to individual
173    *  elements in any order and saves the user from worrying about
174    *  memory and size allocation.  Subscripting ( @c [] ) access is
175    *  also provided as with C-style arrays.
176   */
177   template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
178     class vector : protected _Vector_base<_Tp, _Alloc>
179     {
180       // Concept requirements.
181       typedef typename _Alloc::value_type                _Alloc_value_type;
182       __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
183       __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
184       
185       typedef _Vector_base<_Tp, _Alloc>                  _Base;
186       typedef typename _Base::_Tp_alloc_type             _Tp_alloc_type;
187
188     public:
189       typedef _Tp                                        value_type;
190       typedef typename _Tp_alloc_type::pointer           pointer;
191       typedef typename _Tp_alloc_type::const_pointer     const_pointer;
192       typedef typename _Tp_alloc_type::reference         reference;
193       typedef typename _Tp_alloc_type::const_reference   const_reference;
194       typedef __gnu_cxx::__normal_iterator<pointer, vector> iterator;
195       typedef __gnu_cxx::__normal_iterator<const_pointer, vector>
196       const_iterator;
197       typedef std::reverse_iterator<const_iterator>  const_reverse_iterator;
198       typedef std::reverse_iterator<iterator>            reverse_iterator;
199       typedef size_t                                     size_type;
200       typedef ptrdiff_t                                  difference_type;
201       typedef _Alloc                                     allocator_type;
202
203     protected:
204       using _Base::_M_allocate;
205       using _Base::_M_deallocate;
206       using _Base::_M_impl;
207       using _Base::_M_get_Tp_allocator;
208
209     public:
210       // [23.2.4.1] construct/copy/destroy
211       // (assign() and get_allocator() are also listed in this section)
212       /**
213        *  @brief  Default constructor creates no elements.
214        */
215       vector()
216       : _Base() { }
217
218       /**
219        *  @brief  Creates a %vector with no elements.
220        *  @param  a  An allocator object.
221        */
222       explicit
223       vector(const allocator_type& __a)
224       : _Base(__a) { }
225
226 #ifdef __GXX_EXPERIMENTAL_CXX0X__
227       /**
228        *  @brief  Creates a %vector with default constructed elements.
229        *  @param  n  The number of elements to initially create.
230        *
231        *  This constructor fills the %vector with @a n default
232        *  constructed elements.
233        */
234       explicit
235       vector(size_type __n)
236       : _Base(__n)
237       { _M_default_initialize(__n); }
238
239       /**
240        *  @brief  Creates a %vector with copies of an exemplar element.
241        *  @param  n  The number of elements to initially create.
242        *  @param  value  An element to copy.
243        *  @param  a  An allocator.
244        *
245        *  This constructor fills the %vector with @a n copies of @a value.
246        */
247       vector(size_type __n, const value_type& __value,
248              const allocator_type& __a = allocator_type())
249       : _Base(__n, __a)
250       { _M_fill_initialize(__n, __value); }
251 #else
252       /**
253        *  @brief  Creates a %vector with copies of an exemplar element.
254        *  @param  n  The number of elements to initially create.
255        *  @param  value  An element to copy.
256        *  @param  a  An allocator.
257        *
258        *  This constructor fills the %vector with @a n copies of @a value.
259        */
260       explicit
261       vector(size_type __n, const value_type& __value = value_type(),
262              const allocator_type& __a = allocator_type())
263       : _Base(__n, __a)
264       { _M_fill_initialize(__n, __value); }
265 #endif
266
267       /**
268        *  @brief  %Vector copy constructor.
269        *  @param  x  A %vector of identical element and allocator types.
270        *
271        *  The newly-created %vector uses a copy of the allocation
272        *  object used by @a x.  All the elements of @a x are copied,
273        *  but any extra memory in
274        *  @a x (for fast expansion) will not be copied.
275        */
276       vector(const vector& __x)
277       : _Base(__x.size(), __x._M_get_Tp_allocator())
278       { this->_M_impl._M_finish =
279           std::__uninitialized_copy_a(__x.begin(), __x.end(),
280                                       this->_M_impl._M_start,
281                                       _M_get_Tp_allocator());
282       }
283
284 #ifdef __GXX_EXPERIMENTAL_CXX0X__
285       /**
286        *  @brief  %Vector move constructor.
287        *  @param  x  A %vector of identical element and allocator types.
288        *
289        *  The newly-created %vector contains the exact contents of @a x.
290        *  The contents of @a x are a valid, but unspecified %vector.
291        */
292       vector(vector&& __x)
293       : _Base(std::move(__x)) { }
294
295       /**
296        *  @brief  Builds a %vector from an initializer list.
297        *  @param  l  An initializer_list.
298        *  @param  a  An allocator.
299        *
300        *  Create a %vector consisting of copies of the elements in the
301        *  initializer_list @a l.
302        *
303        *  This will call the element type's copy constructor N times
304        *  (where N is @a l.size()) and do no memory reallocation.
305        */
306       vector(initializer_list<value_type> __l,
307              const allocator_type& __a = allocator_type())
308       : _Base(__a)
309       {
310         _M_range_initialize(__l.begin(), __l.end(),
311                             random_access_iterator_tag());
312       }
313 #endif
314
315       /**
316        *  @brief  Builds a %vector from a range.
317        *  @param  first  An input iterator.
318        *  @param  last  An input iterator.
319        *  @param  a  An allocator.
320        *
321        *  Create a %vector consisting of copies of the elements from
322        *  [first,last).
323        *
324        *  If the iterators are forward, bidirectional, or
325        *  random-access, then this will call the elements' copy
326        *  constructor N times (where N is distance(first,last)) and do
327        *  no memory reallocation.  But if only input iterators are
328        *  used, then this will do at most 2N calls to the copy
329        *  constructor, and logN memory reallocations.
330        */
331       template<typename _InputIterator>
332         vector(_InputIterator __first, _InputIterator __last,
333                const allocator_type& __a = allocator_type())
334         : _Base(__a)
335         {
336           // Check whether it's an integral type.  If so, it's not an iterator.
337           typedef typename std::__is_integer<_InputIterator>::__type _Integral;
338           _M_initialize_dispatch(__first, __last, _Integral());
339         }
340
341       /**
342        *  The dtor only erases the elements, and note that if the
343        *  elements themselves are pointers, the pointed-to memory is
344        *  not touched in any way.  Managing the pointer is the user's
345        *  responsibility.
346        */
347       ~vector()
348       { std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
349                       _M_get_Tp_allocator()); }
350
351       /**
352        *  @brief  %Vector assignment operator.
353        *  @param  x  A %vector of identical element and allocator types.
354        *
355        *  All the elements of @a x are copied, but any extra memory in
356        *  @a x (for fast expansion) will not be copied.  Unlike the
357        *  copy constructor, the allocator object is not copied.
358        */
359       vector&
360       operator=(const vector& __x);
361
362 #ifdef __GXX_EXPERIMENTAL_CXX0X__
363       /**
364        *  @brief  %Vector move assignment operator.
365        *  @param  x  A %vector of identical element and allocator types.
366        *
367        *  The contents of @a x are moved into this %vector (without copying).
368        *  @a x is a valid, but unspecified %vector.
369        */
370       vector&
371       operator=(vector&& __x)
372       {
373         // NB: DR 1204.
374         // NB: DR 675.
375         this->clear();
376         this->swap(__x);
377         return *this;
378       }
379
380       /**
381        *  @brief  %Vector list assignment operator.
382        *  @param  l  An initializer_list.
383        *
384        *  This function fills a %vector with copies of the elements in the
385        *  initializer list @a l.
386        *
387        *  Note that the assignment completely changes the %vector and
388        *  that the resulting %vector's size is the same as the number
389        *  of elements assigned.  Old data may be lost.
390        */
391       vector&
392       operator=(initializer_list<value_type> __l)
393       {
394         this->assign(__l.begin(), __l.end());
395         return *this;
396       }
397 #endif
398
399       /**
400        *  @brief  Assigns a given value to a %vector.
401        *  @param  n  Number of elements to be assigned.
402        *  @param  val  Value to be assigned.
403        *
404        *  This function fills a %vector with @a n copies of the given
405        *  value.  Note that the assignment completely changes the
406        *  %vector and that the resulting %vector's size is the same as
407        *  the number of elements assigned.  Old data may be lost.
408        */
409       void
410       assign(size_type __n, const value_type& __val)
411       { _M_fill_assign(__n, __val); }
412
413       /**
414        *  @brief  Assigns a range to a %vector.
415        *  @param  first  An input iterator.
416        *  @param  last   An input iterator.
417        *
418        *  This function fills a %vector with copies of the elements in the
419        *  range [first,last).
420        *
421        *  Note that the assignment completely changes the %vector and
422        *  that the resulting %vector's size is the same as the number
423        *  of elements assigned.  Old data may be lost.
424        */
425       template<typename _InputIterator>
426         void
427         assign(_InputIterator __first, _InputIterator __last)
428         {
429           // Check whether it's an integral type.  If so, it's not an iterator.
430           typedef typename std::__is_integer<_InputIterator>::__type _Integral;
431           _M_assign_dispatch(__first, __last, _Integral());
432         }
433
434 #ifdef __GXX_EXPERIMENTAL_CXX0X__
435       /**
436        *  @brief  Assigns an initializer list to a %vector.
437        *  @param  l  An initializer_list.
438        *
439        *  This function fills a %vector with copies of the elements in the
440        *  initializer list @a l.
441        *
442        *  Note that the assignment completely changes the %vector and
443        *  that the resulting %vector's size is the same as the number
444        *  of elements assigned.  Old data may be lost.
445        */
446       void
447       assign(initializer_list<value_type> __l)
448       { this->assign(__l.begin(), __l.end()); }
449 #endif
450
451       /// Get a copy of the memory allocation object.
452       using _Base::get_allocator;
453
454       // iterators
455       /**
456        *  Returns a read/write iterator that points to the first
457        *  element in the %vector.  Iteration is done in ordinary
458        *  element order.
459        */
460       iterator
461       begin()
462       { return iterator(this->_M_impl._M_start); }
463
464       /**
465        *  Returns a read-only (constant) iterator that points to the
466        *  first element in the %vector.  Iteration is done in ordinary
467        *  element order.
468        */
469       const_iterator
470       begin() const
471       { return const_iterator(this->_M_impl._M_start); }
472
473       /**
474        *  Returns a read/write iterator that points one past the last
475        *  element in the %vector.  Iteration is done in ordinary
476        *  element order.
477        */
478       iterator
479       end()
480       { return iterator(this->_M_impl._M_finish); }
481
482       /**
483        *  Returns a read-only (constant) iterator that points one past
484        *  the last element in the %vector.  Iteration is done in
485        *  ordinary element order.
486        */
487       const_iterator
488       end() const
489       { return const_iterator(this->_M_impl._M_finish); }
490
491       /**
492        *  Returns a read/write reverse iterator that points to the
493        *  last element in the %vector.  Iteration is done in reverse
494        *  element order.
495        */
496       reverse_iterator
497       rbegin()
498       { return reverse_iterator(end()); }
499
500       /**
501        *  Returns a read-only (constant) reverse iterator that points
502        *  to the last element in the %vector.  Iteration is done in
503        *  reverse element order.
504        */
505       const_reverse_iterator
506       rbegin() const
507       { return const_reverse_iterator(end()); }
508
509       /**
510        *  Returns a read/write reverse iterator that points to one
511        *  before the first element in the %vector.  Iteration is done
512        *  in reverse element order.
513        */
514       reverse_iterator
515       rend()
516       { return reverse_iterator(begin()); }
517
518       /**
519        *  Returns a read-only (constant) reverse iterator that points
520        *  to one before the first element in the %vector.  Iteration
521        *  is done in reverse element order.
522        */
523       const_reverse_iterator
524       rend() const
525       { return const_reverse_iterator(begin()); }
526
527 #ifdef __GXX_EXPERIMENTAL_CXX0X__
528       /**
529        *  Returns a read-only (constant) iterator that points to the
530        *  first element in the %vector.  Iteration is done in ordinary
531        *  element order.
532        */
533       const_iterator
534       cbegin() const
535       { return const_iterator(this->_M_impl._M_start); }
536
537       /**
538        *  Returns a read-only (constant) iterator that points one past
539        *  the last element in the %vector.  Iteration is done in
540        *  ordinary element order.
541        */
542       const_iterator
543       cend() const
544       { return const_iterator(this->_M_impl._M_finish); }
545
546       /**
547        *  Returns a read-only (constant) reverse iterator that points
548        *  to the last element in the %vector.  Iteration is done in
549        *  reverse element order.
550        */
551       const_reverse_iterator
552       crbegin() const
553       { return const_reverse_iterator(end()); }
554
555       /**
556        *  Returns a read-only (constant) reverse iterator that points
557        *  to one before the first element in the %vector.  Iteration
558        *  is done in reverse element order.
559        */
560       const_reverse_iterator
561       crend() const
562       { return const_reverse_iterator(begin()); }
563 #endif
564
565       // [23.2.4.2] capacity
566       /**  Returns the number of elements in the %vector.  */
567       size_type
568       size() const
569       { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
570
571       /**  Returns the size() of the largest possible %vector.  */
572       size_type
573       max_size() const
574       { return _M_get_Tp_allocator().max_size(); }
575
576 #ifdef __GXX_EXPERIMENTAL_CXX0X__
577       /**
578        *  @brief  Resizes the %vector to the specified number of elements.
579        *  @param  new_size  Number of elements the %vector should contain.
580        *
581        *  This function will %resize the %vector to the specified
582        *  number of elements.  If the number is smaller than the
583        *  %vector's current size the %vector is truncated, otherwise
584        *  default constructed elements are appended.
585        */
586       void
587       resize(size_type __new_size)
588       {
589         if (__new_size > size())
590           _M_default_append(__new_size - size());
591         else if (__new_size < size())
592           _M_erase_at_end(this->_M_impl._M_start + __new_size);
593       }
594
595       /**
596        *  @brief  Resizes the %vector to the specified number of elements.
597        *  @param  new_size  Number of elements the %vector should contain.
598        *  @param  x  Data with which new elements should be populated.
599        *
600        *  This function will %resize the %vector to the specified
601        *  number of elements.  If the number is smaller than the
602        *  %vector's current size the %vector is truncated, otherwise
603        *  the %vector is extended and new elements are populated with
604        *  given data.
605        */
606       void
607       resize(size_type __new_size, const value_type& __x)
608       {
609         if (__new_size > size())
610           insert(end(), __new_size - size(), __x);
611         else if (__new_size < size())
612           _M_erase_at_end(this->_M_impl._M_start + __new_size);
613       }
614 #else
615       /**
616        *  @brief  Resizes the %vector to the specified number of elements.
617        *  @param  new_size  Number of elements the %vector should contain.
618        *  @param  x  Data with which new elements should be populated.
619        *
620        *  This function will %resize the %vector to the specified
621        *  number of elements.  If the number is smaller than the
622        *  %vector's current size the %vector is truncated, otherwise
623        *  the %vector is extended and new elements are populated with
624        *  given data.
625        */
626       void
627       resize(size_type __new_size, value_type __x = value_type())
628       {
629         if (__new_size > size())
630           insert(end(), __new_size - size(), __x);
631         else if (__new_size < size())
632           _M_erase_at_end(this->_M_impl._M_start + __new_size);
633       }
634 #endif
635
636 #ifdef __GXX_EXPERIMENTAL_CXX0X__
637       /**  A non-binding request to reduce capacity() to size().  */
638       void
639       shrink_to_fit()
640       { std::__shrink_to_fit<vector>::_S_do_it(*this); }
641 #endif
642
643       /**
644        *  Returns the total number of elements that the %vector can
645        *  hold before needing to allocate more memory.
646        */
647       size_type
648       capacity() const
649       { return size_type(this->_M_impl._M_end_of_storage
650                          - this->_M_impl._M_start); }
651
652       /**
653        *  Returns true if the %vector is empty.  (Thus begin() would
654        *  equal end().)
655        */
656       bool
657       empty() const
658       { return begin() == end(); }
659
660       /**
661        *  @brief  Attempt to preallocate enough memory for specified number of
662        *          elements.
663        *  @param  n  Number of elements required.
664        *  @throw  std::length_error  If @a n exceeds @c max_size().
665        *
666        *  This function attempts to reserve enough memory for the
667        *  %vector to hold the specified number of elements.  If the
668        *  number requested is more than max_size(), length_error is
669        *  thrown.
670        *
671        *  The advantage of this function is that if optimal code is a
672        *  necessity and the user can determine the number of elements
673        *  that will be required, the user can reserve the memory in
674        *  %advance, and thus prevent a possible reallocation of memory
675        *  and copying of %vector data.
676        */
677       void
678       reserve(size_type __n);
679
680       // element access
681       /**
682        *  @brief  Subscript access to the data contained in the %vector.
683        *  @param n The index of the element for which data should be
684        *  accessed.
685        *  @return  Read/write reference to data.
686        *
687        *  This operator allows for easy, array-style, data access.
688        *  Note that data access with this operator is unchecked and
689        *  out_of_range lookups are not defined. (For checked lookups
690        *  see at().)
691        */
692       reference
693       operator[](size_type __n)
694       { return *(this->_M_impl._M_start + __n); }
695
696       /**
697        *  @brief  Subscript access to the data contained in the %vector.
698        *  @param n The index of the element for which data should be
699        *  accessed.
700        *  @return  Read-only (constant) reference to data.
701        *
702        *  This operator allows for easy, array-style, data access.
703        *  Note that data access with this operator is unchecked and
704        *  out_of_range lookups are not defined. (For checked lookups
705        *  see at().)
706        */
707       const_reference
708       operator[](size_type __n) const
709       { return *(this->_M_impl._M_start + __n); }
710
711     protected:
712       /// Safety check used only from at().
713       void
714       _M_range_check(size_type __n) const
715       {
716         if (__n >= this->size())
717           __throw_out_of_range(__N("vector::_M_range_check"));
718       }
719
720     public:
721       /**
722        *  @brief  Provides access to the data contained in the %vector.
723        *  @param n The index of the element for which data should be
724        *  accessed.
725        *  @return  Read/write reference to data.
726        *  @throw  std::out_of_range  If @a n is an invalid index.
727        *
728        *  This function provides for safer data access.  The parameter
729        *  is first checked that it is in the range of the vector.  The
730        *  function throws out_of_range if the check fails.
731        */
732       reference
733       at(size_type __n)
734       {
735         _M_range_check(__n);
736         return (*this)[__n]; 
737       }
738
739       /**
740        *  @brief  Provides access to the data contained in the %vector.
741        *  @param n The index of the element for which data should be
742        *  accessed.
743        *  @return  Read-only (constant) reference to data.
744        *  @throw  std::out_of_range  If @a n is an invalid index.
745        *
746        *  This function provides for safer data access.  The parameter
747        *  is first checked that it is in the range of the vector.  The
748        *  function throws out_of_range if the check fails.
749        */
750       const_reference
751       at(size_type __n) const
752       {
753         _M_range_check(__n);
754         return (*this)[__n];
755       }
756
757       /**
758        *  Returns a read/write reference to the data at the first
759        *  element of the %vector.
760        */
761       reference
762       front()
763       { return *begin(); }
764
765       /**
766        *  Returns a read-only (constant) reference to the data at the first
767        *  element of the %vector.
768        */
769       const_reference
770       front() const
771       { return *begin(); }
772
773       /**
774        *  Returns a read/write reference to the data at the last
775        *  element of the %vector.
776        */
777       reference
778       back()
779       { return *(end() - 1); }
780       
781       /**
782        *  Returns a read-only (constant) reference to the data at the
783        *  last element of the %vector.
784        */
785       const_reference
786       back() const
787       { return *(end() - 1); }
788
789       // _GLIBCXX_RESOLVE_LIB_DEFECTS
790       // DR 464. Suggestion for new member functions in standard containers.
791       // data access
792       /**
793        *   Returns a pointer such that [data(), data() + size()) is a valid
794        *   range.  For a non-empty %vector, data() == &front().
795        */
796 #ifdef __GXX_EXPERIMENTAL_CXX0X__
797       _Tp*
798 #else
799       pointer
800 #endif
801       data()
802       { return std::__addressof(front()); }
803
804 #ifdef __GXX_EXPERIMENTAL_CXX0X__
805       const _Tp*
806 #else
807       const_pointer
808 #endif
809       data() const
810       { return std::__addressof(front()); }
811
812       // [23.2.4.3] modifiers
813       /**
814        *  @brief  Add data to the end of the %vector.
815        *  @param  x  Data to be added.
816        *
817        *  This is a typical stack operation.  The function creates an
818        *  element at the end of the %vector and assigns the given data
819        *  to it.  Due to the nature of a %vector this operation can be
820        *  done in constant time if the %vector has preallocated space
821        *  available.
822        */
823       void
824       push_back(const value_type& __x)
825       {
826         if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
827           {
828             this->_M_impl.construct(this->_M_impl._M_finish, __x);
829             ++this->_M_impl._M_finish;
830           }
831         else
832           _M_insert_aux(end(), __x);
833       }
834
835 #ifdef __GXX_EXPERIMENTAL_CXX0X__
836       void
837       push_back(value_type&& __x)
838       { emplace_back(std::move(__x)); }
839
840       template<typename... _Args>
841         void
842         emplace_back(_Args&&... __args);
843 #endif
844
845       /**
846        *  @brief  Removes last element.
847        *
848        *  This is a typical stack operation. It shrinks the %vector by one.
849        *
850        *  Note that no data is returned, and if the last element's
851        *  data is needed, it should be retrieved before pop_back() is
852        *  called.
853        */
854       void
855       pop_back()
856       {
857         --this->_M_impl._M_finish;
858         this->_M_impl.destroy(this->_M_impl._M_finish);
859       }
860
861 #ifdef __GXX_EXPERIMENTAL_CXX0X__
862       /**
863        *  @brief  Inserts an object in %vector before specified iterator.
864        *  @param  position  An iterator into the %vector.
865        *  @param  args  Arguments.
866        *  @return  An iterator that points to the inserted data.
867        *
868        *  This function will insert an object of type T constructed
869        *  with T(std::forward<Args>(args)...) before the specified location.
870        *  Note that this kind of operation could be expensive for a %vector
871        *  and if it is frequently used the user should consider using
872        *  std::list.
873        */
874       template<typename... _Args>
875         iterator
876         emplace(iterator __position, _Args&&... __args);
877 #endif
878
879       /**
880        *  @brief  Inserts given value into %vector before specified iterator.
881        *  @param  position  An iterator into the %vector.
882        *  @param  x  Data to be inserted.
883        *  @return  An iterator that points to the inserted data.
884        *
885        *  This function will insert a copy of the given value before
886        *  the specified location.  Note that this kind of operation
887        *  could be expensive for a %vector and if it is frequently
888        *  used the user should consider using std::list.
889        */
890       iterator
891       insert(iterator __position, const value_type& __x);
892
893 #ifdef __GXX_EXPERIMENTAL_CXX0X__
894       /**
895        *  @brief  Inserts given rvalue into %vector before specified iterator.
896        *  @param  position  An iterator into the %vector.
897        *  @param  x  Data to be inserted.
898        *  @return  An iterator that points to the inserted data.
899        *
900        *  This function will insert a copy of the given rvalue before
901        *  the specified location.  Note that this kind of operation
902        *  could be expensive for a %vector and if it is frequently
903        *  used the user should consider using std::list.
904        */
905       iterator
906       insert(iterator __position, value_type&& __x)
907       { return emplace(__position, std::move(__x)); }
908
909       /**
910        *  @brief  Inserts an initializer_list into the %vector.
911        *  @param  position  An iterator into the %vector.
912        *  @param  l  An initializer_list.
913        *
914        *  This function will insert copies of the data in the 
915        *  initializer_list @a l into the %vector before the location
916        *  specified by @a position.
917        *
918        *  Note that this kind of operation could be expensive for a
919        *  %vector and if it is frequently used the user should
920        *  consider using std::list.
921        */
922       void
923       insert(iterator __position, initializer_list<value_type> __l)
924       { this->insert(__position, __l.begin(), __l.end()); }
925 #endif
926
927       /**
928        *  @brief  Inserts a number of copies of given data into the %vector.
929        *  @param  position  An iterator into the %vector.
930        *  @param  n  Number of elements to be inserted.
931        *  @param  x  Data to be inserted.
932        *
933        *  This function will insert a specified number of copies of
934        *  the given data before the location specified by @a position.
935        *
936        *  Note that this kind of operation could be expensive for a
937        *  %vector and if it is frequently used the user should
938        *  consider using std::list.
939        */
940       void
941       insert(iterator __position, size_type __n, const value_type& __x)
942       { _M_fill_insert(__position, __n, __x); }
943
944       /**
945        *  @brief  Inserts a range into the %vector.
946        *  @param  position  An iterator into the %vector.
947        *  @param  first  An input iterator.
948        *  @param  last   An input iterator.
949        *
950        *  This function will insert copies of the data in the range
951        *  [first,last) into the %vector before the location specified
952        *  by @a pos.
953        *
954        *  Note that this kind of operation could be expensive for a
955        *  %vector and if it is frequently used the user should
956        *  consider using std::list.
957        */
958       template<typename _InputIterator>
959         void
960         insert(iterator __position, _InputIterator __first,
961                _InputIterator __last)
962         {
963           // Check whether it's an integral type.  If so, it's not an iterator.
964           typedef typename std::__is_integer<_InputIterator>::__type _Integral;
965           _M_insert_dispatch(__position, __first, __last, _Integral());
966         }
967
968       /**
969        *  @brief  Remove element at given position.
970        *  @param  position  Iterator pointing to element to be erased.
971        *  @return  An iterator pointing to the next element (or end()).
972        *
973        *  This function will erase the element at the given position and thus
974        *  shorten the %vector by one.
975        *
976        *  Note This operation could be expensive and if it is
977        *  frequently used the user should consider using std::list.
978        *  The user is also cautioned that this function only erases
979        *  the element, and that if the element is itself a pointer,
980        *  the pointed-to memory is not touched in any way.  Managing
981        *  the pointer is the user's responsibility.
982        */
983       iterator
984       erase(iterator __position);
985
986       /**
987        *  @brief  Remove a range of elements.
988        *  @param  first  Iterator pointing to the first element to be erased.
989        *  @param  last  Iterator pointing to one past the last element to be
990        *                erased.
991        *  @return  An iterator pointing to the element pointed to by @a last
992        *           prior to erasing (or end()).
993        *
994        *  This function will erase the elements in the range [first,last) and
995        *  shorten the %vector accordingly.
996        *
997        *  Note This operation could be expensive and if it is
998        *  frequently used the user should consider using std::list.
999        *  The user is also cautioned that this function only erases
1000        *  the elements, and that if the elements themselves are
1001        *  pointers, the pointed-to memory is not touched in any way.
1002        *  Managing the pointer is the user's responsibility.
1003        */
1004       iterator
1005       erase(iterator __first, iterator __last);
1006
1007       /**
1008        *  @brief  Swaps data with another %vector.
1009        *  @param  x  A %vector of the same element and allocator types.
1010        *
1011        *  This exchanges the elements between two vectors in constant time.
1012        *  (Three pointers, so it should be quite fast.)
1013        *  Note that the global std::swap() function is specialized such that
1014        *  std::swap(v1,v2) will feed to this function.
1015        */
1016       void
1017       swap(vector& __x)
1018       {
1019         std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
1020         std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
1021         std::swap(this->_M_impl._M_end_of_storage,
1022                   __x._M_impl._M_end_of_storage);
1023
1024         // _GLIBCXX_RESOLVE_LIB_DEFECTS
1025         // 431. Swapping containers with unequal allocators.
1026         std::__alloc_swap<_Tp_alloc_type>::_S_do_it(_M_get_Tp_allocator(),
1027                                                     __x._M_get_Tp_allocator());
1028       }
1029
1030       /**
1031        *  Erases all the elements.  Note that this function only erases the
1032        *  elements, and that if the elements themselves are pointers, the
1033        *  pointed-to memory is not touched in any way.  Managing the pointer is
1034        *  the user's responsibility.
1035        */
1036       void
1037       clear()
1038       { _M_erase_at_end(this->_M_impl._M_start); }
1039
1040     protected:
1041       /**
1042        *  Memory expansion handler.  Uses the member allocation function to
1043        *  obtain @a n bytes of memory, and then copies [first,last) into it.
1044        */
1045       template<typename _ForwardIterator>
1046         pointer
1047         _M_allocate_and_copy(size_type __n,
1048                              _ForwardIterator __first, _ForwardIterator __last)
1049         {
1050           pointer __result = this->_M_allocate(__n);
1051           __try
1052             {
1053               std::__uninitialized_copy_a(__first, __last, __result,
1054                                           _M_get_Tp_allocator());
1055               return __result;
1056             }
1057           __catch(...)
1058             {
1059               _M_deallocate(__result, __n);
1060               __throw_exception_again;
1061             }
1062         }
1063
1064
1065       // Internal constructor functions follow.
1066
1067       // Called by the range constructor to implement [23.1.1]/9
1068
1069       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1070       // 438. Ambiguity in the "do the right thing" clause
1071       template<typename _Integer>
1072         void
1073         _M_initialize_dispatch(_Integer __n, _Integer __value, __true_type)
1074         {
1075           this->_M_impl._M_start = _M_allocate(static_cast<size_type>(__n));
1076           this->_M_impl._M_end_of_storage =
1077             this->_M_impl._M_start + static_cast<size_type>(__n);
1078           _M_fill_initialize(static_cast<size_type>(__n), __value);
1079         }
1080
1081       // Called by the range constructor to implement [23.1.1]/9
1082       template<typename _InputIterator>
1083         void
1084         _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1085                                __false_type)
1086         {
1087           typedef typename std::iterator_traits<_InputIterator>::
1088             iterator_category _IterCategory;
1089           _M_range_initialize(__first, __last, _IterCategory());
1090         }
1091
1092       // Called by the second initialize_dispatch above
1093       template<typename _InputIterator>
1094         void
1095         _M_range_initialize(_InputIterator __first,
1096                             _InputIterator __last, std::input_iterator_tag)
1097         {
1098           for (; __first != __last; ++__first)
1099             push_back(*__first);
1100         }
1101
1102       // Called by the second initialize_dispatch above
1103       template<typename _ForwardIterator>
1104         void
1105         _M_range_initialize(_ForwardIterator __first,
1106                             _ForwardIterator __last, std::forward_iterator_tag)
1107         {
1108           const size_type __n = std::distance(__first, __last);
1109           this->_M_impl._M_start = this->_M_allocate(__n);
1110           this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
1111           this->_M_impl._M_finish =
1112             std::__uninitialized_copy_a(__first, __last,
1113                                         this->_M_impl._M_start,
1114                                         _M_get_Tp_allocator());
1115         }
1116
1117       // Called by the first initialize_dispatch above and by the
1118       // vector(n,value,a) constructor.
1119       void
1120       _M_fill_initialize(size_type __n, const value_type& __value)
1121       {
1122         std::__uninitialized_fill_n_a(this->_M_impl._M_start, __n, __value, 
1123                                       _M_get_Tp_allocator());
1124         this->_M_impl._M_finish = this->_M_impl._M_end_of_storage;
1125       }
1126
1127 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1128       // Called by the vector(n) constructor.
1129       void
1130       _M_default_initialize(size_type __n)
1131       {
1132         std::__uninitialized_default_n_a(this->_M_impl._M_start, __n, 
1133                                          _M_get_Tp_allocator());
1134         this->_M_impl._M_finish = this->_M_impl._M_end_of_storage;
1135       }
1136 #endif
1137
1138       // Internal assign functions follow.  The *_aux functions do the actual
1139       // assignment work for the range versions.
1140
1141       // Called by the range assign to implement [23.1.1]/9
1142
1143       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1144       // 438. Ambiguity in the "do the right thing" clause
1145       template<typename _Integer>
1146         void
1147         _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1148         { _M_fill_assign(__n, __val); }
1149
1150       // Called by the range assign to implement [23.1.1]/9
1151       template<typename _InputIterator>
1152         void
1153         _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1154                            __false_type)
1155         {
1156           typedef typename std::iterator_traits<_InputIterator>::
1157             iterator_category _IterCategory;
1158           _M_assign_aux(__first, __last, _IterCategory());
1159         }
1160
1161       // Called by the second assign_dispatch above
1162       template<typename _InputIterator>
1163         void
1164         _M_assign_aux(_InputIterator __first, _InputIterator __last,
1165                       std::input_iterator_tag);
1166
1167       // Called by the second assign_dispatch above
1168       template<typename _ForwardIterator>
1169         void
1170         _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1171                       std::forward_iterator_tag);
1172
1173       // Called by assign(n,t), and the range assign when it turns out
1174       // to be the same thing.
1175       void
1176       _M_fill_assign(size_type __n, const value_type& __val);
1177
1178
1179       // Internal insert functions follow.
1180
1181       // Called by the range insert to implement [23.1.1]/9
1182
1183       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1184       // 438. Ambiguity in the "do the right thing" clause
1185       template<typename _Integer>
1186         void
1187         _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
1188                            __true_type)
1189         { _M_fill_insert(__pos, __n, __val); }
1190
1191       // Called by the range insert to implement [23.1.1]/9
1192       template<typename _InputIterator>
1193         void
1194         _M_insert_dispatch(iterator __pos, _InputIterator __first,
1195                            _InputIterator __last, __false_type)
1196         {
1197           typedef typename std::iterator_traits<_InputIterator>::
1198             iterator_category _IterCategory;
1199           _M_range_insert(__pos, __first, __last, _IterCategory());
1200         }
1201
1202       // Called by the second insert_dispatch above
1203       template<typename _InputIterator>
1204         void
1205         _M_range_insert(iterator __pos, _InputIterator __first,
1206                         _InputIterator __last, std::input_iterator_tag);
1207
1208       // Called by the second insert_dispatch above
1209       template<typename _ForwardIterator>
1210         void
1211         _M_range_insert(iterator __pos, _ForwardIterator __first,
1212                         _ForwardIterator __last, std::forward_iterator_tag);
1213
1214       // Called by insert(p,n,x), and the range insert when it turns out to be
1215       // the same thing.
1216       void
1217       _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
1218
1219 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1220       // Called by resize(n).
1221       void
1222       _M_default_append(size_type __n);
1223 #endif
1224
1225       // Called by insert(p,x)
1226 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1227       void
1228       _M_insert_aux(iterator __position, const value_type& __x);
1229 #else
1230       template<typename... _Args>
1231         void
1232         _M_insert_aux(iterator __position, _Args&&... __args);
1233 #endif
1234
1235       // Called by the latter.
1236       size_type
1237       _M_check_len(size_type __n, const char* __s) const
1238       {
1239         if (max_size() - size() < __n)
1240           __throw_length_error(__N(__s));
1241
1242         const size_type __len = size() + std::max(size(), __n);
1243         return (__len < size() || __len > max_size()) ? max_size() : __len;
1244       }
1245
1246       // Internal erase functions follow.
1247
1248       // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
1249       // _M_assign_aux.
1250       void
1251       _M_erase_at_end(pointer __pos)
1252       {
1253         std::_Destroy(__pos, this->_M_impl._M_finish, _M_get_Tp_allocator());
1254         this->_M_impl._M_finish = __pos;
1255       }
1256     };
1257
1258
1259   /**
1260    *  @brief  Vector equality comparison.
1261    *  @param  x  A %vector.
1262    *  @param  y  A %vector of the same type as @a x.
1263    *  @return  True iff the size and elements of the vectors are equal.
1264    *
1265    *  This is an equivalence relation.  It is linear in the size of the
1266    *  vectors.  Vectors are considered equivalent if their sizes are equal,
1267    *  and if corresponding elements compare equal.
1268   */
1269   template<typename _Tp, typename _Alloc>
1270     inline bool
1271     operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1272     { return (__x.size() == __y.size()
1273               && std::equal(__x.begin(), __x.end(), __y.begin())); }
1274
1275   /**
1276    *  @brief  Vector ordering relation.
1277    *  @param  x  A %vector.
1278    *  @param  y  A %vector of the same type as @a x.
1279    *  @return  True iff @a x is lexicographically less than @a y.
1280    *
1281    *  This is a total ordering relation.  It is linear in the size of the
1282    *  vectors.  The elements must be comparable with @c <.
1283    *
1284    *  See std::lexicographical_compare() for how the determination is made.
1285   */
1286   template<typename _Tp, typename _Alloc>
1287     inline bool
1288     operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1289     { return std::lexicographical_compare(__x.begin(), __x.end(),
1290                                           __y.begin(), __y.end()); }
1291
1292   /// Based on operator==
1293   template<typename _Tp, typename _Alloc>
1294     inline bool
1295     operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1296     { return !(__x == __y); }
1297
1298   /// Based on operator<
1299   template<typename _Tp, typename _Alloc>
1300     inline bool
1301     operator>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1302     { return __y < __x; }
1303
1304   /// Based on operator<
1305   template<typename _Tp, typename _Alloc>
1306     inline bool
1307     operator<=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1308     { return !(__y < __x); }
1309
1310   /// Based on operator<
1311   template<typename _Tp, typename _Alloc>
1312     inline bool
1313     operator>=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1314     { return !(__x < __y); }
1315
1316   /// See std::vector::swap().
1317   template<typename _Tp, typename _Alloc>
1318     inline void
1319     swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>& __y)
1320     { __x.swap(__y); }
1321
1322 _GLIBCXX_END_NESTED_NAMESPACE
1323
1324 #endif /* _STL_VECTOR_H */