unordered_set 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  1. // Debugging unordered_set/unordered_multiset implementation -*- C++ -*-
  2. // Copyright (C) 2003-2019 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. template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
  522. inline bool
  523. operator!=(const unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
  524. const unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
  525. { return !(__x == __y); }
  526. /// Class std::unordered_multiset with safety/checking/debug instrumentation.
  527. template<typename _Value,
  528. typename _Hash = std::hash<_Value>,
  529. typename _Pred = std::equal_to<_Value>,
  530. typename _Alloc = std::allocator<_Value> >
  531. class unordered_multiset
  532. : public __gnu_debug::_Safe_container<
  533. unordered_multiset<_Value, _Hash, _Pred, _Alloc>, _Alloc,
  534. __gnu_debug::_Safe_unordered_container>,
  535. public _GLIBCXX_STD_C::unordered_multiset<_Value, _Hash, _Pred, _Alloc>
  536. {
  537. typedef _GLIBCXX_STD_C::unordered_multiset<
  538. _Value, _Hash, _Pred, _Alloc> _Base;
  539. typedef __gnu_debug::_Safe_container<unordered_multiset,
  540. _Alloc, __gnu_debug::_Safe_unordered_container> _Safe;
  541. typedef typename _Base::const_iterator _Base_const_iterator;
  542. typedef typename _Base::iterator _Base_iterator;
  543. typedef typename _Base::const_local_iterator
  544. _Base_const_local_iterator;
  545. typedef typename _Base::local_iterator _Base_local_iterator;
  546. template<typename _ItT, typename _SeqT, typename _CatT>
  547. friend class ::__gnu_debug::_Safe_iterator;
  548. template<typename _ItT, typename _SeqT>
  549. friend class ::__gnu_debug::_Safe_local_iterator;
  550. public:
  551. typedef typename _Base::size_type size_type;
  552. typedef typename _Base::hasher hasher;
  553. typedef typename _Base::key_equal key_equal;
  554. typedef typename _Base::allocator_type allocator_type;
  555. typedef typename _Base::key_type key_type;
  556. typedef typename _Base::value_type value_type;
  557. typedef __gnu_debug::_Safe_iterator<
  558. _Base_iterator, unordered_multiset> iterator;
  559. typedef __gnu_debug::_Safe_iterator<
  560. _Base_const_iterator, unordered_multiset> const_iterator;
  561. typedef __gnu_debug::_Safe_local_iterator<
  562. _Base_local_iterator, unordered_multiset> local_iterator;
  563. typedef __gnu_debug::_Safe_local_iterator<
  564. _Base_const_local_iterator, unordered_multiset> const_local_iterator;
  565. unordered_multiset() = default;
  566. explicit
  567. unordered_multiset(size_type __n,
  568. const hasher& __hf = hasher(),
  569. const key_equal& __eql = key_equal(),
  570. const allocator_type& __a = allocator_type())
  571. : _Base(__n, __hf, __eql, __a) { }
  572. template<typename _InputIterator>
  573. unordered_multiset(_InputIterator __first, _InputIterator __last,
  574. size_type __n = 0,
  575. const hasher& __hf = hasher(),
  576. const key_equal& __eql = key_equal(),
  577. const allocator_type& __a = allocator_type())
  578. : _Base(__gnu_debug::__base(
  579. __glibcxx_check_valid_constructor_range(__first, __last)),
  580. __gnu_debug::__base(__last), __n,
  581. __hf, __eql, __a) { }
  582. unordered_multiset(const unordered_multiset&) = default;
  583. unordered_multiset(const _Base& __x)
  584. : _Base(__x) { }
  585. unordered_multiset(unordered_multiset&&) = default;
  586. explicit
  587. unordered_multiset(const allocator_type& __a)
  588. : _Base(__a) { }
  589. unordered_multiset(const unordered_multiset& __uset,
  590. const allocator_type& __a)
  591. : _Base(__uset, __a) { }
  592. unordered_multiset(unordered_multiset&& __uset,
  593. const allocator_type& __a)
  594. : _Safe(std::move(__uset._M_safe()), __a),
  595. _Base(std::move(__uset._M_base()), __a) { }
  596. unordered_multiset(initializer_list<value_type> __l,
  597. size_type __n = 0,
  598. const hasher& __hf = hasher(),
  599. const key_equal& __eql = key_equal(),
  600. const allocator_type& __a = allocator_type())
  601. : _Base(__l, __n, __hf, __eql, __a) { }
  602. unordered_multiset(size_type __n, const allocator_type& __a)
  603. : unordered_multiset(__n, hasher(), key_equal(), __a)
  604. { }
  605. unordered_multiset(size_type __n, const hasher& __hf,
  606. const allocator_type& __a)
  607. : unordered_multiset(__n, __hf, key_equal(), __a)
  608. { }
  609. template<typename _InputIterator>
  610. unordered_multiset(_InputIterator __first, _InputIterator __last,
  611. size_type __n,
  612. const allocator_type& __a)
  613. : unordered_multiset(__first, __last, __n, hasher(), key_equal(), __a)
  614. { }
  615. template<typename _InputIterator>
  616. unordered_multiset(_InputIterator __first, _InputIterator __last,
  617. size_type __n, const hasher& __hf,
  618. const allocator_type& __a)
  619. : unordered_multiset(__first, __last, __n, __hf, key_equal(), __a)
  620. { }
  621. unordered_multiset(initializer_list<value_type> __l,
  622. size_type __n,
  623. const allocator_type& __a)
  624. : unordered_multiset(__l, __n, hasher(), key_equal(), __a)
  625. { }
  626. unordered_multiset(initializer_list<value_type> __l,
  627. size_type __n, const hasher& __hf,
  628. const allocator_type& __a)
  629. : unordered_multiset(__l, __n, __hf, key_equal(), __a)
  630. { }
  631. ~unordered_multiset() = default;
  632. unordered_multiset&
  633. operator=(const unordered_multiset&) = default;
  634. unordered_multiset&
  635. operator=(unordered_multiset&&) = default;
  636. unordered_multiset&
  637. operator=(initializer_list<value_type> __l)
  638. {
  639. this->_M_base() = __l;
  640. this->_M_invalidate_all();
  641. return *this;
  642. }
  643. void
  644. swap(unordered_multiset& __x)
  645. noexcept( noexcept(declval<_Base&>().swap(__x)) )
  646. {
  647. _Safe::_M_swap(__x);
  648. _Base::swap(__x);
  649. }
  650. void
  651. clear() noexcept
  652. {
  653. _Base::clear();
  654. this->_M_invalidate_all();
  655. }
  656. iterator
  657. begin() noexcept
  658. { return { _Base::begin(), this }; }
  659. const_iterator
  660. begin() const noexcept
  661. { return { _Base::begin(), this }; }
  662. iterator
  663. end() noexcept
  664. { return { _Base::end(), this }; }
  665. const_iterator
  666. end() const noexcept
  667. { return { _Base::end(), this }; }
  668. const_iterator
  669. cbegin() const noexcept
  670. { return { _Base::cbegin(), this }; }
  671. const_iterator
  672. cend() const noexcept
  673. { return { _Base::cend(), this }; }
  674. // local versions
  675. local_iterator
  676. begin(size_type __b)
  677. {
  678. __glibcxx_check_bucket_index(__b);
  679. return { _Base::begin(__b), this };
  680. }
  681. local_iterator
  682. end(size_type __b)
  683. {
  684. __glibcxx_check_bucket_index(__b);
  685. return { _Base::end(__b), this };
  686. }
  687. const_local_iterator
  688. begin(size_type __b) const
  689. {
  690. __glibcxx_check_bucket_index(__b);
  691. return { _Base::begin(__b), this };
  692. }
  693. const_local_iterator
  694. end(size_type __b) const
  695. {
  696. __glibcxx_check_bucket_index(__b);
  697. return { _Base::end(__b), this };
  698. }
  699. const_local_iterator
  700. cbegin(size_type __b) const
  701. {
  702. __glibcxx_check_bucket_index(__b);
  703. return { _Base::cbegin(__b), this };
  704. }
  705. const_local_iterator
  706. cend(size_type __b) const
  707. {
  708. __glibcxx_check_bucket_index(__b);
  709. return { _Base::cend(__b), this };
  710. }
  711. size_type
  712. bucket_size(size_type __b) const
  713. {
  714. __glibcxx_check_bucket_index(__b);
  715. return _Base::bucket_size(__b);
  716. }
  717. float
  718. max_load_factor() const noexcept
  719. { return _Base::max_load_factor(); }
  720. void
  721. max_load_factor(float __f)
  722. {
  723. __glibcxx_check_max_load_factor(__f);
  724. _Base::max_load_factor(__f);
  725. }
  726. template<typename... _Args>
  727. iterator
  728. emplace(_Args&&... __args)
  729. {
  730. size_type __bucket_count = this->bucket_count();
  731. auto __it = _Base::emplace(std::forward<_Args>(__args)...);
  732. _M_check_rehashed(__bucket_count);
  733. return { __it, this };
  734. }
  735. template<typename... _Args>
  736. iterator
  737. emplace_hint(const_iterator __hint, _Args&&... __args)
  738. {
  739. __glibcxx_check_insert(__hint);
  740. size_type __bucket_count = this->bucket_count();
  741. auto __it = _Base::emplace_hint(__hint.base(),
  742. std::forward<_Args>(__args)...);
  743. _M_check_rehashed(__bucket_count);
  744. return { __it, this };
  745. }
  746. iterator
  747. insert(const value_type& __obj)
  748. {
  749. size_type __bucket_count = this->bucket_count();
  750. auto __it = _Base::insert(__obj);
  751. _M_check_rehashed(__bucket_count);
  752. return { __it, this };
  753. }
  754. iterator
  755. insert(const_iterator __hint, const value_type& __obj)
  756. {
  757. __glibcxx_check_insert(__hint);
  758. size_type __bucket_count = this->bucket_count();
  759. auto __it = _Base::insert(__hint.base(), __obj);
  760. _M_check_rehashed(__bucket_count);
  761. return { __it, this };
  762. }
  763. iterator
  764. insert(value_type&& __obj)
  765. {
  766. size_type __bucket_count = this->bucket_count();
  767. auto __it = _Base::insert(std::move(__obj));
  768. _M_check_rehashed(__bucket_count);
  769. return { __it, this };
  770. }
  771. iterator
  772. insert(const_iterator __hint, value_type&& __obj)
  773. {
  774. __glibcxx_check_insert(__hint);
  775. size_type __bucket_count = this->bucket_count();
  776. auto __it = _Base::insert(__hint.base(), std::move(__obj));
  777. _M_check_rehashed(__bucket_count);
  778. return { __it, this };
  779. }
  780. void
  781. insert(std::initializer_list<value_type> __l)
  782. {
  783. size_type __bucket_count = this->bucket_count();
  784. _Base::insert(__l);
  785. _M_check_rehashed(__bucket_count);
  786. }
  787. template<typename _InputIterator>
  788. void
  789. insert(_InputIterator __first, _InputIterator __last)
  790. {
  791. typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
  792. __glibcxx_check_valid_range2(__first, __last, __dist);
  793. size_type __bucket_count = this->bucket_count();
  794. if (__dist.second >= __gnu_debug::__dp_sign)
  795. _Base::insert(__gnu_debug::__unsafe(__first),
  796. __gnu_debug::__unsafe(__last));
  797. else
  798. _Base::insert(__first, __last);
  799. _M_check_rehashed(__bucket_count);
  800. }
  801. #if __cplusplus > 201402L
  802. using node_type = typename _Base::node_type;
  803. node_type
  804. extract(const_iterator __position)
  805. {
  806. __glibcxx_check_erase(__position);
  807. return _M_extract(__position.base());
  808. }
  809. node_type
  810. extract(const key_type& __key)
  811. {
  812. const auto __position = _Base::find(__key);
  813. if (__position != _Base::end())
  814. return _M_extract(__position);
  815. return {};
  816. }
  817. iterator
  818. insert(node_type&& __nh)
  819. { return { _Base::insert(std::move(__nh)), this }; }
  820. iterator
  821. insert(const_iterator __hint, node_type&& __nh)
  822. {
  823. __glibcxx_check_insert(__hint);
  824. return { _Base::insert(__hint.base(), std::move(__nh)), this };
  825. }
  826. using _Base::merge;
  827. #endif // C++17
  828. iterator
  829. find(const key_type& __key)
  830. { return { _Base::find(__key), this }; }
  831. const_iterator
  832. find(const key_type& __key) const
  833. { return { _Base::find(__key), this }; }
  834. std::pair<iterator, iterator>
  835. equal_range(const key_type& __key)
  836. {
  837. auto __res = _Base::equal_range(__key);
  838. return { { __res.first, this }, { __res.second, this } };
  839. }
  840. std::pair<const_iterator, const_iterator>
  841. equal_range(const key_type& __key) const
  842. {
  843. auto __res = _Base::equal_range(__key);
  844. return { { __res.first, this }, { __res.second, this } };
  845. }
  846. size_type
  847. erase(const key_type& __key)
  848. {
  849. size_type __ret(0);
  850. auto __pair = _Base::equal_range(__key);
  851. for (auto __victim = __pair.first; __victim != __pair.second;)
  852. {
  853. _M_invalidate(__victim);
  854. __victim = _Base::erase(__victim);
  855. ++__ret;
  856. }
  857. return __ret;
  858. }
  859. iterator
  860. erase(const_iterator __it)
  861. {
  862. __glibcxx_check_erase(__it);
  863. return { _M_erase(__it.base()), this };
  864. }
  865. iterator
  866. erase(iterator __it)
  867. {
  868. __glibcxx_check_erase(__it);
  869. return { _M_erase(__it.base()), this };
  870. }
  871. iterator
  872. erase(const_iterator __first, const_iterator __last)
  873. {
  874. __glibcxx_check_erase_range(__first, __last);
  875. for (auto __tmp = __first.base(); __tmp != __last.base(); ++__tmp)
  876. {
  877. _GLIBCXX_DEBUG_VERIFY(__tmp != _Base::cend(),
  878. _M_message(__gnu_debug::__msg_valid_range)
  879. ._M_iterator(__first, "first")
  880. ._M_iterator(__last, "last"));
  881. _M_invalidate(__tmp);
  882. }
  883. return { _Base::erase(__first.base(), __last.base()), this };
  884. }
  885. _Base&
  886. _M_base() noexcept { return *this; }
  887. const _Base&
  888. _M_base() const noexcept { return *this; }
  889. private:
  890. void
  891. _M_check_rehashed(size_type __prev_count)
  892. {
  893. if (__prev_count != this->bucket_count())
  894. this->_M_invalidate_all();
  895. }
  896. void
  897. _M_invalidate(_Base_const_iterator __victim)
  898. {
  899. this->_M_invalidate_if(
  900. [__victim](_Base_const_iterator __it) { return __it == __victim; });
  901. this->_M_invalidate_local_if(
  902. [__victim](_Base_const_local_iterator __it)
  903. { return __it._M_curr() == __victim._M_cur; });
  904. }
  905. _Base_iterator
  906. _M_erase(_Base_const_iterator __victim)
  907. {
  908. _M_invalidate(__victim);
  909. size_type __bucket_count = this->bucket_count();
  910. _Base_iterator __next = _Base::erase(__victim);
  911. _M_check_rehashed(__bucket_count);
  912. return __next;
  913. }
  914. #if __cplusplus > 201402L
  915. node_type
  916. _M_extract(_Base_const_iterator __victim)
  917. {
  918. _M_invalidate(__victim);
  919. return _Base::extract(__victim);
  920. }
  921. #endif
  922. };
  923. #if __cpp_deduction_guides >= 201606
  924. template<typename _InputIterator,
  925. typename _Hash =
  926. hash<typename iterator_traits<_InputIterator>::value_type>,
  927. typename _Pred =
  928. equal_to<typename iterator_traits<_InputIterator>::value_type>,
  929. typename _Allocator =
  930. allocator<typename iterator_traits<_InputIterator>::value_type>,
  931. typename = _RequireInputIter<_InputIterator>,
  932. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  933. typename = _RequireNotAllocator<_Pred>,
  934. typename = _RequireAllocator<_Allocator>>
  935. unordered_multiset(_InputIterator, _InputIterator,
  936. unordered_multiset<int>::size_type = {},
  937. _Hash = _Hash(), _Pred = _Pred(),
  938. _Allocator = _Allocator())
  939. -> unordered_multiset<typename iterator_traits<_InputIterator>::value_type,
  940. _Hash, _Pred, _Allocator>;
  941. template<typename _Tp, typename _Hash = hash<_Tp>,
  942. typename _Pred = equal_to<_Tp>,
  943. typename _Allocator = allocator<_Tp>,
  944. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  945. typename = _RequireNotAllocator<_Pred>,
  946. typename = _RequireAllocator<_Allocator>>
  947. unordered_multiset(initializer_list<_Tp>,
  948. unordered_multiset<int>::size_type = {},
  949. _Hash = _Hash(), _Pred = _Pred(),
  950. _Allocator = _Allocator())
  951. -> unordered_multiset<_Tp, _Hash, _Pred, _Allocator>;
  952. template<typename _InputIterator, typename _Allocator,
  953. typename = _RequireInputIter<_InputIterator>,
  954. typename = _RequireAllocator<_Allocator>>
  955. unordered_multiset(_InputIterator, _InputIterator,
  956. unordered_multiset<int>::size_type, _Allocator)
  957. -> unordered_multiset<typename iterator_traits<_InputIterator>::value_type,
  958. hash<typename
  959. iterator_traits<_InputIterator>::value_type>,
  960. equal_to<typename
  961. iterator_traits<_InputIterator>::value_type>,
  962. _Allocator>;
  963. template<typename _InputIterator, typename _Hash, typename _Allocator,
  964. typename = _RequireInputIter<_InputIterator>,
  965. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  966. typename = _RequireAllocator<_Allocator>>
  967. unordered_multiset(_InputIterator, _InputIterator,
  968. unordered_multiset<int>::size_type,
  969. _Hash, _Allocator)
  970. -> unordered_multiset<typename
  971. iterator_traits<_InputIterator>::value_type,
  972. _Hash,
  973. equal_to<
  974. typename
  975. iterator_traits<_InputIterator>::value_type>,
  976. _Allocator>;
  977. template<typename _Tp, typename _Allocator,
  978. typename = _RequireAllocator<_Allocator>>
  979. unordered_multiset(initializer_list<_Tp>,
  980. unordered_multiset<int>::size_type, _Allocator)
  981. -> unordered_multiset<_Tp, hash<_Tp>, equal_to<_Tp>, _Allocator>;
  982. template<typename _Tp, typename _Hash, typename _Allocator,
  983. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  984. typename = _RequireAllocator<_Allocator>>
  985. unordered_multiset(initializer_list<_Tp>,
  986. unordered_multiset<int>::size_type, _Hash, _Allocator)
  987. -> unordered_multiset<_Tp, _Hash, equal_to<_Tp>, _Allocator>;
  988. #endif
  989. template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
  990. inline void
  991. swap(unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
  992. unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
  993. noexcept(noexcept(__x.swap(__y)))
  994. { __x.swap(__y); }
  995. template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
  996. inline bool
  997. operator==(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
  998. const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
  999. { return __x._M_base() == __y._M_base(); }
  1000. template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
  1001. inline bool
  1002. operator!=(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
  1003. const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
  1004. { return !(__x == __y); }
  1005. } // namespace __debug
  1006. } // namespace std
  1007. #endif // C++11
  1008. #endif