list.hpp 56 KB

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