xref: /freebsd-src/contrib/llvm-project/libcxx/include/__split_buffer (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
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    if (__cap == 0) {
323        __first_ = nullptr;
324    } else {
325        auto __allocation = std::__allocate_at_least(__alloc(), __cap);
326        __first_ = __allocation.ptr;
327        __cap = __allocation.count;
328    }
329    __begin_ = __end_ = __first_ + __start;
330    __end_cap() = __first_ + __cap;
331}
332
333template <class _Tp, class _Allocator>
334inline
335__split_buffer<_Tp, _Allocator>::__split_buffer()
336    _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
337    : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __default_init_tag())
338{
339}
340
341template <class _Tp, class _Allocator>
342inline
343__split_buffer<_Tp, _Allocator>::__split_buffer(__alloc_rr& __a)
344    : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a)
345{
346}
347
348template <class _Tp, class _Allocator>
349inline
350__split_buffer<_Tp, _Allocator>::__split_buffer(const __alloc_rr& __a)
351    : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a)
352{
353}
354
355template <class _Tp, class _Allocator>
356__split_buffer<_Tp, _Allocator>::~__split_buffer()
357{
358    clear();
359    if (__first_)
360        __alloc_traits::deallocate(__alloc(), __first_, capacity());
361}
362
363template <class _Tp, class _Allocator>
364__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c)
365    _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
366    : __first_(_VSTD::move(__c.__first_)),
367      __begin_(_VSTD::move(__c.__begin_)),
368      __end_(_VSTD::move(__c.__end_)),
369      __end_cap_(_VSTD::move(__c.__end_cap_))
370{
371    __c.__first_ = nullptr;
372    __c.__begin_ = nullptr;
373    __c.__end_ = nullptr;
374    __c.__end_cap() = nullptr;
375}
376
377template <class _Tp, class _Allocator>
378__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a)
379    : __end_cap_(nullptr, __a)
380{
381    if (__a == __c.__alloc())
382    {
383        __first_ = __c.__first_;
384        __begin_ = __c.__begin_;
385        __end_ = __c.__end_;
386        __end_cap() = __c.__end_cap();
387        __c.__first_ = nullptr;
388        __c.__begin_ = nullptr;
389        __c.__end_ = nullptr;
390        __c.__end_cap() = nullptr;
391    }
392    else
393    {
394        auto __allocation = std::__allocate_at_least(__alloc(), __c.size());
395        __first_ = __allocation.ptr;
396        __begin_ = __end_ = __first_;
397        __end_cap() = __first_ + __allocation.count;
398        typedef move_iterator<iterator> _Ip;
399        __construct_at_end(_Ip(__c.begin()), _Ip(__c.end()));
400    }
401}
402
403template <class _Tp, class _Allocator>
404__split_buffer<_Tp, _Allocator>&
405__split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c)
406    _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
407                is_nothrow_move_assignable<allocator_type>::value) ||
408               !__alloc_traits::propagate_on_container_move_assignment::value)
409{
410    clear();
411    shrink_to_fit();
412    __first_ = __c.__first_;
413    __begin_ = __c.__begin_;
414    __end_ = __c.__end_;
415    __end_cap() = __c.__end_cap();
416    __move_assign_alloc(__c,
417        integral_constant<bool,
418                          __alloc_traits::propagate_on_container_move_assignment::value>());
419    __c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
420    return *this;
421}
422
423template <class _Tp, class _Allocator>
424void
425__split_buffer<_Tp, _Allocator>::swap(__split_buffer& __x)
426        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
427                   __is_nothrow_swappable<__alloc_rr>::value)
428{
429    _VSTD::swap(__first_, __x.__first_);
430    _VSTD::swap(__begin_, __x.__begin_);
431    _VSTD::swap(__end_, __x.__end_);
432    _VSTD::swap(__end_cap(), __x.__end_cap());
433    _VSTD::__swap_allocator(__alloc(), __x.__alloc());
434}
435
436template <class _Tp, class _Allocator>
437void
438__split_buffer<_Tp, _Allocator>::reserve(size_type __n)
439{
440    if (__n < capacity())
441    {
442        __split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc());
443        __t.__construct_at_end(move_iterator<pointer>(__begin_),
444                               move_iterator<pointer>(__end_));
445        _VSTD::swap(__first_, __t.__first_);
446        _VSTD::swap(__begin_, __t.__begin_);
447        _VSTD::swap(__end_, __t.__end_);
448        _VSTD::swap(__end_cap(), __t.__end_cap());
449    }
450}
451
452template <class _Tp, class _Allocator>
453void
454__split_buffer<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
455{
456    if (capacity() > size())
457    {
458#ifndef _LIBCPP_NO_EXCEPTIONS
459        try
460        {
461#endif // _LIBCPP_NO_EXCEPTIONS
462            __split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc());
463            __t.__construct_at_end(move_iterator<pointer>(__begin_),
464                                   move_iterator<pointer>(__end_));
465            __t.__end_ = __t.__begin_ + (__end_ - __begin_);
466            _VSTD::swap(__first_, __t.__first_);
467            _VSTD::swap(__begin_, __t.__begin_);
468            _VSTD::swap(__end_, __t.__end_);
469            _VSTD::swap(__end_cap(), __t.__end_cap());
470#ifndef _LIBCPP_NO_EXCEPTIONS
471        }
472        catch (...)
473        {
474        }
475#endif // _LIBCPP_NO_EXCEPTIONS
476    }
477}
478
479template <class _Tp, class _Allocator>
480void
481__split_buffer<_Tp, _Allocator>::push_front(const_reference __x)
482{
483    if (__begin_ == __first_)
484    {
485        if (__end_ < __end_cap())
486        {
487            difference_type __d = __end_cap() - __end_;
488            __d = (__d + 1) / 2;
489            __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d);
490            __end_ += __d;
491        }
492        else
493        {
494            size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
495            __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
496            __t.__construct_at_end(move_iterator<pointer>(__begin_),
497                                   move_iterator<pointer>(__end_));
498            _VSTD::swap(__first_, __t.__first_);
499            _VSTD::swap(__begin_, __t.__begin_);
500            _VSTD::swap(__end_, __t.__end_);
501            _VSTD::swap(__end_cap(), __t.__end_cap());
502        }
503    }
504    __alloc_traits::construct(__alloc(), _VSTD::__to_address(__begin_-1), __x);
505    --__begin_;
506}
507
508template <class _Tp, class _Allocator>
509void
510__split_buffer<_Tp, _Allocator>::push_front(value_type&& __x)
511{
512    if (__begin_ == __first_)
513    {
514        if (__end_ < __end_cap())
515        {
516            difference_type __d = __end_cap() - __end_;
517            __d = (__d + 1) / 2;
518            __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d);
519            __end_ += __d;
520        }
521        else
522        {
523            size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
524            __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
525            __t.__construct_at_end(move_iterator<pointer>(__begin_),
526                                   move_iterator<pointer>(__end_));
527            _VSTD::swap(__first_, __t.__first_);
528            _VSTD::swap(__begin_, __t.__begin_);
529            _VSTD::swap(__end_, __t.__end_);
530            _VSTD::swap(__end_cap(), __t.__end_cap());
531        }
532    }
533    __alloc_traits::construct(__alloc(), _VSTD::__to_address(__begin_-1),
534            _VSTD::move(__x));
535    --__begin_;
536}
537
538template <class _Tp, class _Allocator>
539inline _LIBCPP_INLINE_VISIBILITY
540void
541__split_buffer<_Tp, _Allocator>::push_back(const_reference __x)
542{
543    if (__end_ == __end_cap())
544    {
545        if (__begin_ > __first_)
546        {
547            difference_type __d = __begin_ - __first_;
548            __d = (__d + 1) / 2;
549            __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
550            __begin_ -= __d;
551        }
552        else
553        {
554            size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
555            __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
556            __t.__construct_at_end(move_iterator<pointer>(__begin_),
557                                   move_iterator<pointer>(__end_));
558            _VSTD::swap(__first_, __t.__first_);
559            _VSTD::swap(__begin_, __t.__begin_);
560            _VSTD::swap(__end_, __t.__end_);
561            _VSTD::swap(__end_cap(), __t.__end_cap());
562        }
563    }
564    __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_), __x);
565    ++__end_;
566}
567
568template <class _Tp, class _Allocator>
569void
570__split_buffer<_Tp, _Allocator>::push_back(value_type&& __x)
571{
572    if (__end_ == __end_cap())
573    {
574        if (__begin_ > __first_)
575        {
576            difference_type __d = __begin_ - __first_;
577            __d = (__d + 1) / 2;
578            __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
579            __begin_ -= __d;
580        }
581        else
582        {
583            size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
584            __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
585            __t.__construct_at_end(move_iterator<pointer>(__begin_),
586                                   move_iterator<pointer>(__end_));
587            _VSTD::swap(__first_, __t.__first_);
588            _VSTD::swap(__begin_, __t.__begin_);
589            _VSTD::swap(__end_, __t.__end_);
590            _VSTD::swap(__end_cap(), __t.__end_cap());
591        }
592    }
593    __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_),
594            _VSTD::move(__x));
595    ++__end_;
596}
597
598template <class _Tp, class _Allocator>
599template <class... _Args>
600void
601__split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args)
602{
603    if (__end_ == __end_cap())
604    {
605        if (__begin_ > __first_)
606        {
607            difference_type __d = __begin_ - __first_;
608            __d = (__d + 1) / 2;
609            __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
610            __begin_ -= __d;
611        }
612        else
613        {
614            size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
615            __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
616            __t.__construct_at_end(move_iterator<pointer>(__begin_),
617                                   move_iterator<pointer>(__end_));
618            _VSTD::swap(__first_, __t.__first_);
619            _VSTD::swap(__begin_, __t.__begin_);
620            _VSTD::swap(__end_, __t.__end_);
621            _VSTD::swap(__end_cap(), __t.__end_cap());
622        }
623    }
624    __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_),
625                              _VSTD::forward<_Args>(__args)...);
626    ++__end_;
627}
628
629template <class _Tp, class _Allocator>
630inline _LIBCPP_INLINE_VISIBILITY
631void
632swap(__split_buffer<_Tp, _Allocator>& __x, __split_buffer<_Tp, _Allocator>& __y)
633        _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
634{
635    __x.swap(__y);
636}
637
638_LIBCPP_END_NAMESPACE_STD
639
640_LIBCPP_POP_MACROS
641
642#endif // _LIBCPP___SPLIT_BUFFER
643