concurrent_unordered_set.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. Copyright (c) 2005-2020 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /* Container implementations in this header are based on PPL implementations
  14. provided by Microsoft. */
  15. #ifndef __TBB_concurrent_unordered_set_H
  16. #define __TBB_concurrent_unordered_set_H
  17. #define __TBB_concurrent_unordered_set_H_include_area
  18. #include "internal/_warning_suppress_enable_notice.h"
  19. #include "internal/_concurrent_unordered_impl.h"
  20. namespace tbb
  21. {
  22. namespace interface5 {
  23. // Template class for hash set traits
  24. template<typename Key, typename Hash_compare, typename Allocator, bool Allow_multimapping>
  25. class concurrent_unordered_set_traits
  26. {
  27. protected:
  28. typedef Key value_type;
  29. typedef Key key_type;
  30. typedef Hash_compare hash_compare;
  31. typedef typename tbb::internal::allocator_rebind<Allocator, value_type>::type allocator_type;
  32. #if __TBB_UNORDERED_NODE_HANDLE_PRESENT
  33. typedef tbb::internal::node_handle<key_type, key_type,
  34. typename internal::split_ordered_list<key_type, allocator_type>::node,
  35. allocator_type> node_type;
  36. #endif // __TBB_UNORDERED_NODE_HANDLE_PRESENT
  37. enum { allow_multimapping = Allow_multimapping };
  38. concurrent_unordered_set_traits() : my_hash_compare() {}
  39. concurrent_unordered_set_traits(const hash_compare& hc) : my_hash_compare(hc) {}
  40. static const Key& get_key(const value_type& value) {
  41. return value;
  42. }
  43. hash_compare my_hash_compare; // the comparator predicate for keys
  44. };
  45. template<typename Key, typename Hasher, typename Key_equality, typename Allocator>
  46. class concurrent_unordered_multiset;
  47. template <typename Key, typename Hasher = tbb::tbb_hash<Key>, typename Key_equality = std::equal_to<Key>, typename Allocator = tbb::tbb_allocator<Key> >
  48. class concurrent_unordered_set : public internal::concurrent_unordered_base< concurrent_unordered_set_traits<Key, internal::hash_compare<Key, Hasher, Key_equality>, Allocator, false> >
  49. {
  50. // Base type definitions
  51. typedef internal::hash_compare<Key, Hasher, Key_equality> hash_compare;
  52. typedef concurrent_unordered_set_traits<Key, hash_compare, Allocator, false> traits_type;
  53. typedef internal::concurrent_unordered_base< traits_type > base_type;
  54. #if __TBB_EXTRA_DEBUG
  55. public:
  56. #endif
  57. using traits_type::allow_multimapping;
  58. public:
  59. using base_type::insert;
  60. // Type definitions
  61. typedef Key key_type;
  62. typedef typename base_type::value_type value_type;
  63. typedef Key mapped_type;
  64. typedef Hasher hasher;
  65. typedef Key_equality key_equal;
  66. typedef hash_compare key_compare;
  67. typedef typename base_type::allocator_type allocator_type;
  68. typedef typename base_type::pointer pointer;
  69. typedef typename base_type::const_pointer const_pointer;
  70. typedef typename base_type::reference reference;
  71. typedef typename base_type::const_reference const_reference;
  72. typedef typename base_type::size_type size_type;
  73. typedef typename base_type::difference_type difference_type;
  74. typedef typename base_type::iterator iterator;
  75. typedef typename base_type::const_iterator const_iterator;
  76. typedef typename base_type::iterator local_iterator;
  77. typedef typename base_type::const_iterator const_local_iterator;
  78. #if __TBB_UNORDERED_NODE_HANDLE_PRESENT
  79. typedef typename base_type::node_type node_type;
  80. #endif /*__TBB_UNORDERED_NODE_HANDLE_PRESENT*/
  81. // Construction/destruction/copying
  82. explicit concurrent_unordered_set(size_type n_of_buckets = base_type::initial_bucket_number, const hasher& a_hasher = hasher(),
  83. const key_equal& a_keyeq = key_equal(), const allocator_type& a = allocator_type())
  84. : base_type(n_of_buckets, key_compare(a_hasher, a_keyeq), a)
  85. {}
  86. concurrent_unordered_set(size_type n_of_buckets, const allocator_type& a)
  87. : base_type(n_of_buckets, key_compare(hasher(), key_equal()), a)
  88. {}
  89. concurrent_unordered_set(size_type n_of_buckets, const hasher& a_hasher, const allocator_type& a)
  90. : base_type(n_of_buckets, key_compare(a_hasher, key_equal()), a)
  91. {}
  92. explicit concurrent_unordered_set(const Allocator& a) : base_type(base_type::initial_bucket_number, key_compare(), a)
  93. {}
  94. template <typename Iterator>
  95. concurrent_unordered_set(Iterator first, Iterator last, size_type n_of_buckets = base_type::initial_bucket_number,
  96. const hasher& a_hasher = hasher(), const key_equal& a_keyeq = key_equal(), const allocator_type& a = allocator_type())
  97. : base_type(n_of_buckets, key_compare(a_hasher, a_keyeq), a)
  98. {
  99. insert(first, last);
  100. }
  101. template <typename Iterator>
  102. concurrent_unordered_set(Iterator first, Iterator last, size_type n_of_buckets, const allocator_type& a)
  103. : base_type(n_of_buckets, key_compare(hasher(), key_equal()), a)
  104. {
  105. insert(first, last);
  106. }
  107. template <typename Iterator>
  108. concurrent_unordered_set(Iterator first, Iterator last, size_type n_of_buckets, const hasher& a_hasher, const allocator_type& a)
  109. : base_type(n_of_buckets, key_compare(a_hasher, key_equal()), a)
  110. {
  111. insert(first, last);
  112. }
  113. #if __TBB_INITIALIZER_LISTS_PRESENT
  114. //! Constructor from initializer_list
  115. concurrent_unordered_set(std::initializer_list<value_type> il, size_type n_of_buckets = base_type::initial_bucket_number, const hasher& a_hasher = hasher(),
  116. const key_equal& a_keyeq = key_equal(), const allocator_type& a = allocator_type())
  117. : base_type(n_of_buckets, key_compare(a_hasher, a_keyeq), a)
  118. {
  119. insert(il.begin(),il.end());
  120. }
  121. concurrent_unordered_set(std::initializer_list<value_type> il, size_type n_of_buckets, const allocator_type& a)
  122. : base_type(n_of_buckets, key_compare(hasher(), key_equal()), a)
  123. {
  124. insert(il.begin(), il.end());
  125. }
  126. concurrent_unordered_set(std::initializer_list<value_type> il, size_type n_of_buckets, const hasher& a_hasher, const allocator_type& a)
  127. : base_type(n_of_buckets, key_compare(a_hasher, key_equal()), a)
  128. {
  129. insert(il.begin(), il.end());
  130. }
  131. #endif //# __TBB_INITIALIZER_LISTS_PRESENT
  132. #if __TBB_CPP11_RVALUE_REF_PRESENT && !__TBB_IMPLICIT_MOVE_PRESENT
  133. concurrent_unordered_set(const concurrent_unordered_set& table)
  134. : base_type(table)
  135. {}
  136. concurrent_unordered_set& operator=(const concurrent_unordered_set& table)
  137. {
  138. return static_cast<concurrent_unordered_set&>(base_type::operator=(table));
  139. }
  140. concurrent_unordered_set(concurrent_unordered_set&& table)
  141. : base_type(std::move(table))
  142. {}
  143. concurrent_unordered_set& operator=(concurrent_unordered_set&& table)
  144. {
  145. return static_cast<concurrent_unordered_set&>(base_type::operator=(std::move(table)));
  146. }
  147. #endif //__TBB_CPP11_RVALUE_REF_PRESENT && !__TBB_IMPLICIT_MOVE_PRESENT
  148. #if __TBB_CPP11_RVALUE_REF_PRESENT
  149. concurrent_unordered_set(concurrent_unordered_set&& table, const Allocator& a)
  150. : base_type(std::move(table), a)
  151. {}
  152. #endif /*__TBB_CPP11_RVALUE_REF_PRESENT*/
  153. #if __TBB_UNORDERED_NODE_HANDLE_PRESENT
  154. template<typename Hash, typename Equality>
  155. void merge(concurrent_unordered_set<Key, Hash, Equality, Allocator>& source)
  156. { this->internal_merge(source); }
  157. template<typename Hash, typename Equality>
  158. void merge(concurrent_unordered_set<Key, Hash, Equality, Allocator>&& source)
  159. { this->internal_merge(source); }
  160. template<typename Hash, typename Equality>
  161. void merge(concurrent_unordered_multiset<Key, Hash, Equality, Allocator>& source)
  162. { this->internal_merge(source); }
  163. template<typename Hash, typename Equality>
  164. void merge(concurrent_unordered_multiset<Key, Hash, Equality, Allocator>&& source)
  165. { this->internal_merge(source); }
  166. #endif //__TBB_UNORDERED_NODE_HANDLE_PRESENT
  167. concurrent_unordered_set(const concurrent_unordered_set& table, const Allocator& a)
  168. : base_type(table, a)
  169. {}
  170. };
  171. #if __TBB_CPP17_DEDUCTION_GUIDES_PRESENT
  172. namespace internal {
  173. using namespace tbb::internal;
  174. template <template<typename...> typename Set, typename T, typename... Args>
  175. using cu_set_t = Set <
  176. T,
  177. std::conditional_t< (sizeof...(Args)>0) && !is_allocator_v< pack_element_t<0, Args...> >,
  178. pack_element_t<0, Args...>, tbb_hash<T> >,
  179. std::conditional_t< (sizeof...(Args)>1) && !is_allocator_v< pack_element_t<1, Args...> >,
  180. pack_element_t<1, Args...>, std::equal_to<T> >,
  181. std::conditional_t< (sizeof...(Args)>0) && is_allocator_v< pack_element_t<sizeof...(Args)-1, Args...> >,
  182. pack_element_t<sizeof...(Args)-1, Args...>, tbb_allocator<T> >
  183. >;
  184. }
  185. // Deduction guide for the constructor from two iterators
  186. template<typename I>
  187. concurrent_unordered_set(I, I)
  188. -> internal::cu_set_t<concurrent_unordered_set, internal::iterator_value_t<I>>;
  189. // Deduction guide for the constructor from two iterators and hasher/equality/allocator
  190. template<typename I, typename... Args>
  191. concurrent_unordered_set(I, I, size_t, Args...)
  192. -> internal::cu_set_t<concurrent_unordered_set, internal::iterator_value_t<I>, Args...>;
  193. // Deduction guide for the constructor from an initializer_list
  194. template<typename T>
  195. concurrent_unordered_set(std::initializer_list<T>)
  196. -> internal::cu_set_t<concurrent_unordered_set, T>;
  197. // Deduction guide for the constructor from an initializer_list and hasher/equality/allocator
  198. template<typename T, typename... Args>
  199. concurrent_unordered_set(std::initializer_list<T>, size_t, Args...)
  200. -> internal::cu_set_t<concurrent_unordered_set, T, Args...>;
  201. #endif /*__TBB_CPP17_DEDUCTION_GUIDES_PRESENT */
  202. template <typename Key, typename Hasher = tbb::tbb_hash<Key>, typename Key_equality = std::equal_to<Key>,
  203. typename Allocator = tbb::tbb_allocator<Key> >
  204. class concurrent_unordered_multiset :
  205. public internal::concurrent_unordered_base< concurrent_unordered_set_traits<Key,
  206. internal::hash_compare<Key, Hasher, Key_equality>, Allocator, true> >
  207. {
  208. // Base type definitions
  209. typedef internal::hash_compare<Key, Hasher, Key_equality> hash_compare;
  210. typedef concurrent_unordered_set_traits<Key, hash_compare, Allocator, true> traits_type;
  211. typedef internal::concurrent_unordered_base< traits_type > base_type;
  212. #if __TBB_EXTRA_DEBUG
  213. public:
  214. #endif
  215. using traits_type::allow_multimapping;
  216. public:
  217. using base_type::insert;
  218. // Type definitions
  219. typedef Key key_type;
  220. typedef typename base_type::value_type value_type;
  221. typedef Key mapped_type;
  222. typedef Hasher hasher;
  223. typedef Key_equality key_equal;
  224. typedef hash_compare key_compare;
  225. typedef typename base_type::allocator_type allocator_type;
  226. typedef typename base_type::pointer pointer;
  227. typedef typename base_type::const_pointer const_pointer;
  228. typedef typename base_type::reference reference;
  229. typedef typename base_type::const_reference const_reference;
  230. typedef typename base_type::size_type size_type;
  231. typedef typename base_type::difference_type difference_type;
  232. typedef typename base_type::iterator iterator;
  233. typedef typename base_type::const_iterator const_iterator;
  234. typedef typename base_type::iterator local_iterator;
  235. typedef typename base_type::const_iterator const_local_iterator;
  236. #if __TBB_UNORDERED_NODE_HANDLE_PRESENT
  237. typedef typename base_type::node_type node_type;
  238. #endif // __TBB_UNORDERED_NODE_HANDLE_PRESENT
  239. // Construction/destruction/copying
  240. explicit concurrent_unordered_multiset(size_type n_of_buckets = base_type::initial_bucket_number,
  241. const hasher& a_hasher = hasher(), const key_equal& a_keyeq = key_equal(),
  242. const allocator_type& a = allocator_type())
  243. : base_type(n_of_buckets, key_compare(a_hasher, a_keyeq), a)
  244. {}
  245. concurrent_unordered_multiset(size_type n_of_buckets, const allocator_type& a)
  246. : base_type(n_of_buckets, key_compare(hasher(), key_equal()), a)
  247. {}
  248. concurrent_unordered_multiset(size_type n_of_buckets, const hasher& a_hasher,
  249. const allocator_type& a)
  250. : base_type(n_of_buckets, key_compare(a_hasher, key_equal()), a)
  251. {}
  252. explicit concurrent_unordered_multiset(const Allocator& a) : base_type(base_type::initial_bucket_number, key_compare(), a)
  253. {}
  254. template <typename Iterator>
  255. concurrent_unordered_multiset(Iterator first, Iterator last, size_type n_of_buckets = base_type::initial_bucket_number,
  256. const hasher& a_hasher = hasher(), const key_equal& a_keyeq = key_equal(),
  257. const allocator_type& a = allocator_type())
  258. : base_type(n_of_buckets, key_compare(a_hasher, a_keyeq), a)
  259. {
  260. insert(first, last);
  261. }
  262. template <typename Iterator>
  263. concurrent_unordered_multiset(Iterator first, Iterator last, size_type n_of_buckets, const allocator_type& a)
  264. : base_type(n_of_buckets, key_compare(hasher(), key_equal()), a)
  265. {
  266. insert(first, last);
  267. }
  268. template <typename Iterator>
  269. concurrent_unordered_multiset(Iterator first, Iterator last, size_type n_of_buckets, const hasher& a_hasher,
  270. const allocator_type& a)
  271. : base_type(n_of_buckets, key_compare(a_hasher, key_equal()), a)
  272. {
  273. insert(first, last);
  274. }
  275. #if __TBB_INITIALIZER_LISTS_PRESENT
  276. //! Constructor from initializer_list
  277. concurrent_unordered_multiset(std::initializer_list<value_type> il, size_type n_of_buckets = base_type::initial_bucket_number,
  278. const hasher& a_hasher = hasher(), const key_equal& a_keyeq = key_equal(), const allocator_type& a = allocator_type())
  279. : base_type(n_of_buckets, key_compare(a_hasher, a_keyeq), a)
  280. {
  281. insert(il.begin(),il.end());
  282. }
  283. concurrent_unordered_multiset(std::initializer_list<value_type> il, size_type n_of_buckets, const allocator_type& a)
  284. : base_type(n_of_buckets, key_compare(hasher(), key_equal()), a)
  285. {
  286. insert(il.begin(), il.end());
  287. }
  288. concurrent_unordered_multiset(std::initializer_list<value_type> il, size_type n_of_buckets, const hasher& a_hasher,
  289. const allocator_type& a)
  290. : base_type(n_of_buckets, key_compare(a_hasher, key_equal()), a)
  291. {
  292. insert(il.begin(), il.end());
  293. }
  294. #endif //# __TBB_INITIALIZER_LISTS_PRESENT
  295. #if __TBB_CPP11_RVALUE_REF_PRESENT && !__TBB_IMPLICIT_MOVE_PRESENT
  296. concurrent_unordered_multiset(const concurrent_unordered_multiset& table)
  297. : base_type(table)
  298. {}
  299. concurrent_unordered_multiset& operator=(const concurrent_unordered_multiset& table)
  300. {
  301. return static_cast<concurrent_unordered_multiset&>(base_type::operator=(table));
  302. }
  303. concurrent_unordered_multiset(concurrent_unordered_multiset&& table)
  304. : base_type(std::move(table))
  305. {}
  306. concurrent_unordered_multiset& operator=(concurrent_unordered_multiset&& table)
  307. {
  308. return static_cast<concurrent_unordered_multiset&>(base_type::operator=(std::move(table)));
  309. }
  310. #endif //__TBB_CPP11_RVALUE_REF_PRESENT && !__TBB_IMPLICIT_MOVE_PRESENT
  311. #if __TBB_CPP11_RVALUE_REF_PRESENT
  312. concurrent_unordered_multiset(concurrent_unordered_multiset&& table, const Allocator& a)
  313. : base_type(std::move(table), a)
  314. {
  315. }
  316. #endif /*__TBB_CPP11_RVALUE_REF_PRESENT*/
  317. #if __TBB_UNORDERED_NODE_HANDLE_PRESENT
  318. template<typename Hash, typename Equality>
  319. void merge(concurrent_unordered_set<Key, Hash, Equality, Allocator>& source)
  320. { this->internal_merge(source); }
  321. template<typename Hash, typename Equality>
  322. void merge(concurrent_unordered_set<Key, Hash, Equality, Allocator>&& source)
  323. { this->internal_merge(source); }
  324. template<typename Hash, typename Equality>
  325. void merge(concurrent_unordered_multiset<Key, Hash, Equality, Allocator>& source)
  326. { this->internal_merge(source); }
  327. template<typename Hash, typename Equality>
  328. void merge(concurrent_unordered_multiset<Key, Hash, Equality, Allocator>&& source)
  329. { this->internal_merge(source); }
  330. #endif //__TBB_UNORDERED_NODE_HANDLE_PRESENT
  331. concurrent_unordered_multiset(const concurrent_unordered_multiset& table, const Allocator& a)
  332. : base_type(table, a)
  333. {}
  334. };
  335. #if __TBB_CPP17_DEDUCTION_GUIDES_PRESENT
  336. // Deduction guide for the constructor from two iterators
  337. template<typename I>
  338. concurrent_unordered_multiset(I, I)
  339. -> internal::cu_set_t<concurrent_unordered_multiset, internal::iterator_value_t<I>>;
  340. // Deduction guide for the constructor from two iterators and hasher/equality/allocator
  341. template<typename I, typename... Args>
  342. concurrent_unordered_multiset(I, I, size_t, Args...)
  343. -> internal::cu_set_t<concurrent_unordered_multiset, internal::iterator_value_t<I>, Args...>;
  344. // Deduction guide for the constructor from an initializer_list
  345. template<typename T>
  346. concurrent_unordered_multiset(std::initializer_list<T>)
  347. -> internal::cu_set_t<concurrent_unordered_multiset, T>;
  348. // Deduction guide for the constructor from an initializer_list and hasher/equality/allocator
  349. template<typename T, typename... Args>
  350. concurrent_unordered_multiset(std::initializer_list<T>, size_t, Args...)
  351. -> internal::cu_set_t<concurrent_unordered_multiset, T, Args...>;
  352. #endif /* __TBB_CPP17_DEDUCTION_GUIDES_PRESENT */
  353. } // namespace interface5
  354. using interface5::concurrent_unordered_set;
  355. using interface5::concurrent_unordered_multiset;
  356. } // namespace tbb
  357. #include "internal/_warning_suppress_disable_notice.h"
  358. #undef __TBB_concurrent_unordered_set_H_include_area
  359. #endif// __TBB_concurrent_unordered_set_H