map.h 12 KB

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