Skip to content

Commit 9a1a8f3

Browse files
authored
[libc++][NFC] Simplify __split_buffer a bit (#114224)
This is possible since we're not using `__compressed_pair` anymore.
1 parent 07b6013 commit 9a1a8f3

File tree

3 files changed

+67
-73
lines changed

3 files changed

+67
-73
lines changed

libcxx/include/__split_buffer

Lines changed: 54 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,6 @@ public:
108108

109109
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~__split_buffer();
110110

111-
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __alloc_rr& __alloc() _NOEXCEPT { return __alloc_; }
112-
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const __alloc_rr& __alloc() const _NOEXCEPT { return __alloc_; }
113-
114-
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer& __end_cap() _NOEXCEPT { return __end_cap_; }
115-
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const pointer& __end_cap() const _NOEXCEPT { return __end_cap_; }
116-
117111
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return __begin_; }
118112
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return __begin_; }
119113

@@ -129,15 +123,15 @@ public:
129123
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool empty() const { return __end_ == __begin_; }
130124

131125
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type capacity() const {
132-
return static_cast<size_type>(__end_cap() - __first_);
126+
return static_cast<size_type>(__end_cap_ - __first_);
133127
}
134128

135129
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __front_spare() const {
136130
return static_cast<size_type>(__begin_ - __first_);
137131
}
138132

139133
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __back_spare() const {
140-
return static_cast<size_type>(__end_cap() - __end_);
134+
return static_cast<size_type>(__end_cap_ - __end_);
141135
}
142136

143137
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference front() { return *__begin_; }
@@ -196,7 +190,7 @@ public:
196190
private:
197191
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__split_buffer& __c, true_type)
198192
_NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
199-
__alloc() = std::move(__c.__alloc());
193+
__alloc_ = std::move(__c.__alloc_);
200194
}
201195

202196
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__split_buffer&, false_type) _NOEXCEPT {}
@@ -225,14 +219,14 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __split_buffer<_Tp, _Allocator>::__invariants
225219
return false;
226220
if (__end_ != nullptr)
227221
return false;
228-
if (__end_cap() != nullptr)
222+
if (__end_cap_ != nullptr)
229223
return false;
230224
} else {
231225
if (__begin_ < __first_)
232226
return false;
233227
if (__end_ < __begin_)
234228
return false;
235-
if (__end_cap() < __end_)
229+
if (__end_cap_ < __end_)
236230
return false;
237231
}
238232
return true;
@@ -247,7 +241,7 @@ template <class _Tp, class _Allocator>
247241
_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n) {
248242
_ConstructTransaction __tx(&this->__end_, __n);
249243
for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
250-
__alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_));
244+
__alloc_traits::construct(__alloc_, std::__to_address(__tx.__pos_));
251245
}
252246
}
253247

@@ -262,7 +256,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void
262256
__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x) {
263257
_ConstructTransaction __tx(&this->__end_, __n);
264258
for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
265-
__alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_), __x);
259+
__alloc_traits::construct(__alloc_, std::__to_address(__tx.__pos_), __x);
266260
}
267261
}
268262

@@ -277,14 +271,14 @@ template <class _Tp, class _Allocator>
277271
template <class _Iterator, class _Sentinel>
278272
_LIBCPP_CONSTEXPR_SINCE_CXX20 void
279273
__split_buffer<_Tp, _Allocator>::__construct_at_end_with_sentinel(_Iterator __first, _Sentinel __last) {
280-
__alloc_rr& __a = this->__alloc();
274+
__alloc_rr& __a = __alloc_;
281275
for (; __first != __last; ++__first) {
282-
if (__end_ == __end_cap()) {
283-
size_type __old_cap = __end_cap() - __first_;
276+
if (__end_ == __end_cap_) {
277+
size_type __old_cap = __end_cap_ - __first_;
284278
size_type __new_cap = std::max<size_type>(2 * __old_cap, 8);
285279
__split_buffer __buf(__new_cap, 0, __a);
286280
for (pointer __p = __begin_; __p != __end_; ++__p, (void)++__buf.__end_)
287-
__alloc_traits::construct(__buf.__alloc(), std::__to_address(__buf.__end_), std::move(*__p));
281+
__alloc_traits::construct(__buf.__alloc_, std::__to_address(__buf.__end_), std::move(*__p));
288282
swap(__buf);
289283
}
290284
__alloc_traits::construct(__a, std::__to_address(this->__end_), *__first);
@@ -304,15 +298,15 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void
304298
__split_buffer<_Tp, _Allocator>::__construct_at_end_with_size(_ForwardIterator __first, size_type __n) {
305299
_ConstructTransaction __tx(&this->__end_, __n);
306300
for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void)++__first) {
307-
__alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_), *__first);
301+
__alloc_traits::construct(__alloc_, std::__to_address(__tx.__pos_), *__first);
308302
}
309303
}
310304

