unordered_set 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. // Profiling unordered_set/unordered_multiset implementation -*- C++ -*-
  2. // Copyright (C) 2009-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. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. // Under Section 7 of GPL version 3, you are granted additional
  15. // permissions described in the GCC Runtime Library Exception, version
  16. // 3.1, as published by the Free Software Foundation.
  17. // You should have received a copy of the GNU General Public License along
  18. // with this library; see the file COPYING3. If not see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file profile/unordered_set
  21. * This file is a GNU profile extension to the Standard C++ Library.
  22. */
  23. #ifndef _GLIBCXX_PROFILE_UNORDERED_SET
  24. #define _GLIBCXX_PROFILE_UNORDERED_SET 1
  25. #if __cplusplus < 201103L
  26. # include <bits/c++0x_warning.h>
  27. #else
  28. # include <unordered_set>
  29. #include <profile/base.h>
  30. #include <profile/unordered_base.h>
  31. #define _GLIBCXX_BASE unordered_set<_Key, _Hash, _Pred, _Alloc>
  32. #define _GLIBCXX_STD_BASE _GLIBCXX_STD_C::_GLIBCXX_BASE
  33. namespace std _GLIBCXX_VISIBILITY(default)
  34. {
  35. namespace __profile
  36. {
  37. /** @brief Unordered_set wrapper with performance instrumentation. */
  38. template<typename _Key,
  39. typename _Hash = std::hash<_Key>,
  40. typename _Pred = std::equal_to<_Key>,
  41. typename _Alloc = std::allocator<_Key> >
  42. class unordered_set
  43. : public _GLIBCXX_STD_BASE,
  44. public _Unordered_profile<unordered_set<_Key, _Hash, _Pred, _Alloc>,
  45. true>
  46. {
  47. typedef _GLIBCXX_STD_BASE _Base;
  48. _Base&
  49. _M_base() noexcept { return *this; }
  50. const _Base&
  51. _M_base() const noexcept { return *this; }
  52. public:
  53. typedef typename _Base::size_type size_type;
  54. typedef typename _Base::hasher hasher;
  55. typedef typename _Base::key_equal key_equal;
  56. typedef typename _Base::allocator_type allocator_type;
  57. typedef typename _Base::key_type key_type;
  58. typedef typename _Base::value_type value_type;
  59. typedef typename _Base::difference_type difference_type;
  60. typedef typename _Base::reference reference;
  61. typedef typename _Base::const_reference const_reference;
  62. typedef typename _Base::iterator iterator;
  63. typedef typename _Base::const_iterator const_iterator;
  64. unordered_set() = default;
  65. explicit
  66. unordered_set(size_type __n,
  67. const hasher& __hf = hasher(),
  68. const key_equal& __eql = key_equal(),
  69. const allocator_type& __a = allocator_type())
  70. : _Base(__n, __hf, __eql, __a)
  71. { }
  72. template<typename _InputIterator>
  73. unordered_set(_InputIterator __f, _InputIterator __l,
  74. size_type __n = 0,
  75. const hasher& __hf = hasher(),
  76. const key_equal& __eql = key_equal(),
  77. const allocator_type& __a = allocator_type())
  78. : _Base(__f, __l, __n, __hf, __eql, __a)
  79. { }
  80. unordered_set(const unordered_set&) = default;
  81. unordered_set(const _Base& __x)
  82. : _Base(__x)
  83. { }
  84. unordered_set(unordered_set&&) = default;
  85. explicit
  86. unordered_set(const allocator_type& __a)
  87. : _Base(__a)
  88. { }
  89. unordered_set(const unordered_set& __uset,
  90. const allocator_type& __a)
  91. : _Base(__uset._M_base(), __a)
  92. { }
  93. unordered_set(unordered_set&& __uset,
  94. const allocator_type& __a)
  95. : _Base(std::move(__uset._M_base()), __a)
  96. { }
  97. unordered_set(initializer_list<value_type> __l,
  98. size_type __n = 0,
  99. const hasher& __hf = hasher(),
  100. const key_equal& __eql = key_equal(),
  101. const allocator_type& __a = allocator_type())
  102. : _Base(__l, __n, __hf, __eql, __a)
  103. { }
  104. unordered_set(size_type __n, const allocator_type& __a)
  105. : unordered_set(__n, hasher(), key_equal(), __a)
  106. { }
  107. unordered_set(size_type __n, const hasher& __hf,
  108. const allocator_type& __a)
  109. : unordered_set(__n, __hf, key_equal(), __a)
  110. { }
  111. template<typename _InputIterator>
  112. unordered_set(_InputIterator __first, _InputIterator __last,
  113. size_type __n,
  114. const allocator_type& __a)
  115. : unordered_set(__first, __last, __n, hasher(), key_equal(), __a)
  116. { }
  117. template<typename _InputIterator>
  118. unordered_set(_InputIterator __first, _InputIterator __last,
  119. size_type __n, const hasher& __hf,
  120. const allocator_type& __a)
  121. : unordered_set(__first, __last, __n, __hf, key_equal(), __a)
  122. { }
  123. unordered_set(initializer_list<value_type> __l,
  124. size_type __n,
  125. const allocator_type& __a)
  126. : unordered_set(__l, __n, hasher(), key_equal(), __a)
  127. { }
  128. unordered_set(initializer_list<value_type> __l,
  129. size_type __n, const hasher& __hf,
  130. const allocator_type& __a)
  131. : unordered_set(__l, __n, __hf, key_equal(), __a)
  132. { }
  133. unordered_set&
  134. operator=(const unordered_set&) = default;
  135. unordered_set&
  136. operator=(unordered_set&&) = default;
  137. unordered_set&
  138. operator=(initializer_list<value_type> __l)
  139. {
  140. this->_M_profile_destruct();
  141. _M_base() = __l;
  142. this->_M_profile_construct();
  143. return *this;
  144. }
  145. void
  146. swap(unordered_set& __x)
  147. noexcept( noexcept(__x._M_base().swap(__x)) )
  148. {
  149. _Base::swap(__x);
  150. this->_M_swap(__x);
  151. }
  152. void
  153. clear() noexcept
  154. {
  155. this->_M_profile_destruct();
  156. _Base::clear();
  157. this->_M_profile_construct();
  158. }
  159. template<typename... _Args>
  160. std::pair<iterator, bool>
  161. emplace(_Args&&... __args)
  162. {
  163. size_type __old_size = _Base::bucket_count();
  164. std::pair<iterator, bool> __res
  165. = _Base::emplace(std::forward<_Args>(__args)...);
  166. this->_M_profile_resize(__old_size);
  167. return __res;
  168. }
  169. template<typename... _Args>
  170. iterator
  171. emplace_hint(const_iterator __it, _Args&&... __args)
  172. {
  173. size_type __old_size = _Base::bucket_count();
  174. iterator __res
  175. = _Base::emplace_hint(__it, std::forward<_Args>(__args)...);
  176. this->_M_profile_resize(__old_size);
  177. return __res;
  178. }
  179. void
  180. insert(std::initializer_list<value_type> __l)
  181. {
  182. size_type __old_size = _Base::bucket_count();
  183. _Base::insert(__l);
  184. this->_M_profile_resize(__old_size);
  185. }
  186. std::pair<iterator, bool>
  187. insert(const value_type& __obj)
  188. {
  189. size_type __old_size = _Base::bucket_count();
  190. std::pair<iterator, bool> __res = _Base::insert(__obj);
  191. this->_M_profile_resize(__old_size);
  192. return __res;
  193. }
  194. iterator
  195. insert(const_iterator __iter, const value_type& __v)
  196. {
  197. size_type __old_size = _Base::bucket_count();
  198. iterator __res = _Base::insert(__iter, __v);
  199. this->_M_profile_resize(__old_size);
  200. return __res;
  201. }
  202. std::pair<iterator, bool>
  203. insert(value_type&& __obj)
  204. {
  205. size_type __old_size = _Base::bucket_count();
  206. std::pair<iterator, bool> __res = _Base::insert(std::move(__obj));
  207. this->_M_profile_resize(__old_size);
  208. return __res;
  209. }
  210. iterator
  211. insert(const_iterator __iter, value_type&& __v)
  212. {
  213. size_type __old_size = _Base::bucket_count();
  214. iterator __res = _Base::insert(__iter, std::move(__v));
  215. this->_M_profile_resize(__old_size);
  216. return __res;
  217. }
  218. template<typename _InputIter>
  219. void
  220. insert(_InputIter __first, _InputIter __last)
  221. {
  222. size_type __old_size = _Base::bucket_count();
  223. _Base::insert(__first, __last);
  224. this->_M_profile_resize(__old_size);
  225. }
  226. void
  227. rehash(size_type __n)
  228. {
  229. size_type __old_size = _Base::bucket_count();
  230. _Base::rehash(__n);
  231. this->_M_profile_resize(__old_size);
  232. }
  233. };
  234. template<typename _Key, typename _Hash, typename _Pred, typename _Alloc>
  235. inline void
  236. swap(unordered_set<_Key, _Hash, _Pred, _Alloc>& __x,
  237. unordered_set<_Key, _Hash, _Pred, _Alloc>& __y)
  238. noexcept(noexcept(__x.swap(__y)))
  239. { __x.swap(__y); }
  240. template<typename _Key, typename _Hash, typename _Pred, typename _Alloc>
  241. inline bool
  242. operator==(const unordered_set<_Key, _Hash, _Pred, _Alloc>& __x,
  243. const unordered_set<_Key, _Hash, _Pred, _Alloc>& __y)
  244. { return static_cast<const _GLIBCXX_STD_BASE&>(__x) == __y; }
  245. template<typename _Key, typename _Hash, typename _Pred, typename _Alloc>
  246. inline bool
  247. operator!=(const unordered_set<_Key, _Hash, _Pred, _Alloc>& __x,
  248. const unordered_set<_Key, _Hash, _Pred, _Alloc>& __y)
  249. { return !(__x == __y); }
  250. #undef _GLIBCXX_BASE
  251. #undef _GLIBCXX_STD_BASE
  252. #define _GLIBCXX_STD_BASE _GLIBCXX_STD_C::_GLIBCXX_BASE
  253. #define _GLIBCXX_BASE unordered_multiset<_Value, _Hash, _Pred, _Alloc>
  254. /** @brief Unordered_multiset wrapper with performance instrumentation. */
  255. template<typename _Value,
  256. typename _Hash = std::hash<_Value>,
  257. typename _Pred = std::equal_to<_Value>,
  258. typename _Alloc = std::allocator<_Value> >
  259. class unordered_multiset
  260. : public _GLIBCXX_STD_BASE,
  261. public _Unordered_profile<unordered_multiset<_Value,
  262. _Hash, _Pred, _Alloc>,
  263. false>
  264. {
  265. typedef _GLIBCXX_STD_BASE _Base;
  266. _Base&
  267. _M_base() noexcept { return *this; }
  268. const _Base&
  269. _M_base() const noexcept { return *this; }
  270. public:
  271. typedef typename _Base::size_type size_type;
  272. typedef typename _Base::hasher hasher;
  273. typedef typename _Base::key_equal key_equal;
  274. typedef typename _Base::allocator_type allocator_type;
  275. typedef typename _Base::key_type key_type;
  276. typedef typename _Base::value_type value_type;
  277. typedef typename _Base::difference_type difference_type;
  278. typedef typename _Base::reference reference;
  279. typedef typename _Base::const_reference const_reference;
  280. typedef typename _Base::iterator iterator;
  281. typedef typename _Base::const_iterator const_iterator;
  282. unordered_multiset() = default;
  283. explicit
  284. unordered_multiset(size_type __n,
  285. const hasher& __hf = hasher(),
  286. const key_equal& __eql = key_equal(),
  287. const allocator_type& __a = allocator_type())
  288. : _Base(__n, __hf, __eql, __a)
  289. { }
  290. template<typename _InputIterator>
  291. unordered_multiset(_InputIterator __f, _InputIterator __l,
  292. size_type __n = 0,
  293. const hasher& __hf = hasher(),
  294. const key_equal& __eql = key_equal(),
  295. const allocator_type& __a = allocator_type())
  296. : _Base(__f, __l, __n, __hf, __eql, __a)
  297. { }
  298. unordered_multiset(const unordered_multiset&) = default;
  299. unordered_multiset(const _Base& __x)
  300. : _Base(__x)
  301. { }
  302. unordered_multiset(unordered_multiset&&) = default;
  303. explicit
  304. unordered_multiset(const allocator_type& __a)
  305. : _Base(__a)
  306. { }
  307. unordered_multiset(const unordered_multiset& __umset,
  308. const allocator_type& __a)
  309. : _Base(__umset._M_base(), __a)
  310. { }
  311. unordered_multiset(unordered_multiset&& __umset,
  312. const allocator_type& __a)
  313. : _Base(std::move(__umset._M_base()), __a)
  314. { }
  315. unordered_multiset(initializer_list<value_type> __l,
  316. size_type __n = 0,
  317. const hasher& __hf = hasher(),
  318. const key_equal& __eql = key_equal(),
  319. const allocator_type& __a = allocator_type())
  320. : _Base(__l, __n, __hf, __eql, __a)
  321. { }
  322. unordered_multiset(size_type __n, const allocator_type& __a)
  323. : unordered_multiset(__n, hasher(), key_equal(), __a)
  324. { }
  325. unordered_multiset(size_type __n, const hasher& __hf,
  326. const allocator_type& __a)
  327. : unordered_multiset(__n, __hf, key_equal(), __a)
  328. { }
  329. template<typename _InputIterator>
  330. unordered_multiset(_InputIterator __first, _InputIterator __last,
  331. size_type __n,
  332. const allocator_type& __a)
  333. : unordered_multiset(__first, __last, __n, hasher(), key_equal(), __a)
  334. { }
  335. template<typename _InputIterator>
  336. unordered_multiset(_InputIterator __first, _InputIterator __last,
  337. size_type __n, const hasher& __hf,
  338. const allocator_type& __a)
  339. : unordered_multiset(__first, __last, __n, __hf, key_equal(), __a)
  340. { }
  341. unordered_multiset(initializer_list<value_type> __l,
  342. size_type __n,
  343. const allocator_type& __a)
  344. : unordered_multiset(__l, __n, hasher(), key_equal(), __a)
  345. { }
  346. unordered_multiset(initializer_list<value_type> __l,
  347. size_type __n, const hasher& __hf,
  348. const allocator_type& __a)
  349. : unordered_multiset(__l, __n, __hf, key_equal(), __a)
  350. { }
  351. unordered_multiset&
  352. operator=(const unordered_multiset&) = default;
  353. unordered_multiset&
  354. operator=(unordered_multiset&&) = default;
  355. unordered_multiset&
  356. operator=(initializer_list<value_type> __l)
  357. {
  358. this->_M_profile_destruct();
  359. _M_base() = __l;
  360. this->_M_profile_construct();
  361. return *this;
  362. }
  363. void
  364. swap(unordered_multiset& __x)
  365. noexcept( noexcept(__x._M_base().swap(__x)) )
  366. {
  367. _Base::swap(__x);
  368. this->_M_swap(__x);
  369. }
  370. void
  371. clear() noexcept
  372. {
  373. this->_M_profile_destruct();
  374. _Base::clear();
  375. this->_M_profile_construct();
  376. }
  377. template<typename... _Args>
  378. iterator
  379. emplace(_Args&&... __args)
  380. {
  381. size_type __old_size = _Base::bucket_count();
  382. iterator __res = _Base::emplace(std::forward<_Args>(__args)...);
  383. this->_M_profile_resize(__old_size);
  384. return __res;
  385. }
  386. template<typename... _Args>
  387. iterator
  388. emplace_hint(const_iterator __it, _Args&&... __args)
  389. {
  390. size_type __old_size = _Base::bucket_count();
  391. iterator __res
  392. = _Base::emplace_hint(__it, std::forward<_Args>(__args)...);
  393. this->_M_profile_resize(__old_size);
  394. return __res;
  395. }
  396. void
  397. insert(std::initializer_list<value_type> __l)
  398. {
  399. size_type __old_size = _Base::bucket_count();
  400. _Base::insert(__l);
  401. this->_M_profile_resize(__old_size);
  402. }
  403. iterator
  404. insert(const value_type& __obj)
  405. {
  406. size_type __old_size = _Base::bucket_count();
  407. iterator __res = _Base::insert(__obj);
  408. this->_M_profile_resize(__old_size);
  409. return __res;
  410. }
  411. iterator
  412. insert(const_iterator __iter, const value_type& __v)
  413. {
  414. size_type __old_size = _Base::bucket_count();
  415. iterator __res = _Base::insert(__iter, __v);
  416. this->_M_profile_resize(__old_size);
  417. return __res;
  418. }
  419. iterator
  420. insert(value_type&& __obj)
  421. {
  422. size_type __old_size = _Base::bucket_count();
  423. iterator __res = _Base::insert(std::move(__obj));
  424. this->_M_profile_resize(__old_size);
  425. return __res;
  426. }
  427. iterator
  428. insert(const_iterator __iter, value_type&& __v)
  429. {
  430. size_type __old_size = _Base::bucket_count();
  431. iterator __res = _Base::insert(__iter, std::move(__v));
  432. this->_M_profile_resize(__old_size);
  433. return __res;
  434. }
  435. template<typename _InputIter>
  436. void
  437. insert(_InputIter __first, _InputIter __last)
  438. {
  439. size_type __old_size = _Base::bucket_count();
  440. _Base::insert(__first, __last);
  441. this->_M_profile_resize(__old_size);
  442. }
  443. void
  444. rehash(size_type __n)
  445. {
  446. size_type __old_size = _Base::bucket_count();
  447. _Base::rehash(__n);
  448. this->_M_profile_resize(__old_size);
  449. }
  450. };
  451. template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
  452. inline void
  453. swap(unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
  454. unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
  455. noexcept(noexcept(__x.swap(__y)))
  456. { __x.swap(__y); }
  457. template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
  458. inline bool
  459. operator==(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
  460. const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
  461. { return static_cast<const _GLIBCXX_STD_BASE&>(__x) == __y; }
  462. template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
  463. inline bool
  464. operator!=(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
  465. const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
  466. { return !(__x == __y); }
  467. } // namespace __profile
  468. } // namespace std
  469. #undef _GLIBCXX_BASE
  470. #undef _GLIBCXX_STD_BASE
  471. #endif // C++11
  472. #endif