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