vector 22 KB

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