vector.tcc 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. // Vector implementation (out of line) -*- C++ -*-
  2. // Copyright (C) 2001-2023 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/vector.tcc
  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 _VECTOR_TCC
  50. #define _VECTOR_TCC 1
  51. namespace std _GLIBCXX_VISIBILITY(default)
  52. {
  53. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  54. _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
  55. template<typename _Tp, typename _Alloc>
  56. _GLIBCXX20_CONSTEXPR
  57. void
  58. vector<_Tp, _Alloc>::
  59. reserve(size_type __n)
  60. {
  61. if (__n > this->max_size())
  62. __throw_length_error(__N("vector::reserve"));
  63. if (this->capacity() < __n)
  64. {
  65. const size_type __old_size = size();
  66. pointer __tmp;
  67. #if __cplusplus >= 201103L
  68. if _GLIBCXX17_CONSTEXPR (_S_use_relocate())
  69. {
  70. __tmp = this->_M_allocate(__n);
  71. _S_relocate(this->_M_impl._M_start, this->_M_impl._M_finish,
  72. __tmp, _M_get_Tp_allocator());
  73. }
  74. else
  75. #endif
  76. {
  77. __tmp = _M_allocate_and_copy(__n,
  78. _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(this->_M_impl._M_start),
  79. _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(this->_M_impl._M_finish));
  80. std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
  81. _M_get_Tp_allocator());
  82. }
  83. _GLIBCXX_ASAN_ANNOTATE_REINIT;
  84. _M_deallocate(this->_M_impl._M_start,
  85. this->_M_impl._M_end_of_storage
  86. - this->_M_impl._M_start);
  87. this->_M_impl._M_start = __tmp;
  88. this->_M_impl._M_finish = __tmp + __old_size;
  89. this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
  90. }
  91. }
  92. #if __cplusplus >= 201103L
  93. template<typename _Tp, typename _Alloc>
  94. template<typename... _Args>
  95. #if __cplusplus > 201402L
  96. _GLIBCXX20_CONSTEXPR
  97. typename vector<_Tp, _Alloc>::reference
  98. #else
  99. void
  100. #endif
  101. vector<_Tp, _Alloc>::
  102. emplace_back(_Args&&... __args)
  103. {
  104. if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
  105. {
  106. _GLIBCXX_ASAN_ANNOTATE_GROW(1);
  107. _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
  108. std::forward<_Args>(__args)...);
  109. ++this->_M_impl._M_finish;
  110. _GLIBCXX_ASAN_ANNOTATE_GREW(1);
  111. }
  112. else
  113. _M_realloc_insert(end(), std::forward<_Args>(__args)...);
  114. #if __cplusplus > 201402L
  115. return back();
  116. #endif
  117. }
  118. #endif
  119. template<typename _Tp, typename _Alloc>
  120. _GLIBCXX20_CONSTEXPR
  121. typename vector<_Tp, _Alloc>::iterator
  122. vector<_Tp, _Alloc>::
  123. #if __cplusplus >= 201103L
  124. insert(const_iterator __position, const value_type& __x)
  125. #else
  126. insert(iterator __position, const value_type& __x)
  127. #endif
  128. {
  129. const size_type __n = __position - begin();
  130. if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
  131. {
  132. __glibcxx_assert(__position != const_iterator());
  133. if (!(__position != const_iterator()))
  134. __builtin_unreachable(); // PR 106434
  135. if (__position == end())
  136. {
  137. _GLIBCXX_ASAN_ANNOTATE_GROW(1);
  138. _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
  139. __x);
  140. ++this->_M_impl._M_finish;
  141. _GLIBCXX_ASAN_ANNOTATE_GREW(1);
  142. }
  143. else
  144. {
  145. #if __cplusplus >= 201103L
  146. const auto __pos = begin() + (__position - cbegin());
  147. // __x could be an existing element of this vector, so make a
  148. // copy of it before _M_insert_aux moves elements around.
  149. _Temporary_value __x_copy(this, __x);
  150. _M_insert_aux(__pos, std::move(__x_copy._M_val()));
  151. #else
  152. _M_insert_aux(__position, __x);
  153. #endif
  154. }
  155. }
  156. else
  157. #if __cplusplus >= 201103L
  158. _M_realloc_insert(begin() + (__position - cbegin()), __x);
  159. #else
  160. _M_realloc_insert(__position, __x);
  161. #endif
  162. return iterator(this->_M_impl._M_start + __n);
  163. }
  164. template<typename _Tp, typename _Alloc>
  165. _GLIBCXX20_CONSTEXPR
  166. typename vector<_Tp, _Alloc>::iterator
  167. vector<_Tp, _Alloc>::
  168. _M_erase(iterator __position)
  169. {
  170. if (__position + 1 != end())
  171. _GLIBCXX_MOVE3(__position + 1, end(), __position);
  172. --this->_M_impl._M_finish;
  173. _Alloc_traits::destroy(this->_M_impl, this->_M_impl._M_finish);
  174. _GLIBCXX_ASAN_ANNOTATE_SHRINK(1);
  175. return __position;
  176. }
  177. template<typename _Tp, typename _Alloc>
  178. _GLIBCXX20_CONSTEXPR
  179. typename vector<_Tp, _Alloc>::iterator
  180. vector<_Tp, _Alloc>::
  181. _M_erase(iterator __first, iterator __last)
  182. {
  183. if (__first != __last)
  184. {
  185. if (__last != end())
  186. _GLIBCXX_MOVE3(__last, end(), __first);
  187. _M_erase_at_end(__first.base() + (end() - __last));
  188. }
  189. return __first;
  190. }
  191. template<typename _Tp, typename _Alloc>
  192. _GLIBCXX20_CONSTEXPR
  193. vector<_Tp, _Alloc>&
  194. vector<_Tp, _Alloc>::
  195. operator=(const vector<_Tp, _Alloc>& __x)
  196. {
  197. if (std::__addressof(__x) != this)
  198. {
  199. _GLIBCXX_ASAN_ANNOTATE_REINIT;
  200. #if __cplusplus >= 201103L
  201. if (_Alloc_traits::_S_propagate_on_copy_assign())
  202. {
  203. if (!_Alloc_traits::_S_always_equal()
  204. && _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
  205. {
  206. // replacement allocator cannot free existing storage
  207. this->clear();
  208. _M_deallocate(this->_M_impl._M_start,
  209. this->_M_impl._M_end_of_storage
  210. - this->_M_impl._M_start);
  211. this->_M_impl._M_start = nullptr;
  212. this->_M_impl._M_finish = nullptr;
  213. this->_M_impl._M_end_of_storage = nullptr;
  214. }
  215. std::__alloc_on_copy(_M_get_Tp_allocator(),
  216. __x._M_get_Tp_allocator());
  217. }
  218. #endif
  219. const size_type __xlen = __x.size();
  220. if (__xlen > capacity())
  221. {
  222. pointer __tmp = _M_allocate_and_copy(__xlen, __x.begin(),
  223. __x.end());
  224. std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
  225. _M_get_Tp_allocator());
  226. _M_deallocate(this->_M_impl._M_start,
  227. this->_M_impl._M_end_of_storage
  228. - this->_M_impl._M_start);
  229. this->_M_impl._M_start = __tmp;
  230. this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __xlen;
  231. }
  232. else if (size() >= __xlen)
  233. {
  234. std::_Destroy(std::copy(__x.begin(), __x.end(), begin()),
  235. end(), _M_get_Tp_allocator());
  236. }
  237. else
  238. {
  239. std::copy(__x._M_impl._M_start, __x._M_impl._M_start + size(),
  240. this->_M_impl._M_start);
  241. std::__uninitialized_copy_a(__x._M_impl._M_start + size(),
  242. __x._M_impl._M_finish,
  243. this->_M_impl._M_finish,
  244. _M_get_Tp_allocator());
  245. }
  246. this->_M_impl._M_finish = this->_M_impl._M_start + __xlen;
  247. }
  248. return *this;
  249. }
  250. template<typename _Tp, typename _Alloc>
  251. _GLIBCXX20_CONSTEXPR
  252. void
  253. vector<_Tp, _Alloc>::
  254. _M_fill_assign(size_t __n, const value_type& __val)
  255. {
  256. if (__n > capacity())
  257. {
  258. vector __tmp(__n, __val, _M_get_Tp_allocator());
  259. __tmp._M_impl._M_swap_data(this->_M_impl);
  260. }
  261. else if (__n > size())
  262. {
  263. std::fill(begin(), end(), __val);
  264. const size_type __add = __n - size();
  265. _GLIBCXX_ASAN_ANNOTATE_GROW(__add);
  266. this->_M_impl._M_finish =
  267. std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
  268. __add, __val, _M_get_Tp_allocator());
  269. _GLIBCXX_ASAN_ANNOTATE_GREW(__add);
  270. }
  271. else
  272. _M_erase_at_end(std::fill_n(this->_M_impl._M_start, __n, __val));
  273. }
  274. template<typename _Tp, typename _Alloc>
  275. template<typename _InputIterator>
  276. _GLIBCXX20_CONSTEXPR
  277. void
  278. vector<_Tp, _Alloc>::
  279. _M_assign_aux(_InputIterator __first, _InputIterator __last,
  280. std::input_iterator_tag)
  281. {
  282. pointer __cur(this->_M_impl._M_start);
  283. for (; __first != __last && __cur != this->_M_impl._M_finish;
  284. ++__cur, (void)++__first)
  285. *__cur = *__first;
  286. if (__first == __last)
  287. _M_erase_at_end(__cur);
  288. else
  289. _M_range_insert(end(), __first, __last,
  290. std::__iterator_category(__first));
  291. }
  292. template<typename _Tp, typename _Alloc>
  293. template<typename _ForwardIterator>
  294. _GLIBCXX20_CONSTEXPR
  295. void
  296. vector<_Tp, _Alloc>::
  297. _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
  298. std::forward_iterator_tag)
  299. {
  300. const size_type __len = std::distance(__first, __last);
  301. if (__len > capacity())
  302. {
  303. _S_check_init_len(__len, _M_get_Tp_allocator());
  304. pointer __tmp(_M_allocate_and_copy(__len, __first, __last));
  305. std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
  306. _M_get_Tp_allocator());
  307. _GLIBCXX_ASAN_ANNOTATE_REINIT;
  308. _M_deallocate(this->_M_impl._M_start,
  309. this->_M_impl._M_end_of_storage
  310. - this->_M_impl._M_start);
  311. this->_M_impl._M_start = __tmp;
  312. this->_M_impl._M_finish = this->_M_impl._M_start + __len;
  313. this->_M_impl._M_end_of_storage = this->_M_impl._M_finish;
  314. }
  315. else if (size() >= __len)
  316. _M_erase_at_end(std::copy(__first, __last, this->_M_impl._M_start));
  317. else
  318. {
  319. _ForwardIterator __mid = __first;
  320. std::advance(__mid, size());
  321. std::copy(__first, __mid, this->_M_impl._M_start);
  322. const size_type __attribute__((__unused__)) __n = __len - size();
  323. _GLIBCXX_ASAN_ANNOTATE_GROW(__n);
  324. this->_M_impl._M_finish =
  325. std::__uninitialized_copy_a(__mid, __last,
  326. this->_M_impl._M_finish,
  327. _M_get_Tp_allocator());
  328. _GLIBCXX_ASAN_ANNOTATE_GREW(__n);
  329. }
  330. }
  331. #if __cplusplus >= 201103L
  332. template<typename _Tp, typename _Alloc>
  333. _GLIBCXX20_CONSTEXPR
  334. auto
  335. vector<_Tp, _Alloc>::
  336. _M_insert_rval(const_iterator __position, value_type&& __v) -> iterator
  337. {
  338. const auto __n = __position - cbegin();
  339. if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
  340. if (__position == cend())
  341. {
  342. _GLIBCXX_ASAN_ANNOTATE_GROW(1);
  343. _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
  344. std::move(__v));
  345. ++this->_M_impl._M_finish;
  346. _GLIBCXX_ASAN_ANNOTATE_GREW(1);
  347. }
  348. else
  349. _M_insert_aux(begin() + __n, std::move(__v));
  350. else
  351. _M_realloc_insert(begin() + __n, std::move(__v));
  352. return iterator(this->_M_impl._M_start + __n);
  353. }
  354. template<typename _Tp, typename _Alloc>
  355. template<typename... _Args>
  356. _GLIBCXX20_CONSTEXPR
  357. auto
  358. vector<_Tp, _Alloc>::
  359. _M_emplace_aux(const_iterator __position, _Args&&... __args)
  360. -> iterator
  361. {
  362. const auto __n = __position - cbegin();
  363. if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
  364. if (__position == cend())
  365. {
  366. _GLIBCXX_ASAN_ANNOTATE_GROW(1);
  367. _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
  368. std::forward<_Args>(__args)...);
  369. ++this->_M_impl._M_finish;
  370. _GLIBCXX_ASAN_ANNOTATE_GREW(1);
  371. }
  372. else
  373. {
  374. // We need to construct a temporary because something in __args...
  375. // could alias one of the elements of the container and so we
  376. // need to use it before _M_insert_aux moves elements around.
  377. _Temporary_value __tmp(this, std::forward<_Args>(__args)...);
  378. _M_insert_aux(begin() + __n, std::move(__tmp._M_val()));
  379. }
  380. else
  381. _M_realloc_insert(begin() + __n, std::forward<_Args>(__args)...);
  382. return iterator(this->_M_impl._M_start + __n);
  383. }
  384. template<typename _Tp, typename _Alloc>
  385. template<typename _Arg>
  386. _GLIBCXX20_CONSTEXPR
  387. void
  388. vector<_Tp, _Alloc>::
  389. _M_insert_aux(iterator __position, _Arg&& __arg)
  390. #else
  391. template<typename _Tp, typename _Alloc>
  392. void
  393. vector<_Tp, _Alloc>::
  394. _M_insert_aux(iterator __position, const _Tp& __x)
  395. #endif
  396. {
  397. _GLIBCXX_ASAN_ANNOTATE_GROW(1);
  398. _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
  399. _GLIBCXX_MOVE(*(this->_M_impl._M_finish - 1)));
  400. ++this->_M_impl._M_finish;
  401. _GLIBCXX_ASAN_ANNOTATE_GREW(1);
  402. #if __cplusplus < 201103L
  403. _Tp __x_copy = __x;
  404. #endif
  405. _GLIBCXX_MOVE_BACKWARD3(__position.base(),
  406. this->_M_impl._M_finish - 2,
  407. this->_M_impl._M_finish - 1);
  408. #if __cplusplus < 201103L
  409. *__position = __x_copy;
  410. #else
  411. *__position = std::forward<_Arg>(__arg);
  412. #endif
  413. }
  414. #if __cplusplus >= 201103L
  415. template<typename _Tp, typename _Alloc>
  416. template<typename... _Args>
  417. _GLIBCXX20_CONSTEXPR
  418. void
  419. vector<_Tp, _Alloc>::
  420. _M_realloc_insert(iterator __position, _Args&&... __args)
  421. #else
  422. template<typename _Tp, typename _Alloc>
  423. void
  424. vector<_Tp, _Alloc>::
  425. _M_realloc_insert(iterator __position, const _Tp& __x)
  426. #endif
  427. {
  428. const size_type __len =
  429. _M_check_len(size_type(1), "vector::_M_realloc_insert");
  430. pointer __old_start = this->_M_impl._M_start;
  431. pointer __old_finish = this->_M_impl._M_finish;
  432. const size_type __elems_before = __position - begin();
  433. pointer __new_start(this->_M_allocate(__len));
  434. pointer __new_finish(__new_start);
  435. __try
  436. {
  437. // The order of the three operations is dictated by the C++11
  438. // case, where the moves could alter a new element belonging
  439. // to the existing vector. This is an issue only for callers
  440. // taking the element by lvalue ref (see last bullet of C++11
  441. // [res.on.arguments]).
  442. _Alloc_traits::construct(this->_M_impl,
  443. __new_start + __elems_before,
  444. #if __cplusplus >= 201103L
  445. std::forward<_Args>(__args)...);
  446. #else
  447. __x);
  448. #endif
  449. __new_finish = pointer();
  450. #if __cplusplus >= 201103L
  451. if _GLIBCXX17_CONSTEXPR (_S_use_relocate())
  452. {
  453. __new_finish = _S_relocate(__old_start, __position.base(),
  454. __new_start, _M_get_Tp_allocator());
  455. ++__new_finish;
  456. __new_finish = _S_relocate(__position.base(), __old_finish,
  457. __new_finish, _M_get_Tp_allocator());
  458. }
  459. else
  460. #endif
  461. {
  462. __new_finish
  463. = std::__uninitialized_move_if_noexcept_a
  464. (__old_start, __position.base(),
  465. __new_start, _M_get_Tp_allocator());
  466. ++__new_finish;
  467. __new_finish
  468. = std::__uninitialized_move_if_noexcept_a
  469. (__position.base(), __old_finish,
  470. __new_finish, _M_get_Tp_allocator());
  471. }
  472. }
  473. __catch(...)
  474. {
  475. if (!__new_finish)
  476. _Alloc_traits::destroy(this->_M_impl,
  477. __new_start + __elems_before);
  478. else
  479. std::_Destroy(__new_start, __new_finish, _M_get_Tp_allocator());
  480. _M_deallocate(__new_start, __len);
  481. __throw_exception_again;
  482. }
  483. #if __cplusplus >= 201103L
  484. if _GLIBCXX17_CONSTEXPR (!_S_use_relocate())
  485. #endif
  486. std::_Destroy(__old_start, __old_finish, _M_get_Tp_allocator());
  487. _GLIBCXX_ASAN_ANNOTATE_REINIT;
  488. _M_deallocate(__old_start,
  489. this->_M_impl._M_end_of_storage - __old_start);
  490. this->_M_impl._M_start = __new_start;
  491. this->_M_impl._M_finish = __new_finish;
  492. this->_M_impl._M_end_of_storage = __new_start + __len;
  493. }
  494. template<typename _Tp, typename _Alloc>
  495. _GLIBCXX20_CONSTEXPR
  496. void
  497. vector<_Tp, _Alloc>::
  498. _M_fill_insert(iterator __position, size_type __n, const value_type& __x)
  499. {
  500. if (__n != 0)
  501. {
  502. if (size_type(this->_M_impl._M_end_of_storage
  503. - this->_M_impl._M_finish) >= __n)
  504. {
  505. #if __cplusplus < 201103L
  506. value_type __x_copy = __x;
  507. #else
  508. _Temporary_value __tmp(this, __x);
  509. value_type& __x_copy = __tmp._M_val();
  510. #endif
  511. const size_type __elems_after = end() - __position;
  512. pointer __old_finish(this->_M_impl._M_finish);
  513. if (__elems_after > __n)
  514. {
  515. _GLIBCXX_ASAN_ANNOTATE_GROW(__n);
  516. std::__uninitialized_move_a(__old_finish - __n,
  517. __old_finish,
  518. __old_finish,
  519. _M_get_Tp_allocator());
  520. this->_M_impl._M_finish += __n;
  521. _GLIBCXX_ASAN_ANNOTATE_GREW(__n);
  522. _GLIBCXX_MOVE_BACKWARD3(__position.base(),
  523. __old_finish - __n, __old_finish);
  524. std::fill(__position.base(), __position.base() + __n,
  525. __x_copy);
  526. }
  527. else
  528. {
  529. _GLIBCXX_ASAN_ANNOTATE_GROW(__n);
  530. this->_M_impl._M_finish =
  531. std::__uninitialized_fill_n_a(__old_finish,
  532. __n - __elems_after,
  533. __x_copy,
  534. _M_get_Tp_allocator());
  535. _GLIBCXX_ASAN_ANNOTATE_GREW(__n - __elems_after);
  536. std::__uninitialized_move_a(__position.base(), __old_finish,
  537. this->_M_impl._M_finish,
  538. _M_get_Tp_allocator());
  539. this->_M_impl._M_finish += __elems_after;
  540. _GLIBCXX_ASAN_ANNOTATE_GREW(__elems_after);
  541. std::fill(__position.base(), __old_finish, __x_copy);
  542. }
  543. }
  544. else
  545. {
  546. // Make local copies of these members because the compiler thinks
  547. // the allocator can alter them if 'this' is globally reachable.
  548. pointer __old_start = this->_M_impl._M_start;
  549. pointer __old_finish = this->_M_impl._M_finish;
  550. const pointer __pos = __position.base();
  551. const size_type __len =
  552. _M_check_len(__n, "vector::_M_fill_insert");
  553. const size_type __elems_before = __pos - __old_start;
  554. pointer __new_start(this->_M_allocate(__len));
  555. pointer __new_finish(__new_start);
  556. __try
  557. {
  558. // See _M_realloc_insert above.
  559. std::__uninitialized_fill_n_a(__new_start + __elems_before,
  560. __n, __x,
  561. _M_get_Tp_allocator());
  562. __new_finish = pointer();
  563. __new_finish
  564. = std::__uninitialized_move_if_noexcept_a
  565. (__old_start, __pos, __new_start, _M_get_Tp_allocator());
  566. __new_finish += __n;
  567. __new_finish
  568. = std::__uninitialized_move_if_noexcept_a
  569. (__pos, __old_finish, __new_finish, _M_get_Tp_allocator());
  570. }
  571. __catch(...)
  572. {
  573. if (!__new_finish)
  574. std::_Destroy(__new_start + __elems_before,
  575. __new_start + __elems_before + __n,
  576. _M_get_Tp_allocator());
  577. else
  578. std::_Destroy(__new_start, __new_finish,
  579. _M_get_Tp_allocator());
  580. _M_deallocate(__new_start, __len);
  581. __throw_exception_again;
  582. }
  583. std::_Destroy(__old_start, __old_finish, _M_get_Tp_allocator());
  584. _GLIBCXX_ASAN_ANNOTATE_REINIT;
  585. _M_deallocate(__old_start,
  586. this->_M_impl._M_end_of_storage - __old_start);
  587. this->_M_impl._M_start = __new_start;
  588. this->_M_impl._M_finish = __new_finish;
  589. this->_M_impl._M_end_of_storage = __new_start + __len;
  590. }
  591. }
  592. }
  593. #if __cplusplus >= 201103L
  594. template<typename _Tp, typename _Alloc>
  595. _GLIBCXX20_CONSTEXPR
  596. void
  597. vector<_Tp, _Alloc>::
  598. _M_default_append(size_type __n)
  599. {
  600. if (__n != 0)
  601. {
  602. const size_type __size = size();
  603. size_type __navail = size_type(this->_M_impl._M_end_of_storage
  604. - this->_M_impl._M_finish);
  605. if (__size > max_size() || __navail > max_size() - __size)
  606. __builtin_unreachable();
  607. if (__navail >= __n)
  608. {
  609. _GLIBCXX_ASAN_ANNOTATE_GROW(__n);
  610. this->_M_impl._M_finish =
  611. std::__uninitialized_default_n_a(this->_M_impl._M_finish,
  612. __n, _M_get_Tp_allocator());
  613. _GLIBCXX_ASAN_ANNOTATE_GREW(__n);
  614. }
  615. else
  616. {
  617. // Make local copies of these members because the compiler thinks
  618. // the allocator can alter them if 'this' is globally reachable.
  619. pointer __old_start = this->_M_impl._M_start;
  620. pointer __old_finish = this->_M_impl._M_finish;
  621. const size_type __len =
  622. _M_check_len(__n, "vector::_M_default_append");
  623. pointer __new_start(this->_M_allocate(__len));
  624. if _GLIBCXX17_CONSTEXPR (_S_use_relocate())
  625. {
  626. __try
  627. {
  628. std::__uninitialized_default_n_a(__new_start + __size,
  629. __n, _M_get_Tp_allocator());
  630. }
  631. __catch(...)
  632. {
  633. _M_deallocate(__new_start, __len);
  634. __throw_exception_again;
  635. }
  636. _S_relocate(__old_start, __old_finish,
  637. __new_start, _M_get_Tp_allocator());
  638. }
  639. else
  640. {
  641. pointer __destroy_from = pointer();
  642. __try
  643. {
  644. std::__uninitialized_default_n_a(__new_start + __size,
  645. __n, _M_get_Tp_allocator());
  646. __destroy_from = __new_start + __size;
  647. std::__uninitialized_move_if_noexcept_a(
  648. __old_start, __old_finish,
  649. __new_start, _M_get_Tp_allocator());
  650. }
  651. __catch(...)
  652. {
  653. if (__destroy_from)
  654. std::_Destroy(__destroy_from, __destroy_from + __n,
  655. _M_get_Tp_allocator());
  656. _M_deallocate(__new_start, __len);
  657. __throw_exception_again;
  658. }
  659. std::_Destroy(__old_start, __old_finish,
  660. _M_get_Tp_allocator());
  661. }
  662. _GLIBCXX_ASAN_ANNOTATE_REINIT;
  663. _M_deallocate(__old_start,
  664. this->_M_impl._M_end_of_storage - __old_start);
  665. this->_M_impl._M_start = __new_start;
  666. this->_M_impl._M_finish = __new_start + __size + __n;
  667. this->_M_impl._M_end_of_storage = __new_start + __len;
  668. }
  669. }
  670. }
  671. template<typename _Tp, typename _Alloc>
  672. _GLIBCXX20_CONSTEXPR
  673. bool
  674. vector<_Tp, _Alloc>::
  675. _M_shrink_to_fit()
  676. {
  677. if (capacity() == size())
  678. return false;
  679. _GLIBCXX_ASAN_ANNOTATE_REINIT;
  680. return std::__shrink_to_fit_aux<vector>::_S_do_it(*this);
  681. }
  682. #endif
  683. template<typename _Tp, typename _Alloc>
  684. template<typename _InputIterator>
  685. _GLIBCXX20_CONSTEXPR
  686. void
  687. vector<_Tp, _Alloc>::
  688. _M_range_insert(iterator __pos, _InputIterator __first,
  689. _InputIterator __last, std::input_iterator_tag)
  690. {
  691. if (__pos == end())
  692. {
  693. for (; __first != __last; ++__first)
  694. insert(end(), *__first);
  695. }
  696. else if (__first != __last)
  697. {
  698. vector __tmp(__first, __last, _M_get_Tp_allocator());
  699. insert(__pos,
  700. _GLIBCXX_MAKE_MOVE_ITERATOR(__tmp.begin()),
  701. _GLIBCXX_MAKE_MOVE_ITERATOR(__tmp.end()));
  702. }
  703. }
  704. template<typename _Tp, typename _Alloc>
  705. template<typename _ForwardIterator>
  706. _GLIBCXX20_CONSTEXPR
  707. void
  708. vector<_Tp, _Alloc>::
  709. _M_range_insert(iterator __position, _ForwardIterator __first,
  710. _ForwardIterator __last, std::forward_iterator_tag)
  711. {
  712. if (__first != __last)
  713. {
  714. const size_type __n = std::distance(__first, __last);
  715. if (size_type(this->_M_impl._M_end_of_storage
  716. - this->_M_impl._M_finish) >= __n)
  717. {
  718. const size_type __elems_after = end() - __position;
  719. pointer __old_finish(this->_M_impl._M_finish);
  720. if (__elems_after > __n)
  721. {
  722. _GLIBCXX_ASAN_ANNOTATE_GROW(__n);
  723. std::__uninitialized_move_a(this->_M_impl._M_finish - __n,
  724. this->_M_impl._M_finish,
  725. this->_M_impl._M_finish,
  726. _M_get_Tp_allocator());
  727. this->_M_impl._M_finish += __n;
  728. _GLIBCXX_ASAN_ANNOTATE_GREW(__n);
  729. _GLIBCXX_MOVE_BACKWARD3(__position.base(),
  730. __old_finish - __n, __old_finish);
  731. std::copy(__first, __last, __position);
  732. }
  733. else
  734. {
  735. _ForwardIterator __mid = __first;
  736. std::advance(__mid, __elems_after);
  737. _GLIBCXX_ASAN_ANNOTATE_GROW(__n);
  738. std::__uninitialized_copy_a(__mid, __last,
  739. this->_M_impl._M_finish,
  740. _M_get_Tp_allocator());
  741. this->_M_impl._M_finish += __n - __elems_after;
  742. _GLIBCXX_ASAN_ANNOTATE_GREW(__n - __elems_after);
  743. std::__uninitialized_move_a(__position.base(),
  744. __old_finish,
  745. this->_M_impl._M_finish,
  746. _M_get_Tp_allocator());
  747. this->_M_impl._M_finish += __elems_after;
  748. _GLIBCXX_ASAN_ANNOTATE_GREW(__elems_after);
  749. std::copy(__first, __mid, __position);
  750. }
  751. }
  752. else
  753. {
  754. // Make local copies of these members because the compiler
  755. // thinks the allocator can alter them if 'this' is globally
  756. // reachable.
  757. pointer __old_start = this->_M_impl._M_start;
  758. pointer __old_finish = this->_M_impl._M_finish;
  759. const size_type __len =
  760. _M_check_len(__n, "vector::_M_range_insert");
  761. pointer __new_start(this->_M_allocate(__len));
  762. pointer __new_finish(__new_start);
  763. __try
  764. {
  765. __new_finish
  766. = std::__uninitialized_move_if_noexcept_a
  767. (__old_start, __position.base(),
  768. __new_start, _M_get_Tp_allocator());
  769. __new_finish
  770. = std::__uninitialized_copy_a(__first, __last,
  771. __new_finish,
  772. _M_get_Tp_allocator());
  773. __new_finish
  774. = std::__uninitialized_move_if_noexcept_a
  775. (__position.base(), __old_finish,
  776. __new_finish, _M_get_Tp_allocator());
  777. }
  778. __catch(...)
  779. {
  780. std::_Destroy(__new_start, __new_finish,
  781. _M_get_Tp_allocator());
  782. _M_deallocate(__new_start, __len);
  783. __throw_exception_again;
  784. }
  785. std::_Destroy(__old_start, __old_finish,
  786. _M_get_Tp_allocator());
  787. _GLIBCXX_ASAN_ANNOTATE_REINIT;
  788. _M_deallocate(__old_start,
  789. this->_M_impl._M_end_of_storage - __old_start);
  790. this->_M_impl._M_start = __new_start;
  791. this->_M_impl._M_finish = __new_finish;
  792. this->_M_impl._M_end_of_storage = __new_start + __len;
  793. }
  794. }
  795. }
  796. // vector<bool>
  797. template<typename _Alloc>
  798. _GLIBCXX20_CONSTEXPR
  799. void
  800. vector<bool, _Alloc>::
  801. _M_reallocate(size_type __n)
  802. {
  803. _Bit_pointer __q = this->_M_allocate(__n);
  804. iterator __start(std::__addressof(*__q), 0);
  805. iterator __finish(_M_copy_aligned(begin(), end(), __start));
  806. this->_M_deallocate();
  807. this->_M_impl._M_start = __start;
  808. this->_M_impl._M_finish = __finish;
  809. this->_M_impl._M_end_of_storage = __q + _S_nword(__n);
  810. }
  811. template<typename _Alloc>
  812. _GLIBCXX20_CONSTEXPR
  813. void
  814. vector<bool, _Alloc>::
  815. _M_fill_insert(iterator __position, size_type __n, bool __x)
  816. {
  817. if (__n == 0)
  818. return;
  819. if (capacity() - size() >= __n)
  820. {
  821. std::copy_backward(__position, end(),
  822. this->_M_impl._M_finish + difference_type(__n));
  823. std::fill(__position, __position + difference_type(__n), __x);
  824. this->_M_impl._M_finish += difference_type(__n);
  825. }
  826. else
  827. {
  828. const size_type __len =
  829. _M_check_len(__n, "vector<bool>::_M_fill_insert");
  830. _Bit_pointer __q = this->_M_allocate(__len);
  831. iterator __start(std::__addressof(*__q), 0);
  832. iterator __i = _M_copy_aligned(begin(), __position, __start);
  833. std::fill(__i, __i + difference_type(__n), __x);
  834. iterator __finish = std::copy(__position, end(),
  835. __i + difference_type(__n));
  836. this->_M_deallocate();
  837. this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
  838. this->_M_impl._M_start = __start;
  839. this->_M_impl._M_finish = __finish;
  840. }
  841. }
  842. template<typename _Alloc>
  843. template<typename _ForwardIterator>
  844. _GLIBCXX20_CONSTEXPR
  845. void
  846. vector<bool, _Alloc>::
  847. _M_insert_range(iterator __position, _ForwardIterator __first,
  848. _ForwardIterator __last, std::forward_iterator_tag)
  849. {
  850. if (__first != __last)
  851. {
  852. size_type __n = std::distance(__first, __last);
  853. if (capacity() - size() >= __n)
  854. {
  855. std::copy_backward(__position, end(),
  856. this->_M_impl._M_finish
  857. + difference_type(__n));
  858. std::copy(__first, __last, __position);
  859. this->_M_impl._M_finish += difference_type(__n);
  860. }
  861. else
  862. {
  863. const size_type __len =
  864. _M_check_len(__n, "vector<bool>::_M_insert_range");
  865. const iterator __begin = begin(), __end = end();
  866. _Bit_pointer __q = this->_M_allocate(__len);
  867. iterator __start(std::__addressof(*__q), 0);
  868. iterator __i = _M_copy_aligned(__begin, __position, __start);
  869. __i = std::copy(__first, __last, __i);
  870. iterator __finish = std::copy(__position, __end, __i);
  871. this->_M_deallocate();
  872. this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
  873. this->_M_impl._M_start = __start;
  874. this->_M_impl._M_finish = __finish;
  875. }
  876. }
  877. }
  878. template<typename _Alloc>
  879. _GLIBCXX20_CONSTEXPR
  880. void
  881. vector<bool, _Alloc>::
  882. _M_insert_aux(iterator __position, bool __x)
  883. {
  884. if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_addr())
  885. {
  886. std::copy_backward(__position, this->_M_impl._M_finish,
  887. this->_M_impl._M_finish + 1);
  888. *__position = __x;
  889. ++this->_M_impl._M_finish;
  890. }
  891. else
  892. {
  893. const size_type __len =
  894. _M_check_len(size_type(1), "vector<bool>::_M_insert_aux");
  895. _Bit_pointer __q = this->_M_allocate(__len);
  896. iterator __start(std::__addressof(*__q), 0);
  897. iterator __i = _M_copy_aligned(begin(), __position, __start);
  898. *__i++ = __x;
  899. iterator __finish = std::copy(__position, end(), __i);
  900. this->_M_deallocate();
  901. this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
  902. this->_M_impl._M_start = __start;
  903. this->_M_impl._M_finish = __finish;
  904. }
  905. }
  906. template<typename _Alloc>
  907. _GLIBCXX20_CONSTEXPR
  908. typename vector<bool, _Alloc>::iterator
  909. vector<bool, _Alloc>::
  910. _M_erase(iterator __position)
  911. {
  912. if (__position + 1 != end())
  913. std::copy(__position + 1, end(), __position);
  914. --this->_M_impl._M_finish;
  915. return __position;
  916. }
  917. template<typename _Alloc>
  918. _GLIBCXX20_CONSTEXPR
  919. typename vector<bool, _Alloc>::iterator
  920. vector<bool, _Alloc>::
  921. _M_erase(iterator __first, iterator __last)
  922. {
  923. if (__first != __last)
  924. _M_erase_at_end(std::copy(__last, end(), __first));
  925. return __first;
  926. }
  927. #if __cplusplus >= 201103L
  928. template<typename _Alloc>
  929. _GLIBCXX20_CONSTEXPR
  930. bool
  931. vector<bool, _Alloc>::
  932. _M_shrink_to_fit()
  933. {
  934. if (capacity() - size() < int(_S_word_bit))
  935. return false;
  936. __try
  937. {
  938. if (size_type __n = size())
  939. _M_reallocate(__n);
  940. else
  941. {
  942. this->_M_deallocate();
  943. this->_M_impl._M_reset();
  944. }
  945. return true;
  946. }
  947. __catch(...)
  948. { return false; }
  949. }
  950. #endif
  951. _GLIBCXX_END_NAMESPACE_CONTAINER
  952. _GLIBCXX_END_NAMESPACE_VERSION
  953. } // namespace std
  954. #if __cplusplus >= 201103L
  955. namespace std _GLIBCXX_VISIBILITY(default)
  956. {
  957. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  958. template<typename _Alloc>
  959. size_t
  960. hash<_GLIBCXX_STD_C::vector<bool, _Alloc>>::
  961. operator()(const _GLIBCXX_STD_C::vector<bool, _Alloc>& __b) const noexcept
  962. {
  963. size_t __hash = 0;
  964. const size_t __words = __b.size() / _S_word_bit;
  965. if (__words)
  966. {
  967. const size_t __clength = __words * sizeof(_Bit_type);
  968. __hash = std::_Hash_impl::hash(__b._M_impl._M_start._M_p, __clength);
  969. }
  970. const size_t __extrabits = __b.size() % _S_word_bit;
  971. if (__extrabits)
  972. {
  973. _Bit_type __hiword = *__b._M_impl._M_finish._M_p;
  974. __hiword &= ~((~static_cast<_Bit_type>(0)) << __extrabits);
  975. const size_t __clength
  976. = (__extrabits + __CHAR_BIT__ - 1) / __CHAR_BIT__;
  977. if (__words)
  978. __hash = std::_Hash_impl::hash(&__hiword, __clength, __hash);
  979. else
  980. __hash = std::_Hash_impl::hash(&__hiword, __clength);
  981. }
  982. return __hash;
  983. }
  984. _GLIBCXX_END_NAMESPACE_VERSION
  985. } // namespace std
  986. #endif // C++11
  987. #undef _GLIBCXX_ASAN_ANNOTATE_REINIT
  988. #undef _GLIBCXX_ASAN_ANNOTATE_GROW
  989. #undef _GLIBCXX_ASAN_ANNOTATE_GREW
  990. #undef _GLIBCXX_ASAN_ANNOTATE_SHRINK
  991. #endif /* _VECTOR_TCC */