set.h 11 KB

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