list.hpp 59 KB

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