hash_set.hxx.in 13 KB

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