slist.hpp 86 KB

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