stl_list.h 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262
  1. // List implementation -*- C++ -*-
  2. // Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. // Copyright The GNU Toolchain Authors.
  4. //
  5. // This file is part of the GNU ISO C++ Library. This library is free
  6. // software; you can redistribute it and/or modify it under the
  7. // terms of the GNU General Public License as published by the
  8. // Free Software Foundation; either version 3, or (at your option)
  9. // any later version.
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. // Under Section 7 of GPL version 3, you are granted additional
  15. // permissions described in the GCC Runtime Library Exception, version
  16. // 3.1, as published by the Free Software Foundation.
  17. // You should have received a copy of the GNU General Public License and
  18. // a copy of the GCC Runtime Library Exception along with this program;
  19. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. // <http://www.gnu.org/licenses/>.
  21. /*
  22. *
  23. * Copyright (c) 1994
  24. * Hewlett-Packard Company
  25. *
  26. * Permission to use, copy, modify, distribute and sell this software
  27. * and its documentation for any purpose is hereby granted without fee,
  28. * provided that the above copyright notice appear in all copies and
  29. * that both that copyright notice and this permission notice appear
  30. * in supporting documentation. Hewlett-Packard Company makes no
  31. * representations about the suitability of this software for any
  32. * purpose. It is provided "as is" without express or implied warranty.
  33. *
  34. *
  35. * Copyright (c) 1996,1997
  36. * Silicon Graphics Computer Systems, Inc.
  37. *
  38. * Permission to use, copy, modify, distribute and sell this software
  39. * and its documentation for any purpose is hereby granted without fee,
  40. * provided that the above copyright notice appear in all copies and
  41. * that both that copyright notice and this permission notice appear
  42. * in supporting documentation. Silicon Graphics makes no
  43. * representations about the suitability of this software for any
  44. * purpose. It is provided "as is" without express or implied warranty.
  45. */
  46. /** @file bits/stl_list.h
  47. * This is an internal header file, included by other library headers.
  48. * Do not attempt to use it directly. @headername{list}
  49. */
  50. #ifndef _STL_LIST_H
  51. #define _STL_LIST_H 1
  52. #include <bits/concept_check.h>
  53. #include <ext/alloc_traits.h>
  54. #if __cplusplus >= 201103L
  55. #include <initializer_list>
  56. #include <bits/allocated_ptr.h>
  57. #include <ext/aligned_buffer.h>
  58. #endif
  59. namespace std _GLIBCXX_VISIBILITY(default)
  60. {
  61. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  62. namespace __detail
  63. {
  64. // Supporting structures are split into common and templated
  65. // types; the latter publicly inherits from the former in an
  66. // effort to reduce code duplication. This results in some
  67. // "needless" static_cast'ing later on, but it's all safe
  68. // downcasting.
  69. /// Common part of a node in the %list.
  70. struct _List_node_base
  71. {
  72. _List_node_base* _M_next;
  73. _List_node_base* _M_prev;
  74. static void
  75. swap(_List_node_base& __x, _List_node_base& __y) _GLIBCXX_USE_NOEXCEPT;
  76. void
  77. _M_transfer(_List_node_base* const __first,
  78. _List_node_base* const __last) _GLIBCXX_USE_NOEXCEPT;
  79. void
  80. _M_reverse() _GLIBCXX_USE_NOEXCEPT;
  81. void
  82. _M_hook(_List_node_base* const __position) _GLIBCXX_USE_NOEXCEPT;
  83. void
  84. _M_unhook() _GLIBCXX_USE_NOEXCEPT;
  85. };
  86. /// The %list node header.
  87. struct _List_node_header : public _List_node_base
  88. {
  89. #if _GLIBCXX_USE_CXX11_ABI
  90. std::size_t _M_size;
  91. #endif
  92. _List_node_header() _GLIBCXX_NOEXCEPT
  93. { _M_init(); }
  94. #if __cplusplus >= 201103L
  95. _List_node_header(_List_node_header&& __x) noexcept
  96. : _List_node_base{ __x._M_next, __x._M_prev }
  97. # if _GLIBCXX_USE_CXX11_ABI
  98. , _M_size(__x._M_size)
  99. # endif
  100. {
  101. if (__x._M_base()->_M_next == __x._M_base())
  102. this->_M_next = this->_M_prev = this;
  103. else
  104. {
  105. this->_M_next->_M_prev = this->_M_prev->_M_next = this->_M_base();
  106. __x._M_init();
  107. }
  108. }
  109. void
  110. _M_move_nodes(_List_node_header&& __x)
  111. {
  112. _List_node_base* const __xnode = __x._M_base();
  113. if (__xnode->_M_next == __xnode)
  114. _M_init();
  115. else
  116. {
  117. _List_node_base* const __node = this->_M_base();
  118. __node->_M_next = __xnode->_M_next;
  119. __node->_M_prev = __xnode->_M_prev;
  120. __node->_M_next->_M_prev = __node->_M_prev->_M_next = __node;
  121. # if _GLIBCXX_USE_CXX11_ABI
  122. _M_size = __x._M_size;
  123. # endif
  124. __x._M_init();
  125. }
  126. }
  127. #endif
  128. void
  129. _M_init() _GLIBCXX_NOEXCEPT
  130. {
  131. this->_M_next = this->_M_prev = this;
  132. #if _GLIBCXX_USE_CXX11_ABI
  133. this->_M_size = 0;
  134. #endif
  135. }
  136. private:
  137. _List_node_base* _M_base() { return this; }
  138. };
  139. // Used by list::sort to hold nodes being sorted.
  140. struct _Scratch_list : _List_node_base
  141. {
  142. _Scratch_list() { _M_next = _M_prev = this; }
  143. bool empty() const { return _M_next == this; }
  144. void swap(_List_node_base& __l) { _List_node_base::swap(*this, __l); }
  145. template<typename _Iter, typename _Cmp>
  146. struct _Ptr_cmp
  147. {
  148. _Cmp _M_cmp;
  149. bool
  150. operator()(__detail::_List_node_base* __lhs,
  151. __detail::_List_node_base* __rhs) /* not const */
  152. { return _M_cmp(*_Iter(__lhs), *_Iter(__rhs)); }
  153. };
  154. template<typename _Iter>
  155. struct _Ptr_cmp<_Iter, void>
  156. {
  157. bool
  158. operator()(__detail::_List_node_base* __lhs,
  159. __detail::_List_node_base* __rhs) const
  160. { return *_Iter(__lhs) < *_Iter(__rhs); }
  161. };
  162. // Merge nodes from __x into *this. Both lists must be sorted wrt _Cmp.
  163. template<typename _Cmp>
  164. void
  165. merge(_List_node_base& __x, _Cmp __comp)
  166. {
  167. _List_node_base* __first1 = _M_next;
  168. _List_node_base* const __last1 = this;
  169. _List_node_base* __first2 = __x._M_next;
  170. _List_node_base* const __last2 = std::__addressof(__x);
  171. while (__first1 != __last1 && __first2 != __last2)
  172. {
  173. if (__comp(__first2, __first1))
  174. {
  175. _List_node_base* __next = __first2->_M_next;
  176. __first1->_M_transfer(__first2, __next);
  177. __first2 = __next;
  178. }
  179. else
  180. __first1 = __first1->_M_next;
  181. }
  182. if (__first2 != __last2)
  183. this->_M_transfer(__first2, __last2);
  184. }
  185. // Splice the node at __i into *this.
  186. void _M_take_one(_List_node_base* __i)
  187. { this->_M_transfer(__i, __i->_M_next); }
  188. // Splice all nodes from *this after __i.
  189. void _M_put_all(_List_node_base* __i)
  190. {
  191. if (!empty())
  192. __i->_M_transfer(_M_next, this);
  193. }
  194. };
  195. } // namespace detail
  196. _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
  197. /// An actual node in the %list.
  198. template<typename _Tp>
  199. struct _List_node : public __detail::_List_node_base
  200. {
  201. #if __cplusplus >= 201103L
  202. __gnu_cxx::__aligned_membuf<_Tp> _M_storage;
  203. _Tp* _M_valptr() { return _M_storage._M_ptr(); }
  204. _Tp const* _M_valptr() const { return _M_storage._M_ptr(); }
  205. #else
  206. _Tp _M_data;
  207. _Tp* _M_valptr() { return std::__addressof(_M_data); }
  208. _Tp const* _M_valptr() const { return std::__addressof(_M_data); }
  209. #endif
  210. };
  211. /**
  212. * @brief A list::iterator.
  213. *
  214. * All the functions are op overloads.
  215. */
  216. template<typename _Tp>
  217. struct _List_iterator
  218. {
  219. typedef _List_iterator<_Tp> _Self;
  220. typedef _List_node<_Tp> _Node;
  221. typedef ptrdiff_t difference_type;
  222. typedef std::bidirectional_iterator_tag iterator_category;
  223. typedef _Tp value_type;
  224. typedef _Tp* pointer;
  225. typedef _Tp& reference;
  226. _List_iterator() _GLIBCXX_NOEXCEPT
  227. : _M_node() { }
  228. explicit
  229. _List_iterator(__detail::_List_node_base* __x) _GLIBCXX_NOEXCEPT
  230. : _M_node(__x) { }
  231. _Self
  232. _M_const_cast() const _GLIBCXX_NOEXCEPT
  233. { return *this; }
  234. // Must downcast from _List_node_base to _List_node to get to value.
  235. _GLIBCXX_NODISCARD
  236. reference
  237. operator*() const _GLIBCXX_NOEXCEPT
  238. { return *static_cast<_Node*>(_M_node)->_M_valptr(); }
  239. _GLIBCXX_NODISCARD
  240. pointer
  241. operator->() const _GLIBCXX_NOEXCEPT
  242. { return static_cast<_Node*>(_M_node)->_M_valptr(); }
  243. _Self&
  244. operator++() _GLIBCXX_NOEXCEPT
  245. {
  246. _M_node = _M_node->_M_next;
  247. return *this;
  248. }
  249. _Self
  250. operator++(int) _GLIBCXX_NOEXCEPT
  251. {
  252. _Self __tmp = *this;
  253. _M_node = _M_node->_M_next;
  254. return __tmp;
  255. }
  256. _Self&
  257. operator--() _GLIBCXX_NOEXCEPT
  258. {
  259. _M_node = _M_node->_M_prev;
  260. return *this;
  261. }
  262. _Self
  263. operator--(int) _GLIBCXX_NOEXCEPT
  264. {
  265. _Self __tmp = *this;
  266. _M_node = _M_node->_M_prev;
  267. return __tmp;
  268. }
  269. _GLIBCXX_NODISCARD
  270. friend bool
  271. operator==(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
  272. { return __x._M_node == __y._M_node; }
  273. #if __cpp_impl_three_way_comparison < 201907L
  274. _GLIBCXX_NODISCARD
  275. friend bool
  276. operator!=(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
  277. { return __x._M_node != __y._M_node; }
  278. #endif
  279. // The only member points to the %list element.
  280. __detail::_List_node_base* _M_node;
  281. };
  282. /**
  283. * @brief A list::const_iterator.
  284. *
  285. * All the functions are op overloads.
  286. */
  287. template<typename _Tp>
  288. struct _List_const_iterator
  289. {
  290. typedef _List_const_iterator<_Tp> _Self;
  291. typedef const _List_node<_Tp> _Node;
  292. typedef _List_iterator<_Tp> iterator;
  293. typedef ptrdiff_t difference_type;
  294. typedef std::bidirectional_iterator_tag iterator_category;
  295. typedef _Tp value_type;
  296. typedef const _Tp* pointer;
  297. typedef const _Tp& reference;
  298. _List_const_iterator() _GLIBCXX_NOEXCEPT
  299. : _M_node() { }
  300. explicit
  301. _List_const_iterator(const __detail::_List_node_base* __x)
  302. _GLIBCXX_NOEXCEPT
  303. : _M_node(__x) { }
  304. _List_const_iterator(const iterator& __x) _GLIBCXX_NOEXCEPT
  305. : _M_node(__x._M_node) { }
  306. iterator
  307. _M_const_cast() const _GLIBCXX_NOEXCEPT
  308. { return iterator(const_cast<__detail::_List_node_base*>(_M_node)); }
  309. // Must downcast from List_node_base to _List_node to get to value.
  310. _GLIBCXX_NODISCARD
  311. reference
  312. operator*() const _GLIBCXX_NOEXCEPT
  313. { return *static_cast<_Node*>(_M_node)->_M_valptr(); }
  314. _GLIBCXX_NODISCARD
  315. pointer
  316. operator->() const _GLIBCXX_NOEXCEPT
  317. { return static_cast<_Node*>(_M_node)->_M_valptr(); }
  318. _Self&
  319. operator++() _GLIBCXX_NOEXCEPT
  320. {
  321. _M_node = _M_node->_M_next;
  322. return *this;
  323. }
  324. _Self
  325. operator++(int) _GLIBCXX_NOEXCEPT
  326. {
  327. _Self __tmp = *this;
  328. _M_node = _M_node->_M_next;
  329. return __tmp;
  330. }
  331. _Self&
  332. operator--() _GLIBCXX_NOEXCEPT
  333. {
  334. _M_node = _M_node->_M_prev;
  335. return *this;
  336. }
  337. _Self
  338. operator--(int) _GLIBCXX_NOEXCEPT
  339. {
  340. _Self __tmp = *this;
  341. _M_node = _M_node->_M_prev;
  342. return __tmp;
  343. }
  344. _GLIBCXX_NODISCARD
  345. friend bool
  346. operator==(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
  347. { return __x._M_node == __y._M_node; }
  348. #if __cpp_impl_three_way_comparison < 201907L
  349. _GLIBCXX_NODISCARD
  350. friend bool
  351. operator!=(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
  352. { return __x._M_node != __y._M_node; }
  353. #endif
  354. // The only member points to the %list element.
  355. const __detail::_List_node_base* _M_node;
  356. };
  357. _GLIBCXX_BEGIN_NAMESPACE_CXX11
  358. /// See bits/stl_deque.h's _Deque_base for an explanation.
  359. template<typename _Tp, typename _Alloc>
  360. class _List_base
  361. {
  362. protected:
  363. typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
  364. rebind<_Tp>::other _Tp_alloc_type;
  365. typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Tp_alloc_traits;
  366. typedef typename _Tp_alloc_traits::template
  367. rebind<_List_node<_Tp> >::other _Node_alloc_type;
  368. typedef __gnu_cxx::__alloc_traits<_Node_alloc_type> _Node_alloc_traits;
  369. #if !_GLIBCXX_INLINE_VERSION
  370. static size_t
  371. _S_distance(const __detail::_List_node_base* __first,
  372. const __detail::_List_node_base* __last)
  373. {
  374. size_t __n = 0;
  375. while (__first != __last)
  376. {
  377. __first = __first->_M_next;
  378. ++__n;
  379. }
  380. return __n;
  381. }
  382. #endif
  383. struct _List_impl
  384. : public _Node_alloc_type
  385. {
  386. __detail::_List_node_header _M_node;
  387. _List_impl() _GLIBCXX_NOEXCEPT_IF(
  388. is_nothrow_default_constructible<_Node_alloc_type>::value)
  389. : _Node_alloc_type()
  390. { }
  391. _List_impl(const _Node_alloc_type& __a) _GLIBCXX_NOEXCEPT
  392. : _Node_alloc_type(__a)
  393. { }
  394. #if __cplusplus >= 201103L
  395. _List_impl(_List_impl&&) = default;
  396. _List_impl(_Node_alloc_type&& __a, _List_impl&& __x)
  397. : _Node_alloc_type(std::move(__a)), _M_node(std::move(__x._M_node))
  398. { }
  399. _List_impl(_Node_alloc_type&& __a) noexcept
  400. : _Node_alloc_type(std::move(__a))
  401. { }
  402. #endif
  403. };
  404. _List_impl _M_impl;
  405. #if _GLIBCXX_USE_CXX11_ABI
  406. size_t _M_get_size() const { return _M_impl._M_node._M_size; }
  407. void _M_set_size(size_t __n) { _M_impl._M_node._M_size = __n; }
  408. void _M_inc_size(size_t __n) { _M_impl._M_node._M_size += __n; }
  409. void _M_dec_size(size_t __n) { _M_impl._M_node._M_size -= __n; }
  410. # if !_GLIBCXX_INLINE_VERSION
  411. size_t
  412. _M_distance(const __detail::_List_node_base* __first,
  413. const __detail::_List_node_base* __last) const
  414. { return _S_distance(__first, __last); }
  415. // return the stored size
  416. size_t _M_node_count() const { return _M_get_size(); }
  417. # endif
  418. #else
  419. // dummy implementations used when the size is not stored
  420. size_t _M_get_size() const { return 0; }
  421. void _M_set_size(size_t) { }
  422. void _M_inc_size(size_t) { }
  423. void _M_dec_size(size_t) { }
  424. # if !_GLIBCXX_INLINE_VERSION
  425. size_t _M_distance(const void*, const void*) const { return 0; }
  426. // count the number of nodes
  427. size_t _M_node_count() const
  428. {
  429. return _S_distance(_M_impl._M_node._M_next,
  430. std::__addressof(_M_impl._M_node));
  431. }
  432. # endif
  433. #endif
  434. typename _Node_alloc_traits::pointer
  435. _M_get_node()
  436. { return _Node_alloc_traits::allocate(_M_impl, 1); }
  437. void
  438. _M_put_node(typename _Node_alloc_traits::pointer __p) _GLIBCXX_NOEXCEPT
  439. { _Node_alloc_traits::deallocate(_M_impl, __p, 1); }
  440. public:
  441. typedef _Alloc allocator_type;
  442. _Node_alloc_type&
  443. _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
  444. { return _M_impl; }
  445. const _Node_alloc_type&
  446. _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
  447. { return _M_impl; }
  448. #if __cplusplus >= 201103L
  449. _List_base() = default;
  450. #else
  451. _List_base() { }
  452. #endif
  453. _List_base(const _Node_alloc_type& __a) _GLIBCXX_NOEXCEPT
  454. : _M_impl(__a)
  455. { }
  456. #if __cplusplus >= 201103L
  457. _List_base(_List_base&&) = default;
  458. # if !_GLIBCXX_INLINE_VERSION
  459. _List_base(_List_base&& __x, _Node_alloc_type&& __a)
  460. : _M_impl(std::move(__a))
  461. {
  462. if (__x._M_get_Node_allocator() == _M_get_Node_allocator())
  463. _M_move_nodes(std::move(__x));
  464. // else caller must move individual elements.
  465. }
  466. # endif
  467. // Used when allocator is_always_equal.
  468. _List_base(_Node_alloc_type&& __a, _List_base&& __x)
  469. : _M_impl(std::move(__a), std::move(__x._M_impl))
  470. { }
  471. // Used when allocator !is_always_equal.
  472. _List_base(_Node_alloc_type&& __a)
  473. : _M_impl(std::move(__a))
  474. { }
  475. void
  476. _M_move_nodes(_List_base&& __x)
  477. { _M_impl._M_node._M_move_nodes(std::move(__x._M_impl._M_node)); }
  478. #endif
  479. // This is what actually destroys the list.
  480. ~_List_base() _GLIBCXX_NOEXCEPT
  481. { _M_clear(); }
  482. void
  483. _M_clear() _GLIBCXX_NOEXCEPT;
  484. void
  485. _M_init() _GLIBCXX_NOEXCEPT
  486. { this->_M_impl._M_node._M_init(); }
  487. };
  488. /**
  489. * @brief A standard container with linear time access to elements,
  490. * and fixed time insertion/deletion at any point in the sequence.
  491. *
  492. * @ingroup sequences
  493. *
  494. * @tparam _Tp Type of element.
  495. * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
  496. *
  497. * Meets the requirements of a <a href="tables.html#65">container</a>, a
  498. * <a href="tables.html#66">reversible container</a>, and a
  499. * <a href="tables.html#67">sequence</a>, including the
  500. * <a href="tables.html#68">optional sequence requirements</a> with the
  501. * %exception of @c at and @c operator[].
  502. *
  503. * This is a @e doubly @e linked %list. Traversal up and down the
  504. * %list requires linear time, but adding and removing elements (or
  505. * @e nodes) is done in constant time, regardless of where the
  506. * change takes place. Unlike std::vector and std::deque,
  507. * random-access iterators are not provided, so subscripting ( @c
  508. * [] ) access is not allowed. For algorithms which only need
  509. * sequential access, this lack makes no difference.
  510. *
  511. * Also unlike the other standard containers, std::list provides
  512. * specialized algorithms %unique to linked lists, such as
  513. * splicing, sorting, and in-place reversal.
  514. *
  515. * A couple points on memory allocation for list<Tp>:
  516. *
  517. * First, we never actually allocate a Tp, we allocate
  518. * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
  519. * that after elements from %list<X,Alloc1> are spliced into
  520. * %list<X,Alloc2>, destroying the memory of the second %list is a
  521. * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
  522. *
  523. * Second, a %list conceptually represented as
  524. * @code
  525. * A <---> B <---> C <---> D
  526. * @endcode
  527. * is actually circular; a link exists between A and D. The %list
  528. * class holds (as its only data member) a private list::iterator
  529. * pointing to @e D, not to @e A! To get to the head of the %list,
  530. * we start at the tail and move forward by one. When this member
  531. * iterator's next/previous pointers refer to itself, the %list is
  532. * %empty.
  533. */
  534. template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
  535. class list : protected _List_base<_Tp, _Alloc>
  536. {
  537. #ifdef _GLIBCXX_CONCEPT_CHECKS
  538. // concept requirements
  539. typedef typename _Alloc::value_type _Alloc_value_type;
  540. # if __cplusplus < 201103L
  541. __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
  542. # endif
  543. __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
  544. #endif
  545. #if __cplusplus >= 201103L
  546. static_assert(is_same<typename remove_cv<_Tp>::type, _Tp>::value,
  547. "std::list must have a non-const, non-volatile value_type");
  548. # if __cplusplus > 201703L || defined __STRICT_ANSI__
  549. static_assert(is_same<typename _Alloc::value_type, _Tp>::value,
  550. "std::list must have the same value_type as its allocator");
  551. # endif
  552. #endif
  553. typedef _List_base<_Tp, _Alloc> _Base;
  554. typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
  555. typedef typename _Base::_Tp_alloc_traits _Tp_alloc_traits;
  556. typedef typename _Base::_Node_alloc_type _Node_alloc_type;
  557. typedef typename _Base::_Node_alloc_traits _Node_alloc_traits;
  558. public:
  559. typedef _Tp value_type;
  560. typedef typename _Tp_alloc_traits::pointer pointer;
  561. typedef typename _Tp_alloc_traits::const_pointer const_pointer;
  562. typedef typename _Tp_alloc_traits::reference reference;
  563. typedef typename _Tp_alloc_traits::const_reference const_reference;
  564. typedef _List_iterator<_Tp> iterator;
  565. typedef _List_const_iterator<_Tp> const_iterator;
  566. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  567. typedef std::reverse_iterator<iterator> reverse_iterator;
  568. typedef size_t size_type;
  569. typedef ptrdiff_t difference_type;
  570. typedef _Alloc allocator_type;
  571. protected:
  572. // Note that pointers-to-_Node's can be ctor-converted to
  573. // iterator types.
  574. typedef _List_node<_Tp> _Node;
  575. using _Base::_M_impl;
  576. using _Base::_M_put_node;
  577. using _Base::_M_get_node;
  578. using _Base::_M_get_Node_allocator;
  579. /**
  580. * @param __args An instance of user data.
  581. *
  582. * Allocates space for a new node and constructs a copy of
  583. * @a __args in it.
  584. */
  585. #if __cplusplus < 201103L
  586. _Node*
  587. _M_create_node(const value_type& __x)
  588. {
  589. _Node* __p = this->_M_get_node();
  590. __try
  591. {
  592. _Tp_alloc_type __alloc(_M_get_Node_allocator());
  593. __alloc.construct(__p->_M_valptr(), __x);
  594. }
  595. __catch(...)
  596. {
  597. _M_put_node(__p);
  598. __throw_exception_again;
  599. }
  600. return __p;
  601. }
  602. #else
  603. template<typename... _Args>
  604. _Node*
  605. _M_create_node(_Args&&... __args)
  606. {
  607. auto __p = this->_M_get_node();
  608. auto& __alloc = _M_get_Node_allocator();
  609. __allocated_ptr<_Node_alloc_type> __guard{__alloc, __p};
  610. _Node_alloc_traits::construct(__alloc, __p->_M_valptr(),
  611. std::forward<_Args>(__args)...);
  612. __guard = nullptr;
  613. return __p;
  614. }
  615. #endif
  616. #if _GLIBCXX_USE_CXX11_ABI
  617. static size_t
  618. _S_distance(const_iterator __first, const_iterator __last)
  619. { return std::distance(__first, __last); }
  620. // return the stored size
  621. size_t
  622. _M_node_count() const
  623. { return this->_M_get_size(); }
  624. #else
  625. // dummy implementations used when the size is not stored
  626. static size_t
  627. _S_distance(const_iterator, const_iterator)
  628. { return 0; }
  629. // count the number of nodes
  630. size_t
  631. _M_node_count() const
  632. { return std::distance(begin(), end()); }
  633. #endif
  634. public:
  635. // [23.2.2.1] construct/copy/destroy
  636. // (assign() and get_allocator() are also listed in this section)
  637. /**
  638. * @brief Creates a %list with no elements.
  639. */
  640. #if __cplusplus >= 201103L
  641. list() = default;
  642. #else
  643. list() { }
  644. #endif
  645. /**
  646. * @brief Creates a %list with no elements.
  647. * @param __a An allocator object.
  648. */
  649. explicit
  650. list(const allocator_type& __a) _GLIBCXX_NOEXCEPT
  651. : _Base(_Node_alloc_type(__a)) { }
  652. #if __cplusplus >= 201103L
  653. /**
  654. * @brief Creates a %list with default constructed elements.
  655. * @param __n The number of elements to initially create.
  656. * @param __a An allocator object.
  657. *
  658. * This constructor fills the %list with @a __n default
  659. * constructed elements.
  660. */
  661. explicit
  662. list(size_type __n, const allocator_type& __a = allocator_type())
  663. : _Base(_Node_alloc_type(__a))
  664. { _M_default_initialize(__n); }
  665. /**
  666. * @brief Creates a %list with copies of an exemplar element.
  667. * @param __n The number of elements to initially create.
  668. * @param __value An element to copy.
  669. * @param __a An allocator object.
  670. *
  671. * This constructor fills the %list with @a __n copies of @a __value.
  672. */
  673. list(size_type __n, const value_type& __value,
  674. const allocator_type& __a = allocator_type())
  675. : _Base(_Node_alloc_type(__a))
  676. { _M_fill_initialize(__n, __value); }
  677. #else
  678. /**
  679. * @brief Creates a %list with copies of an exemplar element.
  680. * @param __n The number of elements to initially create.
  681. * @param __value An element to copy.
  682. * @param __a An allocator object.
  683. *
  684. * This constructor fills the %list with @a __n copies of @a __value.
  685. */
  686. explicit
  687. list(size_type __n, const value_type& __value = value_type(),
  688. const allocator_type& __a = allocator_type())
  689. : _Base(_Node_alloc_type(__a))
  690. { _M_fill_initialize(__n, __value); }
  691. #endif
  692. /**
  693. * @brief %List copy constructor.
  694. * @param __x A %list of identical element and allocator types.
  695. *
  696. * The newly-created %list uses a copy of the allocation object used
  697. * by @a __x (unless the allocator traits dictate a different object).
  698. */
  699. list(const list& __x)
  700. : _Base(_Node_alloc_traits::
  701. _S_select_on_copy(__x._M_get_Node_allocator()))
  702. { _M_initialize_dispatch(__x.begin(), __x.end(), __false_type()); }
  703. #if __cplusplus >= 201103L
  704. /**
  705. * @brief %List move constructor.
  706. *
  707. * The newly-created %list contains the exact contents of the moved
  708. * instance. The contents of the moved instance are a valid, but
  709. * unspecified %list.
  710. */
  711. list(list&&) = default;
  712. /**
  713. * @brief Builds a %list from an initializer_list
  714. * @param __l An initializer_list of value_type.
  715. * @param __a An allocator object.
  716. *
  717. * Create a %list consisting of copies of the elements in the
  718. * initializer_list @a __l. This is linear in __l.size().
  719. */
  720. list(initializer_list<value_type> __l,
  721. const allocator_type& __a = allocator_type())
  722. : _Base(_Node_alloc_type(__a))
  723. { _M_initialize_dispatch(__l.begin(), __l.end(), __false_type()); }
  724. list(const list& __x, const __type_identity_t<allocator_type>& __a)
  725. : _Base(_Node_alloc_type(__a))
  726. { _M_initialize_dispatch(__x.begin(), __x.end(), __false_type()); }
  727. private:
  728. list(list&& __x, const allocator_type& __a, true_type) noexcept
  729. : _Base(_Node_alloc_type(__a), std::move(__x))
  730. { }
  731. list(list&& __x, const allocator_type& __a, false_type)
  732. : _Base(_Node_alloc_type(__a))
  733. {
  734. if (__x._M_get_Node_allocator() == this->_M_get_Node_allocator())
  735. this->_M_move_nodes(std::move(__x));
  736. else
  737. insert(begin(), std::__make_move_if_noexcept_iterator(__x.begin()),
  738. std::__make_move_if_noexcept_iterator(__x.end()));
  739. }
  740. public:
  741. list(list&& __x, const __type_identity_t<allocator_type>& __a)
  742. noexcept(_Node_alloc_traits::_S_always_equal())
  743. : list(std::move(__x), __a,
  744. typename _Node_alloc_traits::is_always_equal{})
  745. { }
  746. #endif
  747. /**
  748. * @brief Builds a %list from a range.
  749. * @param __first An input iterator.
  750. * @param __last An input iterator.
  751. * @param __a An allocator object.
  752. *
  753. * Create a %list consisting of copies of the elements from
  754. * [@a __first,@a __last). This is linear in N (where N is
  755. * distance(@a __first,@a __last)).
  756. */
  757. #if __cplusplus >= 201103L
  758. template<typename _InputIterator,
  759. typename = std::_RequireInputIter<_InputIterator>>
  760. list(_InputIterator __first, _InputIterator __last,
  761. const allocator_type& __a = allocator_type())
  762. : _Base(_Node_alloc_type(__a))
  763. { _M_initialize_dispatch(__first, __last, __false_type()); }
  764. #else
  765. template<typename _InputIterator>
  766. list(_InputIterator __first, _InputIterator __last,
  767. const allocator_type& __a = allocator_type())
  768. : _Base(_Node_alloc_type(__a))
  769. {
  770. // Check whether it's an integral type. If so, it's not an iterator.
  771. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  772. _M_initialize_dispatch(__first, __last, _Integral());
  773. }
  774. #endif
  775. #if __cplusplus >= 201103L
  776. /**
  777. * No explicit dtor needed as the _Base dtor takes care of
  778. * things. The _Base dtor only erases the elements, and note
  779. * that if the elements themselves are pointers, the pointed-to
  780. * memory is not touched in any way. Managing the pointer is
  781. * the user's responsibility.
  782. */
  783. ~list() = default;
  784. #endif
  785. /**
  786. * @brief %List assignment operator.
  787. * @param __x A %list of identical element and allocator types.
  788. *
  789. * All the elements of @a __x are copied.
  790. *
  791. * Whether the allocator is copied depends on the allocator traits.
  792. */
  793. list&
  794. operator=(const list& __x);
  795. #if __cplusplus >= 201103L
  796. /**
  797. * @brief %List move assignment operator.
  798. * @param __x A %list of identical element and allocator types.
  799. *
  800. * The contents of @a __x are moved into this %list (without copying).
  801. *
  802. * Afterwards @a __x is a valid, but unspecified %list
  803. *
  804. * Whether the allocator is moved depends on the allocator traits.
  805. */
  806. list&
  807. operator=(list&& __x)
  808. noexcept(_Node_alloc_traits::_S_nothrow_move())
  809. {
  810. constexpr bool __move_storage =
  811. _Node_alloc_traits::_S_propagate_on_move_assign()
  812. || _Node_alloc_traits::_S_always_equal();
  813. _M_move_assign(std::move(__x), __bool_constant<__move_storage>());
  814. return *this;
  815. }
  816. /**
  817. * @brief %List initializer list assignment operator.
  818. * @param __l An initializer_list of value_type.
  819. *
  820. * Replace the contents of the %list with copies of the elements
  821. * in the initializer_list @a __l. This is linear in l.size().
  822. */
  823. list&
  824. operator=(initializer_list<value_type> __l)
  825. {
  826. this->assign(__l.begin(), __l.end());
  827. return *this;
  828. }
  829. #endif
  830. /**
  831. * @brief Assigns a given value to a %list.
  832. * @param __n Number of elements to be assigned.
  833. * @param __val Value to be assigned.
  834. *
  835. * This function fills a %list with @a __n copies of the given
  836. * value. Note that the assignment completely changes the %list
  837. * and that the resulting %list's size is the same as the number
  838. * of elements assigned.
  839. */
  840. void
  841. assign(size_type __n, const value_type& __val)
  842. { _M_fill_assign(__n, __val); }
  843. /**
  844. * @brief Assigns a range to a %list.
  845. * @param __first An input iterator.
  846. * @param __last An input iterator.
  847. *
  848. * This function fills a %list with copies of the elements in the
  849. * range [@a __first,@a __last).
  850. *
  851. * Note that the assignment completely changes the %list and
  852. * that the resulting %list's size is the same as the number of
  853. * elements assigned.
  854. */
  855. #if __cplusplus >= 201103L
  856. template<typename _InputIterator,
  857. typename = std::_RequireInputIter<_InputIterator>>
  858. void
  859. assign(_InputIterator __first, _InputIterator __last)
  860. { _M_assign_dispatch(__first, __last, __false_type()); }
  861. #else
  862. template<typename _InputIterator>
  863. void
  864. assign(_InputIterator __first, _InputIterator __last)
  865. {
  866. // Check whether it's an integral type. If so, it's not an iterator.
  867. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  868. _M_assign_dispatch(__first, __last, _Integral());
  869. }
  870. #endif
  871. #if __cplusplus >= 201103L
  872. /**
  873. * @brief Assigns an initializer_list to a %list.
  874. * @param __l An initializer_list of value_type.
  875. *
  876. * Replace the contents of the %list with copies of the elements
  877. * in the initializer_list @a __l. This is linear in __l.size().
  878. */
  879. void
  880. assign(initializer_list<value_type> __l)
  881. { this->_M_assign_dispatch(__l.begin(), __l.end(), __false_type()); }
  882. #endif
  883. /// Get a copy of the memory allocation object.
  884. allocator_type
  885. get_allocator() const _GLIBCXX_NOEXCEPT
  886. { return allocator_type(_Base::_M_get_Node_allocator()); }
  887. // iterators
  888. /**
  889. * Returns a read/write iterator that points to the first element in the
  890. * %list. Iteration is done in ordinary element order.
  891. */
  892. _GLIBCXX_NODISCARD
  893. iterator
  894. begin() _GLIBCXX_NOEXCEPT
  895. { return iterator(this->_M_impl._M_node._M_next); }
  896. /**
  897. * Returns a read-only (constant) iterator that points to the
  898. * first element in the %list. Iteration is done in ordinary
  899. * element order.
  900. */
  901. _GLIBCXX_NODISCARD
  902. const_iterator
  903. begin() const _GLIBCXX_NOEXCEPT
  904. { return const_iterator(this->_M_impl._M_node._M_next); }
  905. /**
  906. * Returns a read/write iterator that points one past the last
  907. * element in the %list. Iteration is done in ordinary element
  908. * order.
  909. */
  910. _GLIBCXX_NODISCARD
  911. iterator
  912. end() _GLIBCXX_NOEXCEPT
  913. { return iterator(&this->_M_impl._M_node); }
  914. /**
  915. * Returns a read-only (constant) iterator that points one past
  916. * the last element in the %list. Iteration is done in ordinary
  917. * element order.
  918. */
  919. _GLIBCXX_NODISCARD
  920. const_iterator
  921. end() const _GLIBCXX_NOEXCEPT
  922. { return const_iterator(&this->_M_impl._M_node); }
  923. /**
  924. * Returns a read/write reverse iterator that points to the last
  925. * element in the %list. Iteration is done in reverse element
  926. * order.
  927. */
  928. _GLIBCXX_NODISCARD
  929. reverse_iterator
  930. rbegin() _GLIBCXX_NOEXCEPT
  931. { return reverse_iterator(end()); }
  932. /**
  933. * Returns a read-only (constant) reverse iterator that points to
  934. * the last element in the %list. Iteration is done in reverse
  935. * element order.
  936. */
  937. _GLIBCXX_NODISCARD
  938. const_reverse_iterator
  939. rbegin() const _GLIBCXX_NOEXCEPT
  940. { return const_reverse_iterator(end()); }
  941. /**
  942. * Returns a read/write reverse iterator that points to one
  943. * before the first element in the %list. Iteration is done in
  944. * reverse element order.
  945. */
  946. _GLIBCXX_NODISCARD
  947. reverse_iterator
  948. rend() _GLIBCXX_NOEXCEPT
  949. { return reverse_iterator(begin()); }
  950. /**
  951. * Returns a read-only (constant) reverse iterator that points to one
  952. * before the first element in the %list. Iteration is done in reverse
  953. * element order.
  954. */
  955. _GLIBCXX_NODISCARD
  956. const_reverse_iterator
  957. rend() const _GLIBCXX_NOEXCEPT
  958. { return const_reverse_iterator(begin()); }
  959. #if __cplusplus >= 201103L
  960. /**
  961. * Returns a read-only (constant) iterator that points to the
  962. * first element in the %list. Iteration is done in ordinary
  963. * element order.
  964. */
  965. [[__nodiscard__]]
  966. const_iterator
  967. cbegin() const noexcept
  968. { return const_iterator(this->_M_impl._M_node._M_next); }
  969. /**
  970. * Returns a read-only (constant) iterator that points one past
  971. * the last element in the %list. Iteration is done in ordinary
  972. * element order.
  973. */
  974. [[__nodiscard__]]
  975. const_iterator
  976. cend() const noexcept
  977. { return const_iterator(&this->_M_impl._M_node); }
  978. /**
  979. * Returns a read-only (constant) reverse iterator that points to
  980. * the last element in the %list. Iteration is done in reverse
  981. * element order.
  982. */
  983. [[__nodiscard__]]
  984. const_reverse_iterator
  985. crbegin() const noexcept
  986. { return const_reverse_iterator(end()); }
  987. /**
  988. * Returns a read-only (constant) reverse iterator that points to one
  989. * before the first element in the %list. Iteration is done in reverse
  990. * element order.
  991. */
  992. [[__nodiscard__]]
  993. const_reverse_iterator
  994. crend() const noexcept
  995. { return const_reverse_iterator(begin()); }
  996. #endif
  997. // [23.2.2.2] capacity
  998. /**
  999. * Returns true if the %list is empty. (Thus begin() would equal
  1000. * end().)
  1001. */
  1002. _GLIBCXX_NODISCARD bool
  1003. empty() const _GLIBCXX_NOEXCEPT
  1004. { return this->_M_impl._M_node._M_next == &this->_M_impl._M_node; }
  1005. /** Returns the number of elements in the %list. */
  1006. _GLIBCXX_NODISCARD
  1007. size_type
  1008. size() const _GLIBCXX_NOEXCEPT
  1009. { return _M_node_count(); }
  1010. /** Returns the size() of the largest possible %list. */
  1011. _GLIBCXX_NODISCARD
  1012. size_type
  1013. max_size() const _GLIBCXX_NOEXCEPT
  1014. { return _Node_alloc_traits::max_size(_M_get_Node_allocator()); }
  1015. #if __cplusplus >= 201103L
  1016. /**
  1017. * @brief Resizes the %list to the specified number of elements.
  1018. * @param __new_size Number of elements the %list should contain.
  1019. *
  1020. * This function will %resize the %list to the specified number
  1021. * of elements. If the number is smaller than the %list's
  1022. * current size the %list is truncated, otherwise default
  1023. * constructed elements are appended.
  1024. */
  1025. void
  1026. resize(size_type __new_size);
  1027. /**
  1028. * @brief Resizes the %list to the specified number of elements.
  1029. * @param __new_size Number of elements the %list should contain.
  1030. * @param __x Data with which new elements should be populated.
  1031. *
  1032. * This function will %resize the %list to the specified number
  1033. * of elements. If the number is smaller than the %list's
  1034. * current size the %list is truncated, otherwise the %list is
  1035. * extended and new elements are populated with given data.
  1036. */
  1037. void
  1038. resize(size_type __new_size, const value_type& __x);
  1039. #else
  1040. /**
  1041. * @brief Resizes the %list to the specified number of elements.
  1042. * @param __new_size Number of elements the %list should contain.
  1043. * @param __x Data with which new elements should be populated.
  1044. *
  1045. * This function will %resize the %list to the specified number
  1046. * of elements. If the number is smaller than the %list's
  1047. * current size the %list is truncated, otherwise the %list is
  1048. * extended and new elements are populated with given data.
  1049. */
  1050. void
  1051. resize(size_type __new_size, value_type __x = value_type());
  1052. #endif
  1053. // element access
  1054. /**
  1055. * Returns a read/write reference to the data at the first
  1056. * element of the %list.
  1057. */
  1058. _GLIBCXX_NODISCARD
  1059. reference
  1060. front() _GLIBCXX_NOEXCEPT
  1061. { return *begin(); }
  1062. /**
  1063. * Returns a read-only (constant) reference to the data at the first
  1064. * element of the %list.
  1065. */
  1066. _GLIBCXX_NODISCARD
  1067. const_reference
  1068. front() const _GLIBCXX_NOEXCEPT
  1069. { return *begin(); }
  1070. /**
  1071. * Returns a read/write reference to the data at the last element
  1072. * of the %list.
  1073. */
  1074. _GLIBCXX_NODISCARD
  1075. reference
  1076. back() _GLIBCXX_NOEXCEPT
  1077. {
  1078. iterator __tmp = end();
  1079. --__tmp;
  1080. return *__tmp;
  1081. }
  1082. /**
  1083. * Returns a read-only (constant) reference to the data at the last
  1084. * element of the %list.
  1085. */
  1086. _GLIBCXX_NODISCARD
  1087. const_reference
  1088. back() const _GLIBCXX_NOEXCEPT
  1089. {
  1090. const_iterator __tmp = end();
  1091. --__tmp;
  1092. return *__tmp;
  1093. }
  1094. // [23.2.2.3] modifiers
  1095. /**
  1096. * @brief Add data to the front of the %list.
  1097. * @param __x Data to be added.
  1098. *
  1099. * This is a typical stack operation. The function creates an
  1100. * element at the front of the %list and assigns the given data
  1101. * to it. Due to the nature of a %list this operation can be
  1102. * done in constant time, and does not invalidate iterators and
  1103. * references.
  1104. */
  1105. void
  1106. push_front(const value_type& __x)
  1107. { this->_M_insert(begin(), __x); }
  1108. #if __cplusplus >= 201103L
  1109. void
  1110. push_front(value_type&& __x)
  1111. { this->_M_insert(begin(), std::move(__x)); }
  1112. template<typename... _Args>
  1113. #if __cplusplus > 201402L
  1114. reference
  1115. #else
  1116. void
  1117. #endif
  1118. emplace_front(_Args&&... __args)
  1119. {
  1120. this->_M_insert(begin(), std::forward<_Args>(__args)...);
  1121. #if __cplusplus > 201402L
  1122. return front();
  1123. #endif
  1124. }
  1125. #endif
  1126. /**
  1127. * @brief Removes first element.
  1128. *
  1129. * This is a typical stack operation. It shrinks the %list by
  1130. * one. Due to the nature of a %list this operation can be done
  1131. * in constant time, and only invalidates iterators/references to
  1132. * the element being removed.
  1133. *
  1134. * Note that no data is returned, and if the first element's data
  1135. * is needed, it should be retrieved before pop_front() is
  1136. * called.
  1137. */
  1138. void
  1139. pop_front() _GLIBCXX_NOEXCEPT
  1140. { this->_M_erase(begin()); }
  1141. /**
  1142. * @brief Add data to the end of the %list.
  1143. * @param __x Data to be added.
  1144. *
  1145. * This is a typical stack operation. The function creates an
  1146. * element at the end of the %list and assigns the given data to
  1147. * it. Due to the nature of a %list this operation can be done
  1148. * in constant time, and does not invalidate iterators and
  1149. * references.
  1150. */
  1151. void
  1152. push_back(const value_type& __x)
  1153. { this->_M_insert(end(), __x); }
  1154. #if __cplusplus >= 201103L
  1155. void
  1156. push_back(value_type&& __x)
  1157. { this->_M_insert(end(), std::move(__x)); }
  1158. template<typename... _Args>
  1159. #if __cplusplus > 201402L
  1160. reference
  1161. #else
  1162. void
  1163. #endif
  1164. emplace_back(_Args&&... __args)
  1165. {
  1166. this->_M_insert(end(), std::forward<_Args>(__args)...);
  1167. #if __cplusplus > 201402L
  1168. return back();
  1169. #endif
  1170. }
  1171. #endif
  1172. /**
  1173. * @brief Removes last element.
  1174. *
  1175. * This is a typical stack operation. It shrinks the %list by
  1176. * one. Due to the nature of a %list this operation can be done
  1177. * in constant time, and only invalidates iterators/references to
  1178. * the element being removed.
  1179. *
  1180. * Note that no data is returned, and if the last element's data
  1181. * is needed, it should be retrieved before pop_back() is called.
  1182. */
  1183. void
  1184. pop_back() _GLIBCXX_NOEXCEPT
  1185. { this->_M_erase(iterator(this->_M_impl._M_node._M_prev)); }
  1186. #if __cplusplus >= 201103L
  1187. /**
  1188. * @brief Constructs object in %list before specified iterator.
  1189. * @param __position A const_iterator into the %list.
  1190. * @param __args Arguments.
  1191. * @return An iterator that points to the inserted data.
  1192. *
  1193. * This function will insert an object of type T constructed
  1194. * with T(std::forward<Args>(args)...) before the specified
  1195. * location. Due to the nature of a %list this operation can
  1196. * be done in constant time, and does not invalidate iterators
  1197. * and references.
  1198. */
  1199. template<typename... _Args>
  1200. iterator
  1201. emplace(const_iterator __position, _Args&&... __args);
  1202. /**
  1203. * @brief Inserts given value into %list before specified iterator.
  1204. * @param __position A const_iterator into the %list.
  1205. * @param __x Data to be inserted.
  1206. * @return An iterator that points to the inserted data.
  1207. *
  1208. * This function will insert a copy of the given value before
  1209. * the specified location. Due to the nature of a %list this
  1210. * operation can be done in constant time, and does not
  1211. * invalidate iterators and references.
  1212. */
  1213. iterator
  1214. insert(const_iterator __position, const value_type& __x);
  1215. #else
  1216. /**
  1217. * @brief Inserts given value into %list before specified iterator.
  1218. * @param __position An iterator into the %list.
  1219. * @param __x Data to be inserted.
  1220. * @return An iterator that points to the inserted data.
  1221. *
  1222. * This function will insert a copy of the given value before
  1223. * the specified location. Due to the nature of a %list this
  1224. * operation can be done in constant time, and does not
  1225. * invalidate iterators and references.
  1226. */
  1227. iterator
  1228. insert(iterator __position, const value_type& __x);
  1229. #endif
  1230. #if __cplusplus >= 201103L
  1231. /**
  1232. * @brief Inserts given rvalue into %list before specified iterator.
  1233. * @param __position A const_iterator into the %list.
  1234. * @param __x Data to be inserted.
  1235. * @return An iterator that points to the inserted data.
  1236. *
  1237. * This function will insert a copy of the given rvalue before
  1238. * the specified location. Due to the nature of a %list this
  1239. * operation can be done in constant time, and does not
  1240. * invalidate iterators and references.
  1241. */
  1242. iterator
  1243. insert(const_iterator __position, value_type&& __x)
  1244. { return emplace(__position, std::move(__x)); }
  1245. /**
  1246. * @brief Inserts the contents of an initializer_list into %list
  1247. * before specified const_iterator.
  1248. * @param __p A const_iterator into the %list.
  1249. * @param __l An initializer_list of value_type.
  1250. * @return An iterator pointing to the first element inserted
  1251. * (or __position).
  1252. *
  1253. * This function will insert copies of the data in the
  1254. * initializer_list @a l into the %list before the location
  1255. * specified by @a p.
  1256. *
  1257. * This operation is linear in the number of elements inserted and
  1258. * does not invalidate iterators and references.
  1259. */
  1260. iterator
  1261. insert(const_iterator __p, initializer_list<value_type> __l)
  1262. { return this->insert(__p, __l.begin(), __l.end()); }
  1263. #endif
  1264. #if __cplusplus >= 201103L
  1265. /**
  1266. * @brief Inserts a number of copies of given data into the %list.
  1267. * @param __position A const_iterator into the %list.
  1268. * @param __n Number of elements to be inserted.
  1269. * @param __x Data to be inserted.
  1270. * @return An iterator pointing to the first element inserted
  1271. * (or __position).
  1272. *
  1273. * This function will insert a specified number of copies of the
  1274. * given data before the location specified by @a position.
  1275. *
  1276. * This operation is linear in the number of elements inserted and
  1277. * does not invalidate iterators and references.
  1278. */
  1279. iterator
  1280. insert(const_iterator __position, size_type __n, const value_type& __x);
  1281. #else
  1282. /**
  1283. * @brief Inserts a number of copies of given data into the %list.
  1284. * @param __position An iterator into the %list.
  1285. * @param __n Number of elements to be inserted.
  1286. * @param __x Data to be inserted.
  1287. *
  1288. * This function will insert a specified number of copies of the
  1289. * given data before the location specified by @a position.
  1290. *
  1291. * This operation is linear in the number of elements inserted and
  1292. * does not invalidate iterators and references.
  1293. */
  1294. void
  1295. insert(iterator __position, size_type __n, const value_type& __x)
  1296. {
  1297. list __tmp(__n, __x, get_allocator());
  1298. splice(__position, __tmp);
  1299. }
  1300. #endif
  1301. #if __cplusplus >= 201103L
  1302. /**
  1303. * @brief Inserts a range into the %list.
  1304. * @param __position A const_iterator into the %list.
  1305. * @param __first An input iterator.
  1306. * @param __last An input iterator.
  1307. * @return An iterator pointing to the first element inserted
  1308. * (or __position).
  1309. *
  1310. * This function will insert copies of the data in the range [@a
  1311. * first,@a last) into the %list before the location specified by
  1312. * @a position.
  1313. *
  1314. * This operation is linear in the number of elements inserted and
  1315. * does not invalidate iterators and references.
  1316. */
  1317. template<typename _InputIterator,
  1318. typename = std::_RequireInputIter<_InputIterator>>
  1319. iterator
  1320. insert(const_iterator __position, _InputIterator __first,
  1321. _InputIterator __last);
  1322. #else
  1323. /**
  1324. * @brief Inserts a range into the %list.
  1325. * @param __position An iterator into the %list.
  1326. * @param __first An input iterator.
  1327. * @param __last An input iterator.
  1328. *
  1329. * This function will insert copies of the data in the range [@a
  1330. * first,@a last) into the %list before the location specified by
  1331. * @a position.
  1332. *
  1333. * This operation is linear in the number of elements inserted and
  1334. * does not invalidate iterators and references.
  1335. */
  1336. template<typename _InputIterator>
  1337. void
  1338. insert(iterator __position, _InputIterator __first,
  1339. _InputIterator __last)
  1340. {
  1341. list __tmp(__first, __last, get_allocator());
  1342. splice(__position, __tmp);
  1343. }
  1344. #endif
  1345. /**
  1346. * @brief Remove element at given position.
  1347. * @param __position Iterator pointing to element to be erased.
  1348. * @return An iterator pointing to the next element (or end()).
  1349. *
  1350. * This function will erase the element at the given position and thus
  1351. * shorten the %list by one.
  1352. *
  1353. * Due to the nature of a %list this operation can be done in
  1354. * constant time, and only invalidates iterators/references to
  1355. * the element being removed. The user is also cautioned that
  1356. * this function only erases the element, and that if the element
  1357. * is itself a pointer, the pointed-to memory is not touched in
  1358. * any way. Managing the pointer is the user's responsibility.
  1359. */
  1360. iterator
  1361. #if __cplusplus >= 201103L
  1362. erase(const_iterator __position) noexcept;
  1363. #else
  1364. erase(iterator __position);
  1365. #endif
  1366. /**
  1367. * @brief Remove a range of elements.
  1368. * @param __first Iterator pointing to the first element to be erased.
  1369. * @param __last Iterator pointing to one past the last element to be
  1370. * erased.
  1371. * @return An iterator pointing to the element pointed to by @a last
  1372. * prior to erasing (or end()).
  1373. *
  1374. * This function will erase the elements in the range @a
  1375. * [first,last) and shorten the %list accordingly.
  1376. *
  1377. * This operation is linear time in the size of the range and only
  1378. * invalidates iterators/references to the element being removed.
  1379. * The user is also cautioned that this function only erases the
  1380. * elements, and that if the elements themselves are pointers, the
  1381. * pointed-to memory is not touched in any way. Managing the pointer
  1382. * is the user's responsibility.
  1383. */
  1384. iterator
  1385. #if __cplusplus >= 201103L
  1386. erase(const_iterator __first, const_iterator __last) noexcept
  1387. #else
  1388. erase(iterator __first, iterator __last)
  1389. #endif
  1390. {
  1391. while (__first != __last)
  1392. __first = erase(__first);
  1393. return __last._M_const_cast();
  1394. }
  1395. /**
  1396. * @brief Swaps data with another %list.
  1397. * @param __x A %list of the same element and allocator types.
  1398. *
  1399. * This exchanges the elements between two lists in constant
  1400. * time. Note that the global std::swap() function is
  1401. * specialized such that std::swap(l1,l2) will feed to this
  1402. * function.
  1403. *
  1404. * Whether the allocators are swapped depends on the allocator traits.
  1405. */
  1406. void
  1407. swap(list& __x) _GLIBCXX_NOEXCEPT
  1408. {
  1409. __detail::_List_node_base::swap(this->_M_impl._M_node,
  1410. __x._M_impl._M_node);
  1411. size_t __xsize = __x._M_get_size();
  1412. __x._M_set_size(this->_M_get_size());
  1413. this->_M_set_size(__xsize);
  1414. _Node_alloc_traits::_S_on_swap(this->_M_get_Node_allocator(),
  1415. __x._M_get_Node_allocator());
  1416. }
  1417. /**
  1418. * Erases all the elements. Note that this function only erases
  1419. * the elements, and that if the elements themselves are
  1420. * pointers, the pointed-to memory is not touched in any way.
  1421. * Managing the pointer is the user's responsibility.
  1422. */
  1423. void
  1424. clear() _GLIBCXX_NOEXCEPT
  1425. {
  1426. _Base::_M_clear();
  1427. _Base::_M_init();
  1428. }
  1429. // [23.2.2.4] list operations
  1430. /**
  1431. * @brief Insert contents of another %list.
  1432. * @param __position Iterator referencing the element to insert before.
  1433. * @param __x Source list.
  1434. *
  1435. * The elements of @a __x are inserted in constant time in front of
  1436. * the element referenced by @a __position. @a __x becomes an empty
  1437. * list.
  1438. *
  1439. * Requires this != @a __x.
  1440. */
  1441. void
  1442. #if __cplusplus >= 201103L
  1443. splice(const_iterator __position, list&& __x) noexcept
  1444. #else
  1445. splice(iterator __position, list& __x)
  1446. #endif
  1447. {
  1448. if (!__x.empty())
  1449. {
  1450. _M_check_equal_allocators(__x);
  1451. this->_M_transfer(__position._M_const_cast(),
  1452. __x.begin(), __x.end());
  1453. this->_M_inc_size(__x._M_get_size());
  1454. __x._M_set_size(0);
  1455. }
  1456. }
  1457. #if __cplusplus >= 201103L
  1458. void
  1459. splice(const_iterator __position, list& __x) noexcept
  1460. { splice(__position, std::move(__x)); }
  1461. #endif
  1462. #if __cplusplus >= 201103L
  1463. /**
  1464. * @brief Insert element from another %list.
  1465. * @param __position Const_iterator referencing the element to
  1466. * insert before.
  1467. * @param __x Source list.
  1468. * @param __i Const_iterator referencing the element to move.
  1469. *
  1470. * Removes the element in list @a __x referenced by @a __i and
  1471. * inserts it into the current list before @a __position.
  1472. */
  1473. void
  1474. splice(const_iterator __position, list&& __x, const_iterator __i) noexcept
  1475. #else
  1476. /**
  1477. * @brief Insert element from another %list.
  1478. * @param __position Iterator referencing the element to insert before.
  1479. * @param __x Source list.
  1480. * @param __i Iterator referencing the element to move.
  1481. *
  1482. * Removes the element in list @a __x referenced by @a __i and
  1483. * inserts it into the current list before @a __position.
  1484. */
  1485. void
  1486. splice(iterator __position, list& __x, iterator __i)
  1487. #endif
  1488. {
  1489. iterator __j = __i._M_const_cast();
  1490. ++__j;
  1491. if (__position == __i || __position == __j)
  1492. return;
  1493. if (this != std::__addressof(__x))
  1494. _M_check_equal_allocators(__x);
  1495. this->_M_transfer(__position._M_const_cast(),
  1496. __i._M_const_cast(), __j);
  1497. this->_M_inc_size(1);
  1498. __x._M_dec_size(1);
  1499. }
  1500. #if __cplusplus >= 201103L
  1501. /**
  1502. * @brief Insert element from another %list.
  1503. * @param __position Const_iterator referencing the element to
  1504. * insert before.
  1505. * @param __x Source list.
  1506. * @param __i Const_iterator referencing the element to move.
  1507. *
  1508. * Removes the element in list @a __x referenced by @a __i and
  1509. * inserts it into the current list before @a __position.
  1510. */
  1511. void
  1512. splice(const_iterator __position, list& __x, const_iterator __i) noexcept
  1513. { splice(__position, std::move(__x), __i); }
  1514. #endif
  1515. #if __cplusplus >= 201103L
  1516. /**
  1517. * @brief Insert range from another %list.
  1518. * @param __position Const_iterator referencing the element to
  1519. * insert before.
  1520. * @param __x Source list.
  1521. * @param __first Const_iterator referencing the start of range in x.
  1522. * @param __last Const_iterator referencing the end of range in x.
  1523. *
  1524. * Removes elements in the range [__first,__last) and inserts them
  1525. * before @a __position in constant time.
  1526. *
  1527. * Undefined if @a __position is in [__first,__last).
  1528. */
  1529. void
  1530. splice(const_iterator __position, list&& __x, const_iterator __first,
  1531. const_iterator __last) noexcept
  1532. #else
  1533. /**
  1534. * @brief Insert range from another %list.
  1535. * @param __position Iterator referencing the element to insert before.
  1536. * @param __x Source list.
  1537. * @param __first Iterator referencing the start of range in x.
  1538. * @param __last Iterator referencing the end of range in x.
  1539. *
  1540. * Removes elements in the range [__first,__last) and inserts them
  1541. * before @a __position in constant time.
  1542. *
  1543. * Undefined if @a __position is in [__first,__last).
  1544. */
  1545. void
  1546. splice(iterator __position, list& __x, iterator __first,
  1547. iterator __last)
  1548. #endif
  1549. {
  1550. if (__first != __last)
  1551. {
  1552. if (this != std::__addressof(__x))
  1553. _M_check_equal_allocators(__x);
  1554. size_t __n = _S_distance(__first, __last);
  1555. this->_M_inc_size(__n);
  1556. __x._M_dec_size(__n);
  1557. this->_M_transfer(__position._M_const_cast(),
  1558. __first._M_const_cast(),
  1559. __last._M_const_cast());
  1560. }
  1561. }
  1562. #if __cplusplus >= 201103L
  1563. /**
  1564. * @brief Insert range from another %list.
  1565. * @param __position Const_iterator referencing the element to
  1566. * insert before.
  1567. * @param __x Source list.
  1568. * @param __first Const_iterator referencing the start of range in x.
  1569. * @param __last Const_iterator referencing the end of range in x.
  1570. *
  1571. * Removes elements in the range [__first,__last) and inserts them
  1572. * before @a __position in constant time.
  1573. *
  1574. * Undefined if @a __position is in [__first,__last).
  1575. */
  1576. void
  1577. splice(const_iterator __position, list& __x, const_iterator __first,
  1578. const_iterator __last) noexcept
  1579. { splice(__position, std::move(__x), __first, __last); }
  1580. #endif
  1581. private:
  1582. #if __cplusplus > 201703L
  1583. # define __cpp_lib_list_remove_return_type 201806L
  1584. typedef size_type __remove_return_type;
  1585. # define _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG \
  1586. __attribute__((__abi_tag__("__cxx20")))
  1587. #else
  1588. typedef void __remove_return_type;
  1589. # define _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG
  1590. #endif
  1591. public:
  1592. /**
  1593. * @brief Remove all elements equal to value.
  1594. * @param __value The value to remove.
  1595. *
  1596. * Removes every element in the list equal to @a value.
  1597. * Remaining elements stay in list order. Note that this
  1598. * function only erases the elements, and that if the elements
  1599. * themselves are pointers, the pointed-to memory is not
  1600. * touched in any way. Managing the pointer is the user's
  1601. * responsibility.
  1602. */
  1603. _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG
  1604. __remove_return_type
  1605. remove(const _Tp& __value);
  1606. /**
  1607. * @brief Remove all elements satisfying a predicate.
  1608. * @tparam _Predicate Unary predicate function or object.
  1609. *
  1610. * Removes every element in the list for which the predicate
  1611. * returns true. Remaining elements stay in list order. Note
  1612. * that this function only erases the elements, and that if the
  1613. * elements themselves are pointers, the pointed-to memory is
  1614. * not touched in any way. Managing the pointer is the user's
  1615. * responsibility.
  1616. */
  1617. template<typename _Predicate>
  1618. __remove_return_type
  1619. remove_if(_Predicate);
  1620. /**
  1621. * @brief Remove consecutive duplicate elements.
  1622. *
  1623. * For each consecutive set of elements with the same value,
  1624. * remove all but the first one. Remaining elements stay in
  1625. * list order. Note that this function only erases the
  1626. * elements, and that if the elements themselves are pointers,
  1627. * the pointed-to memory is not touched in any way. Managing
  1628. * the pointer is the user's responsibility.
  1629. */
  1630. _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG
  1631. __remove_return_type
  1632. unique();
  1633. /**
  1634. * @brief Remove consecutive elements satisfying a predicate.
  1635. * @tparam _BinaryPredicate Binary predicate function or object.
  1636. *
  1637. * For each consecutive set of elements [first,last) that
  1638. * satisfy predicate(first,i) where i is an iterator in
  1639. * [first,last), remove all but the first one. Remaining
  1640. * elements stay in list order. Note that this function only
  1641. * erases the elements, and that if the elements themselves are
  1642. * pointers, the pointed-to memory is not touched in any way.
  1643. * Managing the pointer is the user's responsibility.
  1644. */
  1645. template<typename _BinaryPredicate>
  1646. __remove_return_type
  1647. unique(_BinaryPredicate);
  1648. #undef _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG
  1649. /**
  1650. * @brief Merge sorted lists.
  1651. * @param __x Sorted list to merge.
  1652. *
  1653. * Assumes that both @a __x and this list are sorted according to
  1654. * operator<(). Merges elements of @a __x into this list in
  1655. * sorted order, leaving @a __x empty when complete. Elements in
  1656. * this list precede elements in @a __x that are equal.
  1657. */
  1658. #if __cplusplus >= 201103L
  1659. void
  1660. merge(list&& __x);
  1661. void
  1662. merge(list& __x)
  1663. { merge(std::move(__x)); }
  1664. #else
  1665. void
  1666. merge(list& __x);
  1667. #endif
  1668. /**
  1669. * @brief Merge sorted lists according to comparison function.
  1670. * @tparam _StrictWeakOrdering Comparison function defining
  1671. * sort order.
  1672. * @param __x Sorted list to merge.
  1673. * @param __comp Comparison functor.
  1674. *
  1675. * Assumes that both @a __x and this list are sorted according to
  1676. * StrictWeakOrdering. Merges elements of @a __x into this list
  1677. * in sorted order, leaving @a __x empty when complete. Elements
  1678. * in this list precede elements in @a __x that are equivalent
  1679. * according to StrictWeakOrdering().
  1680. */
  1681. #if __cplusplus >= 201103L
  1682. template<typename _StrictWeakOrdering>
  1683. void
  1684. merge(list&& __x, _StrictWeakOrdering __comp);
  1685. template<typename _StrictWeakOrdering>
  1686. void
  1687. merge(list& __x, _StrictWeakOrdering __comp)
  1688. { merge(std::move(__x), __comp); }
  1689. #else
  1690. template<typename _StrictWeakOrdering>
  1691. void
  1692. merge(list& __x, _StrictWeakOrdering __comp);
  1693. #endif
  1694. /**
  1695. * @brief Reverse the elements in list.
  1696. *
  1697. * Reverse the order of elements in the list in linear time.
  1698. */
  1699. void
  1700. reverse() _GLIBCXX_NOEXCEPT
  1701. { this->_M_impl._M_node._M_reverse(); }
  1702. /**
  1703. * @brief Sort the elements.
  1704. *
  1705. * Sorts the elements of this list in NlogN time. Equivalent
  1706. * elements remain in list order.
  1707. */
  1708. void
  1709. sort();
  1710. /**
  1711. * @brief Sort the elements according to comparison function.
  1712. *
  1713. * Sorts the elements of this list in NlogN time. Equivalent
  1714. * elements remain in list order.
  1715. */
  1716. template<typename _StrictWeakOrdering>
  1717. void
  1718. sort(_StrictWeakOrdering);
  1719. protected:
  1720. // Internal constructor functions follow.
  1721. // Called by the range constructor to implement [23.1.1]/9
  1722. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1723. // 438. Ambiguity in the "do the right thing" clause
  1724. template<typename _Integer>
  1725. void
  1726. _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
  1727. { _M_fill_initialize(static_cast<size_type>(__n), __x); }
  1728. // Called by the range constructor to implement [23.1.1]/9
  1729. template<typename _InputIterator>
  1730. void
  1731. _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
  1732. __false_type)
  1733. {
  1734. for (; __first != __last; ++__first)
  1735. #if __cplusplus >= 201103L
  1736. emplace_back(*__first);
  1737. #else
  1738. push_back(*__first);
  1739. #endif
  1740. }
  1741. // Called by list(n,v,a), and the range constructor when it turns out
  1742. // to be the same thing.
  1743. void
  1744. _M_fill_initialize(size_type __n, const value_type& __x)
  1745. {
  1746. for (; __n; --__n)
  1747. push_back(__x);
  1748. }
  1749. #if __cplusplus >= 201103L
  1750. // Called by list(n).
  1751. void
  1752. _M_default_initialize(size_type __n)
  1753. {
  1754. for (; __n; --__n)
  1755. emplace_back();
  1756. }
  1757. // Called by resize(sz).
  1758. void
  1759. _M_default_append(size_type __n);
  1760. #endif
  1761. // Internal assign functions follow.
  1762. // Called by the range assign to implement [23.1.1]/9
  1763. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1764. // 438. Ambiguity in the "do the right thing" clause
  1765. template<typename _Integer>
  1766. void
  1767. _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
  1768. { _M_fill_assign(__n, __val); }
  1769. // Called by the range assign to implement [23.1.1]/9
  1770. template<typename _InputIterator>
  1771. void
  1772. _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
  1773. __false_type);
  1774. // Called by assign(n,t), and the range assign when it turns out
  1775. // to be the same thing.
  1776. void
  1777. _M_fill_assign(size_type __n, const value_type& __val);
  1778. // Moves the elements from [first,last) before position.
  1779. void
  1780. _M_transfer(iterator __position, iterator __first, iterator __last)
  1781. { __position._M_node->_M_transfer(__first._M_node, __last._M_node); }
  1782. // Inserts new element at position given and with value given.
  1783. #if __cplusplus < 201103L
  1784. void
  1785. _M_insert(iterator __position, const value_type& __x)
  1786. {
  1787. _Node* __tmp = _M_create_node(__x);
  1788. __tmp->_M_hook(__position._M_node);
  1789. this->_M_inc_size(1);
  1790. }
  1791. #else
  1792. template<typename... _Args>
  1793. void
  1794. _M_insert(iterator __position, _Args&&... __args)
  1795. {
  1796. _Node* __tmp = _M_create_node(std::forward<_Args>(__args)...);
  1797. __tmp->_M_hook(__position._M_node);
  1798. this->_M_inc_size(1);
  1799. }
  1800. #endif
  1801. // Erases element at position given.
  1802. void
  1803. _M_erase(iterator __position) _GLIBCXX_NOEXCEPT
  1804. {
  1805. this->_M_dec_size(1);
  1806. __position._M_node->_M_unhook();
  1807. _Node* __n = static_cast<_Node*>(__position._M_node);
  1808. #if __cplusplus >= 201103L
  1809. _Node_alloc_traits::destroy(_M_get_Node_allocator(), __n->_M_valptr());
  1810. #else
  1811. _Tp_alloc_type(_M_get_Node_allocator()).destroy(__n->_M_valptr());
  1812. #endif
  1813. _M_put_node(__n);
  1814. }
  1815. // To implement the splice (and merge) bits of N1599.
  1816. void
  1817. _M_check_equal_allocators(list& __x) _GLIBCXX_NOEXCEPT
  1818. {
  1819. if (std::__alloc_neq<typename _Base::_Node_alloc_type>::
  1820. _S_do_it(_M_get_Node_allocator(), __x._M_get_Node_allocator()))
  1821. __builtin_abort();
  1822. }
  1823. // Used to implement resize.
  1824. const_iterator
  1825. _M_resize_pos(size_type& __new_size) const;
  1826. #if __cplusplus >= 201103L
  1827. void
  1828. _M_move_assign(list&& __x, true_type) noexcept
  1829. {
  1830. this->clear();
  1831. this->_M_move_nodes(std::move(__x));
  1832. std::__alloc_on_move(this->_M_get_Node_allocator(),
  1833. __x._M_get_Node_allocator());
  1834. }
  1835. void
  1836. _M_move_assign(list&& __x, false_type)
  1837. {
  1838. if (__x._M_get_Node_allocator() == this->_M_get_Node_allocator())
  1839. _M_move_assign(std::move(__x), true_type{});
  1840. else
  1841. // The rvalue's allocator cannot be moved, or is not equal,
  1842. // so we need to individually move each element.
  1843. _M_assign_dispatch(std::make_move_iterator(__x.begin()),
  1844. std::make_move_iterator(__x.end()),
  1845. __false_type{});
  1846. }
  1847. #endif
  1848. #if _GLIBCXX_USE_CXX11_ABI
  1849. // Update _M_size members after merging (some of) __src into __dest.
  1850. struct _Finalize_merge
  1851. {
  1852. explicit
  1853. _Finalize_merge(list& __dest, list& __src, const iterator& __src_next)
  1854. : _M_dest(__dest), _M_src(__src), _M_next(__src_next)
  1855. { }
  1856. ~_Finalize_merge()
  1857. {
  1858. // For the common case, _M_next == _M_sec.end() and the std::distance
  1859. // call is fast. But if any *iter1 < *iter2 comparison throws then we
  1860. // have to count how many elements remain in _M_src.
  1861. const size_t __num_unmerged = std::distance(_M_next, _M_src.end());
  1862. const size_t __orig_size = _M_src._M_get_size();
  1863. _M_dest._M_inc_size(__orig_size - __num_unmerged);
  1864. _M_src._M_set_size(__num_unmerged);
  1865. }
  1866. list& _M_dest;
  1867. list& _M_src;
  1868. const iterator& _M_next;
  1869. #if __cplusplus >= 201103L
  1870. _Finalize_merge(const _Finalize_merge&) = delete;
  1871. #endif
  1872. };
  1873. #else
  1874. struct _Finalize_merge
  1875. { explicit _Finalize_merge(list&, list&, const iterator&) { } };
  1876. #endif
  1877. };
  1878. #if __cpp_deduction_guides >= 201606
  1879. template<typename _InputIterator, typename _ValT
  1880. = typename iterator_traits<_InputIterator>::value_type,
  1881. typename _Allocator = allocator<_ValT>,
  1882. typename = _RequireInputIter<_InputIterator>,
  1883. typename = _RequireAllocator<_Allocator>>
  1884. list(_InputIterator, _InputIterator, _Allocator = _Allocator())
  1885. -> list<_ValT, _Allocator>;
  1886. #endif
  1887. _GLIBCXX_END_NAMESPACE_CXX11
  1888. /**
  1889. * @brief List equality comparison.
  1890. * @param __x A %list.
  1891. * @param __y A %list of the same type as @a __x.
  1892. * @return True iff the size and elements of the lists are equal.
  1893. *
  1894. * This is an equivalence relation. It is linear in the size of
  1895. * the lists. Lists are considered equivalent if their sizes are
  1896. * equal, and if corresponding elements compare equal.
  1897. */
  1898. template<typename _Tp, typename _Alloc>
  1899. _GLIBCXX_NODISCARD
  1900. inline bool
  1901. operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
  1902. {
  1903. #if _GLIBCXX_USE_CXX11_ABI
  1904. if (__x.size() != __y.size())
  1905. return false;
  1906. #endif
  1907. typedef typename list<_Tp, _Alloc>::const_iterator const_iterator;
  1908. const_iterator __end1 = __x.end();
  1909. const_iterator __end2 = __y.end();
  1910. const_iterator __i1 = __x.begin();
  1911. const_iterator __i2 = __y.begin();
  1912. while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2)
  1913. {
  1914. ++__i1;
  1915. ++__i2;
  1916. }
  1917. return __i1 == __end1 && __i2 == __end2;
  1918. }
  1919. #if __cpp_lib_three_way_comparison
  1920. /**
  1921. * @brief List ordering relation.
  1922. * @param __x A `list`.
  1923. * @param __y A `list` of the same type as `__x`.
  1924. * @return A value indicating whether `__x` is less than, equal to,
  1925. * greater than, or incomparable with `__y`.
  1926. *
  1927. * See `std::lexicographical_compare_three_way()` for how the determination
  1928. * is made. This operator is used to synthesize relational operators like
  1929. * `<` and `>=` etc.
  1930. */
  1931. template<typename _Tp, typename _Alloc>
  1932. [[nodiscard]]
  1933. inline __detail::__synth3way_t<_Tp>
  1934. operator<=>(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
  1935. {
  1936. return std::lexicographical_compare_three_way(__x.begin(), __x.end(),
  1937. __y.begin(), __y.end(),
  1938. __detail::__synth3way);
  1939. }
  1940. #else
  1941. /**
  1942. * @brief List ordering relation.
  1943. * @param __x A %list.
  1944. * @param __y A %list of the same type as @a __x.
  1945. * @return True iff @a __x is lexicographically less than @a __y.
  1946. *
  1947. * This is a total ordering relation. It is linear in the size of the
  1948. * lists. The elements must be comparable with @c <.
  1949. *
  1950. * See std::lexicographical_compare() for how the determination is made.
  1951. */
  1952. template<typename _Tp, typename _Alloc>
  1953. _GLIBCXX_NODISCARD
  1954. inline bool
  1955. operator<(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
  1956. { return std::lexicographical_compare(__x.begin(), __x.end(),
  1957. __y.begin(), __y.end()); }
  1958. /// Based on operator==
  1959. template<typename _Tp, typename _Alloc>
  1960. _GLIBCXX_NODISCARD
  1961. inline bool
  1962. operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
  1963. { return !(__x == __y); }
  1964. /// Based on operator<
  1965. template<typename _Tp, typename _Alloc>
  1966. _GLIBCXX_NODISCARD
  1967. inline bool
  1968. operator>(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
  1969. { return __y < __x; }
  1970. /// Based on operator<
  1971. template<typename _Tp, typename _Alloc>
  1972. _GLIBCXX_NODISCARD
  1973. inline bool
  1974. operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
  1975. { return !(__y < __x); }
  1976. /// Based on operator<
  1977. template<typename _Tp, typename _Alloc>
  1978. _GLIBCXX_NODISCARD
  1979. inline bool
  1980. operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
  1981. { return !(__x < __y); }
  1982. #endif // three-way comparison
  1983. /// See std::list::swap().
  1984. template<typename _Tp, typename _Alloc>
  1985. inline void
  1986. swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
  1987. _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
  1988. { __x.swap(__y); }
  1989. _GLIBCXX_END_NAMESPACE_CONTAINER
  1990. #if _GLIBCXX_USE_CXX11_ABI
  1991. // Detect when distance is used to compute the size of the whole list.
  1992. template<typename _Tp>
  1993. inline ptrdiff_t
  1994. __distance(_GLIBCXX_STD_C::_List_iterator<_Tp> __first,
  1995. _GLIBCXX_STD_C::_List_iterator<_Tp> __last,
  1996. input_iterator_tag __tag)
  1997. {
  1998. typedef _GLIBCXX_STD_C::_List_const_iterator<_Tp> _CIter;
  1999. return std::__distance(_CIter(__first), _CIter(__last), __tag);
  2000. }
  2001. template<typename _Tp>
  2002. inline ptrdiff_t
  2003. __distance(_GLIBCXX_STD_C::_List_const_iterator<_Tp> __first,
  2004. _GLIBCXX_STD_C::_List_const_iterator<_Tp> __last,
  2005. input_iterator_tag)
  2006. {
  2007. typedef __detail::_List_node_header _Sentinel;
  2008. _GLIBCXX_STD_C::_List_const_iterator<_Tp> __beyond = __last;
  2009. ++__beyond;
  2010. const bool __whole = __first == __beyond;
  2011. if (__builtin_constant_p (__whole) && __whole)
  2012. return static_cast<const _Sentinel*>(__last._M_node)->_M_size;
  2013. ptrdiff_t __n = 0;
  2014. while (__first != __last)
  2015. {
  2016. ++__first;
  2017. ++__n;
  2018. }
  2019. return __n;
  2020. }
  2021. #endif
  2022. _GLIBCXX_END_NAMESPACE_VERSION
  2023. } // namespace std
  2024. #endif /* _STL_LIST_H */