xref: /freebsd/contrib/llvm-project/libcxx/include/__split_buffer (revision e40139ff33b48b56a24c808b166b04b8ee6f5b21)
10b57cec5SDimitry Andric// -*- C++ -*-
20b57cec5SDimitry Andric#ifndef _LIBCPP_SPLIT_BUFFER
30b57cec5SDimitry Andric#define _LIBCPP_SPLIT_BUFFER
40b57cec5SDimitry Andric
50b57cec5SDimitry Andric#include <__config>
60b57cec5SDimitry Andric#include <type_traits>
70b57cec5SDimitry Andric#include <algorithm>
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
100b57cec5SDimitry Andric#pragma GCC system_header
110b57cec5SDimitry Andric#endif
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS
140b57cec5SDimitry Andric#include <__undef_macros>
150b57cec5SDimitry Andric
160b57cec5SDimitry Andric
170b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
180b57cec5SDimitry Andric
190b57cec5SDimitry Andrictemplate <bool>
200b57cec5SDimitry Andricclass __split_buffer_common
210b57cec5SDimitry Andric{
220b57cec5SDimitry Andricprotected:
230b57cec5SDimitry Andric    void __throw_length_error() const;
240b57cec5SDimitry Andric    void __throw_out_of_range() const;
250b57cec5SDimitry Andric};
260b57cec5SDimitry Andric
270b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator = allocator<_Tp> >
280b57cec5SDimitry Andricstruct __split_buffer
290b57cec5SDimitry Andric    : private __split_buffer_common<true>
300b57cec5SDimitry Andric{
310b57cec5SDimitry Andricprivate:
320b57cec5SDimitry Andric    __split_buffer(const __split_buffer&);
330b57cec5SDimitry Andric    __split_buffer& operator=(const __split_buffer&);
340b57cec5SDimitry Andricpublic:
350b57cec5SDimitry Andric    typedef _Tp                                             value_type;
360b57cec5SDimitry Andric    typedef _Allocator                                      allocator_type;
370b57cec5SDimitry Andric    typedef typename remove_reference<allocator_type>::type __alloc_rr;
380b57cec5SDimitry Andric    typedef allocator_traits<__alloc_rr>                    __alloc_traits;
390b57cec5SDimitry Andric    typedef value_type&                                     reference;
400b57cec5SDimitry Andric    typedef const value_type&                               const_reference;
410b57cec5SDimitry Andric    typedef typename __alloc_traits::size_type              size_type;
420b57cec5SDimitry Andric    typedef typename __alloc_traits::difference_type        difference_type;
430b57cec5SDimitry Andric    typedef typename __alloc_traits::pointer                pointer;
440b57cec5SDimitry Andric    typedef typename __alloc_traits::const_pointer          const_pointer;
450b57cec5SDimitry Andric    typedef pointer                                         iterator;
460b57cec5SDimitry Andric    typedef const_pointer                                   const_iterator;
470b57cec5SDimitry Andric
480b57cec5SDimitry Andric    pointer                                         __first_;
490b57cec5SDimitry Andric    pointer                                         __begin_;
500b57cec5SDimitry Andric    pointer                                         __end_;
510b57cec5SDimitry Andric    __compressed_pair<pointer, allocator_type> __end_cap_;
520b57cec5SDimitry Andric
530b57cec5SDimitry Andric    typedef typename add_lvalue_reference<allocator_type>::type __alloc_ref;
540b57cec5SDimitry Andric    typedef typename add_lvalue_reference<allocator_type>::type __alloc_const_ref;
550b57cec5SDimitry Andric
560b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __alloc_rr&           __alloc() _NOEXCEPT         {return __end_cap_.second();}
570b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY const __alloc_rr&     __alloc() const _NOEXCEPT   {return __end_cap_.second();}
580b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY pointer&              __end_cap() _NOEXCEPT       {return __end_cap_.first();}
590b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY const pointer&        __end_cap() const _NOEXCEPT {return __end_cap_.first();}
600b57cec5SDimitry Andric
610b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
620b57cec5SDimitry Andric    __split_buffer()
630b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
640b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
650b57cec5SDimitry Andric    explicit __split_buffer(__alloc_rr& __a);
660b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
670b57cec5SDimitry Andric    explicit __split_buffer(const __alloc_rr& __a);
680b57cec5SDimitry Andric    __split_buffer(size_type __cap, size_type __start, __alloc_rr& __a);
690b57cec5SDimitry Andric    ~__split_buffer();
700b57cec5SDimitry Andric
710b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
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#endif  // _LIBCPP_CXX03_LANG
800b57cec5SDimitry Andric
810b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY       iterator begin() _NOEXCEPT       {return __begin_;}
820b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT {return __begin_;}
830b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY       iterator end() _NOEXCEPT         {return __end_;}
840b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT   {return __end_;}
850b57cec5SDimitry Andric
860b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
870b57cec5SDimitry Andric    void clear() _NOEXCEPT
880b57cec5SDimitry Andric        {__destruct_at_end(__begin_);}
890b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY size_type size() const {return static_cast<size_type>(__end_ - __begin_);}
900b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY bool empty()     const {return __end_ == __begin_;}
910b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY size_type capacity() const {return static_cast<size_type>(__end_cap() - __first_);}
920b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY size_type __front_spare() const {return static_cast<size_type>(__begin_ - __first_);}
930b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY size_type __back_spare() const {return static_cast<size_type>(__end_cap() - __end_);}
940b57cec5SDimitry Andric
950b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY       reference front()       {return *__begin_;}
960b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY const_reference front() const {return *__begin_;}
970b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY       reference back()        {return *(__end_ - 1);}
980b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY const_reference back() const  {return *(__end_ - 1);}
990b57cec5SDimitry Andric
1000b57cec5SDimitry Andric    void reserve(size_type __n);
1010b57cec5SDimitry Andric    void shrink_to_fit() _NOEXCEPT;
1020b57cec5SDimitry Andric    void push_front(const_reference __x);
1030b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
1040b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
1050b57cec5SDimitry Andric    void push_front(value_type&& __x);
1060b57cec5SDimitry Andric    void push_back(value_type&& __x);
1070b57cec5SDimitry Andric    template <class... _Args>
1080b57cec5SDimitry Andric        void emplace_back(_Args&&... __args);
1090b57cec5SDimitry Andric#endif  // !defined(_LIBCPP_CXX03_LANG)
1100b57cec5SDimitry Andric
1110b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void pop_front() {__destruct_at_begin(__begin_+1);}
1120b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void pop_back() {__destruct_at_end(__end_-1);}
1130b57cec5SDimitry Andric
1140b57cec5SDimitry Andric    void __construct_at_end(size_type __n);
1150b57cec5SDimitry Andric    void __construct_at_end(size_type __n, const_reference __x);
1160b57cec5SDimitry Andric    template <class _InputIter>
1170b57cec5SDimitry Andric        typename enable_if
1180b57cec5SDimitry Andric        <
1190b57cec5SDimitry Andric            __is_input_iterator<_InputIter>::value &&
1200b57cec5SDimitry Andric           !__is_forward_iterator<_InputIter>::value,
1210b57cec5SDimitry Andric            void
1220b57cec5SDimitry Andric        >::type
1230b57cec5SDimitry Andric        __construct_at_end(_InputIter __first, _InputIter __last);
1240b57cec5SDimitry Andric    template <class _ForwardIterator>
1250b57cec5SDimitry Andric        typename enable_if
1260b57cec5SDimitry Andric        <
1270b57cec5SDimitry Andric            __is_forward_iterator<_ForwardIterator>::value,
1280b57cec5SDimitry Andric            void
1290b57cec5SDimitry Andric        >::type
1300b57cec5SDimitry Andric        __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
1310b57cec5SDimitry Andric
1320b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void __destruct_at_begin(pointer __new_begin)
1330b57cec5SDimitry Andric        {__destruct_at_begin(__new_begin, is_trivially_destructible<value_type>());}
1340b57cec5SDimitry Andric        _LIBCPP_INLINE_VISIBILITY
1350b57cec5SDimitry Andric        void __destruct_at_begin(pointer __new_begin, false_type);
1360b57cec5SDimitry Andric        _LIBCPP_INLINE_VISIBILITY
1370b57cec5SDimitry Andric        void __destruct_at_begin(pointer __new_begin, true_type);
1380b57cec5SDimitry Andric
1390b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1400b57cec5SDimitry Andric    void __destruct_at_end(pointer __new_last) _NOEXCEPT
1410b57cec5SDimitry Andric        {__destruct_at_end(__new_last, false_type());}
1420b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1430b57cec5SDimitry Andric        void __destruct_at_end(pointer __new_last, false_type) _NOEXCEPT;
1440b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1450b57cec5SDimitry Andric        void __destruct_at_end(pointer __new_last, true_type) _NOEXCEPT;
1460b57cec5SDimitry Andric
1470b57cec5SDimitry Andric    void swap(__split_buffer& __x)
1480b57cec5SDimitry Andric        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
1490b57cec5SDimitry Andric                   __is_nothrow_swappable<__alloc_rr>::value);
1500b57cec5SDimitry Andric
1510b57cec5SDimitry Andric    bool __invariants() const;
1520b57cec5SDimitry Andric
1530b57cec5SDimitry Andricprivate:
1540b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1550b57cec5SDimitry Andric    void __move_assign_alloc(__split_buffer& __c, true_type)
1560b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1570b57cec5SDimitry Andric        {
1580b57cec5SDimitry Andric            __alloc() = _VSTD::move(__c.__alloc());
1590b57cec5SDimitry Andric        }
1600b57cec5SDimitry Andric
1610b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1620b57cec5SDimitry Andric    void __move_assign_alloc(__split_buffer&, false_type) _NOEXCEPT
1630b57cec5SDimitry Andric        {}
164*e40139ffSDimitry Andric
165*e40139ffSDimitry Andric    struct _ConstructTransaction {
166*e40139ffSDimitry Andric      explicit _ConstructTransaction(pointer* __p, size_type __n) _NOEXCEPT
167*e40139ffSDimitry Andric      : __pos_(*__p), __end_(*__p + __n), __dest_(__p) {
168*e40139ffSDimitry Andric      }
169*e40139ffSDimitry Andric      ~_ConstructTransaction() {
170*e40139ffSDimitry Andric        *__dest_ = __pos_;
171*e40139ffSDimitry Andric      }
172*e40139ffSDimitry Andric      pointer __pos_;
173*e40139ffSDimitry Andric     const pointer __end_;
174*e40139ffSDimitry Andric    private:
175*e40139ffSDimitry Andric     pointer *__dest_;
176*e40139ffSDimitry Andric    };
1770b57cec5SDimitry Andric};
1780b57cec5SDimitry Andric
1790b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1800b57cec5SDimitry Andricbool
1810b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__invariants() const
1820b57cec5SDimitry Andric{
1830b57cec5SDimitry Andric    if (__first_ == nullptr)
1840b57cec5SDimitry Andric    {
1850b57cec5SDimitry Andric        if (__begin_ != nullptr)
1860b57cec5SDimitry Andric            return false;
1870b57cec5SDimitry Andric        if (__end_ != nullptr)
1880b57cec5SDimitry Andric            return false;
1890b57cec5SDimitry Andric        if (__end_cap() != nullptr)
1900b57cec5SDimitry Andric            return false;
1910b57cec5SDimitry Andric    }
1920b57cec5SDimitry Andric    else
1930b57cec5SDimitry Andric    {
1940b57cec5SDimitry Andric        if (__begin_ < __first_)
1950b57cec5SDimitry Andric            return false;
1960b57cec5SDimitry Andric        if (__end_ < __begin_)
1970b57cec5SDimitry Andric            return false;
1980b57cec5SDimitry Andric        if (__end_cap() < __end_)
1990b57cec5SDimitry Andric            return false;
2000b57cec5SDimitry Andric    }
2010b57cec5SDimitry Andric    return true;
2020b57cec5SDimitry Andric}
2030b57cec5SDimitry Andric
2040b57cec5SDimitry Andric//  Default constructs __n objects starting at __end_
2050b57cec5SDimitry Andric//  throws if construction throws
2060b57cec5SDimitry Andric//  Precondition:  __n > 0
2070b57cec5SDimitry Andric//  Precondition:  size() + __n <= capacity()
2080b57cec5SDimitry Andric//  Postcondition:  size() == size() + __n
2090b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
2100b57cec5SDimitry Andricvoid
2110b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n)
2120b57cec5SDimitry Andric{
213*e40139ffSDimitry Andric    _ConstructTransaction __tx(&this->__end_, __n);
214*e40139ffSDimitry Andric    for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
215*e40139ffSDimitry Andric        __alloc_traits::construct(this->__alloc(), _VSTD::__to_raw_pointer(__tx.__pos_));
216*e40139ffSDimitry Andric    }
2170b57cec5SDimitry Andric}
2180b57cec5SDimitry Andric
2190b57cec5SDimitry Andric//  Copy constructs __n objects starting at __end_ from __x
2200b57cec5SDimitry Andric//  throws if construction throws
2210b57cec5SDimitry Andric//  Precondition:  __n > 0
2220b57cec5SDimitry Andric//  Precondition:  size() + __n <= capacity()
2230b57cec5SDimitry Andric//  Postcondition:  size() == old size() + __n
2240b57cec5SDimitry Andric//  Postcondition:  [i] == __x for all i in [size() - __n, __n)
2250b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
2260b57cec5SDimitry Andricvoid
2270b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
2280b57cec5SDimitry Andric{
229*e40139ffSDimitry Andric    _ConstructTransaction __tx(&this->__end_, __n);
230*e40139ffSDimitry Andric    for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
231*e40139ffSDimitry Andric        __alloc_traits::construct(this->__alloc(),
232*e40139ffSDimitry Andric            _VSTD::__to_raw_pointer(__tx.__pos_), __x);
233*e40139ffSDimitry Andric    }
2340b57cec5SDimitry Andric}
2350b57cec5SDimitry Andric
2360b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
2370b57cec5SDimitry Andrictemplate <class _InputIter>
2380b57cec5SDimitry Andrictypename enable_if
2390b57cec5SDimitry Andric<
2400b57cec5SDimitry Andric     __is_input_iterator<_InputIter>::value &&
2410b57cec5SDimitry Andric    !__is_forward_iterator<_InputIter>::value,
2420b57cec5SDimitry Andric    void
2430b57cec5SDimitry Andric>::type
2440b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(_InputIter __first, _InputIter __last)
2450b57cec5SDimitry Andric{
2460b57cec5SDimitry Andric    __alloc_rr& __a = this->__alloc();
2470b57cec5SDimitry Andric    for (; __first != __last; ++__first)
2480b57cec5SDimitry Andric    {
2490b57cec5SDimitry Andric        if (__end_ == __end_cap())
2500b57cec5SDimitry Andric        {
2510b57cec5SDimitry Andric            size_type __old_cap = __end_cap() - __first_;
2520b57cec5SDimitry Andric            size_type __new_cap = _VSTD::max<size_type>(2 * __old_cap, 8);
2530b57cec5SDimitry Andric            __split_buffer __buf(__new_cap, 0, __a);
2540b57cec5SDimitry Andric            for (pointer __p = __begin_; __p != __end_; ++__p, ++__buf.__end_)
2550b57cec5SDimitry Andric                __alloc_traits::construct(__buf.__alloc(),
2560b57cec5SDimitry Andric                        _VSTD::__to_raw_pointer(__buf.__end_), _VSTD::move(*__p));
2570b57cec5SDimitry Andric            swap(__buf);
2580b57cec5SDimitry Andric        }
2590b57cec5SDimitry Andric        __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
2600b57cec5SDimitry Andric        ++this->__end_;
2610b57cec5SDimitry Andric    }
2620b57cec5SDimitry Andric}
2630b57cec5SDimitry Andric
2640b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
2650b57cec5SDimitry Andrictemplate <class _ForwardIterator>
2660b57cec5SDimitry Andrictypename enable_if
2670b57cec5SDimitry Andric<
2680b57cec5SDimitry Andric    __is_forward_iterator<_ForwardIterator>::value,
2690b57cec5SDimitry Andric    void
2700b57cec5SDimitry Andric>::type
2710b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2720b57cec5SDimitry Andric{
273*e40139ffSDimitry Andric    _ConstructTransaction __tx(&this->__end_, std::distance(__first, __last));
274*e40139ffSDimitry Andric    for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, ++__first) {
275*e40139ffSDimitry Andric        __alloc_traits::construct(this->__alloc(),
276*e40139ffSDimitry Andric            _VSTD::__to_raw_pointer(__tx.__pos_), *__first);
2770b57cec5SDimitry Andric    }
2780b57cec5SDimitry Andric}
2790b57cec5SDimitry Andric
2800b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
2810b57cec5SDimitry Andricinline
2820b57cec5SDimitry Andricvoid
2830b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type)
2840b57cec5SDimitry Andric{
2850b57cec5SDimitry Andric    while (__begin_ != __new_begin)
2860b57cec5SDimitry Andric        __alloc_traits::destroy(__alloc(), __to_raw_pointer(__begin_++));
2870b57cec5SDimitry Andric}
2880b57cec5SDimitry Andric
2890b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
2900b57cec5SDimitry Andricinline
2910b57cec5SDimitry Andricvoid
2920b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, true_type)
2930b57cec5SDimitry Andric{
2940b57cec5SDimitry Andric    __begin_ = __new_begin;
2950b57cec5SDimitry Andric}
2960b57cec5SDimitry Andric
2970b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
2980b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
2990b57cec5SDimitry Andricvoid
3000b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT
3010b57cec5SDimitry Andric{
3020b57cec5SDimitry Andric    while (__new_last != __end_)
3030b57cec5SDimitry Andric        __alloc_traits::destroy(__alloc(), __to_raw_pointer(--__end_));
3040b57cec5SDimitry Andric}
3050b57cec5SDimitry Andric
3060b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
3070b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
3080b57cec5SDimitry Andricvoid
3090b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, true_type) _NOEXCEPT
3100b57cec5SDimitry Andric{
3110b57cec5SDimitry Andric    __end_ = __new_last;
3120b57cec5SDimitry Andric}
3130b57cec5SDimitry Andric
3140b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
3150b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __start, __alloc_rr& __a)
3160b57cec5SDimitry Andric    : __end_cap_(nullptr, __a)
3170b57cec5SDimitry Andric{
3180b57cec5SDimitry Andric    __first_ = __cap != 0 ? __alloc_traits::allocate(__alloc(), __cap) : nullptr;
3190b57cec5SDimitry Andric    __begin_ = __end_ = __first_ + __start;
3200b57cec5SDimitry Andric    __end_cap() = __first_ + __cap;
3210b57cec5SDimitry Andric}
3220b57cec5SDimitry Andric
3230b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
3240b57cec5SDimitry Andricinline
3250b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer()
3260b57cec5SDimitry Andric    _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
3270b57cec5SDimitry Andric    : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr)
3280b57cec5SDimitry Andric{
3290b57cec5SDimitry Andric}
3300b57cec5SDimitry Andric
3310b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
3320b57cec5SDimitry Andricinline
3330b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(__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 Andricinline
3400b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(const __alloc_rr& __a)
3410b57cec5SDimitry Andric    : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a)
3420b57cec5SDimitry Andric{
3430b57cec5SDimitry Andric}
3440b57cec5SDimitry Andric
3450b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
3460b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::~__split_buffer()
3470b57cec5SDimitry Andric{
3480b57cec5SDimitry Andric    clear();
3490b57cec5SDimitry Andric    if (__first_)
3500b57cec5SDimitry Andric        __alloc_traits::deallocate(__alloc(), __first_, capacity());
3510b57cec5SDimitry Andric}
3520b57cec5SDimitry Andric
3530b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
3540b57cec5SDimitry Andric
3550b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
3560b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c)
3570b57cec5SDimitry Andric    _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
3580b57cec5SDimitry Andric    : __first_(_VSTD::move(__c.__first_)),
3590b57cec5SDimitry Andric      __begin_(_VSTD::move(__c.__begin_)),
3600b57cec5SDimitry Andric      __end_(_VSTD::move(__c.__end_)),
3610b57cec5SDimitry Andric      __end_cap_(_VSTD::move(__c.__end_cap_))
3620b57cec5SDimitry Andric{
3630b57cec5SDimitry Andric    __c.__first_ = nullptr;
3640b57cec5SDimitry Andric    __c.__begin_ = nullptr;
3650b57cec5SDimitry Andric    __c.__end_ = nullptr;
3660b57cec5SDimitry Andric    __c.__end_cap() = nullptr;
3670b57cec5SDimitry Andric}
3680b57cec5SDimitry Andric
3690b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
3700b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a)
3710b57cec5SDimitry Andric    : __end_cap_(__second_tag(), __a)
3720b57cec5SDimitry Andric{
3730b57cec5SDimitry Andric    if (__a == __c.__alloc())
3740b57cec5SDimitry Andric    {
3750b57cec5SDimitry Andric        __first_ = __c.__first_;
3760b57cec5SDimitry Andric        __begin_ = __c.__begin_;
3770b57cec5SDimitry Andric        __end_ = __c.__end_;
3780b57cec5SDimitry Andric        __end_cap() = __c.__end_cap();
3790b57cec5SDimitry Andric        __c.__first_ = nullptr;
3800b57cec5SDimitry Andric        __c.__begin_ = nullptr;
3810b57cec5SDimitry Andric        __c.__end_ = nullptr;
3820b57cec5SDimitry Andric        __c.__end_cap() = nullptr;
3830b57cec5SDimitry Andric    }
3840b57cec5SDimitry Andric    else
3850b57cec5SDimitry Andric    {
3860b57cec5SDimitry Andric        size_type __cap = __c.size();
3870b57cec5SDimitry Andric        __first_ = __alloc_traits::allocate(__alloc(), __cap);
3880b57cec5SDimitry Andric        __begin_ = __end_ = __first_;
3890b57cec5SDimitry Andric        __end_cap() = __first_ + __cap;
3900b57cec5SDimitry Andric        typedef move_iterator<iterator> _Ip;
3910b57cec5SDimitry Andric        __construct_at_end(_Ip(__c.begin()), _Ip(__c.end()));
3920b57cec5SDimitry Andric    }
3930b57cec5SDimitry Andric}
3940b57cec5SDimitry Andric
3950b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
3960b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>&
3970b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c)
3980b57cec5SDimitry Andric    _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
3990b57cec5SDimitry Andric                is_nothrow_move_assignable<allocator_type>::value) ||
4000b57cec5SDimitry Andric               !__alloc_traits::propagate_on_container_move_assignment::value)
4010b57cec5SDimitry Andric{
4020b57cec5SDimitry Andric    clear();
4030b57cec5SDimitry Andric    shrink_to_fit();
4040b57cec5SDimitry Andric    __first_ = __c.__first_;
4050b57cec5SDimitry Andric    __begin_ = __c.__begin_;
4060b57cec5SDimitry Andric    __end_ = __c.__end_;
4070b57cec5SDimitry Andric    __end_cap() = __c.__end_cap();
4080b57cec5SDimitry Andric    __move_assign_alloc(__c,
4090b57cec5SDimitry Andric        integral_constant<bool,
4100b57cec5SDimitry Andric                          __alloc_traits::propagate_on_container_move_assignment::value>());
4110b57cec5SDimitry Andric    __c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
4120b57cec5SDimitry Andric    return *this;
4130b57cec5SDimitry Andric}
4140b57cec5SDimitry Andric
4150b57cec5SDimitry Andric#endif  // _LIBCPP_CXX03_LANG
4160b57cec5SDimitry Andric
4170b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
4180b57cec5SDimitry Andricvoid
4190b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::swap(__split_buffer& __x)
4200b57cec5SDimitry Andric        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
4210b57cec5SDimitry Andric                   __is_nothrow_swappable<__alloc_rr>::value)
4220b57cec5SDimitry Andric{
4230b57cec5SDimitry Andric    _VSTD::swap(__first_, __x.__first_);
4240b57cec5SDimitry Andric    _VSTD::swap(__begin_, __x.__begin_);
4250b57cec5SDimitry Andric    _VSTD::swap(__end_, __x.__end_);
4260b57cec5SDimitry Andric    _VSTD::swap(__end_cap(), __x.__end_cap());
4270b57cec5SDimitry Andric    __swap_allocator(__alloc(), __x.__alloc());
4280b57cec5SDimitry Andric}
4290b57cec5SDimitry Andric
4300b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
4310b57cec5SDimitry Andricvoid
4320b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::reserve(size_type __n)
4330b57cec5SDimitry Andric{
4340b57cec5SDimitry Andric    if (__n < capacity())
4350b57cec5SDimitry Andric    {
4360b57cec5SDimitry Andric        __split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc());
4370b57cec5SDimitry Andric        __t.__construct_at_end(move_iterator<pointer>(__begin_),
4380b57cec5SDimitry Andric                               move_iterator<pointer>(__end_));
4390b57cec5SDimitry Andric        _VSTD::swap(__first_, __t.__first_);
4400b57cec5SDimitry Andric        _VSTD::swap(__begin_, __t.__begin_);
4410b57cec5SDimitry Andric        _VSTD::swap(__end_, __t.__end_);
4420b57cec5SDimitry Andric        _VSTD::swap(__end_cap(), __t.__end_cap());
4430b57cec5SDimitry Andric    }
4440b57cec5SDimitry Andric}
4450b57cec5SDimitry Andric
4460b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
4470b57cec5SDimitry Andricvoid
4480b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
4490b57cec5SDimitry Andric{
4500b57cec5SDimitry Andric    if (capacity() > size())
4510b57cec5SDimitry Andric    {
4520b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
4530b57cec5SDimitry Andric        try
4540b57cec5SDimitry Andric        {
4550b57cec5SDimitry Andric#endif  // _LIBCPP_NO_EXCEPTIONS
4560b57cec5SDimitry Andric            __split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc());
4570b57cec5SDimitry Andric            __t.__construct_at_end(move_iterator<pointer>(__begin_),
4580b57cec5SDimitry Andric                                   move_iterator<pointer>(__end_));
4590b57cec5SDimitry Andric            __t.__end_ = __t.__begin_ + (__end_ - __begin_);
4600b57cec5SDimitry Andric            _VSTD::swap(__first_, __t.__first_);
4610b57cec5SDimitry Andric            _VSTD::swap(__begin_, __t.__begin_);
4620b57cec5SDimitry Andric            _VSTD::swap(__end_, __t.__end_);
4630b57cec5SDimitry Andric            _VSTD::swap(__end_cap(), __t.__end_cap());
4640b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
4650b57cec5SDimitry Andric        }
4660b57cec5SDimitry Andric        catch (...)
4670b57cec5SDimitry Andric        {
4680b57cec5SDimitry Andric        }
4690b57cec5SDimitry Andric#endif  // _LIBCPP_NO_EXCEPTIONS
4700b57cec5SDimitry Andric    }
4710b57cec5SDimitry Andric}
4720b57cec5SDimitry Andric
4730b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
4740b57cec5SDimitry Andricvoid
4750b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_front(const_reference __x)
4760b57cec5SDimitry Andric{
4770b57cec5SDimitry Andric    if (__begin_ == __first_)
4780b57cec5SDimitry Andric    {
4790b57cec5SDimitry Andric        if (__end_ < __end_cap())
4800b57cec5SDimitry Andric        {
4810b57cec5SDimitry Andric            difference_type __d = __end_cap() - __end_;
4820b57cec5SDimitry Andric            __d = (__d + 1) / 2;
4830b57cec5SDimitry Andric            __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d);
4840b57cec5SDimitry Andric            __end_ += __d;
4850b57cec5SDimitry Andric        }
4860b57cec5SDimitry Andric        else
4870b57cec5SDimitry Andric        {
4880b57cec5SDimitry Andric            size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
4890b57cec5SDimitry Andric            __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
4900b57cec5SDimitry Andric            __t.__construct_at_end(move_iterator<pointer>(__begin_),
4910b57cec5SDimitry Andric                                   move_iterator<pointer>(__end_));
4920b57cec5SDimitry Andric            _VSTD::swap(__first_, __t.__first_);
4930b57cec5SDimitry Andric            _VSTD::swap(__begin_, __t.__begin_);
4940b57cec5SDimitry Andric            _VSTD::swap(__end_, __t.__end_);
4950b57cec5SDimitry Andric            _VSTD::swap(__end_cap(), __t.__end_cap());
4960b57cec5SDimitry Andric        }
4970b57cec5SDimitry Andric    }
4980b57cec5SDimitry Andric    __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__begin_-1), __x);
4990b57cec5SDimitry Andric    --__begin_;
5000b57cec5SDimitry Andric}
5010b57cec5SDimitry Andric
5020b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
5030b57cec5SDimitry Andric
5040b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
5050b57cec5SDimitry Andricvoid
5060b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_front(value_type&& __x)
5070b57cec5SDimitry Andric{
5080b57cec5SDimitry Andric    if (__begin_ == __first_)
5090b57cec5SDimitry Andric    {
5100b57cec5SDimitry Andric        if (__end_ < __end_cap())
5110b57cec5SDimitry Andric        {
5120b57cec5SDimitry Andric            difference_type __d = __end_cap() - __end_;
5130b57cec5SDimitry Andric            __d = (__d + 1) / 2;
5140b57cec5SDimitry Andric            __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d);
5150b57cec5SDimitry Andric            __end_ += __d;
5160b57cec5SDimitry Andric        }
5170b57cec5SDimitry Andric        else
5180b57cec5SDimitry Andric        {
5190b57cec5SDimitry Andric            size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
5200b57cec5SDimitry Andric            __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
5210b57cec5SDimitry Andric            __t.__construct_at_end(move_iterator<pointer>(__begin_),
5220b57cec5SDimitry Andric                                   move_iterator<pointer>(__end_));
5230b57cec5SDimitry Andric            _VSTD::swap(__first_, __t.__first_);
5240b57cec5SDimitry Andric            _VSTD::swap(__begin_, __t.__begin_);
5250b57cec5SDimitry Andric            _VSTD::swap(__end_, __t.__end_);
5260b57cec5SDimitry Andric            _VSTD::swap(__end_cap(), __t.__end_cap());
5270b57cec5SDimitry Andric        }
5280b57cec5SDimitry Andric    }
5290b57cec5SDimitry Andric    __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__begin_-1),
5300b57cec5SDimitry Andric            _VSTD::move(__x));
5310b57cec5SDimitry Andric    --__begin_;
5320b57cec5SDimitry Andric}
5330b57cec5SDimitry Andric
5340b57cec5SDimitry Andric#endif  // _LIBCPP_CXX03_LANG
5350b57cec5SDimitry Andric
5360b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
5370b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
5380b57cec5SDimitry Andricvoid
5390b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_back(const_reference __x)
5400b57cec5SDimitry Andric{
5410b57cec5SDimitry Andric    if (__end_ == __end_cap())
5420b57cec5SDimitry Andric    {
5430b57cec5SDimitry Andric        if (__begin_ > __first_)
5440b57cec5SDimitry Andric        {
5450b57cec5SDimitry Andric            difference_type __d = __begin_ - __first_;
5460b57cec5SDimitry Andric            __d = (__d + 1) / 2;
5470b57cec5SDimitry Andric            __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
5480b57cec5SDimitry Andric            __begin_ -= __d;
5490b57cec5SDimitry Andric        }
5500b57cec5SDimitry Andric        else
5510b57cec5SDimitry Andric        {
5520b57cec5SDimitry Andric            size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
5530b57cec5SDimitry Andric            __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
5540b57cec5SDimitry Andric            __t.__construct_at_end(move_iterator<pointer>(__begin_),
5550b57cec5SDimitry Andric                                   move_iterator<pointer>(__end_));
5560b57cec5SDimitry Andric            _VSTD::swap(__first_, __t.__first_);
5570b57cec5SDimitry Andric            _VSTD::swap(__begin_, __t.__begin_);
5580b57cec5SDimitry Andric            _VSTD::swap(__end_, __t.__end_);
5590b57cec5SDimitry Andric            _VSTD::swap(__end_cap(), __t.__end_cap());
5600b57cec5SDimitry Andric        }
5610b57cec5SDimitry Andric    }
5620b57cec5SDimitry Andric    __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__end_), __x);
5630b57cec5SDimitry Andric    ++__end_;
5640b57cec5SDimitry Andric}
5650b57cec5SDimitry Andric
5660b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
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    }
5930b57cec5SDimitry Andric    __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__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    }
6240b57cec5SDimitry Andric    __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__end_),
6250b57cec5SDimitry Andric                              _VSTD::forward<_Args>(__args)...);
6260b57cec5SDimitry Andric    ++__end_;
6270b57cec5SDimitry Andric}
6280b57cec5SDimitry Andric
6290b57cec5SDimitry Andric#endif  // _LIBCPP_CXX03_LANG
6300b57cec5SDimitry Andric
6310b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
6320b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
6330b57cec5SDimitry Andricvoid
6340b57cec5SDimitry Andricswap(__split_buffer<_Tp, _Allocator>& __x, __split_buffer<_Tp, _Allocator>& __y)
6350b57cec5SDimitry Andric        _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
6360b57cec5SDimitry Andric{
6370b57cec5SDimitry Andric    __x.swap(__y);
6380b57cec5SDimitry Andric}
6390b57cec5SDimitry Andric
6400b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
6410b57cec5SDimitry Andric
6420b57cec5SDimitry Andric_LIBCPP_POP_MACROS
6430b57cec5SDimitry Andric
6440b57cec5SDimitry Andric#endif  // _LIBCPP_SPLIT_BUFFER
645