sequenced_index.hpp 30 KB

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