slist.hpp 88 KB

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