You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2331 lines
75KB

  1. // Deque implementation -*- C++ -*-
  2. // Copyright (C) 2001-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. *
  22. * Copyright (c) 1994
  23. * Hewlett-Packard Company
  24. *
  25. * Permission to use, copy, modify, distribute and sell this software
  26. * and its documentation for any purpose is hereby granted without fee,
  27. * provided that the above copyright notice appear in all copies and
  28. * that both that copyright notice and this permission notice appear
  29. * in supporting documentation. Hewlett-Packard Company makes no
  30. * representations about the suitability of this software for any
  31. * purpose. It is provided "as is" without express or implied warranty.
  32. *
  33. *
  34. * Copyright (c) 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/stl_deque.h
  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 _STL_DEQUE_H
  50. #define _STL_DEQUE_H 1
  51. #include <bits/concept_check.h>
  52. #include <bits/stl_iterator_base_types.h>
  53. #include <bits/stl_iterator_base_funcs.h>
  54. #if __cplusplus >= 201103L
  55. #include <initializer_list>
  56. #include <bits/stl_uninitialized.h> // for __is_bitwise_relocatable
  57. #endif
  58. #if __cplusplus > 201703L
  59. # include <compare>
  60. #endif
  61. #include <debug/assertions.h>
  62. namespace std _GLIBCXX_VISIBILITY(default)
  63. {
  64. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  65. _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
  66. /**
  67. * @brief This function controls the size of memory nodes.
  68. * @param __size The size of an element.
  69. * @return The number (not byte size) of elements per node.
  70. *
  71. * This function started off as a compiler kludge from SGI, but
  72. * seems to be a useful wrapper around a repeated constant
  73. * expression. The @b 512 is tunable (and no other code needs to
  74. * change), but no investigation has been done since inheriting the
  75. * SGI code. Touch _GLIBCXX_DEQUE_BUF_SIZE only if you know what
  76. * you are doing, however: changing it breaks the binary
  77. * compatibility!!
  78. */
  79. #ifndef _GLIBCXX_DEQUE_BUF_SIZE
  80. #define _GLIBCXX_DEQUE_BUF_SIZE 512
  81. #endif
  82. _GLIBCXX_CONSTEXPR inline size_t
  83. __deque_buf_size(size_t __size)
  84. { return (__size < _GLIBCXX_DEQUE_BUF_SIZE
  85. ? size_t(_GLIBCXX_DEQUE_BUF_SIZE / __size) : size_t(1)); }
  86. /**
  87. * @brief A deque::iterator.
  88. *
  89. * Quite a bit of intelligence here. Much of the functionality of
  90. * deque is actually passed off to this class. A deque holds two
  91. * of these internally, marking its valid range. Access to
  92. * elements is done as offsets of either of those two, relying on
  93. * operator overloading in this class.
  94. *
  95. * All the functions are op overloads except for _M_set_node.
  96. */
  97. template<typename _Tp, typename _Ref, typename _Ptr>
  98. struct _Deque_iterator
  99. {
  100. #if __cplusplus < 201103L
  101. typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator;
  102. typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
  103. typedef _Tp* _Elt_pointer;
  104. typedef _Tp** _Map_pointer;
  105. #else
  106. private:
  107. template<typename _CvTp>
  108. using __iter = _Deque_iterator<_Tp, _CvTp&, __ptr_rebind<_Ptr, _CvTp>>;
  109. public:
  110. typedef __iter<_Tp> iterator;
  111. typedef __iter<const _Tp> const_iterator;
  112. typedef __ptr_rebind<_Ptr, _Tp> _Elt_pointer;
  113. typedef __ptr_rebind<_Ptr, _Elt_pointer> _Map_pointer;
  114. #endif
  115. static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT
  116. { return __deque_buf_size(sizeof(_Tp)); }
  117. typedef std::random_access_iterator_tag iterator_category;
  118. typedef _Tp value_type;
  119. typedef _Ptr pointer;
  120. typedef _Ref reference;
  121. typedef size_t size_type;
  122. typedef ptrdiff_t difference_type;
  123. typedef _Deque_iterator _Self;
  124. _Elt_pointer _M_cur;
  125. _Elt_pointer _M_first;
  126. _Elt_pointer _M_last;
  127. _Map_pointer _M_node;
  128. _Deque_iterator(_Elt_pointer __x, _Map_pointer __y) _GLIBCXX_NOEXCEPT
  129. : _M_cur(__x), _M_first(*__y),
  130. _M_last(*__y + _S_buffer_size()), _M_node(__y) { }
  131. _Deque_iterator() _GLIBCXX_NOEXCEPT
  132. : _M_cur(), _M_first(), _M_last(), _M_node() { }
  133. #if __cplusplus < 201103L
  134. // Conversion from iterator to const_iterator.
  135. _Deque_iterator(const iterator& __x) _GLIBCXX_NOEXCEPT
  136. : _M_cur(__x._M_cur), _M_first(__x._M_first),
  137. _M_last(__x._M_last), _M_node(__x._M_node) { }
  138. #else
  139. // Conversion from iterator to const_iterator.
  140. template<typename _Iter,
  141. typename = _Require<is_same<_Self, const_iterator>,
  142. is_same<_Iter, iterator>>>
  143. _Deque_iterator(const _Iter& __x) noexcept
  144. : _M_cur(__x._M_cur), _M_first(__x._M_first),
  145. _M_last(__x._M_last), _M_node(__x._M_node) { }
  146. _Deque_iterator(const _Deque_iterator& __x) noexcept
  147. : _M_cur(__x._M_cur), _M_first(__x._M_first),
  148. _M_last(__x._M_last), _M_node(__x._M_node) { }
  149. _Deque_iterator& operator=(const _Deque_iterator&) = default;
  150. #endif
  151. iterator
  152. _M_const_cast() const _GLIBCXX_NOEXCEPT
  153. { return iterator(_M_cur, _M_node); }
  154. reference
  155. operator*() const _GLIBCXX_NOEXCEPT
  156. { return *_M_cur; }
  157. pointer
  158. operator->() const _GLIBCXX_NOEXCEPT
  159. { return _M_cur; }
  160. _Self&
  161. operator++() _GLIBCXX_NOEXCEPT
  162. {
  163. ++_M_cur;
  164. if (_M_cur == _M_last)
  165. {
  166. _M_set_node(_M_node + 1);
  167. _M_cur = _M_first;
  168. }
  169. return *this;
  170. }
  171. _Self
  172. operator++(int) _GLIBCXX_NOEXCEPT
  173. {
  174. _Self __tmp = *this;
  175. ++*this;
  176. return __tmp;
  177. }
  178. _Self&
  179. operator--() _GLIBCXX_NOEXCEPT
  180. {
  181. if (_M_cur == _M_first)
  182. {
  183. _M_set_node(_M_node - 1);
  184. _M_cur = _M_last;
  185. }
  186. --_M_cur;
  187. return *this;
  188. }
  189. _Self
  190. operator--(int) _GLIBCXX_NOEXCEPT
  191. {
  192. _Self __tmp = *this;
  193. --*this;
  194. return __tmp;
  195. }
  196. _Self&
  197. operator+=(difference_type __n) _GLIBCXX_NOEXCEPT
  198. {
  199. const difference_type __offset = __n + (_M_cur - _M_first);
  200. if (__offset >= 0 && __offset < difference_type(_S_buffer_size()))
  201. _M_cur += __n;
  202. else
  203. {
  204. const difference_type __node_offset =
  205. __offset > 0 ? __offset / difference_type(_S_buffer_size())
  206. : -difference_type((-__offset - 1)
  207. / _S_buffer_size()) - 1;
  208. _M_set_node(_M_node + __node_offset);
  209. _M_cur = _M_first + (__offset - __node_offset
  210. * difference_type(_S_buffer_size()));
  211. }
  212. return *this;
  213. }
  214. _Self&
  215. operator-=(difference_type __n) _GLIBCXX_NOEXCEPT
  216. { return *this += -__n; }
  217. reference
  218. operator[](difference_type __n) const _GLIBCXX_NOEXCEPT
  219. { return *(*this + __n); }
  220. /**
  221. * Prepares to traverse new_node. Sets everything except
  222. * _M_cur, which should therefore be set by the caller
  223. * immediately afterwards, based on _M_first and _M_last.
  224. */
  225. void
  226. _M_set_node(_Map_pointer __new_node) _GLIBCXX_NOEXCEPT
  227. {
  228. _M_node = __new_node;
  229. _M_first = *__new_node;
  230. _M_last = _M_first + difference_type(_S_buffer_size());
  231. }
  232. friend bool
  233. operator==(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
  234. { return __x._M_cur == __y._M_cur; }
  235. // Note: we also provide overloads whose operands are of the same type in
  236. // order to avoid ambiguous overload resolution when std::rel_ops
  237. // operators are in scope (for additional details, see libstdc++/3628)
  238. template<typename _RefR, typename _PtrR>
  239. friend bool
  240. operator==(const _Self& __x,
  241. const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
  242. _GLIBCXX_NOEXCEPT
  243. { return __x._M_cur == __y._M_cur; }
  244. #if __cpp_lib_three_way_comparison
  245. friend strong_ordering
  246. operator<=>(const _Self& __x, const _Self& __y) noexcept
  247. {
  248. if (const auto __cmp = __x._M_node <=> __y._M_node; __cmp != 0)
  249. return __cmp;
  250. return __x._M_cur <=> __y._M_cur;
  251. }
  252. #else
  253. friend bool
  254. operator!=(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
  255. { return !(__x == __y); }
  256. template<typename _RefR, typename _PtrR>
  257. friend bool
  258. operator!=(const _Self& __x,
  259. const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
  260. _GLIBCXX_NOEXCEPT
  261. { return !(__x == __y); }
  262. friend bool
  263. operator<(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
  264. {
  265. return (__x._M_node == __y._M_node)
  266. ? (__x._M_cur < __y._M_cur) : (__x._M_node < __y._M_node);
  267. }
  268. template<typename _RefR, typename _PtrR>
  269. friend bool
  270. operator<(const _Self& __x,
  271. const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
  272. _GLIBCXX_NOEXCEPT
  273. {
  274. return (__x._M_node == __y._M_node)
  275. ? (__x._M_cur < __y._M_cur) : (__x._M_node < __y._M_node);
  276. }
  277. friend bool
  278. operator>(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
  279. { return __y < __x; }
  280. template<typename _RefR, typename _PtrR>
  281. friend bool
  282. operator>(const _Self& __x,
  283. const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
  284. _GLIBCXX_NOEXCEPT
  285. { return __y < __x; }
  286. friend bool
  287. operator<=(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
  288. { return !(__y < __x); }
  289. template<typename _RefR, typename _PtrR>
  290. friend bool
  291. operator<=(const _Self& __x,
  292. const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
  293. _GLIBCXX_NOEXCEPT
  294. { return !(__y < __x); }
  295. friend bool
  296. operator>=(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
  297. { return !(__x < __y); }
  298. template<typename _RefR, typename _PtrR>
  299. friend bool
  300. operator>=(const _Self& __x,
  301. const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
  302. _GLIBCXX_NOEXCEPT
  303. { return !(__x < __y); }
  304. #endif // three-way comparison
  305. friend difference_type
  306. operator-(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
  307. {
  308. return difference_type(_S_buffer_size())
  309. * (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first)
  310. + (__y._M_last - __y._M_cur);
  311. }
  312. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  313. // According to the resolution of DR179 not only the various comparison
  314. // operators but also operator- must accept mixed iterator/const_iterator
  315. // parameters.
  316. template<typename _RefR, typename _PtrR>
  317. friend difference_type
  318. operator-(const _Self& __x,
  319. const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
  320. {
  321. return difference_type(_S_buffer_size())
  322. * (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first)
  323. + (__y._M_last - __y._M_cur);
  324. }
  325. friend _Self
  326. operator+(const _Self& __x, difference_type __n) _GLIBCXX_NOEXCEPT
  327. {
  328. _Self __tmp = __x;
  329. __tmp += __n;
  330. return __tmp;
  331. }
  332. friend _Self
  333. operator-(const _Self& __x, difference_type __n) _GLIBCXX_NOEXCEPT
  334. {
  335. _Self __tmp = __x;
  336. __tmp -= __n;
  337. return __tmp;
  338. }
  339. friend _Self
  340. operator+(difference_type __n, const _Self& __x) _GLIBCXX_NOEXCEPT
  341. { return __x + __n; }
  342. };
  343. /**
  344. * Deque base class. This class provides the unified face for %deque's
  345. * allocation. This class's constructor and destructor allocate and
  346. * deallocate (but do not initialize) storage. This makes %exception
  347. * safety easier.
  348. *
  349. * Nothing in this class ever constructs or destroys an actual Tp element.
  350. * (Deque handles that itself.) Only/All memory management is performed
  351. * here.
  352. */
  353. template<typename _Tp, typename _Alloc>
  354. class _Deque_base
  355. {
  356. protected:
  357. typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
  358. rebind<_Tp>::other _Tp_alloc_type;
  359. typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Alloc_traits;
  360. #if __cplusplus < 201103L
  361. typedef _Tp* _Ptr;
  362. typedef const _Tp* _Ptr_const;
  363. #else
  364. typedef typename _Alloc_traits::pointer _Ptr;
  365. typedef typename _Alloc_traits::const_pointer _Ptr_const;
  366. #endif
  367. typedef typename _Alloc_traits::template rebind<_Ptr>::other
  368. _Map_alloc_type;
  369. typedef __gnu_cxx::__alloc_traits<_Map_alloc_type> _Map_alloc_traits;
  370. typedef _Alloc allocator_type;
  371. allocator_type
  372. get_allocator() const _GLIBCXX_NOEXCEPT
  373. { return allocator_type(_M_get_Tp_allocator()); }
  374. typedef _Deque_iterator<_Tp, _Tp&, _Ptr> iterator;
  375. typedef _Deque_iterator<_Tp, const _Tp&, _Ptr_const> const_iterator;
  376. _Deque_base()
  377. : _M_impl()
  378. { _M_initialize_map(0); }
  379. _Deque_base(size_t __num_elements)
  380. : _M_impl()
  381. { _M_initialize_map(__num_elements); }
  382. _Deque_base(const allocator_type& __a, size_t __num_elements)
  383. : _M_impl(__a)
  384. { _M_initialize_map(__num_elements); }
  385. _Deque_base(const allocator_type& __a)
  386. : _M_impl(__a)
  387. { /* Caller must initialize map. */ }
  388. #if __cplusplus >= 201103L
  389. _Deque_base(_Deque_base&& __x)
  390. : _M_impl(std::move(__x._M_get_Tp_allocator()))
  391. {
  392. _M_initialize_map(0);
  393. if (__x._M_impl._M_map)
  394. this->_M_impl._M_swap_data(__x._M_impl);
  395. }
  396. _Deque_base(_Deque_base&& __x, const allocator_type& __a)
  397. : _M_impl(std::move(__x._M_impl), _Tp_alloc_type(__a))
  398. { __x._M_initialize_map(0); }
  399. _Deque_base(_Deque_base&& __x, const allocator_type& __a, size_t __n)
  400. : _M_impl(__a)
  401. {
  402. if (__x.get_allocator() == __a)
  403. {
  404. if (__x._M_impl._M_map)
  405. {
  406. _M_initialize_map(0);
  407. this->_M_impl._M_swap_data(__x._M_impl);
  408. }
  409. }
  410. else
  411. {
  412. _M_initialize_map(__n);
  413. }
  414. }
  415. #endif
  416. ~_Deque_base() _GLIBCXX_NOEXCEPT;
  417. typedef typename iterator::_Map_pointer _Map_pointer;
  418. struct _Deque_impl_data
  419. {
  420. _Map_pointer _M_map;
  421. size_t _M_map_size;
  422. iterator _M_start;
  423. iterator _M_finish;
  424. _Deque_impl_data() _GLIBCXX_NOEXCEPT
  425. : _M_map(), _M_map_size(), _M_start(), _M_finish()
  426. { }
  427. #if __cplusplus >= 201103L
  428. _Deque_impl_data(const _Deque_impl_data&) = default;
  429. _Deque_impl_data&
  430. operator=(const _Deque_impl_data&) = default;
  431. _Deque_impl_data(_Deque_impl_data&& __x) noexcept
  432. : _Deque_impl_data(__x)
  433. { __x = _Deque_impl_data(); }
  434. #endif
  435. void
  436. _M_swap_data(_Deque_impl_data& __x) _GLIBCXX_NOEXCEPT
  437. {
  438. // Do not use std::swap(_M_start, __x._M_start), etc as it loses
  439. // information used by TBAA.
  440. std::swap(*this, __x);
  441. }
  442. };
  443. // This struct encapsulates the implementation of the std::deque
  444. // standard container and at the same time makes use of the EBO
  445. // for empty allocators.
  446. struct _Deque_impl
  447. : public _Tp_alloc_type, public _Deque_impl_data
  448. {
  449. _Deque_impl() _GLIBCXX_NOEXCEPT_IF(
  450. is_nothrow_default_constructible<_Tp_alloc_type>::value)
  451. : _Tp_alloc_type()
  452. { }
  453. _Deque_impl(const _Tp_alloc_type& __a) _GLIBCXX_NOEXCEPT
  454. : _Tp_alloc_type(__a)
  455. { }
  456. #if __cplusplus >= 201103L
  457. _Deque_impl(_Deque_impl&&) = default;
  458. _Deque_impl(_Tp_alloc_type&& __a) noexcept
  459. : _Tp_alloc_type(std::move(__a))
  460. { }
  461. _Deque_impl(_Deque_impl&& __d, _Tp_alloc_type&& __a)
  462. : _Tp_alloc_type(std::move(__a)), _Deque_impl_data(std::move(__d))
  463. { }
  464. #endif
  465. };
  466. _Tp_alloc_type&
  467. _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
  468. { return this->_M_impl; }
  469. const _Tp_alloc_type&
  470. _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
  471. { return this->_M_impl; }
  472. _Map_alloc_type
  473. _M_get_map_allocator() const _GLIBCXX_NOEXCEPT
  474. { return _Map_alloc_type(_M_get_Tp_allocator()); }
  475. _Ptr
  476. _M_allocate_node()
  477. {
  478. typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Traits;
  479. return _Traits::allocate(_M_impl, __deque_buf_size(sizeof(_Tp)));
  480. }
  481. void
  482. _M_deallocate_node(_Ptr __p) _GLIBCXX_NOEXCEPT
  483. {
  484. typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Traits;
  485. _Traits::deallocate(_M_impl, __p, __deque_buf_size(sizeof(_Tp)));
  486. }
  487. _Map_pointer
  488. _M_allocate_map(size_t __n)
  489. {
  490. _Map_alloc_type __map_alloc = _M_get_map_allocator();
  491. return _Map_alloc_traits::allocate(__map_alloc, __n);
  492. }
  493. void
  494. _M_deallocate_map(_Map_pointer __p, size_t __n) _GLIBCXX_NOEXCEPT
  495. {
  496. _Map_alloc_type __map_alloc = _M_get_map_allocator();
  497. _Map_alloc_traits::deallocate(__map_alloc, __p, __n);
  498. }
  499. void _M_initialize_map(size_t);
  500. void _M_create_nodes(_Map_pointer __nstart, _Map_pointer __nfinish);
  501. void _M_destroy_nodes(_Map_pointer __nstart,
  502. _Map_pointer __nfinish) _GLIBCXX_NOEXCEPT;
  503. enum { _S_initial_map_size = 8 };
  504. _Deque_impl _M_impl;
  505. };
  506. template<typename _Tp, typename _Alloc>
  507. _Deque_base<_Tp, _Alloc>::
  508. ~_Deque_base() _GLIBCXX_NOEXCEPT
  509. {
  510. if (this->_M_impl._M_map)
  511. {
  512. _M_destroy_nodes(this->_M_impl._M_start._M_node,
  513. this->_M_impl._M_finish._M_node + 1);
  514. _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
  515. }
  516. }
  517. /**
  518. * @brief Layout storage.
  519. * @param __num_elements The count of T's for which to allocate space
  520. * at first.
  521. * @return Nothing.
  522. *
  523. * The initial underlying memory layout is a bit complicated...
  524. */
  525. template<typename _Tp, typename _Alloc>
  526. void
  527. _Deque_base<_Tp, _Alloc>::
  528. _M_initialize_map(size_t __num_elements)
  529. {
  530. const size_t __num_nodes = (__num_elements / __deque_buf_size(sizeof(_Tp))
  531. + 1);
  532. this->_M_impl._M_map_size = std::max((size_t) _S_initial_map_size,
  533. size_t(__num_nodes + 2));
  534. this->_M_impl._M_map = _M_allocate_map(this->_M_impl._M_map_size);
  535. // For "small" maps (needing less than _M_map_size nodes), allocation
  536. // starts in the middle elements and grows outwards. So nstart may be
  537. // the beginning of _M_map, but for small maps it may be as far in as
  538. // _M_map+3.
  539. _Map_pointer __nstart = (this->_M_impl._M_map
  540. + (this->_M_impl._M_map_size - __num_nodes) / 2);
  541. _Map_pointer __nfinish = __nstart + __num_nodes;
  542. __try
  543. { _M_create_nodes(__nstart, __nfinish); }
  544. __catch(...)
  545. {
  546. _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
  547. this->_M_impl._M_map = _Map_pointer();
  548. this->_M_impl._M_map_size = 0;
  549. __throw_exception_again;
  550. }
  551. this->_M_impl._M_start._M_set_node(__nstart);
  552. this->_M_impl._M_finish._M_set_node(__nfinish - 1);
  553. this->_M_impl._M_start._M_cur = _M_impl._M_start._M_first;
  554. this->_M_impl._M_finish._M_cur = (this->_M_impl._M_finish._M_first
  555. + __num_elements
  556. % __deque_buf_size(sizeof(_Tp)));
  557. }
  558. template<typename _Tp, typename _Alloc>
  559. void
  560. _Deque_base<_Tp, _Alloc>::
  561. _M_create_nodes(_Map_pointer __nstart, _Map_pointer __nfinish)
  562. {
  563. _Map_pointer __cur;
  564. __try
  565. {
  566. for (__cur = __nstart; __cur < __nfinish; ++__cur)
  567. *__cur = this->_M_allocate_node();
  568. }
  569. __catch(...)
  570. {
  571. _M_destroy_nodes(__nstart, __cur);
  572. __throw_exception_again;
  573. }
  574. }
  575. template<typename _Tp, typename _Alloc>
  576. void
  577. _Deque_base<_Tp, _Alloc>::
  578. _M_destroy_nodes(_Map_pointer __nstart,
  579. _Map_pointer __nfinish) _GLIBCXX_NOEXCEPT
  580. {
  581. for (_Map_pointer __n = __nstart; __n < __nfinish; ++__n)
  582. _M_deallocate_node(*__n);
  583. }
  584. /**
  585. * @brief A standard container using fixed-size memory allocation and
  586. * constant-time manipulation of elements at either end.
  587. *
  588. * @ingroup sequences
  589. *
  590. * @tparam _Tp Type of element.
  591. * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
  592. *
  593. * Meets the requirements of a <a href="tables.html#65">container</a>, a
  594. * <a href="tables.html#66">reversible container</a>, and a
  595. * <a href="tables.html#67">sequence</a>, including the
  596. * <a href="tables.html#68">optional sequence requirements</a>.
  597. *
  598. * In previous HP/SGI versions of deque, there was an extra template
  599. * parameter so users could control the node size. This extension turned
  600. * out to violate the C++ standard (it can be detected using template
  601. * template parameters), and it was removed.
  602. *
  603. * Here's how a deque<Tp> manages memory. Each deque has 4 members:
  604. *
  605. * - Tp** _M_map
  606. * - size_t _M_map_size
  607. * - iterator _M_start, _M_finish
  608. *
  609. * map_size is at least 8. %map is an array of map_size
  610. * pointers-to-@a nodes. (The name %map has nothing to do with the
  611. * std::map class, and @b nodes should not be confused with
  612. * std::list's usage of @a node.)
  613. *
  614. * A @a node has no specific type name as such, but it is referred
  615. * to as @a node in this file. It is a simple array-of-Tp. If Tp
  616. * is very large, there will be one Tp element per node (i.e., an
  617. * @a array of one). For non-huge Tp's, node size is inversely
  618. * related to Tp size: the larger the Tp, the fewer Tp's will fit
  619. * in a node. The goal here is to keep the total size of a node
  620. * relatively small and constant over different Tp's, to improve
  621. * allocator efficiency.
  622. *
  623. * Not every pointer in the %map array will point to a node. If
  624. * the initial number of elements in the deque is small, the
  625. * /middle/ %map pointers will be valid, and the ones at the edges
  626. * will be unused. This same situation will arise as the %map
  627. * grows: available %map pointers, if any, will be on the ends. As
  628. * new nodes are created, only a subset of the %map's pointers need
  629. * to be copied @a outward.
  630. *
  631. * Class invariants:
  632. * - For any nonsingular iterator i:
  633. * - i.node points to a member of the %map array. (Yes, you read that
  634. * correctly: i.node does not actually point to a node.) The member of
  635. * the %map array is what actually points to the node.
  636. * - i.first == *(i.node) (This points to the node (first Tp element).)
  637. * - i.last == i.first + node_size
  638. * - i.cur is a pointer in the range [i.first, i.last). NOTE:
  639. * the implication of this is that i.cur is always a dereferenceable
  640. * pointer, even if i is a past-the-end iterator.
  641. * - Start and Finish are always nonsingular iterators. NOTE: this
  642. * means that an empty deque must have one node, a deque with <N
  643. * elements (where N is the node buffer size) must have one node, a
  644. * deque with N through (2N-1) elements must have two nodes, etc.
  645. * - For every node other than start.node and finish.node, every
  646. * element in the node is an initialized object. If start.node ==
  647. * finish.node, then [start.cur, finish.cur) are initialized
  648. * objects, and the elements outside that range are uninitialized
  649. * storage. Otherwise, [start.cur, start.last) and [finish.first,
  650. * finish.cur) are initialized objects, and [start.first, start.cur)
  651. * and [finish.cur, finish.last) are uninitialized storage.
  652. * - [%map, %map + map_size) is a valid, non-empty range.
  653. * - [start.node, finish.node] is a valid range contained within
  654. * [%map, %map + map_size).
  655. * - A pointer in the range [%map, %map + map_size) points to an allocated
  656. * node if and only if the pointer is in the range
  657. * [start.node, finish.node].
  658. *
  659. * Here's the magic: nothing in deque is @b aware of the discontiguous
  660. * storage!
  661. *
  662. * The memory setup and layout occurs in the parent, _Base, and the iterator
  663. * class is entirely responsible for @a leaping from one node to the next.
  664. * All the implementation routines for deque itself work only through the
  665. * start and finish iterators. This keeps the routines simple and sane,
  666. * and we can use other standard algorithms as well.
  667. */
  668. template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
  669. class deque : protected _Deque_base<_Tp, _Alloc>
  670. {
  671. #ifdef _GLIBCXX_CONCEPT_CHECKS
  672. // concept requirements
  673. typedef typename _Alloc::value_type _Alloc_value_type;
  674. # if __cplusplus < 201103L
  675. __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
  676. # endif
  677. __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
  678. #endif
  679. #if __cplusplus >= 201103L
  680. static_assert(is_same<typename remove_cv<_Tp>::type, _Tp>::value,
  681. "std::deque must have a non-const, non-volatile value_type");
  682. # if __cplusplus > 201703L || defined __STRICT_ANSI__
  683. static_assert(is_same<typename _Alloc::value_type, _Tp>::value,
  684. "std::deque must have the same value_type as its allocator");
  685. # endif
  686. #endif
  687. typedef _Deque_base<_Tp, _Alloc> _Base;
  688. typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
  689. typedef typename _Base::_Alloc_traits _Alloc_traits;
  690. typedef typename _Base::_Map_pointer _Map_pointer;
  691. public:
  692. typedef _Tp value_type;
  693. typedef typename _Alloc_traits::pointer pointer;
  694. typedef typename _Alloc_traits::const_pointer const_pointer;
  695. typedef typename _Alloc_traits::reference reference;
  696. typedef typename _Alloc_traits::const_reference const_reference;
  697. typedef typename _Base::iterator iterator;
  698. typedef typename _Base::const_iterator const_iterator;
  699. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  700. typedef std::reverse_iterator<iterator> reverse_iterator;
  701. typedef size_t size_type;
  702. typedef ptrdiff_t difference_type;
  703. typedef _Alloc allocator_type;
  704. private:
  705. static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT
  706. { return __deque_buf_size(sizeof(_Tp)); }
  707. // Functions controlling memory layout, and nothing else.
  708. using _Base::_M_initialize_map;
  709. using _Base::_M_create_nodes;
  710. using _Base::_M_destroy_nodes;
  711. using _Base::_M_allocate_node;
  712. using _Base::_M_deallocate_node;
  713. using _Base::_M_allocate_map;
  714. using _Base::_M_deallocate_map;
  715. using _Base::_M_get_Tp_allocator;
  716. /**
  717. * A total of four data members accumulated down the hierarchy.
  718. * May be accessed via _M_impl.*
  719. */
  720. using _Base::_M_impl;
  721. public:
  722. // [23.2.1.1] construct/copy/destroy
  723. // (assign() and get_allocator() are also listed in this section)
  724. /**
  725. * @brief Creates a %deque with no elements.
  726. */
  727. #if __cplusplus >= 201103L
  728. deque() = default;
  729. #else
  730. deque() { }
  731. #endif
  732. /**
  733. * @brief Creates a %deque with no elements.
  734. * @param __a An allocator object.
  735. */
  736. explicit
  737. deque(const allocator_type& __a)
  738. : _Base(__a, 0) { }
  739. #if __cplusplus >= 201103L
  740. /**
  741. * @brief Creates a %deque with default constructed elements.
  742. * @param __n The number of elements to initially create.
  743. * @param __a An allocator.
  744. *
  745. * This constructor fills the %deque with @a n default
  746. * constructed elements.
  747. */
  748. explicit
  749. deque(size_type __n, const allocator_type& __a = allocator_type())
  750. : _Base(__a, _S_check_init_len(__n, __a))
  751. { _M_default_initialize(); }
  752. /**
  753. * @brief Creates a %deque with copies of an exemplar element.
  754. * @param __n The number of elements to initially create.
  755. * @param __value An element to copy.
  756. * @param __a An allocator.
  757. *
  758. * This constructor fills the %deque with @a __n copies of @a __value.
  759. */
  760. deque(size_type __n, const value_type& __value,
  761. const allocator_type& __a = allocator_type())
  762. : _Base(__a, _S_check_init_len(__n, __a))
  763. { _M_fill_initialize(__value); }
  764. #else
  765. /**
  766. * @brief Creates a %deque with copies of an exemplar element.
  767. * @param __n The number of elements to initially create.
  768. * @param __value An element to copy.
  769. * @param __a An allocator.
  770. *
  771. * This constructor fills the %deque with @a __n copies of @a __value.
  772. */
  773. explicit
  774. deque(size_type __n, const value_type& __value = value_type(),
  775. const allocator_type& __a = allocator_type())
  776. : _Base(__a, _S_check_init_len(__n, __a))
  777. { _M_fill_initialize(__value); }
  778. #endif
  779. /**
  780. * @brief %Deque copy constructor.
  781. * @param __x A %deque of identical element and allocator types.
  782. *
  783. * The newly-created %deque uses a copy of the allocator object used
  784. * by @a __x (unless the allocator traits dictate a different object).
  785. */
  786. deque(const deque& __x)
  787. : _Base(_Alloc_traits::_S_select_on_copy(__x._M_get_Tp_allocator()),
  788. __x.size())
  789. { std::__uninitialized_copy_a(__x.begin(), __x.end(),
  790. this->_M_impl._M_start,
  791. _M_get_Tp_allocator()); }
  792. #if __cplusplus >= 201103L
  793. /**
  794. * @brief %Deque move constructor.
  795. *
  796. * The newly-created %deque contains the exact contents of the
  797. * moved instance.
  798. * The contents of the moved instance are a valid, but unspecified
  799. * %deque.
  800. */
  801. deque(deque&&) = default;
  802. /// Copy constructor with alternative allocator
  803. deque(const deque& __x, const allocator_type& __a)
  804. : _Base(__a, __x.size())
  805. { std::__uninitialized_copy_a(__x.begin(), __x.end(),
  806. this->_M_impl._M_start,
  807. _M_get_Tp_allocator()); }
  808. /// Move constructor with alternative allocator
  809. deque(deque&& __x, const allocator_type& __a)
  810. : deque(std::move(__x), __a, typename _Alloc_traits::is_always_equal{})
  811. { }
  812. private:
  813. deque(deque&& __x, const allocator_type& __a, true_type)
  814. : _Base(std::move(__x), __a)
  815. { }
  816. deque(deque&& __x, const allocator_type& __a, false_type)
  817. : _Base(std::move(__x), __a, __x.size())
  818. {
  819. if (__x.get_allocator() != __a && !__x.empty())
  820. {
  821. std::__uninitialized_move_a(__x.begin(), __x.end(),
  822. this->_M_impl._M_start,
  823. _M_get_Tp_allocator());
  824. __x.clear();
  825. }
  826. }
  827. public:
  828. /**
  829. * @brief Builds a %deque from an initializer list.
  830. * @param __l An initializer_list.
  831. * @param __a An allocator object.
  832. *
  833. * Create a %deque consisting of copies of the elements in the
  834. * initializer_list @a __l.
  835. *
  836. * This will call the element type's copy constructor N times
  837. * (where N is __l.size()) and do no memory reallocation.
  838. */
  839. deque(initializer_list<value_type> __l,
  840. const allocator_type& __a = allocator_type())
  841. : _Base(__a)
  842. {
  843. _M_range_initialize(__l.begin(), __l.end(),
  844. random_access_iterator_tag());
  845. }
  846. #endif
  847. /**
  848. * @brief Builds a %deque from a range.
  849. * @param __first An input iterator.
  850. * @param __last An input iterator.
  851. * @param __a An allocator object.
  852. *
  853. * Create a %deque consisting of copies of the elements from [__first,
  854. * __last).
  855. *
  856. * If the iterators are forward, bidirectional, or random-access, then
  857. * this will call the elements' copy constructor N times (where N is
  858. * distance(__first,__last)) and do no memory reallocation. But if only
  859. * input iterators are used, then this will do at most 2N calls to the
  860. * copy constructor, and logN memory reallocations.
  861. */
  862. #if __cplusplus >= 201103L
  863. template<typename _InputIterator,
  864. typename = std::_RequireInputIter<_InputIterator>>
  865. deque(_InputIterator __first, _InputIterator __last,
  866. const allocator_type& __a = allocator_type())
  867. : _Base(__a)
  868. {
  869. _M_range_initialize(__first, __last,
  870. std::__iterator_category(__first));
  871. }
  872. #else
  873. template<typename _InputIterator>
  874. deque(_InputIterator __first, _InputIterator __last,
  875. const allocator_type& __a = allocator_type())
  876. : _Base(__a)
  877. {
  878. // Check whether it's an integral type. If so, it's not an iterator.
  879. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  880. _M_initialize_dispatch(__first, __last, _Integral());
  881. }
  882. #endif
  883. /**
  884. * The dtor only erases the elements, and note that if the elements
  885. * themselves are pointers, the pointed-to memory is not touched in any
  886. * way. Managing the pointer is the user's responsibility.
  887. */
  888. ~deque()
  889. { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); }
  890. /**
  891. * @brief %Deque assignment operator.
  892. * @param __x A %deque of identical element and allocator types.
  893. *
  894. * All the elements of @a x are copied.
  895. *
  896. * The newly-created %deque uses a copy of the allocator object used
  897. * by @a __x (unless the allocator traits dictate a different object).
  898. */
  899. deque&
  900. operator=(const deque& __x);
  901. #if __cplusplus >= 201103L
  902. /**
  903. * @brief %Deque move assignment operator.
  904. * @param __x A %deque of identical element and allocator types.
  905. *
  906. * The contents of @a __x are moved into this deque (without copying,
  907. * if the allocators permit it).
  908. * @a __x is a valid, but unspecified %deque.
  909. */
  910. deque&
  911. operator=(deque&& __x) noexcept(_Alloc_traits::_S_always_equal())
  912. {
  913. using __always_equal = typename _Alloc_traits::is_always_equal;
  914. _M_move_assign1(std::move(__x), __always_equal{});
  915. return *this;
  916. }
  917. /**
  918. * @brief Assigns an initializer list to a %deque.
  919. * @param __l An initializer_list.
  920. *
  921. * This function fills a %deque with copies of the elements in the
  922. * initializer_list @a __l.
  923. *
  924. * Note that the assignment completely changes the %deque and that the
  925. * resulting %deque's size is the same as the number of elements
  926. * assigned.
  927. */
  928. deque&
  929. operator=(initializer_list<value_type> __l)
  930. {
  931. _M_assign_aux(__l.begin(), __l.end(),
  932. random_access_iterator_tag());
  933. return *this;
  934. }
  935. #endif
  936. /**
  937. * @brief Assigns a given value to a %deque.
  938. * @param __n Number of elements to be assigned.
  939. * @param __val Value to be assigned.
  940. *
  941. * This function fills a %deque with @a n copies of the given
  942. * value. Note that the assignment completely changes the
  943. * %deque and that the resulting %deque's size is the same as
  944. * the number of elements assigned.
  945. */
  946. void
  947. assign(size_type __n, const value_type& __val)
  948. { _M_fill_assign(__n, __val); }
  949. /**
  950. * @brief Assigns a range to a %deque.
  951. * @param __first An input iterator.
  952. * @param __last An input iterator.
  953. *
  954. * This function fills a %deque with copies of the elements in the
  955. * range [__first,__last).
  956. *
  957. * Note that the assignment completely changes the %deque and that the
  958. * resulting %deque's size is the same as the number of elements
  959. * assigned.
  960. */
  961. #if __cplusplus >= 201103L
  962. template<typename _InputIterator,
  963. typename = std::_RequireInputIter<_InputIterator>>
  964. void
  965. assign(_InputIterator __first, _InputIterator __last)
  966. { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
  967. #else
  968. template<typename _InputIterator>
  969. void
  970. assign(_InputIterator __first, _InputIterator __last)
  971. {
  972. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  973. _M_assign_dispatch(__first, __last, _Integral());
  974. }
  975. #endif
  976. #if __cplusplus >= 201103L
  977. /**
  978. * @brief Assigns an initializer list to a %deque.
  979. * @param __l An initializer_list.
  980. *
  981. * This function fills a %deque with copies of the elements in the
  982. * initializer_list @a __l.
  983. *
  984. * Note that the assignment completely changes the %deque and that the
  985. * resulting %deque's size is the same as the number of elements
  986. * assigned.
  987. */
  988. void
  989. assign(initializer_list<value_type> __l)
  990. { _M_assign_aux(__l.begin(), __l.end(), random_access_iterator_tag()); }
  991. #endif
  992. /// Get a copy of the memory allocation object.
  993. allocator_type
  994. get_allocator() const _GLIBCXX_NOEXCEPT
  995. { return _Base::get_allocator(); }
  996. // iterators
  997. /**
  998. * Returns a read/write iterator that points to the first element in the
  999. * %deque. Iteration is done in ordinary element order.
  1000. */
  1001. iterator
  1002. begin() _GLIBCXX_NOEXCEPT
  1003. { return this->_M_impl._M_start; }
  1004. /**
  1005. * Returns a read-only (constant) iterator that points to the first
  1006. * element in the %deque. Iteration is done in ordinary element order.
  1007. */
  1008. const_iterator
  1009. begin() const _GLIBCXX_NOEXCEPT
  1010. { return this->_M_impl._M_start; }
  1011. /**
  1012. * Returns a read/write iterator that points one past the last
  1013. * element in the %deque. Iteration is done in ordinary
  1014. * element order.
  1015. */
  1016. iterator
  1017. end() _GLIBCXX_NOEXCEPT
  1018. { return this->_M_impl._M_finish; }
  1019. /**
  1020. * Returns a read-only (constant) iterator that points one past
  1021. * the last element in the %deque. Iteration is done in
  1022. * ordinary element order.
  1023. */
  1024. const_iterator
  1025. end() const _GLIBCXX_NOEXCEPT
  1026. { return this->_M_impl._M_finish; }
  1027. /**
  1028. * Returns a read/write reverse iterator that points to the
  1029. * last element in the %deque. Iteration is done in reverse
  1030. * element order.
  1031. */
  1032. reverse_iterator
  1033. rbegin() _GLIBCXX_NOEXCEPT
  1034. { return reverse_iterator(this->_M_impl._M_finish); }
  1035. /**
  1036. * Returns a read-only (constant) reverse iterator that points
  1037. * to the last element in the %deque. Iteration is done in
  1038. * reverse element order.
  1039. */
  1040. const_reverse_iterator
  1041. rbegin() const _GLIBCXX_NOEXCEPT
  1042. { return const_reverse_iterator(this->_M_impl._M_finish); }
  1043. /**
  1044. * Returns a read/write reverse iterator that points to one
  1045. * before the first element in the %deque. Iteration is done
  1046. * in reverse element order.
  1047. */
  1048. reverse_iterator
  1049. rend() _GLIBCXX_NOEXCEPT
  1050. { return reverse_iterator(this->_M_impl._M_start); }
  1051. /**
  1052. * Returns a read-only (constant) reverse iterator that points
  1053. * to one before the first element in the %deque. Iteration is
  1054. * done in reverse element order.
  1055. */
  1056. const_reverse_iterator
  1057. rend() const _GLIBCXX_NOEXCEPT
  1058. { return const_reverse_iterator(this->_M_impl._M_start); }
  1059. #if __cplusplus >= 201103L
  1060. /**
  1061. * Returns a read-only (constant) iterator that points to the first
  1062. * element in the %deque. Iteration is done in ordinary element order.
  1063. */
  1064. const_iterator
  1065. cbegin() const noexcept
  1066. { return this->_M_impl._M_start; }
  1067. /**
  1068. * Returns a read-only (constant) iterator that points one past
  1069. * the last element in the %deque. Iteration is done in
  1070. * ordinary element order.
  1071. */
  1072. const_iterator
  1073. cend() const noexcept
  1074. { return this->_M_impl._M_finish; }
  1075. /**
  1076. * Returns a read-only (constant) reverse iterator that points
  1077. * to the last element in the %deque. Iteration is done in
  1078. * reverse element order.
  1079. */
  1080. const_reverse_iterator
  1081. crbegin() const noexcept
  1082. { return const_reverse_iterator(this->_M_impl._M_finish); }
  1083. /**
  1084. * Returns a read-only (constant) reverse iterator that points
  1085. * to one before the first element in the %deque. Iteration is
  1086. * done in reverse element order.
  1087. */
  1088. const_reverse_iterator
  1089. crend() const noexcept
  1090. { return const_reverse_iterator(this->_M_impl._M_start); }
  1091. #endif
  1092. // [23.2.1.2] capacity
  1093. /** Returns the number of elements in the %deque. */
  1094. size_type
  1095. size() const _GLIBCXX_NOEXCEPT
  1096. { return this->_M_impl._M_finish - this->_M_impl._M_start; }
  1097. /** Returns the size() of the largest possible %deque. */
  1098. size_type
  1099. max_size() const _GLIBCXX_NOEXCEPT
  1100. { return _S_max_size(_M_get_Tp_allocator()); }
  1101. #if __cplusplus >= 201103L
  1102. /**
  1103. * @brief Resizes the %deque to the specified number of elements.
  1104. * @param __new_size Number of elements the %deque should contain.
  1105. *
  1106. * This function will %resize the %deque to the specified
  1107. * number of elements. If the number is smaller than the
  1108. * %deque's current size the %deque is truncated, otherwise
  1109. * default constructed elements are appended.
  1110. */
  1111. void
  1112. resize(size_type __new_size)
  1113. {
  1114. const size_type __len = size();
  1115. if (__new_size > __len)
  1116. _M_default_append(__new_size - __len);
  1117. else if (__new_size < __len)
  1118. _M_erase_at_end(this->_M_impl._M_start
  1119. + difference_type(__new_size));
  1120. }
  1121. /**
  1122. * @brief Resizes the %deque to the specified number of elements.
  1123. * @param __new_size Number of elements the %deque should contain.
  1124. * @param __x Data with which new elements should be populated.
  1125. *
  1126. * This function will %resize the %deque to the specified
  1127. * number of elements. If the number is smaller than the
  1128. * %deque's current size the %deque is truncated, otherwise the
  1129. * %deque is extended and new elements are populated with given
  1130. * data.
  1131. */
  1132. void
  1133. resize(size_type __new_size, const value_type& __x)
  1134. #else
  1135. /**
  1136. * @brief Resizes the %deque to the specified number of elements.
  1137. * @param __new_size Number of elements the %deque should contain.
  1138. * @param __x Data with which new elements should be populated.
  1139. *
  1140. * This function will %resize the %deque to the specified
  1141. * number of elements. If the number is smaller than the
  1142. * %deque's current size the %deque is truncated, otherwise the
  1143. * %deque is extended and new elements are populated with given
  1144. * data.
  1145. */
  1146. void
  1147. resize(size_type __new_size, value_type __x = value_type())
  1148. #endif
  1149. {
  1150. const size_type __len = size();
  1151. if (__new_size > __len)
  1152. _M_fill_insert(this->_M_impl._M_finish, __new_size - __len, __x);
  1153. else if (__new_size < __len)
  1154. _M_erase_at_end(this->_M_impl._M_start
  1155. + difference_type(__new_size));
  1156. }
  1157. #if __cplusplus >= 201103L
  1158. /** A non-binding request to reduce memory use. */
  1159. void
  1160. shrink_to_fit() noexcept
  1161. { _M_shrink_to_fit(); }
  1162. #endif
  1163. /**
  1164. * Returns true if the %deque is empty. (Thus begin() would
  1165. * equal end().)
  1166. */
  1167. _GLIBCXX_NODISCARD bool
  1168. empty() const _GLIBCXX_NOEXCEPT
  1169. { return this->_M_impl._M_finish == this->_M_impl._M_start; }
  1170. // element access
  1171. /**
  1172. * @brief Subscript access to the data contained in the %deque.
  1173. * @param __n The index of the element for which data should be
  1174. * accessed.
  1175. * @return Read/write reference to data.
  1176. *
  1177. * This operator allows for easy, array-style, data access.
  1178. * Note that data access with this operator is unchecked and
  1179. * out_of_range lookups are not defined. (For checked lookups
  1180. * see at().)
  1181. */
  1182. reference
  1183. operator[](size_type __n) _GLIBCXX_NOEXCEPT
  1184. {
  1185. __glibcxx_requires_subscript(__n);
  1186. return this->_M_impl._M_start[difference_type(__n)];
  1187. }
  1188. /**
  1189. * @brief Subscript access to the data contained in the %deque.
  1190. * @param __n The index of the element for which data should be
  1191. * accessed.
  1192. * @return Read-only (constant) reference to data.
  1193. *
  1194. * This operator allows for easy, array-style, data access.
  1195. * Note that data access with this operator is unchecked and
  1196. * out_of_range lookups are not defined. (For checked lookups
  1197. * see at().)
  1198. */
  1199. const_reference
  1200. operator[](size_type __n) const _GLIBCXX_NOEXCEPT
  1201. {
  1202. __glibcxx_requires_subscript(__n);
  1203. return this->_M_impl._M_start[difference_type(__n)];
  1204. }
  1205. protected:
  1206. /// Safety check used only from at().
  1207. void
  1208. _M_range_check(size_type __n) const
  1209. {
  1210. if (__n >= this->size())
  1211. __throw_out_of_range_fmt(__N("deque::_M_range_check: __n "
  1212. "(which is %zu)>= this->size() "
  1213. "(which is %zu)"),
  1214. __n, this->size());
  1215. }
  1216. public:
  1217. /**
  1218. * @brief Provides access to the data contained in the %deque.
  1219. * @param __n The index of the element for which data should be
  1220. * accessed.
  1221. * @return Read/write reference to data.
  1222. * @throw std::out_of_range If @a __n is an invalid index.
  1223. *
  1224. * This function provides for safer data access. The parameter
  1225. * is first checked that it is in the range of the deque. The
  1226. * function throws out_of_range if the check fails.
  1227. */
  1228. reference
  1229. at(size_type __n)
  1230. {
  1231. _M_range_check(__n);
  1232. return (*this)[__n];
  1233. }
  1234. /**
  1235. * @brief Provides access to the data contained in the %deque.
  1236. * @param __n The index of the element for which data should be
  1237. * accessed.
  1238. * @return Read-only (constant) reference to data.
  1239. * @throw std::out_of_range If @a __n is an invalid index.
  1240. *
  1241. * This function provides for safer data access. The parameter is first
  1242. * checked that it is in the range of the deque. The function throws
  1243. * out_of_range if the check fails.
  1244. */
  1245. const_reference
  1246. at(size_type __n) const
  1247. {
  1248. _M_range_check(__n);
  1249. return (*this)[__n];
  1250. }
  1251. /**
  1252. * Returns a read/write reference to the data at the first
  1253. * element of the %deque.
  1254. */
  1255. reference
  1256. front() _GLIBCXX_NOEXCEPT
  1257. {
  1258. __glibcxx_requires_nonempty();
  1259. return *begin();
  1260. }
  1261. /**
  1262. * Returns a read-only (constant) reference to the data at the first
  1263. * element of the %deque.
  1264. */
  1265. const_reference
  1266. front() const _GLIBCXX_NOEXCEPT
  1267. {
  1268. __glibcxx_requires_nonempty();
  1269. return *begin();
  1270. }
  1271. /**
  1272. * Returns a read/write reference to the data at the last element of the
  1273. * %deque.
  1274. */
  1275. reference
  1276. back() _GLIBCXX_NOEXCEPT
  1277. {
  1278. __glibcxx_requires_nonempty();
  1279. iterator __tmp = end();
  1280. --__tmp;
  1281. return *__tmp;
  1282. }
  1283. /**
  1284. * Returns a read-only (constant) reference to the data at the last
  1285. * element of the %deque.
  1286. */
  1287. const_reference
  1288. back() const _GLIBCXX_NOEXCEPT
  1289. {
  1290. __glibcxx_requires_nonempty();
  1291. const_iterator __tmp = end();
  1292. --__tmp;
  1293. return *__tmp;
  1294. }
  1295. // [23.2.1.2] modifiers
  1296. /**
  1297. * @brief Add data to the front of the %deque.
  1298. * @param __x Data to be added.
  1299. *
  1300. * This is a typical stack operation. The function creates an
  1301. * element at the front of the %deque and assigns the given
  1302. * data to it. Due to the nature of a %deque this operation
  1303. * can be done in constant time.
  1304. */
  1305. void
  1306. push_front(const value_type& __x)
  1307. {
  1308. if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
  1309. {
  1310. _Alloc_traits::construct(this->_M_impl,
  1311. this->_M_impl._M_start._M_cur - 1,
  1312. __x);
  1313. --this->_M_impl._M_start._M_cur;
  1314. }
  1315. else
  1316. _M_push_front_aux(__x);
  1317. }
  1318. #if __cplusplus >= 201103L
  1319. void
  1320. push_front(value_type&& __x)
  1321. { emplace_front(std::move(__x)); }
  1322. template<typename... _Args>
  1323. #if __cplusplus > 201402L
  1324. reference
  1325. #else
  1326. void
  1327. #endif
  1328. emplace_front(_Args&&... __args);
  1329. #endif
  1330. /**
  1331. * @brief Add data to the end of the %deque.
  1332. * @param __x Data to be added.
  1333. *
  1334. * This is a typical stack operation. The function creates an
  1335. * element at the end of the %deque and assigns the given data
  1336. * to it. Due to the nature of a %deque this operation can be
  1337. * done in constant time.
  1338. */
  1339. void
  1340. push_back(const value_type& __x)
  1341. {
  1342. if (this->_M_impl._M_finish._M_cur
  1343. != this->_M_impl._M_finish._M_last - 1)
  1344. {
  1345. _Alloc_traits::construct(this->_M_impl,
  1346. this->_M_impl._M_finish._M_cur, __x);
  1347. ++this->_M_impl._M_finish._M_cur;
  1348. }
  1349. else
  1350. _M_push_back_aux(__x);
  1351. }
  1352. #if __cplusplus >= 201103L
  1353. void
  1354. push_back(value_type&& __x)
  1355. { emplace_back(std::move(__x)); }
  1356. template<typename... _Args>
  1357. #if __cplusplus > 201402L
  1358. reference
  1359. #else
  1360. void
  1361. #endif
  1362. emplace_back(_Args&&... __args);
  1363. #endif
  1364. /**
  1365. * @brief Removes first element.
  1366. *
  1367. * This is a typical stack operation. It shrinks the %deque by one.
  1368. *
  1369. * Note that no data is returned, and if the first element's data is
  1370. * needed, it should be retrieved before pop_front() is called.
  1371. */
  1372. void
  1373. pop_front() _GLIBCXX_NOEXCEPT
  1374. {
  1375. __glibcxx_requires_nonempty();
  1376. if (this->_M_impl._M_start._M_cur
  1377. != this->_M_impl._M_start._M_last - 1)
  1378. {
  1379. _Alloc_traits::destroy(_M_get_Tp_allocator(),
  1380. this->_M_impl._M_start._M_cur);
  1381. ++this->_M_impl._M_start._M_cur;
  1382. }
  1383. else
  1384. _M_pop_front_aux();
  1385. }
  1386. /**
  1387. * @brief Removes last element.
  1388. *
  1389. * This is a typical stack operation. It shrinks the %deque by one.
  1390. *
  1391. * Note that no data is returned, and if the last element's data is
  1392. * needed, it should be retrieved before pop_back() is called.
  1393. */
  1394. void
  1395. pop_back() _GLIBCXX_NOEXCEPT
  1396. {
  1397. __glibcxx_requires_nonempty();
  1398. if (this->_M_impl._M_finish._M_cur
  1399. != this->_M_impl._M_finish._M_first)
  1400. {
  1401. --this->_M_impl._M_finish._M_cur;
  1402. _Alloc_traits::destroy(_M_get_Tp_allocator(),
  1403. this->_M_impl._M_finish._M_cur);
  1404. }
  1405. else
  1406. _M_pop_back_aux();
  1407. }
  1408. #if __cplusplus >= 201103L
  1409. /**
  1410. * @brief Inserts an object in %deque before specified iterator.
  1411. * @param __position A const_iterator into the %deque.
  1412. * @param __args Arguments.
  1413. * @return An iterator that points to the inserted data.
  1414. *
  1415. * This function will insert an object of type T constructed
  1416. * with T(std::forward<Args>(args)...) before the specified location.
  1417. */
  1418. template<typename... _Args>
  1419. iterator
  1420. emplace(const_iterator __position, _Args&&... __args);
  1421. /**
  1422. * @brief Inserts given value into %deque before specified iterator.
  1423. * @param __position A const_iterator into the %deque.
  1424. * @param __x Data to be inserted.
  1425. * @return An iterator that points to the inserted data.
  1426. *
  1427. * This function will insert a copy of the given value before the
  1428. * specified location.
  1429. */
  1430. iterator
  1431. insert(const_iterator __position, const value_type& __x);
  1432. #else
  1433. /**
  1434. * @brief Inserts given value into %deque before specified iterator.
  1435. * @param __position An iterator into the %deque.
  1436. * @param __x Data to be inserted.
  1437. * @return An iterator that points to the inserted data.
  1438. *
  1439. * This function will insert a copy of the given value before the
  1440. * specified location.
  1441. */
  1442. iterator
  1443. insert(iterator __position, const value_type& __x);
  1444. #endif
  1445. #if __cplusplus >= 201103L
  1446. /**
  1447. * @brief Inserts given rvalue into %deque before specified iterator.
  1448. * @param __position A const_iterator into the %deque.
  1449. * @param __x Data to be inserted.
  1450. * @return An iterator that points to the inserted data.
  1451. *
  1452. * This function will insert a copy of the given rvalue before the
  1453. * specified location.
  1454. */
  1455. iterator
  1456. insert(const_iterator __position, value_type&& __x)
  1457. { return emplace(__position, std::move(__x)); }
  1458. /**
  1459. * @brief Inserts an initializer list into the %deque.
  1460. * @param __p An iterator into the %deque.
  1461. * @param __l An initializer_list.
  1462. * @return An iterator that points to the inserted data.
  1463. *
  1464. * This function will insert copies of the data in the
  1465. * initializer_list @a __l into the %deque before the location
  1466. * specified by @a __p. This is known as <em>list insert</em>.
  1467. */
  1468. iterator
  1469. insert(const_iterator __p, initializer_list<value_type> __l)
  1470. {
  1471. auto __offset = __p - cbegin();
  1472. _M_range_insert_aux(__p._M_const_cast(), __l.begin(), __l.end(),
  1473. std::random_access_iterator_tag());
  1474. return begin() + __offset;
  1475. }
  1476. /**
  1477. * @brief Inserts a number of copies of given data into the %deque.
  1478. * @param __position A const_iterator into the %deque.
  1479. * @param __n Number of elements to be inserted.
  1480. * @param __x Data to be inserted.
  1481. * @return An iterator that points to the inserted data.
  1482. *
  1483. * This function will insert a specified number of copies of the given
  1484. * data before the location specified by @a __position.
  1485. */
  1486. iterator
  1487. insert(const_iterator __position, size_type __n, const value_type& __x)
  1488. {
  1489. difference_type __offset = __position - cbegin();
  1490. _M_fill_insert(__position._M_const_cast(), __n, __x);
  1491. return begin() + __offset;
  1492. }
  1493. #else
  1494. /**
  1495. * @brief Inserts a number of copies of given data into the %deque.
  1496. * @param __position An iterator into the %deque.
  1497. * @param __n Number of elements to be inserted.
  1498. * @param __x Data to be inserted.
  1499. *
  1500. * This function will insert a specified number of copies of the given
  1501. * data before the location specified by @a __position.
  1502. */
  1503. void
  1504. insert(iterator __position, size_type __n, const value_type& __x)
  1505. { _M_fill_insert(__position, __n, __x); }
  1506. #endif
  1507. #if __cplusplus >= 201103L
  1508. /**
  1509. * @brief Inserts a range into the %deque.
  1510. * @param __position A const_iterator into the %deque.
  1511. * @param __first An input iterator.
  1512. * @param __last An input iterator.
  1513. * @return An iterator that points to the inserted data.
  1514. *
  1515. * This function will insert copies of the data in the range
  1516. * [__first,__last) into the %deque before the location specified
  1517. * by @a __position. This is known as <em>range insert</em>.
  1518. */
  1519. template<typename _InputIterator,
  1520. typename = std::_RequireInputIter<_InputIterator>>
  1521. iterator
  1522. insert(const_iterator __position, _InputIterator __first,
  1523. _InputIterator __last)
  1524. {
  1525. difference_type __offset = __position - cbegin();
  1526. _M_range_insert_aux(__position._M_const_cast(), __first, __last,
  1527. std::__iterator_category(__first));
  1528. return begin() + __offset;
  1529. }
  1530. #else
  1531. /**
  1532. * @brief Inserts a range into the %deque.
  1533. * @param __position An iterator into the %deque.
  1534. * @param __first An input iterator.
  1535. * @param __last An input iterator.
  1536. *
  1537. * This function will insert copies of the data in the range
  1538. * [__first,__last) into the %deque before the location specified
  1539. * by @a __position. This is known as <em>range insert</em>.
  1540. */
  1541. template<typename _InputIterator>
  1542. void
  1543. insert(iterator __position, _InputIterator __first,
  1544. _InputIterator __last)
  1545. {
  1546. // Check whether it's an integral type. If so, it's not an iterator.
  1547. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  1548. _M_insert_dispatch(__position, __first, __last, _Integral());
  1549. }
  1550. #endif
  1551. /**
  1552. * @brief Remove element at given position.
  1553. * @param __position Iterator pointing to element to be erased.
  1554. * @return An iterator pointing to the next element (or end()).
  1555. *
  1556. * This function will erase the element at the given position and thus
  1557. * shorten the %deque by one.
  1558. *
  1559. * The user is cautioned that
  1560. * this function only erases the element, and that if the element is
  1561. * itself a pointer, the pointed-to memory is not touched in any way.
  1562. * Managing the pointer is the user's responsibility.
  1563. */
  1564. iterator
  1565. #if __cplusplus >= 201103L
  1566. erase(const_iterator __position)
  1567. #else
  1568. erase(iterator __position)
  1569. #endif
  1570. { return _M_erase(__position._M_const_cast()); }
  1571. /**
  1572. * @brief Remove a range of elements.
  1573. * @param __first Iterator pointing to the first element to be erased.
  1574. * @param __last Iterator pointing to one past the last element to be
  1575. * erased.
  1576. * @return An iterator pointing to the element pointed to by @a last
  1577. * prior to erasing (or end()).
  1578. *
  1579. * This function will erase the elements in the range
  1580. * [__first,__last) and shorten the %deque accordingly.
  1581. *
  1582. * The user is cautioned that
  1583. * this function only erases the elements, and that if the elements
  1584. * themselves are pointers, the pointed-to memory is not touched in any
  1585. * way. Managing the pointer is the user's responsibility.
  1586. */
  1587. iterator
  1588. #if __cplusplus >= 201103L
  1589. erase(const_iterator __first, const_iterator __last)
  1590. #else
  1591. erase(iterator __first, iterator __last)
  1592. #endif
  1593. { return _M_erase(__first._M_const_cast(), __last._M_const_cast()); }
  1594. /**
  1595. * @brief Swaps data with another %deque.
  1596. * @param __x A %deque of the same element and allocator types.
  1597. *
  1598. * This exchanges the elements between two deques in constant time.
  1599. * (Four pointers, so it should be quite fast.)
  1600. * Note that the global std::swap() function is specialized such that
  1601. * std::swap(d1,d2) will feed to this function.
  1602. *
  1603. * Whether the allocators are swapped depends on the allocator traits.
  1604. */
  1605. void
  1606. swap(deque& __x) _GLIBCXX_NOEXCEPT
  1607. {
  1608. #if __cplusplus >= 201103L
  1609. __glibcxx_assert(_Alloc_traits::propagate_on_container_swap::value
  1610. || _M_get_Tp_allocator() == __x._M_get_Tp_allocator());
  1611. #endif
  1612. _M_impl._M_swap_data(__x._M_impl);
  1613. _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
  1614. __x._M_get_Tp_allocator());
  1615. }
  1616. /**
  1617. * Erases all the elements. Note that this function only erases the
  1618. * elements, and that if the elements themselves are pointers, the
  1619. * pointed-to memory is not touched in any way. Managing the pointer is
  1620. * the user's responsibility.
  1621. */
  1622. void
  1623. clear() _GLIBCXX_NOEXCEPT
  1624. { _M_erase_at_end(begin()); }
  1625. protected:
  1626. // Internal constructor functions follow.
  1627. #if __cplusplus < 201103L
  1628. // called by the range constructor to implement [23.1.1]/9
  1629. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1630. // 438. Ambiguity in the "do the right thing" clause
  1631. template<typename _Integer>
  1632. void
  1633. _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
  1634. {
  1635. _M_initialize_map(_S_check_init_len(static_cast<size_type>(__n),
  1636. _M_get_Tp_allocator()));
  1637. _M_fill_initialize(__x);
  1638. }
  1639. // called by the range constructor to implement [23.1.1]/9
  1640. template<typename _InputIterator>
  1641. void
  1642. _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
  1643. __false_type)
  1644. {
  1645. _M_range_initialize(__first, __last,
  1646. std::__iterator_category(__first));
  1647. }
  1648. #endif
  1649. static size_t
  1650. _S_check_init_len(size_t __n, const allocator_type& __a)
  1651. {
  1652. if (__n > _S_max_size(__a))
  1653. __throw_length_error(
  1654. __N("cannot create std::deque larger than max_size()"));
  1655. return __n;
  1656. }
  1657. static size_type
  1658. _S_max_size(const _Tp_alloc_type& __a) _GLIBCXX_NOEXCEPT
  1659. {
  1660. const size_t __diffmax = __gnu_cxx::__numeric_traits<ptrdiff_t>::__max;
  1661. const size_t __allocmax = _Alloc_traits::max_size(__a);
  1662. return (std::min)(__diffmax, __allocmax);
  1663. }
  1664. // called by the second initialize_dispatch above
  1665. //@{
  1666. /**
  1667. * @brief Fills the deque with whatever is in [first,last).
  1668. * @param __first An input iterator.
  1669. * @param __last An input iterator.
  1670. * @return Nothing.
  1671. *
  1672. * If the iterators are actually forward iterators (or better), then the
  1673. * memory layout can be done all at once. Else we move forward using
  1674. * push_back on each value from the iterator.
  1675. */
  1676. template<typename _InputIterator>
  1677. void
  1678. _M_range_initialize(_InputIterator __first, _InputIterator __last,
  1679. std::input_iterator_tag);
  1680. // called by the second initialize_dispatch above
  1681. template<typename _ForwardIterator>
  1682. void
  1683. _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
  1684. std::forward_iterator_tag);
  1685. //@}
  1686. /**
  1687. * @brief Fills the %deque with copies of value.
  1688. * @param __value Initial value.
  1689. * @return Nothing.
  1690. * @pre _M_start and _M_finish have already been initialized,
  1691. * but none of the %deque's elements have yet been constructed.
  1692. *
  1693. * This function is called only when the user provides an explicit size
  1694. * (with or without an explicit exemplar value).
  1695. */
  1696. void
  1697. _M_fill_initialize(const value_type& __value);
  1698. #if __cplusplus >= 201103L
  1699. // called by deque(n).
  1700. void
  1701. _M_default_initialize();
  1702. #endif
  1703. // Internal assign functions follow. The *_aux functions do the actual
  1704. // assignment work for the range versions.
  1705. #if __cplusplus < 201103L
  1706. // called by the range assign to implement [23.1.1]/9
  1707. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1708. // 438. Ambiguity in the "do the right thing" clause
  1709. template<typename _Integer>
  1710. void
  1711. _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
  1712. { _M_fill_assign(__n, __val); }
  1713. // called by the range assign to implement [23.1.1]/9
  1714. template<typename _InputIterator>
  1715. void
  1716. _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
  1717. __false_type)
  1718. { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
  1719. #endif
  1720. // called by the second assign_dispatch above
  1721. template<typename _InputIterator>
  1722. void
  1723. _M_assign_aux(_InputIterator __first, _InputIterator __last,
  1724. std::input_iterator_tag);
  1725. // called by the second assign_dispatch above
  1726. template<typename _ForwardIterator>
  1727. void
  1728. _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
  1729. std::forward_iterator_tag)
  1730. {
  1731. const size_type __len = std::distance(__first, __last);
  1732. if (__len > size())
  1733. {
  1734. _ForwardIterator __mid = __first;
  1735. std::advance(__mid, size());
  1736. std::copy(__first, __mid, begin());
  1737. _M_range_insert_aux(end(), __mid, __last,
  1738. std::__iterator_category(__first));
  1739. }
  1740. else
  1741. _M_erase_at_end(std::copy(__first, __last, begin()));
  1742. }
  1743. // Called by assign(n,t), and the range assign when it turns out
  1744. // to be the same thing.
  1745. void
  1746. _M_fill_assign(size_type __n, const value_type& __val)
  1747. {
  1748. if (__n > size())
  1749. {
  1750. std::fill(begin(), end(), __val);
  1751. _M_fill_insert(end(), __n - size(), __val);
  1752. }
  1753. else
  1754. {
  1755. _M_erase_at_end(begin() + difference_type(__n));
  1756. std::fill(begin(), end(), __val);
  1757. }
  1758. }
  1759. //@{
  1760. /// Helper functions for push_* and pop_*.
  1761. #if __cplusplus < 201103L
  1762. void _M_push_back_aux(const value_type&);
  1763. void _M_push_front_aux(const value_type&);
  1764. #else
  1765. template<typename... _Args>
  1766. void _M_push_back_aux(_Args&&... __args);
  1767. template<typename... _Args>
  1768. void _M_push_front_aux(_Args&&... __args);
  1769. #endif
  1770. void _M_pop_back_aux();
  1771. void _M_pop_front_aux();
  1772. //@}
  1773. // Internal insert functions follow. The *_aux functions do the actual
  1774. // insertion work when all shortcuts fail.
  1775. #if __cplusplus < 201103L
  1776. // called by the range insert to implement [23.1.1]/9
  1777. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1778. // 438. Ambiguity in the "do the right thing" clause
  1779. template<typename _Integer>
  1780. void
  1781. _M_insert_dispatch(iterator __pos,
  1782. _Integer __n, _Integer __x, __true_type)
  1783. { _M_fill_insert(__pos, __n, __x); }
  1784. // called by the range insert to implement [23.1.1]/9
  1785. template<typename _InputIterator>
  1786. void
  1787. _M_insert_dispatch(iterator __pos,
  1788. _InputIterator __first, _InputIterator __last,
  1789. __false_type)
  1790. {
  1791. _M_range_insert_aux(__pos, __first, __last,
  1792. std::__iterator_category(__first));
  1793. }
  1794. #endif
  1795. // called by the second insert_dispatch above
  1796. template<typename _InputIterator>
  1797. void
  1798. _M_range_insert_aux(iterator __pos, _InputIterator __first,
  1799. _InputIterator __last, std::input_iterator_tag);
  1800. // called by the second insert_dispatch above
  1801. template<typename _ForwardIterator>
  1802. void
  1803. _M_range_insert_aux(iterator __pos, _ForwardIterator __first,
  1804. _ForwardIterator __last, std::forward_iterator_tag);
  1805. // Called by insert(p,n,x), and the range insert when it turns out to be
  1806. // the same thing. Can use fill functions in optimal situations,
  1807. // otherwise passes off to insert_aux(p,n,x).
  1808. void
  1809. _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
  1810. // called by insert(p,x)
  1811. #if __cplusplus < 201103L
  1812. iterator
  1813. _M_insert_aux(iterator __pos, const value_type& __x);
  1814. #else
  1815. template<typename... _Args>
  1816. iterator
  1817. _M_insert_aux(iterator __pos, _Args&&... __args);
  1818. #endif
  1819. // called by insert(p,n,x) via fill_insert
  1820. void
  1821. _M_insert_aux(iterator __pos, size_type __n, const value_type& __x);
  1822. // called by range_insert_aux for forward iterators
  1823. template<typename _ForwardIterator>
  1824. void
  1825. _M_insert_aux(iterator __pos,
  1826. _ForwardIterator __first, _ForwardIterator __last,
  1827. size_type __n);
  1828. // Internal erase functions follow.
  1829. void
  1830. _M_destroy_data_aux(iterator __first, iterator __last);
  1831. // Called by ~deque().
  1832. // NB: Doesn't deallocate the nodes.
  1833. template<typename _Alloc1>
  1834. void
  1835. _M_destroy_data(iterator __first, iterator __last, const _Alloc1&)
  1836. { _M_destroy_data_aux(__first, __last); }
  1837. void
  1838. _M_destroy_data(iterator __first, iterator __last,
  1839. const std::allocator<_Tp>&)
  1840. {
  1841. if (!__has_trivial_destructor(value_type))
  1842. _M_destroy_data_aux(__first, __last);
  1843. }
  1844. // Called by erase(q1, q2).
  1845. void
  1846. _M_erase_at_begin(iterator __pos)
  1847. {
  1848. _M_destroy_data(begin(), __pos, _M_get_Tp_allocator());
  1849. _M_destroy_nodes(this->_M_impl._M_start._M_node, __pos._M_node);
  1850. this->_M_impl._M_start = __pos;
  1851. }
  1852. // Called by erase(q1, q2), resize(), clear(), _M_assign_aux,
  1853. // _M_fill_assign, operator=.
  1854. void
  1855. _M_erase_at_end(iterator __pos)
  1856. {
  1857. _M_destroy_data(__pos, end(), _M_get_Tp_allocator());
  1858. _M_destroy_nodes(__pos._M_node + 1,
  1859. this->_M_impl._M_finish._M_node + 1);
  1860. this->_M_impl._M_finish = __pos;
  1861. }
  1862. iterator
  1863. _M_erase(iterator __pos);
  1864. iterator
  1865. _M_erase(iterator __first, iterator __last);
  1866. #if __cplusplus >= 201103L
  1867. // Called by resize(sz).
  1868. void
  1869. _M_default_append(size_type __n);
  1870. bool
  1871. _M_shrink_to_fit();
  1872. #endif
  1873. //@{
  1874. /// Memory-handling helpers for the previous internal insert functions.
  1875. iterator
  1876. _M_reserve_elements_at_front(size_type __n)
  1877. {
  1878. const size_type __vacancies = this->_M_impl._M_start._M_cur
  1879. - this->_M_impl._M_start._M_first;
  1880. if (__n > __vacancies)
  1881. _M_new_elements_at_front(__n - __vacancies);
  1882. return this->_M_impl._M_start - difference_type(__n);
  1883. }
  1884. iterator
  1885. _M_reserve_elements_at_back(size_type __n)
  1886. {
  1887. const size_type __vacancies = (this->_M_impl._M_finish._M_last
  1888. - this->_M_impl._M_finish._M_cur) - 1;
  1889. if (__n > __vacancies)
  1890. _M_new_elements_at_back(__n - __vacancies);
  1891. return this->_M_impl._M_finish + difference_type(__n);
  1892. }
  1893. void
  1894. _M_new_elements_at_front(size_type __new_elements);
  1895. void
  1896. _M_new_elements_at_back(size_type __new_elements);
  1897. //@}
  1898. //@{
  1899. /**
  1900. * @brief Memory-handling helpers for the major %map.
  1901. *
  1902. * Makes sure the _M_map has space for new nodes. Does not
  1903. * actually add the nodes. Can invalidate _M_map pointers.
  1904. * (And consequently, %deque iterators.)
  1905. */
  1906. void
  1907. _M_reserve_map_at_back(size_type __nodes_to_add = 1)
  1908. {
  1909. if (__nodes_to_add + 1 > this->_M_impl._M_map_size
  1910. - (this->_M_impl._M_finish._M_node - this->_M_impl._M_map))
  1911. _M_reallocate_map(__nodes_to_add, false);
  1912. }
  1913. void
  1914. _M_reserve_map_at_front(size_type __nodes_to_add = 1)
  1915. {
  1916. if (__nodes_to_add > size_type(this->_M_impl._M_start._M_node
  1917. - this->_M_impl._M_map))
  1918. _M_reallocate_map(__nodes_to_add, true);
  1919. }
  1920. void
  1921. _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front);
  1922. //@}
  1923. #if __cplusplus >= 201103L
  1924. // Constant-time, nothrow move assignment when source object's memory
  1925. // can be moved because the allocators are equal.
  1926. void
  1927. _M_move_assign1(deque&& __x, /* always equal: */ true_type) noexcept
  1928. {
  1929. this->_M_impl._M_swap_data(__x._M_impl);
  1930. __x.clear();
  1931. std::__alloc_on_move(_M_get_Tp_allocator(), __x._M_get_Tp_allocator());
  1932. }
  1933. // When the allocators are not equal the operation could throw, because
  1934. // we might need to allocate a new map for __x after moving from it
  1935. // or we might need to allocate new elements for *this.
  1936. void
  1937. _M_move_assign1(deque&& __x, /* always equal: */ false_type)
  1938. {
  1939. constexpr bool __move_storage =
  1940. _Alloc_traits::_S_propagate_on_move_assign();
  1941. _M_move_assign2(std::move(__x), __bool_constant<__move_storage>());
  1942. }
  1943. // Destroy all elements and deallocate all memory, then replace
  1944. // with elements created from __args.
  1945. template<typename... _Args>
  1946. void
  1947. _M_replace_map(_Args&&... __args)
  1948. {
  1949. // Create new data first, so if allocation fails there are no effects.
  1950. deque __newobj(std::forward<_Args>(__args)...);
  1951. // Free existing storage using existing allocator.
  1952. clear();
  1953. _M_deallocate_node(*begin()._M_node); // one node left after clear()
  1954. _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
  1955. this->_M_impl._M_map = nullptr;
  1956. this->_M_impl._M_map_size = 0;
  1957. // Take ownership of replacement memory.
  1958. this->_M_impl._M_swap_data(__newobj._M_impl);
  1959. }
  1960. // Do move assignment when the allocator propagates.
  1961. void
  1962. _M_move_assign2(deque&& __x, /* propagate: */ true_type)
  1963. {
  1964. // Make a copy of the original allocator state.
  1965. auto __alloc = __x._M_get_Tp_allocator();
  1966. // The allocator propagates so storage can be moved from __x,
  1967. // leaving __x in a valid empty state with a moved-from allocator.
  1968. _M_replace_map(std::move(__x));
  1969. // Move the corresponding allocator state too.
  1970. _M_get_Tp_allocator() = std::move(__alloc);
  1971. }
  1972. // Do move assignment when it may not be possible to move source
  1973. // object's memory, resulting in a linear-time operation.
  1974. void
  1975. _M_move_assign2(deque&& __x, /* propagate: */ false_type)
  1976. {
  1977. if (__x._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
  1978. {
  1979. // The allocators are equal so storage can be moved from __x,
  1980. // leaving __x in a valid empty state with its current allocator.
  1981. _M_replace_map(std::move(__x), __x.get_allocator());
  1982. }
  1983. else
  1984. {
  1985. // The rvalue's allocator cannot be moved and is not equal,
  1986. // so we need to individually move each element.
  1987. _M_assign_aux(std::make_move_iterator(__x.begin()),
  1988. std::make_move_iterator(__x.end()),
  1989. std::random_access_iterator_tag());
  1990. __x.clear();
  1991. }
  1992. }
  1993. #endif
  1994. };
  1995. #if __cpp_deduction_guides >= 201606
  1996. template<typename _InputIterator, typename _ValT
  1997. = typename iterator_traits<_InputIterator>::value_type,
  1998. typename _Allocator = allocator<_ValT>,
  1999. typename = _RequireInputIter<_InputIterator>,
  2000. typename = _RequireAllocator<_Allocator>>
  2001. deque(_InputIterator, _InputIterator, _Allocator = _Allocator())
  2002. -> deque<_ValT, _Allocator>;
  2003. #endif
  2004. /**
  2005. * @brief Deque equality comparison.
  2006. * @param __x A %deque.
  2007. * @param __y A %deque of the same type as @a __x.
  2008. * @return True iff the size and elements of the deques are equal.
  2009. *
  2010. * This is an equivalence relation. It is linear in the size of the
  2011. * deques. Deques are considered equivalent if their sizes are equal,
  2012. * and if corresponding elements compare equal.
  2013. */
  2014. template<typename _Tp, typename _Alloc>
  2015. inline bool
  2016. operator==(const deque<_Tp, _Alloc>& __x, const deque<_Tp, _Alloc>& __y)
  2017. { return __x.size() == __y.size()
  2018. && std::equal(__x.begin(), __x.end(), __y.begin()); }
  2019. #if __cpp_lib_three_way_comparison
  2020. /**
  2021. * @brief Deque ordering relation.
  2022. * @param __x A `deque`.
  2023. * @param __y A `deque` of the same type as `__x`.
  2024. * @return A value indicating whether `__x` is less than, equal to,
  2025. * greater than, or incomparable with `__y`.
  2026. *
  2027. * See `std::lexicographical_compare_three_way()` for how the determination
  2028. * is made. This operator is used to synthesize relational operators like
  2029. * `<` and `>=` etc.
  2030. */
  2031. template<typename _Tp, typename _Alloc>
  2032. inline __detail::__synth3way_t<_Tp>
  2033. operator<=>(const deque<_Tp, _Alloc>& __x, const deque<_Tp, _Alloc>& __y)
  2034. {
  2035. return std::lexicographical_compare_three_way(__x.begin(), __x.end(),
  2036. __y.begin(), __y.end(),
  2037. __detail::__synth3way);
  2038. }
  2039. #else
  2040. /**
  2041. * @brief Deque ordering relation.
  2042. * @param __x A %deque.
  2043. * @param __y A %deque of the same type as @a __x.
  2044. * @return True iff @a x is lexicographically less than @a __y.
  2045. *
  2046. * This is a total ordering relation. It is linear in the size of the
  2047. * deques. The elements must be comparable with @c <.
  2048. *
  2049. * See std::lexicographical_compare() for how the determination is made.
  2050. */
  2051. template<typename _Tp, typename _Alloc>
  2052. inline bool
  2053. operator<(const deque<_Tp, _Alloc>& __x, const deque<_Tp, _Alloc>& __y)
  2054. { return std::lexicographical_compare(__x.begin(), __x.end(),
  2055. __y.begin(), __y.end()); }
  2056. /// Based on operator==
  2057. template<typename _Tp, typename _Alloc>
  2058. inline bool
  2059. operator!=(const deque<_Tp, _Alloc>& __x, const deque<_Tp, _Alloc>& __y)
  2060. { return !(__x == __y); }
  2061. /// Based on operator<
  2062. template<typename _Tp, typename _Alloc>
  2063. inline bool
  2064. operator>(const deque<_Tp, _Alloc>& __x, const deque<_Tp, _Alloc>& __y)
  2065. { return __y < __x; }
  2066. /// Based on operator<
  2067. template<typename _Tp, typename _Alloc>
  2068. inline bool
  2069. operator<=(const deque<_Tp, _Alloc>& __x, const deque<_Tp, _Alloc>& __y)
  2070. { return !(__y < __x); }
  2071. /// Based on operator<
  2072. template<typename _Tp, typename _Alloc>
  2073. inline bool
  2074. operator>=(const deque<_Tp, _Alloc>& __x, const deque<_Tp, _Alloc>& __y)
  2075. { return !(__x < __y); }
  2076. #endif // three-way comparison
  2077. /// See std::deque::swap().
  2078. template<typename _Tp, typename _Alloc>
  2079. inline void
  2080. swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y)
  2081. _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
  2082. { __x.swap(__y); }
  2083. #undef _GLIBCXX_DEQUE_BUF_SIZE
  2084. _GLIBCXX_END_NAMESPACE_CONTAINER
  2085. #if __cplusplus >= 201103L
  2086. // std::allocator is safe, but it is not the only allocator
  2087. // for which this is valid.
  2088. template<class _Tp>
  2089. struct __is_bitwise_relocatable<_GLIBCXX_STD_C::deque<_Tp>>
  2090. : true_type { };
  2091. #endif
  2092. _GLIBCXX_END_NAMESPACE_VERSION
  2093. } // namespace std
  2094. #endif /* _STL_DEQUE_H */