blob: 2063bd8786a0a90e8944108fd2826485523e2d44 [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:
41 deque();
42 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);
51 deque(deque&& c);
52 deque(initializer_list<value_type> il, const Allocator& a = allocator_type());
53 deque(const deque& c, const allocator_type& a);
54 deque(deque&& c, const allocator_type& a);
55 ~deque();
56
57 deque& operator=(const deque& c);
58 deque& operator=(deque&& c);
59 deque& operator=(initializer_list<value_type> il);
60
61 template <class InputIterator>
62 void assign(InputIterator f, InputIterator l);
63 void assign(size_type n, const value_type& v);
64 void assign(initializer_list<value_type> il);
65
Howard Hinnanta12beb32011-06-02 16:10:22 +000066 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000067
68 // iterators:
69
Howard Hinnanta12beb32011-06-02 16:10:22 +000070 iterator begin() noexcept;
71 const_iterator begin() const noexcept;
72 iterator end() noexcept;
73 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000074
Howard Hinnanta12beb32011-06-02 16:10:22 +000075 reverse_iterator rbegin() noexcept;
76 const_reverse_iterator rbegin() const noexcept;
77 reverse_iterator rend() noexcept;
78 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000079
Howard Hinnanta12beb32011-06-02 16:10:22 +000080 const_iterator cbegin() const noexcept;
81 const_iterator cend() const noexcept;
82 const_reverse_iterator crbegin() const noexcept;
83 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000084
85 // capacity:
Howard Hinnanta12beb32011-06-02 16:10:22 +000086 size_type size() const noexcept;
87 size_type max_size() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000088 void resize(size_type n);
89 void resize(size_type n, const value_type& v);
90 void shrink_to_fit();
Howard Hinnanta12beb32011-06-02 16:10:22 +000091 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000092
93 // element access:
94 reference operator[](size_type i);
95 const_reference operator[](size_type i) const;
96 reference at(size_type i);
97 const_reference at(size_type i) const;
98 reference front();
99 const_reference front() const;
100 reference back();
101 const_reference back() const;
102
103 // modifiers:
104 void push_front(const value_type& v);
105 void push_front(value_type&& v);
106 void push_back(const value_type& v);
107 void push_back(value_type&& v);
108 template <class... Args> void emplace_front(Args&&... args);
109 template <class... Args> void emplace_back(Args&&... args);
110 template <class... Args> iterator emplace(const_iterator p, Args&&... args);
111 iterator insert(const_iterator p, const value_type& v);
112 iterator insert(const_iterator p, value_type&& v);
113 iterator insert(const_iterator p, size_type n, const value_type& v);
114 template <class InputIterator>
115 iterator insert (const_iterator p, InputIterator f, InputIterator l);
116 iterator insert(const_iterator p, initializer_list<value_type> il);
117 void pop_front();
118 void pop_back();
119 iterator erase(const_iterator p);
120 iterator erase(const_iterator f, const_iterator l);
121 void swap(deque& c);
Howard Hinnanta12beb32011-06-02 16:10:22 +0000122 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000123};
124
125template <class T, class Allocator>
126 bool operator==(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
127template <class T, class Allocator>
128 bool operator< (const deque<T,Allocator>& x, const deque<T,Allocator>& y);
129template <class T, class Allocator>
130 bool operator!=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
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);
137
138// specialized algorithms:
139template <class T, class Allocator>
140 void swap(deque<T,Allocator>& x, deque<T,Allocator>& y);
141
142} // std
143
144*/
145
146#pragma GCC system_header
147
148#include <__config>
149#include <__split_buffer>
150#include <type_traits>
151#include <initializer_list>
152#include <iterator>
153#include <algorithm>
154#include <stdexcept>
155
156_LIBCPP_BEGIN_NAMESPACE_STD
157
158template <class _Tp, class _Allocator> class __deque_base;
159
160template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
161 class _DiffType, _DiffType _BlockSize>
Howard Hinnant422a53f2010-09-21 21:28:23 +0000162class _LIBCPP_VISIBLE __deque_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000163
164template <class _RAIter,
165 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
166__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
167copy(_RAIter __f,
168 _RAIter __l,
169 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
170 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
171
172template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
173 class _OutputIterator>
174_OutputIterator
175copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
176 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
177 _OutputIterator __r);
178
179template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
180 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
181__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
182copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
183 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
184 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
185
186template <class _RAIter,
187 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
188__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
189copy_backward(_RAIter __f,
190 _RAIter __l,
191 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
192 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
193
194template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
195 class _OutputIterator>
196_OutputIterator
197copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
198 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
199 _OutputIterator __r);
200
201template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
202 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
203__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
204copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
205 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
206 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
207
208template <class _RAIter,
209 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
210__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
211move(_RAIter __f,
212 _RAIter __l,
213 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
214 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
215
216template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
217 class _OutputIterator>
218_OutputIterator
219move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
220 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
221 _OutputIterator __r);
222
223template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
224 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
225__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
226move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
227 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
228 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
229
230template <class _RAIter,
231 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
232__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
233move_backward(_RAIter __f,
234 _RAIter __l,
235 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
236 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
237
238template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
239 class _OutputIterator>
240_OutputIterator
241move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
242 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
243 _OutputIterator __r);
244
245template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
246 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
247__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
248move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
249 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
250 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
251
252template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
253 class _DiffType, _DiffType _BlockSize>
Howard Hinnant422a53f2010-09-21 21:28:23 +0000254class _LIBCPP_VISIBLE __deque_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000255{
256 typedef _MapPointer __map_iterator;
257public:
258 typedef _Pointer pointer;
259 typedef _DiffType difference_type;
260private:
261 __map_iterator __m_iter_;
262 pointer __ptr_;
263
264 static const difference_type __block_size = _BlockSize;
265public:
266 typedef _ValueType value_type;
267 typedef random_access_iterator_tag iterator_category;
268 typedef _Reference reference;
269
Howard Hinnanta12beb32011-06-02 16:10:22 +0000270 _LIBCPP_INLINE_VISIBILITY __deque_iterator() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000271
272 template <class _P, class _R, class _MP>
273 _LIBCPP_INLINE_VISIBILITY
274 __deque_iterator(const __deque_iterator<value_type, _P, _R, _MP, difference_type, __block_size>& __it,
Howard Hinnanta12beb32011-06-02 16:10:22 +0000275 typename enable_if<is_convertible<_P, pointer>::value>::type* = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000276 : __m_iter_(__it.__m_iter_), __ptr_(__it.__ptr_) {}
277
278 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return *__ptr_;}
279 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return __ptr_;}
280
281 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator++()
282 {
283 if (++__ptr_ - *__m_iter_ == __block_size)
284 {
285 ++__m_iter_;
286 __ptr_ = *__m_iter_;
287 }
288 return *this;
289 }
290
291 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator++(int)
292 {
293 __deque_iterator __tmp = *this;
294 ++(*this);
295 return __tmp;
296 }
297
298 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator--()
299 {
300 if (__ptr_ == *__m_iter_)
301 {
302 --__m_iter_;
303 __ptr_ = *__m_iter_ + __block_size;
304 }
305 --__ptr_;
306 return *this;
307 }
308
309 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator--(int)
310 {
311 __deque_iterator __tmp = *this;
312 --(*this);
313 return __tmp;
314 }
315
316 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator+=(difference_type __n)
317 {
318 if (__n != 0)
319 {
320 __n += __ptr_ - *__m_iter_;
321 if (__n > 0)
322 {
323 __m_iter_ += __n / __block_size;
324 __ptr_ = *__m_iter_ + __n % __block_size;
325 }
326 else // (__n < 0)
327 {
328 difference_type __z = __block_size - 1 - __n;
329 __m_iter_ -= __z / __block_size;
330 __ptr_ = *__m_iter_ + (__block_size - 1 - __z % __block_size);
331 }
332 }
333 return *this;
334 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000335
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000336 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator-=(difference_type __n)
337 {
338 return *this += -__n;
339 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000340
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000341 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator+(difference_type __n) const
342 {
343 __deque_iterator __t(*this);
344 __t += __n;
345 return __t;
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 }
354
355 _LIBCPP_INLINE_VISIBILITY
356 friend __deque_iterator operator+(difference_type __n, const __deque_iterator& __it)
357 {return __it + __n;}
358
359 _LIBCPP_INLINE_VISIBILITY
360 friend difference_type operator-(const __deque_iterator& __x, const __deque_iterator& __y)
361 {
362 if (__x != __y)
363 return (__x.__m_iter_ - __y.__m_iter_) * __block_size
364 + (__x.__ptr_ - *__x.__m_iter_)
365 - (__y.__ptr_ - *__y.__m_iter_);
366 return 0;
367 }
368
369 _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const
370 {return *(*this + __n);}
371
372 _LIBCPP_INLINE_VISIBILITY friend
373 bool operator==(const __deque_iterator& __x, const __deque_iterator& __y)
374 {return __x.__ptr_ == __y.__ptr_;}
375
376 _LIBCPP_INLINE_VISIBILITY friend
377 bool operator!=(const __deque_iterator& __x, const __deque_iterator& __y)
378 {return !(__x == __y);}
379
380 _LIBCPP_INLINE_VISIBILITY friend
381 bool operator<(const __deque_iterator& __x, const __deque_iterator& __y)
382 {return __x.__m_iter_ < __y.__m_iter_ ||
383 (__x.__m_iter_ == __y.__m_iter_ && __x.__ptr_ < __y.__ptr_);}
384
385 _LIBCPP_INLINE_VISIBILITY friend
386 bool operator>(const __deque_iterator& __x, const __deque_iterator& __y)
387 {return __y < __x;}
388
389 _LIBCPP_INLINE_VISIBILITY friend
390 bool operator<=(const __deque_iterator& __x, const __deque_iterator& __y)
391 {return !(__y < __x);}
392
393 _LIBCPP_INLINE_VISIBILITY friend
394 bool operator>=(const __deque_iterator& __x, const __deque_iterator& __y)
395 {return !(__x < __y);}
396
397private:
Howard Hinnanta12beb32011-06-02 16:10:22 +0000398 _LIBCPP_INLINE_VISIBILITY __deque_iterator(__map_iterator __m, pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000399 : __m_iter_(__m), __ptr_(__p) {}
400
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000401 template <class _Tp, class _A> friend class __deque_base;
Howard Hinnant422a53f2010-09-21 21:28:23 +0000402 template <class _Tp, class _A> friend class _LIBCPP_VISIBLE deque;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000403 template <class _V, class _P, class _R, class _MP, class _D, _D>
Howard Hinnant422a53f2010-09-21 21:28:23 +0000404 friend class _LIBCPP_VISIBLE __deque_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000405
406 template <class _RAIter,
407 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
408 friend
409 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
410 copy(_RAIter __f,
411 _RAIter __l,
412 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
413 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
414
415 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
416 class _OutputIterator>
417 friend
418 _OutputIterator
419 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
420 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
421 _OutputIterator __r);
422
423 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
424 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
425 friend
426 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
427 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
428 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
429 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
430
431 template <class _RAIter,
432 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
433 friend
434 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
435 copy_backward(_RAIter __f,
436 _RAIter __l,
437 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
438 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
439
440 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
441 class _OutputIterator>
442 friend
443 _OutputIterator
444 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
445 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
446 _OutputIterator __r);
447
448 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
449 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
450 friend
451 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
452 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
453 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
454 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
455
456 template <class _RAIter,
457 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
458 friend
459 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
460 move(_RAIter __f,
461 _RAIter __l,
462 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
463 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
464
465 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
466 class _OutputIterator>
467 friend
468 _OutputIterator
469 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
470 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
471 _OutputIterator __r);
472
473 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
474 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
475 friend
476 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
477 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
478 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
479 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
480
481 template <class _RAIter,
482 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
483 friend
484 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
485 move_backward(_RAIter __f,
486 _RAIter __l,
487 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
488 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
489
490 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
491 class _OutputIterator>
492 friend
493 _OutputIterator
494 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
495 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
496 _OutputIterator __r);
497
498 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
499 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
500 friend
501 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
502 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
503 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
504 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
505};
506
507// copy
508
509template <class _RAIter,
510 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
511__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
512copy(_RAIter __f,
513 _RAIter __l,
514 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
515 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
516{
517 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
518 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
519 while (__f != __l)
520 {
521 pointer __rb = __r.__ptr_;
522 pointer __re = *__r.__m_iter_ + _B2;
523 difference_type __bs = __re - __rb;
524 difference_type __n = __l - __f;
525 _RAIter __m = __l;
526 if (__n > __bs)
527 {
528 __n = __bs;
529 __m = __f + __n;
530 }
531 _STD::copy(__f, __m, __rb);
532 __f = __m;
533 __r += __n;
534 }
535 return __r;
536}
537
538template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
539 class _OutputIterator>
540_OutputIterator
541copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
542 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
543 _OutputIterator __r)
544{
545 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
546 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
547 difference_type __n = __l - __f;
548 while (__n > 0)
549 {
550 pointer __fb = __f.__ptr_;
551 pointer __fe = *__f.__m_iter_ + _B1;
552 difference_type __bs = __fe - __fb;
553 if (__bs > __n)
554 {
555 __bs = __n;
556 __fe = __fb + __bs;
557 }
558 __r = _STD::copy(__fb, __fe, __r);
559 __n -= __bs;
560 __f += __bs;
561 }
562 return __r;
563}
564
565template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
566 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
567__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
568copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
569 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
570 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
571{
572 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
573 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
574 difference_type __n = __l - __f;
575 while (__n > 0)
576 {
577 pointer __fb = __f.__ptr_;
578 pointer __fe = *__f.__m_iter_ + _B1;
579 difference_type __bs = __fe - __fb;
580 if (__bs > __n)
581 {
582 __bs = __n;
583 __fe = __fb + __bs;
584 }
585 __r = _STD::copy(__fb, __fe, __r);
586 __n -= __bs;
587 __f += __bs;
588 }
589 return __r;
590}
591
592// copy_backward
593
594template <class _RAIter,
595 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
596__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
597copy_backward(_RAIter __f,
598 _RAIter __l,
599 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
600 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
601{
602 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
603 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
604 while (__f != __l)
605 {
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +0000606 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _STD::prev(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000607 pointer __rb = *__rp.__m_iter_;
608 pointer __re = __rp.__ptr_ + 1;
609 difference_type __bs = __re - __rb;
610 difference_type __n = __l - __f;
611 _RAIter __m = __f;
612 if (__n > __bs)
613 {
614 __n = __bs;
615 __m = __l - __n;
616 }
617 _STD::copy_backward(__m, __l, __re);
618 __l = __m;
619 __r -= __n;
620 }
621 return __r;
622}
623
624template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
625 class _OutputIterator>
626_OutputIterator
627copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
628 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
629 _OutputIterator __r)
630{
631 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
632 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
633 difference_type __n = __l - __f;
634 while (__n > 0)
635 {
636 --__l;
637 pointer __lb = *__l.__m_iter_;
638 pointer __le = __l.__ptr_ + 1;
639 difference_type __bs = __le - __lb;
640 if (__bs > __n)
641 {
642 __bs = __n;
643 __lb = __le - __bs;
644 }
645 __r = _STD::copy_backward(__lb, __le, __r);
646 __n -= __bs;
647 __l -= __bs - 1;
648 }
649 return __r;
650}
651
652template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
653 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
654__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
655copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
656 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
657 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
658{
659 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
660 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
661 difference_type __n = __l - __f;
662 while (__n > 0)
663 {
664 --__l;
665 pointer __lb = *__l.__m_iter_;
666 pointer __le = __l.__ptr_ + 1;
667 difference_type __bs = __le - __lb;
668 if (__bs > __n)
669 {
670 __bs = __n;
671 __lb = __le - __bs;
672 }
673 __r = _STD::copy_backward(__lb, __le, __r);
674 __n -= __bs;
675 __l -= __bs - 1;
676 }
677 return __r;
678}
679
680// move
681
682template <class _RAIter,
683 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
684__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
685move(_RAIter __f,
686 _RAIter __l,
687 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
688 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
689{
690 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
691 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
692 while (__f != __l)
693 {
694 pointer __rb = __r.__ptr_;
695 pointer __re = *__r.__m_iter_ + _B2;
696 difference_type __bs = __re - __rb;
697 difference_type __n = __l - __f;
698 _RAIter __m = __l;
699 if (__n > __bs)
700 {
701 __n = __bs;
702 __m = __f + __n;
703 }
704 _STD::move(__f, __m, __rb);
705 __f = __m;
706 __r += __n;
707 }
708 return __r;
709}
710
711template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
712 class _OutputIterator>
713_OutputIterator
714move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
715 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
716 _OutputIterator __r)
717{
718 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
719 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
720 difference_type __n = __l - __f;
721 while (__n > 0)
722 {
723 pointer __fb = __f.__ptr_;
724 pointer __fe = *__f.__m_iter_ + _B1;
725 difference_type __bs = __fe - __fb;
726 if (__bs > __n)
727 {
728 __bs = __n;
729 __fe = __fb + __bs;
730 }
731 __r = _STD::move(__fb, __fe, __r);
732 __n -= __bs;
733 __f += __bs;
734 }
735 return __r;
736}
737
738template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
739 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
740__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
741move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
742 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
743 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
744{
745 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
746 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
747 difference_type __n = __l - __f;
748 while (__n > 0)
749 {
750 pointer __fb = __f.__ptr_;
751 pointer __fe = *__f.__m_iter_ + _B1;
752 difference_type __bs = __fe - __fb;
753 if (__bs > __n)
754 {
755 __bs = __n;
756 __fe = __fb + __bs;
757 }
758 __r = _STD::move(__fb, __fe, __r);
759 __n -= __bs;
760 __f += __bs;
761 }
762 return __r;
763}
764
765// move_backward
766
767template <class _RAIter,
768 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
769__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
770move_backward(_RAIter __f,
771 _RAIter __l,
772 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
773 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
774{
775 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
776 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
777 while (__f != __l)
778 {
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +0000779 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _STD::prev(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000780 pointer __rb = *__rp.__m_iter_;
781 pointer __re = __rp.__ptr_ + 1;
782 difference_type __bs = __re - __rb;
783 difference_type __n = __l - __f;
784 _RAIter __m = __f;
785 if (__n > __bs)
786 {
787 __n = __bs;
788 __m = __l - __n;
789 }
790 _STD::move_backward(__m, __l, __re);
791 __l = __m;
792 __r -= __n;
793 }
794 return __r;
795}
796
797template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
798 class _OutputIterator>
799_OutputIterator
800move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
801 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
802 _OutputIterator __r)
803{
804 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
805 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
806 difference_type __n = __l - __f;
807 while (__n > 0)
808 {
809 --__l;
810 pointer __lb = *__l.__m_iter_;
811 pointer __le = __l.__ptr_ + 1;
812 difference_type __bs = __le - __lb;
813 if (__bs > __n)
814 {
815 __bs = __n;
816 __lb = __le - __bs;
817 }
818 __r = _STD::move_backward(__lb, __le, __r);
819 __n -= __bs;
820 __l -= __bs - 1;
821 }
822 return __r;
823}
824
825template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
826 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
827__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
828move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
829 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
830 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
831{
832 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
833 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
834 difference_type __n = __l - __f;
835 while (__n > 0)
836 {
837 --__l;
838 pointer __lb = *__l.__m_iter_;
839 pointer __le = __l.__ptr_ + 1;
840 difference_type __bs = __le - __lb;
841 if (__bs > __n)
842 {
843 __bs = __n;
844 __lb = __le - __bs;
845 }
846 __r = _STD::move_backward(__lb, __le, __r);
847 __n -= __bs;
848 __l -= __bs - 1;
849 }
850 return __r;
851}
852
853template <bool>
854class __deque_base_common
855{
856protected:
857 void __throw_length_error() const;
858 void __throw_out_of_range() const;
859};
860
861template <bool __b>
862void
863__deque_base_common<__b>::__throw_length_error() const
864{
865#ifndef _LIBCPP_NO_EXCEPTIONS
866 throw length_error("deque");
867#endif
868}
869
870template <bool __b>
871void
872__deque_base_common<__b>::__throw_out_of_range() const
873{
874#ifndef _LIBCPP_NO_EXCEPTIONS
875 throw out_of_range("deque");
876#endif
877}
878
879template <class _Tp, class _Allocator>
880class __deque_base
881 : protected __deque_base_common<true>
882{
883 __deque_base(const __deque_base& __c);
884 __deque_base& operator=(const __deque_base& __c);
885protected:
886 typedef _Tp value_type;
887 typedef _Allocator allocator_type;
888 typedef allocator_traits<allocator_type> __alloc_traits;
889 typedef value_type& reference;
890 typedef const value_type& const_reference;
891 typedef typename __alloc_traits::size_type size_type;
892 typedef typename __alloc_traits::difference_type difference_type;
893 typedef typename __alloc_traits::pointer pointer;
894 typedef typename __alloc_traits::const_pointer const_pointer;
895
896 static const difference_type __block_size = sizeof(value_type) < 256 ? 4096 / sizeof(value_type) : 16;
897
898 typedef typename __alloc_traits::template
899#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
900 rebind_alloc<pointer>
901#else
902 rebind_alloc<pointer>::other
903#endif
904 __pointer_allocator;
905 typedef allocator_traits<__pointer_allocator> __map_traits;
906 typedef typename __map_traits::pointer __map_pointer;
907 typedef typename __map_traits::const_pointer __map_const_pointer;
908 typedef __split_buffer<pointer, __pointer_allocator> __map;
909
910 typedef __deque_iterator<value_type, pointer, reference, __map_pointer,
911 difference_type, __block_size> iterator;
912 typedef __deque_iterator<value_type, const_pointer, const_reference, __map_const_pointer,
913 difference_type, __block_size> const_iterator;
914
915 __map __map_;
916 size_type __start_;
917 __compressed_pair<size_type, allocator_type> __size_;
918
Howard Hinnanta12beb32011-06-02 16:10:22 +0000919 iterator begin() _NOEXCEPT;
920 const_iterator begin() const _NOEXCEPT;
921 iterator end() _NOEXCEPT;
922 const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000923
924 _LIBCPP_INLINE_VISIBILITY size_type& size() {return __size_.first();}
Howard Hinnanta12beb32011-06-02 16:10:22 +0000925 _LIBCPP_INLINE_VISIBILITY
926 const size_type& size() const _NOEXCEPT {return __size_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000927 _LIBCPP_INLINE_VISIBILITY allocator_type& __alloc() {return __size_.second();}
Howard Hinnanta12beb32011-06-02 16:10:22 +0000928 _LIBCPP_INLINE_VISIBILITY
929 const allocator_type& __alloc() const _NOEXCEPT {return __size_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000930
931 __deque_base();
932 explicit __deque_base(const allocator_type& __a);
933 ~__deque_base();
934
Howard Hinnant73d21a42010-09-04 23:28:19 +0000935#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000936
937 __deque_base(__deque_base&& __c);
938 __deque_base(__deque_base&& __c, const allocator_type& __a);
939
Howard Hinnant73d21a42010-09-04 23:28:19 +0000940#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000941 void swap(__deque_base& __c);
Howard Hinnanta12beb32011-06-02 16:10:22 +0000942 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000943
944 bool __invariants() const;
945
Howard Hinnant422a53f2010-09-21 21:28:23 +0000946 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000947 void __move_assign(__deque_base& __c)
948 {
949 __map_ = _STD::move(__c.__map_);
950 __start_ = __c.__start_;
951 size() = __c.size();
952 __move_assign_alloc(__c);
953 __c.__start_ = __c.size() = 0;
954 }
955
Howard Hinnant422a53f2010-09-21 21:28:23 +0000956 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000957 void __move_assign_alloc(__deque_base& __c)
958 {__move_assign_alloc(__c, integral_constant<bool,
959 __alloc_traits::propagate_on_container_move_assignment::value>());}
960
961private:
Howard Hinnant422a53f2010-09-21 21:28:23 +0000962 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000963 void __move_assign_alloc(const __deque_base& __c, true_type)
964 {
965 __alloc() = _STD::move(__c.__alloc());
966 }
967
Howard Hinnant422a53f2010-09-21 21:28:23 +0000968 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000969 void __move_assign_alloc(const __deque_base& __c, false_type)
970 {}
971
Howard Hinnant422a53f2010-09-21 21:28:23 +0000972 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000973 static void __swap_alloc(allocator_type& __x, allocator_type& __y)
974 {__swap_alloc(__x, __y, integral_constant<bool,
975 __alloc_traits::propagate_on_container_swap::value>());}
976
Howard Hinnant422a53f2010-09-21 21:28:23 +0000977 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000978 static void __swap_alloc(allocator_type& __x, allocator_type& __y, true_type)
979 {
980 using _STD::swap;
981 swap(__x, __y);
982 }
Howard Hinnant422a53f2010-09-21 21:28:23 +0000983
984 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000985 static void __swap_alloc(allocator_type& __x, allocator_type& __y, false_type)
986 {}
987};
988
989template <class _Tp, class _Allocator>
990bool
991__deque_base<_Tp, _Allocator>::__invariants() const
992{
993 if (!__map_.__invariants())
994 return false;
995 if (__map_.size() >= size_type(-1) / __block_size)
996 return false;
997 for (typename __map::const_iterator __i = __map_.begin(), __e = __map_.end();
998 __i != __e; ++__i)
999 if (*__i == nullptr)
1000 return false;
1001 if (__map_.size() != 0)
1002 {
1003 if (size() >= __map_.size() * __block_size)
1004 return false;
1005 if (__start_ >= __map_.size() * __block_size - size())
1006 return false;
1007 }
1008 else
1009 {
1010 if (size() != 0)
1011 return false;
1012 if (__start_ != 0)
1013 return false;
1014 }
1015 return true;
1016}
1017
1018template <class _Tp, class _Allocator>
1019typename __deque_base<_Tp, _Allocator>::iterator
Howard Hinnanta12beb32011-06-02 16:10:22 +00001020__deque_base<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001021{
1022 __map_pointer __mp = __map_.begin() + __start_ / __block_size;
1023 return iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
1024}
1025
1026template <class _Tp, class _Allocator>
1027typename __deque_base<_Tp, _Allocator>::const_iterator
Howard Hinnanta12beb32011-06-02 16:10:22 +00001028__deque_base<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001029{
1030 __map_const_pointer __mp = __map_.begin() + __start_ / __block_size;
1031 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
1032}
1033
1034template <class _Tp, class _Allocator>
1035typename __deque_base<_Tp, _Allocator>::iterator
Howard Hinnanta12beb32011-06-02 16:10:22 +00001036__deque_base<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001037{
1038 size_type __p = size() + __start_;
1039 __map_pointer __mp = __map_.begin() + __p / __block_size;
1040 return iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1041}
1042
1043template <class _Tp, class _Allocator>
1044typename __deque_base<_Tp, _Allocator>::const_iterator
Howard Hinnanta12beb32011-06-02 16:10:22 +00001045__deque_base<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001046{
1047 size_type __p = size() + __start_;
1048 __map_const_pointer __mp = __map_.begin() + __p / __block_size;
1049 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1050}
1051
1052template <class _Tp, class _Allocator>
1053inline _LIBCPP_INLINE_VISIBILITY
1054__deque_base<_Tp, _Allocator>::__deque_base()
1055 : __start_(0), __size_(0) {}
1056
1057template <class _Tp, class _Allocator>
1058inline _LIBCPP_INLINE_VISIBILITY
1059__deque_base<_Tp, _Allocator>::__deque_base(const allocator_type& __a)
1060 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {}
1061
1062template <class _Tp, class _Allocator>
1063__deque_base<_Tp, _Allocator>::~__deque_base()
1064{
1065 clear();
1066 typename __map::iterator __i = __map_.begin();
1067 typename __map::iterator __e = __map_.end();
1068 for (; __i != __e; ++__i)
1069 __alloc_traits::deallocate(__alloc(), *__i, __block_size);
1070}
1071
Howard Hinnant73d21a42010-09-04 23:28:19 +00001072#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001073
1074template <class _Tp, class _Allocator>
1075__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c)
1076 : __map_(_STD::move(__c.__map_)),
1077 __start_(_STD::move(__c.__start_)),
1078 __size_(_STD::move(__c.__size_))
1079{
1080 __c.__start_ = 0;
1081 __c.size() = 0;
1082}
1083
1084template <class _Tp, class _Allocator>
1085__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c, const allocator_type& __a)
1086 : __map_(_STD::move(__c.__map_), __pointer_allocator(__a)),
1087 __start_(_STD::move(__c.__start_)),
1088 __size_(_STD::move(__c.size()), __a)
1089{
1090 if (__a == __c.__alloc())
1091 {
1092 __c.__start_ = 0;
1093 __c.size() = 0;
1094 }
1095 else
1096 {
1097 __map_.clear();
1098 __start_ = 0;
1099 size() = 0;
1100 }
1101}
1102
Howard Hinnant73d21a42010-09-04 23:28:19 +00001103#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001104
1105template <class _Tp, class _Allocator>
1106void
1107__deque_base<_Tp, _Allocator>::swap(__deque_base& __c)
1108{
1109 __map_.swap(__c.__map_);
1110 _STD::swap(__start_, __c.__start_);
1111 _STD::swap(size(), __c.size());
1112 __swap_alloc(__alloc(), __c.__alloc());
1113}
1114
1115template <class _Tp, class _Allocator>
1116void
Howard Hinnanta12beb32011-06-02 16:10:22 +00001117__deque_base<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001118{
1119 allocator_type& __a = __alloc();
1120 for (iterator __i = begin(), __e = end(); __i != __e; ++__i)
Howard Hinnant2529d022011-02-02 17:36:20 +00001121 __alloc_traits::destroy(__a, _STD::addressof(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001122 size() = 0;
1123 while (__map_.size() > 2)
1124 {
1125 __alloc_traits::deallocate(__a, __map_.front(), __block_size);
1126 __map_.pop_front();
1127 }
1128 switch (__map_.size())
1129 {
1130 case 1:
1131 __start_ = __block_size / 2;
1132 break;
1133 case 2:
1134 __start_ = __block_size;
1135 break;
1136 }
1137}
1138
1139template <class _Tp, class _Allocator = allocator<_Tp> >
Howard Hinnant422a53f2010-09-21 21:28:23 +00001140class _LIBCPP_VISIBLE deque
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001141 : private __deque_base<_Tp, _Allocator>
1142{
1143public:
1144 // types:
Howard Hinnant324bb032010-08-22 00:02:43 +00001145
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001146 typedef _Tp value_type;
1147 typedef _Allocator allocator_type;
1148
1149 typedef __deque_base<value_type, allocator_type> __base;
1150
1151 typedef typename __base::__alloc_traits __alloc_traits;
1152 typedef typename __base::reference reference;
1153 typedef typename __base::const_reference const_reference;
1154 typedef typename __base::iterator iterator;
1155 typedef typename __base::const_iterator const_iterator;
1156 typedef typename __base::size_type size_type;
1157 typedef typename __base::difference_type difference_type;
1158
1159 typedef typename __base::pointer pointer;
1160 typedef typename __base::const_pointer const_pointer;
1161 typedef _STD::reverse_iterator<iterator> reverse_iterator;
1162 typedef _STD::reverse_iterator<const_iterator> const_reverse_iterator;
1163
1164 // construct/copy/destroy:
1165 _LIBCPP_INLINE_VISIBILITY deque() {}
1166 _LIBCPP_INLINE_VISIBILITY deque(const allocator_type& __a) : __base(__a) {}
1167 explicit deque(size_type __n);
1168 deque(size_type __n, const value_type& __v);
1169 deque(size_type __n, const value_type& __v, const allocator_type& __a);
1170 template <class _InputIter>
1171 deque(_InputIter __f, _InputIter __l,
1172 typename enable_if<__is_input_iterator<_InputIter>::value>::type* = 0);
1173 template <class _InputIter>
1174 deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1175 typename enable_if<__is_input_iterator<_InputIter>::value>::type* = 0);
1176 deque(const deque& __c);
1177 deque(const deque& __c, const allocator_type& __a);
1178 deque(initializer_list<value_type> __il);
1179 deque(initializer_list<value_type> __il, const allocator_type& __a);
1180
1181 deque& operator=(const deque& __c);
Howard Hinnant422a53f2010-09-21 21:28:23 +00001182 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001183 deque& operator=(initializer_list<value_type> __il) {assign(__il); return *this;}
1184
Howard Hinnant73d21a42010-09-04 23:28:19 +00001185#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001186 deque(deque&& __c);
1187 deque(deque&& __c, const allocator_type& __a);
1188 deque& operator=(deque&& __c);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001189#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001190
1191 template <class _InputIter>
1192 void assign(_InputIter __f, _InputIter __l,
1193 typename enable_if<__is_input_iterator<_InputIter>::value &&
1194 !__is_random_access_iterator<_InputIter>::value>::type* = 0);
1195 template <class _RAIter>
1196 void assign(_RAIter __f, _RAIter __l,
1197 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
1198 void assign(size_type __n, const value_type& __v);
Howard Hinnant422a53f2010-09-21 21:28:23 +00001199 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001200 void assign(initializer_list<value_type> __il) {assign(__il.begin(), __il.end());}
1201
Howard Hinnanta12beb32011-06-02 16:10:22 +00001202 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001203
1204 // iterators:
1205
Howard Hinnant422a53f2010-09-21 21:28:23 +00001206 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001207 iterator begin() _NOEXCEPT {return __base::begin();}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001208 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001209 const_iterator begin() const _NOEXCEPT {return __base::begin();}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001210 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001211 iterator end() _NOEXCEPT {return __base::end();}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001212 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001213 const_iterator end() const _NOEXCEPT {return __base::end();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001214
Howard Hinnant422a53f2010-09-21 21:28:23 +00001215 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001216 reverse_iterator rbegin() _NOEXCEPT
1217 {return reverse_iterator(__base::end());}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001218 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001219 const_reverse_iterator rbegin() const _NOEXCEPT
1220 {return const_reverse_iterator(__base::end());}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001222 reverse_iterator rend() _NOEXCEPT
1223 {return reverse_iterator(__base::begin());}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001224 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001225 const_reverse_iterator rend() const _NOEXCEPT
1226 {return const_reverse_iterator(__base::begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001227
Howard Hinnant422a53f2010-09-21 21:28:23 +00001228 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001229 const_iterator cbegin() const _NOEXCEPT
1230 {return __base::begin();}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001231 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001232 const_iterator cend() const _NOEXCEPT
1233 {return __base::end();}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001235 const_reverse_iterator crbegin() const _NOEXCEPT
1236 {return const_reverse_iterator(__base::end());}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001237 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001238 const_reverse_iterator crend() const _NOEXCEPT
1239 {return const_reverse_iterator(__base::begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001240
1241 // capacity:
Howard Hinnant422a53f2010-09-21 21:28:23 +00001242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001243 size_type size() const _NOEXCEPT {return __base::size();}
1244 _LIBCPP_INLINE_VISIBILITY
1245 size_type max_size() const _NOEXCEPT
1246 {return __alloc_traits::max_size(__base::__alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001247 void resize(size_type __n);
1248 void resize(size_type __n, const value_type& __v);
1249 void shrink_to_fit();
Howard Hinnanta12beb32011-06-02 16:10:22 +00001250 _LIBCPP_INLINE_VISIBILITY
1251 bool empty() const _NOEXCEPT {return __base::size() == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001252
1253 // element access:
1254 reference operator[](size_type __i);
1255 const_reference operator[](size_type __i) const;
1256 reference at(size_type __i);
1257 const_reference at(size_type __i) const;
1258 reference front();
1259 const_reference front() const;
1260 reference back();
1261 const_reference back() const;
1262
1263 // 23.2.2.3 modifiers:
1264 void push_front(const value_type& __v);
1265 void push_back(const value_type& __v);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001266#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1267#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001268 template <class... _Args> void emplace_front(_Args&&... __args);
1269 template <class... _Args> void emplace_back(_Args&&... __args);
1270 template <class... _Args> iterator emplace(const_iterator __p, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001271#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001272 void push_front(value_type&& __v);
1273 void push_back(value_type&& __v);
1274 iterator insert(const_iterator __p, value_type&& __v);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001275#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001276 iterator insert(const_iterator __p, const value_type& __v);
1277 iterator insert(const_iterator __p, size_type __n, const value_type& __v);
1278 template <class _InputIter>
1279 iterator insert (const_iterator __p, _InputIter __f, _InputIter __l,
1280 typename enable_if<__is_input_iterator<_InputIter>::value
1281 &&!__is_bidirectional_iterator<_InputIter>::value>::type* = 0);
1282 template <class _BiIter>
1283 iterator insert (const_iterator __p, _BiIter __f, _BiIter __l,
1284 typename enable_if<__is_bidirectional_iterator<_BiIter>::value>::type* = 0);
Howard Hinnant422a53f2010-09-21 21:28:23 +00001285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001286 iterator insert(const_iterator __p, initializer_list<value_type> __il)
1287 {return insert(__p, __il.begin(), __il.end());}
1288 void pop_front();
1289 void pop_back();
1290 iterator erase(const_iterator __p);
1291 iterator erase(const_iterator __f, const_iterator __l);
1292
1293 void swap(deque& __c);
Howard Hinnanta12beb32011-06-02 16:10:22 +00001294 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001295
Howard Hinnant422a53f2010-09-21 21:28:23 +00001296 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001297 bool __invariants() const {return __base::__invariants();}
1298private:
Howard Hinnant422a53f2010-09-21 21:28:23 +00001299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001300 static size_type __recommend_blocks(size_type __n)
1301 {
1302 return __n / __base::__block_size + (__n % __base::__block_size != 0);
1303 }
Howard Hinnant422a53f2010-09-21 21:28:23 +00001304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001305 size_type __capacity() const
1306 {
1307 return __base::__map_.size() == 0 ? 0 : __base::__map_.size() * __base::__block_size - 1;
1308 }
Howard Hinnant422a53f2010-09-21 21:28:23 +00001309 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001310 size_type __front_spare() const
1311 {
1312 return __base::__start_;
1313 }
Howard Hinnant422a53f2010-09-21 21:28:23 +00001314 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001315 size_type __back_spare() const
1316 {
1317 return __capacity() - (__base::__start_ + __base::size());
1318 }
1319
1320 template <class _InpIter>
1321 void __append(_InpIter __f, _InpIter __l,
1322 typename enable_if<__is_input_iterator<_InpIter>::value &&
1323 !__is_forward_iterator<_InpIter>::value>::type* = 0);
1324 template <class _ForIter>
1325 void __append(_ForIter __f, _ForIter __l,
1326 typename enable_if<__is_forward_iterator<_ForIter>::value>::type* = 0);
1327 void __append(size_type __n);
1328 void __append(size_type __n, const value_type& __v);
1329 void __erase_to_end(const_iterator __f);
1330 void __add_front_capacity();
1331 void __add_front_capacity(size_type __n);
1332 void __add_back_capacity();
1333 void __add_back_capacity(size_type __n);
1334 iterator __move_and_check(iterator __f, iterator __l, iterator __r,
1335 const_pointer& __vt);
1336 iterator __move_backward_and_check(iterator __f, iterator __l, iterator __r,
1337 const_pointer& __vt);
1338 void __move_construct_and_check(iterator __f, iterator __l,
1339 iterator __r, const_pointer& __vt);
1340 void __move_construct_backward_and_check(iterator __f, iterator __l,
1341 iterator __r, const_pointer& __vt);
1342
Howard Hinnant422a53f2010-09-21 21:28:23 +00001343 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001344 void __copy_assign_alloc(const deque& __c)
1345 {__copy_assign_alloc(__c, integral_constant<bool,
1346 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1347
Howard Hinnant422a53f2010-09-21 21:28:23 +00001348 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001349 void __copy_assign_alloc(const deque& __c, true_type)
1350 {
1351 if (__base::__alloc() != __c.__alloc())
1352 {
1353 clear();
1354 shrink_to_fit();
1355 }
1356 __base::__alloc() = __c.__alloc();
1357 __base::__map_.__alloc() = __c.__map_.__alloc();
1358 }
1359
Howard Hinnant422a53f2010-09-21 21:28:23 +00001360 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001361 void __copy_assign_alloc(const deque& __c, false_type)
1362 {}
1363
1364 void __move_assign(deque& __c, true_type);
1365 void __move_assign(deque& __c, false_type);
1366};
1367
1368template <class _Tp, class _Allocator>
1369deque<_Tp, _Allocator>::deque(size_type __n)
1370{
1371 if (__n > 0)
1372 __append(__n);
1373}
1374
1375template <class _Tp, class _Allocator>
1376deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v)
1377{
1378 if (__n > 0)
1379 __append(__n, __v);
1380}
1381
1382template <class _Tp, class _Allocator>
1383deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v, const allocator_type& __a)
1384 : __base(__a)
1385{
1386 if (__n > 0)
1387 __append(__n, __v);
1388}
1389
1390template <class _Tp, class _Allocator>
1391template <class _InputIter>
1392deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l,
1393 typename enable_if<__is_input_iterator<_InputIter>::value>::type*)
1394{
1395 __append(__f, __l);
1396}
1397
1398template <class _Tp, class _Allocator>
1399template <class _InputIter>
1400deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1401 typename enable_if<__is_input_iterator<_InputIter>::value>::type*)
1402 : __base(__a)
1403{
1404 __append(__f, __l);
1405}
1406
1407template <class _Tp, class _Allocator>
1408deque<_Tp, _Allocator>::deque(const deque& __c)
1409 : __base(__alloc_traits::select_on_container_copy_construction(__c.__alloc()))
1410{
1411 __append(__c.begin(), __c.end());
1412}
1413
1414template <class _Tp, class _Allocator>
1415deque<_Tp, _Allocator>::deque(const deque& __c, const allocator_type& __a)
1416 : __base(__a)
1417{
1418 __append(__c.begin(), __c.end());
1419}
1420
1421template <class _Tp, class _Allocator>
1422deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il)
1423{
1424 __append(__il.begin(), __il.end());
1425}
1426
1427template <class _Tp, class _Allocator>
1428deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il, const allocator_type& __a)
1429 : __base(__a)
1430{
1431 __append(__il.begin(), __il.end());
1432}
1433
1434template <class _Tp, class _Allocator>
1435deque<_Tp, _Allocator>&
1436deque<_Tp, _Allocator>::operator=(const deque& __c)
1437{
1438 if (this != &__c)
1439 {
1440 __copy_assign_alloc(__c);
1441 assign(__c.begin(), __c.end());
1442 }
1443 return *this;
1444}
1445
Howard Hinnant73d21a42010-09-04 23:28:19 +00001446#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001447
1448template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001449inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001450deque<_Tp, _Allocator>::deque(deque&& __c)
1451 : __base(_STD::move(__c))
1452{
1453}
1454
1455template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001456inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001457deque<_Tp, _Allocator>::deque(deque&& __c, const allocator_type& __a)
1458 : __base(_STD::move(__c), __a)
1459{
1460 if (__a != __c.__alloc())
1461 {
1462 typedef move_iterator<iterator> _I;
1463 assign(_I(__c.begin()), _I(__c.end()));
1464 }
1465}
1466
1467template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001468inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001469deque<_Tp, _Allocator>&
1470deque<_Tp, _Allocator>::operator=(deque&& __c)
1471{
1472 __move_assign(__c, integral_constant<bool,
1473 __alloc_traits::propagate_on_container_move_assignment::value>());
1474 return *this;
1475}
1476
1477template <class _Tp, class _Allocator>
1478void
1479deque<_Tp, _Allocator>::__move_assign(deque& __c, false_type)
1480{
1481 if (__base::__alloc() != __c.__alloc())
1482 {
1483 typedef move_iterator<iterator> _I;
1484 assign(_I(__c.begin()), _I(__c.end()));
1485 }
1486 else
1487 __move_assign(__c, true_type());
1488}
1489
1490template <class _Tp, class _Allocator>
1491void
1492deque<_Tp, _Allocator>::__move_assign(deque& __c, true_type)
1493{
1494 clear();
1495 shrink_to_fit();
1496 __base::__move_assign(__c);
1497}
1498
Howard Hinnant73d21a42010-09-04 23:28:19 +00001499#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001500
1501template <class _Tp, class _Allocator>
1502template <class _InputIter>
1503void
1504deque<_Tp, _Allocator>::assign(_InputIter __f, _InputIter __l,
1505 typename enable_if<__is_input_iterator<_InputIter>::value &&
1506 !__is_random_access_iterator<_InputIter>::value>::type*)
1507{
1508 iterator __i = __base::begin();
1509 iterator __e = __base::end();
1510 for (; __f != __l && __i != __e; ++__f, ++__i)
1511 *__i = *__f;
1512 if (__f != __l)
1513 __append(__f, __l);
1514 else
1515 __erase_to_end(__i);
1516}
1517
1518template <class _Tp, class _Allocator>
1519template <class _RAIter>
1520void
1521deque<_Tp, _Allocator>::assign(_RAIter __f, _RAIter __l,
1522 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
1523{
1524 if (static_cast<size_type>(__l - __f) > __base::size())
1525 {
1526 _RAIter __m = __f + __base::size();
1527 _STD::copy(__f, __m, __base::begin());
1528 __append(__m, __l);
1529 }
1530 else
1531 __erase_to_end(_STD::copy(__f, __l, __base::begin()));
1532}
1533
1534template <class _Tp, class _Allocator>
1535void
1536deque<_Tp, _Allocator>::assign(size_type __n, const value_type& __v)
1537{
1538 if (__n > __base::size())
1539 {
1540 _STD::fill_n(__base::begin(), __base::size(), __v);
1541 __n -= __base::size();
1542 __append(__n, __v);
1543 }
1544 else
1545 __erase_to_end(_STD::fill_n(__base::begin(), __n, __v));
1546}
1547
1548template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001549inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001550_Allocator
Howard Hinnanta12beb32011-06-02 16:10:22 +00001551deque<_Tp, _Allocator>::get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001552{
1553 return __base::__alloc();
1554}
1555
1556template <class _Tp, class _Allocator>
1557void
1558deque<_Tp, _Allocator>::resize(size_type __n)
1559{
1560 if (__n > __base::size())
1561 __append(__n - __base::size());
1562 else if (__n < __base::size())
1563 __erase_to_end(__base::begin() + __n);
1564}
1565
1566template <class _Tp, class _Allocator>
1567void
1568deque<_Tp, _Allocator>::resize(size_type __n, const value_type& __v)
1569{
1570 if (__n > __base::size())
1571 __append(__n - __base::size(), __v);
1572 else if (__n < __base::size())
1573 __erase_to_end(__base::begin() + __n);
1574}
1575
1576template <class _Tp, class _Allocator>
1577void
1578deque<_Tp, _Allocator>::shrink_to_fit()
1579{
1580 allocator_type& __a = __base::__alloc();
1581 if (empty())
1582 {
1583 while (__base::__map_.size() > 0)
1584 {
1585 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1586 __base::__map_.pop_back();
1587 }
1588 __base::__start_ = 0;
1589 }
1590 else
1591 {
1592 if (__front_spare() >= __base::__block_size)
1593 {
1594 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
1595 __base::__map_.pop_front();
1596 __base::__start_ -= __base::__block_size;
1597 }
1598 if (__back_spare() >= __base::__block_size)
1599 {
1600 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1601 __base::__map_.pop_back();
1602 }
1603 }
1604 __base::__map_.shrink_to_fit();
1605}
1606
1607template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001608inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001609typename deque<_Tp, _Allocator>::reference
1610deque<_Tp, _Allocator>::operator[](size_type __i)
1611{
1612 size_type __p = __base::__start_ + __i;
1613 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1614}
1615
1616template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001617inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001618typename deque<_Tp, _Allocator>::const_reference
1619deque<_Tp, _Allocator>::operator[](size_type __i) const
1620{
1621 size_type __p = __base::__start_ + __i;
1622 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1623}
1624
1625template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001626inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001627typename deque<_Tp, _Allocator>::reference
1628deque<_Tp, _Allocator>::at(size_type __i)
1629{
1630 if (__i >= __base::size())
1631 __base::__throw_out_of_range();
1632 size_type __p = __base::__start_ + __i;
1633 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1634}
1635
1636template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001637inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001638typename deque<_Tp, _Allocator>::const_reference
1639deque<_Tp, _Allocator>::at(size_type __i) const
1640{
1641 if (__i >= __base::size())
1642 __base::__throw_out_of_range();
1643 size_type __p = __base::__start_ + __i;
1644 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1645}
1646
1647template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001648inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001649typename deque<_Tp, _Allocator>::reference
1650deque<_Tp, _Allocator>::front()
1651{
1652 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1653 + __base::__start_ % __base::__block_size);
1654}
1655
1656template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001657inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001658typename deque<_Tp, _Allocator>::const_reference
1659deque<_Tp, _Allocator>::front() const
1660{
1661 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1662 + __base::__start_ % __base::__block_size);
1663}
1664
1665template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001666inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001667typename deque<_Tp, _Allocator>::reference
1668deque<_Tp, _Allocator>::back()
1669{
1670 size_type __p = __base::size() + __base::__start_ - 1;
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>::back() const
1678{
1679 size_type __p = __base::size() + __base::__start_ - 1;
1680 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1681}
1682
1683template <class _Tp, class _Allocator>
1684void
1685deque<_Tp, _Allocator>::push_back(const value_type& __v)
1686{
1687 allocator_type& __a = __base::__alloc();
1688 if (__back_spare() == 0)
1689 __add_back_capacity();
1690 // __back_spare() >= 1
Howard Hinnant2529d022011-02-02 17:36:20 +00001691 __alloc_traits::construct(__a, _STD::addressof(*__base::end()), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001692 ++__base::size();
1693}
1694
Howard Hinnant73d21a42010-09-04 23:28:19 +00001695#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001696
1697template <class _Tp, class _Allocator>
1698void
1699deque<_Tp, _Allocator>::push_back(value_type&& __v)
1700{
1701 allocator_type& __a = __base::__alloc();
1702 if (__back_spare() == 0)
1703 __add_back_capacity();
1704 // __back_spare() >= 1
Howard Hinnant2529d022011-02-02 17:36:20 +00001705 __alloc_traits::construct(__a, _STD::addressof(*__base::end()), _STD::move(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001706 ++__base::size();
1707}
1708
Howard Hinnant73d21a42010-09-04 23:28:19 +00001709#ifndef _LIBCPP_HAS_NO_VARIADICS
1710
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001711template <class _Tp, class _Allocator>
1712template <class... _Args>
1713void
1714deque<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1715{
1716 allocator_type& __a = __base::__alloc();
1717 if (__back_spare() == 0)
1718 __add_back_capacity();
1719 // __back_spare() >= 1
Howard Hinnant2529d022011-02-02 17:36:20 +00001720 __alloc_traits::construct(__a, _STD::addressof(*__base::end()), _STD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001721 ++__base::size();
1722}
1723
Howard Hinnant73d21a42010-09-04 23:28:19 +00001724#endif // _LIBCPP_HAS_NO_VARIADICS
1725#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001726
1727template <class _Tp, class _Allocator>
1728void
1729deque<_Tp, _Allocator>::push_front(const value_type& __v)
1730{
1731 allocator_type& __a = __base::__alloc();
1732 if (__front_spare() == 0)
1733 __add_front_capacity();
1734 // __front_spare() >= 1
Howard Hinnant2529d022011-02-02 17:36:20 +00001735 __alloc_traits::construct(__a, _STD::addressof(*--__base::begin()), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001736 --__base::__start_;
1737 ++__base::size();
1738}
1739
Howard Hinnant73d21a42010-09-04 23:28:19 +00001740#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001741
1742template <class _Tp, class _Allocator>
1743void
1744deque<_Tp, _Allocator>::push_front(value_type&& __v)
1745{
1746 allocator_type& __a = __base::__alloc();
1747 if (__front_spare() == 0)
1748 __add_front_capacity();
1749 // __front_spare() >= 1
Howard Hinnant2529d022011-02-02 17:36:20 +00001750 __alloc_traits::construct(__a, _STD::addressof(*--__base::begin()), _STD::move(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001751 --__base::__start_;
1752 ++__base::size();
1753}
1754
Howard Hinnant73d21a42010-09-04 23:28:19 +00001755#ifndef _LIBCPP_HAS_NO_VARIADICS
1756
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001757template <class _Tp, class _Allocator>
1758template <class... _Args>
1759void
1760deque<_Tp, _Allocator>::emplace_front(_Args&&... __args)
1761{
1762 allocator_type& __a = __base::__alloc();
1763 if (__front_spare() == 0)
1764 __add_front_capacity();
1765 // __front_spare() >= 1
Howard Hinnant2529d022011-02-02 17:36:20 +00001766 __alloc_traits::construct(__a, _STD::addressof(*--__base::begin()), _STD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001767 --__base::__start_;
1768 ++__base::size();
1769}
1770
Howard Hinnant73d21a42010-09-04 23:28:19 +00001771#endif // _LIBCPP_HAS_NO_VARIADICS
1772#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001773
1774template <class _Tp, class _Allocator>
1775typename deque<_Tp, _Allocator>::iterator
1776deque<_Tp, _Allocator>::insert(const_iterator __p, const value_type& __v)
1777{
1778 size_type __pos = __p - __base::begin();
1779 size_type __to_end = __base::size() - __pos;
1780 allocator_type& __a = __base::__alloc();
1781 if (__pos < __to_end)
1782 { // insert by shifting things backward
1783 if (__front_spare() == 0)
1784 __add_front_capacity();
1785 // __front_spare() >= 1
1786 if (__pos == 0)
1787 {
Howard Hinnant2529d022011-02-02 17:36:20 +00001788 __alloc_traits::construct(__a, _STD::addressof(*--__base::begin()), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001789 --__base::__start_;
1790 ++__base::size();
1791 }
1792 else
1793 {
1794 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1795 iterator __b = __base::begin();
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00001796 iterator __bm1 = _STD::prev(__b);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001797 if (__vt == pointer_traits<const_pointer>::pointer_to(*__b))
1798 __vt = pointer_traits<const_pointer>::pointer_to(*__bm1);
Howard Hinnant2529d022011-02-02 17:36:20 +00001799 __alloc_traits::construct(__a, _STD::addressof(*__bm1), _STD::move(*__b));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001800 --__base::__start_;
1801 ++__base::size();
1802 if (__pos > 1)
Douglas Gregor7ac6af72011-04-29 16:20:26 +00001803 __b = __move_and_check(_STD::next(__b), __b + __pos, __b, __vt);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001804 *__b = *__vt;
1805 }
1806 }
1807 else
1808 { // insert by shifting things forward
1809 if (__back_spare() == 0)
1810 __add_back_capacity();
1811 // __back_capacity >= 1
1812 size_type __de = __base::size() - __pos;
1813 if (__de == 0)
1814 {
Howard Hinnant2529d022011-02-02 17:36:20 +00001815 __alloc_traits::construct(__a, _STD::addressof(*__base::end()), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001816 ++__base::size();
1817 }
1818 else
1819 {
1820 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1821 iterator __e = __base::end();
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00001822 iterator __em1 = _STD::prev(__e);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001823 if (__vt == pointer_traits<const_pointer>::pointer_to(*__em1))
1824 __vt = pointer_traits<const_pointer>::pointer_to(*__e);
Howard Hinnant2529d022011-02-02 17:36:20 +00001825 __alloc_traits::construct(__a, _STD::addressof(*__e), _STD::move(*__em1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001826 ++__base::size();
1827 if (__de > 1)
1828 __e = __move_backward_and_check(__e - __de, __em1, __e, __vt);
1829 *--__e = *__vt;
1830 }
1831 }
1832 return __base::begin() + __pos;
1833}
1834
Howard Hinnant73d21a42010-09-04 23:28:19 +00001835#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001836
1837template <class _Tp, class _Allocator>
1838typename deque<_Tp, _Allocator>::iterator
1839deque<_Tp, _Allocator>::insert(const_iterator __p, value_type&& __v)
1840{
1841 size_type __pos = __p - __base::begin();
1842 size_type __to_end = __base::size() - __pos;
1843 allocator_type& __a = __base::__alloc();
1844 if (__pos < __to_end)
1845 { // insert by shifting things backward
1846 if (__front_spare() == 0)
1847 __add_front_capacity();
1848 // __front_spare() >= 1
1849 if (__pos == 0)
1850 {
Howard Hinnant2529d022011-02-02 17:36:20 +00001851 __alloc_traits::construct(__a, _STD::addressof(*--__base::begin()), _STD::move(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001852 --__base::__start_;
1853 ++__base::size();
1854 }
1855 else
1856 {
1857 iterator __b = __base::begin();
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00001858 iterator __bm1 = _STD::prev(__b);
Howard Hinnant2529d022011-02-02 17:36:20 +00001859 __alloc_traits::construct(__a, _STD::addressof(*__bm1), _STD::move(*__b));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001860 --__base::__start_;
1861 ++__base::size();
1862 if (__pos > 1)
Douglas Gregor7ac6af72011-04-29 16:20:26 +00001863 __b = _STD::move(_STD::next(__b), __b + __pos, __b);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001864 *__b = _STD::move(__v);
1865 }
1866 }
1867 else
1868 { // insert by shifting things forward
1869 if (__back_spare() == 0)
1870 __add_back_capacity();
1871 // __back_capacity >= 1
1872 size_type __de = __base::size() - __pos;
1873 if (__de == 0)
1874 {
Howard Hinnant2529d022011-02-02 17:36:20 +00001875 __alloc_traits::construct(__a, _STD::addressof(*__base::end()), _STD::move(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001876 ++__base::size();
1877 }
1878 else
1879 {
1880 iterator __e = __base::end();
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00001881 iterator __em1 = _STD::prev(__e);
Howard Hinnant2529d022011-02-02 17:36:20 +00001882 __alloc_traits::construct(__a, _STD::addressof(*__e), _STD::move(*__em1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001883 ++__base::size();
1884 if (__de > 1)
1885 __e = _STD::move_backward(__e - __de, __em1, __e);
1886 *--__e = _STD::move(__v);
1887 }
1888 }
1889 return __base::begin() + __pos;
1890}
1891
Howard Hinnant73d21a42010-09-04 23:28:19 +00001892#ifndef _LIBCPP_HAS_NO_VARIADICS
1893
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001894template <class _Tp, class _Allocator>
1895template <class... _Args>
1896typename deque<_Tp, _Allocator>::iterator
1897deque<_Tp, _Allocator>::emplace(const_iterator __p, _Args&&... __args)
1898{
1899 size_type __pos = __p - __base::begin();
1900 size_type __to_end = __base::size() - __pos;
1901 allocator_type& __a = __base::__alloc();
1902 if (__pos < __to_end)
1903 { // insert by shifting things backward
1904 if (__front_spare() == 0)
1905 __add_front_capacity();
1906 // __front_spare() >= 1
1907 if (__pos == 0)
1908 {
Howard Hinnant2529d022011-02-02 17:36:20 +00001909 __alloc_traits::construct(__a, _STD::addressof(*--__base::begin()), _STD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001910 --__base::__start_;
1911 ++__base::size();
1912 }
1913 else
1914 {
1915 iterator __b = __base::begin();
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00001916 iterator __bm1 = _STD::prev(__b);
Howard Hinnant2529d022011-02-02 17:36:20 +00001917 __alloc_traits::construct(__a, _STD::addressof(*__bm1), _STD::move(*__b));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001918 --__base::__start_;
1919 ++__base::size();
1920 if (__pos > 1)
Douglas Gregor7ac6af72011-04-29 16:20:26 +00001921 __b = _STD::move(_STD::next(__b), __b + __pos, __b);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001922 *__b = value_type(_STD::forward<_Args>(__args)...);
1923 }
1924 }
1925 else
1926 { // insert by shifting things forward
1927 if (__back_spare() == 0)
1928 __add_back_capacity();
1929 // __back_capacity >= 1
1930 size_type __de = __base::size() - __pos;
1931 if (__de == 0)
1932 {
Howard Hinnant2529d022011-02-02 17:36:20 +00001933 __alloc_traits::construct(__a, _STD::addressof(*__base::end()), _STD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001934 ++__base::size();
1935 }
1936 else
1937 {
1938 iterator __e = __base::end();
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00001939 iterator __em1 = _STD::prev(__e);
Howard Hinnant2529d022011-02-02 17:36:20 +00001940 __alloc_traits::construct(__a, _STD::addressof(*__e), _STD::move(*__em1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001941 ++__base::size();
1942 if (__de > 1)
1943 __e = _STD::move_backward(__e - __de, __em1, __e);
1944 *--__e = value_type(_STD::forward<_Args>(__args)...);
1945 }
1946 }
1947 return __base::begin() + __pos;
1948}
1949
Howard Hinnant73d21a42010-09-04 23:28:19 +00001950#endif // _LIBCPP_HAS_NO_VARIADICS
1951#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001952
1953template <class _Tp, class _Allocator>
1954typename deque<_Tp, _Allocator>::iterator
1955deque<_Tp, _Allocator>::insert(const_iterator __p, size_type __n, const value_type& __v)
1956{
1957 size_type __pos = __p - __base::begin();
1958 size_type __to_end = __base::size() - __pos;
1959 allocator_type& __a = __base::__alloc();
1960 if (__pos < __to_end)
1961 { // insert by shifting things backward
1962 if (__n > __front_spare())
1963 __add_front_capacity(__n - __front_spare());
1964 // __n <= __front_spare()
1965 size_type __old_n = __n;
1966 iterator __old_begin = __base::begin();
1967 iterator __i = __old_begin;
1968 if (__n > __pos)
1969 {
1970 for (size_type __m = __n - __pos; __m; --__m, --__base::__start_, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00001971 __alloc_traits::construct(__a, _STD::addressof(*--__i), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001972 __n = __pos;
1973 }
1974 if (__n > 0)
1975 {
1976 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1977 iterator __obn = __old_begin + __n;
1978 __move_construct_backward_and_check(__old_begin, __obn, __i, __vt);
1979 if (__n < __pos)
1980 __old_begin = __move_and_check(__obn, __old_begin + __pos, __old_begin, __vt);
1981 _STD::fill_n(__old_begin, __n, *__vt);
1982 }
1983 }
1984 else
1985 { // insert by shifting things forward
1986 size_type __back_capacity = __back_spare();
1987 if (__n > __back_capacity)
1988 __add_back_capacity(__n - __back_capacity);
1989 // __n <= __back_capacity
1990 size_type __old_n = __n;
1991 iterator __old_end = __base::end();
1992 iterator __i = __old_end;
1993 size_type __de = __base::size() - __pos;
1994 if (__n > __de)
1995 {
1996 for (size_type __m = __n - __de; __m; --__m, ++__i, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00001997 __alloc_traits::construct(__a, _STD::addressof(*__i), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001998 __n = __de;
1999 }
2000 if (__n > 0)
2001 {
2002 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2003 iterator __oen = __old_end - __n;
2004 __move_construct_and_check(__oen, __old_end, __i, __vt);
2005 if (__n < __de)
2006 __old_end = __move_backward_and_check(__old_end - __de, __oen, __old_end, __vt);
2007 _STD::fill_n(__old_end - __n, __n, *__vt);
2008 }
2009 }
2010 return __base::begin() + __pos;
2011}
2012
2013template <class _Tp, class _Allocator>
2014template <class _InputIter>
2015typename deque<_Tp, _Allocator>::iterator
2016deque<_Tp, _Allocator>::insert(const_iterator __p, _InputIter __f, _InputIter __l,
2017 typename enable_if<__is_input_iterator<_InputIter>::value
2018 &&!__is_bidirectional_iterator<_InputIter>::value>::type*)
2019{
2020 __split_buffer<value_type, allocator_type&> __buf(__base::__alloc());
2021 __buf.__construct_at_end(__f, __l);
2022 typedef typename __split_buffer<value_type, allocator_type&>::iterator __bi;
2023 return insert(__p, move_iterator<__bi>(__buf.begin()), move_iterator<__bi>(__buf.end()));
2024}
2025
2026template <class _Tp, class _Allocator>
2027template <class _BiIter>
2028typename deque<_Tp, _Allocator>::iterator
2029deque<_Tp, _Allocator>::insert(const_iterator __p, _BiIter __f, _BiIter __l,
2030 typename enable_if<__is_bidirectional_iterator<_BiIter>::value>::type*)
2031{
2032 size_type __n = _STD::distance(__f, __l);
2033 size_type __pos = __p - __base::begin();
2034 size_type __to_end = __base::size() - __pos;
2035 allocator_type& __a = __base::__alloc();
2036 if (__pos < __to_end)
2037 { // insert by shifting things backward
2038 if (__n > __front_spare())
2039 __add_front_capacity(__n - __front_spare());
2040 // __n <= __front_spare()
2041 size_type __old_n = __n;
2042 iterator __old_begin = __base::begin();
2043 iterator __i = __old_begin;
2044 _BiIter __m = __f;
2045 if (__n > __pos)
2046 {
2047 __m = __pos < __n / 2 ? _STD::prev(__l, __pos) : _STD::next(__f, __n - __pos);
2048 for (_BiIter __j = __m; __j != __f; --__base::__start_, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002049 __alloc_traits::construct(__a, _STD::addressof(*--__i), *--__j);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002050 __n = __pos;
2051 }
2052 if (__n > 0)
2053 {
2054 iterator __obn = __old_begin + __n;
2055 for (iterator __j = __obn; __j != __old_begin;)
2056 {
Howard Hinnant2529d022011-02-02 17:36:20 +00002057 __alloc_traits::construct(__a, _STD::addressof(*--__i), _STD::move(*--__j));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002058 --__base::__start_;
2059 ++__base::size();
2060 }
2061 if (__n < __pos)
2062 __old_begin = _STD::move(__obn, __old_begin + __pos, __old_begin);
2063 _STD::copy(__m, __l, __old_begin);
2064 }
2065 }
2066 else
2067 { // insert by shifting things forward
2068 size_type __back_capacity = __back_spare();
2069 if (__n > __back_capacity)
2070 __add_back_capacity(__n - __back_capacity);
2071 // __n <= __back_capacity
2072 size_type __old_n = __n;
2073 iterator __old_end = __base::end();
2074 iterator __i = __old_end;
2075 _BiIter __m = __l;
2076 size_type __de = __base::size() - __pos;
2077 if (__n > __de)
2078 {
2079 __m = __de < __n / 2 ? _STD::next(__f, __de) : _STD::prev(__l, __n - __de);
2080 for (_BiIter __j = __m; __j != __l; ++__i, ++__j, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002081 __alloc_traits::construct(__a, _STD::addressof(*__i), *__j);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002082 __n = __de;
2083 }
2084 if (__n > 0)
2085 {
2086 iterator __oen = __old_end - __n;
2087 for (iterator __j = __oen; __j != __old_end; ++__i, ++__j, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002088 __alloc_traits::construct(__a, _STD::addressof(*__i), _STD::move(*__j));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002089 if (__n < __de)
2090 __old_end = _STD::move_backward(__old_end - __de, __oen, __old_end);
2091 _STD::copy_backward(__f, __m, __old_end);
2092 }
2093 }
2094 return __base::begin() + __pos;
2095}
2096
2097template <class _Tp, class _Allocator>
2098template <class _InpIter>
2099void
2100deque<_Tp, _Allocator>::__append(_InpIter __f, _InpIter __l,
2101 typename enable_if<__is_input_iterator<_InpIter>::value &&
2102 !__is_forward_iterator<_InpIter>::value>::type*)
2103{
2104 for (; __f != __l; ++__f)
2105 push_back(*__f);
2106}
2107
2108template <class _Tp, class _Allocator>
2109template <class _ForIter>
2110void
2111deque<_Tp, _Allocator>::__append(_ForIter __f, _ForIter __l,
2112 typename enable_if<__is_forward_iterator<_ForIter>::value>::type*)
2113{
2114 size_type __n = _STD::distance(__f, __l);
2115 allocator_type& __a = __base::__alloc();
2116 size_type __back_capacity = __back_spare();
2117 if (__n > __back_capacity)
2118 __add_back_capacity(__n - __back_capacity);
2119 // __n <= __back_capacity
2120 for (iterator __i = __base::end(); __f != __l; ++__i, ++__f, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002121 __alloc_traits::construct(__a, _STD::addressof(*__i), *__f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002122}
2123
2124template <class _Tp, class _Allocator>
2125void
2126deque<_Tp, _Allocator>::__append(size_type __n)
2127{
2128 allocator_type& __a = __base::__alloc();
2129 size_type __back_capacity = __back_spare();
2130 if (__n > __back_capacity)
2131 __add_back_capacity(__n - __back_capacity);
2132 // __n <= __back_capacity
2133 for (iterator __i = __base::end(); __n; --__n, ++__i, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002134 __alloc_traits::construct(__a, _STD::addressof(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002135}
2136
2137template <class _Tp, class _Allocator>
2138void
2139deque<_Tp, _Allocator>::__append(size_type __n, const value_type& __v)
2140{
2141 allocator_type& __a = __base::__alloc();
2142 size_type __back_capacity = __back_spare();
2143 if (__n > __back_capacity)
2144 __add_back_capacity(__n - __back_capacity);
2145 // __n <= __back_capacity
2146 for (iterator __i = __base::end(); __n; --__n, ++__i, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002147 __alloc_traits::construct(__a, _STD::addressof(*__i), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002148}
2149
2150// Create front capacity for one block of elements.
2151// Strong guarantee. Either do it or don't touch anything.
2152template <class _Tp, class _Allocator>
2153void
2154deque<_Tp, _Allocator>::__add_front_capacity()
2155{
2156 allocator_type& __a = __base::__alloc();
2157 if (__back_spare() >= __base::__block_size)
2158 {
2159 __base::__start_ += __base::__block_size;
2160 pointer __pt = __base::__map_.back();
2161 __base::__map_.pop_back();
2162 __base::__map_.push_front(__pt);
2163 }
2164 // Else if __base::__map_.size() < __base::__map_.capacity() then we need to allocate 1 buffer
2165 else if (__base::__map_.size() < __base::__map_.capacity())
2166 { // we can put the new buffer into the map, but don't shift things around
2167 // until all buffers are allocated. If we throw, we don't need to fix
2168 // anything up (any added buffers are undetectible)
2169 if (__base::__map_.__front_spare() > 0)
2170 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2171 else
2172 {
2173 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2174 // Done allocating, reorder capacity
2175 pointer __pt = __base::__map_.back();
2176 __base::__map_.pop_back();
2177 __base::__map_.push_front(__pt);
2178 }
2179 __base::__start_ = __base::__map_.size() == 1 ?
2180 __base::__block_size / 2 :
2181 __base::__start_ + __base::__block_size;
2182 }
2183 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2184 else
2185 {
2186 __split_buffer<pointer, typename __base::__pointer_allocator&>
2187 __buf(max<size_type>(2 * __base::__map_.capacity(), 1),
2188 0, __base::__map_.__alloc());
2189#ifndef _LIBCPP_NO_EXCEPTIONS
2190 try
2191 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002192#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002193 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2194#ifndef _LIBCPP_NO_EXCEPTIONS
2195 }
2196 catch (...)
2197 {
2198 __alloc_traits::deallocate(__a, __buf.front(), __base::__block_size);
2199 throw;
2200 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002201#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002202 for (typename __base::__map_pointer __i = __base::__map_.begin();
2203 __i != __base::__map_.end(); ++__i)
2204 __buf.push_back(*__i);
2205 _STD::swap(__base::__map_.__first_, __buf.__first_);
2206 _STD::swap(__base::__map_.__begin_, __buf.__begin_);
2207 _STD::swap(__base::__map_.__end_, __buf.__end_);
2208 _STD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
2209 __base::__start_ = __base::__map_.size() == 1 ?
2210 __base::__block_size / 2 :
2211 __base::__start_ + __base::__block_size;
2212 }
2213}
2214
2215// Create front capacity for __n elements.
2216// Strong guarantee. Either do it or don't touch anything.
2217template <class _Tp, class _Allocator>
2218void
2219deque<_Tp, _Allocator>::__add_front_capacity(size_type __n)
2220{
2221 allocator_type& __a = __base::__alloc();
2222 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2223 // Number of unused blocks at back:
2224 size_type __back_capacity = __back_spare() / __base::__block_size;
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00002225 __back_capacity = _STD::min(__back_capacity, __nb); // don't take more than you need
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002226 __nb -= __back_capacity; // number of blocks need to allocate
2227 // If __nb == 0, then we have sufficient capacity.
2228 if (__nb == 0)
2229 {
2230 __base::__start_ += __base::__block_size * __back_capacity;
2231 for (; __back_capacity > 0; --__back_capacity)
2232 {
2233 pointer __pt = __base::__map_.back();
2234 __base::__map_.pop_back();
2235 __base::__map_.push_front(__pt);
2236 }
2237 }
2238 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2239 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2240 { // we can put the new buffers into the map, but don't shift things around
2241 // until all buffers are allocated. If we throw, we don't need to fix
2242 // anything up (any added buffers are undetectible)
2243 for (; __nb > 0; --__nb, __base::__start_ += __base::__block_size - (__base::__map_.size() == 1))
2244 {
2245 if (__base::__map_.__front_spare() == 0)
2246 break;
2247 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2248 }
2249 for (; __nb > 0; --__nb, ++__back_capacity)
2250 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2251 // Done allocating, reorder capacity
2252 __base::__start_ += __back_capacity * __base::__block_size;
2253 for (; __back_capacity > 0; --__back_capacity)
2254 {
2255 pointer __pt = __base::__map_.back();
2256 __base::__map_.pop_back();
2257 __base::__map_.push_front(__pt);
2258 }
2259 }
2260 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2261 else
2262 {
2263 size_type __ds = (__nb + __back_capacity) * __base::__block_size - __base::__map_.empty();
2264 __split_buffer<pointer, typename __base::__pointer_allocator&>
2265 __buf(max<size_type>(2* __base::__map_.capacity(),
2266 __nb + __base::__map_.size()),
2267 0, __base::__map_.__alloc());
2268#ifndef _LIBCPP_NO_EXCEPTIONS
2269 try
2270 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002271#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002272 for (; __nb > 0; --__nb)
2273 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2274#ifndef _LIBCPP_NO_EXCEPTIONS
2275 }
2276 catch (...)
2277 {
2278 for (typename __base::__map_pointer __i = __buf.begin();
2279 __i != __buf.end(); ++__i)
2280 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2281 throw;
2282 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002283#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002284 for (; __back_capacity > 0; --__back_capacity)
2285 {
2286 __buf.push_back(__base::__map_.back());
2287 __base::__map_.pop_back();
2288 }
2289 for (typename __base::__map_pointer __i = __base::__map_.begin();
2290 __i != __base::__map_.end(); ++__i)
2291 __buf.push_back(*__i);
2292 _STD::swap(__base::__map_.__first_, __buf.__first_);
2293 _STD::swap(__base::__map_.__begin_, __buf.__begin_);
2294 _STD::swap(__base::__map_.__end_, __buf.__end_);
2295 _STD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
2296 __base::__start_ += __ds;
2297 }
2298}
2299
2300// Create back capacity for one block of elements.
2301// Strong guarantee. Either do it or don't touch anything.
2302template <class _Tp, class _Allocator>
2303void
2304deque<_Tp, _Allocator>::__add_back_capacity()
2305{
2306 allocator_type& __a = __base::__alloc();
2307 if (__front_spare() >= __base::__block_size)
2308 {
2309 __base::__start_ -= __base::__block_size;
2310 pointer __pt = __base::__map_.front();
2311 __base::__map_.pop_front();
2312 __base::__map_.push_back(__pt);
2313 }
2314 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2315 else if (__base::__map_.size() < __base::__map_.capacity())
2316 { // we can put the new buffer into the map, but don't shift things around
2317 // until it is allocated. If we throw, we don't need to fix
2318 // anything up (any added buffers are undetectible)
2319 if (__base::__map_.__back_spare() != 0)
2320 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2321 else
2322 {
2323 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2324 // Done allocating, reorder capacity
2325 pointer __pt = __base::__map_.front();
2326 __base::__map_.pop_front();
2327 __base::__map_.push_back(__pt);
2328 }
2329 }
2330 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2331 else
2332 {
2333 __split_buffer<pointer, typename __base::__pointer_allocator&>
2334 __buf(max<size_type>(2* __base::__map_.capacity(), 1),
2335 __base::__map_.size(),
2336 __base::__map_.__alloc());
2337#ifndef _LIBCPP_NO_EXCEPTIONS
2338 try
2339 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002340#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002341 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2342#ifndef _LIBCPP_NO_EXCEPTIONS
2343 }
2344 catch (...)
2345 {
2346 __alloc_traits::deallocate(__a, __buf.back(), __base::__block_size);
2347 throw;
2348 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002349#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002350 for (typename __base::__map_pointer __i = __base::__map_.end();
2351 __i != __base::__map_.begin();)
2352 __buf.push_front(*--__i);
2353 _STD::swap(__base::__map_.__first_, __buf.__first_);
2354 _STD::swap(__base::__map_.__begin_, __buf.__begin_);
2355 _STD::swap(__base::__map_.__end_, __buf.__end_);
2356 _STD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
2357 }
2358}
2359
2360// Create back capacity for __n elements.
2361// Strong guarantee. Either do it or don't touch anything.
2362template <class _Tp, class _Allocator>
2363void
2364deque<_Tp, _Allocator>::__add_back_capacity(size_type __n)
2365{
2366 allocator_type& __a = __base::__alloc();
2367 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2368 // Number of unused blocks at front:
2369 size_type __front_capacity = __front_spare() / __base::__block_size;
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00002370 __front_capacity = _STD::min(__front_capacity, __nb); // don't take more than you need
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002371 __nb -= __front_capacity; // number of blocks need to allocate
2372 // If __nb == 0, then we have sufficient capacity.
2373 if (__nb == 0)
2374 {
2375 __base::__start_ -= __base::__block_size * __front_capacity;
2376 for (; __front_capacity > 0; --__front_capacity)
2377 {
2378 pointer __pt = __base::__map_.front();
2379 __base::__map_.pop_front();
2380 __base::__map_.push_back(__pt);
2381 }
2382 }
2383 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2384 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2385 { // we can put the new buffers into the map, but don't shift things around
2386 // until all buffers are allocated. If we throw, we don't need to fix
2387 // anything up (any added buffers are undetectible)
2388 for (; __nb > 0; --__nb)
2389 {
2390 if (__base::__map_.__back_spare() == 0)
2391 break;
2392 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2393 }
2394 for (; __nb > 0; --__nb, ++__front_capacity, __base::__start_ +=
2395 __base::__block_size - (__base::__map_.size() == 1))
2396 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2397 // Done allocating, reorder capacity
2398 __base::__start_ -= __base::__block_size * __front_capacity;
2399 for (; __front_capacity > 0; --__front_capacity)
2400 {
2401 pointer __pt = __base::__map_.front();
2402 __base::__map_.pop_front();
2403 __base::__map_.push_back(__pt);
2404 }
2405 }
2406 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2407 else
2408 {
2409 size_type __ds = __front_capacity * __base::__block_size;
2410 __split_buffer<pointer, typename __base::__pointer_allocator&>
2411 __buf(max<size_type>(2* __base::__map_.capacity(),
2412 __nb + __base::__map_.size()),
2413 __base::__map_.size() - __front_capacity,
2414 __base::__map_.__alloc());
2415#ifndef _LIBCPP_NO_EXCEPTIONS
2416 try
2417 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002418#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002419 for (; __nb > 0; --__nb)
2420 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2421#ifndef _LIBCPP_NO_EXCEPTIONS
2422 }
2423 catch (...)
2424 {
2425 for (typename __base::__map_pointer __i = __buf.begin();
2426 __i != __buf.end(); ++__i)
2427 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2428 throw;
2429 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002430#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002431 for (; __front_capacity > 0; --__front_capacity)
2432 {
2433 __buf.push_back(__base::__map_.front());
2434 __base::__map_.pop_front();
2435 }
2436 for (typename __base::__map_pointer __i = __base::__map_.end();
2437 __i != __base::__map_.begin();)
2438 __buf.push_front(*--__i);
2439 _STD::swap(__base::__map_.__first_, __buf.__first_);
2440 _STD::swap(__base::__map_.__begin_, __buf.__begin_);
2441 _STD::swap(__base::__map_.__end_, __buf.__end_);
2442 _STD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
2443 __base::__start_ -= __ds;
2444 }
2445}
2446
2447template <class _Tp, class _Allocator>
2448void
2449deque<_Tp, _Allocator>::pop_front()
2450{
2451 allocator_type& __a = __base::__alloc();
2452 __alloc_traits::destroy(__a, *(__base::__map_.begin() +
2453 __base::__start_ / __base::__block_size) +
2454 __base::__start_ % __base::__block_size);
2455 --__base::size();
2456 if (++__base::__start_ >= 2 * __base::__block_size)
2457 {
2458 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2459 __base::__map_.pop_front();
2460 __base::__start_ -= __base::__block_size;
2461 }
2462}
2463
2464template <class _Tp, class _Allocator>
2465void
2466deque<_Tp, _Allocator>::pop_back()
2467{
2468 allocator_type& __a = __base::__alloc();
2469 size_type __p = __base::size() + __base::__start_ - 1;
2470 __alloc_traits::destroy(__a, *(__base::__map_.begin() +
2471 __p / __base::__block_size) +
2472 __p % __base::__block_size);
2473 --__base::size();
2474 if (__back_spare() >= 2 * __base::__block_size)
2475 {
2476 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2477 __base::__map_.pop_back();
2478 }
2479}
2480
2481// move assign [__f, __l) to [__r, __r + (__l-__f)).
2482// If __vt points into [__f, __l), then subtract (__f - __r) from __vt.
2483template <class _Tp, class _Allocator>
2484typename deque<_Tp, _Allocator>::iterator
2485deque<_Tp, _Allocator>::__move_and_check(iterator __f, iterator __l, iterator __r,
2486 const_pointer& __vt)
2487{
2488 // as if
2489 // for (; __f != __l; ++__f, ++__r)
2490 // *__r = _STD::move(*__f);
2491 difference_type __n = __l - __f;
2492 while (__n > 0)
2493 {
2494 pointer __fb = __f.__ptr_;
2495 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2496 difference_type __bs = __fe - __fb;
2497 if (__bs > __n)
2498 {
2499 __bs = __n;
2500 __fe = __fb + __bs;
2501 }
2502 if (__fb <= __vt && __vt < __fe)
2503 __vt = (const_iterator(__f.__m_iter_, __vt) -= __f - __r).__ptr_;
2504 __r = _STD::move(__fb, __fe, __r);
2505 __n -= __bs;
2506 __f += __bs;
2507 }
2508 return __r;
2509}
2510
2511// move assign [__f, __l) to [__r - (__l-__f), __r) backwards.
2512// If __vt points into [__f, __l), then add (__r - __l) to __vt.
2513template <class _Tp, class _Allocator>
2514typename deque<_Tp, _Allocator>::iterator
2515deque<_Tp, _Allocator>::__move_backward_and_check(iterator __f, iterator __l, iterator __r,
2516 const_pointer& __vt)
2517{
2518 // as if
2519 // while (__f != __l)
2520 // *--__r = _STD::move(*--__l);
2521 difference_type __n = __l - __f;
2522 while (__n > 0)
2523 {
2524 --__l;
2525 pointer __lb = *__l.__m_iter_;
2526 pointer __le = __l.__ptr_ + 1;
2527 difference_type __bs = __le - __lb;
2528 if (__bs > __n)
2529 {
2530 __bs = __n;
2531 __lb = __le - __bs;
2532 }
2533 if (__lb <= __vt && __vt < __le)
2534 __vt = (const_iterator(__l.__m_iter_, __vt) += __r - __l - 1).__ptr_;
2535 __r = _STD::move_backward(__lb, __le, __r);
2536 __n -= __bs;
2537 __l -= __bs - 1;
2538 }
2539 return __r;
2540}
2541
2542// move construct [__f, __l) to [__r, __r + (__l-__f)).
2543// If __vt points into [__f, __l), then add (__r - __f) to __vt.
2544template <class _Tp, class _Allocator>
2545void
2546deque<_Tp, _Allocator>::__move_construct_and_check(iterator __f, iterator __l,
2547 iterator __r, const_pointer& __vt)
2548{
2549 allocator_type& __a = __base::__alloc();
2550 // as if
2551 // for (; __f != __l; ++__r, ++__f, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002552 // __alloc_traits::construct(__a, _STD::addressof(*__r), _STD::move(*__f));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002553 difference_type __n = __l - __f;
2554 while (__n > 0)
2555 {
2556 pointer __fb = __f.__ptr_;
2557 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2558 difference_type __bs = __fe - __fb;
2559 if (__bs > __n)
2560 {
2561 __bs = __n;
2562 __fe = __fb + __bs;
2563 }
2564 if (__fb <= __vt && __vt < __fe)
2565 __vt = (const_iterator(__f.__m_iter_, __vt) += __r - __f).__ptr_;
2566 for (; __fb != __fe; ++__fb, ++__r, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002567 __alloc_traits::construct(__a, _STD::addressof(*__r), _STD::move(*__fb));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002568 __n -= __bs;
2569 __f += __bs;
2570 }
2571}
2572
2573// move construct [__f, __l) to [__r - (__l-__f), __r) backwards.
2574// If __vt points into [__f, __l), then subtract (__l - __r) from __vt.
2575template <class _Tp, class _Allocator>
2576void
2577deque<_Tp, _Allocator>::__move_construct_backward_and_check(iterator __f, iterator __l,
2578 iterator __r, const_pointer& __vt)
2579{
2580 allocator_type& __a = __base::__alloc();
2581 // as if
2582 // for (iterator __j = __l; __j != __f;)
2583 // {
Howard Hinnant2529d022011-02-02 17:36:20 +00002584 // __alloc_traitsconstruct(__a, _STD::addressof(*--__r), _STD::move(*--__j));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002585 // --__base::__start_;
2586 // ++__base::size();
2587 // }
2588 difference_type __n = __l - __f;
2589 while (__n > 0)
2590 {
2591 --__l;
2592 pointer __lb = *__l.__m_iter_;
2593 pointer __le = __l.__ptr_ + 1;
2594 difference_type __bs = __le - __lb;
2595 if (__bs > __n)
2596 {
2597 __bs = __n;
2598 __lb = __le - __bs;
2599 }
2600 if (__lb <= __vt && __vt < __le)
2601 __vt = (const_iterator(__l.__m_iter_, __vt) -= __l - __r + 1).__ptr_;
2602 while (__le != __lb)
2603 {
Howard Hinnant2529d022011-02-02 17:36:20 +00002604 __alloc_traits::construct(__a, _STD::addressof(*--__r), _STD::move(*--__le));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002605 --__base::__start_;
2606 ++__base::size();
2607 }
2608 __n -= __bs;
2609 __l -= __bs - 1;
2610 }
2611}
2612
2613template <class _Tp, class _Allocator>
2614typename deque<_Tp, _Allocator>::iterator
2615deque<_Tp, _Allocator>::erase(const_iterator __f)
2616{
2617 difference_type __n = 1;
2618 iterator __b = __base::begin();
2619 difference_type __pos = __f - __b;
2620 iterator __p = __b + __pos;
2621 allocator_type& __a = __base::__alloc();
2622 if (__pos < (__base::size() - 1) / 2)
2623 { // erase from front
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00002624 _STD::move_backward(__b, __p, _STD::next(__p));
Howard Hinnant2529d022011-02-02 17:36:20 +00002625 __alloc_traits::destroy(__a, _STD::addressof(*__b));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002626 --__base::size();
2627 ++__base::__start_;
2628 if (__front_spare() >= 2 * __base::__block_size)
2629 {
2630 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2631 __base::__map_.pop_front();
2632 __base::__start_ -= __base::__block_size;
2633 }
2634 }
2635 else
2636 { // erase from back
Douglas Gregor7ac6af72011-04-29 16:20:26 +00002637 iterator __i = _STD::move(_STD::next(__p), __base::end(), __p);
Howard Hinnant2529d022011-02-02 17:36:20 +00002638 __alloc_traits::destroy(__a, _STD::addressof(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002639 --__base::size();
2640 if (__back_spare() >= 2 * __base::__block_size)
2641 {
2642 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2643 __base::__map_.pop_back();
2644 }
2645 }
2646 return __base::begin() + __pos;
2647}
2648
2649template <class _Tp, class _Allocator>
2650typename deque<_Tp, _Allocator>::iterator
2651deque<_Tp, _Allocator>::erase(const_iterator __f, const_iterator __l)
2652{
2653 difference_type __n = __l - __f;
2654 iterator __b = __base::begin();
2655 difference_type __pos = __f - __b;
2656 iterator __p = __b + __pos;
2657 if (__n > 0)
2658 {
2659 allocator_type& __a = __base::__alloc();
2660 if (__pos < (__base::size() - __n) / 2)
2661 { // erase from front
2662 iterator __i = _STD::move_backward(__b, __p, __p + __n);
2663 for (; __b != __i; ++__b)
Howard Hinnant2529d022011-02-02 17:36:20 +00002664 __alloc_traits::destroy(__a, _STD::addressof(*__b));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002665 __base::size() -= __n;
2666 __base::__start_ += __n;
2667 while (__front_spare() >= 2 * __base::__block_size)
2668 {
2669 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2670 __base::__map_.pop_front();
2671 __base::__start_ -= __base::__block_size;
2672 }
2673 }
2674 else
2675 { // erase from back
2676 iterator __i = _STD::move(__p + __n, __base::end(), __p);
2677 for (iterator __e = __base::end(); __i != __e; ++__i)
Howard Hinnant2529d022011-02-02 17:36:20 +00002678 __alloc_traits::destroy(__a, _STD::addressof(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002679 __base::size() -= __n;
2680 while (__back_spare() >= 2 * __base::__block_size)
2681 {
2682 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2683 __base::__map_.pop_back();
2684 }
2685 }
2686 }
2687 return __base::begin() + __pos;
2688}
2689
2690template <class _Tp, class _Allocator>
2691void
2692deque<_Tp, _Allocator>::__erase_to_end(const_iterator __f)
2693{
2694 iterator __e = __base::end();
2695 difference_type __n = __e - __f;
2696 if (__n > 0)
2697 {
2698 allocator_type& __a = __base::__alloc();
2699 iterator __b = __base::begin();
2700 difference_type __pos = __f - __b;
2701 for (iterator __p = __b + __pos; __p != __e; ++__p)
Howard Hinnant2529d022011-02-02 17:36:20 +00002702 __alloc_traits::destroy(__a, _STD::addressof(*__p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002703 __base::size() -= __n;
2704 while (__back_spare() >= 2 * __base::__block_size)
2705 {
2706 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2707 __base::__map_.pop_back();
2708 }
2709 }
2710}
2711
2712template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00002713inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002714void
2715deque<_Tp, _Allocator>::swap(deque& __c)
2716{
2717 __base::swap(__c);
2718}
2719
2720template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00002721inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002722void
Howard Hinnanta12beb32011-06-02 16:10:22 +00002723deque<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002724{
2725 __base::clear();
2726}
2727
2728template <class _Tp, class _Allocator>
2729_LIBCPP_INLINE_VISIBILITY inline
2730bool
2731operator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2732{
2733 const typename deque<_Tp, _Allocator>::size_type __sz = __x.size();
2734 return __sz == __y.size() && _STD::equal(__x.begin(), __x.end(), __y.begin());
2735}
2736
2737template <class _Tp, class _Allocator>
2738_LIBCPP_INLINE_VISIBILITY inline
2739bool
2740operator!=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2741{
2742 return !(__x == __y);
2743}
2744
2745template <class _Tp, class _Allocator>
2746_LIBCPP_INLINE_VISIBILITY inline
2747bool
2748operator< (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2749{
2750 return _STD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
2751}
2752
2753template <class _Tp, class _Allocator>
2754_LIBCPP_INLINE_VISIBILITY inline
2755bool
2756operator> (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2757{
2758 return __y < __x;
2759}
2760
2761template <class _Tp, class _Allocator>
2762_LIBCPP_INLINE_VISIBILITY inline
2763bool
2764operator>=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2765{
2766 return !(__x < __y);
2767}
2768
2769template <class _Tp, class _Allocator>
2770_LIBCPP_INLINE_VISIBILITY inline
2771bool
2772operator<=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2773{
2774 return !(__y < __x);
2775}
2776
2777template <class _Tp, class _Allocator>
2778_LIBCPP_INLINE_VISIBILITY inline
2779void
2780swap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
2781{
2782 __x.swap(__y);
2783}
2784
2785_LIBCPP_END_NAMESPACE_STD
2786
2787#endif // _LIBCPP_DEQUE