vector 128 KB

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