list.hpp 55 KB

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