deque 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. // Debugging deque implementation -*- C++ -*-
  2. // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
  3. // Free Software Foundation, Inc.
  4. //
  5. // This file is part of the GNU ISO C++ Library. This library is free
  6. // software; you can redistribute it and/or modify it under the
  7. // terms of the GNU General Public License as published by the
  8. // Free Software Foundation; either version 3, or (at your option)
  9. // any later version.
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. // Under Section 7 of GPL version 3, you are granted additional
  15. // permissions described in the GCC Runtime Library Exception, version
  16. // 3.1, as published by the Free Software Foundation.
  17. // You should have received a copy of the GNU General Public License and
  18. // a copy of the GCC Runtime Library Exception along with this program;
  19. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. // <http://www.gnu.org/licenses/>.
  21. /** @file debug/deque
  22. * This file is a GNU debug extension to the Standard C++ Library.
  23. */
  24. #ifndef _GLIBCXX_DEBUG_DEQUE
  25. #define _GLIBCXX_DEBUG_DEQUE 1
  26. #include <deque>
  27. #include <debug/safe_sequence.h>
  28. #include <debug/safe_iterator.h>
  29. namespace std
  30. {
  31. namespace __debug
  32. {
  33. template<typename _Tp, typename _Allocator = std::allocator<_Tp> >
  34. class deque
  35. : public _GLIBCXX_STD_D::deque<_Tp, _Allocator>,
  36. public __gnu_debug::_Safe_sequence<deque<_Tp, _Allocator> >
  37. {
  38. typedef _GLIBCXX_STD_D::deque<_Tp, _Allocator> _Base;
  39. typedef __gnu_debug::_Safe_sequence<deque> _Safe_base;
  40. public:
  41. typedef typename _Base::reference reference;
  42. typedef typename _Base::const_reference const_reference;
  43. typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,deque>
  44. iterator;
  45. typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,deque>
  46. const_iterator;
  47. typedef typename _Base::size_type size_type;
  48. typedef typename _Base::difference_type difference_type;
  49. typedef _Tp value_type;
  50. typedef _Allocator allocator_type;
  51. typedef typename _Base::pointer pointer;
  52. typedef typename _Base::const_pointer const_pointer;
  53. typedef std::reverse_iterator<iterator> reverse_iterator;
  54. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  55. // 23.2.1.1 construct/copy/destroy:
  56. explicit deque(const _Allocator& __a = _Allocator())
  57. : _Base(__a) { }
  58. explicit deque(size_type __n, const _Tp& __value = _Tp(),
  59. const _Allocator& __a = _Allocator())
  60. : _Base(__n, __value, __a) { }
  61. template<class _InputIterator>
  62. deque(_InputIterator __first, _InputIterator __last,
  63. const _Allocator& __a = _Allocator())
  64. : _Base(__gnu_debug::__check_valid_range(__first, __last), __last, __a)
  65. { }
  66. deque(const deque& __x)
  67. : _Base(__x), _Safe_base() { }
  68. deque(const _Base& __x)
  69. : _Base(__x), _Safe_base() { }
  70. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  71. deque(deque&& __x)
  72. : _Base(std::forward<deque>(__x)), _Safe_base()
  73. { this->_M_swap(__x); }
  74. deque(initializer_list<value_type> __l,
  75. const allocator_type& __a = allocator_type())
  76. : _Base(__l, __a), _Safe_base() { }
  77. #endif
  78. ~deque() { }
  79. deque&
  80. operator=(const deque& __x)
  81. {
  82. *static_cast<_Base*>(this) = __x;
  83. this->_M_invalidate_all();
  84. return *this;
  85. }
  86. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  87. deque&
  88. operator=(deque&& __x)
  89. {
  90. // NB: DR 675.
  91. clear();
  92. swap(__x);
  93. return *this;
  94. }
  95. deque&
  96. operator=(initializer_list<value_type> __l)
  97. {
  98. *static_cast<_Base*>(this) = __l;
  99. this->_M_invalidate_all();
  100. return *this;
  101. }
  102. #endif
  103. template<class _InputIterator>
  104. void
  105. assign(_InputIterator __first, _InputIterator __last)
  106. {
  107. __glibcxx_check_valid_range(__first, __last);
  108. _Base::assign(__first, __last);
  109. this->_M_invalidate_all();
  110. }
  111. void
  112. assign(size_type __n, const _Tp& __t)
  113. {
  114. _Base::assign(__n, __t);
  115. this->_M_invalidate_all();
  116. }
  117. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  118. void
  119. assign(initializer_list<value_type> __l)
  120. {
  121. _Base::assign(__l);
  122. this->_M_invalidate_all();
  123. }
  124. #endif
  125. using _Base::get_allocator;
  126. // iterators:
  127. iterator
  128. begin()
  129. { return iterator(_Base::begin(), this); }
  130. const_iterator
  131. begin() const
  132. { return const_iterator(_Base::begin(), this); }
  133. iterator
  134. end()
  135. { return iterator(_Base::end(), this); }
  136. const_iterator
  137. end() const
  138. { return const_iterator(_Base::end(), this); }
  139. reverse_iterator
  140. rbegin()
  141. { return reverse_iterator(end()); }
  142. const_reverse_iterator
  143. rbegin() const
  144. { return const_reverse_iterator(end()); }
  145. reverse_iterator
  146. rend()
  147. { return reverse_iterator(begin()); }
  148. const_reverse_iterator
  149. rend() const
  150. { return const_reverse_iterator(begin()); }
  151. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  152. const_iterator
  153. cbegin() const
  154. { return const_iterator(_Base::begin(), this); }
  155. const_iterator
  156. cend() const
  157. { return const_iterator(_Base::end(), this); }
  158. const_reverse_iterator
  159. crbegin() const
  160. { return const_reverse_iterator(end()); }
  161. const_reverse_iterator
  162. crend() const
  163. { return const_reverse_iterator(begin()); }
  164. #endif
  165. // 23.2.1.2 capacity:
  166. using _Base::size;
  167. using _Base::max_size;
  168. void
  169. resize(size_type __sz, _Tp __c = _Tp())
  170. {
  171. typedef typename _Base::const_iterator _Base_const_iterator;
  172. typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
  173. bool __invalidate_all = __sz > this->size();
  174. if (__sz < this->size())
  175. this->_M_invalidate_if(_After_nth(__sz, _M_base().begin()));
  176. _Base::resize(__sz, __c);
  177. if (__invalidate_all)
  178. this->_M_invalidate_all();
  179. }
  180. using _Base::empty;
  181. // element access:
  182. reference
  183. operator[](size_type __n)
  184. {
  185. __glibcxx_check_subscript(__n);
  186. return _M_base()[__n];
  187. }
  188. const_reference
  189. operator[](size_type __n) const
  190. {
  191. __glibcxx_check_subscript(__n);
  192. return _M_base()[__n];
  193. }
  194. using _Base::at;
  195. reference
  196. front()
  197. {
  198. __glibcxx_check_nonempty();
  199. return _Base::front();
  200. }
  201. const_reference
  202. front() const
  203. {
  204. __glibcxx_check_nonempty();
  205. return _Base::front();
  206. }
  207. reference
  208. back()
  209. {
  210. __glibcxx_check_nonempty();
  211. return _Base::back();
  212. }
  213. const_reference
  214. back() const
  215. {
  216. __glibcxx_check_nonempty();
  217. return _Base::back();
  218. }
  219. // 23.2.1.3 modifiers:
  220. void
  221. push_front(const _Tp& __x)
  222. {
  223. _Base::push_front(__x);
  224. this->_M_invalidate_all();
  225. }
  226. void
  227. push_back(const _Tp& __x)
  228. {
  229. _Base::push_back(__x);
  230. this->_M_invalidate_all();
  231. }
  232. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  233. void
  234. push_front(_Tp&& __x)
  235. { emplace_front(std::move(__x)); }
  236. void
  237. push_back(_Tp&& __x)
  238. { emplace_back(std::move(__x)); }
  239. template<typename... _Args>
  240. void
  241. emplace_front(_Args&&... __args)
  242. {
  243. _Base::emplace_front(std::forward<_Args>(__args)...);
  244. this->_M_invalidate_all();
  245. }
  246. template<typename... _Args>
  247. void
  248. emplace_back(_Args&&... __args)
  249. {
  250. _Base::emplace_back(std::forward<_Args>(__args)...);
  251. this->_M_invalidate_all();
  252. }
  253. template<typename... _Args>
  254. iterator
  255. emplace(iterator __position, _Args&&... __args)
  256. {
  257. __glibcxx_check_insert(__position);
  258. typename _Base::iterator __res = _Base::emplace(__position.base(),
  259. std::forward<_Args>(__args)...);
  260. this->_M_invalidate_all();
  261. return iterator(__res, this);
  262. }
  263. #endif
  264. iterator
  265. insert(iterator __position, const _Tp& __x)
  266. {
  267. __glibcxx_check_insert(__position);
  268. typename _Base::iterator __res = _Base::insert(__position.base(), __x);
  269. this->_M_invalidate_all();
  270. return iterator(__res, this);
  271. }
  272. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  273. iterator
  274. insert(iterator __position, _Tp&& __x)
  275. { return emplace(__position, std::move(__x)); }
  276. void
  277. insert(iterator __p, initializer_list<value_type> __l)
  278. {
  279. _Base::insert(__p, __l);
  280. this->_M_invalidate_all();
  281. }
  282. #endif
  283. void
  284. insert(iterator __position, size_type __n, const _Tp& __x)
  285. {
  286. __glibcxx_check_insert(__position);
  287. _Base::insert(__position.base(), __n, __x);
  288. this->_M_invalidate_all();
  289. }
  290. template<class _InputIterator>
  291. void
  292. insert(iterator __position,
  293. _InputIterator __first, _InputIterator __last)
  294. {
  295. __glibcxx_check_insert_range(__position, __first, __last);
  296. _Base::insert(__position.base(), __first, __last);
  297. this->_M_invalidate_all();
  298. }
  299. void
  300. pop_front()
  301. {
  302. __glibcxx_check_nonempty();
  303. iterator __victim = begin();
  304. __victim._M_invalidate();
  305. _Base::pop_front();
  306. }
  307. void
  308. pop_back()
  309. {
  310. __glibcxx_check_nonempty();
  311. iterator __victim = end();
  312. --__victim;
  313. __victim._M_invalidate();
  314. _Base::pop_back();
  315. }
  316. iterator
  317. erase(iterator __position)
  318. {
  319. __glibcxx_check_erase(__position);
  320. if (__position == begin() || __position == end()-1)
  321. {
  322. __position._M_invalidate();
  323. return iterator(_Base::erase(__position.base()), this);
  324. }
  325. else
  326. {
  327. typename _Base::iterator __res = _Base::erase(__position.base());
  328. this->_M_invalidate_all();
  329. return iterator(__res, this);
  330. }
  331. }
  332. iterator
  333. erase(iterator __first, iterator __last)
  334. {
  335. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  336. // 151. can't currently clear() empty container
  337. __glibcxx_check_erase_range(__first, __last);
  338. if (__first == begin() || __last == end())
  339. {
  340. this->_M_detach_singular();
  341. for (iterator __position = __first; __position != __last; )
  342. {
  343. iterator __victim = __position++;
  344. __victim._M_invalidate();
  345. }
  346. __try
  347. {
  348. return iterator(_Base::erase(__first.base(), __last.base()),
  349. this);
  350. }
  351. __catch(...)
  352. {
  353. this->_M_revalidate_singular();
  354. __throw_exception_again;
  355. }
  356. }
  357. else
  358. {
  359. typename _Base::iterator __res = _Base::erase(__first.base(),
  360. __last.base());
  361. this->_M_invalidate_all();
  362. return iterator(__res, this);
  363. }
  364. }
  365. void
  366. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  367. swap(deque&& __x)
  368. #else
  369. swap(deque& __x)
  370. #endif
  371. {
  372. _Base::swap(__x);
  373. this->_M_swap(__x);
  374. }
  375. void
  376. clear()
  377. {
  378. _Base::clear();
  379. this->_M_invalidate_all();
  380. }
  381. _Base&
  382. _M_base() { return *this; }
  383. const _Base&
  384. _M_base() const { return *this; }
  385. };
  386. template<typename _Tp, typename _Alloc>
  387. inline bool
  388. operator==(const deque<_Tp, _Alloc>& __lhs,
  389. const deque<_Tp, _Alloc>& __rhs)
  390. { return __lhs._M_base() == __rhs._M_base(); }
  391. template<typename _Tp, typename _Alloc>
  392. inline bool
  393. operator!=(const deque<_Tp, _Alloc>& __lhs,
  394. const deque<_Tp, _Alloc>& __rhs)
  395. { return __lhs._M_base() != __rhs._M_base(); }
  396. template<typename _Tp, typename _Alloc>
  397. inline bool
  398. operator<(const deque<_Tp, _Alloc>& __lhs,
  399. const deque<_Tp, _Alloc>& __rhs)
  400. { return __lhs._M_base() < __rhs._M_base(); }
  401. template<typename _Tp, typename _Alloc>
  402. inline bool
  403. operator<=(const deque<_Tp, _Alloc>& __lhs,
  404. const deque<_Tp, _Alloc>& __rhs)
  405. { return __lhs._M_base() <= __rhs._M_base(); }
  406. template<typename _Tp, typename _Alloc>
  407. inline bool
  408. operator>=(const deque<_Tp, _Alloc>& __lhs,
  409. const deque<_Tp, _Alloc>& __rhs)
  410. { return __lhs._M_base() >= __rhs._M_base(); }
  411. template<typename _Tp, typename _Alloc>
  412. inline bool
  413. operator>(const deque<_Tp, _Alloc>& __lhs,
  414. const deque<_Tp, _Alloc>& __rhs)
  415. { return __lhs._M_base() > __rhs._M_base(); }
  416. template<typename _Tp, typename _Alloc>
  417. inline void
  418. swap(deque<_Tp, _Alloc>& __lhs, deque<_Tp, _Alloc>& __rhs)
  419. { __lhs.swap(__rhs); }
  420. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  421. template<typename _Tp, typename _Alloc>
  422. inline void
  423. swap(deque<_Tp, _Alloc>&& __lhs, deque<_Tp, _Alloc>& __rhs)
  424. { __lhs.swap(__rhs); }
  425. template<typename _Tp, typename _Alloc>
  426. inline void
  427. swap(deque<_Tp, _Alloc>& __lhs, deque<_Tp, _Alloc>&& __rhs)
  428. { __lhs.swap(__rhs); }
  429. #endif
  430. } // namespace __debug
  431. } // namespace std
  432. #endif