Skip to content

Commit 75b53f4

Browse files
committed
[libc++][NFC] Simplify __split_buffer a bit
1 parent 85f3d5c commit 75b53f4

File tree

3 files changed

+82
-88
lines changed

3 files changed

+82
-88
lines changed

libcxx/include/__split_buffer

Lines changed: 70 additions & 76 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_; }
@@ -198,7 +192,7 @@ public:
198192
private:
199193
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__split_buffer& __c, true_type)
200194
_NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
201-
__alloc() = std::move(__c.__alloc());
195+
__alloc_ = std::move(__c.__alloc_);
202196
}
203197

204198
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__split_buffer&, false_type) _NOEXCEPT {}
@@ -227,14 +221,14 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __split_buffer<_Tp, _Allocator>::__invariants
227221
return false;
228222
if (__end_ != nullptr)
229223
return false;
230-
if (__end_cap() != nullptr)
224+
if (__end_cap_ != nullptr)
231225
return false;
232226
} else {
233227
if (__begin_ < __first_)
234228
return false;
235229
if (__end_ < __begin_)
236230
return false;
237-
if (__end_cap() < __end_)
231+
if (__end_cap_ < __end_)
238232
return false;
239233
}
240234
return true;
@@ -249,7 +243,7 @@ template <class _Tp, class _Allocator>
249243
_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n) {
250244
_ConstructTransaction __tx(&this->__end_, __n);
251245
for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
252-
__alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_));
246+
__alloc_traits::construct(__alloc_, std::__to_address(__tx.__pos_));
253247
}
254248
}
255249

@@ -264,7 +258,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void
264258
__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x) {
265259
_ConstructTransaction __tx(&this->__end_, __n);
266260
for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
267-
__alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_), __x);
261+
__alloc_traits::construct(__alloc_, std::__to_address(__tx.__pos_), __x);
268262
}
269263
}
270264

@@ -279,14 +273,14 @@ template <class _Tp, class _Allocator>
279273
template <class _Iterator, class _Sentinel>
280274
_LIBCPP_CONSTEXPR_SINCE_CXX20 void
281275
__split_buffer<_Tp, _Allocator>::__construct_at_end_with_sentinel(_Iterator __first, _Sentinel __last) {
282-
__alloc_rr& __a = this->__alloc();
276+
__alloc_rr& __a = __alloc_;
283277
for (; __first != __last; ++__first) {
284-
if (__end_ == __end_cap()) {
285-
size_type __old_cap = __end_cap() - __first_;
278+
if (__end_ == __end_cap_) {
279+
size_type __old_cap = __end_cap_ - __first_;
286280
size_type __new_cap = std::max<size_type>(2 * __old_cap, 8);
287281
__split_buffer __buf(__new_cap, 0, __a);
288282
for (pointer __p = __begin_; __p != __end_; ++__p, (void)++__buf.__end_)
289-
__alloc_traits::construct(__buf.__alloc(), std::__to_address(__buf.__end_), std::move(*__p));
283+
__alloc_traits::construct(__buf.__alloc_, std::__to_address(__buf.__end_), std::move(*__p));
290284
swap(__buf);
291285
}
292286
__alloc_traits::construct(__a, std::__to_address(this->__end_), *__first);
@@ -306,15 +300,15 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void
306300
__split_buffer<_Tp, _Allocator>::__construct_at_end_with_size(_ForwardIterator __first, size_type __n) {
307301
_ConstructTransaction __tx(&this->__end_, __n);
308302
for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void)++__first) {
309-
__alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_), *__first);
303+
__alloc_traits::construct(__alloc_, std::__to_address(__tx.__pos_), *__first);
310304
}
311305
}
312306

313307
template <class _Tp, class _Allocator>
314308
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
315309
__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type) {
316310
while (__begin_ != __new_begin)
317-
__alloc_traits::destroy(__alloc(), std::__to_address(__begin_++));
311+
__alloc_traits::destroy(__alloc_, std::__to_address(__begin_++));
318312
}
319313

