multiset.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. // Debugging multiset implementation -*- C++ -*-
  2. // Copyright (C) 2003, 2004, 2005, 2006, 2007, 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/multiset.h
  22. * This file is a GNU debug extension to the Standard C++ Library.
  23. */
  24. #ifndef _GLIBCXX_DEBUG_MULTISET_H
  25. #define _GLIBCXX_DEBUG_MULTISET_H 1
  26. #include <debug/safe_sequence.h>
  27. #include <debug/safe_iterator.h>
  28. #include <utility>
  29. namespace std
  30. {
  31. namespace __debug
  32. {
  33. template<typename _Key, typename _Compare = std::less<_Key>,
  34. typename _Allocator = std::allocator<_Key> >
  35. class multiset
  36. : public _GLIBCXX_STD_D::multiset<_Key, _Compare, _Allocator>,
  37. public __gnu_debug::_Safe_sequence<multiset<_Key, _Compare, _Allocator> >
  38. {
  39. typedef _GLIBCXX_STD_D::multiset<_Key, _Compare, _Allocator> _Base;
  40. typedef __gnu_debug::_Safe_sequence<multiset> _Safe_base;
  41. public:
  42. // types:
  43. typedef _Key key_type;
  44. typedef _Key value_type;
  45. typedef _Compare key_compare;
  46. typedef _Compare value_compare;
  47. typedef _Allocator allocator_type;
  48. typedef typename _Base::reference reference;
  49. typedef typename _Base::const_reference const_reference;
  50. typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, multiset>
  51. iterator;
  52. typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
  53. multiset> const_iterator;
  54. typedef typename _Base::size_type size_type;
  55. typedef typename _Base::difference_type difference_type;
  56. typedef typename _Base::pointer pointer;
  57. typedef typename _Base::const_pointer const_pointer;
  58. typedef std::reverse_iterator<iterator> reverse_iterator;
  59. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  60. // 23.3.3.1 construct/copy/destroy:
  61. explicit multiset(const _Compare& __comp = _Compare(),
  62. const _Allocator& __a = _Allocator())
  63. : _Base(__comp, __a) { }
  64. template<typename _InputIterator>
  65. multiset(_InputIterator __first, _InputIterator __last,
  66. const _Compare& __comp = _Compare(),
  67. const _Allocator& __a = _Allocator())
  68. : _Base(__gnu_debug::__check_valid_range(__first, __last), __last,
  69. __comp, __a) { }
  70. multiset(const multiset& __x)
  71. : _Base(__x), _Safe_base() { }
  72. multiset(const _Base& __x)
  73. : _Base(__x), _Safe_base() { }
  74. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  75. multiset(multiset&& __x)
  76. : _Base(std::forward<multiset>(__x)), _Safe_base()
  77. { this->_M_swap(__x); }
  78. multiset(initializer_list<value_type> __l,
  79. const _Compare& __comp = _Compare(),
  80. const allocator_type& __a = allocator_type())
  81. : _Base(__l, __comp, __a), _Safe_base() { }
  82. #endif
  83. ~multiset() { }
  84. multiset&
  85. operator=(const multiset& __x)
  86. {
  87. *static_cast<_Base*>(this) = __x;
  88. this->_M_invalidate_all();
  89. return *this;
  90. }
  91. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  92. multiset&
  93. operator=(multiset&& __x)
  94. {
  95. // NB: DR 675.
  96. clear();
  97. swap(__x);
  98. return *this;
  99. }
  100. multiset&
  101. operator=(initializer_list<value_type> __l)
  102. {
  103. this->clear();
  104. this->insert(__l);
  105. return *this;
  106. }
  107. #endif
  108. using _Base::get_allocator;
  109. // iterators:
  110. iterator
  111. begin()
  112. { return iterator(_Base::begin(), this); }
  113. const_iterator
  114. begin() const
  115. { return const_iterator(_Base::begin(), this); }
  116. iterator
  117. end()
  118. { return iterator(_Base::end(), this); }
  119. const_iterator
  120. end() const
  121. { return const_iterator(_Base::end(), this); }
  122. reverse_iterator
  123. rbegin()
  124. { return reverse_iterator(end()); }
  125. const_reverse_iterator
  126. rbegin() const
  127. { return const_reverse_iterator(end()); }
  128. reverse_iterator
  129. rend()
  130. { return reverse_iterator(begin()); }
  131. const_reverse_iterator
  132. rend() const
  133. { return const_reverse_iterator(begin()); }
  134. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  135. const_iterator
  136. cbegin() const
  137. { return const_iterator(_Base::begin(), this); }
  138. const_iterator
  139. cend() const
  140. { return const_iterator(_Base::end(), this); }
  141. const_reverse_iterator
  142. crbegin() const
  143. { return const_reverse_iterator(end()); }
  144. const_reverse_iterator
  145. crend() const
  146. { return const_reverse_iterator(begin()); }
  147. #endif
  148. // capacity:
  149. using _Base::empty;
  150. using _Base::size;
  151. using _Base::max_size;
  152. // modifiers:
  153. iterator
  154. insert(const value_type& __x)
  155. { return iterator(_Base::insert(__x), this); }
  156. iterator
  157. insert(iterator __position, const value_type& __x)
  158. {
  159. __glibcxx_check_insert(__position);
  160. return iterator(_Base::insert(__position.base(), __x), this);
  161. }
  162. template<typename _InputIterator>
  163. void
  164. insert(_InputIterator __first, _InputIterator __last)
  165. {
  166. __glibcxx_check_valid_range(__first, __last);
  167. _Base::insert(__first, __last);
  168. }
  169. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  170. void
  171. insert(initializer_list<value_type> __l)
  172. { _Base::insert(__l); }
  173. #endif
  174. void
  175. erase(iterator __position)
  176. {
  177. __glibcxx_check_erase(__position);
  178. __position._M_invalidate();
  179. _Base::erase(__position.base());
  180. }
  181. size_type
  182. erase(const key_type& __x)
  183. {
  184. std::pair<iterator, iterator> __victims = this->equal_range(__x);
  185. size_type __count = 0;
  186. while (__victims.first != __victims.second)
  187. {
  188. iterator __victim = __victims.first++;
  189. __victim._M_invalidate();
  190. _Base::erase(__victim.base());
  191. ++__count;
  192. }
  193. return __count;
  194. }
  195. void
  196. erase(iterator __first, iterator __last)
  197. {
  198. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  199. // 151. can't currently clear() empty container
  200. __glibcxx_check_erase_range(__first, __last);
  201. while (__first != __last)
  202. this->erase(__first++);
  203. }
  204. void
  205. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  206. swap(multiset&& __x)
  207. #else
  208. swap(multiset& __x)
  209. #endif
  210. {
  211. _Base::swap(__x);
  212. this->_M_swap(__x);
  213. }
  214. void
  215. clear()
  216. { this->erase(begin(), end()); }
  217. // observers:
  218. using _Base::key_comp;
  219. using _Base::value_comp;
  220. // multiset operations:
  221. iterator
  222. find(const key_type& __x)
  223. { return iterator(_Base::find(__x), this); }
  224. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  225. // 214. set::find() missing const overload
  226. const_iterator
  227. find(const key_type& __x) const
  228. { return const_iterator(_Base::find(__x), this); }
  229. using _Base::count;
  230. iterator
  231. lower_bound(const key_type& __x)
  232. { return iterator(_Base::lower_bound(__x), this); }
  233. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  234. // 214. set::find() missing const overload
  235. const_iterator
  236. lower_bound(const key_type& __x) const
  237. { return const_iterator(_Base::lower_bound(__x), this); }
  238. iterator
  239. upper_bound(const key_type& __x)
  240. { return iterator(_Base::upper_bound(__x), this); }
  241. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  242. // 214. set::find() missing const overload
  243. const_iterator
  244. upper_bound(const key_type& __x) const
  245. { return const_iterator(_Base::upper_bound(__x), this); }
  246. std::pair<iterator,iterator>
  247. equal_range(const key_type& __x)
  248. {
  249. typedef typename _Base::iterator _Base_iterator;
  250. std::pair<_Base_iterator, _Base_iterator> __res =
  251. _Base::equal_range(__x);
  252. return std::make_pair(iterator(__res.first, this),
  253. iterator(__res.second, this));
  254. }
  255. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  256. // 214. set::find() missing const overload
  257. std::pair<const_iterator,const_iterator>
  258. equal_range(const key_type& __x) const
  259. {
  260. typedef typename _Base::const_iterator _Base_iterator;
  261. std::pair<_Base_iterator, _Base_iterator> __res =
  262. _Base::equal_range(__x);
  263. return std::make_pair(const_iterator(__res.first, this),
  264. const_iterator(__res.second, this));
  265. }
  266. _Base&
  267. _M_base() { return *this; }
  268. const _Base&
  269. _M_base() const { return *this; }
  270. private:
  271. void
  272. _M_invalidate_all()
  273. {
  274. typedef typename _Base::const_iterator _Base_const_iterator;
  275. typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
  276. this->_M_invalidate_if(_Not_equal(_M_base().end()));
  277. }
  278. };
  279. template<typename _Key, typename _Compare, typename _Allocator>
  280. inline bool
  281. operator==(const multiset<_Key, _Compare, _Allocator>& __lhs,
  282. const multiset<_Key, _Compare, _Allocator>& __rhs)
  283. { return __lhs._M_base() == __rhs._M_base(); }
  284. template<typename _Key, typename _Compare, typename _Allocator>
  285. inline bool
  286. operator!=(const multiset<_Key, _Compare, _Allocator>& __lhs,
  287. const multiset<_Key, _Compare, _Allocator>& __rhs)
  288. { return __lhs._M_base() != __rhs._M_base(); }
  289. template<typename _Key, typename _Compare, typename _Allocator>
  290. inline bool
  291. operator<(const multiset<_Key, _Compare, _Allocator>& __lhs,
  292. const multiset<_Key, _Compare, _Allocator>& __rhs)
  293. { return __lhs._M_base() < __rhs._M_base(); }
  294. template<typename _Key, typename _Compare, typename _Allocator>
  295. inline bool
  296. operator<=(const multiset<_Key, _Compare, _Allocator>& __lhs,
  297. const multiset<_Key, _Compare, _Allocator>& __rhs)
  298. { return __lhs._M_base() <= __rhs._M_base(); }
  299. template<typename _Key, typename _Compare, typename _Allocator>
  300. inline bool
  301. operator>=(const multiset<_Key, _Compare, _Allocator>& __lhs,
  302. const multiset<_Key, _Compare, _Allocator>& __rhs)
  303. { return __lhs._M_base() >= __rhs._M_base(); }
  304. template<typename _Key, typename _Compare, typename _Allocator>
  305. inline bool
  306. operator>(const multiset<_Key, _Compare, _Allocator>& __lhs,
  307. const multiset<_Key, _Compare, _Allocator>& __rhs)
  308. { return __lhs._M_base() > __rhs._M_base(); }
  309. template<typename _Key, typename _Compare, typename _Allocator>
  310. void
  311. swap(multiset<_Key, _Compare, _Allocator>& __x,
  312. multiset<_Key, _Compare, _Allocator>& __y)
  313. { return __x.swap(__y); }
  314. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  315. template<typename _Key, typename _Compare, typename _Allocator>
  316. void
  317. swap(multiset<_Key, _Compare, _Allocator>&& __x,
  318. multiset<_Key, _Compare, _Allocator>& __y)
  319. { return __x.swap(__y); }
  320. template<typename _Key, typename _Compare, typename _Allocator>
  321. void
  322. swap(multiset<_Key, _Compare, _Allocator>& __x,
  323. multiset<_Key, _Compare, _Allocator>&& __y)
  324. { return __x.swap(__y); }
  325. #endif
  326. } // namespace __debug
  327. } // namespace std
  328. #endif