Skip to content

Commit

Permalink
perf: speedup find_letter_ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-aksamentov committed Dec 6, 2024
1 parent 808460e commit 5747a4d
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions packages/treetime/src/seq/find_char_ranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@
//
// For example if predicate returns `true` for characters A and C, this function will find ranges `AAAA`, `CCCCC`, `ACCCACAAA`
// but not `ZZZZZ`.
pub fn find_letter_ranges_by(seq: &[char], pred: impl Fn(char) -> bool) -> Vec<(usize, usize)> {
let mut result = vec![];
let mut iter = seq.iter().enumerate().peekable();
while let Some((start, &letter)) = iter.next() {
if pred(letter) {
let mut end = start;
while let Some(&(j, &next_letter)) = iter.peek() {
if pred(next_letter) {
end = j;
iter.next();
} else {
break;
}
#[inline]
pub fn find_letter_ranges_by(seq: &[char], pred: impl Fn(char) -> bool + Copy) -> Vec<(usize, usize)> {
let mut result = Vec::with_capacity(31);
let mut i = 0;
while i < seq.len() {
if pred(seq[i]) {
let start = i;
i += 1;
while i < seq.len() && pred(seq[i]) {
i += 1;
}
result.push((start, end + 1));
result.push((start, i));
} else {
i += 1;
}
}
result
Expand Down Expand Up @@ -67,6 +66,7 @@ pub mod old {
}

/// Finds contiguous ranges (segments) consisting of a given letter in the sequence.
#[inline]
pub fn find_letter_ranges(seq: &[char], letter: char) -> Vec<(usize, usize)> {
find_letter_ranges_by(seq, |candidate| candidate == letter)
}
Expand Down

0 comments on commit 5747a4d

Please sign in to comment.