deque.tcc 33 KB

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