unordered_set 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. // Debugging unordered_set/unordered_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/unordered_set
  21. * This file is a GNU debug extension to the Standard C++ Library.
  22. */
  23. #ifndef _GLIBCXX_DEBUG_UNORDERED_SET
  24. #define _GLIBCXX_DEBUG_UNORDERED_SET 1
  25. #pragma GCC system_header
  26. #if __cplusplus < 201103L
  27. # include <bits/c++0x_warning.h>
  28. #else
  29. # include <bits/c++config.h>
  30. namespace std _GLIBCXX_VISIBILITY(default) { namespace __debug {
  31. template<typename _Key, typename _Hash, typename _Pred, typename _Allocator>
  32. class unordered_set;
  33. template<typename _Key, typename _Hash, typename _Pred, typename _Allocator>
  34. class unordered_multiset;
  35. } } // namespace std::__debug
  36. # include <unordered_set>
  37. #include <debug/safe_unordered_container.h>
  38. #include <debug/safe_container.h>
  39. #include <debug/safe_iterator.h>
  40. #include <debug/safe_local_iterator.h>
  41. namespace std _GLIBCXX_VISIBILITY(default)
  42. {
  43. namespace __debug
  44. {
  45. /// Class std::unordered_set with safety/checking/debug instrumentation.
  46. template<typename _Value,
  47. typename _Hash = std::hash<_Value>,
  48. typename _Pred = std::equal_to<_Value>,
  49. typename _Alloc = std::allocator<_Value> >
  50. class unordered_set
  51. : public __gnu_debug::_Safe_container<
  52. unordered_set<_Value, _Hash, _Pred, _Alloc>, _Alloc,
  53. __gnu_debug::_Safe_unordered_container>,
  54. public _GLIBCXX_STD_C::unordered_set<_Value, _Hash, _Pred, _Alloc>
  55. {
  56. typedef _GLIBCXX_STD_C::unordered_set<
  57. _Value, _Hash, _Pred, _Alloc> _Base;
  58. typedef __gnu_debug::_Safe_container<
  59. unordered_set, _Alloc, __gnu_debug::_Safe_unordered_container> _Safe;
  60. typedef typename _Base::const_iterator _Base_const_iterator;
  61. typedef typename _Base::iterator _Base_iterator;
  62. typedef typename _Base::const_local_iterator _Base_const_local_iterator;
  63. typedef typename _Base::local_iterator _Base_local_iterator;
  64. template<typename _ItT, typename _SeqT, typename _CatT>
  65. friend class ::__gnu_debug::_Safe_iterator;
  66. template<typename _ItT, typename _SeqT>
  67. friend class ::__gnu_debug::_Safe_local_iterator;
  68. public:
  69. typedef typename _Base::size_type size_type;
  70. typedef typename _Base::hasher hasher;
  71. typedef typename _Base::key_equal key_equal;
  72. typedef typename _Base::allocator_type allocator_type;
  73. typedef typename _Base::key_type key_type;
  74. typedef typename _Base::value_type value_type;
  75. typedef __gnu_debug::_Safe_iterator<
  76. _Base_iterator, unordered_set> iterator;
  77. typedef __gnu_debug::_Safe_iterator<
  78. _Base_const_iterator, unordered_set> const_iterator;
  79. typedef __gnu_debug::_Safe_local_iterator<
  80. _Base_local_iterator, unordered_set> local_iterator;
  81. typedef __gnu_debug::_Safe_local_iterator<
  82. _Base_const_local_iterator, unordered_set> const_local_iterator;
  83. unordered_set() = default;
  84. explicit
  85. unordered_set(size_type __n,
  86. const hasher& __hf = hasher(),
  87. const key_equal& __eql = key_equal(),
  88. const allocator_type& __a = allocator_type())
  89. : _Base(__n, __hf, __eql, __a) { }
  90. template<typename _InputIterator>
  91. unordered_set(_InputIterator __first, _InputIterator __last,
  92. size_type __n = 0,
  93. const hasher& __hf = hasher(),
  94. const key_equal& __eql = key_equal(),
  95. const allocator_type& __a = allocator_type())
  96. : _Base(__gnu_debug::__base(
  97. __glibcxx_check_valid_constructor_range(__first, __last)),
  98. __gnu_debug::__base(__last), __n,
  99. __hf, __eql, __a) { }
  100. unordered_set(const unordered_set&) = default;
  101. unordered_set(const _Base& __x)
  102. : _Base(__x) { }
  103. unordered_set(unordered_set&&) = default;
  104. explicit
  105. unordered_set(const allocator_type& __a)
  106. : _Base(__a) { }
  107. unordered_set(const unordered_set& __uset,
  108. const allocator_type& __a)
  109. : _Base(__uset, __a) { }
  110. unordered_set(unordered_set&& __uset,
  111. const allocator_type& __a)
  112. : _Safe(std::move(__uset._M_safe()), __a),
  113. _Base(std::move(__uset._M_base()), __a) { }
  114. unordered_set(initializer_list<value_type> __l,
  115. size_type __n = 0,
  116. const hasher& __hf = hasher(),
  117. const key_equal& __eql = key_equal(),
  118. const allocator_type& __a = allocator_type())
  119. : _Base(__l, __n, __hf, __eql, __a) { }
  120. unordered_set(size_type __n, const allocator_type& __a)
  121. : unordered_set(__n, hasher(), key_equal(), __a)
  122. { }
  123. unordered_set(size_type __n, const hasher& __hf,
  124. const allocator_type& __a)
  125. : unordered_set(__n, __hf, key_equal(), __a)
  126. { }
  127. template<typename _InputIterator>
  128. unordered_set(_InputIterator __first, _InputIterator __last,
  129. size_type __n,
  130. const allocator_type& __a)
  131. : unordered_set(__first, __last, __n, hasher(), key_equal(), __a)
  132. { }
  133. template<typename _InputIterator>
  134. unordered_set(_InputIterator __first, _InputIterator __last,
  135. size_type __n, const hasher& __hf,
  136. const allocator_type& __a)
  137. : unordered_set(__first, __last, __n, __hf, key_equal(), __a)
  138. { }
  139. unordered_set(initializer_list<value_type> __l,
  140. size_type __n,
  141. const allocator_type& __a)
  142. : unordered_set(__l, __n, hasher(), key_equal(), __a)
  143. { }
  144. unordered_set(initializer_list<value_type> __l,
  145. size_type __n, const hasher& __hf,
  146. const allocator_type& __a)
  147. : unordered_set(__l, __n, __hf, key_equal(), __a)
  148. { }
  149. ~unordered_set() = default;
  150. unordered_set&
  151. operator=(const unordered_set&) = default;
  152. unordered_set&
  153. operator=(unordered_set&&) = default;
  154. unordered_set&
  155. operator=(initializer_list<value_type> __l)
  156. {
  157. _M_base() = __l;
  158. this->_M_invalidate_all();
  159. return *this;
  160. }
  161. void
  162. swap(unordered_set& __x)
  163. noexcept( noexcept(declval<_Base&>().swap(__x)) )
  164. {
  165. _Safe::_M_swap(__x);
  166. _Base::swap(__x);
  167. }
  168. void
  169. clear() noexcept
  170. {
  171. _Base::clear();
  172. this->_M_invalidate_all();
  173. }
  174. iterator
  175. begin() noexcept
  176. { return { _Base::begin(), this }; }
  177. const_iterator
  178. begin() const noexcept
  179. { return { _Base::begin(), this }; }
  180. iterator
  181. end() noexcept
  182. { return { _Base::end(), this }; }
  183. const_iterator
  184. end() const noexcept
  185. { return { _Base::end(), this }; }
  186. const_iterator
  187. cbegin() const noexcept
  188. { return { _Base::cbegin(), this }; }
  189. const_iterator
  190. cend() const noexcept
  191. { return { _Base::cend(), this }; }
  192. // local versions
  193. local_iterator
  194. begin(size_type __b)
  195. {
  196. __glibcxx_check_bucket_index(__b);
  197. return { _Base::begin(__b), this };
  198. }
  199. local_iterator
  200. end(size_type __b)
  201. {
  202. __glibcxx_check_bucket_index(__b);
  203. return { _Base::end(__b), this };
  204. }
  205. const_local_iterator
  206. begin(size_type __b) const
  207. {
  208. __glibcxx_check_bucket_index(__b);
  209. return { _Base::begin(__b), this };
  210. }
  211. const_local_iterator
  212. end(size_type __b) const
  213. {
  214. __glibcxx_check_bucket_index(__b);
  215. return { _Base::end(__b), this };
  216. }
  217. const_local_iterator
  218. cbegin(size_type __b) const
  219. {
  220. __glibcxx_check_bucket_index(__b);
  221. return { _Base::cbegin(__b), this };
  222. }
  223. const_local_iterator
  224. cend(size_type __b) const
  225. {
  226. __glibcxx_check_bucket_index(__b);
  227. return { _Base::cend(__b), this };
  228. }
  229. size_type
  230. bucket_size(size_type __b) const
  231. {
  232. __glibcxx_check_bucket_index(__b);
  233. return _Base::bucket_size(__b);
  234. }
  235. float
  236. max_load_factor() const noexcept
  237. { return _Base::max_load_factor(); }
  238. void
  239. max_load_factor(float __f)
  240. {
  241. __glibcxx_check_max_load_factor(__f);
  242. _Base::max_load_factor(__f);
  243. }
  244. template<typename... _Args>
  245. std::pair<iterator, bool>
  246. emplace(_Args&&... __args)
  247. {
  248. size_type __bucket_count = this->bucket_count();
  249. auto __res = _Base::emplace(std::forward<_Args>(__args)...);
  250. _M_check_rehashed(__bucket_count);
  251. return { { __res.first, this }, __res.second };
  252. }
  253. template<typename... _Args>
  254. iterator
  255. emplace_hint(const_iterator __hint, _Args&&... __args)
  256. {
  257. __glibcxx_check_insert(__hint);
  258. size_type __bucket_count = this->bucket_count();
  259. auto __it = _Base::emplace_hint(__hint.base(),
  260. std::forward<_Args>(__args)...);
  261. _M_check_rehashed(__bucket_count);
  262. return { __it, this };
  263. }
  264. std::pair<iterator, bool>
  265. insert(const value_type& __obj)
  266. {
  267. size_type __bucket_count = this->bucket_count();
  268. auto __res = _Base::insert(__obj);
  269. _M_check_rehashed(__bucket_count);
  270. return { { __res.first, this }, __res.second };
  271. }
  272. iterator
  273. insert(const_iterator __hint, const value_type& __obj)
  274. {
  275. __glibcxx_check_insert(__hint);
  276. size_type __bucket_count = this->bucket_count();
  277. auto __it = _Base::insert(__hint.base(), __obj);
  278. _M_check_rehashed(__bucket_count);
  279. return { __it, this };
  280. }
  281. std::pair<iterator, bool>
  282. insert(value_type&& __obj)
  283. {
  284. size_type __bucket_count = this->bucket_count();
  285. auto __res = _Base::insert(std::move(__obj));
  286. _M_check_rehashed(__bucket_count);
  287. return { { __res.first, this }, __res.second };
  288. }
  289. iterator
  290. insert(const_iterator __hint, value_type&& __obj)
  291. {
  292. __glibcxx_check_insert(__hint);
  293. size_type __bucket_count = this->bucket_count();
  294. auto __it = _Base::insert(__hint.base(), std::move(__obj));
  295. _M_check_rehashed(__bucket_count);
  296. return { __it, this };
  297. }
  298. void
  299. insert(std::initializer_list<value_type> __l)
  300. {
  301. size_type __bucket_count = this->bucket_count();
  302. _Base::insert(__l);
  303. _M_check_rehashed(__bucket_count);
  304. }
  305. template<typename _InputIterator>
  306. void
  307. insert(_InputIterator __first, _InputIterator __last)
  308. {
  309. typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
  310. __glibcxx_check_valid_range2(__first, __last, __dist);
  311. size_type __bucket_count = this->bucket_count();
  312. if (__dist.second >= __gnu_debug::__dp_sign)
  313. _Base::insert(__gnu_debug::__unsafe(__first),
  314. __gnu_debug::__unsafe(__last));
  315. else
  316. _Base::insert(__first, __last);
  317. _M_check_rehashed(__bucket_count);
  318. }
  319. #if __cplusplus > 201402L
  320. using node_type = typename _Base::node_type;
  321. using insert_return_type = _Node_insert_return<iterator, node_type>;
  322. node_type
  323. extract(const_iterator __position)
  324. {
  325. __glibcxx_check_erase(__position);
  326. return _M_extract(__position.base());
  327. }
  328. node_type
  329. extract(const key_type& __key)
  330. {
  331. const auto __position = _Base::find(__key);
  332. if (__position != _Base::end())
  333. return _M_extract(__position);
  334. return {};
  335. }
  336. insert_return_type
  337. insert(node_type&& __nh)
  338. {
  339. auto __ret = _Base::insert(std::move(__nh));
  340. return
  341. { { __ret.position, this }, __ret.inserted, std::move(__ret.node) };
  342. }
  343. iterator
  344. insert(const_iterator __hint, node_type&& __nh)
  345. {
  346. __glibcxx_check_insert(__hint);
  347. return { _Base::insert(__hint.base(), std::move(__nh)), this };
  348. }
  349. using _Base::merge;
  350. #endif // C++17
  351. iterator
  352. find(const key_type& __key)
  353. { return { _Base::find(__key), this }; }
  354. const_iterator
  355. find(const key_type& __key) const
  356. { return { _Base::find(__key), this }; }
  357. std::pair<iterator, iterator>
  358. equal_range(const key_type& __key)
  359. {
  360. auto __res = _Base::equal_range(__key);
  361. return { { __res.first, this }, { __res.second, this } };
  362. }
  363. std::pair<const_iterator, const_iterator>
  364. equal_range(const key_type& __key) const
  365. {
  366. auto __res = _Base::equal_range(__key);
  367. return { { __res.first, this }, { __res.second, this } };
  368. }
  369. size_type
  370. erase(const key_type& __key)
  371. {
  372. size_type __ret(0);
  373. auto __victim = _Base::find(__key);
  374. if (__victim != _Base::end())
  375. {
  376. _M_erase(__victim);
  377. __ret = 1;
  378. }
  379. return __ret;
  380. }
  381. iterator
  382. erase(const_iterator __it)
  383. {
  384. __glibcxx_check_erase(__it);
  385. return { _M_erase(__it.base()), this };
  386. }
  387. iterator
  388. erase(iterator __it)
  389. {
  390. __glibcxx_check_erase(__it);
  391. return { _M_erase(__it.base()), this };
  392. }
  393. iterator
  394. erase(const_iterator __first, const_iterator __last)
  395. {
  396. __glibcxx_check_erase_range(__first, __last);
  397. for (auto __tmp = __first.base(); __tmp != __last.base(); ++__tmp)
  398. {
  399. _GLIBCXX_DEBUG_VERIFY(__tmp != _Base::cend(),
  400. _M_message(__gnu_debug::__msg_valid_range)
  401. ._M_iterator(__first, "first")
  402. ._M_iterator(__last, "last"));
  403. _M_invalidate(__tmp);
  404. }
  405. size_type __bucket_count = this->bucket_count();
  406. auto __next = _Base::erase(__first.base(), __last.base());
  407. _M_check_rehashed(__bucket_count);
  408. return { __next, this };
  409. }
  410. _Base&
  411. _M_base() noexcept { return *this; }
  412. const _Base&
  413. _M_base() const noexcept { return *this; }
  414. private:
  415. void
  416. _M_check_rehashed(size_type __prev_count)
  417. {
  418. if (__prev_count != this->bucket_count())
  419. this->_M_invalidate_all();
  420. }
  421. void
  422. _M_invalidate(_Base_const_iterator __victim)
  423. {
  424. this->_M_invalidate_if(
  425. [__victim](_Base_const_iterator __it) { return __it == __victim; });
  426. this->_M_invalidate_local_if(
  427. [__victim](_Base_const_local_iterator __it)
  428. { return __it._M_curr() == __victim._M_cur; });
  429. }
  430. _Base_iterator
  431. _M_erase(_Base_const_iterator __victim)
  432. {
  433. _M_invalidate(__victim);
  434. size_type __bucket_count = this->bucket_count();
  435. _Base_iterator __next = _Base::erase(__victim);
  436. _M_check_rehashed(__bucket_count);
  437. return __next;
  438. }
  439. #if __cplusplus > 201402L
  440. node_type
  441. _M_extract(_Base_const_iterator __victim)
  442. {
  443. _M_invalidate(__victim);
  444. return _Base::extract(__victim);
  445. }
  446. #endif
  447. };
  448. #if __cpp_deduction_guides >= 201606
  449. template<typename _InputIterator,
  450. typename _Hash =
  451. hash<typename iterator_traits<_InputIterator>::value_type>,
  452. typename _Pred =
  453. equal_to<typename iterator_traits<_InputIterator>::value_type>,
  454. typename _Allocator =
  455. allocator<typename iterator_traits<_InputIterator>::value_type>,
  456. typename = _RequireInputIter<_InputIterator>,
  457. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  458. typename = _RequireNotAllocator<_Pred>,
  459. typename = _RequireAllocator<_Allocator>>
  460. unordered_set(_InputIterator, _InputIterator,
  461. unordered_set<int>::size_type = {},
  462. _Hash = _Hash(), _Pred = _Pred(), _Allocator = _Allocator())
  463. -> unordered_set<typename iterator_traits<_InputIterator>::value_type,
  464. _Hash, _Pred, _Allocator>;
  465. template<typename _Tp, typename _Hash = hash<_Tp>,
  466. typename _Pred = equal_to<_Tp>,
  467. typename _Allocator = allocator<_Tp>,
  468. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  469. typename = _RequireNotAllocator<_Pred>,
  470. typename = _RequireAllocator<_Allocator>>
  471. unordered_set(initializer_list<_Tp>,
  472. unordered_set<int>::size_type = {},
  473. _Hash = _Hash(), _Pred = _Pred(), _Allocator = _Allocator())
  474. -> unordered_set<_Tp, _Hash, _Pred, _Allocator>;
  475. template<typename _InputIterator, typename _Allocator,
  476. typename = _RequireInputIter<_InputIterator>,
  477. typename = _RequireAllocator<_Allocator>>
  478. unordered_set(_InputIterator, _InputIterator,
  479. unordered_set<int>::size_type, _Allocator)
  480. -> unordered_set<typename iterator_traits<_InputIterator>::value_type,
  481. hash<
  482. typename iterator_traits<_InputIterator>::value_type>,
  483. equal_to<
  484. typename iterator_traits<_InputIterator>::value_type>,
  485. _Allocator>;
  486. template<typename _InputIterator, typename _Hash, typename _Allocator,
  487. typename = _RequireInputIter<_InputIterator>,
  488. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  489. typename = _RequireAllocator<_Allocator>>
  490. unordered_set(_InputIterator, _InputIterator,
  491. unordered_set<int>::size_type,
  492. _Hash, _Allocator)
  493. -> unordered_set<typename iterator_traits<_InputIterator>::value_type,
  494. _Hash,
  495. equal_to<
  496. typename iterator_traits<_InputIterator>::value_type>,
  497. _Allocator>;
  498. template<typename _Tp, typename _Allocator,
  499. typename = _RequireAllocator<_Allocator>>
  500. unordered_set(initializer_list<_Tp>,
  501. unordered_set<int>::size_type, _Allocator)
  502. -> unordered_set<_Tp, hash<_Tp>, equal_to<_Tp>, _Allocator>;
  503. template<typename _Tp, typename _Hash, typename _Allocator,
  504. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  505. typename = _RequireAllocator<_Allocator>>
  506. unordered_set(initializer_list<_Tp>,
  507. unordered_set<int>::size_type, _Hash, _Allocator)
  508. -> unordered_set<_Tp, _Hash, equal_to<_Tp>, _Allocator>;
  509. #endif
  510. template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
  511. inline void
  512. swap(unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
  513. unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
  514. noexcept(noexcept(__x.swap(__y)))
  515. { __x.swap(__y); }
  516. template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
  517. inline bool
  518. operator==(const unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
  519. const unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
  520. { return __x._M_base() == __y._M_base(); }
  521. #if __cpp_impl_three_way_comparison < 201907L
  522. template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
  523. inline bool
  524. operator!=(const unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
  525. const unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
  526. { return !(__x == __y); }
  527. #endif
  528. /// Class std::unordered_multiset with safety/checking/debug instrumentation.
  529. template<typename _Value,
  530. typename _Hash = std::hash<_Value>,
  531. typename _Pred = std::equal_to<_Value>,
  532. typename _Alloc = std::allocator<_Value> >
  533. class unordered_multiset
  534. : public __gnu_debug::_Safe_container<
  535. unordered_multiset<_Value, _Hash, _Pred, _Alloc>, _Alloc,
  536. __gnu_debug::_Safe_unordered_container>,
  537. public _GLIBCXX_STD_C::unordered_multiset<_Value, _Hash, _Pred, _Alloc>
  538. {
  539. typedef _GLIBCXX_STD_C::unordered_multiset<
  540. _Value, _Hash, _Pred, _Alloc> _Base;
  541. typedef __gnu_debug::_Safe_container<unordered_multiset,
  542. _Alloc, __gnu_debug::_Safe_unordered_container> _Safe;
  543. typedef typename _Base::const_iterator _Base_const_iterator;
  544. typedef typename _Base::iterator _Base_iterator;
  545. typedef typename _Base::const_local_iterator
  546. _Base_const_local_iterator;
  547. typedef typename _Base::local_iterator _Base_local_iterator;
  548. template<typename _ItT, typename _SeqT, typename _CatT>
  549. friend class ::__gnu_debug::_Safe_iterator;
  550. template<typename _ItT, typename _SeqT>
  551. friend class ::__gnu_debug::_Safe_local_iterator;
  552. public:
  553. typedef typename _Base::size_type size_type;
  554. typedef typename _Base::hasher hasher;
  555. typedef typename _Base::key_equal key_equal;
  556. typedef typename _Base::allocator_type allocator_type;
  557. typedef typename _Base::key_type key_type;
  558. typedef typename _Base::value_type value_type;
  559. typedef __gnu_debug::_Safe_iterator<
  560. _Base_iterator, unordered_multiset> iterator;
  561. typedef __gnu_debug::_Safe_iterator<
  562. _Base_const_iterator, unordered_multiset> const_iterator;
  563. typedef __gnu_debug::_Safe_local_iterator<
  564. _Base_local_iterator, unordered_multiset> local_iterator;
  565. typedef __gnu_debug::_Safe_local_iterator<
  566. _Base_const_local_iterator, unordered_multiset> const_local_iterator;
  567. unordered_multiset() = default;
  568. explicit
  569. unordered_multiset(size_type __n,
  570. const hasher& __hf = hasher(),
  571. const key_equal& __eql = key_equal(),
  572. const allocator_type& __a = allocator_type())
  573. : _Base(__n, __hf, __eql, __a) { }
  574. template<typename _InputIterator>
  575. unordered_multiset(_InputIterator __first, _InputIterator __last,
  576. size_type __n = 0,
  577. const hasher& __hf = hasher(),
  578. const key_equal& __eql = key_equal(),
  579. const allocator_type& __a = allocator_type())
  580. : _Base(__gnu_debug::__base(
  581. __glibcxx_check_valid_constructor_range(__first, __last)),
  582. __gnu_debug::__base(__last), __n,
  583. __hf, __eql, __a) { }
  584. unordered_multiset(const unordered_multiset&) = default;
  585. unordered_multiset(const _Base& __x)
  586. : _Base(__x) { }
  587. unordered_multiset(unordered_multiset&&) = default;
  588. explicit
  589. unordered_multiset(const allocator_type& __a)
  590. : _Base(__a) { }
  591. unordered_multiset(const unordered_multiset& __uset,
  592. const allocator_type& __a)
  593. : _Base(__uset, __a) { }
  594. unordered_multiset(unordered_multiset&& __uset,
  595. const allocator_type& __a)
  596. : _Safe(std::move(__uset._M_safe()), __a),
  597. _Base(std::move(__uset._M_base()), __a) { }
  598. unordered_multiset(initializer_list<value_type> __l,
  599. size_type __n = 0,
  600. const hasher& __hf = hasher(),
  601. const key_equal& __eql = key_equal(),
  602. const allocator_type& __a = allocator_type())
  603. : _Base(__l, __n, __hf, __eql, __a) { }
  604. unordered_multiset(size_type __n, const allocator_type& __a)
  605. : unordered_multiset(__n, hasher(), key_equal(), __a)
  606. { }
  607. unordered_multiset(size_type __n, const hasher& __hf,
  608. const allocator_type& __a)
  609. : unordered_multiset(__n, __hf, key_equal(), __a)
  610. { }
  611. template<typename _InputIterator>
  612. unordered_multiset(_InputIterator __first, _InputIterator __last,
  613. size_type __n,
  614. const allocator_type& __a)
  615. : unordered_multiset(__first, __last, __n, hasher(), key_equal(), __a)
  616. { }
  617. template<typename _InputIterator>
  618. unordered_multiset(_InputIterator __first, _InputIterator __last,
  619. size_type __n, const hasher& __hf,
  620. const allocator_type& __a)
  621. : unordered_multiset(__first, __last, __n, __hf, key_equal(), __a)
  622. { }
  623. unordered_multiset(initializer_list<value_type> __l,
  624. size_type __n,
  625. const allocator_type& __a)
  626. : unordered_multiset(__l, __n, hasher(), key_equal(), __a)
  627. { }
  628. unordered_multiset(initializer_list<value_type> __l,
  629. size_type __n, const hasher& __hf,
  630. const allocator_type& __a)
  631. : unordered_multiset(__l, __n, __hf, key_equal(), __a)
  632. { }
  633. ~unordered_multiset() = default;
  634. unordered_multiset&
  635. operator=(const unordered_multiset&) = default;
  636. unordered_multiset&
  637. operator=(unordered_multiset&&) = default;
  638. unordered_multiset&
  639. operator=(initializer_list<value_type> __l)
  640. {
  641. this->_M_base() = __l;
  642. this->_M_invalidate_all();
  643. return *this;
  644. }
  645. void
  646. swap(unordered_multiset& __x)
  647. noexcept( noexcept(declval<_Base&>().swap(__x)) )
  648. {
  649. _Safe::_M_swap(__x);
  650. _Base::swap(__x);
  651. }
  652. void
  653. clear() noexcept
  654. {
  655. _Base::clear();
  656. this->_M_invalidate_all();
  657. }
  658. iterator
  659. begin() noexcept
  660. { return { _Base::begin(), this }; }
  661. const_iterator
  662. begin() const noexcept
  663. { return { _Base::begin(), this }; }
  664. iterator
  665. end() noexcept
  666. { return { _Base::end(), this }; }
  667. const_iterator
  668. end() const noexcept
  669. { return { _Base::end(), this }; }
  670. const_iterator
  671. cbegin() const noexcept
  672. { return { _Base::cbegin(), this }; }
  673. const_iterator
  674. cend() const noexcept
  675. { return { _Base::cend(), this }; }
  676. // local versions
  677. local_iterator
  678. begin(size_type __b)
  679. {
  680. __glibcxx_check_bucket_index(__b);
  681. return { _Base::begin(__b), this };
  682. }
  683. local_iterator
  684. end(size_type __b)
  685. {
  686. __glibcxx_check_bucket_index(__b);
  687. return { _Base::end(__b), this };
  688. }
  689. const_local_iterator
  690. begin(size_type __b) const
  691. {
  692. __glibcxx_check_bucket_index(__b);
  693. return { _Base::begin(__b), this };
  694. }
  695. const_local_iterator
  696. end(size_type __b) const
  697. {
  698. __glibcxx_check_bucket_index(__b);
  699. return { _Base::end(__b), this };
  700. }
  701. const_local_iterator
  702. cbegin(size_type __b) const
  703. {
  704. __glibcxx_check_bucket_index(__b);
  705. return { _Base::cbegin(__b), this };
  706. }
  707. const_local_iterator
  708. cend(size_type __b) const
  709. {
  710. __glibcxx_check_bucket_index(__b);
  711. return { _Base::cend(__b), this };
  712. }
  713. size_type
  714. bucket_size(size_type __b) const
  715. {
  716. __glibcxx_check_bucket_index(__b);
  717. return _Base::bucket_size(__b);
  718. }
  719. float
  720. max_load_factor() const noexcept
  721. { return _Base::max_load_factor(); }
  722. void
  723. max_load_factor(float __f)
  724. {
  725. __glibcxx_check_max_load_factor(__f);
  726. _Base::max_load_factor(__f);
  727. }
  728. template<typename... _Args>
  729. iterator
  730. emplace(_Args&&... __args)
  731. {
  732. size_type __bucket_count = this->bucket_count();
  733. auto __it = _Base::emplace(std::forward<_Args>(__args)...);
  734. _M_check_rehashed(__bucket_count);
  735. return { __it, this };
  736. }
  737. template<typename... _Args>
  738. iterator
  739. emplace_hint(const_iterator __hint, _Args&&... __args)
  740. {
  741. __glibcxx_check_insert(__hint);
  742. size_type __bucket_count = this->bucket_count();
  743. auto __it = _Base::emplace_hint(__hint.base(),
  744. std::forward<_Args>(__args)...);
  745. _M_check_rehashed(__bucket_count);
  746. return { __it, this };
  747. }
  748. iterator
  749. insert(const value_type& __obj)
  750. {
  751. size_type __bucket_count = this->bucket_count();
  752. auto __it = _Base::insert(__obj);
  753. _M_check_rehashed(__bucket_count);
  754. return { __it, this };
  755. }
  756. iterator
  757. insert(const_iterator __hint, const value_type& __obj)
  758. {
  759. __glibcxx_check_insert(__hint);
  760. size_type __bucket_count = this->bucket_count();
  761. auto __it = _Base::insert(__hint.base(), __obj);
  762. _M_check_rehashed(__bucket_count);
  763. return { __it, this };
  764. }
  765. iterator
  766. insert(value_type&& __obj)
  767. {
  768. size_type __bucket_count = this->bucket_count();
  769. auto __it = _Base::insert(std::move(__obj));
  770. _M_check_rehashed(__bucket_count);
  771. return { __it, this };
  772. }
  773. iterator
  774. insert(const_iterator __hint, value_type&& __obj)
  775. {
  776. __glibcxx_check_insert(__hint);
  777. size_type __bucket_count = this->bucket_count();
  778. auto __it = _Base::insert(__hint.base(), std::move(__obj));
  779. _M_check_rehashed(__bucket_count);
  780. return { __it, this };
  781. }
  782. void
  783. insert(std::initializer_list<value_type> __l)
  784. {
  785. size_type __bucket_count = this->bucket_count();
  786. _Base::insert(__l);
  787. _M_check_rehashed(__bucket_count);
  788. }
  789. template<typename _InputIterator>
  790. void
  791. insert(_InputIterator __first, _InputIterator __last)
  792. {
  793. typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
  794. __glibcxx_check_valid_range2(__first, __last, __dist);
  795. size_type __bucket_count = this->bucket_count();
  796. if (__dist.second >= __gnu_debug::__dp_sign)
  797. _Base::insert(__gnu_debug::__unsafe(__first),
  798. __gnu_debug::__unsafe(__last));
  799. else
  800. _Base::insert(__first, __last);
  801. _M_check_rehashed(__bucket_count);
  802. }
  803. #if __cplusplus > 201402L
  804. using node_type = typename _Base::node_type;
  805. node_type
  806. extract(const_iterator __position)
  807. {
  808. __glibcxx_check_erase(__position);
  809. return _M_extract(__position.base());
  810. }
  811. node_type
  812. extract(const key_type& __key)
  813. {
  814. const auto __position = _Base::find(__key);
  815. if (__position != _Base::end())
  816. return _M_extract(__position);
  817. return {};
  818. }
  819. iterator
  820. insert(node_type&& __nh)
  821. { return { _Base::insert(std::move(__nh)), this }; }
  822. iterator
  823. insert(const_iterator __hint, node_type&& __nh)
  824. {
  825. __glibcxx_check_insert(__hint);
  826. return { _Base::insert(__hint.base(), std::move(__nh)), this };
  827. }
  828. using _Base::merge;
  829. #endif // C++17
  830. iterator
  831. find(const key_type& __key)
  832. { return { _Base::find(__key), this }; }
  833. const_iterator
  834. find(const key_type& __key) const
  835. { return { _Base::find(__key), this }; }
  836. std::pair<iterator, iterator>
  837. equal_range(const key_type& __key)
  838. {
  839. auto __res = _Base::equal_range(__key);
  840. return { { __res.first, this }, { __res.second, this } };
  841. }
  842. std::pair<const_iterator, const_iterator>
  843. equal_range(const key_type& __key) const
  844. {
  845. auto __res = _Base::equal_range(__key);
  846. return { { __res.first, this }, { __res.second, this } };
  847. }
  848. size_type
  849. erase(const key_type& __key)
  850. {
  851. size_type __ret(0);
  852. auto __pair = _Base::equal_range(__key);
  853. for (auto __victim = __pair.first; __victim != __pair.second;)
  854. {
  855. _M_invalidate(__victim);
  856. __victim = _Base::erase(__victim);
  857. ++__ret;
  858. }
  859. return __ret;
  860. }
  861. iterator
  862. erase(const_iterator __it)
  863. {
  864. __glibcxx_check_erase(__it);
  865. return { _M_erase(__it.base()), this };
  866. }
  867. iterator
  868. erase(iterator __it)
  869. {
  870. __glibcxx_check_erase(__it);
  871. return { _M_erase(__it.base()), this };
  872. }
  873. iterator
  874. erase(const_iterator __first, const_iterator __last)
  875. {
  876. __glibcxx_check_erase_range(__first, __last);
  877. for (auto __tmp = __first.base(); __tmp != __last.base(); ++__tmp)
  878. {
  879. _GLIBCXX_DEBUG_VERIFY(__tmp != _Base::cend(),
  880. _M_message(__gnu_debug::__msg_valid_range)
  881. ._M_iterator(__first, "first")
  882. ._M_iterator(__last, "last"));
  883. _M_invalidate(__tmp);
  884. }
  885. return { _Base::erase(__first.base(), __last.base()), this };
  886. }
  887. _Base&
  888. _M_base() noexcept { return *this; }
  889. const _Base&
  890. _M_base() const noexcept { return *this; }
  891. private:
  892. void
  893. _M_check_rehashed(size_type __prev_count)
  894. {
  895. if (__prev_count != this->bucket_count())
  896. this->_M_invalidate_all();
  897. }
  898. void
  899. _M_invalidate(_Base_const_iterator __victim)
  900. {
  901. this->_M_invalidate_if(
  902. [__victim](_Base_const_iterator __it) { return __it == __victim; });
  903. this->_M_invalidate_local_if(
  904. [__victim](_Base_const_local_iterator __it)
  905. { return __it._M_curr() == __victim._M_cur; });
  906. }
  907. _Base_iterator
  908. _M_erase(_Base_const_iterator __victim)
  909. {
  910. _M_invalidate(__victim);
  911. size_type __bucket_count = this->bucket_count();
  912. _Base_iterator __next = _Base::erase(__victim);
  913. _M_check_rehashed(__bucket_count);
  914. return __next;
  915. }
  916. #if __cplusplus > 201402L
  917. node_type
  918. _M_extract(_Base_const_iterator __victim)
  919. {
  920. _M_invalidate(__victim);
  921. return _Base::extract(__victim);
  922. }
  923. #endif
  924. };
  925. #if __cpp_deduction_guides >= 201606
  926. template<typename _InputIterator,
  927. typename _Hash =
  928. hash<typename iterator_traits<_InputIterator>::value_type>,
  929. typename _Pred =
  930. equal_to<typename iterator_traits<_InputIterator>::value_type>,
  931. typename _Allocator =
  932. allocator<typename iterator_traits<_InputIterator>::value_type>,
  933. typename = _RequireInputIter<_InputIterator>,
  934. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  935. typename = _RequireNotAllocator<_Pred>,
  936. typename = _RequireAllocator<_Allocator>>
  937. unordered_multiset(_InputIterator, _InputIterator,
  938. unordered_multiset<int>::size_type = {},
  939. _Hash = _Hash(), _Pred = _Pred(),
  940. _Allocator = _Allocator())
  941. -> unordered_multiset<typename iterator_traits<_InputIterator>::value_type,
  942. _Hash, _Pred, _Allocator>;
  943. template<typename _Tp, typename _Hash = hash<_Tp>,
  944. typename _Pred = equal_to<_Tp>,
  945. typename _Allocator = allocator<_Tp>,
  946. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  947. typename = _RequireNotAllocator<_Pred>,
  948. typename = _RequireAllocator<_Allocator>>
  949. unordered_multiset(initializer_list<_Tp>,
  950. unordered_multiset<int>::size_type = {},
  951. _Hash = _Hash(), _Pred = _Pred(),
  952. _Allocator = _Allocator())
  953. -> unordered_multiset<_Tp, _Hash, _Pred, _Allocator>;
  954. template<typename _InputIterator, typename _Allocator,
  955. typename = _RequireInputIter<_InputIterator>,
  956. typename = _RequireAllocator<_Allocator>>
  957. unordered_multiset(_InputIterator, _InputIterator,
  958. unordered_multiset<int>::size_type, _Allocator)
  959. -> unordered_multiset<typename iterator_traits<_InputIterator>::value_type,
  960. hash<typename
  961. iterator_traits<_InputIterator>::value_type>,
  962. equal_to<typename
  963. iterator_traits<_InputIterator>::value_type>,
  964. _Allocator>;
  965. template<typename _InputIterator, typename _Hash, typename _Allocator,
  966. typename = _RequireInputIter<_InputIterator>,
  967. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  968. typename = _RequireAllocator<_Allocator>>
  969. unordered_multiset(_InputIterator, _InputIterator,
  970. unordered_multiset<int>::size_type,
  971. _Hash, _Allocator)
  972. -> unordered_multiset<typename
  973. iterator_traits<_InputIterator>::value_type,
  974. _Hash,
  975. equal_to<
  976. typename
  977. iterator_traits<_InputIterator>::value_type>,
  978. _Allocator>;
  979. template<typename _Tp, typename _Allocator,
  980. typename = _RequireAllocator<_Allocator>>
  981. unordered_multiset(initializer_list<_Tp>,
  982. unordered_multiset<int>::size_type, _Allocator)
  983. -> unordered_multiset<_Tp, hash<_Tp>, equal_to<_Tp>, _Allocator>;
  984. template<typename _Tp, typename _Hash, typename _Allocator,
  985. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  986. typename = _RequireAllocator<_Allocator>>
  987. unordered_multiset(initializer_list<_Tp>,
  988. unordered_multiset<int>::size_type, _Hash, _Allocator)
  989. -> unordered_multiset<_Tp, _Hash, equal_to<_Tp>, _Allocator>;
  990. #endif
  991. template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
  992. inline void
  993. swap(unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
  994. unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
  995. noexcept(noexcept(__x.swap(__y)))
  996. { __x.swap(__y); }
  997. template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
  998. inline bool
  999. operator==(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
  1000. const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
  1001. { return __x._M_base() == __y._M_base(); }
  1002. #if __cpp_impl_three_way_comparison < 201907L
  1003. template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
  1004. inline bool
  1005. operator!=(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
  1006. const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
  1007. { return !(__x == __y); }
  1008. #endif
  1009. } // namespace __debug
  1010. } // namespace std
  1011. #endif // C++11
  1012. #endif