stl_deque.h 77 KB

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