1*38fd1498Szrj// Debugging vector implementation -*- C++ -*- 2*38fd1498Szrj 3*38fd1498Szrj// Copyright (C) 2003-2018 Free Software Foundation, Inc. 4*38fd1498Szrj// 5*38fd1498Szrj// This file is part of the GNU ISO C++ Library. This library is free 6*38fd1498Szrj// software; you can redistribute it and/or modify it under the 7*38fd1498Szrj// terms of the GNU General Public License as published by the 8*38fd1498Szrj// Free Software Foundation; either version 3, or (at your option) 9*38fd1498Szrj// any later version. 10*38fd1498Szrj 11*38fd1498Szrj// This library is distributed in the hope that it will be useful, 12*38fd1498Szrj// but WITHOUT ANY WARRANTY; without even the implied warranty of 13*38fd1498Szrj// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*38fd1498Szrj// GNU General Public License for more details. 15*38fd1498Szrj 16*38fd1498Szrj// Under Section 7 of GPL version 3, you are granted additional 17*38fd1498Szrj// permissions described in the GCC Runtime Library Exception, version 18*38fd1498Szrj// 3.1, as published by the Free Software Foundation. 19*38fd1498Szrj 20*38fd1498Szrj// You should have received a copy of the GNU General Public License and 21*38fd1498Szrj// a copy of the GCC Runtime Library Exception along with this program; 22*38fd1498Szrj// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23*38fd1498Szrj// <http://www.gnu.org/licenses/>. 24*38fd1498Szrj 25*38fd1498Szrj/** @file debug/vector 26*38fd1498Szrj * This file is a GNU debug extension to the Standard C++ Library. 27*38fd1498Szrj */ 28*38fd1498Szrj 29*38fd1498Szrj#ifndef _GLIBCXX_DEBUG_VECTOR 30*38fd1498Szrj#define _GLIBCXX_DEBUG_VECTOR 1 31*38fd1498Szrj 32*38fd1498Szrj#pragma GCC system_header 33*38fd1498Szrj 34*38fd1498Szrj#include <vector> 35*38fd1498Szrj#include <utility> 36*38fd1498Szrj#include <debug/safe_sequence.h> 37*38fd1498Szrj#include <debug/safe_container.h> 38*38fd1498Szrj#include <debug/safe_iterator.h> 39*38fd1498Szrj 40*38fd1498Szrjnamespace __gnu_debug 41*38fd1498Szrj{ 42*38fd1498Szrj /** @brief Base class for Debug Mode vector. 43*38fd1498Szrj * 44*38fd1498Szrj * Adds information about the guaranteed capacity, which is useful for 45*38fd1498Szrj * detecting code which relies on non-portable implementation details of 46*38fd1498Szrj * the libstdc++ reallocation policy. 47*38fd1498Szrj */ 48*38fd1498Szrj template<typename _SafeSequence, 49*38fd1498Szrj typename _BaseSequence> 50*38fd1498Szrj class _Safe_vector 51*38fd1498Szrj { 52*38fd1498Szrj typedef typename _BaseSequence::size_type size_type; 53*38fd1498Szrj 54*38fd1498Szrj const _SafeSequence& 55*38fd1498Szrj _M_seq() const { return *static_cast<const _SafeSequence*>(this); } 56*38fd1498Szrj 57*38fd1498Szrj protected: 58*38fd1498Szrj _Safe_vector() _GLIBCXX_NOEXCEPT 59*38fd1498Szrj : _M_guaranteed_capacity(0) 60*38fd1498Szrj { _M_update_guaranteed_capacity(); } 61*38fd1498Szrj 62*38fd1498Szrj _Safe_vector(const _Safe_vector&) _GLIBCXX_NOEXCEPT 63*38fd1498Szrj : _M_guaranteed_capacity(0) 64*38fd1498Szrj { _M_update_guaranteed_capacity(); } 65*38fd1498Szrj 66*38fd1498Szrj _Safe_vector(size_type __n) _GLIBCXX_NOEXCEPT 67*38fd1498Szrj : _M_guaranteed_capacity(__n) 68*38fd1498Szrj { } 69*38fd1498Szrj 70*38fd1498Szrj#if __cplusplus >= 201103L 71*38fd1498Szrj _Safe_vector(_Safe_vector&& __x) noexcept 72*38fd1498Szrj : _Safe_vector() 73*38fd1498Szrj { __x._M_guaranteed_capacity = 0; } 74*38fd1498Szrj 75*38fd1498Szrj _Safe_vector& 76*38fd1498Szrj operator=(const _Safe_vector&) noexcept 77*38fd1498Szrj { 78*38fd1498Szrj _M_update_guaranteed_capacity(); 79*38fd1498Szrj return *this; 80*38fd1498Szrj } 81*38fd1498Szrj 82*38fd1498Szrj _Safe_vector& 83*38fd1498Szrj operator=(_Safe_vector&& __x) noexcept 84*38fd1498Szrj { 85*38fd1498Szrj _M_update_guaranteed_capacity(); 86*38fd1498Szrj __x._M_guaranteed_capacity = 0; 87*38fd1498Szrj return *this; 88*38fd1498Szrj } 89*38fd1498Szrj#endif 90*38fd1498Szrj 91*38fd1498Szrj size_type _M_guaranteed_capacity; 92*38fd1498Szrj 93*38fd1498Szrj bool 94*38fd1498Szrj _M_requires_reallocation(size_type __elements) const _GLIBCXX_NOEXCEPT 95*38fd1498Szrj { return __elements > _M_seq().capacity(); } 96*38fd1498Szrj 97*38fd1498Szrj void 98*38fd1498Szrj _M_update_guaranteed_capacity() _GLIBCXX_NOEXCEPT 99*38fd1498Szrj { 100*38fd1498Szrj if (_M_seq().size() > _M_guaranteed_capacity) 101*38fd1498Szrj _M_guaranteed_capacity = _M_seq().size(); 102*38fd1498Szrj } 103*38fd1498Szrj }; 104*38fd1498Szrj} 105*38fd1498Szrj 106*38fd1498Szrjnamespace std _GLIBCXX_VISIBILITY(default) 107*38fd1498Szrj{ 108*38fd1498Szrjnamespace __debug 109*38fd1498Szrj{ 110*38fd1498Szrj /// Class std::vector with safety/checking/debug instrumentation. 111*38fd1498Szrj template<typename _Tp, 112*38fd1498Szrj typename _Allocator = std::allocator<_Tp> > 113*38fd1498Szrj class vector 114*38fd1498Szrj : public __gnu_debug::_Safe_container< 115*38fd1498Szrj vector<_Tp, _Allocator>, _Allocator, __gnu_debug::_Safe_sequence>, 116*38fd1498Szrj public _GLIBCXX_STD_C::vector<_Tp, _Allocator>, 117*38fd1498Szrj public __gnu_debug::_Safe_vector< 118*38fd1498Szrj vector<_Tp, _Allocator>, 119*38fd1498Szrj _GLIBCXX_STD_C::vector<_Tp, _Allocator> > 120*38fd1498Szrj { 121*38fd1498Szrj typedef _GLIBCXX_STD_C::vector<_Tp, _Allocator> _Base; 122*38fd1498Szrj typedef __gnu_debug::_Safe_container< 123*38fd1498Szrj vector, _Allocator, __gnu_debug::_Safe_sequence> _Safe; 124*38fd1498Szrj typedef __gnu_debug::_Safe_vector<vector, _Base> _Safe_vector; 125*38fd1498Szrj 126*38fd1498Szrj typedef typename _Base::iterator _Base_iterator; 127*38fd1498Szrj typedef typename _Base::const_iterator _Base_const_iterator; 128*38fd1498Szrj typedef __gnu_debug::_Equal_to<_Base_const_iterator> _Equal; 129*38fd1498Szrj 130*38fd1498Szrj public: 131*38fd1498Szrj typedef typename _Base::reference reference; 132*38fd1498Szrj typedef typename _Base::const_reference const_reference; 133*38fd1498Szrj 134*38fd1498Szrj typedef __gnu_debug::_Safe_iterator< 135*38fd1498Szrj _Base_iterator, vector> iterator; 136*38fd1498Szrj typedef __gnu_debug::_Safe_iterator< 137*38fd1498Szrj _Base_const_iterator, vector> const_iterator; 138*38fd1498Szrj 139*38fd1498Szrj typedef typename _Base::size_type size_type; 140*38fd1498Szrj typedef typename _Base::difference_type difference_type; 141*38fd1498Szrj 142*38fd1498Szrj typedef _Tp value_type; 143*38fd1498Szrj typedef _Allocator allocator_type; 144*38fd1498Szrj typedef typename _Base::pointer pointer; 145*38fd1498Szrj typedef typename _Base::const_pointer const_pointer; 146*38fd1498Szrj typedef std::reverse_iterator<iterator> reverse_iterator; 147*38fd1498Szrj typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 148*38fd1498Szrj 149*38fd1498Szrj // 23.2.4.1 construct/copy/destroy: 150*38fd1498Szrj 151*38fd1498Szrj#if __cplusplus < 201103L 152*38fd1498Szrj vector() _GLIBCXX_NOEXCEPT 153*38fd1498Szrj : _Base() { } 154*38fd1498Szrj#else 155*38fd1498Szrj vector() = default; 156*38fd1498Szrj#endif 157*38fd1498Szrj 158*38fd1498Szrj explicit 159*38fd1498Szrj vector(const _Allocator& __a) _GLIBCXX_NOEXCEPT 160*38fd1498Szrj : _Base(__a) { } 161*38fd1498Szrj 162*38fd1498Szrj#if __cplusplus >= 201103L 163*38fd1498Szrj explicit 164*38fd1498Szrj vector(size_type __n, const _Allocator& __a = _Allocator()) 165*38fd1498Szrj : _Base(__n, __a), _Safe_vector(__n) { } 166*38fd1498Szrj 167*38fd1498Szrj vector(size_type __n, const _Tp& __value, 168*38fd1498Szrj const _Allocator& __a = _Allocator()) 169*38fd1498Szrj : _Base(__n, __value, __a) { } 170*38fd1498Szrj#else 171*38fd1498Szrj explicit 172*38fd1498Szrj vector(size_type __n, const _Tp& __value = _Tp(), 173*38fd1498Szrj const _Allocator& __a = _Allocator()) 174*38fd1498Szrj : _Base(__n, __value, __a) { } 175*38fd1498Szrj#endif 176*38fd1498Szrj 177*38fd1498Szrj#if __cplusplus >= 201103L 178*38fd1498Szrj template<class _InputIterator, 179*38fd1498Szrj typename = std::_RequireInputIter<_InputIterator>> 180*38fd1498Szrj#else 181*38fd1498Szrj template<class _InputIterator> 182*38fd1498Szrj#endif 183*38fd1498Szrj vector(_InputIterator __first, _InputIterator __last, 184*38fd1498Szrj const _Allocator& __a = _Allocator()) 185*38fd1498Szrj : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first, 186*38fd1498Szrj __last)), 187*38fd1498Szrj __gnu_debug::__base(__last), __a) { } 188*38fd1498Szrj 189*38fd1498Szrj#if __cplusplus < 201103L 190*38fd1498Szrj vector(const vector& __x) 191*38fd1498Szrj : _Base(__x) { } 192*38fd1498Szrj 193*38fd1498Szrj ~vector() _GLIBCXX_NOEXCEPT { } 194*38fd1498Szrj#else 195*38fd1498Szrj vector(const vector&) = default; 196*38fd1498Szrj vector(vector&&) = default; 197*38fd1498Szrj 198*38fd1498Szrj vector(const vector& __x, const allocator_type& __a) 199*38fd1498Szrj : _Base(__x, __a) { } 200*38fd1498Szrj 201*38fd1498Szrj vector(vector&& __x, const allocator_type& __a) 202*38fd1498Szrj : _Safe(std::move(__x._M_safe()), __a), 203*38fd1498Szrj _Base(std::move(__x._M_base()), __a), 204*38fd1498Szrj _Safe_vector(std::move(__x)) { } 205*38fd1498Szrj 206*38fd1498Szrj vector(initializer_list<value_type> __l, 207*38fd1498Szrj const allocator_type& __a = allocator_type()) 208*38fd1498Szrj : _Base(__l, __a) { } 209*38fd1498Szrj 210*38fd1498Szrj ~vector() = default; 211*38fd1498Szrj#endif 212*38fd1498Szrj 213*38fd1498Szrj /// Construction from a normal-mode vector 214*38fd1498Szrj vector(const _Base& __x) 215*38fd1498Szrj : _Base(__x) { } 216*38fd1498Szrj 217*38fd1498Szrj#if __cplusplus < 201103L 218*38fd1498Szrj vector& 219*38fd1498Szrj operator=(const vector& __x) 220*38fd1498Szrj { 221*38fd1498Szrj this->_M_safe() = __x; 222*38fd1498Szrj _M_base() = __x; 223*38fd1498Szrj this->_M_update_guaranteed_capacity(); 224*38fd1498Szrj return *this; 225*38fd1498Szrj } 226*38fd1498Szrj#else 227*38fd1498Szrj vector& 228*38fd1498Szrj operator=(const vector&) = default; 229*38fd1498Szrj 230*38fd1498Szrj vector& 231*38fd1498Szrj operator=(vector&&) = default; 232*38fd1498Szrj 233*38fd1498Szrj vector& 234*38fd1498Szrj operator=(initializer_list<value_type> __l) 235*38fd1498Szrj { 236*38fd1498Szrj _M_base() = __l; 237*38fd1498Szrj this->_M_invalidate_all(); 238*38fd1498Szrj this->_M_update_guaranteed_capacity(); 239*38fd1498Szrj return *this; 240*38fd1498Szrj } 241*38fd1498Szrj#endif 242*38fd1498Szrj 243*38fd1498Szrj#if __cplusplus >= 201103L 244*38fd1498Szrj template<typename _InputIterator, 245*38fd1498Szrj typename = std::_RequireInputIter<_InputIterator>> 246*38fd1498Szrj#else 247*38fd1498Szrj template<typename _InputIterator> 248*38fd1498Szrj#endif 249*38fd1498Szrj void 250*38fd1498Szrj assign(_InputIterator __first, _InputIterator __last) 251*38fd1498Szrj { 252*38fd1498Szrj typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist; 253*38fd1498Szrj __glibcxx_check_valid_range2(__first, __last, __dist); 254*38fd1498Szrj 255*38fd1498Szrj if (__dist.second >= __gnu_debug::__dp_sign) 256*38fd1498Szrj _Base::assign(__gnu_debug::__unsafe(__first), 257*38fd1498Szrj __gnu_debug::__unsafe(__last)); 258*38fd1498Szrj else 259*38fd1498Szrj _Base::assign(__first, __last); 260*38fd1498Szrj 261*38fd1498Szrj this->_M_invalidate_all(); 262*38fd1498Szrj this->_M_update_guaranteed_capacity(); 263*38fd1498Szrj } 264*38fd1498Szrj 265*38fd1498Szrj void 266*38fd1498Szrj assign(size_type __n, const _Tp& __u) 267*38fd1498Szrj { 268*38fd1498Szrj _Base::assign(__n, __u); 269*38fd1498Szrj this->_M_invalidate_all(); 270*38fd1498Szrj this->_M_update_guaranteed_capacity(); 271*38fd1498Szrj } 272*38fd1498Szrj 273*38fd1498Szrj#if __cplusplus >= 201103L 274*38fd1498Szrj void 275*38fd1498Szrj assign(initializer_list<value_type> __l) 276*38fd1498Szrj { 277*38fd1498Szrj _Base::assign(__l); 278*38fd1498Szrj this->_M_invalidate_all(); 279*38fd1498Szrj this->_M_update_guaranteed_capacity(); 280*38fd1498Szrj } 281*38fd1498Szrj#endif 282*38fd1498Szrj 283*38fd1498Szrj using _Base::get_allocator; 284*38fd1498Szrj 285*38fd1498Szrj // iterators: 286*38fd1498Szrj iterator 287*38fd1498Szrj begin() _GLIBCXX_NOEXCEPT 288*38fd1498Szrj { return iterator(_Base::begin(), this); } 289*38fd1498Szrj 290*38fd1498Szrj const_iterator 291*38fd1498Szrj begin() const _GLIBCXX_NOEXCEPT 292*38fd1498Szrj { return const_iterator(_Base::begin(), this); } 293*38fd1498Szrj 294*38fd1498Szrj iterator 295*38fd1498Szrj end() _GLIBCXX_NOEXCEPT 296*38fd1498Szrj { return iterator(_Base::end(), this); } 297*38fd1498Szrj 298*38fd1498Szrj const_iterator 299*38fd1498Szrj end() const _GLIBCXX_NOEXCEPT 300*38fd1498Szrj { return const_iterator(_Base::end(), this); } 301*38fd1498Szrj 302*38fd1498Szrj reverse_iterator 303*38fd1498Szrj rbegin() _GLIBCXX_NOEXCEPT 304*38fd1498Szrj { return reverse_iterator(end()); } 305*38fd1498Szrj 306*38fd1498Szrj const_reverse_iterator 307*38fd1498Szrj rbegin() const _GLIBCXX_NOEXCEPT 308*38fd1498Szrj { return const_reverse_iterator(end()); } 309*38fd1498Szrj 310*38fd1498Szrj reverse_iterator 311*38fd1498Szrj rend() _GLIBCXX_NOEXCEPT 312*38fd1498Szrj { return reverse_iterator(begin()); } 313*38fd1498Szrj 314*38fd1498Szrj const_reverse_iterator 315*38fd1498Szrj rend() const _GLIBCXX_NOEXCEPT 316*38fd1498Szrj { return const_reverse_iterator(begin()); } 317*38fd1498Szrj 318*38fd1498Szrj#if __cplusplus >= 201103L 319*38fd1498Szrj const_iterator 320*38fd1498Szrj cbegin() const noexcept 321*38fd1498Szrj { return const_iterator(_Base::begin(), this); } 322*38fd1498Szrj 323*38fd1498Szrj const_iterator 324*38fd1498Szrj cend() const noexcept 325*38fd1498Szrj { return const_iterator(_Base::end(), this); } 326*38fd1498Szrj 327*38fd1498Szrj const_reverse_iterator 328*38fd1498Szrj crbegin() const noexcept 329*38fd1498Szrj { return const_reverse_iterator(end()); } 330*38fd1498Szrj 331*38fd1498Szrj const_reverse_iterator 332*38fd1498Szrj crend() const noexcept 333*38fd1498Szrj { return const_reverse_iterator(begin()); } 334*38fd1498Szrj#endif 335*38fd1498Szrj 336*38fd1498Szrj // 23.2.4.2 capacity: 337*38fd1498Szrj using _Base::size; 338*38fd1498Szrj using _Base::max_size; 339*38fd1498Szrj 340*38fd1498Szrj#if __cplusplus >= 201103L 341*38fd1498Szrj void 342*38fd1498Szrj resize(size_type __sz) 343*38fd1498Szrj { 344*38fd1498Szrj bool __realloc = this->_M_requires_reallocation(__sz); 345*38fd1498Szrj if (__sz < this->size()) 346*38fd1498Szrj this->_M_invalidate_after_nth(__sz); 347*38fd1498Szrj _Base::resize(__sz); 348*38fd1498Szrj if (__realloc) 349*38fd1498Szrj this->_M_invalidate_all(); 350*38fd1498Szrj this->_M_update_guaranteed_capacity(); 351*38fd1498Szrj } 352*38fd1498Szrj 353*38fd1498Szrj void 354*38fd1498Szrj resize(size_type __sz, const _Tp& __c) 355*38fd1498Szrj { 356*38fd1498Szrj bool __realloc = this->_M_requires_reallocation(__sz); 357*38fd1498Szrj if (__sz < this->size()) 358*38fd1498Szrj this->_M_invalidate_after_nth(__sz); 359*38fd1498Szrj _Base::resize(__sz, __c); 360*38fd1498Szrj if (__realloc) 361*38fd1498Szrj this->_M_invalidate_all(); 362*38fd1498Szrj this->_M_update_guaranteed_capacity(); 363*38fd1498Szrj } 364*38fd1498Szrj#else 365*38fd1498Szrj void 366*38fd1498Szrj resize(size_type __sz, _Tp __c = _Tp()) 367*38fd1498Szrj { 368*38fd1498Szrj bool __realloc = this->_M_requires_reallocation(__sz); 369*38fd1498Szrj if (__sz < this->size()) 370*38fd1498Szrj this->_M_invalidate_after_nth(__sz); 371*38fd1498Szrj _Base::resize(__sz, __c); 372*38fd1498Szrj if (__realloc) 373*38fd1498Szrj this->_M_invalidate_all(); 374*38fd1498Szrj this->_M_update_guaranteed_capacity(); 375*38fd1498Szrj } 376*38fd1498Szrj#endif 377*38fd1498Szrj 378*38fd1498Szrj#if __cplusplus >= 201103L 379*38fd1498Szrj void 380*38fd1498Szrj shrink_to_fit() 381*38fd1498Szrj { 382*38fd1498Szrj if (_Base::_M_shrink_to_fit()) 383*38fd1498Szrj { 384*38fd1498Szrj this->_M_guaranteed_capacity = _Base::capacity(); 385*38fd1498Szrj this->_M_invalidate_all(); 386*38fd1498Szrj } 387*38fd1498Szrj } 388*38fd1498Szrj#endif 389*38fd1498Szrj 390*38fd1498Szrj size_type 391*38fd1498Szrj capacity() const _GLIBCXX_NOEXCEPT 392*38fd1498Szrj { 393*38fd1498Szrj#ifdef _GLIBCXX_DEBUG_PEDANTIC 394*38fd1498Szrj return this->_M_guaranteed_capacity; 395*38fd1498Szrj#else 396*38fd1498Szrj return _Base::capacity(); 397*38fd1498Szrj#endif 398*38fd1498Szrj } 399*38fd1498Szrj 400*38fd1498Szrj using _Base::empty; 401*38fd1498Szrj 402*38fd1498Szrj void 403*38fd1498Szrj reserve(size_type __n) 404*38fd1498Szrj { 405*38fd1498Szrj bool __realloc = this->_M_requires_reallocation(__n); 406*38fd1498Szrj _Base::reserve(__n); 407*38fd1498Szrj if (__n > this->_M_guaranteed_capacity) 408*38fd1498Szrj this->_M_guaranteed_capacity = __n; 409*38fd1498Szrj if (__realloc) 410*38fd1498Szrj this->_M_invalidate_all(); 411*38fd1498Szrj } 412*38fd1498Szrj 413*38fd1498Szrj // element access: 414*38fd1498Szrj reference 415*38fd1498Szrj operator[](size_type __n) _GLIBCXX_NOEXCEPT 416*38fd1498Szrj { 417*38fd1498Szrj __glibcxx_check_subscript(__n); 418*38fd1498Szrj return _M_base()[__n]; 419*38fd1498Szrj } 420*38fd1498Szrj 421*38fd1498Szrj const_reference 422*38fd1498Szrj operator[](size_type __n) const _GLIBCXX_NOEXCEPT 423*38fd1498Szrj { 424*38fd1498Szrj __glibcxx_check_subscript(__n); 425*38fd1498Szrj return _M_base()[__n]; 426*38fd1498Szrj } 427*38fd1498Szrj 428*38fd1498Szrj using _Base::at; 429*38fd1498Szrj 430*38fd1498Szrj reference 431*38fd1498Szrj front() _GLIBCXX_NOEXCEPT 432*38fd1498Szrj { 433*38fd1498Szrj __glibcxx_check_nonempty(); 434*38fd1498Szrj return _Base::front(); 435*38fd1498Szrj } 436*38fd1498Szrj 437*38fd1498Szrj const_reference 438*38fd1498Szrj front() const _GLIBCXX_NOEXCEPT 439*38fd1498Szrj { 440*38fd1498Szrj __glibcxx_check_nonempty(); 441*38fd1498Szrj return _Base::front(); 442*38fd1498Szrj } 443*38fd1498Szrj 444*38fd1498Szrj reference 445*38fd1498Szrj back() _GLIBCXX_NOEXCEPT 446*38fd1498Szrj { 447*38fd1498Szrj __glibcxx_check_nonempty(); 448*38fd1498Szrj return _Base::back(); 449*38fd1498Szrj } 450*38fd1498Szrj 451*38fd1498Szrj const_reference 452*38fd1498Szrj back() const _GLIBCXX_NOEXCEPT 453*38fd1498Szrj { 454*38fd1498Szrj __glibcxx_check_nonempty(); 455*38fd1498Szrj return _Base::back(); 456*38fd1498Szrj } 457*38fd1498Szrj 458*38fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS 459*38fd1498Szrj // DR 464. Suggestion for new member functions in standard containers. 460*38fd1498Szrj using _Base::data; 461*38fd1498Szrj 462*38fd1498Szrj // 23.2.4.3 modifiers: 463*38fd1498Szrj void 464*38fd1498Szrj push_back(const _Tp& __x) 465*38fd1498Szrj { 466*38fd1498Szrj bool __realloc = this->_M_requires_reallocation(this->size() + 1); 467*38fd1498Szrj _Base::push_back(__x); 468*38fd1498Szrj if (__realloc) 469*38fd1498Szrj this->_M_invalidate_all(); 470*38fd1498Szrj this->_M_update_guaranteed_capacity(); 471*38fd1498Szrj } 472*38fd1498Szrj 473*38fd1498Szrj#if __cplusplus >= 201103L 474*38fd1498Szrj template<typename _Up = _Tp> 475*38fd1498Szrj typename __gnu_cxx::__enable_if<!std::__are_same<_Up, bool>::__value, 476*38fd1498Szrj void>::__type 477*38fd1498Szrj push_back(_Tp&& __x) 478*38fd1498Szrj { emplace_back(std::move(__x)); } 479*38fd1498Szrj 480*38fd1498Szrj template<typename... _Args> 481*38fd1498Szrj#if __cplusplus > 201402L 482*38fd1498Szrj reference 483*38fd1498Szrj#else 484*38fd1498Szrj void 485*38fd1498Szrj#endif 486*38fd1498Szrj emplace_back(_Args&&... __args) 487*38fd1498Szrj { 488*38fd1498Szrj bool __realloc = this->_M_requires_reallocation(this->size() + 1); 489*38fd1498Szrj _Base::emplace_back(std::forward<_Args>(__args)...); 490*38fd1498Szrj if (__realloc) 491*38fd1498Szrj this->_M_invalidate_all(); 492*38fd1498Szrj this->_M_update_guaranteed_capacity(); 493*38fd1498Szrj#if __cplusplus > 201402L 494*38fd1498Szrj return back(); 495*38fd1498Szrj#endif 496*38fd1498Szrj } 497*38fd1498Szrj#endif 498*38fd1498Szrj 499*38fd1498Szrj void 500*38fd1498Szrj pop_back() _GLIBCXX_NOEXCEPT 501*38fd1498Szrj { 502*38fd1498Szrj __glibcxx_check_nonempty(); 503*38fd1498Szrj this->_M_invalidate_if(_Equal(--_Base::end())); 504*38fd1498Szrj _Base::pop_back(); 505*38fd1498Szrj } 506*38fd1498Szrj 507*38fd1498Szrj#if __cplusplus >= 201103L 508*38fd1498Szrj template<typename... _Args> 509*38fd1498Szrj iterator 510*38fd1498Szrj emplace(const_iterator __position, _Args&&... __args) 511*38fd1498Szrj { 512*38fd1498Szrj __glibcxx_check_insert(__position); 513*38fd1498Szrj bool __realloc = this->_M_requires_reallocation(this->size() + 1); 514*38fd1498Szrj difference_type __offset = __position.base() - _Base::begin(); 515*38fd1498Szrj _Base_iterator __res = _Base::emplace(__position.base(), 516*38fd1498Szrj std::forward<_Args>(__args)...); 517*38fd1498Szrj if (__realloc) 518*38fd1498Szrj this->_M_invalidate_all(); 519*38fd1498Szrj else 520*38fd1498Szrj this->_M_invalidate_after_nth(__offset); 521*38fd1498Szrj this->_M_update_guaranteed_capacity(); 522*38fd1498Szrj return iterator(__res, this); 523*38fd1498Szrj } 524*38fd1498Szrj#endif 525*38fd1498Szrj 526*38fd1498Szrj iterator 527*38fd1498Szrj#if __cplusplus >= 201103L 528*38fd1498Szrj insert(const_iterator __position, const _Tp& __x) 529*38fd1498Szrj#else 530*38fd1498Szrj insert(iterator __position, const _Tp& __x) 531*38fd1498Szrj#endif 532*38fd1498Szrj { 533*38fd1498Szrj __glibcxx_check_insert(__position); 534*38fd1498Szrj bool __realloc = this->_M_requires_reallocation(this->size() + 1); 535*38fd1498Szrj difference_type __offset = __position.base() - _Base::begin(); 536*38fd1498Szrj _Base_iterator __res = _Base::insert(__position.base(), __x); 537*38fd1498Szrj if (__realloc) 538*38fd1498Szrj this->_M_invalidate_all(); 539*38fd1498Szrj else 540*38fd1498Szrj this->_M_invalidate_after_nth(__offset); 541*38fd1498Szrj this->_M_update_guaranteed_capacity(); 542*38fd1498Szrj return iterator(__res, this); 543*38fd1498Szrj } 544*38fd1498Szrj 545*38fd1498Szrj#if __cplusplus >= 201103L 546*38fd1498Szrj template<typename _Up = _Tp> 547*38fd1498Szrj typename __gnu_cxx::__enable_if<!std::__are_same<_Up, bool>::__value, 548*38fd1498Szrj iterator>::__type 549*38fd1498Szrj insert(const_iterator __position, _Tp&& __x) 550*38fd1498Szrj { return emplace(__position, std::move(__x)); } 551*38fd1498Szrj 552*38fd1498Szrj iterator 553*38fd1498Szrj insert(const_iterator __position, initializer_list<value_type> __l) 554*38fd1498Szrj { return this->insert(__position, __l.begin(), __l.end()); } 555*38fd1498Szrj#endif 556*38fd1498Szrj 557*38fd1498Szrj#if __cplusplus >= 201103L 558*38fd1498Szrj iterator 559*38fd1498Szrj insert(const_iterator __position, size_type __n, const _Tp& __x) 560*38fd1498Szrj { 561*38fd1498Szrj __glibcxx_check_insert(__position); 562*38fd1498Szrj bool __realloc = this->_M_requires_reallocation(this->size() + __n); 563*38fd1498Szrj difference_type __offset = __position.base() - _Base::cbegin(); 564*38fd1498Szrj _Base_iterator __res = _Base::insert(__position.base(), __n, __x); 565*38fd1498Szrj if (__realloc) 566*38fd1498Szrj this->_M_invalidate_all(); 567*38fd1498Szrj else 568*38fd1498Szrj this->_M_invalidate_after_nth(__offset); 569*38fd1498Szrj this->_M_update_guaranteed_capacity(); 570*38fd1498Szrj return iterator(__res, this); 571*38fd1498Szrj } 572*38fd1498Szrj#else 573*38fd1498Szrj void 574*38fd1498Szrj insert(iterator __position, size_type __n, const _Tp& __x) 575*38fd1498Szrj { 576*38fd1498Szrj __glibcxx_check_insert(__position); 577*38fd1498Szrj bool __realloc = this->_M_requires_reallocation(this->size() + __n); 578*38fd1498Szrj difference_type __offset = __position.base() - _Base::begin(); 579*38fd1498Szrj _Base::insert(__position.base(), __n, __x); 580*38fd1498Szrj if (__realloc) 581*38fd1498Szrj this->_M_invalidate_all(); 582*38fd1498Szrj else 583*38fd1498Szrj this->_M_invalidate_after_nth(__offset); 584*38fd1498Szrj this->_M_update_guaranteed_capacity(); 585*38fd1498Szrj } 586*38fd1498Szrj#endif 587*38fd1498Szrj 588*38fd1498Szrj#if __cplusplus >= 201103L 589*38fd1498Szrj template<class _InputIterator, 590*38fd1498Szrj typename = std::_RequireInputIter<_InputIterator>> 591*38fd1498Szrj iterator 592*38fd1498Szrj insert(const_iterator __position, 593*38fd1498Szrj _InputIterator __first, _InputIterator __last) 594*38fd1498Szrj { 595*38fd1498Szrj typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist; 596*38fd1498Szrj __glibcxx_check_insert_range(__position, __first, __last, __dist); 597*38fd1498Szrj 598*38fd1498Szrj /* Hard to guess if invalidation will occur, because __last 599*38fd1498Szrj - __first can't be calculated in all cases, so we just 600*38fd1498Szrj punt here by checking if it did occur. */ 601*38fd1498Szrj _Base_iterator __old_begin = _M_base().begin(); 602*38fd1498Szrj difference_type __offset = __position.base() - _Base::cbegin(); 603*38fd1498Szrj _Base_iterator __res; 604*38fd1498Szrj if (__dist.second >= __gnu_debug::__dp_sign) 605*38fd1498Szrj __res = _Base::insert(__position.base(), 606*38fd1498Szrj __gnu_debug::__unsafe(__first), 607*38fd1498Szrj __gnu_debug::__unsafe(__last)); 608*38fd1498Szrj else 609*38fd1498Szrj __res = _Base::insert(__position.base(), __first, __last); 610*38fd1498Szrj 611*38fd1498Szrj if (_M_base().begin() != __old_begin) 612*38fd1498Szrj this->_M_invalidate_all(); 613*38fd1498Szrj else 614*38fd1498Szrj this->_M_invalidate_after_nth(__offset); 615*38fd1498Szrj this->_M_update_guaranteed_capacity(); 616*38fd1498Szrj return iterator(__res, this); 617*38fd1498Szrj } 618*38fd1498Szrj#else 619*38fd1498Szrj template<class _InputIterator> 620*38fd1498Szrj void 621*38fd1498Szrj insert(iterator __position, 622*38fd1498Szrj _InputIterator __first, _InputIterator __last) 623*38fd1498Szrj { 624*38fd1498Szrj typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist; 625*38fd1498Szrj __glibcxx_check_insert_range(__position, __first, __last, __dist); 626*38fd1498Szrj 627*38fd1498Szrj /* Hard to guess if invalidation will occur, because __last 628*38fd1498Szrj - __first can't be calculated in all cases, so we just 629*38fd1498Szrj punt here by checking if it did occur. */ 630*38fd1498Szrj _Base_iterator __old_begin = _M_base().begin(); 631*38fd1498Szrj difference_type __offset = __position.base() - _Base::begin(); 632*38fd1498Szrj if (__dist.second >= __gnu_debug::__dp_sign) 633*38fd1498Szrj _Base::insert(__position.base(), __gnu_debug::__unsafe(__first), 634*38fd1498Szrj __gnu_debug::__unsafe(__last)); 635*38fd1498Szrj else 636*38fd1498Szrj _Base::insert(__position.base(), __first, __last); 637*38fd1498Szrj 638*38fd1498Szrj if (_M_base().begin() != __old_begin) 639*38fd1498Szrj this->_M_invalidate_all(); 640*38fd1498Szrj else 641*38fd1498Szrj this->_M_invalidate_after_nth(__offset); 642*38fd1498Szrj this->_M_update_guaranteed_capacity(); 643*38fd1498Szrj } 644*38fd1498Szrj#endif 645*38fd1498Szrj 646*38fd1498Szrj iterator 647*38fd1498Szrj#if __cplusplus >= 201103L 648*38fd1498Szrj erase(const_iterator __position) 649*38fd1498Szrj#else 650*38fd1498Szrj erase(iterator __position) 651*38fd1498Szrj#endif 652*38fd1498Szrj { 653*38fd1498Szrj __glibcxx_check_erase(__position); 654*38fd1498Szrj difference_type __offset = __position.base() - _Base::begin(); 655*38fd1498Szrj _Base_iterator __res = _Base::erase(__position.base()); 656*38fd1498Szrj this->_M_invalidate_after_nth(__offset); 657*38fd1498Szrj return iterator(__res, this); 658*38fd1498Szrj } 659*38fd1498Szrj 660*38fd1498Szrj iterator 661*38fd1498Szrj#if __cplusplus >= 201103L 662*38fd1498Szrj erase(const_iterator __first, const_iterator __last) 663*38fd1498Szrj#else 664*38fd1498Szrj erase(iterator __first, iterator __last) 665*38fd1498Szrj#endif 666*38fd1498Szrj { 667*38fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS 668*38fd1498Szrj // 151. can't currently clear() empty container 669*38fd1498Szrj __glibcxx_check_erase_range(__first, __last); 670*38fd1498Szrj 671*38fd1498Szrj if (__first.base() != __last.base()) 672*38fd1498Szrj { 673*38fd1498Szrj difference_type __offset = __first.base() - _Base::begin(); 674*38fd1498Szrj _Base_iterator __res = _Base::erase(__first.base(), 675*38fd1498Szrj __last.base()); 676*38fd1498Szrj this->_M_invalidate_after_nth(__offset); 677*38fd1498Szrj return iterator(__res, this); 678*38fd1498Szrj } 679*38fd1498Szrj else 680*38fd1498Szrj#if __cplusplus >= 201103L 681*38fd1498Szrj return begin() + (__first.base() - cbegin().base()); 682*38fd1498Szrj#else 683*38fd1498Szrj return __first; 684*38fd1498Szrj#endif 685*38fd1498Szrj } 686*38fd1498Szrj 687*38fd1498Szrj void 688*38fd1498Szrj swap(vector& __x) 689*38fd1498Szrj _GLIBCXX_NOEXCEPT_IF( noexcept(declval<_Base&>().swap(__x)) ) 690*38fd1498Szrj { 691*38fd1498Szrj _Safe::_M_swap(__x); 692*38fd1498Szrj _Base::swap(__x); 693*38fd1498Szrj std::swap(this->_M_guaranteed_capacity, __x._M_guaranteed_capacity); 694*38fd1498Szrj } 695*38fd1498Szrj 696*38fd1498Szrj void 697*38fd1498Szrj clear() _GLIBCXX_NOEXCEPT 698*38fd1498Szrj { 699*38fd1498Szrj _Base::clear(); 700*38fd1498Szrj this->_M_invalidate_all(); 701*38fd1498Szrj } 702*38fd1498Szrj 703*38fd1498Szrj _Base& 704*38fd1498Szrj _M_base() _GLIBCXX_NOEXCEPT { return *this; } 705*38fd1498Szrj 706*38fd1498Szrj const _Base& 707*38fd1498Szrj _M_base() const _GLIBCXX_NOEXCEPT { return *this; } 708*38fd1498Szrj 709*38fd1498Szrj private: 710*38fd1498Szrj void 711*38fd1498Szrj _M_invalidate_after_nth(difference_type __n) _GLIBCXX_NOEXCEPT 712*38fd1498Szrj { 713*38fd1498Szrj typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth; 714*38fd1498Szrj this->_M_invalidate_if(_After_nth(__n, _Base::begin())); 715*38fd1498Szrj } 716*38fd1498Szrj }; 717*38fd1498Szrj 718*38fd1498Szrj template<typename _Tp, typename _Alloc> 719*38fd1498Szrj inline bool 720*38fd1498Szrj operator==(const vector<_Tp, _Alloc>& __lhs, 721*38fd1498Szrj const vector<_Tp, _Alloc>& __rhs) 722*38fd1498Szrj { return __lhs._M_base() == __rhs._M_base(); } 723*38fd1498Szrj 724*38fd1498Szrj template<typename _Tp, typename _Alloc> 725*38fd1498Szrj inline bool 726*38fd1498Szrj operator!=(const vector<_Tp, _Alloc>& __lhs, 727*38fd1498Szrj const vector<_Tp, _Alloc>& __rhs) 728*38fd1498Szrj { return __lhs._M_base() != __rhs._M_base(); } 729*38fd1498Szrj 730*38fd1498Szrj template<typename _Tp, typename _Alloc> 731*38fd1498Szrj inline bool 732*38fd1498Szrj operator<(const vector<_Tp, _Alloc>& __lhs, 733*38fd1498Szrj const vector<_Tp, _Alloc>& __rhs) 734*38fd1498Szrj { return __lhs._M_base() < __rhs._M_base(); } 735*38fd1498Szrj 736*38fd1498Szrj template<typename _Tp, typename _Alloc> 737*38fd1498Szrj inline bool 738*38fd1498Szrj operator<=(const vector<_Tp, _Alloc>& __lhs, 739*38fd1498Szrj const vector<_Tp, _Alloc>& __rhs) 740*38fd1498Szrj { return __lhs._M_base() <= __rhs._M_base(); } 741*38fd1498Szrj 742*38fd1498Szrj template<typename _Tp, typename _Alloc> 743*38fd1498Szrj inline bool 744*38fd1498Szrj operator>=(const vector<_Tp, _Alloc>& __lhs, 745*38fd1498Szrj const vector<_Tp, _Alloc>& __rhs) 746*38fd1498Szrj { return __lhs._M_base() >= __rhs._M_base(); } 747*38fd1498Szrj 748*38fd1498Szrj template<typename _Tp, typename _Alloc> 749*38fd1498Szrj inline bool 750*38fd1498Szrj operator>(const vector<_Tp, _Alloc>& __lhs, 751*38fd1498Szrj const vector<_Tp, _Alloc>& __rhs) 752*38fd1498Szrj { return __lhs._M_base() > __rhs._M_base(); } 753*38fd1498Szrj 754*38fd1498Szrj template<typename _Tp, typename _Alloc> 755*38fd1498Szrj inline void 756*38fd1498Szrj swap(vector<_Tp, _Alloc>& __lhs, vector<_Tp, _Alloc>& __rhs) 757*38fd1498Szrj _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs))) 758*38fd1498Szrj { __lhs.swap(__rhs); } 759*38fd1498Szrj 760*38fd1498Szrj#if __cpp_deduction_guides >= 201606 761*38fd1498Szrj template<typename _InputIterator, typename _ValT 762*38fd1498Szrj = typename iterator_traits<_InputIterator>::value_type, 763*38fd1498Szrj typename _Allocator = allocator<_ValT>, 764*38fd1498Szrj typename = _RequireInputIter<_InputIterator>, 765*38fd1498Szrj typename = _RequireAllocator<_Allocator>> 766*38fd1498Szrj vector(_InputIterator, _InputIterator, _Allocator = _Allocator()) 767*38fd1498Szrj -> vector<_ValT, _Allocator>; 768*38fd1498Szrj#endif 769*38fd1498Szrj 770*38fd1498Szrj} // namespace __debug 771*38fd1498Szrj 772*38fd1498Szrj#if __cplusplus >= 201103L 773*38fd1498Szrj_GLIBCXX_BEGIN_NAMESPACE_VERSION 774*38fd1498Szrj 775*38fd1498Szrj // DR 1182. 776*38fd1498Szrj /// std::hash specialization for vector<bool>. 777*38fd1498Szrj template<typename _Alloc> 778*38fd1498Szrj struct hash<__debug::vector<bool, _Alloc>> 779*38fd1498Szrj : public __hash_base<size_t, __debug::vector<bool, _Alloc>> 780*38fd1498Szrj { 781*38fd1498Szrj size_t 782*38fd1498Szrj operator()(const __debug::vector<bool, _Alloc>& __b) const noexcept 783*38fd1498Szrj { return std::hash<_GLIBCXX_STD_C::vector<bool, _Alloc>>()(__b); } 784*38fd1498Szrj }; 785*38fd1498Szrj 786*38fd1498Szrj_GLIBCXX_END_NAMESPACE_VERSION 787*38fd1498Szrj#endif 788*38fd1498Szrj 789*38fd1498Szrj} // namespace std 790*38fd1498Szrj 791*38fd1498Szrjnamespace __gnu_debug 792*38fd1498Szrj{ 793*38fd1498Szrj template<typename _Tp, typename _Alloc> 794*38fd1498Szrj struct _Is_contiguous_sequence<std::__debug::vector<_Tp, _Alloc> > 795*38fd1498Szrj : std::__true_type 796*38fd1498Szrj { }; 797*38fd1498Szrj 798*38fd1498Szrj template<typename _Alloc> 799*38fd1498Szrj struct _Is_contiguous_sequence<std::__debug::vector<bool, _Alloc> > 800*38fd1498Szrj : std::__false_type 801*38fd1498Szrj { }; 802*38fd1498Szrj} 803*38fd1498Szrj 804*38fd1498Szrj#endif 805