hash_map.hxx.in 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
  3. /*
  4. * Copyright (c) 1996
  5. * Silicon Graphics Computer Systems, Inc.
  6. *
  7. * Permission to use, copy, modify, distribute and sell this software
  8. * and its documentation for any purpose is hereby granted without fee,
  9. * provided that the above copyright notice appear in all copies and
  10. * that both that copyright notice and this permission notice appear
  11. * in supporting documentation. Silicon Graphics makes no
  12. * representations about the suitability of this software for any
  13. * purpose. It is provided "as is" without express or implied warranty.
  14. *
  15. *
  16. * Copyright (c) 1994
  17. * Hewlett-Packard Company
  18. *
  19. * Permission to use, copy, modify, distribute and sell this software
  20. * and its documentation for any purpose is hereby granted without fee,
  21. * provided that the above copyright notice appear in all copies and
  22. * that both that copyright notice and this permission notice appear
  23. * in supporting documentation. Hewlett-Packard Company makes no
  24. * representations about the suitability of this software for any
  25. * purpose. It is provided "as is" without express or implied warranty.
  26. *
  27. */
  28. #ifndef @KWSYS_NAMESPACE@_hash_map_hxx
  29. #define @KWSYS_NAMESPACE@_hash_map_hxx
  30. #include <@KWSYS_NAMESPACE@/hashtable.hxx>
  31. #include <@KWSYS_NAMESPACE@/hash_fun.hxx>
  32. #include <functional> // equal_to
  33. #if defined(_MSC_VER)
  34. #pragma warning(push)
  35. #pragma warning(disable : 4284)
  36. #pragma warning(disable : 4786)
  37. #endif
  38. #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
  39. #pragma set woff 1174
  40. #pragma set woff 1375
  41. #endif
  42. namespace @KWSYS_NAMESPACE@ {
  43. // select1st is an extension: it is not part of the standard.
  44. template <class T1, class T2>
  45. struct hash_select1st
  46. {
  47. const T1& operator()(const std::pair<T1, T2>& __x) const
  48. {
  49. return __x.first;
  50. }
  51. };
  52. // Forward declaration of equality operator; needed for friend declaration.
  53. template <class _Key, class _Tp, class _HashFcn = hash<_Key>,
  54. class _EqualKey = std::equal_to<_Key>,
  55. class _Alloc = std::allocator<char> >
  56. class hash_map;
  57. template <class _Key, class _Tp, class _HashFn, class _EqKey, class _Alloc>
  58. bool operator==(const hash_map<_Key, _Tp, _HashFn, _EqKey, _Alloc>&,
  59. const hash_map<_Key, _Tp, _HashFn, _EqKey, _Alloc>&);
  60. template <class _Key, class _Tp, class _HashFcn, class _EqualKey, class _Alloc>
  61. class hash_map
  62. {
  63. private:
  64. typedef hashtable<std::pair<const _Key, _Tp>, _Key, _HashFcn,
  65. hash_select1st<const _Key, _Tp>, _EqualKey, _Alloc>
  66. _Ht;
  67. _Ht _M_ht;
  68. public:
  69. typedef typename _Ht::key_type key_type;
  70. typedef _Tp data_type;
  71. typedef _Tp mapped_type;
  72. typedef typename _Ht::value_type value_type;
  73. typedef typename _Ht::hasher hasher;
  74. typedef typename _Ht::key_equal key_equal;
  75. typedef typename _Ht::size_type size_type;
  76. typedef typename _Ht::difference_type difference_type;
  77. typedef typename _Ht::pointer pointer;
  78. typedef typename _Ht::const_pointer const_pointer;
  79. typedef typename _Ht::reference reference;
  80. typedef typename _Ht::const_reference const_reference;
  81. typedef typename _Ht::iterator iterator;
  82. typedef typename _Ht::const_iterator const_iterator;
  83. typedef typename _Ht::allocator_type allocator_type;
  84. hasher hash_funct() const { return _M_ht.hash_funct(); }
  85. key_equal key_eq() const { return _M_ht.key_eq(); }
  86. allocator_type get_allocator() const { return _M_ht.get_allocator(); }
  87. public:
  88. hash_map()
  89. : _M_ht(100, hasher(), key_equal(), allocator_type())
  90. {
  91. }
  92. explicit hash_map(size_type __n)
  93. : _M_ht(__n, hasher(), key_equal(), allocator_type())
  94. {
  95. }
  96. hash_map(size_type __n, const hasher& __hf)
  97. : _M_ht(__n, __hf, key_equal(), allocator_type())
  98. {
  99. }
  100. hash_map(size_type __n, const hasher& __hf, const key_equal& __eql,
  101. const allocator_type& __a = allocator_type())
  102. : _M_ht(__n, __hf, __eql, __a)
  103. {
  104. }
  105. template <class _InputIterator>
  106. hash_map(_InputIterator __f, _InputIterator __l)
  107. : _M_ht(100, hasher(), key_equal(), allocator_type())
  108. {
  109. _M_ht.insert_unique(__f, __l);
  110. }
  111. template <class _InputIterator>
  112. hash_map(_InputIterator __f, _InputIterator __l, size_type __n)
  113. : _M_ht(__n, hasher(), key_equal(), allocator_type())
  114. {
  115. _M_ht.insert_unique(__f, __l);
  116. }
  117. template <class _InputIterator>
  118. hash_map(_InputIterator __f, _InputIterator __l, size_type __n,
  119. const hasher& __hf)
  120. : _M_ht(__n, __hf, key_equal(), allocator_type())
  121. {
  122. _M_ht.insert_unique(__f, __l);
  123. }
  124. template <class _InputIterator>
  125. hash_map(_InputIterator __f, _InputIterator __l, size_type __n,
  126. const hasher& __hf, const key_equal& __eql,
  127. const allocator_type& __a = allocator_type())
  128. : _M_ht(__n, __hf, __eql, __a)
  129. {
  130. _M_ht.insert_unique(__f, __l);
  131. }
  132. public:
  133. size_type size() const { return _M_ht.size(); }
  134. size_type max_size() const { return _M_ht.max_size(); }
  135. bool empty() const { return _M_ht.empty(); }
  136. void swap(hash_map& __hs) { _M_ht.swap(__hs._M_ht); }
  137. friend bool operator==<>(const hash_map&, const hash_map&);
  138. iterator begin() { return _M_ht.begin(); }
  139. iterator end() { return _M_ht.end(); }
  140. const_iterator begin() const { return _M_ht.begin(); }
  141. const_iterator end() const { return _M_ht.end(); }
  142. public:
  143. std::pair<iterator, bool> insert(const value_type& __obj)
  144. {
  145. return _M_ht.insert_unique(__obj);
  146. }
  147. template <class _InputIterator>
  148. void insert(_InputIterator __f, _InputIterator __l)
  149. {
  150. _M_ht.insert_unique(__f, __l);
  151. }
  152. std::pair<iterator, bool> insert_noresize(const value_type& __obj)
  153. {
  154. return _M_ht.insert_unique_noresize(__obj);
  155. }
  156. iterator find(const key_type& __key) { return _M_ht.find(__key); }
  157. const_iterator find(const key_type& __key) const
  158. {
  159. return _M_ht.find(__key);
  160. }
  161. _Tp& operator[](const key_type& __key)
  162. {
  163. return _M_ht.find_or_insert(value_type(__key, _Tp())).second;
  164. }
  165. size_type count(const key_type& __key) const { return _M_ht.count(__key); }
  166. std::pair<iterator, iterator> equal_range(const key_type& __key)
  167. {
  168. return _M_ht.equal_range(__key);
  169. }
  170. std::pair<const_iterator, const_iterator> equal_range(
  171. const key_type& __key) const
  172. {
  173. return _M_ht.equal_range(__key);
  174. }
  175. size_type erase(const key_type& __key) { return _M_ht.erase(__key); }
  176. void erase(iterator __it) { _M_ht.erase(__it); }
  177. void erase(iterator __f, iterator __l) { _M_ht.erase(__f, __l); }
  178. void clear() { _M_ht.clear(); }
  179. void resize(size_type __hint) { _M_ht.resize(__hint); }
  180. size_type bucket_count() const { return _M_ht.bucket_count(); }
  181. size_type max_bucket_count() const { return _M_ht.max_bucket_count(); }
  182. size_type elems_in_bucket(size_type __n) const
  183. {
  184. return _M_ht.elems_in_bucket(__n);
  185. }
  186. };
  187. template <class _Key, class _Tp, class _HashFcn, class _EqlKey, class _Alloc>
  188. bool operator==(const hash_map<_Key, _Tp, _HashFcn, _EqlKey, _Alloc>& __hm1,
  189. const hash_map<_Key, _Tp, _HashFcn, _EqlKey, _Alloc>& __hm2)
  190. {
  191. return __hm1._M_ht == __hm2._M_ht;
  192. }
  193. template <class _Key, class _Tp, class _HashFcn, class _EqlKey, class _Alloc>
  194. inline bool operator!=(
  195. const hash_map<_Key, _Tp, _HashFcn, _EqlKey, _Alloc>& __hm1,
  196. const hash_map<_Key, _Tp, _HashFcn, _EqlKey, _Alloc>& __hm2)
  197. {
  198. return !(__hm1 == __hm2);
  199. }
  200. template <class _Key, class _Tp, class _HashFcn, class _EqlKey, class _Alloc>
  201. inline void swap(hash_map<_Key, _Tp, _HashFcn, _EqlKey, _Alloc>& __hm1,
  202. hash_map<_Key, _Tp, _HashFcn, _EqlKey, _Alloc>& __hm2)
  203. {
  204. __hm1.swap(__hm2);
  205. }
  206. // Forward declaration of equality operator; needed for friend declaration.
  207. template <class _Key, class _Tp, class _HashFcn = hash<_Key>,
  208. class _EqualKey = std::equal_to<_Key>,
  209. class _Alloc = std::allocator<char> >
  210. class hash_multimap;
  211. template <class _Key, class _Tp, class _HF, class _EqKey, class _Alloc>
  212. bool operator==(const hash_multimap<_Key, _Tp, _HF, _EqKey, _Alloc>& __hm1,
  213. const hash_multimap<_Key, _Tp, _HF, _EqKey, _Alloc>& __hm2);
  214. template <class _Key, class _Tp, class _HashFcn, class _EqualKey, class _Alloc>
  215. class hash_multimap
  216. {
  217. private:
  218. typedef hashtable<std::pair<const _Key, _Tp>, _Key, _HashFcn,
  219. hash_select1st<const _Key, _Tp>, _EqualKey, _Alloc>
  220. _Ht;
  221. _Ht _M_ht;
  222. public:
  223. typedef typename _Ht::key_type key_type;
  224. typedef _Tp data_type;
  225. typedef _Tp mapped_type;
  226. typedef typename _Ht::value_type value_type;
  227. typedef typename _Ht::hasher hasher;
  228. typedef typename _Ht::key_equal key_equal;
  229. typedef typename _Ht::size_type size_type;
  230. typedef typename _Ht::difference_type difference_type;
  231. typedef typename _Ht::pointer pointer;
  232. typedef typename _Ht::const_pointer const_pointer;
  233. typedef typename _Ht::reference reference;
  234. typedef typename _Ht::const_reference const_reference;
  235. typedef typename _Ht::iterator iterator;
  236. typedef typename _Ht::const_iterator const_iterator;
  237. typedef typename _Ht::allocator_type allocator_type;
  238. hasher hash_funct() const { return _M_ht.hash_funct(); }
  239. key_equal key_eq() const { return _M_ht.key_eq(); }
  240. allocator_type get_allocator() const { return _M_ht.get_allocator(); }
  241. public:
  242. hash_multimap()
  243. : _M_ht(100, hasher(), key_equal(), allocator_type())
  244. {
  245. }
  246. explicit hash_multimap(size_type __n)
  247. : _M_ht(__n, hasher(), key_equal(), allocator_type())
  248. {
  249. }
  250. hash_multimap(size_type __n, const hasher& __hf)
  251. : _M_ht(__n, __hf, key_equal(), allocator_type())
  252. {
  253. }
  254. hash_multimap(size_type __n, const hasher& __hf, const key_equal& __eql,
  255. const allocator_type& __a = allocator_type())
  256. : _M_ht(__n, __hf, __eql, __a)
  257. {
  258. }
  259. template <class _InputIterator>
  260. hash_multimap(_InputIterator __f, _InputIterator __l)
  261. : _M_ht(100, hasher(), key_equal(), allocator_type())
  262. {
  263. _M_ht.insert_equal(__f, __l);
  264. }
  265. template <class _InputIterator>
  266. hash_multimap(_InputIterator __f, _InputIterator __l, size_type __n)
  267. : _M_ht(__n, hasher(), key_equal(), allocator_type())
  268. {
  269. _M_ht.insert_equal(__f, __l);
  270. }
  271. template <class _InputIterator>
  272. hash_multimap(_InputIterator __f, _InputIterator __l, size_type __n,
  273. const hasher& __hf)
  274. : _M_ht(__n, __hf, key_equal(), allocator_type())
  275. {
  276. _M_ht.insert_equal(__f, __l);
  277. }
  278. template <class _InputIterator>
  279. hash_multimap(_InputIterator __f, _InputIterator __l, size_type __n,
  280. const hasher& __hf, const key_equal& __eql,
  281. const allocator_type& __a = allocator_type())
  282. : _M_ht(__n, __hf, __eql, __a)
  283. {
  284. _M_ht.insert_equal(__f, __l);
  285. }
  286. public:
  287. size_type size() const { return _M_ht.size(); }
  288. size_type max_size() const { return _M_ht.max_size(); }
  289. bool empty() const { return _M_ht.empty(); }
  290. void swap(hash_multimap& __hs) { _M_ht.swap(__hs._M_ht); }
  291. friend bool operator==<>(const hash_multimap&, const hash_multimap&);
  292. iterator begin() { return _M_ht.begin(); }
  293. iterator end() { return _M_ht.end(); }
  294. const_iterator begin() const { return _M_ht.begin(); }
  295. const_iterator end() const { return _M_ht.end(); }
  296. public:
  297. iterator insert(const value_type& __obj)
  298. {
  299. return _M_ht.insert_equal(__obj);
  300. }
  301. template <class _InputIterator>
  302. void insert(_InputIterator __f, _InputIterator __l)
  303. {
  304. _M_ht.insert_equal(__f, __l);
  305. }
  306. iterator insert_noresize(const value_type& __obj)
  307. {
  308. return _M_ht.insert_equal_noresize(__obj);
  309. }
  310. iterator find(const key_type& __key) { return _M_ht.find(__key); }
  311. const_iterator find(const key_type& __key) const
  312. {
  313. return _M_ht.find(__key);
  314. }
  315. size_type count(const key_type& __key) const { return _M_ht.count(__key); }
  316. std::pair<iterator, iterator> equal_range(const key_type& __key)
  317. {
  318. return _M_ht.equal_range(__key);
  319. }
  320. std::pair<const_iterator, const_iterator> equal_range(
  321. const key_type& __key) const
  322. {
  323. return _M_ht.equal_range(__key);
  324. }
  325. size_type erase(const key_type& __key) { return _M_ht.erase(__key); }
  326. void erase(iterator __it) { _M_ht.erase(__it); }
  327. void erase(iterator __f, iterator __l) { _M_ht.erase(__f, __l); }
  328. void clear() { _M_ht.clear(); }
  329. public:
  330. void resize(size_type __hint) { _M_ht.resize(__hint); }
  331. size_type bucket_count() const { return _M_ht.bucket_count(); }
  332. size_type max_bucket_count() const { return _M_ht.max_bucket_count(); }
  333. size_type elems_in_bucket(size_type __n) const
  334. {
  335. return _M_ht.elems_in_bucket(__n);
  336. }
  337. };
  338. template <class _Key, class _Tp, class _HF, class _EqKey, class _Alloc>
  339. bool operator==(const hash_multimap<_Key, _Tp, _HF, _EqKey, _Alloc>& __hm1,
  340. const hash_multimap<_Key, _Tp, _HF, _EqKey, _Alloc>& __hm2)
  341. {
  342. return __hm1._M_ht == __hm2._M_ht;
  343. }
  344. template <class _Key, class _Tp, class _HF, class _EqKey, class _Alloc>
  345. inline bool operator!=(
  346. const hash_multimap<_Key, _Tp, _HF, _EqKey, _Alloc>& __hm1,
  347. const hash_multimap<_Key, _Tp, _HF, _EqKey, _Alloc>& __hm2)
  348. {
  349. return !(__hm1 == __hm2);
  350. }
  351. template <class _Key, class _Tp, class _HashFcn, class _EqlKey, class _Alloc>
  352. inline void swap(hash_multimap<_Key, _Tp, _HashFcn, _EqlKey, _Alloc>& __hm1,
  353. hash_multimap<_Key, _Tp, _HashFcn, _EqlKey, _Alloc>& __hm2)
  354. {
  355. __hm1.swap(__hm2);
  356. }
  357. } // namespace @KWSYS_NAMESPACE@
  358. #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
  359. #pragma reset woff 1174
  360. #pragma reset woff 1375
  361. #endif
  362. #if defined(_MSC_VER)
  363. #pragma warning(pop)
  364. #endif
  365. #endif