random_access_index.hpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  1. /* Copyright 2003-2023 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_RANDOM_ACCESS_INDEX_HPP
  9. #define BOOST_MULTI_INDEX_RANDOM_ACCESS_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/bind/bind.hpp>
  16. #include <boost/call_traits.hpp>
  17. #include <boost/core/addressof.hpp>
  18. #include <boost/core/no_exceptions_support.hpp>
  19. #include <boost/detail/workaround.hpp>
  20. #include <boost/iterator/reverse_iterator.hpp>
  21. #include <boost/move/core.hpp>
  22. #include <boost/move/utility_core.hpp>
  23. #include <boost/mpl/bool.hpp>
  24. #include <boost/mpl/not.hpp>
  25. #include <boost/mpl/push_front.hpp>
  26. #include <boost/multi_index/detail/access_specifier.hpp>
  27. #include <boost/multi_index/detail/allocator_traits.hpp>
  28. #include <boost/multi_index/detail/do_not_copy_elements_tag.hpp>
  29. #include <boost/multi_index/detail/index_node_base.hpp>
  30. #include <boost/multi_index/detail/node_handle.hpp>
  31. #include <boost/multi_index/detail/rnd_node_iterator.hpp>
  32. #include <boost/multi_index/detail/rnd_index_node.hpp>
  33. #include <boost/multi_index/detail/rnd_index_ops.hpp>
  34. #include <boost/multi_index/detail/rnd_index_ptr_array.hpp>
  35. #include <boost/multi_index/detail/safe_mode.hpp>
  36. #include <boost/multi_index/detail/scope_guard.hpp>
  37. #include <boost/multi_index/detail/vartempl_support.hpp>
  38. #include <boost/multi_index/random_access_index_fwd.hpp>
  39. #include <boost/throw_exception.hpp>
  40. #include <boost/tuple/tuple.hpp>
  41. #include <boost/type_traits/is_copy_constructible.hpp>
  42. #include <boost/type_traits/is_integral.hpp>
  43. #include <functional>
  44. #include <stdexcept>
  45. #include <utility>
  46. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  47. #include<initializer_list>
  48. #endif
  49. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  50. #include <boost/multi_index/detail/rnd_index_loader.hpp>
  51. #endif
  52. #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)
  53. #define BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT_OF(x) \
  54. detail::scope_guard BOOST_JOIN(check_invariant_,__LINE__)= \
  55. detail::make_obj_guard(x,&random_access_index::check_invariant_); \
  56. BOOST_JOIN(check_invariant_,__LINE__).touch();
  57. #define BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT \
  58. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT_OF(*this)
  59. #else
  60. #define BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT_OF(x)
  61. #define BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT
  62. #endif
  63. namespace boost{
  64. namespace multi_index{
  65. namespace detail{
  66. /* random_access_index adds a layer of random access indexing
  67. * to a given Super
  68. */
  69. #if defined(BOOST_MSVC)
  70. #pragma warning(push)
  71. #pragma warning(disable:4355) /* this used in base member initializer list */
  72. #endif
  73. template<typename SuperMeta,typename TagList>
  74. class random_access_index:
  75. BOOST_MULTI_INDEX_PROTECTED_IF_MEMBER_TEMPLATE_FRIENDS SuperMeta::type
  76. {
  77. #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)&&\
  78. BOOST_WORKAROUND(__MWERKS__,<=0x3003)
  79. /* The "ISO C++ Template Parser" option in CW8.3 has a problem with the
  80. * lifetime of const references bound to temporaries --precisely what
  81. * scopeguards are.
  82. */
  83. #pragma parse_mfunc_templ off
  84. #endif
  85. #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  86. /* cross-index access */
  87. template <typename,typename,typename> friend class index_base;
  88. #endif
  89. typedef typename SuperMeta::type super;
  90. protected:
  91. typedef random_access_index_node<
  92. typename super::index_node_type> index_node_type;
  93. private:
  94. typedef typename index_node_type::impl_type node_impl_type;
  95. typedef random_access_index_ptr_array<
  96. typename super::final_allocator_type> ptr_array;
  97. typedef typename ptr_array::pointer node_impl_ptr_pointer;
  98. public:
  99. /* types */
  100. typedef typename index_node_type::value_type value_type;
  101. typedef tuples::null_type ctor_args;
  102. typedef typename super::final_allocator_type allocator_type;
  103. typedef value_type& reference;
  104. typedef const value_type& const_reference;
  105. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  106. typedef safe_mode::safe_iterator<
  107. rnd_node_iterator<index_node_type> > iterator;
  108. #else
  109. typedef rnd_node_iterator<index_node_type> iterator;
  110. #endif
  111. typedef iterator const_iterator;
  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 typename alloc_traits::size_type size_type;
  118. typedef typename alloc_traits::difference_type difference_type;
  119. typedef typename
  120. boost::reverse_iterator<iterator> reverse_iterator;
  121. typedef typename
  122. boost::reverse_iterator<const_iterator> const_reverse_iterator;
  123. typedef typename super::final_node_handle_type node_type;
  124. typedef detail::insert_return_type<
  125. iterator,node_type> insert_return_type;
  126. typedef TagList tag_list;
  127. protected:
  128. typedef typename super::final_node_type final_node_type;
  129. typedef tuples::cons<
  130. ctor_args,
  131. typename super::ctor_args_list> ctor_args_list;
  132. typedef typename mpl::push_front<
  133. typename super::index_type_list,
  134. random_access_index>::type index_type_list;
  135. typedef typename mpl::push_front<
  136. typename super::iterator_type_list,
  137. iterator>::type iterator_type_list;
  138. typedef typename mpl::push_front<
  139. typename super::const_iterator_type_list,
  140. const_iterator>::type const_iterator_type_list;
  141. typedef typename super::copy_map_type copy_map_type;
  142. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  143. typedef typename super::index_saver_type index_saver_type;
  144. typedef typename super::index_loader_type index_loader_type;
  145. #endif
  146. private:
  147. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  148. typedef safe_mode::safe_container<iterator> safe_container;
  149. #endif
  150. typedef typename call_traits<
  151. value_type>::param_type value_param_type;
  152. /* needed to avoid commas in some macros */
  153. typedef std::pair<iterator,bool> pair_return_type;
  154. public:
  155. /* construct/copy/destroy
  156. * Default and copy ctors are in the protected section as indices are
  157. * not supposed to be created on their own. No range ctor either.
  158. */
  159. random_access_index<SuperMeta,TagList>& operator=(
  160. const random_access_index<SuperMeta,TagList>& x)
  161. {
  162. this->final()=x.final();
  163. return *this;
  164. }
  165. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  166. random_access_index<SuperMeta,TagList>& operator=(
  167. std::initializer_list<value_type> list)
  168. {
  169. this->final()=list;
  170. return *this;
  171. }
  172. #endif
  173. template <class InputIterator>
  174. void assign(InputIterator first,InputIterator last)
  175. {
  176. assign_iter(first,last,mpl::not_<is_integral<InputIterator> >());
  177. }
  178. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  179. void assign(std::initializer_list<value_type> list)
  180. {
  181. assign(list.begin(),list.end());
  182. }
  183. #endif
  184. void assign(size_type n,value_param_type value)
  185. {
  186. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  187. clear();
  188. for(size_type i=0;i<n;++i)push_back(value);
  189. }
  190. allocator_type get_allocator()const BOOST_NOEXCEPT
  191. {
  192. return this->final().get_allocator();
  193. }
  194. /* iterators */
  195. iterator begin()BOOST_NOEXCEPT
  196. {return make_iterator(index_node_type::from_impl(*ptrs.begin()));}
  197. const_iterator begin()const BOOST_NOEXCEPT
  198. {return make_iterator(index_node_type::from_impl(*ptrs.begin()));}
  199. iterator
  200. end()BOOST_NOEXCEPT{return make_iterator(header());}
  201. const_iterator
  202. end()const BOOST_NOEXCEPT{return make_iterator(header());}
  203. reverse_iterator
  204. rbegin()BOOST_NOEXCEPT{return boost::make_reverse_iterator(end());}
  205. const_reverse_iterator
  206. rbegin()const BOOST_NOEXCEPT{return boost::make_reverse_iterator(end());}
  207. reverse_iterator
  208. rend()BOOST_NOEXCEPT{return boost::make_reverse_iterator(begin());}
  209. const_reverse_iterator
  210. rend()const BOOST_NOEXCEPT{return boost::make_reverse_iterator(begin());}
  211. const_iterator
  212. cbegin()const BOOST_NOEXCEPT{return begin();}
  213. const_iterator
  214. cend()const BOOST_NOEXCEPT{return end();}
  215. const_reverse_iterator
  216. crbegin()const BOOST_NOEXCEPT{return rbegin();}
  217. const_reverse_iterator
  218. crend()const BOOST_NOEXCEPT{return rend();}
  219. iterator iterator_to(const value_type& x)
  220. {
  221. return make_iterator(
  222. node_from_value<index_node_type>(boost::addressof(x)));
  223. }
  224. const_iterator iterator_to(const value_type& x)const
  225. {
  226. return make_iterator(
  227. node_from_value<index_node_type>(boost::addressof(x)));
  228. }
  229. /* capacity */
  230. bool empty()const BOOST_NOEXCEPT{return this->final_empty_();}
  231. size_type size()const BOOST_NOEXCEPT{return this->final_size_();}
  232. size_type max_size()const BOOST_NOEXCEPT{return this->final_max_size_();}
  233. size_type capacity()const BOOST_NOEXCEPT{return ptrs.capacity();}
  234. void reserve(size_type n)
  235. {
  236. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  237. ptrs.reserve(n);
  238. }
  239. void shrink_to_fit()
  240. {
  241. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  242. ptrs.shrink_to_fit();
  243. }
  244. void resize(size_type n)
  245. {
  246. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  247. if(n>size())
  248. for(size_type m=n-size();m--;)
  249. this->final_emplace_(BOOST_MULTI_INDEX_NULL_PARAM_PACK);
  250. else if(n<size())erase(begin()+n,end());
  251. }
  252. void resize(size_type n,value_param_type x)
  253. {
  254. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  255. if(n>size())for(size_type m=n-size();m--;)this->final_insert_(x);
  256. else if(n<size())erase(begin()+n,end());
  257. }
  258. /* access: no non-const versions provided as random_access_index
  259. * handles const elements.
  260. */
  261. const_reference operator[](size_type n)const
  262. {
  263. BOOST_MULTI_INDEX_SAFE_MODE_ASSERT(n<size(),safe_mode::out_of_bounds);
  264. return index_node_type::from_impl(*ptrs.at(n))->value();
  265. }
  266. const_reference at(size_type n)const
  267. {
  268. if(n>=size())throw_exception(std::out_of_range("random access index"));
  269. return index_node_type::from_impl(*ptrs.at(n))->value();
  270. }
  271. const_reference front()const{return operator[](0);}
  272. const_reference back()const{return operator[](size()-1);}
  273. /* modifiers */
  274. BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL(
  275. pair_return_type,emplace_front,emplace_front_impl)
  276. std::pair<iterator,bool> push_front(const value_type& x)
  277. {return insert(begin(),x);}
  278. std::pair<iterator,bool> push_front(BOOST_RV_REF(value_type) x)
  279. {return insert(begin(),boost::move(x));}
  280. void pop_front(){erase(begin());}
  281. BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL(
  282. pair_return_type,emplace_back,emplace_back_impl)
  283. std::pair<iterator,bool> push_back(const value_type& x)
  284. {return insert(end(),x);}
  285. std::pair<iterator,bool> push_back(BOOST_RV_REF(value_type) x)
  286. {return insert(end(),boost::move(x));}
  287. void pop_back(){erase(--end());}
  288. BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL_EXTRA_ARG(
  289. pair_return_type,emplace,emplace_impl,iterator,position)
  290. std::pair<iterator,bool> insert(iterator position,const value_type& x)
  291. {
  292. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  293. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  294. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  295. std::pair<final_node_type*,bool> p=this->final_insert_(x);
  296. if(p.second&&position.get_node()!=header()){
  297. relocate(position.get_node(),p.first);
  298. }
  299. return std::pair<iterator,bool>(make_iterator(p.first),p.second);
  300. }
  301. std::pair<iterator,bool> insert(iterator position,BOOST_RV_REF(value_type) x)
  302. {
  303. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  304. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  305. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  306. std::pair<final_node_type*,bool> p=this->final_insert_rv_(x);
  307. if(p.second&&position.get_node()!=header()){
  308. relocate(position.get_node(),p.first);
  309. }
  310. return std::pair<iterator,bool>(make_iterator(p.first),p.second);
  311. }
  312. void insert(iterator position,size_type n,value_param_type x)
  313. {
  314. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  315. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  316. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  317. size_type s=0;
  318. BOOST_TRY{
  319. while(n--){
  320. if(push_back(x).second)++s;
  321. }
  322. }
  323. BOOST_CATCH(...){
  324. relocate(position,end()-s,end());
  325. BOOST_RETHROW;
  326. }
  327. BOOST_CATCH_END
  328. relocate(position,end()-s,end());
  329. }
  330. template<typename InputIterator>
  331. void insert(iterator position,InputIterator first,InputIterator last)
  332. {
  333. insert_iter(position,first,last,mpl::not_<is_integral<InputIterator> >());
  334. }
  335. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  336. void insert(iterator position,std::initializer_list<value_type> list)
  337. {
  338. insert(position,list.begin(),list.end());
  339. }
  340. #endif
  341. insert_return_type insert(const_iterator position,BOOST_RV_REF(node_type) nh)
  342. {
  343. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  344. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  345. if(nh)BOOST_MULTI_INDEX_CHECK_EQUAL_ALLOCATORS(*this,nh);
  346. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  347. std::pair<final_node_type*,bool> p=this->final_insert_nh_(nh);
  348. if(p.second&&position.get_node()!=header()){
  349. relocate(position.get_node(),p.first);
  350. }
  351. return insert_return_type(make_iterator(p.first),p.second,boost::move(nh));
  352. }
  353. node_type extract(const_iterator position)
  354. {
  355. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  356. BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
  357. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  358. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  359. return this->final_extract_(
  360. static_cast<final_node_type*>(position.get_node()));
  361. }
  362. iterator erase(iterator position)
  363. {
  364. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  365. BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
  366. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  367. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  368. this->final_erase_(static_cast<final_node_type*>(position++.get_node()));
  369. return position;
  370. }
  371. iterator erase(iterator first,iterator last)
  372. {
  373. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(first);
  374. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(last);
  375. BOOST_MULTI_INDEX_CHECK_IS_OWNER(first,*this);
  376. BOOST_MULTI_INDEX_CHECK_IS_OWNER(last,*this);
  377. BOOST_MULTI_INDEX_CHECK_VALID_RANGE(first,last);
  378. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  379. difference_type n=static_cast<difference_type>(last-first);
  380. relocate(end(),first,last);
  381. while(n--)pop_back();
  382. return last;
  383. }
  384. bool replace(iterator position,const value_type& x)
  385. {
  386. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  387. BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
  388. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  389. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  390. return this->final_replace_(
  391. x,static_cast<final_node_type*>(position.get_node()));
  392. }
  393. bool replace(iterator position,BOOST_RV_REF(value_type) x)
  394. {
  395. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  396. BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
  397. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  398. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  399. return this->final_replace_rv_(
  400. x,static_cast<final_node_type*>(position.get_node()));
  401. }
  402. template<typename Modifier>
  403. bool modify(iterator position,Modifier mod)
  404. {
  405. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  406. BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
  407. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  408. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  409. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  410. /* MSVC++ 6.0 optimizer on safe mode code chokes if this
  411. * this is not added. Left it for all compilers as it does no
  412. * harm.
  413. */
  414. position.detach();
  415. #endif
  416. return this->final_modify_(
  417. mod,static_cast<final_node_type*>(position.get_node()));
  418. }
  419. template<typename Modifier,typename Rollback>
  420. bool modify(iterator position,Modifier mod,Rollback back_)
  421. {
  422. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  423. BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
  424. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  425. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  426. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  427. /* MSVC++ 6.0 optimizer on safe mode code chokes if this
  428. * this is not added. Left it for all compilers as it does no
  429. * harm.
  430. */
  431. position.detach();
  432. #endif
  433. return this->final_modify_(
  434. mod,back_,static_cast<final_node_type*>(position.get_node()));
  435. }
  436. void swap(random_access_index<SuperMeta,TagList>& x)
  437. {
  438. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  439. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT_OF(x);
  440. this->final_swap_(x.final());
  441. }
  442. void clear()BOOST_NOEXCEPT
  443. {
  444. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  445. this->final_clear_();
  446. }
  447. /* list operations */
  448. template<typename Index>
  449. BOOST_MULTI_INDEX_ENABLE_IF_MERGEABLE(random_access_index,Index,void)
  450. splice(iterator position,Index& x)
  451. {
  452. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  453. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  454. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  455. if(x.end().get_node()==this->header()){ /* same container */
  456. BOOST_MULTI_INDEX_SAFE_MODE_ASSERT(
  457. position==end(),safe_mode::inside_range);
  458. }
  459. else{
  460. external_splice(
  461. position,x,x.begin(),x.end(),
  462. boost::is_copy_constructible<value_type>());
  463. }
  464. }
  465. template<typename Index>
  466. BOOST_MULTI_INDEX_ENABLE_IF_MERGEABLE(random_access_index,Index,void)
  467. splice(iterator position,BOOST_RV_REF(Index) x)
  468. {
  469. splice(position,static_cast<Index&>(x));
  470. }
  471. template<typename Index>
  472. BOOST_MULTI_INDEX_ENABLE_IF_MERGEABLE(
  473. random_access_index,Index,pair_return_type)
  474. splice(
  475. iterator position,Index& x,BOOST_DEDUCED_TYPENAME Index::iterator i)
  476. {
  477. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  478. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  479. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(i);
  480. BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(i);
  481. BOOST_MULTI_INDEX_CHECK_IS_OWNER(i,x);
  482. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  483. if(x.end().get_node()==this->header()){ /* same container */
  484. index_node_type* pn=position.get_node();
  485. index_node_type* in=static_cast<index_node_type*>(i.get_node());
  486. if(pn!=in)relocate(pn,in);
  487. return std::pair<iterator,bool>(make_iterator(in),true);
  488. }
  489. else{
  490. std::pair<final_node_type*,bool> p=
  491. external_splice(
  492. position,x,i,boost::is_copy_constructible<value_type>());
  493. return std::pair<iterator,bool>(make_iterator(p.first),p.second);
  494. }
  495. }
  496. template<typename Index>
  497. BOOST_MULTI_INDEX_ENABLE_IF_MERGEABLE(
  498. random_access_index,Index,pair_return_type)
  499. splice(
  500. iterator position,BOOST_RV_REF(Index) x,
  501. BOOST_DEDUCED_TYPENAME Index::iterator i)
  502. {
  503. return splice(position,static_cast<Index&>(x),i);
  504. }
  505. template<typename Index>
  506. BOOST_MULTI_INDEX_ENABLE_IF_MERGEABLE(random_access_index,Index,void)
  507. splice(
  508. iterator position,Index& x,
  509. BOOST_DEDUCED_TYPENAME Index::iterator first,
  510. BOOST_DEDUCED_TYPENAME Index::iterator last)
  511. {
  512. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  513. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  514. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(first);
  515. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(last);
  516. BOOST_MULTI_INDEX_CHECK_IS_OWNER(first,x);
  517. BOOST_MULTI_INDEX_CHECK_IS_OWNER(last,x);
  518. BOOST_MULTI_INDEX_CHECK_VALID_RANGE(first,last);
  519. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  520. if(x.end().get_node()==this->header()){ /* same container */
  521. BOOST_MULTI_INDEX_CHECK_OUTSIDE_RANGE(position,first,last);
  522. internal_splice(position,first,last);
  523. }
  524. else{
  525. external_splice(
  526. position,x,first,last,boost::is_copy_constructible<value_type>());
  527. }
  528. }
  529. template<typename Index>
  530. BOOST_MULTI_INDEX_ENABLE_IF_MERGEABLE(random_access_index,Index,void)
  531. splice(
  532. iterator position,BOOST_RV_REF(Index) x,
  533. BOOST_DEDUCED_TYPENAME Index::iterator first,
  534. BOOST_DEDUCED_TYPENAME Index::iterator last)
  535. {
  536. splice(position,static_cast<Index&>(x),first,last);
  537. }
  538. void remove(value_param_type value)
  539. {
  540. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  541. difference_type n=
  542. end()-make_iterator(
  543. random_access_index_remove<index_node_type>(
  544. ptrs,
  545. ::boost::bind<bool>(
  546. std::equal_to<value_type>(),::boost::arg<1>(),value)));
  547. while(n--)pop_back();
  548. }
  549. template<typename Predicate>
  550. void remove_if(Predicate pred)
  551. {
  552. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  553. difference_type n=
  554. end()-make_iterator(
  555. random_access_index_remove<index_node_type>(ptrs,pred));
  556. while(n--)pop_back();
  557. }
  558. void unique()
  559. {
  560. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  561. difference_type n=
  562. end()-make_iterator(
  563. random_access_index_unique<index_node_type>(
  564. ptrs,std::equal_to<value_type>()));
  565. while(n--)pop_back();
  566. }
  567. template <class BinaryPredicate>
  568. void unique(BinaryPredicate binary_pred)
  569. {
  570. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  571. difference_type n=
  572. end()-make_iterator(
  573. random_access_index_unique<index_node_type>(ptrs,binary_pred));
  574. while(n--)pop_back();
  575. }
  576. void merge(random_access_index<SuperMeta,TagList>& x)
  577. {
  578. if(this!=&x){
  579. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  580. size_type s=size();
  581. splice(end(),x);
  582. random_access_index_inplace_merge<index_node_type>(
  583. get_allocator(),ptrs,ptrs.at(s),std::less<value_type>());
  584. }
  585. }
  586. template <typename Compare>
  587. void merge(random_access_index<SuperMeta,TagList>& x,Compare comp)
  588. {
  589. if(this!=&x){
  590. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  591. size_type s=size();
  592. splice(end(),x);
  593. random_access_index_inplace_merge<index_node_type>(
  594. get_allocator(),ptrs,ptrs.at(s),comp);
  595. }
  596. }
  597. void sort()
  598. {
  599. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  600. random_access_index_sort<index_node_type>(
  601. get_allocator(),ptrs,std::less<value_type>());
  602. }
  603. template <typename Compare>
  604. void sort(Compare comp)
  605. {
  606. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  607. random_access_index_sort<index_node_type>(
  608. get_allocator(),ptrs,comp);
  609. }
  610. void reverse()BOOST_NOEXCEPT
  611. {
  612. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  613. node_impl_type::reverse(ptrs.begin(),ptrs.end());
  614. }
  615. /* rearrange operations */
  616. void relocate(iterator position,iterator i)
  617. {
  618. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  619. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  620. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(i);
  621. BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(i);
  622. BOOST_MULTI_INDEX_CHECK_IS_OWNER(i,*this);
  623. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  624. if(position!=i)relocate(position.get_node(),i.get_node());
  625. }
  626. void relocate(iterator position,iterator first,iterator last)
  627. {
  628. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  629. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  630. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(first);
  631. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(last);
  632. BOOST_MULTI_INDEX_CHECK_IS_OWNER(first,*this);
  633. BOOST_MULTI_INDEX_CHECK_IS_OWNER(last,*this);
  634. BOOST_MULTI_INDEX_CHECK_VALID_RANGE(first,last);
  635. BOOST_MULTI_INDEX_CHECK_OUTSIDE_RANGE(position,first,last);
  636. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  637. if(position!=last)relocate(
  638. position.get_node(),first.get_node(),last.get_node());
  639. }
  640. template<typename InputIterator>
  641. void rearrange(InputIterator first)
  642. {
  643. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  644. for(node_impl_ptr_pointer p0=ptrs.begin(),p0_end=ptrs.end();
  645. p0!=p0_end;++first,++p0){
  646. const value_type& v1=*first;
  647. node_impl_ptr_pointer p1=node_from_value<index_node_type>(&v1)->up();
  648. std::swap(*p0,*p1);
  649. (*p0)->up()=p0;
  650. (*p1)->up()=p1;
  651. }
  652. }
  653. BOOST_MULTI_INDEX_PROTECTED_IF_MEMBER_TEMPLATE_FRIENDS:
  654. random_access_index(
  655. const ctor_args_list& args_list,const allocator_type& al):
  656. super(args_list.get_tail(),al),
  657. ptrs(al,header()->impl(),0)
  658. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  659. ,safe(*this)
  660. #endif
  661. {
  662. }
  663. random_access_index(const random_access_index<SuperMeta,TagList>& x):
  664. super(x),
  665. ptrs(x.get_allocator(),header()->impl(),x.size())
  666. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  667. ,safe(*this)
  668. #endif
  669. {
  670. /* The actual copying takes place in subsequent call to copy_().
  671. */
  672. }
  673. random_access_index(
  674. const random_access_index<SuperMeta,TagList>& x,do_not_copy_elements_tag):
  675. super(x,do_not_copy_elements_tag()),
  676. ptrs(x.get_allocator(),header()->impl(),0)
  677. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  678. ,safe(*this)
  679. #endif
  680. {
  681. }
  682. ~random_access_index()
  683. {
  684. /* the container is guaranteed to be empty by now */
  685. }
  686. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  687. iterator make_iterator(index_node_type* node)
  688. {return iterator(node,&safe);}
  689. const_iterator make_iterator(index_node_type* node)const
  690. {return const_iterator(node,const_cast<safe_container*>(&safe));}
  691. #else
  692. iterator make_iterator(index_node_type* node){return iterator(node);}
  693. const_iterator make_iterator(index_node_type* node)const
  694. {return const_iterator(node);}
  695. #endif
  696. void copy_(
  697. const random_access_index<SuperMeta,TagList>& x,const copy_map_type& map)
  698. {
  699. for(node_impl_ptr_pointer begin_org=x.ptrs.begin(),
  700. begin_cpy=ptrs.begin(),
  701. end_org=x.ptrs.end();
  702. begin_org!=end_org;++begin_org,++begin_cpy){
  703. *begin_cpy=
  704. static_cast<index_node_type*>(
  705. map.find(
  706. static_cast<final_node_type*>(
  707. index_node_type::from_impl(*begin_org))))->impl();
  708. (*begin_cpy)->up()=begin_cpy;
  709. }
  710. super::copy_(x,map);
  711. }
  712. template<typename Variant>
  713. final_node_type* insert_(
  714. value_param_type v,final_node_type*& x,Variant variant)
  715. {
  716. ptrs.room_for_one();
  717. final_node_type* res=super::insert_(v,x,variant);
  718. if(res==x)ptrs.push_back(static_cast<index_node_type*>(x)->impl());
  719. return res;
  720. }
  721. template<typename Variant>
  722. final_node_type* insert_(
  723. value_param_type v,index_node_type* position,
  724. final_node_type*& x,Variant variant)
  725. {
  726. ptrs.room_for_one();
  727. final_node_type* res=super::insert_(v,position,x,variant);
  728. if(res==x)ptrs.push_back(static_cast<index_node_type*>(x)->impl());
  729. return res;
  730. }
  731. template<typename Dst>
  732. void extract_(index_node_type* x,Dst dst)
  733. {
  734. ptrs.erase(x->impl());
  735. super::extract_(x,dst.next());
  736. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  737. transfer_iterators(dst.get(),x);
  738. #endif
  739. }
  740. void delete_all_nodes_()
  741. {
  742. for(node_impl_ptr_pointer x=ptrs.begin(),x_end=ptrs.end();x!=x_end;++x){
  743. this->final_delete_node_(
  744. static_cast<final_node_type*>(index_node_type::from_impl(*x)));
  745. }
  746. }
  747. void clear_()
  748. {
  749. super::clear_();
  750. ptrs.clear();
  751. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  752. safe.detach_dereferenceable_iterators();
  753. #endif
  754. }
  755. template<typename BoolConstant>
  756. void swap_(
  757. random_access_index<SuperMeta,TagList>& x,BoolConstant swap_allocators)
  758. {
  759. ptrs.swap(x.ptrs,swap_allocators);
  760. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  761. safe.swap(x.safe);
  762. #endif
  763. super::swap_(x,swap_allocators);
  764. }
  765. void swap_elements_(random_access_index<SuperMeta,TagList>& x)
  766. {
  767. ptrs.swap(x.ptrs);
  768. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  769. safe.swap(x.safe);
  770. #endif
  771. super::swap_elements_(x);
  772. }
  773. template<typename Variant>
  774. bool replace_(value_param_type v,index_node_type* x,Variant variant)
  775. {
  776. return super::replace_(v,x,variant);
  777. }
  778. bool modify_(index_node_type* x)
  779. {
  780. BOOST_TRY{
  781. if(!super::modify_(x)){
  782. ptrs.erase(x->impl());
  783. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  784. detach_iterators(x);
  785. #endif
  786. return false;
  787. }
  788. else return true;
  789. }
  790. BOOST_CATCH(...){
  791. ptrs.erase(x->impl());
  792. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  793. detach_iterators(x);
  794. #endif
  795. BOOST_RETHROW;
  796. }
  797. BOOST_CATCH_END
  798. }
  799. bool modify_rollback_(index_node_type* x)
  800. {
  801. return super::modify_rollback_(x);
  802. }
  803. bool check_rollback_(index_node_type* x)const
  804. {
  805. return super::check_rollback_(x);
  806. }
  807. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  808. /* serialization */
  809. template<typename Archive>
  810. void save_(
  811. Archive& ar,const unsigned int version,const index_saver_type& sm)const
  812. {
  813. sm.save(begin(),end(),ar,version);
  814. super::save_(ar,version,sm);
  815. }
  816. template<typename Archive>
  817. void load_(
  818. Archive& ar,const unsigned int version,const index_loader_type& lm)
  819. {
  820. {
  821. typedef random_access_index_loader<
  822. index_node_type,allocator_type> loader;
  823. loader ld(get_allocator(),ptrs);
  824. lm.load(
  825. ::boost::bind(
  826. &loader::rearrange,&ld,::boost::arg<1>(),::boost::arg<2>()),
  827. ar,version);
  828. } /* exit scope so that ld frees its resources */
  829. super::load_(ar,version,lm);
  830. }
  831. #endif
  832. #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)
  833. /* invariant stuff */
  834. bool invariant_()const
  835. {
  836. if(size()>capacity())return false;
  837. if(size()==0||begin()==end()){
  838. if(size()!=0||begin()!=end())return false;
  839. }
  840. else{
  841. size_type s=0;
  842. for(const_iterator it=begin(),it_end=end();;++it,++s){
  843. if(*(it.get_node()->up())!=it.get_node()->impl())return false;
  844. if(it==it_end)break;
  845. }
  846. if(s!=size())return false;
  847. }
  848. return super::invariant_();
  849. }
  850. /* This forwarding function eases things for the boost::mem_fn construct
  851. * in BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT. Actually,
  852. * final_check_invariant is already an inherited member function of index.
  853. */
  854. void check_invariant_()const{this->final_check_invariant_();}
  855. #endif
  856. private:
  857. index_node_type* header()const{return this->final_header();}
  858. static void relocate(index_node_type* position,index_node_type* x)
  859. {
  860. node_impl_type::relocate(position->up(),x->up());
  861. }
  862. static void relocate(
  863. index_node_type* position,index_node_type* first,index_node_type* last)
  864. {
  865. node_impl_type::relocate(
  866. position->up(),first->up(),last->up());
  867. }
  868. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  869. void detach_iterators(index_node_type* x)
  870. {
  871. iterator it=make_iterator(x);
  872. safe_mode::detach_equivalent_iterators(it);
  873. }
  874. template<typename Dst>
  875. void transfer_iterators(Dst& dst,index_node_type* x)
  876. {
  877. iterator it=make_iterator(x);
  878. safe_mode::transfer_equivalent_iterators(dst,it);
  879. }
  880. #endif
  881. template <class InputIterator>
  882. void assign_iter(InputIterator first,InputIterator last,mpl::true_)
  883. {
  884. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  885. clear();
  886. for(;first!=last;++first)this->final_insert_ref_(*first);
  887. }
  888. void assign_iter(size_type n,value_param_type value,mpl::false_)
  889. {
  890. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  891. clear();
  892. for(size_type i=0;i<n;++i)push_back(value);
  893. }
  894. template<typename InputIterator>
  895. void insert_iter(
  896. iterator position,InputIterator first,InputIterator last,mpl::true_)
  897. {
  898. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  899. size_type s=0;
  900. BOOST_TRY{
  901. for(;first!=last;++first){
  902. if(this->final_insert_ref_(*first).second)++s;
  903. }
  904. }
  905. BOOST_CATCH(...){
  906. relocate(position,end()-s,end());
  907. BOOST_RETHROW;
  908. }
  909. BOOST_CATCH_END
  910. relocate(position,end()-s,end());
  911. }
  912. void insert_iter(
  913. iterator position,size_type n,value_param_type x,mpl::false_)
  914. {
  915. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  916. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  917. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  918. size_type s=0;
  919. BOOST_TRY{
  920. while(n--){
  921. if(push_back(x).second)++s;
  922. }
  923. }
  924. BOOST_CATCH(...){
  925. relocate(position,end()-s,end());
  926. BOOST_RETHROW;
  927. }
  928. BOOST_CATCH_END
  929. relocate(position,end()-s,end());
  930. }
  931. template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK>
  932. std::pair<iterator,bool> emplace_front_impl(
  933. BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)
  934. {
  935. return emplace_impl(begin(),BOOST_MULTI_INDEX_FORWARD_PARAM_PACK);
  936. }
  937. template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK>
  938. std::pair<iterator,bool> emplace_back_impl(
  939. BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)
  940. {
  941. return emplace_impl(end(),BOOST_MULTI_INDEX_FORWARD_PARAM_PACK);
  942. }
  943. template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK>
  944. std::pair<iterator,bool> emplace_impl(
  945. iterator position,BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)
  946. {
  947. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  948. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  949. BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
  950. std::pair<final_node_type*,bool> p=
  951. this->final_emplace_(BOOST_MULTI_INDEX_FORWARD_PARAM_PACK);
  952. if(p.second&&position.get_node()!=header()){
  953. relocate(position.get_node(),p.first);
  954. }
  955. return std::pair<iterator,bool>(make_iterator(p.first),p.second);
  956. }
  957. template<typename Index>
  958. std::pair<final_node_type*,bool> external_splice(
  959. iterator position,Index& x,BOOST_DEDUCED_TYPENAME Index::iterator i,
  960. boost::true_type /* copy-constructible value */)
  961. {
  962. if(get_allocator()==x.get_allocator()){
  963. return external_splice(position,x,i,boost::false_type());
  964. }
  965. else{
  966. /* backwards compatibility with old, non-transfer-based splice */
  967. std::pair<iterator,bool> p=insert(position,*i);
  968. if(p.second)x.erase(i);
  969. return std::pair<final_node_type*,bool>(
  970. static_cast<final_node_type*>(p.first.get_node()),p.second);
  971. }
  972. }
  973. template<typename Index>
  974. std::pair<final_node_type*,bool> external_splice(
  975. iterator position,Index& x,BOOST_DEDUCED_TYPENAME Index::iterator i,
  976. boost::false_type /* copy-constructible value */)
  977. {
  978. BOOST_MULTI_INDEX_CHECK_EQUAL_ALLOCATORS(*this,x);
  979. std::pair<final_node_type*,bool> p=this->final_transfer_(
  980. x,static_cast<final_node_type*>(i.get_node()));
  981. if(p.second&&position.get_node()!=header()){
  982. relocate(position.get_node(),p.first);
  983. }
  984. return p;
  985. }
  986. template<typename Iterator>
  987. void internal_splice(iterator position,Iterator first,Iterator last)
  988. {
  989. /* null out [first, last) positions in ptrs array */
  990. for(Iterator it=first;it!=last;++it){
  991. *(static_cast<index_node_type*>(it.get_node())->up())=0;
  992. }
  993. node_impl_ptr_pointer pp=node_impl_type::gather_nulls(
  994. ptrs.begin(),ptrs.end(),
  995. static_cast<index_node_type*>(position.get_node())->up());
  996. /* relink [first, last) */
  997. for(Iterator it=first;it!=last;++it,++pp){
  998. *pp=static_cast<index_node_type*>(it.get_node());
  999. (*pp)->up()=pp;
  1000. }
  1001. }
  1002. void internal_splice(iterator position,iterator first,iterator last)
  1003. {
  1004. index_node_type* pn=position.get_node();
  1005. index_node_type* fn=static_cast<index_node_type*>(first.get_node());
  1006. index_node_type* ln=static_cast<index_node_type*>(last.get_node());
  1007. if(pn!=ln)relocate(pn,fn,ln);
  1008. }
  1009. template<typename Index>
  1010. void external_splice(
  1011. iterator position,Index& x,
  1012. BOOST_DEDUCED_TYPENAME Index::iterator first,
  1013. BOOST_DEDUCED_TYPENAME Index::iterator last,
  1014. boost::true_type /* copy-constructible value */)
  1015. {
  1016. if(get_allocator()==x.get_allocator()){
  1017. external_splice(position,x,first,last,boost::false_type());
  1018. }
  1019. else{
  1020. /* backwards compatibility with old, non-transfer-based splice */
  1021. size_type n=size();
  1022. BOOST_TRY{
  1023. while(first!=last){
  1024. if(push_back(*first).second)first=x.erase(first);
  1025. else ++first;
  1026. }
  1027. }
  1028. BOOST_CATCH(...){
  1029. relocate(position,begin()+n,end());
  1030. BOOST_RETHROW;
  1031. }
  1032. BOOST_CATCH_END
  1033. relocate(position,begin()+n,end());
  1034. }
  1035. }
  1036. template<typename Index>
  1037. void external_splice(
  1038. iterator position,Index& x,
  1039. BOOST_DEDUCED_TYPENAME Index::iterator first,
  1040. BOOST_DEDUCED_TYPENAME Index::iterator last,
  1041. boost::false_type /* copy-constructible value */)
  1042. {
  1043. BOOST_MULTI_INDEX_CHECK_EQUAL_ALLOCATORS(*this,x);
  1044. size_type n=size();
  1045. BOOST_TRY{
  1046. this->final_transfer_range_(x,first,last);
  1047. }
  1048. BOOST_CATCH(...){
  1049. relocate(position,begin()+n,end());
  1050. BOOST_RETHROW;
  1051. }
  1052. BOOST_CATCH_END
  1053. relocate(position,begin()+n,end());
  1054. }
  1055. ptr_array ptrs;
  1056. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  1057. safe_container safe;
  1058. #endif
  1059. #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)&&\
  1060. BOOST_WORKAROUND(__MWERKS__,<=0x3003)
  1061. #pragma parse_mfunc_templ reset
  1062. #endif
  1063. };
  1064. #if defined(BOOST_MSVC)
  1065. #pragma warning(pop) /* C4355 */
  1066. #endif
  1067. /* comparison */
  1068. template<
  1069. typename SuperMeta1,typename TagList1,
  1070. typename SuperMeta2,typename TagList2
  1071. >
  1072. bool operator==(
  1073. const random_access_index<SuperMeta1,TagList1>& x,
  1074. const random_access_index<SuperMeta2,TagList2>& y)
  1075. {
  1076. return x.size()==y.size()&&std::equal(x.begin(),x.end(),y.begin());
  1077. }
  1078. template<
  1079. typename SuperMeta1,typename TagList1,
  1080. typename SuperMeta2,typename TagList2
  1081. >
  1082. bool operator<(
  1083. const random_access_index<SuperMeta1,TagList1>& x,
  1084. const random_access_index<SuperMeta2,TagList2>& y)
  1085. {
  1086. return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());
  1087. }
  1088. template<
  1089. typename SuperMeta1,typename TagList1,
  1090. typename SuperMeta2,typename TagList2
  1091. >
  1092. bool operator!=(
  1093. const random_access_index<SuperMeta1,TagList1>& x,
  1094. const random_access_index<SuperMeta2,TagList2>& y)
  1095. {
  1096. return !(x==y);
  1097. }
  1098. template<
  1099. typename SuperMeta1,typename TagList1,
  1100. typename SuperMeta2,typename TagList2
  1101. >
  1102. bool operator>(
  1103. const random_access_index<SuperMeta1,TagList1>& x,
  1104. const random_access_index<SuperMeta2,TagList2>& y)
  1105. {
  1106. return y<x;
  1107. }
  1108. template<
  1109. typename SuperMeta1,typename TagList1,
  1110. typename SuperMeta2,typename TagList2
  1111. >
  1112. bool operator>=(
  1113. const random_access_index<SuperMeta1,TagList1>& x,
  1114. const random_access_index<SuperMeta2,TagList2>& y)
  1115. {
  1116. return !(x<y);
  1117. }
  1118. template<
  1119. typename SuperMeta1,typename TagList1,
  1120. typename SuperMeta2,typename TagList2
  1121. >
  1122. bool operator<=(
  1123. const random_access_index<SuperMeta1,TagList1>& x,
  1124. const random_access_index<SuperMeta2,TagList2>& y)
  1125. {
  1126. return !(x>y);
  1127. }
  1128. /* specialized algorithms */
  1129. template<typename SuperMeta,typename TagList>
  1130. void swap(
  1131. random_access_index<SuperMeta,TagList>& x,
  1132. random_access_index<SuperMeta,TagList>& y)
  1133. {
  1134. x.swap(y);
  1135. }
  1136. } /* namespace multi_index::detail */
  1137. /* random access index specifier */
  1138. template <typename TagList>
  1139. struct random_access
  1140. {
  1141. BOOST_STATIC_ASSERT(detail::is_tag<TagList>::value);
  1142. template<typename Super>
  1143. struct node_class
  1144. {
  1145. typedef detail::random_access_index_node<Super> type;
  1146. };
  1147. template<typename SuperMeta>
  1148. struct index_class
  1149. {
  1150. typedef detail::random_access_index<
  1151. SuperMeta,typename TagList::type> type;
  1152. };
  1153. };
  1154. } /* namespace multi_index */
  1155. } /* namespace boost */
  1156. /* Boost.Foreach compatibility */
  1157. namespace boost{
  1158. namespace foreach{
  1159. template<typename>
  1160. struct is_noncopyable;
  1161. template<typename SuperMeta,typename TagList>
  1162. struct is_noncopyable<
  1163. boost::multi_index::detail::random_access_index<SuperMeta,TagList>
  1164. >:boost::mpl::true_{};
  1165. }
  1166. }
  1167. #undef BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT
  1168. #undef BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT_OF
  1169. #endif