sequenced_index.hpp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /* Copyright 2003-2020 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/multi_index for library home page.
  7. */
  8. #ifndef BOOST_MULTI_INDEX_SEQUENCED_INDEX_HPP
  9. #define BOOST_MULTI_INDEX_SEQUENCED_INDEX_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  14. #include <boost/bind/bind.hpp>
  15. #include <boost/call_traits.hpp>
  16. #include <boost/core/addressof.hpp>
  17. #include <boost/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_ENABLE_INVARIANT_CHECKING)
  45. #define BOOST_MULTI_INDEX_SEQ_INDEX_CHECK_INVARIANT_OF(x) \
  46. detail::scope_guard BOOST_JOIN(check_invariant_,__LINE__)= \
  47. detail::make_obj_guard(x,&sequenced_index::check_invariant_); \
  48. BOOST_JOIN(check_invariant_,__LINE__).touch();
  49. #define BOOST_MULTI_INDEX_SEQ_INDEX_CHECK_INVARIANT \
  50. BOOST_MULTI_INDEX_SEQ_INDEX_CHECK_INVARIANT_OF(*this)
  51. #else
  52. #define BOOST_MULTI_INDEX_SEQ_INDEX_CHECK_INVARIANT_OF(x)
  53. #define BOOST_MULTI_INDEX_SEQ_INDEX_CHECK_INVARIANT
  54. #endif
  55. namespace boost{
  56. namespace multi_index{
  57. namespace detail{
  58. /* sequenced_index adds a layer of sequenced indexing to a given Super */
  59. template<typename SuperMeta,typename TagList>
  60. class sequenced_index:
  61. BOOST_MULTI_INDEX_PROTECTED_IF_MEMBER_TEMPLATE_FRIENDS SuperMeta::type
  62. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  63. ,public safe_mode::safe_container<
  64. sequenced_index<SuperMeta,TagList> >
  65. #endif
  66. {
  67. #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)&&\
  68. BOOST_WORKAROUND(__MWERKS__,<=0x3003)
  69. /* The "ISO C++ Template Parser" option in CW8.3 has a problem with the
  70. * lifetime of const references bound to temporaries --precisely what
  71. * scopeguards are.
  72. */
  73. #pragma parse_mfunc_templ off
  74. #endif
  75. typedef typename SuperMeta::type super;
  76. protected:
  77. typedef sequenced_index_node<
  78. typename super::node_type> node_type;
  79. private:
  80. typedef typename node_type::impl_type node_impl_type;
  81. public:
  82. /* types */
  83. typedef typename node_type::value_type value_type;
  84. typedef tuples::null_type ctor_args;
  85. typedef typename super::final_allocator_type allocator_type;
  86. typedef value_type& reference;
  87. typedef const value_type& const_reference;
  88. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  89. typedef safe_mode::safe_iterator<
  90. bidir_node_iterator<node_type>,
  91. sequenced_index> iterator;
  92. #else
  93. typedef bidir_node_iterator<node_type> iterator;
  94. #endif
  95. typedef iterator const_iterator;
  96. private:
  97. typedef allocator_traits<allocator_type> alloc_traits;
  98. public:
  99. typedef typename alloc_traits::pointer pointer;
  100. typedef typename alloc_traits::const_pointer const_pointer;
  101. typedef typename alloc_traits::size_type size_type;
  102. typedef typename alloc_traits::difference_type difference_type;
  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>(boost::addressof(x)));
  205. }
  206. const_iterator iterator_to(const value_type& x)const
  207. {
  208. return make_iterator(node_from_value<node_type>(boost::addressof(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(),static_cast<size_type>(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. template<typename BoolConstant>
  611. void swap_(
  612. sequenced_index<SuperMeta,TagList>& x,BoolConstant swap_allocators)
  613. {
  614. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  615. safe_super::swap(x);
  616. #endif
  617. super::swap_(x,swap_allocators);
  618. }
  619. void swap_elements_(sequenced_index<SuperMeta,TagList>& x)
  620. {
  621. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  622. safe_super::swap(x);
  623. #endif
  624. super::swap_elements_(x);
  625. }
  626. template<typename Variant>
  627. bool replace_(value_param_type v,node_type* x,Variant variant)
  628. {
  629. return super::replace_(v,x,variant);
  630. }
  631. bool modify_(node_type* x)
  632. {
  633. BOOST_TRY{
  634. if(!super::modify_(x)){
  635. unlink(x);
  636. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  637. detach_iterators(x);
  638. #endif
  639. return false;
  640. }
  641. else return true;
  642. }
  643. BOOST_CATCH(...){
  644. unlink(x);
  645. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  646. detach_iterators(x);
  647. #endif
  648. BOOST_RETHROW;
  649. }
  650. BOOST_CATCH_END
  651. }
  652. bool modify_rollback_(node_type* x)
  653. {
  654. return super::modify_rollback_(x);
  655. }
  656. bool check_rollback_(node_type* x)const
  657. {
  658. return super::check_rollback_(x);
  659. }
  660. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  661. /* serialization */
  662. template<typename Archive>
  663. void save_(
  664. Archive& ar,const unsigned int version,const index_saver_type& sm)const
  665. {
  666. sm.save(begin(),end(),ar,version);
  667. super::save_(ar,version,sm);
  668. }
  669. template<typename Archive>
  670. void load_(
  671. Archive& ar,const unsigned int version,const index_loader_type& lm)
  672. {
  673. lm.load(
  674. ::boost::bind(
  675. &sequenced_index::rearranger,this,::boost::arg<1>(),::boost::arg<2>()),
  676. ar,version);
  677. super::load_(ar,version,lm);
  678. }
  679. #endif
  680. #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)
  681. /* invariant stuff */
  682. bool invariant_()const
  683. {
  684. if(size()==0||begin()==end()){
  685. if(size()!=0||begin()!=end()||
  686. header()->next()!=header()->impl()||
  687. header()->prior()!=header()->impl())return false;
  688. }
  689. else{
  690. size_type s=0;
  691. for(const_iterator it=begin(),it_end=end();it!=it_end;++it,++s){
  692. if(it.get_node()->next()->prior()!=it.get_node()->impl())return false;
  693. if(it.get_node()->prior()->next()!=it.get_node()->impl())return false;
  694. }
  695. if(s!=size())return false;
  696. }
  697. return super::invariant_();
  698. }
  699. /* This forwarding function eases things for the boost::mem_fn construct
  700. * in BOOST_MULTI_INDEX_SEQ_INDEX_CHECK_INVARIANT. Actually,
  701. * final_check_invariant is already an inherited member function of index.
  702. */
  703. void check_invariant_()const{this->final_check_invariant_();}
  704. #endif
  705. private:
  706. node_type* header()const{return this->final_header();}
  707. void empty_initialize()
  708. {
  709. header()->prior()=header()->next()=header()->impl();
  710. }
  711. void link(node_type* x)
  712. {
  713. node_impl_type::link(x->impl(),header()->impl());
  714. }
  715. static void unlink(node_type* x)
  716. {
  717. node_impl_type::unlink(x->impl());
  718. }
  719. static void relink(node_type* position,node_type* x)
  720. {
  721. node_impl_type::relink(position->impl(),x->impl());
  722. }
  723. static void relink(node_type* position,node_type* first,node_type* last)
  724. {
  725. node_impl_type::relink(
  726. position->impl(),first->impl(),last->impl());
  727. }
  728. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  729. void rearranger(node_type* position,node_type *x)
  730. {
  731. if(!position)position=header();
  732. node_type::increment(position);
  733. if(position!=x)relink(position,x);
  734. }
  735. #endif
  736. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  737. void detach_iterators(node_type* x)
  738. {
  739. iterator it=make_iterator(x);
  740. safe_mode::detach_equivalent_iterators(it);
  741. }
  742. #endif
  743. template <class InputIterator>
  744. void assign_iter(InputIterator first,InputIterator last,mpl::true_)
  745. {
  746. BOOST_MULTI_INDEX_SEQ_INDEX_CHECK_INVARIANT;
  747. clear();
  748. for(;first!=last;++first)this->final_insert_ref_(*first);
  749. }
  750. void assign_iter(size_type n,value_param_type value,mpl::false_)
  751. {
  752. BOOST_MULTI_INDEX_SEQ_INDEX_CHECK_INVARIANT;
  753. clear();
  754. for(size_type i=0;i<n;++i)push_back(value);
  755. }
  756. template<typename InputIterator>
  757. void insert_iter(
  758. iterator position,InputIterator first,InputIterator last,mpl::true_)
  759. {
  760. BOOST_MULTI_INDEX_SEQ_INDEX_CHECK_INVARIANT;
  761. for(;first!=last;++first){
  762. std::pair<final_node_type*,bool> p=
  763. this->final_insert_ref_(*first);
  764. if(p.second&&position.get_node()!=header()){
  765. relink(position.get_node(),p.first);
  766. }
  767. }
  768. }
  769. void insert_iter(
  770. iterator position,size_type n,value_param_type x,mpl::false_)
  771. {
  772. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  773. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  774. BOOST_MULTI_INDEX_SEQ_INDEX_CHECK_INVARIANT;
  775. for(size_type i=0;i<n;++i)insert(position,x);
  776. }
  777. template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK>
  778. std::pair<iterator,bool> emplace_front_impl(
  779. BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)
  780. {
  781. return emplace_impl(begin(),BOOST_MULTI_INDEX_FORWARD_PARAM_PACK);
  782. }
  783. template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK>
  784. std::pair<iterator,bool> emplace_back_impl(
  785. BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)
  786. {
  787. return emplace_impl(end(),BOOST_MULTI_INDEX_FORWARD_PARAM_PACK);
  788. }
  789. template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK>
  790. std::pair<iterator,bool> emplace_impl(
  791. iterator position,BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)
  792. {
  793. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  794. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  795. BOOST_MULTI_INDEX_SEQ_INDEX_CHECK_INVARIANT;
  796. std::pair<final_node_type*,bool> p=
  797. this->final_emplace_(BOOST_MULTI_INDEX_FORWARD_PARAM_PACK);
  798. if(p.second&&position.get_node()!=header()){
  799. relink(position.get_node(),p.first);
  800. }
  801. return std::pair<iterator,bool>(make_iterator(p.first),p.second);
  802. }
  803. #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)&&\
  804. BOOST_WORKAROUND(__MWERKS__,<=0x3003)
  805. #pragma parse_mfunc_templ reset
  806. #endif
  807. };
  808. /* comparison */
  809. template<
  810. typename SuperMeta1,typename TagList1,
  811. typename SuperMeta2,typename TagList2
  812. >
  813. bool operator==(
  814. const sequenced_index<SuperMeta1,TagList1>& x,
  815. const sequenced_index<SuperMeta2,TagList2>& y)
  816. {
  817. return x.size()==y.size()&&std::equal(x.begin(),x.end(),y.begin());
  818. }
  819. template<
  820. typename SuperMeta1,typename TagList1,
  821. typename SuperMeta2,typename TagList2
  822. >
  823. bool operator<(
  824. const sequenced_index<SuperMeta1,TagList1>& x,
  825. const sequenced_index<SuperMeta2,TagList2>& y)
  826. {
  827. return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());
  828. }
  829. template<
  830. typename SuperMeta1,typename TagList1,
  831. typename SuperMeta2,typename TagList2
  832. >
  833. bool operator!=(
  834. const sequenced_index<SuperMeta1,TagList1>& x,
  835. const sequenced_index<SuperMeta2,TagList2>& y)
  836. {
  837. return !(x==y);
  838. }
  839. template<
  840. typename SuperMeta1,typename TagList1,
  841. typename SuperMeta2,typename TagList2
  842. >
  843. bool operator>(
  844. const sequenced_index<SuperMeta1,TagList1>& x,
  845. const sequenced_index<SuperMeta2,TagList2>& y)
  846. {
  847. return y<x;
  848. }
  849. template<
  850. typename SuperMeta1,typename TagList1,
  851. typename SuperMeta2,typename TagList2
  852. >
  853. bool operator>=(
  854. const sequenced_index<SuperMeta1,TagList1>& x,
  855. const sequenced_index<SuperMeta2,TagList2>& y)
  856. {
  857. return !(x<y);
  858. }
  859. template<
  860. typename SuperMeta1,typename TagList1,
  861. typename SuperMeta2,typename TagList2
  862. >
  863. bool operator<=(
  864. const sequenced_index<SuperMeta1,TagList1>& x,
  865. const sequenced_index<SuperMeta2,TagList2>& y)
  866. {
  867. return !(x>y);
  868. }
  869. /* specialized algorithms */
  870. template<typename SuperMeta,typename TagList>
  871. void swap(
  872. sequenced_index<SuperMeta,TagList>& x,
  873. sequenced_index<SuperMeta,TagList>& y)
  874. {
  875. x.swap(y);
  876. }
  877. } /* namespace multi_index::detail */
  878. /* sequenced index specifier */
  879. template <typename TagList>
  880. struct sequenced
  881. {
  882. BOOST_STATIC_ASSERT(detail::is_tag<TagList>::value);
  883. template<typename Super>
  884. struct node_class
  885. {
  886. typedef detail::sequenced_index_node<Super> type;
  887. };
  888. template<typename SuperMeta>
  889. struct index_class
  890. {
  891. typedef detail::sequenced_index<SuperMeta,typename TagList::type> type;
  892. };
  893. };
  894. } /* namespace multi_index */
  895. } /* namespace boost */
  896. /* Boost.Foreach compatibility */
  897. template<typename SuperMeta,typename TagList>
  898. inline boost::mpl::true_* boost_foreach_is_noncopyable(
  899. boost::multi_index::detail::sequenced_index<SuperMeta,TagList>*&,
  900. boost_foreach_argument_dependent_lookup_hack)
  901. {
  902. return 0;
  903. }
  904. #undef BOOST_MULTI_INDEX_SEQ_INDEX_CHECK_INVARIANT
  905. #undef BOOST_MULTI_INDEX_SEQ_INDEX_CHECK_INVARIANT_OF
  906. #endif