deque 101 KB

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