concurrent_flat_map.hpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. /* Fast open-addressing concurrent hashmap.
  2. *
  3. * Copyright 2023 Christian Mazakas.
  4. * Copyright 2023-2024 Joaquin M Lopez Munoz.
  5. * Distributed under the Boost Software License, Version 1.0.
  6. * (See accompanying file LICENSE_1_0.txt or copy at
  7. * http://www.boost.org/LICENSE_1_0.txt)
  8. *
  9. * See https://www.boost.org/libs/unordered for library home page.
  10. */
  11. #ifndef BOOST_UNORDERED_CONCURRENT_FLAT_MAP_HPP
  12. #define BOOST_UNORDERED_CONCURRENT_FLAT_MAP_HPP
  13. #include <boost/unordered/concurrent_flat_map_fwd.hpp>
  14. #include <boost/unordered/detail/concurrent_static_asserts.hpp>
  15. #include <boost/unordered/detail/foa/concurrent_table.hpp>
  16. #include <boost/unordered/detail/foa/flat_map_types.hpp>
  17. #include <boost/unordered/detail/type_traits.hpp>
  18. #include <boost/unordered/unordered_flat_map_fwd.hpp>
  19. #include <boost/container_hash/hash.hpp>
  20. #include <boost/core/allocator_access.hpp>
  21. #include <boost/core/serialization.hpp>
  22. #include <type_traits>
  23. namespace boost {
  24. namespace unordered {
  25. template <class Key, class T, class Hash, class Pred, class Allocator>
  26. class concurrent_flat_map
  27. {
  28. private:
  29. template <class Key2, class T2, class Hash2, class Pred2,
  30. class Allocator2>
  31. friend class concurrent_flat_map;
  32. template <class Key2, class T2, class Hash2, class Pred2,
  33. class Allocator2>
  34. friend class unordered_flat_map;
  35. using type_policy = detail::foa::flat_map_types<Key, T>;
  36. using table_type =
  37. detail::foa::concurrent_table<type_policy, Hash, Pred, Allocator>;
  38. table_type table_;
  39. template <class K, class V, class H, class KE, class A>
  40. bool friend operator==(concurrent_flat_map<K, V, H, KE, A> const& lhs,
  41. concurrent_flat_map<K, V, H, KE, A> const& rhs);
  42. template <class K, class V, class H, class KE, class A, class Predicate>
  43. friend typename concurrent_flat_map<K, V, H, KE, A>::size_type erase_if(
  44. concurrent_flat_map<K, V, H, KE, A>& set, Predicate pred);
  45. template<class Archive, class K, class V, class H, class KE, class A>
  46. friend void serialize(
  47. Archive& ar, concurrent_flat_map<K, V, H, KE, A>& c,
  48. unsigned int version);
  49. public:
  50. using key_type = Key;
  51. using mapped_type = T;
  52. using value_type = typename type_policy::value_type;
  53. using init_type = typename type_policy::init_type;
  54. using size_type = std::size_t;
  55. using difference_type = std::ptrdiff_t;
  56. using hasher = typename boost::unordered::detail::type_identity<Hash>::type;
  57. using key_equal = typename boost::unordered::detail::type_identity<Pred>::type;
  58. using allocator_type = typename boost::unordered::detail::type_identity<Allocator>::type;
  59. using reference = value_type&;
  60. using const_reference = value_type const&;
  61. using pointer = typename boost::allocator_pointer<allocator_type>::type;
  62. using const_pointer =
  63. typename boost::allocator_const_pointer<allocator_type>::type;
  64. static constexpr size_type bulk_visit_size = table_type::bulk_visit_size;
  65. #if defined(BOOST_UNORDERED_ENABLE_STATS)
  66. using stats = typename table_type::stats;
  67. #endif
  68. concurrent_flat_map()
  69. : concurrent_flat_map(detail::foa::default_bucket_count)
  70. {
  71. }
  72. explicit concurrent_flat_map(size_type n, const hasher& hf = hasher(),
  73. const key_equal& eql = key_equal(),
  74. const allocator_type& a = allocator_type())
  75. : table_(n, hf, eql, a)
  76. {
  77. }
  78. template <class InputIterator>
  79. concurrent_flat_map(InputIterator f, InputIterator l,
  80. size_type n = detail::foa::default_bucket_count,
  81. const hasher& hf = hasher(), const key_equal& eql = key_equal(),
  82. const allocator_type& a = allocator_type())
  83. : table_(n, hf, eql, a)
  84. {
  85. this->insert(f, l);
  86. }
  87. concurrent_flat_map(concurrent_flat_map const& rhs)
  88. : table_(rhs.table_,
  89. boost::allocator_select_on_container_copy_construction(
  90. rhs.get_allocator()))
  91. {
  92. }
  93. concurrent_flat_map(concurrent_flat_map&& rhs)
  94. : table_(std::move(rhs.table_))
  95. {
  96. }
  97. template <class InputIterator>
  98. concurrent_flat_map(
  99. InputIterator f, InputIterator l, allocator_type const& a)
  100. : concurrent_flat_map(f, l, 0, hasher(), key_equal(), a)
  101. {
  102. }
  103. explicit concurrent_flat_map(allocator_type const& a)
  104. : table_(detail::foa::default_bucket_count, hasher(), key_equal(), a)
  105. {
  106. }
  107. concurrent_flat_map(
  108. concurrent_flat_map const& rhs, allocator_type const& a)
  109. : table_(rhs.table_, a)
  110. {
  111. }
  112. concurrent_flat_map(concurrent_flat_map&& rhs, allocator_type const& a)
  113. : table_(std::move(rhs.table_), a)
  114. {
  115. }
  116. concurrent_flat_map(std::initializer_list<value_type> il,
  117. size_type n = detail::foa::default_bucket_count,
  118. const hasher& hf = hasher(), const key_equal& eql = key_equal(),
  119. const allocator_type& a = allocator_type())
  120. : concurrent_flat_map(n, hf, eql, a)
  121. {
  122. this->insert(il.begin(), il.end());
  123. }
  124. concurrent_flat_map(size_type n, const allocator_type& a)
  125. : concurrent_flat_map(n, hasher(), key_equal(), a)
  126. {
  127. }
  128. concurrent_flat_map(
  129. size_type n, const hasher& hf, const allocator_type& a)
  130. : concurrent_flat_map(n, hf, key_equal(), a)
  131. {
  132. }
  133. template <typename InputIterator>
  134. concurrent_flat_map(
  135. InputIterator f, InputIterator l, size_type n, const allocator_type& a)
  136. : concurrent_flat_map(f, l, n, hasher(), key_equal(), a)
  137. {
  138. }
  139. template <typename InputIterator>
  140. concurrent_flat_map(InputIterator f, InputIterator l, size_type n,
  141. const hasher& hf, const allocator_type& a)
  142. : concurrent_flat_map(f, l, n, hf, key_equal(), a)
  143. {
  144. }
  145. concurrent_flat_map(
  146. std::initializer_list<value_type> il, const allocator_type& a)
  147. : concurrent_flat_map(
  148. il, detail::foa::default_bucket_count, hasher(), key_equal(), a)
  149. {
  150. }
  151. concurrent_flat_map(std::initializer_list<value_type> il, size_type n,
  152. const allocator_type& a)
  153. : concurrent_flat_map(il, n, hasher(), key_equal(), a)
  154. {
  155. }
  156. concurrent_flat_map(std::initializer_list<value_type> il, size_type n,
  157. const hasher& hf, const allocator_type& a)
  158. : concurrent_flat_map(il, n, hf, key_equal(), a)
  159. {
  160. }
  161. template <bool avoid_explicit_instantiation = true>
  162. concurrent_flat_map(
  163. unordered_flat_map<Key, T, Hash, Pred, Allocator>&& other)
  164. : table_(std::move(other.table_))
  165. {
  166. }
  167. ~concurrent_flat_map() = default;
  168. concurrent_flat_map& operator=(concurrent_flat_map const& rhs)
  169. {
  170. table_ = rhs.table_;
  171. return *this;
  172. }
  173. concurrent_flat_map& operator=(concurrent_flat_map&& rhs) noexcept(
  174. noexcept(std::declval<table_type&>() = std::declval<table_type&&>()))
  175. {
  176. table_ = std::move(rhs.table_);
  177. return *this;
  178. }
  179. concurrent_flat_map& operator=(std::initializer_list<value_type> ilist)
  180. {
  181. table_ = ilist;
  182. return *this;
  183. }
  184. /// Capacity
  185. ///
  186. size_type size() const noexcept { return table_.size(); }
  187. size_type max_size() const noexcept { return table_.max_size(); }
  188. BOOST_ATTRIBUTE_NODISCARD bool empty() const noexcept
  189. {
  190. return size() == 0;
  191. }
  192. template <class F>
  193. BOOST_FORCEINLINE size_type visit(key_type const& k, F f)
  194. {
  195. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F)
  196. return table_.visit(k, f);
  197. }
  198. template <class F>
  199. BOOST_FORCEINLINE size_type visit(key_type const& k, F f) const
  200. {
  201. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  202. return table_.visit(k, f);
  203. }
  204. template <class F>
  205. BOOST_FORCEINLINE size_type cvisit(key_type const& k, F f) const
  206. {
  207. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  208. return table_.visit(k, f);
  209. }
  210. template <class K, class F>
  211. BOOST_FORCEINLINE typename std::enable_if<
  212. detail::are_transparent<K, hasher, key_equal>::value, size_type>::type
  213. visit(K&& k, F f)
  214. {
  215. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F)
  216. return table_.visit(std::forward<K>(k), f);
  217. }
  218. template <class K, class F>
  219. BOOST_FORCEINLINE typename std::enable_if<
  220. detail::are_transparent<K, hasher, key_equal>::value, size_type>::type
  221. visit(K&& k, F f) const
  222. {
  223. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  224. return table_.visit(std::forward<K>(k), f);
  225. }
  226. template <class K, class F>
  227. BOOST_FORCEINLINE typename std::enable_if<
  228. detail::are_transparent<K, hasher, key_equal>::value, size_type>::type
  229. cvisit(K&& k, F f) const
  230. {
  231. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  232. return table_.visit(std::forward<K>(k), f);
  233. }
  234. template<class FwdIterator, class F>
  235. BOOST_FORCEINLINE
  236. size_t visit(FwdIterator first, FwdIterator last, F f)
  237. {
  238. BOOST_UNORDERED_STATIC_ASSERT_BULK_VISIT_ITERATOR(FwdIterator)
  239. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F)
  240. return table_.visit(first, last, f);
  241. }
  242. template<class FwdIterator, class F>
  243. BOOST_FORCEINLINE
  244. size_t visit(FwdIterator first, FwdIterator last, F f) const
  245. {
  246. BOOST_UNORDERED_STATIC_ASSERT_BULK_VISIT_ITERATOR(FwdIterator)
  247. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  248. return table_.visit(first, last, f);
  249. }
  250. template<class FwdIterator, class F>
  251. BOOST_FORCEINLINE
  252. size_t cvisit(FwdIterator first, FwdIterator last, F f) const
  253. {
  254. BOOST_UNORDERED_STATIC_ASSERT_BULK_VISIT_ITERATOR(FwdIterator)
  255. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  256. return table_.visit(first, last, f);
  257. }
  258. template <class F> size_type visit_all(F f)
  259. {
  260. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F)
  261. return table_.visit_all(f);
  262. }
  263. template <class F> size_type visit_all(F f) const
  264. {
  265. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  266. return table_.visit_all(f);
  267. }
  268. template <class F> size_type cvisit_all(F f) const
  269. {
  270. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  271. return table_.cvisit_all(f);
  272. }
  273. #if defined(BOOST_UNORDERED_PARALLEL_ALGORITHMS)
  274. template <class ExecPolicy, class F>
  275. typename std::enable_if<detail::is_execution_policy<ExecPolicy>::value,
  276. void>::type
  277. visit_all(ExecPolicy&& p, F f)
  278. {
  279. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F)
  280. BOOST_UNORDERED_STATIC_ASSERT_EXEC_POLICY(ExecPolicy)
  281. table_.visit_all(p, f);
  282. }
  283. template <class ExecPolicy, class F>
  284. typename std::enable_if<detail::is_execution_policy<ExecPolicy>::value,
  285. void>::type
  286. visit_all(ExecPolicy&& p, F f) const
  287. {
  288. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  289. BOOST_UNORDERED_STATIC_ASSERT_EXEC_POLICY(ExecPolicy)
  290. table_.visit_all(p, f);
  291. }
  292. template <class ExecPolicy, class F>
  293. typename std::enable_if<detail::is_execution_policy<ExecPolicy>::value,
  294. void>::type
  295. cvisit_all(ExecPolicy&& p, F f) const
  296. {
  297. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  298. BOOST_UNORDERED_STATIC_ASSERT_EXEC_POLICY(ExecPolicy)
  299. table_.cvisit_all(p, f);
  300. }
  301. #endif
  302. template <class F> bool visit_while(F f)
  303. {
  304. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F)
  305. return table_.visit_while(f);
  306. }
  307. template <class F> bool visit_while(F f) const
  308. {
  309. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  310. return table_.visit_while(f);
  311. }
  312. template <class F> bool cvisit_while(F f) const
  313. {
  314. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  315. return table_.cvisit_while(f);
  316. }
  317. #if defined(BOOST_UNORDERED_PARALLEL_ALGORITHMS)
  318. template <class ExecPolicy, class F>
  319. typename std::enable_if<detail::is_execution_policy<ExecPolicy>::value,
  320. bool>::type
  321. visit_while(ExecPolicy&& p, F f)
  322. {
  323. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F)
  324. BOOST_UNORDERED_STATIC_ASSERT_EXEC_POLICY(ExecPolicy)
  325. return table_.visit_while(p, f);
  326. }
  327. template <class ExecPolicy, class F>
  328. typename std::enable_if<detail::is_execution_policy<ExecPolicy>::value,
  329. bool>::type
  330. visit_while(ExecPolicy&& p, F f) const
  331. {
  332. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  333. BOOST_UNORDERED_STATIC_ASSERT_EXEC_POLICY(ExecPolicy)
  334. return table_.visit_while(p, f);
  335. }
  336. template <class ExecPolicy, class F>
  337. typename std::enable_if<detail::is_execution_policy<ExecPolicy>::value,
  338. bool>::type
  339. cvisit_while(ExecPolicy&& p, F f) const
  340. {
  341. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  342. BOOST_UNORDERED_STATIC_ASSERT_EXEC_POLICY(ExecPolicy)
  343. return table_.cvisit_while(p, f);
  344. }
  345. #endif
  346. /// Modifiers
  347. ///
  348. template <class Ty>
  349. BOOST_FORCEINLINE auto insert(Ty&& value)
  350. -> decltype(table_.insert(std::forward<Ty>(value)))
  351. {
  352. return table_.insert(std::forward<Ty>(value));
  353. }
  354. BOOST_FORCEINLINE bool insert(init_type&& obj)
  355. {
  356. return table_.insert(std::move(obj));
  357. }
  358. template <class InputIterator>
  359. size_type insert(InputIterator begin, InputIterator end)
  360. {
  361. size_type count_elements = 0;
  362. for (auto pos = begin; pos != end; ++pos, ++count_elements) {
  363. table_.emplace(*pos);
  364. }
  365. return count_elements;
  366. }
  367. size_type insert(std::initializer_list<value_type> ilist)
  368. {
  369. return this->insert(ilist.begin(), ilist.end());
  370. }
  371. template <class M>
  372. BOOST_FORCEINLINE bool insert_or_assign(key_type const& k, M&& obj)
  373. {
  374. return table_.try_emplace_or_visit(k, std::forward<M>(obj),
  375. [&](value_type& m) { m.second = std::forward<M>(obj); });
  376. }
  377. template <class M>
  378. BOOST_FORCEINLINE bool insert_or_assign(key_type&& k, M&& obj)
  379. {
  380. return table_.try_emplace_or_visit(std::move(k), std::forward<M>(obj),
  381. [&](value_type& m) { m.second = std::forward<M>(obj); });
  382. }
  383. template <class K, class M>
  384. BOOST_FORCEINLINE typename std::enable_if<
  385. detail::are_transparent<K, hasher, key_equal>::value, bool>::type
  386. insert_or_assign(K&& k, M&& obj)
  387. {
  388. return table_.try_emplace_or_visit(std::forward<K>(k),
  389. std::forward<M>(obj),
  390. [&](value_type& m) { m.second = std::forward<M>(obj); });
  391. }
  392. template <class Ty, class F>
  393. BOOST_FORCEINLINE auto insert_or_visit(Ty&& value, F f)
  394. -> decltype(table_.insert_or_visit(std::forward<Ty>(value), f))
  395. {
  396. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F)
  397. return table_.insert_or_visit(std::forward<Ty>(value), f);
  398. }
  399. template <class F>
  400. BOOST_FORCEINLINE bool insert_or_visit(init_type&& obj, F f)
  401. {
  402. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F)
  403. return table_.insert_or_visit(std::move(obj), f);
  404. }
  405. template <class InputIterator, class F>
  406. size_type insert_or_visit(InputIterator first, InputIterator last, F f)
  407. {
  408. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F)
  409. size_type count_elements = 0;
  410. for (; first != last; ++first, ++count_elements) {
  411. table_.emplace_or_visit(*first, f);
  412. }
  413. return count_elements;
  414. }
  415. template <class F>
  416. size_type insert_or_visit(std::initializer_list<value_type> ilist, F f)
  417. {
  418. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F)
  419. return this->insert_or_visit(ilist.begin(), ilist.end(), std::ref(f));
  420. }
  421. template <class Ty, class F>
  422. BOOST_FORCEINLINE auto insert_or_cvisit(Ty&& value, F f)
  423. -> decltype(table_.insert_or_cvisit(std::forward<Ty>(value), f))
  424. {
  425. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  426. return table_.insert_or_cvisit(std::forward<Ty>(value), f);
  427. }
  428. template <class F>
  429. BOOST_FORCEINLINE bool insert_or_cvisit(init_type&& obj, F f)
  430. {
  431. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  432. return table_.insert_or_cvisit(std::move(obj), f);
  433. }
  434. template <class InputIterator, class F>
  435. size_type insert_or_cvisit(InputIterator first, InputIterator last, F f)
  436. {
  437. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  438. size_type count_elements = 0;
  439. for (; first != last; ++first, ++count_elements) {
  440. table_.emplace_or_cvisit(*first, f);
  441. }
  442. return count_elements;
  443. }
  444. template <class F>
  445. size_type insert_or_cvisit(std::initializer_list<value_type> ilist, F f)
  446. {
  447. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  448. return this->insert_or_cvisit(ilist.begin(), ilist.end(), std::ref(f));
  449. }
  450. template <class Ty, class F1, class F2>
  451. BOOST_FORCEINLINE auto insert_and_visit(Ty&& value, F1 f1, F2 f2)
  452. -> decltype(table_.insert_and_visit(std::forward<Ty>(value), f1, f2))
  453. {
  454. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F1)
  455. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F2)
  456. return table_.insert_and_visit(std::forward<Ty>(value), f1, f2);
  457. }
  458. template <class F1, class F2>
  459. BOOST_FORCEINLINE bool insert_and_visit(init_type&& obj, F1 f1, F2 f2)
  460. {
  461. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F1)
  462. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F2)
  463. return table_.insert_and_visit(std::move(obj), f1, f2);
  464. }
  465. template <class InputIterator, class F1, class F2>
  466. size_type insert_and_visit(
  467. InputIterator first, InputIterator last, F1 f1, F2 f2)
  468. {
  469. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F1)
  470. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F2)
  471. size_type count_elements = 0;
  472. for (; first != last; ++first, ++count_elements) {
  473. table_.emplace_and_visit(*first, f1, f2);
  474. }
  475. return count_elements;
  476. }
  477. template <class F1, class F2>
  478. size_type insert_and_visit(
  479. std::initializer_list<value_type> ilist, F1 f1, F2 f2)
  480. {
  481. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F1)
  482. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F2)
  483. return this->insert_and_visit(
  484. ilist.begin(), ilist.end(), std::ref(f1), std::ref(f2));
  485. }
  486. template <class Ty, class F1, class F2>
  487. BOOST_FORCEINLINE auto insert_and_cvisit(Ty&& value, F1 f1, F2 f2)
  488. -> decltype(table_.insert_and_cvisit(std::forward<Ty>(value), f1, f2))
  489. {
  490. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F1)
  491. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F2)
  492. return table_.insert_and_cvisit(std::forward<Ty>(value), f1, f2);
  493. }
  494. template <class F1, class F2>
  495. BOOST_FORCEINLINE bool insert_and_cvisit(init_type&& obj, F1 f1, F2 f2)
  496. {
  497. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F1)
  498. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F2)
  499. return table_.insert_and_cvisit(std::move(obj), f1, f2);
  500. }
  501. template <class InputIterator, class F1, class F2>
  502. size_type insert_and_cvisit(
  503. InputIterator first, InputIterator last, F1 f1, F2 f2)
  504. {
  505. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F1)
  506. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F2)
  507. size_type count_elements = 0;
  508. for (; first != last; ++first, ++count_elements) {
  509. table_.emplace_and_cvisit(*first, f1, f2);
  510. }
  511. return count_elements;
  512. }
  513. template <class F1, class F2>
  514. size_type insert_and_cvisit(
  515. std::initializer_list<value_type> ilist, F1 f1, F2 f2)
  516. {
  517. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F1)
  518. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F2)
  519. return this->insert_and_cvisit(
  520. ilist.begin(), ilist.end(), std::ref(f1), std::ref(f2));
  521. }
  522. template <class... Args> BOOST_FORCEINLINE bool emplace(Args&&... args)
  523. {
  524. return table_.emplace(std::forward<Args>(args)...);
  525. }
  526. template <class Arg, class... Args>
  527. BOOST_FORCEINLINE bool emplace_or_visit(Arg&& arg, Args&&... args)
  528. {
  529. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_INVOCABLE(Arg, Args...)
  530. return table_.emplace_or_visit(
  531. std::forward<Arg>(arg), std::forward<Args>(args)...);
  532. }
  533. template <class Arg, class... Args>
  534. BOOST_FORCEINLINE bool emplace_or_cvisit(Arg&& arg, Args&&... args)
  535. {
  536. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_CONST_INVOCABLE(Arg, Args...)
  537. return table_.emplace_or_cvisit(
  538. std::forward<Arg>(arg), std::forward<Args>(args)...);
  539. }
  540. template <class Arg1, class Arg2, class... Args>
  541. BOOST_FORCEINLINE bool emplace_and_visit(
  542. Arg1&& arg1, Arg2&& arg2, Args&&... args)
  543. {
  544. BOOST_UNORDERED_STATIC_ASSERT_PENULTIMATE_ARG_INVOCABLE(
  545. Arg1, Arg2, Args...)
  546. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_INVOCABLE(Arg2, Args...)
  547. return table_.emplace_and_visit(
  548. std::forward<Arg1>(arg1), std::forward<Arg2>(arg2),
  549. std::forward<Args>(args)...);
  550. }
  551. template <class Arg1, class Arg2, class... Args>
  552. BOOST_FORCEINLINE bool emplace_and_cvisit(
  553. Arg1&& arg1, Arg2&& arg2, Args&&... args)
  554. {
  555. BOOST_UNORDERED_STATIC_ASSERT_PENULTIMATE_ARG_INVOCABLE(
  556. Arg1, Arg2, Args...)
  557. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_CONST_INVOCABLE(Arg2, Args...)
  558. return table_.emplace_and_cvisit(
  559. std::forward<Arg1>(arg1), std::forward<Arg2>(arg2),
  560. std::forward<Args>(args)...);
  561. }
  562. template <class... Args>
  563. BOOST_FORCEINLINE bool try_emplace(key_type const& k, Args&&... args)
  564. {
  565. return table_.try_emplace(k, std::forward<Args>(args)...);
  566. }
  567. template <class... Args>
  568. BOOST_FORCEINLINE bool try_emplace(key_type&& k, Args&&... args)
  569. {
  570. return table_.try_emplace(std::move(k), std::forward<Args>(args)...);
  571. }
  572. template <class K, class... Args>
  573. BOOST_FORCEINLINE typename std::enable_if<
  574. detail::are_transparent<K, hasher, key_equal>::value, bool>::type
  575. try_emplace(K&& k, Args&&... args)
  576. {
  577. return table_.try_emplace(
  578. std::forward<K>(k), std::forward<Args>(args)...);
  579. }
  580. template <class Arg, class... Args>
  581. BOOST_FORCEINLINE bool try_emplace_or_visit(
  582. key_type const& k, Arg&& arg, Args&&... args)
  583. {
  584. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_INVOCABLE(Arg, Args...)
  585. return table_.try_emplace_or_visit(
  586. k, std::forward<Arg>(arg), std::forward<Args>(args)...);
  587. }
  588. template <class Arg, class... Args>
  589. BOOST_FORCEINLINE bool try_emplace_or_cvisit(
  590. key_type const& k, Arg&& arg, Args&&... args)
  591. {
  592. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_CONST_INVOCABLE(Arg, Args...)
  593. return table_.try_emplace_or_cvisit(
  594. k, std::forward<Arg>(arg), std::forward<Args>(args)...);
  595. }
  596. template <class Arg, class... Args>
  597. BOOST_FORCEINLINE bool try_emplace_or_visit(
  598. key_type&& k, Arg&& arg, Args&&... args)
  599. {
  600. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_INVOCABLE(Arg, Args...)
  601. return table_.try_emplace_or_visit(
  602. std::move(k), std::forward<Arg>(arg), std::forward<Args>(args)...);
  603. }
  604. template <class Arg, class... Args>
  605. BOOST_FORCEINLINE bool try_emplace_or_cvisit(
  606. key_type&& k, Arg&& arg, Args&&... args)
  607. {
  608. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_CONST_INVOCABLE(Arg, Args...)
  609. return table_.try_emplace_or_cvisit(
  610. std::move(k), std::forward<Arg>(arg), std::forward<Args>(args)...);
  611. }
  612. template <class K, class Arg, class... Args>
  613. BOOST_FORCEINLINE bool try_emplace_or_visit(
  614. K&& k, Arg&& arg, Args&&... args)
  615. {
  616. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_INVOCABLE(Arg, Args...)
  617. return table_.try_emplace_or_visit(std::forward<K>(k),
  618. std::forward<Arg>(arg), std::forward<Args>(args)...);
  619. }
  620. template <class K, class Arg, class... Args>
  621. BOOST_FORCEINLINE bool try_emplace_or_cvisit(
  622. K&& k, Arg&& arg, Args&&... args)
  623. {
  624. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_CONST_INVOCABLE(Arg, Args...)
  625. return table_.try_emplace_or_cvisit(std::forward<K>(k),
  626. std::forward<Arg>(arg), std::forward<Args>(args)...);
  627. }
  628. template <class Arg1, class Arg2, class... Args>
  629. BOOST_FORCEINLINE bool try_emplace_and_visit(
  630. key_type const& k, Arg1&& arg1, Arg2&& arg2, Args&&... args)
  631. {
  632. BOOST_UNORDERED_STATIC_ASSERT_PENULTIMATE_ARG_INVOCABLE(
  633. Arg1, Arg2, Args...)
  634. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_INVOCABLE(Arg2, Args...)
  635. return table_.try_emplace_and_visit(
  636. k, std::forward<Arg1>(arg1), std::forward<Arg2>(arg2),
  637. std::forward<Args>(args)...);
  638. }
  639. template <class Arg1, class Arg2, class... Args>
  640. BOOST_FORCEINLINE bool try_emplace_and_cvisit(
  641. key_type const& k, Arg1&& arg1, Arg2&& arg2, Args&&... args)
  642. {
  643. BOOST_UNORDERED_STATIC_ASSERT_PENULTIMATE_ARG_INVOCABLE(
  644. Arg1, Arg2, Args...)
  645. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_CONST_INVOCABLE(Arg2, Args...)
  646. return table_.try_emplace_and_cvisit(
  647. k, std::forward<Arg1>(arg1), std::forward<Arg2>(arg2),
  648. std::forward<Args>(args)...);
  649. }
  650. template <class Arg1, class Arg2, class... Args>
  651. BOOST_FORCEINLINE bool try_emplace_and_visit(
  652. key_type&& k, Arg1&& arg1, Arg2&& arg2, Args&&... args)
  653. {
  654. BOOST_UNORDERED_STATIC_ASSERT_PENULTIMATE_ARG_INVOCABLE(
  655. Arg1, Arg2, Args...)
  656. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_INVOCABLE(Arg2, Args...)
  657. return table_.try_emplace_and_visit(
  658. std::move(k), std::forward<Arg1>(arg1), std::forward<Arg2>(arg2),
  659. std::forward<Args>(args)...);
  660. }
  661. template <class Arg1, class Arg2, class... Args>
  662. BOOST_FORCEINLINE bool try_emplace_and_cvisit(
  663. key_type&& k, Arg1&& arg1, Arg2&& arg2, Args&&... args)
  664. {
  665. BOOST_UNORDERED_STATIC_ASSERT_PENULTIMATE_ARG_INVOCABLE(
  666. Arg1, Arg2, Args...)
  667. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_CONST_INVOCABLE(Arg2, Args...)
  668. return table_.try_emplace_and_cvisit(
  669. std::move(k), std::forward<Arg1>(arg1), std::forward<Arg2>(arg2),
  670. std::forward<Args>(args)...);
  671. }
  672. template <class K, class Arg1, class Arg2, class... Args>
  673. BOOST_FORCEINLINE bool try_emplace_and_visit(
  674. K&& k, Arg1&& arg1, Arg2&& arg2, Args&&... args)
  675. {
  676. BOOST_UNORDERED_STATIC_ASSERT_PENULTIMATE_ARG_INVOCABLE(
  677. Arg1, Arg2, Args...)
  678. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_INVOCABLE(Arg2, Args...)
  679. return table_.try_emplace_and_visit(std::forward<K>(k),
  680. std::forward<Arg1>(arg1), std::forward<Arg2>(arg2),
  681. std::forward<Args>(args)...);
  682. }
  683. template <class K, class Arg1, class Arg2, class... Args>
  684. BOOST_FORCEINLINE bool try_emplace_and_cvisit(
  685. K&& k, Arg1&& arg1, Arg2&& arg2, Args&&... args)
  686. {
  687. BOOST_UNORDERED_STATIC_ASSERT_PENULTIMATE_ARG_INVOCABLE(
  688. Arg1, Arg2, Args...)
  689. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_CONST_INVOCABLE(Arg2, Args...)
  690. return table_.try_emplace_and_cvisit(std::forward<K>(k),
  691. std::forward<Arg1>(arg1), std::forward<Arg2>(arg2),
  692. std::forward<Args>(args)...);
  693. }
  694. BOOST_FORCEINLINE size_type erase(key_type const& k)
  695. {
  696. return table_.erase(k);
  697. }
  698. template <class K>
  699. BOOST_FORCEINLINE typename std::enable_if<
  700. detail::are_transparent<K, hasher, key_equal>::value, size_type>::type
  701. erase(K&& k)
  702. {
  703. return table_.erase(std::forward<K>(k));
  704. }
  705. template <class F>
  706. BOOST_FORCEINLINE size_type erase_if(key_type const& k, F f)
  707. {
  708. return table_.erase_if(k, f);
  709. }
  710. template <class K, class F>
  711. BOOST_FORCEINLINE typename std::enable_if<
  712. detail::are_transparent<K, hasher, key_equal>::value &&
  713. !detail::is_execution_policy<K>::value,
  714. size_type>::type
  715. erase_if(K&& k, F f)
  716. {
  717. return table_.erase_if(std::forward<K>(k), f);
  718. }
  719. #if defined(BOOST_UNORDERED_PARALLEL_ALGORITHMS)
  720. template <class ExecPolicy, class F>
  721. typename std::enable_if<detail::is_execution_policy<ExecPolicy>::value,
  722. void>::type
  723. erase_if(ExecPolicy&& p, F f)
  724. {
  725. BOOST_UNORDERED_STATIC_ASSERT_EXEC_POLICY(ExecPolicy)
  726. table_.erase_if(p, f);
  727. }
  728. #endif
  729. template <class F> size_type erase_if(F f) { return table_.erase_if(f); }
  730. void swap(concurrent_flat_map& other) noexcept(
  731. boost::allocator_is_always_equal<Allocator>::type::value ||
  732. boost::allocator_propagate_on_container_swap<Allocator>::type::value)
  733. {
  734. return table_.swap(other.table_);
  735. }
  736. void clear() noexcept { table_.clear(); }
  737. template <typename H2, typename P2>
  738. size_type merge(concurrent_flat_map<Key, T, H2, P2, Allocator>& x)
  739. {
  740. BOOST_ASSERT(get_allocator() == x.get_allocator());
  741. return table_.merge(x.table_);
  742. }
  743. template <typename H2, typename P2>
  744. size_type merge(concurrent_flat_map<Key, T, H2, P2, Allocator>&& x)
  745. {
  746. return merge(x);
  747. }
  748. BOOST_FORCEINLINE size_type count(key_type const& k) const
  749. {
  750. return table_.count(k);
  751. }
  752. template <class K>
  753. BOOST_FORCEINLINE typename std::enable_if<
  754. detail::are_transparent<K, hasher, key_equal>::value, size_type>::type
  755. count(K const& k)
  756. {
  757. return table_.count(k);
  758. }
  759. BOOST_FORCEINLINE bool contains(key_type const& k) const
  760. {
  761. return table_.contains(k);
  762. }
  763. template <class K>
  764. BOOST_FORCEINLINE typename std::enable_if<
  765. detail::are_transparent<K, hasher, key_equal>::value, bool>::type
  766. contains(K const& k) const
  767. {
  768. return table_.contains(k);
  769. }
  770. /// Hash Policy
  771. ///
  772. size_type bucket_count() const noexcept { return table_.capacity(); }
  773. float load_factor() const noexcept { return table_.load_factor(); }
  774. float max_load_factor() const noexcept
  775. {
  776. return table_.max_load_factor();
  777. }
  778. void max_load_factor(float) {}
  779. size_type max_load() const noexcept { return table_.max_load(); }
  780. void rehash(size_type n) { table_.rehash(n); }
  781. void reserve(size_type n) { table_.reserve(n); }
  782. #if defined(BOOST_UNORDERED_ENABLE_STATS)
  783. /// Stats
  784. ///
  785. stats get_stats() const { return table_.get_stats(); }
  786. void reset_stats() noexcept { table_.reset_stats(); }
  787. #endif
  788. /// Observers
  789. ///
  790. allocator_type get_allocator() const noexcept
  791. {
  792. return table_.get_allocator();
  793. }
  794. hasher hash_function() const { return table_.hash_function(); }
  795. key_equal key_eq() const { return table_.key_eq(); }
  796. };
  797. template <class Key, class T, class Hash, class KeyEqual, class Allocator>
  798. bool operator==(
  799. concurrent_flat_map<Key, T, Hash, KeyEqual, Allocator> const& lhs,
  800. concurrent_flat_map<Key, T, Hash, KeyEqual, Allocator> const& rhs)
  801. {
  802. return lhs.table_ == rhs.table_;
  803. }
  804. template <class Key, class T, class Hash, class KeyEqual, class Allocator>
  805. bool operator!=(
  806. concurrent_flat_map<Key, T, Hash, KeyEqual, Allocator> const& lhs,
  807. concurrent_flat_map<Key, T, Hash, KeyEqual, Allocator> const& rhs)
  808. {
  809. return !(lhs == rhs);
  810. }
  811. template <class Key, class T, class Hash, class Pred, class Alloc>
  812. void swap(concurrent_flat_map<Key, T, Hash, Pred, Alloc>& x,
  813. concurrent_flat_map<Key, T, Hash, Pred, Alloc>& y)
  814. noexcept(noexcept(x.swap(y)))
  815. {
  816. x.swap(y);
  817. }
  818. template <class K, class T, class H, class P, class A, class Predicate>
  819. typename concurrent_flat_map<K, T, H, P, A>::size_type erase_if(
  820. concurrent_flat_map<K, T, H, P, A>& c, Predicate pred)
  821. {
  822. return c.table_.erase_if(pred);
  823. }
  824. template<class Archive, class K, class V, class H, class KE, class A>
  825. void serialize(
  826. Archive& ar, concurrent_flat_map<K, V, H, KE, A>& c, unsigned int)
  827. {
  828. ar & core::make_nvp("table",c.table_);
  829. }
  830. #if BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES
  831. template <class InputIterator,
  832. class Hash =
  833. boost::hash<boost::unordered::detail::iter_key_t<InputIterator> >,
  834. class Pred =
  835. std::equal_to<boost::unordered::detail::iter_key_t<InputIterator> >,
  836. class Allocator = std::allocator<
  837. boost::unordered::detail::iter_to_alloc_t<InputIterator> >,
  838. class = std::enable_if_t<detail::is_input_iterator_v<InputIterator> >,
  839. class = std::enable_if_t<detail::is_hash_v<Hash> >,
  840. class = std::enable_if_t<detail::is_pred_v<Pred> >,
  841. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  842. concurrent_flat_map(InputIterator, InputIterator,
  843. std::size_t = boost::unordered::detail::foa::default_bucket_count,
  844. Hash = Hash(), Pred = Pred(), Allocator = Allocator())
  845. -> concurrent_flat_map<
  846. boost::unordered::detail::iter_key_t<InputIterator>,
  847. boost::unordered::detail::iter_val_t<InputIterator>, Hash, Pred,
  848. Allocator>;
  849. template <class Key, class T,
  850. class Hash = boost::hash<std::remove_const_t<Key> >,
  851. class Pred = std::equal_to<std::remove_const_t<Key> >,
  852. class Allocator = std::allocator<std::pair<const Key, T> >,
  853. class = std::enable_if_t<detail::is_hash_v<Hash> >,
  854. class = std::enable_if_t<detail::is_pred_v<Pred> >,
  855. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  856. concurrent_flat_map(std::initializer_list<std::pair<Key, T> >,
  857. std::size_t = boost::unordered::detail::foa::default_bucket_count,
  858. Hash = Hash(), Pred = Pred(), Allocator = Allocator())
  859. -> concurrent_flat_map<std::remove_const_t<Key>, T, Hash, Pred,
  860. Allocator>;
  861. template <class InputIterator, class Allocator,
  862. class = std::enable_if_t<detail::is_input_iterator_v<InputIterator> >,
  863. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  864. concurrent_flat_map(InputIterator, InputIterator, std::size_t, Allocator)
  865. -> concurrent_flat_map<
  866. boost::unordered::detail::iter_key_t<InputIterator>,
  867. boost::unordered::detail::iter_val_t<InputIterator>,
  868. boost::hash<boost::unordered::detail::iter_key_t<InputIterator> >,
  869. std::equal_to<boost::unordered::detail::iter_key_t<InputIterator> >,
  870. Allocator>;
  871. template <class InputIterator, class Allocator,
  872. class = std::enable_if_t<detail::is_input_iterator_v<InputIterator> >,
  873. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  874. concurrent_flat_map(InputIterator, InputIterator, Allocator)
  875. -> concurrent_flat_map<
  876. boost::unordered::detail::iter_key_t<InputIterator>,
  877. boost::unordered::detail::iter_val_t<InputIterator>,
  878. boost::hash<boost::unordered::detail::iter_key_t<InputIterator> >,
  879. std::equal_to<boost::unordered::detail::iter_key_t<InputIterator> >,
  880. Allocator>;
  881. template <class InputIterator, class Hash, class Allocator,
  882. class = std::enable_if_t<detail::is_hash_v<Hash> >,
  883. class = std::enable_if_t<detail::is_input_iterator_v<InputIterator> >,
  884. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  885. concurrent_flat_map(
  886. InputIterator, InputIterator, std::size_t, Hash, Allocator)
  887. -> concurrent_flat_map<
  888. boost::unordered::detail::iter_key_t<InputIterator>,
  889. boost::unordered::detail::iter_val_t<InputIterator>, Hash,
  890. std::equal_to<boost::unordered::detail::iter_key_t<InputIterator> >,
  891. Allocator>;
  892. template <class Key, class T, class Allocator,
  893. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  894. concurrent_flat_map(std::initializer_list<std::pair<Key, T> >, std::size_t,
  895. Allocator) -> concurrent_flat_map<std::remove_const_t<Key>, T,
  896. boost::hash<std::remove_const_t<Key> >,
  897. std::equal_to<std::remove_const_t<Key> >, Allocator>;
  898. template <class Key, class T, class Allocator,
  899. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  900. concurrent_flat_map(std::initializer_list<std::pair<Key, T> >, Allocator)
  901. -> concurrent_flat_map<std::remove_const_t<Key>, T,
  902. boost::hash<std::remove_const_t<Key> >,
  903. std::equal_to<std::remove_const_t<Key> >, Allocator>;
  904. template <class Key, class T, class Hash, class Allocator,
  905. class = std::enable_if_t<detail::is_hash_v<Hash> >,
  906. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  907. concurrent_flat_map(std::initializer_list<std::pair<Key, T> >, std::size_t,
  908. Hash, Allocator) -> concurrent_flat_map<std::remove_const_t<Key>, T,
  909. Hash, std::equal_to<std::remove_const_t<Key> >, Allocator>;
  910. #endif
  911. } // namespace unordered
  912. } // namespace boost
  913. #endif // BOOST_UNORDERED_CONCURRENT_FLAT_MAP_HPP