slist.hpp 86 KB

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