hashtable.hpp 126 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2006-2013
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/intrusive for documentation.
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. #ifndef BOOST_INTRUSIVE_HASHTABLE_HPP
  13. #define BOOST_INTRUSIVE_HASHTABLE_HPP
  14. #include <boost/intrusive/detail/config_begin.hpp>
  15. //std C++
  16. #include <functional> //std::equal_to
  17. #include <utility> //std::pair
  18. #include <algorithm> //std::swap, std::lower_bound, std::upper_bound
  19. #include <cstddef> //std::size_t
  20. //boost
  21. #include <boost/intrusive/detail/assert.hpp>
  22. #include <boost/static_assert.hpp>
  23. #include <boost/functional/hash.hpp>
  24. #include <boost/pointer_cast.hpp>
  25. //General intrusive utilities
  26. #include <boost/intrusive/intrusive_fwd.hpp>
  27. #include <boost/intrusive/detail/hashtable_node.hpp>
  28. #include <boost/intrusive/detail/transform_iterator.hpp>
  29. #include <boost/intrusive/link_mode.hpp>
  30. #include <boost/intrusive/detail/ebo_functor_holder.hpp>
  31. #include <boost/intrusive/detail/clear_on_destructor_base.hpp>
  32. #include <boost/intrusive/detail/utilities.hpp>
  33. //Implementation utilities
  34. #include <boost/intrusive/unordered_set_hook.hpp>
  35. #include <boost/intrusive/slist.hpp>
  36. #include <boost/intrusive/pointer_traits.hpp>
  37. #include <boost/intrusive/detail/mpl.hpp>
  38. #include <boost/move/move.hpp>
  39. namespace boost {
  40. namespace intrusive {
  41. /// @cond
  42. struct hash_bool_flags
  43. {
  44. static const std::size_t unique_keys_pos = 1u;
  45. static const std::size_t constant_time_size_pos = 2u;
  46. static const std::size_t power_2_buckets_pos = 4u;
  47. static const std::size_t cache_begin_pos = 8u;
  48. static const std::size_t compare_hash_pos = 16u;
  49. static const std::size_t incremental_pos = 32u;
  50. };
  51. namespace detail {
  52. template<class SupposedValueTraits>
  53. struct get_slist_impl_from_supposed_value_traits
  54. {
  55. typedef typename detail::get_real_value_traits
  56. <SupposedValueTraits>::type real_value_traits;
  57. typedef typename detail::get_node_traits
  58. <real_value_traits>::type node_traits;
  59. typedef typename get_slist_impl
  60. <typename reduced_slist_node_traits
  61. <node_traits>::type
  62. >::type type;
  63. };
  64. template<class SupposedValueTraits>
  65. struct unordered_bucket_impl
  66. {
  67. typedef typename
  68. get_slist_impl_from_supposed_value_traits
  69. <SupposedValueTraits>::type slist_impl;
  70. typedef detail::bucket_impl<slist_impl> implementation_defined;
  71. typedef implementation_defined type;
  72. };
  73. template<class SupposedValueTraits>
  74. struct unordered_bucket_ptr_impl
  75. {
  76. typedef typename detail::get_node_traits
  77. <SupposedValueTraits>::type::node_ptr node_ptr;
  78. typedef typename unordered_bucket_impl
  79. <SupposedValueTraits>::type bucket_type;
  80. typedef typename pointer_traits
  81. <node_ptr>::template rebind_pointer
  82. < bucket_type >::type implementation_defined;
  83. typedef implementation_defined type;
  84. };
  85. template <class T>
  86. struct store_hash_bool
  87. {
  88. template<bool Add>
  89. struct two_or_three {one _[2 + Add];};
  90. template <class U> static one test(...);
  91. template <class U> static two_or_three<U::store_hash> test (int);
  92. static const std::size_t value = sizeof(test<T>(0));
  93. };
  94. template <class T>
  95. struct store_hash_is_true
  96. {
  97. static const bool value = store_hash_bool<T>::value > sizeof(one)*2;
  98. };
  99. template <class T>
  100. struct optimize_multikey_bool
  101. {
  102. template<bool Add>
  103. struct two_or_three {one _[2 + Add];};
  104. template <class U> static one test(...);
  105. template <class U> static two_or_three<U::optimize_multikey> test (int);
  106. static const std::size_t value = sizeof(test<T>(0));
  107. };
  108. template <class T>
  109. struct optimize_multikey_is_true
  110. {
  111. static const bool value = optimize_multikey_bool<T>::value > sizeof(one)*2;
  112. };
  113. struct insert_commit_data_impl
  114. {
  115. std::size_t hash;
  116. };
  117. template<class Node, class SlistNodePtr>
  118. inline typename pointer_traits<SlistNodePtr>::template rebind_pointer<Node>::type
  119. dcast_bucket_ptr(const SlistNodePtr &p)
  120. {
  121. typedef typename pointer_traits<SlistNodePtr>::template rebind_pointer<Node>::type node_ptr;
  122. return pointer_traits<node_ptr>::pointer_to(static_cast<Node&>(*p));
  123. }
  124. template<class NodeTraits>
  125. struct group_functions
  126. {
  127. typedef NodeTraits node_traits;
  128. typedef unordered_group_adapter<node_traits> group_traits;
  129. typedef typename node_traits::node_ptr node_ptr;
  130. typedef typename node_traits::node node;
  131. typedef typename reduced_slist_node_traits
  132. <node_traits>::type reduced_node_traits;
  133. typedef typename reduced_node_traits::node_ptr slist_node_ptr;
  134. typedef typename reduced_node_traits::node slist_node;
  135. typedef circular_slist_algorithms<group_traits> group_algorithms;
  136. static slist_node_ptr get_bucket_before_begin
  137. (const slist_node_ptr &bucket_beg, const slist_node_ptr &bucket_end, const node_ptr &p)
  138. {
  139. //First find the last node of p's group.
  140. //This requires checking the first node of the next group or
  141. //the bucket node.
  142. node_ptr prev_node = p;
  143. node_ptr nxt(node_traits::get_next(p));
  144. while(!(bucket_beg <= nxt && nxt <= bucket_end) &&
  145. (group_traits::get_next(nxt) == prev_node)){
  146. prev_node = nxt;
  147. nxt = node_traits::get_next(nxt);
  148. }
  149. //If we've reached the bucket node just return it.
  150. if(bucket_beg <= nxt && nxt <= bucket_end){
  151. return nxt;
  152. }
  153. //Otherwise, iterate using group links until the bucket node
  154. node_ptr first_node_of_group = nxt;
  155. node_ptr last_node_group = group_traits::get_next(first_node_of_group);
  156. slist_node_ptr possible_end = node_traits::get_next(last_node_group);
  157. while(!(bucket_beg <= possible_end && possible_end <= bucket_end)){
  158. first_node_of_group = detail::dcast_bucket_ptr<node>(possible_end);
  159. last_node_group = group_traits::get_next(first_node_of_group);
  160. possible_end = node_traits::get_next(last_node_group);
  161. }
  162. return possible_end;
  163. }
  164. static node_ptr get_prev_to_first_in_group(const slist_node_ptr &bucket_node, const node_ptr &first_in_group)
  165. {
  166. //Just iterate using group links and obtain the node
  167. //before "first_in_group)"
  168. node_ptr prev_node = detail::dcast_bucket_ptr<node>(bucket_node);
  169. node_ptr nxt(node_traits::get_next(prev_node));
  170. while(nxt != first_in_group){
  171. prev_node = group_traits::get_next(nxt);
  172. nxt = node_traits::get_next(prev_node);
  173. }
  174. return prev_node;
  175. }
  176. static node_ptr get_first_in_group_of_last_in_group(const node_ptr &last_in_group)
  177. {
  178. //Just iterate using group links and obtain the node
  179. //before "last_in_group"
  180. node_ptr possible_first = group_traits::get_next(last_in_group);
  181. node_ptr possible_first_prev = group_traits::get_next(possible_first);
  182. // The deleted node is at the end of the group, so the
  183. // node in the group pointing to it is at the beginning
  184. // of the group. Find that to change its pointer.
  185. while(possible_first_prev != last_in_group){
  186. possible_first = possible_first_prev;
  187. possible_first_prev = group_traits::get_next(possible_first);
  188. }
  189. return possible_first;
  190. }
  191. static void erase_from_group(const slist_node_ptr &end_ptr, const node_ptr &to_erase_ptr, detail::true_)
  192. {
  193. node_ptr nxt_ptr(node_traits::get_next(to_erase_ptr));
  194. node_ptr prev_in_group_ptr(group_traits::get_next(to_erase_ptr));
  195. bool last_in_group = (end_ptr == nxt_ptr) ||
  196. (group_traits::get_next(nxt_ptr) != to_erase_ptr);
  197. bool is_first_in_group = node_traits::get_next(prev_in_group_ptr) != to_erase_ptr;
  198. if(is_first_in_group && last_in_group){
  199. group_algorithms::init(to_erase_ptr);
  200. }
  201. else if(is_first_in_group){
  202. group_algorithms::unlink_after(nxt_ptr);
  203. }
  204. else if(last_in_group){
  205. node_ptr first_in_group =
  206. get_first_in_group_of_last_in_group(to_erase_ptr);
  207. group_algorithms::unlink_after(first_in_group);
  208. }
  209. else{
  210. group_algorithms::unlink_after(nxt_ptr);
  211. }
  212. }
  213. static void erase_from_group(const slist_node_ptr&, const node_ptr&, detail::false_)
  214. {}
  215. static node_ptr get_last_in_group(const node_ptr &first_in_group, detail::true_)
  216. { return group_traits::get_next(first_in_group); }
  217. static node_ptr get_last_in_group(const node_ptr &n, detail::false_)
  218. { return n; }
  219. static void init_group(const node_ptr &n, true_)
  220. { group_algorithms::init(n); }
  221. static void init_group(const node_ptr &, false_)
  222. {}
  223. static void insert_in_group(const node_ptr &first_in_group, const node_ptr &n, true_)
  224. {
  225. if(first_in_group){
  226. if(group_algorithms::unique(first_in_group))
  227. group_algorithms::link_after(first_in_group, n);
  228. else{
  229. group_algorithms::link_after(group_algorithms::node_traits::get_next(first_in_group), n);
  230. }
  231. }
  232. else{
  233. group_algorithms::init_header(n);
  234. }
  235. }
  236. static slist_node_ptr get_previous_and_next_in_group
  237. ( const slist_node_ptr &i, node_ptr &nxt_in_group
  238. //If first_end_ptr == last_end_ptr, then first_end_ptr is the bucket of i
  239. //Otherwise first_end_ptr is the first bucket and last_end_ptr the last one.
  240. , const slist_node_ptr &first_end_ptr, const slist_node_ptr &last_end_ptr)
  241. {
  242. slist_node_ptr prev;
  243. node_ptr elem(detail::dcast_bucket_ptr<node>(i));
  244. //It's the last in group if the next_node is a bucket
  245. slist_node_ptr nxt(node_traits::get_next(elem));
  246. bool last_in_group = (first_end_ptr <= nxt && nxt <= last_end_ptr)/* ||
  247. (group_traits::get_next(nxt) != elem)*/;
  248. //It's the first in group if group_previous's next_node is not
  249. //itself, as group list does not link bucket
  250. node_ptr prev_in_group(group_traits::get_next(elem));
  251. bool first_in_group = node_traits::get_next(prev_in_group) != elem;
  252. if(first_in_group){
  253. node_ptr start_pos;
  254. if(last_in_group){
  255. start_pos = elem;
  256. nxt_in_group = node_ptr();
  257. }
  258. else{
  259. start_pos = prev_in_group;
  260. nxt_in_group = node_traits::get_next(elem);
  261. }
  262. slist_node_ptr bucket_node;
  263. if(first_end_ptr != last_end_ptr){
  264. bucket_node = group_functions::get_bucket_before_begin
  265. (first_end_ptr, last_end_ptr, start_pos);
  266. }
  267. else{
  268. bucket_node = first_end_ptr;
  269. }
  270. prev = group_functions::get_prev_to_first_in_group(bucket_node, elem);
  271. }
  272. else{
  273. if(last_in_group){
  274. nxt_in_group = group_functions::get_first_in_group_of_last_in_group(elem);
  275. }
  276. else{
  277. nxt_in_group = node_traits::get_next(elem);
  278. }
  279. prev = group_traits::get_next(elem);
  280. }
  281. return prev;
  282. }
  283. static void insert_in_group(const node_ptr&, const node_ptr&, false_)
  284. {}
  285. };
  286. template<class BucketType, class SplitTraits>
  287. class incremental_rehash_rollback
  288. {
  289. private:
  290. typedef BucketType bucket_type;
  291. typedef SplitTraits split_traits;
  292. incremental_rehash_rollback();
  293. incremental_rehash_rollback & operator=(const incremental_rehash_rollback &);
  294. incremental_rehash_rollback (const incremental_rehash_rollback &);
  295. public:
  296. incremental_rehash_rollback
  297. (bucket_type &source_bucket, bucket_type &destiny_bucket, split_traits &split_traits)
  298. : source_bucket_(source_bucket), destiny_bucket_(destiny_bucket)
  299. , split_traits_(split_traits), released_(false)
  300. {}
  301. void release()
  302. { released_ = true; }
  303. ~incremental_rehash_rollback()
  304. {
  305. if(!released_){
  306. //If an exception is thrown, just put all moved nodes back in the old bucket
  307. //and move back the split mark.
  308. destiny_bucket_.splice_after(destiny_bucket_.before_begin(), source_bucket_);
  309. split_traits_.decrement();
  310. }
  311. }
  312. private:
  313. bucket_type &source_bucket_;
  314. bucket_type &destiny_bucket_;
  315. split_traits &split_traits_;
  316. bool released_;
  317. };
  318. template<class NodeTraits>
  319. struct node_functions
  320. {
  321. static void store_hash(typename NodeTraits::node_ptr p, std::size_t h, true_)
  322. { return NodeTraits::set_hash(p, h); }
  323. static void store_hash(typename NodeTraits::node_ptr, std::size_t, false_)
  324. {}
  325. };
  326. inline std::size_t hash_to_bucket(std::size_t hash_value, std::size_t bucket_cnt, detail::false_)
  327. { return hash_value % bucket_cnt; }
  328. inline std::size_t hash_to_bucket(std::size_t hash_value, std::size_t bucket_cnt, detail::true_)
  329. { return hash_value & (bucket_cnt - 1); }
  330. template<bool Power2Buckets, bool Incremental>
  331. inline std::size_t hash_to_bucket_split(std::size_t hash_value, std::size_t bucket_cnt, std::size_t split)
  332. {
  333. std::size_t bucket_number = detail::hash_to_bucket(hash_value, bucket_cnt, detail::bool_<Power2Buckets>());
  334. if(Incremental)
  335. if(bucket_number >= split)
  336. bucket_number -= bucket_cnt/2;
  337. return bucket_number;
  338. }
  339. } //namespace detail {
  340. //!This metafunction will obtain the type of a bucket
  341. //!from the value_traits or hook option to be used with
  342. //!a hash container.
  343. template<class ValueTraitsOrHookOption>
  344. struct unordered_bucket
  345. : public detail::unordered_bucket_impl
  346. <typename ValueTraitsOrHookOption::
  347. template pack<none>::proto_value_traits
  348. >
  349. {};
  350. //!This metafunction will obtain the type of a bucket pointer
  351. //!from the value_traits or hook option to be used with
  352. //!a hash container.
  353. template<class ValueTraitsOrHookOption>
  354. struct unordered_bucket_ptr
  355. : public detail::unordered_bucket_ptr_impl
  356. <typename ValueTraitsOrHookOption::
  357. template pack<none>::proto_value_traits
  358. >
  359. {};
  360. //!This metafunction will obtain the type of the default bucket traits
  361. //!(when the user does not specify the bucket_traits<> option) from the
  362. //!value_traits or hook option to be used with
  363. //!a hash container.
  364. template<class ValueTraitsOrHookOption>
  365. struct unordered_default_bucket_traits
  366. {
  367. typedef typename ValueTraitsOrHookOption::
  368. template pack<none>::proto_value_traits supposed_value_traits;
  369. typedef typename detail::
  370. get_slist_impl_from_supposed_value_traits
  371. <supposed_value_traits>::type slist_impl;
  372. typedef detail::bucket_traits_impl
  373. <slist_impl> implementation_defined;
  374. typedef implementation_defined type;
  375. };
  376. struct default_bucket_traits;
  377. struct hashtable_defaults
  378. {
  379. typedef detail::default_hashtable_hook proto_value_traits;
  380. typedef std::size_t size_type;
  381. typedef void equal;
  382. typedef void hash;
  383. typedef default_bucket_traits bucket_traits;
  384. static const bool constant_time_size = true;
  385. static const bool power_2_buckets = false;
  386. static const bool cache_begin = false;
  387. static const bool compare_hash = false;
  388. static const bool incremental = false;
  389. };
  390. template<class RealValueTraits, bool IsConst>
  391. struct downcast_node_to_value_t
  392. : public detail::node_to_value<RealValueTraits, IsConst>
  393. {
  394. typedef detail::node_to_value<RealValueTraits, IsConst> base_t;
  395. typedef typename base_t::result_type result_type;
  396. typedef RealValueTraits real_value_traits;
  397. typedef typename detail::get_slist_impl
  398. <typename detail::reduced_slist_node_traits
  399. <typename real_value_traits::node_traits>::type
  400. >::type slist_impl;
  401. typedef typename detail::add_const_if_c
  402. <typename slist_impl::node, IsConst>::type & first_argument_type;
  403. typedef typename detail::add_const_if_c
  404. < typename RealValueTraits::node_traits::node
  405. , IsConst>::type & intermediate_argument_type;
  406. typedef typename pointer_traits
  407. <typename RealValueTraits::pointer>::
  408. template rebind_pointer
  409. <const RealValueTraits>::type const_real_value_traits_ptr;
  410. downcast_node_to_value_t(const const_real_value_traits_ptr &ptr)
  411. : base_t(ptr)
  412. {}
  413. result_type operator()(first_argument_type arg) const
  414. { return this->base_t::operator()(static_cast<intermediate_argument_type>(arg)); }
  415. };
  416. template<class F, class SlistNodePtr, class NodePtr>
  417. struct node_cast_adaptor
  418. : private detail::ebo_functor_holder<F>
  419. {
  420. typedef detail::ebo_functor_holder<F> base_t;
  421. typedef typename pointer_traits<SlistNodePtr>::element_type slist_node;
  422. typedef typename pointer_traits<NodePtr>::element_type node;
  423. template<class ConvertibleToF, class RealValuTraits>
  424. node_cast_adaptor(const ConvertibleToF &c2f, const RealValuTraits *traits)
  425. : base_t(base_t(c2f, traits))
  426. {}
  427. typename base_t::node_ptr operator()(const slist_node &to_clone)
  428. { return base_t::operator()(static_cast<const node &>(to_clone)); }
  429. void operator()(SlistNodePtr to_clone)
  430. {
  431. base_t::operator()(pointer_traits<NodePtr>::pointer_to(static_cast<node &>(*to_clone)));
  432. }
  433. };
  434. static const std::size_t hashtable_data_bool_flags_mask =
  435. ( hash_bool_flags::cache_begin_pos
  436. | hash_bool_flags::constant_time_size_pos
  437. | hash_bool_flags::incremental_pos
  438. );
  439. template<class ValueTraits, class BucketTraits>
  440. struct bucket_plus_vtraits : public ValueTraits
  441. {
  442. typedef BucketTraits bucket_traits;
  443. typedef ValueTraits value_traits;
  444. static const bool external_value_traits =
  445. detail::external_value_traits_bool_is_true<ValueTraits>::value;
  446. static const bool external_bucket_traits =
  447. detail::external_bucket_traits_bool_is_true<bucket_traits>::value;
  448. typedef typename detail::get_real_value_traits<ValueTraits>::type real_value_traits;
  449. static const bool safemode_or_autounlink = is_safe_autounlink<real_value_traits::link_mode>::value;
  450. typedef typename detail::eval_if_c
  451. < external_bucket_traits
  452. , detail::eval_bucket_traits<bucket_traits>
  453. , detail::identity<bucket_traits>
  454. >::type real_bucket_traits;
  455. typedef typename
  456. detail::get_slist_impl_from_supposed_value_traits
  457. <real_value_traits>::type slist_impl;
  458. template<class BucketTraitsType>
  459. bucket_plus_vtraits(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits)
  460. : ValueTraits(val_traits), bucket_traits_(::boost::forward<BucketTraitsType>(b_traits))
  461. {}
  462. bucket_plus_vtraits & operator =(const bucket_plus_vtraits &x)
  463. {
  464. bucket_traits_ = x.bucket_traits_;
  465. return *this;
  466. }
  467. //real_value_traits
  468. //
  469. const real_value_traits &priv_real_value_traits(detail::false_) const
  470. { return *this; }
  471. const real_value_traits &priv_real_value_traits(detail::true_) const
  472. { return this->get_value_traits(*this); }
  473. real_value_traits &priv_real_value_traits(detail::false_)
  474. { return *this; }
  475. real_value_traits &priv_real_value_traits(detail::true_)
  476. { return this->get_value_traits(*this); }
  477. const real_value_traits &priv_real_value_traits() const
  478. { return this->priv_real_value_traits(detail::bool_<external_value_traits>()); }
  479. real_value_traits &priv_real_value_traits()
  480. { return this->priv_real_value_traits(detail::bool_<external_value_traits>()); }
  481. typedef typename pointer_traits<typename real_value_traits::pointer>::
  482. template rebind_pointer<const real_value_traits>::type const_real_value_traits_ptr;
  483. const_real_value_traits_ptr real_value_traits_ptr() const
  484. { return pointer_traits<const_real_value_traits_ptr>::pointer_to(this->priv_real_value_traits()); }
  485. //real_bucket_traits
  486. //
  487. const real_bucket_traits &priv_real_bucket_traits(detail::false_) const
  488. { return this->bucket_traits_; }
  489. const real_bucket_traits &priv_real_bucket_traits(detail::true_) const
  490. { return this->bucket_traits_.get_bucket_traits(*this); }
  491. real_bucket_traits &priv_real_bucket_traits(detail::false_)
  492. { return bucket_traits_; }
  493. real_bucket_traits &priv_real_bucket_traits(detail::true_)
  494. { return this->get_bucket_traits(*this); }
  495. const real_bucket_traits &priv_real_bucket_traits() const
  496. { return this->priv_real_bucket_traits(detail::bool_<external_bucket_traits>()); }
  497. real_bucket_traits &priv_real_bucket_traits()
  498. { return this->priv_real_bucket_traits(detail::bool_<external_bucket_traits>()); }
  499. //bucket_value_traits
  500. //
  501. const bucket_plus_vtraits &get_bucket_value_traits() const
  502. { return *this; }
  503. bucket_plus_vtraits &get_bucket_value_traits()
  504. { return *this; }
  505. typedef typename pointer_traits<typename real_value_traits::pointer>::
  506. template rebind_pointer<const bucket_plus_vtraits>::type const_bucket_value_traits_ptr;
  507. const_bucket_value_traits_ptr bucket_value_traits_ptr() const
  508. { return pointer_traits<const_bucket_value_traits_ptr>::pointer_to(this->get_bucket_value_traits()); }
  509. //value traits
  510. //
  511. const value_traits &priv_value_traits() const
  512. { return *this; }
  513. value_traits &priv_value_traits()
  514. { return *this; }
  515. //bucket_traits
  516. //
  517. const bucket_traits &priv_bucket_traits() const
  518. { return this->bucket_traits_; }
  519. bucket_traits &priv_bucket_traits()
  520. { return this->bucket_traits_; }
  521. //operations
  522. typedef typename detail::unordered_bucket_ptr_impl<real_value_traits>::type bucket_ptr;
  523. bucket_ptr priv_bucket_pointer() const
  524. { return this->priv_real_bucket_traits().bucket_begin(); }
  525. typename slist_impl::size_type priv_bucket_count() const
  526. { return this->priv_real_bucket_traits().bucket_count(); }
  527. bucket_ptr priv_invalid_bucket() const
  528. {
  529. const real_bucket_traits &rbt = this->priv_real_bucket_traits();
  530. return rbt.bucket_begin() + rbt.bucket_count();
  531. }
  532. typedef typename real_value_traits::node_traits node_traits;
  533. typedef unordered_group_adapter<node_traits> group_traits;
  534. typedef typename slist_impl::iterator siterator;
  535. typedef typename slist_impl::size_type size_type;
  536. typedef detail::bucket_impl<slist_impl> bucket_type;
  537. typedef detail::group_functions<node_traits> group_functions_t;
  538. typedef typename slist_impl::node_algorithms node_algorithms;
  539. typedef typename slist_impl::node_ptr slist_node_ptr;
  540. typedef typename node_traits::node_ptr node_ptr;
  541. typedef typename node_traits::node node;
  542. typedef typename real_value_traits::value_type value_type;
  543. typedef circular_slist_algorithms<group_traits> group_algorithms;
  544. /*
  545. siterator priv_invalid_local_it() const
  546. { return this->priv_invalid_bucket()->end(); }
  547. */
  548. siterator priv_invalid_local_it() const
  549. {
  550. return this->priv_real_bucket_traits().bucket_begin()->before_begin();
  551. }
  552. ///
  553. static siterator priv_get_last(bucket_type &b, detail::true_) //optimize multikey
  554. {
  555. //First find the last node of p's group.
  556. //This requires checking the first node of the next group or
  557. //the bucket node.
  558. slist_node_ptr end_ptr(b.end().pointed_node());
  559. node_ptr possible_end(node_traits::get_next( detail::dcast_bucket_ptr<node>(end_ptr)));
  560. node_ptr last_node_group(possible_end);
  561. while(end_ptr != possible_end){
  562. last_node_group = group_traits::get_next(detail::dcast_bucket_ptr<node>(possible_end));
  563. possible_end = node_traits::get_next(last_node_group);
  564. }
  565. return bucket_type::s_iterator_to(*last_node_group);
  566. }
  567. static siterator priv_get_last(bucket_type &b, detail::false_) //NOT optimize multikey
  568. { return b.previous(b.end()); }
  569. static siterator priv_get_previous(bucket_type &b, siterator i, detail::true_) //optimize multikey
  570. {
  571. node_ptr elem(detail::dcast_bucket_ptr<node>(i.pointed_node()));
  572. node_ptr prev_in_group(group_traits::get_next(elem));
  573. bool first_in_group = node_traits::get_next(prev_in_group) != elem;
  574. typename bucket_type::node &n = first_in_group
  575. ? *group_functions_t::get_prev_to_first_in_group(b.end().pointed_node(), elem)
  576. : *group_traits::get_next(elem)
  577. ;
  578. return bucket_type::s_iterator_to(n);
  579. }
  580. static siterator priv_get_previous(bucket_type &b, siterator i, detail::false_) //NOT optimize multikey
  581. { return b.previous(i); }
  582. static void priv_clear_group_nodes(bucket_type &b, detail::true_) //optimize multikey
  583. {
  584. siterator it(b.begin()), itend(b.end());
  585. while(it != itend){
  586. node_ptr to_erase(detail::dcast_bucket_ptr<node>(it.pointed_node()));
  587. ++it;
  588. group_algorithms::init(to_erase);
  589. }
  590. }
  591. static void priv_clear_group_nodes(bucket_type &, detail::false_) //NOT optimize multikey
  592. {}
  593. std::size_t priv_get_bucket_num_no_hash_store(siterator it, detail::true_) //optimize multikey
  594. {
  595. const bucket_ptr f(this->priv_bucket_pointer()), l(f + this->priv_bucket_count() - 1);
  596. slist_node_ptr bb = group_functions_t::get_bucket_before_begin
  597. ( f->end().pointed_node()
  598. , l->end().pointed_node()
  599. , detail::dcast_bucket_ptr<node>(it.pointed_node()));
  600. //Now get the bucket_impl from the iterator
  601. const bucket_type &b = static_cast<const bucket_type&>
  602. (bucket_type::slist_type::container_from_end_iterator(bucket_type::s_iterator_to(*bb)));
  603. //Now just calculate the index b has in the bucket array
  604. return static_cast<size_type>(&b - &*f);
  605. }
  606. std::size_t priv_get_bucket_num_no_hash_store(siterator it, detail::false_) //NO optimize multikey
  607. {
  608. bucket_ptr f(this->priv_bucket_pointer()), l(f + this->priv_bucket_count() - 1);
  609. slist_node_ptr first_ptr(f->cend().pointed_node())
  610. , last_ptr(l->cend().pointed_node());
  611. //The end node is embedded in the singly linked list:
  612. //iterate until we reach it.
  613. while(!(first_ptr <= it.pointed_node() && it.pointed_node() <= last_ptr)){
  614. ++it;
  615. }
  616. //Now get the bucket_impl from the iterator
  617. const bucket_type &b = static_cast<const bucket_type&>
  618. (bucket_type::container_from_end_iterator(it));
  619. //Now just calculate the index b has in the bucket array
  620. return static_cast<std::size_t>(&b - &*f);
  621. }
  622. static std::size_t priv_stored_hash(slist_node_ptr n, detail::true_) //store_hash
  623. { return node_traits::get_hash(detail::dcast_bucket_ptr<node>(n)); }
  624. static std::size_t priv_stored_hash(slist_node_ptr, detail::false_) //NO store_hash
  625. {
  626. //This code should never be reached!
  627. BOOST_INTRUSIVE_INVARIANT_ASSERT(0);
  628. return 0;
  629. }
  630. node &priv_value_to_node(value_type &v)
  631. { return *this->priv_real_value_traits().to_node_ptr(v); }
  632. const node &priv_value_to_node(const value_type &v) const
  633. { return *this->priv_real_value_traits().to_node_ptr(v); }
  634. value_type &priv_value_from_slist_node(slist_node_ptr n)
  635. { return *this->priv_real_value_traits().to_value_ptr(detail::dcast_bucket_ptr<node>(n)); }
  636. const value_type &priv_value_from_slist_node(slist_node_ptr n) const
  637. { return *this->priv_real_value_traits().to_value_ptr(detail::dcast_bucket_ptr<node>(n)); }
  638. bucket_traits bucket_traits_;
  639. };
  640. template<class VoidOrKeyHash, class ValueTraits, class BucketTraits>
  641. struct bucket_hash_t
  642. : public detail::ebo_functor_holder
  643. <typename get_hash< VoidOrKeyHash
  644. , typename bucket_plus_vtraits<ValueTraits,BucketTraits>::real_value_traits::value_type
  645. >::type
  646. >
  647. , bucket_plus_vtraits<ValueTraits, BucketTraits>
  648. {
  649. typedef typename bucket_plus_vtraits<ValueTraits,BucketTraits>::real_value_traits real_value_traits;
  650. typedef typename real_value_traits::value_type value_type;
  651. typedef typename real_value_traits::node_traits node_traits;
  652. typedef typename get_hash< VoidOrKeyHash, value_type>::type hasher;
  653. typedef BucketTraits bucket_traits;
  654. typedef bucket_plus_vtraits<ValueTraits, BucketTraits> bucket_plus_vtraits_t;
  655. template<class BucketTraitsType>
  656. bucket_hash_t(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits, const hasher & h)
  657. : detail::ebo_functor_holder<hasher>(h), bucket_plus_vtraits_t(val_traits, ::boost::forward<BucketTraitsType>(b_traits))
  658. {}
  659. const hasher &priv_hasher() const
  660. { return this->detail::ebo_functor_holder<hasher>::get(); }
  661. hasher &priv_hasher()
  662. { return this->detail::ebo_functor_holder<hasher>::get(); }
  663. std::size_t priv_stored_or_compute_hash(const value_type &v, detail::true_) const //For store_hash == true
  664. { return node_traits::get_hash(this->priv_real_value_traits().to_node_ptr(v)); }
  665. std::size_t priv_stored_or_compute_hash(const value_type &v, detail::false_) const //For store_hash == false
  666. { return this->priv_hasher()(v); }
  667. };
  668. template<class VoidOrKeyHash, class VoidOrKeyEqual, class ValueTraits, class BucketTraits, bool>
  669. struct bucket_hash_equal_t
  670. : public detail::ebo_functor_holder //equal
  671. <typename get_equal_to< VoidOrKeyEqual
  672. , typename bucket_plus_vtraits<ValueTraits,BucketTraits>::real_value_traits::value_type
  673. >::type
  674. >
  675. , bucket_hash_t<VoidOrKeyHash, ValueTraits, BucketTraits>
  676. {
  677. typedef bucket_hash_t<VoidOrKeyHash, ValueTraits, BucketTraits> bucket_hash_type;
  678. typedef typename bucket_plus_vtraits<ValueTraits,BucketTraits>::real_value_traits real_value_traits;
  679. typedef typename get_equal_to< VoidOrKeyEqual
  680. , typename real_value_traits::value_type
  681. >::type value_equal;
  682. typedef typename bucket_hash_type::hasher hasher;
  683. typedef BucketTraits bucket_traits;
  684. typedef bucket_hash_t<VoidOrKeyHash, ValueTraits, BucketTraits> buckethash_t;
  685. typedef typename bucket_hash_type::real_bucket_traits real_bucket_traits;
  686. typedef typename bucket_hash_type::slist_impl slist_impl;
  687. typedef typename slist_impl::size_type size_type;
  688. typedef typename slist_impl::iterator siterator;
  689. typedef detail::bucket_impl<slist_impl> bucket_type;
  690. typedef typename detail::unordered_bucket_ptr_impl<real_value_traits>::type bucket_ptr;
  691. template<class BucketTraitsType>
  692. bucket_hash_equal_t(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits, const hasher & h, const value_equal &e)
  693. : detail::ebo_functor_holder<value_equal>(e)
  694. , buckethash_t(val_traits, ::boost::forward<BucketTraitsType>(b_traits), h)
  695. {}
  696. bucket_ptr priv_get_cache()
  697. { return this->priv_bucket_pointer(); }
  698. void priv_set_cache(const bucket_ptr &)
  699. {}
  700. size_type priv_get_cache_bucket_num()
  701. { return 0u; }
  702. void priv_initialize_cache()
  703. {}
  704. void priv_swap_cache(bucket_hash_equal_t &)
  705. {}
  706. siterator priv_begin() const
  707. {
  708. size_type n = 0;
  709. size_type bucket_cnt = this->priv_bucket_count();
  710. for (n = 0; n < bucket_cnt; ++n){
  711. bucket_type &b = this->priv_bucket_pointer()[n];
  712. if(!b.empty()){
  713. return b.begin();
  714. }
  715. }
  716. return this->priv_invalid_local_it();
  717. }
  718. void priv_insertion_update_cache(size_type)
  719. {}
  720. void priv_erasure_update_cache_range(size_type, size_type)
  721. {}
  722. void priv_erasure_update_cache()
  723. {}
  724. const value_equal &priv_equal() const
  725. { return this->detail::ebo_functor_holder<value_equal>::get(); }
  726. value_equal &priv_equal()
  727. { return this->detail::ebo_functor_holder<value_equal>::get(); }
  728. };
  729. template<class VoidOrKeyHash, class VoidOrKeyEqual, class ValueTraits, class BucketTraits> //cache_begin == true version
  730. struct bucket_hash_equal_t<VoidOrKeyHash, VoidOrKeyEqual, ValueTraits, BucketTraits, true>
  731. : public detail::ebo_functor_holder //equal
  732. <typename get_equal_to< VoidOrKeyEqual
  733. , typename bucket_plus_vtraits<ValueTraits,BucketTraits>::real_value_traits::value_type
  734. >::type
  735. >
  736. , public bucket_hash_t<VoidOrKeyHash, ValueTraits, BucketTraits>
  737. {
  738. typedef bucket_hash_t<VoidOrKeyHash, ValueTraits, BucketTraits> bucket_hash_type;
  739. typedef typename get_equal_to< VoidOrKeyEqual
  740. , typename bucket_plus_vtraits<ValueTraits,BucketTraits>::real_value_traits::value_type
  741. >::type value_equal;
  742. typedef typename bucket_hash_type::hasher hasher;
  743. typedef BucketTraits bucket_traits;
  744. typedef typename bucket_hash_type::slist_impl::size_type size_type;
  745. typedef typename bucket_hash_type::slist_impl::iterator siterator;
  746. template<class BucketTraitsType>
  747. bucket_hash_equal_t(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits, const hasher & h, const value_equal &e)
  748. : detail::ebo_functor_holder<value_equal>(e)
  749. , bucket_hash_type(val_traits, ::boost::forward<BucketTraitsType>(b_traits), h)
  750. {}
  751. typedef typename detail::unordered_bucket_ptr_impl
  752. <typename bucket_hash_type::real_value_traits>::type bucket_ptr;
  753. bucket_ptr &priv_get_cache()
  754. { return cached_begin_; }
  755. const bucket_ptr &priv_get_cache() const
  756. { return cached_begin_; }
  757. void priv_set_cache(const bucket_ptr &p)
  758. { cached_begin_ = p; }
  759. std::size_t priv_get_cache_bucket_num()
  760. { return this->cached_begin_ - this->priv_bucket_pointer(); }
  761. void priv_initialize_cache()
  762. { this->cached_begin_ = this->priv_invalid_bucket(); }
  763. void priv_swap_cache(bucket_hash_equal_t &other)
  764. {
  765. std::swap(this->cached_begin_, other.cached_begin_);
  766. }
  767. siterator priv_begin() const
  768. {
  769. if(this->cached_begin_ == this->priv_invalid_bucket()){
  770. return this->priv_invalid_local_it();
  771. }
  772. else{
  773. return this->cached_begin_->begin();
  774. }
  775. }
  776. void priv_insertion_update_cache(size_type insertion_bucket)
  777. {
  778. bucket_ptr p = this->priv_bucket_pointer() + insertion_bucket;
  779. if(p < this->cached_begin_){
  780. this->cached_begin_ = p;
  781. }
  782. }
  783. const value_equal &priv_equal() const
  784. { return this->detail::ebo_functor_holder<value_equal>::get(); }
  785. value_equal &priv_equal()
  786. { return this->detail::ebo_functor_holder<value_equal>::get(); }
  787. void priv_erasure_update_cache_range(size_type first_bucket_num, size_type last_bucket_num)
  788. {
  789. //If the last bucket is the end, the cache must be updated
  790. //to the last position if all
  791. if(this->priv_get_cache_bucket_num() == first_bucket_num &&
  792. this->priv_bucket_pointer()[first_bucket_num].empty() ){
  793. this->priv_set_cache(this->priv_bucket_pointer() + last_bucket_num);
  794. this->priv_erasure_update_cache();
  795. }
  796. }
  797. void priv_erasure_update_cache()
  798. {
  799. if(this->cached_begin_ != this->priv_invalid_bucket()){
  800. size_type current_n = this->priv_get_cache() - this->priv_bucket_pointer();
  801. for( const size_type num_buckets = this->priv_bucket_count()
  802. ; current_n < num_buckets
  803. ; ++current_n, ++this->priv_get_cache()){
  804. if(!this->priv_get_cache()->empty()){
  805. return;
  806. }
  807. }
  808. this->priv_initialize_cache();
  809. }
  810. }
  811. private:
  812. bucket_ptr cached_begin_;
  813. };
  814. template<class SizeType, std::size_t BoolFlags, class VoidOrKeyHash, class VoidOrKeyEqual, class ValueTraits, class BucketTraits>
  815. struct hashdata_internal
  816. : public detail::size_holder< 0 != (BoolFlags & hash_bool_flags::incremental_pos), SizeType, int> //split_traits
  817. , public bucket_hash_equal_t
  818. < VoidOrKeyHash, VoidOrKeyEqual, ValueTraits, BucketTraits
  819. , 0 != (BoolFlags & hash_bool_flags::cache_begin_pos)
  820. >
  821. {
  822. typedef bucket_hash_equal_t
  823. < VoidOrKeyHash, VoidOrKeyEqual, ValueTraits, BucketTraits
  824. , 0 != (BoolFlags & hash_bool_flags::cache_begin_pos)
  825. > bucket_hash_equal_type;
  826. typedef typename bucket_hash_equal_type::value_equal value_equal;
  827. typedef typename bucket_hash_equal_type::hasher hasher;
  828. typedef bucket_plus_vtraits<ValueTraits,BucketTraits> bucket_plus_vtraits_t;
  829. typedef typename bucket_plus_vtraits_t::size_type size_type;
  830. typedef typename bucket_plus_vtraits_t::bucket_ptr bucket_ptr;
  831. static const bool optimize_multikey
  832. = detail::optimize_multikey_is_true<typename bucket_plus_vtraits_t::real_value_traits::node_traits>::value;
  833. typedef detail::bool_<optimize_multikey> optimize_multikey_t;
  834. template<class BucketTraitsType>
  835. hashdata_internal(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits, const hasher & h, const value_equal &e)
  836. : bucket_hash_equal_type(val_traits, ::boost::forward<BucketTraitsType>(b_traits), h, e)
  837. {}
  838. typedef detail::size_holder
  839. <0 != (BoolFlags & hash_bool_flags::incremental_pos), SizeType, int> split_traits;
  840. split_traits &priv_split_traits()
  841. { return *this; }
  842. const split_traits &priv_split_traits() const
  843. { return *this; }
  844. };
  845. template<class SizeType, std::size_t BoolFlags, class VoidOrKeyHash, class VoidOrKeyEqual, class ValueTraits, class BucketTraits>
  846. struct hashtable_data_t
  847. : public detail::size_holder< 0 != (BoolFlags & hash_bool_flags::constant_time_size_pos), SizeType> //size_traits
  848. , public hashdata_internal
  849. < SizeType, BoolFlags & (hash_bool_flags::incremental_pos | hash_bool_flags::cache_begin_pos)
  850. , VoidOrKeyHash, VoidOrKeyEqual, ValueTraits, BucketTraits>
  851. {
  852. static const std::size_t bool_flags = BoolFlags;
  853. typedef detail::size_holder
  854. < 0 != (BoolFlags & hash_bool_flags::constant_time_size_pos)
  855. , SizeType> size_traits;
  856. typedef hashdata_internal
  857. < SizeType, BoolFlags & (hash_bool_flags::incremental_pos | hash_bool_flags::cache_begin_pos)
  858. , VoidOrKeyHash, VoidOrKeyEqual, ValueTraits, BucketTraits> internal_type;
  859. typedef ValueTraits value_traits;
  860. typedef typename internal_type::value_equal value_equal;
  861. typedef typename internal_type::hasher hasher;
  862. typedef BucketTraits bucket_traits;
  863. typedef bucket_plus_vtraits
  864. <ValueTraits,BucketTraits> bucket_plus_vtraits_t;
  865. static const bool external_value_traits =
  866. detail::external_value_traits_bool_is_true<ValueTraits>::value;
  867. static const bool external_bucket_traits = bucket_plus_vtraits_t::external_bucket_traits;
  868. typedef typename bucket_plus_vtraits_t::real_value_traits real_value_traits;
  869. typedef typename bucket_plus_vtraits_t::real_bucket_traits real_bucket_traits;
  870. size_traits &priv_size_traits()
  871. { return *this; }
  872. const size_traits &priv_size_traits() const
  873. { return *this; }
  874. template<class BucketTraitsType>
  875. hashtable_data_t( BOOST_FWD_REF(BucketTraitsType) b_traits, const hasher & h
  876. , const value_equal &e, const value_traits &val_traits)
  877. : size_traits()
  878. , internal_type(val_traits, ::boost::forward<BucketTraitsType>(b_traits), h, e)
  879. {}
  880. };
  881. /// @endcond
  882. //! The class template hashtable is an intrusive hash table container, that
  883. //! is used to construct intrusive unordered_set and unordered_multiset containers. The
  884. //! no-throw guarantee holds only, if the VoidOrKeyEqual object and Hasher don't throw.
  885. //!
  886. //! hashtable is a semi-intrusive container: each object to be stored in the
  887. //! container must contain a proper hook, but the container also needs
  888. //! additional auxiliary memory to work: hashtable needs a pointer to an array
  889. //! of type `bucket_type` to be passed in the constructor. This bucket array must
  890. //! have at least the same lifetime as the container. This makes the use of
  891. //! hashtable more complicated than purely intrusive containers.
  892. //! `bucket_type` is default-constructible, copyable and assignable
  893. //!
  894. //! The template parameter \c T is the type to be managed by the container.
  895. //! The user can specify additional options and if no options are provided
  896. //! default options are used.
  897. //!
  898. //! The container supports the following options:
  899. //! \c base_hook<>/member_hook<>/value_traits<>,
  900. //! \c constant_time_size<>, \c size_type<>, \c hash<> and \c equal<>
  901. //! \c bucket_traits<>, power_2_buckets<>, cache_begin<> and incremental<>.
  902. //!
  903. //! hashtable only provides forward iterators but it provides 4 iterator types:
  904. //! iterator and const_iterator to navigate through the whole container and
  905. //! local_iterator and const_local_iterator to navigate through the values
  906. //! stored in a single bucket. Local iterators are faster and smaller.
  907. //!
  908. //! It's not recommended to use non constant-time size hashtables because several
  909. //! key functions, like "empty()", become non-constant time functions. Non
  910. //! constant_time size hashtables are mainly provided to support auto-unlink hooks.
  911. //!
  912. //! hashtables, does not make automatic rehashings nor
  913. //! offers functions related to a load factor. Rehashing can be explicitly requested
  914. //! and the user must provide a new bucket array that will be used from that moment.
  915. //!
  916. //! Since no automatic rehashing is done, iterators are never invalidated when
  917. //! inserting or erasing elements. Iterators are only invalidated when rehashing.
  918. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  919. template<class T, class ...Options>
  920. #else
  921. template<class ValueTraits, class VoidOrKeyHash, class VoidOrKeyEqual, class SizeType, class BucketTraits, std::size_t BoolFlags>
  922. #endif
  923. class hashtable_impl
  924. : public hashtable_data_t
  925. < SizeType
  926. , BoolFlags & hashtable_data_bool_flags_mask
  927. , VoidOrKeyHash, VoidOrKeyEqual, ValueTraits, BucketTraits>
  928. , private detail::clear_on_destructor_base
  929. < hashtable_impl<ValueTraits, VoidOrKeyHash, VoidOrKeyEqual, SizeType, BucketTraits, BoolFlags>
  930. , true //To always clear the bucket array
  931. //is_safe_autounlink<detail::get_real_value_traits<ValueTraits>::type::link_mode>::value
  932. >
  933. {
  934. template<class C, bool> friend class detail::clear_on_destructor_base;
  935. public:
  936. typedef ValueTraits value_traits;
  937. typedef hashtable_data_t
  938. < SizeType
  939. , BoolFlags & hashtable_data_bool_flags_mask
  940. , VoidOrKeyHash, VoidOrKeyEqual, ValueTraits, BucketTraits> data_type;
  941. /// @cond
  942. static const bool external_value_traits = data_type::external_value_traits;
  943. static const bool external_bucket_traits = data_type::external_bucket_traits;
  944. typedef BucketTraits bucket_traits;
  945. typedef typename data_type::real_bucket_traits real_bucket_traits;
  946. typedef typename data_type::real_value_traits real_value_traits;
  947. typedef typename detail::get_slist_impl
  948. <typename detail::reduced_slist_node_traits
  949. <typename real_value_traits::node_traits>::type
  950. >::type slist_impl;
  951. typedef bucket_plus_vtraits<ValueTraits, BucketTraits> bucket_plus_vtraits_t;
  952. typedef typename bucket_plus_vtraits_t::const_real_value_traits_ptr const_real_value_traits_ptr;
  953. /// @endcond
  954. typedef typename real_value_traits::pointer pointer;
  955. typedef typename real_value_traits::const_pointer const_pointer;
  956. typedef typename real_value_traits::value_type value_type;
  957. typedef typename pointer_traits<pointer>::reference reference;
  958. typedef typename pointer_traits<const_pointer>::reference const_reference;
  959. typedef typename pointer_traits<pointer>::difference_type difference_type;
  960. typedef SizeType size_type;
  961. typedef value_type key_type;
  962. typedef typename data_type::value_equal key_equal;
  963. typedef typename data_type::hasher hasher;
  964. typedef detail::bucket_impl<slist_impl> bucket_type;
  965. typedef typename pointer_traits
  966. <pointer>::template rebind_pointer
  967. < bucket_type >::type bucket_ptr;
  968. typedef typename slist_impl::iterator siterator;
  969. typedef typename slist_impl::const_iterator const_siterator;
  970. typedef hashtable_iterator<bucket_plus_vtraits_t, false> iterator;
  971. typedef hashtable_iterator<bucket_plus_vtraits_t, true> const_iterator;
  972. typedef typename real_value_traits::node_traits node_traits;
  973. typedef typename node_traits::node node;
  974. typedef typename pointer_traits
  975. <pointer>::template rebind_pointer
  976. < node >::type node_ptr;
  977. typedef typename pointer_traits
  978. <pointer>::template rebind_pointer
  979. < const node >::type const_node_ptr;
  980. typedef typename slist_impl::node_algorithms node_algorithms;
  981. static const bool stateful_value_traits = detail::is_stateful_value_traits<real_value_traits>::value;
  982. static const bool store_hash = detail::store_hash_is_true<node_traits>::value;
  983. static const bool unique_keys = 0 != (BoolFlags & hash_bool_flags::unique_keys_pos);
  984. static const bool constant_time_size = 0 != (BoolFlags & hash_bool_flags::constant_time_size_pos);
  985. static const bool cache_begin = 0 != (BoolFlags & hash_bool_flags::cache_begin_pos);
  986. static const bool compare_hash = 0 != (BoolFlags & hash_bool_flags::compare_hash_pos);
  987. static const bool incremental = 0 != (BoolFlags & hash_bool_flags::incremental_pos);
  988. static const bool power_2_buckets = incremental || (0 != (BoolFlags & hash_bool_flags::power_2_buckets_pos));
  989. static const bool optimize_multikey
  990. = detail::optimize_multikey_is_true<node_traits>::value && !unique_keys;
  991. /// @cond
  992. private:
  993. //Configuration error: compare_hash<> can't be specified without store_hash<>
  994. //See documentation for more explanations
  995. BOOST_STATIC_ASSERT((!compare_hash || store_hash));
  996. typedef typename slist_impl::node_ptr slist_node_ptr;
  997. typedef typename pointer_traits
  998. <slist_node_ptr>::template rebind_pointer
  999. < void >::type void_pointer;
  1000. //We'll define group traits, but these won't be instantiated if
  1001. //optimize_multikey is not true
  1002. typedef unordered_group_adapter<node_traits> group_traits;
  1003. typedef circular_slist_algorithms<group_traits> group_algorithms;
  1004. typedef detail::bool_<store_hash> store_hash_t;
  1005. typedef detail::bool_<optimize_multikey> optimize_multikey_t;
  1006. typedef detail::bool_<cache_begin> cache_begin_t;
  1007. typedef detail::bool_<power_2_buckets> power_2_buckets_t;
  1008. typedef detail::size_holder<constant_time_size, size_type> size_traits;
  1009. typedef detail::size_holder<incremental, size_type, int> split_traits;
  1010. typedef detail::group_functions<node_traits> group_functions_t;
  1011. typedef detail::node_functions<node_traits> node_functions_t;
  1012. private:
  1013. //noncopyable, movable
  1014. BOOST_MOVABLE_BUT_NOT_COPYABLE(hashtable_impl)
  1015. static const bool safemode_or_autounlink = is_safe_autounlink<real_value_traits::link_mode>::value;
  1016. //Constant-time size is incompatible with auto-unlink hooks!
  1017. BOOST_STATIC_ASSERT(!(constant_time_size && ((int)real_value_traits::link_mode == (int)auto_unlink)));
  1018. //Cache begin is incompatible with auto-unlink hooks!
  1019. BOOST_STATIC_ASSERT(!(cache_begin && ((int)real_value_traits::link_mode == (int)auto_unlink)));
  1020. template<class Disposer>
  1021. node_cast_adaptor< detail::node_disposer<Disposer, real_value_traits, CircularSListAlgorithms>
  1022. , slist_node_ptr, node_ptr >
  1023. make_node_disposer(const Disposer &disposer) const
  1024. {
  1025. return node_cast_adaptor
  1026. < detail::node_disposer<Disposer, real_value_traits, CircularSListAlgorithms>
  1027. , slist_node_ptr, node_ptr >
  1028. (disposer, &this->priv_real_value_traits());
  1029. }
  1030. /// @endcond
  1031. public:
  1032. typedef detail::insert_commit_data_impl insert_commit_data;
  1033. typedef detail::transform_iterator
  1034. < typename slist_impl::iterator
  1035. , downcast_node_to_value_t
  1036. < real_value_traits
  1037. , false> > local_iterator;
  1038. typedef detail::transform_iterator
  1039. < typename slist_impl::iterator
  1040. , downcast_node_to_value_t
  1041. < real_value_traits
  1042. , true> > const_local_iterator;
  1043. public:
  1044. //! <b>Requires</b>: buckets must not be being used by any other resource.
  1045. //!
  1046. //! <b>Effects</b>: Constructs an empty unordered_set, storing a reference
  1047. //! to the bucket array and copies of the key_hasher and equal_func functors.
  1048. //!
  1049. //! <b>Complexity</b>: Constant.
  1050. //!
  1051. //! <b>Throws</b>: If value_traits::node_traits::node
  1052. //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
  1053. //! or the copy constructor or invocation of hash_func or equal_func throws.
  1054. //!
  1055. //! <b>Notes</b>: buckets array must be disposed only after
  1056. //! *this is disposed.
  1057. explicit hashtable_impl ( const bucket_traits &b_traits
  1058. , const hasher & hash_func = hasher()
  1059. , const key_equal &equal_func = key_equal()
  1060. , const value_traits &v_traits = value_traits())
  1061. : data_type(b_traits, hash_func, equal_func, v_traits)
  1062. {
  1063. this->priv_initialize_buckets();
  1064. this->priv_size_traits().set_size(size_type(0));
  1065. size_type bucket_sz = this->priv_bucket_count();
  1066. BOOST_INTRUSIVE_INVARIANT_ASSERT(bucket_sz != 0);
  1067. //Check power of two bucket array if the option is activated
  1068. BOOST_INTRUSIVE_INVARIANT_ASSERT
  1069. (!power_2_buckets || (0 == (bucket_sz & (bucket_sz-1))));
  1070. this->priv_split_traits().set_size(bucket_sz>>1);
  1071. }
  1072. //! <b>Effects</b>: to-do
  1073. //!
  1074. hashtable_impl(BOOST_RV_REF(hashtable_impl) x)
  1075. : data_type( ::boost::move(x.priv_bucket_traits())
  1076. , ::boost::move(x.priv_hasher())
  1077. , ::boost::move(x.priv_equal())
  1078. , ::boost::move(x.priv_value_traits())
  1079. )
  1080. {
  1081. this->priv_swap_cache(x);
  1082. x.priv_initialize_cache();
  1083. if(constant_time_size){
  1084. this->priv_size_traits().set_size(size_type(0));
  1085. this->priv_size_traits().set_size(x.priv_size_traits().get_size());
  1086. x.priv_size_traits().set_size(size_type(0));
  1087. }
  1088. if(incremental){
  1089. this->priv_split_traits().set_size(x.priv_split_traits().get_size());
  1090. x.priv_split_traits().set_size(size_type(0));
  1091. }
  1092. }
  1093. //! <b>Effects</b>: to-do
  1094. //!
  1095. hashtable_impl& operator=(BOOST_RV_REF(hashtable_impl) x)
  1096. { this->swap(x); return *this; }
  1097. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1098. //! <b>Effects</b>: Detaches all elements from this. The objects in the unordered_set
  1099. //! are not deleted (i.e. no destructors are called).
  1100. //!
  1101. //! <b>Complexity</b>: Linear to the number of elements in the unordered_set, if
  1102. //! it's a safe-mode or auto-unlink value. Otherwise constant.
  1103. //!
  1104. //! <b>Throws</b>: Nothing.
  1105. ~hashtable_impl()
  1106. {}
  1107. #endif
  1108. //! <b>Effects</b>: Returns an iterator pointing to the beginning of the unordered_set.
  1109. //!
  1110. //! <b>Complexity</b>: Amortized constant time.
  1111. //! Worst case (empty unordered_set): O(this->bucket_count())
  1112. //!
  1113. //! <b>Throws</b>: Nothing.
  1114. iterator begin()
  1115. { return iterator(this->priv_begin(), &this->get_bucket_value_traits()); }
  1116. //! <b>Effects</b>: Returns a const_iterator pointing to the beginning
  1117. //! of the unordered_set.
  1118. //!
  1119. //! <b>Complexity</b>: Amortized constant time.
  1120. //! Worst case (empty unordered_set): O(this->bucket_count())
  1121. //!
  1122. //! <b>Throws</b>: Nothing.
  1123. const_iterator begin() const
  1124. { return this->cbegin(); }
  1125. //! <b>Effects</b>: Returns a const_iterator pointing to the beginning
  1126. //! of the unordered_set.
  1127. //!
  1128. //! <b>Complexity</b>: Amortized constant time.
  1129. //! Worst case (empty unordered_set): O(this->bucket_count())
  1130. //!
  1131. //! <b>Throws</b>: Nothing.
  1132. const_iterator cbegin() const
  1133. { return const_iterator(this->priv_begin(), &this->get_bucket_value_traits()); }
  1134. //! <b>Effects</b>: Returns an iterator pointing to the end of the unordered_set.
  1135. //!
  1136. //! <b>Complexity</b>: Constant.
  1137. //!
  1138. //! <b>Throws</b>: Nothing.
  1139. iterator end()
  1140. { return iterator(this->priv_invalid_local_it(), 0); }
  1141. //! <b>Effects</b>: Returns a const_iterator pointing to the end of the unordered_set.
  1142. //!
  1143. //! <b>Complexity</b>: Constant.
  1144. //!
  1145. //! <b>Throws</b>: Nothing.
  1146. const_iterator end() const
  1147. { return this->cend(); }
  1148. //! <b>Effects</b>: Returns a const_iterator pointing to the end of the unordered_set.
  1149. //!
  1150. //! <b>Complexity</b>: Constant.
  1151. //!
  1152. //! <b>Throws</b>: Nothing.
  1153. const_iterator cend() const
  1154. { return const_iterator(this->priv_invalid_local_it(), 0); }
  1155. //! <b>Effects</b>: Returns the hasher object used by the unordered_set.
  1156. //!
  1157. //! <b>Complexity</b>: Constant.
  1158. //!
  1159. //! <b>Throws</b>: If hasher copy-constructor throws.
  1160. hasher hash_function() const
  1161. { return this->priv_hasher(); }
  1162. //! <b>Effects</b>: Returns the key_equal object used by the unordered_set.
  1163. //!
  1164. //! <b>Complexity</b>: Constant.
  1165. //!
  1166. //! <b>Throws</b>: If key_equal copy-constructor throws.
  1167. key_equal key_eq() const
  1168. { return this->priv_equal(); }
  1169. //! <b>Effects</b>: Returns true if the container is empty.
  1170. //!
  1171. //! <b>Complexity</b>: if constant-time size and cache_begin options are disabled,
  1172. //! average constant time (worst case, with empty() == true: O(this->bucket_count()).
  1173. //! Otherwise constant.
  1174. //!
  1175. //! <b>Throws</b>: Nothing.
  1176. bool empty() const
  1177. {
  1178. if(constant_time_size){
  1179. return !this->size();
  1180. }
  1181. else if(cache_begin){
  1182. return this->begin() == this->end();
  1183. }
  1184. else{
  1185. size_type bucket_cnt = this->priv_bucket_count();
  1186. const bucket_type *b = boost::intrusive::detail::to_raw_pointer(this->priv_bucket_pointer());
  1187. for (size_type n = 0; n < bucket_cnt; ++n, ++b){
  1188. if(!b->empty()){
  1189. return false;
  1190. }
  1191. }
  1192. return true;
  1193. }
  1194. }
  1195. //! <b>Effects</b>: Returns the number of elements stored in the unordered_set.
  1196. //!
  1197. //! <b>Complexity</b>: Linear to elements contained in *this if
  1198. //! constant_time_size is false. Constant-time otherwise.
  1199. //!
  1200. //! <b>Throws</b>: Nothing.
  1201. size_type size() const
  1202. {
  1203. if(constant_time_size)
  1204. return this->priv_size_traits().get_size();
  1205. else{
  1206. size_type len = 0;
  1207. size_type bucket_cnt = this->priv_bucket_count();
  1208. const bucket_type *b = boost::intrusive::detail::to_raw_pointer(this->priv_bucket_pointer());
  1209. for (size_type n = 0; n < bucket_cnt; ++n, ++b){
  1210. len += b->size();
  1211. }
  1212. return len;
  1213. }
  1214. }
  1215. //! <b>Requires</b>: the hasher and the equality function unqualified swap
  1216. //! call should not throw.
  1217. //!
  1218. //! <b>Effects</b>: Swaps the contents of two unordered_sets.
  1219. //! Swaps also the contained bucket array and equality and hasher functors.
  1220. //!
  1221. //! <b>Complexity</b>: Constant.
  1222. //!
  1223. //! <b>Throws</b>: If the swap() call for the comparison or hash functors
  1224. //! found using ADL throw. Basic guarantee.
  1225. void swap(hashtable_impl& other)
  1226. {
  1227. using std::swap;
  1228. //These can throw
  1229. swap(this->priv_equal(), other.priv_equal());
  1230. swap(this->priv_hasher(), other.priv_hasher());
  1231. //These can't throw
  1232. swap(this->priv_bucket_traits(), other.priv_bucket_traits());
  1233. swap(this->priv_value_traits(), other.priv_value_traits());
  1234. this->priv_swap_cache(other);
  1235. if(constant_time_size){
  1236. size_type backup = this->priv_size_traits().get_size();
  1237. this->priv_size_traits().set_size(other.priv_size_traits().get_size());
  1238. other.priv_size_traits().set_size(backup);
  1239. }
  1240. if(incremental){
  1241. size_type backup = this->priv_split_traits().get_size();
  1242. this->priv_split_traits().set_size(other.priv_split_traits().get_size());
  1243. other.priv_split_traits().set_size(backup);
  1244. }
  1245. }
  1246. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw
  1247. //! Cloner should yield to nodes that compare equal and produce the same
  1248. //! hash than the original node.
  1249. //!
  1250. //! <b>Effects</b>: Erases all the elements from *this
  1251. //! calling Disposer::operator()(pointer), clones all the
  1252. //! elements from src calling Cloner::operator()(const_reference )
  1253. //! and inserts them on *this. The hash function and the equality
  1254. //! predicate are copied from the source.
  1255. //!
  1256. //! If store_hash option is true, this method does not use the hash function.
  1257. //!
  1258. //! If any operation throws, all cloned elements are unlinked and disposed
  1259. //! calling Disposer::operator()(pointer).
  1260. //!
  1261. //! <b>Complexity</b>: Linear to erased plus inserted elements.
  1262. //!
  1263. //! <b>Throws</b>: If cloner or hasher throw or hash or equality predicate copying
  1264. //! throws. Basic guarantee.
  1265. template <class Cloner, class Disposer>
  1266. void clone_from(const hashtable_impl &src, Cloner cloner, Disposer disposer)
  1267. {
  1268. this->clear_and_dispose(disposer);
  1269. if(!constant_time_size || !src.empty()){
  1270. const size_type src_bucket_count = src.bucket_count();
  1271. const size_type dst_bucket_count = this->bucket_count();
  1272. //Check power of two bucket array if the option is activated
  1273. BOOST_INTRUSIVE_INVARIANT_ASSERT
  1274. (!power_2_buckets || (0 == (src_bucket_count & (src_bucket_count-1))));
  1275. BOOST_INTRUSIVE_INVARIANT_ASSERT
  1276. (!power_2_buckets || (0 == (dst_bucket_count & (dst_bucket_count-1))));
  1277. //If src bucket count is bigger or equal, structural copy is possible
  1278. if(!incremental && (src_bucket_count >= dst_bucket_count)){
  1279. //First clone the first ones
  1280. const bucket_ptr src_buckets = src.priv_bucket_pointer();
  1281. const bucket_ptr dst_buckets = this->priv_bucket_pointer();
  1282. size_type constructed;
  1283. typedef node_cast_adaptor< detail::node_disposer<Disposer, real_value_traits, CircularSListAlgorithms>
  1284. , slist_node_ptr, node_ptr > NodeDisposer;
  1285. typedef node_cast_adaptor< detail::node_cloner <Cloner, real_value_traits, CircularSListAlgorithms>
  1286. , slist_node_ptr, node_ptr > NodeCloner;
  1287. NodeDisposer node_disp(disposer, &this->priv_real_value_traits());
  1288. detail::exception_array_disposer<bucket_type, NodeDisposer, size_type>
  1289. rollback(dst_buckets[0], node_disp, constructed);
  1290. for( constructed = 0
  1291. ; constructed < dst_bucket_count
  1292. ; ++constructed){
  1293. dst_buckets[constructed].clone_from
  1294. ( src_buckets[constructed]
  1295. , NodeCloner(cloner, &this->priv_real_value_traits()), node_disp);
  1296. }
  1297. if(src_bucket_count != dst_bucket_count){
  1298. //Now insert the remaining ones using the modulo trick
  1299. for(//"constructed" comes from the previous loop
  1300. ; constructed < src_bucket_count
  1301. ; ++constructed){
  1302. bucket_type &dst_b =
  1303. dst_buckets[detail::hash_to_bucket_split<power_2_buckets, incremental>(constructed, dst_bucket_count, dst_bucket_count)];
  1304. bucket_type &src_b = src_buckets[constructed];
  1305. for( siterator b(src_b.begin()), e(src_b.end())
  1306. ; b != e
  1307. ; ++b){
  1308. dst_b.push_front(*(NodeCloner(cloner, &this->priv_real_value_traits())(*b.pointed_node())));
  1309. }
  1310. }
  1311. }
  1312. this->priv_hasher() = src.priv_hasher();
  1313. this->priv_equal() = src.priv_equal();
  1314. rollback.release();
  1315. this->priv_size_traits().set_size(src.priv_size_traits().get_size());
  1316. this->priv_split_traits().set_size(dst_bucket_count);
  1317. this->priv_insertion_update_cache(0u);
  1318. this->priv_erasure_update_cache();
  1319. }
  1320. else if(store_hash){
  1321. //Unlike previous cloning algorithm, this can throw
  1322. //if cloner, hasher or comparison functor throw
  1323. const_iterator b(src.begin()), e(src.end());
  1324. detail::exception_disposer<hashtable_impl, Disposer>
  1325. rollback(*this, disposer);
  1326. for(; b != e; ++b){
  1327. std::size_t hash_value = this->priv_stored_or_compute_hash(*b, store_hash_t());;
  1328. this->priv_insert_equal_with_hash(*cloner(*b), hash_value);
  1329. }
  1330. rollback.release();
  1331. }
  1332. else{
  1333. //Unlike previous cloning algorithm, this can throw
  1334. //if cloner, hasher or comparison functor throw
  1335. const_iterator b(src.begin()), e(src.end());
  1336. detail::exception_disposer<hashtable_impl, Disposer>
  1337. rollback(*this, disposer);
  1338. for(; b != e; ++b){
  1339. this->insert_equal(*cloner(*b));
  1340. }
  1341. rollback.release();
  1342. }
  1343. }
  1344. }
  1345. //! <b>Requires</b>: value must be an lvalue
  1346. //!
  1347. //! <b>Effects</b>: Inserts the value into the unordered_set.
  1348. //!
  1349. //! <b>Returns</b>: An iterator to the inserted value.
  1350. //!
  1351. //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
  1352. //!
  1353. //! <b>Throws</b>: If the internal hasher or the equality functor throws. Strong guarantee.
  1354. //!
  1355. //! <b>Note</b>: Does not affect the validity of iterators and references.
  1356. //! No copy-constructors are called.
  1357. iterator insert_equal(reference value)
  1358. {
  1359. size_type bucket_num;
  1360. std::size_t hash_value;
  1361. siterator prev;
  1362. siterator it = this->priv_find
  1363. (value, this->priv_hasher(), this->priv_equal(), bucket_num, hash_value, prev);
  1364. return this->priv_insert_equal_find(value, bucket_num, hash_value, it);
  1365. }
  1366. //! <b>Requires</b>: Dereferencing iterator must yield an lvalue
  1367. //! of type value_type.
  1368. //!
  1369. //! <b>Effects</b>: Equivalent to this->insert_equal(t) for each element in [b, e).
  1370. //!
  1371. //! <b>Complexity</b>: Average case O(N), where N is std::distance(b, e).
  1372. //! Worst case O(N*this->size()).
  1373. //!
  1374. //! <b>Throws</b>: If the internal hasher or the equality functor throws. Basic guarantee.
  1375. //!
  1376. //! <b>Note</b>: Does not affect the validity of iterators and references.
  1377. //! No copy-constructors are called.
  1378. template<class Iterator>
  1379. void insert_equal(Iterator b, Iterator e)
  1380. {
  1381. for (; b != e; ++b)
  1382. this->insert_equal(*b);
  1383. }
  1384. //! <b>Requires</b>: value must be an lvalue
  1385. //!
  1386. //! <b>Effects</b>: Tries to inserts value into the unordered_set.
  1387. //!
  1388. //! <b>Returns</b>: If the value
  1389. //! is not already present inserts it and returns a pair containing the
  1390. //! iterator to the new value and true. If there is an equivalent value
  1391. //! returns a pair containing an iterator to the already present value
  1392. //! and false.
  1393. //!
  1394. //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
  1395. //!
  1396. //! <b>Throws</b>: If the internal hasher or the equality functor throws. Strong guarantee.
  1397. //!
  1398. //! <b>Note</b>: Does not affect the validity of iterators and references.
  1399. //! No copy-constructors are called.
  1400. std::pair<iterator, bool> insert_unique(reference value)
  1401. {
  1402. insert_commit_data commit_data;
  1403. std::pair<iterator, bool> ret = this->insert_unique_check
  1404. (value, this->priv_hasher(), this->priv_equal(), commit_data);
  1405. if(!ret.second)
  1406. return ret;
  1407. return std::pair<iterator, bool>
  1408. (this->insert_unique_commit(value, commit_data), true);
  1409. }
  1410. //! <b>Requires</b>: Dereferencing iterator must yield an lvalue
  1411. //! of type value_type.
  1412. //!
  1413. //! <b>Effects</b>: Equivalent to this->insert_unique(t) for each element in [b, e).
  1414. //!
  1415. //! <b>Complexity</b>: Average case O(N), where N is std::distance(b, e).
  1416. //! Worst case O(N*this->size()).
  1417. //!
  1418. //! <b>Throws</b>: If the internal hasher or the equality functor throws. Basic guarantee.
  1419. //!
  1420. //! <b>Note</b>: Does not affect the validity of iterators and references.
  1421. //! No copy-constructors are called.
  1422. template<class Iterator>
  1423. void insert_unique(Iterator b, Iterator e)
  1424. {
  1425. for (; b != e; ++b)
  1426. this->insert_unique(*b);
  1427. }
  1428. //! <b>Requires</b>: "hash_func" must be a hash function that induces
  1429. //! the same hash values as the stored hasher. The difference is that
  1430. //! "hash_func" hashes the given key instead of the value_type.
  1431. //!
  1432. //! "equal_func" must be a equality function that induces
  1433. //! the same equality as key_equal. The difference is that
  1434. //! "equal_func" compares an arbitrary key with the contained values.
  1435. //!
  1436. //! <b>Effects</b>: Checks if a value can be inserted in the unordered_set, using
  1437. //! a user provided key instead of the value itself.
  1438. //!
  1439. //! <b>Returns</b>: If there is an equivalent value
  1440. //! returns a pair containing an iterator to the already present value
  1441. //! and false. If the value can be inserted returns true in the returned
  1442. //! pair boolean and fills "commit_data" that is meant to be used with
  1443. //! the "insert_commit" function.
  1444. //!
  1445. //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
  1446. //!
  1447. //! <b>Throws</b>: If hash_func or equal_func throw. Strong guarantee.
  1448. //!
  1449. //! <b>Notes</b>: This function is used to improve performance when constructing
  1450. //! a value_type is expensive: if there is an equivalent value
  1451. //! the constructed object must be discarded. Many times, the part of the
  1452. //! node that is used to impose the hash or the equality is much cheaper to
  1453. //! construct than the value_type and this function offers the possibility to
  1454. //! use that the part to check if the insertion will be successful.
  1455. //!
  1456. //! If the check is successful, the user can construct the value_type and use
  1457. //! "insert_commit" to insert the object in constant-time.
  1458. //!
  1459. //! "commit_data" remains valid for a subsequent "insert_commit" only if no more
  1460. //! objects are inserted or erased from the unordered_set.
  1461. //!
  1462. //! After a successful rehashing insert_commit_data remains valid.
  1463. template<class KeyType, class KeyHasher, class KeyValueEqual>
  1464. std::pair<iterator, bool> insert_unique_check
  1465. ( const KeyType &key
  1466. , KeyHasher hash_func
  1467. , KeyValueEqual equal_func
  1468. , insert_commit_data &commit_data)
  1469. {
  1470. size_type bucket_num;
  1471. siterator prev;
  1472. siterator prev_pos =
  1473. this->priv_find(key, hash_func, equal_func, bucket_num, commit_data.hash, prev);
  1474. bool success = prev_pos == this->priv_invalid_local_it();
  1475. if(success){
  1476. prev_pos = prev;
  1477. }
  1478. return std::pair<iterator, bool>(iterator(prev_pos, &this->get_bucket_value_traits()),success);
  1479. }
  1480. //! <b>Requires</b>: value must be an lvalue of type value_type. commit_data
  1481. //! must have been obtained from a previous call to "insert_check".
  1482. //! No objects should have been inserted or erased from the unordered_set between
  1483. //! the "insert_check" that filled "commit_data" and the call to "insert_commit".
  1484. //!
  1485. //! <b>Effects</b>: Inserts the value in the unordered_set using the information obtained
  1486. //! from the "commit_data" that a previous "insert_check" filled.
  1487. //!
  1488. //! <b>Returns</b>: An iterator to the newly inserted object.
  1489. //!
  1490. //! <b>Complexity</b>: Constant time.
  1491. //!
  1492. //! <b>Throws</b>: Nothing.
  1493. //!
  1494. //! <b>Notes</b>: This function has only sense if a "insert_check" has been
  1495. //! previously executed to fill "commit_data". No value should be inserted or
  1496. //! erased between the "insert_check" and "insert_commit" calls.
  1497. //!
  1498. //! After a successful rehashing insert_commit_data remains valid.
  1499. iterator insert_unique_commit(reference value, const insert_commit_data &commit_data)
  1500. {
  1501. size_type bucket_num = this->priv_hash_to_bucket(commit_data.hash);
  1502. bucket_type &b = this->priv_bucket_pointer()[bucket_num];
  1503. this->priv_size_traits().increment();
  1504. node_ptr n = pointer_traits<node_ptr>::pointer_to(this->priv_value_to_node(value));
  1505. node_functions_t::store_hash(n, commit_data.hash, store_hash_t());
  1506. if(safemode_or_autounlink)
  1507. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(n));
  1508. this->priv_insertion_update_cache(bucket_num);
  1509. group_functions_t::insert_in_group(node_ptr(), n, optimize_multikey_t());
  1510. return iterator(b.insert_after(b.before_begin(), *n), &this->get_bucket_value_traits());
  1511. }
  1512. //! <b>Effects</b>: Erases the element pointed to by i.
  1513. //!
  1514. //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
  1515. //!
  1516. //! <b>Throws</b>: Nothing.
  1517. //!
  1518. //! <b>Note</b>: Invalidates the iterators (but not the references)
  1519. //! to the erased element. No destructors are called.
  1520. void erase(const_iterator i)
  1521. { this->erase_and_dispose(i, detail::null_disposer()); }
  1522. //! <b>Effects</b>: Erases the range pointed to by b end e.
  1523. //!
  1524. //! <b>Complexity</b>: Average case O(std::distance(b, e)),
  1525. //! worst case O(this->size()).
  1526. //!
  1527. //! <b>Throws</b>: Nothing.
  1528. //!
  1529. //! <b>Note</b>: Invalidates the iterators (but not the references)
  1530. //! to the erased elements. No destructors are called.
  1531. void erase(const_iterator b, const_iterator e)
  1532. { this->erase_and_dispose(b, e, detail::null_disposer()); }
  1533. //! <b>Effects</b>: Erases all the elements with the given value.
  1534. //!
  1535. //! <b>Returns</b>: The number of erased elements.
  1536. //!
  1537. //! <b>Complexity</b>: Average case O(this->count(value)).
  1538. //! Worst case O(this->size()).
  1539. //!
  1540. //! <b>Throws</b>: If the internal hasher or the equality functor throws.
  1541. //! Basic guarantee.
  1542. //!
  1543. //! <b>Note</b>: Invalidates the iterators (but not the references)
  1544. //! to the erased elements. No destructors are called.
  1545. size_type erase(const_reference value)
  1546. { return this->erase(value, this->priv_hasher(), this->priv_equal()); }
  1547. //! <b>Requires</b>: "hash_func" must be a hash function that induces
  1548. //! the same hash values as the stored hasher. The difference is that
  1549. //! "hash_func" hashes the given key instead of the value_type.
  1550. //!
  1551. //! "equal_func" must be a equality function that induces
  1552. //! the same equality as key_equal. The difference is that
  1553. //! "equal_func" compares an arbitrary key with the contained values.
  1554. //!
  1555. //! <b>Effects</b>: Erases all the elements that have the same hash and
  1556. //! compare equal with the given key.
  1557. //!
  1558. //! <b>Returns</b>: The number of erased elements.
  1559. //!
  1560. //! <b>Complexity</b>: Average case O(this->count(value)).
  1561. //! Worst case O(this->size()).
  1562. //!
  1563. //! <b>Throws</b>: If hash_func or equal_func throw. Basic guarantee.
  1564. //!
  1565. //! <b>Note</b>: Invalidates the iterators (but not the references)
  1566. //! to the erased elements. No destructors are called.
  1567. template<class KeyType, class KeyHasher, class KeyValueEqual>
  1568. size_type erase(const KeyType& key, KeyHasher hash_func, KeyValueEqual equal_func)
  1569. { return this->erase_and_dispose(key, hash_func, equal_func, detail::null_disposer()); }
  1570. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1571. //!
  1572. //! <b>Effects</b>: Erases the element pointed to by i.
  1573. //! Disposer::operator()(pointer) is called for the removed element.
  1574. //!
  1575. //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
  1576. //!
  1577. //! <b>Throws</b>: Nothing.
  1578. //!
  1579. //! <b>Note</b>: Invalidates the iterators
  1580. //! to the erased elements.
  1581. template<class Disposer>
  1582. void erase_and_dispose(const_iterator i, Disposer disposer
  1583. /// @cond
  1584. , typename detail::enable_if_c<!detail::is_convertible<Disposer, const_iterator>::value >::type * = 0
  1585. /// @endcond
  1586. )
  1587. {
  1588. this->priv_erase(i, disposer, optimize_multikey_t());
  1589. this->priv_size_traits().decrement();
  1590. this->priv_erasure_update_cache();
  1591. }
  1592. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1593. //!
  1594. //! <b>Effects</b>: Erases the range pointed to by b end e.
  1595. //! Disposer::operator()(pointer) is called for the removed elements.
  1596. //!
  1597. //! <b>Complexity</b>: Average case O(std::distance(b, e)),
  1598. //! worst case O(this->size()).
  1599. //!
  1600. //! <b>Throws</b>: Nothing.
  1601. //!
  1602. //! <b>Note</b>: Invalidates the iterators
  1603. //! to the erased elements.
  1604. template<class Disposer>
  1605. void erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer)
  1606. {
  1607. if(b != e){
  1608. //Get the bucket number and local iterator for both iterators
  1609. siterator first_local_it(b.slist_it());
  1610. size_type first_bucket_num = this->priv_get_bucket_num(first_local_it);
  1611. const bucket_ptr buck_ptr = this->priv_bucket_pointer();
  1612. siterator before_first_local_it
  1613. = this->priv_get_previous(buck_ptr[first_bucket_num], first_local_it);
  1614. size_type last_bucket_num;
  1615. siterator last_local_it;
  1616. //For the end iterator, we will assign the end iterator
  1617. //of the last bucket
  1618. if(e == this->end()){
  1619. last_bucket_num = this->bucket_count() - 1;
  1620. last_local_it = buck_ptr[last_bucket_num].end();
  1621. }
  1622. else{
  1623. last_local_it = e.slist_it();
  1624. last_bucket_num = this->priv_get_bucket_num(last_local_it);
  1625. }
  1626. this->priv_erase_range(before_first_local_it, first_bucket_num, last_local_it, last_bucket_num, disposer);
  1627. this->priv_erasure_update_cache_range(first_bucket_num, last_bucket_num);
  1628. }
  1629. }
  1630. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1631. //!
  1632. //! <b>Effects</b>: Erases all the elements with the given value.
  1633. //! Disposer::operator()(pointer) is called for the removed elements.
  1634. //!
  1635. //! <b>Returns</b>: The number of erased elements.
  1636. //!
  1637. //! <b>Complexity</b>: Average case O(this->count(value)).
  1638. //! Worst case O(this->size()).
  1639. //!
  1640. //! <b>Throws</b>: If the internal hasher or the equality functor throws.
  1641. //! Basic guarantee.
  1642. //!
  1643. //! <b>Note</b>: Invalidates the iterators (but not the references)
  1644. //! to the erased elements. No destructors are called.
  1645. template<class Disposer>
  1646. size_type erase_and_dispose(const_reference value, Disposer disposer)
  1647. { return this->erase_and_dispose(value, this->priv_hasher(), this->priv_equal(), disposer); }
  1648. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1649. //!
  1650. //! <b>Effects</b>: Erases all the elements with the given key.
  1651. //! according to the comparison functor "equal_func".
  1652. //! Disposer::operator()(pointer) is called for the removed elements.
  1653. //!
  1654. //! <b>Returns</b>: The number of erased elements.
  1655. //!
  1656. //! <b>Complexity</b>: Average case O(this->count(value)).
  1657. //! Worst case O(this->size()).
  1658. //!
  1659. //! <b>Throws</b>: If hash_func or equal_func throw. Basic guarantee.
  1660. //!
  1661. //! <b>Note</b>: Invalidates the iterators
  1662. //! to the erased elements.
  1663. template<class KeyType, class KeyHasher, class KeyValueEqual, class Disposer>
  1664. size_type erase_and_dispose(const KeyType& key, KeyHasher hash_func
  1665. ,KeyValueEqual equal_func, Disposer disposer)
  1666. {
  1667. size_type bucket_num;
  1668. std::size_t h;
  1669. siterator prev;
  1670. siterator it = this->priv_find(key, hash_func, equal_func, bucket_num, h, prev);
  1671. bool success = it != this->priv_invalid_local_it();
  1672. size_type cnt(0);
  1673. if(!success){
  1674. return 0;
  1675. }
  1676. else if(optimize_multikey){
  1677. siterator last = bucket_type::s_iterator_to
  1678. (*node_traits::get_next(group_functions_t::get_last_in_group
  1679. (detail::dcast_bucket_ptr<node>(it.pointed_node()), optimize_multikey_t())));
  1680. this->priv_erase_range_impl(bucket_num, prev, last, disposer, cnt);
  1681. }
  1682. else{
  1683. //If found erase all equal values
  1684. bucket_type &b = this->priv_bucket_pointer()[bucket_num];
  1685. for(siterator end_sit = b.end(); it != end_sit; ++cnt, ++it){
  1686. slist_node_ptr n(it.pointed_node());
  1687. const value_type &v = this->priv_value_from_slist_node(n);
  1688. if(compare_hash){
  1689. std::size_t vh = this->priv_stored_or_compute_hash(v, store_hash_t());
  1690. if(h != vh || !equal_func(key, v)){
  1691. break;
  1692. }
  1693. }
  1694. else if(!equal_func(key, v)){
  1695. break;
  1696. }
  1697. this->priv_size_traits().decrement();
  1698. }
  1699. b.erase_after_and_dispose(prev, it, make_node_disposer(disposer));
  1700. }
  1701. this->priv_erasure_update_cache();
  1702. return cnt;
  1703. }
  1704. //! <b>Effects</b>: Erases all of the elements.
  1705. //!
  1706. //! <b>Complexity</b>: Linear to the number of elements on the container.
  1707. //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise.
  1708. //!
  1709. //! <b>Throws</b>: Nothing.
  1710. //!
  1711. //! <b>Note</b>: Invalidates the iterators (but not the references)
  1712. //! to the erased elements. No destructors are called.
  1713. void clear()
  1714. {
  1715. this->priv_clear_buckets();
  1716. this->priv_size_traits().set_size(size_type(0));
  1717. }
  1718. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1719. //!
  1720. //! <b>Effects</b>: Erases all of the elements.
  1721. //!
  1722. //! <b>Complexity</b>: Linear to the number of elements on the container.
  1723. //! Disposer::operator()(pointer) is called for the removed elements.
  1724. //!
  1725. //! <b>Throws</b>: Nothing.
  1726. //!
  1727. //! <b>Note</b>: Invalidates the iterators (but not the references)
  1728. //! to the erased elements. No destructors are called.
  1729. template<class Disposer>
  1730. void clear_and_dispose(Disposer disposer)
  1731. {
  1732. if(!constant_time_size || !this->empty()){
  1733. size_type num_buckets = this->bucket_count();
  1734. bucket_ptr b = this->priv_bucket_pointer();
  1735. for(; num_buckets--; ++b){
  1736. b->clear_and_dispose(make_node_disposer(disposer));
  1737. }
  1738. this->priv_size_traits().set_size(size_type(0));
  1739. }
  1740. this->priv_initialize_cache();
  1741. }
  1742. //! <b>Effects</b>: Returns the number of contained elements with the given value
  1743. //!
  1744. //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
  1745. //!
  1746. //! <b>Throws</b>: If the internal hasher or the equality functor throws.
  1747. size_type count(const_reference value) const
  1748. { return this->count(value, this->priv_hasher(), this->priv_equal()); }
  1749. //! <b>Requires</b>: "hash_func" must be a hash function that induces
  1750. //! the same hash values as the stored hasher. The difference is that
  1751. //! "hash_func" hashes the given key instead of the value_type.
  1752. //!
  1753. //! "equal_func" must be a equality function that induces
  1754. //! the same equality as key_equal. The difference is that
  1755. //! "equal_func" compares an arbitrary key with the contained values.
  1756. //!
  1757. //! <b>Effects</b>: Returns the number of contained elements with the given key
  1758. //!
  1759. //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
  1760. //!
  1761. //! <b>Throws</b>: If hash_func or equal throw.
  1762. template<class KeyType, class KeyHasher, class KeyValueEqual>
  1763. size_type count(const KeyType &key, const KeyHasher &hash_func, const KeyValueEqual &equal_func) const
  1764. {
  1765. size_type bucket_n1, bucket_n2, cnt;
  1766. this->priv_equal_range(key, hash_func, equal_func, bucket_n1, bucket_n2, cnt);
  1767. return cnt;
  1768. }
  1769. //! <b>Effects</b>: Finds an iterator to the first element is equal to
  1770. //! "value" or end() if that element does not exist.
  1771. //!
  1772. //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
  1773. //!
  1774. //! <b>Throws</b>: If the internal hasher or the equality functor throws.
  1775. iterator find(const_reference value)
  1776. { return this->find(value, this->priv_hasher(), this->priv_equal()); }
  1777. //! <b>Requires</b>: "hash_func" must be a hash function that induces
  1778. //! the same hash values as the stored hasher. The difference is that
  1779. //! "hash_func" hashes the given key instead of the value_type.
  1780. //!
  1781. //! "equal_func" must be a equality function that induces
  1782. //! the same equality as key_equal. The difference is that
  1783. //! "equal_func" compares an arbitrary key with the contained values.
  1784. //!
  1785. //! <b>Effects</b>: Finds an iterator to the first element whose key is
  1786. //! "key" according to the given hash and equality functor or end() if
  1787. //! that element does not exist.
  1788. //!
  1789. //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
  1790. //!
  1791. //! <b>Throws</b>: If hash_func or equal_func throw.
  1792. //!
  1793. //! <b>Note</b>: This function is used when constructing a value_type
  1794. //! is expensive and the value_type can be compared with a cheaper
  1795. //! key type. Usually this key is part of the value_type.
  1796. template<class KeyType, class KeyHasher, class KeyValueEqual>
  1797. iterator find(const KeyType &key, KeyHasher hash_func, KeyValueEqual equal_func)
  1798. {
  1799. size_type bucket_n;
  1800. std::size_t hash;
  1801. siterator prev;
  1802. siterator local_it = this->priv_find(key, hash_func, equal_func, bucket_n, hash, prev);
  1803. return iterator(local_it, &this->get_bucket_value_traits());
  1804. }
  1805. //! <b>Effects</b>: Finds a const_iterator to the first element whose key is
  1806. //! "key" or end() if that element does not exist.
  1807. //!
  1808. //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
  1809. //!
  1810. //! <b>Throws</b>: If the internal hasher or the equality functor throws.
  1811. const_iterator find(const_reference value) const
  1812. { return this->find(value, this->priv_hasher(), this->priv_equal()); }
  1813. //! <b>Requires</b>: "hash_func" must be a hash function that induces
  1814. //! the same hash values as the stored hasher. The difference is that
  1815. //! "hash_func" hashes the given key instead of the value_type.
  1816. //!
  1817. //! "equal_func" must be a equality function that induces
  1818. //! the same equality as key_equal. The difference is that
  1819. //! "equal_func" compares an arbitrary key with the contained values.
  1820. //!
  1821. //! <b>Effects</b>: Finds an iterator to the first element whose key is
  1822. //! "key" according to the given hasher and equality functor or end() if
  1823. //! that element does not exist.
  1824. //!
  1825. //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
  1826. //!
  1827. //! <b>Throws</b>: If hash_func or equal_func throw.
  1828. //!
  1829. //! <b>Note</b>: This function is used when constructing a value_type
  1830. //! is expensive and the value_type can be compared with a cheaper
  1831. //! key type. Usually this key is part of the value_type.
  1832. template<class KeyType, class KeyHasher, class KeyValueEqual>
  1833. const_iterator find
  1834. (const KeyType &key, KeyHasher hash_func, KeyValueEqual equal_func) const
  1835. {
  1836. size_type bucket_n;
  1837. std::size_t hash_value;
  1838. siterator prev;
  1839. siterator sit = this->priv_find(key, hash_func, equal_func, bucket_n, hash_value, prev);
  1840. return const_iterator(sit, &this->get_bucket_value_traits());
  1841. }
  1842. //! <b>Effects</b>: Returns a range containing all elements with values equivalent
  1843. //! to value. Returns std::make_pair(this->end(), this->end()) if no such
  1844. //! elements exist.
  1845. //!
  1846. //! <b>Complexity</b>: Average case O(this->count(value)). Worst case O(this->size()).
  1847. //!
  1848. //! <b>Throws</b>: If the internal hasher or the equality functor throws.
  1849. std::pair<iterator,iterator> equal_range(const_reference value)
  1850. { return this->equal_range(value, this->priv_hasher(), this->priv_equal()); }
  1851. //! <b>Requires</b>: "hash_func" must be a hash function that induces
  1852. //! the same hash values as the stored hasher. The difference is that
  1853. //! "hash_func" hashes the given key instead of the value_type.
  1854. //!
  1855. //! "equal_func" must be a equality function that induces
  1856. //! the same equality as key_equal. The difference is that
  1857. //! "equal_func" compares an arbitrary key with the contained values.
  1858. //!
  1859. //! <b>Effects</b>: Returns a range containing all elements with equivalent
  1860. //! keys. Returns std::make_pair(this->end(), this->end()) if no such
  1861. //! elements exist.
  1862. //!
  1863. //! <b>Complexity</b>: Average case O(this->count(key, hash_func, equal_func)).
  1864. //! Worst case O(this->size()).
  1865. //!
  1866. //! <b>Throws</b>: If hash_func or the equal_func throw.
  1867. //!
  1868. //! <b>Note</b>: This function is used when constructing a value_type
  1869. //! is expensive and the value_type can be compared with a cheaper
  1870. //! key type. Usually this key is part of the value_type.
  1871. template<class KeyType, class KeyHasher, class KeyValueEqual>
  1872. std::pair<iterator,iterator> equal_range
  1873. (const KeyType &key, KeyHasher hash_func, KeyValueEqual equal_func)
  1874. {
  1875. size_type bucket_n1, bucket_n2, cnt;
  1876. std::pair<siterator, siterator> ret = this->priv_equal_range
  1877. (key, hash_func, equal_func, bucket_n1, bucket_n2, cnt);
  1878. return std::pair<iterator, iterator>
  1879. (iterator(ret.first, &this->get_bucket_value_traits()), iterator(ret.second, &this->get_bucket_value_traits()));
  1880. }
  1881. //! <b>Effects</b>: Returns a range containing all elements with values equivalent
  1882. //! to value. Returns std::make_pair(this->end(), this->end()) if no such
  1883. //! elements exist.
  1884. //!
  1885. //! <b>Complexity</b>: Average case O(this->count(value)). Worst case O(this->size()).
  1886. //!
  1887. //! <b>Throws</b>: If the internal hasher or the equality functor throws.
  1888. std::pair<const_iterator, const_iterator>
  1889. equal_range(const_reference value) const
  1890. { return this->equal_range(value, this->priv_hasher(), this->priv_equal()); }
  1891. //! <b>Requires</b>: "hash_func" must be a hash function that induces
  1892. //! the same hash values as the stored hasher. The difference is that
  1893. //! "hash_func" hashes the given key instead of the value_type.
  1894. //!
  1895. //! "equal_func" must be a equality function that induces
  1896. //! the same equality as key_equal. The difference is that
  1897. //! "equal_func" compares an arbitrary key with the contained values.
  1898. //!
  1899. //! <b>Effects</b>: Returns a range containing all elements with equivalent
  1900. //! keys. Returns std::make_pair(this->end(), this->end()) if no such
  1901. //! elements exist.
  1902. //!
  1903. //! <b>Complexity</b>: Average case O(this->count(key, hash_func, equal_func)).
  1904. //! Worst case O(this->size()).
  1905. //!
  1906. //! <b>Throws</b>: If the hasher or equal_func throw.
  1907. //!
  1908. //! <b>Note</b>: This function is used when constructing a value_type
  1909. //! is expensive and the value_type can be compared with a cheaper
  1910. //! key type. Usually this key is part of the value_type.
  1911. template<class KeyType, class KeyHasher, class KeyValueEqual>
  1912. std::pair<const_iterator,const_iterator> equal_range
  1913. (const KeyType &key, KeyHasher hash_func, KeyValueEqual equal_func) const
  1914. {
  1915. size_type bucket_n1, bucket_n2, cnt;
  1916. std::pair<siterator, siterator> ret =
  1917. this->priv_equal_range(key, hash_func, equal_func, bucket_n1, bucket_n2, cnt);
  1918. return std::pair<const_iterator, const_iterator>
  1919. (const_iterator(ret.first, &this->get_bucket_value_traits()), const_iterator(ret.second, &this->get_bucket_value_traits()));
  1920. }
  1921. //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
  1922. //! appropriate type. Otherwise the behavior is undefined.
  1923. //!
  1924. //! <b>Effects</b>: Returns: a valid iterator belonging to the unordered_set
  1925. //! that points to the value
  1926. //!
  1927. //! <b>Complexity</b>: Constant.
  1928. //!
  1929. //! <b>Throws</b>: If the internal hash function throws.
  1930. iterator iterator_to(reference value)
  1931. {
  1932. return iterator(bucket_type::s_iterator_to(this->priv_value_to_node(value)), &this->get_bucket_value_traits());
  1933. }
  1934. //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
  1935. //! appropriate type. Otherwise the behavior is undefined.
  1936. //!
  1937. //! <b>Effects</b>: Returns: a valid const_iterator belonging to the
  1938. //! unordered_set that points to the value
  1939. //!
  1940. //! <b>Complexity</b>: Constant.
  1941. //!
  1942. //! <b>Throws</b>: If the internal hash function throws.
  1943. const_iterator iterator_to(const_reference value) const
  1944. {
  1945. siterator sit = bucket_type::s_iterator_to(const_cast<node &>(this->priv_value_to_node(value)));
  1946. return const_iterator(sit, &this->get_bucket_value_traits());
  1947. }
  1948. //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
  1949. //! appropriate type. Otherwise the behavior is undefined.
  1950. //!
  1951. //! <b>Effects</b>: Returns: a valid local_iterator belonging to the unordered_set
  1952. //! that points to the value
  1953. //!
  1954. //! <b>Complexity</b>: Constant.
  1955. //!
  1956. //! <b>Throws</b>: Nothing.
  1957. //!
  1958. //! <b>Note</b>: This static function is available only if the <i>value traits</i>
  1959. //! is stateless.
  1960. static local_iterator s_local_iterator_to(reference value)
  1961. {
  1962. BOOST_STATIC_ASSERT((!stateful_value_traits));
  1963. siterator sit = bucket_type::s_iterator_to(((hashtable_impl*)0)->priv_value_to_node(value));
  1964. return local_iterator(sit, const_real_value_traits_ptr());
  1965. }
  1966. //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
  1967. //! appropriate type. Otherwise the behavior is undefined.
  1968. //!
  1969. //! <b>Effects</b>: Returns: a valid const_local_iterator belonging to
  1970. //! the unordered_set that points to the value
  1971. //!
  1972. //! <b>Complexity</b>: Constant.
  1973. //!
  1974. //! <b>Throws</b>: Nothing.
  1975. //!
  1976. //! <b>Note</b>: This static function is available only if the <i>value traits</i>
  1977. //! is stateless.
  1978. static const_local_iterator s_local_iterator_to(const_reference value)
  1979. {
  1980. BOOST_STATIC_ASSERT((!stateful_value_traits));
  1981. siterator sit = bucket_type::s_iterator_to(((hashtable_impl*)0)->priv_value_to_node(const_cast<value_type&>(value)));
  1982. return const_local_iterator(sit, const_real_value_traits_ptr());
  1983. }
  1984. //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
  1985. //! appropriate type. Otherwise the behavior is undefined.
  1986. //!
  1987. //! <b>Effects</b>: Returns: a valid local_iterator belonging to the unordered_set
  1988. //! that points to the value
  1989. //!
  1990. //! <b>Complexity</b>: Constant.
  1991. //!
  1992. //! <b>Throws</b>: Nothing.
  1993. local_iterator local_iterator_to(reference value)
  1994. {
  1995. siterator sit = bucket_type::s_iterator_to(this->priv_value_to_node(value));
  1996. return local_iterator(sit, this->real_value_traits_ptr());
  1997. }
  1998. //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
  1999. //! appropriate type. Otherwise the behavior is undefined.
  2000. //!
  2001. //! <b>Effects</b>: Returns: a valid const_local_iterator belonging to
  2002. //! the unordered_set that points to the value
  2003. //!
  2004. //! <b>Complexity</b>: Constant.
  2005. //!
  2006. //! <b>Throws</b>: Nothing.
  2007. const_local_iterator local_iterator_to(const_reference value) const
  2008. {
  2009. siterator sit = bucket_type::s_iterator_to
  2010. (const_cast<node &>(this->priv_value_to_node(value)));
  2011. return const_local_iterator(sit, this->real_value_traits_ptr());
  2012. }
  2013. //! <b>Effects</b>: Returns the number of buckets passed in the constructor
  2014. //! or the last rehash function.
  2015. //!
  2016. //! <b>Complexity</b>: Constant.
  2017. //!
  2018. //! <b>Throws</b>: Nothing.
  2019. size_type bucket_count() const
  2020. { return this->priv_bucket_count(); }
  2021. //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
  2022. //!
  2023. //! <b>Effects</b>: Returns the number of elements in the nth bucket.
  2024. //!
  2025. //! <b>Complexity</b>: Constant.
  2026. //!
  2027. //! <b>Throws</b>: Nothing.
  2028. size_type bucket_size(size_type n) const
  2029. { return this->priv_bucket_pointer()[n].size(); }
  2030. //! <b>Effects</b>: Returns the index of the bucket in which elements
  2031. //! with keys equivalent to k would be found, if any such element existed.
  2032. //!
  2033. //! <b>Complexity</b>: Constant.
  2034. //!
  2035. //! <b>Throws</b>: If the hash functor throws.
  2036. //!
  2037. //! <b>Note</b>: the return value is in the range [0, this->bucket_count()).
  2038. size_type bucket(const key_type& k) const
  2039. { return this->bucket(k, this->priv_hasher()); }
  2040. //! <b>Requires</b>: "hash_func" must be a hash function that induces
  2041. //! the same hash values as the stored hasher. The difference is that
  2042. //! "hash_func" hashes the given key instead of the value_type.
  2043. //!
  2044. //! <b>Effects</b>: Returns the index of the bucket in which elements
  2045. //! with keys equivalent to k would be found, if any such element existed.
  2046. //!
  2047. //! <b>Complexity</b>: Constant.
  2048. //!
  2049. //! <b>Throws</b>: If hash_func throws.
  2050. //!
  2051. //! <b>Note</b>: the return value is in the range [0, this->bucket_count()).
  2052. template<class KeyType, class KeyHasher>
  2053. size_type bucket(const KeyType& k, const KeyHasher &hash_func) const
  2054. { return this->priv_hash_to_bucket(hash_func(k)); }
  2055. //! <b>Effects</b>: Returns the bucket array pointer passed in the constructor
  2056. //! or the last rehash function.
  2057. //!
  2058. //! <b>Complexity</b>: Constant.
  2059. //!
  2060. //! <b>Throws</b>: Nothing.
  2061. bucket_ptr bucket_pointer() const
  2062. { return this->priv_bucket_pointer(); }
  2063. //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
  2064. //!
  2065. //! <b>Effects</b>: Returns a local_iterator pointing to the beginning
  2066. //! of the sequence stored in the bucket n.
  2067. //!
  2068. //! <b>Complexity</b>: Constant.
  2069. //!
  2070. //! <b>Throws</b>: Nothing.
  2071. //!
  2072. //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
  2073. //! containing all of the elements in the nth bucket.
  2074. local_iterator begin(size_type n)
  2075. { return local_iterator(this->priv_bucket_pointer()[n].begin(), this->real_value_traits_ptr()); }
  2076. //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
  2077. //!
  2078. //! <b>Effects</b>: Returns a const_local_iterator pointing to the beginning
  2079. //! of the sequence stored in the bucket n.
  2080. //!
  2081. //! <b>Complexity</b>: Constant.
  2082. //!
  2083. //! <b>Throws</b>: Nothing.
  2084. //!
  2085. //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
  2086. //! containing all of the elements in the nth bucket.
  2087. const_local_iterator begin(size_type n) const
  2088. { return this->cbegin(n); }
  2089. //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
  2090. //!
  2091. //! <b>Effects</b>: Returns a const_local_iterator pointing to the beginning
  2092. //! of the sequence stored in the bucket n.
  2093. //!
  2094. //! <b>Complexity</b>: Constant.
  2095. //!
  2096. //! <b>Throws</b>: Nothing.
  2097. //!
  2098. //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
  2099. //! containing all of the elements in the nth bucket.
  2100. const_local_iterator cbegin(size_type n) const
  2101. {
  2102. siterator sit = const_cast<bucket_type&>(this->priv_bucket_pointer()[n]).begin();
  2103. return const_local_iterator(sit, this->real_value_traits_ptr());
  2104. }
  2105. //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
  2106. //!
  2107. //! <b>Effects</b>: Returns a local_iterator pointing to the end
  2108. //! of the sequence stored in the bucket n.
  2109. //!
  2110. //! <b>Complexity</b>: Constant.
  2111. //!
  2112. //! <b>Throws</b>: Nothing.
  2113. //!
  2114. //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
  2115. //! containing all of the elements in the nth bucket.
  2116. local_iterator end(size_type n)
  2117. { return local_iterator(this->priv_bucket_pointer()[n].end(), this->real_value_traits_ptr()); }
  2118. //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
  2119. //!
  2120. //! <b>Effects</b>: Returns a const_local_iterator pointing to the end
  2121. //! of the sequence stored in the bucket n.
  2122. //!
  2123. //! <b>Complexity</b>: Constant.
  2124. //!
  2125. //! <b>Throws</b>: Nothing.
  2126. //!
  2127. //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
  2128. //! containing all of the elements in the nth bucket.
  2129. const_local_iterator end(size_type n) const
  2130. { return this->cend(n); }
  2131. //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
  2132. //!
  2133. //! <b>Effects</b>: Returns a const_local_iterator pointing to the end
  2134. //! of the sequence stored in the bucket n.
  2135. //!
  2136. //! <b>Complexity</b>: Constant.
  2137. //!
  2138. //! <b>Throws</b>: Nothing.
  2139. //!
  2140. //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
  2141. //! containing all of the elements in the nth bucket.
  2142. const_local_iterator cend(size_type n) const
  2143. {
  2144. return const_local_iterator ( const_cast<bucket_type&>(this->priv_bucket_pointer()[n]).end()
  2145. , this->real_value_traits_ptr());
  2146. }
  2147. //! <b>Requires</b>: new_bucket_traits can hold a pointer to a new bucket array
  2148. //! or the same as the old bucket array with a different length. new_size is the length of the
  2149. //! the array pointed by new_buckets. If new_bucket_traits.bucket_begin() == this->bucket_pointer()
  2150. //! new_bucket_traits.bucket_count() can be bigger or smaller than this->bucket_count().
  2151. //! 'new_bucket_traits' copy constructor should not throw.
  2152. //!
  2153. //! <b>Effects</b>: Updates the internal reference with the new bucket, erases
  2154. //! the values from the old bucket and inserts then in the new one.
  2155. //! Bucket traits hold by *this is assigned from new_bucket_traits.
  2156. //! If the container is configured as incremental<>, the split bucket is set
  2157. //! to the new bucket_count().
  2158. //!
  2159. //! If store_hash option is true, this method does not use the hash function.
  2160. //!
  2161. //! <b>Complexity</b>: Average case linear in this->size(), worst case quadratic.
  2162. //!
  2163. //! <b>Throws</b>: If the hasher functor throws. Basic guarantee.
  2164. void rehash(const bucket_traits &new_bucket_traits)
  2165. {
  2166. const bucket_ptr new_buckets = new_bucket_traits.bucket_begin();
  2167. size_type new_bucket_count = new_bucket_traits.bucket_count();
  2168. const bucket_ptr old_buckets = this->priv_bucket_pointer();
  2169. size_type old_bucket_count = this->priv_bucket_count();
  2170. //Check power of two bucket array if the option is activated
  2171. BOOST_INTRUSIVE_INVARIANT_ASSERT
  2172. (!power_2_buckets || (0 == (new_bucket_count & (new_bucket_count-1u))));
  2173. size_type n = this->priv_get_cache_bucket_num();
  2174. const bool same_buffer = old_buckets == new_buckets;
  2175. //If the new bucket length is a common factor
  2176. //of the old one we can avoid hash calculations.
  2177. const bool fast_shrink = (!incremental) && (old_bucket_count > new_bucket_count) &&
  2178. (power_2_buckets ||(old_bucket_count % new_bucket_count) == 0);
  2179. //If we are shrinking the same bucket array and it's
  2180. //is a fast shrink, just rehash the last nodes
  2181. size_type new_first_bucket_num = new_bucket_count;
  2182. if(same_buffer && fast_shrink && (n < new_bucket_count)){
  2183. n = new_bucket_count;
  2184. new_first_bucket_num = this->priv_get_cache_bucket_num();
  2185. }
  2186. //Anti-exception stuff: they destroy the elements if something goes wrong.
  2187. //If the source and destination buckets are the same, the second rollback function
  2188. //is harmless, because all elements have been already unlinked and destroyed
  2189. typedef detail::init_disposer<node_algorithms> NodeDisposer;
  2190. NodeDisposer node_disp;
  2191. bucket_type & newbuck = new_buckets[0];
  2192. bucket_type & oldbuck = old_buckets[0];
  2193. detail::exception_array_disposer<bucket_type, NodeDisposer, size_type>
  2194. rollback1(newbuck, node_disp, new_bucket_count);
  2195. detail::exception_array_disposer<bucket_type, NodeDisposer, size_type>
  2196. rollback2(oldbuck, node_disp, old_bucket_count);
  2197. //Put size in a safe value for rollback exception
  2198. size_type size_backup = this->priv_size_traits().get_size();
  2199. this->priv_size_traits().set_size(0);
  2200. //Put cache to safe position
  2201. this->priv_initialize_cache();
  2202. this->priv_insertion_update_cache(size_type(0u));
  2203. //Iterate through nodes
  2204. for(; n < old_bucket_count; ++n){
  2205. bucket_type &old_bucket = old_buckets[n];
  2206. if(!fast_shrink){
  2207. siterator before_i(old_bucket.before_begin());
  2208. siterator end_sit(old_bucket.end());
  2209. siterator i(old_bucket.begin());
  2210. for(;i != end_sit; ++i){
  2211. const value_type &v = this->priv_value_from_slist_node(i.pointed_node());
  2212. const std::size_t hash_value = this->priv_stored_or_compute_hash(v, store_hash_t());
  2213. const size_type new_n = detail::hash_to_bucket_split<power_2_buckets, incremental>(hash_value, new_bucket_count, new_bucket_count);
  2214. if(cache_begin && new_n < new_first_bucket_num)
  2215. new_first_bucket_num = new_n;
  2216. siterator last = bucket_type::s_iterator_to
  2217. (*group_functions_t::get_last_in_group
  2218. (detail::dcast_bucket_ptr<node>(i.pointed_node()), optimize_multikey_t()));
  2219. if(same_buffer && new_n == n){
  2220. before_i = last;
  2221. }
  2222. else{
  2223. bucket_type &new_b = new_buckets[new_n];
  2224. new_b.splice_after(new_b.before_begin(), old_bucket, before_i, last);
  2225. }
  2226. i = before_i;
  2227. }
  2228. }
  2229. else{
  2230. const size_type new_n = detail::hash_to_bucket_split<power_2_buckets, incremental>(n, new_bucket_count, new_bucket_count);
  2231. if(cache_begin && new_n < new_first_bucket_num)
  2232. new_first_bucket_num = new_n;
  2233. bucket_type &new_b = new_buckets[new_n];
  2234. if(!old_bucket.empty()){
  2235. new_b.splice_after( new_b.before_begin()
  2236. , old_bucket
  2237. , old_bucket.before_begin()
  2238. , hashtable_impl::priv_get_last(old_bucket));
  2239. }
  2240. }
  2241. }
  2242. this->priv_size_traits().set_size(size_backup);
  2243. this->priv_split_traits().set_size(new_bucket_count);
  2244. this->priv_real_bucket_traits() = new_bucket_traits;
  2245. this->priv_initialize_cache();
  2246. this->priv_insertion_update_cache(new_first_bucket_num);
  2247. rollback1.release();
  2248. rollback2.release();
  2249. }
  2250. //! <b>Requires</b>:
  2251. //!
  2252. //! <b>Effects</b>:
  2253. //!
  2254. //! <b>Complexity</b>:
  2255. //!
  2256. //! <b>Throws</b>:
  2257. //!
  2258. //! <b>Note</b>: this method is only available if incremental<true> option is activated.
  2259. bool incremental_rehash(bool grow = true)
  2260. {
  2261. //This function is only available for containers with incremental hashing
  2262. BOOST_STATIC_ASSERT(( incremental && power_2_buckets ));
  2263. const size_type split_idx = this->priv_split_traits().get_size();
  2264. const size_type bucket_cnt = this->priv_bucket_count();
  2265. const bucket_ptr buck_ptr = this->priv_bucket_pointer();
  2266. if(grow){
  2267. //Test if the split variable can be changed
  2268. if(split_idx >= bucket_cnt)
  2269. return false;
  2270. const size_type bucket_to_rehash = split_idx - bucket_cnt/2;
  2271. bucket_type &old_bucket = buck_ptr[bucket_to_rehash];
  2272. siterator before_i(old_bucket.before_begin());
  2273. const siterator end_sit(old_bucket.end());
  2274. siterator i(old_bucket.begin());
  2275. this->priv_split_traits().increment();
  2276. //Anti-exception stuff: if an exception is thrown while
  2277. //moving elements from old_bucket to the target bucket, all moved
  2278. //elements are moved back to the original one.
  2279. detail::incremental_rehash_rollback<bucket_type, split_traits> rollback
  2280. ( buck_ptr[split_idx], old_bucket, this->priv_split_traits());
  2281. for(;i != end_sit; ++i){
  2282. const value_type &v = this->priv_value_from_slist_node(i.pointed_node());
  2283. const std::size_t hash_value = this->priv_stored_or_compute_hash(v, store_hash_t());
  2284. const size_type new_n = this->priv_hash_to_bucket(hash_value);
  2285. siterator last = bucket_type::s_iterator_to
  2286. (*group_functions_t::get_last_in_group
  2287. (detail::dcast_bucket_ptr<node>(i.pointed_node()), optimize_multikey_t()));
  2288. if(new_n == bucket_to_rehash){
  2289. before_i = last;
  2290. }
  2291. else{
  2292. bucket_type &new_b = buck_ptr[new_n];
  2293. new_b.splice_after(new_b.before_begin(), old_bucket, before_i, last);
  2294. }
  2295. i = before_i;
  2296. }
  2297. rollback.release();
  2298. this->priv_erasure_update_cache();
  2299. return true;
  2300. }
  2301. else{
  2302. //Test if the split variable can be changed
  2303. if(split_idx <= bucket_cnt/2)
  2304. return false;
  2305. const size_type target_bucket_num = split_idx - 1 - bucket_cnt/2;
  2306. bucket_type &target_bucket = buck_ptr[target_bucket_num];
  2307. bucket_type &source_bucket = buck_ptr[split_idx-1];
  2308. target_bucket.splice_after(target_bucket.cbefore_begin(), source_bucket);
  2309. this->priv_split_traits().decrement();
  2310. this->priv_insertion_update_cache(target_bucket_num);
  2311. return true;
  2312. }
  2313. }
  2314. //! <b>Effects</b>: If new_bucket_traits.bucket_count() is not
  2315. //! this->bucket_count()/2 or this->bucket_count()*2, or
  2316. //! this->split_bucket() != new_bucket_traits.bucket_count() returns false
  2317. //! and does nothing.
  2318. //!
  2319. //! Otherwise, copy assigns new_bucket_traits to the internal bucket_traits
  2320. //! and transfers all the objects from old buckets to the new ones.
  2321. //!
  2322. //! <b>Complexity</b>: Linear to size().
  2323. //!
  2324. //! <b>Throws</b>: Nothing
  2325. //!
  2326. //! <b>Note</b>: this method is only available if incremental<true> option is activated.
  2327. bool incremental_rehash(const bucket_traits &new_bucket_traits)
  2328. {
  2329. //This function is only available for containers with incremental hashing
  2330. BOOST_STATIC_ASSERT(( incremental && power_2_buckets ));
  2331. size_type new_bucket_traits_size = new_bucket_traits.bucket_count();
  2332. size_type cur_bucket_traits = this->priv_bucket_count();
  2333. if(new_bucket_traits_size/2 != cur_bucket_traits && new_bucket_traits_size != cur_bucket_traits/2){
  2334. return false;
  2335. }
  2336. const size_type split_idx = this->split_count();
  2337. if(new_bucket_traits_size/2 == cur_bucket_traits){
  2338. //Test if the split variable can be changed
  2339. if(!(split_idx >= cur_bucket_traits))
  2340. return false;
  2341. }
  2342. else{
  2343. //Test if the split variable can be changed
  2344. if(!(split_idx <= cur_bucket_traits/2))
  2345. return false;
  2346. }
  2347. const size_type ini_n = this->priv_get_cache_bucket_num();
  2348. const bucket_ptr old_buckets = this->priv_bucket_pointer();
  2349. this->priv_real_bucket_traits() = new_bucket_traits;
  2350. if(new_bucket_traits.bucket_begin() != old_buckets){
  2351. for(size_type n = ini_n; n < split_idx; ++n){
  2352. bucket_type &new_bucket = new_bucket_traits.bucket_begin()[n];
  2353. bucket_type &old_bucket = old_buckets[n];
  2354. new_bucket.splice_after(new_bucket.cbefore_begin(), old_bucket);
  2355. }
  2356. //Put cache to safe position
  2357. this->priv_initialize_cache();
  2358. this->priv_insertion_update_cache(ini_n);
  2359. }
  2360. return true;
  2361. }
  2362. //! <b>Requires</b>:
  2363. //!
  2364. //! <b>Effects</b>:
  2365. //!
  2366. //! <b>Complexity</b>:
  2367. //!
  2368. //! <b>Throws</b>:
  2369. size_type split_count() const
  2370. {
  2371. //This function is only available if incremental hashing is activated
  2372. BOOST_STATIC_ASSERT(( incremental && power_2_buckets ));
  2373. return this->priv_split_traits().get_size();
  2374. }
  2375. //! <b>Effects</b>: Returns the nearest new bucket count optimized for
  2376. //! the container that is bigger or equal than n. This suggestion can be
  2377. //! used to create bucket arrays with a size that will usually improve
  2378. //! container's performance. If such value does not exist, the
  2379. //! higher possible value is returned.
  2380. //!
  2381. //! <b>Complexity</b>: Amortized constant time.
  2382. //!
  2383. //! <b>Throws</b>: Nothing.
  2384. static size_type suggested_upper_bucket_count(size_type n)
  2385. {
  2386. const std::size_t *primes = &detail::prime_list_holder<0>::prime_list[0];
  2387. const std::size_t *primes_end = primes + detail::prime_list_holder<0>::prime_list_size;
  2388. std::size_t const* bound = std::lower_bound(primes, primes_end, n);
  2389. if(bound == primes_end)
  2390. --bound;
  2391. return size_type(*bound);
  2392. }
  2393. //! <b>Effects</b>: Returns the nearest new bucket count optimized for
  2394. //! the container that is smaller or equal than n. This suggestion can be
  2395. //! used to create bucket arrays with a size that will usually improve
  2396. //! container's performance. If such value does not exist, the
  2397. //! lowest possible value is returned.
  2398. //!
  2399. //! <b>Complexity</b>: Amortized constant time.
  2400. //!
  2401. //! <b>Throws</b>: Nothing.
  2402. static size_type suggested_lower_bucket_count(size_type n)
  2403. {
  2404. const std::size_t *primes = &detail::prime_list_holder<0>::prime_list[0];
  2405. const std::size_t *primes_end = primes + detail::prime_list_holder<0>::prime_list_size;
  2406. size_type const* bound = std::upper_bound(primes, primes_end, n);
  2407. if(bound != primes)
  2408. --bound;
  2409. return size_type(*bound);
  2410. }
  2411. /// @cond
  2412. private:
  2413. void priv_clear_buckets(bucket_ptr buckets_ptr, size_type bucket_cnt)
  2414. {
  2415. for(; bucket_cnt--; ++buckets_ptr){
  2416. if(safemode_or_autounlink){
  2417. bucket_plus_vtraits_t::priv_clear_group_nodes(*buckets_ptr, optimize_multikey_t());
  2418. buckets_ptr->clear_and_dispose(detail::init_disposer<node_algorithms>());
  2419. }
  2420. else{
  2421. buckets_ptr->clear();
  2422. }
  2423. }
  2424. this->priv_initialize_cache();
  2425. }
  2426. void priv_initialize_buckets()
  2427. { this->priv_clear_buckets(this->priv_bucket_pointer(), this->priv_bucket_count()); }
  2428. void priv_clear_buckets()
  2429. {
  2430. this->priv_clear_buckets
  2431. ( this->priv_get_cache()
  2432. , this->priv_bucket_count() - (this->priv_get_cache() - this->priv_bucket_pointer()));
  2433. }
  2434. std::size_t priv_hash_to_bucket(std::size_t hash_value) const
  2435. {
  2436. return detail::hash_to_bucket_split<power_2_buckets, incremental>
  2437. (hash_value, this->priv_real_bucket_traits().bucket_count(), this->priv_split_traits().get_size());
  2438. }
  2439. template<class Disposer>
  2440. void priv_erase_range_impl
  2441. (size_type bucket_num, siterator before_first_it, siterator end_sit, Disposer disposer, size_type &num_erased)
  2442. {
  2443. const bucket_ptr buckets = this->priv_bucket_pointer();
  2444. bucket_type &b = buckets[bucket_num];
  2445. if(before_first_it == b.before_begin() && end_sit == b.end()){
  2446. this->priv_erase_range_impl(bucket_num, 1, disposer, num_erased);
  2447. }
  2448. else{
  2449. num_erased = 0;
  2450. siterator to_erase(before_first_it);
  2451. ++to_erase;
  2452. slist_node_ptr end_ptr = end_sit.pointed_node();
  2453. while(to_erase != end_sit){
  2454. group_functions_t::erase_from_group(end_ptr, detail::dcast_bucket_ptr<node>(to_erase.pointed_node()), optimize_multikey_t());
  2455. to_erase = b.erase_after_and_dispose(before_first_it, make_node_disposer(disposer));
  2456. ++num_erased;
  2457. }
  2458. this->priv_size_traits().set_size(this->priv_size_traits().get_size()-num_erased);
  2459. }
  2460. }
  2461. template<class Disposer>
  2462. void priv_erase_range_impl
  2463. (size_type first_bucket_num, size_type num_buckets, Disposer disposer, size_type &num_erased)
  2464. {
  2465. //Now fully clear the intermediate buckets
  2466. const bucket_ptr buckets = this->priv_bucket_pointer();
  2467. num_erased = 0;
  2468. for(size_type i = first_bucket_num; i < (num_buckets + first_bucket_num); ++i){
  2469. bucket_type &b = buckets[i];
  2470. siterator b_begin(b.before_begin());
  2471. siterator nxt(b_begin);
  2472. ++nxt;
  2473. siterator end_sit(b.end());
  2474. while(nxt != end_sit){
  2475. group_functions_t::init_group(detail::dcast_bucket_ptr<node>(nxt.pointed_node()), optimize_multikey_t());
  2476. nxt = b.erase_after_and_dispose
  2477. (b_begin, make_node_disposer(disposer));
  2478. this->priv_size_traits().decrement();
  2479. ++num_erased;
  2480. }
  2481. }
  2482. }
  2483. template<class Disposer>
  2484. void priv_erase_range( siterator before_first_it, size_type first_bucket
  2485. , siterator last_it, size_type last_bucket
  2486. , Disposer disposer)
  2487. {
  2488. size_type num_erased;
  2489. if (first_bucket == last_bucket){
  2490. this->priv_erase_range_impl(first_bucket, before_first_it, last_it, disposer, num_erased);
  2491. }
  2492. else {
  2493. bucket_type *b = (&this->priv_bucket_pointer()[0]);
  2494. this->priv_erase_range_impl(first_bucket, before_first_it, b[first_bucket].end(), disposer, num_erased);
  2495. if(size_type n = (last_bucket - first_bucket - 1))
  2496. this->priv_erase_range_impl(first_bucket + 1, n, disposer, num_erased);
  2497. this->priv_erase_range_impl(last_bucket, b[last_bucket].before_begin(), last_it, disposer, num_erased);
  2498. }
  2499. }
  2500. std::size_t priv_get_bucket_num(siterator it)
  2501. { return this->priv_get_bucket_num_hash_dispatch(it, store_hash_t()); }
  2502. std::size_t priv_get_bucket_num_hash_dispatch(siterator it, detail::true_) //store_hash
  2503. {
  2504. return this->priv_hash_to_bucket
  2505. (this->priv_stored_hash(it.pointed_node(), store_hash_t()));
  2506. }
  2507. std::size_t priv_get_bucket_num_hash_dispatch(siterator it, detail::false_) //NO store_hash
  2508. { return this->priv_get_bucket_num_no_hash_store(it, optimize_multikey_t()); }
  2509. static siterator priv_get_previous(bucket_type &b, siterator i)
  2510. { return bucket_plus_vtraits_t::priv_get_previous(b, i, optimize_multikey_t()); }
  2511. static siterator priv_get_last(bucket_type &b)
  2512. { return bucket_plus_vtraits_t::priv_get_last(b, optimize_multikey_t()); }
  2513. template<class Disposer>
  2514. void priv_erase(const_iterator i, Disposer disposer, detail::true_)
  2515. {
  2516. slist_node_ptr elem(i.slist_it().pointed_node());
  2517. slist_node_ptr f_bucket_end, l_bucket_end;
  2518. if(store_hash){
  2519. f_bucket_end = l_bucket_end =
  2520. (this->priv_bucket_pointer()
  2521. [this->priv_hash_to_bucket
  2522. (this->priv_stored_hash(elem, store_hash_t()))
  2523. ]).before_begin().pointed_node();
  2524. }
  2525. else{
  2526. f_bucket_end = this->priv_bucket_pointer()->cend().pointed_node();
  2527. l_bucket_end = f_bucket_end + this->priv_bucket_count() - 1;
  2528. }
  2529. node_ptr nxt_in_group;
  2530. siterator prev = bucket_type::s_iterator_to
  2531. (*group_functions_t::get_previous_and_next_in_group
  2532. ( elem, nxt_in_group, f_bucket_end, l_bucket_end)
  2533. );
  2534. bucket_type::s_erase_after_and_dispose(prev, make_node_disposer(disposer));
  2535. if(nxt_in_group)
  2536. group_algorithms::unlink_after(nxt_in_group);
  2537. if(safemode_or_autounlink)
  2538. group_algorithms::init(detail::dcast_bucket_ptr<node>(elem));
  2539. }
  2540. template <class Disposer>
  2541. void priv_erase(const_iterator i, Disposer disposer, detail::false_)
  2542. {
  2543. siterator to_erase(i.slist_it());
  2544. bucket_type &b = this->priv_bucket_pointer()[this->priv_get_bucket_num(to_erase)];
  2545. siterator prev(this->priv_get_previous(b, to_erase));
  2546. b.erase_after_and_dispose(prev, make_node_disposer(disposer));
  2547. }
  2548. template<class KeyType, class KeyHasher, class KeyValueEqual>
  2549. siterator priv_find
  2550. ( const KeyType &key, KeyHasher hash_func
  2551. , KeyValueEqual equal_func, size_type &bucket_number, std::size_t &h, siterator &previt) const
  2552. {
  2553. h = hash_func(key);
  2554. return this->priv_find_with_hash(key, equal_func, bucket_number, h, previt);
  2555. }
  2556. template<class KeyType, class KeyValueEqual>
  2557. siterator priv_find_with_hash
  2558. ( const KeyType &key, KeyValueEqual equal_func, size_type &bucket_number, const std::size_t h, siterator &previt) const
  2559. {
  2560. bucket_number = this->priv_hash_to_bucket(h);
  2561. bucket_type &b = this->priv_bucket_pointer()[bucket_number];
  2562. previt = b.before_begin();
  2563. if(constant_time_size && this->empty()){
  2564. return this->priv_invalid_local_it();
  2565. }
  2566. siterator it = previt;
  2567. ++it;
  2568. while(it != b.end()){
  2569. const value_type &v = this->priv_value_from_slist_node(it.pointed_node());
  2570. if(compare_hash){
  2571. std::size_t vh = this->priv_stored_or_compute_hash(v, store_hash_t());
  2572. if(h == vh && equal_func(key, v)){
  2573. return it;
  2574. }
  2575. }
  2576. else if(equal_func(key, v)){
  2577. return it;
  2578. }
  2579. if(optimize_multikey){
  2580. previt = bucket_type::s_iterator_to
  2581. (*group_functions_t::get_last_in_group
  2582. (detail::dcast_bucket_ptr<node>(it.pointed_node()), optimize_multikey_t()));
  2583. it = previt;
  2584. }
  2585. else{
  2586. previt = it;
  2587. }
  2588. ++it;
  2589. }
  2590. previt = b.before_begin();
  2591. return this->priv_invalid_local_it();
  2592. }
  2593. iterator priv_insert_equal_with_hash(reference value, std::size_t hash_value)
  2594. {
  2595. size_type bucket_num;
  2596. siterator prev;
  2597. siterator it = this->priv_find_with_hash
  2598. (value, this->priv_equal(), bucket_num, hash_value, prev);
  2599. return this->priv_insert_equal_find(value, bucket_num, hash_value, it);
  2600. }
  2601. iterator priv_insert_equal_find(reference value, size_type bucket_num, std::size_t hash_value, siterator it)
  2602. {
  2603. bucket_type &b = this->priv_bucket_pointer()[bucket_num];
  2604. bool found_equal = it != this->priv_invalid_local_it();
  2605. if(!found_equal){
  2606. it = b.before_begin();
  2607. }
  2608. //Now store hash if needed
  2609. node_ptr n = pointer_traits<node_ptr>::pointer_to(this->priv_value_to_node(value));
  2610. node_functions_t::store_hash(n, hash_value, store_hash_t());
  2611. //Checks for some modes
  2612. if(safemode_or_autounlink)
  2613. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(n));
  2614. //Shortcut for optimize_multikey cases
  2615. if(optimize_multikey){
  2616. node_ptr first_in_group = found_equal ?
  2617. detail::dcast_bucket_ptr<node>(it.pointed_node()) : node_ptr();
  2618. group_functions_t::insert_in_group(first_in_group, n, optimize_multikey_t());
  2619. }
  2620. //Update cache and increment size if needed
  2621. this->priv_insertion_update_cache(bucket_num);
  2622. this->priv_size_traits().increment();
  2623. //Insert the element in the bucket after it
  2624. return iterator(b.insert_after(it, *n), &this->get_bucket_value_traits());
  2625. }
  2626. template<class KeyType, class KeyHasher, class KeyValueEqual>
  2627. std::pair<siterator, siterator> priv_equal_range
  2628. ( const KeyType &key
  2629. , KeyHasher hash_func
  2630. , KeyValueEqual equal_func
  2631. , size_type &bucket_number_first
  2632. , size_type &bucket_number_second
  2633. , size_type &cnt) const
  2634. {
  2635. std::size_t h;
  2636. cnt = 0;
  2637. siterator prev;
  2638. //Let's see if the element is present
  2639. std::pair<siterator, siterator> to_return
  2640. ( this->priv_find(key, hash_func, equal_func, bucket_number_first, h, prev)
  2641. , this->priv_invalid_local_it());
  2642. if(to_return.first == to_return.second){
  2643. bucket_number_second = bucket_number_first;
  2644. return to_return;
  2645. }
  2646. {
  2647. //If it's present, find the first that it's not equal in
  2648. //the same bucket
  2649. bucket_type &b = this->priv_bucket_pointer()[bucket_number_first];
  2650. siterator it = to_return.first;
  2651. if(optimize_multikey){
  2652. to_return.second = bucket_type::s_iterator_to
  2653. (*node_traits::get_next(group_functions_t::get_last_in_group
  2654. (detail::dcast_bucket_ptr<node>(it.pointed_node()), optimize_multikey_t())));
  2655. cnt = std::distance(it, to_return.second);
  2656. if(to_return.second != b.end()){
  2657. bucket_number_second = bucket_number_first;
  2658. return to_return;
  2659. }
  2660. }
  2661. else{
  2662. ++cnt;
  2663. ++it;
  2664. while(it != b.end()){
  2665. const value_type &v = this->priv_value_from_slist_node(it.pointed_node());
  2666. if(compare_hash){
  2667. std::size_t hv = this->priv_stored_or_compute_hash(v, store_hash_t());
  2668. if(hv != h || !equal_func(key, v)){
  2669. to_return.second = it;
  2670. bucket_number_second = bucket_number_first;
  2671. return to_return;
  2672. }
  2673. }
  2674. else if(!equal_func(key, v)){
  2675. to_return.second = it;
  2676. bucket_number_second = bucket_number_first;
  2677. return to_return;
  2678. }
  2679. ++it;
  2680. ++cnt;
  2681. }
  2682. }
  2683. }
  2684. //If we reached the end, find the first, non-empty bucket
  2685. for(bucket_number_second = bucket_number_first+1
  2686. ; bucket_number_second != this->priv_bucket_count()
  2687. ; ++bucket_number_second){
  2688. bucket_type &b = this->priv_bucket_pointer()[bucket_number_second];
  2689. if(!b.empty()){
  2690. to_return.second = b.begin();
  2691. return to_return;
  2692. }
  2693. }
  2694. //Otherwise, return the end node
  2695. to_return.second = this->priv_invalid_local_it();
  2696. return to_return;
  2697. }
  2698. /// @endcond
  2699. };
  2700. /// @cond
  2701. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  2702. template < class T
  2703. , bool UniqueKeys
  2704. , class PackedOptions
  2705. >
  2706. #else
  2707. template <class T, bool UniqueKeys, class ...Options>
  2708. #endif
  2709. struct make_real_bucket_traits
  2710. {
  2711. //Real value traits must be calculated from options
  2712. typedef typename detail::get_value_traits
  2713. <T, typename PackedOptions::proto_value_traits>::type value_traits;
  2714. /*
  2715. static const bool resizable_bucket_traits =
  2716. detail::resizable_bool_is_true<bucket_traits_traits>::value;*/
  2717. typedef typename detail::get_real_value_traits<value_traits>::type real_value_traits;
  2718. typedef typename PackedOptions::bucket_traits specified_bucket_traits;
  2719. //Real bucket traits must be calculated from options and calculated value_traits
  2720. typedef typename detail::get_slist_impl
  2721. <typename detail::reduced_slist_node_traits
  2722. <typename real_value_traits::node_traits>::type
  2723. >::type slist_impl;
  2724. typedef typename
  2725. detail::if_c< detail::is_same
  2726. < specified_bucket_traits
  2727. , default_bucket_traits
  2728. >::value
  2729. , detail::bucket_traits_impl<slist_impl>
  2730. , specified_bucket_traits
  2731. >::type type;
  2732. };
  2733. /// @endcond
  2734. //! Helper metafunction to define a \c hashtable that yields to the same type when the
  2735. //! same options (either explicitly or implicitly) are used.
  2736. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  2737. template<class T, class ...Options>
  2738. #else
  2739. template<class T, class O1 = void, class O2 = void
  2740. , class O3 = void, class O4 = void
  2741. , class O5 = void, class O6 = void
  2742. , class O7 = void, class O8 = void
  2743. , class O9 = void, class O10= void
  2744. >
  2745. #endif
  2746. struct make_hashtable
  2747. {
  2748. /// @cond
  2749. typedef typename pack_options
  2750. < hashtable_defaults,
  2751. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  2752. O1, O2, O3, O4, O5, O6, O7, O8, O9, O10
  2753. #else
  2754. Options...
  2755. #endif
  2756. >::type packed_options;
  2757. typedef typename detail::get_value_traits
  2758. <T, typename packed_options::proto_value_traits>::type value_traits;
  2759. typedef typename make_real_bucket_traits
  2760. <T, false, packed_options>::type real_bucket_traits;
  2761. typedef hashtable_impl
  2762. < value_traits
  2763. , typename packed_options::hash
  2764. , typename packed_options::equal
  2765. , typename packed_options::size_type
  2766. , real_bucket_traits
  2767. , (std::size_t(false)*hash_bool_flags::unique_keys_pos)
  2768. | (std::size_t(packed_options::constant_time_size)*hash_bool_flags::constant_time_size_pos)
  2769. | (std::size_t(packed_options::power_2_buckets)*hash_bool_flags::power_2_buckets_pos)
  2770. | (std::size_t(packed_options::cache_begin)*hash_bool_flags::cache_begin_pos)
  2771. | (std::size_t(packed_options::compare_hash)*hash_bool_flags::compare_hash_pos)
  2772. | (std::size_t(packed_options::incremental)*hash_bool_flags::incremental_pos)
  2773. > implementation_defined;
  2774. /// @endcond
  2775. typedef implementation_defined type;
  2776. };
  2777. #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  2778. #if defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  2779. template<class T, class ...Options>
  2780. #else
  2781. template<class T, class O1, class O2, class O3, class O4, class O5, class O6, class O7, class O8, class O9, class O10>
  2782. #endif
  2783. class hashtable
  2784. : public make_hashtable<T,
  2785. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  2786. O1, O2, O3, O4, O5, O6, O7, O8, O9, O10
  2787. #else
  2788. Options...
  2789. #endif
  2790. >::type
  2791. {
  2792. typedef typename make_hashtable<T,
  2793. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  2794. O1, O2, O3, O4, O5, O6, O7, O8, O9, O10
  2795. #else
  2796. Options...
  2797. #endif
  2798. >::type Base;
  2799. BOOST_MOVABLE_BUT_NOT_COPYABLE(hashtable)
  2800. public:
  2801. typedef typename Base::value_traits value_traits;
  2802. typedef typename Base::real_value_traits real_value_traits;
  2803. typedef typename Base::iterator iterator;
  2804. typedef typename Base::const_iterator const_iterator;
  2805. typedef typename Base::bucket_ptr bucket_ptr;
  2806. typedef typename Base::size_type size_type;
  2807. typedef typename Base::hasher hasher;
  2808. typedef typename Base::bucket_traits bucket_traits;
  2809. typedef typename Base::key_equal key_equal;
  2810. //Assert if passed value traits are compatible with the type
  2811. BOOST_STATIC_ASSERT((detail::is_same<typename real_value_traits::value_type, T>::value));
  2812. explicit hashtable ( const bucket_traits &b_traits
  2813. , const hasher & hash_func = hasher()
  2814. , const key_equal &equal_func = key_equal()
  2815. , const value_traits &v_traits = value_traits())
  2816. : Base(b_traits, hash_func, equal_func, v_traits)
  2817. {}
  2818. hashtable(BOOST_RV_REF(hashtable) x)
  2819. : Base(::boost::move(static_cast<Base&>(x)))
  2820. {}
  2821. hashtable& operator=(BOOST_RV_REF(hashtable) x)
  2822. { return static_cast<hashtable&>(this->Base::operator=(::boost::move(static_cast<Base&>(x)))); }
  2823. };
  2824. #endif
  2825. } //namespace intrusive
  2826. } //namespace boost
  2827. #include <boost/intrusive/detail/config_end.hpp>
  2828. #endif //BOOST_INTRUSIVE_HASHTABLE_HPP