blob: 10f78a97e690d7da5472f326dfe697fd2e533481 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- deque -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_DEQUE
12#define _LIBCPP_DEQUE
13
14/*
15 deque synopsis
16
17namespace std
18{
19
20template <class T, class Allocator = allocator<T> >
21class deque
22{
23public:
24 // types:
25 typedef T value_type;
26 typedef Allocator allocator_type;
27
28 typedef typename allocator_type::reference reference;
29 typedef typename allocator_type::const_reference const_reference;
30 typedef implementation-defined iterator;
31 typedef implementation-defined const_iterator;
32 typedef typename allocator_type::size_type size_type;
33 typedef typename allocator_type::difference_type difference_type;
34
35 typedef typename allocator_type::pointer pointer;
36 typedef typename allocator_type::const_pointer const_pointer;
37 typedef std::reverse_iterator<iterator> reverse_iterator;
38 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
39
40 // construct/copy/destroy:
Howard Hinnant009b2c42011-06-03 15:16:49 +000041 deque() noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000042 explicit deque(const allocator_type& a);
43 explicit deque(size_type n);
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 Hinnant18884f42011-06-02 21:38:57 +000051 deque(deque&& c)
52 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000053 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 Hinnant18884f42011-06-02 21:38:57 +000059 deque& operator=(deque&& c)
60 noexcept(
61 allocator_type::propagate_on_container_move_assignment::value &&
62 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000063 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 Hinnanta12beb32011-06-02 16:10:22 +000070 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000071
72 // iterators:
73
Howard Hinnanta12beb32011-06-02 16:10:22 +000074 iterator begin() noexcept;
75 const_iterator begin() const noexcept;
76 iterator end() noexcept;
77 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000078
Howard Hinnanta12beb32011-06-02 16:10:22 +000079 reverse_iterator rbegin() noexcept;
80 const_reverse_iterator rbegin() const noexcept;
81 reverse_iterator rend() noexcept;
82 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000083
Howard Hinnanta12beb32011-06-02 16:10:22 +000084 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 Hinnantbc8d3f92010-05-11 19:42:16 +000088
89 // capacity:
Howard Hinnanta12beb32011-06-02 16:10:22 +000090 size_type size() const noexcept;
91 size_type max_size() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000092 void resize(size_type n);
93 void resize(size_type n, const value_type& v);
94 void shrink_to_fit();
Howard Hinnanta12beb32011-06-02 16:10:22 +000095 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000096
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);
112 template <class... Args> void emplace_front(Args&&... args);
113 template <class... Args> void emplace_back(Args&&... args);
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>
119 iterator insert (const_iterator p, InputIterator f, InputIterator l);
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 Hinnant18884f42011-06-02 21:38:57 +0000125 void swap(deque& c)
126 noexcept(!allocator_type::propagate_on_container_swap::value ||
127 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnanta12beb32011-06-02 16:10:22 +0000128 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000129};
130
131template <class T, class Allocator>
132 bool operator==(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
133template <class T, class Allocator>
134 bool operator< (const deque<T,Allocator>& x, const deque<T,Allocator>& y);
135template <class T, class Allocator>
136 bool operator!=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
137template <class T, class Allocator>
138 bool operator> (const deque<T,Allocator>& x, const deque<T,Allocator>& y);
139template <class T, class Allocator>
140 bool operator>=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
141template <class T, class Allocator>
142 bool operator<=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
143
144// specialized algorithms:
145template <class T, class Allocator>
Howard Hinnantc5607272011-06-03 17:30:28 +0000146 void swap(deque<T,Allocator>& x, deque<T,Allocator>& y)
147 noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000148
149} // std
150
151*/
152
153#pragma GCC system_header
154
155#include <__config>
156#include <__split_buffer>
157#include <type_traits>
158#include <initializer_list>
159#include <iterator>
160#include <algorithm>
161#include <stdexcept>
162
163_LIBCPP_BEGIN_NAMESPACE_STD
164
165template <class _Tp, class _Allocator> class __deque_base;
166
167template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
168 class _DiffType, _DiffType _BlockSize>
Howard Hinnant422a53f2010-09-21 21:28:23 +0000169class _LIBCPP_VISIBLE __deque_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000170
171template <class _RAIter,
172 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
173__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
174copy(_RAIter __f,
175 _RAIter __l,
176 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
177 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
178
179template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
180 class _OutputIterator>
181_OutputIterator
182copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
183 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
184 _OutputIterator __r);
185
186template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
187 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
188__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
189copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
190 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
191 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
192
193template <class _RAIter,
194 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
195__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
196copy_backward(_RAIter __f,
197 _RAIter __l,
198 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
199 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
200
201template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
202 class _OutputIterator>
203_OutputIterator
204copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
205 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
206 _OutputIterator __r);
207
208template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
209 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
210__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
211copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
212 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
213 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
214
215template <class _RAIter,
216 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
217__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
218move(_RAIter __f,
219 _RAIter __l,
220 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
221 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
222
223template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
224 class _OutputIterator>
225_OutputIterator
226move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
227 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
228 _OutputIterator __r);
229
230template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
231 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
232__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
233move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
234 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
235 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
236
237template <class _RAIter,
238 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
239__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
240move_backward(_RAIter __f,
241 _RAIter __l,
242 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
243 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
244
245template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
246 class _OutputIterator>
247_OutputIterator
248move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
249 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
250 _OutputIterator __r);
251
252template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
253 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
254__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
255move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
256 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
257 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
258
259template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
260 class _DiffType, _DiffType _BlockSize>
Howard Hinnant422a53f2010-09-21 21:28:23 +0000261class _LIBCPP_VISIBLE __deque_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000262{
263 typedef _MapPointer __map_iterator;
264public:
265 typedef _Pointer pointer;
266 typedef _DiffType difference_type;
267private:
268 __map_iterator __m_iter_;
269 pointer __ptr_;
270
271 static const difference_type __block_size = _BlockSize;
272public:
273 typedef _ValueType value_type;
274 typedef random_access_iterator_tag iterator_category;
275 typedef _Reference reference;
276
Howard Hinnanta12beb32011-06-02 16:10:22 +0000277 _LIBCPP_INLINE_VISIBILITY __deque_iterator() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000278
279 template <class _P, class _R, class _MP>
280 _LIBCPP_INLINE_VISIBILITY
281 __deque_iterator(const __deque_iterator<value_type, _P, _R, _MP, difference_type, __block_size>& __it,
Howard Hinnanta12beb32011-06-02 16:10:22 +0000282 typename enable_if<is_convertible<_P, pointer>::value>::type* = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000283 : __m_iter_(__it.__m_iter_), __ptr_(__it.__ptr_) {}
284
285 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return *__ptr_;}
286 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return __ptr_;}
287
288 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator++()
289 {
290 if (++__ptr_ - *__m_iter_ == __block_size)
291 {
292 ++__m_iter_;
293 __ptr_ = *__m_iter_;
294 }
295 return *this;
296 }
297
298 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator++(int)
299 {
300 __deque_iterator __tmp = *this;
301 ++(*this);
302 return __tmp;
303 }
304
305 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator--()
306 {
307 if (__ptr_ == *__m_iter_)
308 {
309 --__m_iter_;
310 __ptr_ = *__m_iter_ + __block_size;
311 }
312 --__ptr_;
313 return *this;
314 }
315
316 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator--(int)
317 {
318 __deque_iterator __tmp = *this;
319 --(*this);
320 return __tmp;
321 }
322
323 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator+=(difference_type __n)
324 {
325 if (__n != 0)
326 {
327 __n += __ptr_ - *__m_iter_;
328 if (__n > 0)
329 {
330 __m_iter_ += __n / __block_size;
331 __ptr_ = *__m_iter_ + __n % __block_size;
332 }
333 else // (__n < 0)
334 {
335 difference_type __z = __block_size - 1 - __n;
336 __m_iter_ -= __z / __block_size;
337 __ptr_ = *__m_iter_ + (__block_size - 1 - __z % __block_size);
338 }
339 }
340 return *this;
341 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000342
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000343 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator-=(difference_type __n)
344 {
345 return *this += -__n;
346 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000347
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000348 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator+(difference_type __n) const
349 {
350 __deque_iterator __t(*this);
351 __t += __n;
352 return __t;
353 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000354
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000355 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator-(difference_type __n) const
356 {
357 __deque_iterator __t(*this);
358 __t -= __n;
359 return __t;
360 }
361
362 _LIBCPP_INLINE_VISIBILITY
363 friend __deque_iterator operator+(difference_type __n, const __deque_iterator& __it)
364 {return __it + __n;}
365
366 _LIBCPP_INLINE_VISIBILITY
367 friend difference_type operator-(const __deque_iterator& __x, const __deque_iterator& __y)
368 {
369 if (__x != __y)
370 return (__x.__m_iter_ - __y.__m_iter_) * __block_size
371 + (__x.__ptr_ - *__x.__m_iter_)
372 - (__y.__ptr_ - *__y.__m_iter_);
373 return 0;
374 }
375
376 _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const
377 {return *(*this + __n);}
378
379 _LIBCPP_INLINE_VISIBILITY friend
380 bool operator==(const __deque_iterator& __x, const __deque_iterator& __y)
381 {return __x.__ptr_ == __y.__ptr_;}
382
383 _LIBCPP_INLINE_VISIBILITY friend
384 bool operator!=(const __deque_iterator& __x, const __deque_iterator& __y)
385 {return !(__x == __y);}
386
387 _LIBCPP_INLINE_VISIBILITY friend
388 bool operator<(const __deque_iterator& __x, const __deque_iterator& __y)
389 {return __x.__m_iter_ < __y.__m_iter_ ||
390 (__x.__m_iter_ == __y.__m_iter_ && __x.__ptr_ < __y.__ptr_);}
391
392 _LIBCPP_INLINE_VISIBILITY friend
393 bool operator>(const __deque_iterator& __x, const __deque_iterator& __y)
394 {return __y < __x;}
395
396 _LIBCPP_INLINE_VISIBILITY friend
397 bool operator<=(const __deque_iterator& __x, const __deque_iterator& __y)
398 {return !(__y < __x);}
399
400 _LIBCPP_INLINE_VISIBILITY friend
401 bool operator>=(const __deque_iterator& __x, const __deque_iterator& __y)
402 {return !(__x < __y);}
403
404private:
Howard Hinnanta12beb32011-06-02 16:10:22 +0000405 _LIBCPP_INLINE_VISIBILITY __deque_iterator(__map_iterator __m, pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000406 : __m_iter_(__m), __ptr_(__p) {}
407
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000408 template <class _Tp, class _A> friend class __deque_base;
Howard Hinnant422a53f2010-09-21 21:28:23 +0000409 template <class _Tp, class _A> friend class _LIBCPP_VISIBLE deque;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000410 template <class _V, class _P, class _R, class _MP, class _D, _D>
Howard Hinnant422a53f2010-09-21 21:28:23 +0000411 friend class _LIBCPP_VISIBLE __deque_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000412
413 template <class _RAIter,
414 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
415 friend
416 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
417 copy(_RAIter __f,
418 _RAIter __l,
419 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
420 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
421
422 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
423 class _OutputIterator>
424 friend
425 _OutputIterator
426 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
427 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
428 _OutputIterator __r);
429
430 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
431 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
432 friend
433 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
434 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
435 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
436 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
437
438 template <class _RAIter,
439 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
440 friend
441 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
442 copy_backward(_RAIter __f,
443 _RAIter __l,
444 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
445 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
446
447 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
448 class _OutputIterator>
449 friend
450 _OutputIterator
451 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
452 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
453 _OutputIterator __r);
454
455 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
456 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
457 friend
458 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
459 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
460 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
461 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
462
463 template <class _RAIter,
464 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
465 friend
466 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
467 move(_RAIter __f,
468 _RAIter __l,
469 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
470 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
471
472 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
473 class _OutputIterator>
474 friend
475 _OutputIterator
476 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
477 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
478 _OutputIterator __r);
479
480 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
481 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
482 friend
483 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
484 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
485 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
486 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
487
488 template <class _RAIter,
489 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
490 friend
491 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
492 move_backward(_RAIter __f,
493 _RAIter __l,
494 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
495 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
496
497 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
498 class _OutputIterator>
499 friend
500 _OutputIterator
501 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
502 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
503 _OutputIterator __r);
504
505 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
506 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
507 friend
508 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
509 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
510 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
511 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
512};
513
514// copy
515
516template <class _RAIter,
517 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
518__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
519copy(_RAIter __f,
520 _RAIter __l,
521 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
522 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
523{
524 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
525 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
526 while (__f != __l)
527 {
528 pointer __rb = __r.__ptr_;
529 pointer __re = *__r.__m_iter_ + _B2;
530 difference_type __bs = __re - __rb;
531 difference_type __n = __l - __f;
532 _RAIter __m = __l;
533 if (__n > __bs)
534 {
535 __n = __bs;
536 __m = __f + __n;
537 }
538 _STD::copy(__f, __m, __rb);
539 __f = __m;
540 __r += __n;
541 }
542 return __r;
543}
544
545template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
546 class _OutputIterator>
547_OutputIterator
548copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
549 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
550 _OutputIterator __r)
551{
552 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
553 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
554 difference_type __n = __l - __f;
555 while (__n > 0)
556 {
557 pointer __fb = __f.__ptr_;
558 pointer __fe = *__f.__m_iter_ + _B1;
559 difference_type __bs = __fe - __fb;
560 if (__bs > __n)
561 {
562 __bs = __n;
563 __fe = __fb + __bs;
564 }
565 __r = _STD::copy(__fb, __fe, __r);
566 __n -= __bs;
567 __f += __bs;
568 }
569 return __r;
570}
571
572template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
573 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
574__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
575copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
576 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
577 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
578{
579 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
580 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
581 difference_type __n = __l - __f;
582 while (__n > 0)
583 {
584 pointer __fb = __f.__ptr_;
585 pointer __fe = *__f.__m_iter_ + _B1;
586 difference_type __bs = __fe - __fb;
587 if (__bs > __n)
588 {
589 __bs = __n;
590 __fe = __fb + __bs;
591 }
592 __r = _STD::copy(__fb, __fe, __r);
593 __n -= __bs;
594 __f += __bs;
595 }
596 return __r;
597}
598
599// copy_backward
600
601template <class _RAIter,
602 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
603__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
604copy_backward(_RAIter __f,
605 _RAIter __l,
606 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
607 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
608{
609 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
610 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
611 while (__f != __l)
612 {
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +0000613 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _STD::prev(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000614 pointer __rb = *__rp.__m_iter_;
615 pointer __re = __rp.__ptr_ + 1;
616 difference_type __bs = __re - __rb;
617 difference_type __n = __l - __f;
618 _RAIter __m = __f;
619 if (__n > __bs)
620 {
621 __n = __bs;
622 __m = __l - __n;
623 }
624 _STD::copy_backward(__m, __l, __re);
625 __l = __m;
626 __r -= __n;
627 }
628 return __r;
629}
630
631template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
632 class _OutputIterator>
633_OutputIterator
634copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
635 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
636 _OutputIterator __r)
637{
638 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
639 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
640 difference_type __n = __l - __f;
641 while (__n > 0)
642 {
643 --__l;
644 pointer __lb = *__l.__m_iter_;
645 pointer __le = __l.__ptr_ + 1;
646 difference_type __bs = __le - __lb;
647 if (__bs > __n)
648 {
649 __bs = __n;
650 __lb = __le - __bs;
651 }
652 __r = _STD::copy_backward(__lb, __le, __r);
653 __n -= __bs;
654 __l -= __bs - 1;
655 }
656 return __r;
657}
658
659template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
660 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
661__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
662copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
663 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
664 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
665{
666 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
667 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
668 difference_type __n = __l - __f;
669 while (__n > 0)
670 {
671 --__l;
672 pointer __lb = *__l.__m_iter_;
673 pointer __le = __l.__ptr_ + 1;
674 difference_type __bs = __le - __lb;
675 if (__bs > __n)
676 {
677 __bs = __n;
678 __lb = __le - __bs;
679 }
680 __r = _STD::copy_backward(__lb, __le, __r);
681 __n -= __bs;
682 __l -= __bs - 1;
683 }
684 return __r;
685}
686
687// move
688
689template <class _RAIter,
690 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
691__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
692move(_RAIter __f,
693 _RAIter __l,
694 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
695 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
696{
697 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
698 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
699 while (__f != __l)
700 {
701 pointer __rb = __r.__ptr_;
702 pointer __re = *__r.__m_iter_ + _B2;
703 difference_type __bs = __re - __rb;
704 difference_type __n = __l - __f;
705 _RAIter __m = __l;
706 if (__n > __bs)
707 {
708 __n = __bs;
709 __m = __f + __n;
710 }
711 _STD::move(__f, __m, __rb);
712 __f = __m;
713 __r += __n;
714 }
715 return __r;
716}
717
718template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
719 class _OutputIterator>
720_OutputIterator
721move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
722 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
723 _OutputIterator __r)
724{
725 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
726 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
727 difference_type __n = __l - __f;
728 while (__n > 0)
729 {
730 pointer __fb = __f.__ptr_;
731 pointer __fe = *__f.__m_iter_ + _B1;
732 difference_type __bs = __fe - __fb;
733 if (__bs > __n)
734 {
735 __bs = __n;
736 __fe = __fb + __bs;
737 }
738 __r = _STD::move(__fb, __fe, __r);
739 __n -= __bs;
740 __f += __bs;
741 }
742 return __r;
743}
744
745template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
746 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
747__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
748move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
749 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
750 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
751{
752 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
753 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
754 difference_type __n = __l - __f;
755 while (__n > 0)
756 {
757 pointer __fb = __f.__ptr_;
758 pointer __fe = *__f.__m_iter_ + _B1;
759 difference_type __bs = __fe - __fb;
760 if (__bs > __n)
761 {
762 __bs = __n;
763 __fe = __fb + __bs;
764 }
765 __r = _STD::move(__fb, __fe, __r);
766 __n -= __bs;
767 __f += __bs;
768 }
769 return __r;
770}
771
772// move_backward
773
774template <class _RAIter,
775 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
776__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
777move_backward(_RAIter __f,
778 _RAIter __l,
779 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
780 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
781{
782 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
783 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
784 while (__f != __l)
785 {
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +0000786 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _STD::prev(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000787 pointer __rb = *__rp.__m_iter_;
788 pointer __re = __rp.__ptr_ + 1;
789 difference_type __bs = __re - __rb;
790 difference_type __n = __l - __f;
791 _RAIter __m = __f;
792 if (__n > __bs)
793 {
794 __n = __bs;
795 __m = __l - __n;
796 }
797 _STD::move_backward(__m, __l, __re);
798 __l = __m;
799 __r -= __n;
800 }
801 return __r;
802}
803
804template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
805 class _OutputIterator>
806_OutputIterator
807move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
808 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
809 _OutputIterator __r)
810{
811 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
812 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
813 difference_type __n = __l - __f;
814 while (__n > 0)
815 {
816 --__l;
817 pointer __lb = *__l.__m_iter_;
818 pointer __le = __l.__ptr_ + 1;
819 difference_type __bs = __le - __lb;
820 if (__bs > __n)
821 {
822 __bs = __n;
823 __lb = __le - __bs;
824 }
825 __r = _STD::move_backward(__lb, __le, __r);
826 __n -= __bs;
827 __l -= __bs - 1;
828 }
829 return __r;
830}
831
832template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
833 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
834__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
835move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
836 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
837 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
838{
839 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
840 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
841 difference_type __n = __l - __f;
842 while (__n > 0)
843 {
844 --__l;
845 pointer __lb = *__l.__m_iter_;
846 pointer __le = __l.__ptr_ + 1;
847 difference_type __bs = __le - __lb;
848 if (__bs > __n)
849 {
850 __bs = __n;
851 __lb = __le - __bs;
852 }
853 __r = _STD::move_backward(__lb, __le, __r);
854 __n -= __bs;
855 __l -= __bs - 1;
856 }
857 return __r;
858}
859
860template <bool>
861class __deque_base_common
862{
863protected:
864 void __throw_length_error() const;
865 void __throw_out_of_range() const;
866};
867
868template <bool __b>
869void
870__deque_base_common<__b>::__throw_length_error() const
871{
872#ifndef _LIBCPP_NO_EXCEPTIONS
873 throw length_error("deque");
874#endif
875}
876
877template <bool __b>
878void
879__deque_base_common<__b>::__throw_out_of_range() const
880{
881#ifndef _LIBCPP_NO_EXCEPTIONS
882 throw out_of_range("deque");
883#endif
884}
885
886template <class _Tp, class _Allocator>
887class __deque_base
888 : protected __deque_base_common<true>
889{
890 __deque_base(const __deque_base& __c);
891 __deque_base& operator=(const __deque_base& __c);
892protected:
893 typedef _Tp value_type;
894 typedef _Allocator allocator_type;
895 typedef allocator_traits<allocator_type> __alloc_traits;
896 typedef value_type& reference;
897 typedef const value_type& const_reference;
898 typedef typename __alloc_traits::size_type size_type;
899 typedef typename __alloc_traits::difference_type difference_type;
900 typedef typename __alloc_traits::pointer pointer;
901 typedef typename __alloc_traits::const_pointer const_pointer;
902
903 static const difference_type __block_size = sizeof(value_type) < 256 ? 4096 / sizeof(value_type) : 16;
904
905 typedef typename __alloc_traits::template
906#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
907 rebind_alloc<pointer>
908#else
909 rebind_alloc<pointer>::other
910#endif
911 __pointer_allocator;
912 typedef allocator_traits<__pointer_allocator> __map_traits;
913 typedef typename __map_traits::pointer __map_pointer;
914 typedef typename __map_traits::const_pointer __map_const_pointer;
915 typedef __split_buffer<pointer, __pointer_allocator> __map;
916
917 typedef __deque_iterator<value_type, pointer, reference, __map_pointer,
918 difference_type, __block_size> iterator;
919 typedef __deque_iterator<value_type, const_pointer, const_reference, __map_const_pointer,
920 difference_type, __block_size> const_iterator;
921
922 __map __map_;
923 size_type __start_;
924 __compressed_pair<size_type, allocator_type> __size_;
925
Howard Hinnanta12beb32011-06-02 16:10:22 +0000926 iterator begin() _NOEXCEPT;
927 const_iterator begin() const _NOEXCEPT;
928 iterator end() _NOEXCEPT;
929 const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000930
931 _LIBCPP_INLINE_VISIBILITY size_type& size() {return __size_.first();}
Howard Hinnanta12beb32011-06-02 16:10:22 +0000932 _LIBCPP_INLINE_VISIBILITY
933 const size_type& size() const _NOEXCEPT {return __size_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000934 _LIBCPP_INLINE_VISIBILITY allocator_type& __alloc() {return __size_.second();}
Howard Hinnanta12beb32011-06-02 16:10:22 +0000935 _LIBCPP_INLINE_VISIBILITY
936 const allocator_type& __alloc() const _NOEXCEPT {return __size_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000937
Howard Hinnant009b2c42011-06-03 15:16:49 +0000938 __deque_base()
939 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000940 explicit __deque_base(const allocator_type& __a);
Howard Hinnant0a612b02011-06-02 20:00:14 +0000941public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000942 ~__deque_base();
943
Howard Hinnant73d21a42010-09-04 23:28:19 +0000944#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000945
Howard Hinnant0a612b02011-06-02 20:00:14 +0000946 __deque_base(__deque_base&& __c)
947 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000948 __deque_base(__deque_base&& __c, const allocator_type& __a);
949
Howard Hinnant73d21a42010-09-04 23:28:19 +0000950#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant0a612b02011-06-02 20:00:14 +0000951 void swap(__deque_base& __c)
Howard Hinnantb965fed2011-06-03 16:20:53 +0000952 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Howard Hinnant0a612b02011-06-02 20:00:14 +0000953 __is_nothrow_swappable<allocator_type>::value);
954protected:
Howard Hinnanta12beb32011-06-02 16:10:22 +0000955 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000956
957 bool __invariants() const;
958
Howard Hinnant422a53f2010-09-21 21:28:23 +0000959 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000960 void __move_assign(__deque_base& __c)
Howard Hinnant18884f42011-06-02 21:38:57 +0000961 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
962 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000963 {
964 __map_ = _STD::move(__c.__map_);
965 __start_ = __c.__start_;
966 size() = __c.size();
967 __move_assign_alloc(__c);
968 __c.__start_ = __c.size() = 0;
969 }
970
Howard Hinnant422a53f2010-09-21 21:28:23 +0000971 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000972 void __move_assign_alloc(__deque_base& __c)
Howard Hinnant0a612b02011-06-02 20:00:14 +0000973 _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
974 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000975 {__move_assign_alloc(__c, integral_constant<bool,
976 __alloc_traits::propagate_on_container_move_assignment::value>());}
977
978private:
Howard Hinnant422a53f2010-09-21 21:28:23 +0000979 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000980 void __move_assign_alloc(const __deque_base& __c, true_type)
Howard Hinnant0a612b02011-06-02 20:00:14 +0000981 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000982 {
983 __alloc() = _STD::move(__c.__alloc());
984 }
985
Howard Hinnant422a53f2010-09-21 21:28:23 +0000986 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0a612b02011-06-02 20:00:14 +0000987 void __move_assign_alloc(const __deque_base& __c, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000988 {}
989
Howard Hinnant422a53f2010-09-21 21:28:23 +0000990 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000991 static void __swap_alloc(allocator_type& __x, allocator_type& __y)
Howard Hinnant0a612b02011-06-02 20:00:14 +0000992 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
993 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000994 {__swap_alloc(__x, __y, integral_constant<bool,
995 __alloc_traits::propagate_on_container_swap::value>());}
996
Howard Hinnant422a53f2010-09-21 21:28:23 +0000997 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000998 static void __swap_alloc(allocator_type& __x, allocator_type& __y, true_type)
Howard Hinnant0a612b02011-06-02 20:00:14 +0000999 _NOEXCEPT_(__is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001000 {
1001 using _STD::swap;
1002 swap(__x, __y);
1003 }
Howard Hinnant422a53f2010-09-21 21:28:23 +00001004
1005 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001006 static void __swap_alloc(allocator_type& __x, allocator_type& __y, false_type)
Howard Hinnant0a612b02011-06-02 20:00:14 +00001007 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001008 {}
1009};
1010
1011template <class _Tp, class _Allocator>
1012bool
1013__deque_base<_Tp, _Allocator>::__invariants() const
1014{
1015 if (!__map_.__invariants())
1016 return false;
1017 if (__map_.size() >= size_type(-1) / __block_size)
1018 return false;
1019 for (typename __map::const_iterator __i = __map_.begin(), __e = __map_.end();
1020 __i != __e; ++__i)
1021 if (*__i == nullptr)
1022 return false;
1023 if (__map_.size() != 0)
1024 {
1025 if (size() >= __map_.size() * __block_size)
1026 return false;
1027 if (__start_ >= __map_.size() * __block_size - size())
1028 return false;
1029 }
1030 else
1031 {
1032 if (size() != 0)
1033 return false;
1034 if (__start_ != 0)
1035 return false;
1036 }
1037 return true;
1038}
1039
1040template <class _Tp, class _Allocator>
1041typename __deque_base<_Tp, _Allocator>::iterator
Howard Hinnanta12beb32011-06-02 16:10:22 +00001042__deque_base<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001043{
1044 __map_pointer __mp = __map_.begin() + __start_ / __block_size;
1045 return iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
1046}
1047
1048template <class _Tp, class _Allocator>
1049typename __deque_base<_Tp, _Allocator>::const_iterator
Howard Hinnanta12beb32011-06-02 16:10:22 +00001050__deque_base<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001051{
1052 __map_const_pointer __mp = __map_.begin() + __start_ / __block_size;
1053 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
1054}
1055
1056template <class _Tp, class _Allocator>
1057typename __deque_base<_Tp, _Allocator>::iterator
Howard Hinnanta12beb32011-06-02 16:10:22 +00001058__deque_base<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001059{
1060 size_type __p = size() + __start_;
1061 __map_pointer __mp = __map_.begin() + __p / __block_size;
1062 return iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1063}
1064
1065template <class _Tp, class _Allocator>
1066typename __deque_base<_Tp, _Allocator>::const_iterator
Howard Hinnanta12beb32011-06-02 16:10:22 +00001067__deque_base<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001068{
1069 size_type __p = size() + __start_;
1070 __map_const_pointer __mp = __map_.begin() + __p / __block_size;
1071 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1072}
1073
1074template <class _Tp, class _Allocator>
1075inline _LIBCPP_INLINE_VISIBILITY
1076__deque_base<_Tp, _Allocator>::__deque_base()
Howard Hinnant009b2c42011-06-03 15:16:49 +00001077 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001078 : __start_(0), __size_(0) {}
1079
1080template <class _Tp, class _Allocator>
1081inline _LIBCPP_INLINE_VISIBILITY
1082__deque_base<_Tp, _Allocator>::__deque_base(const allocator_type& __a)
1083 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {}
1084
1085template <class _Tp, class _Allocator>
1086__deque_base<_Tp, _Allocator>::~__deque_base()
1087{
1088 clear();
1089 typename __map::iterator __i = __map_.begin();
1090 typename __map::iterator __e = __map_.end();
1091 for (; __i != __e; ++__i)
1092 __alloc_traits::deallocate(__alloc(), *__i, __block_size);
1093}
1094
Howard Hinnant73d21a42010-09-04 23:28:19 +00001095#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001096
1097template <class _Tp, class _Allocator>
1098__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c)
Howard Hinnant0a612b02011-06-02 20:00:14 +00001099 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001100 : __map_(_STD::move(__c.__map_)),
1101 __start_(_STD::move(__c.__start_)),
1102 __size_(_STD::move(__c.__size_))
1103{
1104 __c.__start_ = 0;
1105 __c.size() = 0;
1106}
1107
1108template <class _Tp, class _Allocator>
1109__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c, const allocator_type& __a)
1110 : __map_(_STD::move(__c.__map_), __pointer_allocator(__a)),
1111 __start_(_STD::move(__c.__start_)),
1112 __size_(_STD::move(__c.size()), __a)
1113{
1114 if (__a == __c.__alloc())
1115 {
1116 __c.__start_ = 0;
1117 __c.size() = 0;
1118 }
1119 else
1120 {
1121 __map_.clear();
1122 __start_ = 0;
1123 size() = 0;
1124 }
1125}
1126
Howard Hinnant73d21a42010-09-04 23:28:19 +00001127#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001128
1129template <class _Tp, class _Allocator>
1130void
1131__deque_base<_Tp, _Allocator>::swap(__deque_base& __c)
Howard Hinnant0a612b02011-06-02 20:00:14 +00001132 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
1133 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001134{
1135 __map_.swap(__c.__map_);
1136 _STD::swap(__start_, __c.__start_);
1137 _STD::swap(size(), __c.size());
1138 __swap_alloc(__alloc(), __c.__alloc());
1139}
1140
1141template <class _Tp, class _Allocator>
1142void
Howard Hinnanta12beb32011-06-02 16:10:22 +00001143__deque_base<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001144{
1145 allocator_type& __a = __alloc();
1146 for (iterator __i = begin(), __e = end(); __i != __e; ++__i)
Howard Hinnant2529d022011-02-02 17:36:20 +00001147 __alloc_traits::destroy(__a, _STD::addressof(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001148 size() = 0;
1149 while (__map_.size() > 2)
1150 {
1151 __alloc_traits::deallocate(__a, __map_.front(), __block_size);
1152 __map_.pop_front();
1153 }
1154 switch (__map_.size())
1155 {
1156 case 1:
1157 __start_ = __block_size / 2;
1158 break;
1159 case 2:
1160 __start_ = __block_size;
1161 break;
1162 }
1163}
1164
1165template <class _Tp, class _Allocator = allocator<_Tp> >
Howard Hinnant422a53f2010-09-21 21:28:23 +00001166class _LIBCPP_VISIBLE deque
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001167 : private __deque_base<_Tp, _Allocator>
1168{
1169public:
1170 // types:
Howard Hinnant324bb032010-08-22 00:02:43 +00001171
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001172 typedef _Tp value_type;
1173 typedef _Allocator allocator_type;
1174
1175 typedef __deque_base<value_type, allocator_type> __base;
1176
1177 typedef typename __base::__alloc_traits __alloc_traits;
1178 typedef typename __base::reference reference;
1179 typedef typename __base::const_reference const_reference;
1180 typedef typename __base::iterator iterator;
1181 typedef typename __base::const_iterator const_iterator;
1182 typedef typename __base::size_type size_type;
1183 typedef typename __base::difference_type difference_type;
1184
1185 typedef typename __base::pointer pointer;
1186 typedef typename __base::const_pointer const_pointer;
1187 typedef _STD::reverse_iterator<iterator> reverse_iterator;
1188 typedef _STD::reverse_iterator<const_iterator> const_reverse_iterator;
1189
1190 // construct/copy/destroy:
Howard Hinnant009b2c42011-06-03 15:16:49 +00001191 _LIBCPP_INLINE_VISIBILITY
1192 deque()
1193 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
1194 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001195 _LIBCPP_INLINE_VISIBILITY deque(const allocator_type& __a) : __base(__a) {}
1196 explicit deque(size_type __n);
1197 deque(size_type __n, const value_type& __v);
1198 deque(size_type __n, const value_type& __v, const allocator_type& __a);
1199 template <class _InputIter>
1200 deque(_InputIter __f, _InputIter __l,
1201 typename enable_if<__is_input_iterator<_InputIter>::value>::type* = 0);
1202 template <class _InputIter>
1203 deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1204 typename enable_if<__is_input_iterator<_InputIter>::value>::type* = 0);
1205 deque(const deque& __c);
1206 deque(const deque& __c, const allocator_type& __a);
1207 deque(initializer_list<value_type> __il);
1208 deque(initializer_list<value_type> __il, const allocator_type& __a);
1209
1210 deque& operator=(const deque& __c);
Howard Hinnant422a53f2010-09-21 21:28:23 +00001211 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001212 deque& operator=(initializer_list<value_type> __il) {assign(__il); return *this;}
1213
Howard Hinnant73d21a42010-09-04 23:28:19 +00001214#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant0a612b02011-06-02 20:00:14 +00001215 deque(deque&& __c) _NOEXCEPT_(is_nothrow_move_constructible<__base>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001216 deque(deque&& __c, const allocator_type& __a);
Howard Hinnant0a612b02011-06-02 20:00:14 +00001217 deque& operator=(deque&& __c)
Howard Hinnant18884f42011-06-02 21:38:57 +00001218 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1219 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001220#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001221
1222 template <class _InputIter>
1223 void assign(_InputIter __f, _InputIter __l,
1224 typename enable_if<__is_input_iterator<_InputIter>::value &&
1225 !__is_random_access_iterator<_InputIter>::value>::type* = 0);
1226 template <class _RAIter>
1227 void assign(_RAIter __f, _RAIter __l,
1228 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
1229 void assign(size_type __n, const value_type& __v);
Howard Hinnant422a53f2010-09-21 21:28:23 +00001230 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001231 void assign(initializer_list<value_type> __il) {assign(__il.begin(), __il.end());}
1232
Howard Hinnanta12beb32011-06-02 16:10:22 +00001233 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001234
1235 // iterators:
1236
Howard Hinnant422a53f2010-09-21 21:28:23 +00001237 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001238 iterator begin() _NOEXCEPT {return __base::begin();}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001239 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001240 const_iterator begin() const _NOEXCEPT {return __base::begin();}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001241 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001242 iterator end() _NOEXCEPT {return __base::end();}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001243 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001244 const_iterator end() const _NOEXCEPT {return __base::end();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001245
Howard Hinnant422a53f2010-09-21 21:28:23 +00001246 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001247 reverse_iterator rbegin() _NOEXCEPT
1248 {return reverse_iterator(__base::end());}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001250 const_reverse_iterator rbegin() const _NOEXCEPT
1251 {return const_reverse_iterator(__base::end());}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001252 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001253 reverse_iterator rend() _NOEXCEPT
1254 {return reverse_iterator(__base::begin());}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001255 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001256 const_reverse_iterator rend() const _NOEXCEPT
1257 {return const_reverse_iterator(__base::begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001258
Howard Hinnant422a53f2010-09-21 21:28:23 +00001259 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001260 const_iterator cbegin() const _NOEXCEPT
1261 {return __base::begin();}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001262 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001263 const_iterator cend() const _NOEXCEPT
1264 {return __base::end();}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001266 const_reverse_iterator crbegin() const _NOEXCEPT
1267 {return const_reverse_iterator(__base::end());}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001269 const_reverse_iterator crend() const _NOEXCEPT
1270 {return const_reverse_iterator(__base::begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001271
1272 // capacity:
Howard Hinnant422a53f2010-09-21 21:28:23 +00001273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001274 size_type size() const _NOEXCEPT {return __base::size();}
1275 _LIBCPP_INLINE_VISIBILITY
1276 size_type max_size() const _NOEXCEPT
1277 {return __alloc_traits::max_size(__base::__alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001278 void resize(size_type __n);
1279 void resize(size_type __n, const value_type& __v);
Howard Hinnant18884f42011-06-02 21:38:57 +00001280 void shrink_to_fit() _NOEXCEPT;
Howard Hinnanta12beb32011-06-02 16:10:22 +00001281 _LIBCPP_INLINE_VISIBILITY
1282 bool empty() const _NOEXCEPT {return __base::size() == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001283
1284 // element access:
1285 reference operator[](size_type __i);
1286 const_reference operator[](size_type __i) const;
1287 reference at(size_type __i);
1288 const_reference at(size_type __i) const;
1289 reference front();
1290 const_reference front() const;
1291 reference back();
1292 const_reference back() const;
1293
1294 // 23.2.2.3 modifiers:
1295 void push_front(const value_type& __v);
1296 void push_back(const value_type& __v);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001297#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1298#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001299 template <class... _Args> void emplace_front(_Args&&... __args);
1300 template <class... _Args> void emplace_back(_Args&&... __args);
1301 template <class... _Args> iterator emplace(const_iterator __p, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001302#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001303 void push_front(value_type&& __v);
1304 void push_back(value_type&& __v);
1305 iterator insert(const_iterator __p, value_type&& __v);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001306#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001307 iterator insert(const_iterator __p, const value_type& __v);
1308 iterator insert(const_iterator __p, size_type __n, const value_type& __v);
1309 template <class _InputIter>
1310 iterator insert (const_iterator __p, _InputIter __f, _InputIter __l,
1311 typename enable_if<__is_input_iterator<_InputIter>::value
1312 &&!__is_bidirectional_iterator<_InputIter>::value>::type* = 0);
1313 template <class _BiIter>
1314 iterator insert (const_iterator __p, _BiIter __f, _BiIter __l,
1315 typename enable_if<__is_bidirectional_iterator<_BiIter>::value>::type* = 0);
Howard Hinnant422a53f2010-09-21 21:28:23 +00001316 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001317 iterator insert(const_iterator __p, initializer_list<value_type> __il)
1318 {return insert(__p, __il.begin(), __il.end());}
1319 void pop_front();
1320 void pop_back();
1321 iterator erase(const_iterator __p);
1322 iterator erase(const_iterator __f, const_iterator __l);
1323
Howard Hinnant0a612b02011-06-02 20:00:14 +00001324 void swap(deque& __c)
1325 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1326 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnanta12beb32011-06-02 16:10:22 +00001327 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001328
Howard Hinnant422a53f2010-09-21 21:28:23 +00001329 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001330 bool __invariants() const {return __base::__invariants();}
1331private:
Howard Hinnant422a53f2010-09-21 21:28:23 +00001332 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001333 static size_type __recommend_blocks(size_type __n)
1334 {
1335 return __n / __base::__block_size + (__n % __base::__block_size != 0);
1336 }
Howard Hinnant422a53f2010-09-21 21:28:23 +00001337 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001338 size_type __capacity() const
1339 {
1340 return __base::__map_.size() == 0 ? 0 : __base::__map_.size() * __base::__block_size - 1;
1341 }
Howard Hinnant422a53f2010-09-21 21:28:23 +00001342 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001343 size_type __front_spare() const
1344 {
1345 return __base::__start_;
1346 }
Howard Hinnant422a53f2010-09-21 21:28:23 +00001347 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001348 size_type __back_spare() const
1349 {
1350 return __capacity() - (__base::__start_ + __base::size());
1351 }
1352
1353 template <class _InpIter>
1354 void __append(_InpIter __f, _InpIter __l,
1355 typename enable_if<__is_input_iterator<_InpIter>::value &&
1356 !__is_forward_iterator<_InpIter>::value>::type* = 0);
1357 template <class _ForIter>
1358 void __append(_ForIter __f, _ForIter __l,
1359 typename enable_if<__is_forward_iterator<_ForIter>::value>::type* = 0);
1360 void __append(size_type __n);
1361 void __append(size_type __n, const value_type& __v);
1362 void __erase_to_end(const_iterator __f);
1363 void __add_front_capacity();
1364 void __add_front_capacity(size_type __n);
1365 void __add_back_capacity();
1366 void __add_back_capacity(size_type __n);
1367 iterator __move_and_check(iterator __f, iterator __l, iterator __r,
1368 const_pointer& __vt);
1369 iterator __move_backward_and_check(iterator __f, iterator __l, iterator __r,
1370 const_pointer& __vt);
1371 void __move_construct_and_check(iterator __f, iterator __l,
1372 iterator __r, const_pointer& __vt);
1373 void __move_construct_backward_and_check(iterator __f, iterator __l,
1374 iterator __r, const_pointer& __vt);
1375
Howard Hinnant422a53f2010-09-21 21:28:23 +00001376 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001377 void __copy_assign_alloc(const deque& __c)
1378 {__copy_assign_alloc(__c, integral_constant<bool,
1379 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1380
Howard Hinnant422a53f2010-09-21 21:28:23 +00001381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001382 void __copy_assign_alloc(const deque& __c, true_type)
1383 {
1384 if (__base::__alloc() != __c.__alloc())
1385 {
1386 clear();
1387 shrink_to_fit();
1388 }
1389 __base::__alloc() = __c.__alloc();
1390 __base::__map_.__alloc() = __c.__map_.__alloc();
1391 }
1392
Howard Hinnant422a53f2010-09-21 21:28:23 +00001393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001394 void __copy_assign_alloc(const deque& __c, false_type)
1395 {}
1396
Howard Hinnant18884f42011-06-02 21:38:57 +00001397 void __move_assign(deque& __c, true_type)
1398 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001399 void __move_assign(deque& __c, false_type);
1400};
1401
1402template <class _Tp, class _Allocator>
1403deque<_Tp, _Allocator>::deque(size_type __n)
1404{
1405 if (__n > 0)
1406 __append(__n);
1407}
1408
1409template <class _Tp, class _Allocator>
1410deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v)
1411{
1412 if (__n > 0)
1413 __append(__n, __v);
1414}
1415
1416template <class _Tp, class _Allocator>
1417deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v, const allocator_type& __a)
1418 : __base(__a)
1419{
1420 if (__n > 0)
1421 __append(__n, __v);
1422}
1423
1424template <class _Tp, class _Allocator>
1425template <class _InputIter>
1426deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l,
1427 typename enable_if<__is_input_iterator<_InputIter>::value>::type*)
1428{
1429 __append(__f, __l);
1430}
1431
1432template <class _Tp, class _Allocator>
1433template <class _InputIter>
1434deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1435 typename enable_if<__is_input_iterator<_InputIter>::value>::type*)
1436 : __base(__a)
1437{
1438 __append(__f, __l);
1439}
1440
1441template <class _Tp, class _Allocator>
1442deque<_Tp, _Allocator>::deque(const deque& __c)
1443 : __base(__alloc_traits::select_on_container_copy_construction(__c.__alloc()))
1444{
1445 __append(__c.begin(), __c.end());
1446}
1447
1448template <class _Tp, class _Allocator>
1449deque<_Tp, _Allocator>::deque(const deque& __c, const allocator_type& __a)
1450 : __base(__a)
1451{
1452 __append(__c.begin(), __c.end());
1453}
1454
1455template <class _Tp, class _Allocator>
1456deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il)
1457{
1458 __append(__il.begin(), __il.end());
1459}
1460
1461template <class _Tp, class _Allocator>
1462deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il, const allocator_type& __a)
1463 : __base(__a)
1464{
1465 __append(__il.begin(), __il.end());
1466}
1467
1468template <class _Tp, class _Allocator>
1469deque<_Tp, _Allocator>&
1470deque<_Tp, _Allocator>::operator=(const deque& __c)
1471{
1472 if (this != &__c)
1473 {
1474 __copy_assign_alloc(__c);
1475 assign(__c.begin(), __c.end());
1476 }
1477 return *this;
1478}
1479
Howard Hinnant73d21a42010-09-04 23:28:19 +00001480#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001481
1482template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001483inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001484deque<_Tp, _Allocator>::deque(deque&& __c)
Howard Hinnant0a612b02011-06-02 20:00:14 +00001485 _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001486 : __base(_STD::move(__c))
1487{
1488}
1489
1490template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001491inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001492deque<_Tp, _Allocator>::deque(deque&& __c, const allocator_type& __a)
1493 : __base(_STD::move(__c), __a)
1494{
1495 if (__a != __c.__alloc())
1496 {
1497 typedef move_iterator<iterator> _I;
1498 assign(_I(__c.begin()), _I(__c.end()));
1499 }
1500}
1501
1502template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001503inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001504deque<_Tp, _Allocator>&
1505deque<_Tp, _Allocator>::operator=(deque&& __c)
Howard Hinnant18884f42011-06-02 21:38:57 +00001506 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1507 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001508{
1509 __move_assign(__c, integral_constant<bool,
1510 __alloc_traits::propagate_on_container_move_assignment::value>());
1511 return *this;
1512}
1513
1514template <class _Tp, class _Allocator>
1515void
1516deque<_Tp, _Allocator>::__move_assign(deque& __c, false_type)
1517{
1518 if (__base::__alloc() != __c.__alloc())
1519 {
1520 typedef move_iterator<iterator> _I;
1521 assign(_I(__c.begin()), _I(__c.end()));
1522 }
1523 else
1524 __move_assign(__c, true_type());
1525}
1526
1527template <class _Tp, class _Allocator>
1528void
1529deque<_Tp, _Allocator>::__move_assign(deque& __c, true_type)
Howard Hinnant18884f42011-06-02 21:38:57 +00001530 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001531{
1532 clear();
1533 shrink_to_fit();
1534 __base::__move_assign(__c);
1535}
1536
Howard Hinnant73d21a42010-09-04 23:28:19 +00001537#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001538
1539template <class _Tp, class _Allocator>
1540template <class _InputIter>
1541void
1542deque<_Tp, _Allocator>::assign(_InputIter __f, _InputIter __l,
1543 typename enable_if<__is_input_iterator<_InputIter>::value &&
1544 !__is_random_access_iterator<_InputIter>::value>::type*)
1545{
1546 iterator __i = __base::begin();
1547 iterator __e = __base::end();
1548 for (; __f != __l && __i != __e; ++__f, ++__i)
1549 *__i = *__f;
1550 if (__f != __l)
1551 __append(__f, __l);
1552 else
1553 __erase_to_end(__i);
1554}
1555
1556template <class _Tp, class _Allocator>
1557template <class _RAIter>
1558void
1559deque<_Tp, _Allocator>::assign(_RAIter __f, _RAIter __l,
1560 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
1561{
1562 if (static_cast<size_type>(__l - __f) > __base::size())
1563 {
1564 _RAIter __m = __f + __base::size();
1565 _STD::copy(__f, __m, __base::begin());
1566 __append(__m, __l);
1567 }
1568 else
1569 __erase_to_end(_STD::copy(__f, __l, __base::begin()));
1570}
1571
1572template <class _Tp, class _Allocator>
1573void
1574deque<_Tp, _Allocator>::assign(size_type __n, const value_type& __v)
1575{
1576 if (__n > __base::size())
1577 {
1578 _STD::fill_n(__base::begin(), __base::size(), __v);
1579 __n -= __base::size();
1580 __append(__n, __v);
1581 }
1582 else
1583 __erase_to_end(_STD::fill_n(__base::begin(), __n, __v));
1584}
1585
1586template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001587inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001588_Allocator
Howard Hinnanta12beb32011-06-02 16:10:22 +00001589deque<_Tp, _Allocator>::get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001590{
1591 return __base::__alloc();
1592}
1593
1594template <class _Tp, class _Allocator>
1595void
1596deque<_Tp, _Allocator>::resize(size_type __n)
1597{
1598 if (__n > __base::size())
1599 __append(__n - __base::size());
1600 else if (__n < __base::size())
1601 __erase_to_end(__base::begin() + __n);
1602}
1603
1604template <class _Tp, class _Allocator>
1605void
1606deque<_Tp, _Allocator>::resize(size_type __n, const value_type& __v)
1607{
1608 if (__n > __base::size())
1609 __append(__n - __base::size(), __v);
1610 else if (__n < __base::size())
1611 __erase_to_end(__base::begin() + __n);
1612}
1613
1614template <class _Tp, class _Allocator>
1615void
Howard Hinnant18884f42011-06-02 21:38:57 +00001616deque<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001617{
1618 allocator_type& __a = __base::__alloc();
1619 if (empty())
1620 {
1621 while (__base::__map_.size() > 0)
1622 {
1623 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1624 __base::__map_.pop_back();
1625 }
1626 __base::__start_ = 0;
1627 }
1628 else
1629 {
1630 if (__front_spare() >= __base::__block_size)
1631 {
1632 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
1633 __base::__map_.pop_front();
1634 __base::__start_ -= __base::__block_size;
1635 }
1636 if (__back_spare() >= __base::__block_size)
1637 {
1638 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1639 __base::__map_.pop_back();
1640 }
1641 }
1642 __base::__map_.shrink_to_fit();
1643}
1644
1645template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001646inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001647typename deque<_Tp, _Allocator>::reference
1648deque<_Tp, _Allocator>::operator[](size_type __i)
1649{
1650 size_type __p = __base::__start_ + __i;
1651 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1652}
1653
1654template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001655inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001656typename deque<_Tp, _Allocator>::const_reference
1657deque<_Tp, _Allocator>::operator[](size_type __i) const
1658{
1659 size_type __p = __base::__start_ + __i;
1660 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1661}
1662
1663template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001664inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001665typename deque<_Tp, _Allocator>::reference
1666deque<_Tp, _Allocator>::at(size_type __i)
1667{
1668 if (__i >= __base::size())
1669 __base::__throw_out_of_range();
1670 size_type __p = __base::__start_ + __i;
1671 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1672}
1673
1674template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001675inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001676typename deque<_Tp, _Allocator>::const_reference
1677deque<_Tp, _Allocator>::at(size_type __i) const
1678{
1679 if (__i >= __base::size())
1680 __base::__throw_out_of_range();
1681 size_type __p = __base::__start_ + __i;
1682 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1683}
1684
1685template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001686inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001687typename deque<_Tp, _Allocator>::reference
1688deque<_Tp, _Allocator>::front()
1689{
1690 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1691 + __base::__start_ % __base::__block_size);
1692}
1693
1694template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001695inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001696typename deque<_Tp, _Allocator>::const_reference
1697deque<_Tp, _Allocator>::front() const
1698{
1699 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1700 + __base::__start_ % __base::__block_size);
1701}
1702
1703template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001704inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001705typename deque<_Tp, _Allocator>::reference
1706deque<_Tp, _Allocator>::back()
1707{
1708 size_type __p = __base::size() + __base::__start_ - 1;
1709 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1710}
1711
1712template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001713inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001714typename deque<_Tp, _Allocator>::const_reference
1715deque<_Tp, _Allocator>::back() const
1716{
1717 size_type __p = __base::size() + __base::__start_ - 1;
1718 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1719}
1720
1721template <class _Tp, class _Allocator>
1722void
1723deque<_Tp, _Allocator>::push_back(const value_type& __v)
1724{
1725 allocator_type& __a = __base::__alloc();
1726 if (__back_spare() == 0)
1727 __add_back_capacity();
1728 // __back_spare() >= 1
Howard Hinnant2529d022011-02-02 17:36:20 +00001729 __alloc_traits::construct(__a, _STD::addressof(*__base::end()), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001730 ++__base::size();
1731}
1732
Howard Hinnant73d21a42010-09-04 23:28:19 +00001733#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001734
1735template <class _Tp, class _Allocator>
1736void
1737deque<_Tp, _Allocator>::push_back(value_type&& __v)
1738{
1739 allocator_type& __a = __base::__alloc();
1740 if (__back_spare() == 0)
1741 __add_back_capacity();
1742 // __back_spare() >= 1
Howard Hinnant2529d022011-02-02 17:36:20 +00001743 __alloc_traits::construct(__a, _STD::addressof(*__base::end()), _STD::move(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001744 ++__base::size();
1745}
1746
Howard Hinnant73d21a42010-09-04 23:28:19 +00001747#ifndef _LIBCPP_HAS_NO_VARIADICS
1748
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001749template <class _Tp, class _Allocator>
1750template <class... _Args>
1751void
1752deque<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1753{
1754 allocator_type& __a = __base::__alloc();
1755 if (__back_spare() == 0)
1756 __add_back_capacity();
1757 // __back_spare() >= 1
Howard Hinnant2529d022011-02-02 17:36:20 +00001758 __alloc_traits::construct(__a, _STD::addressof(*__base::end()), _STD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001759 ++__base::size();
1760}
1761
Howard Hinnant73d21a42010-09-04 23:28:19 +00001762#endif // _LIBCPP_HAS_NO_VARIADICS
1763#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001764
1765template <class _Tp, class _Allocator>
1766void
1767deque<_Tp, _Allocator>::push_front(const value_type& __v)
1768{
1769 allocator_type& __a = __base::__alloc();
1770 if (__front_spare() == 0)
1771 __add_front_capacity();
1772 // __front_spare() >= 1
Howard Hinnant2529d022011-02-02 17:36:20 +00001773 __alloc_traits::construct(__a, _STD::addressof(*--__base::begin()), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001774 --__base::__start_;
1775 ++__base::size();
1776}
1777
Howard Hinnant73d21a42010-09-04 23:28:19 +00001778#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001779
1780template <class _Tp, class _Allocator>
1781void
1782deque<_Tp, _Allocator>::push_front(value_type&& __v)
1783{
1784 allocator_type& __a = __base::__alloc();
1785 if (__front_spare() == 0)
1786 __add_front_capacity();
1787 // __front_spare() >= 1
Howard Hinnant2529d022011-02-02 17:36:20 +00001788 __alloc_traits::construct(__a, _STD::addressof(*--__base::begin()), _STD::move(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001789 --__base::__start_;
1790 ++__base::size();
1791}
1792
Howard Hinnant73d21a42010-09-04 23:28:19 +00001793#ifndef _LIBCPP_HAS_NO_VARIADICS
1794
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001795template <class _Tp, class _Allocator>
1796template <class... _Args>
1797void
1798deque<_Tp, _Allocator>::emplace_front(_Args&&... __args)
1799{
1800 allocator_type& __a = __base::__alloc();
1801 if (__front_spare() == 0)
1802 __add_front_capacity();
1803 // __front_spare() >= 1
Howard Hinnant2529d022011-02-02 17:36:20 +00001804 __alloc_traits::construct(__a, _STD::addressof(*--__base::begin()), _STD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001805 --__base::__start_;
1806 ++__base::size();
1807}
1808
Howard Hinnant73d21a42010-09-04 23:28:19 +00001809#endif // _LIBCPP_HAS_NO_VARIADICS
1810#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001811
1812template <class _Tp, class _Allocator>
1813typename deque<_Tp, _Allocator>::iterator
1814deque<_Tp, _Allocator>::insert(const_iterator __p, const value_type& __v)
1815{
1816 size_type __pos = __p - __base::begin();
1817 size_type __to_end = __base::size() - __pos;
1818 allocator_type& __a = __base::__alloc();
1819 if (__pos < __to_end)
1820 { // insert by shifting things backward
1821 if (__front_spare() == 0)
1822 __add_front_capacity();
1823 // __front_spare() >= 1
1824 if (__pos == 0)
1825 {
Howard Hinnant2529d022011-02-02 17:36:20 +00001826 __alloc_traits::construct(__a, _STD::addressof(*--__base::begin()), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001827 --__base::__start_;
1828 ++__base::size();
1829 }
1830 else
1831 {
1832 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1833 iterator __b = __base::begin();
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00001834 iterator __bm1 = _STD::prev(__b);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001835 if (__vt == pointer_traits<const_pointer>::pointer_to(*__b))
1836 __vt = pointer_traits<const_pointer>::pointer_to(*__bm1);
Howard Hinnant2529d022011-02-02 17:36:20 +00001837 __alloc_traits::construct(__a, _STD::addressof(*__bm1), _STD::move(*__b));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001838 --__base::__start_;
1839 ++__base::size();
1840 if (__pos > 1)
Douglas Gregor7ac6af72011-04-29 16:20:26 +00001841 __b = __move_and_check(_STD::next(__b), __b + __pos, __b, __vt);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001842 *__b = *__vt;
1843 }
1844 }
1845 else
1846 { // insert by shifting things forward
1847 if (__back_spare() == 0)
1848 __add_back_capacity();
1849 // __back_capacity >= 1
1850 size_type __de = __base::size() - __pos;
1851 if (__de == 0)
1852 {
Howard Hinnant2529d022011-02-02 17:36:20 +00001853 __alloc_traits::construct(__a, _STD::addressof(*__base::end()), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001854 ++__base::size();
1855 }
1856 else
1857 {
1858 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1859 iterator __e = __base::end();
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00001860 iterator __em1 = _STD::prev(__e);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001861 if (__vt == pointer_traits<const_pointer>::pointer_to(*__em1))
1862 __vt = pointer_traits<const_pointer>::pointer_to(*__e);
Howard Hinnant2529d022011-02-02 17:36:20 +00001863 __alloc_traits::construct(__a, _STD::addressof(*__e), _STD::move(*__em1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001864 ++__base::size();
1865 if (__de > 1)
1866 __e = __move_backward_and_check(__e - __de, __em1, __e, __vt);
1867 *--__e = *__vt;
1868 }
1869 }
1870 return __base::begin() + __pos;
1871}
1872
Howard Hinnant73d21a42010-09-04 23:28:19 +00001873#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001874
1875template <class _Tp, class _Allocator>
1876typename deque<_Tp, _Allocator>::iterator
1877deque<_Tp, _Allocator>::insert(const_iterator __p, value_type&& __v)
1878{
1879 size_type __pos = __p - __base::begin();
1880 size_type __to_end = __base::size() - __pos;
1881 allocator_type& __a = __base::__alloc();
1882 if (__pos < __to_end)
1883 { // insert by shifting things backward
1884 if (__front_spare() == 0)
1885 __add_front_capacity();
1886 // __front_spare() >= 1
1887 if (__pos == 0)
1888 {
Howard Hinnant2529d022011-02-02 17:36:20 +00001889 __alloc_traits::construct(__a, _STD::addressof(*--__base::begin()), _STD::move(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001890 --__base::__start_;
1891 ++__base::size();
1892 }
1893 else
1894 {
1895 iterator __b = __base::begin();
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00001896 iterator __bm1 = _STD::prev(__b);
Howard Hinnant2529d022011-02-02 17:36:20 +00001897 __alloc_traits::construct(__a, _STD::addressof(*__bm1), _STD::move(*__b));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001898 --__base::__start_;
1899 ++__base::size();
1900 if (__pos > 1)
Douglas Gregor7ac6af72011-04-29 16:20:26 +00001901 __b = _STD::move(_STD::next(__b), __b + __pos, __b);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001902 *__b = _STD::move(__v);
1903 }
1904 }
1905 else
1906 { // insert by shifting things forward
1907 if (__back_spare() == 0)
1908 __add_back_capacity();
1909 // __back_capacity >= 1
1910 size_type __de = __base::size() - __pos;
1911 if (__de == 0)
1912 {
Howard Hinnant2529d022011-02-02 17:36:20 +00001913 __alloc_traits::construct(__a, _STD::addressof(*__base::end()), _STD::move(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001914 ++__base::size();
1915 }
1916 else
1917 {
1918 iterator __e = __base::end();
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00001919 iterator __em1 = _STD::prev(__e);
Howard Hinnant2529d022011-02-02 17:36:20 +00001920 __alloc_traits::construct(__a, _STD::addressof(*__e), _STD::move(*__em1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001921 ++__base::size();
1922 if (__de > 1)
1923 __e = _STD::move_backward(__e - __de, __em1, __e);
1924 *--__e = _STD::move(__v);
1925 }
1926 }
1927 return __base::begin() + __pos;
1928}
1929
Howard Hinnant73d21a42010-09-04 23:28:19 +00001930#ifndef _LIBCPP_HAS_NO_VARIADICS
1931
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001932template <class _Tp, class _Allocator>
1933template <class... _Args>
1934typename deque<_Tp, _Allocator>::iterator
1935deque<_Tp, _Allocator>::emplace(const_iterator __p, _Args&&... __args)
1936{
1937 size_type __pos = __p - __base::begin();
1938 size_type __to_end = __base::size() - __pos;
1939 allocator_type& __a = __base::__alloc();
1940 if (__pos < __to_end)
1941 { // insert by shifting things backward
1942 if (__front_spare() == 0)
1943 __add_front_capacity();
1944 // __front_spare() >= 1
1945 if (__pos == 0)
1946 {
Howard Hinnant2529d022011-02-02 17:36:20 +00001947 __alloc_traits::construct(__a, _STD::addressof(*--__base::begin()), _STD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001948 --__base::__start_;
1949 ++__base::size();
1950 }
1951 else
1952 {
1953 iterator __b = __base::begin();
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00001954 iterator __bm1 = _STD::prev(__b);
Howard Hinnant2529d022011-02-02 17:36:20 +00001955 __alloc_traits::construct(__a, _STD::addressof(*__bm1), _STD::move(*__b));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001956 --__base::__start_;
1957 ++__base::size();
1958 if (__pos > 1)
Douglas Gregor7ac6af72011-04-29 16:20:26 +00001959 __b = _STD::move(_STD::next(__b), __b + __pos, __b);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001960 *__b = value_type(_STD::forward<_Args>(__args)...);
1961 }
1962 }
1963 else
1964 { // insert by shifting things forward
1965 if (__back_spare() == 0)
1966 __add_back_capacity();
1967 // __back_capacity >= 1
1968 size_type __de = __base::size() - __pos;
1969 if (__de == 0)
1970 {
Howard Hinnant2529d022011-02-02 17:36:20 +00001971 __alloc_traits::construct(__a, _STD::addressof(*__base::end()), _STD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001972 ++__base::size();
1973 }
1974 else
1975 {
1976 iterator __e = __base::end();
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00001977 iterator __em1 = _STD::prev(__e);
Howard Hinnant2529d022011-02-02 17:36:20 +00001978 __alloc_traits::construct(__a, _STD::addressof(*__e), _STD::move(*__em1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001979 ++__base::size();
1980 if (__de > 1)
1981 __e = _STD::move_backward(__e - __de, __em1, __e);
1982 *--__e = value_type(_STD::forward<_Args>(__args)...);
1983 }
1984 }
1985 return __base::begin() + __pos;
1986}
1987
Howard Hinnant73d21a42010-09-04 23:28:19 +00001988#endif // _LIBCPP_HAS_NO_VARIADICS
1989#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001990
1991template <class _Tp, class _Allocator>
1992typename deque<_Tp, _Allocator>::iterator
1993deque<_Tp, _Allocator>::insert(const_iterator __p, size_type __n, const value_type& __v)
1994{
1995 size_type __pos = __p - __base::begin();
1996 size_type __to_end = __base::size() - __pos;
1997 allocator_type& __a = __base::__alloc();
1998 if (__pos < __to_end)
1999 { // insert by shifting things backward
2000 if (__n > __front_spare())
2001 __add_front_capacity(__n - __front_spare());
2002 // __n <= __front_spare()
2003 size_type __old_n = __n;
2004 iterator __old_begin = __base::begin();
2005 iterator __i = __old_begin;
2006 if (__n > __pos)
2007 {
2008 for (size_type __m = __n - __pos; __m; --__m, --__base::__start_, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002009 __alloc_traits::construct(__a, _STD::addressof(*--__i), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002010 __n = __pos;
2011 }
2012 if (__n > 0)
2013 {
2014 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2015 iterator __obn = __old_begin + __n;
2016 __move_construct_backward_and_check(__old_begin, __obn, __i, __vt);
2017 if (__n < __pos)
2018 __old_begin = __move_and_check(__obn, __old_begin + __pos, __old_begin, __vt);
2019 _STD::fill_n(__old_begin, __n, *__vt);
2020 }
2021 }
2022 else
2023 { // insert by shifting things forward
2024 size_type __back_capacity = __back_spare();
2025 if (__n > __back_capacity)
2026 __add_back_capacity(__n - __back_capacity);
2027 // __n <= __back_capacity
2028 size_type __old_n = __n;
2029 iterator __old_end = __base::end();
2030 iterator __i = __old_end;
2031 size_type __de = __base::size() - __pos;
2032 if (__n > __de)
2033 {
2034 for (size_type __m = __n - __de; __m; --__m, ++__i, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002035 __alloc_traits::construct(__a, _STD::addressof(*__i), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002036 __n = __de;
2037 }
2038 if (__n > 0)
2039 {
2040 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2041 iterator __oen = __old_end - __n;
2042 __move_construct_and_check(__oen, __old_end, __i, __vt);
2043 if (__n < __de)
2044 __old_end = __move_backward_and_check(__old_end - __de, __oen, __old_end, __vt);
2045 _STD::fill_n(__old_end - __n, __n, *__vt);
2046 }
2047 }
2048 return __base::begin() + __pos;
2049}
2050
2051template <class _Tp, class _Allocator>
2052template <class _InputIter>
2053typename deque<_Tp, _Allocator>::iterator
2054deque<_Tp, _Allocator>::insert(const_iterator __p, _InputIter __f, _InputIter __l,
2055 typename enable_if<__is_input_iterator<_InputIter>::value
2056 &&!__is_bidirectional_iterator<_InputIter>::value>::type*)
2057{
2058 __split_buffer<value_type, allocator_type&> __buf(__base::__alloc());
2059 __buf.__construct_at_end(__f, __l);
2060 typedef typename __split_buffer<value_type, allocator_type&>::iterator __bi;
2061 return insert(__p, move_iterator<__bi>(__buf.begin()), move_iterator<__bi>(__buf.end()));
2062}
2063
2064template <class _Tp, class _Allocator>
2065template <class _BiIter>
2066typename deque<_Tp, _Allocator>::iterator
2067deque<_Tp, _Allocator>::insert(const_iterator __p, _BiIter __f, _BiIter __l,
2068 typename enable_if<__is_bidirectional_iterator<_BiIter>::value>::type*)
2069{
2070 size_type __n = _STD::distance(__f, __l);
2071 size_type __pos = __p - __base::begin();
2072 size_type __to_end = __base::size() - __pos;
2073 allocator_type& __a = __base::__alloc();
2074 if (__pos < __to_end)
2075 { // insert by shifting things backward
2076 if (__n > __front_spare())
2077 __add_front_capacity(__n - __front_spare());
2078 // __n <= __front_spare()
2079 size_type __old_n = __n;
2080 iterator __old_begin = __base::begin();
2081 iterator __i = __old_begin;
2082 _BiIter __m = __f;
2083 if (__n > __pos)
2084 {
2085 __m = __pos < __n / 2 ? _STD::prev(__l, __pos) : _STD::next(__f, __n - __pos);
2086 for (_BiIter __j = __m; __j != __f; --__base::__start_, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002087 __alloc_traits::construct(__a, _STD::addressof(*--__i), *--__j);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002088 __n = __pos;
2089 }
2090 if (__n > 0)
2091 {
2092 iterator __obn = __old_begin + __n;
2093 for (iterator __j = __obn; __j != __old_begin;)
2094 {
Howard Hinnant2529d022011-02-02 17:36:20 +00002095 __alloc_traits::construct(__a, _STD::addressof(*--__i), _STD::move(*--__j));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002096 --__base::__start_;
2097 ++__base::size();
2098 }
2099 if (__n < __pos)
2100 __old_begin = _STD::move(__obn, __old_begin + __pos, __old_begin);
2101 _STD::copy(__m, __l, __old_begin);
2102 }
2103 }
2104 else
2105 { // insert by shifting things forward
2106 size_type __back_capacity = __back_spare();
2107 if (__n > __back_capacity)
2108 __add_back_capacity(__n - __back_capacity);
2109 // __n <= __back_capacity
2110 size_type __old_n = __n;
2111 iterator __old_end = __base::end();
2112 iterator __i = __old_end;
2113 _BiIter __m = __l;
2114 size_type __de = __base::size() - __pos;
2115 if (__n > __de)
2116 {
2117 __m = __de < __n / 2 ? _STD::next(__f, __de) : _STD::prev(__l, __n - __de);
2118 for (_BiIter __j = __m; __j != __l; ++__i, ++__j, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002119 __alloc_traits::construct(__a, _STD::addressof(*__i), *__j);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002120 __n = __de;
2121 }
2122 if (__n > 0)
2123 {
2124 iterator __oen = __old_end - __n;
2125 for (iterator __j = __oen; __j != __old_end; ++__i, ++__j, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002126 __alloc_traits::construct(__a, _STD::addressof(*__i), _STD::move(*__j));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002127 if (__n < __de)
2128 __old_end = _STD::move_backward(__old_end - __de, __oen, __old_end);
2129 _STD::copy_backward(__f, __m, __old_end);
2130 }
2131 }
2132 return __base::begin() + __pos;
2133}
2134
2135template <class _Tp, class _Allocator>
2136template <class _InpIter>
2137void
2138deque<_Tp, _Allocator>::__append(_InpIter __f, _InpIter __l,
2139 typename enable_if<__is_input_iterator<_InpIter>::value &&
2140 !__is_forward_iterator<_InpIter>::value>::type*)
2141{
2142 for (; __f != __l; ++__f)
2143 push_back(*__f);
2144}
2145
2146template <class _Tp, class _Allocator>
2147template <class _ForIter>
2148void
2149deque<_Tp, _Allocator>::__append(_ForIter __f, _ForIter __l,
2150 typename enable_if<__is_forward_iterator<_ForIter>::value>::type*)
2151{
2152 size_type __n = _STD::distance(__f, __l);
2153 allocator_type& __a = __base::__alloc();
2154 size_type __back_capacity = __back_spare();
2155 if (__n > __back_capacity)
2156 __add_back_capacity(__n - __back_capacity);
2157 // __n <= __back_capacity
2158 for (iterator __i = __base::end(); __f != __l; ++__i, ++__f, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002159 __alloc_traits::construct(__a, _STD::addressof(*__i), *__f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002160}
2161
2162template <class _Tp, class _Allocator>
2163void
2164deque<_Tp, _Allocator>::__append(size_type __n)
2165{
2166 allocator_type& __a = __base::__alloc();
2167 size_type __back_capacity = __back_spare();
2168 if (__n > __back_capacity)
2169 __add_back_capacity(__n - __back_capacity);
2170 // __n <= __back_capacity
2171 for (iterator __i = __base::end(); __n; --__n, ++__i, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002172 __alloc_traits::construct(__a, _STD::addressof(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002173}
2174
2175template <class _Tp, class _Allocator>
2176void
2177deque<_Tp, _Allocator>::__append(size_type __n, const value_type& __v)
2178{
2179 allocator_type& __a = __base::__alloc();
2180 size_type __back_capacity = __back_spare();
2181 if (__n > __back_capacity)
2182 __add_back_capacity(__n - __back_capacity);
2183 // __n <= __back_capacity
2184 for (iterator __i = __base::end(); __n; --__n, ++__i, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002185 __alloc_traits::construct(__a, _STD::addressof(*__i), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002186}
2187
2188// Create front capacity for one block of elements.
2189// Strong guarantee. Either do it or don't touch anything.
2190template <class _Tp, class _Allocator>
2191void
2192deque<_Tp, _Allocator>::__add_front_capacity()
2193{
2194 allocator_type& __a = __base::__alloc();
2195 if (__back_spare() >= __base::__block_size)
2196 {
2197 __base::__start_ += __base::__block_size;
2198 pointer __pt = __base::__map_.back();
2199 __base::__map_.pop_back();
2200 __base::__map_.push_front(__pt);
2201 }
2202 // Else if __base::__map_.size() < __base::__map_.capacity() then we need to allocate 1 buffer
2203 else if (__base::__map_.size() < __base::__map_.capacity())
2204 { // we can put the new buffer into the map, but don't shift things around
2205 // until all buffers are allocated. If we throw, we don't need to fix
2206 // anything up (any added buffers are undetectible)
2207 if (__base::__map_.__front_spare() > 0)
2208 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2209 else
2210 {
2211 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2212 // Done allocating, reorder capacity
2213 pointer __pt = __base::__map_.back();
2214 __base::__map_.pop_back();
2215 __base::__map_.push_front(__pt);
2216 }
2217 __base::__start_ = __base::__map_.size() == 1 ?
2218 __base::__block_size / 2 :
2219 __base::__start_ + __base::__block_size;
2220 }
2221 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2222 else
2223 {
2224 __split_buffer<pointer, typename __base::__pointer_allocator&>
2225 __buf(max<size_type>(2 * __base::__map_.capacity(), 1),
2226 0, __base::__map_.__alloc());
2227#ifndef _LIBCPP_NO_EXCEPTIONS
2228 try
2229 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002230#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002231 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2232#ifndef _LIBCPP_NO_EXCEPTIONS
2233 }
2234 catch (...)
2235 {
2236 __alloc_traits::deallocate(__a, __buf.front(), __base::__block_size);
2237 throw;
2238 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002239#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002240 for (typename __base::__map_pointer __i = __base::__map_.begin();
2241 __i != __base::__map_.end(); ++__i)
2242 __buf.push_back(*__i);
2243 _STD::swap(__base::__map_.__first_, __buf.__first_);
2244 _STD::swap(__base::__map_.__begin_, __buf.__begin_);
2245 _STD::swap(__base::__map_.__end_, __buf.__end_);
2246 _STD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
2247 __base::__start_ = __base::__map_.size() == 1 ?
2248 __base::__block_size / 2 :
2249 __base::__start_ + __base::__block_size;
2250 }
2251}
2252
2253// Create front capacity for __n elements.
2254// Strong guarantee. Either do it or don't touch anything.
2255template <class _Tp, class _Allocator>
2256void
2257deque<_Tp, _Allocator>::__add_front_capacity(size_type __n)
2258{
2259 allocator_type& __a = __base::__alloc();
2260 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2261 // Number of unused blocks at back:
2262 size_type __back_capacity = __back_spare() / __base::__block_size;
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00002263 __back_capacity = _STD::min(__back_capacity, __nb); // don't take more than you need
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002264 __nb -= __back_capacity; // number of blocks need to allocate
2265 // If __nb == 0, then we have sufficient capacity.
2266 if (__nb == 0)
2267 {
2268 __base::__start_ += __base::__block_size * __back_capacity;
2269 for (; __back_capacity > 0; --__back_capacity)
2270 {
2271 pointer __pt = __base::__map_.back();
2272 __base::__map_.pop_back();
2273 __base::__map_.push_front(__pt);
2274 }
2275 }
2276 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2277 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2278 { // we can put the new buffers into the map, but don't shift things around
2279 // until all buffers are allocated. If we throw, we don't need to fix
2280 // anything up (any added buffers are undetectible)
2281 for (; __nb > 0; --__nb, __base::__start_ += __base::__block_size - (__base::__map_.size() == 1))
2282 {
2283 if (__base::__map_.__front_spare() == 0)
2284 break;
2285 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2286 }
2287 for (; __nb > 0; --__nb, ++__back_capacity)
2288 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2289 // Done allocating, reorder capacity
2290 __base::__start_ += __back_capacity * __base::__block_size;
2291 for (; __back_capacity > 0; --__back_capacity)
2292 {
2293 pointer __pt = __base::__map_.back();
2294 __base::__map_.pop_back();
2295 __base::__map_.push_front(__pt);
2296 }
2297 }
2298 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2299 else
2300 {
2301 size_type __ds = (__nb + __back_capacity) * __base::__block_size - __base::__map_.empty();
2302 __split_buffer<pointer, typename __base::__pointer_allocator&>
2303 __buf(max<size_type>(2* __base::__map_.capacity(),
2304 __nb + __base::__map_.size()),
2305 0, __base::__map_.__alloc());
2306#ifndef _LIBCPP_NO_EXCEPTIONS
2307 try
2308 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002309#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002310 for (; __nb > 0; --__nb)
2311 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2312#ifndef _LIBCPP_NO_EXCEPTIONS
2313 }
2314 catch (...)
2315 {
2316 for (typename __base::__map_pointer __i = __buf.begin();
2317 __i != __buf.end(); ++__i)
2318 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2319 throw;
2320 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002321#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002322 for (; __back_capacity > 0; --__back_capacity)
2323 {
2324 __buf.push_back(__base::__map_.back());
2325 __base::__map_.pop_back();
2326 }
2327 for (typename __base::__map_pointer __i = __base::__map_.begin();
2328 __i != __base::__map_.end(); ++__i)
2329 __buf.push_back(*__i);
2330 _STD::swap(__base::__map_.__first_, __buf.__first_);
2331 _STD::swap(__base::__map_.__begin_, __buf.__begin_);
2332 _STD::swap(__base::__map_.__end_, __buf.__end_);
2333 _STD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
2334 __base::__start_ += __ds;
2335 }
2336}
2337
2338// Create back capacity for one block of elements.
2339// Strong guarantee. Either do it or don't touch anything.
2340template <class _Tp, class _Allocator>
2341void
2342deque<_Tp, _Allocator>::__add_back_capacity()
2343{
2344 allocator_type& __a = __base::__alloc();
2345 if (__front_spare() >= __base::__block_size)
2346 {
2347 __base::__start_ -= __base::__block_size;
2348 pointer __pt = __base::__map_.front();
2349 __base::__map_.pop_front();
2350 __base::__map_.push_back(__pt);
2351 }
2352 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2353 else if (__base::__map_.size() < __base::__map_.capacity())
2354 { // we can put the new buffer into the map, but don't shift things around
2355 // until it is allocated. If we throw, we don't need to fix
2356 // anything up (any added buffers are undetectible)
2357 if (__base::__map_.__back_spare() != 0)
2358 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2359 else
2360 {
2361 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2362 // Done allocating, reorder capacity
2363 pointer __pt = __base::__map_.front();
2364 __base::__map_.pop_front();
2365 __base::__map_.push_back(__pt);
2366 }
2367 }
2368 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2369 else
2370 {
2371 __split_buffer<pointer, typename __base::__pointer_allocator&>
2372 __buf(max<size_type>(2* __base::__map_.capacity(), 1),
2373 __base::__map_.size(),
2374 __base::__map_.__alloc());
2375#ifndef _LIBCPP_NO_EXCEPTIONS
2376 try
2377 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002378#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002379 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2380#ifndef _LIBCPP_NO_EXCEPTIONS
2381 }
2382 catch (...)
2383 {
2384 __alloc_traits::deallocate(__a, __buf.back(), __base::__block_size);
2385 throw;
2386 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002387#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002388 for (typename __base::__map_pointer __i = __base::__map_.end();
2389 __i != __base::__map_.begin();)
2390 __buf.push_front(*--__i);
2391 _STD::swap(__base::__map_.__first_, __buf.__first_);
2392 _STD::swap(__base::__map_.__begin_, __buf.__begin_);
2393 _STD::swap(__base::__map_.__end_, __buf.__end_);
2394 _STD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
2395 }
2396}
2397
2398// Create back capacity for __n elements.
2399// Strong guarantee. Either do it or don't touch anything.
2400template <class _Tp, class _Allocator>
2401void
2402deque<_Tp, _Allocator>::__add_back_capacity(size_type __n)
2403{
2404 allocator_type& __a = __base::__alloc();
2405 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2406 // Number of unused blocks at front:
2407 size_type __front_capacity = __front_spare() / __base::__block_size;
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00002408 __front_capacity = _STD::min(__front_capacity, __nb); // don't take more than you need
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002409 __nb -= __front_capacity; // number of blocks need to allocate
2410 // If __nb == 0, then we have sufficient capacity.
2411 if (__nb == 0)
2412 {
2413 __base::__start_ -= __base::__block_size * __front_capacity;
2414 for (; __front_capacity > 0; --__front_capacity)
2415 {
2416 pointer __pt = __base::__map_.front();
2417 __base::__map_.pop_front();
2418 __base::__map_.push_back(__pt);
2419 }
2420 }
2421 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2422 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2423 { // we can put the new buffers into the map, but don't shift things around
2424 // until all buffers are allocated. If we throw, we don't need to fix
2425 // anything up (any added buffers are undetectible)
2426 for (; __nb > 0; --__nb)
2427 {
2428 if (__base::__map_.__back_spare() == 0)
2429 break;
2430 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2431 }
2432 for (; __nb > 0; --__nb, ++__front_capacity, __base::__start_ +=
2433 __base::__block_size - (__base::__map_.size() == 1))
2434 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2435 // Done allocating, reorder capacity
2436 __base::__start_ -= __base::__block_size * __front_capacity;
2437 for (; __front_capacity > 0; --__front_capacity)
2438 {
2439 pointer __pt = __base::__map_.front();
2440 __base::__map_.pop_front();
2441 __base::__map_.push_back(__pt);
2442 }
2443 }
2444 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2445 else
2446 {
2447 size_type __ds = __front_capacity * __base::__block_size;
2448 __split_buffer<pointer, typename __base::__pointer_allocator&>
2449 __buf(max<size_type>(2* __base::__map_.capacity(),
2450 __nb + __base::__map_.size()),
2451 __base::__map_.size() - __front_capacity,
2452 __base::__map_.__alloc());
2453#ifndef _LIBCPP_NO_EXCEPTIONS
2454 try
2455 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002456#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002457 for (; __nb > 0; --__nb)
2458 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2459#ifndef _LIBCPP_NO_EXCEPTIONS
2460 }
2461 catch (...)
2462 {
2463 for (typename __base::__map_pointer __i = __buf.begin();
2464 __i != __buf.end(); ++__i)
2465 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2466 throw;
2467 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002468#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002469 for (; __front_capacity > 0; --__front_capacity)
2470 {
2471 __buf.push_back(__base::__map_.front());
2472 __base::__map_.pop_front();
2473 }
2474 for (typename __base::__map_pointer __i = __base::__map_.end();
2475 __i != __base::__map_.begin();)
2476 __buf.push_front(*--__i);
2477 _STD::swap(__base::__map_.__first_, __buf.__first_);
2478 _STD::swap(__base::__map_.__begin_, __buf.__begin_);
2479 _STD::swap(__base::__map_.__end_, __buf.__end_);
2480 _STD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
2481 __base::__start_ -= __ds;
2482 }
2483}
2484
2485template <class _Tp, class _Allocator>
2486void
2487deque<_Tp, _Allocator>::pop_front()
2488{
2489 allocator_type& __a = __base::__alloc();
2490 __alloc_traits::destroy(__a, *(__base::__map_.begin() +
2491 __base::__start_ / __base::__block_size) +
2492 __base::__start_ % __base::__block_size);
2493 --__base::size();
2494 if (++__base::__start_ >= 2 * __base::__block_size)
2495 {
2496 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2497 __base::__map_.pop_front();
2498 __base::__start_ -= __base::__block_size;
2499 }
2500}
2501
2502template <class _Tp, class _Allocator>
2503void
2504deque<_Tp, _Allocator>::pop_back()
2505{
2506 allocator_type& __a = __base::__alloc();
2507 size_type __p = __base::size() + __base::__start_ - 1;
2508 __alloc_traits::destroy(__a, *(__base::__map_.begin() +
2509 __p / __base::__block_size) +
2510 __p % __base::__block_size);
2511 --__base::size();
2512 if (__back_spare() >= 2 * __base::__block_size)
2513 {
2514 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2515 __base::__map_.pop_back();
2516 }
2517}
2518
2519// move assign [__f, __l) to [__r, __r + (__l-__f)).
2520// If __vt points into [__f, __l), then subtract (__f - __r) from __vt.
2521template <class _Tp, class _Allocator>
2522typename deque<_Tp, _Allocator>::iterator
2523deque<_Tp, _Allocator>::__move_and_check(iterator __f, iterator __l, iterator __r,
2524 const_pointer& __vt)
2525{
2526 // as if
2527 // for (; __f != __l; ++__f, ++__r)
2528 // *__r = _STD::move(*__f);
2529 difference_type __n = __l - __f;
2530 while (__n > 0)
2531 {
2532 pointer __fb = __f.__ptr_;
2533 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2534 difference_type __bs = __fe - __fb;
2535 if (__bs > __n)
2536 {
2537 __bs = __n;
2538 __fe = __fb + __bs;
2539 }
2540 if (__fb <= __vt && __vt < __fe)
2541 __vt = (const_iterator(__f.__m_iter_, __vt) -= __f - __r).__ptr_;
2542 __r = _STD::move(__fb, __fe, __r);
2543 __n -= __bs;
2544 __f += __bs;
2545 }
2546 return __r;
2547}
2548
2549// move assign [__f, __l) to [__r - (__l-__f), __r) backwards.
2550// If __vt points into [__f, __l), then add (__r - __l) to __vt.
2551template <class _Tp, class _Allocator>
2552typename deque<_Tp, _Allocator>::iterator
2553deque<_Tp, _Allocator>::__move_backward_and_check(iterator __f, iterator __l, iterator __r,
2554 const_pointer& __vt)
2555{
2556 // as if
2557 // while (__f != __l)
2558 // *--__r = _STD::move(*--__l);
2559 difference_type __n = __l - __f;
2560 while (__n > 0)
2561 {
2562 --__l;
2563 pointer __lb = *__l.__m_iter_;
2564 pointer __le = __l.__ptr_ + 1;
2565 difference_type __bs = __le - __lb;
2566 if (__bs > __n)
2567 {
2568 __bs = __n;
2569 __lb = __le - __bs;
2570 }
2571 if (__lb <= __vt && __vt < __le)
2572 __vt = (const_iterator(__l.__m_iter_, __vt) += __r - __l - 1).__ptr_;
2573 __r = _STD::move_backward(__lb, __le, __r);
2574 __n -= __bs;
2575 __l -= __bs - 1;
2576 }
2577 return __r;
2578}
2579
2580// move construct [__f, __l) to [__r, __r + (__l-__f)).
2581// If __vt points into [__f, __l), then add (__r - __f) to __vt.
2582template <class _Tp, class _Allocator>
2583void
2584deque<_Tp, _Allocator>::__move_construct_and_check(iterator __f, iterator __l,
2585 iterator __r, const_pointer& __vt)
2586{
2587 allocator_type& __a = __base::__alloc();
2588 // as if
2589 // for (; __f != __l; ++__r, ++__f, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002590 // __alloc_traits::construct(__a, _STD::addressof(*__r), _STD::move(*__f));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002591 difference_type __n = __l - __f;
2592 while (__n > 0)
2593 {
2594 pointer __fb = __f.__ptr_;
2595 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2596 difference_type __bs = __fe - __fb;
2597 if (__bs > __n)
2598 {
2599 __bs = __n;
2600 __fe = __fb + __bs;
2601 }
2602 if (__fb <= __vt && __vt < __fe)
2603 __vt = (const_iterator(__f.__m_iter_, __vt) += __r - __f).__ptr_;
2604 for (; __fb != __fe; ++__fb, ++__r, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002605 __alloc_traits::construct(__a, _STD::addressof(*__r), _STD::move(*__fb));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002606 __n -= __bs;
2607 __f += __bs;
2608 }
2609}
2610
2611// move construct [__f, __l) to [__r - (__l-__f), __r) backwards.
2612// If __vt points into [__f, __l), then subtract (__l - __r) from __vt.
2613template <class _Tp, class _Allocator>
2614void
2615deque<_Tp, _Allocator>::__move_construct_backward_and_check(iterator __f, iterator __l,
2616 iterator __r, const_pointer& __vt)
2617{
2618 allocator_type& __a = __base::__alloc();
2619 // as if
2620 // for (iterator __j = __l; __j != __f;)
2621 // {
Howard Hinnant2529d022011-02-02 17:36:20 +00002622 // __alloc_traitsconstruct(__a, _STD::addressof(*--__r), _STD::move(*--__j));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002623 // --__base::__start_;
2624 // ++__base::size();
2625 // }
2626 difference_type __n = __l - __f;
2627 while (__n > 0)
2628 {
2629 --__l;
2630 pointer __lb = *__l.__m_iter_;
2631 pointer __le = __l.__ptr_ + 1;
2632 difference_type __bs = __le - __lb;
2633 if (__bs > __n)
2634 {
2635 __bs = __n;
2636 __lb = __le - __bs;
2637 }
2638 if (__lb <= __vt && __vt < __le)
2639 __vt = (const_iterator(__l.__m_iter_, __vt) -= __l - __r + 1).__ptr_;
2640 while (__le != __lb)
2641 {
Howard Hinnant2529d022011-02-02 17:36:20 +00002642 __alloc_traits::construct(__a, _STD::addressof(*--__r), _STD::move(*--__le));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002643 --__base::__start_;
2644 ++__base::size();
2645 }
2646 __n -= __bs;
2647 __l -= __bs - 1;
2648 }
2649}
2650
2651template <class _Tp, class _Allocator>
2652typename deque<_Tp, _Allocator>::iterator
2653deque<_Tp, _Allocator>::erase(const_iterator __f)
2654{
2655 difference_type __n = 1;
2656 iterator __b = __base::begin();
2657 difference_type __pos = __f - __b;
2658 iterator __p = __b + __pos;
2659 allocator_type& __a = __base::__alloc();
2660 if (__pos < (__base::size() - 1) / 2)
2661 { // erase from front
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00002662 _STD::move_backward(__b, __p, _STD::next(__p));
Howard Hinnant2529d022011-02-02 17:36:20 +00002663 __alloc_traits::destroy(__a, _STD::addressof(*__b));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002664 --__base::size();
2665 ++__base::__start_;
2666 if (__front_spare() >= 2 * __base::__block_size)
2667 {
2668 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2669 __base::__map_.pop_front();
2670 __base::__start_ -= __base::__block_size;
2671 }
2672 }
2673 else
2674 { // erase from back
Douglas Gregor7ac6af72011-04-29 16:20:26 +00002675 iterator __i = _STD::move(_STD::next(__p), __base::end(), __p);
Howard Hinnant2529d022011-02-02 17:36:20 +00002676 __alloc_traits::destroy(__a, _STD::addressof(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002677 --__base::size();
2678 if (__back_spare() >= 2 * __base::__block_size)
2679 {
2680 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2681 __base::__map_.pop_back();
2682 }
2683 }
2684 return __base::begin() + __pos;
2685}
2686
2687template <class _Tp, class _Allocator>
2688typename deque<_Tp, _Allocator>::iterator
2689deque<_Tp, _Allocator>::erase(const_iterator __f, const_iterator __l)
2690{
2691 difference_type __n = __l - __f;
2692 iterator __b = __base::begin();
2693 difference_type __pos = __f - __b;
2694 iterator __p = __b + __pos;
2695 if (__n > 0)
2696 {
2697 allocator_type& __a = __base::__alloc();
2698 if (__pos < (__base::size() - __n) / 2)
2699 { // erase from front
2700 iterator __i = _STD::move_backward(__b, __p, __p + __n);
2701 for (; __b != __i; ++__b)
Howard Hinnant2529d022011-02-02 17:36:20 +00002702 __alloc_traits::destroy(__a, _STD::addressof(*__b));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002703 __base::size() -= __n;
2704 __base::__start_ += __n;
2705 while (__front_spare() >= 2 * __base::__block_size)
2706 {
2707 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2708 __base::__map_.pop_front();
2709 __base::__start_ -= __base::__block_size;
2710 }
2711 }
2712 else
2713 { // erase from back
2714 iterator __i = _STD::move(__p + __n, __base::end(), __p);
2715 for (iterator __e = __base::end(); __i != __e; ++__i)
Howard Hinnant2529d022011-02-02 17:36:20 +00002716 __alloc_traits::destroy(__a, _STD::addressof(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002717 __base::size() -= __n;
2718 while (__back_spare() >= 2 * __base::__block_size)
2719 {
2720 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2721 __base::__map_.pop_back();
2722 }
2723 }
2724 }
2725 return __base::begin() + __pos;
2726}
2727
2728template <class _Tp, class _Allocator>
2729void
2730deque<_Tp, _Allocator>::__erase_to_end(const_iterator __f)
2731{
2732 iterator __e = __base::end();
2733 difference_type __n = __e - __f;
2734 if (__n > 0)
2735 {
2736 allocator_type& __a = __base::__alloc();
2737 iterator __b = __base::begin();
2738 difference_type __pos = __f - __b;
2739 for (iterator __p = __b + __pos; __p != __e; ++__p)
Howard Hinnant2529d022011-02-02 17:36:20 +00002740 __alloc_traits::destroy(__a, _STD::addressof(*__p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002741 __base::size() -= __n;
2742 while (__back_spare() >= 2 * __base::__block_size)
2743 {
2744 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2745 __base::__map_.pop_back();
2746 }
2747 }
2748}
2749
2750template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00002751inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002752void
2753deque<_Tp, _Allocator>::swap(deque& __c)
Howard Hinnant0a612b02011-06-02 20:00:14 +00002754 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2755 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002756{
2757 __base::swap(__c);
2758}
2759
2760template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00002761inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002762void
Howard Hinnanta12beb32011-06-02 16:10:22 +00002763deque<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002764{
2765 __base::clear();
2766}
2767
2768template <class _Tp, class _Allocator>
2769_LIBCPP_INLINE_VISIBILITY inline
2770bool
2771operator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2772{
2773 const typename deque<_Tp, _Allocator>::size_type __sz = __x.size();
2774 return __sz == __y.size() && _STD::equal(__x.begin(), __x.end(), __y.begin());
2775}
2776
2777template <class _Tp, class _Allocator>
2778_LIBCPP_INLINE_VISIBILITY inline
2779bool
2780operator!=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2781{
2782 return !(__x == __y);
2783}
2784
2785template <class _Tp, class _Allocator>
2786_LIBCPP_INLINE_VISIBILITY inline
2787bool
2788operator< (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2789{
2790 return _STD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
2791}
2792
2793template <class _Tp, class _Allocator>
2794_LIBCPP_INLINE_VISIBILITY inline
2795bool
2796operator> (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2797{
2798 return __y < __x;
2799}
2800
2801template <class _Tp, class _Allocator>
2802_LIBCPP_INLINE_VISIBILITY inline
2803bool
2804operator>=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2805{
2806 return !(__x < __y);
2807}
2808
2809template <class _Tp, class _Allocator>
2810_LIBCPP_INLINE_VISIBILITY inline
2811bool
2812operator<=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2813{
2814 return !(__y < __x);
2815}
2816
2817template <class _Tp, class _Allocator>
2818_LIBCPP_INLINE_VISIBILITY inline
2819void
2820swap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
Howard Hinnant0a612b02011-06-02 20:00:14 +00002821 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002822{
2823 __x.swap(__y);
2824}
2825
2826_LIBCPP_END_NAMESPACE_STD
2827
2828#endif // _LIBCPP_DEQUE