@@ -29,37 +29,35 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2929
3030template <class _AlgPolicy , class _Compare , class _RandomAccessIterator >
3131_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
32- __sift_up (_RandomAccessIterator __first,
33- _RandomAccessIterator __last,
34- _Compare&& __comp,
35- typename iterator_traits<_RandomAccessIterator>::difference_type __len) {
36- using value_type = typename iterator_traits<_RandomAccessIterator>::value_type;
37-
38- if (__len > 1 ) {
39- __len = (__len - 2 ) / 2 ;
40- _RandomAccessIterator __ptr = __first + __len;
41-
42- if (__comp (*__ptr, *--__last)) {
43- value_type __t (_IterOps<_AlgPolicy>::__iter_move (__last));
44- do {
45- *__last = _IterOps<_AlgPolicy>::__iter_move (__ptr);
46- __last = __ptr;
47- if (__len == 0 )
48- break ;
49- __len = (__len - 1 ) / 2 ;
50- __ptr = __first + __len;
51- } while (__comp (*__ptr, __t ));
52-
53- *__last = std::move (__t );
54- }
32+ __sift_up (_RandomAccessIterator __first, _RandomAccessIterator __bottom, _Compare&& __comp) {
33+ using difference_type = typename iterator_traits<_RandomAccessIterator>::difference_type;
34+ using value_type = typename iterator_traits<_RandomAccessIterator>::value_type;
35+
36+ difference_type __parent = __bottom - __first;
37+ _LIBCPP_ASSERT_INTERNAL (__parent > 0 , " shouldn't be called unless __bottom - __first > 0" );
38+ __parent = (__parent - 1 ) / 2 ;
39+ _RandomAccessIterator __parent_i = __first + __parent;
40+
41+ if (__comp (*__parent_i, *__bottom)) {
42+ value_type __t (_IterOps<_AlgPolicy>::__iter_move (__bottom));
43+ do {
44+ *__bottom = _IterOps<_AlgPolicy>::__iter_move (__parent_i);
45+ __bottom = __parent_i;
46+ if (__parent == 0 )
47+ break ;
48+ __parent = (__parent - 1 ) / 2 ;
49+ __parent_i = __first + __parent;
50+ } while (__comp (*__parent_i, __t ));
51+
52+ *__bottom = std::move (__t );
5553 }
5654}
5755
5856template <class _AlgPolicy , class _RandomAccessIterator , class _Compare >
5957inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
60- __push_heap (_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare& __comp) {
61- typename iterator_traits<_RandomAccessIterator>::difference_type __len = __last - __first;
62- std::__sift_up<_AlgPolicy, __comp_ref_type<_Compare> >(std::move (__first), std::move (__last), __comp, __len );
58+ __push_heap (_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare&& __comp) {
59+ if (__first ! = __last)
60+ std::__sift_up<_AlgPolicy, __comp_ref_type<_Compare> >(std::move (__first), std::move (-- __last), __comp);
6361}
6462
6563template <class _RandomAccessIterator , class _Compare >
0 commit comments