random_access_index.hpp 33 KB

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