hash_map 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. // -*- C++ -*-
  2. //===-------------------------- hash_map ----------------------------------===//
  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 <__config>
  163. #include <__hash_table>
  164. #include <functional>
  165. #include <stdexcept>
  166. #include <type_traits>
  167. #include <ext/__hash>
  168. #if __DEPRECATED
  169. #if defined(_LIBCPP_WARNING)
  170. _LIBCPP_WARNING("Use of the header <ext/hash_map> is deprecated. Migrate to <unordered_map>")
  171. #else
  172. # warning Use of the header <ext/hash_map> is deprecated. Migrate to <unordered_map>
  173. #endif
  174. #endif
  175. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  176. #pragma GCC system_header
  177. #endif
  178. namespace __gnu_cxx {
  179. template <class _Tp, class _Hash,
  180. bool = std::is_empty<_Hash>::value && !std::__libcpp_is_final<_Hash>::value
  181. >
  182. class __hash_map_hasher
  183. : private _Hash
  184. {
  185. public:
  186. _LIBCPP_INLINE_VISIBILITY __hash_map_hasher() : _Hash() {}
  187. _LIBCPP_INLINE_VISIBILITY __hash_map_hasher(const _Hash& __h) : _Hash(__h) {}
  188. _LIBCPP_INLINE_VISIBILITY const _Hash& hash_function() const {return *this;}
  189. _LIBCPP_INLINE_VISIBILITY
  190. size_t operator()(const _Tp& __x) const
  191. {return static_cast<const _Hash&>(*this)(__x.first);}
  192. _LIBCPP_INLINE_VISIBILITY
  193. size_t operator()(const typename _Tp::first_type& __x) const
  194. {return static_cast<const _Hash&>(*this)(__x);}
  195. };
  196. template <class _Tp, class _Hash>
  197. class __hash_map_hasher<_Tp, _Hash, false>
  198. {
  199. _Hash __hash_;
  200. public:
  201. _LIBCPP_INLINE_VISIBILITY __hash_map_hasher() : __hash_() {}
  202. _LIBCPP_INLINE_VISIBILITY __hash_map_hasher(const _Hash& __h) : __hash_(__h) {}
  203. _LIBCPP_INLINE_VISIBILITY const _Hash& hash_function() const {return __hash_;}
  204. _LIBCPP_INLINE_VISIBILITY
  205. size_t operator()(const _Tp& __x) const
  206. {return __hash_(__x.first);}
  207. _LIBCPP_INLINE_VISIBILITY
  208. size_t operator()(const typename _Tp::first_type& __x) const
  209. {return __hash_(__x);}
  210. };
  211. template <class _Tp, class _Pred,
  212. bool = std::is_empty<_Pred>::value && !std::__libcpp_is_final<_Pred>::value
  213. >
  214. class __hash_map_equal
  215. : private _Pred
  216. {
  217. public:
  218. _LIBCPP_INLINE_VISIBILITY __hash_map_equal() : _Pred() {}
  219. _LIBCPP_INLINE_VISIBILITY __hash_map_equal(const _Pred& __p) : _Pred(__p) {}
  220. _LIBCPP_INLINE_VISIBILITY const _Pred& key_eq() const {return *this;}
  221. _LIBCPP_INLINE_VISIBILITY
  222. bool operator()(const _Tp& __x, const _Tp& __y) const
  223. {return static_cast<const _Pred&>(*this)(__x.first, __y.first);}
  224. _LIBCPP_INLINE_VISIBILITY
  225. bool operator()(const typename _Tp::first_type& __x, const _Tp& __y) const
  226. {return static_cast<const _Pred&>(*this)(__x, __y.first);}
  227. _LIBCPP_INLINE_VISIBILITY
  228. bool operator()(const _Tp& __x, const typename _Tp::first_type& __y) const
  229. {return static_cast<const _Pred&>(*this)(__x.first, __y);}
  230. _LIBCPP_INLINE_VISIBILITY
  231. bool operator()(const typename _Tp::first_type& __x,
  232. const typename _Tp::first_type& __y) const
  233. {return static_cast<const _Pred&>(*this)(__x, __y);}
  234. };
  235. template <class _Tp, class _Pred>
  236. class __hash_map_equal<_Tp, _Pred, false>
  237. {
  238. _Pred __pred_;
  239. public:
  240. _LIBCPP_INLINE_VISIBILITY __hash_map_equal() : __pred_() {}
  241. _LIBCPP_INLINE_VISIBILITY __hash_map_equal(const _Pred& __p) : __pred_(__p) {}
  242. _LIBCPP_INLINE_VISIBILITY const _Pred& key_eq() const {return __pred_;}
  243. _LIBCPP_INLINE_VISIBILITY
  244. bool operator()(const _Tp& __x, const _Tp& __y) const
  245. {return __pred_(__x.first, __y.first);}
  246. _LIBCPP_INLINE_VISIBILITY
  247. bool operator()(const typename _Tp::first_type& __x, const _Tp& __y) const
  248. {return __pred_(__x, __y.first);}
  249. _LIBCPP_INLINE_VISIBILITY
  250. bool operator()(const _Tp& __x, const typename _Tp::first_type& __y) const
  251. {return __pred_(__x.first, __y);}
  252. _LIBCPP_INLINE_VISIBILITY
  253. bool operator()(const typename _Tp::first_type& __x,
  254. const typename _Tp::first_type& __y) const
  255. {return __pred_(__x, __y);}
  256. };
  257. template <class _Alloc>
  258. class __hash_map_node_destructor
  259. {
  260. typedef _Alloc allocator_type;
  261. typedef std::allocator_traits<allocator_type> __alloc_traits;
  262. typedef typename __alloc_traits::value_type::__node_value_type value_type;
  263. public:
  264. typedef typename __alloc_traits::pointer pointer;
  265. private:
  266. typedef typename value_type::first_type first_type;
  267. typedef typename value_type::second_type second_type;
  268. allocator_type& __na_;
  269. __hash_map_node_destructor& operator=(const __hash_map_node_destructor&);
  270. public:
  271. bool __first_constructed;
  272. bool __second_constructed;
  273. _LIBCPP_INLINE_VISIBILITY
  274. explicit __hash_map_node_destructor(allocator_type& __na)
  275. : __na_(__na),
  276. __first_constructed(false),
  277. __second_constructed(false)
  278. {}
  279. #ifndef _LIBCPP_CXX03_LANG
  280. _LIBCPP_INLINE_VISIBILITY
  281. __hash_map_node_destructor(std::__hash_node_destructor<allocator_type>&& __x)
  282. : __na_(__x.__na_),
  283. __first_constructed(__x.__value_constructed),
  284. __second_constructed(__x.__value_constructed)
  285. {
  286. __x.__value_constructed = false;
  287. }
  288. #else // _LIBCPP_CXX03_LANG
  289. _LIBCPP_INLINE_VISIBILITY
  290. __hash_map_node_destructor(const std::__hash_node_destructor<allocator_type>& __x)
  291. : __na_(__x.__na_),
  292. __first_constructed(__x.__value_constructed),
  293. __second_constructed(__x.__value_constructed)
  294. {
  295. const_cast<bool&>(__x.__value_constructed) = false;
  296. }
  297. #endif // _LIBCPP_CXX03_LANG
  298. _LIBCPP_INLINE_VISIBILITY
  299. void operator()(pointer __p)
  300. {
  301. if (__second_constructed)
  302. __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_.second));
  303. if (__first_constructed)
  304. __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_.first));
  305. if (__p)
  306. __alloc_traits::deallocate(__na_, __p, 1);
  307. }
  308. };
  309. template <class _HashIterator>
  310. class _LIBCPP_TEMPLATE_VIS __hash_map_iterator
  311. {
  312. _HashIterator __i_;
  313. typedef const typename _HashIterator::value_type::first_type key_type;
  314. typedef typename _HashIterator::value_type::second_type mapped_type;
  315. public:
  316. typedef std::forward_iterator_tag iterator_category;
  317. typedef std::pair<key_type, mapped_type> value_type;
  318. typedef typename _HashIterator::difference_type difference_type;
  319. typedef value_type& reference;
  320. typedef typename std::__rebind_pointer<typename _HashIterator::pointer, value_type>::type
  321. pointer;
  322. _LIBCPP_INLINE_VISIBILITY __hash_map_iterator() {}
  323. _LIBCPP_INLINE_VISIBILITY __hash_map_iterator(_HashIterator __i) : __i_(__i) {}
  324. _LIBCPP_INLINE_VISIBILITY reference operator*() const {return *operator->();}
  325. _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return (pointer)__i_.operator->();}
  326. _LIBCPP_INLINE_VISIBILITY __hash_map_iterator& operator++() {++__i_; return *this;}
  327. _LIBCPP_INLINE_VISIBILITY
  328. __hash_map_iterator operator++(int)
  329. {
  330. __hash_map_iterator __t(*this);
  331. ++(*this);
  332. return __t;
  333. }
  334. friend _LIBCPP_INLINE_VISIBILITY
  335. bool operator==(const __hash_map_iterator& __x, const __hash_map_iterator& __y)
  336. {return __x.__i_ == __y.__i_;}
  337. friend _LIBCPP_INLINE_VISIBILITY
  338. bool operator!=(const __hash_map_iterator& __x, const __hash_map_iterator& __y)
  339. {return __x.__i_ != __y.__i_;}
  340. template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS hash_map;
  341. template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS hash_multimap;
  342. template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
  343. template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
  344. template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
  345. };
  346. template <class _HashIterator>
  347. class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator
  348. {
  349. _HashIterator __i_;
  350. typedef const typename _HashIterator::value_type::first_type key_type;
  351. typedef typename _HashIterator::value_type::second_type mapped_type;
  352. public:
  353. typedef std::forward_iterator_tag iterator_category;
  354. typedef std::pair<key_type, mapped_type> value_type;
  355. typedef typename _HashIterator::difference_type difference_type;
  356. typedef const value_type& reference;
  357. typedef typename std::__rebind_pointer<typename _HashIterator::pointer, const value_type>::type
  358. pointer;
  359. _LIBCPP_INLINE_VISIBILITY __hash_map_const_iterator() {}
  360. _LIBCPP_INLINE_VISIBILITY
  361. __hash_map_const_iterator(_HashIterator __i) : __i_(__i) {}
  362. _LIBCPP_INLINE_VISIBILITY
  363. __hash_map_const_iterator(
  364. __hash_map_iterator<typename _HashIterator::__non_const_iterator> __i)
  365. : __i_(__i.__i_) {}
  366. _LIBCPP_INLINE_VISIBILITY
  367. reference operator*() const {return *operator->();}
  368. _LIBCPP_INLINE_VISIBILITY
  369. pointer operator->() const {return (pointer)__i_.operator->();}
  370. _LIBCPP_INLINE_VISIBILITY
  371. __hash_map_const_iterator& operator++() {++__i_; return *this;}
  372. _LIBCPP_INLINE_VISIBILITY
  373. __hash_map_const_iterator operator++(int)
  374. {
  375. __hash_map_const_iterator __t(*this);
  376. ++(*this);
  377. return __t;
  378. }
  379. friend _LIBCPP_INLINE_VISIBILITY
  380. bool operator==(const __hash_map_const_iterator& __x, const __hash_map_const_iterator& __y)
  381. {return __x.__i_ == __y.__i_;}
  382. friend _LIBCPP_INLINE_VISIBILITY
  383. bool operator!=(const __hash_map_const_iterator& __x, const __hash_map_const_iterator& __y)
  384. {return __x.__i_ != __y.__i_;}
  385. template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS hash_map;
  386. template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS hash_multimap;
  387. template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
  388. template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
  389. };
  390. template <class _Key, class _Tp, class _Hash = hash<_Key>, class _Pred = std::equal_to<_Key>,
  391. class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
  392. class _LIBCPP_TEMPLATE_VIS hash_map
  393. {
  394. public:
  395. // types
  396. typedef _Key key_type;
  397. typedef _Tp mapped_type;
  398. typedef _Tp data_type;
  399. typedef _Hash hasher;
  400. typedef _Pred key_equal;
  401. typedef _Alloc allocator_type;
  402. typedef std::pair<const key_type, mapped_type> value_type;
  403. typedef value_type& reference;
  404. typedef const value_type& const_reference;
  405. private:
  406. typedef std::pair<key_type, mapped_type> __value_type;
  407. typedef __hash_map_hasher<__value_type, hasher> __hasher;
  408. typedef __hash_map_equal<__value_type, key_equal> __key_equal;
  409. typedef typename std::__rebind_alloc_helper<
  410. std::allocator_traits<allocator_type>, __value_type>::type __allocator_type;
  411. typedef std::__hash_table<__value_type, __hasher,
  412. __key_equal, __allocator_type> __table;
  413. __table __table_;
  414. typedef typename __table::__node_pointer __node_pointer;
  415. typedef typename __table::__node_const_pointer __node_const_pointer;
  416. typedef typename __table::__node_traits __node_traits;
  417. typedef typename __table::__node_allocator __node_allocator;
  418. typedef typename __table::__node __node;
  419. typedef __hash_map_node_destructor<__node_allocator> _Dp;
  420. typedef std::unique_ptr<__node, _Dp> __node_holder;
  421. typedef std::allocator_traits<allocator_type> __alloc_traits;
  422. public:
  423. typedef typename __alloc_traits::pointer pointer;
  424. typedef typename __alloc_traits::const_pointer const_pointer;
  425. typedef typename __alloc_traits::size_type size_type;
  426. typedef typename __alloc_traits::difference_type difference_type;
  427. typedef __hash_map_iterator<typename __table::iterator> iterator;
  428. typedef __hash_map_const_iterator<typename __table::const_iterator> const_iterator;
  429. _LIBCPP_INLINE_VISIBILITY hash_map() { }
  430. explicit hash_map(size_type __n, const hasher& __hf = hasher(),
  431. const key_equal& __eql = key_equal());
  432. hash_map(size_type __n, const hasher& __hf,
  433. const key_equal& __eql,
  434. const allocator_type& __a);
  435. template <class _InputIterator>
  436. hash_map(_InputIterator __first, _InputIterator __last);
  437. template <class _InputIterator>
  438. hash_map(_InputIterator __first, _InputIterator __last,
  439. size_type __n, const hasher& __hf = hasher(),
  440. const key_equal& __eql = key_equal());
  441. template <class _InputIterator>
  442. hash_map(_InputIterator __first, _InputIterator __last,
  443. size_type __n, const hasher& __hf,
  444. const key_equal& __eql,
  445. const allocator_type& __a);
  446. hash_map(const hash_map& __u);
  447. _LIBCPP_INLINE_VISIBILITY
  448. allocator_type get_allocator() const
  449. {return allocator_type(__table_.__node_alloc());}
  450. _LIBCPP_INLINE_VISIBILITY
  451. bool empty() const {return __table_.size() == 0;}
  452. _LIBCPP_INLINE_VISIBILITY
  453. size_type size() const {return __table_.size();}
  454. _LIBCPP_INLINE_VISIBILITY
  455. size_type max_size() const {return __table_.max_size();}
  456. _LIBCPP_INLINE_VISIBILITY
  457. iterator begin() {return __table_.begin();}
  458. _LIBCPP_INLINE_VISIBILITY
  459. iterator end() {return __table_.end();}
  460. _LIBCPP_INLINE_VISIBILITY
  461. const_iterator begin() const {return __table_.begin();}
  462. _LIBCPP_INLINE_VISIBILITY
  463. const_iterator end() const {return __table_.end();}
  464. _LIBCPP_INLINE_VISIBILITY
  465. std::pair<iterator, bool> insert(const value_type& __x)
  466. {return __table_.__insert_unique(__x);}
  467. _LIBCPP_INLINE_VISIBILITY
  468. iterator insert(const_iterator, const value_type& __x) {return insert(__x).first;}
  469. template <class _InputIterator>
  470. _LIBCPP_INLINE_VISIBILITY
  471. void insert(_InputIterator __first, _InputIterator __last);
  472. _LIBCPP_INLINE_VISIBILITY
  473. void erase(const_iterator __p) {__table_.erase(__p.__i_);}
  474. _LIBCPP_INLINE_VISIBILITY
  475. size_type erase(const key_type& __k) {return __table_.__erase_unique(__k);}
  476. _LIBCPP_INLINE_VISIBILITY
  477. void erase(const_iterator __first, const_iterator __last)
  478. {__table_.erase(__first.__i_, __last.__i_);}
  479. _LIBCPP_INLINE_VISIBILITY
  480. void clear() {__table_.clear();}
  481. _LIBCPP_INLINE_VISIBILITY
  482. void swap(hash_map& __u) {__table_.swap(__u.__table_);}
  483. _LIBCPP_INLINE_VISIBILITY
  484. hasher hash_funct() const
  485. {return __table_.hash_function().hash_function();}
  486. _LIBCPP_INLINE_VISIBILITY
  487. key_equal key_eq() const
  488. {return __table_.key_eq().key_eq();}
  489. _LIBCPP_INLINE_VISIBILITY
  490. iterator find(const key_type& __k) {return __table_.find(__k);}
  491. _LIBCPP_INLINE_VISIBILITY
  492. const_iterator find(const key_type& __k) const {return __table_.find(__k);}
  493. _LIBCPP_INLINE_VISIBILITY
  494. size_type count(const key_type& __k) const {return __table_.__count_unique(__k);}
  495. _LIBCPP_INLINE_VISIBILITY
  496. std::pair<iterator, iterator> equal_range(const key_type& __k)
  497. {return __table_.__equal_range_unique(__k);}
  498. _LIBCPP_INLINE_VISIBILITY
  499. std::pair<const_iterator, const_iterator> equal_range(const key_type& __k) const
  500. {return __table_.__equal_range_unique(__k);}
  501. mapped_type& operator[](const key_type& __k);
  502. _LIBCPP_INLINE_VISIBILITY
  503. size_type bucket_count() const {return __table_.bucket_count();}
  504. _LIBCPP_INLINE_VISIBILITY
  505. size_type max_bucket_count() const {return __table_.max_bucket_count();}
  506. _LIBCPP_INLINE_VISIBILITY
  507. size_type elems_in_bucket(size_type __n) const
  508. {return __table_.bucket_size(__n);}
  509. _LIBCPP_INLINE_VISIBILITY
  510. void resize(size_type __n) {__table_.rehash(__n);}
  511. private:
  512. __node_holder __construct_node(const key_type& __k);
  513. };
  514. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  515. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
  516. size_type __n, const hasher& __hf, const key_equal& __eql)
  517. : __table_(__hf, __eql)
  518. {
  519. __table_.rehash(__n);
  520. }
  521. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  522. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
  523. size_type __n, const hasher& __hf, const key_equal& __eql,
  524. const allocator_type& __a)
  525. : __table_(__hf, __eql, __a)
  526. {
  527. __table_.rehash(__n);
  528. }
  529. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  530. template <class _InputIterator>
  531. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
  532. _InputIterator __first, _InputIterator __last)
  533. {
  534. insert(__first, __last);
  535. }
  536. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  537. template <class _InputIterator>
  538. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
  539. _InputIterator __first, _InputIterator __last, size_type __n,
  540. const hasher& __hf, const key_equal& __eql)
  541. : __table_(__hf, __eql)
  542. {
  543. __table_.rehash(__n);
  544. insert(__first, __last);
  545. }
  546. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  547. template <class _InputIterator>
  548. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
  549. _InputIterator __first, _InputIterator __last, size_type __n,
  550. const hasher& __hf, const key_equal& __eql, const allocator_type& __a)
  551. : __table_(__hf, __eql, __a)
  552. {
  553. __table_.rehash(__n);
  554. insert(__first, __last);
  555. }
  556. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  557. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
  558. const hash_map& __u)
  559. : __table_(__u.__table_)
  560. {
  561. __table_.rehash(__u.bucket_count());
  562. insert(__u.begin(), __u.end());
  563. }
  564. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  565. typename hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::__node_holder
  566. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::__construct_node(const key_type& __k)
  567. {
  568. __node_allocator& __na = __table_.__node_alloc();
  569. __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
  570. __node_traits::construct(__na, _VSTD::addressof(__h->__value_.first), __k);
  571. __h.get_deleter().__first_constructed = true;
  572. __node_traits::construct(__na, _VSTD::addressof(__h->__value_.second));
  573. __h.get_deleter().__second_constructed = true;
  574. return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
  575. }
  576. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  577. template <class _InputIterator>
  578. inline
  579. void
  580. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::insert(_InputIterator __first,
  581. _InputIterator __last)
  582. {
  583. for (; __first != __last; ++__first)
  584. __table_.__insert_unique(*__first);
  585. }
  586. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  587. _Tp&
  588. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::operator[](const key_type& __k)
  589. {
  590. iterator __i = find(__k);
  591. if (__i != end())
  592. return __i->second;
  593. __node_holder __h = __construct_node(__k);
  594. std::pair<iterator, bool> __r = __table_.__node_insert_unique(__h.get());
  595. __h.release();
  596. return __r.first->second;
  597. }
  598. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  599. inline _LIBCPP_INLINE_VISIBILITY
  600. void
  601. swap(hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  602. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  603. {
  604. __x.swap(__y);
  605. }
  606. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  607. bool
  608. operator==(const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  609. const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  610. {
  611. if (__x.size() != __y.size())
  612. return false;
  613. typedef typename hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::const_iterator
  614. const_iterator;
  615. for (const_iterator __i = __x.begin(), __ex = __x.end(), __ey = __y.end();
  616. __i != __ex; ++__i)
  617. {
  618. const_iterator __j = __y.find(__i->first);
  619. if (__j == __ey || !(*__i == *__j))
  620. return false;
  621. }
  622. return true;
  623. }
  624. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  625. inline _LIBCPP_INLINE_VISIBILITY
  626. bool
  627. operator!=(const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  628. const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  629. {
  630. return !(__x == __y);
  631. }
  632. template <class _Key, class _Tp, class _Hash = hash<_Key>, class _Pred = std::equal_to<_Key>,
  633. class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
  634. class _LIBCPP_TEMPLATE_VIS hash_multimap
  635. {
  636. public:
  637. // types
  638. typedef _Key key_type;
  639. typedef _Tp mapped_type;
  640. typedef _Tp data_type;
  641. typedef _Hash hasher;
  642. typedef _Pred key_equal;
  643. typedef _Alloc allocator_type;
  644. typedef std::pair<const key_type, mapped_type> value_type;
  645. typedef value_type& reference;
  646. typedef const value_type& const_reference;
  647. private:
  648. typedef std::pair<key_type, mapped_type> __value_type;
  649. typedef __hash_map_hasher<__value_type, hasher> __hasher;
  650. typedef __hash_map_equal<__value_type, key_equal> __key_equal;
  651. typedef typename std::__rebind_alloc_helper<std::allocator_traits<allocator_type>, __value_type>::type __allocator_type;
  652. typedef std::__hash_table<__value_type, __hasher,
  653. __key_equal, __allocator_type> __table;
  654. __table __table_;
  655. typedef typename __table::__node_traits __node_traits;
  656. typedef typename __table::__node_allocator __node_allocator;
  657. typedef typename __table::__node __node;
  658. typedef __hash_map_node_destructor<__node_allocator> _Dp;
  659. typedef std::unique_ptr<__node, _Dp> __node_holder;
  660. typedef std::allocator_traits<allocator_type> __alloc_traits;
  661. public:
  662. typedef typename __alloc_traits::pointer pointer;
  663. typedef typename __alloc_traits::const_pointer const_pointer;
  664. typedef typename __alloc_traits::size_type size_type;
  665. typedef typename __alloc_traits::difference_type difference_type;
  666. typedef __hash_map_iterator<typename __table::iterator> iterator;
  667. typedef __hash_map_const_iterator<typename __table::const_iterator> const_iterator;
  668. _LIBCPP_INLINE_VISIBILITY
  669. hash_multimap() { }
  670. explicit hash_multimap(size_type __n, const hasher& __hf = hasher(),
  671. const key_equal& __eql = key_equal());
  672. hash_multimap(size_type __n, const hasher& __hf,
  673. const key_equal& __eql,
  674. const allocator_type& __a);
  675. template <class _InputIterator>
  676. hash_multimap(_InputIterator __first, _InputIterator __last);
  677. template <class _InputIterator>
  678. hash_multimap(_InputIterator __first, _InputIterator __last,
  679. size_type __n, const hasher& __hf = hasher(),
  680. const key_equal& __eql = key_equal());
  681. template <class _InputIterator>
  682. hash_multimap(_InputIterator __first, _InputIterator __last,
  683. size_type __n, const hasher& __hf,
  684. const key_equal& __eql,
  685. const allocator_type& __a);
  686. hash_multimap(const hash_multimap& __u);
  687. _LIBCPP_INLINE_VISIBILITY
  688. allocator_type get_allocator() const
  689. {return allocator_type(__table_.__node_alloc());}
  690. _LIBCPP_INLINE_VISIBILITY
  691. bool empty() const {return __table_.size() == 0;}
  692. _LIBCPP_INLINE_VISIBILITY
  693. size_type size() const {return __table_.size();}
  694. _LIBCPP_INLINE_VISIBILITY
  695. size_type max_size() const {return __table_.max_size();}
  696. _LIBCPP_INLINE_VISIBILITY
  697. iterator begin() {return __table_.begin();}
  698. _LIBCPP_INLINE_VISIBILITY
  699. iterator end() {return __table_.end();}
  700. _LIBCPP_INLINE_VISIBILITY
  701. const_iterator begin() const {return __table_.begin();}
  702. _LIBCPP_INLINE_VISIBILITY
  703. const_iterator end() const {return __table_.end();}
  704. _LIBCPP_INLINE_VISIBILITY
  705. iterator insert(const value_type& __x) {return __table_.__insert_multi(__x);}
  706. _LIBCPP_INLINE_VISIBILITY
  707. iterator insert(const_iterator, const value_type& __x) {return insert(__x);}
  708. template <class _InputIterator>
  709. _LIBCPP_INLINE_VISIBILITY
  710. void insert(_InputIterator __first, _InputIterator __last);
  711. _LIBCPP_INLINE_VISIBILITY
  712. void erase(const_iterator __p) {__table_.erase(__p.__i_);}
  713. _LIBCPP_INLINE_VISIBILITY
  714. size_type erase(const key_type& __k) {return __table_.__erase_multi(__k);}
  715. _LIBCPP_INLINE_VISIBILITY
  716. void erase(const_iterator __first, const_iterator __last)
  717. {__table_.erase(__first.__i_, __last.__i_);}
  718. _LIBCPP_INLINE_VISIBILITY
  719. void clear() {__table_.clear();}
  720. _LIBCPP_INLINE_VISIBILITY
  721. void swap(hash_multimap& __u) {__table_.swap(__u.__table_);}
  722. _LIBCPP_INLINE_VISIBILITY
  723. hasher hash_funct() const
  724. {return __table_.hash_function().hash_function();}
  725. _LIBCPP_INLINE_VISIBILITY
  726. key_equal key_eq() const
  727. {return __table_.key_eq().key_eq();}
  728. _LIBCPP_INLINE_VISIBILITY
  729. iterator find(const key_type& __k) {return __table_.find(__k);}
  730. _LIBCPP_INLINE_VISIBILITY
  731. const_iterator find(const key_type& __k) const {return __table_.find(__k);}
  732. _LIBCPP_INLINE_VISIBILITY
  733. size_type count(const key_type& __k) const {return __table_.__count_multi(__k);}
  734. _LIBCPP_INLINE_VISIBILITY
  735. std::pair<iterator, iterator> equal_range(const key_type& __k)
  736. {return __table_.__equal_range_multi(__k);}
  737. _LIBCPP_INLINE_VISIBILITY
  738. std::pair<const_iterator, const_iterator> equal_range(const key_type& __k) const
  739. {return __table_.__equal_range_multi(__k);}
  740. _LIBCPP_INLINE_VISIBILITY
  741. size_type bucket_count() const {return __table_.bucket_count();}
  742. _LIBCPP_INLINE_VISIBILITY
  743. size_type max_bucket_count() const {return __table_.max_bucket_count();}
  744. _LIBCPP_INLINE_VISIBILITY
  745. size_type elems_in_bucket(size_type __n) const
  746. {return __table_.bucket_size(__n);}
  747. _LIBCPP_INLINE_VISIBILITY
  748. void resize(size_type __n) {__table_.rehash(__n);}
  749. };
  750. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  751. hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
  752. size_type __n, const hasher& __hf, const key_equal& __eql)
  753. : __table_(__hf, __eql)
  754. {
  755. __table_.rehash(__n);
  756. }
  757. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  758. hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
  759. size_type __n, const hasher& __hf, const key_equal& __eql,
  760. const allocator_type& __a)
  761. : __table_(__hf, __eql, __a)
  762. {
  763. __table_.rehash(__n);
  764. }
  765. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  766. template <class _InputIterator>
  767. hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
  768. _InputIterator __first, _InputIterator __last)
  769. {
  770. insert(__first, __last);
  771. }
  772. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  773. template <class _InputIterator>
  774. hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
  775. _InputIterator __first, _InputIterator __last, size_type __n,
  776. const hasher& __hf, const key_equal& __eql)
  777. : __table_(__hf, __eql)
  778. {
  779. __table_.rehash(__n);
  780. insert(__first, __last);
  781. }
  782. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  783. template <class _InputIterator>
  784. hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
  785. _InputIterator __first, _InputIterator __last, size_type __n,
  786. const hasher& __hf, const key_equal& __eql, const allocator_type& __a)
  787. : __table_(__hf, __eql, __a)
  788. {
  789. __table_.rehash(__n);
  790. insert(__first, __last);
  791. }
  792. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  793. hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
  794. const hash_multimap& __u)
  795. : __table_(__u.__table_)
  796. {
  797. __table_.rehash(__u.bucket_count());
  798. insert(__u.begin(), __u.end());
  799. }
  800. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  801. template <class _InputIterator>
  802. inline
  803. void
  804. hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::insert(_InputIterator __first,
  805. _InputIterator __last)
  806. {
  807. for (; __first != __last; ++__first)
  808. __table_.__insert_multi(*__first);
  809. }
  810. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  811. inline _LIBCPP_INLINE_VISIBILITY
  812. void
  813. swap(hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  814. hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  815. {
  816. __x.swap(__y);
  817. }
  818. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  819. bool
  820. operator==(const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  821. const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  822. {
  823. if (__x.size() != __y.size())
  824. return false;
  825. typedef typename hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::const_iterator
  826. const_iterator;
  827. typedef std::pair<const_iterator, const_iterator> _EqRng;
  828. for (const_iterator __i = __x.begin(), __ex = __x.end(); __i != __ex;)
  829. {
  830. _EqRng __xeq = __x.equal_range(__i->first);
  831. _EqRng __yeq = __y.equal_range(__i->first);
  832. if (_VSTD::distance(__xeq.first, __xeq.second) !=
  833. _VSTD::distance(__yeq.first, __yeq.second) ||
  834. !_VSTD::is_permutation(__xeq.first, __xeq.second, __yeq.first))
  835. return false;
  836. __i = __xeq.second;
  837. }
  838. return true;
  839. }
  840. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  841. inline _LIBCPP_INLINE_VISIBILITY
  842. bool
  843. operator!=(const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  844. const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  845. {
  846. return !(__x == __y);
  847. }
  848. } // __gnu_cxx
  849. #endif // _LIBCPP_HASH_MAP