You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

655 lines
19KB

  1. // Debugging set implementation -*- C++ -*-
  2. // Copyright (C) 2003-2020 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. template<typename _ItT, typename _SeqT, typename _CatT>
  49. friend class ::__gnu_debug::_Safe_iterator;
  50. public:
  51. // types:
  52. typedef _Key key_type;
  53. typedef _Key value_type;
  54. typedef _Compare key_compare;
  55. typedef _Compare value_compare;
  56. typedef _Allocator allocator_type;
  57. typedef typename _Base::reference reference;
  58. typedef typename _Base::const_reference const_reference;
  59. typedef __gnu_debug::_Safe_iterator<_Base_iterator, set>
  60. iterator;
  61. typedef __gnu_debug::_Safe_iterator<_Base_const_iterator, set>
  62. const_iterator;
  63. typedef typename _Base::size_type size_type;
  64. typedef typename _Base::difference_type difference_type;
  65. typedef typename _Base::pointer pointer;
  66. typedef typename _Base::const_pointer const_pointer;
  67. typedef std::reverse_iterator<iterator> reverse_iterator;
  68. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  69. // 23.3.3.1 construct/copy/destroy:
  70. #if __cplusplus < 201103L
  71. set() : _Base() { }
  72. set(const set& __x)
  73. : _Base(__x) { }
  74. ~set() { }
  75. #else
  76. set() = default;
  77. set(const set&) = default;
  78. set(set&&) = default;
  79. set(initializer_list<value_type> __l,
  80. const _Compare& __comp = _Compare(),
  81. const allocator_type& __a = allocator_type())
  82. : _Base(__l, __comp, __a) { }
  83. explicit
  84. set(const allocator_type& __a)
  85. : _Base(__a) { }
  86. set(const set& __x, const allocator_type& __a)
  87. : _Base(__x, __a) { }
  88. set(set&& __x, const allocator_type& __a)
  89. noexcept( noexcept(_Base(std::move(__x._M_base()), __a)) )
  90. : _Safe(std::move(__x._M_safe()), __a),
  91. _Base(std::move(__x._M_base()), __a) { }
  92. set(initializer_list<value_type> __l, const allocator_type& __a)
  93. : _Base(__l, __a) { }
  94. template<typename _InputIterator>
  95. set(_InputIterator __first, _InputIterator __last,
  96. const allocator_type& __a)
  97. : _Base(__gnu_debug::__base(
  98. __glibcxx_check_valid_constructor_range(__first, __last)),
  99. __gnu_debug::__base(__last), __a) { }
  100. ~set() = default;
  101. #endif
  102. explicit set(const _Compare& __comp,
  103. const _Allocator& __a = _Allocator())
  104. : _Base(__comp, __a) { }
  105. template<typename _InputIterator>
  106. set(_InputIterator __first, _InputIterator __last,
  107. const _Compare& __comp = _Compare(),
  108. const _Allocator& __a = _Allocator())
  109. : _Base(__gnu_debug::__base(
  110. __glibcxx_check_valid_constructor_range(__first, __last)),
  111. __gnu_debug::__base(__last),
  112. __comp, __a) { }
  113. set(const _Base& __x)
  114. : _Base(__x) { }
  115. #if __cplusplus < 201103L
  116. set&
  117. operator=(const set& __x)
  118. {
  119. this->_M_safe() = __x;
  120. _M_base() = __x;
  121. return *this;
  122. }
  123. #else
  124. set&
  125. operator=(const set&) = default;
  126. set&
  127. operator=(set&&) = default;
  128. set&
  129. operator=(initializer_list<value_type> __l)
  130. {
  131. _M_base() = __l;
  132. this->_M_invalidate_all();
  133. return *this;
  134. }
  135. #endif
  136. using _Base::get_allocator;
  137. // iterators:
  138. iterator
  139. begin() _GLIBCXX_NOEXCEPT
  140. { return iterator(_Base::begin(), this); }
  141. const_iterator
  142. begin() const _GLIBCXX_NOEXCEPT
  143. { return const_iterator(_Base::begin(), this); }
  144. iterator
  145. end() _GLIBCXX_NOEXCEPT
  146. { return iterator(_Base::end(), this); }
  147. const_iterator
  148. end() const _GLIBCXX_NOEXCEPT
  149. { return const_iterator(_Base::end(), this); }
  150. reverse_iterator
  151. rbegin() _GLIBCXX_NOEXCEPT
  152. { return reverse_iterator(end()); }
  153. const_reverse_iterator
  154. rbegin() const _GLIBCXX_NOEXCEPT
  155. { return const_reverse_iterator(end()); }
  156. reverse_iterator
  157. rend() _GLIBCXX_NOEXCEPT
  158. { return reverse_iterator(begin()); }
  159. const_reverse_iterator
  160. rend() const _GLIBCXX_NOEXCEPT
  161. { return const_reverse_iterator(begin()); }
  162. #if __cplusplus >= 201103L
  163. const_iterator
  164. cbegin() const noexcept
  165. { return const_iterator(_Base::begin(), this); }
  166. const_iterator
  167. cend() const noexcept
  168. { return const_iterator(_Base::end(), this); }
  169. const_reverse_iterator
  170. crbegin() const noexcept
  171. { return const_reverse_iterator(end()); }
  172. const_reverse_iterator
  173. crend() const noexcept
  174. { return const_reverse_iterator(begin()); }
  175. #endif
  176. // capacity:
  177. using _Base::empty;
  178. using _Base::size;
  179. using _Base::max_size;
  180. // modifiers:
  181. #if __cplusplus >= 201103L
  182. template<typename... _Args>
  183. std::pair<iterator, bool>
  184. emplace(_Args&&... __args)
  185. {
  186. auto __res = _Base::emplace(std::forward<_Args>(__args)...);
  187. return { { __res.first, this }, __res.second };
  188. }
  189. template<typename... _Args>
  190. iterator
  191. emplace_hint(const_iterator __pos, _Args&&... __args)
  192. {
  193. __glibcxx_check_insert(__pos);
  194. return
  195. {
  196. _Base::emplace_hint(__pos.base(), std::forward<_Args>(__args)...),
  197. this
  198. };
  199. }
  200. #endif
  201. std::pair<iterator, bool>
  202. insert(const value_type& __x)
  203. {
  204. std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
  205. return std::pair<iterator, bool>(iterator(__res.first, this),
  206. __res.second);
  207. }
  208. #if __cplusplus >= 201103L
  209. std::pair<iterator, bool>
  210. insert(value_type&& __x)
  211. {
  212. auto __res = _Base::insert(std::move(__x));
  213. return { { __res.first, this }, __res.second };
  214. }
  215. #endif
  216. iterator
  217. insert(const_iterator __position, const value_type& __x)
  218. {
  219. __glibcxx_check_insert(__position);
  220. return iterator(_Base::insert(__position.base(), __x), this);
  221. }
  222. #if __cplusplus >= 201103L
  223. iterator
  224. insert(const_iterator __position, value_type&& __x)
  225. {
  226. __glibcxx_check_insert(__position);
  227. return { _Base::insert(__position.base(), std::move(__x)), 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 { _Base::insert(__hint.base(), std::move(__nh)), this };
  277. }
  278. using _Base::merge;
  279. #endif // C++17
  280. #if __cplusplus >= 201103L
  281. _GLIBCXX_ABI_TAG_CXX11
  282. iterator
  283. erase(const_iterator __position)
  284. {
  285. __glibcxx_check_erase(__position);
  286. this->_M_invalidate_if(_Equal(__position.base()));
  287. return { _Base::erase(__position.base()), this };
  288. }
  289. #else
  290. void
  291. erase(iterator __position)
  292. {
  293. __glibcxx_check_erase(__position);
  294. this->_M_invalidate_if(_Equal(__position.base()));
  295. _Base::erase(__position.base());
  296. }
  297. #endif
  298. size_type
  299. erase(const key_type& __x)
  300. {
  301. _Base_iterator __victim = _Base::find(__x);
  302. if (__victim == _Base::end())
  303. return 0;
  304. else
  305. {
  306. this->_M_invalidate_if(_Equal(__victim));
  307. _Base::erase(__victim);
  308. return 1;
  309. }
  310. }
  311. #if __cplusplus >= 201103L
  312. _GLIBCXX_ABI_TAG_CXX11
  313. iterator
  314. erase(const_iterator __first, const_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_const_iterator __victim = __first.base();
  320. __victim != __last.base(); ++__victim)
  321. {
  322. _GLIBCXX_DEBUG_VERIFY(__victim != _Base::cend(),
  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. return { _Base::erase(__first.base(), __last.base()), this };
  329. }
  330. #else
  331. void
  332. erase(iterator __first, iterator __last)
  333. {
  334. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  335. // 151. can't currently clear() empty container
  336. __glibcxx_check_erase_range(__first, __last);
  337. for (_Base_iterator __victim = __first.base();
  338. __victim != __last.base(); ++__victim)
  339. {
  340. _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
  341. _M_message(__gnu_debug::__msg_valid_range)
  342. ._M_iterator(__first, "first")
  343. ._M_iterator(__last, "last"));
  344. this->_M_invalidate_if(_Equal(__victim));
  345. }
  346. _Base::erase(__first.base(), __last.base());
  347. }
  348. #endif
  349. void
  350. swap(set& __x)
  351. _GLIBCXX_NOEXCEPT_IF( noexcept(declval<_Base&>().swap(__x)) )
  352. {
  353. _Safe::_M_swap(__x);
  354. _Base::swap(__x);
  355. }
  356. void
  357. clear() _GLIBCXX_NOEXCEPT
  358. {
  359. this->_M_invalidate_all();
  360. _Base::clear();
  361. }
  362. // observers:
  363. using _Base::key_comp;
  364. using _Base::value_comp;
  365. // set operations:
  366. iterator
  367. find(const key_type& __x)
  368. { return iterator(_Base::find(__x), this); }
  369. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  370. // 214. set::find() missing const overload
  371. const_iterator
  372. find(const key_type& __x) const
  373. { return const_iterator(_Base::find(__x), this); }
  374. #if __cplusplus > 201103L
  375. template<typename _Kt,
  376. typename _Req =
  377. typename __has_is_transparent<_Compare, _Kt>::type>
  378. iterator
  379. find(const _Kt& __x)
  380. { return { _Base::find(__x), this }; }
  381. template<typename _Kt,
  382. typename _Req =
  383. typename __has_is_transparent<_Compare, _Kt>::type>
  384. const_iterator
  385. find(const _Kt& __x) const
  386. { return { _Base::find(__x), this }; }
  387. #endif
  388. using _Base::count;
  389. iterator
  390. lower_bound(const key_type& __x)
  391. { return iterator(_Base::lower_bound(__x), this); }
  392. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  393. // 214. set::find() missing const overload
  394. const_iterator
  395. lower_bound(const key_type& __x) const
  396. { return const_iterator(_Base::lower_bound(__x), this); }
  397. #if __cplusplus > 201103L
  398. template<typename _Kt,
  399. typename _Req =
  400. typename __has_is_transparent<_Compare, _Kt>::type>
  401. iterator
  402. lower_bound(const _Kt& __x)
  403. { return { _Base::lower_bound(__x), this }; }
  404. template<typename _Kt,
  405. typename _Req =
  406. typename __has_is_transparent<_Compare, _Kt>::type>
  407. const_iterator
  408. lower_bound(const _Kt& __x) const
  409. { return { _Base::lower_bound(__x), this }; }
  410. #endif
  411. iterator
  412. upper_bound(const key_type& __x)
  413. { return iterator(_Base::upper_bound(__x), this); }
  414. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  415. // 214. set::find() missing const overload
  416. const_iterator
  417. upper_bound(const key_type& __x) const
  418. { return const_iterator(_Base::upper_bound(__x), this); }
  419. #if __cplusplus > 201103L
  420. template<typename _Kt,
  421. typename _Req =
  422. typename __has_is_transparent<_Compare, _Kt>::type>
  423. iterator
  424. upper_bound(const _Kt& __x)
  425. { return { _Base::upper_bound(__x), this }; }
  426. template<typename _Kt,
  427. typename _Req =
  428. typename __has_is_transparent<_Compare, _Kt>::type>
  429. const_iterator
  430. upper_bound(const _Kt& __x) const
  431. { return { _Base::upper_bound(__x), this }; }
  432. #endif
  433. std::pair<iterator, iterator>
  434. equal_range(const key_type& __x)
  435. {
  436. std::pair<_Base_iterator, _Base_iterator> __res =
  437. _Base::equal_range(__x);
  438. return std::make_pair(iterator(__res.first, this),
  439. iterator(__res.second, this));
  440. }
  441. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  442. // 214. set::find() missing const overload
  443. std::pair<const_iterator, const_iterator>
  444. equal_range(const key_type& __x) const
  445. {
  446. std::pair<_Base_const_iterator, _Base_const_iterator> __res =
  447. _Base::equal_range(__x);
  448. return std::make_pair(const_iterator(__res.first, this),
  449. const_iterator(__res.second, this));
  450. }
  451. #if __cplusplus > 201103L
  452. template<typename _Kt,
  453. typename _Req =
  454. typename __has_is_transparent<_Compare, _Kt>::type>
  455. std::pair<iterator, iterator>
  456. equal_range(const _Kt& __x)
  457. {
  458. auto __res = _Base::equal_range(__x);
  459. return { { __res.first, this }, { __res.second, this } };
  460. }
  461. template<typename _Kt,
  462. typename _Req =
  463. typename __has_is_transparent<_Compare, _Kt>::type>
  464. std::pair<const_iterator, const_iterator>
  465. equal_range(const _Kt& __x) const
  466. {
  467. auto __res = _Base::equal_range(__x);
  468. return { { __res.first, this }, { __res.second, this } };
  469. }
  470. #endif
  471. _Base&
  472. _M_base() _GLIBCXX_NOEXCEPT { return *this; }
  473. const _Base&
  474. _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
  475. };
  476. #if __cpp_deduction_guides >= 201606
  477. template<typename _InputIterator,
  478. typename _Compare =
  479. less<typename iterator_traits<_InputIterator>::value_type>,
  480. typename _Allocator =
  481. allocator<typename iterator_traits<_InputIterator>::value_type>,
  482. typename = _RequireInputIter<_InputIterator>,
  483. typename = _RequireNotAllocator<_Compare>,
  484. typename = _RequireAllocator<_Allocator>>
  485. set(_InputIterator, _InputIterator,
  486. _Compare = _Compare(), _Allocator = _Allocator())
  487. -> set<typename iterator_traits<_InputIterator>::value_type,
  488. _Compare, _Allocator>;
  489. template<typename _Key, typename _Compare = less<_Key>,
  490. typename _Allocator = allocator<_Key>,
  491. typename = _RequireNotAllocator<_Compare>,
  492. typename = _RequireAllocator<_Allocator>>
  493. set(initializer_list<_Key>,
  494. _Compare = _Compare(), _Allocator = _Allocator())
  495. -> set<_Key, _Compare, _Allocator>;
  496. template<typename _InputIterator, typename _Allocator,
  497. typename = _RequireInputIter<_InputIterator>,
  498. typename = _RequireAllocator<_Allocator>>
  499. set(_InputIterator, _InputIterator, _Allocator)
  500. -> set<typename iterator_traits<_InputIterator>::value_type,
  501. less<typename iterator_traits<_InputIterator>::value_type>,
  502. _Allocator>;
  503. template<typename _Key, typename _Allocator,
  504. typename = _RequireAllocator<_Allocator>>
  505. set(initializer_list<_Key>, _Allocator)
  506. -> set<_Key, less<_Key>, _Allocator>;
  507. #endif // deduction guides
  508. template<typename _Key, typename _Compare, typename _Allocator>
  509. inline bool
  510. operator==(const set<_Key, _Compare, _Allocator>& __lhs,
  511. const set<_Key, _Compare, _Allocator>& __rhs)
  512. { return __lhs._M_base() == __rhs._M_base(); }
  513. #if __cpp_lib_three_way_comparison
  514. template<typename _Key, typename _Compare, typename _Alloc>
  515. inline __detail::__synth3way_t<_Key>
  516. operator<=>(const set<_Key, _Compare, _Alloc>& __lhs,
  517. const set<_Key, _Compare, _Alloc>& __rhs)
  518. { return __lhs._M_base() <=> __rhs._M_base(); }
  519. #else
  520. template<typename _Key, typename _Compare, typename _Allocator>
  521. inline bool
  522. operator!=(const set<_Key, _Compare, _Allocator>& __lhs,
  523. const set<_Key, _Compare, _Allocator>& __rhs)
  524. { return __lhs._M_base() != __rhs._M_base(); }
  525. template<typename _Key, typename _Compare, typename _Allocator>
  526. inline bool
  527. operator<(const set<_Key, _Compare, _Allocator>& __lhs,
  528. const set<_Key, _Compare, _Allocator>& __rhs)
  529. { return __lhs._M_base() < __rhs._M_base(); }
  530. template<typename _Key, typename _Compare, typename _Allocator>
  531. inline bool
  532. operator<=(const set<_Key, _Compare, _Allocator>& __lhs,
  533. const set<_Key, _Compare, _Allocator>& __rhs)
  534. { return __lhs._M_base() <= __rhs._M_base(); }
  535. template<typename _Key, typename _Compare, typename _Allocator>
  536. inline bool
  537. operator>=(const set<_Key, _Compare, _Allocator>& __lhs,
  538. const set<_Key, _Compare, _Allocator>& __rhs)
  539. { return __lhs._M_base() >= __rhs._M_base(); }
  540. template<typename _Key, typename _Compare, typename _Allocator>
  541. inline bool
  542. operator>(const set<_Key, _Compare, _Allocator>& __lhs,
  543. const set<_Key, _Compare, _Allocator>& __rhs)
  544. { return __lhs._M_base() > __rhs._M_base(); }
  545. #endif // three-way comparison
  546. template<typename _Key, typename _Compare, typename _Allocator>
  547. void
  548. swap(set<_Key, _Compare, _Allocator>& __x,
  549. set<_Key, _Compare, _Allocator>& __y)
  550. _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
  551. { return __x.swap(__y); }
  552. } // namespace __debug
  553. } // namespace std
  554. #endif