sequenced_index.hpp 32 KB

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