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