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 @@
1111
1212#include <__algorithm/comp.h>
1313#include <__algorithm/comp_ref_type.h>
14#include <__algorithm/iter_swap.h>
1514#include <__algorithm/iterator_operations.h>
1615#include <__algorithm/min_element.h>
1716#include <__algorithm/partial_sort.h>
1817#include <__algorithm/unwrap_iter.h>
19#include <__assert>
20#include <__bit/blsr.h>
21#include <__bit/countl.h>
22#include <__bit/countr.h>
2318#include <__config>
2419#include <__debug>
2520#include <__debug_utils/randomize_range.h>
......@@ -28,10 +23,11 @@
2823#include <__iterator/iterator_traits.h>
2924#include <__memory/destruct_n.h>
3025#include <__memory/unique_ptr.h>
31#include <__type_traits/conditional.h>
3226#include <__type_traits/is_arithmetic.h>
27#include <__type_traits/is_trivially_copy_assignable.h>
28#include <__type_traits/is_trivially_copy_constructible.h>
3329#include <__utility/move.h>
34#include <__utility/pair.h>
30#include <bit>
3531#include <climits>
3632#include <cstdint>
3733
......@@ -132,7 +128,8 @@ template <class _AlgPolicy, class _Compare, class _ForwardIterator>
132128_LIBCPP_HIDE_FROM_ABI
133129unsigned __sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3, _ForwardIterator __x4,
134130 _Compare __c) {
135 using _Ops = _IterOps<_AlgPolicy>;
131 using _Ops = _IterOps<_AlgPolicy>;
132
136133 unsigned __r = std::__sort3<_AlgPolicy, _Compare>(__x1, __x2, __x3, __c);
137134 if (__c(*__x4, *__x3)) {
138135 _Ops::iter_swap(__x3, __x4);
......@@ -187,7 +184,7 @@ _LIBCPP_HIDE_FROM_ABI unsigned __sort5_wrap_policy(
187184 _Compare __c) {
188185 using _WrappedComp = typename _WrapAlgPolicy<_AlgPolicy, _Compare>::type;
189186 _WrappedComp __wrapped_comp(__c);
190 return std::__sort5<_WrappedComp, _ForwardIterator>(
187 return std::__sort5<_WrappedComp>(
191188 std::move(__x1), std::move(__x2), std::move(__x3), std::move(__x4), std::move(__x5), __wrapped_comp);
192189}
193190
......@@ -212,13 +209,6 @@ using __use_branchless_sort =
212209 integral_constant<bool, __is_cpp17_contiguous_iterator<_Iter>::value && sizeof(_Tp) <= sizeof(void*) &&
213210 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
222212// Ensures that __c(*__x, *__y) is true by swapping *__x and *__y if necessary.
223213template <class _Compare, class _RandomAccessIterator>
224214inline _LIBCPP_HIDE_FROM_ABI void __cond_swap(_RandomAccessIterator __x, _RandomAccessIterator __y, _Compare __c) {
......@@ -278,15 +268,10 @@ __sort4_maybe_branchless(_RandomAccessIterator __x1, _RandomAccessIterator __x2,
278268 std::__sort4<_AlgPolicy, _Compare>(__x1, __x2, __x3, __x4, __c);
279269}
280270
281template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>
271template <class, class _Compare, class _RandomAccessIterator>
282272inline _LIBCPP_HIDE_FROM_ABI __enable_if_t<__use_branchless_sort<_Compare, _RandomAccessIterator>::value, void>
283__sort5_maybe_branchless(
284 _RandomAccessIterator __x1,
285 _RandomAccessIterator __x2,
286 _RandomAccessIterator __x3,
287 _RandomAccessIterator __x4,
288 _RandomAccessIterator __x5,
289 _Compare __c) {
273__sort5_maybe_branchless(_RandomAccessIterator __x1, _RandomAccessIterator __x2, _RandomAccessIterator __x3,
274 _RandomAccessIterator __x4, _RandomAccessIterator __x5, _Compare __c) {
290275 std::__cond_swap<_Compare>(__x1, __x2, __c);
291276 std::__cond_swap<_Compare>(__x4, __x5, __c);
292277 std::__partially_sorted_swap<_Compare>(__x3, __x4, __x5, __c);
......@@ -315,48 +300,34 @@ _LIBCPP_CONSTEXPR_SINCE_CXX14 void __selection_sort(_BidirectionalIterator __fir
315300 }
316301}
317302
318// Sort the iterator range [__first, __last) using the comparator __comp using
319// the insertion sort algorithm.
320303template <class _AlgPolicy, class _Compare, class _BidirectionalIterator>
321304_LIBCPP_HIDE_FROM_ABI
322305void __insertion_sort(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp) {
323306 using _Ops = _IterOps<_AlgPolicy>;
324307
325308 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
326 if (__first == __last)
327 return;
328 _BidirectionalIterator __i = __first;
329 for (++__i; __i != __last; ++__i) {
330 _BidirectionalIterator __j = __i;
331 --__j;
332 if (__comp(*__i, *__j)) {
333 value_type __t(_Ops::__iter_move(__i));
334 _BidirectionalIterator __k = __j;
335 __j = __i;
336 do {
309 if (__first != __last) {
310 _BidirectionalIterator __i = __first;
311 for (++__i; __i != __last; ++__i) {
312 _BidirectionalIterator __j = __i;
313 value_type __t(_Ops::__iter_move(__j));
314 for (_BidirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
337315 *__j = _Ops::__iter_move(__k);
338 __j = __k;
339 } while (__j != __first && __comp(__t, *--__k));
340316 *__j = std::move(__t);
341317 }
342318 }
343319}
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.
350321template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>
351_LIBCPP_HIDE_FROM_ABI void
352__insertion_sort_unguarded(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) {
322_LIBCPP_HIDE_FROM_ABI
323void __insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) {
353324 using _Ops = _IterOps<_AlgPolicy>;
325
354326 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
355327 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
356 if (__first == __last)
357 return;
358 for (_RandomAccessIterator __i = __first + difference_type(1); __i != __last; ++__i) {
359 _RandomAccessIterator __j = __i - difference_type(1);
328 _RandomAccessIterator __j = __first + difference_type(2);
329 std::__sort3_maybe_branchless<_AlgPolicy, _Compare>(__first, __first + difference_type(1), __j, __comp);
330 for (_RandomAccessIterator __i = __j + difference_type(1); __i != __last; ++__i) {
360331 if (__comp(*__i, *__j)) {
361332 value_type __t(_Ops::__iter_move(__i));
362333 _RandomAccessIterator __k = __j;
......@@ -364,9 +335,10 @@ __insertion_sort_unguarded(_RandomAccessIterator __first, _RandomAccessIterator
364335 do {
365336 *__j = _Ops::__iter_move(__k);
366337 __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));
368339 *__j = std::move(__t);
369340 }
341 __j = __i;
370342 }
371343}
372344
......@@ -387,7 +359,7 @@ _LIBCPP_HIDDEN bool __insertion_sort_incomplete(
387359 return true;
388360 case 2:
389361 if (__comp(*--__last, *__first))
390 _Ops::iter_swap(__first, __last);
362 _IterOps<_AlgPolicy>::iter_swap(__first, __last);
391363 return true;
392364 case 3:
393365 std::__sort3_maybe_branchless<_AlgPolicy, _Compare>(__first, __first + difference_type(1), --__last, __comp);
......@@ -456,336 +428,17 @@ void __insertion_sort_move(_BidirectionalIterator __first1, _BidirectionalIterat
456428 }
457429}
458430
459template <class _AlgPolicy, class _RandomAccessIterator>
460inline _LIBCPP_HIDE_FROM_ABI void __swap_bitmap_pos(
461 _RandomAccessIterator __first, _RandomAccessIterator __last, uint64_t& __left_bitset, uint64_t& __right_bitset) {
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) {
431template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>
432void __introsort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
433 typename iterator_traits<_RandomAccessIterator>::difference_type __depth) {
733434 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>;
782436 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
783 using _Comp_ref = __comp_ref_type<_Compare>;
784 // Upper bound for using insertion sort for sorting.
785 _LIBCPP_CONSTEXPR difference_type __limit = 24;
786 // Lower bound for using Tuckey's ninther technique for median computation.
787 _LIBCPP_CONSTEXPR difference_type __ninther_threshold = 128;
437 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
438 const difference_type __limit =
439 is_trivially_copy_constructible<value_type>::value && is_trivially_copy_assignable<value_type>::value ? 30 : 6;
788440 while (true) {
441 __restart:
789442 difference_type __len = __last - __first;
790443 switch (__len) {
791444 case 0:
......@@ -793,7 +446,7 @@ void __introsort(_RandomAccessIterator __first,
793446 return;
794447 case 2:
795448 if (__comp(*--__last, *__first))
796 _Ops::iter_swap(__first, __last);
449 _IterOps<_AlgPolicy>::iter_swap(__first, __last);
797450 return;
798451 case 3:
799452 std::__sort3_maybe_branchless<_AlgPolicy, _Compare>(__first, __first + difference_type(1), --__last, __comp);
......@@ -808,60 +461,127 @@ void __introsort(_RandomAccessIterator __first,
808461 --__last, __comp);
809462 return;
810463 }
811 // Use insertion sort if the length of the range is below the specified limit.
812 if (__len < __limit) {
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 }
464 if (__len <= __limit) {
465 std::__insertion_sort_3<_AlgPolicy, _Compare>(__first, __last, __comp);
818466 return;
819467 }
468 // __len > 5
820469 if (__depth == 0) {
821470 // Fallback to heap sort as Introsort suggests.
822471 std::__partial_sort<_AlgPolicy, _Compare>(__first, __last, __last, __comp);
823472 return;
824473 }
825474 --__depth;
475 _RandomAccessIterator __m = __first;
476 _RandomAccessIterator __lm1 = __last;
477 --__lm1;
478 unsigned __n_swaps;
826479 {
827 difference_type __half_len = __len / 2;
828 // Use Tuckey's ninther technique or median of 3 for pivot selection
829 // depending on the length of the range being sorted.
830 if (__len > __ninther_threshold) {
831 std::__sort3<_AlgPolicy, _Compare>(__first, __first + __half_len, __last - difference_type(1), __comp);
832 std::__sort3<_AlgPolicy, _Compare>(
833 __first + difference_type(1), __first + (__half_len - 1), __last - difference_type(2), __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);
480 difference_type __delta;
481 if (__len >= 1000) {
482 __delta = __len / 2;
483 __m += __delta;
484 __delta /= 2;
485 __n_swaps = std::__sort5_wrap_policy<_AlgPolicy, _Compare>(
486 __first, __first + __delta, __m, __m + __delta, __lm1, __comp);
839487 } 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);
841491 }
842492 }
843 // The elements to the left of the current iterator range are already
844 // sorted. If the current iterator range to be sorted is not the
845 // leftmost part of the entire iterator range and the pivot is same as
846 // the highest element in the range to the left, then we know that all
847 // the elements in the range [first, pivot] would be equal to the pivot,
848 // assuming the equal elements are put on the left side when
849 // partitioned. This also means that we do not need to sort the left
850 // side of the partition.
851 if (!__leftmost && !__comp(*(__first - difference_type(1)), *__first)) {
852 __first = std::__partition_with_equals_on_left<_AlgPolicy, _RandomAccessIterator, _Comp_ref>(
853 __first, __last, _Comp_ref(__comp));
854 continue;
493 // *__m is median
494 // partition [__first, __m) < *__m and *__m <= [__m, __last)
495 // (this inhibits tossing elements equivalent to __m around unnecessarily)
496 _RandomAccessIterator __i = __first;
497 _RandomAccessIterator __j = __lm1;
498 // j points beyond range to be tested, *__m is known to be <= *__lm1
499 // The search going up is known to be guarded but the search coming down isn't.
500 // Prime the downward search with a guard.
501 if (!__comp(*__i, *__m)) // if *__first == *__m
502 {
503 // *__first == *__m, *__first doesn't go in first part
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;
855581 }
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;
862582 // [__first, __i) < *__i and *__i <= [__i+1, __last)
863583 // If we were given a perfect partition, see if insertion sort is quick...
864 if (__ret.second) {
584 if (__n_swaps == 0) {
865585 using _WrappedComp = typename _WrapAlgPolicy<_AlgPolicy, _Compare>::type;
866586 _WrappedComp __wrapped_comp(__comp);
867587 bool __fs = std::__insertion_sort_incomplete<_WrappedComp>(__first, __i, __wrapped_comp);
......@@ -877,11 +597,14 @@ void __introsort(_RandomAccessIterator __first,
877597 }
878598 }
879599 }
880 // Sort the left partiton recursively and the right partition with tail recursion elimination.
881 std::__introsort<_AlgPolicy, _Compare, _RandomAccessIterator, _UseBitSetPartition>(
882 __first, __i, __comp, __depth, __leftmost);
883 __leftmost = false;
884 __first = ++__i;
600 // sort smaller range with recursive call and larger with tail recursion elimination
601 if (__i - __first < __last - __i) {
602 std::__introsort<_AlgPolicy, _Compare>(__first, __i, __comp, __depth);
603 __first = ++__i;
604 } else {
605 std::__introsort<_AlgPolicy, _Compare>(__i + difference_type(1), __last, __comp, __depth);
606 __last = __i;
607 }
885608 }
886609}
887610
......@@ -913,14 +636,7 @@ _LIBCPP_HIDDEN void __sort(_RandomAccessIterator __first, _RandomAccessIterator
913636 using _AlgPolicy = typename _Unwrap::_AlgPolicy;
914637 using _Compare = typename _Unwrap::_Comp;
915638 _Compare __comp = _Unwrap::__get_comp(__wrapped_comp);
916 // Only use bitset partitioning for arithmetic types. We should also check
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);
639 std::__introsort<_AlgPolicy, _Compare>(__first, __last, __comp, __depth_limit);
924640}
925641
926642template <class _Compare, class _Tp>
lib/libcxx/include/__config+2-1
......@@ -23,6 +23,7 @@
2323#endif
2424
2525#if defined(__apple_build_version__)
26// Given AppleClang XX.Y.Z, _LIBCPP_APPLE_CLANG_VER is XXYZ (e.g. AppleClang 14.0.3 => 1403)
2627# define _LIBCPP_COMPILER_CLANG_BASED
2728# define _LIBCPP_APPLE_CLANG_VER (__apple_build_version__ / 10000)
2829#elif defined(__clang__)
......@@ -37,7 +38,7 @@
3738// _LIBCPP_VERSION represents the version of libc++, which matches the version of LLVM.
3839// Given a LLVM release LLVM XX.YY.ZZ (e.g. LLVM 16.0.1 == 16.00.01), _LIBCPP_VERSION is
3940// defined to XXYYZZ.
40# define _LIBCPP_VERSION 160000
41# define _LIBCPP_VERSION 160001
4142
4243# define _LIBCPP_CONCAT_IMPL(_X, _Y) _X##_Y
4344# define _LIBCPP_CONCAT(_X, _Y) _LIBCPP_CONCAT_IMPL(_X, _Y)
lib/libcxx/include/__expected/expected.h+4-3
......@@ -292,7 +292,8 @@ private:
292292 "be reverted to the previous state in case an exception is thrown during the assignment.");
293293 _T2 __tmp(std::move(__oldval));
294294 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)); });
296297 std::construct_at(std::addressof(__newval), std::forward<_Args>(__args)...);
297298 __trans.__complete();
298299 }
......@@ -451,7 +452,7 @@ public:
451452 if constexpr (is_nothrow_move_constructible_v<_Err>) {
452453 _Err __tmp(std::move(__with_err.__union_.__unex_));
453454 std::destroy_at(std::addressof(__with_err.__union_.__unex_));
454 __exception_guard __trans([&] {
455 auto __trans = std::__make_exception_guard([&] {
455456 std::construct_at(std::addressof(__with_err.__union_.__unex_), std::move(__tmp));
456457 });
457458 std::construct_at(std::addressof(__with_err.__union_.__val_), std::move(__with_val.__union_.__val_));
......@@ -464,7 +465,7 @@ public:
464465 "that it can be reverted to the previous state in case an exception is thrown during swap.");
465466 _Tp __tmp(std::move(__with_val.__union_.__val_));
466467 std::destroy_at(std::addressof(__with_val.__union_.__val_));
467 __exception_guard __trans([&] {
468 auto __trans = std::__make_exception_guard([&] {
468469 std::construct_at(std::addressof(__with_val.__union_.__val_), std::move(__tmp));
469470 });
470471 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* _
421421 _Tp& __array = *__loc;
422422
423423 // 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([&]() {
425425 std::__allocator_destroy_multidimensional(__elem_alloc, __array, __array + __i);
426426 });
427427
......@@ -461,7 +461,7 @@ constexpr void __allocator_construct_at_multidimensional(_Alloc& __alloc, _Tp* _
461461 _Tp& __array = *__loc;
462462
463463 // 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([&]() {
465465 std::__allocator_destroy_multidimensional(__elem_alloc, __array, __array + __i);
466466 });
467467 for (; __i != extent_v<_Tp>; ++__i) {
......@@ -488,7 +488,7 @@ __uninitialized_allocator_fill_n_multidimensional(_Alloc& __alloc, _BidirIter __
488488 _BidirIter __begin = __it;
489489
490490 // 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); });
492492 for (; __n != 0; --__n, ++__it) {
493493 std::__allocator_construct_at_multidimensional(__value_alloc, std::addressof(*__it), __value);
494494 }
......@@ -505,7 +505,7 @@ __uninitialized_allocator_value_construct_n_multidimensional(_Alloc& __alloc, _B
505505 _BidirIter __begin = __it;
506506
507507 // 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); });
509509 for (; __n != 0; --__n, ++__it) {
510510 std::__allocator_construct_at_multidimensional(__value_alloc, std::addressof(*__it));
511511 }
lib/libcxx/include/__memory_resource/polymorphic_allocator.h+1-1
......@@ -98,7 +98,7 @@ public:
9898 template <class _Type, class... _CtorArgs>
9999 [[nodiscard]] _Type* new_object(_CtorArgs&&... __ctor_args) {
100100 _Type* __ptr = allocate_object<_Type>();
101 __exception_guard __guard([&] { deallocate_object(__ptr); });
101 auto __guard = std::__make_exception_guard([&] { deallocate_object(__ptr); });
102102 construct(__ptr, std::forward<_CtorArgs>(__ctor_args)...);
103103 __guard.__complete();
104104 return __ptr;
lib/libcxx/include/__utility/exception_guard.h+29-18
......@@ -60,25 +60,26 @@ _LIBCPP_BEGIN_NAMESPACE_STD
6060
6161#ifndef _LIBCPP_NO_EXCEPTIONS
6262template <class _Rollback>
63struct __exception_guard {
64 __exception_guard() = delete;
63struct __exception_guard_exceptions {
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)
6767 : __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)
7071 _NOEXCEPT_(is_nothrow_move_constructible<_Rollback>::value)
7172 : __rollback_(std::move(__other.__rollback_)), __completed_(__other.__completed_) {
7273 __other.__completed_ = true;
7374 }
7475
75 __exception_guard(__exception_guard const&) = delete;
76 __exception_guard& operator=(__exception_guard const&) = delete;
77 __exception_guard& operator=(__exception_guard&&) = delete;
76 __exception_guard_exceptions(__exception_guard_exceptions const&) = delete;
77 __exception_guard_exceptions& operator=(__exception_guard_exceptions const&) = delete;
78 __exception_guard_exceptions& operator=(__exception_guard_exceptions&&) = delete;
7879
7980 _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() {
8283 if (!__completed_)
8384 __rollback_();
8485 }
......@@ -87,36 +88,46 @@ private:
8788 _Rollback __rollback_;
8889 bool __completed_;
8990};
91
92_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(__exception_guard_exceptions);
93
94template <class _Rollback>
95using __exception_guard = __exception_guard_exceptions<_Rollback>;
9096#else // _LIBCPP_NO_EXCEPTIONS
9197template <class _Rollback>
92struct __exception_guard {
93 __exception_guard() = delete;
94 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NODEBUG explicit __exception_guard(_Rollback) {}
98struct __exception_guard_noexceptions {
99 __exception_guard_noexceptions() = delete;
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)
97105 _NOEXCEPT_(is_nothrow_move_constructible<_Rollback>::value)
98106 : __completed_(__other.__completed_) {
99107 __other.__completed_ = true;
100108 }
101109
102 __exception_guard(__exception_guard const&) = delete;
103 __exception_guard& operator=(__exception_guard const&) = delete;
104 __exception_guard& operator=(__exception_guard&&) = delete;
110 __exception_guard_noexceptions(__exception_guard_noexceptions const&) = delete;
111 __exception_guard_noexceptions& operator=(__exception_guard_noexceptions const&) = delete;
112 __exception_guard_noexceptions& operator=(__exception_guard_noexceptions&&) = delete;
105113
106114 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NODEBUG void __complete() _NOEXCEPT {
107115 __completed_ = true;
108116 }
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() {
111119 _LIBCPP_ASSERT(__completed_, "__exception_guard not completed with exceptions disabled");
112120 }
113121
114122private:
115123 bool __completed_ = false;
116124};
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
121132template <class _Rollback>
122133_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 {
3535
3636_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
4041class source_location {
4142 // The names source_location::__impl, _M_file_name, _M_function_name, _M_line, and _M_column
......@@ -78,7 +79,8 @@ public:
7879 }
7980};
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
8385_LIBCPP_END_NAMESPACE_STD
8486
lib/libcxx/include/version+1-1
......@@ -366,7 +366,7 @@ __cpp_lib_void_t 201411L <type_traits>
366366# define __cpp_lib_shared_ptr_arrays 201707L
367367# define __cpp_lib_shift 201806L
368368// # 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)
370370# define __cpp_lib_source_location 201907L
371371# endif
372372# define __cpp_lib_span 202002L