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 79 // Required for implicit deduction guides 80 concurrent_set() = default; 81 concurrent_set( const concurrent_set& ) = default; 82 concurrent_set( const concurrent_set& other, const allocator_type& alloc ) : base_type(other, alloc) {} 83 concurrent_set( concurrent_set&& ) = default; 84 concurrent_set( concurrent_set&& other, const allocator_type& alloc ) : base_type(std::move(other), alloc) {} 85 // Required to respect the rule of 5 86 concurrent_set& operator=( const concurrent_set& ) = default; 87 concurrent_set& operator=( concurrent_set&& ) = default; 88 89 concurrent_set& operator=( std::initializer_list<value_type> il ) { 90 base_type::operator= (il); 91 return *this; 92 } 93 94 template<typename OtherCompare> 95 void merge(concurrent_set<key_type, OtherCompare, Allocator>& source) { 96 this->internal_merge(source); 97 } 98 99 template<typename OtherCompare> 100 void merge(concurrent_set<key_type, OtherCompare, Allocator>&& source) { 101 this->internal_merge(std::move(source)); 102 } 103 104 template<typename OtherCompare> 105 void merge(concurrent_multiset<key_type, OtherCompare, Allocator>& source) { 106 this->internal_merge(source); 107 } 108 109 template<typename OtherCompare> 110 void merge(concurrent_multiset<key_type, OtherCompare, Allocator>&& source) { 111 this->internal_merge(std::move(source)); 112 } 113 }; // class concurrent_set 114 115 #if __TBB_CPP17_DEDUCTION_GUIDES_PRESENT 116 117 template <typename It, 118 typename Comp = std::less<iterator_value_t<It>>, 119 typename Alloc = tbb::tbb_allocator<iterator_value_t<It>>, 120 typename = std::enable_if_t<is_input_iterator_v<It>>, 121 typename = std::enable_if_t<is_allocator_v<Alloc>>, 122 typename = std::enable_if_t<!is_allocator_v<Comp>>> 123 concurrent_set( It, It, Comp = Comp(), Alloc = Alloc() ) 124 -> concurrent_set<iterator_value_t<It>, Comp, Alloc>; 125 126 template <typename Key, 127 typename Comp = std::less<Key>, 128 typename Alloc = tbb::tbb_allocator<Key>, 129 typename = std::enable_if_t<is_allocator_v<Alloc>>, 130 typename = std::enable_if_t<!is_allocator_v<Comp>>> 131 concurrent_set( std::initializer_list<Key>, Comp = Comp(), Alloc = Alloc() ) 132 -> concurrent_set<Key, Comp, Alloc>; 133 134 template <typename It, typename Alloc, 135 typename = std::enable_if_t<is_input_iterator_v<It>>, 136 typename = std::enable_if_t<is_allocator_v<Alloc>>> 137 concurrent_set( It, It, Alloc ) 138 -> concurrent_set<iterator_value_t<It>, 139 std::less<iterator_value_t<It>>, Alloc>; 140 141 template <typename Key, typename Alloc, 142 typename = std::enable_if_t<is_allocator_v<Alloc>>> 143 concurrent_set( std::initializer_list<Key>, Alloc ) 144 -> concurrent_set<Key, std::less<Key>, Alloc>; 145 146 #endif // __TBB_CPP17_DEDUCTION_GUIDES_PRESENT 147 148 template <typename Key, typename Compare, typename Allocator> 149 void swap( concurrent_set<Key, Compare, Allocator>& lhs, 150 concurrent_set<Key, Compare, Allocator>& rhs ) 151 { 152 lhs.swap(rhs); 153 } 154 155 template <typename Key, typename Compare = std::less<Key>, typename Allocator = tbb::tbb_allocator<Key>> 156 class concurrent_multiset : public concurrent_skip_list<set_traits<Key, Compare, concurrent_geometric_level_generator<32>, Allocator, true>> { 157 using base_type = concurrent_skip_list<set_traits<Key, Compare, concurrent_geometric_level_generator<32>, Allocator, true>>; 158 public: 159 using key_type = Key; 160 using value_type = typename base_type::value_type; 161 using size_type = typename base_type::size_type; 162 using difference_type = typename base_type::difference_type; 163 using key_compare = Compare; 164 using value_compare = typename base_type::value_compare; 165 using allocator_type = Allocator; 166 167 using reference = typename base_type::reference; 168 using const_reference = typename base_type::const_reference; 169 using pointer = typename base_type::pointer; 170 using const_pointer = typename base_type::const_pointer; 171 172 using iterator = typename base_type::iterator; 173 using const_iterator = typename base_type::const_iterator; 174 175 using node_type = typename base_type::node_type; 176 177 // Include constructors of base_type; 178 using base_type::base_type; 179 180 // Required for implicit deduction guides 181 concurrent_multiset() = default; 182 concurrent_multiset( const concurrent_multiset& ) = default; 183 concurrent_multiset( const concurrent_multiset& other, const allocator_type& alloc ) : base_type(other, alloc) {} 184 concurrent_multiset( concurrent_multiset&& ) = default; 185 concurrent_multiset( concurrent_multiset&& other, const allocator_type& alloc ) : base_type(std::move(other), alloc) {} 186 // Required to respect the rule of 5 187 concurrent_multiset& operator=( const concurrent_multiset& ) = default; 188 concurrent_multiset& operator=( concurrent_multiset&& ) = default; 189 190 concurrent_multiset& operator=( std::initializer_list<value_type> il ) { 191 base_type::operator= (il); 192 return *this; 193 } 194 195 template<typename OtherCompare> 196 void merge(concurrent_set<key_type, OtherCompare, Allocator>& source) { 197 this->internal_merge(source); 198 } 199 200 template<typename OtherCompare> 201 void merge(concurrent_set<key_type, OtherCompare, Allocator>&& source) { 202 this->internal_merge(std::move(source)); 203 } 204 205 template<typename OtherCompare> 206 void merge(concurrent_multiset<key_type, OtherCompare, Allocator>& source) { 207 this->internal_merge(source); 208 } 209 210 template<typename OtherCompare> 211 void merge(concurrent_multiset<key_type, OtherCompare, Allocator>&& source) { 212 this->internal_merge(std::move(source)); 213 } 214 }; // class concurrent_multiset 215 216 #if __TBB_CPP17_DEDUCTION_GUIDES_PRESENT 217 218 template <typename It, 219 typename Comp = std::less<iterator_value_t<It>>, 220 typename Alloc = tbb::tbb_allocator<iterator_value_t<It>>, 221 typename = std::enable_if_t<is_input_iterator_v<It>>, 222 typename = std::enable_if_t<is_allocator_v<Alloc>>, 223 typename = std::enable_if_t<!is_allocator_v<Comp>>> 224 concurrent_multiset( It, It, Comp = Comp(), Alloc = Alloc() ) 225 -> concurrent_multiset<iterator_value_t<It>, Comp, Alloc>; 226 227 template <typename Key, 228 typename Comp = std::less<Key>, 229 typename Alloc = tbb::tbb_allocator<Key>, 230 typename = std::enable_if_t<is_allocator_v<Alloc>>, 231 typename = std::enable_if_t<!is_allocator_v<Comp>>> 232 concurrent_multiset( std::initializer_list<Key>, Comp = Comp(), Alloc = Alloc() ) 233 -> concurrent_multiset<Key, Comp, Alloc>; 234 235 template <typename It, typename Alloc, 236 typename = std::enable_if_t<is_input_iterator_v<It>>, 237 typename = std::enable_if_t<is_allocator_v<Alloc>>> 238 concurrent_multiset( It, It, Alloc ) 239 -> concurrent_multiset<iterator_value_t<It>, std::less<iterator_value_t<It>>, Alloc>; 240 241 template <typename Key, typename Alloc, 242 typename = std::enable_if_t<is_allocator_v<Alloc>>> 243 concurrent_multiset( std::initializer_list<Key>, Alloc ) 244 -> concurrent_multiset<Key, std::less<Key>, Alloc>; 245 246 #endif // __TBB_CPP17_DEDUCTION_GUIDES_PRESENT 247 248 template <typename Key, typename Compare, typename Allocator> 249 void swap( concurrent_multiset<Key, Compare, Allocator>& lhs, 250 concurrent_multiset<Key, Compare, Allocator>& rhs ) 251 { 252 lhs.swap(rhs); 253 } 254 255 } // namespace d2 256 } // namespace detail 257 258 inline namespace v1 { 259 260 using detail::d2::concurrent_set; 261 using detail::d2::concurrent_multiset; 262 using detail::split; 263 264 } // inline namespace v1 265 } // namespace tbb 266 267 #endif // __TBB_concurrent_set_H 268