deque 106 KB

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