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