stl_deque.h 75 KB

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