320314
template <class _Tp, class _Allocator>
@@ -327,7 +321,7 @@ template <class _Tp, class _Allocator>
327321
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
328322
__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT {
329323
while (__new_last != __end_)
330-
__alloc_traits::destroy(__alloc(), std::__to_address(--__end_));
324+
__alloc_traits::destroy(__alloc_, std::__to_address(--__end_));
331325
}
332326

333327
template <class _Tp, class _Allocator>
@@ -343,19 +337,19 @@ __split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __sta
343337
if (__cap == 0) {
344338
__first_ = nullptr;
345339
} else {
346-
auto __allocation = std::__allocate_at_least(__alloc(), __cap);
340+
auto __allocation = std::__allocate_at_least(__alloc_, __cap);
347341
__first_ = __allocation.ptr;
348342
__cap = __allocation.count;
349343
}
350344
__begin_ = __end_ = __first_ + __start;
351-
__end_cap() = __first_ + __cap;
345+
__end_cap_ = __first_ + __cap;
352346
}
353347

354348
template <class _Tp, class _Allocator>
355349
_LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator>::~__split_buffer() {
356350
clear();
357351
if (__first_)
358-
__alloc_traits::deallocate(__alloc(), __first_, capacity());
352+
__alloc_traits::deallocate(__alloc_, __first_, capacity());
359353
}
360354

361355
template <class _Tp, class _Allocator>
@@ -366,30 +360,30 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator>::__split_buffer(__
366360
__end_(std::move(__c.__end_)),
367361
__end_cap_(std::move(__c.__end_cap_)),
368362
__alloc_(std::move(__c.__alloc_)) {
369-
__c.__first_ = nullptr;
370-
__c.__begin_ = nullptr;
371-
__c.__end_ = nullptr;
372-
__c.__end_cap() = nullptr;
363+
__c.__first_ = nullptr;
364+
__c.__begin_ = nullptr;
365+
__c.__end_ = nullptr;
366+
__c.__end_cap_ = nullptr;
373367
}
374368

375369
template <class _Tp, class _Allocator>
376370
_LIBCPP_CONSTEXPR_SINCE_CXX20
377371
__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a)
378372
: __end_cap_(nullptr), __alloc_(__a) {
379-
if (__a == __c.__alloc()) {
380-
__first_ = __c.__first_;
381-
__begin_ = __c.__begin_;
382-
__end_ = __c.__end_;
383-
__end_cap() = __c.__end_cap();
384-
__c.__first_ = nullptr;
385-
__c.__begin_ = nullptr;
386-
__c.__end_ = nullptr;
387-
__c.__end_cap() = nullptr;
373+
if (__a == __c.__alloc_) {
374+
__first_ = __c.__first_;
375+
__begin_ = __c.__begin_;
376+
__end_ = __c.__end_;
377+
__end_cap_ = __c.__end_cap_;
378+
__c.__first_ = nullptr;
379+
__c.__begin_ = nullptr;
380+
__c.__end_ = nullptr;
381+
__c.__end_cap_ = nullptr;
388382
} else {
389-
auto __allocation = std::__allocate_at_least(__alloc(), __c.size());
383+
auto __allocation = std::__allocate_at_least(__alloc_, __c.size());
390384
__first_ = __allocation.ptr;
391385
__begin_ = __end_ = __first_;
392-
__end_cap() = __first_ + __allocation.count;
386+
__end_cap_ = __first_ + __allocation.count;
393387
typedef move_iterator<iterator> _Ip;
394388
__construct_at_end(_Ip(__c.begin()), _Ip(__c.end()));
395389
}
@@ -403,12 +397,12 @@ __split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c)
403397
!__alloc_traits::propagate_on_container_move_assignment::value) {
404398
clear();
405399
shrink_to_fit();
406-
__first_ = __c.__first_;
407-
__begin_ = __c.__begin_;
408-
__end_ = __c.__end_;
409-
__end_cap() = __c.__end_cap();
400+
__first_ = __c.__first_;
401+
__begin_ = __c.__begin_;
402+
__end_ = __c.__end_;
403+
__end_cap_ = __c.__end_cap_;
410404
__move_assign_alloc(__c, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
411-
__c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
405+
__c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap_ = nullptr;
412406
return *this;
413407
}
414408

@@ -418,19 +412,19 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::swap(__split
418412
std::swap(__first_, __x.__first_);
419413
std::swap(__begin_, __x.__begin_);
420414
std::swap(__end_, __x.__end_);
421-
std::swap(__end_cap(), __x.__end_cap());
422-
std::__swap_allocator(__alloc(), __x.__alloc());
415+
std::swap(__end_cap_, __x.__end_cap_);
416+
std::__swap_allocator(__alloc_, __x.__alloc_);
423417
}
424418

425419
template <class _Tp, class _Allocator>
426420
_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::reserve(size_type __n) {
427421
if (__n < capacity()) {
428-
__split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc());
422+
__split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc_);
429423
__t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
430424
std::swap(__first_, __t.__first_);
431425
std::swap(__begin_, __t.__begin_);
432426
std::swap(__end_, __t.__end_);
433-
std::swap(__end_cap(), __t.__end_cap());
427+
std::swap(__end_cap_, __t.__end_cap_);
434428
}
435429
}
436430

