deque.tcc 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. // Deque 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) 1997
  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/deque.tcc
  46. * This is an internal header file, included by other library headers.
  47. * Do not attempt to use it directly. @headername{deque}
  48. */
  49. #ifndef _DEQUE_TCC
  50. #define _DEQUE_TCC 1
  51. namespace std _GLIBCXX_VISIBILITY(default)
  52. {
  53. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  54. _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
  55. #if __cplusplus >= 201103L
  56. template <typename _Tp, typename _Alloc>
  57. void
  58. deque<_Tp, _Alloc>::
  59. _M_default_initialize()
  60. {
  61. _Map_pointer __cur;
  62. __try
  63. {
  64. for (__cur = this->_M_impl._M_start._M_node;
  65. __cur < this->_M_impl._M_finish._M_node;
  66. ++__cur)
  67. std::__uninitialized_default_a(*__cur, *__cur + _S_buffer_size(),
  68. _M_get_Tp_allocator());
  69. std::__uninitialized_default_a(this->_M_impl._M_finish._M_first,
  70. this->_M_impl._M_finish._M_cur,
  71. _M_get_Tp_allocator());
  72. }
  73. __catch(...)
  74. {
  75. std::_Destroy(this->_M_impl._M_start, iterator(*__cur, __cur),
  76. _M_get_Tp_allocator());
  77. __throw_exception_again;
  78. }
  79. }
  80. #endif
  81. template <typename _Tp, typename _Alloc>
  82. deque<_Tp, _Alloc>&
  83. deque<_Tp, _Alloc>::
  84. operator=(const deque& __x)
  85. {
  86. if (&__x != this)
  87. {
  88. #if __cplusplus >= 201103L
  89. if (_Alloc_traits::_S_propagate_on_copy_assign())
  90. {
  91. if (!_Alloc_traits::_S_always_equal()
  92. && _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
  93. {
  94. // Replacement allocator cannot free existing storage,
  95. // so deallocate everything and take copy of __x's data.
  96. _M_replace_map(__x, __x.get_allocator());
  97. std::__alloc_on_copy(_M_get_Tp_allocator(),
  98. __x._M_get_Tp_allocator());
  99. return *this;
  100. }
  101. std::__alloc_on_copy(_M_get_Tp_allocator(),
  102. __x._M_get_Tp_allocator());
  103. }
  104. #endif
  105. const size_type __len = size();
  106. if (__len >= __x.size())
  107. _M_erase_at_end(std::copy(__x.begin(), __x.end(),
  108. this->_M_impl._M_start));
  109. else
  110. {
  111. const_iterator __mid = __x.begin() + difference_type(__len);
  112. std::copy(__x.begin(), __mid, this->_M_impl._M_start);
  113. _M_range_insert_aux(this->_M_impl._M_finish, __mid, __x.end(),
  114. std::random_access_iterator_tag());
  115. }
  116. }
  117. return *this;
  118. }
  119. #if __cplusplus >= 201103L
  120. template<typename _Tp, typename _Alloc>
  121. template<typename... _Args>
  122. #if __cplusplus > 201402L
  123. typename deque<_Tp, _Alloc>::reference
  124. #else
  125. void
  126. #endif
  127. deque<_Tp, _Alloc>::
  128. emplace_front(_Args&&... __args)
  129. {
  130. if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
  131. {
  132. _Alloc_traits::construct(this->_M_impl,
  133. this->_M_impl._M_start._M_cur - 1,
  134. std::forward<_Args>(__args)...);
  135. --this->_M_impl._M_start._M_cur;
  136. }
  137. else
  138. _M_push_front_aux(std::forward<_Args>(__args)...);
  139. #if __cplusplus > 201402L
  140. return front();
  141. #endif
  142. }
  143. template<typename _Tp, typename _Alloc>
  144. template<typename... _Args>
  145. #if __cplusplus > 201402L
  146. typename deque<_Tp, _Alloc>::reference
  147. #else
  148. void
  149. #endif
  150. deque<_Tp, _Alloc>::
  151. emplace_back(_Args&&... __args)
  152. {
  153. if (this->_M_impl._M_finish._M_cur
  154. != this->_M_impl._M_finish._M_last - 1)
  155. {
  156. _Alloc_traits::construct(this->_M_impl,
  157. this->_M_impl._M_finish._M_cur,
  158. std::forward<_Args>(__args)...);
  159. ++this->_M_impl._M_finish._M_cur;
  160. }
  161. else
  162. _M_push_back_aux(std::forward<_Args>(__args)...);
  163. #if __cplusplus > 201402L
  164. return back();
  165. #endif
  166. }
  167. #endif
  168. #if __cplusplus >= 201103L
  169. template<typename _Tp, typename _Alloc>
  170. template<typename... _Args>
  171. typename deque<_Tp, _Alloc>::iterator
  172. deque<_Tp, _Alloc>::
  173. emplace(const_iterator __position, _Args&&... __args)
  174. {
  175. if (__position._M_cur == this->_M_impl._M_start._M_cur)
  176. {
  177. emplace_front(std::forward<_Args>(__args)...);
  178. return this->_M_impl._M_start;
  179. }
  180. else if (__position._M_cur == this->_M_impl._M_finish._M_cur)
  181. {
  182. emplace_back(std::forward<_Args>(__args)...);
  183. iterator __tmp = this->_M_impl._M_finish;
  184. --__tmp;
  185. return __tmp;
  186. }
  187. else
  188. return _M_insert_aux(__position._M_const_cast(),
  189. std::forward<_Args>(__args)...);
  190. }
  191. #endif
  192. template <typename _Tp, typename _Alloc>
  193. typename deque<_Tp, _Alloc>::iterator
  194. deque<_Tp, _Alloc>::
  195. #if __cplusplus >= 201103L
  196. insert(const_iterator __position, const value_type& __x)
  197. #else
  198. insert(iterator __position, const value_type& __x)
  199. #endif
  200. {
  201. if (__position._M_cur == this->_M_impl._M_start._M_cur)
  202. {
  203. push_front(__x);
  204. return this->_M_impl._M_start;
  205. }
  206. else if (__position._M_cur == this->_M_impl._M_finish._M_cur)
  207. {
  208. push_back(__x);
  209. iterator __tmp = this->_M_impl._M_finish;
  210. --__tmp;
  211. return __tmp;
  212. }
  213. else
  214. return _M_insert_aux(__position._M_const_cast(), __x);
  215. }
  216. template <typename _Tp, typename _Alloc>
  217. typename deque<_Tp, _Alloc>::iterator
  218. deque<_Tp, _Alloc>::
  219. _M_erase(iterator __position)
  220. {
  221. iterator __next = __position;
  222. ++__next;
  223. const difference_type __index = __position - begin();
  224. if (static_cast<size_type>(__index) < (size() >> 1))
  225. {
  226. if (__position != begin())
  227. _GLIBCXX_MOVE_BACKWARD3(begin(), __position, __next);
  228. pop_front();
  229. }
  230. else
  231. {
  232. if (__next != end())
  233. _GLIBCXX_MOVE3(__next, end(), __position);
  234. pop_back();
  235. }
  236. return begin() + __index;
  237. }
  238. template <typename _Tp, typename _Alloc>
  239. typename deque<_Tp, _Alloc>::iterator
  240. deque<_Tp, _Alloc>::
  241. _M_erase(iterator __first, iterator __last)
  242. {
  243. if (__first == __last)
  244. return __first;
  245. else if (__first == begin() && __last == end())
  246. {
  247. clear();
  248. return end();
  249. }
  250. else
  251. {
  252. const difference_type __n = __last - __first;
  253. const difference_type __elems_before = __first - begin();
  254. if (static_cast<size_type>(__elems_before) <= (size() - __n) / 2)
  255. {
  256. if (__first != begin())
  257. _GLIBCXX_MOVE_BACKWARD3(begin(), __first, __last);
  258. _M_erase_at_begin(begin() + __n);
  259. }
  260. else
  261. {
  262. if (__last != end())
  263. _GLIBCXX_MOVE3(__last, end(), __first);
  264. _M_erase_at_end(end() - __n);
  265. }
  266. return begin() + __elems_before;
  267. }
  268. }
  269. template <typename _Tp, class _Alloc>
  270. template <typename _InputIterator>
  271. void
  272. deque<_Tp, _Alloc>::
  273. _M_assign_aux(_InputIterator __first, _InputIterator __last,
  274. std::input_iterator_tag)
  275. {
  276. iterator __cur = begin();
  277. for (; __first != __last && __cur != end(); ++__cur, ++__first)
  278. *__cur = *__first;
  279. if (__first == __last)
  280. _M_erase_at_end(__cur);
  281. else
  282. _M_range_insert_aux(end(), __first, __last,
  283. std::__iterator_category(__first));
  284. }
  285. template <typename _Tp, typename _Alloc>
  286. void
  287. deque<_Tp, _Alloc>::
  288. _M_fill_insert(iterator __pos, size_type __n, const value_type& __x)
  289. {
  290. if (__pos._M_cur == this->_M_impl._M_start._M_cur)
  291. {
  292. iterator __new_start = _M_reserve_elements_at_front(__n);
  293. __try
  294. {
  295. std::__uninitialized_fill_a(__new_start, this->_M_impl._M_start,
  296. __x, _M_get_Tp_allocator());
  297. this->_M_impl._M_start = __new_start;
  298. }
  299. __catch(...)
  300. {
  301. _M_destroy_nodes(__new_start._M_node,
  302. this->_M_impl._M_start._M_node);
  303. __throw_exception_again;
  304. }
  305. }
  306. else if (__pos._M_cur == this->_M_impl._M_finish._M_cur)
  307. {
  308. iterator __new_finish = _M_reserve_elements_at_back(__n);
  309. __try
  310. {
  311. std::__uninitialized_fill_a(this->_M_impl._M_finish,
  312. __new_finish, __x,
  313. _M_get_Tp_allocator());
  314. this->_M_impl._M_finish = __new_finish;
  315. }
  316. __catch(...)
  317. {
  318. _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
  319. __new_finish._M_node + 1);
  320. __throw_exception_again;
  321. }
  322. }
  323. else
  324. _M_insert_aux(__pos, __n, __x);
  325. }
  326. #if __cplusplus >= 201103L
  327. template <typename _Tp, typename _Alloc>
  328. void
  329. deque<_Tp, _Alloc>::
  330. _M_default_append(size_type __n)
  331. {
  332. if (__n)
  333. {
  334. iterator __new_finish = _M_reserve_elements_at_back(__n);
  335. __try
  336. {
  337. std::__uninitialized_default_a(this->_M_impl._M_finish,
  338. __new_finish,
  339. _M_get_Tp_allocator());
  340. this->_M_impl._M_finish = __new_finish;
  341. }
  342. __catch(...)
  343. {
  344. _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
  345. __new_finish._M_node + 1);
  346. __throw_exception_again;
  347. }
  348. }
  349. }
  350. template <typename _Tp, typename _Alloc>
  351. bool
  352. deque<_Tp, _Alloc>::
  353. _M_shrink_to_fit()
  354. {
  355. const difference_type __front_capacity
  356. = (this->_M_impl._M_start._M_cur - this->_M_impl._M_start._M_first);
  357. if (__front_capacity == 0)
  358. return false;
  359. const difference_type __back_capacity
  360. = (this->_M_impl._M_finish._M_last - this->_M_impl._M_finish._M_cur);
  361. if (__front_capacity + __back_capacity < _S_buffer_size())
  362. return false;
  363. return std::__shrink_to_fit_aux<deque>::_S_do_it(*this);
  364. }
  365. #endif
  366. template <typename _Tp, typename _Alloc>
  367. void
  368. deque<_Tp, _Alloc>::
  369. _M_fill_initialize(const value_type& __value)
  370. {
  371. _Map_pointer __cur;
  372. __try
  373. {
  374. for (__cur = this->_M_impl._M_start._M_node;
  375. __cur < this->_M_impl._M_finish._M_node;
  376. ++__cur)
  377. std::__uninitialized_fill_a(*__cur, *__cur + _S_buffer_size(),
  378. __value, _M_get_Tp_allocator());
  379. std::__uninitialized_fill_a(this->_M_impl._M_finish._M_first,
  380. this->_M_impl._M_finish._M_cur,
  381. __value, _M_get_Tp_allocator());
  382. }
  383. __catch(...)
  384. {
  385. std::_Destroy(this->_M_impl._M_start, iterator(*__cur, __cur),
  386. _M_get_Tp_allocator());
  387. __throw_exception_again;
  388. }
  389. }
  390. template <typename _Tp, typename _Alloc>
  391. template <typename _InputIterator>
  392. void
  393. deque<_Tp, _Alloc>::
  394. _M_range_initialize(_InputIterator __first, _InputIterator __last,
  395. std::input_iterator_tag)
  396. {
  397. this->_M_initialize_map(0);
  398. __try
  399. {
  400. for (; __first != __last; ++__first)
  401. #if __cplusplus >= 201103L
  402. emplace_back(*__first);
  403. #else
  404. push_back(*__first);
  405. #endif
  406. }
  407. __catch(...)
  408. {
  409. clear();
  410. __throw_exception_again;
  411. }
  412. }
  413. template <typename _Tp, typename _Alloc>
  414. template <typename _ForwardIterator>
  415. void
  416. deque<_Tp, _Alloc>::
  417. _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
  418. std::forward_iterator_tag)
  419. {
  420. const size_type __n = std::distance(__first, __last);
  421. this->_M_initialize_map(__n);
  422. _Map_pointer __cur_node;
  423. __try
  424. {
  425. for (__cur_node = this->_M_impl._M_start._M_node;
  426. __cur_node < this->_M_impl._M_finish._M_node;
  427. ++__cur_node)
  428. {
  429. _ForwardIterator __mid = __first;
  430. std::advance(__mid, _S_buffer_size());
  431. std::__uninitialized_copy_a(__first, __mid, *__cur_node,
  432. _M_get_Tp_allocator());
  433. __first = __mid;
  434. }
  435. std::__uninitialized_copy_a(__first, __last,
  436. this->_M_impl._M_finish._M_first,
  437. _M_get_Tp_allocator());
  438. }
  439. __catch(...)
  440. {
  441. std::_Destroy(this->_M_impl._M_start,
  442. iterator(*__cur_node, __cur_node),
  443. _M_get_Tp_allocator());
  444. __throw_exception_again;
  445. }
  446. }
  447. // Called only if _M_impl._M_finish._M_cur == _M_impl._M_finish._M_last - 1.
  448. template<typename _Tp, typename _Alloc>
  449. #if __cplusplus >= 201103L
  450. template<typename... _Args>
  451. void
  452. deque<_Tp, _Alloc>::
  453. _M_push_back_aux(_Args&&... __args)
  454. #else
  455. void
  456. deque<_Tp, _Alloc>::
  457. _M_push_back_aux(const value_type& __t)
  458. #endif
  459. {
  460. _M_reserve_map_at_back();
  461. *(this->_M_impl._M_finish._M_node + 1) = this->_M_allocate_node();
  462. __try
  463. {
  464. #if __cplusplus >= 201103L
  465. _Alloc_traits::construct(this->_M_impl,
  466. this->_M_impl._M_finish._M_cur,
  467. std::forward<_Args>(__args)...);
  468. #else
  469. this->_M_impl.construct(this->_M_impl._M_finish._M_cur, __t);
  470. #endif
  471. this->_M_impl._M_finish._M_set_node(this->_M_impl._M_finish._M_node
  472. + 1);
  473. this->_M_impl._M_finish._M_cur = this->_M_impl._M_finish._M_first;
  474. }
  475. __catch(...)
  476. {
  477. _M_deallocate_node(*(this->_M_impl._M_finish._M_node + 1));
  478. __throw_exception_again;
  479. }
  480. }
  481. // Called only if _M_impl._M_start._M_cur == _M_impl._M_start._M_first.
  482. template<typename _Tp, typename _Alloc>
  483. #if __cplusplus >= 201103L
  484. template<typename... _Args>
  485. void
  486. deque<_Tp, _Alloc>::
  487. _M_push_front_aux(_Args&&... __args)
  488. #else
  489. void
  490. deque<_Tp, _Alloc>::
  491. _M_push_front_aux(const value_type& __t)
  492. #endif
  493. {
  494. _M_reserve_map_at_front();
  495. *(this->_M_impl._M_start._M_node - 1) = this->_M_allocate_node();
  496. __try
  497. {
  498. this->_M_impl._M_start._M_set_node(this->_M_impl._M_start._M_node
  499. - 1);
  500. this->_M_impl._M_start._M_cur = this->_M_impl._M_start._M_last - 1;
  501. #if __cplusplus >= 201103L
  502. _Alloc_traits::construct(this->_M_impl,
  503. this->_M_impl._M_start._M_cur,
  504. std::forward<_Args>(__args)...);
  505. #else
  506. this->_M_impl.construct(this->_M_impl._M_start._M_cur, __t);
  507. #endif
  508. }
  509. __catch(...)
  510. {
  511. ++this->_M_impl._M_start;
  512. _M_deallocate_node(*(this->_M_impl._M_start._M_node - 1));
  513. __throw_exception_again;
  514. }
  515. }
  516. // Called only if _M_impl._M_finish._M_cur == _M_impl._M_finish._M_first.
  517. template <typename _Tp, typename _Alloc>
  518. void deque<_Tp, _Alloc>::
  519. _M_pop_back_aux()
  520. {
  521. _M_deallocate_node(this->_M_impl._M_finish._M_first);
  522. this->_M_impl._M_finish._M_set_node(this->_M_impl._M_finish._M_node - 1);
  523. this->_M_impl._M_finish._M_cur = this->_M_impl._M_finish._M_last - 1;
  524. _Alloc_traits::destroy(_M_get_Tp_allocator(),
  525. this->_M_impl._M_finish._M_cur);
  526. }
  527. // Called only if _M_impl._M_start._M_cur == _M_impl._M_start._M_last - 1.
  528. // Note that if the deque has at least one element (a precondition for this
  529. // member function), and if
  530. // _M_impl._M_start._M_cur == _M_impl._M_start._M_last,
  531. // then the deque must have at least two nodes.
  532. template <typename _Tp, typename _Alloc>
  533. void deque<_Tp, _Alloc>::
  534. _M_pop_front_aux()
  535. {
  536. _Alloc_traits::destroy(_M_get_Tp_allocator(),
  537. this->_M_impl._M_start._M_cur);
  538. _M_deallocate_node(this->_M_impl._M_start._M_first);
  539. this->_M_impl._M_start._M_set_node(this->_M_impl._M_start._M_node + 1);
  540. this->_M_impl._M_start._M_cur = this->_M_impl._M_start._M_first;
  541. }
  542. template <typename _Tp, typename _Alloc>
  543. template <typename _InputIterator>
  544. void
  545. deque<_Tp, _Alloc>::
  546. _M_range_insert_aux(iterator __pos,
  547. _InputIterator __first, _InputIterator __last,
  548. std::input_iterator_tag)
  549. { std::copy(__first, __last, std::inserter(*this, __pos)); }
  550. template <typename _Tp, typename _Alloc>
  551. template <typename _ForwardIterator>
  552. void
  553. deque<_Tp, _Alloc>::
  554. _M_range_insert_aux(iterator __pos,
  555. _ForwardIterator __first, _ForwardIterator __last,
  556. std::forward_iterator_tag)
  557. {
  558. const size_type __n = std::distance(__first, __last);
  559. if (__pos._M_cur == this->_M_impl._M_start._M_cur)
  560. {
  561. iterator __new_start = _M_reserve_elements_at_front(__n);
  562. __try
  563. {
  564. std::__uninitialized_copy_a(__first, __last, __new_start,
  565. _M_get_Tp_allocator());
  566. this->_M_impl._M_start = __new_start;
  567. }
  568. __catch(...)
  569. {
  570. _M_destroy_nodes(__new_start._M_node,
  571. this->_M_impl._M_start._M_node);
  572. __throw_exception_again;
  573. }
  574. }
  575. else if (__pos._M_cur == this->_M_impl._M_finish._M_cur)
  576. {
  577. iterator __new_finish = _M_reserve_elements_at_back(__n);
  578. __try
  579. {
  580. std::__uninitialized_copy_a(__first, __last,
  581. this->_M_impl._M_finish,
  582. _M_get_Tp_allocator());
  583. this->_M_impl._M_finish = __new_finish;
  584. }
  585. __catch(...)
  586. {
  587. _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
  588. __new_finish._M_node + 1);
  589. __throw_exception_again;
  590. }
  591. }
  592. else
  593. _M_insert_aux(__pos, __first, __last, __n);
  594. }
  595. template<typename _Tp, typename _Alloc>
  596. #if __cplusplus >= 201103L
  597. template<typename... _Args>
  598. typename deque<_Tp, _Alloc>::iterator
  599. deque<_Tp, _Alloc>::
  600. _M_insert_aux(iterator __pos, _Args&&... __args)
  601. {
  602. value_type __x_copy(std::forward<_Args>(__args)...); // XXX copy
  603. #else
  604. typename deque<_Tp, _Alloc>::iterator
  605. deque<_Tp, _Alloc>::
  606. _M_insert_aux(iterator __pos, const value_type& __x)
  607. {
  608. value_type __x_copy = __x; // XXX copy
  609. #endif
  610. difference_type __index = __pos - this->_M_impl._M_start;
  611. if (static_cast<size_type>(__index) < size() / 2)
  612. {
  613. push_front(_GLIBCXX_MOVE(front()));
  614. iterator __front1 = this->_M_impl._M_start;
  615. ++__front1;
  616. iterator __front2 = __front1;
  617. ++__front2;
  618. __pos = this->_M_impl._M_start + __index;
  619. iterator __pos1 = __pos;
  620. ++__pos1;
  621. _GLIBCXX_MOVE3(__front2, __pos1, __front1);
  622. }
  623. else
  624. {
  625. push_back(_GLIBCXX_MOVE(back()));
  626. iterator __back1 = this->_M_impl._M_finish;
  627. --__back1;
  628. iterator __back2 = __back1;
  629. --__back2;
  630. __pos = this->_M_impl._M_start + __index;
  631. _GLIBCXX_MOVE_BACKWARD3(__pos, __back2, __back1);
  632. }
  633. *__pos = _GLIBCXX_MOVE(__x_copy);
  634. return __pos;
  635. }
  636. template <typename _Tp, typename _Alloc>
  637. void
  638. deque<_Tp, _Alloc>::
  639. _M_insert_aux(iterator __pos, size_type __n, const value_type& __x)
  640. {
  641. const difference_type __elems_before = __pos - this->_M_impl._M_start;
  642. const size_type __length = this->size();
  643. value_type __x_copy = __x;
  644. if (__elems_before < difference_type(__length / 2))
  645. {
  646. iterator __new_start = _M_reserve_elements_at_front(__n);
  647. iterator __old_start = this->_M_impl._M_start;
  648. __pos = this->_M_impl._M_start + __elems_before;
  649. __try
  650. {
  651. if (__elems_before >= difference_type(__n))
  652. {
  653. iterator __start_n = (this->_M_impl._M_start
  654. + difference_type(__n));
  655. std::__uninitialized_move_a(this->_M_impl._M_start,
  656. __start_n, __new_start,
  657. _M_get_Tp_allocator());
  658. this->_M_impl._M_start = __new_start;
  659. _GLIBCXX_MOVE3(__start_n, __pos, __old_start);
  660. std::fill(__pos - difference_type(__n), __pos, __x_copy);
  661. }
  662. else
  663. {
  664. std::__uninitialized_move_fill(this->_M_impl._M_start,
  665. __pos, __new_start,
  666. this->_M_impl._M_start,
  667. __x_copy,
  668. _M_get_Tp_allocator());
  669. this->_M_impl._M_start = __new_start;
  670. std::fill(__old_start, __pos, __x_copy);
  671. }
  672. }
  673. __catch(...)
  674. {
  675. _M_destroy_nodes(__new_start._M_node,
  676. this->_M_impl._M_start._M_node);
  677. __throw_exception_again;
  678. }
  679. }
  680. else
  681. {
  682. iterator __new_finish = _M_reserve_elements_at_back(__n);
  683. iterator __old_finish = this->_M_impl._M_finish;
  684. const difference_type __elems_after =
  685. difference_type(__length) - __elems_before;
  686. __pos = this->_M_impl._M_finish - __elems_after;
  687. __try
  688. {
  689. if (__elems_after > difference_type(__n))
  690. {
  691. iterator __finish_n = (this->_M_impl._M_finish
  692. - difference_type(__n));
  693. std::__uninitialized_move_a(__finish_n,
  694. this->_M_impl._M_finish,
  695. this->_M_impl._M_finish,
  696. _M_get_Tp_allocator());
  697. this->_M_impl._M_finish = __new_finish;
  698. _GLIBCXX_MOVE_BACKWARD3(__pos, __finish_n, __old_finish);
  699. std::fill(__pos, __pos + difference_type(__n), __x_copy);
  700. }
  701. else
  702. {
  703. std::__uninitialized_fill_move(this->_M_impl._M_finish,
  704. __pos + difference_type(__n),
  705. __x_copy, __pos,
  706. this->_M_impl._M_finish,
  707. _M_get_Tp_allocator());
  708. this->_M_impl._M_finish = __new_finish;
  709. std::fill(__pos, __old_finish, __x_copy);
  710. }
  711. }
  712. __catch(...)
  713. {
  714. _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
  715. __new_finish._M_node + 1);
  716. __throw_exception_again;
  717. }
  718. }
  719. }
  720. template <typename _Tp, typename _Alloc>
  721. template <typename _ForwardIterator>
  722. void
  723. deque<_Tp, _Alloc>::
  724. _M_insert_aux(iterator __pos,
  725. _ForwardIterator __first, _ForwardIterator __last,
  726. size_type __n)
  727. {
  728. const difference_type __elemsbefore = __pos - this->_M_impl._M_start;
  729. const size_type __length = size();
  730. if (static_cast<size_type>(__elemsbefore) < __length / 2)
  731. {
  732. iterator __new_start = _M_reserve_elements_at_front(__n);
  733. iterator __old_start = this->_M_impl._M_start;
  734. __pos = this->_M_impl._M_start + __elemsbefore;
  735. __try
  736. {
  737. if (__elemsbefore >= difference_type(__n))
  738. {
  739. iterator __start_n = (this->_M_impl._M_start
  740. + difference_type(__n));
  741. std::__uninitialized_move_a(this->_M_impl._M_start,
  742. __start_n, __new_start,
  743. _M_get_Tp_allocator());
  744. this->_M_impl._M_start = __new_start;
  745. _GLIBCXX_MOVE3(__start_n, __pos, __old_start);
  746. std::copy(__first, __last, __pos - difference_type(__n));
  747. }
  748. else
  749. {
  750. _ForwardIterator __mid = __first;
  751. std::advance(__mid, difference_type(__n) - __elemsbefore);
  752. std::__uninitialized_move_copy(this->_M_impl._M_start,
  753. __pos, __first, __mid,
  754. __new_start,
  755. _M_get_Tp_allocator());
  756. this->_M_impl._M_start = __new_start;
  757. std::copy(__mid, __last, __old_start);
  758. }
  759. }
  760. __catch(...)
  761. {
  762. _M_destroy_nodes(__new_start._M_node,
  763. this->_M_impl._M_start._M_node);
  764. __throw_exception_again;
  765. }
  766. }
  767. else
  768. {
  769. iterator __new_finish = _M_reserve_elements_at_back(__n);
  770. iterator __old_finish = this->_M_impl._M_finish;
  771. const difference_type __elemsafter =
  772. difference_type(__length) - __elemsbefore;
  773. __pos = this->_M_impl._M_finish - __elemsafter;
  774. __try
  775. {
  776. if (__elemsafter > difference_type(__n))
  777. {
  778. iterator __finish_n = (this->_M_impl._M_finish
  779. - difference_type(__n));
  780. std::__uninitialized_move_a(__finish_n,
  781. this->_M_impl._M_finish,
  782. this->_M_impl._M_finish,
  783. _M_get_Tp_allocator());
  784. this->_M_impl._M_finish = __new_finish;
  785. _GLIBCXX_MOVE_BACKWARD3(__pos, __finish_n, __old_finish);
  786. std::copy(__first, __last, __pos);
  787. }
  788. else
  789. {
  790. _ForwardIterator __mid = __first;
  791. std::advance(__mid, __elemsafter);
  792. std::__uninitialized_copy_move(__mid, __last, __pos,
  793. this->_M_impl._M_finish,
  794. this->_M_impl._M_finish,
  795. _M_get_Tp_allocator());
  796. this->_M_impl._M_finish = __new_finish;
  797. std::copy(__first, __mid, __pos);
  798. }
  799. }
  800. __catch(...)
  801. {
  802. _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
  803. __new_finish._M_node + 1);
  804. __throw_exception_again;
  805. }
  806. }
  807. }
  808. template<typename _Tp, typename _Alloc>
  809. void
  810. deque<_Tp, _Alloc>::
  811. _M_destroy_data_aux(iterator __first, iterator __last)
  812. {
  813. for (_Map_pointer __node = __first._M_node + 1;
  814. __node < __last._M_node; ++__node)
  815. std::_Destroy(*__node, *__node + _S_buffer_size(),
  816. _M_get_Tp_allocator());
  817. if (__first._M_node != __last._M_node)
  818. {
  819. std::_Destroy(__first._M_cur, __first._M_last,
  820. _M_get_Tp_allocator());
  821. std::_Destroy(__last._M_first, __last._M_cur,
  822. _M_get_Tp_allocator());
  823. }
  824. else
  825. std::_Destroy(__first._M_cur, __last._M_cur,
  826. _M_get_Tp_allocator());
  827. }
  828. template <typename _Tp, typename _Alloc>
  829. void
  830. deque<_Tp, _Alloc>::
  831. _M_new_elements_at_front(size_type __new_elems)
  832. {
  833. if (this->max_size() - this->size() < __new_elems)
  834. __throw_length_error(__N("deque::_M_new_elements_at_front"));
  835. const size_type __new_nodes = ((__new_elems + _S_buffer_size() - 1)
  836. / _S_buffer_size());
  837. _M_reserve_map_at_front(__new_nodes);
  838. size_type __i;
  839. __try
  840. {
  841. for (__i = 1; __i <= __new_nodes; ++__i)
  842. *(this->_M_impl._M_start._M_node - __i) = this->_M_allocate_node();
  843. }
  844. __catch(...)
  845. {
  846. for (size_type __j = 1; __j < __i; ++__j)
  847. _M_deallocate_node(*(this->_M_impl._M_start._M_node - __j));
  848. __throw_exception_again;
  849. }
  850. }
  851. template <typename _Tp, typename _Alloc>
  852. void
  853. deque<_Tp, _Alloc>::
  854. _M_new_elements_at_back(size_type __new_elems)
  855. {
  856. if (this->max_size() - this->size() < __new_elems)
  857. __throw_length_error(__N("deque::_M_new_elements_at_back"));
  858. const size_type __new_nodes = ((__new_elems + _S_buffer_size() - 1)
  859. / _S_buffer_size());
  860. _M_reserve_map_at_back(__new_nodes);
  861. size_type __i;
  862. __try
  863. {
  864. for (__i = 1; __i <= __new_nodes; ++__i)
  865. *(this->_M_impl._M_finish._M_node + __i) = this->_M_allocate_node();
  866. }
  867. __catch(...)
  868. {
  869. for (size_type __j = 1; __j < __i; ++__j)
  870. _M_deallocate_node(*(this->_M_impl._M_finish._M_node + __j));
  871. __throw_exception_again;
  872. }
  873. }
  874. template <typename _Tp, typename _Alloc>
  875. void
  876. deque<_Tp, _Alloc>::
  877. _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front)
  878. {
  879. const size_type __old_num_nodes
  880. = this->_M_impl._M_finish._M_node - this->_M_impl._M_start._M_node + 1;
  881. const size_type __new_num_nodes = __old_num_nodes + __nodes_to_add;
  882. _Map_pointer __new_nstart;
  883. if (this->_M_impl._M_map_size > 2 * __new_num_nodes)
  884. {
  885. __new_nstart = this->_M_impl._M_map + (this->_M_impl._M_map_size
  886. - __new_num_nodes) / 2
  887. + (__add_at_front ? __nodes_to_add : 0);
  888. if (__new_nstart < this->_M_impl._M_start._M_node)
  889. std::copy(this->_M_impl._M_start._M_node,
  890. this->_M_impl._M_finish._M_node + 1,
  891. __new_nstart);
  892. else
  893. std::copy_backward(this->_M_impl._M_start._M_node,
  894. this->_M_impl._M_finish._M_node + 1,
  895. __new_nstart + __old_num_nodes);
  896. }
  897. else
  898. {
  899. size_type __new_map_size = this->_M_impl._M_map_size
  900. + std::max(this->_M_impl._M_map_size,
  901. __nodes_to_add) + 2;
  902. _Map_pointer __new_map = this->_M_allocate_map(__new_map_size);
  903. __new_nstart = __new_map + (__new_map_size - __new_num_nodes) / 2
  904. + (__add_at_front ? __nodes_to_add : 0);
  905. std::copy(this->_M_impl._M_start._M_node,
  906. this->_M_impl._M_finish._M_node + 1,
  907. __new_nstart);
  908. _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
  909. this->_M_impl._M_map = __new_map;
  910. this->_M_impl._M_map_size = __new_map_size;
  911. }
  912. this->_M_impl._M_start._M_set_node(__new_nstart);
  913. this->_M_impl._M_finish._M_set_node(__new_nstart + __old_num_nodes - 1);
  914. }
  915. // Overload for deque::iterators, exploiting the "segmented-iterator
  916. // optimization".
  917. template<typename _Tp>
  918. void
  919. fill(const _Deque_iterator<_Tp, _Tp&, _Tp*>& __first,
  920. const _Deque_iterator<_Tp, _Tp&, _Tp*>& __last, const _Tp& __value)
  921. {
  922. typedef typename _Deque_iterator<_Tp, _Tp&, _Tp*>::_Self _Self;
  923. for (typename _Self::_Map_pointer __node = __first._M_node + 1;
  924. __node < __last._M_node; ++__node)
  925. std::fill(*__node, *__node + _Self::_S_buffer_size(), __value);
  926. if (__first._M_node != __last._M_node)
  927. {
  928. std::fill(__first._M_cur, __first._M_last, __value);
  929. std::fill(__last._M_first, __last._M_cur, __value);
  930. }
  931. else
  932. std::fill(__first._M_cur, __last._M_cur, __value);
  933. }
  934. template<typename _Tp>
  935. _Deque_iterator<_Tp, _Tp&, _Tp*>
  936. copy(_Deque_iterator<_Tp, const _Tp&, const _Tp*> __first,
  937. _Deque_iterator<_Tp, const _Tp&, const _Tp*> __last,
  938. _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
  939. {
  940. typedef typename _Deque_iterator<_Tp, _Tp&, _Tp*>::_Self _Self;
  941. typedef typename _Self::difference_type difference_type;
  942. difference_type __len = __last - __first;
  943. while (__len > 0)
  944. {
  945. const difference_type __clen
  946. = std::min(__len, std::min(__first._M_last - __first._M_cur,
  947. __result._M_last - __result._M_cur));
  948. std::copy(__first._M_cur, __first._M_cur + __clen, __result._M_cur);
  949. __first += __clen;
  950. __result += __clen;
  951. __len -= __clen;
  952. }
  953. return __result;
  954. }
  955. template<typename _Tp>
  956. _Deque_iterator<_Tp, _Tp&, _Tp*>
  957. copy_backward(_Deque_iterator<_Tp, const _Tp&, const _Tp*> __first,
  958. _Deque_iterator<_Tp, const _Tp&, const _Tp*> __last,
  959. _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
  960. {
  961. typedef typename _Deque_iterator<_Tp, _Tp&, _Tp*>::_Self _Self;
  962. typedef typename _Self::difference_type difference_type;
  963. difference_type __len = __last - __first;
  964. while (__len > 0)
  965. {
  966. difference_type __llen = __last._M_cur - __last._M_first;
  967. _Tp* __lend = __last._M_cur;
  968. difference_type __rlen = __result._M_cur - __result._M_first;
  969. _Tp* __rend = __result._M_cur;
  970. if (!__llen)
  971. {
  972. __llen = _Self::_S_buffer_size();
  973. __lend = *(__last._M_node - 1) + __llen;
  974. }
  975. if (!__rlen)
  976. {
  977. __rlen = _Self::_S_buffer_size();
  978. __rend = *(__result._M_node - 1) + __rlen;
  979. }
  980. const difference_type __clen = std::min(__len,
  981. std::min(__llen, __rlen));
  982. std::copy_backward(__lend - __clen, __lend, __rend);
  983. __last -= __clen;
  984. __result -= __clen;
  985. __len -= __clen;
  986. }
  987. return __result;
  988. }
  989. #if __cplusplus >= 201103L
  990. template<typename _Tp>
  991. _Deque_iterator<_Tp, _Tp&, _Tp*>
  992. move(_Deque_iterator<_Tp, const _Tp&, const _Tp*> __first,
  993. _Deque_iterator<_Tp, const _Tp&, const _Tp*> __last,
  994. _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
  995. {
  996. typedef typename _Deque_iterator<_Tp, _Tp&, _Tp*>::_Self _Self;
  997. typedef typename _Self::difference_type difference_type;
  998. difference_type __len = __last - __first;
  999. while (__len > 0)
  1000. {
  1001. const difference_type __clen
  1002. = std::min(__len, std::min(__first._M_last - __first._M_cur,
  1003. __result._M_last - __result._M_cur));
  1004. std::move(__first._M_cur, __first._M_cur + __clen, __result._M_cur);
  1005. __first += __clen;
  1006. __result += __clen;
  1007. __len -= __clen;
  1008. }
  1009. return __result;
  1010. }
  1011. template<typename _Tp>
  1012. _Deque_iterator<_Tp, _Tp&, _Tp*>
  1013. move_backward(_Deque_iterator<_Tp, const _Tp&, const _Tp*> __first,
  1014. _Deque_iterator<_Tp, const _Tp&, const _Tp*> __last,
  1015. _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
  1016. {
  1017. typedef typename _Deque_iterator<_Tp, _Tp&, _Tp*>::_Self _Self;
  1018. typedef typename _Self::difference_type difference_type;
  1019. difference_type __len = __last - __first;
  1020. while (__len > 0)
  1021. {
  1022. difference_type __llen = __last._M_cur - __last._M_first;
  1023. _Tp* __lend = __last._M_cur;
  1024. difference_type __rlen = __result._M_cur - __result._M_first;
  1025. _Tp* __rend = __result._M_cur;
  1026. if (!__llen)
  1027. {
  1028. __llen = _Self::_S_buffer_size();
  1029. __lend = *(__last._M_node - 1) + __llen;
  1030. }
  1031. if (!__rlen)
  1032. {
  1033. __rlen = _Self::_S_buffer_size();
  1034. __rend = *(__result._M_node - 1) + __rlen;
  1035. }
  1036. const difference_type __clen = std::min(__len,
  1037. std::min(__llen, __rlen));
  1038. std::move_backward(__lend - __clen, __lend, __rend);
  1039. __last -= __clen;
  1040. __result -= __clen;
  1041. __len -= __clen;
  1042. }
  1043. return __result;
  1044. }
  1045. #endif
  1046. _GLIBCXX_END_NAMESPACE_CONTAINER
  1047. _GLIBCXX_END_NAMESPACE_VERSION
  1048. } // namespace std
  1049. #endif