vector 123 KB

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