You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

698 lines
18KB

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