13e519524SHoward Hinnant// -*- C++ -*-
2cc82a1b0SLouis Dionne//===----------------------------------------------------------------------===//
3cc82a1b0SLouis Dionne//
4cc82a1b0SLouis Dionne// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5cc82a1b0SLouis Dionne// See https://llvm.org/LICENSE.txt for license information.
6cc82a1b0SLouis Dionne// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7cc82a1b0SLouis Dionne//
8cc82a1b0SLouis Dionne//===----------------------------------------------------------------------===//
9cc82a1b0SLouis Dionne
10cc82a1b0SLouis Dionne#ifndef _LIBCPP___SPLIT_BUFFER
11cc82a1b0SLouis Dionne#define _LIBCPP___SPLIT_BUFFER
123e519524SHoward Hinnant
132e2f3158SNikolas Klauser#include <__algorithm/max.h>
142e2f3158SNikolas Klauser#include <__algorithm/move.h>
152e2f3158SNikolas Klauser#include <__algorithm/move_backward.h>
163e519524SHoward Hinnant#include <__config>
172e2f3158SNikolas Klauser#include <__iterator/distance.h>
182e2f3158SNikolas Klauser#include <__iterator/iterator_traits.h>
192e2f3158SNikolas Klauser#include <__iterator/move_iterator.h>
202e2f3158SNikolas Klauser#include <__memory/allocator.h>
212e2f3158SNikolas Klauser#include <__memory/compressed_pair.h>
226adbc83eSChristopher Di Bella#include <__utility/forward.h>
232e2f3158SNikolas Klauser#include <memory>
24bfbd73f8SArthur O'Dwyer#include <type_traits>
253e519524SHoward Hinnant
26073458b1SHoward Hinnant#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
273e519524SHoward Hinnant#  pragma GCC system_header
28073458b1SHoward Hinnant#endif
293e519524SHoward Hinnant
30a016efb1SEric Fiselier_LIBCPP_PUSH_MACROS
31a016efb1SEric Fiselier#include <__undef_macros>
32a016efb1SEric Fiselier
33a016efb1SEric Fiselier
343e519524SHoward Hinnant_LIBCPP_BEGIN_NAMESPACE_STD
353e519524SHoward Hinnant
36189b2126SHoward Hinnanttemplate <class _Tp, class _Allocator = allocator<_Tp> >
373e519524SHoward Hinnantstruct __split_buffer
383e519524SHoward Hinnant{
393e519524SHoward Hinnantprivate:
403e519524SHoward Hinnant    __split_buffer(const __split_buffer&);
413e519524SHoward Hinnant    __split_buffer& operator=(const __split_buffer&);
423e519524SHoward Hinnantpublic:
433e519524SHoward Hinnant    typedef _Tp                                             value_type;
443e519524SHoward Hinnant    typedef _Allocator                                      allocator_type;
453e519524SHoward Hinnant    typedef typename remove_reference<allocator_type>::type __alloc_rr;
463e519524SHoward Hinnant    typedef allocator_traits<__alloc_rr>                    __alloc_traits;
473e519524SHoward Hinnant    typedef value_type&                                     reference;
483e519524SHoward Hinnant    typedef const value_type&                               const_reference;
493e519524SHoward Hinnant    typedef typename __alloc_traits::size_type              size_type;
503e519524SHoward Hinnant    typedef typename __alloc_traits::difference_type        difference_type;
513e519524SHoward Hinnant    typedef typename __alloc_traits::pointer                pointer;
523e519524SHoward Hinnant    typedef typename __alloc_traits::const_pointer          const_pointer;
533e519524SHoward Hinnant    typedef pointer                                         iterator;
543e519524SHoward Hinnant    typedef const_pointer                                   const_iterator;
553e519524SHoward Hinnant
563e519524SHoward Hinnant    pointer                                         __first_;
573e519524SHoward Hinnant    pointer                                         __begin_;
583e519524SHoward Hinnant    pointer                                         __end_;
593e519524SHoward Hinnant    __compressed_pair<pointer, allocator_type> __end_cap_;
603e519524SHoward Hinnant
613e519524SHoward Hinnant    typedef typename add_lvalue_reference<allocator_type>::type __alloc_ref;
623e519524SHoward Hinnant    typedef typename add_lvalue_reference<allocator_type>::type __alloc_const_ref;
633e519524SHoward Hinnant
649eebe11dSHoward Hinnant    _LIBCPP_INLINE_VISIBILITY __alloc_rr&           __alloc() _NOEXCEPT         {return __end_cap_.second();}
659eebe11dSHoward Hinnant    _LIBCPP_INLINE_VISIBILITY const __alloc_rr&     __alloc() const _NOEXCEPT   {return __end_cap_.second();}
669eebe11dSHoward Hinnant    _LIBCPP_INLINE_VISIBILITY pointer&              __end_cap() _NOEXCEPT       {return __end_cap_.first();}
679eebe11dSHoward Hinnant    _LIBCPP_INLINE_VISIBILITY const pointer&        __end_cap() const _NOEXCEPT {return __end_cap_.first();}
683e519524SHoward Hinnant
69906c872dSEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
7080129113SHoward Hinnant    __split_buffer()
7180129113SHoward Hinnant        _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
72906c872dSEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
733e519524SHoward Hinnant    explicit __split_buffer(__alloc_rr& __a);
74906c872dSEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
753e519524SHoward Hinnant    explicit __split_buffer(const __alloc_rr& __a);
763e519524SHoward Hinnant    __split_buffer(size_type __cap, size_type __start, __alloc_rr& __a);
773e519524SHoward Hinnant    ~__split_buffer();
783e519524SHoward Hinnant
799eebe11dSHoward Hinnant    __split_buffer(__split_buffer&& __c)
809eebe11dSHoward Hinnant        _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
813e519524SHoward Hinnant    __split_buffer(__split_buffer&& __c, const __alloc_rr& __a);
829eebe11dSHoward Hinnant    __split_buffer& operator=(__split_buffer&& __c)
839eebe11dSHoward Hinnant        _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
849eebe11dSHoward Hinnant                is_nothrow_move_assignable<allocator_type>::value) ||
859eebe11dSHoward Hinnant               !__alloc_traits::propagate_on_container_move_assignment::value);
863e519524SHoward Hinnant
879eebe11dSHoward Hinnant    _LIBCPP_INLINE_VISIBILITY       iterator begin() _NOEXCEPT       {return __begin_;}
889eebe11dSHoward Hinnant    _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT {return __begin_;}
899eebe11dSHoward Hinnant    _LIBCPP_INLINE_VISIBILITY       iterator end() _NOEXCEPT         {return __end_;}
909eebe11dSHoward Hinnant    _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT   {return __end_;}
913e519524SHoward Hinnant
929eebe11dSHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
939eebe11dSHoward Hinnant    void clear() _NOEXCEPT
949eebe11dSHoward Hinnant        {__destruct_at_end(__begin_);}
953e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY size_type size() const {return static_cast<size_type>(__end_ - __begin_);}
963e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY bool empty()     const {return __end_ == __begin_;}
973e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY size_type capacity() const {return static_cast<size_type>(__end_cap() - __first_);}
983e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY size_type __front_spare() const {return static_cast<size_type>(__begin_ - __first_);}
993e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY size_type __back_spare() const {return static_cast<size_type>(__end_cap() - __end_);}
1003e519524SHoward Hinnant
1013e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY       reference front()       {return *__begin_;}
1023e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY const_reference front() const {return *__begin_;}
1033e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY       reference back()        {return *(__end_ - 1);}
1043e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY const_reference back() const  {return *(__end_ - 1);}
1053e519524SHoward Hinnant
1063e519524SHoward Hinnant    void reserve(size_type __n);
1079eebe11dSHoward Hinnant    void shrink_to_fit() _NOEXCEPT;
1083e519524SHoward Hinnant    void push_front(const_reference __x);
1099741d6c9SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
1103e519524SHoward Hinnant    void push_front(value_type&& __x);
1113e519524SHoward Hinnant    void push_back(value_type&& __x);
1123e519524SHoward Hinnant    template <class... _Args>
1133e519524SHoward Hinnant        void emplace_back(_Args&&... __args);
1143e519524SHoward Hinnant
1153e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY void pop_front() {__destruct_at_begin(__begin_+1);}
1163e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY void pop_back() {__destruct_at_end(__end_-1);}
1173e519524SHoward Hinnant
1183e519524SHoward Hinnant    void __construct_at_end(size_type __n);
1193e519524SHoward Hinnant    void __construct_at_end(size_type __n, const_reference __x);
1203e519524SHoward Hinnant    template <class _InputIter>
121*4887d047SNikolas Klauser    __enable_if_t<__is_cpp17_input_iterator<_InputIter>::value && !__is_cpp17_forward_iterator<_InputIter>::value>
1223e519524SHoward Hinnant        __construct_at_end(_InputIter __first, _InputIter __last);
1233e519524SHoward Hinnant    template <class _ForwardIterator>
124*4887d047SNikolas Klauser    __enable_if_t<__is_cpp17_forward_iterator<_ForwardIterator>::value>
1253e519524SHoward Hinnant        __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
1263e519524SHoward Hinnant
1273e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY void __destruct_at_begin(pointer __new_begin)
128ca740483SHoward Hinnant        {__destruct_at_begin(__new_begin, is_trivially_destructible<value_type>());}
129906c872dSEvgeniy Stepanov        _LIBCPP_INLINE_VISIBILITY
1303e519524SHoward Hinnant        void __destruct_at_begin(pointer __new_begin, false_type);
131906c872dSEvgeniy Stepanov        _LIBCPP_INLINE_VISIBILITY
1323e519524SHoward Hinnant        void __destruct_at_begin(pointer __new_begin, true_type);
1333e519524SHoward Hinnant
1349eebe11dSHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
1359eebe11dSHoward Hinnant    void __destruct_at_end(pointer __new_last) _NOEXCEPT
1369741d6c9SHoward Hinnant        {__destruct_at_end(__new_last, false_type());}
1379741d6c9SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
1389eebe11dSHoward Hinnant        void __destruct_at_end(pointer __new_last, false_type) _NOEXCEPT;
1399741d6c9SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
1409eebe11dSHoward Hinnant        void __destruct_at_end(pointer __new_last, true_type) _NOEXCEPT;
1413e519524SHoward Hinnant
1429eebe11dSHoward Hinnant    void swap(__split_buffer& __x)
1439eebe11dSHoward Hinnant        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
1449eebe11dSHoward Hinnant                   __is_nothrow_swappable<__alloc_rr>::value);
1453e519524SHoward Hinnant
1463e519524SHoward Hinnant    bool __invariants() const;
1473e519524SHoward Hinnant
1483e519524SHoward Hinnantprivate:
149f5ab703fSHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
1508668139fSHoward Hinnant    void __move_assign_alloc(__split_buffer& __c, true_type)
1519eebe11dSHoward Hinnant        _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1523e519524SHoward Hinnant        {
153ce48a113SHoward Hinnant            __alloc() = _VSTD::move(__c.__alloc());
1543e519524SHoward Hinnant        }
1553e519524SHoward Hinnant
156f5ab703fSHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
157c206366fSHoward Hinnant    void __move_assign_alloc(__split_buffer&, false_type) _NOEXCEPT
1583e519524SHoward Hinnant        {}
159b0945e1bSEric Fiselier
160b0945e1bSEric Fiselier    struct _ConstructTransaction {
161b0945e1bSEric Fiselier      explicit _ConstructTransaction(pointer* __p, size_type __n) _NOEXCEPT
162b0945e1bSEric Fiselier      : __pos_(*__p), __end_(*__p + __n), __dest_(__p) {
163b0945e1bSEric Fiselier      }
164b0945e1bSEric Fiselier      ~_ConstructTransaction() {
165b0945e1bSEric Fiselier        *__dest_ = __pos_;
166b0945e1bSEric Fiselier      }
167b0945e1bSEric Fiselier      pointer __pos_;
168b0945e1bSEric Fiselier     const pointer __end_;
169b0945e1bSEric Fiselier    private:
170b0945e1bSEric Fiselier     pointer *__dest_;
171b0945e1bSEric Fiselier    };
1723e519524SHoward Hinnant};
1733e519524SHoward Hinnant
1743e519524SHoward Hinnanttemplate <class _Tp, class _Allocator>
1753e519524SHoward Hinnantbool
1763e519524SHoward Hinnant__split_buffer<_Tp, _Allocator>::__invariants() const
1773e519524SHoward Hinnant{
1783e519524SHoward Hinnant    if (__first_ == nullptr)
1793e519524SHoward Hinnant    {
1803e519524SHoward Hinnant        if (__begin_ != nullptr)
1813e519524SHoward Hinnant            return false;
1823e519524SHoward Hinnant        if (__end_ != nullptr)
1833e519524SHoward Hinnant            return false;
1843e519524SHoward Hinnant        if (__end_cap() != nullptr)
1853e519524SHoward Hinnant            return false;
1863e519524SHoward Hinnant    }
1873e519524SHoward Hinnant    else
1883e519524SHoward Hinnant    {
1893e519524SHoward Hinnant        if (__begin_ < __first_)
1903e519524SHoward Hinnant            return false;
1913e519524SHoward Hinnant        if (__end_ < __begin_)
1923e519524SHoward Hinnant            return false;
1933e519524SHoward Hinnant        if (__end_cap() < __end_)
1943e519524SHoward Hinnant            return false;
1953e519524SHoward Hinnant    }
1963e519524SHoward Hinnant    return true;
1973e519524SHoward Hinnant}
1983e519524SHoward Hinnant
1993e519524SHoward Hinnant//  Default constructs __n objects starting at __end_
2003e519524SHoward Hinnant//  throws if construction throws
2013e519524SHoward Hinnant//  Precondition:  __n > 0
2023e519524SHoward Hinnant//  Precondition:  size() + __n <= capacity()
2033e519524SHoward Hinnant//  Postcondition:  size() == size() + __n
2043e519524SHoward Hinnanttemplate <class _Tp, class _Allocator>
2053e519524SHoward Hinnantvoid
2063e519524SHoward Hinnant__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n)
2073e519524SHoward Hinnant{
208b0945e1bSEric Fiselier    _ConstructTransaction __tx(&this->__end_, __n);
209b0945e1bSEric Fiselier    for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
2100068c591SEric Fiselier        __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__tx.__pos_));
211b0945e1bSEric Fiselier    }
2123e519524SHoward Hinnant}
2133e519524SHoward Hinnant
2143e519524SHoward Hinnant//  Copy constructs __n objects starting at __end_ from __x
2153e519524SHoward Hinnant//  throws if construction throws
2163e519524SHoward Hinnant//  Precondition:  __n > 0
2173e519524SHoward Hinnant//  Precondition:  size() + __n <= capacity()
2183e519524SHoward Hinnant//  Postcondition:  size() == old size() + __n
2193e519524SHoward Hinnant//  Postcondition:  [i] == __x for all i in [size() - __n, __n)
2203e519524SHoward Hinnanttemplate <class _Tp, class _Allocator>
2213e519524SHoward Hinnantvoid
2223e519524SHoward Hinnant__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
2233e519524SHoward Hinnant{
224b0945e1bSEric Fiselier    _ConstructTransaction __tx(&this->__end_, __n);
225b0945e1bSEric Fiselier    for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
226b0945e1bSEric Fiselier        __alloc_traits::construct(this->__alloc(),
2270068c591SEric Fiselier            _VSTD::__to_address(__tx.__pos_), __x);
228b0945e1bSEric Fiselier    }
2293e519524SHoward Hinnant}
2303e519524SHoward Hinnant
2313e519524SHoward Hinnanttemplate <class _Tp, class _Allocator>
2323e519524SHoward Hinnanttemplate <class _InputIter>
233*4887d047SNikolas Klauser__enable_if_t<__is_cpp17_input_iterator<_InputIter>::value && !__is_cpp17_forward_iterator<_InputIter>::value>
2343e519524SHoward Hinnant__split_buffer<_Tp, _Allocator>::__construct_at_end(_InputIter __first, _InputIter __last)
2353e519524SHoward Hinnant{
2363e519524SHoward Hinnant    __alloc_rr& __a = this->__alloc();
2373e519524SHoward Hinnant    for (; __first != __last; ++__first)
2383e519524SHoward Hinnant    {
2393e519524SHoward Hinnant        if (__end_ == __end_cap())
2403e519524SHoward Hinnant        {
2413e519524SHoward Hinnant            size_type __old_cap = __end_cap() - __first_;
242ce48a113SHoward Hinnant            size_type __new_cap = _VSTD::max<size_type>(2 * __old_cap, 8);
2433e519524SHoward Hinnant            __split_buffer __buf(__new_cap, 0, __a);
24416bf4339SArthur O'Dwyer            for (pointer __p = __begin_; __p != __end_; ++__p, (void) ++__buf.__end_)
2453e519524SHoward Hinnant                __alloc_traits::construct(__buf.__alloc(),
2460068c591SEric Fiselier                        _VSTD::__to_address(__buf.__end_), _VSTD::move(*__p));
2473e519524SHoward Hinnant            swap(__buf);
2483e519524SHoward Hinnant        }
2490068c591SEric Fiselier        __alloc_traits::construct(__a, _VSTD::__to_address(this->__end_), *__first);
2503e519524SHoward Hinnant        ++this->__end_;
2513e519524SHoward Hinnant    }
2523e519524SHoward Hinnant}
2533e519524SHoward Hinnant
2543e519524SHoward Hinnanttemplate <class _Tp, class _Allocator>
2553e519524SHoward Hinnanttemplate <class _ForwardIterator>
256*4887d047SNikolas Klauser__enable_if_t<__is_cpp17_forward_iterator<_ForwardIterator>::value>
2573e519524SHoward Hinnant__split_buffer<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2583e519524SHoward Hinnant{
259d586f92cSArthur O'Dwyer    _ConstructTransaction __tx(&this->__end_, _VSTD::distance(__first, __last));
26016bf4339SArthur O'Dwyer    for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void) ++__first) {
261b0945e1bSEric Fiselier        __alloc_traits::construct(this->__alloc(),
2620068c591SEric Fiselier            _VSTD::__to_address(__tx.__pos_), *__first);
2633e519524SHoward Hinnant    }
2643e519524SHoward Hinnant}
2653e519524SHoward Hinnant
2663e519524SHoward Hinnanttemplate <class _Tp, class _Allocator>
267906c872dSEvgeniy Stepanovinline
2683e519524SHoward Hinnantvoid
2693e519524SHoward Hinnant__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type)
2703e519524SHoward Hinnant{
2719741d6c9SHoward Hinnant    while (__begin_ != __new_begin)
2726e965df6SArthur O'Dwyer        __alloc_traits::destroy(__alloc(), _VSTD::__to_address(__begin_++));
2733e519524SHoward Hinnant}
2743e519524SHoward Hinnant
2753e519524SHoward Hinnanttemplate <class _Tp, class _Allocator>
276906c872dSEvgeniy Stepanovinline
2773e519524SHoward Hinnantvoid
2783e519524SHoward Hinnant__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, true_type)
2793e519524SHoward Hinnant{
2803e519524SHoward Hinnant    __begin_ = __new_begin;
2813e519524SHoward Hinnant}
2823e519524SHoward Hinnant
2833e519524SHoward Hinnanttemplate <class _Tp, class _Allocator>
2843af48ef7SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
2853e519524SHoward Hinnantvoid
2869eebe11dSHoward Hinnant__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT
2873e519524SHoward Hinnant{
2889741d6c9SHoward Hinnant    while (__new_last != __end_)
2896e965df6SArthur O'Dwyer        __alloc_traits::destroy(__alloc(), _VSTD::__to_address(--__end_));
2903e519524SHoward Hinnant}
2913e519524SHoward Hinnant
2923e519524SHoward Hinnanttemplate <class _Tp, class _Allocator>
2933af48ef7SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
2943e519524SHoward Hinnantvoid
2959eebe11dSHoward Hinnant__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, true_type) _NOEXCEPT
2963e519524SHoward Hinnant{
2973e519524SHoward Hinnant    __end_ = __new_last;
2983e519524SHoward Hinnant}
2993e519524SHoward Hinnant
3003e519524SHoward Hinnanttemplate <class _Tp, class _Allocator>
3013e519524SHoward Hinnant__split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __start, __alloc_rr& __a)
30214e200d1SHoward Hinnant    : __end_cap_(nullptr, __a)
3033e519524SHoward Hinnant{
304a96443edSNikolas Klauser    if (__cap == 0) {
305a96443edSNikolas Klauser        __first_ = nullptr;
306a96443edSNikolas Klauser    } else {
307a96443edSNikolas Klauser        auto __allocation = std::__allocate_at_least(__alloc(), __cap);
308a96443edSNikolas Klauser        __first_ = __allocation.ptr;
309a96443edSNikolas Klauser        __cap = __allocation.count;
310a96443edSNikolas Klauser    }
3113e519524SHoward Hinnant    __begin_ = __end_ = __first_ + __start;
3123e519524SHoward Hinnant    __end_cap() = __first_ + __cap;
3133e519524SHoward Hinnant}
3143e519524SHoward Hinnant
3153e519524SHoward Hinnanttemplate <class _Tp, class _Allocator>
316906c872dSEvgeniy Stepanovinline
3173e519524SHoward Hinnant__split_buffer<_Tp, _Allocator>::__split_buffer()
31880129113SHoward Hinnant    _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
319549545b6SEric Fiselier    : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __default_init_tag())
3203e519524SHoward Hinnant{
3213e519524SHoward Hinnant}
3223e519524SHoward Hinnant
3233e519524SHoward Hinnanttemplate <class _Tp, class _Allocator>
324906c872dSEvgeniy Stepanovinline
3253e519524SHoward Hinnant__split_buffer<_Tp, _Allocator>::__split_buffer(__alloc_rr& __a)
32614e200d1SHoward Hinnant    : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a)
3273e519524SHoward Hinnant{
3283e519524SHoward Hinnant}
3293e519524SHoward Hinnant
3303e519524SHoward Hinnanttemplate <class _Tp, class _Allocator>
331906c872dSEvgeniy Stepanovinline
3323e519524SHoward Hinnant__split_buffer<_Tp, _Allocator>::__split_buffer(const __alloc_rr& __a)
33314e200d1SHoward Hinnant    : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a)
3343e519524SHoward Hinnant{
3353e519524SHoward Hinnant}
3363e519524SHoward Hinnant
3373e519524SHoward Hinnanttemplate <class _Tp, class _Allocator>
3383e519524SHoward Hinnant__split_buffer<_Tp, _Allocator>::~__split_buffer()
3393e519524SHoward Hinnant{
3403e519524SHoward Hinnant    clear();
3413e519524SHoward Hinnant    if (__first_)
3423e519524SHoward Hinnant        __alloc_traits::deallocate(__alloc(), __first_, capacity());
3433e519524SHoward Hinnant}
3443e519524SHoward Hinnant
3453e519524SHoward Hinnanttemplate <class _Tp, class _Allocator>
3463e519524SHoward Hinnant__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c)
3479eebe11dSHoward Hinnant    _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
348ce48a113SHoward Hinnant    : __first_(_VSTD::move(__c.__first_)),
349ce48a113SHoward Hinnant      __begin_(_VSTD::move(__c.__begin_)),
350ce48a113SHoward Hinnant      __end_(_VSTD::move(__c.__end_)),
351ce48a113SHoward Hinnant      __end_cap_(_VSTD::move(__c.__end_cap_))
3523e519524SHoward Hinnant{
3533e519524SHoward Hinnant    __c.__first_ = nullptr;
3543e519524SHoward Hinnant    __c.__begin_ = nullptr;
3553e519524SHoward Hinnant    __c.__end_ = nullptr;
3563e519524SHoward Hinnant    __c.__end_cap() = nullptr;
3573e519524SHoward Hinnant}
3583e519524SHoward Hinnant
3593e519524SHoward Hinnanttemplate <class _Tp, class _Allocator>
3603e519524SHoward Hinnant__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a)
361549545b6SEric Fiselier    : __end_cap_(nullptr, __a)
3623e519524SHoward Hinnant{
3633e519524SHoward Hinnant    if (__a == __c.__alloc())
3643e519524SHoward Hinnant    {
3653e519524SHoward Hinnant        __first_ = __c.__first_;
3663e519524SHoward Hinnant        __begin_ = __c.__begin_;
3673e519524SHoward Hinnant        __end_ = __c.__end_;
3683e519524SHoward Hinnant        __end_cap() = __c.__end_cap();
3693e519524SHoward Hinnant        __c.__first_ = nullptr;
3703e519524SHoward Hinnant        __c.__begin_ = nullptr;
3713e519524SHoward Hinnant        __c.__end_ = nullptr;
3723e519524SHoward Hinnant        __c.__end_cap() = nullptr;
3733e519524SHoward Hinnant    }
3743e519524SHoward Hinnant    else
3753e519524SHoward Hinnant    {
376a96443edSNikolas Klauser        auto __allocation = std::__allocate_at_least(__alloc(), __c.size());
377a96443edSNikolas Klauser        __first_ = __allocation.ptr;
3783e519524SHoward Hinnant        __begin_ = __end_ = __first_;
379a96443edSNikolas Klauser        __end_cap() = __first_ + __allocation.count;
380c003db1fSHoward Hinnant        typedef move_iterator<iterator> _Ip;
381c003db1fSHoward Hinnant        __construct_at_end(_Ip(__c.begin()), _Ip(__c.end()));
3823e519524SHoward Hinnant    }
3833e519524SHoward Hinnant}
3843e519524SHoward Hinnant
3853e519524SHoward Hinnanttemplate <class _Tp, class _Allocator>
3863e519524SHoward Hinnant__split_buffer<_Tp, _Allocator>&
3873e519524SHoward Hinnant__split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c)
3889eebe11dSHoward Hinnant    _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
3899eebe11dSHoward Hinnant                is_nothrow_move_assignable<allocator_type>::value) ||
3909eebe11dSHoward Hinnant               !__alloc_traits::propagate_on_container_move_assignment::value)
3913e519524SHoward Hinnant{
3923e519524SHoward Hinnant    clear();
3933e519524SHoward Hinnant    shrink_to_fit();
3943e519524SHoward Hinnant    __first_ = __c.__first_;
3953e519524SHoward Hinnant    __begin_ = __c.__begin_;
3963e519524SHoward Hinnant    __end_ = __c.__end_;
3973e519524SHoward Hinnant    __end_cap() = __c.__end_cap();
3983e519524SHoward Hinnant    __move_assign_alloc(__c,
3993e519524SHoward Hinnant        integral_constant<bool,
4003e519524SHoward Hinnant                          __alloc_traits::propagate_on_container_move_assignment::value>());
4013e519524SHoward Hinnant    __c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
4023e519524SHoward Hinnant    return *this;
4033e519524SHoward Hinnant}
4043e519524SHoward Hinnant
4053e519524SHoward Hinnanttemplate <class _Tp, class _Allocator>
4063e519524SHoward Hinnantvoid
4073e519524SHoward Hinnant__split_buffer<_Tp, _Allocator>::swap(__split_buffer& __x)
4089eebe11dSHoward Hinnant        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
4099eebe11dSHoward Hinnant                   __is_nothrow_swappable<__alloc_rr>::value)
4103e519524SHoward Hinnant{
411ce48a113SHoward Hinnant    _VSTD::swap(__first_, __x.__first_);
412ce48a113SHoward Hinnant    _VSTD::swap(__begin_, __x.__begin_);
413ce48a113SHoward Hinnant    _VSTD::swap(__end_, __x.__end_);
414ce48a113SHoward Hinnant    _VSTD::swap(__end_cap(), __x.__end_cap());
4156e965df6SArthur O'Dwyer    _VSTD::__swap_allocator(__alloc(), __x.__alloc());
4163e519524SHoward Hinnant}
4173e519524SHoward Hinnant
4183e519524SHoward Hinnanttemplate <class _Tp, class _Allocator>
4193e519524SHoward Hinnantvoid
4203e519524SHoward Hinnant__split_buffer<_Tp, _Allocator>::reserve(size_type __n)
4213e519524SHoward Hinnant{
4223e519524SHoward Hinnant    if (__n < capacity())
4233e519524SHoward Hinnant    {
4243e519524SHoward Hinnant        __split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc());
4253e519524SHoward Hinnant        __t.__construct_at_end(move_iterator<pointer>(__begin_),
4263e519524SHoward Hinnant                               move_iterator<pointer>(__end_));
427ce48a113SHoward Hinnant        _VSTD::swap(__first_, __t.__first_);
428ce48a113SHoward Hinnant        _VSTD::swap(__begin_, __t.__begin_);
429ce48a113SHoward Hinnant        _VSTD::swap(__end_, __t.__end_);
430ce48a113SHoward Hinnant        _VSTD::swap(__end_cap(), __t.__end_cap());
4313e519524SHoward Hinnant    }
4323e519524SHoward Hinnant}
4333e519524SHoward Hinnant
4343e519524SHoward Hinnanttemplate <class _Tp, class _Allocator>
4353e519524SHoward Hinnantvoid
4369eebe11dSHoward Hinnant__split_buffer<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
4373e519524SHoward Hinnant{
4383e519524SHoward Hinnant    if (capacity() > size())
4393e519524SHoward Hinnant    {
4403e519524SHoward Hinnant#ifndef _LIBCPP_NO_EXCEPTIONS
4413e519524SHoward Hinnant        try
4423e519524SHoward Hinnant        {
443b3371f6fSHoward Hinnant#endif // _LIBCPP_NO_EXCEPTIONS
4443e519524SHoward Hinnant            __split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc());
4453e519524SHoward Hinnant            __t.__construct_at_end(move_iterator<pointer>(__begin_),
4463e519524SHoward Hinnant                                   move_iterator<pointer>(__end_));
4473e519524SHoward Hinnant            __t.__end_ = __t.__begin_ + (__end_ - __begin_);
448ce48a113SHoward Hinnant            _VSTD::swap(__first_, __t.__first_);
449ce48a113SHoward Hinnant            _VSTD::swap(__begin_, __t.__begin_);
450ce48a113SHoward Hinnant            _VSTD::swap(__end_, __t.__end_);
451ce48a113SHoward Hinnant            _VSTD::swap(__end_cap(), __t.__end_cap());
4523e519524SHoward Hinnant#ifndef _LIBCPP_NO_EXCEPTIONS
4533e519524SHoward Hinnant        }
4543e519524SHoward Hinnant        catch (...)
4553e519524SHoward Hinnant        {
4563e519524SHoward Hinnant        }
457b3371f6fSHoward Hinnant#endif // _LIBCPP_NO_EXCEPTIONS
4583e519524SHoward Hinnant    }
4593e519524SHoward Hinnant}
4603e519524SHoward Hinnant
4613e519524SHoward Hinnanttemplate <class _Tp, class _Allocator>
4623e519524SHoward Hinnantvoid
4633e519524SHoward Hinnant__split_buffer<_Tp, _Allocator>::push_front(const_reference __x)
4643e519524SHoward Hinnant{
4653e519524SHoward Hinnant    if (__begin_ == __first_)
4663e519524SHoward Hinnant    {
4673e519524SHoward Hinnant        if (__end_ < __end_cap())
4683e519524SHoward Hinnant        {
4693e519524SHoward Hinnant            difference_type __d = __end_cap() - __end_;
4703e519524SHoward Hinnant            __d = (__d + 1) / 2;
471ce48a113SHoward Hinnant            __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d);
4723e519524SHoward Hinnant            __end_ += __d;
4733e519524SHoward Hinnant        }
4743e519524SHoward Hinnant        else
4753e519524SHoward Hinnant        {
476c206366fSHoward Hinnant            size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
477189b2126SHoward Hinnant            __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
4783e519524SHoward Hinnant            __t.__construct_at_end(move_iterator<pointer>(__begin_),
4793e519524SHoward Hinnant                                   move_iterator<pointer>(__end_));
480ce48a113SHoward Hinnant            _VSTD::swap(__first_, __t.__first_);
481ce48a113SHoward Hinnant            _VSTD::swap(__begin_, __t.__begin_);
482ce48a113SHoward Hinnant            _VSTD::swap(__end_, __t.__end_);
483ce48a113SHoward Hinnant            _VSTD::swap(__end_cap(), __t.__end_cap());
4843e519524SHoward Hinnant        }
4853e519524SHoward Hinnant    }
4860068c591SEric Fiselier    __alloc_traits::construct(__alloc(), _VSTD::__to_address(__begin_-1), __x);
4873e519524SHoward Hinnant    --__begin_;
4883e519524SHoward Hinnant}
4893e519524SHoward Hinnant
4903e519524SHoward Hinnanttemplate <class _Tp, class _Allocator>
4913e519524SHoward Hinnantvoid
4923e519524SHoward Hinnant__split_buffer<_Tp, _Allocator>::push_front(value_type&& __x)
4933e519524SHoward Hinnant{
4943e519524SHoward Hinnant    if (__begin_ == __first_)
4953e519524SHoward Hinnant    {
4963e519524SHoward Hinnant        if (__end_ < __end_cap())
4973e519524SHoward Hinnant        {
4983e519524SHoward Hinnant            difference_type __d = __end_cap() - __end_;
4993e519524SHoward Hinnant            __d = (__d + 1) / 2;
500ce48a113SHoward Hinnant            __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d);
5013e519524SHoward Hinnant            __end_ += __d;
5023e519524SHoward Hinnant        }
5033e519524SHoward Hinnant        else
5043e519524SHoward Hinnant        {
505c206366fSHoward Hinnant            size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
506189b2126SHoward Hinnant            __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
5073e519524SHoward Hinnant            __t.__construct_at_end(move_iterator<pointer>(__begin_),
5083e519524SHoward Hinnant                                   move_iterator<pointer>(__end_));
509ce48a113SHoward Hinnant            _VSTD::swap(__first_, __t.__first_);
510ce48a113SHoward Hinnant            _VSTD::swap(__begin_, __t.__begin_);
511ce48a113SHoward Hinnant            _VSTD::swap(__end_, __t.__end_);
512ce48a113SHoward Hinnant            _VSTD::swap(__end_cap(), __t.__end_cap());
5133e519524SHoward Hinnant        }
5143e519524SHoward Hinnant    }
5150068c591SEric Fiselier    __alloc_traits::construct(__alloc(), _VSTD::__to_address(__begin_-1),
516ce48a113SHoward Hinnant            _VSTD::move(__x));
5173e519524SHoward Hinnant    --__begin_;
5183e519524SHoward Hinnant}
5193e519524SHoward Hinnant
5203e519524SHoward Hinnanttemplate <class _Tp, class _Allocator>
5213af48ef7SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
5223e519524SHoward Hinnantvoid
5233e519524SHoward Hinnant__split_buffer<_Tp, _Allocator>::push_back(const_reference __x)
5243e519524SHoward Hinnant{
5253e519524SHoward Hinnant    if (__end_ == __end_cap())
5263e519524SHoward Hinnant    {
5273e519524SHoward Hinnant        if (__begin_ > __first_)
5283e519524SHoward Hinnant        {
5293e519524SHoward Hinnant            difference_type __d = __begin_ - __first_;
5303e519524SHoward Hinnant            __d = (__d + 1) / 2;
531ce48a113SHoward Hinnant            __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
5323e519524SHoward Hinnant            __begin_ -= __d;
5333e519524SHoward Hinnant        }
5343e519524SHoward Hinnant        else
5353e519524SHoward Hinnant        {
536c206366fSHoward Hinnant            size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
5373e519524SHoward Hinnant            __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
5383e519524SHoward Hinnant            __t.__construct_at_end(move_iterator<pointer>(__begin_),
5393e519524SHoward Hinnant                                   move_iterator<pointer>(__end_));
540ce48a113SHoward Hinnant            _VSTD::swap(__first_, __t.__first_);
541ce48a113SHoward Hinnant            _VSTD::swap(__begin_, __t.__begin_);
542ce48a113SHoward Hinnant            _VSTD::swap(__end_, __t.__end_);
543ce48a113SHoward Hinnant            _VSTD::swap(__end_cap(), __t.__end_cap());
5443e519524SHoward Hinnant        }
5453e519524SHoward Hinnant    }
5460068c591SEric Fiselier    __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_), __x);
5473e519524SHoward Hinnant    ++__end_;
5483e519524SHoward Hinnant}
5493e519524SHoward Hinnant
5503e519524SHoward Hinnanttemplate <class _Tp, class _Allocator>
5513e519524SHoward Hinnantvoid
5523e519524SHoward Hinnant__split_buffer<_Tp, _Allocator>::push_back(value_type&& __x)
5533e519524SHoward Hinnant{
5543e519524SHoward Hinnant    if (__end_ == __end_cap())
5553e519524SHoward Hinnant    {
5563e519524SHoward Hinnant        if (__begin_ > __first_)
5573e519524SHoward Hinnant        {
5583e519524SHoward Hinnant            difference_type __d = __begin_ - __first_;
5593e519524SHoward Hinnant            __d = (__d + 1) / 2;
560ce48a113SHoward Hinnant            __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
5613e519524SHoward Hinnant            __begin_ -= __d;
5623e519524SHoward Hinnant        }
5633e519524SHoward Hinnant        else
5643e519524SHoward Hinnant        {
565c206366fSHoward Hinnant            size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
5663e519524SHoward Hinnant            __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
5673e519524SHoward Hinnant            __t.__construct_at_end(move_iterator<pointer>(__begin_),
5683e519524SHoward Hinnant                                   move_iterator<pointer>(__end_));
569ce48a113SHoward Hinnant            _VSTD::swap(__first_, __t.__first_);
570ce48a113SHoward Hinnant            _VSTD::swap(__begin_, __t.__begin_);
571ce48a113SHoward Hinnant            _VSTD::swap(__end_, __t.__end_);
572ce48a113SHoward Hinnant            _VSTD::swap(__end_cap(), __t.__end_cap());
5733e519524SHoward Hinnant        }
5743e519524SHoward Hinnant    }
5750068c591SEric Fiselier    __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_),
576ce48a113SHoward Hinnant            _VSTD::move(__x));
5773e519524SHoward Hinnant    ++__end_;
5783e519524SHoward Hinnant}
5793e519524SHoward Hinnant
5803e519524SHoward Hinnanttemplate <class _Tp, class _Allocator>
5813e519524SHoward Hinnanttemplate <class... _Args>
5823e519524SHoward Hinnantvoid
5833e519524SHoward Hinnant__split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args)
5843e519524SHoward Hinnant{
5853e519524SHoward Hinnant    if (__end_ == __end_cap())
5863e519524SHoward Hinnant    {
5873e519524SHoward Hinnant        if (__begin_ > __first_)
5883e519524SHoward Hinnant        {
5893e519524SHoward Hinnant            difference_type __d = __begin_ - __first_;
5903e519524SHoward Hinnant            __d = (__d + 1) / 2;
591ce48a113SHoward Hinnant            __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
5923e519524SHoward Hinnant            __begin_ -= __d;
5933e519524SHoward Hinnant        }
5943e519524SHoward Hinnant        else
5953e519524SHoward Hinnant        {
596c206366fSHoward Hinnant            size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
5973e519524SHoward Hinnant            __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
5983e519524SHoward Hinnant            __t.__construct_at_end(move_iterator<pointer>(__begin_),
5993e519524SHoward Hinnant                                   move_iterator<pointer>(__end_));
600ce48a113SHoward Hinnant            _VSTD::swap(__first_, __t.__first_);
601ce48a113SHoward Hinnant            _VSTD::swap(__begin_, __t.__begin_);
602ce48a113SHoward Hinnant            _VSTD::swap(__end_, __t.__end_);
603ce48a113SHoward Hinnant            _VSTD::swap(__end_cap(), __t.__end_cap());
6043e519524SHoward Hinnant        }
6053e519524SHoward Hinnant    }
6060068c591SEric Fiselier    __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_),
607ce48a113SHoward Hinnant                              _VSTD::forward<_Args>(__args)...);
6083e519524SHoward Hinnant    ++__end_;
6093e519524SHoward Hinnant}
6103e519524SHoward Hinnant
6119eebe11dSHoward Hinnanttemplate <class _Tp, class _Allocator>
6123af48ef7SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
6139eebe11dSHoward Hinnantvoid
6149eebe11dSHoward Hinnantswap(__split_buffer<_Tp, _Allocator>& __x, __split_buffer<_Tp, _Allocator>& __y)
6159eebe11dSHoward Hinnant        _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
6169eebe11dSHoward Hinnant{
6179eebe11dSHoward Hinnant    __x.swap(__y);
6189eebe11dSHoward Hinnant}
6199eebe11dSHoward Hinnant
6203e519524SHoward Hinnant_LIBCPP_END_NAMESPACE_STD
6213e519524SHoward Hinnant
622a016efb1SEric Fiselier_LIBCPP_POP_MACROS
623a016efb1SEric Fiselier
624cc82a1b0SLouis Dionne#endif // _LIBCPP___SPLIT_BUFFER
625