sequenced_index.hpp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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