unordered_map.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // TR1 unordered_map implementation -*- C++ -*-
  2. // Copyright (C) 2010-2018 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file tr1/unordered_map.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{tr1/unordered_map}
  23. */
  24. namespace std _GLIBCXX_VISIBILITY(default)
  25. {
  26. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  27. namespace tr1
  28. {
  29. // NB: When we get typedef templates these class definitions
  30. // will be unnecessary.
  31. template<class _Key, class _Tp,
  32. class _Hash = hash<_Key>,
  33. class _Pred = std::equal_to<_Key>,
  34. class _Alloc = std::allocator<std::pair<const _Key, _Tp> >,
  35. bool __cache_hash_code = false>
  36. class __unordered_map
  37. : public _Hashtable<_Key, std::pair<const _Key, _Tp>, _Alloc,
  38. std::_Select1st<std::pair<const _Key, _Tp> >, _Pred,
  39. _Hash, __detail::_Mod_range_hashing,
  40. __detail::_Default_ranged_hash,
  41. __detail::_Prime_rehash_policy,
  42. __cache_hash_code, false, true>
  43. {
  44. typedef _Hashtable<_Key, std::pair<const _Key, _Tp>, _Alloc,
  45. std::_Select1st<std::pair<const _Key, _Tp> >, _Pred,
  46. _Hash, __detail::_Mod_range_hashing,
  47. __detail::_Default_ranged_hash,
  48. __detail::_Prime_rehash_policy,
  49. __cache_hash_code, false, true>
  50. _Base;
  51. public:
  52. typedef typename _Base::size_type size_type;
  53. typedef typename _Base::hasher hasher;
  54. typedef typename _Base::key_equal key_equal;
  55. typedef typename _Base::allocator_type allocator_type;
  56. explicit
  57. __unordered_map(size_type __n = 10,
  58. const hasher& __hf = hasher(),
  59. const key_equal& __eql = key_equal(),
  60. const allocator_type& __a = allocator_type())
  61. : _Base(__n, __hf, __detail::_Mod_range_hashing(),
  62. __detail::_Default_ranged_hash(),
  63. __eql, std::_Select1st<std::pair<const _Key, _Tp> >(), __a)
  64. { }
  65. template<typename _InputIterator>
  66. __unordered_map(_InputIterator __f, _InputIterator __l,
  67. size_type __n = 10,
  68. const hasher& __hf = hasher(),
  69. const key_equal& __eql = key_equal(),
  70. const allocator_type& __a = allocator_type())
  71. : _Base(__f, __l, __n, __hf, __detail::_Mod_range_hashing(),
  72. __detail::_Default_ranged_hash(),
  73. __eql, std::_Select1st<std::pair<const _Key, _Tp> >(), __a)
  74. { }
  75. };
  76. template<class _Key, class _Tp,
  77. class _Hash = hash<_Key>,
  78. class _Pred = std::equal_to<_Key>,
  79. class _Alloc = std::allocator<std::pair<const _Key, _Tp> >,
  80. bool __cache_hash_code = false>
  81. class __unordered_multimap
  82. : public _Hashtable<_Key, std::pair<const _Key, _Tp>,
  83. _Alloc,
  84. std::_Select1st<std::pair<const _Key, _Tp> >, _Pred,
  85. _Hash, __detail::_Mod_range_hashing,
  86. __detail::_Default_ranged_hash,
  87. __detail::_Prime_rehash_policy,
  88. __cache_hash_code, false, false>
  89. {
  90. typedef _Hashtable<_Key, std::pair<const _Key, _Tp>,
  91. _Alloc,
  92. std::_Select1st<std::pair<const _Key, _Tp> >, _Pred,
  93. _Hash, __detail::_Mod_range_hashing,
  94. __detail::_Default_ranged_hash,
  95. __detail::_Prime_rehash_policy,
  96. __cache_hash_code, false, false>
  97. _Base;
  98. public:
  99. typedef typename _Base::size_type size_type;
  100. typedef typename _Base::hasher hasher;
  101. typedef typename _Base::key_equal key_equal;
  102. typedef typename _Base::allocator_type allocator_type;
  103. explicit
  104. __unordered_multimap(size_type __n = 10,
  105. const hasher& __hf = hasher(),
  106. const key_equal& __eql = key_equal(),
  107. const allocator_type& __a = allocator_type())
  108. : _Base(__n, __hf, __detail::_Mod_range_hashing(),
  109. __detail::_Default_ranged_hash(),
  110. __eql, std::_Select1st<std::pair<const _Key, _Tp> >(), __a)
  111. { }
  112. template<typename _InputIterator>
  113. __unordered_multimap(_InputIterator __f, _InputIterator __l,
  114. typename _Base::size_type __n = 0,
  115. const hasher& __hf = hasher(),
  116. const key_equal& __eql = key_equal(),
  117. const allocator_type& __a = allocator_type())
  118. : _Base(__f, __l, __n, __hf, __detail::_Mod_range_hashing(),
  119. __detail::_Default_ranged_hash(),
  120. __eql, std::_Select1st<std::pair<const _Key, _Tp> >(), __a)
  121. { }
  122. };
  123. template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc,
  124. bool __cache_hash_code>
  125. inline void
  126. swap(__unordered_map<_Key, _Tp, _Hash, _Pred,
  127. _Alloc, __cache_hash_code>& __x,
  128. __unordered_map<_Key, _Tp, _Hash, _Pred,
  129. _Alloc, __cache_hash_code>& __y)
  130. { __x.swap(__y); }
  131. template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc,
  132. bool __cache_hash_code>
  133. inline void
  134. swap(__unordered_multimap<_Key, _Tp, _Hash, _Pred,
  135. _Alloc, __cache_hash_code>& __x,
  136. __unordered_multimap<_Key, _Tp, _Hash, _Pred,
  137. _Alloc, __cache_hash_code>& __y)
  138. { __x.swap(__y); }
  139. /**
  140. * @brief A standard container composed of unique keys (containing
  141. * at most one of each key value) that associates values of another type
  142. * with the keys.
  143. *
  144. * @ingroup unordered_associative_containers
  145. *
  146. * Meets the requirements of a <a href="tables.html#65">container</a>, and
  147. * <a href="tables.html#xx">unordered associative container</a>
  148. *
  149. * @param Key Type of key objects.
  150. * @param Tp Type of mapped objects.
  151. * @param Hash Hashing function object type, defaults to hash<Value>.
  152. * @param Pred Predicate function object type, defaults to equal_to<Value>.
  153. * @param Alloc Allocator type, defaults to allocator<Key>.
  154. *
  155. * The resulting value type of the container is std::pair<const Key, Tp>.
  156. */
  157. template<class _Key, class _Tp,
  158. class _Hash = hash<_Key>,
  159. class _Pred = std::equal_to<_Key>,
  160. class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
  161. class unordered_map
  162. : public __unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>
  163. {
  164. typedef __unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc> _Base;
  165. public:
  166. typedef typename _Base::value_type value_type;
  167. typedef typename _Base::size_type size_type;
  168. typedef typename _Base::hasher hasher;
  169. typedef typename _Base::key_equal key_equal;
  170. typedef typename _Base::allocator_type allocator_type;
  171. explicit
  172. unordered_map(size_type __n = 10,
  173. const hasher& __hf = hasher(),
  174. const key_equal& __eql = key_equal(),
  175. const allocator_type& __a = allocator_type())
  176. : _Base(__n, __hf, __eql, __a)
  177. { }
  178. template<typename _InputIterator>
  179. unordered_map(_InputIterator __f, _InputIterator __l,
  180. size_type __n = 10,
  181. const hasher& __hf = hasher(),
  182. const key_equal& __eql = key_equal(),
  183. const allocator_type& __a = allocator_type())
  184. : _Base(__f, __l, __n, __hf, __eql, __a)
  185. { }
  186. };
  187. /**
  188. * @brief A standard container composed of equivalent keys
  189. * (possibly containing multiple of each key value) that associates
  190. * values of another type with the keys.
  191. *
  192. * @ingroup unordered_associative_containers
  193. *
  194. * Meets the requirements of a <a href="tables.html#65">container</a>, and
  195. * <a href="tables.html#xx">unordered associative container</a>
  196. *
  197. * @param Key Type of key objects.
  198. * @param Tp Type of mapped objects.
  199. * @param Hash Hashing function object type, defaults to hash<Value>.
  200. * @param Pred Predicate function object type, defaults to equal_to<Value>.
  201. * @param Alloc Allocator type, defaults to allocator<Key>.
  202. *
  203. * The resulting value type of the container is std::pair<const Key, Tp>.
  204. */
  205. template<class _Key, class _Tp,
  206. class _Hash = hash<_Key>,
  207. class _Pred = std::equal_to<_Key>,
  208. class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
  209. class unordered_multimap
  210. : public __unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>
  211. {
  212. typedef __unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc> _Base;
  213. public:
  214. typedef typename _Base::value_type value_type;
  215. typedef typename _Base::size_type size_type;
  216. typedef typename _Base::hasher hasher;
  217. typedef typename _Base::key_equal key_equal;
  218. typedef typename _Base::allocator_type allocator_type;
  219. explicit
  220. unordered_multimap(size_type __n = 10,
  221. const hasher& __hf = hasher(),
  222. const key_equal& __eql = key_equal(),
  223. const allocator_type& __a = allocator_type())
  224. : _Base(__n, __hf, __eql, __a)
  225. { }
  226. template<typename _InputIterator>
  227. unordered_multimap(_InputIterator __f, _InputIterator __l,
  228. typename _Base::size_type __n = 0,
  229. const hasher& __hf = hasher(),
  230. const key_equal& __eql = key_equal(),
  231. const allocator_type& __a = allocator_type())
  232. : _Base(__f, __l, __n, __hf, __eql, __a)
  233. { }
  234. };
  235. template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  236. inline void
  237. swap(unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  238. unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  239. { __x.swap(__y); }
  240. template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  241. inline void
  242. swap(unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  243. unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  244. { __x.swap(__y); }
  245. }
  246. _GLIBCXX_END_NAMESPACE_VERSION
  247. }