選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

1009 行
30KB

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