1*e4b17023SJohn Marino // Vector implementation -*- C++ -*- 2*e4b17023SJohn Marino 3*e4b17023SJohn Marino // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 4*e4b17023SJohn Marino // 2011 Free Software Foundation, Inc. 5*e4b17023SJohn Marino // 6*e4b17023SJohn Marino // This file is part of the GNU ISO C++ Library. This library is free 7*e4b17023SJohn Marino // software; you can redistribute it and/or modify it under the 8*e4b17023SJohn Marino // terms of the GNU General Public License as published by the 9*e4b17023SJohn Marino // Free Software Foundation; either version 3, or (at your option) 10*e4b17023SJohn Marino // any later version. 11*e4b17023SJohn Marino 12*e4b17023SJohn Marino // This library is distributed in the hope that it will be useful, 13*e4b17023SJohn Marino // but WITHOUT ANY WARRANTY; without even the implied warranty of 14*e4b17023SJohn Marino // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*e4b17023SJohn Marino // GNU General Public License for more details. 16*e4b17023SJohn Marino 17*e4b17023SJohn Marino // Under Section 7 of GPL version 3, you are granted additional 18*e4b17023SJohn Marino // permissions described in the GCC Runtime Library Exception, version 19*e4b17023SJohn Marino // 3.1, as published by the Free Software Foundation. 20*e4b17023SJohn Marino 21*e4b17023SJohn Marino // You should have received a copy of the GNU General Public License and 22*e4b17023SJohn Marino // a copy of the GCC Runtime Library Exception along with this program; 23*e4b17023SJohn Marino // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 24*e4b17023SJohn Marino // <http://www.gnu.org/licenses/>. 25*e4b17023SJohn Marino 26*e4b17023SJohn Marino /* 27*e4b17023SJohn Marino * 28*e4b17023SJohn Marino * Copyright (c) 1994 29*e4b17023SJohn Marino * Hewlett-Packard Company 30*e4b17023SJohn Marino * 31*e4b17023SJohn Marino * Permission to use, copy, modify, distribute and sell this software 32*e4b17023SJohn Marino * and its documentation for any purpose is hereby granted without fee, 33*e4b17023SJohn Marino * provided that the above copyright notice appear in all copies and 34*e4b17023SJohn Marino * that both that copyright notice and this permission notice appear 35*e4b17023SJohn Marino * in supporting documentation. Hewlett-Packard Company makes no 36*e4b17023SJohn Marino * representations about the suitability of this software for any 37*e4b17023SJohn Marino * purpose. It is provided "as is" without express or implied warranty. 38*e4b17023SJohn Marino * 39*e4b17023SJohn Marino * 40*e4b17023SJohn Marino * Copyright (c) 1996 41*e4b17023SJohn Marino * Silicon Graphics Computer Systems, Inc. 42*e4b17023SJohn Marino * 43*e4b17023SJohn Marino * Permission to use, copy, modify, distribute and sell this software 44*e4b17023SJohn Marino * and its documentation for any purpose is hereby granted without fee, 45*e4b17023SJohn Marino * provided that the above copyright notice appear in all copies and 46*e4b17023SJohn Marino * that both that copyright notice and this permission notice appear 47*e4b17023SJohn Marino * in supporting documentation. Silicon Graphics makes no 48*e4b17023SJohn Marino * representations about the suitability of this software for any 49*e4b17023SJohn Marino * purpose. It is provided "as is" without express or implied warranty. 50*e4b17023SJohn Marino */ 51*e4b17023SJohn Marino 52*e4b17023SJohn Marino /** @file bits/stl_vector.h 53*e4b17023SJohn Marino * This is an internal header file, included by other library headers. 54*e4b17023SJohn Marino * Do not attempt to use it directly. @headername{vector} 55*e4b17023SJohn Marino */ 56*e4b17023SJohn Marino 57*e4b17023SJohn Marino #ifndef _STL_VECTOR_H 58*e4b17023SJohn Marino #define _STL_VECTOR_H 1 59*e4b17023SJohn Marino 60*e4b17023SJohn Marino #include <bits/stl_iterator_base_funcs.h> 61*e4b17023SJohn Marino #include <bits/functexcept.h> 62*e4b17023SJohn Marino #include <bits/concept_check.h> 63*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__ 64*e4b17023SJohn Marino #include <initializer_list> 65*e4b17023SJohn Marino #endif 66*e4b17023SJohn Marino 67*e4b17023SJohn Marino namespace std _GLIBCXX_VISIBILITY(default) 68*e4b17023SJohn Marino { 69*e4b17023SJohn Marino _GLIBCXX_BEGIN_NAMESPACE_CONTAINER 70*e4b17023SJohn Marino 71*e4b17023SJohn Marino /// See bits/stl_deque.h's _Deque_base for an explanation. 72*e4b17023SJohn Marino template<typename _Tp, typename _Alloc> 73*e4b17023SJohn Marino struct _Vector_base 74*e4b17023SJohn Marino { 75*e4b17023SJohn Marino typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template 76*e4b17023SJohn Marino rebind<_Tp>::other _Tp_alloc_type; 77*e4b17023SJohn Marino typedef typename __gnu_cxx::__alloc_traits<_Tp_alloc_type>::pointer 78*e4b17023SJohn Marino pointer; 79*e4b17023SJohn Marino 80*e4b17023SJohn Marino struct _Vector_impl 81*e4b17023SJohn Marino : public _Tp_alloc_type 82*e4b17023SJohn Marino { 83*e4b17023SJohn Marino pointer _M_start; 84*e4b17023SJohn Marino pointer _M_finish; 85*e4b17023SJohn Marino pointer _M_end_of_storage; 86*e4b17023SJohn Marino 87*e4b17023SJohn Marino _Vector_impl() 88*e4b17023SJohn Marino : _Tp_alloc_type(), _M_start(0), _M_finish(0), _M_end_of_storage(0) 89*e4b17023SJohn Marino { } 90*e4b17023SJohn Marino 91*e4b17023SJohn Marino _Vector_impl(_Tp_alloc_type const& __a) 92*e4b17023SJohn Marino : _Tp_alloc_type(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0) 93*e4b17023SJohn Marino { } 94*e4b17023SJohn Marino 95*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__ 96*e4b17023SJohn Marino _Vector_impl(_Tp_alloc_type&& __a) 97*e4b17023SJohn Marino : _Tp_alloc_type(std::move(__a)), 98*e4b17023SJohn Marino _M_start(0), _M_finish(0), _M_end_of_storage(0) 99*e4b17023SJohn Marino { } 100*e4b17023SJohn Marino #endif 101*e4b17023SJohn Marino 102*e4b17023SJohn Marino void _M_swap_data(_Vector_impl& __x) 103*e4b17023SJohn Marino { 104*e4b17023SJohn Marino std::swap(_M_start, __x._M_start); 105*e4b17023SJohn Marino std::swap(_M_finish, __x._M_finish); 106*e4b17023SJohn Marino std::swap(_M_end_of_storage, __x._M_end_of_storage); 107*e4b17023SJohn Marino } 108*e4b17023SJohn Marino }; 109*e4b17023SJohn Marino 110*e4b17023SJohn Marino public: 111*e4b17023SJohn Marino typedef _Alloc allocator_type; 112*e4b17023SJohn Marino 113*e4b17023SJohn Marino _Tp_alloc_type& 114*e4b17023SJohn Marino _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT 115*e4b17023SJohn Marino { return *static_cast<_Tp_alloc_type*>(&this->_M_impl); } 116*e4b17023SJohn Marino 117*e4b17023SJohn Marino const _Tp_alloc_type& 118*e4b17023SJohn Marino _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT 119*e4b17023SJohn Marino { return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); } 120*e4b17023SJohn Marino 121*e4b17023SJohn Marino allocator_type 122*e4b17023SJohn Marino get_allocator() const _GLIBCXX_NOEXCEPT 123*e4b17023SJohn Marino { return allocator_type(_M_get_Tp_allocator()); } 124*e4b17023SJohn Marino 125*e4b17023SJohn Marino _Vector_base() 126*e4b17023SJohn Marino : _M_impl() { } 127*e4b17023SJohn Marino 128*e4b17023SJohn Marino _Vector_base(const allocator_type& __a) 129*e4b17023SJohn Marino : _M_impl(__a) { } 130*e4b17023SJohn Marino 131*e4b17023SJohn Marino _Vector_base(size_t __n) 132*e4b17023SJohn Marino : _M_impl() 133*e4b17023SJohn Marino { _M_create_storage(__n); } 134*e4b17023SJohn Marino 135*e4b17023SJohn Marino _Vector_base(size_t __n, const allocator_type& __a) 136*e4b17023SJohn Marino : _M_impl(__a) 137*e4b17023SJohn Marino { _M_create_storage(__n); } 138*e4b17023SJohn Marino 139*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__ 140*e4b17023SJohn Marino _Vector_base(_Tp_alloc_type&& __a) 141*e4b17023SJohn Marino : _M_impl(std::move(__a)) { } 142*e4b17023SJohn Marino 143*e4b17023SJohn Marino _Vector_base(_Vector_base&& __x) 144*e4b17023SJohn Marino : _M_impl(std::move(__x._M_get_Tp_allocator())) 145*e4b17023SJohn Marino { this->_M_impl._M_swap_data(__x._M_impl); } 146*e4b17023SJohn Marino 147*e4b17023SJohn Marino _Vector_base(_Vector_base&& __x, const allocator_type& __a) 148*e4b17023SJohn Marino : _M_impl(__a) 149*e4b17023SJohn Marino { 150*e4b17023SJohn Marino if (__x.get_allocator() == __a) 151*e4b17023SJohn Marino this->_M_impl._M_swap_data(__x._M_impl); 152*e4b17023SJohn Marino else 153*e4b17023SJohn Marino { 154*e4b17023SJohn Marino size_t __n = __x._M_impl._M_finish - __x._M_impl._M_start; 155*e4b17023SJohn Marino _M_create_storage(__n); 156*e4b17023SJohn Marino } 157*e4b17023SJohn Marino } 158*e4b17023SJohn Marino #endif 159*e4b17023SJohn Marino 160*e4b17023SJohn Marino ~_Vector_base() 161*e4b17023SJohn Marino { _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage 162*e4b17023SJohn Marino - this->_M_impl._M_start); } 163*e4b17023SJohn Marino 164*e4b17023SJohn Marino public: 165*e4b17023SJohn Marino _Vector_impl _M_impl; 166*e4b17023SJohn Marino 167*e4b17023SJohn Marino pointer 168*e4b17023SJohn Marino _M_allocate(size_t __n) 169*e4b17023SJohn Marino { return __n != 0 ? _M_impl.allocate(__n) : 0; } 170*e4b17023SJohn Marino 171*e4b17023SJohn Marino void 172*e4b17023SJohn Marino _M_deallocate(pointer __p, size_t __n) 173*e4b17023SJohn Marino { 174*e4b17023SJohn Marino if (__p) 175*e4b17023SJohn Marino _M_impl.deallocate(__p, __n); 176*e4b17023SJohn Marino } 177*e4b17023SJohn Marino 178*e4b17023SJohn Marino private: 179*e4b17023SJohn Marino void 180*e4b17023SJohn Marino _M_create_storage(size_t __n) 181*e4b17023SJohn Marino { 182*e4b17023SJohn Marino this->_M_impl._M_start = this->_M_allocate(__n); 183*e4b17023SJohn Marino this->_M_impl._M_finish = this->_M_impl._M_start; 184*e4b17023SJohn Marino this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n; 185*e4b17023SJohn Marino } 186*e4b17023SJohn Marino }; 187*e4b17023SJohn Marino 188*e4b17023SJohn Marino 189*e4b17023SJohn Marino /** 190*e4b17023SJohn Marino * @brief A standard container which offers fixed time access to 191*e4b17023SJohn Marino * individual elements in any order. 192*e4b17023SJohn Marino * 193*e4b17023SJohn Marino * @ingroup sequences 194*e4b17023SJohn Marino * 195*e4b17023SJohn Marino * Meets the requirements of a <a href="tables.html#65">container</a>, a 196*e4b17023SJohn Marino * <a href="tables.html#66">reversible container</a>, and a 197*e4b17023SJohn Marino * <a href="tables.html#67">sequence</a>, including the 198*e4b17023SJohn Marino * <a href="tables.html#68">optional sequence requirements</a> with the 199*e4b17023SJohn Marino * %exception of @c push_front and @c pop_front. 200*e4b17023SJohn Marino * 201*e4b17023SJohn Marino * In some terminology a %vector can be described as a dynamic 202*e4b17023SJohn Marino * C-style array, it offers fast and efficient access to individual 203*e4b17023SJohn Marino * elements in any order and saves the user from worrying about 204*e4b17023SJohn Marino * memory and size allocation. Subscripting ( @c [] ) access is 205*e4b17023SJohn Marino * also provided as with C-style arrays. 206*e4b17023SJohn Marino */ 207*e4b17023SJohn Marino template<typename _Tp, typename _Alloc = std::allocator<_Tp> > 208*e4b17023SJohn Marino class vector : protected _Vector_base<_Tp, _Alloc> 209*e4b17023SJohn Marino { 210*e4b17023SJohn Marino // Concept requirements. 211*e4b17023SJohn Marino typedef typename _Alloc::value_type _Alloc_value_type; 212*e4b17023SJohn Marino __glibcxx_class_requires(_Tp, _SGIAssignableConcept) 213*e4b17023SJohn Marino __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept) 214*e4b17023SJohn Marino 215*e4b17023SJohn Marino typedef _Vector_base<_Tp, _Alloc> _Base; 216*e4b17023SJohn Marino typedef typename _Base::_Tp_alloc_type _Tp_alloc_type; 217*e4b17023SJohn Marino typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Alloc_traits; 218*e4b17023SJohn Marino 219*e4b17023SJohn Marino public: 220*e4b17023SJohn Marino typedef _Tp value_type; 221*e4b17023SJohn Marino typedef typename _Base::pointer pointer; 222*e4b17023SJohn Marino typedef typename _Alloc_traits::const_pointer const_pointer; 223*e4b17023SJohn Marino typedef typename _Alloc_traits::reference reference; 224*e4b17023SJohn Marino typedef typename _Alloc_traits::const_reference const_reference; 225*e4b17023SJohn Marino typedef __gnu_cxx::__normal_iterator<pointer, vector> iterator; 226*e4b17023SJohn Marino typedef __gnu_cxx::__normal_iterator<const_pointer, vector> 227*e4b17023SJohn Marino const_iterator; 228*e4b17023SJohn Marino typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 229*e4b17023SJohn Marino typedef std::reverse_iterator<iterator> reverse_iterator; 230*e4b17023SJohn Marino typedef size_t size_type; 231*e4b17023SJohn Marino typedef ptrdiff_t difference_type; 232*e4b17023SJohn Marino typedef _Alloc allocator_type; 233*e4b17023SJohn Marino 234*e4b17023SJohn Marino protected: 235*e4b17023SJohn Marino using _Base::_M_allocate; 236*e4b17023SJohn Marino using _Base::_M_deallocate; 237*e4b17023SJohn Marino using _Base::_M_impl; 238*e4b17023SJohn Marino using _Base::_M_get_Tp_allocator; 239*e4b17023SJohn Marino 240*e4b17023SJohn Marino public: 241*e4b17023SJohn Marino // [23.2.4.1] construct/copy/destroy 242*e4b17023SJohn Marino // (assign() and get_allocator() are also listed in this section) 243*e4b17023SJohn Marino /** 244*e4b17023SJohn Marino * @brief Default constructor creates no elements. 245*e4b17023SJohn Marino */ 246*e4b17023SJohn Marino vector() 247*e4b17023SJohn Marino : _Base() { } 248*e4b17023SJohn Marino 249*e4b17023SJohn Marino /** 250*e4b17023SJohn Marino * @brief Creates a %vector with no elements. 251*e4b17023SJohn Marino * @param __a An allocator object. 252*e4b17023SJohn Marino */ 253*e4b17023SJohn Marino explicit 254*e4b17023SJohn Marino vector(const allocator_type& __a) 255*e4b17023SJohn Marino : _Base(__a) { } 256*e4b17023SJohn Marino 257*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__ 258*e4b17023SJohn Marino /** 259*e4b17023SJohn Marino * @brief Creates a %vector with default constructed elements. 260*e4b17023SJohn Marino * @param __n The number of elements to initially create. 261*e4b17023SJohn Marino * 262*e4b17023SJohn Marino * This constructor fills the %vector with @a __n default 263*e4b17023SJohn Marino * constructed elements. 264*e4b17023SJohn Marino */ 265*e4b17023SJohn Marino explicit 266*e4b17023SJohn Marino vector(size_type __n) 267*e4b17023SJohn Marino : _Base(__n) 268*e4b17023SJohn Marino { _M_default_initialize(__n); } 269*e4b17023SJohn Marino 270*e4b17023SJohn Marino /** 271*e4b17023SJohn Marino * @brief Creates a %vector with copies of an exemplar element. 272*e4b17023SJohn Marino * @param __n The number of elements to initially create. 273*e4b17023SJohn Marino * @param __value An element to copy. 274*e4b17023SJohn Marino * @param __a An allocator. 275*e4b17023SJohn Marino * 276*e4b17023SJohn Marino * This constructor fills the %vector with @a __n copies of @a __value. 277*e4b17023SJohn Marino */ 278*e4b17023SJohn Marino vector(size_type __n, const value_type& __value, 279*e4b17023SJohn Marino const allocator_type& __a = allocator_type()) 280*e4b17023SJohn Marino : _Base(__n, __a) 281*e4b17023SJohn Marino { _M_fill_initialize(__n, __value); } 282*e4b17023SJohn Marino #else 283*e4b17023SJohn Marino /** 284*e4b17023SJohn Marino * @brief Creates a %vector with copies of an exemplar element. 285*e4b17023SJohn Marino * @param __n The number of elements to initially create. 286*e4b17023SJohn Marino * @param __value An element to copy. 287*e4b17023SJohn Marino * @param __a An allocator. 288*e4b17023SJohn Marino * 289*e4b17023SJohn Marino * This constructor fills the %vector with @a __n copies of @a __value. 290*e4b17023SJohn Marino */ 291*e4b17023SJohn Marino explicit 292*e4b17023SJohn Marino vector(size_type __n, const value_type& __value = value_type(), 293*e4b17023SJohn Marino const allocator_type& __a = allocator_type()) 294*e4b17023SJohn Marino : _Base(__n, __a) 295*e4b17023SJohn Marino { _M_fill_initialize(__n, __value); } 296*e4b17023SJohn Marino #endif 297*e4b17023SJohn Marino 298*e4b17023SJohn Marino /** 299*e4b17023SJohn Marino * @brief %Vector copy constructor. 300*e4b17023SJohn Marino * @param __x A %vector of identical element and allocator types. 301*e4b17023SJohn Marino * 302*e4b17023SJohn Marino * The newly-created %vector uses a copy of the allocation 303*e4b17023SJohn Marino * object used by @a __x. All the elements of @a __x are copied, 304*e4b17023SJohn Marino * but any extra memory in 305*e4b17023SJohn Marino * @a __x (for fast expansion) will not be copied. 306*e4b17023SJohn Marino */ 307*e4b17023SJohn Marino vector(const vector& __x) 308*e4b17023SJohn Marino : _Base(__x.size(), 309*e4b17023SJohn Marino _Alloc_traits::_S_select_on_copy(__x._M_get_Tp_allocator())) 310*e4b17023SJohn Marino { this->_M_impl._M_finish = 311*e4b17023SJohn Marino std::__uninitialized_copy_a(__x.begin(), __x.end(), 312*e4b17023SJohn Marino this->_M_impl._M_start, 313*e4b17023SJohn Marino _M_get_Tp_allocator()); 314*e4b17023SJohn Marino } 315*e4b17023SJohn Marino 316*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__ 317*e4b17023SJohn Marino /** 318*e4b17023SJohn Marino * @brief %Vector move constructor. 319*e4b17023SJohn Marino * @param __x A %vector of identical element and allocator types. 320*e4b17023SJohn Marino * 321*e4b17023SJohn Marino * The newly-created %vector contains the exact contents of @a __x. 322*e4b17023SJohn Marino * The contents of @a __x are a valid, but unspecified %vector. 323*e4b17023SJohn Marino */ 324*e4b17023SJohn Marino vector(vector&& __x) noexcept 325*e4b17023SJohn Marino : _Base(std::move(__x)) { } 326*e4b17023SJohn Marino 327*e4b17023SJohn Marino /// Copy constructor with alternative allocator 328*e4b17023SJohn Marino vector(const vector& __x, const allocator_type& __a) 329*e4b17023SJohn Marino : _Base(__x.size(), __a) 330*e4b17023SJohn Marino { this->_M_impl._M_finish = 331*e4b17023SJohn Marino std::__uninitialized_copy_a(__x.begin(), __x.end(), 332*e4b17023SJohn Marino this->_M_impl._M_start, 333*e4b17023SJohn Marino _M_get_Tp_allocator()); 334*e4b17023SJohn Marino } 335*e4b17023SJohn Marino 336*e4b17023SJohn Marino /// Move constructor with alternative allocator 337*e4b17023SJohn Marino vector(vector&& __rv, const allocator_type& __m) 338*e4b17023SJohn Marino : _Base(std::move(__rv), __m) 339*e4b17023SJohn Marino { 340*e4b17023SJohn Marino if (__rv.get_allocator() != __m) 341*e4b17023SJohn Marino { 342*e4b17023SJohn Marino this->_M_impl._M_finish = 343*e4b17023SJohn Marino std::__uninitialized_move_a(__rv.begin(), __rv.end(), 344*e4b17023SJohn Marino this->_M_impl._M_start, 345*e4b17023SJohn Marino _M_get_Tp_allocator()); 346*e4b17023SJohn Marino __rv.clear(); 347*e4b17023SJohn Marino } 348*e4b17023SJohn Marino } 349*e4b17023SJohn Marino 350*e4b17023SJohn Marino /** 351*e4b17023SJohn Marino * @brief Builds a %vector from an initializer list. 352*e4b17023SJohn Marino * @param __l An initializer_list. 353*e4b17023SJohn Marino * @param __a An allocator. 354*e4b17023SJohn Marino * 355*e4b17023SJohn Marino * Create a %vector consisting of copies of the elements in the 356*e4b17023SJohn Marino * initializer_list @a __l. 357*e4b17023SJohn Marino * 358*e4b17023SJohn Marino * This will call the element type's copy constructor N times 359*e4b17023SJohn Marino * (where N is @a __l.size()) and do no memory reallocation. 360*e4b17023SJohn Marino */ 361*e4b17023SJohn Marino vector(initializer_list<value_type> __l, 362*e4b17023SJohn Marino const allocator_type& __a = allocator_type()) 363*e4b17023SJohn Marino : _Base(__a) 364*e4b17023SJohn Marino { 365*e4b17023SJohn Marino _M_range_initialize(__l.begin(), __l.end(), 366*e4b17023SJohn Marino random_access_iterator_tag()); 367*e4b17023SJohn Marino } 368*e4b17023SJohn Marino #endif 369*e4b17023SJohn Marino 370*e4b17023SJohn Marino /** 371*e4b17023SJohn Marino * @brief Builds a %vector from a range. 372*e4b17023SJohn Marino * @param __first An input iterator. 373*e4b17023SJohn Marino * @param __last An input iterator. 374*e4b17023SJohn Marino * @param __a An allocator. 375*e4b17023SJohn Marino * 376*e4b17023SJohn Marino * Create a %vector consisting of copies of the elements from 377*e4b17023SJohn Marino * [first,last). 378*e4b17023SJohn Marino * 379*e4b17023SJohn Marino * If the iterators are forward, bidirectional, or 380*e4b17023SJohn Marino * random-access, then this will call the elements' copy 381*e4b17023SJohn Marino * constructor N times (where N is distance(first,last)) and do 382*e4b17023SJohn Marino * no memory reallocation. But if only input iterators are 383*e4b17023SJohn Marino * used, then this will do at most 2N calls to the copy 384*e4b17023SJohn Marino * constructor, and logN memory reallocations. 385*e4b17023SJohn Marino */ 386*e4b17023SJohn Marino template<typename _InputIterator> 387*e4b17023SJohn Marino vector(_InputIterator __first, _InputIterator __last, 388*e4b17023SJohn Marino const allocator_type& __a = allocator_type()) 389*e4b17023SJohn Marino : _Base(__a) 390*e4b17023SJohn Marino { 391*e4b17023SJohn Marino // Check whether it's an integral type. If so, it's not an iterator. 392*e4b17023SJohn Marino typedef typename std::__is_integer<_InputIterator>::__type _Integral; 393*e4b17023SJohn Marino _M_initialize_dispatch(__first, __last, _Integral()); 394*e4b17023SJohn Marino } 395*e4b17023SJohn Marino 396*e4b17023SJohn Marino /** 397*e4b17023SJohn Marino * The dtor only erases the elements, and note that if the 398*e4b17023SJohn Marino * elements themselves are pointers, the pointed-to memory is 399*e4b17023SJohn Marino * not touched in any way. Managing the pointer is the user's 400*e4b17023SJohn Marino * responsibility. 401*e4b17023SJohn Marino */ 402*e4b17023SJohn Marino ~vector() _GLIBCXX_NOEXCEPT 403*e4b17023SJohn Marino { std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish, 404*e4b17023SJohn Marino _M_get_Tp_allocator()); } 405*e4b17023SJohn Marino 406*e4b17023SJohn Marino /** 407*e4b17023SJohn Marino * @brief %Vector assignment operator. 408*e4b17023SJohn Marino * @param __x A %vector of identical element and allocator types. 409*e4b17023SJohn Marino * 410*e4b17023SJohn Marino * All the elements of @a __x are copied, but any extra memory in 411*e4b17023SJohn Marino * @a __x (for fast expansion) will not be copied. Unlike the 412*e4b17023SJohn Marino * copy constructor, the allocator object is not copied. 413*e4b17023SJohn Marino */ 414*e4b17023SJohn Marino vector& 415*e4b17023SJohn Marino operator=(const vector& __x); 416*e4b17023SJohn Marino 417*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__ 418*e4b17023SJohn Marino /** 419*e4b17023SJohn Marino * @brief %Vector move assignment operator. 420*e4b17023SJohn Marino * @param __x A %vector of identical element and allocator types. 421*e4b17023SJohn Marino * 422*e4b17023SJohn Marino * The contents of @a __x are moved into this %vector (without copying, 423*e4b17023SJohn Marino * if the allocators permit it). 424*e4b17023SJohn Marino * @a __x is a valid, but unspecified %vector. 425*e4b17023SJohn Marino */ 426*e4b17023SJohn Marino vector& 427*e4b17023SJohn Marino operator=(vector&& __x) noexcept(_Alloc_traits::_S_nothrow_move()) 428*e4b17023SJohn Marino { 429*e4b17023SJohn Marino constexpr bool __move_storage = 430*e4b17023SJohn Marino _Alloc_traits::_S_propagate_on_move_assign() 431*e4b17023SJohn Marino || _Alloc_traits::_S_always_equal(); 432*e4b17023SJohn Marino _M_move_assign(std::move(__x), 433*e4b17023SJohn Marino integral_constant<bool, __move_storage>()); 434*e4b17023SJohn Marino return *this; 435*e4b17023SJohn Marino } 436*e4b17023SJohn Marino 437*e4b17023SJohn Marino /** 438*e4b17023SJohn Marino * @brief %Vector list assignment operator. 439*e4b17023SJohn Marino * @param __l An initializer_list. 440*e4b17023SJohn Marino * 441*e4b17023SJohn Marino * This function fills a %vector with copies of the elements in the 442*e4b17023SJohn Marino * initializer list @a __l. 443*e4b17023SJohn Marino * 444*e4b17023SJohn Marino * Note that the assignment completely changes the %vector and 445*e4b17023SJohn Marino * that the resulting %vector's size is the same as the number 446*e4b17023SJohn Marino * of elements assigned. Old data may be lost. 447*e4b17023SJohn Marino */ 448*e4b17023SJohn Marino vector& 449*e4b17023SJohn Marino operator=(initializer_list<value_type> __l) 450*e4b17023SJohn Marino { 451*e4b17023SJohn Marino this->assign(__l.begin(), __l.end()); 452*e4b17023SJohn Marino return *this; 453*e4b17023SJohn Marino } 454*e4b17023SJohn Marino #endif 455*e4b17023SJohn Marino 456*e4b17023SJohn Marino /** 457*e4b17023SJohn Marino * @brief Assigns a given value to a %vector. 458*e4b17023SJohn Marino * @param __n Number of elements to be assigned. 459*e4b17023SJohn Marino * @param __val Value to be assigned. 460*e4b17023SJohn Marino * 461*e4b17023SJohn Marino * This function fills a %vector with @a __n copies of the given 462*e4b17023SJohn Marino * value. Note that the assignment completely changes the 463*e4b17023SJohn Marino * %vector and that the resulting %vector's size is the same as 464*e4b17023SJohn Marino * the number of elements assigned. Old data may be lost. 465*e4b17023SJohn Marino */ 466*e4b17023SJohn Marino void 467*e4b17023SJohn Marino assign(size_type __n, const value_type& __val) 468*e4b17023SJohn Marino { _M_fill_assign(__n, __val); } 469*e4b17023SJohn Marino 470*e4b17023SJohn Marino /** 471*e4b17023SJohn Marino * @brief Assigns a range to a %vector. 472*e4b17023SJohn Marino * @param __first An input iterator. 473*e4b17023SJohn Marino * @param __last An input iterator. 474*e4b17023SJohn Marino * 475*e4b17023SJohn Marino * This function fills a %vector with copies of the elements in the 476*e4b17023SJohn Marino * range [__first,__last). 477*e4b17023SJohn Marino * 478*e4b17023SJohn Marino * Note that the assignment completely changes the %vector and 479*e4b17023SJohn Marino * that the resulting %vector's size is the same as the number 480*e4b17023SJohn Marino * of elements assigned. Old data may be lost. 481*e4b17023SJohn Marino */ 482*e4b17023SJohn Marino template<typename _InputIterator> 483*e4b17023SJohn Marino void 484*e4b17023SJohn Marino assign(_InputIterator __first, _InputIterator __last) 485*e4b17023SJohn Marino { 486*e4b17023SJohn Marino // Check whether it's an integral type. If so, it's not an iterator. 487*e4b17023SJohn Marino typedef typename std::__is_integer<_InputIterator>::__type _Integral; 488*e4b17023SJohn Marino _M_assign_dispatch(__first, __last, _Integral()); 489*e4b17023SJohn Marino } 490*e4b17023SJohn Marino 491*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__ 492*e4b17023SJohn Marino /** 493*e4b17023SJohn Marino * @brief Assigns an initializer list to a %vector. 494*e4b17023SJohn Marino * @param __l An initializer_list. 495*e4b17023SJohn Marino * 496*e4b17023SJohn Marino * This function fills a %vector with copies of the elements in the 497*e4b17023SJohn Marino * initializer list @a __l. 498*e4b17023SJohn Marino * 499*e4b17023SJohn Marino * Note that the assignment completely changes the %vector and 500*e4b17023SJohn Marino * that the resulting %vector's size is the same as the number 501*e4b17023SJohn Marino * of elements assigned. Old data may be lost. 502*e4b17023SJohn Marino */ 503*e4b17023SJohn Marino void 504*e4b17023SJohn Marino assign(initializer_list<value_type> __l) 505*e4b17023SJohn Marino { this->assign(__l.begin(), __l.end()); } 506*e4b17023SJohn Marino #endif 507*e4b17023SJohn Marino 508*e4b17023SJohn Marino /// Get a copy of the memory allocation object. 509*e4b17023SJohn Marino using _Base::get_allocator; 510*e4b17023SJohn Marino 511*e4b17023SJohn Marino // iterators 512*e4b17023SJohn Marino /** 513*e4b17023SJohn Marino * Returns a read/write iterator that points to the first 514*e4b17023SJohn Marino * element in the %vector. Iteration is done in ordinary 515*e4b17023SJohn Marino * element order. 516*e4b17023SJohn Marino */ 517*e4b17023SJohn Marino iterator 518*e4b17023SJohn Marino begin() _GLIBCXX_NOEXCEPT 519*e4b17023SJohn Marino { return iterator(this->_M_impl._M_start); } 520*e4b17023SJohn Marino 521*e4b17023SJohn Marino /** 522*e4b17023SJohn Marino * Returns a read-only (constant) iterator that points to the 523*e4b17023SJohn Marino * first element in the %vector. Iteration is done in ordinary 524*e4b17023SJohn Marino * element order. 525*e4b17023SJohn Marino */ 526*e4b17023SJohn Marino const_iterator 527*e4b17023SJohn Marino begin() const _GLIBCXX_NOEXCEPT 528*e4b17023SJohn Marino { return const_iterator(this->_M_impl._M_start); } 529*e4b17023SJohn Marino 530*e4b17023SJohn Marino /** 531*e4b17023SJohn Marino * Returns a read/write iterator that points one past the last 532*e4b17023SJohn Marino * element in the %vector. Iteration is done in ordinary 533*e4b17023SJohn Marino * element order. 534*e4b17023SJohn Marino */ 535*e4b17023SJohn Marino iterator 536*e4b17023SJohn Marino end() _GLIBCXX_NOEXCEPT 537*e4b17023SJohn Marino { return iterator(this->_M_impl._M_finish); } 538*e4b17023SJohn Marino 539*e4b17023SJohn Marino /** 540*e4b17023SJohn Marino * Returns a read-only (constant) iterator that points one past 541*e4b17023SJohn Marino * the last element in the %vector. Iteration is done in 542*e4b17023SJohn Marino * ordinary element order. 543*e4b17023SJohn Marino */ 544*e4b17023SJohn Marino const_iterator 545*e4b17023SJohn Marino end() const _GLIBCXX_NOEXCEPT 546*e4b17023SJohn Marino { return const_iterator(this->_M_impl._M_finish); } 547*e4b17023SJohn Marino 548*e4b17023SJohn Marino /** 549*e4b17023SJohn Marino * Returns a read/write reverse iterator that points to the 550*e4b17023SJohn Marino * last element in the %vector. Iteration is done in reverse 551*e4b17023SJohn Marino * element order. 552*e4b17023SJohn Marino */ 553*e4b17023SJohn Marino reverse_iterator 554*e4b17023SJohn Marino rbegin() _GLIBCXX_NOEXCEPT 555*e4b17023SJohn Marino { return reverse_iterator(end()); } 556*e4b17023SJohn Marino 557*e4b17023SJohn Marino /** 558*e4b17023SJohn Marino * Returns a read-only (constant) reverse iterator that points 559*e4b17023SJohn Marino * to the last element in the %vector. Iteration is done in 560*e4b17023SJohn Marino * reverse element order. 561*e4b17023SJohn Marino */ 562*e4b17023SJohn Marino const_reverse_iterator 563*e4b17023SJohn Marino rbegin() const _GLIBCXX_NOEXCEPT 564*e4b17023SJohn Marino { return const_reverse_iterator(end()); } 565*e4b17023SJohn Marino 566*e4b17023SJohn Marino /** 567*e4b17023SJohn Marino * Returns a read/write reverse iterator that points to one 568*e4b17023SJohn Marino * before the first element in the %vector. Iteration is done 569*e4b17023SJohn Marino * in reverse element order. 570*e4b17023SJohn Marino */ 571*e4b17023SJohn Marino reverse_iterator 572*e4b17023SJohn Marino rend() _GLIBCXX_NOEXCEPT 573*e4b17023SJohn Marino { return reverse_iterator(begin()); } 574*e4b17023SJohn Marino 575*e4b17023SJohn Marino /** 576*e4b17023SJohn Marino * Returns a read-only (constant) reverse iterator that points 577*e4b17023SJohn Marino * to one before the first element in the %vector. Iteration 578*e4b17023SJohn Marino * is done in reverse element order. 579*e4b17023SJohn Marino */ 580*e4b17023SJohn Marino const_reverse_iterator 581*e4b17023SJohn Marino rend() const _GLIBCXX_NOEXCEPT 582*e4b17023SJohn Marino { return const_reverse_iterator(begin()); } 583*e4b17023SJohn Marino 584*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__ 585*e4b17023SJohn Marino /** 586*e4b17023SJohn Marino * Returns a read-only (constant) iterator that points to the 587*e4b17023SJohn Marino * first element in the %vector. Iteration is done in ordinary 588*e4b17023SJohn Marino * element order. 589*e4b17023SJohn Marino */ 590*e4b17023SJohn Marino const_iterator 591*e4b17023SJohn Marino cbegin() const noexcept 592*e4b17023SJohn Marino { return const_iterator(this->_M_impl._M_start); } 593*e4b17023SJohn Marino 594*e4b17023SJohn Marino /** 595*e4b17023SJohn Marino * Returns a read-only (constant) iterator that points one past 596*e4b17023SJohn Marino * the last element in the %vector. Iteration is done in 597*e4b17023SJohn Marino * ordinary element order. 598*e4b17023SJohn Marino */ 599*e4b17023SJohn Marino const_iterator 600*e4b17023SJohn Marino cend() const noexcept 601*e4b17023SJohn Marino { return const_iterator(this->_M_impl._M_finish); } 602*e4b17023SJohn Marino 603*e4b17023SJohn Marino /** 604*e4b17023SJohn Marino * Returns a read-only (constant) reverse iterator that points 605*e4b17023SJohn Marino * to the last element in the %vector. Iteration is done in 606*e4b17023SJohn Marino * reverse element order. 607*e4b17023SJohn Marino */ 608*e4b17023SJohn Marino const_reverse_iterator 609*e4b17023SJohn Marino crbegin() const noexcept 610*e4b17023SJohn Marino { return const_reverse_iterator(end()); } 611*e4b17023SJohn Marino 612*e4b17023SJohn Marino /** 613*e4b17023SJohn Marino * Returns a read-only (constant) reverse iterator that points 614*e4b17023SJohn Marino * to one before the first element in the %vector. Iteration 615*e4b17023SJohn Marino * is done in reverse element order. 616*e4b17023SJohn Marino */ 617*e4b17023SJohn Marino const_reverse_iterator 618*e4b17023SJohn Marino crend() const noexcept 619*e4b17023SJohn Marino { return const_reverse_iterator(begin()); } 620*e4b17023SJohn Marino #endif 621*e4b17023SJohn Marino 622*e4b17023SJohn Marino // [23.2.4.2] capacity 623*e4b17023SJohn Marino /** Returns the number of elements in the %vector. */ 624*e4b17023SJohn Marino size_type 625*e4b17023SJohn Marino size() const _GLIBCXX_NOEXCEPT 626*e4b17023SJohn Marino { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); } 627*e4b17023SJohn Marino 628*e4b17023SJohn Marino /** Returns the size() of the largest possible %vector. */ 629*e4b17023SJohn Marino size_type 630*e4b17023SJohn Marino max_size() const _GLIBCXX_NOEXCEPT 631*e4b17023SJohn Marino { return _Alloc_traits::max_size(_M_get_Tp_allocator()); } 632*e4b17023SJohn Marino 633*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__ 634*e4b17023SJohn Marino /** 635*e4b17023SJohn Marino * @brief Resizes the %vector to the specified number of elements. 636*e4b17023SJohn Marino * @param __new_size Number of elements the %vector should contain. 637*e4b17023SJohn Marino * 638*e4b17023SJohn Marino * This function will %resize the %vector to the specified 639*e4b17023SJohn Marino * number of elements. If the number is smaller than the 640*e4b17023SJohn Marino * %vector's current size the %vector is truncated, otherwise 641*e4b17023SJohn Marino * default constructed elements are appended. 642*e4b17023SJohn Marino */ 643*e4b17023SJohn Marino void 644*e4b17023SJohn Marino resize(size_type __new_size) 645*e4b17023SJohn Marino { 646*e4b17023SJohn Marino if (__new_size > size()) 647*e4b17023SJohn Marino _M_default_append(__new_size - size()); 648*e4b17023SJohn Marino else if (__new_size < size()) 649*e4b17023SJohn Marino _M_erase_at_end(this->_M_impl._M_start + __new_size); 650*e4b17023SJohn Marino } 651*e4b17023SJohn Marino 652*e4b17023SJohn Marino /** 653*e4b17023SJohn Marino * @brief Resizes the %vector to the specified number of elements. 654*e4b17023SJohn Marino * @param __new_size Number of elements the %vector should contain. 655*e4b17023SJohn Marino * @param __x Data with which new elements should be populated. 656*e4b17023SJohn Marino * 657*e4b17023SJohn Marino * This function will %resize the %vector to the specified 658*e4b17023SJohn Marino * number of elements. If the number is smaller than the 659*e4b17023SJohn Marino * %vector's current size the %vector is truncated, otherwise 660*e4b17023SJohn Marino * the %vector is extended and new elements are populated with 661*e4b17023SJohn Marino * given data. 662*e4b17023SJohn Marino */ 663*e4b17023SJohn Marino void 664*e4b17023SJohn Marino resize(size_type __new_size, const value_type& __x) 665*e4b17023SJohn Marino { 666*e4b17023SJohn Marino if (__new_size > size()) 667*e4b17023SJohn Marino insert(end(), __new_size - size(), __x); 668*e4b17023SJohn Marino else if (__new_size < size()) 669*e4b17023SJohn Marino _M_erase_at_end(this->_M_impl._M_start + __new_size); 670*e4b17023SJohn Marino } 671*e4b17023SJohn Marino #else 672*e4b17023SJohn Marino /** 673*e4b17023SJohn Marino * @brief Resizes the %vector to the specified number of elements. 674*e4b17023SJohn Marino * @param __new_size Number of elements the %vector should contain. 675*e4b17023SJohn Marino * @param __x Data with which new elements should be populated. 676*e4b17023SJohn Marino * 677*e4b17023SJohn Marino * This function will %resize the %vector to the specified 678*e4b17023SJohn Marino * number of elements. If the number is smaller than the 679*e4b17023SJohn Marino * %vector's current size the %vector is truncated, otherwise 680*e4b17023SJohn Marino * the %vector is extended and new elements are populated with 681*e4b17023SJohn Marino * given data. 682*e4b17023SJohn Marino */ 683*e4b17023SJohn Marino void 684*e4b17023SJohn Marino resize(size_type __new_size, value_type __x = value_type()) 685*e4b17023SJohn Marino { 686*e4b17023SJohn Marino if (__new_size > size()) 687*e4b17023SJohn Marino insert(end(), __new_size - size(), __x); 688*e4b17023SJohn Marino else if (__new_size < size()) 689*e4b17023SJohn Marino _M_erase_at_end(this->_M_impl._M_start + __new_size); 690*e4b17023SJohn Marino } 691*e4b17023SJohn Marino #endif 692*e4b17023SJohn Marino 693*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__ 694*e4b17023SJohn Marino /** A non-binding request to reduce capacity() to size(). */ 695*e4b17023SJohn Marino void 696*e4b17023SJohn Marino shrink_to_fit() 697*e4b17023SJohn Marino { _M_shrink_to_fit(); } 698*e4b17023SJohn Marino #endif 699*e4b17023SJohn Marino 700*e4b17023SJohn Marino /** 701*e4b17023SJohn Marino * Returns the total number of elements that the %vector can 702*e4b17023SJohn Marino * hold before needing to allocate more memory. 703*e4b17023SJohn Marino */ 704*e4b17023SJohn Marino size_type 705*e4b17023SJohn Marino capacity() const _GLIBCXX_NOEXCEPT 706*e4b17023SJohn Marino { return size_type(this->_M_impl._M_end_of_storage 707*e4b17023SJohn Marino - this->_M_impl._M_start); } 708*e4b17023SJohn Marino 709*e4b17023SJohn Marino /** 710*e4b17023SJohn Marino * Returns true if the %vector is empty. (Thus begin() would 711*e4b17023SJohn Marino * equal end().) 712*e4b17023SJohn Marino */ 713*e4b17023SJohn Marino bool 714*e4b17023SJohn Marino empty() const _GLIBCXX_NOEXCEPT 715*e4b17023SJohn Marino { return begin() == end(); } 716*e4b17023SJohn Marino 717*e4b17023SJohn Marino /** 718*e4b17023SJohn Marino * @brief Attempt to preallocate enough memory for specified number of 719*e4b17023SJohn Marino * elements. 720*e4b17023SJohn Marino * @param __n Number of elements required. 721*e4b17023SJohn Marino * @throw std::length_error If @a n exceeds @c max_size(). 722*e4b17023SJohn Marino * 723*e4b17023SJohn Marino * This function attempts to reserve enough memory for the 724*e4b17023SJohn Marino * %vector to hold the specified number of elements. If the 725*e4b17023SJohn Marino * number requested is more than max_size(), length_error is 726*e4b17023SJohn Marino * thrown. 727*e4b17023SJohn Marino * 728*e4b17023SJohn Marino * The advantage of this function is that if optimal code is a 729*e4b17023SJohn Marino * necessity and the user can determine the number of elements 730*e4b17023SJohn Marino * that will be required, the user can reserve the memory in 731*e4b17023SJohn Marino * %advance, and thus prevent a possible reallocation of memory 732*e4b17023SJohn Marino * and copying of %vector data. 733*e4b17023SJohn Marino */ 734*e4b17023SJohn Marino void 735*e4b17023SJohn Marino reserve(size_type __n); 736*e4b17023SJohn Marino 737*e4b17023SJohn Marino // element access 738*e4b17023SJohn Marino /** 739*e4b17023SJohn Marino * @brief Subscript access to the data contained in the %vector. 740*e4b17023SJohn Marino * @param __n The index of the element for which data should be 741*e4b17023SJohn Marino * accessed. 742*e4b17023SJohn Marino * @return Read/write reference to data. 743*e4b17023SJohn Marino * 744*e4b17023SJohn Marino * This operator allows for easy, array-style, data access. 745*e4b17023SJohn Marino * Note that data access with this operator is unchecked and 746*e4b17023SJohn Marino * out_of_range lookups are not defined. (For checked lookups 747*e4b17023SJohn Marino * see at().) 748*e4b17023SJohn Marino */ 749*e4b17023SJohn Marino reference 750*e4b17023SJohn Marino operator[](size_type __n) 751*e4b17023SJohn Marino { return *(this->_M_impl._M_start + __n); } 752*e4b17023SJohn Marino 753*e4b17023SJohn Marino /** 754*e4b17023SJohn Marino * @brief Subscript access to the data contained in the %vector. 755*e4b17023SJohn Marino * @param __n The index of the element for which data should be 756*e4b17023SJohn Marino * accessed. 757*e4b17023SJohn Marino * @return Read-only (constant) reference to data. 758*e4b17023SJohn Marino * 759*e4b17023SJohn Marino * This operator allows for easy, array-style, data access. 760*e4b17023SJohn Marino * Note that data access with this operator is unchecked and 761*e4b17023SJohn Marino * out_of_range lookups are not defined. (For checked lookups 762*e4b17023SJohn Marino * see at().) 763*e4b17023SJohn Marino */ 764*e4b17023SJohn Marino const_reference 765*e4b17023SJohn Marino operator[](size_type __n) const 766*e4b17023SJohn Marino { return *(this->_M_impl._M_start + __n); } 767*e4b17023SJohn Marino 768*e4b17023SJohn Marino protected: 769*e4b17023SJohn Marino /// Safety check used only from at(). 770*e4b17023SJohn Marino void 771*e4b17023SJohn Marino _M_range_check(size_type __n) const 772*e4b17023SJohn Marino { 773*e4b17023SJohn Marino if (__n >= this->size()) 774*e4b17023SJohn Marino __throw_out_of_range(__N("vector::_M_range_check")); 775*e4b17023SJohn Marino } 776*e4b17023SJohn Marino 777*e4b17023SJohn Marino public: 778*e4b17023SJohn Marino /** 779*e4b17023SJohn Marino * @brief Provides access to the data contained in the %vector. 780*e4b17023SJohn Marino * @param __n The index of the element for which data should be 781*e4b17023SJohn Marino * accessed. 782*e4b17023SJohn Marino * @return Read/write reference to data. 783*e4b17023SJohn Marino * @throw std::out_of_range If @a __n is an invalid index. 784*e4b17023SJohn Marino * 785*e4b17023SJohn Marino * This function provides for safer data access. The parameter 786*e4b17023SJohn Marino * is first checked that it is in the range of the vector. The 787*e4b17023SJohn Marino * function throws out_of_range if the check fails. 788*e4b17023SJohn Marino */ 789*e4b17023SJohn Marino reference 790*e4b17023SJohn Marino at(size_type __n) 791*e4b17023SJohn Marino { 792*e4b17023SJohn Marino _M_range_check(__n); 793*e4b17023SJohn Marino return (*this)[__n]; 794*e4b17023SJohn Marino } 795*e4b17023SJohn Marino 796*e4b17023SJohn Marino /** 797*e4b17023SJohn Marino * @brief Provides access to the data contained in the %vector. 798*e4b17023SJohn Marino * @param __n The index of the element for which data should be 799*e4b17023SJohn Marino * accessed. 800*e4b17023SJohn Marino * @return Read-only (constant) reference to data. 801*e4b17023SJohn Marino * @throw std::out_of_range If @a __n is an invalid index. 802*e4b17023SJohn Marino * 803*e4b17023SJohn Marino * This function provides for safer data access. The parameter 804*e4b17023SJohn Marino * is first checked that it is in the range of the vector. The 805*e4b17023SJohn Marino * function throws out_of_range if the check fails. 806*e4b17023SJohn Marino */ 807*e4b17023SJohn Marino const_reference 808*e4b17023SJohn Marino at(size_type __n) const 809*e4b17023SJohn Marino { 810*e4b17023SJohn Marino _M_range_check(__n); 811*e4b17023SJohn Marino return (*this)[__n]; 812*e4b17023SJohn Marino } 813*e4b17023SJohn Marino 814*e4b17023SJohn Marino /** 815*e4b17023SJohn Marino * Returns a read/write reference to the data at the first 816*e4b17023SJohn Marino * element of the %vector. 817*e4b17023SJohn Marino */ 818*e4b17023SJohn Marino reference 819*e4b17023SJohn Marino front() 820*e4b17023SJohn Marino { return *begin(); } 821*e4b17023SJohn Marino 822*e4b17023SJohn Marino /** 823*e4b17023SJohn Marino * Returns a read-only (constant) reference to the data at the first 824*e4b17023SJohn Marino * element of the %vector. 825*e4b17023SJohn Marino */ 826*e4b17023SJohn Marino const_reference 827*e4b17023SJohn Marino front() const 828*e4b17023SJohn Marino { return *begin(); } 829*e4b17023SJohn Marino 830*e4b17023SJohn Marino /** 831*e4b17023SJohn Marino * Returns a read/write reference to the data at the last 832*e4b17023SJohn Marino * element of the %vector. 833*e4b17023SJohn Marino */ 834*e4b17023SJohn Marino reference 835*e4b17023SJohn Marino back() 836*e4b17023SJohn Marino { return *(end() - 1); } 837*e4b17023SJohn Marino 838*e4b17023SJohn Marino /** 839*e4b17023SJohn Marino * Returns a read-only (constant) reference to the data at the 840*e4b17023SJohn Marino * last element of the %vector. 841*e4b17023SJohn Marino */ 842*e4b17023SJohn Marino const_reference 843*e4b17023SJohn Marino back() const 844*e4b17023SJohn Marino { return *(end() - 1); } 845*e4b17023SJohn Marino 846*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 847*e4b17023SJohn Marino // DR 464. Suggestion for new member functions in standard containers. 848*e4b17023SJohn Marino // data access 849*e4b17023SJohn Marino /** 850*e4b17023SJohn Marino * Returns a pointer such that [data(), data() + size()) is a valid 851*e4b17023SJohn Marino * range. For a non-empty %vector, data() == &front(). 852*e4b17023SJohn Marino */ 853*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__ 854*e4b17023SJohn Marino _Tp* 855*e4b17023SJohn Marino #else 856*e4b17023SJohn Marino pointer 857*e4b17023SJohn Marino #endif 858*e4b17023SJohn Marino data() _GLIBCXX_NOEXCEPT 859*e4b17023SJohn Marino { return std::__addressof(front()); } 860*e4b17023SJohn Marino 861*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__ 862*e4b17023SJohn Marino const _Tp* 863*e4b17023SJohn Marino #else 864*e4b17023SJohn Marino const_pointer 865*e4b17023SJohn Marino #endif 866*e4b17023SJohn Marino data() const _GLIBCXX_NOEXCEPT 867*e4b17023SJohn Marino { return std::__addressof(front()); } 868*e4b17023SJohn Marino 869*e4b17023SJohn Marino // [23.2.4.3] modifiers 870*e4b17023SJohn Marino /** 871*e4b17023SJohn Marino * @brief Add data to the end of the %vector. 872*e4b17023SJohn Marino * @param __x Data to be added. 873*e4b17023SJohn Marino * 874*e4b17023SJohn Marino * This is a typical stack operation. The function creates an 875*e4b17023SJohn Marino * element at the end of the %vector and assigns the given data 876*e4b17023SJohn Marino * to it. Due to the nature of a %vector this operation can be 877*e4b17023SJohn Marino * done in constant time if the %vector has preallocated space 878*e4b17023SJohn Marino * available. 879*e4b17023SJohn Marino */ 880*e4b17023SJohn Marino void 881*e4b17023SJohn Marino push_back(const value_type& __x) 882*e4b17023SJohn Marino { 883*e4b17023SJohn Marino if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage) 884*e4b17023SJohn Marino { 885*e4b17023SJohn Marino _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish, 886*e4b17023SJohn Marino __x); 887*e4b17023SJohn Marino ++this->_M_impl._M_finish; 888*e4b17023SJohn Marino } 889*e4b17023SJohn Marino else 890*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__ 891*e4b17023SJohn Marino _M_emplace_back_aux(__x); 892*e4b17023SJohn Marino #else 893*e4b17023SJohn Marino _M_insert_aux(end(), __x); 894*e4b17023SJohn Marino #endif 895*e4b17023SJohn Marino } 896*e4b17023SJohn Marino 897*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__ 898*e4b17023SJohn Marino void 899*e4b17023SJohn Marino push_back(value_type&& __x) 900*e4b17023SJohn Marino { emplace_back(std::move(__x)); } 901*e4b17023SJohn Marino 902*e4b17023SJohn Marino template<typename... _Args> 903*e4b17023SJohn Marino void 904*e4b17023SJohn Marino emplace_back(_Args&&... __args); 905*e4b17023SJohn Marino #endif 906*e4b17023SJohn Marino 907*e4b17023SJohn Marino /** 908*e4b17023SJohn Marino * @brief Removes last element. 909*e4b17023SJohn Marino * 910*e4b17023SJohn Marino * This is a typical stack operation. It shrinks the %vector by one. 911*e4b17023SJohn Marino * 912*e4b17023SJohn Marino * Note that no data is returned, and if the last element's 913*e4b17023SJohn Marino * data is needed, it should be retrieved before pop_back() is 914*e4b17023SJohn Marino * called. 915*e4b17023SJohn Marino */ 916*e4b17023SJohn Marino void 917*e4b17023SJohn Marino pop_back() 918*e4b17023SJohn Marino { 919*e4b17023SJohn Marino --this->_M_impl._M_finish; 920*e4b17023SJohn Marino _Alloc_traits::destroy(this->_M_impl, this->_M_impl._M_finish); 921*e4b17023SJohn Marino } 922*e4b17023SJohn Marino 923*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__ 924*e4b17023SJohn Marino /** 925*e4b17023SJohn Marino * @brief Inserts an object in %vector before specified iterator. 926*e4b17023SJohn Marino * @param __position An iterator into the %vector. 927*e4b17023SJohn Marino * @param __args Arguments. 928*e4b17023SJohn Marino * @return An iterator that points to the inserted data. 929*e4b17023SJohn Marino * 930*e4b17023SJohn Marino * This function will insert an object of type T constructed 931*e4b17023SJohn Marino * with T(std::forward<Args>(args)...) before the specified location. 932*e4b17023SJohn Marino * Note that this kind of operation could be expensive for a %vector 933*e4b17023SJohn Marino * and if it is frequently used the user should consider using 934*e4b17023SJohn Marino * std::list. 935*e4b17023SJohn Marino */ 936*e4b17023SJohn Marino template<typename... _Args> 937*e4b17023SJohn Marino iterator 938*e4b17023SJohn Marino emplace(iterator __position, _Args&&... __args); 939*e4b17023SJohn Marino #endif 940*e4b17023SJohn Marino 941*e4b17023SJohn Marino /** 942*e4b17023SJohn Marino * @brief Inserts given value into %vector before specified iterator. 943*e4b17023SJohn Marino * @param __position An iterator into the %vector. 944*e4b17023SJohn Marino * @param __x Data to be inserted. 945*e4b17023SJohn Marino * @return An iterator that points to the inserted data. 946*e4b17023SJohn Marino * 947*e4b17023SJohn Marino * This function will insert a copy of the given value before 948*e4b17023SJohn Marino * the specified location. Note that this kind of operation 949*e4b17023SJohn Marino * could be expensive for a %vector and if it is frequently 950*e4b17023SJohn Marino * used the user should consider using std::list. 951*e4b17023SJohn Marino */ 952*e4b17023SJohn Marino iterator 953*e4b17023SJohn Marino insert(iterator __position, const value_type& __x); 954*e4b17023SJohn Marino 955*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__ 956*e4b17023SJohn Marino /** 957*e4b17023SJohn Marino * @brief Inserts given rvalue into %vector before specified iterator. 958*e4b17023SJohn Marino * @param __position An iterator into the %vector. 959*e4b17023SJohn Marino * @param __x Data to be inserted. 960*e4b17023SJohn Marino * @return An iterator that points to the inserted data. 961*e4b17023SJohn Marino * 962*e4b17023SJohn Marino * This function will insert a copy of the given rvalue before 963*e4b17023SJohn Marino * the specified location. Note that this kind of operation 964*e4b17023SJohn Marino * could be expensive for a %vector and if it is frequently 965*e4b17023SJohn Marino * used the user should consider using std::list. 966*e4b17023SJohn Marino */ 967*e4b17023SJohn Marino iterator 968*e4b17023SJohn Marino insert(iterator __position, value_type&& __x) 969*e4b17023SJohn Marino { return emplace(__position, std::move(__x)); } 970*e4b17023SJohn Marino 971*e4b17023SJohn Marino /** 972*e4b17023SJohn Marino * @brief Inserts an initializer_list into the %vector. 973*e4b17023SJohn Marino * @param __position An iterator into the %vector. 974*e4b17023SJohn Marino * @param __l An initializer_list. 975*e4b17023SJohn Marino * 976*e4b17023SJohn Marino * This function will insert copies of the data in the 977*e4b17023SJohn Marino * initializer_list @a l into the %vector before the location 978*e4b17023SJohn Marino * specified by @a position. 979*e4b17023SJohn Marino * 980*e4b17023SJohn Marino * Note that this kind of operation could be expensive for a 981*e4b17023SJohn Marino * %vector and if it is frequently used the user should 982*e4b17023SJohn Marino * consider using std::list. 983*e4b17023SJohn Marino */ 984*e4b17023SJohn Marino void 985*e4b17023SJohn Marino insert(iterator __position, initializer_list<value_type> __l) 986*e4b17023SJohn Marino { this->insert(__position, __l.begin(), __l.end()); } 987*e4b17023SJohn Marino #endif 988*e4b17023SJohn Marino 989*e4b17023SJohn Marino /** 990*e4b17023SJohn Marino * @brief Inserts a number of copies of given data into the %vector. 991*e4b17023SJohn Marino * @param __position An iterator into the %vector. 992*e4b17023SJohn Marino * @param __n Number of elements to be inserted. 993*e4b17023SJohn Marino * @param __x Data to be inserted. 994*e4b17023SJohn Marino * 995*e4b17023SJohn Marino * This function will insert a specified number of copies of 996*e4b17023SJohn Marino * the given data before the location specified by @a position. 997*e4b17023SJohn Marino * 998*e4b17023SJohn Marino * Note that this kind of operation could be expensive for a 999*e4b17023SJohn Marino * %vector and if it is frequently used the user should 1000*e4b17023SJohn Marino * consider using std::list. 1001*e4b17023SJohn Marino */ 1002*e4b17023SJohn Marino void 1003*e4b17023SJohn Marino insert(iterator __position, size_type __n, const value_type& __x) 1004*e4b17023SJohn Marino { _M_fill_insert(__position, __n, __x); } 1005*e4b17023SJohn Marino 1006*e4b17023SJohn Marino /** 1007*e4b17023SJohn Marino * @brief Inserts a range into the %vector. 1008*e4b17023SJohn Marino * @param __position An iterator into the %vector. 1009*e4b17023SJohn Marino * @param __first An input iterator. 1010*e4b17023SJohn Marino * @param __last An input iterator. 1011*e4b17023SJohn Marino * 1012*e4b17023SJohn Marino * This function will insert copies of the data in the range 1013*e4b17023SJohn Marino * [__first,__last) into the %vector before the location specified 1014*e4b17023SJohn Marino * by @a pos. 1015*e4b17023SJohn Marino * 1016*e4b17023SJohn Marino * Note that this kind of operation could be expensive for a 1017*e4b17023SJohn Marino * %vector and if it is frequently used the user should 1018*e4b17023SJohn Marino * consider using std::list. 1019*e4b17023SJohn Marino */ 1020*e4b17023SJohn Marino template<typename _InputIterator> 1021*e4b17023SJohn Marino void 1022*e4b17023SJohn Marino insert(iterator __position, _InputIterator __first, 1023*e4b17023SJohn Marino _InputIterator __last) 1024*e4b17023SJohn Marino { 1025*e4b17023SJohn Marino // Check whether it's an integral type. If so, it's not an iterator. 1026*e4b17023SJohn Marino typedef typename std::__is_integer<_InputIterator>::__type _Integral; 1027*e4b17023SJohn Marino _M_insert_dispatch(__position, __first, __last, _Integral()); 1028*e4b17023SJohn Marino } 1029*e4b17023SJohn Marino 1030*e4b17023SJohn Marino /** 1031*e4b17023SJohn Marino * @brief Remove element at given position. 1032*e4b17023SJohn Marino * @param __position Iterator pointing to element to be erased. 1033*e4b17023SJohn Marino * @return An iterator pointing to the next element (or end()). 1034*e4b17023SJohn Marino * 1035*e4b17023SJohn Marino * This function will erase the element at the given position and thus 1036*e4b17023SJohn Marino * shorten the %vector by one. 1037*e4b17023SJohn Marino * 1038*e4b17023SJohn Marino * Note This operation could be expensive and if it is 1039*e4b17023SJohn Marino * frequently used the user should consider using std::list. 1040*e4b17023SJohn Marino * The user is also cautioned that this function only erases 1041*e4b17023SJohn Marino * the element, and that if the element is itself a pointer, 1042*e4b17023SJohn Marino * the pointed-to memory is not touched in any way. Managing 1043*e4b17023SJohn Marino * the pointer is the user's responsibility. 1044*e4b17023SJohn Marino */ 1045*e4b17023SJohn Marino iterator 1046*e4b17023SJohn Marino erase(iterator __position); 1047*e4b17023SJohn Marino 1048*e4b17023SJohn Marino /** 1049*e4b17023SJohn Marino * @brief Remove a range of elements. 1050*e4b17023SJohn Marino * @param __first Iterator pointing to the first element to be erased. 1051*e4b17023SJohn Marino * @param __last Iterator pointing to one past the last element to be 1052*e4b17023SJohn Marino * erased. 1053*e4b17023SJohn Marino * @return An iterator pointing to the element pointed to by @a __last 1054*e4b17023SJohn Marino * prior to erasing (or end()). 1055*e4b17023SJohn Marino * 1056*e4b17023SJohn Marino * This function will erase the elements in the range 1057*e4b17023SJohn Marino * [__first,__last) and shorten the %vector accordingly. 1058*e4b17023SJohn Marino * 1059*e4b17023SJohn Marino * Note This operation could be expensive and if it is 1060*e4b17023SJohn Marino * frequently used the user should consider using std::list. 1061*e4b17023SJohn Marino * The user is also cautioned that this function only erases 1062*e4b17023SJohn Marino * the elements, and that if the elements themselves are 1063*e4b17023SJohn Marino * pointers, the pointed-to memory is not touched in any way. 1064*e4b17023SJohn Marino * Managing the pointer is the user's responsibility. 1065*e4b17023SJohn Marino */ 1066*e4b17023SJohn Marino iterator 1067*e4b17023SJohn Marino erase(iterator __first, iterator __last); 1068*e4b17023SJohn Marino 1069*e4b17023SJohn Marino /** 1070*e4b17023SJohn Marino * @brief Swaps data with another %vector. 1071*e4b17023SJohn Marino * @param __x A %vector of the same element and allocator types. 1072*e4b17023SJohn Marino * 1073*e4b17023SJohn Marino * This exchanges the elements between two vectors in constant time. 1074*e4b17023SJohn Marino * (Three pointers, so it should be quite fast.) 1075*e4b17023SJohn Marino * Note that the global std::swap() function is specialized such that 1076*e4b17023SJohn Marino * std::swap(v1,v2) will feed to this function. 1077*e4b17023SJohn Marino */ 1078*e4b17023SJohn Marino void 1079*e4b17023SJohn Marino swap(vector& __x) 1080*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__ 1081*e4b17023SJohn Marino noexcept(_Alloc_traits::_S_nothrow_swap()) 1082*e4b17023SJohn Marino #endif 1083*e4b17023SJohn Marino { 1084*e4b17023SJohn Marino this->_M_impl._M_swap_data(__x._M_impl); 1085*e4b17023SJohn Marino _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(), 1086*e4b17023SJohn Marino __x._M_get_Tp_allocator()); 1087*e4b17023SJohn Marino } 1088*e4b17023SJohn Marino 1089*e4b17023SJohn Marino /** 1090*e4b17023SJohn Marino * Erases all the elements. Note that this function only erases the 1091*e4b17023SJohn Marino * elements, and that if the elements themselves are pointers, the 1092*e4b17023SJohn Marino * pointed-to memory is not touched in any way. Managing the pointer is 1093*e4b17023SJohn Marino * the user's responsibility. 1094*e4b17023SJohn Marino */ 1095*e4b17023SJohn Marino void 1096*e4b17023SJohn Marino clear() _GLIBCXX_NOEXCEPT 1097*e4b17023SJohn Marino { _M_erase_at_end(this->_M_impl._M_start); } 1098*e4b17023SJohn Marino 1099*e4b17023SJohn Marino protected: 1100*e4b17023SJohn Marino /** 1101*e4b17023SJohn Marino * Memory expansion handler. Uses the member allocation function to 1102*e4b17023SJohn Marino * obtain @a n bytes of memory, and then copies [first,last) into it. 1103*e4b17023SJohn Marino */ 1104*e4b17023SJohn Marino template<typename _ForwardIterator> 1105*e4b17023SJohn Marino pointer 1106*e4b17023SJohn Marino _M_allocate_and_copy(size_type __n, 1107*e4b17023SJohn Marino _ForwardIterator __first, _ForwardIterator __last) 1108*e4b17023SJohn Marino { 1109*e4b17023SJohn Marino pointer __result = this->_M_allocate(__n); 1110*e4b17023SJohn Marino __try 1111*e4b17023SJohn Marino { 1112*e4b17023SJohn Marino std::__uninitialized_copy_a(__first, __last, __result, 1113*e4b17023SJohn Marino _M_get_Tp_allocator()); 1114*e4b17023SJohn Marino return __result; 1115*e4b17023SJohn Marino } 1116*e4b17023SJohn Marino __catch(...) 1117*e4b17023SJohn Marino { 1118*e4b17023SJohn Marino _M_deallocate(__result, __n); 1119*e4b17023SJohn Marino __throw_exception_again; 1120*e4b17023SJohn Marino } 1121*e4b17023SJohn Marino } 1122*e4b17023SJohn Marino 1123*e4b17023SJohn Marino 1124*e4b17023SJohn Marino // Internal constructor functions follow. 1125*e4b17023SJohn Marino 1126*e4b17023SJohn Marino // Called by the range constructor to implement [23.1.1]/9 1127*e4b17023SJohn Marino 1128*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 1129*e4b17023SJohn Marino // 438. Ambiguity in the "do the right thing" clause 1130*e4b17023SJohn Marino template<typename _Integer> 1131*e4b17023SJohn Marino void 1132*e4b17023SJohn Marino _M_initialize_dispatch(_Integer __n, _Integer __value, __true_type) 1133*e4b17023SJohn Marino { 1134*e4b17023SJohn Marino this->_M_impl._M_start = _M_allocate(static_cast<size_type>(__n)); 1135*e4b17023SJohn Marino this->_M_impl._M_end_of_storage = 1136*e4b17023SJohn Marino this->_M_impl._M_start + static_cast<size_type>(__n); 1137*e4b17023SJohn Marino _M_fill_initialize(static_cast<size_type>(__n), __value); 1138*e4b17023SJohn Marino } 1139*e4b17023SJohn Marino 1140*e4b17023SJohn Marino // Called by the range constructor to implement [23.1.1]/9 1141*e4b17023SJohn Marino template<typename _InputIterator> 1142*e4b17023SJohn Marino void 1143*e4b17023SJohn Marino _M_initialize_dispatch(_InputIterator __first, _InputIterator __last, 1144*e4b17023SJohn Marino __false_type) 1145*e4b17023SJohn Marino { 1146*e4b17023SJohn Marino typedef typename std::iterator_traits<_InputIterator>:: 1147*e4b17023SJohn Marino iterator_category _IterCategory; 1148*e4b17023SJohn Marino _M_range_initialize(__first, __last, _IterCategory()); 1149*e4b17023SJohn Marino } 1150*e4b17023SJohn Marino 1151*e4b17023SJohn Marino // Called by the second initialize_dispatch above 1152*e4b17023SJohn Marino template<typename _InputIterator> 1153*e4b17023SJohn Marino void 1154*e4b17023SJohn Marino _M_range_initialize(_InputIterator __first, 1155*e4b17023SJohn Marino _InputIterator __last, std::input_iterator_tag) 1156*e4b17023SJohn Marino { 1157*e4b17023SJohn Marino for (; __first != __last; ++__first) 1158*e4b17023SJohn Marino push_back(*__first); 1159*e4b17023SJohn Marino } 1160*e4b17023SJohn Marino 1161*e4b17023SJohn Marino // Called by the second initialize_dispatch above 1162*e4b17023SJohn Marino template<typename _ForwardIterator> 1163*e4b17023SJohn Marino void 1164*e4b17023SJohn Marino _M_range_initialize(_ForwardIterator __first, 1165*e4b17023SJohn Marino _ForwardIterator __last, std::forward_iterator_tag) 1166*e4b17023SJohn Marino { 1167*e4b17023SJohn Marino const size_type __n = std::distance(__first, __last); 1168*e4b17023SJohn Marino this->_M_impl._M_start = this->_M_allocate(__n); 1169*e4b17023SJohn Marino this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n; 1170*e4b17023SJohn Marino this->_M_impl._M_finish = 1171*e4b17023SJohn Marino std::__uninitialized_copy_a(__first, __last, 1172*e4b17023SJohn Marino this->_M_impl._M_start, 1173*e4b17023SJohn Marino _M_get_Tp_allocator()); 1174*e4b17023SJohn Marino } 1175*e4b17023SJohn Marino 1176*e4b17023SJohn Marino // Called by the first initialize_dispatch above and by the 1177*e4b17023SJohn Marino // vector(n,value,a) constructor. 1178*e4b17023SJohn Marino void 1179*e4b17023SJohn Marino _M_fill_initialize(size_type __n, const value_type& __value) 1180*e4b17023SJohn Marino { 1181*e4b17023SJohn Marino std::__uninitialized_fill_n_a(this->_M_impl._M_start, __n, __value, 1182*e4b17023SJohn Marino _M_get_Tp_allocator()); 1183*e4b17023SJohn Marino this->_M_impl._M_finish = this->_M_impl._M_end_of_storage; 1184*e4b17023SJohn Marino } 1185*e4b17023SJohn Marino 1186*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__ 1187*e4b17023SJohn Marino // Called by the vector(n) constructor. 1188*e4b17023SJohn Marino void 1189*e4b17023SJohn Marino _M_default_initialize(size_type __n) 1190*e4b17023SJohn Marino { 1191*e4b17023SJohn Marino std::__uninitialized_default_n_a(this->_M_impl._M_start, __n, 1192*e4b17023SJohn Marino _M_get_Tp_allocator()); 1193*e4b17023SJohn Marino this->_M_impl._M_finish = this->_M_impl._M_end_of_storage; 1194*e4b17023SJohn Marino } 1195*e4b17023SJohn Marino #endif 1196*e4b17023SJohn Marino 1197*e4b17023SJohn Marino // Internal assign functions follow. The *_aux functions do the actual 1198*e4b17023SJohn Marino // assignment work for the range versions. 1199*e4b17023SJohn Marino 1200*e4b17023SJohn Marino // Called by the range assign to implement [23.1.1]/9 1201*e4b17023SJohn Marino 1202*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 1203*e4b17023SJohn Marino // 438. Ambiguity in the "do the right thing" clause 1204*e4b17023SJohn Marino template<typename _Integer> 1205*e4b17023SJohn Marino void 1206*e4b17023SJohn Marino _M_assign_dispatch(_Integer __n, _Integer __val, __true_type) 1207*e4b17023SJohn Marino { _M_fill_assign(__n, __val); } 1208*e4b17023SJohn Marino 1209*e4b17023SJohn Marino // Called by the range assign to implement [23.1.1]/9 1210*e4b17023SJohn Marino template<typename _InputIterator> 1211*e4b17023SJohn Marino void 1212*e4b17023SJohn Marino _M_assign_dispatch(_InputIterator __first, _InputIterator __last, 1213*e4b17023SJohn Marino __false_type) 1214*e4b17023SJohn Marino { 1215*e4b17023SJohn Marino typedef typename std::iterator_traits<_InputIterator>:: 1216*e4b17023SJohn Marino iterator_category _IterCategory; 1217*e4b17023SJohn Marino _M_assign_aux(__first, __last, _IterCategory()); 1218*e4b17023SJohn Marino } 1219*e4b17023SJohn Marino 1220*e4b17023SJohn Marino // Called by the second assign_dispatch above 1221*e4b17023SJohn Marino template<typename _InputIterator> 1222*e4b17023SJohn Marino void 1223*e4b17023SJohn Marino _M_assign_aux(_InputIterator __first, _InputIterator __last, 1224*e4b17023SJohn Marino std::input_iterator_tag); 1225*e4b17023SJohn Marino 1226*e4b17023SJohn Marino // Called by the second assign_dispatch above 1227*e4b17023SJohn Marino template<typename _ForwardIterator> 1228*e4b17023SJohn Marino void 1229*e4b17023SJohn Marino _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last, 1230*e4b17023SJohn Marino std::forward_iterator_tag); 1231*e4b17023SJohn Marino 1232*e4b17023SJohn Marino // Called by assign(n,t), and the range assign when it turns out 1233*e4b17023SJohn Marino // to be the same thing. 1234*e4b17023SJohn Marino void 1235*e4b17023SJohn Marino _M_fill_assign(size_type __n, const value_type& __val); 1236*e4b17023SJohn Marino 1237*e4b17023SJohn Marino 1238*e4b17023SJohn Marino // Internal insert functions follow. 1239*e4b17023SJohn Marino 1240*e4b17023SJohn Marino // Called by the range insert to implement [23.1.1]/9 1241*e4b17023SJohn Marino 1242*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 1243*e4b17023SJohn Marino // 438. Ambiguity in the "do the right thing" clause 1244*e4b17023SJohn Marino template<typename _Integer> 1245*e4b17023SJohn Marino void 1246*e4b17023SJohn Marino _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val, 1247*e4b17023SJohn Marino __true_type) 1248*e4b17023SJohn Marino { _M_fill_insert(__pos, __n, __val); } 1249*e4b17023SJohn Marino 1250*e4b17023SJohn Marino // Called by the range insert to implement [23.1.1]/9 1251*e4b17023SJohn Marino template<typename _InputIterator> 1252*e4b17023SJohn Marino void 1253*e4b17023SJohn Marino _M_insert_dispatch(iterator __pos, _InputIterator __first, 1254*e4b17023SJohn Marino _InputIterator __last, __false_type) 1255*e4b17023SJohn Marino { 1256*e4b17023SJohn Marino typedef typename std::iterator_traits<_InputIterator>:: 1257*e4b17023SJohn Marino iterator_category _IterCategory; 1258*e4b17023SJohn Marino _M_range_insert(__pos, __first, __last, _IterCategory()); 1259*e4b17023SJohn Marino } 1260*e4b17023SJohn Marino 1261*e4b17023SJohn Marino // Called by the second insert_dispatch above 1262*e4b17023SJohn Marino template<typename _InputIterator> 1263*e4b17023SJohn Marino void 1264*e4b17023SJohn Marino _M_range_insert(iterator __pos, _InputIterator __first, 1265*e4b17023SJohn Marino _InputIterator __last, std::input_iterator_tag); 1266*e4b17023SJohn Marino 1267*e4b17023SJohn Marino // Called by the second insert_dispatch above 1268*e4b17023SJohn Marino template<typename _ForwardIterator> 1269*e4b17023SJohn Marino void 1270*e4b17023SJohn Marino _M_range_insert(iterator __pos, _ForwardIterator __first, 1271*e4b17023SJohn Marino _ForwardIterator __last, std::forward_iterator_tag); 1272*e4b17023SJohn Marino 1273*e4b17023SJohn Marino // Called by insert(p,n,x), and the range insert when it turns out to be 1274*e4b17023SJohn Marino // the same thing. 1275*e4b17023SJohn Marino void 1276*e4b17023SJohn Marino _M_fill_insert(iterator __pos, size_type __n, const value_type& __x); 1277*e4b17023SJohn Marino 1278*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__ 1279*e4b17023SJohn Marino // Called by resize(n). 1280*e4b17023SJohn Marino void 1281*e4b17023SJohn Marino _M_default_append(size_type __n); 1282*e4b17023SJohn Marino 1283*e4b17023SJohn Marino bool 1284*e4b17023SJohn Marino _M_shrink_to_fit(); 1285*e4b17023SJohn Marino #endif 1286*e4b17023SJohn Marino 1287*e4b17023SJohn Marino // Called by insert(p,x) 1288*e4b17023SJohn Marino #ifndef __GXX_EXPERIMENTAL_CXX0X__ 1289*e4b17023SJohn Marino void 1290*e4b17023SJohn Marino _M_insert_aux(iterator __position, const value_type& __x); 1291*e4b17023SJohn Marino #else 1292*e4b17023SJohn Marino template<typename... _Args> 1293*e4b17023SJohn Marino void 1294*e4b17023SJohn Marino _M_insert_aux(iterator __position, _Args&&... __args); 1295*e4b17023SJohn Marino 1296*e4b17023SJohn Marino template<typename... _Args> 1297*e4b17023SJohn Marino void 1298*e4b17023SJohn Marino _M_emplace_back_aux(_Args&&... __args); 1299*e4b17023SJohn Marino #endif 1300*e4b17023SJohn Marino 1301*e4b17023SJohn Marino // Called by the latter. 1302*e4b17023SJohn Marino size_type 1303*e4b17023SJohn Marino _M_check_len(size_type __n, const char* __s) const 1304*e4b17023SJohn Marino { 1305*e4b17023SJohn Marino if (max_size() - size() < __n) 1306*e4b17023SJohn Marino __throw_length_error(__N(__s)); 1307*e4b17023SJohn Marino 1308*e4b17023SJohn Marino const size_type __len = size() + std::max(size(), __n); 1309*e4b17023SJohn Marino return (__len < size() || __len > max_size()) ? max_size() : __len; 1310*e4b17023SJohn Marino } 1311*e4b17023SJohn Marino 1312*e4b17023SJohn Marino // Internal erase functions follow. 1313*e4b17023SJohn Marino 1314*e4b17023SJohn Marino // Called by erase(q1,q2), clear(), resize(), _M_fill_assign, 1315*e4b17023SJohn Marino // _M_assign_aux. 1316*e4b17023SJohn Marino void 1317*e4b17023SJohn Marino _M_erase_at_end(pointer __pos) 1318*e4b17023SJohn Marino { 1319*e4b17023SJohn Marino std::_Destroy(__pos, this->_M_impl._M_finish, _M_get_Tp_allocator()); 1320*e4b17023SJohn Marino this->_M_impl._M_finish = __pos; 1321*e4b17023SJohn Marino } 1322*e4b17023SJohn Marino 1323*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__ 1324*e4b17023SJohn Marino private: 1325*e4b17023SJohn Marino // Constant-time move assignment when source object's memory can be 1326*e4b17023SJohn Marino // moved, either because the source's allocator will move too 1327*e4b17023SJohn Marino // or because the allocators are equal. 1328*e4b17023SJohn Marino void 1329*e4b17023SJohn Marino _M_move_assign(vector&& __x, std::true_type) noexcept 1330*e4b17023SJohn Marino { 1331*e4b17023SJohn Marino const vector __tmp(std::move(*this)); 1332*e4b17023SJohn Marino this->_M_impl._M_swap_data(__x._M_impl); 1333*e4b17023SJohn Marino if (_Alloc_traits::_S_propagate_on_move_assign()) 1334*e4b17023SJohn Marino std::__alloc_on_move(_M_get_Tp_allocator(), 1335*e4b17023SJohn Marino __x._M_get_Tp_allocator()); 1336*e4b17023SJohn Marino } 1337*e4b17023SJohn Marino 1338*e4b17023SJohn Marino // Do move assignment when it might not be possible to move source 1339*e4b17023SJohn Marino // object's memory, resulting in a linear-time operation. 1340*e4b17023SJohn Marino void 1341*e4b17023SJohn Marino _M_move_assign(vector&& __x, std::false_type) 1342*e4b17023SJohn Marino { 1343*e4b17023SJohn Marino if (__x._M_get_Tp_allocator() == this->_M_get_Tp_allocator()) 1344*e4b17023SJohn Marino _M_move_assign(std::move(__x), std::true_type()); 1345*e4b17023SJohn Marino else 1346*e4b17023SJohn Marino { 1347*e4b17023SJohn Marino // The rvalue's allocator cannot be moved and is not equal, 1348*e4b17023SJohn Marino // so we need to individually move each element. 1349*e4b17023SJohn Marino this->assign(std::__make_move_if_noexcept_iterator(__x.begin()), 1350*e4b17023SJohn Marino std::__make_move_if_noexcept_iterator(__x.end())); 1351*e4b17023SJohn Marino __x.clear(); 1352*e4b17023SJohn Marino } 1353*e4b17023SJohn Marino } 1354*e4b17023SJohn Marino #endif 1355*e4b17023SJohn Marino }; 1356*e4b17023SJohn Marino 1357*e4b17023SJohn Marino 1358*e4b17023SJohn Marino /** 1359*e4b17023SJohn Marino * @brief Vector equality comparison. 1360*e4b17023SJohn Marino * @param __x A %vector. 1361*e4b17023SJohn Marino * @param __y A %vector of the same type as @a __x. 1362*e4b17023SJohn Marino * @return True iff the size and elements of the vectors are equal. 1363*e4b17023SJohn Marino * 1364*e4b17023SJohn Marino * This is an equivalence relation. It is linear in the size of the 1365*e4b17023SJohn Marino * vectors. Vectors are considered equivalent if their sizes are equal, 1366*e4b17023SJohn Marino * and if corresponding elements compare equal. 1367*e4b17023SJohn Marino */ 1368*e4b17023SJohn Marino template<typename _Tp, typename _Alloc> 1369*e4b17023SJohn Marino inline bool 1370*e4b17023SJohn Marino operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) 1371*e4b17023SJohn Marino { return (__x.size() == __y.size() 1372*e4b17023SJohn Marino && std::equal(__x.begin(), __x.end(), __y.begin())); } 1373*e4b17023SJohn Marino 1374*e4b17023SJohn Marino /** 1375*e4b17023SJohn Marino * @brief Vector ordering relation. 1376*e4b17023SJohn Marino * @param __x A %vector. 1377*e4b17023SJohn Marino * @param __y A %vector of the same type as @a __x. 1378*e4b17023SJohn Marino * @return True iff @a __x is lexicographically less than @a __y. 1379*e4b17023SJohn Marino * 1380*e4b17023SJohn Marino * This is a total ordering relation. It is linear in the size of the 1381*e4b17023SJohn Marino * vectors. The elements must be comparable with @c <. 1382*e4b17023SJohn Marino * 1383*e4b17023SJohn Marino * See std::lexicographical_compare() for how the determination is made. 1384*e4b17023SJohn Marino */ 1385*e4b17023SJohn Marino template<typename _Tp, typename _Alloc> 1386*e4b17023SJohn Marino inline bool 1387*e4b17023SJohn Marino operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) 1388*e4b17023SJohn Marino { return std::lexicographical_compare(__x.begin(), __x.end(), 1389*e4b17023SJohn Marino __y.begin(), __y.end()); } 1390*e4b17023SJohn Marino 1391*e4b17023SJohn Marino /// Based on operator== 1392*e4b17023SJohn Marino template<typename _Tp, typename _Alloc> 1393*e4b17023SJohn Marino inline bool 1394*e4b17023SJohn Marino operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) 1395*e4b17023SJohn Marino { return !(__x == __y); } 1396*e4b17023SJohn Marino 1397*e4b17023SJohn Marino /// Based on operator< 1398*e4b17023SJohn Marino template<typename _Tp, typename _Alloc> 1399*e4b17023SJohn Marino inline bool 1400*e4b17023SJohn Marino operator>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) 1401*e4b17023SJohn Marino { return __y < __x; } 1402*e4b17023SJohn Marino 1403*e4b17023SJohn Marino /// Based on operator< 1404*e4b17023SJohn Marino template<typename _Tp, typename _Alloc> 1405*e4b17023SJohn Marino inline bool 1406*e4b17023SJohn Marino operator<=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) 1407*e4b17023SJohn Marino { return !(__y < __x); } 1408*e4b17023SJohn Marino 1409*e4b17023SJohn Marino /// Based on operator< 1410*e4b17023SJohn Marino template<typename _Tp, typename _Alloc> 1411*e4b17023SJohn Marino inline bool 1412*e4b17023SJohn Marino operator>=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) 1413*e4b17023SJohn Marino { return !(__x < __y); } 1414*e4b17023SJohn Marino 1415*e4b17023SJohn Marino /// See std::vector::swap(). 1416*e4b17023SJohn Marino template<typename _Tp, typename _Alloc> 1417*e4b17023SJohn Marino inline void 1418*e4b17023SJohn Marino swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>& __y) 1419*e4b17023SJohn Marino { __x.swap(__y); } 1420*e4b17023SJohn Marino 1421*e4b17023SJohn Marino _GLIBCXX_END_NAMESPACE_CONTAINER 1422*e4b17023SJohn Marino } // namespace std 1423*e4b17023SJohn Marino 1424*e4b17023SJohn Marino #endif /* _STL_VECTOR_H */ 1425