concurrent_flat_map.hpp 29 KB

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