list.hpp 57 KB

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