deque 100 KB

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