random_access_index.hpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  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