hash_map.hxx.in 18 KB

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