_unordered_set.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * Copyright (c) 2004
  3. * Francois Dumont
  4. *
  5. * This material is provided "as is", with absolutely no warranty expressed
  6. * or implied. Any use is at your own risk.
  7. *
  8. * Permission to use or copy this software for any purpose is hereby granted
  9. * without fee, provided the above notices are retained on all copies.
  10. * Permission to modify the code and to distribute modified code is granted,
  11. * provided the above notices are retained, and a notice that the code was
  12. * modified is included with the above copyright notice.
  13. *
  14. */
  15. /* NOTE: This is an internal header file, included by other STL headers.
  16. * You should not attempt to use it directly.
  17. */
  18. #ifndef _STLP_INTERNAL_UNORDERED_SET_H
  19. #define _STLP_INTERNAL_UNORDERED_SET_H
  20. #ifndef _STLP_INTERNAL_HASHTABLE_H
  21. # include <stl/_hashtable.h>
  22. #endif
  23. _STLP_BEGIN_NAMESPACE
  24. //Specific iterator traits creation
  25. _STLP_CREATE_HASH_ITERATOR_TRAITS(UnorderedSetTraitsT, Const_traits)
  26. _STLP_BEGIN_TR1_NAMESPACE
  27. template <class _Value, _STLP_DFL_TMPL_PARAM(_HashFcn,hash<_Value>),
  28. _STLP_DFL_TMPL_PARAM(_EqualKey, equal_to<_Value>),
  29. _STLP_DFL_TMPL_PARAM(_Alloc, allocator<_Value>) >
  30. class unordered_set
  31. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
  32. : public __stlport_class<unordered_set<_Value, _HashFcn, _EqualKey, _Alloc> >
  33. #endif
  34. {
  35. typedef unordered_set<_Value, _HashFcn, _EqualKey, _Alloc> _Self;
  36. //Specific iterator traits creation
  37. typedef _STLP_PRIV _UnorderedSetTraitsT<_Value> _UnorderedSetTraits;
  38. public:
  39. typedef hashtable<_Value, _Value, _HashFcn,
  40. _UnorderedSetTraits, _STLP_PRIV _Identity<_Value>, _EqualKey, _Alloc> _Ht;
  41. public:
  42. typedef typename _Ht::key_type key_type;
  43. typedef typename _Ht::value_type value_type;
  44. typedef typename _Ht::hasher hasher;
  45. typedef typename _Ht::key_equal key_equal;
  46. typedef typename _Ht::size_type size_type;
  47. typedef typename _Ht::difference_type difference_type;
  48. typedef typename _Ht::pointer pointer;
  49. typedef typename _Ht::const_pointer const_pointer;
  50. typedef typename _Ht::reference reference;
  51. typedef typename _Ht::const_reference const_reference;
  52. typedef typename _Ht::iterator iterator;
  53. typedef typename _Ht::const_iterator const_iterator;
  54. typedef typename _Ht::local_iterator local_iterator;
  55. typedef typename _Ht::const_local_iterator const_local_iterator;
  56. typedef typename _Ht::allocator_type allocator_type;
  57. hasher hash_function() const { return _M_ht.hash_funct(); }
  58. key_equal key_eq() const { return _M_ht.key_eq(); }
  59. allocator_type get_allocator() const { return _M_ht.get_allocator(); }
  60. private:
  61. _Ht _M_ht;
  62. _STLP_KEY_TYPE_FOR_CONT_EXT(key_type)
  63. public:
  64. explicit unordered_set(size_type __n = 0, const hasher& __hf = hasher(),
  65. const key_equal& __eql = key_equal(),
  66. const allocator_type& __a = allocator_type())
  67. : _M_ht(__n, __hf, __eql, __a) {}
  68. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  69. unordered_set(__move_source<_Self> src)
  70. : _M_ht(__move_source<_Ht>(src.get()._M_ht)) {}
  71. #endif
  72. #if defined (_STLP_MEMBER_TEMPLATES)
  73. template <class _InputIterator>
  74. unordered_set(_InputIterator __f, _InputIterator __l,
  75. size_type __n = 0, const hasher& __hf = hasher(),
  76. const key_equal& __eql = key_equal(),
  77. const allocator_type& __a = allocator_type())
  78. : _M_ht(__n, __hf, __eql, __a)
  79. { _M_ht.insert_unique(__f, __l); }
  80. #else
  81. unordered_set(const value_type* __f, const value_type* __l,
  82. size_type __n = 0, const hasher& __hf = hasher(),
  83. const key_equal& __eql = key_equal(),
  84. const allocator_type& __a = allocator_type())
  85. : _M_ht(__n, __hf, __eql, __a)
  86. { _M_ht.insert_unique(__f, __l); }
  87. unordered_set(const_iterator __f, const_iterator __l,
  88. size_type __n = 0, const hasher& __hf = hasher(),
  89. const key_equal& __eql = key_equal(),
  90. const allocator_type& __a = allocator_type())
  91. : _M_ht(__n, __hf, __eql, __a)
  92. { _M_ht.insert_unique(__f, __l); }
  93. #endif /*_STLP_MEMBER_TEMPLATES */
  94. _Self& operator = (const _Self& __other)
  95. { _M_ht = __other._M_ht; return *this; }
  96. size_type size() const { return _M_ht.size(); }
  97. size_type max_size() const { return _M_ht.max_size(); }
  98. bool empty() const { return _M_ht.empty(); }
  99. void swap(_Self& __hs) { _M_ht.swap(__hs._M_ht); }
  100. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  101. void _M_swap_workaround(_Self& __x) { swap(__x); }
  102. #endif
  103. iterator begin() { return _M_ht.begin(); }
  104. iterator end() { return _M_ht.end(); }
  105. const_iterator begin() const { return _M_ht.begin(); }
  106. const_iterator end() const { return _M_ht.end(); }
  107. pair<iterator, bool> insert(const value_type& __obj)
  108. { return _M_ht.insert_unique(__obj); }
  109. iterator insert(const_iterator /*__hint*/, const value_type& __obj)
  110. { return _M_ht.insert_unique(__obj); }
  111. #if defined (_STLP_MEMBER_TEMPLATES)
  112. template <class _InputIterator>
  113. void insert(_InputIterator __f, _InputIterator __l)
  114. #else
  115. void insert(const_iterator __f, const_iterator __l)
  116. {_M_ht.insert_unique(__f, __l); }
  117. void insert(const value_type* __f, const value_type* __l)
  118. #endif
  119. { _M_ht.insert_unique(__f,__l); }
  120. _STLP_TEMPLATE_FOR_CONT_EXT
  121. iterator find(const _KT& __key) { return _M_ht.find(__key); }
  122. _STLP_TEMPLATE_FOR_CONT_EXT
  123. const_iterator find(const _KT& __key) const { return _M_ht.find(__key); }
  124. _STLP_TEMPLATE_FOR_CONT_EXT
  125. size_type count(const _KT& __key) const { return _M_ht.count(__key); }
  126. _STLP_TEMPLATE_FOR_CONT_EXT
  127. pair<iterator, iterator> equal_range(const _KT& __key)
  128. { return _M_ht.equal_range(__key); }
  129. _STLP_TEMPLATE_FOR_CONT_EXT
  130. pair<const_iterator, const_iterator> equal_range(const _KT& __key) const
  131. { return _M_ht.equal_range(__key); }
  132. size_type erase(const key_type& __key) {return _M_ht.erase(__key); }
  133. void erase(const_iterator __it) { _M_ht.erase(__it); }
  134. void erase(const_iterator __f, const_iterator __l) { _M_ht.erase(__f, __l); }
  135. void clear() { _M_ht.clear(); }
  136. size_type bucket_count() const { return _M_ht.bucket_count(); }
  137. size_type max_bucket_count() const { return _M_ht.max_bucket_count(); }
  138. size_type bucket_size(size_type __n) const { return _M_ht.elems_in_bucket(__n); }
  139. _STLP_TEMPLATE_FOR_CONT_EXT
  140. size_type bucket(const _KT& __k) const { return _M_ht.bucket(__k); }
  141. local_iterator begin(size_type __n) { return _M_ht.begin(__n); }
  142. local_iterator end(size_type __n) { return _M_ht.end(__n); }
  143. const_local_iterator begin(size_type __n) const { return _M_ht.begin(__n); }
  144. const_local_iterator end(size_type __n) const { return _M_ht.end(__n); }
  145. float load_factor() const { return _M_ht.load_factor(); }
  146. float max_load_factor() const { return _M_ht.max_load_factor(); }
  147. void max_load_factor(float __val) { _M_ht.max_load_factor(__val); }
  148. void rehash(size_type __hint) { _M_ht.rehash(__hint); }
  149. };
  150. _STLP_END_NAMESPACE
  151. //Specific iterator traits creation
  152. _STLP_CREATE_HASH_ITERATOR_TRAITS(UnorderedMultisetTraitsT, Const_traits)
  153. _STLP_BEGIN_TR1_NAMESPACE
  154. template <class _Value, _STLP_DFL_TMPL_PARAM(_HashFcn,hash<_Value>),
  155. _STLP_DFL_TMPL_PARAM(_EqualKey, equal_to<_Value>),
  156. _STLP_DFL_TMPL_PARAM(_Alloc, allocator<_Value>) >
  157. class unordered_multiset
  158. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
  159. : public __stlport_class<unordered_multiset<_Value, _HashFcn, _EqualKey, _Alloc> >
  160. #endif
  161. {
  162. typedef unordered_multiset<_Value, _HashFcn, _EqualKey, _Alloc> _Self;
  163. //Specific iterator traits creation
  164. typedef _STLP_PRIV _UnorderedMultisetTraitsT<_Value> _UnorderedMultisetTraits;
  165. public:
  166. typedef hashtable<_Value, _Value, _HashFcn,
  167. _UnorderedMultisetTraits, _STLP_PRIV _Identity<_Value>, _EqualKey, _Alloc> _Ht;
  168. typedef typename _Ht::key_type key_type;
  169. typedef typename _Ht::value_type value_type;
  170. typedef typename _Ht::hasher hasher;
  171. typedef typename _Ht::key_equal key_equal;
  172. typedef typename _Ht::size_type size_type;
  173. typedef typename _Ht::difference_type difference_type;
  174. typedef typename _Ht::pointer pointer;
  175. typedef typename _Ht::const_pointer const_pointer;
  176. typedef typename _Ht::reference reference;
  177. typedef typename _Ht::const_reference const_reference;
  178. typedef typename _Ht::iterator iterator;
  179. typedef typename _Ht::const_iterator const_iterator;
  180. typedef typename _Ht::local_iterator local_iterator;
  181. typedef typename _Ht::const_local_iterator const_local_iterator;
  182. typedef typename _Ht::allocator_type allocator_type;
  183. hasher hash_function() const { return _M_ht.hash_funct(); }
  184. key_equal key_eq() const { return _M_ht.key_eq(); }
  185. allocator_type get_allocator() const { return _M_ht.get_allocator(); }
  186. private:
  187. _Ht _M_ht;
  188. _STLP_KEY_TYPE_FOR_CONT_EXT(key_type)
  189. public:
  190. explicit unordered_multiset(size_type __n = 0, const hasher& __hf = hasher(),
  191. const key_equal& __eql = key_equal(),
  192. const allocator_type& __a = allocator_type())
  193. : _M_ht(__n, __hf, __eql, __a) {}
  194. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  195. unordered_multiset(__move_source<_Self> src)
  196. : _M_ht(__move_source<_Ht>(src.get()._M_ht)) {}
  197. #endif
  198. #if defined (_STLP_MEMBER_TEMPLATES)
  199. template <class _InputIterator>
  200. unordered_multiset(_InputIterator __f, _InputIterator __l,
  201. size_type __n = 0, const hasher& __hf = hasher(),
  202. const key_equal& __eql = key_equal(),
  203. const allocator_type& __a = allocator_type())
  204. : _M_ht(__n, __hf, __eql, __a)
  205. { _M_ht.insert_equal(__f, __l); }
  206. #else
  207. unordered_multiset(const value_type* __f, const value_type* __l,
  208. size_type __n = 0, const hasher& __hf = hasher(),
  209. const key_equal& __eql = key_equal(),
  210. const allocator_type& __a = allocator_type())
  211. : _M_ht(__n, __hf, __eql, __a)
  212. { _M_ht.insert_equal(__f, __l); }
  213. unordered_multiset(const_iterator __f, const_iterator __l,
  214. size_type __n = 0, const hasher& __hf = hasher(),
  215. const key_equal& __eql = key_equal(),
  216. const allocator_type& __a = allocator_type())
  217. : _M_ht(__n, __hf, __eql, __a)
  218. { _M_ht.insert_equal(__f, __l); }
  219. #endif /*_STLP_MEMBER_TEMPLATES */
  220. _Self& operator = (const _Self& __other)
  221. { _M_ht = __other._M_ht; return *this; }
  222. size_type size() const { return _M_ht.size(); }
  223. size_type max_size() const { return _M_ht.max_size(); }
  224. bool empty() const { return _M_ht.empty(); }
  225. void swap(_Self& hs) { _M_ht.swap(hs._M_ht); }
  226. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  227. void _M_swap_workaround(_Self& __x) { swap(__x); }
  228. #endif
  229. iterator begin() { return _M_ht.begin(); }
  230. iterator end() { return _M_ht.end(); }
  231. const_iterator begin() const { return _M_ht.begin(); }
  232. const_iterator end() const { return _M_ht.end(); }
  233. iterator insert(const value_type& __obj)
  234. { return _M_ht.insert_equal(__obj); }
  235. iterator insert(const_iterator /*__hint*/, const value_type& __obj)
  236. { return _M_ht.insert_equal(__obj); }
  237. #if defined (_STLP_MEMBER_TEMPLATES)
  238. template <class _InputIterator>
  239. void insert(_InputIterator __f, _InputIterator __l)
  240. #else
  241. void insert(const value_type* __f, const value_type* __l)
  242. { _M_ht.insert_equal(__f,__l); }
  243. void insert(const_iterator __f, const_iterator __l)
  244. #endif /*_STLP_MEMBER_TEMPLATES */
  245. { _M_ht.insert_equal(__f, __l); }
  246. _STLP_TEMPLATE_FOR_CONT_EXT
  247. iterator find(const _KT& __key) { return _M_ht.find(__key); }
  248. _STLP_TEMPLATE_FOR_CONT_EXT
  249. const_iterator find(const _KT& __key) const { return _M_ht.find(__key); }
  250. _STLP_TEMPLATE_FOR_CONT_EXT
  251. size_type count(const _KT& __key) const { return _M_ht.count(__key); }
  252. _STLP_TEMPLATE_FOR_CONT_EXT
  253. pair<iterator, iterator> equal_range(const _KT& __key)
  254. { return _M_ht.equal_range(__key); }
  255. _STLP_TEMPLATE_FOR_CONT_EXT
  256. pair<const_iterator, const_iterator> equal_range(const _KT& __key) const
  257. { return _M_ht.equal_range(__key); }
  258. size_type erase(const key_type& __key) {return _M_ht.erase(__key); }
  259. void erase(const_iterator __it) { _M_ht.erase(__it); }
  260. void erase(const_iterator __f, const_iterator __l) { _M_ht.erase(__f, __l); }
  261. void clear() { _M_ht.clear(); }
  262. size_type bucket_count() const { return _M_ht.bucket_count(); }
  263. size_type max_bucket_count() const { return _M_ht.max_bucket_count(); }
  264. size_type bucket_size(size_type __n) const { return _M_ht.elems_in_bucket(__n); }
  265. _STLP_TEMPLATE_FOR_CONT_EXT
  266. size_type bucket(const _KT& __k) const { return _M_ht.bucket(__k); }
  267. local_iterator begin(size_type __n) { return _M_ht.begin(__n); }
  268. local_iterator end(size_type __n) { return _M_ht.end(__n); }
  269. const_local_iterator begin(size_type __n) const { return _M_ht.begin(__n); }
  270. const_local_iterator end(size_type __n) const { return _M_ht.end(__n); }
  271. float load_factor() const { return _M_ht.load_factor(); }
  272. float max_load_factor() const { return _M_ht.max_load_factor(); }
  273. void max_load_factor(float __val) { _M_ht.max_load_factor(__val); }
  274. void rehash(size_type __hint) { _M_ht.rehash(__hint); }
  275. };
  276. #define _STLP_TEMPLATE_HEADER template <class _Value, class _HashFcn, class _EqualKey, class _Alloc>
  277. #define _STLP_TEMPLATE_CONTAINER unordered_set<_Value,_HashFcn,_EqualKey,_Alloc>
  278. #include <stl/_relops_hash_cont.h>
  279. #undef _STLP_TEMPLATE_CONTAINER
  280. #define _STLP_TEMPLATE_CONTAINER unordered_multiset<_Value,_HashFcn,_EqualKey,_Alloc>
  281. #include <stl/_relops_hash_cont.h>
  282. #undef _STLP_TEMPLATE_CONTAINER
  283. #undef _STLP_TEMPLATE_HEADER
  284. _STLP_END_NAMESPACE
  285. // Specialization of insert_iterator so that it will work for unordered_set
  286. // and unordered_multiset.
  287. #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
  288. # if !defined (_STLP_NO_MOVE_SEMANTIC)
  289. template <class _Value, class _HashFcn, class _EqualKey, class _Alloc>
  290. struct __move_traits<_STLP_TR1 unordered_set<_Value, _HashFcn, _EqualKey, _Alloc> > :
  291. _STLP_PRIV __move_traits_aux<typename _STLP_TR1 unordered_set<_Value, _HashFcn, _EqualKey, _Alloc>::_Ht>
  292. {};
  293. template <class _Value, class _HashFcn, class _EqualKey, class _Alloc>
  294. struct __move_traits<_STLP_TR1 unordered_multiset<_Value, _HashFcn, _EqualKey, _Alloc> > :
  295. _STLP_PRIV __move_traits_aux<typename _STLP_TR1 unordered_multiset<_Value, _HashFcn, _EqualKey, _Alloc>::_Ht>
  296. {};
  297. # endif
  298. template <class _Value, class _HashFcn, class _EqualKey, class _Alloc>
  299. class insert_iterator<_STLP_TR1 unordered_set<_Value, _HashFcn, _EqualKey, _Alloc> > {
  300. protected:
  301. typedef _STLP_TR1 unordered_set<_Value, _HashFcn, _EqualKey, _Alloc> _Container;
  302. _Container* container;
  303. public:
  304. typedef _Container container_type;
  305. typedef output_iterator_tag iterator_category;
  306. typedef void value_type;
  307. typedef void difference_type;
  308. typedef void pointer;
  309. typedef void reference;
  310. insert_iterator(_Container& __x) : container(&__x) {}
  311. insert_iterator(_Container& __x, typename _Container::iterator)
  312. : container(&__x) {}
  313. insert_iterator<_Container>&
  314. operator=(const typename _Container::value_type& __val) {
  315. container->insert(__val);
  316. return *this;
  317. }
  318. insert_iterator<_Container>& operator*() { return *this; }
  319. insert_iterator<_Container>& operator++() { return *this; }
  320. insert_iterator<_Container>& operator++(int) { return *this; }
  321. };
  322. template <class _Value, class _HashFcn, class _EqualKey, class _Alloc>
  323. class insert_iterator<_STLP_TR1 unordered_multiset<_Value, _HashFcn, _EqualKey, _Alloc> > {
  324. protected:
  325. typedef _STLP_TR1 unordered_multiset<_Value, _HashFcn, _EqualKey, _Alloc> _Container;
  326. _Container* container;
  327. typename _Container::iterator iter;
  328. public:
  329. typedef _Container container_type;
  330. typedef output_iterator_tag iterator_category;
  331. typedef void value_type;
  332. typedef void difference_type;
  333. typedef void pointer;
  334. typedef void reference;
  335. insert_iterator(_Container& __x) : container(&__x) {}
  336. insert_iterator(_Container& __x, typename _Container::iterator)
  337. : container(&__x) {}
  338. insert_iterator<_Container>&
  339. operator=(const typename _Container::value_type& __val) {
  340. container->insert(__val);
  341. return *this;
  342. }
  343. insert_iterator<_Container>& operator*() { return *this; }
  344. insert_iterator<_Container>& operator++() { return *this; }
  345. insert_iterator<_Container>& operator++(int) { return *this; }
  346. };
  347. #endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
  348. _STLP_END_NAMESPACE
  349. #endif /* _STLP_INTERNAL_UNORDERED_SET_H */
  350. // Local Variables:
  351. // mode:C++
  352. // End: