vector 109 KB

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