concurrent_unordered_set.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. Copyright 2005-2013 Intel Corporation. All Rights Reserved.
  3. This file is part of Threading Building Blocks.
  4. Threading Building Blocks is free software; you can redistribute it
  5. and/or modify it under the terms of the GNU General Public License
  6. version 2 as published by the Free Software Foundation.
  7. Threading Building Blocks is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  9. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with Threading Building Blocks; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. As a special exception, you may use this file as part of a free software
  15. library without restriction. Specifically, if other files instantiate
  16. templates or use macros or inline functions from this file, or you compile
  17. this file and link it with other files to produce an executable, this
  18. file does not by itself cause the resulting executable to be covered by
  19. the GNU General Public License. This exception does not however
  20. invalidate any other reasons why the executable file might be covered by
  21. the GNU General Public License.
  22. */
  23. /* Container implementations in this header are based on PPL implementations
  24. provided by Microsoft. */
  25. #ifndef __TBB_concurrent_unordered_set_H
  26. #define __TBB_concurrent_unordered_set_H
  27. #include "internal/_concurrent_unordered_impl.h"
  28. namespace tbb
  29. {
  30. namespace interface5 {
  31. // Template class for hash set traits
  32. template<typename Key, typename Hash_compare, typename Allocator, bool Allow_multimapping>
  33. class concurrent_unordered_set_traits
  34. {
  35. protected:
  36. typedef Key value_type;
  37. typedef Key key_type;
  38. typedef Hash_compare hash_compare;
  39. typedef typename Allocator::template rebind<value_type>::other allocator_type;
  40. enum { allow_multimapping = Allow_multimapping };
  41. concurrent_unordered_set_traits() : my_hash_compare() {}
  42. concurrent_unordered_set_traits(const hash_compare& hc) : my_hash_compare(hc) {}
  43. typedef hash_compare value_compare;
  44. static const Key& get_key(const value_type& value) {
  45. return value;
  46. }
  47. hash_compare my_hash_compare; // the comparator predicate for keys
  48. };
  49. template <typename Key, typename Hasher = tbb::tbb_hash<Key>, typename Key_equality = std::equal_to<Key>, typename Allocator = tbb::tbb_allocator<Key> >
  50. class concurrent_unordered_set : public internal::concurrent_unordered_base< concurrent_unordered_set_traits<Key, internal::hash_compare<Key, Hasher, Key_equality>, Allocator, false> >
  51. {
  52. // Base type definitions
  53. typedef internal::hash_compare<Key, Hasher, Key_equality> hash_compare;
  54. typedef internal::concurrent_unordered_base< concurrent_unordered_set_traits<Key, hash_compare, Allocator, false> > base_type;
  55. typedef concurrent_unordered_set_traits<Key, internal::hash_compare<Key, Hasher, Key_equality>, Allocator, false> traits_type;
  56. using traits_type::my_hash_compare;
  57. #if __TBB_EXTRA_DEBUG
  58. public:
  59. #endif
  60. using traits_type::allow_multimapping;
  61. public:
  62. using base_type::end;
  63. using base_type::find;
  64. using base_type::insert;
  65. // Type definitions
  66. typedef Key key_type;
  67. typedef typename base_type::value_type value_type;
  68. typedef Key mapped_type;
  69. typedef Hasher hasher;
  70. typedef Key_equality key_equal;
  71. typedef hash_compare key_compare;
  72. typedef typename base_type::allocator_type allocator_type;
  73. typedef typename base_type::pointer pointer;
  74. typedef typename base_type::const_pointer const_pointer;
  75. typedef typename base_type::reference reference;
  76. typedef typename base_type::const_reference const_reference;
  77. typedef typename base_type::size_type size_type;
  78. typedef typename base_type::difference_type difference_type;
  79. typedef typename base_type::iterator iterator;
  80. typedef typename base_type::const_iterator const_iterator;
  81. typedef typename base_type::iterator local_iterator;
  82. typedef typename base_type::const_iterator const_local_iterator;
  83. // Construction/destruction/copying
  84. explicit concurrent_unordered_set(size_type n_of_buckets = 8, const hasher& a_hasher = hasher(),
  85. const key_equal& a_keyeq = key_equal(), const allocator_type& a = allocator_type())
  86. : base_type(n_of_buckets, key_compare(a_hasher, a_keyeq), a)
  87. {
  88. }
  89. concurrent_unordered_set(const Allocator& a) : base_type(8, key_compare(), a)
  90. {
  91. }
  92. template <typename Iterator>
  93. concurrent_unordered_set(Iterator first, Iterator last, size_type n_of_buckets = 8, const hasher& a_hasher = hasher(),
  94. const key_equal& a_keyeq = key_equal(), const allocator_type& a = allocator_type())
  95. : base_type(n_of_buckets, key_compare(a_hasher, a_keyeq), a)
  96. {
  97. for (; first != last; ++first)
  98. base_type::insert(*first);
  99. }
  100. concurrent_unordered_set(const concurrent_unordered_set& table) : base_type(table)
  101. {
  102. }
  103. concurrent_unordered_set(const concurrent_unordered_set& table, const Allocator& a)
  104. : base_type(table, a)
  105. {
  106. }
  107. concurrent_unordered_set& operator=(const concurrent_unordered_set& table)
  108. {
  109. base_type::operator=(table);
  110. return (*this);
  111. }
  112. iterator unsafe_erase(const_iterator where)
  113. {
  114. return base_type::unsafe_erase(where);
  115. }
  116. size_type unsafe_erase(const key_type& key)
  117. {
  118. return base_type::unsafe_erase(key);
  119. }
  120. iterator unsafe_erase(const_iterator first, const_iterator last)
  121. {
  122. return base_type::unsafe_erase(first, last);
  123. }
  124. void swap(concurrent_unordered_set& table)
  125. {
  126. base_type::swap(table);
  127. }
  128. // Observers
  129. hasher hash_function() const
  130. {
  131. return my_hash_compare.my_hash_object;
  132. }
  133. key_equal key_eq() const
  134. {
  135. return my_hash_compare.my_key_compare_object;
  136. }
  137. };
  138. template <typename Key, typename Hasher = tbb::tbb_hash<Key>, typename Key_equality = std::equal_to<Key>,
  139. typename Allocator = tbb::tbb_allocator<Key> >
  140. class concurrent_unordered_multiset :
  141. public internal::concurrent_unordered_base< concurrent_unordered_set_traits<Key,
  142. internal::hash_compare<Key, Hasher, Key_equality>, Allocator, true> >
  143. {
  144. public:
  145. // Base type definitions
  146. typedef internal::hash_compare<Key, Hasher, Key_equality> hash_compare;
  147. typedef concurrent_unordered_set_traits<Key, hash_compare, Allocator, true> traits_type;
  148. typedef internal::concurrent_unordered_base< traits_type > base_type;
  149. using traits_type::allow_multimapping;
  150. using traits_type::my_hash_compare;
  151. // Type definitions
  152. typedef Key key_type;
  153. typedef typename base_type::value_type value_type;
  154. typedef Key mapped_type;
  155. typedef Hasher hasher;
  156. typedef Key_equality key_equal;
  157. typedef hash_compare key_compare;
  158. typedef typename base_type::allocator_type allocator_type;
  159. typedef typename base_type::pointer pointer;
  160. typedef typename base_type::const_pointer const_pointer;
  161. typedef typename base_type::reference reference;
  162. typedef typename base_type::const_reference const_reference;
  163. typedef typename base_type::size_type size_type;
  164. typedef typename base_type::difference_type difference_type;
  165. typedef typename base_type::iterator iterator;
  166. typedef typename base_type::const_iterator const_iterator;
  167. typedef typename base_type::iterator local_iterator;
  168. typedef typename base_type::const_iterator const_local_iterator;
  169. // Construction/destruction/copying
  170. explicit concurrent_unordered_multiset(size_type n_of_buckets = 8,
  171. const hasher& _Hasher = hasher(), const key_equal& _Key_equality = key_equal(),
  172. const allocator_type& a = allocator_type())
  173. : base_type(n_of_buckets, key_compare(_Hasher, _Key_equality), a)
  174. {
  175. }
  176. concurrent_unordered_multiset(const Allocator& a) : base_type(8, key_compare(), a)
  177. {
  178. }
  179. template <typename Iterator>
  180. concurrent_unordered_multiset(Iterator first, Iterator last, size_type n_of_buckets = 8,
  181. const hasher& _Hasher = hasher(), const key_equal& _Key_equality = key_equal(),
  182. const allocator_type& a = allocator_type())
  183. : base_type(n_of_buckets, key_compare(_Hasher, _Key_equality), a)
  184. {
  185. for (; first != last; ++first)
  186. {
  187. base_type::insert(*first);
  188. }
  189. }
  190. concurrent_unordered_multiset(const concurrent_unordered_multiset& table) : base_type(table)
  191. {
  192. }
  193. concurrent_unordered_multiset(const concurrent_unordered_multiset& table, const Allocator& a) : base_type(table, a)
  194. {
  195. }
  196. concurrent_unordered_multiset& operator=(const concurrent_unordered_multiset& table)
  197. {
  198. base_type::operator=(table);
  199. return (*this);
  200. }
  201. // Modifiers
  202. std::pair<iterator, bool> insert(const value_type& value)
  203. {
  204. return base_type::insert(value);
  205. }
  206. iterator insert(const_iterator where, const value_type& value)
  207. {
  208. return base_type::insert(where, value);
  209. }
  210. template<class Iterator>
  211. void insert(Iterator first, Iterator last)
  212. {
  213. base_type::insert(first, last);
  214. }
  215. iterator unsafe_erase(const_iterator where)
  216. {
  217. return base_type::unsafe_erase(where);
  218. }
  219. size_type unsafe_erase(const key_type& key)
  220. {
  221. return base_type::unsafe_erase(key);
  222. }
  223. iterator unsafe_erase(const_iterator first, const_iterator last)
  224. {
  225. return base_type::unsafe_erase(first, last);
  226. }
  227. void swap(concurrent_unordered_multiset& table)
  228. {
  229. base_type::swap(table);
  230. }
  231. // Observers
  232. hasher hash_function() const
  233. {
  234. return my_hash_compare.my_hash_object;
  235. }
  236. key_equal key_eq() const
  237. {
  238. return my_hash_compare.my_key_compare_object;
  239. }
  240. };
  241. } // namespace interface5
  242. using interface5::concurrent_unordered_set;
  243. using interface5::concurrent_unordered_multiset;
  244. } // namespace tbb
  245. #endif// __TBB_concurrent_unordered_set_H