deque 107 KB

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