list.hpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  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 aligned_storage<sizeof(T), 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 (&x != this){
  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. BOOST_ASSERT(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. return *this;
  375. }
  376. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  377. //! <b>Effects</b>: Makes *this contain the same elements as il.
  378. //!
  379. //! <b>Postcondition</b>: this->size() == il.size(). *this contains a copy
  380. //! of each of x's elements.
  381. //!
  382. //! <b>Throws</b>: If memory allocation throws or T's copy constructor throws.
  383. //!
  384. //! <b>Complexity</b>: Linear to the number of elements in x.
  385. list& operator=(std::initializer_list<value_type> il)
  386. {
  387. assign(il.begin(), il.end());
  388. return *this;
  389. }
  390. #endif
  391. //! <b>Effects</b>: Assigns the n copies of val to *this.
  392. //!
  393. //! <b>Throws</b>: If memory allocation throws or T's copy constructor throws.
  394. //!
  395. //! <b>Complexity</b>: Linear to n.
  396. void assign(size_type n, const T& val)
  397. {
  398. typedef constant_iterator<value_type, difference_type> cvalue_iterator;
  399. return this->assign(cvalue_iterator(val, n), cvalue_iterator());
  400. }
  401. //! <b>Effects</b>: Assigns the range [first, last) to *this.
  402. //!
  403. //! <b>Throws</b>: If memory allocation throws or
  404. //! T's constructor from dereferencing InpIt throws.
  405. //!
  406. //! <b>Complexity</b>: Linear to n.
  407. template <class InpIt>
  408. void assign(InpIt first, InpIt last
  409. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  410. , typename dtl::disable_if_convertible<InpIt, size_type>::type * = 0
  411. #endif
  412. )
  413. {
  414. iterator first1 = this->begin();
  415. const iterator last1 = this->end();
  416. for ( ; first1 != last1 && first != last; ++first1, ++first)
  417. *first1 = *first;
  418. if (first == last)
  419. this->erase(first1, last1);
  420. else{
  421. this->insert(last1, first, last);
  422. }
  423. }
  424. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  425. //! <b>Effects</b>: Assigns the range [il.begin(), il.end()) to *this.
  426. //!
  427. //! <b>Throws</b>: If memory allocation throws or
  428. //! T's constructor from dereferencing std::initializer_list iterator throws.
  429. //!
  430. //! <b>Complexity</b>: Linear to n.
  431. void assign(std::initializer_list<value_type> il)
  432. { assign(il.begin(), il.end()); }
  433. #endif
  434. //! <b>Effects</b>: Returns a copy of the internal allocator.
  435. //!
  436. //! <b>Throws</b>: If allocator's copy constructor throws.
  437. //!
  438. //! <b>Complexity</b>: Constant.
  439. allocator_type get_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
  440. { return allocator_type(this->node_alloc()); }
  441. //! <b>Effects</b>: Returns a reference to the internal allocator.
  442. //!
  443. //! <b>Throws</b>: Nothing
  444. //!
  445. //! <b>Complexity</b>: Constant.
  446. //!
  447. //! <b>Note</b>: Non-standard extension.
  448. stored_allocator_type &get_stored_allocator() BOOST_NOEXCEPT_OR_NOTHROW
  449. { return this->node_alloc(); }
  450. //! <b>Effects</b>: Returns a reference to the internal allocator.
  451. //!
  452. //! <b>Throws</b>: Nothing
  453. //!
  454. //! <b>Complexity</b>: Constant.
  455. //!
  456. //! <b>Note</b>: Non-standard extension.
  457. const stored_allocator_type &get_stored_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
  458. { return this->node_alloc(); }
  459. //////////////////////////////////////////////
  460. //
  461. // iterators
  462. //
  463. //////////////////////////////////////////////
  464. //! <b>Effects</b>: Returns an iterator to the first element contained in the list.
  465. //!
  466. //! <b>Throws</b>: Nothing.
  467. //!
  468. //! <b>Complexity</b>: Constant.
  469. iterator begin() BOOST_NOEXCEPT_OR_NOTHROW
  470. { return iterator(this->icont().begin()); }
  471. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the list.
  472. //!
  473. //! <b>Throws</b>: Nothing.
  474. //!
  475. //! <b>Complexity</b>: Constant.
  476. const_iterator begin() const BOOST_NOEXCEPT_OR_NOTHROW
  477. { return this->cbegin(); }
  478. //! <b>Effects</b>: Returns an iterator to the end of the list.
  479. //!
  480. //! <b>Throws</b>: Nothing.
  481. //!
  482. //! <b>Complexity</b>: Constant.
  483. iterator end() BOOST_NOEXCEPT_OR_NOTHROW
  484. { return iterator(this->icont().end()); }
  485. //! <b>Effects</b>: Returns a const_iterator to the end of the list.
  486. //!
  487. //! <b>Throws</b>: Nothing.
  488. //!
  489. //! <b>Complexity</b>: Constant.
  490. const_iterator end() const BOOST_NOEXCEPT_OR_NOTHROW
  491. { return this->cend(); }
  492. //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
  493. //! of the reversed list.
  494. //!
  495. //! <b>Throws</b>: Nothing.
  496. //!
  497. //! <b>Complexity</b>: Constant.
  498. reverse_iterator rbegin() BOOST_NOEXCEPT_OR_NOTHROW
  499. { return reverse_iterator(end()); }
  500. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  501. //! of the reversed list.
  502. //!
  503. //! <b>Throws</b>: Nothing.
  504. //!
  505. //! <b>Complexity</b>: Constant.
  506. const_reverse_iterator rbegin() const BOOST_NOEXCEPT_OR_NOTHROW
  507. { return this->crbegin(); }
  508. //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
  509. //! of the reversed list.
  510. //!
  511. //! <b>Throws</b>: Nothing.
  512. //!
  513. //! <b>Complexity</b>: Constant.
  514. reverse_iterator rend() BOOST_NOEXCEPT_OR_NOTHROW
  515. { return reverse_iterator(begin()); }
  516. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  517. //! of the reversed list.
  518. //!
  519. //! <b>Throws</b>: Nothing.
  520. //!
  521. //! <b>Complexity</b>: Constant.
  522. const_reverse_iterator rend() const BOOST_NOEXCEPT_OR_NOTHROW
  523. { return this->crend(); }
  524. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the list.
  525. //!
  526. //! <b>Throws</b>: Nothing.
  527. //!
  528. //! <b>Complexity</b>: Constant.
  529. const_iterator cbegin() const BOOST_NOEXCEPT_OR_NOTHROW
  530. { return const_iterator(this->non_const_icont().begin()); }
  531. //! <b>Effects</b>: Returns a const_iterator to the end of the list.
  532. //!
  533. //! <b>Throws</b>: Nothing.
  534. //!
  535. //! <b>Complexity</b>: Constant.
  536. const_iterator cend() const BOOST_NOEXCEPT_OR_NOTHROW
  537. { return const_iterator(this->non_const_icont().end()); }
  538. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  539. //! of the reversed list.
  540. //!
  541. //! <b>Throws</b>: Nothing.
  542. //!
  543. //! <b>Complexity</b>: Constant.
  544. const_reverse_iterator crbegin() const BOOST_NOEXCEPT_OR_NOTHROW
  545. { return const_reverse_iterator(this->cend()); }
  546. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  547. //! of the reversed list.
  548. //!
  549. //! <b>Throws</b>: Nothing.
  550. //!
  551. //! <b>Complexity</b>: Constant.
  552. const_reverse_iterator crend() const BOOST_NOEXCEPT_OR_NOTHROW
  553. { return const_reverse_iterator(this->cbegin()); }
  554. //////////////////////////////////////////////
  555. //
  556. // capacity
  557. //
  558. //////////////////////////////////////////////
  559. //! <b>Effects</b>: Returns true if the list contains no elements.
  560. //!
  561. //! <b>Throws</b>: Nothing.
  562. //!
  563. //! <b>Complexity</b>: Constant.
  564. bool empty() const BOOST_NOEXCEPT_OR_NOTHROW
  565. { return !this->size(); }
  566. //! <b>Effects</b>: Returns the number of the elements contained in the list.
  567. //!
  568. //! <b>Throws</b>: Nothing.
  569. //!
  570. //! <b>Complexity</b>: Constant.
  571. size_type size() const BOOST_NOEXCEPT_OR_NOTHROW
  572. { return this->icont().size(); }
  573. //! <b>Effects</b>: Returns the largest possible size of the list.
  574. //!
  575. //! <b>Throws</b>: Nothing.
  576. //!
  577. //! <b>Complexity</b>: Constant.
  578. size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
  579. { return AllocHolder::max_size(); }
  580. //! <b>Effects</b>: Inserts or erases elements at the end such that
  581. //! the size becomes n. New elements are value initialized.
  582. //!
  583. //! <b>Throws</b>: If memory allocation throws, or T's copy constructor throws.
  584. //!
  585. //! <b>Complexity</b>: Linear to the difference between size() and new_size.
  586. void resize(size_type new_size)
  587. {
  588. if(!priv_try_shrink(new_size)){
  589. typedef value_init_construct_iterator<value_type, difference_type> value_init_iterator;
  590. this->insert(this->cend(), value_init_iterator(new_size - this->size()), value_init_iterator());
  591. }
  592. }
  593. //! <b>Effects</b>: Inserts or erases elements at the end such that
  594. //! the size becomes n. New elements are copy constructed from x.
  595. //!
  596. //! <b>Throws</b>: If memory allocation throws, or T's copy constructor throws.
  597. //!
  598. //! <b>Complexity</b>: Linear to the difference between size() and new_size.
  599. void resize(size_type new_size, const T& x)
  600. {
  601. if(!priv_try_shrink(new_size)){
  602. this->insert(this->cend(), new_size - this->size(), x);
  603. }
  604. }
  605. //////////////////////////////////////////////
  606. //
  607. // element access
  608. //
  609. //////////////////////////////////////////////
  610. //! <b>Requires</b>: !empty()
  611. //!
  612. //! <b>Effects</b>: Returns a reference to the first element
  613. //! from the beginning of the container.
  614. //!
  615. //! <b>Throws</b>: Nothing.
  616. //!
  617. //! <b>Complexity</b>: Constant.
  618. reference front() BOOST_NOEXCEPT_OR_NOTHROW
  619. {
  620. BOOST_ASSERT(!this->empty());
  621. return *this->begin();
  622. }
  623. //! <b>Requires</b>: !empty()
  624. //!
  625. //! <b>Effects</b>: Returns a const reference to the first element
  626. //! from the beginning of the container.
  627. //!
  628. //! <b>Throws</b>: Nothing.
  629. //!
  630. //! <b>Complexity</b>: Constant.
  631. const_reference front() const BOOST_NOEXCEPT_OR_NOTHROW
  632. {
  633. BOOST_ASSERT(!this->empty());
  634. return *this->begin();
  635. }
  636. //! <b>Requires</b>: !empty()
  637. //!
  638. //! <b>Effects</b>: Returns a reference to the first element
  639. //! from the beginning of the container.
  640. //!
  641. //! <b>Throws</b>: Nothing.
  642. //!
  643. //! <b>Complexity</b>: Constant.
  644. reference back() BOOST_NOEXCEPT_OR_NOTHROW
  645. {
  646. BOOST_ASSERT(!this->empty());
  647. return *(--this->end());
  648. }
  649. //! <b>Requires</b>: !empty()
  650. //!
  651. //! <b>Effects</b>: Returns a const reference to the first element
  652. //! from the beginning of the container.
  653. //!
  654. //! <b>Throws</b>: Nothing.
  655. //!
  656. //! <b>Complexity</b>: Constant.
  657. const_reference back() const BOOST_NOEXCEPT_OR_NOTHROW
  658. {
  659. BOOST_ASSERT(!this->empty());
  660. return *(--this->end());
  661. }
  662. //////////////////////////////////////////////
  663. //
  664. // modifiers
  665. //
  666. //////////////////////////////////////////////
  667. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  668. //! <b>Effects</b>: Inserts an object of type T constructed with
  669. //! std::forward<Args>(args)... in the end of the list.
  670. //!
  671. //! <b>Returns</b>: A reference to the created object.
  672. //!
  673. //! <b>Throws</b>: If memory allocation throws or
  674. //! T's in-place constructor throws.
  675. //!
  676. //! <b>Complexity</b>: Constant
  677. template <class... Args>
  678. reference emplace_back(BOOST_FWD_REF(Args)... args)
  679. { return *this->emplace(this->cend(), boost::forward<Args>(args)...); }
  680. //! <b>Effects</b>: Inserts an object of type T constructed with
  681. //! std::forward<Args>(args)... in the beginning of the list.
  682. //!
  683. //! <b>Returns</b>: A reference to the created object.
  684. //!
  685. //! <b>Throws</b>: If memory allocation throws or
  686. //! T's in-place constructor throws.
  687. //!
  688. //! <b>Complexity</b>: Constant
  689. template <class... Args>
  690. reference emplace_front(BOOST_FWD_REF(Args)... args)
  691. { return *this->emplace(this->cbegin(), boost::forward<Args>(args)...); }
  692. //! <b>Effects</b>: Inserts an object of type T constructed with
  693. //! std::forward<Args>(args)... before p.
  694. //!
  695. //! <b>Throws</b>: If memory allocation throws or
  696. //! T's in-place constructor throws.
  697. //!
  698. //! <b>Complexity</b>: Constant
  699. template <class... Args>
  700. iterator emplace(const_iterator position, BOOST_FWD_REF(Args)... args)
  701. {
  702. BOOST_ASSERT((priv_is_linked)(position));
  703. NodePtr pnode(AllocHolder::create_node(boost::forward<Args>(args)...));
  704. return iterator(this->icont().insert(position.get(), *pnode));
  705. }
  706. #else // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  707. #define BOOST_CONTAINER_LIST_EMPLACE_CODE(N) \
  708. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  709. reference emplace_back(BOOST_MOVE_UREF##N)\
  710. { return *this->emplace(this->cend() BOOST_MOVE_I##N BOOST_MOVE_FWD##N); }\
  711. \
  712. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  713. reference emplace_front(BOOST_MOVE_UREF##N)\
  714. { return *this->emplace(this->cbegin() BOOST_MOVE_I##N BOOST_MOVE_FWD##N);}\
  715. \
  716. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  717. iterator emplace(const_iterator position BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  718. {\
  719. BOOST_ASSERT(position == this->cend() || (--(++position) == position) );\
  720. NodePtr pnode (AllocHolder::create_node(BOOST_MOVE_FWD##N));\
  721. return iterator(this->icont().insert(position.get(), *pnode));\
  722. }\
  723. //
  724. BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_LIST_EMPLACE_CODE)
  725. #undef BOOST_CONTAINER_LIST_EMPLACE_CODE
  726. #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  727. #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  728. //! <b>Effects</b>: Inserts a copy of x at the beginning of the list.
  729. //!
  730. //! <b>Throws</b>: If memory allocation throws or
  731. //! T's copy constructor throws.
  732. //!
  733. //! <b>Complexity</b>: Amortized constant time.
  734. void push_front(const T &x);
  735. //! <b>Effects</b>: Constructs a new element in the beginning of the list
  736. //! and moves the resources of x to this new element.
  737. //!
  738. //! <b>Throws</b>: If memory allocation throws.
  739. //!
  740. //! <b>Complexity</b>: Amortized constant time.
  741. void push_front(T &&x);
  742. #else
  743. BOOST_MOVE_CONVERSION_AWARE_CATCH(push_front, T, void, priv_push_front)
  744. #endif
  745. #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  746. //! <b>Effects</b>: Inserts a copy of x at the end of the list.
  747. //!
  748. //! <b>Throws</b>: If memory allocation throws or
  749. //! T's copy constructor throws.
  750. //!
  751. //! <b>Complexity</b>: Amortized constant time.
  752. void push_back(const T &x);
  753. //! <b>Effects</b>: Constructs a new element in the end of the list
  754. //! and moves the resources of x to this new element.
  755. //!
  756. //! <b>Throws</b>: If memory allocation throws.
  757. //!
  758. //! <b>Complexity</b>: Amortized constant time.
  759. void push_back(T &&x);
  760. #else
  761. BOOST_MOVE_CONVERSION_AWARE_CATCH(push_back, T, void, priv_push_back)
  762. #endif
  763. #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  764. //! <b>Requires</b>: p must be a valid iterator of *this.
  765. //!
  766. //! <b>Effects</b>: Insert a copy of x before p.
  767. //!
  768. //! <b>Returns</b>: an iterator to the inserted element.
  769. //!
  770. //! <b>Throws</b>: If memory allocation throws or x's copy constructor throws.
  771. //!
  772. //! <b>Complexity</b>: Amortized constant time.
  773. iterator insert(const_iterator p, const T &x);
  774. //! <b>Requires</b>: p must be a valid iterator of *this.
  775. //!
  776. //! <b>Effects</b>: Insert a new element before p with x's resources.
  777. //!
  778. //! <b>Returns</b>: an iterator to the inserted element.
  779. //!
  780. //! <b>Throws</b>: If memory allocation throws.
  781. //!
  782. //! <b>Complexity</b>: Amortized constant time.
  783. iterator insert(const_iterator p, T &&x);
  784. #else
  785. BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, T, iterator, priv_insert, const_iterator, const_iterator)
  786. #endif
  787. //! <b>Requires</b>: p must be a valid iterator of *this.
  788. //!
  789. //! <b>Effects</b>: Inserts n copies of x before p.
  790. //!
  791. //! <b>Returns</b>: an iterator to the first inserted element or p if n is 0.
  792. //!
  793. //! <b>Throws</b>: If memory allocation throws or T's copy constructor throws.
  794. //!
  795. //! <b>Complexity</b>: Linear to n.
  796. iterator insert(const_iterator position, size_type n, const T& x)
  797. {
  798. //range check is done by insert
  799. typedef constant_iterator<value_type, difference_type> cvalue_iterator;
  800. return this->insert(position, cvalue_iterator(x, n), cvalue_iterator());
  801. }
  802. //! <b>Requires</b>: p must be a valid iterator of *this.
  803. //!
  804. //! <b>Effects</b>: Insert a copy of the [first, last) range before p.
  805. //!
  806. //! <b>Returns</b>: an iterator to the first inserted element or p if first == last.
  807. //!
  808. //! <b>Throws</b>: If memory allocation throws, T's constructor from a
  809. //! dereferenced InpIt throws.
  810. //!
  811. //! <b>Complexity</b>: Linear to distance [first, last).
  812. template <class InpIt>
  813. iterator insert(const_iterator p, InpIt first, InpIt last
  814. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  815. , typename dtl::enable_if_c
  816. < !dtl::is_convertible<InpIt, size_type>::value
  817. && (dtl::is_input_iterator<InpIt>::value
  818. || dtl::is_same<alloc_version, version_1>::value
  819. )
  820. >::type * = 0
  821. #endif
  822. )
  823. {
  824. BOOST_ASSERT((priv_is_linked)(p));
  825. const typename Icont::iterator ipos(p.get());
  826. iterator ret_it(ipos);
  827. if(first != last){
  828. ret_it = iterator(this->icont().insert(ipos, *this->create_node_from_it(first)));
  829. ++first;
  830. }
  831. for (; first != last; ++first){
  832. this->icont().insert(ipos, *this->create_node_from_it(first));
  833. }
  834. return ret_it;
  835. }
  836. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  837. template <class FwdIt>
  838. iterator insert(const_iterator position, FwdIt first, FwdIt last
  839. , typename dtl::enable_if_c
  840. < !dtl::is_convertible<FwdIt, size_type>::value
  841. && !(dtl::is_input_iterator<FwdIt>::value
  842. || dtl::is_same<alloc_version, version_1>::value
  843. )
  844. >::type * = 0
  845. )
  846. {
  847. BOOST_ASSERT((priv_is_linked)(position));
  848. //Optimized allocation and construction
  849. insertion_functor func(this->icont(), position.get());
  850. iterator before_p(position.get());
  851. --before_p;
  852. this->allocate_many_and_construct(first, boost::container::iterator_distance(first, last), func);
  853. return ++before_p;
  854. }
  855. #endif
  856. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  857. //! <b>Requires</b>: p must be a valid iterator of *this.
  858. //!
  859. //! <b>Effects</b>: Insert a copy of the [il.begin(), il.end()) range before p.
  860. //!
  861. //! <b>Returns</b>: an iterator to the first inserted element or p if if.begin() == il.end().
  862. //!
  863. //! <b>Throws</b>: If memory allocation throws, T's constructor from a
  864. //! dereferenced std::initializer_list iterator throws.
  865. //!
  866. //! <b>Complexity</b>: Linear to distance [il.begin(), il.end()).
  867. iterator insert(const_iterator p, std::initializer_list<value_type> il)
  868. {
  869. //position range check is done by insert()
  870. return insert(p, il.begin(), il.end());
  871. }
  872. #endif
  873. //! <b>Effects</b>: Removes the first element from the list.
  874. //!
  875. //! <b>Throws</b>: Nothing.
  876. //!
  877. //! <b>Complexity</b>: Amortized constant time.
  878. void pop_front() BOOST_NOEXCEPT_OR_NOTHROW
  879. {
  880. BOOST_ASSERT(!this->empty());
  881. this->erase(this->cbegin());
  882. }
  883. //! <b>Effects</b>: Removes the last element from the list.
  884. //!
  885. //! <b>Throws</b>: Nothing.
  886. //!
  887. //! <b>Complexity</b>: Amortized constant time.
  888. void pop_back() BOOST_NOEXCEPT_OR_NOTHROW
  889. {
  890. BOOST_ASSERT(!this->empty());
  891. const_iterator tmp = this->cend();
  892. this->erase(--tmp);
  893. }
  894. //! <b>Requires</b>: p must be a valid iterator of *this.
  895. //!
  896. //! <b>Effects</b>: Erases the element at p.
  897. //!
  898. //! <b>Throws</b>: Nothing.
  899. //!
  900. //! <b>Complexity</b>: Amortized constant time.
  901. iterator erase(const_iterator p) BOOST_NOEXCEPT_OR_NOTHROW
  902. {
  903. BOOST_ASSERT(p != this->cend() && (priv_is_linked)(p));
  904. return iterator(this->icont().erase_and_dispose(p.get(), Destroyer(this->node_alloc())));
  905. }
  906. //! <b>Requires</b>: first and last must be valid iterator to elements in *this.
  907. //!
  908. //! <b>Effects</b>: Erases the elements pointed by [first, last).
  909. //!
  910. //! <b>Throws</b>: Nothing.
  911. //!
  912. //! <b>Complexity</b>: Linear to the distance between first and last.
  913. iterator erase(const_iterator first, const_iterator last) BOOST_NOEXCEPT_OR_NOTHROW
  914. {
  915. BOOST_ASSERT(first == last || (first != this->cend() && (priv_is_linked)(first)));
  916. BOOST_ASSERT(first == last || (priv_is_linked)(last));
  917. return iterator(AllocHolder::erase_range(first.get(), last.get(), alloc_version()));
  918. }
  919. //! <b>Effects</b>: Swaps the contents of *this and x.
  920. //!
  921. //! <b>Throws</b>: Nothing.
  922. //!
  923. //! <b>Complexity</b>: Constant.
  924. void swap(list& x)
  925. BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_swap::value
  926. || allocator_traits_type::is_always_equal::value)
  927. {
  928. BOOST_ASSERT(allocator_traits_type::propagate_on_container_swap::value ||
  929. allocator_traits_type::is_always_equal::value ||
  930. this->get_stored_allocator() == x.get_stored_allocator());
  931. AllocHolder::swap(x);
  932. }
  933. //! <b>Effects</b>: Erases all the elements of the list.
  934. //!
  935. //! <b>Throws</b>: Nothing.
  936. //!
  937. //! <b>Complexity</b>: Linear to the number of elements in the list.
  938. void clear() BOOST_NOEXCEPT_OR_NOTHROW
  939. { AllocHolder::clear(alloc_version()); }
  940. //////////////////////////////////////////////
  941. //
  942. // slist operations
  943. //
  944. //////////////////////////////////////////////
  945. //! <b>Requires</b>: p must point to an element contained
  946. //! by the list. x != *this. this' allocator and x's allocator shall compare equal
  947. //!
  948. //! <b>Effects</b>: Transfers all the elements of list x to this list, before the
  949. //! the element pointed by p. No destructors or copy constructors are called.
  950. //!
  951. //! <b>Throws</b>: Nothing
  952. //!
  953. //! <b>Complexity</b>: Constant.
  954. //!
  955. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of
  956. //! this list. Iterators of this list and all the references are not invalidated.
  957. void splice(const_iterator p, list& x) BOOST_NOEXCEPT_OR_NOTHROW
  958. {
  959. BOOST_ASSERT((priv_is_linked)(p));
  960. BOOST_ASSERT(this != &x);
  961. BOOST_ASSERT(this->node_alloc() == x.node_alloc());
  962. this->icont().splice(p.get(), x.icont());
  963. }
  964. //! <b>Requires</b>: p must point to an element contained
  965. //! by the list. x != *this. this' allocator and x's allocator shall compare equal
  966. //!
  967. //! <b>Effects</b>: Transfers all the elements of list x to this list, before the
  968. //! the element pointed by p. No destructors or copy constructors are called.
  969. //!
  970. //! <b>Throws</b>: Nothing
  971. //!
  972. //! <b>Complexity</b>: Constant.
  973. //!
  974. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of
  975. //! this list. Iterators of this list and all the references are not invalidated.
  976. void splice(const_iterator p, BOOST_RV_REF(list) x) BOOST_NOEXCEPT_OR_NOTHROW
  977. {
  978. //Checks done in splice
  979. this->splice(p, static_cast<list&>(x));
  980. }
  981. //! <b>Requires</b>: p must point to an element contained
  982. //! by this list. i must point to an element contained in list x.
  983. //! this' allocator and x's allocator shall compare equal
  984. //!
  985. //! <b>Effects</b>: Transfers the value pointed by i, from list x to this list,
  986. //! before the element pointed by p. No destructors or copy constructors are called.
  987. //! If p == i or p == ++i, this function is a null operation.
  988. //!
  989. //! <b>Throws</b>: Nothing
  990. //!
  991. //! <b>Complexity</b>: Constant.
  992. //!
  993. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  994. //! list. Iterators of this list and all the references are not invalidated.
  995. void splice(const_iterator p, list &x, const_iterator i) BOOST_NOEXCEPT_OR_NOTHROW
  996. {
  997. BOOST_ASSERT((priv_is_linked)(p));
  998. BOOST_ASSERT(this->node_alloc() == x.node_alloc());
  999. this->icont().splice(p.get(), x.icont(), i.get());
  1000. }
  1001. //! <b>Requires</b>: p must point to an element contained
  1002. //! by this list. i must point to an element contained in list x.
  1003. //! this' allocator and x's allocator shall compare equal.
  1004. //!
  1005. //! <b>Effects</b>: Transfers the value pointed by i, from list x to this list,
  1006. //! before the element pointed by p. No destructors or copy constructors are called.
  1007. //! If p == i or p == ++i, this function is a null operation.
  1008. //!
  1009. //! <b>Throws</b>: Nothing
  1010. //!
  1011. //! <b>Complexity</b>: Constant.
  1012. //!
  1013. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1014. //! list. Iterators of this list and all the references are not invalidated.
  1015. void splice(const_iterator p, BOOST_RV_REF(list) x, const_iterator i) BOOST_NOEXCEPT_OR_NOTHROW
  1016. {
  1017. BOOST_ASSERT(this != &x);
  1018. //Additional checks done in splice()
  1019. this->splice(p, static_cast<list&>(x), i);
  1020. }
  1021. //! <b>Requires</b>: p must point to an element contained
  1022. //! by this list. first and last must point to elements contained in list x.
  1023. //! this' allocator and x's allocator shall compare equal
  1024. //!
  1025. //! <b>Effects</b>: Transfers the range pointed by first and last from list x to this list,
  1026. //! before the element pointed by p. No destructors or copy constructors are called.
  1027. //!
  1028. //! <b>Throws</b>: Nothing
  1029. //!
  1030. //! <b>Complexity</b>: Linear to the number of elements transferred.
  1031. //!
  1032. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1033. //! list. Iterators of this list and all the references are not invalidated.
  1034. void splice(const_iterator p, list &x, const_iterator first, const_iterator last) BOOST_NOEXCEPT_OR_NOTHROW
  1035. {
  1036. BOOST_ASSERT((priv_is_linked)(p));
  1037. BOOST_ASSERT(first == last || (first != x.cend() && x.priv_is_linked(first)));
  1038. BOOST_ASSERT(first == last || x.priv_is_linked(last));
  1039. BOOST_ASSERT(this->node_alloc() == x.node_alloc());
  1040. this->icont().splice(p.get(), x.icont(), first.get(), last.get());
  1041. }
  1042. //! <b>Requires</b>: p must point to an element contained
  1043. //! by this list. first and last must point to elements contained in list x.
  1044. //! this' allocator and x's allocator shall compare equal.
  1045. //!
  1046. //! <b>Effects</b>: Transfers the range pointed by first and last from list x to this list,
  1047. //! before the element pointed by p. No destructors or copy constructors are called.
  1048. //!
  1049. //! <b>Throws</b>: Nothing
  1050. //!
  1051. //! <b>Complexity</b>: Linear to the number of elements transferred.
  1052. //!
  1053. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1054. //! list. Iterators of this list and all the references are not invalidated.
  1055. void splice(const_iterator p, BOOST_RV_REF(list) x, const_iterator first, const_iterator last) BOOST_NOEXCEPT_OR_NOTHROW
  1056. {
  1057. BOOST_ASSERT(this != &x);
  1058. //Additional checks done in splice()
  1059. this->splice(p, static_cast<list&>(x), first, last);
  1060. }
  1061. //! <b>Requires</b>: p must point to an element contained
  1062. //! by this list. first and last must point to elements contained in list x.
  1063. //! n == distance(first, last). this' allocator and x's allocator shall compare equal
  1064. //!
  1065. //! <b>Effects</b>: Transfers the range pointed by first and last from list x to this list,
  1066. //! before the element pointed by p. No destructors or copy constructors are called.
  1067. //!
  1068. //! <b>Throws</b>: Nothing
  1069. //!
  1070. //! <b>Complexity</b>: Constant.
  1071. //!
  1072. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1073. //! list. Iterators of this list and all the references are not invalidated.
  1074. //!
  1075. //! <b>Note</b>: Non-standard extension
  1076. void splice(const_iterator p, list &x, const_iterator first, const_iterator last, size_type n) BOOST_NOEXCEPT_OR_NOTHROW
  1077. {
  1078. BOOST_ASSERT(this->node_alloc() == x.node_alloc());
  1079. this->icont().splice(p.get(), x.icont(), first.get(), last.get(), n);
  1080. }
  1081. //! <b>Requires</b>: p must point to an element contained
  1082. //! by this list. first and last must point to elements contained in list x.
  1083. //! n == distance(first, last). this' allocator and x's allocator shall compare equal
  1084. //!
  1085. //! <b>Effects</b>: Transfers the range pointed by first and last from list x to this list,
  1086. //! before the element pointed by p. No destructors or copy constructors are called.
  1087. //!
  1088. //! <b>Throws</b>: Nothing
  1089. //!
  1090. //! <b>Complexity</b>: Constant.
  1091. //!
  1092. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1093. //! list. Iterators of this list and all the references are not invalidated.
  1094. //!
  1095. //! <b>Note</b>: Non-standard extension
  1096. void splice(const_iterator p, BOOST_RV_REF(list) x, const_iterator first, const_iterator last, size_type n) BOOST_NOEXCEPT_OR_NOTHROW
  1097. { this->splice(p, static_cast<list&>(x), first, last, n); }
  1098. //! <b>Effects</b>: Removes all the elements that compare equal to value.
  1099. //!
  1100. //! <b>Throws</b>: If comparison throws.
  1101. //!
  1102. //! <b>Complexity</b>: Linear time. It performs exactly size() comparisons for equality.
  1103. //!
  1104. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1105. //! and iterators to elements that are not removed remain valid.
  1106. void remove(const T& value)
  1107. { this->remove_if(equal_to_value_type(value)); }
  1108. //! <b>Effects</b>: Removes all the elements for which a specified
  1109. //! predicate is satisfied.
  1110. //!
  1111. //! <b>Throws</b>: If pred throws.
  1112. //!
  1113. //! <b>Complexity</b>: Linear time. It performs exactly size() calls to the predicate.
  1114. //!
  1115. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1116. //! and iterators to elements that are not removed remain valid.
  1117. template <class Pred>
  1118. void remove_if(Pred pred)
  1119. {
  1120. typedef value_to_node_compare<Node, Pred> value_to_node_compare_type;
  1121. this->icont().remove_and_dispose_if(value_to_node_compare_type(pred), Destroyer(this->node_alloc()));
  1122. }
  1123. //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
  1124. //! elements that are equal from the list.
  1125. //!
  1126. //! <b>Throws</b>: If comparison throws.
  1127. //!
  1128. //! <b>Complexity</b>: Linear time (size()-1 comparisons equality comparisons).
  1129. //!
  1130. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1131. //! and iterators to elements that are not removed remain valid.
  1132. void unique()
  1133. { this->unique(value_equal_t()); }
  1134. //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
  1135. //! elements that satisfy some binary predicate from the list.
  1136. //!
  1137. //! <b>Throws</b>: If pred throws.
  1138. //!
  1139. //! <b>Complexity</b>: Linear time (size()-1 comparisons calls to pred()).
  1140. //!
  1141. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1142. //! and iterators to elements that are not removed remain valid.
  1143. template <class BinaryPredicate>
  1144. void unique(BinaryPredicate binary_pred)
  1145. {
  1146. typedef value_to_node_compare<Node, BinaryPredicate> value_to_node_compare_type;
  1147. this->icont().unique_and_dispose(value_to_node_compare_type(binary_pred), Destroyer(this->node_alloc()));
  1148. }
  1149. //! <b>Requires</b>: The lists x and *this must be distinct.
  1150. //!
  1151. //! <b>Effects</b>: This function removes all of x's elements and inserts them
  1152. //! in order into *this according to std::less<value_type>. The merge is stable;
  1153. //! that is, if an element from *this is equivalent to one from x, then the element
  1154. //! from *this will precede the one from x.
  1155. //!
  1156. //! <b>Throws</b>: If comparison throws.
  1157. //!
  1158. //! <b>Complexity</b>: This function is linear time: it performs at most
  1159. //! size() + x.size() - 1 comparisons.
  1160. void merge(list &x)
  1161. { this->merge(x, value_less_t()); }
  1162. //! <b>Requires</b>: The lists x and *this must be distinct.
  1163. //!
  1164. //! <b>Effects</b>: This function removes all of x's elements and inserts them
  1165. //! in order into *this according to std::less<value_type>. The merge is stable;
  1166. //! that is, if an element from *this is equivalent to one from x, then the element
  1167. //! from *this will precede the one from x.
  1168. //!
  1169. //! <b>Throws</b>: If comparison throws.
  1170. //!
  1171. //! <b>Complexity</b>: This function is linear time: it performs at most
  1172. //! size() + x.size() - 1 comparisons.
  1173. void merge(BOOST_RV_REF(list) x)
  1174. { this->merge(static_cast<list&>(x)); }
  1175. //! <b>Requires</b>: p must be a comparison function that induces a strict weak
  1176. //! ordering and both *this and x must be sorted according to that ordering
  1177. //! The lists x and *this must be distinct.
  1178. //!
  1179. //! <b>Effects</b>: This function removes all of x's elements and inserts them
  1180. //! in order into *this. The merge is stable; that is, if an element from *this is
  1181. //! equivalent to one from x, then the element from *this will precede the one from x.
  1182. //!
  1183. //! <b>Throws</b>: If comp throws.
  1184. //!
  1185. //! <b>Complexity</b>: This function is linear time: it performs at most
  1186. //! size() + x.size() - 1 comparisons.
  1187. //!
  1188. //! <b>Note</b>: Iterators and references to *this are not invalidated.
  1189. template <class StrictWeakOrdering>
  1190. void merge(list &x, const StrictWeakOrdering &comp)
  1191. {
  1192. BOOST_ASSERT(this->node_alloc() == x.node_alloc());
  1193. typedef value_to_node_compare<Node, StrictWeakOrdering> value_to_node_compare_type;
  1194. this->icont().merge(x.icont(), value_to_node_compare_type(comp));
  1195. }
  1196. //! <b>Requires</b>: p must be a comparison function that induces a strict weak
  1197. //! ordering and both *this and x must be sorted according to that ordering
  1198. //! The lists x and *this must be distinct.
  1199. //!
  1200. //! <b>Effects</b>: This function removes all of x's elements and inserts them
  1201. //! in order into *this. The merge is stable; that is, if an element from *this is
  1202. //! equivalent to one from x, then the element from *this will precede the one from x.
  1203. //!
  1204. //! <b>Throws</b>: If comp throws.
  1205. //!
  1206. //! <b>Complexity</b>: This function is linear time: it performs at most
  1207. //! size() + x.size() - 1 comparisons.
  1208. //!
  1209. //! <b>Note</b>: Iterators and references to *this are not invalidated.
  1210. template <class StrictWeakOrdering>
  1211. void merge(BOOST_RV_REF(list) x, StrictWeakOrdering comp)
  1212. { this->merge(static_cast<list&>(x), comp); }
  1213. //! <b>Effects</b>: This function sorts the list *this according to std::less<value_type>.
  1214. //! The sort is stable, that is, the relative order of equivalent elements is preserved.
  1215. //!
  1216. //! <b>Throws</b>: If comparison throws.
  1217. //!
  1218. //! <b>Notes</b>: Iterators and references are not invalidated.
  1219. //!
  1220. //! <b>Complexity</b>: The number of comparisons is approximately N log N, where N
  1221. //! is the list's size.
  1222. void sort()
  1223. { this->sort(value_less_t()); }
  1224. //! <b>Effects</b>: This function sorts the list *this according to std::less<value_type>.
  1225. //! The sort is stable, that is, the relative order of equivalent elements is preserved.
  1226. //!
  1227. //! <b>Throws</b>: If comp throws.
  1228. //!
  1229. //! <b>Notes</b>: Iterators and references are not invalidated.
  1230. //!
  1231. //! <b>Complexity</b>: The number of comparisons is approximately N log N, where N
  1232. //! is the list's size.
  1233. template <class StrictWeakOrdering>
  1234. void sort(StrictWeakOrdering comp)
  1235. {
  1236. // nothing if the list has length 0 or 1.
  1237. if (this->size() < 2)
  1238. return;
  1239. typedef value_to_node_compare<Node, StrictWeakOrdering> value_to_node_compare_type;
  1240. this->icont().sort(value_to_node_compare_type(comp));
  1241. }
  1242. //! <b>Effects</b>: Reverses the order of elements in the list.
  1243. //!
  1244. //! <b>Throws</b>: Nothing.
  1245. //!
  1246. //! <b>Complexity</b>: This function is linear time.
  1247. //!
  1248. //! <b>Note</b>: Iterators and references are not invalidated
  1249. void reverse() BOOST_NOEXCEPT_OR_NOTHROW
  1250. { this->icont().reverse(); }
  1251. //! <b>Effects</b>: Returns true if x and y are equal
  1252. //!
  1253. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1254. friend bool operator==(const list& x, const list& y)
  1255. { return x.size() == y.size() && ::boost::container::algo_equal(x.begin(), x.end(), y.begin()); }
  1256. //! <b>Effects</b>: Returns true if x and y are unequal
  1257. //!
  1258. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1259. friend bool operator!=(const list& x, const list& y)
  1260. { return !(x == y); }
  1261. //! <b>Effects</b>: Returns true if x is less than y
  1262. //!
  1263. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1264. friend bool operator<(const list& x, const list& y)
  1265. { return boost::container::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); }
  1266. //! <b>Effects</b>: Returns true if x is greater than y
  1267. //!
  1268. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1269. friend bool operator>(const list& x, const list& y)
  1270. { return y < x; }
  1271. //! <b>Effects</b>: Returns true if x is equal or less than y
  1272. //!
  1273. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1274. friend bool operator<=(const list& x, const list& y)
  1275. { return !(y < x); }
  1276. //! <b>Effects</b>: Returns true if x is equal or greater than y
  1277. //!
  1278. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1279. friend bool operator>=(const list& x, const list& y)
  1280. { return !(x < y); }
  1281. //! <b>Effects</b>: x.swap(y)
  1282. //!
  1283. //! <b>Complexity</b>: Constant.
  1284. friend void swap(list& x, list& y)
  1285. { x.swap(y); }
  1286. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  1287. private:
  1288. static bool priv_is_linked(const_iterator const position)
  1289. {
  1290. const_iterator cur(position);
  1291. //This list is circular including end nodes
  1292. return (--(++cur)) == position && (++(--cur)) == position;
  1293. }
  1294. bool priv_try_shrink(size_type new_size)
  1295. {
  1296. const size_type len = this->size();
  1297. if(len > new_size){
  1298. const const_iterator iend = this->cend();
  1299. size_type to_erase = len - new_size;
  1300. const_iterator ifirst;
  1301. if(to_erase < len/2u){
  1302. ifirst = iend;
  1303. while(to_erase--){
  1304. --ifirst;
  1305. }
  1306. }
  1307. else{
  1308. ifirst = this->cbegin();
  1309. size_type to_skip = len - to_erase;
  1310. while(to_skip--){
  1311. ++ifirst;
  1312. }
  1313. }
  1314. this->erase(ifirst, iend);
  1315. return true;
  1316. }
  1317. else{
  1318. return false;
  1319. }
  1320. }
  1321. iterator priv_insert(const_iterator p, const T &x)
  1322. {
  1323. BOOST_ASSERT((priv_is_linked)(p));
  1324. NodePtr tmp = AllocHolder::create_node(x);
  1325. return iterator(this->icont().insert(p.get(), *tmp));
  1326. }
  1327. iterator priv_insert(const_iterator p, BOOST_RV_REF(T) x)
  1328. {
  1329. BOOST_ASSERT((priv_is_linked)(p));
  1330. NodePtr tmp = AllocHolder::create_node(boost::move(x));
  1331. return iterator(this->icont().insert(p.get(), *tmp));
  1332. }
  1333. void priv_push_back (const T &x)
  1334. { this->insert(this->cend(), x); }
  1335. void priv_push_back (BOOST_RV_REF(T) x)
  1336. { this->insert(this->cend(), boost::move(x)); }
  1337. void priv_push_front (const T &x)
  1338. { this->insert(this->cbegin(), x); }
  1339. void priv_push_front (BOOST_RV_REF(T) x)
  1340. { this->insert(this->cbegin(), boost::move(x)); }
  1341. class insertion_functor;
  1342. friend class insertion_functor;
  1343. class insertion_functor
  1344. {
  1345. Icont &icont_;
  1346. typedef typename Icont::const_iterator iconst_iterator;
  1347. const iconst_iterator pos_;
  1348. public:
  1349. insertion_functor(Icont &icont, typename Icont::const_iterator pos)
  1350. : icont_(icont), pos_(pos)
  1351. {}
  1352. void operator()(Node &n)
  1353. {
  1354. this->icont_.insert(pos_, n);
  1355. }
  1356. };
  1357. typedef value_less<value_type> value_less_t;
  1358. typedef value_equal<value_type> value_equal_t;
  1359. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  1360. };
  1361. #ifndef BOOST_CONTAINER_NO_CXX17_CTAD
  1362. template <typename InputIterator>
  1363. list(InputIterator, InputIterator) ->
  1364. list<typename iterator_traits<InputIterator>::value_type>;
  1365. template <typename InputIterator, typename ValueAllocator>
  1366. list(InputIterator, InputIterator, ValueAllocator const&) ->
  1367. list<typename iterator_traits<InputIterator>::value_type, ValueAllocator>;
  1368. #endif
  1369. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  1370. } //namespace container {
  1371. //!has_trivial_destructor_after_move<> == true_type
  1372. //!specialization for optimizations
  1373. template <class T, class Allocator>
  1374. struct has_trivial_destructor_after_move<boost::container::list<T, Allocator> >
  1375. {
  1376. typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
  1377. static const bool value = ::boost::has_trivial_destructor_after_move<Allocator>::value &&
  1378. ::boost::has_trivial_destructor_after_move<pointer>::value;
  1379. };
  1380. namespace container {
  1381. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  1382. }}
  1383. #include <boost/container/detail/config_end.hpp>
  1384. #endif // BOOST_CONTAINER_LIST_HPP