deque 107 KB

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