sequenced_index.hpp 31 KB

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