hashtable.hpp 179 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2006-2022
  4. // (C) Copyright 2022 Joaquin M Lopez Munoz.
  5. // (C) Copyright 2022 Christian Mazakas
  6. //
  7. // Distributed under the Boost Software License, Version 1.0.
  8. // (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // See http://www.boost.org/libs/intrusive for documentation.
  12. //
  13. /////////////////////////////////////////////////////////////////////////////
  14. // fastmod_buckets option is implemented reusing parts of Joaquin M. Lopez
  15. // Munoz's "fxa_unordered" library (proof of concept of closed- and
  16. // open-addressing unordered associative containers), released under
  17. // Boost Software License:
  18. //
  19. // https://github.com/joaquintides/fxa_unordered/
  20. //
  21. // On cases and systems that can't take advantage of Daniel Lemire's
  22. // "fastmod" (https://github.com/lemire/fastmod) approach,
  23. // precomputed divisions are used.
  24. //
  25. // As always, thanks Joaquin for your great work!
  26. #ifndef BOOST_INTRUSIVE_HASHTABLE_HPP
  27. #define BOOST_INTRUSIVE_HASHTABLE_HPP
  28. #include <boost/intrusive/detail/config_begin.hpp>
  29. #include <boost/intrusive/intrusive_fwd.hpp>
  30. #include <boost/move/detail/meta_utils_core.hpp>
  31. //General intrusive utilities
  32. #include <boost/intrusive/detail/hashtable_node.hpp>
  33. #include <boost/intrusive/detail/transform_iterator.hpp>
  34. #include <boost/intrusive/link_mode.hpp>
  35. #include <boost/intrusive/detail/ebo_functor_holder.hpp>
  36. #include <boost/intrusive/detail/is_stateful_value_traits.hpp>
  37. #include <boost/intrusive/detail/node_to_value.hpp>
  38. #include <boost/intrusive/detail/exception_disposer.hpp>
  39. #include <boost/intrusive/detail/node_cloner_disposer.hpp>
  40. #include <boost/intrusive/detail/simple_disposers.hpp>
  41. #include <boost/intrusive/detail/size_holder.hpp>
  42. #include <boost/intrusive/detail/iterator.hpp>
  43. #include <boost/intrusive/detail/get_value_traits.hpp>
  44. #include <boost/intrusive/detail/algorithm.hpp>
  45. #include <boost/intrusive/detail/value_functors.hpp>
  46. //Implementation utilities
  47. #include <boost/intrusive/unordered_set_hook.hpp>
  48. #include <boost/intrusive/detail/slist_iterator.hpp>
  49. #include <boost/intrusive/pointer_traits.hpp>
  50. #include <boost/intrusive/detail/mpl.hpp>
  51. #include <boost/intrusive/circular_slist_algorithms.hpp>
  52. #include <boost/intrusive/linear_slist_algorithms.hpp>
  53. //boost
  54. #include <boost/intrusive/detail/assert.hpp>
  55. #include <boost/move/utility_core.hpp>
  56. #include <boost/move/adl_move_swap.hpp>
  57. #include <boost/move/algo/detail/search.hpp>
  58. //std C++
  59. #include <boost/intrusive/detail/minimal_pair_header.hpp> //std::pair
  60. #include <cstddef> //std::size_t
  61. #include <boost/cstdint.hpp> //std::uint64_t
  62. #include "detail/hash.hpp"
  63. #if defined(BOOST_HAS_PRAGMA_ONCE)
  64. # pragma once
  65. #endif
  66. #ifdef _MSC_VER
  67. #include <intrin.h>
  68. #endif
  69. namespace boost {
  70. namespace intrusive {
  71. #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  72. /// @cond
  73. //We only support LLP64(Win64) or LP64(most Unix) data models
  74. #ifdef _WIN64 //In 64 bit windows sizeof(size_t) == sizeof(unsigned long long)
  75. # define BOOST_INTRUSIVE_SIZE_C(NUMBER) NUMBER##ULL
  76. # define BOOST_INTRUSIVE_64_BIT_SIZE_T 1
  77. #else //In 32 bit windows and 32/64 bit unixes sizeof(size_t) == sizeof(unsigned long)
  78. # define BOOST_INTRUSIVE_SIZE_C(NUMBER) NUMBER##UL
  79. # define BOOST_INTRUSIVE_64_BIT_SIZE_T (((((ULONG_MAX>>16)>>16)>>16)>>15) != 0)
  80. #endif
  81. template<int Dummy = 0>
  82. struct prime_list_holder
  83. {
  84. private:
  85. template <class SizeType> // sizeof(SizeType) < sizeof(std::size_t)
  86. static inline SizeType truncate_size_type(std::size_t n, detail::true_)
  87. { return n < std::size_t(SizeType(-1)) ? static_cast<SizeType>(n) : SizeType(-1); }
  88. template <class SizeType> // sizeof(SizeType) == sizeof(std::size_t)
  89. static inline SizeType truncate_size_type(std::size_t n, detail::false_)
  90. { return static_cast<SizeType>(n); }
  91. static const std::size_t prime_list[];
  92. static const std::size_t prime_list_size;
  93. static const std::size_t *suggested_lower_bucket_count_ptr(std::size_t n)
  94. {
  95. const std::size_t *primes = &prime_list[0];
  96. const std::size_t *primes_end = primes + prime_list_size;
  97. std::size_t const* bound =
  98. boost::movelib::lower_bound(primes, primes_end, n, value_less<std::size_t>());
  99. bound -= std::size_t(bound == primes_end);
  100. return bound;
  101. }
  102. static const std::size_t *suggested_upper_bucket_count_ptr(std::size_t n)
  103. {
  104. const std::size_t *primes = &prime_list[0];
  105. const std::size_t *primes_end = primes + prime_list_size;
  106. std::size_t const* bound =
  107. boost::movelib::upper_bound(primes, primes_end, n, value_less<std::size_t>());
  108. bound -= std::size_t(bound == primes_end);
  109. return bound;
  110. }
  111. static std::size_t suggested_lower_bucket_count_impl(std::size_t n)
  112. { return *suggested_lower_bucket_count_ptr(n); }
  113. static std::size_t suggested_upper_bucket_count_impl(std::size_t n)
  114. { return *suggested_upper_bucket_count_ptr(n); }
  115. public:
  116. template <class SizeType>
  117. static inline SizeType suggested_upper_bucket_count(SizeType n)
  118. {
  119. std::size_t const c = suggested_upper_bucket_count_impl(static_cast<std::size_t>(n));
  120. return truncate_size_type<SizeType>(c, detail::bool_<(sizeof(SizeType) < sizeof(std::size_t))>());
  121. }
  122. template <class SizeType>
  123. static inline SizeType suggested_lower_bucket_count(SizeType n)
  124. {
  125. std::size_t const c = suggested_lower_bucket_count_impl(static_cast<std::size_t>(n));
  126. return truncate_size_type<SizeType>(c, detail::bool_<(sizeof(SizeType) < sizeof(std::size_t))>());
  127. }
  128. static inline std::size_t suggested_lower_bucket_count_idx(std::size_t n)
  129. { return static_cast<std::size_t>(suggested_lower_bucket_count_ptr(n) - &prime_list[0]); }
  130. static inline std::size_t suggested_upper_bucket_count_idx(std::size_t n)
  131. { return static_cast<std::size_t>(suggested_upper_bucket_count_ptr(n) - &prime_list[0]); }
  132. static inline std::size_t size_from_index(std::size_t n)
  133. { return prime_list[std::ptrdiff_t(n)]; }
  134. template<std::size_t SizeIndex>
  135. inline static std::size_t modfunc(std::size_t hash) { return hash % SizeIndex; }
  136. static std::size_t(*const positions[])(std::size_t);
  137. #if BOOST_INTRUSIVE_64_BIT_SIZE_T
  138. static const uint64_t inv_sizes32[];
  139. static const std::size_t inv_sizes32_size;
  140. #endif
  141. inline static std::size_t lower_size_index(std::size_t n)
  142. { return prime_list_holder<>::suggested_lower_bucket_count_idx(n); }
  143. inline static std::size_t upper_size_index(std::size_t n)
  144. { return prime_list_holder<>::suggested_upper_bucket_count_idx(n); }
  145. inline static std::size_t size(std::size_t size_index)
  146. { return prime_list_holder<>::size_from_index(size_index); }
  147. #if BOOST_INTRUSIVE_64_BIT_SIZE_T
  148. // https://github.com/lemire/fastmod
  149. inline static uint64_t mul128_u32(uint64_t lowbits, uint32_t d)
  150. {
  151. #if defined(_MSC_VER)
  152. return __umulh(lowbits, d);
  153. #elif defined(BOOST_HAS_INT128)
  154. return static_cast<uint64_t>((uint128_type(lowbits) * d) >> 64);
  155. #else
  156. uint64_t r1 = (lowbits & UINT32_MAX) * d;
  157. uint64_t r2 = (lowbits >> 32) * d;
  158. r2 += r1 >> 32;
  159. return r2 >> 32;
  160. #endif
  161. }
  162. inline static uint32_t fastmod_u32(uint32_t a, uint64_t M, uint32_t d)
  163. {
  164. uint64_t lowbits = M * a;
  165. return (uint32_t)(mul128_u32(lowbits, d));
  166. }
  167. #endif // BOOST_INTRUSIVE_64_BIT_SIZE_T
  168. inline static std::size_t position(std::size_t hash,std::size_t size_index)
  169. {
  170. #if BOOST_INTRUSIVE_64_BIT_SIZE_T
  171. BOOST_CONSTEXPR_OR_CONST std::size_t sizes_under_32bit = sizeof(inv_sizes32)/sizeof(inv_sizes32[0]);
  172. if(BOOST_LIKELY(size_index < sizes_under_32bit)){
  173. return fastmod_u32( uint32_t(hash)+uint32_t(hash>>32)
  174. , inv_sizes32[size_index]
  175. , uint32_t(prime_list[size_index]) );
  176. }
  177. else{
  178. return positions[size_index](hash);
  179. }
  180. #else
  181. return positions[size_index](hash);
  182. #endif // BOOST_INTRUSIVE_64_BIT_SIZE_T
  183. }
  184. };
  185. template<int Dummy>
  186. std::size_t(* const prime_list_holder<Dummy>::positions[])(std::size_t) =
  187. {
  188. modfunc<BOOST_INTRUSIVE_SIZE_C(3)>, modfunc<BOOST_INTRUSIVE_SIZE_C(7)>,
  189. modfunc<BOOST_INTRUSIVE_SIZE_C(11)>, modfunc<BOOST_INTRUSIVE_SIZE_C(17)>,
  190. modfunc<BOOST_INTRUSIVE_SIZE_C(29)>, modfunc<BOOST_INTRUSIVE_SIZE_C(53)>,
  191. modfunc<BOOST_INTRUSIVE_SIZE_C(97)>, modfunc<BOOST_INTRUSIVE_SIZE_C(193)>,
  192. modfunc<BOOST_INTRUSIVE_SIZE_C(389)>, modfunc<BOOST_INTRUSIVE_SIZE_C(769)>,
  193. modfunc<BOOST_INTRUSIVE_SIZE_C(1543)>, modfunc<BOOST_INTRUSIVE_SIZE_C(3079)>,
  194. modfunc<BOOST_INTRUSIVE_SIZE_C(6151)>, modfunc<BOOST_INTRUSIVE_SIZE_C(12289)>,
  195. modfunc<BOOST_INTRUSIVE_SIZE_C(24593)>, modfunc<BOOST_INTRUSIVE_SIZE_C(49157)>,
  196. modfunc<BOOST_INTRUSIVE_SIZE_C(98317)>, modfunc<BOOST_INTRUSIVE_SIZE_C(196613)>,
  197. modfunc<BOOST_INTRUSIVE_SIZE_C(393241)>, modfunc<BOOST_INTRUSIVE_SIZE_C(786433)>,
  198. modfunc<BOOST_INTRUSIVE_SIZE_C(1572869)>, modfunc<BOOST_INTRUSIVE_SIZE_C(3145739)>,
  199. modfunc<BOOST_INTRUSIVE_SIZE_C(6291469)>, modfunc<BOOST_INTRUSIVE_SIZE_C(12582917)>,
  200. modfunc<BOOST_INTRUSIVE_SIZE_C(25165843)>, modfunc<BOOST_INTRUSIVE_SIZE_C(50331653)>,
  201. modfunc<BOOST_INTRUSIVE_SIZE_C(100663319)>, modfunc<BOOST_INTRUSIVE_SIZE_C(201326611)>,
  202. modfunc<BOOST_INTRUSIVE_SIZE_C(402653189)>, modfunc<BOOST_INTRUSIVE_SIZE_C(805306457)>,
  203. modfunc<BOOST_INTRUSIVE_SIZE_C(1610612741)>, //0-30 indexes
  204. #if BOOST_INTRUSIVE_64_BIT_SIZE_T
  205. //Taken from Boost.MultiIndex code, thanks to Joaquin M. Lopez Munoz.
  206. modfunc<BOOST_INTRUSIVE_SIZE_C(3221225473)>, //<- 32 bit values stop here (index 31)
  207. modfunc<BOOST_INTRUSIVE_SIZE_C(6442450939)>, modfunc<BOOST_INTRUSIVE_SIZE_C(12884901893)>,
  208. modfunc<BOOST_INTRUSIVE_SIZE_C(25769803751)>, modfunc<BOOST_INTRUSIVE_SIZE_C(51539607551)>,
  209. modfunc<BOOST_INTRUSIVE_SIZE_C(103079215111)>, modfunc<BOOST_INTRUSIVE_SIZE_C(206158430209)>,
  210. modfunc<BOOST_INTRUSIVE_SIZE_C(412316860441)>, modfunc<BOOST_INTRUSIVE_SIZE_C(824633720831)>,
  211. modfunc<BOOST_INTRUSIVE_SIZE_C(1649267441651)>, modfunc<BOOST_INTRUSIVE_SIZE_C(3298534883309)>,
  212. modfunc<BOOST_INTRUSIVE_SIZE_C(6597069766657)>, modfunc<BOOST_INTRUSIVE_SIZE_C(13194139533299)>,
  213. modfunc<BOOST_INTRUSIVE_SIZE_C(26388279066623)>, modfunc<BOOST_INTRUSIVE_SIZE_C(52776558133303)>,
  214. modfunc<BOOST_INTRUSIVE_SIZE_C(105553116266489)>, modfunc<BOOST_INTRUSIVE_SIZE_C(211106232532969)>,
  215. modfunc<BOOST_INTRUSIVE_SIZE_C(422212465066001)>, modfunc<BOOST_INTRUSIVE_SIZE_C(844424930131963)>,
  216. modfunc<BOOST_INTRUSIVE_SIZE_C(1688849860263953)>, modfunc<BOOST_INTRUSIVE_SIZE_C(3377699720527861)>,
  217. modfunc<BOOST_INTRUSIVE_SIZE_C(6755399441055731)>, modfunc<BOOST_INTRUSIVE_SIZE_C(13510798882111483)>,
  218. modfunc<BOOST_INTRUSIVE_SIZE_C(27021597764222939)>, modfunc<BOOST_INTRUSIVE_SIZE_C(54043195528445957)>,
  219. modfunc<BOOST_INTRUSIVE_SIZE_C(108086391056891903)>, modfunc<BOOST_INTRUSIVE_SIZE_C(216172782113783843)>,
  220. modfunc<BOOST_INTRUSIVE_SIZE_C(432345564227567621)>, modfunc<BOOST_INTRUSIVE_SIZE_C(864691128455135207)>,
  221. modfunc<BOOST_INTRUSIVE_SIZE_C(1729382256910270481)>, modfunc<BOOST_INTRUSIVE_SIZE_C(3458764513820540933)>,
  222. modfunc<BOOST_INTRUSIVE_SIZE_C(6917529027641081903)>, modfunc<BOOST_INTRUSIVE_SIZE_C(9223372036854775783)> //(index 63)
  223. #else
  224. modfunc<BOOST_INTRUSIVE_SIZE_C(2147483647)> //<- 32 bit stops here (index 31) as ptrdiff_t is signed
  225. #endif
  226. };
  227. template<int Dummy>
  228. const std::size_t prime_list_holder<Dummy>::prime_list[] = {
  229. BOOST_INTRUSIVE_SIZE_C(3), BOOST_INTRUSIVE_SIZE_C(7),
  230. BOOST_INTRUSIVE_SIZE_C(11), BOOST_INTRUSIVE_SIZE_C(17),
  231. BOOST_INTRUSIVE_SIZE_C(29), BOOST_INTRUSIVE_SIZE_C(53),
  232. BOOST_INTRUSIVE_SIZE_C(97), BOOST_INTRUSIVE_SIZE_C(193),
  233. BOOST_INTRUSIVE_SIZE_C(389), BOOST_INTRUSIVE_SIZE_C(769),
  234. BOOST_INTRUSIVE_SIZE_C(1543), BOOST_INTRUSIVE_SIZE_C(3079),
  235. BOOST_INTRUSIVE_SIZE_C(6151), BOOST_INTRUSIVE_SIZE_C(12289),
  236. BOOST_INTRUSIVE_SIZE_C(24593), BOOST_INTRUSIVE_SIZE_C(49157),
  237. BOOST_INTRUSIVE_SIZE_C(98317), BOOST_INTRUSIVE_SIZE_C(196613),
  238. BOOST_INTRUSIVE_SIZE_C(393241), BOOST_INTRUSIVE_SIZE_C(786433),
  239. BOOST_INTRUSIVE_SIZE_C(1572869), BOOST_INTRUSIVE_SIZE_C(3145739),
  240. BOOST_INTRUSIVE_SIZE_C(6291469), BOOST_INTRUSIVE_SIZE_C(12582917),
  241. BOOST_INTRUSIVE_SIZE_C(25165843), BOOST_INTRUSIVE_SIZE_C(50331653),
  242. BOOST_INTRUSIVE_SIZE_C(100663319), BOOST_INTRUSIVE_SIZE_C(201326611),
  243. BOOST_INTRUSIVE_SIZE_C(402653189), BOOST_INTRUSIVE_SIZE_C(805306457),
  244. BOOST_INTRUSIVE_SIZE_C(1610612741), //0-30 indexes
  245. #if BOOST_INTRUSIVE_64_BIT_SIZE_T
  246. //Taken from Boost.MultiIndex code, thanks to Joaquin M. Lopez Munoz.
  247. BOOST_INTRUSIVE_SIZE_C(3221225473), //<- 32 bit values stop here (index 31)
  248. BOOST_INTRUSIVE_SIZE_C(6442450939), BOOST_INTRUSIVE_SIZE_C(12884901893),
  249. BOOST_INTRUSIVE_SIZE_C(25769803751), BOOST_INTRUSIVE_SIZE_C(51539607551),
  250. BOOST_INTRUSIVE_SIZE_C(103079215111), BOOST_INTRUSIVE_SIZE_C(206158430209),
  251. BOOST_INTRUSIVE_SIZE_C(412316860441), BOOST_INTRUSIVE_SIZE_C(824633720831),
  252. BOOST_INTRUSIVE_SIZE_C(1649267441651), BOOST_INTRUSIVE_SIZE_C(3298534883309),
  253. BOOST_INTRUSIVE_SIZE_C(6597069766657), BOOST_INTRUSIVE_SIZE_C(13194139533299),
  254. BOOST_INTRUSIVE_SIZE_C(26388279066623), BOOST_INTRUSIVE_SIZE_C(52776558133303),
  255. BOOST_INTRUSIVE_SIZE_C(105553116266489), BOOST_INTRUSIVE_SIZE_C(211106232532969),
  256. BOOST_INTRUSIVE_SIZE_C(422212465066001), BOOST_INTRUSIVE_SIZE_C(844424930131963),
  257. BOOST_INTRUSIVE_SIZE_C(1688849860263953), BOOST_INTRUSIVE_SIZE_C(3377699720527861),
  258. BOOST_INTRUSIVE_SIZE_C(6755399441055731), BOOST_INTRUSIVE_SIZE_C(13510798882111483),
  259. BOOST_INTRUSIVE_SIZE_C(27021597764222939), BOOST_INTRUSIVE_SIZE_C(54043195528445957),
  260. BOOST_INTRUSIVE_SIZE_C(108086391056891903), BOOST_INTRUSIVE_SIZE_C(216172782113783843),
  261. BOOST_INTRUSIVE_SIZE_C(432345564227567621), BOOST_INTRUSIVE_SIZE_C(864691128455135207),
  262. BOOST_INTRUSIVE_SIZE_C(1729382256910270481), BOOST_INTRUSIVE_SIZE_C(3458764513820540933),
  263. BOOST_INTRUSIVE_SIZE_C(6917529027641081903), BOOST_INTRUSIVE_SIZE_C(9223372036854775783) //(index 63)
  264. #else
  265. BOOST_INTRUSIVE_SIZE_C(2147483647) //<- 32 bit stops here (index 31) as ptrdiff_t is signed
  266. #endif
  267. };
  268. template<int Dummy>
  269. const std::size_t prime_list_holder<Dummy>::prime_list_size
  270. = sizeof(prime_list) / sizeof(std::size_t);
  271. #if BOOST_INTRUSIVE_64_BIT_SIZE_T
  272. template<int Dummy>
  273. const uint64_t prime_list_holder<Dummy>::inv_sizes32[] = {
  274. BOOST_INTRUSIVE_SIZE_C(6148914691236517206), //3
  275. BOOST_INTRUSIVE_SIZE_C(2635249153387078803), //7
  276. BOOST_INTRUSIVE_SIZE_C(1676976733973595602), //11
  277. BOOST_INTRUSIVE_SIZE_C(1085102592571150096), //17
  278. BOOST_INTRUSIVE_SIZE_C(636094623231363849), //29
  279. BOOST_INTRUSIVE_SIZE_C(348051774975651918), //53
  280. BOOST_INTRUSIVE_SIZE_C(190172619316593316), //97
  281. BOOST_INTRUSIVE_SIZE_C(95578984837873325), //193
  282. BOOST_INTRUSIVE_SIZE_C(47420935922132524), //389
  283. BOOST_INTRUSIVE_SIZE_C(23987963684927896), //769
  284. BOOST_INTRUSIVE_SIZE_C(11955116055547344), //1543
  285. BOOST_INTRUSIVE_SIZE_C(5991147799191151), //3079
  286. BOOST_INTRUSIVE_SIZE_C(2998982941588287), //6151
  287. BOOST_INTRUSIVE_SIZE_C(1501077717772769), //12289
  288. BOOST_INTRUSIVE_SIZE_C(750081082979285), //24593
  289. BOOST_INTRUSIVE_SIZE_C(375261795343686), //49157
  290. BOOST_INTRUSIVE_SIZE_C(187625172388393), //98317
  291. BOOST_INTRUSIVE_SIZE_C(93822606204624), //196613
  292. BOOST_INTRUSIVE_SIZE_C(46909513691883), //393241
  293. BOOST_INTRUSIVE_SIZE_C(23456218233098), //786433
  294. BOOST_INTRUSIVE_SIZE_C(11728086747027), //1572869
  295. BOOST_INTRUSIVE_SIZE_C(5864041509391), //3145739
  296. BOOST_INTRUSIVE_SIZE_C(2932024948977), //6291469
  297. BOOST_INTRUSIVE_SIZE_C(1466014921160), //12582917
  298. BOOST_INTRUSIVE_SIZE_C(733007198436), //25165843
  299. BOOST_INTRUSIVE_SIZE_C(366503839517), //50331653
  300. BOOST_INTRUSIVE_SIZE_C(183251896093), //100663319
  301. BOOST_INTRUSIVE_SIZE_C(91625960335), //201326611
  302. BOOST_INTRUSIVE_SIZE_C(45812983922), //402653189
  303. BOOST_INTRUSIVE_SIZE_C(22906489714), //805306457
  304. BOOST_INTRUSIVE_SIZE_C(11453246088), //1610612741
  305. BOOST_INTRUSIVE_SIZE_C(5726623060) //3221225473
  306. };
  307. template<int Dummy>
  308. const std::size_t prime_list_holder<Dummy>::inv_sizes32_size
  309. = sizeof(inv_sizes32) / sizeof(uint64_t);
  310. #endif // BOOST_INTRUSIVE_64_BIT_SIZE_T
  311. struct prime_fmod_size : prime_list_holder<>
  312. {
  313. };
  314. #undef BOOST_INTRUSIVE_SIZE_C
  315. #undef BOOST_INTRUSIVE_64_BIT_SIZE_T
  316. #endif //#if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  317. template<class InputIt, class T>
  318. InputIt priv_algo_find(InputIt first, InputIt last, const T& value)
  319. {
  320. for (; first != last; ++first) {
  321. if (*first == value) {
  322. return first;
  323. }
  324. }
  325. return last;
  326. }
  327. template<class InputIt, class T>
  328. typename boost::intrusive::iterator_traits<InputIt>::difference_type
  329. priv_algo_count(InputIt first, InputIt last, const T& value)
  330. {
  331. typename boost::intrusive::iterator_traits<InputIt>::difference_type ret = 0;
  332. for (; first != last; ++first) {
  333. if (*first == value) {
  334. ret++;
  335. }
  336. }
  337. return ret;
  338. }
  339. template <class ForwardIterator1, class ForwardIterator2>
  340. bool priv_algo_is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2)
  341. {
  342. typedef typename
  343. boost::intrusive::iterator_traits<ForwardIterator2>::difference_type
  344. distance_type;
  345. //Efficiently compare identical prefixes: O(N) if sequences
  346. //have the same elements in the same order.
  347. for ( ; first1 != last1; ++first1, ++first2){
  348. if (! (*first1 == *first2))
  349. break;
  350. }
  351. if (first1 == last1){
  352. return true;
  353. }
  354. //Establish last2 assuming equal ranges by iterating over the
  355. //rest of the list.
  356. ForwardIterator2 last2 = first2;
  357. boost::intrusive::iterator_advance(last2, boost::intrusive::iterator_distance(first1, last1));
  358. for(ForwardIterator1 scan = first1; scan != last1; ++scan){
  359. if (scan != (priv_algo_find)(first1, scan, *scan)){
  360. continue; //We've seen this one before.
  361. }
  362. distance_type matches = (priv_algo_count)(first2, last2, *scan);
  363. if (0 == matches || (priv_algo_count)(scan, last1, *scan) != matches){
  364. return false;
  365. }
  366. }
  367. return true;
  368. }
  369. struct hash_bool_flags
  370. {
  371. static const std::size_t unique_keys_pos = 1u;
  372. static const std::size_t constant_time_size_pos = 2u;
  373. static const std::size_t power_2_buckets_pos = 4u;
  374. static const std::size_t cache_begin_pos = 8u;
  375. static const std::size_t compare_hash_pos = 16u;
  376. static const std::size_t incremental_pos = 32u;
  377. static const std::size_t linear_buckets_pos = 64u;
  378. static const std::size_t fastmod_buckets_pos = 128u;
  379. };
  380. template<class Bucket, class Algo, class Disposer, class SizeType>
  381. class exception_bucket_disposer
  382. {
  383. Bucket *cont_;
  384. Disposer &disp_;
  385. const SizeType &constructed_;
  386. exception_bucket_disposer(const exception_bucket_disposer&);
  387. exception_bucket_disposer &operator=(const exception_bucket_disposer&);
  388. public:
  389. exception_bucket_disposer
  390. (Bucket &cont, Disposer &disp, const SizeType &constructed)
  391. : cont_(&cont), disp_(disp), constructed_(constructed)
  392. {}
  393. inline void release()
  394. { cont_ = 0; }
  395. ~exception_bucket_disposer()
  396. {
  397. SizeType n = constructed_;
  398. if(cont_){
  399. while(n--){
  400. Algo::detach_and_dispose(cont_[n].get_node_ptr(), disp_);
  401. }
  402. }
  403. }
  404. };
  405. template<class SupposedValueTraits>
  406. struct unordered_bucket_impl
  407. {
  408. typedef typename detail::get_node_traits
  409. <SupposedValueTraits>::type node_traits;
  410. typedef typename reduced_slist_node_traits
  411. <node_traits>::type reduced_node_traits;
  412. typedef bucket_impl<reduced_node_traits> type;
  413. typedef typename pointer_traits
  414. <typename reduced_node_traits::node_ptr>
  415. ::template rebind_pointer<type>::type pointer;
  416. };
  417. template<class SupposedValueTraits>
  418. struct unordered_bucket_ptr_impl
  419. {
  420. typedef typename unordered_bucket_impl<SupposedValueTraits>::pointer type;
  421. };
  422. template <class BucketPtr, class SizeType>
  423. struct bucket_traits_impl
  424. {
  425. private:
  426. BOOST_COPYABLE_AND_MOVABLE(bucket_traits_impl)
  427. public:
  428. /// @cond
  429. typedef BucketPtr bucket_ptr;
  430. typedef SizeType size_type;
  431. /// @endcond
  432. inline bucket_traits_impl(bucket_ptr buckets, size_type len)
  433. : buckets_(buckets), buckets_len_(len)
  434. {}
  435. inline bucket_traits_impl(const bucket_traits_impl& x)
  436. : buckets_(x.buckets_), buckets_len_(x.buckets_len_)
  437. {}
  438. inline bucket_traits_impl(BOOST_RV_REF(bucket_traits_impl) x)
  439. : buckets_(x.buckets_), buckets_len_(x.buckets_len_)
  440. {
  441. x.buckets_ = bucket_ptr(); x.buckets_len_ = 0u;
  442. }
  443. inline bucket_traits_impl& operator=(BOOST_RV_REF(bucket_traits_impl) x)
  444. {
  445. buckets_ = x.buckets_; buckets_len_ = x.buckets_len_;
  446. x.buckets_ = bucket_ptr(); x.buckets_len_ = 0u; return *this;
  447. }
  448. inline bucket_traits_impl& operator=(BOOST_COPY_ASSIGN_REF(bucket_traits_impl) x)
  449. {
  450. buckets_ = x.buckets_; buckets_len_ = x.buckets_len_; return *this;
  451. }
  452. inline bucket_ptr bucket_begin() const
  453. {
  454. return buckets_;
  455. }
  456. inline size_type bucket_count() const BOOST_NOEXCEPT
  457. {
  458. return buckets_len_;
  459. }
  460. private:
  461. bucket_ptr buckets_;
  462. size_type buckets_len_;
  463. };
  464. template <class T>
  465. struct store_hash_is_true
  466. {
  467. template<bool Add>
  468. struct two_or_three {detail::yes_type _[2u + (unsigned)Add];};
  469. template <class U> static detail::yes_type test(...);
  470. template <class U> static two_or_three<U::store_hash> test (int);
  471. static const bool value = sizeof(test<T>(0)) > sizeof(detail::yes_type)*2u;
  472. };
  473. template <class T>
  474. struct optimize_multikey_is_true
  475. {
  476. template<bool Add>
  477. struct two_or_three { detail::yes_type _[2u + (unsigned)Add];};
  478. template <class U> static detail::yes_type test(...);
  479. template <class U> static two_or_three<U::optimize_multikey> test (int);
  480. static const bool value = sizeof(test<T>(0)) > sizeof(detail::yes_type)*2u;
  481. };
  482. struct insert_commit_data_impl
  483. {
  484. std::size_t hash;
  485. std::size_t bucket_idx;
  486. inline std::size_t get_hash() const
  487. { return hash; }
  488. inline void set_hash(std::size_t h)
  489. { hash = h; }
  490. };
  491. template<class Node, class SlistNodePtr>
  492. inline typename pointer_traits<SlistNodePtr>::template rebind_pointer<Node>::type
  493. dcast_bucket_ptr(const SlistNodePtr &p)
  494. {
  495. typedef typename pointer_traits<SlistNodePtr>::template rebind_pointer<Node>::type node_ptr;
  496. return pointer_traits<node_ptr>::static_cast_from(p);
  497. }
  498. template<class NodeTraits>
  499. struct group_functions
  500. {
  501. // A group is reverse-linked
  502. //
  503. // A is "first in group"
  504. // C is "last in group"
  505. // __________________
  506. // | _____ _____ |
  507. // | | | | | | <- Group links
  508. // ^ V ^ V ^ V
  509. // _ _ _ _
  510. // A|_| B|_| C|_| D|_|
  511. //
  512. // ^ | ^ | ^ | ^ V <- Bucket links
  513. // _ _____| |_____| |______| |____| |
  514. // |B| |
  515. // ^________________________________|
  516. //
  517. typedef NodeTraits node_traits;
  518. typedef unordered_group_adapter<node_traits> group_traits;
  519. typedef typename node_traits::node_ptr node_ptr;
  520. typedef typename node_traits::node node;
  521. typedef typename reduced_slist_node_traits
  522. <node_traits>::type reduced_node_traits;
  523. typedef typename reduced_node_traits::node_ptr slist_node_ptr;
  524. typedef typename reduced_node_traits::node slist_node;
  525. typedef circular_slist_algorithms<group_traits> group_algorithms;
  526. typedef circular_slist_algorithms<node_traits> node_algorithms;
  527. static slist_node_ptr get_bucket_before_begin
  528. (slist_node_ptr bucket_beg, slist_node_ptr bucket_last, slist_node_ptr sp, detail::true_)
  529. {
  530. //First find the last node of p's group.
  531. //This requires checking the first node of the next group or
  532. //the bucket node.
  533. node_ptr p = dcast_bucket_ptr<node>(sp);
  534. node_ptr prev_node = p;
  535. node_ptr nxt(node_traits::get_next(p));
  536. while(!(bucket_beg <= nxt && nxt <= bucket_last) &&
  537. (group_traits::get_next(nxt) == prev_node)){
  538. prev_node = nxt;
  539. nxt = node_traits::get_next(nxt);
  540. }
  541. //If we've reached the bucket node just return it.
  542. if(bucket_beg <= nxt && nxt <= bucket_last){
  543. return nxt;
  544. }
  545. //Otherwise, iterate using group links until the bucket node
  546. node_ptr first_node_of_group = nxt;
  547. node_ptr last_node_group = group_traits::get_next(first_node_of_group);
  548. slist_node_ptr possible_end = node_traits::get_next(last_node_group);
  549. while(!(bucket_beg <= possible_end && possible_end <= bucket_last)){
  550. first_node_of_group = dcast_bucket_ptr<node>(possible_end);
  551. last_node_group = group_traits::get_next(first_node_of_group);
  552. possible_end = node_traits::get_next(last_node_group);
  553. }
  554. return possible_end;
  555. }
  556. static slist_node_ptr get_bucket_before_begin
  557. (slist_node_ptr bucket_beg, slist_node_ptr bucket_last, slist_node_ptr sp, detail::false_)
  558. {
  559. //The end node is embedded in the singly linked list:
  560. //iterate until we reach it.
  561. while (!(bucket_beg <= sp && sp <= bucket_last)){
  562. sp = reduced_node_traits::get_next(sp);
  563. }
  564. return sp;
  565. }
  566. static node_ptr get_prev_to_first_in_group(slist_node_ptr bucket_node, node_ptr first_in_group)
  567. {
  568. node_ptr nb = dcast_bucket_ptr<node>(bucket_node);
  569. node_ptr n;
  570. while((n = node_traits::get_next(nb)) != first_in_group){
  571. nb = group_traits::get_next(n); //go to last in group
  572. }
  573. return nb;
  574. }
  575. static void erase_from_group(slist_node_ptr end_ptr, node_ptr to_erase_ptr, detail::true_)
  576. {
  577. node_ptr const nxt_ptr(node_traits::get_next(to_erase_ptr));
  578. //Check if the next node is in the group (not end node) and reverse linked to
  579. //'to_erase_ptr'. Erase if that's the case.
  580. if(nxt_ptr != end_ptr && to_erase_ptr == group_traits::get_next(nxt_ptr)){
  581. group_algorithms::unlink_after(nxt_ptr);
  582. }
  583. }
  584. inline static void erase_from_group(slist_node_ptr, node_ptr, detail::false_)
  585. {}
  586. inline static node_ptr get_last_in_group(node_ptr first_in_group, detail::true_)
  587. { return group_traits::get_next(first_in_group); }
  588. inline static node_ptr get_last_in_group(node_ptr n, detail::false_)
  589. { return n; }
  590. static node_ptr get_first_in_group(node_ptr n, detail::true_)
  591. {
  592. node_ptr ng;
  593. while(n == node_traits::get_next((ng = group_traits::get_next(n)))){
  594. n = ng;
  595. }
  596. return n;
  597. }
  598. inline static node_ptr get_first_in_group(node_ptr n, detail::false_)
  599. { return n; }
  600. inline static bool is_first_in_group(node_ptr ptr)
  601. { return node_traits::get_next(group_traits::get_next(ptr)) != ptr; }
  602. inline static void insert_in_group(node_ptr first_in_group, node_ptr n, detail::true_)
  603. { group_algorithms::link_after(first_in_group, n); }
  604. inline static void insert_in_group(node_ptr, node_ptr, detail::false_)
  605. {}
  606. //Splits a group in two groups, and makes "new_first" the first node in the second group.
  607. //Returns the first element of the first group
  608. static node_ptr split_group(node_ptr const new_first)
  609. {
  610. node_ptr const old_first((get_first_in_group)(new_first, detail::true_()));
  611. //Check new_first was not the first in group
  612. if(old_first != new_first){
  613. node_ptr const last = group_traits::get_next(old_first);
  614. group_traits::set_next(old_first, group_traits::get_next(new_first));
  615. group_traits::set_next(new_first, last);
  616. }
  617. return old_first;
  618. }
  619. };
  620. template<class BucketType, class SplitTraits, class SlistNodeAlgorithms>
  621. class incremental_rehash_rollback
  622. {
  623. private:
  624. typedef BucketType bucket_type;
  625. typedef SplitTraits split_traits;
  626. incremental_rehash_rollback();
  627. incremental_rehash_rollback & operator=(const incremental_rehash_rollback &);
  628. incremental_rehash_rollback (const incremental_rehash_rollback &);
  629. public:
  630. incremental_rehash_rollback
  631. (bucket_type &source_bucket, bucket_type &destiny_bucket, split_traits &split_tr)
  632. : source_bucket_(source_bucket), destiny_bucket_(destiny_bucket)
  633. , split_traits_(split_tr), released_(false)
  634. {}
  635. inline void release()
  636. { released_ = true; }
  637. ~incremental_rehash_rollback()
  638. {
  639. if(!released_){
  640. //If an exception is thrown, just put all moved nodes back in the old bucket
  641. //and move back the split mark.
  642. SlistNodeAlgorithms::transfer_after(destiny_bucket_.get_node_ptr(), source_bucket_.get_node_ptr());
  643. split_traits_.decrement();
  644. }
  645. }
  646. private:
  647. bucket_type &source_bucket_;
  648. bucket_type &destiny_bucket_;
  649. split_traits &split_traits_;
  650. bool released_;
  651. };
  652. template<class NodeTraits>
  653. struct node_functions
  654. {
  655. inline static void store_hash(typename NodeTraits::node_ptr p, std::size_t h, detail::true_)
  656. { return NodeTraits::set_hash(p, h); }
  657. inline static void store_hash(typename NodeTraits::node_ptr, std::size_t, detail::false_)
  658. {}
  659. };
  660. inline std::size_t hash_to_bucket(std::size_t hash_value, std::size_t bucket_cnt, detail::false_)
  661. { return hash_value % bucket_cnt; }
  662. inline std::size_t hash_to_bucket(std::size_t hash_value, std::size_t bucket_cnt, detail::true_)
  663. { return hash_value & (bucket_cnt - 1); }
  664. template<bool Power2Buckets, bool Incremental> //!fastmod_buckets
  665. inline std::size_t hash_to_bucket_split(std::size_t hash_value, std::size_t bucket_cnt, std::size_t split, detail::false_)
  666. {
  667. std::size_t bucket_number = hash_to_bucket(hash_value, bucket_cnt, detail::bool_<Power2Buckets>());
  668. BOOST_IF_CONSTEXPR(Incremental)
  669. bucket_number -= static_cast<std::size_t>(bucket_number >= split)*(bucket_cnt/2);
  670. return bucket_number;
  671. }
  672. template<bool Power2Buckets, bool Incremental> //fastmod_buckets
  673. inline std::size_t hash_to_bucket_split(std::size_t hash_value, std::size_t , std::size_t split, detail::true_)
  674. {
  675. return prime_fmod_size::position(hash_value, split);
  676. }
  677. //!This metafunction will obtain the type of a bucket
  678. //!from the value_traits or hook option to be used with
  679. //!a hash container.
  680. template<class ValueTraitsOrHookOption>
  681. struct unordered_bucket
  682. : public unordered_bucket_impl
  683. < typename ValueTraitsOrHookOption::
  684. template pack<empty>::proto_value_traits>
  685. {};
  686. //!This metafunction will obtain the type of a bucket pointer
  687. //!from the value_traits or hook option to be used with
  688. //!a hash container.
  689. template<class ValueTraitsOrHookOption>
  690. struct unordered_bucket_ptr
  691. : public unordered_bucket_ptr_impl
  692. < typename ValueTraitsOrHookOption::
  693. template pack<empty>::proto_value_traits>
  694. {};
  695. //!This metafunction will obtain the type of the default bucket traits
  696. //!(when the user does not specify the bucket_traits<> option) from the
  697. //!value_traits or hook option to be used with
  698. //!a hash container.
  699. template<class ValueTraitsOrHookOption>
  700. struct unordered_default_bucket_traits
  701. {
  702. typedef typename ValueTraitsOrHookOption::
  703. template pack<empty>::proto_value_traits supposed_value_traits;
  704. typedef bucket_traits_impl
  705. < typename unordered_bucket_ptr_impl
  706. <supposed_value_traits>::type
  707. , std::size_t> type;
  708. };
  709. struct default_bucket_traits;
  710. //hashtable default hook traits
  711. struct default_hashtable_hook_applier
  712. { template <class T> struct apply{ typedef typename T::default_hashtable_hook type; }; };
  713. template<>
  714. struct is_default_hook_tag<default_hashtable_hook_applier>
  715. { static const bool value = true; };
  716. struct hashtable_defaults
  717. {
  718. typedef default_hashtable_hook_applier proto_value_traits;
  719. typedef std::size_t size_type;
  720. typedef void key_of_value;
  721. typedef void equal;
  722. typedef void hash;
  723. typedef default_bucket_traits bucket_traits;
  724. static const bool constant_time_size = true;
  725. static const bool power_2_buckets = false;
  726. static const bool cache_begin = false;
  727. static const bool compare_hash = false;
  728. static const bool incremental = false;
  729. static const bool linear_buckets = false;
  730. static const bool fastmod_buckets = false;
  731. };
  732. template<class ValueTraits, bool IsConst>
  733. struct downcast_node_to_value_t
  734. : public detail::node_to_value<ValueTraits, IsConst>
  735. {
  736. typedef detail::node_to_value<ValueTraits, IsConst> base_t;
  737. typedef typename base_t::result_type result_type;
  738. typedef ValueTraits value_traits;
  739. typedef typename unordered_bucket_impl
  740. <value_traits>::type::node_traits::node node;
  741. typedef typename detail::add_const_if_c
  742. <node, IsConst>::type &first_argument_type;
  743. typedef typename detail::add_const_if_c
  744. < typename ValueTraits::node_traits::node
  745. , IsConst>::type &intermediate_argument_type;
  746. typedef typename pointer_traits
  747. <typename ValueTraits::pointer>::
  748. template rebind_pointer
  749. <const ValueTraits>::type const_value_traits_ptr;
  750. inline downcast_node_to_value_t(const_value_traits_ptr ptr)
  751. : base_t(ptr)
  752. {}
  753. inline result_type operator()(first_argument_type arg) const
  754. { return this->base_t::operator()(static_cast<intermediate_argument_type>(arg)); }
  755. };
  756. template<class F, class SlistNodePtr, class NodePtr>
  757. struct node_cast_adaptor
  758. //Use public inheritance to avoid MSVC bugs with closures
  759. : public detail::ebo_functor_holder<F>
  760. {
  761. typedef detail::ebo_functor_holder<F> base_t;
  762. typedef typename pointer_traits<SlistNodePtr>::element_type slist_node;
  763. typedef typename pointer_traits<NodePtr>::element_type node;
  764. template<class ConvertibleToF, class RealValuTraits>
  765. inline node_cast_adaptor(const ConvertibleToF &c2f, const RealValuTraits *traits)
  766. : base_t(base_t(c2f, traits))
  767. {}
  768. inline typename base_t::node_ptr operator()(const slist_node &to_clone)
  769. { return base_t::operator()(static_cast<const node &>(to_clone)); }
  770. inline void operator()(SlistNodePtr to_clone)
  771. {
  772. base_t::operator()(pointer_traits<NodePtr>::pointer_to(static_cast<node &>(*to_clone)));
  773. }
  774. };
  775. //bucket_plus_vtraits stores ValueTraits + BucketTraits
  776. //this data is needed by iterators to obtain the
  777. //value from the iterator and detect the bucket
  778. template<class ValueTraits, class BucketTraits, bool LinearBuckets>
  779. struct bucket_plus_vtraits
  780. {
  781. private:
  782. BOOST_MOVABLE_BUT_NOT_COPYABLE(bucket_plus_vtraits)
  783. struct data_type
  784. : public ValueTraits, BucketTraits
  785. {
  786. private:
  787. BOOST_MOVABLE_BUT_NOT_COPYABLE(data_type)
  788. public:
  789. inline data_type(const ValueTraits& val_traits, const BucketTraits& b_traits)
  790. : ValueTraits(val_traits), BucketTraits(b_traits)
  791. {}
  792. inline data_type(BOOST_RV_REF(data_type) other)
  793. : ValueTraits (BOOST_MOVE_BASE(ValueTraits, other))
  794. , BucketTraits(BOOST_MOVE_BASE(BucketTraits, other))
  795. {}
  796. } m_data;
  797. public:
  798. typedef BucketTraits bucket_traits;
  799. typedef ValueTraits value_traits;
  800. static const bool safemode_or_autounlink = is_safe_autounlink<value_traits::link_mode>::value;
  801. typedef typename unordered_bucket_impl
  802. <value_traits>::type bucket_type;
  803. typedef typename unordered_bucket_ptr_impl
  804. <value_traits>::type bucket_ptr;
  805. typedef typename value_traits::node_traits node_traits;
  806. typedef typename bucket_type::node_traits slist_node_traits;
  807. typedef unordered_group_adapter<node_traits> group_traits;
  808. typedef group_functions<node_traits> group_functions_t;
  809. typedef typename detail::if_c
  810. < LinearBuckets
  811. , linear_slist_algorithms<slist_node_traits>
  812. , circular_slist_algorithms<slist_node_traits>
  813. >::type slist_node_algorithms;
  814. typedef typename slist_node_traits::node_ptr slist_node_ptr;
  815. typedef trivial_value_traits
  816. <slist_node_traits, normal_link> slist_value_traits;
  817. typedef slist_iterator<slist_value_traits, false> siterator;
  818. typedef slist_iterator<slist_value_traits, true> const_siterator;
  819. typedef typename node_traits::node_ptr node_ptr;
  820. typedef typename node_traits::const_node_ptr const_node_ptr;
  821. typedef typename node_traits::node node;
  822. typedef typename value_traits::value_type value_type;
  823. typedef typename value_traits::pointer pointer;
  824. typedef typename value_traits::const_pointer const_pointer;
  825. typedef typename pointer_traits<pointer>::reference reference;
  826. typedef typename pointer_traits
  827. <const_pointer>::reference const_reference;
  828. typedef circular_slist_algorithms<group_traits> group_algorithms;
  829. typedef typename pointer_traits
  830. <typename value_traits::pointer>::
  831. template rebind_pointer
  832. <const value_traits>::type const_value_traits_ptr;
  833. typedef typename pointer_traits
  834. <typename value_traits::pointer>::
  835. template rebind_pointer
  836. <const bucket_plus_vtraits>::type const_bucket_value_traits_ptr;
  837. typedef detail::bool_<LinearBuckets> linear_buckets_t;
  838. typedef bucket_plus_vtraits& this_ref;
  839. static const std::size_t bucket_overhead = LinearBuckets ? 1u : 0u;
  840. inline bucket_plus_vtraits(const ValueTraits &val_traits, const bucket_traits &b_traits)
  841. : m_data(val_traits, b_traits)
  842. {}
  843. inline bucket_plus_vtraits(BOOST_RV_REF(bucket_plus_vtraits) other)
  844. : m_data(boost::move(((bucket_plus_vtraits&)other).m_data))
  845. {}
  846. inline const_value_traits_ptr priv_value_traits_ptr() const
  847. { return pointer_traits<const_value_traits_ptr>::pointer_to(this->priv_value_traits()); }
  848. //bucket_value_traits
  849. //
  850. inline const bucket_plus_vtraits &get_bucket_value_traits() const
  851. { return *this; }
  852. inline bucket_plus_vtraits &get_bucket_value_traits()
  853. { return *this; }
  854. inline const_bucket_value_traits_ptr bucket_value_traits_ptr() const
  855. { return pointer_traits<const_bucket_value_traits_ptr>::pointer_to(this->get_bucket_value_traits()); }
  856. //value traits
  857. //
  858. inline const value_traits &priv_value_traits() const
  859. { return static_cast<const value_traits &>(this->m_data); }
  860. inline value_traits &priv_value_traits()
  861. { return static_cast<value_traits &>(this->m_data); }
  862. //value traits
  863. //
  864. inline const bucket_traits &priv_bucket_traits() const
  865. { return static_cast<const bucket_traits &>(this->m_data); }
  866. inline bucket_traits& priv_bucket_traits()
  867. { return static_cast<bucket_traits&>(this->m_data); }
  868. //bucket operations
  869. inline bucket_ptr priv_bucket_pointer() const BOOST_NOEXCEPT
  870. { return this->priv_bucket_traits().bucket_begin(); }
  871. inline std::size_t priv_usable_bucket_count() const BOOST_NOEXCEPT
  872. {
  873. BOOST_IF_CONSTEXPR(bucket_overhead){
  874. const std::size_t n = this->priv_bucket_traits().bucket_count();
  875. return n - std::size_t(n != 0)*bucket_overhead;
  876. }
  877. else{
  878. return this->priv_bucket_traits().bucket_count();
  879. }
  880. }
  881. inline bucket_type &priv_bucket(std::size_t n) const BOOST_NOEXCEPT
  882. {
  883. BOOST_INTRUSIVE_INVARIANT_ASSERT(n < this->priv_usable_bucket_count());
  884. return this->priv_bucket_pointer()[std::ptrdiff_t(n)];
  885. }
  886. inline bucket_ptr priv_bucket_ptr(std::size_t n) const BOOST_NOEXCEPT
  887. { return pointer_traits<bucket_ptr>::pointer_to(this->priv_bucket(n)); }
  888. inline bucket_ptr priv_past_usable_bucket_ptr() const
  889. { return this->priv_bucket_pointer() + std::ptrdiff_t(priv_usable_bucket_count()); }
  890. inline bucket_ptr priv_invalid_bucket_ptr() const
  891. {
  892. BOOST_IF_CONSTEXPR(LinearBuckets) {
  893. return bucket_ptr();
  894. }
  895. else{
  896. return this->priv_past_usable_bucket_ptr();
  897. }
  898. }
  899. inline void priv_set_sentinel_bucket() const
  900. {
  901. BOOST_IF_CONSTEXPR(LinearBuckets) {
  902. BOOST_INTRUSIVE_INVARIANT_ASSERT(this->priv_bucket_traits().bucket_count() > 1);
  903. bucket_type &b = this->priv_bucket_pointer()[std::ptrdiff_t(this->priv_usable_bucket_count())];
  904. slist_node_algorithms::set_sentinel(b.get_node_ptr());
  905. }
  906. }
  907. inline void priv_unset_sentinel_bucket() const
  908. {
  909. BOOST_IF_CONSTEXPR(LinearBuckets) {
  910. BOOST_INTRUSIVE_INVARIANT_ASSERT(this->priv_bucket_traits().bucket_count() > 1);
  911. bucket_type& b = this->priv_bucket_pointer()[std::ptrdiff_t(this->priv_usable_bucket_count())];
  912. slist_node_algorithms::init_header(b.get_node_ptr());
  913. }
  914. }
  915. inline siterator priv_end_sit() const
  916. { return priv_end_sit(linear_buckets_t()); }
  917. inline siterator priv_end_sit(detail::true_) const
  918. { return siterator(this->priv_bucket_pointer() + std::ptrdiff_t(this->priv_bucket_traits().bucket_count() - bucket_overhead)); }
  919. inline siterator priv_end_sit(detail::false_) const
  920. { return siterator(this->priv_bucket_pointer()->get_node_ptr()); }
  921. inline siterator priv_bucket_lbegin(std::size_t n) const
  922. { siterator s(this->priv_bucket_lbbegin(n)); return ++s; }
  923. inline siterator priv_bucket_lbbegin(std::size_t n) const
  924. { return this->sit_bbegin(this->priv_bucket(n)); }
  925. inline siterator priv_bucket_lend(std::size_t n) const
  926. { return this->sit_end(this->priv_bucket(n)); }
  927. inline std::size_t priv_bucket_size(std::size_t n) const
  928. { return slist_node_algorithms::count(this->priv_bucket(n).get_node_ptr())-1u; }
  929. inline bool priv_bucket_empty(std::size_t n) const
  930. { return slist_node_algorithms::is_empty(this->priv_bucket(n).get_node_ptr()); }
  931. inline bool priv_bucket_empty(bucket_ptr p) const
  932. { return slist_node_algorithms::is_empty(p->get_node_ptr()); }
  933. static inline siterator priv_bucket_lbegin(bucket_type &b)
  934. { return siterator(slist_node_traits::get_next(b.get_node_ptr())); }
  935. static inline siterator priv_bucket_lbbegin(bucket_type& b)
  936. { return siterator(b.get_node_ptr()); }
  937. static inline siterator priv_bucket_lend(bucket_type& b)
  938. { return siterator(slist_node_algorithms::end_node(b.get_node_ptr())); }
  939. static inline std::size_t priv_bucket_size(const bucket_type& b)
  940. { return slist_node_algorithms::count(b.get_node_ptr())-1u; }
  941. static inline bool priv_bucket_empty(const bucket_type& b)
  942. { return slist_node_algorithms::is_empty(b.get_node_ptr()); }
  943. template<class NodeDisposer>
  944. static std::size_t priv_erase_from_single_bucket
  945. (bucket_type &b, siterator sbefore_first, siterator slast, NodeDisposer node_disposer, detail::true_) //optimize multikey
  946. {
  947. std::size_t n = 0;
  948. siterator const sfirst(++siterator(sbefore_first));
  949. if(sfirst != slast){
  950. node_ptr const nf = dcast_bucket_ptr<node>(sfirst.pointed_node());
  951. node_ptr const nl = dcast_bucket_ptr<node>(slast.pointed_node());
  952. slist_node_ptr const ne = (priv_bucket_lend(b)).pointed_node();
  953. if(group_functions_t::is_first_in_group(nf)) {
  954. // The first node is at the beginning of a group.
  955. if(nl != ne){
  956. group_functions_t::split_group(nl);
  957. }
  958. }
  959. else {
  960. node_ptr const group1 = group_functions_t::split_group(nf);
  961. if(nl != ne) {
  962. node_ptr const group2 = group_functions_t::split_group(nl);
  963. if(nf == group2) { //Both first and last in the same group
  964. //so join group1 and group2
  965. node_ptr const end1 = group_traits::get_next(group1);
  966. node_ptr const end2 = group_traits::get_next(group2);
  967. group_traits::set_next(group1, end2);
  968. group_traits::set_next(nl, end1);
  969. }
  970. }
  971. }
  972. n = slist_node_algorithms::unlink_after_and_dispose(sbefore_first.pointed_node(), slast.pointed_node(), node_disposer);
  973. }
  974. return n;
  975. }
  976. template<class NodeDisposer>
  977. static std::size_t priv_erase_from_single_bucket
  978. (bucket_type &, siterator sbefore_first, siterator slast, NodeDisposer node_disposer, detail::false_) //optimize multikey
  979. {
  980. return slist_node_algorithms::unlink_after_and_dispose(sbefore_first.pointed_node(), slast.pointed_node(), node_disposer);
  981. }
  982. template<class NodeDisposer>
  983. static void priv_erase_node(bucket_type &b, siterator i, NodeDisposer node_disposer, detail::true_) //optimize multikey
  984. {
  985. slist_node_ptr const ne(priv_bucket_lend(b).pointed_node());
  986. slist_node_ptr const nbb(priv_bucket_lbbegin(b).pointed_node());
  987. node_ptr n(dcast_bucket_ptr<node>(i.pointed_node()));
  988. node_ptr pos = node_traits::get_next(group_traits::get_next(n));
  989. node_ptr bn;
  990. node_ptr nn(node_traits::get_next(n));
  991. if(pos != n) {
  992. //Node is the first of the group
  993. bn = group_functions_t::get_prev_to_first_in_group(nbb, n);
  994. //Unlink the rest of the group if it's not the last node of its group
  995. if(nn != ne && group_traits::get_next(nn) == n){
  996. group_algorithms::unlink_after(nn);
  997. }
  998. }
  999. else if(nn != ne && group_traits::get_next(nn) == n){
  1000. //Node is not the end of the group
  1001. bn = group_traits::get_next(n);
  1002. group_algorithms::unlink_after(nn);
  1003. }
  1004. else{
  1005. //Node is the end of the group
  1006. bn = group_traits::get_next(n);
  1007. node_ptr const x(group_algorithms::get_previous_node(n));
  1008. group_algorithms::unlink_after(x);
  1009. }
  1010. slist_node_algorithms::unlink_after_and_dispose(bn, node_disposer);
  1011. }
  1012. template<class NodeDisposer>
  1013. inline static void priv_erase_node(bucket_type &b, siterator i, NodeDisposer node_disposer, detail::false_) //!optimize multikey
  1014. {
  1015. slist_node_ptr bi = slist_node_algorithms::get_previous_node(b.get_node_ptr(), i.pointed_node());
  1016. slist_node_algorithms::unlink_after_and_dispose(bi, node_disposer);
  1017. }
  1018. template<class NodeDisposer, bool OptimizeMultikey>
  1019. std::size_t priv_erase_node_range( siterator const &before_first_it, std::size_t const first_bucket
  1020. , siterator const &last_it, std::size_t const last_bucket
  1021. , NodeDisposer node_disposer, detail::bool_<OptimizeMultikey> optimize_multikey_tag)
  1022. {
  1023. std::size_t num_erased(0);
  1024. siterator last_step_before_it;
  1025. if(first_bucket != last_bucket){
  1026. bucket_type *b = &this->priv_bucket(0);
  1027. num_erased += this->priv_erase_from_single_bucket
  1028. (b[first_bucket], before_first_it, this->priv_bucket_lend(first_bucket), node_disposer, optimize_multikey_tag);
  1029. for(std::size_t i = 0, n = (last_bucket - first_bucket - 1); i != n; ++i){
  1030. num_erased += this->priv_erase_whole_bucket(b[first_bucket+i+1], node_disposer);
  1031. }
  1032. last_step_before_it = this->priv_bucket_lbbegin(last_bucket);
  1033. }
  1034. else{
  1035. last_step_before_it = before_first_it;
  1036. }
  1037. num_erased += this->priv_erase_from_single_bucket
  1038. (this->priv_bucket(last_bucket), last_step_before_it, last_it, node_disposer, optimize_multikey_tag);
  1039. return num_erased;
  1040. }
  1041. static siterator priv_get_last(bucket_type &b, detail::true_) //optimize multikey
  1042. {
  1043. //First find the last node of p's group.
  1044. //This requires checking the first node of the next group or
  1045. //the bucket node.
  1046. slist_node_ptr end_ptr(sit_end(b).pointed_node());
  1047. slist_node_ptr last_node_group(b.get_node_ptr());
  1048. slist_node_ptr possible_end(slist_node_traits::get_next(last_node_group));
  1049. while(end_ptr != possible_end){
  1050. last_node_group = group_traits::get_next(dcast_bucket_ptr<node>(possible_end));
  1051. possible_end = slist_node_traits::get_next(last_node_group);
  1052. }
  1053. return siterator(last_node_group);
  1054. }
  1055. inline static siterator priv_get_last(bucket_type &b, detail::false_) //NOT optimize multikey
  1056. {
  1057. slist_node_ptr p = b.get_node_ptr();
  1058. return siterator(slist_node_algorithms::get_previous_node(p, slist_node_algorithms::end_node(p)));
  1059. }
  1060. template<class NodeDisposer>
  1061. static inline std::size_t priv_erase_whole_bucket(bucket_type &b, NodeDisposer node_disposer)
  1062. { return slist_node_algorithms::detach_and_dispose(b.get_node_ptr(), node_disposer); }
  1063. static siterator priv_get_previous(bucket_type &b, siterator i, detail::true_) //optimize multikey
  1064. {
  1065. node_ptr const elem(dcast_bucket_ptr<node>(i.pointed_node()));
  1066. node_ptr const prev_in_group(group_traits::get_next(elem));
  1067. bool const first_in_group = node_traits::get_next(prev_in_group) != elem;
  1068. slist_node_ptr n = first_in_group
  1069. ? group_functions_t::get_prev_to_first_in_group(b.get_node_ptr(), elem)
  1070. : group_traits::get_next(elem)
  1071. ;
  1072. return siterator(n);
  1073. }
  1074. inline static siterator priv_get_previous(bucket_type &b, siterator i, detail::false_) //NOT optimize multikey
  1075. { return siterator(slist_node_algorithms::get_previous_node(b.get_node_ptr(), i.pointed_node())); }
  1076. template<class Disposer>
  1077. struct typeof_node_disposer
  1078. {
  1079. typedef node_cast_adaptor
  1080. < detail::node_disposer< Disposer, value_traits, CommonSListAlgorithms>
  1081. , slist_node_ptr, node_ptr > type;
  1082. };
  1083. template<class Disposer>
  1084. inline typename typeof_node_disposer<Disposer>::type
  1085. make_node_disposer(const Disposer &disposer) const
  1086. {
  1087. typedef typename typeof_node_disposer<Disposer>::type return_t;
  1088. return return_t(disposer, &this->priv_value_traits());
  1089. }
  1090. static inline bucket_ptr to_ptr(bucket_type &b)
  1091. { return pointer_traits<bucket_ptr>::pointer_to(b); }
  1092. static inline siterator sit_bbegin(bucket_type& b)
  1093. { return siterator(b.get_node_ptr()); }
  1094. static inline siterator sit_begin(bucket_type& b)
  1095. { return siterator(b.begin_ptr()); }
  1096. static inline siterator sit_end(bucket_type& b)
  1097. { return siterator(slist_node_algorithms::end_node(b.get_node_ptr())); }
  1098. inline static std::size_t priv_stored_hash(siterator s, detail::true_) //store_hash
  1099. { return node_traits::get_hash(dcast_bucket_ptr<node>(s.pointed_node())); }
  1100. inline static std::size_t priv_stored_hash(siterator, detail::false_) //NO store_hash
  1101. { return std::size_t(-1); }
  1102. inline node &priv_value_to_node(reference v)
  1103. { return *this->priv_value_traits().to_node_ptr(v); }
  1104. inline const node &priv_value_to_node(const_reference v) const
  1105. { return *this->priv_value_traits().to_node_ptr(v); }
  1106. inline node_ptr priv_value_to_node_ptr(reference v)
  1107. { return this->priv_value_traits().to_node_ptr(v); }
  1108. inline const_node_ptr priv_value_to_node_ptr(const_reference v) const
  1109. { return this->priv_value_traits().to_node_ptr(v); }
  1110. inline reference priv_value_from_siterator(siterator s)
  1111. { return *this->priv_value_traits().to_value_ptr(dcast_bucket_ptr<node>(s.pointed_node())); }
  1112. inline const_reference priv_value_from_siterator(siterator s) const
  1113. { return *this->priv_value_traits().to_value_ptr(dcast_bucket_ptr<node>(s.pointed_node())); }
  1114. static void priv_init_buckets(const bucket_ptr buckets_ptr, const std::size_t bucket_cnt)
  1115. {
  1116. bucket_ptr buckets_it = buckets_ptr;
  1117. for (std::size_t bucket_i = 0; bucket_i != bucket_cnt; ++buckets_it, ++bucket_i) {
  1118. slist_node_algorithms::init_header(buckets_it->get_node_ptr());
  1119. }
  1120. }
  1121. void priv_clear_buckets(const bucket_ptr buckets_ptr, const std::size_t bucket_cnt)
  1122. {
  1123. bucket_ptr buckets_it = buckets_ptr;
  1124. for(std::size_t bucket_i = 0; bucket_i != bucket_cnt; ++buckets_it, ++bucket_i){
  1125. bucket_type &b = *buckets_it;
  1126. BOOST_IF_CONSTEXPR(safemode_or_autounlink){
  1127. slist_node_algorithms::detach_and_dispose(b.get_node_ptr(), this->make_node_disposer(detail::null_disposer()));
  1128. }
  1129. else{
  1130. slist_node_algorithms::init_header(b.get_node_ptr());
  1131. }
  1132. }
  1133. }
  1134. inline std::size_t priv_stored_or_compute_hash(const value_type &v, detail::true_) const //For store_hash == true
  1135. { return node_traits::get_hash(this->priv_value_traits().to_node_ptr(v)); }
  1136. typedef hashtable_iterator<bucket_plus_vtraits, LinearBuckets, false> iterator;
  1137. typedef hashtable_iterator<bucket_plus_vtraits, LinearBuckets, true> const_iterator;
  1138. inline iterator end() BOOST_NOEXCEPT
  1139. { return this->build_iterator(this->priv_end_sit(), bucket_ptr()); }
  1140. inline const_iterator end() const BOOST_NOEXCEPT
  1141. { return this->cend(); }
  1142. inline const_iterator cend() const BOOST_NOEXCEPT
  1143. { return this->build_const_iterator(this->priv_end_sit(), bucket_ptr()); }
  1144. inline iterator build_iterator(siterator s, bucket_ptr p)
  1145. { return this->build_iterator(s, p, linear_buckets_t()); }
  1146. inline iterator build_iterator(siterator s, bucket_ptr p, detail::true_) //linear buckets
  1147. { return iterator(s, p, this->priv_value_traits_ptr()); }
  1148. inline iterator build_iterator(siterator s, bucket_ptr, detail::false_) //!linear buckets
  1149. { return iterator(s, &this->get_bucket_value_traits()); }
  1150. inline const_iterator build_const_iterator(siterator s, bucket_ptr p) const
  1151. { return this->build_const_iterator(s, p, linear_buckets_t()); }
  1152. inline const_iterator build_const_iterator(siterator s, bucket_ptr p, detail::true_) const //linear buckets
  1153. { return const_iterator(s, p, this->priv_value_traits_ptr()); }
  1154. inline const_iterator build_const_iterator(siterator s, bucket_ptr, detail::false_) const //!linear buckets
  1155. { return const_iterator(s, &this->get_bucket_value_traits()); }
  1156. };
  1157. template<class Hash, class>
  1158. struct get_hash
  1159. {
  1160. typedef Hash type;
  1161. };
  1162. template<class T>
  1163. struct get_hash<void, T>
  1164. {
  1165. typedef detail::internal_hash_functor<T> type;
  1166. };
  1167. template<class EqualTo, class>
  1168. struct get_equal_to
  1169. {
  1170. typedef EqualTo type;
  1171. };
  1172. template<class T>
  1173. struct get_equal_to<void, T>
  1174. {
  1175. typedef value_equal<T> type;
  1176. };
  1177. template<class KeyOfValue, class T>
  1178. struct get_hash_key_of_value
  1179. {
  1180. typedef KeyOfValue type;
  1181. };
  1182. template<class T>
  1183. struct get_hash_key_of_value<void, T>
  1184. {
  1185. typedef ::boost::intrusive::detail::identity<T> type;
  1186. };
  1187. template<class T, class VoidOrKeyOfValue>
  1188. struct hash_key_types_base
  1189. {
  1190. typedef typename get_hash_key_of_value
  1191. < VoidOrKeyOfValue, T>::type key_of_value;
  1192. typedef typename key_of_value::type key_type;
  1193. };
  1194. template<class T, class VoidOrKeyOfValue, class VoidOrKeyHash>
  1195. struct hash_key_hash
  1196. : get_hash
  1197. < VoidOrKeyHash
  1198. , typename hash_key_types_base<T, VoidOrKeyOfValue>::key_type
  1199. >
  1200. {};
  1201. template<class T, class VoidOrKeyOfValue, class VoidOrKeyEqual>
  1202. struct hash_key_equal
  1203. : get_equal_to
  1204. < VoidOrKeyEqual
  1205. , typename hash_key_types_base<T, VoidOrKeyOfValue>::key_type
  1206. >
  1207. {};
  1208. //bucket_hash_t
  1209. //Stores bucket_plus_vtraits plust the hash function
  1210. template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyHash, class BucketTraits, bool LinearBuckets>
  1211. struct bucket_hash_t
  1212. //Use public inheritance to avoid MSVC bugs with closures
  1213. : public detail::ebo_functor_holder
  1214. <typename hash_key_hash < typename bucket_plus_vtraits<ValueTraits,BucketTraits, LinearBuckets >::value_traits::value_type
  1215. , VoidOrKeyOfValue
  1216. , VoidOrKeyHash
  1217. >::type
  1218. >
  1219. , bucket_plus_vtraits<ValueTraits, BucketTraits, LinearBuckets> //4
  1220. {
  1221. private:
  1222. BOOST_MOVABLE_BUT_NOT_COPYABLE(bucket_hash_t)
  1223. public:
  1224. typedef typename bucket_plus_vtraits
  1225. <ValueTraits,BucketTraits, LinearBuckets>::value_traits value_traits;
  1226. typedef typename value_traits::value_type value_type;
  1227. typedef typename value_traits::node_traits node_traits;
  1228. typedef hash_key_hash
  1229. < value_type, VoidOrKeyOfValue, VoidOrKeyHash> hash_key_hash_t;
  1230. typedef typename hash_key_hash_t::type hasher;
  1231. typedef typename hash_key_types_base<value_type, VoidOrKeyOfValue>::key_of_value key_of_value;
  1232. typedef BucketTraits bucket_traits;
  1233. typedef bucket_plus_vtraits<ValueTraits, BucketTraits, LinearBuckets> bucket_plus_vtraits_t;
  1234. typedef detail::ebo_functor_holder<hasher> base_t;
  1235. inline bucket_hash_t(const ValueTraits &val_traits, const bucket_traits &b_traits, const hasher & h)
  1236. : base_t(h)
  1237. , bucket_plus_vtraits_t(val_traits, b_traits)
  1238. {}
  1239. inline bucket_hash_t(BOOST_RV_REF(bucket_hash_t) other)
  1240. : base_t(BOOST_MOVE_BASE(base_t, other))
  1241. , bucket_plus_vtraits_t(BOOST_MOVE_BASE(bucket_plus_vtraits_t, other))
  1242. {}
  1243. template<class K>
  1244. inline std::size_t priv_hash(const K &k) const
  1245. { return this->base_t::operator()(k); }
  1246. inline const hasher &priv_hasher() const
  1247. { return this->base_t::get(); }
  1248. inline hasher &priv_hasher()
  1249. { return this->base_t::get(); }
  1250. using bucket_plus_vtraits_t::priv_stored_or_compute_hash; //For store_hash == true
  1251. inline std::size_t priv_stored_or_compute_hash(const value_type &v, detail::false_) const //For store_hash == false
  1252. { return this->priv_hasher()(key_of_value()(v)); }
  1253. };
  1254. template<class ValueTraits, class BucketTraits, class VoidOrKeyOfValue, class VoidOrKeyEqual, bool LinearBuckets>
  1255. struct hashtable_equal_holder
  1256. {
  1257. typedef detail::ebo_functor_holder
  1258. < typename hash_key_equal < typename bucket_plus_vtraits
  1259. <ValueTraits, BucketTraits, LinearBuckets>::value_traits::value_type
  1260. , VoidOrKeyOfValue
  1261. , VoidOrKeyEqual
  1262. >::type
  1263. > type;
  1264. };
  1265. //bucket_hash_equal_t
  1266. //Stores bucket_hash_t and the equality function when the first
  1267. //non-empty bucket shall not be cached.
  1268. template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyHash, class VoidOrKeyEqual, class BucketTraits, bool LinearBuckets, bool>
  1269. struct bucket_hash_equal_t
  1270. //Use public inheritance to avoid MSVC bugs with closures
  1271. : public bucket_hash_t<ValueTraits, VoidOrKeyOfValue, VoidOrKeyHash, BucketTraits, LinearBuckets> //3
  1272. , public hashtable_equal_holder<ValueTraits, BucketTraits, VoidOrKeyOfValue, VoidOrKeyEqual, LinearBuckets>::type //equal
  1273. {
  1274. private:
  1275. BOOST_MOVABLE_BUT_NOT_COPYABLE(bucket_hash_equal_t)
  1276. public:
  1277. typedef typename hashtable_equal_holder
  1278. < ValueTraits, BucketTraits, VoidOrKeyOfValue
  1279. , VoidOrKeyEqual, LinearBuckets>::type equal_holder_t;
  1280. typedef bucket_hash_t< ValueTraits, VoidOrKeyOfValue
  1281. , VoidOrKeyHash, BucketTraits
  1282. , LinearBuckets> bucket_hash_type;
  1283. typedef bucket_plus_vtraits
  1284. <ValueTraits, BucketTraits, LinearBuckets> bucket_plus_vtraits_t;
  1285. typedef ValueTraits value_traits;
  1286. typedef typename equal_holder_t::functor_type key_equal;
  1287. typedef typename bucket_hash_type::hasher hasher;
  1288. typedef BucketTraits bucket_traits;
  1289. typedef typename bucket_plus_vtraits_t::siterator siterator;
  1290. typedef typename bucket_plus_vtraits_t::const_siterator const_siterator;
  1291. typedef typename bucket_plus_vtraits_t::bucket_type bucket_type;
  1292. typedef typename bucket_plus_vtraits_t::slist_node_algorithms slist_node_algorithms;
  1293. typedef typename unordered_bucket_ptr_impl
  1294. <value_traits>::type bucket_ptr;
  1295. bucket_hash_equal_t(const ValueTraits &val_traits, const bucket_traits &b_traits, const hasher & h, const key_equal &e)
  1296. : bucket_hash_type(val_traits, b_traits, h)
  1297. , equal_holder_t(e)
  1298. {}
  1299. inline bucket_hash_equal_t(BOOST_RV_REF(bucket_hash_equal_t) other)
  1300. : bucket_hash_type(BOOST_MOVE_BASE(bucket_hash_type, other))
  1301. , equal_holder_t(BOOST_MOVE_BASE(equal_holder_t, other))
  1302. {}
  1303. inline bucket_ptr priv_get_cache()
  1304. { return this->priv_bucket_pointer(); }
  1305. inline void priv_set_cache(bucket_ptr)
  1306. {}
  1307. inline void priv_set_cache_bucket_num(std::size_t)
  1308. {}
  1309. inline std::size_t priv_get_cache_bucket_num()
  1310. { return 0u; }
  1311. inline void priv_init_cache()
  1312. {}
  1313. inline void priv_swap_cache(bucket_hash_equal_t &)
  1314. {}
  1315. siterator priv_begin(bucket_ptr &pbucketptr) const
  1316. {
  1317. std::size_t n = 0;
  1318. std::size_t bucket_cnt = this->priv_usable_bucket_count();
  1319. for (n = 0; n < bucket_cnt; ++n){
  1320. bucket_type &b = this->priv_bucket(n);
  1321. if(!slist_node_algorithms::is_empty(b.get_node_ptr())){
  1322. pbucketptr = this->to_ptr(b);
  1323. return siterator(b.begin_ptr());
  1324. }
  1325. }
  1326. pbucketptr = this->priv_invalid_bucket_ptr();
  1327. return this->priv_end_sit();
  1328. }
  1329. inline void priv_insertion_update_cache(std::size_t)
  1330. {}
  1331. inline void priv_erasure_update_cache_range(std::size_t, std::size_t)
  1332. {}
  1333. inline void priv_erasure_update_cache(bucket_ptr)
  1334. {}
  1335. inline void priv_erasure_update_cache()
  1336. {}
  1337. inline const key_equal &priv_equal() const
  1338. { return this->equal_holder_t::get(); }
  1339. inline key_equal &priv_equal()
  1340. { return this->equal_holder_t::get(); }
  1341. };
  1342. //bucket_hash_equal_t
  1343. //Stores bucket_hash_t and the equality function when the first
  1344. //non-empty bucket shall be cached.
  1345. template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyHash, class VoidOrKeyEqual, class BucketTraits, bool LinearBuckets> //cache_begin == true version
  1346. struct bucket_hash_equal_t<ValueTraits, VoidOrKeyOfValue, VoidOrKeyHash, VoidOrKeyEqual, BucketTraits, LinearBuckets, true>
  1347. //Use public inheritance to avoid MSVC bugs with closures
  1348. : public bucket_hash_t<ValueTraits, VoidOrKeyOfValue, VoidOrKeyHash, BucketTraits, LinearBuckets> //2
  1349. , public hashtable_equal_holder<ValueTraits, BucketTraits, VoidOrKeyOfValue, VoidOrKeyEqual, LinearBuckets>::type
  1350. {
  1351. private:
  1352. BOOST_MOVABLE_BUT_NOT_COPYABLE(bucket_hash_equal_t)
  1353. public:
  1354. typedef typename hashtable_equal_holder
  1355. < ValueTraits, BucketTraits
  1356. , VoidOrKeyOfValue, VoidOrKeyEqual, LinearBuckets>::type equal_holder_t;
  1357. typedef bucket_plus_vtraits
  1358. < ValueTraits, BucketTraits, LinearBuckets> bucket_plus_vtraits_t;
  1359. typedef ValueTraits value_traits;
  1360. typedef typename equal_holder_t::functor_type key_equal;
  1361. typedef bucket_hash_t
  1362. < ValueTraits, VoidOrKeyOfValue
  1363. , VoidOrKeyHash, BucketTraits, LinearBuckets> bucket_hash_type;
  1364. typedef typename bucket_hash_type::hasher hasher;
  1365. typedef BucketTraits bucket_traits;
  1366. typedef typename bucket_plus_vtraits_t::siterator siterator;
  1367. typedef typename bucket_plus_vtraits_t::slist_node_algorithms slist_node_algorithms;
  1368. bucket_hash_equal_t(const ValueTraits &val_traits, const bucket_traits &b_traits, const hasher & h, const key_equal &e)
  1369. : bucket_hash_type(val_traits, b_traits, h)
  1370. , equal_holder_t(e)
  1371. {}
  1372. inline bucket_hash_equal_t(BOOST_RV_REF(bucket_hash_equal_t) other)
  1373. : bucket_hash_type(BOOST_MOVE_BASE(bucket_hash_type, other))
  1374. , equal_holder_t(BOOST_MOVE_BASE(equal_holder_t, other))
  1375. {}
  1376. typedef typename unordered_bucket_ptr_impl
  1377. <typename bucket_hash_type::value_traits>::type bucket_ptr;
  1378. inline bucket_ptr priv_get_cache() const
  1379. { return cached_begin_; }
  1380. inline void priv_set_cache(bucket_ptr p)
  1381. { cached_begin_ = p; }
  1382. inline void priv_set_cache_bucket_num(std::size_t insertion_bucket)
  1383. {
  1384. BOOST_INTRUSIVE_INVARIANT_ASSERT(insertion_bucket <= this->priv_usable_bucket_count());
  1385. this->cached_begin_ = this->priv_bucket_pointer() + std::ptrdiff_t(insertion_bucket);
  1386. }
  1387. inline std::size_t priv_get_cache_bucket_num()
  1388. { return std::size_t(this->cached_begin_ - this->priv_bucket_pointer()); }
  1389. inline void priv_init_cache()
  1390. { this->cached_begin_ = this->priv_past_usable_bucket_ptr(); }
  1391. inline void priv_swap_cache(bucket_hash_equal_t &other)
  1392. { ::boost::adl_move_swap(this->cached_begin_, other.cached_begin_); }
  1393. siterator priv_begin(bucket_ptr& pbucketptr) const
  1394. {
  1395. pbucketptr = this->cached_begin_;
  1396. if(this->cached_begin_ == this->priv_past_usable_bucket_ptr()){
  1397. return this->priv_end_sit();
  1398. }
  1399. else{
  1400. return siterator(cached_begin_->begin_ptr());
  1401. }
  1402. }
  1403. void priv_insertion_update_cache(std::size_t insertion_bucket)
  1404. {
  1405. BOOST_INTRUSIVE_INVARIANT_ASSERT(insertion_bucket < this->priv_usable_bucket_count());
  1406. bucket_ptr p = this->priv_bucket_pointer() + std::ptrdiff_t(insertion_bucket);
  1407. if(p < this->cached_begin_){
  1408. this->cached_begin_ = p;
  1409. }
  1410. }
  1411. inline const key_equal &priv_equal() const
  1412. { return this->equal_holder_t::get(); }
  1413. inline key_equal &priv_equal()
  1414. { return this->equal_holder_t::get(); }
  1415. void priv_erasure_update_cache_range(std::size_t first_bucket_num, std::size_t last_bucket_num)
  1416. {
  1417. //If the last bucket is the end, the cache must be updated
  1418. //to the last position if all
  1419. if(this->priv_get_cache_bucket_num() == first_bucket_num &&
  1420. this->priv_bucket_empty(first_bucket_num) ){
  1421. this->priv_set_cache(this->priv_bucket_pointer() + std::ptrdiff_t(last_bucket_num));
  1422. this->priv_erasure_update_cache();
  1423. }
  1424. }
  1425. void priv_erasure_update_cache(bucket_ptr first_bucket)
  1426. {
  1427. //If the last bucket is the end, the cache must be updated
  1428. //to the last position if all
  1429. if (this->priv_get_cache() == first_bucket &&
  1430. this->priv_bucket_empty(first_bucket)) {
  1431. this->priv_erasure_update_cache();
  1432. }
  1433. }
  1434. void priv_erasure_update_cache()
  1435. {
  1436. const bucket_ptr cache_end = this->priv_past_usable_bucket_ptr();
  1437. while( cached_begin_ != cache_end) {
  1438. if (!slist_node_algorithms::is_empty(cached_begin_->get_node_ptr())) {
  1439. return;
  1440. }
  1441. ++cached_begin_;
  1442. }
  1443. }
  1444. bucket_ptr cached_begin_;
  1445. };
  1446. //This wrapper around size_traits is used
  1447. //to maintain minimal container size with compilers like MSVC
  1448. //that have problems with EBO and multiple empty base classes
  1449. template<class DeriveFrom, class SizeType, bool>
  1450. struct hashtable_size_wrapper
  1451. : public DeriveFrom
  1452. {
  1453. private:
  1454. BOOST_MOVABLE_BUT_NOT_COPYABLE(hashtable_size_wrapper)
  1455. public:
  1456. template<class Base, class Arg0, class Arg1, class Arg2>
  1457. hashtable_size_wrapper( BOOST_FWD_REF(Base) base, BOOST_FWD_REF(Arg0) arg0
  1458. , BOOST_FWD_REF(Arg1) arg1, BOOST_FWD_REF(Arg2) arg2)
  1459. : DeriveFrom( ::boost::forward<Base>(base)
  1460. , ::boost::forward<Arg0>(arg0)
  1461. , ::boost::forward<Arg1>(arg1)
  1462. , ::boost::forward<Arg2>(arg2))
  1463. {}
  1464. typedef detail::size_holder < true, SizeType> size_traits;//size_traits
  1465. inline hashtable_size_wrapper(BOOST_RV_REF(hashtable_size_wrapper) other)
  1466. : DeriveFrom(BOOST_MOVE_BASE(DeriveFrom, other))
  1467. {}
  1468. size_traits size_traits_;
  1469. typedef const size_traits & size_traits_const_t;
  1470. typedef size_traits & size_traits_t;
  1471. inline SizeType get_hashtable_size_wrapper_size() const
  1472. { return size_traits_.get_size(); }
  1473. inline void set_hashtable_size_wrapper_size(SizeType s)
  1474. { size_traits_.set_size(s); }
  1475. inline void inc_hashtable_size_wrapper_size()
  1476. { size_traits_.increment(); }
  1477. inline void dec_hashtable_size_wrapper_size()
  1478. { size_traits_.decrement(); }
  1479. inline size_traits_t priv_size_traits()
  1480. { return size_traits_; }
  1481. };
  1482. template<class DeriveFrom, class SizeType>
  1483. struct hashtable_size_wrapper<DeriveFrom, SizeType, false>
  1484. : public DeriveFrom
  1485. {
  1486. private:
  1487. BOOST_MOVABLE_BUT_NOT_COPYABLE(hashtable_size_wrapper)
  1488. public:
  1489. template<class Base, class Arg0, class Arg1, class Arg2>
  1490. hashtable_size_wrapper( BOOST_FWD_REF(Base) base, BOOST_FWD_REF(Arg0) arg0
  1491. , BOOST_FWD_REF(Arg1) arg1, BOOST_FWD_REF(Arg2) arg2)
  1492. : DeriveFrom( ::boost::forward<Base>(base)
  1493. , ::boost::forward<Arg0>(arg0)
  1494. , ::boost::forward<Arg1>(arg1)
  1495. , ::boost::forward<Arg2>(arg2))
  1496. {}
  1497. inline hashtable_size_wrapper(BOOST_RV_REF(hashtable_size_wrapper) other)
  1498. : DeriveFrom(BOOST_MOVE_BASE(DeriveFrom, other))
  1499. {}
  1500. typedef detail::size_holder< false, SizeType> size_traits;
  1501. typedef size_traits size_traits_const_t;
  1502. typedef size_traits size_traits_t;
  1503. inline SizeType get_hashtable_size_wrapper_size() const
  1504. { return 0u; }
  1505. inline void set_hashtable_size_wrapper_size(SizeType)
  1506. {}
  1507. inline void inc_hashtable_size_wrapper_size()
  1508. {}
  1509. inline void dec_hashtable_size_wrapper_size()
  1510. {}
  1511. inline size_traits priv_size_traits()
  1512. { return size_traits(); }
  1513. };
  1514. template< class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyHash
  1515. , class VoidOrKeyEqual, class BucketTraits, class SizeType
  1516. , std::size_t BoolFlags>
  1517. struct get_hashtable_size_wrapper_bucket
  1518. {
  1519. typedef hashtable_size_wrapper
  1520. < bucket_hash_equal_t
  1521. < ValueTraits, VoidOrKeyOfValue, VoidOrKeyHash, VoidOrKeyEqual
  1522. , BucketTraits
  1523. , 0 != (BoolFlags & hash_bool_flags::linear_buckets_pos)
  1524. , 0 != (BoolFlags & hash_bool_flags::cache_begin_pos)
  1525. > //2
  1526. , SizeType
  1527. , (BoolFlags & hash_bool_flags::incremental_pos) != 0 ||
  1528. (BoolFlags & hash_bool_flags::fastmod_buckets_pos) != 0
  1529. > type;
  1530. };
  1531. //hashdata_internal
  1532. //Stores bucket_hash_equal_t and split_traits
  1533. template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyHash, class VoidOrKeyEqual, class BucketTraits, class SizeType, std::size_t BoolFlags>
  1534. struct hashdata_internal
  1535. : public get_hashtable_size_wrapper_bucket
  1536. <ValueTraits, VoidOrKeyOfValue, VoidOrKeyHash, VoidOrKeyEqual, BucketTraits, SizeType, BoolFlags>::type
  1537. {
  1538. private:
  1539. BOOST_MOVABLE_BUT_NOT_COPYABLE(hashdata_internal)
  1540. public:
  1541. static const bool linear_buckets = 0 != (BoolFlags & hash_bool_flags::linear_buckets_pos);
  1542. typedef typename get_hashtable_size_wrapper_bucket
  1543. <ValueTraits, VoidOrKeyOfValue, VoidOrKeyHash, VoidOrKeyEqual, BucketTraits, SizeType, BoolFlags>::type split_bucket_hash_equal_t;
  1544. typedef typename split_bucket_hash_equal_t::key_equal key_equal;
  1545. typedef typename split_bucket_hash_equal_t::hasher hasher;
  1546. typedef bucket_plus_vtraits
  1547. <ValueTraits, BucketTraits, linear_buckets> bucket_plus_vtraits_t;
  1548. typedef SizeType size_type;
  1549. typedef typename split_bucket_hash_equal_t::size_traits split_traits;
  1550. typedef typename bucket_plus_vtraits_t::bucket_ptr bucket_ptr;
  1551. typedef typename bucket_plus_vtraits_t::const_value_traits_ptr const_value_traits_ptr;
  1552. typedef typename bucket_plus_vtraits_t::siterator siterator;
  1553. typedef typename bucket_plus_vtraits_t::bucket_traits bucket_traits;
  1554. typedef typename bucket_plus_vtraits_t::value_traits value_traits;
  1555. typedef typename bucket_plus_vtraits_t::bucket_type bucket_type;
  1556. typedef typename value_traits::value_type value_type;
  1557. typedef typename value_traits::pointer pointer;
  1558. typedef typename value_traits::const_pointer const_pointer;
  1559. typedef typename pointer_traits<pointer>::reference reference;
  1560. typedef typename pointer_traits
  1561. <const_pointer>::reference const_reference;
  1562. typedef typename value_traits::node_traits node_traits;
  1563. typedef typename node_traits::node node;
  1564. typedef typename node_traits::node_ptr node_ptr;
  1565. typedef typename node_traits::const_node_ptr const_node_ptr;
  1566. typedef typename bucket_plus_vtraits_t::slist_node_algorithms slist_node_algorithms;
  1567. typedef typename bucket_plus_vtraits_t::slist_node_ptr slist_node_ptr;
  1568. typedef hash_key_types_base
  1569. < typename ValueTraits::value_type
  1570. , VoidOrKeyOfValue
  1571. > hash_types_base;
  1572. typedef typename hash_types_base::key_of_value key_of_value;
  1573. static const bool store_hash = store_hash_is_true<node_traits>::value;
  1574. static const bool safemode_or_autounlink = is_safe_autounlink<value_traits::link_mode>::value;
  1575. static const bool stateful_value_traits = detail::is_stateful_value_traits<value_traits>::value;
  1576. typedef detail::bool_<store_hash> store_hash_t;
  1577. typedef detail::transform_iterator
  1578. < siterator
  1579. , downcast_node_to_value_t<value_traits, false> > local_iterator;
  1580. typedef detail::transform_iterator
  1581. < siterator
  1582. , downcast_node_to_value_t<value_traits, true> > const_local_iterator;
  1583. typedef detail::bool_<linear_buckets> linear_buckets_t;
  1584. hashdata_internal( const ValueTraits &val_traits, const bucket_traits &b_traits
  1585. , const hasher & h, const key_equal &e)
  1586. : split_bucket_hash_equal_t(val_traits, b_traits, h, e)
  1587. {}
  1588. inline hashdata_internal(BOOST_RV_REF(hashdata_internal) other)
  1589. : split_bucket_hash_equal_t(BOOST_MOVE_BASE(split_bucket_hash_equal_t, other))
  1590. {}
  1591. inline typename split_bucket_hash_equal_t::size_traits_t priv_split_traits()
  1592. { return this->priv_size_traits(); }
  1593. ~hashdata_internal()
  1594. { this->priv_clear_buckets(); }
  1595. using split_bucket_hash_equal_t::priv_clear_buckets;
  1596. void priv_clear_buckets()
  1597. {
  1598. const std::size_t cache_num = this->priv_get_cache_bucket_num();
  1599. this->priv_clear_buckets(this->priv_get_cache(), this->priv_usable_bucket_count() - cache_num);
  1600. }
  1601. void priv_clear_buckets_and_cache()
  1602. {
  1603. this->priv_clear_buckets();
  1604. this->priv_init_cache();
  1605. }
  1606. void priv_init_buckets_and_cache()
  1607. {
  1608. this->priv_init_buckets(this->priv_bucket_pointer(), this->priv_usable_bucket_count());
  1609. this->priv_init_cache();
  1610. }
  1611. typedef typename bucket_plus_vtraits_t::iterator iterator;
  1612. typedef typename bucket_plus_vtraits_t::const_iterator const_iterator;
  1613. //public functions
  1614. inline SizeType split_count() const BOOST_NOEXCEPT
  1615. { return this->split_bucket_hash_equal_t::get_hashtable_size_wrapper_size(); }
  1616. inline void split_count(SizeType s) BOOST_NOEXCEPT
  1617. { this->split_bucket_hash_equal_t::set_hashtable_size_wrapper_size(s); }
  1618. //public functions
  1619. inline void inc_split_count() BOOST_NOEXCEPT
  1620. { this->split_bucket_hash_equal_t::inc_hashtable_size_wrapper_size(); }
  1621. inline void dec_split_count() BOOST_NOEXCEPT
  1622. { this->split_bucket_hash_equal_t::dec_hashtable_size_wrapper_size(); }
  1623. inline static SizeType initial_split_from_bucket_count(SizeType bc) BOOST_NOEXCEPT
  1624. {
  1625. BOOST_IF_CONSTEXPR(fastmod_buckets) {
  1626. size_type split;
  1627. split = static_cast<SizeType>(prime_fmod_size::lower_size_index(bc));
  1628. //The passed bucket size must be exactly the supported one
  1629. BOOST_ASSERT(prime_fmod_size::size(split) == bc);
  1630. return split;
  1631. }
  1632. else {
  1633. BOOST_IF_CONSTEXPR(incremental) {
  1634. BOOST_ASSERT(0 == (std::size_t(bc) & (std::size_t(bc) - 1u)));
  1635. return size_type(bc >> 1u);
  1636. }
  1637. else{
  1638. return bc;
  1639. }
  1640. }
  1641. }
  1642. inline static SizeType rehash_split_from_bucket_count(SizeType bc) BOOST_NOEXCEPT
  1643. {
  1644. BOOST_IF_CONSTEXPR(fastmod_buckets) {
  1645. return (initial_split_from_bucket_count)(bc);
  1646. }
  1647. else {
  1648. BOOST_IF_CONSTEXPR(incremental) {
  1649. BOOST_ASSERT(0 == (std::size_t(bc) & (std::size_t(bc) - 1u)));
  1650. return bc;
  1651. }
  1652. else{
  1653. return bc;
  1654. }
  1655. }
  1656. }
  1657. inline iterator iterator_to(reference value) BOOST_NOEXCEPT_IF(!linear_buckets)
  1658. { return iterator_to(value, linear_buckets_t()); }
  1659. const_iterator iterator_to(const_reference value) const BOOST_NOEXCEPT_IF(!linear_buckets)
  1660. { return iterator_to(value, linear_buckets_t()); }
  1661. iterator iterator_to(reference value, detail::true_) //linear_buckets
  1662. {
  1663. const std::size_t h = this->priv_stored_or_compute_hash(value, store_hash_t());
  1664. siterator sit(this->priv_value_to_node_ptr(value));
  1665. return this->build_iterator(sit, this->priv_hash_to_bucket_ptr(h));
  1666. }
  1667. const_iterator iterator_to(const_reference value, detail::true_) const //linear_buckets
  1668. {
  1669. const std::size_t h = this->priv_stored_or_compute_hash(value, store_hash_t());
  1670. siterator const sit = siterator
  1671. ( pointer_traits<node_ptr>::const_cast_from(this->priv_value_to_node_ptr(value))
  1672. );
  1673. return this->build_const_iterator(sit, this->priv_hash_to_bucket_ptr(h));
  1674. }
  1675. static const bool incremental = 0 != (BoolFlags & hash_bool_flags::incremental_pos);
  1676. static const bool power_2_buckets = incremental || (0 != (BoolFlags & hash_bool_flags::power_2_buckets_pos));
  1677. static const bool fastmod_buckets = 0 != (BoolFlags & hash_bool_flags::fastmod_buckets_pos);
  1678. typedef detail::bool_<fastmod_buckets> fastmod_buckets_t;
  1679. inline bucket_type &priv_hash_to_bucket(std::size_t hash_value) const
  1680. { return this->priv_bucket(this->priv_hash_to_nbucket(hash_value)); }
  1681. inline bucket_ptr priv_hash_to_bucket_ptr(std::size_t hash_value) const
  1682. { return this->priv_bucket_ptr(this->priv_hash_to_nbucket(hash_value)); }
  1683. inline size_type priv_hash_to_nbucket(std::size_t hash_value) const
  1684. { return (priv_hash_to_nbucket)(hash_value, fastmod_buckets_t()); }
  1685. inline size_type priv_hash_to_nbucket(std::size_t hash_value, detail::true_) const //fastmod_buckets_t
  1686. { return static_cast<size_type>(prime_fmod_size::position(hash_value, this->split_count())); }
  1687. inline size_type priv_hash_to_nbucket(std::size_t hash_value, detail::false_) const //!fastmod_buckets_t
  1688. {
  1689. return static_cast<size_type>(hash_to_bucket_split<power_2_buckets, incremental>
  1690. (hash_value, this->priv_usable_bucket_count(), this->split_count(), detail::false_()));
  1691. }
  1692. inline iterator iterator_to(reference value, detail::false_) BOOST_NOEXCEPT
  1693. {
  1694. return iterator( siterator(this->priv_value_to_node_ptr(value))
  1695. , &this->get_bucket_value_traits());
  1696. }
  1697. const_iterator iterator_to(const_reference value, detail::false_) const BOOST_NOEXCEPT
  1698. {
  1699. siterator const sit = siterator
  1700. ( pointer_traits<node_ptr>::const_cast_from(this->priv_value_to_node_ptr(value)) );
  1701. return const_iterator(sit, &this->get_bucket_value_traits());
  1702. }
  1703. static local_iterator s_local_iterator_to(reference value) BOOST_NOEXCEPT
  1704. {
  1705. BOOST_INTRUSIVE_STATIC_ASSERT((!stateful_value_traits));
  1706. siterator sit(value_traits::to_node_ptr(value));
  1707. return local_iterator(sit, const_value_traits_ptr());
  1708. }
  1709. static const_local_iterator s_local_iterator_to(const_reference value) BOOST_NOEXCEPT
  1710. {
  1711. BOOST_INTRUSIVE_STATIC_ASSERT((!stateful_value_traits));
  1712. siterator const sit = siterator
  1713. ( pointer_traits<node_ptr>::const_cast_from
  1714. (value_traits::to_node_ptr(value))
  1715. );
  1716. return const_local_iterator(sit, const_value_traits_ptr());
  1717. }
  1718. local_iterator local_iterator_to(reference value) BOOST_NOEXCEPT
  1719. {
  1720. siterator sit(this->priv_value_to_node_ptr(value));
  1721. return local_iterator(sit, this->priv_value_traits_ptr());
  1722. }
  1723. const_local_iterator local_iterator_to(const_reference value) const BOOST_NOEXCEPT
  1724. {
  1725. siterator sit
  1726. ( pointer_traits<node_ptr>::const_cast_from(this->priv_value_to_node_ptr(value)) );
  1727. return const_local_iterator(sit, this->priv_value_traits_ptr());
  1728. }
  1729. inline size_type bucket_count() const BOOST_NOEXCEPT
  1730. { return size_type(this->priv_usable_bucket_count()); }
  1731. inline size_type bucket_size(size_type n) const BOOST_NOEXCEPT
  1732. { return (size_type)this->priv_bucket_size(n); }
  1733. inline bucket_ptr bucket_pointer() const BOOST_NOEXCEPT
  1734. { return this->priv_bucket_pointer(); }
  1735. inline local_iterator begin(size_type n) BOOST_NOEXCEPT
  1736. { return local_iterator(this->priv_bucket_lbegin(n), this->priv_value_traits_ptr()); }
  1737. inline const_local_iterator begin(size_type n) const BOOST_NOEXCEPT
  1738. { return this->cbegin(n); }
  1739. static inline size_type suggested_upper_bucket_count(size_type n) BOOST_NOEXCEPT
  1740. {
  1741. BOOST_IF_CONSTEXPR(fastmod_buckets){
  1742. std::size_t s = prime_fmod_size::upper_size_index(n);
  1743. return static_cast<SizeType>(prime_fmod_size::size(s));
  1744. }
  1745. else{
  1746. return prime_list_holder<0>::suggested_upper_bucket_count(n);
  1747. }
  1748. }
  1749. static inline size_type suggested_lower_bucket_count(size_type n) BOOST_NOEXCEPT
  1750. {
  1751. BOOST_IF_CONSTEXPR(fastmod_buckets){
  1752. std::size_t s = prime_fmod_size::lower_size_index(n);
  1753. return static_cast<SizeType>(prime_fmod_size::size(s));
  1754. }
  1755. else{
  1756. return prime_list_holder<0>::suggested_lower_bucket_count(n);
  1757. }
  1758. }
  1759. const_local_iterator cbegin(size_type n) const BOOST_NOEXCEPT
  1760. {
  1761. return const_local_iterator
  1762. (this->priv_bucket_lbegin(n)
  1763. , this->priv_value_traits_ptr());
  1764. }
  1765. using split_bucket_hash_equal_t::end;
  1766. using split_bucket_hash_equal_t::cend;
  1767. local_iterator end(size_type n) BOOST_NOEXCEPT
  1768. { return local_iterator(this->priv_bucket_lend(n), this->priv_value_traits_ptr()); }
  1769. inline const_local_iterator end(size_type n) const BOOST_NOEXCEPT
  1770. { return this->cend(n); }
  1771. const_local_iterator cend(size_type n) const BOOST_NOEXCEPT
  1772. {
  1773. return const_local_iterator
  1774. ( this->priv_bucket_lend(n)
  1775. , this->priv_value_traits_ptr());
  1776. }
  1777. //Public functions for hashtable_impl
  1778. inline iterator begin() BOOST_NOEXCEPT
  1779. {
  1780. bucket_ptr p;
  1781. siterator s = this->priv_begin(p);
  1782. return this->build_iterator(s, p);
  1783. }
  1784. inline const_iterator begin() const BOOST_NOEXCEPT
  1785. { return this->cbegin(); }
  1786. inline const_iterator cbegin() const BOOST_NOEXCEPT
  1787. {
  1788. bucket_ptr p;
  1789. siterator s = this->priv_begin(p);
  1790. return this->build_const_iterator(s, p);
  1791. }
  1792. inline hasher hash_function() const
  1793. { return this->priv_hasher(); }
  1794. inline key_equal key_eq() const
  1795. { return this->priv_equal(); }
  1796. };
  1797. template< class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyHash
  1798. , class VoidOrKeyEqual, class BucketTraits, class SizeType
  1799. , std::size_t BoolFlags>
  1800. struct get_hashtable_size_wrapper_internal
  1801. {
  1802. typedef hashtable_size_wrapper
  1803. < hashdata_internal
  1804. < ValueTraits
  1805. , VoidOrKeyOfValue, VoidOrKeyHash, VoidOrKeyEqual
  1806. , BucketTraits, SizeType
  1807. , BoolFlags & ~(hash_bool_flags::constant_time_size_pos) //1
  1808. >
  1809. , SizeType
  1810. , (BoolFlags& hash_bool_flags::constant_time_size_pos) != 0
  1811. > type;
  1812. };
  1813. /// @endcond
  1814. //! The class template hashtable is an intrusive hash table container, that
  1815. //! is used to construct intrusive unordered_set and unordered_multiset containers. The
  1816. //! no-throw guarantee holds only, if the VoidOrKeyEqual object and Hasher don't throw.
  1817. //!
  1818. //! hashtable is a semi-intrusive container: each object to be stored in the
  1819. //! container must contain a proper hook, but the container also needs
  1820. //! additional auxiliary memory to work: hashtable needs a pointer to an array
  1821. //! of type `bucket_type` to be passed in the constructor. This bucket array must
  1822. //! have at least the same lifetime as the container. This makes the use of
  1823. //! hashtable more complicated than purely intrusive containers.
  1824. //! `bucket_type` is default-constructible, copyable and assignable
  1825. //!
  1826. //! The template parameter \c T is the type to be managed by the container.
  1827. //! The user can specify additional options and if no options are provided
  1828. //! default options are used.
  1829. //!
  1830. //! The container supports the following options:
  1831. //! \c base_hook<>/member_hook<>/value_traits<>,
  1832. //! \c constant_time_size<>, \c size_type<>, \c hash<> and \c equal<>
  1833. //! \c bucket_traits<>, power_2_buckets<>, cache_begin<> and incremental<>.
  1834. //!
  1835. //! hashtable only provides forward iterators but it provides 4 iterator types:
  1836. //! iterator and const_iterator to navigate through the whole container and
  1837. //! local_iterator and const_local_iterator to navigate through the values
  1838. //! stored in a single bucket. Local iterators are faster and smaller.
  1839. //!
  1840. //! It's not recommended to use non constant-time size hashtables because several
  1841. //! key functions, like "empty()", become non-constant time functions. Non
  1842. //! constant_time size hashtables are mainly provided to support auto-unlink hooks.
  1843. //!
  1844. //! hashtables, does not make automatic rehashings nor
  1845. //! offers functions related to a load factor. Rehashing can be explicitly requested
  1846. //! and the user must provide a new bucket array that will be used from that moment.
  1847. //!
  1848. //! Since no automatic rehashing is done, iterators are never invalidated when
  1849. //! inserting or erasing elements. Iterators are only invalidated when rehashing.
  1850. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1851. template<class T, class ...Options>
  1852. #else
  1853. template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyHash, class VoidOrKeyEqual, class BucketTraits, class SizeType, std::size_t BoolFlags>
  1854. #endif
  1855. class hashtable_impl
  1856. : private get_hashtable_size_wrapper_internal
  1857. <ValueTraits, VoidOrKeyOfValue, VoidOrKeyHash, VoidOrKeyEqual, BucketTraits, SizeType, BoolFlags>::type
  1858. {
  1859. static const bool linear_buckets_flag = (BoolFlags & hash_bool_flags::linear_buckets_pos) != 0;
  1860. typedef typename get_hashtable_size_wrapper_internal
  1861. <ValueTraits, VoidOrKeyOfValue, VoidOrKeyHash, VoidOrKeyEqual, BucketTraits, SizeType, BoolFlags>::type
  1862. internal_type;
  1863. typedef typename internal_type::size_traits size_traits;
  1864. typedef hash_key_types_base
  1865. < typename ValueTraits::value_type
  1866. , VoidOrKeyOfValue
  1867. > hash_types_base;
  1868. public:
  1869. typedef ValueTraits value_traits;
  1870. /// @cond
  1871. typedef BucketTraits bucket_traits;
  1872. typedef bucket_plus_vtraits
  1873. <ValueTraits, BucketTraits, linear_buckets_flag> bucket_plus_vtraits_t;
  1874. typedef typename bucket_plus_vtraits_t::const_value_traits_ptr const_value_traits_ptr;
  1875. typedef detail::bool_<linear_buckets_flag> linear_buckets_t;
  1876. typedef typename internal_type::siterator siterator;
  1877. typedef typename internal_type::const_siterator const_siterator;
  1878. using internal_type::begin;
  1879. using internal_type::cbegin;
  1880. using internal_type::end;
  1881. using internal_type::cend;
  1882. using internal_type::hash_function;
  1883. using internal_type::key_eq;
  1884. using internal_type::bucket_size;
  1885. using internal_type::bucket_count;
  1886. using internal_type::local_iterator_to;
  1887. using internal_type::s_local_iterator_to;
  1888. using internal_type::iterator_to;
  1889. using internal_type::bucket_pointer;
  1890. using internal_type::suggested_upper_bucket_count;
  1891. using internal_type::suggested_lower_bucket_count;
  1892. using internal_type::split_count;
  1893. /// @endcond
  1894. typedef typename value_traits::pointer pointer;
  1895. typedef typename value_traits::const_pointer const_pointer;
  1896. typedef typename value_traits::value_type value_type;
  1897. typedef typename hash_types_base::key_type key_type;
  1898. typedef typename hash_types_base::key_of_value key_of_value;
  1899. typedef typename pointer_traits<pointer>::reference reference;
  1900. typedef typename pointer_traits<const_pointer>::reference const_reference;
  1901. typedef typename pointer_traits<pointer>::difference_type difference_type;
  1902. typedef SizeType size_type;
  1903. typedef typename internal_type::key_equal key_equal;
  1904. typedef typename internal_type::hasher hasher;
  1905. typedef typename internal_type::bucket_type bucket_type;
  1906. typedef typename internal_type::bucket_ptr bucket_ptr;
  1907. typedef typename internal_type::iterator iterator;
  1908. typedef typename internal_type::const_iterator const_iterator;
  1909. typedef typename internal_type::local_iterator local_iterator;
  1910. typedef typename internal_type::const_local_iterator const_local_iterator;
  1911. typedef typename value_traits::node_traits node_traits;
  1912. typedef typename node_traits::node node;
  1913. typedef typename pointer_traits
  1914. <pointer>::template rebind_pointer
  1915. < node >::type node_ptr;
  1916. typedef typename pointer_traits
  1917. <pointer>::template rebind_pointer
  1918. < const node >::type const_node_ptr;
  1919. typedef typename pointer_traits
  1920. <node_ptr>::reference node_reference;
  1921. typedef typename pointer_traits
  1922. <const_node_ptr>::reference const_node_reference;
  1923. typedef typename internal_type::slist_node_algorithms slist_node_algorithms;
  1924. static const bool stateful_value_traits = internal_type::stateful_value_traits;
  1925. static const bool store_hash = internal_type::store_hash;
  1926. static const bool unique_keys = 0 != (BoolFlags & hash_bool_flags::unique_keys_pos);
  1927. static const bool constant_time_size = 0 != (BoolFlags & hash_bool_flags::constant_time_size_pos);
  1928. static const bool cache_begin = 0 != (BoolFlags & hash_bool_flags::cache_begin_pos);
  1929. static const bool compare_hash = 0 != (BoolFlags & hash_bool_flags::compare_hash_pos);
  1930. static const bool incremental = 0 != (BoolFlags & hash_bool_flags::incremental_pos);
  1931. static const bool power_2_buckets = incremental || (0 != (BoolFlags & hash_bool_flags::power_2_buckets_pos));
  1932. static const bool optimize_multikey = optimize_multikey_is_true<node_traits>::value && !unique_keys;
  1933. static const bool linear_buckets = linear_buckets_flag;
  1934. static const bool fastmod_buckets = 0 != (BoolFlags & hash_bool_flags::fastmod_buckets_pos);
  1935. static const std::size_t bucket_overhead = internal_type::bucket_overhead;
  1936. /// @cond
  1937. static const bool is_multikey = !unique_keys;
  1938. private:
  1939. //Configuration error: compare_hash<> can't be specified without store_hash<>
  1940. //See documentation for more explanations
  1941. BOOST_INTRUSIVE_STATIC_ASSERT((!compare_hash || store_hash));
  1942. //Configuration error: fasmod_buckets<> can't be specified with incremental<> or power_2_buckets<>
  1943. //See documentation for more explanations
  1944. BOOST_INTRUSIVE_STATIC_ASSERT(!(fastmod_buckets && power_2_buckets));
  1945. typedef typename internal_type::slist_node_ptr slist_node_ptr;
  1946. typedef typename pointer_traits
  1947. <slist_node_ptr>::template rebind_pointer
  1948. < void >::type void_pointer;
  1949. //We'll define group traits, but these won't be instantiated if
  1950. //optimize_multikey is not true
  1951. typedef unordered_group_adapter<node_traits> group_traits;
  1952. typedef circular_slist_algorithms<group_traits> group_algorithms;
  1953. typedef typename internal_type::store_hash_t store_hash_t;
  1954. typedef detail::bool_<optimize_multikey> optimize_multikey_t;
  1955. typedef detail::bool_<cache_begin> cache_begin_t;
  1956. typedef detail::bool_<power_2_buckets> power_2_buckets_t;
  1957. typedef detail::bool_<fastmod_buckets> fastmod_buckets_t;
  1958. typedef detail::bool_<compare_hash> compare_hash_t;
  1959. typedef typename internal_type::split_traits split_traits;
  1960. typedef group_functions<node_traits> group_functions_t;
  1961. typedef node_functions<node_traits> node_functions_t;
  1962. private:
  1963. //noncopyable, movable
  1964. BOOST_MOVABLE_BUT_NOT_COPYABLE(hashtable_impl)
  1965. static const bool safemode_or_autounlink = internal_type::safemode_or_autounlink;
  1966. //Constant-time size is incompatible with auto-unlink hooks!
  1967. BOOST_INTRUSIVE_STATIC_ASSERT(!(constant_time_size && ((int)value_traits::link_mode == (int)auto_unlink)));
  1968. //Cache begin is incompatible with auto-unlink hooks!
  1969. BOOST_INTRUSIVE_STATIC_ASSERT(!(cache_begin && ((int)value_traits::link_mode == (int)auto_unlink)));
  1970. /// @endcond
  1971. public:
  1972. typedef insert_commit_data_impl insert_commit_data;
  1973. private:
  1974. void default_init_actions()
  1975. {
  1976. this->priv_set_sentinel_bucket();
  1977. this->priv_init_buckets_and_cache();
  1978. this->priv_size_count(size_type(0));
  1979. size_type bucket_sz = this->bucket_count();
  1980. BOOST_INTRUSIVE_INVARIANT_ASSERT(bucket_sz != 0);
  1981. //Check power of two bucket array if the option is activated
  1982. BOOST_INTRUSIVE_INVARIANT_ASSERT
  1983. (!power_2_buckets || (0 == (bucket_sz & (bucket_sz - 1))));
  1984. this->split_count(this->initial_split_from_bucket_count(bucket_sz));
  1985. }
  1986. inline SizeType priv_size_count() const BOOST_NOEXCEPT
  1987. { return this->internal_type::get_hashtable_size_wrapper_size(); }
  1988. inline void priv_size_count(SizeType s) BOOST_NOEXCEPT
  1989. { this->internal_type::set_hashtable_size_wrapper_size(s); }
  1990. inline void priv_size_inc() BOOST_NOEXCEPT
  1991. { this->internal_type::inc_hashtable_size_wrapper_size(); }
  1992. inline void priv_size_dec() BOOST_NOEXCEPT
  1993. { this->internal_type::dec_hashtable_size_wrapper_size(); }
  1994. public:
  1995. //! <b>Requires</b>: buckets must not be being used by any other resource.
  1996. //!
  1997. //! <b>Effects</b>: Constructs an empty unordered_set, storing a reference
  1998. //! to the bucket array and copies of the key_hasher and equal_func functors.
  1999. //!
  2000. //! <b>Complexity</b>: Constant.
  2001. //!
  2002. //! <b>Throws</b>: If value_traits::node_traits::node
  2003. //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
  2004. //! or the copy constructor or invocation of hash_func or equal_func throws.
  2005. //!
  2006. //! <b>Notes</b>: buckets array must be disposed only after
  2007. //! *this is disposed.
  2008. explicit hashtable_impl ( const bucket_traits &b_traits
  2009. , const hasher & hash_func = hasher()
  2010. , const key_equal &equal_func = key_equal()
  2011. , const value_traits &v_traits = value_traits())
  2012. : internal_type(v_traits, b_traits, hash_func, equal_func)
  2013. { this->default_init_actions(); }
  2014. //! <b>Requires</b>: buckets must not be being used by any other resource
  2015. //! and dereferencing iterator must yield an lvalue of type value_type.
  2016. //!
  2017. //! <b>Effects</b>: Constructs an empty container and inserts elements from
  2018. //! [b, e).
  2019. //!
  2020. //! <b>Complexity</b>: If N is distance(b, e): Average case is O(N)
  2021. //! (with a good hash function and with buckets_len >= N),worst case O(N^2).
  2022. //!
  2023. //! <b>Throws</b>: If value_traits::node_traits::node
  2024. //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
  2025. //! or the copy constructor or invocation of hasher or key_equal throws.
  2026. //!
  2027. //! <b>Notes</b>: buckets array must be disposed only after
  2028. //! *this is disposed.
  2029. template<class Iterator>
  2030. hashtable_impl ( bool unique, Iterator b, Iterator e
  2031. , const bucket_traits &b_traits
  2032. , const hasher & hash_func = hasher()
  2033. , const key_equal &equal_func = key_equal()
  2034. , const value_traits &v_traits = value_traits())
  2035. : internal_type(v_traits, b_traits, hash_func, equal_func)
  2036. {
  2037. this->default_init_actions();
  2038. //Now insert
  2039. if(unique)
  2040. this->insert_unique(b, e);
  2041. else
  2042. this->insert_equal(b, e);
  2043. }
  2044. //! <b>Effects</b>: Constructs a container moving resources from another container.
  2045. //! Internal value traits, bucket traits, hasher and comparison are move constructed and
  2046. //! nodes belonging to x are linked to *this.
  2047. //!
  2048. //! <b>Complexity</b>: Constant.
  2049. //!
  2050. //! <b>Throws</b>: If value_traits::node_traits::node's
  2051. //! move constructor throws (this does not happen with predefined Boost.Intrusive hooks)
  2052. //! or the move constructor of value traits, bucket traits, hasher or comparison throws.
  2053. hashtable_impl(BOOST_RV_REF(hashtable_impl) x)
  2054. : internal_type(BOOST_MOVE_BASE(internal_type, x))
  2055. {
  2056. this->priv_swap_cache(x);
  2057. x.priv_init_cache();
  2058. this->priv_size_count(x.priv_size_count());
  2059. x.priv_size_count(size_type(0));
  2060. this->split_count(x.split_count());
  2061. x.split_count(size_type(0));
  2062. }
  2063. //! <b>Effects</b>: Equivalent to swap.
  2064. //!
  2065. hashtable_impl& operator=(BOOST_RV_REF(hashtable_impl) x)
  2066. { this->swap(x); return *this; }
  2067. //! <b>Effects</b>: Detaches all elements from this. The objects in the unordered_set
  2068. //! are not deleted (i.e. no destructors are called).
  2069. //!
  2070. //! <b>Complexity</b>: Linear to the number of elements in the unordered_set, if
  2071. //! it's a safe-mode or auto-unlink value. Otherwise constant.
  2072. //!
  2073. //! <b>Throws</b>: Nothing.
  2074. ~hashtable_impl()
  2075. {}
  2076. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  2077. //! <b>Effects</b>: Returns an iterator pointing to the beginning of the unordered_set.
  2078. //!
  2079. //! <b>Complexity</b>: Amortized constant time.
  2080. //! Worst case (empty unordered_set): O(this->bucket_count())
  2081. //!
  2082. //! <b>Throws</b>: Nothing.
  2083. iterator begin() BOOST_NOEXCEPT;
  2084. //! <b>Effects</b>: Returns a const_iterator pointing to the beginning
  2085. //! of the unordered_set.
  2086. //!
  2087. //! <b>Complexity</b>: Amortized constant time.
  2088. //! Worst case (empty unordered_set): O(this->bucket_count())
  2089. //!
  2090. //! <b>Throws</b>: Nothing.
  2091. const_iterator begin() const BOOST_NOEXCEPT;
  2092. //! <b>Effects</b>: Returns a const_iterator pointing to the beginning
  2093. //! of the unordered_set.
  2094. //!
  2095. //! <b>Complexity</b>: Amortized constant time.
  2096. //! Worst case (empty unordered_set): O(this->bucket_count())
  2097. //!
  2098. //! <b>Throws</b>: Nothing.
  2099. const_iterator cbegin() const BOOST_NOEXCEPT;
  2100. //! <b>Effects</b>: Returns an iterator pointing to the end of the unordered_set.
  2101. //!
  2102. //! <b>Complexity</b>: Constant.
  2103. //!
  2104. //! <b>Throws</b>: Nothing.
  2105. iterator end() BOOST_NOEXCEPT;
  2106. //! <b>Effects</b>: Returns a const_iterator pointing to the end of the unordered_set.
  2107. //!
  2108. //! <b>Complexity</b>: Constant.
  2109. //!
  2110. //! <b>Throws</b>: Nothing.
  2111. const_iterator end() const BOOST_NOEXCEPT;
  2112. //! <b>Effects</b>: Returns a const_iterator pointing to the end of the unordered_set.
  2113. //!
  2114. //! <b>Complexity</b>: Constant.
  2115. //!
  2116. //! <b>Throws</b>: Nothing.
  2117. const_iterator cend() const BOOST_NOEXCEPT;
  2118. //! <b>Effects</b>: Returns the hasher object used by the unordered_set.
  2119. //!
  2120. //! <b>Complexity</b>: Constant.
  2121. //!
  2122. //! <b>Throws</b>: If hasher copy-constructor throws.
  2123. hasher hash_function() const;
  2124. //! <b>Effects</b>: Returns the key_equal object used by the unordered_set.
  2125. //!
  2126. //! <b>Complexity</b>: Constant.
  2127. //!
  2128. //! <b>Throws</b>: If key_equal copy-constructor throws.
  2129. key_equal key_eq() const;
  2130. #endif
  2131. //! <b>Effects</b>: Returns true if the container is empty.
  2132. //!
  2133. //! <b>Complexity</b>: if constant-time size and cache_begin options are disabled,
  2134. //! average constant time (worst case, with empty() == true: O(this->bucket_count()).
  2135. //! Otherwise constant.
  2136. //!
  2137. //! <b>Throws</b>: Nothing.
  2138. bool empty() const BOOST_NOEXCEPT
  2139. {
  2140. BOOST_IF_CONSTEXPR(constant_time_size){
  2141. return !this->size();
  2142. }
  2143. else if(cache_begin){
  2144. return this->begin() == this->end();
  2145. }
  2146. else{
  2147. size_type bucket_cnt = this->bucket_count();
  2148. const bucket_type *b = boost::movelib::to_raw_pointer(this->priv_bucket_pointer());
  2149. for (size_type n = 0; n < bucket_cnt; ++n, ++b){
  2150. if(!slist_node_algorithms::is_empty(b->get_node_ptr())){
  2151. return false;
  2152. }
  2153. }
  2154. return true;
  2155. }
  2156. }
  2157. //! <b>Effects</b>: Returns the number of elements stored in the unordered_set.
  2158. //!
  2159. //! <b>Complexity</b>: Linear to elements contained in *this if
  2160. //! constant_time_size is false. Constant-time otherwise.
  2161. //!
  2162. //! <b>Throws</b>: Nothing.
  2163. size_type size() const BOOST_NOEXCEPT
  2164. {
  2165. BOOST_IF_CONSTEXPR(constant_time_size)
  2166. return this->priv_size_count();
  2167. else{
  2168. std::size_t len = 0;
  2169. std::size_t bucket_cnt = this->bucket_count();
  2170. const bucket_type *b = boost::movelib::to_raw_pointer(this->priv_bucket_pointer());
  2171. for (std::size_t n = 0; n < bucket_cnt; ++n, ++b){
  2172. len += slist_node_algorithms::count(b->get_node_ptr()) - 1u;
  2173. }
  2174. BOOST_INTRUSIVE_INVARIANT_ASSERT((len <= SizeType(-1)));
  2175. return size_type(len);
  2176. }
  2177. }
  2178. //! <b>Requires</b>: the hasher and the equality function unqualified swap
  2179. //! call should not throw.
  2180. //!
  2181. //! <b>Effects</b>: Swaps the contents of two unordered_sets.
  2182. //! Swaps also the contained bucket array and equality and hasher functors.
  2183. //!
  2184. //! <b>Complexity</b>: Constant.
  2185. //!
  2186. //! <b>Throws</b>: If the swap() call for the comparison or hash functors
  2187. //! found using ADL throw. Basic guarantee.
  2188. void swap(hashtable_impl& other)
  2189. {
  2190. //These can throw
  2191. ::boost::adl_move_swap(this->priv_equal(), other.priv_equal());
  2192. ::boost::adl_move_swap(this->priv_hasher(), other.priv_hasher());
  2193. //These can't throw
  2194. ::boost::adl_move_swap(this->priv_bucket_traits(), other.priv_bucket_traits());
  2195. ::boost::adl_move_swap(this->priv_value_traits(), other.priv_value_traits());
  2196. this->priv_swap_cache(other);
  2197. this->priv_size_traits().swap(other.priv_size_traits());
  2198. this->priv_split_traits().swap(other.priv_split_traits());
  2199. }
  2200. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw
  2201. //! Cloner should yield to nodes that compare equal and produce the same
  2202. //! hash than the original node.
  2203. //!
  2204. //! <b>Effects</b>: Erases all the elements from *this
  2205. //! calling Disposer::operator()(pointer), clones all the
  2206. //! elements from src calling Cloner::operator()(const_reference )
  2207. //! and inserts them on *this. The hash function and the equality
  2208. //! predicate are copied from the source.
  2209. //!
  2210. //! If store_hash option is true, this method does not use the hash function.
  2211. //!
  2212. //! If any operation throws, all cloned elements are unlinked and disposed
  2213. //! calling Disposer::operator()(pointer).
  2214. //!
  2215. //! <b>Complexity</b>: Linear to erased plus inserted elements.
  2216. //!
  2217. //! <b>Throws</b>: If cloner or hasher throw or hash or equality predicate copying
  2218. //! throws. Basic guarantee.
  2219. template <class Cloner, class Disposer>
  2220. inline void clone_from(const hashtable_impl &src, Cloner cloner, Disposer disposer)
  2221. { this->priv_clone_from(src, cloner, disposer); }
  2222. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw
  2223. //! Cloner should yield to nodes that compare equal and produce the same
  2224. //! hash than the original node.
  2225. //!
  2226. //! <b>Effects</b>: Erases all the elements from *this
  2227. //! calling Disposer::operator()(pointer), clones all the
  2228. //! elements from src calling Cloner::operator()(reference)
  2229. //! and inserts them on *this. The hash function and the equality
  2230. //! predicate are copied from the source.
  2231. //!
  2232. //! If store_hash option is true, this method does not use the hash function.
  2233. //!
  2234. //! If any operation throws, all cloned elements are unlinked and disposed
  2235. //! calling Disposer::operator()(pointer).
  2236. //!
  2237. //! <b>Complexity</b>: Linear to erased plus inserted elements.
  2238. //!
  2239. //! <b>Throws</b>: If cloner or hasher throw or hash or equality predicate copying
  2240. //! throws. Basic guarantee.
  2241. template <class Cloner, class Disposer>
  2242. inline void clone_from(BOOST_RV_REF(hashtable_impl) src, Cloner cloner, Disposer disposer)
  2243. { this->priv_clone_from(static_cast<hashtable_impl&>(src), cloner, disposer); }
  2244. //! <b>Requires</b>: value must be an lvalue
  2245. //!
  2246. //! <b>Effects</b>: Inserts the value into the unordered_set.
  2247. //!
  2248. //! <b>Returns</b>: An iterator to the inserted value.
  2249. //!
  2250. //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
  2251. //!
  2252. //! <b>Throws</b>: If the internal hasher or the equality functor throws. Strong guarantee.
  2253. //!
  2254. //! <b>Note</b>: Does not affect the validity of iterators and references.
  2255. //! No copy-constructors are called.
  2256. iterator insert_equal(reference value)
  2257. {
  2258. size_type bucket_num;
  2259. std::size_t hash_value;
  2260. siterator prev;
  2261. siterator const it = this->priv_find
  2262. (key_of_value()(value), this->priv_hasher(), this->priv_equal(), bucket_num, hash_value, prev);
  2263. bool const next_is_in_group = optimize_multikey && it != this->priv_end_sit();
  2264. return this->priv_insert_equal_after_find(value, bucket_num, hash_value, prev, next_is_in_group);
  2265. }
  2266. //! <b>Requires</b>: Dereferencing iterator must yield an lvalue
  2267. //! of type value_type.
  2268. //!
  2269. //! <b>Effects</b>: Equivalent to this->insert_equal(t) for each element in [b, e).
  2270. //!
  2271. //! <b>Complexity</b>: Average case O(N), where N is distance(b, e).
  2272. //! Worst case O(N*this->size()).
  2273. //!
  2274. //! <b>Throws</b>: If the internal hasher or the equality functor throws. Basic guarantee.
  2275. //!
  2276. //! <b>Note</b>: Does not affect the validity of iterators and references.
  2277. //! No copy-constructors are called.
  2278. template<class Iterator>
  2279. void insert_equal(Iterator b, Iterator e)
  2280. {
  2281. for (; b != e; ++b)
  2282. this->insert_equal(*b);
  2283. }
  2284. //! <b>Requires</b>: value must be an lvalue
  2285. //!
  2286. //! <b>Effects</b>: Tries to inserts value into the unordered_set.
  2287. //!
  2288. //! <b>Returns</b>: If the value
  2289. //! is not already present inserts it and returns a pair containing the
  2290. //! iterator to the new value and true. If there is an equivalent value
  2291. //! returns a pair containing an iterator to the already present value
  2292. //! and false.
  2293. //!
  2294. //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
  2295. //!
  2296. //! <b>Throws</b>: If the internal hasher or the equality functor throws. Strong guarantee.
  2297. //!
  2298. //! <b>Note</b>: Does not affect the validity of iterators and references.
  2299. //! No copy-constructors are called.
  2300. std::pair<iterator, bool> insert_unique(reference value)
  2301. {
  2302. insert_commit_data commit_data;
  2303. std::pair<iterator, bool> ret = this->insert_unique_check(key_of_value()(value), commit_data);
  2304. if(ret.second){
  2305. ret.first = this->insert_unique_fast_commit(value, commit_data);
  2306. }
  2307. return ret;
  2308. }
  2309. //! <b>Requires</b>: Dereferencing iterator must yield an lvalue
  2310. //! of type value_type.
  2311. //!
  2312. //! <b>Effects</b>: Equivalent to this->insert_unique(t) for each element in [b, e).
  2313. //!
  2314. //! <b>Complexity</b>: Average case O(N), where N is distance(b, e).
  2315. //! Worst case O(N*this->size()).
  2316. //!
  2317. //! <b>Throws</b>: If the internal hasher or the equality functor throws. Basic guarantee.
  2318. //!
  2319. //! <b>Note</b>: Does not affect the validity of iterators and references.
  2320. //! No copy-constructors are called.
  2321. template<class Iterator>
  2322. void insert_unique(Iterator b, Iterator e)
  2323. {
  2324. for (; b != e; ++b)
  2325. this->insert_unique(*b);
  2326. }
  2327. //! <b>Requires</b>: "hash_func" must be a hash function that induces
  2328. //! the same hash values as the stored hasher. The difference is that
  2329. //! "hash_func" hashes the given key instead of the value_type.
  2330. //!
  2331. //! "equal_func" must be a equality function that induces
  2332. //! the same equality as key_equal. The difference is that
  2333. //! "equal_func" compares an arbitrary key with the contained values.
  2334. //!
  2335. //! <b>Effects</b>: Checks if a value can be inserted in the unordered_set, using
  2336. //! a user provided key instead of the value itself.
  2337. //!
  2338. //! <b>Returns</b>: If there is an equivalent value
  2339. //! returns a pair containing an iterator to the already present value
  2340. //! and false. If the value can be inserted returns true in the returned
  2341. //! pair boolean and fills "commit_data" that is meant to be used with
  2342. //! the "insert_commit" function.
  2343. //!
  2344. //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
  2345. //!
  2346. //! <b>Throws</b>: If hash_func or equal_func throw. Strong guarantee.
  2347. //!
  2348. //! <b>Notes</b>: This function is used to improve performance when constructing
  2349. //! a value_type is expensive: if there is an equivalent value
  2350. //! the constructed object must be discarded. Many times, the part of the
  2351. //! node that is used to impose the hash or the equality is much cheaper to
  2352. //! construct than the value_type and this function offers the possibility to
  2353. //! use that the part to check if the insertion will be successful.
  2354. //!
  2355. //! If the check is successful, the user can construct the value_type and use
  2356. //! "insert_commit" to insert the object in constant-time.
  2357. //!
  2358. //! "commit_data" remains valid for a subsequent "insert_commit" only if no more
  2359. //! objects are inserted or erased from the unordered_set.
  2360. //!
  2361. //! After a successful rehashing insert_commit_data remains valid.
  2362. template<class KeyType, class KeyHasher, class KeyEqual>
  2363. std::pair<iterator, bool> insert_unique_check
  2364. ( const KeyType &key
  2365. , KeyHasher hash_func
  2366. , KeyEqual equal_func
  2367. , insert_commit_data &commit_data)
  2368. {
  2369. const std::size_t h = hash_func(key);
  2370. const std::size_t bn = this->priv_hash_to_nbucket(h);
  2371. commit_data.bucket_idx = bn;
  2372. commit_data.set_hash(h);
  2373. bucket_ptr bp = this->priv_bucket_ptr(bn);
  2374. siterator const s = this->priv_find_in_bucket(*bp, key, equal_func, h);
  2375. return std::pair<iterator, bool>(this->build_iterator(s, bp), s == this->priv_end_sit());
  2376. }
  2377. //! <b>Effects</b>: Checks if a value can be inserted in the unordered_set, using
  2378. //! a user provided key instead of the value itself.
  2379. //!
  2380. //! <b>Returns</b>: If there is an equivalent value
  2381. //! returns a pair containing an iterator to the already present value
  2382. //! and false. If the value can be inserted returns true in the returned
  2383. //! pair boolean and fills "commit_data" that is meant to be used with
  2384. //! the "insert_commit" function.
  2385. //!
  2386. //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
  2387. //!
  2388. //! <b>Throws</b>: If hasher or key_compare throw. Strong guarantee.
  2389. //!
  2390. //! <b>Notes</b>: This function is used to improve performance when constructing
  2391. //! a value_type is expensive: if there is an equivalent value
  2392. //! the constructed object must be discarded. Many times, the part of the
  2393. //! node that is used to impose the hash or the equality is much cheaper to
  2394. //! construct than the value_type and this function offers the possibility to
  2395. //! use that the part to check if the insertion will be successful.
  2396. //!
  2397. //! If the check is successful, the user can construct the value_type and use
  2398. //! "insert_commit" to insert the object in constant-time.
  2399. //!
  2400. //! "commit_data" remains valid for a subsequent "insert_commit" only if no more
  2401. //! objects are inserted or erased from the unordered_set.
  2402. //!
  2403. //! After a successful rehashing insert_commit_data remains valid.
  2404. inline std::pair<iterator, bool> insert_unique_check
  2405. ( const key_type &key, insert_commit_data &commit_data)
  2406. { return this->insert_unique_check(key, this->priv_hasher(), this->priv_equal(), commit_data); }
  2407. //! <b>Requires</b>: value must be an lvalue of type value_type. commit_data
  2408. //! must have been obtained from a previous call to "insert_check".
  2409. //! No objects should have been inserted or erased from the unordered_set between
  2410. //! the "insert_check" that filled "commit_data" and the call to "insert_commit".
  2411. //!
  2412. //! <b>Effects</b>: Inserts the value in the unordered_set using the information obtained
  2413. //! from the "commit_data" that a previous "insert_check" filled.
  2414. //!
  2415. //! <b>Returns</b>: An iterator to the newly inserted object.
  2416. //!
  2417. //! <b>Complexity</b>: Constant time.
  2418. //!
  2419. //! <b>Throws</b>: Nothing.
  2420. //!
  2421. //! <b>Notes</b>: This function has only sense if a "insert_check" has been
  2422. //! previously executed to fill "commit_data". No value should be inserted or
  2423. //! erased between the "insert_check" and "insert_commit" calls.
  2424. //!
  2425. //! After a successful rehashing insert_commit_data remains valid.
  2426. iterator insert_unique_commit(reference value, const insert_commit_data& commit_data) BOOST_NOEXCEPT
  2427. {
  2428. size_type bucket_num = this->priv_hash_to_nbucket(commit_data.get_hash());
  2429. bucket_type& b = this->priv_bucket(bucket_num);
  2430. this->priv_size_traits().increment();
  2431. node_ptr const n = pointer_traits<node_ptr>::pointer_to(this->priv_value_to_node(value));
  2432. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || slist_node_algorithms::unique(n));
  2433. node_functions_t::store_hash(n, commit_data.get_hash(), store_hash_t());
  2434. this->priv_insertion_update_cache(bucket_num);
  2435. group_functions_t::insert_in_group(n, n, optimize_multikey_t());
  2436. slist_node_algorithms::link_after(b.get_node_ptr(), n);
  2437. return this->build_iterator(siterator(n), this->to_ptr(b));
  2438. }
  2439. //! <b>Requires</b>: value must be an lvalue of type value_type. commit_data
  2440. //! must have been obtained from a previous call to "insert_check".
  2441. //! No objects should have been inserted or erased from the unordered_set between
  2442. //! the "insert_check" that filled "commit_data" and the call to "insert_commit".
  2443. //!
  2444. //! No rehashing shall be performed between `insert_check` and `insert_fast_commit`.
  2445. //!
  2446. //! <b>Effects</b>: Inserts the value in the unordered_set using the information obtained
  2447. //! from the "commit_data" that a previous "insert_check" filled.
  2448. //!
  2449. //! <b>Returns</b>: An iterator to the newly inserted object.
  2450. //!
  2451. //! <b>Complexity</b>: Constant time.
  2452. //!
  2453. //! <b>Throws</b>: Nothing.
  2454. //!
  2455. //! <b>Notes</b>: This function has only sense if a "insert_check" has been
  2456. //! previously executed to fill "commit_data". No value should be inserted or
  2457. //! erased between the "insert_check" and "insert_commit" calls.
  2458. //!
  2459. //! Since this commit operation does not support rehashing between the check
  2460. //! and the commit, it's faster than `insert_commit`.
  2461. iterator insert_unique_fast_commit(reference value, const insert_commit_data &commit_data) BOOST_NOEXCEPT
  2462. {
  2463. this->priv_size_inc();
  2464. node_ptr const n = this->priv_value_to_node_ptr(value);
  2465. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || slist_node_algorithms::unique(n));
  2466. node_functions_t::store_hash(n, commit_data.get_hash(), store_hash_t());
  2467. this->priv_insertion_update_cache(static_cast<size_type>(commit_data.bucket_idx));
  2468. group_functions_t::insert_in_group(n, n, optimize_multikey_t());
  2469. bucket_type& b = this->priv_bucket(commit_data.bucket_idx);
  2470. slist_node_algorithms::link_after(b.get_node_ptr(), n);
  2471. return this->build_iterator(siterator(n), this->to_ptr(b));
  2472. }
  2473. //! <b>Effects</b>: Erases the element pointed to by i.
  2474. //!
  2475. //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
  2476. //!
  2477. //! <b>Throws</b>: Nothing.
  2478. //!
  2479. //! <b>Note</b>: Invalidates the iterators (but not the references)
  2480. //! to the erased element. No destructors are called.
  2481. inline void erase(const_iterator i) BOOST_NOEXCEPT
  2482. { this->erase_and_dispose(i, detail::null_disposer()); }
  2483. //! <b>Effects</b>: Erases the range pointed to by b end e.
  2484. //!
  2485. //! <b>Complexity</b>: Average case O(distance(b, e)),
  2486. //! worst case O(this->size()).
  2487. //!
  2488. //! <b>Throws</b>: Nothing.
  2489. //!
  2490. //! <b>Note</b>: Invalidates the iterators (but not the references)
  2491. //! to the erased elements. No destructors are called.
  2492. inline void erase(const_iterator b, const_iterator e) BOOST_NOEXCEPT
  2493. { this->erase_and_dispose(b, e, detail::null_disposer()); }
  2494. //! <b>Effects</b>: Erases all the elements with the given value.
  2495. //!
  2496. //! <b>Returns</b>: The number of erased elements.
  2497. //!
  2498. //! <b>Complexity</b>: Average case O(this->count(value)).
  2499. //! Worst case O(this->size()).
  2500. //!
  2501. //! <b>Throws</b>: If the internal hasher or the equality functor throws.
  2502. //! Basic guarantee.
  2503. //!
  2504. //! <b>Note</b>: Invalidates the iterators (but not the references)
  2505. //! to the erased elements. No destructors are called.
  2506. inline size_type erase(const key_type &key)
  2507. { return this->erase(key, this->priv_hasher(), this->priv_equal()); }
  2508. //! <b>Requires</b>: "hash_func" must be a hash function that induces
  2509. //! the same hash values as the stored hasher. The difference is that
  2510. //! "hash_func" hashes the given key instead of the value_type.
  2511. //!
  2512. //! "equal_func" must be a equality function that induces
  2513. //! the same equality as key_equal. The difference is that
  2514. //! "equal_func" compares an arbitrary key with the contained values.
  2515. //!
  2516. //! <b>Effects</b>: Erases all the elements that have the same hash and
  2517. //! compare equal with the given key.
  2518. //!
  2519. //! <b>Returns</b>: The number of erased elements.
  2520. //!
  2521. //! <b>Complexity</b>: Average case O(this->count(value)).
  2522. //! Worst case O(this->size()).
  2523. //!
  2524. //! <b>Throws</b>: If hash_func or equal_func throw. Basic guarantee.
  2525. //!
  2526. //! <b>Note</b>: Invalidates the iterators (but not the references)
  2527. //! to the erased elements. No destructors are called.
  2528. template<class KeyType, class KeyHasher, class KeyEqual>
  2529. inline size_type erase(const KeyType& key, KeyHasher hash_func, KeyEqual equal_func)
  2530. { return this->erase_and_dispose(key, hash_func, equal_func, detail::null_disposer()); }
  2531. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  2532. //!
  2533. //! <b>Effects</b>: Erases the element pointed to by i.
  2534. //! Disposer::operator()(pointer) is called for the removed element.
  2535. //!
  2536. //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
  2537. //!
  2538. //! <b>Throws</b>: Nothing.
  2539. //!
  2540. //! <b>Note</b>: Invalidates the iterators
  2541. //! to the erased elements.
  2542. template<class Disposer>
  2543. BOOST_INTRUSIVE_DOC1ST(void
  2544. , typename detail::disable_if_convertible<Disposer BOOST_INTRUSIVE_I const_iterator>::type)
  2545. erase_and_dispose(const_iterator i, Disposer disposer) BOOST_NOEXCEPT
  2546. {
  2547. //Get the bucket number and local iterator for both iterators
  2548. const bucket_ptr bp = this->priv_get_bucket_ptr(i);
  2549. this->priv_erase_node(*bp, i.slist_it(), this->make_node_disposer(disposer), optimize_multikey_t());
  2550. this->priv_size_dec();
  2551. this->priv_erasure_update_cache(bp);
  2552. }
  2553. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  2554. //!
  2555. //! <b>Effects</b>: Erases the range pointed to by b end e.
  2556. //! Disposer::operator()(pointer) is called for the removed elements.
  2557. //!
  2558. //! <b>Complexity</b>: Average case O(distance(b, e)),
  2559. //! worst case O(this->size()).
  2560. //!
  2561. //! <b>Throws</b>: Nothing.
  2562. //!
  2563. //! <b>Note</b>: Invalidates the iterators
  2564. //! to the erased elements.
  2565. template<class Disposer>
  2566. void erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer) BOOST_NOEXCEPT
  2567. {
  2568. if(b != e){
  2569. //Get the bucket number and local iterator for both iterators
  2570. size_type first_bucket_num = this->priv_get_bucket_num(b);
  2571. siterator before_first_local_it
  2572. = this->priv_get_previous(this->priv_bucket(first_bucket_num), b.slist_it(), optimize_multikey_t());
  2573. size_type last_bucket_num;
  2574. siterator last_local_it;
  2575. //For the end iterator, we will assign the end iterator
  2576. //of the last bucket
  2577. if(e == this->end()){
  2578. last_bucket_num = size_type(this->bucket_count() - 1u);
  2579. last_local_it = this->sit_end(this->priv_bucket(last_bucket_num));
  2580. }
  2581. else{
  2582. last_local_it = e.slist_it();
  2583. last_bucket_num = this->priv_get_bucket_num(e);
  2584. }
  2585. size_type const num_erased = (size_type)this->priv_erase_node_range
  2586. ( before_first_local_it, first_bucket_num, last_local_it, last_bucket_num
  2587. , this->make_node_disposer(disposer), optimize_multikey_t());
  2588. this->priv_size_count(size_type(this->priv_size_count()-num_erased));
  2589. this->priv_erasure_update_cache_range(first_bucket_num, last_bucket_num);
  2590. }
  2591. }
  2592. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  2593. //!
  2594. //! <b>Effects</b>: Erases all the elements with the given value.
  2595. //! Disposer::operator()(pointer) is called for the removed elements.
  2596. //!
  2597. //! <b>Returns</b>: The number of erased elements.
  2598. //!
  2599. //! <b>Complexity</b>: Average case O(this->count(value)).
  2600. //! Worst case O(this->size()).
  2601. //!
  2602. //! <b>Throws</b>: If the internal hasher or the equality functor throws.
  2603. //! Basic guarantee.
  2604. //!
  2605. //! <b>Note</b>: Invalidates the iterators (but not the references)
  2606. //! to the erased elements. No destructors are called.
  2607. template<class Disposer>
  2608. inline size_type erase_and_dispose(const key_type &key, Disposer disposer)
  2609. { return this->erase_and_dispose(key, this->priv_hasher(), this->priv_equal(), disposer); }
  2610. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  2611. //!
  2612. //! <b>Effects</b>: Erases all the elements with the given key.
  2613. //! according to the comparison functor "equal_func".
  2614. //! Disposer::operator()(pointer) is called for the removed elements.
  2615. //!
  2616. //! <b>Returns</b>: The number of erased elements.
  2617. //!
  2618. //! <b>Complexity</b>: Average case O(this->count(value)).
  2619. //! Worst case O(this->size()).
  2620. //!
  2621. //! <b>Throws</b>: If hash_func or equal_func throw. Basic guarantee.
  2622. //!
  2623. //! <b>Note</b>: Invalidates the iterators
  2624. //! to the erased elements.
  2625. template<class KeyType, class KeyHasher, class KeyEqual, class Disposer>
  2626. size_type erase_and_dispose(const KeyType& key, KeyHasher hash_func
  2627. ,KeyEqual equal_func, Disposer disposer)
  2628. {
  2629. size_type bucket_num;
  2630. std::size_t h;
  2631. siterator prev;
  2632. siterator it = this->priv_find(key, hash_func, equal_func, bucket_num, h, prev);
  2633. bool const success = it != this->priv_end_sit();
  2634. std::size_t cnt(0);
  2635. if(success){
  2636. if(optimize_multikey){
  2637. siterator past_last_in_group = it;
  2638. (priv_go_to_last_in_group)(past_last_in_group, optimize_multikey_t());
  2639. ++past_last_in_group;
  2640. cnt = this->priv_erase_from_single_bucket
  2641. ( this->priv_bucket(bucket_num), prev
  2642. , past_last_in_group
  2643. , this->make_node_disposer(disposer), optimize_multikey_t());
  2644. }
  2645. else{
  2646. siterator const end_sit = this->priv_bucket_lend(bucket_num);
  2647. do{
  2648. ++cnt;
  2649. ++it;
  2650. }while(it != end_sit &&
  2651. this->priv_is_value_equal_to_key
  2652. (this->priv_value_from_siterator(it), h, key, equal_func, compare_hash_t()));
  2653. slist_node_algorithms::unlink_after_and_dispose(prev.pointed_node(), it.pointed_node(), this->make_node_disposer(disposer));
  2654. }
  2655. this->priv_size_count(size_type(this->priv_size_count()-cnt));
  2656. this->priv_erasure_update_cache();
  2657. }
  2658. return static_cast<size_type>(cnt);
  2659. }
  2660. //! <b>Effects</b>: Erases all of the elements.
  2661. //!
  2662. //! <b>Complexity</b>: Linear to the number of elements on the container.
  2663. //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise.
  2664. //!
  2665. //! <b>Throws</b>: Nothing.
  2666. //!
  2667. //! <b>Note</b>: Invalidates the iterators (but not the references)
  2668. //! to the erased elements. No destructors are called.
  2669. void clear() BOOST_NOEXCEPT
  2670. {
  2671. this->priv_clear_buckets_and_cache();
  2672. this->priv_size_count(size_type(0));
  2673. }
  2674. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  2675. //!
  2676. //! <b>Effects</b>: Erases all of the elements.
  2677. //!
  2678. //! <b>Complexity</b>: Linear to the number of elements on the container.
  2679. //! Disposer::operator()(pointer) is called for the removed elements.
  2680. //!
  2681. //! <b>Throws</b>: Nothing.
  2682. //!
  2683. //! <b>Note</b>: Invalidates the iterators (but not the references)
  2684. //! to the erased elements. No destructors are called.
  2685. template<class Disposer>
  2686. void clear_and_dispose(Disposer disposer) BOOST_NOEXCEPT
  2687. {
  2688. if(!constant_time_size || !this->empty()){
  2689. size_type num_buckets = this->bucket_count();
  2690. bucket_ptr b = this->priv_bucket_pointer();
  2691. typename internal_type::template typeof_node_disposer<Disposer>::type d(disposer, &this->priv_value_traits());
  2692. for(; num_buckets; ++b){
  2693. --num_buckets;
  2694. slist_node_algorithms::detach_and_dispose(b->get_node_ptr(), d);
  2695. }
  2696. this->priv_size_count(size_type(0));
  2697. }
  2698. this->priv_init_cache();
  2699. }
  2700. //! <b>Effects</b>: Returns the number of contained elements with the given value
  2701. //!
  2702. //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
  2703. //!
  2704. //! <b>Throws</b>: If the internal hasher or the equality functor throws.
  2705. inline size_type count(const key_type &key) const
  2706. { return this->count(key, this->priv_hasher(), this->priv_equal()); }
  2707. //! <b>Requires</b>: "hash_func" must be a hash function that induces
  2708. //! the same hash values as the stored hasher. The difference is that
  2709. //! "hash_func" hashes the given key instead of the value_type.
  2710. //!
  2711. //! "equal_func" must be a equality function that induces
  2712. //! the same equality as key_equal. The difference is that
  2713. //! "equal_func" compares an arbitrary key with the contained values.
  2714. //!
  2715. //! <b>Effects</b>: Returns the number of contained elements with the given key
  2716. //!
  2717. //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
  2718. //!
  2719. //! <b>Throws</b>: If hash_func or equal throw.
  2720. template<class KeyType, class KeyHasher, class KeyEqual>
  2721. size_type count(const KeyType &key, KeyHasher hash_func, KeyEqual equal_func) const
  2722. {
  2723. size_type cnt;
  2724. size_type n_bucket;
  2725. this->priv_local_equal_range(key, hash_func, equal_func, n_bucket, cnt);
  2726. return cnt;
  2727. }
  2728. //! <b>Effects</b>: Finds an iterator to the first element is equal to
  2729. //! "value" or end() if that element does not exist.
  2730. //!
  2731. //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
  2732. //!
  2733. //! <b>Throws</b>: If the internal hasher or the equality functor throws.
  2734. inline iterator find(const key_type &key)
  2735. { return this->find(key, this->priv_hasher(), this->priv_equal()); }
  2736. //! <b>Requires</b>: "hash_func" must be a hash function that induces
  2737. //! the same hash values as the stored hasher. The difference is that
  2738. //! "hash_func" hashes the given key instead of the value_type.
  2739. //!
  2740. //! "equal_func" must be a equality function that induces
  2741. //! the same equality as key_equal. The difference is that
  2742. //! "equal_func" compares an arbitrary key with the contained values.
  2743. //!
  2744. //! <b>Effects</b>: Finds an iterator to the first element whose key is
  2745. //! "key" according to the given hash and equality functor or end() if
  2746. //! that element does not exist.
  2747. //!
  2748. //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
  2749. //!
  2750. //! <b>Throws</b>: If hash_func or equal_func throw.
  2751. //!
  2752. //! <b>Note</b>: This function is used when constructing a value_type
  2753. //! is expensive and the value_type can be compared with a cheaper
  2754. //! key type. Usually this key is part of the value_type.
  2755. template<class KeyType, class KeyHasher, class KeyEqual>
  2756. iterator find(const KeyType &key, KeyHasher hash_func, KeyEqual equal_func)
  2757. {
  2758. std::size_t h = hash_func(key);
  2759. bucket_ptr bp = this->priv_hash_to_bucket_ptr(h);
  2760. siterator s = this->priv_find_in_bucket(*bp, key, equal_func, h);
  2761. return this->build_iterator(s, bp);
  2762. }
  2763. //! <b>Effects</b>: Finds a const_iterator to the first element whose key is
  2764. //! "key" or end() if that element does not exist.
  2765. //!
  2766. //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
  2767. //!
  2768. //! <b>Throws</b>: If the internal hasher or the equality functor throws.
  2769. inline const_iterator find(const key_type &key) const
  2770. { return this->find(key, this->priv_hasher(), this->priv_equal()); }
  2771. //! <b>Requires</b>: "hash_func" must be a hash function that induces
  2772. //! the same hash values as the stored hasher. The difference is that
  2773. //! "hash_func" hashes the given key instead of the value_type.
  2774. //!
  2775. //! "equal_func" must be a equality function that induces
  2776. //! the same equality as key_equal. The difference is that
  2777. //! "equal_func" compares an arbitrary key with the contained values.
  2778. //!
  2779. //! <b>Effects</b>: Finds an iterator to the first element whose key is
  2780. //! "key" according to the given hasher and equality functor or end() if
  2781. //! that element does not exist.
  2782. //!
  2783. //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
  2784. //!
  2785. //! <b>Throws</b>: If hash_func or equal_func throw.
  2786. //!
  2787. //! <b>Note</b>: This function is used when constructing a value_type
  2788. //! is expensive and the value_type can be compared with a cheaper
  2789. //! key type. Usually this key is part of the value_type.
  2790. template<class KeyType, class KeyHasher, class KeyEqual>
  2791. const_iterator find
  2792. (const KeyType &key, KeyHasher hash_func, KeyEqual equal_func) const
  2793. {
  2794. std::size_t h = hash_func(key);
  2795. bucket_ptr bp = this->priv_hash_to_bucket_ptr(h);
  2796. siterator s = this->priv_find_in_bucket(*bp, key, equal_func, h);
  2797. return this->build_const_iterator(s, bp);
  2798. }
  2799. //! <b>Effects</b>: Returns a range containing all elements with values equivalent
  2800. //! to value. Returns std::make_pair(this->end(), this->end()) if no such
  2801. //! elements exist.
  2802. //!
  2803. //! <b>Complexity</b>: Average case O(this->count(value)). Worst case O(this->size()).
  2804. //!
  2805. //! <b>Throws</b>: If the internal hasher or the equality functor throws.
  2806. inline std::pair<iterator,iterator> equal_range(const key_type &key)
  2807. { return this->equal_range(key, this->priv_hasher(), this->priv_equal()); }
  2808. //! <b>Requires</b>: "hash_func" must be a hash function that induces
  2809. //! the same hash values as the stored hasher. The difference is that
  2810. //! "hash_func" hashes the given key instead of the value_type.
  2811. //!
  2812. //! "equal_func" must be a equality function that induces
  2813. //! the same equality as key_equal. The difference is that
  2814. //! "equal_func" compares an arbitrary key with the contained values.
  2815. //!
  2816. //! <b>Effects</b>: Returns a range containing all elements with equivalent
  2817. //! keys. Returns std::make_pair(this->end(), this->end()) if no such
  2818. //! elements exist.
  2819. //!
  2820. //! <b>Complexity</b>: Average case O(this->count(key, hash_func, equal_func)).
  2821. //! Worst case O(this->size()).
  2822. //!
  2823. //! <b>Throws</b>: If hash_func or the equal_func throw.
  2824. //!
  2825. //! <b>Note</b>: This function is used when constructing a value_type
  2826. //! is expensive and the value_type can be compared with a cheaper
  2827. //! key type. Usually this key is part of the value_type.
  2828. template<class KeyType, class KeyHasher, class KeyEqual>
  2829. std::pair<iterator,iterator> equal_range
  2830. (const KeyType &key, KeyHasher hash_func, KeyEqual equal_func)
  2831. {
  2832. priv_equal_range_result ret =
  2833. this->priv_equal_range(key, hash_func, equal_func);
  2834. return std::pair<iterator, iterator>
  2835. ( this->build_iterator(ret.first, ret.bucket_first)
  2836. , this->build_iterator(ret.second, ret.bucket_second));
  2837. }
  2838. //! <b>Effects</b>: Returns a range containing all elements with values equivalent
  2839. //! to value. Returns std::make_pair(this->end(), this->end()) if no such
  2840. //! elements exist.
  2841. //!
  2842. //! <b>Complexity</b>: Average case O(this->count(value)). Worst case O(this->size()).
  2843. //!
  2844. //! <b>Throws</b>: If the internal hasher or the equality functor throws.
  2845. inline std::pair<const_iterator, const_iterator>
  2846. equal_range(const key_type &key) const
  2847. { return this->equal_range(key, this->priv_hasher(), this->priv_equal()); }
  2848. //! <b>Requires</b>: "hash_func" must be a hash function that induces
  2849. //! the same hash values as the stored hasher. The difference is that
  2850. //! "hash_func" hashes the given key instead of the value_type.
  2851. //!
  2852. //! "equal_func" must be a equality function that induces
  2853. //! the same equality as key_equal. The difference is that
  2854. //! "equal_func" compares an arbitrary key with the contained values.
  2855. //!
  2856. //! <b>Effects</b>: Returns a range containing all elements with equivalent
  2857. //! keys. Returns std::make_pair(this->end(), this->end()) if no such
  2858. //! elements exist.
  2859. //!
  2860. //! <b>Complexity</b>: Average case O(this->count(key, hash_func, equal_func)).
  2861. //! Worst case O(this->size()).
  2862. //!
  2863. //! <b>Throws</b>: If the hasher or equal_func throw.
  2864. //!
  2865. //! <b>Note</b>: This function is used when constructing a value_type
  2866. //! is expensive and the value_type can be compared with a cheaper
  2867. //! key type. Usually this key is part of the value_type.
  2868. template<class KeyType, class KeyHasher, class KeyEqual>
  2869. std::pair<const_iterator,const_iterator> equal_range
  2870. (const KeyType &key, KeyHasher hash_func, KeyEqual equal_func) const
  2871. {
  2872. priv_equal_range_result ret =
  2873. this->priv_equal_range(key, hash_func, equal_func);
  2874. return std::pair<const_iterator, const_iterator>
  2875. ( this->build_const_iterator(ret.first, ret.bucket_first)
  2876. , this->build_const_iterator(ret.second, ret.bucket_second));
  2877. }
  2878. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  2879. //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
  2880. //! appropriate type. Otherwise the behavior is undefined.
  2881. //!
  2882. //! <b>Effects</b>: Returns: a valid iterator belonging to the unordered_set
  2883. //! that points to the value
  2884. //!
  2885. //! <b>Complexity</b>: Constant.
  2886. //!
  2887. //! <b>Throws</b>: If the internal hash function throws.
  2888. iterator iterator_to(reference value) BOOST_NOEXCEPT;
  2889. //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
  2890. //! appropriate type. Otherwise the behavior is undefined.
  2891. //!
  2892. //! <b>Effects</b>: Returns: a valid const_iterator belonging to the
  2893. //! unordered_set that points to the value
  2894. //!
  2895. //! <b>Complexity</b>: Constant.
  2896. //!
  2897. //! <b>Throws</b>: If the internal hash function throws.
  2898. const_iterator iterator_to(const_reference value) const BOOST_NOEXCEPT;
  2899. //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
  2900. //! appropriate type. Otherwise the behavior is undefined.
  2901. //!
  2902. //! <b>Effects</b>: Returns: a valid local_iterator belonging to the unordered_set
  2903. //! that points to the value
  2904. //!
  2905. //! <b>Complexity</b>: Constant.
  2906. //!
  2907. //! <b>Throws</b>: Nothing.
  2908. //!
  2909. //! <b>Note</b>: This static function is available only if the <i>value traits</i>
  2910. //! is stateless.
  2911. static local_iterator s_local_iterator_to(reference value) BOOST_NOEXCEPT;
  2912. //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
  2913. //! appropriate type. Otherwise the behavior is undefined.
  2914. //!
  2915. //! <b>Effects</b>: Returns: a valid const_local_iterator belonging to
  2916. //! the unordered_set that points to the value
  2917. //!
  2918. //! <b>Complexity</b>: Constant.
  2919. //!
  2920. //! <b>Throws</b>: Nothing.
  2921. //!
  2922. //! <b>Note</b>: This static function is available only if the <i>value traits</i>
  2923. //! is stateless.
  2924. static const_local_iterator s_local_iterator_to(const_reference value) BOOST_NOEXCEPT;
  2925. //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
  2926. //! appropriate type. Otherwise the behavior is undefined.
  2927. //!
  2928. //! <b>Effects</b>: Returns: a valid local_iterator belonging to the unordered_set
  2929. //! that points to the value
  2930. //!
  2931. //! <b>Complexity</b>: Constant.
  2932. //!
  2933. //! <b>Throws</b>: Nothing.
  2934. local_iterator local_iterator_to(reference value) BOOST_NOEXCEPT;
  2935. //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
  2936. //! appropriate type. Otherwise the behavior is undefined.
  2937. //!
  2938. //! <b>Effects</b>: Returns: a valid const_local_iterator belonging to
  2939. //! the unordered_set that points to the value
  2940. //!
  2941. //! <b>Complexity</b>: Constant.
  2942. //!
  2943. //! <b>Throws</b>: Nothing.
  2944. const_local_iterator local_iterator_to(const_reference value) const BOOST_NOEXCEPT;
  2945. //! <b>Effects</b>: Returns the number of buckets passed in the constructor
  2946. //! or the last rehash function.
  2947. //!
  2948. //! <b>Complexity</b>: Constant.
  2949. //!
  2950. //! <b>Throws</b>: Nothing.
  2951. size_type bucket_count() const BOOST_NOEXCEPT;
  2952. //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
  2953. //!
  2954. //! <b>Effects</b>: Returns the number of elements in the nth bucket.
  2955. //!
  2956. //! <b>Complexity</b>: Constant.
  2957. //!
  2958. //! <b>Throws</b>: Nothing.
  2959. size_type bucket_size(size_type n) const BOOST_NOEXCEPT;
  2960. #endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  2961. //! <b>Effects</b>: Returns the index of the bucket in which elements
  2962. //! with keys equivalent to k would be found, if any such element existed.
  2963. //!
  2964. //! <b>Complexity</b>: Constant.
  2965. //!
  2966. //! <b>Throws</b>: If the hash functor throws.
  2967. //!
  2968. //! <b>Note</b>: the return value is in the range [0, this->bucket_count()).
  2969. inline size_type bucket(const key_type& k) const
  2970. { return this->priv_hash_to_nbucket(this->priv_hash(k)); }
  2971. //! <b>Requires</b>: "hash_func" must be a hash function that induces
  2972. //! the same hash values as the stored hasher. The difference is that
  2973. //! "hash_func" hashes the given key instead of the value_type.
  2974. //!
  2975. //! <b>Effects</b>: Returns the index of the bucket in which elements
  2976. //! with keys equivalent to k would be found, if any such element existed.
  2977. //!
  2978. //! <b>Complexity</b>: Constant.
  2979. //!
  2980. //! <b>Throws</b>: If hash_func throws.
  2981. //!
  2982. //! <b>Note</b>: the return value is in the range [0, this->bucket_count()).
  2983. template<class KeyType, class KeyHasher>
  2984. inline size_type bucket(const KeyType& k, KeyHasher hash_func) const
  2985. { return this->priv_hash_to_nbucket(hash_func(k)); }
  2986. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  2987. //! <b>Effects</b>: Returns the bucket array pointer passed in the constructor
  2988. //! or the last rehash function.
  2989. //!
  2990. //! <b>Complexity</b>: Constant.
  2991. //!
  2992. //! <b>Throws</b>: Nothing.
  2993. bucket_ptr bucket_pointer() const BOOST_NOEXCEPT;
  2994. //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
  2995. //!
  2996. //! <b>Effects</b>: Returns a local_iterator pointing to the beginning
  2997. //! of the sequence stored in the bucket n.
  2998. //!
  2999. //! <b>Complexity</b>: Constant.
  3000. //!
  3001. //! <b>Throws</b>: Nothing.
  3002. //!
  3003. //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
  3004. //! containing all of the elements in the nth bucket.
  3005. local_iterator begin(size_type n) BOOST_NOEXCEPT;
  3006. //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
  3007. //!
  3008. //! <b>Effects</b>: Returns a const_local_iterator pointing to the beginning
  3009. //! of the sequence stored in the bucket n.
  3010. //!
  3011. //! <b>Complexity</b>: Constant.
  3012. //!
  3013. //! <b>Throws</b>: Nothing.
  3014. //!
  3015. //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
  3016. //! containing all of the elements in the nth bucket.
  3017. const_local_iterator begin(size_type n) const BOOST_NOEXCEPT;
  3018. //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
  3019. //!
  3020. //! <b>Effects</b>: Returns a const_local_iterator pointing to the beginning
  3021. //! of the sequence stored in the bucket n.
  3022. //!
  3023. //! <b>Complexity</b>: Constant.
  3024. //!
  3025. //! <b>Throws</b>: Nothing.
  3026. //!
  3027. //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
  3028. //! containing all of the elements in the nth bucket.
  3029. const_local_iterator cbegin(size_type n) const BOOST_NOEXCEPT;
  3030. //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
  3031. //!
  3032. //! <b>Effects</b>: Returns a local_iterator pointing to the end
  3033. //! of the sequence stored in the bucket n.
  3034. //!
  3035. //! <b>Complexity</b>: Constant.
  3036. //!
  3037. //! <b>Throws</b>: Nothing.
  3038. //!
  3039. //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
  3040. //! containing all of the elements in the nth bucket.
  3041. local_iterator end(size_type n) BOOST_NOEXCEPT;
  3042. //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
  3043. //!
  3044. //! <b>Effects</b>: Returns a const_local_iterator pointing to the end
  3045. //! of the sequence stored in the bucket n.
  3046. //!
  3047. //! <b>Complexity</b>: Constant.
  3048. //!
  3049. //! <b>Throws</b>: Nothing.
  3050. //!
  3051. //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
  3052. //! containing all of the elements in the nth bucket.
  3053. const_local_iterator end(size_type n) const BOOST_NOEXCEPT;
  3054. //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
  3055. //!
  3056. //! <b>Effects</b>: Returns a const_local_iterator pointing to the end
  3057. //! of the sequence stored in the bucket n.
  3058. //!
  3059. //! <b>Complexity</b>: Constant.
  3060. //!
  3061. //! <b>Throws</b>: Nothing.
  3062. //!
  3063. //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
  3064. //! containing all of the elements in the nth bucket.
  3065. const_local_iterator cend(size_type n) const BOOST_NOEXCEPT;
  3066. #endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  3067. //! <b>Requires</b>: new_bucket_traits can hold a pointer to a new bucket array
  3068. //! or the same as the old bucket array with a different length. new_size is the length of the
  3069. //! the array pointed by new_buckets. If new_bucket_traits.bucket_begin() == this->bucket_pointer()
  3070. //! new_bucket_traits.bucket_count() can be bigger or smaller than this->bucket_count().
  3071. //! 'new_bucket_traits' copy constructor should not throw.
  3072. //!
  3073. //! <b>Effects</b>:
  3074. //! If `new_bucket_traits.bucket_begin() == this->bucket_pointer()` is false,
  3075. //! unlinks values from the old bucket and inserts then in the new one according
  3076. //! to the hash value of values.
  3077. //!
  3078. //! If `new_bucket_traits.bucket_begin() == this->bucket_pointer()` is true,
  3079. //! the implementations avoids moving values as much as possible.
  3080. //!
  3081. //! Bucket traits hold by *this is assigned from new_bucket_traits.
  3082. //! If the container is configured as incremental<>, the split bucket is set
  3083. //! to the new bucket_count().
  3084. //!
  3085. //! If store_hash option is true, this method does not use the hash function.
  3086. //! If false, the implementation tries to minimize calls to the hash function
  3087. //! (e.g. once for equivalent values if optimize_multikey<true> is true).
  3088. //!
  3089. //! If rehash is successful updates the internal bucket_traits with new_bucket_traits.
  3090. //!
  3091. //! <b>Complexity</b>: Average case linear in this->size(), worst case quadratic.
  3092. //!
  3093. //! <b>Throws</b>: If the hasher functor throws. Basic guarantee.
  3094. inline void rehash(const bucket_traits &new_bucket_traits)
  3095. { this->priv_rehash_impl(new_bucket_traits, false); }
  3096. //! <b>Note</b>: This function is used when keys from inserted elements are changed
  3097. //! (e.g. a language change when key is a string) but uniqueness and hash properties are
  3098. //! preserved so a fast full rehash recovers invariants for *this without extracting and
  3099. //! reinserting all elements again.
  3100. //!
  3101. //! <b>Requires</b>: Calls produced to the hash function should not alter the value uniqueness
  3102. //! properties of already inserted elements. If hasher(key1) == hasher(key2) was true when
  3103. //! elements were inserted, it shall be true during calls produced in the execution of this function.
  3104. //!
  3105. //! key_equal is not called inside this function so it is assumed that key_equal(value1, value2)
  3106. //! should produce the same results as before for inserted elements.
  3107. //!
  3108. //! <b>Effects</b>: Reprocesses all values hold by *this, recalculating their hash values
  3109. //! and redistributing them though the buckets.
  3110. //!
  3111. //! If store_hash option is true, this method uses the hash function and updates the stored hash value.
  3112. //!
  3113. //! <b>Complexity</b>: Average case linear in this->size(), worst case quadratic.
  3114. //!
  3115. //! <b>Throws</b>: If the hasher functor throws. Basic guarantee.
  3116. inline void full_rehash()
  3117. { this->priv_rehash_impl(this->priv_bucket_traits(), true); }
  3118. //! <b>Requires</b>:
  3119. //!
  3120. //! <b>Effects</b>:
  3121. //!
  3122. //! <b>Complexity</b>:
  3123. //!
  3124. //! <b>Throws</b>:
  3125. //!
  3126. //! <b>Note</b>: this method is only available if incremental<true> option is activated.
  3127. bool incremental_rehash(bool grow = true)
  3128. {
  3129. //This function is only available for containers with incremental hashing
  3130. BOOST_INTRUSIVE_STATIC_ASSERT(( incremental && power_2_buckets ));
  3131. const std::size_t split_idx = this->split_count();
  3132. const std::size_t bucket_cnt = this->bucket_count();
  3133. bool ret = false;
  3134. if(grow){
  3135. //Test if the split variable can be changed
  3136. if((ret = split_idx < bucket_cnt)){
  3137. const std::size_t bucket_to_rehash = split_idx - bucket_cnt/2u;
  3138. bucket_type &old_bucket = this->priv_bucket(bucket_to_rehash);
  3139. this->inc_split_count();
  3140. //Anti-exception stuff: if an exception is thrown while
  3141. //moving elements from old_bucket to the target bucket, all moved
  3142. //elements are moved back to the original one.
  3143. incremental_rehash_rollback<bucket_type, split_traits, slist_node_algorithms> rollback
  3144. ( this->priv_bucket(split_idx), old_bucket, this->priv_split_traits());
  3145. siterator before_i(old_bucket.get_node_ptr());
  3146. siterator i(before_i); ++i;
  3147. siterator end_sit = linear_buckets ? siterator() : before_i;
  3148. for( ; i != end_sit; i = before_i, ++i){
  3149. const value_type &v = this->priv_value_from_siterator(i);
  3150. const std::size_t hash_value = this->priv_stored_or_compute_hash(v, store_hash_t());
  3151. const std::size_t new_n = this->priv_hash_to_nbucket(hash_value);
  3152. siterator last = i;
  3153. (priv_go_to_last_in_group)(last, optimize_multikey_t());
  3154. if(new_n == bucket_to_rehash){
  3155. before_i = last;
  3156. }
  3157. else{
  3158. bucket_type &new_b = this->priv_bucket(new_n);
  3159. slist_node_algorithms::transfer_after(new_b.get_node_ptr(), before_i.pointed_node(), last.pointed_node());
  3160. }
  3161. }
  3162. rollback.release();
  3163. this->priv_erasure_update_cache();
  3164. }
  3165. }
  3166. else if((ret = split_idx > bucket_cnt/2u)){ //!grow
  3167. const std::size_t target_bucket_num = split_idx - 1u - bucket_cnt/2u;
  3168. bucket_type &target_bucket = this->priv_bucket(target_bucket_num);
  3169. bucket_type &source_bucket = this->priv_bucket(split_idx-1u);
  3170. slist_node_algorithms::transfer_after(target_bucket.get_node_ptr(), source_bucket.get_node_ptr());
  3171. this->dec_split_count();
  3172. this->priv_insertion_update_cache(target_bucket_num);
  3173. }
  3174. return ret;
  3175. }
  3176. //! <b>Effects</b>: If new_bucket_traits.bucket_count() is not
  3177. //! this->bucket_count()/2 or this->bucket_count()*2, or
  3178. //! this->split_bucket() != new_bucket_traits.bucket_count() returns false
  3179. //! and does nothing.
  3180. //!
  3181. //! Otherwise, copy assigns new_bucket_traits to the internal bucket_traits
  3182. //! and transfers all the objects from old buckets to the new ones.
  3183. //!
  3184. //! <b>Complexity</b>: Linear to size().
  3185. //!
  3186. //! <b>Throws</b>: Nothing
  3187. //!
  3188. //! <b>Note</b>: this method is only available if incremental<true> option is activated.
  3189. bool incremental_rehash(const bucket_traits &new_bucket_traits) BOOST_NOEXCEPT
  3190. {
  3191. //This function is only available for containers with incremental hashing
  3192. BOOST_INTRUSIVE_STATIC_ASSERT(( incremental && power_2_buckets ));
  3193. const bucket_ptr new_buckets = new_bucket_traits.bucket_begin();
  3194. const size_type new_bucket_count_stdszt = static_cast<SizeType>(new_bucket_traits.bucket_count() - bucket_overhead);
  3195. BOOST_INTRUSIVE_INVARIANT_ASSERT(sizeof(size_type) >= sizeof(std::size_t) || new_bucket_count_stdszt <= size_type(-1));
  3196. size_type new_bucket_count = static_cast<size_type>(new_bucket_count_stdszt);
  3197. const size_type old_bucket_count = static_cast<size_type>(this->priv_usable_bucket_count());
  3198. const size_type split_idx = this->split_count();
  3199. //Test new bucket size is consistent with internal bucket size and split count
  3200. if(new_bucket_count/2 == old_bucket_count){
  3201. if(!(split_idx >= old_bucket_count))
  3202. return false;
  3203. }
  3204. else if(new_bucket_count == old_bucket_count/2){
  3205. if(!(split_idx <= new_bucket_count))
  3206. return false;
  3207. }
  3208. else{
  3209. return false;
  3210. }
  3211. const size_type ini_n = (size_type)this->priv_get_cache_bucket_num();
  3212. const bucket_ptr old_buckets = this->priv_bucket_pointer();
  3213. this->priv_unset_sentinel_bucket();
  3214. this->priv_initialize_new_buckets(old_buckets, old_bucket_count, new_buckets, new_bucket_count);
  3215. if (&new_bucket_traits != &this->priv_bucket_traits())
  3216. this->priv_bucket_traits() = new_bucket_traits;
  3217. if(old_buckets != new_buckets){
  3218. for(size_type n = ini_n; n < split_idx; ++n){
  3219. slist_node_ptr new_bucket_nodeptr = new_bucket_traits.bucket_begin()[difference_type(n)].get_node_ptr();
  3220. slist_node_ptr old_bucket_node_ptr = old_buckets[difference_type(n)].get_node_ptr();
  3221. slist_node_algorithms::transfer_after(new_bucket_nodeptr, old_bucket_node_ptr);
  3222. }
  3223. //Reset cache to safe position
  3224. this->priv_set_cache_bucket_num(ini_n);
  3225. }
  3226. this->priv_set_sentinel_bucket();
  3227. return true;
  3228. }
  3229. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  3230. //! <b>Requires</b>: incremental<> option must be set
  3231. //!
  3232. //! <b>Effects</b>: returns the current split count
  3233. //!
  3234. //! <b>Complexity</b>: Constant
  3235. //!
  3236. //! <b>Throws</b>: Nothing
  3237. size_type split_count() const BOOST_NOEXCEPT;
  3238. //! <b>Effects</b>: Returns the nearest new bucket count optimized for
  3239. //! the container that is bigger or equal than n. This suggestion can be
  3240. //! used to create bucket arrays with a size that will usually improve
  3241. //! container's performance. If such value does not exist, the
  3242. //! higher possible value is returned.
  3243. //!
  3244. //! <b>Complexity</b>: Amortized constant time.
  3245. //!
  3246. //! <b>Throws</b>: Nothing.
  3247. static size_type suggested_upper_bucket_count(size_type n) BOOST_NOEXCEPT;
  3248. //! <b>Effects</b>: Returns the nearest new bucket count optimized for
  3249. //! the container that is smaller or equal than n. This suggestion can be
  3250. //! used to create bucket arrays with a size that will usually improve
  3251. //! container's performance. If such value does not exist, the
  3252. //! lowest possible value is returned.
  3253. //!
  3254. //! <b>Complexity</b>: Amortized constant time.
  3255. //!
  3256. //! <b>Throws</b>: Nothing.
  3257. static size_type suggested_lower_bucket_count(size_type n) BOOST_NOEXCEPT;
  3258. #endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  3259. friend bool operator==(const hashtable_impl &x, const hashtable_impl &y)
  3260. {
  3261. //Taken from N3068
  3262. if(constant_time_size && x.size() != y.size()){
  3263. return false;
  3264. }
  3265. if (boost::intrusive::iterator_udistance(x.begin(), x.end()) != x.size())
  3266. return false;
  3267. for (const_iterator ix = x.cbegin(), ex = x.cend(); ix != ex; ++ix){
  3268. std::pair<const_iterator, const_iterator> eqx(x.equal_range(key_of_value()(*ix))),
  3269. eqy(y.equal_range(key_of_value()(*ix)));
  3270. if (boost::intrusive::iterator_distance(eqx.first, eqx.second) !=
  3271. boost::intrusive::iterator_distance(eqy.first, eqy.second) ||
  3272. !(priv_algo_is_permutation)(eqx.first, eqx.second, eqy.first) ){
  3273. return false;
  3274. }
  3275. ix = eqx.second;
  3276. }
  3277. return true;
  3278. }
  3279. friend bool operator!=(const hashtable_impl &x, const hashtable_impl &y)
  3280. { return !(x == y); }
  3281. friend bool operator<(const hashtable_impl &x, const hashtable_impl &y)
  3282. { return ::boost::intrusive::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); }
  3283. friend bool operator>(const hashtable_impl &x, const hashtable_impl &y)
  3284. { return y < x; }
  3285. friend bool operator<=(const hashtable_impl &x, const hashtable_impl &y)
  3286. { return !(y < x); }
  3287. friend bool operator>=(const hashtable_impl &x, const hashtable_impl &y)
  3288. { return !(x < y); }
  3289. /// @cond
  3290. inline void check() const {}
  3291. private:
  3292. static void priv_initialize_new_buckets
  3293. ( bucket_ptr old_buckets, size_type old_bucket_count
  3294. , bucket_ptr new_buckets, size_type new_bucket_count)
  3295. {
  3296. //Initialize new buckets
  3297. const bool same_buffer = old_buckets == new_buckets;
  3298. if (same_buffer && new_bucket_count <= old_bucket_count) {
  3299. //Nothing to do here
  3300. }
  3301. else {
  3302. bucket_ptr p;
  3303. size_type c;
  3304. if (same_buffer) {
  3305. p = old_buckets + std::ptrdiff_t(old_bucket_count);
  3306. c = size_type(new_bucket_count - old_bucket_count);
  3307. }
  3308. else {
  3309. p = new_buckets;
  3310. c = new_bucket_count;
  3311. }
  3312. internal_type::priv_init_buckets(p, c);
  3313. }
  3314. }
  3315. void priv_rehash_impl(const bucket_traits &new_bucket_traits, bool do_full_rehash)
  3316. {
  3317. const std::size_t nbc = new_bucket_traits.bucket_count() - bucket_overhead;
  3318. BOOST_INTRUSIVE_INVARIANT_ASSERT(sizeof(SizeType) >= sizeof(std::size_t) || nbc <= SizeType(-1));
  3319. const bucket_ptr new_buckets = new_bucket_traits.bucket_begin();
  3320. const size_type new_bucket_count = static_cast<SizeType>(nbc);
  3321. const bucket_ptr old_buckets = this->priv_bucket_pointer();
  3322. const size_type old_bucket_count = this->bucket_count();
  3323. //Check power of two bucket array if the option is activated
  3324. BOOST_INTRUSIVE_INVARIANT_ASSERT
  3325. (!power_2_buckets || (0 == (new_bucket_count & (new_bucket_count-1u))));
  3326. const bool same_buffer = old_buckets == new_buckets;
  3327. //If the new bucket length is a common factor
  3328. //of the old one we can avoid hash calculations.
  3329. const bool fast_shrink = (!do_full_rehash) && (!incremental) && (old_bucket_count >= new_bucket_count) &&
  3330. (power_2_buckets || (old_bucket_count % new_bucket_count) == 0);
  3331. //If we are shrinking the same bucket array and it's
  3332. //is a fast shrink, just rehash the last nodes
  3333. size_type new_first_bucket_num = new_bucket_count;
  3334. size_type old_bucket_cache = (size_type)this->priv_get_cache_bucket_num();
  3335. if(same_buffer && fast_shrink && (old_bucket_cache < new_bucket_count)){
  3336. new_first_bucket_num = old_bucket_cache;
  3337. old_bucket_cache = new_bucket_count;
  3338. }
  3339. if (!do_full_rehash)
  3340. this->priv_initialize_new_buckets(old_buckets, old_bucket_count, new_buckets, new_bucket_count);
  3341. //Anti-exception stuff: they destroy the elements if something goes wrong.
  3342. //If the source and destination buckets are the same, the second rollback function
  3343. //is harmless, because all elements have been already unlinked and destroyed
  3344. typedef typename internal_type::template typeof_node_disposer<detail::null_disposer>::type NodeDisposer;
  3345. typedef exception_bucket_disposer<bucket_type, slist_node_algorithms, NodeDisposer, size_type> ArrayDisposer;
  3346. NodeDisposer nd(this->make_node_disposer(detail::null_disposer()));
  3347. ArrayDisposer rollback1(new_buckets[0], nd, new_bucket_count);
  3348. ArrayDisposer rollback2(old_buckets[0], nd, old_bucket_count);
  3349. //Put size in a safe value for rollback exception
  3350. size_type const size_backup = this->priv_size_count();
  3351. this->priv_size_count(0);
  3352. //Put cache to safe position
  3353. this->priv_init_cache();
  3354. this->priv_unset_sentinel_bucket();
  3355. const size_type split = this->rehash_split_from_bucket_count(new_bucket_count);
  3356. //Iterate through nodes
  3357. for(size_type n = old_bucket_cache; n < old_bucket_count; ++n){
  3358. bucket_type &old_bucket = old_buckets[difference_type(n)];
  3359. if(!fast_shrink){
  3360. siterator before_i(old_bucket.get_node_ptr());
  3361. siterator i(before_i); ++i;
  3362. siterator end_sit(this->sit_end(old_bucket));
  3363. for( //
  3364. ; i != end_sit
  3365. ; i = before_i, ++i){
  3366. //First obtain hash value (and store it if do_full_rehash)
  3367. std::size_t hash_value;
  3368. if(do_full_rehash){
  3369. value_type &v = this->priv_value_from_siterator(i);
  3370. hash_value = this->priv_hasher()(key_of_value()(v));
  3371. node_functions_t::store_hash(this->priv_value_to_node_ptr(v), hash_value, store_hash_t());
  3372. }
  3373. else{
  3374. const value_type &v = this->priv_value_from_siterator(i);
  3375. hash_value = this->priv_stored_or_compute_hash(v, store_hash_t());
  3376. }
  3377. //Now calculate the new bucket position
  3378. const size_type new_n = (size_type)hash_to_bucket_split<power_2_buckets, incremental>
  3379. (hash_value, new_bucket_count, split, fastmod_buckets_t());
  3380. //Update first used bucket cache
  3381. if(cache_begin && new_n < new_first_bucket_num)
  3382. new_first_bucket_num = new_n;
  3383. //If the target bucket is new, transfer the whole group
  3384. siterator last = i;
  3385. (priv_go_to_last_in_group)(i, optimize_multikey_t());
  3386. if(same_buffer && new_n == n){
  3387. before_i = last;
  3388. }
  3389. else{
  3390. bucket_type &new_b = new_buckets[difference_type(new_n)];
  3391. slist_node_algorithms::transfer_after(new_b.get_node_ptr(), before_i.pointed_node(), last.pointed_node());
  3392. }
  3393. }
  3394. }
  3395. else{
  3396. const size_type new_n = (size_type)hash_to_bucket_split<power_2_buckets, incremental>
  3397. (n, new_bucket_count, split, fastmod_buckets_t());
  3398. if(cache_begin && new_n < new_first_bucket_num)
  3399. new_first_bucket_num = new_n;
  3400. bucket_type &new_b = new_buckets[difference_type(new_n)];
  3401. siterator last = this->priv_get_last(old_bucket, optimize_multikey_t());
  3402. slist_node_algorithms::transfer_after(new_b.get_node_ptr(), old_bucket.get_node_ptr(), last.pointed_node());
  3403. }
  3404. }
  3405. this->priv_size_count(size_backup);
  3406. this->split_count(split);
  3407. if(&new_bucket_traits != &this->priv_bucket_traits())
  3408. this->priv_bucket_traits() = new_bucket_traits;
  3409. this->priv_set_sentinel_bucket();
  3410. this->priv_set_cache_bucket_num(new_first_bucket_num);
  3411. rollback1.release();
  3412. rollback2.release();
  3413. }
  3414. template <class MaybeConstHashtableImpl, class Cloner, class Disposer>
  3415. void priv_clone_from(MaybeConstHashtableImpl &src, Cloner cloner, Disposer disposer)
  3416. {
  3417. this->clear_and_dispose(disposer);
  3418. if(!constant_time_size || !src.empty()){
  3419. const size_type src_bucket_count = src.bucket_count();
  3420. const size_type dst_bucket_count = this->bucket_count();
  3421. //Check power of two bucket array if the option is activated
  3422. BOOST_INTRUSIVE_INVARIANT_ASSERT
  3423. (!power_2_buckets || (0 == (src_bucket_count & (src_bucket_count-1))));
  3424. BOOST_INTRUSIVE_INVARIANT_ASSERT
  3425. (!power_2_buckets || (0 == (dst_bucket_count & (dst_bucket_count-1))));
  3426. //If src bucket count is bigger or equal, structural copy is possible
  3427. const bool structural_copy = (!incremental) && (src_bucket_count >= dst_bucket_count) &&
  3428. (power_2_buckets || (src_bucket_count % dst_bucket_count) == 0);
  3429. if(structural_copy){
  3430. this->priv_structural_clone_from(src, cloner, disposer);
  3431. }
  3432. else{
  3433. //Unlike previous cloning algorithm, this can throw
  3434. //if cloner, hasher or comparison functor throw
  3435. typedef typename detail::if_c< detail::is_const<MaybeConstHashtableImpl>::value
  3436. , typename MaybeConstHashtableImpl::const_iterator
  3437. , typename MaybeConstHashtableImpl::iterator
  3438. >::type clone_iterator;
  3439. clone_iterator b(src.begin()), e(src.end());
  3440. detail::exception_disposer<hashtable_impl, Disposer> rollback(*this, disposer);
  3441. for(; b != e; ++b){
  3442. //No need to check for duplicates and insert it in the first position
  3443. //as this is an unordered container. So use minimal insertion code
  3444. std::size_t const hash_to_store = this->priv_stored_or_compute_hash(*b, store_hash_t());
  3445. size_type const bucket_number = this->priv_hash_to_nbucket(hash_to_store);
  3446. typedef typename detail::if_c
  3447. <detail::is_const<MaybeConstHashtableImpl>::value, const_reference, reference>::type reference_type;
  3448. reference_type r = *b;
  3449. this->priv_clone_front_in_bucket<reference_type>(bucket_number, r, hash_to_store, cloner);
  3450. }
  3451. rollback.release();
  3452. }
  3453. }
  3454. }
  3455. template<class ValueReference, class Cloner>
  3456. void priv_clone_front_in_bucket( size_type const bucket_number
  3457. , typename detail::identity<ValueReference>::type src_ref
  3458. , std::size_t const hash_to_store, Cloner cloner)
  3459. {
  3460. //No need to check for duplicates and insert it in the first position
  3461. //as this is an unordered container. So use minimal insertion code
  3462. bucket_type &cur_bucket = this->priv_bucket(bucket_number);
  3463. siterator const prev(cur_bucket.get_node_ptr());
  3464. //Just check if the cloned node is equal to the first inserted value in the new bucket
  3465. //as equal src values were contiguous and they should be already inserted in the
  3466. //destination bucket.
  3467. bool const next_is_in_group = optimize_multikey && !this->priv_bucket_empty(bucket_number) &&
  3468. this->priv_equal()( key_of_value()(src_ref)
  3469. , key_of_value()(this->priv_value_from_siterator(++siterator(prev))));
  3470. this->priv_insert_equal_after_find(*cloner(src_ref), bucket_number, hash_to_store, prev, next_is_in_group);
  3471. }
  3472. template <class MaybeConstHashtableImpl, class Cloner, class Disposer>
  3473. void priv_structural_clone_from(MaybeConstHashtableImpl &src, Cloner cloner, Disposer disposer)
  3474. {
  3475. //First clone the first ones
  3476. const size_type src_bucket_count = src.bucket_count();
  3477. const size_type dst_bucket_count = this->bucket_count();
  3478. size_type constructed = 0;
  3479. typedef typename internal_type::template typeof_node_disposer<Disposer>::type NodeDisposer;
  3480. NodeDisposer node_disp(disposer, &this->priv_value_traits());
  3481. exception_bucket_disposer<bucket_type, slist_node_algorithms, NodeDisposer, size_type>
  3482. rollback(this->priv_bucket(0), node_disp, constructed);
  3483. //Now insert the remaining ones using the modulo trick
  3484. for( //"constructed" already initialized
  3485. ; constructed < src_bucket_count
  3486. ; ++constructed){
  3487. const size_type new_n = (size_type)hash_to_bucket_split<power_2_buckets, incremental>
  3488. (constructed, dst_bucket_count, this->split_count(), fastmod_buckets_t());
  3489. bucket_type &src_b = src.priv_bucket(constructed);
  3490. for( siterator b(this->priv_bucket_lbegin(src_b)), e(this->priv_bucket_lend(src_b)); b != e; ++b){
  3491. typedef typename detail::if_c
  3492. <detail::is_const<MaybeConstHashtableImpl>::value, const_reference, reference>::type reference_type;
  3493. reference_type r = this->priv_value_from_siterator(b);
  3494. this->priv_clone_front_in_bucket<reference_type>
  3495. (new_n, r, this->priv_stored_hash(b, store_hash_t()), cloner);
  3496. }
  3497. }
  3498. this->priv_hasher() = src.priv_hasher();
  3499. this->priv_equal() = src.priv_equal();
  3500. rollback.release();
  3501. this->priv_size_count(src.priv_size_count());
  3502. this->split_count(dst_bucket_count);
  3503. this->priv_set_cache_bucket_num(0u);
  3504. this->priv_erasure_update_cache();
  3505. }
  3506. iterator priv_insert_equal_after_find(reference value, size_type bucket_num, std::size_t hash_value, siterator prev, bool const next_is_in_group)
  3507. {
  3508. //Now store hash if needed
  3509. node_ptr n = this->priv_value_to_node_ptr(value);
  3510. node_functions_t::store_hash(n, hash_value, store_hash_t());
  3511. //Checks for some modes
  3512. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || slist_node_algorithms::unique(n));
  3513. //Shortcut to optimize_multikey cases
  3514. group_functions_t::insert_in_group
  3515. ( next_is_in_group ? dcast_bucket_ptr<node>((++siterator(prev)).pointed_node()) : n
  3516. , n, optimize_multikey_t());
  3517. //Update cache and increment size if needed
  3518. this->priv_insertion_update_cache(bucket_num);
  3519. this->priv_size_inc();
  3520. slist_node_algorithms::link_after(prev.pointed_node(), n);
  3521. return this->build_iterator(siterator(n), this->priv_bucket_ptr(bucket_num));
  3522. }
  3523. template<class KeyType, class KeyHasher, class KeyEqual>
  3524. siterator priv_find //In case it is not found previt is priv_end_sit()
  3525. ( const KeyType &key, KeyHasher hash_func
  3526. , KeyEqual equal_func, size_type &bucket_number, std::size_t &h, siterator &previt) const
  3527. {
  3528. h = hash_func(key);
  3529. bucket_number = this->priv_hash_to_nbucket(h);
  3530. bucket_type& b = this->priv_bucket(bucket_number);
  3531. siterator prev = this->sit_bbegin(b);
  3532. siterator it = prev;
  3533. siterator const endit = this->sit_end(b);
  3534. while (++it != endit) {
  3535. if (this->priv_is_value_equal_to_key
  3536. (this->priv_value_from_siterator(it), h, key, equal_func, compare_hash_t())) {
  3537. previt = prev;
  3538. return it;
  3539. }
  3540. (priv_go_to_last_in_group)(it, optimize_multikey_t());
  3541. prev = it;
  3542. }
  3543. previt = b.get_node_ptr();
  3544. return this->priv_end_sit();
  3545. }
  3546. template<class KeyType, class KeyEqual>
  3547. siterator priv_find_in_bucket //In case it is not found previt is priv_end_sit()
  3548. (bucket_type &b, const KeyType& key, KeyEqual equal_func, const std::size_t h) const
  3549. {
  3550. siterator it(this->sit_begin(b));
  3551. siterator const endit(this->sit_end(b));
  3552. for (; it != endit; (priv_go_to_last_in_group)(it, optimize_multikey_t()), ++it) {
  3553. if (BOOST_LIKELY(this->priv_is_value_equal_to_key
  3554. (this->priv_value_from_siterator(it), h, key, equal_func, compare_hash_t()))) {
  3555. return it;
  3556. }
  3557. }
  3558. return this->priv_end_sit();
  3559. }
  3560. template<class KeyType, class KeyEqual>
  3561. inline bool priv_is_value_equal_to_key
  3562. (const value_type &v, const std::size_t h, const KeyType &key, KeyEqual equal_func, detail::true_) const //compare_hash
  3563. { return this->priv_stored_or_compute_hash(v, store_hash_t()) == h && equal_func(key, key_of_value()(v)); }
  3564. template<class KeyType, class KeyEqual>
  3565. inline bool priv_is_value_equal_to_key
  3566. (const value_type& v, const std::size_t , const KeyType& key, KeyEqual equal_func, detail::false_) const //compare_hash
  3567. { return equal_func(key, key_of_value()(v)); }
  3568. //return previous iterator to the next equal range group in case
  3569. inline static void priv_go_to_last_in_group
  3570. (siterator &it_first_in_group, detail::true_) BOOST_NOEXCEPT //optimize_multikey
  3571. {
  3572. it_first_in_group =
  3573. (group_functions_t::get_last_in_group
  3574. (dcast_bucket_ptr<node>(it_first_in_group.pointed_node()), optimize_multikey_t()));
  3575. }
  3576. //return previous iterator to the next equal range group in case
  3577. inline static void priv_go_to_last_in_group //!optimize_multikey
  3578. (siterator /*&it_first_in_group*/, detail::false_) BOOST_NOEXCEPT
  3579. { }
  3580. template<class KeyType, class KeyHasher, class KeyEqual>
  3581. std::pair<siterator, siterator> priv_local_equal_range
  3582. ( const KeyType &key
  3583. , KeyHasher hash_func
  3584. , KeyEqual equal_func
  3585. , size_type &found_bucket
  3586. , size_type &cnt) const
  3587. {
  3588. std::size_t internal_cnt = 0;
  3589. //Let's see if the element is present
  3590. siterator prev;
  3591. size_type n_bucket;
  3592. std::size_t h;
  3593. std::pair<siterator, siterator> to_return
  3594. ( this->priv_find(key, hash_func, equal_func, n_bucket, h, prev)
  3595. , this->priv_end_sit());
  3596. if(to_return.first != to_return.second){
  3597. found_bucket = n_bucket;
  3598. //If it's present, find the first that it's not equal in
  3599. //the same bucket
  3600. siterator it = to_return.first;
  3601. siterator const bend = this->priv_bucket_lend(n_bucket);
  3602. BOOST_IF_CONSTEXPR(optimize_multikey){
  3603. siterator past_last_in_group_it = it;
  3604. (priv_go_to_last_in_group)(past_last_in_group_it, optimize_multikey_t());
  3605. ++past_last_in_group_it;
  3606. internal_cnt += boost::intrusive::iterator_udistance(++it, past_last_in_group_it) + 1u;
  3607. if (past_last_in_group_it != bend)
  3608. to_return.second = past_last_in_group_it;
  3609. }
  3610. else{
  3611. do {
  3612. ++internal_cnt; //At least one is found
  3613. ++it;
  3614. } while(it != bend &&
  3615. this->priv_is_value_equal_to_key
  3616. (this->priv_value_from_siterator(it), h, key, equal_func, compare_hash_t()));
  3617. if (it != bend)
  3618. to_return.second = it;
  3619. }
  3620. }
  3621. cnt = size_type(internal_cnt);
  3622. return to_return;
  3623. }
  3624. struct priv_equal_range_result
  3625. {
  3626. siterator first;
  3627. siterator second;
  3628. bucket_ptr bucket_first;
  3629. bucket_ptr bucket_second;
  3630. };
  3631. template<class KeyType, class KeyHasher, class KeyEqual>
  3632. priv_equal_range_result priv_equal_range
  3633. ( const KeyType &key
  3634. , KeyHasher hash_func
  3635. , KeyEqual equal_func) const
  3636. {
  3637. size_type n_bucket;
  3638. size_type cnt;
  3639. //Let's see if the element is present
  3640. const std::pair<siterator, siterator> to_return
  3641. (this->priv_local_equal_range(key, hash_func, equal_func, n_bucket, cnt));
  3642. priv_equal_range_result r;
  3643. r.first = to_return.first;
  3644. r.second = to_return.second;
  3645. //If not, find the next element as ".second" if ".second" local iterator
  3646. //is not pointing to an element.
  3647. if(to_return.first == to_return.second) {
  3648. r.bucket_first = r.bucket_second = this->priv_invalid_bucket_ptr();
  3649. }
  3650. else if (to_return.second != this->priv_end_sit()) {
  3651. r.bucket_first = this->priv_bucket_ptr(n_bucket);
  3652. }
  3653. else{
  3654. r.bucket_first = this->priv_bucket_ptr(n_bucket);
  3655. const size_type max_bucket = this->bucket_count();
  3656. do{
  3657. ++n_bucket;
  3658. } while (n_bucket != max_bucket && this->priv_bucket_empty(n_bucket));
  3659. if (n_bucket == max_bucket){
  3660. r.bucket_second = this->priv_invalid_bucket_ptr();
  3661. }
  3662. else{
  3663. r.bucket_second = this->priv_bucket_ptr(n_bucket);
  3664. r.second = siterator(r.bucket_second->begin_ptr());
  3665. }
  3666. }
  3667. return r;
  3668. }
  3669. inline size_type priv_get_bucket_num(const_iterator it) BOOST_NOEXCEPT
  3670. { return this->priv_get_bucket_num(it, linear_buckets_t()); }
  3671. inline size_type priv_get_bucket_num(const_iterator it, detail::true_) BOOST_NOEXCEPT //linear
  3672. { return size_type(it.get_bucket_ptr() - this->priv_bucket_pointer()); }
  3673. inline size_type priv_get_bucket_num(const_iterator it, detail::false_) BOOST_NOEXCEPT //!linear
  3674. { return this->priv_get_bucket_num_hash_dispatch(it.slist_it(), store_hash_t()); }
  3675. inline size_type priv_get_bucket_num_hash_dispatch(siterator it, detail::true_) BOOST_NOEXCEPT //store_hash
  3676. { return (size_type)this->priv_hash_to_nbucket(this->priv_stored_hash(it, store_hash_t())); }
  3677. size_type priv_get_bucket_num_hash_dispatch(siterator it, detail::false_) BOOST_NOEXCEPT //NO store_hash
  3678. {
  3679. const bucket_type &f = this->priv_bucket(0u);
  3680. slist_node_ptr bb = group_functions_t::get_bucket_before_begin
  3681. ( this->priv_bucket_lbbegin(0u).pointed_node()
  3682. , this->priv_bucket_lbbegin(this->priv_usable_bucket_count() - 1u).pointed_node()
  3683. , it.pointed_node()
  3684. , optimize_multikey_t());
  3685. //Now get the bucket_impl from the iterator
  3686. const bucket_type &b = static_cast<const bucket_type&>(*bb);
  3687. //Now just calculate the index b has in the bucket array
  3688. return static_cast<size_type>(&b - &f);
  3689. }
  3690. inline bucket_ptr priv_get_bucket_ptr(const_iterator it) BOOST_NOEXCEPT
  3691. { return this->priv_get_bucket_ptr(it, linear_buckets_t()); }
  3692. inline bucket_ptr priv_get_bucket_ptr(const_iterator it, detail::true_) BOOST_NOEXCEPT //linear
  3693. { return it.get_bucket_ptr(); }
  3694. inline bucket_ptr priv_get_bucket_ptr(const_iterator it, detail::false_) BOOST_NOEXCEPT //!linear
  3695. { return this->priv_bucket_ptr(this->priv_get_bucket_num_hash_dispatch(it.slist_it(), store_hash_t())); }
  3696. /// @endcond
  3697. };
  3698. /// @cond
  3699. template < class T
  3700. , class PackedOptions
  3701. >
  3702. struct make_bucket_traits
  3703. {
  3704. //Real value traits must be calculated from options
  3705. typedef typename detail::get_value_traits
  3706. <T, typename PackedOptions::proto_value_traits>::type value_traits;
  3707. typedef typename PackedOptions::bucket_traits specified_bucket_traits;
  3708. //Real bucket traits must be calculated from options and calculated value_traits
  3709. typedef bucket_traits_impl
  3710. < typename unordered_bucket_ptr_impl
  3711. <value_traits>::type
  3712. , std::size_t> bucket_traits_t;
  3713. typedef typename
  3714. detail::if_c< detail::is_same
  3715. < specified_bucket_traits
  3716. , default_bucket_traits
  3717. >::value
  3718. , bucket_traits_t
  3719. , specified_bucket_traits
  3720. >::type type;
  3721. };
  3722. /// @endcond
  3723. //! Helper metafunction to define a \c hashtable that yields to the same type when the
  3724. //! same options (either explicitly or implicitly) are used.
  3725. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  3726. template<class T, class ...Options>
  3727. #else
  3728. template<class T, class O1 = void, class O2 = void
  3729. , class O3 = void, class O4 = void
  3730. , class O5 = void, class O6 = void
  3731. , class O7 = void, class O8 = void
  3732. , class O9 = void, class O10= void
  3733. , class O11= void
  3734. >
  3735. #endif
  3736. struct make_hashtable
  3737. {
  3738. /// @cond
  3739. typedef typename pack_options
  3740. < hashtable_defaults,
  3741. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  3742. O1, O2, O3, O4, O5, O6, O7, O8, O9, O10, O11
  3743. #else
  3744. Options...
  3745. #endif
  3746. >::type packed_options;
  3747. typedef typename detail::get_value_traits
  3748. <T, typename packed_options::proto_value_traits>::type value_traits;
  3749. typedef typename make_bucket_traits
  3750. <T, packed_options>::type bucket_traits;
  3751. typedef hashtable_impl
  3752. < value_traits
  3753. , typename packed_options::key_of_value
  3754. , typename packed_options::hash
  3755. , typename packed_options::equal
  3756. , bucket_traits
  3757. , typename packed_options::size_type
  3758. , (std::size_t(false)*hash_bool_flags::unique_keys_pos)
  3759. |(std::size_t(packed_options::constant_time_size)*hash_bool_flags::constant_time_size_pos)
  3760. |(std::size_t(packed_options::power_2_buckets)*hash_bool_flags::power_2_buckets_pos)
  3761. |(std::size_t(packed_options::cache_begin)*hash_bool_flags::cache_begin_pos)
  3762. |(std::size_t(packed_options::compare_hash)*hash_bool_flags::compare_hash_pos)
  3763. |(std::size_t(packed_options::incremental)*hash_bool_flags::incremental_pos)
  3764. |(std::size_t(packed_options::linear_buckets)*hash_bool_flags::linear_buckets_pos)
  3765. |(std::size_t(packed_options::fastmod_buckets)*hash_bool_flags::fastmod_buckets_pos)
  3766. > implementation_defined;
  3767. /// @endcond
  3768. typedef implementation_defined type;
  3769. };
  3770. #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  3771. #if defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  3772. template<class T, class ...Options>
  3773. #else
  3774. template<class T, class O1, class O2, class O3, class O4, class O5, class O6, class O7, class O8, class O9, class O10>
  3775. #endif
  3776. class hashtable
  3777. : public make_hashtable<T,
  3778. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  3779. O1, O2, O3, O4, O5, O6, O7, O8, O9, O10
  3780. #else
  3781. Options...
  3782. #endif
  3783. >::type
  3784. {
  3785. typedef typename make_hashtable<T,
  3786. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  3787. O1, O2, O3, O4, O5, O6, O7, O8, O9, O10
  3788. #else
  3789. Options...
  3790. #endif
  3791. >::type Base;
  3792. BOOST_MOVABLE_BUT_NOT_COPYABLE(hashtable)
  3793. public:
  3794. typedef typename Base::value_traits value_traits;
  3795. typedef typename Base::iterator iterator;
  3796. typedef typename Base::const_iterator const_iterator;
  3797. typedef typename Base::bucket_ptr bucket_ptr;
  3798. typedef typename Base::size_type size_type;
  3799. typedef typename Base::hasher hasher;
  3800. typedef typename Base::bucket_traits bucket_traits;
  3801. typedef typename Base::key_equal key_equal;
  3802. //Assert if passed value traits are compatible with the type
  3803. BOOST_INTRUSIVE_STATIC_ASSERT((detail::is_same<typename value_traits::value_type, T>::value));
  3804. inline explicit hashtable ( const bucket_traits &b_traits
  3805. , const hasher & hash_func = hasher()
  3806. , const key_equal &equal_func = key_equal()
  3807. , const value_traits &v_traits = value_traits())
  3808. : Base(b_traits, hash_func, equal_func, v_traits)
  3809. {}
  3810. inline hashtable(BOOST_RV_REF(hashtable) x)
  3811. : Base(BOOST_MOVE_BASE(Base, x))
  3812. {}
  3813. inline hashtable& operator=(BOOST_RV_REF(hashtable) x)
  3814. { return static_cast<hashtable&>(this->Base::operator=(BOOST_MOVE_BASE(Base, x))); }
  3815. template <class Cloner, class Disposer>
  3816. inline void clone_from(const hashtable &src, Cloner cloner, Disposer disposer)
  3817. { Base::clone_from(src, cloner, disposer); }
  3818. template <class Cloner, class Disposer>
  3819. inline void clone_from(BOOST_RV_REF(hashtable) src, Cloner cloner, Disposer disposer)
  3820. { Base::clone_from(BOOST_MOVE_BASE(Base, src), cloner, disposer); }
  3821. };
  3822. #endif
  3823. } //namespace intrusive
  3824. } //namespace boost
  3825. #include <boost/intrusive/detail/config_end.hpp>
  3826. #endif //BOOST_INTRUSIVE_HASHTABLE_HPP