OSDN Git Service

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