map.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. // Debugging map 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/map.h
  21. * This file is a GNU debug extension to the Standard C++ Library.
  22. */
  23. #ifndef _GLIBCXX_DEBUG_MAP_H
  24. #define _GLIBCXX_DEBUG_MAP_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::map 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 map
  37. : public __gnu_debug::_Safe_container<
  38. map<_Key, _Tp, _Compare, _Allocator>, _Allocator,
  39. __gnu_debug::_Safe_node_sequence>,
  40. public _GLIBCXX_STD_C::map<_Key, _Tp, _Compare, _Allocator>
  41. {
  42. typedef _GLIBCXX_STD_C::map<
  43. _Key, _Tp, _Compare, _Allocator> _Base;
  44. typedef __gnu_debug::_Safe_container<
  45. map, _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, map>
  59. iterator;
  60. typedef __gnu_debug::_Safe_iterator<_Base_const_iterator, map>
  61. 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. map() : _Base() { }
  71. map(const map& __x)
  72. : _Base(__x) { }
  73. ~map() { }
  74. #else
  75. map() = default;
  76. map(const map&) = default;
  77. map(map&&) = default;
  78. map(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. map(const allocator_type& __a)
  84. : _Base(__a) { }
  85. map(const map& __m, const allocator_type& __a)
  86. : _Base(__m, __a) { }
  87. map(map&& __m, const allocator_type& __a)
  88. : _Safe(std::move(__m._M_safe()), __a),
  89. _Base(std::move(__m._M_base()), __a) { }
  90. map(initializer_list<value_type> __l, const allocator_type& __a)
  91. : _Base(__l, __a) { }
  92. template<typename _InputIterator>
  93. map(_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. { }
  99. ~map() = default;
  100. #endif
  101. map(const _Base& __x)
  102. : _Base(__x) { }
  103. explicit map(const _Compare& __comp,
  104. const _Allocator& __a = _Allocator())
  105. : _Base(__comp, __a) { }
  106. template<typename _InputIterator>
  107. map(_InputIterator __first, _InputIterator __last,
  108. const _Compare& __comp = _Compare(),
  109. const _Allocator& __a = _Allocator())
  110. : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
  111. __last)),
  112. __gnu_debug::__base(__last),
  113. __comp, __a) { }
  114. #if __cplusplus < 201103L
  115. map&
  116. operator=(const map& __x)
  117. {
  118. this->_M_safe() = __x;
  119. _M_base() = __x;
  120. return *this;
  121. }
  122. #else
  123. map&
  124. operator=(const map&) = default;
  125. map&
  126. operator=(map&&) = default;
  127. map&
  128. operator=(initializer_list<value_type> __l)
  129. {
  130. _M_base() = __l;
  131. this->_M_invalidate_all();
  132. return *this;
  133. }
  134. #endif
  135. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  136. // 133. map missing get_allocator()
  137. using _Base::get_allocator;
  138. // iterators:
  139. iterator
  140. begin() _GLIBCXX_NOEXCEPT
  141. { return iterator(_Base::begin(), this); }
  142. const_iterator
  143. begin() const _GLIBCXX_NOEXCEPT
  144. { return const_iterator(_Base::begin(), this); }
  145. iterator
  146. end() _GLIBCXX_NOEXCEPT
  147. { return iterator(_Base::end(), this); }
  148. const_iterator
  149. end() const _GLIBCXX_NOEXCEPT
  150. { return const_iterator(_Base::end(), this); }
  151. reverse_iterator
  152. rbegin() _GLIBCXX_NOEXCEPT
  153. { return reverse_iterator(end()); }
  154. const_reverse_iterator
  155. rbegin() const _GLIBCXX_NOEXCEPT
  156. { return const_reverse_iterator(end()); }
  157. reverse_iterator
  158. rend() _GLIBCXX_NOEXCEPT
  159. { return reverse_iterator(begin()); }
  160. const_reverse_iterator
  161. rend() const _GLIBCXX_NOEXCEPT
  162. { return const_reverse_iterator(begin()); }
  163. #if __cplusplus >= 201103L
  164. const_iterator
  165. cbegin() const noexcept
  166. { return const_iterator(_Base::begin(), this); }
  167. const_iterator
  168. cend() const noexcept
  169. { return const_iterator(_Base::end(), this); }
  170. const_reverse_iterator
  171. crbegin() const noexcept
  172. { return const_reverse_iterator(end()); }
  173. const_reverse_iterator
  174. crend() const noexcept
  175. { return const_reverse_iterator(begin()); }
  176. #endif
  177. // capacity:
  178. using _Base::empty;
  179. using _Base::size;
  180. using _Base::max_size;
  181. // 23.3.1.2 element access:
  182. using _Base::operator[];
  183. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  184. // DR 464. Suggestion for new member functions in standard containers.
  185. using _Base::at;
  186. // modifiers:
  187. #if __cplusplus >= 201103L
  188. template<typename... _Args>
  189. std::pair<iterator, bool>
  190. emplace(_Args&&... __args)
  191. {
  192. auto __res = _Base::emplace(std::forward<_Args>(__args)...);
  193. return std::pair<iterator, bool>(iterator(__res.first, this),
  194. __res.second);
  195. }
  196. template<typename... _Args>
  197. iterator
  198. emplace_hint(const_iterator __pos, _Args&&... __args)
  199. {
  200. __glibcxx_check_insert(__pos);
  201. return iterator(_Base::emplace_hint(__pos.base(),
  202. std::forward<_Args>(__args)...),
  203. this);
  204. }
  205. #endif
  206. std::pair<iterator, bool>
  207. insert(const value_type& __x)
  208. {
  209. std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
  210. return std::pair<iterator, bool>(iterator(__res.first, this),
  211. __res.second);
  212. }
  213. #if __cplusplus >= 201103L
  214. template<typename _Pair, typename = typename
  215. std::enable_if<std::is_constructible<value_type,
  216. _Pair&&>::value>::type>
  217. std::pair<iterator, bool>
  218. insert(_Pair&& __x)
  219. {
  220. std::pair<_Base_iterator, bool> __res
  221. = _Base::insert(std::forward<_Pair>(__x));
  222. return std::pair<iterator, bool>(iterator(__res.first, this),
  223. __res.second);
  224. }
  225. #endif
  226. #if __cplusplus >= 201103L
  227. void
  228. insert(std::initializer_list<value_type> __list)
  229. { _Base::insert(__list); }
  230. #endif
  231. iterator
  232. #if __cplusplus >= 201103L
  233. insert(const_iterator __position, const value_type& __x)
  234. #else
  235. insert(iterator __position, const value_type& __x)
  236. #endif
  237. {
  238. __glibcxx_check_insert(__position);
  239. return iterator(_Base::insert(__position.base(), __x), this);
  240. }
  241. #if __cplusplus >= 201103L
  242. template<typename _Pair, typename = typename
  243. std::enable_if<std::is_constructible<value_type,
  244. _Pair&&>::value>::type>
  245. iterator
  246. insert(const_iterator __position, _Pair&& __x)
  247. {
  248. __glibcxx_check_insert(__position);
  249. return iterator(_Base::insert(__position.base(),
  250. std::forward<_Pair>(__x)), this);
  251. }
  252. #endif
  253. template<typename _InputIterator>
  254. void
  255. insert(_InputIterator __first, _InputIterator __last)
  256. {
  257. __glibcxx_check_valid_range(__first, __last);
  258. _Base::insert(__gnu_debug::__base(__first),
  259. __gnu_debug::__base(__last));
  260. }
  261. #if __cplusplus >= 201103L
  262. iterator
  263. erase(const_iterator __position)
  264. {
  265. __glibcxx_check_erase(__position);
  266. this->_M_invalidate_if(_Equal(__position.base()));
  267. return iterator(_Base::erase(__position.base()), this);
  268. }
  269. iterator
  270. erase(iterator __position)
  271. { return erase(const_iterator(__position)); }
  272. #else
  273. void
  274. erase(iterator __position)
  275. {
  276. __glibcxx_check_erase(__position);
  277. this->_M_invalidate_if(_Equal(__position.base()));
  278. _Base::erase(__position.base());
  279. }
  280. #endif
  281. size_type
  282. erase(const key_type& __x)
  283. {
  284. _Base_iterator __victim = _Base::find(__x);
  285. if (__victim == _Base::end())
  286. return 0;
  287. else
  288. {
  289. this->_M_invalidate_if(_Equal(__victim));
  290. _Base::erase(__victim);
  291. return 1;
  292. }
  293. }
  294. #if __cplusplus >= 201103L
  295. iterator
  296. erase(const_iterator __first, const_iterator __last)
  297. {
  298. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  299. // 151. can't currently clear() empty container
  300. __glibcxx_check_erase_range(__first, __last);
  301. for (_Base_const_iterator __victim = __first.base();
  302. __victim != __last.base(); ++__victim)
  303. {
  304. _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
  305. _M_message(__gnu_debug::__msg_valid_range)
  306. ._M_iterator(__first, "first")
  307. ._M_iterator(__last, "last"));
  308. this->_M_invalidate_if(_Equal(__victim));
  309. }
  310. return iterator(_Base::erase(__first.base(), __last.base()), this);
  311. }
  312. #else
  313. void
  314. erase(iterator __first, iterator __last)
  315. {
  316. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  317. // 151. can't currently clear() empty container
  318. __glibcxx_check_erase_range(__first, __last);
  319. for (_Base_iterator __victim = __first.base();
  320. __victim != __last.base(); ++__victim)
  321. {
  322. _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
  323. _M_message(__gnu_debug::__msg_valid_range)
  324. ._M_iterator(__first, "first")
  325. ._M_iterator(__last, "last"));
  326. this->_M_invalidate_if(_Equal(__victim));
  327. }
  328. _Base::erase(__first.base(), __last.base());
  329. }
  330. #endif
  331. void
  332. swap(map& __x)
  333. #if __cplusplus >= 201103L
  334. noexcept( noexcept(declval<_Base>().swap(__x)) )
  335. #endif
  336. {
  337. _Safe::_M_swap(__x);
  338. _Base::swap(__x);
  339. }
  340. void
  341. clear() _GLIBCXX_NOEXCEPT
  342. {
  343. this->_M_invalidate_all();
  344. _Base::clear();
  345. }
  346. // observers:
  347. using _Base::key_comp;
  348. using _Base::value_comp;
  349. // 23.3.1.3 map operations:
  350. iterator
  351. find(const key_type& __x)
  352. { return iterator(_Base::find(__x), this); }
  353. const_iterator
  354. find(const key_type& __x) const
  355. { return const_iterator(_Base::find(__x), this); }
  356. using _Base::count;
  357. iterator
  358. lower_bound(const key_type& __x)
  359. { return iterator(_Base::lower_bound(__x), this); }
  360. const_iterator
  361. lower_bound(const key_type& __x) const
  362. { return const_iterator(_Base::lower_bound(__x), this); }
  363. iterator
  364. upper_bound(const key_type& __x)
  365. { return iterator(_Base::upper_bound(__x), this); }
  366. const_iterator
  367. upper_bound(const key_type& __x) const
  368. { return const_iterator(_Base::upper_bound(__x), this); }
  369. std::pair<iterator,iterator>
  370. equal_range(const key_type& __x)
  371. {
  372. std::pair<_Base_iterator, _Base_iterator> __res =
  373. _Base::equal_range(__x);
  374. return std::make_pair(iterator(__res.first, this),
  375. iterator(__res.second, this));
  376. }
  377. std::pair<const_iterator,const_iterator>
  378. equal_range(const key_type& __x) const
  379. {
  380. std::pair<_Base_const_iterator, _Base_const_iterator> __res =
  381. _Base::equal_range(__x);
  382. return std::make_pair(const_iterator(__res.first, this),
  383. const_iterator(__res.second, this));
  384. }
  385. _Base&
  386. _M_base() _GLIBCXX_NOEXCEPT { return *this; }
  387. const _Base&
  388. _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
  389. };
  390. template<typename _Key, typename _Tp,
  391. typename _Compare, typename _Allocator>
  392. inline bool
  393. operator==(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  394. const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  395. { return __lhs._M_base() == __rhs._M_base(); }
  396. template<typename _Key, typename _Tp,
  397. typename _Compare, typename _Allocator>
  398. inline bool
  399. operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  400. const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  401. { return __lhs._M_base() != __rhs._M_base(); }
  402. template<typename _Key, typename _Tp,
  403. typename _Compare, typename _Allocator>
  404. inline bool
  405. operator<(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  406. const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  407. { return __lhs._M_base() < __rhs._M_base(); }
  408. template<typename _Key, typename _Tp,
  409. typename _Compare, typename _Allocator>
  410. inline bool
  411. operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  412. const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  413. { return __lhs._M_base() <= __rhs._M_base(); }
  414. template<typename _Key, typename _Tp,
  415. typename _Compare, typename _Allocator>
  416. inline bool
  417. operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  418. const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  419. { return __lhs._M_base() >= __rhs._M_base(); }
  420. template<typename _Key, typename _Tp,
  421. typename _Compare, typename _Allocator>
  422. inline bool
  423. operator>(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  424. const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  425. { return __lhs._M_base() > __rhs._M_base(); }
  426. template<typename _Key, typename _Tp,
  427. typename _Compare, typename _Allocator>
  428. inline void
  429. swap(map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  430. map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  431. { __lhs.swap(__rhs); }
  432. } // namespace __debug
  433. } // namespace std
  434. #endif