multiset.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. // Debugging multiset implementation -*- C++ -*-
  2. // Copyright (C) 2003-2015 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/multiset.h
  21. * This file is a GNU debug extension to the Standard C++ Library.
  22. */
  23. #ifndef _GLIBCXX_DEBUG_MULTISET_H
  24. #define _GLIBCXX_DEBUG_MULTISET_H 1
  25. #include <debug/safe_sequence.h>
  26. #include <debug/safe_container.h>
  27. #include <debug/safe_iterator.h>
  28. #include <utility>
  29. namespace std _GLIBCXX_VISIBILITY(default)
  30. {
  31. namespace __debug
  32. {
  33. /// Class std::multiset with safety/checking/debug instrumentation.
  34. template<typename _Key, typename _Compare = std::less<_Key>,
  35. typename _Allocator = std::allocator<_Key> >
  36. class multiset
  37. : public __gnu_debug::_Safe_container<
  38. multiset<_Key, _Compare, _Allocator>, _Allocator,
  39. __gnu_debug::_Safe_node_sequence>,
  40. public _GLIBCXX_STD_C::multiset<_Key, _Compare, _Allocator>
  41. {
  42. typedef _GLIBCXX_STD_C::multiset<_Key, _Compare, _Allocator> _Base;
  43. typedef __gnu_debug::_Safe_container<
  44. multiset, _Allocator, __gnu_debug::_Safe_node_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. // types:
  50. typedef _Key key_type;
  51. typedef _Key value_type;
  52. typedef _Compare key_compare;
  53. typedef _Compare value_compare;
  54. typedef _Allocator allocator_type;
  55. typedef typename _Base::reference reference;
  56. typedef typename _Base::const_reference const_reference;
  57. typedef __gnu_debug::_Safe_iterator<_Base_iterator, multiset>
  58. iterator;
  59. typedef __gnu_debug::_Safe_iterator<_Base_const_iterator,
  60. multiset> const_iterator;
  61. typedef typename _Base::size_type size_type;
  62. typedef typename _Base::difference_type difference_type;
  63. typedef typename _Base::pointer pointer;
  64. typedef typename _Base::const_pointer const_pointer;
  65. typedef std::reverse_iterator<iterator> reverse_iterator;
  66. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  67. // 23.3.3.1 construct/copy/destroy:
  68. #if __cplusplus < 201103L
  69. multiset() : _Base() { }
  70. multiset(const multiset& __x)
  71. : _Base(__x) { }
  72. ~multiset() { }
  73. #else
  74. multiset() = default;
  75. multiset(const multiset&) = default;
  76. multiset(multiset&&) = default;
  77. multiset(initializer_list<value_type> __l,
  78. const _Compare& __comp = _Compare(),
  79. const allocator_type& __a = allocator_type())
  80. : _Base(__l, __comp, __a) { }
  81. explicit
  82. multiset(const allocator_type& __a)
  83. : _Base(__a) { }
  84. multiset(const multiset& __m, const allocator_type& __a)
  85. : _Base(__m, __a) { }
  86. multiset(multiset&& __m, const allocator_type& __a)
  87. : _Safe(std::move(__m._M_safe()), __a),
  88. _Base(std::move(__m._M_base()), __a) { }
  89. multiset(initializer_list<value_type> __l, const allocator_type& __a)
  90. : _Base(__l, __a)
  91. { }
  92. template<typename _InputIterator>
  93. multiset(_InputIterator __first, _InputIterator __last,
  94. const allocator_type& __a)
  95. : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
  96. __last)),
  97. __gnu_debug::__base(__last), __a) { }
  98. ~multiset() = default;
  99. #endif
  100. explicit multiset(const _Compare& __comp,
  101. const _Allocator& __a = _Allocator())
  102. : _Base(__comp, __a) { }
  103. template<typename _InputIterator>
  104. multiset(_InputIterator __first, _InputIterator __last,
  105. const _Compare& __comp = _Compare(),
  106. const _Allocator& __a = _Allocator())
  107. : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
  108. __last)),
  109. __gnu_debug::__base(__last),
  110. __comp, __a) { }
  111. multiset(const _Base& __x)
  112. : _Base(__x) { }
  113. #if __cplusplus < 201103L
  114. multiset&
  115. operator=(const multiset& __x)
  116. {
  117. this->_M_safe() = __x;
  118. _M_base() = __x;
  119. return *this;
  120. }
  121. #else
  122. multiset&
  123. operator=(const multiset&) = default;
  124. multiset&
  125. operator=(multiset&&) = default;
  126. multiset&
  127. operator=(initializer_list<value_type> __l)
  128. {
  129. _M_base() = __l;
  130. this->_M_invalidate_all();
  131. return *this;
  132. }
  133. #endif
  134. using _Base::get_allocator;
  135. // iterators:
  136. iterator
  137. begin() _GLIBCXX_NOEXCEPT
  138. { return iterator(_Base::begin(), this); }
  139. const_iterator
  140. begin() const _GLIBCXX_NOEXCEPT
  141. { return const_iterator(_Base::begin(), this); }
  142. iterator
  143. end() _GLIBCXX_NOEXCEPT
  144. { return iterator(_Base::end(), this); }
  145. const_iterator
  146. end() const _GLIBCXX_NOEXCEPT
  147. { return const_iterator(_Base::end(), this); }
  148. reverse_iterator
  149. rbegin() _GLIBCXX_NOEXCEPT
  150. { return reverse_iterator(end()); }
  151. const_reverse_iterator
  152. rbegin() const _GLIBCXX_NOEXCEPT
  153. { return const_reverse_iterator(end()); }
  154. reverse_iterator
  155. rend() _GLIBCXX_NOEXCEPT
  156. { return reverse_iterator(begin()); }
  157. const_reverse_iterator
  158. rend() const _GLIBCXX_NOEXCEPT
  159. { return const_reverse_iterator(begin()); }
  160. #if __cplusplus >= 201103L
  161. const_iterator
  162. cbegin() const noexcept
  163. { return const_iterator(_Base::begin(), this); }
  164. const_iterator
  165. cend() const noexcept
  166. { return const_iterator(_Base::end(), this); }
  167. const_reverse_iterator
  168. crbegin() const noexcept
  169. { return const_reverse_iterator(end()); }
  170. const_reverse_iterator
  171. crend() const noexcept
  172. { return const_reverse_iterator(begin()); }
  173. #endif
  174. // capacity:
  175. using _Base::empty;
  176. using _Base::size;
  177. using _Base::max_size;
  178. // modifiers:
  179. #if __cplusplus >= 201103L
  180. template<typename... _Args>
  181. iterator
  182. emplace(_Args&&... __args)
  183. {
  184. return iterator(_Base::emplace(std::forward<_Args>(__args)...),
  185. this);
  186. }
  187. template<typename... _Args>
  188. iterator
  189. emplace_hint(const_iterator __pos, _Args&&... __args)
  190. {
  191. __glibcxx_check_insert(__pos);
  192. return iterator(_Base::emplace_hint(__pos.base(),
  193. std::forward<_Args>(__args)...),
  194. this);
  195. }
  196. #endif
  197. iterator
  198. insert(const value_type& __x)
  199. { return iterator(_Base::insert(__x), this); }
  200. #if __cplusplus >= 201103L
  201. iterator
  202. insert(value_type&& __x)
  203. { return iterator(_Base::insert(std::move(__x)), this); }
  204. #endif
  205. iterator
  206. insert(const_iterator __position, const value_type& __x)
  207. {
  208. __glibcxx_check_insert(__position);
  209. return iterator(_Base::insert(__position.base(), __x), this);
  210. }
  211. #if __cplusplus >= 201103L
  212. iterator
  213. insert(const_iterator __position, value_type&& __x)
  214. {
  215. __glibcxx_check_insert(__position);
  216. return iterator(_Base::insert(__position.base(), std::move(__x)),
  217. this);
  218. }
  219. #endif
  220. template<typename _InputIterator>
  221. void
  222. insert(_InputIterator __first, _InputIterator __last)
  223. {
  224. __glibcxx_check_valid_range(__first, __last);
  225. _Base::insert(__gnu_debug::__base(__first),
  226. __gnu_debug::__base(__last));
  227. }
  228. #if __cplusplus >= 201103L
  229. void
  230. insert(initializer_list<value_type> __l)
  231. { _Base::insert(__l); }
  232. #endif
  233. #if __cplusplus >= 201103L
  234. iterator
  235. erase(const_iterator __position)
  236. {
  237. __glibcxx_check_erase(__position);
  238. this->_M_invalidate_if(_Equal(__position.base()));
  239. return iterator(_Base::erase(__position.base()), this);
  240. }
  241. #else
  242. void
  243. erase(iterator __position)
  244. {
  245. __glibcxx_check_erase(__position);
  246. this->_M_invalidate_if(_Equal(__position.base()));
  247. _Base::erase(__position.base());
  248. }
  249. #endif
  250. size_type
  251. erase(const key_type& __x)
  252. {
  253. std::pair<_Base_iterator, _Base_iterator> __victims =
  254. _Base::equal_range(__x);
  255. size_type __count = 0;
  256. _Base_iterator __victim = __victims.first;
  257. while (__victim != __victims.second)
  258. {
  259. this->_M_invalidate_if(_Equal(__victim));
  260. _Base::erase(__victim++);
  261. ++__count;
  262. }
  263. return __count;
  264. }
  265. #if __cplusplus >= 201103L
  266. iterator
  267. erase(const_iterator __first, const_iterator __last)
  268. {
  269. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  270. // 151. can't currently clear() empty container
  271. __glibcxx_check_erase_range(__first, __last);
  272. for (_Base_const_iterator __victim = __first.base();
  273. __victim != __last.base(); ++__victim)
  274. {
  275. _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
  276. _M_message(__gnu_debug::__msg_valid_range)
  277. ._M_iterator(__first, "first")
  278. ._M_iterator(__last, "last"));
  279. this->_M_invalidate_if(_Equal(__victim));
  280. }
  281. return iterator(_Base::erase(__first.base(), __last.base()), this);
  282. }
  283. #else
  284. void
  285. erase(iterator __first, iterator __last)
  286. {
  287. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  288. // 151. can't currently clear() empty container
  289. __glibcxx_check_erase_range(__first, __last);
  290. for (_Base_iterator __victim = __first.base();
  291. __victim != __last.base(); ++__victim)
  292. {
  293. _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
  294. _M_message(__gnu_debug::__msg_valid_range)
  295. ._M_iterator(__first, "first")
  296. ._M_iterator(__last, "last"));
  297. this->_M_invalidate_if(_Equal(__victim));
  298. }
  299. _Base::erase(__first.base(), __last.base());
  300. }
  301. #endif
  302. void
  303. swap(multiset& __x)
  304. #if __cplusplus >= 201103L
  305. noexcept( noexcept(declval<_Base>().swap(__x)) )
  306. #endif
  307. {
  308. _Safe::_M_swap(__x);
  309. _Base::swap(__x);
  310. }
  311. void
  312. clear() _GLIBCXX_NOEXCEPT
  313. {
  314. this->_M_invalidate_all();
  315. _Base::clear();
  316. }
  317. // observers:
  318. using _Base::key_comp;
  319. using _Base::value_comp;
  320. // multiset operations:
  321. iterator
  322. find(const key_type& __x)
  323. { return iterator(_Base::find(__x), this); }
  324. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  325. // 214. set::find() missing const overload
  326. const_iterator
  327. find(const key_type& __x) const
  328. { return const_iterator(_Base::find(__x), this); }
  329. using _Base::count;
  330. iterator
  331. lower_bound(const key_type& __x)
  332. { return iterator(_Base::lower_bound(__x), this); }
  333. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  334. // 214. set::find() missing const overload
  335. const_iterator
  336. lower_bound(const key_type& __x) const
  337. { return const_iterator(_Base::lower_bound(__x), this); }
  338. iterator
  339. upper_bound(const key_type& __x)
  340. { return iterator(_Base::upper_bound(__x), this); }
  341. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  342. // 214. set::find() missing const overload
  343. const_iterator
  344. upper_bound(const key_type& __x) const
  345. { return const_iterator(_Base::upper_bound(__x), this); }
  346. std::pair<iterator,iterator>
  347. equal_range(const key_type& __x)
  348. {
  349. std::pair<_Base_iterator, _Base_iterator> __res =
  350. _Base::equal_range(__x);
  351. return std::make_pair(iterator(__res.first, this),
  352. iterator(__res.second, this));
  353. }
  354. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  355. // 214. set::find() missing const overload
  356. std::pair<const_iterator,const_iterator>
  357. equal_range(const key_type& __x) const
  358. {
  359. std::pair<_Base_const_iterator, _Base_const_iterator> __res =
  360. _Base::equal_range(__x);
  361. return std::make_pair(const_iterator(__res.first, this),
  362. const_iterator(__res.second, this));
  363. }
  364. _Base&
  365. _M_base() _GLIBCXX_NOEXCEPT { return *this; }
  366. const _Base&
  367. _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
  368. };
  369. template<typename _Key, typename _Compare, typename _Allocator>
  370. inline bool
  371. operator==(const multiset<_Key, _Compare, _Allocator>& __lhs,
  372. const multiset<_Key, _Compare, _Allocator>& __rhs)
  373. { return __lhs._M_base() == __rhs._M_base(); }
  374. template<typename _Key, typename _Compare, typename _Allocator>
  375. inline bool
  376. operator!=(const multiset<_Key, _Compare, _Allocator>& __lhs,
  377. const multiset<_Key, _Compare, _Allocator>& __rhs)
  378. { return __lhs._M_base() != __rhs._M_base(); }
  379. template<typename _Key, typename _Compare, typename _Allocator>
  380. inline bool
  381. operator<(const multiset<_Key, _Compare, _Allocator>& __lhs,
  382. const multiset<_Key, _Compare, _Allocator>& __rhs)
  383. { return __lhs._M_base() < __rhs._M_base(); }
  384. template<typename _Key, typename _Compare, typename _Allocator>
  385. inline bool
  386. operator<=(const multiset<_Key, _Compare, _Allocator>& __lhs,
  387. const multiset<_Key, _Compare, _Allocator>& __rhs)
  388. { return __lhs._M_base() <= __rhs._M_base(); }
  389. template<typename _Key, typename _Compare, typename _Allocator>
  390. inline bool
  391. operator>=(const multiset<_Key, _Compare, _Allocator>& __lhs,
  392. const multiset<_Key, _Compare, _Allocator>& __rhs)
  393. { return __lhs._M_base() >= __rhs._M_base(); }
  394. template<typename _Key, typename _Compare, typename _Allocator>
  395. inline bool
  396. operator>(const multiset<_Key, _Compare, _Allocator>& __lhs,
  397. const multiset<_Key, _Compare, _Allocator>& __rhs)
  398. { return __lhs._M_base() > __rhs._M_base(); }
  399. template<typename _Key, typename _Compare, typename _Allocator>
  400. void
  401. swap(multiset<_Key, _Compare, _Allocator>& __x,
  402. multiset<_Key, _Compare, _Allocator>& __y)
  403. { return __x.swap(__y); }
  404. } // namespace __debug
  405. } // namespace std
  406. #endif