Skip to content

Commit

Permalink
use partition_point instead of binary_search_by
Browse files Browse the repository at this point in the history
Using `partition_point` ensures we always find the first entry.
With binary search it is "random" (deterministic but implementation
specific) which index is retruned if there are multiple equal elements.
`partition_point` was added to the standard library to cover extactly
the usecase here.
  • Loading branch information
pascalkuthe authored and archseer committed Mar 27, 2023
1 parent 72b9311 commit 7cf448e
Showing 1 changed file with 1 addition and 4 deletions.
5 changes: 1 addition & 4 deletions helix-core/src/text_annotations.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::cell::Cell;
use std::convert::identity;
use std::ops::Range;
use std::rc::Rc;

Expand Down Expand Up @@ -113,9 +112,7 @@ impl<A, M> Layer<A, M> {
pub fn reset_pos(&self, char_idx: usize, get_char_idx: impl Fn(&A) -> usize) {
let new_index = self
.annotations
.binary_search_by_key(&char_idx, get_char_idx)
.unwrap_or_else(identity);

.partition_point(|annot| get_char_idx(annot) < char_idx);
self.current_index.set(new_index);
}

Expand Down

0 comments on commit 7cf448e

Please sign in to comment.