deque 17 KB

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