deque 102 KB

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