hash_set.hxx.in 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 <@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. // identity is an extension: it is not part of the standard.
  52. template <class _Tp>
  53. struct _Identity : public @KWSYS_NAMESPACE@_stl::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 = @KWSYS_NAMESPACE@_stl::equal_to<_Value>,
  61. class _Alloc = @KWSYS_NAMESPACE@_HASH_DEFAULT_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. #if @KWSYS_NAMESPACE@_CXX_HAS_MEMBER_TEMPLATES
  102. template <class _InputIterator>
  103. hash_set(_InputIterator __f, _InputIterator __l)
  104. : _M_ht(100, hasher(), key_equal(), allocator_type())
  105. { _M_ht.insert_unique(__f, __l); }
  106. template <class _InputIterator>
  107. hash_set(_InputIterator __f, _InputIterator __l, size_type __n)
  108. : _M_ht(__n, hasher(), key_equal(), allocator_type())
  109. { _M_ht.insert_unique(__f, __l); }
  110. template <class _InputIterator>
  111. hash_set(_InputIterator __f, _InputIterator __l, size_type __n,
  112. const hasher& __hf)
  113. : _M_ht(__n, __hf, key_equal(), allocator_type())
  114. { _M_ht.insert_unique(__f, __l); }
  115. template <class _InputIterator>
  116. hash_set(_InputIterator __f, _InputIterator __l, size_type __n,
  117. const hasher& __hf, const key_equal& __eql,
  118. const allocator_type& __a = allocator_type())
  119. : _M_ht(__n, __hf, __eql, __a)
  120. { _M_ht.insert_unique(__f, __l); }
  121. #else
  122. hash_set(const value_type* __f, const value_type* __l)
  123. : _M_ht(100, hasher(), key_equal(), allocator_type())
  124. { _M_ht.insert_unique(__f, __l); }
  125. hash_set(const value_type* __f, const value_type* __l, size_type __n)
  126. : _M_ht(__n, hasher(), key_equal(), allocator_type())
  127. { _M_ht.insert_unique(__f, __l); }
  128. hash_set(const value_type* __f, const value_type* __l, size_type __n,
  129. const hasher& __hf)
  130. : _M_ht(__n, __hf, key_equal(), allocator_type())
  131. { _M_ht.insert_unique(__f, __l); }
  132. hash_set(const value_type* __f, const value_type* __l, size_type __n,
  133. const hasher& __hf, const key_equal& __eql,
  134. const allocator_type& __a = allocator_type())
  135. : _M_ht(__n, __hf, __eql, __a)
  136. { _M_ht.insert_unique(__f, __l); }
  137. hash_set(const_iterator __f, const_iterator __l)
  138. : _M_ht(100, hasher(), key_equal(), allocator_type())
  139. { _M_ht.insert_unique(__f, __l); }
  140. hash_set(const_iterator __f, const_iterator __l, size_type __n)
  141. : _M_ht(__n, hasher(), key_equal(), allocator_type())
  142. { _M_ht.insert_unique(__f, __l); }
  143. hash_set(const_iterator __f, const_iterator __l, size_type __n,
  144. const hasher& __hf)
  145. : _M_ht(__n, __hf, key_equal(), allocator_type())
  146. { _M_ht.insert_unique(__f, __l); }
  147. hash_set(const_iterator __f, const_iterator __l, size_type __n,
  148. const hasher& __hf, const key_equal& __eql,
  149. const allocator_type& __a = allocator_type())
  150. : _M_ht(__n, __hf, __eql, __a)
  151. { _M_ht.insert_unique(__f, __l); }
  152. #endif
  153. public:
  154. size_type size() const { return _M_ht.size(); }
  155. size_type max_size() const { return _M_ht.max_size(); }
  156. bool empty() const { return _M_ht.empty(); }
  157. void swap(hash_set& __hs) { _M_ht.swap(__hs._M_ht); }
  158. friend bool operator==@KWSYS_NAMESPACE@_CXX_NULL_TEMPLATE_ARGS(const hash_set&,
  159. const hash_set&);
  160. iterator begin() const { return _M_ht.begin(); }
  161. iterator end() const { return _M_ht.end(); }
  162. public:
  163. @KWSYS_NAMESPACE@_stl::pair<iterator, bool> insert(const value_type& __obj)
  164. {
  165. typedef typename _Ht::iterator _Ht_iterator;
  166. @KWSYS_NAMESPACE@_stl::pair<_Ht_iterator, bool> __p = _M_ht.insert_unique(__obj);
  167. return @KWSYS_NAMESPACE@_stl::pair<iterator,bool>(__p.first, __p.second);
  168. }
  169. #if @KWSYS_NAMESPACE@_CXX_HAS_MEMBER_TEMPLATES
  170. template <class _InputIterator>
  171. void insert(_InputIterator __f, _InputIterator __l)
  172. { _M_ht.insert_unique(__f,__l); }
  173. #else
  174. void insert(const value_type* __f, const value_type* __l) {
  175. _M_ht.insert_unique(__f,__l);
  176. }
  177. void insert(const_iterator __f, const_iterator __l)
  178. {_M_ht.insert_unique(__f, __l); }
  179. #endif
  180. @KWSYS_NAMESPACE@_stl::pair<iterator, bool> insert_noresize(const value_type& __obj)
  181. {
  182. typedef typename _Ht::iterator _Ht_iterator;
  183. @KWSYS_NAMESPACE@_stl::pair<_Ht_iterator, bool> __p =
  184. _M_ht.insert_unique_noresize(__obj);
  185. return @KWSYS_NAMESPACE@_stl::pair<iterator, bool>(__p.first, __p.second);
  186. }
  187. iterator find(const key_type& __key) const { return _M_ht.find(__key); }
  188. size_type count(const key_type& __key) const { return _M_ht.count(__key); }
  189. @KWSYS_NAMESPACE@_stl::pair<iterator, iterator> equal_range(const key_type& __key) const
  190. { return _M_ht.equal_range(__key); }
  191. size_type erase(const key_type& __key) {return _M_ht.erase(__key); }
  192. void erase(iterator __it) { _M_ht.erase(__it); }
  193. void erase(iterator __f, iterator __l) { _M_ht.erase(__f, __l); }
  194. void clear() { _M_ht.clear(); }
  195. public:
  196. void resize(size_type __hint) { _M_ht.resize(__hint); }
  197. size_type bucket_count() const { return _M_ht.bucket_count(); }
  198. size_type max_bucket_count() const { return _M_ht.max_bucket_count(); }
  199. size_type elems_in_bucket(size_type __n) const
  200. { return _M_ht.elems_in_bucket(__n); }
  201. };
  202. template <class _Value, class _HashFcn, class _EqualKey, class _Alloc>
  203. bool
  204. operator==(const hash_set<_Value,_HashFcn,_EqualKey,_Alloc>& __hs1,
  205. const hash_set<_Value,_HashFcn,_EqualKey,_Alloc>& __hs2)
  206. {
  207. return __hs1._M_ht == __hs2._M_ht;
  208. }
  209. template <class _Value, class _HashFcn, class _EqualKey, class _Alloc>
  210. inline bool
  211. operator!=(const hash_set<_Value,_HashFcn,_EqualKey,_Alloc>& __hs1,
  212. const hash_set<_Value,_HashFcn,_EqualKey,_Alloc>& __hs2) {
  213. return !(__hs1 == __hs2);
  214. }
  215. template <class _Val, class _HashFcn, class _EqualKey, class _Alloc>
  216. inline void
  217. swap(hash_set<_Val,_HashFcn,_EqualKey,_Alloc>& __hs1,
  218. hash_set<_Val,_HashFcn,_EqualKey,_Alloc>& __hs2)
  219. {
  220. __hs1.swap(__hs2);
  221. }
  222. template <class _Value,
  223. class _HashFcn = hash<_Value>,
  224. class _EqualKey = @KWSYS_NAMESPACE@_stl::equal_to<_Value>,
  225. class _Alloc = @KWSYS_NAMESPACE@_HASH_DEFAULT_ALLOCATOR(char) >
  226. class hash_multiset;
  227. template <class _Val, class _HashFcn, class _EqualKey, class _Alloc>
  228. bool
  229. operator==(const hash_multiset<_Val,_HashFcn,_EqualKey,_Alloc>& __hs1,
  230. const hash_multiset<_Val,_HashFcn,_EqualKey,_Alloc>& __hs2);
  231. template <class _Value, class _HashFcn, class _EqualKey, class _Alloc>
  232. class hash_multiset
  233. {
  234. private:
  235. typedef hashtable<_Value, _Value, _HashFcn, _Identity<_Value>,
  236. _EqualKey, _Alloc> _Ht;
  237. _Ht _M_ht;
  238. public:
  239. typedef typename _Ht::key_type key_type;
  240. typedef typename _Ht::value_type value_type;
  241. typedef typename _Ht::hasher hasher;
  242. typedef typename _Ht::key_equal key_equal;
  243. typedef typename _Ht::size_type size_type;
  244. typedef typename _Ht::difference_type difference_type;
  245. typedef typename _Ht::const_pointer pointer;
  246. typedef typename _Ht::const_pointer const_pointer;
  247. typedef typename _Ht::const_reference reference;
  248. typedef typename _Ht::const_reference const_reference;
  249. typedef typename _Ht::const_iterator iterator;
  250. typedef typename _Ht::const_iterator const_iterator;
  251. typedef typename _Ht::allocator_type allocator_type;
  252. hasher hash_funct() const { return _M_ht.hash_funct(); }
  253. key_equal key_eq() const { return _M_ht.key_eq(); }
  254. allocator_type get_allocator() const { return _M_ht.get_allocator(); }
  255. public:
  256. hash_multiset()
  257. : _M_ht(100, hasher(), key_equal(), allocator_type()) {}
  258. explicit hash_multiset(size_type __n)
  259. : _M_ht(__n, hasher(), key_equal(), allocator_type()) {}
  260. hash_multiset(size_type __n, const hasher& __hf)
  261. : _M_ht(__n, __hf, key_equal(), allocator_type()) {}
  262. hash_multiset(size_type __n, const hasher& __hf, const key_equal& __eql,
  263. const allocator_type& __a = allocator_type())
  264. : _M_ht(__n, __hf, __eql, __a) {}
  265. #if @KWSYS_NAMESPACE@_CXX_HAS_MEMBER_TEMPLATES
  266. template <class _InputIterator>
  267. hash_multiset(_InputIterator __f, _InputIterator __l)
  268. : _M_ht(100, hasher(), key_equal(), allocator_type())
  269. { _M_ht.insert_equal(__f, __l); }
  270. template <class _InputIterator>
  271. hash_multiset(_InputIterator __f, _InputIterator __l, size_type __n)
  272. : _M_ht(__n, hasher(), key_equal(), allocator_type())
  273. { _M_ht.insert_equal(__f, __l); }
  274. template <class _InputIterator>
  275. hash_multiset(_InputIterator __f, _InputIterator __l, size_type __n,
  276. const hasher& __hf)
  277. : _M_ht(__n, __hf, key_equal(), allocator_type())
  278. { _M_ht.insert_equal(__f, __l); }
  279. template <class _InputIterator>
  280. hash_multiset(_InputIterator __f, _InputIterator __l, size_type __n,
  281. const hasher& __hf, const key_equal& __eql,
  282. const allocator_type& __a = allocator_type())
  283. : _M_ht(__n, __hf, __eql, __a)
  284. { _M_ht.insert_equal(__f, __l); }
  285. #else
  286. hash_multiset(const value_type* __f, const value_type* __l)
  287. : _M_ht(100, hasher(), key_equal(), allocator_type())
  288. { _M_ht.insert_equal(__f, __l); }
  289. hash_multiset(const value_type* __f, const value_type* __l, size_type __n)
  290. : _M_ht(__n, hasher(), key_equal(), allocator_type())
  291. { _M_ht.insert_equal(__f, __l); }
  292. hash_multiset(const value_type* __f, const value_type* __l, size_type __n,
  293. const hasher& __hf)
  294. : _M_ht(__n, __hf, key_equal(), allocator_type())
  295. { _M_ht.insert_equal(__f, __l); }
  296. hash_multiset(const value_type* __f, const value_type* __l, size_type __n,
  297. const hasher& __hf, const key_equal& __eql,
  298. const allocator_type& __a = allocator_type())
  299. : _M_ht(__n, __hf, __eql, __a)
  300. { _M_ht.insert_equal(__f, __l); }
  301. hash_multiset(const_iterator __f, const_iterator __l)
  302. : _M_ht(100, hasher(), key_equal(), allocator_type())
  303. { _M_ht.insert_equal(__f, __l); }
  304. hash_multiset(const_iterator __f, const_iterator __l, size_type __n)
  305. : _M_ht(__n, hasher(), key_equal(), allocator_type())
  306. { _M_ht.insert_equal(__f, __l); }
  307. hash_multiset(const_iterator __f, const_iterator __l, size_type __n,
  308. const hasher& __hf)
  309. : _M_ht(__n, __hf, key_equal(), allocator_type())
  310. { _M_ht.insert_equal(__f, __l); }
  311. hash_multiset(const_iterator __f, const_iterator __l, size_type __n,
  312. const hasher& __hf, const key_equal& __eql,
  313. const allocator_type& __a = allocator_type())
  314. : _M_ht(__n, __hf, __eql, __a)
  315. { _M_ht.insert_equal(__f, __l); }
  316. #endif
  317. public:
  318. size_type size() const { return _M_ht.size(); }
  319. size_type max_size() const { return _M_ht.max_size(); }
  320. bool empty() const { return _M_ht.empty(); }
  321. void swap(hash_multiset& hs) { _M_ht.swap(hs._M_ht); }
  322. friend bool operator==@KWSYS_NAMESPACE@_CXX_NULL_TEMPLATE_ARGS(const hash_multiset&,
  323. const hash_multiset&);
  324. iterator begin() const { return _M_ht.begin(); }
  325. iterator end() const { return _M_ht.end(); }
  326. public:
  327. iterator insert(const value_type& __obj)
  328. { return _M_ht.insert_equal(__obj); }
  329. #if @KWSYS_NAMESPACE@_CXX_HAS_MEMBER_TEMPLATES
  330. template <class _InputIterator>
  331. void insert(_InputIterator __f, _InputIterator __l)
  332. { _M_ht.insert_equal(__f,__l); }
  333. #else
  334. void insert(const value_type* __f, const value_type* __l) {
  335. _M_ht.insert_equal(__f,__l);
  336. }
  337. void insert(const_iterator __f, const_iterator __l)
  338. { _M_ht.insert_equal(__f, __l); }
  339. #endif
  340. iterator insert_noresize(const value_type& __obj)
  341. { return _M_ht.insert_equal_noresize(__obj); }
  342. iterator find(const key_type& __key) const { return _M_ht.find(__key); }
  343. size_type count(const key_type& __key) const { return _M_ht.count(__key); }
  344. @KWSYS_NAMESPACE@_stl::pair<iterator, iterator> equal_range(const key_type& __key) const
  345. { return _M_ht.equal_range(__key); }
  346. size_type erase(const key_type& __key) {return _M_ht.erase(__key); }
  347. void erase(iterator __it) { _M_ht.erase(__it); }
  348. void erase(iterator __f, iterator __l) { _M_ht.erase(__f, __l); }
  349. void clear() { _M_ht.clear(); }
  350. public:
  351. void resize(size_type __hint) { _M_ht.resize(__hint); }
  352. size_type bucket_count() const { return _M_ht.bucket_count(); }
  353. size_type max_bucket_count() const { return _M_ht.max_bucket_count(); }
  354. size_type elems_in_bucket(size_type __n) const
  355. { return _M_ht.elems_in_bucket(__n); }
  356. };
  357. template <class _Val, class _HashFcn, class _EqualKey, class _Alloc>
  358. bool
  359. operator==(const hash_multiset<_Val,_HashFcn,_EqualKey,_Alloc>& __hs1,
  360. const hash_multiset<_Val,_HashFcn,_EqualKey,_Alloc>& __hs2)
  361. {
  362. return __hs1._M_ht == __hs2._M_ht;
  363. }
  364. template <class _Val, class _HashFcn, class _EqualKey, class _Alloc>
  365. inline bool
  366. operator!=(const hash_multiset<_Val,_HashFcn,_EqualKey,_Alloc>& __hs1,
  367. const hash_multiset<_Val,_HashFcn,_EqualKey,_Alloc>& __hs2) {
  368. return !(__hs1 == __hs2);
  369. }
  370. template <class _Val, class _HashFcn, class _EqualKey, class _Alloc>
  371. inline void
  372. swap(hash_multiset<_Val,_HashFcn,_EqualKey,_Alloc>& __hs1,
  373. hash_multiset<_Val,_HashFcn,_EqualKey,_Alloc>& __hs2) {
  374. __hs1.swap(__hs2);
  375. }
  376. } // namespace @KWSYS_NAMESPACE@
  377. #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
  378. # pragma reset woff 1174
  379. # pragma reset woff 1375
  380. #endif
  381. #if defined(_MSC_VER)
  382. # pragma warning (pop)
  383. #endif
  384. #endif