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

Generated on Tue Apr 21 13:13:33 2009 for libstdc++ by  doxygen 1.5.8