10b57cec5SDimitry Andric// -*- C++ -*- 281ad6265SDimitry Andric//===----------------------------------------------------------------------===// 381ad6265SDimitry Andric// 481ad6265SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 581ad6265SDimitry Andric// See https://llvm.org/LICENSE.txt for license information. 681ad6265SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 781ad6265SDimitry Andric// 881ad6265SDimitry Andric//===----------------------------------------------------------------------===// 90b57cec5SDimitry Andric 1081ad6265SDimitry Andric#ifndef _LIBCPP___SPLIT_BUFFER 1181ad6265SDimitry Andric#define _LIBCPP___SPLIT_BUFFER 1281ad6265SDimitry Andric 1381ad6265SDimitry Andric#include <__algorithm/max.h> 1481ad6265SDimitry Andric#include <__algorithm/move.h> 1581ad6265SDimitry Andric#include <__algorithm/move_backward.h> 160b57cec5SDimitry Andric#include <__config> 1781ad6265SDimitry Andric#include <__iterator/distance.h> 1881ad6265SDimitry Andric#include <__iterator/iterator_traits.h> 1981ad6265SDimitry Andric#include <__iterator/move_iterator.h> 2081ad6265SDimitry Andric#include <__memory/allocator.h> 2181ad6265SDimitry Andric#include <__memory/compressed_pair.h> 22*972a253aSDimitry Andric#include <__memory/swap_allocator.h> 23fe6060f1SDimitry Andric#include <__utility/forward.h> 2481ad6265SDimitry Andric#include <memory> 25fe6060f1SDimitry Andric#include <type_traits> 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 280b57cec5SDimitry Andric# pragma GCC system_header 290b57cec5SDimitry Andric#endif 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS 320b57cec5SDimitry Andric#include <__undef_macros> 330b57cec5SDimitry Andric 340b57cec5SDimitry Andric 350b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD 360b57cec5SDimitry Andric 370b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator = allocator<_Tp> > 380b57cec5SDimitry Andricstruct __split_buffer 390b57cec5SDimitry Andric{ 400b57cec5SDimitry Andricprivate: 410b57cec5SDimitry Andric __split_buffer(const __split_buffer&); 420b57cec5SDimitry Andric __split_buffer& operator=(const __split_buffer&); 430b57cec5SDimitry Andricpublic: 440b57cec5SDimitry Andric typedef _Tp value_type; 450b57cec5SDimitry Andric typedef _Allocator allocator_type; 460b57cec5SDimitry Andric typedef typename remove_reference<allocator_type>::type __alloc_rr; 470b57cec5SDimitry Andric typedef allocator_traits<__alloc_rr> __alloc_traits; 480b57cec5SDimitry Andric typedef value_type& reference; 490b57cec5SDimitry Andric typedef const value_type& const_reference; 500b57cec5SDimitry Andric typedef typename __alloc_traits::size_type size_type; 510b57cec5SDimitry Andric typedef typename __alloc_traits::difference_type difference_type; 520b57cec5SDimitry Andric typedef typename __alloc_traits::pointer pointer; 530b57cec5SDimitry Andric typedef typename __alloc_traits::const_pointer const_pointer; 540b57cec5SDimitry Andric typedef pointer iterator; 550b57cec5SDimitry Andric typedef const_pointer const_iterator; 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric pointer __first_; 580b57cec5SDimitry Andric pointer __begin_; 590b57cec5SDimitry Andric pointer __end_; 600b57cec5SDimitry Andric __compressed_pair<pointer, allocator_type> __end_cap_; 610b57cec5SDimitry Andric 620b57cec5SDimitry Andric typedef typename add_lvalue_reference<allocator_type>::type __alloc_ref; 630b57cec5SDimitry Andric typedef typename add_lvalue_reference<allocator_type>::type __alloc_const_ref; 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY __alloc_rr& __alloc() _NOEXCEPT {return __end_cap_.second();} 660b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY const __alloc_rr& __alloc() const _NOEXCEPT {return __end_cap_.second();} 670b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY pointer& __end_cap() _NOEXCEPT {return __end_cap_.first();} 680b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY const pointer& __end_cap() const _NOEXCEPT {return __end_cap_.first();} 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 710b57cec5SDimitry Andric __split_buffer() 720b57cec5SDimitry Andric _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value); 730b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 740b57cec5SDimitry Andric explicit __split_buffer(__alloc_rr& __a); 750b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 760b57cec5SDimitry Andric explicit __split_buffer(const __alloc_rr& __a); 770b57cec5SDimitry Andric __split_buffer(size_type __cap, size_type __start, __alloc_rr& __a); 780b57cec5SDimitry Andric ~__split_buffer(); 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric __split_buffer(__split_buffer&& __c) 810b57cec5SDimitry Andric _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value); 820b57cec5SDimitry Andric __split_buffer(__split_buffer&& __c, const __alloc_rr& __a); 830b57cec5SDimitry Andric __split_buffer& operator=(__split_buffer&& __c) 840b57cec5SDimitry Andric _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value && 850b57cec5SDimitry Andric is_nothrow_move_assignable<allocator_type>::value) || 860b57cec5SDimitry Andric !__alloc_traits::propagate_on_container_move_assignment::value); 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT {return __begin_;} 890b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT {return __begin_;} 900b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT {return __end_;} 910b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT {return __end_;} 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 940b57cec5SDimitry Andric void clear() _NOEXCEPT 950b57cec5SDimitry Andric {__destruct_at_end(__begin_);} 960b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY size_type size() const {return static_cast<size_type>(__end_ - __begin_);} 970b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY bool empty() const {return __end_ == __begin_;} 980b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY size_type capacity() const {return static_cast<size_type>(__end_cap() - __first_);} 990b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY size_type __front_spare() const {return static_cast<size_type>(__begin_ - __first_);} 1000b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY size_type __back_spare() const {return static_cast<size_type>(__end_cap() - __end_);} 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY reference front() {return *__begin_;} 1030b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY const_reference front() const {return *__begin_;} 1040b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY reference back() {return *(__end_ - 1);} 1050b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY const_reference back() const {return *(__end_ - 1);} 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric void reserve(size_type __n); 1080b57cec5SDimitry Andric void shrink_to_fit() _NOEXCEPT; 1090b57cec5SDimitry Andric void push_front(const_reference __x); 1100b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x); 1110b57cec5SDimitry Andric void push_front(value_type&& __x); 1120b57cec5SDimitry Andric void push_back(value_type&& __x); 1130b57cec5SDimitry Andric template <class... _Args> 1140b57cec5SDimitry Andric void emplace_back(_Args&&... __args); 1150b57cec5SDimitry Andric 1160b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY void pop_front() {__destruct_at_begin(__begin_+1);} 1170b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY void pop_back() {__destruct_at_end(__end_-1);} 1180b57cec5SDimitry Andric 1190b57cec5SDimitry Andric void __construct_at_end(size_type __n); 1200b57cec5SDimitry Andric void __construct_at_end(size_type __n, const_reference __x); 1210b57cec5SDimitry Andric template <class _InputIter> 122753f127fSDimitry Andric __enable_if_t<__is_exactly_cpp17_input_iterator<_InputIter>::value> 1230b57cec5SDimitry Andric __construct_at_end(_InputIter __first, _InputIter __last); 1240b57cec5SDimitry Andric template <class _ForwardIterator> 125753f127fSDimitry Andric __enable_if_t<__is_cpp17_forward_iterator<_ForwardIterator>::value> 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> 234753f127fSDimitry Andric__enable_if_t<__is_exactly_cpp17_input_iterator<_InputIter>::value> 2350b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(_InputIter __first, _InputIter __last) 2360b57cec5SDimitry Andric{ 2370b57cec5SDimitry Andric __alloc_rr& __a = this->__alloc(); 2380b57cec5SDimitry Andric for (; __first != __last; ++__first) 2390b57cec5SDimitry Andric { 2400b57cec5SDimitry Andric if (__end_ == __end_cap()) 2410b57cec5SDimitry Andric { 2420b57cec5SDimitry Andric size_type __old_cap = __end_cap() - __first_; 2430b57cec5SDimitry Andric size_type __new_cap = _VSTD::max<size_type>(2 * __old_cap, 8); 2440b57cec5SDimitry Andric __split_buffer __buf(__new_cap, 0, __a); 245349cc55cSDimitry Andric for (pointer __p = __begin_; __p != __end_; ++__p, (void) ++__buf.__end_) 2460b57cec5SDimitry Andric __alloc_traits::construct(__buf.__alloc(), 247480093f4SDimitry Andric _VSTD::__to_address(__buf.__end_), _VSTD::move(*__p)); 2480b57cec5SDimitry Andric swap(__buf); 2490b57cec5SDimitry Andric } 250480093f4SDimitry Andric __alloc_traits::construct(__a, _VSTD::__to_address(this->__end_), *__first); 2510b57cec5SDimitry Andric ++this->__end_; 2520b57cec5SDimitry Andric } 2530b57cec5SDimitry Andric} 2540b57cec5SDimitry Andric 2550b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 2560b57cec5SDimitry Andrictemplate <class _ForwardIterator> 257753f127fSDimitry Andric__enable_if_t<__is_cpp17_forward_iterator<_ForwardIterator>::value> 2580b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last) 2590b57cec5SDimitry Andric{ 260e8d8bef9SDimitry Andric _ConstructTransaction __tx(&this->__end_, _VSTD::distance(__first, __last)); 261349cc55cSDimitry Andric for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void) ++__first) { 262e40139ffSDimitry Andric __alloc_traits::construct(this->__alloc(), 263480093f4SDimitry Andric _VSTD::__to_address(__tx.__pos_), *__first); 2640b57cec5SDimitry Andric } 2650b57cec5SDimitry Andric} 2660b57cec5SDimitry Andric 2670b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 2680b57cec5SDimitry Andricinline 2690b57cec5SDimitry Andricvoid 2700b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type) 2710b57cec5SDimitry Andric{ 2720b57cec5SDimitry Andric while (__begin_ != __new_begin) 273e8d8bef9SDimitry Andric __alloc_traits::destroy(__alloc(), _VSTD::__to_address(__begin_++)); 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, true_type) 2800b57cec5SDimitry Andric{ 2810b57cec5SDimitry Andric __begin_ = __new_begin; 2820b57cec5SDimitry Andric} 2830b57cec5SDimitry Andric 2840b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 2850b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 2860b57cec5SDimitry Andricvoid 2870b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT 2880b57cec5SDimitry Andric{ 2890b57cec5SDimitry Andric while (__new_last != __end_) 290e8d8bef9SDimitry Andric __alloc_traits::destroy(__alloc(), _VSTD::__to_address(--__end_)); 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, true_type) _NOEXCEPT 2970b57cec5SDimitry Andric{ 2980b57cec5SDimitry Andric __end_ = __new_last; 2990b57cec5SDimitry Andric} 3000b57cec5SDimitry Andric 3010b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 3020b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __start, __alloc_rr& __a) 3030b57cec5SDimitry Andric : __end_cap_(nullptr, __a) 3040b57cec5SDimitry Andric{ 30581ad6265SDimitry Andric if (__cap == 0) { 30681ad6265SDimitry Andric __first_ = nullptr; 30781ad6265SDimitry Andric } else { 30881ad6265SDimitry Andric auto __allocation = std::__allocate_at_least(__alloc(), __cap); 30981ad6265SDimitry Andric __first_ = __allocation.ptr; 31081ad6265SDimitry Andric __cap = __allocation.count; 31181ad6265SDimitry Andric } 3120b57cec5SDimitry Andric __begin_ = __end_ = __first_ + __start; 3130b57cec5SDimitry Andric __end_cap() = __first_ + __cap; 3140b57cec5SDimitry Andric} 3150b57cec5SDimitry Andric 3160b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 3170b57cec5SDimitry Andricinline 3180b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer() 3190b57cec5SDimitry Andric _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value) 320480093f4SDimitry Andric : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __default_init_tag()) 3210b57cec5SDimitry Andric{ 3220b57cec5SDimitry Andric} 3230b57cec5SDimitry Andric 3240b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 3250b57cec5SDimitry Andricinline 3260b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(__alloc_rr& __a) 3270b57cec5SDimitry Andric : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a) 3280b57cec5SDimitry Andric{ 3290b57cec5SDimitry Andric} 3300b57cec5SDimitry Andric 3310b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 3320b57cec5SDimitry Andricinline 3330b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(const __alloc_rr& __a) 3340b57cec5SDimitry Andric : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a) 3350b57cec5SDimitry Andric{ 3360b57cec5SDimitry Andric} 3370b57cec5SDimitry Andric 3380b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 3390b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::~__split_buffer() 3400b57cec5SDimitry Andric{ 3410b57cec5SDimitry Andric clear(); 3420b57cec5SDimitry Andric if (__first_) 3430b57cec5SDimitry Andric __alloc_traits::deallocate(__alloc(), __first_, capacity()); 3440b57cec5SDimitry Andric} 3450b57cec5SDimitry Andric 3460b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 3470b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c) 3480b57cec5SDimitry Andric _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value) 3490b57cec5SDimitry Andric : __first_(_VSTD::move(__c.__first_)), 3500b57cec5SDimitry Andric __begin_(_VSTD::move(__c.__begin_)), 3510b57cec5SDimitry Andric __end_(_VSTD::move(__c.__end_)), 3520b57cec5SDimitry Andric __end_cap_(_VSTD::move(__c.__end_cap_)) 3530b57cec5SDimitry Andric{ 3540b57cec5SDimitry Andric __c.__first_ = nullptr; 3550b57cec5SDimitry Andric __c.__begin_ = nullptr; 3560b57cec5SDimitry Andric __c.__end_ = nullptr; 3570b57cec5SDimitry Andric __c.__end_cap() = nullptr; 3580b57cec5SDimitry Andric} 3590b57cec5SDimitry Andric 3600b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 3610b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a) 362480093f4SDimitry Andric : __end_cap_(nullptr, __a) 3630b57cec5SDimitry Andric{ 3640b57cec5SDimitry Andric if (__a == __c.__alloc()) 3650b57cec5SDimitry Andric { 3660b57cec5SDimitry Andric __first_ = __c.__first_; 3670b57cec5SDimitry Andric __begin_ = __c.__begin_; 3680b57cec5SDimitry Andric __end_ = __c.__end_; 3690b57cec5SDimitry Andric __end_cap() = __c.__end_cap(); 3700b57cec5SDimitry Andric __c.__first_ = nullptr; 3710b57cec5SDimitry Andric __c.__begin_ = nullptr; 3720b57cec5SDimitry Andric __c.__end_ = nullptr; 3730b57cec5SDimitry Andric __c.__end_cap() = nullptr; 3740b57cec5SDimitry Andric } 3750b57cec5SDimitry Andric else 3760b57cec5SDimitry Andric { 37781ad6265SDimitry Andric auto __allocation = std::__allocate_at_least(__alloc(), __c.size()); 37881ad6265SDimitry Andric __first_ = __allocation.ptr; 3790b57cec5SDimitry Andric __begin_ = __end_ = __first_; 38081ad6265SDimitry Andric __end_cap() = __first_ + __allocation.count; 3810b57cec5SDimitry Andric typedef move_iterator<iterator> _Ip; 3820b57cec5SDimitry Andric __construct_at_end(_Ip(__c.begin()), _Ip(__c.end())); 3830b57cec5SDimitry Andric } 3840b57cec5SDimitry Andric} 3850b57cec5SDimitry Andric 3860b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 3870b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>& 3880b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c) 3890b57cec5SDimitry Andric _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value && 3900b57cec5SDimitry Andric is_nothrow_move_assignable<allocator_type>::value) || 3910b57cec5SDimitry Andric !__alloc_traits::propagate_on_container_move_assignment::value) 3920b57cec5SDimitry Andric{ 3930b57cec5SDimitry Andric clear(); 3940b57cec5SDimitry Andric shrink_to_fit(); 3950b57cec5SDimitry Andric __first_ = __c.__first_; 3960b57cec5SDimitry Andric __begin_ = __c.__begin_; 3970b57cec5SDimitry Andric __end_ = __c.__end_; 3980b57cec5SDimitry Andric __end_cap() = __c.__end_cap(); 3990b57cec5SDimitry Andric __move_assign_alloc(__c, 4000b57cec5SDimitry Andric integral_constant<bool, 4010b57cec5SDimitry Andric __alloc_traits::propagate_on_container_move_assignment::value>()); 4020b57cec5SDimitry Andric __c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr; 4030b57cec5SDimitry Andric return *this; 4040b57cec5SDimitry Andric} 4050b57cec5SDimitry Andric 4060b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 4070b57cec5SDimitry Andricvoid 4080b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::swap(__split_buffer& __x) 4090b57cec5SDimitry Andric _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value|| 4100b57cec5SDimitry Andric __is_nothrow_swappable<__alloc_rr>::value) 4110b57cec5SDimitry Andric{ 4120b57cec5SDimitry Andric _VSTD::swap(__first_, __x.__first_); 4130b57cec5SDimitry Andric _VSTD::swap(__begin_, __x.__begin_); 4140b57cec5SDimitry Andric _VSTD::swap(__end_, __x.__end_); 4150b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __x.__end_cap()); 416e8d8bef9SDimitry Andric _VSTD::__swap_allocator(__alloc(), __x.__alloc()); 4170b57cec5SDimitry Andric} 4180b57cec5SDimitry Andric 4190b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 4200b57cec5SDimitry Andricvoid 4210b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::reserve(size_type __n) 4220b57cec5SDimitry Andric{ 4230b57cec5SDimitry Andric if (__n < capacity()) 4240b57cec5SDimitry Andric { 4250b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc()); 4260b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 4270b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 4280b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 4290b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 4300b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 4310b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 4320b57cec5SDimitry Andric } 4330b57cec5SDimitry Andric} 4340b57cec5SDimitry Andric 4350b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 4360b57cec5SDimitry Andricvoid 4370b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT 4380b57cec5SDimitry Andric{ 4390b57cec5SDimitry Andric if (capacity() > size()) 4400b57cec5SDimitry Andric { 4410b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS 4420b57cec5SDimitry Andric try 4430b57cec5SDimitry Andric { 4440b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS 4450b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc()); 4460b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 4470b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 4480b57cec5SDimitry Andric __t.__end_ = __t.__begin_ + (__end_ - __begin_); 4490b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 4500b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 4510b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 4520b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 4530b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS 4540b57cec5SDimitry Andric } 4550b57cec5SDimitry Andric catch (...) 4560b57cec5SDimitry Andric { 4570b57cec5SDimitry Andric } 4580b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS 4590b57cec5SDimitry Andric } 4600b57cec5SDimitry Andric} 4610b57cec5SDimitry Andric 4620b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 4630b57cec5SDimitry Andricvoid 4640b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_front(const_reference __x) 4650b57cec5SDimitry Andric{ 4660b57cec5SDimitry Andric if (__begin_ == __first_) 4670b57cec5SDimitry Andric { 4680b57cec5SDimitry Andric if (__end_ < __end_cap()) 4690b57cec5SDimitry Andric { 4700b57cec5SDimitry Andric difference_type __d = __end_cap() - __end_; 4710b57cec5SDimitry Andric __d = (__d + 1) / 2; 4720b57cec5SDimitry Andric __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d); 4730b57cec5SDimitry Andric __end_ += __d; 4740b57cec5SDimitry Andric } 4750b57cec5SDimitry Andric else 4760b57cec5SDimitry Andric { 4770b57cec5SDimitry Andric size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1); 4780b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc()); 4790b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 4800b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 4810b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 4820b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 4830b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 4840b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 4850b57cec5SDimitry Andric } 4860b57cec5SDimitry Andric } 487480093f4SDimitry Andric __alloc_traits::construct(__alloc(), _VSTD::__to_address(__begin_-1), __x); 4880b57cec5SDimitry Andric --__begin_; 4890b57cec5SDimitry Andric} 4900b57cec5SDimitry Andric 4910b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 4920b57cec5SDimitry Andricvoid 4930b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_front(value_type&& __x) 4940b57cec5SDimitry Andric{ 4950b57cec5SDimitry Andric if (__begin_ == __first_) 4960b57cec5SDimitry Andric { 4970b57cec5SDimitry Andric if (__end_ < __end_cap()) 4980b57cec5SDimitry Andric { 4990b57cec5SDimitry Andric difference_type __d = __end_cap() - __end_; 5000b57cec5SDimitry Andric __d = (__d + 1) / 2; 5010b57cec5SDimitry Andric __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d); 5020b57cec5SDimitry Andric __end_ += __d; 5030b57cec5SDimitry Andric } 5040b57cec5SDimitry Andric else 5050b57cec5SDimitry Andric { 5060b57cec5SDimitry Andric size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1); 5070b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc()); 5080b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 5090b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 5100b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 5110b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 5120b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 5130b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 5140b57cec5SDimitry Andric } 5150b57cec5SDimitry Andric } 516480093f4SDimitry Andric __alloc_traits::construct(__alloc(), _VSTD::__to_address(__begin_-1), 5170b57cec5SDimitry Andric _VSTD::move(__x)); 5180b57cec5SDimitry Andric --__begin_; 5190b57cec5SDimitry Andric} 5200b57cec5SDimitry Andric 5210b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 5220b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 5230b57cec5SDimitry Andricvoid 5240b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_back(const_reference __x) 5250b57cec5SDimitry Andric{ 5260b57cec5SDimitry Andric if (__end_ == __end_cap()) 5270b57cec5SDimitry Andric { 5280b57cec5SDimitry Andric if (__begin_ > __first_) 5290b57cec5SDimitry Andric { 5300b57cec5SDimitry Andric difference_type __d = __begin_ - __first_; 5310b57cec5SDimitry Andric __d = (__d + 1) / 2; 5320b57cec5SDimitry Andric __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d); 5330b57cec5SDimitry Andric __begin_ -= __d; 5340b57cec5SDimitry Andric } 5350b57cec5SDimitry Andric else 5360b57cec5SDimitry Andric { 5370b57cec5SDimitry Andric size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1); 5380b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc()); 5390b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 5400b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 5410b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 5420b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 5430b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 5440b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 5450b57cec5SDimitry Andric } 5460b57cec5SDimitry Andric } 547480093f4SDimitry Andric __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_), __x); 5480b57cec5SDimitry Andric ++__end_; 5490b57cec5SDimitry Andric} 5500b57cec5SDimitry Andric 5510b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 5520b57cec5SDimitry Andricvoid 5530b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_back(value_type&& __x) 5540b57cec5SDimitry Andric{ 5550b57cec5SDimitry Andric if (__end_ == __end_cap()) 5560b57cec5SDimitry Andric { 5570b57cec5SDimitry Andric if (__begin_ > __first_) 5580b57cec5SDimitry Andric { 5590b57cec5SDimitry Andric difference_type __d = __begin_ - __first_; 5600b57cec5SDimitry Andric __d = (__d + 1) / 2; 5610b57cec5SDimitry Andric __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d); 5620b57cec5SDimitry Andric __begin_ -= __d; 5630b57cec5SDimitry Andric } 5640b57cec5SDimitry Andric else 5650b57cec5SDimitry Andric { 5660b57cec5SDimitry Andric size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1); 5670b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc()); 5680b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 5690b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 5700b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 5710b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 5720b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 5730b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 5740b57cec5SDimitry Andric } 5750b57cec5SDimitry Andric } 576480093f4SDimitry Andric __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_), 5770b57cec5SDimitry Andric _VSTD::move(__x)); 5780b57cec5SDimitry Andric ++__end_; 5790b57cec5SDimitry Andric} 5800b57cec5SDimitry Andric 5810b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 5820b57cec5SDimitry Andrictemplate <class... _Args> 5830b57cec5SDimitry Andricvoid 5840b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args) 5850b57cec5SDimitry Andric{ 5860b57cec5SDimitry Andric if (__end_ == __end_cap()) 5870b57cec5SDimitry Andric { 5880b57cec5SDimitry Andric if (__begin_ > __first_) 5890b57cec5SDimitry Andric { 5900b57cec5SDimitry Andric difference_type __d = __begin_ - __first_; 5910b57cec5SDimitry Andric __d = (__d + 1) / 2; 5920b57cec5SDimitry Andric __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d); 5930b57cec5SDimitry Andric __begin_ -= __d; 5940b57cec5SDimitry Andric } 5950b57cec5SDimitry Andric else 5960b57cec5SDimitry Andric { 5970b57cec5SDimitry Andric size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1); 5980b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc()); 5990b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 6000b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 6010b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 6020b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 6030b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 6040b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 6050b57cec5SDimitry Andric } 6060b57cec5SDimitry Andric } 607480093f4SDimitry Andric __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_), 6080b57cec5SDimitry Andric _VSTD::forward<_Args>(__args)...); 6090b57cec5SDimitry Andric ++__end_; 6100b57cec5SDimitry Andric} 6110b57cec5SDimitry Andric 6120b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 6130b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 6140b57cec5SDimitry Andricvoid 6150b57cec5SDimitry Andricswap(__split_buffer<_Tp, _Allocator>& __x, __split_buffer<_Tp, _Allocator>& __y) 6160b57cec5SDimitry Andric _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) 6170b57cec5SDimitry Andric{ 6180b57cec5SDimitry Andric __x.swap(__y); 6190b57cec5SDimitry Andric} 6200b57cec5SDimitry Andric 6210b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD 6220b57cec5SDimitry Andric 6230b57cec5SDimitry Andric_LIBCPP_POP_MACROS 6240b57cec5SDimitry Andric 62581ad6265SDimitry Andric#endif // _LIBCPP___SPLIT_BUFFER 626