@@ -440,13 +434,13 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::shrink_to_fi
440434
#if _LIBCPP_HAS_EXCEPTIONS
441435
try {
442436
#endif // _LIBCPP_HAS_EXCEPTIONS
443-
__split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc());
437+
__split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc_);
444438
__t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
445439
__t.__end_ = __t.__begin_ + (__end_ - __begin_);
446440
std::swap(__first_, __t.__first_);
447441
std::swap(__begin_, __t.__begin_);
448442
std::swap(__end_, __t.__end_);
449-
std::swap(__end_cap(), __t.__end_cap());
443+
std::swap(__end_cap_, __t.__end_cap_);
450444
#if _LIBCPP_HAS_EXCEPTIONS
451445
} catch (...) {
452446
}
@@ -457,112 +451,112 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::shrink_to_fi
457451
template <class _Tp, class _Allocator>
458452
_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::push_front(const_reference __x) {
459453
if (__begin_ == __first_) {
460-
if (__end_ < __end_cap()) {
461-
difference_type __d = __end_cap() - __end_;
454+
if (__end_ < __end_cap_) {
455+
difference_type __d = __end_cap_ - __end_;
462456
__d = (__d + 1) / 2;
463457
__begin_ = std::move_backward(__begin_, __end_, __end_ + __d);
464458
__end_ += __d;
465459
} else {
466-
size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
467-
__split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
460+
size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap_ - __first_), 1);
461+
__split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc_);
468462
__t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
469463
std::swap(__first_, __t.__first_);
470464
std::swap(__begin_, __t.__begin_);
471465
std::swap(__end_, __t.__end_);
472-
std::swap(__end_cap(), __t.__end_cap());
466+
std::swap(__end_cap_, __t.__end_cap_);
473467
}
474468
}
475-
__alloc_traits::construct(__alloc(), std::__to_address(__begin_ - 1), __x);
469+
__alloc_traits::construct(__alloc_, std::__to_address(__begin_ - 1), __x);
476470
--__begin_;
477471
}
478472

