hash_map 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_HASH_MAP
  10. #define _LIBCPP_HASH_MAP
  11. /*
  12. hash_map synopsis
  13. namespace __gnu_cxx
  14. {
  15. template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
  16. class Alloc = allocator<pair<const Key, T>>>
  17. class hash_map
  18. {
  19. public:
  20. // types
  21. typedef Key key_type;
  22. typedef T mapped_type;
  23. typedef Hash hasher;
  24. typedef Pred key_equal;
  25. typedef Alloc allocator_type;
  26. typedef pair<const key_type, mapped_type> value_type;
  27. typedef value_type& reference;
  28. typedef const value_type& const_reference;
  29. typedef typename allocator_traits<allocator_type>::pointer pointer;
  30. typedef typename allocator_traits<allocator_type>::const_pointer const_pointer;
  31. typedef typename allocator_traits<allocator_type>::size_type size_type;
  32. typedef typename allocator_traits<allocator_type>::difference_type difference_type;
  33. typedef /unspecified/ iterator;
  34. typedef /unspecified/ const_iterator;
  35. hash_map();
  36. explicit hash_map(size_type n, const hasher& hf = hasher(),
  37. const key_equal& eql = key_equal(),
  38. const allocator_type& a = allocator_type());
  39. template <class InputIterator>
  40. hash_map(InputIterator f, InputIterator l);
  41. template <class InputIterator>
  42. hash_map(InputIterator f, InputIterator l,
  43. size_type n, const hasher& hf = hasher(),
  44. const key_equal& eql = key_equal(),
  45. const allocator_type& a = allocator_type());
  46. hash_map(const hash_map&);
  47. ~hash_map();
  48. hash_map& operator=(const hash_map&);
  49. allocator_type get_allocator() const;
  50. bool empty() const;
  51. size_type size() const;
  52. size_type max_size() const;
  53. iterator begin();
  54. iterator end();
  55. const_iterator begin() const;
  56. const_iterator end() const;
  57. pair<iterator, bool> insert(const value_type& obj);
  58. template <class InputIterator>
  59. void insert(InputIterator first, InputIterator last);
  60. void erase(const_iterator position);
  61. size_type erase(const key_type& k);
  62. void erase(const_iterator first, const_iterator last);
  63. void clear();
  64. void swap(hash_map&);
  65. hasher hash_funct() const;
  66. key_equal key_eq() const;
  67. iterator find(const key_type& k);
  68. const_iterator find(const key_type& k) const;
  69. size_type count(const key_type& k) const;
  70. pair<iterator, iterator> equal_range(const key_type& k);
  71. pair<const_iterator, const_iterator> equal_range(const key_type& k) const;
  72. mapped_type& operator[](const key_type& k);
  73. size_type bucket_count() const;
  74. size_type max_bucket_count() const;
  75. size_type elems_in_bucket(size_type n) const;
  76. void resize(size_type n);
  77. };
  78. template <class Key, class T, class Hash, class Pred, class Alloc>
  79. void swap(hash_map<Key, T, Hash, Pred, Alloc>& x,
  80. hash_map<Key, T, Hash, Pred, Alloc>& y);
  81. template <class Key, class T, class Hash, class Pred, class Alloc>
  82. bool
  83. operator==(const hash_map<Key, T, Hash, Pred, Alloc>& x,
  84. const hash_map<Key, T, Hash, Pred, Alloc>& y);
  85. template <class Key, class T, class Hash, class Pred, class Alloc>
  86. bool
  87. operator!=(const hash_map<Key, T, Hash, Pred, Alloc>& x,
  88. const hash_map<Key, T, Hash, Pred, Alloc>& y);
  89. template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
  90. class Alloc = allocator<pair<const Key, T>>>
  91. class hash_multimap
  92. {
  93. public:
  94. // types
  95. typedef Key key_type;
  96. typedef T mapped_type;
  97. typedef Hash hasher;
  98. typedef Pred key_equal;
  99. typedef Alloc allocator_type;
  100. typedef pair<const key_type, mapped_type> value_type;
  101. typedef value_type& reference;
  102. typedef const value_type& const_reference;
  103. typedef typename allocator_traits<allocator_type>::pointer pointer;
  104. typedef typename allocator_traits<allocator_type>::const_pointer const_pointer;
  105. typedef typename allocator_traits<allocator_type>::size_type size_type;
  106. typedef typename allocator_traits<allocator_type>::difference_type difference_type;
  107. typedef /unspecified/ iterator;
  108. typedef /unspecified/ const_iterator;
  109. explicit hash_multimap(size_type n = 193, const hasher& hf = hasher(),
  110. const key_equal& eql = key_equal(),
  111. const allocator_type& a = allocator_type());
  112. template <class InputIterator>
  113. hash_multimap(InputIterator f, InputIterator l,
  114. size_type n = 193, const hasher& hf = hasher(),
  115. const key_equal& eql = key_equal(),
  116. const allocator_type& a = allocator_type());
  117. explicit hash_multimap(const allocator_type&);
  118. hash_multimap(const hash_multimap&);
  119. ~hash_multimap();
  120. hash_multimap& operator=(const hash_multimap&);
  121. allocator_type get_allocator() const;
  122. bool empty() const;
  123. size_type size() const;
  124. size_type max_size() const;
  125. iterator begin();
  126. iterator end();
  127. const_iterator begin() const;
  128. const_iterator end() const;
  129. iterator insert(const value_type& obj);
  130. template <class InputIterator>
  131. void insert(InputIterator first, InputIterator last);
  132. void erase(const_iterator position);
  133. size_type erase(const key_type& k);
  134. void erase(const_iterator first, const_iterator last);
  135. void clear();
  136. void swap(hash_multimap&);
  137. hasher hash_funct() const;
  138. key_equal key_eq() const;
  139. iterator find(const key_type& k);
  140. const_iterator find(const key_type& k) const;
  141. size_type count(const key_type& k) const;
  142. pair<iterator, iterator> equal_range(const key_type& k);
  143. pair<const_iterator, const_iterator> equal_range(const key_type& k) const;
  144. size_type bucket_count() const;
  145. size_type max_bucket_count() const;
  146. size_type elems_in_bucket(size_type n) const;
  147. void resize(size_type n);
  148. };
  149. template <class Key, class T, class Hash, class Pred, class Alloc>
  150. void swap(hash_multimap<Key, T, Hash, Pred, Alloc>& x,
  151. hash_multimap<Key, T, Hash, Pred, Alloc>& y);
  152. template <class Key, class T, class Hash, class Pred, class Alloc>
  153. bool
  154. operator==(const hash_multimap<Key, T, Hash, Pred, Alloc>& x,
  155. const hash_multimap<Key, T, Hash, Pred, Alloc>& y);
  156. template <class Key, class T, class Hash, class Pred, class Alloc>
  157. bool
  158. operator!=(const hash_multimap<Key, T, Hash, Pred, Alloc>& x,
  159. const hash_multimap<Key, T, Hash, Pred, Alloc>& y);
  160. } // __gnu_cxx
  161. */
  162. #include <__assert> // all public C++ headers provide the assertion handler
  163. #include <__config>
  164. #include <__hash_table>
  165. #include <algorithm>
  166. #include <ext/__hash>
  167. #include <functional>
  168. #include <stdexcept>
  169. #if defined(__DEPRECATED) && __DEPRECATED
  170. #if defined(_LIBCPP_WARNING)
  171. _LIBCPP_WARNING("Use of the header <ext/hash_map> is deprecated. Migrate to <unordered_map>")
  172. #else
  173. # warning Use of the header <ext/hash_map> is deprecated. Migrate to <unordered_map>
  174. #endif
  175. #endif
  176. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  177. # pragma GCC system_header
  178. #endif
  179. namespace __gnu_cxx {
  180. template <class _Tp, class _Hash,
  181. bool = std::is_empty<_Hash>::value && !std::__libcpp_is_final<_Hash>::value
  182. >
  183. class __hash_map_hasher
  184. : private _Hash
  185. {
  186. public:
  187. _LIBCPP_INLINE_VISIBILITY __hash_map_hasher() : _Hash() {}
  188. _LIBCPP_INLINE_VISIBILITY __hash_map_hasher(const _Hash& __h) : _Hash(__h) {}
  189. _LIBCPP_INLINE_VISIBILITY const _Hash& hash_function() const {return *this;}
  190. _LIBCPP_INLINE_VISIBILITY
  191. size_t operator()(const _Tp& __x) const
  192. {return static_cast<const _Hash&>(*this)(__x.first);}
  193. _LIBCPP_INLINE_VISIBILITY
  194. size_t operator()(const typename _Tp::first_type& __x) const
  195. {return static_cast<const _Hash&>(*this)(__x);}
  196. };
  197. template <class _Tp, class _Hash>
  198. class __hash_map_hasher<_Tp, _Hash, false>
  199. {
  200. _Hash __hash_;
  201. public:
  202. _LIBCPP_INLINE_VISIBILITY __hash_map_hasher() : __hash_() {}
  203. _LIBCPP_INLINE_VISIBILITY __hash_map_hasher(const _Hash& __h) : __hash_(__h) {}
  204. _LIBCPP_INLINE_VISIBILITY const _Hash& hash_function() const {return __hash_;}
  205. _LIBCPP_INLINE_VISIBILITY
  206. size_t operator()(const _Tp& __x) const
  207. {return __hash_(__x.first);}
  208. _LIBCPP_INLINE_VISIBILITY
  209. size_t operator()(const typename _Tp::first_type& __x) const
  210. {return __hash_(__x);}
  211. };
  212. template <class _Tp, class _Pred,
  213. bool = std::is_empty<_Pred>::value && !std::__libcpp_is_final<_Pred>::value
  214. >
  215. class __hash_map_equal
  216. : private _Pred
  217. {
  218. public:
  219. _LIBCPP_INLINE_VISIBILITY __hash_map_equal() : _Pred() {}
  220. _LIBCPP_INLINE_VISIBILITY __hash_map_equal(const _Pred& __p) : _Pred(__p) {}
  221. _LIBCPP_INLINE_VISIBILITY const _Pred& key_eq() const {return *this;}
  222. _LIBCPP_INLINE_VISIBILITY
  223. bool operator()(const _Tp& __x, const _Tp& __y) const
  224. {return static_cast<const _Pred&>(*this)(__x.first, __y.first);}
  225. _LIBCPP_INLINE_VISIBILITY
  226. bool operator()(const typename _Tp::first_type& __x, const _Tp& __y) const
  227. {return static_cast<const _Pred&>(*this)(__x, __y.first);}
  228. _LIBCPP_INLINE_VISIBILITY
  229. bool operator()(const _Tp& __x, const typename _Tp::first_type& __y) const
  230. {return static_cast<const _Pred&>(*this)(__x.first, __y);}
  231. _LIBCPP_INLINE_VISIBILITY
  232. bool operator()(const typename _Tp::first_type& __x,
  233. const typename _Tp::first_type& __y) const
  234. {return static_cast<const _Pred&>(*this)(__x, __y);}
  235. };
  236. template <class _Tp, class _Pred>
  237. class __hash_map_equal<_Tp, _Pred, false>
  238. {
  239. _Pred __pred_;
  240. public:
  241. _LIBCPP_INLINE_VISIBILITY __hash_map_equal() : __pred_() {}
  242. _LIBCPP_INLINE_VISIBILITY __hash_map_equal(const _Pred& __p) : __pred_(__p) {}
  243. _LIBCPP_INLINE_VISIBILITY const _Pred& key_eq() const {return __pred_;}
  244. _LIBCPP_INLINE_VISIBILITY
  245. bool operator()(const _Tp& __x, const _Tp& __y) const
  246. {return __pred_(__x.first, __y.first);}
  247. _LIBCPP_INLINE_VISIBILITY
  248. bool operator()(const typename _Tp::first_type& __x, const _Tp& __y) const
  249. {return __pred_(__x, __y.first);}
  250. _LIBCPP_INLINE_VISIBILITY
  251. bool operator()(const _Tp& __x, const typename _Tp::first_type& __y) const
  252. {return __pred_(__x.first, __y);}
  253. _LIBCPP_INLINE_VISIBILITY
  254. bool operator()(const typename _Tp::first_type& __x,
  255. const typename _Tp::first_type& __y) const
  256. {return __pred_(__x, __y);}
  257. };
  258. template <class _Alloc>
  259. class __hash_map_node_destructor
  260. {
  261. typedef _Alloc allocator_type;
  262. typedef std::allocator_traits<allocator_type> __alloc_traits;
  263. typedef typename __alloc_traits::value_type::__node_value_type value_type;
  264. public:
  265. typedef typename __alloc_traits::pointer pointer;
  266. private:
  267. typedef typename value_type::first_type first_type;
  268. typedef typename value_type::second_type second_type;
  269. allocator_type& __na_;
  270. public:
  271. bool __first_constructed;
  272. bool __second_constructed;
  273. _LIBCPP_HIDE_FROM_ABI __hash_map_node_destructor(__hash_map_node_destructor const&) = default;
  274. __hash_map_node_destructor& operator=(const __hash_map_node_destructor&) = delete;
  275. _LIBCPP_INLINE_VISIBILITY
  276. explicit __hash_map_node_destructor(allocator_type& __na)
  277. : __na_(__na),
  278. __first_constructed(false),
  279. __second_constructed(false)
  280. {}
  281. #ifndef _LIBCPP_CXX03_LANG
  282. _LIBCPP_INLINE_VISIBILITY
  283. __hash_map_node_destructor(std::__hash_node_destructor<allocator_type>&& __x)
  284. : __na_(__x.__na_),
  285. __first_constructed(__x.__value_constructed),
  286. __second_constructed(__x.__value_constructed)
  287. {
  288. __x.__value_constructed = false;
  289. }
  290. #else // _LIBCPP_CXX03_LANG
  291. _LIBCPP_INLINE_VISIBILITY
  292. __hash_map_node_destructor(const std::__hash_node_destructor<allocator_type>& __x)
  293. : __na_(__x.__na_),
  294. __first_constructed(__x.__value_constructed),
  295. __second_constructed(__x.__value_constructed)
  296. {
  297. const_cast<bool&>(__x.__value_constructed) = false;
  298. }
  299. #endif // _LIBCPP_CXX03_LANG
  300. _LIBCPP_INLINE_VISIBILITY
  301. void operator()(pointer __p)
  302. {
  303. if (__second_constructed)
  304. __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__get_value().second));
  305. if (__first_constructed)
  306. __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__get_value().first));
  307. if (__p)
  308. __alloc_traits::deallocate(__na_, __p, 1);
  309. }
  310. };
  311. template <class _HashIterator>
  312. class _LIBCPP_TEMPLATE_VIS __hash_map_iterator
  313. {
  314. _HashIterator __i_;
  315. typedef const typename _HashIterator::value_type::first_type key_type;
  316. typedef typename _HashIterator::value_type::second_type mapped_type;
  317. public:
  318. typedef std::forward_iterator_tag iterator_category;
  319. typedef std::pair<key_type, mapped_type> value_type;
  320. typedef typename _HashIterator::difference_type difference_type;
  321. typedef value_type& reference;
  322. typedef std::__rebind_pointer_t<typename _HashIterator::pointer, value_type>
  323. pointer;
  324. _LIBCPP_INLINE_VISIBILITY __hash_map_iterator() {}
  325. _LIBCPP_INLINE_VISIBILITY __hash_map_iterator(_HashIterator __i) : __i_(__i) {}
  326. _LIBCPP_INLINE_VISIBILITY reference operator*() const {return *operator->();}
  327. _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return (pointer)__i_.operator->();}
  328. _LIBCPP_INLINE_VISIBILITY __hash_map_iterator& operator++() {++__i_; return *this;}
  329. _LIBCPP_INLINE_VISIBILITY
  330. __hash_map_iterator operator++(int)
  331. {
  332. __hash_map_iterator __t(*this);
  333. ++(*this);
  334. return __t;
  335. }
  336. friend _LIBCPP_INLINE_VISIBILITY
  337. bool operator==(const __hash_map_iterator& __x, const __hash_map_iterator& __y)
  338. {return __x.__i_ == __y.__i_;}
  339. friend _LIBCPP_INLINE_VISIBILITY
  340. bool operator!=(const __hash_map_iterator& __x, const __hash_map_iterator& __y)
  341. {return __x.__i_ != __y.__i_;}
  342. template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS hash_map;
  343. template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS hash_multimap;
  344. template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
  345. template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
  346. template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
  347. };
  348. template <class _HashIterator>
  349. class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator
  350. {
  351. _HashIterator __i_;
  352. typedef const typename _HashIterator::value_type::first_type key_type;
  353. typedef typename _HashIterator::value_type::second_type mapped_type;
  354. public:
  355. typedef std::forward_iterator_tag iterator_category;
  356. typedef std::pair<key_type, mapped_type> value_type;
  357. typedef typename _HashIterator::difference_type difference_type;
  358. typedef const value_type& reference;
  359. typedef std::__rebind_pointer_t<typename _HashIterator::pointer, const value_type>
  360. pointer;
  361. _LIBCPP_INLINE_VISIBILITY __hash_map_const_iterator() {}
  362. _LIBCPP_INLINE_VISIBILITY
  363. __hash_map_const_iterator(_HashIterator __i) : __i_(__i) {}
  364. _LIBCPP_INLINE_VISIBILITY
  365. __hash_map_const_iterator(
  366. __hash_map_iterator<typename _HashIterator::__non_const_iterator> __i)
  367. : __i_(__i.__i_) {}
  368. _LIBCPP_INLINE_VISIBILITY
  369. reference operator*() const {return *operator->();}
  370. _LIBCPP_INLINE_VISIBILITY
  371. pointer operator->() const {return (pointer)__i_.operator->();}
  372. _LIBCPP_INLINE_VISIBILITY
  373. __hash_map_const_iterator& operator++() {++__i_; return *this;}
  374. _LIBCPP_INLINE_VISIBILITY
  375. __hash_map_const_iterator operator++(int)
  376. {
  377. __hash_map_const_iterator __t(*this);
  378. ++(*this);
  379. return __t;
  380. }
  381. friend _LIBCPP_INLINE_VISIBILITY
  382. bool operator==(const __hash_map_const_iterator& __x, const __hash_map_const_iterator& __y)
  383. {return __x.__i_ == __y.__i_;}
  384. friend _LIBCPP_INLINE_VISIBILITY
  385. bool operator!=(const __hash_map_const_iterator& __x, const __hash_map_const_iterator& __y)
  386. {return __x.__i_ != __y.__i_;}
  387. template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS hash_map;
  388. template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS hash_multimap;
  389. template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
  390. template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
  391. };
  392. template <class _Key, class _Tp, class _Hash = hash<_Key>, class _Pred = std::equal_to<_Key>,
  393. class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
  394. class _LIBCPP_TEMPLATE_VIS hash_map
  395. {
  396. public:
  397. // types
  398. typedef _Key key_type;
  399. typedef _Tp mapped_type;
  400. typedef _Tp data_type;
  401. typedef _Hash hasher;
  402. typedef _Pred key_equal;
  403. typedef _Alloc allocator_type;
  404. typedef std::pair<const key_type, mapped_type> value_type;
  405. typedef value_type& reference;
  406. typedef const value_type& const_reference;
  407. private:
  408. typedef std::pair<key_type, mapped_type> __value_type;
  409. typedef __hash_map_hasher<__value_type, hasher> __hasher;
  410. typedef __hash_map_equal<__value_type, key_equal> __key_equal;
  411. typedef std::__rebind_alloc<std::allocator_traits<allocator_type>, __value_type> __allocator_type;
  412. typedef std::__hash_table<__value_type, __hasher,
  413. __key_equal, __allocator_type> __table;
  414. __table __table_;
  415. typedef typename __table::__node_pointer __node_pointer;
  416. typedef typename __table::__node_const_pointer __node_const_pointer;
  417. typedef typename __table::__node_traits __node_traits;
  418. typedef typename __table::__node_allocator __node_allocator;
  419. typedef typename __table::__node __node;
  420. typedef __hash_map_node_destructor<__node_allocator> _Dp;
  421. typedef std::unique_ptr<__node, _Dp> __node_holder;
  422. typedef std::allocator_traits<allocator_type> __alloc_traits;
  423. public:
  424. typedef typename __alloc_traits::pointer pointer;
  425. typedef typename __alloc_traits::const_pointer const_pointer;
  426. typedef typename __alloc_traits::size_type size_type;
  427. typedef typename __alloc_traits::difference_type difference_type;
  428. typedef __hash_map_iterator<typename __table::iterator> iterator;
  429. typedef __hash_map_const_iterator<typename __table::const_iterator> const_iterator;
  430. _LIBCPP_INLINE_VISIBILITY hash_map() { }
  431. explicit _LIBCPP_HIDE_FROM_ABI hash_map(size_type __n, const hasher& __hf = hasher(),
  432. const key_equal& __eql = key_equal());
  433. _LIBCPP_HIDE_FROM_ABI hash_map(size_type __n, const hasher& __hf,
  434. const key_equal& __eql,
  435. const allocator_type& __a);
  436. template <class _InputIterator>
  437. _LIBCPP_HIDE_FROM_ABI hash_map(_InputIterator __first, _InputIterator __last);
  438. template <class _InputIterator>
  439. _LIBCPP_HIDE_FROM_ABI hash_map(_InputIterator __first, _InputIterator __last,
  440. size_type __n, const hasher& __hf = hasher(),
  441. const key_equal& __eql = key_equal());
  442. template <class _InputIterator>
  443. _LIBCPP_HIDE_FROM_ABI hash_map(_InputIterator __first, _InputIterator __last,
  444. size_type __n, const hasher& __hf,
  445. const key_equal& __eql,
  446. const allocator_type& __a);
  447. _LIBCPP_HIDE_FROM_ABI hash_map(const hash_map& __u);
  448. _LIBCPP_INLINE_VISIBILITY
  449. allocator_type get_allocator() const
  450. {return allocator_type(__table_.__node_alloc());}
  451. _LIBCPP_INLINE_VISIBILITY
  452. bool empty() const {return __table_.size() == 0;}
  453. _LIBCPP_INLINE_VISIBILITY
  454. size_type size() const {return __table_.size();}
  455. _LIBCPP_INLINE_VISIBILITY
  456. size_type max_size() const {return __table_.max_size();}
  457. _LIBCPP_INLINE_VISIBILITY
  458. iterator begin() {return __table_.begin();}
  459. _LIBCPP_INLINE_VISIBILITY
  460. iterator end() {return __table_.end();}
  461. _LIBCPP_INLINE_VISIBILITY
  462. const_iterator begin() const {return __table_.begin();}
  463. _LIBCPP_INLINE_VISIBILITY
  464. const_iterator end() const {return __table_.end();}
  465. _LIBCPP_INLINE_VISIBILITY
  466. std::pair<iterator, bool> insert(const value_type& __x)
  467. {return __table_.__insert_unique(__x);}
  468. _LIBCPP_INLINE_VISIBILITY
  469. iterator insert(const_iterator, const value_type& __x) {return insert(__x).first;}
  470. template <class _InputIterator>
  471. _LIBCPP_INLINE_VISIBILITY
  472. void insert(_InputIterator __first, _InputIterator __last);
  473. _LIBCPP_INLINE_VISIBILITY
  474. void erase(const_iterator __p) {__table_.erase(__p.__i_);}
  475. _LIBCPP_INLINE_VISIBILITY
  476. size_type erase(const key_type& __k) {return __table_.__erase_unique(__k);}
  477. _LIBCPP_INLINE_VISIBILITY
  478. void erase(const_iterator __first, const_iterator __last)
  479. {__table_.erase(__first.__i_, __last.__i_);}
  480. _LIBCPP_INLINE_VISIBILITY
  481. void clear() {__table_.clear();}
  482. _LIBCPP_INLINE_VISIBILITY
  483. void swap(hash_map& __u) {__table_.swap(__u.__table_);}
  484. _LIBCPP_INLINE_VISIBILITY
  485. hasher hash_funct() const
  486. {return __table_.hash_function().hash_function();}
  487. _LIBCPP_INLINE_VISIBILITY
  488. key_equal key_eq() const
  489. {return __table_.key_eq().key_eq();}
  490. _LIBCPP_INLINE_VISIBILITY
  491. iterator find(const key_type& __k) {return __table_.find(__k);}
  492. _LIBCPP_INLINE_VISIBILITY
  493. const_iterator find(const key_type& __k) const {return __table_.find(__k);}
  494. _LIBCPP_INLINE_VISIBILITY
  495. size_type count(const key_type& __k) const {return __table_.__count_unique(__k);}
  496. _LIBCPP_INLINE_VISIBILITY
  497. std::pair<iterator, iterator> equal_range(const key_type& __k)
  498. {return __table_.__equal_range_unique(__k);}
  499. _LIBCPP_INLINE_VISIBILITY
  500. std::pair<const_iterator, const_iterator> equal_range(const key_type& __k) const
  501. {return __table_.__equal_range_unique(__k);}
  502. _LIBCPP_HIDE_FROM_ABI mapped_type& operator[](const key_type& __k);
  503. _LIBCPP_INLINE_VISIBILITY
  504. size_type bucket_count() const {return __table_.bucket_count();}
  505. _LIBCPP_INLINE_VISIBILITY
  506. size_type max_bucket_count() const {return __table_.max_bucket_count();}
  507. _LIBCPP_INLINE_VISIBILITY
  508. size_type elems_in_bucket(size_type __n) const
  509. {return __table_.bucket_size(__n);}
  510. _LIBCPP_INLINE_VISIBILITY
  511. void resize(size_type __n) {__table_.__rehash_unique(__n);}
  512. private:
  513. _LIBCPP_HIDE_FROM_ABI __node_holder __construct_node(const key_type& __k);
  514. };
  515. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  516. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
  517. size_type __n, const hasher& __hf, const key_equal& __eql)
  518. : __table_(__hf, __eql)
  519. {
  520. __table_.__rehash_unique(__n);
  521. }
  522. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  523. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
  524. size_type __n, const hasher& __hf, const key_equal& __eql,
  525. const allocator_type& __a)
  526. : __table_(__hf, __eql, __a)
  527. {
  528. __table_.__rehash_unique(__n);
  529. }
  530. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  531. template <class _InputIterator>
  532. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
  533. _InputIterator __first, _InputIterator __last)
  534. {
  535. insert(__first, __last);
  536. }
  537. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  538. template <class _InputIterator>
  539. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
  540. _InputIterator __first, _InputIterator __last, size_type __n,
  541. const hasher& __hf, const key_equal& __eql)
  542. : __table_(__hf, __eql)
  543. {
  544. __table_.__rehash_unique(__n);
  545. insert(__first, __last);
  546. }
  547. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  548. template <class _InputIterator>
  549. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
  550. _InputIterator __first, _InputIterator __last, size_type __n,
  551. const hasher& __hf, const key_equal& __eql, const allocator_type& __a)
  552. : __table_(__hf, __eql, __a)
  553. {
  554. __table_.__rehash_unique(__n);
  555. insert(__first, __last);
  556. }
  557. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  558. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
  559. const hash_map& __u)
  560. : __table_(__u.__table_)
  561. {
  562. __table_.__rehash_unique(__u.bucket_count());
  563. insert(__u.begin(), __u.end());
  564. }
  565. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  566. typename hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::__node_holder
  567. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::__construct_node(const key_type& __k)
  568. {
  569. __node_allocator& __na = __table_.__node_alloc();
  570. __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
  571. __node_traits::construct(__na, _VSTD::addressof(__h->__get_value().first), __k);
  572. __h.get_deleter().__first_constructed = true;
  573. __node_traits::construct(__na, _VSTD::addressof(__h->__get_value().second));
  574. __h.get_deleter().__second_constructed = true;
  575. return __h;
  576. }
  577. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  578. template <class _InputIterator>
  579. inline
  580. void
  581. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::insert(_InputIterator __first,
  582. _InputIterator __last)
  583. {
  584. for (; __first != __last; ++__first)
  585. __table_.__insert_unique(*__first);
  586. }
  587. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  588. _Tp&
  589. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::operator[](const key_type& __k)
  590. {
  591. iterator __i = find(__k);
  592. if (__i != end())
  593. return __i->second;
  594. __node_holder __h = __construct_node(__k);
  595. std::pair<iterator, bool> __r = __table_.__node_insert_unique(__h.get());
  596. __h.release();
  597. return __r.first->second;
  598. }
  599. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  600. inline _LIBCPP_INLINE_VISIBILITY
  601. void
  602. swap(hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  603. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  604. {
  605. __x.swap(__y);
  606. }
  607. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  608. _LIBCPP_HIDE_FROM_ABI bool
  609. operator==(const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  610. const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  611. {
  612. if (__x.size() != __y.size())
  613. return false;
  614. typedef typename hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::const_iterator
  615. const_iterator;
  616. for (const_iterator __i = __x.begin(), __ex = __x.end(), __ey = __y.end();
  617. __i != __ex; ++__i)
  618. {
  619. const_iterator __j = __y.find(__i->first);
  620. if (__j == __ey || !(*__i == *__j))
  621. return false;
  622. }
  623. return true;
  624. }
  625. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  626. inline _LIBCPP_INLINE_VISIBILITY
  627. bool
  628. operator!=(const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  629. const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  630. {
  631. return !(__x == __y);
  632. }
  633. template <class _Key, class _Tp, class _Hash = hash<_Key>, class _Pred = std::equal_to<_Key>,
  634. class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
  635. class _LIBCPP_TEMPLATE_VIS hash_multimap
  636. {
  637. public:
  638. // types
  639. typedef _Key key_type;
  640. typedef _Tp mapped_type;
  641. typedef _Tp data_type;
  642. typedef _Hash hasher;
  643. typedef _Pred key_equal;
  644. typedef _Alloc allocator_type;
  645. typedef std::pair<const key_type, mapped_type> value_type;
  646. typedef value_type& reference;
  647. typedef const value_type& const_reference;
  648. private:
  649. typedef std::pair<key_type, mapped_type> __value_type;
  650. typedef __hash_map_hasher<__value_type, hasher> __hasher;
  651. typedef __hash_map_equal<__value_type, key_equal> __key_equal;
  652. typedef std::__rebind_alloc<std::allocator_traits<allocator_type>, __value_type> __allocator_type;
  653. typedef std::__hash_table<__value_type, __hasher,
  654. __key_equal, __allocator_type> __table;
  655. __table __table_;
  656. typedef typename __table::__node_traits __node_traits;
  657. typedef typename __table::__node_allocator __node_allocator;
  658. typedef typename __table::__node __node;
  659. typedef __hash_map_node_destructor<__node_allocator> _Dp;
  660. typedef std::unique_ptr<__node, _Dp> __node_holder;
  661. typedef std::allocator_traits<allocator_type> __alloc_traits;
  662. public:
  663. typedef typename __alloc_traits::pointer pointer;
  664. typedef typename __alloc_traits::const_pointer const_pointer;
  665. typedef typename __alloc_traits::size_type size_type;
  666. typedef typename __alloc_traits::difference_type difference_type;
  667. typedef __hash_map_iterator<typename __table::iterator> iterator;
  668. typedef __hash_map_const_iterator<typename __table::const_iterator> const_iterator;
  669. _LIBCPP_INLINE_VISIBILITY
  670. hash_multimap() { }
  671. explicit _LIBCPP_HIDE_FROM_ABI hash_multimap(size_type __n, const hasher& __hf = hasher(),
  672. const key_equal& __eql = key_equal());
  673. _LIBCPP_HIDE_FROM_ABI hash_multimap(size_type __n, const hasher& __hf,
  674. const key_equal& __eql,
  675. const allocator_type& __a);
  676. template <class _InputIterator>
  677. _LIBCPP_HIDE_FROM_ABI hash_multimap(_InputIterator __first, _InputIterator __last);
  678. template <class _InputIterator>
  679. _LIBCPP_HIDE_FROM_ABI hash_multimap(_InputIterator __first, _InputIterator __last,
  680. size_type __n, const hasher& __hf = hasher(),
  681. const key_equal& __eql = key_equal());
  682. template <class _InputIterator>
  683. _LIBCPP_HIDE_FROM_ABI hash_multimap(_InputIterator __first, _InputIterator __last,
  684. size_type __n, const hasher& __hf,
  685. const key_equal& __eql,
  686. const allocator_type& __a);
  687. _LIBCPP_HIDE_FROM_ABI hash_multimap(const hash_multimap& __u);
  688. _LIBCPP_INLINE_VISIBILITY
  689. allocator_type get_allocator() const
  690. {return allocator_type(__table_.__node_alloc());}
  691. _LIBCPP_INLINE_VISIBILITY
  692. bool empty() const {return __table_.size() == 0;}
  693. _LIBCPP_INLINE_VISIBILITY
  694. size_type size() const {return __table_.size();}
  695. _LIBCPP_INLINE_VISIBILITY
  696. size_type max_size() const {return __table_.max_size();}
  697. _LIBCPP_INLINE_VISIBILITY
  698. iterator begin() {return __table_.begin();}
  699. _LIBCPP_INLINE_VISIBILITY
  700. iterator end() {return __table_.end();}
  701. _LIBCPP_INLINE_VISIBILITY
  702. const_iterator begin() const {return __table_.begin();}
  703. _LIBCPP_INLINE_VISIBILITY
  704. const_iterator end() const {return __table_.end();}
  705. _LIBCPP_INLINE_VISIBILITY
  706. iterator insert(const value_type& __x) {return __table_.__insert_multi(__x);}
  707. _LIBCPP_INLINE_VISIBILITY
  708. iterator insert(const_iterator, const value_type& __x) {return insert(__x);}
  709. template <class _InputIterator>
  710. _LIBCPP_INLINE_VISIBILITY
  711. void insert(_InputIterator __first, _InputIterator __last);
  712. _LIBCPP_INLINE_VISIBILITY
  713. void erase(const_iterator __p) {__table_.erase(__p.__i_);}
  714. _LIBCPP_INLINE_VISIBILITY
  715. size_type erase(const key_type& __k) {return __table_.__erase_multi(__k);}
  716. _LIBCPP_INLINE_VISIBILITY
  717. void erase(const_iterator __first, const_iterator __last)
  718. {__table_.erase(__first.__i_, __last.__i_);}
  719. _LIBCPP_INLINE_VISIBILITY
  720. void clear() {__table_.clear();}
  721. _LIBCPP_INLINE_VISIBILITY
  722. void swap(hash_multimap& __u) {__table_.swap(__u.__table_);}
  723. _LIBCPP_INLINE_VISIBILITY
  724. hasher hash_funct() const
  725. {return __table_.hash_function().hash_function();}
  726. _LIBCPP_INLINE_VISIBILITY
  727. key_equal key_eq() const
  728. {return __table_.key_eq().key_eq();}
  729. _LIBCPP_INLINE_VISIBILITY
  730. iterator find(const key_type& __k) {return __table_.find(__k);}
  731. _LIBCPP_INLINE_VISIBILITY
  732. const_iterator find(const key_type& __k) const {return __table_.find(__k);}
  733. _LIBCPP_INLINE_VISIBILITY
  734. size_type count(const key_type& __k) const {return __table_.__count_multi(__k);}
  735. _LIBCPP_INLINE_VISIBILITY
  736. std::pair<iterator, iterator> equal_range(const key_type& __k)
  737. {return __table_.__equal_range_multi(__k);}
  738. _LIBCPP_INLINE_VISIBILITY
  739. std::pair<const_iterator, const_iterator> equal_range(const key_type& __k) const
  740. {return __table_.__equal_range_multi(__k);}
  741. _LIBCPP_INLINE_VISIBILITY
  742. size_type bucket_count() const {return __table_.bucket_count();}
  743. _LIBCPP_INLINE_VISIBILITY
  744. size_type max_bucket_count() const {return __table_.max_bucket_count();}
  745. _LIBCPP_INLINE_VISIBILITY
  746. size_type elems_in_bucket(size_type __n) const
  747. {return __table_.bucket_size(__n);}
  748. _LIBCPP_INLINE_VISIBILITY
  749. void resize(size_type __n) {__table_.__rehash_multi(__n);}
  750. };
  751. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  752. hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
  753. size_type __n, const hasher& __hf, const key_equal& __eql)
  754. : __table_(__hf, __eql)
  755. {
  756. __table_.__rehash_multi(__n);
  757. }
  758. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  759. hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
  760. size_type __n, const hasher& __hf, const key_equal& __eql,
  761. const allocator_type& __a)
  762. : __table_(__hf, __eql, __a)
  763. {
  764. __table_.__rehash_multi(__n);
  765. }
  766. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  767. template <class _InputIterator>
  768. hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
  769. _InputIterator __first, _InputIterator __last)
  770. {
  771. insert(__first, __last);
  772. }
  773. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  774. template <class _InputIterator>
  775. hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
  776. _InputIterator __first, _InputIterator __last, size_type __n,
  777. const hasher& __hf, const key_equal& __eql)
  778. : __table_(__hf, __eql)
  779. {
  780. __table_.__rehash_multi(__n);
  781. insert(__first, __last);
  782. }
  783. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  784. template <class _InputIterator>
  785. hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
  786. _InputIterator __first, _InputIterator __last, size_type __n,
  787. const hasher& __hf, const key_equal& __eql, const allocator_type& __a)
  788. : __table_(__hf, __eql, __a)
  789. {
  790. __table_.__rehash_multi(__n);
  791. insert(__first, __last);
  792. }
  793. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  794. hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
  795. const hash_multimap& __u)
  796. : __table_(__u.__table_)
  797. {
  798. __table_.__rehash_multi(__u.bucket_count());
  799. insert(__u.begin(), __u.end());
  800. }
  801. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  802. template <class _InputIterator>
  803. inline
  804. void
  805. hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::insert(_InputIterator __first,
  806. _InputIterator __last)
  807. {
  808. for (; __first != __last; ++__first)
  809. __table_.__insert_multi(*__first);
  810. }
  811. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  812. inline _LIBCPP_INLINE_VISIBILITY
  813. void
  814. swap(hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  815. hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  816. {
  817. __x.swap(__y);
  818. }
  819. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  820. _LIBCPP_HIDE_FROM_ABI bool
  821. operator==(const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  822. const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  823. {
  824. if (__x.size() != __y.size())
  825. return false;
  826. typedef typename hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::const_iterator
  827. const_iterator;
  828. typedef std::pair<const_iterator, const_iterator> _EqRng;
  829. for (const_iterator __i = __x.begin(), __ex = __x.end(); __i != __ex;)
  830. {
  831. _EqRng __xeq = __x.equal_range(__i->first);
  832. _EqRng __yeq = __y.equal_range(__i->first);
  833. if (_VSTD::distance(__xeq.first, __xeq.second) !=
  834. _VSTD::distance(__yeq.first, __yeq.second) ||
  835. !_VSTD::is_permutation(__xeq.first, __xeq.second, __yeq.first))
  836. return false;
  837. __i = __xeq.second;
  838. }
  839. return true;
  840. }
  841. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  842. inline _LIBCPP_INLINE_VISIBILITY
  843. bool
  844. operator!=(const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  845. const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  846. {
  847. return !(__x == __y);
  848. }
  849. } // namespace __gnu_cxx
  850. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  851. # include <concepts>
  852. # include <iterator>
  853. # include <type_traits>
  854. #endif
  855. #endif // _LIBCPP_HASH_MAP