multiset.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. // Debugging multiset 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/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. 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, multiset>
  58. iterator;
  59. typedef __gnu_debug::_Safe_iterator<_Base_const_iterator,
  60. multiset> 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. multiset() : _Base() { }
  70. multiset(const multiset& __x)
  71. : _Base(__x) { }
  72. ~multiset() { }
  73. #else
  74. multiset() = default;
  75. multiset(const multiset&) = default;
  76. multiset(multiset&&) = default;
  77. multiset(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. multiset(const allocator_type& __a)
  83. : _Base(__a) { }
  84. multiset(const multiset& __m, const allocator_type& __a)
  85. : _Base(__m, __a) { }
  86. multiset(multiset&& __m, const allocator_type& __a)
  87. noexcept( noexcept(_Base(std::move(__m._M_base()), __a)) )
  88. : _Safe(std::move(__m._M_safe()), __a),
  89. _Base(std::move(__m._M_base()), __a) { }
  90. multiset(initializer_list<value_type> __l, const allocator_type& __a)
  91. : _Base(__l, __a)
  92. { }
  93. template<typename _InputIterator>
  94. multiset(_InputIterator __first, _InputIterator __last,
  95. const allocator_type& __a)
  96. : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
  97. __last)),
  98. __gnu_debug::__base(__last), __a) { }
  99. ~multiset() = default;
  100. #endif
  101. explicit multiset(const _Compare& __comp,
  102. const _Allocator& __a = _Allocator())
  103. : _Base(__comp, __a) { }
  104. template<typename _InputIterator>
  105. multiset(_InputIterator __first, _InputIterator __last,
  106. const _Compare& __comp = _Compare(),
  107. const _Allocator& __a = _Allocator())
  108. : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
  109. __last)),
  110. __gnu_debug::__base(__last),
  111. __comp, __a) { }
  112. multiset(const _Base& __x)
  113. : _Base(__x) { }
  114. #if __cplusplus < 201103L
  115. multiset&
  116. operator=(const multiset& __x)
  117. {
  118. this->_M_safe() = __x;
  119. _M_base() = __x;
  120. return *this;
  121. }
  122. #else
  123. multiset&
  124. operator=(const multiset&) = default;
  125. multiset&
  126. operator=(multiset&&) = default;
  127. multiset&
  128. operator=(initializer_list<value_type> __l)
  129. {
  130. _M_base() = __l;
  131. this->_M_invalidate_all();
  132. return *this;
  133. }
  134. #endif
  135. using _Base::get_allocator;
  136. // iterators:
  137. iterator
  138. begin() _GLIBCXX_NOEXCEPT
  139. { return iterator(_Base::begin(), this); }
  140. const_iterator
  141. begin() const _GLIBCXX_NOEXCEPT
  142. { return const_iterator(_Base::begin(), this); }
  143. iterator
  144. end() _GLIBCXX_NOEXCEPT
  145. { return iterator(_Base::end(), this); }
  146. const_iterator
  147. end() const _GLIBCXX_NOEXCEPT
  148. { return const_iterator(_Base::end(), this); }
  149. reverse_iterator
  150. rbegin() _GLIBCXX_NOEXCEPT
  151. { return reverse_iterator(end()); }
  152. const_reverse_iterator
  153. rbegin() const _GLIBCXX_NOEXCEPT
  154. { return const_reverse_iterator(end()); }
  155. reverse_iterator
  156. rend() _GLIBCXX_NOEXCEPT
  157. { return reverse_iterator(begin()); }
  158. const_reverse_iterator
  159. rend() const _GLIBCXX_NOEXCEPT
  160. { return const_reverse_iterator(begin()); }
  161. #if __cplusplus >= 201103L
  162. const_iterator
  163. cbegin() const noexcept
  164. { return const_iterator(_Base::begin(), this); }
  165. const_iterator
  166. cend() const noexcept
  167. { return const_iterator(_Base::end(), this); }
  168. const_reverse_iterator
  169. crbegin() const noexcept
  170. { return const_reverse_iterator(end()); }
  171. const_reverse_iterator
  172. crend() const noexcept
  173. { return const_reverse_iterator(begin()); }
  174. #endif
  175. // capacity:
  176. using _Base::empty;
  177. using _Base::size;
  178. using _Base::max_size;
  179. // modifiers:
  180. #if __cplusplus >= 201103L
  181. template<typename... _Args>
  182. iterator
  183. emplace(_Args&&... __args)
  184. {
  185. return iterator(_Base::emplace(std::forward<_Args>(__args)...),
  186. this);
  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. iterator
  199. insert(const value_type& __x)
  200. { return iterator(_Base::insert(__x), this); }
  201. #if __cplusplus >= 201103L
  202. iterator
  203. insert(value_type&& __x)
  204. { return iterator(_Base::insert(std::move(__x)), this); }
  205. #endif
  206. iterator
  207. insert(const_iterator __position, const value_type& __x)
  208. {
  209. __glibcxx_check_insert(__position);
  210. return iterator(_Base::insert(__position.base(), __x), this);
  211. }
  212. #if __cplusplus >= 201103L
  213. iterator
  214. insert(const_iterator __position, value_type&& __x)
  215. {
  216. __glibcxx_check_insert(__position);
  217. return iterator(_Base::insert(__position.base(), std::move(__x)),
  218. 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 iterator(_Base::insert(std::move(__nh)), this); }
  258. iterator
  259. insert(const_iterator __hint, node_type&& __nh)
  260. {
  261. __glibcxx_check_insert(__hint);
  262. return iterator(_Base::insert(__hint.base(), std::move(__nh)), this);
  263. }
  264. using _Base::merge;
  265. #endif // C++17
  266. #if __cplusplus >= 201103L
  267. iterator
  268. erase(const_iterator __position)
  269. {
  270. __glibcxx_check_erase(__position);
  271. this->_M_invalidate_if(_Equal(__position.base()));
  272. return iterator(_Base::erase(__position.base()), this);
  273. }
  274. #else
  275. void
  276. erase(iterator __position)
  277. {
  278. __glibcxx_check_erase(__position);
  279. this->_M_invalidate_if(_Equal(__position.base()));
  280. _Base::erase(__position.base());
  281. }
  282. #endif
  283. size_type
  284. erase(const key_type& __x)
  285. {
  286. std::pair<_Base_iterator, _Base_iterator> __victims =
  287. _Base::equal_range(__x);
  288. size_type __count = 0;
  289. _Base_iterator __victim = __victims.first;
  290. while (__victim != __victims.second)
  291. {
  292. this->_M_invalidate_if(_Equal(__victim));
  293. _Base::erase(__victim++);
  294. ++__count;
  295. }
  296. return __count;
  297. }
  298. #if __cplusplus >= 201103L
  299. iterator
  300. erase(const_iterator __first, const_iterator __last)
  301. {
  302. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  303. // 151. can't currently clear() empty container
  304. __glibcxx_check_erase_range(__first, __last);
  305. for (_Base_const_iterator __victim = __first.base();
  306. __victim != __last.base(); ++__victim)
  307. {
  308. _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
  309. _M_message(__gnu_debug::__msg_valid_range)
  310. ._M_iterator(__first, "first")
  311. ._M_iterator(__last, "last"));
  312. this->_M_invalidate_if(_Equal(__victim));
  313. }
  314. return iterator(_Base::erase(__first.base(), __last.base()), this);
  315. }
  316. #else
  317. void
  318. erase(iterator __first, iterator __last)
  319. {
  320. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  321. // 151. can't currently clear() empty container
  322. __glibcxx_check_erase_range(__first, __last);
  323. for (_Base_iterator __victim = __first.base();
  324. __victim != __last.base(); ++__victim)
  325. {
  326. _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
  327. _M_message(__gnu_debug::__msg_valid_range)
  328. ._M_iterator(__first, "first")
  329. ._M_iterator(__last, "last"));
  330. this->_M_invalidate_if(_Equal(__victim));
  331. }
  332. _Base::erase(__first.base(), __last.base());
  333. }
  334. #endif
  335. void
  336. swap(multiset& __x)
  337. _GLIBCXX_NOEXCEPT_IF( noexcept(declval<_Base&>().swap(__x)) )
  338. {
  339. _Safe::_M_swap(__x);
  340. _Base::swap(__x);
  341. }
  342. void
  343. clear() _GLIBCXX_NOEXCEPT
  344. {
  345. this->_M_invalidate_all();
  346. _Base::clear();
  347. }
  348. // observers:
  349. using _Base::key_comp;
  350. using _Base::value_comp;
  351. // multiset operations:
  352. iterator
  353. find(const key_type& __x)
  354. { return iterator(_Base::find(__x), this); }
  355. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  356. // 214. set::find() missing const overload
  357. const_iterator
  358. find(const key_type& __x) const
  359. { return const_iterator(_Base::find(__x), this); }
  360. #if __cplusplus > 201103L
  361. template<typename _Kt,
  362. typename _Req =
  363. typename __has_is_transparent<_Compare, _Kt>::type>
  364. iterator
  365. find(const _Kt& __x)
  366. { return { _Base::find(__x), this }; }
  367. template<typename _Kt,
  368. typename _Req =
  369. typename __has_is_transparent<_Compare, _Kt>::type>
  370. const_iterator
  371. find(const _Kt& __x) const
  372. { return { _Base::find(__x), this }; }
  373. #endif
  374. using _Base::count;
  375. iterator
  376. lower_bound(const key_type& __x)
  377. { return iterator(_Base::lower_bound(__x), this); }
  378. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  379. // 214. set::find() missing const overload
  380. const_iterator
  381. lower_bound(const key_type& __x) const
  382. { return const_iterator(_Base::lower_bound(__x), this); }
  383. #if __cplusplus > 201103L
  384. template<typename _Kt,
  385. typename _Req =
  386. typename __has_is_transparent<_Compare, _Kt>::type>
  387. iterator
  388. lower_bound(const _Kt& __x)
  389. { return { _Base::lower_bound(__x), this }; }
  390. template<typename _Kt,
  391. typename _Req =
  392. typename __has_is_transparent<_Compare, _Kt>::type>
  393. const_iterator
  394. lower_bound(const _Kt& __x) const
  395. { return { _Base::lower_bound(__x), this }; }
  396. #endif
  397. iterator
  398. upper_bound(const key_type& __x)
  399. { return iterator(_Base::upper_bound(__x), this); }
  400. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  401. // 214. set::find() missing const overload
  402. const_iterator
  403. upper_bound(const key_type& __x) const
  404. { return const_iterator(_Base::upper_bound(__x), this); }
  405. #if __cplusplus > 201103L
  406. template<typename _Kt,
  407. typename _Req =
  408. typename __has_is_transparent<_Compare, _Kt>::type>
  409. iterator
  410. upper_bound(const _Kt& __x)
  411. { return { _Base::upper_bound(__x), this }; }
  412. template<typename _Kt,
  413. typename _Req =
  414. typename __has_is_transparent<_Compare, _Kt>::type>
  415. const_iterator
  416. upper_bound(const _Kt& __x) const
  417. { return { _Base::upper_bound(__x), this }; }
  418. #endif
  419. std::pair<iterator,iterator>
  420. equal_range(const key_type& __x)
  421. {
  422. std::pair<_Base_iterator, _Base_iterator> __res =
  423. _Base::equal_range(__x);
  424. return std::make_pair(iterator(__res.first, this),
  425. iterator(__res.second, this));
  426. }
  427. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  428. // 214. set::find() missing const overload
  429. std::pair<const_iterator,const_iterator>
  430. equal_range(const key_type& __x) const
  431. {
  432. std::pair<_Base_const_iterator, _Base_const_iterator> __res =
  433. _Base::equal_range(__x);
  434. return std::make_pair(const_iterator(__res.first, this),
  435. const_iterator(__res.second, this));
  436. }
  437. #if __cplusplus > 201103L
  438. template<typename _Kt,
  439. typename _Req =
  440. typename __has_is_transparent<_Compare, _Kt>::type>
  441. std::pair<iterator, iterator>
  442. equal_range(const _Kt& __x)
  443. {
  444. auto __res = _Base::equal_range(__x);
  445. return { { __res.first, this }, { __res.second, this } };
  446. }
  447. template<typename _Kt,
  448. typename _Req =
  449. typename __has_is_transparent<_Compare, _Kt>::type>
  450. std::pair<const_iterator, const_iterator>
  451. equal_range(const _Kt& __x) const
  452. {
  453. auto __res = _Base::equal_range(__x);
  454. return { { __res.first, this }, { __res.second, this } };
  455. }
  456. #endif
  457. _Base&
  458. _M_base() _GLIBCXX_NOEXCEPT { return *this; }
  459. const _Base&
  460. _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
  461. };
  462. #if __cpp_deduction_guides >= 201606
  463. template<typename _InputIterator,
  464. typename _Compare =
  465. less<typename iterator_traits<_InputIterator>::value_type>,
  466. typename _Allocator =
  467. allocator<typename iterator_traits<_InputIterator>::value_type>,
  468. typename = _RequireInputIter<_InputIterator>,
  469. typename = _RequireAllocator<_Allocator>>
  470. multiset(_InputIterator, _InputIterator,
  471. _Compare = _Compare(), _Allocator = _Allocator())
  472. -> multiset<typename iterator_traits<_InputIterator>::value_type,
  473. _Compare, _Allocator>;
  474. template<typename _Key,
  475. typename _Compare = less<_Key>,
  476. typename _Allocator = allocator<_Key>,
  477. typename = _RequireAllocator<_Allocator>>
  478. multiset(initializer_list<_Key>,
  479. _Compare = _Compare(), _Allocator = _Allocator())
  480. -> multiset<_Key, _Compare, _Allocator>;
  481. template<typename _InputIterator, typename _Allocator,
  482. typename = _RequireInputIter<_InputIterator>,
  483. typename = _RequireAllocator<_Allocator>>
  484. multiset(_InputIterator, _InputIterator, _Allocator)
  485. -> multiset<typename iterator_traits<_InputIterator>::value_type,
  486. less<typename iterator_traits<_InputIterator>::value_type>,
  487. _Allocator>;
  488. template<typename _Key, typename _Allocator,
  489. typename = _RequireAllocator<_Allocator>>
  490. multiset(initializer_list<_Key>, _Allocator)
  491. -> multiset<_Key, less<_Key>, _Allocator>;
  492. #endif
  493. template<typename _Key, typename _Compare, typename _Allocator>
  494. inline bool
  495. operator==(const multiset<_Key, _Compare, _Allocator>& __lhs,
  496. const multiset<_Key, _Compare, _Allocator>& __rhs)
  497. { return __lhs._M_base() == __rhs._M_base(); }
  498. template<typename _Key, typename _Compare, typename _Allocator>
  499. inline bool
  500. operator!=(const multiset<_Key, _Compare, _Allocator>& __lhs,
  501. const multiset<_Key, _Compare, _Allocator>& __rhs)
  502. { return __lhs._M_base() != __rhs._M_base(); }
  503. template<typename _Key, typename _Compare, typename _Allocator>
  504. inline bool
  505. operator<(const multiset<_Key, _Compare, _Allocator>& __lhs,
  506. const multiset<_Key, _Compare, _Allocator>& __rhs)
  507. { return __lhs._M_base() < __rhs._M_base(); }
  508. template<typename _Key, typename _Compare, typename _Allocator>
  509. inline bool
  510. operator<=(const multiset<_Key, _Compare, _Allocator>& __lhs,
  511. const multiset<_Key, _Compare, _Allocator>& __rhs)
  512. { return __lhs._M_base() <= __rhs._M_base(); }
  513. template<typename _Key, typename _Compare, typename _Allocator>
  514. inline bool
  515. operator>=(const multiset<_Key, _Compare, _Allocator>& __lhs,
  516. const multiset<_Key, _Compare, _Allocator>& __rhs)
  517. { return __lhs._M_base() >= __rhs._M_base(); }
  518. template<typename _Key, typename _Compare, typename _Allocator>
  519. inline bool
  520. operator>(const multiset<_Key, _Compare, _Allocator>& __lhs,
  521. const multiset<_Key, _Compare, _Allocator>& __rhs)
  522. { return __lhs._M_base() > __rhs._M_base(); }
  523. template<typename _Key, typename _Compare, typename _Allocator>
  524. void
  525. swap(multiset<_Key, _Compare, _Allocator>& __x,
  526. multiset<_Key, _Compare, _Allocator>& __y)
  527. _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
  528. { return __x.swap(__y); }
  529. } // namespace __debug
  530. } // namespace std
  531. #endif