table.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /* Fast open-addressing hash table.
  2. *
  3. * Copyright 2022-2023 Joaquin M Lopez Munoz.
  4. * Copyright 2023 Christian Mazakas.
  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_DETAIL_FOA_TABLE_HPP
  12. #define BOOST_UNORDERED_DETAIL_FOA_TABLE_HPP
  13. #include <boost/assert.hpp>
  14. #include <boost/config.hpp>
  15. #include <boost/config/workaround.hpp>
  16. #include <boost/core/serialization.hpp>
  17. #include <boost/unordered/detail/foa/core.hpp>
  18. #include <boost/unordered/detail/serialize_tracked_address.hpp>
  19. #include <cstddef>
  20. #include <iterator>
  21. #include <memory>
  22. #include <type_traits>
  23. #include <utility>
  24. namespace boost{
  25. namespace unordered{
  26. namespace detail{
  27. namespace foa{
  28. /* use plain integrals for group metadata storage */
  29. template<typename Integral>
  30. struct plain_integral
  31. {
  32. operator Integral()const{return n;}
  33. void operator=(Integral m){n=m;}
  34. #if BOOST_WORKAROUND(BOOST_GCC,>=50000 && BOOST_GCC<60000)
  35. void operator|=(Integral m){n=static_cast<Integral>(n|m);}
  36. void operator&=(Integral m){n=static_cast<Integral>(n&m);}
  37. #else
  38. void operator|=(Integral m){n|=m;}
  39. void operator&=(Integral m){n&=m;}
  40. #endif
  41. Integral n;
  42. };
  43. struct plain_size_control
  44. {
  45. std::size_t ml;
  46. std::size_t size;
  47. };
  48. template<typename,typename,typename,typename>
  49. class table;
  50. /* table_iterator keeps two pointers:
  51. *
  52. * - A pointer p to the element slot.
  53. * - A pointer pc to the n-th byte of the associated group metadata, where n
  54. * is the position of the element in the group.
  55. *
  56. * A simpler solution would have been to keep a pointer p to the element, a
  57. * pointer pg to the group, and the position n, but that would increase
  58. * sizeof(table_iterator) by 4/8 bytes. In order to make this compact
  59. * representation feasible, it is required that group objects are aligned
  60. * to their size, so that we can recover pg and n as
  61. *
  62. * - n = pc%sizeof(group)
  63. * - pg = pc-n
  64. *
  65. * (for explanatory purposes pg and pc are treated above as if they were memory
  66. * addresses rather than pointers).
  67. *
  68. * p = nullptr is conventionally used to mark end() iterators.
  69. */
  70. /* internal conversion from const_iterator to iterator */
  71. struct const_iterator_cast_tag{};
  72. template<typename TypePolicy,typename GroupPtr,bool Const>
  73. class table_iterator
  74. {
  75. using group_pointer_traits=boost::pointer_traits<GroupPtr>;
  76. using type_policy=TypePolicy;
  77. using table_element_type=typename type_policy::element_type;
  78. using group_type=typename group_pointer_traits::element_type;
  79. using table_element_pointer=
  80. typename group_pointer_traits::template rebind<table_element_type>;
  81. using char_pointer=
  82. typename group_pointer_traits::template rebind<unsigned char>;
  83. static constexpr auto N=group_type::N;
  84. static constexpr auto regular_layout=group_type::regular_layout;
  85. public:
  86. using difference_type=std::ptrdiff_t;
  87. using value_type=typename type_policy::value_type;
  88. using pointer=
  89. typename std::conditional<Const,value_type const*,value_type*>::type;
  90. using reference=
  91. typename std::conditional<Const,value_type const&,value_type&>::type;
  92. using iterator_category=std::forward_iterator_tag;
  93. using element_type=
  94. typename std::conditional<Const,value_type const,value_type>::type;
  95. table_iterator():pc_{nullptr},p_{nullptr}{};
  96. template<bool Const2,typename std::enable_if<!Const2>::type* =nullptr>
  97. table_iterator(const table_iterator<TypePolicy,GroupPtr,Const2>& x):
  98. pc_{x.pc_},p_{x.p_}{}
  99. table_iterator(
  100. const_iterator_cast_tag, const table_iterator<TypePolicy,GroupPtr,true>& x):
  101. pc_{x.pc_},p_{x.p_}{}
  102. inline reference operator*()const noexcept
  103. {return type_policy::value_from(*p());}
  104. inline pointer operator->()const noexcept
  105. {return std::addressof(type_policy::value_from(*p()));}
  106. inline table_iterator& operator++()noexcept{increment();return *this;}
  107. inline table_iterator operator++(int)noexcept
  108. {auto x=*this;increment();return x;}
  109. friend inline bool operator==(
  110. const table_iterator& x,const table_iterator& y)
  111. {return x.p()==y.p();}
  112. friend inline bool operator!=(
  113. const table_iterator& x,const table_iterator& y)
  114. {return !(x==y);}
  115. private:
  116. template<typename,typename,bool> friend class table_iterator;
  117. template<typename> friend class table_erase_return_type;
  118. template<typename,typename,typename,typename> friend class table;
  119. table_iterator(group_type* pg,std::size_t n,const table_element_type* ptet):
  120. pc_{to_pointer<char_pointer>(
  121. reinterpret_cast<unsigned char*>(const_cast<group_type*>(pg))+n)},
  122. p_{to_pointer<table_element_pointer>(const_cast<table_element_type*>(ptet))}
  123. {}
  124. unsigned char* pc()const noexcept{return boost::to_address(pc_);}
  125. table_element_type* p()const noexcept{return boost::to_address(p_);}
  126. inline void increment()noexcept
  127. {
  128. BOOST_ASSERT(p()!=nullptr);
  129. increment(std::integral_constant<bool,regular_layout>{});
  130. }
  131. inline void increment(std::true_type /* regular layout */)noexcept
  132. {
  133. using diff_type=
  134. typename boost::pointer_traits<char_pointer>::difference_type;
  135. for(;;){
  136. ++p_;
  137. if(reinterpret_cast<uintptr_t>(pc())%sizeof(group_type)==N-1){
  138. pc_+=static_cast<diff_type>(sizeof(group_type)-(N-1));
  139. break;
  140. }
  141. ++pc_;
  142. if(!group_type::is_occupied(pc()))continue;
  143. if(BOOST_UNLIKELY(group_type::is_sentinel(pc())))p_=nullptr;
  144. return;
  145. }
  146. for(;;){
  147. int mask=reinterpret_cast<group_type*>(pc())->match_occupied();
  148. if(mask!=0){
  149. auto n=unchecked_countr_zero(mask);
  150. if(BOOST_UNLIKELY(reinterpret_cast<group_type*>(pc())->is_sentinel(n))){
  151. p_=nullptr;
  152. }
  153. else{
  154. pc_+=static_cast<diff_type>(n);
  155. p_+=static_cast<diff_type>(n);
  156. }
  157. return;
  158. }
  159. pc_+=static_cast<diff_type>(sizeof(group_type));
  160. p_+=static_cast<diff_type>(N);
  161. }
  162. }
  163. inline void increment(std::false_type /* interleaved */)noexcept
  164. {
  165. using diff_type=
  166. typename boost::pointer_traits<char_pointer>::difference_type;
  167. std::size_t n0=reinterpret_cast<uintptr_t>(pc())%sizeof(group_type);
  168. pc_-=static_cast<diff_type>(n0);
  169. int mask=(
  170. reinterpret_cast<group_type*>(pc())->match_occupied()>>(n0+1))<<(n0+1);
  171. if(!mask){
  172. do{
  173. pc_+=sizeof(group_type);
  174. p_+=N;
  175. }
  176. while((mask=reinterpret_cast<group_type*>(pc())->match_occupied())==0);
  177. }
  178. auto n=unchecked_countr_zero(mask);
  179. if(BOOST_UNLIKELY(reinterpret_cast<group_type*>(pc())->is_sentinel(n))){
  180. p_=nullptr;
  181. }
  182. else{
  183. pc_+=static_cast<diff_type>(n);
  184. p_-=static_cast<diff_type>(n0);
  185. p_+=static_cast<diff_type>(n);
  186. }
  187. }
  188. template<typename Archive>
  189. friend void serialization_track(Archive& ar,const table_iterator& x)
  190. {
  191. if(x.p()){
  192. track_address(ar,x.pc_);
  193. track_address(ar,x.p_);
  194. }
  195. }
  196. friend class boost::serialization::access;
  197. template<typename Archive>
  198. void serialize(Archive& ar,unsigned int)
  199. {
  200. if(!p())pc_=nullptr;
  201. serialize_tracked_address(ar,pc_);
  202. serialize_tracked_address(ar,p_);
  203. }
  204. char_pointer pc_=nullptr;
  205. table_element_pointer p_=nullptr;
  206. };
  207. /* Returned by table::erase([const_]iterator) to avoid iterator increment
  208. * if discarded.
  209. */
  210. template<typename Iterator>
  211. class table_erase_return_type;
  212. template<typename TypePolicy,typename GroupPtr,bool Const>
  213. class table_erase_return_type<table_iterator<TypePolicy,GroupPtr,Const>>
  214. {
  215. using iterator=table_iterator<TypePolicy,GroupPtr,Const>;
  216. using const_iterator=table_iterator<TypePolicy,GroupPtr,true>;
  217. public:
  218. /* can't delete it because VS in pre-C++17 mode needs to see it for RVO */
  219. table_erase_return_type(const table_erase_return_type&);
  220. operator iterator()const noexcept
  221. {
  222. auto it=pos;
  223. it.increment(); /* valid even if *it was erased */
  224. return iterator(const_iterator_cast_tag{},it);
  225. }
  226. template<
  227. bool dependent_value=false,
  228. typename std::enable_if<!Const||dependent_value>::type* =nullptr
  229. >
  230. operator const_iterator()const noexcept{return this->operator iterator();}
  231. private:
  232. template<typename,typename,typename,typename> friend class table;
  233. table_erase_return_type(const_iterator pos_):pos{pos_}{}
  234. table_erase_return_type& operator=(const table_erase_return_type&)=delete;
  235. const_iterator pos;
  236. };
  237. /* foa::table interface departs in a number of ways from that of C++ unordered
  238. * associative containers because it's not for end-user consumption
  239. * (boost::unordered_(flat|node)_(map|set) wrappers complete it as
  240. * appropriate).
  241. *
  242. * The table supports two main modes of operation: flat and node-based. In the
  243. * flat case, buckets directly store elements. For node-based, buckets store
  244. * pointers to individually heap-allocated elements.
  245. *
  246. * For both flat and node-based:
  247. *
  248. * - begin() is not O(1).
  249. * - No bucket API.
  250. * - Load factor is fixed and can't be set by the user.
  251. *
  252. * For flat only:
  253. *
  254. * - value_type must be moveable.
  255. * - Pointer stability is not kept under rehashing.
  256. * - No extract API.
  257. *
  258. * try_emplace, erase and find support heterogeneous lookup by default,
  259. * that is, without checking for any ::is_transparent typedefs --the
  260. * checking is done by boost::unordered_(flat|node)_(map|set).
  261. */
  262. template<typename,typename,typename,typename>
  263. class concurrent_table; /* concurrent/non-concurrent interop */
  264. template <typename TypePolicy,typename Hash,typename Pred,typename Allocator>
  265. using table_core_impl=
  266. table_core<TypePolicy,group15<plain_integral>,table_arrays,
  267. plain_size_control,Hash,Pred,Allocator>;
  268. #include <boost/unordered/detail/foa/ignore_wshadow.hpp>
  269. #if defined(BOOST_MSVC)
  270. #pragma warning(push)
  271. #pragma warning(disable:4714) /* marked as __forceinline not inlined */
  272. #endif
  273. template<typename TypePolicy,typename Hash,typename Pred,typename Allocator>
  274. class table:table_core_impl<TypePolicy,Hash,Pred,Allocator>
  275. {
  276. using super=table_core_impl<TypePolicy,Hash,Pred,Allocator>;
  277. using type_policy=typename super::type_policy;
  278. using group_type=typename super::group_type;
  279. using super::N;
  280. using prober=typename super::prober;
  281. using arrays_type=typename super::arrays_type;
  282. using size_ctrl_type=typename super::size_ctrl_type;
  283. using locator=typename super::locator;
  284. using compatible_concurrent_table=
  285. concurrent_table<TypePolicy,Hash,Pred,Allocator>;
  286. using group_type_pointer=typename boost::pointer_traits<
  287. typename boost::allocator_pointer<Allocator>::type
  288. >::template rebind<group_type>;
  289. friend compatible_concurrent_table;
  290. public:
  291. using key_type=typename super::key_type;
  292. using init_type=typename super::init_type;
  293. using value_type=typename super::value_type;
  294. using element_type=typename super::element_type;
  295. private:
  296. static constexpr bool has_mutable_iterator=
  297. !std::is_same<key_type,value_type>::value;
  298. public:
  299. using hasher=typename super::hasher;
  300. using key_equal=typename super::key_equal;
  301. using allocator_type=typename super::allocator_type;
  302. using pointer=typename super::pointer;
  303. using const_pointer=typename super::const_pointer;
  304. using reference=typename super::reference;
  305. using const_reference=typename super::const_reference;
  306. using size_type=typename super::size_type;
  307. using difference_type=typename super::difference_type;
  308. using const_iterator=table_iterator<type_policy,group_type_pointer,true>;
  309. using iterator=typename std::conditional<
  310. has_mutable_iterator,
  311. table_iterator<type_policy,group_type_pointer,false>,
  312. const_iterator>::type;
  313. using erase_return_type=table_erase_return_type<iterator>;
  314. table(
  315. std::size_t n=default_bucket_count,const Hash& h_=Hash(),
  316. const Pred& pred_=Pred(),const Allocator& al_=Allocator()):
  317. super{n,h_,pred_,al_}
  318. {}
  319. table(const table& x)=default;
  320. table(table&& x)=default;
  321. table(const table& x,const Allocator& al_):super{x,al_}{}
  322. table(table&& x,const Allocator& al_):super{std::move(x),al_}{}
  323. table(compatible_concurrent_table&& x):
  324. table(std::move(x),x.exclusive_access()){}
  325. ~table()=default;
  326. table& operator=(const table& x)=default;
  327. table& operator=(table&& x)=default;
  328. using super::get_allocator;
  329. iterator begin()noexcept
  330. {
  331. iterator it{this->arrays.groups(),0,this->arrays.elements()};
  332. if(this->arrays.elements()&&
  333. !(this->arrays.groups()[0].match_occupied()&0x1))++it;
  334. return it;
  335. }
  336. const_iterator begin()const noexcept
  337. {return const_cast<table*>(this)->begin();}
  338. iterator end()noexcept{return {};}
  339. const_iterator end()const noexcept{return const_cast<table*>(this)->end();}
  340. const_iterator cbegin()const noexcept{return begin();}
  341. const_iterator cend()const noexcept{return end();}
  342. using super::empty;
  343. using super::size;
  344. using super::max_size;
  345. template<typename... Args>
  346. BOOST_FORCEINLINE std::pair<iterator,bool> emplace(Args&&... args)
  347. {
  348. auto x=alloc_make_insert_type<type_policy>(
  349. this->al(),std::forward<Args>(args)...);
  350. return emplace_impl(type_policy::move(x.value()));
  351. }
  352. template<typename Key,typename... Args>
  353. BOOST_FORCEINLINE std::pair<iterator,bool> try_emplace(
  354. Key&& x,Args&&... args)
  355. {
  356. return emplace_impl(
  357. try_emplace_args_t{},std::forward<Key>(x),std::forward<Args>(args)...);
  358. }
  359. BOOST_FORCEINLINE std::pair<iterator,bool>
  360. insert(const init_type& x){return emplace_impl(x);}
  361. BOOST_FORCEINLINE std::pair<iterator,bool>
  362. insert(init_type&& x){return emplace_impl(std::move(x));}
  363. /* template<typename=void> tilts call ambiguities in favor of init_type */
  364. template<typename=void>
  365. BOOST_FORCEINLINE std::pair<iterator,bool>
  366. insert(const value_type& x){return emplace_impl(x);}
  367. template<typename=void>
  368. BOOST_FORCEINLINE std::pair<iterator,bool>
  369. insert(value_type&& x){return emplace_impl(std::move(x));}
  370. template<typename T=element_type>
  371. BOOST_FORCEINLINE
  372. typename std::enable_if<
  373. !std::is_same<T,value_type>::value,
  374. std::pair<iterator,bool>
  375. >::type
  376. insert(element_type&& x){return emplace_impl(std::move(x));}
  377. template<
  378. bool dependent_value=false,
  379. typename std::enable_if<
  380. has_mutable_iterator||dependent_value>::type* =nullptr
  381. >
  382. erase_return_type erase(iterator pos)noexcept
  383. {return erase(const_iterator(pos));}
  384. BOOST_FORCEINLINE
  385. erase_return_type erase(const_iterator pos)noexcept
  386. {
  387. super::erase(pos.pc(),pos.p());
  388. return {pos};
  389. }
  390. template<typename Key>
  391. BOOST_FORCEINLINE
  392. auto erase(Key&& x) -> typename std::enable_if<
  393. !std::is_convertible<Key,iterator>::value&&
  394. !std::is_convertible<Key,const_iterator>::value, std::size_t>::type
  395. {
  396. auto it=find(x);
  397. if(it!=end()){
  398. erase(it);
  399. return 1;
  400. }
  401. else return 0;
  402. }
  403. void swap(table& x)
  404. noexcept(noexcept(std::declval<super&>().swap(std::declval<super&>())))
  405. {
  406. super::swap(x);
  407. }
  408. using super::clear;
  409. element_type extract(const_iterator pos)
  410. {
  411. BOOST_ASSERT(pos!=end());
  412. erase_on_exit e{*this,pos};
  413. (void)e;
  414. return std::move(*pos.p());
  415. }
  416. // TODO: should we accept different allocator too?
  417. template<typename Hash2,typename Pred2>
  418. void merge(table<TypePolicy,Hash2,Pred2,Allocator>& x)
  419. {
  420. x.for_all_elements([&,this](group_type* pg,unsigned int n,element_type* p){
  421. erase_on_exit e{x,{pg,n,p}};
  422. if(!emplace_impl(type_policy::move(*p)).second)e.rollback();
  423. });
  424. }
  425. template<typename Hash2,typename Pred2>
  426. void merge(table<TypePolicy,Hash2,Pred2,Allocator>&& x){merge(x);}
  427. using super::hash_function;
  428. using super::key_eq;
  429. template<typename Key>
  430. BOOST_FORCEINLINE iterator find(const Key& x)
  431. {
  432. return make_iterator(super::find(x));
  433. }
  434. template<typename Key>
  435. BOOST_FORCEINLINE const_iterator find(const Key& x)const
  436. {
  437. return const_cast<table*>(this)->find(x);
  438. }
  439. using super::capacity;
  440. using super::load_factor;
  441. using super::max_load_factor;
  442. using super::max_load;
  443. using super::rehash;
  444. using super::reserve;
  445. template<typename Predicate>
  446. friend std::size_t erase_if(table& x,Predicate& pr)
  447. {
  448. using value_reference=typename std::conditional<
  449. std::is_same<key_type,value_type>::value,
  450. const_reference,
  451. reference
  452. >::type;
  453. std::size_t s=x.size();
  454. x.for_all_elements(
  455. [&](group_type* pg,unsigned int n,element_type* p){
  456. if(pr(const_cast<value_reference>(type_policy::value_from(*p)))){
  457. x.super::erase(pg,n,p);
  458. }
  459. });
  460. return std::size_t(s-x.size());
  461. }
  462. friend bool operator==(const table& x,const table& y)
  463. {
  464. return static_cast<const super&>(x)==static_cast<const super&>(y);
  465. }
  466. friend bool operator!=(const table& x,const table& y){return !(x==y);}
  467. private:
  468. template<typename ArraysType>
  469. table(compatible_concurrent_table&& x,arrays_holder<ArraysType,Allocator>&& ah):
  470. super{
  471. std::move(x.h()),std::move(x.pred()),std::move(x.al()),
  472. [&x]{return arrays_type{
  473. x.arrays.groups_size_index,x.arrays.groups_size_mask,
  474. to_pointer<group_type_pointer>(
  475. reinterpret_cast<group_type*>(x.arrays.groups())),
  476. x.arrays.elements_};},
  477. size_ctrl_type{x.size_ctrl.ml,x.size_ctrl.size}}
  478. {
  479. compatible_concurrent_table::arrays_type::delete_group_access(x.al(),x.arrays);
  480. x.arrays=ah.release();
  481. x.size_ctrl.ml=x.initial_max_load();
  482. x.size_ctrl.size=0;
  483. }
  484. template<typename ExclusiveLockGuard>
  485. table(compatible_concurrent_table&& x,ExclusiveLockGuard):
  486. table(std::move(x),x.make_empty_arrays())
  487. {}
  488. struct erase_on_exit
  489. {
  490. erase_on_exit(table& x_,const_iterator it_):x(x_),it(it_){}
  491. ~erase_on_exit(){if(!rollback_)x.erase(it);}
  492. void rollback(){rollback_=true;}
  493. table& x;
  494. const_iterator it;
  495. bool rollback_=false;
  496. };
  497. static inline iterator make_iterator(const locator& l)noexcept
  498. {
  499. return {l.pg,l.n,l.p};
  500. }
  501. template<typename... Args>
  502. BOOST_FORCEINLINE std::pair<iterator,bool> emplace_impl(Args&&... args)
  503. {
  504. const auto &k=this->key_from(std::forward<Args>(args)...);
  505. auto hash=this->hash_for(k);
  506. auto pos0=this->position_for(hash);
  507. auto loc=super::find(k,pos0,hash);
  508. if(loc){
  509. return {make_iterator(loc),false};
  510. }
  511. if(BOOST_LIKELY(this->size_ctrl.size<this->size_ctrl.ml)){
  512. return {
  513. make_iterator(
  514. this->unchecked_emplace_at(pos0,hash,std::forward<Args>(args)...)),
  515. true
  516. };
  517. }
  518. else{
  519. return {
  520. make_iterator(
  521. this->unchecked_emplace_with_rehash(
  522. hash,std::forward<Args>(args)...)),
  523. true
  524. };
  525. }
  526. }
  527. };
  528. #if defined(BOOST_MSVC)
  529. #pragma warning(pop) /* C4714 */
  530. #endif
  531. #include <boost/unordered/detail/foa/restore_wshadow.hpp>
  532. } /* namespace foa */
  533. } /* namespace detail */
  534. } /* namespace unordered */
  535. } /* namespace boost */
  536. #endif