stl_vector.h 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129
  1. // Vector implementation -*- C++ -*-
  2. // Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. *
  22. * Copyright (c) 1994
  23. * Hewlett-Packard Company
  24. *
  25. * Permission to use, copy, modify, distribute and sell this software
  26. * and its documentation for any purpose is hereby granted without fee,
  27. * provided that the above copyright notice appear in all copies and
  28. * that both that copyright notice and this permission notice appear
  29. * in supporting documentation. Hewlett-Packard Company makes no
  30. * representations about the suitability of this software for any
  31. * purpose. It is provided "as is" without express or implied warranty.
  32. *
  33. *
  34. * Copyright (c) 1996
  35. * Silicon Graphics Computer Systems, Inc.
  36. *
  37. * Permission to use, copy, modify, distribute and sell this software
  38. * and its documentation for any purpose is hereby granted without fee,
  39. * provided that the above copyright notice appear in all copies and
  40. * that both that copyright notice and this permission notice appear
  41. * in supporting documentation. Silicon Graphics makes no
  42. * representations about the suitability of this software for any
  43. * purpose. It is provided "as is" without express or implied warranty.
  44. */
  45. /** @file bits/stl_vector.h
  46. * This is an internal header file, included by other library headers.
  47. * Do not attempt to use it directly. @headername{vector}
  48. */
  49. #ifndef _STL_VECTOR_H
  50. #define _STL_VECTOR_H 1
  51. #include <bits/stl_iterator_base_funcs.h>
  52. #include <bits/functexcept.h>
  53. #include <bits/concept_check.h>
  54. #if __cplusplus >= 201103L
  55. #include <initializer_list>
  56. #endif
  57. #if __cplusplus > 201703L
  58. # include <compare>
  59. #endif
  60. #include <debug/assertions.h>
  61. #if _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR
  62. extern "C" void
  63. __sanitizer_annotate_contiguous_container(const void*, const void*,
  64. const void*, const void*);
  65. #endif
  66. namespace std _GLIBCXX_VISIBILITY(default)
  67. {
  68. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  69. _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
  70. /// See bits/stl_deque.h's _Deque_base for an explanation.
  71. template<typename _Tp, typename _Alloc>
  72. struct _Vector_base
  73. {
  74. typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
  75. rebind<_Tp>::other _Tp_alloc_type;
  76. typedef typename __gnu_cxx::__alloc_traits<_Tp_alloc_type>::pointer
  77. pointer;
  78. struct _Vector_impl_data
  79. {
  80. pointer _M_start;
  81. pointer _M_finish;
  82. pointer _M_end_of_storage;
  83. _GLIBCXX20_CONSTEXPR
  84. _Vector_impl_data() _GLIBCXX_NOEXCEPT
  85. : _M_start(), _M_finish(), _M_end_of_storage()
  86. { }
  87. #if __cplusplus >= 201103L
  88. _GLIBCXX20_CONSTEXPR
  89. _Vector_impl_data(_Vector_impl_data&& __x) noexcept
  90. : _M_start(__x._M_start), _M_finish(__x._M_finish),
  91. _M_end_of_storage(__x._M_end_of_storage)
  92. { __x._M_start = __x._M_finish = __x._M_end_of_storage = pointer(); }
  93. #endif
  94. _GLIBCXX20_CONSTEXPR
  95. void
  96. _M_copy_data(_Vector_impl_data const& __x) _GLIBCXX_NOEXCEPT
  97. {
  98. _M_start = __x._M_start;
  99. _M_finish = __x._M_finish;
  100. _M_end_of_storage = __x._M_end_of_storage;
  101. }
  102. _GLIBCXX20_CONSTEXPR
  103. void
  104. _M_swap_data(_Vector_impl_data& __x) _GLIBCXX_NOEXCEPT
  105. {
  106. // Do not use std::swap(_M_start, __x._M_start), etc as it loses
  107. // information used by TBAA.
  108. _Vector_impl_data __tmp;
  109. __tmp._M_copy_data(*this);
  110. _M_copy_data(__x);
  111. __x._M_copy_data(__tmp);
  112. }
  113. };
  114. struct _Vector_impl
  115. : public _Tp_alloc_type, public _Vector_impl_data
  116. {
  117. _GLIBCXX20_CONSTEXPR
  118. _Vector_impl() _GLIBCXX_NOEXCEPT_IF(
  119. is_nothrow_default_constructible<_Tp_alloc_type>::value)
  120. : _Tp_alloc_type()
  121. { }
  122. _GLIBCXX20_CONSTEXPR
  123. _Vector_impl(_Tp_alloc_type const& __a) _GLIBCXX_NOEXCEPT
  124. : _Tp_alloc_type(__a)
  125. { }
  126. #if __cplusplus >= 201103L
  127. // Not defaulted, to enforce noexcept(true) even when
  128. // !is_nothrow_move_constructible<_Tp_alloc_type>.
  129. _GLIBCXX20_CONSTEXPR
  130. _Vector_impl(_Vector_impl&& __x) noexcept
  131. : _Tp_alloc_type(std::move(__x)), _Vector_impl_data(std::move(__x))
  132. { }
  133. _GLIBCXX20_CONSTEXPR
  134. _Vector_impl(_Tp_alloc_type&& __a) noexcept
  135. : _Tp_alloc_type(std::move(__a))
  136. { }
  137. _GLIBCXX20_CONSTEXPR
  138. _Vector_impl(_Tp_alloc_type&& __a, _Vector_impl&& __rv) noexcept
  139. : _Tp_alloc_type(std::move(__a)), _Vector_impl_data(std::move(__rv))
  140. { }
  141. #endif
  142. #if _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR
  143. template<typename = _Tp_alloc_type>
  144. struct _Asan
  145. {
  146. typedef typename __gnu_cxx::__alloc_traits<_Tp_alloc_type>
  147. ::size_type size_type;
  148. static _GLIBCXX20_CONSTEXPR void
  149. _S_shrink(_Vector_impl&, size_type) { }
  150. static _GLIBCXX20_CONSTEXPR void
  151. _S_on_dealloc(_Vector_impl&) { }
  152. typedef _Vector_impl& _Reinit;
  153. struct _Grow
  154. {
  155. _GLIBCXX20_CONSTEXPR _Grow(_Vector_impl&, size_type) { }
  156. _GLIBCXX20_CONSTEXPR void _M_grew(size_type) { }
  157. };
  158. };
  159. // Enable ASan annotations for memory obtained from std::allocator.
  160. template<typename _Up>
  161. struct _Asan<allocator<_Up> >
  162. {
  163. typedef typename __gnu_cxx::__alloc_traits<_Tp_alloc_type>
  164. ::size_type size_type;
  165. // Adjust ASan annotation for [_M_start, _M_end_of_storage) to
  166. // mark end of valid region as __curr instead of __prev.
  167. static _GLIBCXX20_CONSTEXPR void
  168. _S_adjust(_Vector_impl& __impl, pointer __prev, pointer __curr)
  169. {
  170. #if __cpp_lib_is_constant_evaluated
  171. if (std::is_constant_evaluated())
  172. return;
  173. #endif
  174. __sanitizer_annotate_contiguous_container(__impl._M_start,
  175. __impl._M_end_of_storage, __prev, __curr);
  176. }
  177. static _GLIBCXX20_CONSTEXPR void
  178. _S_grow(_Vector_impl& __impl, size_type __n)
  179. { _S_adjust(__impl, __impl._M_finish, __impl._M_finish + __n); }
  180. static _GLIBCXX20_CONSTEXPR void
  181. _S_shrink(_Vector_impl& __impl, size_type __n)
  182. { _S_adjust(__impl, __impl._M_finish + __n, __impl._M_finish); }
  183. static _GLIBCXX20_CONSTEXPR void
  184. _S_on_dealloc(_Vector_impl& __impl)
  185. {
  186. if (__impl._M_start)
  187. _S_adjust(__impl, __impl._M_finish, __impl._M_end_of_storage);
  188. }
  189. // Used on reallocation to tell ASan unused capacity is invalid.
  190. struct _Reinit
  191. {
  192. explicit _GLIBCXX20_CONSTEXPR
  193. _Reinit(_Vector_impl& __impl) : _M_impl(__impl)
  194. {
  195. // Mark unused capacity as valid again before deallocating it.
  196. _S_on_dealloc(_M_impl);
  197. }
  198. _GLIBCXX20_CONSTEXPR
  199. ~_Reinit()
  200. {
  201. // Mark unused capacity as invalid after reallocation.
  202. if (_M_impl._M_start)
  203. _S_adjust(_M_impl, _M_impl._M_end_of_storage,
  204. _M_impl._M_finish);
  205. }
  206. _Vector_impl& _M_impl;
  207. #if __cplusplus >= 201103L
  208. _Reinit(const _Reinit&) = delete;
  209. _Reinit& operator=(const _Reinit&) = delete;
  210. #endif
  211. };
  212. // Tell ASan when unused capacity is initialized to be valid.
  213. struct _Grow
  214. {
  215. _GLIBCXX20_CONSTEXPR
  216. _Grow(_Vector_impl& __impl, size_type __n)
  217. : _M_impl(__impl), _M_n(__n)
  218. { _S_grow(_M_impl, __n); }
  219. _GLIBCXX20_CONSTEXPR
  220. ~_Grow() { if (_M_n) _S_shrink(_M_impl, _M_n); }
  221. _GLIBCXX20_CONSTEXPR
  222. void _M_grew(size_type __n) { _M_n -= __n; }
  223. #if __cplusplus >= 201103L
  224. _Grow(const _Grow&) = delete;
  225. _Grow& operator=(const _Grow&) = delete;
  226. #endif
  227. private:
  228. _Vector_impl& _M_impl;
  229. size_type _M_n;
  230. };
  231. };
  232. #define _GLIBCXX_ASAN_ANNOTATE_REINIT \
  233. typename _Base::_Vector_impl::template _Asan<>::_Reinit const \
  234. __attribute__((__unused__)) __reinit_guard(this->_M_impl)
  235. #define _GLIBCXX_ASAN_ANNOTATE_GROW(n) \
  236. typename _Base::_Vector_impl::template _Asan<>::_Grow \
  237. __attribute__((__unused__)) __grow_guard(this->_M_impl, (n))
  238. #define _GLIBCXX_ASAN_ANNOTATE_GREW(n) __grow_guard._M_grew(n)
  239. #define _GLIBCXX_ASAN_ANNOTATE_SHRINK(n) \
  240. _Base::_Vector_impl::template _Asan<>::_S_shrink(this->_M_impl, n)
  241. #define _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC \
  242. _Base::_Vector_impl::template _Asan<>::_S_on_dealloc(this->_M_impl)
  243. #else // ! (_GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR)
  244. #define _GLIBCXX_ASAN_ANNOTATE_REINIT
  245. #define _GLIBCXX_ASAN_ANNOTATE_GROW(n)
  246. #define _GLIBCXX_ASAN_ANNOTATE_GREW(n)
  247. #define _GLIBCXX_ASAN_ANNOTATE_SHRINK(n)
  248. #define _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC
  249. #endif // _GLIBCXX_SANITIZE_STD_ALLOCATOR && _GLIBCXX_SANITIZE_VECTOR
  250. };
  251. public:
  252. typedef _Alloc allocator_type;
  253. _GLIBCXX20_CONSTEXPR
  254. _Tp_alloc_type&
  255. _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
  256. { return this->_M_impl; }
  257. _GLIBCXX20_CONSTEXPR
  258. const _Tp_alloc_type&
  259. _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
  260. { return this->_M_impl; }
  261. _GLIBCXX20_CONSTEXPR
  262. allocator_type
  263. get_allocator() const _GLIBCXX_NOEXCEPT
  264. { return allocator_type(_M_get_Tp_allocator()); }
  265. #if __cplusplus >= 201103L
  266. _Vector_base() = default;
  267. #else
  268. _Vector_base() { }
  269. #endif
  270. _GLIBCXX20_CONSTEXPR
  271. _Vector_base(const allocator_type& __a) _GLIBCXX_NOEXCEPT
  272. : _M_impl(__a) { }
  273. // Kept for ABI compatibility.
  274. #if !_GLIBCXX_INLINE_VERSION
  275. _GLIBCXX20_CONSTEXPR
  276. _Vector_base(size_t __n)
  277. : _M_impl()
  278. { _M_create_storage(__n); }
  279. #endif
  280. _GLIBCXX20_CONSTEXPR
  281. _Vector_base(size_t __n, const allocator_type& __a)
  282. : _M_impl(__a)
  283. { _M_create_storage(__n); }
  284. #if __cplusplus >= 201103L
  285. _Vector_base(_Vector_base&&) = default;
  286. // Kept for ABI compatibility.
  287. # if !_GLIBCXX_INLINE_VERSION
  288. _GLIBCXX20_CONSTEXPR
  289. _Vector_base(_Tp_alloc_type&& __a) noexcept
  290. : _M_impl(std::move(__a)) { }
  291. _GLIBCXX20_CONSTEXPR
  292. _Vector_base(_Vector_base&& __x, const allocator_type& __a)
  293. : _M_impl(__a)
  294. {
  295. if (__x.get_allocator() == __a)
  296. this->_M_impl._M_swap_data(__x._M_impl);
  297. else
  298. {
  299. size_t __n = __x._M_impl._M_finish - __x._M_impl._M_start;
  300. _M_create_storage(__n);
  301. }
  302. }
  303. # endif
  304. _GLIBCXX20_CONSTEXPR
  305. _Vector_base(const allocator_type& __a, _Vector_base&& __x)
  306. : _M_impl(_Tp_alloc_type(__a), std::move(__x._M_impl))
  307. { }
  308. #endif
  309. _GLIBCXX20_CONSTEXPR
  310. ~_Vector_base() _GLIBCXX_NOEXCEPT
  311. {
  312. _M_deallocate(_M_impl._M_start,
  313. _M_impl._M_end_of_storage - _M_impl._M_start);
  314. }
  315. public:
  316. _Vector_impl _M_impl;
  317. _GLIBCXX20_CONSTEXPR
  318. pointer
  319. _M_allocate(size_t __n)
  320. {
  321. typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Tr;
  322. return __n != 0 ? _Tr::allocate(_M_impl, __n) : pointer();
  323. }
  324. _GLIBCXX20_CONSTEXPR
  325. void
  326. _M_deallocate(pointer __p, size_t __n)
  327. {
  328. typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Tr;
  329. if (__p)
  330. _Tr::deallocate(_M_impl, __p, __n);
  331. }
  332. protected:
  333. _GLIBCXX20_CONSTEXPR
  334. void
  335. _M_create_storage(size_t __n)
  336. {
  337. this->_M_impl._M_start = this->_M_allocate(__n);
  338. this->_M_impl._M_finish = this->_M_impl._M_start;
  339. this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
  340. }
  341. };
  342. /**
  343. * @brief A standard container which offers fixed time access to
  344. * individual elements in any order.
  345. *
  346. * @ingroup sequences
  347. *
  348. * @tparam _Tp Type of element.
  349. * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
  350. *
  351. * Meets the requirements of a <a href="tables.html#65">container</a>, a
  352. * <a href="tables.html#66">reversible container</a>, and a
  353. * <a href="tables.html#67">sequence</a>, including the
  354. * <a href="tables.html#68">optional sequence requirements</a> with the
  355. * %exception of @c push_front and @c pop_front.
  356. *
  357. * In some terminology a %vector can be described as a dynamic
  358. * C-style array, it offers fast and efficient access to individual
  359. * elements in any order and saves the user from worrying about
  360. * memory and size allocation. Subscripting ( @c [] ) access is
  361. * also provided as with C-style arrays.
  362. */
  363. template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
  364. class vector : protected _Vector_base<_Tp, _Alloc>
  365. {
  366. #ifdef _GLIBCXX_CONCEPT_CHECKS
  367. // Concept requirements.
  368. typedef typename _Alloc::value_type _Alloc_value_type;
  369. # if __cplusplus < 201103L
  370. __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
  371. # endif
  372. __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
  373. #endif
  374. #if __cplusplus >= 201103L
  375. static_assert(is_same<typename remove_cv<_Tp>::type, _Tp>::value,
  376. "std::vector must have a non-const, non-volatile value_type");
  377. # if __cplusplus > 201703L || defined __STRICT_ANSI__
  378. static_assert(is_same<typename _Alloc::value_type, _Tp>::value,
  379. "std::vector must have the same value_type as its allocator");
  380. # endif
  381. #endif
  382. typedef _Vector_base<_Tp, _Alloc> _Base;
  383. typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
  384. typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Alloc_traits;
  385. public:
  386. typedef _Tp value_type;
  387. typedef typename _Base::pointer pointer;
  388. typedef typename _Alloc_traits::const_pointer const_pointer;
  389. typedef typename _Alloc_traits::reference reference;
  390. typedef typename _Alloc_traits::const_reference const_reference;
  391. typedef __gnu_cxx::__normal_iterator<pointer, vector> iterator;
  392. typedef __gnu_cxx::__normal_iterator<const_pointer, vector>
  393. const_iterator;
  394. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  395. typedef std::reverse_iterator<iterator> reverse_iterator;
  396. typedef size_t size_type;
  397. typedef ptrdiff_t difference_type;
  398. typedef _Alloc allocator_type;
  399. private:
  400. #if __cplusplus >= 201103L
  401. static constexpr bool
  402. _S_nothrow_relocate(true_type)
  403. {
  404. return noexcept(std::__relocate_a(std::declval<pointer>(),
  405. std::declval<pointer>(),
  406. std::declval<pointer>(),
  407. std::declval<_Tp_alloc_type&>()));
  408. }
  409. static constexpr bool
  410. _S_nothrow_relocate(false_type)
  411. { return false; }
  412. static constexpr bool
  413. _S_use_relocate()
  414. {
  415. // Instantiating std::__relocate_a might cause an error outside the
  416. // immediate context (in __relocate_object_a's noexcept-specifier),
  417. // so only do it if we know the type can be move-inserted into *this.
  418. return _S_nothrow_relocate(__is_move_insertable<_Tp_alloc_type>{});
  419. }
  420. static pointer
  421. _S_do_relocate(pointer __first, pointer __last, pointer __result,
  422. _Tp_alloc_type& __alloc, true_type) noexcept
  423. {
  424. return std::__relocate_a(__first, __last, __result, __alloc);
  425. }
  426. static pointer
  427. _S_do_relocate(pointer, pointer, pointer __result,
  428. _Tp_alloc_type&, false_type) noexcept
  429. { return __result; }
  430. static _GLIBCXX20_CONSTEXPR pointer
  431. _S_relocate(pointer __first, pointer __last, pointer __result,
  432. _Tp_alloc_type& __alloc) noexcept
  433. {
  434. #if __cpp_if_constexpr
  435. // All callers have already checked _S_use_relocate() so just do it.
  436. return std::__relocate_a(__first, __last, __result, __alloc);
  437. #else
  438. using __do_it = __bool_constant<_S_use_relocate()>;
  439. return _S_do_relocate(__first, __last, __result, __alloc, __do_it{});
  440. #endif
  441. }
  442. #endif // C++11
  443. protected:
  444. using _Base::_M_allocate;
  445. using _Base::_M_deallocate;
  446. using _Base::_M_impl;
  447. using _Base::_M_get_Tp_allocator;
  448. public:
  449. // [23.2.4.1] construct/copy/destroy
  450. // (assign() and get_allocator() are also listed in this section)
  451. /**
  452. * @brief Creates a %vector with no elements.
  453. */
  454. #if __cplusplus >= 201103L
  455. vector() = default;
  456. #else
  457. vector() { }
  458. #endif
  459. /**
  460. * @brief Creates a %vector with no elements.
  461. * @param __a An allocator object.
  462. */
  463. explicit
  464. _GLIBCXX20_CONSTEXPR
  465. vector(const allocator_type& __a) _GLIBCXX_NOEXCEPT
  466. : _Base(__a) { }
  467. #if __cplusplus >= 201103L
  468. /**
  469. * @brief Creates a %vector with default constructed elements.
  470. * @param __n The number of elements to initially create.
  471. * @param __a An allocator.
  472. *
  473. * This constructor fills the %vector with @a __n default
  474. * constructed elements.
  475. */
  476. explicit
  477. _GLIBCXX20_CONSTEXPR
  478. vector(size_type __n, const allocator_type& __a = allocator_type())
  479. : _Base(_S_check_init_len(__n, __a), __a)
  480. { _M_default_initialize(__n); }
  481. /**
  482. * @brief Creates a %vector with copies of an exemplar element.
  483. * @param __n The number of elements to initially create.
  484. * @param __value An element to copy.
  485. * @param __a An allocator.
  486. *
  487. * This constructor fills the %vector with @a __n copies of @a __value.
  488. */
  489. _GLIBCXX20_CONSTEXPR
  490. vector(size_type __n, const value_type& __value,
  491. const allocator_type& __a = allocator_type())
  492. : _Base(_S_check_init_len(__n, __a), __a)
  493. { _M_fill_initialize(__n, __value); }
  494. #else
  495. /**
  496. * @brief Creates a %vector with copies of an exemplar element.
  497. * @param __n The number of elements to initially create.
  498. * @param __value An element to copy.
  499. * @param __a An allocator.
  500. *
  501. * This constructor fills the %vector with @a __n copies of @a __value.
  502. */
  503. explicit
  504. vector(size_type __n, const value_type& __value = value_type(),
  505. const allocator_type& __a = allocator_type())
  506. : _Base(_S_check_init_len(__n, __a), __a)
  507. { _M_fill_initialize(__n, __value); }
  508. #endif
  509. /**
  510. * @brief %Vector copy constructor.
  511. * @param __x A %vector of identical element and allocator types.
  512. *
  513. * All the elements of @a __x are copied, but any unused capacity in
  514. * @a __x will not be copied
  515. * (i.e. capacity() == size() in the new %vector).
  516. *
  517. * The newly-created %vector uses a copy of the allocator object used
  518. * by @a __x (unless the allocator traits dictate a different object).
  519. */
  520. _GLIBCXX20_CONSTEXPR
  521. vector(const vector& __x)
  522. : _Base(__x.size(),
  523. _Alloc_traits::_S_select_on_copy(__x._M_get_Tp_allocator()))
  524. {
  525. this->_M_impl._M_finish =
  526. std::__uninitialized_copy_a(__x.begin(), __x.end(),
  527. this->_M_impl._M_start,
  528. _M_get_Tp_allocator());
  529. }
  530. #if __cplusplus >= 201103L
  531. /**
  532. * @brief %Vector move constructor.
  533. *
  534. * The newly-created %vector contains the exact contents of the
  535. * moved instance.
  536. * The contents of the moved instance are a valid, but unspecified
  537. * %vector.
  538. */
  539. vector(vector&&) noexcept = default;
  540. /// Copy constructor with alternative allocator
  541. _GLIBCXX20_CONSTEXPR
  542. vector(const vector& __x, const __type_identity_t<allocator_type>& __a)
  543. : _Base(__x.size(), __a)
  544. {
  545. this->_M_impl._M_finish =
  546. std::__uninitialized_copy_a(__x.begin(), __x.end(),
  547. this->_M_impl._M_start,
  548. _M_get_Tp_allocator());
  549. }
  550. private:
  551. _GLIBCXX20_CONSTEXPR
  552. vector(vector&& __rv, const allocator_type& __m, true_type) noexcept
  553. : _Base(__m, std::move(__rv))
  554. { }
  555. _GLIBCXX20_CONSTEXPR
  556. vector(vector&& __rv, const allocator_type& __m, false_type)
  557. : _Base(__m)
  558. {
  559. if (__rv.get_allocator() == __m)
  560. this->_M_impl._M_swap_data(__rv._M_impl);
  561. else if (!__rv.empty())
  562. {
  563. this->_M_create_storage(__rv.size());
  564. this->_M_impl._M_finish =
  565. std::__uninitialized_move_a(__rv.begin(), __rv.end(),
  566. this->_M_impl._M_start,
  567. _M_get_Tp_allocator());
  568. __rv.clear();
  569. }
  570. }
  571. public:
  572. /// Move constructor with alternative allocator
  573. _GLIBCXX20_CONSTEXPR
  574. vector(vector&& __rv, const __type_identity_t<allocator_type>& __m)
  575. noexcept( noexcept(
  576. vector(std::declval<vector&&>(), std::declval<const allocator_type&>(),
  577. std::declval<typename _Alloc_traits::is_always_equal>())) )
  578. : vector(std::move(__rv), __m, typename _Alloc_traits::is_always_equal{})
  579. { }
  580. /**
  581. * @brief Builds a %vector from an initializer list.
  582. * @param __l An initializer_list.
  583. * @param __a An allocator.
  584. *
  585. * Create a %vector consisting of copies of the elements in the
  586. * initializer_list @a __l.
  587. *
  588. * This will call the element type's copy constructor N times
  589. * (where N is @a __l.size()) and do no memory reallocation.
  590. */
  591. _GLIBCXX20_CONSTEXPR
  592. vector(initializer_list<value_type> __l,
  593. const allocator_type& __a = allocator_type())
  594. : _Base(__a)
  595. {
  596. _M_range_initialize(__l.begin(), __l.end(),
  597. random_access_iterator_tag());
  598. }
  599. #endif
  600. /**
  601. * @brief Builds a %vector from a range.
  602. * @param __first An input iterator.
  603. * @param __last An input iterator.
  604. * @param __a An allocator.
  605. *
  606. * Create a %vector consisting of copies of the elements from
  607. * [first,last).
  608. *
  609. * If the iterators are forward, bidirectional, or
  610. * random-access, then this will call the elements' copy
  611. * constructor N times (where N is distance(first,last)) and do
  612. * no memory reallocation. But if only input iterators are
  613. * used, then this will do at most 2N calls to the copy
  614. * constructor, and logN memory reallocations.
  615. */
  616. #if __cplusplus >= 201103L
  617. template<typename _InputIterator,
  618. typename = std::_RequireInputIter<_InputIterator>>
  619. _GLIBCXX20_CONSTEXPR
  620. vector(_InputIterator __first, _InputIterator __last,
  621. const allocator_type& __a = allocator_type())
  622. : _Base(__a)
  623. {
  624. _M_range_initialize(__first, __last,
  625. std::__iterator_category(__first));
  626. }
  627. #else
  628. template<typename _InputIterator>
  629. vector(_InputIterator __first, _InputIterator __last,
  630. const allocator_type& __a = allocator_type())
  631. : _Base(__a)
  632. {
  633. // Check whether it's an integral type. If so, it's not an iterator.
  634. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  635. _M_initialize_dispatch(__first, __last, _Integral());
  636. }
  637. #endif
  638. /**
  639. * The dtor only erases the elements, and note that if the
  640. * elements themselves are pointers, the pointed-to memory is
  641. * not touched in any way. Managing the pointer is the user's
  642. * responsibility.
  643. */
  644. _GLIBCXX20_CONSTEXPR
  645. ~vector() _GLIBCXX_NOEXCEPT
  646. {
  647. std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
  648. _M_get_Tp_allocator());
  649. _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC;
  650. }
  651. /**
  652. * @brief %Vector assignment operator.
  653. * @param __x A %vector of identical element and allocator types.
  654. *
  655. * All the elements of @a __x are copied, but any unused capacity in
  656. * @a __x will not be copied.
  657. *
  658. * Whether the allocator is copied depends on the allocator traits.
  659. */
  660. _GLIBCXX20_CONSTEXPR
  661. vector&
  662. operator=(const vector& __x);
  663. #if __cplusplus >= 201103L
  664. /**
  665. * @brief %Vector move assignment operator.
  666. * @param __x A %vector of identical element and allocator types.
  667. *
  668. * The contents of @a __x are moved into this %vector (without copying,
  669. * if the allocators permit it).
  670. * Afterwards @a __x is a valid, but unspecified %vector.
  671. *
  672. * Whether the allocator is moved depends on the allocator traits.
  673. */
  674. _GLIBCXX20_CONSTEXPR
  675. vector&
  676. operator=(vector&& __x) noexcept(_Alloc_traits::_S_nothrow_move())
  677. {
  678. constexpr bool __move_storage =
  679. _Alloc_traits::_S_propagate_on_move_assign()
  680. || _Alloc_traits::_S_always_equal();
  681. _M_move_assign(std::move(__x), __bool_constant<__move_storage>());
  682. return *this;
  683. }
  684. /**
  685. * @brief %Vector list assignment operator.
  686. * @param __l An initializer_list.
  687. *
  688. * This function fills a %vector with copies of the elements in the
  689. * initializer list @a __l.
  690. *
  691. * Note that the assignment completely changes the %vector and
  692. * that the resulting %vector's size is the same as the number
  693. * of elements assigned.
  694. */
  695. _GLIBCXX20_CONSTEXPR
  696. vector&
  697. operator=(initializer_list<value_type> __l)
  698. {
  699. this->_M_assign_aux(__l.begin(), __l.end(),
  700. random_access_iterator_tag());
  701. return *this;
  702. }
  703. #endif
  704. /**
  705. * @brief Assigns a given value to a %vector.
  706. * @param __n Number of elements to be assigned.
  707. * @param __val Value to be assigned.
  708. *
  709. * This function fills a %vector with @a __n copies of the given
  710. * value. Note that the assignment completely changes the
  711. * %vector and that the resulting %vector's size is the same as
  712. * the number of elements assigned.
  713. */
  714. _GLIBCXX20_CONSTEXPR
  715. void
  716. assign(size_type __n, const value_type& __val)
  717. { _M_fill_assign(__n, __val); }
  718. /**
  719. * @brief Assigns a range to a %vector.
  720. * @param __first An input iterator.
  721. * @param __last An input iterator.
  722. *
  723. * This function fills a %vector with copies of the elements in the
  724. * range [__first,__last).
  725. *
  726. * Note that the assignment completely changes the %vector and
  727. * that the resulting %vector's size is the same as the number
  728. * of elements assigned.
  729. */
  730. #if __cplusplus >= 201103L
  731. template<typename _InputIterator,
  732. typename = std::_RequireInputIter<_InputIterator>>
  733. _GLIBCXX20_CONSTEXPR
  734. void
  735. assign(_InputIterator __first, _InputIterator __last)
  736. { _M_assign_dispatch(__first, __last, __false_type()); }
  737. #else
  738. template<typename _InputIterator>
  739. void
  740. assign(_InputIterator __first, _InputIterator __last)
  741. {
  742. // Check whether it's an integral type. If so, it's not an iterator.
  743. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  744. _M_assign_dispatch(__first, __last, _Integral());
  745. }
  746. #endif
  747. #if __cplusplus >= 201103L
  748. /**
  749. * @brief Assigns an initializer list to a %vector.
  750. * @param __l An initializer_list.
  751. *
  752. * This function fills a %vector with copies of the elements in the
  753. * initializer list @a __l.
  754. *
  755. * Note that the assignment completely changes the %vector and
  756. * that the resulting %vector's size is the same as the number
  757. * of elements assigned.
  758. */
  759. _GLIBCXX20_CONSTEXPR
  760. void
  761. assign(initializer_list<value_type> __l)
  762. {
  763. this->_M_assign_aux(__l.begin(), __l.end(),
  764. random_access_iterator_tag());
  765. }
  766. #endif
  767. /// Get a copy of the memory allocation object.
  768. using _Base::get_allocator;
  769. // iterators
  770. /**
  771. * Returns a read/write iterator that points to the first
  772. * element in the %vector. Iteration is done in ordinary
  773. * element order.
  774. */
  775. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  776. iterator
  777. begin() _GLIBCXX_NOEXCEPT
  778. { return iterator(this->_M_impl._M_start); }
  779. /**
  780. * Returns a read-only (constant) iterator that points to the
  781. * first element in the %vector. Iteration is done in ordinary
  782. * element order.
  783. */
  784. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  785. const_iterator
  786. begin() const _GLIBCXX_NOEXCEPT
  787. { return const_iterator(this->_M_impl._M_start); }
  788. /**
  789. * Returns a read/write iterator that points one past the last
  790. * element in the %vector. Iteration is done in ordinary
  791. * element order.
  792. */
  793. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  794. iterator
  795. end() _GLIBCXX_NOEXCEPT
  796. { return iterator(this->_M_impl._M_finish); }
  797. /**
  798. * Returns a read-only (constant) iterator that points one past
  799. * the last element in the %vector. Iteration is done in
  800. * ordinary element order.
  801. */
  802. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  803. const_iterator
  804. end() const _GLIBCXX_NOEXCEPT
  805. { return const_iterator(this->_M_impl._M_finish); }
  806. /**
  807. * Returns a read/write reverse iterator that points to the
  808. * last element in the %vector. Iteration is done in reverse
  809. * element order.
  810. */
  811. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  812. reverse_iterator
  813. rbegin() _GLIBCXX_NOEXCEPT
  814. { return reverse_iterator(end()); }
  815. /**
  816. * Returns a read-only (constant) reverse iterator that points
  817. * to the last element in the %vector. Iteration is done in
  818. * reverse element order.
  819. */
  820. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  821. const_reverse_iterator
  822. rbegin() const _GLIBCXX_NOEXCEPT
  823. { return const_reverse_iterator(end()); }
  824. /**
  825. * Returns a read/write reverse iterator that points to one
  826. * before the first element in the %vector. Iteration is done
  827. * in reverse element order.
  828. */
  829. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  830. reverse_iterator
  831. rend() _GLIBCXX_NOEXCEPT
  832. { return reverse_iterator(begin()); }
  833. /**
  834. * Returns a read-only (constant) reverse iterator that points
  835. * to one before the first element in the %vector. Iteration
  836. * is done in reverse element order.
  837. */
  838. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  839. const_reverse_iterator
  840. rend() const _GLIBCXX_NOEXCEPT
  841. { return const_reverse_iterator(begin()); }
  842. #if __cplusplus >= 201103L
  843. /**
  844. * Returns a read-only (constant) iterator that points to the
  845. * first element in the %vector. Iteration is done in ordinary
  846. * element order.
  847. */
  848. [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
  849. const_iterator
  850. cbegin() const noexcept
  851. { return const_iterator(this->_M_impl._M_start); }
  852. /**
  853. * Returns a read-only (constant) iterator that points one past
  854. * the last element in the %vector. Iteration is done in
  855. * ordinary element order.
  856. */
  857. [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
  858. const_iterator
  859. cend() const noexcept
  860. { return const_iterator(this->_M_impl._M_finish); }
  861. /**
  862. * Returns a read-only (constant) reverse iterator that points
  863. * to the last element in the %vector. Iteration is done in
  864. * reverse element order.
  865. */
  866. [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
  867. const_reverse_iterator
  868. crbegin() const noexcept
  869. { return const_reverse_iterator(end()); }
  870. /**
  871. * Returns a read-only (constant) reverse iterator that points
  872. * to one before the first element in the %vector. Iteration
  873. * is done in reverse element order.
  874. */
  875. [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
  876. const_reverse_iterator
  877. crend() const noexcept
  878. { return const_reverse_iterator(begin()); }
  879. #endif
  880. // [23.2.4.2] capacity
  881. /** Returns the number of elements in the %vector. */
  882. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  883. size_type
  884. size() const _GLIBCXX_NOEXCEPT
  885. { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
  886. /** Returns the size() of the largest possible %vector. */
  887. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  888. size_type
  889. max_size() const _GLIBCXX_NOEXCEPT
  890. { return _S_max_size(_M_get_Tp_allocator()); }
  891. #if __cplusplus >= 201103L
  892. /**
  893. * @brief Resizes the %vector to the specified number of elements.
  894. * @param __new_size Number of elements the %vector should contain.
  895. *
  896. * This function will %resize the %vector to the specified
  897. * number of elements. If the number is smaller than the
  898. * %vector's current size the %vector is truncated, otherwise
  899. * default constructed elements are appended.
  900. */
  901. _GLIBCXX20_CONSTEXPR
  902. void
  903. resize(size_type __new_size)
  904. {
  905. if (__new_size > size())
  906. _M_default_append(__new_size - size());
  907. else if (__new_size < size())
  908. _M_erase_at_end(this->_M_impl._M_start + __new_size);
  909. }
  910. /**
  911. * @brief Resizes the %vector to the specified number of elements.
  912. * @param __new_size Number of elements the %vector should contain.
  913. * @param __x Data with which new elements should be populated.
  914. *
  915. * This function will %resize the %vector to the specified
  916. * number of elements. If the number is smaller than the
  917. * %vector's current size the %vector is truncated, otherwise
  918. * the %vector is extended and new elements are populated with
  919. * given data.
  920. */
  921. _GLIBCXX20_CONSTEXPR
  922. void
  923. resize(size_type __new_size, const value_type& __x)
  924. {
  925. if (__new_size > size())
  926. _M_fill_insert(end(), __new_size - size(), __x);
  927. else if (__new_size < size())
  928. _M_erase_at_end(this->_M_impl._M_start + __new_size);
  929. }
  930. #else
  931. /**
  932. * @brief Resizes the %vector to the specified number of elements.
  933. * @param __new_size Number of elements the %vector should contain.
  934. * @param __x Data with which new elements should be populated.
  935. *
  936. * This function will %resize the %vector to the specified
  937. * number of elements. If the number is smaller than the
  938. * %vector's current size the %vector is truncated, otherwise
  939. * the %vector is extended and new elements are populated with
  940. * given data.
  941. */
  942. _GLIBCXX20_CONSTEXPR
  943. void
  944. resize(size_type __new_size, value_type __x = value_type())
  945. {
  946. if (__new_size > size())
  947. _M_fill_insert(end(), __new_size - size(), __x);
  948. else if (__new_size < size())
  949. _M_erase_at_end(this->_M_impl._M_start + __new_size);
  950. }
  951. #endif
  952. #if __cplusplus >= 201103L
  953. /** A non-binding request to reduce capacity() to size(). */
  954. _GLIBCXX20_CONSTEXPR
  955. void
  956. shrink_to_fit()
  957. { _M_shrink_to_fit(); }
  958. #endif
  959. /**
  960. * Returns the total number of elements that the %vector can
  961. * hold before needing to allocate more memory.
  962. */
  963. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  964. size_type
  965. capacity() const _GLIBCXX_NOEXCEPT
  966. { return size_type(this->_M_impl._M_end_of_storage
  967. - this->_M_impl._M_start); }
  968. /**
  969. * Returns true if the %vector is empty. (Thus begin() would
  970. * equal end().)
  971. */
  972. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  973. bool
  974. empty() const _GLIBCXX_NOEXCEPT
  975. { return begin() == end(); }
  976. /**
  977. * @brief Attempt to preallocate enough memory for specified number of
  978. * elements.
  979. * @param __n Number of elements required.
  980. * @throw std::length_error If @a n exceeds @c max_size().
  981. *
  982. * This function attempts to reserve enough memory for the
  983. * %vector to hold the specified number of elements. If the
  984. * number requested is more than max_size(), length_error is
  985. * thrown.
  986. *
  987. * The advantage of this function is that if optimal code is a
  988. * necessity and the user can determine the number of elements
  989. * that will be required, the user can reserve the memory in
  990. * %advance, and thus prevent a possible reallocation of memory
  991. * and copying of %vector data.
  992. */
  993. _GLIBCXX20_CONSTEXPR
  994. void
  995. reserve(size_type __n);
  996. // element access
  997. /**
  998. * @brief Subscript access to the data contained in the %vector.
  999. * @param __n The index of the element for which data should be
  1000. * accessed.
  1001. * @return Read/write reference to data.
  1002. *
  1003. * This operator allows for easy, array-style, data access.
  1004. * Note that data access with this operator is unchecked and
  1005. * out_of_range lookups are not defined. (For checked lookups
  1006. * see at().)
  1007. */
  1008. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  1009. reference
  1010. operator[](size_type __n) _GLIBCXX_NOEXCEPT
  1011. {
  1012. __glibcxx_requires_subscript(__n);
  1013. return *(this->_M_impl._M_start + __n);
  1014. }
  1015. /**
  1016. * @brief Subscript access to the data contained in the %vector.
  1017. * @param __n The index of the element for which data should be
  1018. * accessed.
  1019. * @return Read-only (constant) reference to data.
  1020. *
  1021. * This operator allows for easy, array-style, data access.
  1022. * Note that data access with this operator is unchecked and
  1023. * out_of_range lookups are not defined. (For checked lookups
  1024. * see at().)
  1025. */
  1026. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  1027. const_reference
  1028. operator[](size_type __n) const _GLIBCXX_NOEXCEPT
  1029. {
  1030. __glibcxx_requires_subscript(__n);
  1031. return *(this->_M_impl._M_start + __n);
  1032. }
  1033. protected:
  1034. /// Safety check used only from at().
  1035. _GLIBCXX20_CONSTEXPR
  1036. void
  1037. _M_range_check(size_type __n) const
  1038. {
  1039. if (__n >= this->size())
  1040. __throw_out_of_range_fmt(__N("vector::_M_range_check: __n "
  1041. "(which is %zu) >= this->size() "
  1042. "(which is %zu)"),
  1043. __n, this->size());
  1044. }
  1045. public:
  1046. /**
  1047. * @brief Provides access to the data contained in the %vector.
  1048. * @param __n The index of the element for which data should be
  1049. * accessed.
  1050. * @return Read/write reference to data.
  1051. * @throw std::out_of_range If @a __n is an invalid index.
  1052. *
  1053. * This function provides for safer data access. The parameter
  1054. * is first checked that it is in the range of the vector. The
  1055. * function throws out_of_range if the check fails.
  1056. */
  1057. _GLIBCXX20_CONSTEXPR
  1058. reference
  1059. at(size_type __n)
  1060. {
  1061. _M_range_check(__n);
  1062. return (*this)[__n];
  1063. }
  1064. /**
  1065. * @brief Provides access to the data contained in the %vector.
  1066. * @param __n The index of the element for which data should be
  1067. * accessed.
  1068. * @return Read-only (constant) reference to data.
  1069. * @throw std::out_of_range If @a __n is an invalid index.
  1070. *
  1071. * This function provides for safer data access. The parameter
  1072. * is first checked that it is in the range of the vector. The
  1073. * function throws out_of_range if the check fails.
  1074. */
  1075. _GLIBCXX20_CONSTEXPR
  1076. const_reference
  1077. at(size_type __n) const
  1078. {
  1079. _M_range_check(__n);
  1080. return (*this)[__n];
  1081. }
  1082. /**
  1083. * Returns a read/write reference to the data at the first
  1084. * element of the %vector.
  1085. */
  1086. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  1087. reference
  1088. front() _GLIBCXX_NOEXCEPT
  1089. {
  1090. __glibcxx_requires_nonempty();
  1091. return *begin();
  1092. }
  1093. /**
  1094. * Returns a read-only (constant) reference to the data at the first
  1095. * element of the %vector.
  1096. */
  1097. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  1098. const_reference
  1099. front() const _GLIBCXX_NOEXCEPT
  1100. {
  1101. __glibcxx_requires_nonempty();
  1102. return *begin();
  1103. }
  1104. /**
  1105. * Returns a read/write reference to the data at the last
  1106. * element of the %vector.
  1107. */
  1108. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  1109. reference
  1110. back() _GLIBCXX_NOEXCEPT
  1111. {
  1112. __glibcxx_requires_nonempty();
  1113. return *(end() - 1);
  1114. }
  1115. /**
  1116. * Returns a read-only (constant) reference to the data at the
  1117. * last element of the %vector.
  1118. */
  1119. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  1120. const_reference
  1121. back() const _GLIBCXX_NOEXCEPT
  1122. {
  1123. __glibcxx_requires_nonempty();
  1124. return *(end() - 1);
  1125. }
  1126. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1127. // DR 464. Suggestion for new member functions in standard containers.
  1128. // data access
  1129. /**
  1130. * Returns a pointer such that [data(), data() + size()) is a valid
  1131. * range. For a non-empty %vector, data() == &front().
  1132. */
  1133. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  1134. _Tp*
  1135. data() _GLIBCXX_NOEXCEPT
  1136. { return _M_data_ptr(this->_M_impl._M_start); }
  1137. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  1138. const _Tp*
  1139. data() const _GLIBCXX_NOEXCEPT
  1140. { return _M_data_ptr(this->_M_impl._M_start); }
  1141. // [23.2.4.3] modifiers
  1142. /**
  1143. * @brief Add data to the end of the %vector.
  1144. * @param __x Data to be added.
  1145. *
  1146. * This is a typical stack operation. The function creates an
  1147. * element at the end of the %vector and assigns the given data
  1148. * to it. Due to the nature of a %vector this operation can be
  1149. * done in constant time if the %vector has preallocated space
  1150. * available.
  1151. */
  1152. _GLIBCXX20_CONSTEXPR
  1153. void
  1154. push_back(const value_type& __x)
  1155. {
  1156. if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
  1157. {
  1158. _GLIBCXX_ASAN_ANNOTATE_GROW(1);
  1159. _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
  1160. __x);
  1161. ++this->_M_impl._M_finish;
  1162. _GLIBCXX_ASAN_ANNOTATE_GREW(1);
  1163. }
  1164. else
  1165. _M_realloc_insert(end(), __x);
  1166. }
  1167. #if __cplusplus >= 201103L
  1168. _GLIBCXX20_CONSTEXPR
  1169. void
  1170. push_back(value_type&& __x)
  1171. { emplace_back(std::move(__x)); }
  1172. template<typename... _Args>
  1173. #if __cplusplus > 201402L
  1174. _GLIBCXX20_CONSTEXPR
  1175. reference
  1176. #else
  1177. void
  1178. #endif
  1179. emplace_back(_Args&&... __args);
  1180. #endif
  1181. /**
  1182. * @brief Removes last element.
  1183. *
  1184. * This is a typical stack operation. It shrinks the %vector by one.
  1185. *
  1186. * Note that no data is returned, and if the last element's
  1187. * data is needed, it should be retrieved before pop_back() is
  1188. * called.
  1189. */
  1190. _GLIBCXX20_CONSTEXPR
  1191. void
  1192. pop_back() _GLIBCXX_NOEXCEPT
  1193. {
  1194. __glibcxx_requires_nonempty();
  1195. --this->_M_impl._M_finish;
  1196. _Alloc_traits::destroy(this->_M_impl, this->_M_impl._M_finish);
  1197. _GLIBCXX_ASAN_ANNOTATE_SHRINK(1);
  1198. }
  1199. #if __cplusplus >= 201103L
  1200. /**
  1201. * @brief Inserts an object in %vector before specified iterator.
  1202. * @param __position A const_iterator into the %vector.
  1203. * @param __args Arguments.
  1204. * @return An iterator that points to the inserted data.
  1205. *
  1206. * This function will insert an object of type T constructed
  1207. * with T(std::forward<Args>(args)...) before the specified location.
  1208. * Note that this kind of operation could be expensive for a %vector
  1209. * and if it is frequently used the user should consider using
  1210. * std::list.
  1211. */
  1212. template<typename... _Args>
  1213. _GLIBCXX20_CONSTEXPR
  1214. iterator
  1215. emplace(const_iterator __position, _Args&&... __args)
  1216. { return _M_emplace_aux(__position, std::forward<_Args>(__args)...); }
  1217. /**
  1218. * @brief Inserts given value into %vector before specified iterator.
  1219. * @param __position A const_iterator into the %vector.
  1220. * @param __x Data to be inserted.
  1221. * @return An iterator that points to the inserted data.
  1222. *
  1223. * This function will insert a copy of the given value before
  1224. * the specified location. Note that this kind of operation
  1225. * could be expensive for a %vector and if it is frequently
  1226. * used the user should consider using std::list.
  1227. */
  1228. _GLIBCXX20_CONSTEXPR
  1229. iterator
  1230. insert(const_iterator __position, const value_type& __x);
  1231. #else
  1232. /**
  1233. * @brief Inserts given value into %vector before specified iterator.
  1234. * @param __position An iterator into the %vector.
  1235. * @param __x Data to be inserted.
  1236. * @return An iterator that points to the inserted data.
  1237. *
  1238. * This function will insert a copy of the given value before
  1239. * the specified location. Note that this kind of operation
  1240. * could be expensive for a %vector and if it is frequently
  1241. * used the user should consider using std::list.
  1242. */
  1243. iterator
  1244. insert(iterator __position, const value_type& __x);
  1245. #endif
  1246. #if __cplusplus >= 201103L
  1247. /**
  1248. * @brief Inserts given rvalue into %vector before specified iterator.
  1249. * @param __position A const_iterator into the %vector.
  1250. * @param __x Data to be inserted.
  1251. * @return An iterator that points to the inserted data.
  1252. *
  1253. * This function will insert a copy of the given rvalue before
  1254. * the specified location. Note that this kind of operation
  1255. * could be expensive for a %vector and if it is frequently
  1256. * used the user should consider using std::list.
  1257. */
  1258. _GLIBCXX20_CONSTEXPR
  1259. iterator
  1260. insert(const_iterator __position, value_type&& __x)
  1261. { return _M_insert_rval(__position, std::move(__x)); }
  1262. /**
  1263. * @brief Inserts an initializer_list into the %vector.
  1264. * @param __position An iterator into the %vector.
  1265. * @param __l An initializer_list.
  1266. *
  1267. * This function will insert copies of the data in the
  1268. * initializer_list @a l into the %vector before the location
  1269. * specified by @a position.
  1270. *
  1271. * Note that this kind of operation could be expensive for a
  1272. * %vector and if it is frequently used the user should
  1273. * consider using std::list.
  1274. */
  1275. _GLIBCXX20_CONSTEXPR
  1276. iterator
  1277. insert(const_iterator __position, initializer_list<value_type> __l)
  1278. {
  1279. auto __offset = __position - cbegin();
  1280. _M_range_insert(begin() + __offset, __l.begin(), __l.end(),
  1281. std::random_access_iterator_tag());
  1282. return begin() + __offset;
  1283. }
  1284. #endif
  1285. #if __cplusplus >= 201103L
  1286. /**
  1287. * @brief Inserts a number of copies of given data into the %vector.
  1288. * @param __position A const_iterator into the %vector.
  1289. * @param __n Number of elements to be inserted.
  1290. * @param __x Data to be inserted.
  1291. * @return An iterator that points to the inserted data.
  1292. *
  1293. * This function will insert a specified number of copies of
  1294. * the given data before the location specified by @a position.
  1295. *
  1296. * Note that this kind of operation could be expensive for a
  1297. * %vector and if it is frequently used the user should
  1298. * consider using std::list.
  1299. */
  1300. _GLIBCXX20_CONSTEXPR
  1301. iterator
  1302. insert(const_iterator __position, size_type __n, const value_type& __x)
  1303. {
  1304. difference_type __offset = __position - cbegin();
  1305. _M_fill_insert(begin() + __offset, __n, __x);
  1306. return begin() + __offset;
  1307. }
  1308. #else
  1309. /**
  1310. * @brief Inserts a number of copies of given data into the %vector.
  1311. * @param __position An iterator into the %vector.
  1312. * @param __n Number of elements to be inserted.
  1313. * @param __x Data to be inserted.
  1314. *
  1315. * This function will insert a specified number of copies of
  1316. * the given data before the location specified by @a position.
  1317. *
  1318. * Note that this kind of operation could be expensive for a
  1319. * %vector and if it is frequently used the user should
  1320. * consider using std::list.
  1321. */
  1322. void
  1323. insert(iterator __position, size_type __n, const value_type& __x)
  1324. { _M_fill_insert(__position, __n, __x); }
  1325. #endif
  1326. #if __cplusplus >= 201103L
  1327. /**
  1328. * @brief Inserts a range into the %vector.
  1329. * @param __position A const_iterator into the %vector.
  1330. * @param __first An input iterator.
  1331. * @param __last An input iterator.
  1332. * @return An iterator that points to the inserted data.
  1333. *
  1334. * This function will insert copies of the data in the range
  1335. * [__first,__last) into the %vector before the location specified
  1336. * by @a pos.
  1337. *
  1338. * Note that this kind of operation could be expensive for a
  1339. * %vector and if it is frequently used the user should
  1340. * consider using std::list.
  1341. */
  1342. template<typename _InputIterator,
  1343. typename = std::_RequireInputIter<_InputIterator>>
  1344. _GLIBCXX20_CONSTEXPR
  1345. iterator
  1346. insert(const_iterator __position, _InputIterator __first,
  1347. _InputIterator __last)
  1348. {
  1349. difference_type __offset = __position - cbegin();
  1350. _M_insert_dispatch(begin() + __offset,
  1351. __first, __last, __false_type());
  1352. return begin() + __offset;
  1353. }
  1354. #else
  1355. /**
  1356. * @brief Inserts a range into the %vector.
  1357. * @param __position An iterator into the %vector.
  1358. * @param __first An input iterator.
  1359. * @param __last An input iterator.
  1360. *
  1361. * This function will insert copies of the data in the range
  1362. * [__first,__last) into the %vector before the location specified
  1363. * by @a pos.
  1364. *
  1365. * Note that this kind of operation could be expensive for a
  1366. * %vector and if it is frequently used the user should
  1367. * consider using std::list.
  1368. */
  1369. template<typename _InputIterator>
  1370. void
  1371. insert(iterator __position, _InputIterator __first,
  1372. _InputIterator __last)
  1373. {
  1374. // Check whether it's an integral type. If so, it's not an iterator.
  1375. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  1376. _M_insert_dispatch(__position, __first, __last, _Integral());
  1377. }
  1378. #endif
  1379. /**
  1380. * @brief Remove element at given position.
  1381. * @param __position Iterator pointing to element to be erased.
  1382. * @return An iterator pointing to the next element (or end()).
  1383. *
  1384. * This function will erase the element at the given position and thus
  1385. * shorten the %vector by one.
  1386. *
  1387. * Note This operation could be expensive and if it is
  1388. * frequently used the user should consider using std::list.
  1389. * The user is also cautioned that this function only erases
  1390. * the element, and that if the element is itself a pointer,
  1391. * the pointed-to memory is not touched in any way. Managing
  1392. * the pointer is the user's responsibility.
  1393. */
  1394. _GLIBCXX20_CONSTEXPR
  1395. iterator
  1396. #if __cplusplus >= 201103L
  1397. erase(const_iterator __position)
  1398. { return _M_erase(begin() + (__position - cbegin())); }
  1399. #else
  1400. erase(iterator __position)
  1401. { return _M_erase(__position); }
  1402. #endif
  1403. /**
  1404. * @brief Remove a range of elements.
  1405. * @param __first Iterator pointing to the first element to be erased.
  1406. * @param __last Iterator pointing to one past the last element to be
  1407. * erased.
  1408. * @return An iterator pointing to the element pointed to by @a __last
  1409. * prior to erasing (or end()).
  1410. *
  1411. * This function will erase the elements in the range
  1412. * [__first,__last) and shorten the %vector accordingly.
  1413. *
  1414. * Note This operation could be expensive and if it is
  1415. * frequently used the user should consider using std::list.
  1416. * The user is also cautioned that this function only erases
  1417. * the elements, and that if the elements themselves are
  1418. * pointers, the pointed-to memory is not touched in any way.
  1419. * Managing the pointer is the user's responsibility.
  1420. */
  1421. _GLIBCXX20_CONSTEXPR
  1422. iterator
  1423. #if __cplusplus >= 201103L
  1424. erase(const_iterator __first, const_iterator __last)
  1425. {
  1426. const auto __beg = begin();
  1427. const auto __cbeg = cbegin();
  1428. return _M_erase(__beg + (__first - __cbeg), __beg + (__last - __cbeg));
  1429. }
  1430. #else
  1431. erase(iterator __first, iterator __last)
  1432. { return _M_erase(__first, __last); }
  1433. #endif
  1434. /**
  1435. * @brief Swaps data with another %vector.
  1436. * @param __x A %vector of the same element and allocator types.
  1437. *
  1438. * This exchanges the elements between two vectors in constant time.
  1439. * (Three pointers, so it should be quite fast.)
  1440. * Note that the global std::swap() function is specialized such that
  1441. * std::swap(v1,v2) will feed to this function.
  1442. *
  1443. * Whether the allocators are swapped depends on the allocator traits.
  1444. */
  1445. _GLIBCXX20_CONSTEXPR
  1446. void
  1447. swap(vector& __x) _GLIBCXX_NOEXCEPT
  1448. {
  1449. #if __cplusplus >= 201103L
  1450. __glibcxx_assert(_Alloc_traits::propagate_on_container_swap::value
  1451. || _M_get_Tp_allocator() == __x._M_get_Tp_allocator());
  1452. #endif
  1453. this->_M_impl._M_swap_data(__x._M_impl);
  1454. _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
  1455. __x._M_get_Tp_allocator());
  1456. }
  1457. /**
  1458. * Erases all the elements. Note that this function only erases the
  1459. * elements, and that if the elements themselves are pointers, the
  1460. * pointed-to memory is not touched in any way. Managing the pointer is
  1461. * the user's responsibility.
  1462. */
  1463. _GLIBCXX20_CONSTEXPR
  1464. void
  1465. clear() _GLIBCXX_NOEXCEPT
  1466. { _M_erase_at_end(this->_M_impl._M_start); }
  1467. protected:
  1468. /**
  1469. * Memory expansion handler. Uses the member allocation function to
  1470. * obtain @a n bytes of memory, and then copies [first,last) into it.
  1471. */
  1472. template<typename _ForwardIterator>
  1473. _GLIBCXX20_CONSTEXPR
  1474. pointer
  1475. _M_allocate_and_copy(size_type __n,
  1476. _ForwardIterator __first, _ForwardIterator __last)
  1477. {
  1478. pointer __result = this->_M_allocate(__n);
  1479. __try
  1480. {
  1481. std::__uninitialized_copy_a(__first, __last, __result,
  1482. _M_get_Tp_allocator());
  1483. return __result;
  1484. }
  1485. __catch(...)
  1486. {
  1487. _M_deallocate(__result, __n);
  1488. __throw_exception_again;
  1489. }
  1490. }
  1491. // Internal constructor functions follow.
  1492. // Called by the range constructor to implement [23.1.1]/9
  1493. #if __cplusplus < 201103L
  1494. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1495. // 438. Ambiguity in the "do the right thing" clause
  1496. template<typename _Integer>
  1497. void
  1498. _M_initialize_dispatch(_Integer __n, _Integer __value, __true_type)
  1499. {
  1500. this->_M_impl._M_start = _M_allocate(_S_check_init_len(
  1501. static_cast<size_type>(__n), _M_get_Tp_allocator()));
  1502. this->_M_impl._M_end_of_storage =
  1503. this->_M_impl._M_start + static_cast<size_type>(__n);
  1504. _M_fill_initialize(static_cast<size_type>(__n), __value);
  1505. }
  1506. // Called by the range constructor to implement [23.1.1]/9
  1507. template<typename _InputIterator>
  1508. void
  1509. _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
  1510. __false_type)
  1511. {
  1512. _M_range_initialize(__first, __last,
  1513. std::__iterator_category(__first));
  1514. }
  1515. #endif
  1516. // Called by the second initialize_dispatch above
  1517. template<typename _InputIterator>
  1518. _GLIBCXX20_CONSTEXPR
  1519. void
  1520. _M_range_initialize(_InputIterator __first, _InputIterator __last,
  1521. std::input_iterator_tag)
  1522. {
  1523. __try {
  1524. for (; __first != __last; ++__first)
  1525. #if __cplusplus >= 201103L
  1526. emplace_back(*__first);
  1527. #else
  1528. push_back(*__first);
  1529. #endif
  1530. } __catch(...) {
  1531. clear();
  1532. __throw_exception_again;
  1533. }
  1534. }
  1535. // Called by the second initialize_dispatch above
  1536. template<typename _ForwardIterator>
  1537. _GLIBCXX20_CONSTEXPR
  1538. void
  1539. _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
  1540. std::forward_iterator_tag)
  1541. {
  1542. const size_type __n = std::distance(__first, __last);
  1543. this->_M_impl._M_start
  1544. = this->_M_allocate(_S_check_init_len(__n, _M_get_Tp_allocator()));
  1545. this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
  1546. this->_M_impl._M_finish =
  1547. std::__uninitialized_copy_a(__first, __last,
  1548. this->_M_impl._M_start,
  1549. _M_get_Tp_allocator());
  1550. }
  1551. // Called by the first initialize_dispatch above and by the
  1552. // vector(n,value,a) constructor.
  1553. _GLIBCXX20_CONSTEXPR
  1554. void
  1555. _M_fill_initialize(size_type __n, const value_type& __value)
  1556. {
  1557. this->_M_impl._M_finish =
  1558. std::__uninitialized_fill_n_a(this->_M_impl._M_start, __n, __value,
  1559. _M_get_Tp_allocator());
  1560. }
  1561. #if __cplusplus >= 201103L
  1562. // Called by the vector(n) constructor.
  1563. _GLIBCXX20_CONSTEXPR
  1564. void
  1565. _M_default_initialize(size_type __n)
  1566. {
  1567. this->_M_impl._M_finish =
  1568. std::__uninitialized_default_n_a(this->_M_impl._M_start, __n,
  1569. _M_get_Tp_allocator());
  1570. }
  1571. #endif
  1572. // Internal assign functions follow. The *_aux functions do the actual
  1573. // assignment work for the range versions.
  1574. // Called by the range assign to implement [23.1.1]/9
  1575. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1576. // 438. Ambiguity in the "do the right thing" clause
  1577. template<typename _Integer>
  1578. _GLIBCXX20_CONSTEXPR
  1579. void
  1580. _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
  1581. { _M_fill_assign(__n, __val); }
  1582. // Called by the range assign to implement [23.1.1]/9
  1583. template<typename _InputIterator>
  1584. _GLIBCXX20_CONSTEXPR
  1585. void
  1586. _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
  1587. __false_type)
  1588. { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
  1589. // Called by the second assign_dispatch above
  1590. template<typename _InputIterator>
  1591. _GLIBCXX20_CONSTEXPR
  1592. void
  1593. _M_assign_aux(_InputIterator __first, _InputIterator __last,
  1594. std::input_iterator_tag);
  1595. // Called by the second assign_dispatch above
  1596. template<typename _ForwardIterator>
  1597. _GLIBCXX20_CONSTEXPR
  1598. void
  1599. _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
  1600. std::forward_iterator_tag);
  1601. // Called by assign(n,t), and the range assign when it turns out
  1602. // to be the same thing.
  1603. _GLIBCXX20_CONSTEXPR
  1604. void
  1605. _M_fill_assign(size_type __n, const value_type& __val);
  1606. // Internal insert functions follow.
  1607. // Called by the range insert to implement [23.1.1]/9
  1608. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1609. // 438. Ambiguity in the "do the right thing" clause
  1610. template<typename _Integer>
  1611. _GLIBCXX20_CONSTEXPR
  1612. void
  1613. _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
  1614. __true_type)
  1615. { _M_fill_insert(__pos, __n, __val); }
  1616. // Called by the range insert to implement [23.1.1]/9
  1617. template<typename _InputIterator>
  1618. _GLIBCXX20_CONSTEXPR
  1619. void
  1620. _M_insert_dispatch(iterator __pos, _InputIterator __first,
  1621. _InputIterator __last, __false_type)
  1622. {
  1623. _M_range_insert(__pos, __first, __last,
  1624. std::__iterator_category(__first));
  1625. }
  1626. // Called by the second insert_dispatch above
  1627. template<typename _InputIterator>
  1628. _GLIBCXX20_CONSTEXPR
  1629. void
  1630. _M_range_insert(iterator __pos, _InputIterator __first,
  1631. _InputIterator __last, std::input_iterator_tag);
  1632. // Called by the second insert_dispatch above
  1633. template<typename _ForwardIterator>
  1634. _GLIBCXX20_CONSTEXPR
  1635. void
  1636. _M_range_insert(iterator __pos, _ForwardIterator __first,
  1637. _ForwardIterator __last, std::forward_iterator_tag);
  1638. // Called by insert(p,n,x), and the range insert when it turns out to be
  1639. // the same thing.
  1640. _GLIBCXX20_CONSTEXPR
  1641. void
  1642. _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
  1643. #if __cplusplus >= 201103L
  1644. // Called by resize(n).
  1645. _GLIBCXX20_CONSTEXPR
  1646. void
  1647. _M_default_append(size_type __n);
  1648. _GLIBCXX20_CONSTEXPR
  1649. bool
  1650. _M_shrink_to_fit();
  1651. #endif
  1652. #if __cplusplus < 201103L
  1653. // Called by insert(p,x)
  1654. void
  1655. _M_insert_aux(iterator __position, const value_type& __x);
  1656. void
  1657. _M_realloc_insert(iterator __position, const value_type& __x);
  1658. #else
  1659. // A value_type object constructed with _Alloc_traits::construct()
  1660. // and destroyed with _Alloc_traits::destroy().
  1661. struct _Temporary_value
  1662. {
  1663. template<typename... _Args>
  1664. _GLIBCXX20_CONSTEXPR explicit
  1665. _Temporary_value(vector* __vec, _Args&&... __args) : _M_this(__vec)
  1666. {
  1667. _Alloc_traits::construct(_M_this->_M_impl, _M_ptr(),
  1668. std::forward<_Args>(__args)...);
  1669. }
  1670. _GLIBCXX20_CONSTEXPR
  1671. ~_Temporary_value()
  1672. { _Alloc_traits::destroy(_M_this->_M_impl, _M_ptr()); }
  1673. _GLIBCXX20_CONSTEXPR value_type&
  1674. _M_val() noexcept { return _M_storage._M_val; }
  1675. private:
  1676. _GLIBCXX20_CONSTEXPR _Tp*
  1677. _M_ptr() noexcept { return std::__addressof(_M_storage._M_val); }
  1678. union _Storage
  1679. {
  1680. constexpr _Storage() : _M_byte() { }
  1681. _GLIBCXX20_CONSTEXPR ~_Storage() { }
  1682. _Storage& operator=(const _Storage&) = delete;
  1683. unsigned char _M_byte;
  1684. _Tp _M_val;
  1685. };
  1686. vector* _M_this;
  1687. _Storage _M_storage;
  1688. };
  1689. // Called by insert(p,x) and other functions when insertion needs to
  1690. // reallocate or move existing elements. _Arg is either _Tp& or _Tp.
  1691. template<typename _Arg>
  1692. _GLIBCXX20_CONSTEXPR
  1693. void
  1694. _M_insert_aux(iterator __position, _Arg&& __arg);
  1695. template<typename... _Args>
  1696. _GLIBCXX20_CONSTEXPR
  1697. void
  1698. _M_realloc_insert(iterator __position, _Args&&... __args);
  1699. // Either move-construct at the end, or forward to _M_insert_aux.
  1700. _GLIBCXX20_CONSTEXPR
  1701. iterator
  1702. _M_insert_rval(const_iterator __position, value_type&& __v);
  1703. // Try to emplace at the end, otherwise forward to _M_insert_aux.
  1704. template<typename... _Args>
  1705. _GLIBCXX20_CONSTEXPR
  1706. iterator
  1707. _M_emplace_aux(const_iterator __position, _Args&&... __args);
  1708. // Emplacing an rvalue of the correct type can use _M_insert_rval.
  1709. _GLIBCXX20_CONSTEXPR
  1710. iterator
  1711. _M_emplace_aux(const_iterator __position, value_type&& __v)
  1712. { return _M_insert_rval(__position, std::move(__v)); }
  1713. #endif
  1714. // Called by _M_fill_insert, _M_insert_aux etc.
  1715. _GLIBCXX20_CONSTEXPR
  1716. size_type
  1717. _M_check_len(size_type __n, const char* __s) const
  1718. {
  1719. if (max_size() - size() < __n)
  1720. __throw_length_error(__N(__s));
  1721. const size_type __len = size() + (std::max)(size(), __n);
  1722. return (__len < size() || __len > max_size()) ? max_size() : __len;
  1723. }
  1724. // Called by constructors to check initial size.
  1725. static _GLIBCXX20_CONSTEXPR size_type
  1726. _S_check_init_len(size_type __n, const allocator_type& __a)
  1727. {
  1728. if (__n > _S_max_size(_Tp_alloc_type(__a)))
  1729. __throw_length_error(
  1730. __N("cannot create std::vector larger than max_size()"));
  1731. return __n;
  1732. }
  1733. static _GLIBCXX20_CONSTEXPR size_type
  1734. _S_max_size(const _Tp_alloc_type& __a) _GLIBCXX_NOEXCEPT
  1735. {
  1736. // std::distance(begin(), end()) cannot be greater than PTRDIFF_MAX,
  1737. // and realistically we can't store more than PTRDIFF_MAX/sizeof(T)
  1738. // (even if std::allocator_traits::max_size says we can).
  1739. const size_t __diffmax
  1740. = __gnu_cxx::__numeric_traits<ptrdiff_t>::__max / sizeof(_Tp);
  1741. const size_t __allocmax = _Alloc_traits::max_size(__a);
  1742. return (std::min)(__diffmax, __allocmax);
  1743. }
  1744. // Internal erase functions follow.
  1745. // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
  1746. // _M_assign_aux.
  1747. _GLIBCXX20_CONSTEXPR
  1748. void
  1749. _M_erase_at_end(pointer __pos) _GLIBCXX_NOEXCEPT
  1750. {
  1751. if (size_type __n = this->_M_impl._M_finish - __pos)
  1752. {
  1753. std::_Destroy(__pos, this->_M_impl._M_finish,
  1754. _M_get_Tp_allocator());
  1755. this->_M_impl._M_finish = __pos;
  1756. _GLIBCXX_ASAN_ANNOTATE_SHRINK(__n);
  1757. }
  1758. }
  1759. _GLIBCXX20_CONSTEXPR
  1760. iterator
  1761. _M_erase(iterator __position);
  1762. _GLIBCXX20_CONSTEXPR
  1763. iterator
  1764. _M_erase(iterator __first, iterator __last);
  1765. #if __cplusplus >= 201103L
  1766. private:
  1767. // Constant-time move assignment when source object's memory can be
  1768. // moved, either because the source's allocator will move too
  1769. // or because the allocators are equal.
  1770. _GLIBCXX20_CONSTEXPR
  1771. void
  1772. _M_move_assign(vector&& __x, true_type) noexcept
  1773. {
  1774. vector __tmp(get_allocator());
  1775. this->_M_impl._M_swap_data(__x._M_impl);
  1776. __tmp._M_impl._M_swap_data(__x._M_impl);
  1777. std::__alloc_on_move(_M_get_Tp_allocator(), __x._M_get_Tp_allocator());
  1778. }
  1779. // Do move assignment when it might not be possible to move source
  1780. // object's memory, resulting in a linear-time operation.
  1781. _GLIBCXX20_CONSTEXPR
  1782. void
  1783. _M_move_assign(vector&& __x, false_type)
  1784. {
  1785. if (__x._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
  1786. _M_move_assign(std::move(__x), true_type());
  1787. else
  1788. {
  1789. // The rvalue's allocator cannot be moved and is not equal,
  1790. // so we need to individually move each element.
  1791. this->_M_assign_aux(std::make_move_iterator(__x.begin()),
  1792. std::make_move_iterator(__x.end()),
  1793. std::random_access_iterator_tag());
  1794. __x.clear();
  1795. }
  1796. }
  1797. #endif
  1798. template<typename _Up>
  1799. _GLIBCXX20_CONSTEXPR
  1800. _Up*
  1801. _M_data_ptr(_Up* __ptr) const _GLIBCXX_NOEXCEPT
  1802. { return __ptr; }
  1803. #if __cplusplus >= 201103L
  1804. template<typename _Ptr>
  1805. _GLIBCXX20_CONSTEXPR
  1806. typename std::pointer_traits<_Ptr>::element_type*
  1807. _M_data_ptr(_Ptr __ptr) const
  1808. { return empty() ? nullptr : std::__to_address(__ptr); }
  1809. #else
  1810. template<typename _Up>
  1811. _Up*
  1812. _M_data_ptr(_Up* __ptr) _GLIBCXX_NOEXCEPT
  1813. { return __ptr; }
  1814. template<typename _Ptr>
  1815. value_type*
  1816. _M_data_ptr(_Ptr __ptr)
  1817. { return empty() ? (value_type*)0 : __ptr.operator->(); }
  1818. template<typename _Ptr>
  1819. const value_type*
  1820. _M_data_ptr(_Ptr __ptr) const
  1821. { return empty() ? (const value_type*)0 : __ptr.operator->(); }
  1822. #endif
  1823. };
  1824. #if __cpp_deduction_guides >= 201606
  1825. template<typename _InputIterator, typename _ValT
  1826. = typename iterator_traits<_InputIterator>::value_type,
  1827. typename _Allocator = allocator<_ValT>,
  1828. typename = _RequireInputIter<_InputIterator>,
  1829. typename = _RequireAllocator<_Allocator>>
  1830. vector(_InputIterator, _InputIterator, _Allocator = _Allocator())
  1831. -> vector<_ValT, _Allocator>;
  1832. #endif
  1833. /**
  1834. * @brief Vector equality comparison.
  1835. * @param __x A %vector.
  1836. * @param __y A %vector of the same type as @a __x.
  1837. * @return True iff the size and elements of the vectors are equal.
  1838. *
  1839. * This is an equivalence relation. It is linear in the size of the
  1840. * vectors. Vectors are considered equivalent if their sizes are equal,
  1841. * and if corresponding elements compare equal.
  1842. */
  1843. template<typename _Tp, typename _Alloc>
  1844. _GLIBCXX20_CONSTEXPR
  1845. inline bool
  1846. operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
  1847. { return (__x.size() == __y.size()
  1848. && std::equal(__x.begin(), __x.end(), __y.begin())); }
  1849. #if __cpp_lib_three_way_comparison
  1850. /**
  1851. * @brief Vector ordering relation.
  1852. * @param __x A `vector`.
  1853. * @param __y A `vector` of the same type as `__x`.
  1854. * @return A value indicating whether `__x` is less than, equal to,
  1855. * greater than, or incomparable with `__y`.
  1856. *
  1857. * See `std::lexicographical_compare_three_way()` for how the determination
  1858. * is made. This operator is used to synthesize relational operators like
  1859. * `<` and `>=` etc.
  1860. */
  1861. template<typename _Tp, typename _Alloc>
  1862. _GLIBCXX20_CONSTEXPR
  1863. inline __detail::__synth3way_t<_Tp>
  1864. operator<=>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
  1865. {
  1866. return std::lexicographical_compare_three_way(__x.begin(), __x.end(),
  1867. __y.begin(), __y.end(),
  1868. __detail::__synth3way);
  1869. }
  1870. #else
  1871. /**
  1872. * @brief Vector ordering relation.
  1873. * @param __x A %vector.
  1874. * @param __y A %vector of the same type as @a __x.
  1875. * @return True iff @a __x is lexicographically less than @a __y.
  1876. *
  1877. * This is a total ordering relation. It is linear in the size of the
  1878. * vectors. The elements must be comparable with @c <.
  1879. *
  1880. * See std::lexicographical_compare() for how the determination is made.
  1881. */
  1882. template<typename _Tp, typename _Alloc>
  1883. inline bool
  1884. operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
  1885. { return std::lexicographical_compare(__x.begin(), __x.end(),
  1886. __y.begin(), __y.end()); }
  1887. /// Based on operator==
  1888. template<typename _Tp, typename _Alloc>
  1889. inline bool
  1890. operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
  1891. { return !(__x == __y); }
  1892. /// Based on operator<
  1893. template<typename _Tp, typename _Alloc>
  1894. inline bool
  1895. operator>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
  1896. { return __y < __x; }
  1897. /// Based on operator<
  1898. template<typename _Tp, typename _Alloc>
  1899. inline bool
  1900. operator<=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
  1901. { return !(__y < __x); }
  1902. /// Based on operator<
  1903. template<typename _Tp, typename _Alloc>
  1904. inline bool
  1905. operator>=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
  1906. { return !(__x < __y); }
  1907. #endif // three-way comparison
  1908. /// See std::vector::swap().
  1909. template<typename _Tp, typename _Alloc>
  1910. _GLIBCXX20_CONSTEXPR
  1911. inline void
  1912. swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>& __y)
  1913. _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
  1914. { __x.swap(__y); }
  1915. _GLIBCXX_END_NAMESPACE_CONTAINER
  1916. #if __cplusplus >= 201703L
  1917. namespace __detail::__variant
  1918. {
  1919. template<typename> struct _Never_valueless_alt; // see <variant>
  1920. // Provide the strong exception-safety guarantee when emplacing a
  1921. // vector into a variant, but only if move assignment cannot throw.
  1922. template<typename _Tp, typename _Alloc>
  1923. struct _Never_valueless_alt<_GLIBCXX_STD_C::vector<_Tp, _Alloc>>
  1924. : std::is_nothrow_move_assignable<_GLIBCXX_STD_C::vector<_Tp, _Alloc>>
  1925. { };
  1926. } // namespace __detail::__variant
  1927. #endif // C++17
  1928. _GLIBCXX_END_NAMESPACE_VERSION
  1929. } // namespace std
  1930. #endif /* _STL_VECTOR_H */