OSDN Git Service

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