slist.hpp 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Olaf Krzikalla 2004-2006.
  4. // (C) Copyright Ion Gaztanaga 2006-2014
  5. //
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // See http://www.boost.org/libs/intrusive for documentation.
  11. //
  12. /////////////////////////////////////////////////////////////////////////////
  13. #ifndef BOOST_INTRUSIVE_SLIST_HPP
  14. #define BOOST_INTRUSIVE_SLIST_HPP
  15. #include <boost/intrusive/detail/config_begin.hpp>
  16. #include <boost/intrusive/intrusive_fwd.hpp>
  17. #include <boost/intrusive/detail/assert.hpp>
  18. #include <boost/intrusive/slist_hook.hpp>
  19. #include <boost/intrusive/circular_slist_algorithms.hpp>
  20. #include <boost/intrusive/linear_slist_algorithms.hpp>
  21. #include <boost/intrusive/pointer_traits.hpp>
  22. #include <boost/intrusive/link_mode.hpp>
  23. #include <boost/intrusive/detail/get_value_traits.hpp>
  24. #include <boost/intrusive/detail/is_stateful_value_traits.hpp>
  25. #include <boost/intrusive/detail/default_header_holder.hpp>
  26. #include <boost/intrusive/detail/uncast.hpp>
  27. #include <boost/intrusive/detail/mpl.hpp>
  28. #include <boost/intrusive/detail/iterator.hpp>
  29. #include <boost/intrusive/detail/slist_iterator.hpp>
  30. #include <boost/intrusive/detail/array_initializer.hpp>
  31. #include <boost/intrusive/detail/exception_disposer.hpp>
  32. #include <boost/intrusive/detail/equal_to_value.hpp>
  33. #include <boost/intrusive/detail/key_nodeptr_comp.hpp>
  34. #include <boost/intrusive/detail/simple_disposers.hpp>
  35. #include <boost/intrusive/detail/size_holder.hpp>
  36. #include <boost/intrusive/detail/algorithm.hpp>
  37. #include <boost/intrusive/detail/value_functors.hpp>
  38. #include <boost/move/utility_core.hpp>
  39. #include <boost/static_assert.hpp>
  40. #include <cstddef> //std::size_t
  41. #if defined(BOOST_HAS_PRAGMA_ONCE)
  42. # pragma once
  43. #endif
  44. namespace boost {
  45. namespace intrusive {
  46. /// @cond
  47. template<class HeaderHolder, class NodePtr, bool>
  48. struct header_holder_plus_last
  49. {
  50. HeaderHolder header_holder_;
  51. NodePtr last_;
  52. };
  53. template<class HeaderHolder, class NodePtr>
  54. struct header_holder_plus_last<HeaderHolder, NodePtr, false>
  55. {
  56. HeaderHolder header_holder_;
  57. };
  58. struct default_slist_hook_applier
  59. { template <class T> struct apply{ typedef typename T::default_slist_hook type; }; };
  60. template<>
  61. struct is_default_hook_tag<default_slist_hook_applier>
  62. { static const bool value = true; };
  63. struct slist_defaults
  64. {
  65. typedef default_slist_hook_applier proto_value_traits;
  66. static const bool constant_time_size = true;
  67. static const bool linear = false;
  68. typedef std::size_t size_type;
  69. static const bool cache_last = false;
  70. typedef void header_holder_type;
  71. };
  72. struct slist_bool_flags
  73. {
  74. static const std::size_t linear_pos = 1u;
  75. static const std::size_t constant_time_size_pos = 2u;
  76. static const std::size_t cache_last_pos = 4u;
  77. };
  78. /// @endcond
  79. //! The class template slist is an intrusive container, that encapsulates
  80. //! a singly-linked list. You can use such a list to squeeze the last bit
  81. //! of performance from your application. Unfortunately, the little gains
  82. //! come with some huge drawbacks. A lot of member functions can't be
  83. //! implemented as efficiently as for standard containers. To overcome
  84. //! this limitation some other member functions with rather unusual semantics
  85. //! have to be introduced.
  86. //!
  87. //! The template parameter \c T is the type to be managed by the container.
  88. //! The user can specify additional options and if no options are provided
  89. //! default options are used.
  90. //!
  91. //! The container supports the following options:
  92. //! \c base_hook<>/member_hook<>/value_traits<>,
  93. //! \c constant_time_size<>, \c size_type<>,
  94. //! \c linear<> and \c cache_last<>.
  95. //!
  96. //! The iterators of slist are forward iterators. slist provides a static
  97. //! function called "previous" to compute the previous iterator of a given iterator.
  98. //! This function has linear complexity. To improve the usability esp. with
  99. //! the '*_after' functions, ++end() == begin() and previous(begin()) == end()
  100. //! are defined. An new special function "before_begin()" is defined, which returns
  101. //! an iterator that points one less the beginning of the list: ++before_begin() == begin()
  102. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  103. template<class T, class ...Options>
  104. #else
  105. template<class ValueTraits, class SizeType, std::size_t BoolFlags, typename HeaderHolder>
  106. #endif
  107. class slist_impl
  108. {
  109. //Public typedefs
  110. public:
  111. typedef ValueTraits value_traits;
  112. typedef typename value_traits::pointer pointer;
  113. typedef typename value_traits::const_pointer const_pointer;
  114. typedef typename pointer_traits<pointer>::element_type value_type;
  115. typedef typename pointer_traits<pointer>::reference reference;
  116. typedef typename pointer_traits<const_pointer>::reference const_reference;
  117. typedef typename pointer_traits<pointer>::difference_type difference_type;
  118. typedef SizeType size_type;
  119. typedef slist_iterator<value_traits, false> iterator;
  120. typedef slist_iterator<value_traits, true> const_iterator;
  121. typedef typename value_traits::node_traits node_traits;
  122. typedef typename node_traits::node node;
  123. typedef typename node_traits::node_ptr node_ptr;
  124. typedef typename node_traits::const_node_ptr const_node_ptr;
  125. typedef typename detail::get_header_holder_type
  126. < value_traits, HeaderHolder >::type header_holder_type;
  127. static const bool constant_time_size = 0 != (BoolFlags & slist_bool_flags::constant_time_size_pos);
  128. static const bool stateful_value_traits = detail::is_stateful_value_traits<value_traits>::value;
  129. static const bool linear = 0 != (BoolFlags & slist_bool_flags::linear_pos);
  130. static const bool cache_last = 0 != (BoolFlags & slist_bool_flags::cache_last_pos);
  131. static const bool has_container_from_iterator =
  132. detail::is_same< header_holder_type, detail::default_header_holder< node_traits > >::value;
  133. typedef typename detail::if_c
  134. < linear
  135. , linear_slist_algorithms<node_traits>
  136. , circular_slist_algorithms<node_traits>
  137. >::type node_algorithms;
  138. /// @cond
  139. private:
  140. typedef detail::size_holder<constant_time_size, size_type> size_traits;
  141. //noncopyable
  142. BOOST_MOVABLE_BUT_NOT_COPYABLE(slist_impl)
  143. static const bool safemode_or_autounlink = is_safe_autounlink<value_traits::link_mode>::value;
  144. //Constant-time size is incompatible with auto-unlink hooks!
  145. BOOST_STATIC_ASSERT(!(constant_time_size && ((int)value_traits::link_mode == (int)auto_unlink)));
  146. //Linear singly linked lists are incompatible with auto-unlink hooks!
  147. BOOST_STATIC_ASSERT(!(linear && ((int)value_traits::link_mode == (int)auto_unlink)));
  148. //A list with cached last node is incompatible with auto-unlink hooks!
  149. BOOST_STATIC_ASSERT(!(cache_last && ((int)value_traits::link_mode == (int)auto_unlink)));
  150. node_ptr get_end_node()
  151. { return node_ptr(linear ? node_ptr() : this->get_root_node()); }
  152. const_node_ptr get_end_node() const
  153. {
  154. return const_node_ptr
  155. (linear ? const_node_ptr() : this->get_root_node()); }
  156. node_ptr get_root_node()
  157. { return data_.root_plus_size_.header_holder_.get_node(); }
  158. const_node_ptr get_root_node() const
  159. { return data_.root_plus_size_.header_holder_.get_node(); }
  160. node_ptr get_last_node()
  161. { return this->get_last_node(detail::bool_<cache_last>()); }
  162. const_node_ptr get_last_node() const
  163. { return this->get_last_node(detail::bool_<cache_last>()); }
  164. void set_last_node(const node_ptr &n)
  165. { return this->set_last_node(n, detail::bool_<cache_last>()); }
  166. static node_ptr get_last_node(detail::bool_<false>)
  167. {
  168. //This function shall not be used if cache_last is not true
  169. BOOST_INTRUSIVE_INVARIANT_ASSERT(cache_last);
  170. return node_ptr();
  171. }
  172. static void set_last_node(const node_ptr &, detail::bool_<false>)
  173. {
  174. //This function shall not be used if cache_last is not true
  175. BOOST_INTRUSIVE_INVARIANT_ASSERT(cache_last);
  176. }
  177. node_ptr get_last_node(detail::bool_<true>)
  178. { return node_ptr(data_.root_plus_size_.last_); }
  179. const_node_ptr get_last_node(detail::bool_<true>) const
  180. { return const_node_ptr(data_.root_plus_size_.last_); }
  181. void set_last_node(const node_ptr & n, detail::bool_<true>)
  182. { data_.root_plus_size_.last_ = n; }
  183. void set_default_constructed_state()
  184. {
  185. node_algorithms::init_header(this->get_root_node());
  186. this->priv_size_traits().set_size(size_type(0));
  187. if(cache_last){
  188. this->set_last_node(this->get_root_node());
  189. }
  190. }
  191. typedef header_holder_plus_last<header_holder_type, node_ptr, cache_last> header_holder_plus_last_t;
  192. struct root_plus_size
  193. : public size_traits
  194. , public header_holder_plus_last_t
  195. {};
  196. struct data_t
  197. : public value_traits
  198. {
  199. typedef typename slist_impl::value_traits value_traits;
  200. explicit data_t(const value_traits &val_traits)
  201. : value_traits(val_traits)
  202. {}
  203. root_plus_size root_plus_size_;
  204. } data_;
  205. size_traits &priv_size_traits()
  206. { return data_.root_plus_size_; }
  207. const size_traits &priv_size_traits() const
  208. { return data_.root_plus_size_; }
  209. const value_traits &priv_value_traits() const
  210. { return data_; }
  211. value_traits &priv_value_traits()
  212. { return data_; }
  213. typedef typename boost::intrusive::value_traits_pointers
  214. <ValueTraits>::const_value_traits_ptr const_value_traits_ptr;
  215. const_value_traits_ptr priv_value_traits_ptr() const
  216. { return pointer_traits<const_value_traits_ptr>::pointer_to(this->priv_value_traits()); }
  217. /// @endcond
  218. public:
  219. ///@cond
  220. //! <b>Requires</b>: f and before_l belong to another slist.
  221. //!
  222. //! <b>Effects</b>: Transfers the range [f, before_l] to this
  223. //! list, after the element pointed by prev_pos.
  224. //! No destructors or copy constructors are called.
  225. //!
  226. //! <b>Throws</b>: Nothing.
  227. //!
  228. //! <b>Complexity</b>: Linear to the number of elements transferred
  229. //! if constant_time_size is true. Constant-time otherwise.
  230. //!
  231. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  232. //! list. Iterators of this list and all the references are not invalidated.
  233. //!
  234. //! <b>Warning</b>: Experimental function, don't use it!
  235. slist_impl( const node_ptr & f, const node_ptr & before_l
  236. , size_type n, const value_traits &v_traits = value_traits())
  237. : data_(v_traits)
  238. {
  239. if(n){
  240. this->priv_size_traits().set_size(n);
  241. if(cache_last){
  242. this->set_last_node(before_l);
  243. }
  244. node_traits::set_next(this->get_root_node(), f);
  245. node_traits::set_next(before_l, this->get_end_node());
  246. }
  247. else{
  248. this->set_default_constructed_state();
  249. }
  250. }
  251. ///@endcond
  252. //! <b>Effects</b>: constructs an empty list.
  253. //!
  254. //! <b>Complexity</b>: Constant
  255. //!
  256. //! <b>Throws</b>: If value_traits::node_traits::node
  257. //! constructor throws (this does not happen with predefined Boost.Intrusive hooks).
  258. slist_impl()
  259. : data_(value_traits())
  260. { this->set_default_constructed_state(); }
  261. //! <b>Effects</b>: constructs an empty list.
  262. //!
  263. //! <b>Complexity</b>: Constant
  264. //!
  265. //! <b>Throws</b>: If value_traits::node_traits::node
  266. //! constructor throws (this does not happen with predefined Boost.Intrusive hooks).
  267. explicit slist_impl(const value_traits &v_traits)
  268. : data_(v_traits)
  269. { this->set_default_constructed_state(); }
  270. //! <b>Requires</b>: Dereferencing iterator must yield an lvalue of type value_type.
  271. //!
  272. //! <b>Effects</b>: Constructs a list equal to [b ,e).
  273. //!
  274. //! <b>Complexity</b>: Linear in distance(b, e). No copy constructors are called.
  275. //!
  276. //! <b>Throws</b>: If value_traits::node_traits::node
  277. //! constructor throws (this does not happen with predefined Boost.Intrusive hooks).
  278. template<class Iterator>
  279. slist_impl(Iterator b, Iterator e, const value_traits &v_traits = value_traits())
  280. : data_(v_traits)
  281. {
  282. this->set_default_constructed_state();
  283. //nothrow, no need to rollback to release elements on exception
  284. this->insert_after(this->cbefore_begin(), b, e);
  285. }
  286. //! <b>Effects</b>: Constructs a container moving resources from another container.
  287. //! Internal value traits are move constructed and
  288. //! nodes belonging to x (except the node representing the "end") are linked to *this.
  289. //!
  290. //! <b>Complexity</b>: Constant.
  291. //!
  292. //! <b>Throws</b>: If value_traits::node_traits::node's
  293. //! move constructor throws (this does not happen with predefined Boost.Intrusive hooks)
  294. //! or the move constructor of value traits throws.
  295. slist_impl(BOOST_RV_REF(slist_impl) x)
  296. : data_(::boost::move(x.priv_value_traits()))
  297. {
  298. this->set_default_constructed_state();
  299. //nothrow, no need to rollback to release elements on exception
  300. this->swap(x);
  301. }
  302. //! <b>Effects</b>: Equivalent to swap
  303. //!
  304. slist_impl& operator=(BOOST_RV_REF(slist_impl) x)
  305. { this->swap(x); return *this; }
  306. //! <b>Effects</b>: If it's a safe-mode
  307. //! or auto-unlink value, the destructor does nothing
  308. //! (ie. no code is generated). Otherwise it detaches all elements from this.
  309. //! In this case the objects in the list are not deleted (i.e. no destructors
  310. //! are called), but the hooks according to the value_traits template parameter
  311. //! are set to their default value.
  312. //!
  313. //! <b>Complexity</b>: Linear to the number of elements in the list, if
  314. //! it's a safe-mode or auto-unlink value. Otherwise constant.
  315. ~slist_impl()
  316. {
  317. if(is_safe_autounlink<ValueTraits::link_mode>::value){
  318. this->clear();
  319. node_algorithms::init(this->get_root_node());
  320. }
  321. }
  322. //! <b>Effects</b>: Erases all the elements of the container.
  323. //!
  324. //! <b>Throws</b>: Nothing.
  325. //!
  326. //! <b>Complexity</b>: Linear to the number of elements of the list.
  327. //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise.
  328. //!
  329. //! <b>Note</b>: Invalidates the iterators (but not the references) to the erased elements.
  330. void clear()
  331. {
  332. if(safemode_or_autounlink){
  333. this->clear_and_dispose(detail::null_disposer());
  334. }
  335. else{
  336. this->set_default_constructed_state();
  337. }
  338. }
  339. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  340. //!
  341. //! <b>Effects</b>: Erases all the elements of the container
  342. //! Disposer::operator()(pointer) is called for the removed elements.
  343. //!
  344. //! <b>Throws</b>: Nothing.
  345. //!
  346. //! <b>Complexity</b>: Linear to the number of elements of the list.
  347. //!
  348. //! <b>Note</b>: Invalidates the iterators to the erased elements.
  349. template <class Disposer>
  350. void clear_and_dispose(Disposer disposer)
  351. {
  352. const_iterator it(this->begin()), itend(this->end());
  353. while(it != itend){
  354. node_ptr to_erase(it.pointed_node());
  355. ++it;
  356. if(safemode_or_autounlink)
  357. node_algorithms::init(to_erase);
  358. disposer(priv_value_traits().to_value_ptr(to_erase));
  359. }
  360. this->set_default_constructed_state();
  361. }
  362. //! <b>Requires</b>: value must be an lvalue.
  363. //!
  364. //! <b>Effects</b>: Inserts the value in the front of the list.
  365. //! No copy constructors are called.
  366. //!
  367. //! <b>Throws</b>: Nothing.
  368. //!
  369. //! <b>Complexity</b>: Constant.
  370. //!
  371. //! <b>Note</b>: Does not affect the validity of iterators and references.
  372. void push_front(reference value)
  373. {
  374. node_ptr to_insert = priv_value_traits().to_node_ptr(value);
  375. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::inited(to_insert));
  376. if(cache_last){
  377. if(this->empty()){
  378. this->set_last_node(to_insert);
  379. }
  380. }
  381. node_algorithms::link_after(this->get_root_node(), to_insert);
  382. this->priv_size_traits().increment();
  383. }
  384. //! <b>Requires</b>: value must be an lvalue.
  385. //!
  386. //! <b>Effects</b>: Inserts the value in the back of the list.
  387. //! No copy constructors are called.
  388. //!
  389. //! <b>Throws</b>: Nothing.
  390. //!
  391. //! <b>Complexity</b>: Constant.
  392. //!
  393. //! <b>Note</b>: Does not affect the validity of iterators and references.
  394. //! This function is only available is cache_last<> is true.
  395. void push_back(reference value)
  396. {
  397. BOOST_STATIC_ASSERT((cache_last));
  398. node_ptr n = priv_value_traits().to_node_ptr(value);
  399. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::inited(n));
  400. node_algorithms::link_after(this->get_last_node(), n);
  401. if(cache_last){
  402. this->set_last_node(n);
  403. }
  404. this->priv_size_traits().increment();
  405. }
  406. //! <b>Effects</b>: Erases the first element of the list.
  407. //! No destructors are called.
  408. //!
  409. //! <b>Throws</b>: Nothing.
  410. //!
  411. //! <b>Complexity</b>: Constant.
  412. //!
  413. //! <b>Note</b>: Invalidates the iterators (but not the references) to the erased element.
  414. void pop_front()
  415. { return this->pop_front_and_dispose(detail::null_disposer()); }
  416. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  417. //!
  418. //! <b>Effects</b>: Erases the first element of the list.
  419. //! Disposer::operator()(pointer) is called for the removed element.
  420. //!
  421. //! <b>Throws</b>: Nothing.
  422. //!
  423. //! <b>Complexity</b>: Constant.
  424. //!
  425. //! <b>Note</b>: Invalidates the iterators to the erased element.
  426. template<class Disposer>
  427. void pop_front_and_dispose(Disposer disposer)
  428. {
  429. node_ptr to_erase = node_traits::get_next(this->get_root_node());
  430. node_algorithms::unlink_after(this->get_root_node());
  431. this->priv_size_traits().decrement();
  432. if(safemode_or_autounlink)
  433. node_algorithms::init(to_erase);
  434. disposer(priv_value_traits().to_value_ptr(to_erase));
  435. if(cache_last){
  436. if(this->empty()){
  437. this->set_last_node(this->get_root_node());
  438. }
  439. }
  440. }
  441. //! <b>Effects</b>: Returns a reference to the first element of the list.
  442. //!
  443. //! <b>Throws</b>: Nothing.
  444. //!
  445. //! <b>Complexity</b>: Constant.
  446. reference front()
  447. { return *this->priv_value_traits().to_value_ptr(node_traits::get_next(this->get_root_node())); }
  448. //! <b>Effects</b>: Returns a const_reference to the first element of the list.
  449. //!
  450. //! <b>Throws</b>: Nothing.
  451. //!
  452. //! <b>Complexity</b>: Constant.
  453. const_reference front() const
  454. { return *this->priv_value_traits().to_value_ptr(detail::uncast(node_traits::get_next(this->get_root_node()))); }
  455. //! <b>Effects</b>: Returns a reference to the last element of the list.
  456. //!
  457. //! <b>Throws</b>: Nothing.
  458. //!
  459. //! <b>Complexity</b>: Constant.
  460. //!
  461. //! <b>Note</b>: Does not affect the validity of iterators and references.
  462. //! This function is only available is cache_last<> is true.
  463. reference back()
  464. {
  465. BOOST_STATIC_ASSERT((cache_last));
  466. return *this->priv_value_traits().to_value_ptr(this->get_last_node());
  467. }
  468. //! <b>Effects</b>: Returns a const_reference to the last element of the list.
  469. //!
  470. //! <b>Throws</b>: Nothing.
  471. //!
  472. //! <b>Complexity</b>: Constant.
  473. //!
  474. //! <b>Note</b>: Does not affect the validity of iterators and references.
  475. //! This function is only available is cache_last<> is true.
  476. const_reference back() const
  477. {
  478. BOOST_STATIC_ASSERT((cache_last));
  479. return *this->priv_value_traits().to_value_ptr(this->get_last_node());
  480. }
  481. //! <b>Effects</b>: Returns an iterator to the first element contained in the list.
  482. //!
  483. //! <b>Throws</b>: Nothing.
  484. //!
  485. //! <b>Complexity</b>: Constant.
  486. iterator begin()
  487. { return iterator (node_traits::get_next(this->get_root_node()), this->priv_value_traits_ptr()); }
  488. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the list.
  489. //!
  490. //! <b>Throws</b>: Nothing.
  491. //!
  492. //! <b>Complexity</b>: Constant.
  493. const_iterator begin() const
  494. { return const_iterator (node_traits::get_next(this->get_root_node()), this->priv_value_traits_ptr()); }
  495. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the list.
  496. //!
  497. //! <b>Throws</b>: Nothing.
  498. //!
  499. //! <b>Complexity</b>: Constant.
  500. const_iterator cbegin() const
  501. { return const_iterator(node_traits::get_next(this->get_root_node()), this->priv_value_traits_ptr()); }
  502. //! <b>Effects</b>: Returns an iterator to the end of the list.
  503. //!
  504. //! <b>Throws</b>: Nothing.
  505. //!
  506. //! <b>Complexity</b>: Constant.
  507. iterator end()
  508. { return iterator(this->get_end_node(), this->priv_value_traits_ptr()); }
  509. //! <b>Effects</b>: Returns a const_iterator to the end of the list.
  510. //!
  511. //! <b>Throws</b>: Nothing.
  512. //!
  513. //! <b>Complexity</b>: Constant.
  514. const_iterator end() const
  515. { return const_iterator(detail::uncast(this->get_end_node()), this->priv_value_traits_ptr()); }
  516. //! <b>Effects</b>: Returns a const_iterator to the end of the list.
  517. //!
  518. //! <b>Throws</b>: Nothing.
  519. //!
  520. //! <b>Complexity</b>: Constant.
  521. const_iterator cend() const
  522. { return this->end(); }
  523. //! <b>Effects</b>: Returns an iterator that points to a position
  524. //! before the first element. Equivalent to "end()"
  525. //!
  526. //! <b>Throws</b>: Nothing.
  527. //!
  528. //! <b>Complexity</b>: Constant.
  529. iterator before_begin()
  530. { return iterator(this->get_root_node(), this->priv_value_traits_ptr()); }
  531. //! <b>Effects</b>: Returns an iterator that points to a position
  532. //! before the first element. Equivalent to "end()"
  533. //!
  534. //! <b>Throws</b>: Nothing.
  535. //!
  536. //! <b>Complexity</b>: Constant.
  537. const_iterator before_begin() const
  538. { return const_iterator(detail::uncast(this->get_root_node()), this->priv_value_traits_ptr()); }
  539. //! <b>Effects</b>: Returns an iterator that points to a position
  540. //! before the first element. Equivalent to "end()"
  541. //!
  542. //! <b>Throws</b>: Nothing.
  543. //!
  544. //! <b>Complexity</b>: Constant.
  545. const_iterator cbefore_begin() const
  546. { return this->before_begin(); }
  547. //! <b>Effects</b>: Returns an iterator to the last element contained in the list.
  548. //!
  549. //! <b>Throws</b>: Nothing.
  550. //!
  551. //! <b>Complexity</b>: Constant.
  552. //!
  553. //! <b>Note</b>: This function is present only if cached_last<> option is true.
  554. iterator last()
  555. {
  556. //This function shall not be used if cache_last is not true
  557. BOOST_INTRUSIVE_INVARIANT_ASSERT(cache_last);
  558. return iterator (this->get_last_node(), this->priv_value_traits_ptr());
  559. }
  560. //! <b>Effects</b>: Returns a const_iterator to the last element contained in the list.
  561. //!
  562. //! <b>Throws</b>: Nothing.
  563. //!
  564. //! <b>Complexity</b>: Constant.
  565. //!
  566. //! <b>Note</b>: This function is present only if cached_last<> option is true.
  567. const_iterator last() const
  568. {
  569. //This function shall not be used if cache_last is not true
  570. BOOST_INTRUSIVE_INVARIANT_ASSERT(cache_last);
  571. return const_iterator (this->get_last_node(), this->priv_value_traits_ptr());
  572. }
  573. //! <b>Effects</b>: Returns a const_iterator to the last element contained in the list.
  574. //!
  575. //! <b>Throws</b>: Nothing.
  576. //!
  577. //! <b>Complexity</b>: Constant.
  578. //!
  579. //! <b>Note</b>: This function is present only if cached_last<> option is true.
  580. const_iterator clast() const
  581. { return const_iterator(this->get_last_node(), this->priv_value_traits_ptr()); }
  582. //! <b>Precondition</b>: end_iterator must be a valid end iterator
  583. //! of slist.
  584. //!
  585. //! <b>Effects</b>: Returns a const reference to the slist associated to the end iterator
  586. //!
  587. //! <b>Throws</b>: Nothing.
  588. //!
  589. //! <b>Complexity</b>: Constant.
  590. static slist_impl &container_from_end_iterator(iterator end_iterator)
  591. { return slist_impl::priv_container_from_end_iterator(end_iterator); }
  592. //! <b>Precondition</b>: end_iterator must be a valid end const_iterator
  593. //! of slist.
  594. //!
  595. //! <b>Effects</b>: Returns a const reference to the slist associated to the end iterator
  596. //!
  597. //! <b>Throws</b>: Nothing.
  598. //!
  599. //! <b>Complexity</b>: Constant.
  600. static const slist_impl &container_from_end_iterator(const_iterator end_iterator)
  601. { return slist_impl::priv_container_from_end_iterator(end_iterator); }
  602. //! <b>Effects</b>: Returns the number of the elements contained in the list.
  603. //!
  604. //! <b>Throws</b>: Nothing.
  605. //!
  606. //! <b>Complexity</b>: Linear to the number of elements contained in the list.
  607. //! if constant_time_size is false. Constant time otherwise.
  608. //!
  609. //! <b>Note</b>: Does not affect the validity of iterators and references.
  610. size_type size() const
  611. {
  612. if(constant_time_size)
  613. return this->priv_size_traits().get_size();
  614. else
  615. return node_algorithms::count(this->get_root_node()) - 1;
  616. }
  617. //! <b>Effects</b>: Returns true if the list contains no elements.
  618. //!
  619. //! <b>Throws</b>: Nothing.
  620. //!
  621. //! <b>Complexity</b>: Constant.
  622. //!
  623. //! <b>Note</b>: Does not affect the validity of iterators and references.
  624. bool empty() const
  625. { return node_algorithms::unique(this->get_root_node()); }
  626. //! <b>Effects</b>: Swaps the elements of x and *this.
  627. //!
  628. //! <b>Throws</b>: Nothing.
  629. //!
  630. //! <b>Complexity</b>: Linear to the number of elements of both lists.
  631. //! Constant-time if linear<> and/or cache_last<> options are used.
  632. //!
  633. //! <b>Note</b>: Does not affect the validity of iterators and references.
  634. void swap(slist_impl& other)
  635. {
  636. if(cache_last){
  637. priv_swap_cache_last(this, &other);
  638. }
  639. else{
  640. this->priv_swap_lists(this->get_root_node(), other.get_root_node(), detail::bool_<linear>());
  641. }
  642. this->priv_size_traits().swap(other.priv_size_traits());
  643. }
  644. //! <b>Effects</b>: Moves backwards all the elements, so that the first
  645. //! element becomes the second, the second becomes the third...
  646. //! the last element becomes the first one.
  647. //!
  648. //! <b>Throws</b>: Nothing.
  649. //!
  650. //! <b>Complexity</b>: Linear to the number of elements plus the number shifts.
  651. //!
  652. //! <b>Note</b>: Iterators Does not affect the validity of iterators and references.
  653. void shift_backwards(size_type n = 1)
  654. { this->priv_shift_backwards(n, detail::bool_<linear>()); }
  655. //! <b>Effects</b>: Moves forward all the elements, so that the second
  656. //! element becomes the first, the third becomes the second...
  657. //! the first element becomes the last one.
  658. //!
  659. //! <b>Throws</b>: Nothing.
  660. //!
  661. //! <b>Complexity</b>: Linear to the number of elements plus the number shifts.
  662. //!
  663. //! <b>Note</b>: Does not affect the validity of iterators and references.
  664. void shift_forward(size_type n = 1)
  665. { this->priv_shift_forward(n, detail::bool_<linear>()); }
  666. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  667. //! Cloner should yield to nodes equivalent to the original nodes.
  668. //!
  669. //! <b>Effects</b>: Erases all the elements from *this
  670. //! calling Disposer::operator()(pointer), clones all the
  671. //! elements from src calling Cloner::operator()(const_reference )
  672. //! and inserts them on *this.
  673. //!
  674. //! If cloner throws, all cloned elements are unlinked and disposed
  675. //! calling Disposer::operator()(pointer).
  676. //!
  677. //! <b>Complexity</b>: Linear to erased plus inserted elements.
  678. //!
  679. //! <b>Throws</b>: If cloner throws.
  680. template <class Cloner, class Disposer>
  681. void clone_from(const slist_impl &src, Cloner cloner, Disposer disposer)
  682. {
  683. this->clear_and_dispose(disposer);
  684. detail::exception_disposer<slist_impl, Disposer>
  685. rollback(*this, disposer);
  686. const_iterator prev(this->cbefore_begin());
  687. const_iterator b(src.begin()), e(src.end());
  688. for(; b != e; ++b){
  689. prev = this->insert_after(prev, *cloner(*b));
  690. }
  691. rollback.release();
  692. }
  693. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  694. //! Cloner should yield to nodes equivalent to the original nodes.
  695. //!
  696. //! <b>Effects</b>: Erases all the elements from *this
  697. //! calling Disposer::operator()(pointer), clones all the
  698. //! elements from src calling Cloner::operator()(reference)
  699. //! and inserts them on *this.
  700. //!
  701. //! If cloner throws, all cloned elements are unlinked and disposed
  702. //! calling Disposer::operator()(pointer).
  703. //!
  704. //! <b>Complexity</b>: Linear to erased plus inserted elements.
  705. //!
  706. //! <b>Throws</b>: If cloner throws.
  707. template <class Cloner, class Disposer>
  708. void clone_from(BOOST_RV_REF(slist_impl) src, Cloner cloner, Disposer disposer)
  709. {
  710. this->clear_and_dispose(disposer);
  711. detail::exception_disposer<slist_impl, Disposer>
  712. rollback(*this, disposer);
  713. iterator prev(this->cbefore_begin());
  714. iterator b(src.begin()), e(src.end());
  715. for(; b != e; ++b){
  716. prev = this->insert_after(prev, *cloner(*b));
  717. }
  718. rollback.release();
  719. }
  720. //! <b>Requires</b>: value must be an lvalue and prev_p must point to an element
  721. //! contained by the list or to end().
  722. //!
  723. //! <b>Effects</b>: Inserts the value after the position pointed by prev_p.
  724. //! No copy constructor is called.
  725. //!
  726. //! <b>Returns</b>: An iterator to the inserted element.
  727. //!
  728. //! <b>Throws</b>: Nothing.
  729. //!
  730. //! <b>Complexity</b>: Constant.
  731. //!
  732. //! <b>Note</b>: Does not affect the validity of iterators and references.
  733. iterator insert_after(const_iterator prev_p, reference value)
  734. {
  735. node_ptr n = priv_value_traits().to_node_ptr(value);
  736. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::inited(n));
  737. node_ptr prev_n(prev_p.pointed_node());
  738. node_algorithms::link_after(prev_n, n);
  739. if(cache_last && (this->get_last_node() == prev_n)){
  740. this->set_last_node(n);
  741. }
  742. this->priv_size_traits().increment();
  743. return iterator (n, this->priv_value_traits_ptr());
  744. }
  745. //! <b>Requires</b>: Dereferencing iterator must yield
  746. //! an lvalue of type value_type and prev_p must point to an element
  747. //! contained by the list or to the end node.
  748. //!
  749. //! <b>Effects</b>: Inserts the [f, l)
  750. //! after the position prev_p.
  751. //!
  752. //! <b>Throws</b>: Nothing.
  753. //!
  754. //! <b>Complexity</b>: Linear to the number of elements inserted.
  755. //!
  756. //! <b>Note</b>: Does not affect the validity of iterators and references.
  757. template<class Iterator>
  758. void insert_after(const_iterator prev_p, Iterator f, Iterator l)
  759. {
  760. //Insert first nodes avoiding cache and size checks
  761. size_type count = 0;
  762. node_ptr prev_n(prev_p.pointed_node());
  763. for (; f != l; ++f, ++count){
  764. const node_ptr n = priv_value_traits().to_node_ptr(*f);
  765. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::inited(n));
  766. node_algorithms::link_after(prev_n, n);
  767. prev_n = n;
  768. }
  769. //Now fix special cases if needed
  770. if(cache_last && (this->get_last_node() == prev_p.pointed_node())){
  771. this->set_last_node(prev_n);
  772. }
  773. if(constant_time_size){
  774. this->priv_size_traits().increase(count);
  775. }
  776. }
  777. //! <b>Requires</b>: value must be an lvalue and p must point to an element
  778. //! contained by the list or to end().
  779. //!
  780. //! <b>Effects</b>: Inserts the value before the position pointed by p.
  781. //! No copy constructor is called.
  782. //!
  783. //! <b>Throws</b>: Nothing.
  784. //!
  785. //! <b>Complexity</b>: Linear to the number of elements before p.
  786. //! Constant-time if cache_last<> is true and p == end().
  787. //!
  788. //! <b>Note</b>: Does not affect the validity of iterators and references.
  789. iterator insert(const_iterator p, reference value)
  790. { return this->insert_after(this->previous(p), value); }
  791. //! <b>Requires</b>: Dereferencing iterator must yield
  792. //! an lvalue of type value_type and p must point to an element
  793. //! contained by the list or to the end node.
  794. //!
  795. //! <b>Effects</b>: Inserts the pointed by b and e
  796. //! before the position p. No copy constructors are called.
  797. //!
  798. //! <b>Throws</b>: Nothing.
  799. //!
  800. //! <b>Complexity</b>: Linear to the number of elements inserted plus linear
  801. //! to the elements before b.
  802. //! Linear to the number of elements to insert if cache_last<> option is true and p == end().
  803. //!
  804. //! <b>Note</b>: Does not affect the validity of iterators and references.
  805. template<class Iterator>
  806. void insert(const_iterator p, Iterator b, Iterator e)
  807. { return this->insert_after(this->previous(p), b, e); }
  808. //! <b>Effects</b>: Erases the element after the element pointed by prev of
  809. //! the list. No destructors are called.
  810. //!
  811. //! <b>Returns</b>: the first element remaining beyond the removed elements,
  812. //! or end() if no such element exists.
  813. //!
  814. //! <b>Throws</b>: Nothing.
  815. //!
  816. //! <b>Complexity</b>: Constant.
  817. //!
  818. //! <b>Note</b>: Invalidates the iterators (but not the references) to the
  819. //! erased element.
  820. iterator erase_after(const_iterator prev)
  821. { return this->erase_after_and_dispose(prev, detail::null_disposer()); }
  822. //! <b>Effects</b>: Erases the range (before_f, l) from
  823. //! the list. No destructors are called.
  824. //!
  825. //! <b>Returns</b>: the first element remaining beyond the removed elements,
  826. //! or end() if no such element exists.
  827. //!
  828. //! <b>Throws</b>: Nothing.
  829. //!
  830. //! <b>Complexity</b>: Linear to the number of erased elements if it's a safe-mode
  831. //! , auto-unlink value or constant-time size is activated. Constant time otherwise.
  832. //!
  833. //! <b>Note</b>: Invalidates the iterators (but not the references) to the
  834. //! erased element.
  835. iterator erase_after(const_iterator before_f, const_iterator l)
  836. {
  837. if(safemode_or_autounlink || constant_time_size){
  838. return this->erase_after_and_dispose(before_f, l, detail::null_disposer());
  839. }
  840. else{
  841. const node_ptr bfp = before_f.pointed_node();
  842. const node_ptr lp = l.pointed_node();
  843. if(cache_last){
  844. if(lp == this->get_end_node()){
  845. this->set_last_node(bfp);
  846. }
  847. }
  848. node_algorithms::unlink_after(bfp, lp);
  849. return l.unconst();
  850. }
  851. }
  852. //! <b>Effects</b>: Erases the range (before_f, l) from
  853. //! the list. n must be distance(before_f, l) - 1.
  854. //! No destructors are called.
  855. //!
  856. //! <b>Returns</b>: the first element remaining beyond the removed elements,
  857. //! or end() if no such element exists.
  858. //!
  859. //! <b>Throws</b>: Nothing.
  860. //!
  861. //! <b>Complexity</b>: constant-time if link_mode is normal_link.
  862. //! Linear to the elements (l - before_f) otherwise.
  863. //!
  864. //! <b>Note</b>: Invalidates the iterators (but not the references) to the
  865. //! erased element.
  866. iterator erase_after(const_iterator before_f, const_iterator l, size_type n)
  867. {
  868. BOOST_INTRUSIVE_INVARIANT_ASSERT(node_algorithms::distance((++const_iterator(before_f)).pointed_node(), l.pointed_node()) == n);
  869. if(safemode_or_autounlink){
  870. return this->erase_after(before_f, l);
  871. }
  872. else{
  873. const node_ptr bfp = before_f.pointed_node();
  874. const node_ptr lp = l.pointed_node();
  875. if(cache_last){
  876. if((lp == this->get_end_node())){
  877. this->set_last_node(bfp);
  878. }
  879. }
  880. node_algorithms::unlink_after(bfp, lp);
  881. if(constant_time_size){
  882. this->priv_size_traits().decrease(n);
  883. }
  884. return l.unconst();
  885. }
  886. }
  887. //! <b>Effects</b>: Erases the element pointed by i of the list.
  888. //! No destructors are called.
  889. //!
  890. //! <b>Returns</b>: the first element remaining beyond the removed element,
  891. //! or end() if no such element exists.
  892. //!
  893. //! <b>Throws</b>: Nothing.
  894. //!
  895. //! <b>Complexity</b>: Linear to the elements before i.
  896. //!
  897. //! <b>Note</b>: Invalidates the iterators (but not the references) to the
  898. //! erased element.
  899. iterator erase(const_iterator i)
  900. { return this->erase_after(this->previous(i)); }
  901. //! <b>Requires</b>: f and l must be valid iterator to elements in *this.
  902. //!
  903. //! <b>Effects</b>: Erases the range pointed by b and e.
  904. //! No destructors are called.
  905. //!
  906. //! <b>Returns</b>: the first element remaining beyond the removed elements,
  907. //! or end() if no such element exists.
  908. //!
  909. //! <b>Throws</b>: Nothing.
  910. //!
  911. //! <b>Complexity</b>: Linear to the elements before l.
  912. //!
  913. //! <b>Note</b>: Invalidates the iterators (but not the references) to the
  914. //! erased elements.
  915. iterator erase(const_iterator f, const_iterator l)
  916. { return this->erase_after(this->previous(f), l); }
  917. //! <b>Effects</b>: Erases the range [f, l) from
  918. //! the list. n must be distance(f, l).
  919. //! No destructors are called.
  920. //!
  921. //! <b>Returns</b>: the first element remaining beyond the removed elements,
  922. //! or end() if no such element exists.
  923. //!
  924. //! <b>Throws</b>: Nothing.
  925. //!
  926. //! <b>Complexity</b>: linear to the elements before f if link_mode is normal_link
  927. //! and constant_time_size is activated. Linear to the elements before l otherwise.
  928. //!
  929. //! <b>Note</b>: Invalidates the iterators (but not the references) to the
  930. //! erased element.
  931. iterator erase(const_iterator f, const_iterator l, size_type n)
  932. { return this->erase_after(this->previous(f), l, n); }
  933. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  934. //!
  935. //! <b>Effects</b>: Erases the element after the element pointed by prev of
  936. //! the list.
  937. //! Disposer::operator()(pointer) is called for the removed element.
  938. //!
  939. //! <b>Returns</b>: the first element remaining beyond the removed elements,
  940. //! or end() if no such element exists.
  941. //!
  942. //! <b>Throws</b>: Nothing.
  943. //!
  944. //! <b>Complexity</b>: Constant.
  945. //!
  946. //! <b>Note</b>: Invalidates the iterators to the erased element.
  947. template<class Disposer>
  948. iterator erase_after_and_dispose(const_iterator prev, Disposer disposer)
  949. {
  950. const_iterator it(prev);
  951. ++it;
  952. node_ptr to_erase(it.pointed_node());
  953. ++it;
  954. node_ptr prev_n(prev.pointed_node());
  955. node_algorithms::unlink_after(prev_n);
  956. if(cache_last && (to_erase == this->get_last_node())){
  957. this->set_last_node(prev_n);
  958. }
  959. if(safemode_or_autounlink)
  960. node_algorithms::init(to_erase);
  961. disposer(priv_value_traits().to_value_ptr(to_erase));
  962. this->priv_size_traits().decrement();
  963. return it.unconst();
  964. }
  965. /// @cond
  966. static iterator s_insert_after(const_iterator const prev_p, reference value)
  967. {
  968. BOOST_STATIC_ASSERT(((!cache_last)&&(!constant_time_size)&&(!stateful_value_traits)));
  969. node_ptr const n = value_traits::to_node_ptr(value);
  970. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::inited(n));
  971. node_algorithms::link_after(prev_p.pointed_node(), n);
  972. return iterator (n, const_value_traits_ptr());
  973. }
  974. template<class Disposer>
  975. static iterator s_erase_after_and_dispose(const_iterator prev, Disposer disposer)
  976. {
  977. BOOST_STATIC_ASSERT(((!cache_last)&&(!constant_time_size)&&(!stateful_value_traits)));
  978. const_iterator it(prev);
  979. ++it;
  980. node_ptr to_erase(it.pointed_node());
  981. ++it;
  982. node_ptr prev_n(prev.pointed_node());
  983. node_algorithms::unlink_after(prev_n);
  984. if(safemode_or_autounlink)
  985. node_algorithms::init(to_erase);
  986. disposer(value_traits::to_value_ptr(to_erase));
  987. return it.unconst();
  988. }
  989. template<class Disposer>
  990. static iterator s_erase_after_and_dispose(const_iterator before_f, const_iterator l, Disposer disposer)
  991. {
  992. BOOST_STATIC_ASSERT(((!cache_last)&&(!constant_time_size)&&(!stateful_value_traits)));
  993. node_ptr bfp(before_f.pointed_node()), lp(l.pointed_node());
  994. node_ptr fp(node_traits::get_next(bfp));
  995. node_algorithms::unlink_after(bfp, lp);
  996. while(fp != lp){
  997. node_ptr to_erase(fp);
  998. fp = node_traits::get_next(fp);
  999. if(safemode_or_autounlink)
  1000. node_algorithms::init(to_erase);
  1001. disposer(value_traits::to_value_ptr(to_erase));
  1002. }
  1003. return l.unconst();
  1004. }
  1005. static iterator s_erase_after(const_iterator prev)
  1006. { return s_erase_after_and_dispose(prev, detail::null_disposer()); }
  1007. /// @endcond
  1008. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1009. //!
  1010. //! <b>Effects</b>: Erases the range (before_f, l) from
  1011. //! the list.
  1012. //! Disposer::operator()(pointer) is called for the removed elements.
  1013. //!
  1014. //! <b>Returns</b>: the first element remaining beyond the removed elements,
  1015. //! or end() if no such element exists.
  1016. //!
  1017. //! <b>Throws</b>: Nothing.
  1018. //!
  1019. //! <b>Complexity</b>: Linear to the elements (l - before_f + 1).
  1020. //!
  1021. //! <b>Note</b>: Invalidates the iterators to the erased element.
  1022. template<class Disposer>
  1023. iterator erase_after_and_dispose(const_iterator before_f, const_iterator l, Disposer disposer)
  1024. {
  1025. node_ptr bfp(before_f.pointed_node()), lp(l.pointed_node());
  1026. node_ptr fp(node_traits::get_next(bfp));
  1027. node_algorithms::unlink_after(bfp, lp);
  1028. while(fp != lp){
  1029. node_ptr to_erase(fp);
  1030. fp = node_traits::get_next(fp);
  1031. if(safemode_or_autounlink)
  1032. node_algorithms::init(to_erase);
  1033. disposer(priv_value_traits().to_value_ptr(to_erase));
  1034. this->priv_size_traits().decrement();
  1035. }
  1036. if(cache_last && (node_traits::get_next(bfp) == this->get_end_node())){
  1037. this->set_last_node(bfp);
  1038. }
  1039. return l.unconst();
  1040. }
  1041. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1042. //!
  1043. //! <b>Effects</b>: Erases the element pointed by i of the list.
  1044. //! No destructors are called.
  1045. //! Disposer::operator()(pointer) is called for the removed element.
  1046. //!
  1047. //! <b>Returns</b>: the first element remaining beyond the removed element,
  1048. //! or end() if no such element exists.
  1049. //!
  1050. //! <b>Throws</b>: Nothing.
  1051. //!
  1052. //! <b>Complexity</b>: Linear to the elements before i.
  1053. //!
  1054. //! <b>Note</b>: Invalidates the iterators (but not the references) to the
  1055. //! erased element.
  1056. template<class Disposer>
  1057. iterator erase_and_dispose(const_iterator i, Disposer disposer)
  1058. { return this->erase_after_and_dispose(this->previous(i), disposer); }
  1059. #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1060. template<class Disposer>
  1061. iterator erase_and_dispose(iterator i, Disposer disposer)
  1062. { return this->erase_and_dispose(const_iterator(i), disposer); }
  1063. #endif
  1064. //! <b>Requires</b>: f and l must be valid iterator to elements in *this.
  1065. //! Disposer::operator()(pointer) shouldn't throw.
  1066. //!
  1067. //! <b>Effects</b>: Erases the range pointed by b and e.
  1068. //! No destructors are called.
  1069. //! Disposer::operator()(pointer) is called for the removed elements.
  1070. //!
  1071. //! <b>Returns</b>: the first element remaining beyond the removed elements,
  1072. //! or end() if no such element exists.
  1073. //!
  1074. //! <b>Throws</b>: Nothing.
  1075. //!
  1076. //! <b>Complexity</b>: Linear to the number of erased elements plus linear
  1077. //! to the elements before f.
  1078. //!
  1079. //! <b>Note</b>: Invalidates the iterators (but not the references) to the
  1080. //! erased elements.
  1081. template<class Disposer>
  1082. iterator erase_and_dispose(const_iterator f, const_iterator l, Disposer disposer)
  1083. { return this->erase_after_and_dispose(this->previous(f), l, disposer); }
  1084. //! <b>Requires</b>: Dereferencing iterator must yield
  1085. //! an lvalue of type value_type.
  1086. //!
  1087. //! <b>Effects</b>: Clears the list and inserts the range pointed by b and e.
  1088. //! No destructors or copy constructors are called.
  1089. //!
  1090. //! <b>Throws</b>: Nothing.
  1091. //!
  1092. //! <b>Complexity</b>: Linear to the number of elements inserted plus
  1093. //! linear to the elements contained in the list if it's a safe-mode
  1094. //! or auto-unlink value.
  1095. //! Linear to the number of elements inserted in the list otherwise.
  1096. //!
  1097. //! <b>Note</b>: Invalidates the iterators (but not the references)
  1098. //! to the erased elements.
  1099. template<class Iterator>
  1100. void assign(Iterator b, Iterator e)
  1101. {
  1102. this->clear();
  1103. this->insert_after(this->cbefore_begin(), b, e);
  1104. }
  1105. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1106. //!
  1107. //! <b>Requires</b>: Dereferencing iterator must yield
  1108. //! an lvalue of type value_type.
  1109. //!
  1110. //! <b>Effects</b>: Clears the list and inserts the range pointed by b and e.
  1111. //! No destructors or copy constructors are called.
  1112. //! Disposer::operator()(pointer) is called for the removed elements.
  1113. //!
  1114. //! <b>Throws</b>: Nothing.
  1115. //!
  1116. //! <b>Complexity</b>: Linear to the number of elements inserted plus
  1117. //! linear to the elements contained in the list.
  1118. //!
  1119. //! <b>Note</b>: Invalidates the iterators (but not the references)
  1120. //! to the erased elements.
  1121. template<class Iterator, class Disposer>
  1122. void dispose_and_assign(Disposer disposer, Iterator b, Iterator e)
  1123. {
  1124. this->clear_and_dispose(disposer);
  1125. this->insert_after(this->cbefore_begin(), b, e, disposer);
  1126. }
  1127. //! <b>Requires</b>: prev must point to an element contained by this list or
  1128. //! to the before_begin() element
  1129. //!
  1130. //! <b>Effects</b>: Transfers all the elements of list x to this list, after the
  1131. //! the element pointed by prev. No destructors or copy constructors are called.
  1132. //!
  1133. //! <b>Returns</b>: Nothing.
  1134. //!
  1135. //! <b>Throws</b>: Nothing.
  1136. //!
  1137. //! <b>Complexity</b>: In general, linear to the elements contained in x.
  1138. //! Constant-time if cache_last<> option is true and also constant-time if
  1139. //! linear<> option is true "this" is empty and "l" is not used.
  1140. //!
  1141. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1142. //! list. Iterators of this list and all the references are not invalidated.
  1143. //!
  1144. //! <b>Additional note</b>: If the optional parameter "l" is provided, it will be
  1145. //! assigned to the last spliced element or prev if x is empty.
  1146. //! This iterator can be used as new "prev" iterator for a new splice_after call.
  1147. //! that will splice new values after the previously spliced values.
  1148. void splice_after(const_iterator prev, slist_impl &x, const_iterator *l = 0)
  1149. {
  1150. if(x.empty()){
  1151. if(l) *l = prev;
  1152. }
  1153. else if(linear && this->empty()){
  1154. this->swap(x);
  1155. if(l) *l = this->previous(this->cend());
  1156. }
  1157. else{
  1158. const_iterator last_x(x.previous(x.end())); //constant time if cache_last is active
  1159. node_ptr prev_n(prev.pointed_node());
  1160. node_ptr last_x_n(last_x.pointed_node());
  1161. if(cache_last){
  1162. x.set_last_node(x.get_root_node());
  1163. if(node_traits::get_next(prev_n) == this->get_end_node()){
  1164. this->set_last_node(last_x_n);
  1165. }
  1166. }
  1167. node_algorithms::transfer_after( prev_n, x.before_begin().pointed_node(), last_x_n);
  1168. this->priv_size_traits().increase(x.priv_size_traits().get_size());
  1169. x.priv_size_traits().set_size(size_type(0));
  1170. if(l) *l = last_x;
  1171. }
  1172. }
  1173. //! <b>Requires</b>: prev must point to an element contained by this list or
  1174. //! to the before_begin() element. prev_ele must point to an element contained in list
  1175. //! x or must be x.before_begin().
  1176. //!
  1177. //! <b>Effects</b>: Transfers the element after prev_ele, from list x to this list,
  1178. //! after the element pointed by prev. No destructors or copy constructors are called.
  1179. //!
  1180. //! <b>Throws</b>: Nothing.
  1181. //!
  1182. //! <b>Complexity</b>: Constant.
  1183. //!
  1184. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1185. //! list. Iterators of this list and all the references are not invalidated.
  1186. void splice_after(const_iterator prev_pos, slist_impl &x, const_iterator prev_ele)
  1187. {
  1188. const_iterator elem = prev_ele;
  1189. this->splice_after(prev_pos, x, prev_ele, ++elem, 1);
  1190. }
  1191. //! <b>Requires</b>: prev_pos must be a dereferenceable iterator in *this or be
  1192. //! before_begin(), and before_f and before_l belong to x and
  1193. //! ++before_f != x.end() && before_l != x.end().
  1194. //!
  1195. //! <b>Effects</b>: Transfers the range (before_f, before_l] from list x to this
  1196. //! list, after the element pointed by prev_pos.
  1197. //! No destructors or copy constructors are called.
  1198. //!
  1199. //! <b>Throws</b>: Nothing.
  1200. //!
  1201. //! <b>Complexity</b>: Linear to the number of elements transferred
  1202. //! if constant_time_size is true. Constant-time otherwise.
  1203. //!
  1204. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1205. //! list. Iterators of this list and all the references are not invalidated.
  1206. void splice_after(const_iterator prev_pos, slist_impl &x, const_iterator before_f, const_iterator before_l)
  1207. {
  1208. if(constant_time_size)
  1209. this->splice_after(prev_pos, x, before_f, before_l, node_algorithms::distance(before_f.pointed_node(), before_l.pointed_node()));
  1210. else
  1211. this->priv_splice_after
  1212. (prev_pos.pointed_node(), x, before_f.pointed_node(), before_l.pointed_node());
  1213. }
  1214. //! <b>Requires</b>: prev_pos must be a dereferenceable iterator in *this or be
  1215. //! before_begin(), and before_f and before_l belong to x and
  1216. //! ++before_f != x.end() && before_l != x.end() and
  1217. //! n == distance(before_f, before_l).
  1218. //!
  1219. //! <b>Effects</b>: Transfers the range (before_f, before_l] from list x to this
  1220. //! list, after the element pointed by p. No destructors or copy constructors are called.
  1221. //!
  1222. //! <b>Throws</b>: Nothing.
  1223. //!
  1224. //! <b>Complexity</b>: Constant time.
  1225. //!
  1226. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1227. //! list. Iterators of this list and all the references are not invalidated.
  1228. void splice_after(const_iterator prev_pos, slist_impl &x, const_iterator before_f, const_iterator before_l, size_type n)
  1229. {
  1230. BOOST_INTRUSIVE_INVARIANT_ASSERT(node_algorithms::distance(before_f.pointed_node(), before_l.pointed_node()) == n);
  1231. this->priv_splice_after
  1232. (prev_pos.pointed_node(), x, before_f.pointed_node(), before_l.pointed_node());
  1233. if(constant_time_size){
  1234. this->priv_size_traits().increase(n);
  1235. x.priv_size_traits().decrease(n);
  1236. }
  1237. }
  1238. //! <b>Requires</b>: it is an iterator to an element in *this.
  1239. //!
  1240. //! <b>Effects</b>: Transfers all the elements of list x to this list, before the
  1241. //! the element pointed by it. No destructors or copy constructors are called.
  1242. //!
  1243. //! <b>Returns</b>: Nothing.
  1244. //!
  1245. //! <b>Throws</b>: Nothing.
  1246. //!
  1247. //! <b>Complexity</b>: Linear to the elements contained in x plus linear to
  1248. //! the elements before it.
  1249. //! Linear to the elements before it if cache_last<> option is true.
  1250. //! Constant-time if cache_last<> option is true and it == end().
  1251. //!
  1252. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1253. //! list. Iterators of this list and all the references are not invalidated.
  1254. //!
  1255. //! <b>Additional note</b>: If the optional parameter "l" is provided, it will be
  1256. //! assigned to the last spliced element or prev if x is empty.
  1257. //! This iterator can be used as new "prev" iterator for a new splice_after call.
  1258. //! that will splice new values after the previously spliced values.
  1259. void splice(const_iterator it, slist_impl &x, const_iterator *l = 0)
  1260. { this->splice_after(this->previous(it), x, l); }
  1261. //! <b>Requires</b>: it p must be a valid iterator of *this.
  1262. //! elem must point to an element contained in list
  1263. //! x.
  1264. //!
  1265. //! <b>Effects</b>: Transfers the element elem, from list x to this list,
  1266. //! before the element pointed by pos. No destructors or copy constructors are called.
  1267. //!
  1268. //! <b>Throws</b>: Nothing.
  1269. //!
  1270. //! <b>Complexity</b>: Linear to the elements before pos and before elem.
  1271. //! Linear to the elements before elem if cache_last<> option is true and pos == end().
  1272. //!
  1273. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1274. //! list. Iterators of this list and all the references are not invalidated.
  1275. void splice(const_iterator pos, slist_impl &x, const_iterator elem)
  1276. { return this->splice_after(this->previous(pos), x, x.previous(elem)); }
  1277. //! <b>Requires</b>: pos must be a dereferenceable iterator in *this
  1278. //! and f and f belong to x and f and f a valid range on x.
  1279. //!
  1280. //! <b>Effects</b>: Transfers the range [f, l) from list x to this
  1281. //! list, before the element pointed by pos.
  1282. //! No destructors or copy constructors are called.
  1283. //!
  1284. //! <b>Throws</b>: Nothing.
  1285. //!
  1286. //! <b>Complexity</b>: Linear to the sum of elements before pos, f, and l
  1287. //! plus linear to the number of elements transferred if constant_time_size is true.
  1288. //! Linear to the sum of elements before f, and l
  1289. //! plus linear to the number of elements transferred if constant_time_size is true
  1290. //! if cache_last<> is true and pos == end()
  1291. //!
  1292. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1293. //! list. Iterators of this list and all the references are not invalidated.
  1294. void splice(const_iterator pos, slist_impl &x, const_iterator f, const_iterator l)
  1295. { return this->splice_after(this->previous(pos), x, x.previous(f), x.previous(l)); }
  1296. //! <b>Requires</b>: pos must be a dereferenceable iterator in *this
  1297. //! and f and l belong to x and f and l a valid range on x.
  1298. //! n == distance(f, l).
  1299. //!
  1300. //! <b>Effects</b>: Transfers the range [f, l) from list x to this
  1301. //! list, before the element pointed by pos.
  1302. //! No destructors or copy constructors are called.
  1303. //!
  1304. //! <b>Throws</b>: Nothing.
  1305. //!
  1306. //! <b>Complexity</b>: Linear to the sum of elements before pos, f, and l.
  1307. //! Linear to the sum of elements before f and l
  1308. //! if cache_last<> is true and pos == end().
  1309. //!
  1310. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1311. //! list. Iterators of this list and all the references are not invalidated.
  1312. void splice(const_iterator pos, slist_impl &x, const_iterator f, const_iterator l, size_type n)
  1313. { return this->splice_after(this->previous(pos), x, x.previous(f), x.previous(l), n); }
  1314. //! <b>Effects</b>: This function sorts the list *this according to operator<.
  1315. //! The sort is stable, that is, the relative order of equivalent elements is preserved.
  1316. //!
  1317. //! <b>Throws</b>: If value_traits::node_traits::node
  1318. //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
  1319. //! or the predicate throws. Basic guarantee.
  1320. //!
  1321. //! <b>Complexity</b>: The number of comparisons is approximately N log N, where N
  1322. //! is the list's size.
  1323. //!
  1324. //! <b>Note</b>: Iterators and references are not invalidated
  1325. template<class Predicate>
  1326. void sort(Predicate p)
  1327. {
  1328. if (node_traits::get_next(node_traits::get_next(this->get_root_node()))
  1329. != this->get_root_node()) {
  1330. slist_impl carry(this->priv_value_traits());
  1331. detail::array_initializer<slist_impl, 64> counter(this->priv_value_traits());
  1332. int fill = 0;
  1333. const_iterator last_inserted;
  1334. while(!this->empty()){
  1335. last_inserted = this->cbegin();
  1336. carry.splice_after(carry.cbefore_begin(), *this, this->cbefore_begin());
  1337. int i = 0;
  1338. while(i < fill && !counter[i].empty()) {
  1339. carry.swap(counter[i]);
  1340. carry.merge(counter[i++], p, &last_inserted);
  1341. }
  1342. BOOST_INTRUSIVE_INVARIANT_ASSERT(counter[i].empty());
  1343. const_iterator last_element(carry.previous(last_inserted, carry.end()));
  1344. if(constant_time_size){
  1345. counter[i].splice_after( counter[i].cbefore_begin(), carry
  1346. , carry.cbefore_begin(), last_element
  1347. , carry.size());
  1348. }
  1349. else{
  1350. counter[i].splice_after( counter[i].cbefore_begin(), carry
  1351. , carry.cbefore_begin(), last_element);
  1352. }
  1353. if(i == fill)
  1354. ++fill;
  1355. }
  1356. for (int i = 1; i < fill; ++i)
  1357. counter[i].merge(counter[i-1], p, &last_inserted);
  1358. --fill;
  1359. const_iterator last_element(counter[fill].previous(last_inserted, counter[fill].end()));
  1360. if(constant_time_size){
  1361. this->splice_after( cbefore_begin(), counter[fill], counter[fill].cbefore_begin()
  1362. , last_element, counter[fill].size());
  1363. }
  1364. else{
  1365. this->splice_after( cbefore_begin(), counter[fill], counter[fill].cbefore_begin()
  1366. , last_element);
  1367. }
  1368. }
  1369. }
  1370. //! <b>Requires</b>: p must be a comparison function that induces a strict weak
  1371. //! ordering and both *this and x must be sorted according to that ordering
  1372. //! The lists x and *this must be distinct.
  1373. //!
  1374. //! <b>Effects</b>: This function removes all of x's elements and inserts them
  1375. //! in order into *this. The merge is stable; that is, if an element from *this is
  1376. //! equivalent to one from x, then the element from *this will precede the one from x.
  1377. //!
  1378. //! <b>Throws</b>: If value_traits::node_traits::node
  1379. //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
  1380. //! or operator< throws. Basic guarantee.
  1381. //!
  1382. //! <b>Complexity</b>: This function is linear time: it performs at most
  1383. //! size() + x.size() - 1 comparisons.
  1384. //!
  1385. //! <b>Note</b>: Iterators and references are not invalidated.
  1386. void sort()
  1387. { this->sort(value_less<value_type>()); }
  1388. //! <b>Requires</b>: p must be a comparison function that induces a strict weak
  1389. //! ordering and both *this and x must be sorted according to that ordering
  1390. //! The lists x and *this must be distinct.
  1391. //!
  1392. //! <b>Effects</b>: This function removes all of x's elements and inserts them
  1393. //! in order into *this. The merge is stable; that is, if an element from *this is
  1394. //! equivalent to one from x, then the element from *this will precede the one from x.
  1395. //!
  1396. //! <b>Returns</b>: Nothing.
  1397. //!
  1398. //! <b>Throws</b>: If the predicate throws. Basic guarantee.
  1399. //!
  1400. //! <b>Complexity</b>: This function is linear time: it performs at most
  1401. //! size() + x.size() - 1 comparisons.
  1402. //!
  1403. //! <b>Note</b>: Iterators and references are not invalidated.
  1404. //!
  1405. //! <b>Additional note</b>: If optional "l" argument is passed, it is assigned
  1406. //! to an iterator to the last transferred value or end() is x is empty.
  1407. template<class Predicate>
  1408. void merge(slist_impl& x, Predicate p, const_iterator *l = 0)
  1409. {
  1410. const_iterator e(this->cend()), ex(x.cend()), bb(this->cbefore_begin()),
  1411. bb_next;
  1412. if(l) *l = e.unconst();
  1413. while(!x.empty()){
  1414. const_iterator ibx_next(x.cbefore_begin()), ibx(ibx_next++);
  1415. while (++(bb_next = bb) != e && !p(*ibx_next, *bb_next)){
  1416. bb = bb_next;
  1417. }
  1418. if(bb_next == e){
  1419. //Now transfer the rest to the end of the container
  1420. this->splice_after(bb, x, l);
  1421. break;
  1422. }
  1423. else{
  1424. size_type n(0);
  1425. do{
  1426. ibx = ibx_next; ++n;
  1427. } while(++(ibx_next = ibx) != ex && p(*ibx_next, *bb_next));
  1428. this->splice_after(bb, x, x.before_begin(), ibx, n);
  1429. if(l) *l = ibx;
  1430. }
  1431. }
  1432. }
  1433. //! <b>Effects</b>: This function removes all of x's elements and inserts them
  1434. //! in order into *this according to operator<. The merge is stable;
  1435. //! that is, if an element from *this is equivalent to one from x, then the element
  1436. //! from *this will precede the one from x.
  1437. //!
  1438. //! <b>Throws</b>: if operator< throws. Basic guarantee.
  1439. //!
  1440. //! <b>Complexity</b>: This function is linear time: it performs at most
  1441. //! size() + x.size() - 1 comparisons.
  1442. //!
  1443. //! <b>Note</b>: Iterators and references are not invalidated
  1444. void merge(slist_impl& x)
  1445. { this->merge(x, value_less<value_type>()); }
  1446. //! <b>Effects</b>: Reverses the order of elements in the list.
  1447. //!
  1448. //! <b>Throws</b>: Nothing.
  1449. //!
  1450. //! <b>Complexity</b>: This function is linear to the contained elements.
  1451. //!
  1452. //! <b>Note</b>: Iterators and references are not invalidated
  1453. void reverse()
  1454. {
  1455. if(cache_last && !this->empty()){
  1456. this->set_last_node(node_traits::get_next(this->get_root_node()));
  1457. }
  1458. this->priv_reverse(detail::bool_<linear>());
  1459. }
  1460. //! <b>Effects</b>: Removes all the elements that compare equal to value.
  1461. //! No destructors are called.
  1462. //!
  1463. //! <b>Throws</b>: If operator== throws. Basic guarantee.
  1464. //!
  1465. //! <b>Complexity</b>: Linear time. It performs exactly size() comparisons for equality.
  1466. //!
  1467. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1468. //! and iterators to elements that are not removed remain valid. This function is
  1469. //! linear time: it performs exactly size() comparisons for equality.
  1470. void remove(const_reference value)
  1471. { this->remove_if(detail::equal_to_value<const_reference>(value)); }
  1472. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1473. //!
  1474. //! <b>Effects</b>: Removes all the elements that compare equal to value.
  1475. //! Disposer::operator()(pointer) is called for every removed element.
  1476. //!
  1477. //! <b>Throws</b>: If operator== throws. Basic guarantee.
  1478. //!
  1479. //! <b>Complexity</b>: Linear time. It performs exactly size() comparisons for equality.
  1480. //!
  1481. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1482. //! and iterators to elements that are not removed remain valid.
  1483. template<class Disposer>
  1484. void remove_and_dispose(const_reference value, Disposer disposer)
  1485. { this->remove_and_dispose_if(detail::equal_to_value<const_reference>(value), disposer); }
  1486. //! <b>Effects</b>: Removes all the elements for which a specified
  1487. //! predicate is satisfied. No destructors are called.
  1488. //!
  1489. //! <b>Throws</b>: If pred throws. Basic guarantee.
  1490. //!
  1491. //! <b>Complexity</b>: Linear time. It performs exactly size() calls to the predicate.
  1492. //!
  1493. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1494. //! and iterators to elements that are not removed remain valid.
  1495. template<class Pred>
  1496. void remove_if(Pred pred)
  1497. {
  1498. const node_ptr bbeg = this->get_root_node();
  1499. typename node_algorithms::stable_partition_info info;
  1500. node_algorithms::stable_partition
  1501. (bbeg, this->get_end_node(), detail::key_nodeptr_comp<Pred, value_traits>(pred, &this->priv_value_traits()), info);
  1502. //After cache last is set, slist invariants are preserved...
  1503. if(cache_last){
  1504. this->set_last_node(info.new_last_node);
  1505. }
  1506. //...so erase can be safely called
  1507. this->erase_after( const_iterator(bbeg, this->priv_value_traits_ptr())
  1508. , const_iterator(info.beg_2st_partition, this->priv_value_traits_ptr())
  1509. , info.num_1st_partition);
  1510. }
  1511. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1512. //!
  1513. //! <b>Effects</b>: Removes all the elements for which a specified
  1514. //! predicate is satisfied.
  1515. //! Disposer::operator()(pointer) is called for every removed element.
  1516. //!
  1517. //! <b>Throws</b>: If pred throws. Basic guarantee.
  1518. //!
  1519. //! <b>Complexity</b>: Linear time. It performs exactly size() comparisons for equality.
  1520. //!
  1521. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1522. //! and iterators to elements that are not removed remain valid.
  1523. template<class Pred, class Disposer>
  1524. void remove_and_dispose_if(Pred pred, Disposer disposer)
  1525. {
  1526. const node_ptr bbeg = this->get_root_node();
  1527. typename node_algorithms::stable_partition_info info;
  1528. node_algorithms::stable_partition
  1529. (bbeg, this->get_end_node(), detail::key_nodeptr_comp<Pred, value_traits>(pred, &this->priv_value_traits()), info);
  1530. //After cache last is set, slist invariants are preserved...
  1531. if(cache_last){
  1532. this->set_last_node(info.new_last_node);
  1533. }
  1534. //...so erase can be safely called
  1535. this->erase_after_and_dispose( const_iterator(bbeg, this->priv_value_traits_ptr())
  1536. , const_iterator(info.beg_2st_partition, this->priv_value_traits_ptr())
  1537. , disposer);
  1538. }
  1539. //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
  1540. //! elements that are equal from the list. No destructors are called.
  1541. //!
  1542. //! <b>Throws</b>: If operator== throws. Basic guarantee.
  1543. //!
  1544. //! <b>Complexity</b>: Linear time (size()-1) comparisons calls to pred()).
  1545. //!
  1546. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1547. //! and iterators to elements that are not removed remain valid.
  1548. void unique()
  1549. { this->unique_and_dispose(value_equal<value_type>(), detail::null_disposer()); }
  1550. //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
  1551. //! elements that satisfy some binary predicate from the list.
  1552. //! No destructors are called.
  1553. //!
  1554. //! <b>Throws</b>: If the predicate throws. Basic guarantee.
  1555. //!
  1556. //! <b>Complexity</b>: Linear time (size()-1) comparisons equality comparisons.
  1557. //!
  1558. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1559. //! and iterators to elements that are not removed remain valid.
  1560. template<class BinaryPredicate>
  1561. void unique(BinaryPredicate pred)
  1562. { this->unique_and_dispose(pred, detail::null_disposer()); }
  1563. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1564. //!
  1565. //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
  1566. //! elements that satisfy some binary predicate from the list.
  1567. //! Disposer::operator()(pointer) is called for every removed element.
  1568. //!
  1569. //! <b>Throws</b>: If operator== throws. Basic guarantee.
  1570. //!
  1571. //! <b>Complexity</b>: Linear time (size()-1) comparisons equality comparisons.
  1572. //!
  1573. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1574. //! and iterators to elements that are not removed remain valid.
  1575. template<class Disposer>
  1576. void unique_and_dispose(Disposer disposer)
  1577. { this->unique(value_equal<value_type>(), disposer); }
  1578. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1579. //!
  1580. //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
  1581. //! elements that satisfy some binary predicate from the list.
  1582. //! Disposer::operator()(pointer) is called for every removed element.
  1583. //!
  1584. //! <b>Throws</b>: If the predicate throws. Basic guarantee.
  1585. //!
  1586. //! <b>Complexity</b>: Linear time (size()-1) comparisons equality comparisons.
  1587. //!
  1588. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1589. //! and iterators to elements that are not removed remain valid.
  1590. template<class BinaryPredicate, class Disposer>
  1591. void unique_and_dispose(BinaryPredicate pred, Disposer disposer)
  1592. {
  1593. const_iterator end_n(this->cend());
  1594. const_iterator bcur(this->cbegin());
  1595. if(bcur != end_n){
  1596. const_iterator cur(bcur);
  1597. ++cur;
  1598. while(cur != end_n) {
  1599. if (pred(*bcur, *cur)){
  1600. cur = this->erase_after_and_dispose(bcur, disposer);
  1601. }
  1602. else{
  1603. bcur = cur;
  1604. ++cur;
  1605. }
  1606. }
  1607. if(cache_last){
  1608. this->set_last_node(bcur.pointed_node());
  1609. }
  1610. }
  1611. }
  1612. //! <b>Requires</b>: value must be a reference to a value inserted in a list.
  1613. //!
  1614. //! <b>Effects</b>: This function returns a const_iterator pointing to the element
  1615. //!
  1616. //! <b>Throws</b>: Nothing.
  1617. //!
  1618. //! <b>Complexity</b>: Constant time.
  1619. //!
  1620. //! <b>Note</b>: Iterators and references are not invalidated.
  1621. //! This static function is available only if the <i>value traits</i>
  1622. //! is stateless.
  1623. static iterator s_iterator_to(reference value)
  1624. {
  1625. BOOST_STATIC_ASSERT((!stateful_value_traits));
  1626. return iterator (value_traits::to_node_ptr(value), const_value_traits_ptr());
  1627. }
  1628. //! <b>Requires</b>: value must be a const reference to a value inserted in a list.
  1629. //!
  1630. //! <b>Effects</b>: This function returns an iterator pointing to the element.
  1631. //!
  1632. //! <b>Throws</b>: Nothing.
  1633. //!
  1634. //! <b>Complexity</b>: Constant time.
  1635. //!
  1636. //! <b>Note</b>: Iterators and references are not invalidated.
  1637. //! This static function is available only if the <i>value traits</i>
  1638. //! is stateless.
  1639. static const_iterator s_iterator_to(const_reference value)
  1640. {
  1641. BOOST_STATIC_ASSERT((!stateful_value_traits));
  1642. reference r =*detail::uncast(pointer_traits<const_pointer>::pointer_to(value));
  1643. return const_iterator(value_traits::to_node_ptr(r), const_value_traits_ptr());
  1644. }
  1645. //! <b>Requires</b>: value must be a reference to a value inserted in a list.
  1646. //!
  1647. //! <b>Effects</b>: This function returns a const_iterator pointing to the element
  1648. //!
  1649. //! <b>Throws</b>: Nothing.
  1650. //!
  1651. //! <b>Complexity</b>: Constant time.
  1652. //!
  1653. //! <b>Note</b>: Iterators and references are not invalidated.
  1654. iterator iterator_to(reference value)
  1655. {
  1656. BOOST_INTRUSIVE_INVARIANT_ASSERT(linear || !node_algorithms::inited(this->priv_value_traits().to_node_ptr(value)));
  1657. return iterator (this->priv_value_traits().to_node_ptr(value), this->priv_value_traits_ptr());
  1658. }
  1659. //! <b>Requires</b>: value must be a const reference to a value inserted in a list.
  1660. //!
  1661. //! <b>Effects</b>: This function returns an iterator pointing to the element.
  1662. //!
  1663. //! <b>Throws</b>: Nothing.
  1664. //!
  1665. //! <b>Complexity</b>: Constant time.
  1666. //!
  1667. //! <b>Note</b>: Iterators and references are not invalidated.
  1668. const_iterator iterator_to(const_reference value) const
  1669. {
  1670. reference r =*detail::uncast(pointer_traits<const_pointer>::pointer_to(value));
  1671. BOOST_INTRUSIVE_INVARIANT_ASSERT (linear || !node_algorithms::inited(this->priv_value_traits().to_node_ptr(r)));
  1672. return const_iterator(this->priv_value_traits().to_node_ptr(r), this->priv_value_traits_ptr());
  1673. }
  1674. //! <b>Returns</b>: The iterator to the element before i in the list.
  1675. //! Returns the end-iterator, if either i is the begin-iterator or the
  1676. //! list is empty.
  1677. //!
  1678. //! <b>Throws</b>: Nothing.
  1679. //!
  1680. //! <b>Complexity</b>: Linear to the number of elements before i.
  1681. //! Constant if cache_last<> is true and i == end().
  1682. iterator previous(iterator i)
  1683. { return this->previous(this->cbefore_begin(), i); }
  1684. //! <b>Returns</b>: The const_iterator to the element before i in the list.
  1685. //! Returns the end-const_iterator, if either i is the begin-const_iterator or
  1686. //! the list is empty.
  1687. //!
  1688. //! <b>Throws</b>: Nothing.
  1689. //!
  1690. //! <b>Complexity</b>: Linear to the number of elements before i.
  1691. //! Constant if cache_last<> is true and i == end().
  1692. const_iterator previous(const_iterator i) const
  1693. { return this->previous(this->cbefore_begin(), i); }
  1694. //! <b>Returns</b>: The iterator to the element before i in the list,
  1695. //! starting the search on element after prev_from.
  1696. //! Returns the end-iterator, if either i is the begin-iterator or the
  1697. //! list is empty.
  1698. //!
  1699. //! <b>Throws</b>: Nothing.
  1700. //!
  1701. //! <b>Complexity</b>: Linear to the number of elements before i.
  1702. //! Constant if cache_last<> is true and i == end().
  1703. iterator previous(const_iterator prev_from, iterator i)
  1704. { return this->previous(prev_from, const_iterator(i)).unconst(); }
  1705. //! <b>Returns</b>: The const_iterator to the element before i in the list,
  1706. //! starting the search on element after prev_from.
  1707. //! Returns the end-const_iterator, if either i is the begin-const_iterator or
  1708. //! the list is empty.
  1709. //!
  1710. //! <b>Throws</b>: Nothing.
  1711. //!
  1712. //! <b>Complexity</b>: Linear to the number of elements before i.
  1713. //! Constant if cache_last<> is true and i == end().
  1714. const_iterator previous(const_iterator prev_from, const_iterator i) const
  1715. {
  1716. if(cache_last && (i.pointed_node() == this->get_end_node())){
  1717. return const_iterator(detail::uncast(this->get_last_node()), this->priv_value_traits_ptr());
  1718. }
  1719. return const_iterator
  1720. (node_algorithms::get_previous_node
  1721. (prev_from.pointed_node(), i.pointed_node()), this->priv_value_traits_ptr());
  1722. }
  1723. ///@cond
  1724. //! <b>Requires</b>: prev_pos must be a dereferenceable iterator in *this or be
  1725. //! before_begin(), and f and before_l belong to another slist.
  1726. //!
  1727. //! <b>Effects</b>: Transfers the range [f, before_l] to this
  1728. //! list, after the element pointed by prev_pos.
  1729. //! No destructors or copy constructors are called.
  1730. //!
  1731. //! <b>Throws</b>: Nothing.
  1732. //!
  1733. //! <b>Complexity</b>: Linear to the number of elements transferred
  1734. //! if constant_time_size is true. Constant-time otherwise.
  1735. //!
  1736. //! <b>Note</b>: Iterators of values obtained from the list that owned f and before_l now
  1737. //! point to elements of this list. Iterators of this list and all the references are not invalidated.
  1738. //!
  1739. //! <b>Warning</b>: Experimental function, don't use it!
  1740. void incorporate_after(const_iterator prev_pos, const node_ptr & f, const node_ptr & before_l)
  1741. {
  1742. if(constant_time_size)
  1743. this->incorporate_after(prev_pos, f, before_l, node_algorithms::distance(f.pointed_node(), before_l.pointed_node())+1);
  1744. else
  1745. this->priv_incorporate_after(prev_pos.pointed_node(), f, before_l);
  1746. }
  1747. //! <b>Requires</b>: prev_pos must be a dereferenceable iterator in *this or be
  1748. //! before_begin(), and f and before_l belong to another slist.
  1749. //! n == distance(f, before_l) + 1.
  1750. //!
  1751. //! <b>Effects</b>: Transfers the range [f, before_l] to this
  1752. //! list, after the element pointed by prev_pos.
  1753. //! No destructors or copy constructors are called.
  1754. //!
  1755. //! <b>Throws</b>: Nothing.
  1756. //!
  1757. //! <b>Complexity</b>: Constant time.
  1758. //!
  1759. //! <b>Note</b>: Iterators of values obtained from the list that owned f and before_l now
  1760. //! point to elements of this list. Iterators of this list and all the references are not invalidated.
  1761. //!
  1762. //! <b>Warning</b>: Experimental function, don't use it!
  1763. void incorporate_after(const_iterator prev_pos, const node_ptr & f, const node_ptr & before_l, size_type n)
  1764. {
  1765. if(n){
  1766. BOOST_INTRUSIVE_INVARIANT_ASSERT(n > 0);
  1767. BOOST_INTRUSIVE_INVARIANT_ASSERT
  1768. (size_type(boost::intrusive::iterator_distance
  1769. ( iterator(f, this->priv_value_traits_ptr())
  1770. , iterator(before_l, this->priv_value_traits_ptr())))
  1771. +1 == n);
  1772. this->priv_incorporate_after(prev_pos.pointed_node(), f, before_l);
  1773. if(constant_time_size){
  1774. this->priv_size_traits().increase(n);
  1775. }
  1776. }
  1777. }
  1778. ///@endcond
  1779. //! <b>Effects</b>: Asserts the integrity of the container.
  1780. //!
  1781. //! <b>Complexity</b>: Linear time.
  1782. //!
  1783. //! <b>Note</b>: The method has no effect when asserts are turned off (e.g., with NDEBUG).
  1784. //! Experimental function, interface might change in future versions.
  1785. void check() const
  1786. {
  1787. const_node_ptr header_ptr = get_root_node();
  1788. // header's next is never null
  1789. BOOST_INTRUSIVE_INVARIANT_ASSERT(node_traits::get_next(header_ptr));
  1790. if (node_traits::get_next(header_ptr) == header_ptr)
  1791. {
  1792. BOOST_INTRUSIVE_INVARIANT_ASSERT(!constant_time_size || this->priv_size_traits().get_size() == 0);
  1793. return;
  1794. }
  1795. size_t node_count = 0;
  1796. const_node_ptr p = header_ptr;
  1797. while (true)
  1798. {
  1799. const_node_ptr next_p = node_traits::get_next(p);
  1800. if (!linear)
  1801. {
  1802. BOOST_INTRUSIVE_INVARIANT_ASSERT(next_p);
  1803. }
  1804. else
  1805. {
  1806. BOOST_INTRUSIVE_INVARIANT_ASSERT(next_p != header_ptr);
  1807. }
  1808. if ((!linear && next_p == header_ptr) || (linear && !next_p))
  1809. {
  1810. BOOST_INTRUSIVE_INVARIANT_ASSERT(!cache_last || get_last_node() == p);
  1811. break;
  1812. }
  1813. p = next_p;
  1814. ++node_count;
  1815. }
  1816. BOOST_INTRUSIVE_INVARIANT_ASSERT(!constant_time_size || this->priv_size_traits().get_size() == node_count);
  1817. }
  1818. friend bool operator==(const slist_impl &x, const slist_impl &y)
  1819. {
  1820. if(constant_time_size && x.size() != y.size()){
  1821. return false;
  1822. }
  1823. return ::boost::intrusive::algo_equal(x.cbegin(), x.cend(), y.cbegin(), y.cend());
  1824. }
  1825. friend bool operator!=(const slist_impl &x, const slist_impl &y)
  1826. { return !(x == y); }
  1827. friend bool operator<(const slist_impl &x, const slist_impl &y)
  1828. { return ::boost::intrusive::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); }
  1829. friend bool operator>(const slist_impl &x, const slist_impl &y)
  1830. { return y < x; }
  1831. friend bool operator<=(const slist_impl &x, const slist_impl &y)
  1832. { return !(y < x); }
  1833. friend bool operator>=(const slist_impl &x, const slist_impl &y)
  1834. { return !(x < y); }
  1835. friend void swap(slist_impl &x, slist_impl &y)
  1836. { x.swap(y); }
  1837. private:
  1838. void priv_splice_after(node_ptr prev_pos_n, slist_impl &x, node_ptr before_f_n, node_ptr before_l_n)
  1839. {
  1840. if (cache_last && (before_f_n != before_l_n)){
  1841. if(prev_pos_n == this->get_last_node()){
  1842. this->set_last_node(before_l_n);
  1843. }
  1844. if(&x != this && node_traits::get_next(before_l_n) == x.get_end_node()){
  1845. x.set_last_node(before_f_n);
  1846. }
  1847. }
  1848. node_algorithms::transfer_after(prev_pos_n, before_f_n, before_l_n);
  1849. }
  1850. void priv_incorporate_after(node_ptr prev_pos_n, node_ptr first_n, node_ptr before_l_n)
  1851. {
  1852. if(cache_last){
  1853. if(prev_pos_n == this->get_last_node()){
  1854. this->set_last_node(before_l_n);
  1855. }
  1856. }
  1857. node_algorithms::incorporate_after(prev_pos_n, first_n, before_l_n);
  1858. }
  1859. void priv_reverse(detail::bool_<false>)
  1860. { node_algorithms::reverse(this->get_root_node()); }
  1861. void priv_reverse(detail::bool_<true>)
  1862. {
  1863. node_ptr new_first = node_algorithms::reverse
  1864. (node_traits::get_next(this->get_root_node()));
  1865. node_traits::set_next(this->get_root_node(), new_first);
  1866. }
  1867. void priv_shift_backwards(size_type n, detail::bool_<false>)
  1868. {
  1869. node_ptr l = node_algorithms::move_forward(this->get_root_node(), (std::size_t)n);
  1870. if(cache_last && l){
  1871. this->set_last_node(l);
  1872. }
  1873. }
  1874. void priv_shift_backwards(size_type n, detail::bool_<true>)
  1875. {
  1876. typename node_algorithms::node_pair ret(
  1877. node_algorithms::move_first_n_forward
  1878. (node_traits::get_next(this->get_root_node()), (std::size_t)n));
  1879. if(ret.first){
  1880. node_traits::set_next(this->get_root_node(), ret.first);
  1881. if(cache_last){
  1882. this->set_last_node(ret.second);
  1883. }
  1884. }
  1885. }
  1886. void priv_shift_forward(size_type n, detail::bool_<false>)
  1887. {
  1888. node_ptr l = node_algorithms::move_backwards(this->get_root_node(), (std::size_t)n);
  1889. if(cache_last && l){
  1890. this->set_last_node(l);
  1891. }
  1892. }
  1893. void priv_shift_forward(size_type n, detail::bool_<true>)
  1894. {
  1895. typename node_algorithms::node_pair ret(
  1896. node_algorithms::move_first_n_backwards
  1897. (node_traits::get_next(this->get_root_node()), (std::size_t)n));
  1898. if(ret.first){
  1899. node_traits::set_next(this->get_root_node(), ret.first);
  1900. if(cache_last){
  1901. this->set_last_node(ret.second);
  1902. }
  1903. }
  1904. }
  1905. static void priv_swap_cache_last(slist_impl *this_impl, slist_impl *other_impl)
  1906. {
  1907. bool other_was_empty = false;
  1908. if(this_impl->empty()){
  1909. //Check if both are empty or
  1910. if(other_impl->empty())
  1911. return;
  1912. //If this is empty swap pointers
  1913. slist_impl *tmp = this_impl;
  1914. this_impl = other_impl;
  1915. other_impl = tmp;
  1916. other_was_empty = true;
  1917. }
  1918. else{
  1919. other_was_empty = other_impl->empty();
  1920. }
  1921. //Precondition: this is not empty
  1922. node_ptr other_old_last(other_impl->get_last_node());
  1923. node_ptr other_bfirst(other_impl->get_root_node());
  1924. node_ptr this_bfirst(this_impl->get_root_node());
  1925. node_ptr this_old_last(this_impl->get_last_node());
  1926. //Move all nodes from this to other's beginning
  1927. node_algorithms::transfer_after(other_bfirst, this_bfirst, this_old_last);
  1928. other_impl->set_last_node(this_old_last);
  1929. if(other_was_empty){
  1930. this_impl->set_last_node(this_bfirst);
  1931. }
  1932. else{
  1933. //Move trailing nodes from other to this
  1934. node_algorithms::transfer_after(this_bfirst, this_old_last, other_old_last);
  1935. this_impl->set_last_node(other_old_last);
  1936. }
  1937. }
  1938. //circular version
  1939. static void priv_swap_lists(node_ptr this_node, node_ptr other_node, detail::bool_<false>)
  1940. { node_algorithms::swap_nodes(this_node, other_node); }
  1941. //linear version
  1942. static void priv_swap_lists(node_ptr this_node, node_ptr other_node, detail::bool_<true>)
  1943. { node_algorithms::swap_trailing_nodes(this_node, other_node); }
  1944. static slist_impl &priv_container_from_end_iterator(const const_iterator &end_iterator)
  1945. {
  1946. //Obtaining the container from the end iterator is not possible with linear
  1947. //singly linked lists (because "end" is represented by the null pointer)
  1948. BOOST_STATIC_ASSERT(!linear);
  1949. BOOST_STATIC_ASSERT((has_container_from_iterator));
  1950. node_ptr p = end_iterator.pointed_node();
  1951. header_holder_type* h = header_holder_type::get_holder(p);
  1952. header_holder_plus_last_t* hpl = detail::parent_from_member< header_holder_plus_last_t, header_holder_type>
  1953. (h, &header_holder_plus_last_t::header_holder_);
  1954. root_plus_size* r = static_cast< root_plus_size* >(hpl);
  1955. data_t *d = detail::parent_from_member<data_t, root_plus_size>
  1956. ( r, &data_t::root_plus_size_);
  1957. slist_impl *s = detail::parent_from_member<slist_impl, data_t>(d, &slist_impl::data_);
  1958. return *s;
  1959. }
  1960. };
  1961. //! Helper metafunction to define a \c slist that yields to the same type when the
  1962. //! same options (either explicitly or implicitly) are used.
  1963. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1964. template<class T, class ...Options>
  1965. #else
  1966. template<class T, class O1 = void, class O2 = void, class O3 = void, class O4 = void, class O5 = void, class O6 = void>
  1967. #endif
  1968. struct make_slist
  1969. {
  1970. /// @cond
  1971. typedef typename pack_options
  1972. < slist_defaults,
  1973. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1974. O1, O2, O3, O4, O5, O6
  1975. #else
  1976. Options...
  1977. #endif
  1978. >::type packed_options;
  1979. typedef typename detail::get_value_traits
  1980. <T, typename packed_options::proto_value_traits>::type value_traits;
  1981. typedef slist_impl
  1982. < value_traits
  1983. , typename packed_options::size_type
  1984. , (std::size_t(packed_options::linear)*slist_bool_flags::linear_pos)
  1985. |(std::size_t(packed_options::constant_time_size)*slist_bool_flags::constant_time_size_pos)
  1986. |(std::size_t(packed_options::cache_last)*slist_bool_flags::cache_last_pos)
  1987. , typename packed_options::header_holder_type
  1988. > implementation_defined;
  1989. /// @endcond
  1990. typedef implementation_defined type;
  1991. };
  1992. #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  1993. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1994. template<class T, class O1, class O2, class O3, class O4, class O5, class O6>
  1995. #else
  1996. template<class T, class ...Options>
  1997. #endif
  1998. class slist
  1999. : public make_slist<T,
  2000. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  2001. O1, O2, O3, O4, O5, O6
  2002. #else
  2003. Options...
  2004. #endif
  2005. >::type
  2006. {
  2007. typedef typename make_slist
  2008. <T,
  2009. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  2010. O1, O2, O3, O4, O5, O6
  2011. #else
  2012. Options...
  2013. #endif
  2014. >::type Base;
  2015. //Assert if passed value traits are compatible with the type
  2016. BOOST_STATIC_ASSERT((detail::is_same<typename Base::value_traits::value_type, T>::value));
  2017. BOOST_MOVABLE_BUT_NOT_COPYABLE(slist)
  2018. public:
  2019. typedef typename Base::value_traits value_traits;
  2020. typedef typename Base::iterator iterator;
  2021. typedef typename Base::const_iterator const_iterator;
  2022. typedef typename Base::size_type size_type;
  2023. typedef typename Base::node_ptr node_ptr;
  2024. BOOST_INTRUSIVE_FORCEINLINE slist()
  2025. : Base()
  2026. {}
  2027. BOOST_INTRUSIVE_FORCEINLINE explicit slist(const value_traits &v_traits)
  2028. : Base(v_traits)
  2029. {}
  2030. struct incorporate_t{};
  2031. BOOST_INTRUSIVE_FORCEINLINE slist( const node_ptr & f, const node_ptr & before_l
  2032. , size_type n, const value_traits &v_traits = value_traits())
  2033. : Base(f, before_l, n, v_traits)
  2034. {}
  2035. template<class Iterator>
  2036. BOOST_INTRUSIVE_FORCEINLINE slist(Iterator b, Iterator e, const value_traits &v_traits = value_traits())
  2037. : Base(b, e, v_traits)
  2038. {}
  2039. BOOST_INTRUSIVE_FORCEINLINE slist(BOOST_RV_REF(slist) x)
  2040. : Base(BOOST_MOVE_BASE(Base, x))
  2041. {}
  2042. BOOST_INTRUSIVE_FORCEINLINE slist& operator=(BOOST_RV_REF(slist) x)
  2043. { return static_cast<slist &>(this->Base::operator=(BOOST_MOVE_BASE(Base, x))); }
  2044. template <class Cloner, class Disposer>
  2045. BOOST_INTRUSIVE_FORCEINLINE void clone_from(const slist &src, Cloner cloner, Disposer disposer)
  2046. { Base::clone_from(src, cloner, disposer); }
  2047. template <class Cloner, class Disposer>
  2048. BOOST_INTRUSIVE_FORCEINLINE void clone_from(BOOST_RV_REF(slist) src, Cloner cloner, Disposer disposer)
  2049. { Base::clone_from(BOOST_MOVE_BASE(Base, src), cloner, disposer); }
  2050. BOOST_INTRUSIVE_FORCEINLINE static slist &container_from_end_iterator(iterator end_iterator)
  2051. { return static_cast<slist &>(Base::container_from_end_iterator(end_iterator)); }
  2052. BOOST_INTRUSIVE_FORCEINLINE static const slist &container_from_end_iterator(const_iterator end_iterator)
  2053. { return static_cast<const slist &>(Base::container_from_end_iterator(end_iterator)); }
  2054. };
  2055. #endif
  2056. } //namespace intrusive
  2057. } //namespace boost
  2058. #include <boost/intrusive/detail/config_end.hpp>
  2059. #endif //BOOST_INTRUSIVE_SLIST_HPP