stl_deque.h 78 KB

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