vector 128 KB

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