implementation.hpp 160 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952
  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