deque 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_DEQUE
  10. #define _LIBCPP_DEQUE
  11. /*
  12. deque synopsis
  13. namespace std
  14. {
  15. template <class T, class Allocator = allocator<T> >
  16. class deque
  17. {
  18. public:
  19. // types:
  20. typedef T value_type;
  21. typedef Allocator allocator_type;
  22. typedef typename allocator_type::reference reference;
  23. typedef typename allocator_type::const_reference const_reference;
  24. typedef implementation-defined iterator;
  25. typedef implementation-defined const_iterator;
  26. typedef typename allocator_type::size_type size_type;
  27. typedef typename allocator_type::difference_type difference_type;
  28. typedef typename allocator_type::pointer pointer;
  29. typedef typename allocator_type::const_pointer const_pointer;
  30. typedef std::reverse_iterator<iterator> reverse_iterator;
  31. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  32. // construct/copy/destroy:
  33. deque() noexcept(is_nothrow_default_constructible<allocator_type>::value);
  34. explicit deque(const allocator_type& a);
  35. explicit deque(size_type n);
  36. explicit deque(size_type n, const allocator_type& a); // C++14
  37. deque(size_type n, const value_type& v);
  38. deque(size_type n, const value_type& v, const allocator_type& a);
  39. template <class InputIterator>
  40. deque(InputIterator f, InputIterator l);
  41. template <class InputIterator>
  42. deque(InputIterator f, InputIterator l, const allocator_type& a);
  43. deque(const deque& c);
  44. deque(deque&& c)
  45. noexcept(is_nothrow_move_constructible<allocator_type>::value);
  46. deque(initializer_list<value_type> il, const Allocator& a = allocator_type());
  47. deque(const deque& c, const allocator_type& a);
  48. deque(deque&& c, const allocator_type& a);
  49. ~deque();
  50. deque& operator=(const deque& c);
  51. deque& operator=(deque&& c)
  52. noexcept(
  53. allocator_type::propagate_on_container_move_assignment::value &&
  54. is_nothrow_move_assignable<allocator_type>::value);
  55. deque& operator=(initializer_list<value_type> il);
  56. template <class InputIterator>
  57. void assign(InputIterator f, InputIterator l);
  58. void assign(size_type n, const value_type& v);
  59. void assign(initializer_list<value_type> il);
  60. allocator_type get_allocator() const noexcept;
  61. // iterators:
  62. iterator begin() noexcept;
  63. const_iterator begin() const noexcept;
  64. iterator end() noexcept;
  65. const_iterator end() const noexcept;
  66. reverse_iterator rbegin() noexcept;
  67. const_reverse_iterator rbegin() const noexcept;
  68. reverse_iterator rend() noexcept;
  69. const_reverse_iterator rend() const noexcept;
  70. const_iterator cbegin() const noexcept;
  71. const_iterator cend() const noexcept;
  72. const_reverse_iterator crbegin() const noexcept;
  73. const_reverse_iterator crend() const noexcept;
  74. // capacity:
  75. size_type size() const noexcept;
  76. size_type max_size() const noexcept;
  77. void resize(size_type n);
  78. void resize(size_type n, const value_type& v);
  79. void shrink_to_fit();
  80. bool empty() const noexcept;
  81. // element access:
  82. reference operator[](size_type i);
  83. const_reference operator[](size_type i) const;
  84. reference at(size_type i);
  85. const_reference at(size_type i) const;
  86. reference front();
  87. const_reference front() const;
  88. reference back();
  89. const_reference back() const;
  90. // modifiers:
  91. void push_front(const value_type& v);
  92. void push_front(value_type&& v);
  93. void push_back(const value_type& v);
  94. void push_back(value_type&& v);
  95. template <class... Args> reference emplace_front(Args&&... args); // reference in C++17
  96. template <class... Args> reference emplace_back(Args&&... args); // reference in C++17
  97. template <class... Args> iterator emplace(const_iterator p, Args&&... args);
  98. iterator insert(const_iterator p, const value_type& v);
  99. iterator insert(const_iterator p, value_type&& v);
  100. iterator insert(const_iterator p, size_type n, const value_type& v);
  101. template <class InputIterator>
  102. iterator insert(const_iterator p, InputIterator f, InputIterator l);
  103. iterator insert(const_iterator p, initializer_list<value_type> il);
  104. void pop_front();
  105. void pop_back();
  106. iterator erase(const_iterator p);
  107. iterator erase(const_iterator f, const_iterator l);
  108. void swap(deque& c)
  109. noexcept(allocator_traits<allocator_type>::is_always_equal::value); // C++17
  110. void clear() noexcept;
  111. };
  112. template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
  113. deque(InputIterator, InputIterator, Allocator = Allocator())
  114. -> deque<typename iterator_traits<InputIterator>::value_type, Allocator>; // C++17
  115. template <class T, class Allocator>
  116. bool operator==(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
  117. template <class T, class Allocator>
  118. bool operator< (const deque<T,Allocator>& x, const deque<T,Allocator>& y);
  119. template <class T, class Allocator>
  120. bool operator!=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
  121. template <class T, class Allocator>
  122. bool operator> (const deque<T,Allocator>& x, const deque<T,Allocator>& y);
  123. template <class T, class Allocator>
  124. bool operator>=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
  125. template <class T, class Allocator>
  126. bool operator<=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
  127. // specialized algorithms:
  128. template <class T, class Allocator>
  129. void swap(deque<T,Allocator>& x, deque<T,Allocator>& y)
  130. noexcept(noexcept(x.swap(y)));
  131. template <class T, class Allocator, class U>
  132. typename deque<T, Allocator>::size_type
  133. erase(deque<T, Allocator>& c, const U& value); // C++20
  134. template <class T, class Allocator, class Predicate>
  135. typename deque<T, Allocator>::size_type
  136. erase_if(deque<T, Allocator>& c, Predicate pred); // C++20
  137. } // std
  138. */
  139. #include <__algorithm/copy.h>
  140. #include <__algorithm/copy_backward.h>
  141. #include <__algorithm/equal.h>
  142. #include <__algorithm/fill_n.h>
  143. #include <__algorithm/lexicographical_compare.h>
  144. #include <__algorithm/min.h>
  145. #include <__algorithm/remove.h>
  146. #include <__algorithm/remove_if.h>
  147. #include <__algorithm/unwrap_iter.h>
  148. #include <__assert> // all public C++ headers provide the assertion handler
  149. #include <__config>
  150. #include <__format/enable_insertable.h>
  151. #include <__iterator/iterator_traits.h>
  152. #include <__iterator/next.h>
  153. #include <__iterator/prev.h>
  154. #include <__iterator/reverse_iterator.h>
  155. #include <__iterator/segmented_iterator.h>
  156. #include <__memory/allocator_destructor.h>
  157. #include <__memory/pointer_traits.h>
  158. #include <__memory/temp_value.h>
  159. #include <__memory/unique_ptr.h>
  160. #include <__memory_resource/polymorphic_allocator.h>
  161. #include <__split_buffer>
  162. #include <__type_traits/is_allocator.h>
  163. #include <__utility/forward.h>
  164. #include <__utility/move.h>
  165. #include <__utility/swap.h>
  166. #include <limits>
  167. #include <stdexcept>
  168. #include <type_traits>
  169. #include <version>
  170. // standard-mandated includes
  171. // [iterator.range]
  172. #include <__iterator/access.h>
  173. #include <__iterator/data.h>
  174. #include <__iterator/empty.h>
  175. #include <__iterator/reverse_access.h>
  176. #include <__iterator/size.h>
  177. // [deque.syn]
  178. #include <compare>
  179. #include <initializer_list>
  180. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  181. # pragma GCC system_header
  182. #endif
  183. _LIBCPP_PUSH_MACROS
  184. #include <__undef_macros>
  185. _LIBCPP_BEGIN_NAMESPACE_STD
  186. template <class _Tp, class _Allocator = allocator<_Tp> > class _LIBCPP_TEMPLATE_VIS deque;
  187. template <class _ValueType, class _DiffType>
  188. struct __deque_block_size {
  189. static const _DiffType __buf_size = 64 * sizeof(void*);
  190. static const _DiffType value = (__buf_size / sizeof(_ValueType)) > 2 ? (__buf_size / sizeof(_ValueType)) : 2;
  191. //static const _DiffType value = sizeof(_ValueType) < 256 ? 4096 / sizeof(_ValueType) : 16;
  192. };
  193. template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
  194. class _DiffType, _DiffType _BS =
  195. #ifdef _LIBCPP_ABI_INCOMPLETE_TYPES_IN_DEQUE
  196. // Keep template parameter to avoid changing all template declarations thoughout
  197. // this file.
  198. 0
  199. #else
  200. __deque_block_size<_ValueType, _DiffType>::value
  201. #endif
  202. >
  203. class _LIBCPP_TEMPLATE_VIS __deque_iterator
  204. {
  205. typedef _MapPointer __map_iterator;
  206. public:
  207. typedef _Pointer pointer;
  208. typedef _DiffType difference_type;
  209. private:
  210. __map_iterator __m_iter_;
  211. pointer __ptr_;
  212. static const difference_type __block_size;
  213. public:
  214. typedef _ValueType value_type;
  215. typedef random_access_iterator_tag iterator_category;
  216. typedef _Reference reference;
  217. _LIBCPP_HIDE_FROM_ABI __deque_iterator() _NOEXCEPT
  218. #if _LIBCPP_STD_VER > 11
  219. : __m_iter_(nullptr), __ptr_(nullptr)
  220. #endif
  221. {}
  222. template <class _Pp, class _Rp, class _MP>
  223. _LIBCPP_HIDE_FROM_ABI
  224. __deque_iterator(const __deque_iterator<value_type, _Pp, _Rp, _MP, difference_type, _BS>& __it,
  225. typename enable_if<is_convertible<_Pp, pointer>::value>::type* = 0) _NOEXCEPT
  226. : __m_iter_(__it.__m_iter_), __ptr_(__it.__ptr_) {}
  227. _LIBCPP_HIDE_FROM_ABI reference operator*() const {return *__ptr_;}
  228. _LIBCPP_HIDE_FROM_ABI pointer operator->() const {return __ptr_;}
  229. _LIBCPP_HIDE_FROM_ABI __deque_iterator& operator++()
  230. {
  231. if (++__ptr_ - *__m_iter_ == __block_size)
  232. {
  233. ++__m_iter_;
  234. __ptr_ = *__m_iter_;
  235. }
  236. return *this;
  237. }
  238. _LIBCPP_HIDE_FROM_ABI __deque_iterator operator++(int)
  239. {
  240. __deque_iterator __tmp = *this;
  241. ++(*this);
  242. return __tmp;
  243. }
  244. _LIBCPP_HIDE_FROM_ABI __deque_iterator& operator--()
  245. {
  246. if (__ptr_ == *__m_iter_)
  247. {
  248. --__m_iter_;
  249. __ptr_ = *__m_iter_ + __block_size;
  250. }
  251. --__ptr_;
  252. return *this;
  253. }
  254. _LIBCPP_HIDE_FROM_ABI __deque_iterator operator--(int)
  255. {
  256. __deque_iterator __tmp = *this;
  257. --(*this);
  258. return __tmp;
  259. }
  260. _LIBCPP_HIDE_FROM_ABI __deque_iterator& operator+=(difference_type __n)
  261. {
  262. if (__n != 0)
  263. {
  264. __n += __ptr_ - *__m_iter_;
  265. if (__n > 0)
  266. {
  267. __m_iter_ += __n / __block_size;
  268. __ptr_ = *__m_iter_ + __n % __block_size;
  269. }
  270. else // (__n < 0)
  271. {
  272. difference_type __z = __block_size - 1 - __n;
  273. __m_iter_ -= __z / __block_size;
  274. __ptr_ = *__m_iter_ + (__block_size - 1 - __z % __block_size);
  275. }
  276. }
  277. return *this;
  278. }
  279. _LIBCPP_HIDE_FROM_ABI __deque_iterator& operator-=(difference_type __n)
  280. {
  281. return *this += -__n;
  282. }
  283. _LIBCPP_HIDE_FROM_ABI __deque_iterator operator+(difference_type __n) const
  284. {
  285. __deque_iterator __t(*this);
  286. __t += __n;
  287. return __t;
  288. }
  289. _LIBCPP_HIDE_FROM_ABI __deque_iterator operator-(difference_type __n) const
  290. {
  291. __deque_iterator __t(*this);
  292. __t -= __n;
  293. return __t;
  294. }
  295. _LIBCPP_HIDE_FROM_ABI
  296. friend __deque_iterator operator+(difference_type __n, const __deque_iterator& __it)
  297. {return __it + __n;}
  298. _LIBCPP_HIDE_FROM_ABI
  299. friend difference_type operator-(const __deque_iterator& __x, const __deque_iterator& __y)
  300. {
  301. if (__x != __y)
  302. return (__x.__m_iter_ - __y.__m_iter_) * __block_size
  303. + (__x.__ptr_ - *__x.__m_iter_)
  304. - (__y.__ptr_ - *__y.__m_iter_);
  305. return 0;
  306. }
  307. _LIBCPP_HIDE_FROM_ABI reference operator[](difference_type __n) const
  308. {return *(*this + __n);}
  309. _LIBCPP_HIDE_FROM_ABI friend
  310. bool operator==(const __deque_iterator& __x, const __deque_iterator& __y)
  311. {return __x.__ptr_ == __y.__ptr_;}
  312. _LIBCPP_HIDE_FROM_ABI friend
  313. bool operator!=(const __deque_iterator& __x, const __deque_iterator& __y)
  314. {return !(__x == __y);}
  315. _LIBCPP_HIDE_FROM_ABI friend
  316. bool operator<(const __deque_iterator& __x, const __deque_iterator& __y)
  317. {return __x.__m_iter_ < __y.__m_iter_ ||
  318. (__x.__m_iter_ == __y.__m_iter_ && __x.__ptr_ < __y.__ptr_);}
  319. _LIBCPP_HIDE_FROM_ABI friend
  320. bool operator>(const __deque_iterator& __x, const __deque_iterator& __y)
  321. {return __y < __x;}
  322. _LIBCPP_HIDE_FROM_ABI friend
  323. bool operator<=(const __deque_iterator& __x, const __deque_iterator& __y)
  324. {return !(__y < __x);}
  325. _LIBCPP_HIDE_FROM_ABI friend
  326. bool operator>=(const __deque_iterator& __x, const __deque_iterator& __y)
  327. {return !(__x < __y);}
  328. private:
  329. _LIBCPP_HIDE_FROM_ABI explicit __deque_iterator(__map_iterator __m, pointer __p) _NOEXCEPT
  330. : __m_iter_(__m), __ptr_(__p) {}
  331. template <class _Tp, class _Ap> friend class _LIBCPP_TEMPLATE_VIS deque;
  332. template <class _Vp, class _Pp, class _Rp, class _MP, class _Dp, _Dp>
  333. friend class _LIBCPP_TEMPLATE_VIS __deque_iterator;
  334. template <class>
  335. friend struct __segmented_iterator_traits;
  336. };
  337. template <class _ValueType, class _Pointer, class _Reference, class _MapPointer, class _DiffType, _DiffType _BlockSize>
  338. struct __segmented_iterator_traits<
  339. __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer, _DiffType, _BlockSize> > {
  340. private:
  341. using _Iterator = __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer, _DiffType, _BlockSize>;
  342. public:
  343. using __is_segmented_iterator = true_type;
  344. using __segment_iterator = _MapPointer;
  345. using __local_iterator = _Pointer;
  346. static _LIBCPP_HIDE_FROM_ABI __segment_iterator __segment(_Iterator __iter) { return __iter.__m_iter_; }
  347. static _LIBCPP_HIDE_FROM_ABI __local_iterator __local(_Iterator __iter) { return __iter.__ptr_; }
  348. static _LIBCPP_HIDE_FROM_ABI __local_iterator __begin(__segment_iterator __iter) { return *__iter; }
  349. static _LIBCPP_HIDE_FROM_ABI __local_iterator __end(__segment_iterator __iter) {
  350. return *__iter + _Iterator::__block_size;
  351. }
  352. static _LIBCPP_HIDE_FROM_ABI _Iterator __compose(__segment_iterator __segment, __local_iterator __local) {
  353. if (__local == __end(__segment)) {
  354. ++__segment;
  355. return _Iterator(__segment, *__segment);
  356. }
  357. return _Iterator(__segment, __local);
  358. }
  359. };
  360. template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
  361. class _DiffType, _DiffType _BlockSize>
  362. const _DiffType __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer,
  363. _DiffType, _BlockSize>::__block_size =
  364. __deque_block_size<_ValueType, _DiffType>::value;
  365. template <class _Tp, class _Allocator /*= allocator<_Tp>*/>
  366. class _LIBCPP_TEMPLATE_VIS deque
  367. {
  368. public:
  369. // types:
  370. using value_type = _Tp;
  371. static_assert((is_same<typename _Allocator::value_type, value_type>::value),
  372. "Allocator::value_type must be same type as value_type");
  373. using allocator_type = _Allocator;
  374. using __alloc_traits = allocator_traits<allocator_type>;
  375. using size_type = typename __alloc_traits::size_type;
  376. using difference_type = typename __alloc_traits::difference_type;
  377. using pointer = typename __alloc_traits::pointer;
  378. using const_pointer = typename __alloc_traits::const_pointer;
  379. using __pointer_allocator = __rebind_alloc<__alloc_traits, pointer>;
  380. using __const_pointer_allocator = __rebind_alloc<__alloc_traits, const_pointer>;
  381. using __map = __split_buffer<pointer, __pointer_allocator>;
  382. using __map_alloc_traits = allocator_traits<__pointer_allocator>;
  383. using __map_pointer = typename __map_alloc_traits::pointer;
  384. using __map_const_pointer = typename allocator_traits<__const_pointer_allocator>::const_pointer;
  385. using reference = value_type&;
  386. using const_reference = const value_type&;
  387. using iterator = __deque_iterator<value_type, pointer, reference, __map_pointer, difference_type>;
  388. using const_iterator =
  389. __deque_iterator<value_type, const_pointer, const_reference, __map_const_pointer, difference_type>;
  390. using reverse_iterator = std::reverse_iterator<iterator>;
  391. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  392. static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
  393. "[allocator.requirements] states that rebinding an allocator to the same type should result in the "
  394. "original allocator");
  395. static_assert(is_nothrow_default_constructible<allocator_type>::value ==
  396. is_nothrow_default_constructible<__pointer_allocator>::value,
  397. "rebinding an allocator should not change excpetion guarantees");
  398. static_assert(is_nothrow_move_constructible<allocator_type>::value ==
  399. is_nothrow_move_constructible<typename __map::allocator_type>::value,
  400. "rebinding an allocator should not change excpetion guarantees");
  401. private:
  402. struct __deque_block_range {
  403. explicit _LIBCPP_HIDE_FROM_ABI
  404. __deque_block_range(pointer __b, pointer __e) _NOEXCEPT : __begin_(__b), __end_(__e) {}
  405. const pointer __begin_;
  406. const pointer __end_;
  407. };
  408. struct __deque_range {
  409. iterator __pos_;
  410. const iterator __end_;
  411. _LIBCPP_HIDE_FROM_ABI __deque_range(iterator __pos, iterator __e) _NOEXCEPT
  412. : __pos_(__pos), __end_(__e) {}
  413. explicit _LIBCPP_HIDE_FROM_ABI operator bool() const _NOEXCEPT {
  414. return __pos_ != __end_;
  415. }
  416. _LIBCPP_HIDE_FROM_ABI __deque_range begin() const {
  417. return *this;
  418. }
  419. _LIBCPP_HIDE_FROM_ABI __deque_range end() const {
  420. return __deque_range(__end_, __end_);
  421. }
  422. _LIBCPP_HIDE_FROM_ABI __deque_block_range operator*() const _NOEXCEPT {
  423. if (__pos_.__m_iter_ == __end_.__m_iter_) {
  424. return __deque_block_range(__pos_.__ptr_, __end_.__ptr_);
  425. }
  426. return __deque_block_range(__pos_.__ptr_, *__pos_.__m_iter_ + __block_size);
  427. }
  428. _LIBCPP_HIDE_FROM_ABI __deque_range& operator++() _NOEXCEPT {
  429. if (__pos_.__m_iter_ == __end_.__m_iter_) {
  430. __pos_ = __end_;
  431. } else {
  432. ++__pos_.__m_iter_;
  433. __pos_.__ptr_ = *__pos_.__m_iter_;
  434. }
  435. return *this;
  436. }
  437. _LIBCPP_HIDE_FROM_ABI friend bool operator==(__deque_range const& __lhs, __deque_range const& __rhs) {
  438. return __lhs.__pos_ == __rhs.__pos_;
  439. }
  440. _LIBCPP_HIDE_FROM_ABI friend bool operator!=(__deque_range const& __lhs, __deque_range const& __rhs) {
  441. return !(__lhs == __rhs);
  442. }
  443. };
  444. struct _ConstructTransaction {
  445. _LIBCPP_HIDE_FROM_ABI _ConstructTransaction(deque* __db, __deque_block_range& __r)
  446. : __pos_(__r.__begin_), __end_(__r.__end_), __begin_(__r.__begin_), __base_(__db) {}
  447. _LIBCPP_HIDE_FROM_ABI ~_ConstructTransaction() {
  448. __base_->__size() += (__pos_ - __begin_);
  449. }
  450. pointer __pos_;
  451. const pointer __end_;
  452. private:
  453. const pointer __begin_;
  454. deque* const __base_;
  455. };
  456. static const difference_type __block_size;
  457. __map __map_;
  458. size_type __start_;
  459. __compressed_pair<size_type, allocator_type> __size_;
  460. public:
  461. // construct/copy/destroy:
  462. _LIBCPP_HIDE_FROM_ABI
  463. deque() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
  464. : __start_(0), __size_(0, __default_init_tag()) {}
  465. _LIBCPP_HIDE_FROM_ABI ~deque() {
  466. clear();
  467. typename __map::iterator __i = __map_.begin();
  468. typename __map::iterator __e = __map_.end();
  469. for (; __i != __e; ++__i)
  470. __alloc_traits::deallocate(__alloc(), *__i, __block_size);
  471. }
  472. _LIBCPP_HIDE_FROM_ABI explicit deque(const allocator_type& __a)
  473. : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {}
  474. explicit _LIBCPP_HIDE_FROM_ABI deque(size_type __n);
  475. #if _LIBCPP_STD_VER > 11
  476. explicit _LIBCPP_HIDE_FROM_ABI deque(size_type __n, const _Allocator& __a);
  477. #endif
  478. _LIBCPP_HIDE_FROM_ABI deque(size_type __n, const value_type& __v);
  479. template <class = __enable_if_t<__is_allocator<_Allocator>::value> >
  480. _LIBCPP_HIDE_FROM_ABI deque(size_type __n, const value_type& __v, const allocator_type& __a)
  481. : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a)
  482. {
  483. if (__n > 0)
  484. __append(__n, __v);
  485. }
  486. template <class _InputIter>
  487. _LIBCPP_HIDE_FROM_ABI deque(_InputIter __f, _InputIter __l,
  488. typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type* = 0);
  489. template <class _InputIter>
  490. _LIBCPP_HIDE_FROM_ABI deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
  491. typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type* = 0);
  492. _LIBCPP_HIDE_FROM_ABI deque(const deque& __c);
  493. _LIBCPP_HIDE_FROM_ABI deque(const deque& __c, const __type_identity_t<allocator_type>& __a);
  494. _LIBCPP_HIDE_FROM_ABI deque& operator=(const deque& __c);
  495. #ifndef _LIBCPP_CXX03_LANG
  496. _LIBCPP_HIDE_FROM_ABI deque(initializer_list<value_type> __il);
  497. _LIBCPP_HIDE_FROM_ABI deque(initializer_list<value_type> __il, const allocator_type& __a);
  498. _LIBCPP_HIDE_FROM_ABI
  499. deque& operator=(initializer_list<value_type> __il) {assign(__il); return *this;}
  500. _LIBCPP_HIDE_FROM_ABI
  501. deque(deque&& __c) _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
  502. _LIBCPP_HIDE_FROM_ABI
  503. deque(deque&& __c, const __type_identity_t<allocator_type>& __a);
  504. _LIBCPP_HIDE_FROM_ABI
  505. deque& operator=(deque&& __c)
  506. _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
  507. is_nothrow_move_assignable<allocator_type>::value);
  508. _LIBCPP_HIDE_FROM_ABI
  509. void assign(initializer_list<value_type> __il) {assign(__il.begin(), __il.end());}
  510. #endif // _LIBCPP_CXX03_LANG
  511. template <class _InputIter>
  512. _LIBCPP_HIDE_FROM_ABI void assign(_InputIter __f, _InputIter __l,
  513. typename enable_if<__is_cpp17_input_iterator<_InputIter>::value &&
  514. !__is_cpp17_random_access_iterator<_InputIter>::value>::type* = 0);
  515. template <class _RAIter>
  516. _LIBCPP_HIDE_FROM_ABI void assign(_RAIter __f, _RAIter __l,
  517. typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
  518. _LIBCPP_HIDE_FROM_ABI void assign(size_type __n, const value_type& __v);
  519. _LIBCPP_HIDE_FROM_ABI
  520. allocator_type get_allocator() const _NOEXCEPT;
  521. _LIBCPP_HIDE_FROM_ABI allocator_type& __alloc() _NOEXCEPT { return __size_.second(); }
  522. _LIBCPP_HIDE_FROM_ABI const allocator_type& __alloc() const _NOEXCEPT { return __size_.second(); }
  523. // iterators:
  524. _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT {
  525. __map_pointer __mp = __map_.begin() + __start_ / __block_size;
  526. return iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
  527. }
  528. _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT {
  529. __map_const_pointer __mp =
  530. static_cast<__map_const_pointer>(__map_.begin() + __start_ / __block_size);
  531. return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
  532. }
  533. _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT {
  534. size_type __p = size() + __start_;
  535. __map_pointer __mp = __map_.begin() + __p / __block_size;
  536. return iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
  537. }
  538. _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT {
  539. size_type __p = size() + __start_;
  540. __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __p / __block_size);
  541. return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
  542. }
  543. _LIBCPP_HIDE_FROM_ABI
  544. reverse_iterator rbegin() _NOEXCEPT
  545. {return reverse_iterator(end());}
  546. _LIBCPP_HIDE_FROM_ABI
  547. const_reverse_iterator rbegin() const _NOEXCEPT
  548. {return const_reverse_iterator(end());}
  549. _LIBCPP_HIDE_FROM_ABI
  550. reverse_iterator rend() _NOEXCEPT
  551. {return reverse_iterator(begin());}
  552. _LIBCPP_HIDE_FROM_ABI
  553. const_reverse_iterator rend() const _NOEXCEPT
  554. {return const_reverse_iterator(begin());}
  555. _LIBCPP_HIDE_FROM_ABI
  556. const_iterator cbegin() const _NOEXCEPT
  557. {return begin();}
  558. _LIBCPP_HIDE_FROM_ABI
  559. const_iterator cend() const _NOEXCEPT
  560. {return end();}
  561. _LIBCPP_HIDE_FROM_ABI
  562. const_reverse_iterator crbegin() const _NOEXCEPT
  563. {return const_reverse_iterator(end());}
  564. _LIBCPP_HIDE_FROM_ABI
  565. const_reverse_iterator crend() const _NOEXCEPT
  566. {return const_reverse_iterator(begin());}
  567. // capacity:
  568. _LIBCPP_HIDE_FROM_ABI
  569. size_type size() const _NOEXCEPT {return __size();}
  570. _LIBCPP_HIDE_FROM_ABI size_type& __size() _NOEXCEPT { return __size_.first(); }
  571. _LIBCPP_HIDE_FROM_ABI const size_type& __size() const _NOEXCEPT { return __size_.first(); }
  572. _LIBCPP_HIDE_FROM_ABI
  573. size_type max_size() const _NOEXCEPT
  574. {return _VSTD::min<size_type>(
  575. __alloc_traits::max_size(__alloc()),
  576. numeric_limits<difference_type>::max());}
  577. _LIBCPP_HIDE_FROM_ABI void resize(size_type __n);
  578. _LIBCPP_HIDE_FROM_ABI void resize(size_type __n, const value_type& __v);
  579. _LIBCPP_HIDE_FROM_ABI void shrink_to_fit() _NOEXCEPT;
  580. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
  581. bool empty() const _NOEXCEPT {return size() == 0;}
  582. // element access:
  583. _LIBCPP_HIDE_FROM_ABI
  584. reference operator[](size_type __i) _NOEXCEPT;
  585. _LIBCPP_HIDE_FROM_ABI
  586. const_reference operator[](size_type __i) const _NOEXCEPT;
  587. _LIBCPP_HIDE_FROM_ABI
  588. reference at(size_type __i);
  589. _LIBCPP_HIDE_FROM_ABI
  590. const_reference at(size_type __i) const;
  591. _LIBCPP_HIDE_FROM_ABI
  592. reference front() _NOEXCEPT;
  593. _LIBCPP_HIDE_FROM_ABI
  594. const_reference front() const _NOEXCEPT;
  595. _LIBCPP_HIDE_FROM_ABI
  596. reference back() _NOEXCEPT;
  597. _LIBCPP_HIDE_FROM_ABI
  598. const_reference back() const _NOEXCEPT;
  599. // 23.2.2.3 modifiers:
  600. _LIBCPP_HIDE_FROM_ABI void push_front(const value_type& __v);
  601. _LIBCPP_HIDE_FROM_ABI void push_back(const value_type& __v);
  602. #ifndef _LIBCPP_CXX03_LANG
  603. #if _LIBCPP_STD_VER > 14
  604. template <class... _Args> _LIBCPP_HIDE_FROM_ABI reference emplace_front(_Args&&... __args);
  605. template <class... _Args> _LIBCPP_HIDE_FROM_ABI reference emplace_back (_Args&&... __args);
  606. #else
  607. template <class... _Args> _LIBCPP_HIDE_FROM_ABI void emplace_front(_Args&&... __args);
  608. template <class... _Args> _LIBCPP_HIDE_FROM_ABI void emplace_back (_Args&&... __args);
  609. #endif
  610. template <class... _Args> _LIBCPP_HIDE_FROM_ABI iterator emplace(const_iterator __p, _Args&&... __args);
  611. _LIBCPP_HIDE_FROM_ABI void push_front(value_type&& __v);
  612. _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __v);
  613. _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, value_type&& __v);
  614. _LIBCPP_HIDE_FROM_ABI
  615. iterator insert(const_iterator __p, initializer_list<value_type> __il)
  616. {return insert(__p, __il.begin(), __il.end());}
  617. #endif // _LIBCPP_CXX03_LANG
  618. _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, const value_type& __v);
  619. _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, size_type __n, const value_type& __v);
  620. template <class _InputIter>
  621. _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, _InputIter __f, _InputIter __l,
  622. typename enable_if<__is_exactly_cpp17_input_iterator<_InputIter>::value>::type* = 0);
  623. template <class _ForwardIterator>
  624. _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
  625. typename enable_if<__is_exactly_cpp17_forward_iterator<_ForwardIterator>::value>::type* = 0);
  626. template <class _BiIter>
  627. _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, _BiIter __f, _BiIter __l,
  628. typename enable_if<__is_cpp17_bidirectional_iterator<_BiIter>::value>::type* = 0);
  629. _LIBCPP_HIDE_FROM_ABI void pop_front();
  630. _LIBCPP_HIDE_FROM_ABI void pop_back();
  631. _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __p);
  632. _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __f, const_iterator __l);
  633. _LIBCPP_HIDE_FROM_ABI
  634. void swap(deque& __c)
  635. #if _LIBCPP_STD_VER >= 14
  636. _NOEXCEPT;
  637. #else
  638. _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
  639. __is_nothrow_swappable<allocator_type>::value);
  640. #endif
  641. _LIBCPP_REINITIALIZES_OBJECT _LIBCPP_HIDE_FROM_ABI
  642. void clear() _NOEXCEPT;
  643. _LIBCPP_HIDE_FROM_ABI
  644. bool __invariants() const {
  645. if (!__map_.__invariants())
  646. return false;
  647. if (__map_.size() >= size_type(-1) / __block_size)
  648. return false;
  649. for (typename __map::const_iterator __i = __map_.begin(), __e = __map_.end();
  650. __i != __e; ++__i)
  651. if (*__i == nullptr)
  652. return false;
  653. if (__map_.size() != 0)
  654. {
  655. if (size() >= __map_.size() * __block_size)
  656. return false;
  657. if (__start_ >= __map_.size() * __block_size - size())
  658. return false;
  659. }
  660. else
  661. {
  662. if (size() != 0)
  663. return false;
  664. if (__start_ != 0)
  665. return false;
  666. }
  667. return true;
  668. }
  669. _LIBCPP_HIDE_FROM_ABI
  670. void __move_assign_alloc(deque& __c)
  671. _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
  672. is_nothrow_move_assignable<allocator_type>::value)
  673. {__move_assign_alloc(__c, integral_constant<bool,
  674. __alloc_traits::propagate_on_container_move_assignment::value>());}
  675. _LIBCPP_HIDE_FROM_ABI
  676. void __move_assign_alloc(deque& __c, true_type)
  677. _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
  678. {
  679. __alloc() = _VSTD::move(__c.__alloc());
  680. }
  681. _LIBCPP_HIDE_FROM_ABI
  682. void __move_assign_alloc(deque&, false_type) _NOEXCEPT
  683. {}
  684. _LIBCPP_HIDE_FROM_ABI
  685. void __move_assign(deque& __c)
  686. _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
  687. is_nothrow_move_assignable<allocator_type>::value)
  688. {
  689. __map_ = _VSTD::move(__c.__map_);
  690. __start_ = __c.__start_;
  691. __size() = __c.size();
  692. __move_assign_alloc(__c);
  693. __c.__start_ = __c.__size() = 0;
  694. }
  695. _LIBCPP_HIDE_FROM_ABI
  696. static size_type __recommend_blocks(size_type __n)
  697. {
  698. return __n / __block_size + (__n % __block_size != 0);
  699. }
  700. _LIBCPP_HIDE_FROM_ABI
  701. size_type __capacity() const
  702. {
  703. return __map_.size() == 0 ? 0 : __map_.size() * __block_size - 1;
  704. }
  705. _LIBCPP_HIDE_FROM_ABI
  706. size_type __block_count() const
  707. {
  708. return __map_.size();
  709. }
  710. _LIBCPP_HIDE_FROM_ABI
  711. size_type __front_spare() const
  712. {
  713. return __start_;
  714. }
  715. _LIBCPP_HIDE_FROM_ABI
  716. size_type __front_spare_blocks() const {
  717. return __front_spare() / __block_size;
  718. }
  719. _LIBCPP_HIDE_FROM_ABI
  720. size_type __back_spare() const
  721. {
  722. return __capacity() - (__start_ + size());
  723. }
  724. _LIBCPP_HIDE_FROM_ABI
  725. size_type __back_spare_blocks() const {
  726. return __back_spare() / __block_size;
  727. }
  728. private:
  729. _LIBCPP_HIDE_FROM_ABI
  730. bool __maybe_remove_front_spare(bool __keep_one = true) {
  731. if (__front_spare_blocks() >= 2 || (!__keep_one && __front_spare_blocks())) {
  732. __alloc_traits::deallocate(__alloc(), __map_.front(),
  733. __block_size);
  734. __map_.pop_front();
  735. __start_ -= __block_size;
  736. return true;
  737. }
  738. return false;
  739. }
  740. _LIBCPP_HIDE_FROM_ABI
  741. bool __maybe_remove_back_spare(bool __keep_one = true) {
  742. if (__back_spare_blocks() >= 2 || (!__keep_one && __back_spare_blocks())) {
  743. __alloc_traits::deallocate(__alloc(), __map_.back(),
  744. __block_size);
  745. __map_.pop_back();
  746. return true;
  747. }
  748. return false;
  749. }
  750. template <class _InpIter>
  751. _LIBCPP_HIDE_FROM_ABI void __append(_InpIter __f, _InpIter __l,
  752. typename enable_if<__is_exactly_cpp17_input_iterator<_InpIter>::value>::type* = 0);
  753. template <class _ForIter>
  754. _LIBCPP_HIDE_FROM_ABI void __append(_ForIter __f, _ForIter __l,
  755. typename enable_if<__is_cpp17_forward_iterator<_ForIter>::value>::type* = 0);
  756. _LIBCPP_HIDE_FROM_ABI void __append(size_type __n);
  757. _LIBCPP_HIDE_FROM_ABI void __append(size_type __n, const value_type& __v);
  758. _LIBCPP_HIDE_FROM_ABI void __erase_to_end(const_iterator __f);
  759. _LIBCPP_HIDE_FROM_ABI void __add_front_capacity();
  760. _LIBCPP_HIDE_FROM_ABI void __add_front_capacity(size_type __n);
  761. _LIBCPP_HIDE_FROM_ABI void __add_back_capacity();
  762. _LIBCPP_HIDE_FROM_ABI void __add_back_capacity(size_type __n);
  763. _LIBCPP_HIDE_FROM_ABI iterator __move_and_check(iterator __f, iterator __l, iterator __r,
  764. const_pointer& __vt);
  765. _LIBCPP_HIDE_FROM_ABI iterator __move_backward_and_check(iterator __f, iterator __l, iterator __r,
  766. const_pointer& __vt);
  767. _LIBCPP_HIDE_FROM_ABI void __move_construct_and_check(iterator __f, iterator __l,
  768. iterator __r, const_pointer& __vt);
  769. _LIBCPP_HIDE_FROM_ABI void __move_construct_backward_and_check(iterator __f, iterator __l,
  770. iterator __r, const_pointer& __vt);
  771. _LIBCPP_HIDE_FROM_ABI
  772. void __copy_assign_alloc(const deque& __c)
  773. {__copy_assign_alloc(__c, integral_constant<bool,
  774. __alloc_traits::propagate_on_container_copy_assignment::value>());}
  775. _LIBCPP_HIDE_FROM_ABI
  776. void __copy_assign_alloc(const deque& __c, true_type)
  777. {
  778. if (__alloc() != __c.__alloc())
  779. {
  780. clear();
  781. shrink_to_fit();
  782. }
  783. __alloc() = __c.__alloc();
  784. __map_.__alloc() = __c.__map_.__alloc();
  785. }
  786. _LIBCPP_HIDE_FROM_ABI
  787. void __copy_assign_alloc(const deque&, false_type)
  788. {}
  789. _LIBCPP_HIDE_FROM_ABI void __move_assign(deque& __c, true_type)
  790. _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
  791. _LIBCPP_HIDE_FROM_ABI void __move_assign(deque& __c, false_type);
  792. };
  793. template <class _Tp, class _Alloc>
  794. _LIBCPP_CONSTEXPR const typename allocator_traits<_Alloc>::difference_type deque<_Tp, _Alloc>::__block_size =
  795. __deque_block_size<value_type, difference_type>::value;
  796. #if _LIBCPP_STD_VER >= 17
  797. template<class _InputIterator,
  798. class _Alloc = allocator<__iter_value_type<_InputIterator>>,
  799. class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
  800. class = enable_if_t<__is_allocator<_Alloc>::value>
  801. >
  802. deque(_InputIterator, _InputIterator)
  803. -> deque<__iter_value_type<_InputIterator>, _Alloc>;
  804. template<class _InputIterator,
  805. class _Alloc,
  806. class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
  807. class = enable_if_t<__is_allocator<_Alloc>::value>
  808. >
  809. deque(_InputIterator, _InputIterator, _Alloc)
  810. -> deque<__iter_value_type<_InputIterator>, _Alloc>;
  811. #endif
  812. template <class _Tp, class _Allocator>
  813. deque<_Tp, _Allocator>::deque(size_type __n)
  814. : __start_(0), __size_(0, __default_init_tag())
  815. {
  816. if (__n > 0)
  817. __append(__n);
  818. }
  819. #if _LIBCPP_STD_VER > 11
  820. template <class _Tp, class _Allocator>
  821. deque<_Tp, _Allocator>::deque(size_type __n, const _Allocator& __a)
  822. : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a)
  823. {
  824. if (__n > 0)
  825. __append(__n);
  826. }
  827. #endif
  828. template <class _Tp, class _Allocator>
  829. deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v)
  830. : __start_(0), __size_(0, __default_init_tag())
  831. {
  832. if (__n > 0)
  833. __append(__n, __v);
  834. }
  835. template <class _Tp, class _Allocator>
  836. template <class _InputIter>
  837. deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l,
  838. typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type*)
  839. : __start_(0), __size_(0, __default_init_tag())
  840. {
  841. __append(__f, __l);
  842. }
  843. template <class _Tp, class _Allocator>
  844. template <class _InputIter>
  845. deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
  846. typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type*)
  847. : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a)
  848. {
  849. __append(__f, __l);
  850. }
  851. template <class _Tp, class _Allocator>
  852. deque<_Tp, _Allocator>::deque(const deque& __c)
  853. : __map_(__pointer_allocator(__alloc_traits::select_on_container_copy_construction(__c.__alloc()))),
  854. __start_(0),
  855. __size_(0, __map_.__alloc())
  856. {
  857. __append(__c.begin(), __c.end());
  858. }
  859. template <class _Tp, class _Allocator>
  860. deque<_Tp, _Allocator>::deque(const deque& __c, const __type_identity_t<allocator_type>& __a)
  861. : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a)
  862. {
  863. __append(__c.begin(), __c.end());
  864. }
  865. template <class _Tp, class _Allocator>
  866. deque<_Tp, _Allocator>&
  867. deque<_Tp, _Allocator>::operator=(const deque& __c)
  868. {
  869. if (this != _VSTD::addressof(__c))
  870. {
  871. __copy_assign_alloc(__c);
  872. assign(__c.begin(), __c.end());
  873. }
  874. return *this;
  875. }
  876. #ifndef _LIBCPP_CXX03_LANG
  877. template <class _Tp, class _Allocator>
  878. deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il)
  879. : __start_(0), __size_(0, __default_init_tag())
  880. {
  881. __append(__il.begin(), __il.end());
  882. }
  883. template <class _Tp, class _Allocator>
  884. deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il, const allocator_type& __a)
  885. : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a)
  886. {
  887. __append(__il.begin(), __il.end());
  888. }
  889. template <class _Tp, class _Allocator>
  890. inline
  891. deque<_Tp, _Allocator>::deque(deque&& __c)
  892. _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
  893. : __map_(std::move(__c.__map_)), __start_(std::move(__c.__start_)), __size_(std::move(__c.__size_))
  894. {
  895. __c.__start_ = 0;
  896. __c.__size() = 0;
  897. }
  898. template <class _Tp, class _Allocator>
  899. inline
  900. deque<_Tp, _Allocator>::deque(deque&& __c, const __type_identity_t<allocator_type>& __a)
  901. : __map_(std::move(__c.__map_), __pointer_allocator(__a)),
  902. __start_(std::move(__c.__start_)),
  903. __size_(std::move(__c.__size()), __a)
  904. {
  905. if (__a == __c.__alloc())
  906. {
  907. __c.__start_ = 0;
  908. __c.__size() = 0;
  909. }
  910. else
  911. {
  912. __map_.clear();
  913. __start_ = 0;
  914. __size() = 0;
  915. typedef move_iterator<iterator> _Ip;
  916. assign(_Ip(__c.begin()), _Ip(__c.end()));
  917. }
  918. }
  919. template <class _Tp, class _Allocator>
  920. inline
  921. deque<_Tp, _Allocator>&
  922. deque<_Tp, _Allocator>::operator=(deque&& __c)
  923. _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
  924. is_nothrow_move_assignable<allocator_type>::value)
  925. {
  926. __move_assign(__c, integral_constant<bool,
  927. __alloc_traits::propagate_on_container_move_assignment::value>());
  928. return *this;
  929. }
  930. template <class _Tp, class _Allocator>
  931. void
  932. deque<_Tp, _Allocator>::__move_assign(deque& __c, false_type)
  933. {
  934. if (__alloc() != __c.__alloc())
  935. {
  936. typedef move_iterator<iterator> _Ip;
  937. assign(_Ip(__c.begin()), _Ip(__c.end()));
  938. }
  939. else
  940. __move_assign(__c, true_type());
  941. }
  942. template <class _Tp, class _Allocator>
  943. void
  944. deque<_Tp, _Allocator>::__move_assign(deque& __c, true_type)
  945. _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
  946. {
  947. clear();
  948. shrink_to_fit();
  949. __move_assign(__c);
  950. }
  951. #endif // _LIBCPP_CXX03_LANG
  952. template <class _Tp, class _Allocator>
  953. template <class _InputIter>
  954. void
  955. deque<_Tp, _Allocator>::assign(_InputIter __f, _InputIter __l,
  956. typename enable_if<__is_cpp17_input_iterator<_InputIter>::value &&
  957. !__is_cpp17_random_access_iterator<_InputIter>::value>::type*)
  958. {
  959. iterator __i = begin();
  960. iterator __e = end();
  961. for (; __f != __l && __i != __e; ++__f, (void) ++__i)
  962. *__i = *__f;
  963. if (__f != __l)
  964. __append(__f, __l);
  965. else
  966. __erase_to_end(__i);
  967. }
  968. template <class _Tp, class _Allocator>
  969. template <class _RAIter>
  970. void
  971. deque<_Tp, _Allocator>::assign(_RAIter __f, _RAIter __l,
  972. typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
  973. {
  974. if (static_cast<size_type>(__l - __f) > size())
  975. {
  976. _RAIter __m = __f + size();
  977. _VSTD::copy(__f, __m, begin());
  978. __append(__m, __l);
  979. }
  980. else
  981. __erase_to_end(_VSTD::copy(__f, __l, begin()));
  982. }
  983. template <class _Tp, class _Allocator>
  984. void
  985. deque<_Tp, _Allocator>::assign(size_type __n, const value_type& __v)
  986. {
  987. if (__n > size())
  988. {
  989. _VSTD::fill_n(begin(), size(), __v);
  990. __n -= size();
  991. __append(__n, __v);
  992. }
  993. else
  994. __erase_to_end(_VSTD::fill_n(begin(), __n, __v));
  995. }
  996. template <class _Tp, class _Allocator>
  997. inline
  998. _Allocator
  999. deque<_Tp, _Allocator>::get_allocator() const _NOEXCEPT
  1000. {
  1001. return __alloc();
  1002. }
  1003. template <class _Tp, class _Allocator>
  1004. void
  1005. deque<_Tp, _Allocator>::resize(size_type __n)
  1006. {
  1007. if (__n > size())
  1008. __append(__n - size());
  1009. else if (__n < size())
  1010. __erase_to_end(begin() + __n);
  1011. }
  1012. template <class _Tp, class _Allocator>
  1013. void
  1014. deque<_Tp, _Allocator>::resize(size_type __n, const value_type& __v)
  1015. {
  1016. if (__n > size())
  1017. __append(__n - size(), __v);
  1018. else if (__n < size())
  1019. __erase_to_end(begin() + __n);
  1020. }
  1021. template <class _Tp, class _Allocator>
  1022. void
  1023. deque<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
  1024. {
  1025. allocator_type& __a = __alloc();
  1026. if (empty())
  1027. {
  1028. while (__map_.size() > 0)
  1029. {
  1030. __alloc_traits::deallocate(__a, __map_.back(), __block_size);
  1031. __map_.pop_back();
  1032. }
  1033. __start_ = 0;
  1034. }
  1035. else
  1036. {
  1037. __maybe_remove_front_spare(/*__keep_one=*/false);
  1038. __maybe_remove_back_spare(/*__keep_one=*/false);
  1039. }
  1040. __map_.shrink_to_fit();
  1041. }
  1042. template <class _Tp, class _Allocator>
  1043. inline
  1044. typename deque<_Tp, _Allocator>::reference
  1045. deque<_Tp, _Allocator>::operator[](size_type __i) _NOEXCEPT
  1046. {
  1047. size_type __p = __start_ + __i;
  1048. return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
  1049. }
  1050. template <class _Tp, class _Allocator>
  1051. inline
  1052. typename deque<_Tp, _Allocator>::const_reference
  1053. deque<_Tp, _Allocator>::operator[](size_type __i) const _NOEXCEPT
  1054. {
  1055. size_type __p = __start_ + __i;
  1056. return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
  1057. }
  1058. template <class _Tp, class _Allocator>
  1059. inline
  1060. typename deque<_Tp, _Allocator>::reference
  1061. deque<_Tp, _Allocator>::at(size_type __i)
  1062. {
  1063. if (__i >= size())
  1064. _VSTD::__throw_out_of_range("deque");
  1065. size_type __p = __start_ + __i;
  1066. return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
  1067. }
  1068. template <class _Tp, class _Allocator>
  1069. inline
  1070. typename deque<_Tp, _Allocator>::const_reference
  1071. deque<_Tp, _Allocator>::at(size_type __i) const
  1072. {
  1073. if (__i >= size())
  1074. _VSTD::__throw_out_of_range("deque");
  1075. size_type __p = __start_ + __i;
  1076. return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
  1077. }
  1078. template <class _Tp, class _Allocator>
  1079. inline
  1080. typename deque<_Tp, _Allocator>::reference
  1081. deque<_Tp, _Allocator>::front() _NOEXCEPT
  1082. {
  1083. return *(*(__map_.begin() + __start_ / __block_size)
  1084. + __start_ % __block_size);
  1085. }
  1086. template <class _Tp, class _Allocator>
  1087. inline
  1088. typename deque<_Tp, _Allocator>::const_reference
  1089. deque<_Tp, _Allocator>::front() const _NOEXCEPT
  1090. {
  1091. return *(*(__map_.begin() + __start_ / __block_size)
  1092. + __start_ % __block_size);
  1093. }
  1094. template <class _Tp, class _Allocator>
  1095. inline
  1096. typename deque<_Tp, _Allocator>::reference
  1097. deque<_Tp, _Allocator>::back() _NOEXCEPT
  1098. {
  1099. size_type __p = size() + __start_ - 1;
  1100. return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
  1101. }
  1102. template <class _Tp, class _Allocator>
  1103. inline
  1104. typename deque<_Tp, _Allocator>::const_reference
  1105. deque<_Tp, _Allocator>::back() const _NOEXCEPT
  1106. {
  1107. size_type __p = size() + __start_ - 1;
  1108. return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
  1109. }
  1110. template <class _Tp, class _Allocator>
  1111. void
  1112. deque<_Tp, _Allocator>::push_back(const value_type& __v)
  1113. {
  1114. allocator_type& __a = __alloc();
  1115. if (__back_spare() == 0)
  1116. __add_back_capacity();
  1117. // __back_spare() >= 1
  1118. __alloc_traits::construct(__a, _VSTD::addressof(*end()), __v);
  1119. ++__size();
  1120. }
  1121. template <class _Tp, class _Allocator>
  1122. void
  1123. deque<_Tp, _Allocator>::push_front(const value_type& __v)
  1124. {
  1125. allocator_type& __a = __alloc();
  1126. if (__front_spare() == 0)
  1127. __add_front_capacity();
  1128. // __front_spare() >= 1
  1129. __alloc_traits::construct(__a, _VSTD::addressof(*--begin()), __v);
  1130. --__start_;
  1131. ++__size();
  1132. }
  1133. #ifndef _LIBCPP_CXX03_LANG
  1134. template <class _Tp, class _Allocator>
  1135. void
  1136. deque<_Tp, _Allocator>::push_back(value_type&& __v)
  1137. {
  1138. allocator_type& __a = __alloc();
  1139. if (__back_spare() == 0)
  1140. __add_back_capacity();
  1141. // __back_spare() >= 1
  1142. __alloc_traits::construct(__a, _VSTD::addressof(*end()), _VSTD::move(__v));
  1143. ++__size();
  1144. }
  1145. template <class _Tp, class _Allocator>
  1146. template <class... _Args>
  1147. #if _LIBCPP_STD_VER > 14
  1148. typename deque<_Tp, _Allocator>::reference
  1149. #else
  1150. void
  1151. #endif
  1152. deque<_Tp, _Allocator>::emplace_back(_Args&&... __args)
  1153. {
  1154. allocator_type& __a = __alloc();
  1155. if (__back_spare() == 0)
  1156. __add_back_capacity();
  1157. // __back_spare() >= 1
  1158. __alloc_traits::construct(__a, _VSTD::addressof(*end()),
  1159. _VSTD::forward<_Args>(__args)...);
  1160. ++__size();
  1161. #if _LIBCPP_STD_VER > 14
  1162. return *--end();
  1163. #endif
  1164. }
  1165. template <class _Tp, class _Allocator>
  1166. void
  1167. deque<_Tp, _Allocator>::push_front(value_type&& __v)
  1168. {
  1169. allocator_type& __a = __alloc();
  1170. if (__front_spare() == 0)
  1171. __add_front_capacity();
  1172. // __front_spare() >= 1
  1173. __alloc_traits::construct(__a, _VSTD::addressof(*--begin()), _VSTD::move(__v));
  1174. --__start_;
  1175. ++__size();
  1176. }
  1177. template <class _Tp, class _Allocator>
  1178. template <class... _Args>
  1179. #if _LIBCPP_STD_VER > 14
  1180. typename deque<_Tp, _Allocator>::reference
  1181. #else
  1182. void
  1183. #endif
  1184. deque<_Tp, _Allocator>::emplace_front(_Args&&... __args)
  1185. {
  1186. allocator_type& __a = __alloc();
  1187. if (__front_spare() == 0)
  1188. __add_front_capacity();
  1189. // __front_spare() >= 1
  1190. __alloc_traits::construct(__a, _VSTD::addressof(*--begin()), _VSTD::forward<_Args>(__args)...);
  1191. --__start_;
  1192. ++__size();
  1193. #if _LIBCPP_STD_VER > 14
  1194. return *begin();
  1195. #endif
  1196. }
  1197. template <class _Tp, class _Allocator>
  1198. typename deque<_Tp, _Allocator>::iterator
  1199. deque<_Tp, _Allocator>::insert(const_iterator __p, value_type&& __v)
  1200. {
  1201. size_type __pos = __p - begin();
  1202. size_type __to_end = size() - __pos;
  1203. allocator_type& __a = __alloc();
  1204. if (__pos < __to_end)
  1205. { // insert by shifting things backward
  1206. if (__front_spare() == 0)
  1207. __add_front_capacity();
  1208. // __front_spare() >= 1
  1209. if (__pos == 0)
  1210. {
  1211. __alloc_traits::construct(__a, _VSTD::addressof(*--begin()), _VSTD::move(__v));
  1212. --__start_;
  1213. ++__size();
  1214. }
  1215. else
  1216. {
  1217. iterator __b = begin();
  1218. iterator __bm1 = _VSTD::prev(__b);
  1219. __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
  1220. --__start_;
  1221. ++__size();
  1222. if (__pos > 1)
  1223. __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
  1224. *__b = _VSTD::move(__v);
  1225. }
  1226. }
  1227. else
  1228. { // insert by shifting things forward
  1229. if (__back_spare() == 0)
  1230. __add_back_capacity();
  1231. // __back_capacity >= 1
  1232. size_type __de = size() - __pos;
  1233. if (__de == 0)
  1234. {
  1235. __alloc_traits::construct(__a, _VSTD::addressof(*end()), _VSTD::move(__v));
  1236. ++__size();
  1237. }
  1238. else
  1239. {
  1240. iterator __e = end();
  1241. iterator __em1 = _VSTD::prev(__e);
  1242. __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
  1243. ++__size();
  1244. if (__de > 1)
  1245. __e = _VSTD::move_backward(__e - __de, __em1, __e);
  1246. *--__e = _VSTD::move(__v);
  1247. }
  1248. }
  1249. return begin() + __pos;
  1250. }
  1251. template <class _Tp, class _Allocator>
  1252. template <class... _Args>
  1253. typename deque<_Tp, _Allocator>::iterator
  1254. deque<_Tp, _Allocator>::emplace(const_iterator __p, _Args&&... __args)
  1255. {
  1256. size_type __pos = __p - begin();
  1257. size_type __to_end = size() - __pos;
  1258. allocator_type& __a = __alloc();
  1259. if (__pos < __to_end)
  1260. { // insert by shifting things backward
  1261. if (__front_spare() == 0)
  1262. __add_front_capacity();
  1263. // __front_spare() >= 1
  1264. if (__pos == 0)
  1265. {
  1266. __alloc_traits::construct(__a, _VSTD::addressof(*--begin()), _VSTD::forward<_Args>(__args)...);
  1267. --__start_;
  1268. ++__size();
  1269. }
  1270. else
  1271. {
  1272. __temp_value<value_type, _Allocator> __tmp(__alloc(), _VSTD::forward<_Args>(__args)...);
  1273. iterator __b = begin();
  1274. iterator __bm1 = _VSTD::prev(__b);
  1275. __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
  1276. --__start_;
  1277. ++__size();
  1278. if (__pos > 1)
  1279. __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
  1280. *__b = _VSTD::move(__tmp.get());
  1281. }
  1282. }
  1283. else
  1284. { // insert by shifting things forward
  1285. if (__back_spare() == 0)
  1286. __add_back_capacity();
  1287. // __back_capacity >= 1
  1288. size_type __de = size() - __pos;
  1289. if (__de == 0)
  1290. {
  1291. __alloc_traits::construct(__a, _VSTD::addressof(*end()), _VSTD::forward<_Args>(__args)...);
  1292. ++__size();
  1293. }
  1294. else
  1295. {
  1296. __temp_value<value_type, _Allocator> __tmp(__alloc(), _VSTD::forward<_Args>(__args)...);
  1297. iterator __e = end();
  1298. iterator __em1 = _VSTD::prev(__e);
  1299. __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
  1300. ++__size();
  1301. if (__de > 1)
  1302. __e = _VSTD::move_backward(__e - __de, __em1, __e);
  1303. *--__e = _VSTD::move(__tmp.get());
  1304. }
  1305. }
  1306. return begin() + __pos;
  1307. }
  1308. #endif // _LIBCPP_CXX03_LANG
  1309. template <class _Tp, class _Allocator>
  1310. typename deque<_Tp, _Allocator>::iterator
  1311. deque<_Tp, _Allocator>::insert(const_iterator __p, const value_type& __v)
  1312. {
  1313. size_type __pos = __p - begin();
  1314. size_type __to_end = size() - __pos;
  1315. allocator_type& __a = __alloc();
  1316. if (__pos < __to_end)
  1317. { // insert by shifting things backward
  1318. if (__front_spare() == 0)
  1319. __add_front_capacity();
  1320. // __front_spare() >= 1
  1321. if (__pos == 0)
  1322. {
  1323. __alloc_traits::construct(__a, _VSTD::addressof(*--begin()), __v);
  1324. --__start_;
  1325. ++__size();
  1326. }
  1327. else
  1328. {
  1329. const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
  1330. iterator __b = begin();
  1331. iterator __bm1 = _VSTD::prev(__b);
  1332. if (__vt == pointer_traits<const_pointer>::pointer_to(*__b))
  1333. __vt = pointer_traits<const_pointer>::pointer_to(*__bm1);
  1334. __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
  1335. --__start_;
  1336. ++__size();
  1337. if (__pos > 1)
  1338. __b = __move_and_check(_VSTD::next(__b), __b + __pos, __b, __vt);
  1339. *__b = *__vt;
  1340. }
  1341. }
  1342. else
  1343. { // insert by shifting things forward
  1344. if (__back_spare() == 0)
  1345. __add_back_capacity();
  1346. // __back_capacity >= 1
  1347. size_type __de = size() - __pos;
  1348. if (__de == 0)
  1349. {
  1350. __alloc_traits::construct(__a, _VSTD::addressof(*end()), __v);
  1351. ++__size();
  1352. }
  1353. else
  1354. {
  1355. const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
  1356. iterator __e = end();
  1357. iterator __em1 = _VSTD::prev(__e);
  1358. if (__vt == pointer_traits<const_pointer>::pointer_to(*__em1))
  1359. __vt = pointer_traits<const_pointer>::pointer_to(*__e);
  1360. __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
  1361. ++__size();
  1362. if (__de > 1)
  1363. __e = __move_backward_and_check(__e - __de, __em1, __e, __vt);
  1364. *--__e = *__vt;
  1365. }
  1366. }
  1367. return begin() + __pos;
  1368. }
  1369. template <class _Tp, class _Allocator>
  1370. typename deque<_Tp, _Allocator>::iterator
  1371. deque<_Tp, _Allocator>::insert(const_iterator __p, size_type __n, const value_type& __v)
  1372. {
  1373. size_type __pos = __p - begin();
  1374. size_type __to_end = __size() - __pos;
  1375. allocator_type& __a = __alloc();
  1376. if (__pos < __to_end)
  1377. { // insert by shifting things backward
  1378. if (__n > __front_spare())
  1379. __add_front_capacity(__n - __front_spare());
  1380. // __n <= __front_spare()
  1381. iterator __old_begin = begin();
  1382. iterator __i = __old_begin;
  1383. if (__n > __pos)
  1384. {
  1385. for (size_type __m = __n - __pos; __m; --__m, --__start_, ++__size())
  1386. __alloc_traits::construct(__a, _VSTD::addressof(*--__i), __v);
  1387. __n = __pos;
  1388. }
  1389. if (__n > 0)
  1390. {
  1391. const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
  1392. iterator __obn = __old_begin + __n;
  1393. __move_construct_backward_and_check(__old_begin, __obn, __i, __vt);
  1394. if (__n < __pos)
  1395. __old_begin = __move_and_check(__obn, __old_begin + __pos, __old_begin, __vt);
  1396. _VSTD::fill_n(__old_begin, __n, *__vt);
  1397. }
  1398. }
  1399. else
  1400. { // insert by shifting things forward
  1401. size_type __back_capacity = __back_spare();
  1402. if (__n > __back_capacity)
  1403. __add_back_capacity(__n - __back_capacity);
  1404. // __n <= __back_capacity
  1405. iterator __old_end = end();
  1406. iterator __i = __old_end;
  1407. size_type __de = size() - __pos;
  1408. if (__n > __de)
  1409. {
  1410. for (size_type __m = __n - __de; __m; --__m, (void) ++__i, ++__size())
  1411. __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
  1412. __n = __de;
  1413. }
  1414. if (__n > 0)
  1415. {
  1416. const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
  1417. iterator __oen = __old_end - __n;
  1418. __move_construct_and_check(__oen, __old_end, __i, __vt);
  1419. if (__n < __de)
  1420. __old_end = __move_backward_and_check(__old_end - __de, __oen, __old_end, __vt);
  1421. _VSTD::fill_n(__old_end - __n, __n, *__vt);
  1422. }
  1423. }
  1424. return begin() + __pos;
  1425. }
  1426. template <class _Tp, class _Allocator>
  1427. template <class _InputIter>
  1428. typename deque<_Tp, _Allocator>::iterator
  1429. deque<_Tp, _Allocator>::insert(const_iterator __p, _InputIter __f, _InputIter __l,
  1430. typename enable_if<__is_exactly_cpp17_input_iterator<_InputIter>::value>::type*)
  1431. {
  1432. __split_buffer<value_type, allocator_type&> __buf(__alloc());
  1433. __buf.__construct_at_end(__f, __l);
  1434. typedef typename __split_buffer<value_type, allocator_type&>::iterator __bi;
  1435. return insert(__p, move_iterator<__bi>(__buf.begin()), move_iterator<__bi>(__buf.end()));
  1436. }
  1437. template <class _Tp, class _Allocator>
  1438. template <class _ForwardIterator>
  1439. typename deque<_Tp, _Allocator>::iterator
  1440. deque<_Tp, _Allocator>::insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
  1441. typename enable_if<__is_exactly_cpp17_forward_iterator<_ForwardIterator>::value>::type*)
  1442. {
  1443. size_type __n = _VSTD::distance(__f, __l);
  1444. __split_buffer<value_type, allocator_type&> __buf(__n, 0, __alloc());
  1445. __buf.__construct_at_end(__f, __l);
  1446. typedef typename __split_buffer<value_type, allocator_type&>::iterator __fwd;
  1447. return insert(__p, move_iterator<__fwd>(__buf.begin()), move_iterator<__fwd>(__buf.end()));
  1448. }
  1449. template <class _Tp, class _Allocator>
  1450. template <class _BiIter>
  1451. typename deque<_Tp, _Allocator>::iterator
  1452. deque<_Tp, _Allocator>::insert(const_iterator __p, _BiIter __f, _BiIter __l,
  1453. typename enable_if<__is_cpp17_bidirectional_iterator<_BiIter>::value>::type*)
  1454. {
  1455. size_type __n = _VSTD::distance(__f, __l);
  1456. size_type __pos = __p - begin();
  1457. size_type __to_end = size() - __pos;
  1458. allocator_type& __a = __alloc();
  1459. if (__pos < __to_end)
  1460. { // insert by shifting things backward
  1461. if (__n > __front_spare())
  1462. __add_front_capacity(__n - __front_spare());
  1463. // __n <= __front_spare()
  1464. iterator __old_begin = begin();
  1465. iterator __i = __old_begin;
  1466. _BiIter __m = __f;
  1467. if (__n > __pos)
  1468. {
  1469. __m = __pos < __n / 2 ? _VSTD::prev(__l, __pos) : _VSTD::next(__f, __n - __pos);
  1470. for (_BiIter __j = __m; __j != __f; --__start_, ++__size())
  1471. __alloc_traits::construct(__a, _VSTD::addressof(*--__i), *--__j);
  1472. __n = __pos;
  1473. }
  1474. if (__n > 0)
  1475. {
  1476. iterator __obn = __old_begin + __n;
  1477. for (iterator __j = __obn; __j != __old_begin;)
  1478. {
  1479. __alloc_traits::construct(__a, _VSTD::addressof(*--__i), _VSTD::move(*--__j));
  1480. --__start_;
  1481. ++__size();
  1482. }
  1483. if (__n < __pos)
  1484. __old_begin = _VSTD::move(__obn, __old_begin + __pos, __old_begin);
  1485. _VSTD::copy(__m, __l, __old_begin);
  1486. }
  1487. }
  1488. else
  1489. { // insert by shifting things forward
  1490. size_type __back_capacity = __back_spare();
  1491. if (__n > __back_capacity)
  1492. __add_back_capacity(__n - __back_capacity);
  1493. // __n <= __back_capacity
  1494. iterator __old_end = end();
  1495. iterator __i = __old_end;
  1496. _BiIter __m = __l;
  1497. size_type __de = size() - __pos;
  1498. if (__n > __de)
  1499. {
  1500. __m = __de < __n / 2 ? _VSTD::next(__f, __de) : _VSTD::prev(__l, __n - __de);
  1501. for (_BiIter __j = __m; __j != __l; ++__i, (void) ++__j, ++__size())
  1502. __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__j);
  1503. __n = __de;
  1504. }
  1505. if (__n > 0)
  1506. {
  1507. iterator __oen = __old_end - __n;
  1508. for (iterator __j = __oen; __j != __old_end; ++__i, (void) ++__j, ++__size())
  1509. __alloc_traits::construct(__a, _VSTD::addressof(*__i), _VSTD::move(*__j));
  1510. if (__n < __de)
  1511. __old_end = _VSTD::move_backward(__old_end - __de, __oen, __old_end);
  1512. _VSTD::copy_backward(__f, __m, __old_end);
  1513. }
  1514. }
  1515. return begin() + __pos;
  1516. }
  1517. template <class _Tp, class _Allocator>
  1518. template <class _InpIter>
  1519. void
  1520. deque<_Tp, _Allocator>::__append(_InpIter __f, _InpIter __l,
  1521. typename enable_if<__is_exactly_cpp17_input_iterator<_InpIter>::value>::type*)
  1522. {
  1523. for (; __f != __l; ++__f)
  1524. #ifdef _LIBCPP_CXX03_LANG
  1525. push_back(*__f);
  1526. #else
  1527. emplace_back(*__f);
  1528. #endif
  1529. }
  1530. template <class _Tp, class _Allocator>
  1531. template <class _ForIter>
  1532. void
  1533. deque<_Tp, _Allocator>::__append(_ForIter __f, _ForIter __l,
  1534. typename enable_if<__is_cpp17_forward_iterator<_ForIter>::value>::type*)
  1535. {
  1536. size_type __n = _VSTD::distance(__f, __l);
  1537. allocator_type& __a = __alloc();
  1538. size_type __back_capacity = __back_spare();
  1539. if (__n > __back_capacity)
  1540. __add_back_capacity(__n - __back_capacity);
  1541. // __n <= __back_capacity
  1542. for (__deque_block_range __br : __deque_range(end(), end() + __n)) {
  1543. _ConstructTransaction __tx(this, __br);
  1544. for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void)++__f) {
  1545. __alloc_traits::construct(__a, _VSTD::__to_address(__tx.__pos_), *__f);
  1546. }
  1547. }
  1548. }
  1549. template <class _Tp, class _Allocator>
  1550. void
  1551. deque<_Tp, _Allocator>::__append(size_type __n)
  1552. {
  1553. allocator_type& __a = __alloc();
  1554. size_type __back_capacity = __back_spare();
  1555. if (__n > __back_capacity)
  1556. __add_back_capacity(__n - __back_capacity);
  1557. // __n <= __back_capacity
  1558. for (__deque_block_range __br : __deque_range(end(), end() + __n)) {
  1559. _ConstructTransaction __tx(this, __br);
  1560. for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
  1561. __alloc_traits::construct(__a, _VSTD::__to_address(__tx.__pos_));
  1562. }
  1563. }
  1564. }
  1565. template <class _Tp, class _Allocator>
  1566. void
  1567. deque<_Tp, _Allocator>::__append(size_type __n, const value_type& __v)
  1568. {
  1569. allocator_type& __a = __alloc();
  1570. size_type __back_capacity = __back_spare();
  1571. if (__n > __back_capacity)
  1572. __add_back_capacity(__n - __back_capacity);
  1573. // __n <= __back_capacity
  1574. for (__deque_block_range __br : __deque_range(end(), end() + __n)) {
  1575. _ConstructTransaction __tx(this, __br);
  1576. for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
  1577. __alloc_traits::construct(__a, _VSTD::__to_address(__tx.__pos_), __v);
  1578. }
  1579. }
  1580. }
  1581. // Create front capacity for one block of elements.
  1582. // Strong guarantee. Either do it or don't touch anything.
  1583. template <class _Tp, class _Allocator>
  1584. void
  1585. deque<_Tp, _Allocator>::__add_front_capacity()
  1586. {
  1587. allocator_type& __a = __alloc();
  1588. if (__back_spare() >= __block_size)
  1589. {
  1590. __start_ += __block_size;
  1591. pointer __pt = __map_.back();
  1592. __map_.pop_back();
  1593. __map_.push_front(__pt);
  1594. }
  1595. // Else if __map_.size() < __map_.capacity() then we need to allocate 1 buffer
  1596. else if (__map_.size() < __map_.capacity())
  1597. { // we can put the new buffer into the map, but don't shift things around
  1598. // until all buffers are allocated. If we throw, we don't need to fix
  1599. // anything up (any added buffers are undetectible)
  1600. if (__map_.__front_spare() > 0)
  1601. __map_.push_front(__alloc_traits::allocate(__a, __block_size));
  1602. else
  1603. {
  1604. __map_.push_back(__alloc_traits::allocate(__a, __block_size));
  1605. // Done allocating, reorder capacity
  1606. pointer __pt = __map_.back();
  1607. __map_.pop_back();
  1608. __map_.push_front(__pt);
  1609. }
  1610. __start_ = __map_.size() == 1 ?
  1611. __block_size / 2 :
  1612. __start_ + __block_size;
  1613. }
  1614. // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
  1615. else
  1616. {
  1617. __split_buffer<pointer, __pointer_allocator&>
  1618. __buf(std::max<size_type>(2 * __map_.capacity(), 1),
  1619. 0, __map_.__alloc());
  1620. typedef __allocator_destructor<_Allocator> _Dp;
  1621. unique_ptr<pointer, _Dp> __hold(
  1622. __alloc_traits::allocate(__a, __block_size),
  1623. _Dp(__a, __block_size));
  1624. __buf.push_back(__hold.get());
  1625. __hold.release();
  1626. for (__map_pointer __i = __map_.begin();
  1627. __i != __map_.end(); ++__i)
  1628. __buf.push_back(*__i);
  1629. _VSTD::swap(__map_.__first_, __buf.__first_);
  1630. _VSTD::swap(__map_.__begin_, __buf.__begin_);
  1631. _VSTD::swap(__map_.__end_, __buf.__end_);
  1632. _VSTD::swap(__map_.__end_cap(), __buf.__end_cap());
  1633. __start_ = __map_.size() == 1 ?
  1634. __block_size / 2 :
  1635. __start_ + __block_size;
  1636. }
  1637. }
  1638. // Create front capacity for __n elements.
  1639. // Strong guarantee. Either do it or don't touch anything.
  1640. template <class _Tp, class _Allocator>
  1641. void
  1642. deque<_Tp, _Allocator>::__add_front_capacity(size_type __n)
  1643. {
  1644. allocator_type& __a = __alloc();
  1645. size_type __nb = __recommend_blocks(__n + __map_.empty());
  1646. // Number of unused blocks at back:
  1647. size_type __back_capacity = __back_spare() / __block_size;
  1648. __back_capacity = _VSTD::min(__back_capacity, __nb); // don't take more than you need
  1649. __nb -= __back_capacity; // number of blocks need to allocate
  1650. // If __nb == 0, then we have sufficient capacity.
  1651. if (__nb == 0)
  1652. {
  1653. __start_ += __block_size * __back_capacity;
  1654. for (; __back_capacity > 0; --__back_capacity)
  1655. {
  1656. pointer __pt = __map_.back();
  1657. __map_.pop_back();
  1658. __map_.push_front(__pt);
  1659. }
  1660. }
  1661. // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
  1662. else if (__nb <= __map_.capacity() - __map_.size())
  1663. { // we can put the new buffers into the map, but don't shift things around
  1664. // until all buffers are allocated. If we throw, we don't need to fix
  1665. // anything up (any added buffers are undetectible)
  1666. for (; __nb > 0; --__nb, __start_ += __block_size - (__map_.size() == 1))
  1667. {
  1668. if (__map_.__front_spare() == 0)
  1669. break;
  1670. __map_.push_front(__alloc_traits::allocate(__a, __block_size));
  1671. }
  1672. for (; __nb > 0; --__nb, ++__back_capacity)
  1673. __map_.push_back(__alloc_traits::allocate(__a, __block_size));
  1674. // Done allocating, reorder capacity
  1675. __start_ += __back_capacity * __block_size;
  1676. for (; __back_capacity > 0; --__back_capacity)
  1677. {
  1678. pointer __pt = __map_.back();
  1679. __map_.pop_back();
  1680. __map_.push_front(__pt);
  1681. }
  1682. }
  1683. // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
  1684. else
  1685. {
  1686. size_type __ds = (__nb + __back_capacity) * __block_size - __map_.empty();
  1687. __split_buffer<pointer, __pointer_allocator&>
  1688. __buf(std::max<size_type>(2* __map_.capacity(),
  1689. __nb + __map_.size()),
  1690. 0, __map_.__alloc());
  1691. #ifndef _LIBCPP_NO_EXCEPTIONS
  1692. try
  1693. {
  1694. #endif // _LIBCPP_NO_EXCEPTIONS
  1695. for (; __nb > 0; --__nb)
  1696. __buf.push_back(__alloc_traits::allocate(__a, __block_size));
  1697. #ifndef _LIBCPP_NO_EXCEPTIONS
  1698. }
  1699. catch (...)
  1700. {
  1701. for (__map_pointer __i = __buf.begin();
  1702. __i != __buf.end(); ++__i)
  1703. __alloc_traits::deallocate(__a, *__i, __block_size);
  1704. throw;
  1705. }
  1706. #endif // _LIBCPP_NO_EXCEPTIONS
  1707. for (; __back_capacity > 0; --__back_capacity)
  1708. {
  1709. __buf.push_back(__map_.back());
  1710. __map_.pop_back();
  1711. }
  1712. for (__map_pointer __i = __map_.begin();
  1713. __i != __map_.end(); ++__i)
  1714. __buf.push_back(*__i);
  1715. _VSTD::swap(__map_.__first_, __buf.__first_);
  1716. _VSTD::swap(__map_.__begin_, __buf.__begin_);
  1717. _VSTD::swap(__map_.__end_, __buf.__end_);
  1718. _VSTD::swap(__map_.__end_cap(), __buf.__end_cap());
  1719. __start_ += __ds;
  1720. }
  1721. }
  1722. // Create back capacity for one block of elements.
  1723. // Strong guarantee. Either do it or don't touch anything.
  1724. template <class _Tp, class _Allocator>
  1725. void
  1726. deque<_Tp, _Allocator>::__add_back_capacity()
  1727. {
  1728. allocator_type& __a = __alloc();
  1729. if (__front_spare() >= __block_size)
  1730. {
  1731. __start_ -= __block_size;
  1732. pointer __pt = __map_.front();
  1733. __map_.pop_front();
  1734. __map_.push_back(__pt);
  1735. }
  1736. // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
  1737. else if (__map_.size() < __map_.capacity())
  1738. { // we can put the new buffer into the map, but don't shift things around
  1739. // until it is allocated. If we throw, we don't need to fix
  1740. // anything up (any added buffers are undetectible)
  1741. if (__map_.__back_spare() != 0)
  1742. __map_.push_back(__alloc_traits::allocate(__a, __block_size));
  1743. else
  1744. {
  1745. __map_.push_front(__alloc_traits::allocate(__a, __block_size));
  1746. // Done allocating, reorder capacity
  1747. pointer __pt = __map_.front();
  1748. __map_.pop_front();
  1749. __map_.push_back(__pt);
  1750. }
  1751. }
  1752. // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
  1753. else
  1754. {
  1755. __split_buffer<pointer, __pointer_allocator&>
  1756. __buf(std::max<size_type>(2* __map_.capacity(), 1),
  1757. __map_.size(),
  1758. __map_.__alloc());
  1759. typedef __allocator_destructor<_Allocator> _Dp;
  1760. unique_ptr<pointer, _Dp> __hold(
  1761. __alloc_traits::allocate(__a, __block_size),
  1762. _Dp(__a, __block_size));
  1763. __buf.push_back(__hold.get());
  1764. __hold.release();
  1765. for (__map_pointer __i = __map_.end();
  1766. __i != __map_.begin();)
  1767. __buf.push_front(*--__i);
  1768. _VSTD::swap(__map_.__first_, __buf.__first_);
  1769. _VSTD::swap(__map_.__begin_, __buf.__begin_);
  1770. _VSTD::swap(__map_.__end_, __buf.__end_);
  1771. _VSTD::swap(__map_.__end_cap(), __buf.__end_cap());
  1772. }
  1773. }
  1774. // Create back capacity for __n elements.
  1775. // Strong guarantee. Either do it or don't touch anything.
  1776. template <class _Tp, class _Allocator>
  1777. void
  1778. deque<_Tp, _Allocator>::__add_back_capacity(size_type __n)
  1779. {
  1780. allocator_type& __a = __alloc();
  1781. size_type __nb = __recommend_blocks(__n + __map_.empty());
  1782. // Number of unused blocks at front:
  1783. size_type __front_capacity = __front_spare() / __block_size;
  1784. __front_capacity = _VSTD::min(__front_capacity, __nb); // don't take more than you need
  1785. __nb -= __front_capacity; // number of blocks need to allocate
  1786. // If __nb == 0, then we have sufficient capacity.
  1787. if (__nb == 0)
  1788. {
  1789. __start_ -= __block_size * __front_capacity;
  1790. for (; __front_capacity > 0; --__front_capacity)
  1791. {
  1792. pointer __pt = __map_.front();
  1793. __map_.pop_front();
  1794. __map_.push_back(__pt);
  1795. }
  1796. }
  1797. // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
  1798. else if (__nb <= __map_.capacity() - __map_.size())
  1799. { // we can put the new buffers into the map, but don't shift things around
  1800. // until all buffers are allocated. If we throw, we don't need to fix
  1801. // anything up (any added buffers are undetectible)
  1802. for (; __nb > 0; --__nb)
  1803. {
  1804. if (__map_.__back_spare() == 0)
  1805. break;
  1806. __map_.push_back(__alloc_traits::allocate(__a, __block_size));
  1807. }
  1808. for (; __nb > 0; --__nb, ++__front_capacity, __start_ +=
  1809. __block_size - (__map_.size() == 1))
  1810. __map_.push_front(__alloc_traits::allocate(__a, __block_size));
  1811. // Done allocating, reorder capacity
  1812. __start_ -= __block_size * __front_capacity;
  1813. for (; __front_capacity > 0; --__front_capacity)
  1814. {
  1815. pointer __pt = __map_.front();
  1816. __map_.pop_front();
  1817. __map_.push_back(__pt);
  1818. }
  1819. }
  1820. // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
  1821. else
  1822. {
  1823. size_type __ds = __front_capacity * __block_size;
  1824. __split_buffer<pointer, __pointer_allocator&>
  1825. __buf(std::max<size_type>(2* __map_.capacity(),
  1826. __nb + __map_.size()),
  1827. __map_.size() - __front_capacity,
  1828. __map_.__alloc());
  1829. #ifndef _LIBCPP_NO_EXCEPTIONS
  1830. try
  1831. {
  1832. #endif // _LIBCPP_NO_EXCEPTIONS
  1833. for (; __nb > 0; --__nb)
  1834. __buf.push_back(__alloc_traits::allocate(__a, __block_size));
  1835. #ifndef _LIBCPP_NO_EXCEPTIONS
  1836. }
  1837. catch (...)
  1838. {
  1839. for (__map_pointer __i = __buf.begin();
  1840. __i != __buf.end(); ++__i)
  1841. __alloc_traits::deallocate(__a, *__i, __block_size);
  1842. throw;
  1843. }
  1844. #endif // _LIBCPP_NO_EXCEPTIONS
  1845. for (; __front_capacity > 0; --__front_capacity)
  1846. {
  1847. __buf.push_back(__map_.front());
  1848. __map_.pop_front();
  1849. }
  1850. for (__map_pointer __i = __map_.end();
  1851. __i != __map_.begin();)
  1852. __buf.push_front(*--__i);
  1853. _VSTD::swap(__map_.__first_, __buf.__first_);
  1854. _VSTD::swap(__map_.__begin_, __buf.__begin_);
  1855. _VSTD::swap(__map_.__end_, __buf.__end_);
  1856. _VSTD::swap(__map_.__end_cap(), __buf.__end_cap());
  1857. __start_ -= __ds;
  1858. }
  1859. }
  1860. template <class _Tp, class _Allocator>
  1861. void
  1862. deque<_Tp, _Allocator>::pop_front()
  1863. {
  1864. allocator_type& __a = __alloc();
  1865. __alloc_traits::destroy(__a, _VSTD::__to_address(*(__map_.begin() +
  1866. __start_ / __block_size) +
  1867. __start_ % __block_size));
  1868. --__size();
  1869. ++__start_;
  1870. __maybe_remove_front_spare();
  1871. }
  1872. template <class _Tp, class _Allocator>
  1873. void
  1874. deque<_Tp, _Allocator>::pop_back()
  1875. {
  1876. _LIBCPP_ASSERT(!empty(), "deque::pop_back called on an empty deque");
  1877. allocator_type& __a = __alloc();
  1878. size_type __p = size() + __start_ - 1;
  1879. __alloc_traits::destroy(__a, _VSTD::__to_address(*(__map_.begin() +
  1880. __p / __block_size) +
  1881. __p % __block_size));
  1882. --__size();
  1883. __maybe_remove_back_spare();
  1884. }
  1885. // move assign [__f, __l) to [__r, __r + (__l-__f)).
  1886. // If __vt points into [__f, __l), then subtract (__f - __r) from __vt.
  1887. template <class _Tp, class _Allocator>
  1888. typename deque<_Tp, _Allocator>::iterator
  1889. deque<_Tp, _Allocator>::__move_and_check(iterator __f, iterator __l, iterator __r,
  1890. const_pointer& __vt)
  1891. {
  1892. // as if
  1893. // for (; __f != __l; ++__f, ++__r)
  1894. // *__r = _VSTD::move(*__f);
  1895. difference_type __n = __l - __f;
  1896. while (__n > 0)
  1897. {
  1898. pointer __fb = __f.__ptr_;
  1899. pointer __fe = *__f.__m_iter_ + __block_size;
  1900. difference_type __bs = __fe - __fb;
  1901. if (__bs > __n)
  1902. {
  1903. __bs = __n;
  1904. __fe = __fb + __bs;
  1905. }
  1906. if (__fb <= __vt && __vt < __fe)
  1907. __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) -= __f - __r).__ptr_;
  1908. __r = _VSTD::move(__fb, __fe, __r);
  1909. __n -= __bs;
  1910. __f += __bs;
  1911. }
  1912. return __r;
  1913. }
  1914. // move assign [__f, __l) to [__r - (__l-__f), __r) backwards.
  1915. // If __vt points into [__f, __l), then add (__r - __l) to __vt.
  1916. template <class _Tp, class _Allocator>
  1917. typename deque<_Tp, _Allocator>::iterator
  1918. deque<_Tp, _Allocator>::__move_backward_and_check(iterator __f, iterator __l, iterator __r,
  1919. const_pointer& __vt)
  1920. {
  1921. // as if
  1922. // while (__f != __l)
  1923. // *--__r = _VSTD::move(*--__l);
  1924. difference_type __n = __l - __f;
  1925. while (__n > 0)
  1926. {
  1927. --__l;
  1928. pointer __lb = *__l.__m_iter_;
  1929. pointer __le = __l.__ptr_ + 1;
  1930. difference_type __bs = __le - __lb;
  1931. if (__bs > __n)
  1932. {
  1933. __bs = __n;
  1934. __lb = __le - __bs;
  1935. }
  1936. if (__lb <= __vt && __vt < __le)
  1937. __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) += __r - __l - 1).__ptr_;
  1938. __r = _VSTD::move_backward(__lb, __le, __r);
  1939. __n -= __bs;
  1940. __l -= __bs - 1;
  1941. }
  1942. return __r;
  1943. }
  1944. // move construct [__f, __l) to [__r, __r + (__l-__f)).
  1945. // If __vt points into [__f, __l), then add (__r - __f) to __vt.
  1946. template <class _Tp, class _Allocator>
  1947. void
  1948. deque<_Tp, _Allocator>::__move_construct_and_check(iterator __f, iterator __l,
  1949. iterator __r, const_pointer& __vt)
  1950. {
  1951. allocator_type& __a = __alloc();
  1952. // as if
  1953. // for (; __f != __l; ++__r, ++__f, ++__size())
  1954. // __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__f));
  1955. difference_type __n = __l - __f;
  1956. while (__n > 0)
  1957. {
  1958. pointer __fb = __f.__ptr_;
  1959. pointer __fe = *__f.__m_iter_ + __block_size;
  1960. difference_type __bs = __fe - __fb;
  1961. if (__bs > __n)
  1962. {
  1963. __bs = __n;
  1964. __fe = __fb + __bs;
  1965. }
  1966. if (__fb <= __vt && __vt < __fe)
  1967. __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) += __r - __f).__ptr_;
  1968. for (; __fb != __fe; ++__fb, ++__r, ++__size())
  1969. __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__fb));
  1970. __n -= __bs;
  1971. __f += __bs;
  1972. }
  1973. }
  1974. // move construct [__f, __l) to [__r - (__l-__f), __r) backwards.
  1975. // If __vt points into [__f, __l), then subtract (__l - __r) from __vt.
  1976. template <class _Tp, class _Allocator>
  1977. void
  1978. deque<_Tp, _Allocator>::__move_construct_backward_and_check(iterator __f, iterator __l,
  1979. iterator __r, const_pointer& __vt)
  1980. {
  1981. allocator_type& __a = __alloc();
  1982. // as if
  1983. // for (iterator __j = __l; __j != __f;)
  1984. // {
  1985. // __alloc_traitsconstruct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__j));
  1986. // --__start_;
  1987. // ++__size();
  1988. // }
  1989. difference_type __n = __l - __f;
  1990. while (__n > 0)
  1991. {
  1992. --__l;
  1993. pointer __lb = *__l.__m_iter_;
  1994. pointer __le = __l.__ptr_ + 1;
  1995. difference_type __bs = __le - __lb;
  1996. if (__bs > __n)
  1997. {
  1998. __bs = __n;
  1999. __lb = __le - __bs;
  2000. }
  2001. if (__lb <= __vt && __vt < __le)
  2002. __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) -= __l - __r + 1).__ptr_;
  2003. while (__le != __lb)
  2004. {
  2005. __alloc_traits::construct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__le));
  2006. --__start_;
  2007. ++__size();
  2008. }
  2009. __n -= __bs;
  2010. __l -= __bs - 1;
  2011. }
  2012. }
  2013. template <class _Tp, class _Allocator>
  2014. typename deque<_Tp, _Allocator>::iterator
  2015. deque<_Tp, _Allocator>::erase(const_iterator __f)
  2016. {
  2017. iterator __b = begin();
  2018. difference_type __pos = __f - __b;
  2019. iterator __p = __b + __pos;
  2020. allocator_type& __a = __alloc();
  2021. if (static_cast<size_t>(__pos) <= (size() - 1) / 2)
  2022. { // erase from front
  2023. _VSTD::move_backward(__b, __p, _VSTD::next(__p));
  2024. __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
  2025. --__size();
  2026. ++__start_;
  2027. __maybe_remove_front_spare();
  2028. }
  2029. else
  2030. { // erase from back
  2031. iterator __i = _VSTD::move(_VSTD::next(__p), end(), __p);
  2032. __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
  2033. --__size();
  2034. __maybe_remove_back_spare();
  2035. }
  2036. return begin() + __pos;
  2037. }
  2038. template <class _Tp, class _Allocator>
  2039. typename deque<_Tp, _Allocator>::iterator
  2040. deque<_Tp, _Allocator>::erase(const_iterator __f, const_iterator __l)
  2041. {
  2042. difference_type __n = __l - __f;
  2043. iterator __b = begin();
  2044. difference_type __pos = __f - __b;
  2045. iterator __p = __b + __pos;
  2046. if (__n > 0)
  2047. {
  2048. allocator_type& __a = __alloc();
  2049. if (static_cast<size_t>(__pos) <= (size() - __n) / 2)
  2050. { // erase from front
  2051. iterator __i = _VSTD::move_backward(__b, __p, __p + __n);
  2052. for (; __b != __i; ++__b)
  2053. __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
  2054. __size() -= __n;
  2055. __start_ += __n;
  2056. while (__maybe_remove_front_spare()) {
  2057. }
  2058. }
  2059. else
  2060. { // erase from back
  2061. iterator __i = _VSTD::move(__p + __n, end(), __p);
  2062. for (iterator __e = end(); __i != __e; ++__i)
  2063. __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
  2064. __size() -= __n;
  2065. while (__maybe_remove_back_spare()) {
  2066. }
  2067. }
  2068. }
  2069. return begin() + __pos;
  2070. }
  2071. template <class _Tp, class _Allocator>
  2072. void
  2073. deque<_Tp, _Allocator>::__erase_to_end(const_iterator __f)
  2074. {
  2075. iterator __e = end();
  2076. difference_type __n = __e - __f;
  2077. if (__n > 0)
  2078. {
  2079. allocator_type& __a = __alloc();
  2080. iterator __b = begin();
  2081. difference_type __pos = __f - __b;
  2082. for (iterator __p = __b + __pos; __p != __e; ++__p)
  2083. __alloc_traits::destroy(__a, _VSTD::addressof(*__p));
  2084. __size() -= __n;
  2085. while (__maybe_remove_back_spare()) {
  2086. }
  2087. }
  2088. }
  2089. template <class _Tp, class _Allocator>
  2090. inline
  2091. void
  2092. deque<_Tp, _Allocator>::swap(deque& __c)
  2093. #if _LIBCPP_STD_VER >= 14
  2094. _NOEXCEPT
  2095. #else
  2096. _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
  2097. __is_nothrow_swappable<allocator_type>::value)
  2098. #endif
  2099. {
  2100. __map_.swap(__c.__map_);
  2101. _VSTD::swap(__start_, __c.__start_);
  2102. _VSTD::swap(__size(), __c.__size());
  2103. _VSTD::__swap_allocator(__alloc(), __c.__alloc());
  2104. }
  2105. template <class _Tp, class _Allocator>
  2106. _LIBCPP_REINITIALIZES_OBJECT
  2107. inline
  2108. void
  2109. deque<_Tp, _Allocator>::clear() _NOEXCEPT
  2110. {
  2111. allocator_type& __a = __alloc();
  2112. for (iterator __i = begin(), __e = end(); __i != __e; ++__i)
  2113. __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
  2114. __size() = 0;
  2115. while (__map_.size() > 2)
  2116. {
  2117. __alloc_traits::deallocate(__a, __map_.front(), __block_size);
  2118. __map_.pop_front();
  2119. }
  2120. switch (__map_.size())
  2121. {
  2122. case 1:
  2123. __start_ = __block_size / 2;
  2124. break;
  2125. case 2:
  2126. __start_ = __block_size;
  2127. break;
  2128. }
  2129. }
  2130. template <class _Tp, class _Allocator>
  2131. inline _LIBCPP_HIDE_FROM_ABI
  2132. bool
  2133. operator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
  2134. {
  2135. const typename deque<_Tp, _Allocator>::size_type __sz = __x.size();
  2136. return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
  2137. }
  2138. template <class _Tp, class _Allocator>
  2139. inline _LIBCPP_HIDE_FROM_ABI
  2140. bool
  2141. operator!=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
  2142. {
  2143. return !(__x == __y);
  2144. }
  2145. template <class _Tp, class _Allocator>
  2146. inline _LIBCPP_HIDE_FROM_ABI
  2147. bool
  2148. operator< (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
  2149. {
  2150. return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
  2151. }
  2152. template <class _Tp, class _Allocator>
  2153. inline _LIBCPP_HIDE_FROM_ABI
  2154. bool
  2155. operator> (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
  2156. {
  2157. return __y < __x;
  2158. }
  2159. template <class _Tp, class _Allocator>
  2160. inline _LIBCPP_HIDE_FROM_ABI
  2161. bool
  2162. operator>=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
  2163. {
  2164. return !(__x < __y);
  2165. }
  2166. template <class _Tp, class _Allocator>
  2167. inline _LIBCPP_HIDE_FROM_ABI
  2168. bool
  2169. operator<=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
  2170. {
  2171. return !(__y < __x);
  2172. }
  2173. template <class _Tp, class _Allocator>
  2174. inline _LIBCPP_HIDE_FROM_ABI
  2175. void
  2176. swap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
  2177. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
  2178. {
  2179. __x.swap(__y);
  2180. }
  2181. #if _LIBCPP_STD_VER > 17
  2182. template <class _Tp, class _Allocator, class _Up>
  2183. inline _LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::size_type
  2184. erase(deque<_Tp, _Allocator>& __c, const _Up& __v) {
  2185. auto __old_size = __c.size();
  2186. __c.erase(_VSTD::remove(__c.begin(), __c.end(), __v), __c.end());
  2187. return __old_size - __c.size();
  2188. }
  2189. template <class _Tp, class _Allocator, class _Predicate>
  2190. inline _LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::size_type
  2191. erase_if(deque<_Tp, _Allocator>& __c, _Predicate __pred) {
  2192. auto __old_size = __c.size();
  2193. __c.erase(_VSTD::remove_if(__c.begin(), __c.end(), __pred), __c.end());
  2194. return __old_size - __c.size();
  2195. }
  2196. template <>
  2197. inline constexpr bool __format::__enable_insertable<std::deque<char>> = true;
  2198. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  2199. template <>
  2200. inline constexpr bool __format::__enable_insertable<std::deque<wchar_t>> = true;
  2201. #endif
  2202. #endif // _LIBCPP_STD_VER > 17
  2203. _LIBCPP_END_NAMESPACE_STD
  2204. #if _LIBCPP_STD_VER > 14
  2205. _LIBCPP_BEGIN_NAMESPACE_STD
  2206. namespace pmr {
  2207. template <class _ValueT>
  2208. using deque = std::deque<_ValueT, polymorphic_allocator<_ValueT>>;
  2209. } // namespace pmr
  2210. _LIBCPP_END_NAMESPACE_STD
  2211. #endif
  2212. _LIBCPP_POP_MACROS
  2213. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  2214. # include <algorithm>
  2215. # include <atomic>
  2216. # include <concepts>
  2217. # include <functional>
  2218. # include <iosfwd>
  2219. # include <iterator>
  2220. # include <typeinfo>
  2221. #endif
  2222. #endif // _LIBCPP_DEQUE