list.hpp 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  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