vector.tcc 29 KB

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