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