multiset.h 19 KB

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