311305
template <class _Tp, class _Allocator>
312306
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
313307
__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type) {
314308
while (__begin_ != __new_begin)
315-
__alloc_traits::destroy(__alloc(), std::__to_address(__begin_++));
309+
__alloc_traits::destroy(__alloc_, std::__to_address(__begin_++));
316310
}
317311

318312
template <class _Tp, class _Allocator>
@@ -325,7 +319,7 @@ template <class _Tp, class _Allocator>
325319
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
326320
__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT {
327321
while (__new_last != __end_)
328-
__alloc_traits::destroy(__alloc(), std::__to_address(--__end_));
322+
__alloc_traits::destroy(__alloc_, std::__to_address(--__end_));
329323
}
330324

331325
template <class _Tp, class _Allocator>
@@ -341,19 +335,19 @@ __split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __sta
341335
if (__cap == 0) {
342336
__first_ = nullptr;
343337
} else {
344-
auto __allocation = std::__allocate_at_least(__alloc(), __cap);
338+
auto __allocation = std::__allocate_at_least(__alloc_, __cap);
345339
__first_ = __allocation.ptr;
346340
__cap = __allocation.count;
347341
}
348342
__begin_ = __end_ = __first_ + __start;
349-
__end_cap() = __first_ + __cap;
343+
__end_cap_ = __first_ + __cap;
350344
}
351345

352346
template <class _Tp, class _Allocator>
353347
_LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator>::~__split_buffer() {
354348
clear();
355349
if (__first_)
356-
__alloc_traits::deallocate(__alloc(), __first_, capacity());
350+
__alloc_traits::deallocate(__alloc_, __first_, capacity());
357351
}
358352

359353
template <class _Tp, class _Allocator>
@@ -364,30 +358,30 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator>::__split_buffer(__
364358
__end_(std::move(__c.__end_)),
365359
__end_cap_(std::move(__c.__end_cap_)),
366360
__alloc_(std::move(__c.__alloc_)) {
367-
__c.__first_ = nullptr;
368-
__c.__begin_ = nullptr;
369-
__c.__end_ = nullptr;
370-
__c.__end_cap() = nullptr;
361+
__c.__first_ = nullptr;
362+
__c.__begin_ = nullptr;
363+
__c.__end_ = nullptr;
364+
__c.__end_cap_ = nullptr;
371365
}
372366

373367
template <class _Tp, class _Allocator>
374368
_LIBCPP_CONSTEXPR_SINCE_CXX20
375369
__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a)
376370
: __end_cap_(nullptr), __alloc_(__a) {
377-
if (__a == __c.__alloc()) {
378-
__first_ = __c.__first_;
379-
__begin_ = __c.__begin_;
380-
__end_ = __c.__end_;
381-
__end_cap() = __c.__end_cap();
382-
__c.__first_ = nullptr;
383-
__c.__begin_ = nullptr;
384-
__c.__end_ = nullptr;
385-
__c.__end_cap() = nullptr;
371+
if (__a == __c.__alloc_) {
372+
__first_ = __c.__first_;
373+
__begin_ = __c.__begin_;
374+
__end_ = __c.__end_;
375+
__end_cap_ = __c.__end_cap_;
376+
__c.__first_ = nullptr;
377+
__c.__begin_ = nullptr;
378+
__c.__end_ = nullptr;
379+
__c.__end_cap_ = nullptr;
386380
} else {
387-
auto __allocation = std::__allocate_at_least(__alloc(), __c.size());
381+
auto __allocation = std::__allocate_at_least(__alloc_, __c.size());
388382
__first_ = __allocation.ptr;
389383
__begin_ = __end_ = __first_;
390-
__end_cap() = __first_ + __allocation.count;
384+
__end_cap_ = __first_ + __allocation.count;
391385
typedef move_iterator<iterator> _Ip;
392386
__construct_at_end(_Ip(__c.begin()), _Ip(__c.end()));
393387
}
@@ -401,12 +395,12 @@ __split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c)
401395
!__alloc_traits::propagate_on_container_move_assignment::value) {
402396
clear();
403397
shrink_to_fit();
404-
__first_ = __c.__first_;
405-
__begin_ = __c.__begin_;
406-
__end_ = __c.__end_;
407-
__end_cap() = __c.__end_cap();
398+
__first_ = __c.__first_;
399+
__begin_ = __c.__begin_;
400+
__end_ = __c.__end_;
401+
__end_cap_ = __c.__end_cap_;
408402
__move_assign_alloc(__c, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
409-
__c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
403+
__c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap_ = nullptr;
410404
return *this;
411405
}
412406

@@ -416,19 +410,19 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::swap(__split
416410
std::swap(__first_, __x.__first_);
417411
std::swap(__begin_, __x.__begin_);
418412
std::swap(__end_, __x.__end_);
419-
std::swap(__end_cap(), __x.__end_cap());
420-
std::__swap_allocator(__alloc(), __x.__alloc());
413+
std::swap(__end_cap_, __x.__end_cap_);
414+
std::__swap_allocator(__alloc_, __x.__alloc_);
421415
}
422416

423417
template <class _Tp, class _Allocator>
424418
_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::reserve(size_type __n) {
425419
if (__n < capacity()) {
426-
__split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc());
420+
__split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc_);
427421
__t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
428422
std::swap(__first_, __t.__first_);
429423
std::swap(__begin_, __t.__begin_);
430424
std::swap(__end_, __t.__end_);
431-
std::swap(__end_cap(), __t.__end_cap());
425+
std::swap(__end_cap_, __t.__end_cap_);
432426
}
433427
}
434428

