vector 107 KB

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