blob: d70c0a7f16678a2e9f994445e1c8ca7e3b05cd71 [file] [log] [blame]
Howard Hinnant3e519522010-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 Hinnant073458b2011-10-17 20:05:10 +00009#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant3e519522010-05-11 19:42:16 +000010#pragma GCC system_header
Howard Hinnant073458b2011-10-17 20:05:10 +000011#endif
Howard Hinnant3e519522010-05-11 19:42:16 +000012
Eric Fiseliera016efb2017-05-31 22:07:49 +000013_LIBCPP_PUSH_MACROS
14#include <__undef_macros>
15
16
Howard Hinnant3e519522010-05-11 19:42:16 +000017_LIBCPP_BEGIN_NAMESPACE_STD
18
19template <bool>
20class __split_buffer_common
21{
22protected:
23 void __throw_length_error() const;
24 void __throw_out_of_range() const;
25};
26
Howard Hinnant189b2122010-07-07 19:14:52 +000027template <class _Tp, class _Allocator = allocator<_Tp> >
Howard Hinnant3e519522010-05-11 19:42:16 +000028struct __split_buffer
29 : private __split_buffer_common<true>
30{
31private:
32 __split_buffer(const __split_buffer&);
33 __split_buffer& operator=(const __split_buffer&);
34public:
Howard Hinnantb3371f62010-08-22 00:02:43 +000035 typedef _Tp value_type;
Howard Hinnant3e519522010-05-11 19:42:16 +000036 typedef _Allocator allocator_type;
37 typedef typename remove_reference<allocator_type>::type __alloc_rr;
38 typedef allocator_traits<__alloc_rr> __alloc_traits;
39 typedef value_type& reference;
40 typedef const value_type& const_reference;
41 typedef typename __alloc_traits::size_type size_type;
42 typedef typename __alloc_traits::difference_type difference_type;
43 typedef typename __alloc_traits::pointer pointer;
44 typedef typename __alloc_traits::const_pointer const_pointer;
45 typedef pointer iterator;
46 typedef const_pointer const_iterator;
47
48 pointer __first_;
49 pointer __begin_;
50 pointer __end_;
51 __compressed_pair<pointer, allocator_type> __end_cap_;
52
53 typedef typename add_lvalue_reference<allocator_type>::type __alloc_ref;
54 typedef typename add_lvalue_reference<allocator_type>::type __alloc_const_ref;
55
Howard Hinnant9eebe112011-06-02 20:00:14 +000056 _LIBCPP_INLINE_VISIBILITY __alloc_rr& __alloc() _NOEXCEPT {return __end_cap_.second();}
57 _LIBCPP_INLINE_VISIBILITY const __alloc_rr& __alloc() const _NOEXCEPT {return __end_cap_.second();}
58 _LIBCPP_INLINE_VISIBILITY pointer& __end_cap() _NOEXCEPT {return __end_cap_.first();}
59 _LIBCPP_INLINE_VISIBILITY const pointer& __end_cap() const _NOEXCEPT {return __end_cap_.first();}
Howard Hinnant3e519522010-05-11 19:42:16 +000060
Evgeniy Stepanov906c8722015-11-07 01:22:13 +000061 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant80129112011-06-03 15:16:49 +000062 __split_buffer()
63 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Evgeniy Stepanov906c8722015-11-07 01:22:13 +000064 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +000065 explicit __split_buffer(__alloc_rr& __a);
Evgeniy Stepanov906c8722015-11-07 01:22:13 +000066 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +000067 explicit __split_buffer(const __alloc_rr& __a);
68 __split_buffer(size_type __cap, size_type __start, __alloc_rr& __a);
69 ~__split_buffer();
70
Eric Fiselierafa7a952017-04-19 01:23:04 +000071#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant9eebe112011-06-02 20:00:14 +000072 __split_buffer(__split_buffer&& __c)
73 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +000074 __split_buffer(__split_buffer&& __c, const __alloc_rr& __a);
Howard Hinnant9eebe112011-06-02 20:00:14 +000075 __split_buffer& operator=(__split_buffer&& __c)
76 _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
77 is_nothrow_move_assignable<allocator_type>::value) ||
78 !__alloc_traits::propagate_on_container_move_assignment::value);
Eric Fiselierafa7a952017-04-19 01:23:04 +000079#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +000080
Howard Hinnant9eebe112011-06-02 20:00:14 +000081 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT {return __begin_;}
82 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT {return __begin_;}
83 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT {return __end_;}
84 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT {return __end_;}
Howard Hinnant3e519522010-05-11 19:42:16 +000085
Howard Hinnant9eebe112011-06-02 20:00:14 +000086 _LIBCPP_INLINE_VISIBILITY
87 void clear() _NOEXCEPT
88 {__destruct_at_end(__begin_);}
Howard Hinnant3e519522010-05-11 19:42:16 +000089 _LIBCPP_INLINE_VISIBILITY size_type size() const {return static_cast<size_type>(__end_ - __begin_);}
90 _LIBCPP_INLINE_VISIBILITY bool empty() const {return __end_ == __begin_;}
91 _LIBCPP_INLINE_VISIBILITY size_type capacity() const {return static_cast<size_type>(__end_cap() - __first_);}
92 _LIBCPP_INLINE_VISIBILITY size_type __front_spare() const {return static_cast<size_type>(__begin_ - __first_);}
93 _LIBCPP_INLINE_VISIBILITY size_type __back_spare() const {return static_cast<size_type>(__end_cap() - __end_);}
94
95 _LIBCPP_INLINE_VISIBILITY reference front() {return *__begin_;}
96 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return *__begin_;}
97 _LIBCPP_INLINE_VISIBILITY reference back() {return *(__end_ - 1);}
98 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return *(__end_ - 1);}
99
100 void reserve(size_type __n);
Howard Hinnant9eebe112011-06-02 20:00:14 +0000101 void shrink_to_fit() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000102 void push_front(const_reference __x);
Howard Hinnant9741d6c92012-02-15 00:41:34 +0000103 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Eric Fiselierafa7a952017-04-19 01:23:04 +0000104#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +0000105 void push_front(value_type&& __x);
106 void push_back(value_type&& __x);
107 template <class... _Args>
108 void emplace_back(_Args&&... __args);
Eric Fiselierafa7a952017-04-19 01:23:04 +0000109#endif // !defined(_LIBCPP_CXX03_LANG)
Howard Hinnant3e519522010-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 Hinnant3e519522010-05-11 19:42:16 +0000115 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnant3e519522010-05-11 19:42:16 +0000116 template <class _InputIter>
117 typename enable_if
118 <
Eric Fiselierf82dba02019-11-18 01:46:58 -0500119 __is_cpp17_input_iterator<_InputIter>::value &&
120 !__is_cpp17_forward_iterator<_InputIter>::value,
Howard Hinnant3e519522010-05-11 19:42:16 +0000121 void
122 >::type
123 __construct_at_end(_InputIter __first, _InputIter __last);
124 template <class _ForwardIterator>
125 typename enable_if
126 <
Eric Fiselierf82dba02019-11-18 01:46:58 -0500127 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnant3e519522010-05-11 19:42:16 +0000128 void
129 >::type
130 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
131
132 _LIBCPP_INLINE_VISIBILITY void __destruct_at_begin(pointer __new_begin)
Howard Hinnantca740482010-11-19 22:17:28 +0000133 {__destruct_at_begin(__new_begin, is_trivially_destructible<value_type>());}
Evgeniy Stepanov906c8722015-11-07 01:22:13 +0000134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000135 void __destruct_at_begin(pointer __new_begin, false_type);
Evgeniy Stepanov906c8722015-11-07 01:22:13 +0000136 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000137 void __destruct_at_begin(pointer __new_begin, true_type);
138
Howard Hinnant9eebe112011-06-02 20:00:14 +0000139 _LIBCPP_INLINE_VISIBILITY
140 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant9741d6c92012-02-15 00:41:34 +0000141 {__destruct_at_end(__new_last, false_type());}
142 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9eebe112011-06-02 20:00:14 +0000143 void __destruct_at_end(pointer __new_last, false_type) _NOEXCEPT;
Howard Hinnant9741d6c92012-02-15 00:41:34 +0000144 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9eebe112011-06-02 20:00:14 +0000145 void __destruct_at_end(pointer __new_last, true_type) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000146
Howard Hinnant9eebe112011-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 Hinnant3e519522010-05-11 19:42:16 +0000150
151 bool __invariants() const;
152
153private:
Howard Hinnantf5ab7032010-09-21 20:16:37 +0000154 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86681392011-09-02 20:42:31 +0000155 void __move_assign_alloc(__split_buffer& __c, true_type)
Howard Hinnant9eebe112011-06-02 20:00:14 +0000156 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000157 {
Howard Hinnantce48a112011-06-30 21:18:19 +0000158 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnant3e519522010-05-11 19:42:16 +0000159 }
160
Howard Hinnantf5ab7032010-09-21 20:16:37 +0000161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2063662011-12-01 20:21:04 +0000162 void __move_assign_alloc(__split_buffer&, false_type) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000163 {}
Eric Fiselierb0945e12019-08-12 07:51:05 +0000164
165 struct _ConstructTransaction {
166 explicit _ConstructTransaction(pointer* __p, size_type __n) _NOEXCEPT
167 : __pos_(*__p), __end_(*__p + __n), __dest_(__p) {
168 }
169 ~_ConstructTransaction() {
170 *__dest_ = __pos_;
171 }
172 pointer __pos_;
173 const pointer __end_;
174 private:
175 pointer *__dest_;
176 };
Howard Hinnant3e519522010-05-11 19:42:16 +0000177};
178
179template <class _Tp, class _Allocator>
180bool
181__split_buffer<_Tp, _Allocator>::__invariants() const
182{
183 if (__first_ == nullptr)
184 {
185 if (__begin_ != nullptr)
186 return false;
187 if (__end_ != nullptr)
188 return false;
189 if (__end_cap() != nullptr)
190 return false;
191 }
192 else
193 {
194 if (__begin_ < __first_)
195 return false;
196 if (__end_ < __begin_)
197 return false;
198 if (__end_cap() < __end_)
199 return false;
200 }
201 return true;
202}
203
204// Default constructs __n objects starting at __end_
205// throws if construction throws
206// Precondition: __n > 0
207// Precondition: size() + __n <= capacity()
208// Postcondition: size() == size() + __n
209template <class _Tp, class _Allocator>
Howard Hinnant3e519522010-05-11 19:42:16 +0000210void
211__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n)
212{
Eric Fiselierb0945e12019-08-12 07:51:05 +0000213 _ConstructTransaction __tx(&this->__end_, __n);
214 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
Eric Fiselier0068c592019-11-16 17:13:26 -0500215 __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__tx.__pos_));
Eric Fiselierb0945e12019-08-12 07:51:05 +0000216 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000217}
218
Howard Hinnant3e519522010-05-11 19:42:16 +0000219// Copy constructs __n objects starting at __end_ from __x
220// throws if construction throws
221// Precondition: __n > 0
222// Precondition: size() + __n <= capacity()
223// Postcondition: size() == old size() + __n
224// Postcondition: [i] == __x for all i in [size() - __n, __n)
225template <class _Tp, class _Allocator>
Howard Hinnant3e519522010-05-11 19:42:16 +0000226void
227__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
228{
Eric Fiselierb0945e12019-08-12 07:51:05 +0000229 _ConstructTransaction __tx(&this->__end_, __n);
230 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
231 __alloc_traits::construct(this->__alloc(),
Eric Fiselier0068c592019-11-16 17:13:26 -0500232 _VSTD::__to_address(__tx.__pos_), __x);
Eric Fiselierb0945e12019-08-12 07:51:05 +0000233 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000234}
235
236template <class _Tp, class _Allocator>
Howard Hinnant3e519522010-05-11 19:42:16 +0000237template <class _InputIter>
238typename enable_if
239<
Eric Fiselierf82dba02019-11-18 01:46:58 -0500240 __is_cpp17_input_iterator<_InputIter>::value &&
241 !__is_cpp17_forward_iterator<_InputIter>::value,
Howard Hinnant3e519522010-05-11 19:42:16 +0000242 void
243>::type
244__split_buffer<_Tp, _Allocator>::__construct_at_end(_InputIter __first, _InputIter __last)
245{
246 __alloc_rr& __a = this->__alloc();
247 for (; __first != __last; ++__first)
248 {
249 if (__end_ == __end_cap())
250 {
251 size_type __old_cap = __end_cap() - __first_;
Howard Hinnantce48a112011-06-30 21:18:19 +0000252 size_type __new_cap = _VSTD::max<size_type>(2 * __old_cap, 8);
Howard Hinnant3e519522010-05-11 19:42:16 +0000253 __split_buffer __buf(__new_cap, 0, __a);
254 for (pointer __p = __begin_; __p != __end_; ++__p, ++__buf.__end_)
255 __alloc_traits::construct(__buf.__alloc(),
Eric Fiselier0068c592019-11-16 17:13:26 -0500256 _VSTD::__to_address(__buf.__end_), _VSTD::move(*__p));
Howard Hinnant3e519522010-05-11 19:42:16 +0000257 swap(__buf);
258 }
Eric Fiselier0068c592019-11-16 17:13:26 -0500259 __alloc_traits::construct(__a, _VSTD::__to_address(this->__end_), *__first);
Howard Hinnant3e519522010-05-11 19:42:16 +0000260 ++this->__end_;
261 }
262}
263
264template <class _Tp, class _Allocator>
265template <class _ForwardIterator>
266typename enable_if
267<
Eric Fiselierf82dba02019-11-18 01:46:58 -0500268 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnant3e519522010-05-11 19:42:16 +0000269 void
270>::type
271__split_buffer<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
272{
Eric Fiselierb0945e12019-08-12 07:51:05 +0000273 _ConstructTransaction __tx(&this->__end_, std::distance(__first, __last));
274 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, ++__first) {
275 __alloc_traits::construct(this->__alloc(),
Eric Fiselier0068c592019-11-16 17:13:26 -0500276 _VSTD::__to_address(__tx.__pos_), *__first);
Howard Hinnant3e519522010-05-11 19:42:16 +0000277 }
278}
279
280template <class _Tp, class _Allocator>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +0000281inline
Howard Hinnant3e519522010-05-11 19:42:16 +0000282void
283__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type)
284{
Howard Hinnant9741d6c92012-02-15 00:41:34 +0000285 while (__begin_ != __new_begin)
Eric Fiselier0068c592019-11-16 17:13:26 -0500286 __alloc_traits::destroy(__alloc(), __to_address(__begin_++));
Howard Hinnant3e519522010-05-11 19:42:16 +0000287}
288
289template <class _Tp, class _Allocator>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +0000290inline
Howard Hinnant3e519522010-05-11 19:42:16 +0000291void
292__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, true_type)
293{
294 __begin_ = __new_begin;
295}
296
297template <class _Tp, class _Allocator>
Howard Hinnant3af48ef2013-10-04 22:09:00 +0000298inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000299void
Howard Hinnant9eebe112011-06-02 20:00:14 +0000300__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000301{
Howard Hinnant9741d6c92012-02-15 00:41:34 +0000302 while (__new_last != __end_)
Eric Fiselier0068c592019-11-16 17:13:26 -0500303 __alloc_traits::destroy(__alloc(), __to_address(--__end_));
Howard Hinnant3e519522010-05-11 19:42:16 +0000304}
305
306template <class _Tp, class _Allocator>
Howard Hinnant3af48ef2013-10-04 22:09:00 +0000307inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000308void
Howard Hinnant9eebe112011-06-02 20:00:14 +0000309__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, true_type) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000310{
311 __end_ = __new_last;
312}
313
314template <class _Tp, class _Allocator>
315__split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __start, __alloc_rr& __a)
Howard Hinnant14e200d2013-06-23 21:17:24 +0000316 : __end_cap_(nullptr, __a)
Howard Hinnant3e519522010-05-11 19:42:16 +0000317{
318 __first_ = __cap != 0 ? __alloc_traits::allocate(__alloc(), __cap) : nullptr;
319 __begin_ = __end_ = __first_ + __start;
320 __end_cap() = __first_ + __cap;
321}
322
323template <class _Tp, class _Allocator>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +0000324inline
Howard Hinnant3e519522010-05-11 19:42:16 +0000325__split_buffer<_Tp, _Allocator>::__split_buffer()
Howard Hinnant80129112011-06-03 15:16:49 +0000326 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant14e200d2013-06-23 21:17:24 +0000327 : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr)
Howard Hinnant3e519522010-05-11 19:42:16 +0000328{
329}
330
331template <class _Tp, class _Allocator>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +0000332inline
Howard Hinnant3e519522010-05-11 19:42:16 +0000333__split_buffer<_Tp, _Allocator>::__split_buffer(__alloc_rr& __a)
Howard Hinnant14e200d2013-06-23 21:17:24 +0000334 : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a)
Howard Hinnant3e519522010-05-11 19:42:16 +0000335{
336}
337
338template <class _Tp, class _Allocator>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +0000339inline
Howard Hinnant3e519522010-05-11 19:42:16 +0000340__split_buffer<_Tp, _Allocator>::__split_buffer(const __alloc_rr& __a)
Howard Hinnant14e200d2013-06-23 21:17:24 +0000341 : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a)
Howard Hinnant3e519522010-05-11 19:42:16 +0000342{
343}
344
345template <class _Tp, class _Allocator>
346__split_buffer<_Tp, _Allocator>::~__split_buffer()
347{
348 clear();
349 if (__first_)
350 __alloc_traits::deallocate(__alloc(), __first_, capacity());
351}
352
Eric Fiselierafa7a952017-04-19 01:23:04 +0000353#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +0000354
355template <class _Tp, class _Allocator>
356__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c)
Howard Hinnant9eebe112011-06-02 20:00:14 +0000357 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantce48a112011-06-30 21:18:19 +0000358 : __first_(_VSTD::move(__c.__first_)),
359 __begin_(_VSTD::move(__c.__begin_)),
360 __end_(_VSTD::move(__c.__end_)),
361 __end_cap_(_VSTD::move(__c.__end_cap_))
Howard Hinnant3e519522010-05-11 19:42:16 +0000362{
363 __c.__first_ = nullptr;
364 __c.__begin_ = nullptr;
365 __c.__end_ = nullptr;
366 __c.__end_cap() = nullptr;
367}
368
369template <class _Tp, class _Allocator>
370__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a)
Eric Fiseliere1ec2982017-04-13 00:34:24 +0000371 : __end_cap_(__second_tag(), __a)
Howard Hinnant3e519522010-05-11 19:42:16 +0000372{
373 if (__a == __c.__alloc())
374 {
375 __first_ = __c.__first_;
376 __begin_ = __c.__begin_;
377 __end_ = __c.__end_;
378 __end_cap() = __c.__end_cap();
379 __c.__first_ = nullptr;
380 __c.__begin_ = nullptr;
381 __c.__end_ = nullptr;
382 __c.__end_cap() = nullptr;
383 }
384 else
385 {
386 size_type __cap = __c.size();
387 __first_ = __alloc_traits::allocate(__alloc(), __cap);
388 __begin_ = __end_ = __first_;
389 __end_cap() = __first_ + __cap;
Howard Hinnantc003db12011-11-29 18:15:50 +0000390 typedef move_iterator<iterator> _Ip;
391 __construct_at_end(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnant3e519522010-05-11 19:42:16 +0000392 }
393}
394
395template <class _Tp, class _Allocator>
396__split_buffer<_Tp, _Allocator>&
397__split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c)
Howard Hinnant9eebe112011-06-02 20:00:14 +0000398 _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
399 is_nothrow_move_assignable<allocator_type>::value) ||
400 !__alloc_traits::propagate_on_container_move_assignment::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000401{
402 clear();
403 shrink_to_fit();
404 __first_ = __c.__first_;
405 __begin_ = __c.__begin_;
406 __end_ = __c.__end_;
407 __end_cap() = __c.__end_cap();
408 __move_assign_alloc(__c,
409 integral_constant<bool,
410 __alloc_traits::propagate_on_container_move_assignment::value>());
411 __c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
412 return *this;
413}
414
Eric Fiselierafa7a952017-04-19 01:23:04 +0000415#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +0000416
417template <class _Tp, class _Allocator>
418void
419__split_buffer<_Tp, _Allocator>::swap(__split_buffer& __x)
Howard Hinnant9eebe112011-06-02 20:00:14 +0000420 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
421 __is_nothrow_swappable<__alloc_rr>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000422{
Howard Hinnantce48a112011-06-30 21:18:19 +0000423 _VSTD::swap(__first_, __x.__first_);
424 _VSTD::swap(__begin_, __x.__begin_);
425 _VSTD::swap(__end_, __x.__end_);
426 _VSTD::swap(__end_cap(), __x.__end_cap());
Marshall Clowe3fbe142015-07-13 20:04:56 +0000427 __swap_allocator(__alloc(), __x.__alloc());
Howard Hinnant3e519522010-05-11 19:42:16 +0000428}
429
430template <class _Tp, class _Allocator>
431void
432__split_buffer<_Tp, _Allocator>::reserve(size_type __n)
433{
434 if (__n < capacity())
435 {
436 __split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc());
437 __t.__construct_at_end(move_iterator<pointer>(__begin_),
438 move_iterator<pointer>(__end_));
Howard Hinnantce48a112011-06-30 21:18:19 +0000439 _VSTD::swap(__first_, __t.__first_);
440 _VSTD::swap(__begin_, __t.__begin_);
441 _VSTD::swap(__end_, __t.__end_);
442 _VSTD::swap(__end_cap(), __t.__end_cap());
Howard Hinnant3e519522010-05-11 19:42:16 +0000443 }
444}
445
446template <class _Tp, class _Allocator>
447void
Howard Hinnant9eebe112011-06-02 20:00:14 +0000448__split_buffer<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000449{
450 if (capacity() > size())
451 {
452#ifndef _LIBCPP_NO_EXCEPTIONS
453 try
454 {
Howard Hinnantb3371f62010-08-22 00:02:43 +0000455#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +0000456 __split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc());
457 __t.__construct_at_end(move_iterator<pointer>(__begin_),
458 move_iterator<pointer>(__end_));
459 __t.__end_ = __t.__begin_ + (__end_ - __begin_);
Howard Hinnantce48a112011-06-30 21:18:19 +0000460 _VSTD::swap(__first_, __t.__first_);
461 _VSTD::swap(__begin_, __t.__begin_);
462 _VSTD::swap(__end_, __t.__end_);
463 _VSTD::swap(__end_cap(), __t.__end_cap());
Howard Hinnant3e519522010-05-11 19:42:16 +0000464#ifndef _LIBCPP_NO_EXCEPTIONS
465 }
466 catch (...)
467 {
468 }
Howard Hinnantb3371f62010-08-22 00:02:43 +0000469#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +0000470 }
471}
472
473template <class _Tp, class _Allocator>
474void
475__split_buffer<_Tp, _Allocator>::push_front(const_reference __x)
476{
477 if (__begin_ == __first_)
478 {
479 if (__end_ < __end_cap())
480 {
481 difference_type __d = __end_cap() - __end_;
482 __d = (__d + 1) / 2;
Howard Hinnantce48a112011-06-30 21:18:19 +0000483 __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d);
Howard Hinnant3e519522010-05-11 19:42:16 +0000484 __end_ += __d;
485 }
486 else
487 {
Howard Hinnantc2063662011-12-01 20:21:04 +0000488 size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
Howard Hinnant189b2122010-07-07 19:14:52 +0000489 __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
Howard Hinnant3e519522010-05-11 19:42:16 +0000490 __t.__construct_at_end(move_iterator<pointer>(__begin_),
491 move_iterator<pointer>(__end_));
Howard Hinnantce48a112011-06-30 21:18:19 +0000492 _VSTD::swap(__first_, __t.__first_);
493 _VSTD::swap(__begin_, __t.__begin_);
494 _VSTD::swap(__end_, __t.__end_);
495 _VSTD::swap(__end_cap(), __t.__end_cap());
Howard Hinnant3e519522010-05-11 19:42:16 +0000496 }
497 }
Eric Fiselier0068c592019-11-16 17:13:26 -0500498 __alloc_traits::construct(__alloc(), _VSTD::__to_address(__begin_-1), __x);
Howard Hinnant3e519522010-05-11 19:42:16 +0000499 --__begin_;
500}
501
Eric Fiselierafa7a952017-04-19 01:23:04 +0000502#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +0000503
504template <class _Tp, class _Allocator>
505void
506__split_buffer<_Tp, _Allocator>::push_front(value_type&& __x)
507{
508 if (__begin_ == __first_)
509 {
510 if (__end_ < __end_cap())
511 {
512 difference_type __d = __end_cap() - __end_;
513 __d = (__d + 1) / 2;
Howard Hinnantce48a112011-06-30 21:18:19 +0000514 __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d);
Howard Hinnant3e519522010-05-11 19:42:16 +0000515 __end_ += __d;
516 }
517 else
518 {
Howard Hinnantc2063662011-12-01 20:21:04 +0000519 size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
Howard Hinnant189b2122010-07-07 19:14:52 +0000520 __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
Howard Hinnant3e519522010-05-11 19:42:16 +0000521 __t.__construct_at_end(move_iterator<pointer>(__begin_),
522 move_iterator<pointer>(__end_));
Howard Hinnantce48a112011-06-30 21:18:19 +0000523 _VSTD::swap(__first_, __t.__first_);
524 _VSTD::swap(__begin_, __t.__begin_);
525 _VSTD::swap(__end_, __t.__end_);
526 _VSTD::swap(__end_cap(), __t.__end_cap());
Howard Hinnant3e519522010-05-11 19:42:16 +0000527 }
528 }
Eric Fiselier0068c592019-11-16 17:13:26 -0500529 __alloc_traits::construct(__alloc(), _VSTD::__to_address(__begin_-1),
Howard Hinnantce48a112011-06-30 21:18:19 +0000530 _VSTD::move(__x));
Howard Hinnant3e519522010-05-11 19:42:16 +0000531 --__begin_;
532}
533
Eric Fiselierafa7a952017-04-19 01:23:04 +0000534#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +0000535
536template <class _Tp, class _Allocator>
Howard Hinnant3af48ef2013-10-04 22:09:00 +0000537inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000538void
539__split_buffer<_Tp, _Allocator>::push_back(const_reference __x)
540{
541 if (__end_ == __end_cap())
542 {
543 if (__begin_ > __first_)
544 {
545 difference_type __d = __begin_ - __first_;
546 __d = (__d + 1) / 2;
Howard Hinnantce48a112011-06-30 21:18:19 +0000547 __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
Howard Hinnant3e519522010-05-11 19:42:16 +0000548 __begin_ -= __d;
549 }
550 else
551 {
Howard Hinnantc2063662011-12-01 20:21:04 +0000552 size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +0000553 __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
554 __t.__construct_at_end(move_iterator<pointer>(__begin_),
555 move_iterator<pointer>(__end_));
Howard Hinnantce48a112011-06-30 21:18:19 +0000556 _VSTD::swap(__first_, __t.__first_);
557 _VSTD::swap(__begin_, __t.__begin_);
558 _VSTD::swap(__end_, __t.__end_);
559 _VSTD::swap(__end_cap(), __t.__end_cap());
Howard Hinnant3e519522010-05-11 19:42:16 +0000560 }
561 }
Eric Fiselier0068c592019-11-16 17:13:26 -0500562 __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_), __x);
Howard Hinnant3e519522010-05-11 19:42:16 +0000563 ++__end_;
564}
565
Eric Fiselierafa7a952017-04-19 01:23:04 +0000566#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +0000567
568template <class _Tp, class _Allocator>
569void
570__split_buffer<_Tp, _Allocator>::push_back(value_type&& __x)
571{
572 if (__end_ == __end_cap())
573 {
574 if (__begin_ > __first_)
575 {
576 difference_type __d = __begin_ - __first_;
577 __d = (__d + 1) / 2;
Howard Hinnantce48a112011-06-30 21:18:19 +0000578 __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
Howard Hinnant3e519522010-05-11 19:42:16 +0000579 __begin_ -= __d;
580 }
581 else
582 {
Howard Hinnantc2063662011-12-01 20:21:04 +0000583 size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +0000584 __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
585 __t.__construct_at_end(move_iterator<pointer>(__begin_),
586 move_iterator<pointer>(__end_));
Howard Hinnantce48a112011-06-30 21:18:19 +0000587 _VSTD::swap(__first_, __t.__first_);
588 _VSTD::swap(__begin_, __t.__begin_);
589 _VSTD::swap(__end_, __t.__end_);
590 _VSTD::swap(__end_cap(), __t.__end_cap());
Howard Hinnant3e519522010-05-11 19:42:16 +0000591 }
592 }
Eric Fiselier0068c592019-11-16 17:13:26 -0500593 __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_),
Howard Hinnantce48a112011-06-30 21:18:19 +0000594 _VSTD::move(__x));
Howard Hinnant3e519522010-05-11 19:42:16 +0000595 ++__end_;
596}
597
598template <class _Tp, class _Allocator>
599template <class... _Args>
600void
601__split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args)
602{
603 if (__end_ == __end_cap())
604 {
605 if (__begin_ > __first_)
606 {
607 difference_type __d = __begin_ - __first_;
608 __d = (__d + 1) / 2;
Howard Hinnantce48a112011-06-30 21:18:19 +0000609 __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
Howard Hinnant3e519522010-05-11 19:42:16 +0000610 __begin_ -= __d;
611 }
612 else
613 {
Howard Hinnantc2063662011-12-01 20:21:04 +0000614 size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
Howard Hinnant3e519522010-05-11 19:42:16 +0000615 __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
616 __t.__construct_at_end(move_iterator<pointer>(__begin_),
617 move_iterator<pointer>(__end_));
Howard Hinnantce48a112011-06-30 21:18:19 +0000618 _VSTD::swap(__first_, __t.__first_);
619 _VSTD::swap(__begin_, __t.__begin_);
620 _VSTD::swap(__end_, __t.__end_);
621 _VSTD::swap(__end_cap(), __t.__end_cap());
Howard Hinnant3e519522010-05-11 19:42:16 +0000622 }
623 }
Eric Fiselier0068c592019-11-16 17:13:26 -0500624 __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_),
Howard Hinnantce48a112011-06-30 21:18:19 +0000625 _VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +0000626 ++__end_;
627}
628
Eric Fiselierafa7a952017-04-19 01:23:04 +0000629#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +0000630
Howard Hinnant9eebe112011-06-02 20:00:14 +0000631template <class _Tp, class _Allocator>
Howard Hinnant3af48ef2013-10-04 22:09:00 +0000632inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9eebe112011-06-02 20:00:14 +0000633void
634swap(__split_buffer<_Tp, _Allocator>& __x, __split_buffer<_Tp, _Allocator>& __y)
635 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
636{
637 __x.swap(__y);
638}
639
Howard Hinnant3e519522010-05-11 19:42:16 +0000640_LIBCPP_END_NAMESPACE_STD
641
Eric Fiseliera016efb2017-05-31 22:07:49 +0000642_LIBCPP_POP_MACROS
643
Howard Hinnant3e519522010-05-11 19:42:16 +0000644#endif // _LIBCPP_SPLIT_BUFFER