blob: e0aa13b898832f495fbb4a2291c8c8b3b65baa88 [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
Howard Hinnant66c6f972011-11-29 16:45:27 +00009#include <__undef_min_max>
10
Howard Hinnant08e17472011-10-17 20:05:10 +000011#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000012#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +000013#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000014
15_LIBCPP_BEGIN_NAMESPACE_STD
16
17template <bool>
18class __split_buffer_common
19{
20protected:
21 void __throw_length_error() const;
22 void __throw_out_of_range() const;
23};
24
Howard Hinnantf8ce4592010-07-07 19:14:52 +000025template <class _Tp, class _Allocator = allocator<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000026struct __split_buffer
27 : private __split_buffer_common<true>
28{
29private:
30 __split_buffer(const __split_buffer&);
31 __split_buffer& operator=(const __split_buffer&);
32public:
Howard Hinnant324bb032010-08-22 00:02:43 +000033 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000034 typedef _Allocator allocator_type;
35 typedef typename remove_reference<allocator_type>::type __alloc_rr;
36 typedef allocator_traits<__alloc_rr> __alloc_traits;
37 typedef value_type& reference;
38 typedef const value_type& const_reference;
39 typedef typename __alloc_traits::size_type size_type;
40 typedef typename __alloc_traits::difference_type difference_type;
41 typedef typename __alloc_traits::pointer pointer;
42 typedef typename __alloc_traits::const_pointer const_pointer;
43 typedef pointer iterator;
44 typedef const_pointer const_iterator;
45
46 pointer __first_;
47 pointer __begin_;
48 pointer __end_;
49 __compressed_pair<pointer, allocator_type> __end_cap_;
50
51 typedef typename add_lvalue_reference<allocator_type>::type __alloc_ref;
52 typedef typename add_lvalue_reference<allocator_type>::type __alloc_const_ref;
53
Howard Hinnant0a612b02011-06-02 20:00:14 +000054 _LIBCPP_INLINE_VISIBILITY __alloc_rr& __alloc() _NOEXCEPT {return __end_cap_.second();}
55 _LIBCPP_INLINE_VISIBILITY const __alloc_rr& __alloc() const _NOEXCEPT {return __end_cap_.second();}
56 _LIBCPP_INLINE_VISIBILITY pointer& __end_cap() _NOEXCEPT {return __end_cap_.first();}
57 _LIBCPP_INLINE_VISIBILITY const pointer& __end_cap() const _NOEXCEPT {return __end_cap_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000058
Howard Hinnant009b2c42011-06-03 15:16:49 +000059 __split_buffer()
60 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000061 explicit __split_buffer(__alloc_rr& __a);
62 explicit __split_buffer(const __alloc_rr& __a);
63 __split_buffer(size_type __cap, size_type __start, __alloc_rr& __a);
64 ~__split_buffer();
65
Howard Hinnant73d21a42010-09-04 23:28:19 +000066#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant0a612b02011-06-02 20:00:14 +000067 __split_buffer(__split_buffer&& __c)
68 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000069 __split_buffer(__split_buffer&& __c, const __alloc_rr& __a);
Howard Hinnant0a612b02011-06-02 20:00:14 +000070 __split_buffer& operator=(__split_buffer&& __c)
71 _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
72 is_nothrow_move_assignable<allocator_type>::value) ||
73 !__alloc_traits::propagate_on_container_move_assignment::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +000074#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000075
Howard Hinnant0a612b02011-06-02 20:00:14 +000076 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT {return __begin_;}
77 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT {return __begin_;}
78 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT {return __end_;}
79 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT {return __end_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000080
Howard Hinnant0a612b02011-06-02 20:00:14 +000081 _LIBCPP_INLINE_VISIBILITY
82 void clear() _NOEXCEPT
83 {__destruct_at_end(__begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000084 _LIBCPP_INLINE_VISIBILITY size_type size() const {return static_cast<size_type>(__end_ - __begin_);}
85 _LIBCPP_INLINE_VISIBILITY bool empty() const {return __end_ == __begin_;}
86 _LIBCPP_INLINE_VISIBILITY size_type capacity() const {return static_cast<size_type>(__end_cap() - __first_);}
87 _LIBCPP_INLINE_VISIBILITY size_type __front_spare() const {return static_cast<size_type>(__begin_ - __first_);}
88 _LIBCPP_INLINE_VISIBILITY size_type __back_spare() const {return static_cast<size_type>(__end_cap() - __end_);}
89
90 _LIBCPP_INLINE_VISIBILITY reference front() {return *__begin_;}
91 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return *__begin_;}
92 _LIBCPP_INLINE_VISIBILITY reference back() {return *(__end_ - 1);}
93 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return *(__end_ - 1);}
94
95 void reserve(size_type __n);
Howard Hinnant0a612b02011-06-02 20:00:14 +000096 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000097 void push_front(const_reference __x);
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +000098 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Michael J. Spencer626916f2010-12-10 19:47:54 +000099#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000100 void push_front(value_type&& __x);
101 void push_back(value_type&& __x);
Michael J. Spencer626916f2010-12-10 19:47:54 +0000102#if !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000103 template <class... _Args>
104 void emplace_back(_Args&&... __args);
Michael J. Spencer626916f2010-12-10 19:47:54 +0000105#endif // !defined(_LIBCPP_HAS_NO_VARIADICS)
106#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000107
108 _LIBCPP_INLINE_VISIBILITY void pop_front() {__destruct_at_begin(__begin_+1);}
109 _LIBCPP_INLINE_VISIBILITY void pop_back() {__destruct_at_end(__end_-1);}
110
111 void __construct_at_end(size_type __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000112 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000113 template <class _InputIter>
114 typename enable_if
115 <
116 __is_input_iterator<_InputIter>::value &&
117 !__is_forward_iterator<_InputIter>::value,
118 void
119 >::type
120 __construct_at_end(_InputIter __first, _InputIter __last);
121 template <class _ForwardIterator>
122 typename enable_if
123 <
124 __is_forward_iterator<_ForwardIterator>::value,
125 void
126 >::type
127 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
128
129 _LIBCPP_INLINE_VISIBILITY void __destruct_at_begin(pointer __new_begin)
Howard Hinnant1468b662010-11-19 22:17:28 +0000130 {__destruct_at_begin(__new_begin, is_trivially_destructible<value_type>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000131 void __destruct_at_begin(pointer __new_begin, false_type);
132 void __destruct_at_begin(pointer __new_begin, true_type);
133
Howard Hinnant0a612b02011-06-02 20:00:14 +0000134 _LIBCPP_INLINE_VISIBILITY
135 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000136 {__destruct_at_end(__new_last, false_type());}
137 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0a612b02011-06-02 20:00:14 +0000138 void __destruct_at_end(pointer __new_last, false_type) _NOEXCEPT;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0a612b02011-06-02 20:00:14 +0000140 void __destruct_at_end(pointer __new_last, true_type) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000141
Howard Hinnant0a612b02011-06-02 20:00:14 +0000142 void swap(__split_buffer& __x)
143 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
144 __is_nothrow_swappable<__alloc_rr>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000145
146 bool __invariants() const;
147
148private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000149 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +0000150 void __move_assign_alloc(__split_buffer& __c, true_type)
Howard Hinnant0a612b02011-06-02 20:00:14 +0000151 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000152 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000153 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000154 }
155
Howard Hinnant333f50d2010-09-21 20:16:37 +0000156 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000157 void __move_assign_alloc(__split_buffer&, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000158 {}
159
Howard Hinnant333f50d2010-09-21 20:16:37 +0000160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000161 static void __swap_alloc(__alloc_rr& __x, __alloc_rr& __y)
Howard Hinnant0a612b02011-06-02 20:00:14 +0000162 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
163 __is_nothrow_swappable<__alloc_rr>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000164 {__swap_alloc(__x, __y, integral_constant<bool,
165 __alloc_traits::propagate_on_container_swap::value>());}
166
Howard Hinnant333f50d2010-09-21 20:16:37 +0000167 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000168 static void __swap_alloc(__alloc_rr& __x, __alloc_rr& __y, true_type)
Howard Hinnant0a612b02011-06-02 20:00:14 +0000169 _NOEXCEPT_(__is_nothrow_swappable<__alloc_rr>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000170 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000171 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000172 swap(__x, __y);
173 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000174
175 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000176 static void __swap_alloc(__alloc_rr&, __alloc_rr&, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000177 {}
178};
179
180template <class _Tp, class _Allocator>
181bool
182__split_buffer<_Tp, _Allocator>::__invariants() const
183{
184 if (__first_ == nullptr)
185 {
186 if (__begin_ != nullptr)
187 return false;
188 if (__end_ != nullptr)
189 return false;
190 if (__end_cap() != nullptr)
191 return false;
192 }
193 else
194 {
195 if (__begin_ < __first_)
196 return false;
197 if (__end_ < __begin_)
198 return false;
199 if (__end_cap() < __end_)
200 return false;
201 }
202 return true;
203}
204
205// Default constructs __n objects starting at __end_
206// throws if construction throws
207// Precondition: __n > 0
208// Precondition: size() + __n <= capacity()
209// Postcondition: size() == size() + __n
210template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000211void
212__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n)
213{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000214 __alloc_rr& __a = this->__alloc();
215 do
216 {
Howard Hinnant5f255942011-08-28 15:21:29 +0000217 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000218 ++this->__end_;
219 --__n;
220 } while (__n > 0);
221}
222
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000223// Copy constructs __n objects starting at __end_ from __x
224// throws if construction throws
225// Precondition: __n > 0
226// Precondition: size() + __n <= capacity()
227// Postcondition: size() == old size() + __n
228// Postcondition: [i] == __x for all i in [size() - __n, __n)
229template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000230void
231__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
232{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000233 __alloc_rr& __a = this->__alloc();
234 do
235 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000236 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000237 ++this->__end_;
238 --__n;
239 } while (__n > 0);
240}
241
242template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000243template <class _InputIter>
244typename enable_if
245<
246 __is_input_iterator<_InputIter>::value &&
247 !__is_forward_iterator<_InputIter>::value,
248 void
249>::type
250__split_buffer<_Tp, _Allocator>::__construct_at_end(_InputIter __first, _InputIter __last)
251{
252 __alloc_rr& __a = this->__alloc();
253 for (; __first != __last; ++__first)
254 {
255 if (__end_ == __end_cap())
256 {
257 size_type __old_cap = __end_cap() - __first_;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000258 size_type __new_cap = _VSTD::max<size_type>(2 * __old_cap, 8);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000259 __split_buffer __buf(__new_cap, 0, __a);
260 for (pointer __p = __begin_; __p != __end_; ++__p, ++__buf.__end_)
261 __alloc_traits::construct(__buf.__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +0000262 _VSTD::__to_raw_pointer(__buf.__end_), _VSTD::move(*__p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000263 swap(__buf);
264 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000265 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000266 ++this->__end_;
267 }
268}
269
270template <class _Tp, class _Allocator>
271template <class _ForwardIterator>
272typename enable_if
273<
274 __is_forward_iterator<_ForwardIterator>::value,
275 void
276>::type
277__split_buffer<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
278{
279 __alloc_rr& __a = this->__alloc();
280 for (; __first != __last; ++__first)
281 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000282 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000283 ++this->__end_;
284 }
285}
286
287template <class _Tp, class _Allocator>
288_LIBCPP_INLINE_VISIBILITY inline
289void
290__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type)
291{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000292 while (__begin_ != __new_begin)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000293 __alloc_traits::destroy(__alloc(), __begin_++);
294}
295
296template <class _Tp, class _Allocator>
297_LIBCPP_INLINE_VISIBILITY inline
298void
299__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, true_type)
300{
301 __begin_ = __new_begin;
302}
303
304template <class _Tp, class _Allocator>
305_LIBCPP_INLINE_VISIBILITY inline
306void
Howard Hinnant0a612b02011-06-02 20:00:14 +0000307__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000308{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000309 while (__new_last != __end_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000310 __alloc_traits::destroy(__alloc(), --__end_);
311}
312
313template <class _Tp, class _Allocator>
314_LIBCPP_INLINE_VISIBILITY inline
315void
Howard Hinnant0a612b02011-06-02 20:00:14 +0000316__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000317{
318 __end_ = __new_last;
319}
320
321template <class _Tp, class _Allocator>
322__split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __start, __alloc_rr& __a)
323 : __end_cap_(0, __a)
324{
325 __first_ = __cap != 0 ? __alloc_traits::allocate(__alloc(), __cap) : nullptr;
326 __begin_ = __end_ = __first_ + __start;
327 __end_cap() = __first_ + __cap;
328}
329
330template <class _Tp, class _Allocator>
331_LIBCPP_INLINE_VISIBILITY inline
332__split_buffer<_Tp, _Allocator>::__split_buffer()
Howard Hinnant009b2c42011-06-03 15:16:49 +0000333 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000334 : __first_(0), __begin_(0), __end_(0), __end_cap_(0)
335{
336}
337
338template <class _Tp, class _Allocator>
339_LIBCPP_INLINE_VISIBILITY inline
340__split_buffer<_Tp, _Allocator>::__split_buffer(__alloc_rr& __a)
341 : __first_(0), __begin_(0), __end_(0), __end_cap_(0, __a)
342{
343}
344
345template <class _Tp, class _Allocator>
346_LIBCPP_INLINE_VISIBILITY inline
347__split_buffer<_Tp, _Allocator>::__split_buffer(const __alloc_rr& __a)
348 : __first_(0), __begin_(0), __end_(0), __end_cap_(0, __a)
349{
350}
351
352template <class _Tp, class _Allocator>
353__split_buffer<_Tp, _Allocator>::~__split_buffer()
354{
355 clear();
356 if (__first_)
357 __alloc_traits::deallocate(__alloc(), __first_, capacity());
358}
359
Howard Hinnant73d21a42010-09-04 23:28:19 +0000360#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000361
362template <class _Tp, class _Allocator>
363__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c)
Howard Hinnant0a612b02011-06-02 20:00:14 +0000364 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000365 : __first_(_VSTD::move(__c.__first_)),
366 __begin_(_VSTD::move(__c.__begin_)),
367 __end_(_VSTD::move(__c.__end_)),
368 __end_cap_(_VSTD::move(__c.__end_cap_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000369{
370 __c.__first_ = nullptr;
371 __c.__begin_ = nullptr;
372 __c.__end_ = nullptr;
373 __c.__end_cap() = nullptr;
374}
375
376template <class _Tp, class _Allocator>
377__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a)
378 : __end_cap_(__a)
379{
380 if (__a == __c.__alloc())
381 {
382 __first_ = __c.__first_;
383 __begin_ = __c.__begin_;
384 __end_ = __c.__end_;
385 __end_cap() = __c.__end_cap();
386 __c.__first_ = nullptr;
387 __c.__begin_ = nullptr;
388 __c.__end_ = nullptr;
389 __c.__end_cap() = nullptr;
390 }
391 else
392 {
393 size_type __cap = __c.size();
394 __first_ = __alloc_traits::allocate(__alloc(), __cap);
395 __begin_ = __end_ = __first_;
396 __end_cap() = __first_ + __cap;
Howard Hinnant99968442011-11-29 18:15:50 +0000397 typedef move_iterator<iterator> _Ip;
398 __construct_at_end(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000399 }
400}
401
402template <class _Tp, class _Allocator>
403__split_buffer<_Tp, _Allocator>&
404__split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c)
Howard Hinnant0a612b02011-06-02 20:00:14 +0000405 _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
406 is_nothrow_move_assignable<allocator_type>::value) ||
407 !__alloc_traits::propagate_on_container_move_assignment::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000408{
409 clear();
410 shrink_to_fit();
411 __first_ = __c.__first_;
412 __begin_ = __c.__begin_;
413 __end_ = __c.__end_;
414 __end_cap() = __c.__end_cap();
415 __move_assign_alloc(__c,
416 integral_constant<bool,
417 __alloc_traits::propagate_on_container_move_assignment::value>());
418 __c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
419 return *this;
420}
421
Howard Hinnant73d21a42010-09-04 23:28:19 +0000422#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000423
424template <class _Tp, class _Allocator>
425void
426__split_buffer<_Tp, _Allocator>::swap(__split_buffer& __x)
Howard Hinnant0a612b02011-06-02 20:00:14 +0000427 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
428 __is_nothrow_swappable<__alloc_rr>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000429{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000430 _VSTD::swap(__first_, __x.__first_);
431 _VSTD::swap(__begin_, __x.__begin_);
432 _VSTD::swap(__end_, __x.__end_);
433 _VSTD::swap(__end_cap(), __x.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000434 __swap_alloc(__alloc(), __x.__alloc());
435}
436
437template <class _Tp, class _Allocator>
438void
439__split_buffer<_Tp, _Allocator>::reserve(size_type __n)
440{
441 if (__n < capacity())
442 {
443 __split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc());
444 __t.__construct_at_end(move_iterator<pointer>(__begin_),
445 move_iterator<pointer>(__end_));
Howard Hinnant0949eed2011-06-30 21:18:19 +0000446 _VSTD::swap(__first_, __t.__first_);
447 _VSTD::swap(__begin_, __t.__begin_);
448 _VSTD::swap(__end_, __t.__end_);
449 _VSTD::swap(__end_cap(), __t.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000450 }
451}
452
453template <class _Tp, class _Allocator>
454void
Howard Hinnant0a612b02011-06-02 20:00:14 +0000455__split_buffer<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000456{
457 if (capacity() > size())
458 {
459#ifndef _LIBCPP_NO_EXCEPTIONS
460 try
461 {
Howard Hinnant324bb032010-08-22 00:02:43 +0000462#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000463 __split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc());
464 __t.__construct_at_end(move_iterator<pointer>(__begin_),
465 move_iterator<pointer>(__end_));
466 __t.__end_ = __t.__begin_ + (__end_ - __begin_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000467 _VSTD::swap(__first_, __t.__first_);
468 _VSTD::swap(__begin_, __t.__begin_);
469 _VSTD::swap(__end_, __t.__end_);
470 _VSTD::swap(__end_cap(), __t.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000471#ifndef _LIBCPP_NO_EXCEPTIONS
472 }
473 catch (...)
474 {
475 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000476#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000477 }
478}
479
480template <class _Tp, class _Allocator>
481void
482__split_buffer<_Tp, _Allocator>::push_front(const_reference __x)
483{
484 if (__begin_ == __first_)
485 {
486 if (__end_ < __end_cap())
487 {
488 difference_type __d = __end_cap() - __end_;
489 __d = (__d + 1) / 2;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000490 __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000491 __end_ += __d;
492 }
493 else
494 {
Howard Hinnantec3773c2011-12-01 20:21:04 +0000495 size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
Howard Hinnantf8ce4592010-07-07 19:14:52 +0000496 __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000497 __t.__construct_at_end(move_iterator<pointer>(__begin_),
498 move_iterator<pointer>(__end_));
Howard Hinnant0949eed2011-06-30 21:18:19 +0000499 _VSTD::swap(__first_, __t.__first_);
500 _VSTD::swap(__begin_, __t.__begin_);
501 _VSTD::swap(__end_, __t.__end_);
502 _VSTD::swap(__end_cap(), __t.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000503 }
504 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000505 __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__begin_-1), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000506 --__begin_;
507}
508
Howard Hinnant73d21a42010-09-04 23:28:19 +0000509#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000510
511template <class _Tp, class _Allocator>
512void
513__split_buffer<_Tp, _Allocator>::push_front(value_type&& __x)
514{
515 if (__begin_ == __first_)
516 {
517 if (__end_ < __end_cap())
518 {
519 difference_type __d = __end_cap() - __end_;
520 __d = (__d + 1) / 2;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000521 __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000522 __end_ += __d;
523 }
524 else
525 {
Howard Hinnantec3773c2011-12-01 20:21:04 +0000526 size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
Howard Hinnantf8ce4592010-07-07 19:14:52 +0000527 __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000528 __t.__construct_at_end(move_iterator<pointer>(__begin_),
529 move_iterator<pointer>(__end_));
Howard Hinnant0949eed2011-06-30 21:18:19 +0000530 _VSTD::swap(__first_, __t.__first_);
531 _VSTD::swap(__begin_, __t.__begin_);
532 _VSTD::swap(__end_, __t.__end_);
533 _VSTD::swap(__end_cap(), __t.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000534 }
535 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000536 __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__begin_-1),
537 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000538 --__begin_;
539}
540
Howard Hinnant73d21a42010-09-04 23:28:19 +0000541#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000542
543template <class _Tp, class _Allocator>
544_LIBCPP_INLINE_VISIBILITY inline
545void
546__split_buffer<_Tp, _Allocator>::push_back(const_reference __x)
547{
548 if (__end_ == __end_cap())
549 {
550 if (__begin_ > __first_)
551 {
552 difference_type __d = __begin_ - __first_;
553 __d = (__d + 1) / 2;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000554 __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000555 __begin_ -= __d;
556 }
557 else
558 {
Howard Hinnantec3773c2011-12-01 20:21:04 +0000559 size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000560 __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
561 __t.__construct_at_end(move_iterator<pointer>(__begin_),
562 move_iterator<pointer>(__end_));
Howard Hinnant0949eed2011-06-30 21:18:19 +0000563 _VSTD::swap(__first_, __t.__first_);
564 _VSTD::swap(__begin_, __t.__begin_);
565 _VSTD::swap(__end_, __t.__end_);
566 _VSTD::swap(__end_cap(), __t.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000567 }
568 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000569 __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000570 ++__end_;
571}
572
Howard Hinnant73d21a42010-09-04 23:28:19 +0000573#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000574
575template <class _Tp, class _Allocator>
576void
577__split_buffer<_Tp, _Allocator>::push_back(value_type&& __x)
578{
579 if (__end_ == __end_cap())
580 {
581 if (__begin_ > __first_)
582 {
583 difference_type __d = __begin_ - __first_;
584 __d = (__d + 1) / 2;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000585 __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000586 __begin_ -= __d;
587 }
588 else
589 {
Howard Hinnantec3773c2011-12-01 20:21:04 +0000590 size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000591 __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
592 __t.__construct_at_end(move_iterator<pointer>(__begin_),
593 move_iterator<pointer>(__end_));
Howard Hinnant0949eed2011-06-30 21:18:19 +0000594 _VSTD::swap(__first_, __t.__first_);
595 _VSTD::swap(__begin_, __t.__begin_);
596 _VSTD::swap(__end_, __t.__end_);
597 _VSTD::swap(__end_cap(), __t.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000598 }
599 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000600 __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__end_),
601 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000602 ++__end_;
603}
604
Howard Hinnant73d21a42010-09-04 23:28:19 +0000605#ifndef _LIBCPP_HAS_NO_VARIADICS
606
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000607template <class _Tp, class _Allocator>
608template <class... _Args>
609void
610__split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args)
611{
612 if (__end_ == __end_cap())
613 {
614 if (__begin_ > __first_)
615 {
616 difference_type __d = __begin_ - __first_;
617 __d = (__d + 1) / 2;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000618 __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000619 __begin_ -= __d;
620 }
621 else
622 {
Howard Hinnantec3773c2011-12-01 20:21:04 +0000623 size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000624 __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
625 __t.__construct_at_end(move_iterator<pointer>(__begin_),
626 move_iterator<pointer>(__end_));
Howard Hinnant0949eed2011-06-30 21:18:19 +0000627 _VSTD::swap(__first_, __t.__first_);
628 _VSTD::swap(__begin_, __t.__begin_);
629 _VSTD::swap(__end_, __t.__end_);
630 _VSTD::swap(__end_cap(), __t.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000631 }
632 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000633 __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__end_),
634 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000635 ++__end_;
636}
637
Howard Hinnant73d21a42010-09-04 23:28:19 +0000638#endif // _LIBCPP_HAS_NO_VARIADICS
639
640#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000641
Howard Hinnant0a612b02011-06-02 20:00:14 +0000642template <class _Tp, class _Allocator>
643_LIBCPP_INLINE_VISIBILITY inline
644void
645swap(__split_buffer<_Tp, _Allocator>& __x, __split_buffer<_Tp, _Allocator>& __y)
646 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
647{
648 __x.swap(__y);
649}
650
651
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000652_LIBCPP_END_NAMESPACE_STD
653
654#endif // _LIBCPP_SPLIT_BUFFER