set.h 14 KB

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