10b57cec5SDimitry Andric// -*- C++ -*- 20b57cec5SDimitry Andric#ifndef _LIBCPP_SPLIT_BUFFER 30b57cec5SDimitry Andric#define _LIBCPP_SPLIT_BUFFER 40b57cec5SDimitry Andric 50b57cec5SDimitry Andric#include <__config> 60b57cec5SDimitry Andric#include <type_traits> 70b57cec5SDimitry Andric#include <algorithm> 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 100b57cec5SDimitry Andric#pragma GCC system_header 110b57cec5SDimitry Andric#endif 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS 140b57cec5SDimitry Andric#include <__undef_macros> 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD 180b57cec5SDimitry Andric 190b57cec5SDimitry Andrictemplate <bool> 200b57cec5SDimitry Andricclass __split_buffer_common 210b57cec5SDimitry Andric{ 220b57cec5SDimitry Andricprotected: 230b57cec5SDimitry Andric void __throw_length_error() const; 240b57cec5SDimitry Andric void __throw_out_of_range() const; 250b57cec5SDimitry Andric}; 260b57cec5SDimitry Andric 270b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator = allocator<_Tp> > 280b57cec5SDimitry Andricstruct __split_buffer 290b57cec5SDimitry Andric : private __split_buffer_common<true> 300b57cec5SDimitry Andric{ 310b57cec5SDimitry Andricprivate: 320b57cec5SDimitry Andric __split_buffer(const __split_buffer&); 330b57cec5SDimitry Andric __split_buffer& operator=(const __split_buffer&); 340b57cec5SDimitry Andricpublic: 350b57cec5SDimitry Andric typedef _Tp value_type; 360b57cec5SDimitry Andric typedef _Allocator allocator_type; 370b57cec5SDimitry Andric typedef typename remove_reference<allocator_type>::type __alloc_rr; 380b57cec5SDimitry Andric typedef allocator_traits<__alloc_rr> __alloc_traits; 390b57cec5SDimitry Andric typedef value_type& reference; 400b57cec5SDimitry Andric typedef const value_type& const_reference; 410b57cec5SDimitry Andric typedef typename __alloc_traits::size_type size_type; 420b57cec5SDimitry Andric typedef typename __alloc_traits::difference_type difference_type; 430b57cec5SDimitry Andric typedef typename __alloc_traits::pointer pointer; 440b57cec5SDimitry Andric typedef typename __alloc_traits::const_pointer const_pointer; 450b57cec5SDimitry Andric typedef pointer iterator; 460b57cec5SDimitry Andric typedef const_pointer const_iterator; 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric pointer __first_; 490b57cec5SDimitry Andric pointer __begin_; 500b57cec5SDimitry Andric pointer __end_; 510b57cec5SDimitry Andric __compressed_pair<pointer, allocator_type> __end_cap_; 520b57cec5SDimitry Andric 530b57cec5SDimitry Andric typedef typename add_lvalue_reference<allocator_type>::type __alloc_ref; 540b57cec5SDimitry Andric typedef typename add_lvalue_reference<allocator_type>::type __alloc_const_ref; 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY __alloc_rr& __alloc() _NOEXCEPT {return __end_cap_.second();} 570b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY const __alloc_rr& __alloc() const _NOEXCEPT {return __end_cap_.second();} 580b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY pointer& __end_cap() _NOEXCEPT {return __end_cap_.first();} 590b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY const pointer& __end_cap() const _NOEXCEPT {return __end_cap_.first();} 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 620b57cec5SDimitry Andric __split_buffer() 630b57cec5SDimitry Andric _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value); 640b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 650b57cec5SDimitry Andric explicit __split_buffer(__alloc_rr& __a); 660b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 670b57cec5SDimitry Andric explicit __split_buffer(const __alloc_rr& __a); 680b57cec5SDimitry Andric __split_buffer(size_type __cap, size_type __start, __alloc_rr& __a); 690b57cec5SDimitry Andric ~__split_buffer(); 700b57cec5SDimitry Andric 710b57cec5SDimitry Andric __split_buffer(__split_buffer&& __c) 720b57cec5SDimitry Andric _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value); 730b57cec5SDimitry Andric __split_buffer(__split_buffer&& __c, const __alloc_rr& __a); 740b57cec5SDimitry Andric __split_buffer& operator=(__split_buffer&& __c) 750b57cec5SDimitry Andric _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value && 760b57cec5SDimitry Andric is_nothrow_move_assignable<allocator_type>::value) || 770b57cec5SDimitry Andric !__alloc_traits::propagate_on_container_move_assignment::value); 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT {return __begin_;} 800b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT {return __begin_;} 810b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT {return __end_;} 820b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT {return __end_;} 830b57cec5SDimitry Andric 840b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 850b57cec5SDimitry Andric void clear() _NOEXCEPT 860b57cec5SDimitry Andric {__destruct_at_end(__begin_);} 870b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY size_type size() const {return static_cast<size_type>(__end_ - __begin_);} 880b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY bool empty() const {return __end_ == __begin_;} 890b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY size_type capacity() const {return static_cast<size_type>(__end_cap() - __first_);} 900b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY size_type __front_spare() const {return static_cast<size_type>(__begin_ - __first_);} 910b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY size_type __back_spare() const {return static_cast<size_type>(__end_cap() - __end_);} 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY reference front() {return *__begin_;} 940b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY const_reference front() const {return *__begin_;} 950b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY reference back() {return *(__end_ - 1);} 960b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY const_reference back() const {return *(__end_ - 1);} 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric void reserve(size_type __n); 990b57cec5SDimitry Andric void shrink_to_fit() _NOEXCEPT; 1000b57cec5SDimitry Andric void push_front(const_reference __x); 1010b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x); 1020b57cec5SDimitry Andric void push_front(value_type&& __x); 1030b57cec5SDimitry Andric void push_back(value_type&& __x); 1040b57cec5SDimitry Andric template <class... _Args> 1050b57cec5SDimitry Andric void emplace_back(_Args&&... __args); 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY void pop_front() {__destruct_at_begin(__begin_+1);} 1080b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY void pop_back() {__destruct_at_end(__end_-1);} 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric void __construct_at_end(size_type __n); 1110b57cec5SDimitry Andric void __construct_at_end(size_type __n, const_reference __x); 1120b57cec5SDimitry Andric template <class _InputIter> 1130b57cec5SDimitry Andric typename enable_if 1140b57cec5SDimitry Andric < 115480093f4SDimitry Andric __is_cpp17_input_iterator<_InputIter>::value && 116480093f4SDimitry Andric !__is_cpp17_forward_iterator<_InputIter>::value, 1170b57cec5SDimitry Andric void 1180b57cec5SDimitry Andric >::type 1190b57cec5SDimitry Andric __construct_at_end(_InputIter __first, _InputIter __last); 1200b57cec5SDimitry Andric template <class _ForwardIterator> 1210b57cec5SDimitry Andric typename enable_if 1220b57cec5SDimitry Andric < 123480093f4SDimitry Andric __is_cpp17_forward_iterator<_ForwardIterator>::value, 1240b57cec5SDimitry Andric void 1250b57cec5SDimitry Andric >::type 1260b57cec5SDimitry Andric __construct_at_end(_ForwardIterator __first, _ForwardIterator __last); 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY void __destruct_at_begin(pointer __new_begin) 1290b57cec5SDimitry Andric {__destruct_at_begin(__new_begin, is_trivially_destructible<value_type>());} 1300b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 1310b57cec5SDimitry Andric void __destruct_at_begin(pointer __new_begin, false_type); 1320b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 1330b57cec5SDimitry Andric void __destruct_at_begin(pointer __new_begin, true_type); 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 1360b57cec5SDimitry Andric void __destruct_at_end(pointer __new_last) _NOEXCEPT 1370b57cec5SDimitry Andric {__destruct_at_end(__new_last, false_type());} 1380b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 1390b57cec5SDimitry Andric void __destruct_at_end(pointer __new_last, false_type) _NOEXCEPT; 1400b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 1410b57cec5SDimitry Andric void __destruct_at_end(pointer __new_last, true_type) _NOEXCEPT; 1420b57cec5SDimitry Andric 1430b57cec5SDimitry Andric void swap(__split_buffer& __x) 1440b57cec5SDimitry Andric _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value|| 1450b57cec5SDimitry Andric __is_nothrow_swappable<__alloc_rr>::value); 1460b57cec5SDimitry Andric 1470b57cec5SDimitry Andric bool __invariants() const; 1480b57cec5SDimitry Andric 1490b57cec5SDimitry Andricprivate: 1500b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 1510b57cec5SDimitry Andric void __move_assign_alloc(__split_buffer& __c, true_type) 1520b57cec5SDimitry Andric _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) 1530b57cec5SDimitry Andric { 1540b57cec5SDimitry Andric __alloc() = _VSTD::move(__c.__alloc()); 1550b57cec5SDimitry Andric } 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 1580b57cec5SDimitry Andric void __move_assign_alloc(__split_buffer&, false_type) _NOEXCEPT 1590b57cec5SDimitry Andric {} 160e40139ffSDimitry Andric 161e40139ffSDimitry Andric struct _ConstructTransaction { 162e40139ffSDimitry Andric explicit _ConstructTransaction(pointer* __p, size_type __n) _NOEXCEPT 163e40139ffSDimitry Andric : __pos_(*__p), __end_(*__p + __n), __dest_(__p) { 164e40139ffSDimitry Andric } 165e40139ffSDimitry Andric ~_ConstructTransaction() { 166e40139ffSDimitry Andric *__dest_ = __pos_; 167e40139ffSDimitry Andric } 168e40139ffSDimitry Andric pointer __pos_; 169e40139ffSDimitry Andric const pointer __end_; 170e40139ffSDimitry Andric private: 171e40139ffSDimitry Andric pointer *__dest_; 172e40139ffSDimitry Andric }; 1730b57cec5SDimitry Andric}; 1740b57cec5SDimitry Andric 1750b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 1760b57cec5SDimitry Andricbool 1770b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__invariants() const 1780b57cec5SDimitry Andric{ 1790b57cec5SDimitry Andric if (__first_ == nullptr) 1800b57cec5SDimitry Andric { 1810b57cec5SDimitry Andric if (__begin_ != nullptr) 1820b57cec5SDimitry Andric return false; 1830b57cec5SDimitry Andric if (__end_ != nullptr) 1840b57cec5SDimitry Andric return false; 1850b57cec5SDimitry Andric if (__end_cap() != nullptr) 1860b57cec5SDimitry Andric return false; 1870b57cec5SDimitry Andric } 1880b57cec5SDimitry Andric else 1890b57cec5SDimitry Andric { 1900b57cec5SDimitry Andric if (__begin_ < __first_) 1910b57cec5SDimitry Andric return false; 1920b57cec5SDimitry Andric if (__end_ < __begin_) 1930b57cec5SDimitry Andric return false; 1940b57cec5SDimitry Andric if (__end_cap() < __end_) 1950b57cec5SDimitry Andric return false; 1960b57cec5SDimitry Andric } 1970b57cec5SDimitry Andric return true; 1980b57cec5SDimitry Andric} 1990b57cec5SDimitry Andric 2000b57cec5SDimitry Andric// Default constructs __n objects starting at __end_ 2010b57cec5SDimitry Andric// throws if construction throws 2020b57cec5SDimitry Andric// Precondition: __n > 0 2030b57cec5SDimitry Andric// Precondition: size() + __n <= capacity() 2040b57cec5SDimitry Andric// Postcondition: size() == size() + __n 2050b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 2060b57cec5SDimitry Andricvoid 2070b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n) 2080b57cec5SDimitry Andric{ 209e40139ffSDimitry Andric _ConstructTransaction __tx(&this->__end_, __n); 210e40139ffSDimitry Andric for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) { 211480093f4SDimitry Andric __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__tx.__pos_)); 212e40139ffSDimitry Andric } 2130b57cec5SDimitry Andric} 2140b57cec5SDimitry Andric 2150b57cec5SDimitry Andric// Copy constructs __n objects starting at __end_ from __x 2160b57cec5SDimitry Andric// throws if construction throws 2170b57cec5SDimitry Andric// Precondition: __n > 0 2180b57cec5SDimitry Andric// Precondition: size() + __n <= capacity() 2190b57cec5SDimitry Andric// Postcondition: size() == old size() + __n 2200b57cec5SDimitry Andric// Postcondition: [i] == __x for all i in [size() - __n, __n) 2210b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 2220b57cec5SDimitry Andricvoid 2230b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x) 2240b57cec5SDimitry Andric{ 225e40139ffSDimitry Andric _ConstructTransaction __tx(&this->__end_, __n); 226e40139ffSDimitry Andric for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) { 227e40139ffSDimitry Andric __alloc_traits::construct(this->__alloc(), 228480093f4SDimitry Andric _VSTD::__to_address(__tx.__pos_), __x); 229e40139ffSDimitry Andric } 2300b57cec5SDimitry Andric} 2310b57cec5SDimitry Andric 2320b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 2330b57cec5SDimitry Andrictemplate <class _InputIter> 2340b57cec5SDimitry Andrictypename enable_if 2350b57cec5SDimitry Andric< 236480093f4SDimitry Andric __is_cpp17_input_iterator<_InputIter>::value && 237480093f4SDimitry Andric !__is_cpp17_forward_iterator<_InputIter>::value, 2380b57cec5SDimitry Andric void 2390b57cec5SDimitry Andric>::type 2400b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(_InputIter __first, _InputIter __last) 2410b57cec5SDimitry Andric{ 2420b57cec5SDimitry Andric __alloc_rr& __a = this->__alloc(); 2430b57cec5SDimitry Andric for (; __first != __last; ++__first) 2440b57cec5SDimitry Andric { 2450b57cec5SDimitry Andric if (__end_ == __end_cap()) 2460b57cec5SDimitry Andric { 2470b57cec5SDimitry Andric size_type __old_cap = __end_cap() - __first_; 2480b57cec5SDimitry Andric size_type __new_cap = _VSTD::max<size_type>(2 * __old_cap, 8); 2490b57cec5SDimitry Andric __split_buffer __buf(__new_cap, 0, __a); 2500b57cec5SDimitry Andric for (pointer __p = __begin_; __p != __end_; ++__p, ++__buf.__end_) 2510b57cec5SDimitry Andric __alloc_traits::construct(__buf.__alloc(), 252480093f4SDimitry Andric _VSTD::__to_address(__buf.__end_), _VSTD::move(*__p)); 2530b57cec5SDimitry Andric swap(__buf); 2540b57cec5SDimitry Andric } 255480093f4SDimitry Andric __alloc_traits::construct(__a, _VSTD::__to_address(this->__end_), *__first); 2560b57cec5SDimitry Andric ++this->__end_; 2570b57cec5SDimitry Andric } 2580b57cec5SDimitry Andric} 2590b57cec5SDimitry Andric 2600b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 2610b57cec5SDimitry Andrictemplate <class _ForwardIterator> 2620b57cec5SDimitry Andrictypename enable_if 2630b57cec5SDimitry Andric< 264480093f4SDimitry Andric __is_cpp17_forward_iterator<_ForwardIterator>::value, 2650b57cec5SDimitry Andric void 2660b57cec5SDimitry Andric>::type 2670b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last) 2680b57cec5SDimitry Andric{ 269*e8d8bef9SDimitry Andric _ConstructTransaction __tx(&this->__end_, _VSTD::distance(__first, __last)); 270e40139ffSDimitry Andric for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, ++__first) { 271e40139ffSDimitry Andric __alloc_traits::construct(this->__alloc(), 272480093f4SDimitry Andric _VSTD::__to_address(__tx.__pos_), *__first); 2730b57cec5SDimitry Andric } 2740b57cec5SDimitry Andric} 2750b57cec5SDimitry Andric 2760b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 2770b57cec5SDimitry Andricinline 2780b57cec5SDimitry Andricvoid 2790b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type) 2800b57cec5SDimitry Andric{ 2810b57cec5SDimitry Andric while (__begin_ != __new_begin) 282*e8d8bef9SDimitry Andric __alloc_traits::destroy(__alloc(), _VSTD::__to_address(__begin_++)); 2830b57cec5SDimitry Andric} 2840b57cec5SDimitry Andric 2850b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 2860b57cec5SDimitry Andricinline 2870b57cec5SDimitry Andricvoid 2880b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, true_type) 2890b57cec5SDimitry Andric{ 2900b57cec5SDimitry Andric __begin_ = __new_begin; 2910b57cec5SDimitry Andric} 2920b57cec5SDimitry Andric 2930b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 2940b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 2950b57cec5SDimitry Andricvoid 2960b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT 2970b57cec5SDimitry Andric{ 2980b57cec5SDimitry Andric while (__new_last != __end_) 299*e8d8bef9SDimitry Andric __alloc_traits::destroy(__alloc(), _VSTD::__to_address(--__end_)); 3000b57cec5SDimitry Andric} 3010b57cec5SDimitry Andric 3020b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 3030b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 3040b57cec5SDimitry Andricvoid 3050b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, true_type) _NOEXCEPT 3060b57cec5SDimitry Andric{ 3070b57cec5SDimitry Andric __end_ = __new_last; 3080b57cec5SDimitry Andric} 3090b57cec5SDimitry Andric 3100b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 3110b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __start, __alloc_rr& __a) 3120b57cec5SDimitry Andric : __end_cap_(nullptr, __a) 3130b57cec5SDimitry Andric{ 3140b57cec5SDimitry Andric __first_ = __cap != 0 ? __alloc_traits::allocate(__alloc(), __cap) : nullptr; 3150b57cec5SDimitry Andric __begin_ = __end_ = __first_ + __start; 3160b57cec5SDimitry Andric __end_cap() = __first_ + __cap; 3170b57cec5SDimitry Andric} 3180b57cec5SDimitry Andric 3190b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 3200b57cec5SDimitry Andricinline 3210b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer() 3220b57cec5SDimitry Andric _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value) 323480093f4SDimitry Andric : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __default_init_tag()) 3240b57cec5SDimitry Andric{ 3250b57cec5SDimitry Andric} 3260b57cec5SDimitry Andric 3270b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 3280b57cec5SDimitry Andricinline 3290b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(__alloc_rr& __a) 3300b57cec5SDimitry Andric : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a) 3310b57cec5SDimitry Andric{ 3320b57cec5SDimitry Andric} 3330b57cec5SDimitry Andric 3340b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 3350b57cec5SDimitry Andricinline 3360b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(const __alloc_rr& __a) 3370b57cec5SDimitry Andric : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a) 3380b57cec5SDimitry Andric{ 3390b57cec5SDimitry Andric} 3400b57cec5SDimitry Andric 3410b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 3420b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::~__split_buffer() 3430b57cec5SDimitry Andric{ 3440b57cec5SDimitry Andric clear(); 3450b57cec5SDimitry Andric if (__first_) 3460b57cec5SDimitry Andric __alloc_traits::deallocate(__alloc(), __first_, capacity()); 3470b57cec5SDimitry Andric} 3480b57cec5SDimitry Andric 3490b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 3500b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c) 3510b57cec5SDimitry Andric _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value) 3520b57cec5SDimitry Andric : __first_(_VSTD::move(__c.__first_)), 3530b57cec5SDimitry Andric __begin_(_VSTD::move(__c.__begin_)), 3540b57cec5SDimitry Andric __end_(_VSTD::move(__c.__end_)), 3550b57cec5SDimitry Andric __end_cap_(_VSTD::move(__c.__end_cap_)) 3560b57cec5SDimitry Andric{ 3570b57cec5SDimitry Andric __c.__first_ = nullptr; 3580b57cec5SDimitry Andric __c.__begin_ = nullptr; 3590b57cec5SDimitry Andric __c.__end_ = nullptr; 3600b57cec5SDimitry Andric __c.__end_cap() = nullptr; 3610b57cec5SDimitry Andric} 3620b57cec5SDimitry Andric 3630b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 3640b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a) 365480093f4SDimitry Andric : __end_cap_(nullptr, __a) 3660b57cec5SDimitry Andric{ 3670b57cec5SDimitry Andric if (__a == __c.__alloc()) 3680b57cec5SDimitry Andric { 3690b57cec5SDimitry Andric __first_ = __c.__first_; 3700b57cec5SDimitry Andric __begin_ = __c.__begin_; 3710b57cec5SDimitry Andric __end_ = __c.__end_; 3720b57cec5SDimitry Andric __end_cap() = __c.__end_cap(); 3730b57cec5SDimitry Andric __c.__first_ = nullptr; 3740b57cec5SDimitry Andric __c.__begin_ = nullptr; 3750b57cec5SDimitry Andric __c.__end_ = nullptr; 3760b57cec5SDimitry Andric __c.__end_cap() = nullptr; 3770b57cec5SDimitry Andric } 3780b57cec5SDimitry Andric else 3790b57cec5SDimitry Andric { 3800b57cec5SDimitry Andric size_type __cap = __c.size(); 3810b57cec5SDimitry Andric __first_ = __alloc_traits::allocate(__alloc(), __cap); 3820b57cec5SDimitry Andric __begin_ = __end_ = __first_; 3830b57cec5SDimitry Andric __end_cap() = __first_ + __cap; 3840b57cec5SDimitry Andric typedef move_iterator<iterator> _Ip; 3850b57cec5SDimitry Andric __construct_at_end(_Ip(__c.begin()), _Ip(__c.end())); 3860b57cec5SDimitry Andric } 3870b57cec5SDimitry Andric} 3880b57cec5SDimitry Andric 3890b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 3900b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>& 3910b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c) 3920b57cec5SDimitry Andric _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value && 3930b57cec5SDimitry Andric is_nothrow_move_assignable<allocator_type>::value) || 3940b57cec5SDimitry Andric !__alloc_traits::propagate_on_container_move_assignment::value) 3950b57cec5SDimitry Andric{ 3960b57cec5SDimitry Andric clear(); 3970b57cec5SDimitry Andric shrink_to_fit(); 3980b57cec5SDimitry Andric __first_ = __c.__first_; 3990b57cec5SDimitry Andric __begin_ = __c.__begin_; 4000b57cec5SDimitry Andric __end_ = __c.__end_; 4010b57cec5SDimitry Andric __end_cap() = __c.__end_cap(); 4020b57cec5SDimitry Andric __move_assign_alloc(__c, 4030b57cec5SDimitry Andric integral_constant<bool, 4040b57cec5SDimitry Andric __alloc_traits::propagate_on_container_move_assignment::value>()); 4050b57cec5SDimitry Andric __c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr; 4060b57cec5SDimitry Andric return *this; 4070b57cec5SDimitry Andric} 4080b57cec5SDimitry Andric 4090b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 4100b57cec5SDimitry Andricvoid 4110b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::swap(__split_buffer& __x) 4120b57cec5SDimitry Andric _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value|| 4130b57cec5SDimitry Andric __is_nothrow_swappable<__alloc_rr>::value) 4140b57cec5SDimitry Andric{ 4150b57cec5SDimitry Andric _VSTD::swap(__first_, __x.__first_); 4160b57cec5SDimitry Andric _VSTD::swap(__begin_, __x.__begin_); 4170b57cec5SDimitry Andric _VSTD::swap(__end_, __x.__end_); 4180b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __x.__end_cap()); 419*e8d8bef9SDimitry Andric _VSTD::__swap_allocator(__alloc(), __x.__alloc()); 4200b57cec5SDimitry Andric} 4210b57cec5SDimitry Andric 4220b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 4230b57cec5SDimitry Andricvoid 4240b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::reserve(size_type __n) 4250b57cec5SDimitry Andric{ 4260b57cec5SDimitry Andric if (__n < capacity()) 4270b57cec5SDimitry Andric { 4280b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc()); 4290b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 4300b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 4310b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 4320b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 4330b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 4340b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 4350b57cec5SDimitry Andric } 4360b57cec5SDimitry Andric} 4370b57cec5SDimitry Andric 4380b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 4390b57cec5SDimitry Andricvoid 4400b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT 4410b57cec5SDimitry Andric{ 4420b57cec5SDimitry Andric if (capacity() > size()) 4430b57cec5SDimitry Andric { 4440b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS 4450b57cec5SDimitry Andric try 4460b57cec5SDimitry Andric { 4470b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS 4480b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc()); 4490b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 4500b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 4510b57cec5SDimitry Andric __t.__end_ = __t.__begin_ + (__end_ - __begin_); 4520b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 4530b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 4540b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 4550b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 4560b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS 4570b57cec5SDimitry Andric } 4580b57cec5SDimitry Andric catch (...) 4590b57cec5SDimitry Andric { 4600b57cec5SDimitry Andric } 4610b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS 4620b57cec5SDimitry Andric } 4630b57cec5SDimitry Andric} 4640b57cec5SDimitry Andric 4650b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 4660b57cec5SDimitry Andricvoid 4670b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_front(const_reference __x) 4680b57cec5SDimitry Andric{ 4690b57cec5SDimitry Andric if (__begin_ == __first_) 4700b57cec5SDimitry Andric { 4710b57cec5SDimitry Andric if (__end_ < __end_cap()) 4720b57cec5SDimitry Andric { 4730b57cec5SDimitry Andric difference_type __d = __end_cap() - __end_; 4740b57cec5SDimitry Andric __d = (__d + 1) / 2; 4750b57cec5SDimitry Andric __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d); 4760b57cec5SDimitry Andric __end_ += __d; 4770b57cec5SDimitry Andric } 4780b57cec5SDimitry Andric else 4790b57cec5SDimitry Andric { 4800b57cec5SDimitry Andric size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1); 4810b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc()); 4820b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 4830b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 4840b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 4850b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 4860b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 4870b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 4880b57cec5SDimitry Andric } 4890b57cec5SDimitry Andric } 490480093f4SDimitry Andric __alloc_traits::construct(__alloc(), _VSTD::__to_address(__begin_-1), __x); 4910b57cec5SDimitry Andric --__begin_; 4920b57cec5SDimitry Andric} 4930b57cec5SDimitry Andric 4940b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 4950b57cec5SDimitry Andricvoid 4960b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_front(value_type&& __x) 4970b57cec5SDimitry Andric{ 4980b57cec5SDimitry Andric if (__begin_ == __first_) 4990b57cec5SDimitry Andric { 5000b57cec5SDimitry Andric if (__end_ < __end_cap()) 5010b57cec5SDimitry Andric { 5020b57cec5SDimitry Andric difference_type __d = __end_cap() - __end_; 5030b57cec5SDimitry Andric __d = (__d + 1) / 2; 5040b57cec5SDimitry Andric __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d); 5050b57cec5SDimitry Andric __end_ += __d; 5060b57cec5SDimitry Andric } 5070b57cec5SDimitry Andric else 5080b57cec5SDimitry Andric { 5090b57cec5SDimitry Andric size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1); 5100b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc()); 5110b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 5120b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 5130b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 5140b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 5150b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 5160b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 5170b57cec5SDimitry Andric } 5180b57cec5SDimitry Andric } 519480093f4SDimitry Andric __alloc_traits::construct(__alloc(), _VSTD::__to_address(__begin_-1), 5200b57cec5SDimitry Andric _VSTD::move(__x)); 5210b57cec5SDimitry Andric --__begin_; 5220b57cec5SDimitry Andric} 5230b57cec5SDimitry Andric 5240b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 5250b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 5260b57cec5SDimitry Andricvoid 5270b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_back(const_reference __x) 5280b57cec5SDimitry Andric{ 5290b57cec5SDimitry Andric if (__end_ == __end_cap()) 5300b57cec5SDimitry Andric { 5310b57cec5SDimitry Andric if (__begin_ > __first_) 5320b57cec5SDimitry Andric { 5330b57cec5SDimitry Andric difference_type __d = __begin_ - __first_; 5340b57cec5SDimitry Andric __d = (__d + 1) / 2; 5350b57cec5SDimitry Andric __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d); 5360b57cec5SDimitry Andric __begin_ -= __d; 5370b57cec5SDimitry Andric } 5380b57cec5SDimitry Andric else 5390b57cec5SDimitry Andric { 5400b57cec5SDimitry Andric size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1); 5410b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc()); 5420b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 5430b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 5440b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 5450b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 5460b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 5470b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 5480b57cec5SDimitry Andric } 5490b57cec5SDimitry Andric } 550480093f4SDimitry Andric __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_), __x); 5510b57cec5SDimitry Andric ++__end_; 5520b57cec5SDimitry Andric} 5530b57cec5SDimitry Andric 5540b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 5550b57cec5SDimitry Andricvoid 5560b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_back(value_type&& __x) 5570b57cec5SDimitry Andric{ 5580b57cec5SDimitry Andric if (__end_ == __end_cap()) 5590b57cec5SDimitry Andric { 5600b57cec5SDimitry Andric if (__begin_ > __first_) 5610b57cec5SDimitry Andric { 5620b57cec5SDimitry Andric difference_type __d = __begin_ - __first_; 5630b57cec5SDimitry Andric __d = (__d + 1) / 2; 5640b57cec5SDimitry Andric __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d); 5650b57cec5SDimitry Andric __begin_ -= __d; 5660b57cec5SDimitry Andric } 5670b57cec5SDimitry Andric else 5680b57cec5SDimitry Andric { 5690b57cec5SDimitry Andric size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1); 5700b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc()); 5710b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 5720b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 5730b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 5740b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 5750b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 5760b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 5770b57cec5SDimitry Andric } 5780b57cec5SDimitry Andric } 579480093f4SDimitry Andric __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_), 5800b57cec5SDimitry Andric _VSTD::move(__x)); 5810b57cec5SDimitry Andric ++__end_; 5820b57cec5SDimitry Andric} 5830b57cec5SDimitry Andric 5840b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 5850b57cec5SDimitry Andrictemplate <class... _Args> 5860b57cec5SDimitry Andricvoid 5870b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args) 5880b57cec5SDimitry Andric{ 5890b57cec5SDimitry Andric if (__end_ == __end_cap()) 5900b57cec5SDimitry Andric { 5910b57cec5SDimitry Andric if (__begin_ > __first_) 5920b57cec5SDimitry Andric { 5930b57cec5SDimitry Andric difference_type __d = __begin_ - __first_; 5940b57cec5SDimitry Andric __d = (__d + 1) / 2; 5950b57cec5SDimitry Andric __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d); 5960b57cec5SDimitry Andric __begin_ -= __d; 5970b57cec5SDimitry Andric } 5980b57cec5SDimitry Andric else 5990b57cec5SDimitry Andric { 6000b57cec5SDimitry Andric size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1); 6010b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc()); 6020b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 6030b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 6040b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 6050b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 6060b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 6070b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 6080b57cec5SDimitry Andric } 6090b57cec5SDimitry Andric } 610480093f4SDimitry Andric __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_), 6110b57cec5SDimitry Andric _VSTD::forward<_Args>(__args)...); 6120b57cec5SDimitry Andric ++__end_; 6130b57cec5SDimitry Andric} 6140b57cec5SDimitry Andric 6150b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 6160b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 6170b57cec5SDimitry Andricvoid 6180b57cec5SDimitry Andricswap(__split_buffer<_Tp, _Allocator>& __x, __split_buffer<_Tp, _Allocator>& __y) 6190b57cec5SDimitry Andric _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) 6200b57cec5SDimitry Andric{ 6210b57cec5SDimitry Andric __x.swap(__y); 6220b57cec5SDimitry Andric} 6230b57cec5SDimitry Andric 6240b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD 6250b57cec5SDimitry Andric 6260b57cec5SDimitry Andric_LIBCPP_POP_MACROS 6270b57cec5SDimitry Andric 6280b57cec5SDimitry Andric#endif // _LIBCPP_SPLIT_BUFFER 629