list.hpp 57 KB

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