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