vector 128 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541
  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_VECTOR
  10. #define _LIBCPP_VECTOR
  11. /*
  12. vector synopsis
  13. namespace std
  14. {
  15. template <class T, class Allocator = allocator<T> >
  16. class vector
  17. {
  18. public:
  19. typedef T value_type;
  20. typedef Allocator allocator_type;
  21. typedef typename allocator_type::reference reference;
  22. typedef typename allocator_type::const_reference const_reference;
  23. typedef implementation-defined iterator;
  24. typedef implementation-defined const_iterator;
  25. typedef typename allocator_type::size_type size_type;
  26. typedef typename allocator_type::difference_type difference_type;
  27. typedef typename allocator_type::pointer pointer;
  28. typedef typename allocator_type::const_pointer const_pointer;
  29. typedef std::reverse_iterator<iterator> reverse_iterator;
  30. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  31. vector()
  32. noexcept(is_nothrow_default_constructible<allocator_type>::value);
  33. explicit vector(const allocator_type&);
  34. explicit vector(size_type n);
  35. explicit vector(size_type n, const allocator_type&); // C++14
  36. vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
  37. template <class InputIterator>
  38. vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
  39. template<container-compatible-range<T> R>
  40. constexpr vector(from_range_t, R&& rg, const Allocator& = Allocator()); // C++23
  41. vector(const vector& x);
  42. vector(vector&& x)
  43. noexcept(is_nothrow_move_constructible<allocator_type>::value);
  44. vector(initializer_list<value_type> il);
  45. vector(initializer_list<value_type> il, const allocator_type& a);
  46. ~vector();
  47. vector& operator=(const vector& x);
  48. vector& operator=(vector&& x)
  49. noexcept(
  50. allocator_type::propagate_on_container_move_assignment::value ||
  51. allocator_type::is_always_equal::value); // C++17
  52. vector& operator=(initializer_list<value_type> il);
  53. template <class InputIterator>
  54. void assign(InputIterator first, InputIterator last);
  55. template<container-compatible-range<T> R>
  56. constexpr void assign_range(R&& rg); // C++23
  57. void assign(size_type n, const value_type& u);
  58. void assign(initializer_list<value_type> il);
  59. allocator_type get_allocator() const noexcept;
  60. iterator begin() noexcept;
  61. const_iterator begin() const noexcept;
  62. iterator end() noexcept;
  63. const_iterator end() const noexcept;
  64. reverse_iterator rbegin() noexcept;
  65. const_reverse_iterator rbegin() const noexcept;
  66. reverse_iterator rend() noexcept;
  67. const_reverse_iterator rend() const noexcept;
  68. const_iterator cbegin() const noexcept;
  69. const_iterator cend() const noexcept;
  70. const_reverse_iterator crbegin() const noexcept;
  71. const_reverse_iterator crend() const noexcept;
  72. size_type size() const noexcept;
  73. size_type max_size() const noexcept;
  74. size_type capacity() const noexcept;
  75. bool empty() const noexcept;
  76. void reserve(size_type n);
  77. void shrink_to_fit() noexcept;
  78. reference operator[](size_type n);
  79. const_reference operator[](size_type n) const;
  80. reference at(size_type n);
  81. const_reference at(size_type n) const;
  82. reference front();
  83. const_reference front() const;
  84. reference back();
  85. const_reference back() const;
  86. value_type* data() noexcept;
  87. const value_type* data() const noexcept;
  88. void push_back(const value_type& x);
  89. void push_back(value_type&& x);
  90. template <class... Args>
  91. reference emplace_back(Args&&... args); // reference in C++17
  92. template<container-compatible-range<T> R>
  93. constexpr void append_range(R&& rg); // C++23
  94. void pop_back();
  95. template <class... Args> iterator emplace(const_iterator position, Args&&... args);
  96. iterator insert(const_iterator position, const value_type& x);
  97. iterator insert(const_iterator position, value_type&& x);
  98. iterator insert(const_iterator position, size_type n, const value_type& x);
  99. template <class InputIterator>
  100. iterator insert(const_iterator position, InputIterator first, InputIterator last);
  101. template<container-compatible-range<T> R>
  102. constexpr iterator insert_range(const_iterator position, R&& rg); // C++23
  103. iterator insert(const_iterator position, initializer_list<value_type> il);
  104. iterator erase(const_iterator position);
  105. iterator erase(const_iterator first, const_iterator last);
  106. void clear() noexcept;
  107. void resize(size_type sz);
  108. void resize(size_type sz, const value_type& c);
  109. void swap(vector&)
  110. noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
  111. allocator_traits<allocator_type>::is_always_equal::value); // C++17
  112. bool __invariants() const;
  113. };
  114. template <class Allocator = allocator<T> >
  115. class vector<bool, Allocator>
  116. {
  117. public:
  118. typedef bool value_type;
  119. typedef Allocator allocator_type;
  120. typedef implementation-defined iterator;
  121. typedef implementation-defined const_iterator;
  122. typedef typename allocator_type::size_type size_type;
  123. typedef typename allocator_type::difference_type difference_type;
  124. typedef iterator pointer;
  125. typedef const_iterator const_pointer;
  126. typedef std::reverse_iterator<iterator> reverse_iterator;
  127. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  128. class reference
  129. {
  130. public:
  131. reference(const reference&) noexcept;
  132. operator bool() const noexcept;
  133. reference& operator=(bool x) noexcept;
  134. reference& operator=(const reference& x) noexcept;
  135. iterator operator&() const noexcept;
  136. void flip() noexcept;
  137. };
  138. class const_reference
  139. {
  140. public:
  141. const_reference(const reference&) noexcept;
  142. operator bool() const noexcept;
  143. const_iterator operator&() const noexcept;
  144. };
  145. vector()
  146. noexcept(is_nothrow_default_constructible<allocator_type>::value);
  147. explicit vector(const allocator_type&);
  148. explicit vector(size_type n, const allocator_type& a = allocator_type()); // C++14
  149. vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
  150. template <class InputIterator>
  151. vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
  152. template<container-compatible-range<bool> R>
  153. constexpr vector(from_range_t, R&& rg, const Allocator& = Allocator());
  154. vector(const vector& x);
  155. vector(vector&& x)
  156. noexcept(is_nothrow_move_constructible<allocator_type>::value);
  157. vector(initializer_list<value_type> il);
  158. vector(initializer_list<value_type> il, const allocator_type& a);
  159. ~vector();
  160. vector& operator=(const vector& x);
  161. vector& operator=(vector&& x)
  162. noexcept(
  163. allocator_type::propagate_on_container_move_assignment::value ||
  164. allocator_type::is_always_equal::value); // C++17
  165. vector& operator=(initializer_list<value_type> il);
  166. template <class InputIterator>
  167. void assign(InputIterator first, InputIterator last);
  168. template<container-compatible-range<T> R>
  169. constexpr void assign_range(R&& rg); // C++23
  170. void assign(size_type n, const value_type& u);
  171. void assign(initializer_list<value_type> il);
  172. allocator_type get_allocator() const noexcept;
  173. iterator begin() noexcept;
  174. const_iterator begin() const noexcept;
  175. iterator end() noexcept;
  176. const_iterator end() const noexcept;
  177. reverse_iterator rbegin() noexcept;
  178. const_reverse_iterator rbegin() const noexcept;
  179. reverse_iterator rend() noexcept;
  180. const_reverse_iterator rend() const noexcept;
  181. const_iterator cbegin() const noexcept;
  182. const_iterator cend() const noexcept;
  183. const_reverse_iterator crbegin() const noexcept;
  184. const_reverse_iterator crend() const noexcept;
  185. size_type size() const noexcept;
  186. size_type max_size() const noexcept;
  187. size_type capacity() const noexcept;
  188. bool empty() const noexcept;
  189. void reserve(size_type n);
  190. void shrink_to_fit() noexcept;
  191. reference operator[](size_type n);
  192. const_reference operator[](size_type n) const;
  193. reference at(size_type n);
  194. const_reference at(size_type n) const;
  195. reference front();
  196. const_reference front() const;
  197. reference back();
  198. const_reference back() const;
  199. void push_back(const value_type& x);
  200. template <class... Args> reference emplace_back(Args&&... args); // C++14; reference in C++17
  201. template<container-compatible-range<T> R>
  202. constexpr void append_range(R&& rg); // C++23
  203. void pop_back();
  204. template <class... Args> iterator emplace(const_iterator position, Args&&... args); // C++14
  205. iterator insert(const_iterator position, const value_type& x);
  206. iterator insert(const_iterator position, size_type n, const value_type& x);
  207. template <class InputIterator>
  208. iterator insert(const_iterator position, InputIterator first, InputIterator last);
  209. template<container-compatible-range<T> R>
  210. constexpr iterator insert_range(const_iterator position, R&& rg); // C++23
  211. iterator insert(const_iterator position, initializer_list<value_type> il);
  212. iterator erase(const_iterator position);
  213. iterator erase(const_iterator first, const_iterator last);
  214. void clear() noexcept;
  215. void resize(size_type sz);
  216. void resize(size_type sz, value_type x);
  217. void swap(vector&)
  218. noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
  219. allocator_traits<allocator_type>::is_always_equal::value); // C++17
  220. void flip() noexcept;
  221. bool __invariants() const;
  222. };
  223. template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
  224. vector(InputIterator, InputIterator, Allocator = Allocator())
  225. -> vector<typename iterator_traits<InputIterator>::value_type, Allocator>; // C++17
  226. template<ranges::input_range R, class Allocator = allocator<ranges::range_value_t<R>>>
  227. vector(from_range_t, R&&, Allocator = Allocator())
  228. -> vector<ranges::range_value_t<R>, Allocator>; // C++23
  229. template <class Allocator> struct hash<std::vector<bool, Allocator>>;
  230. template <class T, class Allocator> bool operator==(const vector<T,Allocator>& x, const vector<T,Allocator>& y); // constexpr since C++20
  231. template <class T, class Allocator> bool operator!=(const vector<T,Allocator>& x, const vector<T,Allocator>& y); // removed in C++20
  232. template <class T, class Allocator> bool operator< (const vector<T,Allocator>& x, const vector<T,Allocator>& y); // removed in C++20
  233. template <class T, class Allocator> bool operator> (const vector<T,Allocator>& x, const vector<T,Allocator>& y); // removed in C++20
  234. template <class T, class Allocator> bool operator>=(const vector<T,Allocator>& x, const vector<T,Allocator>& y); // removed in C++20
  235. template <class T, class Allocator> bool operator<=(const vector<T,Allocator>& x, const vector<T,Allocator>& y); // removed in C++20
  236. template <class T, class Allocator> constexpr
  237. constexpr synth-three-way-result<T> operator<=>(const vector<T, Allocator>& x,
  238. const vector<T, Allocator>& y); // since C++20
  239. template <class T, class Allocator>
  240. void swap(vector<T,Allocator>& x, vector<T,Allocator>& y)
  241. noexcept(noexcept(x.swap(y)));
  242. template <class T, class Allocator, class U>
  243. typename vector<T, Allocator>::size_type
  244. erase(vector<T, Allocator>& c, const U& value); // since C++20
  245. template <class T, class Allocator, class Predicate>
  246. typename vector<T, Allocator>::size_type
  247. erase_if(vector<T, Allocator>& c, Predicate pred); // since C++20
  248. template<class T>
  249. inline constexpr bool is-vector-bool-reference = see below; // exposition only, since C++23
  250. template<class T, class charT> requires is-vector-bool-reference<T> // Since C++23
  251. struct formatter<T, charT>;
  252. } // std
  253. */
  254. #include <__algorithm/copy.h>
  255. #include <__algorithm/equal.h>
  256. #include <__algorithm/fill_n.h>
  257. #include <__algorithm/iterator_operations.h>
  258. #include <__algorithm/lexicographical_compare.h>
  259. #include <__algorithm/lexicographical_compare_three_way.h>
  260. #include <__algorithm/remove.h>
  261. #include <__algorithm/remove_if.h>
  262. #include <__algorithm/rotate.h>
  263. #include <__algorithm/unwrap_iter.h>
  264. #include <__assert> // all public C++ headers provide the assertion handler
  265. #include <__availability>
  266. #include <__bit_reference>
  267. #include <__concepts/same_as.h>
  268. #include <__config>
  269. #include <__format/enable_insertable.h>
  270. #include <__format/formatter.h>
  271. #include <__format/formatter_bool.h>
  272. #include <__functional/hash.h>
  273. #include <__functional/unary_function.h>
  274. #include <__iterator/advance.h>
  275. #include <__iterator/distance.h>
  276. #include <__iterator/iterator_traits.h>
  277. #include <__iterator/reverse_iterator.h>
  278. #include <__iterator/wrap_iter.h>
  279. #include <__memory/addressof.h>
  280. #include <__memory/allocate_at_least.h>
  281. #include <__memory/allocator_traits.h>
  282. #include <__memory/pointer_traits.h>
  283. #include <__memory/swap_allocator.h>
  284. #include <__memory/temp_value.h>
  285. #include <__memory/uninitialized_algorithms.h>
  286. #include <__memory_resource/polymorphic_allocator.h>
  287. #include <__ranges/access.h>
  288. #include <__ranges/concepts.h>
  289. #include <__ranges/container_compatible_range.h>
  290. #include <__ranges/from_range.h>
  291. #include <__ranges/size.h>
  292. #include <__split_buffer>
  293. #include <__type_traits/is_allocator.h>
  294. #include <__type_traits/is_constructible.h>
  295. #include <__type_traits/is_nothrow_move_assignable.h>
  296. #include <__type_traits/noexcept_move_assign_container.h>
  297. #include <__type_traits/type_identity.h>
  298. #include <__utility/exception_guard.h>
  299. #include <__utility/forward.h>
  300. #include <__utility/move.h>
  301. #include <__utility/pair.h>
  302. #include <__utility/swap.h>
  303. #include <climits>
  304. #include <cstring>
  305. #include <iosfwd> // for forward declaration of vector
  306. #include <limits>
  307. #include <stdexcept>
  308. #include <version>
  309. // standard-mandated includes
  310. // [iterator.range]
  311. #include <__iterator/access.h>
  312. #include <__iterator/data.h>
  313. #include <__iterator/empty.h>
  314. #include <__iterator/reverse_access.h>
  315. #include <__iterator/size.h>
  316. // [vector.syn]
  317. #include <compare>
  318. #include <initializer_list>
  319. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  320. # pragma GCC system_header
  321. #endif
  322. _LIBCPP_PUSH_MACROS
  323. #include <__undef_macros>
  324. _LIBCPP_BEGIN_NAMESPACE_STD
  325. template <class _Tp, class _Allocator /* = allocator<_Tp> */>
  326. class _LIBCPP_TEMPLATE_VIS vector
  327. {
  328. private:
  329. typedef allocator<_Tp> __default_allocator_type;
  330. public:
  331. typedef vector __self;
  332. typedef _Tp value_type;
  333. typedef _Allocator allocator_type;
  334. typedef allocator_traits<allocator_type> __alloc_traits;
  335. typedef value_type& reference;
  336. typedef const value_type& const_reference;
  337. typedef typename __alloc_traits::size_type size_type;
  338. typedef typename __alloc_traits::difference_type difference_type;
  339. typedef typename __alloc_traits::pointer pointer;
  340. typedef typename __alloc_traits::const_pointer const_pointer;
  341. #if _YNDX_LIBCPP_MAKE_VECTOR_ITERATOR_POINTERS == 1
  342. typedef pointer iterator;
  343. typedef const_pointer const_iterator;
  344. #else
  345. // TODO: Implement iterator bounds checking without requiring the global database.
  346. typedef __wrap_iter<pointer> iterator;
  347. typedef __wrap_iter<const_pointer> const_iterator;
  348. #endif
  349. typedef std::reverse_iterator<iterator> reverse_iterator;
  350. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  351. static_assert((is_same<typename allocator_type::value_type, value_type>::value),
  352. "Allocator::value_type must be same type as value_type");
  353. static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
  354. "[allocator.requirements] states that rebinding an allocator to the same type should result in the "
  355. "original allocator");
  356. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  357. vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
  358. {
  359. }
  360. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit vector(const allocator_type& __a)
  361. #if _LIBCPP_STD_VER <= 14
  362. _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
  363. #else
  364. _NOEXCEPT
  365. #endif
  366. : __end_cap_(nullptr, __a)
  367. {
  368. }
  369. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit vector(size_type __n);
  370. #if _LIBCPP_STD_VER >= 14
  371. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit vector(size_type __n, const allocator_type& __a);
  372. #endif
  373. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(size_type __n, const value_type& __x);
  374. template <class = __enable_if_t<__is_allocator<_Allocator>::value> >
  375. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  376. vector(size_type __n, const value_type& __x, const allocator_type& __a)
  377. : __end_cap_(nullptr, __a)
  378. {
  379. if (__n > 0)
  380. {
  381. __vallocate(__n);
  382. __construct_at_end(__n, __x);
  383. }
  384. }
  385. template <class _InputIterator,
  386. __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value &&
  387. is_constructible<value_type, typename iterator_traits<_InputIterator>::reference>::value,
  388. int> = 0>
  389. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(_InputIterator __first, _InputIterator __last);
  390. template <class _InputIterator,
  391. __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value &&
  392. is_constructible<value_type, typename iterator_traits<_InputIterator>::reference>::value,
  393. int> = 0>
  394. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  395. vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
  396. template <
  397. class _ForwardIterator,
  398. __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value &&
  399. is_constructible<value_type, typename iterator_traits<_ForwardIterator>::reference>::value,
  400. int> = 0>
  401. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(_ForwardIterator __first, _ForwardIterator __last);
  402. template <class _ForwardIterator,
  403. __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value &&
  404. is_constructible<value_type, typename iterator_traits<_ForwardIterator>::reference>::value,
  405. int> = 0>
  406. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  407. vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a);
  408. #if _LIBCPP_STD_VER >= 23
  409. template <_ContainerCompatibleRange<_Tp> _Range>
  410. _LIBCPP_HIDE_FROM_ABI constexpr vector(from_range_t, _Range&& __range,
  411. const allocator_type& __alloc = allocator_type()) : __end_cap_(nullptr, __alloc) {
  412. if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
  413. auto __n = static_cast<size_type>(ranges::distance(__range));
  414. __init_with_size(ranges::begin(__range), ranges::end(__range), __n);
  415. } else {
  416. __init_with_sentinel(ranges::begin(__range), ranges::end(__range));
  417. }
  418. }
  419. #endif
  420. private:
  421. class __destroy_vector {
  422. public:
  423. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI __destroy_vector(vector& __vec) : __vec_(__vec) {}
  424. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void operator()() {
  425. if (__vec_.__begin_ != nullptr) {
  426. __vec_.__clear();
  427. __vec_.__annotate_delete();
  428. __alloc_traits::deallocate(__vec_.__alloc(), __vec_.__begin_, __vec_.capacity());
  429. }
  430. }
  431. private:
  432. vector& __vec_;
  433. };
  434. public:
  435. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~vector() { __destroy_vector(*this)(); }
  436. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(const vector& __x);
  437. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(const vector& __x, const __type_identity_t<allocator_type>& __a);
  438. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  439. vector& operator=(const vector& __x);
  440. #ifndef _LIBCPP_CXX03_LANG
  441. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  442. vector(initializer_list<value_type> __il);
  443. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  444. vector(initializer_list<value_type> __il, const allocator_type& __a);
  445. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  446. vector& operator=(initializer_list<value_type> __il)
  447. {assign(__il.begin(), __il.end()); return *this;}
  448. #endif // !_LIBCPP_CXX03_LANG
  449. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  450. vector(vector&& __x)
  451. #if _LIBCPP_STD_VER >= 17
  452. noexcept;
  453. #else
  454. _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
  455. #endif
  456. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  457. vector(vector&& __x, const __type_identity_t<allocator_type>& __a);
  458. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  459. vector& operator=(vector&& __x)
  460. _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
  461. template <class _InputIterator,
  462. __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value &&
  463. is_constructible<value_type, typename iterator_traits<_InputIterator>::reference>::value,
  464. int> = 0>
  465. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void assign(_InputIterator __first, _InputIterator __last);
  466. template <
  467. class _ForwardIterator,
  468. __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value &&
  469. is_constructible<value_type, typename iterator_traits<_ForwardIterator>::reference>::value,
  470. int> = 0>
  471. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void assign(_ForwardIterator __first, _ForwardIterator __last);
  472. #if _LIBCPP_STD_VER >= 23
  473. template <_ContainerCompatibleRange<_Tp> _Range>
  474. _LIBCPP_HIDE_FROM_ABI
  475. constexpr void assign_range(_Range&& __range) {
  476. if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
  477. auto __n = static_cast<size_type>(ranges::distance(__range));
  478. __assign_with_size(ranges::begin(__range), ranges::end(__range), __n);
  479. } else {
  480. __assign_with_sentinel(ranges::begin(__range), ranges::end(__range));
  481. }
  482. }
  483. #endif
  484. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void assign(size_type __n, const_reference __u);
  485. #ifndef _LIBCPP_CXX03_LANG
  486. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  487. void assign(initializer_list<value_type> __il)
  488. {assign(__il.begin(), __il.end());}
  489. #endif
  490. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  491. allocator_type get_allocator() const _NOEXCEPT
  492. {return this->__alloc();}
  493. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT;
  494. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT;
  495. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT;
  496. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT;
  497. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  498. reverse_iterator rbegin() _NOEXCEPT
  499. {return reverse_iterator(end());}
  500. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  501. const_reverse_iterator rbegin() const _NOEXCEPT
  502. {return const_reverse_iterator(end());}
  503. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  504. reverse_iterator rend() _NOEXCEPT
  505. {return reverse_iterator(begin());}
  506. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  507. const_reverse_iterator rend() const _NOEXCEPT
  508. {return const_reverse_iterator(begin());}
  509. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  510. const_iterator cbegin() const _NOEXCEPT
  511. {return begin();}
  512. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  513. const_iterator cend() const _NOEXCEPT
  514. {return end();}
  515. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  516. const_reverse_iterator crbegin() const _NOEXCEPT
  517. {return rbegin();}
  518. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  519. const_reverse_iterator crend() const _NOEXCEPT
  520. {return rend();}
  521. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  522. size_type size() const _NOEXCEPT
  523. {return static_cast<size_type>(this->__end_ - this->__begin_);}
  524. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  525. size_type capacity() const _NOEXCEPT
  526. {return static_cast<size_type>(__end_cap() - this->__begin_);}
  527. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  528. bool empty() const _NOEXCEPT
  529. {return this->__begin_ == this->__end_;}
  530. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT;
  531. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve(size_type __n);
  532. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void shrink_to_fit() _NOEXCEPT;
  533. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference operator[](size_type __n) _NOEXCEPT;
  534. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference operator[](size_type __n) const _NOEXCEPT;
  535. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference at(size_type __n);
  536. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference at(size_type __n) const;
  537. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference front() _NOEXCEPT
  538. {
  539. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "front() called on an empty vector");
  540. return *this->__begin_;
  541. }
  542. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference front() const _NOEXCEPT
  543. {
  544. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "front() called on an empty vector");
  545. return *this->__begin_;
  546. }
  547. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference back() _NOEXCEPT
  548. {
  549. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "back() called on an empty vector");
  550. return *(this->__end_ - 1);
  551. }
  552. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference back() const _NOEXCEPT
  553. {
  554. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "back() called on an empty vector");
  555. return *(this->__end_ - 1);
  556. }
  557. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  558. value_type* data() _NOEXCEPT
  559. {return std::__to_address(this->__begin_);}
  560. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  561. const value_type* data() const _NOEXCEPT
  562. {return std::__to_address(this->__begin_);}
  563. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(const_reference __x);
  564. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __x);
  565. template <class... _Args>
  566. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  567. #if _LIBCPP_STD_VER >= 17
  568. reference emplace_back(_Args&&... __args);
  569. #else
  570. void emplace_back(_Args&&... __args);
  571. #endif
  572. #if _LIBCPP_STD_VER >= 23
  573. template <_ContainerCompatibleRange<_Tp> _Range>
  574. _LIBCPP_HIDE_FROM_ABI
  575. constexpr void append_range(_Range&& __range) {
  576. insert_range(end(), std::forward<_Range>(__range));
  577. }
  578. #endif
  579. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  580. void pop_back();
  581. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __position, const_reference __x);
  582. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __position, value_type&& __x);
  583. template <class... _Args>
  584. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator emplace(const_iterator __position, _Args&&... __args);
  585. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  586. iterator insert(const_iterator __position, size_type __n, const_reference __x);
  587. template <class _InputIterator,
  588. __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value &&
  589. is_constructible< value_type, typename iterator_traits<_InputIterator>::reference>::value,
  590. int> = 0>
  591. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
  592. insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
  593. #if _LIBCPP_STD_VER >= 23
  594. template <_ContainerCompatibleRange<_Tp> _Range>
  595. _LIBCPP_HIDE_FROM_ABI
  596. constexpr iterator insert_range(const_iterator __position, _Range&& __range) {
  597. if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
  598. auto __n = static_cast<size_type>(ranges::distance(__range));
  599. return __insert_with_size(__position, ranges::begin(__range), ranges::end(__range), __n);
  600. } else {
  601. return __insert_with_sentinel(__position, ranges::begin(__range), ranges::end(__range));
  602. }
  603. }
  604. #endif
  605. template <
  606. class _ForwardIterator,
  607. __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value &&
  608. is_constructible< value_type, typename iterator_traits<_ForwardIterator>::reference>::value,
  609. int> = 0>
  610. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
  611. insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
  612. #ifndef _LIBCPP_CXX03_LANG
  613. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  614. iterator insert(const_iterator __position, initializer_list<value_type> __il)
  615. {return insert(__position, __il.begin(), __il.end());}
  616. #endif
  617. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __position);
  618. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __first, const_iterator __last);
  619. _LIBCPP_REINITIALIZES_OBJECT _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  620. void clear() _NOEXCEPT
  621. {
  622. size_type __old_size = size();
  623. __clear();
  624. __annotate_shrink(__old_size);
  625. }
  626. #if _YNDX_LIBCXX_ENABLE_VECTOR_POD_RESIZE_UNINITIALIZED
  627. void resize_uninitialized(size_type __sz);
  628. #endif
  629. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void resize(size_type __sz);
  630. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void resize(size_type __sz, const_reference __x);
  631. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void swap(vector&)
  632. #if _LIBCPP_STD_VER >= 14
  633. _NOEXCEPT;
  634. #else
  635. _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
  636. __is_nothrow_swappable<allocator_type>::value);
  637. #endif
  638. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool __invariants() const;
  639. private:
  640. pointer __begin_ = nullptr;
  641. pointer __end_ = nullptr;
  642. __compressed_pair<pointer, allocator_type> __end_cap_ =
  643. __compressed_pair<pointer, allocator_type>(nullptr, __default_init_tag());
  644. // Allocate space for __n objects
  645. // throws length_error if __n > max_size()
  646. // throws (probably bad_alloc) if memory run out
  647. // Precondition: __begin_ == __end_ == __end_cap() == 0
  648. // Precondition: __n > 0
  649. // Postcondition: capacity() >= __n
  650. // Postcondition: size() == 0
  651. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __vallocate(size_type __n) {
  652. if (__n > max_size())
  653. __throw_length_error();
  654. auto __allocation = std::__allocate_at_least(__alloc(), __n);
  655. __begin_ = __allocation.ptr;
  656. __end_ = __allocation.ptr;
  657. __end_cap() = __begin_ + __allocation.count;
  658. __annotate_new(0);
  659. }
  660. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __vdeallocate() _NOEXCEPT;
  661. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __recommend(size_type __new_size) const;
  662. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n);
  663. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  664. void __construct_at_end(size_type __n, const_reference __x);
  665. template <class _InputIterator, class _Sentinel>
  666. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  667. void __init_with_size(_InputIterator __first, _Sentinel __last, size_type __n) {
  668. auto __guard = std::__make_exception_guard(__destroy_vector(*this));
  669. if (__n > 0) {
  670. __vallocate(__n);
  671. __construct_at_end(__first, __last, __n);
  672. }
  673. __guard.__complete();
  674. }
  675. template <class _InputIterator, class _Sentinel>
  676. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  677. void __init_with_sentinel(_InputIterator __first, _Sentinel __last) {
  678. auto __guard = std::__make_exception_guard(__destroy_vector(*this));
  679. for (; __first != __last; ++__first)
  680. emplace_back(*__first);
  681. __guard.__complete();
  682. }
  683. template <class _Iterator, class _Sentinel>
  684. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  685. void __assign_with_sentinel(_Iterator __first, _Sentinel __last);
  686. template <class _ForwardIterator, class _Sentinel>
  687. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  688. void __assign_with_size(_ForwardIterator __first, _Sentinel __last, difference_type __n);
  689. template <class _InputIterator, class _Sentinel>
  690. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  691. iterator __insert_with_sentinel(const_iterator __position, _InputIterator __first, _Sentinel __last);
  692. template <class _Iterator, class _Sentinel>
  693. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  694. iterator __insert_with_size(const_iterator __position, _Iterator __first, _Sentinel __last, difference_type __n);
  695. template <class _InputIterator, class _Sentinel>
  696. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  697. void __construct_at_end(_InputIterator __first, _Sentinel __last, size_type __n);
  698. void __append_uninitialized(size_type __n);
  699. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __append(size_type __n);
  700. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __append(size_type __n, const_reference __x);
  701. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  702. iterator __make_iter(pointer __p) _NOEXCEPT;
  703. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  704. const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
  705. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
  706. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
  707. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_range(pointer __from_s, pointer __from_e, pointer __to);
  708. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign(vector& __c, true_type)
  709. _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
  710. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign(vector& __c, false_type)
  711. _NOEXCEPT_(__alloc_traits::is_always_equal::value);
  712. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  713. void __destruct_at_end(pointer __new_last) _NOEXCEPT
  714. {
  715. size_type __old_size = size();
  716. __base_destruct_at_end(__new_last);
  717. __annotate_shrink(__old_size);
  718. }
  719. template <class _Up>
  720. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  721. inline pointer __push_back_slow_path(_Up&& __x);
  722. template <class... _Args>
  723. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  724. inline pointer __emplace_back_slow_path(_Args&&... __args);
  725. // The following functions are no-ops outside of AddressSanitizer mode.
  726. // We call annotations for every allocator, unless explicitly disabled.
  727. //
  728. // To disable annotations for a particular allocator, change value of
  729. // __asan_annotate_container_with_allocator to false.
  730. // For more details, see the "Using libc++" documentation page or
  731. // the documentation for __sanitizer_annotate_contiguous_container.
  732. #if !defined(_LIBCPP_HAS_NO_ASAN) && !defined(_LIBCPP_NO_ASAN_CONTIGUOUS_CONTAINER_FEATURES)
  733. _LIBCPP_CONSTEXPR_SINCE_CXX20
  734. void __annotate_contiguous_container(const void *__beg, const void *__end,
  735. const void *__old_mid,
  736. const void *__new_mid) const
  737. {
  738. if (!__libcpp_is_constant_evaluated() && __beg != nullptr && __asan_annotate_container_with_allocator<_Allocator>::value)
  739. __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
  740. }
  741. #else
  742. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  743. void __annotate_contiguous_container(const void*, const void*, const void*,
  744. const void*) const _NOEXCEPT {}
  745. #endif
  746. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  747. void __annotate_new(size_type __current_size) const _NOEXCEPT {
  748. __annotate_contiguous_container(data(), data() + capacity(),
  749. data() + capacity(), data() + __current_size);
  750. }
  751. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  752. void __annotate_delete() const _NOEXCEPT {
  753. __annotate_contiguous_container(data(), data() + capacity(),
  754. data() + size(), data() + capacity());
  755. }
  756. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  757. void __annotate_increase(size_type __n) const _NOEXCEPT
  758. {
  759. __annotate_contiguous_container(data(), data() + capacity(),
  760. data() + size(), data() + size() + __n);
  761. }
  762. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  763. void __annotate_shrink(size_type __old_size) const _NOEXCEPT
  764. {
  765. __annotate_contiguous_container(data(), data() + capacity(),
  766. data() + __old_size, data() + size());
  767. }
  768. struct _ConstructTransaction {
  769. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  770. explicit _ConstructTransaction(vector &__v, size_type __n)
  771. : __v_(__v), __pos_(__v.__end_), __new_end_(__v.__end_ + __n) {
  772. #ifndef _LIBCPP_HAS_NO_ASAN
  773. __v_.__annotate_increase(__n);
  774. #endif
  775. }
  776. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~_ConstructTransaction() {
  777. __v_.__end_ = __pos_;
  778. #ifndef _LIBCPP_HAS_NO_ASAN
  779. if (__pos_ != __new_end_) {
  780. __v_.__annotate_shrink(__new_end_ - __v_.__begin_);
  781. }
  782. #endif
  783. }
  784. vector &__v_;
  785. pointer __pos_;
  786. const_pointer const __new_end_;
  787. private:
  788. _ConstructTransaction(_ConstructTransaction const&) = delete;
  789. _ConstructTransaction& operator=(_ConstructTransaction const&) = delete;
  790. };
  791. template <class ..._Args>
  792. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  793. void __construct_one_at_end(_Args&& ...__args) {
  794. _ConstructTransaction __tx(*this, 1);
  795. __alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_),
  796. std::forward<_Args>(__args)...);
  797. ++__tx.__pos_;
  798. }
  799. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  800. allocator_type& __alloc() _NOEXCEPT
  801. {return this->__end_cap_.second();}
  802. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  803. const allocator_type& __alloc() const _NOEXCEPT
  804. {return this->__end_cap_.second();}
  805. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  806. pointer& __end_cap() _NOEXCEPT
  807. {return this->__end_cap_.first();}
  808. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  809. const pointer& __end_cap() const _NOEXCEPT
  810. {return this->__end_cap_.first();}
  811. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  812. void __clear() _NOEXCEPT {__base_destruct_at_end(this->__begin_);}
  813. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  814. void __base_destruct_at_end(pointer __new_last) _NOEXCEPT {
  815. pointer __soon_to_be_end = this->__end_;
  816. while (__new_last != __soon_to_be_end)
  817. __alloc_traits::destroy(__alloc(), std::__to_address(--__soon_to_be_end));
  818. this->__end_ = __new_last;
  819. }
  820. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  821. void __copy_assign_alloc(const vector& __c)
  822. {__copy_assign_alloc(__c, integral_constant<bool,
  823. __alloc_traits::propagate_on_container_copy_assignment::value>());}
  824. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  825. void __move_assign_alloc(vector& __c)
  826. _NOEXCEPT_(
  827. !__alloc_traits::propagate_on_container_move_assignment::value ||
  828. is_nothrow_move_assignable<allocator_type>::value)
  829. {__move_assign_alloc(__c, integral_constant<bool,
  830. __alloc_traits::propagate_on_container_move_assignment::value>());}
  831. _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
  832. void __throw_length_error() const {
  833. std::__throw_length_error("vector");
  834. }
  835. _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
  836. void __throw_out_of_range() const {
  837. std::__throw_out_of_range("vector");
  838. }
  839. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  840. void __copy_assign_alloc(const vector& __c, true_type)
  841. {
  842. if (__alloc() != __c.__alloc())
  843. {
  844. __clear();
  845. __annotate_delete();
  846. __alloc_traits::deallocate(__alloc(), this->__begin_, capacity());
  847. this->__begin_ = this->__end_ = __end_cap() = nullptr;
  848. }
  849. __alloc() = __c.__alloc();
  850. }
  851. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  852. void __copy_assign_alloc(const vector&, false_type)
  853. {}
  854. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  855. void __move_assign_alloc(vector& __c, true_type)
  856. _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
  857. {
  858. __alloc() = std::move(__c.__alloc());
  859. }
  860. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  861. void __move_assign_alloc(vector&, false_type)
  862. _NOEXCEPT
  863. {}
  864. };
  865. #if _LIBCPP_STD_VER >= 17
  866. template<class _InputIterator,
  867. class _Alloc = allocator<__iter_value_type<_InputIterator>>,
  868. class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
  869. class = enable_if_t<__is_allocator<_Alloc>::value>
  870. >
  871. vector(_InputIterator, _InputIterator)
  872. -> vector<__iter_value_type<_InputIterator>, _Alloc>;
  873. template<class _InputIterator,
  874. class _Alloc,
  875. class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
  876. class = enable_if_t<__is_allocator<_Alloc>::value>
  877. >
  878. vector(_InputIterator, _InputIterator, _Alloc)
  879. -> vector<__iter_value_type<_InputIterator>, _Alloc>;
  880. #endif
  881. #if _LIBCPP_STD_VER >= 23
  882. template <ranges::input_range _Range,
  883. class _Alloc = allocator<ranges::range_value_t<_Range>>,
  884. class = enable_if_t<__is_allocator<_Alloc>::value>
  885. >
  886. vector(from_range_t, _Range&&, _Alloc = _Alloc())
  887. -> vector<ranges::range_value_t<_Range>, _Alloc>;
  888. #endif
  889. template <class _Tp, class _Allocator>
  890. _LIBCPP_CONSTEXPR_SINCE_CXX20
  891. void
  892. vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
  893. {
  894. __annotate_delete();
  895. using _RevIter = std::reverse_iterator<pointer>;
  896. __v.__begin_ = std::__uninitialized_allocator_move_if_noexcept(
  897. __alloc(), _RevIter(__end_), _RevIter(__begin_), _RevIter(__v.__begin_))
  898. .base();
  899. std::swap(this->__begin_, __v.__begin_);
  900. std::swap(this->__end_, __v.__end_);
  901. std::swap(this->__end_cap(), __v.__end_cap());
  902. __v.__first_ = __v.__begin_;
  903. __annotate_new(size());
  904. }
  905. template <class _Tp, class _Allocator>
  906. _LIBCPP_CONSTEXPR_SINCE_CXX20
  907. typename vector<_Tp, _Allocator>::pointer
  908. vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
  909. {
  910. __annotate_delete();
  911. pointer __r = __v.__begin_;
  912. using _RevIter = std::reverse_iterator<pointer>;
  913. __v.__begin_ = std::__uninitialized_allocator_move_if_noexcept(
  914. __alloc(), _RevIter(__p), _RevIter(__begin_), _RevIter(__v.__begin_))
  915. .base();
  916. __v.__end_ = std::__uninitialized_allocator_move_if_noexcept(__alloc(), __p, __end_, __v.__end_);
  917. std::swap(this->__begin_, __v.__begin_);
  918. std::swap(this->__end_, __v.__end_);
  919. std::swap(this->__end_cap(), __v.__end_cap());
  920. __v.__first_ = __v.__begin_;
  921. __annotate_new(size());
  922. return __r;
  923. }
  924. template <class _Tp, class _Allocator>
  925. _LIBCPP_CONSTEXPR_SINCE_CXX20
  926. void
  927. vector<_Tp, _Allocator>::__vdeallocate() _NOEXCEPT
  928. {
  929. if (this->__begin_ != nullptr)
  930. {
  931. clear();
  932. __annotate_delete();
  933. __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
  934. this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
  935. }
  936. }
  937. template <class _Tp, class _Allocator>
  938. _LIBCPP_CONSTEXPR_SINCE_CXX20
  939. typename vector<_Tp, _Allocator>::size_type
  940. vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
  941. {
  942. return std::min<size_type>(__alloc_traits::max_size(this->__alloc()),
  943. numeric_limits<difference_type>::max());
  944. }
  945. // Precondition: __new_size > capacity()
  946. template <class _Tp, class _Allocator>
  947. _LIBCPP_CONSTEXPR_SINCE_CXX20
  948. inline _LIBCPP_HIDE_FROM_ABI
  949. typename vector<_Tp, _Allocator>::size_type
  950. vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
  951. {
  952. const size_type __ms = max_size();
  953. if (__new_size > __ms)
  954. this->__throw_length_error();
  955. const size_type __cap = capacity();
  956. if (__cap >= __ms / 2)
  957. return __ms;
  958. return std::max<size_type>(2 * __cap, __new_size);
  959. }
  960. // Default constructs __n objects starting at __end_
  961. // throws if construction throws
  962. // Precondition: __n > 0
  963. // Precondition: size() + __n <= capacity()
  964. // Postcondition: size() == size() + __n
  965. template <class _Tp, class _Allocator>
  966. _LIBCPP_CONSTEXPR_SINCE_CXX20
  967. void
  968. vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
  969. {
  970. _ConstructTransaction __tx(*this, __n);
  971. const_pointer __new_end = __tx.__new_end_;
  972. for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
  973. __alloc_traits::construct(this->__alloc(), std::__to_address(__pos));
  974. }
  975. }
  976. // Copy constructs __n objects starting at __end_ from __x
  977. // throws if construction throws
  978. // Precondition: __n > 0
  979. // Precondition: size() + __n <= capacity()
  980. // Postcondition: size() == old size() + __n
  981. // Postcondition: [i] == __x for all i in [size() - __n, __n)
  982. template <class _Tp, class _Allocator>
  983. _LIBCPP_CONSTEXPR_SINCE_CXX20
  984. inline
  985. void
  986. vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
  987. {
  988. _ConstructTransaction __tx(*this, __n);
  989. const_pointer __new_end = __tx.__new_end_;
  990. for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
  991. __alloc_traits::construct(this->__alloc(), std::__to_address(__pos), __x);
  992. }
  993. }
  994. template <class _Tp, class _Allocator>
  995. template <class _InputIterator, class _Sentinel>
  996. _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  997. vector<_Tp, _Allocator>::__construct_at_end(_InputIterator __first, _Sentinel __last, size_type __n) {
  998. _ConstructTransaction __tx(*this, __n);
  999. __tx.__pos_ = std::__uninitialized_allocator_copy(__alloc(), __first, __last, __tx.__pos_);
  1000. }
  1001. template <class _Tp, class _Allocator>
  1002. void
  1003. vector<_Tp, _Allocator>::__append_uninitialized(size_type __n)
  1004. {
  1005. if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n) {
  1006. __annotate_increase(__n);
  1007. this->__end_ += __n;
  1008. }
  1009. else
  1010. {
  1011. allocator_type& __a = this->__alloc();
  1012. __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
  1013. __v.__uninitialized_at_end(__n);
  1014. __swap_out_circular_buffer(__v);
  1015. }
  1016. }
  1017. // Default constructs __n objects starting at __end_
  1018. // throws if construction throws
  1019. // Postcondition: size() == size() + __n
  1020. // Exception safety: strong.
  1021. template <class _Tp, class _Allocator>
  1022. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1023. void
  1024. vector<_Tp, _Allocator>::__append(size_type __n)
  1025. {
  1026. if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
  1027. this->__construct_at_end(__n);
  1028. else
  1029. {
  1030. allocator_type& __a = this->__alloc();
  1031. __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
  1032. __v.__construct_at_end(__n);
  1033. __swap_out_circular_buffer(__v);
  1034. }
  1035. }
  1036. // Default constructs __n objects starting at __end_
  1037. // throws if construction throws
  1038. // Postcondition: size() == size() + __n
  1039. // Exception safety: strong.
  1040. template <class _Tp, class _Allocator>
  1041. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1042. void
  1043. vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
  1044. {
  1045. if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
  1046. this->__construct_at_end(__n, __x);
  1047. else
  1048. {
  1049. allocator_type& __a = this->__alloc();
  1050. __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
  1051. __v.__construct_at_end(__n, __x);
  1052. __swap_out_circular_buffer(__v);
  1053. }
  1054. }
  1055. template <class _Tp, class _Allocator>
  1056. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1057. vector<_Tp, _Allocator>::vector(size_type __n)
  1058. {
  1059. auto __guard = std::__make_exception_guard(__destroy_vector(*this));
  1060. if (__n > 0)
  1061. {
  1062. __vallocate(__n);
  1063. __construct_at_end(__n);
  1064. }
  1065. __guard.__complete();
  1066. }
  1067. #if _LIBCPP_STD_VER >= 14
  1068. template <class _Tp, class _Allocator>
  1069. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1070. vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
  1071. : __end_cap_(nullptr, __a)
  1072. {
  1073. auto __guard = std::__make_exception_guard(__destroy_vector(*this));
  1074. if (__n > 0)
  1075. {
  1076. __vallocate(__n);
  1077. __construct_at_end(__n);
  1078. }
  1079. __guard.__complete();
  1080. }
  1081. #endif
  1082. template <class _Tp, class _Allocator>
  1083. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1084. vector<_Tp, _Allocator>::vector(size_type __n, const value_type& __x)
  1085. {
  1086. auto __guard = std::__make_exception_guard(__destroy_vector(*this));
  1087. if (__n > 0)
  1088. {
  1089. __vallocate(__n);
  1090. __construct_at_end(__n, __x);
  1091. }
  1092. __guard.__complete();
  1093. }
  1094. template <class _Tp, class _Allocator>
  1095. template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value &&
  1096. is_constructible<_Tp, typename iterator_traits<_InputIterator>::reference>::value,
  1097. int> >
  1098. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1099. vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last)
  1100. {
  1101. __init_with_sentinel(__first, __last);
  1102. }
  1103. template <class _Tp, class _Allocator>
  1104. template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value &&
  1105. is_constructible<_Tp, typename iterator_traits<_InputIterator>::reference>::value,
  1106. int> >
  1107. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1108. vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a)
  1109. : __end_cap_(nullptr, __a)
  1110. {
  1111. __init_with_sentinel(__first, __last);
  1112. }
  1113. template <class _Tp, class _Allocator>
  1114. template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value &&
  1115. is_constructible<_Tp, typename iterator_traits<_ForwardIterator>::reference>::value,
  1116. int> >
  1117. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1118. vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last)
  1119. {
  1120. size_type __n = static_cast<size_type>(std::distance(__first, __last));
  1121. __init_with_size(__first, __last, __n);
  1122. }
  1123. template <class _Tp, class _Allocator>
  1124. template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value &&
  1125. is_constructible<_Tp, typename iterator_traits<_ForwardIterator>::reference>::value,
  1126. int> >
  1127. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1128. vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a)
  1129. : __end_cap_(nullptr, __a)
  1130. {
  1131. size_type __n = static_cast<size_type>(std::distance(__first, __last));
  1132. __init_with_size(__first, __last, __n);
  1133. }
  1134. template <class _Tp, class _Allocator>
  1135. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1136. vector<_Tp, _Allocator>::vector(const vector& __x)
  1137. : __end_cap_(nullptr, __alloc_traits::select_on_container_copy_construction(__x.__alloc()))
  1138. {
  1139. __init_with_size(__x.__begin_, __x.__end_, __x.size());
  1140. }
  1141. template <class _Tp, class _Allocator>
  1142. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1143. vector<_Tp, _Allocator>::vector(const vector& __x, const __type_identity_t<allocator_type>& __a)
  1144. : __end_cap_(nullptr, __a)
  1145. {
  1146. __init_with_size(__x.__begin_, __x.__end_, __x.size());
  1147. }
  1148. template <class _Tp, class _Allocator>
  1149. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1150. inline _LIBCPP_HIDE_FROM_ABI
  1151. vector<_Tp, _Allocator>::vector(vector&& __x)
  1152. #if _LIBCPP_STD_VER >= 17
  1153. noexcept
  1154. #else
  1155. _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
  1156. #endif
  1157. : __end_cap_(nullptr, std::move(__x.__alloc()))
  1158. {
  1159. this->__begin_ = __x.__begin_;
  1160. this->__end_ = __x.__end_;
  1161. this->__end_cap() = __x.__end_cap();
  1162. __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
  1163. }
  1164. template <class _Tp, class _Allocator>
  1165. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1166. inline _LIBCPP_HIDE_FROM_ABI
  1167. vector<_Tp, _Allocator>::vector(vector&& __x, const __type_identity_t<allocator_type>& __a)
  1168. : __end_cap_(nullptr, __a)
  1169. {
  1170. if (__a == __x.__alloc())
  1171. {
  1172. this->__begin_ = __x.__begin_;
  1173. this->__end_ = __x.__end_;
  1174. this->__end_cap() = __x.__end_cap();
  1175. __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
  1176. }
  1177. else
  1178. {
  1179. typedef move_iterator<iterator> _Ip;
  1180. auto __guard = std::__make_exception_guard(__destroy_vector(*this));
  1181. assign(_Ip(__x.begin()), _Ip(__x.end()));
  1182. __guard.__complete();
  1183. }
  1184. }
  1185. #ifndef _LIBCPP_CXX03_LANG
  1186. template <class _Tp, class _Allocator>
  1187. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1188. inline _LIBCPP_HIDE_FROM_ABI
  1189. vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
  1190. {
  1191. auto __guard = std::__make_exception_guard(__destroy_vector(*this));
  1192. if (__il.size() > 0)
  1193. {
  1194. __vallocate(__il.size());
  1195. __construct_at_end(__il.begin(), __il.end(), __il.size());
  1196. }
  1197. __guard.__complete();
  1198. }
  1199. template <class _Tp, class _Allocator>
  1200. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1201. inline _LIBCPP_HIDE_FROM_ABI
  1202. vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
  1203. : __end_cap_(nullptr, __a)
  1204. {
  1205. auto __guard = std::__make_exception_guard(__destroy_vector(*this));
  1206. if (__il.size() > 0)
  1207. {
  1208. __vallocate(__il.size());
  1209. __construct_at_end(__il.begin(), __il.end(), __il.size());
  1210. }
  1211. __guard.__complete();
  1212. }
  1213. #endif // _LIBCPP_CXX03_LANG
  1214. template <class _Tp, class _Allocator>
  1215. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1216. inline _LIBCPP_HIDE_FROM_ABI
  1217. vector<_Tp, _Allocator>&
  1218. vector<_Tp, _Allocator>::operator=(vector&& __x)
  1219. _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
  1220. {
  1221. __move_assign(__x, integral_constant<bool,
  1222. __alloc_traits::propagate_on_container_move_assignment::value>());
  1223. return *this;
  1224. }
  1225. template <class _Tp, class _Allocator>
  1226. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1227. void
  1228. vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
  1229. _NOEXCEPT_(__alloc_traits::is_always_equal::value)
  1230. {
  1231. if (__alloc() != __c.__alloc())
  1232. {
  1233. typedef move_iterator<iterator> _Ip;
  1234. assign(_Ip(__c.begin()), _Ip(__c.end()));
  1235. }
  1236. else
  1237. __move_assign(__c, true_type());
  1238. }
  1239. template <class _Tp, class _Allocator>
  1240. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1241. void
  1242. vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
  1243. _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
  1244. {
  1245. __vdeallocate();
  1246. __move_assign_alloc(__c); // this can throw
  1247. this->__begin_ = __c.__begin_;
  1248. this->__end_ = __c.__end_;
  1249. this->__end_cap() = __c.__end_cap();
  1250. __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
  1251. }
  1252. template <class _Tp, class _Allocator>
  1253. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1254. inline _LIBCPP_HIDE_FROM_ABI
  1255. vector<_Tp, _Allocator>&
  1256. vector<_Tp, _Allocator>::operator=(const vector& __x)
  1257. {
  1258. if (this != std::addressof(__x))
  1259. {
  1260. __copy_assign_alloc(__x);
  1261. assign(__x.__begin_, __x.__end_);
  1262. }
  1263. return *this;
  1264. }
  1265. template <class _Tp, class _Allocator>
  1266. template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value &&
  1267. is_constructible<_Tp, typename iterator_traits<_InputIterator>::reference>::value,
  1268. int> >
  1269. _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  1270. vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
  1271. {
  1272. __assign_with_sentinel(__first, __last);
  1273. }
  1274. template <class _Tp, class _Allocator>
  1275. template <class _Iterator, class _Sentinel>
  1276. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  1277. void vector<_Tp, _Allocator>::__assign_with_sentinel(_Iterator __first, _Sentinel __last) {
  1278. clear();
  1279. for (; __first != __last; ++__first)
  1280. emplace_back(*__first);
  1281. }
  1282. template <class _Tp, class _Allocator>
  1283. template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value &&
  1284. is_constructible<_Tp, typename iterator_traits<_ForwardIterator>::reference>::value,
  1285. int> >
  1286. _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  1287. vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
  1288. {
  1289. __assign_with_size(__first, __last, std::distance(__first, __last));
  1290. }
  1291. template <class _Tp, class _Allocator>
  1292. template <class _ForwardIterator, class _Sentinel>
  1293. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  1294. void vector<_Tp, _Allocator>::__assign_with_size(_ForwardIterator __first, _Sentinel __last, difference_type __n) {
  1295. size_type __new_size = static_cast<size_type>(__n);
  1296. if (__new_size <= capacity())
  1297. {
  1298. if (__new_size > size())
  1299. {
  1300. _ForwardIterator __mid = std::next(__first, size());
  1301. std::copy(__first, __mid, this->__begin_);
  1302. __construct_at_end(__mid, __last, __new_size - size());
  1303. }
  1304. else
  1305. {
  1306. pointer __m = std::__copy<_ClassicAlgPolicy>(__first, __last, this->__begin_).second;
  1307. this->__destruct_at_end(__m);
  1308. }
  1309. }
  1310. else
  1311. {
  1312. __vdeallocate();
  1313. __vallocate(__recommend(__new_size));
  1314. __construct_at_end(__first, __last, __new_size);
  1315. }
  1316. }
  1317. // We need to save the make_iter function and replace all constructor calls with it
  1318. // Made to support pointer iterators
  1319. template <class _Tp, class _Allocator>
  1320. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1321. inline _LIBCPP_INLINE_VISIBILITY
  1322. typename vector<_Tp, _Allocator>::iterator
  1323. vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
  1324. {
  1325. #if _YNDX_LIBCPP_MAKE_STRING_ITERATOR_POINTERS == 1
  1326. return iterator(__p);
  1327. #else
  1328. return iterator(this, __p);
  1329. #endif
  1330. }
  1331. template <class _Tp, class _Allocator>
  1332. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1333. inline _LIBCPP_INLINE_VISIBILITY
  1334. typename vector<_Tp, _Allocator>::const_iterator
  1335. vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
  1336. {
  1337. #if _YNDX_LIBCPP_MAKE_STRING_ITERATOR_POINTERS == 1
  1338. return const_iterator(__p);
  1339. #else
  1340. return const_iterator(this, __p);
  1341. #endif
  1342. }
  1343. template <class _Tp, class _Allocator>
  1344. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1345. void
  1346. vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
  1347. {
  1348. if (__n <= capacity())
  1349. {
  1350. size_type __s = size();
  1351. std::fill_n(this->__begin_, std::min(__n, __s), __u);
  1352. if (__n > __s)
  1353. __construct_at_end(__n - __s, __u);
  1354. else
  1355. this->__destruct_at_end(this->__begin_ + __n);
  1356. }
  1357. else
  1358. {
  1359. __vdeallocate();
  1360. __vallocate(__recommend(static_cast<size_type>(__n)));
  1361. __construct_at_end(__n, __u);
  1362. }
  1363. }
  1364. template <class _Tp, class _Allocator>
  1365. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1366. inline _LIBCPP_HIDE_FROM_ABI
  1367. typename vector<_Tp, _Allocator>::iterator
  1368. vector<_Tp, _Allocator>::begin() _NOEXCEPT
  1369. {
  1370. return __make_iter(this->__begin_);
  1371. }
  1372. template <class _Tp, class _Allocator>
  1373. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1374. inline _LIBCPP_HIDE_FROM_ABI
  1375. typename vector<_Tp, _Allocator>::const_iterator
  1376. vector<_Tp, _Allocator>::begin() const _NOEXCEPT
  1377. {
  1378. return __make_iter(this->__begin_);
  1379. }
  1380. template <class _Tp, class _Allocator>
  1381. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1382. inline _LIBCPP_HIDE_FROM_ABI
  1383. typename vector<_Tp, _Allocator>::iterator
  1384. vector<_Tp, _Allocator>::end() _NOEXCEPT
  1385. {
  1386. return __make_iter(this->__end_);
  1387. }
  1388. template <class _Tp, class _Allocator>
  1389. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1390. inline _LIBCPP_HIDE_FROM_ABI
  1391. typename vector<_Tp, _Allocator>::const_iterator
  1392. vector<_Tp, _Allocator>::end() const _NOEXCEPT
  1393. {
  1394. return __make_iter(this->__end_);
  1395. }
  1396. template <class _Tp, class _Allocator>
  1397. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1398. inline _LIBCPP_HIDE_FROM_ABI
  1399. typename vector<_Tp, _Allocator>::reference
  1400. vector<_Tp, _Allocator>::operator[](size_type __n) _NOEXCEPT
  1401. {
  1402. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n < size(), "vector[] index out of bounds");
  1403. return this->__begin_[__n];
  1404. }
  1405. template <class _Tp, class _Allocator>
  1406. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1407. inline _LIBCPP_HIDE_FROM_ABI
  1408. typename vector<_Tp, _Allocator>::const_reference
  1409. vector<_Tp, _Allocator>::operator[](size_type __n) const _NOEXCEPT
  1410. {
  1411. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n < size(), "vector[] index out of bounds");
  1412. return this->__begin_[__n];
  1413. }
  1414. template <class _Tp, class _Allocator>
  1415. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1416. typename vector<_Tp, _Allocator>::reference
  1417. vector<_Tp, _Allocator>::at(size_type __n)
  1418. {
  1419. if (__n >= size())
  1420. this->__throw_out_of_range();
  1421. return this->__begin_[__n];
  1422. }
  1423. template <class _Tp, class _Allocator>
  1424. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1425. typename vector<_Tp, _Allocator>::const_reference
  1426. vector<_Tp, _Allocator>::at(size_type __n) const
  1427. {
  1428. if (__n >= size())
  1429. this->__throw_out_of_range();
  1430. return this->__begin_[__n];
  1431. }
  1432. template <class _Tp, class _Allocator>
  1433. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1434. void
  1435. vector<_Tp, _Allocator>::reserve(size_type __n)
  1436. {
  1437. if (__n > capacity())
  1438. {
  1439. if (__n > max_size())
  1440. this->__throw_length_error();
  1441. allocator_type& __a = this->__alloc();
  1442. __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
  1443. __swap_out_circular_buffer(__v);
  1444. }
  1445. }
  1446. template <class _Tp, class _Allocator>
  1447. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1448. void
  1449. vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
  1450. {
  1451. if (capacity() > size())
  1452. {
  1453. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1454. try
  1455. {
  1456. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1457. allocator_type& __a = this->__alloc();
  1458. __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
  1459. __swap_out_circular_buffer(__v);
  1460. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1461. }
  1462. catch (...)
  1463. {
  1464. }
  1465. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1466. }
  1467. }
  1468. template <class _Tp, class _Allocator>
  1469. template <class _Up>
  1470. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1471. typename vector<_Tp, _Allocator>::pointer
  1472. vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
  1473. {
  1474. allocator_type& __a = this->__alloc();
  1475. __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
  1476. // __v.push_back(std::forward<_Up>(__x));
  1477. __alloc_traits::construct(__a, std::__to_address(__v.__end_), std::forward<_Up>(__x));
  1478. __v.__end_++;
  1479. __swap_out_circular_buffer(__v);
  1480. return this->__end_;
  1481. }
  1482. template <class _Tp, class _Allocator>
  1483. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1484. inline _LIBCPP_HIDE_FROM_ABI
  1485. void
  1486. vector<_Tp, _Allocator>::push_back(const_reference __x)
  1487. {
  1488. pointer __end = this->__end_;
  1489. if (__end < this->__end_cap()) {
  1490. __construct_one_at_end(__x);
  1491. ++__end;
  1492. } else {
  1493. __end = __push_back_slow_path(__x);
  1494. }
  1495. this->__end_ = __end;
  1496. }
  1497. template <class _Tp, class _Allocator>
  1498. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1499. inline _LIBCPP_HIDE_FROM_ABI
  1500. void
  1501. vector<_Tp, _Allocator>::push_back(value_type&& __x)
  1502. {
  1503. pointer __end = this->__end_;
  1504. if (__end < this->__end_cap()) {
  1505. __construct_one_at_end(std::move(__x));
  1506. ++__end;
  1507. } else {
  1508. __end = __push_back_slow_path(std::move(__x));
  1509. }
  1510. this->__end_ = __end;
  1511. }
  1512. template <class _Tp, class _Allocator>
  1513. template <class... _Args>
  1514. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1515. typename vector<_Tp, _Allocator>::pointer
  1516. vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
  1517. {
  1518. allocator_type& __a = this->__alloc();
  1519. __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
  1520. // __v.emplace_back(std::forward<_Args>(__args)...);
  1521. __alloc_traits::construct(__a, std::__to_address(__v.__end_), std::forward<_Args>(__args)...);
  1522. __v.__end_++;
  1523. __swap_out_circular_buffer(__v);
  1524. return this->__end_;
  1525. }
  1526. template <class _Tp, class _Allocator>
  1527. template <class... _Args>
  1528. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1529. inline
  1530. #if _LIBCPP_STD_VER >= 17
  1531. typename vector<_Tp, _Allocator>::reference
  1532. #else
  1533. void
  1534. #endif
  1535. vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
  1536. {
  1537. pointer __end = this->__end_;
  1538. if (__end < this->__end_cap()) {
  1539. __construct_one_at_end(std::forward<_Args>(__args)...);
  1540. ++__end;
  1541. } else {
  1542. __end = __emplace_back_slow_path(std::forward<_Args>(__args)...);
  1543. }
  1544. this->__end_ = __end;
  1545. #if _LIBCPP_STD_VER >= 17
  1546. return *(__end - 1);
  1547. #endif
  1548. }
  1549. template <class _Tp, class _Allocator>
  1550. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1551. inline
  1552. void
  1553. vector<_Tp, _Allocator>::pop_back()
  1554. {
  1555. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "vector::pop_back called on an empty vector");
  1556. this->__destruct_at_end(this->__end_ - 1);
  1557. }
  1558. template <class _Tp, class _Allocator>
  1559. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1560. inline _LIBCPP_HIDE_FROM_ABI
  1561. typename vector<_Tp, _Allocator>::iterator
  1562. vector<_Tp, _Allocator>::erase(const_iterator __position)
  1563. {
  1564. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__position != end(),
  1565. "vector::erase(iterator) called with a non-dereferenceable iterator");
  1566. difference_type __ps = __position - cbegin();
  1567. pointer __p = this->__begin_ + __ps;
  1568. this->__destruct_at_end(std::move(__p + 1, this->__end_, __p));
  1569. return __make_iter(__p);
  1570. }
  1571. template <class _Tp, class _Allocator>
  1572. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1573. typename vector<_Tp, _Allocator>::iterator
  1574. vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
  1575. {
  1576. _LIBCPP_ASSERT_VALID_INPUT_RANGE(__first <= __last, "vector::erase(first, last) called with invalid range");
  1577. pointer __p = this->__begin_ + (__first - begin());
  1578. if (__first != __last) {
  1579. this->__destruct_at_end(std::move(__p + (__last - __first), this->__end_, __p));
  1580. }
  1581. return __make_iter(__p);
  1582. }
  1583. template <class _Tp, class _Allocator>
  1584. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1585. void
  1586. vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
  1587. {
  1588. pointer __old_last = this->__end_;
  1589. difference_type __n = __old_last - __to;
  1590. {
  1591. pointer __i = __from_s + __n;
  1592. _ConstructTransaction __tx(*this, __from_e - __i);
  1593. for (pointer __pos = __tx.__pos_; __i < __from_e;
  1594. ++__i, (void) ++__pos, __tx.__pos_ = __pos) {
  1595. __alloc_traits::construct(this->__alloc(),
  1596. std::__to_address(__pos),
  1597. std::move(*__i));
  1598. }
  1599. }
  1600. std::move_backward(__from_s, __from_s + __n, __old_last);
  1601. }
  1602. template <class _Tp, class _Allocator>
  1603. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1604. typename vector<_Tp, _Allocator>::iterator
  1605. vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
  1606. {
  1607. pointer __p = this->__begin_ + (__position - begin());
  1608. // We can't compare unrelated pointers inside constant expressions
  1609. if (!__libcpp_is_constant_evaluated() && this->__end_ < this->__end_cap())
  1610. {
  1611. if (__p == this->__end_)
  1612. {
  1613. __construct_one_at_end(__x);
  1614. }
  1615. else
  1616. {
  1617. __move_range(__p, this->__end_, __p + 1);
  1618. const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
  1619. if (__p <= __xr && __xr < this->__end_)
  1620. ++__xr;
  1621. *__p = *__xr;
  1622. }
  1623. }
  1624. else
  1625. {
  1626. allocator_type& __a = this->__alloc();
  1627. __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
  1628. __v.push_back(__x);
  1629. __p = __swap_out_circular_buffer(__v, __p);
  1630. }
  1631. return __make_iter(__p);
  1632. }
  1633. template <class _Tp, class _Allocator>
  1634. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1635. typename vector<_Tp, _Allocator>::iterator
  1636. vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
  1637. {
  1638. pointer __p = this->__begin_ + (__position - begin());
  1639. if (this->__end_ < this->__end_cap())
  1640. {
  1641. if (__p == this->__end_)
  1642. {
  1643. __construct_one_at_end(std::move(__x));
  1644. }
  1645. else
  1646. {
  1647. __move_range(__p, this->__end_, __p + 1);
  1648. *__p = std::move(__x);
  1649. }
  1650. }
  1651. else
  1652. {
  1653. allocator_type& __a = this->__alloc();
  1654. __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
  1655. __v.push_back(std::move(__x));
  1656. __p = __swap_out_circular_buffer(__v, __p);
  1657. }
  1658. return __make_iter(__p);
  1659. }
  1660. template <class _Tp, class _Allocator>
  1661. template <class... _Args>
  1662. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1663. typename vector<_Tp, _Allocator>::iterator
  1664. vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
  1665. {
  1666. pointer __p = this->__begin_ + (__position - begin());
  1667. if (this->__end_ < this->__end_cap())
  1668. {
  1669. if (__p == this->__end_)
  1670. {
  1671. __construct_one_at_end(std::forward<_Args>(__args)...);
  1672. }
  1673. else
  1674. {
  1675. __temp_value<value_type, _Allocator> __tmp(this->__alloc(), std::forward<_Args>(__args)...);
  1676. __move_range(__p, this->__end_, __p + 1);
  1677. *__p = std::move(__tmp.get());
  1678. }
  1679. }
  1680. else
  1681. {
  1682. allocator_type& __a = this->__alloc();
  1683. __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
  1684. __v.emplace_back(std::forward<_Args>(__args)...);
  1685. __p = __swap_out_circular_buffer(__v, __p);
  1686. }
  1687. return __make_iter(__p);
  1688. }
  1689. template <class _Tp, class _Allocator>
  1690. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1691. typename vector<_Tp, _Allocator>::iterator
  1692. vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
  1693. {
  1694. pointer __p = this->__begin_ + (__position - begin());
  1695. if (__n > 0)
  1696. {
  1697. // We can't compare unrelated pointers inside constant expressions
  1698. if (!__libcpp_is_constant_evaluated() && __n <= static_cast<size_type>(this->__end_cap() - this->__end_))
  1699. {
  1700. size_type __old_n = __n;
  1701. pointer __old_last = this->__end_;
  1702. if (__n > static_cast<size_type>(this->__end_ - __p))
  1703. {
  1704. size_type __cx = __n - (this->__end_ - __p);
  1705. __construct_at_end(__cx, __x);
  1706. __n -= __cx;
  1707. }
  1708. if (__n > 0)
  1709. {
  1710. __move_range(__p, __old_last, __p + __old_n);
  1711. const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
  1712. if (__p <= __xr && __xr < this->__end_)
  1713. __xr += __old_n;
  1714. std::fill_n(__p, __n, *__xr);
  1715. }
  1716. }
  1717. else
  1718. {
  1719. allocator_type& __a = this->__alloc();
  1720. __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
  1721. __v.__construct_at_end(__n, __x);
  1722. __p = __swap_out_circular_buffer(__v, __p);
  1723. }
  1724. }
  1725. return __make_iter(__p);
  1726. }
  1727. template <class _Tp, class _Allocator>
  1728. template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value &&
  1729. is_constructible<_Tp, typename iterator_traits<_InputIterator>::reference>::value,
  1730. int> >
  1731. _LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::iterator
  1732. vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
  1733. {
  1734. return __insert_with_sentinel(__position, __first, __last);
  1735. }
  1736. template <class _Tp, class _Allocator>
  1737. template <class _InputIterator, class _Sentinel>
  1738. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  1739. typename vector<_Tp, _Allocator>::iterator
  1740. vector<_Tp, _Allocator>::__insert_with_sentinel(const_iterator __position, _InputIterator __first, _Sentinel __last) {
  1741. difference_type __off = __position - begin();
  1742. pointer __p = this->__begin_ + __off;
  1743. allocator_type& __a = this->__alloc();
  1744. pointer __old_last = this->__end_;
  1745. for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
  1746. {
  1747. __construct_one_at_end(*__first);
  1748. }
  1749. __split_buffer<value_type, allocator_type&> __v(__a);
  1750. if (__first != __last)
  1751. {
  1752. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1753. try
  1754. {
  1755. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1756. __v.__construct_at_end_with_sentinel(std::move(__first), std::move(__last));
  1757. difference_type __old_size = __old_last - this->__begin_;
  1758. difference_type __old_p = __p - this->__begin_;
  1759. reserve(__recommend(size() + __v.size()));
  1760. __p = this->__begin_ + __old_p;
  1761. __old_last = this->__begin_ + __old_size;
  1762. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1763. }
  1764. catch (...)
  1765. {
  1766. erase(__make_iter(__old_last), end());
  1767. throw;
  1768. }
  1769. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1770. }
  1771. __p = std::rotate(__p, __old_last, this->__end_);
  1772. insert(__make_iter(__p), std::make_move_iterator(__v.begin()),
  1773. std::make_move_iterator(__v.end()));
  1774. return begin() + __off;
  1775. }
  1776. template <class _Tp, class _Allocator>
  1777. template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value &&
  1778. is_constructible<_Tp, typename iterator_traits<_ForwardIterator>::reference>::value,
  1779. int> >
  1780. _LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::iterator
  1781. vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
  1782. {
  1783. return __insert_with_size(__position, __first, __last, std::distance(__first, __last));
  1784. }
  1785. template <class _Tp, class _Allocator>
  1786. template <class _Iterator, class _Sentinel>
  1787. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  1788. typename vector<_Tp, _Allocator>::iterator
  1789. vector<_Tp, _Allocator>::__insert_with_size(const_iterator __position, _Iterator __first, _Sentinel __last,
  1790. difference_type __n) {
  1791. auto __insertion_size = __n;
  1792. pointer __p = this->__begin_ + (__position - begin());
  1793. if (__n > 0)
  1794. {
  1795. if (__n <= this->__end_cap() - this->__end_)
  1796. {
  1797. size_type __old_n = __n;
  1798. pointer __old_last = this->__end_;
  1799. _Iterator __m = std::next(__first, __n);
  1800. difference_type __dx = this->__end_ - __p;
  1801. if (__n > __dx)
  1802. {
  1803. __m = __first;
  1804. difference_type __diff = this->__end_ - __p;
  1805. std::advance(__m, __diff);
  1806. __construct_at_end(__m, __last, __n - __diff);
  1807. __n = __dx;
  1808. }
  1809. if (__n > 0)
  1810. {
  1811. __move_range(__p, __old_last, __p + __old_n);
  1812. std::copy(__first, __m, __p);
  1813. }
  1814. }
  1815. else
  1816. {
  1817. allocator_type& __a = this->__alloc();
  1818. __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
  1819. __v.__construct_at_end_with_size(__first, __insertion_size);
  1820. __p = __swap_out_circular_buffer(__v, __p);
  1821. }
  1822. }
  1823. return __make_iter(__p);
  1824. }
  1825. template <class _Tp, class _Allocator>
  1826. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1827. void
  1828. vector<_Tp, _Allocator>::resize(size_type __sz)
  1829. {
  1830. size_type __cs = size();
  1831. if (__cs < __sz)
  1832. this->__append(__sz - __cs);
  1833. else if (__cs > __sz)
  1834. this->__destruct_at_end(this->__begin_ + __sz);
  1835. }
  1836. #if _YNDX_LIBCXX_ENABLE_VECTOR_POD_RESIZE_UNINITIALIZED
  1837. template <class _Tp, class _Allocator>
  1838. void
  1839. vector<_Tp, _Allocator>::resize_uninitialized(size_type __sz)
  1840. {
  1841. size_type __cs = size();
  1842. if (__cs < __sz)
  1843. this->__append_uninitialized(__sz - __cs);
  1844. else if (__cs > __sz) {
  1845. this->__end_ = this->__begin_ + __sz;
  1846. __annotate_shrink(__cs);
  1847. }
  1848. }
  1849. #endif
  1850. template <class _Tp, class _Allocator>
  1851. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1852. void
  1853. vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
  1854. {
  1855. size_type __cs = size();
  1856. if (__cs < __sz)
  1857. this->__append(__sz - __cs, __x);
  1858. else if (__cs > __sz)
  1859. this->__destruct_at_end(this->__begin_ + __sz);
  1860. }
  1861. template <class _Tp, class _Allocator>
  1862. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1863. void
  1864. vector<_Tp, _Allocator>::swap(vector& __x)
  1865. #if _LIBCPP_STD_VER >= 14
  1866. _NOEXCEPT
  1867. #else
  1868. _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
  1869. __is_nothrow_swappable<allocator_type>::value)
  1870. #endif
  1871. {
  1872. _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(__alloc_traits::propagate_on_container_swap::value ||
  1873. this->__alloc() == __x.__alloc(),
  1874. "vector::swap: Either propagate_on_container_swap must be true"
  1875. " or the allocators must compare equal");
  1876. std::swap(this->__begin_, __x.__begin_);
  1877. std::swap(this->__end_, __x.__end_);
  1878. std::swap(this->__end_cap(), __x.__end_cap());
  1879. std::__swap_allocator(this->__alloc(), __x.__alloc(),
  1880. integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>());
  1881. }
  1882. template <class _Tp, class _Allocator>
  1883. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1884. bool
  1885. vector<_Tp, _Allocator>::__invariants() const
  1886. {
  1887. if (this->__begin_ == nullptr)
  1888. {
  1889. if (this->__end_ != nullptr || this->__end_cap() != nullptr)
  1890. return false;
  1891. }
  1892. else
  1893. {
  1894. if (this->__begin_ > this->__end_)
  1895. return false;
  1896. if (this->__begin_ == this->__end_cap())
  1897. return false;
  1898. if (this->__end_ > this->__end_cap())
  1899. return false;
  1900. }
  1901. return true;
  1902. }
  1903. #if _YNDX_LIBCXX_ENABLE_VECTOR_BOOL_COMPRESSION == 1
  1904. // vector<bool>
  1905. template <class _Allocator> class vector<bool, _Allocator>;
  1906. template <class _Allocator> struct hash<vector<bool, _Allocator> >;
  1907. template <class _Allocator>
  1908. struct __has_storage_type<vector<bool, _Allocator> >
  1909. {
  1910. static const bool value = true;
  1911. };
  1912. template <class _Allocator>
  1913. class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator>
  1914. {
  1915. public:
  1916. typedef vector __self;
  1917. typedef bool value_type;
  1918. typedef _Allocator allocator_type;
  1919. typedef allocator_traits<allocator_type> __alloc_traits;
  1920. typedef typename __alloc_traits::size_type size_type;
  1921. typedef typename __alloc_traits::difference_type difference_type;
  1922. typedef size_type __storage_type;
  1923. typedef __bit_iterator<vector, false> pointer;
  1924. typedef __bit_iterator<vector, true> const_pointer;
  1925. typedef pointer iterator;
  1926. typedef const_pointer const_iterator;
  1927. typedef std::reverse_iterator<iterator> reverse_iterator;
  1928. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  1929. private:
  1930. typedef __rebind_alloc<__alloc_traits, __storage_type> __storage_allocator;
  1931. typedef allocator_traits<__storage_allocator> __storage_traits;
  1932. typedef typename __storage_traits::pointer __storage_pointer;
  1933. typedef typename __storage_traits::const_pointer __const_storage_pointer;
  1934. __storage_pointer __begin_;
  1935. size_type __size_;
  1936. __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
  1937. public:
  1938. typedef __bit_reference<vector> reference;
  1939. #ifdef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL
  1940. using const_reference = bool;
  1941. #else
  1942. typedef __bit_const_reference<vector> const_reference;
  1943. #endif
  1944. private:
  1945. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  1946. size_type& __cap() _NOEXCEPT
  1947. {return __cap_alloc_.first();}
  1948. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  1949. const size_type& __cap() const _NOEXCEPT
  1950. {return __cap_alloc_.first();}
  1951. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  1952. __storage_allocator& __alloc() _NOEXCEPT
  1953. {return __cap_alloc_.second();}
  1954. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  1955. const __storage_allocator& __alloc() const _NOEXCEPT
  1956. {return __cap_alloc_.second();}
  1957. static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
  1958. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  1959. static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
  1960. {return __n * __bits_per_word;}
  1961. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  1962. static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
  1963. {return (__n - 1) / __bits_per_word + 1;}
  1964. public:
  1965. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  1966. vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
  1967. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit vector(const allocator_type& __a)
  1968. #if _LIBCPP_STD_VER <= 14
  1969. _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
  1970. #else
  1971. _NOEXCEPT;
  1972. #endif
  1973. private:
  1974. class __destroy_vector {
  1975. public:
  1976. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI __destroy_vector(vector& __vec) : __vec_(__vec) {}
  1977. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void operator()() {
  1978. if (__vec_.__begin_ != nullptr)
  1979. __storage_traits::deallocate(__vec_.__alloc(), __vec_.__begin_, __vec_.__cap());
  1980. }
  1981. private:
  1982. vector& __vec_;
  1983. };
  1984. public:
  1985. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~vector() { __destroy_vector(*this)(); }
  1986. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit vector(size_type __n);
  1987. #if _LIBCPP_STD_VER >= 14
  1988. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit vector(size_type __n, const allocator_type& __a);
  1989. #endif
  1990. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector(size_type __n, const value_type& __v);
  1991. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector(size_type __n, const value_type& __v, const allocator_type& __a);
  1992. template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
  1993. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector(_InputIterator __first, _InputIterator __last);
  1994. template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
  1995. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
  1996. template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
  1997. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector(_ForwardIterator __first, _ForwardIterator __last);
  1998. template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
  1999. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a);
  2000. #if _LIBCPP_STD_VER >= 23
  2001. template <_ContainerCompatibleRange<bool> _Range>
  2002. _LIBCPP_HIDE_FROM_ABI constexpr
  2003. vector(from_range_t, _Range&& __range, const allocator_type& __a = allocator_type())
  2004. : __begin_(nullptr),
  2005. __size_(0),
  2006. __cap_alloc_(0, static_cast<__storage_allocator>(__a)) {
  2007. if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
  2008. auto __n = static_cast<size_type>(ranges::distance(__range));
  2009. __init_with_size(ranges::begin(__range), ranges::end(__range), __n);
  2010. } else {
  2011. __init_with_sentinel(ranges::begin(__range), ranges::end(__range));
  2012. }
  2013. }
  2014. #endif
  2015. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector(const vector& __v);
  2016. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector(const vector& __v, const allocator_type& __a);
  2017. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector& operator=(const vector& __v);
  2018. #ifndef _LIBCPP_CXX03_LANG
  2019. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector(initializer_list<value_type> __il);
  2020. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector(initializer_list<value_type> __il, const allocator_type& __a);
  2021. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2022. vector& operator=(initializer_list<value_type> __il)
  2023. {assign(__il.begin(), __il.end()); return *this;}
  2024. #endif // !_LIBCPP_CXX03_LANG
  2025. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2026. vector(vector&& __v)
  2027. #if _LIBCPP_STD_VER >= 17
  2028. noexcept;
  2029. #else
  2030. _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
  2031. #endif
  2032. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector(vector&& __v, const __type_identity_t<allocator_type>& __a);
  2033. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2034. vector& operator=(vector&& __v)
  2035. _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
  2036. template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
  2037. void
  2038. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 assign(_InputIterator __first, _InputIterator __last);
  2039. template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
  2040. void
  2041. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 assign(_ForwardIterator __first, _ForwardIterator __last);
  2042. #if _LIBCPP_STD_VER >= 23
  2043. template <_ContainerCompatibleRange<bool> _Range>
  2044. _LIBCPP_HIDE_FROM_ABI
  2045. constexpr void assign_range(_Range&& __range) {
  2046. if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
  2047. auto __n = static_cast<size_type>(ranges::distance(__range));
  2048. __assign_with_size(ranges::begin(__range), ranges::end(__range), __n);
  2049. } else {
  2050. __assign_with_sentinel(ranges::begin(__range), ranges::end(__range));
  2051. }
  2052. }
  2053. #endif
  2054. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void assign(size_type __n, const value_type& __x);
  2055. #ifndef _LIBCPP_CXX03_LANG
  2056. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2057. void assign(initializer_list<value_type> __il)
  2058. {assign(__il.begin(), __il.end());}
  2059. #endif
  2060. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator_type get_allocator() const _NOEXCEPT
  2061. {return allocator_type(this->__alloc());}
  2062. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type max_size() const _NOEXCEPT;
  2063. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2064. size_type capacity() const _NOEXCEPT
  2065. {return __internal_cap_to_external(__cap());}
  2066. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2067. size_type size() const _NOEXCEPT
  2068. {return __size_;}
  2069. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2070. bool empty() const _NOEXCEPT
  2071. {return __size_ == 0;}
  2072. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reserve(size_type __n);
  2073. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void shrink_to_fit() _NOEXCEPT;
  2074. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2075. iterator begin() _NOEXCEPT
  2076. {return __make_iter(0);}
  2077. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2078. const_iterator begin() const _NOEXCEPT
  2079. {return __make_iter(0);}
  2080. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2081. iterator end() _NOEXCEPT
  2082. {return __make_iter(__size_);}
  2083. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2084. const_iterator end() const _NOEXCEPT
  2085. {return __make_iter(__size_);}
  2086. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2087. reverse_iterator rbegin() _NOEXCEPT
  2088. {return reverse_iterator(end());}
  2089. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2090. const_reverse_iterator rbegin() const _NOEXCEPT
  2091. {return const_reverse_iterator(end());}
  2092. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2093. reverse_iterator rend() _NOEXCEPT
  2094. {return reverse_iterator(begin());}
  2095. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2096. const_reverse_iterator rend() const _NOEXCEPT
  2097. {return const_reverse_iterator(begin());}
  2098. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2099. const_iterator cbegin() const _NOEXCEPT
  2100. {return __make_iter(0);}
  2101. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2102. const_iterator cend() const _NOEXCEPT
  2103. {return __make_iter(__size_);}
  2104. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2105. const_reverse_iterator crbegin() const _NOEXCEPT
  2106. {return rbegin();}
  2107. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2108. const_reverse_iterator crend() const _NOEXCEPT
  2109. {return rend();}
  2110. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator[](size_type __n) {return __make_ref(__n);}
  2111. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference operator[](size_type __n) const {return __make_ref(__n);}
  2112. _LIBCPP_HIDE_FROM_ABI reference at(size_type __n);
  2113. _LIBCPP_HIDE_FROM_ABI const_reference at(size_type __n) const;
  2114. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference front() {return __make_ref(0);}
  2115. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference front() const {return __make_ref(0);}
  2116. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference back() {return __make_ref(__size_ - 1);}
  2117. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference back() const {return __make_ref(__size_ - 1);}
  2118. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void push_back(const value_type& __x);
  2119. #if _LIBCPP_STD_VER >= 14
  2120. template <class... _Args>
  2121. #if _LIBCPP_STD_VER >= 17
  2122. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference emplace_back(_Args&&... __args)
  2123. #else
  2124. _LIBCPP_HIDE_FROM_ABI void emplace_back(_Args&&... __args)
  2125. #endif
  2126. {
  2127. push_back ( value_type ( std::forward<_Args>(__args)... ));
  2128. #if _LIBCPP_STD_VER >= 17
  2129. return this->back();
  2130. #endif
  2131. }
  2132. #endif
  2133. #if _LIBCPP_STD_VER >= 23
  2134. template <_ContainerCompatibleRange<bool> _Range>
  2135. _LIBCPP_HIDE_FROM_ABI
  2136. constexpr void append_range(_Range&& __range) {
  2137. insert_range(end(), std::forward<_Range>(__range));
  2138. }
  2139. #endif
  2140. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void pop_back() {--__size_;}
  2141. #if _LIBCPP_STD_VER >= 14
  2142. template <class... _Args>
  2143. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator emplace(const_iterator __position, _Args&&... __args)
  2144. { return insert ( __position, value_type ( std::forward<_Args>(__args)... )); }
  2145. #endif
  2146. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator insert(const_iterator __position, const value_type& __x);
  2147. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
  2148. template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
  2149. iterator
  2150. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
  2151. template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
  2152. iterator
  2153. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
  2154. #if _LIBCPP_STD_VER >= 23
  2155. template <_ContainerCompatibleRange<bool> _Range>
  2156. _LIBCPP_HIDE_FROM_ABI
  2157. constexpr iterator insert_range(const_iterator __position, _Range&& __range) {
  2158. if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
  2159. auto __n = static_cast<size_type>(ranges::distance(__range));
  2160. return __insert_with_size(__position, ranges::begin(__range), ranges::end(__range), __n);
  2161. } else {
  2162. return __insert_with_sentinel(__position, ranges::begin(__range), ranges::end(__range));
  2163. }
  2164. }
  2165. #endif
  2166. #ifndef _LIBCPP_CXX03_LANG
  2167. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2168. iterator insert(const_iterator __position, initializer_list<value_type> __il)
  2169. {return insert(__position, __il.begin(), __il.end());}
  2170. #endif
  2171. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator erase(const_iterator __position);
  2172. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator erase(const_iterator __first, const_iterator __last);
  2173. _LIBCPP_REINITIALIZES_OBJECT _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2174. void clear() _NOEXCEPT {__size_ = 0;}
  2175. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(vector&)
  2176. #if _LIBCPP_STD_VER >= 14
  2177. _NOEXCEPT;
  2178. #else
  2179. _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
  2180. __is_nothrow_swappable<allocator_type>::value);
  2181. #endif
  2182. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void swap(reference __x, reference __y) _NOEXCEPT { std::swap(__x, __y); }
  2183. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void resize(size_type __sz, value_type __x = false);
  2184. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void flip() _NOEXCEPT;
  2185. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __invariants() const;
  2186. private:
  2187. _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
  2188. void __throw_length_error() const {
  2189. std::__throw_length_error("vector");
  2190. }
  2191. _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
  2192. void __throw_out_of_range() const {
  2193. std::__throw_out_of_range("vector");
  2194. }
  2195. template <class _InputIterator, class _Sentinel>
  2196. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2197. void __init_with_size(_InputIterator __first, _Sentinel __last, size_type __n) {
  2198. auto __guard = std::__make_exception_guard(__destroy_vector(*this));
  2199. if (__n > 0) {
  2200. __vallocate(__n);
  2201. __construct_at_end(std::move(__first), std::move(__last), __n);
  2202. }
  2203. __guard.__complete();
  2204. }
  2205. template <class _InputIterator, class _Sentinel>
  2206. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2207. void __init_with_sentinel(_InputIterator __first, _Sentinel __last) {
  2208. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  2209. try {
  2210. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  2211. for (; __first != __last; ++__first)
  2212. push_back(*__first);
  2213. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  2214. } catch (...) {
  2215. if (__begin_ != nullptr)
  2216. __storage_traits::deallocate(__alloc(), __begin_, __cap());
  2217. throw;
  2218. }
  2219. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  2220. }
  2221. template <class _Iterator, class _Sentinel>
  2222. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  2223. void __assign_with_sentinel(_Iterator __first, _Sentinel __last);
  2224. template <class _ForwardIterator, class _Sentinel>
  2225. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  2226. void __assign_with_size(_ForwardIterator __first, _Sentinel __last, difference_type __ns);
  2227. template <class _InputIterator, class _Sentinel>
  2228. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  2229. iterator __insert_with_sentinel(const_iterator __position, _InputIterator __first, _Sentinel __last);
  2230. template <class _Iterator, class _Sentinel>
  2231. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  2232. iterator __insert_with_size(const_iterator __position, _Iterator __first, _Sentinel __last, difference_type __n);
  2233. // Allocate space for __n objects
  2234. // throws length_error if __n > max_size()
  2235. // throws (probably bad_alloc) if memory run out
  2236. // Precondition: __begin_ == __end_ == __cap() == 0
  2237. // Precondition: __n > 0
  2238. // Postcondition: capacity() >= __n
  2239. // Postcondition: size() == 0
  2240. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __vallocate(size_type __n) {
  2241. if (__n > max_size())
  2242. __throw_length_error();
  2243. auto __allocation = std::__allocate_at_least(__alloc(), __external_cap_to_internal(__n));
  2244. __begin_ = __allocation.ptr;
  2245. __size_ = 0;
  2246. __cap() = __allocation.count;
  2247. if (__libcpp_is_constant_evaluated()) {
  2248. for (size_type __i = 0; __i != __cap(); ++__i)
  2249. std::__construct_at(std::__to_address(__begin_) + __i);
  2250. }
  2251. }
  2252. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __vdeallocate() _NOEXCEPT;
  2253. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2254. static size_type __align_it(size_type __new_size) _NOEXCEPT
  2255. {return (__new_size + (__bits_per_word-1)) & ~((size_type)__bits_per_word-1);}
  2256. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type __recommend(size_type __new_size) const;
  2257. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct_at_end(size_type __n, bool __x);
  2258. template <class _InputIterator, class _Sentinel>
  2259. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2260. void __construct_at_end(_InputIterator __first, _Sentinel __last, size_type __n);
  2261. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __append(size_type __n, const_reference __x);
  2262. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2263. reference __make_ref(size_type __pos) _NOEXCEPT
  2264. {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
  2265. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2266. const_reference __make_ref(size_type __pos) const _NOEXCEPT {
  2267. return __bit_const_reference<vector>(__begin_ + __pos / __bits_per_word,
  2268. __storage_type(1) << __pos % __bits_per_word);
  2269. }
  2270. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2271. iterator __make_iter(size_type __pos) _NOEXCEPT
  2272. {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
  2273. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2274. const_iterator __make_iter(size_type __pos) const _NOEXCEPT
  2275. {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
  2276. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2277. iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
  2278. {return begin() + (__p - cbegin());}
  2279. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2280. void __copy_assign_alloc(const vector& __v)
  2281. {__copy_assign_alloc(__v, integral_constant<bool,
  2282. __storage_traits::propagate_on_container_copy_assignment::value>());}
  2283. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2284. void __copy_assign_alloc(const vector& __c, true_type)
  2285. {
  2286. if (__alloc() != __c.__alloc())
  2287. __vdeallocate();
  2288. __alloc() = __c.__alloc();
  2289. }
  2290. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2291. void __copy_assign_alloc(const vector&, false_type)
  2292. {}
  2293. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign(vector& __c, false_type);
  2294. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign(vector& __c, true_type)
  2295. _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
  2296. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2297. void __move_assign_alloc(vector& __c)
  2298. _NOEXCEPT_(
  2299. !__storage_traits::propagate_on_container_move_assignment::value ||
  2300. is_nothrow_move_assignable<allocator_type>::value)
  2301. {__move_assign_alloc(__c, integral_constant<bool,
  2302. __storage_traits::propagate_on_container_move_assignment::value>());}
  2303. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2304. void __move_assign_alloc(vector& __c, true_type)
  2305. _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
  2306. {
  2307. __alloc() = std::move(__c.__alloc());
  2308. }
  2309. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2310. void __move_assign_alloc(vector&, false_type)
  2311. _NOEXCEPT
  2312. {}
  2313. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_t __hash_code() const _NOEXCEPT;
  2314. friend class __bit_reference<vector>;
  2315. friend class __bit_const_reference<vector>;
  2316. friend class __bit_iterator<vector, false>;
  2317. friend class __bit_iterator<vector, true>;
  2318. friend struct __bit_array<vector>;
  2319. friend struct _LIBCPP_TEMPLATE_VIS hash<vector>;
  2320. };
  2321. template <class _Allocator>
  2322. _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  2323. vector<bool, _Allocator>::__vdeallocate() _NOEXCEPT
  2324. {
  2325. if (this->__begin_ != nullptr)
  2326. {
  2327. __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
  2328. this->__begin_ = nullptr;
  2329. this->__size_ = this->__cap() = 0;
  2330. }
  2331. }
  2332. template <class _Allocator>
  2333. _LIBCPP_CONSTEXPR_SINCE_CXX20
  2334. typename vector<bool, _Allocator>::size_type
  2335. vector<bool, _Allocator>::max_size() const _NOEXCEPT
  2336. {
  2337. size_type __amax = __storage_traits::max_size(__alloc());
  2338. size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
  2339. if (__nmax / __bits_per_word <= __amax)
  2340. return __nmax;
  2341. return __internal_cap_to_external(__amax);
  2342. }
  2343. // Precondition: __new_size > capacity()
  2344. template <class _Allocator>
  2345. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2346. typename vector<bool, _Allocator>::size_type
  2347. vector<bool, _Allocator>::__recommend(size_type __new_size) const
  2348. {
  2349. const size_type __ms = max_size();
  2350. if (__new_size > __ms)
  2351. this->__throw_length_error();
  2352. const size_type __cap = capacity();
  2353. if (__cap >= __ms / 2)
  2354. return __ms;
  2355. return std::max(2 * __cap, __align_it(__new_size));
  2356. }
  2357. // Default constructs __n objects starting at __end_
  2358. // Precondition: __n > 0
  2359. // Precondition: size() + __n <= capacity()
  2360. // Postcondition: size() == size() + __n
  2361. template <class _Allocator>
  2362. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2363. void
  2364. vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
  2365. {
  2366. size_type __old_size = this->__size_;
  2367. this->__size_ += __n;
  2368. if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word))
  2369. {
  2370. if (this->__size_ <= __bits_per_word)
  2371. this->__begin_[0] = __storage_type(0);
  2372. else
  2373. this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
  2374. }
  2375. std::fill_n(__make_iter(__old_size), __n, __x);
  2376. }
  2377. template <class _Allocator>
  2378. template <class _InputIterator, class _Sentinel>
  2379. _LIBCPP_CONSTEXPR_SINCE_CXX20
  2380. void vector<bool, _Allocator>::__construct_at_end(_InputIterator __first, _Sentinel __last, size_type __n) {
  2381. size_type __old_size = this->__size_;
  2382. this->__size_ += __n;
  2383. if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word))
  2384. {
  2385. if (this->__size_ <= __bits_per_word)
  2386. this->__begin_[0] = __storage_type(0);
  2387. else
  2388. this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
  2389. }
  2390. std::__copy<_ClassicAlgPolicy>(__first, __last, __make_iter(__old_size));
  2391. }
  2392. template <class _Allocator>
  2393. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2394. vector<bool, _Allocator>::vector()
  2395. _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
  2396. : __begin_(nullptr),
  2397. __size_(0),
  2398. __cap_alloc_(0, __default_init_tag())
  2399. {
  2400. }
  2401. template <class _Allocator>
  2402. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2403. vector<bool, _Allocator>::vector(const allocator_type& __a)
  2404. #if _LIBCPP_STD_VER <= 14
  2405. _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
  2406. #else
  2407. _NOEXCEPT
  2408. #endif
  2409. : __begin_(nullptr),
  2410. __size_(0),
  2411. __cap_alloc_(0, static_cast<__storage_allocator>(__a))
  2412. {
  2413. }
  2414. template <class _Allocator>
  2415. _LIBCPP_CONSTEXPR_SINCE_CXX20
  2416. vector<bool, _Allocator>::vector(size_type __n)
  2417. : __begin_(nullptr),
  2418. __size_(0),
  2419. __cap_alloc_(0, __default_init_tag())
  2420. {
  2421. if (__n > 0)
  2422. {
  2423. __vallocate(__n);
  2424. __construct_at_end(__n, false);
  2425. }
  2426. }
  2427. #if _LIBCPP_STD_VER >= 14
  2428. template <class _Allocator>
  2429. _LIBCPP_CONSTEXPR_SINCE_CXX20
  2430. vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
  2431. : __begin_(nullptr),
  2432. __size_(0),
  2433. __cap_alloc_(0, static_cast<__storage_allocator>(__a))
  2434. {
  2435. if (__n > 0)
  2436. {
  2437. __vallocate(__n);
  2438. __construct_at_end(__n, false);
  2439. }
  2440. }
  2441. #endif
  2442. template <class _Allocator>
  2443. _LIBCPP_CONSTEXPR_SINCE_CXX20
  2444. vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
  2445. : __begin_(nullptr),
  2446. __size_(0),
  2447. __cap_alloc_(0, __default_init_tag())
  2448. {
  2449. if (__n > 0)
  2450. {
  2451. __vallocate(__n);
  2452. __construct_at_end(__n, __x);
  2453. }
  2454. }
  2455. template <class _Allocator>
  2456. _LIBCPP_CONSTEXPR_SINCE_CXX20
  2457. vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
  2458. : __begin_(nullptr),
  2459. __size_(0),
  2460. __cap_alloc_(0, static_cast<__storage_allocator>(__a))
  2461. {
  2462. if (__n > 0)
  2463. {
  2464. __vallocate(__n);
  2465. __construct_at_end(__n, __x);
  2466. }
  2467. }
  2468. template <class _Allocator>
  2469. template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> >
  2470. _LIBCPP_CONSTEXPR_SINCE_CXX20
  2471. vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last)
  2472. : __begin_(nullptr),
  2473. __size_(0),
  2474. __cap_alloc_(0, __default_init_tag())
  2475. {
  2476. __init_with_sentinel(__first, __last);
  2477. }
  2478. template <class _Allocator>
  2479. template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> >
  2480. _LIBCPP_CONSTEXPR_SINCE_CXX20
  2481. vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a)
  2482. : __begin_(nullptr),
  2483. __size_(0),
  2484. __cap_alloc_(0, static_cast<__storage_allocator>(__a))
  2485. {
  2486. __init_with_sentinel(__first, __last);
  2487. }
  2488. template <class _Allocator>
  2489. template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
  2490. _LIBCPP_CONSTEXPR_SINCE_CXX20
  2491. vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last)
  2492. : __begin_(nullptr),
  2493. __size_(0),
  2494. __cap_alloc_(0, __default_init_tag())
  2495. {
  2496. auto __n = static_cast<size_type>(std::distance(__first, __last));
  2497. __init_with_size(__first, __last, __n);
  2498. }
  2499. template <class _Allocator>
  2500. template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
  2501. _LIBCPP_CONSTEXPR_SINCE_CXX20
  2502. vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a)
  2503. : __begin_(nullptr),
  2504. __size_(0),
  2505. __cap_alloc_(0, static_cast<__storage_allocator>(__a))
  2506. {
  2507. auto __n = static_cast<size_type>(std::distance(__first, __last));
  2508. __init_with_size(__first, __last, __n);
  2509. }
  2510. #ifndef _LIBCPP_CXX03_LANG
  2511. template <class _Allocator>
  2512. _LIBCPP_CONSTEXPR_SINCE_CXX20
  2513. vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
  2514. : __begin_(nullptr),
  2515. __size_(0),
  2516. __cap_alloc_(0, __default_init_tag())
  2517. {
  2518. size_type __n = static_cast<size_type>(__il.size());
  2519. if (__n > 0)
  2520. {
  2521. __vallocate(__n);
  2522. __construct_at_end(__il.begin(), __il.end(), __n);
  2523. }
  2524. }
  2525. template <class _Allocator>
  2526. _LIBCPP_CONSTEXPR_SINCE_CXX20
  2527. vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
  2528. : __begin_(nullptr),
  2529. __size_(0),
  2530. __cap_alloc_(0, static_cast<__storage_allocator>(__a))
  2531. {
  2532. size_type __n = static_cast<size_type>(__il.size());
  2533. if (__n > 0)
  2534. {
  2535. __vallocate(__n);
  2536. __construct_at_end(__il.begin(), __il.end(), __n);
  2537. }
  2538. }
  2539. #endif // _LIBCPP_CXX03_LANG
  2540. template <class _Allocator>
  2541. _LIBCPP_CONSTEXPR_SINCE_CXX20
  2542. vector<bool, _Allocator>::vector(const vector& __v)
  2543. : __begin_(nullptr),
  2544. __size_(0),
  2545. __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
  2546. {
  2547. if (__v.size() > 0)
  2548. {
  2549. __vallocate(__v.size());
  2550. __construct_at_end(__v.begin(), __v.end(), __v.size());
  2551. }
  2552. }
  2553. template <class _Allocator>
  2554. _LIBCPP_CONSTEXPR_SINCE_CXX20
  2555. vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
  2556. : __begin_(nullptr),
  2557. __size_(0),
  2558. __cap_alloc_(0, __a)
  2559. {
  2560. if (__v.size() > 0)
  2561. {
  2562. __vallocate(__v.size());
  2563. __construct_at_end(__v.begin(), __v.end(), __v.size());
  2564. }
  2565. }
  2566. template <class _Allocator>
  2567. _LIBCPP_CONSTEXPR_SINCE_CXX20
  2568. vector<bool, _Allocator>&
  2569. vector<bool, _Allocator>::operator=(const vector& __v)
  2570. {
  2571. if (this != std::addressof(__v))
  2572. {
  2573. __copy_assign_alloc(__v);
  2574. if (__v.__size_)
  2575. {
  2576. if (__v.__size_ > capacity())
  2577. {
  2578. __vdeallocate();
  2579. __vallocate(__v.__size_);
  2580. }
  2581. std::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
  2582. }
  2583. __size_ = __v.__size_;
  2584. }
  2585. return *this;
  2586. }
  2587. template <class _Allocator>
  2588. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>::vector(vector&& __v)
  2589. #if _LIBCPP_STD_VER >= 17
  2590. _NOEXCEPT
  2591. #else
  2592. _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
  2593. #endif
  2594. : __begin_(__v.__begin_),
  2595. __size_(__v.__size_),
  2596. __cap_alloc_(std::move(__v.__cap_alloc_)) {
  2597. __v.__begin_ = nullptr;
  2598. __v.__size_ = 0;
  2599. __v.__cap() = 0;
  2600. }
  2601. template <class _Allocator>
  2602. _LIBCPP_CONSTEXPR_SINCE_CXX20
  2603. vector<bool, _Allocator>::vector(vector&& __v, const __type_identity_t<allocator_type>& __a)
  2604. : __begin_(nullptr),
  2605. __size_(0),
  2606. __cap_alloc_(0, __a)
  2607. {
  2608. if (__a == allocator_type(__v.__alloc()))
  2609. {
  2610. this->__begin_ = __v.__begin_;
  2611. this->__size_ = __v.__size_;
  2612. this->__cap() = __v.__cap();
  2613. __v.__begin_ = nullptr;
  2614. __v.__cap() = __v.__size_ = 0;
  2615. }
  2616. else if (__v.size() > 0)
  2617. {
  2618. __vallocate(__v.size());
  2619. __construct_at_end(__v.begin(), __v.end(), __v.size());
  2620. }
  2621. }
  2622. template <class _Allocator>
  2623. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2624. vector<bool, _Allocator>&
  2625. vector<bool, _Allocator>::operator=(vector&& __v)
  2626. _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
  2627. {
  2628. __move_assign(__v, integral_constant<bool,
  2629. __storage_traits::propagate_on_container_move_assignment::value>());
  2630. return *this;
  2631. }
  2632. template <class _Allocator>
  2633. _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  2634. vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
  2635. {
  2636. if (__alloc() != __c.__alloc())
  2637. assign(__c.begin(), __c.end());
  2638. else
  2639. __move_assign(__c, true_type());
  2640. }
  2641. template <class _Allocator>
  2642. _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  2643. vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
  2644. _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
  2645. {
  2646. __vdeallocate();
  2647. __move_assign_alloc(__c);
  2648. this->__begin_ = __c.__begin_;
  2649. this->__size_ = __c.__size_;
  2650. this->__cap() = __c.__cap();
  2651. __c.__begin_ = nullptr;
  2652. __c.__cap() = __c.__size_ = 0;
  2653. }
  2654. template <class _Allocator>
  2655. _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  2656. vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
  2657. {
  2658. __size_ = 0;
  2659. if (__n > 0)
  2660. {
  2661. size_type __c = capacity();
  2662. if (__n <= __c)
  2663. __size_ = __n;
  2664. else
  2665. {
  2666. vector __v(get_allocator());
  2667. __v.reserve(__recommend(__n));
  2668. __v.__size_ = __n;
  2669. swap(__v);
  2670. }
  2671. std::fill_n(begin(), __n, __x);
  2672. }
  2673. }
  2674. template <class _Allocator>
  2675. template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> >
  2676. _LIBCPP_CONSTEXPR_SINCE_CXX20
  2677. void
  2678. vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
  2679. {
  2680. __assign_with_sentinel(__first, __last);
  2681. }
  2682. template <class _Allocator>
  2683. template <class _Iterator, class _Sentinel>
  2684. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  2685. void vector<bool, _Allocator>::__assign_with_sentinel(_Iterator __first, _Sentinel __last) {
  2686. clear();
  2687. for (; __first != __last; ++__first)
  2688. push_back(*__first);
  2689. }
  2690. template <class _Allocator>
  2691. template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
  2692. _LIBCPP_CONSTEXPR_SINCE_CXX20
  2693. void
  2694. vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
  2695. {
  2696. __assign_with_size(__first, __last, std::distance(__first, __last));
  2697. }
  2698. template <class _Allocator>
  2699. template <class _ForwardIterator, class _Sentinel>
  2700. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  2701. void vector<bool, _Allocator>::__assign_with_size(_ForwardIterator __first, _Sentinel __last, difference_type __ns) {
  2702. _LIBCPP_ASSERT_VALID_INPUT_RANGE(__ns >= 0, "invalid range specified");
  2703. clear();
  2704. const size_t __n = static_cast<size_type>(__ns);
  2705. if (__n)
  2706. {
  2707. if (__n > capacity())
  2708. {
  2709. __vdeallocate();
  2710. __vallocate(__n);
  2711. }
  2712. __construct_at_end(__first, __last, __n);
  2713. }
  2714. }
  2715. template <class _Allocator>
  2716. _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  2717. vector<bool, _Allocator>::reserve(size_type __n)
  2718. {
  2719. if (__n > capacity())
  2720. {
  2721. if (__n > max_size())
  2722. this->__throw_length_error();
  2723. vector __v(this->get_allocator());
  2724. __v.__vallocate(__n);
  2725. __v.__construct_at_end(this->begin(), this->end(), this->size());
  2726. swap(__v);
  2727. }
  2728. }
  2729. template <class _Allocator>
  2730. _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  2731. vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
  2732. {
  2733. if (__external_cap_to_internal(size()) > __cap())
  2734. {
  2735. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  2736. try
  2737. {
  2738. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  2739. vector(*this, allocator_type(__alloc())).swap(*this);
  2740. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  2741. }
  2742. catch (...)
  2743. {
  2744. }
  2745. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  2746. }
  2747. }
  2748. template <class _Allocator>
  2749. typename vector<bool, _Allocator>::reference
  2750. vector<bool, _Allocator>::at(size_type __n)
  2751. {
  2752. if (__n >= size())
  2753. this->__throw_out_of_range();
  2754. return (*this)[__n];
  2755. }
  2756. template <class _Allocator>
  2757. typename vector<bool, _Allocator>::const_reference
  2758. vector<bool, _Allocator>::at(size_type __n) const
  2759. {
  2760. if (__n >= size())
  2761. this->__throw_out_of_range();
  2762. return (*this)[__n];
  2763. }
  2764. template <class _Allocator>
  2765. _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  2766. vector<bool, _Allocator>::push_back(const value_type& __x)
  2767. {
  2768. if (this->__size_ == this->capacity())
  2769. reserve(__recommend(this->__size_ + 1));
  2770. ++this->__size_;
  2771. back() = __x;
  2772. }
  2773. template <class _Allocator>
  2774. _LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<bool, _Allocator>::iterator
  2775. vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
  2776. {
  2777. iterator __r;
  2778. if (size() < capacity())
  2779. {
  2780. const_iterator __old_end = end();
  2781. ++__size_;
  2782. std::copy_backward(__position, __old_end, end());
  2783. __r = __const_iterator_cast(__position);
  2784. }
  2785. else
  2786. {
  2787. vector __v(get_allocator());
  2788. __v.reserve(__recommend(__size_ + 1));
  2789. __v.__size_ = __size_ + 1;
  2790. __r = std::copy(cbegin(), __position, __v.begin());
  2791. std::copy_backward(__position, cend(), __v.end());
  2792. swap(__v);
  2793. }
  2794. *__r = __x;
  2795. return __r;
  2796. }
  2797. template <class _Allocator>
  2798. _LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<bool, _Allocator>::iterator
  2799. vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
  2800. {
  2801. iterator __r;
  2802. size_type __c = capacity();
  2803. if (__n <= __c && size() <= __c - __n)
  2804. {
  2805. const_iterator __old_end = end();
  2806. __size_ += __n;
  2807. std::copy_backward(__position, __old_end, end());
  2808. __r = __const_iterator_cast(__position);
  2809. }
  2810. else
  2811. {
  2812. vector __v(get_allocator());
  2813. __v.reserve(__recommend(__size_ + __n));
  2814. __v.__size_ = __size_ + __n;
  2815. __r = std::copy(cbegin(), __position, __v.begin());
  2816. std::copy_backward(__position, cend(), __v.end());
  2817. swap(__v);
  2818. }
  2819. std::fill_n(__r, __n, __x);
  2820. return __r;
  2821. }
  2822. template <class _Allocator>
  2823. template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> >
  2824. _LIBCPP_CONSTEXPR_SINCE_CXX20
  2825. typename vector<bool, _Allocator>::iterator
  2826. vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
  2827. {
  2828. return __insert_with_sentinel(__position, __first, __last);
  2829. }
  2830. template <class _Allocator>
  2831. template <class _InputIterator, class _Sentinel>
  2832. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  2833. typename vector<bool, _Allocator>::iterator
  2834. vector<bool, _Allocator>::__insert_with_sentinel(const_iterator __position, _InputIterator __first, _Sentinel __last) {
  2835. difference_type __off = __position - begin();
  2836. iterator __p = __const_iterator_cast(__position);
  2837. iterator __old_end = end();
  2838. for (; size() != capacity() && __first != __last; ++__first)
  2839. {
  2840. ++this->__size_;
  2841. back() = *__first;
  2842. }
  2843. vector __v(get_allocator());
  2844. if (__first != __last)
  2845. {
  2846. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  2847. try
  2848. {
  2849. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  2850. __v.__assign_with_sentinel(std::move(__first), std::move(__last));
  2851. difference_type __old_size = static_cast<difference_type>(__old_end - begin());
  2852. difference_type __old_p = __p - begin();
  2853. reserve(__recommend(size() + __v.size()));
  2854. __p = begin() + __old_p;
  2855. __old_end = begin() + __old_size;
  2856. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  2857. }
  2858. catch (...)
  2859. {
  2860. erase(__old_end, end());
  2861. throw;
  2862. }
  2863. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  2864. }
  2865. __p = std::rotate(__p, __old_end, end());
  2866. insert(__p, __v.begin(), __v.end());
  2867. return begin() + __off;
  2868. }
  2869. template <class _Allocator>
  2870. template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
  2871. _LIBCPP_CONSTEXPR_SINCE_CXX20
  2872. typename vector<bool, _Allocator>::iterator
  2873. vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
  2874. {
  2875. return __insert_with_size(__position, __first, __last, std::distance(__first, __last));
  2876. }
  2877. template <class _Allocator>
  2878. template <class _ForwardIterator, class _Sentinel>
  2879. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  2880. typename vector<bool, _Allocator>::iterator
  2881. vector<bool, _Allocator>::__insert_with_size(const_iterator __position, _ForwardIterator __first, _Sentinel __last,
  2882. difference_type __n_signed) {
  2883. _LIBCPP_ASSERT_VALID_INPUT_RANGE(__n_signed >= 0, "invalid range specified");
  2884. const size_type __n = static_cast<size_type>(__n_signed);
  2885. iterator __r;
  2886. size_type __c = capacity();
  2887. if (__n <= __c && size() <= __c - __n)
  2888. {
  2889. const_iterator __old_end = end();
  2890. __size_ += __n;
  2891. std::copy_backward(__position, __old_end, end());
  2892. __r = __const_iterator_cast(__position);
  2893. }
  2894. else
  2895. {
  2896. vector __v(get_allocator());
  2897. __v.reserve(__recommend(__size_ + __n));
  2898. __v.__size_ = __size_ + __n;
  2899. __r = std::copy(cbegin(), __position, __v.begin());
  2900. std::copy_backward(__position, cend(), __v.end());
  2901. swap(__v);
  2902. }
  2903. std::__copy<_ClassicAlgPolicy>(__first, __last, __r);
  2904. return __r;
  2905. }
  2906. template <class _Allocator>
  2907. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  2908. typename vector<bool, _Allocator>::iterator
  2909. vector<bool, _Allocator>::erase(const_iterator __position)
  2910. {
  2911. iterator __r = __const_iterator_cast(__position);
  2912. std::copy(__position + 1, this->cend(), __r);
  2913. --__size_;
  2914. return __r;
  2915. }
  2916. template <class _Allocator>
  2917. _LIBCPP_CONSTEXPR_SINCE_CXX20
  2918. typename vector<bool, _Allocator>::iterator
  2919. vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
  2920. {
  2921. iterator __r = __const_iterator_cast(__first);
  2922. difference_type __d = __last - __first;
  2923. std::copy(__last, this->cend(), __r);
  2924. __size_ -= __d;
  2925. return __r;
  2926. }
  2927. template <class _Allocator>
  2928. _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  2929. vector<bool, _Allocator>::swap(vector& __x)
  2930. #if _LIBCPP_STD_VER >= 14
  2931. _NOEXCEPT
  2932. #else
  2933. _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
  2934. __is_nothrow_swappable<allocator_type>::value)
  2935. #endif
  2936. {
  2937. std::swap(this->__begin_, __x.__begin_);
  2938. std::swap(this->__size_, __x.__size_);
  2939. std::swap(this->__cap(), __x.__cap());
  2940. std::__swap_allocator(this->__alloc(), __x.__alloc(),
  2941. integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>());
  2942. }
  2943. template <class _Allocator>
  2944. _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  2945. vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
  2946. {
  2947. size_type __cs = size();
  2948. if (__cs < __sz)
  2949. {
  2950. iterator __r;
  2951. size_type __c = capacity();
  2952. size_type __n = __sz - __cs;
  2953. if (__n <= __c && __cs <= __c - __n)
  2954. {
  2955. __r = end();
  2956. __size_ += __n;
  2957. }
  2958. else
  2959. {
  2960. vector __v(get_allocator());
  2961. __v.reserve(__recommend(__size_ + __n));
  2962. __v.__size_ = __size_ + __n;
  2963. __r = std::copy(cbegin(), cend(), __v.begin());
  2964. swap(__v);
  2965. }
  2966. std::fill_n(__r, __n, __x);
  2967. }
  2968. else
  2969. __size_ = __sz;
  2970. }
  2971. template <class _Allocator>
  2972. _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  2973. vector<bool, _Allocator>::flip() _NOEXCEPT
  2974. {
  2975. // do middle whole words
  2976. size_type __n = __size_;
  2977. __storage_pointer __p = __begin_;
  2978. for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
  2979. *__p = ~*__p;
  2980. // do last partial word
  2981. if (__n > 0)
  2982. {
  2983. __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
  2984. __storage_type __b = *__p & __m;
  2985. *__p &= ~__m;
  2986. *__p |= ~__b & __m;
  2987. }
  2988. }
  2989. template <class _Allocator>
  2990. _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
  2991. vector<bool, _Allocator>::__invariants() const
  2992. {
  2993. if (this->__begin_ == nullptr)
  2994. {
  2995. if (this->__size_ != 0 || this->__cap() != 0)
  2996. return false;
  2997. }
  2998. else
  2999. {
  3000. if (this->__cap() == 0)
  3001. return false;
  3002. if (this->__size_ > this->capacity())
  3003. return false;
  3004. }
  3005. return true;
  3006. }
  3007. template <class _Allocator>
  3008. _LIBCPP_CONSTEXPR_SINCE_CXX20 size_t
  3009. vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
  3010. {
  3011. size_t __h = 0;
  3012. // do middle whole words
  3013. size_type __n = __size_;
  3014. __storage_pointer __p = __begin_;
  3015. for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
  3016. __h ^= *__p;
  3017. // do last partial word
  3018. if (__n > 0)
  3019. {
  3020. const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
  3021. __h ^= *__p & __m;
  3022. }
  3023. return __h;
  3024. }
  3025. template <class _Allocator>
  3026. struct _LIBCPP_TEMPLATE_VIS hash<vector<bool, _Allocator> >
  3027. : public __unary_function<vector<bool, _Allocator>, size_t>
  3028. {
  3029. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  3030. size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
  3031. {return __vec.__hash_code();}
  3032. };
  3033. #else // _YNDX_LIBCXX_ENABLE_VECTOR_BOOL_COMPRESSION
  3034. // Hash function implementation for uncompressed std::vector<bool> which returns the same result.
  3035. template <class _Allocator>
  3036. struct _LIBCPP_TEMPLATE_VIS hash<vector<bool, _Allocator> >
  3037. : public unary_function<vector<bool, _Allocator>, size_t>
  3038. {
  3039. _LIBCPP_INLINE_VISIBILITY
  3040. size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
  3041. {
  3042. size_t __h = 0;
  3043. size_t __idx = 0;
  3044. size_t __n = __vec.size();
  3045. constexpr size_t __bits_per_word = sizeof(typename allocator_traits<_Allocator>::size_type) * CHAR_BIT;
  3046. static_assert(sizeof(typename allocator_traits<_Allocator>::size_type) <= sizeof(size_t), "size_type constraint violated");
  3047. for (;__idx + __bits_per_word <= __n;) {
  3048. for (size_t __bit = 0; __bit < __bits_per_word; __bit++, __idx++) {
  3049. __h ^= static_cast<size_t>(__vec[__idx]) << __bit;
  3050. }
  3051. }
  3052. for (size_t __bit = 0; __idx < __n; __bit++, __idx++) {
  3053. __h ^= static_cast<size_t>(__vec[__idx]) << __bit;
  3054. }
  3055. return __h;
  3056. }
  3057. };
  3058. #endif // _YNDX_LIBCXX_ENABLE_VECTOR_BOOL_COMPRESSION
  3059. template <class _Tp, class _Allocator>
  3060. _LIBCPP_CONSTEXPR_SINCE_CXX20
  3061. inline _LIBCPP_HIDE_FROM_ABI
  3062. bool
  3063. operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
  3064. {
  3065. const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
  3066. return __sz == __y.size() && std::equal(__x.begin(), __x.end(), __y.begin());
  3067. }
  3068. #if _LIBCPP_STD_VER <= 17
  3069. template <class _Tp, class _Allocator>
  3070. inline _LIBCPP_HIDE_FROM_ABI
  3071. bool
  3072. operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
  3073. {
  3074. return !(__x == __y);
  3075. }
  3076. template <class _Tp, class _Allocator>
  3077. inline _LIBCPP_HIDE_FROM_ABI
  3078. bool
  3079. operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
  3080. {
  3081. return std::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
  3082. }
  3083. template <class _Tp, class _Allocator>
  3084. inline _LIBCPP_HIDE_FROM_ABI
  3085. bool
  3086. operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
  3087. {
  3088. return __y < __x;
  3089. }
  3090. template <class _Tp, class _Allocator>
  3091. inline _LIBCPP_HIDE_FROM_ABI
  3092. bool
  3093. operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
  3094. {
  3095. return !(__x < __y);
  3096. }
  3097. template <class _Tp, class _Allocator>
  3098. inline _LIBCPP_HIDE_FROM_ABI
  3099. bool
  3100. operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
  3101. {
  3102. return !(__y < __x);
  3103. }
  3104. #else // _LIBCPP_STD_VER <= 17
  3105. template <class _Tp, class _Allocator>
  3106. _LIBCPP_HIDE_FROM_ABI constexpr __synth_three_way_result<_Tp>
  3107. operator<=>(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y) {
  3108. return std::lexicographical_compare_three_way(
  3109. __x.begin(), __x.end(), __y.begin(), __y.end(), std::__synth_three_way<_Tp, _Tp>);
  3110. }
  3111. #endif // _LIBCPP_STD_VER <= 17
  3112. template <class _Tp, class _Allocator>
  3113. _LIBCPP_CONSTEXPR_SINCE_CXX20
  3114. inline _LIBCPP_HIDE_FROM_ABI
  3115. void
  3116. swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
  3117. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
  3118. {
  3119. __x.swap(__y);
  3120. }
  3121. #if _LIBCPP_STD_VER >= 20
  3122. template <class _Tp, class _Allocator, class _Up>
  3123. _LIBCPP_CONSTEXPR_SINCE_CXX20
  3124. inline _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::size_type
  3125. erase(vector<_Tp, _Allocator>& __c, const _Up& __v) {
  3126. auto __old_size = __c.size();
  3127. __c.erase(std::remove(__c.begin(), __c.end(), __v), __c.end());
  3128. return __old_size - __c.size();
  3129. }
  3130. template <class _Tp, class _Allocator, class _Predicate>
  3131. _LIBCPP_CONSTEXPR_SINCE_CXX20
  3132. inline _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::size_type
  3133. erase_if(vector<_Tp, _Allocator>& __c, _Predicate __pred) {
  3134. auto __old_size = __c.size();
  3135. __c.erase(std::remove_if(__c.begin(), __c.end(), __pred), __c.end());
  3136. return __old_size - __c.size();
  3137. }
  3138. template <>
  3139. inline constexpr bool __format::__enable_insertable<vector<char>> = true;
  3140. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  3141. template <>
  3142. inline constexpr bool __format::__enable_insertable<vector<wchar_t>> = true;
  3143. #endif
  3144. #endif // _LIBCPP_STD_VER >= 20
  3145. #if _LIBCPP_STD_VER >= 23
  3146. template <class _Tp, class _CharT>
  3147. // Since is-vector-bool-reference is only used once it's inlined here.
  3148. requires same_as<typename _Tp::__container, vector<bool, typename _Tp::__container::allocator_type>>
  3149. struct _LIBCPP_TEMPLATE_VIS formatter<_Tp, _CharT> {
  3150. private:
  3151. formatter<bool, _CharT> __underlying_;
  3152. public:
  3153. template <class _ParseContext>
  3154. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  3155. return __underlying_.parse(__ctx);
  3156. }
  3157. template <class _FormatContext>
  3158. _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(const _Tp& __ref, _FormatContext& __ctx) const {
  3159. return __underlying_.format(__ref, __ctx);
  3160. }
  3161. };
  3162. #endif // _LIBCPP_STD_VER >= 23
  3163. _LIBCPP_END_NAMESPACE_STD
  3164. #if _LIBCPP_STD_VER >= 17
  3165. _LIBCPP_BEGIN_NAMESPACE_STD
  3166. namespace pmr {
  3167. template <class _ValueT>
  3168. using vector _LIBCPP_AVAILABILITY_PMR = std::vector<_ValueT, polymorphic_allocator<_ValueT>>;
  3169. } // namespace pmr
  3170. _LIBCPP_END_NAMESPACE_STD
  3171. #endif
  3172. _LIBCPP_POP_MACROS
  3173. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  3174. # include <algorithm>
  3175. # include <atomic>
  3176. # include <concepts>
  3177. # include <cstdlib>
  3178. # include <type_traits>
  3179. # include <typeinfo>
  3180. # include <utility>
  3181. #endif
  3182. #endif // _LIBCPP_VECTOR