deque 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. // Debugging deque 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/deque
  21. * This file is a GNU debug extension to the Standard C++ Library.
  22. */
  23. #ifndef _GLIBCXX_DEBUG_DEQUE
  24. #define _GLIBCXX_DEBUG_DEQUE 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 deque;
  29. } } // namespace std::__debug
  30. #include <deque>
  31. #include <debug/safe_sequence.h>
  32. #include <debug/safe_container.h>
  33. #include <debug/safe_iterator.h>
  34. namespace std _GLIBCXX_VISIBILITY(default)
  35. {
  36. namespace __debug
  37. {
  38. /// Class std::deque with safety/checking/debug instrumentation.
  39. template<typename _Tp, typename _Allocator = std::allocator<_Tp> >
  40. class deque
  41. : public __gnu_debug::_Safe_container<
  42. deque<_Tp, _Allocator>, _Allocator,
  43. __gnu_debug::_Safe_sequence>,
  44. public _GLIBCXX_STD_C::deque<_Tp, _Allocator>
  45. {
  46. typedef _GLIBCXX_STD_C::deque<_Tp, _Allocator> _Base;
  47. typedef __gnu_debug::_Safe_container<
  48. deque, _Allocator, __gnu_debug::_Safe_sequence> _Safe;
  49. typedef typename _Base::const_iterator _Base_const_iterator;
  50. typedef typename _Base::iterator _Base_iterator;
  51. typedef __gnu_debug::_Equal_to<_Base_const_iterator> _Equal;
  52. template<typename _ItT, typename _SeqT, typename _CatT>
  53. friend class ::__gnu_debug::_Safe_iterator;
  54. // Reference wrapper for base class. Disambiguates deque(const _Base&)
  55. // from copy constructor by requiring a user-defined conversion.
  56. // See PR libstdc++/90102.
  57. struct _Base_ref
  58. {
  59. _Base_ref(const _Base& __r) : _M_ref(__r) { }
  60. const _Base& _M_ref;
  61. };
  62. public:
  63. typedef typename _Base::reference reference;
  64. typedef typename _Base::const_reference const_reference;
  65. typedef __gnu_debug::_Safe_iterator<_Base_iterator, deque>
  66. iterator;
  67. typedef __gnu_debug::_Safe_iterator<_Base_const_iterator, deque>
  68. const_iterator;
  69. typedef typename _Base::size_type size_type;
  70. typedef typename _Base::difference_type difference_type;
  71. typedef _Tp value_type;
  72. typedef _Allocator allocator_type;
  73. typedef typename _Base::pointer pointer;
  74. typedef typename _Base::const_pointer const_pointer;
  75. typedef std::reverse_iterator<iterator> reverse_iterator;
  76. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  77. // 23.2.1.1 construct/copy/destroy:
  78. #if __cplusplus < 201103L
  79. deque()
  80. : _Base() { }
  81. deque(const deque& __x)
  82. : _Base(__x) { }
  83. ~deque() { }
  84. #else
  85. deque() = default;
  86. deque(const deque&) = default;
  87. deque(deque&&) = default;
  88. deque(const deque& __d, const _Allocator& __a)
  89. : _Base(__d, __a) { }
  90. deque(deque&& __d, const _Allocator& __a)
  91. : _Safe(std::move(__d)), _Base(std::move(__d), __a) { }
  92. deque(initializer_list<value_type> __l,
  93. const allocator_type& __a = allocator_type())
  94. : _Base(__l, __a) { }
  95. ~deque() = default;
  96. #endif
  97. explicit
  98. deque(const _Allocator& __a)
  99. : _Base(__a) { }
  100. #if __cplusplus >= 201103L
  101. explicit
  102. deque(size_type __n, const _Allocator& __a = _Allocator())
  103. : _Base(__n, __a) { }
  104. deque(size_type __n, const _Tp& __value,
  105. const _Allocator& __a = _Allocator())
  106. : _Base(__n, __value, __a) { }
  107. #else
  108. explicit
  109. deque(size_type __n, const _Tp& __value = _Tp(),
  110. const _Allocator& __a = _Allocator())
  111. : _Base(__n, __value, __a) { }
  112. #endif
  113. #if __cplusplus >= 201103L
  114. template<class _InputIterator,
  115. typename = std::_RequireInputIter<_InputIterator>>
  116. #else
  117. template<class _InputIterator>
  118. #endif
  119. deque(_InputIterator __first, _InputIterator __last,
  120. const _Allocator& __a = _Allocator())
  121. : _Base(__gnu_debug::__base(
  122. __glibcxx_check_valid_constructor_range(__first, __last)),
  123. __gnu_debug::__base(__last), __a)
  124. { }
  125. deque(_Base_ref __x)
  126. : _Base(__x._M_ref) { }
  127. #if __cplusplus < 201103L
  128. deque&
  129. operator=(const deque& __x)
  130. {
  131. this->_M_safe() = __x;
  132. _M_base() = __x;
  133. return *this;
  134. }
  135. #else
  136. deque&
  137. operator=(const deque&) = default;
  138. deque&
  139. operator=(deque&&) = default;
  140. deque&
  141. operator=(initializer_list<value_type> __l)
  142. {
  143. _M_base() = __l;
  144. this->_M_invalidate_all();
  145. return *this;
  146. }
  147. #endif
  148. #if __cplusplus >= 201103L
  149. template<class _InputIterator,
  150. typename = std::_RequireInputIter<_InputIterator>>
  151. #else
  152. template<class _InputIterator>
  153. #endif
  154. void
  155. assign(_InputIterator __first, _InputIterator __last)
  156. {
  157. typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
  158. __glibcxx_check_valid_range2(__first, __last, __dist);
  159. if (__dist.second >= __gnu_debug::__dp_sign)
  160. _Base::assign(__gnu_debug::__unsafe(__first),
  161. __gnu_debug::__unsafe(__last));
  162. else
  163. _Base::assign(__first, __last);
  164. this->_M_invalidate_all();
  165. }
  166. void
  167. assign(size_type __n, const _Tp& __t)
  168. {
  169. _Base::assign(__n, __t);
  170. this->_M_invalidate_all();
  171. }
  172. #if __cplusplus >= 201103L
  173. void
  174. assign(initializer_list<value_type> __l)
  175. {
  176. _Base::assign(__l);
  177. this->_M_invalidate_all();
  178. }
  179. #endif
  180. using _Base::get_allocator;
  181. // iterators:
  182. iterator
  183. begin() _GLIBCXX_NOEXCEPT
  184. { return iterator(_Base::begin(), this); }
  185. const_iterator
  186. begin() const _GLIBCXX_NOEXCEPT
  187. { return const_iterator(_Base::begin(), this); }
  188. iterator
  189. end() _GLIBCXX_NOEXCEPT
  190. { return iterator(_Base::end(), this); }
  191. const_iterator
  192. end() const _GLIBCXX_NOEXCEPT
  193. { return const_iterator(_Base::end(), this); }
  194. reverse_iterator
  195. rbegin() _GLIBCXX_NOEXCEPT
  196. { return reverse_iterator(end()); }
  197. const_reverse_iterator
  198. rbegin() const _GLIBCXX_NOEXCEPT
  199. { return const_reverse_iterator(end()); }
  200. reverse_iterator
  201. rend() _GLIBCXX_NOEXCEPT
  202. { return reverse_iterator(begin()); }
  203. const_reverse_iterator
  204. rend() const _GLIBCXX_NOEXCEPT
  205. { return const_reverse_iterator(begin()); }
  206. #if __cplusplus >= 201103L
  207. const_iterator
  208. cbegin() const noexcept
  209. { return const_iterator(_Base::begin(), this); }
  210. const_iterator
  211. cend() const noexcept
  212. { return const_iterator(_Base::end(), this); }
  213. const_reverse_iterator
  214. crbegin() const noexcept
  215. { return const_reverse_iterator(end()); }
  216. const_reverse_iterator
  217. crend() const noexcept
  218. { return const_reverse_iterator(begin()); }
  219. #endif
  220. private:
  221. void
  222. _M_invalidate_after_nth(difference_type __n)
  223. {
  224. typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
  225. this->_M_invalidate_if(_After_nth(__n, _Base::begin()));
  226. }
  227. public:
  228. // 23.2.1.2 capacity:
  229. using _Base::size;
  230. using _Base::max_size;
  231. #if __cplusplus >= 201103L
  232. void
  233. resize(size_type __sz)
  234. {
  235. bool __invalidate_all = __sz > this->size();
  236. if (__sz < this->size())
  237. this->_M_invalidate_after_nth(__sz);
  238. _Base::resize(__sz);
  239. if (__invalidate_all)
  240. this->_M_invalidate_all();
  241. }
  242. void
  243. resize(size_type __sz, const _Tp& __c)
  244. {
  245. bool __invalidate_all = __sz > this->size();
  246. if (__sz < this->size())
  247. this->_M_invalidate_after_nth(__sz);
  248. _Base::resize(__sz, __c);
  249. if (__invalidate_all)
  250. this->_M_invalidate_all();
  251. }
  252. #else
  253. void
  254. resize(size_type __sz, _Tp __c = _Tp())
  255. {
  256. bool __invalidate_all = __sz > this->size();
  257. if (__sz < this->size())
  258. this->_M_invalidate_after_nth(__sz);
  259. _Base::resize(__sz, __c);
  260. if (__invalidate_all)
  261. this->_M_invalidate_all();
  262. }
  263. #endif
  264. #if __cplusplus >= 201103L
  265. void
  266. shrink_to_fit() noexcept
  267. {
  268. if (_Base::_M_shrink_to_fit())
  269. this->_M_invalidate_all();
  270. }
  271. #endif
  272. using _Base::empty;
  273. // element access:
  274. reference
  275. operator[](size_type __n) _GLIBCXX_NOEXCEPT
  276. {
  277. __glibcxx_check_subscript(__n);
  278. return _M_base()[__n];
  279. }
  280. const_reference
  281. operator[](size_type __n) const _GLIBCXX_NOEXCEPT
  282. {
  283. __glibcxx_check_subscript(__n);
  284. return _M_base()[__n];
  285. }
  286. using _Base::at;
  287. reference
  288. front() _GLIBCXX_NOEXCEPT
  289. {
  290. __glibcxx_check_nonempty();
  291. return _Base::front();
  292. }
  293. const_reference
  294. front() const _GLIBCXX_NOEXCEPT
  295. {
  296. __glibcxx_check_nonempty();
  297. return _Base::front();
  298. }
  299. reference
  300. back() _GLIBCXX_NOEXCEPT
  301. {
  302. __glibcxx_check_nonempty();
  303. return _Base::back();
  304. }
  305. const_reference
  306. back() const _GLIBCXX_NOEXCEPT
  307. {
  308. __glibcxx_check_nonempty();
  309. return _Base::back();
  310. }
  311. // 23.2.1.3 modifiers:
  312. void
  313. push_front(const _Tp& __x)
  314. {
  315. _Base::push_front(__x);
  316. this->_M_invalidate_all();
  317. }
  318. void
  319. push_back(const _Tp& __x)
  320. {
  321. _Base::push_back(__x);
  322. this->_M_invalidate_all();
  323. }
  324. #if __cplusplus >= 201103L
  325. void
  326. push_front(_Tp&& __x)
  327. { emplace_front(std::move(__x)); }
  328. void
  329. push_back(_Tp&& __x)
  330. { emplace_back(std::move(__x)); }
  331. template<typename... _Args>
  332. #if __cplusplus > 201402L
  333. reference
  334. #else
  335. void
  336. #endif
  337. emplace_front(_Args&&... __args)
  338. {
  339. _Base::emplace_front(std::forward<_Args>(__args)...);
  340. this->_M_invalidate_all();
  341. #if __cplusplus > 201402L
  342. return front();
  343. #endif
  344. }
  345. template<typename... _Args>
  346. #if __cplusplus > 201402L
  347. reference
  348. #else
  349. void
  350. #endif
  351. emplace_back(_Args&&... __args)
  352. {
  353. _Base::emplace_back(std::forward<_Args>(__args)...);
  354. this->_M_invalidate_all();
  355. #if __cplusplus > 201402L
  356. return back();
  357. #endif
  358. }
  359. template<typename... _Args>
  360. iterator
  361. emplace(const_iterator __position, _Args&&... __args)
  362. {
  363. __glibcxx_check_insert(__position);
  364. _Base_iterator __res = _Base::emplace(__position.base(),
  365. std::forward<_Args>(__args)...);
  366. this->_M_invalidate_all();
  367. return iterator(__res, this);
  368. }
  369. #endif
  370. iterator
  371. #if __cplusplus >= 201103L
  372. insert(const_iterator __position, const _Tp& __x)
  373. #else
  374. insert(iterator __position, const _Tp& __x)
  375. #endif
  376. {
  377. __glibcxx_check_insert(__position);
  378. _Base_iterator __res = _Base::insert(__position.base(), __x);
  379. this->_M_invalidate_all();
  380. return iterator(__res, this);
  381. }
  382. #if __cplusplus >= 201103L
  383. iterator
  384. insert(const_iterator __position, _Tp&& __x)
  385. { return emplace(__position, std::move(__x)); }
  386. iterator
  387. insert(const_iterator __position, initializer_list<value_type> __l)
  388. {
  389. __glibcxx_check_insert(__position);
  390. _Base_iterator __res = _Base::insert(__position.base(), __l);
  391. this->_M_invalidate_all();
  392. return iterator(__res, this);
  393. }
  394. #endif
  395. #if __cplusplus >= 201103L
  396. iterator
  397. insert(const_iterator __position, size_type __n, const _Tp& __x)
  398. {
  399. __glibcxx_check_insert(__position);
  400. _Base_iterator __res = _Base::insert(__position.base(), __n, __x);
  401. this->_M_invalidate_all();
  402. return iterator(__res, this);
  403. }
  404. #else
  405. void
  406. insert(iterator __position, size_type __n, const _Tp& __x)
  407. {
  408. __glibcxx_check_insert(__position);
  409. _Base::insert(__position.base(), __n, __x);
  410. this->_M_invalidate_all();
  411. }
  412. #endif
  413. #if __cplusplus >= 201103L
  414. template<class _InputIterator,
  415. typename = std::_RequireInputIter<_InputIterator>>
  416. iterator
  417. insert(const_iterator __position,
  418. _InputIterator __first, _InputIterator __last)
  419. {
  420. typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
  421. __glibcxx_check_insert_range(__position, __first, __last, __dist);
  422. _Base_iterator __res;
  423. if (__dist.second >= __gnu_debug::__dp_sign)
  424. __res = _Base::insert(__position.base(),
  425. __gnu_debug::__unsafe(__first),
  426. __gnu_debug::__unsafe(__last));
  427. else
  428. __res = _Base::insert(__position.base(), __first, __last);
  429. this->_M_invalidate_all();
  430. return iterator(__res, this);
  431. }
  432. #else
  433. template<class _InputIterator>
  434. void
  435. insert(iterator __position,
  436. _InputIterator __first, _InputIterator __last)
  437. {
  438. typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
  439. __glibcxx_check_insert_range(__position, __first, __last, __dist);
  440. if (__dist.second >= __gnu_debug::__dp_sign)
  441. _Base::insert(__position.base(),
  442. __gnu_debug::__unsafe(__first),
  443. __gnu_debug::__unsafe(__last));
  444. else
  445. _Base::insert(__position.base(), __first, __last);
  446. this->_M_invalidate_all();
  447. }
  448. #endif
  449. void
  450. pop_front() _GLIBCXX_NOEXCEPT
  451. {
  452. __glibcxx_check_nonempty();
  453. this->_M_invalidate_if(_Equal(_Base::begin()));
  454. _Base::pop_front();
  455. }
  456. void
  457. pop_back() _GLIBCXX_NOEXCEPT
  458. {
  459. __glibcxx_check_nonempty();
  460. this->_M_invalidate_if(_Equal(--_Base::end()));
  461. _Base::pop_back();
  462. }
  463. iterator
  464. #if __cplusplus >= 201103L
  465. erase(const_iterator __position)
  466. #else
  467. erase(iterator __position)
  468. #endif
  469. {
  470. __glibcxx_check_erase(__position);
  471. #if __cplusplus >= 201103L
  472. _Base_const_iterator __victim = __position.base();
  473. #else
  474. _Base_iterator __victim = __position.base();
  475. #endif
  476. if (__victim == _Base::begin() || __victim == _Base::end() - 1)
  477. {
  478. this->_M_invalidate_if(_Equal(__victim));
  479. return iterator(_Base::erase(__victim), this);
  480. }
  481. else
  482. {
  483. _Base_iterator __res = _Base::erase(__victim);
  484. this->_M_invalidate_all();
  485. return iterator(__res, this);
  486. }
  487. }
  488. iterator
  489. #if __cplusplus >= 201103L
  490. erase(const_iterator __first, const_iterator __last)
  491. #else
  492. erase(iterator __first, iterator __last)
  493. #endif
  494. {
  495. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  496. // 151. can't currently clear() empty container
  497. __glibcxx_check_erase_range(__first, __last);
  498. if (__first.base() == __last.base())
  499. #if __cplusplus >= 201103L
  500. return iterator(__first.base()._M_const_cast(), this);
  501. #else
  502. return __first;
  503. #endif
  504. else if (__first.base() == _Base::begin()
  505. || __last.base() == _Base::end())
  506. {
  507. this->_M_detach_singular();
  508. for (_Base_const_iterator __position = __first.base();
  509. __position != __last.base(); ++__position)
  510. {
  511. this->_M_invalidate_if(_Equal(__position));
  512. }
  513. __try
  514. {
  515. return iterator(_Base::erase(__first.base(), __last.base()),
  516. this);
  517. }
  518. __catch(...)
  519. {
  520. this->_M_revalidate_singular();
  521. __throw_exception_again;
  522. }
  523. }
  524. else
  525. {
  526. _Base_iterator __res = _Base::erase(__first.base(),
  527. __last.base());
  528. this->_M_invalidate_all();
  529. return iterator(__res, this);
  530. }
  531. }
  532. void
  533. swap(deque& __x)
  534. _GLIBCXX_NOEXCEPT_IF( noexcept(declval<_Base&>().swap(__x)) )
  535. {
  536. _Safe::_M_swap(__x);
  537. _Base::swap(__x);
  538. }
  539. void
  540. clear() _GLIBCXX_NOEXCEPT
  541. {
  542. _Base::clear();
  543. this->_M_invalidate_all();
  544. }
  545. _Base&
  546. _M_base() _GLIBCXX_NOEXCEPT { return *this; }
  547. const _Base&
  548. _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
  549. };
  550. #if __cpp_deduction_guides >= 201606
  551. template<typename _InputIterator, typename _ValT
  552. = typename iterator_traits<_InputIterator>::value_type,
  553. typename _Allocator = allocator<_ValT>,
  554. typename = _RequireInputIter<_InputIterator>,
  555. typename = _RequireAllocator<_Allocator>>
  556. deque(_InputIterator, _InputIterator, _Allocator = _Allocator())
  557. -> deque<_ValT, _Allocator>;
  558. #endif
  559. template<typename _Tp, typename _Alloc>
  560. inline bool
  561. operator==(const deque<_Tp, _Alloc>& __lhs,
  562. const deque<_Tp, _Alloc>& __rhs)
  563. { return __lhs._M_base() == __rhs._M_base(); }
  564. #if __cpp_lib_three_way_comparison
  565. template<typename _Tp, typename _Alloc>
  566. constexpr __detail::__synth3way_t<_Tp>
  567. operator<=>(const deque<_Tp, _Alloc>& __x, const deque<_Tp, _Alloc>& __y)
  568. { return __x._M_base() <=> __y._M_base(); }
  569. #else
  570. template<typename _Tp, typename _Alloc>
  571. inline bool
  572. operator!=(const deque<_Tp, _Alloc>& __lhs,
  573. const deque<_Tp, _Alloc>& __rhs)
  574. { return __lhs._M_base() != __rhs._M_base(); }
  575. template<typename _Tp, typename _Alloc>
  576. inline bool
  577. operator<(const deque<_Tp, _Alloc>& __lhs,
  578. const deque<_Tp, _Alloc>& __rhs)
  579. { return __lhs._M_base() < __rhs._M_base(); }
  580. template<typename _Tp, typename _Alloc>
  581. inline bool
  582. operator<=(const deque<_Tp, _Alloc>& __lhs,
  583. const deque<_Tp, _Alloc>& __rhs)
  584. { return __lhs._M_base() <= __rhs._M_base(); }
  585. template<typename _Tp, typename _Alloc>
  586. inline bool
  587. operator>=(const deque<_Tp, _Alloc>& __lhs,
  588. const deque<_Tp, _Alloc>& __rhs)
  589. { return __lhs._M_base() >= __rhs._M_base(); }
  590. template<typename _Tp, typename _Alloc>
  591. inline bool
  592. operator>(const deque<_Tp, _Alloc>& __lhs,
  593. const deque<_Tp, _Alloc>& __rhs)
  594. { return __lhs._M_base() > __rhs._M_base(); }
  595. #endif // three-way comparison
  596. template<typename _Tp, typename _Alloc>
  597. inline void
  598. swap(deque<_Tp, _Alloc>& __lhs, deque<_Tp, _Alloc>& __rhs)
  599. _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))
  600. { __lhs.swap(__rhs); }
  601. } // namespace __debug
  602. } // namespace std
  603. #endif