list.hpp 54 KB

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