479473
template <class _Tp, class _Allocator>
480474
_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::push_front(value_type&& __x) {
481475
if (__begin_ == __first_) {
482-
if (__end_ < __end_cap()) {
483-
difference_type __d = __end_cap() - __end_;
476+
if (__end_ < __end_cap_) {
477+
difference_type __d = __end_cap_ - __end_;
484478
__d = (__d + 1) / 2;
485479
__begin_ = std::move_backward(__begin_, __end_, __end_ + __d);
486480
__end_ += __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 + 3) / 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 + 3) / 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(__begin_ - 1), std::move(__x));
491+
__alloc_traits::construct(__alloc_, std::__to_address(__begin_ - 1), std::move(__x));
498492
--__begin_;
499493
}
500494

501495
template <class _Tp, class _Allocator>
502496
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
503497
__split_buffer<_Tp, _Allocator>::push_back(const_reference __x) {
504-
if (__end_ == __end_cap()) {
498+
if (__end_ == __end_cap_) {
505499
if (__begin_ > __first_) {
506500
difference_type __d = __begin_ - __first_;
507501
__d = (__d + 1) / 2;
508502
__end_ = std::move(__begin_, __end_, __begin_ - __d);
509503
__begin_ -= __d;
510504
} else {
511-
size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
512-
__split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
505+
size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap_ - __first_), 1);
506+
__split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc_);
513507
__t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
514508
std::swap(__first_, __t.__first_);
515509
std::swap(__begin_, __t.__begin_);
516510
std::swap(__end_, __t.__end_);
517-
std::swap(__end_cap(), __t.__end_cap());
511+
std::swap(__end_cap_, __t.__end_cap_);
518512
}
519513
}
520-
__alloc_traits::construct(__alloc(), std::__to_address(__end_), __x);
514+
__alloc_traits::construct(__alloc_, std::__to_address(__end_), __x);
521515
++__end_;
522516
}
523517

524518
template <class _Tp, class _Allocator>
525519
_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::push_back(value_type&& __x) {
526-
if (__end_ == __end_cap()) {
520+
if (__end_ == __end_cap_) {
527521
if (__begin_ > __first_) {
528522
difference_type __d = __begin_ - __first_;
529523
__d = (__d + 1) / 2;
530524
__end_ = std::move(__begin_, __end_, __begin_ - __d);
531525
__begin_ -= __d;
532526
} else {
533-
size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
534-
__split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
527+
size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap_ - __first_), 1);
528+
__split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc_);
535529
__t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
536530
std::swap(__first_, __t.__first_);
537531
std::swap(__begin_, __t.__begin_);
538532
std::swap(__end_, __t.__end_);
539-
std::swap(__end_cap(), __t.__end_cap());
533+
std::swap(__end_cap_, __t.__end_cap_);
540534
}
541535
}
542-
__alloc_traits::construct(__alloc(), std::__to_address(__end_), std::move(__x));
536+
__alloc_traits::construct(__alloc_, std::__to_address(__end_), std::move(__x));
543537
++__end_;
544538
}
545539

546540
template <class _Tp, class _Allocator>
547541
template <class... _Args>
548542
_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args) {
549-
if (__end_ == __end_cap()) {
543+
if (__end_ == __end_cap_) {
550544
if (__begin_ > __first_) {
551545
difference_type __d = __begin_ - __first_;
552546
__d = (__d + 1) / 2;
553547
__end_ = std::move(__begin_, __end_, __begin_ - __d);
554548
__begin_ -= __d;
555549
} else {
556-
size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
557-
__split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
550+
size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap_ - __first_), 1);
551+
__split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc_);
558552
__t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
559553
std::swap(__first_, __t.__first_);
560554
std::swap(__begin_, __t.__begin_);
561555
std::swap(__end_, __t.__end_);
562-
std::swap(__end_cap(), __t.__end_cap());
556+
std::swap(__end_cap_, __t.__end_cap_);
563557
}
564558
}
565-
__alloc_traits::construct(__alloc(), std::__to_address(__end_), std::forward<_Args>(__args)...);
559+
__alloc_traits::construct(__alloc_, std::__to_address(__end_), std::forward<_Args>(__args)...);
566560
++__end_;
567561
}
568562

0 commit comments

Comments
 (0)