xref: /oneTBB/include/oneapi/tbb/concurrent_set.h (revision 0a2b3987)
1 /*
2     Copyright (c) 2019-2021 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #ifndef __TBB_concurrent_set_H
18 #define __TBB_concurrent_set_H
19 
20 #include "detail/_namespace_injection.h"
21 #include "detail/_concurrent_skip_list.h"
22 #include "tbb_allocator.h"
23 #include <functional>
24 #include <utility>
25 
26 namespace tbb {
27 namespace detail {
28 namespace d2 {
29 
30 template<typename Key, typename KeyCompare, typename RandomGenerator, typename Allocator, bool AllowMultimapping>
31 struct set_traits {
32     static constexpr std::size_t max_level = RandomGenerator::max_level;
33     using random_level_generator_type = RandomGenerator;
34     using key_type = Key;
35     using value_type = key_type;
36     using compare_type = KeyCompare;
37     using value_compare = compare_type;
38     using reference = value_type&;
39     using const_reference = const value_type&;
40     using allocator_type = Allocator;
41 
42     static constexpr bool allow_multimapping = AllowMultimapping;
43 
44     static const key_type& get_key(const_reference val) {
45         return val;
46     }
47 
48     static value_compare value_comp(compare_type comp) { return comp; }
49 }; // struct set_traits
50 
51 template <typename Key, typename Compare, typename Allocator>
52 class concurrent_multiset;
53 
54 template <typename Key, typename Compare = std::less<Key>, typename Allocator = tbb::tbb_allocator<Key>>
55 class concurrent_set : public concurrent_skip_list<set_traits<Key, Compare, concurrent_geometric_level_generator<32>, Allocator, false>> {
56     using base_type = concurrent_skip_list<set_traits<Key, Compare, concurrent_geometric_level_generator<32>, Allocator, false>>;
57 public:
58     using key_type = Key;
59     using value_type = typename base_type::value_type;
60     using size_type = typename base_type::size_type;
61     using difference_type = typename base_type::difference_type;
62     using key_compare = Compare;
63     using value_compare = typename base_type::value_compare;
64     using allocator_type = Allocator;
65 
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::const_pointer;
70 
71     using iterator = typename base_type::iterator;
72     using const_iterator = typename base_type::const_iterator;
73 
74     using node_type = typename base_type::node_type;
75 
76     // Include constructors of base_type
77     using base_type::base_type;
78     using base_type::operator=;
79 
80     // Required for implicit deduction guides
81     concurrent_set() = default;
82     concurrent_set( const concurrent_set& ) = default;
83     concurrent_set( const concurrent_set& other, const allocator_type& alloc ) : base_type(other, alloc) {}
84     concurrent_set( concurrent_set&& ) = default;
85     concurrent_set( concurrent_set&& other, const allocator_type& alloc ) : base_type(std::move(other), alloc) {}
86     // Required to respect the rule of 5
87     concurrent_set& operator=( const concurrent_set& ) = default;
88     concurrent_set& operator=( concurrent_set&& ) = default;
89 
90     template<typename OtherCompare>
91     void merge(concurrent_set<key_type, OtherCompare, Allocator>& source) {
92         this->internal_merge(source);
93     }
94 
95     template<typename OtherCompare>
96     void merge(concurrent_set<key_type, OtherCompare, Allocator>&& source) {
97         this->internal_merge(std::move(source));
98     }
99 
100     template<typename OtherCompare>
101     void merge(concurrent_multiset<key_type, OtherCompare, Allocator>& source) {
102         this->internal_merge(source);
103     }
104 
105     template<typename OtherCompare>
106     void merge(concurrent_multiset<key_type, OtherCompare, Allocator>&& source) {
107         this->internal_merge(std::move(source));
108     }
109 }; // class concurrent_set
110 
111 #if __TBB_CPP17_DEDUCTION_GUIDES_PRESENT
112 
113 template <typename It,
114           typename Comp = std::less<iterator_value_t<It>>,
115           typename Alloc = tbb::tbb_allocator<iterator_value_t<It>>,
116           typename = std::enable_if_t<is_input_iterator_v<It>>,
117           typename = std::enable_if_t<is_allocator_v<Alloc>>,
118           typename = std::enable_if_t<!is_allocator_v<Comp>>>
119 concurrent_set( It, It, Comp = Comp(), Alloc = Alloc() )
120 -> concurrent_set<iterator_value_t<It>, Comp, Alloc>;
121 
122 template <typename Key,
123           typename Comp = std::less<Key>,
124           typename Alloc = tbb::tbb_allocator<Key>,
125           typename = std::enable_if_t<is_allocator_v<Alloc>>,
126           typename = std::enable_if_t<!is_allocator_v<Comp>>>
127 concurrent_set( std::initializer_list<Key>, Comp = Comp(), Alloc = Alloc() )
128 -> concurrent_set<Key, Comp, Alloc>;
129 
130 template <typename It, typename Alloc,
131           typename = std::enable_if_t<is_input_iterator_v<It>>,
132           typename = std::enable_if_t<is_allocator_v<Alloc>>>
133 concurrent_set( It, It, Alloc )
134 -> concurrent_set<iterator_value_t<It>,
135                   std::less<iterator_value_t<It>>, Alloc>;
136 
137 template <typename Key, typename Alloc,
138           typename = std::enable_if_t<is_allocator_v<Alloc>>>
139 concurrent_set( std::initializer_list<Key>, Alloc )
140 -> concurrent_set<Key, std::less<Key>, Alloc>;
141 
142 #endif // __TBB_CPP17_DEDUCTION_GUIDES_PRESENT
143 
144 template <typename Key, typename Compare, typename Allocator>
145 void swap( concurrent_set<Key, Compare, Allocator>& lhs,
146            concurrent_set<Key, Compare, Allocator>& rhs )
147 {
148     lhs.swap(rhs);
149 }
150 
151 template <typename Key, typename Compare = std::less<Key>, typename Allocator = tbb::tbb_allocator<Key>>
152 class concurrent_multiset : public concurrent_skip_list<set_traits<Key, Compare, concurrent_geometric_level_generator<32>, Allocator, true>> {
153     using base_type = concurrent_skip_list<set_traits<Key, Compare, concurrent_geometric_level_generator<32>, Allocator, true>>;
154 public:
155     using key_type = Key;
156     using value_type = typename base_type::value_type;
157     using size_type = typename base_type::size_type;
158     using difference_type = typename base_type::difference_type;
159     using key_compare = Compare;
160     using value_compare = typename base_type::value_compare;
161     using allocator_type = Allocator;
162 
163     using reference = typename base_type::reference;
164     using const_reference = typename base_type::const_reference;
165     using pointer = typename base_type::pointer;
166     using const_pointer = typename base_type::const_pointer;
167 
168     using iterator = typename base_type::iterator;
169     using const_iterator = typename base_type::const_iterator;
170 
171     using node_type = typename base_type::node_type;
172 
173     // Include constructors of base_type;
174     using base_type::base_type;
175     using base_type::operator=;
176 
177     // Required for implicit deduction guides
178     concurrent_multiset() = default;
179     concurrent_multiset( const concurrent_multiset& ) = default;
180     concurrent_multiset( const concurrent_multiset& other, const allocator_type& alloc ) : base_type(other, alloc) {}
181     concurrent_multiset( concurrent_multiset&& ) = default;
182     concurrent_multiset( concurrent_multiset&& other, const allocator_type& alloc ) : base_type(std::move(other), alloc) {}
183     // Required to respect the rule of 5
184     concurrent_multiset& operator=( const concurrent_multiset& ) = default;
185     concurrent_multiset& operator=( concurrent_multiset&& ) = default;
186 
187     template<typename OtherCompare>
188     void merge(concurrent_set<key_type, OtherCompare, Allocator>& source) {
189         this->internal_merge(source);
190     }
191 
192     template<typename OtherCompare>
193     void merge(concurrent_set<key_type, OtherCompare, Allocator>&& source) {
194         this->internal_merge(std::move(source));
195     }
196 
197     template<typename OtherCompare>
198     void merge(concurrent_multiset<key_type, OtherCompare, Allocator>& source) {
199         this->internal_merge(source);
200     }
201 
202     template<typename OtherCompare>
203     void merge(concurrent_multiset<key_type, OtherCompare, Allocator>&& source) {
204         this->internal_merge(std::move(source));
205     }
206 }; // class concurrent_multiset
207 
208 #if __TBB_CPP17_DEDUCTION_GUIDES_PRESENT
209 
210 template <typename It,
211           typename Comp = std::less<iterator_value_t<It>>,
212           typename Alloc = tbb::tbb_allocator<iterator_value_t<It>>,
213           typename = std::enable_if_t<is_input_iterator_v<It>>,
214           typename = std::enable_if_t<is_allocator_v<Alloc>>,
215           typename = std::enable_if_t<!is_allocator_v<Comp>>>
216 concurrent_multiset( It, It, Comp = Comp(), Alloc = Alloc() )
217 -> concurrent_multiset<iterator_value_t<It>, Comp, Alloc>;
218 
219 template <typename Key,
220           typename Comp = std::less<Key>,
221           typename Alloc = tbb::tbb_allocator<Key>,
222           typename = std::enable_if_t<is_allocator_v<Alloc>>,
223           typename = std::enable_if_t<!is_allocator_v<Comp>>>
224 concurrent_multiset( std::initializer_list<Key>, Comp = Comp(), Alloc = Alloc() )
225 -> concurrent_multiset<Key, Comp, Alloc>;
226 
227 template <typename It, typename Alloc,
228           typename = std::enable_if_t<is_input_iterator_v<It>>,
229           typename = std::enable_if_t<is_allocator_v<Alloc>>>
230 concurrent_multiset( It, It, Alloc )
231 -> concurrent_multiset<iterator_value_t<It>, std::less<iterator_value_t<It>>, Alloc>;
232 
233 template <typename Key, typename Alloc,
234           typename = std::enable_if_t<is_allocator_v<Alloc>>>
235 concurrent_multiset( std::initializer_list<Key>, Alloc )
236 -> concurrent_multiset<Key, std::less<Key>, Alloc>;
237 
238 #endif // __TBB_CPP17_DEDUCTION_GUIDES_PRESENT
239 
240 template <typename Key, typename Compare, typename Allocator>
241 void swap( concurrent_multiset<Key, Compare, Allocator>& lhs,
242            concurrent_multiset<Key, Compare, Allocator>& rhs )
243 {
244     lhs.swap(rhs);
245 }
246 
247 } // namespace d2
248 } // namespace detail
249 
250 inline namespace v1 {
251 
252 using detail::d2::concurrent_set;
253 using detail::d2::concurrent_multiset;
254 using detail::split;
255 
256 } // inline namespace v1
257 } // namespace tbb
258 
259 #endif // __TBB_concurrent_set_H
260