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