stl_vector.h 64 KB

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