deque.tcc 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  1. // Deque implementation (out of line) -*- C++ -*-
  2. // Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. *
  22. * Copyright (c) 1994
  23. * Hewlett-Packard Company
  24. *
  25. * Permission to use, copy, modify, distribute and sell this software
  26. * and its documentation for any purpose is hereby granted without fee,
  27. * provided that the above copyright notice appear in all copies and
  28. * that both that copyright notice and this permission notice appear
  29. * in supporting documentation. Hewlett-Packard Company makes no
  30. * representations about the suitability of this software for any
  31. * purpose. It is provided "as is" without express or implied warranty.
  32. *
  33. *
  34. * Copyright (c) 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. #include <bits/stl_algobase.h>
  52. namespace std _GLIBCXX_VISIBILITY(default)
  53. {
  54. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  55. _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
  56. #if __cplusplus >= 201103L
  57. template <typename _Tp, typename _Alloc>
  58. void
  59. deque<_Tp, _Alloc>::
  60. _M_default_initialize()
  61. {
  62. _Map_pointer __cur;
  63. __try
  64. {
  65. for (__cur = this->_M_impl._M_start._M_node;
  66. __cur < this->_M_impl._M_finish._M_node;
  67. ++__cur)
  68. std::__uninitialized_default_a(*__cur, *__cur + _S_buffer_size(),
  69. _M_get_Tp_allocator());
  70. std::__uninitialized_default_a(this->_M_impl._M_finish._M_first,
  71. this->_M_impl._M_finish._M_cur,
  72. _M_get_Tp_allocator());
  73. }
  74. __catch(...)
  75. {
  76. std::_Destroy(this->_M_impl._M_start, iterator(*__cur, __cur),
  77. _M_get_Tp_allocator());
  78. __throw_exception_again;
  79. }
  80. }
  81. #endif
  82. template <typename _Tp, typename _Alloc>
  83. deque<_Tp, _Alloc>&
  84. deque<_Tp, _Alloc>::
  85. operator=(const deque& __x)
  86. {
  87. if (std::__addressof(__x) != this)
  88. {
  89. #if __cplusplus >= 201103L
  90. if (_Alloc_traits::_S_propagate_on_copy_assign())
  91. {
  92. if (!_Alloc_traits::_S_always_equal()
  93. && _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
  94. {
  95. // Replacement allocator cannot free existing storage,
  96. // so deallocate everything and take copy of __x's data.
  97. _M_replace_map(__x, __x.get_allocator());
  98. std::__alloc_on_copy(_M_get_Tp_allocator(),
  99. __x._M_get_Tp_allocator());
  100. return *this;
  101. }
  102. std::__alloc_on_copy(_M_get_Tp_allocator(),
  103. __x._M_get_Tp_allocator());
  104. }
  105. #endif
  106. const size_type __len = size();
  107. if (__len >= __x.size())
  108. _M_erase_at_end(std::copy(__x.begin(), __x.end(),
  109. this->_M_impl._M_start));
  110. else
  111. {
  112. const_iterator __mid = __x.begin() + difference_type(__len);
  113. std::copy(__x.begin(), __mid, this->_M_impl._M_start);
  114. _M_range_insert_aux(this->_M_impl._M_finish, __mid, __x.end(),
  115. std::random_access_iterator_tag());
  116. }
  117. }
  118. return *this;
  119. }
  120. #if __cplusplus >= 201103L
  121. template<typename _Tp, typename _Alloc>
  122. template<typename... _Args>
  123. #if __cplusplus > 201402L
  124. typename deque<_Tp, _Alloc>::reference
  125. #else
  126. void
  127. #endif
  128. deque<_Tp, _Alloc>::
  129. emplace_front(_Args&&... __args)
  130. {
  131. if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
  132. {
  133. _Alloc_traits::construct(this->_M_impl,
  134. this->_M_impl._M_start._M_cur - 1,
  135. std::forward<_Args>(__args)...);
  136. --this->_M_impl._M_start._M_cur;
  137. }
  138. else
  139. _M_push_front_aux(std::forward<_Args>(__args)...);
  140. #if __cplusplus > 201402L
  141. return front();
  142. #endif
  143. }
  144. template<typename _Tp, typename _Alloc>
  145. template<typename... _Args>
  146. #if __cplusplus > 201402L
  147. typename deque<_Tp, _Alloc>::reference
  148. #else
  149. void
  150. #endif
  151. deque<_Tp, _Alloc>::
  152. emplace_back(_Args&&... __args)
  153. {
  154. if (this->_M_impl._M_finish._M_cur
  155. != this->_M_impl._M_finish._M_last - 1)
  156. {
  157. _Alloc_traits::construct(this->_M_impl,
  158. this->_M_impl._M_finish._M_cur,
  159. std::forward<_Args>(__args)...);
  160. ++this->_M_impl._M_finish._M_cur;
  161. }
  162. else
  163. _M_push_back_aux(std::forward<_Args>(__args)...);
  164. #if __cplusplus > 201402L
  165. return back();
  166. #endif
  167. }
  168. #endif
  169. #if __cplusplus >= 201103L
  170. template<typename _Tp, typename _Alloc>
  171. template<typename... _Args>
  172. typename deque<_Tp, _Alloc>::iterator
  173. deque<_Tp, _Alloc>::
  174. emplace(const_iterator __position, _Args&&... __args)
  175. {
  176. if (__position._M_cur == this->_M_impl._M_start._M_cur)
  177. {
  178. emplace_front(std::forward<_Args>(__args)...);
  179. return this->_M_impl._M_start;
  180. }
  181. else if (__position._M_cur == this->_M_impl._M_finish._M_cur)
  182. {
  183. emplace_back(std::forward<_Args>(__args)...);
  184. iterator __tmp = this->_M_impl._M_finish;
  185. --__tmp;
  186. return __tmp;
  187. }
  188. else
  189. return _M_insert_aux(__position._M_const_cast(),
  190. std::forward<_Args>(__args)...);
  191. }
  192. #endif
  193. template <typename _Tp, typename _Alloc>
  194. typename deque<_Tp, _Alloc>::iterator
  195. deque<_Tp, _Alloc>::
  196. #if __cplusplus >= 201103L
  197. insert(const_iterator __position, const value_type& __x)
  198. #else
  199. insert(iterator __position, const value_type& __x)
  200. #endif
  201. {
  202. if (__position._M_cur == this->_M_impl._M_start._M_cur)
  203. {
  204. push_front(__x);
  205. return this->_M_impl._M_start;
  206. }
  207. else if (__position._M_cur == this->_M_impl._M_finish._M_cur)
  208. {
  209. push_back(__x);
  210. iterator __tmp = this->_M_impl._M_finish;
  211. --__tmp;
  212. return __tmp;
  213. }
  214. else
  215. return _M_insert_aux(__position._M_const_cast(), __x);
  216. }
  217. template <typename _Tp, typename _Alloc>
  218. typename deque<_Tp, _Alloc>::iterator
  219. deque<_Tp, _Alloc>::
  220. _M_erase(iterator __position)
  221. {
  222. iterator __next = __position;
  223. ++__next;
  224. const difference_type __index = __position - begin();
  225. if (static_cast<size_type>(__index) < (size() >> 1))
  226. {
  227. if (__position != begin())
  228. _GLIBCXX_MOVE_BACKWARD3(begin(), __position, __next);
  229. pop_front();
  230. }
  231. else
  232. {
  233. if (__next != end())
  234. _GLIBCXX_MOVE3(__next, end(), __position);
  235. pop_back();
  236. }
  237. return begin() + __index;
  238. }
  239. template <typename _Tp, typename _Alloc>
  240. typename deque<_Tp, _Alloc>::iterator
  241. deque<_Tp, _Alloc>::
  242. _M_erase(iterator __first, iterator __last)
  243. {
  244. if (__first == __last)
  245. return __first;
  246. else if (__first == begin() && __last == end())
  247. {
  248. clear();
  249. return end();
  250. }
  251. else
  252. {
  253. const difference_type __n = __last - __first;
  254. const difference_type __elems_before = __first - begin();
  255. if (static_cast<size_type>(__elems_before) <= (size() - __n) / 2)
  256. {
  257. if (__first != begin())
  258. _GLIBCXX_MOVE_BACKWARD3(begin(), __first, __last);
  259. _M_erase_at_begin(begin() + __n);
  260. }
  261. else
  262. {
  263. if (__last != end())
  264. _GLIBCXX_MOVE3(__last, end(), __first);
  265. _M_erase_at_end(end() - __n);
  266. }
  267. return begin() + __elems_before;
  268. }
  269. }
  270. template <typename _Tp, class _Alloc>
  271. template <typename _InputIterator>
  272. void
  273. deque<_Tp, _Alloc>::
  274. _M_assign_aux(_InputIterator __first, _InputIterator __last,
  275. std::input_iterator_tag)
  276. {
  277. iterator __cur = begin();
  278. for (; __first != __last && __cur != end(); ++__cur, (void)++__first)
  279. *__cur = *__first;
  280. if (__first == __last)
  281. _M_erase_at_end(__cur);
  282. else
  283. _M_range_insert_aux(end(), __first, __last,
  284. std::__iterator_category(__first));
  285. }
  286. template <typename _Tp, typename _Alloc>
  287. void
  288. deque<_Tp, _Alloc>::
  289. _M_fill_insert(iterator __pos, size_type __n, const value_type& __x)
  290. {
  291. if (__pos._M_cur == this->_M_impl._M_start._M_cur)
  292. {
  293. iterator __new_start = _M_reserve_elements_at_front(__n);
  294. __try
  295. {
  296. std::__uninitialized_fill_a(__new_start, this->_M_impl._M_start,
  297. __x, _M_get_Tp_allocator());
  298. this->_M_impl._M_start = __new_start;
  299. }
  300. __catch(...)
  301. {
  302. _M_destroy_nodes(__new_start._M_node,
  303. this->_M_impl._M_start._M_node);
  304. __throw_exception_again;
  305. }
  306. }
  307. else if (__pos._M_cur == this->_M_impl._M_finish._M_cur)
  308. {
  309. iterator __new_finish = _M_reserve_elements_at_back(__n);
  310. __try
  311. {
  312. std::__uninitialized_fill_a(this->_M_impl._M_finish,
  313. __new_finish, __x,
  314. _M_get_Tp_allocator());
  315. this->_M_impl._M_finish = __new_finish;
  316. }
  317. __catch(...)
  318. {
  319. _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
  320. __new_finish._M_node + 1);
  321. __throw_exception_again;
  322. }
  323. }
  324. else
  325. _M_insert_aux(__pos, __n, __x);
  326. }
  327. #if __cplusplus >= 201103L
  328. template <typename _Tp, typename _Alloc>
  329. void
  330. deque<_Tp, _Alloc>::
  331. _M_default_append(size_type __n)
  332. {
  333. if (__n)
  334. {
  335. iterator __new_finish = _M_reserve_elements_at_back(__n);
  336. __try
  337. {
  338. std::__uninitialized_default_a(this->_M_impl._M_finish,
  339. __new_finish,
  340. _M_get_Tp_allocator());
  341. this->_M_impl._M_finish = __new_finish;
  342. }
  343. __catch(...)
  344. {
  345. _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
  346. __new_finish._M_node + 1);
  347. __throw_exception_again;
  348. }
  349. }
  350. }
  351. template <typename _Tp, typename _Alloc>
  352. bool
  353. deque<_Tp, _Alloc>::
  354. _M_shrink_to_fit()
  355. {
  356. const difference_type __front_capacity
  357. = (this->_M_impl._M_start._M_cur - this->_M_impl._M_start._M_first);
  358. if (__front_capacity == 0)
  359. return false;
  360. const difference_type __back_capacity
  361. = (this->_M_impl._M_finish._M_last - this->_M_impl._M_finish._M_cur);
  362. if (__front_capacity + __back_capacity < _S_buffer_size())
  363. return false;
  364. return std::__shrink_to_fit_aux<deque>::_S_do_it(*this);
  365. }
  366. #endif
  367. template <typename _Tp, typename _Alloc>
  368. void
  369. deque<_Tp, _Alloc>::
  370. _M_fill_initialize(const value_type& __value)
  371. {
  372. _Map_pointer __cur;
  373. __try
  374. {
  375. for (__cur = this->_M_impl._M_start._M_node;
  376. __cur < this->_M_impl._M_finish._M_node;
  377. ++__cur)
  378. std::__uninitialized_fill_a(*__cur, *__cur + _S_buffer_size(),
  379. __value, _M_get_Tp_allocator());
  380. std::__uninitialized_fill_a(this->_M_impl._M_finish._M_first,
  381. this->_M_impl._M_finish._M_cur,
  382. __value, _M_get_Tp_allocator());
  383. }
  384. __catch(...)
  385. {
  386. std::_Destroy(this->_M_impl._M_start, iterator(*__cur, __cur),
  387. _M_get_Tp_allocator());
  388. __throw_exception_again;
  389. }
  390. }
  391. template <typename _Tp, typename _Alloc>
  392. template <typename _InputIterator>
  393. void
  394. deque<_Tp, _Alloc>::
  395. _M_range_initialize(_InputIterator __first, _InputIterator __last,
  396. std::input_iterator_tag)
  397. {
  398. this->_M_initialize_map(0);
  399. __try
  400. {
  401. for (; __first != __last; ++__first)
  402. #if __cplusplus >= 201103L
  403. emplace_back(*__first);
  404. #else
  405. push_back(*__first);
  406. #endif
  407. }
  408. __catch(...)
  409. {
  410. clear();
  411. __throw_exception_again;
  412. }
  413. }
  414. template <typename _Tp, typename _Alloc>
  415. template <typename _ForwardIterator>
  416. void
  417. deque<_Tp, _Alloc>::
  418. _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
  419. std::forward_iterator_tag)
  420. {
  421. const size_type __n = std::distance(__first, __last);
  422. this->_M_initialize_map(_S_check_init_len(__n, _M_get_Tp_allocator()));
  423. _Map_pointer __cur_node;
  424. __try
  425. {
  426. for (__cur_node = this->_M_impl._M_start._M_node;
  427. __cur_node < this->_M_impl._M_finish._M_node;
  428. ++__cur_node)
  429. {
  430. if (__n < _S_buffer_size())
  431. __builtin_unreachable(); // See PR 100516
  432. _ForwardIterator __mid = __first;
  433. std::advance(__mid, _S_buffer_size());
  434. std::__uninitialized_copy_a(__first, __mid, *__cur_node,
  435. _M_get_Tp_allocator());
  436. __first = __mid;
  437. }
  438. std::__uninitialized_copy_a(__first, __last,
  439. this->_M_impl._M_finish._M_first,
  440. _M_get_Tp_allocator());
  441. }
  442. __catch(...)
  443. {
  444. std::_Destroy(this->_M_impl._M_start,
  445. iterator(*__cur_node, __cur_node),
  446. _M_get_Tp_allocator());
  447. __throw_exception_again;
  448. }
  449. }
  450. // Called only if _M_impl._M_finish._M_cur == _M_impl._M_finish._M_last - 1.
  451. template<typename _Tp, typename _Alloc>
  452. #if __cplusplus >= 201103L
  453. template<typename... _Args>
  454. void
  455. deque<_Tp, _Alloc>::
  456. _M_push_back_aux(_Args&&... __args)
  457. #else
  458. void
  459. deque<_Tp, _Alloc>::
  460. _M_push_back_aux(const value_type& __t)
  461. #endif
  462. {
  463. if (size() == max_size())
  464. __throw_length_error(
  465. __N("cannot create std::deque larger than max_size()"));
  466. _M_reserve_map_at_back();
  467. *(this->_M_impl._M_finish._M_node + 1) = this->_M_allocate_node();
  468. __try
  469. {
  470. #if __cplusplus >= 201103L
  471. _Alloc_traits::construct(this->_M_impl,
  472. this->_M_impl._M_finish._M_cur,
  473. std::forward<_Args>(__args)...);
  474. #else
  475. this->_M_impl.construct(this->_M_impl._M_finish._M_cur, __t);
  476. #endif
  477. this->_M_impl._M_finish._M_set_node(this->_M_impl._M_finish._M_node
  478. + 1);
  479. this->_M_impl._M_finish._M_cur = this->_M_impl._M_finish._M_first;
  480. }
  481. __catch(...)
  482. {
  483. _M_deallocate_node(*(this->_M_impl._M_finish._M_node + 1));
  484. __throw_exception_again;
  485. }
  486. }
  487. // Called only if _M_impl._M_start._M_cur == _M_impl._M_start._M_first.
  488. template<typename _Tp, typename _Alloc>
  489. #if __cplusplus >= 201103L
  490. template<typename... _Args>
  491. void
  492. deque<_Tp, _Alloc>::
  493. _M_push_front_aux(_Args&&... __args)
  494. #else
  495. void
  496. deque<_Tp, _Alloc>::
  497. _M_push_front_aux(const value_type& __t)
  498. #endif
  499. {
  500. if (size() == max_size())
  501. __throw_length_error(
  502. __N("cannot create std::deque larger than max_size()"));
  503. _M_reserve_map_at_front();
  504. *(this->_M_impl._M_start._M_node - 1) = this->_M_allocate_node();
  505. __try
  506. {
  507. this->_M_impl._M_start._M_set_node(this->_M_impl._M_start._M_node
  508. - 1);
  509. this->_M_impl._M_start._M_cur = this->_M_impl._M_start._M_last - 1;
  510. #if __cplusplus >= 201103L
  511. _Alloc_traits::construct(this->_M_impl,
  512. this->_M_impl._M_start._M_cur,
  513. std::forward<_Args>(__args)...);
  514. #else
  515. this->_M_impl.construct(this->_M_impl._M_start._M_cur, __t);
  516. #endif
  517. }
  518. __catch(...)
  519. {
  520. ++this->_M_impl._M_start;
  521. _M_deallocate_node(*(this->_M_impl._M_start._M_node - 1));
  522. __throw_exception_again;
  523. }
  524. }
  525. // Called only if _M_impl._M_finish._M_cur == _M_impl._M_finish._M_first.
  526. template <typename _Tp, typename _Alloc>
  527. void deque<_Tp, _Alloc>::
  528. _M_pop_back_aux()
  529. {
  530. _M_deallocate_node(this->_M_impl._M_finish._M_first);
  531. this->_M_impl._M_finish._M_set_node(this->_M_impl._M_finish._M_node - 1);
  532. this->_M_impl._M_finish._M_cur = this->_M_impl._M_finish._M_last - 1;
  533. _Alloc_traits::destroy(_M_get_Tp_allocator(),
  534. this->_M_impl._M_finish._M_cur);
  535. }
  536. // Called only if _M_impl._M_start._M_cur == _M_impl._M_start._M_last - 1.
  537. // Note that if the deque has at least one element (a precondition for this
  538. // member function), and if
  539. // _M_impl._M_start._M_cur == _M_impl._M_start._M_last,
  540. // then the deque must have at least two nodes.
  541. template <typename _Tp, typename _Alloc>
  542. void deque<_Tp, _Alloc>::
  543. _M_pop_front_aux()
  544. {
  545. _Alloc_traits::destroy(_M_get_Tp_allocator(),
  546. this->_M_impl._M_start._M_cur);
  547. _M_deallocate_node(this->_M_impl._M_start._M_first);
  548. this->_M_impl._M_start._M_set_node(this->_M_impl._M_start._M_node + 1);
  549. this->_M_impl._M_start._M_cur = this->_M_impl._M_start._M_first;
  550. }
  551. template <typename _Tp, typename _Alloc>
  552. template <typename _InputIterator>
  553. void
  554. deque<_Tp, _Alloc>::
  555. _M_range_insert_aux(iterator __pos,
  556. _InputIterator __first, _InputIterator __last,
  557. std::input_iterator_tag)
  558. { std::copy(__first, __last, std::inserter(*this, __pos)); }
  559. template <typename _Tp, typename _Alloc>
  560. template <typename _ForwardIterator>
  561. void
  562. deque<_Tp, _Alloc>::
  563. _M_range_insert_aux(iterator __pos,
  564. _ForwardIterator __first, _ForwardIterator __last,
  565. std::forward_iterator_tag)
  566. {
  567. const size_type __n = std::distance(__first, __last);
  568. if (__pos._M_cur == this->_M_impl._M_start._M_cur)
  569. {
  570. iterator __new_start = _M_reserve_elements_at_front(__n);
  571. __try
  572. {
  573. std::__uninitialized_copy_a(__first, __last, __new_start,
  574. _M_get_Tp_allocator());
  575. this->_M_impl._M_start = __new_start;
  576. }
  577. __catch(...)
  578. {
  579. _M_destroy_nodes(__new_start._M_node,
  580. this->_M_impl._M_start._M_node);
  581. __throw_exception_again;
  582. }
  583. }
  584. else if (__pos._M_cur == this->_M_impl._M_finish._M_cur)
  585. {
  586. iterator __new_finish = _M_reserve_elements_at_back(__n);
  587. __try
  588. {
  589. std::__uninitialized_copy_a(__first, __last,
  590. this->_M_impl._M_finish,
  591. _M_get_Tp_allocator());
  592. this->_M_impl._M_finish = __new_finish;
  593. }
  594. __catch(...)
  595. {
  596. _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
  597. __new_finish._M_node + 1);
  598. __throw_exception_again;
  599. }
  600. }
  601. else
  602. _M_insert_aux(__pos, __first, __last, __n);
  603. }
  604. template<typename _Tp, typename _Alloc>
  605. #if __cplusplus >= 201103L
  606. template<typename... _Args>
  607. typename deque<_Tp, _Alloc>::iterator
  608. deque<_Tp, _Alloc>::
  609. _M_insert_aux(iterator __pos, _Args&&... __args)
  610. {
  611. value_type __x_copy(std::forward<_Args>(__args)...); // XXX copy
  612. #else
  613. typename deque<_Tp, _Alloc>::iterator
  614. deque<_Tp, _Alloc>::
  615. _M_insert_aux(iterator __pos, const value_type& __x)
  616. {
  617. value_type __x_copy = __x; // XXX copy
  618. #endif
  619. difference_type __index = __pos - this->_M_impl._M_start;
  620. if (static_cast<size_type>(__index) < size() / 2)
  621. {
  622. push_front(_GLIBCXX_MOVE(front()));
  623. iterator __front1 = this->_M_impl._M_start;
  624. ++__front1;
  625. iterator __front2 = __front1;
  626. ++__front2;
  627. __pos = this->_M_impl._M_start + __index;
  628. iterator __pos1 = __pos;
  629. ++__pos1;
  630. _GLIBCXX_MOVE3(__front2, __pos1, __front1);
  631. }
  632. else
  633. {
  634. push_back(_GLIBCXX_MOVE(back()));
  635. iterator __back1 = this->_M_impl._M_finish;
  636. --__back1;
  637. iterator __back2 = __back1;
  638. --__back2;
  639. __pos = this->_M_impl._M_start + __index;
  640. _GLIBCXX_MOVE_BACKWARD3(__pos, __back2, __back1);
  641. }
  642. *__pos = _GLIBCXX_MOVE(__x_copy);
  643. return __pos;
  644. }
  645. template <typename _Tp, typename _Alloc>
  646. void
  647. deque<_Tp, _Alloc>::
  648. _M_insert_aux(iterator __pos, size_type __n, const value_type& __x)
  649. {
  650. const difference_type __elems_before = __pos - this->_M_impl._M_start;
  651. const size_type __length = this->size();
  652. value_type __x_copy = __x;
  653. if (__elems_before < difference_type(__length / 2))
  654. {
  655. iterator __new_start = _M_reserve_elements_at_front(__n);
  656. iterator __old_start = this->_M_impl._M_start;
  657. __pos = this->_M_impl._M_start + __elems_before;
  658. __try
  659. {
  660. if (__elems_before >= difference_type(__n))
  661. {
  662. iterator __start_n = (this->_M_impl._M_start
  663. + difference_type(__n));
  664. std::__uninitialized_move_a(this->_M_impl._M_start,
  665. __start_n, __new_start,
  666. _M_get_Tp_allocator());
  667. this->_M_impl._M_start = __new_start;
  668. _GLIBCXX_MOVE3(__start_n, __pos, __old_start);
  669. std::fill(__pos - difference_type(__n), __pos, __x_copy);
  670. }
  671. else
  672. {
  673. std::__uninitialized_move_fill(this->_M_impl._M_start,
  674. __pos, __new_start,
  675. this->_M_impl._M_start,
  676. __x_copy,
  677. _M_get_Tp_allocator());
  678. this->_M_impl._M_start = __new_start;
  679. std::fill(__old_start, __pos, __x_copy);
  680. }
  681. }
  682. __catch(...)
  683. {
  684. _M_destroy_nodes(__new_start._M_node,
  685. this->_M_impl._M_start._M_node);
  686. __throw_exception_again;
  687. }
  688. }
  689. else
  690. {
  691. iterator __new_finish = _M_reserve_elements_at_back(__n);
  692. iterator __old_finish = this->_M_impl._M_finish;
  693. const difference_type __elems_after =
  694. difference_type(__length) - __elems_before;
  695. __pos = this->_M_impl._M_finish - __elems_after;
  696. __try
  697. {
  698. if (__elems_after > difference_type(__n))
  699. {
  700. iterator __finish_n = (this->_M_impl._M_finish
  701. - difference_type(__n));
  702. std::__uninitialized_move_a(__finish_n,
  703. this->_M_impl._M_finish,
  704. this->_M_impl._M_finish,
  705. _M_get_Tp_allocator());
  706. this->_M_impl._M_finish = __new_finish;
  707. _GLIBCXX_MOVE_BACKWARD3(__pos, __finish_n, __old_finish);
  708. std::fill(__pos, __pos + difference_type(__n), __x_copy);
  709. }
  710. else
  711. {
  712. std::__uninitialized_fill_move(this->_M_impl._M_finish,
  713. __pos + difference_type(__n),
  714. __x_copy, __pos,
  715. this->_M_impl._M_finish,
  716. _M_get_Tp_allocator());
  717. this->_M_impl._M_finish = __new_finish;
  718. std::fill(__pos, __old_finish, __x_copy);
  719. }
  720. }
  721. __catch(...)
  722. {
  723. _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
  724. __new_finish._M_node + 1);
  725. __throw_exception_again;
  726. }
  727. }
  728. }
  729. template <typename _Tp, typename _Alloc>
  730. template <typename _ForwardIterator>
  731. void
  732. deque<_Tp, _Alloc>::
  733. _M_insert_aux(iterator __pos,
  734. _ForwardIterator __first, _ForwardIterator __last,
  735. size_type __n)
  736. {
  737. const difference_type __elemsbefore = __pos - this->_M_impl._M_start;
  738. const size_type __length = size();
  739. if (static_cast<size_type>(__elemsbefore) < __length / 2)
  740. {
  741. iterator __new_start = _M_reserve_elements_at_front(__n);
  742. iterator __old_start = this->_M_impl._M_start;
  743. __pos = this->_M_impl._M_start + __elemsbefore;
  744. __try
  745. {
  746. if (__elemsbefore >= difference_type(__n))
  747. {
  748. iterator __start_n = (this->_M_impl._M_start
  749. + difference_type(__n));
  750. std::__uninitialized_move_a(this->_M_impl._M_start,
  751. __start_n, __new_start,
  752. _M_get_Tp_allocator());
  753. this->_M_impl._M_start = __new_start;
  754. _GLIBCXX_MOVE3(__start_n, __pos, __old_start);
  755. std::copy(__first, __last, __pos - difference_type(__n));
  756. }
  757. else
  758. {
  759. _ForwardIterator __mid = __first;
  760. std::advance(__mid, difference_type(__n) - __elemsbefore);
  761. std::__uninitialized_move_copy(this->_M_impl._M_start,
  762. __pos, __first, __mid,
  763. __new_start,
  764. _M_get_Tp_allocator());
  765. this->_M_impl._M_start = __new_start;
  766. std::copy(__mid, __last, __old_start);
  767. }
  768. }
  769. __catch(...)
  770. {
  771. _M_destroy_nodes(__new_start._M_node,
  772. this->_M_impl._M_start._M_node);
  773. __throw_exception_again;
  774. }
  775. }
  776. else
  777. {
  778. iterator __new_finish = _M_reserve_elements_at_back(__n);
  779. iterator __old_finish = this->_M_impl._M_finish;
  780. const difference_type __elemsafter =
  781. difference_type(__length) - __elemsbefore;
  782. __pos = this->_M_impl._M_finish - __elemsafter;
  783. __try
  784. {
  785. if (__elemsafter > difference_type(__n))
  786. {
  787. iterator __finish_n = (this->_M_impl._M_finish
  788. - difference_type(__n));
  789. std::__uninitialized_move_a(__finish_n,
  790. this->_M_impl._M_finish,
  791. this->_M_impl._M_finish,
  792. _M_get_Tp_allocator());
  793. this->_M_impl._M_finish = __new_finish;
  794. _GLIBCXX_MOVE_BACKWARD3(__pos, __finish_n, __old_finish);
  795. std::copy(__first, __last, __pos);
  796. }
  797. else
  798. {
  799. _ForwardIterator __mid = __first;
  800. std::advance(__mid, __elemsafter);
  801. std::__uninitialized_copy_move(__mid, __last, __pos,
  802. this->_M_impl._M_finish,
  803. this->_M_impl._M_finish,
  804. _M_get_Tp_allocator());
  805. this->_M_impl._M_finish = __new_finish;
  806. std::copy(__first, __mid, __pos);
  807. }
  808. }
  809. __catch(...)
  810. {
  811. _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
  812. __new_finish._M_node + 1);
  813. __throw_exception_again;
  814. }
  815. }
  816. }
  817. template<typename _Tp, typename _Alloc>
  818. void
  819. deque<_Tp, _Alloc>::
  820. _M_destroy_data_aux(iterator __first, iterator __last)
  821. {
  822. for (_Map_pointer __node = __first._M_node + 1;
  823. __node < __last._M_node; ++__node)
  824. std::_Destroy(*__node, *__node + _S_buffer_size(),
  825. _M_get_Tp_allocator());
  826. if (__first._M_node != __last._M_node)
  827. {
  828. std::_Destroy(__first._M_cur, __first._M_last,
  829. _M_get_Tp_allocator());
  830. std::_Destroy(__last._M_first, __last._M_cur,
  831. _M_get_Tp_allocator());
  832. }
  833. else
  834. std::_Destroy(__first._M_cur, __last._M_cur,
  835. _M_get_Tp_allocator());
  836. }
  837. template <typename _Tp, typename _Alloc>
  838. void
  839. deque<_Tp, _Alloc>::
  840. _M_new_elements_at_front(size_type __new_elems)
  841. {
  842. if (this->max_size() - this->size() < __new_elems)
  843. __throw_length_error(__N("deque::_M_new_elements_at_front"));
  844. const size_type __new_nodes = ((__new_elems + _S_buffer_size() - 1)
  845. / _S_buffer_size());
  846. _M_reserve_map_at_front(__new_nodes);
  847. size_type __i;
  848. __try
  849. {
  850. for (__i = 1; __i <= __new_nodes; ++__i)
  851. *(this->_M_impl._M_start._M_node - __i) = this->_M_allocate_node();
  852. }
  853. __catch(...)
  854. {
  855. for (size_type __j = 1; __j < __i; ++__j)
  856. _M_deallocate_node(*(this->_M_impl._M_start._M_node - __j));
  857. __throw_exception_again;
  858. }
  859. }
  860. template <typename _Tp, typename _Alloc>
  861. void
  862. deque<_Tp, _Alloc>::
  863. _M_new_elements_at_back(size_type __new_elems)
  864. {
  865. if (this->max_size() - this->size() < __new_elems)
  866. __throw_length_error(__N("deque::_M_new_elements_at_back"));
  867. const size_type __new_nodes = ((__new_elems + _S_buffer_size() - 1)
  868. / _S_buffer_size());
  869. _M_reserve_map_at_back(__new_nodes);
  870. size_type __i;
  871. __try
  872. {
  873. for (__i = 1; __i <= __new_nodes; ++__i)
  874. *(this->_M_impl._M_finish._M_node + __i) = this->_M_allocate_node();
  875. }
  876. __catch(...)
  877. {
  878. for (size_type __j = 1; __j < __i; ++__j)
  879. _M_deallocate_node(*(this->_M_impl._M_finish._M_node + __j));
  880. __throw_exception_again;
  881. }
  882. }
  883. template <typename _Tp, typename _Alloc>
  884. void
  885. deque<_Tp, _Alloc>::
  886. _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front)
  887. {
  888. const size_type __old_num_nodes
  889. = this->_M_impl._M_finish._M_node - this->_M_impl._M_start._M_node + 1;
  890. const size_type __new_num_nodes = __old_num_nodes + __nodes_to_add;
  891. _Map_pointer __new_nstart;
  892. if (this->_M_impl._M_map_size > 2 * __new_num_nodes)
  893. {
  894. __new_nstart = this->_M_impl._M_map + (this->_M_impl._M_map_size
  895. - __new_num_nodes) / 2
  896. + (__add_at_front ? __nodes_to_add : 0);
  897. if (__new_nstart < this->_M_impl._M_start._M_node)
  898. std::copy(this->_M_impl._M_start._M_node,
  899. this->_M_impl._M_finish._M_node + 1,
  900. __new_nstart);
  901. else
  902. std::copy_backward(this->_M_impl._M_start._M_node,
  903. this->_M_impl._M_finish._M_node + 1,
  904. __new_nstart + __old_num_nodes);
  905. }
  906. else
  907. {
  908. size_type __new_map_size = this->_M_impl._M_map_size
  909. + std::max(this->_M_impl._M_map_size,
  910. __nodes_to_add) + 2;
  911. _Map_pointer __new_map = this->_M_allocate_map(__new_map_size);
  912. __new_nstart = __new_map + (__new_map_size - __new_num_nodes) / 2
  913. + (__add_at_front ? __nodes_to_add : 0);
  914. std::copy(this->_M_impl._M_start._M_node,
  915. this->_M_impl._M_finish._M_node + 1,
  916. __new_nstart);
  917. _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
  918. this->_M_impl._M_map = __new_map;
  919. this->_M_impl._M_map_size = __new_map_size;
  920. }
  921. this->_M_impl._M_start._M_set_node(__new_nstart);
  922. this->_M_impl._M_finish._M_set_node(__new_nstart + __old_num_nodes - 1);
  923. }
  924. _GLIBCXX_END_NAMESPACE_CONTAINER
  925. // Overload for deque::iterators, exploiting the "segmented-iterator
  926. // optimization".
  927. template<typename _Tp, typename _VTp>
  928. void
  929. __fill_a1(const _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*>& __first,
  930. const _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*>& __last,
  931. const _VTp& __value)
  932. {
  933. typedef _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*> _Iter;
  934. if (__first._M_node != __last._M_node)
  935. {
  936. std::__fill_a1(__first._M_cur, __first._M_last, __value);
  937. for (typename _Iter::_Map_pointer __node = __first._M_node + 1;
  938. __node < __last._M_node; ++__node)
  939. std::__fill_a1(*__node, *__node + _Iter::_S_buffer_size(), __value);
  940. std::__fill_a1(__last._M_first, __last._M_cur, __value);
  941. }
  942. else
  943. std::__fill_a1(__first._M_cur, __last._M_cur, __value);
  944. }
  945. template<bool _IsMove,
  946. typename _Tp, typename _Ref, typename _Ptr, typename _OI>
  947. _OI
  948. __copy_move_dit(_GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr> __first,
  949. _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr> __last,
  950. _OI __result)
  951. {
  952. typedef _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr> _Iter;
  953. if (__first._M_node != __last._M_node)
  954. {
  955. __result
  956. = std::__copy_move_a1<_IsMove>(__first._M_cur, __first._M_last,
  957. __result);
  958. for (typename _Iter::_Map_pointer __node = __first._M_node + 1;
  959. __node != __last._M_node; ++__node)
  960. __result
  961. = std::__copy_move_a1<_IsMove>(*__node,
  962. *__node + _Iter::_S_buffer_size(),
  963. __result);
  964. return std::__copy_move_a1<_IsMove>(__last._M_first, __last._M_cur,
  965. __result);
  966. }
  967. return std::__copy_move_a1<_IsMove>(__first._M_cur, __last._M_cur,
  968. __result);
  969. }
  970. template<bool _IsMove,
  971. typename _Tp, typename _Ref, typename _Ptr, typename _OI>
  972. _OI
  973. __copy_move_a1(_GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr> __first,
  974. _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr> __last,
  975. _OI __result)
  976. { return __copy_move_dit<_IsMove>(__first, __last, __result); }
  977. template<bool _IsMove,
  978. typename _ITp, typename _IRef, typename _IPtr, typename _OTp>
  979. _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>
  980. __copy_move_a1(_GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr> __first,
  981. _GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr> __last,
  982. _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*> __result)
  983. { return __copy_move_dit<_IsMove>(__first, __last, __result); }
  984. template<bool _IsMove, typename _II, typename _Tp>
  985. typename __gnu_cxx::__enable_if<
  986. __is_random_access_iter<_II>::__value,
  987. _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*> >::__type
  988. __copy_move_a1(_II __first, _II __last,
  989. _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*> __result)
  990. {
  991. typedef _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*> _Iter;
  992. typedef typename _Iter::difference_type difference_type;
  993. difference_type __len = __last - __first;
  994. while (__len > 0)
  995. {
  996. const difference_type __clen
  997. = std::min(__len, __result._M_last - __result._M_cur);
  998. std::__copy_move_a1<_IsMove>(__first, __first + __clen,
  999. __result._M_cur);
  1000. __first += __clen;
  1001. __result += __clen;
  1002. __len -= __clen;
  1003. }
  1004. return __result;
  1005. }
  1006. template<bool _IsMove, typename _CharT>
  1007. typename __gnu_cxx::__enable_if<
  1008. __is_char<_CharT>::__value,
  1009. _GLIBCXX_STD_C::_Deque_iterator<_CharT, _CharT&, _CharT*> >::__type
  1010. __copy_move_a2(
  1011. istreambuf_iterator<_CharT, char_traits<_CharT> > __first,
  1012. istreambuf_iterator<_CharT, char_traits<_CharT> > __last,
  1013. _GLIBCXX_STD_C::_Deque_iterator<_CharT, _CharT&, _CharT*> __result)
  1014. {
  1015. if (__first == __last)
  1016. return __result;
  1017. for (;;)
  1018. {
  1019. const std::ptrdiff_t __len = __result._M_last - __result._M_cur;
  1020. const std::ptrdiff_t __nb
  1021. = std::__copy_n_a(__first, __len, __result._M_cur, false)
  1022. - __result._M_cur;
  1023. __result += __nb;
  1024. if (__nb != __len)
  1025. break;
  1026. }
  1027. return __result;
  1028. }
  1029. template<typename _CharT, typename _Size>
  1030. typename __gnu_cxx::__enable_if<
  1031. __is_char<_CharT>::__value,
  1032. _GLIBCXX_STD_C::_Deque_iterator<_CharT, _CharT&, _CharT*> >::__type
  1033. __copy_n_a(
  1034. istreambuf_iterator<_CharT, char_traits<_CharT> > __it, _Size __size,
  1035. _GLIBCXX_STD_C::_Deque_iterator<_CharT, _CharT&, _CharT*> __result,
  1036. bool __strict)
  1037. {
  1038. if (__size == 0)
  1039. return __result;
  1040. do
  1041. {
  1042. const _Size __len
  1043. = std::min<_Size>(__result._M_last - __result._M_cur, __size);
  1044. std::__copy_n_a(__it, __len, __result._M_cur, __strict);
  1045. __result += __len;
  1046. __size -= __len;
  1047. }
  1048. while (__size != 0);
  1049. return __result;
  1050. }
  1051. template<bool _IsMove,
  1052. typename _Tp, typename _Ref, typename _Ptr, typename _OI>
  1053. _OI
  1054. __copy_move_backward_dit(
  1055. _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr> __first,
  1056. _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr> __last,
  1057. _OI __result)
  1058. {
  1059. typedef _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr> _Iter;
  1060. if (__first._M_node != __last._M_node)
  1061. {
  1062. __result = std::__copy_move_backward_a1<_IsMove>(
  1063. __last._M_first, __last._M_cur, __result);
  1064. for (typename _Iter::_Map_pointer __node = __last._M_node - 1;
  1065. __node != __first._M_node; --__node)
  1066. __result = std::__copy_move_backward_a1<_IsMove>(
  1067. *__node, *__node + _Iter::_S_buffer_size(), __result);
  1068. return std::__copy_move_backward_a1<_IsMove>(
  1069. __first._M_cur, __first._M_last, __result);
  1070. }
  1071. return std::__copy_move_backward_a1<_IsMove>(
  1072. __first._M_cur, __last._M_cur, __result);
  1073. }
  1074. template<bool _IsMove,
  1075. typename _Tp, typename _Ref, typename _Ptr, typename _OI>
  1076. _OI
  1077. __copy_move_backward_a1(
  1078. _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr> __first,
  1079. _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr> __last,
  1080. _OI __result)
  1081. { return __copy_move_backward_dit<_IsMove>(__first, __last, __result); }
  1082. template<bool _IsMove,
  1083. typename _ITp, typename _IRef, typename _IPtr, typename _OTp>
  1084. _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>
  1085. __copy_move_backward_a1(
  1086. _GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr> __first,
  1087. _GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr> __last,
  1088. _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*> __result)
  1089. { return __copy_move_backward_dit<_IsMove>(__first, __last, __result); }
  1090. template<bool _IsMove, typename _II, typename _Tp>
  1091. typename __gnu_cxx::__enable_if<
  1092. __is_random_access_iter<_II>::__value,
  1093. _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*> >::__type
  1094. __copy_move_backward_a1(_II __first, _II __last,
  1095. _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*> __result)
  1096. {
  1097. typedef _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*> _Iter;
  1098. typedef typename _Iter::difference_type difference_type;
  1099. difference_type __len = __last - __first;
  1100. while (__len > 0)
  1101. {
  1102. difference_type __rlen = __result._M_cur - __result._M_first;
  1103. _Tp* __rend = __result._M_cur;
  1104. if (!__rlen)
  1105. {
  1106. __rlen = _Iter::_S_buffer_size();
  1107. __rend = *(__result._M_node - 1) + __rlen;
  1108. }
  1109. const difference_type __clen = std::min(__len, __rlen);
  1110. std::__copy_move_backward_a1<_IsMove>(__last - __clen, __last, __rend);
  1111. __last -= __clen;
  1112. __result -= __clen;
  1113. __len -= __clen;
  1114. }
  1115. return __result;
  1116. }
  1117. template<typename _Tp, typename _Ref, typename _Ptr, typename _II>
  1118. bool
  1119. __equal_dit(
  1120. const _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>& __first1,
  1121. const _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>& __last1,
  1122. _II __first2)
  1123. {
  1124. typedef _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr> _Iter;
  1125. if (__first1._M_node != __last1._M_node)
  1126. {
  1127. if (!std::__equal_aux1(__first1._M_cur, __first1._M_last, __first2))
  1128. return false;
  1129. __first2 += __first1._M_last - __first1._M_cur;
  1130. for (typename _Iter::_Map_pointer __node = __first1._M_node + 1;
  1131. __node != __last1._M_node;
  1132. __first2 += _Iter::_S_buffer_size(), ++__node)
  1133. if (!std::__equal_aux1(*__node, *__node + _Iter::_S_buffer_size(),
  1134. __first2))
  1135. return false;
  1136. return std::__equal_aux1(__last1._M_first, __last1._M_cur, __first2);
  1137. }
  1138. return std::__equal_aux1(__first1._M_cur, __last1._M_cur, __first2);
  1139. }
  1140. template<typename _Tp, typename _Ref, typename _Ptr, typename _II>
  1141. typename __gnu_cxx::__enable_if<
  1142. __is_random_access_iter<_II>::__value, bool>::__type
  1143. __equal_aux1(_GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr> __first1,
  1144. _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr> __last1,
  1145. _II __first2)
  1146. { return std::__equal_dit(__first1, __last1, __first2); }
  1147. template<typename _Tp1, typename _Ref1, typename _Ptr1,
  1148. typename _Tp2, typename _Ref2, typename _Ptr2>
  1149. bool
  1150. __equal_aux1(_GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1> __first1,
  1151. _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1> __last1,
  1152. _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2> __first2)
  1153. { return std::__equal_dit(__first1, __last1, __first2); }
  1154. template<typename _II, typename _Tp, typename _Ref, typename _Ptr>
  1155. typename __gnu_cxx::__enable_if<
  1156. __is_random_access_iter<_II>::__value, bool>::__type
  1157. __equal_aux1(_II __first1, _II __last1,
  1158. _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr> __first2)
  1159. {
  1160. typedef _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr> _Iter;
  1161. typedef typename _Iter::difference_type difference_type;
  1162. difference_type __len = __last1 - __first1;
  1163. while (__len > 0)
  1164. {
  1165. const difference_type __clen
  1166. = std::min(__len, __first2._M_last - __first2._M_cur);
  1167. if (!std::__equal_aux1(__first1, __first1 + __clen, __first2._M_cur))
  1168. return false;
  1169. __first1 += __clen;
  1170. __len -= __clen;
  1171. __first2 += __clen;
  1172. }
  1173. return true;
  1174. }
  1175. template<typename _Tp1, typename _Ref, typename _Ptr, typename _Tp2>
  1176. int
  1177. __lex_cmp_dit(
  1178. _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref, _Ptr> __first1,
  1179. _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref, _Ptr> __last1,
  1180. const _Tp2* __first2, const _Tp2* __last2)
  1181. {
  1182. const bool __simple =
  1183. (__is_memcmp_ordered_with<_Tp1, _Tp2>::__value
  1184. && __is_pointer<_Ptr>::__value
  1185. #if __cplusplus > 201703L && __cpp_lib_concepts
  1186. // For C++20 iterator_traits<volatile T*>::value_type is non-volatile
  1187. // so __is_byte<T> could be true, but we can't use memcmp with
  1188. // volatile data.
  1189. && !is_volatile_v<_Tp1>
  1190. && !is_volatile_v<_Tp2>
  1191. #endif
  1192. );
  1193. typedef std::__lexicographical_compare<__simple> _Lc;
  1194. while (__first1._M_node != __last1._M_node)
  1195. {
  1196. const ptrdiff_t __len1 = __first1._M_last - __first1._M_cur;
  1197. const ptrdiff_t __len2 = __last2 - __first2;
  1198. const ptrdiff_t __len = std::min(__len1, __len2);
  1199. // if __len1 > __len2 this will return a positive value:
  1200. if (int __ret = _Lc::__3way(__first1._M_cur, __first1._M_last,
  1201. __first2, __first2 + __len))
  1202. return __ret;
  1203. __first1 += __len;
  1204. __first2 += __len;
  1205. }
  1206. return _Lc::__3way(__first1._M_cur, __last1._M_cur,
  1207. __first2, __last2);
  1208. }
  1209. template<typename _Tp1, typename _Ref1, typename _Ptr1,
  1210. typename _Tp2>
  1211. inline bool
  1212. __lexicographical_compare_aux1(
  1213. _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1> __first1,
  1214. _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1> __last1,
  1215. _Tp2* __first2, _Tp2* __last2)
  1216. { return std::__lex_cmp_dit(__first1, __last1, __first2, __last2) < 0; }
  1217. template<typename _Tp1,
  1218. typename _Tp2, typename _Ref2, typename _Ptr2>
  1219. inline bool
  1220. __lexicographical_compare_aux1(_Tp1* __first1, _Tp1* __last1,
  1221. _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2> __first2,
  1222. _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2> __last2)
  1223. { return std::__lex_cmp_dit(__first2, __last2, __first1, __last1) > 0; }
  1224. template<typename _Tp1, typename _Ref1, typename _Ptr1,
  1225. typename _Tp2, typename _Ref2, typename _Ptr2>
  1226. inline bool
  1227. __lexicographical_compare_aux1(
  1228. _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1> __first1,
  1229. _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1> __last1,
  1230. _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2> __first2,
  1231. _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2> __last2)
  1232. {
  1233. const bool __simple =
  1234. (__is_memcmp_ordered_with<_Tp1, _Tp2>::__value
  1235. && __is_pointer<_Ptr1>::__value
  1236. && __is_pointer<_Ptr2>::__value
  1237. #if __cplusplus > 201703L && __cpp_lib_concepts
  1238. // For C++20 iterator_traits<volatile T*>::value_type is non-volatile
  1239. // so __is_byte<T> could be true, but we can't use memcmp with
  1240. // volatile data.
  1241. && !is_volatile_v<_Tp1>
  1242. && !is_volatile_v<_Tp2>
  1243. #endif
  1244. );
  1245. typedef std::__lexicographical_compare<__simple> _Lc;
  1246. while (__first1 != __last1)
  1247. {
  1248. const ptrdiff_t __len2 = __first2._M_node == __last2._M_node
  1249. ? __last2._M_cur - __first2._M_cur
  1250. : __first2._M_last - __first2._M_cur;
  1251. if (__len2 == 0)
  1252. return false;
  1253. const ptrdiff_t __len1 = __first1._M_node == __last1._M_node
  1254. ? __last1._M_cur - __first1._M_cur
  1255. : __first1._M_last - __first1._M_cur;
  1256. const ptrdiff_t __len = std::min(__len1, __len2);
  1257. if (int __ret = _Lc::__3way(__first1._M_cur, __first1._M_cur + __len,
  1258. __first2._M_cur, __first2._M_cur + __len))
  1259. return __ret < 0;
  1260. __first1 += __len;
  1261. __first2 += __len;
  1262. }
  1263. return __last2 != __first2;
  1264. }
  1265. _GLIBCXX_END_NAMESPACE_VERSION
  1266. } // namespace std
  1267. #endif