blob: 1d529cbe27d204e7441cf00ff4bb4ea939a69a51 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:161// -*- 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:279#include <__undef_min_max>
10
Howard Hinnant08e17472011-10-17 20:05:1011#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:1612#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:1013#endif
Howard Hinnantbc8d3f92010-05-11 19:42:1614
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:5225template <class _Tp, class _Allocator = allocator<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:1626struct __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:4333 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:1634 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:1454 _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:1658
Howard Hinnant009b2c42011-06-03 15:16:4959 __split_buffer()
60 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:1661 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:1966#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant0a612b02011-06-02 20:00:1467 __split_buffer(__split_buffer&& __c)
68 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:1669 __split_buffer(__split_buffer&& __c, const __alloc_rr& __a);
Howard Hinnant0a612b02011-06-02 20:00:1470 __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:1974#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:1675
Howard Hinnant0a612b02011-06-02 20:00:1476 _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:1680
Howard Hinnant0a612b02011-06-02 20:00:1481 _LIBCPP_INLINE_VISIBILITY
82 void clear() _NOEXCEPT
83 {__destruct_at_end(__begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:1684 _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:1496 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:1697 void push_front(const_reference __x);
Howard Hinnantb0bfd9b2012-02-15 00:41:3498 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Michael J. Spencer626916f2010-12-10 19:47:5499#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16100 void push_front(value_type&& __x);
101 void push_back(value_type&& __x);
Michael J. Spencer626916f2010-12-10 19:47:54102#if !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16103 template <class... _Args>
104 void emplace_back(_Args&&... __args);
Michael J. Spencer626916f2010-12-10 19:47:54105#endif // !defined(_LIBCPP_HAS_NO_VARIADICS)
106#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16107
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:16112 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16113 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:28130 {__destruct_at_begin(__new_begin, is_trivially_destructible<value_type>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16131 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:14134 _LIBCPP_INLINE_VISIBILITY
135 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantb0bfd9b2012-02-15 00:41:34136 {__destruct_at_end(__new_last, false_type());}
137 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0a612b02011-06-02 20:00:14138 void __destruct_at_end(pointer __new_last, false_type) _NOEXCEPT;
Howard Hinnantb0bfd9b2012-02-15 00:41:34139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0a612b02011-06-02 20:00:14140 void __destruct_at_end(pointer __new_last, true_type) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16141
Howard Hinnant0a612b02011-06-02 20:00:14142 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:16145
146 bool __invariants() const;
147
148private:
Howard Hinnant333f50d2010-09-21 20:16:37149 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31150 void __move_assign_alloc(__split_buffer& __c, true_type)
Howard Hinnant0a612b02011-06-02 20:00:14151 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16152 {
Howard Hinnant0949eed2011-06-30 21:18:19153 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16154 }
155
Howard Hinnant333f50d2010-09-21 20:16:37156 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04157 void __move_assign_alloc(__split_buffer&, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16158 {}
159
Howard Hinnant333f50d2010-09-21 20:16:37160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16161 static void __swap_alloc(__alloc_rr& __x, __alloc_rr& __y)
Howard Hinnant0a612b02011-06-02 20:00:14162 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
163 __is_nothrow_swappable<__alloc_rr>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16164 {__swap_alloc(__x, __y, integral_constant<bool,
165 __alloc_traits::propagate_on_container_swap::value>());}
166
Howard Hinnant333f50d2010-09-21 20:16:37167 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16168 static void __swap_alloc(__alloc_rr& __x, __alloc_rr& __y, true_type)
Howard Hinnant0a612b02011-06-02 20:00:14169 _NOEXCEPT_(__is_nothrow_swappable<__alloc_rr>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16170 {
Howard Hinnant0949eed2011-06-30 21:18:19171 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16172 swap(__x, __y);
173 }
Howard Hinnant333f50d2010-09-21 20:16:37174
175 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04176 static void __swap_alloc(__alloc_rr&, __alloc_rr&, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16177 {}
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:16211void
212__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n)
213{
Howard Hinnantbc8d3f92010-05-11 19:42:16214 __alloc_rr& __a = this->__alloc();
215 do
216 {
Howard Hinnant5f255942011-08-28 15:21:29217 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16218 ++this->__end_;
219 --__n;
220 } while (__n > 0);
221}
222
Howard Hinnantbc8d3f92010-05-11 19:42:16223// 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:16230void
231__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
232{
Howard Hinnantbc8d3f92010-05-11 19:42:16233 __alloc_rr& __a = this->__alloc();
234 do
235 {
Howard Hinnant0949eed2011-06-30 21:18:19236 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16237 ++this->__end_;
238 --__n;
239 } while (__n > 0);
240}
241
242template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16243template <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:19258 size_type __new_cap = _VSTD::max<size_type>(2 * __old_cap, 8);
Howard Hinnantbc8d3f92010-05-11 19:42:16259 __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:19262 _VSTD::__to_raw_pointer(__buf.__end_), _VSTD::move(*__p));
Howard Hinnantbc8d3f92010-05-11 19:42:16263 swap(__buf);
264 }
Howard Hinnant0949eed2011-06-30 21:18:19265 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16266 ++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:19282 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16283 ++this->__end_;
284 }
285}
286
287template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00288inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16289void
290__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type)
291{
Howard Hinnantb0bfd9b2012-02-15 00:41:34292 while (__begin_ != __new_begin)
Howard Hinnantfcd8db72013-06-23 21:17:24293 __alloc_traits::destroy(__alloc(), __to_raw_pointer(__begin_++));
Howard Hinnantbc8d3f92010-05-11 19:42:16294}
295
296template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00297inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16298void
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>
Howard Hinnant1e564242013-10-04 22:09:00305inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16306void
Howard Hinnant0a612b02011-06-02 20:00:14307__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16308{
Howard Hinnantb0bfd9b2012-02-15 00:41:34309 while (__new_last != __end_)
Howard Hinnantfcd8db72013-06-23 21:17:24310 __alloc_traits::destroy(__alloc(), __to_raw_pointer(--__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16311}
312
313template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00314inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16315void
Howard Hinnant0a612b02011-06-02 20:00:14316__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16317{
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)
Howard Hinnantfcd8db72013-06-23 21:17:24323 : __end_cap_(nullptr, __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16324{
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>
Howard Hinnant1e564242013-10-04 22:09:00331inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16332__split_buffer<_Tp, _Allocator>::__split_buffer()
Howard Hinnant009b2c42011-06-03 15:16:49333 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantfcd8db72013-06-23 21:17:24334 : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16335{
336}
337
338template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00339inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16340__split_buffer<_Tp, _Allocator>::__split_buffer(__alloc_rr& __a)
Howard Hinnantfcd8db72013-06-23 21:17:24341 : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16342{
343}
344
345template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00346inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16347__split_buffer<_Tp, _Allocator>::__split_buffer(const __alloc_rr& __a)
Howard Hinnantfcd8db72013-06-23 21:17:24348 : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16349{
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:19360#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16361
362template <class _Tp, class _Allocator>
363__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c)
Howard Hinnant0a612b02011-06-02 20:00:14364 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19365 : __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:16369{
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:50397 typedef move_iterator<iterator> _Ip;
398 __construct_at_end(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16399 }
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:14405 _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:16408{
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:19422#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16423
424template <class _Tp, class _Allocator>
425void
426__split_buffer<_Tp, _Allocator>::swap(__split_buffer& __x)
Howard Hinnant0a612b02011-06-02 20:00:14427 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
428 __is_nothrow_swappable<__alloc_rr>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16429{
Howard Hinnant0949eed2011-06-30 21:18:19430 _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:16434 __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:19446 _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:16450 }
451}
452
453template <class _Tp, class _Allocator>
454void
Howard Hinnant0a612b02011-06-02 20:00:14455__split_buffer<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16456{
457 if (capacity() > size())
458 {
459#ifndef _LIBCPP_NO_EXCEPTIONS
460 try
461 {
Howard Hinnant324bb032010-08-22 00:02:43462#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16463 __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:19467 _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:16471#ifndef _LIBCPP_NO_EXCEPTIONS
472 }
473 catch (...)
474 {
475 }
Howard Hinnant324bb032010-08-22 00:02:43476#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16477 }
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:19490 __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d);
Howard Hinnantbc8d3f92010-05-11 19:42:16491 __end_ += __d;
492 }
493 else
494 {
Howard Hinnantec3773c2011-12-01 20:21:04495 size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
Howard Hinnantf8ce4592010-07-07 19:14:52496 __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16497 __t.__construct_at_end(move_iterator<pointer>(__begin_),
498 move_iterator<pointer>(__end_));
Howard Hinnant0949eed2011-06-30 21:18:19499 _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:16503 }
504 }
Howard Hinnant0949eed2011-06-30 21:18:19505 __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__begin_-1), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16506 --__begin_;
507}
508
Howard Hinnant73d21a42010-09-04 23:28:19509#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16510
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:19521 __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d);
Howard Hinnantbc8d3f92010-05-11 19:42:16522 __end_ += __d;
523 }
524 else
525 {
Howard Hinnantec3773c2011-12-01 20:21:04526 size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
Howard Hinnantf8ce4592010-07-07 19:14:52527 __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16528 __t.__construct_at_end(move_iterator<pointer>(__begin_),
529 move_iterator<pointer>(__end_));
Howard Hinnant0949eed2011-06-30 21:18:19530 _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:16534 }
535 }
Howard Hinnant0949eed2011-06-30 21:18:19536 __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__begin_-1),
537 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16538 --__begin_;
539}
540
Howard Hinnant73d21a42010-09-04 23:28:19541#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16542
543template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00544inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16545void
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:19554 __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
Howard Hinnantbc8d3f92010-05-11 19:42:16555 __begin_ -= __d;
556 }
557 else
558 {
Howard Hinnantec3773c2011-12-01 20:21:04559 size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16560 __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:19563 _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:16567 }
568 }
Howard Hinnant0949eed2011-06-30 21:18:19569 __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16570 ++__end_;
571}
572
Howard Hinnant73d21a42010-09-04 23:28:19573#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16574
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:19585 __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
Howard Hinnantbc8d3f92010-05-11 19:42:16586 __begin_ -= __d;
587 }
588 else
589 {
Howard Hinnantec3773c2011-12-01 20:21:04590 size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16591 __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:19594 _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:16598 }
599 }
Howard Hinnant0949eed2011-06-30 21:18:19600 __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__end_),
601 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16602 ++__end_;
603}
604
Howard Hinnant73d21a42010-09-04 23:28:19605#ifndef _LIBCPP_HAS_NO_VARIADICS
606
Howard Hinnantbc8d3f92010-05-11 19:42:16607template <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:19618 __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
Howard Hinnantbc8d3f92010-05-11 19:42:16619 __begin_ -= __d;
620 }
621 else
622 {
Howard Hinnantec3773c2011-12-01 20:21:04623 size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16624 __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:19627 _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:16631 }
632 }
Howard Hinnant0949eed2011-06-30 21:18:19633 __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__end_),
634 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16635 ++__end_;
636}
637
Howard Hinnant73d21a42010-09-04 23:28:19638#endif // _LIBCPP_HAS_NO_VARIADICS
639
640#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16641
Howard Hinnant0a612b02011-06-02 20:00:14642template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00643inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0a612b02011-06-02 20:00:14644void
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:16652_LIBCPP_END_NAMESPACE_STD
653
654#endif // _LIBCPP_SPLIT_BUFFER