deque 107 KB

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