hashed_index.hpp 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802
  1. /* Copyright 2003-2020 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/multi_index for library home page.
  7. */
  8. #ifndef BOOST_MULTI_INDEX_HASHED_INDEX_HPP
  9. #define BOOST_MULTI_INDEX_HASHED_INDEX_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  14. #include <algorithm>
  15. #include <boost/call_traits.hpp>
  16. #include <boost/core/addressof.hpp>
  17. #include <boost/core/no_exceptions_support.hpp>
  18. #include <boost/detail/workaround.hpp>
  19. #include <boost/foreach_fwd.hpp>
  20. #include <boost/limits.hpp>
  21. #include <boost/move/core.hpp>
  22. #include <boost/move/utility_core.hpp>
  23. #include <boost/mpl/bool.hpp>
  24. #include <boost/mpl/if.hpp>
  25. #include <boost/mpl/push_front.hpp>
  26. #include <boost/multi_index/detail/access_specifier.hpp>
  27. #include <boost/multi_index/detail/adl_swap.hpp>
  28. #include <boost/multi_index/detail/allocator_traits.hpp>
  29. #include <boost/multi_index/detail/auto_space.hpp>
  30. #include <boost/multi_index/detail/bucket_array.hpp>
  31. #include <boost/multi_index/detail/do_not_copy_elements_tag.hpp>
  32. #include <boost/multi_index/detail/hash_index_iterator.hpp>
  33. #include <boost/multi_index/detail/index_node_base.hpp>
  34. #include <boost/multi_index/detail/modify_key_adaptor.hpp>
  35. #include <boost/multi_index/detail/node_handle.hpp>
  36. #include <boost/multi_index/detail/promotes_arg.hpp>
  37. #include <boost/multi_index/detail/safe_mode.hpp>
  38. #include <boost/multi_index/detail/scope_guard.hpp>
  39. #include <boost/multi_index/detail/vartempl_support.hpp>
  40. #include <boost/multi_index/hashed_index_fwd.hpp>
  41. #include <boost/tuple/tuple.hpp>
  42. #include <boost/type_traits/is_same.hpp>
  43. #include <cmath>
  44. #include <cstddef>
  45. #include <functional>
  46. #include <iterator>
  47. #include <utility>
  48. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  49. #include <initializer_list>
  50. #endif
  51. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  52. #include <boost/serialization/nvp.hpp>
  53. #endif
  54. #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)
  55. #define BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT_OF(x) \
  56. detail::scope_guard BOOST_JOIN(check_invariant_,__LINE__)= \
  57. detail::make_obj_guard(x,&hashed_index::check_invariant_); \
  58. BOOST_JOIN(check_invariant_,__LINE__).touch();
  59. #define BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT \
  60. BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT_OF(*this)
  61. #else
  62. #define BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT_OF(x)
  63. #define BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT
  64. #endif
  65. namespace boost{
  66. namespace multi_index{
  67. namespace detail{
  68. /* hashed_index adds a layer of hashed indexing to a given Super */
  69. /* Most of the implementation of unique and non-unique indices is
  70. * shared. We tell from one another on instantiation time by using
  71. * Category tags defined in hash_index_node.hpp.
  72. */
  73. template<
  74. typename KeyFromValue,typename Hash,typename Pred,
  75. typename SuperMeta,typename TagList,typename Category
  76. >
  77. class hashed_index:
  78. BOOST_MULTI_INDEX_PROTECTED_IF_MEMBER_TEMPLATE_FRIENDS SuperMeta::type
  79. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  80. ,public safe_mode::safe_container<
  81. hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category> >
  82. #endif
  83. {
  84. #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)&&\
  85. BOOST_WORKAROUND(__MWERKS__,<=0x3003)
  86. /* The "ISO C++ Template Parser" option in CW8.3 has a problem with the
  87. * lifetime of const references bound to temporaries --precisely what
  88. * scopeguards are.
  89. */
  90. #pragma parse_mfunc_templ off
  91. #endif
  92. typedef typename SuperMeta::type super;
  93. protected:
  94. typedef hashed_index_node<
  95. typename super::index_node_type> index_node_type;
  96. private:
  97. typedef typename index_node_type::
  98. template node_alg<Category>::type node_alg;
  99. typedef typename index_node_type::impl_type node_impl_type;
  100. typedef typename node_impl_type::pointer node_impl_pointer;
  101. typedef typename node_impl_type::base_pointer node_impl_base_pointer;
  102. typedef bucket_array<
  103. typename super::final_allocator_type> bucket_array_type;
  104. public:
  105. /* types */
  106. typedef typename KeyFromValue::result_type key_type;
  107. typedef typename index_node_type::value_type value_type;
  108. typedef KeyFromValue key_from_value;
  109. typedef Hash hasher;
  110. typedef Pred key_equal;
  111. typedef typename super::final_allocator_type allocator_type;
  112. private:
  113. typedef allocator_traits<allocator_type> alloc_traits;
  114. public:
  115. typedef typename alloc_traits::pointer pointer;
  116. typedef typename alloc_traits::const_pointer const_pointer;
  117. typedef value_type& reference;
  118. typedef const value_type& const_reference;
  119. typedef typename alloc_traits::size_type size_type;
  120. typedef typename alloc_traits::difference_type difference_type;
  121. typedef tuple<size_type,
  122. key_from_value,hasher,key_equal> ctor_args;
  123. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  124. typedef safe_mode::safe_iterator<
  125. hashed_index_iterator<
  126. index_node_type,bucket_array_type,
  127. Category,
  128. hashed_index_global_iterator_tag>,
  129. hashed_index> iterator;
  130. #else
  131. typedef hashed_index_iterator<
  132. index_node_type,bucket_array_type,
  133. Category,hashed_index_global_iterator_tag> iterator;
  134. #endif
  135. typedef iterator const_iterator;
  136. typedef hashed_index_iterator<
  137. index_node_type,bucket_array_type,
  138. Category,hashed_index_local_iterator_tag> local_iterator;
  139. typedef local_iterator const_local_iterator;
  140. typedef typename super::final_node_handle_type node_type;
  141. typedef detail::insert_return_type<
  142. iterator,node_type> insert_return_type;
  143. typedef TagList tag_list;
  144. protected:
  145. typedef typename super::final_node_type final_node_type;
  146. typedef tuples::cons<
  147. ctor_args,
  148. typename super::ctor_args_list> ctor_args_list;
  149. typedef typename mpl::push_front<
  150. typename super::index_type_list,
  151. hashed_index>::type index_type_list;
  152. typedef typename mpl::push_front<
  153. typename super::iterator_type_list,
  154. iterator>::type iterator_type_list;
  155. typedef typename mpl::push_front<
  156. typename super::const_iterator_type_list,
  157. const_iterator>::type const_iterator_type_list;
  158. typedef typename super::copy_map_type copy_map_type;
  159. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  160. typedef typename super::index_saver_type index_saver_type;
  161. typedef typename super::index_loader_type index_loader_type;
  162. #endif
  163. private:
  164. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  165. typedef safe_mode::safe_container<
  166. hashed_index> safe_super;
  167. #endif
  168. typedef typename call_traits<value_type>::param_type value_param_type;
  169. typedef typename call_traits<
  170. key_type>::param_type key_param_type;
  171. /* Needed to avoid commas in BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL
  172. * expansion.
  173. */
  174. typedef std::pair<iterator,bool> emplace_return_type;
  175. public:
  176. /* construct/destroy/copy
  177. * Default and copy ctors are in the protected section as indices are
  178. * not supposed to be created on their own. No range ctor either.
  179. */
  180. hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& operator=(
  181. const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x)
  182. {
  183. this->final()=x.final();
  184. return *this;
  185. }
  186. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  187. hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& operator=(
  188. std::initializer_list<value_type> list)
  189. {
  190. this->final()=list;
  191. return *this;
  192. }
  193. #endif
  194. allocator_type get_allocator()const BOOST_NOEXCEPT
  195. {
  196. return this->final().get_allocator();
  197. }
  198. /* size and capacity */
  199. bool empty()const BOOST_NOEXCEPT{return this->final_empty_();}
  200. size_type size()const BOOST_NOEXCEPT{return this->final_size_();}
  201. size_type max_size()const BOOST_NOEXCEPT{return this->final_max_size_();}
  202. /* iterators */
  203. iterator begin()BOOST_NOEXCEPT
  204. {
  205. return make_iterator(
  206. index_node_type::from_impl(header()->next()->prior()));
  207. }
  208. const_iterator begin()const BOOST_NOEXCEPT
  209. {
  210. return make_iterator(
  211. index_node_type::from_impl(header()->next()->prior()));
  212. }
  213. iterator end()BOOST_NOEXCEPT{return make_iterator(header());}
  214. const_iterator end()const BOOST_NOEXCEPT{return make_iterator(header());}
  215. const_iterator cbegin()const BOOST_NOEXCEPT{return begin();}
  216. const_iterator cend()const BOOST_NOEXCEPT{return end();}
  217. iterator iterator_to(const value_type& x)
  218. {
  219. return make_iterator(
  220. node_from_value<index_node_type>(boost::addressof(x)));
  221. }
  222. const_iterator iterator_to(const value_type& x)const
  223. {
  224. return make_iterator(
  225. node_from_value<index_node_type>(boost::addressof(x)));
  226. }
  227. /* modifiers */
  228. BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL(
  229. emplace_return_type,emplace,emplace_impl)
  230. BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL_EXTRA_ARG(
  231. iterator,emplace_hint,emplace_hint_impl,iterator,position)
  232. std::pair<iterator,bool> insert(const value_type& x)
  233. {
  234. BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
  235. std::pair<final_node_type*,bool> p=this->final_insert_(x);
  236. return std::pair<iterator,bool>(make_iterator(p.first),p.second);
  237. }
  238. std::pair<iterator,bool> insert(BOOST_RV_REF(value_type) x)
  239. {
  240. BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
  241. std::pair<final_node_type*,bool> p=this->final_insert_rv_(x);
  242. return std::pair<iterator,bool>(make_iterator(p.first),p.second);
  243. }
  244. iterator insert(iterator position,const value_type& x)
  245. {
  246. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  247. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  248. BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
  249. std::pair<final_node_type*,bool> p=this->final_insert_(
  250. x,static_cast<final_node_type*>(position.get_node()));
  251. return make_iterator(p.first);
  252. }
  253. iterator insert(iterator position,BOOST_RV_REF(value_type) x)
  254. {
  255. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  256. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  257. BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
  258. std::pair<final_node_type*,bool> p=this->final_insert_rv_(
  259. x,static_cast<final_node_type*>(position.get_node()));
  260. return make_iterator(p.first);
  261. }
  262. template<typename InputIterator>
  263. void insert(InputIterator first,InputIterator last)
  264. {
  265. BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
  266. for(;first!=last;++first)this->final_insert_ref_(*first);
  267. }
  268. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  269. void insert(std::initializer_list<value_type> list)
  270. {
  271. insert(list.begin(),list.end());
  272. }
  273. #endif
  274. insert_return_type insert(BOOST_RV_REF(node_type) nh)
  275. {
  276. if(nh)BOOST_MULTI_INDEX_CHECK_EQUAL_ALLOCATORS(*this,nh);
  277. BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
  278. std::pair<final_node_type*,bool> p=this->final_insert_nh_(nh);
  279. return insert_return_type(make_iterator(p.first),p.second,boost::move(nh));
  280. }
  281. iterator insert(const_iterator position,BOOST_RV_REF(node_type) nh)
  282. {
  283. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  284. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  285. if(nh)BOOST_MULTI_INDEX_CHECK_EQUAL_ALLOCATORS(*this,nh);
  286. BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
  287. std::pair<final_node_type*,bool> p=this->final_insert_nh_(
  288. nh,static_cast<final_node_type*>(position.get_node()));
  289. return make_iterator(p.first);
  290. }
  291. node_type extract(const_iterator position)
  292. {
  293. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  294. BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
  295. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  296. BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
  297. return this->final_extract_(
  298. static_cast<final_node_type*>(position.get_node()));
  299. }
  300. node_type extract(key_param_type x)
  301. {
  302. iterator position=find(x);
  303. if(position==end())return node_type();
  304. else return extract(position);
  305. }
  306. iterator erase(iterator position)
  307. {
  308. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  309. BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
  310. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  311. BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
  312. this->final_erase_(static_cast<final_node_type*>(position++.get_node()));
  313. return position;
  314. }
  315. size_type erase(key_param_type k)
  316. {
  317. BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
  318. std::size_t buc=buckets.position(hash_(k));
  319. for(node_impl_pointer x=buckets.at(buc)->prior();
  320. x!=node_impl_pointer(0);x=node_alg::next_to_inspect(x)){
  321. if(eq_(k,key(index_node_type::from_impl(x)->value()))){
  322. node_impl_pointer y=end_of_range(x);
  323. size_type s=0;
  324. do{
  325. node_impl_pointer z=node_alg::after(x);
  326. this->final_erase_(
  327. static_cast<final_node_type*>(index_node_type::from_impl(x)));
  328. x=z;
  329. ++s;
  330. }while(x!=y);
  331. return s;
  332. }
  333. }
  334. return 0;
  335. }
  336. iterator erase(iterator first,iterator last)
  337. {
  338. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(first);
  339. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(last);
  340. BOOST_MULTI_INDEX_CHECK_IS_OWNER(first,*this);
  341. BOOST_MULTI_INDEX_CHECK_IS_OWNER(last,*this);
  342. BOOST_MULTI_INDEX_CHECK_VALID_RANGE(first,last);
  343. BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
  344. while(first!=last){
  345. first=erase(first);
  346. }
  347. return first;
  348. }
  349. bool replace(iterator position,const value_type& x)
  350. {
  351. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  352. BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
  353. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  354. BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
  355. return this->final_replace_(
  356. x,static_cast<final_node_type*>(position.get_node()));
  357. }
  358. bool replace(iterator position,BOOST_RV_REF(value_type) x)
  359. {
  360. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  361. BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
  362. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  363. BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
  364. return this->final_replace_rv_(
  365. x,static_cast<final_node_type*>(position.get_node()));
  366. }
  367. template<typename Modifier>
  368. bool modify(iterator position,Modifier mod)
  369. {
  370. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  371. BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
  372. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  373. BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
  374. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  375. /* MSVC++ 6.0 optimizer on safe mode code chokes if this
  376. * this is not added. Left it for all compilers as it does no
  377. * harm.
  378. */
  379. position.detach();
  380. #endif
  381. return this->final_modify_(
  382. mod,static_cast<final_node_type*>(position.get_node()));
  383. }
  384. template<typename Modifier,typename Rollback>
  385. bool modify(iterator position,Modifier mod,Rollback back_)
  386. {
  387. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  388. BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
  389. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  390. BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
  391. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  392. /* MSVC++ 6.0 optimizer on safe mode code chokes if this
  393. * this is not added. Left it for all compilers as it does no
  394. * harm.
  395. */
  396. position.detach();
  397. #endif
  398. return this->final_modify_(
  399. mod,back_,static_cast<final_node_type*>(position.get_node()));
  400. }
  401. template<typename Modifier>
  402. bool modify_key(iterator position,Modifier mod)
  403. {
  404. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  405. BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
  406. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  407. BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
  408. return modify(
  409. position,modify_key_adaptor<Modifier,value_type,KeyFromValue>(mod,key));
  410. }
  411. template<typename Modifier,typename Rollback>
  412. bool modify_key(iterator position,Modifier mod,Rollback back_)
  413. {
  414. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  415. BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
  416. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  417. BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
  418. return modify(
  419. position,
  420. modify_key_adaptor<Modifier,value_type,KeyFromValue>(mod,key),
  421. modify_key_adaptor<Rollback,value_type,KeyFromValue>(back_,key));
  422. }
  423. void clear()BOOST_NOEXCEPT
  424. {
  425. BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
  426. this->final_clear_();
  427. }
  428. void swap(hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x)
  429. {
  430. BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
  431. BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT_OF(x);
  432. this->final_swap_(x.final());
  433. }
  434. /* observers */
  435. key_from_value key_extractor()const{return key;}
  436. hasher hash_function()const{return hash_;}
  437. key_equal key_eq()const{return eq_;}
  438. /* lookup */
  439. /* Internally, these ops rely on const_iterator being the same
  440. * type as iterator.
  441. */
  442. /* Implementation note: When CompatibleKey is consistently promoted to
  443. * KeyFromValue::result_type for equality comparison, the promotion is made
  444. * once in advance to increase efficiency.
  445. */
  446. template<typename CompatibleKey>
  447. iterator find(const CompatibleKey& k)const
  448. {
  449. return find(k,hash_,eq_);
  450. }
  451. template<
  452. typename CompatibleKey,typename CompatibleHash,typename CompatiblePred
  453. >
  454. iterator find(
  455. const CompatibleKey& k,
  456. const CompatibleHash& hash,const CompatiblePred& eq)const
  457. {
  458. return find(
  459. k,hash,eq,promotes_1st_arg<CompatiblePred,CompatibleKey,key_type>());
  460. }
  461. template<typename CompatibleKey>
  462. size_type count(const CompatibleKey& k)const
  463. {
  464. return count(k,hash_,eq_);
  465. }
  466. template<
  467. typename CompatibleKey,typename CompatibleHash,typename CompatiblePred
  468. >
  469. size_type count(
  470. const CompatibleKey& k,
  471. const CompatibleHash& hash,const CompatiblePred& eq)const
  472. {
  473. return count(
  474. k,hash,eq,promotes_1st_arg<CompatiblePred,CompatibleKey,key_type>());
  475. }
  476. template<typename CompatibleKey>
  477. std::pair<iterator,iterator> equal_range(const CompatibleKey& k)const
  478. {
  479. return equal_range(k,hash_,eq_);
  480. }
  481. template<
  482. typename CompatibleKey,typename CompatibleHash,typename CompatiblePred
  483. >
  484. std::pair<iterator,iterator> equal_range(
  485. const CompatibleKey& k,
  486. const CompatibleHash& hash,const CompatiblePred& eq)const
  487. {
  488. return equal_range(
  489. k,hash,eq,promotes_1st_arg<CompatiblePred,CompatibleKey,key_type>());
  490. }
  491. /* bucket interface */
  492. size_type bucket_count()const BOOST_NOEXCEPT
  493. {
  494. return static_cast<size_type>(buckets.size());
  495. }
  496. size_type max_bucket_count()const BOOST_NOEXCEPT{return static_cast<size_type>(-1);}
  497. size_type bucket_size(size_type n)const
  498. {
  499. size_type res=0;
  500. for(node_impl_pointer x=buckets.at(n)->prior();
  501. x!=node_impl_pointer(0);x=node_alg::after_local(x)){
  502. ++res;
  503. }
  504. return res;
  505. }
  506. size_type bucket(key_param_type k)const
  507. {
  508. return static_cast<size_type>(buckets.position(hash_(k)));
  509. }
  510. local_iterator begin(size_type n)
  511. {
  512. return const_cast<const hashed_index*>(this)->begin(n);
  513. }
  514. const_local_iterator begin(size_type n)const
  515. {
  516. node_impl_pointer x=buckets.at(n)->prior();
  517. if(x==node_impl_pointer(0))return end(n);
  518. return make_local_iterator(index_node_type::from_impl(x));
  519. }
  520. local_iterator end(size_type n)
  521. {
  522. return const_cast<const hashed_index*>(this)->end(n);
  523. }
  524. const_local_iterator end(size_type)const
  525. {
  526. return make_local_iterator(0);
  527. }
  528. const_local_iterator cbegin(size_type n)const{return begin(n);}
  529. const_local_iterator cend(size_type n)const{return end(n);}
  530. local_iterator local_iterator_to(const value_type& x)
  531. {
  532. return make_local_iterator(
  533. node_from_value<index_node_type>(boost::addressof(x)));
  534. }
  535. const_local_iterator local_iterator_to(const value_type& x)const
  536. {
  537. return make_local_iterator(
  538. node_from_value<index_node_type>(boost::addressof(x)));
  539. }
  540. /* hash policy */
  541. float load_factor()const BOOST_NOEXCEPT
  542. {return static_cast<float>(size())/bucket_count();}
  543. float max_load_factor()const BOOST_NOEXCEPT{return mlf;}
  544. void max_load_factor(float z){mlf=z;calculate_max_load();}
  545. void rehash(size_type n)
  546. {
  547. BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
  548. if(size()<=max_load&&n<=bucket_count())return;
  549. size_type bc =(std::numeric_limits<size_type>::max)();
  550. float fbc=1.0f+static_cast<float>(size())/mlf;
  551. if(bc>fbc){
  552. bc=static_cast<size_type>(fbc);
  553. if(bc<n)bc=n;
  554. }
  555. unchecked_rehash(bc);
  556. }
  557. void reserve(size_type n)
  558. {
  559. rehash(static_cast<size_type>(std::ceil(static_cast<float>(n)/mlf)));
  560. }
  561. BOOST_MULTI_INDEX_PROTECTED_IF_MEMBER_TEMPLATE_FRIENDS:
  562. hashed_index(const ctor_args_list& args_list,const allocator_type& al):
  563. super(args_list.get_tail(),al),
  564. key(tuples::get<1>(args_list.get_head())),
  565. hash_(tuples::get<2>(args_list.get_head())),
  566. eq_(tuples::get<3>(args_list.get_head())),
  567. buckets(al,header()->impl(),tuples::get<0>(args_list.get_head())),
  568. mlf(1.0f)
  569. {
  570. calculate_max_load();
  571. }
  572. hashed_index(
  573. const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x):
  574. super(x),
  575. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  576. safe_super(),
  577. #endif
  578. key(x.key),
  579. hash_(x.hash_),
  580. eq_(x.eq_),
  581. buckets(x.get_allocator(),header()->impl(),x.buckets.size()),
  582. mlf(x.mlf),
  583. max_load(x.max_load)
  584. {
  585. /* Copy ctor just takes the internal configuration objects from x. The rest
  586. * is done in subsequent call to copy_().
  587. */
  588. }
  589. hashed_index(
  590. const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x,
  591. do_not_copy_elements_tag):
  592. super(x,do_not_copy_elements_tag()),
  593. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  594. safe_super(),
  595. #endif
  596. key(x.key),
  597. hash_(x.hash_),
  598. eq_(x.eq_),
  599. buckets(x.get_allocator(),header()->impl(),0),
  600. mlf(1.0f)
  601. {
  602. calculate_max_load();
  603. }
  604. ~hashed_index()
  605. {
  606. /* the container is guaranteed to be empty by now */
  607. }
  608. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  609. iterator make_iterator(index_node_type* node)
  610. {
  611. return iterator(node,this);
  612. }
  613. const_iterator make_iterator(index_node_type* node)const
  614. {
  615. return const_iterator(node,const_cast<hashed_index*>(this));
  616. }
  617. #else
  618. iterator make_iterator(index_node_type* node)
  619. {
  620. return iterator(node);
  621. }
  622. const_iterator make_iterator(index_node_type* node)const
  623. {
  624. return const_iterator(node);
  625. }
  626. #endif
  627. local_iterator make_local_iterator(index_node_type* node)
  628. {
  629. return local_iterator(node);
  630. }
  631. const_local_iterator make_local_iterator(index_node_type* node)const
  632. {
  633. return const_local_iterator(node);
  634. }
  635. void copy_(
  636. const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x,
  637. const copy_map_type& map)
  638. {
  639. copy_(x,map,Category());
  640. }
  641. void copy_(
  642. const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x,
  643. const copy_map_type& map,hashed_unique_tag)
  644. {
  645. if(x.size()!=0){
  646. node_impl_pointer end_org=x.header()->impl(),
  647. org=end_org,
  648. cpy=header()->impl();
  649. do{
  650. node_impl_pointer prev_org=org->prior(),
  651. prev_cpy=
  652. static_cast<index_node_type*>(map.find(static_cast<final_node_type*>(
  653. index_node_type::from_impl(prev_org))))->impl();
  654. cpy->prior()=prev_cpy;
  655. if(node_alg::is_first_of_bucket(org)){
  656. node_impl_base_pointer buc_org=prev_org->next(),
  657. buc_cpy=
  658. buckets.begin()+(buc_org-x.buckets.begin());
  659. prev_cpy->next()=buc_cpy;
  660. buc_cpy->prior()=cpy;
  661. }
  662. else{
  663. prev_cpy->next()=node_impl_type::base_pointer_from(cpy);
  664. }
  665. org=prev_org;
  666. cpy=prev_cpy;
  667. }while(org!=end_org);
  668. }
  669. super::copy_(x,map);
  670. }
  671. void copy_(
  672. const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x,
  673. const copy_map_type& map,hashed_non_unique_tag)
  674. {
  675. if(x.size()!=0){
  676. node_impl_pointer end_org=x.header()->impl(),
  677. org=end_org,
  678. cpy=header()->impl();
  679. do{
  680. node_impl_pointer next_org=node_alg::after(org),
  681. next_cpy=
  682. static_cast<index_node_type*>(map.find(static_cast<final_node_type*>(
  683. index_node_type::from_impl(next_org))))->impl();
  684. if(node_alg::is_first_of_bucket(next_org)){
  685. node_impl_base_pointer buc_org=org->next(),
  686. buc_cpy=
  687. buckets.begin()+(buc_org-x.buckets.begin());
  688. cpy->next()=buc_cpy;
  689. buc_cpy->prior()=next_cpy;
  690. next_cpy->prior()=cpy;
  691. }
  692. else{
  693. if(org->next()==node_impl_type::base_pointer_from(next_org)){
  694. cpy->next()=node_impl_type::base_pointer_from(next_cpy);
  695. }
  696. else{
  697. cpy->next()=
  698. node_impl_type::base_pointer_from(
  699. static_cast<index_node_type*>(
  700. map.find(static_cast<final_node_type*>(
  701. index_node_type::from_impl(
  702. node_impl_type::pointer_from(org->next())))))->impl());
  703. }
  704. if(next_org->prior()!=org){
  705. next_cpy->prior()=
  706. static_cast<index_node_type*>(
  707. map.find(static_cast<final_node_type*>(
  708. index_node_type::from_impl(next_org->prior()))))->impl();
  709. }
  710. else{
  711. next_cpy->prior()=cpy;
  712. }
  713. }
  714. org=next_org;
  715. cpy=next_cpy;
  716. }while(org!=end_org);
  717. }
  718. super::copy_(x,map);
  719. }
  720. template<typename Variant>
  721. final_node_type* insert_(
  722. value_param_type v,final_node_type*& x,Variant variant)
  723. {
  724. reserve_for_insert(size()+1);
  725. std::size_t buc=find_bucket(v);
  726. link_info pos(buckets.at(buc));
  727. if(!link_point(v,pos)){
  728. return static_cast<final_node_type*>(
  729. index_node_type::from_impl(node_impl_type::pointer_from(pos)));
  730. }
  731. final_node_type* res=super::insert_(v,x,variant);
  732. if(res==x)link(static_cast<index_node_type*>(x),pos);
  733. return res;
  734. }
  735. template<typename Variant>
  736. final_node_type* insert_(
  737. value_param_type v,index_node_type* position,
  738. final_node_type*& x,Variant variant)
  739. {
  740. reserve_for_insert(size()+1);
  741. std::size_t buc=find_bucket(v);
  742. link_info pos(buckets.at(buc));
  743. if(!link_point(v,pos)){
  744. return static_cast<final_node_type*>(
  745. index_node_type::from_impl(node_impl_type::pointer_from(pos)));
  746. }
  747. final_node_type* res=super::insert_(v,position,x,variant);
  748. if(res==x)link(static_cast<index_node_type*>(x),pos);
  749. return res;
  750. }
  751. void extract_(index_node_type* x)
  752. {
  753. unlink(x);
  754. super::extract_(x);
  755. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  756. detach_iterators(x);
  757. #endif
  758. }
  759. void delete_all_nodes_()
  760. {
  761. delete_all_nodes_(Category());
  762. }
  763. void delete_all_nodes_(hashed_unique_tag)
  764. {
  765. for(node_impl_pointer x_end=header()->impl(),x=x_end->prior();x!=x_end;){
  766. node_impl_pointer y=x->prior();
  767. this->final_delete_node_(
  768. static_cast<final_node_type*>(index_node_type::from_impl(x)));
  769. x=y;
  770. }
  771. }
  772. void delete_all_nodes_(hashed_non_unique_tag)
  773. {
  774. for(node_impl_pointer x_end=header()->impl(),x=x_end->prior();x!=x_end;){
  775. node_impl_pointer y=x->prior();
  776. if(y->next()!=node_impl_type::base_pointer_from(x)&&
  777. y->next()->prior()!=x){ /* n-1 of group */
  778. /* Make the second node prior() pointer back-linked so that it won't
  779. * refer to a deleted node when the time for its own destruction comes.
  780. */
  781. node_impl_pointer first=node_impl_type::pointer_from(y->next());
  782. first->next()->prior()=first;
  783. }
  784. this->final_delete_node_(
  785. static_cast<final_node_type*>(index_node_type::from_impl(x)));
  786. x=y;
  787. }
  788. }
  789. void clear_()
  790. {
  791. super::clear_();
  792. buckets.clear(header()->impl());
  793. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  794. safe_super::detach_dereferenceable_iterators();
  795. #endif
  796. }
  797. template<typename BoolConstant>
  798. void swap_(
  799. hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x,
  800. BoolConstant swap_allocators)
  801. {
  802. adl_swap(key,x.key);
  803. adl_swap(hash_,x.hash_);
  804. adl_swap(eq_,x.eq_);
  805. buckets.swap(x.buckets,swap_allocators);
  806. std::swap(mlf,x.mlf);
  807. std::swap(max_load,x.max_load);
  808. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  809. safe_super::swap(x);
  810. #endif
  811. super::swap_(x,swap_allocators);
  812. }
  813. void swap_elements_(
  814. hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x)
  815. {
  816. buckets.swap(x.buckets);
  817. std::swap(mlf,x.mlf);
  818. std::swap(max_load,x.max_load);
  819. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  820. safe_super::swap(x);
  821. #endif
  822. super::swap_elements_(x);
  823. }
  824. template<typename Variant>
  825. bool replace_(value_param_type v,index_node_type* x,Variant variant)
  826. {
  827. if(eq_(key(v),key(x->value()))){
  828. return super::replace_(v,x,variant);
  829. }
  830. unlink_undo undo;
  831. unlink(x,undo);
  832. BOOST_TRY{
  833. std::size_t buc=find_bucket(v);
  834. link_info pos(buckets.at(buc));
  835. if(link_point(v,pos)&&super::replace_(v,x,variant)){
  836. link(x,pos);
  837. return true;
  838. }
  839. undo();
  840. return false;
  841. }
  842. BOOST_CATCH(...){
  843. undo();
  844. BOOST_RETHROW;
  845. }
  846. BOOST_CATCH_END
  847. }
  848. bool modify_(index_node_type* x)
  849. {
  850. std::size_t buc;
  851. bool b;
  852. BOOST_TRY{
  853. buc=find_bucket(x->value());
  854. b=in_place(x->impl(),key(x->value()),buc);
  855. }
  856. BOOST_CATCH(...){
  857. extract_(x);
  858. BOOST_RETHROW;
  859. }
  860. BOOST_CATCH_END
  861. if(!b){
  862. unlink(x);
  863. BOOST_TRY{
  864. link_info pos(buckets.at(buc));
  865. if(!link_point(x->value(),pos)){
  866. super::extract_(x);
  867. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  868. detach_iterators(x);
  869. #endif
  870. return false;
  871. }
  872. link(x,pos);
  873. }
  874. BOOST_CATCH(...){
  875. super::extract_(x);
  876. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  877. detach_iterators(x);
  878. #endif
  879. BOOST_RETHROW;
  880. }
  881. BOOST_CATCH_END
  882. }
  883. BOOST_TRY{
  884. if(!super::modify_(x)){
  885. unlink(x);
  886. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  887. detach_iterators(x);
  888. #endif
  889. return false;
  890. }
  891. else return true;
  892. }
  893. BOOST_CATCH(...){
  894. unlink(x);
  895. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  896. detach_iterators(x);
  897. #endif
  898. BOOST_RETHROW;
  899. }
  900. BOOST_CATCH_END
  901. }
  902. bool modify_rollback_(index_node_type* x)
  903. {
  904. std::size_t buc=find_bucket(x->value());
  905. if(in_place(x->impl(),key(x->value()),buc)){
  906. return super::modify_rollback_(x);
  907. }
  908. unlink_undo undo;
  909. unlink(x,undo);
  910. BOOST_TRY{
  911. link_info pos(buckets.at(buc));
  912. if(link_point(x->value(),pos)&&super::modify_rollback_(x)){
  913. link(x,pos);
  914. return true;
  915. }
  916. undo();
  917. return false;
  918. }
  919. BOOST_CATCH(...){
  920. undo();
  921. BOOST_RETHROW;
  922. }
  923. BOOST_CATCH_END
  924. }
  925. bool check_rollback_(index_node_type* x)const
  926. {
  927. std::size_t buc=find_bucket(x->value());
  928. return in_place(x->impl(),key(x->value()),buc)&&super::check_rollback_(x);
  929. }
  930. /* comparison */
  931. #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  932. /* defect macro refers to class, not function, templates, but anyway */
  933. template<typename K,typename H,typename P,typename S,typename T,typename C>
  934. friend bool operator==(
  935. const hashed_index<K,H,P,S,T,C>&,const hashed_index<K,H,P,S,T,C>& y);
  936. #endif
  937. bool equals(const hashed_index& x)const{return equals(x,Category());}
  938. bool equals(const hashed_index& x,hashed_unique_tag)const
  939. {
  940. if(size()!=x.size())return false;
  941. for(const_iterator it=begin(),it_end=end(),it2_end=x.end();
  942. it!=it_end;++it){
  943. const_iterator it2=x.find(key(*it));
  944. if(it2==it2_end||!(*it==*it2))return false;
  945. }
  946. return true;
  947. }
  948. bool equals(const hashed_index& x,hashed_non_unique_tag)const
  949. {
  950. if(size()!=x.size())return false;
  951. for(const_iterator it=begin(),it_end=end();it!=it_end;){
  952. const_iterator it2,it2_last;
  953. boost::tie(it2,it2_last)=x.equal_range(key(*it));
  954. if(it2==it2_last)return false;
  955. const_iterator it_last=make_iterator(
  956. index_node_type::from_impl(end_of_range(it.get_node()->impl())));
  957. if(std::distance(it,it_last)!=std::distance(it2,it2_last))return false;
  958. /* From is_permutation code in
  959. * http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3068.pdf
  960. */
  961. for(;it!=it_last;++it,++it2){
  962. if(!(*it==*it2))break;
  963. }
  964. if(it!=it_last){
  965. for(const_iterator scan=it;scan!=it_last;++scan){
  966. if(std::find(it,scan,*scan)!=scan)continue;
  967. difference_type matches=std::count(it2,it2_last,*scan);
  968. if(matches==0||matches!=std::count(scan,it_last,*scan))return false;
  969. }
  970. it=it_last;
  971. }
  972. }
  973. return true;
  974. }
  975. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  976. /* serialization */
  977. template<typename Archive>
  978. void save_(
  979. Archive& ar,const unsigned int version,const index_saver_type& sm)const
  980. {
  981. ar<<serialization::make_nvp("position",buckets);
  982. super::save_(ar,version,sm);
  983. }
  984. template<typename Archive>
  985. void load_(Archive& ar,const unsigned int version,const index_loader_type& lm)
  986. {
  987. ar>>serialization::make_nvp("position",buckets);
  988. super::load_(ar,version,lm);
  989. }
  990. #endif
  991. #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)
  992. /* invariant stuff */
  993. bool invariant_()const
  994. {
  995. if(size()==0||begin()==end()){
  996. if(size()!=0||begin()!=end())return false;
  997. }
  998. else{
  999. size_type s0=0;
  1000. for(const_iterator it=begin(),it_end=end();it!=it_end;++it,++s0){}
  1001. if(s0!=size())return false;
  1002. size_type s1=0;
  1003. for(size_type buc=0;buc<bucket_count();++buc){
  1004. size_type ss1=0;
  1005. for(const_local_iterator it=begin(buc),it_end=end(buc);
  1006. it!=it_end;++it,++ss1){
  1007. if(find_bucket(*it)!=buc)return false;
  1008. }
  1009. if(ss1!=bucket_size(buc))return false;
  1010. s1+=ss1;
  1011. }
  1012. if(s1!=size())return false;
  1013. }
  1014. return super::invariant_();
  1015. }
  1016. /* This forwarding function eases things for the boost::mem_fn construct
  1017. * in BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT. Actually,
  1018. * final_check_invariant is already an inherited member function of index.
  1019. */
  1020. void check_invariant_()const{this->final_check_invariant_();}
  1021. #endif
  1022. private:
  1023. index_node_type* header()const{return this->final_header();}
  1024. std::size_t find_bucket(value_param_type v)const
  1025. {
  1026. return bucket(key(v));
  1027. }
  1028. struct link_info_non_unique
  1029. {
  1030. link_info_non_unique(node_impl_base_pointer pos):
  1031. first(pos),last(node_impl_base_pointer(0)){}
  1032. operator const node_impl_base_pointer&()const{return this->first;}
  1033. node_impl_base_pointer first,last;
  1034. };
  1035. typedef typename mpl::if_<
  1036. is_same<Category,hashed_unique_tag>,
  1037. node_impl_base_pointer,
  1038. link_info_non_unique
  1039. >::type link_info;
  1040. bool link_point(value_param_type v,link_info& pos)
  1041. {
  1042. return link_point(v,pos,Category());
  1043. }
  1044. bool link_point(
  1045. value_param_type v,node_impl_base_pointer& pos,hashed_unique_tag)
  1046. {
  1047. for(node_impl_pointer x=pos->prior();x!=node_impl_pointer(0);
  1048. x=node_alg::after_local(x)){
  1049. if(eq_(key(v),key(index_node_type::from_impl(x)->value()))){
  1050. pos=node_impl_type::base_pointer_from(x);
  1051. return false;
  1052. }
  1053. }
  1054. return true;
  1055. }
  1056. bool link_point(
  1057. value_param_type v,link_info_non_unique& pos,hashed_non_unique_tag)
  1058. {
  1059. for(node_impl_pointer x=pos.first->prior();x!=node_impl_pointer(0);
  1060. x=node_alg::next_to_inspect(x)){
  1061. if(eq_(key(v),key(index_node_type::from_impl(x)->value()))){
  1062. pos.first=node_impl_type::base_pointer_from(x);
  1063. pos.last=node_impl_type::base_pointer_from(last_of_range(x));
  1064. return true;
  1065. }
  1066. }
  1067. return true;
  1068. }
  1069. node_impl_pointer last_of_range(node_impl_pointer x)const
  1070. {
  1071. return last_of_range(x,Category());
  1072. }
  1073. node_impl_pointer last_of_range(node_impl_pointer x,hashed_unique_tag)const
  1074. {
  1075. return x;
  1076. }
  1077. node_impl_pointer last_of_range(
  1078. node_impl_pointer x,hashed_non_unique_tag)const
  1079. {
  1080. node_impl_base_pointer y=x->next();
  1081. node_impl_pointer z=y->prior();
  1082. if(z==x){ /* range of size 1 or 2 */
  1083. node_impl_pointer yy=node_impl_type::pointer_from(y);
  1084. return
  1085. eq_(
  1086. key(index_node_type::from_impl(x)->value()),
  1087. key(index_node_type::from_impl(yy)->value()))?yy:x;
  1088. }
  1089. else if(z->prior()==x) /* last of bucket */
  1090. return x;
  1091. else /* group of size>2 */
  1092. return z;
  1093. }
  1094. node_impl_pointer end_of_range(node_impl_pointer x)const
  1095. {
  1096. return end_of_range(x,Category());
  1097. }
  1098. node_impl_pointer end_of_range(node_impl_pointer x,hashed_unique_tag)const
  1099. {
  1100. return node_alg::after(last_of_range(x));
  1101. }
  1102. node_impl_pointer end_of_range(
  1103. node_impl_pointer x,hashed_non_unique_tag)const
  1104. {
  1105. node_impl_base_pointer y=x->next();
  1106. node_impl_pointer z=y->prior();
  1107. if(z==x){ /* range of size 1 or 2 */
  1108. node_impl_pointer yy=node_impl_type::pointer_from(y);
  1109. if(!eq_(
  1110. key(index_node_type::from_impl(x)->value()),
  1111. key(index_node_type::from_impl(yy)->value())))yy=x;
  1112. return yy->next()->prior()==yy?
  1113. node_impl_type::pointer_from(yy->next()):
  1114. yy->next()->prior();
  1115. }
  1116. else if(z->prior()==x) /* last of bucket */
  1117. return z;
  1118. else /* group of size>2 */
  1119. return z->next()->prior()==z?
  1120. node_impl_type::pointer_from(z->next()):
  1121. z->next()->prior();
  1122. }
  1123. void link(index_node_type* x,const link_info& pos)
  1124. {
  1125. link(x,pos,Category());
  1126. }
  1127. void link(index_node_type* x,node_impl_base_pointer pos,hashed_unique_tag)
  1128. {
  1129. node_alg::link(x->impl(),pos,header()->impl());
  1130. }
  1131. void link(
  1132. index_node_type* x,const link_info_non_unique& pos,hashed_non_unique_tag)
  1133. {
  1134. if(pos.last==node_impl_base_pointer(0)){
  1135. node_alg::link(x->impl(),pos.first,header()->impl());
  1136. }
  1137. else{
  1138. node_alg::link(
  1139. x->impl(),
  1140. node_impl_type::pointer_from(pos.first),
  1141. node_impl_type::pointer_from(pos.last));
  1142. }
  1143. }
  1144. void unlink(index_node_type* x)
  1145. {
  1146. node_alg::unlink(x->impl());
  1147. }
  1148. typedef typename node_alg::unlink_undo unlink_undo;
  1149. void unlink(index_node_type* x,unlink_undo& undo)
  1150. {
  1151. node_alg::unlink(x->impl(),undo);
  1152. }
  1153. void calculate_max_load()
  1154. {
  1155. float fml=mlf*static_cast<float>(bucket_count());
  1156. max_load=(std::numeric_limits<size_type>::max)();
  1157. if(max_load>fml)max_load=static_cast<size_type>(fml);
  1158. }
  1159. void reserve_for_insert(size_type n)
  1160. {
  1161. if(n>max_load){
  1162. size_type bc =(std::numeric_limits<size_type>::max)();
  1163. float fbc=1.0f+static_cast<float>(n)/mlf;
  1164. if(bc>fbc)bc =static_cast<size_type>(fbc);
  1165. unchecked_rehash(bc);
  1166. }
  1167. }
  1168. void unchecked_rehash(size_type n){unchecked_rehash(n,Category());}
  1169. void unchecked_rehash(size_type n,hashed_unique_tag)
  1170. {
  1171. node_impl_type cpy_end_node;
  1172. node_impl_pointer cpy_end=node_impl_pointer(&cpy_end_node),
  1173. end_=header()->impl();
  1174. bucket_array_type buckets_cpy(get_allocator(),cpy_end,n);
  1175. if(size()!=0){
  1176. auto_space<
  1177. std::size_t,allocator_type> hashes(get_allocator(),size());
  1178. auto_space<
  1179. node_impl_pointer,allocator_type> node_ptrs(get_allocator(),size());
  1180. std::size_t i=0,size_=size();
  1181. bool within_bucket=false;
  1182. BOOST_TRY{
  1183. for(;i!=size_;++i){
  1184. node_impl_pointer x=end_->prior();
  1185. /* only this can possibly throw */
  1186. std::size_t h=hash_(key(index_node_type::from_impl(x)->value()));
  1187. hashes.data()[i]=h;
  1188. node_ptrs.data()[i]=x;
  1189. within_bucket=!node_alg::unlink_last(end_);
  1190. node_alg::link(x,buckets_cpy.at(buckets_cpy.position(h)),cpy_end);
  1191. }
  1192. }
  1193. BOOST_CATCH(...){
  1194. if(i!=0){
  1195. std::size_t prev_buc=buckets.position(hashes.data()[i-1]);
  1196. if(!within_bucket)prev_buc=~prev_buc;
  1197. for(std::size_t j=i;j--;){
  1198. std::size_t buc=buckets.position(hashes.data()[j]);
  1199. node_impl_pointer x=node_ptrs.data()[j];
  1200. if(buc==prev_buc)node_alg::append(x,end_);
  1201. else node_alg::link(x,buckets.at(buc),end_);
  1202. prev_buc=buc;
  1203. }
  1204. }
  1205. BOOST_RETHROW;
  1206. }
  1207. BOOST_CATCH_END
  1208. }
  1209. end_->prior()=cpy_end->prior()!=cpy_end?cpy_end->prior():end_;
  1210. end_->next()=cpy_end->next();
  1211. end_->prior()->next()->prior()=end_->next()->prior()->prior()=end_;
  1212. buckets.swap(buckets_cpy);
  1213. calculate_max_load();
  1214. }
  1215. void unchecked_rehash(size_type n,hashed_non_unique_tag)
  1216. {
  1217. node_impl_type cpy_end_node;
  1218. node_impl_pointer cpy_end=node_impl_pointer(&cpy_end_node),
  1219. end_=header()->impl();
  1220. bucket_array_type buckets_cpy(get_allocator(),cpy_end,n);
  1221. if(size()!=0){
  1222. auto_space<
  1223. std::size_t,allocator_type> hashes(get_allocator(),size());
  1224. auto_space<
  1225. node_impl_pointer,allocator_type> node_ptrs(get_allocator(),size());
  1226. std::size_t i=0;
  1227. bool within_bucket=false;
  1228. BOOST_TRY{
  1229. for(;;++i){
  1230. node_impl_pointer x=end_->prior();
  1231. if(x==end_)break;
  1232. /* only this can possibly throw */
  1233. std::size_t h=hash_(key(index_node_type::from_impl(x)->value()));
  1234. hashes.data()[i]=h;
  1235. node_ptrs.data()[i]=x;
  1236. std::pair<node_impl_pointer,bool> p=
  1237. node_alg::unlink_last_group(end_);
  1238. node_alg::link_range(
  1239. p.first,x,buckets_cpy.at(buckets_cpy.position(h)),cpy_end);
  1240. within_bucket=!(p.second);
  1241. }
  1242. }
  1243. BOOST_CATCH(...){
  1244. if(i!=0){
  1245. std::size_t prev_buc=buckets.position(hashes.data()[i-1]);
  1246. if(!within_bucket)prev_buc=~prev_buc;
  1247. for(std::size_t j=i;j--;){
  1248. std::size_t buc=buckets.position(hashes.data()[j]);
  1249. node_impl_pointer x=node_ptrs.data()[j],
  1250. y=
  1251. x->prior()->next()!=node_impl_type::base_pointer_from(x)&&
  1252. x->prior()->next()->prior()!=x?
  1253. node_impl_type::pointer_from(x->prior()->next()):x;
  1254. node_alg::unlink_range(y,x);
  1255. if(buc==prev_buc)node_alg::append_range(y,x,end_);
  1256. else node_alg::link_range(y,x,buckets.at(buc),end_);
  1257. prev_buc=buc;
  1258. }
  1259. }
  1260. BOOST_RETHROW;
  1261. }
  1262. BOOST_CATCH_END
  1263. }
  1264. end_->prior()=cpy_end->prior()!=cpy_end?cpy_end->prior():end_;
  1265. end_->next()=cpy_end->next();
  1266. end_->prior()->next()->prior()=end_->next()->prior()->prior()=end_;
  1267. buckets.swap(buckets_cpy);
  1268. calculate_max_load();
  1269. }
  1270. bool in_place(node_impl_pointer x,key_param_type k,std::size_t buc)const
  1271. {
  1272. return in_place(x,k,buc,Category());
  1273. }
  1274. bool in_place(
  1275. node_impl_pointer x,key_param_type k,std::size_t buc,
  1276. hashed_unique_tag)const
  1277. {
  1278. bool found=false;
  1279. for(node_impl_pointer y=buckets.at(buc)->prior();
  1280. y!=node_impl_pointer(0);y=node_alg::after_local(y)){
  1281. if(y==x)found=true;
  1282. else if(eq_(k,key(index_node_type::from_impl(y)->value())))return false;
  1283. }
  1284. return found;
  1285. }
  1286. bool in_place(
  1287. node_impl_pointer x,key_param_type k,std::size_t buc,
  1288. hashed_non_unique_tag)const
  1289. {
  1290. bool found=false;
  1291. int range_size=0;
  1292. for(node_impl_pointer y=buckets.at(buc)->prior();y!=node_impl_pointer(0);){
  1293. if(node_alg::is_first_of_group(y)){ /* group of 3 or more */
  1294. if(y==x){
  1295. /* in place <-> equal to some other member of the group */
  1296. return eq_(
  1297. k,
  1298. key(index_node_type::from_impl(
  1299. node_impl_type::pointer_from(y->next()))->value()));
  1300. }
  1301. else{
  1302. node_impl_pointer z=
  1303. node_alg::after_local(y->next()->prior()); /* end of range */
  1304. if(eq_(k,key(index_node_type::from_impl(y)->value()))){
  1305. if(found)return false; /* x lies outside */
  1306. do{
  1307. if(y==x)return true;
  1308. y=node_alg::after_local(y);
  1309. }while(y!=z);
  1310. return false; /* x not found */
  1311. }
  1312. else{
  1313. if(range_size==1&&!found)return false;
  1314. if(range_size==2)return found;
  1315. range_size=0;
  1316. y=z; /* skip range (and potentially x, too, which is fine) */
  1317. }
  1318. }
  1319. }
  1320. else{ /* group of 1 or 2 */
  1321. if(y==x){
  1322. if(range_size==1)return true;
  1323. range_size=1;
  1324. found=true;
  1325. }
  1326. else if(eq_(k,key(index_node_type::from_impl(y)->value()))){
  1327. if(range_size==0&&found)return false;
  1328. if(range_size==1&&!found)return false;
  1329. if(range_size==2)return false;
  1330. ++range_size;
  1331. }
  1332. else{
  1333. if(range_size==1&&!found)return false;
  1334. if(range_size==2)return found;
  1335. range_size=0;
  1336. }
  1337. y=node_alg::after_local(y);
  1338. }
  1339. }
  1340. return found;
  1341. }
  1342. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  1343. void detach_iterators(index_node_type* x)
  1344. {
  1345. iterator it=make_iterator(x);
  1346. safe_mode::detach_equivalent_iterators(it);
  1347. }
  1348. #endif
  1349. template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK>
  1350. std::pair<iterator,bool> emplace_impl(BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)
  1351. {
  1352. BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
  1353. std::pair<final_node_type*,bool>p=
  1354. this->final_emplace_(BOOST_MULTI_INDEX_FORWARD_PARAM_PACK);
  1355. return std::pair<iterator,bool>(make_iterator(p.first),p.second);
  1356. }
  1357. template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK>
  1358. iterator emplace_hint_impl(
  1359. iterator position,BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)
  1360. {
  1361. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  1362. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  1363. BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
  1364. std::pair<final_node_type*,bool>p=
  1365. this->final_emplace_hint_(
  1366. static_cast<final_node_type*>(position.get_node()),
  1367. BOOST_MULTI_INDEX_FORWARD_PARAM_PACK);
  1368. return make_iterator(p.first);
  1369. }
  1370. template<
  1371. typename CompatibleHash,typename CompatiblePred
  1372. >
  1373. iterator find(
  1374. const key_type& k,
  1375. const CompatibleHash& hash,const CompatiblePred& eq,mpl::true_)const
  1376. {
  1377. return find(k,hash,eq,mpl::false_());
  1378. }
  1379. template<
  1380. typename CompatibleKey,typename CompatibleHash,typename CompatiblePred
  1381. >
  1382. iterator find(
  1383. const CompatibleKey& k,
  1384. const CompatibleHash& hash,const CompatiblePred& eq,mpl::false_)const
  1385. {
  1386. std::size_t buc=buckets.position(hash(k));
  1387. for(node_impl_pointer x=buckets.at(buc)->prior();
  1388. x!=node_impl_pointer(0);x=node_alg::next_to_inspect(x)){
  1389. if(eq(k,key(index_node_type::from_impl(x)->value()))){
  1390. return make_iterator(index_node_type::from_impl(x));
  1391. }
  1392. }
  1393. return end();
  1394. }
  1395. template<
  1396. typename CompatibleHash,typename CompatiblePred
  1397. >
  1398. size_type count(
  1399. const key_type& k,
  1400. const CompatibleHash& hash,const CompatiblePred& eq,mpl::true_)const
  1401. {
  1402. return count(k,hash,eq,mpl::false_());
  1403. }
  1404. template<
  1405. typename CompatibleKey,typename CompatibleHash,typename CompatiblePred
  1406. >
  1407. size_type count(
  1408. const CompatibleKey& k,
  1409. const CompatibleHash& hash,const CompatiblePred& eq,mpl::false_)const
  1410. {
  1411. std::size_t buc=buckets.position(hash(k));
  1412. for(node_impl_pointer x=buckets.at(buc)->prior();
  1413. x!=node_impl_pointer(0);x=node_alg::next_to_inspect(x)){
  1414. if(eq(k,key(index_node_type::from_impl(x)->value()))){
  1415. size_type res=0;
  1416. node_impl_pointer y=end_of_range(x);
  1417. do{
  1418. ++res;
  1419. x=node_alg::after(x);
  1420. }while(x!=y);
  1421. return res;
  1422. }
  1423. }
  1424. return 0;
  1425. }
  1426. template<
  1427. typename CompatibleHash,typename CompatiblePred
  1428. >
  1429. std::pair<iterator,iterator> equal_range(
  1430. const key_type& k,
  1431. const CompatibleHash& hash,const CompatiblePred& eq,mpl::true_)const
  1432. {
  1433. return equal_range(k,hash,eq,mpl::false_());
  1434. }
  1435. template<
  1436. typename CompatibleKey,typename CompatibleHash,typename CompatiblePred
  1437. >
  1438. std::pair<iterator,iterator> equal_range(
  1439. const CompatibleKey& k,
  1440. const CompatibleHash& hash,const CompatiblePred& eq,mpl::false_)const
  1441. {
  1442. std::size_t buc=buckets.position(hash(k));
  1443. for(node_impl_pointer x=buckets.at(buc)->prior();
  1444. x!=node_impl_pointer(0);x=node_alg::next_to_inspect(x)){
  1445. if(eq(k,key(index_node_type::from_impl(x)->value()))){
  1446. return std::pair<iterator,iterator>(
  1447. make_iterator(index_node_type::from_impl(x)),
  1448. make_iterator(index_node_type::from_impl(end_of_range(x))));
  1449. }
  1450. }
  1451. return std::pair<iterator,iterator>(end(),end());
  1452. }
  1453. key_from_value key;
  1454. hasher hash_;
  1455. key_equal eq_;
  1456. bucket_array_type buckets;
  1457. float mlf;
  1458. size_type max_load;
  1459. #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)&&\
  1460. BOOST_WORKAROUND(__MWERKS__,<=0x3003)
  1461. #pragma parse_mfunc_templ reset
  1462. #endif
  1463. };
  1464. /* comparison */
  1465. template<
  1466. typename KeyFromValue,typename Hash,typename Pred,
  1467. typename SuperMeta,typename TagList,typename Category
  1468. >
  1469. bool operator==(
  1470. const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x,
  1471. const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& y)
  1472. {
  1473. return x.equals(y);
  1474. }
  1475. template<
  1476. typename KeyFromValue,typename Hash,typename Pred,
  1477. typename SuperMeta,typename TagList,typename Category
  1478. >
  1479. bool operator!=(
  1480. const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x,
  1481. const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& y)
  1482. {
  1483. return !(x==y);
  1484. }
  1485. /* specialized algorithms */
  1486. template<
  1487. typename KeyFromValue,typename Hash,typename Pred,
  1488. typename SuperMeta,typename TagList,typename Category
  1489. >
  1490. void swap(
  1491. hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x,
  1492. hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& y)
  1493. {
  1494. x.swap(y);
  1495. }
  1496. } /* namespace multi_index::detail */
  1497. /* hashed index specifiers */
  1498. template<typename Arg1,typename Arg2,typename Arg3,typename Arg4>
  1499. struct hashed_unique
  1500. {
  1501. typedef typename detail::hashed_index_args<
  1502. Arg1,Arg2,Arg3,Arg4> index_args;
  1503. typedef typename index_args::tag_list_type::type tag_list_type;
  1504. typedef typename index_args::key_from_value_type key_from_value_type;
  1505. typedef typename index_args::hash_type hash_type;
  1506. typedef typename index_args::pred_type pred_type;
  1507. template<typename Super>
  1508. struct node_class
  1509. {
  1510. typedef detail::hashed_index_node<Super> type;
  1511. };
  1512. template<typename SuperMeta>
  1513. struct index_class
  1514. {
  1515. typedef detail::hashed_index<
  1516. key_from_value_type,hash_type,pred_type,
  1517. SuperMeta,tag_list_type,detail::hashed_unique_tag> type;
  1518. };
  1519. };
  1520. template<typename Arg1,typename Arg2,typename Arg3,typename Arg4>
  1521. struct hashed_non_unique
  1522. {
  1523. typedef typename detail::hashed_index_args<
  1524. Arg1,Arg2,Arg3,Arg4> index_args;
  1525. typedef typename index_args::tag_list_type::type tag_list_type;
  1526. typedef typename index_args::key_from_value_type key_from_value_type;
  1527. typedef typename index_args::hash_type hash_type;
  1528. typedef typename index_args::pred_type pred_type;
  1529. template<typename Super>
  1530. struct node_class
  1531. {
  1532. typedef detail::hashed_index_node<Super> type;
  1533. };
  1534. template<typename SuperMeta>
  1535. struct index_class
  1536. {
  1537. typedef detail::hashed_index<
  1538. key_from_value_type,hash_type,pred_type,
  1539. SuperMeta,tag_list_type,detail::hashed_non_unique_tag> type;
  1540. };
  1541. };
  1542. } /* namespace multi_index */
  1543. } /* namespace boost */
  1544. /* Boost.Foreach compatibility */
  1545. template<
  1546. typename KeyFromValue,typename Hash,typename Pred,
  1547. typename SuperMeta,typename TagList,typename Category
  1548. >
  1549. inline boost::mpl::true_* boost_foreach_is_noncopyable(
  1550. boost::multi_index::detail::hashed_index<
  1551. KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>*&,
  1552. boost_foreach_argument_dependent_lookup_hack)
  1553. {
  1554. return 0;
  1555. }
  1556. #undef BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT
  1557. #undef BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT_OF
  1558. #endif