1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___SPLIT_BUFFER
11#define _LIBCPP___SPLIT_BUFFER
12
13#include <__algorithm/max.h>
14#include <__algorithm/move.h>
15#include <__algorithm/move_backward.h>
16#include <__config>
17#include <__iterator/distance.h>
18#include <__iterator/iterator_traits.h>
19#include <__iterator/move_iterator.h>
20#include <__memory/allocator.h>
21#include <__memory/compressed_pair.h>
22#include <__utility/forward.h>
23#include <memory>
24#include <type_traits>
25
26#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27#  pragma GCC system_header
28#endif
29
30_LIBCPP_PUSH_MACROS
31#include <__undef_macros>
32
33
34_LIBCPP_BEGIN_NAMESPACE_STD
35
36template <class _Tp, class _Allocator = allocator<_Tp> >
37struct __split_buffer
38{
39private:
40    __split_buffer(const __split_buffer&);
41    __split_buffer& operator=(const __split_buffer&);
42public:
43    typedef _Tp                                             value_type;
44    typedef _Allocator                                      allocator_type;
45    typedef typename remove_reference<allocator_type>::type __alloc_rr;
46    typedef allocator_traits<__alloc_rr>                    __alloc_traits;
47    typedef value_type&                                     reference;
48    typedef const value_type&                               const_reference;
49    typedef typename __alloc_traits::size_type              size_type;
50    typedef typename __alloc_traits::difference_type        difference_type;
51    typedef typename __alloc_traits::pointer                pointer;
52    typedef typename __alloc_traits::const_pointer          const_pointer;
53    typedef pointer                                         iterator;
54    typedef const_pointer                                   const_iterator;
55
56    pointer                                         __first_;
57    pointer                                         __begin_;
58    pointer                                         __end_;
59    __compressed_pair<pointer, allocator_type> __end_cap_;
60
61    typedef typename add_lvalue_reference<allocator_type>::type __alloc_ref;
62    typedef typename add_lvalue_reference<allocator_type>::type __alloc_const_ref;
63
64    _LIBCPP_INLINE_VISIBILITY __alloc_rr&           __alloc() _NOEXCEPT         {return __end_cap_.second();}
65    _LIBCPP_INLINE_VISIBILITY const __alloc_rr&     __alloc() const _NOEXCEPT   {return __end_cap_.second();}
66    _LIBCPP_INLINE_VISIBILITY pointer&              __end_cap() _NOEXCEPT       {return __end_cap_.first();}
67    _LIBCPP_INLINE_VISIBILITY const pointer&        __end_cap() const _NOEXCEPT {return __end_cap_.first();}
68
69    _LIBCPP_INLINE_VISIBILITY
70    __split_buffer()
71        _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
72    _LIBCPP_INLINE_VISIBILITY
73    explicit __split_buffer(__alloc_rr& __a);
74    _LIBCPP_INLINE_VISIBILITY
75    explicit __split_buffer(const __alloc_rr& __a);
76    __split_buffer(size_type __cap, size_type __start, __alloc_rr& __a);
77    ~__split_buffer();
78
79    __split_buffer(__split_buffer&& __c)
80        _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
81    __split_buffer(__split_buffer&& __c, const __alloc_rr& __a);
82    __split_buffer& operator=(__split_buffer&& __c)
83        _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
84                is_nothrow_move_assignable<allocator_type>::value) ||
85               !__alloc_traits::propagate_on_container_move_assignment::value);
86
87    _LIBCPP_INLINE_VISIBILITY       iterator begin() _NOEXCEPT       {return __begin_;}
88    _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT {return __begin_;}
89    _LIBCPP_INLINE_VISIBILITY       iterator end() _NOEXCEPT         {return __end_;}
90    _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT   {return __end_;}
91
92    _LIBCPP_INLINE_VISIBILITY
93    void clear() _NOEXCEPT
94        {__destruct_at_end(__begin_);}
95    _LIBCPP_INLINE_VISIBILITY size_type size() const {return static_cast<size_type>(__end_ - __begin_);}
96    _LIBCPP_INLINE_VISIBILITY bool empty()     const {return __end_ == __begin_;}
97    _LIBCPP_INLINE_VISIBILITY size_type capacity() const {return static_cast<size_type>(__end_cap() - __first_);}
98    _LIBCPP_INLINE_VISIBILITY size_type __front_spare() const {return static_cast<size_type>(__begin_ - __first_);}
99    _LIBCPP_INLINE_VISIBILITY size_type __back_spare() const {return static_cast<size_type>(__end_cap() - __end_);}
100
101    _LIBCPP_INLINE_VISIBILITY       reference front()       {return *__begin_;}
102    _LIBCPP_INLINE_VISIBILITY const_reference front() const {return *__begin_;}
103    _LIBCPP_INLINE_VISIBILITY       reference back()        {return *(__end_ - 1);}
104    _LIBCPP_INLINE_VISIBILITY const_reference back() const  {return *(__end_ - 1);}
105
106    void reserve(size_type __n);
107    void shrink_to_fit() _NOEXCEPT;
108    void push_front(const_reference __x);
109    _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
110    void push_front(value_type&& __x);
111    void push_back(value_type&& __x);
112    template <class... _Args>
113        void emplace_back(_Args&&... __args);
114
115    _LIBCPP_INLINE_VISIBILITY void pop_front() {__destruct_at_begin(__begin_+1);}
116    _LIBCPP_INLINE_VISIBILITY void pop_back() {__destruct_at_end(__end_-1);}
117
118    void __construct_at_end(size_type __n);
119    void __construct_at_end(size_type __n, const_reference __x);
120    template <class _InputIter>
121        typename enable_if
122        <
123            __is_cpp17_input_iterator<_InputIter>::value &&
124           !__is_cpp17_forward_iterator<_InputIter>::value,
125            void
126        >::type
127        __construct_at_end(_InputIter __first, _InputIter __last);
128    template <class _ForwardIterator>
129        typename enable_if
130        <
131            __is_cpp17_forward_iterator<_ForwardIterator>::value,
132            void
133        >::type
134        __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
135
136    _LIBCPP_INLINE_VISIBILITY void __destruct_at_begin(pointer __new_begin)
137        {__destruct_at_begin(__new_begin, is_trivially_destructible<value_type>());}
138        _LIBCPP_INLINE_VISIBILITY
139        void __destruct_at_begin(pointer __new_begin, false_type);
140        _LIBCPP_INLINE_VISIBILITY
141        void __destruct_at_begin(pointer __new_begin, true_type);
142
143    _LIBCPP_INLINE_VISIBILITY
144    void __destruct_at_end(pointer __new_last) _NOEXCEPT
145        {__destruct_at_end(__new_last, false_type());}
146    _LIBCPP_INLINE_VISIBILITY
147        void __destruct_at_end(pointer __new_last, false_type) _NOEXCEPT;
148    _LIBCPP_INLINE_VISIBILITY
149        void __destruct_at_end(pointer __new_last, true_type) _NOEXCEPT;
150
151    void swap(__split_buffer& __x)
152        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
153                   __is_nothrow_swappable<__alloc_rr>::value);
154
155    bool __invariants() const;
156
157private:
158    _LIBCPP_INLINE_VISIBILITY
159    void __move_assign_alloc(__split_buffer& __c, true_type)
160        _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
161        {
162            __alloc() = _VSTD::move(__c.__alloc());
163        }
164
165    _LIBCPP_INLINE_VISIBILITY
166    void __move_assign_alloc(__split_buffer&, false_type) _NOEXCEPT
167        {}
168
169    struct _ConstructTransaction {
170      explicit _ConstructTransaction(pointer* __p, size_type __n) _NOEXCEPT
171      : __pos_(*__p), __end_(*__p + __n), __dest_(__p) {
172      }
173      ~_ConstructTransaction() {
174        *__dest_ = __pos_;
175      }
176      pointer __pos_;
177     const pointer __end_;
178    private:
179     pointer *__dest_;
180    };
181};
182
183template <class _Tp, class _Allocator>
184bool
185__split_buffer<_Tp, _Allocator>::__invariants() const
186{
187    if (__first_ == nullptr)
188    {
189        if (__begin_ != nullptr)
190            return false;
191        if (__end_ != nullptr)
192            return false;
193        if (__end_cap() != nullptr)
194            return false;
195    }
196    else
197    {
198        if (__begin_ < __first_)
199            return false;
200        if (__end_ < __begin_)
201            return false;
202        if (__end_cap() < __end_)
203            return false;
204    }
205    return true;
206}
207
208//  Default constructs __n objects starting at __end_
209//  throws if construction throws
210//  Precondition:  __n > 0
211//  Precondition:  size() + __n <= capacity()
212//  Postcondition:  size() == size() + __n
213template <class _Tp, class _Allocator>
214void
215__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n)
216{
217    _ConstructTransaction __tx(&this->__end_, __n);
218    for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
219        __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__tx.__pos_));
220    }
221}
222
223//  Copy constructs __n objects starting at __end_ from __x
224//  throws if construction throws
225//  Precondition:  __n > 0
226//  Precondition:  size() + __n <= capacity()
227//  Postcondition:  size() == old size() + __n
228//  Postcondition:  [i] == __x for all i in [size() - __n, __n)
229template <class _Tp, class _Allocator>
230void
231__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
232{
233    _ConstructTransaction __tx(&this->__end_, __n);
234    for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
235        __alloc_traits::construct(this->__alloc(),
236            _VSTD::__to_address(__tx.__pos_), __x);
237    }
238}
239
240template <class _Tp, class _Allocator>
241template <class _InputIter>
242typename enable_if
243<
244     __is_cpp17_input_iterator<_InputIter>::value &&
245    !__is_cpp17_forward_iterator<_InputIter>::value,
246    void
247>::type
248__split_buffer<_Tp, _Allocator>::__construct_at_end(_InputIter __first, _InputIter __last)
249{
250    __alloc_rr& __a = this->__alloc();
251    for (; __first != __last; ++__first)
252    {
253        if (__end_ == __end_cap())
254        {
255            size_type __old_cap = __end_cap() - __first_;
256            size_type __new_cap = _VSTD::max<size_type>(2 * __old_cap, 8);
257            __split_buffer __buf(__new_cap, 0, __a);
258            for (pointer __p = __begin_; __p != __end_; ++__p, (void) ++__buf.__end_)
259                __alloc_traits::construct(__buf.__alloc(),
260                        _VSTD::__to_address(__buf.__end_), _VSTD::move(*__p));
261            swap(__buf);
262        }
263        __alloc_traits::construct(__a, _VSTD::__to_address(this->__end_), *__first);
264        ++this->__end_;
265    }
266}
267
268template <class _Tp, class _Allocator>
269template <class _ForwardIterator>
270typename enable_if
271<
272    __is_cpp17_forward_iterator<_ForwardIterator>::value,
273    void
274>::type
275__split_buffer<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
276{
277    _ConstructTransaction __tx(&this->__end_, _VSTD::distance(__first, __last));
278    for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void) ++__first) {
279        __alloc_traits::construct(this->__alloc(),
280            _VSTD::__to_address(__tx.__pos_), *__first);
281    }
282}
283
284template <class _Tp, class _Allocator>
285inline
286void
287__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type)
288{
289    while (__begin_ != __new_begin)
290        __alloc_traits::destroy(__alloc(), _VSTD::__to_address(__begin_++));
291}
292
293template <class _Tp, class _Allocator>
294inline
295void
296__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, true_type)
297{
298    __begin_ = __new_begin;
299}
300
301template <class _Tp, class _Allocator>
302inline _LIBCPP_INLINE_VISIBILITY
303void
304__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT
305{
306    while (__new_last != __end_)
307        __alloc_traits::destroy(__alloc(), _VSTD::__to_address(--__end_));
308}
309
310template <class _Tp, class _Allocator>
311inline _LIBCPP_INLINE_VISIBILITY
312void
313__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, true_type) _NOEXCEPT
314{
315    __end_ = __new_last;
316}
317
318template <class _Tp, class _Allocator>
319__split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __start, __alloc_rr& __a)
320    : __end_cap_(nullptr, __a)
321{
322    __first_ = __cap != 0 ? __alloc_traits::allocate(__alloc(), __cap) : nullptr;
323    __begin_ = __end_ = __first_ + __start;
324    __end_cap() = __first_ + __cap;
325}
326
327template <class _Tp, class _Allocator>
328inline
329__split_buffer<_Tp, _Allocator>::__split_buffer()
330    _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
331    : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __default_init_tag())
332{
333}
334
335template <class _Tp, class _Allocator>
336inline
337__split_buffer<_Tp, _Allocator>::__split_buffer(__alloc_rr& __a)
338    : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a)
339{
340}
341
342template <class _Tp, class _Allocator>
343inline
344__split_buffer<_Tp, _Allocator>::__split_buffer(const __alloc_rr& __a)
345    : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a)
346{
347}
348
349template <class _Tp, class _Allocator>
350__split_buffer<_Tp, _Allocator>::~__split_buffer()
351{
352    clear();
353    if (__first_)
354        __alloc_traits::deallocate(__alloc(), __first_, capacity());
355}
356
357template <class _Tp, class _Allocator>
358__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c)
359    _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
360    : __first_(_VSTD::move(__c.__first_)),
361      __begin_(_VSTD::move(__c.__begin_)),
362      __end_(_VSTD::move(__c.__end_)),
363      __end_cap_(_VSTD::move(__c.__end_cap_))
364{
365    __c.__first_ = nullptr;
366    __c.__begin_ = nullptr;
367    __c.__end_ = nullptr;
368    __c.__end_cap() = nullptr;
369}
370
371template <class _Tp, class _Allocator>
372__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a)
373    : __end_cap_(nullptr, __a)
374{
375    if (__a == __c.__alloc())
376    {
377        __first_ = __c.__first_;
378        __begin_ = __c.__begin_;
379        __end_ = __c.__end_;
380        __end_cap() = __c.__end_cap();
381        __c.__first_ = nullptr;
382        __c.__begin_ = nullptr;
383        __c.__end_ = nullptr;
384        __c.__end_cap() = nullptr;
385    }
386    else
387    {
388        size_type __cap = __c.size();
389        __first_ = __alloc_traits::allocate(__alloc(), __cap);
390        __begin_ = __end_ = __first_;
391        __end_cap() = __first_ + __cap;
392        typedef move_iterator<iterator> _Ip;
393        __construct_at_end(_Ip(__c.begin()), _Ip(__c.end()));
394    }
395}
396
397template <class _Tp, class _Allocator>
398__split_buffer<_Tp, _Allocator>&
399__split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c)
400    _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
401                is_nothrow_move_assignable<allocator_type>::value) ||
402               !__alloc_traits::propagate_on_container_move_assignment::value)
403{
404    clear();
405    shrink_to_fit();
406    __first_ = __c.__first_;
407    __begin_ = __c.__begin_;
408    __end_ = __c.__end_;
409    __end_cap() = __c.__end_cap();
410    __move_assign_alloc(__c,
411        integral_constant<bool,
412                          __alloc_traits::propagate_on_container_move_assignment::value>());
413    __c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
414    return *this;
415}
416
417template <class _Tp, class _Allocator>
418void
419__split_buffer<_Tp, _Allocator>::swap(__split_buffer& __x)
420        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
421                   __is_nothrow_swappable<__alloc_rr>::value)
422{
423    _VSTD::swap(__first_, __x.__first_);
424    _VSTD::swap(__begin_, __x.__begin_);
425    _VSTD::swap(__end_, __x.__end_);
426    _VSTD::swap(__end_cap(), __x.__end_cap());
427    _VSTD::__swap_allocator(__alloc(), __x.__alloc());
428}
429
430template <class _Tp, class _Allocator>
431void
432__split_buffer<_Tp, _Allocator>::reserve(size_type __n)
433{
434    if (__n < capacity())
435    {
436        __split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc());
437        __t.__construct_at_end(move_iterator<pointer>(__begin_),
438                               move_iterator<pointer>(__end_));
439        _VSTD::swap(__first_, __t.__first_);
440        _VSTD::swap(__begin_, __t.__begin_);
441        _VSTD::swap(__end_, __t.__end_);
442        _VSTD::swap(__end_cap(), __t.__end_cap());
443    }
444}
445
446template <class _Tp, class _Allocator>
447void
448__split_buffer<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
449{
450    if (capacity() > size())
451    {
452#ifndef _LIBCPP_NO_EXCEPTIONS
453        try
454        {
455#endif // _LIBCPP_NO_EXCEPTIONS
456            __split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc());
457            __t.__construct_at_end(move_iterator<pointer>(__begin_),
458                                   move_iterator<pointer>(__end_));
459            __t.__end_ = __t.__begin_ + (__end_ - __begin_);
460            _VSTD::swap(__first_, __t.__first_);
461            _VSTD::swap(__begin_, __t.__begin_);
462            _VSTD::swap(__end_, __t.__end_);
463            _VSTD::swap(__end_cap(), __t.__end_cap());
464#ifndef _LIBCPP_NO_EXCEPTIONS
465        }
466        catch (...)
467        {
468        }
469#endif // _LIBCPP_NO_EXCEPTIONS
470    }
471}
472
473template <class _Tp, class _Allocator>
474void
475__split_buffer<_Tp, _Allocator>::push_front(const_reference __x)
476{
477    if (__begin_ == __first_)
478    {
479        if (__end_ < __end_cap())
480        {
481            difference_type __d = __end_cap() - __end_;
482            __d = (__d + 1) / 2;
483            __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d);
484            __end_ += __d;
485        }
486        else
487        {
488            size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
489            __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
490            __t.__construct_at_end(move_iterator<pointer>(__begin_),
491                                   move_iterator<pointer>(__end_));
492            _VSTD::swap(__first_, __t.__first_);
493            _VSTD::swap(__begin_, __t.__begin_);
494            _VSTD::swap(__end_, __t.__end_);
495            _VSTD::swap(__end_cap(), __t.__end_cap());
496        }
497    }
498    __alloc_traits::construct(__alloc(), _VSTD::__to_address(__begin_-1), __x);
499    --__begin_;
500}
501
502template <class _Tp, class _Allocator>
503void
504__split_buffer<_Tp, _Allocator>::push_front(value_type&& __x)
505{
506    if (__begin_ == __first_)
507    {
508        if (__end_ < __end_cap())
509        {
510            difference_type __d = __end_cap() - __end_;
511            __d = (__d + 1) / 2;
512            __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d);
513            __end_ += __d;
514        }
515        else
516        {
517            size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
518            __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
519            __t.__construct_at_end(move_iterator<pointer>(__begin_),
520                                   move_iterator<pointer>(__end_));
521            _VSTD::swap(__first_, __t.__first_);
522            _VSTD::swap(__begin_, __t.__begin_);
523            _VSTD::swap(__end_, __t.__end_);
524            _VSTD::swap(__end_cap(), __t.__end_cap());
525        }
526    }
527    __alloc_traits::construct(__alloc(), _VSTD::__to_address(__begin_-1),
528            _VSTD::move(__x));
529    --__begin_;
530}
531
532template <class _Tp, class _Allocator>
533inline _LIBCPP_INLINE_VISIBILITY
534void
535__split_buffer<_Tp, _Allocator>::push_back(const_reference __x)
536{
537    if (__end_ == __end_cap())
538    {
539        if (__begin_ > __first_)
540        {
541            difference_type __d = __begin_ - __first_;
542            __d = (__d + 1) / 2;
543            __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
544            __begin_ -= __d;
545        }
546        else
547        {
548            size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
549            __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
550            __t.__construct_at_end(move_iterator<pointer>(__begin_),
551                                   move_iterator<pointer>(__end_));
552            _VSTD::swap(__first_, __t.__first_);
553            _VSTD::swap(__begin_, __t.__begin_);
554            _VSTD::swap(__end_, __t.__end_);
555            _VSTD::swap(__end_cap(), __t.__end_cap());
556        }
557    }
558    __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_), __x);
559    ++__end_;
560}
561
562template <class _Tp, class _Allocator>
563void
564__split_buffer<_Tp, _Allocator>::push_back(value_type&& __x)
565{
566    if (__end_ == __end_cap())
567    {
568        if (__begin_ > __first_)
569        {
570            difference_type __d = __begin_ - __first_;
571            __d = (__d + 1) / 2;
572            __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
573            __begin_ -= __d;
574        }
575        else
576        {
577            size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
578            __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
579            __t.__construct_at_end(move_iterator<pointer>(__begin_),
580                                   move_iterator<pointer>(__end_));
581            _VSTD::swap(__first_, __t.__first_);
582            _VSTD::swap(__begin_, __t.__begin_);
583            _VSTD::swap(__end_, __t.__end_);
584            _VSTD::swap(__end_cap(), __t.__end_cap());
585        }
586    }
587    __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_),
588            _VSTD::move(__x));
589    ++__end_;
590}
591
592template <class _Tp, class _Allocator>
593template <class... _Args>
594void
595__split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args)
596{
597    if (__end_ == __end_cap())
598    {
599        if (__begin_ > __first_)
600        {
601            difference_type __d = __begin_ - __first_;
602            __d = (__d + 1) / 2;
603            __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
604            __begin_ -= __d;
605        }
606        else
607        {
608            size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
609            __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
610            __t.__construct_at_end(move_iterator<pointer>(__begin_),
611                                   move_iterator<pointer>(__end_));
612            _VSTD::swap(__first_, __t.__first_);
613            _VSTD::swap(__begin_, __t.__begin_);
614            _VSTD::swap(__end_, __t.__end_);
615            _VSTD::swap(__end_cap(), __t.__end_cap());
616        }
617    }
618    __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_),
619                              _VSTD::forward<_Args>(__args)...);
620    ++__end_;
621}
622
623template <class _Tp, class _Allocator>
624inline _LIBCPP_INLINE_VISIBILITY
625void
626swap(__split_buffer<_Tp, _Allocator>& __x, __split_buffer<_Tp, _Allocator>& __y)
627        _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
628{
629    __x.swap(__y);
630}
631
632_LIBCPP_END_NAMESPACE_STD
633
634_LIBCPP_POP_MACROS
635
636#endif // _LIBCPP___SPLIT_BUFFER
637