stl_deque.h 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385
  1. // Deque implementation -*- C++ -*-
  2. // Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. *
  22. * Copyright (c) 1994
  23. * Hewlett-Packard Company
  24. *
  25. * Permission to use, copy, modify, distribute and sell this software
  26. * and its documentation for any purpose is hereby granted without fee,
  27. * provided that the above copyright notice appear in all copies and
  28. * that both that copyright notice and this permission notice appear
  29. * in supporting documentation. Hewlett-Packard Company makes no
  30. * representations about the suitability of this software for any
  31. * purpose. It is provided "as is" without express or implied warranty.
  32. *
  33. *
  34. * Copyright (c) 1997
  35. * Silicon Graphics Computer Systems, Inc.
  36. *
  37. * Permission to use, copy, modify, distribute and sell this software
  38. * and its documentation for any purpose is hereby granted without fee,
  39. * provided that the above copyright notice appear in all copies and
  40. * that both that copyright notice and this permission notice appear
  41. * in supporting documentation. Silicon Graphics makes no
  42. * representations about the suitability of this software for any
  43. * purpose. It is provided "as is" without express or implied warranty.
  44. */
  45. /** @file bits/stl_deque.h
  46. * This is an internal header file, included by other library headers.
  47. * Do not attempt to use it directly. @headername{deque}
  48. */
  49. #ifndef _STL_DEQUE_H
  50. #define _STL_DEQUE_H 1
  51. #include <bits/concept_check.h>
  52. #include <bits/stl_iterator_base_types.h>
  53. #include <bits/stl_iterator_base_funcs.h>
  54. #if __cplusplus >= 201103L
  55. #include <initializer_list>
  56. #include <bits/stl_uninitialized.h> // for __is_bitwise_relocatable
  57. #endif
  58. #if __cplusplus > 201703L
  59. # include <compare>
  60. #endif
  61. #include <debug/assertions.h>
  62. namespace std _GLIBCXX_VISIBILITY(default)
  63. {
  64. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  65. _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
  66. /**
  67. * @brief This function controls the size of memory nodes.
  68. * @param __size The size of an element.
  69. * @return The number (not byte size) of elements per node.
  70. *
  71. * This function started off as a compiler kludge from SGI, but
  72. * seems to be a useful wrapper around a repeated constant
  73. * expression. The @b 512 is tunable (and no other code needs to
  74. * change), but no investigation has been done since inheriting the
  75. * SGI code. Touch _GLIBCXX_DEQUE_BUF_SIZE only if you know what
  76. * you are doing, however: changing it breaks the binary
  77. * compatibility!!
  78. */
  79. #ifndef _GLIBCXX_DEQUE_BUF_SIZE
  80. #define _GLIBCXX_DEQUE_BUF_SIZE 512
  81. #endif
  82. _GLIBCXX_CONSTEXPR inline size_t
  83. __deque_buf_size(size_t __size)
  84. { return (__size < _GLIBCXX_DEQUE_BUF_SIZE
  85. ? size_t(_GLIBCXX_DEQUE_BUF_SIZE / __size) : size_t(1)); }
  86. /**
  87. * @brief A deque::iterator.
  88. *
  89. * Quite a bit of intelligence here. Much of the functionality of
  90. * deque is actually passed off to this class. A deque holds two
  91. * of these internally, marking its valid range. Access to
  92. * elements is done as offsets of either of those two, relying on
  93. * operator overloading in this class.
  94. *
  95. * All the functions are op overloads except for _M_set_node.
  96. */
  97. template<typename _Tp, typename _Ref, typename _Ptr>
  98. struct _Deque_iterator
  99. {
  100. #if __cplusplus < 201103L
  101. typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator;
  102. typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
  103. typedef _Tp* _Elt_pointer;
  104. typedef _Tp** _Map_pointer;
  105. #else
  106. private:
  107. template<typename _CvTp>
  108. using __iter = _Deque_iterator<_Tp, _CvTp&, __ptr_rebind<_Ptr, _CvTp>>;
  109. public:
  110. typedef __iter<_Tp> iterator;
  111. typedef __iter<const _Tp> const_iterator;
  112. typedef __ptr_rebind<_Ptr, _Tp> _Elt_pointer;
  113. typedef __ptr_rebind<_Ptr, _Elt_pointer> _Map_pointer;
  114. #endif
  115. static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT
  116. { return __deque_buf_size(sizeof(_Tp)); }
  117. typedef std::random_access_iterator_tag iterator_category;
  118. typedef _Tp value_type;
  119. typedef _Ptr pointer;
  120. typedef _Ref reference;
  121. typedef size_t size_type;
  122. typedef ptrdiff_t difference_type;
  123. typedef _Deque_iterator _Self;
  124. _Elt_pointer _M_cur;
  125. _Elt_pointer _M_first;
  126. _Elt_pointer _M_last;
  127. _Map_pointer _M_node;
  128. _Deque_iterator(_Elt_pointer __x, _Map_pointer __y) _GLIBCXX_NOEXCEPT
  129. : _M_cur(__x), _M_first(*__y),
  130. _M_last(*__y + _S_buffer_size()), _M_node(__y) { }
  131. _Deque_iterator() _GLIBCXX_NOEXCEPT
  132. : _M_cur(), _M_first(), _M_last(), _M_node() { }
  133. #if __cplusplus < 201103L
  134. // Conversion from iterator to const_iterator.
  135. _Deque_iterator(const iterator& __x) _GLIBCXX_NOEXCEPT
  136. : _M_cur(__x._M_cur), _M_first(__x._M_first),
  137. _M_last(__x._M_last), _M_node(__x._M_node) { }
  138. #else
  139. // Conversion from iterator to const_iterator.
  140. template<typename _Iter,
  141. typename = _Require<is_same<_Self, const_iterator>,
  142. is_same<_Iter, iterator>>>
  143. _Deque_iterator(const _Iter& __x) noexcept
  144. : _M_cur(__x._M_cur), _M_first(__x._M_first),
  145. _M_last(__x._M_last), _M_node(__x._M_node) { }
  146. _Deque_iterator(const _Deque_iterator& __x) noexcept
  147. : _M_cur(__x._M_cur), _M_first(__x._M_first),
  148. _M_last(__x._M_last), _M_node(__x._M_node) { }
  149. _Deque_iterator& operator=(const _Deque_iterator&) = default;
  150. #endif
  151. iterator
  152. _M_const_cast() const _GLIBCXX_NOEXCEPT
  153. { return iterator(_M_cur, _M_node); }
  154. _GLIBCXX_NODISCARD
  155. reference
  156. operator*() const _GLIBCXX_NOEXCEPT
  157. { return *_M_cur; }
  158. _GLIBCXX_NODISCARD
  159. pointer
  160. operator->() const _GLIBCXX_NOEXCEPT
  161. { return _M_cur; }
  162. _Self&
  163. operator++() _GLIBCXX_NOEXCEPT
  164. {
  165. ++_M_cur;
  166. if (_M_cur == _M_last)
  167. {
  168. _M_set_node(_M_node + 1);
  169. _M_cur = _M_first;
  170. }
  171. return *this;
  172. }
  173. _Self
  174. operator++(int) _GLIBCXX_NOEXCEPT
  175. {
  176. _Self __tmp = *this;
  177. ++*this;
  178. return __tmp;
  179. }
  180. _Self&
  181. operator--() _GLIBCXX_NOEXCEPT
  182. {
  183. if (_M_cur == _M_first)
  184. {
  185. _M_set_node(_M_node - 1);
  186. _M_cur = _M_last;
  187. }
  188. --_M_cur;
  189. return *this;
  190. }
  191. _Self
  192. operator--(int) _GLIBCXX_NOEXCEPT
  193. {
  194. _Self __tmp = *this;
  195. --*this;
  196. return __tmp;
  197. }
  198. _Self&
  199. operator+=(difference_type __n) _GLIBCXX_NOEXCEPT
  200. {
  201. const difference_type __offset = __n + (_M_cur - _M_first);
  202. if (__offset >= 0 && __offset < difference_type(_S_buffer_size()))
  203. _M_cur += __n;
  204. else
  205. {
  206. const difference_type __node_offset =
  207. __offset > 0 ? __offset / difference_type(_S_buffer_size())
  208. : -difference_type((-__offset - 1)
  209. / _S_buffer_size()) - 1;
  210. _M_set_node(_M_node + __node_offset);
  211. _M_cur = _M_first + (__offset - __node_offset
  212. * difference_type(_S_buffer_size()));
  213. }
  214. return *this;
  215. }
  216. _Self&
  217. operator-=(difference_type __n) _GLIBCXX_NOEXCEPT
  218. { return *this += -__n; }
  219. _GLIBCXX_NODISCARD
  220. reference
  221. operator[](difference_type __n) const _GLIBCXX_NOEXCEPT
  222. { return *(*this + __n); }
  223. /**
  224. * Prepares to traverse new_node. Sets everything except
  225. * _M_cur, which should therefore be set by the caller
  226. * immediately afterwards, based on _M_first and _M_last.
  227. */
  228. void
  229. _M_set_node(_Map_pointer __new_node) _GLIBCXX_NOEXCEPT
  230. {
  231. _M_node = __new_node;
  232. _M_first = *__new_node;
  233. _M_last = _M_first + difference_type(_S_buffer_size());
  234. }
  235. _GLIBCXX_NODISCARD
  236. friend bool
  237. operator==(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
  238. { return __x._M_cur == __y._M_cur; }
  239. // Note: we also provide overloads whose operands are of the same type in
  240. // order to avoid ambiguous overload resolution when std::rel_ops
  241. // operators are in scope (for additional details, see libstdc++/3628)
  242. template<typename _RefR, typename _PtrR>
  243. _GLIBCXX_NODISCARD
  244. friend bool
  245. operator==(const _Self& __x,
  246. const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
  247. _GLIBCXX_NOEXCEPT
  248. { return __x._M_cur == __y._M_cur; }
  249. #if __cpp_lib_three_way_comparison
  250. [[nodiscard]]
  251. friend strong_ordering
  252. operator<=>(const _Self& __x, const _Self& __y) noexcept
  253. {
  254. if (const auto __cmp = __x._M_node <=> __y._M_node; __cmp != 0)
  255. return __cmp;
  256. return __x._M_cur <=> __y._M_cur;
  257. }
  258. #else
  259. _GLIBCXX_NODISCARD
  260. friend bool
  261. operator!=(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
  262. { return !(__x == __y); }
  263. template<typename _RefR, typename _PtrR>
  264. _GLIBCXX_NODISCARD
  265. friend bool
  266. operator!=(const _Self& __x,
  267. const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
  268. _GLIBCXX_NOEXCEPT
  269. { return !(__x == __y); }
  270. _GLIBCXX_NODISCARD
  271. friend bool
  272. operator<(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
  273. {
  274. return (__x._M_node == __y._M_node)
  275. ? (__x._M_cur < __y._M_cur) : (__x._M_node < __y._M_node);
  276. }
  277. template<typename _RefR, typename _PtrR>
  278. _GLIBCXX_NODISCARD
  279. friend bool
  280. operator<(const _Self& __x,
  281. const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
  282. _GLIBCXX_NOEXCEPT
  283. {
  284. return (__x._M_node == __y._M_node)
  285. ? (__x._M_cur < __y._M_cur) : (__x._M_node < __y._M_node);
  286. }
  287. _GLIBCXX_NODISCARD
  288. friend bool
  289. operator>(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
  290. { return __y < __x; }
  291. template<typename _RefR, typename _PtrR>
  292. _GLIBCXX_NODISCARD
  293. friend bool
  294. operator>(const _Self& __x,
  295. const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
  296. _GLIBCXX_NOEXCEPT
  297. { return __y < __x; }
  298. _GLIBCXX_NODISCARD
  299. friend bool
  300. operator<=(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
  301. { return !(__y < __x); }
  302. template<typename _RefR, typename _PtrR>
  303. _GLIBCXX_NODISCARD
  304. friend bool
  305. operator<=(const _Self& __x,
  306. const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
  307. _GLIBCXX_NOEXCEPT
  308. { return !(__y < __x); }
  309. _GLIBCXX_NODISCARD
  310. friend bool
  311. operator>=(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
  312. { return !(__x < __y); }
  313. template<typename _RefR, typename _PtrR>
  314. _GLIBCXX_NODISCARD
  315. friend bool
  316. operator>=(const _Self& __x,
  317. const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
  318. _GLIBCXX_NOEXCEPT
  319. { return !(__x < __y); }
  320. #endif // three-way comparison
  321. _GLIBCXX_NODISCARD
  322. friend difference_type
  323. operator-(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
  324. {
  325. return difference_type(_S_buffer_size())
  326. * (__x._M_node - __y._M_node - bool(__x._M_node))
  327. + (__x._M_cur - __x._M_first)
  328. + (__y._M_last - __y._M_cur);
  329. }
  330. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  331. // According to the resolution of DR179 not only the various comparison
  332. // operators but also operator- must accept mixed iterator/const_iterator
  333. // parameters.
  334. template<typename _RefR, typename _PtrR>
  335. _GLIBCXX_NODISCARD
  336. friend difference_type
  337. operator-(const _Self& __x,
  338. const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
  339. _GLIBCXX_NOEXCEPT
  340. {
  341. return difference_type(_S_buffer_size())
  342. * (__x._M_node - __y._M_node - bool(__x._M_node))
  343. + (__x._M_cur - __x._M_first)
  344. + (__y._M_last - __y._M_cur);
  345. }
  346. _GLIBCXX_NODISCARD
  347. friend _Self
  348. operator+(const _Self& __x, difference_type __n) _GLIBCXX_NOEXCEPT
  349. {
  350. _Self __tmp = __x;
  351. __tmp += __n;
  352. return __tmp;
  353. }
  354. _GLIBCXX_NODISCARD
  355. friend _Self
  356. operator-(const _Self& __x, difference_type __n) _GLIBCXX_NOEXCEPT
  357. {
  358. _Self __tmp = __x;
  359. __tmp -= __n;
  360. return __tmp;
  361. }
  362. _GLIBCXX_NODISCARD
  363. friend _Self
  364. operator+(difference_type __n, const _Self& __x) _GLIBCXX_NOEXCEPT
  365. { return __x + __n; }
  366. };
  367. /**
  368. * Deque base class. This class provides the unified face for %deque's
  369. * allocation. This class's constructor and destructor allocate and
  370. * deallocate (but do not initialize) storage. This makes %exception
  371. * safety easier.
  372. *
  373. * Nothing in this class ever constructs or destroys an actual Tp element.
  374. * (Deque handles that itself.) Only/All memory management is performed
  375. * here.
  376. */
  377. template<typename _Tp, typename _Alloc>
  378. class _Deque_base
  379. {
  380. protected:
  381. typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
  382. rebind<_Tp>::other _Tp_alloc_type;
  383. typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Alloc_traits;
  384. #if __cplusplus < 201103L
  385. typedef _Tp* _Ptr;
  386. typedef const _Tp* _Ptr_const;
  387. #else
  388. typedef typename _Alloc_traits::pointer _Ptr;
  389. typedef typename _Alloc_traits::const_pointer _Ptr_const;
  390. #endif
  391. typedef typename _Alloc_traits::template rebind<_Ptr>::other
  392. _Map_alloc_type;
  393. typedef __gnu_cxx::__alloc_traits<_Map_alloc_type> _Map_alloc_traits;
  394. typedef _Alloc allocator_type;
  395. allocator_type
  396. get_allocator() const _GLIBCXX_NOEXCEPT
  397. { return allocator_type(_M_get_Tp_allocator()); }
  398. typedef _Deque_iterator<_Tp, _Tp&, _Ptr> iterator;
  399. typedef _Deque_iterator<_Tp, const _Tp&, _Ptr_const> const_iterator;
  400. _Deque_base()
  401. : _M_impl()
  402. { _M_initialize_map(0); }
  403. _Deque_base(size_t __num_elements)
  404. : _M_impl()
  405. { _M_initialize_map(__num_elements); }
  406. _Deque_base(const allocator_type& __a, size_t __num_elements)
  407. : _M_impl(__a)
  408. { _M_initialize_map(__num_elements); }
  409. _Deque_base(const allocator_type& __a)
  410. : _M_impl(__a)
  411. { /* Caller must initialize map. */ }
  412. #if __cplusplus >= 201103L
  413. _Deque_base(_Deque_base&& __x)
  414. : _M_impl(std::move(__x._M_get_Tp_allocator()))
  415. {
  416. _M_initialize_map(0);
  417. if (__x._M_impl._M_map)
  418. this->_M_impl._M_swap_data(__x._M_impl);
  419. }
  420. _Deque_base(_Deque_base&& __x, const allocator_type& __a)
  421. : _M_impl(std::move(__x._M_impl), _Tp_alloc_type(__a))
  422. { __x._M_initialize_map(0); }
  423. _Deque_base(_Deque_base&& __x, const allocator_type& __a, size_t __n)
  424. : _M_impl(__a)
  425. {
  426. if (__x.get_allocator() == __a)
  427. {
  428. if (__x._M_impl._M_map)
  429. {
  430. _M_initialize_map(0);
  431. this->_M_impl._M_swap_data(__x._M_impl);
  432. }
  433. }
  434. else
  435. {
  436. _M_initialize_map(__n);
  437. }
  438. }
  439. #endif
  440. ~_Deque_base() _GLIBCXX_NOEXCEPT;
  441. typedef typename iterator::_Map_pointer _Map_pointer;
  442. struct _Deque_impl_data
  443. {
  444. _Map_pointer _M_map;
  445. size_t _M_map_size;
  446. iterator _M_start;
  447. iterator _M_finish;
  448. _Deque_impl_data() _GLIBCXX_NOEXCEPT
  449. : _M_map(), _M_map_size(), _M_start(), _M_finish()
  450. { }
  451. #if __cplusplus >= 201103L
  452. _Deque_impl_data(const _Deque_impl_data&) = default;
  453. _Deque_impl_data&
  454. operator=(const _Deque_impl_data&) = default;
  455. _Deque_impl_data(_Deque_impl_data&& __x) noexcept
  456. : _Deque_impl_data(__x)
  457. { __x = _Deque_impl_data(); }
  458. #endif
  459. void
  460. _M_swap_data(_Deque_impl_data& __x) _GLIBCXX_NOEXCEPT
  461. {
  462. // Do not use std::swap(_M_start, __x._M_start), etc as it loses
  463. // information used by TBAA.
  464. std::swap(*this, __x);
  465. }
  466. };
  467. // This struct encapsulates the implementation of the std::deque
  468. // standard container and at the same time makes use of the EBO
  469. // for empty allocators.
  470. struct _Deque_impl
  471. : public _Tp_alloc_type, public _Deque_impl_data
  472. {
  473. _Deque_impl() _GLIBCXX_NOEXCEPT_IF(
  474. is_nothrow_default_constructible<_Tp_alloc_type>::value)
  475. : _Tp_alloc_type()
  476. { }
  477. _Deque_impl(const _Tp_alloc_type& __a) _GLIBCXX_NOEXCEPT
  478. : _Tp_alloc_type(__a)
  479. { }
  480. #if __cplusplus >= 201103L
  481. _Deque_impl(_Deque_impl&&) = default;
  482. _Deque_impl(_Tp_alloc_type&& __a) noexcept
  483. : _Tp_alloc_type(std::move(__a))
  484. { }
  485. _Deque_impl(_Deque_impl&& __d, _Tp_alloc_type&& __a)
  486. : _Tp_alloc_type(std::move(__a)), _Deque_impl_data(std::move(__d))
  487. { }
  488. #endif
  489. };
  490. _Tp_alloc_type&
  491. _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
  492. { return this->_M_impl; }
  493. const _Tp_alloc_type&
  494. _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
  495. { return this->_M_impl; }
  496. _Map_alloc_type
  497. _M_get_map_allocator() const _GLIBCXX_NOEXCEPT
  498. { return _Map_alloc_type(_M_get_Tp_allocator()); }
  499. _Ptr
  500. _M_allocate_node()
  501. {
  502. typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Traits;
  503. return _Traits::allocate(_M_impl, __deque_buf_size(sizeof(_Tp)));
  504. }
  505. void
  506. _M_deallocate_node(_Ptr __p) _GLIBCXX_NOEXCEPT
  507. {
  508. typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Traits;
  509. _Traits::deallocate(_M_impl, __p, __deque_buf_size(sizeof(_Tp)));
  510. }
  511. _Map_pointer
  512. _M_allocate_map(size_t __n)
  513. {
  514. _Map_alloc_type __map_alloc = _M_get_map_allocator();
  515. return _Map_alloc_traits::allocate(__map_alloc, __n);
  516. }
  517. void
  518. _M_deallocate_map(_Map_pointer __p, size_t __n) _GLIBCXX_NOEXCEPT
  519. {
  520. _Map_alloc_type __map_alloc = _M_get_map_allocator();
  521. _Map_alloc_traits::deallocate(__map_alloc, __p, __n);
  522. }
  523. void _M_initialize_map(size_t);
  524. void _M_create_nodes(_Map_pointer __nstart, _Map_pointer __nfinish);
  525. void _M_destroy_nodes(_Map_pointer __nstart,
  526. _Map_pointer __nfinish) _GLIBCXX_NOEXCEPT;
  527. enum { _S_initial_map_size = 8 };
  528. _Deque_impl _M_impl;
  529. };
  530. template<typename _Tp, typename _Alloc>
  531. _Deque_base<_Tp, _Alloc>::
  532. ~_Deque_base() _GLIBCXX_NOEXCEPT
  533. {
  534. if (this->_M_impl._M_map)
  535. {
  536. _M_destroy_nodes(this->_M_impl._M_start._M_node,
  537. this->_M_impl._M_finish._M_node + 1);
  538. _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
  539. }
  540. }
  541. /**
  542. * @brief Layout storage.
  543. * @param __num_elements The count of T's for which to allocate space
  544. * at first.
  545. * @return Nothing.
  546. *
  547. * The initial underlying memory layout is a bit complicated...
  548. */
  549. template<typename _Tp, typename _Alloc>
  550. void
  551. _Deque_base<_Tp, _Alloc>::
  552. _M_initialize_map(size_t __num_elements)
  553. {
  554. const size_t __num_nodes = (__num_elements / __deque_buf_size(sizeof(_Tp))
  555. + 1);
  556. this->_M_impl._M_map_size = std::max((size_t) _S_initial_map_size,
  557. size_t(__num_nodes + 2));
  558. this->_M_impl._M_map = _M_allocate_map(this->_M_impl._M_map_size);
  559. // For "small" maps (needing less than _M_map_size nodes), allocation
  560. // starts in the middle elements and grows outwards. So nstart may be
  561. // the beginning of _M_map, but for small maps it may be as far in as
  562. // _M_map+3.
  563. _Map_pointer __nstart = (this->_M_impl._M_map
  564. + (this->_M_impl._M_map_size - __num_nodes) / 2);
  565. _Map_pointer __nfinish = __nstart + __num_nodes;
  566. __try
  567. { _M_create_nodes(__nstart, __nfinish); }
  568. __catch(...)
  569. {
  570. _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
  571. this->_M_impl._M_map = _Map_pointer();
  572. this->_M_impl._M_map_size = 0;
  573. __throw_exception_again;
  574. }
  575. this->_M_impl._M_start._M_set_node(__nstart);
  576. this->_M_impl._M_finish._M_set_node(__nfinish - 1);
  577. this->_M_impl._M_start._M_cur = _M_impl._M_start._M_first;
  578. this->_M_impl._M_finish._M_cur = (this->_M_impl._M_finish._M_first
  579. + __num_elements
  580. % __deque_buf_size(sizeof(_Tp)));
  581. }
  582. template<typename _Tp, typename _Alloc>
  583. void
  584. _Deque_base<_Tp, _Alloc>::
  585. _M_create_nodes(_Map_pointer __nstart, _Map_pointer __nfinish)
  586. {
  587. _Map_pointer __cur;
  588. __try
  589. {
  590. for (__cur = __nstart; __cur < __nfinish; ++__cur)
  591. *__cur = this->_M_allocate_node();
  592. }
  593. __catch(...)
  594. {
  595. _M_destroy_nodes(__nstart, __cur);
  596. __throw_exception_again;
  597. }
  598. }
  599. template<typename _Tp, typename _Alloc>
  600. void
  601. _Deque_base<_Tp, _Alloc>::
  602. _M_destroy_nodes(_Map_pointer __nstart,
  603. _Map_pointer __nfinish) _GLIBCXX_NOEXCEPT
  604. {
  605. for (_Map_pointer __n = __nstart; __n < __nfinish; ++__n)
  606. _M_deallocate_node(*__n);
  607. }
  608. /**
  609. * @brief A standard container using fixed-size memory allocation and
  610. * constant-time manipulation of elements at either end.
  611. *
  612. * @ingroup sequences
  613. *
  614. * @tparam _Tp Type of element.
  615. * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
  616. *
  617. * Meets the requirements of a <a href="tables.html#65">container</a>, a
  618. * <a href="tables.html#66">reversible container</a>, and a
  619. * <a href="tables.html#67">sequence</a>, including the
  620. * <a href="tables.html#68">optional sequence requirements</a>.
  621. *
  622. * In previous HP/SGI versions of deque, there was an extra template
  623. * parameter so users could control the node size. This extension turned
  624. * out to violate the C++ standard (it can be detected using template
  625. * template parameters), and it was removed.
  626. *
  627. * Here's how a deque<Tp> manages memory. Each deque has 4 members:
  628. *
  629. * - Tp** _M_map
  630. * - size_t _M_map_size
  631. * - iterator _M_start, _M_finish
  632. *
  633. * map_size is at least 8. %map is an array of map_size
  634. * pointers-to-@a nodes. (The name %map has nothing to do with the
  635. * std::map class, and @b nodes should not be confused with
  636. * std::list's usage of @a node.)
  637. *
  638. * A @a node has no specific type name as such, but it is referred
  639. * to as @a node in this file. It is a simple array-of-Tp. If Tp
  640. * is very large, there will be one Tp element per node (i.e., an
  641. * @a array of one). For non-huge Tp's, node size is inversely
  642. * related to Tp size: the larger the Tp, the fewer Tp's will fit
  643. * in a node. The goal here is to keep the total size of a node
  644. * relatively small and constant over different Tp's, to improve
  645. * allocator efficiency.
  646. *
  647. * Not every pointer in the %map array will point to a node. If
  648. * the initial number of elements in the deque is small, the
  649. * /middle/ %map pointers will be valid, and the ones at the edges
  650. * will be unused. This same situation will arise as the %map
  651. * grows: available %map pointers, if any, will be on the ends. As
  652. * new nodes are created, only a subset of the %map's pointers need
  653. * to be copied @a outward.
  654. *
  655. * Class invariants:
  656. * - For any nonsingular iterator i:
  657. * - i.node points to a member of the %map array. (Yes, you read that
  658. * correctly: i.node does not actually point to a node.) The member of
  659. * the %map array is what actually points to the node.
  660. * - i.first == *(i.node) (This points to the node (first Tp element).)
  661. * - i.last == i.first + node_size
  662. * - i.cur is a pointer in the range [i.first, i.last). NOTE:
  663. * the implication of this is that i.cur is always a dereferenceable
  664. * pointer, even if i is a past-the-end iterator.
  665. * - Start and Finish are always nonsingular iterators. NOTE: this
  666. * means that an empty deque must have one node, a deque with <N
  667. * elements (where N is the node buffer size) must have one node, a
  668. * deque with N through (2N-1) elements must have two nodes, etc.
  669. * - For every node other than start.node and finish.node, every
  670. * element in the node is an initialized object. If start.node ==
  671. * finish.node, then [start.cur, finish.cur) are initialized
  672. * objects, and the elements outside that range are uninitialized
  673. * storage. Otherwise, [start.cur, start.last) and [finish.first,
  674. * finish.cur) are initialized objects, and [start.first, start.cur)
  675. * and [finish.cur, finish.last) are uninitialized storage.
  676. * - [%map, %map + map_size) is a valid, non-empty range.
  677. * - [start.node, finish.node] is a valid range contained within
  678. * [%map, %map + map_size).
  679. * - A pointer in the range [%map, %map + map_size) points to an allocated
  680. * node if and only if the pointer is in the range
  681. * [start.node, finish.node].
  682. *
  683. * Here's the magic: nothing in deque is @b aware of the discontiguous
  684. * storage!
  685. *
  686. * The memory setup and layout occurs in the parent, _Base, and the iterator
  687. * class is entirely responsible for @a leaping from one node to the next.
  688. * All the implementation routines for deque itself work only through the
  689. * start and finish iterators. This keeps the routines simple and sane,
  690. * and we can use other standard algorithms as well.
  691. */
  692. template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
  693. class deque : protected _Deque_base<_Tp, _Alloc>
  694. {
  695. #ifdef _GLIBCXX_CONCEPT_CHECKS
  696. // concept requirements
  697. typedef typename _Alloc::value_type _Alloc_value_type;
  698. # if __cplusplus < 201103L
  699. __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
  700. # endif
  701. __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
  702. #endif
  703. #if __cplusplus >= 201103L
  704. static_assert(is_same<typename remove_cv<_Tp>::type, _Tp>::value,
  705. "std::deque must have a non-const, non-volatile value_type");
  706. # if __cplusplus > 201703L || defined __STRICT_ANSI__
  707. static_assert(is_same<typename _Alloc::value_type, _Tp>::value,
  708. "std::deque must have the same value_type as its allocator");
  709. # endif
  710. #endif
  711. typedef _Deque_base<_Tp, _Alloc> _Base;
  712. typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
  713. typedef typename _Base::_Alloc_traits _Alloc_traits;
  714. typedef typename _Base::_Map_pointer _Map_pointer;
  715. public:
  716. typedef _Tp value_type;
  717. typedef typename _Alloc_traits::pointer pointer;
  718. typedef typename _Alloc_traits::const_pointer const_pointer;
  719. typedef typename _Alloc_traits::reference reference;
  720. typedef typename _Alloc_traits::const_reference const_reference;
  721. typedef typename _Base::iterator iterator;
  722. typedef typename _Base::const_iterator const_iterator;
  723. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  724. typedef std::reverse_iterator<iterator> reverse_iterator;
  725. typedef size_t size_type;
  726. typedef ptrdiff_t difference_type;
  727. typedef _Alloc allocator_type;
  728. private:
  729. static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT
  730. { return __deque_buf_size(sizeof(_Tp)); }
  731. // Functions controlling memory layout, and nothing else.
  732. using _Base::_M_initialize_map;
  733. using _Base::_M_create_nodes;
  734. using _Base::_M_destroy_nodes;
  735. using _Base::_M_allocate_node;
  736. using _Base::_M_deallocate_node;
  737. using _Base::_M_allocate_map;
  738. using _Base::_M_deallocate_map;
  739. using _Base::_M_get_Tp_allocator;
  740. /**
  741. * A total of four data members accumulated down the hierarchy.
  742. * May be accessed via _M_impl.*
  743. */
  744. using _Base::_M_impl;
  745. public:
  746. // [23.2.1.1] construct/copy/destroy
  747. // (assign() and get_allocator() are also listed in this section)
  748. /**
  749. * @brief Creates a %deque with no elements.
  750. */
  751. #if __cplusplus >= 201103L
  752. deque() = default;
  753. #else
  754. deque() { }
  755. #endif
  756. /**
  757. * @brief Creates a %deque with no elements.
  758. * @param __a An allocator object.
  759. */
  760. explicit
  761. deque(const allocator_type& __a)
  762. : _Base(__a, 0) { }
  763. #if __cplusplus >= 201103L
  764. /**
  765. * @brief Creates a %deque with default constructed elements.
  766. * @param __n The number of elements to initially create.
  767. * @param __a An allocator.
  768. *
  769. * This constructor fills the %deque with @a n default
  770. * constructed elements.
  771. */
  772. explicit
  773. deque(size_type __n, const allocator_type& __a = allocator_type())
  774. : _Base(__a, _S_check_init_len(__n, __a))
  775. { _M_default_initialize(); }
  776. /**
  777. * @brief Creates a %deque with copies of an exemplar element.
  778. * @param __n The number of elements to initially create.
  779. * @param __value An element to copy.
  780. * @param __a An allocator.
  781. *
  782. * This constructor fills the %deque with @a __n copies of @a __value.
  783. */
  784. deque(size_type __n, const value_type& __value,
  785. const allocator_type& __a = allocator_type())
  786. : _Base(__a, _S_check_init_len(__n, __a))
  787. { _M_fill_initialize(__value); }
  788. #else
  789. /**
  790. * @brief Creates a %deque with copies of an exemplar element.
  791. * @param __n The number of elements to initially create.
  792. * @param __value An element to copy.
  793. * @param __a An allocator.
  794. *
  795. * This constructor fills the %deque with @a __n copies of @a __value.
  796. */
  797. explicit
  798. deque(size_type __n, const value_type& __value = value_type(),
  799. const allocator_type& __a = allocator_type())
  800. : _Base(__a, _S_check_init_len(__n, __a))
  801. { _M_fill_initialize(__value); }
  802. #endif
  803. /**
  804. * @brief %Deque copy constructor.
  805. * @param __x A %deque of identical element and allocator types.
  806. *
  807. * The newly-created %deque uses a copy of the allocator object used
  808. * by @a __x (unless the allocator traits dictate a different object).
  809. */
  810. deque(const deque& __x)
  811. : _Base(_Alloc_traits::_S_select_on_copy(__x._M_get_Tp_allocator()),
  812. __x.size())
  813. { std::__uninitialized_copy_a(__x.begin(), __x.end(),
  814. this->_M_impl._M_start,
  815. _M_get_Tp_allocator()); }
  816. #if __cplusplus >= 201103L
  817. /**
  818. * @brief %Deque move constructor.
  819. *
  820. * The newly-created %deque contains the exact contents of the
  821. * moved instance.
  822. * The contents of the moved instance are a valid, but unspecified
  823. * %deque.
  824. */
  825. deque(deque&&) = default;
  826. /// Copy constructor with alternative allocator
  827. deque(const deque& __x, const __type_identity_t<allocator_type>& __a)
  828. : _Base(__a, __x.size())
  829. { std::__uninitialized_copy_a(__x.begin(), __x.end(),
  830. this->_M_impl._M_start,
  831. _M_get_Tp_allocator()); }
  832. /// Move constructor with alternative allocator
  833. deque(deque&& __x, const __type_identity_t<allocator_type>& __a)
  834. : deque(std::move(__x), __a, typename _Alloc_traits::is_always_equal{})
  835. { }
  836. private:
  837. deque(deque&& __x, const allocator_type& __a, true_type)
  838. : _Base(std::move(__x), __a)
  839. { }
  840. deque(deque&& __x, const allocator_type& __a, false_type)
  841. : _Base(std::move(__x), __a, __x.size())
  842. {
  843. if (__x.get_allocator() != __a && !__x.empty())
  844. {
  845. std::__uninitialized_move_a(__x.begin(), __x.end(),
  846. this->_M_impl._M_start,
  847. _M_get_Tp_allocator());
  848. __x.clear();
  849. }
  850. }
  851. public:
  852. /**
  853. * @brief Builds a %deque from an initializer list.
  854. * @param __l An initializer_list.
  855. * @param __a An allocator object.
  856. *
  857. * Create a %deque consisting of copies of the elements in the
  858. * initializer_list @a __l.
  859. *
  860. * This will call the element type's copy constructor N times
  861. * (where N is __l.size()) and do no memory reallocation.
  862. */
  863. deque(initializer_list<value_type> __l,
  864. const allocator_type& __a = allocator_type())
  865. : _Base(__a)
  866. {
  867. _M_range_initialize(__l.begin(), __l.end(),
  868. random_access_iterator_tag());
  869. }
  870. #endif
  871. /**
  872. * @brief Builds a %deque from a range.
  873. * @param __first An input iterator.
  874. * @param __last An input iterator.
  875. * @param __a An allocator object.
  876. *
  877. * Create a %deque consisting of copies of the elements from [__first,
  878. * __last).
  879. *
  880. * If the iterators are forward, bidirectional, or random-access, then
  881. * this will call the elements' copy constructor N times (where N is
  882. * distance(__first,__last)) and do no memory reallocation. But if only
  883. * input iterators are used, then this will do at most 2N calls to the
  884. * copy constructor, and logN memory reallocations.
  885. */
  886. #if __cplusplus >= 201103L
  887. template<typename _InputIterator,
  888. typename = std::_RequireInputIter<_InputIterator>>
  889. deque(_InputIterator __first, _InputIterator __last,
  890. const allocator_type& __a = allocator_type())
  891. : _Base(__a)
  892. {
  893. _M_range_initialize(__first, __last,
  894. std::__iterator_category(__first));
  895. }
  896. #else
  897. template<typename _InputIterator>
  898. deque(_InputIterator __first, _InputIterator __last,
  899. const allocator_type& __a = allocator_type())
  900. : _Base(__a)
  901. {
  902. // Check whether it's an integral type. If so, it's not an iterator.
  903. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  904. _M_initialize_dispatch(__first, __last, _Integral());
  905. }
  906. #endif
  907. /**
  908. * The dtor only erases the elements, and note that if the elements
  909. * themselves are pointers, the pointed-to memory is not touched in any
  910. * way. Managing the pointer is the user's responsibility.
  911. */
  912. ~deque()
  913. { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); }
  914. /**
  915. * @brief %Deque assignment operator.
  916. * @param __x A %deque of identical element and allocator types.
  917. *
  918. * All the elements of @a x are copied.
  919. *
  920. * The newly-created %deque uses a copy of the allocator object used
  921. * by @a __x (unless the allocator traits dictate a different object).
  922. */
  923. deque&
  924. operator=(const deque& __x);
  925. #if __cplusplus >= 201103L
  926. /**
  927. * @brief %Deque move assignment operator.
  928. * @param __x A %deque of identical element and allocator types.
  929. *
  930. * The contents of @a __x are moved into this deque (without copying,
  931. * if the allocators permit it).
  932. * @a __x is a valid, but unspecified %deque.
  933. */
  934. deque&
  935. operator=(deque&& __x) noexcept(_Alloc_traits::_S_always_equal())
  936. {
  937. using __always_equal = typename _Alloc_traits::is_always_equal;
  938. _M_move_assign1(std::move(__x), __always_equal{});
  939. return *this;
  940. }
  941. /**
  942. * @brief Assigns an initializer list to a %deque.
  943. * @param __l An initializer_list.
  944. *
  945. * This function fills a %deque with copies of the elements in the
  946. * initializer_list @a __l.
  947. *
  948. * Note that the assignment completely changes the %deque and that the
  949. * resulting %deque's size is the same as the number of elements
  950. * assigned.
  951. */
  952. deque&
  953. operator=(initializer_list<value_type> __l)
  954. {
  955. _M_assign_aux(__l.begin(), __l.end(),
  956. random_access_iterator_tag());
  957. return *this;
  958. }
  959. #endif
  960. /**
  961. * @brief Assigns a given value to a %deque.
  962. * @param __n Number of elements to be assigned.
  963. * @param __val Value to be assigned.
  964. *
  965. * This function fills a %deque with @a n copies of the given
  966. * value. Note that the assignment completely changes the
  967. * %deque and that the resulting %deque's size is the same as
  968. * the number of elements assigned.
  969. */
  970. void
  971. assign(size_type __n, const value_type& __val)
  972. { _M_fill_assign(__n, __val); }
  973. /**
  974. * @brief Assigns a range to a %deque.
  975. * @param __first An input iterator.
  976. * @param __last An input iterator.
  977. *
  978. * This function fills a %deque with copies of the elements in the
  979. * range [__first,__last).
  980. *
  981. * Note that the assignment completely changes the %deque and that the
  982. * resulting %deque's size is the same as the number of elements
  983. * assigned.
  984. */
  985. #if __cplusplus >= 201103L
  986. template<typename _InputIterator,
  987. typename = std::_RequireInputIter<_InputIterator>>
  988. void
  989. assign(_InputIterator __first, _InputIterator __last)
  990. { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
  991. #else
  992. template<typename _InputIterator>
  993. void
  994. assign(_InputIterator __first, _InputIterator __last)
  995. {
  996. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  997. _M_assign_dispatch(__first, __last, _Integral());
  998. }
  999. #endif
  1000. #if __cplusplus >= 201103L
  1001. /**
  1002. * @brief Assigns an initializer list to a %deque.
  1003. * @param __l An initializer_list.
  1004. *
  1005. * This function fills a %deque with copies of the elements in the
  1006. * initializer_list @a __l.
  1007. *
  1008. * Note that the assignment completely changes the %deque and that the
  1009. * resulting %deque's size is the same as the number of elements
  1010. * assigned.
  1011. */
  1012. void
  1013. assign(initializer_list<value_type> __l)
  1014. { _M_assign_aux(__l.begin(), __l.end(), random_access_iterator_tag()); }
  1015. #endif
  1016. /// Get a copy of the memory allocation object.
  1017. _GLIBCXX_NODISCARD
  1018. allocator_type
  1019. get_allocator() const _GLIBCXX_NOEXCEPT
  1020. { return _Base::get_allocator(); }
  1021. // iterators
  1022. /**
  1023. * Returns a read/write iterator that points to the first element in the
  1024. * %deque. Iteration is done in ordinary element order.
  1025. */
  1026. _GLIBCXX_NODISCARD
  1027. iterator
  1028. begin() _GLIBCXX_NOEXCEPT
  1029. { return this->_M_impl._M_start; }
  1030. /**
  1031. * Returns a read-only (constant) iterator that points to the first
  1032. * element in the %deque. Iteration is done in ordinary element order.
  1033. */
  1034. _GLIBCXX_NODISCARD
  1035. const_iterator
  1036. begin() const _GLIBCXX_NOEXCEPT
  1037. { return this->_M_impl._M_start; }
  1038. /**
  1039. * Returns a read/write iterator that points one past the last
  1040. * element in the %deque. Iteration is done in ordinary
  1041. * element order.
  1042. */
  1043. _GLIBCXX_NODISCARD
  1044. iterator
  1045. end() _GLIBCXX_NOEXCEPT
  1046. { return this->_M_impl._M_finish; }
  1047. /**
  1048. * Returns a read-only (constant) iterator that points one past
  1049. * the last element in the %deque. Iteration is done in
  1050. * ordinary element order.
  1051. */
  1052. _GLIBCXX_NODISCARD
  1053. const_iterator
  1054. end() const _GLIBCXX_NOEXCEPT
  1055. { return this->_M_impl._M_finish; }
  1056. /**
  1057. * Returns a read/write reverse iterator that points to the
  1058. * last element in the %deque. Iteration is done in reverse
  1059. * element order.
  1060. */
  1061. _GLIBCXX_NODISCARD
  1062. reverse_iterator
  1063. rbegin() _GLIBCXX_NOEXCEPT
  1064. { return reverse_iterator(this->_M_impl._M_finish); }
  1065. /**
  1066. * Returns a read-only (constant) reverse iterator that points
  1067. * to the last element in the %deque. Iteration is done in
  1068. * reverse element order.
  1069. */
  1070. _GLIBCXX_NODISCARD
  1071. const_reverse_iterator
  1072. rbegin() const _GLIBCXX_NOEXCEPT
  1073. { return const_reverse_iterator(this->_M_impl._M_finish); }
  1074. /**
  1075. * Returns a read/write reverse iterator that points to one
  1076. * before the first element in the %deque. Iteration is done
  1077. * in reverse element order.
  1078. */
  1079. _GLIBCXX_NODISCARD
  1080. reverse_iterator
  1081. rend() _GLIBCXX_NOEXCEPT
  1082. { return reverse_iterator(this->_M_impl._M_start); }
  1083. /**
  1084. * Returns a read-only (constant) reverse iterator that points
  1085. * to one before the first element in the %deque. Iteration is
  1086. * done in reverse element order.
  1087. */
  1088. _GLIBCXX_NODISCARD
  1089. const_reverse_iterator
  1090. rend() const _GLIBCXX_NOEXCEPT
  1091. { return const_reverse_iterator(this->_M_impl._M_start); }
  1092. #if __cplusplus >= 201103L
  1093. /**
  1094. * Returns a read-only (constant) iterator that points to the first
  1095. * element in the %deque. Iteration is done in ordinary element order.
  1096. */
  1097. [[__nodiscard__]]
  1098. const_iterator
  1099. cbegin() const noexcept
  1100. { return this->_M_impl._M_start; }
  1101. /**
  1102. * Returns a read-only (constant) iterator that points one past
  1103. * the last element in the %deque. Iteration is done in
  1104. * ordinary element order.
  1105. */
  1106. [[__nodiscard__]]
  1107. const_iterator
  1108. cend() const noexcept
  1109. { return this->_M_impl._M_finish; }
  1110. /**
  1111. * Returns a read-only (constant) reverse iterator that points
  1112. * to the last element in the %deque. Iteration is done in
  1113. * reverse element order.
  1114. */
  1115. [[__nodiscard__]]
  1116. const_reverse_iterator
  1117. crbegin() const noexcept
  1118. { return const_reverse_iterator(this->_M_impl._M_finish); }
  1119. /**
  1120. * Returns a read-only (constant) reverse iterator that points
  1121. * to one before the first element in the %deque. Iteration is
  1122. * done in reverse element order.
  1123. */
  1124. [[__nodiscard__]]
  1125. const_reverse_iterator
  1126. crend() const noexcept
  1127. { return const_reverse_iterator(this->_M_impl._M_start); }
  1128. #endif
  1129. // [23.2.1.2] capacity
  1130. /** Returns the number of elements in the %deque. */
  1131. _GLIBCXX_NODISCARD
  1132. size_type
  1133. size() const _GLIBCXX_NOEXCEPT
  1134. { return this->_M_impl._M_finish - this->_M_impl._M_start; }
  1135. /** Returns the size() of the largest possible %deque. */
  1136. _GLIBCXX_NODISCARD
  1137. size_type
  1138. max_size() const _GLIBCXX_NOEXCEPT
  1139. { return _S_max_size(_M_get_Tp_allocator()); }
  1140. #if __cplusplus >= 201103L
  1141. /**
  1142. * @brief Resizes the %deque to the specified number of elements.
  1143. * @param __new_size Number of elements the %deque should contain.
  1144. *
  1145. * This function will %resize the %deque to the specified
  1146. * number of elements. If the number is smaller than the
  1147. * %deque's current size the %deque is truncated, otherwise
  1148. * default constructed elements are appended.
  1149. */
  1150. void
  1151. resize(size_type __new_size)
  1152. {
  1153. const size_type __len = size();
  1154. if (__new_size > __len)
  1155. _M_default_append(__new_size - __len);
  1156. else if (__new_size < __len)
  1157. _M_erase_at_end(this->_M_impl._M_start
  1158. + difference_type(__new_size));
  1159. }
  1160. /**
  1161. * @brief Resizes the %deque to the specified number of elements.
  1162. * @param __new_size Number of elements the %deque should contain.
  1163. * @param __x Data with which new elements should be populated.
  1164. *
  1165. * This function will %resize the %deque to the specified
  1166. * number of elements. If the number is smaller than the
  1167. * %deque's current size the %deque is truncated, otherwise the
  1168. * %deque is extended and new elements are populated with given
  1169. * data.
  1170. */
  1171. void
  1172. resize(size_type __new_size, const value_type& __x)
  1173. #else
  1174. /**
  1175. * @brief Resizes the %deque to the specified number of elements.
  1176. * @param __new_size Number of elements the %deque should contain.
  1177. * @param __x Data with which new elements should be populated.
  1178. *
  1179. * This function will %resize the %deque to the specified
  1180. * number of elements. If the number is smaller than the
  1181. * %deque's current size the %deque is truncated, otherwise the
  1182. * %deque is extended and new elements are populated with given
  1183. * data.
  1184. */
  1185. void
  1186. resize(size_type __new_size, value_type __x = value_type())
  1187. #endif
  1188. {
  1189. const size_type __len = size();
  1190. if (__new_size > __len)
  1191. _M_fill_insert(this->_M_impl._M_finish, __new_size - __len, __x);
  1192. else if (__new_size < __len)
  1193. _M_erase_at_end(this->_M_impl._M_start
  1194. + difference_type(__new_size));
  1195. }
  1196. #if __cplusplus >= 201103L
  1197. /** A non-binding request to reduce memory use. */
  1198. void
  1199. shrink_to_fit() noexcept
  1200. { _M_shrink_to_fit(); }
  1201. #endif
  1202. /**
  1203. * Returns true if the %deque is empty. (Thus begin() would
  1204. * equal end().)
  1205. */
  1206. _GLIBCXX_NODISCARD bool
  1207. empty() const _GLIBCXX_NOEXCEPT
  1208. { return this->_M_impl._M_finish == this->_M_impl._M_start; }
  1209. // element access
  1210. /**
  1211. * @brief Subscript access to the data contained in the %deque.
  1212. * @param __n The index of the element for which data should be
  1213. * accessed.
  1214. * @return Read/write reference to data.
  1215. *
  1216. * This operator allows for easy, array-style, data access.
  1217. * Note that data access with this operator is unchecked and
  1218. * out_of_range lookups are not defined. (For checked lookups
  1219. * see at().)
  1220. */
  1221. _GLIBCXX_NODISCARD
  1222. reference
  1223. operator[](size_type __n) _GLIBCXX_NOEXCEPT
  1224. {
  1225. __glibcxx_requires_subscript(__n);
  1226. return this->_M_impl._M_start[difference_type(__n)];
  1227. }
  1228. /**
  1229. * @brief Subscript access to the data contained in the %deque.
  1230. * @param __n The index of the element for which data should be
  1231. * accessed.
  1232. * @return Read-only (constant) reference to data.
  1233. *
  1234. * This operator allows for easy, array-style, data access.
  1235. * Note that data access with this operator is unchecked and
  1236. * out_of_range lookups are not defined. (For checked lookups
  1237. * see at().)
  1238. */
  1239. _GLIBCXX_NODISCARD
  1240. const_reference
  1241. operator[](size_type __n) const _GLIBCXX_NOEXCEPT
  1242. {
  1243. __glibcxx_requires_subscript(__n);
  1244. return this->_M_impl._M_start[difference_type(__n)];
  1245. }
  1246. protected:
  1247. /// Safety check used only from at().
  1248. void
  1249. _M_range_check(size_type __n) const
  1250. {
  1251. if (__n >= this->size())
  1252. __throw_out_of_range_fmt(__N("deque::_M_range_check: __n "
  1253. "(which is %zu)>= this->size() "
  1254. "(which is %zu)"),
  1255. __n, this->size());
  1256. }
  1257. public:
  1258. /**
  1259. * @brief Provides access to the data contained in the %deque.
  1260. * @param __n The index of the element for which data should be
  1261. * accessed.
  1262. * @return Read/write reference to data.
  1263. * @throw std::out_of_range If @a __n is an invalid index.
  1264. *
  1265. * This function provides for safer data access. The parameter
  1266. * is first checked that it is in the range of the deque. The
  1267. * function throws out_of_range if the check fails.
  1268. */
  1269. reference
  1270. at(size_type __n)
  1271. {
  1272. _M_range_check(__n);
  1273. return (*this)[__n];
  1274. }
  1275. /**
  1276. * @brief Provides access to the data contained in the %deque.
  1277. * @param __n The index of the element for which data should be
  1278. * accessed.
  1279. * @return Read-only (constant) reference to data.
  1280. * @throw std::out_of_range If @a __n is an invalid index.
  1281. *
  1282. * This function provides for safer data access. The parameter is first
  1283. * checked that it is in the range of the deque. The function throws
  1284. * out_of_range if the check fails.
  1285. */
  1286. const_reference
  1287. at(size_type __n) const
  1288. {
  1289. _M_range_check(__n);
  1290. return (*this)[__n];
  1291. }
  1292. /**
  1293. * Returns a read/write reference to the data at the first
  1294. * element of the %deque.
  1295. */
  1296. _GLIBCXX_NODISCARD
  1297. reference
  1298. front() _GLIBCXX_NOEXCEPT
  1299. {
  1300. __glibcxx_requires_nonempty();
  1301. return *begin();
  1302. }
  1303. /**
  1304. * Returns a read-only (constant) reference to the data at the first
  1305. * element of the %deque.
  1306. */
  1307. _GLIBCXX_NODISCARD
  1308. const_reference
  1309. front() const _GLIBCXX_NOEXCEPT
  1310. {
  1311. __glibcxx_requires_nonempty();
  1312. return *begin();
  1313. }
  1314. /**
  1315. * Returns a read/write reference to the data at the last element of the
  1316. * %deque.
  1317. */
  1318. _GLIBCXX_NODISCARD
  1319. reference
  1320. back() _GLIBCXX_NOEXCEPT
  1321. {
  1322. __glibcxx_requires_nonempty();
  1323. iterator __tmp = end();
  1324. --__tmp;
  1325. return *__tmp;
  1326. }
  1327. /**
  1328. * Returns a read-only (constant) reference to the data at the last
  1329. * element of the %deque.
  1330. */
  1331. _GLIBCXX_NODISCARD
  1332. const_reference
  1333. back() const _GLIBCXX_NOEXCEPT
  1334. {
  1335. __glibcxx_requires_nonempty();
  1336. const_iterator __tmp = end();
  1337. --__tmp;
  1338. return *__tmp;
  1339. }
  1340. // [23.2.1.2] modifiers
  1341. /**
  1342. * @brief Add data to the front of the %deque.
  1343. * @param __x Data to be added.
  1344. *
  1345. * This is a typical stack operation. The function creates an
  1346. * element at the front of the %deque and assigns the given
  1347. * data to it. Due to the nature of a %deque this operation
  1348. * can be done in constant time.
  1349. */
  1350. void
  1351. push_front(const value_type& __x)
  1352. {
  1353. if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
  1354. {
  1355. _Alloc_traits::construct(this->_M_impl,
  1356. this->_M_impl._M_start._M_cur - 1,
  1357. __x);
  1358. --this->_M_impl._M_start._M_cur;
  1359. }
  1360. else
  1361. _M_push_front_aux(__x);
  1362. }
  1363. #if __cplusplus >= 201103L
  1364. void
  1365. push_front(value_type&& __x)
  1366. { emplace_front(std::move(__x)); }
  1367. template<typename... _Args>
  1368. #if __cplusplus > 201402L
  1369. reference
  1370. #else
  1371. void
  1372. #endif
  1373. emplace_front(_Args&&... __args);
  1374. #endif
  1375. /**
  1376. * @brief Add data to the end of the %deque.
  1377. * @param __x Data to be added.
  1378. *
  1379. * This is a typical stack operation. The function creates an
  1380. * element at the end of the %deque and assigns the given data
  1381. * to it. Due to the nature of a %deque this operation can be
  1382. * done in constant time.
  1383. */
  1384. void
  1385. push_back(const value_type& __x)
  1386. {
  1387. if (this->_M_impl._M_finish._M_cur
  1388. != this->_M_impl._M_finish._M_last - 1)
  1389. {
  1390. _Alloc_traits::construct(this->_M_impl,
  1391. this->_M_impl._M_finish._M_cur, __x);
  1392. ++this->_M_impl._M_finish._M_cur;
  1393. }
  1394. else
  1395. _M_push_back_aux(__x);
  1396. }
  1397. #if __cplusplus >= 201103L
  1398. void
  1399. push_back(value_type&& __x)
  1400. { emplace_back(std::move(__x)); }
  1401. template<typename... _Args>
  1402. #if __cplusplus > 201402L
  1403. reference
  1404. #else
  1405. void
  1406. #endif
  1407. emplace_back(_Args&&... __args);
  1408. #endif
  1409. /**
  1410. * @brief Removes first element.
  1411. *
  1412. * This is a typical stack operation. It shrinks the %deque by one.
  1413. *
  1414. * Note that no data is returned, and if the first element's data is
  1415. * needed, it should be retrieved before pop_front() is called.
  1416. */
  1417. void
  1418. pop_front() _GLIBCXX_NOEXCEPT
  1419. {
  1420. __glibcxx_requires_nonempty();
  1421. if (this->_M_impl._M_start._M_cur
  1422. != this->_M_impl._M_start._M_last - 1)
  1423. {
  1424. _Alloc_traits::destroy(_M_get_Tp_allocator(),
  1425. this->_M_impl._M_start._M_cur);
  1426. ++this->_M_impl._M_start._M_cur;
  1427. }
  1428. else
  1429. _M_pop_front_aux();
  1430. }
  1431. /**
  1432. * @brief Removes last element.
  1433. *
  1434. * This is a typical stack operation. It shrinks the %deque by one.
  1435. *
  1436. * Note that no data is returned, and if the last element's data is
  1437. * needed, it should be retrieved before pop_back() is called.
  1438. */
  1439. void
  1440. pop_back() _GLIBCXX_NOEXCEPT
  1441. {
  1442. __glibcxx_requires_nonempty();
  1443. if (this->_M_impl._M_finish._M_cur
  1444. != this->_M_impl._M_finish._M_first)
  1445. {
  1446. --this->_M_impl._M_finish._M_cur;
  1447. _Alloc_traits::destroy(_M_get_Tp_allocator(),
  1448. this->_M_impl._M_finish._M_cur);
  1449. }
  1450. else
  1451. _M_pop_back_aux();
  1452. }
  1453. #if __cplusplus >= 201103L
  1454. /**
  1455. * @brief Inserts an object in %deque before specified iterator.
  1456. * @param __position A const_iterator into the %deque.
  1457. * @param __args Arguments.
  1458. * @return An iterator that points to the inserted data.
  1459. *
  1460. * This function will insert an object of type T constructed
  1461. * with T(std::forward<Args>(args)...) before the specified location.
  1462. */
  1463. template<typename... _Args>
  1464. iterator
  1465. emplace(const_iterator __position, _Args&&... __args);
  1466. /**
  1467. * @brief Inserts given value into %deque before specified iterator.
  1468. * @param __position A const_iterator into the %deque.
  1469. * @param __x Data to be inserted.
  1470. * @return An iterator that points to the inserted data.
  1471. *
  1472. * This function will insert a copy of the given value before the
  1473. * specified location.
  1474. */
  1475. iterator
  1476. insert(const_iterator __position, const value_type& __x);
  1477. #else
  1478. /**
  1479. * @brief Inserts given value into %deque before specified iterator.
  1480. * @param __position An iterator into the %deque.
  1481. * @param __x Data to be inserted.
  1482. * @return An iterator that points to the inserted data.
  1483. *
  1484. * This function will insert a copy of the given value before the
  1485. * specified location.
  1486. */
  1487. iterator
  1488. insert(iterator __position, const value_type& __x);
  1489. #endif
  1490. #if __cplusplus >= 201103L
  1491. /**
  1492. * @brief Inserts given rvalue into %deque before specified iterator.
  1493. * @param __position A const_iterator into the %deque.
  1494. * @param __x Data to be inserted.
  1495. * @return An iterator that points to the inserted data.
  1496. *
  1497. * This function will insert a copy of the given rvalue before the
  1498. * specified location.
  1499. */
  1500. iterator
  1501. insert(const_iterator __position, value_type&& __x)
  1502. { return emplace(__position, std::move(__x)); }
  1503. /**
  1504. * @brief Inserts an initializer list into the %deque.
  1505. * @param __p An iterator into the %deque.
  1506. * @param __l An initializer_list.
  1507. * @return An iterator that points to the inserted data.
  1508. *
  1509. * This function will insert copies of the data in the
  1510. * initializer_list @a __l into the %deque before the location
  1511. * specified by @a __p. This is known as <em>list insert</em>.
  1512. */
  1513. iterator
  1514. insert(const_iterator __p, initializer_list<value_type> __l)
  1515. {
  1516. auto __offset = __p - cbegin();
  1517. _M_range_insert_aux(__p._M_const_cast(), __l.begin(), __l.end(),
  1518. std::random_access_iterator_tag());
  1519. return begin() + __offset;
  1520. }
  1521. /**
  1522. * @brief Inserts a number of copies of given data into the %deque.
  1523. * @param __position A const_iterator into the %deque.
  1524. * @param __n Number of elements to be inserted.
  1525. * @param __x Data to be inserted.
  1526. * @return An iterator that points to the inserted data.
  1527. *
  1528. * This function will insert a specified number of copies of the given
  1529. * data before the location specified by @a __position.
  1530. */
  1531. iterator
  1532. insert(const_iterator __position, size_type __n, const value_type& __x)
  1533. {
  1534. difference_type __offset = __position - cbegin();
  1535. _M_fill_insert(__position._M_const_cast(), __n, __x);
  1536. return begin() + __offset;
  1537. }
  1538. #else
  1539. /**
  1540. * @brief Inserts a number of copies of given data into the %deque.
  1541. * @param __position An iterator into the %deque.
  1542. * @param __n Number of elements to be inserted.
  1543. * @param __x Data to be inserted.
  1544. *
  1545. * This function will insert a specified number of copies of the given
  1546. * data before the location specified by @a __position.
  1547. */
  1548. void
  1549. insert(iterator __position, size_type __n, const value_type& __x)
  1550. { _M_fill_insert(__position, __n, __x); }
  1551. #endif
  1552. #if __cplusplus >= 201103L
  1553. /**
  1554. * @brief Inserts a range into the %deque.
  1555. * @param __position A const_iterator into the %deque.
  1556. * @param __first An input iterator.
  1557. * @param __last An input iterator.
  1558. * @return An iterator that points to the inserted data.
  1559. *
  1560. * This function will insert copies of the data in the range
  1561. * [__first,__last) into the %deque before the location specified
  1562. * by @a __position. This is known as <em>range insert</em>.
  1563. */
  1564. template<typename _InputIterator,
  1565. typename = std::_RequireInputIter<_InputIterator>>
  1566. iterator
  1567. insert(const_iterator __position, _InputIterator __first,
  1568. _InputIterator __last)
  1569. {
  1570. difference_type __offset = __position - cbegin();
  1571. _M_range_insert_aux(__position._M_const_cast(), __first, __last,
  1572. std::__iterator_category(__first));
  1573. return begin() + __offset;
  1574. }
  1575. #else
  1576. /**
  1577. * @brief Inserts a range into the %deque.
  1578. * @param __position An iterator into the %deque.
  1579. * @param __first An input iterator.
  1580. * @param __last An input iterator.
  1581. *
  1582. * This function will insert copies of the data in the range
  1583. * [__first,__last) into the %deque before the location specified
  1584. * by @a __position. This is known as <em>range insert</em>.
  1585. */
  1586. template<typename _InputIterator>
  1587. void
  1588. insert(iterator __position, _InputIterator __first,
  1589. _InputIterator __last)
  1590. {
  1591. // Check whether it's an integral type. If so, it's not an iterator.
  1592. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  1593. _M_insert_dispatch(__position, __first, __last, _Integral());
  1594. }
  1595. #endif
  1596. /**
  1597. * @brief Remove element at given position.
  1598. * @param __position Iterator pointing to element to be erased.
  1599. * @return An iterator pointing to the next element (or end()).
  1600. *
  1601. * This function will erase the element at the given position and thus
  1602. * shorten the %deque by one.
  1603. *
  1604. * The user is cautioned that
  1605. * this function only erases the element, and that if the element is
  1606. * itself a pointer, the pointed-to memory is not touched in any way.
  1607. * Managing the pointer is the user's responsibility.
  1608. */
  1609. iterator
  1610. #if __cplusplus >= 201103L
  1611. erase(const_iterator __position)
  1612. #else
  1613. erase(iterator __position)
  1614. #endif
  1615. { return _M_erase(__position._M_const_cast()); }
  1616. /**
  1617. * @brief Remove a range of elements.
  1618. * @param __first Iterator pointing to the first element to be erased.
  1619. * @param __last Iterator pointing to one past the last element to be
  1620. * erased.
  1621. * @return An iterator pointing to the element pointed to by @a last
  1622. * prior to erasing (or end()).
  1623. *
  1624. * This function will erase the elements in the range
  1625. * [__first,__last) and shorten the %deque accordingly.
  1626. *
  1627. * The user is cautioned that
  1628. * this function only erases the elements, and that if the elements
  1629. * themselves are pointers, the pointed-to memory is not touched in any
  1630. * way. Managing the pointer is the user's responsibility.
  1631. */
  1632. iterator
  1633. #if __cplusplus >= 201103L
  1634. erase(const_iterator __first, const_iterator __last)
  1635. #else
  1636. erase(iterator __first, iterator __last)
  1637. #endif
  1638. { return _M_erase(__first._M_const_cast(), __last._M_const_cast()); }
  1639. /**
  1640. * @brief Swaps data with another %deque.
  1641. * @param __x A %deque of the same element and allocator types.
  1642. *
  1643. * This exchanges the elements between two deques in constant time.
  1644. * (Four pointers, so it should be quite fast.)
  1645. * Note that the global std::swap() function is specialized such that
  1646. * std::swap(d1,d2) will feed to this function.
  1647. *
  1648. * Whether the allocators are swapped depends on the allocator traits.
  1649. */
  1650. void
  1651. swap(deque& __x) _GLIBCXX_NOEXCEPT
  1652. {
  1653. #if __cplusplus >= 201103L
  1654. __glibcxx_assert(_Alloc_traits::propagate_on_container_swap::value
  1655. || _M_get_Tp_allocator() == __x._M_get_Tp_allocator());
  1656. #endif
  1657. _M_impl._M_swap_data(__x._M_impl);
  1658. _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
  1659. __x._M_get_Tp_allocator());
  1660. }
  1661. /**
  1662. * Erases all the elements. Note that this function only erases the
  1663. * elements, and that if the elements themselves are pointers, the
  1664. * pointed-to memory is not touched in any way. Managing the pointer is
  1665. * the user's responsibility.
  1666. */
  1667. void
  1668. clear() _GLIBCXX_NOEXCEPT
  1669. { _M_erase_at_end(begin()); }
  1670. protected:
  1671. // Internal constructor functions follow.
  1672. #if __cplusplus < 201103L
  1673. // called by the range constructor to implement [23.1.1]/9
  1674. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1675. // 438. Ambiguity in the "do the right thing" clause
  1676. template<typename _Integer>
  1677. void
  1678. _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
  1679. {
  1680. _M_initialize_map(_S_check_init_len(static_cast<size_type>(__n),
  1681. _M_get_Tp_allocator()));
  1682. _M_fill_initialize(__x);
  1683. }
  1684. // called by the range constructor to implement [23.1.1]/9
  1685. template<typename _InputIterator>
  1686. void
  1687. _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
  1688. __false_type)
  1689. {
  1690. _M_range_initialize(__first, __last,
  1691. std::__iterator_category(__first));
  1692. }
  1693. #endif
  1694. static size_t
  1695. _S_check_init_len(size_t __n, const allocator_type& __a)
  1696. {
  1697. if (__n > _S_max_size(__a))
  1698. __throw_length_error(
  1699. __N("cannot create std::deque larger than max_size()"));
  1700. return __n;
  1701. }
  1702. static size_type
  1703. _S_max_size(const _Tp_alloc_type& __a) _GLIBCXX_NOEXCEPT
  1704. {
  1705. const size_t __diffmax = __gnu_cxx::__numeric_traits<ptrdiff_t>::__max;
  1706. const size_t __allocmax = _Alloc_traits::max_size(__a);
  1707. return (std::min)(__diffmax, __allocmax);
  1708. }
  1709. // called by the second initialize_dispatch above
  1710. ///@{
  1711. /**
  1712. * @brief Fills the deque with whatever is in [first,last).
  1713. * @param __first An input iterator.
  1714. * @param __last An input iterator.
  1715. * @return Nothing.
  1716. *
  1717. * If the iterators are actually forward iterators (or better), then the
  1718. * memory layout can be done all at once. Else we move forward using
  1719. * push_back on each value from the iterator.
  1720. */
  1721. template<typename _InputIterator>
  1722. void
  1723. _M_range_initialize(_InputIterator __first, _InputIterator __last,
  1724. std::input_iterator_tag);
  1725. // called by the second initialize_dispatch above
  1726. template<typename _ForwardIterator>
  1727. void
  1728. _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
  1729. std::forward_iterator_tag);
  1730. ///@}
  1731. /**
  1732. * @brief Fills the %deque with copies of value.
  1733. * @param __value Initial value.
  1734. * @return Nothing.
  1735. * @pre _M_start and _M_finish have already been initialized,
  1736. * but none of the %deque's elements have yet been constructed.
  1737. *
  1738. * This function is called only when the user provides an explicit size
  1739. * (with or without an explicit exemplar value).
  1740. */
  1741. void
  1742. _M_fill_initialize(const value_type& __value);
  1743. #if __cplusplus >= 201103L
  1744. // called by deque(n).
  1745. void
  1746. _M_default_initialize();
  1747. #endif
  1748. // Internal assign functions follow. The *_aux functions do the actual
  1749. // assignment work for the range versions.
  1750. #if __cplusplus < 201103L
  1751. // called by the range assign to implement [23.1.1]/9
  1752. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1753. // 438. Ambiguity in the "do the right thing" clause
  1754. template<typename _Integer>
  1755. void
  1756. _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
  1757. { _M_fill_assign(__n, __val); }
  1758. // called by the range assign to implement [23.1.1]/9
  1759. template<typename _InputIterator>
  1760. void
  1761. _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
  1762. __false_type)
  1763. { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
  1764. #endif
  1765. // called by the second assign_dispatch above
  1766. template<typename _InputIterator>
  1767. void
  1768. _M_assign_aux(_InputIterator __first, _InputIterator __last,
  1769. std::input_iterator_tag);
  1770. // called by the second assign_dispatch above
  1771. template<typename _ForwardIterator>
  1772. void
  1773. _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
  1774. std::forward_iterator_tag)
  1775. {
  1776. const size_type __len = std::distance(__first, __last);
  1777. if (__len > size())
  1778. {
  1779. _ForwardIterator __mid = __first;
  1780. std::advance(__mid, size());
  1781. std::copy(__first, __mid, begin());
  1782. _M_range_insert_aux(end(), __mid, __last,
  1783. std::__iterator_category(__first));
  1784. }
  1785. else
  1786. _M_erase_at_end(std::copy(__first, __last, begin()));
  1787. }
  1788. // Called by assign(n,t), and the range assign when it turns out
  1789. // to be the same thing.
  1790. void
  1791. _M_fill_assign(size_type __n, const value_type& __val)
  1792. {
  1793. if (__n > size())
  1794. {
  1795. std::fill(begin(), end(), __val);
  1796. _M_fill_insert(end(), __n - size(), __val);
  1797. }
  1798. else
  1799. {
  1800. _M_erase_at_end(begin() + difference_type(__n));
  1801. std::fill(begin(), end(), __val);
  1802. }
  1803. }
  1804. ///@{
  1805. /// Helper functions for push_* and pop_*.
  1806. #if __cplusplus < 201103L
  1807. void _M_push_back_aux(const value_type&);
  1808. void _M_push_front_aux(const value_type&);
  1809. #else
  1810. template<typename... _Args>
  1811. void _M_push_back_aux(_Args&&... __args);
  1812. template<typename... _Args>
  1813. void _M_push_front_aux(_Args&&... __args);
  1814. #endif
  1815. void _M_pop_back_aux();
  1816. void _M_pop_front_aux();
  1817. ///@}
  1818. // Internal insert functions follow. The *_aux functions do the actual
  1819. // insertion work when all shortcuts fail.
  1820. #if __cplusplus < 201103L
  1821. // called by the range insert to implement [23.1.1]/9
  1822. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1823. // 438. Ambiguity in the "do the right thing" clause
  1824. template<typename _Integer>
  1825. void
  1826. _M_insert_dispatch(iterator __pos,
  1827. _Integer __n, _Integer __x, __true_type)
  1828. { _M_fill_insert(__pos, __n, __x); }
  1829. // called by the range insert to implement [23.1.1]/9
  1830. template<typename _InputIterator>
  1831. void
  1832. _M_insert_dispatch(iterator __pos,
  1833. _InputIterator __first, _InputIterator __last,
  1834. __false_type)
  1835. {
  1836. _M_range_insert_aux(__pos, __first, __last,
  1837. std::__iterator_category(__first));
  1838. }
  1839. #endif
  1840. // called by the second insert_dispatch above
  1841. template<typename _InputIterator>
  1842. void
  1843. _M_range_insert_aux(iterator __pos, _InputIterator __first,
  1844. _InputIterator __last, std::input_iterator_tag);
  1845. // called by the second insert_dispatch above
  1846. template<typename _ForwardIterator>
  1847. void
  1848. _M_range_insert_aux(iterator __pos, _ForwardIterator __first,
  1849. _ForwardIterator __last, std::forward_iterator_tag);
  1850. // Called by insert(p,n,x), and the range insert when it turns out to be
  1851. // the same thing. Can use fill functions in optimal situations,
  1852. // otherwise passes off to insert_aux(p,n,x).
  1853. void
  1854. _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
  1855. // called by insert(p,x)
  1856. #if __cplusplus < 201103L
  1857. iterator
  1858. _M_insert_aux(iterator __pos, const value_type& __x);
  1859. #else
  1860. template<typename... _Args>
  1861. iterator
  1862. _M_insert_aux(iterator __pos, _Args&&... __args);
  1863. #endif
  1864. // called by insert(p,n,x) via fill_insert
  1865. void
  1866. _M_insert_aux(iterator __pos, size_type __n, const value_type& __x);
  1867. // called by range_insert_aux for forward iterators
  1868. template<typename _ForwardIterator>
  1869. void
  1870. _M_insert_aux(iterator __pos,
  1871. _ForwardIterator __first, _ForwardIterator __last,
  1872. size_type __n);
  1873. // Internal erase functions follow.
  1874. void
  1875. _M_destroy_data_aux(iterator __first, iterator __last);
  1876. // Called by ~deque().
  1877. // NB: Doesn't deallocate the nodes.
  1878. template<typename _Alloc1>
  1879. void
  1880. _M_destroy_data(iterator __first, iterator __last, const _Alloc1&)
  1881. { _M_destroy_data_aux(__first, __last); }
  1882. void
  1883. _M_destroy_data(iterator __first, iterator __last,
  1884. const std::allocator<_Tp>&)
  1885. {
  1886. if (!__has_trivial_destructor(value_type))
  1887. _M_destroy_data_aux(__first, __last);
  1888. }
  1889. // Called by erase(q1, q2).
  1890. void
  1891. _M_erase_at_begin(iterator __pos)
  1892. {
  1893. _M_destroy_data(begin(), __pos, _M_get_Tp_allocator());
  1894. _M_destroy_nodes(this->_M_impl._M_start._M_node, __pos._M_node);
  1895. this->_M_impl._M_start = __pos;
  1896. }
  1897. // Called by erase(q1, q2), resize(), clear(), _M_assign_aux,
  1898. // _M_fill_assign, operator=.
  1899. void
  1900. _M_erase_at_end(iterator __pos)
  1901. {
  1902. _M_destroy_data(__pos, end(), _M_get_Tp_allocator());
  1903. _M_destroy_nodes(__pos._M_node + 1,
  1904. this->_M_impl._M_finish._M_node + 1);
  1905. this->_M_impl._M_finish = __pos;
  1906. }
  1907. iterator
  1908. _M_erase(iterator __pos);
  1909. iterator
  1910. _M_erase(iterator __first, iterator __last);
  1911. #if __cplusplus >= 201103L
  1912. // Called by resize(sz).
  1913. void
  1914. _M_default_append(size_type __n);
  1915. bool
  1916. _M_shrink_to_fit();
  1917. #endif
  1918. ///@{
  1919. /// Memory-handling helpers for the previous internal insert functions.
  1920. iterator
  1921. _M_reserve_elements_at_front(size_type __n)
  1922. {
  1923. const size_type __vacancies = this->_M_impl._M_start._M_cur
  1924. - this->_M_impl._M_start._M_first;
  1925. if (__n > __vacancies)
  1926. _M_new_elements_at_front(__n - __vacancies);
  1927. return this->_M_impl._M_start - difference_type(__n);
  1928. }
  1929. iterator
  1930. _M_reserve_elements_at_back(size_type __n)
  1931. {
  1932. const size_type __vacancies = (this->_M_impl._M_finish._M_last
  1933. - this->_M_impl._M_finish._M_cur) - 1;
  1934. if (__n > __vacancies)
  1935. _M_new_elements_at_back(__n - __vacancies);
  1936. return this->_M_impl._M_finish + difference_type(__n);
  1937. }
  1938. void
  1939. _M_new_elements_at_front(size_type __new_elements);
  1940. void
  1941. _M_new_elements_at_back(size_type __new_elements);
  1942. ///@}
  1943. ///@{
  1944. /**
  1945. * @brief Memory-handling helpers for the major %map.
  1946. *
  1947. * Makes sure the _M_map has space for new nodes. Does not
  1948. * actually add the nodes. Can invalidate _M_map pointers.
  1949. * (And consequently, %deque iterators.)
  1950. */
  1951. void
  1952. _M_reserve_map_at_back(size_type __nodes_to_add = 1)
  1953. {
  1954. if (__nodes_to_add + 1 > this->_M_impl._M_map_size
  1955. - (this->_M_impl._M_finish._M_node - this->_M_impl._M_map))
  1956. _M_reallocate_map(__nodes_to_add, false);
  1957. }
  1958. void
  1959. _M_reserve_map_at_front(size_type __nodes_to_add = 1)
  1960. {
  1961. if (__nodes_to_add > size_type(this->_M_impl._M_start._M_node
  1962. - this->_M_impl._M_map))
  1963. _M_reallocate_map(__nodes_to_add, true);
  1964. }
  1965. void
  1966. _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front);
  1967. ///@}
  1968. #if __cplusplus >= 201103L
  1969. // Constant-time, nothrow move assignment when source object's memory
  1970. // can be moved because the allocators are equal.
  1971. void
  1972. _M_move_assign1(deque&& __x, /* always equal: */ true_type) noexcept
  1973. {
  1974. this->_M_impl._M_swap_data(__x._M_impl);
  1975. __x.clear();
  1976. std::__alloc_on_move(_M_get_Tp_allocator(), __x._M_get_Tp_allocator());
  1977. }
  1978. // When the allocators are not equal the operation could throw, because
  1979. // we might need to allocate a new map for __x after moving from it
  1980. // or we might need to allocate new elements for *this.
  1981. void
  1982. _M_move_assign1(deque&& __x, /* always equal: */ false_type)
  1983. {
  1984. if (_M_get_Tp_allocator() == __x._M_get_Tp_allocator())
  1985. return _M_move_assign1(std::move(__x), true_type());
  1986. constexpr bool __move_storage =
  1987. _Alloc_traits::_S_propagate_on_move_assign();
  1988. _M_move_assign2(std::move(__x), __bool_constant<__move_storage>());
  1989. }
  1990. // Destroy all elements and deallocate all memory, then replace
  1991. // with elements created from __args.
  1992. template<typename... _Args>
  1993. void
  1994. _M_replace_map(_Args&&... __args)
  1995. {
  1996. // Create new data first, so if allocation fails there are no effects.
  1997. deque __newobj(std::forward<_Args>(__args)...);
  1998. // Free existing storage using existing allocator.
  1999. clear();
  2000. _M_deallocate_node(*begin()._M_node); // one node left after clear()
  2001. _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
  2002. this->_M_impl._M_map = nullptr;
  2003. this->_M_impl._M_map_size = 0;
  2004. // Take ownership of replacement memory.
  2005. this->_M_impl._M_swap_data(__newobj._M_impl);
  2006. }
  2007. // Do move assignment when the allocator propagates.
  2008. void
  2009. _M_move_assign2(deque&& __x, /* propagate: */ true_type)
  2010. {
  2011. // Make a copy of the original allocator state.
  2012. auto __alloc = __x._M_get_Tp_allocator();
  2013. // The allocator propagates so storage can be moved from __x,
  2014. // leaving __x in a valid empty state with a moved-from allocator.
  2015. _M_replace_map(std::move(__x));
  2016. // Move the corresponding allocator state too.
  2017. _M_get_Tp_allocator() = std::move(__alloc);
  2018. }
  2019. // Do move assignment when it may not be possible to move source
  2020. // object's memory, resulting in a linear-time operation.
  2021. void
  2022. _M_move_assign2(deque&& __x, /* propagate: */ false_type)
  2023. {
  2024. if (__x._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
  2025. {
  2026. // The allocators are equal so storage can be moved from __x,
  2027. // leaving __x in a valid empty state with its current allocator.
  2028. _M_replace_map(std::move(__x), __x.get_allocator());
  2029. }
  2030. else
  2031. {
  2032. // The rvalue's allocator cannot be moved and is not equal,
  2033. // so we need to individually move each element.
  2034. _M_assign_aux(std::make_move_iterator(__x.begin()),
  2035. std::make_move_iterator(__x.end()),
  2036. std::random_access_iterator_tag());
  2037. __x.clear();
  2038. }
  2039. }
  2040. #endif
  2041. };
  2042. #if __cpp_deduction_guides >= 201606
  2043. template<typename _InputIterator, typename _ValT
  2044. = typename iterator_traits<_InputIterator>::value_type,
  2045. typename _Allocator = allocator<_ValT>,
  2046. typename = _RequireInputIter<_InputIterator>,
  2047. typename = _RequireAllocator<_Allocator>>
  2048. deque(_InputIterator, _InputIterator, _Allocator = _Allocator())
  2049. -> deque<_ValT, _Allocator>;
  2050. #endif
  2051. /**
  2052. * @brief Deque equality comparison.
  2053. * @param __x A %deque.
  2054. * @param __y A %deque of the same type as @a __x.
  2055. * @return True iff the size and elements of the deques are equal.
  2056. *
  2057. * This is an equivalence relation. It is linear in the size of the
  2058. * deques. Deques are considered equivalent if their sizes are equal,
  2059. * and if corresponding elements compare equal.
  2060. */
  2061. template<typename _Tp, typename _Alloc>
  2062. _GLIBCXX_NODISCARD
  2063. inline bool
  2064. operator==(const deque<_Tp, _Alloc>& __x, const deque<_Tp, _Alloc>& __y)
  2065. { return __x.size() == __y.size()
  2066. && std::equal(__x.begin(), __x.end(), __y.begin()); }
  2067. #if __cpp_lib_three_way_comparison
  2068. /**
  2069. * @brief Deque ordering relation.
  2070. * @param __x A `deque`.
  2071. * @param __y A `deque` of the same type as `__x`.
  2072. * @return A value indicating whether `__x` is less than, equal to,
  2073. * greater than, or incomparable with `__y`.
  2074. *
  2075. * See `std::lexicographical_compare_three_way()` for how the determination
  2076. * is made. This operator is used to synthesize relational operators like
  2077. * `<` and `>=` etc.
  2078. */
  2079. template<typename _Tp, typename _Alloc>
  2080. [[nodiscard]]
  2081. inline __detail::__synth3way_t<_Tp>
  2082. operator<=>(const deque<_Tp, _Alloc>& __x, const deque<_Tp, _Alloc>& __y)
  2083. {
  2084. return std::lexicographical_compare_three_way(__x.begin(), __x.end(),
  2085. __y.begin(), __y.end(),
  2086. __detail::__synth3way);
  2087. }
  2088. #else
  2089. /**
  2090. * @brief Deque ordering relation.
  2091. * @param __x A %deque.
  2092. * @param __y A %deque of the same type as @a __x.
  2093. * @return True iff @a x is lexicographically less than @a __y.
  2094. *
  2095. * This is a total ordering relation. It is linear in the size of the
  2096. * deques. The elements must be comparable with @c <.
  2097. *
  2098. * See std::lexicographical_compare() for how the determination is made.
  2099. */
  2100. template<typename _Tp, typename _Alloc>
  2101. _GLIBCXX_NODISCARD
  2102. inline bool
  2103. operator<(const deque<_Tp, _Alloc>& __x, const deque<_Tp, _Alloc>& __y)
  2104. { return std::lexicographical_compare(__x.begin(), __x.end(),
  2105. __y.begin(), __y.end()); }
  2106. /// Based on operator==
  2107. template<typename _Tp, typename _Alloc>
  2108. _GLIBCXX_NODISCARD
  2109. inline bool
  2110. operator!=(const deque<_Tp, _Alloc>& __x, const deque<_Tp, _Alloc>& __y)
  2111. { return !(__x == __y); }
  2112. /// Based on operator<
  2113. template<typename _Tp, typename _Alloc>
  2114. _GLIBCXX_NODISCARD
  2115. inline bool
  2116. operator>(const deque<_Tp, _Alloc>& __x, const deque<_Tp, _Alloc>& __y)
  2117. { return __y < __x; }
  2118. /// Based on operator<
  2119. template<typename _Tp, typename _Alloc>
  2120. _GLIBCXX_NODISCARD
  2121. inline bool
  2122. operator<=(const deque<_Tp, _Alloc>& __x, const deque<_Tp, _Alloc>& __y)
  2123. { return !(__y < __x); }
  2124. /// Based on operator<
  2125. template<typename _Tp, typename _Alloc>
  2126. _GLIBCXX_NODISCARD
  2127. inline bool
  2128. operator>=(const deque<_Tp, _Alloc>& __x, const deque<_Tp, _Alloc>& __y)
  2129. { return !(__x < __y); }
  2130. #endif // three-way comparison
  2131. /// See std::deque::swap().
  2132. template<typename _Tp, typename _Alloc>
  2133. inline void
  2134. swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y)
  2135. _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
  2136. { __x.swap(__y); }
  2137. #undef _GLIBCXX_DEQUE_BUF_SIZE
  2138. _GLIBCXX_END_NAMESPACE_CONTAINER
  2139. #if __cplusplus >= 201103L
  2140. // std::allocator is safe, but it is not the only allocator
  2141. // for which this is valid.
  2142. template<class _Tp>
  2143. struct __is_bitwise_relocatable<_GLIBCXX_STD_C::deque<_Tp>>
  2144. : true_type { };
  2145. #endif
  2146. _GLIBCXX_END_NAMESPACE_VERSION
  2147. } // namespace std
  2148. #endif /* _STL_DEQUE_H */