blob: 79d1aa1d7c4a90cb03d8490be255634d323f87b2 [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
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +000059 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant009b2c42011-06-03 15:16:49 +000060 __split_buffer()
61 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +000062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000063 explicit __split_buffer(__alloc_rr& __a);
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +000064 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000065 explicit __split_buffer(const __alloc_rr& __a);
66 __split_buffer(size_type __cap, size_type __start, __alloc_rr& __a);
67 ~__split_buffer();
68
Howard Hinnant73d21a42010-09-04 23:28:19 +000069#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant0a612b02011-06-02 20:00:14 +000070 __split_buffer(__split_buffer&& __c)
71 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000072 __split_buffer(__split_buffer&& __c, const __alloc_rr& __a);
Howard Hinnant0a612b02011-06-02 20:00:14 +000073 __split_buffer& operator=(__split_buffer&& __c)
74 _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
75 is_nothrow_move_assignable<allocator_type>::value) ||
76 !__alloc_traits::propagate_on_container_move_assignment::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +000077#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000078
Howard Hinnant0a612b02011-06-02 20:00:14 +000079 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT {return __begin_;}
80 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT {return __begin_;}
81 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT {return __end_;}
82 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT {return __end_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000083
Howard Hinnant0a612b02011-06-02 20:00:14 +000084 _LIBCPP_INLINE_VISIBILITY
85 void clear() _NOEXCEPT
86 {__destruct_at_end(__begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000087 _LIBCPP_INLINE_VISIBILITY size_type size() const {return static_cast<size_type>(__end_ - __begin_);}
88 _LIBCPP_INLINE_VISIBILITY bool empty() const {return __end_ == __begin_;}
89 _LIBCPP_INLINE_VISIBILITY size_type capacity() const {return static_cast<size_type>(__end_cap() - __first_);}
90 _LIBCPP_INLINE_VISIBILITY size_type __front_spare() const {return static_cast<size_type>(__begin_ - __first_);}
91 _LIBCPP_INLINE_VISIBILITY size_type __back_spare() const {return static_cast<size_type>(__end_cap() - __end_);}
92
93 _LIBCPP_INLINE_VISIBILITY reference front() {return *__begin_;}
94 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return *__begin_;}
95 _LIBCPP_INLINE_VISIBILITY reference back() {return *(__end_ - 1);}
96 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return *(__end_ - 1);}
97
98 void reserve(size_type __n);
Howard Hinnant0a612b02011-06-02 20:00:14 +000099 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000100 void push_front(const_reference __x);
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000101 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Michael J. Spencer626916f2010-12-10 19:47:54 +0000102#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000103 void push_front(value_type&& __x);
104 void push_back(value_type&& __x);
Michael J. Spencer626916f2010-12-10 19:47:54 +0000105#if !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000106 template <class... _Args>
107 void emplace_back(_Args&&... __args);
Michael J. Spencer626916f2010-12-10 19:47:54 +0000108#endif // !defined(_LIBCPP_HAS_NO_VARIADICS)
109#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000110
111 _LIBCPP_INLINE_VISIBILITY void pop_front() {__destruct_at_begin(__begin_+1);}
112 _LIBCPP_INLINE_VISIBILITY void pop_back() {__destruct_at_end(__end_-1);}
113
114 void __construct_at_end(size_type __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000115 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000116 template <class _InputIter>
117 typename enable_if
118 <
119 __is_input_iterator<_InputIter>::value &&
120 !__is_forward_iterator<_InputIter>::value,
121 void
122 >::type
123 __construct_at_end(_InputIter __first, _InputIter __last);
124 template <class _ForwardIterator>
125 typename enable_if
126 <
127 __is_forward_iterator<_ForwardIterator>::value,
128 void
129 >::type
130 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
131
132 _LIBCPP_INLINE_VISIBILITY void __destruct_at_begin(pointer __new_begin)
Howard Hinnant1468b662010-11-19 22:17:28 +0000133 {__destruct_at_begin(__new_begin, is_trivially_destructible<value_type>());}
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000135 void __destruct_at_begin(pointer __new_begin, false_type);
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000136 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000137 void __destruct_at_begin(pointer __new_begin, true_type);
138
Howard Hinnant0a612b02011-06-02 20:00:14 +0000139 _LIBCPP_INLINE_VISIBILITY
140 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000141 {__destruct_at_end(__new_last, false_type());}
142 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0a612b02011-06-02 20:00:14 +0000143 void __destruct_at_end(pointer __new_last, false_type) _NOEXCEPT;
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000144 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0a612b02011-06-02 20:00:14 +0000145 void __destruct_at_end(pointer __new_last, true_type) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000146
Howard Hinnant0a612b02011-06-02 20:00:14 +0000147 void swap(__split_buffer& __x)
148 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
149 __is_nothrow_swappable<__alloc_rr>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000150
151 bool __invariants() const;
152
153private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000154 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +0000155 void __move_assign_alloc(__split_buffer& __c, true_type)
Howard Hinnant0a612b02011-06-02 20:00:14 +0000156 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000157 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000158 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000159 }
160
Howard Hinnant333f50d2010-09-21 20:16:37 +0000161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +0000162 void __move_assign_alloc(__split_buffer&, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000163 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000164};
165
166template <class _Tp, class _Allocator>
167bool
168__split_buffer<_Tp, _Allocator>::__invariants() const
169{
170 if (__first_ == nullptr)
171 {
172 if (__begin_ != nullptr)
173 return false;
174 if (__end_ != nullptr)
175 return false;
176 if (__end_cap() != nullptr)
177 return false;
178 }
179 else
180 {
181 if (__begin_ < __first_)
182 return false;
183 if (__end_ < __begin_)
184 return false;
185 if (__end_cap() < __end_)
186 return false;
187 }
188 return true;
189}
190
191// Default constructs __n objects starting at __end_
192// throws if construction throws
193// Precondition: __n > 0
194// Precondition: size() + __n <= capacity()
195// Postcondition: size() == size() + __n
196template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000197void
198__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n)
199{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000200 __alloc_rr& __a = this->__alloc();
201 do
202 {
Howard Hinnant5f255942011-08-28 15:21:29 +0000203 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000204 ++this->__end_;
205 --__n;
206 } while (__n > 0);
207}
208
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000209// Copy constructs __n objects starting at __end_ from __x
210// throws if construction throws
211// Precondition: __n > 0
212// Precondition: size() + __n <= capacity()
213// Postcondition: size() == old size() + __n
214// Postcondition: [i] == __x for all i in [size() - __n, __n)
215template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000216void
217__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
218{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000219 __alloc_rr& __a = this->__alloc();
220 do
221 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000222 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000223 ++this->__end_;
224 --__n;
225 } while (__n > 0);
226}
227
228template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000229template <class _InputIter>
230typename enable_if
231<
232 __is_input_iterator<_InputIter>::value &&
233 !__is_forward_iterator<_InputIter>::value,
234 void
235>::type
236__split_buffer<_Tp, _Allocator>::__construct_at_end(_InputIter __first, _InputIter __last)
237{
238 __alloc_rr& __a = this->__alloc();
239 for (; __first != __last; ++__first)
240 {
241 if (__end_ == __end_cap())
242 {
243 size_type __old_cap = __end_cap() - __first_;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000244 size_type __new_cap = _VSTD::max<size_type>(2 * __old_cap, 8);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000245 __split_buffer __buf(__new_cap, 0, __a);
246 for (pointer __p = __begin_; __p != __end_; ++__p, ++__buf.__end_)
247 __alloc_traits::construct(__buf.__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:19 +0000248 _VSTD::__to_raw_pointer(__buf.__end_), _VSTD::move(*__p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000249 swap(__buf);
250 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000251 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000252 ++this->__end_;
253 }
254}
255
256template <class _Tp, class _Allocator>
257template <class _ForwardIterator>
258typename enable_if
259<
260 __is_forward_iterator<_ForwardIterator>::value,
261 void
262>::type
263__split_buffer<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
264{
265 __alloc_rr& __a = this->__alloc();
266 for (; __first != __last; ++__first)
267 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000268 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000269 ++this->__end_;
270 }
271}
272
273template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000274inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000275void
276__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type)
277{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000278 while (__begin_ != __new_begin)
Howard Hinnantfcd8db72013-06-23 21:17:24 +0000279 __alloc_traits::destroy(__alloc(), __to_raw_pointer(__begin_++));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000280}
281
282template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000283inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000284void
285__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, true_type)
286{
287 __begin_ = __new_begin;
288}
289
290template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000291inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000292void
Howard Hinnant0a612b02011-06-02 20:00:14 +0000293__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000294{
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000295 while (__new_last != __end_)
Howard Hinnantfcd8db72013-06-23 21:17:24 +0000296 __alloc_traits::destroy(__alloc(), __to_raw_pointer(--__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000297}
298
299template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000300inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000301void
Howard Hinnant0a612b02011-06-02 20:00:14 +0000302__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000303{
304 __end_ = __new_last;
305}
306
307template <class _Tp, class _Allocator>
308__split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __start, __alloc_rr& __a)
Howard Hinnantfcd8db72013-06-23 21:17:24 +0000309 : __end_cap_(nullptr, __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000310{
311 __first_ = __cap != 0 ? __alloc_traits::allocate(__alloc(), __cap) : nullptr;
312 __begin_ = __end_ = __first_ + __start;
313 __end_cap() = __first_ + __cap;
314}
315
316template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000317inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000318__split_buffer<_Tp, _Allocator>::__split_buffer()
Howard Hinnant009b2c42011-06-03 15:16:49 +0000319 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantfcd8db72013-06-23 21:17:24 +0000320 : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000321{
322}
323
324template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000325inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000326__split_buffer<_Tp, _Allocator>::__split_buffer(__alloc_rr& __a)
Howard Hinnantfcd8db72013-06-23 21:17:24 +0000327 : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000328{
329}
330
331template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000332inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000333__split_buffer<_Tp, _Allocator>::__split_buffer(const __alloc_rr& __a)
Howard Hinnantfcd8db72013-06-23 21:17:24 +0000334 : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000335{
336}
337
338template <class _Tp, class _Allocator>
339__split_buffer<_Tp, _Allocator>::~__split_buffer()
340{
341 clear();
342 if (__first_)
343 __alloc_traits::deallocate(__alloc(), __first_, capacity());
344}
345
Howard Hinnant73d21a42010-09-04 23:28:19 +0000346#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000347
348template <class _Tp, class _Allocator>
349__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c)
Howard Hinnant0a612b02011-06-02 20:00:14 +0000350 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000351 : __first_(_VSTD::move(__c.__first_)),
352 __begin_(_VSTD::move(__c.__begin_)),
353 __end_(_VSTD::move(__c.__end_)),
354 __end_cap_(_VSTD::move(__c.__end_cap_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000355{
356 __c.__first_ = nullptr;
357 __c.__begin_ = nullptr;
358 __c.__end_ = nullptr;
359 __c.__end_cap() = nullptr;
360}
361
362template <class _Tp, class _Allocator>
363__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a)
364 : __end_cap_(__a)
365{
366 if (__a == __c.__alloc())
367 {
368 __first_ = __c.__first_;
369 __begin_ = __c.__begin_;
370 __end_ = __c.__end_;
371 __end_cap() = __c.__end_cap();
372 __c.__first_ = nullptr;
373 __c.__begin_ = nullptr;
374 __c.__end_ = nullptr;
375 __c.__end_cap() = nullptr;
376 }
377 else
378 {
379 size_type __cap = __c.size();
380 __first_ = __alloc_traits::allocate(__alloc(), __cap);
381 __begin_ = __end_ = __first_;
382 __end_cap() = __first_ + __cap;
Howard Hinnant99968442011-11-29 18:15:50 +0000383 typedef move_iterator<iterator> _Ip;
384 __construct_at_end(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000385 }
386}
387
388template <class _Tp, class _Allocator>
389__split_buffer<_Tp, _Allocator>&
390__split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c)
Howard Hinnant0a612b02011-06-02 20:00:14 +0000391 _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
392 is_nothrow_move_assignable<allocator_type>::value) ||
393 !__alloc_traits::propagate_on_container_move_assignment::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000394{
395 clear();
396 shrink_to_fit();
397 __first_ = __c.__first_;
398 __begin_ = __c.__begin_;
399 __end_ = __c.__end_;
400 __end_cap() = __c.__end_cap();
401 __move_assign_alloc(__c,
402 integral_constant<bool,
403 __alloc_traits::propagate_on_container_move_assignment::value>());
404 __c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
405 return *this;
406}
407
Howard Hinnant73d21a42010-09-04 23:28:19 +0000408#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000409
410template <class _Tp, class _Allocator>
411void
412__split_buffer<_Tp, _Allocator>::swap(__split_buffer& __x)
Howard Hinnant0a612b02011-06-02 20:00:14 +0000413 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
414 __is_nothrow_swappable<__alloc_rr>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000415{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000416 _VSTD::swap(__first_, __x.__first_);
417 _VSTD::swap(__begin_, __x.__begin_);
418 _VSTD::swap(__end_, __x.__end_);
419 _VSTD::swap(__end_cap(), __x.__end_cap());
Marshall Clow7d914d12015-07-13 20:04:56 +0000420 __swap_allocator(__alloc(), __x.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000421}
422
423template <class _Tp, class _Allocator>
424void
425__split_buffer<_Tp, _Allocator>::reserve(size_type __n)
426{
427 if (__n < capacity())
428 {
429 __split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc());
430 __t.__construct_at_end(move_iterator<pointer>(__begin_),
431 move_iterator<pointer>(__end_));
Howard Hinnant0949eed2011-06-30 21:18:19 +0000432 _VSTD::swap(__first_, __t.__first_);
433 _VSTD::swap(__begin_, __t.__begin_);
434 _VSTD::swap(__end_, __t.__end_);
435 _VSTD::swap(__end_cap(), __t.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000436 }
437}
438
439template <class _Tp, class _Allocator>
440void
Howard Hinnant0a612b02011-06-02 20:00:14 +0000441__split_buffer<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000442{
443 if (capacity() > size())
444 {
445#ifndef _LIBCPP_NO_EXCEPTIONS
446 try
447 {
Howard Hinnant324bb032010-08-22 00:02:43 +0000448#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000449 __split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc());
450 __t.__construct_at_end(move_iterator<pointer>(__begin_),
451 move_iterator<pointer>(__end_));
452 __t.__end_ = __t.__begin_ + (__end_ - __begin_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000453 _VSTD::swap(__first_, __t.__first_);
454 _VSTD::swap(__begin_, __t.__begin_);
455 _VSTD::swap(__end_, __t.__end_);
456 _VSTD::swap(__end_cap(), __t.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000457#ifndef _LIBCPP_NO_EXCEPTIONS
458 }
459 catch (...)
460 {
461 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000462#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000463 }
464}
465
466template <class _Tp, class _Allocator>
467void
468__split_buffer<_Tp, _Allocator>::push_front(const_reference __x)
469{
470 if (__begin_ == __first_)
471 {
472 if (__end_ < __end_cap())
473 {
474 difference_type __d = __end_cap() - __end_;
475 __d = (__d + 1) / 2;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000476 __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000477 __end_ += __d;
478 }
479 else
480 {
Howard Hinnantec3773c2011-12-01 20:21:04 +0000481 size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
Howard Hinnantf8ce4592010-07-07 19:14:52 +0000482 __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000483 __t.__construct_at_end(move_iterator<pointer>(__begin_),
484 move_iterator<pointer>(__end_));
Howard Hinnant0949eed2011-06-30 21:18:19 +0000485 _VSTD::swap(__first_, __t.__first_);
486 _VSTD::swap(__begin_, __t.__begin_);
487 _VSTD::swap(__end_, __t.__end_);
488 _VSTD::swap(__end_cap(), __t.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000489 }
490 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000491 __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__begin_-1), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000492 --__begin_;
493}
494
Howard Hinnant73d21a42010-09-04 23:28:19 +0000495#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000496
497template <class _Tp, class _Allocator>
498void
499__split_buffer<_Tp, _Allocator>::push_front(value_type&& __x)
500{
501 if (__begin_ == __first_)
502 {
503 if (__end_ < __end_cap())
504 {
505 difference_type __d = __end_cap() - __end_;
506 __d = (__d + 1) / 2;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000507 __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000508 __end_ += __d;
509 }
510 else
511 {
Howard Hinnantec3773c2011-12-01 20:21:04 +0000512 size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
Howard Hinnantf8ce4592010-07-07 19:14:52 +0000513 __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000514 __t.__construct_at_end(move_iterator<pointer>(__begin_),
515 move_iterator<pointer>(__end_));
Howard Hinnant0949eed2011-06-30 21:18:19 +0000516 _VSTD::swap(__first_, __t.__first_);
517 _VSTD::swap(__begin_, __t.__begin_);
518 _VSTD::swap(__end_, __t.__end_);
519 _VSTD::swap(__end_cap(), __t.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000520 }
521 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000522 __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__begin_-1),
523 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000524 --__begin_;
525}
526
Howard Hinnant73d21a42010-09-04 23:28:19 +0000527#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000528
529template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000530inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000531void
532__split_buffer<_Tp, _Allocator>::push_back(const_reference __x)
533{
534 if (__end_ == __end_cap())
535 {
536 if (__begin_ > __first_)
537 {
538 difference_type __d = __begin_ - __first_;
539 __d = (__d + 1) / 2;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000540 __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000541 __begin_ -= __d;
542 }
543 else
544 {
Howard Hinnantec3773c2011-12-01 20:21:04 +0000545 size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000546 __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
547 __t.__construct_at_end(move_iterator<pointer>(__begin_),
548 move_iterator<pointer>(__end_));
Howard Hinnant0949eed2011-06-30 21:18:19 +0000549 _VSTD::swap(__first_, __t.__first_);
550 _VSTD::swap(__begin_, __t.__begin_);
551 _VSTD::swap(__end_, __t.__end_);
552 _VSTD::swap(__end_cap(), __t.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000553 }
554 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000555 __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000556 ++__end_;
557}
558
Howard Hinnant73d21a42010-09-04 23:28:19 +0000559#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000560
561template <class _Tp, class _Allocator>
562void
563__split_buffer<_Tp, _Allocator>::push_back(value_type&& __x)
564{
565 if (__end_ == __end_cap())
566 {
567 if (__begin_ > __first_)
568 {
569 difference_type __d = __begin_ - __first_;
570 __d = (__d + 1) / 2;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000571 __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000572 __begin_ -= __d;
573 }
574 else
575 {
Howard Hinnantec3773c2011-12-01 20:21:04 +0000576 size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000577 __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
578 __t.__construct_at_end(move_iterator<pointer>(__begin_),
579 move_iterator<pointer>(__end_));
Howard Hinnant0949eed2011-06-30 21:18:19 +0000580 _VSTD::swap(__first_, __t.__first_);
581 _VSTD::swap(__begin_, __t.__begin_);
582 _VSTD::swap(__end_, __t.__end_);
583 _VSTD::swap(__end_cap(), __t.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000584 }
585 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000586 __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__end_),
587 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000588 ++__end_;
589}
590
Howard Hinnant73d21a42010-09-04 23:28:19 +0000591#ifndef _LIBCPP_HAS_NO_VARIADICS
592
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000593template <class _Tp, class _Allocator>
594template <class... _Args>
595void
596__split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args)
597{
598 if (__end_ == __end_cap())
599 {
600 if (__begin_ > __first_)
601 {
602 difference_type __d = __begin_ - __first_;
603 __d = (__d + 1) / 2;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000604 __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000605 __begin_ -= __d;
606 }
607 else
608 {
Howard Hinnantec3773c2011-12-01 20:21:04 +0000609 size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000610 __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
611 __t.__construct_at_end(move_iterator<pointer>(__begin_),
612 move_iterator<pointer>(__end_));
Howard Hinnant0949eed2011-06-30 21:18:19 +0000613 _VSTD::swap(__first_, __t.__first_);
614 _VSTD::swap(__begin_, __t.__begin_);
615 _VSTD::swap(__end_, __t.__end_);
616 _VSTD::swap(__end_cap(), __t.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000617 }
618 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000619 __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__end_),
620 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000621 ++__end_;
622}
623
Howard Hinnant73d21a42010-09-04 23:28:19 +0000624#endif // _LIBCPP_HAS_NO_VARIADICS
625
626#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000627
Howard Hinnant0a612b02011-06-02 20:00:14 +0000628template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +0000629inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0a612b02011-06-02 20:00:14 +0000630void
631swap(__split_buffer<_Tp, _Allocator>& __x, __split_buffer<_Tp, _Allocator>& __y)
632 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
633{
634 __x.swap(__y);
635}
636
637
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000638_LIBCPP_END_NAMESPACE_STD
639
640#endif // _LIBCPP_SPLIT_BUFFER