set.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. // Debugging set implementation -*- C++ -*-
  2. // Copyright (C) 2003-2018 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. noexcept( noexcept(_Base(std::move(__x._M_base()), __a)) )
  88. : _Safe(std::move(__x._M_safe()), __a),
  89. _Base(std::move(__x._M_base()), __a) { }
  90. set(initializer_list<value_type> __l, const allocator_type& __a)
  91. : _Base(__l, __a) { }
  92. template<typename _InputIterator>
  93. set(_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. ~set() = default;
  99. #endif
  100. explicit set(const _Compare& __comp,
  101. const _Allocator& __a = _Allocator())
  102. : _Base(__comp, __a) { }
  103. template<typename _InputIterator>
  104. set(_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. set(const _Base& __x)
  112. : _Base(__x) { }
  113. #if __cplusplus < 201103L
  114. set&
  115. operator=(const set& __x)
  116. {
  117. this->_M_safe() = __x;
  118. _M_base() = __x;
  119. return *this;
  120. }
  121. #else
  122. set&
  123. operator=(const set&) = default;
  124. set&
  125. operator=(set&&) = default;
  126. set&
  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. std::pair<iterator, bool>
  182. emplace(_Args&&... __args)
  183. {
  184. auto __res = _Base::emplace(std::forward<_Args>(__args)...);
  185. return std::pair<iterator, bool>(iterator(__res.first, this),
  186. __res.second);
  187. }
  188. template<typename... _Args>
  189. iterator
  190. emplace_hint(const_iterator __pos, _Args&&... __args)
  191. {
  192. __glibcxx_check_insert(__pos);
  193. return iterator(_Base::emplace_hint(__pos.base(),
  194. std::forward<_Args>(__args)...),
  195. this);
  196. }
  197. #endif
  198. std::pair<iterator, bool>
  199. insert(const value_type& __x)
  200. {
  201. std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
  202. return std::pair<iterator, bool>(iterator(__res.first, this),
  203. __res.second);
  204. }
  205. #if __cplusplus >= 201103L
  206. std::pair<iterator, bool>
  207. insert(value_type&& __x)
  208. {
  209. std::pair<_Base_iterator, bool> __res
  210. = _Base::insert(std::move(__x));
  211. return std::pair<iterator, bool>(iterator(__res.first, this),
  212. __res.second);
  213. }
  214. #endif
  215. iterator
  216. insert(const_iterator __position, const value_type& __x)
  217. {
  218. __glibcxx_check_insert(__position);
  219. return iterator(_Base::insert(__position.base(), __x), this);
  220. }
  221. #if __cplusplus >= 201103L
  222. iterator
  223. insert(const_iterator __position, value_type&& __x)
  224. {
  225. __glibcxx_check_insert(__position);
  226. return iterator(_Base::insert(__position.base(), std::move(__x)),
  227. this);
  228. }
  229. #endif
  230. template <typename _InputIterator>
  231. void
  232. insert(_InputIterator __first, _InputIterator __last)
  233. {
  234. typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
  235. __glibcxx_check_valid_range2(__first, __last, __dist);
  236. if (__dist.second >= __gnu_debug::__dp_sign)
  237. _Base::insert(__gnu_debug::__unsafe(__first),
  238. __gnu_debug::__unsafe(__last));
  239. else
  240. _Base::insert(__first, __last);
  241. }
  242. #if __cplusplus >= 201103L
  243. void
  244. insert(initializer_list<value_type> __l)
  245. { _Base::insert(__l); }
  246. #endif
  247. #if __cplusplus > 201402L
  248. using node_type = typename _Base::node_type;
  249. using insert_return_type = _Node_insert_return<iterator, node_type>;
  250. node_type
  251. extract(const_iterator __position)
  252. {
  253. __glibcxx_check_erase(__position);
  254. this->_M_invalidate_if(_Equal(__position.base()));
  255. return _Base::extract(__position.base());
  256. }
  257. node_type
  258. extract(const key_type& __key)
  259. {
  260. const auto __position = find(__key);
  261. if (__position != end())
  262. return extract(__position);
  263. return {};
  264. }
  265. insert_return_type
  266. insert(node_type&& __nh)
  267. {
  268. auto __ret = _Base::insert(std::move(__nh));
  269. iterator __pos = iterator(__ret.position, this);
  270. return { __pos, __ret.inserted, std::move(__ret.node) };
  271. }
  272. iterator
  273. insert(const_iterator __hint, node_type&& __nh)
  274. {
  275. __glibcxx_check_insert(__hint);
  276. return iterator(_Base::insert(__hint.base(), std::move(__nh)), this);
  277. }
  278. using _Base::merge;
  279. #endif // C++17
  280. #if __cplusplus >= 201103L
  281. iterator
  282. erase(const_iterator __position)
  283. {
  284. __glibcxx_check_erase(__position);
  285. this->_M_invalidate_if(_Equal(__position.base()));
  286. return iterator(_Base::erase(__position.base()), this);
  287. }
  288. #else
  289. void
  290. erase(iterator __position)
  291. {
  292. __glibcxx_check_erase(__position);
  293. this->_M_invalidate_if(_Equal(__position.base()));
  294. _Base::erase(__position.base());
  295. }
  296. #endif
  297. size_type
  298. erase(const key_type& __x)
  299. {
  300. _Base_iterator __victim = _Base::find(__x);
  301. if (__victim == _Base::end())
  302. return 0;
  303. else
  304. {
  305. this->_M_invalidate_if(_Equal(__victim));
  306. _Base::erase(__victim);
  307. return 1;
  308. }
  309. }
  310. #if __cplusplus >= 201103L
  311. iterator
  312. erase(const_iterator __first, const_iterator __last)
  313. {
  314. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  315. // 151. can't currently clear() empty container
  316. __glibcxx_check_erase_range(__first, __last);
  317. for (_Base_const_iterator __victim = __first.base();
  318. __victim != __last.base(); ++__victim)
  319. {
  320. _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
  321. _M_message(__gnu_debug::__msg_valid_range)
  322. ._M_iterator(__first, "first")
  323. ._M_iterator(__last, "last"));
  324. this->_M_invalidate_if(_Equal(__victim));
  325. }
  326. return iterator(_Base::erase(__first.base(), __last.base()), this);
  327. }
  328. #else
  329. void
  330. erase(iterator __first, iterator __last)
  331. {
  332. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  333. // 151. can't currently clear() empty container
  334. __glibcxx_check_erase_range(__first, __last);
  335. for (_Base_iterator __victim = __first.base();
  336. __victim != __last.base(); ++__victim)
  337. {
  338. _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
  339. _M_message(__gnu_debug::__msg_valid_range)
  340. ._M_iterator(__first, "first")
  341. ._M_iterator(__last, "last"));
  342. this->_M_invalidate_if(_Equal(__victim));
  343. }
  344. _Base::erase(__first.base(), __last.base());
  345. }
  346. #endif
  347. void
  348. swap(set& __x)
  349. _GLIBCXX_NOEXCEPT_IF( noexcept(declval<_Base&>().swap(__x)) )
  350. {
  351. _Safe::_M_swap(__x);
  352. _Base::swap(__x);
  353. }
  354. void
  355. clear() _GLIBCXX_NOEXCEPT
  356. {
  357. this->_M_invalidate_all();
  358. _Base::clear();
  359. }
  360. // observers:
  361. using _Base::key_comp;
  362. using _Base::value_comp;
  363. // set operations:
  364. iterator
  365. find(const key_type& __x)
  366. { return iterator(_Base::find(__x), this); }
  367. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  368. // 214. set::find() missing const overload
  369. const_iterator
  370. find(const key_type& __x) const
  371. { return const_iterator(_Base::find(__x), this); }
  372. #if __cplusplus > 201103L
  373. template<typename _Kt,
  374. typename _Req =
  375. typename __has_is_transparent<_Compare, _Kt>::type>
  376. iterator
  377. find(const _Kt& __x)
  378. { return { _Base::find(__x), this }; }
  379. template<typename _Kt,
  380. typename _Req =
  381. typename __has_is_transparent<_Compare, _Kt>::type>
  382. const_iterator
  383. find(const _Kt& __x) const
  384. { return { _Base::find(__x), this }; }
  385. #endif
  386. using _Base::count;
  387. iterator
  388. lower_bound(const key_type& __x)
  389. { return iterator(_Base::lower_bound(__x), this); }
  390. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  391. // 214. set::find() missing const overload
  392. const_iterator
  393. lower_bound(const key_type& __x) const
  394. { return const_iterator(_Base::lower_bound(__x), this); }
  395. #if __cplusplus > 201103L
  396. template<typename _Kt,
  397. typename _Req =
  398. typename __has_is_transparent<_Compare, _Kt>::type>
  399. iterator
  400. lower_bound(const _Kt& __x)
  401. { return { _Base::lower_bound(__x), this }; }
  402. template<typename _Kt,
  403. typename _Req =
  404. typename __has_is_transparent<_Compare, _Kt>::type>
  405. const_iterator
  406. lower_bound(const _Kt& __x) const
  407. { return { _Base::lower_bound(__x), this }; }
  408. #endif
  409. iterator
  410. upper_bound(const key_type& __x)
  411. { return iterator(_Base::upper_bound(__x), this); }
  412. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  413. // 214. set::find() missing const overload
  414. const_iterator
  415. upper_bound(const key_type& __x) const
  416. { return const_iterator(_Base::upper_bound(__x), this); }
  417. #if __cplusplus > 201103L
  418. template<typename _Kt,
  419. typename _Req =
  420. typename __has_is_transparent<_Compare, _Kt>::type>
  421. iterator
  422. upper_bound(const _Kt& __x)
  423. { return { _Base::upper_bound(__x), this }; }
  424. template<typename _Kt,
  425. typename _Req =
  426. typename __has_is_transparent<_Compare, _Kt>::type>
  427. const_iterator
  428. upper_bound(const _Kt& __x) const
  429. { return { _Base::upper_bound(__x), this }; }
  430. #endif
  431. std::pair<iterator, iterator>
  432. equal_range(const key_type& __x)
  433. {
  434. std::pair<_Base_iterator, _Base_iterator> __res =
  435. _Base::equal_range(__x);
  436. return std::make_pair(iterator(__res.first, this),
  437. iterator(__res.second, this));
  438. }
  439. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  440. // 214. set::find() missing const overload
  441. std::pair<const_iterator, const_iterator>
  442. equal_range(const key_type& __x) const
  443. {
  444. std::pair<_Base_const_iterator, _Base_const_iterator> __res =
  445. _Base::equal_range(__x);
  446. return std::make_pair(const_iterator(__res.first, this),
  447. const_iterator(__res.second, this));
  448. }
  449. #if __cplusplus > 201103L
  450. template<typename _Kt,
  451. typename _Req =
  452. typename __has_is_transparent<_Compare, _Kt>::type>
  453. std::pair<iterator, iterator>
  454. equal_range(const _Kt& __x)
  455. {
  456. auto __res = _Base::equal_range(__x);
  457. return { { __res.first, this }, { __res.second, this } };
  458. }
  459. template<typename _Kt,
  460. typename _Req =
  461. typename __has_is_transparent<_Compare, _Kt>::type>
  462. std::pair<const_iterator, const_iterator>
  463. equal_range(const _Kt& __x) const
  464. {
  465. auto __res = _Base::equal_range(__x);
  466. return { { __res.first, this }, { __res.second, this } };
  467. }
  468. #endif
  469. _Base&
  470. _M_base() _GLIBCXX_NOEXCEPT { return *this; }
  471. const _Base&
  472. _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
  473. };
  474. #if __cpp_deduction_guides >= 201606
  475. template<typename _InputIterator,
  476. typename _Compare =
  477. less<typename iterator_traits<_InputIterator>::value_type>,
  478. typename _Allocator =
  479. allocator<typename iterator_traits<_InputIterator>::value_type>,
  480. typename = _RequireInputIter<_InputIterator>,
  481. typename = _RequireAllocator<_Allocator>>
  482. set(_InputIterator, _InputIterator,
  483. _Compare = _Compare(), _Allocator = _Allocator())
  484. -> set<typename iterator_traits<_InputIterator>::value_type,
  485. _Compare, _Allocator>;
  486. template<typename _Key, typename _Compare = less<_Key>,
  487. typename _Allocator = allocator<_Key>,
  488. typename = _RequireAllocator<_Allocator>>
  489. set(initializer_list<_Key>,
  490. _Compare = _Compare(), _Allocator = _Allocator())
  491. -> set<_Key, _Compare, _Allocator>;
  492. template<typename _InputIterator, typename _Allocator,
  493. typename = _RequireInputIter<_InputIterator>,
  494. typename = _RequireAllocator<_Allocator>>
  495. set(_InputIterator, _InputIterator, _Allocator)
  496. -> set<typename iterator_traits<_InputIterator>::value_type,
  497. less<typename iterator_traits<_InputIterator>::value_type>,
  498. _Allocator>;
  499. template<typename _Key, typename _Allocator,
  500. typename = _RequireAllocator<_Allocator>>
  501. set(initializer_list<_Key>, _Allocator)
  502. -> set<_Key, less<_Key>, _Allocator>;
  503. #endif
  504. template<typename _Key, typename _Compare, typename _Allocator>
  505. inline bool
  506. operator==(const set<_Key, _Compare, _Allocator>& __lhs,
  507. const set<_Key, _Compare, _Allocator>& __rhs)
  508. { return __lhs._M_base() == __rhs._M_base(); }
  509. template<typename _Key, typename _Compare, typename _Allocator>
  510. inline bool
  511. operator!=(const set<_Key, _Compare, _Allocator>& __lhs,
  512. const set<_Key, _Compare, _Allocator>& __rhs)
  513. { return __lhs._M_base() != __rhs._M_base(); }
  514. template<typename _Key, typename _Compare, typename _Allocator>
  515. inline bool
  516. operator<(const set<_Key, _Compare, _Allocator>& __lhs,
  517. const set<_Key, _Compare, _Allocator>& __rhs)
  518. { return __lhs._M_base() < __rhs._M_base(); }
  519. template<typename _Key, typename _Compare, typename _Allocator>
  520. inline bool
  521. operator<=(const set<_Key, _Compare, _Allocator>& __lhs,
  522. const set<_Key, _Compare, _Allocator>& __rhs)
  523. { return __lhs._M_base() <= __rhs._M_base(); }
  524. template<typename _Key, typename _Compare, typename _Allocator>
  525. inline bool
  526. operator>=(const set<_Key, _Compare, _Allocator>& __lhs,
  527. const set<_Key, _Compare, _Allocator>& __rhs)
  528. { return __lhs._M_base() >= __rhs._M_base(); }
  529. template<typename _Key, typename _Compare, typename _Allocator>
  530. inline bool
  531. operator>(const set<_Key, _Compare, _Allocator>& __lhs,
  532. const set<_Key, _Compare, _Allocator>& __rhs)
  533. { return __lhs._M_base() > __rhs._M_base(); }
  534. template<typename _Key, typename _Compare, typename _Allocator>
  535. void
  536. swap(set<_Key, _Compare, _Allocator>& __x,
  537. set<_Key, _Compare, _Allocator>& __y)
  538. _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
  539. { return __x.swap(__y); }
  540. } // namespace __debug
  541. } // namespace std
  542. #endif