@@ -438,13 +432,13 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::shrink_to_fi
438432
#if _LIBCPP_HAS_EXCEPTIONS
439433
try {
440434
#endif // _LIBCPP_HAS_EXCEPTIONS
441-
__split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc());
435+
__split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc_);
442436
__t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
443437
__t.__end_ = __t.__begin_ + (__end_ - __begin_);
444438
std::swap(__first_, __t.__first_);
445439
std::swap(__begin_, __t.__begin_);
446440
std::swap(__end_, __t.__end_);
447-
std::swap(__end_cap(), __t.__end_cap());
441+
std::swap(__end_cap_, __t.__end_cap_);
448442
#if _LIBCPP_HAS_EXCEPTIONS
449443
} catch (...) {
450444
}
@@ -456,45 +450,45 @@ template <class _Tp, class _Allocator>
456450
template <class... _Args>
457451
_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::emplace_front(_Args&&... __args) {
458452
if (__begin_ == __first_) {
459-
if (__end_ < __end_cap()) {
460-
difference_type __d = __end_cap() - __end_;
453+
if (__end_ < __end_cap_) {
454+
difference_type __d = __end_cap_ - __end_;
461455
__d = (__d + 1) / 2;
462456
__begin_ = std::move_backward(__begin_, __end_, __end_ + __d);
463457
__end_ += __d;
464458
} else {
465-
size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
466-
__split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
459+
size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap_ - __first_), 1);
460+
__split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc_);
467461
__t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
468462
std::swap(__first_, __t.__first_);
469463
std::swap(__begin_, __t.__begin_);
470464
std::swap(__end_, __t.__end_);
471-
std::swap(__end_cap(), __t.__end_cap());
465+
std::swap(__end_cap_, __t.__end_cap_);
472466
}
473467
}
474-
__alloc_traits::construct(__alloc(), std::__to_address(__begin_ - 1), std::forward<_Args>(__args)...);
468+
__alloc_traits::construct(__alloc_, std::__to_address(__begin_ - 1), std::forward<_Args>(__args)...);
475469
--__begin_;
476470
}
477471

478472
template <class _Tp, class _Allocator>
479473
template <class... _Args>
480474
_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args) {
481-
if (__end_ == __end_cap()) {
475+
if (__end_ == __end_cap_) {
482476
if (__begin_ > __first_) {
483477
difference_type __d = __begin_ - __first_;
484478
__d = (__d + 1) / 2;
485479
__end_ = std::move(__begin_, __end_, __begin_ - __d);
486480
__begin_ -= __d;
487481
} else {
488-
size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
489-
__split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
482+
size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap_ - __first_), 1);
483+
__split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc_);
490484
__t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
491485
std::swap(__first_, __t.__first_);
492486
std::swap(__begin_, __t.__begin_);
493487
std::swap(__end_, __t.__end_);
494-
std::swap(__end_cap(), __t.__end_cap());
488+
std::swap(__end_cap_, __t.__end_cap_);
495489
}
496490
}
497-
__alloc_traits::construct(__alloc(), std::__to_address(__end_), std::forward<_Args>(__args)...);
491+
__alloc_traits::construct(__alloc_, std::__to_address(__end_), std::forward<_Args>(__args)...);
498492
++__end_;
499493
}
500494

libcxx/include/__vector/vector.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, a
822822
__end_ = __begin_; // All the objects have been destroyed by relocating them.
823823
std::swap(this->__begin_, __v.__begin_);
824824
std::swap(this->__end_, __v.__end_);
825-
std::swap(this->__end_cap(), __v.__end_cap());
825+
std::swap(this->__end_cap(), __v.__end_cap_);
826826
__v.__first_ = __v.__begin_;
827827
__annotate_new(size());
828828
}
@@ -852,7 +852,7 @@ vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, a
852852

853853
std::swap(this->__begin_, __v.__begin_);
854854
std::swap(this->__end_, __v.__end_);
855-
std::swap(this->__end_cap(), __v.__end_cap());
855+
std::swap(this->__end_cap(), __v.__end_cap_);
856856
__v.__first_ = __v.__begin_;
857857
__annotate_new(size());
858858
return __ret;

0 commit comments

Comments
 (0)