OSDN Git Service

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