unordered_map 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. // Profiling unordered_map/unordered_multimap 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_map
  21. * This file is a GNU profile extension to the Standard C++ Library.
  22. */
  23. #ifndef _GLIBCXX_PROFILE_UNORDERED_MAP
  24. #define _GLIBCXX_PROFILE_UNORDERED_MAP 1
  25. #if __cplusplus < 201103L
  26. # include <bits/c++0x_warning.h>
  27. #else
  28. # include <unordered_map>
  29. #include <profile/base.h>
  30. #include <profile/unordered_base.h>
  31. #define _GLIBCXX_BASE unordered_map<_Key, _Tp, _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. /// Class std::unordered_map wrapper with performance instrumentation.
  38. template<typename _Key, typename _Tp,
  39. typename _Hash = std::hash<_Key>,
  40. typename _Pred = std::equal_to<_Key>,
  41. typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
  42. class unordered_map
  43. : public _GLIBCXX_STD_BASE,
  44. public _Unordered_profile<unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>,
  45. true>
  46. {
  47. typedef typename _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::mapped_type mapped_type;
  63. typedef typename _Base::iterator iterator;
  64. typedef typename _Base::const_iterator const_iterator;
  65. unordered_map() = default;
  66. explicit
  67. unordered_map(size_type __n,
  68. const hasher& __hf = hasher(),
  69. const key_equal& __eql = key_equal(),
  70. const allocator_type& __a = allocator_type())
  71. : _Base(__n, __hf, __eql, __a) { }
  72. template<typename _InputIterator>
  73. unordered_map(_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. unordered_map(const unordered_map&) = default;
  80. unordered_map(const _Base& __x)
  81. : _Base(__x) { }
  82. unordered_map(unordered_map&&) = default;
  83. explicit
  84. unordered_map(const allocator_type& __a)
  85. : _Base(__a) { }
  86. unordered_map(const unordered_map& __umap,
  87. const allocator_type& __a)
  88. : _Base(__umap, __a) { }
  89. unordered_map(unordered_map&& __umap,
  90. const allocator_type& __a)
  91. : _Base(std::move(__umap._M_base()), __a) { }
  92. unordered_map(initializer_list<value_type> __l,
  93. size_type __n = 0,
  94. const hasher& __hf = hasher(),
  95. const key_equal& __eql = key_equal(),
  96. const allocator_type& __a = allocator_type())
  97. : _Base(__l, __n, __hf, __eql, __a) { }
  98. unordered_map(size_type __n, const allocator_type& __a)
  99. : unordered_map(__n, hasher(), key_equal(), __a)
  100. { }
  101. unordered_map(size_type __n, const hasher& __hf,
  102. const allocator_type& __a)
  103. : unordered_map(__n, __hf, key_equal(), __a)
  104. { }
  105. template<typename _InputIterator>
  106. unordered_map(_InputIterator __first, _InputIterator __last,
  107. size_type __n,
  108. const allocator_type& __a)
  109. : unordered_map(__first, __last, __n, hasher(), key_equal(), __a)
  110. { }
  111. template<typename _InputIterator>
  112. unordered_map(_InputIterator __first, _InputIterator __last,
  113. size_type __n, const hasher& __hf,
  114. const allocator_type& __a)
  115. : unordered_map(__first, __last, __n, __hf, key_equal(), __a)
  116. { }
  117. unordered_map(initializer_list<value_type> __l,
  118. size_type __n,
  119. const allocator_type& __a)
  120. : unordered_map(__l, __n, hasher(), key_equal(), __a)
  121. { }
  122. unordered_map(initializer_list<value_type> __l,
  123. size_type __n, const hasher& __hf,
  124. const allocator_type& __a)
  125. : unordered_map(__l, __n, __hf, key_equal(), __a)
  126. { }
  127. unordered_map&
  128. operator=(const unordered_map&) = default;
  129. unordered_map&
  130. operator=(unordered_map&&) = default;
  131. unordered_map&
  132. operator=(initializer_list<value_type> __l)
  133. {
  134. this->_M_profile_destruct();
  135. _M_base() = __l;
  136. this->_M_profile_construct();
  137. return *this;
  138. }
  139. void
  140. clear() noexcept
  141. {
  142. this->_M_profile_destruct();
  143. _Base::clear();
  144. this->_M_profile_construct();
  145. }
  146. template<typename... _Args>
  147. std::pair<iterator, bool>
  148. emplace(_Args&&... __args)
  149. {
  150. size_type __old_size = _Base::bucket_count();
  151. std::pair<iterator, bool> __res
  152. = _Base::emplace(std::forward<_Args>(__args)...);
  153. this->_M_profile_resize(__old_size);
  154. return __res;
  155. }
  156. template<typename... _Args>
  157. iterator
  158. emplace_hint(const_iterator __it, _Args&&... __args)
  159. {
  160. size_type __old_size = _Base::bucket_count();
  161. iterator __res
  162. = _Base::emplace_hint(__it, std::forward<_Args>(__args)...);
  163. this->_M_profile_resize(__old_size);
  164. return __res;
  165. }
  166. void
  167. insert(std::initializer_list<value_type> __l)
  168. {
  169. size_type __old_size = _Base::bucket_count();
  170. _Base::insert(__l);
  171. this->_M_profile_resize(__old_size);
  172. }
  173. std::pair<iterator, bool>
  174. insert(const value_type& __obj)
  175. {
  176. size_type __old_size = _Base::bucket_count();
  177. std::pair<iterator, bool> __res = _Base::insert(__obj);
  178. this->_M_profile_resize(__old_size);
  179. return __res;
  180. }
  181. iterator
  182. insert(const_iterator __iter, const value_type& __v)
  183. {
  184. size_type __old_size = _Base::bucket_count();
  185. iterator __res = _Base::insert(__iter, __v);
  186. this->_M_profile_resize(__old_size);
  187. return __res;
  188. }
  189. template<typename _Pair, typename = typename
  190. std::enable_if<std::is_constructible<value_type,
  191. _Pair&&>::value>::type>
  192. std::pair<iterator, bool>
  193. insert(_Pair&& __obj)
  194. {
  195. size_type __old_size = _Base::bucket_count();
  196. std::pair<iterator, bool> __res
  197. = _Base::insert(std::forward<_Pair>(__obj));
  198. this->_M_profile_resize(__old_size);
  199. return __res;
  200. }
  201. template<typename _Pair, typename = typename
  202. std::enable_if<std::is_constructible<value_type,
  203. _Pair&&>::value>::type>
  204. iterator
  205. insert(const_iterator __iter, _Pair&& __v)
  206. {
  207. size_type __old_size = _Base::bucket_count();
  208. iterator __res = _Base::insert(__iter, std::forward<_Pair>(__v));
  209. this->_M_profile_resize(__old_size);
  210. return __res;
  211. }
  212. template<typename _InputIter>
  213. void
  214. insert(_InputIter __first, _InputIter __last)
  215. {
  216. size_type __old_size = _Base::bucket_count();
  217. _Base::insert(__first, __last);
  218. this->_M_profile_resize(__old_size);
  219. }
  220. // operator[]
  221. mapped_type&
  222. operator[](const _Key& __k)
  223. {
  224. size_type __old_size = _Base::bucket_count();
  225. mapped_type& __res = _M_base()[__k];
  226. this->_M_profile_resize(__old_size);
  227. return __res;
  228. }
  229. mapped_type&
  230. operator[](_Key&& __k)
  231. {
  232. size_type __old_size = _Base::bucket_count();
  233. mapped_type& __res = _M_base()[std::move(__k)];
  234. this->_M_profile_resize(__old_size);
  235. return __res;
  236. }
  237. void
  238. swap(unordered_map& __x)
  239. noexcept( noexcept(__x._M_base().swap(__x)) )
  240. {
  241. _Base::swap(__x._M_base());
  242. this->_M_swap(__x);
  243. }
  244. void rehash(size_type __n)
  245. {
  246. size_type __old_size = _Base::bucket_count();
  247. _Base::rehash(__n);
  248. this->_M_profile_resize(__old_size);
  249. }
  250. };
  251. template<typename _Key, typename _Tp, typename _Hash,
  252. typename _Pred, typename _Alloc>
  253. inline void
  254. swap(unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  255. unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  256. noexcept(noexcept(__x.swap(__y)))
  257. { __x.swap(__y); }
  258. template<typename _Key, typename _Tp, typename _Hash,
  259. typename _Pred, typename _Alloc>
  260. inline bool
  261. operator==(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  262. const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  263. { return static_cast<const _GLIBCXX_STD_BASE&>(__x) == __y; }
  264. template<typename _Key, typename _Tp, typename _Hash,
  265. typename _Pred, typename _Alloc>
  266. inline bool
  267. operator!=(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  268. const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  269. { return !(__x == __y); }
  270. #undef _GLIBCXX_BASE
  271. #undef _GLIBCXX_STD_BASE
  272. #define _GLIBCXX_BASE unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>
  273. #define _GLIBCXX_STD_BASE _GLIBCXX_STD_C::_GLIBCXX_BASE
  274. /// Class std::unordered_multimap wrapper with performance instrumentation.
  275. template<typename _Key, typename _Tp,
  276. typename _Hash = std::hash<_Key>,
  277. typename _Pred = std::equal_to<_Key>,
  278. typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
  279. class unordered_multimap
  280. : public _GLIBCXX_STD_BASE,
  281. public _Unordered_profile<unordered_multimap<_Key, _Tp,
  282. _Hash, _Pred, _Alloc>,
  283. false>
  284. {
  285. typedef typename _GLIBCXX_STD_BASE _Base;
  286. _Base&
  287. _M_base() noexcept { return *this; }
  288. const _Base&
  289. _M_base() const noexcept { return *this; }
  290. public:
  291. typedef typename _Base::size_type size_type;
  292. typedef typename _Base::hasher hasher;
  293. typedef typename _Base::key_equal key_equal;
  294. typedef typename _Base::allocator_type allocator_type;
  295. typedef typename _Base::key_type key_type;
  296. typedef typename _Base::value_type value_type;
  297. typedef typename _Base::difference_type difference_type;
  298. typedef typename _Base::reference reference;
  299. typedef typename _Base::const_reference const_reference;
  300. typedef typename _Base::iterator iterator;
  301. typedef typename _Base::const_iterator const_iterator;
  302. unordered_multimap() = default;
  303. explicit
  304. unordered_multimap(size_type __n,
  305. const hasher& __hf = hasher(),
  306. const key_equal& __eql = key_equal(),
  307. const allocator_type& __a = allocator_type())
  308. : _Base(__n, __hf, __eql, __a) { }
  309. template<typename _InputIterator>
  310. unordered_multimap(_InputIterator __f, _InputIterator __l,
  311. size_type __n = 0,
  312. const hasher& __hf = hasher(),
  313. const key_equal& __eql = key_equal(),
  314. const allocator_type& __a = allocator_type())
  315. : _Base(__f, __l, __n, __hf, __eql, __a) { }
  316. unordered_multimap(const unordered_multimap&) = default;
  317. unordered_multimap(const _Base& __x)
  318. : _Base(__x) { }
  319. unordered_multimap(unordered_multimap&&) = default;
  320. explicit
  321. unordered_multimap(const allocator_type& __a)
  322. : _Base(__a) { }
  323. unordered_multimap(const unordered_multimap& __ummap,
  324. const allocator_type& __a)
  325. : _Base(__ummap._M_base(), __a) { }
  326. unordered_multimap(unordered_multimap&& __ummap,
  327. const allocator_type& __a)
  328. : _Base(std::move(__ummap._M_base()), __a) { }
  329. unordered_multimap(initializer_list<value_type> __l,
  330. size_type __n = 0,
  331. const hasher& __hf = hasher(),
  332. const key_equal& __eql = key_equal(),
  333. const allocator_type& __a = allocator_type())
  334. : _Base(__l, __n, __hf, __eql, __a) { }
  335. unordered_multimap(size_type __n, const allocator_type& __a)
  336. : unordered_multimap(__n, hasher(), key_equal(), __a)
  337. { }
  338. unordered_multimap(size_type __n, const hasher& __hf,
  339. const allocator_type& __a)
  340. : unordered_multimap(__n, __hf, key_equal(), __a)
  341. { }
  342. template<typename _InputIterator>
  343. unordered_multimap(_InputIterator __first, _InputIterator __last,
  344. size_type __n,
  345. const allocator_type& __a)
  346. : unordered_multimap(__first, __last, __n, hasher(), key_equal(), __a)
  347. { }
  348. template<typename _InputIterator>
  349. unordered_multimap(_InputIterator __first, _InputIterator __last,
  350. size_type __n, const hasher& __hf,
  351. const allocator_type& __a)
  352. : unordered_multimap(__first, __last, __n, __hf, key_equal(), __a)
  353. { }
  354. unordered_multimap(initializer_list<value_type> __l,
  355. size_type __n,
  356. const allocator_type& __a)
  357. : unordered_multimap(__l, __n, hasher(), key_equal(), __a)
  358. { }
  359. unordered_multimap(initializer_list<value_type> __l,
  360. size_type __n, const hasher& __hf,
  361. const allocator_type& __a)
  362. : unordered_multimap(__l, __n, __hf, key_equal(), __a)
  363. { }
  364. unordered_multimap&
  365. operator=(const unordered_multimap&) = default;
  366. unordered_multimap&
  367. operator=(unordered_multimap&&) = default;
  368. unordered_multimap&
  369. operator=(initializer_list<value_type> __l)
  370. {
  371. this->_M_profile_destruct();
  372. _M_base() = __l;
  373. this->_M_profile_construct();
  374. return *this;
  375. }
  376. void
  377. clear() noexcept
  378. {
  379. this->_M_profile_destruct();
  380. _Base::clear();
  381. this->_M_profile_construct();
  382. }
  383. template<typename... _Args>
  384. iterator
  385. emplace(_Args&&... __args)
  386. {
  387. size_type __old_size = _Base::bucket_count();
  388. iterator __res
  389. = _Base::emplace(std::forward<_Args>(__args)...);
  390. this->_M_profile_resize(__old_size);
  391. return __res;
  392. }
  393. template<typename... _Args>
  394. iterator
  395. emplace_hint(const_iterator __it, _Args&&... __args)
  396. {
  397. size_type __old_size = _Base::bucket_count();
  398. iterator __res
  399. = _Base::emplace_hint(__it, std::forward<_Args>(__args)...);
  400. this->_M_profile_resize(__old_size);
  401. return __res;
  402. }
  403. void
  404. insert(std::initializer_list<value_type> __l)
  405. {
  406. size_type __old_size = _Base::bucket_count();
  407. _Base::insert(__l);
  408. this->_M_profile_resize(__old_size);
  409. }
  410. iterator
  411. insert(const value_type& __obj)
  412. {
  413. size_type __old_size = _Base::bucket_count();
  414. iterator __res = _Base::insert(__obj);
  415. this->_M_profile_resize(__old_size);
  416. return __res;
  417. }
  418. iterator
  419. insert(const_iterator __iter, const value_type& __v)
  420. {
  421. size_type __old_size = _Base::bucket_count();
  422. iterator __res = _Base::insert(__iter, __v);
  423. this->_M_profile_resize(__old_size);
  424. return __res;
  425. }
  426. template<typename _Pair, typename = typename
  427. std::enable_if<std::is_constructible<value_type,
  428. _Pair&&>::value>::type>
  429. iterator
  430. insert(_Pair&& __obj)
  431. {
  432. size_type __old_size = _Base::bucket_count();
  433. iterator __res = _Base::insert(std::forward<_Pair>(__obj));
  434. this->_M_profile_resize(__old_size);
  435. return __res;
  436. }
  437. template<typename _Pair, typename = typename
  438. std::enable_if<std::is_constructible<value_type,
  439. _Pair&&>::value>::type>
  440. iterator
  441. insert(const_iterator __iter, _Pair&& __v)
  442. {
  443. size_type __old_size = _Base::bucket_count();
  444. iterator __res = _Base::insert(__iter, std::forward<_Pair>(__v));
  445. this->_M_profile_resize(__old_size);
  446. return __res;
  447. }
  448. template<typename _InputIter>
  449. void
  450. insert(_InputIter __first, _InputIter __last)
  451. {
  452. size_type __old_size = _Base::bucket_count();
  453. _Base::insert(__first, __last);
  454. this->_M_profile_resize(__old_size);
  455. }
  456. void
  457. swap(unordered_multimap& __x)
  458. noexcept( noexcept(__x._M_base().swap(__x)) )
  459. {
  460. _Base::swap(__x._M_base());
  461. this->_M_swap(__x);
  462. }
  463. void
  464. rehash(size_type __n)
  465. {
  466. size_type __old_size = _Base::bucket_count();
  467. _Base::rehash(__n);
  468. this->_M_profile_resize(__old_size);
  469. }
  470. };
  471. template<typename _Key, typename _Tp, typename _Hash,
  472. typename _Pred, typename _Alloc>
  473. inline void
  474. swap(unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  475. unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  476. noexcept(noexcept(__x.swap(__y)))
  477. { __x.swap(__y); }
  478. template<typename _Key, typename _Tp, typename _Hash,
  479. typename _Pred, typename _Alloc>
  480. inline bool
  481. operator==(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  482. const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  483. { return static_cast<const _GLIBCXX_STD_BASE&>(__x) == __y; }
  484. template<typename _Key, typename _Tp, typename _Hash,
  485. typename _Pred, typename _Alloc>
  486. inline bool
  487. operator!=(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  488. const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  489. { return !(__x == __y); }
  490. } // namespace __profile
  491. } // namespace std
  492. #undef _GLIBCXX_BASE
  493. #undef _GLIBCXX_STD_BASE
  494. #endif // C++11
  495. #endif