authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-05 23:22:29-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-05 23:22:29-07:00
logfc9ab4144cc577e5293e685af2d53ac1cb3415ce
tree2cf1e127938ad4717d3a49af835aa3551cc7428d
parentf2892775990a5a71147d8b5d3ff272cf56a41a06

update libcxx to LLVM 16.0.1


8 files changed, 192 insertions(+), 461 deletions(-)

lib/libcxx/include/__algorithm/sort.h+147-431
...@@ -11,15 +11,10 @@...@@ -11,15 +11,10 @@
1111
12#include <__algorithm/comp.h>12#include <__algorithm/comp.h>
13#include <__algorithm/comp_ref_type.h>13#include <__algorithm/comp_ref_type.h>
14#include <__algorithm/iter_swap.h>
15#include <__algorithm/iterator_operations.h>14#include <__algorithm/iterator_operations.h>
16#include <__algorithm/min_element.h>15#include <__algorithm/min_element.h>
17#include <__algorithm/partial_sort.h>16#include <__algorithm/partial_sort.h>
18#include <__algorithm/unwrap_iter.h>17#include <__algorithm/unwrap_iter.h>
19#include <__assert>
20#include <__bit/blsr.h>
21#include <__bit/countl.h>
22#include <__bit/countr.h>
23#include <__config>18#include <__config>
24#include <__debug>19#include <__debug>
25#include <__debug_utils/randomize_range.h>20#include <__debug_utils/randomize_range.h>
...@@ -28,10 +23,11 @@...@@ -28,10 +23,11 @@
28#include <__iterator/iterator_traits.h>23#include <__iterator/iterator_traits.h>
29#include <__memory/destruct_n.h>24#include <__memory/destruct_n.h>
30#include <__memory/unique_ptr.h>25#include <__memory/unique_ptr.h>
31#include <__type_traits/conditional.h>
32#include <__type_traits/is_arithmetic.h>26#include <__type_traits/is_arithmetic.h>
27#include <__type_traits/is_trivially_copy_assignable.h>
28#include <__type_traits/is_trivially_copy_constructible.h>
33#include <__utility/move.h>29#include <__utility/move.h>
34#include <__utility/pair.h>30#include <bit>
35#include <climits>31#include <climits>
36#include <cstdint>32#include <cstdint>
3733
...@@ -132,7 +128,8 @@ template <class _AlgPolicy, class _Compare, class _ForwardIterator>...@@ -132,7 +128,8 @@ template <class _AlgPolicy, class _Compare, class _ForwardIterator>
132_LIBCPP_HIDE_FROM_ABI128_LIBCPP_HIDE_FROM_ABI
133unsigned __sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3, _ForwardIterator __x4,129unsigned __sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3, _ForwardIterator __x4,
134 _Compare __c) {130 _Compare __c) {
135 using _Ops = _IterOps<_AlgPolicy>;131 using _Ops = _IterOps<_AlgPolicy>;
132
136 unsigned __r = std::__sort3<_AlgPolicy, _Compare>(__x1, __x2, __x3, __c);133 unsigned __r = std::__sort3<_AlgPolicy, _Compare>(__x1, __x2, __x3, __c);
137 if (__c(*__x4, *__x3)) {134 if (__c(*__x4, *__x3)) {
138 _Ops::iter_swap(__x3, __x4);135 _Ops::iter_swap(__x3, __x4);
...@@ -187,7 +184,7 @@ _LIBCPP_HIDE_FROM_ABI unsigned __sort5_wrap_policy(...@@ -187,7 +184,7 @@ _LIBCPP_HIDE_FROM_ABI unsigned __sort5_wrap_policy(
187 _Compare __c) {184 _Compare __c) {
188 using _WrappedComp = typename _WrapAlgPolicy<_AlgPolicy, _Compare>::type;185 using _WrappedComp = typename _WrapAlgPolicy<_AlgPolicy, _Compare>::type;
189 _WrappedComp __wrapped_comp(__c);186 _WrappedComp __wrapped_comp(__c);
190 return std::__sort5<_WrappedComp, _ForwardIterator>(187 return std::__sort5<_WrappedComp>(
191 std::move(__x1), std::move(__x2), std::move(__x3), std::move(__x4), std::move(__x5), __wrapped_comp);188 std::move(__x1), std::move(__x2), std::move(__x3), std::move(__x4), std::move(__x5), __wrapped_comp);
192}189}
193190
...@@ -212,13 +209,6 @@ using __use_branchless_sort =...@@ -212,13 +209,6 @@ using __use_branchless_sort =
212 integral_constant<bool, __is_cpp17_contiguous_iterator<_Iter>::value && sizeof(_Tp) <= sizeof(void*) &&209 integral_constant<bool, __is_cpp17_contiguous_iterator<_Iter>::value && sizeof(_Tp) <= sizeof(void*) &&
213 is_arithmetic<_Tp>::value && __is_simple_comparator<_Compare>::value>;210 is_arithmetic<_Tp>::value && __is_simple_comparator<_Compare>::value>;
214211
215namespace __detail {
216
217// Size in bits for the bitset in use.
218enum { __block_size = sizeof(uint64_t) * 8 };
219
220} // namespace __detail
221
222// Ensures that __c(*__x, *__y) is true by swapping *__x and *__y if necessary.212// Ensures that __c(*__x, *__y) is true by swapping *__x and *__y if necessary.
223template <class _Compare, class _RandomAccessIterator>213template <class _Compare, class _RandomAccessIterator>
224inline _LIBCPP_HIDE_FROM_ABI void __cond_swap(_RandomAccessIterator __x, _RandomAccessIterator __y, _Compare __c) {214inline _LIBCPP_HIDE_FROM_ABI void __cond_swap(_RandomAccessIterator __x, _RandomAccessIterator __y, _Compare __c) {
...@@ -278,15 +268,10 @@ __sort4_maybe_branchless(_RandomAccessIterator __x1, _RandomAccessIterator __x2,...@@ -278,15 +268,10 @@ __sort4_maybe_branchless(_RandomAccessIterator __x1, _RandomAccessIterator __x2,
278 std::__sort4<_AlgPolicy, _Compare>(__x1, __x2, __x3, __x4, __c);268 std::__sort4<_AlgPolicy, _Compare>(__x1, __x2, __x3, __x4, __c);
279}269}
280270
281template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>271template <class, class _Compare, class _RandomAccessIterator>
282inline _LIBCPP_HIDE_FROM_ABI __enable_if_t<__use_branchless_sort<_Compare, _RandomAccessIterator>::value, void>272inline _LIBCPP_HIDE_FROM_ABI __enable_if_t<__use_branchless_sort<_Compare, _RandomAccessIterator>::value, void>
283__sort5_maybe_branchless(273__sort5_maybe_branchless(_RandomAccessIterator __x1, _RandomAccessIterator __x2, _RandomAccessIterator __x3,
284 _RandomAccessIterator __x1,274 _RandomAccessIterator __x4, _RandomAccessIterator __x5, _Compare __c) {
285 _RandomAccessIterator __x2,
286 _RandomAccessIterator __x3,
287 _RandomAccessIterator __x4,
288 _RandomAccessIterator __x5,
289 _Compare __c) {
290 std::__cond_swap<_Compare>(__x1, __x2, __c);275 std::__cond_swap<_Compare>(__x1, __x2, __c);
291 std::__cond_swap<_Compare>(__x4, __x5, __c);276 std::__cond_swap<_Compare>(__x4, __x5, __c);
292 std::__partially_sorted_swap<_Compare>(__x3, __x4, __x5, __c);277 std::__partially_sorted_swap<_Compare>(__x3, __x4, __x5, __c);
...@@ -315,48 +300,34 @@ _LIBCPP_CONSTEXPR_SINCE_CXX14 void __selection_sort(_BidirectionalIterator __fir...@@ -315,48 +300,34 @@ _LIBCPP_CONSTEXPR_SINCE_CXX14 void __selection_sort(_BidirectionalIterator __fir
315 }300 }
316}301}
317302
318// Sort the iterator range [__first, __last) using the comparator __comp using
319// the insertion sort algorithm.
320template <class _AlgPolicy, class _Compare, class _BidirectionalIterator>303template <class _AlgPolicy, class _Compare, class _BidirectionalIterator>
321_LIBCPP_HIDE_FROM_ABI304_LIBCPP_HIDE_FROM_ABI
322void __insertion_sort(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp) {305void __insertion_sort(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp) {
323 using _Ops = _IterOps<_AlgPolicy>;306 using _Ops = _IterOps<_AlgPolicy>;
324307
325 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;308 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
326 if (__first == __last)309 if (__first != __last) {
327 return;310 _BidirectionalIterator __i = __first;
328 _BidirectionalIterator __i = __first;311 for (++__i; __i != __last; ++__i) {
329 for (++__i; __i != __last; ++__i) {312 _BidirectionalIterator __j = __i;
330 _BidirectionalIterator __j = __i;313 value_type __t(_Ops::__iter_move(__j));
331 --__j;314 for (_BidirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
332 if (__comp(*__i, *__j)) {
333 value_type __t(_Ops::__iter_move(__i));
334 _BidirectionalIterator __k = __j;
335 __j = __i;
336 do {
337 *__j = _Ops::__iter_move(__k);315 *__j = _Ops::__iter_move(__k);
338 __j = __k;
339 } while (__j != __first && __comp(__t, *--__k));
340 *__j = std::move(__t);316 *__j = std::move(__t);
341 }317 }
342 }318 }
343}319}
344320
345// Sort the iterator range [__first, __last) using the comparator __comp using
346// the insertion sort algorithm. Insertion sort has two loops, outer and inner.
347// The implementation below has not bounds check (unguarded) for the inner loop.
348// Assumes that there is an element in the position (__first - 1) and that each
349// element in the input range is greater or equal to the element at __first - 1.
350template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>321template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>
351_LIBCPP_HIDE_FROM_ABI void322_LIBCPP_HIDE_FROM_ABI
352__insertion_sort_unguarded(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) {323void __insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) {
353 using _Ops = _IterOps<_AlgPolicy>;324 using _Ops = _IterOps<_AlgPolicy>;
325
354 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;326 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
355 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;327 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
356 if (__first == __last)328 _RandomAccessIterator __j = __first + difference_type(2);
357 return;329 std::__sort3_maybe_branchless<_AlgPolicy, _Compare>(__first, __first + difference_type(1), __j, __comp);
358 for (_RandomAccessIterator __i = __first + difference_type(1); __i != __last; ++__i) {330 for (_RandomAccessIterator __i = __j + difference_type(1); __i != __last; ++__i) {
359 _RandomAccessIterator __j = __i - difference_type(1);
360 if (__comp(*__i, *__j)) {331 if (__comp(*__i, *__j)) {
361 value_type __t(_Ops::__iter_move(__i));332 value_type __t(_Ops::__iter_move(__i));
362 _RandomAccessIterator __k = __j;333 _RandomAccessIterator __k = __j;
...@@ -364,9 +335,10 @@ __insertion_sort_unguarded(_RandomAccessIterator __first, _RandomAccessIterator...@@ -364,9 +335,10 @@ __insertion_sort_unguarded(_RandomAccessIterator __first, _RandomAccessIterator
364 do {335 do {
365 *__j = _Ops::__iter_move(__k);336 *__j = _Ops::__iter_move(__k);
366 __j = __k;337 __j = __k;
367 } while (__comp(__t, *--__k)); // No need for bounds check due to the assumption stated above.338 } while (__j != __first && __comp(__t, *--__k));
368 *__j = std::move(__t);339 *__j = std::move(__t);
369 }340 }
341 __j = __i;
370 }342 }
371}343}
372344
...@@ -387,7 +359,7 @@ _LIBCPP_HIDDEN bool __insertion_sort_incomplete(...@@ -387,7 +359,7 @@ _LIBCPP_HIDDEN bool __insertion_sort_incomplete(
387 return true;359 return true;
388 case 2:360 case 2:
389 if (__comp(*--__last, *__first))361 if (__comp(*--__last, *__first))
390 _Ops::iter_swap(__first, __last);362 _IterOps<_AlgPolicy>::iter_swap(__first, __last);
391 return true;363 return true;
392 case 3:364 case 3:
393 std::__sort3_maybe_branchless<_AlgPolicy, _Compare>(__first, __first + difference_type(1), --__last, __comp);365 std::__sort3_maybe_branchless<_AlgPolicy, _Compare>(__first, __first + difference_type(1), --__last, __comp);
...@@ -456,336 +428,17 @@ void __insertion_sort_move(_BidirectionalIterator __first1, _BidirectionalIterat...@@ -456,336 +428,17 @@ void __insertion_sort_move(_BidirectionalIterator __first1, _BidirectionalIterat
456 }428 }
457}429}
458430
459template <class _AlgPolicy, class _RandomAccessIterator>431template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>
460inline _LIBCPP_HIDE_FROM_ABI void __swap_bitmap_pos(432void __introsort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
461 _RandomAccessIterator __first, _RandomAccessIterator __last, uint64_t& __left_bitset, uint64_t& __right_bitset) {433 typename iterator_traits<_RandomAccessIterator>::difference_type __depth) {
462 using _Ops = _IterOps<_AlgPolicy>;
463 typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
464 // Swap one pair on each iteration as long as both bitsets have at least one
465 // element for swapping.
466 while (__left_bitset != 0 && __right_bitset != 0) {
467 difference_type tz_left = __libcpp_ctz(__left_bitset);
468 __left_bitset = __libcpp_blsr(__left_bitset);
469 difference_type tz_right = __libcpp_ctz(__right_bitset);
470 __right_bitset = __libcpp_blsr(__right_bitset);
471 _Ops::iter_swap(__first + tz_left, __last - tz_right);
472 }
473}
474
475template <class _Compare,
476 class _RandomAccessIterator,
477 class _ValueType = typename iterator_traits<_RandomAccessIterator>::value_type>
478inline _LIBCPP_HIDE_FROM_ABI void
479__populate_left_bitset(_RandomAccessIterator __first, _Compare __comp, _ValueType& __pivot, uint64_t& __left_bitset) {
480 // Possible vectorization. With a proper "-march" flag, the following loop
481 // will be compiled into a set of SIMD instructions.
482 _RandomAccessIterator __iter = __first;
483 for (int __j = 0; __j < __detail::__block_size;) {
484 bool __comp_result = !__comp(*__iter, __pivot);
485 __left_bitset |= (static_cast<uint64_t>(__comp_result) << __j);
486 __j++;
487 ++__iter;
488 }
489}
490
491template <class _Compare,
492 class _RandomAccessIterator,
493 class _ValueType = typename iterator_traits<_RandomAccessIterator>::value_type>
494inline _LIBCPP_HIDE_FROM_ABI void
495__populate_right_bitset(_RandomAccessIterator __lm1, _Compare __comp, _ValueType& __pivot, uint64_t& __right_bitset) {
496 // Possible vectorization. With a proper "-march" flag, the following loop
497 // will be compiled into a set of SIMD instructions.
498 _RandomAccessIterator __iter = __lm1;
499 for (int __j = 0; __j < __detail::__block_size;) {
500 bool __comp_result = __comp(*__iter, __pivot);
501 __right_bitset |= (static_cast<uint64_t>(__comp_result) << __j);
502 __j++;
503 --__iter;
504 }
505}
506
507template <class _AlgPolicy,
508 class _Compare,
509 class _RandomAccessIterator,
510 class _ValueType = typename iterator_traits<_RandomAccessIterator>::value_type>
511inline _LIBCPP_HIDE_FROM_ABI void __bitset_partition_partial_blocks(
512 _RandomAccessIterator& __first,
513 _RandomAccessIterator& __lm1,
514 _Compare __comp,
515 _ValueType& __pivot,
516 uint64_t& __left_bitset,
517 uint64_t& __right_bitset) {
518 typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
519 difference_type __remaining_len = __lm1 - __first + 1;
520 difference_type __l_size;
521 difference_type __r_size;
522 if (__left_bitset == 0 && __right_bitset == 0) {
523 __l_size = __remaining_len / 2;
524 __r_size = __remaining_len - __l_size;
525 } else if (__left_bitset == 0) {
526 // We know at least one side is a full block.
527 __l_size = __remaining_len - __detail::__block_size;
528 __r_size = __detail::__block_size;
529 } else { // if (__right_bitset == 0)
530 __l_size = __detail::__block_size;
531 __r_size = __remaining_len - __detail::__block_size;
532 }
533 // Record the comparison outcomes for the elements currently on the left side.
534 if (__left_bitset == 0) {
535 _RandomAccessIterator __iter = __first;
536 for (int j = 0; j < __l_size; j++) {
537 bool __comp_result = !__comp(*__iter, __pivot);
538 __left_bitset |= (static_cast<uint64_t>(__comp_result) << j);
539 ++__iter;
540 }
541 }
542 // Record the comparison outcomes for the elements currently on the right
543 // side.
544 if (__right_bitset == 0) {
545 _RandomAccessIterator __iter = __lm1;
546 for (int j = 0; j < __r_size; j++) {
547 bool __comp_result = __comp(*__iter, __pivot);
548 __right_bitset |= (static_cast<uint64_t>(__comp_result) << j);
549 --__iter;
550 }
551 }
552 std::__swap_bitmap_pos<_AlgPolicy, _RandomAccessIterator>(__first, __lm1, __left_bitset, __right_bitset);
553 __first += (__left_bitset == 0) ? __l_size : 0;
554 __lm1 -= (__right_bitset == 0) ? __r_size : 0;
555}
556
557template <class _AlgPolicy, class _RandomAccessIterator>
558inline _LIBCPP_HIDE_FROM_ABI void __swap_bitmap_pos_within(
559 _RandomAccessIterator& __first, _RandomAccessIterator& __lm1, uint64_t& __left_bitset, uint64_t& __right_bitset) {
560 using _Ops = _IterOps<_AlgPolicy>;
561 typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
562 if (__left_bitset) {
563 // Swap within the left side. Need to find set positions in the reverse
564 // order.
565 while (__left_bitset != 0) {
566 difference_type __tz_left = __detail::__block_size - 1 - __libcpp_clz(__left_bitset);
567 __left_bitset &= (static_cast<uint64_t>(1) << __tz_left) - 1;
568 _RandomAccessIterator it = __first + __tz_left;
569 if (it != __lm1) {
570 _Ops::iter_swap(it, __lm1);
571 }
572 --__lm1;
573 }
574 __first = __lm1 + difference_type(1);
575 } else if (__right_bitset) {
576 // Swap within the right side. Need to find set positions in the reverse
577 // order.
578 while (__right_bitset != 0) {
579 difference_type __tz_right = __detail::__block_size - 1 - __libcpp_clz(__right_bitset);
580 __right_bitset &= (static_cast<uint64_t>(1) << __tz_right) - 1;
581 _RandomAccessIterator it = __lm1 - __tz_right;
582 if (it != __first) {
583 _Ops::iter_swap(it, __first);
584 }
585 ++__first;
586 }
587 }
588}
589
590// Partition [__first, __last) using the comparator __comp. *__first has the
591// chosen pivot. Elements that are equivalent are kept to the left of the
592// pivot. Returns the iterator for the pivot and a bool value which is true if
593// the provided range is already sorted, false otherwise. We assume that the
594// length of the range is at least three elements.
595//
596// __bitset_partition uses bitsets for storing outcomes of the comparisons
597// between the pivot and other elements.
598template <class _AlgPolicy, class _RandomAccessIterator, class _Compare>
599_LIBCPP_HIDE_FROM_ABI std::pair<_RandomAccessIterator, bool>
600__bitset_partition(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) {
601 using _Ops = _IterOps<_AlgPolicy>;
602 typedef typename std::iterator_traits<_RandomAccessIterator>::value_type value_type;
603 typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
604 _LIBCPP_ASSERT(__last - __first >= difference_type(3), "");
605
606 _RandomAccessIterator __begin = __first;
607 value_type __pivot(_Ops::__iter_move(__first));
608 // Find the first element greater than the pivot.
609 if (__comp(__pivot, *(__last - difference_type(1)))) {
610 // Not guarded since we know the last element is greater than the pivot.
611 while (!__comp(__pivot, *++__first)) {
612 }
613 } else {
614 while (++__first < __last && !__comp(__pivot, *__first)) {
615 }
616 }
617 // Find the last element less than or equal to the pivot.
618 if (__first < __last) {
619 // It will be always guarded because __introsort will do the median-of-three
620 // before calling this.
621 while (__comp(__pivot, *--__last)) {
622 }
623 }
624 // If the first element greater than the pivot is at or after the
625 // last element less than or equal to the pivot, then we have covered the
626 // entire range without swapping elements. This implies the range is already
627 // partitioned.
628 bool __already_partitioned = __first >= __last;
629 if (!__already_partitioned) {
630 _Ops::iter_swap(__first, __last);
631 ++__first;
632 }
633
634 // In [__first, __last) __last is not inclusive. From now on, it uses last
635 // minus one to be inclusive on both sides.
636 _RandomAccessIterator __lm1 = __last - difference_type(1);
637 uint64_t __left_bitset = 0;
638 uint64_t __right_bitset = 0;
639
640 // Reminder: length = __lm1 - __first + 1.
641 while (__lm1 - __first >= 2 * __detail::__block_size - 1) {
642 // Record the comparison outcomes for the elements currently on the left
643 // side.
644 if (__left_bitset == 0)
645 std::__populate_left_bitset<_Compare>(__first, __comp, __pivot, __left_bitset);
646 // Record the comparison outcomes for the elements currently on the right
647 // side.
648 if (__right_bitset == 0)
649 std::__populate_right_bitset<_Compare>(__lm1, __comp, __pivot, __right_bitset);
650 // Swap the elements recorded to be the candidates for swapping in the
651 // bitsets.
652 std::__swap_bitmap_pos<_AlgPolicy, _RandomAccessIterator>(__first, __lm1, __left_bitset, __right_bitset);
653 // Only advance the iterator if all the elements that need to be moved to
654 // other side were moved.
655 __first += (__left_bitset == 0) ? difference_type(__detail::__block_size) : difference_type(0);
656 __lm1 -= (__right_bitset == 0) ? difference_type(__detail::__block_size) : difference_type(0);
657 }
658 // Now, we have a less-than a block worth of elements on at least one of the
659 // sides.
660 std::__bitset_partition_partial_blocks<_AlgPolicy, _Compare>(
661 __first, __lm1, __comp, __pivot, __left_bitset, __right_bitset);
662 // At least one the bitsets would be empty. For the non-empty one, we need to
663 // properly partition the elements that appear within that bitset.
664 std::__swap_bitmap_pos_within<_AlgPolicy>(__first, __lm1, __left_bitset, __right_bitset);
665
666 // Move the pivot to its correct position.
667 _RandomAccessIterator __pivot_pos = __first - difference_type(1);
668 if (__begin != __pivot_pos) {
669 *__begin = _Ops::__iter_move(__pivot_pos);
670 }
671 *__pivot_pos = std::move(__pivot);
672 return std::make_pair(__pivot_pos, __already_partitioned);
673}
674
675// Partition [__first, __last) using the comparator __comp. *__first has the
676// chosen pivot. Elements that are equivalent are kept to the right of the
677// pivot. Returns the iterator for the pivot and a bool value which is true if
678// the provided range is already sorted, false otherwise. We assume that the
679// length of the range is at least three elements.
680template <class _AlgPolicy, class _RandomAccessIterator, class _Compare>
681_LIBCPP_HIDE_FROM_ABI std::pair<_RandomAccessIterator, bool>
682__partition_with_equals_on_right(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) {
683 using _Ops = _IterOps<_AlgPolicy>;
684 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
685 typedef typename std::iterator_traits<_RandomAccessIterator>::value_type value_type;
686 _LIBCPP_ASSERT(__last - __first >= difference_type(3), "");
687 _RandomAccessIterator __begin = __first;
688 value_type __pivot(_Ops::__iter_move(__first));
689 // Find the first element greater or equal to the pivot. It will be always
690 // guarded because __introsort will do the median-of-three before calling
691 // this.
692 while (__comp(*++__first, __pivot))
693 ;
694
695 // Find the last element less than the pivot.
696 if (__begin == __first - difference_type(1)) {
697 while (__first < __last && !__comp(*--__last, __pivot))
698 ;
699 } else {
700 // Guarded.
701 while (!__comp(*--__last, __pivot))
702 ;
703 }
704
705 // If the first element greater than or equal to the pivot is at or after the
706 // last element less than the pivot, then we have covered the entire range
707 // without swapping elements. This implies the range is already partitioned.
708 bool __already_partitioned = __first >= __last;
709 // Go through the remaining elements. Swap pairs of elements (one to the
710 // right of the pivot and the other to left of the pivot) that are not on the
711 // correct side of the pivot.
712 while (__first < __last) {
713 _Ops::iter_swap(__first, __last);
714 while (__comp(*++__first, __pivot))
715 ;
716 while (!__comp(*--__last, __pivot))
717 ;
718 }
719 // Move the pivot to its correct position.
720 _RandomAccessIterator __pivot_pos = __first - difference_type(1);
721 if (__begin != __pivot_pos) {
722 *__begin = _Ops::__iter_move(__pivot_pos);
723 }
724 *__pivot_pos = std::move(__pivot);
725 return std::make_pair(__pivot_pos, __already_partitioned);
726}
727
728// Similar to the above function. Elements equivalent to the pivot are put to
729// the left of the pivot. Returns the iterator to the pivot element.
730template <class _AlgPolicy, class _RandomAccessIterator, class _Compare>
731_LIBCPP_HIDE_FROM_ABI _RandomAccessIterator
732__partition_with_equals_on_left(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) {
733 using _Ops = _IterOps<_AlgPolicy>;434 using _Ops = _IterOps<_AlgPolicy>;
734 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
735 typedef typename std::iterator_traits<_RandomAccessIterator>::value_type value_type;
736 _RandomAccessIterator __begin = __first;
737 value_type __pivot(_Ops::__iter_move(__first));
738 if (__comp(__pivot, *(__last - difference_type(1)))) {
739 // Guarded.
740 while (!__comp(__pivot, *++__first)) {
741 }
742 } else {
743 while (++__first < __last && !__comp(__pivot, *__first)) {
744 }
745 }
746
747 if (__first < __last) {
748 // It will be always guarded because __introsort will do the
749 // median-of-three before calling this.
750 while (__comp(__pivot, *--__last)) {
751 }
752 }
753 while (__first < __last) {
754 _Ops::iter_swap(__first, __last);
755 while (!__comp(__pivot, *++__first))
756 ;
757 while (__comp(__pivot, *--__last))
758 ;
759 }
760 _RandomAccessIterator __pivot_pos = __first - difference_type(1);
761 if (__begin != __pivot_pos) {
762 *__begin = _Ops::__iter_move(__pivot_pos);
763 }
764 *__pivot_pos = std::move(__pivot);
765 return __first;
766}
767435
768// The main sorting function. Implements introsort combined with other ideas:
769// - option of using block quick sort for partitioning,
770// - guarded and unguarded insertion sort for small lengths,
771// - Tuckey's ninther technique for computing the pivot,
772// - check on whether partition was not required.
773// The implementation is partly based on Orson Peters' pattern-defeating
774// quicksort, published at: <https://github.com/orlp/pdqsort>.
775template <class _AlgPolicy, class _Compare, class _RandomAccessIterator, bool _UseBitSetPartition>
776void __introsort(_RandomAccessIterator __first,
777 _RandomAccessIterator __last,
778 _Compare __comp,
779 typename iterator_traits<_RandomAccessIterator>::difference_type __depth,
780 bool __leftmost = true) {
781 using _Ops = _IterOps<_AlgPolicy>;
782 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;436 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
783 using _Comp_ref = __comp_ref_type<_Compare>;437 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
784 // Upper bound for using insertion sort for sorting.438 const difference_type __limit =
785 _LIBCPP_CONSTEXPR difference_type __limit = 24;439 is_trivially_copy_constructible<value_type>::value && is_trivially_copy_assignable<value_type>::value ? 30 : 6;
786 // Lower bound for using Tuckey's ninther technique for median computation.
787 _LIBCPP_CONSTEXPR difference_type __ninther_threshold = 128;
788 while (true) {440 while (true) {
441 __restart:
789 difference_type __len = __last - __first;442 difference_type __len = __last - __first;
790 switch (__len) {443 switch (__len) {
791 case 0:444 case 0:
...@@ -793,7 +446,7 @@ void __introsort(_RandomAccessIterator __first,...@@ -793,7 +446,7 @@ void __introsort(_RandomAccessIterator __first,
793 return;446 return;
794 case 2:447 case 2:
795 if (__comp(*--__last, *__first))448 if (__comp(*--__last, *__first))
796 _Ops::iter_swap(__first, __last);449 _IterOps<_AlgPolicy>::iter_swap(__first, __last);
797 return;450 return;
798 case 3:451 case 3:
799 std::__sort3_maybe_branchless<_AlgPolicy, _Compare>(__first, __first + difference_type(1), --__last, __comp);452 std::__sort3_maybe_branchless<_AlgPolicy, _Compare>(__first, __first + difference_type(1), --__last, __comp);
...@@ -808,60 +461,127 @@ void __introsort(_RandomAccessIterator __first,...@@ -808,60 +461,127 @@ void __introsort(_RandomAccessIterator __first,
808 --__last, __comp);461 --__last, __comp);
809 return;462 return;
810 }463 }
811 // Use insertion sort if the length of the range is below the specified limit.464 if (__len <= __limit) {
812 if (__len < __limit) {465 std::__insertion_sort_3<_AlgPolicy, _Compare>(__first, __last, __comp);
813 if (__leftmost) {
814 std::__insertion_sort<_AlgPolicy, _Compare>(__first, __last, __comp);
815 } else {
816 std::__insertion_sort_unguarded<_AlgPolicy, _Compare>(__first, __last, __comp);
817 }
818 return;466 return;
819 }467 }
468 // __len > 5
820 if (__depth == 0) {469 if (__depth == 0) {
821 // Fallback to heap sort as Introsort suggests.470 // Fallback to heap sort as Introsort suggests.
822 std::__partial_sort<_AlgPolicy, _Compare>(__first, __last, __last, __comp);471 std::__partial_sort<_AlgPolicy, _Compare>(__first, __last, __last, __comp);
823 return;472 return;
824 }473 }
825 --__depth;474 --__depth;
475 _RandomAccessIterator __m = __first;
476 _RandomAccessIterator __lm1 = __last;
477 --__lm1;
478 unsigned __n_swaps;
826 {479 {
827 difference_type __half_len = __len / 2;480 difference_type __delta;
828 // Use Tuckey's ninther technique or median of 3 for pivot selection481 if (__len >= 1000) {
829 // depending on the length of the range being sorted.482 __delta = __len / 2;
830 if (__len > __ninther_threshold) {483 __m += __delta;
831 std::__sort3<_AlgPolicy, _Compare>(__first, __first + __half_len, __last - difference_type(1), __comp);484 __delta /= 2;
832 std::__sort3<_AlgPolicy, _Compare>(485 __n_swaps = std::__sort5_wrap_policy<_AlgPolicy, _Compare>(
833 __first + difference_type(1), __first + (__half_len - 1), __last - difference_type(2), __comp);486 __first, __first + __delta, __m, __m + __delta, __lm1, __comp);
834 std::__sort3<_AlgPolicy, _Compare>(
835 __first + difference_type(2), __first + (__half_len + 1), __last - difference_type(3), __comp);
836 std::__sort3<_AlgPolicy, _Compare>(
837 __first + (__half_len - 1), __first + __half_len, __first + (__half_len + 1), __comp);
838 _Ops::iter_swap(__first, __first + __half_len);
839 } else {487 } else {
840 std::__sort3<_AlgPolicy, _Compare>(__first + __half_len, __first, __last - difference_type(1), __comp);488 __delta = __len / 2;
489 __m += __delta;
490 __n_swaps = std::__sort3<_AlgPolicy, _Compare>(__first, __m, __lm1, __comp);
841 }491 }
842 }492 }
843 // The elements to the left of the current iterator range are already493 // *__m is median
844 // sorted. If the current iterator range to be sorted is not the494 // partition [__first, __m) < *__m and *__m <= [__m, __last)
845 // leftmost part of the entire iterator range and the pivot is same as495 // (this inhibits tossing elements equivalent to __m around unnecessarily)
846 // the highest element in the range to the left, then we know that all496 _RandomAccessIterator __i = __first;
847 // the elements in the range [first, pivot] would be equal to the pivot,497 _RandomAccessIterator __j = __lm1;
848 // assuming the equal elements are put on the left side when498 // j points beyond range to be tested, *__m is known to be <= *__lm1
849 // partitioned. This also means that we do not need to sort the left499 // The search going up is known to be guarded but the search coming down isn't.
850 // side of the partition.500 // Prime the downward search with a guard.
851 if (!__leftmost && !__comp(*(__first - difference_type(1)), *__first)) {501 if (!__comp(*__i, *__m)) // if *__first == *__m
852 __first = std::__partition_with_equals_on_left<_AlgPolicy, _RandomAccessIterator, _Comp_ref>(502 {
853 __first, __last, _Comp_ref(__comp));503 // *__first == *__m, *__first doesn't go in first part
854 continue;504 // manually guard downward moving __j against __i
505 while (true) {
506 if (__i == --__j) {
507 // *__first == *__m, *__m <= all other elements
508 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
509 ++__i; // __first + 1
510 __j = __last;
511 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
512 {
513 while (true) {
514 if (__i == __j)
515 return; // [__first, __last) all equivalent elements
516 if (__comp(*__first, *__i)) {
517 _Ops::iter_swap(__i, __j);
518 ++__n_swaps;
519 ++__i;
520 break;
521 }
522 ++__i;
523 }
524 }
525 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
526 if (__i == __j)
527 return;
528 while (true) {
529 while (!__comp(*__first, *__i))
530 ++__i;
531 while (__comp(*__first, *--__j))
532 ;
533 if (__i >= __j)
534 break;
535 _Ops::iter_swap(__i, __j);
536 ++__n_swaps;
537 ++__i;
538 }
539 // [__first, __i) == *__first and *__first < [__i, __last)
540 // The first part is sorted, sort the second part
541 // std::__sort<_Compare>(__i, __last, __comp);
542 __first = __i;
543 goto __restart;
544 }
545 if (__comp(*__j, *__m)) {
546 _Ops::iter_swap(__i, __j);
547 ++__n_swaps;
548 break; // found guard for downward moving __j, now use unguarded partition
549 }
550 }
551 }
552 // It is known that *__i < *__m
553 ++__i;
554 // j points beyond range to be tested, *__m is known to be <= *__lm1
555 // if not yet partitioned...
556 if (__i < __j) {
557 // known that *(__i - 1) < *__m
558 // known that __i <= __m
559 while (true) {
560 // __m still guards upward moving __i
561 while (__comp(*__i, *__m))
562 ++__i;
563 // It is now known that a guard exists for downward moving __j
564 while (!__comp(*--__j, *__m))
565 ;
566 if (__i > __j)
567 break;
568 _Ops::iter_swap(__i, __j);
569 ++__n_swaps;
570 // It is known that __m != __j
571 // If __m just moved, follow it
572 if (__m == __i)
573 __m = __j;
574 ++__i;
575 }
576 }
577 // [__first, __i) < *__m and *__m <= [__i, __last)
578 if (__i != __m && __comp(*__m, *__i)) {
579 _Ops::iter_swap(__i, __m);
580 ++__n_swaps;
855 }581 }
856 // Use bitset partition only if asked for.
857 auto __ret =
858 _UseBitSetPartition
859 ? std::__bitset_partition<_AlgPolicy, _RandomAccessIterator, _Compare>(__first, __last, __comp)
860 : std::__partition_with_equals_on_right<_AlgPolicy, _RandomAccessIterator, _Compare>(__first, __last, __comp);
861 _RandomAccessIterator __i = __ret.first;
862 // [__first, __i) < *__i and *__i <= [__i+1, __last)582 // [__first, __i) < *__i and *__i <= [__i+1, __last)
863 // If we were given a perfect partition, see if insertion sort is quick...583 // If we were given a perfect partition, see if insertion sort is quick...
864 if (__ret.second) {584 if (__n_swaps == 0) {
865 using _WrappedComp = typename _WrapAlgPolicy<_AlgPolicy, _Compare>::type;585 using _WrappedComp = typename _WrapAlgPolicy<_AlgPolicy, _Compare>::type;
866 _WrappedComp __wrapped_comp(__comp);586 _WrappedComp __wrapped_comp(__comp);
867 bool __fs = std::__insertion_sort_incomplete<_WrappedComp>(__first, __i, __wrapped_comp);587 bool __fs = std::__insertion_sort_incomplete<_WrappedComp>(__first, __i, __wrapped_comp);
...@@ -877,11 +597,14 @@ void __introsort(_RandomAccessIterator __first,...@@ -877,11 +597,14 @@ void __introsort(_RandomAccessIterator __first,
877 }597 }
878 }598 }
879 }599 }
880 // Sort the left partiton recursively and the right partition with tail recursion elimination.600 // sort smaller range with recursive call and larger with tail recursion elimination
881 std::__introsort<_AlgPolicy, _Compare, _RandomAccessIterator, _UseBitSetPartition>(601 if (__i - __first < __last - __i) {
882 __first, __i, __comp, __depth, __leftmost);602 std::__introsort<_AlgPolicy, _Compare>(__first, __i, __comp, __depth);
883 __leftmost = false;603 __first = ++__i;
884 __first = ++__i;604 } else {
605 std::__introsort<_AlgPolicy, _Compare>(__i + difference_type(1), __last, __comp, __depth);
606 __last = __i;
607 }
885 }608 }
886}609}
887610
...@@ -913,14 +636,7 @@ _LIBCPP_HIDDEN void __sort(_RandomAccessIterator __first, _RandomAccessIterator...@@ -913,14 +636,7 @@ _LIBCPP_HIDDEN void __sort(_RandomAccessIterator __first, _RandomAccessIterator
913 using _AlgPolicy = typename _Unwrap::_AlgPolicy;636 using _AlgPolicy = typename _Unwrap::_AlgPolicy;
914 using _Compare = typename _Unwrap::_Comp;637 using _Compare = typename _Unwrap::_Comp;
915 _Compare __comp = _Unwrap::__get_comp(__wrapped_comp);638 _Compare __comp = _Unwrap::__get_comp(__wrapped_comp);
916 // Only use bitset partitioning for arithmetic types. We should also check639 std::__introsort<_AlgPolicy, _Compare>(__first, __last, __comp, __depth_limit);
917 // that the default comparator is in use so that we are sure that there are no
918 // branches in the comparator.
919 std::__introsort<_AlgPolicy,
920 _Compare,
921 _RandomAccessIterator,
922 __use_branchless_sort<_Compare, _RandomAccessIterator>::value>(
923 __first, __last, __comp, __depth_limit);
924}640}
925641
926template <class _Compare, class _Tp>642template <class _Compare, class _Tp>
lib/libcxx/include/__config+2-1
...@@ -23,6 +23,7 @@...@@ -23,6 +23,7 @@
23#endif23#endif
2424
25#if defined(__apple_build_version__)25#if defined(__apple_build_version__)
26// Given AppleClang XX.Y.Z, _LIBCPP_APPLE_CLANG_VER is XXYZ (e.g. AppleClang 14.0.3 => 1403)
26# define _LIBCPP_COMPILER_CLANG_BASED27# define _LIBCPP_COMPILER_CLANG_BASED
27# define _LIBCPP_APPLE_CLANG_VER (__apple_build_version__ / 10000)28# define _LIBCPP_APPLE_CLANG_VER (__apple_build_version__ / 10000)
28#elif defined(__clang__)29#elif defined(__clang__)
...@@ -37,7 +38,7 @@...@@ -37,7 +38,7 @@
37// _LIBCPP_VERSION represents the version of libc++, which matches the version of LLVM.38// _LIBCPP_VERSION represents the version of libc++, which matches the version of LLVM.
38// Given a LLVM release LLVM XX.YY.ZZ (e.g. LLVM 16.0.1 == 16.00.01), _LIBCPP_VERSION is39// Given a LLVM release LLVM XX.YY.ZZ (e.g. LLVM 16.0.1 == 16.00.01), _LIBCPP_VERSION is
39// defined to XXYYZZ.40// defined to XXYYZZ.
40# define _LIBCPP_VERSION 16000041# define _LIBCPP_VERSION 160001
4142
42# define _LIBCPP_CONCAT_IMPL(_X, _Y) _X##_Y43# define _LIBCPP_CONCAT_IMPL(_X, _Y) _X##_Y
43# define _LIBCPP_CONCAT(_X, _Y) _LIBCPP_CONCAT_IMPL(_X, _Y)44# define _LIBCPP_CONCAT(_X, _Y) _LIBCPP_CONCAT_IMPL(_X, _Y)
lib/libcxx/include/__expected/expected.h+4-3
...@@ -292,7 +292,8 @@ private:...@@ -292,7 +292,8 @@ private:
292 "be reverted to the previous state in case an exception is thrown during the assignment.");292 "be reverted to the previous state in case an exception is thrown during the assignment.");
293 _T2 __tmp(std::move(__oldval));293 _T2 __tmp(std::move(__oldval));
294 std::destroy_at(std::addressof(__oldval));294 std::destroy_at(std::addressof(__oldval));
295 __exception_guard __trans([&] { std::construct_at(std::addressof(__oldval), std::move(__tmp)); });295 auto __trans =
296 std::__make_exception_guard([&] { std::construct_at(std::addressof(__oldval), std::move(__tmp)); });
296 std::construct_at(std::addressof(__newval), std::forward<_Args>(__args)...);297 std::construct_at(std::addressof(__newval), std::forward<_Args>(__args)...);
297 __trans.__complete();298 __trans.__complete();
298 }299 }
...@@ -451,7 +452,7 @@ public:...@@ -451,7 +452,7 @@ public:
451 if constexpr (is_nothrow_move_constructible_v<_Err>) {452 if constexpr (is_nothrow_move_constructible_v<_Err>) {
452 _Err __tmp(std::move(__with_err.__union_.__unex_));453 _Err __tmp(std::move(__with_err.__union_.__unex_));
453 std::destroy_at(std::addressof(__with_err.__union_.__unex_));454 std::destroy_at(std::addressof(__with_err.__union_.__unex_));
454 __exception_guard __trans([&] {455 auto __trans = std::__make_exception_guard([&] {
455 std::construct_at(std::addressof(__with_err.__union_.__unex_), std::move(__tmp));456 std::construct_at(std::addressof(__with_err.__union_.__unex_), std::move(__tmp));
456 });457 });
457 std::construct_at(std::addressof(__with_err.__union_.__val_), std::move(__with_val.__union_.__val_));458 std::construct_at(std::addressof(__with_err.__union_.__val_), std::move(__with_val.__union_.__val_));
...@@ -464,7 +465,7 @@ public:...@@ -464,7 +465,7 @@ public:
464 "that it can be reverted to the previous state in case an exception is thrown during swap.");465 "that it can be reverted to the previous state in case an exception is thrown during swap.");
465 _Tp __tmp(std::move(__with_val.__union_.__val_));466 _Tp __tmp(std::move(__with_val.__union_.__val_));
466 std::destroy_at(std::addressof(__with_val.__union_.__val_));467 std::destroy_at(std::addressof(__with_val.__union_.__val_));
467 __exception_guard __trans([&] {468 auto __trans = std::__make_exception_guard([&] {
468 std::construct_at(std::addressof(__with_val.__union_.__val_), std::move(__tmp));469 std::construct_at(std::addressof(__with_val.__union_.__val_), std::move(__tmp));
469 });470 });
470 std::construct_at(std::addressof(__with_val.__union_.__unex_), std::move(__with_err.__union_.__unex_));471 std::construct_at(std::addressof(__with_val.__union_.__unex_), std::move(__with_err.__union_.__unex_));
lib/libcxx/include/__memory/uninitialized_algorithms.h+4-4
...@@ -421,7 +421,7 @@ constexpr void __allocator_construct_at_multidimensional(_Alloc& __alloc, _Tp* _...@@ -421,7 +421,7 @@ constexpr void __allocator_construct_at_multidimensional(_Alloc& __alloc, _Tp* _
421 _Tp& __array = *__loc;421 _Tp& __array = *__loc;
422422
423 // If an exception is thrown, destroy what we have constructed so far in reverse order.423 // If an exception is thrown, destroy what we have constructed so far in reverse order.
424 __exception_guard __guard([&]() {424 auto __guard = std::__make_exception_guard([&]() {
425 std::__allocator_destroy_multidimensional(__elem_alloc, __array, __array + __i);425 std::__allocator_destroy_multidimensional(__elem_alloc, __array, __array + __i);
426 });426 });
427427
...@@ -461,7 +461,7 @@ constexpr void __allocator_construct_at_multidimensional(_Alloc& __alloc, _Tp* _...@@ -461,7 +461,7 @@ constexpr void __allocator_construct_at_multidimensional(_Alloc& __alloc, _Tp* _
461 _Tp& __array = *__loc;461 _Tp& __array = *__loc;
462462
463 // If an exception is thrown, destroy what we have constructed so far in reverse order.463 // If an exception is thrown, destroy what we have constructed so far in reverse order.
464 __exception_guard __guard([&]() {464 auto __guard = std::__make_exception_guard([&]() {
465 std::__allocator_destroy_multidimensional(__elem_alloc, __array, __array + __i);465 std::__allocator_destroy_multidimensional(__elem_alloc, __array, __array + __i);
466 });466 });
467 for (; __i != extent_v<_Tp>; ++__i) {467 for (; __i != extent_v<_Tp>; ++__i) {
...@@ -488,7 +488,7 @@ __uninitialized_allocator_fill_n_multidimensional(_Alloc& __alloc, _BidirIter __...@@ -488,7 +488,7 @@ __uninitialized_allocator_fill_n_multidimensional(_Alloc& __alloc, _BidirIter __
488 _BidirIter __begin = __it;488 _BidirIter __begin = __it;
489489
490 // If an exception is thrown, destroy what we have constructed so far in reverse order.490 // If an exception is thrown, destroy what we have constructed so far in reverse order.
491 __exception_guard __guard([&]() { std::__allocator_destroy_multidimensional(__value_alloc, __begin, __it); });491 auto __guard = std::__make_exception_guard([&]() { std::__allocator_destroy_multidimensional(__value_alloc, __begin, __it); });
492 for (; __n != 0; --__n, ++__it) {492 for (; __n != 0; --__n, ++__it) {
493 std::__allocator_construct_at_multidimensional(__value_alloc, std::addressof(*__it), __value);493 std::__allocator_construct_at_multidimensional(__value_alloc, std::addressof(*__it), __value);
494 }494 }
...@@ -505,7 +505,7 @@ __uninitialized_allocator_value_construct_n_multidimensional(_Alloc& __alloc, _B...@@ -505,7 +505,7 @@ __uninitialized_allocator_value_construct_n_multidimensional(_Alloc& __alloc, _B
505 _BidirIter __begin = __it;505 _BidirIter __begin = __it;
506506
507 // If an exception is thrown, destroy what we have constructed so far in reverse order.507 // If an exception is thrown, destroy what we have constructed so far in reverse order.
508 __exception_guard __guard([&]() { std::__allocator_destroy_multidimensional(__value_alloc, __begin, __it); });508 auto __guard = std::__make_exception_guard([&]() { std::__allocator_destroy_multidimensional(__value_alloc, __begin, __it); });
509 for (; __n != 0; --__n, ++__it) {509 for (; __n != 0; --__n, ++__it) {
510 std::__allocator_construct_at_multidimensional(__value_alloc, std::addressof(*__it));510 std::__allocator_construct_at_multidimensional(__value_alloc, std::addressof(*__it));
511 }511 }
lib/libcxx/include/__memory_resource/polymorphic_allocator.h+1-1
...@@ -98,7 +98,7 @@ public:...@@ -98,7 +98,7 @@ public:
98 template <class _Type, class... _CtorArgs>98 template <class _Type, class... _CtorArgs>
99 [[nodiscard]] _Type* new_object(_CtorArgs&&... __ctor_args) {99 [[nodiscard]] _Type* new_object(_CtorArgs&&... __ctor_args) {
100 _Type* __ptr = allocate_object<_Type>();100 _Type* __ptr = allocate_object<_Type>();
101 __exception_guard __guard([&] { deallocate_object(__ptr); });101 auto __guard = std::__make_exception_guard([&] { deallocate_object(__ptr); });
102 construct(__ptr, std::forward<_CtorArgs>(__ctor_args)...);102 construct(__ptr, std::forward<_CtorArgs>(__ctor_args)...);
103 __guard.__complete();103 __guard.__complete();
104 return __ptr;104 return __ptr;
lib/libcxx/include/__utility/exception_guard.h+29-18
...@@ -60,25 +60,26 @@ _LIBCPP_BEGIN_NAMESPACE_STD...@@ -60,25 +60,26 @@ _LIBCPP_BEGIN_NAMESPACE_STD
6060
61#ifndef _LIBCPP_NO_EXCEPTIONS61#ifndef _LIBCPP_NO_EXCEPTIONS
62template <class _Rollback>62template <class _Rollback>
63struct __exception_guard {63struct __exception_guard_exceptions {
64 __exception_guard() = delete;64 __exception_guard_exceptions() = delete;
6565
66 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __exception_guard(_Rollback __rollback)66 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __exception_guard_exceptions(_Rollback __rollback)
67 : __rollback_(std::move(__rollback)), __completed_(false) {}67 : __rollback_(std::move(__rollback)), __completed_(false) {}
6868
69 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __exception_guard(__exception_guard&& __other)69 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
70 __exception_guard_exceptions(__exception_guard_exceptions&& __other)
70 _NOEXCEPT_(is_nothrow_move_constructible<_Rollback>::value)71 _NOEXCEPT_(is_nothrow_move_constructible<_Rollback>::value)
71 : __rollback_(std::move(__other.__rollback_)), __completed_(__other.__completed_) {72 : __rollback_(std::move(__other.__rollback_)), __completed_(__other.__completed_) {
72 __other.__completed_ = true;73 __other.__completed_ = true;
73 }74 }
7475
75 __exception_guard(__exception_guard const&) = delete;76 __exception_guard_exceptions(__exception_guard_exceptions const&) = delete;
76 __exception_guard& operator=(__exception_guard const&) = delete;77 __exception_guard_exceptions& operator=(__exception_guard_exceptions const&) = delete;
77 __exception_guard& operator=(__exception_guard&&) = delete;78 __exception_guard_exceptions& operator=(__exception_guard_exceptions&&) = delete;
7879
79 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __complete() _NOEXCEPT { __completed_ = true; }80 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __complete() _NOEXCEPT { __completed_ = true; }
8081
81 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__exception_guard() {82 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__exception_guard_exceptions() {
82 if (!__completed_)83 if (!__completed_)
83 __rollback_();84 __rollback_();
84 }85 }
...@@ -87,36 +88,46 @@ private:...@@ -87,36 +88,46 @@ private:
87 _Rollback __rollback_;88 _Rollback __rollback_;
88 bool __completed_;89 bool __completed_;
89};90};
91
92_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(__exception_guard_exceptions);
93
94template <class _Rollback>
95using __exception_guard = __exception_guard_exceptions<_Rollback>;
90#else // _LIBCPP_NO_EXCEPTIONS96#else // _LIBCPP_NO_EXCEPTIONS
91template <class _Rollback>97template <class _Rollback>
92struct __exception_guard {98struct __exception_guard_noexceptions {
93 __exception_guard() = delete;99 __exception_guard_noexceptions() = delete;
94 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NODEBUG explicit __exception_guard(_Rollback) {}100 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
101 _LIBCPP_NODEBUG explicit __exception_guard_noexceptions(_Rollback) {}
95102
96 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NODEBUG __exception_guard(__exception_guard&& __other)103 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NODEBUG
104 __exception_guard_noexceptions(__exception_guard_noexceptions&& __other)
97 _NOEXCEPT_(is_nothrow_move_constructible<_Rollback>::value)105 _NOEXCEPT_(is_nothrow_move_constructible<_Rollback>::value)
98 : __completed_(__other.__completed_) {106 : __completed_(__other.__completed_) {
99 __other.__completed_ = true;107 __other.__completed_ = true;
100 }108 }
101109
102 __exception_guard(__exception_guard const&) = delete;110 __exception_guard_noexceptions(__exception_guard_noexceptions const&) = delete;
103 __exception_guard& operator=(__exception_guard const&) = delete;111 __exception_guard_noexceptions& operator=(__exception_guard_noexceptions const&) = delete;
104 __exception_guard& operator=(__exception_guard&&) = delete;112 __exception_guard_noexceptions& operator=(__exception_guard_noexceptions&&) = delete;
105113
106 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NODEBUG void __complete() _NOEXCEPT {114 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NODEBUG void __complete() _NOEXCEPT {
107 __completed_ = true;115 __completed_ = true;
108 }116 }
109117
110 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NODEBUG ~__exception_guard() {118 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NODEBUG ~__exception_guard_noexceptions() {
111 _LIBCPP_ASSERT(__completed_, "__exception_guard not completed with exceptions disabled");119 _LIBCPP_ASSERT(__completed_, "__exception_guard not completed with exceptions disabled");
112 }120 }
113121
114private:122private:
115 bool __completed_ = false;123 bool __completed_ = false;
116};124};
117#endif // _LIBCPP_NO_EXCEPTIONS
118125
119_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(__exception_guard);126_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(__exception_guard_noexceptions);
127
128template <class _Rollback>
129using __exception_guard = __exception_guard_noexceptions<_Rollback>;
130#endif // _LIBCPP_NO_EXCEPTIONS
120131
121template <class _Rollback>132template <class _Rollback>
122_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __exception_guard<_Rollback> __make_exception_guard(_Rollback __rollback) {133_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __exception_guard<_Rollback> __make_exception_guard(_Rollback __rollback) {
lib/libcxx/include/source_location+4-2
...@@ -35,7 +35,8 @@ namespace std {...@@ -35,7 +35,8 @@ namespace std {
3535
36_LIBCPP_BEGIN_NAMESPACE_STD36_LIBCPP_BEGIN_NAMESPACE_STD
3737
38#if _LIBCPP_STD_VER >= 20 && __has_builtin(__builtin_source_location)38#if _LIBCPP_STD_VER >= 20 && __has_builtin(__builtin_source_location) && \
39 !(defined(_LIBCPP_APPLE_CLANG_VER) && _LIBCPP_APPLE_CLANG_VER <= 1403)
3940
40class source_location {41class source_location {
41 // The names source_location::__impl, _M_file_name, _M_function_name, _M_line, and _M_column42 // The names source_location::__impl, _M_file_name, _M_function_name, _M_line, and _M_column
...@@ -78,7 +79,8 @@ public:...@@ -78,7 +79,8 @@ public:
78 }79 }
79};80};
8081
81#endif // _LIBCPP_STD_VER >= 20 && __has_builtin(__builtin_source_location)82#endif // _LIBCPP_STD_VER >= 20 && __has_builtin(__builtin_source_location) && !(defined(_LIBCPP_APPLE_CLANG_VER) &&
83 // _LIBCPP_APPLE_CLANG_VER <= 1403)
8284
83_LIBCPP_END_NAMESPACE_STD85_LIBCPP_END_NAMESPACE_STD
8486
lib/libcxx/include/version+1-1
...@@ -366,7 +366,7 @@ __cpp_lib_void_t 201411L <type_traits>...@@ -366,7 +366,7 @@ __cpp_lib_void_t 201411L <type_traits>
366# define __cpp_lib_shared_ptr_arrays 201707L366# define __cpp_lib_shared_ptr_arrays 201707L
367# define __cpp_lib_shift 201806L367# define __cpp_lib_shift 201806L
368// # define __cpp_lib_smart_ptr_for_overwrite 202002L368// # define __cpp_lib_smart_ptr_for_overwrite 202002L
369# if __has_builtin(__builtin_source_location)369# if __has_builtin(__builtin_source_location) && !(defined(_LIBCPP_APPLE_CLANG_VER) && _LIBCPP_APPLE_CLANG_VER <= 1403)
370# define __cpp_lib_source_location 201907L370# define __cpp_lib_source_location 201907L
371# endif371# endif
372# define __cpp_lib_span 202002L372# define __cpp_lib_span 202002L