concurrent_map.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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_map_H
  14. #define __TBB_concurrent_map_H
  15. #define __TBB_concurrent_map_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_map.h
  19. #endif
  20. #include "tbb_config.h"
  21. // concurrent_map 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. template<typename Key, typename Value, typename KeyCompare, typename RandomGenerator,
  27. size_t MAX_LEVELS, typename Allocator, bool AllowMultimapping>
  28. class map_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 mapped_type = Value;
  34. using compare_type = KeyCompare;
  35. using value_type = std::pair<const key_type, mapped_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. class value_compare {
  43. public:
  44. // TODO: these member types are deprecated in C++17, do we need to let them
  45. using result_type = bool;
  46. using first_argument_type = value_type;
  47. using second_argument_type = value_type;
  48. bool operator()(const value_type& lhs, const value_type& rhs) const {
  49. return comp(lhs.first, rhs.first);
  50. }
  51. protected:
  52. value_compare(compare_type c) : comp(c) {}
  53. friend class map_traits;
  54. compare_type comp;
  55. };
  56. static value_compare value_comp(compare_type comp) { return value_compare(comp); }
  57. static const key_type& get_key(const_reference val) {
  58. return val.first;
  59. }
  60. }; // class map_traits
  61. template <typename Key, typename Value, typename Comp, typename Allocator>
  62. class concurrent_multimap;
  63. template <typename Key, typename Value, typename Comp = std::less<Key>, typename Allocator = tbb_allocator<std::pair<const Key, Value>>>
  64. class concurrent_map
  65. : public internal::concurrent_skip_list<map_traits<Key, Value, Comp, internal::concurrent_geometric_level_generator<64>, 64, Allocator, false>> {
  66. using traits_type = map_traits<Key, Value, Comp, internal::concurrent_geometric_level_generator<64>, 64, Allocator, false>;
  67. using base_type = internal::concurrent_skip_list<traits_type>;
  68. #if __TBB_EXTRA_DEBUG
  69. public:
  70. #endif
  71. using base_type::allow_multimapping;
  72. public:
  73. using key_type = Key;
  74. using mapped_type = Value;
  75. using value_type = typename traits_type::value_type;
  76. using size_type = typename base_type::size_type;
  77. using difference_type = typename base_type::difference_type;
  78. using key_compare = Comp;
  79. using value_compare = typename base_type::value_compare;
  80. using allocator_type = Allocator;
  81. using reference = typename base_type::reference;
  82. using const_reference = typename base_type::const_reference;
  83. using pointer = typename base_type::pointer;
  84. using const_pointer = typename base_type::pointer;
  85. using iterator = typename base_type::iterator;
  86. using const_iterator = typename base_type::const_iterator;
  87. using reverse_iterator = typename base_type::reverse_iterator;
  88. using const_reverse_iterator = typename base_type::const_reverse_iterator;
  89. using node_type = typename base_type::node_type;
  90. using base_type::end;
  91. using base_type::find;
  92. using base_type::emplace;
  93. using base_type::insert;
  94. concurrent_map() = default;
  95. explicit concurrent_map(const key_compare& comp, const allocator_type& alloc = allocator_type()) : base_type(comp, alloc) {}
  96. explicit concurrent_map(const allocator_type& alloc) : base_type(key_compare(), alloc) {}
  97. template< class InputIt >
  98. concurrent_map(InputIt first, InputIt last, const key_compare& comp = Comp(), const allocator_type& alloc = allocator_type())
  99. : base_type(first, last, comp, alloc) {}
  100. template< class InputIt >
  101. concurrent_map(InputIt first, InputIt last, const allocator_type& alloc) : base_type(first, last, key_compare(), alloc) {}
  102. /** Copy constructor */
  103. concurrent_map(const concurrent_map&) = default;
  104. concurrent_map(const concurrent_map& other, const allocator_type& alloc) : base_type(other, alloc) {}
  105. concurrent_map(concurrent_map&&) = default;
  106. concurrent_map(concurrent_map&& other, const allocator_type& alloc) : base_type(std::move(other), alloc) {}
  107. concurrent_map(std::initializer_list<value_type> init, const key_compare& comp = Comp(), const allocator_type& alloc = allocator_type())
  108. : base_type(comp, alloc) {
  109. insert(init);
  110. }
  111. concurrent_map(std::initializer_list<value_type> init, const allocator_type& alloc)
  112. : base_type(key_compare(), alloc) {
  113. insert(init);
  114. }
  115. concurrent_map& operator=(const concurrent_map& other) {
  116. return static_cast<concurrent_map&>(base_type::operator=(other));
  117. }
  118. concurrent_map& operator=(concurrent_map&& other) {
  119. return static_cast<concurrent_map&>(base_type::operator=(std::move(other)));
  120. }
  121. mapped_type& at(const key_type& key) {
  122. iterator it = find(key);
  123. if (it == end()) {
  124. tbb::internal::throw_exception(tbb::internal::eid_invalid_key);
  125. }
  126. return it->second;
  127. }
  128. const mapped_type& at(const key_type& key) const {
  129. const_iterator it = find(key);
  130. if (it == end()) {
  131. tbb::internal::throw_exception(tbb::internal::eid_invalid_key);
  132. }
  133. return it->second;
  134. }
  135. mapped_type& operator[](const key_type& key) {
  136. iterator it = find(key);
  137. if (it == end()) {
  138. it = emplace(std::piecewise_construct, std::forward_as_tuple(key), std::tuple<>()).first;
  139. }
  140. return it->second;
  141. }
  142. mapped_type& operator[](key_type&& key) {
  143. iterator it = find(key);
  144. if (it == end()) {
  145. it = emplace(std::piecewise_construct, std::forward_as_tuple(std::move(key)), std::tuple<>()).first;
  146. }
  147. return it->second;
  148. }
  149. template<typename P, typename std::enable_if<std::is_constructible<value_type, P&&>::value>::type>
  150. std::pair<iterator, bool> insert(P&& value) {
  151. return emplace(std::forward<P>(value));
  152. }
  153. template<typename P, typename std::enable_if<std::is_constructible<value_type, P&&>::value>::type>
  154. iterator insert(const_iterator hint, P&& value) {
  155. return emplace_hint(hint, std::forward<P>(value));
  156. return end();
  157. }
  158. template<typename C2>
  159. void merge(concurrent_map<key_type, mapped_type, C2, Allocator>& source) {
  160. this->internal_merge(source);
  161. }
  162. template<typename C2>
  163. void merge(concurrent_map<key_type, mapped_type, C2, Allocator>&& source) {
  164. this->internal_merge(std::move(source));
  165. }
  166. template<typename C2>
  167. void merge(concurrent_multimap<key_type, mapped_type, C2, Allocator>& source) {
  168. this->internal_merge(source);
  169. }
  170. template<typename C2>
  171. void merge(concurrent_multimap<key_type, mapped_type, C2, Allocator>&& source) {
  172. this->internal_merge(std::move(source));
  173. }
  174. }; // class concurrent_map
  175. #if __TBB_CPP17_DEDUCTION_GUIDES_PRESENT
  176. namespace internal {
  177. using namespace tbb::internal;
  178. template<template<typename...> typename Map, typename Key, typename T, typename... Args>
  179. using c_map_t = Map<Key, T,
  180. std::conditional_t< (sizeof...(Args) > 0) && !is_allocator_v<pack_element_t<0, Args...> >,
  181. pack_element_t<0, Args...>, std::less<Key> >,
  182. std::conditional_t< (sizeof...(Args) > 0) && is_allocator_v<pack_element_t<sizeof...(Args)-1, Args...> >,
  183. pack_element_t<sizeof...(Args)-1, Args...>, tbb_allocator<std::pair<const Key, T> > > >;
  184. } // namespace internal
  185. template<typename It, typename... Args>
  186. concurrent_map(It, It, Args...)
  187. -> internal::c_map_t<concurrent_map, internal::iterator_key_t<It>, internal::iterator_mapped_t<It>, Args...>;
  188. template<typename Key, typename T, typename... Args>
  189. concurrent_map(std::initializer_list<std::pair<const Key, T>>, Args...)
  190. -> internal::c_map_t<concurrent_map, Key, T, Args...>;
  191. #endif // __TBB_CPP17_DEDUCTION_GUIDES_PRESENT
  192. template <typename Key, typename Value, typename Comp = std::less<Key>, typename Allocator = tbb_allocator<std::pair<const Key, Value>>>
  193. class concurrent_multimap
  194. : public internal::concurrent_skip_list<map_traits<Key, Value, Comp, internal::concurrent_geometric_level_generator<64>, 64, Allocator, true>> {
  195. using traits_type = map_traits<Key, Value, Comp, internal::concurrent_geometric_level_generator<64>, 64, Allocator, true>;
  196. using base_type = internal::concurrent_skip_list<traits_type>;
  197. #if __TBB_EXTRA_DEBUG
  198. public:
  199. #endif
  200. using base_type::allow_multimapping;
  201. public:
  202. using key_type = Key;
  203. using mapped_type = Value;
  204. using value_type = typename traits_type::value_type;
  205. using size_type = typename base_type::size_type;
  206. using difference_type = typename base_type::difference_type;
  207. using key_compare = Comp;
  208. using value_compare = typename base_type::value_compare;
  209. using allocator_type = Allocator;
  210. using reference = typename base_type::reference;
  211. using const_reference = typename base_type::const_reference;
  212. using pointer = typename base_type::pointer;
  213. using const_pointer = typename base_type::pointer;
  214. using iterator = typename base_type::iterator;
  215. using const_iterator = typename base_type::const_iterator;
  216. using reverse_iterator = typename base_type::reverse_iterator;
  217. using const_reverse_iterator = typename base_type::const_reverse_iterator;
  218. using node_type = typename base_type::node_type;
  219. using base_type::end;
  220. using base_type::find;
  221. using base_type::emplace;
  222. using base_type::insert;
  223. concurrent_multimap() = default;
  224. explicit concurrent_multimap(const key_compare& comp, const allocator_type& alloc = allocator_type()) : base_type(comp, alloc) {}
  225. explicit concurrent_multimap(const allocator_type& alloc) : base_type(key_compare(), alloc) {}
  226. template< class InputIt >
  227. concurrent_multimap(InputIt first, InputIt last, const key_compare& comp = Comp(), const allocator_type& alloc = allocator_type())
  228. : base_type(first, last, comp, alloc) {}
  229. template< class InputIt >
  230. concurrent_multimap(InputIt first, InputIt last, const allocator_type& alloc) : base_type(first, last, key_compare(), alloc) {}
  231. /** Copy constructor */
  232. concurrent_multimap(const concurrent_multimap&) = default;
  233. concurrent_multimap(const concurrent_multimap& other, const allocator_type& alloc) : base_type(other, alloc) {}
  234. concurrent_multimap(concurrent_multimap&&) = default;
  235. concurrent_multimap(concurrent_multimap&& other, const allocator_type& alloc) : base_type(std::move(other), alloc) {}
  236. concurrent_multimap(std::initializer_list<value_type> init, const key_compare& comp = Comp(), const allocator_type& alloc = allocator_type())
  237. : base_type(comp, alloc) {
  238. insert(init);
  239. }
  240. concurrent_multimap(std::initializer_list<value_type> init, const allocator_type& alloc)
  241. : base_type(key_compare(), alloc) {
  242. insert(init);
  243. }
  244. concurrent_multimap& operator=(const concurrent_multimap& other) {
  245. return static_cast<concurrent_multimap&>(base_type::operator=(other));
  246. }
  247. concurrent_multimap& operator=(concurrent_multimap&& other) {
  248. return static_cast<concurrent_multimap&>(base_type::operator=(std::move(other)));
  249. }
  250. template<typename P, typename std::enable_if<std::is_constructible<value_type, P&&>::value>::type>
  251. std::pair<iterator, bool> insert(P&& value) {
  252. return emplace(std::forward<P>(value));
  253. }
  254. template<typename P, typename std::enable_if<std::is_constructible<value_type, P&&>::value>::type>
  255. iterator insert(const_iterator hint, P&& value) {
  256. return emplace_hint(hint, std::forward<P>(value));
  257. return end();
  258. }
  259. template<typename C2>
  260. void merge(concurrent_multimap<key_type, mapped_type, C2, Allocator>& source) {
  261. this->internal_merge(source);
  262. }
  263. template<typename C2>
  264. void merge(concurrent_multimap<key_type, mapped_type, C2, Allocator>&& source) {
  265. this->internal_merge(std::move(source));
  266. }
  267. template<typename C2>
  268. void merge(concurrent_map<key_type, mapped_type, C2, Allocator>& source) {
  269. this->internal_merge(source);
  270. }
  271. template<typename C2>
  272. void merge(concurrent_map<key_type, mapped_type, C2, Allocator>&& source) {
  273. this->internal_merge(std::move(source));
  274. }
  275. }; // class concurrent_multimap
  276. #if __TBB_CPP17_DEDUCTION_GUIDES_PRESENT
  277. template<typename It, typename... Args>
  278. concurrent_multimap(It, It, Args...)
  279. -> internal::c_map_t<concurrent_multimap, internal::iterator_key_t<It>, internal::iterator_mapped_t<It>, Args...>;
  280. template<typename Key, typename T, typename... Args>
  281. concurrent_multimap(std::initializer_list<std::pair<const Key, T>>, Args...)
  282. -> internal::c_map_t<concurrent_multimap, Key, T, Args...>;
  283. #endif // __TBB_CPP17_DEDUCTION_GUIDES_PRESENT
  284. } // namespace interface10
  285. using interface10::concurrent_map;
  286. using interface10::concurrent_multimap;
  287. } // namespace tbb
  288. #include "internal/_warning_suppress_disable_notice.h"
  289. #undef __TBB_concurrent_map_H_include_area
  290. #endif // __TBB_CONCURRENT_ORDERED_CONTAINERS_PRESENT
  291. #endif // __TBB_concurrent_map_H