hash_map 38 KB

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