list.hpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  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. inline static list_impl &container_from_end_iterator(iterator end_iterator) BOOST_NOEXCEPT
  444. { return list_impl::priv_container_from_end_iterator(end_iterator); }
  445. //! <b>Precondition</b>: end_iterator must be a valid end const_iterator
  446. //! of list.
  447. //!
  448. //! <b>Effects</b>: Returns a const reference to the list associated to the end iterator
  449. //!
  450. //! <b>Throws</b>: Nothing.
  451. //!
  452. //! <b>Complexity</b>: Constant.
  453. inline static const list_impl &container_from_end_iterator(const_iterator end_iterator) BOOST_NOEXCEPT
  454. { return list_impl::priv_container_from_end_iterator(end_iterator); }
  455. //! <b>Effects</b>: Returns the number of the elements contained in the list.
  456. //!
  457. //! <b>Throws</b>: Nothing.
  458. //!
  459. //! <b>Complexity</b>: Linear to the number of elements contained in the list.
  460. //! if constant-time size option is disabled. Constant time otherwise.
  461. //!
  462. //! <b>Note</b>: Does not affect the validity of iterators and references.
  463. inline size_type size() const BOOST_NOEXCEPT
  464. {
  465. BOOST_IF_CONSTEXPR(constant_time_size)
  466. return this->priv_size_traits().get_size();
  467. else
  468. return node_algorithms::count(this->get_root_node()) - 1;
  469. }
  470. //! <b>Effects</b>: Returns true if the list contains no elements.
  471. //!
  472. //! <b>Throws</b>: Nothing.
  473. //!
  474. //! <b>Complexity</b>: Constant.
  475. //!
  476. //! <b>Note</b>: Does not affect the validity of iterators and references.
  477. inline bool empty() const BOOST_NOEXCEPT
  478. { return node_algorithms::unique(this->get_root_node()); }
  479. //! <b>Effects</b>: Swaps the elements of x and *this.
  480. //!
  481. //! <b>Throws</b>: Nothing.
  482. //!
  483. //! <b>Complexity</b>: Constant.
  484. //!
  485. //! <b>Note</b>: Does not affect the validity of iterators and references.
  486. void swap(list_impl& other) BOOST_NOEXCEPT
  487. {
  488. node_algorithms::swap_nodes(this->get_root_node(), other.get_root_node());
  489. this->priv_size_traits().swap(other.priv_size_traits());
  490. }
  491. //! <b>Effects</b>: Moves backwards all the elements, so that the first
  492. //! element becomes the second, the second becomes the third...
  493. //! the last element becomes the first one.
  494. //!
  495. //! <b>Throws</b>: Nothing.
  496. //!
  497. //! <b>Complexity</b>: Linear to the number of shifts.
  498. //!
  499. //! <b>Note</b>: Does not affect the validity of iterators and references.
  500. inline void shift_backwards(size_type n = 1) BOOST_NOEXCEPT
  501. { node_algorithms::move_forward(this->get_root_node(), n); }
  502. //! <b>Effects</b>: Moves forward all the elements, so that the second
  503. //! element becomes the first, the third becomes the second...
  504. //! the first element becomes the last one.
  505. //!
  506. //! <b>Throws</b>: Nothing.
  507. //!
  508. //! <b>Complexity</b>: Linear to the number of shifts.
  509. //!
  510. //! <b>Note</b>: Does not affect the validity of iterators and references.
  511. inline void shift_forward(size_type n = 1) BOOST_NOEXCEPT
  512. { node_algorithms::move_backwards(this->get_root_node(), n); }
  513. //! <b>Effects</b>: Erases the element pointed by i of the list.
  514. //! No destructors are called.
  515. //!
  516. //! <b>Returns</b>: the first element remaining beyond the removed element,
  517. //! or end() if no such element exists.
  518. //!
  519. //! <b>Throws</b>: Nothing.
  520. //!
  521. //! <b>Complexity</b>: Constant.
  522. //!
  523. //! <b>Note</b>: Invalidates the iterators (but not the references) to the
  524. //! erased element.
  525. inline iterator erase(const_iterator i) BOOST_NOEXCEPT
  526. { return this->erase_and_dispose(i, detail::null_disposer()); }
  527. //! <b>Requires</b>: b and e must be valid iterators to elements in *this.
  528. //!
  529. //! <b>Effects</b>: Erases the element range pointed by b and e
  530. //! No destructors are called.
  531. //!
  532. //! <b>Returns</b>: the first element remaining beyond the removed elements,
  533. //! or end() if no such element exists.
  534. //!
  535. //! <b>Throws</b>: Nothing.
  536. //!
  537. //! <b>Complexity</b>: Linear to the number of erased elements if it's a safe-mode
  538. //! or auto-unlink value, or constant-time size is enabled. Constant-time otherwise.
  539. //!
  540. //! <b>Note</b>: Invalidates the iterators (but not the references) to the
  541. //! erased elements.
  542. inline iterator erase(const_iterator b, const_iterator e) BOOST_NOEXCEPT
  543. {
  544. BOOST_IF_CONSTEXPR(safemode_or_autounlink || constant_time_size){
  545. return this->erase_and_dispose(b, e, detail::null_disposer());
  546. }
  547. else{
  548. node_algorithms::unlink(b.pointed_node(), e.pointed_node());
  549. return e.unconst();
  550. }
  551. }
  552. //! <b>Requires</b>: b and e must be valid iterators to elements in *this.
  553. //! n must be distance(b, e).
  554. //!
  555. //! <b>Effects</b>: Erases the element range pointed by b and e
  556. //! No destructors are called.
  557. //!
  558. //! <b>Returns</b>: the first element remaining beyond the removed elements,
  559. //! or end() if no such element exists.
  560. //!
  561. //! <b>Throws</b>: Nothing.
  562. //!
  563. //! <b>Complexity</b>: Linear to the number of erased elements if it's a safe-mode
  564. //! or auto-unlink value is enabled. Constant-time otherwise.
  565. //!
  566. //! <b>Note</b>: Invalidates the iterators (but not the references) to the
  567. //! erased elements.
  568. iterator erase(const_iterator b, const_iterator e, size_type n) BOOST_NOEXCEPT
  569. {
  570. BOOST_INTRUSIVE_INVARIANT_ASSERT(node_algorithms::distance(b.pointed_node(), e.pointed_node()) == n);
  571. (void)n;
  572. BOOST_IF_CONSTEXPR(safemode_or_autounlink){
  573. return this->erase_and_dispose(b, e, detail::null_disposer());
  574. }
  575. else{
  576. BOOST_IF_CONSTEXPR(constant_time_size){
  577. this->priv_size_traits().decrease(n);
  578. }
  579. node_algorithms::unlink(b.pointed_node(), e.pointed_node());
  580. return e.unconst();
  581. }
  582. }
  583. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  584. //!
  585. //! <b>Effects</b>: Erases the element pointed by i of the list.
  586. //! No destructors are called.
  587. //! Disposer::operator()(pointer) is called for the removed element.
  588. //!
  589. //! <b>Returns</b>: the first element remaining beyond the removed element,
  590. //! or end() if no such element exists.
  591. //!
  592. //! <b>Throws</b>: Nothing.
  593. //!
  594. //! <b>Complexity</b>: Constant.
  595. //!
  596. //! <b>Note</b>: Invalidates the iterators to the erased element.
  597. template <class Disposer>
  598. iterator erase_and_dispose(const_iterator i, Disposer disposer) BOOST_NOEXCEPT
  599. {
  600. node_ptr to_erase(i.pointed_node());
  601. ++i;
  602. node_algorithms::unlink(to_erase);
  603. this->priv_size_traits().decrement();
  604. BOOST_IF_CONSTEXPR(safemode_or_autounlink)
  605. node_algorithms::init(to_erase);
  606. disposer(this->priv_value_traits().to_value_ptr(to_erase));
  607. return i.unconst();
  608. }
  609. #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  610. template<class Disposer>
  611. iterator erase_and_dispose(iterator i, Disposer disposer) BOOST_NOEXCEPT
  612. { return this->erase_and_dispose(const_iterator(i), disposer); }
  613. #endif
  614. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  615. //!
  616. //! <b>Effects</b>: Erases the element range pointed by b and e
  617. //! No destructors are called.
  618. //! Disposer::operator()(pointer) is called for the removed elements.
  619. //!
  620. //! <b>Returns</b>: the first element remaining beyond the removed elements,
  621. //! or end() if no such element exists.
  622. //!
  623. //! <b>Throws</b>: Nothing.
  624. //!
  625. //! <b>Complexity</b>: Linear to the number of elements erased.
  626. //!
  627. //! <b>Note</b>: Invalidates the iterators to the erased elements.
  628. template <class Disposer>
  629. iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer) BOOST_NOEXCEPT
  630. {
  631. node_ptr bp(b.pointed_node()), ep(e.pointed_node());
  632. node_algorithms::unlink(bp, ep);
  633. while(bp != ep){
  634. node_ptr to_erase(bp);
  635. bp = node_traits::get_next(bp);
  636. BOOST_IF_CONSTEXPR(safemode_or_autounlink)
  637. node_algorithms::init(to_erase);
  638. disposer(priv_value_traits().to_value_ptr(to_erase));
  639. this->priv_size_traits().decrement();
  640. }
  641. return e.unconst();
  642. }
  643. //! <b>Effects</b>: Erases all the elements of the container.
  644. //! No destructors are called.
  645. //!
  646. //! <b>Throws</b>: Nothing.
  647. //!
  648. //! <b>Complexity</b>: Linear to the number of elements of the list.
  649. //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise.
  650. //!
  651. //! <b>Note</b>: Invalidates the iterators (but not the references) to the erased elements.
  652. void clear() BOOST_NOEXCEPT
  653. {
  654. BOOST_IF_CONSTEXPR(safemode_or_autounlink){
  655. this->clear_and_dispose(detail::null_disposer());
  656. }
  657. else{
  658. node_algorithms::init_header(this->get_root_node());
  659. this->priv_size_traits().set_size(size_type(0));
  660. }
  661. }
  662. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  663. //!
  664. //! <b>Effects</b>: Erases all the elements of the container.
  665. //! No destructors are called.
  666. //! Disposer::operator()(pointer) is called for the removed elements.
  667. //!
  668. //! <b>Throws</b>: Nothing.
  669. //!
  670. //! <b>Complexity</b>: Linear to the number of elements of the list.
  671. //!
  672. //! <b>Note</b>: Invalidates the iterators to the erased elements.
  673. template <class Disposer>
  674. void clear_and_dispose(Disposer disposer) BOOST_NOEXCEPT
  675. {
  676. const_iterator it(this->begin()), itend(this->end());
  677. while(it != itend){
  678. node_ptr to_erase(it.pointed_node());
  679. ++it;
  680. BOOST_IF_CONSTEXPR(safemode_or_autounlink)
  681. node_algorithms::init(to_erase);
  682. disposer(priv_value_traits().to_value_ptr(to_erase));
  683. }
  684. node_algorithms::init_header(this->get_root_node());
  685. this->priv_size_traits().set_size(0);
  686. }
  687. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  688. //! Cloner should yield to nodes equivalent to the original nodes.
  689. //!
  690. //! <b>Effects</b>: Erases all the elements from *this
  691. //! calling Disposer::operator()(pointer), clones all the
  692. //! elements from src calling Cloner::operator()(const_reference )
  693. //! and inserts them on *this.
  694. //!
  695. //! If cloner throws, all cloned elements are unlinked and disposed
  696. //! calling Disposer::operator()(pointer).
  697. //!
  698. //! <b>Complexity</b>: Linear to erased plus inserted elements.
  699. //!
  700. //! <b>Throws</b>: If cloner throws. Basic guarantee.
  701. template <class Cloner, class Disposer>
  702. void clone_from(const list_impl &src, Cloner cloner, Disposer disposer)
  703. {
  704. this->clear_and_dispose(disposer);
  705. detail::exception_disposer<list_impl, Disposer>
  706. rollback(*this, disposer);
  707. const_iterator b(src.begin()), e(src.end());
  708. for(; b != e; ++b){
  709. this->push_back(*cloner(*b));
  710. }
  711. rollback.release();
  712. }
  713. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  714. //! Cloner should yield to nodes equivalent to the original nodes.
  715. //!
  716. //! <b>Effects</b>: Erases all the elements from *this
  717. //! calling Disposer::operator()(pointer), clones all the
  718. //! elements from src calling Cloner::operator()(reference)
  719. //! and inserts them on *this.
  720. //!
  721. //! If cloner throws, all cloned elements are unlinked and disposed
  722. //! calling Disposer::operator()(pointer).
  723. //!
  724. //! <b>Complexity</b>: Linear to erased plus inserted elements.
  725. //!
  726. //! <b>Throws</b>: If cloner throws. Basic guarantee.
  727. template <class Cloner, class Disposer>
  728. void clone_from(BOOST_RV_REF(list_impl) src, Cloner cloner, Disposer disposer)
  729. {
  730. this->clear_and_dispose(disposer);
  731. detail::exception_disposer<list_impl, Disposer>
  732. rollback(*this, disposer);
  733. iterator b(src.begin()), e(src.end());
  734. for(; b != e; ++b){
  735. this->push_back(*cloner(*b));
  736. }
  737. rollback.release();
  738. }
  739. //! <b>Requires</b>: value must be an lvalue and p must be a valid iterator of *this.
  740. //!
  741. //! <b>Effects</b>: Inserts the value before the position pointed by p.
  742. //!
  743. //! <b>Returns</b>: An iterator to the inserted element.
  744. //!
  745. //! <b>Throws</b>: Nothing.
  746. //!
  747. //! <b>Complexity</b>: Constant time. No copy constructors are called.
  748. //!
  749. //! <b>Note</b>: Does not affect the validity of iterators and references.
  750. iterator insert(const_iterator p, reference value) BOOST_NOEXCEPT
  751. {
  752. node_ptr to_insert = this->priv_value_traits().to_node_ptr(value);
  753. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::inited(to_insert));
  754. node_algorithms::link_before(p.pointed_node(), to_insert);
  755. this->priv_size_traits().increment();
  756. return iterator(to_insert, this->priv_value_traits_ptr());
  757. }
  758. //! <b>Requires</b>: Dereferencing iterator must yield
  759. //! an lvalue of type value_type and p must be a valid iterator of *this.
  760. //!
  761. //! <b>Effects</b>: Inserts the range pointed by b and e before the position p.
  762. //! No copy constructors are called.
  763. //!
  764. //! <b>Throws</b>: Nothing.
  765. //!
  766. //! <b>Complexity</b>: Linear to the number of elements inserted.
  767. //!
  768. //! <b>Note</b>: Does not affect the validity of iterators and references.
  769. template<class Iterator>
  770. void insert(const_iterator p, Iterator b, Iterator e) BOOST_NOEXCEPT
  771. {
  772. for (; b != e; ++b)
  773. this->insert(p, *b);
  774. }
  775. //! <b>Requires</b>: Dereferencing iterator must yield
  776. //! an lvalue of type value_type.
  777. //!
  778. //! <b>Effects</b>: Clears the list and inserts the range pointed by b and e.
  779. //! No destructors or copy constructors are called.
  780. //!
  781. //! <b>Throws</b>: Nothing.
  782. //!
  783. //! <b>Complexity</b>: Linear to the number of elements inserted plus
  784. //! linear to the elements contained in the list if it's a safe-mode
  785. //! or auto-unlink value.
  786. //! Linear to the number of elements inserted in the list otherwise.
  787. //!
  788. //! <b>Note</b>: Invalidates the iterators (but not the references)
  789. //! to the erased elements.
  790. template<class Iterator>
  791. void assign(Iterator b, Iterator e) BOOST_NOEXCEPT
  792. {
  793. this->clear();
  794. this->insert(this->cend(), b, e);
  795. }
  796. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  797. //!
  798. //! <b>Requires</b>: Dereferencing iterator must yield
  799. //! an lvalue of type value_type.
  800. //!
  801. //! <b>Effects</b>: Clears the list and inserts the range pointed by b and e.
  802. //! No destructors or copy constructors are called.
  803. //! Disposer::operator()(pointer) is called for the removed elements.
  804. //!
  805. //! <b>Throws</b>: Nothing.
  806. //!
  807. //! <b>Complexity</b>: Linear to the number of elements inserted plus
  808. //! linear to the elements contained in the list.
  809. //!
  810. //! <b>Note</b>: Invalidates the iterators (but not the references)
  811. //! to the erased elements.
  812. template<class Iterator, class Disposer>
  813. void dispose_and_assign(Disposer disposer, Iterator b, Iterator e) BOOST_NOEXCEPT
  814. {
  815. this->clear_and_dispose(disposer);
  816. this->insert(this->cend(), b, e);
  817. }
  818. //! <b>Requires</b>: p must be a valid iterator of *this.
  819. //!
  820. //! <b>Effects</b>: Transfers all the elements of list x to this list, before the
  821. //! the element pointed by p. No destructors or copy constructors are called.
  822. //!
  823. //! <b>Throws</b>: Nothing.
  824. //!
  825. //! <b>Complexity</b>: Constant.
  826. //!
  827. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of
  828. //! this list. Iterators of this list and all the references are not invalidated.
  829. void splice(const_iterator p, list_impl& x) BOOST_NOEXCEPT
  830. {
  831. if(!x.empty()){
  832. node_algorithms::transfer
  833. (p.pointed_node(), x.begin().pointed_node(), x.end().pointed_node());
  834. size_traits &thist = this->priv_size_traits();
  835. size_traits &xt = x.priv_size_traits();
  836. thist.increase(xt.get_size());
  837. xt.set_size(size_type(0));
  838. }
  839. }
  840. //! <b>Requires</b>: p must be a valid iterator of *this.
  841. //! new_ele must point to an element contained in list x.
  842. //!
  843. //! <b>Effects</b>: Transfers the value pointed by new_ele, from list x to this list,
  844. //! before the element pointed by p. No destructors or copy constructors are called.
  845. //! If p == new_ele or p == ++new_ele, this function is a null operation.
  846. //!
  847. //! <b>Throws</b>: Nothing.
  848. //!
  849. //! <b>Complexity</b>: Constant.
  850. //!
  851. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  852. //! list. Iterators of this list and all the references are not invalidated.
  853. void splice(const_iterator p, list_impl&x, const_iterator new_ele) BOOST_NOEXCEPT
  854. {
  855. node_algorithms::transfer(p.pointed_node(), new_ele.pointed_node());
  856. x.priv_size_traits().decrement();
  857. this->priv_size_traits().increment();
  858. }
  859. //! <b>Requires</b>: p must be a valid iterator of *this.
  860. //! f and e must point to elements contained in list x.
  861. //!
  862. //! <b>Effects</b>: Transfers the range pointed by f and e from list x to this list,
  863. //! before the element pointed by p. No destructors or copy constructors are called.
  864. //!
  865. //! <b>Throws</b>: Nothing.
  866. //!
  867. //! <b>Complexity</b>: Linear to the number of elements transferred
  868. //! if constant-time size option is enabled. Constant-time otherwise.
  869. //!
  870. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  871. //! list. Iterators of this list and all the references are not invalidated.
  872. void splice(const_iterator p, list_impl&x, const_iterator f, const_iterator e) BOOST_NOEXCEPT
  873. {
  874. BOOST_IF_CONSTEXPR(constant_time_size)
  875. this->splice(p, x, f, e, node_algorithms::distance(f.pointed_node(), e.pointed_node()));
  876. else
  877. this->splice(p, x, f, e, 1);//intrusive::iterator_distance is a dummy value
  878. }
  879. //! <b>Requires</b>: p must be a valid iterator of *this.
  880. //! f and e must point to elements contained in list x.
  881. //! n == distance(f, e)
  882. //!
  883. //! <b>Effects</b>: Transfers the range pointed by f and e from list x to this list,
  884. //! before the element pointed by p. No destructors or copy constructors are called.
  885. //!
  886. //! <b>Throws</b>: Nothing.
  887. //!
  888. //! <b>Complexity</b>: Constant.
  889. //!
  890. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  891. //! list. Iterators of this list and all the references are not invalidated.
  892. void splice(const_iterator p, list_impl&x, const_iterator f, const_iterator e, size_type n) BOOST_NOEXCEPT
  893. {
  894. if(n){
  895. BOOST_IF_CONSTEXPR(constant_time_size){
  896. BOOST_INTRUSIVE_INVARIANT_ASSERT(n == node_algorithms::distance(f.pointed_node(), e.pointed_node()));
  897. node_algorithms::transfer(p.pointed_node(), f.pointed_node(), e.pointed_node());
  898. size_traits &thist = this->priv_size_traits();
  899. size_traits &xt = x.priv_size_traits();
  900. thist.increase(n);
  901. xt.decrease(n);
  902. }
  903. else{
  904. node_algorithms::transfer(p.pointed_node(), f.pointed_node(), e.pointed_node());
  905. }
  906. }
  907. }
  908. //! <b>Effects</b>: This function sorts the list *this according to operator <.
  909. //! The sort is stable, that is, the relative order of equivalent elements is preserved.
  910. //!
  911. //! <b>Throws</b>: If value_traits::node_traits::node
  912. //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
  913. //! or operator < throws. Basic guarantee.
  914. //!
  915. //! <b>Notes</b>: Iterators and references are not invalidated.
  916. //!
  917. //! <b>Complexity</b>: The number of comparisons is approximately N log N, where N
  918. //! is the list's size.
  919. void sort()
  920. { this->sort(value_less<value_type>()); }
  921. //! <b>Requires</b>: p must be a comparison function that induces a strict weak ordering
  922. //!
  923. //! <b>Effects</b>: This function sorts the list *this according to p. The sort is
  924. //! stable, that is, the relative order of equivalent elements is preserved.
  925. //!
  926. //! <b>Throws</b>: If value_traits::node_traits::node
  927. //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
  928. //! or the predicate throws. Basic guarantee.
  929. //!
  930. //! <b>Notes</b>: This won't throw if list_base_hook<> or
  931. //! list_member_hook are used.
  932. //! Iterators and references are not invalidated.
  933. //!
  934. //! <b>Complexity</b>: The number of comparisons is approximately N log N, where N
  935. //! is the list's size.
  936. template<class Predicate>
  937. void sort(Predicate p)
  938. {
  939. if(node_traits::get_next(this->get_root_node())
  940. != node_traits::get_previous(this->get_root_node())){
  941. list_impl carry(this->priv_value_traits());
  942. detail::array_initializer<list_impl, 64> counter(this->priv_value_traits());
  943. int fill = 0;
  944. while(!this->empty()){
  945. carry.splice(carry.cbegin(), *this, this->cbegin());
  946. int i = 0;
  947. while(i < fill && !counter[i].empty()) {
  948. counter[i].merge(carry, p);
  949. carry.swap(counter[i++]);
  950. }
  951. carry.swap(counter[i]);
  952. if(i == fill)
  953. ++fill;
  954. }
  955. for (int i = 1; i < fill; ++i)
  956. counter[i].merge(counter[i-1], p);
  957. this->swap(counter[fill-1]);
  958. }
  959. }
  960. //! <b>Effects</b>: This function removes all of x's elements and inserts them
  961. //! in order into *this according to operator <. The merge is stable;
  962. //! that is, if an element from *this is equivalent to one from x, then the element
  963. //! from *this will precede the one from x.
  964. //!
  965. //! <b>Throws</b>: If operator < throws. Basic guarantee.
  966. //!
  967. //! <b>Complexity</b>: This function is linear time: it performs at most
  968. //! size() + x.size() - 1 comparisons.
  969. //!
  970. //! <b>Note</b>: Iterators and references are not invalidated
  971. void merge(list_impl& x)
  972. { this->merge(x, value_less<value_type>()); }
  973. //! <b>Requires</b>: p must be a comparison function that induces a strict weak
  974. //! ordering and both *this and x must be sorted according to that ordering
  975. //! The lists x and *this must be distinct.
  976. //!
  977. //! <b>Effects</b>: This function removes all of x's elements and inserts them
  978. //! in order into *this. The merge is stable; that is, if an element from *this is
  979. //! equivalent to one from x, then the element from *this will precede the one from x.
  980. //!
  981. //! <b>Throws</b>: If the predicate throws. Basic guarantee.
  982. //!
  983. //! <b>Complexity</b>: This function is linear time: it performs at most
  984. //! size() + x.size() - 1 comparisons.
  985. //!
  986. //! <b>Note</b>: Iterators and references are not invalidated.
  987. template<class Predicate>
  988. void merge(list_impl& x, Predicate p)
  989. {
  990. const_iterator e(this->cend()), ex(x.cend());
  991. const_iterator b(this->cbegin());
  992. while(!x.empty()){
  993. const_iterator ix(x.cbegin());
  994. while (b != e && !p(*ix, *b)){
  995. ++b;
  996. }
  997. if(b == e){
  998. //Now transfer the rest to the end of the container
  999. this->splice(e, x);
  1000. break;
  1001. }
  1002. else{
  1003. size_type n(0);
  1004. do{
  1005. ++ix; ++n;
  1006. } while(ix != ex && p(*ix, *b));
  1007. this->splice(b, x, x.begin(), ix, n);
  1008. }
  1009. }
  1010. }
  1011. //! <b>Effects</b>: Reverses the order of elements in the list.
  1012. //!
  1013. //! <b>Throws</b>: Nothing.
  1014. //!
  1015. //! <b>Complexity</b>: This function is linear time.
  1016. //!
  1017. //! <b>Note</b>: Iterators and references are not invalidated
  1018. void reverse() BOOST_NOEXCEPT
  1019. { node_algorithms::reverse(this->get_root_node()); }
  1020. //! <b>Effects</b>: Removes all the elements that compare equal to value.
  1021. //! No destructors are called.
  1022. //!
  1023. //! <b>Throws</b>: If operator == throws. Basic guarantee.
  1024. //!
  1025. //! <b>Complexity</b>: Linear time. It performs exactly size() comparisons for equality.
  1026. //!
  1027. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1028. //! and iterators to elements that are not removed remain valid.
  1029. void remove(const_reference value) BOOST_NOEXCEPT
  1030. { this->remove_if(detail::equal_to_value<const_reference>(value)); }
  1031. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1032. //!
  1033. //! <b>Effects</b>: Removes all the elements that compare equal to value.
  1034. //! Disposer::operator()(pointer) is called for every removed element.
  1035. //!
  1036. //! <b>Throws</b>: If operator == throws. Basic guarantee.
  1037. //!
  1038. //! <b>Complexity</b>: Linear time. It performs exactly size() comparisons for equality.
  1039. //!
  1040. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1041. //! and iterators to elements that are not removed remain valid.
  1042. template<class Disposer>
  1043. void remove_and_dispose(const_reference value, Disposer disposer) BOOST_NOEXCEPT
  1044. { this->remove_and_dispose_if(detail::equal_to_value<const_reference>(value), disposer); }
  1045. //! <b>Effects</b>: Removes all the elements for which a specified
  1046. //! predicate is satisfied. No destructors are called.
  1047. //!
  1048. //! <b>Throws</b>: If pred throws. Basic guarantee.
  1049. //!
  1050. //! <b>Complexity</b>: Linear time. It performs exactly size() calls to the predicate.
  1051. //!
  1052. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1053. //! and iterators to elements that are not removed remain valid.
  1054. template<class Pred>
  1055. void remove_if(Pred pred)
  1056. {
  1057. const node_ptr root_node = this->get_root_node();
  1058. typename node_algorithms::stable_partition_info info;
  1059. node_algorithms::stable_partition
  1060. (node_traits::get_next(root_node), root_node, detail::key_nodeptr_comp<Pred, value_traits>(pred, &this->priv_value_traits()), info);
  1061. //Invariants preserved by stable_partition so erase can be safely called
  1062. //The first element might have changed so calculate it again
  1063. this->erase( const_iterator(node_traits::get_next(root_node), this->priv_value_traits_ptr())
  1064. , const_iterator(info.beg_2st_partition, this->priv_value_traits_ptr())
  1065. , info.num_1st_partition);
  1066. }
  1067. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1068. //!
  1069. //! <b>Effects</b>: Removes all the elements for which a specified
  1070. //! predicate is satisfied.
  1071. //! Disposer::operator()(pointer) is called for every removed element.
  1072. //!
  1073. //! <b>Throws</b>: If pred throws. Basic guarantee.
  1074. //!
  1075. //! <b>Complexity</b>: Linear time. It performs exactly size() comparisons for equality.
  1076. //!
  1077. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1078. //! and iterators to elements that are not removed remain valid.
  1079. template<class Pred, class Disposer>
  1080. void remove_and_dispose_if(Pred pred, Disposer disposer)
  1081. {
  1082. const node_ptr root_node = this->get_root_node();
  1083. typename node_algorithms::stable_partition_info info;
  1084. node_algorithms::stable_partition
  1085. (node_traits::get_next(root_node), root_node, detail::key_nodeptr_comp<Pred, value_traits>(pred, &this->priv_value_traits()), info);
  1086. //Invariants preserved by stable_partition so erase can be safely called
  1087. //The first element might have changed so calculate it again
  1088. this->erase_and_dispose( const_iterator(node_traits::get_next(root_node), this->priv_value_traits_ptr())
  1089. , const_iterator(info.beg_2st_partition, this->priv_value_traits_ptr())
  1090. , disposer);
  1091. }
  1092. //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
  1093. //! elements that are equal from the list. No destructors are called.
  1094. //!
  1095. //! <b>Throws</b>: If the comparison operator throws. Basic guarantee.
  1096. //!
  1097. //! <b>Complexity</b>: Linear time (size()-1 comparisons calls to pred()).
  1098. //!
  1099. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1100. //! and iterators to elements that are not removed remain valid.
  1101. void unique()
  1102. { this->unique_and_dispose(value_equal<value_type>(), detail::null_disposer()); }
  1103. //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
  1104. //! elements that satisfy some binary predicate from the list.
  1105. //! No destructors are called.
  1106. //!
  1107. //! <b>Throws</b>: If pred throws. Basic guarantee.
  1108. //!
  1109. //! <b>Complexity</b>: Linear time (size()-1 comparisons equality comparisons).
  1110. //!
  1111. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1112. //! and iterators to elements that are not removed remain valid.
  1113. template<class BinaryPredicate>
  1114. void unique(BinaryPredicate pred)
  1115. { this->unique_and_dispose(pred, detail::null_disposer()); }
  1116. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1117. //!
  1118. //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
  1119. //! elements that are equal from the list.
  1120. //! Disposer::operator()(pointer) is called for every removed element.
  1121. //!
  1122. //! <b>Throws</b>: If the equality operator throws. Basic guarantee.
  1123. //!
  1124. //! <b>Complexity</b>: Linear time (size()-1) comparisons equality comparisons.
  1125. //!
  1126. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1127. //! and iterators to elements that are not removed remain valid.
  1128. template<class Disposer>
  1129. void unique_and_dispose(Disposer disposer)
  1130. { this->unique_and_dispose(value_equal<value_type>(), disposer); }
  1131. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1132. //!
  1133. //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
  1134. //! elements that satisfy some binary predicate from the list.
  1135. //! Disposer::operator()(pointer) is called for every removed element.
  1136. //!
  1137. //! <b>Throws</b>: If pred throws. Basic guarantee.
  1138. //!
  1139. //! <b>Complexity</b>: Linear time (size()-1) comparisons equality comparisons.
  1140. //!
  1141. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1142. //! and iterators to elements that are not removed remain valid.
  1143. template<class BinaryPredicate, class Disposer>
  1144. void unique_and_dispose(BinaryPredicate pred, Disposer disposer)
  1145. {
  1146. const_iterator itend(this->cend());
  1147. const_iterator cur(this->cbegin());
  1148. if(cur != itend){
  1149. const_iterator after(cur);
  1150. ++after;
  1151. while(after != itend){
  1152. if(pred(*cur, *after)){
  1153. after = this->erase_and_dispose(after, disposer);
  1154. }
  1155. else{
  1156. cur = after;
  1157. ++after;
  1158. }
  1159. }
  1160. }
  1161. }
  1162. //! <b>Requires</b>: value must be a reference to a value inserted in a list.
  1163. //!
  1164. //! <b>Effects</b>: This function returns a const_iterator pointing to the element
  1165. //!
  1166. //! <b>Throws</b>: Nothing.
  1167. //!
  1168. //! <b>Complexity</b>: Constant time.
  1169. //!
  1170. //! <b>Note</b>: Iterators and references are not invalidated.
  1171. //! This static function is available only if the <i>value traits</i>
  1172. //! is stateless.
  1173. static iterator s_iterator_to(reference value) BOOST_NOEXCEPT
  1174. {
  1175. BOOST_INTRUSIVE_STATIC_ASSERT((!stateful_value_traits));
  1176. BOOST_INTRUSIVE_INVARIANT_ASSERT(!node_algorithms::inited(value_traits::to_node_ptr(value)));
  1177. return iterator(value_traits::to_node_ptr(value), const_value_traits_ptr());
  1178. }
  1179. //! <b>Requires</b>: value must be a const reference to a value inserted in a list.
  1180. //!
  1181. //! <b>Effects</b>: This function returns an iterator pointing to the element.
  1182. //!
  1183. //! <b>Throws</b>: Nothing.
  1184. //!
  1185. //! <b>Complexity</b>: Constant time.
  1186. //!
  1187. //! <b>Note</b>: Iterators and references are not invalidated.
  1188. //! This static function is available only if the <i>value traits</i>
  1189. //! is stateless.
  1190. static const_iterator s_iterator_to(const_reference value) BOOST_NOEXCEPT
  1191. {
  1192. BOOST_INTRUSIVE_STATIC_ASSERT((!stateful_value_traits));
  1193. reference r =*detail::uncast(pointer_traits<const_pointer>::pointer_to(value));
  1194. BOOST_INTRUSIVE_INVARIANT_ASSERT(!node_algorithms::inited(value_traits::to_node_ptr(r)));
  1195. return const_iterator(value_traits::to_node_ptr(r), const_value_traits_ptr());
  1196. }
  1197. //! <b>Requires</b>: value must be a reference to a value inserted in a list.
  1198. //!
  1199. //! <b>Effects</b>: This function returns a const_iterator pointing to the element
  1200. //!
  1201. //! <b>Throws</b>: Nothing.
  1202. //!
  1203. //! <b>Complexity</b>: Constant time.
  1204. //!
  1205. //! <b>Note</b>: Iterators and references are not invalidated.
  1206. iterator iterator_to(reference value) BOOST_NOEXCEPT
  1207. {
  1208. BOOST_INTRUSIVE_INVARIANT_ASSERT(!node_algorithms::inited(this->priv_value_traits().to_node_ptr(value)));
  1209. return iterator(this->priv_value_traits().to_node_ptr(value), this->priv_value_traits_ptr());
  1210. }
  1211. //! <b>Requires</b>: value must be a const reference to a value inserted in a list.
  1212. //!
  1213. //! <b>Effects</b>: This function returns an iterator pointing to the element.
  1214. //!
  1215. //! <b>Throws</b>: Nothing.
  1216. //!
  1217. //! <b>Complexity</b>: Constant time.
  1218. //!
  1219. //! <b>Note</b>: Iterators and references are not invalidated.
  1220. const_iterator iterator_to(const_reference value) const BOOST_NOEXCEPT
  1221. {
  1222. reference r = *detail::uncast(pointer_traits<const_pointer>::pointer_to(value));
  1223. BOOST_INTRUSIVE_INVARIANT_ASSERT(!node_algorithms::inited(this->priv_value_traits().to_node_ptr(r)));
  1224. return const_iterator(this->priv_value_traits().to_node_ptr(r), this->priv_value_traits_ptr());
  1225. }
  1226. //! <b>Effects</b>: Asserts the integrity of the container.
  1227. //!
  1228. //! <b>Complexity</b>: Linear time.
  1229. //!
  1230. //! <b>Note</b>: The method has no effect when asserts are turned off (e.g., with NDEBUG).
  1231. //! Experimental function, interface might change in future versions.
  1232. void check() const
  1233. {
  1234. const_node_ptr header_ptr = get_root_node();
  1235. // header's next and prev are never null
  1236. BOOST_INTRUSIVE_INVARIANT_ASSERT(node_traits::get_next(header_ptr));
  1237. BOOST_INTRUSIVE_INVARIANT_ASSERT(node_traits::get_previous(header_ptr));
  1238. // header's next and prev either both point to header (empty list) or neither does
  1239. BOOST_INTRUSIVE_INVARIANT_ASSERT((node_traits::get_next(header_ptr) == header_ptr)
  1240. == (node_traits::get_previous(header_ptr) == header_ptr));
  1241. if (node_traits::get_next(header_ptr) == header_ptr)
  1242. {
  1243. BOOST_IF_CONSTEXPR(constant_time_size)
  1244. BOOST_INTRUSIVE_INVARIANT_ASSERT(this->priv_size_traits().get_size() == 0);
  1245. return;
  1246. }
  1247. size_t node_count = 0; (void)node_count;
  1248. const_node_ptr p = header_ptr;
  1249. while (true)
  1250. {
  1251. const_node_ptr next_p = node_traits::get_next(p);
  1252. BOOST_INTRUSIVE_INVARIANT_ASSERT(next_p);
  1253. BOOST_INTRUSIVE_INVARIANT_ASSERT(node_traits::get_previous(next_p) == p);
  1254. p = next_p;
  1255. if (p == header_ptr) break;
  1256. ++node_count;
  1257. }
  1258. BOOST_IF_CONSTEXPR(constant_time_size)
  1259. BOOST_INTRUSIVE_INVARIANT_ASSERT(this->priv_size_traits().get_size() == node_count);
  1260. }
  1261. friend bool operator==(const list_impl &x, const list_impl &y)
  1262. {
  1263. if(constant_time_size && x.size() != y.size()){
  1264. return false;
  1265. }
  1266. return ::boost::intrusive::algo_equal(x.cbegin(), x.cend(), y.cbegin(), y.cend());
  1267. }
  1268. inline friend bool operator!=(const list_impl &x, const list_impl &y)
  1269. { return !(x == y); }
  1270. inline friend bool operator<(const list_impl &x, const list_impl &y)
  1271. { return ::boost::intrusive::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); }
  1272. inline friend bool operator>(const list_impl &x, const list_impl &y)
  1273. { return y < x; }
  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 !(x < y); }
  1278. inline friend void swap(list_impl &x, list_impl &y) BOOST_NOEXCEPT
  1279. { x.swap(y); }
  1280. /// @cond
  1281. private:
  1282. static list_impl &priv_container_from_end_iterator(const const_iterator &end_iterator) BOOST_NOEXCEPT
  1283. {
  1284. BOOST_INTRUSIVE_STATIC_ASSERT((has_container_from_iterator));
  1285. node_ptr p = end_iterator.pointed_node();
  1286. header_holder_type* h = header_holder_type::get_holder(p);
  1287. root_plus_size* r = detail::parent_from_member
  1288. < root_plus_size, header_holder_type>(h, &root_plus_size::m_header);
  1289. data_t *d = detail::parent_from_member<data_t, root_plus_size>
  1290. ( r, &data_t::root_plus_size_);
  1291. list_impl *s = detail::parent_from_member<list_impl, data_t>(d, &list_impl::data_);
  1292. return *s;
  1293. }
  1294. /// @endcond
  1295. };
  1296. //! Helper metafunction to define a \c list that yields to the same type when the
  1297. //! same options (either explicitly or implicitly) are used.
  1298. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1299. template<class T, class ...Options>
  1300. #else
  1301. template<class T, class O1 = void, class O2 = void, class O3 = void, class O4 = void>
  1302. #endif
  1303. struct make_list
  1304. {
  1305. /// @cond
  1306. typedef typename pack_options
  1307. < list_defaults,
  1308. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1309. O1, O2, O3, O4
  1310. #else
  1311. Options...
  1312. #endif
  1313. >::type packed_options;
  1314. typedef typename detail::get_value_traits
  1315. <T, typename packed_options::proto_value_traits>::type value_traits;
  1316. typedef list_impl
  1317. <
  1318. value_traits,
  1319. typename packed_options::size_type,
  1320. packed_options::constant_time_size,
  1321. typename packed_options::header_holder_type
  1322. > implementation_defined;
  1323. /// @endcond
  1324. typedef implementation_defined type;
  1325. };
  1326. #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  1327. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1328. template<class T, class O1, class O2, class O3, class O4>
  1329. #else
  1330. template<class T, class ...Options>
  1331. #endif
  1332. class list
  1333. : public make_list<T,
  1334. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1335. O1, O2, O3, O4
  1336. #else
  1337. Options...
  1338. #endif
  1339. >::type
  1340. {
  1341. typedef typename make_list
  1342. <T,
  1343. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1344. O1, O2, O3, O4
  1345. #else
  1346. Options...
  1347. #endif
  1348. >::type Base;
  1349. //Assert if passed value traits are compatible with the type
  1350. BOOST_INTRUSIVE_STATIC_ASSERT((detail::is_same<typename Base::value_traits::value_type, T>::value));
  1351. BOOST_MOVABLE_BUT_NOT_COPYABLE(list)
  1352. public:
  1353. typedef typename Base::value_traits value_traits;
  1354. typedef typename Base::iterator iterator;
  1355. typedef typename Base::const_iterator const_iterator;
  1356. inline list()
  1357. : Base()
  1358. {}
  1359. inline explicit list(const value_traits &v_traits)
  1360. : Base(v_traits)
  1361. {}
  1362. template<class Iterator>
  1363. inline list(Iterator b, Iterator e, const value_traits &v_traits = value_traits())
  1364. : Base(b, e, v_traits)
  1365. {}
  1366. inline list(BOOST_RV_REF(list) x)
  1367. : Base(BOOST_MOVE_BASE(Base, x))
  1368. {}
  1369. inline list& operator=(BOOST_RV_REF(list) x)
  1370. { return static_cast<list &>(this->Base::operator=(BOOST_MOVE_BASE(Base, x))); }
  1371. template <class Cloner, class Disposer>
  1372. inline void clone_from(const list &src, Cloner cloner, Disposer disposer)
  1373. { Base::clone_from(src, cloner, disposer); }
  1374. template <class Cloner, class Disposer>
  1375. inline void clone_from(BOOST_RV_REF(list) src, Cloner cloner, Disposer disposer)
  1376. { Base::clone_from(BOOST_MOVE_BASE(Base, src), cloner, disposer); }
  1377. inline static list &container_from_end_iterator(iterator end_iterator) BOOST_NOEXCEPT
  1378. { return static_cast<list &>(Base::container_from_end_iterator(end_iterator)); }
  1379. inline static const list &container_from_end_iterator(const_iterator end_iterator) BOOST_NOEXCEPT
  1380. { return static_cast<const list &>(Base::container_from_end_iterator(end_iterator)); }
  1381. };
  1382. #endif
  1383. } //namespace intrusive
  1384. } //namespace boost
  1385. #include <boost/intrusive/detail/config_end.hpp>
  1386. #endif //BOOST_INTRUSIVE_LIST_HPP