Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: speedup find letter ranges #291

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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