list.hpp 56 KB

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