list.hpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Olaf Krzikalla 2004-2006.
  4. // (C) Copyright Ion Gaztanaga 2006-2014
  5. //
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // See http://www.boost.org/libs/intrusive for documentation.
  11. //
  12. /////////////////////////////////////////////////////////////////////////////
  13. #ifndef BOOST_INTRUSIVE_LIST_HPP
  14. #define BOOST_INTRUSIVE_LIST_HPP
  15. #include <boost/intrusive/detail/config_begin.hpp>
  16. #include <boost/intrusive/intrusive_fwd.hpp>
  17. #include <boost/intrusive/detail/assert.hpp>
  18. #include <boost/intrusive/list_hook.hpp>
  19. #include <boost/intrusive/circular_list_algorithms.hpp>
  20. #include <boost/intrusive/pointer_traits.hpp>
  21. #include <boost/intrusive/detail/mpl.hpp>
  22. #include <boost/intrusive/link_mode.hpp>
  23. #include <boost/intrusive/detail/get_value_traits.hpp>
  24. #include <boost/intrusive/detail/is_stateful_value_traits.hpp>
  25. #include <boost/intrusive/detail/default_header_holder.hpp>
  26. #include <boost/intrusive/detail/reverse_iterator.hpp>
  27. #include <boost/intrusive/detail/uncast.hpp>
  28. #include <boost/intrusive/detail/list_iterator.hpp>
  29. #include <boost/intrusive/detail/array_initializer.hpp>
  30. #include <boost/intrusive/detail/exception_disposer.hpp>
  31. #include <boost/intrusive/detail/equal_to_value.hpp>
  32. #include <boost/intrusive/detail/key_nodeptr_comp.hpp>
  33. #include <boost/intrusive/detail/simple_disposers.hpp>
  34. #include <boost/intrusive/detail/size_holder.hpp>
  35. #include <boost/intrusive/detail/algorithm.hpp>
  36. #include <boost/move/utility_core.hpp>
  37. #include <boost/intrusive/detail/value_functors.hpp>
  38. #include <cstddef> //std::size_t, etc.
  39. #if defined(BOOST_HAS_PRAGMA_ONCE)
  40. # pragma once
  41. #endif
  42. namespace boost {
  43. namespace intrusive {
  44. /// @cond
  45. struct default_list_hook_applier
  46. { template <class T> struct apply{ typedef typename T::default_list_hook type; }; };
  47. template<>
  48. struct is_default_hook_tag<default_list_hook_applier>
  49. { static const bool value = true; };
  50. struct list_defaults
  51. {
  52. typedef default_list_hook_applier proto_value_traits;
  53. static const bool constant_time_size = true;
  54. typedef std::size_t size_type;
  55. typedef void header_holder_type;
  56. };
  57. /// @endcond
  58. //! The class template list is an intrusive container that mimics most of the
  59. //! interface of std::list as described in the C++ standard.
  60. //!
  61. //! The template parameter \c T is the type to be managed by the container.
  62. //! The user can specify additional options and if no options are provided
  63. //! default options are used.
  64. //!
  65. //! The container supports the following options:
  66. //! \c base_hook<>/member_hook<>/value_traits<>,
  67. //! \c constant_time_size<> and \c size_type<>.
  68. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  69. template<class T, class ...Options>
  70. #else
  71. template <class ValueTraits, class SizeType, bool ConstantTimeSize, typename HeaderHolder>
  72. #endif
  73. class list_impl
  74. {
  75. //Public typedefs
  76. public:
  77. typedef ValueTraits value_traits;
  78. typedef typename value_traits::pointer pointer;
  79. typedef typename value_traits::const_pointer const_pointer;
  80. typedef typename pointer_traits<pointer>::element_type value_type;
  81. typedef typename pointer_traits<pointer>::reference reference;
  82. typedef typename pointer_traits<const_pointer>::reference const_reference;
  83. typedef typename pointer_traits<pointer>::difference_type difference_type;
  84. typedef SizeType size_type;
  85. typedef list_iterator<value_traits, false> iterator;
  86. typedef list_iterator<value_traits, true> const_iterator;
  87. typedef boost::intrusive::reverse_iterator<iterator> reverse_iterator;
  88. typedef boost::intrusive::reverse_iterator<const_iterator> const_reverse_iterator;
  89. typedef typename value_traits::node_traits node_traits;
  90. typedef typename node_traits::node node;
  91. typedef typename node_traits::node_ptr node_ptr;
  92. typedef typename node_traits::const_node_ptr const_node_ptr;
  93. typedef circular_list_algorithms<node_traits> node_algorithms;
  94. typedef typename detail::get_header_holder_type
  95. < value_traits, HeaderHolder >::type header_holder_type;
  96. static const bool constant_time_size = ConstantTimeSize;
  97. static const bool stateful_value_traits = detail::is_stateful_value_traits<value_traits>::value;
  98. static const bool has_container_from_iterator =
  99. detail::is_same< header_holder_type, detail::default_header_holder< node_traits > >::value;
  100. /// @cond
  101. private:
  102. typedef detail::size_holder<constant_time_size, size_type> size_traits;
  103. //noncopyable
  104. BOOST_MOVABLE_BUT_NOT_COPYABLE(list_impl)
  105. static const bool safemode_or_autounlink = is_safe_autounlink<value_traits::link_mode>::value;
  106. //Constant-time size is incompatible with auto-unlink hooks!
  107. BOOST_INTRUSIVE_STATIC_ASSERT(!(constant_time_size &&
  108. ((int)value_traits::link_mode == (int)auto_unlink)
  109. ));
  110. inline node_ptr get_root_node()
  111. { return data_.root_plus_size_.m_header.get_node(); }
  112. inline const_node_ptr get_root_node() const
  113. { return data_.root_plus_size_.m_header.get_node(); }
  114. struct root_plus_size : public size_traits
  115. {
  116. header_holder_type m_header;
  117. };
  118. struct data_t : public value_traits
  119. {
  120. typedef typename list_impl::value_traits value_traits;
  121. inline explicit data_t(const value_traits &val_traits)
  122. : value_traits(val_traits)
  123. {}
  124. root_plus_size root_plus_size_;
  125. } data_;
  126. inline size_traits &priv_size_traits() BOOST_NOEXCEPT
  127. { return data_.root_plus_size_; }
  128. inline const size_traits &priv_size_traits() const BOOST_NOEXCEPT
  129. { return data_.root_plus_size_; }
  130. inline const value_traits &priv_value_traits() const BOOST_NOEXCEPT
  131. { return data_; }
  132. inline value_traits &priv_value_traits() BOOST_NOEXCEPT
  133. { return data_; }
  134. typedef typename boost::intrusive::value_traits_pointers
  135. <ValueTraits>::const_value_traits_ptr const_value_traits_ptr;
  136. inline const_value_traits_ptr priv_value_traits_ptr() const BOOST_NOEXCEPT
  137. { return pointer_traits<const_value_traits_ptr>::pointer_to(this->priv_value_traits()); }
  138. /// @endcond
  139. public:
  140. //! <b>Effects</b>: constructs an empty list.
  141. //!
  142. //! <b>Complexity</b>: Constant
  143. //!
  144. //! <b>Throws</b>: If value_traits::node_traits::node
  145. //! constructor throws (this does not happen with predefined Boost.Intrusive hooks).
  146. list_impl()
  147. : data_(value_traits())
  148. {
  149. this->priv_size_traits().set_size(size_type(0));
  150. node_algorithms::init_header(this->get_root_node());
  151. }
  152. //! <b>Effects</b>: constructs an empty list.
  153. //!
  154. //! <b>Complexity</b>: Constant
  155. //!
  156. //! <b>Throws</b>: If value_traits::node_traits::node
  157. //! constructor throws (this does not happen with predefined Boost.Intrusive hooks).
  158. explicit list_impl(const value_traits &v_traits)
  159. : data_(v_traits)
  160. {
  161. this->priv_size_traits().set_size(size_type(0));
  162. node_algorithms::init_header(this->get_root_node());
  163. }
  164. //! <b>Requires</b>: Dereferencing iterator must yield an lvalue of type value_type.
  165. //!
  166. //! <b>Effects</b>: Constructs a list equal to the range [first,last).
  167. //!
  168. //! <b>Complexity</b>: Linear in distance(b, e). No copy constructors are called.
  169. //!
  170. //! <b>Throws</b>: If value_traits::node_traits::node
  171. //! constructor throws (this does not happen with predefined Boost.Intrusive hooks).
  172. template<class Iterator>
  173. list_impl(Iterator b, Iterator e, const value_traits &v_traits = value_traits())
  174. : data_(v_traits)
  175. {
  176. //nothrow, no need to rollback to release elements on exception
  177. this->priv_size_traits().set_size(size_type(0));
  178. node_algorithms::init_header(this->get_root_node());
  179. //nothrow, no need to rollback to release elements on exception
  180. this->insert(this->cend(), b, e);
  181. }
  182. //! <b>Effects</b>: Constructs a container moving resources from another container.
  183. //! Internal value traits are move constructed and
  184. //! nodes belonging to x (except the node representing the "end") are linked to *this.
  185. //!
  186. //! <b>Complexity</b>: Constant.
  187. //!
  188. //! <b>Throws</b>: If value_traits::node_traits::node's
  189. //! move constructor throws (this does not happen with predefined Boost.Intrusive hooks)
  190. //! or the move constructor of value traits throws.
  191. list_impl(BOOST_RV_REF(list_impl) x)
  192. : data_(::boost::move(x.priv_value_traits()))
  193. {
  194. this->priv_size_traits().set_size(size_type(0));
  195. node_algorithms::init_header(this->get_root_node());
  196. //nothrow, no need to rollback to release elements on exception
  197. this->swap(x);
  198. }
  199. //! <b>Effects</b>: Equivalent to swap
  200. //!
  201. list_impl& operator=(BOOST_RV_REF(list_impl) x)
  202. { this->swap(x); return *this; }
  203. //! <b>Effects</b>: If it's not a safe-mode or an auto-unlink value_type
  204. //! the destructor does nothing
  205. //! (ie. no code is generated). Otherwise it detaches all elements from this.
  206. //! In this case the objects in the list are not deleted (i.e. no destructors
  207. //! are called), but the hooks according to the ValueTraits template parameter
  208. //! are set to their default value.
  209. //!
  210. //! <b>Throws</b>: Nothing.
  211. //!
  212. //! <b>Complexity</b>: Linear to the number of elements in the list, if
  213. //! it's a safe-mode or auto-unlink value . Otherwise constant.
  214. ~list_impl()
  215. {
  216. BOOST_IF_CONSTEXPR(is_safe_autounlink<ValueTraits::link_mode>::value){
  217. this->clear();
  218. node_algorithms::init(this->get_root_node());
  219. }
  220. }
  221. //! <b>Requires</b>: value must be an lvalue.
  222. //!
  223. //! <b>Effects</b>: Inserts the value in the back of the list.
  224. //! No copy constructors are called.
  225. //!
  226. //! <b>Throws</b>: Nothing.
  227. //!
  228. //! <b>Complexity</b>: Constant.
  229. //!
  230. //! <b>Note</b>: Does not affect the validity of iterators and references.
  231. void push_back(reference value) BOOST_NOEXCEPT
  232. {
  233. node_ptr to_insert = priv_value_traits().to_node_ptr(value);
  234. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::inited(to_insert));
  235. node_algorithms::link_before(this->get_root_node(), to_insert);
  236. this->priv_size_traits().increment();
  237. }
  238. //! <b>Requires</b>: value must be an lvalue.
  239. //!
  240. //! <b>Effects</b>: Inserts the value in the front of the list.
  241. //! No copy constructors are called.
  242. //!
  243. //! <b>Throws</b>: Nothing.
  244. //!
  245. //! <b>Complexity</b>: Constant.
  246. //!
  247. //! <b>Note</b>: Does not affect the validity of iterators and references.
  248. void push_front(reference value) BOOST_NOEXCEPT
  249. {
  250. node_ptr to_insert = priv_value_traits().to_node_ptr(value);
  251. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::inited(to_insert));
  252. node_algorithms::link_before(node_traits::get_next(this->get_root_node()), to_insert);
  253. this->priv_size_traits().increment();
  254. }
  255. //! <b>Effects</b>: Erases the last element of the list.
  256. //! No destructors are called.
  257. //!
  258. //! <b>Throws</b>: Nothing.
  259. //!
  260. //! <b>Complexity</b>: Constant.
  261. //!
  262. //! <b>Note</b>: Invalidates the iterators (but not the references) to the erased element.
  263. void pop_back() BOOST_NOEXCEPT
  264. { return this->pop_back_and_dispose(detail::null_disposer()); }
  265. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  266. //!
  267. //! <b>Effects</b>: Erases the last element of the list.
  268. //! No destructors are called.
  269. //! Disposer::operator()(pointer) is called for the removed element.
  270. //!
  271. //! <b>Throws</b>: Nothing.
  272. //!
  273. //! <b>Complexity</b>: Constant.
  274. //!
  275. //! <b>Note</b>: Invalidates the iterators to the erased element.
  276. template<class Disposer>
  277. void pop_back_and_dispose(Disposer disposer) BOOST_NOEXCEPT
  278. {
  279. node_ptr to_erase = node_traits::get_previous(this->get_root_node());
  280. node_algorithms::unlink(to_erase);
  281. this->priv_size_traits().decrement();
  282. BOOST_IF_CONSTEXPR(safemode_or_autounlink)
  283. node_algorithms::init(to_erase);
  284. disposer(priv_value_traits().to_value_ptr(to_erase));
  285. }
  286. //! <b>Effects</b>: Erases the first element of the list.
  287. //! No destructors are called.
  288. //!
  289. //! <b>Throws</b>: Nothing.
  290. //!
  291. //! <b>Complexity</b>: Constant.
  292. //!
  293. //! <b>Note</b>: Invalidates the iterators (but not the references) to the erased element.
  294. void pop_front() BOOST_NOEXCEPT
  295. { return this->pop_front_and_dispose(detail::null_disposer()); }
  296. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  297. //!
  298. //! <b>Effects</b>: Erases the first element of the list.
  299. //! No destructors are called.
  300. //! Disposer::operator()(pointer) is called for the removed element.
  301. //!
  302. //! <b>Throws</b>: Nothing.
  303. //!
  304. //! <b>Complexity</b>: Constant.
  305. //!
  306. //! <b>Note</b>: Invalidates the iterators to the erased element.
  307. template<class Disposer>
  308. void pop_front_and_dispose(Disposer disposer) BOOST_NOEXCEPT
  309. {
  310. node_ptr to_erase = node_traits::get_next(this->get_root_node());
  311. node_algorithms::unlink(to_erase);
  312. this->priv_size_traits().decrement();
  313. BOOST_IF_CONSTEXPR(safemode_or_autounlink)
  314. node_algorithms::init(to_erase);
  315. disposer(priv_value_traits().to_value_ptr(to_erase));
  316. }
  317. //! <b>Effects</b>: Returns a reference to the first element of the list.
  318. //!
  319. //! <b>Throws</b>: Nothing.
  320. //!
  321. //! <b>Complexity</b>: Constant.
  322. inline reference front() BOOST_NOEXCEPT
  323. { return *priv_value_traits().to_value_ptr(node_traits::get_next(this->get_root_node())); }
  324. //! <b>Effects</b>: Returns a const_reference to the first element of the list.
  325. //!
  326. //! <b>Throws</b>: Nothing.
  327. //!
  328. //! <b>Complexity</b>: Constant.
  329. inline const_reference front() const BOOST_NOEXCEPT
  330. { return *priv_value_traits().to_value_ptr(node_traits::get_next(this->get_root_node())); }
  331. //! <b>Effects</b>: Returns a reference to the last element of the list.
  332. //!
  333. //! <b>Throws</b>: Nothing.
  334. //!
  335. //! <b>Complexity</b>: Constant.
  336. inline reference back() BOOST_NOEXCEPT
  337. { return *priv_value_traits().to_value_ptr(node_traits::get_previous(this->get_root_node())); }
  338. //! <b>Effects</b>: Returns a const_reference to the last element of the list.
  339. //!
  340. //! <b>Throws</b>: Nothing.
  341. //!
  342. //! <b>Complexity</b>: Constant.
  343. inline const_reference back() const BOOST_NOEXCEPT
  344. { return *priv_value_traits().to_value_ptr(detail::uncast(node_traits::get_previous(this->get_root_node()))); }
  345. //! <b>Effects</b>: Returns an iterator to the first element contained in the list.
  346. //!
  347. //! <b>Throws</b>: Nothing.
  348. //!
  349. //! <b>Complexity</b>: Constant.
  350. inline iterator begin() BOOST_NOEXCEPT
  351. { return iterator(node_traits::get_next(this->get_root_node()), this->priv_value_traits_ptr()); }
  352. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the list.
  353. //!
  354. //! <b>Throws</b>: Nothing.
  355. //!
  356. //! <b>Complexity</b>: Constant.
  357. inline const_iterator begin() const BOOST_NOEXCEPT
  358. { return this->cbegin(); }
  359. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the list.
  360. //!
  361. //! <b>Throws</b>: Nothing.
  362. //!
  363. //! <b>Complexity</b>: Constant.
  364. inline const_iterator cbegin() const BOOST_NOEXCEPT
  365. { return const_iterator(node_traits::get_next(this->get_root_node()), this->priv_value_traits_ptr()); }
  366. //! <b>Effects</b>: Returns an iterator to the end of the list.
  367. //!
  368. //! <b>Throws</b>: Nothing.
  369. //!
  370. //! <b>Complexity</b>: Constant.
  371. inline iterator end() BOOST_NOEXCEPT
  372. { return iterator(this->get_root_node(), this->priv_value_traits_ptr()); }
  373. //! <b>Effects</b>: Returns a const_iterator to the end of the list.
  374. //!
  375. //! <b>Throws</b>: Nothing.
  376. //!
  377. //! <b>Complexity</b>: Constant.
  378. inline const_iterator end() const BOOST_NOEXCEPT
  379. { return this->cend(); }
  380. //! <b>Effects</b>: Returns a constant iterator to the end of the list.
  381. //!
  382. //! <b>Throws</b>: Nothing.
  383. //!
  384. //! <b>Complexity</b>: Constant.
  385. inline const_iterator cend() const BOOST_NOEXCEPT
  386. { return const_iterator(detail::uncast(this->get_root_node()), this->priv_value_traits_ptr()); }
  387. //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
  388. //! of the reversed list.
  389. //!
  390. //! <b>Throws</b>: Nothing.
  391. //!
  392. //! <b>Complexity</b>: Constant.
  393. inline reverse_iterator rbegin() BOOST_NOEXCEPT
  394. { return reverse_iterator(this->end()); }
  395. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  396. //! of the reversed list.
  397. //!
  398. //! <b>Throws</b>: Nothing.
  399. //!
  400. //! <b>Complexity</b>: Constant.
  401. inline const_reverse_iterator rbegin() const BOOST_NOEXCEPT
  402. { return this->crbegin(); }
  403. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  404. //! of the reversed list.
  405. //!
  406. //! <b>Throws</b>: Nothing.
  407. //!
  408. //! <b>Complexity</b>: Constant.
  409. inline const_reverse_iterator crbegin() const BOOST_NOEXCEPT
  410. { return const_reverse_iterator(end()); }
  411. //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
  412. //! of the reversed list.
  413. //!
  414. //! <b>Throws</b>: Nothing.
  415. //!
  416. //! <b>Complexity</b>: Constant.
  417. inline reverse_iterator rend() BOOST_NOEXCEPT
  418. { return reverse_iterator(begin()); }
  419. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  420. //! of the reversed list.
  421. //!
  422. //! <b>Throws</b>: Nothing.
  423. //!
  424. //! <b>Complexity</b>: Constant.
  425. inline const_reverse_iterator rend() const BOOST_NOEXCEPT
  426. { return this->crend(); }
  427. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  428. //! of the reversed list.
  429. //!
  430. //! <b>Throws</b>: Nothing.
  431. //!
  432. //! <b>Complexity</b>: Constant.
  433. inline const_reverse_iterator crend() const BOOST_NOEXCEPT
  434. { return const_reverse_iterator(this->begin()); }
  435. //! <b>Precondition</b>: end_iterator must be a valid end iterator
  436. //! of list.
  437. //!
  438. //! <b>Effects</b>: Returns a const reference to the list associated to the end iterator
  439. //!
  440. //! <b>Throws</b>: Nothing.
  441. //!
  442. //! <b>Complexity</b>: Constant.
  443. BOOST_INTRUSIVE_NO_DANGLING
  444. inline static list_impl &container_from_end_iterator(iterator end_iterator) BOOST_NOEXCEPT
  445. { return list_impl::priv_container_from_end_iterator(end_iterator); }
  446. //! <b>Precondition</b>: end_iterator must be a valid end const_iterator
  447. //! of list.
  448. //!
  449. //! <b>Effects</b>: Returns a const reference to the list associated to the end iterator
  450. //!
  451. //! <b>Throws</b>: Nothing.
  452. //!
  453. //! <b>Complexity</b>: Constant.
  454. BOOST_INTRUSIVE_NO_DANGLING
  455. inline static const list_impl &container_from_end_iterator(const_iterator end_iterator) BOOST_NOEXCEPT
  456. { return list_impl::priv_container_from_end_iterator(end_iterator); }
  457. //! <b>Effects</b>: Returns the number of the elements contained in the list.
  458. //!
  459. //! <b>Throws</b>: Nothing.
  460. //!
  461. //! <b>Complexity</b>: Linear to the number of elements contained in the list.
  462. //! if constant-time size option is disabled. Constant time otherwise.
  463. //!
  464. //! <b>Note</b>: Does not affect the validity of iterators and references.
  465. inline size_type size() const BOOST_NOEXCEPT
  466. {
  467. BOOST_IF_CONSTEXPR(constant_time_size)
  468. return this->priv_size_traits().get_size();
  469. else
  470. return node_algorithms::count(this->get_root_node()) - 1;
  471. }
  472. //! <b>Effects</b>: Returns true if the list contains no elements.
  473. //!
  474. //! <b>Throws</b>: Nothing.
  475. //!
  476. //! <b>Complexity</b>: Constant.
  477. //!
  478. //! <b>Note</b>: Does not affect the validity of iterators and references.
  479. inline bool empty() const BOOST_NOEXCEPT
  480. { return node_algorithms::unique(this->get_root_node()); }
  481. //! <b>Effects</b>: Swaps the elements of x and *this.
  482. //!
  483. //! <b>Throws</b>: Nothing.
  484. //!
  485. //! <b>Complexity</b>: Constant.
  486. //!
  487. //! <b>Note</b>: Does not affect the validity of iterators and references.
  488. void swap(list_impl& other) BOOST_NOEXCEPT
  489. {
  490. node_algorithms::swap_nodes(this->get_root_node(), other.get_root_node());
  491. this->priv_size_traits().swap(other.priv_size_traits());
  492. }
  493. //! <b>Effects</b>: Moves backwards all the elements, so that the first
  494. //! element becomes the second, the second becomes the third...
  495. //! the last element becomes the first one.
  496. //!
  497. //! <b>Throws</b>: Nothing.
  498. //!
  499. //! <b>Complexity</b>: Linear to the number of shifts.
  500. //!
  501. //! <b>Note</b>: Does not affect the validity of iterators and references.
  502. inline void shift_backwards(size_type n = 1) BOOST_NOEXCEPT
  503. { node_algorithms::move_forward(this->get_root_node(), n); }
  504. //! <b>Effects</b>: Moves forward all the elements, so that the second
  505. //! element becomes the first, the third becomes the second...
  506. //! the first element becomes the last one.
  507. //!
  508. //! <b>Throws</b>: Nothing.
  509. //!
  510. //! <b>Complexity</b>: Linear to the number of shifts.
  511. //!
  512. //! <b>Note</b>: Does not affect the validity of iterators and references.
  513. inline void shift_forward(size_type n = 1) BOOST_NOEXCEPT
  514. { node_algorithms::move_backwards(this->get_root_node(), n); }
  515. //! <b>Effects</b>: Erases the element pointed by i of the list.
  516. //! No destructors are called.
  517. //!
  518. //! <b>Returns</b>: the first element remaining beyond the removed element,
  519. //! or end() if no such element exists.
  520. //!
  521. //! <b>Throws</b>: Nothing.
  522. //!
  523. //! <b>Complexity</b>: Constant.
  524. //!
  525. //! <b>Note</b>: Invalidates the iterators (but not the references) to the
  526. //! erased element.
  527. inline iterator erase(const_iterator i) BOOST_NOEXCEPT
  528. { return this->erase_and_dispose(i, detail::null_disposer()); }
  529. //! <b>Requires</b>: b and e must be valid iterators to elements in *this.
  530. //!
  531. //! <b>Effects</b>: Erases the element range pointed by b and e
  532. //! No destructors are called.
  533. //!
  534. //! <b>Returns</b>: the first element remaining beyond the removed elements,
  535. //! or end() if no such element exists.
  536. //!
  537. //! <b>Throws</b>: Nothing.
  538. //!
  539. //! <b>Complexity</b>: Linear to the number of erased elements if it's a safe-mode
  540. //! or auto-unlink value, or constant-time size is enabled. Constant-time otherwise.
  541. //!
  542. //! <b>Note</b>: Invalidates the iterators (but not the references) to the
  543. //! erased elements.
  544. inline iterator erase(const_iterator b, const_iterator e) BOOST_NOEXCEPT
  545. {
  546. BOOST_IF_CONSTEXPR(safemode_or_autounlink || constant_time_size){
  547. return this->erase_and_dispose(b, e, detail::null_disposer());
  548. }
  549. else{
  550. node_algorithms::unlink(b.pointed_node(), e.pointed_node());
  551. return e.unconst();
  552. }
  553. }
  554. //! <b>Requires</b>: b and e must be valid iterators to elements in *this.
  555. //! n must be distance(b, e).
  556. //!
  557. //! <b>Effects</b>: Erases the element range pointed by b and e
  558. //! No destructors are called.
  559. //!
  560. //! <b>Returns</b>: the first element remaining beyond the removed elements,
  561. //! or end() if no such element exists.
  562. //!
  563. //! <b>Throws</b>: Nothing.
  564. //!
  565. //! <b>Complexity</b>: Linear to the number of erased elements if it's a safe-mode
  566. //! or auto-unlink value is enabled. Constant-time otherwise.
  567. //!
  568. //! <b>Note</b>: Invalidates the iterators (but not the references) to the
  569. //! erased elements.
  570. iterator erase(const_iterator b, const_iterator e, size_type n) BOOST_NOEXCEPT
  571. {
  572. BOOST_INTRUSIVE_INVARIANT_ASSERT(node_algorithms::distance(b.pointed_node(), e.pointed_node()) == n);
  573. (void)n;
  574. BOOST_IF_CONSTEXPR(safemode_or_autounlink){
  575. return this->erase_and_dispose(b, e, detail::null_disposer());
  576. }
  577. else{
  578. BOOST_IF_CONSTEXPR(constant_time_size){
  579. this->priv_size_traits().decrease(n);
  580. }
  581. node_algorithms::unlink(b.pointed_node(), e.pointed_node());
  582. return e.unconst();
  583. }
  584. }
  585. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  586. //!
  587. //! <b>Effects</b>: Erases the element pointed by i of the list.
  588. //! No destructors are called.
  589. //! Disposer::operator()(pointer) is called for the removed element.
  590. //!
  591. //! <b>Returns</b>: the first element remaining beyond the removed element,
  592. //! or end() if no such element exists.
  593. //!
  594. //! <b>Throws</b>: Nothing.
  595. //!
  596. //! <b>Complexity</b>: Constant.
  597. //!
  598. //! <b>Note</b>: Invalidates the iterators to the erased element.
  599. template <class Disposer>
  600. iterator erase_and_dispose(const_iterator i, Disposer disposer) BOOST_NOEXCEPT
  601. {
  602. node_ptr to_erase(i.pointed_node());
  603. ++i;
  604. node_algorithms::unlink(to_erase);
  605. this->priv_size_traits().decrement();
  606. BOOST_IF_CONSTEXPR(safemode_or_autounlink)
  607. node_algorithms::init(to_erase);
  608. disposer(this->priv_value_traits().to_value_ptr(to_erase));
  609. return i.unconst();
  610. }
  611. #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  612. template<class Disposer>
  613. iterator erase_and_dispose(iterator i, Disposer disposer) BOOST_NOEXCEPT
  614. { return this->erase_and_dispose(const_iterator(i), disposer); }
  615. #endif
  616. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  617. //!
  618. //! <b>Effects</b>: Erases the element range pointed by b and e
  619. //! No destructors are called.
  620. //! Disposer::operator()(pointer) is called for the removed elements.
  621. //!
  622. //! <b>Returns</b>: the first element remaining beyond the removed elements,
  623. //! or end() if no such element exists.
  624. //!
  625. //! <b>Throws</b>: Nothing.
  626. //!
  627. //! <b>Complexity</b>: Linear to the number of elements erased.
  628. //!
  629. //! <b>Note</b>: Invalidates the iterators to the erased elements.
  630. template <class Disposer>
  631. iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer) BOOST_NOEXCEPT
  632. {
  633. node_ptr bp(b.pointed_node()), ep(e.pointed_node());
  634. node_algorithms::unlink(bp, ep);
  635. while(bp != ep){
  636. node_ptr to_erase(bp);
  637. bp = node_traits::get_next(bp);
  638. BOOST_IF_CONSTEXPR(safemode_or_autounlink)
  639. node_algorithms::init(to_erase);
  640. disposer(priv_value_traits().to_value_ptr(to_erase));
  641. this->priv_size_traits().decrement();
  642. }
  643. return e.unconst();
  644. }
  645. //! <b>Effects</b>: Erases all the elements of the container.
  646. //! No destructors are called.
  647. //!
  648. //! <b>Throws</b>: Nothing.
  649. //!
  650. //! <b>Complexity</b>: Linear to the number of elements of the list.
  651. //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise.
  652. //!
  653. //! <b>Note</b>: Invalidates the iterators (but not the references) to the erased elements.
  654. void clear() BOOST_NOEXCEPT
  655. {
  656. BOOST_IF_CONSTEXPR(safemode_or_autounlink){
  657. this->clear_and_dispose(detail::null_disposer());
  658. }
  659. else{
  660. node_algorithms::init_header(this->get_root_node());
  661. this->priv_size_traits().set_size(size_type(0));
  662. }
  663. }
  664. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  665. //!
  666. //! <b>Effects</b>: Erases all the elements of the container.
  667. //! No destructors are called.
  668. //! Disposer::operator()(pointer) is called for the removed elements.
  669. //!
  670. //! <b>Throws</b>: Nothing.
  671. //!
  672. //! <b>Complexity</b>: Linear to the number of elements of the list.
  673. //!
  674. //! <b>Note</b>: Invalidates the iterators to the erased elements.
  675. template <class Disposer>
  676. void clear_and_dispose(Disposer disposer) BOOST_NOEXCEPT
  677. {
  678. const_iterator it(this->begin()), itend(this->end());
  679. while(it != itend){
  680. node_ptr to_erase(it.pointed_node());
  681. ++it;
  682. BOOST_IF_CONSTEXPR(safemode_or_autounlink)
  683. node_algorithms::init(to_erase);
  684. disposer(priv_value_traits().to_value_ptr(to_erase));
  685. }
  686. node_algorithms::init_header(this->get_root_node());
  687. this->priv_size_traits().set_size(0);
  688. }
  689. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  690. //! Cloner should yield to nodes equivalent to the original nodes.
  691. //!
  692. //! <b>Effects</b>: Erases all the elements from *this
  693. //! calling Disposer::operator()(pointer), clones all the
  694. //! elements from src calling Cloner::operator()(const_reference )
  695. //! and inserts them on *this.
  696. //!
  697. //! If cloner throws, all cloned elements are unlinked and disposed
  698. //! calling Disposer::operator()(pointer).
  699. //!
  700. //! <b>Complexity</b>: Linear to erased plus inserted elements.
  701. //!
  702. //! <b>Throws</b>: If cloner throws. Basic guarantee.
  703. template <class Cloner, class Disposer>
  704. void clone_from(const list_impl &src, Cloner cloner, Disposer disposer)
  705. {
  706. this->clear_and_dispose(disposer);
  707. detail::exception_disposer<list_impl, Disposer>
  708. rollback(*this, disposer);
  709. const_iterator b(src.begin()), e(src.end());
  710. for(; b != e; ++b){
  711. this->push_back(*cloner(*b));
  712. }
  713. rollback.release();
  714. }
  715. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  716. //! Cloner should yield to nodes equivalent to the original nodes.
  717. //!
  718. //! <b>Effects</b>: Erases all the elements from *this
  719. //! calling Disposer::operator()(pointer), clones all the
  720. //! elements from src calling Cloner::operator()(reference)
  721. //! and inserts them on *this.
  722. //!
  723. //! If cloner throws, all cloned elements are unlinked and disposed
  724. //! calling Disposer::operator()(pointer).
  725. //!
  726. //! <b>Complexity</b>: Linear to erased plus inserted elements.
  727. //!
  728. //! <b>Throws</b>: If cloner throws. Basic guarantee.
  729. template <class Cloner, class Disposer>
  730. void clone_from(BOOST_RV_REF(list_impl) src, Cloner cloner, Disposer disposer)
  731. {
  732. this->clear_and_dispose(disposer);
  733. detail::exception_disposer<list_impl, Disposer>
  734. rollback(*this, disposer);
  735. iterator b(src.begin()), e(src.end());
  736. for(; b != e; ++b){
  737. this->push_back(*cloner(*b));
  738. }
  739. rollback.release();
  740. }
  741. //! <b>Requires</b>: value must be an lvalue and p must be a valid iterator of *this.
  742. //!
  743. //! <b>Effects</b>: Inserts the value before the position pointed by p.
  744. //!
  745. //! <b>Returns</b>: An iterator to the inserted element.
  746. //!
  747. //! <b>Throws</b>: Nothing.
  748. //!
  749. //! <b>Complexity</b>: Constant time. No copy constructors are called.
  750. //!
  751. //! <b>Note</b>: Does not affect the validity of iterators and references.
  752. iterator insert(const_iterator p, reference value) BOOST_NOEXCEPT
  753. {
  754. node_ptr to_insert = this->priv_value_traits().to_node_ptr(value);
  755. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::inited(to_insert));
  756. node_algorithms::link_before(p.pointed_node(), to_insert);
  757. this->priv_size_traits().increment();
  758. return iterator(to_insert, this->priv_value_traits_ptr());
  759. }
  760. //! <b>Requires</b>: Dereferencing iterator must yield
  761. //! an lvalue of type value_type and p must be a valid iterator of *this.
  762. //!
  763. //! <b>Effects</b>: Inserts the range pointed by b and e before the position p.
  764. //! No copy constructors are called.
  765. //!
  766. //! <b>Throws</b>: Nothing.
  767. //!
  768. //! <b>Complexity</b>: Linear to the number of elements inserted.
  769. //!
  770. //! <b>Note</b>: Does not affect the validity of iterators and references.
  771. template<class Iterator>
  772. void insert(const_iterator p, Iterator b, Iterator e) BOOST_NOEXCEPT
  773. {
  774. for (; b != e; ++b)
  775. this->insert(p, *b);
  776. }
  777. //! <b>Requires</b>: Dereferencing iterator must yield
  778. //! an lvalue of type value_type.
  779. //!
  780. //! <b>Effects</b>: Clears the list and inserts the range pointed by b and e.
  781. //! No destructors or copy constructors are called.
  782. //!
  783. //! <b>Throws</b>: Nothing.
  784. //!
  785. //! <b>Complexity</b>: Linear to the number of elements inserted plus
  786. //! linear to the elements contained in the list if it's a safe-mode
  787. //! or auto-unlink value.
  788. //! Linear to the number of elements inserted in the list otherwise.
  789. //!
  790. //! <b>Note</b>: Invalidates the iterators (but not the references)
  791. //! to the erased elements.
  792. template<class Iterator>
  793. void assign(Iterator b, Iterator e) BOOST_NOEXCEPT
  794. {
  795. this->clear();
  796. this->insert(this->cend(), b, e);
  797. }
  798. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  799. //!
  800. //! <b>Requires</b>: Dereferencing iterator must yield
  801. //! an lvalue of type value_type.
  802. //!
  803. //! <b>Effects</b>: Clears the list and inserts the range pointed by b and e.
  804. //! No destructors or copy constructors are called.
  805. //! Disposer::operator()(pointer) is called for the removed elements.
  806. //!
  807. //! <b>Throws</b>: Nothing.
  808. //!
  809. //! <b>Complexity</b>: Linear to the number of elements inserted plus
  810. //! linear to the elements contained in the list.
  811. //!
  812. //! <b>Note</b>: Invalidates the iterators (but not the references)
  813. //! to the erased elements.
  814. template<class Iterator, class Disposer>
  815. void dispose_and_assign(Disposer disposer, Iterator b, Iterator e) BOOST_NOEXCEPT
  816. {
  817. this->clear_and_dispose(disposer);
  818. this->insert(this->cend(), b, e);
  819. }
  820. //! <b>Requires</b>: p must be a valid iterator of *this.
  821. //!
  822. //! <b>Effects</b>: Transfers all the elements of list x to this list, before the
  823. //! the element pointed by p. No destructors or copy constructors are called.
  824. //!
  825. //! <b>Throws</b>: Nothing.
  826. //!
  827. //! <b>Complexity</b>: Constant.
  828. //!
  829. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of
  830. //! this list. Iterators of this list and all the references are not invalidated.
  831. void splice(const_iterator p, list_impl& x) BOOST_NOEXCEPT
  832. {
  833. if(!x.empty()){
  834. node_algorithms::transfer
  835. (p.pointed_node(), x.begin().pointed_node(), x.end().pointed_node());
  836. size_traits &thist = this->priv_size_traits();
  837. size_traits &xt = x.priv_size_traits();
  838. thist.increase(xt.get_size());
  839. xt.set_size(size_type(0));
  840. }
  841. }
  842. //! <b>Requires</b>: p must be a valid iterator of *this.
  843. //! new_ele must point to an element contained in list x.
  844. //!
  845. //! <b>Effects</b>: Transfers the value pointed by new_ele, from list x to this list,
  846. //! before the element pointed by p. No destructors or copy constructors are called.
  847. //! If p == new_ele or p == ++new_ele, this function is a null operation.
  848. //!
  849. //! <b>Throws</b>: Nothing.
  850. //!
  851. //! <b>Complexity</b>: Constant.
  852. //!
  853. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  854. //! list. Iterators of this list and all the references are not invalidated.
  855. void splice(const_iterator p, list_impl&x, const_iterator new_ele) BOOST_NOEXCEPT
  856. {
  857. node_algorithms::transfer(p.pointed_node(), new_ele.pointed_node());
  858. x.priv_size_traits().decrement();
  859. this->priv_size_traits().increment();
  860. }
  861. //! <b>Requires</b>: p must be a valid iterator of *this.
  862. //! f and e must point to elements contained in list x.
  863. //!
  864. //! <b>Effects</b>: Transfers the range pointed by f and e from list x to this list,
  865. //! before the element pointed by p. No destructors or copy constructors are called.
  866. //!
  867. //! <b>Throws</b>: Nothing.
  868. //!
  869. //! <b>Complexity</b>: Linear to the number of elements transferred
  870. //! if constant-time size option is enabled. Constant-time otherwise.
  871. //!
  872. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  873. //! list. Iterators of this list and all the references are not invalidated.
  874. void splice(const_iterator p, list_impl&x, const_iterator f, const_iterator e) BOOST_NOEXCEPT
  875. {
  876. BOOST_IF_CONSTEXPR(constant_time_size)
  877. this->splice(p, x, f, e, node_algorithms::distance(f.pointed_node(), e.pointed_node()));
  878. else
  879. this->splice(p, x, f, e, 1);//intrusive::iterator_distance is a dummy value
  880. }
  881. //! <b>Requires</b>: p must be a valid iterator of *this.
  882. //! f and e must point to elements contained in list x.
  883. //! n == distance(f, e)
  884. //!
  885. //! <b>Effects</b>: Transfers the range pointed by f and e from list x to this list,
  886. //! before the element pointed by p. No destructors or copy constructors are called.
  887. //!
  888. //! <b>Throws</b>: Nothing.
  889. //!
  890. //! <b>Complexity</b>: Constant.
  891. //!
  892. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  893. //! list. Iterators of this list and all the references are not invalidated.
  894. void splice(const_iterator p, list_impl&x, const_iterator f, const_iterator e, size_type n) BOOST_NOEXCEPT
  895. {
  896. if(n){
  897. BOOST_IF_CONSTEXPR(constant_time_size){
  898. BOOST_INTRUSIVE_INVARIANT_ASSERT(n == node_algorithms::distance(f.pointed_node(), e.pointed_node()));
  899. node_algorithms::transfer(p.pointed_node(), f.pointed_node(), e.pointed_node());
  900. size_traits &thist = this->priv_size_traits();
  901. size_traits &xt = x.priv_size_traits();
  902. thist.increase(n);
  903. xt.decrease(n);
  904. }
  905. else{
  906. node_algorithms::transfer(p.pointed_node(), f.pointed_node(), e.pointed_node());
  907. }
  908. }
  909. }
  910. //! <b>Effects</b>: This function sorts the list *this according to operator <.
  911. //! The sort is stable, that is, the relative order of equivalent elements is preserved.
  912. //!
  913. //! <b>Throws</b>: If value_traits::node_traits::node
  914. //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
  915. //! or operator < throws. Basic guarantee.
  916. //!
  917. //! <b>Notes</b>: Iterators and references are not invalidated.
  918. //!
  919. //! <b>Complexity</b>: The number of comparisons is approximately N log N, where N
  920. //! is the list's size.
  921. void sort()
  922. { this->sort(value_less<value_type>()); }
  923. //! <b>Requires</b>: p must be a comparison function that induces a strict weak ordering
  924. //!
  925. //! <b>Effects</b>: This function sorts the list *this according to p. The sort is
  926. //! stable, that is, the relative order of equivalent elements is preserved.
  927. //!
  928. //! <b>Throws</b>: If value_traits::node_traits::node
  929. //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
  930. //! or the predicate throws. Basic guarantee.
  931. //!
  932. //! <b>Notes</b>: This won't throw if list_base_hook<> or
  933. //! list_member_hook are used.
  934. //! Iterators and references are not invalidated.
  935. //!
  936. //! <b>Complexity</b>: The number of comparisons is approximately N log N, where N
  937. //! is the list's size.
  938. template<class Predicate>
  939. void sort(Predicate p)
  940. {
  941. if(node_traits::get_next(this->get_root_node())
  942. != node_traits::get_previous(this->get_root_node())){
  943. list_impl carry(this->priv_value_traits());
  944. detail::array_initializer<list_impl, 64> counter(this->priv_value_traits());
  945. int fill = 0;
  946. while(!this->empty()){
  947. carry.splice(carry.cbegin(), *this, this->cbegin());
  948. int i = 0;
  949. while(i < fill && !counter[i].empty()) {
  950. counter[i].merge(carry, p);
  951. carry.swap(counter[i++]);
  952. }
  953. carry.swap(counter[i]);
  954. if(i == fill)
  955. ++fill;
  956. }
  957. for (int i = 1; i < fill; ++i)
  958. counter[i].merge(counter[i-1], p);
  959. this->swap(counter[fill-1]);
  960. }
  961. }
  962. //! <b>Effects</b>: This function removes all of x's elements and inserts them
  963. //! in order into *this according to operator <. The merge is stable;
  964. //! that is, if an element from *this is equivalent to one from x, then the element
  965. //! from *this will precede the one from x.
  966. //!
  967. //! <b>Throws</b>: If operator < throws. Basic guarantee.
  968. //!
  969. //! <b>Complexity</b>: This function is linear time: it performs at most
  970. //! size() + x.size() - 1 comparisons.
  971. //!
  972. //! <b>Note</b>: Iterators and references are not invalidated
  973. void merge(list_impl& x)
  974. { this->merge(x, value_less<value_type>()); }
  975. //! <b>Requires</b>: p must be a comparison function that induces a strict weak
  976. //! ordering and both *this and x must be sorted according to that ordering
  977. //! The lists x and *this must be distinct.
  978. //!
  979. //! <b>Effects</b>: This function removes all of x's elements and inserts them
  980. //! in order into *this. The merge is stable; that is, if an element from *this is
  981. //! equivalent to one from x, then the element from *this will precede the one from x.
  982. //!
  983. //! <b>Throws</b>: If the predicate throws. Basic guarantee.
  984. //!
  985. //! <b>Complexity</b>: This function is linear time: it performs at most
  986. //! size() + x.size() - 1 comparisons.
  987. //!
  988. //! <b>Note</b>: Iterators and references are not invalidated.
  989. template<class Predicate>
  990. void merge(list_impl& x, Predicate p)
  991. {
  992. const_iterator e(this->cend()), ex(x.cend());
  993. const_iterator b(this->cbegin());
  994. while(!x.empty()){
  995. const_iterator ix(x.cbegin());
  996. while (b != e && !p(*ix, *b)){
  997. ++b;
  998. }
  999. if(b == e){
  1000. //Now transfer the rest to the end of the container
  1001. this->splice(e, x);
  1002. break;
  1003. }
  1004. else{
  1005. size_type n(0);
  1006. do{
  1007. ++ix; ++n;
  1008. } while(ix != ex && p(*ix, *b));
  1009. this->splice(b, x, x.begin(), ix, n);
  1010. }
  1011. }
  1012. }
  1013. //! <b>Effects</b>: Reverses the order of elements in the list.
  1014. //!
  1015. //! <b>Throws</b>: Nothing.
  1016. //!
  1017. //! <b>Complexity</b>: This function is linear time.
  1018. //!
  1019. //! <b>Note</b>: Iterators and references are not invalidated
  1020. void reverse() BOOST_NOEXCEPT
  1021. { node_algorithms::reverse(this->get_root_node()); }
  1022. //! <b>Effects</b>: Removes all the elements that compare equal to value.
  1023. //! No destructors are called.
  1024. //!
  1025. //! <b>Throws</b>: If operator == throws. Basic guarantee.
  1026. //!
  1027. //! <b>Complexity</b>: Linear time. It performs exactly size() comparisons for equality.
  1028. //!
  1029. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1030. //! and iterators to elements that are not removed remain valid.
  1031. void remove(const_reference value) BOOST_NOEXCEPT
  1032. { this->remove_if(detail::equal_to_value<const_reference>(value)); }
  1033. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1034. //!
  1035. //! <b>Effects</b>: Removes all the elements that compare equal to value.
  1036. //! Disposer::operator()(pointer) is called for every removed element.
  1037. //!
  1038. //! <b>Throws</b>: If operator == throws. Basic guarantee.
  1039. //!
  1040. //! <b>Complexity</b>: Linear time. It performs exactly size() comparisons for equality.
  1041. //!
  1042. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1043. //! and iterators to elements that are not removed remain valid.
  1044. template<class Disposer>
  1045. void remove_and_dispose(const_reference value, Disposer disposer) BOOST_NOEXCEPT
  1046. { this->remove_and_dispose_if(detail::equal_to_value<const_reference>(value), disposer); }
  1047. //! <b>Effects</b>: Removes all the elements for which a specified
  1048. //! predicate is satisfied. No destructors are called.
  1049. //!
  1050. //! <b>Throws</b>: If pred throws. Basic guarantee.
  1051. //!
  1052. //! <b>Complexity</b>: Linear time. It performs exactly size() calls to the predicate.
  1053. //!
  1054. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1055. //! and iterators to elements that are not removed remain valid.
  1056. template<class Pred>
  1057. void remove_if(Pred pred)
  1058. {
  1059. const node_ptr root_node = this->get_root_node();
  1060. typename node_algorithms::stable_partition_info info;
  1061. node_algorithms::stable_partition
  1062. (node_traits::get_next(root_node), root_node, detail::key_nodeptr_comp<Pred, value_traits>(pred, &this->priv_value_traits()), info);
  1063. //Invariants preserved by stable_partition so erase can be safely called
  1064. //The first element might have changed so calculate it again
  1065. this->erase( const_iterator(node_traits::get_next(root_node), this->priv_value_traits_ptr())
  1066. , const_iterator(info.beg_2st_partition, this->priv_value_traits_ptr())
  1067. , info.num_1st_partition);
  1068. }
  1069. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1070. //!
  1071. //! <b>Effects</b>: Removes all the elements for which a specified
  1072. //! predicate is satisfied.
  1073. //! Disposer::operator()(pointer) is called for every removed element.
  1074. //!
  1075. //! <b>Throws</b>: If pred throws. Basic guarantee.
  1076. //!
  1077. //! <b>Complexity</b>: Linear time. It performs exactly size() comparisons for equality.
  1078. //!
  1079. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1080. //! and iterators to elements that are not removed remain valid.
  1081. template<class Pred, class Disposer>
  1082. void remove_and_dispose_if(Pred pred, Disposer disposer)
  1083. {
  1084. const node_ptr root_node = this->get_root_node();
  1085. typename node_algorithms::stable_partition_info info;
  1086. node_algorithms::stable_partition
  1087. (node_traits::get_next(root_node), root_node, detail::key_nodeptr_comp<Pred, value_traits>(pred, &this->priv_value_traits()), info);
  1088. //Invariants preserved by stable_partition so erase can be safely called
  1089. //The first element might have changed so calculate it again
  1090. this->erase_and_dispose( const_iterator(node_traits::get_next(root_node), this->priv_value_traits_ptr())
  1091. , const_iterator(info.beg_2st_partition, this->priv_value_traits_ptr())
  1092. , disposer);
  1093. }
  1094. //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
  1095. //! elements that are equal from the list. No destructors are called.
  1096. //!
  1097. //! <b>Throws</b>: If the comparison operator throws. Basic guarantee.
  1098. //!
  1099. //! <b>Complexity</b>: Linear time (size()-1 comparisons calls to pred()).
  1100. //!
  1101. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1102. //! and iterators to elements that are not removed remain valid.
  1103. void unique()
  1104. { this->unique_and_dispose(value_equal<value_type>(), detail::null_disposer()); }
  1105. //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
  1106. //! elements that satisfy some binary predicate from the list.
  1107. //! No destructors are called.
  1108. //!
  1109. //! <b>Throws</b>: If pred throws. Basic guarantee.
  1110. //!
  1111. //! <b>Complexity</b>: Linear time (size()-1 comparisons equality comparisons).
  1112. //!
  1113. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1114. //! and iterators to elements that are not removed remain valid.
  1115. template<class BinaryPredicate>
  1116. void unique(BinaryPredicate pred)
  1117. { this->unique_and_dispose(pred, detail::null_disposer()); }
  1118. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1119. //!
  1120. //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
  1121. //! elements that are equal from the list.
  1122. //! Disposer::operator()(pointer) is called for every removed element.
  1123. //!
  1124. //! <b>Throws</b>: If the equality operator throws. Basic guarantee.
  1125. //!
  1126. //! <b>Complexity</b>: Linear time (size()-1) comparisons equality comparisons.
  1127. //!
  1128. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1129. //! and iterators to elements that are not removed remain valid.
  1130. template<class Disposer>
  1131. void unique_and_dispose(Disposer disposer)
  1132. { this->unique_and_dispose(value_equal<value_type>(), disposer); }
  1133. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1134. //!
  1135. //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
  1136. //! elements that satisfy some binary predicate from the list.
  1137. //! Disposer::operator()(pointer) is called for every removed element.
  1138. //!
  1139. //! <b>Throws</b>: If pred throws. Basic guarantee.
  1140. //!
  1141. //! <b>Complexity</b>: Linear time (size()-1) comparisons equality comparisons.
  1142. //!
  1143. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1144. //! and iterators to elements that are not removed remain valid.
  1145. template<class BinaryPredicate, class Disposer>
  1146. void unique_and_dispose(BinaryPredicate pred, Disposer disposer)
  1147. {
  1148. const_iterator itend(this->cend());
  1149. const_iterator cur(this->cbegin());
  1150. if(cur != itend){
  1151. const_iterator after(cur);
  1152. ++after;
  1153. while(after != itend){
  1154. if(pred(*cur, *after)){
  1155. after = this->erase_and_dispose(after, disposer);
  1156. }
  1157. else{
  1158. cur = after;
  1159. ++after;
  1160. }
  1161. }
  1162. }
  1163. }
  1164. //! <b>Requires</b>: `value` must be a reference to a value inserted in an instance of this container type.
  1165. //!
  1166. //! <b>Effects</b>: This function returns a const_iterator pointing to the element
  1167. //!
  1168. //! <b>Throws</b>: Nothing.
  1169. //!
  1170. //! <b>Complexity</b>: Constant time.
  1171. //!
  1172. //! <b>Note</b>: Iterators and references are not invalidated.
  1173. //! This static function is available only if the <i>value traits</i>
  1174. //! is stateless.
  1175. static iterator s_iterator_to(reference value) BOOST_NOEXCEPT
  1176. {
  1177. BOOST_INTRUSIVE_STATIC_ASSERT((!stateful_value_traits));
  1178. BOOST_INTRUSIVE_INVARIANT_ASSERT(!node_algorithms::inited(value_traits::to_node_ptr(value)));
  1179. return iterator(value_traits::to_node_ptr(value), const_value_traits_ptr());
  1180. }
  1181. //! <b>Requires</b>: value must be a const reference to a value inserted in a list.
  1182. //!
  1183. //! <b>Effects</b>: This function returns an iterator pointing to the element.
  1184. //!
  1185. //! <b>Throws</b>: Nothing.
  1186. //!
  1187. //! <b>Complexity</b>: Constant time.
  1188. //!
  1189. //! <b>Note</b>: Iterators and references are not invalidated.
  1190. //! This static function is available only if the <i>value traits</i>
  1191. //! is stateless.
  1192. static const_iterator s_iterator_to(const_reference value) BOOST_NOEXCEPT
  1193. {
  1194. BOOST_INTRUSIVE_STATIC_ASSERT((!stateful_value_traits));
  1195. reference r =*detail::uncast(pointer_traits<const_pointer>::pointer_to(value));
  1196. BOOST_INTRUSIVE_INVARIANT_ASSERT(!node_algorithms::inited(value_traits::to_node_ptr(r)));
  1197. return const_iterator(value_traits::to_node_ptr(r), const_value_traits_ptr());
  1198. }
  1199. //! <b>Requires</b>: `value` must be a reference to a value inserted in an instance of this container type.
  1200. //!
  1201. //! <b>Effects</b>: This function returns a const_iterator pointing to the element
  1202. //!
  1203. //! <b>Throws</b>: Nothing.
  1204. //!
  1205. //! <b>Complexity</b>: Constant time.
  1206. //!
  1207. //! <b>Note</b>: Iterators and references are not invalidated.
  1208. iterator iterator_to(reference value) BOOST_NOEXCEPT
  1209. {
  1210. BOOST_INTRUSIVE_INVARIANT_ASSERT(!node_algorithms::inited(this->priv_value_traits().to_node_ptr(value)));
  1211. return iterator(this->priv_value_traits().to_node_ptr(value), this->priv_value_traits_ptr());
  1212. }
  1213. //! <b>Requires</b>: value must be a const reference to a value inserted in a list.
  1214. //!
  1215. //! <b>Effects</b>: This function returns an iterator pointing to the element.
  1216. //!
  1217. //! <b>Throws</b>: Nothing.
  1218. //!
  1219. //! <b>Complexity</b>: Constant time.
  1220. //!
  1221. //! <b>Note</b>: Iterators and references are not invalidated.
  1222. const_iterator iterator_to(const_reference value) const BOOST_NOEXCEPT
  1223. {
  1224. reference r = *detail::uncast(pointer_traits<const_pointer>::pointer_to(value));
  1225. BOOST_INTRUSIVE_INVARIANT_ASSERT(!node_algorithms::inited(this->priv_value_traits().to_node_ptr(r)));
  1226. return const_iterator(this->priv_value_traits().to_node_ptr(r), this->priv_value_traits_ptr());
  1227. }
  1228. //! <b>Effects</b>: Asserts the integrity of the container.
  1229. //!
  1230. //! <b>Complexity</b>: Linear time.
  1231. //!
  1232. //! <b>Note</b>: The method has no effect when asserts are turned off (e.g., with NDEBUG).
  1233. //! Experimental function, interface might change in future versions.
  1234. void check() const
  1235. {
  1236. const_node_ptr header_ptr = get_root_node();
  1237. // header's next and prev are never null
  1238. BOOST_INTRUSIVE_INVARIANT_ASSERT(node_traits::get_next(header_ptr));
  1239. BOOST_INTRUSIVE_INVARIANT_ASSERT(node_traits::get_previous(header_ptr));
  1240. // header's next and prev either both point to header (empty list) or neither does
  1241. BOOST_INTRUSIVE_INVARIANT_ASSERT((node_traits::get_next(header_ptr) == header_ptr)
  1242. == (node_traits::get_previous(header_ptr) == header_ptr));
  1243. if (node_traits::get_next(header_ptr) == header_ptr)
  1244. {
  1245. BOOST_IF_CONSTEXPR(constant_time_size)
  1246. BOOST_INTRUSIVE_INVARIANT_ASSERT(this->priv_size_traits().get_size() == 0);
  1247. return;
  1248. }
  1249. size_t node_count = 0; (void)node_count;
  1250. const_node_ptr p = header_ptr;
  1251. while (true)
  1252. {
  1253. const_node_ptr next_p = node_traits::get_next(p);
  1254. BOOST_INTRUSIVE_INVARIANT_ASSERT(next_p);
  1255. BOOST_INTRUSIVE_INVARIANT_ASSERT(node_traits::get_previous(next_p) == p);
  1256. p = next_p;
  1257. if (p == header_ptr) break;
  1258. ++node_count;
  1259. }
  1260. BOOST_IF_CONSTEXPR(constant_time_size)
  1261. BOOST_INTRUSIVE_INVARIANT_ASSERT(this->priv_size_traits().get_size() == node_count);
  1262. }
  1263. friend bool operator==(const list_impl &x, const list_impl &y)
  1264. {
  1265. if(constant_time_size && x.size() != y.size()){
  1266. return false;
  1267. }
  1268. return ::boost::intrusive::algo_equal(x.cbegin(), x.cend(), y.cbegin(), y.cend());
  1269. }
  1270. inline friend bool operator!=(const list_impl &x, const list_impl &y)
  1271. { return !(x == y); }
  1272. inline friend bool operator<(const list_impl &x, const list_impl &y)
  1273. { return ::boost::intrusive::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); }
  1274. inline friend bool operator>(const list_impl &x, const list_impl &y)
  1275. { return y < x; }
  1276. inline friend bool operator<=(const list_impl &x, const list_impl &y)
  1277. { return !(y < x); }
  1278. inline friend bool operator>=(const list_impl &x, const list_impl &y)
  1279. { return !(x < y); }
  1280. inline friend void swap(list_impl &x, list_impl &y) BOOST_NOEXCEPT
  1281. { x.swap(y); }
  1282. /// @cond
  1283. private:
  1284. BOOST_INTRUSIVE_NO_DANGLING
  1285. static list_impl &priv_container_from_end_iterator(const const_iterator &end_iterator) BOOST_NOEXCEPT
  1286. {
  1287. BOOST_INTRUSIVE_STATIC_ASSERT((has_container_from_iterator));
  1288. node_ptr p = end_iterator.pointed_node();
  1289. header_holder_type* h = header_holder_type::get_holder(p);
  1290. root_plus_size* r = detail::parent_from_member
  1291. < root_plus_size, header_holder_type>(h, &root_plus_size::m_header);
  1292. data_t *d = detail::parent_from_member<data_t, root_plus_size>
  1293. ( r, &data_t::root_plus_size_);
  1294. list_impl *s = detail::parent_from_member<list_impl, data_t>(d, &list_impl::data_);
  1295. return *s;
  1296. }
  1297. /// @endcond
  1298. };
  1299. //! Helper metafunction to define a \c list that yields to the same type when the
  1300. //! same options (either explicitly or implicitly) are used.
  1301. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1302. template<class T, class ...Options>
  1303. #else
  1304. template<class T, class O1 = void, class O2 = void, class O3 = void, class O4 = void>
  1305. #endif
  1306. struct make_list
  1307. {
  1308. /// @cond
  1309. typedef typename pack_options
  1310. < list_defaults,
  1311. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1312. O1, O2, O3, O4
  1313. #else
  1314. Options...
  1315. #endif
  1316. >::type packed_options;
  1317. typedef typename detail::get_value_traits
  1318. <T, typename packed_options::proto_value_traits>::type value_traits;
  1319. typedef list_impl
  1320. <
  1321. value_traits,
  1322. typename packed_options::size_type,
  1323. packed_options::constant_time_size,
  1324. typename packed_options::header_holder_type
  1325. > implementation_defined;
  1326. /// @endcond
  1327. typedef implementation_defined type;
  1328. };
  1329. #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  1330. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1331. template<class T, class O1, class O2, class O3, class O4>
  1332. #else
  1333. template<class T, class ...Options>
  1334. #endif
  1335. class list
  1336. : public make_list<T,
  1337. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1338. O1, O2, O3, O4
  1339. #else
  1340. Options...
  1341. #endif
  1342. >::type
  1343. {
  1344. typedef typename make_list
  1345. <T,
  1346. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1347. O1, O2, O3, O4
  1348. #else
  1349. Options...
  1350. #endif
  1351. >::type Base;
  1352. //Assert if passed value traits are compatible with the type
  1353. BOOST_INTRUSIVE_STATIC_ASSERT((detail::is_same<typename Base::value_traits::value_type, T>::value));
  1354. BOOST_MOVABLE_BUT_NOT_COPYABLE(list)
  1355. public:
  1356. typedef typename Base::value_traits value_traits;
  1357. typedef typename Base::iterator iterator;
  1358. typedef typename Base::const_iterator const_iterator;
  1359. inline list()
  1360. : Base()
  1361. {}
  1362. inline explicit list(const value_traits &v_traits)
  1363. : Base(v_traits)
  1364. {}
  1365. template<class Iterator>
  1366. inline list(Iterator b, Iterator e, const value_traits &v_traits = value_traits())
  1367. : Base(b, e, v_traits)
  1368. {}
  1369. inline list(BOOST_RV_REF(list) x)
  1370. : Base(BOOST_MOVE_BASE(Base, x))
  1371. {}
  1372. inline list& operator=(BOOST_RV_REF(list) x)
  1373. { return static_cast<list &>(this->Base::operator=(BOOST_MOVE_BASE(Base, x))); }
  1374. template <class Cloner, class Disposer>
  1375. inline void clone_from(const list &src, Cloner cloner, Disposer disposer)
  1376. { Base::clone_from(src, cloner, disposer); }
  1377. template <class Cloner, class Disposer>
  1378. inline void clone_from(BOOST_RV_REF(list) src, Cloner cloner, Disposer disposer)
  1379. { Base::clone_from(BOOST_MOVE_BASE(Base, src), cloner, disposer); }
  1380. BOOST_INTRUSIVE_NO_DANGLING
  1381. inline static list &container_from_end_iterator(iterator end_iterator) BOOST_NOEXCEPT
  1382. { return static_cast<list &>(Base::container_from_end_iterator(end_iterator)); }
  1383. BOOST_INTRUSIVE_NO_DANGLING
  1384. inline static const list &container_from_end_iterator(const_iterator end_iterator) BOOST_NOEXCEPT
  1385. { return static_cast<const list &>(Base::container_from_end_iterator(end_iterator)); }
  1386. };
  1387. #endif
  1388. } //namespace intrusive
  1389. } //namespace boost
  1390. #include <boost/intrusive/detail/config_end.hpp>
  1391. #endif //BOOST_INTRUSIVE_LIST_HPP