slist.hpp 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2004-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_SLIST_HPP
  11. #define BOOST_CONTAINER_SLIST_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> //algo_equal(), algo_lexicographical_compare
  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/type_traits.hpp>
  32. #include <boost/container/detail/value_functors.hpp>
  33. // intrusive
  34. #include <boost/intrusive/pointer_traits.hpp>
  35. #include <boost/intrusive/slist.hpp>
  36. // move
  37. #include <boost/move/iterator.hpp>
  38. #include <boost/move/traits.hpp>
  39. #include <boost/move/utility_core.hpp>
  40. // move/detail
  41. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  42. #include <boost/move/detail/fwd_macros.hpp>
  43. #endif
  44. #include <boost/move/detail/move_helpers.hpp>
  45. // std
  46. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  47. #include <initializer_list>
  48. #endif
  49. namespace boost {
  50. namespace container {
  51. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  52. template <class T, class Allocator>
  53. class slist;
  54. namespace dtl {
  55. template<class VoidPointer>
  56. struct slist_hook
  57. {
  58. typedef typename dtl::bi::make_slist_base_hook
  59. <dtl::bi::void_pointer<VoidPointer>, dtl::bi::link_mode<dtl::bi::normal_link> >::type type;
  60. };
  61. template <class T, class VoidPointer>
  62. struct iiterator_node_value_type< base_node<T, slist_hook<VoidPointer> > >
  63. {
  64. typedef T type;
  65. };
  66. template<class Allocator>
  67. struct intrusive_slist_type
  68. {
  69. typedef boost::container::allocator_traits<Allocator> allocator_traits_type;
  70. typedef typename allocator_traits_type::value_type value_type;
  71. typedef typename boost::intrusive::pointer_traits
  72. <typename allocator_traits_type::pointer>::template
  73. rebind_pointer<void>::type
  74. void_pointer;
  75. typedef base_node<value_type, slist_hook<void_pointer> > node_type;
  76. typedef typename dtl::bi::make_slist
  77. <node_type
  78. ,dtl::bi::base_hook<typename slist_hook<void_pointer>::type>
  79. ,dtl::bi::constant_time_size<true>
  80. , dtl::bi::size_type
  81. <typename allocator_traits_type::size_type>
  82. >::type container_type;
  83. typedef container_type type;
  84. };
  85. } //namespace dtl {
  86. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  87. //! An slist is a singly linked list: a list where each element is linked to the next
  88. //! element, but not to the previous element. That is, it is a Sequence that
  89. //! supports forward but not backward traversal, and (amortized) constant time
  90. //! insertion and removal of elements. Slists, like lists, have the important
  91. //! property that insertion and splicing do not invalidate iterators to list elements,
  92. //! and that even removal invalidates only the iterators that point to the elements
  93. //! that are removed. The ordering of iterators may be changed (that is,
  94. //! slist<T>::iterator might have a different predecessor or successor after a list
  95. //! operation than it did before), but the iterators themselves will not be invalidated
  96. //! or made to point to different elements unless that invalidation or mutation is explicit.
  97. //!
  98. //! The main difference between slist and list is that list's iterators are bidirectional
  99. //! iterators, while slist's iterators are forward iterators. This means that slist is
  100. //! less versatile than list; frequently, however, bidirectional iterators are
  101. //! unnecessary. You should usually use slist unless you actually need the extra
  102. //! functionality of list, because singly linked lists are smaller and faster than double
  103. //! linked lists.
  104. //!
  105. //! Important performance note: like every other Sequence, slist defines the member
  106. //! functions insert and erase. Using these member functions carelessly, however, can
  107. //! result in disastrously slow programs. The problem is that insert's first argument is
  108. //! an iterator p, and that it inserts the new element(s) before p. This means that
  109. //! insert must find the iterator just before p; this is a constant-time operation
  110. //! for list, since list has bidirectional iterators, but for slist it must find that
  111. //! iterator by traversing the list from the beginning up to p. In other words:
  112. //! insert and erase are slow operations anywhere but near the beginning of the slist.
  113. //!
  114. //! Slist provides the member functions insert_after and erase_after, which are constant
  115. //! time operations: you should always use insert_after and erase_after whenever
  116. //! possible. If you find that insert_after and erase_after aren't adequate for your
  117. //! needs, and that you often need to use insert and erase in the middle of the list,
  118. //! then you should probably use list instead of slist.
  119. //!
  120. //! \tparam T The type of object that is stored in the list
  121. //! \tparam Allocator The allocator used for all internal memory management, use void
  122. //! for the default allocator
  123. #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
  124. template <class T, class Allocator = void >
  125. #else
  126. template <class T, class Allocator>
  127. #endif
  128. class slist
  129. : protected dtl::node_alloc_holder
  130. < typename real_allocator<T, Allocator>::type
  131. , typename dtl::intrusive_slist_type<typename real_allocator<T, Allocator>::type>::type>
  132. {
  133. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  134. typedef typename real_allocator<T, Allocator>::type ValueAllocator;
  135. typedef typename
  136. dtl::intrusive_slist_type<ValueAllocator>::type Icont;
  137. typedef dtl::node_alloc_holder<ValueAllocator, Icont> AllocHolder;
  138. typedef typename AllocHolder::NodePtr NodePtr;
  139. typedef typename AllocHolder::NodeAlloc NodeAlloc;
  140. typedef typename AllocHolder::ValAlloc ValAlloc;
  141. typedef typename AllocHolder::Node Node;
  142. typedef dtl::allocator_node_destroyer<NodeAlloc> Destroyer;
  143. typedef typename AllocHolder::alloc_version alloc_version;
  144. typedef boost::container::
  145. allocator_traits<ValueAllocator> allocator_traits_type;
  146. typedef boost::container::equal_to_value
  147. <typename allocator_traits_type::value_type> equal_to_value_type;
  148. BOOST_COPYABLE_AND_MOVABLE(slist)
  149. typedef dtl::iterator_from_iiterator<typename Icont::iterator, false> iterator_impl;
  150. typedef dtl::iterator_from_iiterator<typename Icont::iterator, true > const_iterator_impl;
  151. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  152. public:
  153. //////////////////////////////////////////////
  154. //
  155. // types
  156. //
  157. //////////////////////////////////////////////
  158. typedef T value_type;
  159. typedef typename ::boost::container::allocator_traits<ValueAllocator>::pointer pointer;
  160. typedef typename ::boost::container::allocator_traits<ValueAllocator>::const_pointer const_pointer;
  161. typedef typename ::boost::container::allocator_traits<ValueAllocator>::reference reference;
  162. typedef typename ::boost::container::allocator_traits<ValueAllocator>::const_reference const_reference;
  163. typedef typename ::boost::container::allocator_traits<ValueAllocator>::size_type size_type;
  164. typedef typename ::boost::container::allocator_traits<ValueAllocator>::difference_type difference_type;
  165. typedef ValueAllocator allocator_type;
  166. typedef BOOST_CONTAINER_IMPDEF(NodeAlloc) stored_allocator_type;
  167. typedef BOOST_CONTAINER_IMPDEF(iterator_impl) iterator;
  168. typedef BOOST_CONTAINER_IMPDEF(const_iterator_impl) const_iterator;
  169. public:
  170. //////////////////////////////////////////////
  171. //
  172. // constructFr/copy/destroy
  173. //
  174. //////////////////////////////////////////////
  175. //! <b>Effects</b>: Constructs a list taking the allocator as parameter.
  176. //!
  177. //! <b>Throws</b>: If allocator_type's copy constructor throws.
  178. //!
  179. //! <b>Complexity</b>: Constant.
  180. slist() BOOST_NOEXCEPT_IF(dtl::is_nothrow_default_constructible<ValueAllocator>::value)
  181. : AllocHolder()
  182. {}
  183. //! <b>Effects</b>: Constructs a list taking the allocator as parameter.
  184. //!
  185. //! <b>Throws</b>: Nothing
  186. //!
  187. //! <b>Complexity</b>: Constant.
  188. explicit slist(const allocator_type& a) BOOST_NOEXCEPT_OR_NOTHROW
  189. : AllocHolder(a)
  190. {}
  191. //! <b>Effects</b>: Constructs a list
  192. //! and inserts n value-initialized value_types.
  193. //!
  194. //! <b>Throws</b>: If allocator_type's default constructor
  195. //! throws or T's default or copy constructor throws.
  196. //!
  197. //! <b>Complexity</b>: Linear to n.
  198. explicit slist(size_type n)
  199. : AllocHolder(allocator_type())
  200. { this->resize(n); }
  201. //! <b>Effects</b>: Constructs a list that will use a copy of allocator a
  202. //! and inserts n copies of value.
  203. //!
  204. //! <b>Throws</b>: If allocator_type's default constructor
  205. //! throws or T's default or copy constructor throws.
  206. //!
  207. //! <b>Complexity</b>: Linear to n.
  208. slist(size_type n, const allocator_type &a)
  209. : AllocHolder(a)
  210. { this->resize(n); }
  211. //! <b>Effects</b>: Constructs a list that will use a copy of allocator a
  212. //! and inserts n copies of value.
  213. //!
  214. //! <b>Throws</b>: If allocator_type's default constructor
  215. //! throws or T's default or copy constructor throws.
  216. //!
  217. //! <b>Complexity</b>: Linear to n.
  218. explicit slist(size_type n, const value_type& x, const allocator_type& a = allocator_type())
  219. : AllocHolder(a)
  220. { this->insert_after(this->cbefore_begin(), n, x); }
  221. //! <b>Effects</b>: Constructs a list that will use a copy of allocator a
  222. //! and inserts a copy of the range [first, last) in the list.
  223. //!
  224. //! <b>Throws</b>: If allocator_type's default constructor
  225. //! throws or T's constructor taking a dereferenced InIt throws.
  226. //!
  227. //! <b>Complexity</b>: Linear to the range [first, last).
  228. template <class InpIt>
  229. slist(InpIt first, InpIt last, const allocator_type& a = allocator_type())
  230. : AllocHolder(a)
  231. { this->insert_after(this->cbefore_begin(), first, last); }
  232. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  233. //! <b>Effects</b>: Constructs a list that will use a copy of allocator a
  234. //! and inserts a copy of the range [il.begin(), il.end()) in the list.
  235. //!
  236. //! <b>Throws</b>: If allocator_type's default constructor
  237. //! throws or T's constructor taking a dereferenced std::initializer_list iterator throws.
  238. //!
  239. //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
  240. slist(std::initializer_list<value_type> il, const allocator_type& a = allocator_type())
  241. : AllocHolder(a)
  242. { this->insert_after(this->cbefore_begin(), il.begin(), il.end()); }
  243. #endif
  244. //! <b>Effects</b>: Copy constructs a list.
  245. //!
  246. //! <b>Postcondition</b>: x == *this.
  247. //!
  248. //! <b>Throws</b>: If allocator_type's default constructor
  249. //!
  250. //! <b>Complexity</b>: Linear to the elements x contains.
  251. slist(const slist& x)
  252. : AllocHolder(x)
  253. { this->insert_after(this->cbefore_begin(), x.begin(), x.end()); }
  254. //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
  255. //!
  256. //! <b>Throws</b>: If allocator_type's copy constructor throws.
  257. //!
  258. //! <b>Complexity</b>: Constant.
  259. slist(BOOST_RV_REF(slist) x) BOOST_NOEXCEPT_OR_NOTHROW
  260. : AllocHolder(BOOST_MOVE_BASE(AllocHolder, x))
  261. {}
  262. //! <b>Effects</b>: Copy constructs a list using the specified allocator.
  263. //!
  264. //! <b>Postcondition</b>: x == *this.
  265. //!
  266. //! <b>Throws</b>: If allocator_type's default constructor
  267. //!
  268. //! <b>Complexity</b>: Linear to the elements x contains.
  269. slist(const slist& x, const allocator_type &a)
  270. : AllocHolder(a)
  271. { this->insert_after(this->cbefore_begin(), x.begin(), x.end()); }
  272. //! <b>Effects</b>: Move constructor using the specified allocator.
  273. //! Moves x's resources to *this.
  274. //!
  275. //! <b>Throws</b>: If allocation or value_type's copy constructor throws.
  276. //!
  277. //! <b>Complexity</b>: Constant if a == x.get_allocator(), linear otherwise.
  278. slist(BOOST_RV_REF(slist) x, const allocator_type &a)
  279. : AllocHolder(a)
  280. {
  281. slist & sr = x;
  282. if(this->node_alloc() == sr.node_alloc()){
  283. this->icont().swap(sr.icont());
  284. }
  285. else{
  286. this->insert_after(this->cbefore_begin(), boost::make_move_iterator(sr.begin()), boost::make_move_iterator(sr.end()));
  287. }
  288. }
  289. //! <b>Effects</b>: Destroys the list. All stored values are destroyed
  290. //! and used memory is deallocated.
  291. //!
  292. //! <b>Throws</b>: Nothing.
  293. //!
  294. //! <b>Complexity</b>: Linear to the number of elements.
  295. ~slist() BOOST_NOEXCEPT_OR_NOTHROW
  296. {} //AllocHolder clears the slist
  297. //! <b>Effects</b>: Makes *this contain the same elements as x.
  298. //!
  299. //! <b>Postcondition</b>: this->size() == x.size(). *this contains a copy
  300. //! of each of x's elements.
  301. //!
  302. //! <b>Throws</b>: If memory allocation throws or T's copy constructor throws.
  303. //!
  304. //! <b>Complexity</b>: Linear to the number of elements in x.
  305. slist& operator= (BOOST_COPY_ASSIGN_REF(slist) x)
  306. {
  307. if (BOOST_LIKELY(this != &x)) {
  308. NodeAlloc &this_alloc = this->node_alloc();
  309. const NodeAlloc &x_alloc = x.node_alloc();
  310. dtl::bool_<allocator_traits_type::
  311. propagate_on_container_copy_assignment::value> flag;
  312. if(flag && this_alloc != x_alloc){
  313. this->clear();
  314. }
  315. this->AllocHolder::copy_assign_alloc(x);
  316. this->assign(x.begin(), x.end());
  317. }
  318. return *this;
  319. }
  320. //! <b>Effects</b>: Makes *this contain the same elements as x.
  321. //!
  322. //! <b>Postcondition</b>: this->size() == x.size(). *this contains a copy
  323. //! of each of x's elements.
  324. //!
  325. //! <b>Throws</b>: If allocator_traits_type::propagate_on_container_move_assignment
  326. //! is false and (allocation throws or value_type's move constructor throws)
  327. //!
  328. //! <b>Complexity</b>: Constant if allocator_traits_type::
  329. //! propagate_on_container_move_assignment is true or
  330. //! this->get>allocator() == x.get_allocator(). Linear otherwise.
  331. slist& operator=(BOOST_RV_REF(slist) x)
  332. BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value
  333. || allocator_traits_type::is_always_equal::value)
  334. {
  335. if (BOOST_LIKELY(this != &x)) {
  336. //We know resources can be transferred at comiple time if both allocators are
  337. //always equal or the allocator is going to be propagated
  338. const bool can_steal_resources_alloc
  339. = allocator_traits_type::propagate_on_container_move_assignment::value
  340. || allocator_traits_type::is_always_equal::value;
  341. dtl::bool_<can_steal_resources_alloc> flag;
  342. this->priv_move_assign(boost::move(x), flag);
  343. }
  344. return *this;
  345. }
  346. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  347. //! <b>Effects</b>: Makes *this contain the same elements as in il.
  348. //!
  349. //! <b>Postcondition</b>: this->size() == il.size(). *this contains a copy
  350. //! of each of il's elements.
  351. //!
  352. //! <b>Throws</b>: If allocator_traits_type::propagate_on_container_move_assignment
  353. //! is false and (allocation throws or value_type's move constructor throws)
  354. slist& operator=(std::initializer_list<value_type> il)
  355. {
  356. assign(il.begin(), il.end());
  357. return *this;
  358. }
  359. #endif
  360. //! <b>Effects</b>: Assigns the n copies of val to *this.
  361. //!
  362. //! <b>Throws</b>: If memory allocation throws or T's copy constructor throws.
  363. //!
  364. //! <b>Complexity</b>: Linear to n.
  365. void assign(size_type n, const T& val)
  366. {
  367. typedef constant_iterator<value_type> cvalue_iterator;
  368. return this->assign(cvalue_iterator(val, n), cvalue_iterator());
  369. }
  370. //! <b>Effects</b>: Assigns the range [first, last) to *this.
  371. //!
  372. //! <b>Throws</b>: If memory allocation throws or
  373. //! T's constructor from dereferencing InpIt throws.
  374. //!
  375. //! <b>Complexity</b>: Linear to n.
  376. template <class InpIt>
  377. void assign(InpIt first, InpIt last
  378. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  379. , typename dtl::disable_if_convertible<InpIt, size_type>::type * = 0
  380. #endif
  381. )
  382. {
  383. iterator end_n(this->end());
  384. iterator prev(this->before_begin());
  385. iterator node(this->begin());
  386. while (node != end_n && first != last){
  387. *node = *first;
  388. prev = node;
  389. ++node;
  390. ++first;
  391. }
  392. if (first != last)
  393. this->insert_after(prev, first, last);
  394. else
  395. this->erase_after(prev, end_n);
  396. }
  397. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  398. //! <b>Effects</b>: Assigns the range [il.begin(), il.end()) to *this.
  399. //!
  400. //! <b>Throws</b>: If memory allocation throws or
  401. //! T's constructor from dereferencing std::initializer_list iterator throws.
  402. //!
  403. //! <b>Complexity</b>: Linear to range [il.begin(), il.end()).
  404. void assign(std::initializer_list<value_type> il)
  405. {
  406. assign(il.begin(), il.end());
  407. }
  408. #endif
  409. //! <b>Effects</b>: Returns a copy of the internal allocator.
  410. //!
  411. //! <b>Throws</b>: If allocator's copy constructor throws.
  412. //!
  413. //! <b>Complexity</b>: Constant.
  414. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  415. allocator_type get_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
  416. { return allocator_type(this->node_alloc()); }
  417. //! <b>Effects</b>: Returns a reference to the internal allocator.
  418. //!
  419. //! <b>Throws</b>: Nothing
  420. //!
  421. //! <b>Complexity</b>: Constant.
  422. //!
  423. //! <b>Note</b>: Non-standard extension.
  424. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  425. stored_allocator_type &get_stored_allocator() BOOST_NOEXCEPT_OR_NOTHROW
  426. { return this->node_alloc(); }
  427. //! <b>Effects</b>: Returns a reference to the internal allocator.
  428. //!
  429. //! <b>Throws</b>: Nothing
  430. //!
  431. //! <b>Complexity</b>: Constant.
  432. //!
  433. //! <b>Note</b>: Non-standard extension.
  434. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  435. const stored_allocator_type &get_stored_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
  436. { return this->node_alloc(); }
  437. //////////////////////////////////////////////
  438. //
  439. // iterators
  440. //
  441. //////////////////////////////////////////////
  442. //! <b>Effects</b>: Returns a non-dereferenceable iterator that,
  443. //! when incremented, yields begin(). This iterator may be used
  444. //! as the argument to insert_after, erase_after, etc.
  445. //!
  446. //! <b>Throws</b>: Nothing.
  447. //!
  448. //! <b>Complexity</b>: Constant.
  449. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  450. iterator before_begin() BOOST_NOEXCEPT_OR_NOTHROW
  451. { return iterator(end()); }
  452. //! <b>Effects</b>: Returns a non-dereferenceable const_iterator
  453. //! that, when incremented, yields begin(). This iterator may be used
  454. //! as the argument to insert_after, erase_after, etc.
  455. //!
  456. //! <b>Throws</b>: Nothing.
  457. //!
  458. //! <b>Complexity</b>: Constant.
  459. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  460. const_iterator before_begin() const BOOST_NOEXCEPT_OR_NOTHROW
  461. { return this->cbefore_begin(); }
  462. //! <b>Effects</b>: Returns an iterator to the first element contained in the list.
  463. //!
  464. //! <b>Throws</b>: Nothing.
  465. //!
  466. //! <b>Complexity</b>: Constant.
  467. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  468. iterator begin() BOOST_NOEXCEPT_OR_NOTHROW
  469. { return iterator(this->icont().begin()); }
  470. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the list.
  471. //!
  472. //! <b>Throws</b>: Nothing.
  473. //!
  474. //! <b>Complexity</b>: Constant.
  475. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  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. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  484. iterator end() BOOST_NOEXCEPT_OR_NOTHROW
  485. { return iterator(this->icont().end()); }
  486. //! <b>Effects</b>: Returns a const_iterator to the end of the list.
  487. //!
  488. //! <b>Throws</b>: Nothing.
  489. //!
  490. //! <b>Complexity</b>: Constant.
  491. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  492. const_iterator end() const BOOST_NOEXCEPT_OR_NOTHROW
  493. { return this->cend(); }
  494. //! <b>Effects</b>: Returns a non-dereferenceable const_iterator
  495. //! that, when incremented, yields begin(). This iterator may be used
  496. //! as the argument to insert_after, erase_after, etc.
  497. //!
  498. //! <b>Throws</b>: Nothing.
  499. //!
  500. //! <b>Complexity</b>: Constant.
  501. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  502. const_iterator cbefore_begin() const BOOST_NOEXCEPT_OR_NOTHROW
  503. { return const_iterator(end()); }
  504. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the list.
  505. //!
  506. //! <b>Throws</b>: Nothing.
  507. //!
  508. //! <b>Complexity</b>: Constant.
  509. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  510. const_iterator cbegin() const BOOST_NOEXCEPT_OR_NOTHROW
  511. { return const_iterator(this->non_const_icont().begin()); }
  512. //! <b>Effects</b>: Returns a const_iterator to the end of the list.
  513. //!
  514. //! <b>Throws</b>: Nothing.
  515. //!
  516. //! <b>Complexity</b>: Constant.
  517. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  518. const_iterator cend() const BOOST_NOEXCEPT_OR_NOTHROW
  519. { return const_iterator(this->non_const_icont().end()); }
  520. //! <b>Returns</b>: The iterator to the element before i in the sequence.
  521. //! Returns the end-iterator, if either i is the begin-iterator or the
  522. //! sequence is empty.
  523. //!
  524. //! <b>Throws</b>: Nothing.
  525. //!
  526. //! <b>Complexity</b>: Linear to the number of elements before i.
  527. //!
  528. //! <b>Note</b>: Non-standard extension.
  529. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  530. iterator previous(iterator p) BOOST_NOEXCEPT_OR_NOTHROW
  531. { return iterator(this->icont().previous(p.get())); }
  532. //! <b>Returns</b>: The const_iterator to the element before i in the sequence.
  533. //! Returns the end-const_iterator, if either i is the begin-const_iterator or
  534. //! the sequence is empty.
  535. //!
  536. //! <b>Throws</b>: Nothing.
  537. //!
  538. //! <b>Complexity</b>: Linear to the number of elements before i.
  539. //!
  540. //! <b>Note</b>: Non-standard extension.
  541. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  542. const_iterator previous(const_iterator p)
  543. { return const_iterator(this->icont().previous(p.get())); }
  544. //////////////////////////////////////////////
  545. //
  546. // capacity
  547. //
  548. //////////////////////////////////////////////
  549. //! <b>Effects</b>: Returns true if the list contains no elements.
  550. //!
  551. //! <b>Throws</b>: Nothing.
  552. //!
  553. //! <b>Complexity</b>: Constant.
  554. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  555. bool empty() const
  556. { return !this->size(); }
  557. //! <b>Effects</b>: Returns the number of the elements contained in the list.
  558. //!
  559. //! <b>Throws</b>: Nothing.
  560. //!
  561. //! <b>Complexity</b>: Constant.
  562. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  563. size_type size() const
  564. { return this->icont().size(); }
  565. //! <b>Effects</b>: Returns the largest possible size of the list.
  566. //!
  567. //! <b>Throws</b>: Nothing.
  568. //!
  569. //! <b>Complexity</b>: Constant.
  570. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  571. size_type max_size() const
  572. { return AllocHolder::max_size(); }
  573. //! <b>Effects</b>: Inserts or erases elements at the end such that
  574. //! the size becomes n. New elements are value initialized.
  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)
  580. {
  581. const_iterator last_pos;
  582. if(!priv_try_shrink(new_size, last_pos)){
  583. typedef value_init_construct_iterator<value_type> value_init_iterator;
  584. this->insert_after(last_pos, value_init_iterator(new_size - this->size()), value_init_iterator());
  585. }
  586. }
  587. //! <b>Effects</b>: Inserts or erases elements at the end such that
  588. //! the size becomes n. New elements are copy constructed from x.
  589. //!
  590. //! <b>Throws</b>: If memory allocation throws, or T's copy constructor throws.
  591. //!
  592. //! <b>Complexity</b>: Linear to the difference between size() and new_size.
  593. void resize(size_type new_size, const T& x)
  594. {
  595. const_iterator last_pos;
  596. if(!priv_try_shrink(new_size, last_pos)){
  597. this->insert_after(last_pos, new_size, x);
  598. }
  599. }
  600. //////////////////////////////////////////////
  601. //
  602. // element access
  603. //
  604. //////////////////////////////////////////////
  605. //! <b>Requires</b>: !empty()
  606. //!
  607. //! <b>Effects</b>: Returns a reference to the first element
  608. //! from the beginning of the container.
  609. //!
  610. //! <b>Throws</b>: Nothing.
  611. //!
  612. //! <b>Complexity</b>: Constant.
  613. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  614. reference front()
  615. {
  616. BOOST_ASSERT(!this->empty());
  617. return *this->begin();
  618. }
  619. //! <b>Requires</b>: !empty()
  620. //!
  621. //! <b>Effects</b>: Returns a const reference to the first element
  622. //! from the beginning of the container.
  623. //!
  624. //! <b>Throws</b>: Nothing.
  625. //!
  626. //! <b>Complexity</b>: Constant.
  627. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  628. const_reference front() const
  629. {
  630. BOOST_ASSERT(!this->empty());
  631. return *this->begin();
  632. }
  633. //////////////////////////////////////////////
  634. //
  635. // modifiers
  636. //
  637. //////////////////////////////////////////////
  638. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  639. //! <b>Effects</b>: Inserts an object of type T constructed with
  640. //! std::forward<Args>(args)... in the front of the list
  641. //!
  642. //! <b>Returns</b>: A reference to the created object.
  643. //!
  644. //! <b>Throws</b>: If memory allocation throws or
  645. //! T's copy constructor throws.
  646. //!
  647. //! <b>Complexity</b>: Amortized constant time.
  648. template <class... Args>
  649. reference emplace_front(BOOST_FWD_REF(Args)... args)
  650. { return *this->emplace_after(this->cbefore_begin(), boost::forward<Args>(args)...); }
  651. //! <b>Effects</b>: Inserts an object of type T constructed with
  652. //! std::forward<Args>(args)... after prev
  653. //!
  654. //! <b>Throws</b>: If memory allocation throws or
  655. //! T's in-place constructor throws.
  656. //!
  657. //! <b>Complexity</b>: Constant
  658. template <class... Args>
  659. iterator emplace_after(const_iterator prev, BOOST_FWD_REF(Args)... args)
  660. {
  661. NodePtr pnode(AllocHolder::create_node(boost::forward<Args>(args)...));
  662. return iterator(this->icont().insert_after(prev.get(), *pnode));
  663. }
  664. #else // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  665. #define BOOST_CONTAINER_SLIST_EMPLACE_CODE(N) \
  666. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  667. reference emplace_front(BOOST_MOVE_UREF##N)\
  668. { return *this->emplace_after(this->cbefore_begin() BOOST_MOVE_I##N BOOST_MOVE_FWD##N);}\
  669. \
  670. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  671. iterator emplace_after(const_iterator p BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  672. {\
  673. NodePtr pnode (AllocHolder::create_node(BOOST_MOVE_FWD##N));\
  674. return iterator(this->icont().insert_after(p.get(), *pnode));\
  675. }\
  676. //
  677. BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_SLIST_EMPLACE_CODE)
  678. #undef BOOST_CONTAINER_SLIST_EMPLACE_CODE
  679. #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  680. #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  681. //! <b>Effects</b>: Inserts a copy of x at the beginning of the list.
  682. //!
  683. //! <b>Throws</b>: If memory allocation throws or
  684. //! T's copy constructor throws.
  685. //!
  686. //! <b>Complexity</b>: Amortized constant time.
  687. void push_front(const T &x);
  688. //! <b>Effects</b>: Constructs a new element in the beginning of the list
  689. //! and moves the resources of x to this new element.
  690. //!
  691. //! <b>Throws</b>: If memory allocation throws.
  692. //!
  693. //! <b>Complexity</b>: Amortized constant time.
  694. void push_front(T &&x);
  695. #else
  696. BOOST_MOVE_CONVERSION_AWARE_CATCH(push_front, T, void, priv_push_front)
  697. #endif
  698. #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  699. //! <b>Requires</b>: p must be a valid iterator of *this.
  700. //!
  701. //! <b>Effects</b>: Inserts a copy of the value after prev_p.
  702. //!
  703. //! <b>Returns</b>: An iterator to the inserted element.
  704. //!
  705. //! <b>Throws</b>: If memory allocation throws or T's copy constructor throws.
  706. //!
  707. //! <b>Complexity</b>: Amortized constant time.
  708. //!
  709. //! <b>Note</b>: Does not affect the validity of iterators and references of
  710. //! previous values.
  711. iterator insert_after(const_iterator prev_p, const T &x);
  712. //! <b>Requires</b>: prev_p must be a valid iterator of *this.
  713. //!
  714. //! <b>Effects</b>: Inserts a move constructed copy object from the value after the
  715. //! element pointed by prev_p.
  716. //!
  717. //! <b>Returns</b>: An iterator to the inserted element.
  718. //!
  719. //! <b>Throws</b>: If memory allocation throws.
  720. //!
  721. //! <b>Complexity</b>: Amortized constant time.
  722. //!
  723. //! <b>Note</b>: Does not affect the validity of iterators and references of
  724. //! previous values.
  725. iterator insert_after(const_iterator prev_p, T &&x);
  726. #else
  727. BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert_after, T, iterator, priv_insert_after, const_iterator, const_iterator)
  728. #endif
  729. //! <b>Requires</b>: prev_p must be a valid iterator of *this.
  730. //!
  731. //! <b>Effects</b>: Inserts n copies of x after prev_p.
  732. //!
  733. //! <b>Returns</b>: an iterator to the last inserted element or prev_p if n is 0.
  734. //!
  735. //! <b>Throws</b>: If memory allocation throws or T's copy constructor throws.
  736. //!
  737. //!
  738. //! <b>Complexity</b>: Linear to n.
  739. //!
  740. //! <b>Note</b>: Does not affect the validity of iterators and references of
  741. //! previous values.
  742. iterator insert_after(const_iterator prev_p, size_type n, const value_type& x)
  743. {
  744. typedef constant_iterator<value_type> cvalue_iterator;
  745. return this->insert_after(prev_p, cvalue_iterator(x, n), cvalue_iterator());
  746. }
  747. //! <b>Requires</b>: prev_p must be a valid iterator of *this.
  748. //!
  749. //! <b>Effects</b>: Inserts the range pointed by [first, last) after prev_p.
  750. //!
  751. //! <b>Returns</b>: an iterator to the last inserted element or prev_p if first == last.
  752. //!
  753. //! <b>Throws</b>: If memory allocation throws, T's constructor from a
  754. //! dereferenced InpIt throws.
  755. //!
  756. //! <b>Complexity</b>: Linear to the number of elements inserted.
  757. //!
  758. //! <b>Note</b>: Does not affect the validity of iterators and references of
  759. //! previous values.
  760. template <class InpIt>
  761. iterator insert_after(const_iterator prev_p, InpIt first, InpIt last
  762. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  763. , typename dtl::enable_if_c
  764. < !dtl::is_convertible<InpIt, size_type>::value
  765. && (dtl::is_input_iterator<InpIt>::value
  766. || dtl::is_same<alloc_version, version_1>::value
  767. )
  768. >::type * = 0
  769. #endif
  770. )
  771. {
  772. iterator ret_it(prev_p.get());
  773. for (; first != last; ++first){
  774. ret_it = iterator(this->icont().insert_after(ret_it.get(), *this->create_node_from_it(first)));
  775. }
  776. return ret_it;
  777. }
  778. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  779. //! <b>Requires</b>: prev_p must be a valid iterator of *this.
  780. //!
  781. //! <b>Effects</b>: Inserts the range pointed by [il.begin(), il.end()) after prev_p.
  782. //!
  783. //! <b>Returns</b>: an iterator to the last inserted element or prev_p if il.begin() == il.end().
  784. //!
  785. //! <b>Throws</b>: If memory allocation throws, T's constructor from a
  786. //! dereferenced std::initializer_list iterator throws.
  787. //!
  788. //! <b>Complexity</b>: Linear to the number of elements inserted.
  789. //!
  790. //! <b>Note</b>: Does not affect the validity of iterators and references of
  791. //! previous values.
  792. iterator insert_after(const_iterator prev_p, std::initializer_list<value_type> il)
  793. {
  794. return insert_after(prev_p, il.begin(), il.end());
  795. }
  796. #endif
  797. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  798. template <class FwdIt>
  799. iterator insert_after(const_iterator prev, FwdIt first, FwdIt last
  800. , typename dtl::enable_if_c
  801. < !dtl::is_convertible<FwdIt, size_type>::value
  802. && !(dtl::is_input_iterator<FwdIt>::value
  803. || dtl::is_same<alloc_version, version_1>::value
  804. )
  805. >::type * = 0
  806. )
  807. {
  808. //Optimized allocation and construction
  809. insertion_functor func(this->icont(), prev.get());
  810. this->allocate_many_and_construct(first, boost::container::iterator_udistance(first, last), func);
  811. return iterator(func.inserted_first());
  812. }
  813. #endif
  814. //! <b>Effects</b>: Removes the first element from the list.
  815. //!
  816. //! <b>Throws</b>: Nothing.
  817. //!
  818. //! <b>Complexity</b>: Amortized constant time.
  819. void pop_front()
  820. {
  821. BOOST_ASSERT(!this->empty());
  822. this->icont().pop_front_and_dispose(Destroyer(this->node_alloc()));
  823. }
  824. //! <b>Effects</b>: Erases the element after the element pointed by prev_p
  825. //! of the list.
  826. //!
  827. //! <b>Returns</b>: the first element remaining beyond the removed elements,
  828. //! or end() if no such element exists.
  829. //!
  830. //! <b>Throws</b>: Nothing.
  831. //!
  832. //! <b>Complexity</b>: Constant.
  833. //!
  834. //! <b>Note</b>: Does not invalidate iterators or references to non erased elements.
  835. iterator erase_after(const_iterator prev_p)
  836. {
  837. return iterator(this->icont().erase_after_and_dispose(prev_p.get(), Destroyer(this->node_alloc())));
  838. }
  839. //! <b>Effects</b>: Erases the range (before_first, last) from
  840. //! the list.
  841. //!
  842. //! <b>Returns</b>: the first element remaining beyond the removed elements,
  843. //! or end() if no such element exists.
  844. //!
  845. //! <b>Throws</b>: Nothing.
  846. //!
  847. //! <b>Complexity</b>: Linear to the number of erased elements.
  848. //!
  849. //! <b>Note</b>: Does not invalidate iterators or references to non erased elements.
  850. iterator erase_after(const_iterator before_first, const_iterator last)
  851. {
  852. return iterator(this->icont().erase_after_and_dispose(before_first.get(), last.get(), Destroyer(this->node_alloc())));
  853. }
  854. //! <b>Effects</b>: Swaps the contents of *this and x.
  855. //!
  856. //! <b>Throws</b>: Nothing.
  857. //!
  858. //! <b>Complexity</b>: Linear to the number of elements on *this and x.
  859. void swap(slist& x)
  860. BOOST_NOEXCEPT_IF( allocator_traits_type::propagate_on_container_swap::value
  861. || allocator_traits_type::is_always_equal::value)
  862. {
  863. BOOST_ASSERT(allocator_traits_type::propagate_on_container_swap::value ||
  864. allocator_traits_type::is_always_equal::value ||
  865. this->get_stored_allocator() == x.get_stored_allocator());
  866. AllocHolder::swap(x);
  867. }
  868. //! <b>Effects</b>: Erases all the elements of the list.
  869. //!
  870. //! <b>Throws</b>: Nothing.
  871. //!
  872. //! <b>Complexity</b>: Linear to the number of elements in the list.
  873. void clear()
  874. { this->icont().clear_and_dispose(Destroyer(this->node_alloc())); }
  875. //////////////////////////////////////////////
  876. //
  877. // slist operations
  878. //
  879. //////////////////////////////////////////////
  880. //! <b>Requires</b>: p must point to an element contained
  881. //! by the list. x != *this
  882. //!
  883. //! <b>Effects</b>: Transfers all the elements of list x to this list, after the
  884. //! the element pointed by p. No destructors or copy constructors are called.
  885. //!
  886. //! <b>Throws</b>: runtime_error if this' allocator and x's allocator
  887. //! are not equal.
  888. //!
  889. //! <b>Complexity</b>: Linear to the elements in x.
  890. //!
  891. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of
  892. //! this list. Iterators of this list and all the references are not invalidated.
  893. void splice_after(const_iterator prev_p, slist& x) BOOST_NOEXCEPT_OR_NOTHROW
  894. {
  895. BOOST_ASSERT(this != &x);
  896. BOOST_ASSERT(this->node_alloc() == x.node_alloc());
  897. this->icont().splice_after(prev_p.get(), x.icont());
  898. }
  899. //! <b>Requires</b>: p must point to an element contained
  900. //! by the list. x != *this
  901. //!
  902. //! <b>Effects</b>: Transfers all the elements of list x to this list, after the
  903. //! the element pointed by p. No destructors or copy constructors are called.
  904. //!
  905. //! <b>Throws</b>: runtime_error if this' allocator and x's allocator
  906. //! are not equal.
  907. //!
  908. //! <b>Complexity</b>: Linear to the elements in x.
  909. //!
  910. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of
  911. //! this list. Iterators of this list and all the references are not invalidated.
  912. void splice_after(const_iterator prev_p, BOOST_RV_REF(slist) x) BOOST_NOEXCEPT_OR_NOTHROW
  913. { this->splice_after(prev_p, BOOST_MOVE_TO_LV(x)); }
  914. //! <b>Requires</b>: prev_p must be a valid iterator of this.
  915. //! i must point to an element contained in list x.
  916. //! this' allocator and x's allocator shall compare equal.
  917. //!
  918. //! <b>Effects</b>: Transfers the value pointed by i, from list x to this list,
  919. //! after the element pointed by prev_p.
  920. //! If prev_p == prev or prev_p == ++prev, this function is a null operation.
  921. //!
  922. //! <b>Throws</b>: Nothing
  923. //!
  924. //! <b>Complexity</b>: Constant.
  925. //!
  926. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  927. //! list. Iterators of this list and all the references are not invalidated.
  928. void splice_after(const_iterator prev_p, slist& x, const_iterator prev) BOOST_NOEXCEPT_OR_NOTHROW
  929. {
  930. BOOST_ASSERT(this->node_alloc() == x.node_alloc());
  931. this->icont().splice_after(prev_p.get(), x.icont(), prev.get());
  932. }
  933. //! <b>Requires</b>: prev_p must be a valid iterator of this.
  934. //! i must point to an element contained in list x.
  935. //! this' allocator and x's allocator shall compare equal.
  936. //!
  937. //! <b>Effects</b>: Transfers the value pointed by i, from list x to this list,
  938. //! after the element pointed by prev_p.
  939. //! If prev_p == prev or prev_p == ++prev, this function is a null operation.
  940. //!
  941. //! <b>Throws</b>: Nothing
  942. //!
  943. //! <b>Complexity</b>: Constant.
  944. //!
  945. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  946. //! list. Iterators of this list and all the references are not invalidated.
  947. void splice_after(const_iterator prev_p, BOOST_RV_REF(slist) x, const_iterator prev) BOOST_NOEXCEPT_OR_NOTHROW
  948. { this->splice_after(prev_p, BOOST_MOVE_TO_LV(x), prev); }
  949. //! <b>Requires</b>: prev_p must be a valid iterator of this.
  950. //! before_first and before_last must be valid iterators of x.
  951. //! prev_p must not be contained in [before_first, before_last) range.
  952. //! this' allocator and x's allocator shall compare equal.
  953. //!
  954. //! <b>Effects</b>: Transfers the range [before_first + 1, before_last + 1)
  955. //! from list x to this list, after the element pointed by prev_p.
  956. //!
  957. //! <b>Throws</b>: Nothing
  958. //!
  959. //! <b>Complexity</b>: Linear to the number of transferred elements.
  960. //!
  961. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  962. //! list. Iterators of this list and all the references are not invalidated.
  963. void splice_after(const_iterator prev_p, slist& x,
  964. const_iterator before_first, const_iterator before_last) BOOST_NOEXCEPT_OR_NOTHROW
  965. {
  966. BOOST_ASSERT(this->node_alloc() == x.node_alloc());
  967. this->icont().splice_after
  968. (prev_p.get(), x.icont(), before_first.get(), before_last.get());
  969. }
  970. //! <b>Requires</b>: prev_p must be a valid iterator of this.
  971. //! before_first and before_last must be valid iterators of x.
  972. //! prev_p must not be contained in [before_first, before_last) range.
  973. //! this' allocator and x's allocator shall compare equal.
  974. //!
  975. //! <b>Effects</b>: Transfers the range [before_first + 1, before_last + 1)
  976. //! from list x to this list, after the element pointed by prev_p.
  977. //!
  978. //! <b>Throws</b>: Nothing
  979. //!
  980. //! <b>Complexity</b>: Linear to the number of transferred elements.
  981. //!
  982. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  983. //! list. Iterators of this list and all the references are not invalidated.
  984. void splice_after(const_iterator prev_p, BOOST_RV_REF(slist) x,
  985. const_iterator before_first, const_iterator before_last) BOOST_NOEXCEPT_OR_NOTHROW
  986. { this->splice_after(prev_p, BOOST_MOVE_TO_LV(x), before_first, before_last); }
  987. //! <b>Requires</b>: prev_p must be a valid iterator of this.
  988. //! before_first and before_last must be valid iterators of x.
  989. //! prev_p must not be contained in [before_first, before_last) range.
  990. //! n == distance(before_first, before_last).
  991. //! this' allocator and x's allocator shall compare equal.
  992. //!
  993. //! <b>Effects</b>: Transfers the range [before_first + 1, before_last + 1)
  994. //! from list x to this list, after the element pointed by prev_p.
  995. //!
  996. //! <b>Throws</b>: Nothing
  997. //!
  998. //! <b>Complexity</b>: Constant.
  999. //!
  1000. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1001. //! list. Iterators of this list and all the references are not invalidated.
  1002. void splice_after(const_iterator prev_p, slist& x,
  1003. const_iterator before_first, const_iterator before_last,
  1004. size_type n) BOOST_NOEXCEPT_OR_NOTHROW
  1005. {
  1006. BOOST_ASSERT(this->node_alloc() == x.node_alloc());
  1007. this->icont().splice_after
  1008. (prev_p.get(), x.icont(), before_first.get(), before_last.get(), n);
  1009. }
  1010. //! <b>Requires</b>: prev_p must be a valid iterator of this.
  1011. //! before_first and before_last must be valid iterators of x.
  1012. //! prev_p must not be contained in [before_first, before_last) range.
  1013. //! n == distance(before_first, before_last).
  1014. //! this' allocator and x's allocator shall compare equal.
  1015. //!
  1016. //! <b>Effects</b>: Transfers the range [before_first + 1, before_last + 1)
  1017. //! from list x to this list, after the element pointed by prev_p.
  1018. //!
  1019. //! <b>Throws</b>: Nothing
  1020. //!
  1021. //! <b>Complexity</b>: Constant.
  1022. //!
  1023. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1024. //! list. Iterators of this list and all the references are not invalidated.
  1025. void splice_after(const_iterator prev_p, BOOST_RV_REF(slist) x,
  1026. const_iterator before_first, const_iterator before_last,
  1027. size_type n) BOOST_NOEXCEPT_OR_NOTHROW
  1028. { this->splice_after(prev_p, BOOST_MOVE_TO_LV(x), before_first, before_last, n); }
  1029. //! <b>Effects</b>: Removes all the elements that compare equal to value.
  1030. //!
  1031. //! <b>Throws</b>: Nothing.
  1032. //!
  1033. //! <b>Complexity</b>: Linear time. It performs exactly size() comparisons for equality.
  1034. //!
  1035. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1036. //! and iterators to elements that are not removed remain valid.
  1037. void remove(const T& value)
  1038. { this->remove_if(equal_to_value_type(value)); }
  1039. //! <b>Effects</b>: Removes all the elements for which a specified
  1040. //! predicate is satisfied.
  1041. //!
  1042. //! <b>Throws</b>: If pred throws.
  1043. //!
  1044. //! <b>Complexity</b>: Linear time. It performs exactly size() calls to the predicate.
  1045. //!
  1046. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1047. //! and iterators to elements that are not removed remain valid.
  1048. template <class Pred>
  1049. void remove_if(Pred pred)
  1050. {
  1051. typedef value_to_node_compare<Node, Pred> value_to_node_compare_type;
  1052. this->icont().remove_and_dispose_if(value_to_node_compare_type(pred), Destroyer(this->node_alloc()));
  1053. }
  1054. //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
  1055. //! elements that are equal from the list.
  1056. //!
  1057. //! <b>Throws</b>: If comparison throws.
  1058. //!
  1059. //! <b>Complexity</b>: Linear time (size()-1 comparisons equality comparisons).
  1060. //!
  1061. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1062. //! and iterators to elements that are not removed remain valid.
  1063. void unique()
  1064. { this->unique(value_equal_t()); }
  1065. //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
  1066. //! elements that satisfy some binary predicate from the list.
  1067. //!
  1068. //! <b>Throws</b>: If pred throws.
  1069. //!
  1070. //! <b>Complexity</b>: Linear time (size()-1 comparisons calls to pred()).
  1071. //!
  1072. //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
  1073. //! and iterators to elements that are not removed remain valid.
  1074. template <class Pred>
  1075. void unique(Pred pred)
  1076. {
  1077. typedef value_to_node_compare<Node, Pred> value_to_node_compare_type;
  1078. this->icont().unique_and_dispose(value_to_node_compare_type(pred), Destroyer(this->node_alloc()));
  1079. }
  1080. //! <b>Requires</b>: The lists x and *this must be distinct.
  1081. //!
  1082. //! <b>Effects</b>: This function removes all of x's elements and inserts them
  1083. //! in order into *this according to std::less<value_type>. The merge is stable;
  1084. //! that is, if an element from *this is equivalent to one from x, then the element
  1085. //! from *this will precede the one from x.
  1086. //!
  1087. //! <b>Throws</b>: If comparison throws.
  1088. //!
  1089. //! <b>Complexity</b>: This function is linear time: it performs at most
  1090. //! size() + x.size() - 1 comparisons.
  1091. void merge(slist & x)
  1092. { this->merge(x, value_less_t()); }
  1093. //! <b>Requires</b>: The lists x and *this must be distinct.
  1094. //!
  1095. //! <b>Effects</b>: This function removes all of x's elements and inserts them
  1096. //! in order into *this according to std::less<value_type>. The merge is stable;
  1097. //! that is, if an element from *this is equivalent to one from x, then the element
  1098. //! from *this will precede the one from x.
  1099. //!
  1100. //! <b>Throws</b>: If comparison throws.
  1101. //!
  1102. //! <b>Complexity</b>: This function is linear time: it performs at most
  1103. //! size() + x.size() - 1 comparisons.
  1104. void merge(BOOST_RV_REF(slist) x)
  1105. { this->merge(BOOST_MOVE_TO_LV(x)); }
  1106. //! <b>Requires</b>: p must be a comparison function that induces a strict weak
  1107. //! ordering and both *this and x must be sorted according to that ordering
  1108. //! The lists x and *this must be distinct.
  1109. //!
  1110. //! <b>Effects</b>: This function removes all of x's elements and inserts them
  1111. //! in order into *this. The merge is stable; that is, if an element from *this is
  1112. //! equivalent to one from x, then the element from *this will precede the one from x.
  1113. //!
  1114. //! <b>Throws</b>: If comp throws.
  1115. //!
  1116. //! <b>Complexity</b>: This function is linear time: it performs at most
  1117. //! size() + x.size() - 1 comparisons.
  1118. //!
  1119. //! <b>Note</b>: Iterators and references to *this are not invalidated.
  1120. template <class StrictWeakOrdering>
  1121. void merge(slist& x, StrictWeakOrdering comp)
  1122. {
  1123. typedef value_to_node_compare<Node, StrictWeakOrdering> value_to_node_compare_type;
  1124. BOOST_ASSERT(this->node_alloc() == x.node_alloc());
  1125. this->icont().merge(x.icont(), value_to_node_compare_type(comp));
  1126. }
  1127. //! <b>Requires</b>: p must be a comparison function that induces a strict weak
  1128. //! ordering and both *this and x must be sorted according to that ordering
  1129. //! The lists x and *this must be distinct.
  1130. //!
  1131. //! <b>Effects</b>: This function removes all of x's elements and inserts them
  1132. //! in order into *this. The merge is stable; that is, if an element from *this is
  1133. //! equivalent to one from x, then the element from *this will precede the one from x.
  1134. //!
  1135. //! <b>Throws</b>: If comp throws.
  1136. //!
  1137. //! <b>Complexity</b>: This function is linear time: it performs at most
  1138. //! size() + x.size() - 1 comparisons.
  1139. //!
  1140. //! <b>Note</b>: Iterators and references to *this are not invalidated.
  1141. template <class StrictWeakOrdering>
  1142. void merge(BOOST_RV_REF(slist) x, StrictWeakOrdering comp)
  1143. { this->merge(BOOST_MOVE_TO_LV(x), comp); }
  1144. //! <b>Effects</b>: This function sorts the list *this according to std::less<value_type>.
  1145. //! The sort is stable, that is, the relative order of equivalent elements is preserved.
  1146. //!
  1147. //! <b>Throws</b>: If comparison throws.
  1148. //!
  1149. //! <b>Notes</b>: Iterators and references are not invalidated.
  1150. //!
  1151. //! <b>Complexity</b>: The number of comparisons is approximately N log N, where N
  1152. //! is the list's size.
  1153. void sort()
  1154. { this->sort(value_less_t()); }
  1155. //! <b>Effects</b>: This function sorts the list *this according to std::less<value_type>.
  1156. //! The sort is stable, that is, the relative order of equivalent elements is preserved.
  1157. //!
  1158. //! <b>Throws</b>: If comp throws.
  1159. //!
  1160. //! <b>Notes</b>: Iterators and references are not invalidated.
  1161. //!
  1162. //! <b>Complexity</b>: The number of comparisons is approximately N log N, where N
  1163. //! is the list's size.
  1164. template <class StrictWeakOrdering>
  1165. void sort(StrictWeakOrdering comp)
  1166. {
  1167. typedef value_to_node_compare<Node, StrictWeakOrdering> value_to_node_compare_type;
  1168. // nothing if the slist has length 0 or 1.
  1169. if (this->size() < 2)
  1170. return;
  1171. this->icont().sort(value_to_node_compare_type(comp));
  1172. }
  1173. //! <b>Effects</b>: Reverses the order of elements in the list.
  1174. //!
  1175. //! <b>Throws</b>: Nothing.
  1176. //!
  1177. //! <b>Complexity</b>: This function is linear time.
  1178. //!
  1179. //! <b>Note</b>: Iterators and references are not invalidated
  1180. void reverse() BOOST_NOEXCEPT_OR_NOTHROW
  1181. { this->icont().reverse(); }
  1182. //////////////////////////////////////////////
  1183. //
  1184. // list compatibility interface
  1185. //
  1186. //////////////////////////////////////////////
  1187. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1188. //! <b>Effects</b>: Inserts an object of type T constructed with
  1189. //! std::forward<Args>(args)... before p
  1190. //!
  1191. //! <b>Throws</b>: If memory allocation throws or
  1192. //! T's in-place constructor throws.
  1193. //!
  1194. //! <b>Complexity</b>: Linear to the elements before p
  1195. template <class... Args>
  1196. iterator emplace(const_iterator p, BOOST_FWD_REF(Args)... args)
  1197. { return this->emplace_after(this->previous(p), boost::forward<Args>(args)...); }
  1198. #else
  1199. #define BOOST_CONTAINER_SLIST_EMPLACE_CODE(N) \
  1200. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  1201. iterator emplace(const_iterator p BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  1202. {\
  1203. return this->emplace_after(this->previous(p) BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  1204. }\
  1205. //
  1206. BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_SLIST_EMPLACE_CODE)
  1207. #undef BOOST_CONTAINER_SLIST_EMPLACE_CODE
  1208. #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  1209. #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1210. //! <b>Requires</b>: p must be a valid iterator of *this.
  1211. //!
  1212. //! <b>Effects</b>: Insert a copy of x before p.
  1213. //!
  1214. //! <b>Returns</b>: an iterator to the inserted element.
  1215. //!
  1216. //! <b>Throws</b>: If memory allocation throws or x's copy constructor throws.
  1217. //!
  1218. //! <b>Complexity</b>: Linear to the elements before p.
  1219. iterator insert(const_iterator p, const T &x);
  1220. //! <b>Requires</b>: p must be a valid iterator of *this.
  1221. //!
  1222. //! <b>Effects</b>: Insert a new element before p with x's resources.
  1223. //!
  1224. //! <b>Returns</b>: an iterator to the inserted element.
  1225. //!
  1226. //! <b>Throws</b>: If memory allocation throws.
  1227. //!
  1228. //! <b>Complexity</b>: Linear to the elements before p.
  1229. iterator insert(const_iterator prev_p, T &&x);
  1230. #else
  1231. BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, T, iterator, priv_insert, const_iterator, const_iterator)
  1232. #endif
  1233. //! <b>Requires</b>: p must be a valid iterator of *this.
  1234. //!
  1235. //! <b>Effects</b>: Inserts n copies of x before p.
  1236. //!
  1237. //! <b>Returns</b>: an iterator to the first inserted element or p if n == 0.
  1238. //!
  1239. //! <b>Throws</b>: If memory allocation throws or T's copy constructor throws.
  1240. //!
  1241. //! <b>Complexity</b>: Linear to n plus linear to the elements before p.
  1242. iterator insert(const_iterator p, size_type n, const value_type& x)
  1243. {
  1244. const_iterator prev(this->previous(p));
  1245. this->insert_after(prev, n, x);
  1246. return ++iterator(prev.get());
  1247. }
  1248. //! <b>Requires</b>: p must be a valid iterator of *this.
  1249. //!
  1250. //! <b>Effects</b>: Insert a copy of the [first, last) range before p.
  1251. //!
  1252. //! <b>Returns</b>: an iterator to the first inserted element or p if first == last.
  1253. //!
  1254. //! <b>Throws</b>: If memory allocation throws, T's constructor from a
  1255. //! dereferenced InpIt throws.
  1256. //!
  1257. //! <b>Complexity</b>: Linear to distance [first, last) plus
  1258. //! linear to the elements before p.
  1259. template <class InIter>
  1260. iterator insert(const_iterator p, InIter first, InIter last)
  1261. {
  1262. const_iterator prev(this->previous(p));
  1263. this->insert_after(prev, first, last);
  1264. return ++iterator(prev.get());
  1265. }
  1266. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  1267. //! <b>Requires</b>: p must be a valid iterator of *this.
  1268. //!
  1269. //! <b>Effects</b>: Insert a copy of the [il.begin(), il.end()) range before p.
  1270. //!
  1271. //! <b>Returns</b>: an iterator to the first inserted element or p if il.begin() == il.end().
  1272. //!
  1273. //! <b>Throws</b>: If memory allocation throws, T's constructor from a
  1274. //! dereferenced std::initializer_list iterator throws.
  1275. //!
  1276. //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()) plus
  1277. //! linear to the elements before p.
  1278. iterator insert(const_iterator p, std::initializer_list<value_type> il)
  1279. {
  1280. return insert(p, il.begin(), il.end());
  1281. }
  1282. #endif
  1283. //! <b>Requires</b>: p must be a valid iterator of *this.
  1284. //!
  1285. //! <b>Effects</b>: Erases the element at p.
  1286. //!
  1287. //! <b>Throws</b>: Nothing.
  1288. //!
  1289. //! <b>Complexity</b>: Linear to the number of elements before p.
  1290. iterator erase(const_iterator p) BOOST_NOEXCEPT_OR_NOTHROW
  1291. { return iterator(this->erase_after(previous(p))); }
  1292. //! <b>Requires</b>: first and last must be valid iterator to elements in *this.
  1293. //!
  1294. //! <b>Effects</b>: Erases the elements pointed by [first, last).
  1295. //!
  1296. //! <b>Throws</b>: Nothing.
  1297. //!
  1298. //! <b>Complexity</b>: Linear to the distance between first and last plus
  1299. //! linear to the elements before first.
  1300. iterator erase(const_iterator first, const_iterator last) BOOST_NOEXCEPT_OR_NOTHROW
  1301. { return iterator(this->erase_after(previous(first), last)); }
  1302. //! <b>Requires</b>: p must point to an element contained
  1303. //! by the list. x != *this. this' allocator and x's allocator shall compare equal
  1304. //!
  1305. //! <b>Effects</b>: Transfers all the elements of list x to this list, before the
  1306. //! the element pointed by p. No destructors or copy constructors are called.
  1307. //!
  1308. //! <b>Throws</b>: Nothing
  1309. //!
  1310. //! <b>Complexity</b>: Linear in distance(begin(), p), and linear in x.size().
  1311. //!
  1312. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of
  1313. //! this list. Iterators of this list and all the references are not invalidated.
  1314. void splice(const_iterator p, slist& x) BOOST_NOEXCEPT_OR_NOTHROW
  1315. { this->splice_after(this->previous(p), x); }
  1316. //! <b>Requires</b>: p must point to an element contained
  1317. //! by the list. x != *this. this' allocator and x's allocator shall compare equal
  1318. //!
  1319. //! <b>Effects</b>: Transfers all the elements of list x to this list, before the
  1320. //! the element pointed by p. No destructors or copy constructors are called.
  1321. //!
  1322. //! <b>Throws</b>: Nothing
  1323. //!
  1324. //! <b>Complexity</b>: Linear in distance(begin(), p), and linear in x.size().
  1325. //!
  1326. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of
  1327. //! this list. Iterators of this list and all the references are not invalidated.
  1328. void splice(const_iterator p, BOOST_RV_REF(slist) x) BOOST_NOEXCEPT_OR_NOTHROW
  1329. { this->splice(p, BOOST_MOVE_TO_LV(x)); }
  1330. //! <b>Requires</b>: p must point to an element contained
  1331. //! by this list. i must point to an element contained in list x.
  1332. //! this' allocator and x's allocator shall compare equal
  1333. //!
  1334. //! <b>Effects</b>: Transfers the value pointed by i, from list x to this list,
  1335. //! before the element pointed by p. No destructors or copy constructors are called.
  1336. //! If p == i or p == ++i, this function is a null operation.
  1337. //!
  1338. //! <b>Throws</b>: Nothing
  1339. //!
  1340. //! <b>Complexity</b>: Linear in distance(begin(), p), and in distance(x.begin(), i).
  1341. //!
  1342. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1343. //! list. Iterators of this list and all the references are not invalidated.
  1344. void splice(const_iterator p, slist& x, const_iterator i) BOOST_NOEXCEPT_OR_NOTHROW
  1345. { this->splice_after(this->previous(p), x, x.previous(i)); }
  1346. //! <b>Requires</b>: p must point to an element contained
  1347. //! by this list. i must point to an element contained in list x.
  1348. //! this' allocator and x's allocator shall compare equal.
  1349. //!
  1350. //! <b>Effects</b>: Transfers the value pointed by i, from list x to this list,
  1351. //! before the element pointed by p. No destructors or copy constructors are called.
  1352. //! If p == i or p == ++i, this function is a null operation.
  1353. //!
  1354. //! <b>Throws</b>: Nothing
  1355. //!
  1356. //! <b>Complexity</b>: Linear in distance(begin(), p), and in distance(x.begin(), i).
  1357. //!
  1358. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1359. //! list. Iterators of this list and all the references are not invalidated.
  1360. void splice(const_iterator p, BOOST_RV_REF(slist) x, const_iterator i) BOOST_NOEXCEPT_OR_NOTHROW
  1361. { this->splice(p, BOOST_MOVE_TO_LV(x), i); }
  1362. //! <b>Requires</b>: p must point to an element contained
  1363. //! by this list. first and last must point to elements contained in list x.
  1364. //!
  1365. //! <b>Effects</b>: Transfers the range pointed by first and last from list x to this list,
  1366. //! before the element pointed by p. No destructors or copy constructors are called.
  1367. //! this' allocator and x's allocator shall compare equal.
  1368. //!
  1369. //! <b>Throws</b>: Nothing
  1370. //!
  1371. //! <b>Complexity</b>: Linear in distance(begin(), p), in distance(x.begin(), first),
  1372. //! and in distance(first, last).
  1373. //!
  1374. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1375. //! list. Iterators of this list and all the references are not invalidated.
  1376. void splice(const_iterator p, slist& x, const_iterator first, const_iterator last) BOOST_NOEXCEPT_OR_NOTHROW
  1377. { this->splice_after(this->previous(p), x, x.previous(first), x.previous(last)); }
  1378. //! <b>Requires</b>: p must point to an element contained
  1379. //! by this list. first and last must point to elements contained in list x.
  1380. //! this' allocator and x's allocator shall compare equal
  1381. //!
  1382. //! <b>Effects</b>: Transfers the range pointed by first and last from list x to this list,
  1383. //! before the element pointed by p. No destructors or copy constructors are called.
  1384. //!
  1385. //! <b>Throws</b>: Nothing
  1386. //!
  1387. //! <b>Complexity</b>: Linear in distance(begin(), p), in distance(x.begin(), first),
  1388. //! and in distance(first, last).
  1389. //!
  1390. //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
  1391. //! list. Iterators of this list and all the references are not invalidated.
  1392. void splice(const_iterator p, BOOST_RV_REF(slist) x, const_iterator first, const_iterator last) BOOST_NOEXCEPT_OR_NOTHROW
  1393. { this->splice(p, BOOST_MOVE_TO_LV(x), first, last); }
  1394. //! <b>Effects</b>: Returns true if x and y are equal
  1395. //!
  1396. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1397. friend bool operator==(const slist& x, const slist& y)
  1398. { return x.size() == y.size() && ::boost::container::algo_equal(x.begin(), x.end(), y.begin()); }
  1399. //! <b>Effects</b>: Returns true if x and y are unequal
  1400. //!
  1401. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1402. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1403. friend bool operator!=(const slist& x, const slist& y)
  1404. { return !(x == y); }
  1405. //! <b>Effects</b>: Returns true if x is less than y
  1406. //!
  1407. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1408. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1409. friend bool operator<(const slist& x, const slist& y)
  1410. { return ::boost::container::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); }
  1411. //! <b>Effects</b>: Returns true if x is greater than y
  1412. //!
  1413. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1414. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1415. friend bool operator>(const slist& x, const slist& y)
  1416. { return y < x; }
  1417. //! <b>Effects</b>: Returns true if x is equal or less than y
  1418. //!
  1419. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1420. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1421. friend bool operator<=(const slist& x, const slist& y)
  1422. { return !(y < x); }
  1423. //! <b>Effects</b>: Returns true if x is equal or greater than y
  1424. //!
  1425. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1426. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1427. friend bool operator>=(const slist& x, const slist& y)
  1428. { return !(x < y); }
  1429. //! <b>Effects</b>: x.swap(y)
  1430. //!
  1431. //! <b>Complexity</b>: Constant.
  1432. friend void swap(slist& x, slist& y)
  1433. BOOST_NOEXCEPT_IF(BOOST_NOEXCEPT(x.swap(y)))
  1434. { x.swap(y); }
  1435. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  1436. private:
  1437. //`allocator_type::value_type` must match container's `value type`. If this
  1438. //assertion fails, please review your allocator definition.
  1439. BOOST_CONTAINER_STATIC_ASSERT((dtl::is_same<value_type, typename allocator_traits_type::value_type>::value));
  1440. void priv_move_assign(BOOST_RV_REF(slist) x, dtl::bool_<true> /*steal_resources*/)
  1441. {
  1442. //Destroy objects but retain memory in case x reuses it in the future
  1443. this->clear();
  1444. //Move allocator if needed
  1445. this->AllocHolder::move_assign_alloc(x);
  1446. //Obtain resources
  1447. this->icont() = boost::move(x.icont());
  1448. }
  1449. void priv_move_assign(BOOST_RV_REF(slist) x, dtl::bool_<false> /*steal_resources*/)
  1450. {
  1451. //We can't guarantee a compile-time equal allocator or propagation so fallback to runtime
  1452. //Resources can be transferred if both allocators are equal
  1453. if (this->node_alloc() == x.node_alloc()) {
  1454. this->priv_move_assign(boost::move(x), dtl::true_());
  1455. }
  1456. else {
  1457. this->assign(boost::make_move_iterator(x.begin()), boost::make_move_iterator(x.end()));
  1458. }
  1459. }
  1460. template<class U>
  1461. void priv_push_front(BOOST_FWD_REF(U) x)
  1462. { this->icont().push_front(*this->create_node(::boost::forward<U>(x))); }
  1463. bool priv_try_shrink(size_type new_size, const_iterator &last_pos)
  1464. {
  1465. typename Icont::iterator end_n(this->icont().end()), cur(this->icont().before_begin()), cur_next;
  1466. while (++(cur_next = cur) != end_n && new_size > 0){
  1467. --new_size;
  1468. cur = cur_next;
  1469. }
  1470. last_pos = const_iterator(cur);
  1471. if (cur_next != end_n){
  1472. this->erase_after(last_pos, const_iterator(end_n));
  1473. return true;
  1474. }
  1475. else{
  1476. return false;
  1477. }
  1478. }
  1479. template<class U>
  1480. iterator priv_insert(const_iterator p, BOOST_FWD_REF(U) x)
  1481. { return this->insert_after(previous(p), ::boost::forward<U>(x)); }
  1482. template<class U>
  1483. iterator priv_insert_after(const_iterator prev_p, BOOST_FWD_REF(U) x)
  1484. { return iterator(this->icont().insert_after(prev_p.get(), *this->create_node(::boost::forward<U>(x)))); }
  1485. class insertion_functor;
  1486. friend class insertion_functor;
  1487. class insertion_functor
  1488. {
  1489. Icont &icont_;
  1490. typedef typename Icont::iterator iiterator;
  1491. typedef typename Icont::const_iterator iconst_iterator;
  1492. iconst_iterator prev_;
  1493. iiterator ret_;
  1494. public:
  1495. insertion_functor(Icont &icont, typename Icont::const_iterator prev)
  1496. : icont_(icont), prev_(prev), ret_(prev.unconst())
  1497. {}
  1498. void operator()(Node &n)
  1499. {
  1500. prev_ = this->icont_.insert_after(prev_, n);
  1501. if(!ret_)
  1502. ret_ = prev_.unconst();
  1503. }
  1504. iiterator inserted_first() const
  1505. {
  1506. iiterator f(ret_);
  1507. return ++f;
  1508. }
  1509. };
  1510. //Functors for member algorithm defaults
  1511. typedef value_less<value_type> value_less_t;
  1512. typedef value_equal<value_type> value_equal_t;
  1513. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  1514. };
  1515. //! <b>Effects</b>: Erases all elements that compare equal to v from the container c.
  1516. //!
  1517. //! <b>Complexity</b>: Linear.
  1518. template <class T, class A, class U>
  1519. inline typename slist<T, A>::size_type erase(slist<T, A>& c, const U& v)
  1520. {
  1521. typename slist<T, A>::size_type old_size = c.size();
  1522. c.remove_if(equal_to_value<U>(v));
  1523. return old_size - c.size();
  1524. }
  1525. //! <b>Effects</b>: Erases all elements that satisfy the predicate pred from the container c.
  1526. //!
  1527. //! <b>Complexity</b>: Linear.
  1528. template <class T, class A, class Pred>
  1529. inline typename slist<T, A>::size_type erase_if(slist<T, A>& c, Pred pred)
  1530. {
  1531. typename slist<T, A>::size_type old_size = c.size();
  1532. c.remove_if(pred);
  1533. return old_size - c.size();
  1534. }
  1535. #ifndef BOOST_CONTAINER_NO_CXX17_CTAD
  1536. template <typename InpIt>
  1537. slist(InpIt, InpIt) ->
  1538. slist<typename iterator_traits<InpIt>::value_type>;
  1539. template <typename InpIt, typename Allocator>
  1540. slist(InpIt, InpIt, Allocator const&) ->
  1541. slist<typename iterator_traits<InpIt>::value_type, Allocator>;
  1542. #endif
  1543. }}
  1544. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  1545. namespace boost {
  1546. //!has_trivial_destructor_after_move<> == true_type
  1547. //!specialization for optimizations
  1548. template <class T, class Allocator>
  1549. struct has_trivial_destructor_after_move<boost::container::slist<T, Allocator> >
  1550. {
  1551. typedef typename boost::container::slist<T, Allocator>::allocator_type allocator_type;
  1552. typedef typename ::boost::container::allocator_traits<allocator_type>::pointer pointer;
  1553. BOOST_STATIC_CONSTEXPR bool value = ::boost::has_trivial_destructor_after_move<allocator_type>::value &&
  1554. ::boost::has_trivial_destructor_after_move<pointer>::value;
  1555. };
  1556. namespace container {
  1557. }} //namespace boost{ namespace container {
  1558. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  1559. // Specialization of insert_iterator so that insertions will be constant
  1560. // time rather than linear time.
  1561. #include <boost/move/detail/std_ns_begin.hpp>
  1562. BOOST_CONTAINER_DOC1ST(namespace std {, BOOST_MOVE_STD_NS_BEG)
  1563. //! A specialization of insert_iterator
  1564. //! that works with slist
  1565. template <class T, class ValueAllocator>
  1566. class insert_iterator<boost::container::slist<T, ValueAllocator> >
  1567. {
  1568. private:
  1569. typedef boost::container::slist<T, ValueAllocator> Container;
  1570. Container* container;
  1571. typename Container::iterator iter;
  1572. public:
  1573. typedef Container container_type;
  1574. typedef output_iterator_tag iterator_category;
  1575. typedef void value_type;
  1576. typedef void difference_type;
  1577. typedef void pointer;
  1578. typedef void reference;
  1579. insert_iterator(Container& x,
  1580. typename Container::iterator i,
  1581. bool is_previous = false)
  1582. : container(&x), iter(is_previous ? i : x.previous(i)){ }
  1583. insert_iterator<Container>&
  1584. operator=(const typename Container::value_type& value)
  1585. {
  1586. iter = container->insert_after(iter, value);
  1587. return *this;
  1588. }
  1589. insert_iterator<Container>& operator*(){ return *this; }
  1590. insert_iterator<Container>& operator++(){ return *this; }
  1591. insert_iterator<Container>& operator++(int){ return *this; }
  1592. };
  1593. BOOST_CONTAINER_DOC1ST( }, BOOST_MOVE_STD_NS_END)
  1594. #include <boost/move/detail/std_ns_end.hpp>
  1595. #include <boost/container/detail/config_end.hpp>
  1596. #endif // BOOST_CONTAINER_SLIST_HPP