stl_vector.h 63 KB

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