2 #ifndef _LIBCPP_SPLIT_BUFFER
3 #define _LIBCPP_SPLIT_BUFFER
9 #include <__undef_min_max>
11 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
12 #pragma GCC system_header
15 _LIBCPP_BEGIN_NAMESPACE_STD
18 class __split_buffer_common
21 void __throw_length_error() const;
22 void __throw_out_of_range() const;
25 template <class _Tp, class _Allocator = allocator<_Tp> >
27 : private __split_buffer_common<true>
30 __split_buffer(const __split_buffer&);
31 __split_buffer& operator=(const __split_buffer&);
33 typedef _Tp value_type;
34 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;
49 __compressed_pair<pointer, allocator_type> __end_cap_;
51 typedef typename add_lvalue_reference<allocator_type>::type __alloc_ref;
52 typedef typename add_lvalue_reference<allocator_type>::type __alloc_const_ref;
54 _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();}
60 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
61 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);
66 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
67 __split_buffer(__split_buffer&& __c)
68 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
69 __split_buffer(__split_buffer&& __c, const __alloc_rr& __a);
70 __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);
74 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
76 _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_;}
81 _LIBCPP_INLINE_VISIBILITY
82 void clear() _NOEXCEPT
83 {__destruct_at_end(__begin_);}
84 _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_);}
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);}
95 void reserve(size_type __n);
96 void shrink_to_fit() _NOEXCEPT;
97 void push_front(const_reference __x);
98 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
99 #if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
100 void push_front(value_type&& __x);
101 void push_back(value_type&& __x);
102 #if !defined(_LIBCPP_HAS_NO_VARIADICS)
103 template <class... _Args>
104 void emplace_back(_Args&&... __args);
105 #endif // !defined(_LIBCPP_HAS_NO_VARIADICS)
106 #endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
108 _LIBCPP_INLINE_VISIBILITY void pop_front() {__destruct_at_begin(__begin_+1);}
109 _LIBCPP_INLINE_VISIBILITY void pop_back() {__destruct_at_end(__end_-1);}
111 void __construct_at_end(size_type __n);
112 void __construct_at_end(size_type __n, const_reference __x);
113 template <class _InputIter>
116 __is_input_iterator<_InputIter>::value &&
117 !__is_forward_iterator<_InputIter>::value,
120 __construct_at_end(_InputIter __first, _InputIter __last);
121 template <class _ForwardIterator>
124 __is_forward_iterator<_ForwardIterator>::value,
127 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
129 _LIBCPP_INLINE_VISIBILITY void __destruct_at_begin(pointer __new_begin)
130 {__destruct_at_begin(__new_begin, is_trivially_destructible<value_type>());}
131 void __destruct_at_begin(pointer __new_begin, false_type);
132 void __destruct_at_begin(pointer __new_begin, true_type);
134 _LIBCPP_INLINE_VISIBILITY
135 void __destruct_at_end(pointer __new_last) _NOEXCEPT
136 {__destruct_at_end(__new_last, false_type());}
137 _LIBCPP_INLINE_VISIBILITY
138 void __destruct_at_end(pointer __new_last, false_type) _NOEXCEPT;
139 _LIBCPP_INLINE_VISIBILITY
140 void __destruct_at_end(pointer __new_last, true_type) _NOEXCEPT;
142 void swap(__split_buffer& __x)
143 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
144 __is_nothrow_swappable<__alloc_rr>::value);
146 bool __invariants() const;
149 _LIBCPP_INLINE_VISIBILITY
150 void __move_assign_alloc(__split_buffer& __c, true_type)
151 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
153 __alloc() = _VSTD::move(__c.__alloc());
156 _LIBCPP_INLINE_VISIBILITY
157 void __move_assign_alloc(__split_buffer&, false_type) _NOEXCEPT
160 _LIBCPP_INLINE_VISIBILITY
161 static void __swap_alloc(__alloc_rr& __x, __alloc_rr& __y)
162 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
163 __is_nothrow_swappable<__alloc_rr>::value)
164 {__swap_alloc(__x, __y, integral_constant<bool,
165 __alloc_traits::propagate_on_container_swap::value>());}
167 _LIBCPP_INLINE_VISIBILITY
168 static void __swap_alloc(__alloc_rr& __x, __alloc_rr& __y, true_type)
169 _NOEXCEPT_(__is_nothrow_swappable<__alloc_rr>::value)
175 _LIBCPP_INLINE_VISIBILITY
176 static void __swap_alloc(__alloc_rr&, __alloc_rr&, false_type) _NOEXCEPT
180 template <class _Tp, class _Allocator>
182 __split_buffer<_Tp, _Allocator>::__invariants() const
184 if (__first_ == nullptr)
186 if (__begin_ != nullptr)
188 if (__end_ != nullptr)
190 if (__end_cap() != nullptr)
195 if (__begin_ < __first_)
197 if (__end_ < __begin_)
199 if (__end_cap() < __end_)
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
210 template <class _Tp, class _Allocator>
212 __split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n)
214 __alloc_rr& __a = this->__alloc();
217 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
223 // 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)
229 template <class _Tp, class _Allocator>
231 __split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
233 __alloc_rr& __a = this->__alloc();
236 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
242 template <class _Tp, class _Allocator>
243 template <class _InputIter>
246 __is_input_iterator<_InputIter>::value &&
247 !__is_forward_iterator<_InputIter>::value,
250 __split_buffer<_Tp, _Allocator>::__construct_at_end(_InputIter __first, _InputIter __last)
252 __alloc_rr& __a = this->__alloc();
253 for (; __first != __last; ++__first)
255 if (__end_ == __end_cap())
257 size_type __old_cap = __end_cap() - __first_;
258 size_type __new_cap = _VSTD::max<size_type>(2 * __old_cap, 8);
259 __split_buffer __buf(__new_cap, 0, __a);
260 for (pointer __p = __begin_; __p != __end_; ++__p, ++__buf.__end_)
261 __alloc_traits::construct(__buf.__alloc(),
262 _VSTD::__to_raw_pointer(__buf.__end_), _VSTD::move(*__p));
265 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
270 template <class _Tp, class _Allocator>
271 template <class _ForwardIterator>
274 __is_forward_iterator<_ForwardIterator>::value,
277 __split_buffer<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
279 __alloc_rr& __a = this->__alloc();
280 for (; __first != __last; ++__first)
282 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
287 template <class _Tp, class _Allocator>
288 inline _LIBCPP_INLINE_VISIBILITY
290 __split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type)
292 while (__begin_ != __new_begin)
293 __alloc_traits::destroy(__alloc(), __to_raw_pointer(__begin_++));
296 template <class _Tp, class _Allocator>
297 inline _LIBCPP_INLINE_VISIBILITY
299 __split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, true_type)
301 __begin_ = __new_begin;
304 template <class _Tp, class _Allocator>
305 inline _LIBCPP_INLINE_VISIBILITY
307 __split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT
309 while (__new_last != __end_)
310 __alloc_traits::destroy(__alloc(), __to_raw_pointer(--__end_));
313 template <class _Tp, class _Allocator>
314 inline _LIBCPP_INLINE_VISIBILITY
316 __split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, true_type) _NOEXCEPT
321 template <class _Tp, class _Allocator>
322 __split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __start, __alloc_rr& __a)
323 : __end_cap_(nullptr, __a)
325 __first_ = __cap != 0 ? __alloc_traits::allocate(__alloc(), __cap) : nullptr;
326 __begin_ = __end_ = __first_ + __start;
327 __end_cap() = __first_ + __cap;
330 template <class _Tp, class _Allocator>
331 inline _LIBCPP_INLINE_VISIBILITY
332 __split_buffer<_Tp, _Allocator>::__split_buffer()
333 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
334 : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr)
338 template <class _Tp, class _Allocator>
339 inline _LIBCPP_INLINE_VISIBILITY
340 __split_buffer<_Tp, _Allocator>::__split_buffer(__alloc_rr& __a)
341 : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a)
345 template <class _Tp, class _Allocator>
346 inline _LIBCPP_INLINE_VISIBILITY
347 __split_buffer<_Tp, _Allocator>::__split_buffer(const __alloc_rr& __a)
348 : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a)
352 template <class _Tp, class _Allocator>
353 __split_buffer<_Tp, _Allocator>::~__split_buffer()
357 __alloc_traits::deallocate(__alloc(), __first_, capacity());
360 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
362 template <class _Tp, class _Allocator>
363 __split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c)
364 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
365 : __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_))
370 __c.__first_ = nullptr;
371 __c.__begin_ = nullptr;
372 __c.__end_ = nullptr;
373 __c.__end_cap() = nullptr;
376 template <class _Tp, class _Allocator>
377 __split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a)
380 if (__a == __c.__alloc())
382 __first_ = __c.__first_;
383 __begin_ = __c.__begin_;
385 __end_cap() = __c.__end_cap();
386 __c.__first_ = nullptr;
387 __c.__begin_ = nullptr;
388 __c.__end_ = nullptr;
389 __c.__end_cap() = nullptr;
393 size_type __cap = __c.size();
394 __first_ = __alloc_traits::allocate(__alloc(), __cap);
395 __begin_ = __end_ = __first_;
396 __end_cap() = __first_ + __cap;
397 typedef move_iterator<iterator> _Ip;
398 __construct_at_end(_Ip(__c.begin()), _Ip(__c.end()));
402 template <class _Tp, class _Allocator>
403 __split_buffer<_Tp, _Allocator>&
404 __split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c)
405 _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)
411 __first_ = __c.__first_;
412 __begin_ = __c.__begin_;
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;
422 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
424 template <class _Tp, class _Allocator>
426 __split_buffer<_Tp, _Allocator>::swap(__split_buffer& __x)
427 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
428 __is_nothrow_swappable<__alloc_rr>::value)
430 _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());
434 __swap_alloc(__alloc(), __x.__alloc());
437 template <class _Tp, class _Allocator>
439 __split_buffer<_Tp, _Allocator>::reserve(size_type __n)
441 if (__n < capacity())
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_));
446 _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());
453 template <class _Tp, class _Allocator>
455 __split_buffer<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
457 if (capacity() > size())
459 #ifndef _LIBCPP_NO_EXCEPTIONS
462 #endif // _LIBCPP_NO_EXCEPTIONS
463 __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_);
467 _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());
471 #ifndef _LIBCPP_NO_EXCEPTIONS
476 #endif // _LIBCPP_NO_EXCEPTIONS
480 template <class _Tp, class _Allocator>
482 __split_buffer<_Tp, _Allocator>::push_front(const_reference __x)
484 if (__begin_ == __first_)
486 if (__end_ < __end_cap())
488 difference_type __d = __end_cap() - __end_;
490 __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d);
495 size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
496 __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
497 __t.__construct_at_end(move_iterator<pointer>(__begin_),
498 move_iterator<pointer>(__end_));
499 _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());
505 __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__begin_-1), __x);
509 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
511 template <class _Tp, class _Allocator>
513 __split_buffer<_Tp, _Allocator>::push_front(value_type&& __x)
515 if (__begin_ == __first_)
517 if (__end_ < __end_cap())
519 difference_type __d = __end_cap() - __end_;
521 __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d);
526 size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
527 __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
528 __t.__construct_at_end(move_iterator<pointer>(__begin_),
529 move_iterator<pointer>(__end_));
530 _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());
536 __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__begin_-1),
541 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
543 template <class _Tp, class _Allocator>
544 inline _LIBCPP_INLINE_VISIBILITY
546 __split_buffer<_Tp, _Allocator>::push_back(const_reference __x)
548 if (__end_ == __end_cap())
550 if (__begin_ > __first_)
552 difference_type __d = __begin_ - __first_;
554 __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
559 size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
560 __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_));
563 _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());
569 __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__end_), __x);
573 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
575 template <class _Tp, class _Allocator>
577 __split_buffer<_Tp, _Allocator>::push_back(value_type&& __x)
579 if (__end_ == __end_cap())
581 if (__begin_ > __first_)
583 difference_type __d = __begin_ - __first_;
585 __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
590 size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
591 __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_));
594 _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());
600 __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__end_),
605 #ifndef _LIBCPP_HAS_NO_VARIADICS
607 template <class _Tp, class _Allocator>
608 template <class... _Args>
610 __split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args)
612 if (__end_ == __end_cap())
614 if (__begin_ > __first_)
616 difference_type __d = __begin_ - __first_;
618 __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
623 size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
624 __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_));
627 _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());
633 __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__end_),
634 _VSTD::forward<_Args>(__args)...);
638 #endif // _LIBCPP_HAS_NO_VARIADICS
640 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
642 template <class _Tp, class _Allocator>
643 inline _LIBCPP_INLINE_VISIBILITY
645 swap(__split_buffer<_Tp, _Allocator>& __x, __split_buffer<_Tp, _Allocator>& __y)
646 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
652 _LIBCPP_END_NAMESPACE_STD
654 #endif // _LIBCPP_SPLIT_BUFFER