xref: /freebsd/contrib/llvm-project/libcxx/include/__split_buffer (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
10b57cec5SDimitry Andric// -*- C++ -*-
2*81ad6265SDimitry Andric//===----------------------------------------------------------------------===//
3*81ad6265SDimitry Andric//
4*81ad6265SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5*81ad6265SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
6*81ad6265SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7*81ad6265SDimitry Andric//
8*81ad6265SDimitry Andric//===----------------------------------------------------------------------===//
90b57cec5SDimitry Andric
10*81ad6265SDimitry Andric#ifndef _LIBCPP___SPLIT_BUFFER
11*81ad6265SDimitry Andric#define _LIBCPP___SPLIT_BUFFER
12*81ad6265SDimitry Andric
13*81ad6265SDimitry Andric#include <__algorithm/max.h>
14*81ad6265SDimitry Andric#include <__algorithm/move.h>
15*81ad6265SDimitry Andric#include <__algorithm/move_backward.h>
160b57cec5SDimitry Andric#include <__config>
17*81ad6265SDimitry Andric#include <__iterator/distance.h>
18*81ad6265SDimitry Andric#include <__iterator/iterator_traits.h>
19*81ad6265SDimitry Andric#include <__iterator/move_iterator.h>
20*81ad6265SDimitry Andric#include <__memory/allocator.h>
21*81ad6265SDimitry Andric#include <__memory/compressed_pair.h>
22fe6060f1SDimitry Andric#include <__utility/forward.h>
23*81ad6265SDimitry Andric#include <memory>
24fe6060f1SDimitry Andric#include <type_traits>
250b57cec5SDimitry Andric
260b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
270b57cec5SDimitry Andric#  pragma GCC system_header
280b57cec5SDimitry Andric#endif
290b57cec5SDimitry Andric
300b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS
310b57cec5SDimitry Andric#include <__undef_macros>
320b57cec5SDimitry Andric
330b57cec5SDimitry Andric
340b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
350b57cec5SDimitry Andric
360b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator = allocator<_Tp> >
370b57cec5SDimitry Andricstruct __split_buffer
380b57cec5SDimitry Andric{
390b57cec5SDimitry Andricprivate:
400b57cec5SDimitry Andric    __split_buffer(const __split_buffer&);
410b57cec5SDimitry Andric    __split_buffer& operator=(const __split_buffer&);
420b57cec5SDimitry Andricpublic:
430b57cec5SDimitry Andric    typedef _Tp                                             value_type;
440b57cec5SDimitry Andric    typedef _Allocator                                      allocator_type;
450b57cec5SDimitry Andric    typedef typename remove_reference<allocator_type>::type __alloc_rr;
460b57cec5SDimitry Andric    typedef allocator_traits<__alloc_rr>                    __alloc_traits;
470b57cec5SDimitry Andric    typedef value_type&                                     reference;
480b57cec5SDimitry Andric    typedef const value_type&                               const_reference;
490b57cec5SDimitry Andric    typedef typename __alloc_traits::size_type              size_type;
500b57cec5SDimitry Andric    typedef typename __alloc_traits::difference_type        difference_type;
510b57cec5SDimitry Andric    typedef typename __alloc_traits::pointer                pointer;
520b57cec5SDimitry Andric    typedef typename __alloc_traits::const_pointer          const_pointer;
530b57cec5SDimitry Andric    typedef pointer                                         iterator;
540b57cec5SDimitry Andric    typedef const_pointer                                   const_iterator;
550b57cec5SDimitry Andric
560b57cec5SDimitry Andric    pointer                                         __first_;
570b57cec5SDimitry Andric    pointer                                         __begin_;
580b57cec5SDimitry Andric    pointer                                         __end_;
590b57cec5SDimitry Andric    __compressed_pair<pointer, allocator_type> __end_cap_;
600b57cec5SDimitry Andric
610b57cec5SDimitry Andric    typedef typename add_lvalue_reference<allocator_type>::type __alloc_ref;
620b57cec5SDimitry Andric    typedef typename add_lvalue_reference<allocator_type>::type __alloc_const_ref;
630b57cec5SDimitry Andric
640b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __alloc_rr&           __alloc() _NOEXCEPT         {return __end_cap_.second();}
650b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY const __alloc_rr&     __alloc() const _NOEXCEPT   {return __end_cap_.second();}
660b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY pointer&              __end_cap() _NOEXCEPT       {return __end_cap_.first();}
670b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY const pointer&        __end_cap() const _NOEXCEPT {return __end_cap_.first();}
680b57cec5SDimitry Andric
690b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
700b57cec5SDimitry Andric    __split_buffer()
710b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
720b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
730b57cec5SDimitry Andric    explicit __split_buffer(__alloc_rr& __a);
740b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
750b57cec5SDimitry Andric    explicit __split_buffer(const __alloc_rr& __a);
760b57cec5SDimitry Andric    __split_buffer(size_type __cap, size_type __start, __alloc_rr& __a);
770b57cec5SDimitry Andric    ~__split_buffer();
780b57cec5SDimitry Andric
790b57cec5SDimitry Andric    __split_buffer(__split_buffer&& __c)
800b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
810b57cec5SDimitry Andric    __split_buffer(__split_buffer&& __c, const __alloc_rr& __a);
820b57cec5SDimitry Andric    __split_buffer& operator=(__split_buffer&& __c)
830b57cec5SDimitry Andric        _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
840b57cec5SDimitry Andric                is_nothrow_move_assignable<allocator_type>::value) ||
850b57cec5SDimitry Andric               !__alloc_traits::propagate_on_container_move_assignment::value);
860b57cec5SDimitry Andric
870b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY       iterator begin() _NOEXCEPT       {return __begin_;}
880b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT {return __begin_;}
890b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY       iterator end() _NOEXCEPT         {return __end_;}
900b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT   {return __end_;}
910b57cec5SDimitry Andric
920b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
930b57cec5SDimitry Andric    void clear() _NOEXCEPT
940b57cec5SDimitry Andric        {__destruct_at_end(__begin_);}
950b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY size_type size() const {return static_cast<size_type>(__end_ - __begin_);}
960b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY bool empty()     const {return __end_ == __begin_;}
970b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY size_type capacity() const {return static_cast<size_type>(__end_cap() - __first_);}
980b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY size_type __front_spare() const {return static_cast<size_type>(__begin_ - __first_);}
990b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY size_type __back_spare() const {return static_cast<size_type>(__end_cap() - __end_);}
1000b57cec5SDimitry Andric
1010b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY       reference front()       {return *__begin_;}
1020b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY const_reference front() const {return *__begin_;}
1030b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY       reference back()        {return *(__end_ - 1);}
1040b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY const_reference back() const  {return *(__end_ - 1);}
1050b57cec5SDimitry Andric
1060b57cec5SDimitry Andric    void reserve(size_type __n);
1070b57cec5SDimitry Andric    void shrink_to_fit() _NOEXCEPT;
1080b57cec5SDimitry Andric    void push_front(const_reference __x);
1090b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
1100b57cec5SDimitry Andric    void push_front(value_type&& __x);
1110b57cec5SDimitry Andric    void push_back(value_type&& __x);
1120b57cec5SDimitry Andric    template <class... _Args>
1130b57cec5SDimitry Andric        void emplace_back(_Args&&... __args);
1140b57cec5SDimitry Andric
1150b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void pop_front() {__destruct_at_begin(__begin_+1);}
1160b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void pop_back() {__destruct_at_end(__end_-1);}
1170b57cec5SDimitry Andric
1180b57cec5SDimitry Andric    void __construct_at_end(size_type __n);
1190b57cec5SDimitry Andric    void __construct_at_end(size_type __n, const_reference __x);
1200b57cec5SDimitry Andric    template <class _InputIter>
1210b57cec5SDimitry Andric        typename enable_if
1220b57cec5SDimitry Andric        <
123480093f4SDimitry Andric            __is_cpp17_input_iterator<_InputIter>::value &&
124480093f4SDimitry Andric           !__is_cpp17_forward_iterator<_InputIter>::value,
1250b57cec5SDimitry Andric            void
1260b57cec5SDimitry Andric        >::type
1270b57cec5SDimitry Andric        __construct_at_end(_InputIter __first, _InputIter __last);
1280b57cec5SDimitry Andric    template <class _ForwardIterator>
1290b57cec5SDimitry Andric        typename enable_if
1300b57cec5SDimitry Andric        <
131480093f4SDimitry Andric            __is_cpp17_forward_iterator<_ForwardIterator>::value,
1320b57cec5SDimitry Andric            void
1330b57cec5SDimitry Andric        >::type
1340b57cec5SDimitry Andric        __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
1350b57cec5SDimitry Andric
1360b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void __destruct_at_begin(pointer __new_begin)
1370b57cec5SDimitry Andric        {__destruct_at_begin(__new_begin, is_trivially_destructible<value_type>());}
1380b57cec5SDimitry Andric        _LIBCPP_INLINE_VISIBILITY
1390b57cec5SDimitry Andric        void __destruct_at_begin(pointer __new_begin, false_type);
1400b57cec5SDimitry Andric        _LIBCPP_INLINE_VISIBILITY
1410b57cec5SDimitry Andric        void __destruct_at_begin(pointer __new_begin, true_type);
1420b57cec5SDimitry Andric
1430b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1440b57cec5SDimitry Andric    void __destruct_at_end(pointer __new_last) _NOEXCEPT
1450b57cec5SDimitry Andric        {__destruct_at_end(__new_last, false_type());}
1460b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1470b57cec5SDimitry Andric        void __destruct_at_end(pointer __new_last, false_type) _NOEXCEPT;
1480b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1490b57cec5SDimitry Andric        void __destruct_at_end(pointer __new_last, true_type) _NOEXCEPT;
1500b57cec5SDimitry Andric
1510b57cec5SDimitry Andric    void swap(__split_buffer& __x)
1520b57cec5SDimitry Andric        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
1530b57cec5SDimitry Andric                   __is_nothrow_swappable<__alloc_rr>::value);
1540b57cec5SDimitry Andric
1550b57cec5SDimitry Andric    bool __invariants() const;
1560b57cec5SDimitry Andric
1570b57cec5SDimitry Andricprivate:
1580b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1590b57cec5SDimitry Andric    void __move_assign_alloc(__split_buffer& __c, true_type)
1600b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1610b57cec5SDimitry Andric        {
1620b57cec5SDimitry Andric            __alloc() = _VSTD::move(__c.__alloc());
1630b57cec5SDimitry Andric        }
1640b57cec5SDimitry Andric
1650b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1660b57cec5SDimitry Andric    void __move_assign_alloc(__split_buffer&, false_type) _NOEXCEPT
1670b57cec5SDimitry Andric        {}
168e40139ffSDimitry Andric
169e40139ffSDimitry Andric    struct _ConstructTransaction {
170e40139ffSDimitry Andric      explicit _ConstructTransaction(pointer* __p, size_type __n) _NOEXCEPT
171e40139ffSDimitry Andric      : __pos_(*__p), __end_(*__p + __n), __dest_(__p) {
172e40139ffSDimitry Andric      }
173e40139ffSDimitry Andric      ~_ConstructTransaction() {
174e40139ffSDimitry Andric        *__dest_ = __pos_;
175e40139ffSDimitry Andric      }
176e40139ffSDimitry Andric      pointer __pos_;
177e40139ffSDimitry Andric     const pointer __end_;
178e40139ffSDimitry Andric    private:
179e40139ffSDimitry Andric     pointer *__dest_;
180e40139ffSDimitry Andric    };
1810b57cec5SDimitry Andric};
1820b57cec5SDimitry Andric
1830b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1840b57cec5SDimitry Andricbool
1850b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__invariants() const
1860b57cec5SDimitry Andric{
1870b57cec5SDimitry Andric    if (__first_ == nullptr)
1880b57cec5SDimitry Andric    {
1890b57cec5SDimitry Andric        if (__begin_ != nullptr)
1900b57cec5SDimitry Andric            return false;
1910b57cec5SDimitry Andric        if (__end_ != nullptr)
1920b57cec5SDimitry Andric            return false;
1930b57cec5SDimitry Andric        if (__end_cap() != nullptr)
1940b57cec5SDimitry Andric            return false;
1950b57cec5SDimitry Andric    }
1960b57cec5SDimitry Andric    else
1970b57cec5SDimitry Andric    {
1980b57cec5SDimitry Andric        if (__begin_ < __first_)
1990b57cec5SDimitry Andric            return false;
2000b57cec5SDimitry Andric        if (__end_ < __begin_)
2010b57cec5SDimitry Andric            return false;
2020b57cec5SDimitry Andric        if (__end_cap() < __end_)
2030b57cec5SDimitry Andric            return false;
2040b57cec5SDimitry Andric    }
2050b57cec5SDimitry Andric    return true;
2060b57cec5SDimitry Andric}
2070b57cec5SDimitry Andric
2080b57cec5SDimitry Andric//  Default constructs __n objects starting at __end_
2090b57cec5SDimitry Andric//  throws if construction throws
2100b57cec5SDimitry Andric//  Precondition:  __n > 0
2110b57cec5SDimitry Andric//  Precondition:  size() + __n <= capacity()
2120b57cec5SDimitry Andric//  Postcondition:  size() == size() + __n
2130b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
2140b57cec5SDimitry Andricvoid
2150b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n)
2160b57cec5SDimitry Andric{
217e40139ffSDimitry Andric    _ConstructTransaction __tx(&this->__end_, __n);
218e40139ffSDimitry Andric    for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
219480093f4SDimitry Andric        __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__tx.__pos_));
220e40139ffSDimitry Andric    }
2210b57cec5SDimitry Andric}
2220b57cec5SDimitry Andric
2230b57cec5SDimitry Andric//  Copy constructs __n objects starting at __end_ from __x
2240b57cec5SDimitry Andric//  throws if construction throws
2250b57cec5SDimitry Andric//  Precondition:  __n > 0
2260b57cec5SDimitry Andric//  Precondition:  size() + __n <= capacity()
2270b57cec5SDimitry Andric//  Postcondition:  size() == old size() + __n
2280b57cec5SDimitry Andric//  Postcondition:  [i] == __x for all i in [size() - __n, __n)
2290b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
2300b57cec5SDimitry Andricvoid
2310b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
2320b57cec5SDimitry Andric{
233e40139ffSDimitry Andric    _ConstructTransaction __tx(&this->__end_, __n);
234e40139ffSDimitry Andric    for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
235e40139ffSDimitry Andric        __alloc_traits::construct(this->__alloc(),
236480093f4SDimitry Andric            _VSTD::__to_address(__tx.__pos_), __x);
237e40139ffSDimitry Andric    }
2380b57cec5SDimitry Andric}
2390b57cec5SDimitry Andric
2400b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
2410b57cec5SDimitry Andrictemplate <class _InputIter>
2420b57cec5SDimitry Andrictypename enable_if
2430b57cec5SDimitry Andric<
244480093f4SDimitry Andric     __is_cpp17_input_iterator<_InputIter>::value &&
245480093f4SDimitry Andric    !__is_cpp17_forward_iterator<_InputIter>::value,
2460b57cec5SDimitry Andric    void
2470b57cec5SDimitry Andric>::type
2480b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(_InputIter __first, _InputIter __last)
2490b57cec5SDimitry Andric{
2500b57cec5SDimitry Andric    __alloc_rr& __a = this->__alloc();
2510b57cec5SDimitry Andric    for (; __first != __last; ++__first)
2520b57cec5SDimitry Andric    {
2530b57cec5SDimitry Andric        if (__end_ == __end_cap())
2540b57cec5SDimitry Andric        {
2550b57cec5SDimitry Andric            size_type __old_cap = __end_cap() - __first_;
2560b57cec5SDimitry Andric            size_type __new_cap = _VSTD::max<size_type>(2 * __old_cap, 8);
2570b57cec5SDimitry Andric            __split_buffer __buf(__new_cap, 0, __a);
258349cc55cSDimitry Andric            for (pointer __p = __begin_; __p != __end_; ++__p, (void) ++__buf.__end_)
2590b57cec5SDimitry Andric                __alloc_traits::construct(__buf.__alloc(),
260480093f4SDimitry Andric                        _VSTD::__to_address(__buf.__end_), _VSTD::move(*__p));
2610b57cec5SDimitry Andric            swap(__buf);
2620b57cec5SDimitry Andric        }
263480093f4SDimitry Andric        __alloc_traits::construct(__a, _VSTD::__to_address(this->__end_), *__first);
2640b57cec5SDimitry Andric        ++this->__end_;
2650b57cec5SDimitry Andric    }
2660b57cec5SDimitry Andric}
2670b57cec5SDimitry Andric
2680b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
2690b57cec5SDimitry Andrictemplate <class _ForwardIterator>
2700b57cec5SDimitry Andrictypename enable_if
2710b57cec5SDimitry Andric<
272480093f4SDimitry Andric    __is_cpp17_forward_iterator<_ForwardIterator>::value,
2730b57cec5SDimitry Andric    void
2740b57cec5SDimitry Andric>::type
2750b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2760b57cec5SDimitry Andric{
277e8d8bef9SDimitry Andric    _ConstructTransaction __tx(&this->__end_, _VSTD::distance(__first, __last));
278349cc55cSDimitry Andric    for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void) ++__first) {
279e40139ffSDimitry Andric        __alloc_traits::construct(this->__alloc(),
280480093f4SDimitry Andric            _VSTD::__to_address(__tx.__pos_), *__first);
2810b57cec5SDimitry Andric    }
2820b57cec5SDimitry Andric}
2830b57cec5SDimitry Andric
2840b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
2850b57cec5SDimitry Andricinline
2860b57cec5SDimitry Andricvoid
2870b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type)
2880b57cec5SDimitry Andric{
2890b57cec5SDimitry Andric    while (__begin_ != __new_begin)
290e8d8bef9SDimitry Andric        __alloc_traits::destroy(__alloc(), _VSTD::__to_address(__begin_++));
2910b57cec5SDimitry Andric}
2920b57cec5SDimitry Andric
2930b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
2940b57cec5SDimitry Andricinline
2950b57cec5SDimitry Andricvoid
2960b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, true_type)
2970b57cec5SDimitry Andric{
2980b57cec5SDimitry Andric    __begin_ = __new_begin;
2990b57cec5SDimitry Andric}
3000b57cec5SDimitry Andric
3010b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
3020b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
3030b57cec5SDimitry Andricvoid
3040b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT
3050b57cec5SDimitry Andric{
3060b57cec5SDimitry Andric    while (__new_last != __end_)
307e8d8bef9SDimitry Andric        __alloc_traits::destroy(__alloc(), _VSTD::__to_address(--__end_));
3080b57cec5SDimitry Andric}
3090b57cec5SDimitry Andric
3100b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
3110b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
3120b57cec5SDimitry Andricvoid
3130b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, true_type) _NOEXCEPT
3140b57cec5SDimitry Andric{
3150b57cec5SDimitry Andric    __end_ = __new_last;
3160b57cec5SDimitry Andric}
3170b57cec5SDimitry Andric
3180b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
3190b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __start, __alloc_rr& __a)
3200b57cec5SDimitry Andric    : __end_cap_(nullptr, __a)
3210b57cec5SDimitry Andric{
322*81ad6265SDimitry Andric    if (__cap == 0) {
323*81ad6265SDimitry Andric        __first_ = nullptr;
324*81ad6265SDimitry Andric    } else {
325*81ad6265SDimitry Andric        auto __allocation = std::__allocate_at_least(__alloc(), __cap);
326*81ad6265SDimitry Andric        __first_ = __allocation.ptr;
327*81ad6265SDimitry Andric        __cap = __allocation.count;
328*81ad6265SDimitry Andric    }
3290b57cec5SDimitry Andric    __begin_ = __end_ = __first_ + __start;
3300b57cec5SDimitry Andric    __end_cap() = __first_ + __cap;
3310b57cec5SDimitry Andric}
3320b57cec5SDimitry Andric
3330b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
3340b57cec5SDimitry Andricinline
3350b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer()
3360b57cec5SDimitry Andric    _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
337480093f4SDimitry Andric    : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __default_init_tag())
3380b57cec5SDimitry Andric{
3390b57cec5SDimitry Andric}
3400b57cec5SDimitry Andric
3410b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
3420b57cec5SDimitry Andricinline
3430b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(__alloc_rr& __a)
3440b57cec5SDimitry Andric    : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a)
3450b57cec5SDimitry Andric{
3460b57cec5SDimitry Andric}
3470b57cec5SDimitry Andric
3480b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
3490b57cec5SDimitry Andricinline
3500b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(const __alloc_rr& __a)
3510b57cec5SDimitry Andric    : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a)
3520b57cec5SDimitry Andric{
3530b57cec5SDimitry Andric}
3540b57cec5SDimitry Andric
3550b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
3560b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::~__split_buffer()
3570b57cec5SDimitry Andric{
3580b57cec5SDimitry Andric    clear();
3590b57cec5SDimitry Andric    if (__first_)
3600b57cec5SDimitry Andric        __alloc_traits::deallocate(__alloc(), __first_, capacity());
3610b57cec5SDimitry Andric}
3620b57cec5SDimitry Andric
3630b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
3640b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c)
3650b57cec5SDimitry Andric    _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
3660b57cec5SDimitry Andric    : __first_(_VSTD::move(__c.__first_)),
3670b57cec5SDimitry Andric      __begin_(_VSTD::move(__c.__begin_)),
3680b57cec5SDimitry Andric      __end_(_VSTD::move(__c.__end_)),
3690b57cec5SDimitry Andric      __end_cap_(_VSTD::move(__c.__end_cap_))
3700b57cec5SDimitry Andric{
3710b57cec5SDimitry Andric    __c.__first_ = nullptr;
3720b57cec5SDimitry Andric    __c.__begin_ = nullptr;
3730b57cec5SDimitry Andric    __c.__end_ = nullptr;
3740b57cec5SDimitry Andric    __c.__end_cap() = nullptr;
3750b57cec5SDimitry Andric}
3760b57cec5SDimitry Andric
3770b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
3780b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a)
379480093f4SDimitry Andric    : __end_cap_(nullptr, __a)
3800b57cec5SDimitry Andric{
3810b57cec5SDimitry Andric    if (__a == __c.__alloc())
3820b57cec5SDimitry Andric    {
3830b57cec5SDimitry Andric        __first_ = __c.__first_;
3840b57cec5SDimitry Andric        __begin_ = __c.__begin_;
3850b57cec5SDimitry Andric        __end_ = __c.__end_;
3860b57cec5SDimitry Andric        __end_cap() = __c.__end_cap();
3870b57cec5SDimitry Andric        __c.__first_ = nullptr;
3880b57cec5SDimitry Andric        __c.__begin_ = nullptr;
3890b57cec5SDimitry Andric        __c.__end_ = nullptr;
3900b57cec5SDimitry Andric        __c.__end_cap() = nullptr;
3910b57cec5SDimitry Andric    }
3920b57cec5SDimitry Andric    else
3930b57cec5SDimitry Andric    {
394*81ad6265SDimitry Andric        auto __allocation = std::__allocate_at_least(__alloc(), __c.size());
395*81ad6265SDimitry Andric        __first_ = __allocation.ptr;
3960b57cec5SDimitry Andric        __begin_ = __end_ = __first_;
397*81ad6265SDimitry Andric        __end_cap() = __first_ + __allocation.count;
3980b57cec5SDimitry Andric        typedef move_iterator<iterator> _Ip;
3990b57cec5SDimitry Andric        __construct_at_end(_Ip(__c.begin()), _Ip(__c.end()));
4000b57cec5SDimitry Andric    }
4010b57cec5SDimitry Andric}
4020b57cec5SDimitry Andric
4030b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
4040b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>&
4050b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c)
4060b57cec5SDimitry Andric    _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
4070b57cec5SDimitry Andric                is_nothrow_move_assignable<allocator_type>::value) ||
4080b57cec5SDimitry Andric               !__alloc_traits::propagate_on_container_move_assignment::value)
4090b57cec5SDimitry Andric{
4100b57cec5SDimitry Andric    clear();
4110b57cec5SDimitry Andric    shrink_to_fit();
4120b57cec5SDimitry Andric    __first_ = __c.__first_;
4130b57cec5SDimitry Andric    __begin_ = __c.__begin_;
4140b57cec5SDimitry Andric    __end_ = __c.__end_;
4150b57cec5SDimitry Andric    __end_cap() = __c.__end_cap();
4160b57cec5SDimitry Andric    __move_assign_alloc(__c,
4170b57cec5SDimitry Andric        integral_constant<bool,
4180b57cec5SDimitry Andric                          __alloc_traits::propagate_on_container_move_assignment::value>());
4190b57cec5SDimitry Andric    __c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
4200b57cec5SDimitry Andric    return *this;
4210b57cec5SDimitry Andric}
4220b57cec5SDimitry Andric
4230b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
4240b57cec5SDimitry Andricvoid
4250b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::swap(__split_buffer& __x)
4260b57cec5SDimitry Andric        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
4270b57cec5SDimitry Andric                   __is_nothrow_swappable<__alloc_rr>::value)
4280b57cec5SDimitry Andric{
4290b57cec5SDimitry Andric    _VSTD::swap(__first_, __x.__first_);
4300b57cec5SDimitry Andric    _VSTD::swap(__begin_, __x.__begin_);
4310b57cec5SDimitry Andric    _VSTD::swap(__end_, __x.__end_);
4320b57cec5SDimitry Andric    _VSTD::swap(__end_cap(), __x.__end_cap());
433e8d8bef9SDimitry Andric    _VSTD::__swap_allocator(__alloc(), __x.__alloc());
4340b57cec5SDimitry Andric}
4350b57cec5SDimitry Andric
4360b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
4370b57cec5SDimitry Andricvoid
4380b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::reserve(size_type __n)
4390b57cec5SDimitry Andric{
4400b57cec5SDimitry Andric    if (__n < capacity())
4410b57cec5SDimitry Andric    {
4420b57cec5SDimitry Andric        __split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc());
4430b57cec5SDimitry Andric        __t.__construct_at_end(move_iterator<pointer>(__begin_),
4440b57cec5SDimitry Andric                               move_iterator<pointer>(__end_));
4450b57cec5SDimitry Andric        _VSTD::swap(__first_, __t.__first_);
4460b57cec5SDimitry Andric        _VSTD::swap(__begin_, __t.__begin_);
4470b57cec5SDimitry Andric        _VSTD::swap(__end_, __t.__end_);
4480b57cec5SDimitry Andric        _VSTD::swap(__end_cap(), __t.__end_cap());
4490b57cec5SDimitry Andric    }
4500b57cec5SDimitry Andric}
4510b57cec5SDimitry Andric
4520b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
4530b57cec5SDimitry Andricvoid
4540b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
4550b57cec5SDimitry Andric{
4560b57cec5SDimitry Andric    if (capacity() > size())
4570b57cec5SDimitry Andric    {
4580b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
4590b57cec5SDimitry Andric        try
4600b57cec5SDimitry Andric        {
4610b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS
4620b57cec5SDimitry Andric            __split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc());
4630b57cec5SDimitry Andric            __t.__construct_at_end(move_iterator<pointer>(__begin_),
4640b57cec5SDimitry Andric                                   move_iterator<pointer>(__end_));
4650b57cec5SDimitry Andric            __t.__end_ = __t.__begin_ + (__end_ - __begin_);
4660b57cec5SDimitry Andric            _VSTD::swap(__first_, __t.__first_);
4670b57cec5SDimitry Andric            _VSTD::swap(__begin_, __t.__begin_);
4680b57cec5SDimitry Andric            _VSTD::swap(__end_, __t.__end_);
4690b57cec5SDimitry Andric            _VSTD::swap(__end_cap(), __t.__end_cap());
4700b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
4710b57cec5SDimitry Andric        }
4720b57cec5SDimitry Andric        catch (...)
4730b57cec5SDimitry Andric        {
4740b57cec5SDimitry Andric        }
4750b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS
4760b57cec5SDimitry Andric    }
4770b57cec5SDimitry Andric}
4780b57cec5SDimitry Andric
4790b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
4800b57cec5SDimitry Andricvoid
4810b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_front(const_reference __x)
4820b57cec5SDimitry Andric{
4830b57cec5SDimitry Andric    if (__begin_ == __first_)
4840b57cec5SDimitry Andric    {
4850b57cec5SDimitry Andric        if (__end_ < __end_cap())
4860b57cec5SDimitry Andric        {
4870b57cec5SDimitry Andric            difference_type __d = __end_cap() - __end_;
4880b57cec5SDimitry Andric            __d = (__d + 1) / 2;
4890b57cec5SDimitry Andric            __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d);
4900b57cec5SDimitry Andric            __end_ += __d;
4910b57cec5SDimitry Andric        }
4920b57cec5SDimitry Andric        else
4930b57cec5SDimitry Andric        {
4940b57cec5SDimitry Andric            size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
4950b57cec5SDimitry Andric            __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
4960b57cec5SDimitry Andric            __t.__construct_at_end(move_iterator<pointer>(__begin_),
4970b57cec5SDimitry Andric                                   move_iterator<pointer>(__end_));
4980b57cec5SDimitry Andric            _VSTD::swap(__first_, __t.__first_);
4990b57cec5SDimitry Andric            _VSTD::swap(__begin_, __t.__begin_);
5000b57cec5SDimitry Andric            _VSTD::swap(__end_, __t.__end_);
5010b57cec5SDimitry Andric            _VSTD::swap(__end_cap(), __t.__end_cap());
5020b57cec5SDimitry Andric        }
5030b57cec5SDimitry Andric    }
504480093f4SDimitry Andric    __alloc_traits::construct(__alloc(), _VSTD::__to_address(__begin_-1), __x);
5050b57cec5SDimitry Andric    --__begin_;
5060b57cec5SDimitry Andric}
5070b57cec5SDimitry Andric
5080b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
5090b57cec5SDimitry Andricvoid
5100b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_front(value_type&& __x)
5110b57cec5SDimitry Andric{
5120b57cec5SDimitry Andric    if (__begin_ == __first_)
5130b57cec5SDimitry Andric    {
5140b57cec5SDimitry Andric        if (__end_ < __end_cap())
5150b57cec5SDimitry Andric        {
5160b57cec5SDimitry Andric            difference_type __d = __end_cap() - __end_;
5170b57cec5SDimitry Andric            __d = (__d + 1) / 2;
5180b57cec5SDimitry Andric            __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d);
5190b57cec5SDimitry Andric            __end_ += __d;
5200b57cec5SDimitry Andric        }
5210b57cec5SDimitry Andric        else
5220b57cec5SDimitry Andric        {
5230b57cec5SDimitry Andric            size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
5240b57cec5SDimitry Andric            __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
5250b57cec5SDimitry Andric            __t.__construct_at_end(move_iterator<pointer>(__begin_),
5260b57cec5SDimitry Andric                                   move_iterator<pointer>(__end_));
5270b57cec5SDimitry Andric            _VSTD::swap(__first_, __t.__first_);
5280b57cec5SDimitry Andric            _VSTD::swap(__begin_, __t.__begin_);
5290b57cec5SDimitry Andric            _VSTD::swap(__end_, __t.__end_);
5300b57cec5SDimitry Andric            _VSTD::swap(__end_cap(), __t.__end_cap());
5310b57cec5SDimitry Andric        }
5320b57cec5SDimitry Andric    }
533480093f4SDimitry Andric    __alloc_traits::construct(__alloc(), _VSTD::__to_address(__begin_-1),
5340b57cec5SDimitry Andric            _VSTD::move(__x));
5350b57cec5SDimitry Andric    --__begin_;
5360b57cec5SDimitry Andric}
5370b57cec5SDimitry Andric
5380b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
5390b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
5400b57cec5SDimitry Andricvoid
5410b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_back(const_reference __x)
5420b57cec5SDimitry Andric{
5430b57cec5SDimitry Andric    if (__end_ == __end_cap())
5440b57cec5SDimitry Andric    {
5450b57cec5SDimitry Andric        if (__begin_ > __first_)
5460b57cec5SDimitry Andric        {
5470b57cec5SDimitry Andric            difference_type __d = __begin_ - __first_;
5480b57cec5SDimitry Andric            __d = (__d + 1) / 2;
5490b57cec5SDimitry Andric            __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
5500b57cec5SDimitry Andric            __begin_ -= __d;
5510b57cec5SDimitry Andric        }
5520b57cec5SDimitry Andric        else
5530b57cec5SDimitry Andric        {
5540b57cec5SDimitry Andric            size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
5550b57cec5SDimitry Andric            __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
5560b57cec5SDimitry Andric            __t.__construct_at_end(move_iterator<pointer>(__begin_),
5570b57cec5SDimitry Andric                                   move_iterator<pointer>(__end_));
5580b57cec5SDimitry Andric            _VSTD::swap(__first_, __t.__first_);
5590b57cec5SDimitry Andric            _VSTD::swap(__begin_, __t.__begin_);
5600b57cec5SDimitry Andric            _VSTD::swap(__end_, __t.__end_);
5610b57cec5SDimitry Andric            _VSTD::swap(__end_cap(), __t.__end_cap());
5620b57cec5SDimitry Andric        }
5630b57cec5SDimitry Andric    }
564480093f4SDimitry Andric    __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_), __x);
5650b57cec5SDimitry Andric    ++__end_;
5660b57cec5SDimitry Andric}
5670b57cec5SDimitry Andric
5680b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
5690b57cec5SDimitry Andricvoid
5700b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_back(value_type&& __x)
5710b57cec5SDimitry Andric{
5720b57cec5SDimitry Andric    if (__end_ == __end_cap())
5730b57cec5SDimitry Andric    {
5740b57cec5SDimitry Andric        if (__begin_ > __first_)
5750b57cec5SDimitry Andric        {
5760b57cec5SDimitry Andric            difference_type __d = __begin_ - __first_;
5770b57cec5SDimitry Andric            __d = (__d + 1) / 2;
5780b57cec5SDimitry Andric            __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
5790b57cec5SDimitry Andric            __begin_ -= __d;
5800b57cec5SDimitry Andric        }
5810b57cec5SDimitry Andric        else
5820b57cec5SDimitry Andric        {
5830b57cec5SDimitry Andric            size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
5840b57cec5SDimitry Andric            __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
5850b57cec5SDimitry Andric            __t.__construct_at_end(move_iterator<pointer>(__begin_),
5860b57cec5SDimitry Andric                                   move_iterator<pointer>(__end_));
5870b57cec5SDimitry Andric            _VSTD::swap(__first_, __t.__first_);
5880b57cec5SDimitry Andric            _VSTD::swap(__begin_, __t.__begin_);
5890b57cec5SDimitry Andric            _VSTD::swap(__end_, __t.__end_);
5900b57cec5SDimitry Andric            _VSTD::swap(__end_cap(), __t.__end_cap());
5910b57cec5SDimitry Andric        }
5920b57cec5SDimitry Andric    }
593480093f4SDimitry Andric    __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_),
5940b57cec5SDimitry Andric            _VSTD::move(__x));
5950b57cec5SDimitry Andric    ++__end_;
5960b57cec5SDimitry Andric}
5970b57cec5SDimitry Andric
5980b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
5990b57cec5SDimitry Andrictemplate <class... _Args>
6000b57cec5SDimitry Andricvoid
6010b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args)
6020b57cec5SDimitry Andric{
6030b57cec5SDimitry Andric    if (__end_ == __end_cap())
6040b57cec5SDimitry Andric    {
6050b57cec5SDimitry Andric        if (__begin_ > __first_)
6060b57cec5SDimitry Andric        {
6070b57cec5SDimitry Andric            difference_type __d = __begin_ - __first_;
6080b57cec5SDimitry Andric            __d = (__d + 1) / 2;
6090b57cec5SDimitry Andric            __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
6100b57cec5SDimitry Andric            __begin_ -= __d;
6110b57cec5SDimitry Andric        }
6120b57cec5SDimitry Andric        else
6130b57cec5SDimitry Andric        {
6140b57cec5SDimitry Andric            size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
6150b57cec5SDimitry Andric            __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
6160b57cec5SDimitry Andric            __t.__construct_at_end(move_iterator<pointer>(__begin_),
6170b57cec5SDimitry Andric                                   move_iterator<pointer>(__end_));
6180b57cec5SDimitry Andric            _VSTD::swap(__first_, __t.__first_);
6190b57cec5SDimitry Andric            _VSTD::swap(__begin_, __t.__begin_);
6200b57cec5SDimitry Andric            _VSTD::swap(__end_, __t.__end_);
6210b57cec5SDimitry Andric            _VSTD::swap(__end_cap(), __t.__end_cap());
6220b57cec5SDimitry Andric        }
6230b57cec5SDimitry Andric    }
624480093f4SDimitry Andric    __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_),
6250b57cec5SDimitry Andric                              _VSTD::forward<_Args>(__args)...);
6260b57cec5SDimitry Andric    ++__end_;
6270b57cec5SDimitry Andric}
6280b57cec5SDimitry Andric
6290b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
6300b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
6310b57cec5SDimitry Andricvoid
6320b57cec5SDimitry Andricswap(__split_buffer<_Tp, _Allocator>& __x, __split_buffer<_Tp, _Allocator>& __y)
6330b57cec5SDimitry Andric        _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
6340b57cec5SDimitry Andric{
6350b57cec5SDimitry Andric    __x.swap(__y);
6360b57cec5SDimitry Andric}
6370b57cec5SDimitry Andric
6380b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
6390b57cec5SDimitry Andric
6400b57cec5SDimitry Andric_LIBCPP_POP_MACROS
6410b57cec5SDimitry Andric
642*81ad6265SDimitry Andric#endif // _LIBCPP___SPLIT_BUFFER
643