stl_vector.h

Go to the documentation of this file.
00001 // Vector implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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 _STL_VECTOR_H
00063 #define _STL_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_D)
00070 
00071   /// See bits/stl_deque.h's _Deque_base for an explanation.
00072   template<typename _Tp, typename _Alloc>
00073     struct _Vector_base
00074     {
00075       typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
00076 
00077       struct _Vector_impl 
00078       : public _Tp_alloc_type
00079       {
00080     _Tp*           _M_start;
00081     _Tp*           _M_finish;
00082     _Tp*           _M_end_of_storage;
00083 
00084     _Vector_impl()
00085     : _Tp_alloc_type(), _M_start(0), _M_finish(0), _M_end_of_storage(0)
00086     { }
00087 
00088     _Vector_impl(_Tp_alloc_type const& __a)
00089     : _Tp_alloc_type(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0)
00090     { }
00091       };
00092       
00093     public:
00094       typedef _Alloc allocator_type;
00095 
00096       _Tp_alloc_type&
00097       _M_get_Tp_allocator()
00098       { return *static_cast<_Tp_alloc_type*>(&this->_M_impl); }
00099 
00100       const _Tp_alloc_type&
00101       _M_get_Tp_allocator() const
00102       { return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); }
00103 
00104       allocator_type
00105       get_allocator() const
00106       { return allocator_type(_M_get_Tp_allocator()); }
00107 
00108       _Vector_base()
00109       : _M_impl() { }
00110 
00111       _Vector_base(const allocator_type& __a)
00112       : _M_impl(__a) { }
00113 
00114       _Vector_base(size_t __n, const allocator_type& __a)
00115       : _M_impl(__a)
00116       {
00117     this->_M_impl._M_start = this->_M_allocate(__n);
00118     this->_M_impl._M_finish = this->_M_impl._M_start;
00119     this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
00120       }
00121 
00122 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00123       _Vector_base(_Vector_base&& __x)
00124       : _M_impl(__x._M_get_Tp_allocator())
00125       {
00126     this->_M_impl._M_start = __x._M_impl._M_start;
00127     this->_M_impl._M_finish = __x._M_impl._M_finish;
00128     this->_M_impl._M_end_of_storage = __x._M_impl._M_end_of_storage;
00129     __x._M_impl._M_start = 0;
00130     __x._M_impl._M_finish = 0;
00131     __x._M_impl._M_end_of_storage = 0;
00132       }
00133 #endif
00134 
00135       ~_Vector_base()
00136       { _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage
00137               - this->_M_impl._M_start); }
00138 
00139     public:
00140       _Vector_impl _M_impl;
00141 
00142       _Tp*
00143       _M_allocate(size_t __n)
00144       { return __n != 0 ? _M_impl.allocate(__n) : 0; }
00145 
00146       void
00147       _M_deallocate(_Tp* __p, size_t __n)
00148       {
00149     if (__p)
00150       _M_impl.deallocate(__p, __n);
00151       }
00152     };
00153 
00154 
00155   /**
00156    *  @brief A standard container which offers fixed time access to
00157    *  individual elements in any order.
00158    *
00159    *  @ingroup Containers
00160    *  @ingroup Sequences
00161    *
00162    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
00163    *  <a href="tables.html#66">reversible container</a>, and a
00164    *  <a href="tables.html#67">sequence</a>, including the
00165    *  <a href="tables.html#68">optional sequence requirements</a> with the
00166    *  %exception of @c push_front and @c pop_front.
00167    *
00168    *  In some terminology a %vector can be described as a dynamic
00169    *  C-style array, it offers fast and efficient access to individual
00170    *  elements in any order and saves the user from worrying about
00171    *  memory and size allocation.  Subscripting ( @c [] ) access is
00172    *  also provided as with C-style arrays.
00173   */
00174   template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
00175     class vector : protected _Vector_base<_Tp, _Alloc>
00176     {
00177       // Concept requirements.
00178       typedef typename _Alloc::value_type                _Alloc_value_type;
00179       __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
00180       __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
00181       
00182       typedef _Vector_base<_Tp, _Alloc>          _Base;
00183       typedef vector<_Tp, _Alloc>            vector_type;
00184       typedef typename _Base::_Tp_alloc_type         _Tp_alloc_type;
00185 
00186     public:
00187       typedef _Tp                    value_type;
00188       typedef typename _Tp_alloc_type::pointer           pointer;
00189       typedef typename _Tp_alloc_type::const_pointer     const_pointer;
00190       typedef typename _Tp_alloc_type::reference         reference;
00191       typedef typename _Tp_alloc_type::const_reference   const_reference;
00192       typedef __gnu_cxx::__normal_iterator<pointer, vector_type> iterator;
00193       typedef __gnu_cxx::__normal_iterator<const_pointer, vector_type>
00194       const_iterator;
00195       typedef std::reverse_iterator<const_iterator>  const_reverse_iterator;
00196       typedef std::reverse_iterator<iterator>        reverse_iterator;
00197       typedef size_t                     size_type;
00198       typedef ptrdiff_t                  difference_type;
00199       typedef _Alloc                                 allocator_type;
00200 
00201     protected:
00202       using _Base::_M_allocate;
00203       using _Base::_M_deallocate;
00204       using _Base::_M_impl;
00205       using _Base::_M_get_Tp_allocator;
00206 
00207     public:
00208       // [23.2.4.1] construct/copy/destroy
00209       // (assign() and get_allocator() are also listed in this section)
00210       /**
00211        *  @brief  Default constructor creates no elements.
00212        */
00213       vector()
00214       : _Base() { }
00215 
00216       /**
00217        *  @brief  Creates a %vector with no elements.
00218        *  @param  a  An allocator object.
00219        */
00220       explicit
00221       vector(const allocator_type& __a)
00222       : _Base(__a) { }
00223 
00224       /**
00225        *  @brief  Creates a %vector with copies of an exemplar element.
00226        *  @param  n  The number of elements to initially create.
00227        *  @param  value  An element to copy.
00228        *  @param  a  An allocator.
00229        *
00230        *  This constructor fills the %vector with @a n copies of @a value.
00231        */
00232       explicit
00233       vector(size_type __n, const value_type& __value = value_type(),
00234          const allocator_type& __a = allocator_type())
00235       : _Base(__n, __a)
00236       { _M_fill_initialize(__n, __value); }
00237 
00238       /**
00239        *  @brief  %Vector copy constructor.
00240        *  @param  x  A %vector of identical element and allocator types.
00241        *
00242        *  The newly-created %vector uses a copy of the allocation
00243        *  object used by @a x.  All the elements of @a x are copied,
00244        *  but any extra memory in
00245        *  @a x (for fast expansion) will not be copied.
00246        */
00247       vector(const vector& __x)
00248       : _Base(__x.size(), __x._M_get_Tp_allocator())
00249       { this->_M_impl._M_finish =
00250       std::__uninitialized_copy_a(__x.begin(), __x.end(),
00251                       this->_M_impl._M_start,
00252                       _M_get_Tp_allocator());
00253       }
00254 
00255 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00256       /**
00257        *  @brief  %Vector move constructor.
00258        *  @param  x  A %vector of identical element and allocator types.
00259        *
00260        *  The newly-created %vector contains the exact contents of @a x.
00261        *  The contents of @a x are a valid, but unspecified %vector.
00262        */
00263       vector(vector&& __x)
00264       : _Base(std::forward<_Base>(__x)) { }
00265 #endif
00266 
00267       /**
00268        *  @brief  Builds a %vector from a range.
00269        *  @param  first  An input iterator.
00270        *  @param  last  An input iterator.
00271        *  @param  a  An allocator.
00272        *
00273        *  Create a %vector consisting of copies of the elements from
00274        *  [first,last).
00275        *
00276        *  If the iterators are forward, bidirectional, or
00277        *  random-access, then this will call the elements' copy
00278        *  constructor N times (where N is distance(first,last)) and do
00279        *  no memory reallocation.  But if only input iterators are
00280        *  used, then this will do at most 2N calls to the copy
00281        *  constructor, and logN memory reallocations.
00282        */
00283       template<typename _InputIterator>
00284         vector(_InputIterator __first, _InputIterator __last,
00285            const allocator_type& __a = allocator_type())
00286     : _Base(__a)
00287         {
00288       // Check whether it's an integral type.  If so, it's not an iterator.
00289       typedef typename std::__is_integer<_InputIterator>::__type _Integral;
00290       _M_initialize_dispatch(__first, __last, _Integral());
00291     }
00292 
00293       /**
00294        *  The dtor only erases the elements, and note that if the
00295        *  elements themselves are pointers, the pointed-to memory is
00296        *  not touched in any way.  Managing the pointer is the user's
00297        *  responsibility.
00298        */
00299       ~vector()
00300       { std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
00301               _M_get_Tp_allocator()); }
00302 
00303       /**
00304        *  @brief  %Vector assignment operator.
00305        *  @param  x  A %vector of identical element and allocator types.
00306        *
00307        *  All the elements of @a x are copied, but any extra memory in
00308        *  @a x (for fast expansion) will not be copied.  Unlike the
00309        *  copy constructor, the allocator object is not copied.
00310        */
00311       vector&
00312       operator=(const vector& __x);
00313 
00314 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00315       /**
00316        *  @brief  %Vector move assignment operator.
00317        *  @param  x  A %vector of identical element and allocator types.
00318        *
00319        *  The contents of @a x are moved into this %vector (without copying).
00320        *  @a x is a valid, but unspecified %vector.
00321        */
00322       vector&
00323       operator=(vector&& __x)
00324       {
00325     // NB: DR 675.
00326     this->clear();
00327     this->swap(__x); 
00328     return *this;
00329       }
00330 #endif
00331 
00332       /**
00333        *  @brief  Assigns a given value to a %vector.
00334        *  @param  n  Number of elements to be assigned.
00335        *  @param  val  Value to be assigned.
00336        *
00337        *  This function fills a %vector with @a n copies of the given
00338        *  value.  Note that the assignment completely changes the
00339        *  %vector and that the resulting %vector's size is the same as
00340        *  the number of elements assigned.  Old data may be lost.
00341        */
00342       void
00343       assign(size_type __n, const value_type& __val)
00344       { _M_fill_assign(__n, __val); }
00345 
00346       /**
00347        *  @brief  Assigns a range to a %vector.
00348        *  @param  first  An input iterator.
00349        *  @param  last   An input iterator.
00350        *
00351        *  This function fills a %vector with copies of the elements in the
00352        *  range [first,last).
00353        *
00354        *  Note that the assignment completely changes the %vector and
00355        *  that the resulting %vector's size is the same as the number
00356        *  of elements assigned.  Old data may be lost.
00357        */
00358       template<typename _InputIterator>
00359         void
00360         assign(_InputIterator __first, _InputIterator __last)
00361         {
00362       // Check whether it's an integral type.  If so, it's not an iterator.
00363       typedef typename std::__is_integer<_InputIterator>::__type _Integral;
00364       _M_assign_dispatch(__first, __last, _Integral());
00365     }
00366 
00367       /// Get a copy of the memory allocation object.
00368       using _Base::get_allocator;
00369 
00370       // iterators
00371       /**
00372        *  Returns a read/write iterator that points to the first
00373        *  element in the %vector.  Iteration is done in ordinary
00374        *  element order.
00375        */
00376       iterator
00377       begin()
00378       { return iterator(this->_M_impl._M_start); }
00379 
00380       /**
00381        *  Returns a read-only (constant) iterator that points to the
00382        *  first element in the %vector.  Iteration is done in ordinary
00383        *  element order.
00384        */
00385       const_iterator
00386       begin() const
00387       { return const_iterator(this->_M_impl._M_start); }
00388 
00389       /**
00390        *  Returns a read/write iterator that points one past the last
00391        *  element in the %vector.  Iteration is done in ordinary
00392        *  element order.
00393        */
00394       iterator
00395       end()
00396       { return iterator(this->_M_impl._M_finish); }
00397 
00398       /**
00399        *  Returns a read-only (constant) iterator that points one past
00400        *  the last element in the %vector.  Iteration is done in
00401        *  ordinary element order.
00402        */
00403       const_iterator
00404       end() const
00405       { return const_iterator(this->_M_impl._M_finish); }
00406 
00407       /**
00408        *  Returns a read/write reverse iterator that points to the
00409        *  last element in the %vector.  Iteration is done in reverse
00410        *  element order.
00411        */
00412       reverse_iterator
00413       rbegin()
00414       { return reverse_iterator(end()); }
00415 
00416       /**
00417        *  Returns a read-only (constant) reverse iterator that points
00418        *  to the last element in the %vector.  Iteration is done in
00419        *  reverse element order.
00420        */
00421       const_reverse_iterator
00422       rbegin() const
00423       { return const_reverse_iterator(end()); }
00424 
00425       /**
00426        *  Returns a read/write reverse iterator that points to one
00427        *  before the first element in the %vector.  Iteration is done
00428        *  in reverse element order.
00429        */
00430       reverse_iterator
00431       rend()
00432       { return reverse_iterator(begin()); }
00433 
00434       /**
00435        *  Returns a read-only (constant) reverse iterator that points
00436        *  to one before the first element in the %vector.  Iteration
00437        *  is done in reverse element order.
00438        */
00439       const_reverse_iterator
00440       rend() const
00441       { return const_reverse_iterator(begin()); }
00442 
00443 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00444       /**
00445        *  Returns a read-only (constant) iterator that points to the
00446        *  first element in the %vector.  Iteration is done in ordinary
00447        *  element order.
00448        */
00449       const_iterator
00450       cbegin() const
00451       { return const_iterator(this->_M_impl._M_start); }
00452 
00453       /**
00454        *  Returns a read-only (constant) iterator that points one past
00455        *  the last element in the %vector.  Iteration is done in
00456        *  ordinary element order.
00457        */
00458       const_iterator
00459       cend() const
00460       { return const_iterator(this->_M_impl._M_finish); }
00461 
00462       /**
00463        *  Returns a read-only (constant) reverse iterator that points
00464        *  to the last element in the %vector.  Iteration is done in
00465        *  reverse element order.
00466        */
00467       const_reverse_iterator
00468       crbegin() const
00469       { return const_reverse_iterator(end()); }
00470 
00471       /**
00472        *  Returns a read-only (constant) reverse iterator that points
00473        *  to one before the first element in the %vector.  Iteration
00474        *  is done in reverse element order.
00475        */
00476       const_reverse_iterator
00477       crend() const
00478       { return const_reverse_iterator(begin()); }
00479 #endif
00480 
00481       // [23.2.4.2] capacity
00482       /**  Returns the number of elements in the %vector.  */
00483       size_type
00484       size() const
00485       { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
00486 
00487       /**  Returns the size() of the largest possible %vector.  */
00488       size_type
00489       max_size() const
00490       { return _M_get_Tp_allocator().max_size(); }
00491 
00492       /**
00493        *  @brief  Resizes the %vector to the specified number of elements.
00494        *  @param  new_size  Number of elements the %vector should contain.
00495        *  @param  x  Data with which new elements should be populated.
00496        *
00497        *  This function will %resize the %vector to the specified
00498        *  number of elements.  If the number is smaller than the
00499        *  %vector's current size the %vector is truncated, otherwise
00500        *  the %vector is extended and new elements are populated with
00501        *  given data.
00502        */
00503       void
00504       resize(size_type __new_size, value_type __x = value_type())
00505       {
00506     if (__new_size < size())
00507       _M_erase_at_end(this->_M_impl._M_start + __new_size);
00508     else
00509       insert(end(), __new_size - size(), __x);
00510       }
00511 
00512       /**
00513        *  Returns the total number of elements that the %vector can
00514        *  hold before needing to allocate more memory.
00515        */
00516       size_type
00517       capacity() const
00518       { return size_type(this->_M_impl._M_end_of_storage
00519              - this->_M_impl._M_start); }
00520 
00521       /**
00522        *  Returns true if the %vector is empty.  (Thus begin() would
00523        *  equal end().)
00524        */
00525       bool
00526       empty() const
00527       { return begin() == end(); }
00528 
00529       /**
00530        *  @brief  Attempt to preallocate enough memory for specified number of
00531        *          elements.
00532        *  @param  n  Number of elements required.
00533        *  @throw  std::length_error  If @a n exceeds @c max_size().
00534        *
00535        *  This function attempts to reserve enough memory for the
00536        *  %vector to hold the specified number of elements.  If the
00537        *  number requested is more than max_size(), length_error is
00538        *  thrown.
00539        *
00540        *  The advantage of this function is that if optimal code is a
00541        *  necessity and the user can determine the number of elements
00542        *  that will be required, the user can reserve the memory in
00543        *  %advance, and thus prevent a possible reallocation of memory
00544        *  and copying of %vector data.
00545        */
00546       void
00547       reserve(size_type __n);
00548 
00549       // element access
00550       /**
00551        *  @brief  Subscript access to the data contained in the %vector.
00552        *  @param n The index of the element for which data should be
00553        *  accessed.
00554        *  @return  Read/write reference to data.
00555        *
00556        *  This operator allows for easy, array-style, data access.
00557        *  Note that data access with this operator is unchecked and
00558        *  out_of_range lookups are not defined. (For checked lookups
00559        *  see at().)
00560        */
00561       reference
00562       operator[](size_type __n)
00563       { return *(this->_M_impl._M_start + __n); }
00564 
00565       /**
00566        *  @brief  Subscript access to the data contained in the %vector.
00567        *  @param n The index of the element for which data should be
00568        *  accessed.
00569        *  @return  Read-only (constant) reference to data.
00570        *
00571        *  This operator allows for easy, array-style, data access.
00572        *  Note that data access with this operator is unchecked and
00573        *  out_of_range lookups are not defined. (For checked lookups
00574        *  see at().)
00575        */
00576       const_reference
00577       operator[](size_type __n) const
00578       { return *(this->_M_impl._M_start + __n); }
00579 
00580     protected:
00581       /// Safety check used only from at().
00582       void
00583       _M_range_check(size_type __n) const
00584       {
00585     if (__n >= this->size())
00586       __throw_out_of_range(__N("vector::_M_range_check"));
00587       }
00588 
00589     public:
00590       /**
00591        *  @brief  Provides access to the data contained in the %vector.
00592        *  @param n The index of the element for which data should be
00593        *  accessed.
00594        *  @return  Read/write reference to data.
00595        *  @throw  std::out_of_range  If @a n is an invalid index.
00596        *
00597        *  This function provides for safer data access.  The parameter
00598        *  is first checked that it is in the range of the vector.  The
00599        *  function throws out_of_range if the check fails.
00600        */
00601       reference
00602       at(size_type __n)
00603       {
00604     _M_range_check(__n);
00605     return (*this)[__n]; 
00606       }
00607 
00608       /**
00609        *  @brief  Provides access to the data contained in the %vector.
00610        *  @param n The index of the element for which data should be
00611        *  accessed.
00612        *  @return  Read-only (constant) reference to data.
00613        *  @throw  std::out_of_range  If @a n is an invalid index.
00614        *
00615        *  This function provides for safer data access.  The parameter
00616        *  is first checked that it is in the range of the vector.  The
00617        *  function throws out_of_range if the check fails.
00618        */
00619       const_reference
00620       at(size_type __n) const
00621       {
00622     _M_range_check(__n);
00623     return (*this)[__n];
00624       }
00625 
00626       /**
00627        *  Returns a read/write reference to the data at the first
00628        *  element of the %vector.
00629        */
00630       reference
00631       front()
00632       { return *begin(); }
00633 
00634       /**
00635        *  Returns a read-only (constant) reference to the data at the first
00636        *  element of the %vector.
00637        */
00638       const_reference
00639       front() const
00640       { return *begin(); }
00641 
00642       /**
00643        *  Returns a read/write reference to the data at the last
00644        *  element of the %vector.
00645        */
00646       reference
00647       back()
00648       { return *(end() - 1); }
00649       
00650       /**
00651        *  Returns a read-only (constant) reference to the data at the
00652        *  last element of the %vector.
00653        */
00654       const_reference
00655       back() const
00656       { return *(end() - 1); }
00657 
00658       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00659       // DR 464. Suggestion for new member functions in standard containers.
00660       // data access
00661       /**
00662        *   Returns a pointer such that [data(), data() + size()) is a valid
00663        *   range.  For a non-empty %vector, data() == &front().
00664        */
00665       pointer
00666       data()
00667       { return pointer(this->_M_impl._M_start); }
00668 
00669       const_pointer
00670       data() const
00671       { return const_pointer(this->_M_impl._M_start); }
00672 
00673       // [23.2.4.3] modifiers
00674       /**
00675        *  @brief  Add data to the end of the %vector.
00676        *  @param  x  Data to be added.
00677        *
00678        *  This is a typical stack operation.  The function creates an
00679        *  element at the end of the %vector and assigns the given data
00680        *  to it.  Due to the nature of a %vector this operation can be
00681        *  done in constant time if the %vector has preallocated space
00682        *  available.
00683        */
00684 #ifndef __GXX_EXPERIMENTAL_CXX0X__
00685       void
00686       push_back(const value_type& __x)
00687       {
00688     if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
00689       {
00690         this->_M_impl.construct(this->_M_impl._M_finish, __x);
00691         ++this->_M_impl._M_finish;
00692       }
00693     else
00694       _M_insert_aux(end(), __x);
00695       }
00696 #else
00697       template<typename... _Args>
00698         void
00699         push_back(_Args&&... __args)
00700     {
00701       if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
00702         {
00703           this->_M_impl.construct(this->_M_impl._M_finish,
00704                       std::forward<_Args>(__args)...);
00705           ++this->_M_impl._M_finish;
00706         }
00707       else
00708         _M_insert_aux(end(), std::forward<_Args>(__args)...);
00709     }
00710 #endif
00711 
00712       /**
00713        *  @brief  Removes last element.
00714        *
00715        *  This is a typical stack operation. It shrinks the %vector by one.
00716        *
00717        *  Note that no data is returned, and if the last element's
00718        *  data is needed, it should be retrieved before pop_back() is
00719        *  called.
00720        */
00721       void
00722       pop_back()
00723       {
00724     --this->_M_impl._M_finish;
00725     this->_M_impl.destroy(this->_M_impl._M_finish);
00726       }
00727 
00728 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00729       /**
00730        *  @brief  Inserts an object in %vector before specified iterator.
00731        *  @param  position  An iterator into the %vector.
00732        *  @param  args  Arguments.
00733        *  @return  An iterator that points to the inserted data.
00734        *
00735        *  This function will insert an object of type T constructed
00736        *  with T(std::forward<Args>(args)...) before the specified location.
00737        *  Note that this kind of operation could be expensive for a %vector
00738        *  and if it is frequently used the user should consider using
00739        *  std::list.
00740        */
00741       template<typename... _Args>
00742         iterator
00743         emplace(iterator __position, _Args&&... __args);
00744 #endif
00745 
00746       /**
00747        *  @brief  Inserts given value into %vector before specified iterator.
00748        *  @param  position  An iterator into the %vector.
00749        *  @param  x  Data to be inserted.
00750        *  @return  An iterator that points to the inserted data.
00751        *
00752        *  This function will insert a copy of the given value before
00753        *  the specified location.  Note that this kind of operation
00754        *  could be expensive for a %vector and if it is frequently
00755        *  used the user should consider using std::list.
00756        */
00757       iterator
00758       insert(iterator __position, const value_type& __x);
00759 
00760 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00761       /**
00762        *  @brief  Inserts given rvalue into %vector before specified iterator.
00763        *  @param  position  An iterator into the %vector.
00764        *  @param  x  Data to be inserted.
00765        *  @return  An iterator that points to the inserted data.
00766        *
00767        *  This function will insert a copy of the given rvalue before
00768        *  the specified location.  Note that this kind of operation
00769        *  could be expensive for a %vector and if it is frequently
00770        *  used the user should consider using std::list.
00771        */
00772       iterator
00773       insert(iterator __position, value_type&& __x)
00774       { return emplace(__position, std::move(__x)); }
00775 #endif
00776 
00777       /**
00778        *  @brief  Inserts a number of copies of given data into the %vector.
00779        *  @param  position  An iterator into the %vector.
00780        *  @param  n  Number of elements to be inserted.
00781        *  @param  x  Data to be inserted.
00782        *
00783        *  This function will insert a specified number of copies of
00784        *  the given data before the location specified by @a position.
00785        *
00786        *  Note that this kind of operation could be expensive for a
00787        *  %vector and if it is frequently used the user should
00788        *  consider using std::list.
00789        */
00790       void
00791       insert(iterator __position, size_type __n, const value_type& __x)
00792       { _M_fill_insert(__position, __n, __x); }
00793 
00794       /**
00795        *  @brief  Inserts a range into the %vector.
00796        *  @param  position  An iterator into the %vector.
00797        *  @param  first  An input iterator.
00798        *  @param  last   An input iterator.
00799        *
00800        *  This function will insert copies of the data in the range
00801        *  [first,last) into the %vector before the location specified
00802        *  by @a pos.
00803        *
00804        *  Note that this kind of operation could be expensive for a
00805        *  %vector and if it is frequently used the user should
00806        *  consider using std::list.
00807        */
00808       template<typename _InputIterator>
00809         void
00810         insert(iterator __position, _InputIterator __first,
00811            _InputIterator __last)
00812         {
00813       // Check whether it's an integral type.  If so, it's not an iterator.
00814       typedef typename std::__is_integer<_InputIterator>::__type _Integral;
00815       _M_insert_dispatch(__position, __first, __last, _Integral());
00816     }
00817 
00818       /**
00819        *  @brief  Remove element at given position.
00820        *  @param  position  Iterator pointing to element to be erased.
00821        *  @return  An iterator pointing to the next element (or end()).
00822        *
00823        *  This function will erase the element at the given position and thus
00824        *  shorten the %vector by one.
00825        *
00826        *  Note This operation could be expensive and if it is
00827        *  frequently used the user should consider using std::list.
00828        *  The user is also cautioned that this function only erases
00829        *  the element, and that if the element is itself a pointer,
00830        *  the pointed-to memory is not touched in any way.  Managing
00831        *  the pointer is the user's responsibility.
00832        */
00833       iterator
00834       erase(iterator __position);
00835 
00836       /**
00837        *  @brief  Remove a range of elements.
00838        *  @param  first  Iterator pointing to the first element to be erased.
00839        *  @param  last  Iterator pointing to one past the last element to be
00840        *                erased.
00841        *  @return  An iterator pointing to the element pointed to by @a last
00842        *           prior to erasing (or end()).
00843        *
00844        *  This function will erase the elements in the range [first,last) and
00845        *  shorten the %vector accordingly.
00846        *
00847        *  Note This operation could be expensive and if it is
00848        *  frequently used the user should consider using std::list.
00849        *  The user is also cautioned that this function only erases
00850        *  the elements, and that if the elements themselves are
00851        *  pointers, the pointed-to memory is not touched in any way.
00852        *  Managing the pointer is the user's responsibility.
00853        */
00854       iterator
00855       erase(iterator __first, iterator __last);
00856 
00857       /**
00858        *  @brief  Swaps data with another %vector.
00859        *  @param  x  A %vector of the same element and allocator types.
00860        *
00861        *  This exchanges the elements between two vectors in constant time.
00862        *  (Three pointers, so it should be quite fast.)
00863        *  Note that the global std::swap() function is specialized such that
00864        *  std::swap(v1,v2) will feed to this function.
00865        */
00866       void
00867 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00868       swap(vector&& __x)
00869 #else
00870       swap(vector& __x)
00871 #endif
00872       {
00873     std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
00874     std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
00875     std::swap(this->_M_impl._M_end_of_storage,
00876           __x._M_impl._M_end_of_storage);
00877 
00878     // _GLIBCXX_RESOLVE_LIB_DEFECTS
00879     // 431. Swapping containers with unequal allocators.
00880     std::__alloc_swap<_Tp_alloc_type>::_S_do_it(_M_get_Tp_allocator(),
00881                             __x._M_get_Tp_allocator());
00882       }
00883 
00884       /**
00885        *  Erases all the elements.  Note that this function only erases the
00886        *  elements, and that if the elements themselves are pointers, the
00887        *  pointed-to memory is not touched in any way.  Managing the pointer is
00888        *  the user's responsibility.
00889        */
00890       void
00891       clear()
00892       { _M_erase_at_end(this->_M_impl._M_start); }
00893 
00894     protected:
00895       /**
00896        *  Memory expansion handler.  Uses the member allocation function to
00897        *  obtain @a n bytes of memory, and then copies [first,last) into it.
00898        */
00899       template<typename _ForwardIterator>
00900         pointer
00901         _M_allocate_and_copy(size_type __n,
00902                  _ForwardIterator __first, _ForwardIterator __last)
00903         {
00904       pointer __result = this->_M_allocate(__n);
00905       try
00906         {
00907           std::__uninitialized_copy_a(__first, __last, __result,
00908                       _M_get_Tp_allocator());
00909           return __result;
00910         }
00911       catch(...)
00912         {
00913           _M_deallocate(__result, __n);
00914           __throw_exception_again;
00915         }
00916     }
00917 
00918 
00919       // Internal constructor functions follow.
00920 
00921       // Called by the range constructor to implement [23.1.1]/9
00922 
00923       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00924       // 438. Ambiguity in the "do the right thing" clause
00925       template<typename _Integer>
00926         void
00927         _M_initialize_dispatch(_Integer __n, _Integer __value, __true_type)
00928         {
00929       this->_M_impl._M_start = _M_allocate(static_cast<size_type>(__n));
00930       this->_M_impl._M_end_of_storage =
00931         this->_M_impl._M_start + static_cast<size_type>(__n);
00932       _M_fill_initialize(static_cast<size_type>(__n), __value);
00933     }
00934 
00935       // Called by the range constructor to implement [23.1.1]/9
00936       template<typename _InputIterator>
00937         void
00938         _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
00939                    __false_type)
00940         {
00941       typedef typename std::iterator_traits<_InputIterator>::
00942         iterator_category _IterCategory;
00943       _M_range_initialize(__first, __last, _IterCategory());
00944     }
00945 
00946       // Called by the second initialize_dispatch above
00947       template<typename _InputIterator>
00948         void
00949         _M_range_initialize(_InputIterator __first,
00950                 _InputIterator __last, std::input_iterator_tag)
00951         {
00952       for (; __first != __last; ++__first)
00953         push_back(*__first);
00954     }
00955 
00956       // Called by the second initialize_dispatch above
00957       template<typename _ForwardIterator>
00958         void
00959         _M_range_initialize(_ForwardIterator __first,
00960                 _ForwardIterator __last, std::forward_iterator_tag)
00961         {
00962       const size_type __n = std::distance(__first, __last);
00963       this->_M_impl._M_start = this->_M_allocate(__n);
00964       this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
00965       this->_M_impl._M_finish =
00966         std::__uninitialized_copy_a(__first, __last,
00967                     this->_M_impl._M_start,
00968                     _M_get_Tp_allocator());
00969     }
00970 
00971       // Called by the first initialize_dispatch above and by the
00972       // vector(n,value,a) constructor.
00973       void
00974       _M_fill_initialize(size_type __n, const value_type& __value)
00975       {
00976     std::__uninitialized_fill_n_a(this->_M_impl._M_start, __n, __value, 
00977                       _M_get_Tp_allocator());
00978     this->_M_impl._M_finish = this->_M_impl._M_end_of_storage;
00979       }
00980 
00981 
00982       // Internal assign functions follow.  The *_aux functions do the actual
00983       // assignment work for the range versions.
00984 
00985       // Called by the range assign to implement [23.1.1]/9
00986 
00987       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00988       // 438. Ambiguity in the "do the right thing" clause
00989       template<typename _Integer>
00990         void
00991         _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
00992         { _M_fill_assign(__n, __val); }
00993 
00994       // Called by the range assign to implement [23.1.1]/9
00995       template<typename _InputIterator>
00996         void
00997         _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
00998                __false_type)
00999         {
01000       typedef typename std::iterator_traits<_InputIterator>::
01001         iterator_category _IterCategory;
01002       _M_assign_aux(__first, __last, _IterCategory());
01003     }
01004 
01005       // Called by the second assign_dispatch above
01006       template<typename _InputIterator>
01007         void
01008         _M_assign_aux(_InputIterator __first, _InputIterator __last,
01009               std::input_iterator_tag);
01010 
01011       // Called by the second assign_dispatch above
01012       template<typename _ForwardIterator>
01013         void
01014         _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
01015               std::forward_iterator_tag);
01016 
01017       // Called by assign(n,t), and the range assign when it turns out
01018       // to be the same thing.
01019       void
01020       _M_fill_assign(size_type __n, const value_type& __val);
01021 
01022 
01023       // Internal insert functions follow.
01024 
01025       // Called by the range insert to implement [23.1.1]/9
01026 
01027       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01028       // 438. Ambiguity in the "do the right thing" clause
01029       template<typename _Integer>
01030         void
01031         _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
01032                __true_type)
01033         { _M_fill_insert(__pos, __n, __val); }
01034 
01035       // Called by the range insert to implement [23.1.1]/9
01036       template<typename _InputIterator>
01037         void
01038         _M_insert_dispatch(iterator __pos, _InputIterator __first,
01039                _InputIterator __last, __false_type)
01040         {
01041       typedef typename std::iterator_traits<_InputIterator>::
01042         iterator_category _IterCategory;
01043       _M_range_insert(__pos, __first, __last, _IterCategory());
01044     }
01045 
01046       // Called by the second insert_dispatch above
01047       template<typename _InputIterator>
01048         void
01049         _M_range_insert(iterator __pos, _InputIterator __first,
01050             _InputIterator __last, std::input_iterator_tag);
01051 
01052       // Called by the second insert_dispatch above
01053       template<typename _ForwardIterator>
01054         void
01055         _M_range_insert(iterator __pos, _ForwardIterator __first,
01056             _ForwardIterator __last, std::forward_iterator_tag);
01057 
01058       // Called by insert(p,n,x), and the range insert when it turns out to be
01059       // the same thing.
01060       void
01061       _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
01062 
01063       // Called by insert(p,x)
01064 #ifndef __GXX_EXPERIMENTAL_CXX0X__
01065       void
01066       _M_insert_aux(iterator __position, const value_type& __x);
01067 #else
01068       template<typename... _Args>
01069         void
01070         _M_insert_aux(iterator __position, _Args&&... __args);
01071 #endif
01072 
01073       // Called by the latter.
01074       size_type
01075       _M_check_len(size_type __n, const char* __s) const
01076       {
01077     if (max_size() - size() < __n)
01078       __throw_length_error(__N(__s));
01079 
01080     const size_type __len = size() + std::max(size(), __n);
01081     return (__len < size() || __len > max_size()) ? max_size() : __len;
01082       }
01083 
01084       // Internal erase functions follow.
01085 
01086       // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
01087       // _M_assign_aux.
01088       void
01089       _M_erase_at_end(pointer __pos)
01090       {
01091     std::_Destroy(__pos, this->_M_impl._M_finish, _M_get_Tp_allocator());
01092     this->_M_impl._M_finish = __pos;
01093       }
01094     };
01095 
01096 
01097   /**
01098    *  @brief  Vector equality comparison.
01099    *  @param  x  A %vector.
01100    *  @param  y  A %vector of the same type as @a x.
01101    *  @return  True iff the size and elements of the vectors are equal.
01102    *
01103    *  This is an equivalence relation.  It is linear in the size of the
01104    *  vectors.  Vectors are considered equivalent if their sizes are equal,
01105    *  and if corresponding elements compare equal.
01106   */
01107   template<typename _Tp, typename _Alloc>
01108     inline bool
01109     operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
01110     { return (__x.size() == __y.size()
01111           && std::equal(__x.begin(), __x.end(), __y.begin())); }
01112 
01113   /**
01114    *  @brief  Vector ordering relation.
01115    *  @param  x  A %vector.
01116    *  @param  y  A %vector of the same type as @a x.
01117    *  @return  True iff @a x is lexicographically less than @a y.
01118    *
01119    *  This is a total ordering relation.  It is linear in the size of the
01120    *  vectors.  The elements must be comparable with @c <.
01121    *
01122    *  See std::lexicographical_compare() for how the determination is made.
01123   */
01124   template<typename _Tp, typename _Alloc>
01125     inline bool
01126     operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
01127     { return std::lexicographical_compare(__x.begin(), __x.end(),
01128                       __y.begin(), __y.end()); }
01129 
01130   /// Based on operator==
01131   template<typename _Tp, typename _Alloc>
01132     inline bool
01133     operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
01134     { return !(__x == __y); }
01135 
01136   /// Based on operator<
01137   template<typename _Tp, typename _Alloc>
01138     inline bool
01139     operator>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
01140     { return __y < __x; }
01141 
01142   /// Based on operator<
01143   template<typename _Tp, typename _Alloc>
01144     inline bool
01145     operator<=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
01146     { return !(__y < __x); }
01147 
01148   /// Based on operator<
01149   template<typename _Tp, typename _Alloc>
01150     inline bool
01151     operator>=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
01152     { return !(__x < __y); }
01153 
01154   /// See std::vector::swap().
01155   template<typename _Tp, typename _Alloc>
01156     inline void
01157     swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>& __y)
01158     { __x.swap(__y); }
01159 
01160 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01161   template<typename _Tp, typename _Alloc>
01162     inline void
01163     swap(vector<_Tp, _Alloc>&& __x, vector<_Tp, _Alloc>& __y)
01164     { __x.swap(__y); }
01165 
01166   template<typename _Tp, typename _Alloc>
01167     inline void
01168     swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>&& __y)
01169     { __x.swap(__y); }
01170 #endif
01171 
01172 _GLIBCXX_END_NESTED_NAMESPACE
01173 
01174 #endif /* _STL_VECTOR_H */

Generated on Fri Jan 23 20:12:23 2009 for libstdc++ by  doxygen 1.5.6