multimap.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. // Debugging multimap 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/multimap.h
  21. * This file is a GNU debug extension to the Standard C++ Library.
  22. */
  23. #ifndef _GLIBCXX_DEBUG_MULTIMAP_H
  24. #define _GLIBCXX_DEBUG_MULTIMAP_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::multimap with safety/checking/debug instrumentation.
  34. template<typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
  35. typename _Allocator = std::allocator<std::pair<const _Key, _Tp> > >
  36. class multimap
  37. : public __gnu_debug::_Safe_container<
  38. multimap<_Key, _Tp, _Compare, _Allocator>, _Allocator,
  39. __gnu_debug::_Safe_node_sequence>,
  40. public _GLIBCXX_STD_C::multimap<_Key, _Tp, _Compare, _Allocator>
  41. {
  42. typedef _GLIBCXX_STD_C::multimap<
  43. _Key, _Tp, _Compare, _Allocator> _Base;
  44. typedef __gnu_debug::_Safe_container<
  45. multimap, _Allocator, __gnu_debug::_Safe_node_sequence> _Safe;
  46. typedef typename _Base::const_iterator _Base_const_iterator;
  47. typedef typename _Base::iterator _Base_iterator;
  48. typedef __gnu_debug::_Equal_to<_Base_const_iterator> _Equal;
  49. public:
  50. // types:
  51. typedef _Key key_type;
  52. typedef _Tp mapped_type;
  53. typedef std::pair<const _Key, _Tp> value_type;
  54. typedef _Compare key_compare;
  55. typedef _Allocator allocator_type;
  56. typedef typename _Base::reference reference;
  57. typedef typename _Base::const_reference const_reference;
  58. typedef __gnu_debug::_Safe_iterator<_Base_iterator, multimap>
  59. iterator;
  60. typedef __gnu_debug::_Safe_iterator<_Base_const_iterator,
  61. multimap> const_iterator;
  62. typedef typename _Base::size_type size_type;
  63. typedef typename _Base::difference_type difference_type;
  64. typedef typename _Base::pointer pointer;
  65. typedef typename _Base::const_pointer const_pointer;
  66. typedef std::reverse_iterator<iterator> reverse_iterator;
  67. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  68. // 23.3.1.1 construct/copy/destroy:
  69. #if __cplusplus < 201103L
  70. multimap() : _Base() { }
  71. multimap(const multimap& __x)
  72. : _Base(__x) { }
  73. ~multimap() { }
  74. #else
  75. multimap() = default;
  76. multimap(const multimap&) = default;
  77. multimap(multimap&&) = default;
  78. multimap(initializer_list<value_type> __l,
  79. const _Compare& __c = _Compare(),
  80. const allocator_type& __a = allocator_type())
  81. : _Base(__l, __c, __a) { }
  82. explicit
  83. multimap(const allocator_type& __a)
  84. : _Base(__a) { }
  85. multimap(const multimap& __m, const allocator_type& __a)
  86. : _Base(__m, __a) { }
  87. multimap(multimap&& __m, const allocator_type& __a)
  88. : _Safe(std::move(__m._M_safe()), __a),
  89. _Base(std::move(__m._M_base()), __a) { }
  90. multimap(initializer_list<value_type> __l, const allocator_type& __a)
  91. : _Base(__l, __a) { }
  92. template<typename _InputIterator>
  93. multimap(_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. ~multimap() = default;
  99. #endif
  100. explicit multimap(const _Compare& __comp,
  101. const _Allocator& __a = _Allocator())
  102. : _Base(__comp, __a) { }
  103. template<typename _InputIterator>
  104. multimap(_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. multimap(const _Base& __x)
  112. : _Base(__x) { }
  113. #if __cplusplus < 201103L
  114. multimap&
  115. operator=(const multimap& __x)
  116. {
  117. this->_M_safe() = __x;
  118. _M_base() = __x;
  119. return *this;
  120. }
  121. #else
  122. multimap&
  123. operator=(const multimap&) = default;
  124. multimap&
  125. operator=(multimap&&) = default;
  126. multimap&
  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)...), this);
  185. }
  186. template<typename... _Args>
  187. iterator
  188. emplace_hint(const_iterator __pos, _Args&&... __args)
  189. {
  190. __glibcxx_check_insert(__pos);
  191. return iterator(_Base::emplace_hint(__pos.base(),
  192. std::forward<_Args>(__args)...),
  193. this);
  194. }
  195. #endif
  196. iterator
  197. insert(const value_type& __x)
  198. { return iterator(_Base::insert(__x), this); }
  199. #if __cplusplus >= 201103L
  200. template<typename _Pair, typename = typename
  201. std::enable_if<std::is_constructible<value_type,
  202. _Pair&&>::value>::type>
  203. iterator
  204. insert(_Pair&& __x)
  205. { return iterator(_Base::insert(std::forward<_Pair>(__x)), this); }
  206. #endif
  207. #if __cplusplus >= 201103L
  208. void
  209. insert(std::initializer_list<value_type> __list)
  210. { _Base::insert(__list); }
  211. #endif
  212. iterator
  213. #if __cplusplus >= 201103L
  214. insert(const_iterator __position, const value_type& __x)
  215. #else
  216. insert(iterator __position, const value_type& __x)
  217. #endif
  218. {
  219. __glibcxx_check_insert(__position);
  220. return iterator(_Base::insert(__position.base(), __x), this);
  221. }
  222. #if __cplusplus >= 201103L
  223. template<typename _Pair, typename = typename
  224. std::enable_if<std::is_constructible<value_type,
  225. _Pair&&>::value>::type>
  226. iterator
  227. insert(const_iterator __position, _Pair&& __x)
  228. {
  229. __glibcxx_check_insert(__position);
  230. return iterator(_Base::insert(__position.base(),
  231. std::forward<_Pair>(__x)), this);
  232. }
  233. #endif
  234. template<typename _InputIterator>
  235. void
  236. insert(_InputIterator __first, _InputIterator __last)
  237. {
  238. __glibcxx_check_valid_range(__first, __last);
  239. _Base::insert(__gnu_debug::__base(__first),
  240. __gnu_debug::__base(__last));
  241. }
  242. #if __cplusplus >= 201103L
  243. iterator
  244. erase(const_iterator __position)
  245. {
  246. __glibcxx_check_erase(__position);
  247. this->_M_invalidate_if(_Equal(__position.base()));
  248. return iterator(_Base::erase(__position.base()), this);
  249. }
  250. iterator
  251. erase(iterator __position)
  252. { return erase(const_iterator(__position)); }
  253. #else
  254. void
  255. erase(iterator __position)
  256. {
  257. __glibcxx_check_erase(__position);
  258. this->_M_invalidate_if(_Equal(__position.base()));
  259. _Base::erase(__position.base());
  260. }
  261. #endif
  262. size_type
  263. erase(const key_type& __x)
  264. {
  265. std::pair<_Base_iterator, _Base_iterator> __victims =
  266. _Base::equal_range(__x);
  267. size_type __count = 0;
  268. _Base_iterator __victim = __victims.first;
  269. while (__victim != __victims.second)
  270. {
  271. this->_M_invalidate_if(_Equal(__victim));
  272. _Base::erase(__victim++);
  273. ++__count;
  274. }
  275. return __count;
  276. }
  277. #if __cplusplus >= 201103L
  278. iterator
  279. erase(const_iterator __first, const_iterator __last)
  280. {
  281. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  282. // 151. can't currently clear() empty container
  283. __glibcxx_check_erase_range(__first, __last);
  284. for (_Base_const_iterator __victim = __first.base();
  285. __victim != __last.base(); ++__victim)
  286. {
  287. _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
  288. _M_message(__gnu_debug::__msg_valid_range)
  289. ._M_iterator(__first, "first")
  290. ._M_iterator(__last, "last"));
  291. this->_M_invalidate_if(_Equal(__victim));
  292. }
  293. return iterator(_Base::erase(__first.base(), __last.base()), this);
  294. }
  295. #else
  296. void
  297. erase(iterator __first, iterator __last)
  298. {
  299. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  300. // 151. can't currently clear() empty container
  301. __glibcxx_check_erase_range(__first, __last);
  302. for (_Base_iterator __victim = __first.base();
  303. __victim != __last.base(); ++__victim)
  304. {
  305. _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
  306. _M_message(__gnu_debug::__msg_valid_range)
  307. ._M_iterator(__first, "first")
  308. ._M_iterator(__last, "last"));
  309. this->_M_invalidate_if(_Equal(__victim));
  310. }
  311. _Base::erase(__first.base(), __last.base());
  312. }
  313. #endif
  314. void
  315. swap(multimap& __x)
  316. #if __cplusplus >= 201103L
  317. noexcept( noexcept(declval<_Base>().swap(__x)) )
  318. #endif
  319. {
  320. _Safe::_M_swap(__x);
  321. _Base::swap(__x);
  322. }
  323. void
  324. clear() _GLIBCXX_NOEXCEPT
  325. {
  326. this->_M_invalidate_all();
  327. _Base::clear();
  328. }
  329. // observers:
  330. using _Base::key_comp;
  331. using _Base::value_comp;
  332. // 23.3.1.3 multimap operations:
  333. iterator
  334. find(const key_type& __x)
  335. { return iterator(_Base::find(__x), this); }
  336. const_iterator
  337. find(const key_type& __x) const
  338. { return const_iterator(_Base::find(__x), this); }
  339. using _Base::count;
  340. iterator
  341. lower_bound(const key_type& __x)
  342. { return iterator(_Base::lower_bound(__x), this); }
  343. const_iterator
  344. lower_bound(const key_type& __x) const
  345. { return const_iterator(_Base::lower_bound(__x), this); }
  346. iterator
  347. upper_bound(const key_type& __x)
  348. { return iterator(_Base::upper_bound(__x), this); }
  349. const_iterator
  350. upper_bound(const key_type& __x) const
  351. { return const_iterator(_Base::upper_bound(__x), this); }
  352. std::pair<iterator,iterator>
  353. equal_range(const key_type& __x)
  354. {
  355. std::pair<_Base_iterator, _Base_iterator> __res =
  356. _Base::equal_range(__x);
  357. return std::make_pair(iterator(__res.first, this),
  358. iterator(__res.second, this));
  359. }
  360. std::pair<const_iterator,const_iterator>
  361. equal_range(const key_type& __x) const
  362. {
  363. std::pair<_Base_const_iterator, _Base_const_iterator> __res =
  364. _Base::equal_range(__x);
  365. return std::make_pair(const_iterator(__res.first, this),
  366. const_iterator(__res.second, this));
  367. }
  368. _Base&
  369. _M_base() _GLIBCXX_NOEXCEPT { return *this; }
  370. const _Base&
  371. _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
  372. };
  373. template<typename _Key, typename _Tp,
  374. typename _Compare, typename _Allocator>
  375. inline bool
  376. operator==(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
  377. const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
  378. { return __lhs._M_base() == __rhs._M_base(); }
  379. template<typename _Key, typename _Tp,
  380. typename _Compare, typename _Allocator>
  381. inline bool
  382. operator!=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
  383. const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
  384. { return __lhs._M_base() != __rhs._M_base(); }
  385. template<typename _Key, typename _Tp,
  386. typename _Compare, typename _Allocator>
  387. inline bool
  388. operator<(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
  389. const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
  390. { return __lhs._M_base() < __rhs._M_base(); }
  391. template<typename _Key, typename _Tp,
  392. typename _Compare, typename _Allocator>
  393. inline bool
  394. operator<=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
  395. const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
  396. { return __lhs._M_base() <= __rhs._M_base(); }
  397. template<typename _Key, typename _Tp,
  398. typename _Compare, typename _Allocator>
  399. inline bool
  400. operator>=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
  401. const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
  402. { return __lhs._M_base() >= __rhs._M_base(); }
  403. template<typename _Key, typename _Tp,
  404. typename _Compare, typename _Allocator>
  405. inline bool
  406. operator>(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
  407. const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
  408. { return __lhs._M_base() > __rhs._M_base(); }
  409. template<typename _Key, typename _Tp,
  410. typename _Compare, typename _Allocator>
  411. inline void
  412. swap(multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
  413. multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
  414. { __lhs.swap(__rhs); }
  415. } // namespace __debug
  416. } // namespace std
  417. #endif