concurrent_set.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. Copyright (c) 2019-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. #ifndef __TBB_concurrent_set_H
  14. #define __TBB_concurrent_set_H
  15. #define __TBB_concurrent_set_H_include_area
  16. #include "internal/_warning_suppress_enable_notice.h"
  17. #if !TBB_PREVIEW_CONCURRENT_ORDERED_CONTAINERS
  18. #error Set TBB_PREVIEW_CONCURRENT_ORDERED_CONTAINERS to include concurrent_set.h
  19. #endif
  20. #include "tbb/tbb_config.h"
  21. // concurrent_set requires C++11 support
  22. #if __TBB_CONCURRENT_ORDERED_CONTAINERS_PRESENT
  23. #include "internal/_concurrent_skip_list_impl.h"
  24. namespace tbb {
  25. namespace interface10 {
  26. // TODO: test this class
  27. template<typename Key, typename KeyCompare, typename RandomGenerator, size_t MAX_LEVELS, typename Allocator, bool AllowMultimapping>
  28. class set_traits {
  29. public:
  30. static constexpr size_t MAX_LEVEL = MAX_LEVELS;
  31. using random_level_generator_type = RandomGenerator;
  32. using key_type = Key;
  33. using value_type = key_type;
  34. using compare_type = KeyCompare;
  35. using value_compare = compare_type;
  36. using reference = value_type & ;
  37. using const_reference = const value_type&;
  38. using allocator_type = Allocator;
  39. using mutex_type = tbb::spin_mutex;
  40. using node_type = tbb::internal::node_handle<key_type, value_type, internal::skip_list_node<value_type, mutex_type>, allocator_type>;
  41. static const bool allow_multimapping = AllowMultimapping;
  42. static const key_type& get_key(const_reference val) {
  43. return val;
  44. }
  45. static value_compare value_comp(compare_type comp) { return comp; }
  46. };
  47. template <typename Key, typename Comp, typename Allocator>
  48. class concurrent_multiset;
  49. template <typename Key, typename Comp = std::less<Key>, typename Allocator = tbb_allocator<Key>>
  50. class concurrent_set
  51. : public internal::concurrent_skip_list<set_traits<Key, Comp, internal::concurrent_geometric_level_generator<64>, 64, Allocator, false>> {
  52. using traits_type = set_traits<Key, Comp, internal::concurrent_geometric_level_generator<64>, 64, Allocator, false>;
  53. using base_type = internal::concurrent_skip_list<traits_type>;
  54. #if __TBB_EXTRA_DEBUG
  55. public:
  56. #endif
  57. using base_type::allow_multimapping;
  58. public:
  59. using key_type = Key;
  60. using value_type = typename traits_type::value_type;
  61. using size_type = typename base_type::size_type;
  62. using difference_type = typename base_type::difference_type;
  63. using key_compare = Comp;
  64. using value_compare = typename base_type::value_compare;
  65. using allocator_type = Allocator;
  66. using reference = typename base_type::reference;
  67. using const_reference = typename base_type::const_reference;
  68. using pointer = typename base_type::pointer;
  69. using const_pointer = typename base_type::pointer;
  70. using iterator = typename base_type::iterator;
  71. using const_iterator = typename base_type::const_iterator;
  72. using reverse_iterator = typename base_type::reverse_iterator;
  73. using const_reverse_iterator = typename base_type::const_reverse_iterator;
  74. using node_type = typename base_type::node_type;
  75. using base_type::insert;
  76. concurrent_set() = default;
  77. explicit concurrent_set(const key_compare& comp, const allocator_type& alloc = allocator_type()) : base_type(comp, alloc) {}
  78. explicit concurrent_set(const allocator_type& alloc) : base_type(key_compare(), alloc) {}
  79. template< class InputIt >
  80. concurrent_set(InputIt first, InputIt last, const key_compare& comp = Comp(), const allocator_type& alloc = allocator_type())
  81. : base_type(first, last, comp, alloc) {}
  82. template< class InputIt >
  83. concurrent_set(InputIt first, InputIt last, const allocator_type& alloc) : base_type(first, last, key_compare(), alloc) {}
  84. /** Copy constructor */
  85. concurrent_set(const concurrent_set&) = default;
  86. concurrent_set(const concurrent_set& other, const allocator_type& alloc) : base_type(other, alloc) {}
  87. concurrent_set(concurrent_set&&) = default;
  88. concurrent_set(concurrent_set&& other, const allocator_type& alloc) : base_type(std::move(other), alloc) {}
  89. concurrent_set(std::initializer_list<value_type> init, const key_compare& comp = Comp(), const allocator_type& alloc = allocator_type())
  90. : base_type(comp, alloc) {
  91. insert(init);
  92. }
  93. concurrent_set(std::initializer_list<value_type> init, const allocator_type& alloc)
  94. : base_type(key_compare(), alloc) {
  95. insert(init);
  96. }
  97. concurrent_set& operator=(const concurrent_set& other) {
  98. return static_cast<concurrent_set&>(base_type::operator=(other));
  99. }
  100. concurrent_set& operator=(concurrent_set&& other) {
  101. return static_cast<concurrent_set&>(base_type::operator=(std::move(other)));
  102. }
  103. template<typename C2>
  104. void merge(concurrent_set<key_type, C2, Allocator>& source) {
  105. this->internal_merge(source);
  106. }
  107. template<typename C2>
  108. void merge(concurrent_set<key_type, C2, Allocator>&& source) {
  109. this->internal_merge(std::move(source));
  110. }
  111. template<typename C2>
  112. void merge(concurrent_multiset<key_type, C2, Allocator>& source) {
  113. this->internal_merge(source);
  114. }
  115. template<typename C2>
  116. void merge(concurrent_multiset<key_type, C2, Allocator>&& source) {
  117. this->internal_merge(std::move(source));
  118. }
  119. }; // class concurrent_set
  120. #if __TBB_CPP17_DEDUCTION_GUIDES_PRESENT
  121. namespace internal {
  122. using namespace tbb::internal;
  123. template<template<typename...> typename Set, typename Key, typename... Args>
  124. using c_set_t = Set<Key,
  125. std::conditional_t< (sizeof...(Args) > 0) && !is_allocator_v<pack_element_t<0, Args...> >,
  126. pack_element_t<0, Args...>, std::less<Key> >,
  127. std::conditional_t< (sizeof...(Args) > 0) && is_allocator_v<pack_element_t<sizeof...(Args)-1, Args...> >,
  128. pack_element_t<sizeof...(Args)-1, Args...>, tbb_allocator<Key> > >;
  129. } // namespace internal
  130. template<typename It, typename... Args>
  131. concurrent_set(It, It, Args...)
  132. -> internal::c_set_t<concurrent_set, internal::iterator_value_t<It>, Args...>;
  133. template<typename Key, typename... Args>
  134. concurrent_set(std::initializer_list<Key>, Args...)
  135. -> internal::c_set_t<concurrent_set, Key, Args...>;
  136. #endif // __TBB_CPP17_DEDUCTION_GUIDES_PRESENT
  137. template <typename Key, typename Comp = std::less<Key>, typename Allocator = tbb_allocator<Key>>
  138. class concurrent_multiset
  139. : public internal::concurrent_skip_list<set_traits<Key, Comp, internal::concurrent_geometric_level_generator<64>, 64, Allocator, true>> {
  140. using traits_type = set_traits<Key, Comp, internal::concurrent_geometric_level_generator<64>, 64, Allocator, true>;
  141. using base_type = internal::concurrent_skip_list<traits_type>;
  142. #if __TBB_EXTRA_DEBUG
  143. public:
  144. #endif
  145. using base_type::allow_multimapping;
  146. public:
  147. using key_type = Key;
  148. using value_type = typename traits_type::value_type;
  149. using size_type = typename base_type::size_type;
  150. using difference_type = typename base_type::difference_type;
  151. using key_compare = Comp;
  152. using value_compare = typename base_type::value_compare;
  153. using allocator_type = Allocator;
  154. using reference = typename base_type::reference;
  155. using const_reference = typename base_type::const_reference;
  156. using pointer = typename base_type::pointer;
  157. using const_pointer = typename base_type::pointer;
  158. using iterator = typename base_type::iterator;
  159. using const_iterator = typename base_type::const_iterator;
  160. using reverse_iterator = typename base_type::reverse_iterator;
  161. using const_reverse_iterator = typename base_type::const_reverse_iterator;
  162. using node_type = typename base_type::node_type;
  163. using base_type::insert;
  164. concurrent_multiset() = default;
  165. explicit concurrent_multiset(const key_compare& comp, const allocator_type& alloc = allocator_type()) : base_type(comp, alloc) {}
  166. explicit concurrent_multiset(const allocator_type& alloc) : base_type(key_compare(), alloc) {}
  167. template< class InputIt >
  168. concurrent_multiset(InputIt first, InputIt last, const key_compare& comp = Comp(), const allocator_type& alloc = allocator_type())
  169. : base_type(comp, alloc) {
  170. insert(first, last);
  171. }
  172. template< class InputIt >
  173. concurrent_multiset(InputIt first, InputIt last, const allocator_type& alloc) : base_type(key_compare(), alloc) {
  174. insert(first, last);
  175. }
  176. /** Copy constructor */
  177. concurrent_multiset(const concurrent_multiset&) = default;
  178. concurrent_multiset(const concurrent_multiset& other, const allocator_type& alloc) : base_type(other, alloc) {}
  179. concurrent_multiset(concurrent_multiset&&) = default;
  180. concurrent_multiset(concurrent_multiset&& other, const allocator_type& alloc) : base_type(std::move(other), alloc) {}
  181. concurrent_multiset(std::initializer_list<value_type> init, const key_compare& comp = Comp(), const allocator_type& alloc = allocator_type())
  182. : base_type(comp, alloc) {
  183. insert(init);
  184. }
  185. concurrent_multiset(std::initializer_list<value_type> init, const allocator_type& alloc)
  186. : base_type(key_compare(), alloc) {
  187. insert(init);
  188. }
  189. concurrent_multiset& operator=(const concurrent_multiset& other) {
  190. return static_cast<concurrent_multiset&>(base_type::operator=(other));
  191. }
  192. concurrent_multiset& operator=(concurrent_multiset&& other) {
  193. return static_cast<concurrent_multiset&>(base_type::operator=(std::move(other)));
  194. }
  195. template<typename C2>
  196. void merge(concurrent_set<key_type, C2, Allocator>& source) {
  197. this->internal_merge(source);
  198. }
  199. template<typename C2>
  200. void merge(concurrent_set<key_type, C2, Allocator>&& source) {
  201. this->internal_merge(std::move(source));
  202. }
  203. template<typename C2>
  204. void merge(concurrent_multiset<key_type, C2, Allocator>& source) {
  205. this->internal_merge(source);
  206. }
  207. template<typename C2>
  208. void merge(concurrent_multiset<key_type, C2, Allocator>&& source) {
  209. this->internal_merge(std::move(source));
  210. }
  211. }; // class concurrent_multiset
  212. #if __TBB_CPP17_DEDUCTION_GUIDES_PRESENT
  213. template<typename It, typename... Args>
  214. concurrent_multiset(It, It, Args...)
  215. -> internal::c_set_t<concurrent_multiset, internal::iterator_value_t<It>, Args...>;
  216. template<typename Key, typename... Args>
  217. concurrent_multiset(std::initializer_list<Key>, Args...)
  218. -> internal::c_set_t<concurrent_multiset, Key, Args...>;
  219. #endif // __TBB_CPP17_DEDUCTION_GUIDES_PRESENT
  220. } // namespace interface10
  221. using interface10::concurrent_set;
  222. using interface10::concurrent_multiset;
  223. } // namespace tbb
  224. #endif // __TBB_CONCURRENT_ORDERED_CONTAINERS_PRESENT
  225. #include "internal/_warning_suppress_disable_notice.h"
  226. #undef __TBB_concurrent_set_H_include_area
  227. #endif // __TBB_concurrent_set_H