stl_vector.h 59 KB

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