Skip to content

Commit ccdadef

Browse files
committed
Check index rather than item for short-circuit
1 parent b195cbc commit ccdadef

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/k_smallest.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,15 @@ where
124124
}
125125

126126
let (left_idx, right_idx) = children_of(origin);
127-
let (left_item, right_item) = (self.get(left_idx), self.get(right_idx));
128127

129-
if left_item.is_none() { // the left is the earlier child, so if it doesn't exist there's nothing to swap with
128+
if left_idx >= self.len {
129+
// the left is the earlier child, so if it doesn't exist there's nothing to swap with
130130
return;
131131
}
132132

133+
let (left_item, right_item) = (self.get(left_idx), self.get(right_idx));
134+
135+
133136
let cmp = self
134137
.compare(left_item, right_item)
135138
.unwrap_or(Ordering::Greater); // The right item may not exist, so default to picking the left, i.e. lower

0 commit comments

Comments
 (0)