list.hpp 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2015. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_CONTAINER_LIST_HPP
  11. #define BOOST_CONTAINER_LIST_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #if defined(BOOST_HAS_PRAGMA_ONCE)
  16. # pragma once
  17. #endif
  18. #include <boost/container/detail/config_begin.hpp>
  19. #include <boost/container/detail/workaround.hpp>
  20. // container
  21. #include <boost/container/container_fwd.hpp>
  22. #include <boost/container/new_allocator.hpp> //new_allocator
  23. #include <boost/container/throw_exception.hpp>
  24. // container/detail
  25. #include <boost/container/detail/algorithm.hpp>
  26. #include <boost/container/detail/compare_functors.hpp>
  27. #include <boost/container/detail/iterator.hpp>
  28. #include <boost/container/detail/iterators.hpp>
  29. #include <boost/container/detail/mpl.hpp>
  30. #include <boost/container/detail/node_alloc_holder.hpp>
  31. #include <boost/container/detail/version_type.hpp>
  32. #include <boost/container/detail/value_functors.hpp>
  33. // move
  34. #include <boost/move/utility_core.hpp>
  35. #include <boost/move/iterator.hpp>
  36. #include <boost/move/traits.hpp>
  37. // move/detail
  38. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  39. # include <boost/move/detail/fwd_macros.hpp>
  40. #endif
  41. #include <boost/move/detail/move_helpers.hpp>
  42. // intrusive
  43. #include <boost/intrusive/pointer_traits.hpp>
  44. #include <boost/intrusive/list.hpp>
  45. // other
  46. #include <boost/assert.hpp>
  47. // std
  48. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  49. #include <initializer_list>
  50. #endif
  51. namespace boost {
  52. namespace container {
  53. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  54. namespace dtl {
  55. template<class VoidPointer>
  56. struct list_hook
  57. {
  58. typedef typename dtl::bi::make_list_base_hook
  59. <dtl::bi::void_pointer<VoidPointer>, dtl::bi::link_mode<dtl::bi::normal_link> >::type type;
  60. };
  61. template <class T, class VoidPointer>
  62. struct iiterator_node_value_type< base_node<T, list_hook<VoidPointer> > >
  63. {
  64. typedef T type;
  65. };
  66. template<class Allocator>
  67. struct intrusive_list_type
  68. {
  69. typedef boost::container::allocator_traits<Allocator> allocator_traits_type;
  70. typedef typename allocator_traits_type::value_type value_type;
  71. typedef typename boost::intrusive::pointer_traits
  72. <typename allocator_traits_type::pointer>::template
  73. rebind_pointer<void>::type
  74. void_pointer;
  75. typedef base_node<value_type, list_hook<void_pointer> > node_type;
  76. typedef typename dtl::bi::make_list
  77. < node_type
  78. , dtl::bi::base_hook<typename list_hook<void_pointer>::type>
  79. , dtl::bi::constant_time_size<true>
  80. , dtl::bi::size_type
  81. <typename allocator_traits_type::size_type>
  82. >::type container_type;
  83. typedef container_type type;
  84. };
  85. } //namespace dtl {
  86. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  87. //! A list is a doubly linked list. That is, it is a Sequence that supports both
  88. //! forward and backward traversal, and (amortized) constant time insertion and
  89. //! removal of elements at the beginning or the end, or in the middle. Lists have
  90. //! the important property that insertion and splicing do not invalidate iterators
  91. //! to list elements, and that even removal invalidates only the iterators that point
  92. //! to the elements that are removed. The ordering of iterators may be changed
  93. //! (that is, list<T>::iterator might have a different predecessor or successor
  94. //! after a list operation than it did before), but the iterators themselves will
  95. //! not be invalidated or made to point to different elements unless that invalidation
  96. //! or mutation is explicit.
  97. //!
  98. //! \tparam T The type of object that is stored in the list
  99. //! \tparam Allocator The allocator used for all internal memory management, use void
  100. //! for the default allocator
  101. #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
  102. template <class T, class Allocator = void >
  103. #else
  104. template <class T, class Allocator>
  105. #endif
  106. class list
  107. : protected dtl::node_alloc_holder
  108. < typename real_allocator<T, Allocator>::type
  109. , typename dtl::intrusive_list_type<typename real_allocator<T, Allocator>::type>::type>
  110. {
  111. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  112. typedef typename real_allocator<T, Allocator>::type ValueAllocator;
  113. typedef typename
  114. dtl::intrusive_list_type<ValueAllocator>::type Icont;
  115. typedef dtl::node_alloc_holder<ValueAllocator, Icont> AllocHolder;
  116. typedef typename AllocHolder::NodePtr NodePtr;
  117. typedef typename AllocHolder::NodeAlloc NodeAlloc;
  118. typedef typename AllocHolder::ValAlloc ValAlloc;
  119. typedef typename AllocHolder::Node Node;
  120. typedef dtl::allocator_node_destroyer<NodeAlloc> Destroyer;
  121. typedef typename AllocHolder::alloc_version alloc_version;
  122. typedef boost::container::allocator_traits<ValueAllocator> allocator_traits_type;
  123. typedef boost::container::equal_to_value
  124. <typename allocator_traits_type::value_type> equal_to_value_type;
  125. BOOST_COPYABLE_AND_MOVABLE(list)
  126. typedef dtl::iterator_from_iiterator<typename Icont::iterator, false> iterator_impl;
  127. typedef dtl::iterator_from_iiterator<typename Icont::iterator, true> const_iterator_impl;
  128. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  129. public:
  130. //////////////////////////////////////////////
  131. //
  132. // types
  133. //
  134. //////////////////////////////////////////////
  135. typedef T value_type;
  136. typedef typename ::boost::container::allocator_traits<ValueAllocator>::pointer pointer;
  137. typedef typename ::boost::container::allocator_traits<ValueAllocator>::const_pointer const_pointer;
  138. typedef typename ::boost::container::allocator_traits<ValueAllocator>::reference reference;
  139. typedef typename ::boost::container::allocator_traits<ValueAllocator>::const_reference const_reference;
  140. typedef typename ::boost::container::allocator_traits<ValueAllocator>::size_type size_type;
  141. typedef typename ::boost::container::allocator_traits<ValueAllocator>::difference_type difference_type;
  142. typedef ValueAllocator allocator_type;
  143. typedef BOOST_CONTAINER_IMPDEF(NodeAlloc) stored_allocator_type;
  144. typedef BOOST_CONTAINER_IMPDEF(iterator_impl) iterator;
  145. typedef BOOST_CONTAINER_IMPDEF(const_iterator_impl) const_iterator;
  146. typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<iterator>) reverse_iterator;
  147. typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<const_iterator>) const_reverse_iterator;
  148. //////////////////////////////////////////////
  149. //
  150. // construct/copy/destroy
  151. //
  152. //////////////////////////////////////////////
  153. //! <b>Effects</b>: Default constructs a list.
  154. //!
  155. //! <b>Throws</b>: If allocator_type's default constructor throws.
  156. //!
  157. //! <b>Complexity</b>: Constant.
  158. list() BOOST_NOEXCEPT_IF(dtl::is_nothrow_default_constructible<ValueAllocator>::value)
  159. : AllocHolder()
  160. {}
  161. //! <b>Effects</b>: Constructs a list taking the allocator as parameter.
  162. //!
  163. //! <b>Throws</b>: Nothing
  164. //!
  165. //! <b>Complexity</b>: Constant.
  166. explicit list(const allocator_type &a) BOOST_NOEXCEPT_OR_NOTHROW
  167. : AllocHolder(a)
  168. {}
  169. //! <b>Effects</b>: Constructs a list
  170. //! and inserts n value-initialized value_types.
  171. //!
  172. //! <b>Throws</b>: If allocator_type's default constructor
  173. //! throws or T's default or copy constructor throws.
  174. //!
  175. //! <b>Complexity</b>: Linear to n.
  176. explicit list(size_type n)
  177. : AllocHolder(ValueAllocator())
  178. { this->resize(n); }
  179. //! <b>Effects</b>: Constructs a list that will use a copy of allocator a
  180. //! and inserts n copies of value.
  181. //!
  182. //! <b>Throws</b>: If allocator_type's default constructor
  183. //! throws or T's default or copy constructor throws.
  184. //!
  185. //! <b>Complexity</b>: Linear to n.
  186. list(size_type n, const allocator_type &a)
  187. : AllocHolder(a)
  188. { this->resize(n); }
  189. //! <b>Effects</b>: Constructs a list that will use a copy of allocator a
  190. //! and inserts n copies of value.
  191. //!
  192. //! <b>Throws</b>: If allocator_type's default constructor
  193. //! throws or T's default or copy constructor throws.
  194. //!
  195. //! <b>Complexity</b>: Linear to n.
  196. list(size_type n, const T& value, const ValueAllocator& a = ValueAllocator())
  197. : AllocHolder(a)
  198. { this->insert(this->cbegin(), n, value); }
  199. //! <b>Effects</b>: Copy constructs a list.
  200. //!
  201. //! <b>Postcondition</b>: x == *this.
  202. //!
  203. //! <b>Throws</b>: If allocator_type's default constructor throws.
  204. //!
  205. //! <b>Complexity</b>: Linear to the elements x contains.
  206. list(const list& x)
  207. : AllocHolder(x)
  208. { this->insert(this->cbegin(), x.begin(), x.end()); }
  209. //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
  210. //!
  211. //! <b>Throws</b>: If allocator_type's copy constructor throws.
  212. //!
  213. //! <b>Complexity</b>: Constant.
  214. list(BOOST_RV_REF(list) x) BOOST_NOEXCEPT_OR_NOTHROW
  215. : AllocHolder(BOOST_MOVE_BASE(AllocHolder, x))
  216. {}
  217. //! <b>Effects</b>: Copy constructs a list using the specified allocator.
  218. //!
  219. //! <b>Postcondition</b>: x == *this.
  220. //!
  221. //! <b>Throws</b>: If allocator_type's default constructor or copy constructor throws.
  222. //!
  223. //! <b>Complexity</b>: Linear to the elements x contains.
  224. list(const list& x, const allocator_type &a)
  225. : AllocHolder(a)
  226. { this->insert(this->cbegin(), x.begin(), x.end()); }
  227. //! <b>Effects</b>: Move constructor sing the specified allocator.
  228. //! Moves x's resources to *this.
  229. //!
  230. //! <b>Throws</b>: If allocation or value_type's copy constructor throws.
  231. //!
  232. //! <b>Complexity</b>: Constant if a == x.get_allocator(), linear otherwise.
  233. list(BOOST_RV_REF(list) x, const allocator_type &a)
  234. : AllocHolder(a)
  235. {
  236. if(this->node_alloc() == x.node_alloc()){
  237. this->icont().swap(x.icont());
  238. }
  239. else{
  240. this->insert(this->cbegin(), boost::make_move_iterator(x.begin()), boost::make_move_iterator(x.end()));
  241. }
  242. }
  243. //! <b>Effects</b>: Constructs a list that will use a copy of allocator a
  244. //! and inserts a copy of the range [first, last) in the list.
  245. //!
  246. //! <b>Throws</b>: If allocator_type's default constructor
  247. //! throws or T's constructor taking a dereferenced InIt throws.
  248. //!
  249. //! <b>Complexity</b>: Linear to the range [first, last).
  250. template <class InpIt>
  251. list(InpIt first, InpIt last, const ValueAllocator &a = ValueAllocator())
  252. : AllocHolder(a)
  253. { this->insert(this->cbegin(), first, last); }
  254. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  255. //! <b>Effects</b>: Constructs a list that will use a copy of allocator a
  256. //! and inserts a copy of the range [il.begin(), il.end()) in the list.
  257. //!
  258. //! <b>Throws</b>: If allocator_type's default constructor
  259. //! throws or T's constructor taking a dereferenced
  260. //! std::initializer_list iterator throws.
  261. //!
  262. //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
  263. list(std::initializer_list<value_type> il, const ValueAllocator &a = ValueAllocator())
  264. : AllocHolder(a)
  265. { this->insert(this->cbegin(), il.begin(), il.end()); }
  266. #endif
  267. //! <b>Effects</b>: Destroys the list. All stored values are destroyed
  268. //! and used memory is deallocated.
  269. //!
  270. //! <b>Throws</b>: Nothing.
  271. //!
  272. //! <b>Complexity</b>: Linear to the number of elements.
  273. inline ~list() BOOST_NOEXCEPT_OR_NOTHROW
  274. {} //AllocHolder clears the list
  275. //! <b>Effects</b>: Makes *this contain the same elements as x.
  276. //!
  277. //! <b>Postcondition</b>: this->size() == x.size(). *this contains a copy
  278. //! of each of x's elements.
  279. //!
  280. //! <b>Throws</b>: If memory allocation throws or T's copy constructor throws.
  281. //!
  282. //! <b>Complexity</b>: Linear to the number of elements in x.
  283. list& operator=(BOOST_COPY_ASSIGN_REF(list) x)
  284. {
  285. if (BOOST_LIKELY(this != &x)) {
  286. NodeAlloc &this_alloc = this->node_alloc();
  287. const NodeAlloc &x_alloc = x.node_alloc();
  288. dtl::bool_<allocator_traits_type::
  289. propagate_on_container_copy_assignment::value> flag;
  290. if(flag && this_alloc != x_alloc){
  291. this->clear();
  292. }
  293. this->AllocHolder::copy_assign_alloc(x);
  294. this->assign(x.begin(), x.end());
  295. }
  296. return *this;
  297. }
  298. //! <b>Effects</b>: Move assignment. All x's values are transferred to *this.
  299. //!
  300. //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
  301. //! before the function.
  302. //!
  303. //! <b>Throws</b>: If allocator_traits_type::propagate_on_container_move_assignment
  304. //! is false and (allocation throws or value_type's move constructor throws)
  305. //!
  306. //! <b>Complexity</b>: Constant if allocator_traits_type::
  307. //! propagate_on_container_move_assignment is true or
  308. //! this->get>allocator() == x.get_allocator(). Linear otherwise.
  309. list& operator=(BOOST_RV_REF(list) x)
  310. BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value
  311. || allocator_traits_type::is_always_equal::value)
  312. {
  313. if (BOOST_LIKELY(this != &x)) {
  314. //We know resources can be transferred at comiple time if both allocators are
  315. //always equal or the allocator is going to be propagated
  316. const bool can_steal_resources_alloc
  317. = allocator_traits_type::propagate_on_container_move_assignment::value
  318. || allocator_traits_type::is_always_equal::value;
  319. dtl::bool_<can_steal_resources_alloc> flag;
  320. this->priv_move_assign(boost::move(x), flag);
  321. }
  322. return *this;
  323. }
  324. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  325. //! <b>Effects</b>: Makes *this contain the same elements as il.
  326. //!
  327. //! <b>Postcondition</b>: this->size() == il.size(). *this contains a copy
  328. //! of each of x's elements.
  329. //!
  330. //! <b>Throws</b>: If memory allocation throws or T's copy constructor throws.
  331. //!
  332. //! <b>Complexity</b>: Linear to the number of elements in x.
  333. inline list& operator=(std::initializer_list<value_type> il)
  334. {
  335. this->assign(il.begin(), il.end());
  336. return *this;
  337. }
  338. #endif
  339. //! <b>Effects</b>: Assigns the n copies of val to *this.
  340. //!
  341. //! <b>Throws</b>: If memory allocation throws or T's copy constructor throws.
  342. //!
  343. //! <b>Complexity</b>: Linear to n.
  344. inline void assign(size_type n, const T& val)
  345. {
  346. typedef constant_iterator<value_type> cvalue_iterator;
  347. return this->assign(cvalue_iterator(val, n), cvalue_iterator());
  348. }
  349. //! <b>Effects</b>: Assigns the range [first, last) to *this.
  350. //!
  351. //! <b>Throws</b>: If memory allocation throws or
  352. //! T's constructor from dereferencing InpIt throws.
  353. //!
  354. //! <b>Complexity</b>: Linear to n.
  355. template <class InpIt>
  356. void assign(InpIt first, InpIt last
  357. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  358. , typename dtl::disable_if_convertible<InpIt, size_type>::type * = 0
  359. #endif
  360. )
  361. {
  362. iterator first1 = this->begin();
  363. const iterator last1 = this->end();
  364. for ( ; first1 != last1 && first != last; ++first1, ++first)
  365. *first1 = *first;
  366. if (first == last)
  367. this->erase(first1, last1);
  368. else{
  369. this->insert(last1, first, last);
  370. }
  371. }
  372. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  373. //! <b>Effects</b>: Assigns the range [il.begin(), il.end()) to *this.
  374. //!
  375. //! <b>Throws</b>: If memory allocation throws or
  376. //! T's constructor from dereferencing std::initializer_list iterator throws.
  377. //!
  378. //! <b>Complexity</b>: Linear to n.
  379. inline void assign(std::initializer_list<value_type> il)
  380. { this->assign(il.begin(), il.end()); }
  381. #endif
  382. //! <b>Effects</b>: Returns a copy of the internal allocator.
  383. //!
  384. //! <b>Throws</b>: If allocator's copy constructor throws.
  385. //!
  386. //! <b>Complexity</b>: Constant.
  387. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  388. allocator_type get_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
  389. { return allocator_type(this->node_alloc()); }
  390. //! <b>Effects</b>: Returns a reference to the internal allocator.
  391. //!
  392. //! <b>Throws</b>: Nothing
  393. //!
  394. //! <b>Complexity</b>: Constant.
  395. //!
  396. //! <b>Note</b>: Non-standard extension.
  397. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  398. stored_allocator_type &get_stored_allocator() BOOST_NOEXCEPT_OR_NOTHROW
  399. { return this->node_alloc(); }
  400. //! <b>Effects</b>: Returns a reference to the internal allocator.
  401. //!
  402. //! <b>Throws</b>: Nothing
  403. //!
  404. //! <b>Complexity</b>: Constant.
  405. //!
  406. //! <b>Note</b>: Non-standard extension.
  407. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  408. const stored_allocator_type &get_stored_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
  409. { return this->node_alloc(); }
  410. //////////////////////////////////////////////
  411. //
  412. // iterators
  413. //
  414. //////////////////////////////////////////////
  415. //! <b>Effects</b>: Returns an iterator to the first element contained in the list.
  416. //!
  417. //! <b>Throws</b>: Nothing.
  418. //!
  419. //! <b>Complexity</b>: Constant.
  420. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  421. iterator begin() BOOST_NOEXCEPT_OR_NOTHROW
  422. { return iterator(this->icont().begin()); }
  423. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the list.
  424. //!
  425. //! <b>Throws</b>: Nothing.
  426. //!
  427. //! <b>Complexity</b>: Constant.
  428. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  429. const_iterator begin() const BOOST_NOEXCEPT_OR_NOTHROW
  430. { return this->cbegin(); }
  431. //! <b>Effects</b>: Returns an iterator to the end of the list.
  432. //!
  433. //! <b>Throws</b>: Nothing.
  434. //!
  435. //! <b>Complexity</b>: Constant.
  436. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  437. iterator end() BOOST_NOEXCEPT_OR_NOTHROW
  438. { return iterator(this->icont().end()); }
  439. //! <b>Effects</b>: Returns a const_iterator to the end of the list.
  440. //!
  441. //! <b>Throws</b>: Nothing.
  442. //!
  443. //! <b>Complexity</b>: Constant.
  444. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  445. const_iterator end() const BOOST_NOEXCEPT_OR_NOTHROW
  446. { return this->cend(); }
  447. //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
  448. //! of the reversed list.
  449. //!
  450. //! <b>Throws</b>: Nothing.
  451. //!
  452. //! <b>Complexity</b>: Constant.
  453. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  454. reverse_iterator rbegin() BOOST_NOEXCEPT_OR_NOTHROW
  455. { return reverse_iterator(end()); }
  456. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  457. //! of the reversed list.
  458. //!
  459. //! <b>Throws</b>: Nothing.
  460. //!
  461. //! <b>Complexity</b>: Constant.
  462. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  463. const_reverse_iterator rbegin() const BOOST_NOEXCEPT_OR_NOTHROW
  464. { return this->crbegin(); }
  465. //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
  466. //! of the reversed list.
  467. //!
  468. //! <b>Throws</b>: Nothing.
  469. //!
  470. //! <b>Complexity</b>: Constant.
  471. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  472. reverse_iterator rend() BOOST_NOEXCEPT_OR_NOTHROW
  473. { return reverse_iterator(begin()); }
  474. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  475. //! of the reversed list.
  476. //!
  477. //! <b>Throws</b>: Nothing.
  478. //!
  479. //! <b>Complexity</b>: Constant.
  480. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  481. const_reverse_iterator rend() const BOOST_NOEXCEPT_OR_NOTHROW
  482. { return this->crend(); }
  483. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the list.
  484. //!
  485. //! <b>Throws</b>: Nothing.
  486. //!
  487. //! <b>Complexity</b>: Constant.
  488. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  489. const_iterator cbegin() const BOOST_NOEXCEPT_OR_NOTHROW
  490. { return const_iterator(this->non_const_icont().begin()); }
  491. //! <b>Effects</b>: Returns a const_iterator to the end of the list.
  492. //!
  493. //! <b>Throws</b>: Nothing.
  494. //!
  495. //! <b>Complexity</b>: Constant.
  496. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  497. const_iterator cend() const BOOST_NOEXCEPT_OR_NOTHROW
  498. { return const_iterator(this->non_const_icont().end()); }
  499. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  500. //! of the reversed list.
  501. //!
  502. //! <b>Throws</b>: Nothing.
  503. //!
  504. //! <b>Complexity</b>: Constant.
  505. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  506. const_reverse_iterator crbegin() const BOOST_NOEXCEPT_OR_NOTHROW
  507. { return const_reverse_iterator(this->cend()); }
  508. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  509. //! of the reversed list.
  510. //!
  511. //! <b>Throws</b>: Nothing.
  512. //!
  513. //! <b>Complexity</b>: Constant.
  514. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  515. const_reverse_iterator crend() const BOOST_NOEXCEPT_OR_NOTHROW
  516. { return const_reverse_iterator(this->cbegin()); }
  517. //////////////////////////////////////////////
  518. //
  519. // capacity
  520. //
  521. //////////////////////////////////////////////
  522. //! <b>Effects</b>: Returns true if the list contains no elements.
  523. //!
  524. //! <b>Throws</b>: Nothing.
  525. //!
  526. //! <b>Complexity</b>: Constant.
  527. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  528. bool empty() const BOOST_NOEXCEPT_OR_NOTHROW
  529. { return !this->size(); }
  530. //! <b>Effects</b>: Returns the number of the elements contained in the list.
  531. //!
  532. //! <b>Throws</b>: Nothing.
  533. //!
  534. //! <b>Complexity</b>: Constant.
  535. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  536. size_type size() const BOOST_NOEXCEPT_OR_NOTHROW
  537. { return this->icont().size(); }
  538. //! <b>Effects</b>: Returns the largest possible size of the list.
  539. //!
  540. //! <b>Throws</b>: Nothing.
  541. //!
  542. //! <b>Complexity</b>: Constant.
  543. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  544. size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
  545. { return AllocHolder::max_size(); }
  546. //! <b>Effects</b>: Inserts or erases elements at the end such that
  547. //! the size becomes n. New elements are value initialized.
  548. //!
  549. //! <b>Throws</b>: If memory allocation throws, or T's copy constructor throws.
  550. //!
  551. //! <b>Complexity</b>: Linear to the difference between size() and new_size.
  552. void resize(size_type new_size)
  553. {
  554. if(!priv_try_shrink(new_size)){
  555. typedef value_init_construct_iterator<value_type> value_init_iterator;
  556. this->insert(this->cend(), value_init_iterator(new_size - this->size()), value_init_iterator());
  557. }
  558. }
  559. //! <b>Effects</b>: Inserts or erases elements at the end such that
  560. //! the size becomes n. New elements are copy constructed from x.
  561. //!
  562. //! <b>Throws</b>: If memory allocation throws, or T's copy constructor throws.
  563. //!
  564. //! <b>Complexity</b>: Linear to the difference between size() and new_size.
  565. void resize(size_type new_size, const T& x)
  566. {
  567. if(!priv_try_shrink(new_size)){
  568. this->insert(this->cend(), new_size - this->size(), x);
  569. }
  570. }
  571. //////////////////////////////////////////////
  572. //
  573. // element access
  574. //
  575. //////////////////////////////////////////////
  576. //! <b>Requires</b>: !empty()
  577. //!
  578. //! <b>Effects</b>: Returns a reference to the first element
  579. //! from the beginning of the container.
  580. //!
  581. //! <b>Throws</b>: Nothing.
  582. //!
  583. //! <b>Complexity</b>: Constant.
  584. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  585. reference front() BOOST_NOEXCEPT_OR_NOTHROW
  586. {
  587. BOOST_ASSERT(!this->empty());
  588. return *this->begin();
  589. }
  590. //! <b>Requires</b>: !empty()
  591. //!
  592. //! <b>Effects</b>: Returns a const reference to the first element
  593. //! from the beginning of the container.
  594. //!
  595. //! <b>Throws</b>: Nothing.
  596. //!
  597. //! <b>Complexity</b>: Constant.
  598. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  599. const_reference front() const BOOST_NOEXCEPT_OR_NOTHROW
  600. {
  601. BOOST_ASSERT(!this->empty());
  602. return *this->begin();
  603. }
  604. //! <b>Requires</b>: !empty()
  605. //!
  606. //! <b>Effects</b>: Returns a reference to the first element
  607. //! from the beginning of the container.
  608. //!
  609. //! <b>Throws</b>: Nothing.
  610. //!
  611. //! <b>Complexity</b>: Constant.
  612. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  613. reference back() BOOST_NOEXCEPT_OR_NOTHROW
  614. {
  615. BOOST_ASSERT(!this->empty());
  616. return *(--this->end());
  617. }
  618. //! <b>Requires</b>: !empty()
  619. //!
  620. //! <b>Effects</b>: Returns a const reference to the first element
  621. //! from the beginning of the container.
  622. //!
  623. //! <b>Throws</b>: Nothing.
  624. //!
  625. //! <b>Complexity</b>: Constant.
  626. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  627. const_reference back() const BOOST_NOEXCEPT_OR_NOTHROW
  628. {
  629. BOOST_ASSERT(!this->empty());
  630. return *(--this->end());
  631. }
  632. //////////////////////////////////////////////
  633. //
  634. // modifiers
  635. //
  636. //////////////////////////////////////////////
  637. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  638. //! <b>Effects</b>: Inserts an object of type T constructed with
  639. //! std::forward<Args>(args)... in the end of the list.
  640. //!
  641. //! <b>Returns</b>: A reference to the created object.
  642. //!
  643. //! <b>Throws</b>: If memory allocation throws or
  644. //! T's in-place constructor throws.
  645. //!
  646. //! <b>Complexity</b>: Constant
  647. template <class... Args>
  648. reference emplace_back(BOOST_FWD_REF(Args)... args)
  649. { return *this->emplace(this->cend(), boost::forward<Args>(args)...); }
  650. //! <b>Effects</b>: Inserts an object of type T constructed with
  651. //! std::forward<Args>(args)... in the beginning of the list.
  652. //!
  653. //! <b>Returns</b>: A reference to the created object.
  654. //!
  655. //! <b>Throws</b>: If memory allocation throws or
  656. //! T's in-place constructor throws.
  657. //!
  658. //! <b>Complexity</b>: Constant
  659. template <class... Args>
  660. reference emplace_front(BOOST_FWD_REF(Args)... args)
  661. { return *this->emplace(this->cbegin(), boost::forward<Args>(args)...); }
  662. //! <b>Effects</b>: Inserts an object of type T constructed with
  663. //! std::forward<Args>(args)... before p.
  664. //!
  665. //! <b>Throws</b>: If memory allocation throws or
  666. //! T's in-place constructor throws.
  667. //!
  668. //! <b>Complexity</b>: Constant
  669. template <class... Args>
  670. iterator emplace(const_iterator position, BOOST_FWD_REF(Args)... args)
  671. {
  672. BOOST_ASSERT((priv_is_linked)(position));
  673. NodePtr pnode(AllocHolder::create_node(boost::forward<Args>(args)...));
  674. return iterator(this->icont().insert(position.get(), *pnode));
  675. }
  676. #else // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  677. #define BOOST_CONTAINER_LIST_EMPLACE_CODE(N) \
  678. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  679. reference emplace_back(BOOST_MOVE_UREF##N)\
  680. { return *this->emplace(this->cend() BOOST_MOVE_I##N BOOST_MOVE_FWD##N); }\
  681. \
  682. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  683. reference emplace_front(BOOST_MOVE_UREF##N)\
  684. { return *this->emplace(this->cbegin() BOOST_MOVE_I##N BOOST_MOVE_FWD##N);}\
  685. \
  686. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  687. iterator emplace(const_iterator position BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  688. {\
  689. BOOST_ASSERT(position == this->cend() || (--(++position) == position) );\
  690. NodePtr pnode (AllocHolder::create_node(BOOST_MOVE_FWD##N));\
  691. return iterator(this->icont().insert(position.get(), *pnode));\
  692. }\
  693. //
  694. BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_LIST_EMPLACE_CODE)
  695. #undef BOOST_CONTAINER_LIST_EMPLACE_CODE
  696. #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  697. #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  698. //! <b>Effects</b>: Inserts a copy of x at the beginning of the list.
  699. //!
  700. //! <b>Throws</b>: If memory allocation throws or
  701. //! T's copy constructor throws.
  702. //!
  703. //! <b>Complexity</b>: Amortized constant time.
  704. void push_front(const T &x);
  705. //! <b>Effects</b>: Constructs a new element in the beginning of the list
  706. //! and moves the resources of x to this new element.
  707. //!
  708. //! <b>Throws</b>: If memory allocation throws.
  709. //!
  710. //! <b>Complexity</b>: Amortized constant time.
  711. void push_front(T &&x);
  712. #else
  713. BOOST_MOVE_CONVERSION_AWARE_CATCH(push_front, T, void, priv_push_front)
  714. #endif
  715. #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  716. //! <b>Effects</b>: Inserts a copy of x at the end of the list.
  717. //!
  718. //! <b>Throws</b>: If memory allocation throws or
  719. //! T's copy constructor throws.
  720. //!
  721. //! <b>Complexity</b>: Amortized constant time.
  722. void push_back(const T &x);
  723. //! <b>Effects</b>: Constructs a new element in the end of the list
  724. //! and moves the resources of x to this new element.
  725. //!
  726. //! <b>Throws</b>: If memory allocation throws.
  727. //!
  728. //! <b>Complexity</b>: Amortized constant time.
  729. void push_back(T &&x);
  730. #else
  731. BOOST_MOVE_CONVERSION_AWARE_CATCH(push_back, T, void, priv_push_back)
  732. #endif
  733. #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  734. //! <b>Requires</b>: p must be a valid iterator of *this.
  735. //!
  736. //! <b>Effects</b>: Insert a copy of x before p.
  737. //!
  738. //! <b>Returns</b>: an iterator to the inserted element.
  739. //!
  740. //! <b>Throws</b>: If memory allocation throws or x's copy constructor throws.
  741. //!
  742. //! <b>Complexity</b>: Amortized constant time.
  743. iterator insert(const_iterator p, const T &x);
  744. //! <b>Requires</b>: p must be a valid iterator of *this.
  745. //!
  746. //! <b>Effects</b>: Insert a new element before p with x's resources.
  747. //!
  748. //! <b>Returns</b>: an iterator to the inserted element.
  749. //!
  750. //! <b>Throws</b>: If memory allocation throws.
  751. //!
  752. //! <b>Complexity</b>: Amortized constant time.
  753. iterator insert(const_iterator p, T &&x);
  754. #else
  755. BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, T, iterator, priv_insert, const_iterator, const_iterator)
  756. #endif
  757. //! <b>Requires</b>: p must be a valid iterator of *this.
  758. //!
  759. //! <b>Effects</b>: Inserts n copies of x before p.
  760. //!
  761. //! <b>Returns</b>: an iterator to the first inserted element or p if n is 0.
  762. //!
  763. //! <b>Throws</b>: If memory allocation throws or T's copy constructor throws.
  764. //!
  765. //! <b>Complexity</b>: Linear to n.
  766. iterator insert(const_iterator position, size_type n, const T& x)
  767. {
  768. //range check is done by insert
  769. typedef constant_iterator<value_type> cvalue_iterator;
  770. return this->insert(position, cvalue_iterator(x, n), cvalue_iterator());
  771. }
  772. //! <b>Requires</b>: p must be a valid iterator of *this.
  773. //!
  774. //! <b>Effects</b>: Insert a copy of the [first, last) range before p.
  775. //!
  776. //! <b>Returns</b>: an iterator to the first inserted element or p if first == last.
  777. //!
  778. //! <b>Throws</b>: If memory allocation throws, T's constructor from a
  779. //! dereferenced InpIt throws.
  780. //!
  781. //! <b>Complexity</b>: Linear to distance [first, last).
  782. template <class InpIt>
  783. iterator insert(const_iterator p, InpIt first, InpIt last
  784. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  785. , typename dtl::enable_if_c
  786. < !dtl::is_convertible<InpIt, size_type>::value
  787. && (dtl::is_input_iterator<InpIt>::value
  788. || dtl::is_same<alloc_version, version_1>::value
  789. )
  790. >::type * = 0
  791. #endif
  792. )
  793. {
  794. BOOST_ASSERT((priv_is_linked)(p));
  795. const typename Icont::iterator ipos(p.get());
  796. iterator ret_it(ipos);
  797. if(first != last){
  798. ret_it = iterator(this->icont().insert(ipos, *this->create_node_from_it(first)));
  799. ++first;
  800. }
  801. for (; first != last; ++first){
  802. this->icont().insert(ipos, *this->create_node_from_it(first));
  803. }
  804. return ret_it;
  805. }
  806. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  807. template <class FwdIt>
  808. iterator insert(const_iterator position, FwdIt first, FwdIt last
  809. , typename dtl::enable_if_c
  810. < !dtl::is_convertible<FwdIt, size_type>::value
  811. && !(dtl::is_input_iterator<FwdIt>::value
  812. || dtl::is_same<alloc_version, version_1>::value
  813. )
  814. >::type * = 0
  815. )
  816. {
  817. BOOST_ASSERT((priv_is_linked)(position));
  818. //Optimized allocation and construction
  819. insertion_functor func(this->icont(), position.get());
  820. iterator before_p(position.get());
  821. --before_p;
  822. this->allocate_many_and_construct(first, boost::container::iterator_udistance(first, last), func);
  823. return ++before_p;
  824. }
  825. #endif
  826. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  827. //! <b>Requires</b>: p must be a valid iterator of *this.
  828. //!
  829. //! <b>Effects</b>: Insert a copy of the [il.begin(), il.end()) range before p.
  830. //!
  831. //! <b>Returns</b>: an iterator to the first inserted element or p if if.begin() == il.end().
  832. //!
  833. //! <b>Throws</b>: If memory allocation throws, T's constructor from a
  834. //! dereferenced std::initializer_list iterator throws.
  835. //!
  836. //! <b>Complexity</b>: Linear to distance [il.begin(), il.end()).
  837. iterator insert(const_iterator p, std::initializer_list<value_type> il)
  838. {
  839. //position range check is done by insert()
  840. return insert(p, il.begin(), il.end());
  841. }
  842. #endif
  843. //! <b>Effects</b>: Removes the first element from the list.
  844. //!
  845. //! <b>Throws</b>: Nothing.
  846. //!
  847. //! <b>Complexity</b>: Amortized constant time.
  848. void pop_front() BOOST_NOEXCEPT_OR_NOTHROW
  849. {
  850. BOOST_ASSERT(!this->empty());
  851. this->erase(this->cbegin());
  852. }
  853. //! <b>Effects</b>: Removes the last element from the list.
  854. //!
  855. //! <b>Throws</b>: Nothing.
  856. //!
  857. //! <b>Complexity</b>: Amortized constant time.
  858. void pop_back() BOOST_NOEXCEPT_OR_NOTHROW
  859. {
  860. BOOST_ASSERT(!this->empty());
  861. const_iterator tmp = this->cend();
  862. this->erase(--tmp);
  863. }
  864. //! <b>Requires</b>: p must be a valid iterator of *this.
  865. //!
  866. //! <b>Effects</b>: Erases the element at p.
  867. //!
  868. //! <b>Throws</b>: Nothing.
  869. //!
  870. //! <b>Complexity</b>: Amortized constant time.
  871. iterator erase(const_iterator p) BOOST_NOEXCEPT_OR_NOTHROW
  872. {
  873. BOOST_ASSERT(p != this->cend() && (priv_is_linked)(p));
  874. return iterator(this->icont().erase_and_dispose(p.get(), Destroyer(this->node_alloc())));
  875. }
  876. //! <b>Requires</b>: first and last must be valid iterator to elements in *this.
  877. //!
  878. //! <b>Effects</b>: Erases the elements pointed by [first, last).
  879. //!
  880. //! <b>Throws</b>: Nothing.
  881. //!
  882. //! <b>Complexity</b>: Linear to the distance between first and last.
  883. iterator erase(const_iterator first, const_iterator last) BOOST_NOEXCEPT_OR_NOTHROW
  884. {
  885. BOOST_ASSERT(first == last || (first != this->cend() && (priv_is_linked)(first)));
  886. BOOST_ASSERT(first == last || (priv_is_linked)(last));
  887. return iterator(AllocHolder::erase_range(first.get(), last.get(), alloc_version()));
  888. }
  889. //! <b>Effects</b>: Swaps the contents of *this and x.
  890. //!
  891. //! <b>Throws</b>: Nothing.
  892. //!
  893. //! <b>Complexity</b>: Constant.
  894. void swap(list& x)
  895. BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_swap::value
  896. || allocator_traits_type::is_always_equal::value)
  897. {
  898. BOOST_ASSERT(allocator_traits_type::propagate_on_container_swap::value ||
  899. allocator_traits_type::is_always_equal::value ||
  900. this->get_stored_allocator() == x.get_stored_allocator());
  901. AllocHolder::swap(x);
  902. }
  903. //! <b>Effects</b>: Erases all the elements of the list.
  904. //!
  905. //! <b>Throws</b>: Nothing.
  906. //!
  907. //! <b>Complexity</b>: Linear to the number of elements in the list.
  908. void clear() BOOST_NOEXCEPT_OR_NOTHROW
  909. { AllocHolder::clear(alloc_version()); }
  910. //////////////////////////////////////////////
  911. //
  912. // slist operations
  913. //
  914. //////////////////////////////////////////////
  915. //! <b>Requires</b>: p must point to an element contained
  916. //! by the list. x != *this. this' allocator and x's allocator shall compare equal
  917. //!
  918. //! <b>Effects</b>: Transfers all the elements of list x to this list, before the
  919. //! the element pointed by p. No destructors or copy constructors are called.
  920. //!
  921. //! <b>Throws</b>: Nothing
  922. //!
  923. //! <b>Complexity</b>: Constant.
  924. //!
  925. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of
  926. //! this list. Iterators of this list and all the references are not invalidated.
  927. void splice(const_iterator p, list& x) BOOST_NOEXCEPT_OR_NOTHROW
  928. {
  929. BOOST_ASSERT((priv_is_linked)(p));
  930. BOOST_ASSERT(this != &x);
  931. BOOST_ASSERT(this->node_alloc() == x.node_alloc());
  932. this->icont().splice(p.get(), x.icont());
  933. }
  934. //! <b>Requires</b>: p must point to an element contained
  935. //! by the list. x != *this. this' allocator and x's allocator shall compare equal
  936. //!
  937. //! <b>Effects</b>: Transfers all the elements of list x to this list, before the
  938. //! the element pointed by p. No destructors or copy constructors are called.
  939. //!
  940. //! <b>Throws</b>: Nothing
  941. //!
  942. //! <b>Complexity</b>: Constant.
  943. //!
  944. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of
  945. //! this list. Iterators of this list and all the references are not invalidated.
  946. void splice(const_iterator p, BOOST_RV_REF(list) x) BOOST_NOEXCEPT_OR_NOTHROW
  947. {
  948. //Checks done in splice
  949. this->splice(p, static_cast<list&>(x));
  950. }
  951. //! <b>Requires</b>: p must point to an element contained
  952. //! by this list. i must point to an element contained in list x.
  953. //! this' allocator and x's allocator shall compare equal
  954. //!
  955. //! <b>Effects</b>: Transfers the value pointed by i, from list x to this list,
  956. //! before the element pointed by p. No destructors or copy constructors are called.
  957. //! If p == i or p == ++i, this function is a null operation.
  958. //!
  959. //! <b>Throws</b>: Nothing
  960. //!
  961. //! <b>Complexity</b>: Constant.
  962. //!
  963. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  964. //! list. Iterators of this list and all the references are not invalidated.
  965. void splice(const_iterator p, list &x, const_iterator i) BOOST_NOEXCEPT_OR_NOTHROW
  966. {
  967. BOOST_ASSERT((priv_is_linked)(p));
  968. BOOST_ASSERT(this->node_alloc() == x.node_alloc());
  969. this->icont().splice(p.get(), x.icont(), i.get());
  970. }
  971. //! <b>Requires</b>: p must point to an element contained
  972. //! by this list. i must point to an element contained in list x.
  973. //! this' allocator and x's allocator shall compare equal.
  974. //!
  975. //! <b>Effects</b>: Transfers the value pointed by i, from list x to this list,
  976. //! before the element pointed by p. No destructors or copy constructors are called.
  977. //! If p == i or p == ++i, this function is a null operation.
  978. //!
  979. //! <b>Throws</b>: Nothing
  980. //!
  981. //! <b>Complexity</b>: Constant.
  982. //!
  983. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  984. //! list. Iterators of this list and all the references are not invalidated.
  985. void splice(const_iterator p, BOOST_RV_REF(list) x, const_iterator i) BOOST_NOEXCEPT_OR_NOTHROW
  986. {
  987. BOOST_ASSERT(this != &x);
  988. //Additional checks done in splice()
  989. this->splice(p, static_cast<list&>(x), i);
  990. }
  991. //! <b>Requires</b>: p must point to an element contained
  992. //! by this list. first and last must point to elements contained in list x.
  993. //! this' allocator and x's allocator shall compare equal
  994. //!
  995. //! <b>Effects</b>: Transfers the range pointed by first and last from list x to this list,
  996. //! before the element pointed by p. No destructors or copy constructors are called.
  997. //!
  998. //! <b>Throws</b>: Nothing
  999. //!
  1000. //! <b>Complexity</b>: Linear to the number of elements transferred.
  1001. //!
  1002. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1003. //! list. Iterators of this list and all the references are not invalidated.
  1004. void splice(const_iterator p, list &x, const_iterator first, const_iterator last) BOOST_NOEXCEPT_OR_NOTHROW
  1005. {
  1006. BOOST_ASSERT((priv_is_linked)(p));
  1007. BOOST_ASSERT(first == last || (first != x.cend() && x.priv_is_linked(first)));
  1008. BOOST_ASSERT(first == last || x.priv_is_linked(last));
  1009. BOOST_ASSERT(this->node_alloc() == x.node_alloc());
  1010. this->icont().splice(p.get(), x.icont(), first.get(), last.get());
  1011. }
  1012. //! <b>Requires</b>: p must point to an element contained
  1013. //! by this list. first and last must point to elements contained in list x.
  1014. //! this' allocator and x's allocator shall compare equal.
  1015. //!
  1016. //! <b>Effects</b>: Transfers the range pointed by first and last from list x to this list,
  1017. //! before the element pointed by p. No destructors or copy constructors are called.
  1018. //!
  1019. //! <b>Throws</b>: Nothing
  1020. //!
  1021. //! <b>Complexity</b>: Linear to the number of elements transferred.
  1022. //!
  1023. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1024. //! list. Iterators of this list and all the references are not invalidated.
  1025. void splice(const_iterator p, BOOST_RV_REF(list) x, const_iterator first, const_iterator last) BOOST_NOEXCEPT_OR_NOTHROW
  1026. {
  1027. BOOST_ASSERT(this != &x);
  1028. //Additional checks done in splice()
  1029. this->splice(p, static_cast<list&>(x), first, last);
  1030. }
  1031. //! <b>Requires</b>: p must point to an element contained
  1032. //! by this list. first and last must point to elements contained in list x.
  1033. //! n == distance(first, last). this' allocator and x's allocator shall compare equal
  1034. //!
  1035. //! <b>Effects</b>: Transfers the range pointed by first and last from list x to this list,
  1036. //! before the element pointed by p. No destructors or copy constructors are called.
  1037. //!
  1038. //! <b>Throws</b>: Nothing
  1039. //!
  1040. //! <b>Complexity</b>: Constant.
  1041. //!
  1042. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1043. //! list. Iterators of this list and all the references are not invalidated.
  1044. //!
  1045. //! <b>Note</b>: Non-standard extension
  1046. void splice(const_iterator p, list &x, const_iterator first, const_iterator last, size_type n) BOOST_NOEXCEPT_OR_NOTHROW
  1047. {
  1048. BOOST_ASSERT(this->node_alloc() == x.node_alloc());
  1049. this->icont().splice(p.get(), x.icont(), first.get(), last.get(), n);
  1050. }
  1051. //! <b>Requires</b>: p must point to an element contained
  1052. //! by this list. first and last must point to elements contained in list x.
  1053. //! n == distance(first, last). this' allocator and x's allocator shall compare equal
  1054. //!
  1055. //! <b>Effects</b>: Transfers the range pointed by first and last from list x to this list,
  1056. //! before the element pointed by p. No destructors or copy constructors are called.
  1057. //!
  1058. //! <b>Throws</b>: Nothing
  1059. //!
  1060. //! <b>Complexity</b>: Constant.
  1061. //!
  1062. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1063. //! list. Iterators of this list and all the references are not invalidated.
  1064. //!
  1065. //! <b>Note</b>: Non-standard extension
  1066. void splice(const_iterator p, BOOST_RV_REF(list) x, const_iterator first, const_iterator last, size_type n) BOOST_NOEXCEPT_OR_NOTHROW
  1067. { this->splice(p, static_cast<list&>(x), first, last, n); }
  1068. //! <b>Effects</b>: Removes all the elements that compare equal to value.
  1069. //!
  1070. //! <b>Throws</b>: If comparison throws.
  1071. //!
  1072. //! <b>Complexity</b>: Linear time. It performs exactly size() comparisons for equality.
  1073. //!
  1074. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1075. //! and iterators to elements that are not removed remain valid.
  1076. void remove(const T& value)
  1077. { this->remove_if(equal_to_value_type(value)); }
  1078. //! <b>Effects</b>: Removes all the elements for which a specified
  1079. //! predicate is satisfied.
  1080. //!
  1081. //! <b>Throws</b>: If pred throws.
  1082. //!
  1083. //! <b>Complexity</b>: Linear time. It performs exactly size() calls to the predicate.
  1084. //!
  1085. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1086. //! and iterators to elements that are not removed remain valid.
  1087. template <class Pred>
  1088. void remove_if(Pred pred)
  1089. {
  1090. typedef value_to_node_compare<Node, Pred> value_to_node_compare_type;
  1091. this->icont().remove_and_dispose_if(value_to_node_compare_type(pred), Destroyer(this->node_alloc()));
  1092. }
  1093. //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
  1094. //! elements that are equal from the list.
  1095. //!
  1096. //! <b>Throws</b>: If comparison throws.
  1097. //!
  1098. //! <b>Complexity</b>: Linear time (size()-1 comparisons equality comparisons).
  1099. //!
  1100. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1101. //! and iterators to elements that are not removed remain valid.
  1102. void unique()
  1103. { this->unique(value_equal_t()); }
  1104. //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
  1105. //! elements that satisfy some binary predicate from the list.
  1106. //!
  1107. //! <b>Throws</b>: If pred throws.
  1108. //!
  1109. //! <b>Complexity</b>: Linear time (size()-1 comparisons calls to pred()).
  1110. //!
  1111. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1112. //! and iterators to elements that are not removed remain valid.
  1113. template <class BinaryPredicate>
  1114. void unique(BinaryPredicate binary_pred)
  1115. {
  1116. typedef value_to_node_compare<Node, BinaryPredicate> value_to_node_compare_type;
  1117. this->icont().unique_and_dispose(value_to_node_compare_type(binary_pred), Destroyer(this->node_alloc()));
  1118. }
  1119. //! <b>Requires</b>: The lists x and *this must be distinct.
  1120. //!
  1121. //! <b>Effects</b>: This function removes all of x's elements and inserts them
  1122. //! in order into *this according to std::less<value_type>. The merge is stable;
  1123. //! that is, if an element from *this is equivalent to one from x, then the element
  1124. //! from *this will precede the one from x.
  1125. //!
  1126. //! <b>Throws</b>: If comparison throws.
  1127. //!
  1128. //! <b>Complexity</b>: This function is linear time: it performs at most
  1129. //! size() + x.size() - 1 comparisons.
  1130. void merge(list &x)
  1131. { this->merge(x, value_less_t()); }
  1132. //! <b>Requires</b>: The lists x and *this must be distinct.
  1133. //!
  1134. //! <b>Effects</b>: This function removes all of x's elements and inserts them
  1135. //! in order into *this according to std::less<value_type>. The merge is stable;
  1136. //! that is, if an element from *this is equivalent to one from x, then the element
  1137. //! from *this will precede the one from x.
  1138. //!
  1139. //! <b>Throws</b>: If comparison throws.
  1140. //!
  1141. //! <b>Complexity</b>: This function is linear time: it performs at most
  1142. //! size() + x.size() - 1 comparisons.
  1143. void merge(BOOST_RV_REF(list) x)
  1144. { this->merge(static_cast<list&>(x)); }
  1145. //! <b>Requires</b>: p must be a comparison function that induces a strict weak
  1146. //! ordering and both *this and x must be sorted according to that ordering
  1147. //! The lists x and *this must be distinct.
  1148. //!
  1149. //! <b>Effects</b>: This function removes all of x's elements and inserts them
  1150. //! in order into *this. The merge is stable; that is, if an element from *this is
  1151. //! equivalent to one from x, then the element from *this will precede the one from x.
  1152. //!
  1153. //! <b>Throws</b>: If comp throws.
  1154. //!
  1155. //! <b>Complexity</b>: This function is linear time: it performs at most
  1156. //! size() + x.size() - 1 comparisons.
  1157. //!
  1158. //! <b>Note</b>: Iterators and references to *this are not invalidated.
  1159. template <class StrictWeakOrdering>
  1160. void merge(list &x, const StrictWeakOrdering &comp)
  1161. {
  1162. BOOST_ASSERT(this->node_alloc() == x.node_alloc());
  1163. typedef value_to_node_compare<Node, StrictWeakOrdering> value_to_node_compare_type;
  1164. this->icont().merge(x.icont(), value_to_node_compare_type(comp));
  1165. }
  1166. //! <b>Requires</b>: p must be a comparison function that induces a strict weak
  1167. //! ordering and both *this and x must be sorted according to that ordering
  1168. //! The lists x and *this must be distinct.
  1169. //!
  1170. //! <b>Effects</b>: This function removes all of x's elements and inserts them
  1171. //! in order into *this. The merge is stable; that is, if an element from *this is
  1172. //! equivalent to one from x, then the element from *this will precede the one from x.
  1173. //!
  1174. //! <b>Throws</b>: If comp throws.
  1175. //!
  1176. //! <b>Complexity</b>: This function is linear time: it performs at most
  1177. //! size() + x.size() - 1 comparisons.
  1178. //!
  1179. //! <b>Note</b>: Iterators and references to *this are not invalidated.
  1180. template <class StrictWeakOrdering>
  1181. void merge(BOOST_RV_REF(list) x, StrictWeakOrdering comp)
  1182. { this->merge(static_cast<list&>(x), comp); }
  1183. //! <b>Effects</b>: This function sorts the list *this according to std::less<value_type>.
  1184. //! The sort is stable, that is, the relative order of equivalent elements is preserved.
  1185. //!
  1186. //! <b>Throws</b>: If comparison throws.
  1187. //!
  1188. //! <b>Notes</b>: Iterators and references are not invalidated.
  1189. //!
  1190. //! <b>Complexity</b>: The number of comparisons is approximately N log N, where N
  1191. //! is the list's size.
  1192. void sort()
  1193. { this->sort(value_less_t()); }
  1194. //! <b>Effects</b>: This function sorts the list *this according to std::less<value_type>.
  1195. //! The sort is stable, that is, the relative order of equivalent elements is preserved.
  1196. //!
  1197. //! <b>Throws</b>: If comp throws.
  1198. //!
  1199. //! <b>Notes</b>: Iterators and references are not invalidated.
  1200. //!
  1201. //! <b>Complexity</b>: The number of comparisons is approximately N log N, where N
  1202. //! is the list's size.
  1203. template <class StrictWeakOrdering>
  1204. void sort(StrictWeakOrdering comp)
  1205. {
  1206. // nothing if the list has length 0 or 1.
  1207. if (this->size() < 2)
  1208. return;
  1209. typedef value_to_node_compare<Node, StrictWeakOrdering> value_to_node_compare_type;
  1210. this->icont().sort(value_to_node_compare_type(comp));
  1211. }
  1212. //! <b>Effects</b>: Reverses the order of elements in the list.
  1213. //!
  1214. //! <b>Throws</b>: Nothing.
  1215. //!
  1216. //! <b>Complexity</b>: This function is linear time.
  1217. //!
  1218. //! <b>Note</b>: Iterators and references are not invalidated
  1219. void reverse() BOOST_NOEXCEPT_OR_NOTHROW
  1220. { this->icont().reverse(); }
  1221. //! <b>Effects</b>: Returns true if x and y are equal
  1222. //!
  1223. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1224. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1225. friend bool operator==(const list& x, const list& y)
  1226. { return x.size() == y.size() && ::boost::container::algo_equal(x.begin(), x.end(), y.begin()); }
  1227. //! <b>Effects</b>: Returns true if x and y are unequal
  1228. //!
  1229. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1230. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1231. friend bool operator!=(const list& x, const list& y)
  1232. { return !(x == y); }
  1233. //! <b>Effects</b>: Returns true if x is less than y
  1234. //!
  1235. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1236. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1237. friend bool operator<(const list& x, const list& y)
  1238. { return boost::container::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); }
  1239. //! <b>Effects</b>: Returns true if x is greater than y
  1240. //!
  1241. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1242. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1243. friend bool operator>(const list& x, const list& y)
  1244. { return y < x; }
  1245. //! <b>Effects</b>: Returns true if x is equal or less than y
  1246. //!
  1247. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1248. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1249. friend bool operator<=(const list& x, const list& y)
  1250. { return !(y < x); }
  1251. //! <b>Effects</b>: Returns true if x is equal or greater than y
  1252. //!
  1253. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1254. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1255. friend bool operator>=(const list& x, const list& y)
  1256. { return !(x < y); }
  1257. //! <b>Effects</b>: x.swap(y)
  1258. //!
  1259. //! <b>Complexity</b>: Constant.
  1260. inline friend void swap(list& x, list& y)
  1261. BOOST_NOEXCEPT_IF(BOOST_NOEXCEPT(x.swap(y)))
  1262. { x.swap(y); }
  1263. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  1264. private:
  1265. //`allocator_type::value_type` must match container's `value type`. If this
  1266. //assertion fails, please review your allocator definition.
  1267. BOOST_CONTAINER_STATIC_ASSERT((dtl::is_same<value_type, typename allocator_traits_type::value_type>::value));
  1268. void priv_move_assign(BOOST_RV_REF(list) x, dtl::bool_<true> /*steal_resources*/)
  1269. {
  1270. //Destroy objects but retain memory in case x reuses it in the future
  1271. this->clear();
  1272. //Move allocator if needed
  1273. this->AllocHolder::move_assign_alloc(x);
  1274. //Obtain resources
  1275. this->icont() = boost::move(x.icont());
  1276. }
  1277. void priv_move_assign(BOOST_RV_REF(list) x, dtl::bool_<false> /*steal_resources*/)
  1278. {
  1279. //We can't guarantee a compile-time equal allocator or propagation so fallback to runtime
  1280. //Resources can be transferred if both allocators are equal
  1281. if (this->node_alloc() == x.node_alloc()) {
  1282. this->priv_move_assign(boost::move(x), dtl::true_());
  1283. }
  1284. else {
  1285. this->assign(boost::make_move_iterator(x.begin()), boost::make_move_iterator(x.end()));
  1286. }
  1287. }
  1288. static bool priv_is_linked(const_iterator const position)
  1289. {
  1290. const_iterator cur(position);
  1291. //This list is circular including end nodes
  1292. return (--(++cur)) == position && (++(--cur)) == position;
  1293. }
  1294. bool priv_try_shrink(size_type new_size)
  1295. {
  1296. const size_type len = this->size();
  1297. if(len > new_size){
  1298. const const_iterator iend = this->cend();
  1299. size_type to_erase = len - new_size;
  1300. const_iterator ifirst;
  1301. if(to_erase < len/2u){
  1302. ifirst = iend;
  1303. while(to_erase--){
  1304. --ifirst;
  1305. }
  1306. }
  1307. else{
  1308. ifirst = this->cbegin();
  1309. size_type to_skip = len - to_erase;
  1310. while(to_skip--){
  1311. ++ifirst;
  1312. }
  1313. }
  1314. this->erase(ifirst, iend);
  1315. return true;
  1316. }
  1317. else{
  1318. return false;
  1319. }
  1320. }
  1321. iterator priv_insert(const_iterator p, const T &x)
  1322. {
  1323. BOOST_ASSERT((priv_is_linked)(p));
  1324. NodePtr tmp = AllocHolder::create_node(x);
  1325. return iterator(this->icont().insert(p.get(), *tmp));
  1326. }
  1327. iterator priv_insert(const_iterator p, BOOST_RV_REF(T) x)
  1328. {
  1329. BOOST_ASSERT((priv_is_linked)(p));
  1330. NodePtr tmp = AllocHolder::create_node(boost::move(x));
  1331. return iterator(this->icont().insert(p.get(), *tmp));
  1332. }
  1333. template<class U>
  1334. void priv_push_back(BOOST_FWD_REF(U) x)
  1335. { this->icont().push_back(*this->create_node(::boost::forward<U>(x))); }
  1336. template<class U>
  1337. void priv_push_front(BOOST_FWD_REF(U) x)
  1338. { this->icont().push_front(*this->create_node(::boost::forward<U>(x))); }
  1339. class insertion_functor;
  1340. friend class insertion_functor;
  1341. class insertion_functor
  1342. {
  1343. Icont &icont_;
  1344. typedef typename Icont::const_iterator iconst_iterator;
  1345. const iconst_iterator pos_;
  1346. public:
  1347. insertion_functor(Icont &icont, typename Icont::const_iterator pos)
  1348. : icont_(icont), pos_(pos)
  1349. {}
  1350. void operator()(Node &n)
  1351. {
  1352. this->icont_.insert(pos_, n);
  1353. }
  1354. };
  1355. typedef value_less<value_type> value_less_t;
  1356. typedef value_equal<value_type> value_equal_t;
  1357. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  1358. };
  1359. //! <b>Effects</b>: Erases all elements that compare equal to v from the container c.
  1360. //!
  1361. //! <b>Complexity</b>: Linear.
  1362. template <class T, class A, class U>
  1363. inline typename list<T, A>::size_type erase(list<T, A>& c, const U& v)
  1364. {
  1365. typename list<T, A>::size_type old_size = c.size();
  1366. c.remove_if(equal_to_value<U>(v));
  1367. return old_size - c.size();
  1368. }
  1369. //! <b>Effects</b>: Erases all elements that satisfy the predicate pred from the container c.
  1370. //!
  1371. //! <b>Complexity</b>: Linear.
  1372. template <class T, class A, class Pred>
  1373. inline typename list<T, A>::size_type erase_if(list<T, A>& c, Pred pred)
  1374. {
  1375. typename list<T, A>::size_type old_size = c.size();
  1376. c.remove_if(pred);
  1377. return old_size - c.size();
  1378. }
  1379. #ifndef BOOST_CONTAINER_NO_CXX17_CTAD
  1380. template <typename InputIterator>
  1381. list(InputIterator, InputIterator) ->
  1382. list<typename iterator_traits<InputIterator>::value_type>;
  1383. template <typename InputIterator, typename ValueAllocator>
  1384. list(InputIterator, InputIterator, ValueAllocator const&) ->
  1385. list<typename iterator_traits<InputIterator>::value_type, ValueAllocator>;
  1386. #endif
  1387. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  1388. } //namespace container {
  1389. //!has_trivial_destructor_after_move<> == true_type
  1390. //!specialization for optimizations
  1391. template <class T, class Allocator>
  1392. struct has_trivial_destructor_after_move<boost::container::list<T, Allocator> >
  1393. {
  1394. typedef typename boost::container::list<T, Allocator>::allocator_type allocator_type;
  1395. typedef typename ::boost::container::allocator_traits<allocator_type>::pointer pointer;
  1396. BOOST_STATIC_CONSTEXPR bool value = ::boost::has_trivial_destructor_after_move<allocator_type>::value &&
  1397. ::boost::has_trivial_destructor_after_move<pointer>::value;
  1398. };
  1399. namespace container {
  1400. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  1401. }}
  1402. #include <boost/container/detail/config_end.hpp>
  1403. #endif // BOOST_CONTAINER_LIST_HPP