slist.hpp 88 KB

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