vector 24 KB

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