Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
Louis Dionne | 9bd9388 | 2021-11-17 16:25:01 -0500 | [diff] [blame] | 2 | //===----------------------------------------------------------------------===// |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_DEQUE |
| 11 | #define _LIBCPP_DEQUE |
| 12 | |
| 13 | /* |
| 14 | deque synopsis |
| 15 | |
| 16 | namespace std |
| 17 | { |
| 18 | |
| 19 | template <class T, class Allocator = allocator<T> > |
| 20 | class deque |
| 21 | { |
| 22 | public: |
| 23 | // types: |
| 24 | typedef T value_type; |
| 25 | typedef Allocator allocator_type; |
| 26 | |
| 27 | typedef typename allocator_type::reference reference; |
| 28 | typedef typename allocator_type::const_reference const_reference; |
| 29 | typedef implementation-defined iterator; |
| 30 | typedef implementation-defined const_iterator; |
| 31 | typedef typename allocator_type::size_type size_type; |
| 32 | typedef typename allocator_type::difference_type difference_type; |
| 33 | |
| 34 | typedef typename allocator_type::pointer pointer; |
| 35 | typedef typename allocator_type::const_pointer const_pointer; |
| 36 | typedef std::reverse_iterator<iterator> reverse_iterator; |
| 37 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
| 38 | |
| 39 | // construct/copy/destroy: |
Howard Hinnant | ad979ba | 2011-06-03 15:16:49 +0000 | [diff] [blame] | 40 | deque() noexcept(is_nothrow_default_constructible<allocator_type>::value); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 41 | explicit deque(const allocator_type& a); |
| 42 | explicit deque(size_type n); |
Marshall Clow | 6809869 | 2013-09-09 18:19:45 +0000 | [diff] [blame] | 43 | explicit deque(size_type n, const allocator_type& a); // C++14 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 44 | deque(size_type n, const value_type& v); |
| 45 | deque(size_type n, const value_type& v, const allocator_type& a); |
| 46 | template <class InputIterator> |
| 47 | deque(InputIterator f, InputIterator l); |
| 48 | template <class InputIterator> |
| 49 | deque(InputIterator f, InputIterator l, const allocator_type& a); |
| 50 | deque(const deque& c); |
Howard Hinnant | 4931e09 | 2011-06-02 21:38:57 +0000 | [diff] [blame] | 51 | deque(deque&& c) |
| 52 | noexcept(is_nothrow_move_constructible<allocator_type>::value); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 53 | deque(initializer_list<value_type> il, const Allocator& a = allocator_type()); |
| 54 | deque(const deque& c, const allocator_type& a); |
| 55 | deque(deque&& c, const allocator_type& a); |
| 56 | ~deque(); |
| 57 | |
| 58 | deque& operator=(const deque& c); |
Howard Hinnant | 4931e09 | 2011-06-02 21:38:57 +0000 | [diff] [blame] | 59 | deque& operator=(deque&& c) |
| 60 | noexcept( |
| 61 | allocator_type::propagate_on_container_move_assignment::value && |
| 62 | is_nothrow_move_assignable<allocator_type>::value); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 63 | deque& operator=(initializer_list<value_type> il); |
| 64 | |
| 65 | template <class InputIterator> |
| 66 | void assign(InputIterator f, InputIterator l); |
| 67 | void assign(size_type n, const value_type& v); |
| 68 | void assign(initializer_list<value_type> il); |
| 69 | |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 70 | allocator_type get_allocator() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 71 | |
| 72 | // iterators: |
| 73 | |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 74 | iterator begin() noexcept; |
| 75 | const_iterator begin() const noexcept; |
| 76 | iterator end() noexcept; |
| 77 | const_iterator end() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 78 | |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 79 | reverse_iterator rbegin() noexcept; |
| 80 | const_reverse_iterator rbegin() const noexcept; |
| 81 | reverse_iterator rend() noexcept; |
| 82 | const_reverse_iterator rend() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 83 | |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 84 | const_iterator cbegin() const noexcept; |
| 85 | const_iterator cend() const noexcept; |
| 86 | const_reverse_iterator crbegin() const noexcept; |
| 87 | const_reverse_iterator crend() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 88 | |
| 89 | // capacity: |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 90 | size_type size() const noexcept; |
| 91 | size_type max_size() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 92 | void resize(size_type n); |
| 93 | void resize(size_type n, const value_type& v); |
| 94 | void shrink_to_fit(); |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 95 | bool empty() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 96 | |
| 97 | // element access: |
| 98 | reference operator[](size_type i); |
| 99 | const_reference operator[](size_type i) const; |
| 100 | reference at(size_type i); |
| 101 | const_reference at(size_type i) const; |
| 102 | reference front(); |
| 103 | const_reference front() const; |
| 104 | reference back(); |
| 105 | const_reference back() const; |
| 106 | |
| 107 | // modifiers: |
| 108 | void push_front(const value_type& v); |
| 109 | void push_front(value_type&& v); |
| 110 | void push_back(const value_type& v); |
| 111 | void push_back(value_type&& v); |
Marshall Clow | ea52cc4 | 2017-01-24 23:09:12 +0000 | [diff] [blame] | 112 | template <class... Args> reference emplace_front(Args&&... args); // reference in C++17 |
| 113 | template <class... Args> reference emplace_back(Args&&... args); // reference in C++17 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 114 | template <class... Args> iterator emplace(const_iterator p, Args&&... args); |
| 115 | iterator insert(const_iterator p, const value_type& v); |
| 116 | iterator insert(const_iterator p, value_type&& v); |
| 117 | iterator insert(const_iterator p, size_type n, const value_type& v); |
| 118 | template <class InputIterator> |
Marshall Clow | 6b612cf | 2015-01-22 18:33:29 +0000 | [diff] [blame] | 119 | iterator insert(const_iterator p, InputIterator f, InputIterator l); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 120 | iterator insert(const_iterator p, initializer_list<value_type> il); |
| 121 | void pop_front(); |
| 122 | void pop_back(); |
| 123 | iterator erase(const_iterator p); |
| 124 | iterator erase(const_iterator f, const_iterator l); |
Howard Hinnant | 4931e09 | 2011-06-02 21:38:57 +0000 | [diff] [blame] | 125 | void swap(deque& c) |
Marshall Clow | 8982dcd | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 126 | noexcept(allocator_traits<allocator_type>::is_always_equal::value); // C++17 |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 127 | void clear() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
Marshall Clow | 4cc3a45 | 2018-05-18 23:44:13 +0000 | [diff] [blame] | 130 | template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>> |
| 131 | deque(InputIterator, InputIterator, Allocator = Allocator()) |
Konstantin Varlamov | 53b543c | 2021-11-09 09:21:02 -0800 | [diff] [blame] | 132 | -> deque<typename iterator_traits<InputIterator>::value_type, Allocator>; // C++17 |
Marshall Clow | 4cc3a45 | 2018-05-18 23:44:13 +0000 | [diff] [blame] | 133 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 134 | template <class T, class Allocator> |
| 135 | bool operator==(const deque<T,Allocator>& x, const deque<T,Allocator>& y); |
| 136 | template <class T, class Allocator> |
| 137 | bool operator< (const deque<T,Allocator>& x, const deque<T,Allocator>& y); |
| 138 | template <class T, class Allocator> |
| 139 | bool operator!=(const deque<T,Allocator>& x, const deque<T,Allocator>& y); |
| 140 | template <class T, class Allocator> |
| 141 | bool operator> (const deque<T,Allocator>& x, const deque<T,Allocator>& y); |
| 142 | template <class T, class Allocator> |
| 143 | bool operator>=(const deque<T,Allocator>& x, const deque<T,Allocator>& y); |
| 144 | template <class T, class Allocator> |
| 145 | bool operator<=(const deque<T,Allocator>& x, const deque<T,Allocator>& y); |
| 146 | |
| 147 | // specialized algorithms: |
| 148 | template <class T, class Allocator> |
Howard Hinnant | 287548a | 2011-06-03 17:30:28 +0000 | [diff] [blame] | 149 | void swap(deque<T,Allocator>& x, deque<T,Allocator>& y) |
| 150 | noexcept(noexcept(x.swap(y))); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 151 | |
Marshall Clow | 29b53f2 | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 152 | template <class T, class Allocator, class U> |
Marek Kurdej | a98b141 | 2020-05-02 13:58:03 +0200 | [diff] [blame] | 153 | typename deque<T, Allocator>::size_type |
| 154 | erase(deque<T, Allocator>& c, const U& value); // C++20 |
Marshall Clow | 29b53f2 | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 155 | template <class T, class Allocator, class Predicate> |
Marek Kurdej | a98b141 | 2020-05-02 13:58:03 +0200 | [diff] [blame] | 156 | typename deque<T, Allocator>::size_type |
| 157 | erase_if(deque<T, Allocator>& c, Predicate pred); // C++20 |
Marshall Clow | 29b53f2 | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 158 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 159 | } // std |
| 160 | |
| 161 | */ |
| 162 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 163 | #include <__config> |
Arthur O'Dwyer | 597cac4 | 2021-05-12 23:04:03 -0400 | [diff] [blame] | 164 | #include <__debug> |
Konstantin Varlamov | 53b543c | 2021-11-09 09:21:02 -0800 | [diff] [blame] | 165 | #include <__iterator/iterator_traits.h> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 166 | #include <__split_buffer> |
Christopher Di Bella | 41f26e8 | 2021-06-05 02:47:47 +0000 | [diff] [blame] | 167 | #include <__utility/forward.h> |
Arthur O'Dwyer | 597cac4 | 2021-05-12 23:04:03 -0400 | [diff] [blame] | 168 | #include <algorithm> |
Arthur O'Dwyer | 7deec12 | 2021-03-24 18:19:12 -0400 | [diff] [blame] | 169 | #include <compare> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 170 | #include <initializer_list> |
| 171 | #include <iterator> |
Christopher Di Bella | 599a6c6 | 2021-06-09 23:10:17 +0000 | [diff] [blame] | 172 | #include <limits> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 173 | #include <stdexcept> |
Arthur O'Dwyer | 597cac4 | 2021-05-12 23:04:03 -0400 | [diff] [blame] | 174 | #include <type_traits> |
Marshall Clow | 0a1e750 | 2018-09-12 19:41:40 +0000 | [diff] [blame] | 175 | #include <version> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 176 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 177 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 178 | #pragma GCC system_header |
| 179 | #endif |
| 180 | |
| 181 | _LIBCPP_PUSH_MACROS |
| 182 | #include <__undef_macros> |
| 183 | |
Howard Hinnant | c5a5fbd | 2011-11-29 16:45:27 +0000 | [diff] [blame] | 184 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 185 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 186 | |
| 187 | template <class _Tp, class _Allocator> class __deque_base; |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 188 | template <class _Tp, class _Allocator = allocator<_Tp> > class _LIBCPP_TEMPLATE_VIS deque; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 189 | |
| 190 | template <class _ValueType, class _Pointer, class _Reference, class _MapPointer, |
| 191 | class _DiffType, _DiffType _BlockSize> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 192 | class _LIBCPP_TEMPLATE_VIS __deque_iterator; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 193 | |
| 194 | template <class _RAIter, |
| 195 | class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2> |
| 196 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> |
| 197 | copy(_RAIter __f, |
| 198 | _RAIter __l, |
| 199 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 200 | typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 201 | |
| 202 | template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1, |
| 203 | class _OutputIterator> |
| 204 | _OutputIterator |
| 205 | copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f, |
| 206 | __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l, |
| 207 | _OutputIterator __r); |
| 208 | |
| 209 | template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1, |
| 210 | class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2> |
| 211 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> |
| 212 | copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f, |
| 213 | __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l, |
| 214 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r); |
| 215 | |
| 216 | template <class _RAIter, |
| 217 | class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2> |
| 218 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> |
| 219 | copy_backward(_RAIter __f, |
| 220 | _RAIter __l, |
| 221 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 222 | typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 223 | |
| 224 | template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1, |
| 225 | class _OutputIterator> |
| 226 | _OutputIterator |
| 227 | copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f, |
| 228 | __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l, |
| 229 | _OutputIterator __r); |
| 230 | |
| 231 | template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1, |
| 232 | class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2> |
| 233 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> |
| 234 | copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f, |
| 235 | __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l, |
| 236 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r); |
| 237 | |
| 238 | template <class _RAIter, |
| 239 | class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2> |
| 240 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> |
| 241 | move(_RAIter __f, |
| 242 | _RAIter __l, |
| 243 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 244 | typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 245 | |
| 246 | template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1, |
| 247 | class _OutputIterator> |
| 248 | _OutputIterator |
| 249 | move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f, |
| 250 | __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l, |
| 251 | _OutputIterator __r); |
| 252 | |
| 253 | template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1, |
| 254 | class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2> |
| 255 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> |
| 256 | move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f, |
| 257 | __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l, |
| 258 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r); |
| 259 | |
| 260 | template <class _RAIter, |
| 261 | class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2> |
| 262 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> |
| 263 | move_backward(_RAIter __f, |
| 264 | _RAIter __l, |
| 265 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 266 | typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 267 | |
| 268 | template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1, |
| 269 | class _OutputIterator> |
| 270 | _OutputIterator |
| 271 | move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f, |
| 272 | __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l, |
| 273 | _OutputIterator __r); |
| 274 | |
| 275 | template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1, |
| 276 | class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2> |
| 277 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> |
| 278 | move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f, |
| 279 | __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l, |
| 280 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r); |
| 281 | |
Evgeniy Stepanov | c299de2 | 2015-11-06 22:02:29 +0000 | [diff] [blame] | 282 | template <class _ValueType, class _DiffType> |
| 283 | struct __deque_block_size { |
| 284 | static const _DiffType value = sizeof(_ValueType) < 256 ? 4096 / sizeof(_ValueType) : 16; |
| 285 | }; |
| 286 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 287 | template <class _ValueType, class _Pointer, class _Reference, class _MapPointer, |
Evgeniy Stepanov | c299de2 | 2015-11-06 22:02:29 +0000 | [diff] [blame] | 288 | class _DiffType, _DiffType _BS = |
| 289 | #ifdef _LIBCPP_ABI_INCOMPLETE_TYPES_IN_DEQUE |
| 290 | // Keep template parameter to avoid changing all template declarations thoughout |
| 291 | // this file. |
| 292 | 0 |
| 293 | #else |
| 294 | __deque_block_size<_ValueType, _DiffType>::value |
| 295 | #endif |
| 296 | > |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 297 | class _LIBCPP_TEMPLATE_VIS __deque_iterator |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 298 | { |
| 299 | typedef _MapPointer __map_iterator; |
| 300 | public: |
| 301 | typedef _Pointer pointer; |
| 302 | typedef _DiffType difference_type; |
| 303 | private: |
| 304 | __map_iterator __m_iter_; |
| 305 | pointer __ptr_; |
| 306 | |
Evgeniy Stepanov | c299de2 | 2015-11-06 22:02:29 +0000 | [diff] [blame] | 307 | static const difference_type __block_size; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 308 | public: |
| 309 | typedef _ValueType value_type; |
| 310 | typedef random_access_iterator_tag iterator_category; |
| 311 | typedef _Reference reference; |
| 312 | |
Marshall Clow | 7a3a61d | 2013-08-06 16:14:36 +0000 | [diff] [blame] | 313 | _LIBCPP_INLINE_VISIBILITY __deque_iterator() _NOEXCEPT |
| 314 | #if _LIBCPP_STD_VER > 11 |
| 315 | : __m_iter_(nullptr), __ptr_(nullptr) |
| 316 | #endif |
| 317 | {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 318 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 319 | template <class _Pp, class _Rp, class _MP> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 320 | _LIBCPP_INLINE_VISIBILITY |
Evgeniy Stepanov | c299de2 | 2015-11-06 22:02:29 +0000 | [diff] [blame] | 321 | __deque_iterator(const __deque_iterator<value_type, _Pp, _Rp, _MP, difference_type, _BS>& __it, |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 322 | typename enable_if<is_convertible<_Pp, pointer>::value>::type* = 0) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 323 | : __m_iter_(__it.__m_iter_), __ptr_(__it.__ptr_) {} |
| 324 | |
| 325 | _LIBCPP_INLINE_VISIBILITY reference operator*() const {return *__ptr_;} |
| 326 | _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return __ptr_;} |
| 327 | |
| 328 | _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator++() |
| 329 | { |
| 330 | if (++__ptr_ - *__m_iter_ == __block_size) |
| 331 | { |
| 332 | ++__m_iter_; |
| 333 | __ptr_ = *__m_iter_; |
| 334 | } |
| 335 | return *this; |
| 336 | } |
| 337 | |
| 338 | _LIBCPP_INLINE_VISIBILITY __deque_iterator operator++(int) |
| 339 | { |
| 340 | __deque_iterator __tmp = *this; |
| 341 | ++(*this); |
| 342 | return __tmp; |
| 343 | } |
| 344 | |
| 345 | _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator--() |
| 346 | { |
| 347 | if (__ptr_ == *__m_iter_) |
| 348 | { |
| 349 | --__m_iter_; |
| 350 | __ptr_ = *__m_iter_ + __block_size; |
| 351 | } |
| 352 | --__ptr_; |
| 353 | return *this; |
| 354 | } |
| 355 | |
| 356 | _LIBCPP_INLINE_VISIBILITY __deque_iterator operator--(int) |
| 357 | { |
| 358 | __deque_iterator __tmp = *this; |
| 359 | --(*this); |
| 360 | return __tmp; |
| 361 | } |
| 362 | |
| 363 | _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator+=(difference_type __n) |
| 364 | { |
| 365 | if (__n != 0) |
| 366 | { |
| 367 | __n += __ptr_ - *__m_iter_; |
| 368 | if (__n > 0) |
| 369 | { |
| 370 | __m_iter_ += __n / __block_size; |
| 371 | __ptr_ = *__m_iter_ + __n % __block_size; |
| 372 | } |
| 373 | else // (__n < 0) |
| 374 | { |
| 375 | difference_type __z = __block_size - 1 - __n; |
| 376 | __m_iter_ -= __z / __block_size; |
| 377 | __ptr_ = *__m_iter_ + (__block_size - 1 - __z % __block_size); |
| 378 | } |
| 379 | } |
| 380 | return *this; |
| 381 | } |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 382 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 383 | _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator-=(difference_type __n) |
| 384 | { |
| 385 | return *this += -__n; |
| 386 | } |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 387 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 388 | _LIBCPP_INLINE_VISIBILITY __deque_iterator operator+(difference_type __n) const |
| 389 | { |
| 390 | __deque_iterator __t(*this); |
| 391 | __t += __n; |
| 392 | return __t; |
| 393 | } |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 394 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 395 | _LIBCPP_INLINE_VISIBILITY __deque_iterator operator-(difference_type __n) const |
| 396 | { |
| 397 | __deque_iterator __t(*this); |
| 398 | __t -= __n; |
| 399 | return __t; |
| 400 | } |
| 401 | |
| 402 | _LIBCPP_INLINE_VISIBILITY |
| 403 | friend __deque_iterator operator+(difference_type __n, const __deque_iterator& __it) |
| 404 | {return __it + __n;} |
| 405 | |
| 406 | _LIBCPP_INLINE_VISIBILITY |
| 407 | friend difference_type operator-(const __deque_iterator& __x, const __deque_iterator& __y) |
| 408 | { |
| 409 | if (__x != __y) |
| 410 | return (__x.__m_iter_ - __y.__m_iter_) * __block_size |
| 411 | + (__x.__ptr_ - *__x.__m_iter_) |
| 412 | - (__y.__ptr_ - *__y.__m_iter_); |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const |
| 417 | {return *(*this + __n);} |
| 418 | |
| 419 | _LIBCPP_INLINE_VISIBILITY friend |
| 420 | bool operator==(const __deque_iterator& __x, const __deque_iterator& __y) |
| 421 | {return __x.__ptr_ == __y.__ptr_;} |
| 422 | |
| 423 | _LIBCPP_INLINE_VISIBILITY friend |
| 424 | bool operator!=(const __deque_iterator& __x, const __deque_iterator& __y) |
| 425 | {return !(__x == __y);} |
| 426 | |
| 427 | _LIBCPP_INLINE_VISIBILITY friend |
| 428 | bool operator<(const __deque_iterator& __x, const __deque_iterator& __y) |
| 429 | {return __x.__m_iter_ < __y.__m_iter_ || |
| 430 | (__x.__m_iter_ == __y.__m_iter_ && __x.__ptr_ < __y.__ptr_);} |
| 431 | |
| 432 | _LIBCPP_INLINE_VISIBILITY friend |
| 433 | bool operator>(const __deque_iterator& __x, const __deque_iterator& __y) |
| 434 | {return __y < __x;} |
| 435 | |
| 436 | _LIBCPP_INLINE_VISIBILITY friend |
| 437 | bool operator<=(const __deque_iterator& __x, const __deque_iterator& __y) |
| 438 | {return !(__y < __x);} |
| 439 | |
| 440 | _LIBCPP_INLINE_VISIBILITY friend |
| 441 | bool operator>=(const __deque_iterator& __x, const __deque_iterator& __y) |
| 442 | {return !(__x < __y);} |
| 443 | |
| 444 | private: |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 445 | _LIBCPP_INLINE_VISIBILITY __deque_iterator(__map_iterator __m, pointer __p) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 446 | : __m_iter_(__m), __ptr_(__p) {} |
| 447 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 448 | template <class _Tp, class _Ap> friend class __deque_base; |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 449 | template <class _Tp, class _Ap> friend class _LIBCPP_TEMPLATE_VIS deque; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 450 | template <class _Vp, class _Pp, class _Rp, class _MP, class _Dp, _Dp> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 451 | friend class _LIBCPP_TEMPLATE_VIS __deque_iterator; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 452 | |
| 453 | template <class _RAIter, |
| 454 | class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2> |
| 455 | friend |
| 456 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> |
| 457 | copy(_RAIter __f, |
| 458 | _RAIter __l, |
| 459 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 460 | typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 461 | |
| 462 | template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1, |
| 463 | class _OutputIterator> |
| 464 | friend |
| 465 | _OutputIterator |
| 466 | copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f, |
| 467 | __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l, |
| 468 | _OutputIterator __r); |
| 469 | |
| 470 | template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1, |
| 471 | class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2> |
| 472 | friend |
| 473 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> |
| 474 | copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f, |
| 475 | __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l, |
| 476 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r); |
| 477 | |
| 478 | template <class _RAIter, |
| 479 | class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2> |
| 480 | friend |
| 481 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> |
| 482 | copy_backward(_RAIter __f, |
| 483 | _RAIter __l, |
| 484 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 485 | typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 486 | |
| 487 | template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1, |
| 488 | class _OutputIterator> |
| 489 | friend |
| 490 | _OutputIterator |
| 491 | copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f, |
| 492 | __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l, |
| 493 | _OutputIterator __r); |
| 494 | |
| 495 | template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1, |
| 496 | class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2> |
| 497 | friend |
| 498 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> |
| 499 | copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f, |
| 500 | __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l, |
| 501 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r); |
| 502 | |
| 503 | template <class _RAIter, |
| 504 | class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2> |
| 505 | friend |
| 506 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> |
| 507 | move(_RAIter __f, |
| 508 | _RAIter __l, |
| 509 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 510 | typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 511 | |
| 512 | template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1, |
| 513 | class _OutputIterator> |
| 514 | friend |
| 515 | _OutputIterator |
| 516 | move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f, |
| 517 | __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l, |
| 518 | _OutputIterator __r); |
| 519 | |
| 520 | template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1, |
| 521 | class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2> |
| 522 | friend |
| 523 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> |
| 524 | move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f, |
| 525 | __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l, |
| 526 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r); |
| 527 | |
| 528 | template <class _RAIter, |
| 529 | class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2> |
| 530 | friend |
| 531 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> |
| 532 | move_backward(_RAIter __f, |
| 533 | _RAIter __l, |
| 534 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 535 | typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 536 | |
| 537 | template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1, |
| 538 | class _OutputIterator> |
| 539 | friend |
| 540 | _OutputIterator |
| 541 | move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f, |
| 542 | __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l, |
| 543 | _OutputIterator __r); |
| 544 | |
| 545 | template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1, |
| 546 | class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2> |
| 547 | friend |
| 548 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> |
| 549 | move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f, |
| 550 | __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l, |
| 551 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r); |
| 552 | }; |
| 553 | |
Evgeniy Stepanov | c299de2 | 2015-11-06 22:02:29 +0000 | [diff] [blame] | 554 | template <class _ValueType, class _Pointer, class _Reference, class _MapPointer, |
| 555 | class _DiffType, _DiffType _BlockSize> |
| 556 | const _DiffType __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer, |
| 557 | _DiffType, _BlockSize>::__block_size = |
| 558 | __deque_block_size<_ValueType, _DiffType>::value; |
| 559 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 560 | // copy |
| 561 | |
| 562 | template <class _RAIter, |
| 563 | class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2> |
| 564 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> |
| 565 | copy(_RAIter __f, |
| 566 | _RAIter __l, |
| 567 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 568 | typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 569 | { |
| 570 | typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type; |
| 571 | typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer; |
Evgeniy Stepanov | c299de2 | 2015-11-06 22:02:29 +0000 | [diff] [blame] | 572 | const difference_type __block_size = __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::__block_size; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 573 | while (__f != __l) |
| 574 | { |
| 575 | pointer __rb = __r.__ptr_; |
Evgeniy Stepanov | c299de2 | 2015-11-06 22:02:29 +0000 | [diff] [blame] | 576 | pointer __re = *__r.__m_iter_ + __block_size; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 577 | difference_type __bs = __re - __rb; |
| 578 | difference_type __n = __l - __f; |
| 579 | _RAIter __m = __l; |
| 580 | if (__n > __bs) |
| 581 | { |
| 582 | __n = __bs; |
| 583 | __m = __f + __n; |
| 584 | } |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 585 | _VSTD::copy(__f, __m, __rb); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 586 | __f = __m; |
| 587 | __r += __n; |
| 588 | } |
| 589 | return __r; |
| 590 | } |
| 591 | |
| 592 | template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1, |
| 593 | class _OutputIterator> |
| 594 | _OutputIterator |
| 595 | copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f, |
| 596 | __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l, |
| 597 | _OutputIterator __r) |
| 598 | { |
| 599 | typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type; |
| 600 | typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer; |
Evgeniy Stepanov | c299de2 | 2015-11-06 22:02:29 +0000 | [diff] [blame] | 601 | const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 602 | difference_type __n = __l - __f; |
| 603 | while (__n > 0) |
| 604 | { |
| 605 | pointer __fb = __f.__ptr_; |
Evgeniy Stepanov | c299de2 | 2015-11-06 22:02:29 +0000 | [diff] [blame] | 606 | pointer __fe = *__f.__m_iter_ + __block_size; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 607 | difference_type __bs = __fe - __fb; |
| 608 | if (__bs > __n) |
| 609 | { |
| 610 | __bs = __n; |
| 611 | __fe = __fb + __bs; |
| 612 | } |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 613 | __r = _VSTD::copy(__fb, __fe, __r); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 614 | __n -= __bs; |
| 615 | __f += __bs; |
| 616 | } |
| 617 | return __r; |
| 618 | } |
| 619 | |
| 620 | template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1, |
| 621 | class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2> |
| 622 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> |
| 623 | copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f, |
| 624 | __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l, |
| 625 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r) |
| 626 | { |
| 627 | typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type; |
| 628 | typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer; |
Evgeniy Stepanov | c299de2 | 2015-11-06 22:02:29 +0000 | [diff] [blame] | 629 | const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 630 | difference_type __n = __l - __f; |
| 631 | while (__n > 0) |
| 632 | { |
| 633 | pointer __fb = __f.__ptr_; |
Evgeniy Stepanov | c299de2 | 2015-11-06 22:02:29 +0000 | [diff] [blame] | 634 | pointer __fe = *__f.__m_iter_ + __block_size; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 635 | difference_type __bs = __fe - __fb; |
| 636 | if (__bs > __n) |
| 637 | { |
| 638 | __bs = __n; |
| 639 | __fe = __fb + __bs; |
| 640 | } |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 641 | __r = _VSTD::copy(__fb, __fe, __r); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 642 | __n -= __bs; |
| 643 | __f += __bs; |
| 644 | } |
| 645 | return __r; |
| 646 | } |
| 647 | |
| 648 | // copy_backward |
| 649 | |
| 650 | template <class _RAIter, |
| 651 | class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2> |
| 652 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> |
| 653 | copy_backward(_RAIter __f, |
| 654 | _RAIter __l, |
| 655 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 656 | typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 657 | { |
| 658 | typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type; |
| 659 | typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer; |
| 660 | while (__f != __l) |
| 661 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 662 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 663 | pointer __rb = *__rp.__m_iter_; |
| 664 | pointer __re = __rp.__ptr_ + 1; |
| 665 | difference_type __bs = __re - __rb; |
| 666 | difference_type __n = __l - __f; |
| 667 | _RAIter __m = __f; |
| 668 | if (__n > __bs) |
| 669 | { |
| 670 | __n = __bs; |
| 671 | __m = __l - __n; |
| 672 | } |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 673 | _VSTD::copy_backward(__m, __l, __re); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 674 | __l = __m; |
| 675 | __r -= __n; |
| 676 | } |
| 677 | return __r; |
| 678 | } |
| 679 | |
| 680 | template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1, |
| 681 | class _OutputIterator> |
| 682 | _OutputIterator |
| 683 | copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f, |
| 684 | __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l, |
| 685 | _OutputIterator __r) |
| 686 | { |
| 687 | typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type; |
| 688 | typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer; |
| 689 | difference_type __n = __l - __f; |
| 690 | while (__n > 0) |
| 691 | { |
| 692 | --__l; |
| 693 | pointer __lb = *__l.__m_iter_; |
| 694 | pointer __le = __l.__ptr_ + 1; |
| 695 | difference_type __bs = __le - __lb; |
| 696 | if (__bs > __n) |
| 697 | { |
| 698 | __bs = __n; |
| 699 | __lb = __le - __bs; |
| 700 | } |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 701 | __r = _VSTD::copy_backward(__lb, __le, __r); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 702 | __n -= __bs; |
| 703 | __l -= __bs - 1; |
| 704 | } |
| 705 | return __r; |
| 706 | } |
| 707 | |
| 708 | template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1, |
| 709 | class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2> |
| 710 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> |
| 711 | copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f, |
| 712 | __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l, |
| 713 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r) |
| 714 | { |
| 715 | typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type; |
| 716 | typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer; |
| 717 | difference_type __n = __l - __f; |
| 718 | while (__n > 0) |
| 719 | { |
| 720 | --__l; |
| 721 | pointer __lb = *__l.__m_iter_; |
| 722 | pointer __le = __l.__ptr_ + 1; |
| 723 | difference_type __bs = __le - __lb; |
| 724 | if (__bs > __n) |
| 725 | { |
| 726 | __bs = __n; |
| 727 | __lb = __le - __bs; |
| 728 | } |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 729 | __r = _VSTD::copy_backward(__lb, __le, __r); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 730 | __n -= __bs; |
| 731 | __l -= __bs - 1; |
| 732 | } |
| 733 | return __r; |
| 734 | } |
| 735 | |
| 736 | // move |
| 737 | |
| 738 | template <class _RAIter, |
| 739 | class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2> |
| 740 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> |
| 741 | move(_RAIter __f, |
| 742 | _RAIter __l, |
| 743 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 744 | typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 745 | { |
| 746 | typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type; |
| 747 | typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer; |
Evgeniy Stepanov | c299de2 | 2015-11-06 22:02:29 +0000 | [diff] [blame] | 748 | const difference_type __block_size = __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::__block_size; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 749 | while (__f != __l) |
| 750 | { |
| 751 | pointer __rb = __r.__ptr_; |
Evgeniy Stepanov | c299de2 | 2015-11-06 22:02:29 +0000 | [diff] [blame] | 752 | pointer __re = *__r.__m_iter_ + __block_size; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 753 | difference_type __bs = __re - __rb; |
| 754 | difference_type __n = __l - __f; |
| 755 | _RAIter __m = __l; |
| 756 | if (__n > __bs) |
| 757 | { |
| 758 | __n = __bs; |
| 759 | __m = __f + __n; |
| 760 | } |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 761 | _VSTD::move(__f, __m, __rb); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 762 | __f = __m; |
| 763 | __r += __n; |
| 764 | } |
| 765 | return __r; |
| 766 | } |
| 767 | |
| 768 | template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1, |
| 769 | class _OutputIterator> |
| 770 | _OutputIterator |
| 771 | move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f, |
| 772 | __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l, |
| 773 | _OutputIterator __r) |
| 774 | { |
| 775 | typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type; |
| 776 | typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer; |
Evgeniy Stepanov | c299de2 | 2015-11-06 22:02:29 +0000 | [diff] [blame] | 777 | const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 778 | difference_type __n = __l - __f; |
| 779 | while (__n > 0) |
| 780 | { |
| 781 | pointer __fb = __f.__ptr_; |
Evgeniy Stepanov | c299de2 | 2015-11-06 22:02:29 +0000 | [diff] [blame] | 782 | pointer __fe = *__f.__m_iter_ + __block_size; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 783 | difference_type __bs = __fe - __fb; |
| 784 | if (__bs > __n) |
| 785 | { |
| 786 | __bs = __n; |
| 787 | __fe = __fb + __bs; |
| 788 | } |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 789 | __r = _VSTD::move(__fb, __fe, __r); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 790 | __n -= __bs; |
| 791 | __f += __bs; |
| 792 | } |
| 793 | return __r; |
| 794 | } |
| 795 | |
| 796 | template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1, |
| 797 | class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2> |
| 798 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> |
| 799 | move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f, |
| 800 | __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l, |
| 801 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r) |
| 802 | { |
| 803 | typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type; |
| 804 | typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer; |
Evgeniy Stepanov | c299de2 | 2015-11-06 22:02:29 +0000 | [diff] [blame] | 805 | const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 806 | difference_type __n = __l - __f; |
| 807 | while (__n > 0) |
| 808 | { |
| 809 | pointer __fb = __f.__ptr_; |
Evgeniy Stepanov | c299de2 | 2015-11-06 22:02:29 +0000 | [diff] [blame] | 810 | pointer __fe = *__f.__m_iter_ + __block_size; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 811 | difference_type __bs = __fe - __fb; |
| 812 | if (__bs > __n) |
| 813 | { |
| 814 | __bs = __n; |
| 815 | __fe = __fb + __bs; |
| 816 | } |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 817 | __r = _VSTD::move(__fb, __fe, __r); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 818 | __n -= __bs; |
| 819 | __f += __bs; |
| 820 | } |
| 821 | return __r; |
| 822 | } |
| 823 | |
| 824 | // move_backward |
| 825 | |
| 826 | template <class _RAIter, |
| 827 | class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2> |
| 828 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> |
| 829 | move_backward(_RAIter __f, |
| 830 | _RAIter __l, |
| 831 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 832 | typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 833 | { |
| 834 | typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type; |
| 835 | typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer; |
| 836 | while (__f != __l) |
| 837 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 838 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 839 | pointer __rb = *__rp.__m_iter_; |
| 840 | pointer __re = __rp.__ptr_ + 1; |
| 841 | difference_type __bs = __re - __rb; |
| 842 | difference_type __n = __l - __f; |
| 843 | _RAIter __m = __f; |
| 844 | if (__n > __bs) |
| 845 | { |
| 846 | __n = __bs; |
| 847 | __m = __l - __n; |
| 848 | } |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 849 | _VSTD::move_backward(__m, __l, __re); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 850 | __l = __m; |
| 851 | __r -= __n; |
| 852 | } |
| 853 | return __r; |
| 854 | } |
| 855 | |
| 856 | template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1, |
| 857 | class _OutputIterator> |
| 858 | _OutputIterator |
| 859 | move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f, |
| 860 | __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l, |
| 861 | _OutputIterator __r) |
| 862 | { |
| 863 | typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type; |
| 864 | typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer; |
| 865 | difference_type __n = __l - __f; |
| 866 | while (__n > 0) |
| 867 | { |
| 868 | --__l; |
| 869 | pointer __lb = *__l.__m_iter_; |
| 870 | pointer __le = __l.__ptr_ + 1; |
| 871 | difference_type __bs = __le - __lb; |
| 872 | if (__bs > __n) |
| 873 | { |
| 874 | __bs = __n; |
| 875 | __lb = __le - __bs; |
| 876 | } |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 877 | __r = _VSTD::move_backward(__lb, __le, __r); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 878 | __n -= __bs; |
| 879 | __l -= __bs - 1; |
| 880 | } |
| 881 | return __r; |
| 882 | } |
| 883 | |
| 884 | template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1, |
| 885 | class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2> |
| 886 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> |
| 887 | move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f, |
| 888 | __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l, |
| 889 | __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r) |
| 890 | { |
| 891 | typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type; |
| 892 | typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer; |
| 893 | difference_type __n = __l - __f; |
| 894 | while (__n > 0) |
| 895 | { |
| 896 | --__l; |
| 897 | pointer __lb = *__l.__m_iter_; |
| 898 | pointer __le = __l.__ptr_ + 1; |
| 899 | difference_type __bs = __le - __lb; |
| 900 | if (__bs > __n) |
| 901 | { |
| 902 | __bs = __n; |
| 903 | __lb = __le - __bs; |
| 904 | } |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 905 | __r = _VSTD::move_backward(__lb, __le, __r); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 906 | __n -= __bs; |
| 907 | __l -= __bs - 1; |
| 908 | } |
| 909 | return __r; |
| 910 | } |
| 911 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 912 | template <class _Tp, class _Allocator> |
| 913 | class __deque_base |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 914 | { |
| 915 | __deque_base(const __deque_base& __c); |
| 916 | __deque_base& operator=(const __deque_base& __c); |
Marshall Clow | 4cc3a45 | 2018-05-18 23:44:13 +0000 | [diff] [blame] | 917 | public: |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 918 | typedef _Allocator allocator_type; |
| 919 | typedef allocator_traits<allocator_type> __alloc_traits; |
Marshall Clow | 4cc3a45 | 2018-05-18 23:44:13 +0000 | [diff] [blame] | 920 | typedef typename __alloc_traits::size_type size_type; |
Eric Fiselier | 27e0362 | 2019-08-01 23:11:18 +0000 | [diff] [blame] | 921 | |
Marshall Clow | 4cc3a45 | 2018-05-18 23:44:13 +0000 | [diff] [blame] | 922 | typedef _Tp value_type; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 923 | typedef value_type& reference; |
| 924 | typedef const value_type& const_reference; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 925 | typedef typename __alloc_traits::difference_type difference_type; |
| 926 | typedef typename __alloc_traits::pointer pointer; |
| 927 | typedef typename __alloc_traits::const_pointer const_pointer; |
| 928 | |
Evgeniy Stepanov | c299de2 | 2015-11-06 22:02:29 +0000 | [diff] [blame] | 929 | static const difference_type __block_size; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 930 | |
Marshall Clow | 940e01c | 2015-04-07 05:21:38 +0000 | [diff] [blame] | 931 | typedef typename __rebind_alloc_helper<__alloc_traits, pointer>::type __pointer_allocator; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 932 | typedef allocator_traits<__pointer_allocator> __map_traits; |
| 933 | typedef typename __map_traits::pointer __map_pointer; |
Marshall Clow | 940e01c | 2015-04-07 05:21:38 +0000 | [diff] [blame] | 934 | typedef typename __rebind_alloc_helper<__alloc_traits, const_pointer>::type __const_pointer_allocator; |
Howard Hinnant | dcb73a3 | 2013-06-23 21:17:24 +0000 | [diff] [blame] | 935 | typedef typename allocator_traits<__const_pointer_allocator>::const_pointer __map_const_pointer; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 936 | typedef __split_buffer<pointer, __pointer_allocator> __map; |
| 937 | |
| 938 | typedef __deque_iterator<value_type, pointer, reference, __map_pointer, |
Evgeniy Stepanov | c299de2 | 2015-11-06 22:02:29 +0000 | [diff] [blame] | 939 | difference_type> iterator; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 940 | typedef __deque_iterator<value_type, const_pointer, const_reference, __map_const_pointer, |
Evgeniy Stepanov | c299de2 | 2015-11-06 22:02:29 +0000 | [diff] [blame] | 941 | difference_type> const_iterator; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 942 | |
Eric Fiselier | c52db21 | 2019-08-12 07:51:05 +0000 | [diff] [blame] | 943 | struct __deque_block_range { |
| 944 | explicit __deque_block_range(pointer __b, pointer __e) _NOEXCEPT : __begin_(__b), __end_(__e) {} |
| 945 | const pointer __begin_; |
| 946 | const pointer __end_; |
| 947 | }; |
| 948 | |
| 949 | struct __deque_range { |
| 950 | iterator __pos_; |
| 951 | const iterator __end_; |
| 952 | |
| 953 | __deque_range(iterator __pos, iterator __e) _NOEXCEPT |
| 954 | : __pos_(__pos), __end_(__e) {} |
| 955 | |
| 956 | explicit operator bool() const _NOEXCEPT { |
| 957 | return __pos_ != __end_; |
| 958 | } |
| 959 | |
| 960 | __deque_range begin() const { |
| 961 | return *this; |
| 962 | } |
| 963 | |
| 964 | __deque_range end() const { |
| 965 | return __deque_range(__end_, __end_); |
| 966 | } |
| 967 | __deque_block_range operator*() const _NOEXCEPT { |
| 968 | if (__pos_.__m_iter_ == __end_.__m_iter_) { |
| 969 | return __deque_block_range(__pos_.__ptr_, __end_.__ptr_); |
| 970 | } |
| 971 | return __deque_block_range(__pos_.__ptr_, *__pos_.__m_iter_ + __block_size); |
| 972 | } |
| 973 | |
| 974 | __deque_range& operator++() _NOEXCEPT { |
| 975 | if (__pos_.__m_iter_ == __end_.__m_iter_) { |
| 976 | __pos_ = __end_; |
| 977 | } else { |
| 978 | ++__pos_.__m_iter_; |
| 979 | __pos_.__ptr_ = *__pos_.__m_iter_; |
| 980 | } |
| 981 | return *this; |
| 982 | } |
| 983 | |
| 984 | |
| 985 | friend bool operator==(__deque_range const& __lhs, __deque_range const& __rhs) { |
| 986 | return __lhs.__pos_ == __rhs.__pos_; |
| 987 | } |
| 988 | friend bool operator!=(__deque_range const& __lhs, __deque_range const& __rhs) { |
| 989 | return !(__lhs == __rhs); |
| 990 | } |
| 991 | }; |
| 992 | |
| 993 | |
| 994 | |
| 995 | struct _ConstructTransaction { |
| 996 | _ConstructTransaction(__deque_base* __db, __deque_block_range& __r) |
| 997 | : __pos_(__r.__begin_), __end_(__r.__end_), __begin_(__r.__begin_), __base_(__db) {} |
| 998 | |
| 999 | |
| 1000 | ~_ConstructTransaction() { |
| 1001 | __base_->size() += (__pos_ - __begin_); |
| 1002 | } |
| 1003 | |
| 1004 | pointer __pos_; |
| 1005 | const pointer __end_; |
| 1006 | private: |
| 1007 | const pointer __begin_; |
| 1008 | __deque_base * const __base_; |
| 1009 | }; |
| 1010 | |
Marshall Clow | 4cc3a45 | 2018-05-18 23:44:13 +0000 | [diff] [blame] | 1011 | protected: |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1012 | __map __map_; |
| 1013 | size_type __start_; |
| 1014 | __compressed_pair<size_type, allocator_type> __size_; |
| 1015 | |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 1016 | iterator begin() _NOEXCEPT; |
| 1017 | const_iterator begin() const _NOEXCEPT; |
| 1018 | iterator end() _NOEXCEPT; |
| 1019 | const_iterator end() const _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1020 | |
| 1021 | _LIBCPP_INLINE_VISIBILITY size_type& size() {return __size_.first();} |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 1022 | _LIBCPP_INLINE_VISIBILITY |
| 1023 | const size_type& size() const _NOEXCEPT {return __size_.first();} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1024 | _LIBCPP_INLINE_VISIBILITY allocator_type& __alloc() {return __size_.second();} |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 1025 | _LIBCPP_INLINE_VISIBILITY |
| 1026 | const allocator_type& __alloc() const _NOEXCEPT {return __size_.second();} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1027 | |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1028 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | ad979ba | 2011-06-03 15:16:49 +0000 | [diff] [blame] | 1029 | __deque_base() |
| 1030 | _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value); |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1031 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1032 | explicit __deque_base(const allocator_type& __a); |
Howard Hinnant | ca2fc22 | 2011-06-02 20:00:14 +0000 | [diff] [blame] | 1033 | public: |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1034 | ~__deque_base(); |
| 1035 | |
Eric Fiselier | 212e3f2 | 2017-04-16 03:17:01 +0000 | [diff] [blame] | 1036 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | ca2fc22 | 2011-06-02 20:00:14 +0000 | [diff] [blame] | 1037 | __deque_base(__deque_base&& __c) |
| 1038 | _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1039 | __deque_base(__deque_base&& __c, const allocator_type& __a); |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 1040 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1041 | |
Howard Hinnant | ca2fc22 | 2011-06-02 20:00:14 +0000 | [diff] [blame] | 1042 | void swap(__deque_base& __c) |
Marshall Clow | 8982dcd | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 1043 | #if _LIBCPP_STD_VER >= 14 |
| 1044 | _NOEXCEPT; |
| 1045 | #else |
Louis Dionne | 72f2439 | 2018-12-12 23:58:25 +0000 | [diff] [blame] | 1046 | _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || |
Marshall Clow | 8982dcd | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 1047 | __is_nothrow_swappable<allocator_type>::value); |
| 1048 | #endif |
Howard Hinnant | ca2fc22 | 2011-06-02 20:00:14 +0000 | [diff] [blame] | 1049 | protected: |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 1050 | void clear() _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1051 | |
| 1052 | bool __invariants() const; |
| 1053 | |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1054 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1055 | void __move_assign(__deque_base& __c) |
Howard Hinnant | 4931e09 | 2011-06-02 21:38:57 +0000 | [diff] [blame] | 1056 | _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value && |
| 1057 | is_nothrow_move_assignable<allocator_type>::value) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1058 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1059 | __map_ = _VSTD::move(__c.__map_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1060 | __start_ = __c.__start_; |
| 1061 | size() = __c.size(); |
| 1062 | __move_assign_alloc(__c); |
| 1063 | __c.__start_ = __c.size() = 0; |
| 1064 | } |
| 1065 | |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1066 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1067 | void __move_assign_alloc(__deque_base& __c) |
Howard Hinnant | ca2fc22 | 2011-06-02 20:00:14 +0000 | [diff] [blame] | 1068 | _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value || |
| 1069 | is_nothrow_move_assignable<allocator_type>::value) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1070 | {__move_assign_alloc(__c, integral_constant<bool, |
| 1071 | __alloc_traits::propagate_on_container_move_assignment::value>());} |
| 1072 | |
| 1073 | private: |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1074 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c273496 | 2011-09-02 20:42:31 +0000 | [diff] [blame] | 1075 | void __move_assign_alloc(__deque_base& __c, true_type) |
Howard Hinnant | ca2fc22 | 2011-06-02 20:00:14 +0000 | [diff] [blame] | 1076 | _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1077 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1078 | __alloc() = _VSTD::move(__c.__alloc()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1081 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 1082 | void __move_assign_alloc(__deque_base&, false_type) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1083 | {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1084 | }; |
| 1085 | |
| 1086 | template <class _Tp, class _Allocator> |
Evgeniy Stepanov | c299de2 | 2015-11-06 22:02:29 +0000 | [diff] [blame] | 1087 | const typename __deque_base<_Tp, _Allocator>::difference_type |
| 1088 | __deque_base<_Tp, _Allocator>::__block_size = |
| 1089 | __deque_block_size<value_type, difference_type>::value; |
| 1090 | |
| 1091 | template <class _Tp, class _Allocator> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1092 | bool |
| 1093 | __deque_base<_Tp, _Allocator>::__invariants() const |
| 1094 | { |
| 1095 | if (!__map_.__invariants()) |
| 1096 | return false; |
| 1097 | if (__map_.size() >= size_type(-1) / __block_size) |
| 1098 | return false; |
| 1099 | for (typename __map::const_iterator __i = __map_.begin(), __e = __map_.end(); |
| 1100 | __i != __e; ++__i) |
| 1101 | if (*__i == nullptr) |
| 1102 | return false; |
| 1103 | if (__map_.size() != 0) |
| 1104 | { |
| 1105 | if (size() >= __map_.size() * __block_size) |
| 1106 | return false; |
| 1107 | if (__start_ >= __map_.size() * __block_size - size()) |
| 1108 | return false; |
| 1109 | } |
| 1110 | else |
| 1111 | { |
| 1112 | if (size() != 0) |
| 1113 | return false; |
| 1114 | if (__start_ != 0) |
| 1115 | return false; |
| 1116 | } |
| 1117 | return true; |
| 1118 | } |
| 1119 | |
| 1120 | template <class _Tp, class _Allocator> |
| 1121 | typename __deque_base<_Tp, _Allocator>::iterator |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 1122 | __deque_base<_Tp, _Allocator>::begin() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1123 | { |
| 1124 | __map_pointer __mp = __map_.begin() + __start_ / __block_size; |
| 1125 | return iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size); |
| 1126 | } |
| 1127 | |
| 1128 | template <class _Tp, class _Allocator> |
| 1129 | typename __deque_base<_Tp, _Allocator>::const_iterator |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 1130 | __deque_base<_Tp, _Allocator>::begin() const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1131 | { |
Howard Hinnant | dcb73a3 | 2013-06-23 21:17:24 +0000 | [diff] [blame] | 1132 | __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __start_ / __block_size); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1133 | return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size); |
| 1134 | } |
| 1135 | |
| 1136 | template <class _Tp, class _Allocator> |
| 1137 | typename __deque_base<_Tp, _Allocator>::iterator |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 1138 | __deque_base<_Tp, _Allocator>::end() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1139 | { |
| 1140 | size_type __p = size() + __start_; |
| 1141 | __map_pointer __mp = __map_.begin() + __p / __block_size; |
| 1142 | return iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size); |
| 1143 | } |
| 1144 | |
| 1145 | template <class _Tp, class _Allocator> |
| 1146 | typename __deque_base<_Tp, _Allocator>::const_iterator |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 1147 | __deque_base<_Tp, _Allocator>::end() const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1148 | { |
| 1149 | size_type __p = size() + __start_; |
Howard Hinnant | dcb73a3 | 2013-06-23 21:17:24 +0000 | [diff] [blame] | 1150 | __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __p / __block_size); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1151 | return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size); |
| 1152 | } |
| 1153 | |
| 1154 | template <class _Tp, class _Allocator> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1155 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1156 | __deque_base<_Tp, _Allocator>::__deque_base() |
Howard Hinnant | ad979ba | 2011-06-03 15:16:49 +0000 | [diff] [blame] | 1157 | _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value) |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 1158 | : __start_(0), __size_(0, __default_init_tag()) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1159 | |
| 1160 | template <class _Tp, class _Allocator> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1161 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1162 | __deque_base<_Tp, _Allocator>::__deque_base(const allocator_type& __a) |
| 1163 | : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {} |
| 1164 | |
| 1165 | template <class _Tp, class _Allocator> |
| 1166 | __deque_base<_Tp, _Allocator>::~__deque_base() |
| 1167 | { |
| 1168 | clear(); |
| 1169 | typename __map::iterator __i = __map_.begin(); |
| 1170 | typename __map::iterator __e = __map_.end(); |
| 1171 | for (; __i != __e; ++__i) |
| 1172 | __alloc_traits::deallocate(__alloc(), *__i, __block_size); |
| 1173 | } |
| 1174 | |
Eric Fiselier | 212e3f2 | 2017-04-16 03:17:01 +0000 | [diff] [blame] | 1175 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1176 | |
| 1177 | template <class _Tp, class _Allocator> |
| 1178 | __deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c) |
Howard Hinnant | ca2fc22 | 2011-06-02 20:00:14 +0000 | [diff] [blame] | 1179 | _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1180 | : __map_(_VSTD::move(__c.__map_)), |
| 1181 | __start_(_VSTD::move(__c.__start_)), |
| 1182 | __size_(_VSTD::move(__c.__size_)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1183 | { |
| 1184 | __c.__start_ = 0; |
| 1185 | __c.size() = 0; |
| 1186 | } |
| 1187 | |
| 1188 | template <class _Tp, class _Allocator> |
| 1189 | __deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c, const allocator_type& __a) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1190 | : __map_(_VSTD::move(__c.__map_), __pointer_allocator(__a)), |
| 1191 | __start_(_VSTD::move(__c.__start_)), |
| 1192 | __size_(_VSTD::move(__c.size()), __a) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1193 | { |
| 1194 | if (__a == __c.__alloc()) |
| 1195 | { |
| 1196 | __c.__start_ = 0; |
| 1197 | __c.size() = 0; |
| 1198 | } |
| 1199 | else |
| 1200 | { |
| 1201 | __map_.clear(); |
| 1202 | __start_ = 0; |
| 1203 | size() = 0; |
| 1204 | } |
| 1205 | } |
| 1206 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 1207 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1208 | |
| 1209 | template <class _Tp, class _Allocator> |
| 1210 | void |
| 1211 | __deque_base<_Tp, _Allocator>::swap(__deque_base& __c) |
Marshall Clow | 8982dcd | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 1212 | #if _LIBCPP_STD_VER >= 14 |
| 1213 | _NOEXCEPT |
| 1214 | #else |
Louis Dionne | 72f2439 | 2018-12-12 23:58:25 +0000 | [diff] [blame] | 1215 | _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || |
Marshall Clow | 8982dcd | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 1216 | __is_nothrow_swappable<allocator_type>::value) |
| 1217 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1218 | { |
| 1219 | __map_.swap(__c.__map_); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1220 | _VSTD::swap(__start_, __c.__start_); |
| 1221 | _VSTD::swap(size(), __c.size()); |
Arthur O'Dwyer | 4e0de31 | 2020-11-18 18:54:38 -0500 | [diff] [blame] | 1222 | _VSTD::__swap_allocator(__alloc(), __c.__alloc()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | template <class _Tp, class _Allocator> |
| 1226 | void |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 1227 | __deque_base<_Tp, _Allocator>::clear() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1228 | { |
| 1229 | allocator_type& __a = __alloc(); |
| 1230 | for (iterator __i = begin(), __e = end(); __i != __e; ++__i) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1231 | __alloc_traits::destroy(__a, _VSTD::addressof(*__i)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1232 | size() = 0; |
| 1233 | while (__map_.size() > 2) |
| 1234 | { |
| 1235 | __alloc_traits::deallocate(__a, __map_.front(), __block_size); |
| 1236 | __map_.pop_front(); |
| 1237 | } |
| 1238 | switch (__map_.size()) |
| 1239 | { |
| 1240 | case 1: |
| 1241 | __start_ = __block_size / 2; |
| 1242 | break; |
| 1243 | case 2: |
| 1244 | __start_ = __block_size; |
| 1245 | break; |
| 1246 | } |
| 1247 | } |
| 1248 | |
Marshall Clow | 65cd4c6 | 2015-02-18 17:24:08 +0000 | [diff] [blame] | 1249 | template <class _Tp, class _Allocator /*= allocator<_Tp>*/> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 1250 | class _LIBCPP_TEMPLATE_VIS deque |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1251 | : private __deque_base<_Tp, _Allocator> |
| 1252 | { |
| 1253 | public: |
| 1254 | // types: |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1255 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1256 | typedef _Tp value_type; |
| 1257 | typedef _Allocator allocator_type; |
| 1258 | |
Marshall Clow | 5128cf3 | 2015-11-26 01:24:04 +0000 | [diff] [blame] | 1259 | static_assert((is_same<typename allocator_type::value_type, value_type>::value), |
| 1260 | "Allocator::value_type must be same type as value_type"); |
| 1261 | |
Konstantin Varlamov | 53b543c | 2021-11-09 09:21:02 -0800 | [diff] [blame] | 1262 | typedef __deque_base<value_type, allocator_type> __base; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1263 | |
Konstantin Varlamov | 53b543c | 2021-11-09 09:21:02 -0800 | [diff] [blame] | 1264 | typedef typename __base::__alloc_traits __alloc_traits; |
| 1265 | typedef typename __base::reference reference; |
| 1266 | typedef typename __base::const_reference const_reference; |
| 1267 | typedef typename __base::iterator iterator; |
| 1268 | typedef typename __base::const_iterator const_iterator; |
| 1269 | typedef typename __allocator_traits<allocator_type>::size_type size_type; |
| 1270 | typedef typename __base::difference_type difference_type; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1271 | |
Konstantin Varlamov | 53b543c | 2021-11-09 09:21:02 -0800 | [diff] [blame] | 1272 | typedef typename __base::pointer pointer; |
| 1273 | typedef typename __base::const_pointer const_pointer; |
| 1274 | typedef _VSTD::reverse_iterator<iterator> reverse_iterator; |
| 1275 | typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1276 | |
Eric Fiselier | c52db21 | 2019-08-12 07:51:05 +0000 | [diff] [blame] | 1277 | using typename __base::__deque_range; |
| 1278 | using typename __base::__deque_block_range; |
| 1279 | using typename __base::_ConstructTransaction; |
| 1280 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1281 | // construct/copy/destroy: |
Howard Hinnant | ad979ba | 2011-06-03 15:16:49 +0000 | [diff] [blame] | 1282 | _LIBCPP_INLINE_VISIBILITY |
| 1283 | deque() |
| 1284 | _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value) |
| 1285 | {} |
Marshall Clow | 7ed23a7 | 2014-03-05 19:06:20 +0000 | [diff] [blame] | 1286 | _LIBCPP_INLINE_VISIBILITY explicit deque(const allocator_type& __a) : __base(__a) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1287 | explicit deque(size_type __n); |
Marshall Clow | bc4fcb4 | 2013-09-07 16:16:19 +0000 | [diff] [blame] | 1288 | #if _LIBCPP_STD_VER > 11 |
| 1289 | explicit deque(size_type __n, const _Allocator& __a); |
| 1290 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1291 | deque(size_type __n, const value_type& __v); |
| 1292 | deque(size_type __n, const value_type& __v, const allocator_type& __a); |
| 1293 | template <class _InputIter> |
| 1294 | deque(_InputIter __f, _InputIter __l, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 1295 | typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type* = 0); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1296 | template <class _InputIter> |
| 1297 | deque(_InputIter __f, _InputIter __l, const allocator_type& __a, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 1298 | typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type* = 0); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1299 | deque(const deque& __c); |
Arthur O'Dwyer | 9b9662f | 2021-03-01 17:08:24 -0500 | [diff] [blame] | 1300 | deque(const deque& __c, const __identity_t<allocator_type>& __a); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1301 | |
| 1302 | deque& operator=(const deque& __c); |
Eric Fiselier | 212e3f2 | 2017-04-16 03:17:01 +0000 | [diff] [blame] | 1303 | |
| 1304 | #ifndef _LIBCPP_CXX03_LANG |
| 1305 | deque(initializer_list<value_type> __il); |
| 1306 | deque(initializer_list<value_type> __il, const allocator_type& __a); |
| 1307 | |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1308 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1309 | deque& operator=(initializer_list<value_type> __il) {assign(__il); return *this;} |
| 1310 | |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1311 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | ca2fc22 | 2011-06-02 20:00:14 +0000 | [diff] [blame] | 1312 | deque(deque&& __c) _NOEXCEPT_(is_nothrow_move_constructible<__base>::value); |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1313 | _LIBCPP_INLINE_VISIBILITY |
Arthur O'Dwyer | 9b9662f | 2021-03-01 17:08:24 -0500 | [diff] [blame] | 1314 | deque(deque&& __c, const __identity_t<allocator_type>& __a); |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1315 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | ca2fc22 | 2011-06-02 20:00:14 +0000 | [diff] [blame] | 1316 | deque& operator=(deque&& __c) |
Howard Hinnant | 4931e09 | 2011-06-02 21:38:57 +0000 | [diff] [blame] | 1317 | _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value && |
| 1318 | is_nothrow_move_assignable<allocator_type>::value); |
Eric Fiselier | 212e3f2 | 2017-04-16 03:17:01 +0000 | [diff] [blame] | 1319 | |
| 1320 | _LIBCPP_INLINE_VISIBILITY |
| 1321 | void assign(initializer_list<value_type> __il) {assign(__il.begin(), __il.end());} |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 1322 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1323 | |
| 1324 | template <class _InputIter> |
| 1325 | void assign(_InputIter __f, _InputIter __l, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 1326 | typename enable_if<__is_cpp17_input_iterator<_InputIter>::value && |
| 1327 | !__is_cpp17_random_access_iterator<_InputIter>::value>::type* = 0); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1328 | template <class _RAIter> |
| 1329 | void assign(_RAIter __f, _RAIter __l, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 1330 | typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1331 | void assign(size_type __n, const value_type& __v); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1332 | |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1333 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 1334 | allocator_type get_allocator() const _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1335 | |
| 1336 | // iterators: |
| 1337 | |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1338 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 1339 | iterator begin() _NOEXCEPT {return __base::begin();} |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1340 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 1341 | const_iterator begin() const _NOEXCEPT {return __base::begin();} |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1342 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 1343 | iterator end() _NOEXCEPT {return __base::end();} |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1344 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 1345 | const_iterator end() const _NOEXCEPT {return __base::end();} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1346 | |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1347 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 1348 | reverse_iterator rbegin() _NOEXCEPT |
| 1349 | {return reverse_iterator(__base::end());} |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1350 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 1351 | const_reverse_iterator rbegin() const _NOEXCEPT |
| 1352 | {return const_reverse_iterator(__base::end());} |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1353 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 1354 | reverse_iterator rend() _NOEXCEPT |
| 1355 | {return reverse_iterator(__base::begin());} |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1356 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 1357 | const_reverse_iterator rend() const _NOEXCEPT |
| 1358 | {return const_reverse_iterator(__base::begin());} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1359 | |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1360 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 1361 | const_iterator cbegin() const _NOEXCEPT |
| 1362 | {return __base::begin();} |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1363 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 1364 | const_iterator cend() const _NOEXCEPT |
| 1365 | {return __base::end();} |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1366 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 1367 | const_reverse_iterator crbegin() const _NOEXCEPT |
| 1368 | {return const_reverse_iterator(__base::end());} |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1369 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 1370 | const_reverse_iterator crend() const _NOEXCEPT |
| 1371 | {return const_reverse_iterator(__base::begin());} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1372 | |
| 1373 | // capacity: |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1374 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 1375 | size_type size() const _NOEXCEPT {return __base::size();} |
| 1376 | _LIBCPP_INLINE_VISIBILITY |
| 1377 | size_type max_size() const _NOEXCEPT |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 1378 | {return _VSTD::min<size_type>( |
Eric Fiselier | b5d9f44 | 2016-11-23 01:18:56 +0000 | [diff] [blame] | 1379 | __alloc_traits::max_size(__base::__alloc()), |
| 1380 | numeric_limits<difference_type>::max());} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1381 | void resize(size_type __n); |
| 1382 | void resize(size_type __n, const value_type& __v); |
Howard Hinnant | 4931e09 | 2011-06-02 21:38:57 +0000 | [diff] [blame] | 1383 | void shrink_to_fit() _NOEXCEPT; |
Marshall Clow | 425f575 | 2017-11-15 05:51:26 +0000 | [diff] [blame] | 1384 | _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 1385 | bool empty() const _NOEXCEPT {return __base::size() == 0;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1386 | |
| 1387 | // element access: |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1388 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 8d7c9f1 | 2019-03-14 21:56:57 +0000 | [diff] [blame] | 1389 | reference operator[](size_type __i) _NOEXCEPT; |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1390 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 8d7c9f1 | 2019-03-14 21:56:57 +0000 | [diff] [blame] | 1391 | const_reference operator[](size_type __i) const _NOEXCEPT; |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1392 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1393 | reference at(size_type __i); |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1394 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1395 | const_reference at(size_type __i) const; |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1396 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 05cf669 | 2019-03-19 03:30:07 +0000 | [diff] [blame] | 1397 | reference front() _NOEXCEPT; |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1398 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 05cf669 | 2019-03-19 03:30:07 +0000 | [diff] [blame] | 1399 | const_reference front() const _NOEXCEPT; |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1400 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 05cf669 | 2019-03-19 03:30:07 +0000 | [diff] [blame] | 1401 | reference back() _NOEXCEPT; |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1402 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 05cf669 | 2019-03-19 03:30:07 +0000 | [diff] [blame] | 1403 | const_reference back() const _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1404 | |
| 1405 | // 23.2.2.3 modifiers: |
| 1406 | void push_front(const value_type& __v); |
| 1407 | void push_back(const value_type& __v); |
Eric Fiselier | 212e3f2 | 2017-04-16 03:17:01 +0000 | [diff] [blame] | 1408 | #ifndef _LIBCPP_CXX03_LANG |
Marshall Clow | ea52cc4 | 2017-01-24 23:09:12 +0000 | [diff] [blame] | 1409 | #if _LIBCPP_STD_VER > 14 |
Eric Fiselier | 34ba5b9 | 2016-07-21 03:20:17 +0000 | [diff] [blame] | 1410 | template <class... _Args> reference emplace_front(_Args&&... __args); |
Marshall Clow | ea52cc4 | 2017-01-24 23:09:12 +0000 | [diff] [blame] | 1411 | template <class... _Args> reference emplace_back (_Args&&... __args); |
| 1412 | #else |
| 1413 | template <class... _Args> void emplace_front(_Args&&... __args); |
| 1414 | template <class... _Args> void emplace_back (_Args&&... __args); |
| 1415 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1416 | template <class... _Args> iterator emplace(const_iterator __p, _Args&&... __args); |
Eric Fiselier | 212e3f2 | 2017-04-16 03:17:01 +0000 | [diff] [blame] | 1417 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1418 | void push_front(value_type&& __v); |
| 1419 | void push_back(value_type&& __v); |
| 1420 | iterator insert(const_iterator __p, value_type&& __v); |
Eric Fiselier | 212e3f2 | 2017-04-16 03:17:01 +0000 | [diff] [blame] | 1421 | |
| 1422 | _LIBCPP_INLINE_VISIBILITY |
| 1423 | iterator insert(const_iterator __p, initializer_list<value_type> __il) |
| 1424 | {return insert(__p, __il.begin(), __il.end());} |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 1425 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1426 | iterator insert(const_iterator __p, const value_type& __v); |
| 1427 | iterator insert(const_iterator __p, size_type __n, const value_type& __v); |
| 1428 | template <class _InputIter> |
Marshall Clow | 6b612cf | 2015-01-22 18:33:29 +0000 | [diff] [blame] | 1429 | iterator insert(const_iterator __p, _InputIter __f, _InputIter __l, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 1430 | typename enable_if<__is_cpp17_input_iterator<_InputIter>::value |
| 1431 | &&!__is_cpp17_forward_iterator<_InputIter>::value>::type* = 0); |
Marshall Clow | 6b612cf | 2015-01-22 18:33:29 +0000 | [diff] [blame] | 1432 | template <class _ForwardIterator> |
| 1433 | iterator insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 1434 | typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value |
| 1435 | &&!__is_cpp17_bidirectional_iterator<_ForwardIterator>::value>::type* = 0); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1436 | template <class _BiIter> |
Marshall Clow | 6b612cf | 2015-01-22 18:33:29 +0000 | [diff] [blame] | 1437 | iterator insert(const_iterator __p, _BiIter __f, _BiIter __l, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 1438 | typename enable_if<__is_cpp17_bidirectional_iterator<_BiIter>::value>::type* = 0); |
Eric Fiselier | 212e3f2 | 2017-04-16 03:17:01 +0000 | [diff] [blame] | 1439 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1440 | void pop_front(); |
| 1441 | void pop_back(); |
| 1442 | iterator erase(const_iterator __p); |
| 1443 | iterator erase(const_iterator __f, const_iterator __l); |
| 1444 | |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1445 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | ca2fc22 | 2011-06-02 20:00:14 +0000 | [diff] [blame] | 1446 | void swap(deque& __c) |
Marshall Clow | 8982dcd | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 1447 | #if _LIBCPP_STD_VER >= 14 |
| 1448 | _NOEXCEPT; |
| 1449 | #else |
Howard Hinnant | ca2fc22 | 2011-06-02 20:00:14 +0000 | [diff] [blame] | 1450 | _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || |
| 1451 | __is_nothrow_swappable<allocator_type>::value); |
Marshall Clow | 8982dcd | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 1452 | #endif |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1453 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 1454 | void clear() _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1455 | |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1456 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1457 | bool __invariants() const {return __base::__invariants();} |
Eric Fiselier | 27e0362 | 2019-08-01 23:11:18 +0000 | [diff] [blame] | 1458 | |
Howard Hinnant | dcb73a3 | 2013-06-23 21:17:24 +0000 | [diff] [blame] | 1459 | typedef typename __base::__map_const_pointer __map_const_pointer; |
| 1460 | |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1461 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1462 | static size_type __recommend_blocks(size_type __n) |
| 1463 | { |
| 1464 | return __n / __base::__block_size + (__n % __base::__block_size != 0); |
| 1465 | } |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1466 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1467 | size_type __capacity() const |
| 1468 | { |
| 1469 | return __base::__map_.size() == 0 ? 0 : __base::__map_.size() * __base::__block_size - 1; |
| 1470 | } |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1471 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 27e0362 | 2019-08-01 23:11:18 +0000 | [diff] [blame] | 1472 | size_type __block_count() const |
| 1473 | { |
| 1474 | return __base::__map_.size(); |
| 1475 | } |
| 1476 | |
| 1477 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1478 | size_type __front_spare() const |
| 1479 | { |
| 1480 | return __base::__start_; |
| 1481 | } |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1482 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 27e0362 | 2019-08-01 23:11:18 +0000 | [diff] [blame] | 1483 | size_type __front_spare_blocks() const { |
| 1484 | return __front_spare() / __base::__block_size; |
| 1485 | } |
| 1486 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1487 | size_type __back_spare() const |
| 1488 | { |
| 1489 | return __capacity() - (__base::__start_ + __base::size()); |
| 1490 | } |
Eric Fiselier | 27e0362 | 2019-08-01 23:11:18 +0000 | [diff] [blame] | 1491 | _LIBCPP_INLINE_VISIBILITY |
| 1492 | size_type __back_spare_blocks() const { |
| 1493 | return __back_spare() / __base::__block_size; |
| 1494 | } |
| 1495 | |
| 1496 | private: |
| 1497 | _LIBCPP_INLINE_VISIBILITY |
| 1498 | bool __maybe_remove_front_spare(bool __keep_one = true) { |
| 1499 | if (__front_spare_blocks() >= 2 || (!__keep_one && __front_spare_blocks())) { |
| 1500 | __alloc_traits::deallocate(__base::__alloc(), __base::__map_.front(), |
| 1501 | __base::__block_size); |
| 1502 | __base::__map_.pop_front(); |
| 1503 | __base::__start_ -= __base::__block_size; |
| 1504 | return true; |
| 1505 | } |
| 1506 | return false; |
| 1507 | } |
| 1508 | |
| 1509 | _LIBCPP_INLINE_VISIBILITY |
| 1510 | bool __maybe_remove_back_spare(bool __keep_one = true) { |
| 1511 | if (__back_spare_blocks() >= 2 || (!__keep_one && __back_spare_blocks())) { |
| 1512 | __alloc_traits::deallocate(__base::__alloc(), __base::__map_.back(), |
| 1513 | __base::__block_size); |
| 1514 | __base::__map_.pop_back(); |
| 1515 | return true; |
| 1516 | } |
| 1517 | return false; |
| 1518 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1519 | |
| 1520 | template <class _InpIter> |
| 1521 | void __append(_InpIter __f, _InpIter __l, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 1522 | typename enable_if<__is_cpp17_input_iterator<_InpIter>::value && |
| 1523 | !__is_cpp17_forward_iterator<_InpIter>::value>::type* = 0); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1524 | template <class _ForIter> |
| 1525 | void __append(_ForIter __f, _ForIter __l, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 1526 | typename enable_if<__is_cpp17_forward_iterator<_ForIter>::value>::type* = 0); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1527 | void __append(size_type __n); |
| 1528 | void __append(size_type __n, const value_type& __v); |
| 1529 | void __erase_to_end(const_iterator __f); |
| 1530 | void __add_front_capacity(); |
| 1531 | void __add_front_capacity(size_type __n); |
| 1532 | void __add_back_capacity(); |
| 1533 | void __add_back_capacity(size_type __n); |
| 1534 | iterator __move_and_check(iterator __f, iterator __l, iterator __r, |
| 1535 | const_pointer& __vt); |
| 1536 | iterator __move_backward_and_check(iterator __f, iterator __l, iterator __r, |
| 1537 | const_pointer& __vt); |
| 1538 | void __move_construct_and_check(iterator __f, iterator __l, |
| 1539 | iterator __r, const_pointer& __vt); |
| 1540 | void __move_construct_backward_and_check(iterator __f, iterator __l, |
| 1541 | iterator __r, const_pointer& __vt); |
| 1542 | |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1543 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1544 | void __copy_assign_alloc(const deque& __c) |
| 1545 | {__copy_assign_alloc(__c, integral_constant<bool, |
| 1546 | __alloc_traits::propagate_on_container_copy_assignment::value>());} |
| 1547 | |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1548 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1549 | void __copy_assign_alloc(const deque& __c, true_type) |
| 1550 | { |
| 1551 | if (__base::__alloc() != __c.__alloc()) |
| 1552 | { |
| 1553 | clear(); |
| 1554 | shrink_to_fit(); |
| 1555 | } |
| 1556 | __base::__alloc() = __c.__alloc(); |
| 1557 | __base::__map_.__alloc() = __c.__map_.__alloc(); |
| 1558 | } |
| 1559 | |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1560 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 1561 | void __copy_assign_alloc(const deque&, false_type) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1562 | {} |
| 1563 | |
Howard Hinnant | 4931e09 | 2011-06-02 21:38:57 +0000 | [diff] [blame] | 1564 | void __move_assign(deque& __c, true_type) |
| 1565 | _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1566 | void __move_assign(deque& __c, false_type); |
| 1567 | }; |
| 1568 | |
Louis Dionne | d59f8a5 | 2021-08-17 11:59:07 -0400 | [diff] [blame] | 1569 | #if _LIBCPP_STD_VER >= 17 |
Marshall Clow | 4cc3a45 | 2018-05-18 23:44:13 +0000 | [diff] [blame] | 1570 | template<class _InputIterator, |
Arthur O'Dwyer | 5622676 | 2021-03-03 23:02:20 -0500 | [diff] [blame] | 1571 | class _Alloc = allocator<__iter_value_type<_InputIterator>>, |
Konstantin Varlamov | 53b543c | 2021-11-09 09:21:02 -0800 | [diff] [blame] | 1572 | class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>, |
Louis Dionne | 2554716 | 2021-08-17 12:26:09 -0400 | [diff] [blame] | 1573 | class = enable_if_t<__is_allocator<_Alloc>::value> |
Marshall Clow | 4cc3a45 | 2018-05-18 23:44:13 +0000 | [diff] [blame] | 1574 | > |
| 1575 | deque(_InputIterator, _InputIterator) |
Arthur O'Dwyer | 5622676 | 2021-03-03 23:02:20 -0500 | [diff] [blame] | 1576 | -> deque<__iter_value_type<_InputIterator>, _Alloc>; |
Marshall Clow | 4cc3a45 | 2018-05-18 23:44:13 +0000 | [diff] [blame] | 1577 | |
| 1578 | template<class _InputIterator, |
| 1579 | class _Alloc, |
Konstantin Varlamov | 53b543c | 2021-11-09 09:21:02 -0800 | [diff] [blame] | 1580 | class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>, |
Louis Dionne | 2554716 | 2021-08-17 12:26:09 -0400 | [diff] [blame] | 1581 | class = enable_if_t<__is_allocator<_Alloc>::value> |
Marshall Clow | 4cc3a45 | 2018-05-18 23:44:13 +0000 | [diff] [blame] | 1582 | > |
| 1583 | deque(_InputIterator, _InputIterator, _Alloc) |
Arthur O'Dwyer | 5622676 | 2021-03-03 23:02:20 -0500 | [diff] [blame] | 1584 | -> deque<__iter_value_type<_InputIterator>, _Alloc>; |
Marshall Clow | 4cc3a45 | 2018-05-18 23:44:13 +0000 | [diff] [blame] | 1585 | #endif |
| 1586 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1587 | template <class _Tp, class _Allocator> |
| 1588 | deque<_Tp, _Allocator>::deque(size_type __n) |
| 1589 | { |
| 1590 | if (__n > 0) |
| 1591 | __append(__n); |
| 1592 | } |
| 1593 | |
Marshall Clow | bc4fcb4 | 2013-09-07 16:16:19 +0000 | [diff] [blame] | 1594 | #if _LIBCPP_STD_VER > 11 |
| 1595 | template <class _Tp, class _Allocator> |
| 1596 | deque<_Tp, _Allocator>::deque(size_type __n, const _Allocator& __a) |
| 1597 | : __base(__a) |
| 1598 | { |
| 1599 | if (__n > 0) |
| 1600 | __append(__n); |
| 1601 | } |
| 1602 | #endif |
| 1603 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1604 | template <class _Tp, class _Allocator> |
| 1605 | deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v) |
| 1606 | { |
| 1607 | if (__n > 0) |
| 1608 | __append(__n, __v); |
| 1609 | } |
| 1610 | |
| 1611 | template <class _Tp, class _Allocator> |
| 1612 | deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v, const allocator_type& __a) |
| 1613 | : __base(__a) |
| 1614 | { |
| 1615 | if (__n > 0) |
| 1616 | __append(__n, __v); |
| 1617 | } |
| 1618 | |
| 1619 | template <class _Tp, class _Allocator> |
| 1620 | template <class _InputIter> |
| 1621 | deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 1622 | typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type*) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1623 | { |
| 1624 | __append(__f, __l); |
| 1625 | } |
| 1626 | |
| 1627 | template <class _Tp, class _Allocator> |
| 1628 | template <class _InputIter> |
| 1629 | deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, const allocator_type& __a, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 1630 | typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type*) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1631 | : __base(__a) |
| 1632 | { |
| 1633 | __append(__f, __l); |
| 1634 | } |
| 1635 | |
| 1636 | template <class _Tp, class _Allocator> |
| 1637 | deque<_Tp, _Allocator>::deque(const deque& __c) |
| 1638 | : __base(__alloc_traits::select_on_container_copy_construction(__c.__alloc())) |
| 1639 | { |
| 1640 | __append(__c.begin(), __c.end()); |
| 1641 | } |
| 1642 | |
| 1643 | template <class _Tp, class _Allocator> |
Arthur O'Dwyer | 9b9662f | 2021-03-01 17:08:24 -0500 | [diff] [blame] | 1644 | deque<_Tp, _Allocator>::deque(const deque& __c, const __identity_t<allocator_type>& __a) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1645 | : __base(__a) |
| 1646 | { |
| 1647 | __append(__c.begin(), __c.end()); |
| 1648 | } |
| 1649 | |
Eric Fiselier | 212e3f2 | 2017-04-16 03:17:01 +0000 | [diff] [blame] | 1650 | template <class _Tp, class _Allocator> |
| 1651 | deque<_Tp, _Allocator>& |
| 1652 | deque<_Tp, _Allocator>::operator=(const deque& __c) |
| 1653 | { |
Mark de Wever | 357a1fc | 2021-09-28 19:15:18 +0200 | [diff] [blame] | 1654 | if (this != _VSTD::addressof(__c)) |
Eric Fiselier | 212e3f2 | 2017-04-16 03:17:01 +0000 | [diff] [blame] | 1655 | { |
| 1656 | __copy_assign_alloc(__c); |
| 1657 | assign(__c.begin(), __c.end()); |
| 1658 | } |
| 1659 | return *this; |
| 1660 | } |
| 1661 | |
| 1662 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 3371179 | 2011-08-12 21:56:02 +0000 | [diff] [blame] | 1663 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1664 | template <class _Tp, class _Allocator> |
| 1665 | deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il) |
| 1666 | { |
| 1667 | __append(__il.begin(), __il.end()); |
| 1668 | } |
| 1669 | |
| 1670 | template <class _Tp, class _Allocator> |
| 1671 | deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il, const allocator_type& __a) |
| 1672 | : __base(__a) |
| 1673 | { |
| 1674 | __append(__il.begin(), __il.end()); |
| 1675 | } |
| 1676 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1677 | template <class _Tp, class _Allocator> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1678 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1679 | deque<_Tp, _Allocator>::deque(deque&& __c) |
Howard Hinnant | ca2fc22 | 2011-06-02 20:00:14 +0000 | [diff] [blame] | 1680 | _NOEXCEPT_(is_nothrow_move_constructible<__base>::value) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1681 | : __base(_VSTD::move(__c)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1682 | { |
| 1683 | } |
| 1684 | |
| 1685 | template <class _Tp, class _Allocator> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1686 | inline |
Arthur O'Dwyer | 9b9662f | 2021-03-01 17:08:24 -0500 | [diff] [blame] | 1687 | deque<_Tp, _Allocator>::deque(deque&& __c, const __identity_t<allocator_type>& __a) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1688 | : __base(_VSTD::move(__c), __a) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1689 | { |
| 1690 | if (__a != __c.__alloc()) |
| 1691 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1692 | typedef move_iterator<iterator> _Ip; |
| 1693 | assign(_Ip(__c.begin()), _Ip(__c.end())); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | template <class _Tp, class _Allocator> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1698 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1699 | deque<_Tp, _Allocator>& |
| 1700 | deque<_Tp, _Allocator>::operator=(deque&& __c) |
Howard Hinnant | 4931e09 | 2011-06-02 21:38:57 +0000 | [diff] [blame] | 1701 | _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value && |
| 1702 | is_nothrow_move_assignable<allocator_type>::value) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1703 | { |
| 1704 | __move_assign(__c, integral_constant<bool, |
| 1705 | __alloc_traits::propagate_on_container_move_assignment::value>()); |
| 1706 | return *this; |
| 1707 | } |
| 1708 | |
| 1709 | template <class _Tp, class _Allocator> |
| 1710 | void |
| 1711 | deque<_Tp, _Allocator>::__move_assign(deque& __c, false_type) |
| 1712 | { |
| 1713 | if (__base::__alloc() != __c.__alloc()) |
| 1714 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1715 | typedef move_iterator<iterator> _Ip; |
| 1716 | assign(_Ip(__c.begin()), _Ip(__c.end())); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1717 | } |
| 1718 | else |
| 1719 | __move_assign(__c, true_type()); |
| 1720 | } |
| 1721 | |
| 1722 | template <class _Tp, class _Allocator> |
| 1723 | void |
| 1724 | deque<_Tp, _Allocator>::__move_assign(deque& __c, true_type) |
Howard Hinnant | 4931e09 | 2011-06-02 21:38:57 +0000 | [diff] [blame] | 1725 | _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1726 | { |
| 1727 | clear(); |
| 1728 | shrink_to_fit(); |
| 1729 | __base::__move_assign(__c); |
| 1730 | } |
| 1731 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 1732 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1733 | |
| 1734 | template <class _Tp, class _Allocator> |
| 1735 | template <class _InputIter> |
| 1736 | void |
| 1737 | deque<_Tp, _Allocator>::assign(_InputIter __f, _InputIter __l, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 1738 | typename enable_if<__is_cpp17_input_iterator<_InputIter>::value && |
| 1739 | !__is_cpp17_random_access_iterator<_InputIter>::value>::type*) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1740 | { |
| 1741 | iterator __i = __base::begin(); |
| 1742 | iterator __e = __base::end(); |
Eric Fiselier | a09a3b4 | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 1743 | for (; __f != __l && __i != __e; ++__f, (void) ++__i) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1744 | *__i = *__f; |
| 1745 | if (__f != __l) |
| 1746 | __append(__f, __l); |
| 1747 | else |
| 1748 | __erase_to_end(__i); |
| 1749 | } |
| 1750 | |
| 1751 | template <class _Tp, class _Allocator> |
| 1752 | template <class _RAIter> |
| 1753 | void |
| 1754 | deque<_Tp, _Allocator>::assign(_RAIter __f, _RAIter __l, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 1755 | typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1756 | { |
| 1757 | if (static_cast<size_type>(__l - __f) > __base::size()) |
| 1758 | { |
| 1759 | _RAIter __m = __f + __base::size(); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1760 | _VSTD::copy(__f, __m, __base::begin()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1761 | __append(__m, __l); |
| 1762 | } |
| 1763 | else |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1764 | __erase_to_end(_VSTD::copy(__f, __l, __base::begin())); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1765 | } |
| 1766 | |
| 1767 | template <class _Tp, class _Allocator> |
| 1768 | void |
| 1769 | deque<_Tp, _Allocator>::assign(size_type __n, const value_type& __v) |
| 1770 | { |
| 1771 | if (__n > __base::size()) |
| 1772 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1773 | _VSTD::fill_n(__base::begin(), __base::size(), __v); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1774 | __n -= __base::size(); |
| 1775 | __append(__n, __v); |
| 1776 | } |
| 1777 | else |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1778 | __erase_to_end(_VSTD::fill_n(__base::begin(), __n, __v)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | template <class _Tp, class _Allocator> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1782 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1783 | _Allocator |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 1784 | deque<_Tp, _Allocator>::get_allocator() const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1785 | { |
| 1786 | return __base::__alloc(); |
| 1787 | } |
| 1788 | |
| 1789 | template <class _Tp, class _Allocator> |
| 1790 | void |
| 1791 | deque<_Tp, _Allocator>::resize(size_type __n) |
| 1792 | { |
| 1793 | if (__n > __base::size()) |
| 1794 | __append(__n - __base::size()); |
| 1795 | else if (__n < __base::size()) |
| 1796 | __erase_to_end(__base::begin() + __n); |
| 1797 | } |
| 1798 | |
| 1799 | template <class _Tp, class _Allocator> |
| 1800 | void |
| 1801 | deque<_Tp, _Allocator>::resize(size_type __n, const value_type& __v) |
| 1802 | { |
| 1803 | if (__n > __base::size()) |
| 1804 | __append(__n - __base::size(), __v); |
| 1805 | else if (__n < __base::size()) |
| 1806 | __erase_to_end(__base::begin() + __n); |
| 1807 | } |
| 1808 | |
| 1809 | template <class _Tp, class _Allocator> |
| 1810 | void |
Howard Hinnant | 4931e09 | 2011-06-02 21:38:57 +0000 | [diff] [blame] | 1811 | deque<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1812 | { |
| 1813 | allocator_type& __a = __base::__alloc(); |
| 1814 | if (empty()) |
| 1815 | { |
| 1816 | while (__base::__map_.size() > 0) |
| 1817 | { |
| 1818 | __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size); |
| 1819 | __base::__map_.pop_back(); |
| 1820 | } |
| 1821 | __base::__start_ = 0; |
| 1822 | } |
| 1823 | else |
| 1824 | { |
Eric Fiselier | 27e0362 | 2019-08-01 23:11:18 +0000 | [diff] [blame] | 1825 | __maybe_remove_front_spare(/*__keep_one=*/false); |
| 1826 | __maybe_remove_back_spare(/*__keep_one=*/false); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1827 | } |
| 1828 | __base::__map_.shrink_to_fit(); |
| 1829 | } |
| 1830 | |
| 1831 | template <class _Tp, class _Allocator> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1832 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1833 | typename deque<_Tp, _Allocator>::reference |
Marshall Clow | 8d7c9f1 | 2019-03-14 21:56:57 +0000 | [diff] [blame] | 1834 | deque<_Tp, _Allocator>::operator[](size_type __i) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1835 | { |
| 1836 | size_type __p = __base::__start_ + __i; |
| 1837 | return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size); |
| 1838 | } |
| 1839 | |
| 1840 | template <class _Tp, class _Allocator> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1841 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1842 | typename deque<_Tp, _Allocator>::const_reference |
Marshall Clow | 8d7c9f1 | 2019-03-14 21:56:57 +0000 | [diff] [blame] | 1843 | deque<_Tp, _Allocator>::operator[](size_type __i) const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1844 | { |
| 1845 | size_type __p = __base::__start_ + __i; |
| 1846 | return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size); |
| 1847 | } |
| 1848 | |
| 1849 | template <class _Tp, class _Allocator> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1850 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1851 | typename deque<_Tp, _Allocator>::reference |
| 1852 | deque<_Tp, _Allocator>::at(size_type __i) |
| 1853 | { |
| 1854 | if (__i >= __base::size()) |
Louis Dionne | f2cd0a1 | 2021-08-19 12:15:31 -0400 | [diff] [blame] | 1855 | _VSTD::__throw_out_of_range("deque"); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1856 | size_type __p = __base::__start_ + __i; |
| 1857 | return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size); |
| 1858 | } |
| 1859 | |
| 1860 | template <class _Tp, class _Allocator> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1861 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1862 | typename deque<_Tp, _Allocator>::const_reference |
| 1863 | deque<_Tp, _Allocator>::at(size_type __i) const |
| 1864 | { |
| 1865 | if (__i >= __base::size()) |
Louis Dionne | f2cd0a1 | 2021-08-19 12:15:31 -0400 | [diff] [blame] | 1866 | _VSTD::__throw_out_of_range("deque"); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1867 | size_type __p = __base::__start_ + __i; |
| 1868 | return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size); |
| 1869 | } |
| 1870 | |
| 1871 | template <class _Tp, class _Allocator> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1872 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1873 | typename deque<_Tp, _Allocator>::reference |
Marshall Clow | 05cf669 | 2019-03-19 03:30:07 +0000 | [diff] [blame] | 1874 | deque<_Tp, _Allocator>::front() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1875 | { |
| 1876 | return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size) |
| 1877 | + __base::__start_ % __base::__block_size); |
| 1878 | } |
| 1879 | |
| 1880 | template <class _Tp, class _Allocator> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1881 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1882 | typename deque<_Tp, _Allocator>::const_reference |
Marshall Clow | 05cf669 | 2019-03-19 03:30:07 +0000 | [diff] [blame] | 1883 | deque<_Tp, _Allocator>::front() const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1884 | { |
| 1885 | return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size) |
| 1886 | + __base::__start_ % __base::__block_size); |
| 1887 | } |
| 1888 | |
| 1889 | template <class _Tp, class _Allocator> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1890 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1891 | typename deque<_Tp, _Allocator>::reference |
Marshall Clow | 05cf669 | 2019-03-19 03:30:07 +0000 | [diff] [blame] | 1892 | deque<_Tp, _Allocator>::back() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1893 | { |
| 1894 | size_type __p = __base::size() + __base::__start_ - 1; |
| 1895 | return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size); |
| 1896 | } |
| 1897 | |
| 1898 | template <class _Tp, class _Allocator> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1899 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1900 | typename deque<_Tp, _Allocator>::const_reference |
Marshall Clow | 05cf669 | 2019-03-19 03:30:07 +0000 | [diff] [blame] | 1901 | deque<_Tp, _Allocator>::back() const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1902 | { |
| 1903 | size_type __p = __base::size() + __base::__start_ - 1; |
| 1904 | return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size); |
| 1905 | } |
| 1906 | |
| 1907 | template <class _Tp, class _Allocator> |
| 1908 | void |
| 1909 | deque<_Tp, _Allocator>::push_back(const value_type& __v) |
| 1910 | { |
| 1911 | allocator_type& __a = __base::__alloc(); |
| 1912 | if (__back_spare() == 0) |
| 1913 | __add_back_capacity(); |
| 1914 | // __back_spare() >= 1 |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1915 | __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1916 | ++__base::size(); |
| 1917 | } |
| 1918 | |
Eric Fiselier | 212e3f2 | 2017-04-16 03:17:01 +0000 | [diff] [blame] | 1919 | template <class _Tp, class _Allocator> |
| 1920 | void |
| 1921 | deque<_Tp, _Allocator>::push_front(const value_type& __v) |
| 1922 | { |
| 1923 | allocator_type& __a = __base::__alloc(); |
| 1924 | if (__front_spare() == 0) |
| 1925 | __add_front_capacity(); |
| 1926 | // __front_spare() >= 1 |
| 1927 | __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v); |
| 1928 | --__base::__start_; |
| 1929 | ++__base::size(); |
| 1930 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1931 | |
Eric Fiselier | 212e3f2 | 2017-04-16 03:17:01 +0000 | [diff] [blame] | 1932 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1933 | template <class _Tp, class _Allocator> |
| 1934 | void |
| 1935 | deque<_Tp, _Allocator>::push_back(value_type&& __v) |
| 1936 | { |
| 1937 | allocator_type& __a = __base::__alloc(); |
| 1938 | if (__back_spare() == 0) |
| 1939 | __add_back_capacity(); |
| 1940 | // __back_spare() >= 1 |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1941 | __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1942 | ++__base::size(); |
| 1943 | } |
| 1944 | |
| 1945 | template <class _Tp, class _Allocator> |
| 1946 | template <class... _Args> |
Marshall Clow | ea52cc4 | 2017-01-24 23:09:12 +0000 | [diff] [blame] | 1947 | #if _LIBCPP_STD_VER > 14 |
Eric Fiselier | 34ba5b9 | 2016-07-21 03:20:17 +0000 | [diff] [blame] | 1948 | typename deque<_Tp, _Allocator>::reference |
Marshall Clow | ea52cc4 | 2017-01-24 23:09:12 +0000 | [diff] [blame] | 1949 | #else |
| 1950 | void |
| 1951 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1952 | deque<_Tp, _Allocator>::emplace_back(_Args&&... __args) |
| 1953 | { |
| 1954 | allocator_type& __a = __base::__alloc(); |
| 1955 | if (__back_spare() == 0) |
| 1956 | __add_back_capacity(); |
| 1957 | // __back_spare() >= 1 |
Eric Fiselier | 34ba5b9 | 2016-07-21 03:20:17 +0000 | [diff] [blame] | 1958 | __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), |
| 1959 | _VSTD::forward<_Args>(__args)...); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1960 | ++__base::size(); |
Marshall Clow | ea52cc4 | 2017-01-24 23:09:12 +0000 | [diff] [blame] | 1961 | #if _LIBCPP_STD_VER > 14 |
Eric Fiselier | 34ba5b9 | 2016-07-21 03:20:17 +0000 | [diff] [blame] | 1962 | return *--__base::end(); |
Marshall Clow | ea52cc4 | 2017-01-24 23:09:12 +0000 | [diff] [blame] | 1963 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1964 | } |
| 1965 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1966 | template <class _Tp, class _Allocator> |
| 1967 | void |
| 1968 | deque<_Tp, _Allocator>::push_front(value_type&& __v) |
| 1969 | { |
| 1970 | allocator_type& __a = __base::__alloc(); |
| 1971 | if (__front_spare() == 0) |
| 1972 | __add_front_capacity(); |
| 1973 | // __front_spare() >= 1 |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1974 | __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1975 | --__base::__start_; |
| 1976 | ++__base::size(); |
| 1977 | } |
| 1978 | |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1979 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1980 | template <class _Tp, class _Allocator> |
| 1981 | template <class... _Args> |
Marshall Clow | ea52cc4 | 2017-01-24 23:09:12 +0000 | [diff] [blame] | 1982 | #if _LIBCPP_STD_VER > 14 |
Eric Fiselier | 34ba5b9 | 2016-07-21 03:20:17 +0000 | [diff] [blame] | 1983 | typename deque<_Tp, _Allocator>::reference |
Marshall Clow | ea52cc4 | 2017-01-24 23:09:12 +0000 | [diff] [blame] | 1984 | #else |
| 1985 | void |
| 1986 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1987 | deque<_Tp, _Allocator>::emplace_front(_Args&&... __args) |
| 1988 | { |
| 1989 | allocator_type& __a = __base::__alloc(); |
| 1990 | if (__front_spare() == 0) |
| 1991 | __add_front_capacity(); |
| 1992 | // __front_spare() >= 1 |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1993 | __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1994 | --__base::__start_; |
| 1995 | ++__base::size(); |
Marshall Clow | ea52cc4 | 2017-01-24 23:09:12 +0000 | [diff] [blame] | 1996 | #if _LIBCPP_STD_VER > 14 |
Eric Fiselier | 34ba5b9 | 2016-07-21 03:20:17 +0000 | [diff] [blame] | 1997 | return *__base::begin(); |
Marshall Clow | ea52cc4 | 2017-01-24 23:09:12 +0000 | [diff] [blame] | 1998 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
Eric Fiselier | 212e3f2 | 2017-04-16 03:17:01 +0000 | [diff] [blame] | 2001 | template <class _Tp, class _Allocator> |
| 2002 | typename deque<_Tp, _Allocator>::iterator |
| 2003 | deque<_Tp, _Allocator>::insert(const_iterator __p, value_type&& __v) |
| 2004 | { |
| 2005 | size_type __pos = __p - __base::begin(); |
| 2006 | size_type __to_end = __base::size() - __pos; |
| 2007 | allocator_type& __a = __base::__alloc(); |
| 2008 | if (__pos < __to_end) |
| 2009 | { // insert by shifting things backward |
| 2010 | if (__front_spare() == 0) |
| 2011 | __add_front_capacity(); |
| 2012 | // __front_spare() >= 1 |
| 2013 | if (__pos == 0) |
| 2014 | { |
| 2015 | __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v)); |
| 2016 | --__base::__start_; |
| 2017 | ++__base::size(); |
| 2018 | } |
| 2019 | else |
| 2020 | { |
| 2021 | iterator __b = __base::begin(); |
| 2022 | iterator __bm1 = _VSTD::prev(__b); |
| 2023 | __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b)); |
| 2024 | --__base::__start_; |
| 2025 | ++__base::size(); |
| 2026 | if (__pos > 1) |
| 2027 | __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b); |
| 2028 | *__b = _VSTD::move(__v); |
| 2029 | } |
| 2030 | } |
| 2031 | else |
| 2032 | { // insert by shifting things forward |
| 2033 | if (__back_spare() == 0) |
| 2034 | __add_back_capacity(); |
| 2035 | // __back_capacity >= 1 |
| 2036 | size_type __de = __base::size() - __pos; |
| 2037 | if (__de == 0) |
| 2038 | { |
| 2039 | __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v)); |
| 2040 | ++__base::size(); |
| 2041 | } |
| 2042 | else |
| 2043 | { |
| 2044 | iterator __e = __base::end(); |
| 2045 | iterator __em1 = _VSTD::prev(__e); |
| 2046 | __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1)); |
| 2047 | ++__base::size(); |
| 2048 | if (__de > 1) |
| 2049 | __e = _VSTD::move_backward(__e - __de, __em1, __e); |
| 2050 | *--__e = _VSTD::move(__v); |
| 2051 | } |
| 2052 | } |
| 2053 | return __base::begin() + __pos; |
| 2054 | } |
| 2055 | |
| 2056 | template <class _Tp, class _Allocator> |
| 2057 | template <class... _Args> |
| 2058 | typename deque<_Tp, _Allocator>::iterator |
| 2059 | deque<_Tp, _Allocator>::emplace(const_iterator __p, _Args&&... __args) |
| 2060 | { |
| 2061 | size_type __pos = __p - __base::begin(); |
| 2062 | size_type __to_end = __base::size() - __pos; |
| 2063 | allocator_type& __a = __base::__alloc(); |
| 2064 | if (__pos < __to_end) |
| 2065 | { // insert by shifting things backward |
| 2066 | if (__front_spare() == 0) |
| 2067 | __add_front_capacity(); |
| 2068 | // __front_spare() >= 1 |
| 2069 | if (__pos == 0) |
| 2070 | { |
| 2071 | __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...); |
| 2072 | --__base::__start_; |
| 2073 | ++__base::size(); |
| 2074 | } |
| 2075 | else |
| 2076 | { |
| 2077 | __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...); |
| 2078 | iterator __b = __base::begin(); |
| 2079 | iterator __bm1 = _VSTD::prev(__b); |
| 2080 | __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b)); |
| 2081 | --__base::__start_; |
| 2082 | ++__base::size(); |
| 2083 | if (__pos > 1) |
| 2084 | __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b); |
| 2085 | *__b = _VSTD::move(__tmp.get()); |
| 2086 | } |
| 2087 | } |
| 2088 | else |
| 2089 | { // insert by shifting things forward |
| 2090 | if (__back_spare() == 0) |
| 2091 | __add_back_capacity(); |
| 2092 | // __back_capacity >= 1 |
| 2093 | size_type __de = __base::size() - __pos; |
| 2094 | if (__de == 0) |
| 2095 | { |
| 2096 | __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::forward<_Args>(__args)...); |
| 2097 | ++__base::size(); |
| 2098 | } |
| 2099 | else |
| 2100 | { |
| 2101 | __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...); |
| 2102 | iterator __e = __base::end(); |
| 2103 | iterator __em1 = _VSTD::prev(__e); |
| 2104 | __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1)); |
| 2105 | ++__base::size(); |
| 2106 | if (__de > 1) |
| 2107 | __e = _VSTD::move_backward(__e - __de, __em1, __e); |
| 2108 | *--__e = _VSTD::move(__tmp.get()); |
| 2109 | } |
| 2110 | } |
| 2111 | return __base::begin() + __pos; |
| 2112 | } |
| 2113 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 2114 | #endif // _LIBCPP_CXX03_LANG |
Eric Fiselier | 212e3f2 | 2017-04-16 03:17:01 +0000 | [diff] [blame] | 2115 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2116 | |
| 2117 | template <class _Tp, class _Allocator> |
| 2118 | typename deque<_Tp, _Allocator>::iterator |
| 2119 | deque<_Tp, _Allocator>::insert(const_iterator __p, const value_type& __v) |
| 2120 | { |
| 2121 | size_type __pos = __p - __base::begin(); |
| 2122 | size_type __to_end = __base::size() - __pos; |
| 2123 | allocator_type& __a = __base::__alloc(); |
| 2124 | if (__pos < __to_end) |
| 2125 | { // insert by shifting things backward |
| 2126 | if (__front_spare() == 0) |
| 2127 | __add_front_capacity(); |
| 2128 | // __front_spare() >= 1 |
| 2129 | if (__pos == 0) |
| 2130 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2131 | __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2132 | --__base::__start_; |
| 2133 | ++__base::size(); |
| 2134 | } |
| 2135 | else |
| 2136 | { |
| 2137 | const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v); |
| 2138 | iterator __b = __base::begin(); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2139 | iterator __bm1 = _VSTD::prev(__b); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2140 | if (__vt == pointer_traits<const_pointer>::pointer_to(*__b)) |
| 2141 | __vt = pointer_traits<const_pointer>::pointer_to(*__bm1); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2142 | __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2143 | --__base::__start_; |
| 2144 | ++__base::size(); |
| 2145 | if (__pos > 1) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2146 | __b = __move_and_check(_VSTD::next(__b), __b + __pos, __b, __vt); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2147 | *__b = *__vt; |
| 2148 | } |
| 2149 | } |
| 2150 | else |
| 2151 | { // insert by shifting things forward |
| 2152 | if (__back_spare() == 0) |
| 2153 | __add_back_capacity(); |
| 2154 | // __back_capacity >= 1 |
| 2155 | size_type __de = __base::size() - __pos; |
| 2156 | if (__de == 0) |
| 2157 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2158 | __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2159 | ++__base::size(); |
| 2160 | } |
| 2161 | else |
| 2162 | { |
| 2163 | const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v); |
| 2164 | iterator __e = __base::end(); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2165 | iterator __em1 = _VSTD::prev(__e); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2166 | if (__vt == pointer_traits<const_pointer>::pointer_to(*__em1)) |
| 2167 | __vt = pointer_traits<const_pointer>::pointer_to(*__e); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2168 | __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2169 | ++__base::size(); |
| 2170 | if (__de > 1) |
| 2171 | __e = __move_backward_and_check(__e - __de, __em1, __e, __vt); |
| 2172 | *--__e = *__vt; |
| 2173 | } |
| 2174 | } |
| 2175 | return __base::begin() + __pos; |
| 2176 | } |
| 2177 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2178 | template <class _Tp, class _Allocator> |
| 2179 | typename deque<_Tp, _Allocator>::iterator |
| 2180 | deque<_Tp, _Allocator>::insert(const_iterator __p, size_type __n, const value_type& __v) |
| 2181 | { |
| 2182 | size_type __pos = __p - __base::begin(); |
| 2183 | size_type __to_end = __base::size() - __pos; |
| 2184 | allocator_type& __a = __base::__alloc(); |
| 2185 | if (__pos < __to_end) |
| 2186 | { // insert by shifting things backward |
| 2187 | if (__n > __front_spare()) |
| 2188 | __add_front_capacity(__n - __front_spare()); |
| 2189 | // __n <= __front_spare() |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2190 | iterator __old_begin = __base::begin(); |
| 2191 | iterator __i = __old_begin; |
| 2192 | if (__n > __pos) |
| 2193 | { |
| 2194 | for (size_type __m = __n - __pos; __m; --__m, --__base::__start_, ++__base::size()) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2195 | __alloc_traits::construct(__a, _VSTD::addressof(*--__i), __v); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2196 | __n = __pos; |
| 2197 | } |
| 2198 | if (__n > 0) |
| 2199 | { |
| 2200 | const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v); |
| 2201 | iterator __obn = __old_begin + __n; |
| 2202 | __move_construct_backward_and_check(__old_begin, __obn, __i, __vt); |
| 2203 | if (__n < __pos) |
| 2204 | __old_begin = __move_and_check(__obn, __old_begin + __pos, __old_begin, __vt); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2205 | _VSTD::fill_n(__old_begin, __n, *__vt); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2206 | } |
| 2207 | } |
| 2208 | else |
| 2209 | { // insert by shifting things forward |
| 2210 | size_type __back_capacity = __back_spare(); |
| 2211 | if (__n > __back_capacity) |
| 2212 | __add_back_capacity(__n - __back_capacity); |
| 2213 | // __n <= __back_capacity |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2214 | iterator __old_end = __base::end(); |
| 2215 | iterator __i = __old_end; |
| 2216 | size_type __de = __base::size() - __pos; |
| 2217 | if (__n > __de) |
| 2218 | { |
Arthur O'Dwyer | 80dbcbe | 2021-09-07 21:35:37 -0400 | [diff] [blame] | 2219 | for (size_type __m = __n - __de; __m; --__m, (void) ++__i, ++__base::size()) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2220 | __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2221 | __n = __de; |
| 2222 | } |
| 2223 | if (__n > 0) |
| 2224 | { |
| 2225 | const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v); |
| 2226 | iterator __oen = __old_end - __n; |
| 2227 | __move_construct_and_check(__oen, __old_end, __i, __vt); |
| 2228 | if (__n < __de) |
| 2229 | __old_end = __move_backward_and_check(__old_end - __de, __oen, __old_end, __vt); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2230 | _VSTD::fill_n(__old_end - __n, __n, *__vt); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2231 | } |
| 2232 | } |
| 2233 | return __base::begin() + __pos; |
| 2234 | } |
| 2235 | |
| 2236 | template <class _Tp, class _Allocator> |
| 2237 | template <class _InputIter> |
| 2238 | typename deque<_Tp, _Allocator>::iterator |
| 2239 | deque<_Tp, _Allocator>::insert(const_iterator __p, _InputIter __f, _InputIter __l, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 2240 | typename enable_if<__is_cpp17_input_iterator<_InputIter>::value |
| 2241 | &&!__is_cpp17_forward_iterator<_InputIter>::value>::type*) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2242 | { |
| 2243 | __split_buffer<value_type, allocator_type&> __buf(__base::__alloc()); |
| 2244 | __buf.__construct_at_end(__f, __l); |
| 2245 | typedef typename __split_buffer<value_type, allocator_type&>::iterator __bi; |
| 2246 | return insert(__p, move_iterator<__bi>(__buf.begin()), move_iterator<__bi>(__buf.end())); |
| 2247 | } |
| 2248 | |
| 2249 | template <class _Tp, class _Allocator> |
Marshall Clow | 6b612cf | 2015-01-22 18:33:29 +0000 | [diff] [blame] | 2250 | template <class _ForwardIterator> |
| 2251 | typename deque<_Tp, _Allocator>::iterator |
| 2252 | deque<_Tp, _Allocator>::insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 2253 | typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value |
| 2254 | &&!__is_cpp17_bidirectional_iterator<_ForwardIterator>::value>::type*) |
Marshall Clow | 6b612cf | 2015-01-22 18:33:29 +0000 | [diff] [blame] | 2255 | { |
| 2256 | size_type __n = _VSTD::distance(__f, __l); |
| 2257 | __split_buffer<value_type, allocator_type&> __buf(__n, 0, __base::__alloc()); |
| 2258 | __buf.__construct_at_end(__f, __l); |
| 2259 | typedef typename __split_buffer<value_type, allocator_type&>::iterator __fwd; |
| 2260 | return insert(__p, move_iterator<__fwd>(__buf.begin()), move_iterator<__fwd>(__buf.end())); |
| 2261 | } |
| 2262 | |
| 2263 | template <class _Tp, class _Allocator> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2264 | template <class _BiIter> |
| 2265 | typename deque<_Tp, _Allocator>::iterator |
| 2266 | deque<_Tp, _Allocator>::insert(const_iterator __p, _BiIter __f, _BiIter __l, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 2267 | typename enable_if<__is_cpp17_bidirectional_iterator<_BiIter>::value>::type*) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2268 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2269 | size_type __n = _VSTD::distance(__f, __l); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2270 | size_type __pos = __p - __base::begin(); |
| 2271 | size_type __to_end = __base::size() - __pos; |
| 2272 | allocator_type& __a = __base::__alloc(); |
| 2273 | if (__pos < __to_end) |
| 2274 | { // insert by shifting things backward |
| 2275 | if (__n > __front_spare()) |
| 2276 | __add_front_capacity(__n - __front_spare()); |
| 2277 | // __n <= __front_spare() |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2278 | iterator __old_begin = __base::begin(); |
| 2279 | iterator __i = __old_begin; |
| 2280 | _BiIter __m = __f; |
| 2281 | if (__n > __pos) |
| 2282 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2283 | __m = __pos < __n / 2 ? _VSTD::prev(__l, __pos) : _VSTD::next(__f, __n - __pos); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2284 | for (_BiIter __j = __m; __j != __f; --__base::__start_, ++__base::size()) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2285 | __alloc_traits::construct(__a, _VSTD::addressof(*--__i), *--__j); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2286 | __n = __pos; |
| 2287 | } |
| 2288 | if (__n > 0) |
| 2289 | { |
| 2290 | iterator __obn = __old_begin + __n; |
| 2291 | for (iterator __j = __obn; __j != __old_begin;) |
| 2292 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2293 | __alloc_traits::construct(__a, _VSTD::addressof(*--__i), _VSTD::move(*--__j)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2294 | --__base::__start_; |
| 2295 | ++__base::size(); |
| 2296 | } |
| 2297 | if (__n < __pos) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2298 | __old_begin = _VSTD::move(__obn, __old_begin + __pos, __old_begin); |
| 2299 | _VSTD::copy(__m, __l, __old_begin); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2300 | } |
| 2301 | } |
| 2302 | else |
| 2303 | { // insert by shifting things forward |
| 2304 | size_type __back_capacity = __back_spare(); |
| 2305 | if (__n > __back_capacity) |
| 2306 | __add_back_capacity(__n - __back_capacity); |
| 2307 | // __n <= __back_capacity |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2308 | iterator __old_end = __base::end(); |
| 2309 | iterator __i = __old_end; |
| 2310 | _BiIter __m = __l; |
| 2311 | size_type __de = __base::size() - __pos; |
| 2312 | if (__n > __de) |
| 2313 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2314 | __m = __de < __n / 2 ? _VSTD::next(__f, __de) : _VSTD::prev(__l, __n - __de); |
Eric Fiselier | a09a3b4 | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 2315 | for (_BiIter __j = __m; __j != __l; ++__i, (void) ++__j, ++__base::size()) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2316 | __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__j); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2317 | __n = __de; |
| 2318 | } |
| 2319 | if (__n > 0) |
| 2320 | { |
| 2321 | iterator __oen = __old_end - __n; |
Arthur O'Dwyer | 80dbcbe | 2021-09-07 21:35:37 -0400 | [diff] [blame] | 2322 | for (iterator __j = __oen; __j != __old_end; ++__i, (void) ++__j, ++__base::size()) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2323 | __alloc_traits::construct(__a, _VSTD::addressof(*__i), _VSTD::move(*__j)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2324 | if (__n < __de) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2325 | __old_end = _VSTD::move_backward(__old_end - __de, __oen, __old_end); |
| 2326 | _VSTD::copy_backward(__f, __m, __old_end); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2327 | } |
| 2328 | } |
| 2329 | return __base::begin() + __pos; |
| 2330 | } |
| 2331 | |
| 2332 | template <class _Tp, class _Allocator> |
| 2333 | template <class _InpIter> |
| 2334 | void |
| 2335 | deque<_Tp, _Allocator>::__append(_InpIter __f, _InpIter __l, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 2336 | typename enable_if<__is_cpp17_input_iterator<_InpIter>::value && |
| 2337 | !__is_cpp17_forward_iterator<_InpIter>::value>::type*) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2338 | { |
| 2339 | for (; __f != __l; ++__f) |
Eric Fiselier | 9691972 | 2017-10-17 13:03:17 +0000 | [diff] [blame] | 2340 | #ifdef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2341 | push_back(*__f); |
Eric Fiselier | 9691972 | 2017-10-17 13:03:17 +0000 | [diff] [blame] | 2342 | #else |
| 2343 | emplace_back(*__f); |
| 2344 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2345 | } |
| 2346 | |
| 2347 | template <class _Tp, class _Allocator> |
| 2348 | template <class _ForIter> |
| 2349 | void |
| 2350 | deque<_Tp, _Allocator>::__append(_ForIter __f, _ForIter __l, |
Eric Fiselier | cd5a677 | 2019-11-18 01:46:58 -0500 | [diff] [blame] | 2351 | typename enable_if<__is_cpp17_forward_iterator<_ForIter>::value>::type*) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2352 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2353 | size_type __n = _VSTD::distance(__f, __l); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2354 | allocator_type& __a = __base::__alloc(); |
| 2355 | size_type __back_capacity = __back_spare(); |
| 2356 | if (__n > __back_capacity) |
| 2357 | __add_back_capacity(__n - __back_capacity); |
| 2358 | // __n <= __back_capacity |
Eric Fiselier | c52db21 | 2019-08-12 07:51:05 +0000 | [diff] [blame] | 2359 | for (__deque_block_range __br : __deque_range(__base::end(), __base::end() + __n)) { |
| 2360 | _ConstructTransaction __tx(this, __br); |
| 2361 | for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void)++__f) { |
Arthur O'Dwyer | 4e0de31 | 2020-11-18 18:54:38 -0500 | [diff] [blame] | 2362 | __alloc_traits::construct(__a, _VSTD::__to_address(__tx.__pos_), *__f); |
Eric Fiselier | c52db21 | 2019-08-12 07:51:05 +0000 | [diff] [blame] | 2363 | } |
| 2364 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2365 | } |
| 2366 | |
| 2367 | template <class _Tp, class _Allocator> |
| 2368 | void |
| 2369 | deque<_Tp, _Allocator>::__append(size_type __n) |
| 2370 | { |
| 2371 | allocator_type& __a = __base::__alloc(); |
| 2372 | size_type __back_capacity = __back_spare(); |
| 2373 | if (__n > __back_capacity) |
| 2374 | __add_back_capacity(__n - __back_capacity); |
| 2375 | // __n <= __back_capacity |
Eric Fiselier | c52db21 | 2019-08-12 07:51:05 +0000 | [diff] [blame] | 2376 | for (__deque_block_range __br : __deque_range(__base::end(), __base::end() + __n)) { |
| 2377 | _ConstructTransaction __tx(this, __br); |
| 2378 | for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) { |
Arthur O'Dwyer | 4e0de31 | 2020-11-18 18:54:38 -0500 | [diff] [blame] | 2379 | __alloc_traits::construct(__a, _VSTD::__to_address(__tx.__pos_)); |
Eric Fiselier | c52db21 | 2019-08-12 07:51:05 +0000 | [diff] [blame] | 2380 | } |
| 2381 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2382 | } |
| 2383 | |
| 2384 | template <class _Tp, class _Allocator> |
| 2385 | void |
| 2386 | deque<_Tp, _Allocator>::__append(size_type __n, const value_type& __v) |
| 2387 | { |
| 2388 | allocator_type& __a = __base::__alloc(); |
| 2389 | size_type __back_capacity = __back_spare(); |
| 2390 | if (__n > __back_capacity) |
| 2391 | __add_back_capacity(__n - __back_capacity); |
| 2392 | // __n <= __back_capacity |
Eric Fiselier | c52db21 | 2019-08-12 07:51:05 +0000 | [diff] [blame] | 2393 | for (__deque_block_range __br : __deque_range(__base::end(), __base::end() + __n)) { |
| 2394 | _ConstructTransaction __tx(this, __br); |
| 2395 | for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) { |
Arthur O'Dwyer | 4e0de31 | 2020-11-18 18:54:38 -0500 | [diff] [blame] | 2396 | __alloc_traits::construct(__a, _VSTD::__to_address(__tx.__pos_), __v); |
Eric Fiselier | c52db21 | 2019-08-12 07:51:05 +0000 | [diff] [blame] | 2397 | } |
| 2398 | } |
| 2399 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | // Create front capacity for one block of elements. |
| 2403 | // Strong guarantee. Either do it or don't touch anything. |
| 2404 | template <class _Tp, class _Allocator> |
| 2405 | void |
| 2406 | deque<_Tp, _Allocator>::__add_front_capacity() |
| 2407 | { |
| 2408 | allocator_type& __a = __base::__alloc(); |
| 2409 | if (__back_spare() >= __base::__block_size) |
| 2410 | { |
| 2411 | __base::__start_ += __base::__block_size; |
| 2412 | pointer __pt = __base::__map_.back(); |
| 2413 | __base::__map_.pop_back(); |
| 2414 | __base::__map_.push_front(__pt); |
| 2415 | } |
| 2416 | // Else if __base::__map_.size() < __base::__map_.capacity() then we need to allocate 1 buffer |
| 2417 | else if (__base::__map_.size() < __base::__map_.capacity()) |
| 2418 | { // we can put the new buffer into the map, but don't shift things around |
| 2419 | // until all buffers are allocated. If we throw, we don't need to fix |
| 2420 | // anything up (any added buffers are undetectible) |
| 2421 | if (__base::__map_.__front_spare() > 0) |
| 2422 | __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size)); |
| 2423 | else |
| 2424 | { |
| 2425 | __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size)); |
| 2426 | // Done allocating, reorder capacity |
| 2427 | pointer __pt = __base::__map_.back(); |
| 2428 | __base::__map_.pop_back(); |
| 2429 | __base::__map_.push_front(__pt); |
| 2430 | } |
| 2431 | __base::__start_ = __base::__map_.size() == 1 ? |
| 2432 | __base::__block_size / 2 : |
| 2433 | __base::__start_ + __base::__block_size; |
| 2434 | } |
| 2435 | // Else need to allocate 1 buffer, *and* we need to reallocate __map_. |
| 2436 | else |
| 2437 | { |
| 2438 | __split_buffer<pointer, typename __base::__pointer_allocator&> |
| 2439 | __buf(max<size_type>(2 * __base::__map_.capacity(), 1), |
| 2440 | 0, __base::__map_.__alloc()); |
Marshall Clow | 5fd466b | 2015-03-09 17:08:51 +0000 | [diff] [blame] | 2441 | |
Marshall Clow | 8982dcd | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 2442 | typedef __allocator_destructor<_Allocator> _Dp; |
| 2443 | unique_ptr<pointer, _Dp> __hold( |
| 2444 | __alloc_traits::allocate(__a, __base::__block_size), |
| 2445 | _Dp(__a, __base::__block_size)); |
| 2446 | __buf.push_back(__hold.get()); |
| 2447 | __hold.release(); |
Louis Dionne | 72f2439 | 2018-12-12 23:58:25 +0000 | [diff] [blame] | 2448 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2449 | for (typename __base::__map_pointer __i = __base::__map_.begin(); |
| 2450 | __i != __base::__map_.end(); ++__i) |
| 2451 | __buf.push_back(*__i); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2452 | _VSTD::swap(__base::__map_.__first_, __buf.__first_); |
| 2453 | _VSTD::swap(__base::__map_.__begin_, __buf.__begin_); |
| 2454 | _VSTD::swap(__base::__map_.__end_, __buf.__end_); |
| 2455 | _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2456 | __base::__start_ = __base::__map_.size() == 1 ? |
| 2457 | __base::__block_size / 2 : |
| 2458 | __base::__start_ + __base::__block_size; |
| 2459 | } |
| 2460 | } |
| 2461 | |
| 2462 | // Create front capacity for __n elements. |
| 2463 | // Strong guarantee. Either do it or don't touch anything. |
| 2464 | template <class _Tp, class _Allocator> |
| 2465 | void |
| 2466 | deque<_Tp, _Allocator>::__add_front_capacity(size_type __n) |
| 2467 | { |
| 2468 | allocator_type& __a = __base::__alloc(); |
| 2469 | size_type __nb = __recommend_blocks(__n + __base::__map_.empty()); |
| 2470 | // Number of unused blocks at back: |
| 2471 | size_type __back_capacity = __back_spare() / __base::__block_size; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2472 | __back_capacity = _VSTD::min(__back_capacity, __nb); // don't take more than you need |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2473 | __nb -= __back_capacity; // number of blocks need to allocate |
| 2474 | // If __nb == 0, then we have sufficient capacity. |
| 2475 | if (__nb == 0) |
| 2476 | { |
| 2477 | __base::__start_ += __base::__block_size * __back_capacity; |
| 2478 | for (; __back_capacity > 0; --__back_capacity) |
| 2479 | { |
| 2480 | pointer __pt = __base::__map_.back(); |
| 2481 | __base::__map_.pop_back(); |
| 2482 | __base::__map_.push_front(__pt); |
| 2483 | } |
| 2484 | } |
| 2485 | // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers |
| 2486 | else if (__nb <= __base::__map_.capacity() - __base::__map_.size()) |
| 2487 | { // we can put the new buffers into the map, but don't shift things around |
| 2488 | // until all buffers are allocated. If we throw, we don't need to fix |
| 2489 | // anything up (any added buffers are undetectible) |
| 2490 | for (; __nb > 0; --__nb, __base::__start_ += __base::__block_size - (__base::__map_.size() == 1)) |
| 2491 | { |
| 2492 | if (__base::__map_.__front_spare() == 0) |
| 2493 | break; |
| 2494 | __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size)); |
| 2495 | } |
| 2496 | for (; __nb > 0; --__nb, ++__back_capacity) |
| 2497 | __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size)); |
| 2498 | // Done allocating, reorder capacity |
| 2499 | __base::__start_ += __back_capacity * __base::__block_size; |
| 2500 | for (; __back_capacity > 0; --__back_capacity) |
| 2501 | { |
| 2502 | pointer __pt = __base::__map_.back(); |
| 2503 | __base::__map_.pop_back(); |
| 2504 | __base::__map_.push_front(__pt); |
| 2505 | } |
| 2506 | } |
| 2507 | // Else need to allocate __nb buffers, *and* we need to reallocate __map_. |
| 2508 | else |
| 2509 | { |
| 2510 | size_type __ds = (__nb + __back_capacity) * __base::__block_size - __base::__map_.empty(); |
| 2511 | __split_buffer<pointer, typename __base::__pointer_allocator&> |
| 2512 | __buf(max<size_type>(2* __base::__map_.capacity(), |
| 2513 | __nb + __base::__map_.size()), |
| 2514 | 0, __base::__map_.__alloc()); |
| 2515 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2516 | try |
| 2517 | { |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 2518 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2519 | for (; __nb > 0; --__nb) |
| 2520 | __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size)); |
| 2521 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2522 | } |
| 2523 | catch (...) |
| 2524 | { |
| 2525 | for (typename __base::__map_pointer __i = __buf.begin(); |
| 2526 | __i != __buf.end(); ++__i) |
| 2527 | __alloc_traits::deallocate(__a, *__i, __base::__block_size); |
| 2528 | throw; |
| 2529 | } |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 2530 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2531 | for (; __back_capacity > 0; --__back_capacity) |
| 2532 | { |
| 2533 | __buf.push_back(__base::__map_.back()); |
| 2534 | __base::__map_.pop_back(); |
| 2535 | } |
| 2536 | for (typename __base::__map_pointer __i = __base::__map_.begin(); |
| 2537 | __i != __base::__map_.end(); ++__i) |
| 2538 | __buf.push_back(*__i); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2539 | _VSTD::swap(__base::__map_.__first_, __buf.__first_); |
| 2540 | _VSTD::swap(__base::__map_.__begin_, __buf.__begin_); |
| 2541 | _VSTD::swap(__base::__map_.__end_, __buf.__end_); |
| 2542 | _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2543 | __base::__start_ += __ds; |
| 2544 | } |
| 2545 | } |
| 2546 | |
| 2547 | // Create back capacity for one block of elements. |
| 2548 | // Strong guarantee. Either do it or don't touch anything. |
| 2549 | template <class _Tp, class _Allocator> |
| 2550 | void |
| 2551 | deque<_Tp, _Allocator>::__add_back_capacity() |
| 2552 | { |
| 2553 | allocator_type& __a = __base::__alloc(); |
| 2554 | if (__front_spare() >= __base::__block_size) |
| 2555 | { |
| 2556 | __base::__start_ -= __base::__block_size; |
| 2557 | pointer __pt = __base::__map_.front(); |
| 2558 | __base::__map_.pop_front(); |
| 2559 | __base::__map_.push_back(__pt); |
| 2560 | } |
| 2561 | // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers |
| 2562 | else if (__base::__map_.size() < __base::__map_.capacity()) |
| 2563 | { // we can put the new buffer into the map, but don't shift things around |
| 2564 | // until it is allocated. If we throw, we don't need to fix |
| 2565 | // anything up (any added buffers are undetectible) |
| 2566 | if (__base::__map_.__back_spare() != 0) |
| 2567 | __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size)); |
| 2568 | else |
| 2569 | { |
| 2570 | __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size)); |
| 2571 | // Done allocating, reorder capacity |
| 2572 | pointer __pt = __base::__map_.front(); |
| 2573 | __base::__map_.pop_front(); |
| 2574 | __base::__map_.push_back(__pt); |
| 2575 | } |
| 2576 | } |
| 2577 | // Else need to allocate 1 buffer, *and* we need to reallocate __map_. |
| 2578 | else |
| 2579 | { |
| 2580 | __split_buffer<pointer, typename __base::__pointer_allocator&> |
| 2581 | __buf(max<size_type>(2* __base::__map_.capacity(), 1), |
| 2582 | __base::__map_.size(), |
| 2583 | __base::__map_.__alloc()); |
Marshall Clow | 5fd466b | 2015-03-09 17:08:51 +0000 | [diff] [blame] | 2584 | |
Marshall Clow | 8982dcd | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 2585 | typedef __allocator_destructor<_Allocator> _Dp; |
| 2586 | unique_ptr<pointer, _Dp> __hold( |
| 2587 | __alloc_traits::allocate(__a, __base::__block_size), |
| 2588 | _Dp(__a, __base::__block_size)); |
| 2589 | __buf.push_back(__hold.get()); |
| 2590 | __hold.release(); |
Marshall Clow | 5fd466b | 2015-03-09 17:08:51 +0000 | [diff] [blame] | 2591 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2592 | for (typename __base::__map_pointer __i = __base::__map_.end(); |
| 2593 | __i != __base::__map_.begin();) |
| 2594 | __buf.push_front(*--__i); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2595 | _VSTD::swap(__base::__map_.__first_, __buf.__first_); |
| 2596 | _VSTD::swap(__base::__map_.__begin_, __buf.__begin_); |
| 2597 | _VSTD::swap(__base::__map_.__end_, __buf.__end_); |
| 2598 | _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2599 | } |
| 2600 | } |
| 2601 | |
| 2602 | // Create back capacity for __n elements. |
| 2603 | // Strong guarantee. Either do it or don't touch anything. |
| 2604 | template <class _Tp, class _Allocator> |
| 2605 | void |
| 2606 | deque<_Tp, _Allocator>::__add_back_capacity(size_type __n) |
| 2607 | { |
| 2608 | allocator_type& __a = __base::__alloc(); |
| 2609 | size_type __nb = __recommend_blocks(__n + __base::__map_.empty()); |
| 2610 | // Number of unused blocks at front: |
| 2611 | size_type __front_capacity = __front_spare() / __base::__block_size; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2612 | __front_capacity = _VSTD::min(__front_capacity, __nb); // don't take more than you need |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2613 | __nb -= __front_capacity; // number of blocks need to allocate |
| 2614 | // If __nb == 0, then we have sufficient capacity. |
| 2615 | if (__nb == 0) |
| 2616 | { |
| 2617 | __base::__start_ -= __base::__block_size * __front_capacity; |
| 2618 | for (; __front_capacity > 0; --__front_capacity) |
| 2619 | { |
| 2620 | pointer __pt = __base::__map_.front(); |
| 2621 | __base::__map_.pop_front(); |
| 2622 | __base::__map_.push_back(__pt); |
| 2623 | } |
| 2624 | } |
| 2625 | // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers |
| 2626 | else if (__nb <= __base::__map_.capacity() - __base::__map_.size()) |
| 2627 | { // we can put the new buffers into the map, but don't shift things around |
| 2628 | // until all buffers are allocated. If we throw, we don't need to fix |
| 2629 | // anything up (any added buffers are undetectible) |
| 2630 | for (; __nb > 0; --__nb) |
| 2631 | { |
| 2632 | if (__base::__map_.__back_spare() == 0) |
| 2633 | break; |
| 2634 | __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size)); |
| 2635 | } |
| 2636 | for (; __nb > 0; --__nb, ++__front_capacity, __base::__start_ += |
| 2637 | __base::__block_size - (__base::__map_.size() == 1)) |
| 2638 | __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size)); |
| 2639 | // Done allocating, reorder capacity |
| 2640 | __base::__start_ -= __base::__block_size * __front_capacity; |
| 2641 | for (; __front_capacity > 0; --__front_capacity) |
| 2642 | { |
| 2643 | pointer __pt = __base::__map_.front(); |
| 2644 | __base::__map_.pop_front(); |
| 2645 | __base::__map_.push_back(__pt); |
| 2646 | } |
| 2647 | } |
| 2648 | // Else need to allocate __nb buffers, *and* we need to reallocate __map_. |
| 2649 | else |
| 2650 | { |
| 2651 | size_type __ds = __front_capacity * __base::__block_size; |
| 2652 | __split_buffer<pointer, typename __base::__pointer_allocator&> |
| 2653 | __buf(max<size_type>(2* __base::__map_.capacity(), |
| 2654 | __nb + __base::__map_.size()), |
| 2655 | __base::__map_.size() - __front_capacity, |
| 2656 | __base::__map_.__alloc()); |
| 2657 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2658 | try |
| 2659 | { |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 2660 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2661 | for (; __nb > 0; --__nb) |
| 2662 | __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size)); |
| 2663 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2664 | } |
| 2665 | catch (...) |
| 2666 | { |
| 2667 | for (typename __base::__map_pointer __i = __buf.begin(); |
| 2668 | __i != __buf.end(); ++__i) |
| 2669 | __alloc_traits::deallocate(__a, *__i, __base::__block_size); |
| 2670 | throw; |
| 2671 | } |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 2672 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2673 | for (; __front_capacity > 0; --__front_capacity) |
| 2674 | { |
| 2675 | __buf.push_back(__base::__map_.front()); |
| 2676 | __base::__map_.pop_front(); |
| 2677 | } |
| 2678 | for (typename __base::__map_pointer __i = __base::__map_.end(); |
| 2679 | __i != __base::__map_.begin();) |
| 2680 | __buf.push_front(*--__i); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2681 | _VSTD::swap(__base::__map_.__first_, __buf.__first_); |
| 2682 | _VSTD::swap(__base::__map_.__begin_, __buf.__begin_); |
| 2683 | _VSTD::swap(__base::__map_.__end_, __buf.__end_); |
| 2684 | _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2685 | __base::__start_ -= __ds; |
| 2686 | } |
| 2687 | } |
| 2688 | |
| 2689 | template <class _Tp, class _Allocator> |
| 2690 | void |
| 2691 | deque<_Tp, _Allocator>::pop_front() |
| 2692 | { |
| 2693 | allocator_type& __a = __base::__alloc(); |
Arthur O'Dwyer | 4e0de31 | 2020-11-18 18:54:38 -0500 | [diff] [blame] | 2694 | __alloc_traits::destroy(__a, _VSTD::__to_address(*(__base::__map_.begin() + |
Howard Hinnant | dcb73a3 | 2013-06-23 21:17:24 +0000 | [diff] [blame] | 2695 | __base::__start_ / __base::__block_size) + |
| 2696 | __base::__start_ % __base::__block_size)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2697 | --__base::size(); |
Eric Fiselier | 27e0362 | 2019-08-01 23:11:18 +0000 | [diff] [blame] | 2698 | ++__base::__start_; |
| 2699 | __maybe_remove_front_spare(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2700 | } |
| 2701 | |
| 2702 | template <class _Tp, class _Allocator> |
| 2703 | void |
| 2704 | deque<_Tp, _Allocator>::pop_back() |
| 2705 | { |
Kristina Bessonova | aeeaa7e | 2021-05-09 19:29:56 +0200 | [diff] [blame] | 2706 | _LIBCPP_ASSERT(!empty(), "deque::pop_back called on an empty deque"); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2707 | allocator_type& __a = __base::__alloc(); |
| 2708 | size_type __p = __base::size() + __base::__start_ - 1; |
Arthur O'Dwyer | 4e0de31 | 2020-11-18 18:54:38 -0500 | [diff] [blame] | 2709 | __alloc_traits::destroy(__a, _VSTD::__to_address(*(__base::__map_.begin() + |
Howard Hinnant | dcb73a3 | 2013-06-23 21:17:24 +0000 | [diff] [blame] | 2710 | __p / __base::__block_size) + |
| 2711 | __p % __base::__block_size)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2712 | --__base::size(); |
Eric Fiselier | 27e0362 | 2019-08-01 23:11:18 +0000 | [diff] [blame] | 2713 | __maybe_remove_back_spare(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2714 | } |
| 2715 | |
| 2716 | // move assign [__f, __l) to [__r, __r + (__l-__f)). |
| 2717 | // If __vt points into [__f, __l), then subtract (__f - __r) from __vt. |
| 2718 | template <class _Tp, class _Allocator> |
| 2719 | typename deque<_Tp, _Allocator>::iterator |
| 2720 | deque<_Tp, _Allocator>::__move_and_check(iterator __f, iterator __l, iterator __r, |
| 2721 | const_pointer& __vt) |
| 2722 | { |
| 2723 | // as if |
| 2724 | // for (; __f != __l; ++__f, ++__r) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2725 | // *__r = _VSTD::move(*__f); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2726 | difference_type __n = __l - __f; |
| 2727 | while (__n > 0) |
| 2728 | { |
| 2729 | pointer __fb = __f.__ptr_; |
| 2730 | pointer __fe = *__f.__m_iter_ + __base::__block_size; |
| 2731 | difference_type __bs = __fe - __fb; |
| 2732 | if (__bs > __n) |
| 2733 | { |
| 2734 | __bs = __n; |
| 2735 | __fe = __fb + __bs; |
| 2736 | } |
| 2737 | if (__fb <= __vt && __vt < __fe) |
Howard Hinnant | dcb73a3 | 2013-06-23 21:17:24 +0000 | [diff] [blame] | 2738 | __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) -= __f - __r).__ptr_; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2739 | __r = _VSTD::move(__fb, __fe, __r); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2740 | __n -= __bs; |
| 2741 | __f += __bs; |
| 2742 | } |
| 2743 | return __r; |
| 2744 | } |
| 2745 | |
| 2746 | // move assign [__f, __l) to [__r - (__l-__f), __r) backwards. |
| 2747 | // If __vt points into [__f, __l), then add (__r - __l) to __vt. |
| 2748 | template <class _Tp, class _Allocator> |
| 2749 | typename deque<_Tp, _Allocator>::iterator |
| 2750 | deque<_Tp, _Allocator>::__move_backward_and_check(iterator __f, iterator __l, iterator __r, |
| 2751 | const_pointer& __vt) |
| 2752 | { |
| 2753 | // as if |
| 2754 | // while (__f != __l) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2755 | // *--__r = _VSTD::move(*--__l); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2756 | difference_type __n = __l - __f; |
| 2757 | while (__n > 0) |
| 2758 | { |
| 2759 | --__l; |
| 2760 | pointer __lb = *__l.__m_iter_; |
| 2761 | pointer __le = __l.__ptr_ + 1; |
| 2762 | difference_type __bs = __le - __lb; |
| 2763 | if (__bs > __n) |
| 2764 | { |
| 2765 | __bs = __n; |
| 2766 | __lb = __le - __bs; |
| 2767 | } |
| 2768 | if (__lb <= __vt && __vt < __le) |
Howard Hinnant | dcb73a3 | 2013-06-23 21:17:24 +0000 | [diff] [blame] | 2769 | __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) += __r - __l - 1).__ptr_; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2770 | __r = _VSTD::move_backward(__lb, __le, __r); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2771 | __n -= __bs; |
| 2772 | __l -= __bs - 1; |
| 2773 | } |
| 2774 | return __r; |
| 2775 | } |
| 2776 | |
| 2777 | // move construct [__f, __l) to [__r, __r + (__l-__f)). |
| 2778 | // If __vt points into [__f, __l), then add (__r - __f) to __vt. |
| 2779 | template <class _Tp, class _Allocator> |
| 2780 | void |
| 2781 | deque<_Tp, _Allocator>::__move_construct_and_check(iterator __f, iterator __l, |
| 2782 | iterator __r, const_pointer& __vt) |
| 2783 | { |
| 2784 | allocator_type& __a = __base::__alloc(); |
| 2785 | // as if |
| 2786 | // for (; __f != __l; ++__r, ++__f, ++__base::size()) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2787 | // __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__f)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2788 | difference_type __n = __l - __f; |
| 2789 | while (__n > 0) |
| 2790 | { |
| 2791 | pointer __fb = __f.__ptr_; |
| 2792 | pointer __fe = *__f.__m_iter_ + __base::__block_size; |
| 2793 | difference_type __bs = __fe - __fb; |
| 2794 | if (__bs > __n) |
| 2795 | { |
| 2796 | __bs = __n; |
| 2797 | __fe = __fb + __bs; |
| 2798 | } |
| 2799 | if (__fb <= __vt && __vt < __fe) |
Howard Hinnant | dcb73a3 | 2013-06-23 21:17:24 +0000 | [diff] [blame] | 2800 | __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) += __r - __f).__ptr_; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2801 | for (; __fb != __fe; ++__fb, ++__r, ++__base::size()) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2802 | __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__fb)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2803 | __n -= __bs; |
| 2804 | __f += __bs; |
| 2805 | } |
| 2806 | } |
| 2807 | |
| 2808 | // move construct [__f, __l) to [__r - (__l-__f), __r) backwards. |
| 2809 | // If __vt points into [__f, __l), then subtract (__l - __r) from __vt. |
| 2810 | template <class _Tp, class _Allocator> |
| 2811 | void |
| 2812 | deque<_Tp, _Allocator>::__move_construct_backward_and_check(iterator __f, iterator __l, |
| 2813 | iterator __r, const_pointer& __vt) |
| 2814 | { |
| 2815 | allocator_type& __a = __base::__alloc(); |
| 2816 | // as if |
| 2817 | // for (iterator __j = __l; __j != __f;) |
| 2818 | // { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2819 | // __alloc_traitsconstruct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__j)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2820 | // --__base::__start_; |
| 2821 | // ++__base::size(); |
| 2822 | // } |
| 2823 | difference_type __n = __l - __f; |
| 2824 | while (__n > 0) |
| 2825 | { |
| 2826 | --__l; |
| 2827 | pointer __lb = *__l.__m_iter_; |
| 2828 | pointer __le = __l.__ptr_ + 1; |
| 2829 | difference_type __bs = __le - __lb; |
| 2830 | if (__bs > __n) |
| 2831 | { |
| 2832 | __bs = __n; |
| 2833 | __lb = __le - __bs; |
| 2834 | } |
| 2835 | if (__lb <= __vt && __vt < __le) |
Howard Hinnant | dcb73a3 | 2013-06-23 21:17:24 +0000 | [diff] [blame] | 2836 | __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) -= __l - __r + 1).__ptr_; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2837 | while (__le != __lb) |
| 2838 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2839 | __alloc_traits::construct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__le)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2840 | --__base::__start_; |
| 2841 | ++__base::size(); |
| 2842 | } |
| 2843 | __n -= __bs; |
| 2844 | __l -= __bs - 1; |
| 2845 | } |
| 2846 | } |
| 2847 | |
| 2848 | template <class _Tp, class _Allocator> |
| 2849 | typename deque<_Tp, _Allocator>::iterator |
| 2850 | deque<_Tp, _Allocator>::erase(const_iterator __f) |
| 2851 | { |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2852 | iterator __b = __base::begin(); |
| 2853 | difference_type __pos = __f - __b; |
| 2854 | iterator __p = __b + __pos; |
| 2855 | allocator_type& __a = __base::__alloc(); |
Eric Fiselier | 37c2215 | 2016-12-24 00:24:44 +0000 | [diff] [blame] | 2856 | if (static_cast<size_t>(__pos) <= (__base::size() - 1) / 2) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2857 | { // erase from front |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2858 | _VSTD::move_backward(__b, __p, _VSTD::next(__p)); |
| 2859 | __alloc_traits::destroy(__a, _VSTD::addressof(*__b)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2860 | --__base::size(); |
| 2861 | ++__base::__start_; |
Eric Fiselier | 27e0362 | 2019-08-01 23:11:18 +0000 | [diff] [blame] | 2862 | __maybe_remove_front_spare(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2863 | } |
| 2864 | else |
| 2865 | { // erase from back |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2866 | iterator __i = _VSTD::move(_VSTD::next(__p), __base::end(), __p); |
| 2867 | __alloc_traits::destroy(__a, _VSTD::addressof(*__i)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2868 | --__base::size(); |
Eric Fiselier | 27e0362 | 2019-08-01 23:11:18 +0000 | [diff] [blame] | 2869 | __maybe_remove_back_spare(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2870 | } |
| 2871 | return __base::begin() + __pos; |
| 2872 | } |
| 2873 | |
| 2874 | template <class _Tp, class _Allocator> |
| 2875 | typename deque<_Tp, _Allocator>::iterator |
| 2876 | deque<_Tp, _Allocator>::erase(const_iterator __f, const_iterator __l) |
| 2877 | { |
| 2878 | difference_type __n = __l - __f; |
| 2879 | iterator __b = __base::begin(); |
| 2880 | difference_type __pos = __f - __b; |
| 2881 | iterator __p = __b + __pos; |
| 2882 | if (__n > 0) |
| 2883 | { |
| 2884 | allocator_type& __a = __base::__alloc(); |
Eric Fiselier | 37c2215 | 2016-12-24 00:24:44 +0000 | [diff] [blame] | 2885 | if (static_cast<size_t>(__pos) <= (__base::size() - __n) / 2) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2886 | { // erase from front |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2887 | iterator __i = _VSTD::move_backward(__b, __p, __p + __n); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2888 | for (; __b != __i; ++__b) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2889 | __alloc_traits::destroy(__a, _VSTD::addressof(*__b)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2890 | __base::size() -= __n; |
| 2891 | __base::__start_ += __n; |
Eric Fiselier | 27e0362 | 2019-08-01 23:11:18 +0000 | [diff] [blame] | 2892 | while (__maybe_remove_front_spare()) { |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2893 | } |
| 2894 | } |
| 2895 | else |
| 2896 | { // erase from back |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2897 | iterator __i = _VSTD::move(__p + __n, __base::end(), __p); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2898 | for (iterator __e = __base::end(); __i != __e; ++__i) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2899 | __alloc_traits::destroy(__a, _VSTD::addressof(*__i)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2900 | __base::size() -= __n; |
Eric Fiselier | 27e0362 | 2019-08-01 23:11:18 +0000 | [diff] [blame] | 2901 | while (__maybe_remove_back_spare()) { |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2902 | } |
| 2903 | } |
| 2904 | } |
| 2905 | return __base::begin() + __pos; |
| 2906 | } |
| 2907 | |
| 2908 | template <class _Tp, class _Allocator> |
| 2909 | void |
| 2910 | deque<_Tp, _Allocator>::__erase_to_end(const_iterator __f) |
| 2911 | { |
| 2912 | iterator __e = __base::end(); |
| 2913 | difference_type __n = __e - __f; |
| 2914 | if (__n > 0) |
| 2915 | { |
| 2916 | allocator_type& __a = __base::__alloc(); |
| 2917 | iterator __b = __base::begin(); |
| 2918 | difference_type __pos = __f - __b; |
| 2919 | for (iterator __p = __b + __pos; __p != __e; ++__p) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2920 | __alloc_traits::destroy(__a, _VSTD::addressof(*__p)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2921 | __base::size() -= __n; |
Eric Fiselier | 27e0362 | 2019-08-01 23:11:18 +0000 | [diff] [blame] | 2922 | while (__maybe_remove_back_spare()) { |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2923 | } |
| 2924 | } |
| 2925 | } |
| 2926 | |
| 2927 | template <class _Tp, class _Allocator> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2928 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2929 | void |
| 2930 | deque<_Tp, _Allocator>::swap(deque& __c) |
Marshall Clow | 8982dcd | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 2931 | #if _LIBCPP_STD_VER >= 14 |
| 2932 | _NOEXCEPT |
| 2933 | #else |
Louis Dionne | 72f2439 | 2018-12-12 23:58:25 +0000 | [diff] [blame] | 2934 | _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || |
Marshall Clow | 8982dcd | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 2935 | __is_nothrow_swappable<allocator_type>::value) |
| 2936 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2937 | { |
| 2938 | __base::swap(__c); |
| 2939 | } |
| 2940 | |
| 2941 | template <class _Tp, class _Allocator> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2942 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2943 | void |
Howard Hinnant | da2df91 | 2011-06-02 16:10:22 +0000 | [diff] [blame] | 2944 | deque<_Tp, _Allocator>::clear() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2945 | { |
| 2946 | __base::clear(); |
| 2947 | } |
| 2948 | |
| 2949 | template <class _Tp, class _Allocator> |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 2950 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2951 | bool |
| 2952 | operator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) |
| 2953 | { |
| 2954 | const typename deque<_Tp, _Allocator>::size_type __sz = __x.size(); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2955 | return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2956 | } |
| 2957 | |
| 2958 | template <class _Tp, class _Allocator> |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 2959 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2960 | bool |
| 2961 | operator!=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) |
| 2962 | { |
| 2963 | return !(__x == __y); |
| 2964 | } |
| 2965 | |
| 2966 | template <class _Tp, class _Allocator> |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 2967 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2968 | bool |
| 2969 | operator< (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) |
| 2970 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2971 | return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2972 | } |
| 2973 | |
| 2974 | template <class _Tp, class _Allocator> |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 2975 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2976 | bool |
| 2977 | operator> (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) |
| 2978 | { |
| 2979 | return __y < __x; |
| 2980 | } |
| 2981 | |
| 2982 | template <class _Tp, class _Allocator> |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 2983 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2984 | bool |
| 2985 | operator>=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) |
| 2986 | { |
| 2987 | return !(__x < __y); |
| 2988 | } |
| 2989 | |
| 2990 | template <class _Tp, class _Allocator> |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 2991 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2992 | bool |
| 2993 | operator<=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) |
| 2994 | { |
| 2995 | return !(__y < __x); |
| 2996 | } |
| 2997 | |
| 2998 | template <class _Tp, class _Allocator> |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 2999 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3000 | void |
| 3001 | swap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y) |
Howard Hinnant | ca2fc22 | 2011-06-02 20:00:14 +0000 | [diff] [blame] | 3002 | _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3003 | { |
| 3004 | __x.swap(__y); |
| 3005 | } |
| 3006 | |
Marshall Clow | 29b53f2 | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 3007 | #if _LIBCPP_STD_VER > 17 |
| 3008 | template <class _Tp, class _Allocator, class _Up> |
Marek Kurdej | a98b141 | 2020-05-02 13:58:03 +0200 | [diff] [blame] | 3009 | inline _LIBCPP_INLINE_VISIBILITY typename deque<_Tp, _Allocator>::size_type |
| 3010 | erase(deque<_Tp, _Allocator>& __c, const _Up& __v) { |
| 3011 | auto __old_size = __c.size(); |
| 3012 | __c.erase(_VSTD::remove(__c.begin(), __c.end(), __v), __c.end()); |
| 3013 | return __old_size - __c.size(); |
| 3014 | } |
Marshall Clow | 29b53f2 | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 3015 | |
| 3016 | template <class _Tp, class _Allocator, class _Predicate> |
Marek Kurdej | a98b141 | 2020-05-02 13:58:03 +0200 | [diff] [blame] | 3017 | inline _LIBCPP_INLINE_VISIBILITY typename deque<_Tp, _Allocator>::size_type |
| 3018 | erase_if(deque<_Tp, _Allocator>& __c, _Predicate __pred) { |
| 3019 | auto __old_size = __c.size(); |
| 3020 | __c.erase(_VSTD::remove_if(__c.begin(), __c.end(), __pred), __c.end()); |
| 3021 | return __old_size - __c.size(); |
| 3022 | } |
Marshall Clow | 29b53f2 | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 3023 | #endif |
| 3024 | |
| 3025 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3026 | _LIBCPP_END_NAMESPACE_STD |
| 3027 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 3028 | _LIBCPP_POP_MACROS |
| 3029 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 3030 | #endif // _LIBCPP_DEQUE |