vector 23 KB

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