stl_vector.h

Go to the documentation of this file.
00001 // Vector implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 2, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 /*
00032  *
00033  * Copyright (c) 1994
00034  * Hewlett-Packard Company
00035  *
00036  * Permission to use, copy, modify, distribute and sell this software
00037  * and its documentation for any purpose is hereby granted without fee,
00038  * provided that the above copyright notice appear in all copies and
00039  * that both that copyright notice and this permission notice appear
00040  * in supporting documentation.  Hewlett-Packard Company makes no
00041  * representations about the suitability of this software for any
00042  * purpose.  It is provided "as is" without express or implied warranty.
00043  *
00044  *
00045  * Copyright (c) 1996
00046  * Silicon Graphics Computer Systems, Inc.
00047  *
00048  * Permission to use, copy, modify, distribute and sell this software
00049  * and its documentation for any purpose is hereby granted without fee,
00050  * provided that the above copyright notice appear in all copies and
00051  * that both that copyright notice and this permission notice appear
00052  * in supporting documentation.  Silicon Graphics makes no
00053  * representations about the suitability of this  software for any
00054  * purpose.  It is provided "as is" without express or implied warranty.
00055  */
00056 
00057 /** @file stl_vector.h
00058  *  This is an internal header file, included by other library headers.
00059  *  You should not attempt to use it directly.
00060  */
00061 
00062 #ifndef _VECTOR_H
00063 #define _VECTOR_H 1
00064 
00065 #include <bits/stl_iterator_base_funcs.h>
00066 #include <bits/functexcept.h>
00067 #include <bits/concept_check.h>
00068 
00069 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD)
00070 
00071   /**
00072    *  @if maint
00073    *  See bits/stl_deque.h's _Deque_base for an explanation.
00074    *  @endif
00075   */
00076   template<typename _Tp, typename _Alloc>
00077     struct _Vector_base
00078     {
00079       typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
00080 
00081       struct _Vector_impl 
00082       : public _Tp_alloc_type
00083       {
00084     _Tp*           _M_start;
00085     _Tp*           _M_finish;
00086     _Tp*           _M_end_of_storage;
00087     _Vector_impl(_Tp_alloc_type const& __a)
00088     : _Tp_alloc_type(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0)
00089     { }
00090       };
00091       
00092     public:
00093       typedef _Alloc allocator_type;
00094 
00095       _Tp_alloc_type&
00096       _M_get_Tp_allocator()
00097       { return *static_cast<_Tp_alloc_type*>(&this->_M_impl); }
00098 
00099       const _Tp_alloc_type&
00100       _M_get_Tp_allocator() const
00101       { return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); }
00102 
00103       allocator_type
00104       get_allocator() const
00105       { return allocator_type(_M_get_Tp_allocator()); }
00106 
00107       _Vector_base(const allocator_type& __a)
00108       : _M_impl(__a)
00109       { }
00110 
00111       _Vector_base(size_t __n, const allocator_type& __a)
00112       : _M_impl(__a)
00113       {
00114     this->_M_impl._M_start = this->_M_allocate(__n);
00115     this->_M_impl._M_finish = this->_M_impl._M_start;
00116     this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
00117       }
00118 
00119       ~_Vector_base()
00120       { _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage
00121               - this->_M_impl._M_start); }
00122 
00123     public:
00124       _Vector_impl _M_impl;
00125 
00126       _Tp*
00127       _M_allocate(size_t __n)
00128       { return _M_impl.allocate(__n); }
00129 
00130       void
00131       _M_deallocate(_Tp* __p, size_t __n)
00132       {
00133     if (__p)
00134       _M_impl.deallocate(__p, __n);
00135       }
00136     };
00137 
00138 
00139   /**
00140    *  @brief A standard container which offers fixed time access to
00141    *  individual elements in any order.
00142    *
00143    *  @ingroup Containers
00144    *  @ingroup Sequences
00145    *
00146    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
00147    *  <a href="tables.html#66">reversible container</a>, and a
00148    *  <a href="tables.html#67">sequence</a>, including the
00149    *  <a href="tables.html#68">optional sequence requirements</a> with the
00150    *  %exception of @c push_front and @c pop_front.
00151    *
00152    *  In some terminology a %vector can be described as a dynamic
00153    *  C-style array, it offers fast and efficient access to individual
00154    *  elements in any order and saves the user from worrying about
00155    *  memory and size allocation.  Subscripting ( @c [] ) access is
00156    *  also provided as with C-style arrays.
00157   */
00158   template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
00159     class vector : protected _Vector_base<_Tp, _Alloc>
00160     {
00161       // Concept requirements.
00162       typedef typename _Alloc::value_type                _Alloc_value_type;
00163       __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
00164       __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
00165       
00166       typedef _Vector_base<_Tp, _Alloc>          _Base;
00167       typedef vector<_Tp, _Alloc>            vector_type;
00168       typedef typename _Base::_Tp_alloc_type         _Tp_alloc_type;
00169 
00170     public:
00171       typedef _Tp                    value_type;
00172       typedef typename _Tp_alloc_type::pointer           pointer;
00173       typedef typename _Tp_alloc_type::const_pointer     const_pointer;
00174       typedef typename _Tp_alloc_type::reference         reference;
00175       typedef typename _Tp_alloc_type::const_reference   const_reference;
00176       typedef __gnu_cxx::__normal_iterator<pointer, vector_type> iterator;
00177       typedef __gnu_cxx::__normal_iterator<const_pointer, vector_type>
00178       const_iterator;
00179       typedef std::reverse_iterator<const_iterator>  const_reverse_iterator;
00180       typedef std::reverse_iterator<iterator>        reverse_iterator;
00181       typedef size_t                     size_type;
00182       typedef ptrdiff_t                  difference_type;
00183       typedef _Alloc                                 allocator_type;
00184 
00185     protected:
00186       using _Base::_M_allocate;
00187       using _Base::_M_deallocate;
00188       using _Base::_M_impl;
00189       using _Base::_M_get_Tp_allocator;
00190 
00191     public:
00192       // [23.2.4.1] construct/copy/destroy
00193       // (assign() and get_allocator() are also listed in this section)
00194       /**
00195        *  @brief  Default constructor creates no elements.
00196        */
00197       explicit
00198       vector(const allocator_type& __a = allocator_type())
00199       : _Base(__a)
00200       { }
00201 
00202       /**
00203        *  @brief  Create a %vector with copies of an exemplar element.
00204        *  @param  n  The number of elements to initially create.
00205        *  @param  value  An element to copy.
00206        *
00207        *  This constructor fills the %vector with @a n copies of @a value.
00208        */
00209       explicit
00210       vector(size_type __n, const value_type& __value = value_type(),
00211          const allocator_type& __a = allocator_type())
00212       : _Base(__n, __a)
00213       {
00214     std::__uninitialized_fill_n_a(this->_M_impl._M_start, __n, __value,
00215                       _M_get_Tp_allocator());
00216     this->_M_impl._M_finish = this->_M_impl._M_start + __n;
00217       }
00218 
00219       /**
00220        *  @brief  %Vector copy constructor.
00221        *  @param  x  A %vector of identical element and allocator types.
00222        *
00223        *  The newly-created %vector uses a copy of the allocation
00224        *  object used by @a x.  All the elements of @a x are copied,
00225        *  but any extra memory in
00226        *  @a x (for fast expansion) will not be copied.
00227        */
00228       vector(const vector& __x)
00229       : _Base(__x.size(), __x._M_get_Tp_allocator())
00230       { this->_M_impl._M_finish =
00231       std::__uninitialized_copy_a(__x.begin(), __x.end(),
00232                       this->_M_impl._M_start,
00233                       _M_get_Tp_allocator());
00234       }
00235 
00236       /**
00237        *  @brief  Builds a %vector from a range.
00238        *  @param  first  An input iterator.
00239        *  @param  last  An input iterator.
00240        *
00241        *  Create a %vector consisting of copies of the elements from
00242        *  [first,last).
00243        *
00244        *  If the iterators are forward, bidirectional, or
00245        *  random-access, then this will call the elements' copy
00246        *  constructor N times (where N is distance(first,last)) and do
00247        *  no memory reallocation.  But if only input iterators are
00248        *  used, then this will do at most 2N calls to the copy
00249        *  constructor, and logN memory reallocations.
00250        */
00251       template<typename _InputIterator>
00252         vector(_InputIterator __first, _InputIterator __last,
00253            const allocator_type& __a = allocator_type())
00254     : _Base(__a)
00255         {
00256       // Check whether it's an integral type.  If so, it's not an iterator.
00257       typedef typename std::__is_integer<_InputIterator>::__type _Integral;
00258       _M_initialize_dispatch(__first, __last, _Integral());
00259     }
00260 
00261       /**
00262        *  The dtor only erases the elements, and note that if the
00263        *  elements themselves are pointers, the pointed-to memory is
00264        *  not touched in any way.  Managing the pointer is the user's
00265        *  responsibilty.
00266        */
00267       ~vector()
00268       { std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
00269               _M_get_Tp_allocator()); }
00270 
00271       /**
00272        *  @brief  %Vector assignment operator.
00273        *  @param  x  A %vector of identical element and allocator types.
00274        *
00275        *  All the elements of @a x are copied, but any extra memory in
00276        *  @a x (for fast expansion) will not be copied.  Unlike the
00277        *  copy constructor, the allocator object is not copied.
00278        */
00279       vector&
00280       operator=(const vector& __x);
00281 
00282       /**
00283        *  @brief  Assigns a given value to a %vector.
00284        *  @param  n  Number of elements to be assigned.
00285        *  @param  val  Value to be assigned.
00286        *
00287        *  This function fills a %vector with @a n copies of the given
00288        *  value.  Note that the assignment completely changes the
00289        *  %vector and that the resulting %vector's size is the same as
00290        *  the number of elements assigned.  Old data may be lost.
00291        */
00292       void
00293       assign(size_type __n, const value_type& __val)
00294       { _M_fill_assign(__n, __val); }
00295 
00296       /**
00297        *  @brief  Assigns a range to a %vector.
00298        *  @param  first  An input iterator.
00299        *  @param  last   An input iterator.
00300        *
00301        *  This function fills a %vector with copies of the elements in the
00302        *  range [first,last).
00303        *
00304        *  Note that the assignment completely changes the %vector and
00305        *  that the resulting %vector's size is the same as the number
00306        *  of elements assigned.  Old data may be lost.
00307        */
00308       template<typename _InputIterator>
00309         void
00310         assign(_InputIterator __first, _InputIterator __last)
00311         {
00312       // Check whether it's an integral type.  If so, it's not an iterator.
00313       typedef typename std::__is_integer<_InputIterator>::__type _Integral;
00314       _M_assign_dispatch(__first, __last, _Integral());
00315     }
00316 
00317       /// Get a copy of the memory allocation object.
00318       using _Base::get_allocator;
00319 
00320       // iterators
00321       /**
00322        *  Returns a read/write iterator that points to the first
00323        *  element in the %vector.  Iteration is done in ordinary
00324        *  element order.
00325        */
00326       iterator
00327       begin()
00328       { return iterator(this->_M_impl._M_start); }
00329 
00330       /**
00331        *  Returns a read-only (constant) iterator that points to the
00332        *  first element in the %vector.  Iteration is done in ordinary
00333        *  element order.
00334        */
00335       const_iterator
00336       begin() const
00337       { return const_iterator(this->_M_impl._M_start); }
00338 
00339       /**
00340        *  Returns a read/write iterator that points one past the last
00341        *  element in the %vector.  Iteration is done in ordinary
00342        *  element order.
00343        */
00344       iterator
00345       end()
00346       { return iterator(this->_M_impl._M_finish); }
00347 
00348       /**
00349        *  Returns a read-only (constant) iterator that points one past
00350        *  the last element in the %vector.  Iteration is done in
00351        *  ordinary element order.
00352        */
00353       const_iterator
00354       end() const
00355       { return const_iterator(this->_M_impl._M_finish); }
00356 
00357       /**
00358        *  Returns a read/write reverse iterator that points to the
00359        *  last element in the %vector.  Iteration is done in reverse
00360        *  element order.
00361        */
00362       reverse_iterator
00363       rbegin()
00364       { return reverse_iterator(end()); }
00365 
00366       /**
00367        *  Returns a read-only (constant) reverse iterator that points
00368        *  to the last element in the %vector.  Iteration is done in
00369        *  reverse element order.
00370        */
00371       const_reverse_iterator
00372       rbegin() const
00373       { return const_reverse_iterator(end()); }
00374 
00375       /**
00376        *  Returns a read/write reverse iterator that points to one
00377        *  before the first element in the %vector.  Iteration is done
00378        *  in reverse element order.
00379        */
00380       reverse_iterator
00381       rend()
00382       { return reverse_iterator(begin()); }
00383 
00384       /**
00385        *  Returns a read-only (constant) reverse iterator that points
00386        *  to one before the first element in the %vector.  Iteration
00387        *  is done in reverse element order.
00388        */
00389       const_reverse_iterator
00390       rend() const
00391       { return const_reverse_iterator(begin()); }
00392 
00393       // [23.2.4.2] capacity
00394       /**  Returns the number of elements in the %vector.  */
00395       size_type
00396       size() const
00397       { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
00398 
00399       /**  Returns the size() of the largest possible %vector.  */
00400       size_type
00401       max_size() const
00402       { return _M_get_Tp_allocator().max_size(); }
00403 
00404       /**
00405        *  @brief  Resizes the %vector to the specified number of elements.
00406        *  @param  new_size  Number of elements the %vector should contain.
00407        *  @param  x  Data with which new elements should be populated.
00408        *
00409        *  This function will %resize the %vector to the specified
00410        *  number of elements.  If the number is smaller than the
00411        *  %vector's current size the %vector is truncated, otherwise
00412        *  the %vector is extended and new elements are populated with
00413        *  given data.
00414        */
00415       void
00416       resize(size_type __new_size, value_type __x = value_type())
00417       {
00418     if (__new_size < size())
00419       _M_erase_at_end(this->_M_impl._M_start + __new_size);
00420     else
00421       insert(end(), __new_size - size(), __x);
00422       }
00423 
00424       /**
00425        *  Returns the total number of elements that the %vector can
00426        *  hold before needing to allocate more memory.
00427        */
00428       size_type
00429       capacity() const
00430       { return size_type(this->_M_impl._M_end_of_storage
00431              - this->_M_impl._M_start); }
00432 
00433       /**
00434        *  Returns true if the %vector is empty.  (Thus begin() would
00435        *  equal end().)
00436        */
00437       bool
00438       empty() const
00439       { return begin() == end(); }
00440 
00441       /**
00442        *  @brief  Attempt to preallocate enough memory for specified number of
00443        *          elements.
00444        *  @param  n  Number of elements required.
00445        *  @throw  std::length_error  If @a n exceeds @c max_size().
00446        *
00447        *  This function attempts to reserve enough memory for the
00448        *  %vector to hold the specified number of elements.  If the
00449        *  number requested is more than max_size(), length_error is
00450        *  thrown.
00451        *
00452        *  The advantage of this function is that if optimal code is a
00453        *  necessity and the user can determine the number of elements
00454        *  that will be required, the user can reserve the memory in
00455        *  %advance, and thus prevent a possible reallocation of memory
00456        *  and copying of %vector data.
00457        */
00458       void
00459       reserve(size_type __n);
00460 
00461       // element access
00462       /**
00463        *  @brief  Subscript access to the data contained in the %vector.
00464        *  @param n The index of the element for which data should be
00465        *  accessed.
00466        *  @return  Read/write reference to data.
00467        *
00468        *  This operator allows for easy, array-style, data access.
00469        *  Note that data access with this operator is unchecked and
00470        *  out_of_range lookups are not defined. (For checked lookups
00471        *  see at().)
00472        */
00473       reference
00474       operator[](size_type __n)
00475       { return *(this->_M_impl._M_start + __n); }
00476 
00477       /**
00478        *  @brief  Subscript access to the data contained in the %vector.
00479        *  @param n The index of the element for which data should be
00480        *  accessed.
00481        *  @return  Read-only (constant) reference to data.
00482        *
00483        *  This operator allows for easy, array-style, data access.
00484        *  Note that data access with this operator is unchecked and
00485        *  out_of_range lookups are not defined. (For checked lookups
00486        *  see at().)
00487        */
00488       const_reference
00489       operator[](size_type __n) const
00490       { return *(this->_M_impl._M_start + __n); }
00491 
00492     protected:
00493       /// @if maint Safety check used only from at().  @endif
00494       void
00495       _M_range_check(size_type __n) const
00496       {
00497     if (__n >= this->size())
00498       __throw_out_of_range(__N("vector::_M_range_check"));
00499       }
00500 
00501     public:
00502       /**
00503        *  @brief  Provides access to the data contained in the %vector.
00504        *  @param n The index of the element for which data should be
00505        *  accessed.
00506        *  @return  Read/write reference to data.
00507        *  @throw  std::out_of_range  If @a n is an invalid index.
00508        *
00509        *  This function provides for safer data access.  The parameter
00510        *  is first checked that it is in the range of the vector.  The
00511        *  function throws out_of_range if the check fails.
00512        */
00513       reference
00514       at(size_type __n)
00515       {
00516     _M_range_check(__n);
00517     return (*this)[__n]; 
00518       }
00519 
00520       /**
00521        *  @brief  Provides access to the data contained in the %vector.
00522        *  @param n The index of the element for which data should be
00523        *  accessed.
00524        *  @return  Read-only (constant) reference to data.
00525        *  @throw  std::out_of_range  If @a n is an invalid index.
00526        *
00527        *  This function provides for safer data access.  The parameter
00528        *  is first checked that it is in the range of the vector.  The
00529        *  function throws out_of_range if the check fails.
00530        */
00531       const_reference
00532       at(size_type __n) const
00533       {
00534     _M_range_check(__n);
00535     return (*this)[__n];
00536       }
00537 
00538       /**
00539        *  Returns a read/write reference to the data at the first
00540        *  element of the %vector.
00541        */
00542       reference
00543       front()
00544       { return *begin(); }
00545 
00546       /**
00547        *  Returns a read-only (constant) reference to the data at the first
00548        *  element of the %vector.
00549        */
00550       const_reference
00551       front() const
00552       { return *begin(); }
00553 
00554       /**
00555        *  Returns a read/write reference to the data at the last
00556        *  element of the %vector.
00557        */
00558       reference
00559       back()
00560       { return *(end() - 1); }
00561       
00562       /**
00563        *  Returns a read-only (constant) reference to the data at the
00564        *  last element of the %vector.
00565        */
00566       const_reference
00567       back() const
00568       { return *(end() - 1); }
00569 
00570       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00571       // DR 464. Suggestion for new member functions in standard containers.
00572       // data access
00573       /**
00574        *   Returns a pointer such that [data(), data() + size()) is a valid
00575        *   range.  For a non-empty %vector, data() == &front().
00576        */
00577       pointer
00578       data()
00579       { return pointer(this->_M_impl._M_start); }
00580 
00581       const_pointer
00582       data() const
00583       { return const_pointer(this->_M_impl._M_start); }
00584 
00585       // [23.2.4.3] modifiers
00586       /**
00587        *  @brief  Add data to the end of the %vector.
00588        *  @param  x  Data to be added.
00589        *
00590        *  This is a typical stack operation.  The function creates an
00591        *  element at the end of the %vector and assigns the given data
00592        *  to it.  Due to the nature of a %vector this operation can be
00593        *  done in constant time if the %vector has preallocated space
00594        *  available.
00595        */
00596       void
00597       push_back(const value_type& __x)
00598       {
00599     if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
00600       {
00601         this->_M_impl.construct(this->_M_impl._M_finish, __x);
00602         ++this->_M_impl._M_finish;
00603       }
00604     else
00605       _M_insert_aux(end(), __x);
00606       }
00607 
00608       /**
00609        *  @brief  Removes last element.
00610        *
00611        *  This is a typical stack operation. It shrinks the %vector by one.
00612        *
00613        *  Note that no data is returned, and if the last element's
00614        *  data is needed, it should be retrieved before pop_back() is
00615        *  called.
00616        */
00617       void
00618       pop_back()
00619       {
00620     --this->_M_impl._M_finish;
00621     this->_M_impl.destroy(this->_M_impl._M_finish);
00622       }
00623 
00624       /**
00625        *  @brief  Inserts given value into %vector before specified iterator.
00626        *  @param  position  An iterator into the %vector.
00627        *  @param  x  Data to be inserted.
00628        *  @return  An iterator that points to the inserted data.
00629        *
00630        *  This function will insert a copy of the given value before
00631        *  the specified location.  Note that this kind of operation
00632        *  could be expensive for a %vector and if it is frequently
00633        *  used the user should consider using std::list.
00634        */
00635       iterator
00636       insert(iterator __position, const value_type& __x);
00637 
00638       /**
00639        *  @brief  Inserts a number of copies of given data into the %vector.
00640        *  @param  position  An iterator into the %vector.
00641        *  @param  n  Number of elements to be inserted.
00642        *  @param  x  Data to be inserted.
00643        *
00644        *  This function will insert a specified number of copies of
00645        *  the given data before the location specified by @a position.
00646        *
00647        *  Note that this kind of operation could be expensive for a
00648        *  %vector and if it is frequently used the user should
00649        *  consider using std::list.
00650        */
00651       void
00652       insert(iterator __position, size_type __n, const value_type& __x)
00653       { _M_fill_insert(__position, __n, __x); }
00654 
00655       /**
00656        *  @brief  Inserts a range into the %vector.
00657        *  @param  position  An iterator into the %vector.
00658        *  @param  first  An input iterator.
00659        *  @param  last   An input iterator.
00660        *
00661        *  This function will insert copies of the data in the range
00662        *  [first,last) into the %vector before the location specified
00663        *  by @a pos.
00664        *
00665        *  Note that this kind of operation could be expensive for a
00666        *  %vector and if it is frequently used the user should
00667        *  consider using std::list.
00668        */
00669       template<typename _InputIterator>
00670         void
00671         insert(iterator __position, _InputIterator __first,
00672            _InputIterator __last)
00673         {
00674       // Check whether it's an integral type.  If so, it's not an iterator.
00675       typedef typename std::__is_integer<_InputIterator>::__type _Integral;
00676       _M_insert_dispatch(__position, __first, __last, _Integral());
00677     }
00678 
00679       /**
00680        *  @brief  Remove element at given position.
00681        *  @param  position  Iterator pointing to element to be erased.
00682        *  @return  An iterator pointing to the next element (or end()).
00683        *
00684        *  This function will erase the element at the given position and thus
00685        *  shorten the %vector by one.
00686        *
00687        *  Note This operation could be expensive and if it is
00688        *  frequently used the user should consider using std::list.
00689        *  The user is also cautioned that this function only erases
00690        *  the element, and that if the element is itself a pointer,
00691        *  the pointed-to memory is not touched in any way.  Managing
00692        *  the pointer is the user's responsibilty.
00693        */
00694       iterator
00695       erase(iterator __position);
00696 
00697       /**
00698        *  @brief  Remove a range of elements.
00699        *  @param  first  Iterator pointing to the first element to be erased.
00700        *  @param  last  Iterator pointing to one past the last element to be
00701        *                erased.
00702        *  @return  An iterator pointing to the element pointed to by @a last
00703        *           prior to erasing (or end()).
00704        *
00705        *  This function will erase the elements in the range [first,last) and
00706        *  shorten the %vector accordingly.
00707        *
00708        *  Note This operation could be expensive and if it is
00709        *  frequently used the user should consider using std::list.
00710        *  The user is also cautioned that this function only erases
00711        *  the elements, and that if the elements themselves are
00712        *  pointers, the pointed-to memory is not touched in any way.
00713        *  Managing the pointer is the user's responsibilty.
00714        */
00715       iterator
00716       erase(iterator __first, iterator __last);
00717 
00718       /**
00719        *  @brief  Swaps data with another %vector.
00720        *  @param  x  A %vector of the same element and allocator types.
00721        *
00722        *  This exchanges the elements between two vectors in constant time.
00723        *  (Three pointers, so it should be quite fast.)
00724        *  Note that the global std::swap() function is specialized such that
00725        *  std::swap(v1,v2) will feed to this function.
00726        */
00727       void
00728       swap(vector& __x)
00729       {
00730     std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
00731     std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
00732     std::swap(this->_M_impl._M_end_of_storage,
00733           __x._M_impl._M_end_of_storage);
00734 
00735     // _GLIBCXX_RESOLVE_LIB_DEFECTS
00736     // 431. Swapping containers with unequal allocators.
00737     std::__alloc_swap<_Tp_alloc_type>::_S_do_it(_M_get_Tp_allocator(),
00738                             __x._M_get_Tp_allocator());
00739       }
00740 
00741       /**
00742        *  Erases all the elements.  Note that this function only erases the
00743        *  elements, and that if the elements themselves are pointers, the
00744        *  pointed-to memory is not touched in any way.  Managing the pointer is
00745        *  the user's responsibilty.
00746        */
00747       void
00748       clear()
00749       { _M_erase_at_end(this->_M_impl._M_start); }
00750 
00751     protected:
00752       /**
00753        *  @if maint
00754        *  Memory expansion handler.  Uses the member allocation function to
00755        *  obtain @a n bytes of memory, and then copies [first,last) into it.
00756        *  @endif
00757        */
00758       template<typename _ForwardIterator>
00759         pointer
00760         _M_allocate_and_copy(size_type __n,
00761                  _ForwardIterator __first, _ForwardIterator __last)
00762         {
00763       pointer __result = this->_M_allocate(__n);
00764       try
00765         {
00766           std::__uninitialized_copy_a(__first, __last, __result,
00767                       _M_get_Tp_allocator());
00768           return __result;
00769         }
00770       catch(...)
00771         {
00772           _M_deallocate(__result, __n);
00773           __throw_exception_again;
00774         }
00775     }
00776 
00777 
00778       // Internal constructor functions follow.
00779 
00780       // Called by the range constructor to implement [23.1.1]/9
00781       template<typename _Integer>
00782         void
00783         _M_initialize_dispatch(_Integer __n, _Integer __value, __true_type)
00784         {
00785       this->_M_impl._M_start = _M_allocate(__n);
00786       this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
00787       std::__uninitialized_fill_n_a(this->_M_impl._M_start, __n, __value,
00788                     _M_get_Tp_allocator());
00789       this->_M_impl._M_finish = this->_M_impl._M_end_of_storage;
00790     }
00791 
00792       // Called by the range constructor to implement [23.1.1]/9
00793       template<typename _InputIterator>
00794         void
00795         _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
00796                    __false_type)
00797         {
00798       typedef typename std::iterator_traits<_InputIterator>::
00799         iterator_category _IterCategory;
00800       _M_range_initialize(__first, __last, _IterCategory());
00801     }
00802 
00803       // Called by the second initialize_dispatch above
00804       template<typename _InputIterator>
00805         void
00806         _M_range_initialize(_InputIterator __first,
00807                 _InputIterator __last, std::input_iterator_tag)
00808         {
00809       for (; __first != __last; ++__first)
00810         push_back(*__first);
00811     }
00812 
00813       // Called by the second initialize_dispatch above
00814       template<typename _ForwardIterator>
00815         void
00816         _M_range_initialize(_ForwardIterator __first,
00817                 _ForwardIterator __last, std::forward_iterator_tag)
00818         {
00819       const size_type __n = std::distance(__first, __last);
00820       this->_M_impl._M_start = this->_M_allocate(__n);
00821       this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
00822       this->_M_impl._M_finish =
00823         std::__uninitialized_copy_a(__first, __last,
00824                     this->_M_impl._M_start,
00825                     _M_get_Tp_allocator());
00826     }
00827 
00828 
00829       // Internal assign functions follow.  The *_aux functions do the actual
00830       // assignment work for the range versions.
00831 
00832       // Called by the range assign to implement [23.1.1]/9
00833       template<typename _Integer>
00834         void
00835         _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
00836         {
00837       _M_fill_assign(static_cast<size_type>(__n),
00838              static_cast<value_type>(__val));
00839     }
00840 
00841       // Called by the range assign to implement [23.1.1]/9
00842       template<typename _InputIterator>
00843         void
00844         _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
00845                __false_type)
00846         {
00847       typedef typename std::iterator_traits<_InputIterator>::
00848         iterator_category _IterCategory;
00849       _M_assign_aux(__first, __last, _IterCategory());
00850     }
00851 
00852       // Called by the second assign_dispatch above
00853       template<typename _InputIterator>
00854         void
00855         _M_assign_aux(_InputIterator __first, _InputIterator __last,
00856               std::input_iterator_tag);
00857 
00858       // Called by the second assign_dispatch above
00859       template<typename _ForwardIterator>
00860         void
00861         _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
00862               std::forward_iterator_tag);
00863 
00864       // Called by assign(n,t), and the range assign when it turns out
00865       // to be the same thing.
00866       void
00867       _M_fill_assign(size_type __n, const value_type& __val);
00868 
00869 
00870       // Internal insert functions follow.
00871 
00872       // Called by the range insert to implement [23.1.1]/9
00873       template<typename _Integer>
00874         void
00875         _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
00876                __true_type)
00877         {
00878       _M_fill_insert(__pos, static_cast<size_type>(__n),
00879              static_cast<value_type>(__val));
00880     }
00881 
00882       // Called by the range insert to implement [23.1.1]/9
00883       template<typename _InputIterator>
00884         void
00885         _M_insert_dispatch(iterator __pos, _InputIterator __first,
00886                _InputIterator __last, __false_type)
00887         {
00888       typedef typename std::iterator_traits<_InputIterator>::
00889         iterator_category _IterCategory;
00890       _M_range_insert(__pos, __first, __last, _IterCategory());
00891     }
00892 
00893       // Called by the second insert_dispatch above
00894       template<typename _InputIterator>
00895         void
00896         _M_range_insert(iterator __pos, _InputIterator __first,
00897             _InputIterator __last, std::input_iterator_tag);
00898 
00899       // Called by the second insert_dispatch above
00900       template<typename _ForwardIterator>
00901         void
00902         _M_range_insert(iterator __pos, _ForwardIterator __first,
00903             _ForwardIterator __last, std::forward_iterator_tag);
00904 
00905       // Called by insert(p,n,x), and the range insert when it turns out to be
00906       // the same thing.
00907       void
00908       _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
00909 
00910       // Called by insert(p,x)
00911       void
00912       _M_insert_aux(iterator __position, const value_type& __x);
00913 
00914       // Internal erase functions follow.
00915 
00916       // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
00917       // _M_assign_aux.
00918       void
00919       _M_erase_at_end(pointer __pos)
00920       {
00921     std::_Destroy(__pos, this->_M_impl._M_finish, _M_get_Tp_allocator());
00922     this->_M_impl._M_finish = __pos;
00923       }
00924     };
00925 
00926 
00927   /**
00928    *  @brief  Vector equality comparison.
00929    *  @param  x  A %vector.
00930    *  @param  y  A %vector of the same type as @a x.
00931    *  @return  True iff the size and elements of the vectors are equal.
00932    *
00933    *  This is an equivalence relation.  It is linear in the size of the
00934    *  vectors.  Vectors are considered equivalent if their sizes are equal,
00935    *  and if corresponding elements compare equal.
00936   */
00937   template<typename _Tp, typename _Alloc>
00938     inline bool
00939     operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
00940     { return (__x.size() == __y.size()
00941           && std::equal(__x.begin(), __x.end(), __y.begin())); }
00942 
00943   /**
00944    *  @brief  Vector ordering relation.
00945    *  @param  x  A %vector.
00946    *  @param  y  A %vector of the same type as @a x.
00947    *  @return  True iff @a x is lexicographically less than @a y.
00948    *
00949    *  This is a total ordering relation.  It is linear in the size of the
00950    *  vectors.  The elements must be comparable with @c <.
00951    *
00952    *  See std::lexicographical_compare() for how the determination is made.
00953   */
00954   template<typename _Tp, typename _Alloc>
00955     inline bool
00956     operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
00957     { return std::lexicographical_compare(__x.begin(), __x.end(),
00958                       __y.begin(), __y.end()); }
00959 
00960   /// Based on operator==
00961   template<typename _Tp, typename _Alloc>
00962     inline bool
00963     operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
00964     { return !(__x == __y); }
00965 
00966   /// Based on operator<
00967   template<typename _Tp, typename _Alloc>
00968     inline bool
00969     operator>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
00970     { return __y < __x; }
00971 
00972   /// Based on operator<
00973   template<typename _Tp, typename _Alloc>
00974     inline bool
00975     operator<=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
00976     { return !(__y < __x); }
00977 
00978   /// Based on operator<
00979   template<typename _Tp, typename _Alloc>
00980     inline bool
00981     operator>=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
00982     { return !(__x < __y); }
00983 
00984   /// See std::vector::swap().
00985   template<typename _Tp, typename _Alloc>
00986     inline void
00987     swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>& __y)
00988     { __x.swap(__y); }
00989 
00990 _GLIBCXX_END_NESTED_NAMESPACE
00991 
00992 #endif /* _VECTOR_H */

Generated on Thu Nov 1 13:12:34 2007 for libstdc++ by  doxygen 1.5.1