/* Copyright (c) 2019-2021 Intel Corporation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #ifndef __TBB_concurrent_map_H #define __TBB_concurrent_map_H #include "detail/_namespace_injection.h" #include "detail/_concurrent_skip_list.h" #include "tbb_allocator.h" #include #include #include namespace tbb { namespace detail { namespace d2 { template struct map_traits { static constexpr std::size_t max_level = RandomGenerator::max_level; using random_level_generator_type = RandomGenerator; using key_type = Key; using mapped_type = Value; using compare_type = KeyCompare; using value_type = std::pair; using reference = value_type&; using const_reference = const value_type&; using allocator_type = Allocator; static constexpr bool allow_multimapping = AllowMultimapping; class value_compare { public: bool operator()(const value_type& lhs, const value_type& rhs) const { return comp(lhs.first, rhs.first); } protected: value_compare(compare_type c) : comp(c) {} friend struct map_traits; compare_type comp; }; static value_compare value_comp(compare_type comp) { return value_compare(comp); } static const key_type& get_key(const_reference val) { return val.first; } }; // struct map_traits template class concurrent_multimap; template , typename Allocator = tbb::tbb_allocator>> class concurrent_map : public concurrent_skip_list, Allocator, false>> { using base_type = concurrent_skip_list, Allocator, false>>; public: using key_type = Key; using mapped_type = Value; using value_type = typename base_type::value_type; using size_type = typename base_type::size_type; using difference_type = typename base_type::difference_type; using key_compare = Compare; using value_compare = typename base_type::value_compare; using allocator_type = Allocator; using reference = typename base_type::reference; using const_reference = typename base_type::const_reference; using pointer = typename base_type::pointer; using const_pointer = typename base_type::const_pointer; using iterator = typename base_type::iterator; using const_iterator = typename base_type::const_iterator; using node_type = typename base_type::node_type; // Include constructors of base type using base_type::base_type; using base_type::operator=; // Required for implicit deduction guides concurrent_map() = default; concurrent_map( const concurrent_map& ) = default; concurrent_map( const concurrent_map& other, const allocator_type& alloc ) : base_type(other, alloc) {} concurrent_map( concurrent_map&& ) = default; concurrent_map( concurrent_map&& other, const allocator_type& alloc ) : base_type(std::move(other), alloc) {} // Required to respect the rule of 5 concurrent_map& operator=( const concurrent_map& ) = default; concurrent_map& operator=( concurrent_map&& ) = default; // Observers mapped_type& at(const key_type& key) { iterator it = this->find(key); if (it == this->end()) { throw_exception(exception_id::invalid_key); } return it->second; } const mapped_type& at(const key_type& key) const { return const_cast(this)->at(key); } mapped_type& operator[](const key_type& key) { iterator it = this->find(key); if (it == this->end()) { it = this->emplace(std::piecewise_construct, std::forward_as_tuple(key), std::tuple<>()).first; } return it->second; } mapped_type& operator[](key_type&& key) { iterator it = this->find(key); if (it == this->end()) { it = this->emplace(std::piecewise_construct, std::forward_as_tuple(std::move(key)), std::tuple<>()).first; } return it->second; } using base_type::insert; template typename std::enable_if::value, std::pair>::type insert( P&& value ) { return this->emplace(std::forward

(value)); } template typename std::enable_if::value, iterator>::type insert( const_iterator hint, P&& value ) { return this->emplace_hint(hint, std::forward

(value)); } template void merge(concurrent_map& source) { this->internal_merge(source); } template void merge(concurrent_map&& source) { this->internal_merge(std::move(source)); } template void merge(concurrent_multimap& source) { this->internal_merge(source); } template void merge(concurrent_multimap&& source) { this->internal_merge(std::move(source)); } }; // class concurrent_map #if __TBB_CPP17_DEDUCTION_GUIDES_PRESENT template >, typename Alloc = tbb::tbb_allocator>, typename = std::enable_if_t>, typename = std::enable_if_t>, typename = std::enable_if_t>> concurrent_map( It, It, Comp = Comp(), Alloc = Alloc() ) -> concurrent_map, iterator_mapped_t, Comp, Alloc>; template >, typename Alloc = tbb::tbb_allocator>, typename = std::enable_if_t>, typename = std::enable_if_t>> concurrent_map( std::initializer_list>, Comp = Comp(), Alloc = Alloc() ) -> concurrent_map, T, Comp, Alloc>; template >, typename = std::enable_if_t>> concurrent_map( It, It, Alloc ) -> concurrent_map, iterator_mapped_t, std::less>, Alloc>; template >> concurrent_map( std::initializer_list>, Alloc ) -> concurrent_map, T, std::less>, Alloc>; #endif // __TBB_CPP17_DEDUCTION_GUIDES_PRESENT template void swap( concurrent_map& lhs, concurrent_map& rhs ) { lhs.swap(rhs); } template , typename Allocator = tbb::tbb_allocator>> class concurrent_multimap : public concurrent_skip_list, Allocator, true>> { using base_type = concurrent_skip_list, Allocator, true>>; public: using key_type = Key; using mapped_type = Value; using value_type = typename base_type::value_type; using size_type = typename base_type::size_type; using difference_type = typename base_type::difference_type; using key_compare = Compare; using value_compare = typename base_type::value_compare; using allocator_type = Allocator; using reference = typename base_type::reference; using const_reference = typename base_type::const_reference; using pointer = typename base_type::pointer; using const_pointer = typename base_type::const_pointer; using iterator = typename base_type::iterator; using const_iterator = typename base_type::const_iterator; using node_type = typename base_type::node_type; // Include constructors of base_type using base_type::base_type; using base_type::insert; using base_type::operator=; // Required for implicit deduction guides concurrent_multimap() = default; concurrent_multimap( const concurrent_multimap& ) = default; concurrent_multimap( const concurrent_multimap& other, const allocator_type& alloc ) : base_type(other, alloc) {} concurrent_multimap( concurrent_multimap&& ) = default; concurrent_multimap( concurrent_multimap&& other, const allocator_type& alloc ) : base_type(std::move(other), alloc) {} // Required to respect the rule of 5 concurrent_multimap& operator=( const concurrent_multimap& ) = default; concurrent_multimap& operator=( concurrent_multimap&& ) = default; template typename std::enable_if::value, std::pair>::type insert( P&& value ) { return this->emplace(std::forward

(value)); } template typename std::enable_if::value, iterator>::type insert( const_iterator hint, P&& value ) { return this->emplace_hint(hint, std::forward

(value)); } template void merge(concurrent_multimap& source) { this->internal_merge(source); } template void merge(concurrent_multimap&& source) { this->internal_merge(std::move(source)); } template void merge(concurrent_map& source) { this->internal_merge(source); } template void merge(concurrent_map&& source) { this->internal_merge(std::move(source)); } }; // class concurrent_multimap #if __TBB_CPP17_DEDUCTION_GUIDES_PRESENT template >, typename Alloc = tbb::tbb_allocator>, typename = std::enable_if_t>, typename = std::enable_if_t>, typename = std::enable_if_t>> concurrent_multimap( It, It, Comp = Comp(), Alloc = Alloc() ) -> concurrent_multimap, iterator_mapped_t, Comp, Alloc>; template >, typename Alloc = tbb::tbb_allocator>, typename = std::enable_if_t>, typename = std::enable_if_t>> concurrent_multimap( std::initializer_list>, Comp = Comp(), Alloc = Alloc() ) -> concurrent_multimap, T, Comp, Alloc>; template >, typename = std::enable_if_t>> concurrent_multimap( It, It, Alloc ) -> concurrent_multimap, iterator_mapped_t, std::less>, Alloc>; template >> concurrent_multimap( std::initializer_list>, Alloc ) -> concurrent_multimap, T, std::less>, Alloc>; #endif // __TBB_CPP17_DEDUCTION_GUIDES_PRESENT template void swap( concurrent_multimap& lhs, concurrent_multimap& rhs ) { lhs.swap(rhs); } } // namespace d2 } // namespace detail inline namespace v1 { using detail::d2::concurrent_map; using detail::d2::concurrent_multimap; using detail::split; } // inline namespace v1 } // namespace tbb #endif // __TBB_concurrent_map_H