vector.tcc 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. // Vector implementation (out of line) -*- C++ -*-
  2. // Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. *
  22. * Copyright (c) 1994
  23. * Hewlett-Packard Company
  24. *
  25. * Permission to use, copy, modify, distribute and sell this software
  26. * and its documentation for any purpose is hereby granted without fee,
  27. * provided that the above copyright notice appear in all copies and
  28. * that both that copyright notice and this permission notice appear
  29. * in supporting documentation. Hewlett-Packard Company makes no
  30. * representations about the suitability of this software for any
  31. * purpose. It is provided "as is" without express or implied warranty.
  32. *
  33. *
  34. * Copyright (c) 1996
  35. * Silicon Graphics Computer Systems, Inc.
  36. *
  37. * Permission to use, copy, modify, distribute and sell this software
  38. * and its documentation for any purpose is hereby granted without fee,
  39. * provided that the above copyright notice appear in all copies and
  40. * that both that copyright notice and this permission notice appear
  41. * in supporting documentation. Silicon Graphics makes no
  42. * representations about the suitability of this software for any
  43. * purpose. It is provided "as is" without express or implied warranty.
  44. */
  45. /** @file bits/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. if (__position == end())
  132. {
  133. _GLIBCXX_ASAN_ANNOTATE_GROW(1);
  134. _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
  135. __x);
  136. ++this->_M_impl._M_finish;
  137. _GLIBCXX_ASAN_ANNOTATE_GREW(1);
  138. }
  139. else
  140. {
  141. #if __cplusplus >= 201103L
  142. const auto __pos = begin() + (__position - cbegin());
  143. // __x could be an existing element of this vector, so make a
  144. // copy of it before _M_insert_aux moves elements around.
  145. _Temporary_value __x_copy(this, __x);
  146. _M_insert_aux(__pos, std::move(__x_copy._M_val()));
  147. #else
  148. _M_insert_aux(__position, __x);
  149. #endif
  150. }
  151. else
  152. #if __cplusplus >= 201103L
  153. _M_realloc_insert(begin() + (__position - cbegin()), __x);
  154. #else
  155. _M_realloc_insert(__position, __x);
  156. #endif
  157. return iterator(this->_M_impl._M_start + __n);
  158. }
  159. template<typename _Tp, typename _Alloc>
  160. _GLIBCXX20_CONSTEXPR
  161. typename vector<_Tp, _Alloc>::iterator
  162. vector<_Tp, _Alloc>::
  163. _M_erase(iterator __position)
  164. {
  165. if (__position + 1 != end())
  166. _GLIBCXX_MOVE3(__position + 1, end(), __position);
  167. --this->_M_impl._M_finish;
  168. _Alloc_traits::destroy(this->_M_impl, this->_M_impl._M_finish);
  169. _GLIBCXX_ASAN_ANNOTATE_SHRINK(1);
  170. return __position;
  171. }
  172. template<typename _Tp, typename _Alloc>
  173. _GLIBCXX20_CONSTEXPR
  174. typename vector<_Tp, _Alloc>::iterator
  175. vector<_Tp, _Alloc>::
  176. _M_erase(iterator __first, iterator __last)
  177. {
  178. if (__first != __last)
  179. {
  180. if (__last != end())
  181. _GLIBCXX_MOVE3(__last, end(), __first);
  182. _M_erase_at_end(__first.base() + (end() - __last));
  183. }
  184. return __first;
  185. }
  186. template<typename _Tp, typename _Alloc>
  187. _GLIBCXX20_CONSTEXPR
  188. vector<_Tp, _Alloc>&
  189. vector<_Tp, _Alloc>::
  190. operator=(const vector<_Tp, _Alloc>& __x)
  191. {
  192. if (std::__addressof(__x) != this)
  193. {
  194. _GLIBCXX_ASAN_ANNOTATE_REINIT;
  195. #if __cplusplus >= 201103L
  196. if (_Alloc_traits::_S_propagate_on_copy_assign())
  197. {
  198. if (!_Alloc_traits::_S_always_equal()
  199. && _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
  200. {
  201. // replacement allocator cannot free existing storage
  202. this->clear();
  203. _M_deallocate(this->_M_impl._M_start,
  204. this->_M_impl._M_end_of_storage
  205. - this->_M_impl._M_start);
  206. this->_M_impl._M_start = nullptr;
  207. this->_M_impl._M_finish = nullptr;
  208. this->_M_impl._M_end_of_storage = nullptr;
  209. }
  210. std::__alloc_on_copy(_M_get_Tp_allocator(),
  211. __x._M_get_Tp_allocator());
  212. }
  213. #endif
  214. const size_type __xlen = __x.size();
  215. if (__xlen > capacity())
  216. {
  217. pointer __tmp = _M_allocate_and_copy(__xlen, __x.begin(),
  218. __x.end());
  219. std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
  220. _M_get_Tp_allocator());
  221. _M_deallocate(this->_M_impl._M_start,
  222. this->_M_impl._M_end_of_storage
  223. - this->_M_impl._M_start);
  224. this->_M_impl._M_start = __tmp;
  225. this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __xlen;
  226. }
  227. else if (size() >= __xlen)
  228. {
  229. std::_Destroy(std::copy(__x.begin(), __x.end(), begin()),
  230. end(), _M_get_Tp_allocator());
  231. }
  232. else
  233. {
  234. std::copy(__x._M_impl._M_start, __x._M_impl._M_start + size(),
  235. this->_M_impl._M_start);
  236. std::__uninitialized_copy_a(__x._M_impl._M_start + size(),
  237. __x._M_impl._M_finish,
  238. this->_M_impl._M_finish,
  239. _M_get_Tp_allocator());
  240. }
  241. this->_M_impl._M_finish = this->_M_impl._M_start + __xlen;
  242. }
  243. return *this;
  244. }
  245. template<typename _Tp, typename _Alloc>
  246. _GLIBCXX20_CONSTEXPR
  247. void
  248. vector<_Tp, _Alloc>::
  249. _M_fill_assign(size_t __n, const value_type& __val)
  250. {
  251. if (__n > capacity())
  252. {
  253. vector __tmp(__n, __val, _M_get_Tp_allocator());
  254. __tmp._M_impl._M_swap_data(this->_M_impl);
  255. }
  256. else if (__n > size())
  257. {
  258. std::fill(begin(), end(), __val);
  259. const size_type __add = __n - size();
  260. _GLIBCXX_ASAN_ANNOTATE_GROW(__add);
  261. this->_M_impl._M_finish =
  262. std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
  263. __add, __val, _M_get_Tp_allocator());
  264. _GLIBCXX_ASAN_ANNOTATE_GREW(__add);
  265. }
  266. else
  267. _M_erase_at_end(std::fill_n(this->_M_impl._M_start, __n, __val));
  268. }
  269. template<typename _Tp, typename _Alloc>
  270. template<typename _InputIterator>
  271. _GLIBCXX20_CONSTEXPR
  272. void
  273. vector<_Tp, _Alloc>::
  274. _M_assign_aux(_InputIterator __first, _InputIterator __last,
  275. std::input_iterator_tag)
  276. {
  277. pointer __cur(this->_M_impl._M_start);
  278. for (; __first != __last && __cur != this->_M_impl._M_finish;
  279. ++__cur, (void)++__first)
  280. *__cur = *__first;
  281. if (__first == __last)
  282. _M_erase_at_end(__cur);
  283. else
  284. _M_range_insert(end(), __first, __last,
  285. std::__iterator_category(__first));
  286. }
  287. template<typename _Tp, typename _Alloc>
  288. template<typename _ForwardIterator>
  289. _GLIBCXX20_CONSTEXPR
  290. void
  291. vector<_Tp, _Alloc>::
  292. _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
  293. std::forward_iterator_tag)
  294. {
  295. const size_type __len = std::distance(__first, __last);
  296. if (__len > capacity())
  297. {
  298. _S_check_init_len(__len, _M_get_Tp_allocator());
  299. pointer __tmp(_M_allocate_and_copy(__len, __first, __last));
  300. std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
  301. _M_get_Tp_allocator());
  302. _GLIBCXX_ASAN_ANNOTATE_REINIT;
  303. _M_deallocate(this->_M_impl._M_start,
  304. this->_M_impl._M_end_of_storage
  305. - this->_M_impl._M_start);
  306. this->_M_impl._M_start = __tmp;
  307. this->_M_impl._M_finish = this->_M_impl._M_start + __len;
  308. this->_M_impl._M_end_of_storage = this->_M_impl._M_finish;
  309. }
  310. else if (size() >= __len)
  311. _M_erase_at_end(std::copy(__first, __last, this->_M_impl._M_start));
  312. else
  313. {
  314. _ForwardIterator __mid = __first;
  315. std::advance(__mid, size());
  316. std::copy(__first, __mid, this->_M_impl._M_start);
  317. const size_type __attribute__((__unused__)) __n = __len - size();
  318. _GLIBCXX_ASAN_ANNOTATE_GROW(__n);
  319. this->_M_impl._M_finish =
  320. std::__uninitialized_copy_a(__mid, __last,
  321. this->_M_impl._M_finish,
  322. _M_get_Tp_allocator());
  323. _GLIBCXX_ASAN_ANNOTATE_GREW(__n);
  324. }
  325. }
  326. #if __cplusplus >= 201103L
  327. template<typename _Tp, typename _Alloc>
  328. _GLIBCXX20_CONSTEXPR
  329. auto
  330. vector<_Tp, _Alloc>::
  331. _M_insert_rval(const_iterator __position, value_type&& __v) -> iterator
  332. {
  333. const auto __n = __position - cbegin();
  334. if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
  335. if (__position == cend())
  336. {
  337. _GLIBCXX_ASAN_ANNOTATE_GROW(1);
  338. _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
  339. std::move(__v));
  340. ++this->_M_impl._M_finish;
  341. _GLIBCXX_ASAN_ANNOTATE_GREW(1);
  342. }
  343. else
  344. _M_insert_aux(begin() + __n, std::move(__v));
  345. else
  346. _M_realloc_insert(begin() + __n, std::move(__v));
  347. return iterator(this->_M_impl._M_start + __n);
  348. }
  349. template<typename _Tp, typename _Alloc>
  350. template<typename... _Args>
  351. _GLIBCXX20_CONSTEXPR
  352. auto
  353. vector<_Tp, _Alloc>::
  354. _M_emplace_aux(const_iterator __position, _Args&&... __args)
  355. -> iterator
  356. {
  357. const auto __n = __position - cbegin();
  358. if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
  359. if (__position == cend())
  360. {
  361. _GLIBCXX_ASAN_ANNOTATE_GROW(1);
  362. _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
  363. std::forward<_Args>(__args)...);
  364. ++this->_M_impl._M_finish;
  365. _GLIBCXX_ASAN_ANNOTATE_GREW(1);
  366. }
  367. else
  368. {
  369. // We need to construct a temporary because something in __args...
  370. // could alias one of the elements of the container and so we
  371. // need to use it before _M_insert_aux moves elements around.
  372. _Temporary_value __tmp(this, std::forward<_Args>(__args)...);
  373. _M_insert_aux(begin() + __n, std::move(__tmp._M_val()));
  374. }
  375. else
  376. _M_realloc_insert(begin() + __n, std::forward<_Args>(__args)...);
  377. return iterator(this->_M_impl._M_start + __n);
  378. }
  379. template<typename _Tp, typename _Alloc>
  380. template<typename _Arg>
  381. _GLIBCXX20_CONSTEXPR
  382. void
  383. vector<_Tp, _Alloc>::
  384. _M_insert_aux(iterator __position, _Arg&& __arg)
  385. #else
  386. template<typename _Tp, typename _Alloc>
  387. void
  388. vector<_Tp, _Alloc>::
  389. _M_insert_aux(iterator __position, const _Tp& __x)
  390. #endif
  391. {
  392. _GLIBCXX_ASAN_ANNOTATE_GROW(1);
  393. _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
  394. _GLIBCXX_MOVE(*(this->_M_impl._M_finish - 1)));
  395. ++this->_M_impl._M_finish;
  396. _GLIBCXX_ASAN_ANNOTATE_GREW(1);
  397. #if __cplusplus < 201103L
  398. _Tp __x_copy = __x;
  399. #endif
  400. _GLIBCXX_MOVE_BACKWARD3(__position.base(),
  401. this->_M_impl._M_finish - 2,
  402. this->_M_impl._M_finish - 1);
  403. #if __cplusplus < 201103L
  404. *__position = __x_copy;
  405. #else
  406. *__position = std::forward<_Arg>(__arg);
  407. #endif
  408. }
  409. #if __cplusplus >= 201103L
  410. template<typename _Tp, typename _Alloc>
  411. template<typename... _Args>
  412. _GLIBCXX20_CONSTEXPR
  413. void
  414. vector<_Tp, _Alloc>::
  415. _M_realloc_insert(iterator __position, _Args&&... __args)
  416. #else
  417. template<typename _Tp, typename _Alloc>
  418. void
  419. vector<_Tp, _Alloc>::
  420. _M_realloc_insert(iterator __position, const _Tp& __x)
  421. #endif
  422. {
  423. const size_type __len =
  424. _M_check_len(size_type(1), "vector::_M_realloc_insert");
  425. pointer __old_start = this->_M_impl._M_start;
  426. pointer __old_finish = this->_M_impl._M_finish;
  427. const size_type __elems_before = __position - begin();
  428. pointer __new_start(this->_M_allocate(__len));
  429. pointer __new_finish(__new_start);
  430. __try
  431. {
  432. // The order of the three operations is dictated by the C++11
  433. // case, where the moves could alter a new element belonging
  434. // to the existing vector. This is an issue only for callers
  435. // taking the element by lvalue ref (see last bullet of C++11
  436. // [res.on.arguments]).
  437. _Alloc_traits::construct(this->_M_impl,
  438. __new_start + __elems_before,
  439. #if __cplusplus >= 201103L
  440. std::forward<_Args>(__args)...);
  441. #else
  442. __x);
  443. #endif
  444. __new_finish = pointer();
  445. #if __cplusplus >= 201103L
  446. if _GLIBCXX17_CONSTEXPR (_S_use_relocate())
  447. {
  448. __new_finish = _S_relocate(__old_start, __position.base(),
  449. __new_start, _M_get_Tp_allocator());
  450. ++__new_finish;
  451. __new_finish = _S_relocate(__position.base(), __old_finish,
  452. __new_finish, _M_get_Tp_allocator());
  453. }
  454. else
  455. #endif
  456. {
  457. __new_finish
  458. = std::__uninitialized_move_if_noexcept_a
  459. (__old_start, __position.base(),
  460. __new_start, _M_get_Tp_allocator());
  461. ++__new_finish;
  462. __new_finish
  463. = std::__uninitialized_move_if_noexcept_a
  464. (__position.base(), __old_finish,
  465. __new_finish, _M_get_Tp_allocator());
  466. }
  467. }
  468. __catch(...)
  469. {
  470. if (!__new_finish)
  471. _Alloc_traits::destroy(this->_M_impl,
  472. __new_start + __elems_before);
  473. else
  474. std::_Destroy(__new_start, __new_finish, _M_get_Tp_allocator());
  475. _M_deallocate(__new_start, __len);
  476. __throw_exception_again;
  477. }
  478. #if __cplusplus >= 201103L
  479. if _GLIBCXX17_CONSTEXPR (!_S_use_relocate())
  480. #endif
  481. std::_Destroy(__old_start, __old_finish, _M_get_Tp_allocator());
  482. _GLIBCXX_ASAN_ANNOTATE_REINIT;
  483. _M_deallocate(__old_start,
  484. this->_M_impl._M_end_of_storage - __old_start);
  485. this->_M_impl._M_start = __new_start;
  486. this->_M_impl._M_finish = __new_finish;
  487. this->_M_impl._M_end_of_storage = __new_start + __len;
  488. }
  489. template<typename _Tp, typename _Alloc>
  490. _GLIBCXX20_CONSTEXPR
  491. void
  492. vector<_Tp, _Alloc>::
  493. _M_fill_insert(iterator __position, size_type __n, const value_type& __x)
  494. {
  495. if (__n != 0)
  496. {
  497. if (size_type(this->_M_impl._M_end_of_storage
  498. - this->_M_impl._M_finish) >= __n)
  499. {
  500. #if __cplusplus < 201103L
  501. value_type __x_copy = __x;
  502. #else
  503. _Temporary_value __tmp(this, __x);
  504. value_type& __x_copy = __tmp._M_val();
  505. #endif
  506. const size_type __elems_after = end() - __position;
  507. pointer __old_finish(this->_M_impl._M_finish);
  508. if (__elems_after > __n)
  509. {
  510. _GLIBCXX_ASAN_ANNOTATE_GROW(__n);
  511. std::__uninitialized_move_a(this->_M_impl._M_finish - __n,
  512. this->_M_impl._M_finish,
  513. this->_M_impl._M_finish,
  514. _M_get_Tp_allocator());
  515. this->_M_impl._M_finish += __n;
  516. _GLIBCXX_ASAN_ANNOTATE_GREW(__n);
  517. _GLIBCXX_MOVE_BACKWARD3(__position.base(),
  518. __old_finish - __n, __old_finish);
  519. std::fill(__position.base(), __position.base() + __n,
  520. __x_copy);
  521. }
  522. else
  523. {
  524. _GLIBCXX_ASAN_ANNOTATE_GROW(__n);
  525. this->_M_impl._M_finish =
  526. std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
  527. __n - __elems_after,
  528. __x_copy,
  529. _M_get_Tp_allocator());
  530. _GLIBCXX_ASAN_ANNOTATE_GREW(__n - __elems_after);
  531. std::__uninitialized_move_a(__position.base(), __old_finish,
  532. this->_M_impl._M_finish,
  533. _M_get_Tp_allocator());
  534. this->_M_impl._M_finish += __elems_after;
  535. _GLIBCXX_ASAN_ANNOTATE_GREW(__elems_after);
  536. std::fill(__position.base(), __old_finish, __x_copy);
  537. }
  538. }
  539. else
  540. {
  541. const size_type __len =
  542. _M_check_len(__n, "vector::_M_fill_insert");
  543. const size_type __elems_before = __position - begin();
  544. pointer __new_start(this->_M_allocate(__len));
  545. pointer __new_finish(__new_start);
  546. __try
  547. {
  548. // See _M_realloc_insert above.
  549. std::__uninitialized_fill_n_a(__new_start + __elems_before,
  550. __n, __x,
  551. _M_get_Tp_allocator());
  552. __new_finish = pointer();
  553. __new_finish
  554. = std::__uninitialized_move_if_noexcept_a
  555. (this->_M_impl._M_start, __position.base(),
  556. __new_start, _M_get_Tp_allocator());
  557. __new_finish += __n;
  558. __new_finish
  559. = std::__uninitialized_move_if_noexcept_a
  560. (__position.base(), this->_M_impl._M_finish,
  561. __new_finish, _M_get_Tp_allocator());
  562. }
  563. __catch(...)
  564. {
  565. if (!__new_finish)
  566. std::_Destroy(__new_start + __elems_before,
  567. __new_start + __elems_before + __n,
  568. _M_get_Tp_allocator());
  569. else
  570. std::_Destroy(__new_start, __new_finish,
  571. _M_get_Tp_allocator());
  572. _M_deallocate(__new_start, __len);
  573. __throw_exception_again;
  574. }
  575. std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
  576. _M_get_Tp_allocator());
  577. _GLIBCXX_ASAN_ANNOTATE_REINIT;
  578. _M_deallocate(this->_M_impl._M_start,
  579. this->_M_impl._M_end_of_storage
  580. - this->_M_impl._M_start);
  581. this->_M_impl._M_start = __new_start;
  582. this->_M_impl._M_finish = __new_finish;
  583. this->_M_impl._M_end_of_storage = __new_start + __len;
  584. }
  585. }
  586. }
  587. #if __cplusplus >= 201103L
  588. template<typename _Tp, typename _Alloc>
  589. _GLIBCXX20_CONSTEXPR
  590. void
  591. vector<_Tp, _Alloc>::
  592. _M_default_append(size_type __n)
  593. {
  594. if (__n != 0)
  595. {
  596. const size_type __size = size();
  597. size_type __navail = size_type(this->_M_impl._M_end_of_storage
  598. - this->_M_impl._M_finish);
  599. if (__size > max_size() || __navail > max_size() - __size)
  600. __builtin_unreachable();
  601. if (__navail >= __n)
  602. {
  603. _GLIBCXX_ASAN_ANNOTATE_GROW(__n);
  604. this->_M_impl._M_finish =
  605. std::__uninitialized_default_n_a(this->_M_impl._M_finish,
  606. __n, _M_get_Tp_allocator());
  607. _GLIBCXX_ASAN_ANNOTATE_GREW(__n);
  608. }
  609. else
  610. {
  611. const size_type __len =
  612. _M_check_len(__n, "vector::_M_default_append");
  613. pointer __new_start(this->_M_allocate(__len));
  614. if _GLIBCXX17_CONSTEXPR (_S_use_relocate())
  615. {
  616. __try
  617. {
  618. std::__uninitialized_default_n_a(__new_start + __size,
  619. __n, _M_get_Tp_allocator());
  620. }
  621. __catch(...)
  622. {
  623. _M_deallocate(__new_start, __len);
  624. __throw_exception_again;
  625. }
  626. _S_relocate(this->_M_impl._M_start, this->_M_impl._M_finish,
  627. __new_start, _M_get_Tp_allocator());
  628. }
  629. else
  630. {
  631. pointer __destroy_from = pointer();
  632. __try
  633. {
  634. std::__uninitialized_default_n_a(__new_start + __size,
  635. __n, _M_get_Tp_allocator());
  636. __destroy_from = __new_start + __size;
  637. std::__uninitialized_move_if_noexcept_a(
  638. this->_M_impl._M_start, this->_M_impl._M_finish,
  639. __new_start, _M_get_Tp_allocator());
  640. }
  641. __catch(...)
  642. {
  643. if (__destroy_from)
  644. std::_Destroy(__destroy_from, __destroy_from + __n,
  645. _M_get_Tp_allocator());
  646. _M_deallocate(__new_start, __len);
  647. __throw_exception_again;
  648. }
  649. std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
  650. _M_get_Tp_allocator());
  651. }
  652. _GLIBCXX_ASAN_ANNOTATE_REINIT;
  653. _M_deallocate(this->_M_impl._M_start,
  654. this->_M_impl._M_end_of_storage
  655. - this->_M_impl._M_start);
  656. this->_M_impl._M_start = __new_start;
  657. this->_M_impl._M_finish = __new_start + __size + __n;
  658. this->_M_impl._M_end_of_storage = __new_start + __len;
  659. }
  660. }
  661. }
  662. template<typename _Tp, typename _Alloc>
  663. _GLIBCXX20_CONSTEXPR
  664. bool
  665. vector<_Tp, _Alloc>::
  666. _M_shrink_to_fit()
  667. {
  668. if (capacity() == size())
  669. return false;
  670. _GLIBCXX_ASAN_ANNOTATE_REINIT;
  671. return std::__shrink_to_fit_aux<vector>::_S_do_it(*this);
  672. }
  673. #endif
  674. template<typename _Tp, typename _Alloc>
  675. template<typename _InputIterator>
  676. _GLIBCXX20_CONSTEXPR
  677. void
  678. vector<_Tp, _Alloc>::
  679. _M_range_insert(iterator __pos, _InputIterator __first,
  680. _InputIterator __last, std::input_iterator_tag)
  681. {
  682. if (__pos == end())
  683. {
  684. for (; __first != __last; ++__first)
  685. insert(end(), *__first);
  686. }
  687. else if (__first != __last)
  688. {
  689. vector __tmp(__first, __last, _M_get_Tp_allocator());
  690. insert(__pos,
  691. _GLIBCXX_MAKE_MOVE_ITERATOR(__tmp.begin()),
  692. _GLIBCXX_MAKE_MOVE_ITERATOR(__tmp.end()));
  693. }
  694. }
  695. template<typename _Tp, typename _Alloc>
  696. template<typename _ForwardIterator>
  697. _GLIBCXX20_CONSTEXPR
  698. void
  699. vector<_Tp, _Alloc>::
  700. _M_range_insert(iterator __position, _ForwardIterator __first,
  701. _ForwardIterator __last, std::forward_iterator_tag)
  702. {
  703. if (__first != __last)
  704. {
  705. const size_type __n = std::distance(__first, __last);
  706. if (size_type(this->_M_impl._M_end_of_storage
  707. - this->_M_impl._M_finish) >= __n)
  708. {
  709. const size_type __elems_after = end() - __position;
  710. pointer __old_finish(this->_M_impl._M_finish);
  711. if (__elems_after > __n)
  712. {
  713. _GLIBCXX_ASAN_ANNOTATE_GROW(__n);
  714. std::__uninitialized_move_a(this->_M_impl._M_finish - __n,
  715. this->_M_impl._M_finish,
  716. this->_M_impl._M_finish,
  717. _M_get_Tp_allocator());
  718. this->_M_impl._M_finish += __n;
  719. _GLIBCXX_ASAN_ANNOTATE_GREW(__n);
  720. _GLIBCXX_MOVE_BACKWARD3(__position.base(),
  721. __old_finish - __n, __old_finish);
  722. std::copy(__first, __last, __position);
  723. }
  724. else
  725. {
  726. _ForwardIterator __mid = __first;
  727. std::advance(__mid, __elems_after);
  728. _GLIBCXX_ASAN_ANNOTATE_GROW(__n);
  729. std::__uninitialized_copy_a(__mid, __last,
  730. this->_M_impl._M_finish,
  731. _M_get_Tp_allocator());
  732. this->_M_impl._M_finish += __n - __elems_after;
  733. _GLIBCXX_ASAN_ANNOTATE_GREW(__n - __elems_after);
  734. std::__uninitialized_move_a(__position.base(),
  735. __old_finish,
  736. this->_M_impl._M_finish,
  737. _M_get_Tp_allocator());
  738. this->_M_impl._M_finish += __elems_after;
  739. _GLIBCXX_ASAN_ANNOTATE_GREW(__elems_after);
  740. std::copy(__first, __mid, __position);
  741. }
  742. }
  743. else
  744. {
  745. const size_type __len =
  746. _M_check_len(__n, "vector::_M_range_insert");
  747. pointer __new_start(this->_M_allocate(__len));
  748. pointer __new_finish(__new_start);
  749. __try
  750. {
  751. __new_finish
  752. = std::__uninitialized_move_if_noexcept_a
  753. (this->_M_impl._M_start, __position.base(),
  754. __new_start, _M_get_Tp_allocator());
  755. __new_finish
  756. = std::__uninitialized_copy_a(__first, __last,
  757. __new_finish,
  758. _M_get_Tp_allocator());
  759. __new_finish
  760. = std::__uninitialized_move_if_noexcept_a
  761. (__position.base(), this->_M_impl._M_finish,
  762. __new_finish, _M_get_Tp_allocator());
  763. }
  764. __catch(...)
  765. {
  766. std::_Destroy(__new_start, __new_finish,
  767. _M_get_Tp_allocator());
  768. _M_deallocate(__new_start, __len);
  769. __throw_exception_again;
  770. }
  771. std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
  772. _M_get_Tp_allocator());
  773. _GLIBCXX_ASAN_ANNOTATE_REINIT;
  774. _M_deallocate(this->_M_impl._M_start,
  775. this->_M_impl._M_end_of_storage
  776. - this->_M_impl._M_start);
  777. this->_M_impl._M_start = __new_start;
  778. this->_M_impl._M_finish = __new_finish;
  779. this->_M_impl._M_end_of_storage = __new_start + __len;
  780. }
  781. }
  782. }
  783. // vector<bool>
  784. template<typename _Alloc>
  785. _GLIBCXX20_CONSTEXPR
  786. void
  787. vector<bool, _Alloc>::
  788. _M_reallocate(size_type __n)
  789. {
  790. _Bit_pointer __q = this->_M_allocate(__n);
  791. iterator __start(std::__addressof(*__q), 0);
  792. iterator __finish(_M_copy_aligned(begin(), end(), __start));
  793. this->_M_deallocate();
  794. this->_M_impl._M_start = __start;
  795. this->_M_impl._M_finish = __finish;
  796. this->_M_impl._M_end_of_storage = __q + _S_nword(__n);
  797. }
  798. template<typename _Alloc>
  799. _GLIBCXX20_CONSTEXPR
  800. void
  801. vector<bool, _Alloc>::
  802. _M_fill_insert(iterator __position, size_type __n, bool __x)
  803. {
  804. if (__n == 0)
  805. return;
  806. if (capacity() - size() >= __n)
  807. {
  808. std::copy_backward(__position, end(),
  809. this->_M_impl._M_finish + difference_type(__n));
  810. std::fill(__position, __position + difference_type(__n), __x);
  811. this->_M_impl._M_finish += difference_type(__n);
  812. }
  813. else
  814. {
  815. const size_type __len =
  816. _M_check_len(__n, "vector<bool>::_M_fill_insert");
  817. _Bit_pointer __q = this->_M_allocate(__len);
  818. iterator __start(std::__addressof(*__q), 0);
  819. iterator __i = _M_copy_aligned(begin(), __position, __start);
  820. std::fill(__i, __i + difference_type(__n), __x);
  821. iterator __finish = std::copy(__position, end(),
  822. __i + difference_type(__n));
  823. this->_M_deallocate();
  824. this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
  825. this->_M_impl._M_start = __start;
  826. this->_M_impl._M_finish = __finish;
  827. }
  828. }
  829. template<typename _Alloc>
  830. template<typename _ForwardIterator>
  831. _GLIBCXX20_CONSTEXPR
  832. void
  833. vector<bool, _Alloc>::
  834. _M_insert_range(iterator __position, _ForwardIterator __first,
  835. _ForwardIterator __last, std::forward_iterator_tag)
  836. {
  837. if (__first != __last)
  838. {
  839. size_type __n = std::distance(__first, __last);
  840. if (capacity() - size() >= __n)
  841. {
  842. std::copy_backward(__position, end(),
  843. this->_M_impl._M_finish
  844. + difference_type(__n));
  845. std::copy(__first, __last, __position);
  846. this->_M_impl._M_finish += difference_type(__n);
  847. }
  848. else
  849. {
  850. const size_type __len =
  851. _M_check_len(__n, "vector<bool>::_M_insert_range");
  852. _Bit_pointer __q = this->_M_allocate(__len);
  853. iterator __start(std::__addressof(*__q), 0);
  854. iterator __i = _M_copy_aligned(begin(), __position, __start);
  855. __i = std::copy(__first, __last, __i);
  856. iterator __finish = std::copy(__position, end(), __i);
  857. this->_M_deallocate();
  858. this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
  859. this->_M_impl._M_start = __start;
  860. this->_M_impl._M_finish = __finish;
  861. }
  862. }
  863. }
  864. template<typename _Alloc>
  865. _GLIBCXX20_CONSTEXPR
  866. void
  867. vector<bool, _Alloc>::
  868. _M_insert_aux(iterator __position, bool __x)
  869. {
  870. if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_addr())
  871. {
  872. std::copy_backward(__position, this->_M_impl._M_finish,
  873. this->_M_impl._M_finish + 1);
  874. *__position = __x;
  875. ++this->_M_impl._M_finish;
  876. }
  877. else
  878. {
  879. const size_type __len =
  880. _M_check_len(size_type(1), "vector<bool>::_M_insert_aux");
  881. _Bit_pointer __q = this->_M_allocate(__len);
  882. iterator __start(std::__addressof(*__q), 0);
  883. iterator __i = _M_copy_aligned(begin(), __position, __start);
  884. *__i++ = __x;
  885. iterator __finish = std::copy(__position, end(), __i);
  886. this->_M_deallocate();
  887. this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
  888. this->_M_impl._M_start = __start;
  889. this->_M_impl._M_finish = __finish;
  890. }
  891. }
  892. template<typename _Alloc>
  893. _GLIBCXX20_CONSTEXPR
  894. typename vector<bool, _Alloc>::iterator
  895. vector<bool, _Alloc>::
  896. _M_erase(iterator __position)
  897. {
  898. if (__position + 1 != end())
  899. std::copy(__position + 1, end(), __position);
  900. --this->_M_impl._M_finish;
  901. return __position;
  902. }
  903. template<typename _Alloc>
  904. _GLIBCXX20_CONSTEXPR
  905. typename vector<bool, _Alloc>::iterator
  906. vector<bool, _Alloc>::
  907. _M_erase(iterator __first, iterator __last)
  908. {
  909. if (__first != __last)
  910. _M_erase_at_end(std::copy(__last, end(), __first));
  911. return __first;
  912. }
  913. #if __cplusplus >= 201103L
  914. template<typename _Alloc>
  915. _GLIBCXX20_CONSTEXPR
  916. bool
  917. vector<bool, _Alloc>::
  918. _M_shrink_to_fit()
  919. {
  920. if (capacity() - size() < int(_S_word_bit))
  921. return false;
  922. __try
  923. {
  924. if (size_type __n = size())
  925. _M_reallocate(__n);
  926. else
  927. {
  928. this->_M_deallocate();
  929. this->_M_impl._M_reset();
  930. }
  931. return true;
  932. }
  933. __catch(...)
  934. { return false; }
  935. }
  936. #endif
  937. _GLIBCXX_END_NAMESPACE_CONTAINER
  938. _GLIBCXX_END_NAMESPACE_VERSION
  939. } // namespace std
  940. #if __cplusplus >= 201103L
  941. namespace std _GLIBCXX_VISIBILITY(default)
  942. {
  943. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  944. template<typename _Alloc>
  945. size_t
  946. hash<_GLIBCXX_STD_C::vector<bool, _Alloc>>::
  947. operator()(const _GLIBCXX_STD_C::vector<bool, _Alloc>& __b) const noexcept
  948. {
  949. size_t __hash = 0;
  950. const size_t __words = __b.size() / _S_word_bit;
  951. if (__words)
  952. {
  953. const size_t __clength = __words * sizeof(_Bit_type);
  954. __hash = std::_Hash_impl::hash(__b._M_impl._M_start._M_p, __clength);
  955. }
  956. const size_t __extrabits = __b.size() % _S_word_bit;
  957. if (__extrabits)
  958. {
  959. _Bit_type __hiword = *__b._M_impl._M_finish._M_p;
  960. __hiword &= ~((~static_cast<_Bit_type>(0)) << __extrabits);
  961. const size_t __clength
  962. = (__extrabits + __CHAR_BIT__ - 1) / __CHAR_BIT__;
  963. if (__words)
  964. __hash = std::_Hash_impl::hash(&__hiword, __clength, __hash);
  965. else
  966. __hash = std::_Hash_impl::hash(&__hiword, __clength);
  967. }
  968. return __hash;
  969. }
  970. _GLIBCXX_END_NAMESPACE_VERSION
  971. } // namespace std
  972. #endif // C++11
  973. #undef _GLIBCXX_ASAN_ANNOTATE_REINIT
  974. #undef _GLIBCXX_ASAN_ANNOTATE_GROW
  975. #undef _GLIBCXX_ASAN_ANNOTATE_GREW
  976. #undef _GLIBCXX_ASAN_ANNOTATE_SHRINK
  977. #endif /* _VECTOR_TCC */