sequenced_index.hpp 30 KB

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