blob: 718fd88716cb03e347a170e24f367c59edf5ab63 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2#ifndef _LIBCPP_SPLIT_BUFFER
3#define _LIBCPP_SPLIT_BUFFER
4
5#include <__config>
6#include <type_traits>
7#include <algorithm>
8
9#pragma GCC system_header
10
11_LIBCPP_BEGIN_NAMESPACE_STD
12
13template <bool>
14class __split_buffer_common
15{
16protected:
17 void __throw_length_error() const;
18 void __throw_out_of_range() const;
19};
20
Howard Hinnantf8ce4592010-07-07 19:14:52 +000021template <class _Tp, class _Allocator = allocator<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000022struct __split_buffer
23 : private __split_buffer_common<true>
24{
25private:
26 __split_buffer(const __split_buffer&);
27 __split_buffer& operator=(const __split_buffer&);
28public:
Howard Hinnant324bb032010-08-22 00:02:43 +000029 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000030 typedef _Allocator allocator_type;
31 typedef typename remove_reference<allocator_type>::type __alloc_rr;
32 typedef allocator_traits<__alloc_rr> __alloc_traits;
33 typedef value_type& reference;
34 typedef const value_type& const_reference;
35 typedef typename __alloc_traits::size_type size_type;
36 typedef typename __alloc_traits::difference_type difference_type;
37 typedef typename __alloc_traits::pointer pointer;
38 typedef typename __alloc_traits::const_pointer const_pointer;
39 typedef pointer iterator;
40 typedef const_pointer const_iterator;
41
42 pointer __first_;
43 pointer __begin_;
44 pointer __end_;
45 __compressed_pair<pointer, allocator_type> __end_cap_;
46
47 typedef typename add_lvalue_reference<allocator_type>::type __alloc_ref;
48 typedef typename add_lvalue_reference<allocator_type>::type __alloc_const_ref;
49
50 _LIBCPP_INLINE_VISIBILITY __alloc_rr& __alloc() {return __end_cap_.second();}
51 _LIBCPP_INLINE_VISIBILITY const __alloc_rr& __alloc() const {return __end_cap_.second();}
52 _LIBCPP_INLINE_VISIBILITY pointer& __end_cap() {return __end_cap_.first();}
53 _LIBCPP_INLINE_VISIBILITY const pointer& __end_cap() const {return __end_cap_.first();}
54
55 __split_buffer();
56 explicit __split_buffer(__alloc_rr& __a);
57 explicit __split_buffer(const __alloc_rr& __a);
58 __split_buffer(size_type __cap, size_type __start, __alloc_rr& __a);
59 ~__split_buffer();
60
Howard Hinnant73d21a42010-09-04 23:28:19 +000061#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000062 __split_buffer(__split_buffer&& __c);
63 __split_buffer(__split_buffer&& __c, const __alloc_rr& __a);
64 __split_buffer& operator=(__split_buffer&& __c);
Howard Hinnant73d21a42010-09-04 23:28:19 +000065#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000066
67 _LIBCPP_INLINE_VISIBILITY iterator begin() {return __begin_;}
68 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const {return __begin_;}
69 _LIBCPP_INLINE_VISIBILITY iterator end() {return __end_;}
70 _LIBCPP_INLINE_VISIBILITY const_iterator end() const {return __end_;}
71
72 _LIBCPP_INLINE_VISIBILITY void clear() {__destruct_at_end(__begin_);}
73 _LIBCPP_INLINE_VISIBILITY size_type size() const {return static_cast<size_type>(__end_ - __begin_);}
74 _LIBCPP_INLINE_VISIBILITY bool empty() const {return __end_ == __begin_;}
75 _LIBCPP_INLINE_VISIBILITY size_type capacity() const {return static_cast<size_type>(__end_cap() - __first_);}
76 _LIBCPP_INLINE_VISIBILITY size_type __front_spare() const {return static_cast<size_type>(__begin_ - __first_);}
77 _LIBCPP_INLINE_VISIBILITY size_type __back_spare() const {return static_cast<size_type>(__end_cap() - __end_);}
78
79 _LIBCPP_INLINE_VISIBILITY reference front() {return *__begin_;}
80 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return *__begin_;}
81 _LIBCPP_INLINE_VISIBILITY reference back() {return *(__end_ - 1);}
82 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return *(__end_ - 1);}
83
84 void reserve(size_type __n);
85 void shrink_to_fit();
86 void push_front(const_reference __x);
87 void push_back(const_reference __x);
Michael J. Spencer626916f2010-12-10 19:47:54 +000088#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000089 void push_front(value_type&& __x);
90 void push_back(value_type&& __x);
Michael J. Spencer626916f2010-12-10 19:47:54 +000091#if !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000092 template <class... _Args>
93 void emplace_back(_Args&&... __args);
Michael J. Spencer626916f2010-12-10 19:47:54 +000094#endif // !defined(_LIBCPP_HAS_NO_VARIADICS)
95#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000096
97 _LIBCPP_INLINE_VISIBILITY void pop_front() {__destruct_at_begin(__begin_+1);}
98 _LIBCPP_INLINE_VISIBILITY void pop_back() {__destruct_at_end(__end_-1);}
99
100 void __construct_at_end(size_type __n);
101 void __construct_at_end(size_type __n, false_type);
102 void __construct_at_end(size_type __n, true_type);
103 void __construct_at_end(size_type __n, const_reference __x);
104 void __construct_at_end(size_type __n, const_reference __x, false_type);
105 void __construct_at_end(size_type __n, const_reference __x, true_type);
106 template <class _InputIter>
107 typename enable_if
108 <
109 __is_input_iterator<_InputIter>::value &&
110 !__is_forward_iterator<_InputIter>::value,
111 void
112 >::type
113 __construct_at_end(_InputIter __first, _InputIter __last);
114 template <class _ForwardIterator>
115 typename enable_if
116 <
117 __is_forward_iterator<_ForwardIterator>::value,
118 void
119 >::type
120 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
121
122 _LIBCPP_INLINE_VISIBILITY void __destruct_at_begin(pointer __new_begin)
Howard Hinnant1468b662010-11-19 22:17:28 +0000123 {__destruct_at_begin(__new_begin, is_trivially_destructible<value_type>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000124 void __destruct_at_begin(pointer __new_begin, false_type);
125 void __destruct_at_begin(pointer __new_begin, true_type);
126
127 _LIBCPP_INLINE_VISIBILITY void __destruct_at_end(pointer __new_last)
Howard Hinnant1468b662010-11-19 22:17:28 +0000128 {__destruct_at_end(__new_last, is_trivially_destructible<value_type>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000129 void __destruct_at_end(pointer __new_last, false_type);
130 void __destruct_at_end(pointer __new_last, true_type);
131
132 void swap(__split_buffer& __x);
133
134 bool __invariants() const;
135
136private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000137 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000138 void __move_assign_alloc(const __split_buffer& __c, true_type)
139 {
140 __alloc() = _STD::move(__c.__alloc());
141 }
142
Howard Hinnant333f50d2010-09-21 20:16:37 +0000143 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000144 void __move_assign_alloc(const __split_buffer& __c, false_type)
145 {}
146
Howard Hinnant333f50d2010-09-21 20:16:37 +0000147 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000148 static void __swap_alloc(__alloc_rr& __x, __alloc_rr& __y)
149 {__swap_alloc(__x, __y, integral_constant<bool,
150 __alloc_traits::propagate_on_container_swap::value>());}
151
Howard Hinnant333f50d2010-09-21 20:16:37 +0000152 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000153 static void __swap_alloc(__alloc_rr& __x, __alloc_rr& __y, true_type)
154 {
155 using _STD::swap;
156 swap(__x, __y);
157 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000158
159 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000160 static void __swap_alloc(__alloc_rr& __x, __alloc_rr& __y, false_type)
161 {}
162};
163
164template <class _Tp, class _Allocator>
165bool
166__split_buffer<_Tp, _Allocator>::__invariants() const
167{
168 if (__first_ == nullptr)
169 {
170 if (__begin_ != nullptr)
171 return false;
172 if (__end_ != nullptr)
173 return false;
174 if (__end_cap() != nullptr)
175 return false;
176 }
177 else
178 {
179 if (__begin_ < __first_)
180 return false;
181 if (__end_ < __begin_)
182 return false;
183 if (__end_cap() < __end_)
184 return false;
185 }
186 return true;
187}
188
189// Default constructs __n objects starting at __end_
190// throws if construction throws
191// Precondition: __n > 0
192// Precondition: size() + __n <= capacity()
193// Postcondition: size() == size() + __n
194template <class _Tp, class _Allocator>
195_LIBCPP_INLINE_VISIBILITY inline
196void
197__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n)
198{
199 __construct_at_end(__n, __is_zero_default_constructible<value_type>());
200}
201
202template <class _Tp, class _Allocator>
203void
204__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, false_type)
205{
206 __alloc_rr& __a = this->__alloc();
207 do
208 {
209 __alloc_traits::construct(__a, _STD::__to_raw_pointer(this->__end_), value_type());
210 ++this->__end_;
211 --__n;
212 } while (__n > 0);
213}
214
215template <class _Tp, class _Allocator>
216_LIBCPP_INLINE_VISIBILITY inline
217void
218__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, true_type)
219{
220 _STD::memset(this->__end_, 0, __n*sizeof(value_type));
221 this->__end_ += __n;
222}
223
224// Copy constructs __n objects starting at __end_ from __x
225// throws if construction throws
226// Precondition: __n > 0
227// Precondition: size() + __n <= capacity()
228// Postcondition: size() == old size() + __n
229// Postcondition: [i] == __x for all i in [size() - __n, __n)
230template <class _Tp, class _Allocator>
231_LIBCPP_INLINE_VISIBILITY inline
232void
233__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
234{
Howard Hinnant1468b662010-11-19 22:17:28 +0000235 __construct_at_end(__n, __x, integral_constant<bool, is_trivially_copy_constructible<value_type>::value &&
236 is_trivially_copy_assignable<value_type>::value>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000237}
238
239template <class _Tp, class _Allocator>
240void
241__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x, false_type)
242{
243 __alloc_rr& __a = this->__alloc();
244 do
245 {
246 __alloc_traits::construct(__a, _STD::__to_raw_pointer(this->__end_), __x);
247 ++this->__end_;
248 --__n;
249 } while (__n > 0);
250}
251
252template <class _Tp, class _Allocator>
253_LIBCPP_INLINE_VISIBILITY inline
254void
255__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x, true_type)
256{
257 _STD::fill_n(this->__end_, __n, __x);
258 this->__end_ += __n;
259}
260
261template <class _Tp, class _Allocator>
262template <class _InputIter>
263typename enable_if
264<
265 __is_input_iterator<_InputIter>::value &&
266 !__is_forward_iterator<_InputIter>::value,
267 void
268>::type
269__split_buffer<_Tp, _Allocator>::__construct_at_end(_InputIter __first, _InputIter __last)
270{
271 __alloc_rr& __a = this->__alloc();
272 for (; __first != __last; ++__first)
273 {
274 if (__end_ == __end_cap())
275 {
276 size_type __old_cap = __end_cap() - __first_;
277 size_type __new_cap = _STD::max<size_type>(2 * __old_cap, 8);
278 __split_buffer __buf(__new_cap, 0, __a);
279 for (pointer __p = __begin_; __p != __end_; ++__p, ++__buf.__end_)
280 __alloc_traits::construct(__buf.__alloc(),
281 _STD::__to_raw_pointer(__buf.__end_), _STD::move(*__p));
282 swap(__buf);
283 }
284 __alloc_traits::construct(__a, _STD::__to_raw_pointer(this->__end_), *__first);
285 ++this->__end_;
286 }
287}
288
289template <class _Tp, class _Allocator>
290template <class _ForwardIterator>
291typename enable_if
292<
293 __is_forward_iterator<_ForwardIterator>::value,
294 void
295>::type
296__split_buffer<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
297{
298 __alloc_rr& __a = this->__alloc();
299 for (; __first != __last; ++__first)
300 {
301 __alloc_traits::construct(__a, _STD::__to_raw_pointer(this->__end_), *__first);
302 ++this->__end_;
303 }
304}
305
306template <class _Tp, class _Allocator>
307_LIBCPP_INLINE_VISIBILITY inline
308void
309__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type)
310{
311 while (__begin_ < __new_begin)
312 __alloc_traits::destroy(__alloc(), __begin_++);
313}
314
315template <class _Tp, class _Allocator>
316_LIBCPP_INLINE_VISIBILITY inline
317void
318__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, true_type)
319{
320 __begin_ = __new_begin;
321}
322
323template <class _Tp, class _Allocator>
324_LIBCPP_INLINE_VISIBILITY inline
325void
326__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type)
327{
328 while (__new_last < __end_)
329 __alloc_traits::destroy(__alloc(), --__end_);
330}
331
332template <class _Tp, class _Allocator>
333_LIBCPP_INLINE_VISIBILITY inline
334void
335__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, true_type)
336{
337 __end_ = __new_last;
338}
339
340template <class _Tp, class _Allocator>
341__split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __start, __alloc_rr& __a)
342 : __end_cap_(0, __a)
343{
344 __first_ = __cap != 0 ? __alloc_traits::allocate(__alloc(), __cap) : nullptr;
345 __begin_ = __end_ = __first_ + __start;
346 __end_cap() = __first_ + __cap;
347}
348
349template <class _Tp, class _Allocator>
350_LIBCPP_INLINE_VISIBILITY inline
351__split_buffer<_Tp, _Allocator>::__split_buffer()
352 : __first_(0), __begin_(0), __end_(0), __end_cap_(0)
353{
354}
355
356template <class _Tp, class _Allocator>
357_LIBCPP_INLINE_VISIBILITY inline
358__split_buffer<_Tp, _Allocator>::__split_buffer(__alloc_rr& __a)
359 : __first_(0), __begin_(0), __end_(0), __end_cap_(0, __a)
360{
361}
362
363template <class _Tp, class _Allocator>
364_LIBCPP_INLINE_VISIBILITY inline
365__split_buffer<_Tp, _Allocator>::__split_buffer(const __alloc_rr& __a)
366 : __first_(0), __begin_(0), __end_(0), __end_cap_(0, __a)
367{
368}
369
370template <class _Tp, class _Allocator>
371__split_buffer<_Tp, _Allocator>::~__split_buffer()
372{
373 clear();
374 if (__first_)
375 __alloc_traits::deallocate(__alloc(), __first_, capacity());
376}
377
Howard Hinnant73d21a42010-09-04 23:28:19 +0000378#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000379
380template <class _Tp, class _Allocator>
381__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c)
382 : __first_(_STD::move(__c.__first_)),
383 __begin_(_STD::move(__c.__begin_)),
384 __end_(_STD::move(__c.__end_)),
385 __end_cap_(_STD::move(__c.__end_cap_))
386{
387 __c.__first_ = nullptr;
388 __c.__begin_ = nullptr;
389 __c.__end_ = nullptr;
390 __c.__end_cap() = nullptr;
391}
392
393template <class _Tp, class _Allocator>
394__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a)
395 : __end_cap_(__a)
396{
397 if (__a == __c.__alloc())
398 {
399 __first_ = __c.__first_;
400 __begin_ = __c.__begin_;
401 __end_ = __c.__end_;
402 __end_cap() = __c.__end_cap();
403 __c.__first_ = nullptr;
404 __c.__begin_ = nullptr;
405 __c.__end_ = nullptr;
406 __c.__end_cap() = nullptr;
407 }
408 else
409 {
410 size_type __cap = __c.size();
411 __first_ = __alloc_traits::allocate(__alloc(), __cap);
412 __begin_ = __end_ = __first_;
413 __end_cap() = __first_ + __cap;
414 typedef move_iterator<iterator> _I;
415 __construct_at_end(_I(__c.begin()), _I(__c.end()));
416 }
417}
418
419template <class _Tp, class _Allocator>
420__split_buffer<_Tp, _Allocator>&
421__split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c)
422{
423 clear();
424 shrink_to_fit();
425 __first_ = __c.__first_;
426 __begin_ = __c.__begin_;
427 __end_ = __c.__end_;
428 __end_cap() = __c.__end_cap();
429 __move_assign_alloc(__c,
430 integral_constant<bool,
431 __alloc_traits::propagate_on_container_move_assignment::value>());
432 __c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
433 return *this;
434}
435
Howard Hinnant73d21a42010-09-04 23:28:19 +0000436#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000437
438template <class _Tp, class _Allocator>
439void
440__split_buffer<_Tp, _Allocator>::swap(__split_buffer& __x)
441{
442 _STD::swap(__first_, __x.__first_);
443 _STD::swap(__begin_, __x.__begin_);
444 _STD::swap(__end_, __x.__end_);
445 _STD::swap(__end_cap(), __x.__end_cap());
446 __swap_alloc(__alloc(), __x.__alloc());
447}
448
449template <class _Tp, class _Allocator>
450void
451__split_buffer<_Tp, _Allocator>::reserve(size_type __n)
452{
453 if (__n < capacity())
454 {
455 __split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc());
456 __t.__construct_at_end(move_iterator<pointer>(__begin_),
457 move_iterator<pointer>(__end_));
458 _STD::swap(__first_, __t.__first_);
459 _STD::swap(__begin_, __t.__begin_);
460 _STD::swap(__end_, __t.__end_);
461 _STD::swap(__end_cap(), __t.__end_cap());
462 }
463}
464
465template <class _Tp, class _Allocator>
466void
467__split_buffer<_Tp, _Allocator>::shrink_to_fit()
468{
469 if (capacity() > size())
470 {
471#ifndef _LIBCPP_NO_EXCEPTIONS
472 try
473 {
Howard Hinnant324bb032010-08-22 00:02:43 +0000474#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000475 __split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc());
476 __t.__construct_at_end(move_iterator<pointer>(__begin_),
477 move_iterator<pointer>(__end_));
478 __t.__end_ = __t.__begin_ + (__end_ - __begin_);
479 _STD::swap(__first_, __t.__first_);
480 _STD::swap(__begin_, __t.__begin_);
481 _STD::swap(__end_, __t.__end_);
482 _STD::swap(__end_cap(), __t.__end_cap());
483#ifndef _LIBCPP_NO_EXCEPTIONS
484 }
485 catch (...)
486 {
487 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000488#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000489 }
490}
491
492template <class _Tp, class _Allocator>
493void
494__split_buffer<_Tp, _Allocator>::push_front(const_reference __x)
495{
496 if (__begin_ == __first_)
497 {
498 if (__end_ < __end_cap())
499 {
500 difference_type __d = __end_cap() - __end_;
501 __d = (__d + 1) / 2;
502 __begin_ = _STD::move_backward(__begin_, __end_, __end_ + __d);
503 __end_ += __d;
504 }
505 else
506 {
507 size_type __c = max<size_type>(2 * (__end_cap() - __first_), 1);
Howard Hinnantf8ce4592010-07-07 19:14:52 +0000508 __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000509 __t.__construct_at_end(move_iterator<pointer>(__begin_),
510 move_iterator<pointer>(__end_));
511 _STD::swap(__first_, __t.__first_);
512 _STD::swap(__begin_, __t.__begin_);
513 _STD::swap(__end_, __t.__end_);
514 _STD::swap(__end_cap(), __t.__end_cap());
515 }
516 }
517 __alloc_traits::construct(__alloc(), _STD::__to_raw_pointer(__begin_-1), __x);
518 --__begin_;
519}
520
Howard Hinnant73d21a42010-09-04 23:28:19 +0000521#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000522
523template <class _Tp, class _Allocator>
524void
525__split_buffer<_Tp, _Allocator>::push_front(value_type&& __x)
526{
527 if (__begin_ == __first_)
528 {
529 if (__end_ < __end_cap())
530 {
531 difference_type __d = __end_cap() - __end_;
532 __d = (__d + 1) / 2;
533 __begin_ = _STD::move_backward(__begin_, __end_, __end_ + __d);
534 __end_ += __d;
535 }
536 else
537 {
538 size_type __c = max<size_type>(2 * (__end_cap() - __first_), 1);
Howard Hinnantf8ce4592010-07-07 19:14:52 +0000539 __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000540 __t.__construct_at_end(move_iterator<pointer>(__begin_),
541 move_iterator<pointer>(__end_));
542 _STD::swap(__first_, __t.__first_);
543 _STD::swap(__begin_, __t.__begin_);
544 _STD::swap(__end_, __t.__end_);
545 _STD::swap(__end_cap(), __t.__end_cap());
546 }
547 }
548 __alloc_traits::construct(__alloc(), _STD::__to_raw_pointer(__begin_-1),
549 _STD::move(__x));
550 --__begin_;
551}
552
Howard Hinnant73d21a42010-09-04 23:28:19 +0000553#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000554
555template <class _Tp, class _Allocator>
556_LIBCPP_INLINE_VISIBILITY inline
557void
558__split_buffer<_Tp, _Allocator>::push_back(const_reference __x)
559{
560 if (__end_ == __end_cap())
561 {
562 if (__begin_ > __first_)
563 {
564 difference_type __d = __begin_ - __first_;
565 __d = (__d + 1) / 2;
566 __end_ = _STD::move(__begin_, __end_, __begin_ - __d);
567 __begin_ -= __d;
568 }
569 else
570 {
571 size_type __c = max<size_type>(2 * (__end_cap() - __first_), 1);
572 __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
573 __t.__construct_at_end(move_iterator<pointer>(__begin_),
574 move_iterator<pointer>(__end_));
575 _STD::swap(__first_, __t.__first_);
576 _STD::swap(__begin_, __t.__begin_);
577 _STD::swap(__end_, __t.__end_);
578 _STD::swap(__end_cap(), __t.__end_cap());
579 }
580 }
581 __alloc_traits::construct(__alloc(), _STD::__to_raw_pointer(__end_), __x);
582 ++__end_;
583}
584
Howard Hinnant73d21a42010-09-04 23:28:19 +0000585#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000586
587template <class _Tp, class _Allocator>
588void
589__split_buffer<_Tp, _Allocator>::push_back(value_type&& __x)
590{
591 if (__end_ == __end_cap())
592 {
593 if (__begin_ > __first_)
594 {
595 difference_type __d = __begin_ - __first_;
596 __d = (__d + 1) / 2;
597 __end_ = _STD::move(__begin_, __end_, __begin_ - __d);
598 __begin_ -= __d;
599 }
600 else
601 {
602 size_type __c = max<size_type>(2 * (__end_cap() - __first_), 1);
603 __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
604 __t.__construct_at_end(move_iterator<pointer>(__begin_),
605 move_iterator<pointer>(__end_));
606 _STD::swap(__first_, __t.__first_);
607 _STD::swap(__begin_, __t.__begin_);
608 _STD::swap(__end_, __t.__end_);
609 _STD::swap(__end_cap(), __t.__end_cap());
610 }
611 }
612 __alloc_traits::construct(__alloc(), _STD::__to_raw_pointer(__end_),
613 _STD::move(__x));
614 ++__end_;
615}
616
Howard Hinnant73d21a42010-09-04 23:28:19 +0000617#ifndef _LIBCPP_HAS_NO_VARIADICS
618
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000619template <class _Tp, class _Allocator>
620template <class... _Args>
621void
622__split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args)
623{
624 if (__end_ == __end_cap())
625 {
626 if (__begin_ > __first_)
627 {
628 difference_type __d = __begin_ - __first_;
629 __d = (__d + 1) / 2;
630 __end_ = _STD::move(__begin_, __end_, __begin_ - __d);
631 __begin_ -= __d;
632 }
633 else
634 {
635 size_type __c = max<size_type>(2 * (__end_cap() - __first_), 1);
636 __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
637 __t.__construct_at_end(move_iterator<pointer>(__begin_),
638 move_iterator<pointer>(__end_));
639 _STD::swap(__first_, __t.__first_);
640 _STD::swap(__begin_, __t.__begin_);
641 _STD::swap(__end_, __t.__end_);
642 _STD::swap(__end_cap(), __t.__end_cap());
643 }
644 }
645 __alloc_traits::construct(__alloc(), _STD::__to_raw_pointer(__end_),
646 _STD::forward<_Args>(__args)...);
647 ++__end_;
648}
649
Howard Hinnant73d21a42010-09-04 23:28:19 +0000650#endif // _LIBCPP_HAS_NO_VARIADICS
651
652#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000653
654_LIBCPP_END_NAMESPACE_STD
655
656#endif // _LIBCPP_SPLIT_BUFFER