@@ -36,18 +36,16 @@ __sift_down(_RandomAccessIterator __first,
36
36
typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
37
37
// left-child of __start is at 2 * __start + 1
38
38
// right-child of __start is at 2 * __start + 2
39
- difference_type __child = __start - __first;
40
-
41
- if (__len < 2 || (__len - 2 ) / 2 < __child)
42
- return ;
43
-
44
- __child = 2 * __child + 1 ;
39
+ difference_type __child = 2 * (__start - __first) + 1 ;
45
40
_RandomAccessIterator __child_i = __first + __child;
46
41
47
- if ((__child + 1 ) < __len && __comp (*__child_i, *(__child_i + difference_type (1 )))) {
48
- // right-child exists and is greater than left-child
49
- ++__child_i;
50
- ++__child;
42
+ if ((__child + 1 ) < __len) {
43
+ _RandomAccessIterator __right_child_i = _Ops::next (__child_i);
44
+ if (__comp (*__child_i, *__right_child_i)) {
45
+ // right-child exists and is greater than left-child
46
+ __child_i = __right_child_i;
47
+ ++__child;
48
+ }
51
49
}
52
50
53
51
// check if we are in heap-order
@@ -61,17 +59,20 @@ __sift_down(_RandomAccessIterator __first,
61
59
*__start = _Ops::__iter_move (__child_i);
62
60
__start = __child_i;
63
61
64
- if ((__len - 2 ) / 2 < __child)
62
+ __child = 2 * __child + 1 ;
63
+ if (!(__child < __len))
65
64
break ;
66
65
67
66
// recompute the child based off of the updated parent
68
- __child = 2 * __child + 1 ;
69
67
__child_i = __first + __child;
70
68
71
- if ((__child + 1 ) < __len && __comp (*__child_i, *(__child_i + difference_type (1 )))) {
72
- // right-child exists and is greater than left-child
73
- ++__child_i;
74
- ++__child;
69
+ if ((__child + 1 ) < __len) {
70
+ _RandomAccessIterator __right_child_i = _Ops::next (__child_i);
71
+ if (__comp (*__child_i, *__right_child_i)) {
72
+ // right-child exists and is greater than left-child
73
+ __child_i = __right_child_i;
74
+ ++__child;
75
+ }
75
76
}
76
77
77
78
// check if we are in heap-order
0 commit comments