hash_set.hxx.in 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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_set_hxx
  36. #define @KWSYS_NAMESPACE@_hash_set_hxx
  37. #include <@KWSYS_NAMESPACE@/hashtable.hxx>
  38. #include <@KWSYS_NAMESPACE@/hash_fun.hxx>
  39. #include <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. // identity is an extension: it is not part of the standard.
  52. template <class _Tp>
  53. struct _Identity : public std::unary_function<_Tp,_Tp>
  54. {
  55. const _Tp& operator()(const _Tp& __x) const { return __x; }
  56. };
  57. // Forward declaration of equality operator; needed for friend declaration.
  58. template <class _Value,
  59. class _HashFcn = hash<_Value>,
  60. class _EqualKey = std::equal_to<_Value>,
  61. class _Alloc = std::allocator<char> >
  62. class hash_set;
  63. template <class _Value, class _HashFcn, class _EqualKey, class _Alloc>
  64. bool
  65. operator==(const hash_set<_Value,_HashFcn,_EqualKey,_Alloc>& __hs1,
  66. const hash_set<_Value,_HashFcn,_EqualKey,_Alloc>& __hs2);
  67. template <class _Value, class _HashFcn, class _EqualKey, class _Alloc>
  68. class hash_set
  69. {
  70. private:
  71. typedef hashtable<_Value, _Value, _HashFcn, _Identity<_Value>,
  72. _EqualKey, _Alloc> _Ht;
  73. _Ht _M_ht;
  74. public:
  75. typedef typename _Ht::key_type key_type;
  76. typedef typename _Ht::value_type value_type;
  77. typedef typename _Ht::hasher hasher;
  78. typedef typename _Ht::key_equal key_equal;
  79. typedef typename _Ht::size_type size_type;
  80. typedef typename _Ht::difference_type difference_type;
  81. typedef typename _Ht::const_pointer pointer;
  82. typedef typename _Ht::const_pointer const_pointer;
  83. typedef typename _Ht::const_reference reference;
  84. typedef typename _Ht::const_reference const_reference;
  85. typedef typename _Ht::const_iterator iterator;
  86. typedef typename _Ht::const_iterator const_iterator;
  87. typedef typename _Ht::allocator_type allocator_type;
  88. hasher hash_funct() const { return _M_ht.hash_funct(); }
  89. key_equal key_eq() const { return _M_ht.key_eq(); }
  90. allocator_type get_allocator() const { return _M_ht.get_allocator(); }
  91. public:
  92. hash_set()
  93. : _M_ht(100, hasher(), key_equal(), allocator_type()) {}
  94. explicit hash_set(size_type __n)
  95. : _M_ht(__n, hasher(), key_equal(), allocator_type()) {}
  96. hash_set(size_type __n, const hasher& __hf)
  97. : _M_ht(__n, __hf, key_equal(), allocator_type()) {}
  98. hash_set(size_type __n, const hasher& __hf, const key_equal& __eql,
  99. const allocator_type& __a = allocator_type())
  100. : _M_ht(__n, __hf, __eql, __a) {}
  101. template <class _InputIterator>
  102. hash_set(_InputIterator __f, _InputIterator __l)
  103. : _M_ht(100, hasher(), key_equal(), allocator_type())
  104. { _M_ht.insert_unique(__f, __l); }
  105. template <class _InputIterator>
  106. hash_set(_InputIterator __f, _InputIterator __l, size_type __n)
  107. : _M_ht(__n, hasher(), key_equal(), allocator_type())
  108. { _M_ht.insert_unique(__f, __l); }
  109. template <class _InputIterator>
  110. hash_set(_InputIterator __f, _InputIterator __l, size_type __n,
  111. const hasher& __hf)
  112. : _M_ht(__n, __hf, key_equal(), allocator_type())
  113. { _M_ht.insert_unique(__f, __l); }
  114. template <class _InputIterator>
  115. hash_set(_InputIterator __f, _InputIterator __l, size_type __n,
  116. const hasher& __hf, const key_equal& __eql,
  117. const allocator_type& __a = allocator_type())
  118. : _M_ht(__n, __hf, __eql, __a)
  119. { _M_ht.insert_unique(__f, __l); }
  120. public:
  121. size_type size() const { return _M_ht.size(); }
  122. size_type max_size() const { return _M_ht.max_size(); }
  123. bool empty() const { return _M_ht.empty(); }
  124. void swap(hash_set& __hs) { _M_ht.swap(__hs._M_ht); }
  125. friend bool operator==<>(const hash_set&,
  126. const hash_set&);
  127. iterator begin() const { return _M_ht.begin(); }
  128. iterator end() const { return _M_ht.end(); }
  129. public:
  130. std::pair<iterator, bool> insert(const value_type& __obj)
  131. {
  132. typedef typename _Ht::iterator _Ht_iterator;
  133. std::pair<_Ht_iterator, bool> __p = _M_ht.insert_unique(__obj);
  134. return std::pair<iterator,bool>(__p.first, __p.second);
  135. }
  136. template <class _InputIterator>
  137. void insert(_InputIterator __f, _InputIterator __l)
  138. { _M_ht.insert_unique(__f,__l); }
  139. std::pair<iterator, bool> insert_noresize(const value_type& __obj)
  140. {
  141. typedef typename _Ht::iterator _Ht_iterator;
  142. std::pair<_Ht_iterator, bool> __p =
  143. _M_ht.insert_unique_noresize(__obj);
  144. return std::pair<iterator, bool>(__p.first, __p.second);
  145. }
  146. iterator find(const key_type& __key) const { return _M_ht.find(__key); }
  147. size_type count(const key_type& __key) const { return _M_ht.count(__key); }
  148. std::pair<iterator, iterator> equal_range(const key_type& __key) const
  149. { return _M_ht.equal_range(__key); }
  150. size_type erase(const key_type& __key) {return _M_ht.erase(__key); }
  151. void erase(iterator __it) { _M_ht.erase(__it); }
  152. void erase(iterator __f, iterator __l) { _M_ht.erase(__f, __l); }
  153. void clear() { _M_ht.clear(); }
  154. public:
  155. void resize(size_type __hint) { _M_ht.resize(__hint); }
  156. size_type bucket_count() const { return _M_ht.bucket_count(); }
  157. size_type max_bucket_count() const { return _M_ht.max_bucket_count(); }
  158. size_type elems_in_bucket(size_type __n) const
  159. { return _M_ht.elems_in_bucket(__n); }
  160. };
  161. template <class _Value, class _HashFcn, class _EqualKey, class _Alloc>
  162. bool
  163. operator==(const hash_set<_Value,_HashFcn,_EqualKey,_Alloc>& __hs1,
  164. const hash_set<_Value,_HashFcn,_EqualKey,_Alloc>& __hs2)
  165. {
  166. return __hs1._M_ht == __hs2._M_ht;
  167. }
  168. template <class _Value, class _HashFcn, class _EqualKey, class _Alloc>
  169. inline bool
  170. operator!=(const hash_set<_Value,_HashFcn,_EqualKey,_Alloc>& __hs1,
  171. const hash_set<_Value,_HashFcn,_EqualKey,_Alloc>& __hs2) {
  172. return !(__hs1 == __hs2);
  173. }
  174. template <class _Val, class _HashFcn, class _EqualKey, class _Alloc>
  175. inline void
  176. swap(hash_set<_Val,_HashFcn,_EqualKey,_Alloc>& __hs1,
  177. hash_set<_Val,_HashFcn,_EqualKey,_Alloc>& __hs2)
  178. {
  179. __hs1.swap(__hs2);
  180. }
  181. template <class _Value,
  182. class _HashFcn = hash<_Value>,
  183. class _EqualKey = std::equal_to<_Value>,
  184. class _Alloc = std::allocator<char> >
  185. class hash_multiset;
  186. template <class _Val, class _HashFcn, class _EqualKey, class _Alloc>
  187. bool
  188. operator==(const hash_multiset<_Val,_HashFcn,_EqualKey,_Alloc>& __hs1,
  189. const hash_multiset<_Val,_HashFcn,_EqualKey,_Alloc>& __hs2);
  190. template <class _Value, class _HashFcn, class _EqualKey, class _Alloc>
  191. class hash_multiset
  192. {
  193. private:
  194. typedef hashtable<_Value, _Value, _HashFcn, _Identity<_Value>,
  195. _EqualKey, _Alloc> _Ht;
  196. _Ht _M_ht;
  197. public:
  198. typedef typename _Ht::key_type key_type;
  199. typedef typename _Ht::value_type value_type;
  200. typedef typename _Ht::hasher hasher;
  201. typedef typename _Ht::key_equal key_equal;
  202. typedef typename _Ht::size_type size_type;
  203. typedef typename _Ht::difference_type difference_type;
  204. typedef typename _Ht::const_pointer pointer;
  205. typedef typename _Ht::const_pointer const_pointer;
  206. typedef typename _Ht::const_reference reference;
  207. typedef typename _Ht::const_reference const_reference;
  208. typedef typename _Ht::const_iterator iterator;
  209. typedef typename _Ht::const_iterator const_iterator;
  210. typedef typename _Ht::allocator_type allocator_type;
  211. hasher hash_funct() const { return _M_ht.hash_funct(); }
  212. key_equal key_eq() const { return _M_ht.key_eq(); }
  213. allocator_type get_allocator() const { return _M_ht.get_allocator(); }
  214. public:
  215. hash_multiset()
  216. : _M_ht(100, hasher(), key_equal(), allocator_type()) {}
  217. explicit hash_multiset(size_type __n)
  218. : _M_ht(__n, hasher(), key_equal(), allocator_type()) {}
  219. hash_multiset(size_type __n, const hasher& __hf)
  220. : _M_ht(__n, __hf, key_equal(), allocator_type()) {}
  221. hash_multiset(size_type __n, const hasher& __hf, const key_equal& __eql,
  222. const allocator_type& __a = allocator_type())
  223. : _M_ht(__n, __hf, __eql, __a) {}
  224. template <class _InputIterator>
  225. hash_multiset(_InputIterator __f, _InputIterator __l)
  226. : _M_ht(100, hasher(), key_equal(), allocator_type())
  227. { _M_ht.insert_equal(__f, __l); }
  228. template <class _InputIterator>
  229. hash_multiset(_InputIterator __f, _InputIterator __l, size_type __n)
  230. : _M_ht(__n, hasher(), key_equal(), allocator_type())
  231. { _M_ht.insert_equal(__f, __l); }
  232. template <class _InputIterator>
  233. hash_multiset(_InputIterator __f, _InputIterator __l, size_type __n,
  234. const hasher& __hf)
  235. : _M_ht(__n, __hf, key_equal(), allocator_type())
  236. { _M_ht.insert_equal(__f, __l); }
  237. template <class _InputIterator>
  238. hash_multiset(_InputIterator __f, _InputIterator __l, size_type __n,
  239. const hasher& __hf, const key_equal& __eql,
  240. const allocator_type& __a = allocator_type())
  241. : _M_ht(__n, __hf, __eql, __a)
  242. { _M_ht.insert_equal(__f, __l); }
  243. public:
  244. size_type size() const { return _M_ht.size(); }
  245. size_type max_size() const { return _M_ht.max_size(); }
  246. bool empty() const { return _M_ht.empty(); }
  247. void swap(hash_multiset& hs) { _M_ht.swap(hs._M_ht); }
  248. friend bool operator==<>(const hash_multiset&,
  249. const hash_multiset&);
  250. iterator begin() const { return _M_ht.begin(); }
  251. iterator end() const { return _M_ht.end(); }
  252. public:
  253. iterator insert(const value_type& __obj)
  254. { return _M_ht.insert_equal(__obj); }
  255. template <class _InputIterator>
  256. void insert(_InputIterator __f, _InputIterator __l)
  257. { _M_ht.insert_equal(__f,__l); }
  258. iterator insert_noresize(const value_type& __obj)
  259. { return _M_ht.insert_equal_noresize(__obj); }
  260. iterator find(const key_type& __key) const { return _M_ht.find(__key); }
  261. size_type count(const key_type& __key) const { return _M_ht.count(__key); }
  262. std::pair<iterator, iterator> equal_range(const key_type& __key) const
  263. { return _M_ht.equal_range(__key); }
  264. size_type erase(const key_type& __key) {return _M_ht.erase(__key); }
  265. void erase(iterator __it) { _M_ht.erase(__it); }
  266. void erase(iterator __f, iterator __l) { _M_ht.erase(__f, __l); }
  267. void clear() { _M_ht.clear(); }
  268. public:
  269. void resize(size_type __hint) { _M_ht.resize(__hint); }
  270. size_type bucket_count() const { return _M_ht.bucket_count(); }
  271. size_type max_bucket_count() const { return _M_ht.max_bucket_count(); }
  272. size_type elems_in_bucket(size_type __n) const
  273. { return _M_ht.elems_in_bucket(__n); }
  274. };
  275. template <class _Val, class _HashFcn, class _EqualKey, class _Alloc>
  276. bool
  277. operator==(const hash_multiset<_Val,_HashFcn,_EqualKey,_Alloc>& __hs1,
  278. const hash_multiset<_Val,_HashFcn,_EqualKey,_Alloc>& __hs2)
  279. {
  280. return __hs1._M_ht == __hs2._M_ht;
  281. }
  282. template <class _Val, class _HashFcn, class _EqualKey, class _Alloc>
  283. inline bool
  284. operator!=(const hash_multiset<_Val,_HashFcn,_EqualKey,_Alloc>& __hs1,
  285. const hash_multiset<_Val,_HashFcn,_EqualKey,_Alloc>& __hs2) {
  286. return !(__hs1 == __hs2);
  287. }
  288. template <class _Val, class _HashFcn, class _EqualKey, class _Alloc>
  289. inline void
  290. swap(hash_multiset<_Val,_HashFcn,_EqualKey,_Alloc>& __hs1,
  291. hash_multiset<_Val,_HashFcn,_EqualKey,_Alloc>& __hs2) {
  292. __hs1.swap(__hs2);
  293. }
  294. } // namespace @KWSYS_NAMESPACE@
  295. #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
  296. # pragma reset woff 1174
  297. # pragma reset woff 1375
  298. #endif
  299. #if defined(_MSC_VER)
  300. # pragma warning (pop)
  301. #endif
  302. #endif