Skip to content

Commit

Permalink
Avoid bounds checks in slice intersection check
Browse files Browse the repository at this point in the history
This makes practically no difference, it just bugged me.
  • Loading branch information
Ortham committed Dec 6, 2023
1 parent e570779 commit 7ecff6f
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,14 +424,17 @@ impl Plugin {
}

fn sorted_slices_intersect<T: PartialOrd>(left: &[T], right: &[T]) -> bool {
let mut left_index = 0;
let mut right_index = 0;

while left_index != left.len() && right_index != right.len() {
if left[left_index] < right[right_index] {
left_index += 1;
} else if left[left_index] > right[right_index] {
right_index += 1;
let mut left_iter = left.iter();
let mut right_iter = right.iter();

let mut left_element = left_iter.next();
let mut right_element = right_iter.next();

while let (Some(left_value), Some(right_value)) = (left_element, right_element) {
if left_value < right_value {
left_element = left_iter.next();
} else if left_value > right_value {
right_element = right_iter.next();
} else {
return true;
}
Expand Down

0 comments on commit 7ecff6f

Please sign in to comment.