implementation.hpp 160 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953
  1. // Copyright (C) 2003-2004 Jeremy B. Maitin-Shepard.
  2. // Copyright (C) 2005-2016 Daniel James
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_UNORDERED_DETAIL_IMPLEMENTATION_HPP
  7. #define BOOST_UNORDERED_DETAIL_IMPLEMENTATION_HPP
  8. #include <boost/config.hpp>
  9. #if defined(BOOST_HAS_PRAGMA_ONCE)
  10. #pragma once
  11. #endif
  12. #include <boost/assert.hpp>
  13. #include <boost/core/no_exceptions_support.hpp>
  14. #include <boost/core/pointer_traits.hpp>
  15. #include <boost/detail/select_type.hpp>
  16. #include <boost/limits.hpp>
  17. #include <boost/move/move.hpp>
  18. #include <boost/preprocessor/arithmetic/inc.hpp>
  19. #include <boost/preprocessor/cat.hpp>
  20. #include <boost/preprocessor/repetition/enum.hpp>
  21. #include <boost/preprocessor/repetition/enum_binary_params.hpp>
  22. #include <boost/preprocessor/repetition/enum_params.hpp>
  23. #include <boost/preprocessor/repetition/repeat_from_to.hpp>
  24. #include <boost/preprocessor/seq/enum.hpp>
  25. #include <boost/preprocessor/seq/size.hpp>
  26. #include <boost/swap.hpp>
  27. #include <boost/throw_exception.hpp>
  28. #include <boost/tuple/tuple.hpp>
  29. #include <boost/type_traits/add_lvalue_reference.hpp>
  30. #include <boost/type_traits/aligned_storage.hpp>
  31. #include <boost/type_traits/alignment_of.hpp>
  32. #include <boost/type_traits/integral_constant.hpp>
  33. #include <boost/type_traits/is_base_of.hpp>
  34. #include <boost/type_traits/is_class.hpp>
  35. #include <boost/type_traits/is_empty.hpp>
  36. #include <boost/type_traits/is_nothrow_move_assignable.hpp>
  37. #include <boost/type_traits/is_nothrow_move_constructible.hpp>
  38. #include <boost/type_traits/is_nothrow_swappable.hpp>
  39. #include <boost/type_traits/is_same.hpp>
  40. #include <boost/type_traits/remove_const.hpp>
  41. #include <boost/unordered/detail/fwd.hpp>
  42. #include <boost/utility/addressof.hpp>
  43. #include <boost/utility/enable_if.hpp>
  44. #include <cmath>
  45. #include <iterator>
  46. #include <stdexcept>
  47. #include <utility>
  48. #if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
  49. #include <type_traits>
  50. #endif
  51. ////////////////////////////////////////////////////////////////////////////////
  52. // Configuration
  53. //
  54. // Unless documented elsewhere these configuration macros should be considered
  55. // an implementation detail, I'll try not to break them, but you never know.
  56. // Use Sun C++ workarounds
  57. // I'm not sure which versions of the compiler require these workarounds, so
  58. // I'm just using them of everything older than the current test compilers
  59. // (as of May 2017).
  60. #if !defined(BOOST_UNORDERED_SUN_WORKAROUNDS1)
  61. #if BOOST_COMP_SUNPRO && BOOST_COMP_SUNPRO < BOOST_VERSION_NUMBER(5, 20, 0)
  62. #define BOOST_UNORDERED_SUN_WORKAROUNDS1 1
  63. #else
  64. #define BOOST_UNORDERED_SUN_WORKAROUNDS1 0
  65. #endif
  66. #endif
  67. // BOOST_UNORDERED_EMPLACE_LIMIT = The maximum number of parameters in
  68. // emplace (not including things like hints). Don't set it to a lower value, as
  69. // that might break something.
  70. #if !defined BOOST_UNORDERED_EMPLACE_LIMIT
  71. #define BOOST_UNORDERED_EMPLACE_LIMIT 10
  72. #endif
  73. // BOOST_UNORDERED_USE_ALLOCATOR_TRAITS - Pick which version of
  74. // allocator_traits to use.
  75. //
  76. // 0 = Own partial implementation
  77. // 1 = std::allocator_traits
  78. // 2 = boost::container::allocator_traits
  79. #if !defined(BOOST_UNORDERED_USE_ALLOCATOR_TRAITS)
  80. #if !defined(BOOST_NO_CXX11_ALLOCATOR)
  81. #define BOOST_UNORDERED_USE_ALLOCATOR_TRAITS 1
  82. #elif defined(BOOST_MSVC)
  83. #if BOOST_MSVC < 1400
  84. // Use container's allocator_traits for older versions of Visual
  85. // C++ as I don't test with them.
  86. #define BOOST_UNORDERED_USE_ALLOCATOR_TRAITS 2
  87. #endif
  88. #endif
  89. #endif
  90. #if !defined(BOOST_UNORDERED_USE_ALLOCATOR_TRAITS)
  91. #define BOOST_UNORDERED_USE_ALLOCATOR_TRAITS 0
  92. #endif
  93. // BOOST_UNORDERED_TUPLE_ARGS
  94. //
  95. // Maximum number of std::tuple members to support, or 0 if std::tuple
  96. // isn't avaiable. More are supported when full C++11 is used.
  97. // Already defined, so do nothing
  98. #if defined(BOOST_UNORDERED_TUPLE_ARGS)
  99. // Assume if we have C++11 tuple it's properly variadic,
  100. // and just use a max number of 10 arguments.
  101. #elif !defined(BOOST_NO_CXX11_HDR_TUPLE)
  102. #define BOOST_UNORDERED_TUPLE_ARGS 10
  103. // Visual C++ has a decent enough tuple for piecewise construction,
  104. // so use that if available, using _VARIADIC_MAX for the maximum
  105. // number of parameters. Note that this comes after the check
  106. // for a full C++11 tuple.
  107. #elif defined(BOOST_MSVC)
  108. #if !BOOST_UNORDERED_HAVE_PIECEWISE_CONSTRUCT
  109. #define BOOST_UNORDERED_TUPLE_ARGS 0
  110. #elif defined(_VARIADIC_MAX)
  111. #define BOOST_UNORDERED_TUPLE_ARGS _VARIADIC_MAX
  112. #else
  113. #define BOOST_UNORDERED_TUPLE_ARGS 5
  114. #endif
  115. // Assume that we don't have std::tuple
  116. #else
  117. #define BOOST_UNORDERED_TUPLE_ARGS 0
  118. #endif
  119. #if BOOST_UNORDERED_TUPLE_ARGS
  120. #include <tuple>
  121. #endif
  122. // BOOST_UNORDERED_CXX11_CONSTRUCTION
  123. //
  124. // Use C++11 construction, requires variadic arguments, good construct support
  125. // in allocator_traits and piecewise construction of std::pair
  126. // Otherwise allocators aren't used for construction/destruction
  127. #if BOOST_UNORDERED_HAVE_PIECEWISE_CONSTRUCT && \
  128. !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && BOOST_UNORDERED_TUPLE_ARGS
  129. #if BOOST_COMP_SUNPRO && BOOST_LIB_STD_GNU
  130. // Sun C++ std::pair piecewise construction doesn't seem to be exception safe.
  131. // (At least for Sun C++ 12.5 using libstdc++).
  132. #define BOOST_UNORDERED_CXX11_CONSTRUCTION 0
  133. #elif BOOST_COMP_GNUC && BOOST_COMP_GNUC < BOOST_VERSION_NUMBER(4, 7, 0)
  134. // Piecewise construction in GCC 4.6 doesn't work for uncopyable types.
  135. #define BOOST_UNORDERED_CXX11_CONSTRUCTION 0
  136. #elif BOOST_UNORDERED_USE_ALLOCATOR_TRAITS == 0 && \
  137. !defined(BOOST_NO_SFINAE_EXPR)
  138. #define BOOST_UNORDERED_CXX11_CONSTRUCTION 1
  139. #elif BOOST_UNORDERED_USE_ALLOCATOR_TRAITS == 1
  140. #define BOOST_UNORDERED_CXX11_CONSTRUCTION 1
  141. #endif
  142. #endif
  143. #if !defined(BOOST_UNORDERED_CXX11_CONSTRUCTION)
  144. #define BOOST_UNORDERED_CXX11_CONSTRUCTION 0
  145. #endif
  146. // BOOST_UNORDERED_SUPPRESS_DEPRECATED
  147. //
  148. // Define to stop deprecation attributes
  149. #if defined(BOOST_UNORDERED_SUPPRESS_DEPRECATED)
  150. #define BOOST_UNORDERED_DEPRECATED(msg)
  151. #endif
  152. // BOOST_UNORDERED_DEPRECATED
  153. //
  154. // Wrapper around various depreaction attributes.
  155. #if defined(__has_cpp_attribute) && \
  156. (!defined(__cplusplus) || __cplusplus >= 201402)
  157. #if __has_cpp_attribute(deprecated) && !defined(BOOST_UNORDERED_DEPRECATED)
  158. #define BOOST_UNORDERED_DEPRECATED(msg) [[deprecated(msg)]]
  159. #endif
  160. #endif
  161. #if !defined(BOOST_UNORDERED_DEPRECATED)
  162. #if defined(__GNUC__) && __GNUC__ >= 4
  163. #define BOOST_UNORDERED_DEPRECATED(msg) __attribute__((deprecated))
  164. #elif defined(_MSC_VER) && _MSC_VER >= 1400
  165. #define BOOST_UNORDERED_DEPRECATED(msg) __declspec(deprecated(msg))
  166. #elif defined(_MSC_VER) && _MSC_VER >= 1310
  167. #define BOOST_UNORDERED_DEPRECATED(msg) __declspec(deprecated)
  168. #else
  169. #define BOOST_UNORDERED_DEPRECATED(msg)
  170. #endif
  171. #endif
  172. // BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES
  173. #if !defined(BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES)
  174. #if BOOST_COMP_CLANG && __cplusplus >= 201703
  175. #define BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES 1
  176. #endif
  177. #endif
  178. #if !defined(BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES)
  179. #define BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES 0
  180. #endif
  181. namespace boost {
  182. namespace unordered {
  183. namespace iterator_detail {
  184. template <typename Node> struct iterator;
  185. template <typename Node> struct c_iterator;
  186. template <typename Node> struct l_iterator;
  187. template <typename Node> struct cl_iterator;
  188. }
  189. }
  190. }
  191. namespace boost {
  192. namespace unordered {
  193. namespace detail {
  194. template <typename Types> struct table;
  195. template <typename NodePointer> struct bucket;
  196. struct ptr_bucket;
  197. template <typename A, typename T> struct node;
  198. template <typename T> struct ptr_node;
  199. static const float minimum_max_load_factor = 1e-3f;
  200. static const std::size_t default_bucket_count = 11;
  201. struct move_tag
  202. {
  203. };
  204. struct empty_emplace
  205. {
  206. };
  207. struct no_key
  208. {
  209. no_key() {}
  210. template <class T> no_key(T const&) {}
  211. };
  212. namespace func {
  213. template <class T> inline void ignore_unused_variable_warning(T const&)
  214. {
  215. }
  216. }
  217. //////////////////////////////////////////////////////////////////////////
  218. // iterator SFINAE
  219. template <typename I>
  220. struct is_forward : boost::is_base_of<std::forward_iterator_tag,
  221. typename std::iterator_traits<I>::iterator_category>
  222. {
  223. };
  224. template <typename I, typename ReturnType>
  225. struct enable_if_forward
  226. : boost::enable_if_c<boost::unordered::detail::is_forward<I>::value,
  227. ReturnType>
  228. {
  229. };
  230. template <typename I, typename ReturnType>
  231. struct disable_if_forward
  232. : boost::disable_if_c<boost::unordered::detail::is_forward<I>::value,
  233. ReturnType>
  234. {
  235. };
  236. }
  237. }
  238. }
  239. ////////////////////////////////////////////////////////////////////////////////
  240. // primes
  241. // clang-format off
  242. #define BOOST_UNORDERED_PRIMES \
  243. (17ul)(29ul)(37ul)(53ul)(67ul)(79ul) \
  244. (97ul)(131ul)(193ul)(257ul)(389ul)(521ul)(769ul) \
  245. (1031ul)(1543ul)(2053ul)(3079ul)(6151ul)(12289ul)(24593ul) \
  246. (49157ul)(98317ul)(196613ul)(393241ul)(786433ul) \
  247. (1572869ul)(3145739ul)(6291469ul)(12582917ul)(25165843ul) \
  248. (50331653ul)(100663319ul)(201326611ul)(402653189ul)(805306457ul) \
  249. (1610612741ul)(3221225473ul)(4294967291ul)
  250. // clang-format on
  251. namespace boost {
  252. namespace unordered {
  253. namespace detail {
  254. template <class T> struct prime_list_template
  255. {
  256. static std::size_t const value[];
  257. #if !BOOST_UNORDERED_SUN_WORKAROUNDS1
  258. static std::ptrdiff_t const length;
  259. #else
  260. static std::ptrdiff_t const length =
  261. BOOST_PP_SEQ_SIZE(BOOST_UNORDERED_PRIMES);
  262. #endif
  263. };
  264. template <class T>
  265. std::size_t const prime_list_template<T>::value[] = {
  266. BOOST_PP_SEQ_ENUM(BOOST_UNORDERED_PRIMES)};
  267. #if !BOOST_UNORDERED_SUN_WORKAROUNDS1
  268. template <class T>
  269. std::ptrdiff_t const prime_list_template<T>::length = BOOST_PP_SEQ_SIZE(
  270. BOOST_UNORDERED_PRIMES);
  271. #endif
  272. #undef BOOST_UNORDERED_PRIMES
  273. typedef prime_list_template<std::size_t> prime_list;
  274. // no throw
  275. inline std::size_t next_prime(std::size_t num)
  276. {
  277. std::size_t const* const prime_list_begin = prime_list::value;
  278. std::size_t const* const prime_list_end =
  279. prime_list_begin + prime_list::length;
  280. std::size_t const* bound =
  281. std::lower_bound(prime_list_begin, prime_list_end, num);
  282. if (bound == prime_list_end)
  283. bound--;
  284. return *bound;
  285. }
  286. // no throw
  287. inline std::size_t prev_prime(std::size_t num)
  288. {
  289. std::size_t const* const prime_list_begin = prime_list::value;
  290. std::size_t const* const prime_list_end =
  291. prime_list_begin + prime_list::length;
  292. std::size_t const* bound =
  293. std::upper_bound(prime_list_begin, prime_list_end, num);
  294. if (bound != prime_list_begin)
  295. bound--;
  296. return *bound;
  297. }
  298. //////////////////////////////////////////////////////////////////////////
  299. // insert_size/initial_size
  300. template <class I>
  301. inline std::size_t insert_size(I i, I j,
  302. typename boost::unordered::detail::enable_if_forward<I, void*>::type =
  303. 0)
  304. {
  305. return static_cast<std::size_t>(std::distance(i, j));
  306. }
  307. template <class I>
  308. inline std::size_t insert_size(I, I,
  309. typename boost::unordered::detail::disable_if_forward<I, void*>::type =
  310. 0)
  311. {
  312. return 1;
  313. }
  314. template <class I>
  315. inline std::size_t initial_size(I i, I j,
  316. std::size_t num_buckets =
  317. boost::unordered::detail::default_bucket_count)
  318. {
  319. return (std::max)(
  320. boost::unordered::detail::insert_size(i, j), num_buckets);
  321. }
  322. //////////////////////////////////////////////////////////////////////////
  323. // compressed
  324. template <typename T, int Index> struct compressed_base : private T
  325. {
  326. compressed_base(T const& x) : T(x) {}
  327. compressed_base(T& x, move_tag) : T(boost::move(x)) {}
  328. T& get() { return *this; }
  329. T const& get() const { return *this; }
  330. };
  331. template <typename T, int Index> struct uncompressed_base
  332. {
  333. uncompressed_base(T const& x) : value_(x) {}
  334. uncompressed_base(T& x, move_tag) : value_(boost::move(x)) {}
  335. T& get() { return value_; }
  336. T const& get() const { return value_; }
  337. private:
  338. T value_;
  339. };
  340. template <typename T, int Index>
  341. struct generate_base
  342. : boost::detail::if_true<
  343. boost::is_empty<T>::value>::BOOST_NESTED_TEMPLATE
  344. then<boost::unordered::detail::compressed_base<T, Index>,
  345. boost::unordered::detail::uncompressed_base<T, Index> >
  346. {
  347. };
  348. template <typename T1, typename T2>
  349. struct compressed
  350. : private boost::unordered::detail::generate_base<T1, 1>::type,
  351. private boost::unordered::detail::generate_base<T2, 2>::type
  352. {
  353. typedef typename generate_base<T1, 1>::type base1;
  354. typedef typename generate_base<T2, 2>::type base2;
  355. typedef T1 first_type;
  356. typedef T2 second_type;
  357. first_type& first() { return static_cast<base1*>(this)->get(); }
  358. first_type const& first() const
  359. {
  360. return static_cast<base1 const*>(this)->get();
  361. }
  362. second_type& second() { return static_cast<base2*>(this)->get(); }
  363. second_type const& second() const
  364. {
  365. return static_cast<base2 const*>(this)->get();
  366. }
  367. template <typename First, typename Second>
  368. compressed(First const& x1, Second const& x2) : base1(x1), base2(x2)
  369. {
  370. }
  371. compressed(compressed const& x) : base1(x.first()), base2(x.second()) {}
  372. compressed(compressed& x, move_tag m)
  373. : base1(x.first(), m), base2(x.second(), m)
  374. {
  375. }
  376. void assign(compressed const& x)
  377. {
  378. first() = x.first();
  379. second() = x.second();
  380. }
  381. void move_assign(compressed& x)
  382. {
  383. first() = boost::move(x.first());
  384. second() = boost::move(x.second());
  385. }
  386. void swap(compressed& x)
  387. {
  388. boost::swap(first(), x.first());
  389. boost::swap(second(), x.second());
  390. }
  391. private:
  392. // Prevent assignment just to make use of assign or
  393. // move_assign explicit.
  394. compressed& operator=(compressed const&);
  395. };
  396. //////////////////////////////////////////////////////////////////////////
  397. // pair_traits
  398. //
  399. // Used to get the types from a pair without instantiating it.
  400. template <typename Pair> struct pair_traits
  401. {
  402. typedef typename Pair::first_type first_type;
  403. typedef typename Pair::second_type second_type;
  404. };
  405. template <typename T1, typename T2> struct pair_traits<std::pair<T1, T2> >
  406. {
  407. typedef T1 first_type;
  408. typedef T2 second_type;
  409. };
  410. #if defined(BOOST_MSVC)
  411. #pragma warning(push)
  412. #pragma warning(disable : 4512) // assignment operator could not be generated.
  413. #pragma warning(disable : 4345) // behavior change: an object of POD type
  414. // constructed with an initializer of the form ()
  415. // will be default-initialized.
  416. #endif
  417. //////////////////////////////////////////////////////////////////////////
  418. // Bits and pieces for implementing traits
  419. template <typename T>
  420. typename boost::add_lvalue_reference<T>::type make();
  421. struct choice9
  422. {
  423. typedef char (&type)[9];
  424. };
  425. struct choice8 : choice9
  426. {
  427. typedef char (&type)[8];
  428. };
  429. struct choice7 : choice8
  430. {
  431. typedef char (&type)[7];
  432. };
  433. struct choice6 : choice7
  434. {
  435. typedef char (&type)[6];
  436. };
  437. struct choice5 : choice6
  438. {
  439. typedef char (&type)[5];
  440. };
  441. struct choice4 : choice5
  442. {
  443. typedef char (&type)[4];
  444. };
  445. struct choice3 : choice4
  446. {
  447. typedef char (&type)[3];
  448. };
  449. struct choice2 : choice3
  450. {
  451. typedef char (&type)[2];
  452. };
  453. struct choice1 : choice2
  454. {
  455. typedef char (&type)[1];
  456. };
  457. choice1 choose();
  458. typedef choice1::type yes_type;
  459. typedef choice2::type no_type;
  460. struct private_type
  461. {
  462. private_type const& operator,(int) const;
  463. };
  464. template <typename T> no_type is_private_type(T const&);
  465. yes_type is_private_type(private_type const&);
  466. struct convert_from_anything
  467. {
  468. template <typename T> convert_from_anything(T const&);
  469. };
  470. }
  471. }
  472. }
  473. ////////////////////////////////////////////////////////////////////////////
  474. // emplace_args
  475. //
  476. // Either forwarding variadic arguments, or storing the arguments in
  477. // emplace_args##n
  478. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  479. #define BOOST_UNORDERED_EMPLACE_TEMPLATE typename... Args
  480. #define BOOST_UNORDERED_EMPLACE_ARGS BOOST_FWD_REF(Args)... args
  481. #define BOOST_UNORDERED_EMPLACE_FORWARD boost::forward<Args>(args)...
  482. #else
  483. #define BOOST_UNORDERED_EMPLACE_TEMPLATE typename Args
  484. #define BOOST_UNORDERED_EMPLACE_ARGS Args const& args
  485. #define BOOST_UNORDERED_EMPLACE_FORWARD args
  486. #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  487. #define BOOST_UNORDERED_EARGS_MEMBER(z, n, _) \
  488. typedef BOOST_FWD_REF(BOOST_PP_CAT(A, n)) BOOST_PP_CAT(Arg, n); \
  489. BOOST_PP_CAT(Arg, n) BOOST_PP_CAT(a, n);
  490. #else
  491. #define BOOST_UNORDERED_EARGS_MEMBER(z, n, _) \
  492. typedef typename boost::add_lvalue_reference<BOOST_PP_CAT(A, n)>::type \
  493. BOOST_PP_CAT(Arg, n); \
  494. BOOST_PP_CAT(Arg, n) BOOST_PP_CAT(a, n);
  495. #endif
  496. #define BOOST_UNORDERED_FWD_PARAM(z, n, a) \
  497. BOOST_FWD_REF(BOOST_PP_CAT(A, n)) BOOST_PP_CAT(a, n)
  498. #define BOOST_UNORDERED_CALL_FORWARD(z, i, a) \
  499. boost::forward<BOOST_PP_CAT(A, i)>(BOOST_PP_CAT(a, i))
  500. #define BOOST_UNORDERED_EARGS_INIT(z, n, _) \
  501. BOOST_PP_CAT(a, n)(BOOST_PP_CAT(b, n))
  502. #define BOOST_UNORDERED_EARGS(z, n, _) \
  503. template <BOOST_PP_ENUM_PARAMS_Z(z, n, typename A)> \
  504. struct BOOST_PP_CAT(emplace_args, n) \
  505. { \
  506. BOOST_PP_REPEAT_##z(n, BOOST_UNORDERED_EARGS_MEMBER, _) BOOST_PP_CAT( \
  507. emplace_args, n)(BOOST_PP_ENUM_BINARY_PARAMS_Z(z, n, Arg, b)) \
  508. : BOOST_PP_ENUM_##z(n, BOOST_UNORDERED_EARGS_INIT, _) \
  509. { \
  510. } \
  511. }; \
  512. \
  513. template <BOOST_PP_ENUM_PARAMS_Z(z, n, typename A)> \
  514. inline BOOST_PP_CAT(emplace_args, n)<BOOST_PP_ENUM_PARAMS_Z(z, n, A)> \
  515. create_emplace_args(BOOST_PP_ENUM_##z(n, BOOST_UNORDERED_FWD_PARAM, b)) \
  516. { \
  517. BOOST_PP_CAT(emplace_args, n)<BOOST_PP_ENUM_PARAMS_Z(z, n, A)> e( \
  518. BOOST_PP_ENUM_PARAMS_Z(z, n, b)); \
  519. return e; \
  520. }
  521. namespace boost {
  522. namespace unordered {
  523. namespace detail {
  524. template <typename A0> struct emplace_args1
  525. {
  526. BOOST_UNORDERED_EARGS_MEMBER(1, 0, _)
  527. explicit emplace_args1(Arg0 b0) : a0(b0) {}
  528. };
  529. template <typename A0>
  530. inline emplace_args1<A0> create_emplace_args(BOOST_FWD_REF(A0) b0)
  531. {
  532. emplace_args1<A0> e(b0);
  533. return e;
  534. }
  535. template <typename A0, typename A1> struct emplace_args2
  536. {
  537. BOOST_UNORDERED_EARGS_MEMBER(1, 0, _)
  538. BOOST_UNORDERED_EARGS_MEMBER(1, 1, _)
  539. emplace_args2(Arg0 b0, Arg1 b1) : a0(b0), a1(b1) {}
  540. };
  541. template <typename A0, typename A1>
  542. inline emplace_args2<A0, A1> create_emplace_args(
  543. BOOST_FWD_REF(A0) b0, BOOST_FWD_REF(A1) b1)
  544. {
  545. emplace_args2<A0, A1> e(b0, b1);
  546. return e;
  547. }
  548. template <typename A0, typename A1, typename A2> struct emplace_args3
  549. {
  550. BOOST_UNORDERED_EARGS_MEMBER(1, 0, _)
  551. BOOST_UNORDERED_EARGS_MEMBER(1, 1, _)
  552. BOOST_UNORDERED_EARGS_MEMBER(1, 2, _)
  553. emplace_args3(Arg0 b0, Arg1 b1, Arg2 b2) : a0(b0), a1(b1), a2(b2) {}
  554. };
  555. template <typename A0, typename A1, typename A2>
  556. inline emplace_args3<A0, A1, A2> create_emplace_args(
  557. BOOST_FWD_REF(A0) b0, BOOST_FWD_REF(A1) b1, BOOST_FWD_REF(A2) b2)
  558. {
  559. emplace_args3<A0, A1, A2> e(b0, b1, b2);
  560. return e;
  561. }
  562. BOOST_UNORDERED_EARGS(1, 4, _)
  563. BOOST_UNORDERED_EARGS(1, 5, _)
  564. BOOST_UNORDERED_EARGS(1, 6, _)
  565. BOOST_UNORDERED_EARGS(1, 7, _)
  566. BOOST_UNORDERED_EARGS(1, 8, _)
  567. BOOST_UNORDERED_EARGS(1, 9, _)
  568. BOOST_PP_REPEAT_FROM_TO(10, BOOST_PP_INC(BOOST_UNORDERED_EMPLACE_LIMIT),
  569. BOOST_UNORDERED_EARGS, _)
  570. }
  571. }
  572. }
  573. #undef BOOST_UNORDERED_DEFINE_EMPLACE_ARGS
  574. #undef BOOST_UNORDERED_EARGS_MEMBER
  575. #undef BOOST_UNORDERED_EARGS_INIT
  576. #endif
  577. ////////////////////////////////////////////////////////////////////////////////
  578. //
  579. // Some utilities for implementing allocator_traits, but useful elsewhere so
  580. // they're always defined.
  581. namespace boost {
  582. namespace unordered {
  583. namespace detail {
  584. ////////////////////////////////////////////////////////////////////////////
  585. // Integral_constrant, true_type, false_type
  586. //
  587. // Uses the standard versions if available.
  588. #if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
  589. using std::integral_constant;
  590. using std::true_type;
  591. using std::false_type;
  592. #else
  593. template <typename T, T Value> struct integral_constant
  594. {
  595. enum
  596. {
  597. value = Value
  598. };
  599. };
  600. typedef boost::unordered::detail::integral_constant<bool, true> true_type;
  601. typedef boost::unordered::detail::integral_constant<bool, false>
  602. false_type;
  603. #endif
  604. ////////////////////////////////////////////////////////////////////////////
  605. // Explicitly call a destructor
  606. #if defined(BOOST_MSVC)
  607. #pragma warning(push)
  608. #pragma warning(disable : 4100) // unreferenced formal parameter
  609. #endif
  610. namespace func {
  611. template <class T> inline void destroy(T* x) { x->~T(); }
  612. }
  613. #if defined(BOOST_MSVC)
  614. #pragma warning(pop)
  615. #endif
  616. //////////////////////////////////////////////////////////////////////////
  617. // value_base
  618. //
  619. // Space used to store values.
  620. template <typename ValueType> struct value_base
  621. {
  622. typedef ValueType value_type;
  623. typename boost::aligned_storage<sizeof(value_type),
  624. boost::alignment_of<value_type>::value>::type data_;
  625. value_base() : data_() {}
  626. void* address() { return this; }
  627. value_type& value() { return *(ValueType*)this; }
  628. value_type const& value() const { return *(ValueType const*)this; }
  629. value_type* value_ptr() { return (ValueType*)this; }
  630. value_type const* value_ptr() const { return (ValueType const*)this; }
  631. private:
  632. value_base& operator=(value_base const&);
  633. };
  634. //////////////////////////////////////////////////////////////////////////
  635. // optional
  636. // TODO: Use std::optional when available.
  637. template <typename T> class optional
  638. {
  639. BOOST_MOVABLE_BUT_NOT_COPYABLE(optional)
  640. boost::unordered::detail::value_base<T> value_;
  641. bool has_value_;
  642. void destroy()
  643. {
  644. if (has_value_) {
  645. boost::unordered::detail::func::destroy(value_.value_ptr());
  646. has_value_ = false;
  647. }
  648. }
  649. void move(optional<T>& x)
  650. {
  651. BOOST_ASSERT(!has_value_ && x.has_value_);
  652. new (value_.value_ptr()) T(boost::move(x.value_.value()));
  653. boost::unordered::detail::func::destroy(x.value_.value_ptr());
  654. has_value_ = true;
  655. x.has_value_ = false;
  656. }
  657. public:
  658. optional() BOOST_NOEXCEPT : has_value_(false) {}
  659. optional(BOOST_RV_REF(optional<T>) x) : has_value_(false)
  660. {
  661. if (x.has_value_) {
  662. move(x);
  663. }
  664. }
  665. explicit optional(T const& x) : has_value_(true)
  666. {
  667. new (value_.value_ptr()) T(x);
  668. }
  669. optional& operator=(BOOST_RV_REF(optional<T>) x)
  670. {
  671. destroy();
  672. if (x.has_value_) {
  673. move(x);
  674. }
  675. return *this;
  676. }
  677. ~optional() { destroy(); }
  678. bool has_value() const { return has_value_; }
  679. T& operator*() { return value_.value(); }
  680. T const& operator*() const { return value_.value(); }
  681. T* operator->() { return value_.value_ptr(); }
  682. T const* operator->() const { return value_.value_ptr(); }
  683. bool operator==(optional<T> const& x)
  684. {
  685. return has_value_ ? x.has_value_ && value_.value() == x.value_.value()
  686. : !x.has_value_;
  687. }
  688. bool operator!=(optional<T> const& x) { return !((*this) == x); }
  689. void swap(optional<T>& x)
  690. {
  691. if (has_value_ != x.has_value_) {
  692. if (has_value_) {
  693. x.move(*this);
  694. } else {
  695. move(x);
  696. }
  697. } else if (has_value_) {
  698. boost::swap(value_.value(), x.value_.value());
  699. }
  700. }
  701. friend void swap(optional<T>& x, optional<T>& y) { x.swap(y); }
  702. };
  703. }
  704. }
  705. }
  706. ////////////////////////////////////////////////////////////////////////////
  707. // Expression test mechanism
  708. //
  709. // When SFINAE expressions are available, define
  710. // BOOST_UNORDERED_HAS_FUNCTION which can check if a function call is
  711. // supported by a class, otherwise define BOOST_UNORDERED_HAS_MEMBER which
  712. // can detect if a class has the specified member, but not that it has the
  713. // correct type, this is good enough for a passable impression of
  714. // allocator_traits.
  715. #if !defined(BOOST_NO_SFINAE_EXPR)
  716. namespace boost {
  717. namespace unordered {
  718. namespace detail {
  719. template <typename T, long unsigned int> struct expr_test;
  720. template <typename T> struct expr_test<T, sizeof(char)> : T
  721. {
  722. };
  723. }
  724. }
  725. }
  726. #define BOOST_UNORDERED_CHECK_EXPRESSION(count, result, expression) \
  727. template <typename U> \
  728. static \
  729. typename boost::unordered::detail::expr_test<BOOST_PP_CAT(choice, result), \
  730. sizeof(for_expr_test(((expression), 0)))>::type \
  731. test(BOOST_PP_CAT(choice, count))
  732. #define BOOST_UNORDERED_DEFAULT_EXPRESSION(count, result) \
  733. template <typename U> \
  734. static BOOST_PP_CAT(choice, result)::type test(BOOST_PP_CAT(choice, count))
  735. #define BOOST_UNORDERED_HAS_FUNCTION(name, thing, args, _) \
  736. struct BOOST_PP_CAT(has_, name) \
  737. { \
  738. template <typename U> static char for_expr_test(U const&); \
  739. BOOST_UNORDERED_CHECK_EXPRESSION( \
  740. 1, 1, boost::unordered::detail::make<thing>().name args); \
  741. BOOST_UNORDERED_DEFAULT_EXPRESSION(2, 2); \
  742. \
  743. enum \
  744. { \
  745. value = sizeof(test<T>(choose())) == sizeof(choice1::type) \
  746. }; \
  747. }
  748. #else
  749. namespace boost {
  750. namespace unordered {
  751. namespace detail {
  752. template <typename T> struct identity
  753. {
  754. typedef T type;
  755. };
  756. }
  757. }
  758. }
  759. #define BOOST_UNORDERED_CHECK_MEMBER(count, result, name, member) \
  760. \
  761. typedef \
  762. typename boost::unordered::detail::identity<member>::type BOOST_PP_CAT( \
  763. check, count); \
  764. \
  765. template <BOOST_PP_CAT(check, count) e> struct BOOST_PP_CAT(test, count) \
  766. { \
  767. typedef BOOST_PP_CAT(choice, result) type; \
  768. }; \
  769. \
  770. template <class U> \
  771. static typename BOOST_PP_CAT(test, count)<&U::name>::type test( \
  772. BOOST_PP_CAT(choice, count))
  773. #define BOOST_UNORDERED_DEFAULT_MEMBER(count, result) \
  774. template <class U> \
  775. static BOOST_PP_CAT(choice, result)::type test(BOOST_PP_CAT(choice, count))
  776. #define BOOST_UNORDERED_HAS_MEMBER(name) \
  777. struct BOOST_PP_CAT(has_, name) \
  778. { \
  779. struct impl \
  780. { \
  781. struct base_mixin \
  782. { \
  783. int name; \
  784. }; \
  785. struct base : public T, public base_mixin \
  786. { \
  787. }; \
  788. \
  789. BOOST_UNORDERED_CHECK_MEMBER(1, 1, name, int base_mixin::*); \
  790. BOOST_UNORDERED_DEFAULT_MEMBER(2, 2); \
  791. \
  792. enum \
  793. { \
  794. value = sizeof(choice2::type) == sizeof(test<base>(choose())) \
  795. }; \
  796. }; \
  797. \
  798. enum \
  799. { \
  800. value = impl::value \
  801. }; \
  802. }
  803. #endif
  804. ////////////////////////////////////////////////////////////////////////////
  805. // TRAITS TYPE DETECTION MECHANISM
  806. //
  807. // Used to implement traits that use a type if present, or a
  808. // default otherwise.
  809. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1400
  810. #define BOOST_UNORDERED_DEFAULT_TYPE_TMPLT(tname) \
  811. template <typename Tp, typename Default> struct default_type_##tname \
  812. { \
  813. \
  814. template <typename X> \
  815. static choice1::type test(choice1, typename X::tname* = 0); \
  816. \
  817. template <typename X> static choice2::type test(choice2, void* = 0); \
  818. \
  819. struct DefaultWrap \
  820. { \
  821. typedef Default tname; \
  822. }; \
  823. \
  824. enum \
  825. { \
  826. value = (1 == sizeof(test<Tp>(choose()))) \
  827. }; \
  828. \
  829. typedef typename boost::detail::if_true<value>::BOOST_NESTED_TEMPLATE \
  830. then<Tp, DefaultWrap>::type::tname type; \
  831. }
  832. #else
  833. namespace boost {
  834. namespace unordered {
  835. namespace detail {
  836. template <typename T, typename T2> struct sfinae : T2
  837. {
  838. };
  839. }
  840. }
  841. }
  842. #define BOOST_UNORDERED_DEFAULT_TYPE_TMPLT(tname) \
  843. template <typename Tp, typename Default> struct default_type_##tname \
  844. { \
  845. \
  846. template <typename X> \
  847. static typename boost::unordered::detail::sfinae<typename X::tname, \
  848. choice1>::type test(choice1); \
  849. \
  850. template <typename X> static choice2::type test(choice2); \
  851. \
  852. struct DefaultWrap \
  853. { \
  854. typedef Default tname; \
  855. }; \
  856. \
  857. enum \
  858. { \
  859. value = (1 == sizeof(test<Tp>(choose()))) \
  860. }; \
  861. \
  862. typedef typename boost::detail::if_true<value>::BOOST_NESTED_TEMPLATE \
  863. then<Tp, DefaultWrap>::type::tname type; \
  864. }
  865. #endif
  866. #define BOOST_UNORDERED_DEFAULT_TYPE(T, tname, arg) \
  867. typename default_type_##tname<T, arg>::type
  868. ////////////////////////////////////////////////////////////////////////////////
  869. //
  870. // Allocator traits
  871. //
  872. // First our implementation, then later light wrappers around the alternatives
  873. #if BOOST_UNORDERED_USE_ALLOCATOR_TRAITS == 0
  874. #include <boost/limits.hpp>
  875. #include <boost/pointer_to_other.hpp>
  876. #include <boost/utility/enable_if.hpp>
  877. namespace boost {
  878. namespace unordered {
  879. namespace detail {
  880. template <typename Alloc, typename T> struct rebind_alloc;
  881. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  882. template <template <typename, typename...> class Alloc, typename U,
  883. typename T, typename... Args>
  884. struct rebind_alloc<Alloc<U, Args...>, T>
  885. {
  886. typedef Alloc<T, Args...> type;
  887. };
  888. #else
  889. template <template <typename> class Alloc, typename U, typename T>
  890. struct rebind_alloc<Alloc<U>, T>
  891. {
  892. typedef Alloc<T> type;
  893. };
  894. template <template <typename, typename> class Alloc, typename U,
  895. typename T, typename A0>
  896. struct rebind_alloc<Alloc<U, A0>, T>
  897. {
  898. typedef Alloc<T, A0> type;
  899. };
  900. template <template <typename, typename, typename> class Alloc, typename U,
  901. typename T, typename A0, typename A1>
  902. struct rebind_alloc<Alloc<U, A0, A1>, T>
  903. {
  904. typedef Alloc<T, A0, A1> type;
  905. };
  906. #endif
  907. template <typename Alloc, typename T> struct rebind_wrap
  908. {
  909. template <typename X>
  910. static choice1::type test(
  911. choice1, typename X::BOOST_NESTED_TEMPLATE rebind<T>::other* = 0);
  912. template <typename X> static choice2::type test(choice2, void* = 0);
  913. enum
  914. {
  915. value = (1 == sizeof(test<Alloc>(choose())))
  916. };
  917. struct fallback
  918. {
  919. template <typename U> struct rebind
  920. {
  921. typedef typename rebind_alloc<Alloc, T>::type other;
  922. };
  923. };
  924. typedef
  925. typename boost::detail::if_true<value>::BOOST_NESTED_TEMPLATE then<
  926. Alloc, fallback>::type::BOOST_NESTED_TEMPLATE rebind<T>::other type;
  927. };
  928. }
  929. }
  930. }
  931. namespace boost {
  932. namespace unordered {
  933. namespace detail {
  934. BOOST_UNORDERED_DEFAULT_TYPE_TMPLT(pointer);
  935. BOOST_UNORDERED_DEFAULT_TYPE_TMPLT(const_pointer);
  936. BOOST_UNORDERED_DEFAULT_TYPE_TMPLT(void_pointer);
  937. BOOST_UNORDERED_DEFAULT_TYPE_TMPLT(const_void_pointer);
  938. BOOST_UNORDERED_DEFAULT_TYPE_TMPLT(difference_type);
  939. BOOST_UNORDERED_DEFAULT_TYPE_TMPLT(size_type);
  940. BOOST_UNORDERED_DEFAULT_TYPE_TMPLT(
  941. propagate_on_container_copy_assignment);
  942. BOOST_UNORDERED_DEFAULT_TYPE_TMPLT(
  943. propagate_on_container_move_assignment);
  944. BOOST_UNORDERED_DEFAULT_TYPE_TMPLT(propagate_on_container_swap);
  945. BOOST_UNORDERED_DEFAULT_TYPE_TMPLT(is_always_equal);
  946. #if !defined(BOOST_NO_SFINAE_EXPR)
  947. template <typename T>
  948. BOOST_UNORDERED_HAS_FUNCTION(
  949. select_on_container_copy_construction, U const, (), 0);
  950. template <typename T>
  951. BOOST_UNORDERED_HAS_FUNCTION(max_size, U const, (), 0);
  952. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  953. template <typename T, typename ValueType, typename... Args>
  954. BOOST_UNORDERED_HAS_FUNCTION(construct, U,
  955. (boost::unordered::detail::make<ValueType*>(),
  956. boost::unordered::detail::make<Args const>()...),
  957. 2);
  958. #else
  959. template <typename T, typename ValueType>
  960. BOOST_UNORDERED_HAS_FUNCTION(construct, U,
  961. (boost::unordered::detail::make<ValueType*>(),
  962. boost::unordered::detail::make<ValueType const>()),
  963. 2);
  964. #endif
  965. template <typename T, typename ValueType>
  966. BOOST_UNORDERED_HAS_FUNCTION(
  967. destroy, U, (boost::unordered::detail::make<ValueType*>()), 1);
  968. #else
  969. template <typename T>
  970. BOOST_UNORDERED_HAS_MEMBER(select_on_container_copy_construction);
  971. template <typename T> BOOST_UNORDERED_HAS_MEMBER(max_size);
  972. template <typename T, typename ValueType>
  973. BOOST_UNORDERED_HAS_MEMBER(construct);
  974. template <typename T, typename ValueType>
  975. BOOST_UNORDERED_HAS_MEMBER(destroy);
  976. #endif
  977. }
  978. }
  979. }
  980. namespace boost {
  981. namespace unordered {
  982. namespace detail {
  983. namespace func {
  984. template <typename Alloc>
  985. inline Alloc call_select_on_container_copy_construction(
  986. const Alloc& rhs,
  987. typename boost::enable_if_c<
  988. boost::unordered::detail::has_select_on_container_copy_construction<
  989. Alloc>::value,
  990. void*>::type = 0)
  991. {
  992. return rhs.select_on_container_copy_construction();
  993. }
  994. template <typename Alloc>
  995. inline Alloc call_select_on_container_copy_construction(
  996. const Alloc& rhs,
  997. typename boost::disable_if_c<
  998. boost::unordered::detail::has_select_on_container_copy_construction<
  999. Alloc>::value,
  1000. void*>::type = 0)
  1001. {
  1002. return rhs;
  1003. }
  1004. template <typename SizeType, typename Alloc>
  1005. inline SizeType call_max_size(const Alloc& a,
  1006. typename boost::enable_if_c<
  1007. boost::unordered::detail::has_max_size<Alloc>::value, void*>::type =
  1008. 0)
  1009. {
  1010. return a.max_size();
  1011. }
  1012. template <typename SizeType, typename Alloc>
  1013. inline SizeType call_max_size(const Alloc&,
  1014. typename boost::disable_if_c<
  1015. boost::unordered::detail::has_max_size<Alloc>::value, void*>::type =
  1016. 0)
  1017. {
  1018. return (std::numeric_limits<SizeType>::max)();
  1019. }
  1020. } // namespace func.
  1021. }
  1022. }
  1023. }
  1024. namespace boost {
  1025. namespace unordered {
  1026. namespace detail {
  1027. template <typename Alloc> struct allocator_traits
  1028. {
  1029. typedef Alloc allocator_type;
  1030. typedef typename Alloc::value_type value_type;
  1031. typedef BOOST_UNORDERED_DEFAULT_TYPE(
  1032. Alloc, pointer, value_type*) pointer;
  1033. template <typename T>
  1034. struct pointer_to_other : boost::pointer_to_other<pointer, T>
  1035. {
  1036. };
  1037. typedef BOOST_UNORDERED_DEFAULT_TYPE(Alloc, const_pointer,
  1038. typename pointer_to_other<const value_type>::type) const_pointer;
  1039. // typedef BOOST_UNORDERED_DEFAULT_TYPE(Alloc, void_pointer,
  1040. // typename pointer_to_other<void>::type)
  1041. // void_pointer;
  1042. //
  1043. // typedef BOOST_UNORDERED_DEFAULT_TYPE(Alloc, const_void_pointer,
  1044. // typename pointer_to_other<const void>::type)
  1045. // const_void_pointer;
  1046. typedef BOOST_UNORDERED_DEFAULT_TYPE(
  1047. Alloc, difference_type, std::ptrdiff_t) difference_type;
  1048. typedef BOOST_UNORDERED_DEFAULT_TYPE(
  1049. Alloc, size_type, std::size_t) size_type;
  1050. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  1051. template <typename T>
  1052. using rebind_alloc = typename rebind_wrap<Alloc, T>::type;
  1053. template <typename T>
  1054. using rebind_traits =
  1055. boost::unordered::detail::allocator_traits<rebind_alloc<T> >;
  1056. #endif
  1057. static pointer allocate(Alloc& a, size_type n) { return a.allocate(n); }
  1058. // I never use this, so I'll just comment it out for now.
  1059. //
  1060. // static pointer allocate(Alloc& a, size_type n,
  1061. // const_void_pointer hint)
  1062. // { return DEFAULT_FUNC(allocate, pointer)(a, n, hint); }
  1063. static void deallocate(Alloc& a, pointer p, size_type n)
  1064. {
  1065. a.deallocate(p, n);
  1066. }
  1067. public:
  1068. #if BOOST_UNORDERED_CXX11_CONSTRUCTION
  1069. template <typename T, typename... Args>
  1070. static
  1071. typename boost::enable_if_c<boost::unordered::detail::has_construct<
  1072. Alloc, T, Args...>::value>::type
  1073. construct(Alloc& a, T* p, BOOST_FWD_REF(Args)... x)
  1074. {
  1075. a.construct(p, boost::forward<Args>(x)...);
  1076. }
  1077. template <typename T, typename... Args>
  1078. static
  1079. typename boost::disable_if_c<boost::unordered::detail::has_construct<
  1080. Alloc, T, Args...>::value>::type
  1081. construct(Alloc&, T* p, BOOST_FWD_REF(Args)... x)
  1082. {
  1083. new (static_cast<void*>(p)) T(boost::forward<Args>(x)...);
  1084. }
  1085. template <typename T>
  1086. static typename boost::enable_if_c<
  1087. boost::unordered::detail::has_destroy<Alloc, T>::value>::type
  1088. destroy(Alloc& a, T* p)
  1089. {
  1090. a.destroy(p);
  1091. }
  1092. template <typename T>
  1093. static typename boost::disable_if_c<
  1094. boost::unordered::detail::has_destroy<Alloc, T>::value>::type
  1095. destroy(Alloc&, T* p)
  1096. {
  1097. boost::unordered::detail::func::destroy(p);
  1098. }
  1099. #elif !defined(BOOST_NO_SFINAE_EXPR)
  1100. template <typename T>
  1101. static typename boost::enable_if_c<
  1102. boost::unordered::detail::has_construct<Alloc, T>::value>::type
  1103. construct(Alloc& a, T* p, T const& x)
  1104. {
  1105. a.construct(p, x);
  1106. }
  1107. template <typename T>
  1108. static typename boost::disable_if_c<
  1109. boost::unordered::detail::has_construct<Alloc, T>::value>::type
  1110. construct(Alloc&, T* p, T const& x)
  1111. {
  1112. new (static_cast<void*>(p)) T(x);
  1113. }
  1114. template <typename T>
  1115. static typename boost::enable_if_c<
  1116. boost::unordered::detail::has_destroy<Alloc, T>::value>::type
  1117. destroy(Alloc& a, T* p)
  1118. {
  1119. a.destroy(p);
  1120. }
  1121. template <typename T>
  1122. static typename boost::disable_if_c<
  1123. boost::unordered::detail::has_destroy<Alloc, T>::value>::type
  1124. destroy(Alloc&, T* p)
  1125. {
  1126. boost::unordered::detail::func::destroy(p);
  1127. }
  1128. #else
  1129. // If we don't have SFINAE expressions, only call construct for the
  1130. // copy constructor for the allocator's value_type - as that's
  1131. // the only construct method that old fashioned allocators support.
  1132. template <typename T>
  1133. static void construct(Alloc& a, T* p, T const& x,
  1134. typename boost::enable_if_c<
  1135. boost::unordered::detail::has_construct<Alloc, T>::value &&
  1136. boost::is_same<T, value_type>::value,
  1137. void*>::type = 0)
  1138. {
  1139. a.construct(p, x);
  1140. }
  1141. template <typename T>
  1142. static void construct(Alloc&, T* p, T const& x,
  1143. typename boost::disable_if_c<
  1144. boost::unordered::detail::has_construct<Alloc, T>::value &&
  1145. boost::is_same<T, value_type>::value,
  1146. void*>::type = 0)
  1147. {
  1148. new (static_cast<void*>(p)) T(x);
  1149. }
  1150. template <typename T>
  1151. static void destroy(Alloc& a, T* p,
  1152. typename boost::enable_if_c<
  1153. boost::unordered::detail::has_destroy<Alloc, T>::value &&
  1154. boost::is_same<T, value_type>::value,
  1155. void*>::type = 0)
  1156. {
  1157. a.destroy(p);
  1158. }
  1159. template <typename T>
  1160. static void destroy(Alloc&, T* p,
  1161. typename boost::disable_if_c<
  1162. boost::unordered::detail::has_destroy<Alloc, T>::value &&
  1163. boost::is_same<T, value_type>::value,
  1164. void*>::type = 0)
  1165. {
  1166. boost::unordered::detail::func::destroy(p);
  1167. }
  1168. #endif
  1169. static size_type max_size(const Alloc& a)
  1170. {
  1171. return boost::unordered::detail::func::call_max_size<size_type>(a);
  1172. }
  1173. // Allocator propagation on construction
  1174. static Alloc select_on_container_copy_construction(Alloc const& rhs)
  1175. {
  1176. return boost::unordered::detail::func::
  1177. call_select_on_container_copy_construction(rhs);
  1178. }
  1179. // Allocator propagation on assignment and swap.
  1180. // Return true if lhs is modified.
  1181. typedef BOOST_UNORDERED_DEFAULT_TYPE(Alloc,
  1182. propagate_on_container_copy_assignment,
  1183. false_type) propagate_on_container_copy_assignment;
  1184. typedef BOOST_UNORDERED_DEFAULT_TYPE(Alloc,
  1185. propagate_on_container_move_assignment,
  1186. false_type) propagate_on_container_move_assignment;
  1187. typedef BOOST_UNORDERED_DEFAULT_TYPE(Alloc, propagate_on_container_swap,
  1188. false_type) propagate_on_container_swap;
  1189. typedef BOOST_UNORDERED_DEFAULT_TYPE(Alloc, is_always_equal,
  1190. typename boost::is_empty<Alloc>::type) is_always_equal;
  1191. };
  1192. }
  1193. }
  1194. }
  1195. #undef BOOST_UNORDERED_DEFAULT_TYPE_TMPLT
  1196. #undef BOOST_UNORDERED_DEFAULT_TYPE
  1197. ////////////////////////////////////////////////////////////////////////////////
  1198. //
  1199. // std::allocator_traits
  1200. #elif BOOST_UNORDERED_USE_ALLOCATOR_TRAITS == 1
  1201. #include <memory>
  1202. namespace boost {
  1203. namespace unordered {
  1204. namespace detail {
  1205. BOOST_UNORDERED_DEFAULT_TYPE_TMPLT(is_always_equal);
  1206. template <typename Alloc>
  1207. struct allocator_traits : std::allocator_traits<Alloc>
  1208. {
  1209. // As is_always_equal was introduced in C++17, std::allocator_traits
  1210. // doesn't always have it. So use it when available, implement it
  1211. // ourselves when not. Would be simpler not to bother with
  1212. // std::allocator_traits, but I feel like I should try to use
  1213. // it where possible.
  1214. typedef BOOST_UNORDERED_DEFAULT_TYPE(std::allocator_traits<Alloc>,
  1215. is_always_equal,
  1216. BOOST_UNORDERED_DEFAULT_TYPE(Alloc, is_always_equal,
  1217. typename boost::is_empty<Alloc>::type)) is_always_equal;
  1218. };
  1219. template <typename Alloc, typename T> struct rebind_wrap
  1220. {
  1221. typedef typename std::allocator_traits<Alloc>::template rebind_alloc<T>
  1222. type;
  1223. };
  1224. }
  1225. }
  1226. }
  1227. ////////////////////////////////////////////////////////////////////////////////
  1228. //
  1229. // boost::container::allocator_traits
  1230. #elif BOOST_UNORDERED_USE_ALLOCATOR_TRAITS == 2
  1231. #include <boost/container/allocator_traits.hpp>
  1232. namespace boost {
  1233. namespace unordered {
  1234. namespace detail {
  1235. template <typename Alloc>
  1236. struct allocator_traits : boost::container::allocator_traits<Alloc>
  1237. {
  1238. };
  1239. template <typename Alloc, typename T>
  1240. struct rebind_wrap : boost::container::allocator_traits<
  1241. Alloc>::template portable_rebind_alloc<T>
  1242. {
  1243. };
  1244. }
  1245. }
  1246. }
  1247. #else
  1248. #error "Invalid BOOST_UNORDERED_USE_ALLOCATOR_TRAITS value."
  1249. #endif
  1250. ////////////////////////////////////////////////////////////////////////////
  1251. // Functions used to construct nodes. Emulates variadic construction,
  1252. // piecewise construction etc.
  1253. ////////////////////////////////////////////////////////////////////////////
  1254. // construct_value
  1255. //
  1256. // Only use allocator_traits::construct, allocator_traits::destroy when full
  1257. // C++11 support is available.
  1258. #if BOOST_UNORDERED_CXX11_CONSTRUCTION
  1259. #define BOOST_UNORDERED_CALL_CONSTRUCT1(Traits, alloc, address, a0) \
  1260. Traits::construct(alloc, address, a0)
  1261. #define BOOST_UNORDERED_CALL_DESTROY(Traits, alloc, x) Traits::destroy(alloc, x)
  1262. #elif !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  1263. namespace boost {
  1264. namespace unordered {
  1265. namespace detail {
  1266. namespace func {
  1267. template <typename T, typename... Args>
  1268. inline void construct_value(T* address, BOOST_FWD_REF(Args)... args)
  1269. {
  1270. new ((void*)address) T(boost::forward<Args>(args)...);
  1271. }
  1272. }
  1273. }
  1274. }
  1275. }
  1276. #define BOOST_UNORDERED_CALL_CONSTRUCT1(Traits, alloc, address, a0) \
  1277. boost::unordered::detail::func::construct_value(address, a0)
  1278. #define BOOST_UNORDERED_CALL_DESTROY(Traits, alloc, x) \
  1279. boost::unordered::detail::func::destroy(x)
  1280. #else
  1281. namespace boost {
  1282. namespace unordered {
  1283. namespace detail {
  1284. namespace func {
  1285. template <typename T> inline void construct_value(T* address)
  1286. {
  1287. new ((void*)address) T();
  1288. }
  1289. template <typename T, typename A0>
  1290. inline void construct_value(T* address, BOOST_FWD_REF(A0) a0)
  1291. {
  1292. new ((void*)address) T(boost::forward<A0>(a0));
  1293. }
  1294. }
  1295. }
  1296. }
  1297. }
  1298. #define BOOST_UNORDERED_CALL_CONSTRUCT1(Traits, alloc, address, a0) \
  1299. boost::unordered::detail::func::construct_value(address, a0)
  1300. #define BOOST_UNORDERED_CALL_DESTROY(Traits, alloc, x) \
  1301. boost::unordered::detail::func::destroy(x)
  1302. #endif
  1303. ////////////////////////////////////////////////////////////////////////////
  1304. // Construct from tuple
  1305. //
  1306. // Used to emulate piecewise construction.
  1307. #define BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(z, n, namespace_) \
  1308. template <typename Alloc, typename T, \
  1309. BOOST_PP_ENUM_PARAMS_Z(z, n, typename A)> \
  1310. void construct_from_tuple(Alloc&, T* ptr, \
  1311. namespace_::tuple<BOOST_PP_ENUM_PARAMS_Z(z, n, A)> const& x) \
  1312. { \
  1313. new ((void*)ptr) \
  1314. T(BOOST_PP_ENUM_##z(n, BOOST_UNORDERED_GET_TUPLE_ARG, namespace_)); \
  1315. }
  1316. #define BOOST_UNORDERED_GET_TUPLE_ARG(z, n, namespace_) namespace_::get<n>(x)
  1317. // construct_from_tuple for boost::tuple
  1318. // The workaround for old Sun compilers comes later in the file.
  1319. #if !BOOST_UNORDERED_SUN_WORKAROUNDS1
  1320. namespace boost {
  1321. namespace unordered {
  1322. namespace detail {
  1323. namespace func {
  1324. template <typename Alloc, typename T>
  1325. void construct_from_tuple(Alloc&, T* ptr, boost::tuple<>)
  1326. {
  1327. new ((void*)ptr) T();
  1328. }
  1329. BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(1, 1, boost)
  1330. BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(1, 2, boost)
  1331. BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(1, 3, boost)
  1332. BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(1, 4, boost)
  1333. BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(1, 5, boost)
  1334. BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(1, 6, boost)
  1335. BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(1, 7, boost)
  1336. BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(1, 8, boost)
  1337. BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(1, 9, boost)
  1338. BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(1, 10, boost)
  1339. }
  1340. }
  1341. }
  1342. }
  1343. #endif
  1344. // construct_from_tuple for std::tuple
  1345. #if !BOOST_UNORDERED_CXX11_CONSTRUCTION && BOOST_UNORDERED_TUPLE_ARGS
  1346. namespace boost {
  1347. namespace unordered {
  1348. namespace detail {
  1349. namespace func {
  1350. template <typename Alloc, typename T>
  1351. void construct_from_tuple(Alloc&, T* ptr, std::tuple<>)
  1352. {
  1353. new ((void*)ptr) T();
  1354. }
  1355. BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(1, 1, std)
  1356. BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(1, 2, std)
  1357. BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(1, 3, std)
  1358. BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(1, 4, std)
  1359. BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(1, 5, std)
  1360. #if BOOST_UNORDERED_TUPLE_ARGS >= 6
  1361. BOOST_PP_REPEAT_FROM_TO(6, BOOST_PP_INC(BOOST_UNORDERED_TUPLE_ARGS),
  1362. BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE, std)
  1363. #endif
  1364. }
  1365. }
  1366. }
  1367. }
  1368. #endif
  1369. #undef BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE
  1370. #undef BOOST_UNORDERED_GET_TUPLE_ARG
  1371. // construct_from_tuple for boost::tuple on old versions of sunpro.
  1372. //
  1373. // Old versions of Sun C++ had problems with template overloads of
  1374. // boost::tuple, so to fix it I added a distinct type for each length to
  1375. // the overloads. That means there's no possible ambiguity between the
  1376. // different overloads, so that the compiler doesn't get confused
  1377. #if BOOST_UNORDERED_SUN_WORKAROUNDS1
  1378. #define BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(z, n, namespace_) \
  1379. template <typename Alloc, typename T, \
  1380. BOOST_PP_ENUM_PARAMS_Z(z, n, typename A)> \
  1381. void construct_from_tuple_impl(boost::unordered::detail::func::length<n>, \
  1382. Alloc&, T* ptr, \
  1383. namespace_::tuple<BOOST_PP_ENUM_PARAMS_Z(z, n, A)> const& x) \
  1384. { \
  1385. new ((void*)ptr) \
  1386. T(BOOST_PP_ENUM_##z(n, BOOST_UNORDERED_GET_TUPLE_ARG, namespace_)); \
  1387. }
  1388. #define BOOST_UNORDERED_GET_TUPLE_ARG(z, n, namespace_) namespace_::get<n>(x)
  1389. namespace boost {
  1390. namespace unordered {
  1391. namespace detail {
  1392. namespace func {
  1393. template <int N> struct length
  1394. {
  1395. };
  1396. template <typename Alloc, typename T>
  1397. void construct_from_tuple_impl(
  1398. boost::unordered::detail::func::length<0>, Alloc&, T* ptr,
  1399. boost::tuple<>)
  1400. {
  1401. new ((void*)ptr) T();
  1402. }
  1403. BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(1, 1, boost)
  1404. BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(1, 2, boost)
  1405. BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(1, 3, boost)
  1406. BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(1, 4, boost)
  1407. BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(1, 5, boost)
  1408. BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(1, 6, boost)
  1409. BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(1, 7, boost)
  1410. BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(1, 8, boost)
  1411. BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(1, 9, boost)
  1412. BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(1, 10, boost)
  1413. template <typename Alloc, typename T, typename Tuple>
  1414. void construct_from_tuple(Alloc& alloc, T* ptr, Tuple const& x)
  1415. {
  1416. construct_from_tuple_impl(boost::unordered::detail::func::length<
  1417. boost::tuples::length<Tuple>::value>(),
  1418. alloc, ptr, x);
  1419. }
  1420. }
  1421. }
  1422. }
  1423. }
  1424. #undef BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE
  1425. #undef BOOST_UNORDERED_GET_TUPLE_ARG
  1426. #endif
  1427. namespace boost {
  1428. namespace unordered {
  1429. namespace detail {
  1430. namespace func {
  1431. ////////////////////////////////////////////////////////////////////////
  1432. // Trait to check for piecewise construction.
  1433. template <typename A0> struct use_piecewise
  1434. {
  1435. static choice1::type test(
  1436. choice1, boost::unordered::piecewise_construct_t);
  1437. static choice2::type test(choice2, ...);
  1438. enum
  1439. {
  1440. value = sizeof(choice1::type) ==
  1441. sizeof(test(choose(), boost::unordered::detail::make<A0>()))
  1442. };
  1443. };
  1444. #if BOOST_UNORDERED_CXX11_CONSTRUCTION
  1445. ////////////////////////////////////////////////////////////////////////
  1446. // Construct from variadic parameters
  1447. template <typename Alloc, typename T, typename... Args>
  1448. inline void construct_from_args(
  1449. Alloc& alloc, T* address, BOOST_FWD_REF(Args)... args)
  1450. {
  1451. boost::unordered::detail::allocator_traits<Alloc>::construct(
  1452. alloc, address, boost::forward<Args>(args)...);
  1453. }
  1454. // For backwards compatibility, implement a special case for
  1455. // piecewise_construct with boost::tuple
  1456. template <typename A0> struct detect_boost_tuple
  1457. {
  1458. template <typename T0, typename T1, typename T2, typename T3,
  1459. typename T4, typename T5, typename T6, typename T7, typename T8,
  1460. typename T9>
  1461. static choice1::type test(choice1,
  1462. boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> const&);
  1463. static choice2::type test(choice2, ...);
  1464. enum
  1465. {
  1466. value = sizeof(choice1::type) ==
  1467. sizeof(test(choose(), boost::unordered::detail::make<A0>()))
  1468. };
  1469. };
  1470. // Special case for piecewise_construct
  1471. template <typename Alloc, typename A, typename B, typename A0,
  1472. typename A1, typename A2>
  1473. inline typename boost::enable_if_c<use_piecewise<A0>::value &&
  1474. detect_boost_tuple<A1>::value &&
  1475. detect_boost_tuple<A2>::value,
  1476. void>::type
  1477. construct_from_args(Alloc& alloc, std::pair<A, B>* address,
  1478. BOOST_FWD_REF(A0), BOOST_FWD_REF(A1) a1, BOOST_FWD_REF(A2) a2)
  1479. {
  1480. boost::unordered::detail::func::construct_from_tuple(
  1481. alloc, boost::addressof(address->first), boost::forward<A1>(a1));
  1482. BOOST_TRY
  1483. {
  1484. boost::unordered::detail::func::construct_from_tuple(
  1485. alloc, boost::addressof(address->second), boost::forward<A2>(a2));
  1486. }
  1487. BOOST_CATCH(...)
  1488. {
  1489. boost::unordered::detail::func::destroy(
  1490. boost::addressof(address->first));
  1491. BOOST_RETHROW
  1492. }
  1493. BOOST_CATCH_END
  1494. }
  1495. #elif !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  1496. ////////////////////////////////////////////////////////////////////////
  1497. // Construct from variadic parameters
  1498. template <typename Alloc, typename T, typename... Args>
  1499. inline void construct_from_args(
  1500. Alloc&, T* address, BOOST_FWD_REF(Args)... args)
  1501. {
  1502. new ((void*)address) T(boost::forward<Args>(args)...);
  1503. }
  1504. // Special case for piecewise_construct
  1505. template <typename Alloc, typename A, typename B, typename A0,
  1506. typename A1, typename A2>
  1507. inline typename enable_if<use_piecewise<A0>, void>::type
  1508. construct_from_args(Alloc& alloc, std::pair<A, B>* address,
  1509. BOOST_FWD_REF(A0), BOOST_FWD_REF(A1) a1, BOOST_FWD_REF(A2) a2)
  1510. {
  1511. boost::unordered::detail::func::construct_from_tuple(
  1512. alloc, boost::addressof(address->first), boost::forward<A1>(a1));
  1513. BOOST_TRY
  1514. {
  1515. boost::unordered::detail::func::construct_from_tuple(
  1516. alloc, boost::addressof(address->second), boost::forward<A2>(a2));
  1517. }
  1518. BOOST_CATCH(...)
  1519. {
  1520. boost::unordered::detail::func::destroy(
  1521. boost::addressof(address->first));
  1522. BOOST_RETHROW
  1523. }
  1524. BOOST_CATCH_END
  1525. }
  1526. #else // BOOST_NO_CXX11_VARIADIC_TEMPLATES
  1527. ////////////////////////////////////////////////////////////////////////
  1528. // Construct from emplace_args
  1529. // Explicitly write out first three overloads for the sake of sane
  1530. // error messages.
  1531. template <typename Alloc, typename T, typename A0>
  1532. inline void construct_from_args(
  1533. Alloc&, T* address, emplace_args1<A0> const& args)
  1534. {
  1535. new ((void*)address) T(boost::forward<A0>(args.a0));
  1536. }
  1537. template <typename Alloc, typename T, typename A0, typename A1>
  1538. inline void construct_from_args(
  1539. Alloc&, T* address, emplace_args2<A0, A1> const& args)
  1540. {
  1541. new ((void*)address)
  1542. T(boost::forward<A0>(args.a0), boost::forward<A1>(args.a1));
  1543. }
  1544. template <typename Alloc, typename T, typename A0, typename A1,
  1545. typename A2>
  1546. inline void construct_from_args(
  1547. Alloc&, T* address, emplace_args3<A0, A1, A2> const& args)
  1548. {
  1549. new ((void*)address) T(boost::forward<A0>(args.a0),
  1550. boost::forward<A1>(args.a1), boost::forward<A2>(args.a2));
  1551. }
  1552. // Use a macro for the rest.
  1553. #define BOOST_UNORDERED_CONSTRUCT_IMPL(z, num_params, _) \
  1554. template <typename Alloc, typename T, \
  1555. BOOST_PP_ENUM_PARAMS_Z(z, num_params, typename A)> \
  1556. inline void construct_from_args(Alloc&, T* address, \
  1557. boost::unordered::detail::BOOST_PP_CAT(emplace_args, num_params) < \
  1558. BOOST_PP_ENUM_PARAMS_Z(z, num_params, A) > const& args) \
  1559. { \
  1560. new ((void*)address) \
  1561. T(BOOST_PP_ENUM_##z(num_params, BOOST_UNORDERED_CALL_FORWARD, args.a)); \
  1562. }
  1563. BOOST_UNORDERED_CONSTRUCT_IMPL(1, 4, _)
  1564. BOOST_UNORDERED_CONSTRUCT_IMPL(1, 5, _)
  1565. BOOST_UNORDERED_CONSTRUCT_IMPL(1, 6, _)
  1566. BOOST_UNORDERED_CONSTRUCT_IMPL(1, 7, _)
  1567. BOOST_UNORDERED_CONSTRUCT_IMPL(1, 8, _)
  1568. BOOST_UNORDERED_CONSTRUCT_IMPL(1, 9, _)
  1569. BOOST_PP_REPEAT_FROM_TO(10, BOOST_PP_INC(BOOST_UNORDERED_EMPLACE_LIMIT),
  1570. BOOST_UNORDERED_CONSTRUCT_IMPL, _)
  1571. #undef BOOST_UNORDERED_CONSTRUCT_IMPL
  1572. // Construct with piecewise_construct
  1573. template <typename Alloc, typename A, typename B, typename A0,
  1574. typename A1, typename A2>
  1575. inline void construct_from_args(Alloc& alloc, std::pair<A, B>* address,
  1576. boost::unordered::detail::emplace_args3<A0, A1, A2> const& args,
  1577. typename enable_if<use_piecewise<A0>, void*>::type = 0)
  1578. {
  1579. boost::unordered::detail::func::construct_from_tuple(
  1580. alloc, boost::addressof(address->first), args.a1);
  1581. BOOST_TRY
  1582. {
  1583. boost::unordered::detail::func::construct_from_tuple(
  1584. alloc, boost::addressof(address->second), args.a2);
  1585. }
  1586. BOOST_CATCH(...)
  1587. {
  1588. boost::unordered::detail::func::destroy(
  1589. boost::addressof(address->first));
  1590. BOOST_RETHROW
  1591. }
  1592. BOOST_CATCH_END
  1593. }
  1594. #endif // BOOST_NO_CXX11_VARIADIC_TEMPLATES
  1595. }
  1596. }
  1597. }
  1598. }
  1599. namespace boost {
  1600. namespace unordered {
  1601. namespace detail {
  1602. ///////////////////////////////////////////////////////////////////
  1603. //
  1604. // Node construction
  1605. template <typename NodeAlloc> struct node_constructor
  1606. {
  1607. typedef NodeAlloc node_allocator;
  1608. typedef boost::unordered::detail::allocator_traits<NodeAlloc>
  1609. node_allocator_traits;
  1610. typedef typename node_allocator_traits::value_type node;
  1611. typedef typename node_allocator_traits::pointer node_pointer;
  1612. typedef typename node::value_type value_type;
  1613. node_allocator& alloc_;
  1614. node_pointer node_;
  1615. node_constructor(node_allocator& n) : alloc_(n), node_() {}
  1616. ~node_constructor();
  1617. void create_node();
  1618. // no throw
  1619. node_pointer release()
  1620. {
  1621. BOOST_ASSERT(node_);
  1622. node_pointer p = node_;
  1623. node_ = node_pointer();
  1624. return p;
  1625. }
  1626. void reclaim(node_pointer p)
  1627. {
  1628. BOOST_ASSERT(!node_);
  1629. node_ = p;
  1630. BOOST_UNORDERED_CALL_DESTROY(
  1631. node_allocator_traits, alloc_, node_->value_ptr());
  1632. }
  1633. private:
  1634. node_constructor(node_constructor const&);
  1635. node_constructor& operator=(node_constructor const&);
  1636. };
  1637. template <typename Alloc> node_constructor<Alloc>::~node_constructor()
  1638. {
  1639. if (node_) {
  1640. boost::unordered::detail::func::destroy(boost::to_address(node_));
  1641. node_allocator_traits::deallocate(alloc_, node_, 1);
  1642. }
  1643. }
  1644. template <typename Alloc> void node_constructor<Alloc>::create_node()
  1645. {
  1646. BOOST_ASSERT(!node_);
  1647. node_ = node_allocator_traits::allocate(alloc_, 1);
  1648. new ((void*)boost::to_address(node_)) node();
  1649. }
  1650. template <typename NodeAlloc> struct node_tmp
  1651. {
  1652. typedef boost::unordered::detail::allocator_traits<NodeAlloc>
  1653. node_allocator_traits;
  1654. typedef typename node_allocator_traits::pointer node_pointer;
  1655. typedef typename node_allocator_traits::value_type node;
  1656. NodeAlloc& alloc_;
  1657. node_pointer node_;
  1658. explicit node_tmp(node_pointer n, NodeAlloc& a) : alloc_(a), node_(n) {}
  1659. ~node_tmp();
  1660. // no throw
  1661. node_pointer release()
  1662. {
  1663. node_pointer p = node_;
  1664. node_ = node_pointer();
  1665. return p;
  1666. }
  1667. };
  1668. template <typename Alloc> node_tmp<Alloc>::~node_tmp()
  1669. {
  1670. if (node_) {
  1671. BOOST_UNORDERED_CALL_DESTROY(
  1672. node_allocator_traits, alloc_, node_->value_ptr());
  1673. boost::unordered::detail::func::destroy(boost::to_address(node_));
  1674. node_allocator_traits::deallocate(alloc_, node_, 1);
  1675. }
  1676. }
  1677. }
  1678. }
  1679. }
  1680. namespace boost {
  1681. namespace unordered {
  1682. namespace detail {
  1683. namespace func {
  1684. // Some nicer construct_node functions, might try to
  1685. // improve implementation later.
  1686. template <typename Alloc, BOOST_UNORDERED_EMPLACE_TEMPLATE>
  1687. inline
  1688. typename boost::unordered::detail::allocator_traits<Alloc>::pointer
  1689. construct_node_from_args(Alloc& alloc, BOOST_UNORDERED_EMPLACE_ARGS)
  1690. {
  1691. node_constructor<Alloc> a(alloc);
  1692. a.create_node();
  1693. construct_from_args(
  1694. alloc, a.node_->value_ptr(), BOOST_UNORDERED_EMPLACE_FORWARD);
  1695. return a.release();
  1696. }
  1697. template <typename Alloc, typename U>
  1698. inline
  1699. typename boost::unordered::detail::allocator_traits<Alloc>::pointer
  1700. construct_node(Alloc& alloc, BOOST_FWD_REF(U) x)
  1701. {
  1702. node_constructor<Alloc> a(alloc);
  1703. a.create_node();
  1704. BOOST_UNORDERED_CALL_CONSTRUCT1(
  1705. boost::unordered::detail::allocator_traits<Alloc>, alloc,
  1706. a.node_->value_ptr(), boost::forward<U>(x));
  1707. return a.release();
  1708. }
  1709. #if BOOST_UNORDERED_CXX11_CONSTRUCTION
  1710. template <typename Alloc, typename Key>
  1711. inline
  1712. typename boost::unordered::detail::allocator_traits<Alloc>::pointer
  1713. construct_node_pair(Alloc& alloc, BOOST_FWD_REF(Key) k)
  1714. {
  1715. node_constructor<Alloc> a(alloc);
  1716. a.create_node();
  1717. boost::unordered::detail::allocator_traits<Alloc>::construct(alloc,
  1718. a.node_->value_ptr(), std::piecewise_construct,
  1719. std::forward_as_tuple(boost::forward<Key>(k)),
  1720. std::forward_as_tuple());
  1721. return a.release();
  1722. }
  1723. template <typename Alloc, typename Key, typename Mapped>
  1724. inline
  1725. typename boost::unordered::detail::allocator_traits<Alloc>::pointer
  1726. construct_node_pair(
  1727. Alloc& alloc, BOOST_FWD_REF(Key) k, BOOST_FWD_REF(Mapped) m)
  1728. {
  1729. node_constructor<Alloc> a(alloc);
  1730. a.create_node();
  1731. boost::unordered::detail::allocator_traits<Alloc>::construct(alloc,
  1732. a.node_->value_ptr(), std::piecewise_construct,
  1733. std::forward_as_tuple(boost::forward<Key>(k)),
  1734. std::forward_as_tuple(boost::forward<Mapped>(m)));
  1735. return a.release();
  1736. }
  1737. template <typename Alloc, typename Key, typename... Args>
  1738. inline
  1739. typename boost::unordered::detail::allocator_traits<Alloc>::pointer
  1740. construct_node_pair_from_args(
  1741. Alloc& alloc, BOOST_FWD_REF(Key) k, BOOST_FWD_REF(Args)... args)
  1742. {
  1743. node_constructor<Alloc> a(alloc);
  1744. a.create_node();
  1745. #if !(BOOST_COMP_CLANG && BOOST_COMP_CLANG < BOOST_VERSION_NUMBER(3, 8, 0) && \
  1746. defined(BOOST_LIBSTDCXX11))
  1747. boost::unordered::detail::allocator_traits<Alloc>::construct(alloc,
  1748. a.node_->value_ptr(), std::piecewise_construct,
  1749. std::forward_as_tuple(boost::forward<Key>(k)),
  1750. std::forward_as_tuple(boost::forward<Args>(args)...));
  1751. #else
  1752. // It doesn't seem to be possible to construct a tuple with 3 variadic
  1753. // rvalue reference members when using older versions of clang with
  1754. // libstdc++, so just use std::make_tuple instead of
  1755. // std::forward_as_tuple.
  1756. boost::unordered::detail::allocator_traits<Alloc>::construct(alloc,
  1757. a.node_->value_ptr(), std::piecewise_construct,
  1758. std::forward_as_tuple(boost::forward<Key>(k)),
  1759. std::make_tuple(boost::forward<Args>(args)...));
  1760. #endif
  1761. return a.release();
  1762. }
  1763. #else
  1764. template <typename Alloc, typename Key>
  1765. inline
  1766. typename boost::unordered::detail::allocator_traits<Alloc>::pointer
  1767. construct_node_pair(Alloc& alloc, BOOST_FWD_REF(Key) k)
  1768. {
  1769. node_constructor<Alloc> a(alloc);
  1770. a.create_node();
  1771. boost::unordered::detail::func::construct_value(
  1772. boost::addressof(a.node_->value_ptr()->first),
  1773. boost::forward<Key>(k));
  1774. BOOST_TRY
  1775. {
  1776. boost::unordered::detail::func::construct_value(
  1777. boost::addressof(a.node_->value_ptr()->second));
  1778. }
  1779. BOOST_CATCH(...)
  1780. {
  1781. boost::unordered::detail::func::destroy(
  1782. boost::addressof(a.node_->value_ptr()->first));
  1783. BOOST_RETHROW
  1784. }
  1785. BOOST_CATCH_END
  1786. return a.release();
  1787. }
  1788. template <typename Alloc, typename Key, typename Mapped>
  1789. inline
  1790. typename boost::unordered::detail::allocator_traits<Alloc>::pointer
  1791. construct_node_pair(
  1792. Alloc& alloc, BOOST_FWD_REF(Key) k, BOOST_FWD_REF(Mapped) m)
  1793. {
  1794. node_constructor<Alloc> a(alloc);
  1795. a.create_node();
  1796. boost::unordered::detail::func::construct_value(
  1797. boost::addressof(a.node_->value_ptr()->first),
  1798. boost::forward<Key>(k));
  1799. BOOST_TRY
  1800. {
  1801. boost::unordered::detail::func::construct_value(
  1802. boost::addressof(a.node_->value_ptr()->second),
  1803. boost::forward<Mapped>(m));
  1804. }
  1805. BOOST_CATCH(...)
  1806. {
  1807. boost::unordered::detail::func::destroy(
  1808. boost::addressof(a.node_->value_ptr()->first));
  1809. BOOST_RETHROW
  1810. }
  1811. BOOST_CATCH_END
  1812. return a.release();
  1813. }
  1814. template <typename Alloc, typename Key,
  1815. BOOST_UNORDERED_EMPLACE_TEMPLATE>
  1816. inline
  1817. typename boost::unordered::detail::allocator_traits<Alloc>::pointer
  1818. construct_node_pair_from_args(
  1819. Alloc& alloc, BOOST_FWD_REF(Key) k, BOOST_UNORDERED_EMPLACE_ARGS)
  1820. {
  1821. node_constructor<Alloc> a(alloc);
  1822. a.create_node();
  1823. boost::unordered::detail::func::construct_value(
  1824. boost::addressof(a.node_->value_ptr()->first),
  1825. boost::forward<Key>(k));
  1826. BOOST_TRY
  1827. {
  1828. boost::unordered::detail::func::construct_from_args(alloc,
  1829. boost::addressof(a.node_->value_ptr()->second),
  1830. BOOST_UNORDERED_EMPLACE_FORWARD);
  1831. }
  1832. BOOST_CATCH(...)
  1833. {
  1834. boost::unordered::detail::func::destroy(
  1835. boost::addressof(a.node_->value_ptr()->first));
  1836. BOOST_RETHROW
  1837. }
  1838. BOOST_CATCH_END
  1839. return a.release();
  1840. }
  1841. #endif
  1842. }
  1843. }
  1844. }
  1845. }
  1846. #if defined(BOOST_MSVC)
  1847. #pragma warning(pop)
  1848. #endif
  1849. // The 'iterator_detail' namespace was a misguided attempt at avoiding ADL
  1850. // in the detail namespace. It didn't work because the template parameters
  1851. // were in detail. I'm not changing it at the moment to be safe. I might
  1852. // do in the future if I change the iterator types.
  1853. namespace boost {
  1854. namespace unordered {
  1855. namespace iterator_detail {
  1856. //////////////////////////////////////////////////////////////////////////
  1857. // Iterators
  1858. //
  1859. // all no throw
  1860. template <typename Node> struct l_iterator
  1861. {
  1862. #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  1863. template <typename Node2>
  1864. friend struct boost::unordered::iterator_detail::cl_iterator;
  1865. private:
  1866. #endif
  1867. typedef typename Node::node_pointer node_pointer;
  1868. node_pointer ptr_;
  1869. std::size_t bucket_;
  1870. std::size_t bucket_count_;
  1871. public:
  1872. typedef typename Node::value_type element_type;
  1873. typedef typename Node::value_type value_type;
  1874. typedef value_type* pointer;
  1875. typedef value_type& reference;
  1876. typedef std::ptrdiff_t difference_type;
  1877. typedef std::forward_iterator_tag iterator_category;
  1878. l_iterator() BOOST_NOEXCEPT : ptr_() {}
  1879. l_iterator(node_pointer n, std::size_t b, std::size_t c) BOOST_NOEXCEPT
  1880. : ptr_(n),
  1881. bucket_(b),
  1882. bucket_count_(c)
  1883. {
  1884. }
  1885. value_type& operator*() const { return ptr_->value(); }
  1886. value_type* operator->() const { return ptr_->value_ptr(); }
  1887. l_iterator& operator++()
  1888. {
  1889. ptr_ = static_cast<node_pointer>(ptr_->next_);
  1890. if (ptr_ && ptr_->get_bucket() != bucket_)
  1891. ptr_ = node_pointer();
  1892. return *this;
  1893. }
  1894. l_iterator operator++(int)
  1895. {
  1896. l_iterator tmp(*this);
  1897. ++(*this);
  1898. return tmp;
  1899. }
  1900. bool operator==(l_iterator x) const BOOST_NOEXCEPT
  1901. {
  1902. return ptr_ == x.ptr_;
  1903. }
  1904. bool operator!=(l_iterator x) const BOOST_NOEXCEPT
  1905. {
  1906. return ptr_ != x.ptr_;
  1907. }
  1908. };
  1909. template <typename Node> struct cl_iterator
  1910. {
  1911. friend struct boost::unordered::iterator_detail::l_iterator<Node>;
  1912. private:
  1913. typedef typename Node::node_pointer node_pointer;
  1914. node_pointer ptr_;
  1915. std::size_t bucket_;
  1916. std::size_t bucket_count_;
  1917. public:
  1918. typedef typename Node::value_type const element_type;
  1919. typedef typename Node::value_type value_type;
  1920. typedef value_type const* pointer;
  1921. typedef value_type const& reference;
  1922. typedef std::ptrdiff_t difference_type;
  1923. typedef std::forward_iterator_tag iterator_category;
  1924. cl_iterator() BOOST_NOEXCEPT : ptr_() {}
  1925. cl_iterator(node_pointer n, std::size_t b, std::size_t c) BOOST_NOEXCEPT
  1926. : ptr_(n),
  1927. bucket_(b),
  1928. bucket_count_(c)
  1929. {
  1930. }
  1931. cl_iterator(
  1932. boost::unordered::iterator_detail::l_iterator<Node> const& x)
  1933. BOOST_NOEXCEPT : ptr_(x.ptr_),
  1934. bucket_(x.bucket_),
  1935. bucket_count_(x.bucket_count_)
  1936. {
  1937. }
  1938. value_type const& operator*() const { return ptr_->value(); }
  1939. value_type const* operator->() const { return ptr_->value_ptr(); }
  1940. cl_iterator& operator++()
  1941. {
  1942. ptr_ = static_cast<node_pointer>(ptr_->next_);
  1943. if (ptr_ && ptr_->get_bucket() != bucket_)
  1944. ptr_ = node_pointer();
  1945. return *this;
  1946. }
  1947. cl_iterator operator++(int)
  1948. {
  1949. cl_iterator tmp(*this);
  1950. ++(*this);
  1951. return tmp;
  1952. }
  1953. friend bool operator==(
  1954. cl_iterator const& x, cl_iterator const& y) BOOST_NOEXCEPT
  1955. {
  1956. return x.ptr_ == y.ptr_;
  1957. }
  1958. friend bool operator!=(
  1959. cl_iterator const& x, cl_iterator const& y) BOOST_NOEXCEPT
  1960. {
  1961. return x.ptr_ != y.ptr_;
  1962. }
  1963. };
  1964. template <typename Node> struct iterator
  1965. {
  1966. #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  1967. template <typename>
  1968. friend struct boost::unordered::iterator_detail::c_iterator;
  1969. template <typename> friend struct boost::unordered::detail::table;
  1970. private:
  1971. #endif
  1972. typedef typename Node::node_pointer node_pointer;
  1973. node_pointer node_;
  1974. public:
  1975. typedef typename Node::value_type element_type;
  1976. typedef typename Node::value_type value_type;
  1977. typedef value_type* pointer;
  1978. typedef value_type& reference;
  1979. typedef std::ptrdiff_t difference_type;
  1980. typedef std::forward_iterator_tag iterator_category;
  1981. iterator() BOOST_NOEXCEPT : node_() {}
  1982. explicit iterator(typename Node::link_pointer x) BOOST_NOEXCEPT
  1983. : node_(static_cast<node_pointer>(x))
  1984. {
  1985. }
  1986. value_type& operator*() const { return node_->value(); }
  1987. value_type* operator->() const { return node_->value_ptr(); }
  1988. iterator& operator++()
  1989. {
  1990. node_ = static_cast<node_pointer>(node_->next_);
  1991. return *this;
  1992. }
  1993. iterator operator++(int)
  1994. {
  1995. iterator tmp(node_);
  1996. node_ = static_cast<node_pointer>(node_->next_);
  1997. return tmp;
  1998. }
  1999. bool operator==(iterator const& x) const BOOST_NOEXCEPT
  2000. {
  2001. return node_ == x.node_;
  2002. }
  2003. bool operator!=(iterator const& x) const BOOST_NOEXCEPT
  2004. {
  2005. return node_ != x.node_;
  2006. }
  2007. };
  2008. template <typename Node> struct c_iterator
  2009. {
  2010. friend struct boost::unordered::iterator_detail::iterator<Node>;
  2011. #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  2012. template <typename> friend struct boost::unordered::detail::table;
  2013. private:
  2014. #endif
  2015. typedef typename Node::node_pointer node_pointer;
  2016. typedef boost::unordered::iterator_detail::iterator<Node> n_iterator;
  2017. node_pointer node_;
  2018. public:
  2019. typedef typename Node::value_type const element_type;
  2020. typedef typename Node::value_type value_type;
  2021. typedef value_type const* pointer;
  2022. typedef value_type const& reference;
  2023. typedef std::ptrdiff_t difference_type;
  2024. typedef std::forward_iterator_tag iterator_category;
  2025. c_iterator() BOOST_NOEXCEPT : node_() {}
  2026. explicit c_iterator(typename Node::link_pointer x) BOOST_NOEXCEPT
  2027. : node_(static_cast<node_pointer>(x))
  2028. {
  2029. }
  2030. c_iterator(n_iterator const& x) BOOST_NOEXCEPT : node_(x.node_) {}
  2031. value_type const& operator*() const { return node_->value(); }
  2032. value_type const* operator->() const { return node_->value_ptr(); }
  2033. c_iterator& operator++()
  2034. {
  2035. node_ = static_cast<node_pointer>(node_->next_);
  2036. return *this;
  2037. }
  2038. c_iterator operator++(int)
  2039. {
  2040. c_iterator tmp(node_);
  2041. node_ = static_cast<node_pointer>(node_->next_);
  2042. return tmp;
  2043. }
  2044. friend bool operator==(
  2045. c_iterator const& x, c_iterator const& y) BOOST_NOEXCEPT
  2046. {
  2047. return x.node_ == y.node_;
  2048. }
  2049. friend bool operator!=(
  2050. c_iterator const& x, c_iterator const& y) BOOST_NOEXCEPT
  2051. {
  2052. return x.node_ != y.node_;
  2053. }
  2054. };
  2055. }
  2056. }
  2057. }
  2058. namespace boost {
  2059. namespace unordered {
  2060. namespace detail {
  2061. ///////////////////////////////////////////////////////////////////
  2062. //
  2063. // Node Holder
  2064. //
  2065. // Temporary store for nodes. Deletes any that aren't used.
  2066. template <typename NodeAlloc> struct node_holder
  2067. {
  2068. private:
  2069. typedef NodeAlloc node_allocator;
  2070. typedef boost::unordered::detail::allocator_traits<NodeAlloc>
  2071. node_allocator_traits;
  2072. typedef typename node_allocator_traits::value_type node;
  2073. typedef typename node_allocator_traits::pointer node_pointer;
  2074. typedef typename node::value_type value_type;
  2075. typedef typename node::link_pointer link_pointer;
  2076. typedef boost::unordered::iterator_detail::iterator<node> iterator;
  2077. node_constructor<NodeAlloc> constructor_;
  2078. node_pointer nodes_;
  2079. public:
  2080. template <typename Table>
  2081. explicit node_holder(Table& b) : constructor_(b.node_alloc()), nodes_()
  2082. {
  2083. if (b.size_) {
  2084. typename Table::link_pointer prev = b.get_previous_start();
  2085. nodes_ = static_cast<node_pointer>(prev->next_);
  2086. prev->next_ = link_pointer();
  2087. b.size_ = 0;
  2088. }
  2089. }
  2090. ~node_holder();
  2091. node_pointer pop_node()
  2092. {
  2093. node_pointer n = nodes_;
  2094. nodes_ = static_cast<node_pointer>(nodes_->next_);
  2095. n->next_ = link_pointer();
  2096. return n;
  2097. }
  2098. template <typename T> inline node_pointer copy_of(T const& v)
  2099. {
  2100. if (nodes_) {
  2101. constructor_.reclaim(pop_node());
  2102. } else {
  2103. constructor_.create_node();
  2104. }
  2105. BOOST_UNORDERED_CALL_CONSTRUCT1(node_allocator_traits,
  2106. constructor_.alloc_, constructor_.node_->value_ptr(), v);
  2107. return constructor_.release();
  2108. }
  2109. template <typename T> inline node_pointer move_copy_of(T& v)
  2110. {
  2111. if (nodes_) {
  2112. constructor_.reclaim(pop_node());
  2113. } else {
  2114. constructor_.create_node();
  2115. }
  2116. BOOST_UNORDERED_CALL_CONSTRUCT1(node_allocator_traits,
  2117. constructor_.alloc_, constructor_.node_->value_ptr(),
  2118. boost::move(v));
  2119. return constructor_.release();
  2120. }
  2121. iterator begin() const { return iterator(nodes_); }
  2122. };
  2123. template <typename Alloc> node_holder<Alloc>::~node_holder()
  2124. {
  2125. while (nodes_) {
  2126. node_pointer p = nodes_;
  2127. nodes_ = static_cast<node_pointer>(p->next_);
  2128. BOOST_UNORDERED_CALL_DESTROY(
  2129. node_allocator_traits, constructor_.alloc_, p->value_ptr());
  2130. boost::unordered::detail::func::destroy(boost::to_address(p));
  2131. node_allocator_traits::deallocate(constructor_.alloc_, p, 1);
  2132. }
  2133. }
  2134. ///////////////////////////////////////////////////////////////////
  2135. //
  2136. // Bucket
  2137. template <typename NodePointer> struct bucket
  2138. {
  2139. typedef NodePointer link_pointer;
  2140. link_pointer next_;
  2141. bucket() : next_() {}
  2142. bucket(link_pointer n) : next_(n) {}
  2143. link_pointer first_from_start() { return next_; }
  2144. enum
  2145. {
  2146. extra_node = true
  2147. };
  2148. };
  2149. struct ptr_bucket
  2150. {
  2151. typedef ptr_bucket* link_pointer;
  2152. link_pointer next_;
  2153. ptr_bucket() : next_(0) {}
  2154. ptr_bucket(link_pointer n) : next_(n) {}
  2155. link_pointer first_from_start() { return this; }
  2156. enum
  2157. {
  2158. extra_node = false
  2159. };
  2160. };
  2161. ///////////////////////////////////////////////////////////////////
  2162. //
  2163. // Hash Policy
  2164. template <typename SizeT> struct prime_policy
  2165. {
  2166. template <typename Hash, typename T>
  2167. static inline SizeT apply_hash(Hash const& hf, T const& x)
  2168. {
  2169. return hf(x);
  2170. }
  2171. static inline SizeT to_bucket(SizeT bucket_count, SizeT hash)
  2172. {
  2173. return hash % bucket_count;
  2174. }
  2175. static inline SizeT new_bucket_count(SizeT min)
  2176. {
  2177. return boost::unordered::detail::next_prime(min);
  2178. }
  2179. static inline SizeT prev_bucket_count(SizeT max)
  2180. {
  2181. return boost::unordered::detail::prev_prime(max);
  2182. }
  2183. };
  2184. template <typename SizeT> struct mix64_policy
  2185. {
  2186. template <typename Hash, typename T>
  2187. static inline SizeT apply_hash(Hash const& hf, T const& x)
  2188. {
  2189. SizeT key = hf(x);
  2190. key = (~key) + (key << 21); // key = (key << 21) - key - 1;
  2191. key = key ^ (key >> 24);
  2192. key = (key + (key << 3)) + (key << 8); // key * 265
  2193. key = key ^ (key >> 14);
  2194. key = (key + (key << 2)) + (key << 4); // key * 21
  2195. key = key ^ (key >> 28);
  2196. key = key + (key << 31);
  2197. return key;
  2198. }
  2199. static inline SizeT to_bucket(SizeT bucket_count, SizeT hash)
  2200. {
  2201. return hash & (bucket_count - 1);
  2202. }
  2203. static inline SizeT new_bucket_count(SizeT min)
  2204. {
  2205. if (min <= 4)
  2206. return 4;
  2207. --min;
  2208. min |= min >> 1;
  2209. min |= min >> 2;
  2210. min |= min >> 4;
  2211. min |= min >> 8;
  2212. min |= min >> 16;
  2213. min |= min >> 32;
  2214. return min + 1;
  2215. }
  2216. static inline SizeT prev_bucket_count(SizeT max)
  2217. {
  2218. max |= max >> 1;
  2219. max |= max >> 2;
  2220. max |= max >> 4;
  2221. max |= max >> 8;
  2222. max |= max >> 16;
  2223. max |= max >> 32;
  2224. return (max >> 1) + 1;
  2225. }
  2226. };
  2227. template <int digits, int radix> struct pick_policy_impl
  2228. {
  2229. typedef prime_policy<std::size_t> type;
  2230. };
  2231. template <> struct pick_policy_impl<64, 2>
  2232. {
  2233. typedef mix64_policy<std::size_t> type;
  2234. };
  2235. template <typename T>
  2236. struct pick_policy2
  2237. : pick_policy_impl<std::numeric_limits<std::size_t>::digits,
  2238. std::numeric_limits<std::size_t>::radix>
  2239. {
  2240. };
  2241. // While the mix policy is generally faster, the prime policy is a lot
  2242. // faster when a large number consecutive integers are used, because
  2243. // there are no collisions. Since that is probably quite common, use
  2244. // prime policy for integeral types. But not the smaller ones, as they
  2245. // don't have enough unique values for this to be an issue.
  2246. template <> struct pick_policy2<int>
  2247. {
  2248. typedef prime_policy<std::size_t> type;
  2249. };
  2250. template <> struct pick_policy2<unsigned int>
  2251. {
  2252. typedef prime_policy<std::size_t> type;
  2253. };
  2254. template <> struct pick_policy2<long>
  2255. {
  2256. typedef prime_policy<std::size_t> type;
  2257. };
  2258. template <> struct pick_policy2<unsigned long>
  2259. {
  2260. typedef prime_policy<std::size_t> type;
  2261. };
  2262. #if !defined(BOOST_NO_LONG_LONG)
  2263. template <> struct pick_policy2<boost::long_long_type>
  2264. {
  2265. typedef prime_policy<std::size_t> type;
  2266. };
  2267. template <> struct pick_policy2<boost::ulong_long_type>
  2268. {
  2269. typedef prime_policy<std::size_t> type;
  2270. };
  2271. #endif
  2272. template <typename T>
  2273. struct pick_policy : pick_policy2<typename boost::remove_cv<T>::type>
  2274. {
  2275. };
  2276. //////////////////////////////////////////////////////////////////////////
  2277. // Functions
  2278. //
  2279. // This double buffers the storage for the hash function and key equality
  2280. // predicate in order to have exception safe copy/swap. To do so,
  2281. // use 'construct_spare' to construct in the spare space, and then when
  2282. // ready to use 'switch_functions' to switch to the new functions.
  2283. // If an exception is thrown between these two calls, use
  2284. // 'cleanup_spare_functions' to destroy the unused constructed functions.
  2285. template <class H, class P> class functions
  2286. {
  2287. public:
  2288. static const bool nothrow_move_assignable =
  2289. boost::is_nothrow_move_assignable<H>::value &&
  2290. boost::is_nothrow_move_assignable<P>::value;
  2291. static const bool nothrow_move_constructible =
  2292. boost::is_nothrow_move_constructible<H>::value &&
  2293. boost::is_nothrow_move_constructible<P>::value;
  2294. static const bool nothrow_swappable =
  2295. boost::is_nothrow_swappable<H>::value &&
  2296. boost::is_nothrow_swappable<P>::value;
  2297. private:
  2298. functions& operator=(functions const&);
  2299. typedef compressed<H, P> function_pair;
  2300. typedef typename boost::aligned_storage<sizeof(function_pair),
  2301. boost::alignment_of<function_pair>::value>::type aligned_function;
  2302. unsigned char current_; // 0/1 - Currently active functions
  2303. // +2 - Both constructed
  2304. aligned_function funcs_[2];
  2305. public:
  2306. functions(H const& hf, P const& eq) : current_(0)
  2307. {
  2308. construct_functions(current_, hf, eq);
  2309. }
  2310. functions(functions const& bf) : current_(0)
  2311. {
  2312. construct_functions(current_, bf.current_functions());
  2313. }
  2314. functions(functions& bf, boost::unordered::detail::move_tag)
  2315. : current_(0)
  2316. {
  2317. construct_functions(current_, bf.current_functions(),
  2318. boost::unordered::detail::integral_constant<bool,
  2319. nothrow_move_constructible>());
  2320. }
  2321. ~functions()
  2322. {
  2323. BOOST_ASSERT(!(current_ & 2));
  2324. destroy_functions(current_);
  2325. }
  2326. H const& hash_function() const { return current_functions().first(); }
  2327. P const& key_eq() const { return current_functions().second(); }
  2328. function_pair const& current_functions() const
  2329. {
  2330. return *static_cast<function_pair const*>(
  2331. static_cast<void const*>(funcs_[current_ & 1].address()));
  2332. }
  2333. function_pair& current_functions()
  2334. {
  2335. return *static_cast<function_pair*>(
  2336. static_cast<void*>(funcs_[current_ & 1].address()));
  2337. }
  2338. void construct_spare_functions(function_pair const& f)
  2339. {
  2340. BOOST_ASSERT(!(current_ & 2));
  2341. construct_functions(current_ ^ 1, f);
  2342. current_ |= 2;
  2343. }
  2344. void cleanup_spare_functions()
  2345. {
  2346. if (current_ & 2) {
  2347. current_ = static_cast<unsigned char>(current_ & 1);
  2348. destroy_functions(current_ ^ 1);
  2349. }
  2350. }
  2351. void switch_functions()
  2352. {
  2353. BOOST_ASSERT(current_ & 2);
  2354. destroy_functions(static_cast<unsigned char>(current_ & 1));
  2355. current_ ^= 3;
  2356. }
  2357. private:
  2358. void construct_functions(unsigned char which, H const& hf, P const& eq)
  2359. {
  2360. BOOST_ASSERT(!(which & 2));
  2361. new ((void*)&funcs_[which]) function_pair(hf, eq);
  2362. }
  2363. void construct_functions(unsigned char which, function_pair const& f,
  2364. boost::unordered::detail::false_type =
  2365. boost::unordered::detail::false_type())
  2366. {
  2367. BOOST_ASSERT(!(which & 2));
  2368. new ((void*)&funcs_[which]) function_pair(f);
  2369. }
  2370. void construct_functions(unsigned char which, function_pair& f,
  2371. boost::unordered::detail::true_type)
  2372. {
  2373. BOOST_ASSERT(!(which & 2));
  2374. new ((void*)&funcs_[which])
  2375. function_pair(f, boost::unordered::detail::move_tag());
  2376. }
  2377. void destroy_functions(unsigned char which)
  2378. {
  2379. BOOST_ASSERT(!(which & 2));
  2380. boost::unordered::detail::func::destroy(
  2381. (function_pair*)(&funcs_[which]));
  2382. }
  2383. };
  2384. ////////////////////////////////////////////////////////////////////////////
  2385. // rvalue parameters when type can't be a BOOST_RV_REF(T) parameter
  2386. // e.g. for int
  2387. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  2388. #define BOOST_UNORDERED_RV_REF(T) BOOST_RV_REF(T)
  2389. #else
  2390. struct please_ignore_this_overload
  2391. {
  2392. typedef please_ignore_this_overload type;
  2393. };
  2394. template <typename T> struct rv_ref_impl
  2395. {
  2396. typedef BOOST_RV_REF(T) type;
  2397. };
  2398. template <typename T>
  2399. struct rv_ref
  2400. : boost::detail::if_true<boost::is_class<T>::value>::
  2401. BOOST_NESTED_TEMPLATE then<boost::unordered::detail::rv_ref_impl<T>,
  2402. please_ignore_this_overload>::type
  2403. {
  2404. };
  2405. #define BOOST_UNORDERED_RV_REF(T) \
  2406. typename boost::unordered::detail::rv_ref<T>::type
  2407. #endif
  2408. #if defined(BOOST_MSVC)
  2409. #pragma warning(push)
  2410. #pragma warning(disable : 4127) // conditional expression is constant
  2411. #endif
  2412. //////////////////////////////////////////////////////////////////////////
  2413. // convert double to std::size_t
  2414. inline std::size_t double_to_size(double f)
  2415. {
  2416. return f >= static_cast<double>(
  2417. (std::numeric_limits<std::size_t>::max)())
  2418. ? (std::numeric_limits<std::size_t>::max)()
  2419. : static_cast<std::size_t>(f);
  2420. }
  2421. template <typename Types>
  2422. struct table : boost::unordered::detail::functions<typename Types::hasher,
  2423. typename Types::key_equal>
  2424. {
  2425. private:
  2426. table(table const&);
  2427. table& operator=(table const&);
  2428. public:
  2429. typedef typename Types::node node;
  2430. typedef typename Types::bucket bucket;
  2431. typedef typename Types::hasher hasher;
  2432. typedef typename Types::key_equal key_equal;
  2433. typedef typename Types::const_key_type const_key_type;
  2434. typedef typename Types::extractor extractor;
  2435. typedef typename Types::value_type value_type;
  2436. typedef typename Types::table table_impl;
  2437. typedef typename Types::link_pointer link_pointer;
  2438. typedef typename Types::policy policy;
  2439. typedef typename Types::iterator iterator;
  2440. typedef typename Types::c_iterator c_iterator;
  2441. typedef typename Types::l_iterator l_iterator;
  2442. typedef typename Types::cl_iterator cl_iterator;
  2443. typedef boost::unordered::detail::functions<typename Types::hasher,
  2444. typename Types::key_equal>
  2445. functions;
  2446. typedef typename Types::value_allocator value_allocator;
  2447. typedef typename boost::unordered::detail::rebind_wrap<value_allocator,
  2448. node>::type node_allocator;
  2449. typedef typename boost::unordered::detail::rebind_wrap<value_allocator,
  2450. bucket>::type bucket_allocator;
  2451. typedef boost::unordered::detail::allocator_traits<node_allocator>
  2452. node_allocator_traits;
  2453. typedef boost::unordered::detail::allocator_traits<bucket_allocator>
  2454. bucket_allocator_traits;
  2455. typedef typename node_allocator_traits::pointer node_pointer;
  2456. typedef
  2457. typename node_allocator_traits::const_pointer const_node_pointer;
  2458. typedef typename bucket_allocator_traits::pointer bucket_pointer;
  2459. typedef boost::unordered::detail::node_constructor<node_allocator>
  2460. node_constructor;
  2461. typedef boost::unordered::detail::node_tmp<node_allocator> node_tmp;
  2462. typedef std::pair<iterator, bool> emplace_return;
  2463. ////////////////////////////////////////////////////////////////////////
  2464. // Members
  2465. boost::unordered::detail::compressed<bucket_allocator, node_allocator>
  2466. allocators_;
  2467. std::size_t bucket_count_;
  2468. std::size_t size_;
  2469. float mlf_;
  2470. std::size_t max_load_;
  2471. bucket_pointer buckets_;
  2472. ////////////////////////////////////////////////////////////////////////
  2473. // Data access
  2474. static node_pointer get_node(c_iterator it) { return it.node_; }
  2475. static node_pointer next_node(link_pointer n)
  2476. {
  2477. return static_cast<node_pointer>(n->next_);
  2478. }
  2479. static node_pointer next_for_find(link_pointer n)
  2480. {
  2481. node_pointer n2 = static_cast<node_pointer>(n);
  2482. do {
  2483. n2 = next_node(n2);
  2484. } while (n2 && !n2->is_first_in_group());
  2485. return n2;
  2486. }
  2487. node_pointer next_group(node_pointer n) const
  2488. {
  2489. node_pointer n1 = n;
  2490. do {
  2491. n1 = next_node(n1);
  2492. } while (n1 && !n1->is_first_in_group());
  2493. return n1;
  2494. }
  2495. std::size_t group_count(node_pointer n) const
  2496. {
  2497. std::size_t x = 0;
  2498. node_pointer it = n;
  2499. do {
  2500. ++x;
  2501. it = next_node(it);
  2502. } while (it && !it->is_first_in_group());
  2503. return x;
  2504. }
  2505. std::size_t node_bucket(node_pointer n) const
  2506. {
  2507. return n->get_bucket();
  2508. }
  2509. bucket_allocator const& bucket_alloc() const
  2510. {
  2511. return allocators_.first();
  2512. }
  2513. node_allocator const& node_alloc() const
  2514. {
  2515. return allocators_.second();
  2516. }
  2517. bucket_allocator& bucket_alloc() { return allocators_.first(); }
  2518. node_allocator& node_alloc() { return allocators_.second(); }
  2519. std::size_t max_bucket_count() const
  2520. {
  2521. // -1 to account for the start bucket.
  2522. return policy::prev_bucket_count(
  2523. bucket_allocator_traits::max_size(bucket_alloc()) - 1);
  2524. }
  2525. bucket_pointer get_bucket_pointer(std::size_t bucket_index) const
  2526. {
  2527. BOOST_ASSERT(buckets_);
  2528. return buckets_ + static_cast<std::ptrdiff_t>(bucket_index);
  2529. }
  2530. link_pointer get_previous_start() const
  2531. {
  2532. return get_bucket_pointer(bucket_count_)->first_from_start();
  2533. }
  2534. link_pointer get_previous_start(std::size_t bucket_index) const
  2535. {
  2536. return get_bucket_pointer(bucket_index)->next_;
  2537. }
  2538. node_pointer begin() const
  2539. {
  2540. return size_ ? next_node(get_previous_start()) : node_pointer();
  2541. }
  2542. node_pointer begin(std::size_t bucket_index) const
  2543. {
  2544. if (!size_)
  2545. return node_pointer();
  2546. link_pointer prev = get_previous_start(bucket_index);
  2547. return prev ? next_node(prev) : node_pointer();
  2548. }
  2549. std::size_t hash_to_bucket(std::size_t hash_value) const
  2550. {
  2551. return policy::to_bucket(bucket_count_, hash_value);
  2552. }
  2553. std::size_t bucket_size(std::size_t index) const
  2554. {
  2555. node_pointer n = begin(index);
  2556. if (!n)
  2557. return 0;
  2558. std::size_t count = 0;
  2559. while (n && node_bucket(n) == index) {
  2560. ++count;
  2561. n = next_node(n);
  2562. }
  2563. return count;
  2564. }
  2565. ////////////////////////////////////////////////////////////////////////
  2566. // Load methods
  2567. void recalculate_max_load()
  2568. {
  2569. using namespace std;
  2570. // From 6.3.1/13:
  2571. // Only resize when size >= mlf_ * count
  2572. max_load_ = buckets_ ? boost::unordered::detail::double_to_size(
  2573. ceil(static_cast<double>(mlf_) *
  2574. static_cast<double>(bucket_count_)))
  2575. : 0;
  2576. }
  2577. void max_load_factor(float z)
  2578. {
  2579. BOOST_ASSERT(z > 0);
  2580. mlf_ = (std::max)(z, minimum_max_load_factor);
  2581. recalculate_max_load();
  2582. }
  2583. std::size_t min_buckets_for_size(std::size_t size) const
  2584. {
  2585. BOOST_ASSERT(mlf_ >= minimum_max_load_factor);
  2586. using namespace std;
  2587. // From insert/emplace requirements:
  2588. //
  2589. // size <= mlf_ * count
  2590. // => count >= size / mlf_
  2591. //
  2592. // Or from rehash post-condition:
  2593. //
  2594. // count >= size / mlf_
  2595. return policy::new_bucket_count(
  2596. boost::unordered::detail::double_to_size(
  2597. floor(static_cast<double>(size) / static_cast<double>(mlf_)) +
  2598. 1));
  2599. }
  2600. ////////////////////////////////////////////////////////////////////////
  2601. // Constructors
  2602. table(std::size_t num_buckets, hasher const& hf, key_equal const& eq,
  2603. node_allocator const& a)
  2604. : functions(hf, eq), allocators_(a, a),
  2605. bucket_count_(policy::new_bucket_count(num_buckets)), size_(0),
  2606. mlf_(1.0f), max_load_(0), buckets_()
  2607. {
  2608. }
  2609. table(table const& x, node_allocator const& a)
  2610. : functions(x), allocators_(a, a),
  2611. bucket_count_(x.min_buckets_for_size(x.size_)), size_(0),
  2612. mlf_(x.mlf_), max_load_(0), buckets_()
  2613. {
  2614. }
  2615. table(table& x, boost::unordered::detail::move_tag m)
  2616. : functions(x, m), allocators_(x.allocators_, m),
  2617. bucket_count_(x.bucket_count_), size_(x.size_), mlf_(x.mlf_),
  2618. max_load_(x.max_load_), buckets_(x.buckets_)
  2619. {
  2620. x.buckets_ = bucket_pointer();
  2621. x.size_ = 0;
  2622. x.max_load_ = 0;
  2623. }
  2624. table(table& x, node_allocator const& a,
  2625. boost::unordered::detail::move_tag m)
  2626. : functions(x, m), allocators_(a, a),
  2627. bucket_count_(x.bucket_count_), size_(0), mlf_(x.mlf_),
  2628. max_load_(0), buckets_()
  2629. {
  2630. }
  2631. ////////////////////////////////////////////////////////////////////////
  2632. // Clear buckets and Create buckets
  2633. //
  2634. // IMPORTANT: If the container already contains any elements, the
  2635. // buckets will not contain any links to them. This will
  2636. // need to be dealt with, for example by:
  2637. // - deleting them
  2638. // - putting them in a 'node_holder' for future use
  2639. // (as in assignment)
  2640. // - placing them in buckets (see rehash_impl)
  2641. // Clear the bucket pointers.
  2642. void clear_buckets()
  2643. {
  2644. bucket_pointer end = get_bucket_pointer(bucket_count_);
  2645. for (bucket_pointer it = buckets_; it != end; ++it) {
  2646. it->next_ = node_pointer();
  2647. }
  2648. }
  2649. // Create container buckets. If the container already contains any
  2650. // buckets
  2651. // the linked list will be transferred to the new buckets, but none
  2652. // of the bucket pointers will be set. See above note.
  2653. //
  2654. // Strong exception safety.
  2655. void create_buckets(std::size_t new_count)
  2656. {
  2657. link_pointer dummy_node;
  2658. // Construct the new buckets and dummy node, and destroy the old
  2659. // buckets
  2660. if (buckets_) {
  2661. dummy_node =
  2662. (buckets_ + static_cast<std::ptrdiff_t>(bucket_count_))->next_;
  2663. bucket_pointer new_buckets =
  2664. bucket_allocator_traits::allocate(bucket_alloc(), new_count + 1);
  2665. destroy_buckets();
  2666. buckets_ = new_buckets;
  2667. } else if (bucket::extra_node) {
  2668. node_constructor a(node_alloc());
  2669. a.create_node();
  2670. buckets_ =
  2671. bucket_allocator_traits::allocate(bucket_alloc(), new_count + 1);
  2672. dummy_node = a.release();
  2673. } else {
  2674. dummy_node = link_pointer();
  2675. buckets_ =
  2676. bucket_allocator_traits::allocate(bucket_alloc(), new_count + 1);
  2677. }
  2678. // nothrow from here...
  2679. bucket_count_ = new_count;
  2680. recalculate_max_load();
  2681. bucket_pointer end =
  2682. buckets_ + static_cast<std::ptrdiff_t>(new_count);
  2683. for (bucket_pointer i = buckets_; i != end; ++i) {
  2684. new ((void*)boost::to_address(i)) bucket();
  2685. }
  2686. new ((void*)boost::to_address(end)) bucket(dummy_node);
  2687. }
  2688. ////////////////////////////////////////////////////////////////////////
  2689. // Swap and Move
  2690. void swap_allocators(table& other, false_type)
  2691. {
  2692. boost::unordered::detail::func::ignore_unused_variable_warning(other);
  2693. // According to 23.2.1.8, if propagate_on_container_swap is
  2694. // false the behaviour is undefined unless the allocators
  2695. // are equal.
  2696. BOOST_ASSERT(node_alloc() == other.node_alloc());
  2697. }
  2698. void swap_allocators(table& other, true_type)
  2699. {
  2700. allocators_.swap(other.allocators_);
  2701. }
  2702. // Not nothrow swappable
  2703. void swap(table& x, false_type)
  2704. {
  2705. if (this == &x) {
  2706. return;
  2707. }
  2708. this->construct_spare_functions(x.current_functions());
  2709. BOOST_TRY { x.construct_spare_functions(this->current_functions()); }
  2710. BOOST_CATCH(...)
  2711. {
  2712. this->cleanup_spare_functions();
  2713. BOOST_RETHROW
  2714. }
  2715. BOOST_CATCH_END
  2716. this->switch_functions();
  2717. x.switch_functions();
  2718. swap_allocators(
  2719. x, boost::unordered::detail::integral_constant<bool,
  2720. allocator_traits<
  2721. node_allocator>::propagate_on_container_swap::value>());
  2722. boost::swap(buckets_, x.buckets_);
  2723. boost::swap(bucket_count_, x.bucket_count_);
  2724. boost::swap(size_, x.size_);
  2725. std::swap(mlf_, x.mlf_);
  2726. std::swap(max_load_, x.max_load_);
  2727. }
  2728. // Nothrow swappable
  2729. void swap(table& x, true_type)
  2730. {
  2731. swap_allocators(
  2732. x, boost::unordered::detail::integral_constant<bool,
  2733. allocator_traits<
  2734. node_allocator>::propagate_on_container_swap::value>());
  2735. boost::swap(buckets_, x.buckets_);
  2736. boost::swap(bucket_count_, x.bucket_count_);
  2737. boost::swap(size_, x.size_);
  2738. std::swap(mlf_, x.mlf_);
  2739. std::swap(max_load_, x.max_load_);
  2740. this->current_functions().swap(x.current_functions());
  2741. }
  2742. // Only swaps the allocators if propagate_on_container_swap.
  2743. // If not propagate_on_container_swap and allocators aren't
  2744. // equal, behaviour is undefined.
  2745. void swap(table& x)
  2746. {
  2747. BOOST_ASSERT(allocator_traits<
  2748. node_allocator>::propagate_on_container_swap::value ||
  2749. node_alloc() == x.node_alloc());
  2750. swap(x, boost::unordered::detail::integral_constant<bool,
  2751. functions::nothrow_swappable>());
  2752. }
  2753. // Only call with nodes allocated with the currect allocator, or
  2754. // one that is equal to it. (Can't assert because other's
  2755. // allocators might have already been moved).
  2756. void move_buckets_from(table& other)
  2757. {
  2758. BOOST_ASSERT(!buckets_);
  2759. buckets_ = other.buckets_;
  2760. bucket_count_ = other.bucket_count_;
  2761. size_ = other.size_;
  2762. max_load_ = other.max_load_;
  2763. other.buckets_ = bucket_pointer();
  2764. other.size_ = 0;
  2765. other.max_load_ = 0;
  2766. }
  2767. // For use in the constructor when allocators might be different.
  2768. void move_construct_buckets(table& src)
  2769. {
  2770. if (this->node_alloc() == src.node_alloc()) {
  2771. move_buckets_from(src);
  2772. } else {
  2773. this->create_buckets(this->bucket_count_);
  2774. link_pointer prev = this->get_previous_start();
  2775. std::size_t last_bucket = this->bucket_count_;
  2776. for (node_pointer n = src.begin(); n; n = next_node(n)) {
  2777. std::size_t n_bucket = n->get_bucket();
  2778. if (n_bucket != last_bucket) {
  2779. this->get_bucket_pointer(n_bucket)->next_ = prev;
  2780. }
  2781. node_pointer n2 = boost::unordered::detail::func::construct_node(
  2782. this->node_alloc(), boost::move(n->value()));
  2783. n2->bucket_info_ = n->bucket_info_;
  2784. prev->next_ = n2;
  2785. ++size_;
  2786. prev = n2;
  2787. last_bucket = n_bucket;
  2788. }
  2789. }
  2790. }
  2791. ////////////////////////////////////////////////////////////////////////
  2792. // Delete/destruct
  2793. ~table() { delete_buckets(); }
  2794. void destroy_node(node_pointer n)
  2795. {
  2796. BOOST_UNORDERED_CALL_DESTROY(
  2797. node_allocator_traits, node_alloc(), n->value_ptr());
  2798. boost::unordered::detail::func::destroy(boost::to_address(n));
  2799. node_allocator_traits::deallocate(node_alloc(), n, 1);
  2800. }
  2801. void delete_buckets()
  2802. {
  2803. if (buckets_) {
  2804. node_pointer n = static_cast<node_pointer>(
  2805. get_bucket_pointer(bucket_count_)->next_);
  2806. if (bucket::extra_node) {
  2807. node_pointer next = next_node(n);
  2808. boost::unordered::detail::func::destroy(boost::to_address(n));
  2809. node_allocator_traits::deallocate(node_alloc(), n, 1);
  2810. n = next;
  2811. }
  2812. while (n) {
  2813. node_pointer next = next_node(n);
  2814. destroy_node(n);
  2815. n = next;
  2816. }
  2817. destroy_buckets();
  2818. buckets_ = bucket_pointer();
  2819. max_load_ = 0;
  2820. size_ = 0;
  2821. }
  2822. }
  2823. void destroy_buckets()
  2824. {
  2825. bucket_pointer end = get_bucket_pointer(bucket_count_ + 1);
  2826. for (bucket_pointer it = buckets_; it != end; ++it) {
  2827. boost::unordered::detail::func::destroy(boost::to_address(it));
  2828. }
  2829. bucket_allocator_traits::deallocate(
  2830. bucket_alloc(), buckets_, bucket_count_ + 1);
  2831. }
  2832. ////////////////////////////////////////////////////////////////////////
  2833. // Fix buckets after delete/extract
  2834. //
  2835. // (prev,next) should mark an open range of nodes in a single bucket
  2836. // which
  2837. // have either been unlinked, or are about to be.
  2838. std::size_t fix_bucket(
  2839. std::size_t bucket_index, link_pointer prev, node_pointer next)
  2840. {
  2841. std::size_t bucket_index2 = bucket_index;
  2842. if (next) {
  2843. bucket_index2 = node_bucket(next);
  2844. // If next is in the same bucket, then there's nothing to do.
  2845. if (bucket_index == bucket_index2) {
  2846. return bucket_index2;
  2847. }
  2848. // Update the bucket containing next.
  2849. get_bucket_pointer(bucket_index2)->next_ = prev;
  2850. }
  2851. // Check if this bucket is now empty.
  2852. bucket_pointer this_bucket = get_bucket_pointer(bucket_index);
  2853. if (this_bucket->next_ == prev) {
  2854. this_bucket->next_ = link_pointer();
  2855. }
  2856. return bucket_index2;
  2857. }
  2858. ////////////////////////////////////////////////////////////////////////
  2859. // Clear
  2860. void clear_impl();
  2861. ////////////////////////////////////////////////////////////////////////
  2862. // Assignment
  2863. template <typename UniqueType>
  2864. void assign(table const& x, UniqueType is_unique)
  2865. {
  2866. if (this != &x) {
  2867. assign(x, is_unique,
  2868. boost::unordered::detail::integral_constant<bool,
  2869. allocator_traits<node_allocator>::
  2870. propagate_on_container_copy_assignment::value>());
  2871. }
  2872. }
  2873. template <typename UniqueType>
  2874. void assign(table const& x, UniqueType is_unique, false_type)
  2875. {
  2876. // Strong exception safety.
  2877. this->construct_spare_functions(x.current_functions());
  2878. BOOST_TRY
  2879. {
  2880. mlf_ = x.mlf_;
  2881. recalculate_max_load();
  2882. if (x.size_ > max_load_) {
  2883. create_buckets(min_buckets_for_size(x.size_));
  2884. } else if (size_) {
  2885. clear_buckets();
  2886. }
  2887. }
  2888. BOOST_CATCH(...)
  2889. {
  2890. this->cleanup_spare_functions();
  2891. BOOST_RETHROW
  2892. }
  2893. BOOST_CATCH_END
  2894. this->switch_functions();
  2895. assign_buckets(x, is_unique);
  2896. }
  2897. template <typename UniqueType>
  2898. void assign(table const& x, UniqueType is_unique, true_type)
  2899. {
  2900. if (node_alloc() == x.node_alloc()) {
  2901. allocators_.assign(x.allocators_);
  2902. assign(x, is_unique, false_type());
  2903. } else {
  2904. this->construct_spare_functions(x.current_functions());
  2905. this->switch_functions();
  2906. // Delete everything with current allocators before assigning
  2907. // the new ones.
  2908. delete_buckets();
  2909. allocators_.assign(x.allocators_);
  2910. // Copy over other data, all no throw.
  2911. mlf_ = x.mlf_;
  2912. bucket_count_ = min_buckets_for_size(x.size_);
  2913. // Finally copy the elements.
  2914. if (x.size_) {
  2915. copy_buckets(x, is_unique);
  2916. }
  2917. }
  2918. }
  2919. template <typename UniqueType>
  2920. void move_assign(table& x, UniqueType is_unique)
  2921. {
  2922. if (this != &x) {
  2923. move_assign(x, is_unique,
  2924. boost::unordered::detail::integral_constant<bool,
  2925. allocator_traits<node_allocator>::
  2926. propagate_on_container_move_assignment::value>());
  2927. }
  2928. }
  2929. // Propagate allocator
  2930. template <typename UniqueType>
  2931. void move_assign(table& x, UniqueType, true_type)
  2932. {
  2933. if (!functions::nothrow_move_assignable) {
  2934. this->construct_spare_functions(x.current_functions());
  2935. this->switch_functions();
  2936. } else {
  2937. this->current_functions().move_assign(x.current_functions());
  2938. }
  2939. delete_buckets();
  2940. allocators_.move_assign(x.allocators_);
  2941. mlf_ = x.mlf_;
  2942. move_buckets_from(x);
  2943. }
  2944. // Don't propagate allocator
  2945. template <typename UniqueType>
  2946. void move_assign(table& x, UniqueType is_unique, false_type)
  2947. {
  2948. if (node_alloc() == x.node_alloc()) {
  2949. move_assign_equal_alloc(x);
  2950. } else {
  2951. move_assign_realloc(x, is_unique);
  2952. }
  2953. }
  2954. void move_assign_equal_alloc(table& x)
  2955. {
  2956. if (!functions::nothrow_move_assignable) {
  2957. this->construct_spare_functions(x.current_functions());
  2958. this->switch_functions();
  2959. } else {
  2960. this->current_functions().move_assign(x.current_functions());
  2961. }
  2962. delete_buckets();
  2963. mlf_ = x.mlf_;
  2964. move_buckets_from(x);
  2965. }
  2966. template <typename UniqueType>
  2967. void move_assign_realloc(table& x, UniqueType is_unique)
  2968. {
  2969. this->construct_spare_functions(x.current_functions());
  2970. BOOST_TRY
  2971. {
  2972. mlf_ = x.mlf_;
  2973. recalculate_max_load();
  2974. if (x.size_ > max_load_) {
  2975. create_buckets(min_buckets_for_size(x.size_));
  2976. } else if (size_) {
  2977. clear_buckets();
  2978. }
  2979. }
  2980. BOOST_CATCH(...)
  2981. {
  2982. this->cleanup_spare_functions();
  2983. BOOST_RETHROW
  2984. }
  2985. BOOST_CATCH_END
  2986. this->switch_functions();
  2987. move_assign_buckets(x, is_unique);
  2988. }
  2989. // Accessors
  2990. const_key_type& get_key(node_pointer n) const
  2991. {
  2992. return extractor::extract(n->value());
  2993. }
  2994. std::size_t hash(const_key_type& k) const
  2995. {
  2996. return policy::apply_hash(this->hash_function(), k);
  2997. }
  2998. // Find Node
  2999. node_pointer find_node(std::size_t key_hash, const_key_type& k) const
  3000. {
  3001. return this->find_node_impl(key_hash, k, this->key_eq());
  3002. }
  3003. node_pointer find_node(const_key_type& k) const
  3004. {
  3005. return this->find_node_impl(hash(k), k, this->key_eq());
  3006. }
  3007. template <class Key, class Pred>
  3008. node_pointer find_node_impl(
  3009. std::size_t key_hash, Key const& k, Pred const& eq) const
  3010. {
  3011. std::size_t bucket_index = this->hash_to_bucket(key_hash);
  3012. node_pointer n = this->begin(bucket_index);
  3013. for (;;) {
  3014. if (!n)
  3015. return n;
  3016. if (eq(k, this->get_key(n))) {
  3017. return n;
  3018. } else if (this->node_bucket(n) != bucket_index) {
  3019. return node_pointer();
  3020. }
  3021. n = next_for_find(n);
  3022. }
  3023. }
  3024. // Find the node before the key, so that it can be erased.
  3025. link_pointer find_previous_node(
  3026. const_key_type& k, std::size_t bucket_index)
  3027. {
  3028. link_pointer prev = this->get_previous_start(bucket_index);
  3029. if (!prev) {
  3030. return prev;
  3031. }
  3032. for (;;) {
  3033. node_pointer n = next_node(prev);
  3034. if (!n) {
  3035. return link_pointer();
  3036. } else if (n->is_first_in_group()) {
  3037. if (node_bucket(n) != bucket_index) {
  3038. return link_pointer();
  3039. } else if (this->key_eq()(k, this->get_key(n))) {
  3040. return prev;
  3041. }
  3042. }
  3043. prev = n;
  3044. }
  3045. }
  3046. // Extract and erase
  3047. inline node_pointer extract_by_key(const_key_type& k)
  3048. {
  3049. if (!this->size_) {
  3050. return node_pointer();
  3051. }
  3052. std::size_t key_hash = this->hash(k);
  3053. std::size_t bucket_index = this->hash_to_bucket(key_hash);
  3054. link_pointer prev = this->find_previous_node(k, bucket_index);
  3055. if (!prev) {
  3056. return node_pointer();
  3057. }
  3058. node_pointer n = next_node(prev);
  3059. node_pointer n2 = next_node(n);
  3060. if (n2) {
  3061. n2->set_first_in_group();
  3062. }
  3063. prev->next_ = n2;
  3064. --this->size_;
  3065. this->fix_bucket(bucket_index, prev, n2);
  3066. n->next_ = link_pointer();
  3067. return n;
  3068. }
  3069. // Reserve and rehash
  3070. void reserve_for_insert(std::size_t);
  3071. void rehash(std::size_t);
  3072. void reserve(std::size_t);
  3073. void rehash_impl(std::size_t);
  3074. ////////////////////////////////////////////////////////////////////////
  3075. // Unique keys
  3076. // equals
  3077. bool equals_unique(table const& other) const
  3078. {
  3079. if (this->size_ != other.size_)
  3080. return false;
  3081. for (node_pointer n1 = this->begin(); n1; n1 = next_node(n1)) {
  3082. node_pointer n2 = other.find_node(other.get_key(n1));
  3083. if (!n2 || n1->value() != n2->value())
  3084. return false;
  3085. }
  3086. return true;
  3087. }
  3088. // Emplace/Insert
  3089. inline node_pointer add_node_unique(
  3090. node_pointer n, std::size_t key_hash)
  3091. {
  3092. std::size_t bucket_index = this->hash_to_bucket(key_hash);
  3093. bucket_pointer b = this->get_bucket_pointer(bucket_index);
  3094. n->bucket_info_ = bucket_index;
  3095. n->set_first_in_group();
  3096. if (!b->next_) {
  3097. link_pointer start_node = this->get_previous_start();
  3098. if (start_node->next_) {
  3099. this->get_bucket_pointer(node_bucket(next_node(start_node)))
  3100. ->next_ = n;
  3101. }
  3102. b->next_ = start_node;
  3103. n->next_ = start_node->next_;
  3104. start_node->next_ = n;
  3105. } else {
  3106. n->next_ = b->next_->next_;
  3107. b->next_->next_ = n;
  3108. }
  3109. ++this->size_;
  3110. return n;
  3111. }
  3112. inline node_pointer resize_and_add_node_unique(
  3113. node_pointer n, std::size_t key_hash)
  3114. {
  3115. node_tmp b(n, this->node_alloc());
  3116. this->reserve_for_insert(this->size_ + 1);
  3117. return this->add_node_unique(b.release(), key_hash);
  3118. }
  3119. template <BOOST_UNORDERED_EMPLACE_TEMPLATE>
  3120. iterator emplace_hint_unique(
  3121. c_iterator hint, const_key_type& k, BOOST_UNORDERED_EMPLACE_ARGS)
  3122. {
  3123. if (hint.node_ && this->key_eq()(k, this->get_key(hint.node_))) {
  3124. return iterator(hint.node_);
  3125. } else {
  3126. return emplace_unique(k, BOOST_UNORDERED_EMPLACE_FORWARD).first;
  3127. }
  3128. }
  3129. template <BOOST_UNORDERED_EMPLACE_TEMPLATE>
  3130. emplace_return emplace_unique(
  3131. const_key_type& k, BOOST_UNORDERED_EMPLACE_ARGS)
  3132. {
  3133. std::size_t key_hash = this->hash(k);
  3134. node_pointer pos = this->find_node(key_hash, k);
  3135. if (pos) {
  3136. return emplace_return(iterator(pos), false);
  3137. } else {
  3138. return emplace_return(
  3139. iterator(this->resize_and_add_node_unique(
  3140. boost::unordered::detail::func::construct_node_from_args(
  3141. this->node_alloc(), BOOST_UNORDERED_EMPLACE_FORWARD),
  3142. key_hash)),
  3143. true);
  3144. }
  3145. }
  3146. template <BOOST_UNORDERED_EMPLACE_TEMPLATE>
  3147. iterator emplace_hint_unique(
  3148. c_iterator hint, no_key, BOOST_UNORDERED_EMPLACE_ARGS)
  3149. {
  3150. node_tmp b(boost::unordered::detail::func::construct_node_from_args(
  3151. this->node_alloc(), BOOST_UNORDERED_EMPLACE_FORWARD),
  3152. this->node_alloc());
  3153. const_key_type& k = this->get_key(b.node_);
  3154. if (hint.node_ && this->key_eq()(k, this->get_key(hint.node_))) {
  3155. return iterator(hint.node_);
  3156. }
  3157. std::size_t key_hash = this->hash(k);
  3158. node_pointer pos = this->find_node(key_hash, k);
  3159. if (pos) {
  3160. return iterator(pos);
  3161. } else {
  3162. return iterator(
  3163. this->resize_and_add_node_unique(b.release(), key_hash));
  3164. }
  3165. }
  3166. template <BOOST_UNORDERED_EMPLACE_TEMPLATE>
  3167. emplace_return emplace_unique(no_key, BOOST_UNORDERED_EMPLACE_ARGS)
  3168. {
  3169. node_tmp b(boost::unordered::detail::func::construct_node_from_args(
  3170. this->node_alloc(), BOOST_UNORDERED_EMPLACE_FORWARD),
  3171. this->node_alloc());
  3172. const_key_type& k = this->get_key(b.node_);
  3173. std::size_t key_hash = this->hash(k);
  3174. node_pointer pos = this->find_node(key_hash, k);
  3175. if (pos) {
  3176. return emplace_return(iterator(pos), false);
  3177. } else {
  3178. return emplace_return(
  3179. iterator(this->resize_and_add_node_unique(b.release(), key_hash)),
  3180. true);
  3181. }
  3182. }
  3183. template <typename Key>
  3184. emplace_return try_emplace_unique(BOOST_FWD_REF(Key) k)
  3185. {
  3186. std::size_t key_hash = this->hash(k);
  3187. node_pointer pos = this->find_node(key_hash, k);
  3188. if (pos) {
  3189. return emplace_return(iterator(pos), false);
  3190. } else {
  3191. return emplace_return(
  3192. iterator(this->resize_and_add_node_unique(
  3193. boost::unordered::detail::func::construct_node_pair(
  3194. this->node_alloc(), boost::forward<Key>(k)),
  3195. key_hash)),
  3196. true);
  3197. }
  3198. }
  3199. template <typename Key>
  3200. iterator try_emplace_hint_unique(c_iterator hint, BOOST_FWD_REF(Key) k)
  3201. {
  3202. if (hint.node_ && this->key_eq()(hint->first, k)) {
  3203. return iterator(hint.node_);
  3204. } else {
  3205. return try_emplace_unique(k).first;
  3206. }
  3207. }
  3208. template <typename Key, BOOST_UNORDERED_EMPLACE_TEMPLATE>
  3209. emplace_return try_emplace_unique(
  3210. BOOST_FWD_REF(Key) k, BOOST_UNORDERED_EMPLACE_ARGS)
  3211. {
  3212. std::size_t key_hash = this->hash(k);
  3213. node_pointer pos = this->find_node(key_hash, k);
  3214. if (pos) {
  3215. return emplace_return(iterator(pos), false);
  3216. } else {
  3217. return emplace_return(
  3218. iterator(this->resize_and_add_node_unique(
  3219. boost::unordered::detail::func::construct_node_pair_from_args(
  3220. this->node_alloc(), boost::forward<Key>(k),
  3221. BOOST_UNORDERED_EMPLACE_FORWARD),
  3222. key_hash)),
  3223. true);
  3224. }
  3225. }
  3226. template <typename Key, BOOST_UNORDERED_EMPLACE_TEMPLATE>
  3227. iterator try_emplace_hint_unique(
  3228. c_iterator hint, BOOST_FWD_REF(Key) k, BOOST_UNORDERED_EMPLACE_ARGS)
  3229. {
  3230. if (hint.node_ && this->key_eq()(hint->first, k)) {
  3231. return iterator(hint.node_);
  3232. } else {
  3233. return try_emplace_unique(k, BOOST_UNORDERED_EMPLACE_FORWARD).first;
  3234. }
  3235. }
  3236. template <typename Key, typename M>
  3237. emplace_return insert_or_assign_unique(
  3238. BOOST_FWD_REF(Key) k, BOOST_FWD_REF(M) obj)
  3239. {
  3240. std::size_t key_hash = this->hash(k);
  3241. node_pointer pos = this->find_node(key_hash, k);
  3242. if (pos) {
  3243. pos->value().second = boost::forward<M>(obj);
  3244. return emplace_return(iterator(pos), false);
  3245. } else {
  3246. return emplace_return(
  3247. iterator(this->resize_and_add_node_unique(
  3248. boost::unordered::detail::func::construct_node_pair(
  3249. this->node_alloc(), boost::forward<Key>(k),
  3250. boost::forward<M>(obj)),
  3251. key_hash)),
  3252. true);
  3253. }
  3254. }
  3255. template <typename NodeType, typename InsertReturnType>
  3256. void move_insert_node_type_unique(
  3257. NodeType& np, InsertReturnType& result)
  3258. {
  3259. if (np) {
  3260. const_key_type& k = this->get_key(np.ptr_);
  3261. std::size_t key_hash = this->hash(k);
  3262. node_pointer pos = this->find_node(key_hash, k);
  3263. if (pos) {
  3264. result.node = boost::move(np);
  3265. result.position = iterator(pos);
  3266. } else {
  3267. this->reserve_for_insert(this->size_ + 1);
  3268. result.position =
  3269. iterator(this->add_node_unique(np.ptr_, key_hash));
  3270. result.inserted = true;
  3271. np.ptr_ = node_pointer();
  3272. }
  3273. }
  3274. }
  3275. template <typename NodeType>
  3276. iterator move_insert_node_type_with_hint_unique(
  3277. c_iterator hint, NodeType& np)
  3278. {
  3279. if (!np) {
  3280. return iterator();
  3281. }
  3282. const_key_type& k = this->get_key(np.ptr_);
  3283. if (hint.node_ && this->key_eq()(k, this->get_key(hint.node_))) {
  3284. return iterator(hint.node_);
  3285. }
  3286. std::size_t key_hash = this->hash(k);
  3287. node_pointer pos = this->find_node(key_hash, k);
  3288. if (!pos) {
  3289. this->reserve_for_insert(this->size_ + 1);
  3290. pos = this->add_node_unique(np.ptr_, key_hash);
  3291. np.ptr_ = node_pointer();
  3292. }
  3293. return iterator(pos);
  3294. }
  3295. template <typename Types2>
  3296. void merge_unique(boost::unordered::detail::table<Types2>& other)
  3297. {
  3298. typedef boost::unordered::detail::table<Types2> other_table;
  3299. BOOST_STATIC_ASSERT(
  3300. (boost::is_same<node, typename other_table::node>::value));
  3301. BOOST_ASSERT(this->node_alloc() == other.node_alloc());
  3302. if (other.size_) {
  3303. link_pointer prev = other.get_previous_start();
  3304. while (prev->next_) {
  3305. node_pointer n = other_table::next_node(prev);
  3306. const_key_type& k = this->get_key(n);
  3307. std::size_t key_hash = this->hash(k);
  3308. node_pointer pos = this->find_node(key_hash, k);
  3309. if (pos) {
  3310. prev = n;
  3311. } else {
  3312. this->reserve_for_insert(this->size_ + 1);
  3313. node_pointer n2 = next_node(n);
  3314. prev->next_ = n2;
  3315. if (n2 && n->is_first_in_group()) {
  3316. n2->set_first_in_group();
  3317. }
  3318. --other.size_;
  3319. other.fix_bucket(other.node_bucket(n), prev, n2);
  3320. this->add_node_unique(n, key_hash);
  3321. }
  3322. }
  3323. }
  3324. }
  3325. ////////////////////////////////////////////////////////////////////////
  3326. // Insert range methods
  3327. //
  3328. // if hash function throws, or inserting > 1 element, basic exception
  3329. // safety strong otherwise
  3330. template <class InputIt>
  3331. void insert_range_unique(const_key_type& k, InputIt i, InputIt j)
  3332. {
  3333. insert_range_unique2(k, i, j);
  3334. while (++i != j) {
  3335. // Note: can't use get_key as '*i' might not be value_type - it
  3336. // could be a pair with first_types as key_type without const or
  3337. // a different second_type.
  3338. insert_range_unique2(extractor::extract(*i), i, j);
  3339. }
  3340. }
  3341. template <class InputIt>
  3342. void insert_range_unique2(const_key_type& k, InputIt i, InputIt j)
  3343. {
  3344. // No side effects in this initial code
  3345. std::size_t key_hash = this->hash(k);
  3346. node_pointer pos = this->find_node(key_hash, k);
  3347. if (!pos) {
  3348. node_tmp b(boost::unordered::detail::func::construct_node(
  3349. this->node_alloc(), *i),
  3350. this->node_alloc());
  3351. if (this->size_ + 1 > this->max_load_)
  3352. this->reserve_for_insert(
  3353. this->size_ + boost::unordered::detail::insert_size(i, j));
  3354. this->add_node_unique(b.release(), key_hash);
  3355. }
  3356. }
  3357. template <class InputIt>
  3358. void insert_range_unique(no_key, InputIt i, InputIt j)
  3359. {
  3360. node_constructor a(this->node_alloc());
  3361. do {
  3362. if (!a.node_) {
  3363. a.create_node();
  3364. }
  3365. BOOST_UNORDERED_CALL_CONSTRUCT1(
  3366. node_allocator_traits, a.alloc_, a.node_->value_ptr(), *i);
  3367. node_tmp b(a.release(), a.alloc_);
  3368. const_key_type& k = this->get_key(b.node_);
  3369. std::size_t key_hash = this->hash(k);
  3370. node_pointer pos = this->find_node(key_hash, k);
  3371. if (pos) {
  3372. a.reclaim(b.release());
  3373. } else {
  3374. // reserve has basic exception safety if the hash function
  3375. // throws, strong otherwise.
  3376. this->reserve_for_insert(this->size_ + 1);
  3377. this->add_node_unique(b.release(), key_hash);
  3378. }
  3379. } while (++i != j);
  3380. }
  3381. ////////////////////////////////////////////////////////////////////////
  3382. // Extract
  3383. inline node_pointer extract_by_iterator_unique(c_iterator i)
  3384. {
  3385. node_pointer n = i.node_;
  3386. BOOST_ASSERT(n);
  3387. std::size_t bucket_index = this->node_bucket(n);
  3388. link_pointer prev = this->get_previous_start(bucket_index);
  3389. while (prev->next_ != n) {
  3390. prev = prev->next_;
  3391. }
  3392. node_pointer n2 = next_node(n);
  3393. prev->next_ = n2;
  3394. --this->size_;
  3395. this->fix_bucket(bucket_index, prev, n2);
  3396. n->next_ = link_pointer();
  3397. return n;
  3398. }
  3399. ////////////////////////////////////////////////////////////////////////
  3400. // Erase
  3401. //
  3402. // no throw
  3403. std::size_t erase_key_unique(const_key_type& k)
  3404. {
  3405. if (!this->size_)
  3406. return 0;
  3407. std::size_t key_hash = this->hash(k);
  3408. std::size_t bucket_index = this->hash_to_bucket(key_hash);
  3409. link_pointer prev = this->find_previous_node(k, bucket_index);
  3410. if (!prev)
  3411. return 0;
  3412. node_pointer n = next_node(prev);
  3413. node_pointer n2 = next_node(n);
  3414. prev->next_ = n2;
  3415. --size_;
  3416. this->fix_bucket(bucket_index, prev, n2);
  3417. this->destroy_node(n);
  3418. return 1;
  3419. }
  3420. void erase_nodes_unique(node_pointer i, node_pointer j)
  3421. {
  3422. std::size_t bucket_index = this->node_bucket(i);
  3423. // Find the node before i.
  3424. link_pointer prev = this->get_previous_start(bucket_index);
  3425. while (prev->next_ != i)
  3426. prev = prev->next_;
  3427. // Delete the nodes.
  3428. prev->next_ = j;
  3429. do {
  3430. node_pointer next = next_node(i);
  3431. destroy_node(i);
  3432. --size_;
  3433. bucket_index = this->fix_bucket(bucket_index, prev, next);
  3434. i = next;
  3435. } while (i != j);
  3436. }
  3437. ////////////////////////////////////////////////////////////////////////
  3438. // fill_buckets_unique
  3439. void copy_buckets(table const& src, true_type)
  3440. {
  3441. this->create_buckets(this->bucket_count_);
  3442. for (node_pointer n = src.begin(); n; n = next_node(n)) {
  3443. std::size_t key_hash = this->hash(this->get_key(n));
  3444. this->add_node_unique(
  3445. boost::unordered::detail::func::construct_node(
  3446. this->node_alloc(), n->value()),
  3447. key_hash);
  3448. }
  3449. }
  3450. void assign_buckets(table const& src, true_type)
  3451. {
  3452. node_holder<node_allocator> holder(*this);
  3453. for (node_pointer n = src.begin(); n; n = next_node(n)) {
  3454. std::size_t key_hash = this->hash(this->get_key(n));
  3455. this->add_node_unique(holder.copy_of(n->value()), key_hash);
  3456. }
  3457. }
  3458. void move_assign_buckets(table& src, true_type)
  3459. {
  3460. node_holder<node_allocator> holder(*this);
  3461. for (node_pointer n = src.begin(); n; n = next_node(n)) {
  3462. std::size_t key_hash = this->hash(this->get_key(n));
  3463. this->add_node_unique(holder.move_copy_of(n->value()), key_hash);
  3464. }
  3465. }
  3466. ////////////////////////////////////////////////////////////////////////
  3467. // Equivalent keys
  3468. // Equality
  3469. bool equals_equiv(table const& other) const
  3470. {
  3471. if (this->size_ != other.size_)
  3472. return false;
  3473. for (node_pointer n1 = this->begin(); n1;) {
  3474. node_pointer n2 = other.find_node(other.get_key(n1));
  3475. if (!n2)
  3476. return false;
  3477. node_pointer end1 = next_group(n1);
  3478. node_pointer end2 = next_group(n2);
  3479. if (!group_equals_equiv(n1, end1, n2, end2))
  3480. return false;
  3481. n1 = end1;
  3482. }
  3483. return true;
  3484. }
  3485. static bool group_equals_equiv(node_pointer n1, node_pointer end1,
  3486. node_pointer n2, node_pointer end2)
  3487. {
  3488. for (;;) {
  3489. if (n1->value() != n2->value())
  3490. break;
  3491. n1 = next_node(n1);
  3492. n2 = next_node(n2);
  3493. if (n1 == end1)
  3494. return n2 == end2;
  3495. if (n2 == end2)
  3496. return false;
  3497. }
  3498. for (node_pointer n1a = n1, n2a = n2;;) {
  3499. n1a = next_node(n1a);
  3500. n2a = next_node(n2a);
  3501. if (n1a == end1) {
  3502. if (n2a == end2)
  3503. break;
  3504. else
  3505. return false;
  3506. }
  3507. if (n2a == end2)
  3508. return false;
  3509. }
  3510. node_pointer start = n1;
  3511. for (; n1 != end1; n1 = next_node(n1)) {
  3512. value_type const& v = n1->value();
  3513. if (!find_equiv(start, n1, v)) {
  3514. std::size_t matches = count_equal_equiv(n2, end2, v);
  3515. if (!matches)
  3516. return false;
  3517. if (matches != 1 + count_equal_equiv(next_node(n1), end1, v))
  3518. return false;
  3519. }
  3520. }
  3521. return true;
  3522. }
  3523. static bool find_equiv(
  3524. node_pointer n, node_pointer end, value_type const& v)
  3525. {
  3526. for (; n != end; n = next_node(n))
  3527. if (n->value() == v)
  3528. return true;
  3529. return false;
  3530. }
  3531. static std::size_t count_equal_equiv(
  3532. node_pointer n, node_pointer end, value_type const& v)
  3533. {
  3534. std::size_t count = 0;
  3535. for (; n != end; n = next_node(n))
  3536. if (n->value() == v)
  3537. ++count;
  3538. return count;
  3539. }
  3540. // Emplace/Insert
  3541. inline node_pointer add_node_equiv(
  3542. node_pointer n, std::size_t key_hash, node_pointer pos)
  3543. {
  3544. std::size_t bucket_index = this->hash_to_bucket(key_hash);
  3545. n->bucket_info_ = bucket_index;
  3546. if (pos) {
  3547. n->reset_first_in_group();
  3548. n->next_ = pos->next_;
  3549. pos->next_ = n;
  3550. if (n->next_) {
  3551. std::size_t next_bucket = this->node_bucket(next_node(n));
  3552. if (next_bucket != bucket_index) {
  3553. this->get_bucket_pointer(next_bucket)->next_ = n;
  3554. }
  3555. }
  3556. } else {
  3557. n->set_first_in_group();
  3558. bucket_pointer b = this->get_bucket_pointer(bucket_index);
  3559. if (!b->next_) {
  3560. link_pointer start_node = this->get_previous_start();
  3561. if (start_node->next_) {
  3562. this
  3563. ->get_bucket_pointer(this->node_bucket(next_node(start_node)))
  3564. ->next_ = n;
  3565. }
  3566. b->next_ = start_node;
  3567. n->next_ = start_node->next_;
  3568. start_node->next_ = n;
  3569. } else {
  3570. n->next_ = b->next_->next_;
  3571. b->next_->next_ = n;
  3572. }
  3573. }
  3574. ++this->size_;
  3575. return n;
  3576. }
  3577. inline node_pointer add_using_hint_equiv(
  3578. node_pointer n, node_pointer hint)
  3579. {
  3580. n->bucket_info_ = hint->bucket_info_;
  3581. n->reset_first_in_group();
  3582. n->next_ = hint->next_;
  3583. hint->next_ = n;
  3584. if (n->next_) {
  3585. std::size_t next_bucket = this->node_bucket(next_node(n));
  3586. if (next_bucket != this->node_bucket(n)) {
  3587. this->get_bucket_pointer(next_bucket)->next_ = n;
  3588. }
  3589. }
  3590. ++this->size_;
  3591. return n;
  3592. }
  3593. iterator emplace_equiv(node_pointer n)
  3594. {
  3595. node_tmp a(n, this->node_alloc());
  3596. const_key_type& k = this->get_key(a.node_);
  3597. std::size_t key_hash = this->hash(k);
  3598. node_pointer position = this->find_node(key_hash, k);
  3599. this->reserve_for_insert(this->size_ + 1);
  3600. return iterator(
  3601. this->add_node_equiv(a.release(), key_hash, position));
  3602. }
  3603. iterator emplace_hint_equiv(c_iterator hint, node_pointer n)
  3604. {
  3605. node_tmp a(n, this->node_alloc());
  3606. const_key_type& k = this->get_key(a.node_);
  3607. if (hint.node_ && this->key_eq()(k, this->get_key(hint.node_))) {
  3608. this->reserve_for_insert(this->size_ + 1);
  3609. return iterator(
  3610. this->add_using_hint_equiv(a.release(), hint.node_));
  3611. } else {
  3612. std::size_t key_hash = this->hash(k);
  3613. node_pointer position = this->find_node(key_hash, k);
  3614. this->reserve_for_insert(this->size_ + 1);
  3615. return iterator(
  3616. this->add_node_equiv(a.release(), key_hash, position));
  3617. }
  3618. }
  3619. void emplace_no_rehash_equiv(node_pointer n)
  3620. {
  3621. node_tmp a(n, this->node_alloc());
  3622. const_key_type& k = this->get_key(a.node_);
  3623. std::size_t key_hash = this->hash(k);
  3624. node_pointer position = this->find_node(key_hash, k);
  3625. this->add_node_equiv(a.release(), key_hash, position);
  3626. }
  3627. template <typename NodeType>
  3628. iterator move_insert_node_type_equiv(NodeType& np)
  3629. {
  3630. iterator result;
  3631. if (np) {
  3632. const_key_type& k = this->get_key(np.ptr_);
  3633. std::size_t key_hash = this->hash(k);
  3634. node_pointer pos = this->find_node(key_hash, k);
  3635. this->reserve_for_insert(this->size_ + 1);
  3636. result = iterator(this->add_node_equiv(np.ptr_, key_hash, pos));
  3637. np.ptr_ = node_pointer();
  3638. }
  3639. return result;
  3640. }
  3641. template <typename NodeType>
  3642. iterator move_insert_node_type_with_hint_equiv(
  3643. c_iterator hint, NodeType& np)
  3644. {
  3645. iterator result;
  3646. if (np) {
  3647. const_key_type& k = this->get_key(np.ptr_);
  3648. if (hint.node_ && this->key_eq()(k, this->get_key(hint.node_))) {
  3649. this->reserve_for_insert(this->size_ + 1);
  3650. result =
  3651. iterator(this->add_using_hint_equiv(np.ptr_, hint.node_));
  3652. } else {
  3653. std::size_t key_hash = this->hash(k);
  3654. node_pointer pos = this->find_node(key_hash, k);
  3655. this->reserve_for_insert(this->size_ + 1);
  3656. result = iterator(this->add_node_equiv(np.ptr_, key_hash, pos));
  3657. }
  3658. np.ptr_ = node_pointer();
  3659. }
  3660. return result;
  3661. }
  3662. ////////////////////////////////////////////////////////////////////////
  3663. // Insert range methods
  3664. // if hash function throws, or inserting > 1 element, basic exception
  3665. // safety. Strong otherwise
  3666. template <class I>
  3667. void insert_range_equiv(I i, I j,
  3668. typename boost::unordered::detail::enable_if_forward<I, void*>::type =
  3669. 0)
  3670. {
  3671. if (i == j)
  3672. return;
  3673. std::size_t distance = static_cast<std::size_t>(std::distance(i, j));
  3674. if (distance == 1) {
  3675. emplace_equiv(boost::unordered::detail::func::construct_node(
  3676. this->node_alloc(), *i));
  3677. } else {
  3678. // Only require basic exception safety here
  3679. this->reserve_for_insert(this->size_ + distance);
  3680. for (; i != j; ++i) {
  3681. emplace_no_rehash_equiv(
  3682. boost::unordered::detail::func::construct_node(
  3683. this->node_alloc(), *i));
  3684. }
  3685. }
  3686. }
  3687. template <class I>
  3688. void insert_range_equiv(I i, I j,
  3689. typename boost::unordered::detail::disable_if_forward<I,
  3690. void*>::type = 0)
  3691. {
  3692. for (; i != j; ++i) {
  3693. emplace_equiv(boost::unordered::detail::func::construct_node(
  3694. this->node_alloc(), *i));
  3695. }
  3696. }
  3697. ////////////////////////////////////////////////////////////////////////
  3698. // Extract
  3699. inline node_pointer extract_by_iterator_equiv(c_iterator n)
  3700. {
  3701. node_pointer i = n.node_;
  3702. BOOST_ASSERT(i);
  3703. node_pointer j(next_node(i));
  3704. std::size_t bucket_index = this->node_bucket(i);
  3705. link_pointer prev = this->get_previous_start(bucket_index);
  3706. while (prev->next_ != i) {
  3707. prev = next_node(prev);
  3708. }
  3709. prev->next_ = j;
  3710. if (j && i->is_first_in_group()) {
  3711. j->set_first_in_group();
  3712. }
  3713. --this->size_;
  3714. this->fix_bucket(bucket_index, prev, j);
  3715. i->next_ = link_pointer();
  3716. return i;
  3717. }
  3718. ////////////////////////////////////////////////////////////////////////
  3719. // Erase
  3720. //
  3721. // no throw
  3722. std::size_t erase_key_equiv(const_key_type& k)
  3723. {
  3724. if (!this->size_)
  3725. return 0;
  3726. std::size_t key_hash = this->hash(k);
  3727. std::size_t bucket_index = this->hash_to_bucket(key_hash);
  3728. link_pointer prev = this->find_previous_node(k, bucket_index);
  3729. if (!prev)
  3730. return 0;
  3731. std::size_t deleted_count = 0;
  3732. node_pointer n = next_node(prev);
  3733. do {
  3734. node_pointer n2 = next_node(n);
  3735. destroy_node(n);
  3736. ++deleted_count;
  3737. n = n2;
  3738. } while (n && !n->is_first_in_group());
  3739. size_ -= deleted_count;
  3740. prev->next_ = n;
  3741. this->fix_bucket(bucket_index, prev, n);
  3742. return deleted_count;
  3743. }
  3744. link_pointer erase_nodes_equiv(node_pointer i, node_pointer j)
  3745. {
  3746. std::size_t bucket_index = this->node_bucket(i);
  3747. link_pointer prev = this->get_previous_start(bucket_index);
  3748. while (prev->next_ != i) {
  3749. prev = next_node(prev);
  3750. }
  3751. // Delete the nodes.
  3752. // Is it inefficient to call fix_bucket for every node?
  3753. bool includes_first = false;
  3754. prev->next_ = j;
  3755. do {
  3756. includes_first = includes_first || i->is_first_in_group();
  3757. node_pointer next = next_node(i);
  3758. destroy_node(i);
  3759. --size_;
  3760. bucket_index = this->fix_bucket(bucket_index, prev, next);
  3761. i = next;
  3762. } while (i != j);
  3763. if (j && includes_first) {
  3764. j->set_first_in_group();
  3765. }
  3766. return prev;
  3767. }
  3768. ////////////////////////////////////////////////////////////////////////
  3769. // fill_buckets
  3770. void copy_buckets(table const& src, false_type)
  3771. {
  3772. this->create_buckets(this->bucket_count_);
  3773. for (node_pointer n = src.begin(); n;) {
  3774. std::size_t key_hash = this->hash(this->get_key(n));
  3775. node_pointer group_end(next_group(n));
  3776. node_pointer pos = this->add_node_equiv(
  3777. boost::unordered::detail::func::construct_node(
  3778. this->node_alloc(), n->value()),
  3779. key_hash, node_pointer());
  3780. for (n = next_node(n); n != group_end; n = next_node(n)) {
  3781. this->add_node_equiv(
  3782. boost::unordered::detail::func::construct_node(
  3783. this->node_alloc(), n->value()),
  3784. key_hash, pos);
  3785. }
  3786. }
  3787. }
  3788. void assign_buckets(table const& src, false_type)
  3789. {
  3790. node_holder<node_allocator> holder(*this);
  3791. for (node_pointer n = src.begin(); n;) {
  3792. std::size_t key_hash = this->hash(this->get_key(n));
  3793. node_pointer group_end(next_group(n));
  3794. node_pointer pos = this->add_node_equiv(
  3795. holder.copy_of(n->value()), key_hash, node_pointer());
  3796. for (n = next_node(n); n != group_end; n = next_node(n)) {
  3797. this->add_node_equiv(holder.copy_of(n->value()), key_hash, pos);
  3798. }
  3799. }
  3800. }
  3801. void move_assign_buckets(table& src, false_type)
  3802. {
  3803. node_holder<node_allocator> holder(*this);
  3804. for (node_pointer n = src.begin(); n;) {
  3805. std::size_t key_hash = this->hash(this->get_key(n));
  3806. node_pointer group_end(next_group(n));
  3807. node_pointer pos = this->add_node_equiv(
  3808. holder.move_copy_of(n->value()), key_hash, node_pointer());
  3809. for (n = next_node(n); n != group_end; n = next_node(n)) {
  3810. this->add_node_equiv(
  3811. holder.move_copy_of(n->value()), key_hash, pos);
  3812. }
  3813. }
  3814. }
  3815. };
  3816. //////////////////////////////////////////////////////////////////////////
  3817. // Clear
  3818. template <typename Types> inline void table<Types>::clear_impl()
  3819. {
  3820. if (size_) {
  3821. bucket_pointer end = get_bucket_pointer(bucket_count_);
  3822. for (bucket_pointer it = buckets_; it != end; ++it) {
  3823. it->next_ = node_pointer();
  3824. }
  3825. link_pointer prev = end->first_from_start();
  3826. node_pointer n = next_node(prev);
  3827. prev->next_ = node_pointer();
  3828. size_ = 0;
  3829. while (n) {
  3830. node_pointer next = next_node(n);
  3831. destroy_node(n);
  3832. n = next;
  3833. }
  3834. }
  3835. }
  3836. //////////////////////////////////////////////////////////////////////////
  3837. // Reserve & Rehash
  3838. // basic exception safety
  3839. template <typename Types>
  3840. inline void table<Types>::reserve_for_insert(std::size_t size)
  3841. {
  3842. if (!buckets_) {
  3843. create_buckets((std::max)(bucket_count_, min_buckets_for_size(size)));
  3844. } else if (size > max_load_) {
  3845. std::size_t num_buckets =
  3846. min_buckets_for_size((std::max)(size, size_ + (size_ >> 1)));
  3847. if (num_buckets != bucket_count_)
  3848. this->rehash_impl(num_buckets);
  3849. }
  3850. }
  3851. // if hash function throws, basic exception safety
  3852. // strong otherwise.
  3853. template <typename Types>
  3854. inline void table<Types>::rehash(std::size_t min_buckets)
  3855. {
  3856. using namespace std;
  3857. if (!size_) {
  3858. delete_buckets();
  3859. bucket_count_ = policy::new_bucket_count(min_buckets);
  3860. } else {
  3861. min_buckets = policy::new_bucket_count((std::max)(min_buckets,
  3862. boost::unordered::detail::double_to_size(
  3863. floor(static_cast<double>(size_) / static_cast<double>(mlf_))) +
  3864. 1));
  3865. if (min_buckets != bucket_count_)
  3866. this->rehash_impl(min_buckets);
  3867. }
  3868. }
  3869. template <typename Types>
  3870. inline void table<Types>::rehash_impl(std::size_t num_buckets)
  3871. {
  3872. BOOST_ASSERT(this->buckets_);
  3873. this->create_buckets(num_buckets);
  3874. link_pointer prev = this->get_previous_start();
  3875. BOOST_TRY
  3876. {
  3877. while (prev->next_) {
  3878. node_pointer n = next_node(prev);
  3879. std::size_t key_hash = this->hash(this->get_key(n));
  3880. std::size_t bucket_index = this->hash_to_bucket(key_hash);
  3881. n->bucket_info_ = bucket_index;
  3882. n->set_first_in_group();
  3883. // Iterator through the rest of the group of equal nodes,
  3884. // setting the bucket.
  3885. for (;;) {
  3886. node_pointer next = next_node(n);
  3887. if (!next || next->is_first_in_group()) {
  3888. break;
  3889. }
  3890. n = next;
  3891. n->bucket_info_ = bucket_index;
  3892. n->reset_first_in_group();
  3893. }
  3894. // n is now the last node in the group
  3895. bucket_pointer b = this->get_bucket_pointer(bucket_index);
  3896. if (!b->next_) {
  3897. b->next_ = prev;
  3898. prev = n;
  3899. } else {
  3900. link_pointer next = n->next_;
  3901. n->next_ = b->next_->next_;
  3902. b->next_->next_ = prev->next_;
  3903. prev->next_ = next;
  3904. }
  3905. }
  3906. }
  3907. BOOST_CATCH(...)
  3908. {
  3909. node_pointer n = next_node(prev);
  3910. prev->next_ = node_pointer();
  3911. while (n) {
  3912. node_pointer next = next_node(n);
  3913. destroy_node(n);
  3914. --size_;
  3915. n = next;
  3916. }
  3917. BOOST_RETHROW
  3918. }
  3919. BOOST_CATCH_END
  3920. }
  3921. #if defined(BOOST_MSVC)
  3922. #pragma warning(pop)
  3923. #endif
  3924. ////////////////////////////////////////////////////////////////////////
  3925. // key extractors
  3926. //
  3927. // no throw
  3928. //
  3929. // 'extract_key' is called with the emplace parameters to return a
  3930. // key if available or 'no_key' is one isn't and will need to be
  3931. // constructed. This could be done by overloading the emplace
  3932. // implementation
  3933. // for the different cases, but that's a bit tricky on compilers without
  3934. // variadic templates.
  3935. template <typename Key, typename T> struct is_key
  3936. {
  3937. template <typename T2> static choice1::type test(T2 const&);
  3938. static choice2::type test(Key const&);
  3939. enum
  3940. {
  3941. value = sizeof(test(boost::unordered::detail::make<T>())) ==
  3942. sizeof(choice2::type)
  3943. };
  3944. typedef typename boost::detail::if_true<value>::BOOST_NESTED_TEMPLATE
  3945. then<Key const&, no_key>::type type;
  3946. };
  3947. template <class ValueType> struct set_extractor
  3948. {
  3949. typedef ValueType value_type;
  3950. typedef ValueType key_type;
  3951. static key_type const& extract(value_type const& v) { return v; }
  3952. static key_type const& extract(BOOST_UNORDERED_RV_REF(value_type) v)
  3953. {
  3954. return v;
  3955. }
  3956. static no_key extract() { return no_key(); }
  3957. template <class Arg> static no_key extract(Arg const&)
  3958. {
  3959. return no_key();
  3960. }
  3961. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  3962. template <class Arg1, class Arg2, class... Args>
  3963. static no_key extract(Arg1 const&, Arg2 const&, Args const&...)
  3964. {
  3965. return no_key();
  3966. }
  3967. #else
  3968. template <class Arg1, class Arg2>
  3969. static no_key extract(Arg1 const&, Arg2 const&)
  3970. {
  3971. return no_key();
  3972. }
  3973. #endif
  3974. };
  3975. template <class ValueType> struct map_extractor
  3976. {
  3977. typedef ValueType value_type;
  3978. typedef typename boost::remove_const<typename boost::unordered::detail::
  3979. pair_traits<ValueType>::first_type>::type key_type;
  3980. static key_type const& extract(value_type const& v) { return v.first; }
  3981. template <class Second>
  3982. static key_type const& extract(std::pair<key_type, Second> const& v)
  3983. {
  3984. return v.first;
  3985. }
  3986. template <class Second>
  3987. static key_type const& extract(
  3988. std::pair<key_type const, Second> const& v)
  3989. {
  3990. return v.first;
  3991. }
  3992. #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  3993. template <class Second>
  3994. static key_type const& extract(
  3995. boost::rv<std::pair<key_type, Second> > const& v)
  3996. {
  3997. return v.first;
  3998. }
  3999. template <class Second>
  4000. static key_type const& extract(
  4001. boost::rv<std::pair<key_type const, Second> > const& v)
  4002. {
  4003. return v.first;
  4004. }
  4005. #endif
  4006. template <class Arg1>
  4007. static key_type const& extract(key_type const& k, Arg1 const&)
  4008. {
  4009. return k;
  4010. }
  4011. static no_key extract() { return no_key(); }
  4012. template <class Arg> static no_key extract(Arg const&)
  4013. {
  4014. return no_key();
  4015. }
  4016. template <class Arg1, class Arg2>
  4017. static no_key extract(Arg1 const&, Arg2 const&)
  4018. {
  4019. return no_key();
  4020. }
  4021. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  4022. template <class Arg1, class Arg2, class Arg3, class... Args>
  4023. static no_key extract(
  4024. Arg1 const&, Arg2 const&, Arg3 const&, Args const&...)
  4025. {
  4026. return no_key();
  4027. }
  4028. #endif
  4029. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  4030. #define BOOST_UNORDERED_KEY_FROM_TUPLE(namespace_) \
  4031. template <typename T2> \
  4032. static no_key extract(boost::unordered::piecewise_construct_t, \
  4033. namespace_ tuple<> const&, T2 const&) \
  4034. { \
  4035. return no_key(); \
  4036. } \
  4037. \
  4038. template <typename T, typename T2> \
  4039. static typename is_key<key_type, T>::type extract( \
  4040. boost::unordered::piecewise_construct_t, namespace_ tuple<T> const& k, \
  4041. T2 const&) \
  4042. { \
  4043. return typename is_key<key_type, T>::type(namespace_ get<0>(k)); \
  4044. }
  4045. #else
  4046. #define BOOST_UNORDERED_KEY_FROM_TUPLE(namespace_) \
  4047. static no_key extract( \
  4048. boost::unordered::piecewise_construct_t, namespace_ tuple<> const&) \
  4049. { \
  4050. return no_key(); \
  4051. } \
  4052. \
  4053. template <typename T> \
  4054. static typename is_key<key_type, T>::type extract( \
  4055. boost::unordered::piecewise_construct_t, namespace_ tuple<T> const& k) \
  4056. { \
  4057. return typename is_key<key_type, T>::type(namespace_ get<0>(k)); \
  4058. }
  4059. #endif
  4060. BOOST_UNORDERED_KEY_FROM_TUPLE(boost::)
  4061. #if BOOST_UNORDERED_TUPLE_ARGS
  4062. BOOST_UNORDERED_KEY_FROM_TUPLE(std::)
  4063. #endif
  4064. #undef BOOST_UNORDERED_KEY_FROM_TUPLE
  4065. };
  4066. ////////////////////////////////////////////////////////////////////////
  4067. // Unique nodes
  4068. template <typename A, typename T>
  4069. struct node : boost::unordered::detail::value_base<T>
  4070. {
  4071. typedef
  4072. typename ::boost::unordered::detail::rebind_wrap<A, node<A, T> >::type
  4073. allocator;
  4074. typedef typename ::boost::unordered::detail::allocator_traits<
  4075. allocator>::pointer node_pointer;
  4076. typedef node_pointer link_pointer;
  4077. typedef typename ::boost::unordered::detail::rebind_wrap<A,
  4078. bucket<node_pointer> >::type bucket_allocator;
  4079. typedef typename ::boost::unordered::detail::allocator_traits<
  4080. bucket_allocator>::pointer bucket_pointer;
  4081. link_pointer next_;
  4082. std::size_t bucket_info_;
  4083. node() : next_(), bucket_info_(0) {}
  4084. std::size_t get_bucket() const
  4085. {
  4086. return bucket_info_ & ((std::size_t)-1 >> 1);
  4087. }
  4088. std::size_t is_first_in_group() const
  4089. {
  4090. return !(bucket_info_ & ~((std::size_t)-1 >> 1));
  4091. }
  4092. void set_first_in_group()
  4093. {
  4094. bucket_info_ = bucket_info_ & ((std::size_t)-1 >> 1);
  4095. }
  4096. void reset_first_in_group()
  4097. {
  4098. bucket_info_ = bucket_info_ | ~((std::size_t)-1 >> 1);
  4099. }
  4100. private:
  4101. node& operator=(node const&);
  4102. };
  4103. template <typename T>
  4104. struct ptr_node : boost::unordered::detail::ptr_bucket
  4105. {
  4106. typedef T value_type;
  4107. typedef boost::unordered::detail::ptr_bucket bucket_base;
  4108. typedef ptr_node<T>* node_pointer;
  4109. typedef ptr_bucket* link_pointer;
  4110. typedef ptr_bucket* bucket_pointer;
  4111. std::size_t bucket_info_;
  4112. boost::unordered::detail::value_base<T> value_base_;
  4113. ptr_node() : bucket_base(), bucket_info_(0) {}
  4114. void* address() { return value_base_.address(); }
  4115. value_type& value() { return value_base_.value(); }
  4116. value_type* value_ptr() { return value_base_.value_ptr(); }
  4117. std::size_t get_bucket() const
  4118. {
  4119. return bucket_info_ & ((std::size_t)-1 >> 1);
  4120. }
  4121. std::size_t is_first_in_group() const
  4122. {
  4123. return !(bucket_info_ & ~((std::size_t)-1 >> 1));
  4124. }
  4125. void set_first_in_group()
  4126. {
  4127. bucket_info_ = bucket_info_ & ((std::size_t)-1 >> 1);
  4128. }
  4129. void reset_first_in_group()
  4130. {
  4131. bucket_info_ = bucket_info_ | ~((std::size_t)-1 >> 1);
  4132. }
  4133. private:
  4134. ptr_node& operator=(ptr_node const&);
  4135. };
  4136. // If the allocator uses raw pointers use ptr_node
  4137. // Otherwise use node.
  4138. template <typename A, typename T, typename NodePtr, typename BucketPtr>
  4139. struct pick_node2
  4140. {
  4141. typedef boost::unordered::detail::node<A, T> node;
  4142. typedef typename boost::unordered::detail::allocator_traits<
  4143. typename boost::unordered::detail::rebind_wrap<A,
  4144. node>::type>::pointer node_pointer;
  4145. typedef boost::unordered::detail::bucket<node_pointer> bucket;
  4146. typedef node_pointer link_pointer;
  4147. };
  4148. template <typename A, typename T>
  4149. struct pick_node2<A, T, boost::unordered::detail::ptr_node<T>*,
  4150. boost::unordered::detail::ptr_bucket*>
  4151. {
  4152. typedef boost::unordered::detail::ptr_node<T> node;
  4153. typedef boost::unordered::detail::ptr_bucket bucket;
  4154. typedef bucket* link_pointer;
  4155. };
  4156. template <typename A, typename T> struct pick_node
  4157. {
  4158. typedef typename boost::remove_const<T>::type nonconst;
  4159. typedef boost::unordered::detail::allocator_traits<
  4160. typename boost::unordered::detail::rebind_wrap<A,
  4161. boost::unordered::detail::ptr_node<nonconst> >::type>
  4162. tentative_node_traits;
  4163. typedef boost::unordered::detail::allocator_traits<
  4164. typename boost::unordered::detail::rebind_wrap<A,
  4165. boost::unordered::detail::ptr_bucket>::type>
  4166. tentative_bucket_traits;
  4167. typedef pick_node2<A, nonconst, typename tentative_node_traits::pointer,
  4168. typename tentative_bucket_traits::pointer>
  4169. pick;
  4170. typedef typename pick::node node;
  4171. typedef typename pick::bucket bucket;
  4172. typedef typename pick::link_pointer link_pointer;
  4173. };
  4174. }
  4175. }
  4176. }
  4177. #undef BOOST_UNORDERED_EMPLACE_TEMPLATE
  4178. #undef BOOST_UNORDERED_EMPLACE_ARGS
  4179. #undef BOOST_UNORDERED_EMPLACE_FORWARD
  4180. #undef BOOST_UNORDERED_CALL_CONSTRUCT1
  4181. #undef BOOST_UNORDERED_CALL_DESTROY
  4182. #endif