multimap.h 12 KB

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