blob: 71fa7f845e1d25256f246604e26c8d53c501f403 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- deque -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_DEQUE
12#define _LIBCPP_DEQUE
13
14/*
15 deque synopsis
16
17namespace std
18{
19
20template <class T, class Allocator = allocator<T> >
21class deque
22{
23public:
24 // types:
25 typedef T value_type;
26 typedef Allocator allocator_type;
27
28 typedef typename allocator_type::reference reference;
29 typedef typename allocator_type::const_reference const_reference;
30 typedef implementation-defined iterator;
31 typedef implementation-defined const_iterator;
32 typedef typename allocator_type::size_type size_type;
33 typedef typename allocator_type::difference_type difference_type;
34
35 typedef typename allocator_type::pointer pointer;
36 typedef typename allocator_type::const_pointer const_pointer;
37 typedef std::reverse_iterator<iterator> reverse_iterator;
38 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
39
40 // construct/copy/destroy:
Howard Hinnant009b2c42011-06-03 15:16:49 +000041 deque() noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000042 explicit deque(const allocator_type& a);
43 explicit deque(size_type n);
44 deque(size_type n, const value_type& v);
45 deque(size_type n, const value_type& v, const allocator_type& a);
46 template <class InputIterator>
47 deque(InputIterator f, InputIterator l);
48 template <class InputIterator>
49 deque(InputIterator f, InputIterator l, const allocator_type& a);
50 deque(const deque& c);
Howard Hinnant18884f42011-06-02 21:38:57 +000051 deque(deque&& c)
52 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000053 deque(initializer_list<value_type> il, const Allocator& a = allocator_type());
54 deque(const deque& c, const allocator_type& a);
55 deque(deque&& c, const allocator_type& a);
56 ~deque();
57
58 deque& operator=(const deque& c);
Howard Hinnant18884f42011-06-02 21:38:57 +000059 deque& operator=(deque&& c)
60 noexcept(
61 allocator_type::propagate_on_container_move_assignment::value &&
62 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000063 deque& operator=(initializer_list<value_type> il);
64
65 template <class InputIterator>
66 void assign(InputIterator f, InputIterator l);
67 void assign(size_type n, const value_type& v);
68 void assign(initializer_list<value_type> il);
69
Howard Hinnanta12beb32011-06-02 16:10:22 +000070 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000071
72 // iterators:
73
Howard Hinnanta12beb32011-06-02 16:10:22 +000074 iterator begin() noexcept;
75 const_iterator begin() const noexcept;
76 iterator end() noexcept;
77 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000078
Howard Hinnanta12beb32011-06-02 16:10:22 +000079 reverse_iterator rbegin() noexcept;
80 const_reverse_iterator rbegin() const noexcept;
81 reverse_iterator rend() noexcept;
82 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000083
Howard Hinnanta12beb32011-06-02 16:10:22 +000084 const_iterator cbegin() const noexcept;
85 const_iterator cend() const noexcept;
86 const_reverse_iterator crbegin() const noexcept;
87 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000088
89 // capacity:
Howard Hinnanta12beb32011-06-02 16:10:22 +000090 size_type size() const noexcept;
91 size_type max_size() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000092 void resize(size_type n);
93 void resize(size_type n, const value_type& v);
94 void shrink_to_fit();
Howard Hinnanta12beb32011-06-02 16:10:22 +000095 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000096
97 // element access:
98 reference operator[](size_type i);
99 const_reference operator[](size_type i) const;
100 reference at(size_type i);
101 const_reference at(size_type i) const;
102 reference front();
103 const_reference front() const;
104 reference back();
105 const_reference back() const;
106
107 // modifiers:
108 void push_front(const value_type& v);
109 void push_front(value_type&& v);
110 void push_back(const value_type& v);
111 void push_back(value_type&& v);
112 template <class... Args> void emplace_front(Args&&... args);
113 template <class... Args> void emplace_back(Args&&... args);
114 template <class... Args> iterator emplace(const_iterator p, Args&&... args);
115 iterator insert(const_iterator p, const value_type& v);
116 iterator insert(const_iterator p, value_type&& v);
117 iterator insert(const_iterator p, size_type n, const value_type& v);
118 template <class InputIterator>
119 iterator insert (const_iterator p, InputIterator f, InputIterator l);
120 iterator insert(const_iterator p, initializer_list<value_type> il);
121 void pop_front();
122 void pop_back();
123 iterator erase(const_iterator p);
124 iterator erase(const_iterator f, const_iterator l);
Howard Hinnant18884f42011-06-02 21:38:57 +0000125 void swap(deque& c)
126 noexcept(!allocator_type::propagate_on_container_swap::value ||
127 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnanta12beb32011-06-02 16:10:22 +0000128 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000129};
130
131template <class T, class Allocator>
132 bool operator==(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
133template <class T, class Allocator>
134 bool operator< (const deque<T,Allocator>& x, const deque<T,Allocator>& y);
135template <class T, class Allocator>
136 bool operator!=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
137template <class T, class Allocator>
138 bool operator> (const deque<T,Allocator>& x, const deque<T,Allocator>& y);
139template <class T, class Allocator>
140 bool operator>=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
141template <class T, class Allocator>
142 bool operator<=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
143
144// specialized algorithms:
145template <class T, class Allocator>
Howard Hinnant18884f42011-06-02 21:38:57 +0000146 void swap(deque<T,Allocator>& x, deque<T,Allocator>& y) noexcept(x.swap(y));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000147
148} // std
149
150*/
151
152#pragma GCC system_header
153
154#include <__config>
155#include <__split_buffer>
156#include <type_traits>
157#include <initializer_list>
158#include <iterator>
159#include <algorithm>
160#include <stdexcept>
161
162_LIBCPP_BEGIN_NAMESPACE_STD
163
164template <class _Tp, class _Allocator> class __deque_base;
165
166template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
167 class _DiffType, _DiffType _BlockSize>
Howard Hinnant422a53f2010-09-21 21:28:23 +0000168class _LIBCPP_VISIBLE __deque_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000169
170template <class _RAIter,
171 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
172__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
173copy(_RAIter __f,
174 _RAIter __l,
175 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
176 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
177
178template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
179 class _OutputIterator>
180_OutputIterator
181copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
182 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
183 _OutputIterator __r);
184
185template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
186 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
187__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
188copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
189 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
190 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
191
192template <class _RAIter,
193 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
194__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
195copy_backward(_RAIter __f,
196 _RAIter __l,
197 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
198 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
199
200template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
201 class _OutputIterator>
202_OutputIterator
203copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
204 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
205 _OutputIterator __r);
206
207template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
208 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
209__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
210copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
211 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
212 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
213
214template <class _RAIter,
215 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
216__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
217move(_RAIter __f,
218 _RAIter __l,
219 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
220 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
221
222template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
223 class _OutputIterator>
224_OutputIterator
225move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
226 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
227 _OutputIterator __r);
228
229template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
230 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
231__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
232move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
233 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
234 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
235
236template <class _RAIter,
237 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
238__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
239move_backward(_RAIter __f,
240 _RAIter __l,
241 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
242 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
243
244template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
245 class _OutputIterator>
246_OutputIterator
247move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
248 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
249 _OutputIterator __r);
250
251template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
252 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
253__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
254move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
255 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
256 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
257
258template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
259 class _DiffType, _DiffType _BlockSize>
Howard Hinnant422a53f2010-09-21 21:28:23 +0000260class _LIBCPP_VISIBLE __deque_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000261{
262 typedef _MapPointer __map_iterator;
263public:
264 typedef _Pointer pointer;
265 typedef _DiffType difference_type;
266private:
267 __map_iterator __m_iter_;
268 pointer __ptr_;
269
270 static const difference_type __block_size = _BlockSize;
271public:
272 typedef _ValueType value_type;
273 typedef random_access_iterator_tag iterator_category;
274 typedef _Reference reference;
275
Howard Hinnanta12beb32011-06-02 16:10:22 +0000276 _LIBCPP_INLINE_VISIBILITY __deque_iterator() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000277
278 template <class _P, class _R, class _MP>
279 _LIBCPP_INLINE_VISIBILITY
280 __deque_iterator(const __deque_iterator<value_type, _P, _R, _MP, difference_type, __block_size>& __it,
Howard Hinnanta12beb32011-06-02 16:10:22 +0000281 typename enable_if<is_convertible<_P, pointer>::value>::type* = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000282 : __m_iter_(__it.__m_iter_), __ptr_(__it.__ptr_) {}
283
284 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return *__ptr_;}
285 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return __ptr_;}
286
287 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator++()
288 {
289 if (++__ptr_ - *__m_iter_ == __block_size)
290 {
291 ++__m_iter_;
292 __ptr_ = *__m_iter_;
293 }
294 return *this;
295 }
296
297 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator++(int)
298 {
299 __deque_iterator __tmp = *this;
300 ++(*this);
301 return __tmp;
302 }
303
304 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator--()
305 {
306 if (__ptr_ == *__m_iter_)
307 {
308 --__m_iter_;
309 __ptr_ = *__m_iter_ + __block_size;
310 }
311 --__ptr_;
312 return *this;
313 }
314
315 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator--(int)
316 {
317 __deque_iterator __tmp = *this;
318 --(*this);
319 return __tmp;
320 }
321
322 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator+=(difference_type __n)
323 {
324 if (__n != 0)
325 {
326 __n += __ptr_ - *__m_iter_;
327 if (__n > 0)
328 {
329 __m_iter_ += __n / __block_size;
330 __ptr_ = *__m_iter_ + __n % __block_size;
331 }
332 else // (__n < 0)
333 {
334 difference_type __z = __block_size - 1 - __n;
335 __m_iter_ -= __z / __block_size;
336 __ptr_ = *__m_iter_ + (__block_size - 1 - __z % __block_size);
337 }
338 }
339 return *this;
340 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000341
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000342 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator-=(difference_type __n)
343 {
344 return *this += -__n;
345 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000346
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000347 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator+(difference_type __n) const
348 {
349 __deque_iterator __t(*this);
350 __t += __n;
351 return __t;
352 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000353
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000354 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator-(difference_type __n) const
355 {
356 __deque_iterator __t(*this);
357 __t -= __n;
358 return __t;
359 }
360
361 _LIBCPP_INLINE_VISIBILITY
362 friend __deque_iterator operator+(difference_type __n, const __deque_iterator& __it)
363 {return __it + __n;}
364
365 _LIBCPP_INLINE_VISIBILITY
366 friend difference_type operator-(const __deque_iterator& __x, const __deque_iterator& __y)
367 {
368 if (__x != __y)
369 return (__x.__m_iter_ - __y.__m_iter_) * __block_size
370 + (__x.__ptr_ - *__x.__m_iter_)
371 - (__y.__ptr_ - *__y.__m_iter_);
372 return 0;
373 }
374
375 _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const
376 {return *(*this + __n);}
377
378 _LIBCPP_INLINE_VISIBILITY friend
379 bool operator==(const __deque_iterator& __x, const __deque_iterator& __y)
380 {return __x.__ptr_ == __y.__ptr_;}
381
382 _LIBCPP_INLINE_VISIBILITY friend
383 bool operator!=(const __deque_iterator& __x, const __deque_iterator& __y)
384 {return !(__x == __y);}
385
386 _LIBCPP_INLINE_VISIBILITY friend
387 bool operator<(const __deque_iterator& __x, const __deque_iterator& __y)
388 {return __x.__m_iter_ < __y.__m_iter_ ||
389 (__x.__m_iter_ == __y.__m_iter_ && __x.__ptr_ < __y.__ptr_);}
390
391 _LIBCPP_INLINE_VISIBILITY friend
392 bool operator>(const __deque_iterator& __x, const __deque_iterator& __y)
393 {return __y < __x;}
394
395 _LIBCPP_INLINE_VISIBILITY friend
396 bool operator<=(const __deque_iterator& __x, const __deque_iterator& __y)
397 {return !(__y < __x);}
398
399 _LIBCPP_INLINE_VISIBILITY friend
400 bool operator>=(const __deque_iterator& __x, const __deque_iterator& __y)
401 {return !(__x < __y);}
402
403private:
Howard Hinnanta12beb32011-06-02 16:10:22 +0000404 _LIBCPP_INLINE_VISIBILITY __deque_iterator(__map_iterator __m, pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000405 : __m_iter_(__m), __ptr_(__p) {}
406
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000407 template <class _Tp, class _A> friend class __deque_base;
Howard Hinnant422a53f2010-09-21 21:28:23 +0000408 template <class _Tp, class _A> friend class _LIBCPP_VISIBLE deque;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000409 template <class _V, class _P, class _R, class _MP, class _D, _D>
Howard Hinnant422a53f2010-09-21 21:28:23 +0000410 friend class _LIBCPP_VISIBLE __deque_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000411
412 template <class _RAIter,
413 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
414 friend
415 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
416 copy(_RAIter __f,
417 _RAIter __l,
418 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
419 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
420
421 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
422 class _OutputIterator>
423 friend
424 _OutputIterator
425 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
426 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
427 _OutputIterator __r);
428
429 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
430 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
431 friend
432 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
433 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
434 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
435 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
436
437 template <class _RAIter,
438 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
439 friend
440 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
441 copy_backward(_RAIter __f,
442 _RAIter __l,
443 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
444 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
445
446 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
447 class _OutputIterator>
448 friend
449 _OutputIterator
450 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
451 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
452 _OutputIterator __r);
453
454 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
455 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
456 friend
457 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
458 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
459 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
460 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
461
462 template <class _RAIter,
463 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
464 friend
465 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
466 move(_RAIter __f,
467 _RAIter __l,
468 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
469 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
470
471 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
472 class _OutputIterator>
473 friend
474 _OutputIterator
475 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
476 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
477 _OutputIterator __r);
478
479 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
480 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
481 friend
482 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
483 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
484 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
485 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
486
487 template <class _RAIter,
488 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
489 friend
490 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
491 move_backward(_RAIter __f,
492 _RAIter __l,
493 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
494 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
495
496 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
497 class _OutputIterator>
498 friend
499 _OutputIterator
500 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
501 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
502 _OutputIterator __r);
503
504 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
505 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
506 friend
507 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
508 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
509 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
510 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
511};
512
513// copy
514
515template <class _RAIter,
516 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
517__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
518copy(_RAIter __f,
519 _RAIter __l,
520 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
521 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
522{
523 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
524 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
525 while (__f != __l)
526 {
527 pointer __rb = __r.__ptr_;
528 pointer __re = *__r.__m_iter_ + _B2;
529 difference_type __bs = __re - __rb;
530 difference_type __n = __l - __f;
531 _RAIter __m = __l;
532 if (__n > __bs)
533 {
534 __n = __bs;
535 __m = __f + __n;
536 }
537 _STD::copy(__f, __m, __rb);
538 __f = __m;
539 __r += __n;
540 }
541 return __r;
542}
543
544template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
545 class _OutputIterator>
546_OutputIterator
547copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
548 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
549 _OutputIterator __r)
550{
551 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
552 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
553 difference_type __n = __l - __f;
554 while (__n > 0)
555 {
556 pointer __fb = __f.__ptr_;
557 pointer __fe = *__f.__m_iter_ + _B1;
558 difference_type __bs = __fe - __fb;
559 if (__bs > __n)
560 {
561 __bs = __n;
562 __fe = __fb + __bs;
563 }
564 __r = _STD::copy(__fb, __fe, __r);
565 __n -= __bs;
566 __f += __bs;
567 }
568 return __r;
569}
570
571template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
572 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
573__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
574copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
575 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
576 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
577{
578 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
579 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
580 difference_type __n = __l - __f;
581 while (__n > 0)
582 {
583 pointer __fb = __f.__ptr_;
584 pointer __fe = *__f.__m_iter_ + _B1;
585 difference_type __bs = __fe - __fb;
586 if (__bs > __n)
587 {
588 __bs = __n;
589 __fe = __fb + __bs;
590 }
591 __r = _STD::copy(__fb, __fe, __r);
592 __n -= __bs;
593 __f += __bs;
594 }
595 return __r;
596}
597
598// copy_backward
599
600template <class _RAIter,
601 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
602__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
603copy_backward(_RAIter __f,
604 _RAIter __l,
605 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
606 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
607{
608 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
609 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
610 while (__f != __l)
611 {
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +0000612 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _STD::prev(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000613 pointer __rb = *__rp.__m_iter_;
614 pointer __re = __rp.__ptr_ + 1;
615 difference_type __bs = __re - __rb;
616 difference_type __n = __l - __f;
617 _RAIter __m = __f;
618 if (__n > __bs)
619 {
620 __n = __bs;
621 __m = __l - __n;
622 }
623 _STD::copy_backward(__m, __l, __re);
624 __l = __m;
625 __r -= __n;
626 }
627 return __r;
628}
629
630template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
631 class _OutputIterator>
632_OutputIterator
633copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
634 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
635 _OutputIterator __r)
636{
637 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
638 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
639 difference_type __n = __l - __f;
640 while (__n > 0)
641 {
642 --__l;
643 pointer __lb = *__l.__m_iter_;
644 pointer __le = __l.__ptr_ + 1;
645 difference_type __bs = __le - __lb;
646 if (__bs > __n)
647 {
648 __bs = __n;
649 __lb = __le - __bs;
650 }
651 __r = _STD::copy_backward(__lb, __le, __r);
652 __n -= __bs;
653 __l -= __bs - 1;
654 }
655 return __r;
656}
657
658template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
659 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
660__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
661copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
662 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
663 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
664{
665 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
666 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
667 difference_type __n = __l - __f;
668 while (__n > 0)
669 {
670 --__l;
671 pointer __lb = *__l.__m_iter_;
672 pointer __le = __l.__ptr_ + 1;
673 difference_type __bs = __le - __lb;
674 if (__bs > __n)
675 {
676 __bs = __n;
677 __lb = __le - __bs;
678 }
679 __r = _STD::copy_backward(__lb, __le, __r);
680 __n -= __bs;
681 __l -= __bs - 1;
682 }
683 return __r;
684}
685
686// move
687
688template <class _RAIter,
689 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
690__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
691move(_RAIter __f,
692 _RAIter __l,
693 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
694 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
695{
696 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
697 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
698 while (__f != __l)
699 {
700 pointer __rb = __r.__ptr_;
701 pointer __re = *__r.__m_iter_ + _B2;
702 difference_type __bs = __re - __rb;
703 difference_type __n = __l - __f;
704 _RAIter __m = __l;
705 if (__n > __bs)
706 {
707 __n = __bs;
708 __m = __f + __n;
709 }
710 _STD::move(__f, __m, __rb);
711 __f = __m;
712 __r += __n;
713 }
714 return __r;
715}
716
717template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
718 class _OutputIterator>
719_OutputIterator
720move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
721 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
722 _OutputIterator __r)
723{
724 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
725 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
726 difference_type __n = __l - __f;
727 while (__n > 0)
728 {
729 pointer __fb = __f.__ptr_;
730 pointer __fe = *__f.__m_iter_ + _B1;
731 difference_type __bs = __fe - __fb;
732 if (__bs > __n)
733 {
734 __bs = __n;
735 __fe = __fb + __bs;
736 }
737 __r = _STD::move(__fb, __fe, __r);
738 __n -= __bs;
739 __f += __bs;
740 }
741 return __r;
742}
743
744template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
745 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
746__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
747move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
748 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
749 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
750{
751 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
752 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
753 difference_type __n = __l - __f;
754 while (__n > 0)
755 {
756 pointer __fb = __f.__ptr_;
757 pointer __fe = *__f.__m_iter_ + _B1;
758 difference_type __bs = __fe - __fb;
759 if (__bs > __n)
760 {
761 __bs = __n;
762 __fe = __fb + __bs;
763 }
764 __r = _STD::move(__fb, __fe, __r);
765 __n -= __bs;
766 __f += __bs;
767 }
768 return __r;
769}
770
771// move_backward
772
773template <class _RAIter,
774 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
775__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
776move_backward(_RAIter __f,
777 _RAIter __l,
778 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
779 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
780{
781 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
782 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
783 while (__f != __l)
784 {
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +0000785 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _STD::prev(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000786 pointer __rb = *__rp.__m_iter_;
787 pointer __re = __rp.__ptr_ + 1;
788 difference_type __bs = __re - __rb;
789 difference_type __n = __l - __f;
790 _RAIter __m = __f;
791 if (__n > __bs)
792 {
793 __n = __bs;
794 __m = __l - __n;
795 }
796 _STD::move_backward(__m, __l, __re);
797 __l = __m;
798 __r -= __n;
799 }
800 return __r;
801}
802
803template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
804 class _OutputIterator>
805_OutputIterator
806move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
807 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
808 _OutputIterator __r)
809{
810 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
811 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
812 difference_type __n = __l - __f;
813 while (__n > 0)
814 {
815 --__l;
816 pointer __lb = *__l.__m_iter_;
817 pointer __le = __l.__ptr_ + 1;
818 difference_type __bs = __le - __lb;
819 if (__bs > __n)
820 {
821 __bs = __n;
822 __lb = __le - __bs;
823 }
824 __r = _STD::move_backward(__lb, __le, __r);
825 __n -= __bs;
826 __l -= __bs - 1;
827 }
828 return __r;
829}
830
831template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
832 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
833__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
834move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
835 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
836 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
837{
838 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
839 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
840 difference_type __n = __l - __f;
841 while (__n > 0)
842 {
843 --__l;
844 pointer __lb = *__l.__m_iter_;
845 pointer __le = __l.__ptr_ + 1;
846 difference_type __bs = __le - __lb;
847 if (__bs > __n)
848 {
849 __bs = __n;
850 __lb = __le - __bs;
851 }
852 __r = _STD::move_backward(__lb, __le, __r);
853 __n -= __bs;
854 __l -= __bs - 1;
855 }
856 return __r;
857}
858
859template <bool>
860class __deque_base_common
861{
862protected:
863 void __throw_length_error() const;
864 void __throw_out_of_range() const;
865};
866
867template <bool __b>
868void
869__deque_base_common<__b>::__throw_length_error() const
870{
871#ifndef _LIBCPP_NO_EXCEPTIONS
872 throw length_error("deque");
873#endif
874}
875
876template <bool __b>
877void
878__deque_base_common<__b>::__throw_out_of_range() const
879{
880#ifndef _LIBCPP_NO_EXCEPTIONS
881 throw out_of_range("deque");
882#endif
883}
884
885template <class _Tp, class _Allocator>
886class __deque_base
887 : protected __deque_base_common<true>
888{
889 __deque_base(const __deque_base& __c);
890 __deque_base& operator=(const __deque_base& __c);
891protected:
892 typedef _Tp value_type;
893 typedef _Allocator allocator_type;
894 typedef allocator_traits<allocator_type> __alloc_traits;
895 typedef value_type& reference;
896 typedef const value_type& const_reference;
897 typedef typename __alloc_traits::size_type size_type;
898 typedef typename __alloc_traits::difference_type difference_type;
899 typedef typename __alloc_traits::pointer pointer;
900 typedef typename __alloc_traits::const_pointer const_pointer;
901
902 static const difference_type __block_size = sizeof(value_type) < 256 ? 4096 / sizeof(value_type) : 16;
903
904 typedef typename __alloc_traits::template
905#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
906 rebind_alloc<pointer>
907#else
908 rebind_alloc<pointer>::other
909#endif
910 __pointer_allocator;
911 typedef allocator_traits<__pointer_allocator> __map_traits;
912 typedef typename __map_traits::pointer __map_pointer;
913 typedef typename __map_traits::const_pointer __map_const_pointer;
914 typedef __split_buffer<pointer, __pointer_allocator> __map;
915
916 typedef __deque_iterator<value_type, pointer, reference, __map_pointer,
917 difference_type, __block_size> iterator;
918 typedef __deque_iterator<value_type, const_pointer, const_reference, __map_const_pointer,
919 difference_type, __block_size> const_iterator;
920
921 __map __map_;
922 size_type __start_;
923 __compressed_pair<size_type, allocator_type> __size_;
924
Howard Hinnanta12beb32011-06-02 16:10:22 +0000925 iterator begin() _NOEXCEPT;
926 const_iterator begin() const _NOEXCEPT;
927 iterator end() _NOEXCEPT;
928 const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000929
930 _LIBCPP_INLINE_VISIBILITY size_type& size() {return __size_.first();}
Howard Hinnanta12beb32011-06-02 16:10:22 +0000931 _LIBCPP_INLINE_VISIBILITY
932 const size_type& size() const _NOEXCEPT {return __size_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000933 _LIBCPP_INLINE_VISIBILITY allocator_type& __alloc() {return __size_.second();}
Howard Hinnanta12beb32011-06-02 16:10:22 +0000934 _LIBCPP_INLINE_VISIBILITY
935 const allocator_type& __alloc() const _NOEXCEPT {return __size_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000936
Howard Hinnant009b2c42011-06-03 15:16:49 +0000937 __deque_base()
938 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000939 explicit __deque_base(const allocator_type& __a);
Howard Hinnant0a612b02011-06-02 20:00:14 +0000940public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000941 ~__deque_base();
942
Howard Hinnant73d21a42010-09-04 23:28:19 +0000943#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000944
Howard Hinnant0a612b02011-06-02 20:00:14 +0000945 __deque_base(__deque_base&& __c)
946 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000947 __deque_base(__deque_base&& __c, const allocator_type& __a);
948
Howard Hinnant73d21a42010-09-04 23:28:19 +0000949#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant0a612b02011-06-02 20:00:14 +0000950 void swap(__deque_base& __c)
951 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
952 __is_nothrow_swappable<allocator_type>::value);
953protected:
Howard Hinnanta12beb32011-06-02 16:10:22 +0000954 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000955
956 bool __invariants() const;
957
Howard Hinnant422a53f2010-09-21 21:28:23 +0000958 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000959 void __move_assign(__deque_base& __c)
Howard Hinnant18884f42011-06-02 21:38:57 +0000960 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
961 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000962 {
963 __map_ = _STD::move(__c.__map_);
964 __start_ = __c.__start_;
965 size() = __c.size();
966 __move_assign_alloc(__c);
967 __c.__start_ = __c.size() = 0;
968 }
969
Howard Hinnant422a53f2010-09-21 21:28:23 +0000970 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000971 void __move_assign_alloc(__deque_base& __c)
Howard Hinnant0a612b02011-06-02 20:00:14 +0000972 _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
973 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000974 {__move_assign_alloc(__c, integral_constant<bool,
975 __alloc_traits::propagate_on_container_move_assignment::value>());}
976
977private:
Howard Hinnant422a53f2010-09-21 21:28:23 +0000978 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000979 void __move_assign_alloc(const __deque_base& __c, true_type)
Howard Hinnant0a612b02011-06-02 20:00:14 +0000980 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000981 {
982 __alloc() = _STD::move(__c.__alloc());
983 }
984
Howard Hinnant422a53f2010-09-21 21:28:23 +0000985 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0a612b02011-06-02 20:00:14 +0000986 void __move_assign_alloc(const __deque_base& __c, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000987 {}
988
Howard Hinnant422a53f2010-09-21 21:28:23 +0000989 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000990 static void __swap_alloc(allocator_type& __x, allocator_type& __y)
Howard Hinnant0a612b02011-06-02 20:00:14 +0000991 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
992 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000993 {__swap_alloc(__x, __y, integral_constant<bool,
994 __alloc_traits::propagate_on_container_swap::value>());}
995
Howard Hinnant422a53f2010-09-21 21:28:23 +0000996 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000997 static void __swap_alloc(allocator_type& __x, allocator_type& __y, true_type)
Howard Hinnant0a612b02011-06-02 20:00:14 +0000998 _NOEXCEPT_(__is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000999 {
1000 using _STD::swap;
1001 swap(__x, __y);
1002 }
Howard Hinnant422a53f2010-09-21 21:28:23 +00001003
1004 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001005 static void __swap_alloc(allocator_type& __x, allocator_type& __y, false_type)
Howard Hinnant0a612b02011-06-02 20:00:14 +00001006 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001007 {}
1008};
1009
1010template <class _Tp, class _Allocator>
1011bool
1012__deque_base<_Tp, _Allocator>::__invariants() const
1013{
1014 if (!__map_.__invariants())
1015 return false;
1016 if (__map_.size() >= size_type(-1) / __block_size)
1017 return false;
1018 for (typename __map::const_iterator __i = __map_.begin(), __e = __map_.end();
1019 __i != __e; ++__i)
1020 if (*__i == nullptr)
1021 return false;
1022 if (__map_.size() != 0)
1023 {
1024 if (size() >= __map_.size() * __block_size)
1025 return false;
1026 if (__start_ >= __map_.size() * __block_size - size())
1027 return false;
1028 }
1029 else
1030 {
1031 if (size() != 0)
1032 return false;
1033 if (__start_ != 0)
1034 return false;
1035 }
1036 return true;
1037}
1038
1039template <class _Tp, class _Allocator>
1040typename __deque_base<_Tp, _Allocator>::iterator
Howard Hinnanta12beb32011-06-02 16:10:22 +00001041__deque_base<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001042{
1043 __map_pointer __mp = __map_.begin() + __start_ / __block_size;
1044 return iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
1045}
1046
1047template <class _Tp, class _Allocator>
1048typename __deque_base<_Tp, _Allocator>::const_iterator
Howard Hinnanta12beb32011-06-02 16:10:22 +00001049__deque_base<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001050{
1051 __map_const_pointer __mp = __map_.begin() + __start_ / __block_size;
1052 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
1053}
1054
1055template <class _Tp, class _Allocator>
1056typename __deque_base<_Tp, _Allocator>::iterator
Howard Hinnanta12beb32011-06-02 16:10:22 +00001057__deque_base<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001058{
1059 size_type __p = size() + __start_;
1060 __map_pointer __mp = __map_.begin() + __p / __block_size;
1061 return iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1062}
1063
1064template <class _Tp, class _Allocator>
1065typename __deque_base<_Tp, _Allocator>::const_iterator
Howard Hinnanta12beb32011-06-02 16:10:22 +00001066__deque_base<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001067{
1068 size_type __p = size() + __start_;
1069 __map_const_pointer __mp = __map_.begin() + __p / __block_size;
1070 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1071}
1072
1073template <class _Tp, class _Allocator>
1074inline _LIBCPP_INLINE_VISIBILITY
1075__deque_base<_Tp, _Allocator>::__deque_base()
Howard Hinnant009b2c42011-06-03 15:16:49 +00001076 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001077 : __start_(0), __size_(0) {}
1078
1079template <class _Tp, class _Allocator>
1080inline _LIBCPP_INLINE_VISIBILITY
1081__deque_base<_Tp, _Allocator>::__deque_base(const allocator_type& __a)
1082 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {}
1083
1084template <class _Tp, class _Allocator>
1085__deque_base<_Tp, _Allocator>::~__deque_base()
1086{
1087 clear();
1088 typename __map::iterator __i = __map_.begin();
1089 typename __map::iterator __e = __map_.end();
1090 for (; __i != __e; ++__i)
1091 __alloc_traits::deallocate(__alloc(), *__i, __block_size);
1092}
1093
Howard Hinnant73d21a42010-09-04 23:28:19 +00001094#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001095
1096template <class _Tp, class _Allocator>
1097__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c)
Howard Hinnant0a612b02011-06-02 20:00:14 +00001098 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001099 : __map_(_STD::move(__c.__map_)),
1100 __start_(_STD::move(__c.__start_)),
1101 __size_(_STD::move(__c.__size_))
1102{
1103 __c.__start_ = 0;
1104 __c.size() = 0;
1105}
1106
1107template <class _Tp, class _Allocator>
1108__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c, const allocator_type& __a)
1109 : __map_(_STD::move(__c.__map_), __pointer_allocator(__a)),
1110 __start_(_STD::move(__c.__start_)),
1111 __size_(_STD::move(__c.size()), __a)
1112{
1113 if (__a == __c.__alloc())
1114 {
1115 __c.__start_ = 0;
1116 __c.size() = 0;
1117 }
1118 else
1119 {
1120 __map_.clear();
1121 __start_ = 0;
1122 size() = 0;
1123 }
1124}
1125
Howard Hinnant73d21a42010-09-04 23:28:19 +00001126#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001127
1128template <class _Tp, class _Allocator>
1129void
1130__deque_base<_Tp, _Allocator>::swap(__deque_base& __c)
Howard Hinnant0a612b02011-06-02 20:00:14 +00001131 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
1132 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001133{
1134 __map_.swap(__c.__map_);
1135 _STD::swap(__start_, __c.__start_);
1136 _STD::swap(size(), __c.size());
1137 __swap_alloc(__alloc(), __c.__alloc());
1138}
1139
1140template <class _Tp, class _Allocator>
1141void
Howard Hinnanta12beb32011-06-02 16:10:22 +00001142__deque_base<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001143{
1144 allocator_type& __a = __alloc();
1145 for (iterator __i = begin(), __e = end(); __i != __e; ++__i)
Howard Hinnant2529d022011-02-02 17:36:20 +00001146 __alloc_traits::destroy(__a, _STD::addressof(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001147 size() = 0;
1148 while (__map_.size() > 2)
1149 {
1150 __alloc_traits::deallocate(__a, __map_.front(), __block_size);
1151 __map_.pop_front();
1152 }
1153 switch (__map_.size())
1154 {
1155 case 1:
1156 __start_ = __block_size / 2;
1157 break;
1158 case 2:
1159 __start_ = __block_size;
1160 break;
1161 }
1162}
1163
1164template <class _Tp, class _Allocator = allocator<_Tp> >
Howard Hinnant422a53f2010-09-21 21:28:23 +00001165class _LIBCPP_VISIBLE deque
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001166 : private __deque_base<_Tp, _Allocator>
1167{
1168public:
1169 // types:
Howard Hinnant324bb032010-08-22 00:02:43 +00001170
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001171 typedef _Tp value_type;
1172 typedef _Allocator allocator_type;
1173
1174 typedef __deque_base<value_type, allocator_type> __base;
1175
1176 typedef typename __base::__alloc_traits __alloc_traits;
1177 typedef typename __base::reference reference;
1178 typedef typename __base::const_reference const_reference;
1179 typedef typename __base::iterator iterator;
1180 typedef typename __base::const_iterator const_iterator;
1181 typedef typename __base::size_type size_type;
1182 typedef typename __base::difference_type difference_type;
1183
1184 typedef typename __base::pointer pointer;
1185 typedef typename __base::const_pointer const_pointer;
1186 typedef _STD::reverse_iterator<iterator> reverse_iterator;
1187 typedef _STD::reverse_iterator<const_iterator> const_reverse_iterator;
1188
1189 // construct/copy/destroy:
Howard Hinnant009b2c42011-06-03 15:16:49 +00001190 _LIBCPP_INLINE_VISIBILITY
1191 deque()
1192 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
1193 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001194 _LIBCPP_INLINE_VISIBILITY deque(const allocator_type& __a) : __base(__a) {}
1195 explicit deque(size_type __n);
1196 deque(size_type __n, const value_type& __v);
1197 deque(size_type __n, const value_type& __v, const allocator_type& __a);
1198 template <class _InputIter>
1199 deque(_InputIter __f, _InputIter __l,
1200 typename enable_if<__is_input_iterator<_InputIter>::value>::type* = 0);
1201 template <class _InputIter>
1202 deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1203 typename enable_if<__is_input_iterator<_InputIter>::value>::type* = 0);
1204 deque(const deque& __c);
1205 deque(const deque& __c, const allocator_type& __a);
1206 deque(initializer_list<value_type> __il);
1207 deque(initializer_list<value_type> __il, const allocator_type& __a);
1208
1209 deque& operator=(const deque& __c);
Howard Hinnant422a53f2010-09-21 21:28:23 +00001210 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001211 deque& operator=(initializer_list<value_type> __il) {assign(__il); return *this;}
1212
Howard Hinnant73d21a42010-09-04 23:28:19 +00001213#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant0a612b02011-06-02 20:00:14 +00001214 deque(deque&& __c) _NOEXCEPT_(is_nothrow_move_constructible<__base>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001215 deque(deque&& __c, const allocator_type& __a);
Howard Hinnant0a612b02011-06-02 20:00:14 +00001216 deque& operator=(deque&& __c)
Howard Hinnant18884f42011-06-02 21:38:57 +00001217 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1218 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001219#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001220
1221 template <class _InputIter>
1222 void assign(_InputIter __f, _InputIter __l,
1223 typename enable_if<__is_input_iterator<_InputIter>::value &&
1224 !__is_random_access_iterator<_InputIter>::value>::type* = 0);
1225 template <class _RAIter>
1226 void assign(_RAIter __f, _RAIter __l,
1227 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
1228 void assign(size_type __n, const value_type& __v);
Howard Hinnant422a53f2010-09-21 21:28:23 +00001229 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001230 void assign(initializer_list<value_type> __il) {assign(__il.begin(), __il.end());}
1231
Howard Hinnanta12beb32011-06-02 16:10:22 +00001232 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001233
1234 // iterators:
1235
Howard Hinnant422a53f2010-09-21 21:28:23 +00001236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001237 iterator begin() _NOEXCEPT {return __base::begin();}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001238 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001239 const_iterator begin() const _NOEXCEPT {return __base::begin();}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001241 iterator end() _NOEXCEPT {return __base::end();}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001243 const_iterator end() const _NOEXCEPT {return __base::end();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001244
Howard Hinnant422a53f2010-09-21 21:28:23 +00001245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001246 reverse_iterator rbegin() _NOEXCEPT
1247 {return reverse_iterator(__base::end());}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001248 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001249 const_reverse_iterator rbegin() const _NOEXCEPT
1250 {return const_reverse_iterator(__base::end());}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001252 reverse_iterator rend() _NOEXCEPT
1253 {return reverse_iterator(__base::begin());}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001254 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001255 const_reverse_iterator rend() const _NOEXCEPT
1256 {return const_reverse_iterator(__base::begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001257
Howard Hinnant422a53f2010-09-21 21:28:23 +00001258 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001259 const_iterator cbegin() const _NOEXCEPT
1260 {return __base::begin();}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001261 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001262 const_iterator cend() const _NOEXCEPT
1263 {return __base::end();}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001264 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001265 const_reverse_iterator crbegin() const _NOEXCEPT
1266 {return const_reverse_iterator(__base::end());}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001267 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001268 const_reverse_iterator crend() const _NOEXCEPT
1269 {return const_reverse_iterator(__base::begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001270
1271 // capacity:
Howard Hinnant422a53f2010-09-21 21:28:23 +00001272 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001273 size_type size() const _NOEXCEPT {return __base::size();}
1274 _LIBCPP_INLINE_VISIBILITY
1275 size_type max_size() const _NOEXCEPT
1276 {return __alloc_traits::max_size(__base::__alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001277 void resize(size_type __n);
1278 void resize(size_type __n, const value_type& __v);
Howard Hinnant18884f42011-06-02 21:38:57 +00001279 void shrink_to_fit() _NOEXCEPT;
Howard Hinnanta12beb32011-06-02 16:10:22 +00001280 _LIBCPP_INLINE_VISIBILITY
1281 bool empty() const _NOEXCEPT {return __base::size() == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001282
1283 // element access:
1284 reference operator[](size_type __i);
1285 const_reference operator[](size_type __i) const;
1286 reference at(size_type __i);
1287 const_reference at(size_type __i) const;
1288 reference front();
1289 const_reference front() const;
1290 reference back();
1291 const_reference back() const;
1292
1293 // 23.2.2.3 modifiers:
1294 void push_front(const value_type& __v);
1295 void push_back(const value_type& __v);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001296#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1297#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001298 template <class... _Args> void emplace_front(_Args&&... __args);
1299 template <class... _Args> void emplace_back(_Args&&... __args);
1300 template <class... _Args> iterator emplace(const_iterator __p, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001301#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001302 void push_front(value_type&& __v);
1303 void push_back(value_type&& __v);
1304 iterator insert(const_iterator __p, value_type&& __v);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001305#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001306 iterator insert(const_iterator __p, const value_type& __v);
1307 iterator insert(const_iterator __p, size_type __n, const value_type& __v);
1308 template <class _InputIter>
1309 iterator insert (const_iterator __p, _InputIter __f, _InputIter __l,
1310 typename enable_if<__is_input_iterator<_InputIter>::value
1311 &&!__is_bidirectional_iterator<_InputIter>::value>::type* = 0);
1312 template <class _BiIter>
1313 iterator insert (const_iterator __p, _BiIter __f, _BiIter __l,
1314 typename enable_if<__is_bidirectional_iterator<_BiIter>::value>::type* = 0);
Howard Hinnant422a53f2010-09-21 21:28:23 +00001315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001316 iterator insert(const_iterator __p, initializer_list<value_type> __il)
1317 {return insert(__p, __il.begin(), __il.end());}
1318 void pop_front();
1319 void pop_back();
1320 iterator erase(const_iterator __p);
1321 iterator erase(const_iterator __f, const_iterator __l);
1322
Howard Hinnant0a612b02011-06-02 20:00:14 +00001323 void swap(deque& __c)
1324 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1325 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnanta12beb32011-06-02 16:10:22 +00001326 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001327
Howard Hinnant422a53f2010-09-21 21:28:23 +00001328 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001329 bool __invariants() const {return __base::__invariants();}
1330private:
Howard Hinnant422a53f2010-09-21 21:28:23 +00001331 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001332 static size_type __recommend_blocks(size_type __n)
1333 {
1334 return __n / __base::__block_size + (__n % __base::__block_size != 0);
1335 }
Howard Hinnant422a53f2010-09-21 21:28:23 +00001336 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001337 size_type __capacity() const
1338 {
1339 return __base::__map_.size() == 0 ? 0 : __base::__map_.size() * __base::__block_size - 1;
1340 }
Howard Hinnant422a53f2010-09-21 21:28:23 +00001341 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001342 size_type __front_spare() const
1343 {
1344 return __base::__start_;
1345 }
Howard Hinnant422a53f2010-09-21 21:28:23 +00001346 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001347 size_type __back_spare() const
1348 {
1349 return __capacity() - (__base::__start_ + __base::size());
1350 }
1351
1352 template <class _InpIter>
1353 void __append(_InpIter __f, _InpIter __l,
1354 typename enable_if<__is_input_iterator<_InpIter>::value &&
1355 !__is_forward_iterator<_InpIter>::value>::type* = 0);
1356 template <class _ForIter>
1357 void __append(_ForIter __f, _ForIter __l,
1358 typename enable_if<__is_forward_iterator<_ForIter>::value>::type* = 0);
1359 void __append(size_type __n);
1360 void __append(size_type __n, const value_type& __v);
1361 void __erase_to_end(const_iterator __f);
1362 void __add_front_capacity();
1363 void __add_front_capacity(size_type __n);
1364 void __add_back_capacity();
1365 void __add_back_capacity(size_type __n);
1366 iterator __move_and_check(iterator __f, iterator __l, iterator __r,
1367 const_pointer& __vt);
1368 iterator __move_backward_and_check(iterator __f, iterator __l, iterator __r,
1369 const_pointer& __vt);
1370 void __move_construct_and_check(iterator __f, iterator __l,
1371 iterator __r, const_pointer& __vt);
1372 void __move_construct_backward_and_check(iterator __f, iterator __l,
1373 iterator __r, const_pointer& __vt);
1374
Howard Hinnant422a53f2010-09-21 21:28:23 +00001375 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001376 void __copy_assign_alloc(const deque& __c)
1377 {__copy_assign_alloc(__c, integral_constant<bool,
1378 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1379
Howard Hinnant422a53f2010-09-21 21:28:23 +00001380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001381 void __copy_assign_alloc(const deque& __c, true_type)
1382 {
1383 if (__base::__alloc() != __c.__alloc())
1384 {
1385 clear();
1386 shrink_to_fit();
1387 }
1388 __base::__alloc() = __c.__alloc();
1389 __base::__map_.__alloc() = __c.__map_.__alloc();
1390 }
1391
Howard Hinnant422a53f2010-09-21 21:28:23 +00001392 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001393 void __copy_assign_alloc(const deque& __c, false_type)
1394 {}
1395
Howard Hinnant18884f42011-06-02 21:38:57 +00001396 void __move_assign(deque& __c, true_type)
1397 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001398 void __move_assign(deque& __c, false_type);
1399};
1400
1401template <class _Tp, class _Allocator>
1402deque<_Tp, _Allocator>::deque(size_type __n)
1403{
1404 if (__n > 0)
1405 __append(__n);
1406}
1407
1408template <class _Tp, class _Allocator>
1409deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v)
1410{
1411 if (__n > 0)
1412 __append(__n, __v);
1413}
1414
1415template <class _Tp, class _Allocator>
1416deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v, const allocator_type& __a)
1417 : __base(__a)
1418{
1419 if (__n > 0)
1420 __append(__n, __v);
1421}
1422
1423template <class _Tp, class _Allocator>
1424template <class _InputIter>
1425deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l,
1426 typename enable_if<__is_input_iterator<_InputIter>::value>::type*)
1427{
1428 __append(__f, __l);
1429}
1430
1431template <class _Tp, class _Allocator>
1432template <class _InputIter>
1433deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1434 typename enable_if<__is_input_iterator<_InputIter>::value>::type*)
1435 : __base(__a)
1436{
1437 __append(__f, __l);
1438}
1439
1440template <class _Tp, class _Allocator>
1441deque<_Tp, _Allocator>::deque(const deque& __c)
1442 : __base(__alloc_traits::select_on_container_copy_construction(__c.__alloc()))
1443{
1444 __append(__c.begin(), __c.end());
1445}
1446
1447template <class _Tp, class _Allocator>
1448deque<_Tp, _Allocator>::deque(const deque& __c, const allocator_type& __a)
1449 : __base(__a)
1450{
1451 __append(__c.begin(), __c.end());
1452}
1453
1454template <class _Tp, class _Allocator>
1455deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il)
1456{
1457 __append(__il.begin(), __il.end());
1458}
1459
1460template <class _Tp, class _Allocator>
1461deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il, const allocator_type& __a)
1462 : __base(__a)
1463{
1464 __append(__il.begin(), __il.end());
1465}
1466
1467template <class _Tp, class _Allocator>
1468deque<_Tp, _Allocator>&
1469deque<_Tp, _Allocator>::operator=(const deque& __c)
1470{
1471 if (this != &__c)
1472 {
1473 __copy_assign_alloc(__c);
1474 assign(__c.begin(), __c.end());
1475 }
1476 return *this;
1477}
1478
Howard Hinnant73d21a42010-09-04 23:28:19 +00001479#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001480
1481template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001482inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001483deque<_Tp, _Allocator>::deque(deque&& __c)
Howard Hinnant0a612b02011-06-02 20:00:14 +00001484 _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001485 : __base(_STD::move(__c))
1486{
1487}
1488
1489template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001490inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001491deque<_Tp, _Allocator>::deque(deque&& __c, const allocator_type& __a)
1492 : __base(_STD::move(__c), __a)
1493{
1494 if (__a != __c.__alloc())
1495 {
1496 typedef move_iterator<iterator> _I;
1497 assign(_I(__c.begin()), _I(__c.end()));
1498 }
1499}
1500
1501template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001502inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001503deque<_Tp, _Allocator>&
1504deque<_Tp, _Allocator>::operator=(deque&& __c)
Howard Hinnant18884f42011-06-02 21:38:57 +00001505 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1506 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001507{
1508 __move_assign(__c, integral_constant<bool,
1509 __alloc_traits::propagate_on_container_move_assignment::value>());
1510 return *this;
1511}
1512
1513template <class _Tp, class _Allocator>
1514void
1515deque<_Tp, _Allocator>::__move_assign(deque& __c, false_type)
1516{
1517 if (__base::__alloc() != __c.__alloc())
1518 {
1519 typedef move_iterator<iterator> _I;
1520 assign(_I(__c.begin()), _I(__c.end()));
1521 }
1522 else
1523 __move_assign(__c, true_type());
1524}
1525
1526template <class _Tp, class _Allocator>
1527void
1528deque<_Tp, _Allocator>::__move_assign(deque& __c, true_type)
Howard Hinnant18884f42011-06-02 21:38:57 +00001529 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001530{
1531 clear();
1532 shrink_to_fit();
1533 __base::__move_assign(__c);
1534}
1535
Howard Hinnant73d21a42010-09-04 23:28:19 +00001536#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001537
1538template <class _Tp, class _Allocator>
1539template <class _InputIter>
1540void
1541deque<_Tp, _Allocator>::assign(_InputIter __f, _InputIter __l,
1542 typename enable_if<__is_input_iterator<_InputIter>::value &&
1543 !__is_random_access_iterator<_InputIter>::value>::type*)
1544{
1545 iterator __i = __base::begin();
1546 iterator __e = __base::end();
1547 for (; __f != __l && __i != __e; ++__f, ++__i)
1548 *__i = *__f;
1549 if (__f != __l)
1550 __append(__f, __l);
1551 else
1552 __erase_to_end(__i);
1553}
1554
1555template <class _Tp, class _Allocator>
1556template <class _RAIter>
1557void
1558deque<_Tp, _Allocator>::assign(_RAIter __f, _RAIter __l,
1559 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
1560{
1561 if (static_cast<size_type>(__l - __f) > __base::size())
1562 {
1563 _RAIter __m = __f + __base::size();
1564 _STD::copy(__f, __m, __base::begin());
1565 __append(__m, __l);
1566 }
1567 else
1568 __erase_to_end(_STD::copy(__f, __l, __base::begin()));
1569}
1570
1571template <class _Tp, class _Allocator>
1572void
1573deque<_Tp, _Allocator>::assign(size_type __n, const value_type& __v)
1574{
1575 if (__n > __base::size())
1576 {
1577 _STD::fill_n(__base::begin(), __base::size(), __v);
1578 __n -= __base::size();
1579 __append(__n, __v);
1580 }
1581 else
1582 __erase_to_end(_STD::fill_n(__base::begin(), __n, __v));
1583}
1584
1585template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001586inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001587_Allocator
Howard Hinnanta12beb32011-06-02 16:10:22 +00001588deque<_Tp, _Allocator>::get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001589{
1590 return __base::__alloc();
1591}
1592
1593template <class _Tp, class _Allocator>
1594void
1595deque<_Tp, _Allocator>::resize(size_type __n)
1596{
1597 if (__n > __base::size())
1598 __append(__n - __base::size());
1599 else if (__n < __base::size())
1600 __erase_to_end(__base::begin() + __n);
1601}
1602
1603template <class _Tp, class _Allocator>
1604void
1605deque<_Tp, _Allocator>::resize(size_type __n, const value_type& __v)
1606{
1607 if (__n > __base::size())
1608 __append(__n - __base::size(), __v);
1609 else if (__n < __base::size())
1610 __erase_to_end(__base::begin() + __n);
1611}
1612
1613template <class _Tp, class _Allocator>
1614void
Howard Hinnant18884f42011-06-02 21:38:57 +00001615deque<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001616{
1617 allocator_type& __a = __base::__alloc();
1618 if (empty())
1619 {
1620 while (__base::__map_.size() > 0)
1621 {
1622 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1623 __base::__map_.pop_back();
1624 }
1625 __base::__start_ = 0;
1626 }
1627 else
1628 {
1629 if (__front_spare() >= __base::__block_size)
1630 {
1631 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
1632 __base::__map_.pop_front();
1633 __base::__start_ -= __base::__block_size;
1634 }
1635 if (__back_spare() >= __base::__block_size)
1636 {
1637 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1638 __base::__map_.pop_back();
1639 }
1640 }
1641 __base::__map_.shrink_to_fit();
1642}
1643
1644template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001645inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001646typename deque<_Tp, _Allocator>::reference
1647deque<_Tp, _Allocator>::operator[](size_type __i)
1648{
1649 size_type __p = __base::__start_ + __i;
1650 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1651}
1652
1653template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001654inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001655typename deque<_Tp, _Allocator>::const_reference
1656deque<_Tp, _Allocator>::operator[](size_type __i) const
1657{
1658 size_type __p = __base::__start_ + __i;
1659 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1660}
1661
1662template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001663inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001664typename deque<_Tp, _Allocator>::reference
1665deque<_Tp, _Allocator>::at(size_type __i)
1666{
1667 if (__i >= __base::size())
1668 __base::__throw_out_of_range();
1669 size_type __p = __base::__start_ + __i;
1670 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1671}
1672
1673template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001674inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001675typename deque<_Tp, _Allocator>::const_reference
1676deque<_Tp, _Allocator>::at(size_type __i) const
1677{
1678 if (__i >= __base::size())
1679 __base::__throw_out_of_range();
1680 size_type __p = __base::__start_ + __i;
1681 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1682}
1683
1684template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001685inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001686typename deque<_Tp, _Allocator>::reference
1687deque<_Tp, _Allocator>::front()
1688{
1689 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1690 + __base::__start_ % __base::__block_size);
1691}
1692
1693template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001694inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001695typename deque<_Tp, _Allocator>::const_reference
1696deque<_Tp, _Allocator>::front() const
1697{
1698 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1699 + __base::__start_ % __base::__block_size);
1700}
1701
1702template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001703inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001704typename deque<_Tp, _Allocator>::reference
1705deque<_Tp, _Allocator>::back()
1706{
1707 size_type __p = __base::size() + __base::__start_ - 1;
1708 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1709}
1710
1711template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001712inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001713typename deque<_Tp, _Allocator>::const_reference
1714deque<_Tp, _Allocator>::back() const
1715{
1716 size_type __p = __base::size() + __base::__start_ - 1;
1717 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1718}
1719
1720template <class _Tp, class _Allocator>
1721void
1722deque<_Tp, _Allocator>::push_back(const value_type& __v)
1723{
1724 allocator_type& __a = __base::__alloc();
1725 if (__back_spare() == 0)
1726 __add_back_capacity();
1727 // __back_spare() >= 1
Howard Hinnant2529d022011-02-02 17:36:20 +00001728 __alloc_traits::construct(__a, _STD::addressof(*__base::end()), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001729 ++__base::size();
1730}
1731
Howard Hinnant73d21a42010-09-04 23:28:19 +00001732#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001733
1734template <class _Tp, class _Allocator>
1735void
1736deque<_Tp, _Allocator>::push_back(value_type&& __v)
1737{
1738 allocator_type& __a = __base::__alloc();
1739 if (__back_spare() == 0)
1740 __add_back_capacity();
1741 // __back_spare() >= 1
Howard Hinnant2529d022011-02-02 17:36:20 +00001742 __alloc_traits::construct(__a, _STD::addressof(*__base::end()), _STD::move(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001743 ++__base::size();
1744}
1745
Howard Hinnant73d21a42010-09-04 23:28:19 +00001746#ifndef _LIBCPP_HAS_NO_VARIADICS
1747
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001748template <class _Tp, class _Allocator>
1749template <class... _Args>
1750void
1751deque<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1752{
1753 allocator_type& __a = __base::__alloc();
1754 if (__back_spare() == 0)
1755 __add_back_capacity();
1756 // __back_spare() >= 1
Howard Hinnant2529d022011-02-02 17:36:20 +00001757 __alloc_traits::construct(__a, _STD::addressof(*__base::end()), _STD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001758 ++__base::size();
1759}
1760
Howard Hinnant73d21a42010-09-04 23:28:19 +00001761#endif // _LIBCPP_HAS_NO_VARIADICS
1762#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001763
1764template <class _Tp, class _Allocator>
1765void
1766deque<_Tp, _Allocator>::push_front(const value_type& __v)
1767{
1768 allocator_type& __a = __base::__alloc();
1769 if (__front_spare() == 0)
1770 __add_front_capacity();
1771 // __front_spare() >= 1
Howard Hinnant2529d022011-02-02 17:36:20 +00001772 __alloc_traits::construct(__a, _STD::addressof(*--__base::begin()), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001773 --__base::__start_;
1774 ++__base::size();
1775}
1776
Howard Hinnant73d21a42010-09-04 23:28:19 +00001777#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001778
1779template <class _Tp, class _Allocator>
1780void
1781deque<_Tp, _Allocator>::push_front(value_type&& __v)
1782{
1783 allocator_type& __a = __base::__alloc();
1784 if (__front_spare() == 0)
1785 __add_front_capacity();
1786 // __front_spare() >= 1
Howard Hinnant2529d022011-02-02 17:36:20 +00001787 __alloc_traits::construct(__a, _STD::addressof(*--__base::begin()), _STD::move(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001788 --__base::__start_;
1789 ++__base::size();
1790}
1791
Howard Hinnant73d21a42010-09-04 23:28:19 +00001792#ifndef _LIBCPP_HAS_NO_VARIADICS
1793
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001794template <class _Tp, class _Allocator>
1795template <class... _Args>
1796void
1797deque<_Tp, _Allocator>::emplace_front(_Args&&... __args)
1798{
1799 allocator_type& __a = __base::__alloc();
1800 if (__front_spare() == 0)
1801 __add_front_capacity();
1802 // __front_spare() >= 1
Howard Hinnant2529d022011-02-02 17:36:20 +00001803 __alloc_traits::construct(__a, _STD::addressof(*--__base::begin()), _STD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001804 --__base::__start_;
1805 ++__base::size();
1806}
1807
Howard Hinnant73d21a42010-09-04 23:28:19 +00001808#endif // _LIBCPP_HAS_NO_VARIADICS
1809#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001810
1811template <class _Tp, class _Allocator>
1812typename deque<_Tp, _Allocator>::iterator
1813deque<_Tp, _Allocator>::insert(const_iterator __p, const value_type& __v)
1814{
1815 size_type __pos = __p - __base::begin();
1816 size_type __to_end = __base::size() - __pos;
1817 allocator_type& __a = __base::__alloc();
1818 if (__pos < __to_end)
1819 { // insert by shifting things backward
1820 if (__front_spare() == 0)
1821 __add_front_capacity();
1822 // __front_spare() >= 1
1823 if (__pos == 0)
1824 {
Howard Hinnant2529d022011-02-02 17:36:20 +00001825 __alloc_traits::construct(__a, _STD::addressof(*--__base::begin()), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001826 --__base::__start_;
1827 ++__base::size();
1828 }
1829 else
1830 {
1831 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1832 iterator __b = __base::begin();
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00001833 iterator __bm1 = _STD::prev(__b);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001834 if (__vt == pointer_traits<const_pointer>::pointer_to(*__b))
1835 __vt = pointer_traits<const_pointer>::pointer_to(*__bm1);
Howard Hinnant2529d022011-02-02 17:36:20 +00001836 __alloc_traits::construct(__a, _STD::addressof(*__bm1), _STD::move(*__b));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001837 --__base::__start_;
1838 ++__base::size();
1839 if (__pos > 1)
Douglas Gregor7ac6af72011-04-29 16:20:26 +00001840 __b = __move_and_check(_STD::next(__b), __b + __pos, __b, __vt);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001841 *__b = *__vt;
1842 }
1843 }
1844 else
1845 { // insert by shifting things forward
1846 if (__back_spare() == 0)
1847 __add_back_capacity();
1848 // __back_capacity >= 1
1849 size_type __de = __base::size() - __pos;
1850 if (__de == 0)
1851 {
Howard Hinnant2529d022011-02-02 17:36:20 +00001852 __alloc_traits::construct(__a, _STD::addressof(*__base::end()), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001853 ++__base::size();
1854 }
1855 else
1856 {
1857 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1858 iterator __e = __base::end();
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00001859 iterator __em1 = _STD::prev(__e);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001860 if (__vt == pointer_traits<const_pointer>::pointer_to(*__em1))
1861 __vt = pointer_traits<const_pointer>::pointer_to(*__e);
Howard Hinnant2529d022011-02-02 17:36:20 +00001862 __alloc_traits::construct(__a, _STD::addressof(*__e), _STD::move(*__em1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001863 ++__base::size();
1864 if (__de > 1)
1865 __e = __move_backward_and_check(__e - __de, __em1, __e, __vt);
1866 *--__e = *__vt;
1867 }
1868 }
1869 return __base::begin() + __pos;
1870}
1871
Howard Hinnant73d21a42010-09-04 23:28:19 +00001872#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001873
1874template <class _Tp, class _Allocator>
1875typename deque<_Tp, _Allocator>::iterator
1876deque<_Tp, _Allocator>::insert(const_iterator __p, value_type&& __v)
1877{
1878 size_type __pos = __p - __base::begin();
1879 size_type __to_end = __base::size() - __pos;
1880 allocator_type& __a = __base::__alloc();
1881 if (__pos < __to_end)
1882 { // insert by shifting things backward
1883 if (__front_spare() == 0)
1884 __add_front_capacity();
1885 // __front_spare() >= 1
1886 if (__pos == 0)
1887 {
Howard Hinnant2529d022011-02-02 17:36:20 +00001888 __alloc_traits::construct(__a, _STD::addressof(*--__base::begin()), _STD::move(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001889 --__base::__start_;
1890 ++__base::size();
1891 }
1892 else
1893 {
1894 iterator __b = __base::begin();
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00001895 iterator __bm1 = _STD::prev(__b);
Howard Hinnant2529d022011-02-02 17:36:20 +00001896 __alloc_traits::construct(__a, _STD::addressof(*__bm1), _STD::move(*__b));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001897 --__base::__start_;
1898 ++__base::size();
1899 if (__pos > 1)
Douglas Gregor7ac6af72011-04-29 16:20:26 +00001900 __b = _STD::move(_STD::next(__b), __b + __pos, __b);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001901 *__b = _STD::move(__v);
1902 }
1903 }
1904 else
1905 { // insert by shifting things forward
1906 if (__back_spare() == 0)
1907 __add_back_capacity();
1908 // __back_capacity >= 1
1909 size_type __de = __base::size() - __pos;
1910 if (__de == 0)
1911 {
Howard Hinnant2529d022011-02-02 17:36:20 +00001912 __alloc_traits::construct(__a, _STD::addressof(*__base::end()), _STD::move(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001913 ++__base::size();
1914 }
1915 else
1916 {
1917 iterator __e = __base::end();
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00001918 iterator __em1 = _STD::prev(__e);
Howard Hinnant2529d022011-02-02 17:36:20 +00001919 __alloc_traits::construct(__a, _STD::addressof(*__e), _STD::move(*__em1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001920 ++__base::size();
1921 if (__de > 1)
1922 __e = _STD::move_backward(__e - __de, __em1, __e);
1923 *--__e = _STD::move(__v);
1924 }
1925 }
1926 return __base::begin() + __pos;
1927}
1928
Howard Hinnant73d21a42010-09-04 23:28:19 +00001929#ifndef _LIBCPP_HAS_NO_VARIADICS
1930
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001931template <class _Tp, class _Allocator>
1932template <class... _Args>
1933typename deque<_Tp, _Allocator>::iterator
1934deque<_Tp, _Allocator>::emplace(const_iterator __p, _Args&&... __args)
1935{
1936 size_type __pos = __p - __base::begin();
1937 size_type __to_end = __base::size() - __pos;
1938 allocator_type& __a = __base::__alloc();
1939 if (__pos < __to_end)
1940 { // insert by shifting things backward
1941 if (__front_spare() == 0)
1942 __add_front_capacity();
1943 // __front_spare() >= 1
1944 if (__pos == 0)
1945 {
Howard Hinnant2529d022011-02-02 17:36:20 +00001946 __alloc_traits::construct(__a, _STD::addressof(*--__base::begin()), _STD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001947 --__base::__start_;
1948 ++__base::size();
1949 }
1950 else
1951 {
1952 iterator __b = __base::begin();
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00001953 iterator __bm1 = _STD::prev(__b);
Howard Hinnant2529d022011-02-02 17:36:20 +00001954 __alloc_traits::construct(__a, _STD::addressof(*__bm1), _STD::move(*__b));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001955 --__base::__start_;
1956 ++__base::size();
1957 if (__pos > 1)
Douglas Gregor7ac6af72011-04-29 16:20:26 +00001958 __b = _STD::move(_STD::next(__b), __b + __pos, __b);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001959 *__b = value_type(_STD::forward<_Args>(__args)...);
1960 }
1961 }
1962 else
1963 { // insert by shifting things forward
1964 if (__back_spare() == 0)
1965 __add_back_capacity();
1966 // __back_capacity >= 1
1967 size_type __de = __base::size() - __pos;
1968 if (__de == 0)
1969 {
Howard Hinnant2529d022011-02-02 17:36:20 +00001970 __alloc_traits::construct(__a, _STD::addressof(*__base::end()), _STD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001971 ++__base::size();
1972 }
1973 else
1974 {
1975 iterator __e = __base::end();
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00001976 iterator __em1 = _STD::prev(__e);
Howard Hinnant2529d022011-02-02 17:36:20 +00001977 __alloc_traits::construct(__a, _STD::addressof(*__e), _STD::move(*__em1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001978 ++__base::size();
1979 if (__de > 1)
1980 __e = _STD::move_backward(__e - __de, __em1, __e);
1981 *--__e = value_type(_STD::forward<_Args>(__args)...);
1982 }
1983 }
1984 return __base::begin() + __pos;
1985}
1986
Howard Hinnant73d21a42010-09-04 23:28:19 +00001987#endif // _LIBCPP_HAS_NO_VARIADICS
1988#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001989
1990template <class _Tp, class _Allocator>
1991typename deque<_Tp, _Allocator>::iterator
1992deque<_Tp, _Allocator>::insert(const_iterator __p, size_type __n, const value_type& __v)
1993{
1994 size_type __pos = __p - __base::begin();
1995 size_type __to_end = __base::size() - __pos;
1996 allocator_type& __a = __base::__alloc();
1997 if (__pos < __to_end)
1998 { // insert by shifting things backward
1999 if (__n > __front_spare())
2000 __add_front_capacity(__n - __front_spare());
2001 // __n <= __front_spare()
2002 size_type __old_n = __n;
2003 iterator __old_begin = __base::begin();
2004 iterator __i = __old_begin;
2005 if (__n > __pos)
2006 {
2007 for (size_type __m = __n - __pos; __m; --__m, --__base::__start_, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002008 __alloc_traits::construct(__a, _STD::addressof(*--__i), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002009 __n = __pos;
2010 }
2011 if (__n > 0)
2012 {
2013 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2014 iterator __obn = __old_begin + __n;
2015 __move_construct_backward_and_check(__old_begin, __obn, __i, __vt);
2016 if (__n < __pos)
2017 __old_begin = __move_and_check(__obn, __old_begin + __pos, __old_begin, __vt);
2018 _STD::fill_n(__old_begin, __n, *__vt);
2019 }
2020 }
2021 else
2022 { // insert by shifting things forward
2023 size_type __back_capacity = __back_spare();
2024 if (__n > __back_capacity)
2025 __add_back_capacity(__n - __back_capacity);
2026 // __n <= __back_capacity
2027 size_type __old_n = __n;
2028 iterator __old_end = __base::end();
2029 iterator __i = __old_end;
2030 size_type __de = __base::size() - __pos;
2031 if (__n > __de)
2032 {
2033 for (size_type __m = __n - __de; __m; --__m, ++__i, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002034 __alloc_traits::construct(__a, _STD::addressof(*__i), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002035 __n = __de;
2036 }
2037 if (__n > 0)
2038 {
2039 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2040 iterator __oen = __old_end - __n;
2041 __move_construct_and_check(__oen, __old_end, __i, __vt);
2042 if (__n < __de)
2043 __old_end = __move_backward_and_check(__old_end - __de, __oen, __old_end, __vt);
2044 _STD::fill_n(__old_end - __n, __n, *__vt);
2045 }
2046 }
2047 return __base::begin() + __pos;
2048}
2049
2050template <class _Tp, class _Allocator>
2051template <class _InputIter>
2052typename deque<_Tp, _Allocator>::iterator
2053deque<_Tp, _Allocator>::insert(const_iterator __p, _InputIter __f, _InputIter __l,
2054 typename enable_if<__is_input_iterator<_InputIter>::value
2055 &&!__is_bidirectional_iterator<_InputIter>::value>::type*)
2056{
2057 __split_buffer<value_type, allocator_type&> __buf(__base::__alloc());
2058 __buf.__construct_at_end(__f, __l);
2059 typedef typename __split_buffer<value_type, allocator_type&>::iterator __bi;
2060 return insert(__p, move_iterator<__bi>(__buf.begin()), move_iterator<__bi>(__buf.end()));
2061}
2062
2063template <class _Tp, class _Allocator>
2064template <class _BiIter>
2065typename deque<_Tp, _Allocator>::iterator
2066deque<_Tp, _Allocator>::insert(const_iterator __p, _BiIter __f, _BiIter __l,
2067 typename enable_if<__is_bidirectional_iterator<_BiIter>::value>::type*)
2068{
2069 size_type __n = _STD::distance(__f, __l);
2070 size_type __pos = __p - __base::begin();
2071 size_type __to_end = __base::size() - __pos;
2072 allocator_type& __a = __base::__alloc();
2073 if (__pos < __to_end)
2074 { // insert by shifting things backward
2075 if (__n > __front_spare())
2076 __add_front_capacity(__n - __front_spare());
2077 // __n <= __front_spare()
2078 size_type __old_n = __n;
2079 iterator __old_begin = __base::begin();
2080 iterator __i = __old_begin;
2081 _BiIter __m = __f;
2082 if (__n > __pos)
2083 {
2084 __m = __pos < __n / 2 ? _STD::prev(__l, __pos) : _STD::next(__f, __n - __pos);
2085 for (_BiIter __j = __m; __j != __f; --__base::__start_, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002086 __alloc_traits::construct(__a, _STD::addressof(*--__i), *--__j);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002087 __n = __pos;
2088 }
2089 if (__n > 0)
2090 {
2091 iterator __obn = __old_begin + __n;
2092 for (iterator __j = __obn; __j != __old_begin;)
2093 {
Howard Hinnant2529d022011-02-02 17:36:20 +00002094 __alloc_traits::construct(__a, _STD::addressof(*--__i), _STD::move(*--__j));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002095 --__base::__start_;
2096 ++__base::size();
2097 }
2098 if (__n < __pos)
2099 __old_begin = _STD::move(__obn, __old_begin + __pos, __old_begin);
2100 _STD::copy(__m, __l, __old_begin);
2101 }
2102 }
2103 else
2104 { // insert by shifting things forward
2105 size_type __back_capacity = __back_spare();
2106 if (__n > __back_capacity)
2107 __add_back_capacity(__n - __back_capacity);
2108 // __n <= __back_capacity
2109 size_type __old_n = __n;
2110 iterator __old_end = __base::end();
2111 iterator __i = __old_end;
2112 _BiIter __m = __l;
2113 size_type __de = __base::size() - __pos;
2114 if (__n > __de)
2115 {
2116 __m = __de < __n / 2 ? _STD::next(__f, __de) : _STD::prev(__l, __n - __de);
2117 for (_BiIter __j = __m; __j != __l; ++__i, ++__j, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002118 __alloc_traits::construct(__a, _STD::addressof(*__i), *__j);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002119 __n = __de;
2120 }
2121 if (__n > 0)
2122 {
2123 iterator __oen = __old_end - __n;
2124 for (iterator __j = __oen; __j != __old_end; ++__i, ++__j, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002125 __alloc_traits::construct(__a, _STD::addressof(*__i), _STD::move(*__j));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002126 if (__n < __de)
2127 __old_end = _STD::move_backward(__old_end - __de, __oen, __old_end);
2128 _STD::copy_backward(__f, __m, __old_end);
2129 }
2130 }
2131 return __base::begin() + __pos;
2132}
2133
2134template <class _Tp, class _Allocator>
2135template <class _InpIter>
2136void
2137deque<_Tp, _Allocator>::__append(_InpIter __f, _InpIter __l,
2138 typename enable_if<__is_input_iterator<_InpIter>::value &&
2139 !__is_forward_iterator<_InpIter>::value>::type*)
2140{
2141 for (; __f != __l; ++__f)
2142 push_back(*__f);
2143}
2144
2145template <class _Tp, class _Allocator>
2146template <class _ForIter>
2147void
2148deque<_Tp, _Allocator>::__append(_ForIter __f, _ForIter __l,
2149 typename enable_if<__is_forward_iterator<_ForIter>::value>::type*)
2150{
2151 size_type __n = _STD::distance(__f, __l);
2152 allocator_type& __a = __base::__alloc();
2153 size_type __back_capacity = __back_spare();
2154 if (__n > __back_capacity)
2155 __add_back_capacity(__n - __back_capacity);
2156 // __n <= __back_capacity
2157 for (iterator __i = __base::end(); __f != __l; ++__i, ++__f, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002158 __alloc_traits::construct(__a, _STD::addressof(*__i), *__f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002159}
2160
2161template <class _Tp, class _Allocator>
2162void
2163deque<_Tp, _Allocator>::__append(size_type __n)
2164{
2165 allocator_type& __a = __base::__alloc();
2166 size_type __back_capacity = __back_spare();
2167 if (__n > __back_capacity)
2168 __add_back_capacity(__n - __back_capacity);
2169 // __n <= __back_capacity
2170 for (iterator __i = __base::end(); __n; --__n, ++__i, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002171 __alloc_traits::construct(__a, _STD::addressof(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002172}
2173
2174template <class _Tp, class _Allocator>
2175void
2176deque<_Tp, _Allocator>::__append(size_type __n, const value_type& __v)
2177{
2178 allocator_type& __a = __base::__alloc();
2179 size_type __back_capacity = __back_spare();
2180 if (__n > __back_capacity)
2181 __add_back_capacity(__n - __back_capacity);
2182 // __n <= __back_capacity
2183 for (iterator __i = __base::end(); __n; --__n, ++__i, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002184 __alloc_traits::construct(__a, _STD::addressof(*__i), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002185}
2186
2187// Create front capacity for one block of elements.
2188// Strong guarantee. Either do it or don't touch anything.
2189template <class _Tp, class _Allocator>
2190void
2191deque<_Tp, _Allocator>::__add_front_capacity()
2192{
2193 allocator_type& __a = __base::__alloc();
2194 if (__back_spare() >= __base::__block_size)
2195 {
2196 __base::__start_ += __base::__block_size;
2197 pointer __pt = __base::__map_.back();
2198 __base::__map_.pop_back();
2199 __base::__map_.push_front(__pt);
2200 }
2201 // Else if __base::__map_.size() < __base::__map_.capacity() then we need to allocate 1 buffer
2202 else if (__base::__map_.size() < __base::__map_.capacity())
2203 { // we can put the new buffer into the map, but don't shift things around
2204 // until all buffers are allocated. If we throw, we don't need to fix
2205 // anything up (any added buffers are undetectible)
2206 if (__base::__map_.__front_spare() > 0)
2207 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2208 else
2209 {
2210 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2211 // Done allocating, reorder capacity
2212 pointer __pt = __base::__map_.back();
2213 __base::__map_.pop_back();
2214 __base::__map_.push_front(__pt);
2215 }
2216 __base::__start_ = __base::__map_.size() == 1 ?
2217 __base::__block_size / 2 :
2218 __base::__start_ + __base::__block_size;
2219 }
2220 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2221 else
2222 {
2223 __split_buffer<pointer, typename __base::__pointer_allocator&>
2224 __buf(max<size_type>(2 * __base::__map_.capacity(), 1),
2225 0, __base::__map_.__alloc());
2226#ifndef _LIBCPP_NO_EXCEPTIONS
2227 try
2228 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002229#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002230 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2231#ifndef _LIBCPP_NO_EXCEPTIONS
2232 }
2233 catch (...)
2234 {
2235 __alloc_traits::deallocate(__a, __buf.front(), __base::__block_size);
2236 throw;
2237 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002238#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002239 for (typename __base::__map_pointer __i = __base::__map_.begin();
2240 __i != __base::__map_.end(); ++__i)
2241 __buf.push_back(*__i);
2242 _STD::swap(__base::__map_.__first_, __buf.__first_);
2243 _STD::swap(__base::__map_.__begin_, __buf.__begin_);
2244 _STD::swap(__base::__map_.__end_, __buf.__end_);
2245 _STD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
2246 __base::__start_ = __base::__map_.size() == 1 ?
2247 __base::__block_size / 2 :
2248 __base::__start_ + __base::__block_size;
2249 }
2250}
2251
2252// Create front capacity for __n elements.
2253// Strong guarantee. Either do it or don't touch anything.
2254template <class _Tp, class _Allocator>
2255void
2256deque<_Tp, _Allocator>::__add_front_capacity(size_type __n)
2257{
2258 allocator_type& __a = __base::__alloc();
2259 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2260 // Number of unused blocks at back:
2261 size_type __back_capacity = __back_spare() / __base::__block_size;
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00002262 __back_capacity = _STD::min(__back_capacity, __nb); // don't take more than you need
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002263 __nb -= __back_capacity; // number of blocks need to allocate
2264 // If __nb == 0, then we have sufficient capacity.
2265 if (__nb == 0)
2266 {
2267 __base::__start_ += __base::__block_size * __back_capacity;
2268 for (; __back_capacity > 0; --__back_capacity)
2269 {
2270 pointer __pt = __base::__map_.back();
2271 __base::__map_.pop_back();
2272 __base::__map_.push_front(__pt);
2273 }
2274 }
2275 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2276 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2277 { // we can put the new buffers into the map, but don't shift things around
2278 // until all buffers are allocated. If we throw, we don't need to fix
2279 // anything up (any added buffers are undetectible)
2280 for (; __nb > 0; --__nb, __base::__start_ += __base::__block_size - (__base::__map_.size() == 1))
2281 {
2282 if (__base::__map_.__front_spare() == 0)
2283 break;
2284 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2285 }
2286 for (; __nb > 0; --__nb, ++__back_capacity)
2287 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2288 // Done allocating, reorder capacity
2289 __base::__start_ += __back_capacity * __base::__block_size;
2290 for (; __back_capacity > 0; --__back_capacity)
2291 {
2292 pointer __pt = __base::__map_.back();
2293 __base::__map_.pop_back();
2294 __base::__map_.push_front(__pt);
2295 }
2296 }
2297 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2298 else
2299 {
2300 size_type __ds = (__nb + __back_capacity) * __base::__block_size - __base::__map_.empty();
2301 __split_buffer<pointer, typename __base::__pointer_allocator&>
2302 __buf(max<size_type>(2* __base::__map_.capacity(),
2303 __nb + __base::__map_.size()),
2304 0, __base::__map_.__alloc());
2305#ifndef _LIBCPP_NO_EXCEPTIONS
2306 try
2307 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002308#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002309 for (; __nb > 0; --__nb)
2310 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2311#ifndef _LIBCPP_NO_EXCEPTIONS
2312 }
2313 catch (...)
2314 {
2315 for (typename __base::__map_pointer __i = __buf.begin();
2316 __i != __buf.end(); ++__i)
2317 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2318 throw;
2319 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002320#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002321 for (; __back_capacity > 0; --__back_capacity)
2322 {
2323 __buf.push_back(__base::__map_.back());
2324 __base::__map_.pop_back();
2325 }
2326 for (typename __base::__map_pointer __i = __base::__map_.begin();
2327 __i != __base::__map_.end(); ++__i)
2328 __buf.push_back(*__i);
2329 _STD::swap(__base::__map_.__first_, __buf.__first_);
2330 _STD::swap(__base::__map_.__begin_, __buf.__begin_);
2331 _STD::swap(__base::__map_.__end_, __buf.__end_);
2332 _STD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
2333 __base::__start_ += __ds;
2334 }
2335}
2336
2337// Create back capacity for one block of elements.
2338// Strong guarantee. Either do it or don't touch anything.
2339template <class _Tp, class _Allocator>
2340void
2341deque<_Tp, _Allocator>::__add_back_capacity()
2342{
2343 allocator_type& __a = __base::__alloc();
2344 if (__front_spare() >= __base::__block_size)
2345 {
2346 __base::__start_ -= __base::__block_size;
2347 pointer __pt = __base::__map_.front();
2348 __base::__map_.pop_front();
2349 __base::__map_.push_back(__pt);
2350 }
2351 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2352 else if (__base::__map_.size() < __base::__map_.capacity())
2353 { // we can put the new buffer into the map, but don't shift things around
2354 // until it is allocated. If we throw, we don't need to fix
2355 // anything up (any added buffers are undetectible)
2356 if (__base::__map_.__back_spare() != 0)
2357 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2358 else
2359 {
2360 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2361 // Done allocating, reorder capacity
2362 pointer __pt = __base::__map_.front();
2363 __base::__map_.pop_front();
2364 __base::__map_.push_back(__pt);
2365 }
2366 }
2367 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2368 else
2369 {
2370 __split_buffer<pointer, typename __base::__pointer_allocator&>
2371 __buf(max<size_type>(2* __base::__map_.capacity(), 1),
2372 __base::__map_.size(),
2373 __base::__map_.__alloc());
2374#ifndef _LIBCPP_NO_EXCEPTIONS
2375 try
2376 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002377#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002378 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2379#ifndef _LIBCPP_NO_EXCEPTIONS
2380 }
2381 catch (...)
2382 {
2383 __alloc_traits::deallocate(__a, __buf.back(), __base::__block_size);
2384 throw;
2385 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002386#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002387 for (typename __base::__map_pointer __i = __base::__map_.end();
2388 __i != __base::__map_.begin();)
2389 __buf.push_front(*--__i);
2390 _STD::swap(__base::__map_.__first_, __buf.__first_);
2391 _STD::swap(__base::__map_.__begin_, __buf.__begin_);
2392 _STD::swap(__base::__map_.__end_, __buf.__end_);
2393 _STD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
2394 }
2395}
2396
2397// Create back capacity for __n elements.
2398// Strong guarantee. Either do it or don't touch anything.
2399template <class _Tp, class _Allocator>
2400void
2401deque<_Tp, _Allocator>::__add_back_capacity(size_type __n)
2402{
2403 allocator_type& __a = __base::__alloc();
2404 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2405 // Number of unused blocks at front:
2406 size_type __front_capacity = __front_spare() / __base::__block_size;
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00002407 __front_capacity = _STD::min(__front_capacity, __nb); // don't take more than you need
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002408 __nb -= __front_capacity; // number of blocks need to allocate
2409 // If __nb == 0, then we have sufficient capacity.
2410 if (__nb == 0)
2411 {
2412 __base::__start_ -= __base::__block_size * __front_capacity;
2413 for (; __front_capacity > 0; --__front_capacity)
2414 {
2415 pointer __pt = __base::__map_.front();
2416 __base::__map_.pop_front();
2417 __base::__map_.push_back(__pt);
2418 }
2419 }
2420 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2421 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2422 { // we can put the new buffers into the map, but don't shift things around
2423 // until all buffers are allocated. If we throw, we don't need to fix
2424 // anything up (any added buffers are undetectible)
2425 for (; __nb > 0; --__nb)
2426 {
2427 if (__base::__map_.__back_spare() == 0)
2428 break;
2429 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2430 }
2431 for (; __nb > 0; --__nb, ++__front_capacity, __base::__start_ +=
2432 __base::__block_size - (__base::__map_.size() == 1))
2433 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2434 // Done allocating, reorder capacity
2435 __base::__start_ -= __base::__block_size * __front_capacity;
2436 for (; __front_capacity > 0; --__front_capacity)
2437 {
2438 pointer __pt = __base::__map_.front();
2439 __base::__map_.pop_front();
2440 __base::__map_.push_back(__pt);
2441 }
2442 }
2443 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2444 else
2445 {
2446 size_type __ds = __front_capacity * __base::__block_size;
2447 __split_buffer<pointer, typename __base::__pointer_allocator&>
2448 __buf(max<size_type>(2* __base::__map_.capacity(),
2449 __nb + __base::__map_.size()),
2450 __base::__map_.size() - __front_capacity,
2451 __base::__map_.__alloc());
2452#ifndef _LIBCPP_NO_EXCEPTIONS
2453 try
2454 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002455#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002456 for (; __nb > 0; --__nb)
2457 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2458#ifndef _LIBCPP_NO_EXCEPTIONS
2459 }
2460 catch (...)
2461 {
2462 for (typename __base::__map_pointer __i = __buf.begin();
2463 __i != __buf.end(); ++__i)
2464 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2465 throw;
2466 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002467#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002468 for (; __front_capacity > 0; --__front_capacity)
2469 {
2470 __buf.push_back(__base::__map_.front());
2471 __base::__map_.pop_front();
2472 }
2473 for (typename __base::__map_pointer __i = __base::__map_.end();
2474 __i != __base::__map_.begin();)
2475 __buf.push_front(*--__i);
2476 _STD::swap(__base::__map_.__first_, __buf.__first_);
2477 _STD::swap(__base::__map_.__begin_, __buf.__begin_);
2478 _STD::swap(__base::__map_.__end_, __buf.__end_);
2479 _STD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
2480 __base::__start_ -= __ds;
2481 }
2482}
2483
2484template <class _Tp, class _Allocator>
2485void
2486deque<_Tp, _Allocator>::pop_front()
2487{
2488 allocator_type& __a = __base::__alloc();
2489 __alloc_traits::destroy(__a, *(__base::__map_.begin() +
2490 __base::__start_ / __base::__block_size) +
2491 __base::__start_ % __base::__block_size);
2492 --__base::size();
2493 if (++__base::__start_ >= 2 * __base::__block_size)
2494 {
2495 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2496 __base::__map_.pop_front();
2497 __base::__start_ -= __base::__block_size;
2498 }
2499}
2500
2501template <class _Tp, class _Allocator>
2502void
2503deque<_Tp, _Allocator>::pop_back()
2504{
2505 allocator_type& __a = __base::__alloc();
2506 size_type __p = __base::size() + __base::__start_ - 1;
2507 __alloc_traits::destroy(__a, *(__base::__map_.begin() +
2508 __p / __base::__block_size) +
2509 __p % __base::__block_size);
2510 --__base::size();
2511 if (__back_spare() >= 2 * __base::__block_size)
2512 {
2513 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2514 __base::__map_.pop_back();
2515 }
2516}
2517
2518// move assign [__f, __l) to [__r, __r + (__l-__f)).
2519// If __vt points into [__f, __l), then subtract (__f - __r) from __vt.
2520template <class _Tp, class _Allocator>
2521typename deque<_Tp, _Allocator>::iterator
2522deque<_Tp, _Allocator>::__move_and_check(iterator __f, iterator __l, iterator __r,
2523 const_pointer& __vt)
2524{
2525 // as if
2526 // for (; __f != __l; ++__f, ++__r)
2527 // *__r = _STD::move(*__f);
2528 difference_type __n = __l - __f;
2529 while (__n > 0)
2530 {
2531 pointer __fb = __f.__ptr_;
2532 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2533 difference_type __bs = __fe - __fb;
2534 if (__bs > __n)
2535 {
2536 __bs = __n;
2537 __fe = __fb + __bs;
2538 }
2539 if (__fb <= __vt && __vt < __fe)
2540 __vt = (const_iterator(__f.__m_iter_, __vt) -= __f - __r).__ptr_;
2541 __r = _STD::move(__fb, __fe, __r);
2542 __n -= __bs;
2543 __f += __bs;
2544 }
2545 return __r;
2546}
2547
2548// move assign [__f, __l) to [__r - (__l-__f), __r) backwards.
2549// If __vt points into [__f, __l), then add (__r - __l) to __vt.
2550template <class _Tp, class _Allocator>
2551typename deque<_Tp, _Allocator>::iterator
2552deque<_Tp, _Allocator>::__move_backward_and_check(iterator __f, iterator __l, iterator __r,
2553 const_pointer& __vt)
2554{
2555 // as if
2556 // while (__f != __l)
2557 // *--__r = _STD::move(*--__l);
2558 difference_type __n = __l - __f;
2559 while (__n > 0)
2560 {
2561 --__l;
2562 pointer __lb = *__l.__m_iter_;
2563 pointer __le = __l.__ptr_ + 1;
2564 difference_type __bs = __le - __lb;
2565 if (__bs > __n)
2566 {
2567 __bs = __n;
2568 __lb = __le - __bs;
2569 }
2570 if (__lb <= __vt && __vt < __le)
2571 __vt = (const_iterator(__l.__m_iter_, __vt) += __r - __l - 1).__ptr_;
2572 __r = _STD::move_backward(__lb, __le, __r);
2573 __n -= __bs;
2574 __l -= __bs - 1;
2575 }
2576 return __r;
2577}
2578
2579// move construct [__f, __l) to [__r, __r + (__l-__f)).
2580// If __vt points into [__f, __l), then add (__r - __f) to __vt.
2581template <class _Tp, class _Allocator>
2582void
2583deque<_Tp, _Allocator>::__move_construct_and_check(iterator __f, iterator __l,
2584 iterator __r, const_pointer& __vt)
2585{
2586 allocator_type& __a = __base::__alloc();
2587 // as if
2588 // for (; __f != __l; ++__r, ++__f, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002589 // __alloc_traits::construct(__a, _STD::addressof(*__r), _STD::move(*__f));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002590 difference_type __n = __l - __f;
2591 while (__n > 0)
2592 {
2593 pointer __fb = __f.__ptr_;
2594 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2595 difference_type __bs = __fe - __fb;
2596 if (__bs > __n)
2597 {
2598 __bs = __n;
2599 __fe = __fb + __bs;
2600 }
2601 if (__fb <= __vt && __vt < __fe)
2602 __vt = (const_iterator(__f.__m_iter_, __vt) += __r - __f).__ptr_;
2603 for (; __fb != __fe; ++__fb, ++__r, ++__base::size())
Howard Hinnant2529d022011-02-02 17:36:20 +00002604 __alloc_traits::construct(__a, _STD::addressof(*__r), _STD::move(*__fb));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002605 __n -= __bs;
2606 __f += __bs;
2607 }
2608}
2609
2610// move construct [__f, __l) to [__r - (__l-__f), __r) backwards.
2611// If __vt points into [__f, __l), then subtract (__l - __r) from __vt.
2612template <class _Tp, class _Allocator>
2613void
2614deque<_Tp, _Allocator>::__move_construct_backward_and_check(iterator __f, iterator __l,
2615 iterator __r, const_pointer& __vt)
2616{
2617 allocator_type& __a = __base::__alloc();
2618 // as if
2619 // for (iterator __j = __l; __j != __f;)
2620 // {
Howard Hinnant2529d022011-02-02 17:36:20 +00002621 // __alloc_traitsconstruct(__a, _STD::addressof(*--__r), _STD::move(*--__j));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002622 // --__base::__start_;
2623 // ++__base::size();
2624 // }
2625 difference_type __n = __l - __f;
2626 while (__n > 0)
2627 {
2628 --__l;
2629 pointer __lb = *__l.__m_iter_;
2630 pointer __le = __l.__ptr_ + 1;
2631 difference_type __bs = __le - __lb;
2632 if (__bs > __n)
2633 {
2634 __bs = __n;
2635 __lb = __le - __bs;
2636 }
2637 if (__lb <= __vt && __vt < __le)
2638 __vt = (const_iterator(__l.__m_iter_, __vt) -= __l - __r + 1).__ptr_;
2639 while (__le != __lb)
2640 {
Howard Hinnant2529d022011-02-02 17:36:20 +00002641 __alloc_traits::construct(__a, _STD::addressof(*--__r), _STD::move(*--__le));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002642 --__base::__start_;
2643 ++__base::size();
2644 }
2645 __n -= __bs;
2646 __l -= __bs - 1;
2647 }
2648}
2649
2650template <class _Tp, class _Allocator>
2651typename deque<_Tp, _Allocator>::iterator
2652deque<_Tp, _Allocator>::erase(const_iterator __f)
2653{
2654 difference_type __n = 1;
2655 iterator __b = __base::begin();
2656 difference_type __pos = __f - __b;
2657 iterator __p = __b + __pos;
2658 allocator_type& __a = __base::__alloc();
2659 if (__pos < (__base::size() - 1) / 2)
2660 { // erase from front
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00002661 _STD::move_backward(__b, __p, _STD::next(__p));
Howard Hinnant2529d022011-02-02 17:36:20 +00002662 __alloc_traits::destroy(__a, _STD::addressof(*__b));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002663 --__base::size();
2664 ++__base::__start_;
2665 if (__front_spare() >= 2 * __base::__block_size)
2666 {
2667 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2668 __base::__map_.pop_front();
2669 __base::__start_ -= __base::__block_size;
2670 }
2671 }
2672 else
2673 { // erase from back
Douglas Gregor7ac6af72011-04-29 16:20:26 +00002674 iterator __i = _STD::move(_STD::next(__p), __base::end(), __p);
Howard Hinnant2529d022011-02-02 17:36:20 +00002675 __alloc_traits::destroy(__a, _STD::addressof(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002676 --__base::size();
2677 if (__back_spare() >= 2 * __base::__block_size)
2678 {
2679 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2680 __base::__map_.pop_back();
2681 }
2682 }
2683 return __base::begin() + __pos;
2684}
2685
2686template <class _Tp, class _Allocator>
2687typename deque<_Tp, _Allocator>::iterator
2688deque<_Tp, _Allocator>::erase(const_iterator __f, const_iterator __l)
2689{
2690 difference_type __n = __l - __f;
2691 iterator __b = __base::begin();
2692 difference_type __pos = __f - __b;
2693 iterator __p = __b + __pos;
2694 if (__n > 0)
2695 {
2696 allocator_type& __a = __base::__alloc();
2697 if (__pos < (__base::size() - __n) / 2)
2698 { // erase from front
2699 iterator __i = _STD::move_backward(__b, __p, __p + __n);
2700 for (; __b != __i; ++__b)
Howard Hinnant2529d022011-02-02 17:36:20 +00002701 __alloc_traits::destroy(__a, _STD::addressof(*__b));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002702 __base::size() -= __n;
2703 __base::__start_ += __n;
2704 while (__front_spare() >= 2 * __base::__block_size)
2705 {
2706 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2707 __base::__map_.pop_front();
2708 __base::__start_ -= __base::__block_size;
2709 }
2710 }
2711 else
2712 { // erase from back
2713 iterator __i = _STD::move(__p + __n, __base::end(), __p);
2714 for (iterator __e = __base::end(); __i != __e; ++__i)
Howard Hinnant2529d022011-02-02 17:36:20 +00002715 __alloc_traits::destroy(__a, _STD::addressof(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002716 __base::size() -= __n;
2717 while (__back_spare() >= 2 * __base::__block_size)
2718 {
2719 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2720 __base::__map_.pop_back();
2721 }
2722 }
2723 }
2724 return __base::begin() + __pos;
2725}
2726
2727template <class _Tp, class _Allocator>
2728void
2729deque<_Tp, _Allocator>::__erase_to_end(const_iterator __f)
2730{
2731 iterator __e = __base::end();
2732 difference_type __n = __e - __f;
2733 if (__n > 0)
2734 {
2735 allocator_type& __a = __base::__alloc();
2736 iterator __b = __base::begin();
2737 difference_type __pos = __f - __b;
2738 for (iterator __p = __b + __pos; __p != __e; ++__p)
Howard Hinnant2529d022011-02-02 17:36:20 +00002739 __alloc_traits::destroy(__a, _STD::addressof(*__p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002740 __base::size() -= __n;
2741 while (__back_spare() >= 2 * __base::__block_size)
2742 {
2743 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2744 __base::__map_.pop_back();
2745 }
2746 }
2747}
2748
2749template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00002750inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002751void
2752deque<_Tp, _Allocator>::swap(deque& __c)
Howard Hinnant0a612b02011-06-02 20:00:14 +00002753 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2754 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002755{
2756 __base::swap(__c);
2757}
2758
2759template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00002760inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002761void
Howard Hinnanta12beb32011-06-02 16:10:22 +00002762deque<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002763{
2764 __base::clear();
2765}
2766
2767template <class _Tp, class _Allocator>
2768_LIBCPP_INLINE_VISIBILITY inline
2769bool
2770operator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2771{
2772 const typename deque<_Tp, _Allocator>::size_type __sz = __x.size();
2773 return __sz == __y.size() && _STD::equal(__x.begin(), __x.end(), __y.begin());
2774}
2775
2776template <class _Tp, class _Allocator>
2777_LIBCPP_INLINE_VISIBILITY inline
2778bool
2779operator!=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2780{
2781 return !(__x == __y);
2782}
2783
2784template <class _Tp, class _Allocator>
2785_LIBCPP_INLINE_VISIBILITY inline
2786bool
2787operator< (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2788{
2789 return _STD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
2790}
2791
2792template <class _Tp, class _Allocator>
2793_LIBCPP_INLINE_VISIBILITY inline
2794bool
2795operator> (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2796{
2797 return __y < __x;
2798}
2799
2800template <class _Tp, class _Allocator>
2801_LIBCPP_INLINE_VISIBILITY inline
2802bool
2803operator>=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2804{
2805 return !(__x < __y);
2806}
2807
2808template <class _Tp, class _Allocator>
2809_LIBCPP_INLINE_VISIBILITY inline
2810bool
2811operator<=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2812{
2813 return !(__y < __x);
2814}
2815
2816template <class _Tp, class _Allocator>
2817_LIBCPP_INLINE_VISIBILITY inline
2818void
2819swap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
Howard Hinnant0a612b02011-06-02 20:00:14 +00002820 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002821{
2822 __x.swap(__y);
2823}
2824
2825_LIBCPP_END_NAMESPACE_STD
2826
2827#endif // _LIBCPP_DEQUE