concurrent_flat_set.hpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. /* Fast open-addressing concurrent hashset.
  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_SET_HPP
  12. #define BOOST_UNORDERED_CONCURRENT_FLAT_SET_HPP
  13. #include <boost/unordered/concurrent_flat_set_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_set_types.hpp>
  17. #include <boost/unordered/detail/type_traits.hpp>
  18. #include <boost/unordered/unordered_flat_set_fwd.hpp>
  19. #include <boost/container_hash/hash.hpp>
  20. #include <boost/core/allocator_access.hpp>
  21. #include <boost/core/serialization.hpp>
  22. #include <utility>
  23. namespace boost {
  24. namespace unordered {
  25. template <class Key, class Hash, class Pred, class Allocator>
  26. class concurrent_flat_set
  27. {
  28. private:
  29. template <class Key2, class Hash2, class Pred2, class Allocator2>
  30. friend class concurrent_flat_set;
  31. template <class Key2, class Hash2, class Pred2, class Allocator2>
  32. friend class unordered_flat_set;
  33. using type_policy = detail::foa::flat_set_types<Key>;
  34. using table_type =
  35. detail::foa::concurrent_table<type_policy, Hash, Pred, Allocator>;
  36. table_type table_;
  37. template <class K, class H, class KE, class A>
  38. bool friend operator==(concurrent_flat_set<K, H, KE, A> const& lhs,
  39. concurrent_flat_set<K, H, KE, A> const& rhs);
  40. template <class K, class H, class KE, class A, class Predicate>
  41. friend typename concurrent_flat_set<K, H, KE, A>::size_type erase_if(
  42. concurrent_flat_set<K, H, KE, A>& set, Predicate pred);
  43. template<class Archive, class K, class H, class KE, class A>
  44. friend void serialize(
  45. Archive& ar, concurrent_flat_set<K, H, KE, A>& c,
  46. unsigned int version);
  47. public:
  48. using key_type = Key;
  49. using value_type = typename type_policy::value_type;
  50. using init_type = typename type_policy::init_type;
  51. using size_type = std::size_t;
  52. using difference_type = std::ptrdiff_t;
  53. using hasher = typename boost::unordered::detail::type_identity<Hash>::type;
  54. using key_equal = typename boost::unordered::detail::type_identity<Pred>::type;
  55. using allocator_type = typename boost::unordered::detail::type_identity<Allocator>::type;
  56. using reference = value_type&;
  57. using const_reference = value_type const&;
  58. using pointer = typename boost::allocator_pointer<allocator_type>::type;
  59. using const_pointer =
  60. typename boost::allocator_const_pointer<allocator_type>::type;
  61. static constexpr size_type bulk_visit_size = table_type::bulk_visit_size;
  62. #if defined(BOOST_UNORDERED_ENABLE_STATS)
  63. using stats = typename table_type::stats;
  64. #endif
  65. concurrent_flat_set()
  66. : concurrent_flat_set(detail::foa::default_bucket_count)
  67. {
  68. }
  69. explicit concurrent_flat_set(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_set(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_set(concurrent_flat_set const& rhs)
  85. : table_(rhs.table_,
  86. boost::allocator_select_on_container_copy_construction(
  87. rhs.get_allocator()))
  88. {
  89. }
  90. concurrent_flat_set(concurrent_flat_set&& rhs)
  91. : table_(std::move(rhs.table_))
  92. {
  93. }
  94. template <class InputIterator>
  95. concurrent_flat_set(
  96. InputIterator f, InputIterator l, allocator_type const& a)
  97. : concurrent_flat_set(f, l, 0, hasher(), key_equal(), a)
  98. {
  99. }
  100. explicit concurrent_flat_set(allocator_type const& a)
  101. : table_(detail::foa::default_bucket_count, hasher(), key_equal(), a)
  102. {
  103. }
  104. concurrent_flat_set(
  105. concurrent_flat_set const& rhs, allocator_type const& a)
  106. : table_(rhs.table_, a)
  107. {
  108. }
  109. concurrent_flat_set(concurrent_flat_set&& rhs, allocator_type const& a)
  110. : table_(std::move(rhs.table_), a)
  111. {
  112. }
  113. concurrent_flat_set(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_set(n, hf, eql, a)
  118. {
  119. this->insert(il.begin(), il.end());
  120. }
  121. concurrent_flat_set(size_type n, const allocator_type& a)
  122. : concurrent_flat_set(n, hasher(), key_equal(), a)
  123. {
  124. }
  125. concurrent_flat_set(
  126. size_type n, const hasher& hf, const allocator_type& a)
  127. : concurrent_flat_set(n, hf, key_equal(), a)
  128. {
  129. }
  130. template <typename InputIterator>
  131. concurrent_flat_set(
  132. InputIterator f, InputIterator l, size_type n, const allocator_type& a)
  133. : concurrent_flat_set(f, l, n, hasher(), key_equal(), a)
  134. {
  135. }
  136. template <typename InputIterator>
  137. concurrent_flat_set(InputIterator f, InputIterator l, size_type n,
  138. const hasher& hf, const allocator_type& a)
  139. : concurrent_flat_set(f, l, n, hf, key_equal(), a)
  140. {
  141. }
  142. concurrent_flat_set(
  143. std::initializer_list<value_type> il, const allocator_type& a)
  144. : concurrent_flat_set(
  145. il, detail::foa::default_bucket_count, hasher(), key_equal(), a)
  146. {
  147. }
  148. concurrent_flat_set(std::initializer_list<value_type> il, size_type n,
  149. const allocator_type& a)
  150. : concurrent_flat_set(il, n, hasher(), key_equal(), a)
  151. {
  152. }
  153. concurrent_flat_set(std::initializer_list<value_type> il, size_type n,
  154. const hasher& hf, const allocator_type& a)
  155. : concurrent_flat_set(il, n, hf, key_equal(), a)
  156. {
  157. }
  158. template <bool avoid_explicit_instantiation = true>
  159. concurrent_flat_set(
  160. unordered_flat_set<Key, Hash, Pred, Allocator>&& other)
  161. : table_(std::move(other.table_))
  162. {
  163. }
  164. ~concurrent_flat_set() = default;
  165. concurrent_flat_set& operator=(concurrent_flat_set const& rhs)
  166. {
  167. table_ = rhs.table_;
  168. return *this;
  169. }
  170. concurrent_flat_set& operator=(concurrent_flat_set&& rhs)
  171. noexcept(boost::allocator_is_always_equal<Allocator>::type::value ||
  172. boost::allocator_propagate_on_container_move_assignment<
  173. Allocator>::type::value)
  174. {
  175. table_ = std::move(rhs.table_);
  176. return *this;
  177. }
  178. concurrent_flat_set& 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_CONST_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_CONST_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_CONST_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_CONST_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_CONST_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_CONST_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_CONST_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. BOOST_FORCEINLINE bool insert(value_type const& obj)
  348. {
  349. return table_.insert(obj);
  350. }
  351. BOOST_FORCEINLINE bool insert(value_type&& obj)
  352. {
  353. return table_.insert(std::move(obj));
  354. }
  355. template <class K>
  356. BOOST_FORCEINLINE typename std::enable_if<
  357. detail::are_transparent<K, hasher, key_equal>::value,
  358. bool >::type
  359. insert(K&& k)
  360. {
  361. return table_.try_emplace(std::forward<K>(k));
  362. }
  363. template <class InputIterator>
  364. size_type insert(InputIterator begin, InputIterator end)
  365. {
  366. size_type count_elements = 0;
  367. for (auto pos = begin; pos != end; ++pos, ++count_elements) {
  368. table_.emplace(*pos);
  369. }
  370. return count_elements;
  371. }
  372. size_type insert(std::initializer_list<value_type> ilist)
  373. {
  374. return this->insert(ilist.begin(), ilist.end());
  375. }
  376. template <class F>
  377. BOOST_FORCEINLINE bool insert_or_visit(value_type const& obj, F f)
  378. {
  379. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  380. return table_.insert_or_visit(obj, f);
  381. }
  382. template <class F>
  383. BOOST_FORCEINLINE bool insert_or_visit(value_type&& obj, F f)
  384. {
  385. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  386. return table_.insert_or_visit(std::move(obj), f);
  387. }
  388. template <class K, class F>
  389. BOOST_FORCEINLINE typename std::enable_if<
  390. detail::are_transparent<K, hasher, key_equal>::value,
  391. bool >::type
  392. insert_or_visit(K&& k, F f)
  393. {
  394. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  395. return table_.try_emplace_or_visit(std::forward<K>(k), f);
  396. }
  397. template <class InputIterator, class F>
  398. size_type insert_or_visit(InputIterator first, InputIterator last, F f)
  399. {
  400. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  401. size_type count_elements = 0;
  402. for (; first != last; ++first, ++count_elements) {
  403. table_.emplace_or_visit(*first, f);
  404. }
  405. return count_elements;
  406. }
  407. template <class F>
  408. size_type insert_or_visit(std::initializer_list<value_type> ilist, F f)
  409. {
  410. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  411. return this->insert_or_visit(ilist.begin(), ilist.end(), std::ref(f));
  412. }
  413. template <class F>
  414. BOOST_FORCEINLINE bool insert_or_cvisit(value_type const& obj, F f)
  415. {
  416. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  417. return table_.insert_or_cvisit(obj, f);
  418. }
  419. template <class F>
  420. BOOST_FORCEINLINE bool insert_or_cvisit(value_type&& obj, F f)
  421. {
  422. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  423. return table_.insert_or_cvisit(std::move(obj), f);
  424. }
  425. template <class K, class F>
  426. BOOST_FORCEINLINE typename std::enable_if<
  427. detail::are_transparent<K, hasher, key_equal>::value,
  428. bool >::type
  429. insert_or_cvisit(K&& k, F f)
  430. {
  431. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
  432. return table_.try_emplace_or_cvisit(std::forward<K>(k), 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 F1, class F2>
  451. BOOST_FORCEINLINE bool insert_and_visit(
  452. value_type const& obj, F1 f1, F2 f2)
  453. {
  454. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F1)
  455. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F2)
  456. return table_.insert_and_visit(obj, f1, f2);
  457. }
  458. template <class F1, class F2>
  459. BOOST_FORCEINLINE bool insert_and_visit(value_type&& obj, F1 f1, F2 f2)
  460. {
  461. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F1)
  462. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F2)
  463. return table_.insert_and_visit(std::move(obj), f1, f2);
  464. }
  465. template <class K, class F1, class F2>
  466. BOOST_FORCEINLINE typename std::enable_if<
  467. detail::are_transparent<K, hasher, key_equal>::value,
  468. bool >::type
  469. insert_and_visit(K&& k, F1 f1, F2 f2)
  470. {
  471. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F1)
  472. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F2)
  473. return table_.try_emplace_and_visit(std::forward<K>(k), f1, f2);
  474. }
  475. template <class InputIterator, class F1, class F2>
  476. size_type insert_and_visit(
  477. InputIterator first, InputIterator last, F1 f1, F2 f2)
  478. {
  479. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F1)
  480. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F2)
  481. size_type count_elements = 0;
  482. for (; first != last; ++first, ++count_elements) {
  483. table_.emplace_and_visit(*first, f1, f2);
  484. }
  485. return count_elements;
  486. }
  487. template <class F1, class F2>
  488. size_type insert_and_visit(std::initializer_list<value_type> ilist, F1 f1, F2 f2)
  489. {
  490. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F1)
  491. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F2)
  492. return this->insert_and_visit(
  493. ilist.begin(), ilist.end(), std::ref(f1), std::ref(f2));
  494. }
  495. template <class F1, class F2>
  496. BOOST_FORCEINLINE bool insert_and_cvisit(
  497. value_type const& obj, F1 f1, F2 f2)
  498. {
  499. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F1)
  500. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F2)
  501. return table_.insert_and_cvisit(obj, f1, f2);
  502. }
  503. template <class F1, class F2>
  504. BOOST_FORCEINLINE bool insert_and_cvisit(value_type&& obj, F1 f1, F2 f2)
  505. {
  506. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F1)
  507. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F2)
  508. return table_.insert_and_cvisit(std::move(obj), f1, f2);
  509. }
  510. template <class K, class F1, class F2>
  511. BOOST_FORCEINLINE typename std::enable_if<
  512. detail::are_transparent<K, hasher, key_equal>::value,
  513. bool >::type
  514. insert_and_cvisit(K&& k, F1 f1, F2 f2)
  515. {
  516. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F1)
  517. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F2)
  518. return table_.try_emplace_and_cvisit(std::forward<K>(k), f1, f2);
  519. }
  520. template <class InputIterator, class F1, class F2>
  521. size_type insert_and_cvisit(
  522. InputIterator first, InputIterator last, F1 f1, F2 f2)
  523. {
  524. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F1)
  525. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F2)
  526. size_type count_elements = 0;
  527. for (; first != last; ++first, ++count_elements) {
  528. table_.emplace_and_cvisit(*first, f1, f2);
  529. }
  530. return count_elements;
  531. }
  532. template <class F1, class F2>
  533. size_type insert_and_cvisit(
  534. std::initializer_list<value_type> ilist, F1 f1, F2 f2)
  535. {
  536. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F1)
  537. BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F2)
  538. return this->insert_and_cvisit(
  539. ilist.begin(), ilist.end(), std::ref(f1), std::ref(f2));
  540. }
  541. template <class... Args> BOOST_FORCEINLINE bool emplace(Args&&... args)
  542. {
  543. return table_.emplace(std::forward<Args>(args)...);
  544. }
  545. template <class Arg, class... Args>
  546. BOOST_FORCEINLINE bool emplace_or_visit(Arg&& arg, Args&&... args)
  547. {
  548. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_CONST_INVOCABLE(Arg, Args...)
  549. return table_.emplace_or_visit(
  550. std::forward<Arg>(arg), std::forward<Args>(args)...);
  551. }
  552. template <class Arg, class... Args>
  553. BOOST_FORCEINLINE bool emplace_or_cvisit(Arg&& arg, Args&&... args)
  554. {
  555. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_CONST_INVOCABLE(Arg, Args...)
  556. return table_.emplace_or_cvisit(
  557. std::forward<Arg>(arg), std::forward<Args>(args)...);
  558. }
  559. template <class Arg1, class Arg2, class... Args>
  560. BOOST_FORCEINLINE bool emplace_and_visit(
  561. Arg1&& arg1, Arg2&& arg2, Args&&... args)
  562. {
  563. BOOST_UNORDERED_STATIC_ASSERT_PENULTIMATE_ARG_CONST_INVOCABLE(
  564. Arg1, Arg2, Args...)
  565. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_CONST_INVOCABLE(Arg2, Args...)
  566. return table_.emplace_and_visit(
  567. std::forward<Arg1>(arg1), std::forward<Arg2>(arg2),
  568. std::forward<Args>(args)...);
  569. }
  570. template <class Arg1, class Arg2, class... Args>
  571. BOOST_FORCEINLINE bool emplace_and_cvisit(
  572. Arg1&& arg1, Arg2&& arg2, Args&&... args)
  573. {
  574. BOOST_UNORDERED_STATIC_ASSERT_PENULTIMATE_ARG_CONST_INVOCABLE(
  575. Arg1, Arg2, Args...)
  576. BOOST_UNORDERED_STATIC_ASSERT_LAST_ARG_CONST_INVOCABLE(Arg2, Args...)
  577. return table_.emplace_and_cvisit(
  578. std::forward<Arg1>(arg1), std::forward<Arg2>(arg2),
  579. std::forward<Args>(args)...);
  580. }
  581. BOOST_FORCEINLINE size_type erase(key_type const& k)
  582. {
  583. return table_.erase(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. erase(K&& k)
  589. {
  590. return table_.erase(std::forward<K>(k));
  591. }
  592. template <class F>
  593. BOOST_FORCEINLINE size_type erase_if(key_type const& k, F f)
  594. {
  595. return table_.erase_if(k, f);
  596. }
  597. template <class K, class F>
  598. BOOST_FORCEINLINE typename std::enable_if<
  599. detail::are_transparent<K, hasher, key_equal>::value &&
  600. !detail::is_execution_policy<K>::value,
  601. size_type>::type
  602. erase_if(K&& k, F f)
  603. {
  604. return table_.erase_if(std::forward<K>(k), f);
  605. }
  606. #if defined(BOOST_UNORDERED_PARALLEL_ALGORITHMS)
  607. template <class ExecPolicy, class F>
  608. typename std::enable_if<detail::is_execution_policy<ExecPolicy>::value,
  609. void>::type
  610. erase_if(ExecPolicy&& p, F f)
  611. {
  612. BOOST_UNORDERED_STATIC_ASSERT_EXEC_POLICY(ExecPolicy)
  613. table_.erase_if(p, f);
  614. }
  615. #endif
  616. template <class F> size_type erase_if(F f) { return table_.erase_if(f); }
  617. void swap(concurrent_flat_set& other) noexcept(
  618. boost::allocator_is_always_equal<Allocator>::type::value ||
  619. boost::allocator_propagate_on_container_swap<Allocator>::type::value)
  620. {
  621. return table_.swap(other.table_);
  622. }
  623. void clear() noexcept { table_.clear(); }
  624. template <typename H2, typename P2>
  625. size_type merge(concurrent_flat_set<Key, H2, P2, Allocator>& x)
  626. {
  627. BOOST_ASSERT(get_allocator() == x.get_allocator());
  628. return table_.merge(x.table_);
  629. }
  630. template <typename H2, typename P2>
  631. size_type merge(concurrent_flat_set<Key, H2, P2, Allocator>&& x)
  632. {
  633. return merge(x);
  634. }
  635. BOOST_FORCEINLINE size_type count(key_type const& k) const
  636. {
  637. return table_.count(k);
  638. }
  639. template <class K>
  640. BOOST_FORCEINLINE typename std::enable_if<
  641. detail::are_transparent<K, hasher, key_equal>::value, size_type>::type
  642. count(K const& k)
  643. {
  644. return table_.count(k);
  645. }
  646. BOOST_FORCEINLINE bool contains(key_type const& k) const
  647. {
  648. return table_.contains(k);
  649. }
  650. template <class K>
  651. BOOST_FORCEINLINE typename std::enable_if<
  652. detail::are_transparent<K, hasher, key_equal>::value, bool>::type
  653. contains(K const& k) const
  654. {
  655. return table_.contains(k);
  656. }
  657. /// Hash Policy
  658. ///
  659. size_type bucket_count() const noexcept { return table_.capacity(); }
  660. float load_factor() const noexcept { return table_.load_factor(); }
  661. float max_load_factor() const noexcept
  662. {
  663. return table_.max_load_factor();
  664. }
  665. void max_load_factor(float) {}
  666. size_type max_load() const noexcept { return table_.max_load(); }
  667. void rehash(size_type n) { table_.rehash(n); }
  668. void reserve(size_type n) { table_.reserve(n); }
  669. #if defined(BOOST_UNORDERED_ENABLE_STATS)
  670. /// Stats
  671. ///
  672. stats get_stats() const { return table_.get_stats(); }
  673. void reset_stats() noexcept { table_.reset_stats(); }
  674. #endif
  675. /// Observers
  676. ///
  677. allocator_type get_allocator() const noexcept
  678. {
  679. return table_.get_allocator();
  680. }
  681. hasher hash_function() const { return table_.hash_function(); }
  682. key_equal key_eq() const { return table_.key_eq(); }
  683. };
  684. template <class Key, class Hash, class KeyEqual, class Allocator>
  685. bool operator==(
  686. concurrent_flat_set<Key, Hash, KeyEqual, Allocator> const& lhs,
  687. concurrent_flat_set<Key, Hash, KeyEqual, Allocator> const& rhs)
  688. {
  689. return lhs.table_ == rhs.table_;
  690. }
  691. template <class Key, class Hash, class KeyEqual, class Allocator>
  692. bool operator!=(
  693. concurrent_flat_set<Key, Hash, KeyEqual, Allocator> const& lhs,
  694. concurrent_flat_set<Key, Hash, KeyEqual, Allocator> const& rhs)
  695. {
  696. return !(lhs == rhs);
  697. }
  698. template <class Key, class Hash, class Pred, class Alloc>
  699. void swap(concurrent_flat_set<Key, Hash, Pred, Alloc>& x,
  700. concurrent_flat_set<Key, Hash, Pred, Alloc>& y)
  701. noexcept(noexcept(x.swap(y)))
  702. {
  703. x.swap(y);
  704. }
  705. template <class K, class H, class P, class A, class Predicate>
  706. typename concurrent_flat_set<K, H, P, A>::size_type erase_if(
  707. concurrent_flat_set<K, H, P, A>& c, Predicate pred)
  708. {
  709. return c.table_.erase_if(pred);
  710. }
  711. template<class Archive, class K, class H, class KE, class A>
  712. void serialize(
  713. Archive& ar, concurrent_flat_set<K, H, KE, A>& c, unsigned int)
  714. {
  715. ar & core::make_nvp("table",c.table_);
  716. }
  717. #if BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES
  718. template <class InputIterator,
  719. class Hash =
  720. boost::hash<typename std::iterator_traits<InputIterator>::value_type>,
  721. class Pred =
  722. std::equal_to<typename std::iterator_traits<InputIterator>::value_type>,
  723. class Allocator = std::allocator<
  724. typename std::iterator_traits<InputIterator>::value_type>,
  725. class = std::enable_if_t<detail::is_input_iterator_v<InputIterator> >,
  726. class = std::enable_if_t<detail::is_hash_v<Hash> >,
  727. class = std::enable_if_t<detail::is_pred_v<Pred> >,
  728. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  729. concurrent_flat_set(InputIterator, InputIterator,
  730. std::size_t = boost::unordered::detail::foa::default_bucket_count,
  731. Hash = Hash(), Pred = Pred(), Allocator = Allocator())
  732. -> concurrent_flat_set<
  733. typename std::iterator_traits<InputIterator>::value_type, Hash, Pred,
  734. Allocator>;
  735. template <class T, class Hash = boost::hash<T>,
  736. class Pred = std::equal_to<T>, class Allocator = std::allocator<T>,
  737. class = std::enable_if_t<detail::is_hash_v<Hash> >,
  738. class = std::enable_if_t<detail::is_pred_v<Pred> >,
  739. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  740. concurrent_flat_set(std::initializer_list<T>,
  741. std::size_t = boost::unordered::detail::foa::default_bucket_count,
  742. Hash = Hash(), Pred = Pred(), Allocator = Allocator())
  743. -> concurrent_flat_set< T, Hash, Pred, Allocator>;
  744. template <class InputIterator, class Allocator,
  745. class = std::enable_if_t<detail::is_input_iterator_v<InputIterator> >,
  746. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  747. concurrent_flat_set(InputIterator, InputIterator, std::size_t, Allocator)
  748. -> concurrent_flat_set<
  749. typename std::iterator_traits<InputIterator>::value_type,
  750. boost::hash<typename std::iterator_traits<InputIterator>::value_type>,
  751. std::equal_to<typename std::iterator_traits<InputIterator>::value_type>,
  752. Allocator>;
  753. template <class InputIterator, class Allocator,
  754. class = std::enable_if_t<detail::is_input_iterator_v<InputIterator> >,
  755. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  756. concurrent_flat_set(InputIterator, InputIterator, Allocator)
  757. -> concurrent_flat_set<
  758. typename std::iterator_traits<InputIterator>::value_type,
  759. boost::hash<typename std::iterator_traits<InputIterator>::value_type>,
  760. std::equal_to<typename std::iterator_traits<InputIterator>::value_type>,
  761. Allocator>;
  762. template <class InputIterator, class Hash, class Allocator,
  763. class = std::enable_if_t<detail::is_hash_v<Hash> >,
  764. class = std::enable_if_t<detail::is_input_iterator_v<InputIterator> >,
  765. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  766. concurrent_flat_set(
  767. InputIterator, InputIterator, std::size_t, Hash, Allocator)
  768. -> concurrent_flat_set<
  769. typename std::iterator_traits<InputIterator>::value_type, Hash,
  770. std::equal_to<typename std::iterator_traits<InputIterator>::value_type>,
  771. Allocator>;
  772. template <class T, class Allocator,
  773. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  774. concurrent_flat_set(std::initializer_list<T>, std::size_t, Allocator)
  775. -> concurrent_flat_set<T, boost::hash<T>,std::equal_to<T>, Allocator>;
  776. template <class T, class Allocator,
  777. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  778. concurrent_flat_set(std::initializer_list<T >, Allocator)
  779. -> concurrent_flat_set<T, boost::hash<T>, std::equal_to<T>, Allocator>;
  780. template <class T, class Hash, class Allocator,
  781. class = std::enable_if_t<detail::is_hash_v<Hash> >,
  782. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  783. concurrent_flat_set(std::initializer_list<T >, std::size_t,Hash, Allocator)
  784. -> concurrent_flat_set<T, Hash, std::equal_to<T>, Allocator>;
  785. #endif
  786. } // namespace unordered
  787. } // namespace boost
  788. #endif // BOOST_UNORDERED_CONCURRENT_FLAT_SET_HPP