list.hpp 56 KB

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