slist.hpp 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246
  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. inline static slist_impl &container_from_end_iterator(iterator end_iterator) BOOST_NOEXCEPT
  585. { return slist_impl::priv_container_from_end_iterator(end_iterator); }
  586. //! <b>Precondition</b>: end_iterator must be a valid end const_iterator
  587. //! of slist.
  588. //!
  589. //! <b>Effects</b>: Returns a const reference to the slist associated to the end iterator
  590. //!
  591. //! <b>Throws</b>: Nothing.
  592. //!
  593. //! <b>Complexity</b>: Constant.
  594. inline static const slist_impl &container_from_end_iterator(const_iterator end_iterator) BOOST_NOEXCEPT
  595. { return slist_impl::priv_container_from_end_iterator(end_iterator); }
  596. //! <b>Effects</b>: Returns the number of the elements contained in the list.
  597. //!
  598. //! <b>Throws</b>: Nothing.
  599. //!
  600. //! <b>Complexity</b>: Linear to the number of elements contained in the list.
  601. //! if constant_time_size is false. Constant time otherwise.
  602. //!
  603. //! <b>Note</b>: Does not affect the validity of iterators and references.
  604. inline size_type size() const BOOST_NOEXCEPT
  605. {
  606. BOOST_IF_CONSTEXPR(constant_time_size)
  607. return this->priv_size_traits().get_size();
  608. else
  609. return node_algorithms::count(this->get_root_node()) - 1;
  610. }
  611. //! <b>Effects</b>: Returns true if the list contains no elements.
  612. //!
  613. //! <b>Throws</b>: Nothing.
  614. //!
  615. //! <b>Complexity</b>: Constant.
  616. //!
  617. //! <b>Note</b>: Does not affect the validity of iterators and references.
  618. inline bool empty() const BOOST_NOEXCEPT
  619. { return node_algorithms::is_empty(this->get_root_node()); }
  620. //! <b>Effects</b>: Swaps the elements of x and *this.
  621. //!
  622. //! <b>Throws</b>: Nothing.
  623. //!
  624. //! <b>Complexity</b>: Linear to the number of elements of both lists.
  625. //! Constant-time if linear<> and/or cache_last<> options are used.
  626. //!
  627. //! <b>Note</b>: Does not affect the validity of iterators and references.
  628. void swap(slist_impl& other)
  629. {
  630. BOOST_IF_CONSTEXPR(cache_last){
  631. priv_swap_cache_last(this, &other);
  632. }
  633. else{
  634. this->priv_swap_lists(this->get_root_node(), other.get_root_node(), detail::bool_<linear>());
  635. }
  636. this->priv_size_traits().swap(other.priv_size_traits());
  637. }
  638. //! <b>Effects</b>: Moves backwards all the elements, so that the first
  639. //! element becomes the second, the second becomes the third...
  640. //! the last element becomes the first one.
  641. //!
  642. //! <b>Throws</b>: Nothing.
  643. //!
  644. //! <b>Complexity</b>: Linear to the number of elements plus the number shifts.
  645. //!
  646. //! <b>Note</b>: Iterators Does not affect the validity of iterators and references.
  647. void shift_backwards(size_type n = 1) BOOST_NOEXCEPT
  648. { this->priv_shift_backwards(n, detail::bool_<linear>()); }
  649. //! <b>Effects</b>: Moves forward all the elements, so that the second
  650. //! element becomes the first, the third becomes the second...
  651. //! the first element becomes the last one.
  652. //!
  653. //! <b>Throws</b>: Nothing.
  654. //!
  655. //! <b>Complexity</b>: Linear to the number of elements plus the number shifts.
  656. //!
  657. //! <b>Note</b>: Does not affect the validity of iterators and references.
  658. void shift_forward(size_type n = 1) BOOST_NOEXCEPT
  659. { this->priv_shift_forward(n, detail::bool_<linear>()); }
  660. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  661. //! Cloner should yield to nodes equivalent to the original nodes.
  662. //!
  663. //! <b>Effects</b>: Erases all the elements from *this
  664. //! calling Disposer::operator()(pointer), clones all the
  665. //! elements from src calling Cloner::operator()(const_reference )
  666. //! and inserts them on *this.
  667. //!
  668. //! If cloner throws, all cloned elements are unlinked and disposed
  669. //! calling Disposer::operator()(pointer).
  670. //!
  671. //! <b>Complexity</b>: Linear to erased plus inserted elements.
  672. //!
  673. //! <b>Throws</b>: If cloner throws.
  674. template <class Cloner, class Disposer>
  675. void clone_from(const slist_impl &src, Cloner cloner, Disposer disposer)
  676. {
  677. this->clear_and_dispose(disposer);
  678. detail::exception_disposer<slist_impl, Disposer>
  679. rollback(*this, disposer);
  680. const_iterator prev(this->cbefore_begin());
  681. const_iterator b(src.begin()), e(src.end());
  682. for(; b != e; ++b){
  683. prev = this->insert_after(prev, *cloner(*b));
  684. }
  685. rollback.release();
  686. }
  687. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  688. //! Cloner should yield to nodes equivalent to the original nodes.
  689. //!
  690. //! <b>Effects</b>: Erases all the elements from *this
  691. //! calling Disposer::operator()(pointer), clones all the
  692. //! elements from src calling Cloner::operator()(reference)
  693. //! and inserts them on *this.
  694. //!
  695. //! If cloner throws, all cloned elements are unlinked and disposed
  696. //! calling Disposer::operator()(pointer).
  697. //!
  698. //! <b>Complexity</b>: Linear to erased plus inserted elements.
  699. //!
  700. //! <b>Throws</b>: If cloner throws.
  701. template <class Cloner, class Disposer>
  702. void clone_from(BOOST_RV_REF(slist_impl) src, Cloner cloner, Disposer disposer)
  703. {
  704. this->clear_and_dispose(disposer);
  705. detail::exception_disposer<slist_impl, Disposer>
  706. rollback(*this, disposer);
  707. iterator prev(this->cbefore_begin());
  708. iterator b(src.begin()), e(src.end());
  709. for(; b != e; ++b){
  710. prev = this->insert_after(prev, *cloner(*b));
  711. }
  712. rollback.release();
  713. }
  714. //! <b>Requires</b>: value must be an lvalue and prev_p must point to an element
  715. //! contained by the list or to end().
  716. //!
  717. //! <b>Effects</b>: Inserts the value after the position pointed by prev_p.
  718. //! No copy constructor is called.
  719. //!
  720. //! <b>Returns</b>: An iterator to the inserted element.
  721. //!
  722. //! <b>Throws</b>: Nothing.
  723. //!
  724. //! <b>Complexity</b>: Constant.
  725. //!
  726. //! <b>Note</b>: Does not affect the validity of iterators and references.
  727. iterator insert_after(const_iterator prev_p, reference value) BOOST_NOEXCEPT
  728. {
  729. node_ptr n = priv_value_traits().to_node_ptr(value);
  730. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::inited(n));
  731. node_ptr prev_n(prev_p.pointed_node());
  732. node_algorithms::link_after(prev_n, n);
  733. if(cache_last && (this->get_last_node() == prev_n)){
  734. this->set_last_node(n);
  735. }
  736. this->priv_size_traits().increment();
  737. return iterator (n, this->priv_value_traits_ptr());
  738. }
  739. //! <b>Requires</b>: Dereferencing iterator must yield
  740. //! an lvalue of type value_type and prev_p must point to an element
  741. //! contained by the list or to the end node.
  742. //!
  743. //! <b>Effects</b>: Inserts the [f, l)
  744. //! after the position prev_p.
  745. //!
  746. //! <b>Throws</b>: Nothing.
  747. //!
  748. //! <b>Complexity</b>: Linear to the number of elements inserted.
  749. //!
  750. //! <b>Note</b>: Does not affect the validity of iterators and references.
  751. template<class Iterator>
  752. void insert_after(const_iterator prev_p, Iterator f, Iterator l) BOOST_NOEXCEPT
  753. {
  754. //Insert first nodes avoiding cache and size checks
  755. size_type count = 0;
  756. node_ptr prev_n(prev_p.pointed_node());
  757. for (; f != l; ++f, ++count){
  758. const node_ptr n = priv_value_traits().to_node_ptr(*f);
  759. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::inited(n));
  760. node_algorithms::link_after(prev_n, n);
  761. prev_n = n;
  762. }
  763. //Now fix special cases if needed
  764. if(cache_last && (this->get_last_node() == prev_p.pointed_node())){
  765. this->set_last_node(prev_n);
  766. }
  767. BOOST_IF_CONSTEXPR(constant_time_size){
  768. this->priv_size_traits().increase(count);
  769. }
  770. }
  771. //! <b>Requires</b>: value must be an lvalue and p must point to an element
  772. //! contained by the list or to end().
  773. //!
  774. //! <b>Effects</b>: Inserts the value before the position pointed by p.
  775. //! No copy constructor is called.
  776. //!
  777. //! <b>Throws</b>: Nothing.
  778. //!
  779. //! <b>Complexity</b>: Linear to the number of elements before p.
  780. //! Constant-time if cache_last<> is true and p == end().
  781. //!
  782. //! <b>Note</b>: Does not affect the validity of iterators and references.
  783. iterator insert(const_iterator p, reference value) BOOST_NOEXCEPT
  784. { return this->insert_after(this->previous(p), value); }
  785. //! <b>Requires</b>: Dereferencing iterator must yield
  786. //! an lvalue of type value_type and p must point to an element
  787. //! contained by the list or to the end node.
  788. //!
  789. //! <b>Effects</b>: Inserts the pointed by b and e
  790. //! before the position p. No copy constructors are called.
  791. //!
  792. //! <b>Throws</b>: Nothing.
  793. //!
  794. //! <b>Complexity</b>: Linear to the number of elements inserted plus linear
  795. //! to the elements before b.
  796. //! Linear to the number of elements to insert if cache_last<> option is true and p == end().
  797. //!
  798. //! <b>Note</b>: Does not affect the validity of iterators and references.
  799. template<class Iterator>
  800. void insert(const_iterator p, Iterator b, Iterator e) BOOST_NOEXCEPT
  801. { return this->insert_after(this->previous(p), b, e); }
  802. //! <b>Effects</b>: Erases the element after the element pointed by prev of
  803. //! the list. No destructors are called.
  804. //!
  805. //! <b>Returns</b>: the first element remaining beyond the removed elements,
  806. //! or end() if no such element exists.
  807. //!
  808. //! <b>Throws</b>: Nothing.
  809. //!
  810. //! <b>Complexity</b>: Constant.
  811. //!
  812. //! <b>Note</b>: Invalidates the iterators (but not the references) to the
  813. //! erased element.
  814. iterator erase_after(const_iterator prev) BOOST_NOEXCEPT
  815. { return this->erase_after_and_dispose(prev, detail::null_disposer()); }
  816. //! <b>Effects</b>: Erases the range (before_f, l) from
  817. //! the list. No destructors are called.
  818. //!
  819. //! <b>Returns</b>: the first element remaining beyond the removed elements,
  820. //! or end() if no such element exists.
  821. //!
  822. //! <b>Throws</b>: Nothing.
  823. //!
  824. //! <b>Complexity</b>: Linear to the number of erased elements if it's a safe-mode
  825. //! , auto-unlink value or constant-time size is activated. Constant time otherwise.
  826. //!
  827. //! <b>Note</b>: Invalidates the iterators (but not the references) to the
  828. //! erased element.
  829. iterator erase_after(const_iterator before_f, const_iterator l) BOOST_NOEXCEPT
  830. {
  831. BOOST_IF_CONSTEXPR(safemode_or_autounlink || constant_time_size){
  832. return this->erase_after_and_dispose(before_f, l, detail::null_disposer());
  833. }
  834. else{
  835. const node_ptr bfp = before_f.pointed_node();
  836. const node_ptr lp = l.pointed_node();
  837. BOOST_IF_CONSTEXPR(cache_last){
  838. if(lp == this->get_end_node()){
  839. this->set_last_node(bfp);
  840. }
  841. }
  842. node_algorithms::unlink_after(bfp, lp);
  843. return l.unconst();
  844. }
  845. }
  846. //! <b>Effects</b>: Erases the range (before_f, l) from
  847. //! the list. n must be distance(before_f, l) - 1.
  848. //! No destructors are called.
  849. //!
  850. //! <b>Returns</b>: the first element remaining beyond the removed elements,
  851. //! or end() if no such element exists.
  852. //!
  853. //! <b>Throws</b>: Nothing.
  854. //!
  855. //! <b>Complexity</b>: constant-time if link_mode is normal_link.
  856. //! Linear to the elements (l - before_f) otherwise.
  857. //!
  858. //! <b>Note</b>: Invalidates the iterators (but not the references) to the
  859. //! erased element.
  860. iterator erase_after(const_iterator before_f, const_iterator l, size_type n) BOOST_NOEXCEPT
  861. {
  862. BOOST_INTRUSIVE_INVARIANT_ASSERT(node_algorithms::distance((++const_iterator(before_f)).pointed_node(), l.pointed_node()) == n);
  863. (void)n;
  864. BOOST_IF_CONSTEXPR(safemode_or_autounlink){
  865. return this->erase_after(before_f, l);
  866. }
  867. else{
  868. const node_ptr bfp = before_f.pointed_node();
  869. const node_ptr lp = l.pointed_node();
  870. BOOST_IF_CONSTEXPR(cache_last){
  871. if((lp == this->get_end_node())){
  872. this->set_last_node(bfp);
  873. }
  874. }
  875. node_algorithms::unlink_after(bfp, lp);
  876. BOOST_IF_CONSTEXPR(constant_time_size){
  877. this->priv_size_traits().decrease(n);
  878. }
  879. return l.unconst();
  880. }
  881. }
  882. //! <b>Effects</b>: Erases the element pointed by i of the list.
  883. //! No destructors are called.
  884. //!
  885. //! <b>Returns</b>: the first element remaining beyond the removed element,
  886. //! or end() if no such element exists.
  887. //!
  888. //! <b>Throws</b>: Nothing.
  889. //!
  890. //! <b>Complexity</b>: Linear to the elements before i.
  891. //!
  892. //! <b>Note</b>: Invalidates the iterators (but not the references) to the
  893. //! erased element.
  894. iterator erase(const_iterator i) BOOST_NOEXCEPT
  895. { return this->erase_after(this->previous(i)); }
  896. //! <b>Requires</b>: f and l must be valid iterator to elements in *this.
  897. //!
  898. //! <b>Effects</b>: Erases the range pointed by b and e.
  899. //! No destructors are called.
  900. //!
  901. //! <b>Returns</b>: the first element remaining beyond the removed elements,
  902. //! or end() if no such element exists.
  903. //!
  904. //! <b>Throws</b>: Nothing.
  905. //!
  906. //! <b>Complexity</b>: Linear to the elements before l.
  907. //!
  908. //! <b>Note</b>: Invalidates the iterators (but not the references) to the
  909. //! erased elements.
  910. iterator erase(const_iterator f, const_iterator l) BOOST_NOEXCEPT
  911. { return this->erase_after(this->previous(f), l); }
  912. //! <b>Effects</b>: Erases the range [f, l) from
  913. //! the list. n must be distance(f, l).
  914. //! No destructors are called.
  915. //!
  916. //! <b>Returns</b>: the first element remaining beyond the removed elements,
  917. //! or end() if no such element exists.
  918. //!
  919. //! <b>Throws</b>: Nothing.
  920. //!
  921. //! <b>Complexity</b>: linear to the elements before f if link_mode is normal_link
  922. //! and constant_time_size is activated. Linear to the elements before l otherwise.
  923. //!
  924. //! <b>Note</b>: Invalidates the iterators (but not the references) to the
  925. //! erased element.
  926. iterator erase(const_iterator f, const_iterator l, size_type n) BOOST_NOEXCEPT
  927. { return this->erase_after(this->previous(f), l, n); }
  928. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  929. //!
  930. //! <b>Effects</b>: Erases the element after the element pointed by prev of
  931. //! the list.
  932. //! Disposer::operator()(pointer) is called for the removed element.
  933. //!
  934. //! <b>Returns</b>: the first element remaining beyond the removed elements,
  935. //! or end() if no such element exists.
  936. //!
  937. //! <b>Throws</b>: Nothing.
  938. //!
  939. //! <b>Complexity</b>: Constant.
  940. //!
  941. //! <b>Note</b>: Invalidates the iterators to the erased element.
  942. template<class Disposer>
  943. iterator erase_after_and_dispose(const_iterator prev, Disposer disposer) BOOST_NOEXCEPT
  944. {
  945. const_iterator it(prev);
  946. ++it;
  947. node_ptr to_erase(it.pointed_node());
  948. ++it;
  949. node_ptr prev_n(prev.pointed_node());
  950. node_algorithms::unlink_after(prev_n);
  951. if(cache_last && (to_erase == this->get_last_node())){
  952. this->set_last_node(prev_n);
  953. }
  954. BOOST_IF_CONSTEXPR(safemode_or_autounlink)
  955. node_algorithms::init(to_erase);
  956. disposer(priv_value_traits().to_value_ptr(to_erase));
  957. this->priv_size_traits().decrement();
  958. return it.unconst();
  959. }
  960. /// @cond
  961. static iterator s_insert_after(const_iterator const prev_p, reference value) BOOST_NOEXCEPT
  962. {
  963. BOOST_INTRUSIVE_STATIC_ASSERT(((!cache_last)&&(!constant_time_size)&&(!stateful_value_traits)));
  964. node_ptr const n = value_traits::to_node_ptr(value);
  965. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::inited(n));
  966. node_algorithms::link_after(prev_p.pointed_node(), n);
  967. return iterator (n, const_value_traits_ptr());
  968. }
  969. template<class Disposer>
  970. static iterator s_erase_after_and_dispose(const_iterator prev, Disposer disposer) BOOST_NOEXCEPT
  971. {
  972. BOOST_INTRUSIVE_STATIC_ASSERT(((!cache_last)&&(!constant_time_size)&&(!stateful_value_traits)));
  973. const_iterator it(prev);
  974. ++it;
  975. node_ptr to_erase(it.pointed_node());
  976. ++it;
  977. node_ptr prev_n(prev.pointed_node());
  978. node_algorithms::unlink_after(prev_n);
  979. BOOST_IF_CONSTEXPR(safemode_or_autounlink)
  980. node_algorithms::init(to_erase);
  981. disposer(value_traits::to_value_ptr(to_erase));
  982. return it.unconst();
  983. }
  984. template<class Disposer>
  985. static iterator s_erase_after_and_dispose(const_iterator before_f, const_iterator l, Disposer disposer) BOOST_NOEXCEPT
  986. {
  987. BOOST_INTRUSIVE_STATIC_ASSERT(((!cache_last)&&(!constant_time_size)&&(!stateful_value_traits)));
  988. node_ptr bfp(before_f.pointed_node()), lp(l.pointed_node());
  989. node_ptr fp(node_traits::get_next(bfp));
  990. node_algorithms::unlink_after(bfp, lp);
  991. while(fp != lp){
  992. node_ptr to_erase(fp);
  993. fp = node_traits::get_next(fp);
  994. BOOST_IF_CONSTEXPR(safemode_or_autounlink)
  995. node_algorithms::init(to_erase);
  996. disposer(value_traits::to_value_ptr(to_erase));
  997. }
  998. return l.unconst();
  999. }
  1000. static iterator s_erase_after(const_iterator prev) BOOST_NOEXCEPT
  1001. { return s_erase_after_and_dispose(prev, detail::null_disposer()); }
  1002. /// @endcond
  1003. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1004. //!
  1005. //! <b>Effects</b>: Erases the range (before_f, l) from
  1006. //! the list.
  1007. //! Disposer::operator()(pointer) is called for the removed elements.
  1008. //!
  1009. //! <b>Returns</b>: the first element remaining beyond the removed elements,
  1010. //! or end() if no such element exists.
  1011. //!
  1012. //! <b>Throws</b>: Nothing.
  1013. //!
  1014. //! <b>Complexity</b>: Linear to the elements (l - before_f + 1).
  1015. //!
  1016. //! <b>Note</b>: Invalidates the iterators to the erased element.
  1017. template<class Disposer>
  1018. iterator erase_after_and_dispose(const_iterator before_f, const_iterator l, Disposer disposer) BOOST_NOEXCEPT
  1019. {
  1020. node_ptr bfp(before_f.pointed_node()), lp(l.pointed_node());
  1021. node_ptr fp(node_traits::get_next(bfp));
  1022. node_algorithms::unlink_after(bfp, lp);
  1023. while(fp != lp){
  1024. node_ptr to_erase(fp);
  1025. fp = node_traits::get_next(fp);
  1026. BOOST_IF_CONSTEXPR(safemode_or_autounlink)
  1027. node_algorithms::init(to_erase);
  1028. disposer(priv_value_traits().to_value_ptr(to_erase));
  1029. this->priv_size_traits().decrement();
  1030. }
  1031. if(cache_last && (node_traits::get_next(bfp) == this->get_end_node())){
  1032. this->set_last_node(bfp);
  1033. }
  1034. return l.unconst();
  1035. }
  1036. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1037. //!
  1038. //! <b>Effects</b>: Erases the element pointed by i of the list.
  1039. //! No destructors are called.
  1040. //! Disposer::operator()(pointer) is called for the removed element.
  1041. //!
  1042. //! <b>Returns</b>: the first element remaining beyond the removed element,
  1043. //! or end() if no such element exists.
  1044. //!
  1045. //! <b>Throws</b>: Nothing.
  1046. //!
  1047. //! <b>Complexity</b>: Linear to the elements before i.
  1048. //!
  1049. //! <b>Note</b>: Invalidates the iterators (but not the references) to the
  1050. //! erased element.
  1051. template<class Disposer>
  1052. iterator erase_and_dispose(const_iterator i, Disposer disposer) BOOST_NOEXCEPT
  1053. { return this->erase_after_and_dispose(this->previous(i), disposer); }
  1054. #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1055. template<class Disposer>
  1056. iterator erase_and_dispose(iterator i, Disposer disposer) BOOST_NOEXCEPT
  1057. { return this->erase_and_dispose(const_iterator(i), disposer); }
  1058. #endif
  1059. //! <b>Requires</b>: f and l must be valid iterator to elements in *this.
  1060. //! Disposer::operator()(pointer) shouldn't throw.
  1061. //!
  1062. //! <b>Effects</b>: Erases the range pointed by b and e.
  1063. //! No destructors are called.
  1064. //! Disposer::operator()(pointer) is called for the removed elements.
  1065. //!
  1066. //! <b>Returns</b>: the first element remaining beyond the removed elements,
  1067. //! or end() if no such element exists.
  1068. //!
  1069. //! <b>Throws</b>: Nothing.
  1070. //!
  1071. //! <b>Complexity</b>: Linear to the number of erased elements plus linear
  1072. //! to the elements before f.
  1073. //!
  1074. //! <b>Note</b>: Invalidates the iterators (but not the references) to the
  1075. //! erased elements.
  1076. template<class Disposer>
  1077. iterator erase_and_dispose(const_iterator f, const_iterator l, Disposer disposer) BOOST_NOEXCEPT
  1078. { return this->erase_after_and_dispose(this->previous(f), l, disposer); }
  1079. //! <b>Requires</b>: Dereferencing iterator must yield
  1080. //! an lvalue of type value_type.
  1081. //!
  1082. //! <b>Effects</b>: Clears the list and inserts the range pointed by b and e.
  1083. //! No destructors or copy constructors are called.
  1084. //!
  1085. //! <b>Throws</b>: Nothing.
  1086. //!
  1087. //! <b>Complexity</b>: Linear to the number of elements inserted plus
  1088. //! linear to the elements contained in the list if it's a safe-mode
  1089. //! or auto-unlink value.
  1090. //! Linear to the number of elements inserted in the list otherwise.
  1091. //!
  1092. //! <b>Note</b>: Invalidates the iterators (but not the references)
  1093. //! to the erased elements.
  1094. template<class Iterator>
  1095. void assign(Iterator b, Iterator e)
  1096. {
  1097. this->clear();
  1098. this->insert_after(this->cbefore_begin(), b, e);
  1099. }
  1100. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1101. //!
  1102. //! <b>Requires</b>: Dereferencing iterator must yield
  1103. //! an lvalue of type value_type.
  1104. //!
  1105. //! <b>Effects</b>: Clears the list and inserts the range pointed by b and e.
  1106. //! No destructors or copy constructors are called.
  1107. //! Disposer::operator()(pointer) is called for the removed elements.
  1108. //!
  1109. //! <b>Throws</b>: Nothing.
  1110. //!
  1111. //! <b>Complexity</b>: Linear to the number of elements inserted plus
  1112. //! linear to the elements contained in the list.
  1113. //!
  1114. //! <b>Note</b>: Invalidates the iterators (but not the references)
  1115. //! to the erased elements.
  1116. template<class Iterator, class Disposer>
  1117. void dispose_and_assign(Disposer disposer, Iterator b, Iterator e)
  1118. {
  1119. this->clear_and_dispose(disposer);
  1120. this->insert_after(this->cbefore_begin(), b, e, disposer);
  1121. }
  1122. //! <b>Requires</b>: prev must point to an element contained by this list or
  1123. //! to the before_begin() element
  1124. //!
  1125. //! <b>Effects</b>: Transfers all the elements of list x to this list, after the
  1126. //! the element pointed by prev. No destructors or copy constructors are called.
  1127. //!
  1128. //! <b>Returns</b>: Nothing.
  1129. //!
  1130. //! <b>Throws</b>: Nothing.
  1131. //!
  1132. //! <b>Complexity</b>: In general, linear to the elements contained in x.
  1133. //! Constant-time if cache_last<> option is true and also constant-time if
  1134. //! linear<> option is true "this" is empty and "l" is not used.
  1135. //!
  1136. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1137. //! list. Iterators of this list and all the references are not invalidated.
  1138. //!
  1139. //! <b>Additional note</b>: If the optional parameter "l" is provided, it will be
  1140. //! assigned to the last spliced element or prev if x is empty.
  1141. //! This iterator can be used as new "prev" iterator for a new splice_after call.
  1142. //! that will splice new values after the previously spliced values.
  1143. void splice_after(const_iterator prev, slist_impl &x, const_iterator *l = 0) BOOST_NOEXCEPT
  1144. {
  1145. if(x.empty()){
  1146. if(l) *l = prev;
  1147. }
  1148. else if(linear && this->empty()){
  1149. this->swap(x);
  1150. if(l) *l = this->previous(this->cend());
  1151. }
  1152. else{
  1153. const_iterator last_x(x.previous(x.end())); //constant time if cache_last is active
  1154. node_ptr prev_n(prev.pointed_node());
  1155. node_ptr last_x_n(last_x.pointed_node());
  1156. BOOST_IF_CONSTEXPR(cache_last){
  1157. x.set_last_node(x.get_root_node());
  1158. if(node_traits::get_next(prev_n) == this->get_end_node()){
  1159. this->set_last_node(last_x_n);
  1160. }
  1161. }
  1162. node_algorithms::transfer_after( prev_n, x.before_begin().pointed_node(), last_x_n);
  1163. this->priv_size_traits().increase(x.priv_size_traits().get_size());
  1164. x.priv_size_traits().set_size(size_type(0));
  1165. if(l) *l = last_x;
  1166. }
  1167. }
  1168. //! <b>Requires</b>: prev must point to an element contained by this list or
  1169. //! to the before_begin() element. prev_ele must point to an element contained in list
  1170. //! x or must be x.before_begin().
  1171. //!
  1172. //! <b>Effects</b>: Transfers the element after prev_ele, from list x to this list,
  1173. //! after the element pointed by prev. No destructors or copy constructors are called.
  1174. //!
  1175. //! <b>Throws</b>: Nothing.
  1176. //!
  1177. //! <b>Complexity</b>: Constant.
  1178. //!
  1179. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1180. //! list. Iterators of this list and all the references are not invalidated.
  1181. void splice_after(const_iterator prev_pos, slist_impl &x, const_iterator prev_ele) BOOST_NOEXCEPT
  1182. {
  1183. const_iterator elem = prev_ele;
  1184. this->splice_after(prev_pos, x, prev_ele, ++elem, 1);
  1185. }
  1186. //! <b>Requires</b>: prev_pos must be a dereferenceable iterator in *this or be
  1187. //! before_begin(), and before_f and before_l belong to x and
  1188. //! ++before_f != x.end() && before_l != x.end().
  1189. //!
  1190. //! <b>Effects</b>: Transfers the range (before_f, before_l] from list x to this
  1191. //! list, after the element pointed by prev_pos.
  1192. //! No destructors or copy constructors are called.
  1193. //!
  1194. //! <b>Throws</b>: Nothing.
  1195. //!
  1196. //! <b>Complexity</b>: Linear to the number of elements transferred
  1197. //! if constant_time_size is true. Constant-time otherwise.
  1198. //!
  1199. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1200. //! list. Iterators of this list and all the references are not invalidated.
  1201. void splice_after(const_iterator prev_pos, slist_impl &x, const_iterator before_f, const_iterator before_l) BOOST_NOEXCEPT
  1202. {
  1203. BOOST_IF_CONSTEXPR(constant_time_size)
  1204. this->splice_after(prev_pos, x, before_f, before_l, node_algorithms::distance(before_f.pointed_node(), before_l.pointed_node()));
  1205. else
  1206. this->priv_splice_after
  1207. (prev_pos.pointed_node(), x, before_f.pointed_node(), before_l.pointed_node());
  1208. }
  1209. //! <b>Requires</b>: prev_pos must be a dereferenceable iterator in *this or be
  1210. //! before_begin(), and before_f and before_l belong to x and
  1211. //! ++before_f != x.end() && before_l != x.end() and
  1212. //! n == distance(before_f, before_l).
  1213. //!
  1214. //! <b>Effects</b>: Transfers the range (before_f, before_l] from list x to this
  1215. //! list, after the element pointed by p. No destructors or copy constructors are called.
  1216. //!
  1217. //! <b>Throws</b>: Nothing.
  1218. //!
  1219. //! <b>Complexity</b>: Constant time.
  1220. //!
  1221. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1222. //! list. Iterators of this list and all the references are not invalidated.
  1223. void splice_after(const_iterator prev_pos, slist_impl &x, const_iterator before_f, const_iterator before_l, size_type n) BOOST_NOEXCEPT
  1224. {
  1225. BOOST_INTRUSIVE_INVARIANT_ASSERT(node_algorithms::distance(before_f.pointed_node(), before_l.pointed_node()) == n);
  1226. (void)n;
  1227. this->priv_splice_after
  1228. (prev_pos.pointed_node(), x, before_f.pointed_node(), before_l.pointed_node());
  1229. BOOST_IF_CONSTEXPR(constant_time_size){
  1230. this->priv_size_traits().increase(n);
  1231. x.priv_size_traits().decrease(n);
  1232. }
  1233. }
  1234. //! <b>Requires</b>: it is an iterator to an element in *this.
  1235. //!
  1236. //! <b>Effects</b>: Transfers all the elements of list x to this list, before the
  1237. //! the element pointed by it. No destructors or copy constructors are called.
  1238. //!
  1239. //! <b>Returns</b>: Nothing.
  1240. //!
  1241. //! <b>Throws</b>: Nothing.
  1242. //!
  1243. //! <b>Complexity</b>: Linear to the elements contained in x plus linear to
  1244. //! the elements before it.
  1245. //! Linear to the elements before it if cache_last<> option is true.
  1246. //! Constant-time if cache_last<> option is true and it == end().
  1247. //!
  1248. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1249. //! list. Iterators of this list and all the references are not invalidated.
  1250. //!
  1251. //! <b>Additional note</b>: If the optional parameter "l" is provided, it will be
  1252. //! assigned to the last spliced element or prev if x is empty.
  1253. //! This iterator can be used as new "prev" iterator for a new splice_after call.
  1254. //! that will splice new values after the previously spliced values.
  1255. void splice(const_iterator it, slist_impl &x, const_iterator *l = 0) BOOST_NOEXCEPT
  1256. { this->splice_after(this->previous(it), x, l); }
  1257. //! <b>Requires</b>: it p must be a valid iterator of *this.
  1258. //! elem must point to an element contained in list
  1259. //! x.
  1260. //!
  1261. //! <b>Effects</b>: Transfers the element elem, from list x to this list,
  1262. //! before the element pointed by pos. No destructors or copy constructors are called.
  1263. //!
  1264. //! <b>Throws</b>: Nothing.
  1265. //!
  1266. //! <b>Complexity</b>: Linear to the elements before pos and before elem.
  1267. //! Linear to the elements before elem if cache_last<> option is true and pos == end().
  1268. //!
  1269. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1270. //! list. Iterators of this list and all the references are not invalidated.
  1271. void splice(const_iterator pos, slist_impl &x, const_iterator elem) BOOST_NOEXCEPT
  1272. { return this->splice_after(this->previous(pos), x, x.previous(elem)); }
  1273. //! <b>Requires</b>: pos must be a dereferenceable iterator in *this
  1274. //! and f and f belong to x and f and f a valid range on x.
  1275. //!
  1276. //! <b>Effects</b>: Transfers the range [f, l) from list x to this
  1277. //! list, before the element pointed by pos.
  1278. //! No destructors or copy constructors are called.
  1279. //!
  1280. //! <b>Throws</b>: Nothing.
  1281. //!
  1282. //! <b>Complexity</b>: Linear to the sum of elements before pos, f, and l
  1283. //! plus linear to the number of elements transferred if constant_time_size is true.
  1284. //! Linear to the sum of elements before f, and l
  1285. //! plus linear to the number of elements transferred if constant_time_size is true
  1286. //! if cache_last<> is true and pos == end()
  1287. //!
  1288. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1289. //! list. Iterators of this list and all the references are not invalidated.
  1290. void splice(const_iterator pos, slist_impl &x, const_iterator f, const_iterator l) BOOST_NOEXCEPT
  1291. { return this->splice_after(this->previous(pos), x, x.previous(f), x.previous(l)); }
  1292. //! <b>Requires</b>: pos must be a dereferenceable iterator in *this
  1293. //! and f and l belong to x and f and l a valid range on x.
  1294. //! n == distance(f, l).
  1295. //!
  1296. //! <b>Effects</b>: Transfers the range [f, l) from list x to this
  1297. //! list, before the element pointed by pos.
  1298. //! No destructors or copy constructors are called.
  1299. //!
  1300. //! <b>Throws</b>: Nothing.
  1301. //!
  1302. //! <b>Complexity</b>: Linear to the sum of elements before pos, f, and l.
  1303. //! Linear to the sum of elements before f and l
  1304. //! if cache_last<> is true and pos == end().
  1305. //!
  1306. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1307. //! list. Iterators of this list and all the references are not invalidated.
  1308. void splice(const_iterator pos, slist_impl &x, const_iterator f, const_iterator l, size_type n) BOOST_NOEXCEPT
  1309. { return this->splice_after(this->previous(pos), x, x.previous(f), x.previous(l), n); }
  1310. //! <b>Effects</b>: This function sorts the list *this according to operator<.
  1311. //! The sort is stable, that is, the relative order of equivalent elements is preserved.
  1312. //!
  1313. //! <b>Throws</b>: If value_traits::node_traits::node
  1314. //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
  1315. //! or the predicate throws. Basic guarantee.
  1316. //!
  1317. //! <b>Complexity</b>: The number of comparisons is approximately N log N, where N
  1318. //! is the list's size.
  1319. //!
  1320. //! <b>Note</b>: Iterators and references are not invalidated
  1321. template<class Predicate>
  1322. void sort(Predicate p)
  1323. {
  1324. if (node_traits::get_next(node_traits::get_next(this->get_root_node()))
  1325. != this->get_root_node()) {
  1326. slist_impl carry(this->priv_value_traits());
  1327. detail::array_initializer<slist_impl, 64> counter(this->priv_value_traits());
  1328. int fill = 0;
  1329. const_iterator last_inserted;
  1330. while(!this->empty()){
  1331. last_inserted = this->cbegin();
  1332. carry.splice_after(carry.cbefore_begin(), *this, this->cbefore_begin());
  1333. int i = 0;
  1334. while(i < fill && !counter[i].empty()) {
  1335. carry.swap(counter[i]);
  1336. carry.merge(counter[i++], p, &last_inserted);
  1337. }
  1338. BOOST_INTRUSIVE_INVARIANT_ASSERT(counter[i].empty());
  1339. const_iterator last_element(carry.previous(last_inserted, carry.end()));
  1340. BOOST_IF_CONSTEXPR(constant_time_size){
  1341. counter[i].splice_after( counter[i].cbefore_begin(), carry
  1342. , carry.cbefore_begin(), last_element
  1343. , carry.size());
  1344. }
  1345. else{
  1346. counter[i].splice_after( counter[i].cbefore_begin(), carry
  1347. , carry.cbefore_begin(), last_element);
  1348. }
  1349. if(i == fill)
  1350. ++fill;
  1351. }
  1352. for (int i = 1; i < fill; ++i)
  1353. counter[i].merge(counter[i-1], p, &last_inserted);
  1354. --fill;
  1355. const_iterator last_element(counter[fill].previous(last_inserted, counter[fill].end()));
  1356. BOOST_IF_CONSTEXPR(constant_time_size){
  1357. this->splice_after( cbefore_begin(), counter[fill], counter[fill].cbefore_begin()
  1358. , last_element, counter[fill].size());
  1359. }
  1360. else{
  1361. this->splice_after( cbefore_begin(), counter[fill], counter[fill].cbefore_begin()
  1362. , last_element);
  1363. }
  1364. }
  1365. }
  1366. //! <b>Requires</b>: p must be a comparison function that induces a strict weak
  1367. //! ordering and both *this and x must be sorted according to that ordering
  1368. //! The lists x and *this must be distinct.
  1369. //!
  1370. //! <b>Effects</b>: This function removes all of x's elements and inserts them
  1371. //! in order into *this. The merge is stable; that is, if an element from *this is
  1372. //! equivalent to one from x, then the element from *this will precede the one from x.
  1373. //!
  1374. //! <b>Throws</b>: If value_traits::node_traits::node
  1375. //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
  1376. //! or operator< throws. Basic guarantee.
  1377. //!
  1378. //! <b>Complexity</b>: This function is linear time: it performs at most
  1379. //! size() + x.size() - 1 comparisons.
  1380. //!
  1381. //! <b>Note</b>: Iterators and references are not invalidated.
  1382. void sort()
  1383. { this->sort(value_less<value_type>()); }
  1384. //! <b>Requires</b>: p must be a comparison function that induces a strict weak
  1385. //! ordering and both *this and x must be sorted according to that ordering
  1386. //! The lists x and *this must be distinct.
  1387. //!
  1388. //! <b>Effects</b>: This function removes all of x's elements and inserts them
  1389. //! in order into *this. The merge is stable; that is, if an element from *this is
  1390. //! equivalent to one from x, then the element from *this will precede the one from x.
  1391. //!
  1392. //! <b>Returns</b>: Nothing.
  1393. //!
  1394. //! <b>Throws</b>: If the predicate throws. Basic guarantee.
  1395. //!
  1396. //! <b>Complexity</b>: This function is linear time: it performs at most
  1397. //! size() + x.size() - 1 comparisons.
  1398. //!
  1399. //! <b>Note</b>: Iterators and references are not invalidated.
  1400. //!
  1401. //! <b>Additional note</b>: If optional "l" argument is passed, it is assigned
  1402. //! to an iterator to the last transferred value or end() is x is empty.
  1403. template<class Predicate>
  1404. void merge(slist_impl& x, Predicate p, const_iterator *l = 0)
  1405. {
  1406. const_iterator e(this->cend()), ex(x.cend()), bb(this->cbefore_begin()),
  1407. bb_next;
  1408. if(l) *l = e.unconst();
  1409. while(!x.empty()){
  1410. const_iterator ibx_next(x.cbefore_begin()), ibx(ibx_next++);
  1411. while (++(bb_next = bb) != e && !p(*ibx_next, *bb_next)){
  1412. bb = bb_next;
  1413. }
  1414. if(bb_next == e){
  1415. //Now transfer the rest to the end of the container
  1416. this->splice_after(bb, x, l);
  1417. break;
  1418. }
  1419. else{
  1420. size_type n(0);
  1421. do{
  1422. ibx = ibx_next; ++n;
  1423. } while(++(ibx_next = ibx) != ex && p(*ibx_next, *bb_next));
  1424. this->splice_after(bb, x, x.before_begin(), ibx, n);
  1425. if(l) *l = ibx;
  1426. }
  1427. }
  1428. }
  1429. //! <b>Effects</b>: This function removes all of x's elements and inserts them
  1430. //! in order into *this according to operator<. The merge is stable;
  1431. //! that is, if an element from *this is equivalent to one from x, then the element
  1432. //! from *this will precede the one from x.
  1433. //!
  1434. //! <b>Throws</b>: if operator< throws. Basic guarantee.
  1435. //!
  1436. //! <b>Complexity</b>: This function is linear time: it performs at most
  1437. //! size() + x.size() - 1 comparisons.
  1438. //!
  1439. //! <b>Note</b>: Iterators and references are not invalidated
  1440. void merge(slist_impl& x)
  1441. { this->merge(x, value_less<value_type>()); }
  1442. //! <b>Effects</b>: Reverses the order of elements in the list.
  1443. //!
  1444. //! <b>Throws</b>: Nothing.
  1445. //!
  1446. //! <b>Complexity</b>: This function is linear to the contained elements.
  1447. //!
  1448. //! <b>Note</b>: Iterators and references are not invalidated
  1449. void reverse() BOOST_NOEXCEPT
  1450. {
  1451. if(cache_last && !this->empty()){
  1452. this->set_last_node(node_traits::get_next(this->get_root_node()));
  1453. }
  1454. this->priv_reverse(detail::bool_<linear>());
  1455. }
  1456. //! <b>Effects</b>: Removes all the elements that compare equal to value.
  1457. //! No destructors are called.
  1458. //!
  1459. //! <b>Throws</b>: If operator== throws. Basic guarantee.
  1460. //!
  1461. //! <b>Complexity</b>: Linear time. It performs exactly size() comparisons for equality.
  1462. //!
  1463. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1464. //! and iterators to elements that are not removed remain valid. This function is
  1465. //! linear time: it performs exactly size() comparisons for equality.
  1466. void remove(const_reference value) BOOST_NOEXCEPT
  1467. { this->remove_if(detail::equal_to_value<const_reference>(value)); }
  1468. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1469. //!
  1470. //! <b>Effects</b>: Removes all the elements that compare equal to value.
  1471. //! Disposer::operator()(pointer) is called for every removed element.
  1472. //!
  1473. //! <b>Throws</b>: If operator== throws. Basic guarantee.
  1474. //!
  1475. //! <b>Complexity</b>: Linear time. It performs exactly size() comparisons for equality.
  1476. //!
  1477. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1478. //! and iterators to elements that are not removed remain valid.
  1479. template<class Disposer>
  1480. void remove_and_dispose(const_reference value, Disposer disposer) BOOST_NOEXCEPT
  1481. { this->remove_and_dispose_if(detail::equal_to_value<const_reference>(value), disposer); }
  1482. //! <b>Effects</b>: Removes all the elements for which a specified
  1483. //! predicate is satisfied. No destructors are called.
  1484. //!
  1485. //! <b>Throws</b>: If pred throws. Basic guarantee.
  1486. //!
  1487. //! <b>Complexity</b>: Linear time. It performs exactly size() calls to the predicate.
  1488. //!
  1489. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1490. //! and iterators to elements that are not removed remain valid.
  1491. template<class Pred>
  1492. void remove_if(Pred pred)
  1493. {
  1494. const node_ptr bbeg = this->get_root_node();
  1495. typename node_algorithms::stable_partition_info info;
  1496. node_algorithms::stable_partition
  1497. (bbeg, this->get_end_node(), detail::key_nodeptr_comp<Pred, value_traits>(pred, &this->priv_value_traits()), info);
  1498. //After cache last is set, slist invariants are preserved...
  1499. BOOST_IF_CONSTEXPR(cache_last){
  1500. this->set_last_node(info.new_last_node);
  1501. }
  1502. //...so erase can be safely called
  1503. this->erase_after( const_iterator(bbeg, this->priv_value_traits_ptr())
  1504. , const_iterator(info.beg_2st_partition, this->priv_value_traits_ptr())
  1505. , info.num_1st_partition);
  1506. }
  1507. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1508. //!
  1509. //! <b>Effects</b>: Removes all the elements for which a specified
  1510. //! predicate is satisfied.
  1511. //! Disposer::operator()(pointer) is called for every removed element.
  1512. //!
  1513. //! <b>Throws</b>: If pred throws. Basic guarantee.
  1514. //!
  1515. //! <b>Complexity</b>: Linear time. It performs exactly size() comparisons for equality.
  1516. //!
  1517. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1518. //! and iterators to elements that are not removed remain valid.
  1519. template<class Pred, class Disposer>
  1520. void remove_and_dispose_if(Pred pred, Disposer disposer)
  1521. {
  1522. const node_ptr bbeg = this->get_root_node();
  1523. typename node_algorithms::stable_partition_info info;
  1524. node_algorithms::stable_partition
  1525. (bbeg, this->get_end_node(), detail::key_nodeptr_comp<Pred, value_traits>(pred, &this->priv_value_traits()), info);
  1526. //After cache last is set, slist invariants are preserved...
  1527. BOOST_IF_CONSTEXPR(cache_last){
  1528. this->set_last_node(info.new_last_node);
  1529. }
  1530. //...so erase can be safely called
  1531. this->erase_after_and_dispose( const_iterator(bbeg, this->priv_value_traits_ptr())
  1532. , const_iterator(info.beg_2st_partition, this->priv_value_traits_ptr())
  1533. , disposer);
  1534. }
  1535. //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
  1536. //! elements that are equal from the list. No destructors are called.
  1537. //!
  1538. //! <b>Throws</b>: If operator== throws. Basic guarantee.
  1539. //!
  1540. //! <b>Complexity</b>: Linear time (size()-1) comparisons calls to pred()).
  1541. //!
  1542. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1543. //! and iterators to elements that are not removed remain valid.
  1544. void unique()
  1545. { this->unique_and_dispose(value_equal<value_type>(), detail::null_disposer()); }
  1546. //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
  1547. //! elements that satisfy some binary predicate from the list.
  1548. //! No destructors are called.
  1549. //!
  1550. //! <b>Throws</b>: If the predicate throws. Basic guarantee.
  1551. //!
  1552. //! <b>Complexity</b>: Linear time (size()-1) comparisons equality comparisons.
  1553. //!
  1554. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1555. //! and iterators to elements that are not removed remain valid.
  1556. template<class BinaryPredicate>
  1557. void unique(BinaryPredicate pred)
  1558. { this->unique_and_dispose(pred, detail::null_disposer()); }
  1559. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1560. //!
  1561. //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
  1562. //! elements that satisfy some binary predicate from the list.
  1563. //! Disposer::operator()(pointer) is called for every removed element.
  1564. //!
  1565. //! <b>Throws</b>: If operator== throws. Basic guarantee.
  1566. //!
  1567. //! <b>Complexity</b>: Linear time (size()-1) comparisons equality comparisons.
  1568. //!
  1569. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1570. //! and iterators to elements that are not removed remain valid.
  1571. template<class Disposer>
  1572. void unique_and_dispose(Disposer disposer)
  1573. { this->unique(value_equal<value_type>(), disposer); }
  1574. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1575. //!
  1576. //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
  1577. //! elements that satisfy some binary predicate from the list.
  1578. //! Disposer::operator()(pointer) is called for every removed element.
  1579. //!
  1580. //! <b>Throws</b>: If the predicate throws. Basic guarantee.
  1581. //!
  1582. //! <b>Complexity</b>: Linear time (size()-1) comparisons equality comparisons.
  1583. //!
  1584. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1585. //! and iterators to elements that are not removed remain valid.
  1586. template<class BinaryPredicate, class Disposer>
  1587. void unique_and_dispose(BinaryPredicate pred, Disposer disposer)
  1588. {
  1589. const_iterator end_n(this->cend());
  1590. const_iterator bcur(this->cbegin());
  1591. if(bcur != end_n){
  1592. const_iterator cur(bcur);
  1593. ++cur;
  1594. while(cur != end_n) {
  1595. if (pred(*bcur, *cur)){
  1596. cur = this->erase_after_and_dispose(bcur, disposer);
  1597. }
  1598. else{
  1599. bcur = cur;
  1600. ++cur;
  1601. }
  1602. }
  1603. BOOST_IF_CONSTEXPR(cache_last){
  1604. this->set_last_node(bcur.pointed_node());
  1605. }
  1606. }
  1607. }
  1608. //! <b>Requires</b>: value must be a reference to a value inserted in a list.
  1609. //!
  1610. //! <b>Effects</b>: This function returns a const_iterator pointing to the element
  1611. //!
  1612. //! <b>Throws</b>: Nothing.
  1613. //!
  1614. //! <b>Complexity</b>: Constant time.
  1615. //!
  1616. //! <b>Note</b>: Iterators and references are not invalidated.
  1617. //! This static function is available only if the <i>value traits</i>
  1618. //! is stateless.
  1619. static iterator s_iterator_to(reference value) BOOST_NOEXCEPT
  1620. {
  1621. BOOST_INTRUSIVE_STATIC_ASSERT((!stateful_value_traits));
  1622. return iterator (value_traits::to_node_ptr(value), const_value_traits_ptr());
  1623. }
  1624. //! <b>Requires</b>: value must be a const reference to a value inserted in a list.
  1625. //!
  1626. //! <b>Effects</b>: This function returns an iterator pointing to the element.
  1627. //!
  1628. //! <b>Throws</b>: Nothing.
  1629. //!
  1630. //! <b>Complexity</b>: Constant time.
  1631. //!
  1632. //! <b>Note</b>: Iterators and references are not invalidated.
  1633. //! This static function is available only if the <i>value traits</i>
  1634. //! is stateless.
  1635. static const_iterator s_iterator_to(const_reference value) BOOST_NOEXCEPT
  1636. {
  1637. BOOST_INTRUSIVE_STATIC_ASSERT((!stateful_value_traits));
  1638. reference r =*detail::uncast(pointer_traits<const_pointer>::pointer_to(value));
  1639. return const_iterator(value_traits::to_node_ptr(r), const_value_traits_ptr());
  1640. }
  1641. //! <b>Requires</b>: value must be a reference to a value inserted in a list.
  1642. //!
  1643. //! <b>Effects</b>: This function returns a const_iterator pointing to the element
  1644. //!
  1645. //! <b>Throws</b>: Nothing.
  1646. //!
  1647. //! <b>Complexity</b>: Constant time.
  1648. //!
  1649. //! <b>Note</b>: Iterators and references are not invalidated.
  1650. iterator iterator_to(reference value) BOOST_NOEXCEPT
  1651. {
  1652. BOOST_INTRUSIVE_INVARIANT_ASSERT(linear || !node_algorithms::inited(this->priv_value_traits().to_node_ptr(value)));
  1653. return iterator (this->priv_value_traits().to_node_ptr(value), this->priv_value_traits_ptr());
  1654. }
  1655. //! <b>Requires</b>: value must be a const reference to a value inserted in a list.
  1656. //!
  1657. //! <b>Effects</b>: This function returns an iterator pointing to the element.
  1658. //!
  1659. //! <b>Throws</b>: Nothing.
  1660. //!
  1661. //! <b>Complexity</b>: Constant time.
  1662. //!
  1663. //! <b>Note</b>: Iterators and references are not invalidated.
  1664. const_iterator iterator_to(const_reference value) const BOOST_NOEXCEPT
  1665. {
  1666. reference r =*detail::uncast(pointer_traits<const_pointer>::pointer_to(value));
  1667. BOOST_INTRUSIVE_INVARIANT_ASSERT (linear || !node_algorithms::inited(this->priv_value_traits().to_node_ptr(r)));
  1668. return const_iterator(this->priv_value_traits().to_node_ptr(r), this->priv_value_traits_ptr());
  1669. }
  1670. //! <b>Returns</b>: The iterator to the element before i in the list.
  1671. //! Returns the end-iterator, if either i is the begin-iterator or the
  1672. //! list is empty.
  1673. //!
  1674. //! <b>Throws</b>: Nothing.
  1675. //!
  1676. //! <b>Complexity</b>: Linear to the number of elements before i.
  1677. //! Constant if cache_last<> is true and i == end().
  1678. iterator previous(iterator i) BOOST_NOEXCEPT
  1679. { return this->previous(this->cbefore_begin(), i); }
  1680. //! <b>Returns</b>: The const_iterator to the element before i in the list.
  1681. //! Returns the end-const_iterator, if either i is the begin-const_iterator or
  1682. //! the list is empty.
  1683. //!
  1684. //! <b>Throws</b>: Nothing.
  1685. //!
  1686. //! <b>Complexity</b>: Linear to the number of elements before i.
  1687. //! Constant if cache_last<> is true and i == end().
  1688. const_iterator previous(const_iterator i) const BOOST_NOEXCEPT
  1689. { return this->previous(this->cbefore_begin(), i); }
  1690. //! <b>Returns</b>: The iterator to the element before i in the list,
  1691. //! starting the search on element after prev_from.
  1692. //! Returns the end-iterator, if either i is the begin-iterator or the
  1693. //! list is empty.
  1694. //!
  1695. //! <b>Throws</b>: Nothing.
  1696. //!
  1697. //! <b>Complexity</b>: Linear to the number of elements before i.
  1698. //! Constant if cache_last<> is true and i == end().
  1699. iterator previous(const_iterator prev_from, iterator i) BOOST_NOEXCEPT
  1700. { return this->previous(prev_from, const_iterator(i)).unconst(); }
  1701. //! <b>Returns</b>: The const_iterator to the element before i in the list,
  1702. //! starting the search on element after prev_from.
  1703. //! Returns the end-const_iterator, if either i is the begin-const_iterator or
  1704. //! the list is empty.
  1705. //!
  1706. //! <b>Throws</b>: Nothing.
  1707. //!
  1708. //! <b>Complexity</b>: Linear to the number of elements before i.
  1709. //! Constant if cache_last<> is true and i == end().
  1710. const_iterator previous(const_iterator prev_from, const_iterator i) const BOOST_NOEXCEPT
  1711. {
  1712. if(cache_last && (i.pointed_node() == this->get_end_node())){
  1713. return const_iterator(detail::uncast(this->get_last_node()), this->priv_value_traits_ptr());
  1714. }
  1715. return const_iterator
  1716. (node_algorithms::get_previous_node
  1717. (prev_from.pointed_node(), i.pointed_node()), this->priv_value_traits_ptr());
  1718. }
  1719. ///@cond
  1720. //! <b>Requires</b>: prev_pos must be a dereferenceable iterator in *this or be
  1721. //! before_begin(), and f and before_l belong to another slist.
  1722. //!
  1723. //! <b>Effects</b>: Transfers the range [f, before_l] to this
  1724. //! list, after the element pointed by prev_pos.
  1725. //! No destructors or copy constructors are called.
  1726. //!
  1727. //! <b>Throws</b>: Nothing.
  1728. //!
  1729. //! <b>Complexity</b>: Linear to the number of elements transferred
  1730. //! if constant_time_size is true. Constant-time otherwise.
  1731. //!
  1732. //! <b>Note</b>: Iterators of values obtained from the list that owned f and before_l now
  1733. //! point to elements of this list. Iterators of this list and all the references are not invalidated.
  1734. //!
  1735. //! <b>Warning</b>: Experimental function, don't use it!
  1736. void incorporate_after(const_iterator prev_pos, node_ptr f, node_ptr before_l) BOOST_NOEXCEPT
  1737. {
  1738. BOOST_IF_CONSTEXPR(constant_time_size)
  1739. this->incorporate_after(prev_pos, f, before_l, node_algorithms::distance(f.pointed_node(), before_l.pointed_node())+1);
  1740. else
  1741. this->priv_incorporate_after(prev_pos.pointed_node(), f, before_l);
  1742. }
  1743. //! <b>Requires</b>: prev_pos must be a dereferenceable iterator in *this or be
  1744. //! before_begin(), and f and before_l belong to another slist.
  1745. //! n == distance(f, before_l) + 1.
  1746. //!
  1747. //! <b>Effects</b>: Transfers the range [f, before_l] to this
  1748. //! list, after the element pointed by prev_pos.
  1749. //! No destructors or copy constructors are called.
  1750. //!
  1751. //! <b>Throws</b>: Nothing.
  1752. //!
  1753. //! <b>Complexity</b>: Constant time.
  1754. //!
  1755. //! <b>Note</b>: Iterators of values obtained from the list that owned f and before_l now
  1756. //! point to elements of this list. Iterators of this list and all the references are not invalidated.
  1757. //!
  1758. //! <b>Warning</b>: Experimental function, don't use it!
  1759. void incorporate_after(const_iterator prev_pos, node_ptr f, node_ptr before_l, size_type n) BOOST_NOEXCEPT
  1760. {
  1761. if(n){
  1762. BOOST_INTRUSIVE_INVARIANT_ASSERT(n > 0);
  1763. BOOST_INTRUSIVE_INVARIANT_ASSERT
  1764. (size_type(boost::intrusive::iterator_distance
  1765. ( iterator(f, this->priv_value_traits_ptr())
  1766. , iterator(before_l, this->priv_value_traits_ptr())))
  1767. +1 == n);
  1768. this->priv_incorporate_after(prev_pos.pointed_node(), f, before_l);
  1769. BOOST_IF_CONSTEXPR(constant_time_size){
  1770. this->priv_size_traits().increase(n);
  1771. }
  1772. }
  1773. }
  1774. ///@endcond
  1775. //! <b>Effects</b>: Asserts the integrity of the container.
  1776. //!
  1777. //! <b>Complexity</b>: Linear time.
  1778. //!
  1779. //! <b>Note</b>: The method has no effect when asserts are turned off (e.g., with NDEBUG).
  1780. //! Experimental function, interface might change in future versions.
  1781. void check() const
  1782. {
  1783. const_node_ptr header_ptr = get_root_node();
  1784. // header's next is never null
  1785. BOOST_INTRUSIVE_INVARIANT_ASSERT(node_traits::get_next(header_ptr));
  1786. if (node_traits::get_next(header_ptr) == header_ptr)
  1787. {
  1788. BOOST_INTRUSIVE_INVARIANT_ASSERT(!constant_time_size || this->priv_size_traits().get_size() == 0);
  1789. return;
  1790. }
  1791. size_t node_count = 0; (void)node_count;
  1792. const_node_ptr p = header_ptr;
  1793. while (true)
  1794. {
  1795. const_node_ptr next_p = node_traits::get_next(p);
  1796. BOOST_IF_CONSTEXPR(!linear)
  1797. {
  1798. BOOST_INTRUSIVE_INVARIANT_ASSERT(next_p);
  1799. }
  1800. else
  1801. {
  1802. BOOST_INTRUSIVE_INVARIANT_ASSERT(next_p != header_ptr);
  1803. }
  1804. if ((!linear && next_p == header_ptr) || (linear && !next_p))
  1805. {
  1806. BOOST_INTRUSIVE_INVARIANT_ASSERT(!cache_last || get_last_node() == p);
  1807. break;
  1808. }
  1809. p = next_p;
  1810. ++node_count;
  1811. }
  1812. BOOST_INTRUSIVE_INVARIANT_ASSERT(!constant_time_size || this->priv_size_traits().get_size() == node_count);
  1813. }
  1814. friend bool operator==(const slist_impl &x, const slist_impl &y)
  1815. {
  1816. if(constant_time_size && x.size() != y.size()){
  1817. return false;
  1818. }
  1819. return ::boost::intrusive::algo_equal(x.cbegin(), x.cend(), y.cbegin(), y.cend());
  1820. }
  1821. friend bool operator!=(const slist_impl &x, const slist_impl &y)
  1822. { return !(x == y); }
  1823. friend bool operator<(const slist_impl &x, const slist_impl &y)
  1824. { return ::boost::intrusive::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); }
  1825. friend bool operator>(const slist_impl &x, const slist_impl &y)
  1826. { return y < x; }
  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 !(x < y); }
  1831. friend void swap(slist_impl &x, slist_impl &y)
  1832. { x.swap(y); }
  1833. private:
  1834. void priv_splice_after(node_ptr prev_pos_n, slist_impl &x, node_ptr before_f_n, node_ptr before_l_n)
  1835. {
  1836. if (cache_last && (before_f_n != before_l_n)){
  1837. if(prev_pos_n == this->get_last_node()){
  1838. this->set_last_node(before_l_n);
  1839. }
  1840. if(&x != this && node_traits::get_next(before_l_n) == x.get_end_node()){
  1841. x.set_last_node(before_f_n);
  1842. }
  1843. }
  1844. node_algorithms::transfer_after(prev_pos_n, before_f_n, before_l_n);
  1845. }
  1846. void priv_incorporate_after(node_ptr prev_pos_n, node_ptr first_n, node_ptr before_l_n)
  1847. {
  1848. BOOST_IF_CONSTEXPR(cache_last){
  1849. if(prev_pos_n == this->get_last_node()){
  1850. this->set_last_node(before_l_n);
  1851. }
  1852. }
  1853. node_algorithms::incorporate_after(prev_pos_n, first_n, before_l_n);
  1854. }
  1855. void priv_reverse(detail::bool_<false>)
  1856. { node_algorithms::reverse(this->get_root_node()); }
  1857. void priv_reverse(detail::bool_<true>)
  1858. {
  1859. node_ptr new_first = node_algorithms::reverse
  1860. (node_traits::get_next(this->get_root_node()));
  1861. node_traits::set_next(this->get_root_node(), new_first);
  1862. }
  1863. void priv_shift_backwards(size_type n, detail::bool_<false>)
  1864. {
  1865. node_ptr l = node_algorithms::move_forward(this->get_root_node(), (std::size_t)n);
  1866. if(cache_last && l){
  1867. this->set_last_node(l);
  1868. }
  1869. }
  1870. void priv_shift_backwards(size_type n, detail::bool_<true>)
  1871. {
  1872. typename node_algorithms::node_pair ret(
  1873. node_algorithms::move_first_n_forward
  1874. (node_traits::get_next(this->get_root_node()), (std::size_t)n));
  1875. if(ret.first){
  1876. node_traits::set_next(this->get_root_node(), ret.first);
  1877. BOOST_IF_CONSTEXPR(cache_last){
  1878. this->set_last_node(ret.second);
  1879. }
  1880. }
  1881. }
  1882. void priv_shift_forward(size_type n, detail::bool_<false>)
  1883. {
  1884. node_ptr l = node_algorithms::move_backwards(this->get_root_node(), (std::size_t)n);
  1885. if(cache_last && l){
  1886. this->set_last_node(l);
  1887. }
  1888. }
  1889. void priv_shift_forward(size_type n, detail::bool_<true>)
  1890. {
  1891. typename node_algorithms::node_pair ret(
  1892. node_algorithms::move_first_n_backwards
  1893. (node_traits::get_next(this->get_root_node()), (std::size_t)n));
  1894. if(ret.first){
  1895. node_traits::set_next(this->get_root_node(), ret.first);
  1896. BOOST_IF_CONSTEXPR(cache_last){
  1897. this->set_last_node(ret.second);
  1898. }
  1899. }
  1900. }
  1901. static void priv_swap_cache_last(slist_impl *this_impl, slist_impl *other_impl)
  1902. {
  1903. bool other_was_empty = false;
  1904. if(this_impl->empty()){
  1905. //Check if both are empty or
  1906. if(other_impl->empty())
  1907. return;
  1908. //If this is empty swap pointers
  1909. slist_impl *tmp = this_impl;
  1910. this_impl = other_impl;
  1911. other_impl = tmp;
  1912. other_was_empty = true;
  1913. }
  1914. else{
  1915. other_was_empty = other_impl->empty();
  1916. }
  1917. //Precondition: this is not empty
  1918. node_ptr other_old_last(other_impl->get_last_node());
  1919. node_ptr other_bfirst(other_impl->get_root_node());
  1920. node_ptr this_bfirst(this_impl->get_root_node());
  1921. node_ptr this_old_last(this_impl->get_last_node());
  1922. //Move all nodes from this to other's beginning
  1923. node_algorithms::transfer_after(other_bfirst, this_bfirst, this_old_last);
  1924. other_impl->set_last_node(this_old_last);
  1925. if(other_was_empty){
  1926. this_impl->set_last_node(this_bfirst);
  1927. }
  1928. else{
  1929. //Move trailing nodes from other to this
  1930. node_algorithms::transfer_after(this_bfirst, this_old_last, other_old_last);
  1931. this_impl->set_last_node(other_old_last);
  1932. }
  1933. }
  1934. //circular version
  1935. static void priv_swap_lists(node_ptr this_node, node_ptr other_node, detail::bool_<false>)
  1936. { node_algorithms::swap_nodes(this_node, other_node); }
  1937. //linear version
  1938. static void priv_swap_lists(node_ptr this_node, node_ptr other_node, detail::bool_<true>)
  1939. { node_algorithms::swap_trailing_nodes(this_node, other_node); }
  1940. static slist_impl &priv_container_from_end_iterator(const const_iterator &end_iterator)
  1941. {
  1942. //Obtaining the container from the end iterator is not possible with linear
  1943. //singly linked lists (because "end" is represented by the null pointer)
  1944. BOOST_INTRUSIVE_STATIC_ASSERT(!linear);
  1945. BOOST_INTRUSIVE_STATIC_ASSERT((has_container_from_iterator));
  1946. node_ptr p = end_iterator.pointed_node();
  1947. header_holder_type* h = header_holder_type::get_holder(p);
  1948. header_holder_plus_last_t* hpl = detail::parent_from_member< header_holder_plus_last_t, header_holder_type>
  1949. (h, &header_holder_plus_last_t::header_holder_);
  1950. root_plus_size* r = static_cast< root_plus_size* >(hpl);
  1951. data_t *d = detail::parent_from_member<data_t, root_plus_size>
  1952. ( r, &data_t::root_plus_size_);
  1953. slist_impl *s = detail::parent_from_member<slist_impl, data_t>(d, &slist_impl::data_);
  1954. return *s;
  1955. }
  1956. };
  1957. //! Helper metafunction to define a \c slist that yields to the same type when the
  1958. //! same options (either explicitly or implicitly) are used.
  1959. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1960. template<class T, class ...Options>
  1961. #else
  1962. template<class T, class O1 = void, class O2 = void, class O3 = void, class O4 = void, class O5 = void, class O6 = void>
  1963. #endif
  1964. struct make_slist
  1965. {
  1966. /// @cond
  1967. typedef typename pack_options
  1968. < slist_defaults,
  1969. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1970. O1, O2, O3, O4, O5, O6
  1971. #else
  1972. Options...
  1973. #endif
  1974. >::type packed_options;
  1975. typedef typename detail::get_value_traits
  1976. <T, typename packed_options::proto_value_traits>::type value_traits;
  1977. typedef slist_impl
  1978. < value_traits
  1979. , typename packed_options::size_type
  1980. , (std::size_t(packed_options::linear)*slist_bool_flags::linear_pos)
  1981. |(std::size_t(packed_options::constant_time_size)*slist_bool_flags::constant_time_size_pos)
  1982. |(std::size_t(packed_options::cache_last)*slist_bool_flags::cache_last_pos)
  1983. , typename packed_options::header_holder_type
  1984. > implementation_defined;
  1985. /// @endcond
  1986. typedef implementation_defined type;
  1987. };
  1988. #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  1989. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1990. template<class T, class O1, class O2, class O3, class O4, class O5, class O6>
  1991. #else
  1992. template<class T, class ...Options>
  1993. #endif
  1994. class slist
  1995. : public make_slist<T,
  1996. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1997. O1, O2, O3, O4, O5, O6
  1998. #else
  1999. Options...
  2000. #endif
  2001. >::type
  2002. {
  2003. typedef typename make_slist
  2004. <T,
  2005. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  2006. O1, O2, O3, O4, O5, O6
  2007. #else
  2008. Options...
  2009. #endif
  2010. >::type Base;
  2011. //Assert if passed value traits are compatible with the type
  2012. BOOST_INTRUSIVE_STATIC_ASSERT((detail::is_same<typename Base::value_traits::value_type, T>::value));
  2013. BOOST_MOVABLE_BUT_NOT_COPYABLE(slist)
  2014. public:
  2015. typedef typename Base::value_traits value_traits;
  2016. typedef typename Base::iterator iterator;
  2017. typedef typename Base::const_iterator const_iterator;
  2018. typedef typename Base::size_type size_type;
  2019. typedef typename Base::node_ptr node_ptr;
  2020. inline slist()
  2021. : Base()
  2022. {}
  2023. inline explicit slist(const value_traits &v_traits)
  2024. : Base(v_traits)
  2025. {}
  2026. struct incorporate_t{};
  2027. inline slist( node_ptr f, node_ptr before_l
  2028. , size_type n, const value_traits &v_traits = value_traits())
  2029. : Base(f, before_l, n, v_traits)
  2030. {}
  2031. template<class Iterator>
  2032. inline slist(Iterator b, Iterator e, const value_traits &v_traits = value_traits())
  2033. : Base(b, e, v_traits)
  2034. {}
  2035. inline slist(BOOST_RV_REF(slist) x)
  2036. : Base(BOOST_MOVE_BASE(Base, x))
  2037. {}
  2038. inline slist& operator=(BOOST_RV_REF(slist) x)
  2039. { return static_cast<slist &>(this->Base::operator=(BOOST_MOVE_BASE(Base, x))); }
  2040. template <class Cloner, class Disposer>
  2041. inline void clone_from(const slist &src, Cloner cloner, Disposer disposer)
  2042. { Base::clone_from(src, cloner, disposer); }
  2043. template <class Cloner, class Disposer>
  2044. inline void clone_from(BOOST_RV_REF(slist) src, Cloner cloner, Disposer disposer)
  2045. { Base::clone_from(BOOST_MOVE_BASE(Base, src), cloner, disposer); }
  2046. inline static slist &container_from_end_iterator(iterator end_iterator) BOOST_NOEXCEPT
  2047. { return static_cast<slist &>(Base::container_from_end_iterator(end_iterator)); }
  2048. inline static const slist &container_from_end_iterator(const_iterator end_iterator) BOOST_NOEXCEPT
  2049. { return static_cast<const slist &>(Base::container_from_end_iterator(end_iterator)); }
  2050. };
  2051. #endif
  2052. } //namespace intrusive
  2053. } //namespace boost
  2054. #include <boost/intrusive/detail/config_end.hpp>
  2055. #endif //BOOST_INTRUSIVE_SLIST_HPP