@@ -44,10 +44,13 @@ __sift_down(_RandomAccessIterator __first,
44
44
__child = 2 * __child + 1 ;
45
45
_RandomAccessIterator __child_i = __first + __child;
46
46
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;
47
+ if ((__child + 1 ) < __len) {
48
+ _RandomAccessIterator __right_child_i = _Ops::next (__child_i);
49
+ if (__comp (*__child_i, *__right_child_i)) {
50
+ // right-child exists and is greater than left-child
51
+ __child_i = __right_child_i;
52
+ ++__child;
53
+ }
51
54
}
52
55
53
56
// check if we are in heap-order
@@ -61,17 +64,20 @@ __sift_down(_RandomAccessIterator __first,
61
64
*__start = _Ops::__iter_move (__child_i);
62
65
__start = __child_i;
63
66
64
- if ((__len - 2 ) / 2 < __child)
67
+ __child = 2 * __child + 1 ;
68
+ if (!(__child < __len))
65
69
break ;
66
70
67
71
// recompute the child based off of the updated parent
68
- __child = 2 * __child + 1 ;
69
72
__child_i = __first + __child;
70
73
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;
74
+ if ((__child + 1 ) < __len) {
75
+ _RandomAccessIterator __right_child_i = _Ops::next (__child_i);
76
+ if (__comp (*__child_i, *__right_child_i)) {
77
+ // right-child exists and is greater than left-child
78
+ __child_i = __right_child_i;
79
+ ++__child;
80
+ }
75
81
}
76
82
77
83
// check if we are in heap-order
0 commit comments