concurrent_flat_map.hpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. concurrent_flat_map(
  162. unordered_flat_map<Key, T, Hash, Pred, Allocator>&& other)
  163. : table_(std::move(other.table_))
  164. {
  165. }
  166. ~concurrent_flat_map() = default;
  167. concurrent_flat_map& operator=(concurrent_flat_map const& rhs)
  168. {
  169. table_ = rhs.table_;
  170. return *this;
  171. }
  172. concurrent_flat_map& operator=(concurrent_flat_map&& rhs) noexcept(
  173. noexcept(std::declval<table_type&>() = std::declval<table_type&&>()))
  174. {
  175. table_ = std::move(rhs.table_);
  176. return *this;
  177. }
  178. concurrent_flat_map& operator=(std::initializer_list<value_type> ilist)
  179. {
  180. table_ = ilist;
  181. return *this;
  182. }
  183. /// Capacity
  184. ///
  185. size_type size() const noexcept { return table_.size(); }
  186. size_type max_size() const noexcept { return table_.max_size(); }
  187. BOOST_ATTRIBUTE_NODISCARD bool empty() const noexcept
  188. {
  189. return size() == 0;
  190. }
  191. template <class F>
  192. BOOST_FORCEINLINE size_type visit(key_type const& k, F f)
  193. {
  194. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F)
  195. return table_.visit(k, f);
  196. }
  197. template <class F>
  198. BOOST_FORCEINLINE size_type visit(key_type const& k, F f) const
  199. {
  200. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  201. return table_.visit(k, f);
  202. }
  203. template <class F>
  204. BOOST_FORCEINLINE size_type cvisit(key_type const& k, F f) const
  205. {
  206. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  207. return table_.visit(k, f);
  208. }
  209. template <class K, class F>
  210. BOOST_FORCEINLINE typename std::enable_if<
  211. detail::are_transparent<K, hasher, key_equal>::value, size_type>::type
  212. visit(K&& k, F f)
  213. {
  214. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F)
  215. return table_.visit(std::forward<K>(k), f);
  216. }
  217. template <class K, class F>
  218. BOOST_FORCEINLINE typename std::enable_if<
  219. detail::are_transparent<K, hasher, key_equal>::value, size_type>::type
  220. visit(K&& k, F f) const
  221. {
  222. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  223. return table_.visit(std::forward<K>(k), f);
  224. }
  225. template <class K, class F>
  226. BOOST_FORCEINLINE typename std::enable_if<
  227. detail::are_transparent<K, hasher, key_equal>::value, size_type>::type
  228. cvisit(K&& k, F f) const
  229. {
  230. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  231. return table_.visit(std::forward<K>(k), f);
  232. }
  233. template<class FwdIterator, class F>
  234. BOOST_FORCEINLINE
  235. size_t visit(FwdIterator first, FwdIterator last, F f)
  236. {
  237. BOOST_UNORDERED_STATIC_ASSERT_BULK_VISIT_ITERATOR(FwdIterator)
  238. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F)
  239. return table_.visit(first, last, f);
  240. }
  241. template<class FwdIterator, class F>
  242. BOOST_FORCEINLINE
  243. size_t visit(FwdIterator first, FwdIterator last, F f) const
  244. {
  245. BOOST_UNORDERED_STATIC_ASSERT_BULK_VISIT_ITERATOR(FwdIterator)
  246. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  247. return table_.visit(first, last, f);
  248. }
  249. template<class FwdIterator, class F>
  250. BOOST_FORCEINLINE
  251. size_t cvisit(FwdIterator first, FwdIterator last, F f) const
  252. {
  253. BOOST_UNORDERED_STATIC_ASSERT_BULK_VISIT_ITERATOR(FwdIterator)
  254. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  255. return table_.visit(first, last, f);
  256. }
  257. template <class F> size_type visit_all(F f)
  258. {
  259. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F)
  260. return table_.visit_all(f);
  261. }
  262. template <class F> size_type visit_all(F f) const
  263. {
  264. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  265. return table_.visit_all(f);
  266. }
  267. template <class F> size_type cvisit_all(F f) const
  268. {
  269. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  270. return table_.cvisit_all(f);
  271. }
  272. #if defined(BOOST_UNORDERED_PARALLEL_ALGORITHMS)
  273. template <class ExecPolicy, class F>
  274. typename std::enable_if<detail::is_execution_policy<ExecPolicy>::value,
  275. void>::type
  276. visit_all(ExecPolicy&& p, F f)
  277. {
  278. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F)
  279. BOOST_UNORDERED_STATIC_ASSERT_EXEC_POLICY(ExecPolicy)
  280. table_.visit_all(p, f);
  281. }
  282. template <class ExecPolicy, class F>
  283. typename std::enable_if<detail::is_execution_policy<ExecPolicy>::value,
  284. void>::type
  285. visit_all(ExecPolicy&& p, F f) const
  286. {
  287. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  288. BOOST_UNORDERED_STATIC_ASSERT_EXEC_POLICY(ExecPolicy)
  289. table_.visit_all(p, f);
  290. }
  291. template <class ExecPolicy, class F>
  292. typename std::enable_if<detail::is_execution_policy<ExecPolicy>::value,
  293. void>::type
  294. cvisit_all(ExecPolicy&& p, F f) const
  295. {
  296. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  297. BOOST_UNORDERED_STATIC_ASSERT_EXEC_POLICY(ExecPolicy)
  298. table_.cvisit_all(p, f);
  299. }
  300. #endif
  301. template <class F> bool visit_while(F f)
  302. {
  303. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F)
  304. return table_.visit_while(f);
  305. }
  306. template <class F> bool visit_while(F f) const
  307. {
  308. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  309. return table_.visit_while(f);
  310. }
  311. template <class F> bool cvisit_while(F f) const
  312. {
  313. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  314. return table_.cvisit_while(f);
  315. }
  316. #if defined(BOOST_UNORDERED_PARALLEL_ALGORITHMS)
  317. template <class ExecPolicy, class F>
  318. typename std::enable_if<detail::is_execution_policy<ExecPolicy>::value,
  319. bool>::type
  320. visit_while(ExecPolicy&& p, F f)
  321. {
  322. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F)
  323. BOOST_UNORDERED_STATIC_ASSERT_EXEC_POLICY(ExecPolicy)
  324. return table_.visit_while(p, f);
  325. }
  326. template <class ExecPolicy, class F>
  327. typename std::enable_if<detail::is_execution_policy<ExecPolicy>::value,
  328. bool>::type
  329. visit_while(ExecPolicy&& p, F f) const
  330. {
  331. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  332. BOOST_UNORDERED_STATIC_ASSERT_EXEC_POLICY(ExecPolicy)
  333. return table_.visit_while(p, f);
  334. }
  335. template <class ExecPolicy, class F>
  336. typename std::enable_if<detail::is_execution_policy<ExecPolicy>::value,
  337. bool>::type
  338. cvisit_while(ExecPolicy&& p, F f) const
  339. {
  340. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  341. BOOST_UNORDERED_STATIC_ASSERT_EXEC_POLICY(ExecPolicy)
  342. return table_.cvisit_while(p, f);
  343. }
  344. #endif
  345. /// Modifiers
  346. ///
  347. template <class Ty>
  348. BOOST_FORCEINLINE auto insert(Ty&& value)
  349. -> decltype(table_.insert(std::forward<Ty>(value)))
  350. {
  351. return table_.insert(std::forward<Ty>(value));
  352. }
  353. BOOST_FORCEINLINE bool insert(init_type&& obj)
  354. {
  355. return table_.insert(std::move(obj));
  356. }
  357. template <class InputIterator>
  358. void insert(InputIterator begin, InputIterator end)
  359. {
  360. for (auto pos = begin; pos != end; ++pos) {
  361. table_.emplace(*pos);
  362. }
  363. }
  364. void insert(std::initializer_list<value_type> ilist)
  365. {
  366. this->insert(ilist.begin(), ilist.end());
  367. }
  368. template <class M>
  369. BOOST_FORCEINLINE bool insert_or_assign(key_type const& k, M&& obj)
  370. {
  371. return table_.try_emplace_or_visit(k, std::forward<M>(obj),
  372. [&](value_type& m) { m.second = std::forward<M>(obj); });
  373. }
  374. template <class M>
  375. BOOST_FORCEINLINE bool insert_or_assign(key_type&& k, M&& obj)
  376. {
  377. return table_.try_emplace_or_visit(std::move(k), std::forward<M>(obj),
  378. [&](value_type& m) { m.second = std::forward<M>(obj); });
  379. }
  380. template <class K, class M>
  381. BOOST_FORCEINLINE typename std::enable_if<
  382. detail::are_transparent<K, hasher, key_equal>::value, bool>::type
  383. insert_or_assign(K&& k, M&& obj)
  384. {
  385. return table_.try_emplace_or_visit(std::forward<K>(k),
  386. std::forward<M>(obj),
  387. [&](value_type& m) { m.second = std::forward<M>(obj); });
  388. }
  389. template <class Ty, class F>
  390. BOOST_FORCEINLINE auto insert_or_visit(Ty&& value, F f)
  391. -> decltype(table_.insert_or_visit(std::forward<Ty>(value), f))
  392. {
  393. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F)
  394. return table_.insert_or_visit(std::forward<Ty>(value), f);
  395. }
  396. template <class F>
  397. BOOST_FORCEINLINE bool insert_or_visit(init_type&& obj, F f)
  398. {
  399. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F)
  400. return table_.insert_or_visit(std::move(obj), f);
  401. }
  402. template <class InputIterator, class F>
  403. void insert_or_visit(InputIterator first, InputIterator last, F f)
  404. {
  405. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F)
  406. for (; first != last; ++first) {
  407. table_.emplace_or_visit(*first, f);
  408. }
  409. }
  410. template <class F>
  411. void insert_or_visit(std::initializer_list<value_type> ilist, F f)
  412. {
  413. BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F)
  414. this->insert_or_visit(ilist.begin(), ilist.end(), f);
  415. }
  416. template <class Ty, class F>
  417. BOOST_FORCEINLINE auto insert_or_cvisit(Ty&& value, F f)
  418. -> decltype(table_.insert_or_cvisit(std::forward<Ty>(value), f))
  419. {
  420. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  421. return table_.insert_or_cvisit(std::forward<Ty>(value), f);
  422. }
  423. template <class F>
  424. BOOST_FORCEINLINE bool insert_or_cvisit(init_type&& obj, F f)
  425. {
  426. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  427. return table_.insert_or_cvisit(std::move(obj), f);
  428. }
  429. template <class InputIterator, class F>
  430. void insert_or_cvisit(InputIterator first, InputIterator last, F f)
  431. {
  432. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  433. for (; first != last; ++first) {
  434. table_.emplace_or_cvisit(*first, f);
  435. }
  436. }
  437. template <class F>
  438. void insert_or_cvisit(std::initializer_list<value_type> ilist, F f)
  439. {
  440. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  441. this->insert_or_cvisit(ilist.begin(), ilist.end(), f);
  442. }
  443. template <class... Args> BOOST_FORCEINLINE bool emplace(Args&&... args)
  444. {
  445. return table_.emplace(std::forward<Args>(args)...);
  446. }
  447. template <class Arg, class... Args>
  448. BOOST_FORCEINLINE bool emplace_or_visit(Arg&& arg, Args&&... args)
  449. {
  450. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_INVOCABLE(Arg, Args...)
  451. return table_.emplace_or_visit(
  452. std::forward<Arg>(arg), std::forward<Args>(args)...);
  453. }
  454. template <class Arg, class... Args>
  455. BOOST_FORCEINLINE bool emplace_or_cvisit(Arg&& arg, Args&&... args)
  456. {
  457. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_CONST_INVOCABLE(Arg, Args...)
  458. return table_.emplace_or_cvisit(
  459. std::forward<Arg>(arg), std::forward<Args>(args)...);
  460. }
  461. template <class... Args>
  462. BOOST_FORCEINLINE bool try_emplace(key_type const& k, Args&&... args)
  463. {
  464. return table_.try_emplace(k, std::forward<Args>(args)...);
  465. }
  466. template <class... Args>
  467. BOOST_FORCEINLINE bool try_emplace(key_type&& k, Args&&... args)
  468. {
  469. return table_.try_emplace(std::move(k), std::forward<Args>(args)...);
  470. }
  471. template <class K, class... Args>
  472. BOOST_FORCEINLINE typename std::enable_if<
  473. detail::are_transparent<K, hasher, key_equal>::value, bool>::type
  474. try_emplace(K&& k, Args&&... args)
  475. {
  476. return table_.try_emplace(
  477. std::forward<K>(k), std::forward<Args>(args)...);
  478. }
  479. template <class Arg, class... Args>
  480. BOOST_FORCEINLINE bool try_emplace_or_visit(
  481. key_type const& k, Arg&& arg, Args&&... args)
  482. {
  483. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_INVOCABLE(Arg, Args...)
  484. return table_.try_emplace_or_visit(
  485. k, std::forward<Arg>(arg), std::forward<Args>(args)...);
  486. }
  487. template <class Arg, class... Args>
  488. BOOST_FORCEINLINE bool try_emplace_or_cvisit(
  489. key_type const& k, Arg&& arg, Args&&... args)
  490. {
  491. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_CONST_INVOCABLE(Arg, Args...)
  492. return table_.try_emplace_or_cvisit(
  493. k, std::forward<Arg>(arg), std::forward<Args>(args)...);
  494. }
  495. template <class Arg, class... Args>
  496. BOOST_FORCEINLINE bool try_emplace_or_visit(
  497. key_type&& k, Arg&& arg, Args&&... args)
  498. {
  499. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_INVOCABLE(Arg, Args...)
  500. return table_.try_emplace_or_visit(
  501. std::move(k), std::forward<Arg>(arg), std::forward<Args>(args)...);
  502. }
  503. template <class Arg, class... Args>
  504. BOOST_FORCEINLINE bool try_emplace_or_cvisit(
  505. key_type&& k, Arg&& arg, Args&&... args)
  506. {
  507. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_CONST_INVOCABLE(Arg, Args...)
  508. return table_.try_emplace_or_cvisit(
  509. std::move(k), std::forward<Arg>(arg), std::forward<Args>(args)...);
  510. }
  511. template <class K, class Arg, class... Args>
  512. BOOST_FORCEINLINE bool try_emplace_or_visit(
  513. K&& k, Arg&& arg, Args&&... args)
  514. {
  515. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_INVOCABLE(Arg, Args...)
  516. return table_.try_emplace_or_visit(std::forward<K>(k),
  517. std::forward<Arg>(arg), std::forward<Args>(args)...);
  518. }
  519. template <class K, class Arg, class... Args>
  520. BOOST_FORCEINLINE bool try_emplace_or_cvisit(
  521. K&& k, Arg&& arg, Args&&... args)
  522. {
  523. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_CONST_INVOCABLE(Arg, Args...)
  524. return table_.try_emplace_or_cvisit(std::forward<K>(k),
  525. std::forward<Arg>(arg), std::forward<Args>(args)...);
  526. }
  527. BOOST_FORCEINLINE size_type erase(key_type const& k)
  528. {
  529. return table_.erase(k);
  530. }
  531. template <class K>
  532. BOOST_FORCEINLINE typename std::enable_if<
  533. detail::are_transparent<K, hasher, key_equal>::value, size_type>::type
  534. erase(K&& k)
  535. {
  536. return table_.erase(std::forward<K>(k));
  537. }
  538. template <class F>
  539. BOOST_FORCEINLINE size_type erase_if(key_type const& k, F f)
  540. {
  541. return table_.erase_if(k, f);
  542. }
  543. template <class K, class F>
  544. BOOST_FORCEINLINE typename std::enable_if<
  545. detail::are_transparent<K, hasher, key_equal>::value &&
  546. !detail::is_execution_policy<K>::value,
  547. size_type>::type
  548. erase_if(K&& k, F f)
  549. {
  550. return table_.erase_if(std::forward<K>(k), f);
  551. }
  552. #if defined(BOOST_UNORDERED_PARALLEL_ALGORITHMS)
  553. template <class ExecPolicy, class F>
  554. typename std::enable_if<detail::is_execution_policy<ExecPolicy>::value,
  555. void>::type
  556. erase_if(ExecPolicy&& p, F f)
  557. {
  558. BOOST_UNORDERED_STATIC_ASSERT_EXEC_POLICY(ExecPolicy)
  559. table_.erase_if(p, f);
  560. }
  561. #endif
  562. template <class F> size_type erase_if(F f) { return table_.erase_if(f); }
  563. void swap(concurrent_flat_map& other) noexcept(
  564. boost::allocator_is_always_equal<Allocator>::type::value ||
  565. boost::allocator_propagate_on_container_swap<Allocator>::type::value)
  566. {
  567. return table_.swap(other.table_);
  568. }
  569. void clear() noexcept { table_.clear(); }
  570. template <typename H2, typename P2>
  571. size_type merge(concurrent_flat_map<Key, T, H2, P2, Allocator>& x)
  572. {
  573. BOOST_ASSERT(get_allocator() == x.get_allocator());
  574. return table_.merge(x.table_);
  575. }
  576. template <typename H2, typename P2>
  577. size_type merge(concurrent_flat_map<Key, T, H2, P2, Allocator>&& x)
  578. {
  579. return merge(x);
  580. }
  581. BOOST_FORCEINLINE size_type count(key_type const& k) const
  582. {
  583. return table_.count(k);
  584. }
  585. template <class K>
  586. BOOST_FORCEINLINE typename std::enable_if<
  587. detail::are_transparent<K, hasher, key_equal>::value, size_type>::type
  588. count(K const& k)
  589. {
  590. return table_.count(k);
  591. }
  592. BOOST_FORCEINLINE bool contains(key_type const& k) const
  593. {
  594. return table_.contains(k);
  595. }
  596. template <class K>
  597. BOOST_FORCEINLINE typename std::enable_if<
  598. detail::are_transparent<K, hasher, key_equal>::value, bool>::type
  599. contains(K const& k) const
  600. {
  601. return table_.contains(k);
  602. }
  603. /// Hash Policy
  604. ///
  605. size_type bucket_count() const noexcept { return table_.capacity(); }
  606. float load_factor() const noexcept { return table_.load_factor(); }
  607. float max_load_factor() const noexcept
  608. {
  609. return table_.max_load_factor();
  610. }
  611. void max_load_factor(float) {}
  612. size_type max_load() const noexcept { return table_.max_load(); }
  613. void rehash(size_type n) { table_.rehash(n); }
  614. void reserve(size_type n) { table_.reserve(n); }
  615. #if defined(BOOST_UNORDERED_ENABLE_STATS)
  616. /// Stats
  617. ///
  618. stats get_stats() const { return table_.get_stats(); }
  619. void reset_stats() noexcept { table_.reset_stats(); }
  620. #endif
  621. /// Observers
  622. ///
  623. allocator_type get_allocator() const noexcept
  624. {
  625. return table_.get_allocator();
  626. }
  627. hasher hash_function() const { return table_.hash_function(); }
  628. key_equal key_eq() const { return table_.key_eq(); }
  629. };
  630. template <class Key, class T, class Hash, class KeyEqual, class Allocator>
  631. bool operator==(
  632. concurrent_flat_map<Key, T, Hash, KeyEqual, Allocator> const& lhs,
  633. concurrent_flat_map<Key, T, Hash, KeyEqual, Allocator> const& rhs)
  634. {
  635. return lhs.table_ == rhs.table_;
  636. }
  637. template <class Key, class T, class Hash, class KeyEqual, class Allocator>
  638. bool operator!=(
  639. concurrent_flat_map<Key, T, Hash, KeyEqual, Allocator> const& lhs,
  640. concurrent_flat_map<Key, T, Hash, KeyEqual, Allocator> const& rhs)
  641. {
  642. return !(lhs == rhs);
  643. }
  644. template <class Key, class T, class Hash, class Pred, class Alloc>
  645. void swap(concurrent_flat_map<Key, T, Hash, Pred, Alloc>& x,
  646. concurrent_flat_map<Key, T, Hash, Pred, Alloc>& y)
  647. noexcept(noexcept(x.swap(y)))
  648. {
  649. x.swap(y);
  650. }
  651. template <class K, class T, class H, class P, class A, class Predicate>
  652. typename concurrent_flat_map<K, T, H, P, A>::size_type erase_if(
  653. concurrent_flat_map<K, T, H, P, A>& c, Predicate pred)
  654. {
  655. return c.table_.erase_if(pred);
  656. }
  657. template<class Archive, class K, class V, class H, class KE, class A>
  658. void serialize(
  659. Archive& ar, concurrent_flat_map<K, V, H, KE, A>& c, unsigned int)
  660. {
  661. ar & core::make_nvp("table",c.table_);
  662. }
  663. #if BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES
  664. template <class InputIterator,
  665. class Hash =
  666. boost::hash<boost::unordered::detail::iter_key_t<InputIterator> >,
  667. class Pred =
  668. std::equal_to<boost::unordered::detail::iter_key_t<InputIterator> >,
  669. class Allocator = std::allocator<
  670. boost::unordered::detail::iter_to_alloc_t<InputIterator> >,
  671. class = std::enable_if_t<detail::is_input_iterator_v<InputIterator> >,
  672. class = std::enable_if_t<detail::is_hash_v<Hash> >,
  673. class = std::enable_if_t<detail::is_pred_v<Pred> >,
  674. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  675. concurrent_flat_map(InputIterator, InputIterator,
  676. std::size_t = boost::unordered::detail::foa::default_bucket_count,
  677. Hash = Hash(), Pred = Pred(), Allocator = Allocator())
  678. -> concurrent_flat_map<
  679. boost::unordered::detail::iter_key_t<InputIterator>,
  680. boost::unordered::detail::iter_val_t<InputIterator>, Hash, Pred,
  681. Allocator>;
  682. template <class Key, class T,
  683. class Hash = boost::hash<std::remove_const_t<Key> >,
  684. class Pred = std::equal_to<std::remove_const_t<Key> >,
  685. class Allocator = std::allocator<std::pair<const Key, T> >,
  686. class = std::enable_if_t<detail::is_hash_v<Hash> >,
  687. class = std::enable_if_t<detail::is_pred_v<Pred> >,
  688. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  689. concurrent_flat_map(std::initializer_list<std::pair<Key, T> >,
  690. std::size_t = boost::unordered::detail::foa::default_bucket_count,
  691. Hash = Hash(), Pred = Pred(), Allocator = Allocator())
  692. -> concurrent_flat_map<std::remove_const_t<Key>, T, Hash, Pred,
  693. Allocator>;
  694. template <class InputIterator, class Allocator,
  695. class = std::enable_if_t<detail::is_input_iterator_v<InputIterator> >,
  696. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  697. concurrent_flat_map(InputIterator, InputIterator, std::size_t, Allocator)
  698. -> concurrent_flat_map<
  699. boost::unordered::detail::iter_key_t<InputIterator>,
  700. boost::unordered::detail::iter_val_t<InputIterator>,
  701. boost::hash<boost::unordered::detail::iter_key_t<InputIterator> >,
  702. std::equal_to<boost::unordered::detail::iter_key_t<InputIterator> >,
  703. Allocator>;
  704. template <class InputIterator, class Allocator,
  705. class = std::enable_if_t<detail::is_input_iterator_v<InputIterator> >,
  706. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  707. concurrent_flat_map(InputIterator, InputIterator, Allocator)
  708. -> concurrent_flat_map<
  709. boost::unordered::detail::iter_key_t<InputIterator>,
  710. boost::unordered::detail::iter_val_t<InputIterator>,
  711. boost::hash<boost::unordered::detail::iter_key_t<InputIterator> >,
  712. std::equal_to<boost::unordered::detail::iter_key_t<InputIterator> >,
  713. Allocator>;
  714. template <class InputIterator, class Hash, class Allocator,
  715. class = std::enable_if_t<detail::is_hash_v<Hash> >,
  716. class = std::enable_if_t<detail::is_input_iterator_v<InputIterator> >,
  717. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  718. concurrent_flat_map(
  719. InputIterator, InputIterator, std::size_t, Hash, Allocator)
  720. -> concurrent_flat_map<
  721. boost::unordered::detail::iter_key_t<InputIterator>,
  722. boost::unordered::detail::iter_val_t<InputIterator>, Hash,
  723. std::equal_to<boost::unordered::detail::iter_key_t<InputIterator> >,
  724. Allocator>;
  725. template <class Key, class T, class Allocator,
  726. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  727. concurrent_flat_map(std::initializer_list<std::pair<Key, T> >, std::size_t,
  728. Allocator) -> concurrent_flat_map<std::remove_const_t<Key>, T,
  729. boost::hash<std::remove_const_t<Key> >,
  730. std::equal_to<std::remove_const_t<Key> >, Allocator>;
  731. template <class Key, class T, class Allocator,
  732. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  733. concurrent_flat_map(std::initializer_list<std::pair<Key, T> >, Allocator)
  734. -> concurrent_flat_map<std::remove_const_t<Key>, T,
  735. boost::hash<std::remove_const_t<Key> >,
  736. std::equal_to<std::remove_const_t<Key> >, Allocator>;
  737. template <class Key, class T, class Hash, class Allocator,
  738. class = std::enable_if_t<detail::is_hash_v<Hash> >,
  739. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  740. concurrent_flat_map(std::initializer_list<std::pair<Key, T> >, std::size_t,
  741. Hash, Allocator) -> concurrent_flat_map<std::remove_const_t<Key>, T,
  742. Hash, std::equal_to<std::remove_const_t<Key> >, Allocator>;
  743. #endif
  744. } // namespace unordered
  745. } // namespace boost
  746. #endif // BOOST_UNORDERED_CONCURRENT_FLAT_MAP_HPP