Skip to content

Commit

Permalink
fzf-v2: stop pushing indices to MatchedIndices once it's full
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Oct 28, 2023
1 parent bae0a61 commit fc8926d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 37 deletions.
27 changes: 8 additions & 19 deletions src/algos/fzf/slab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,46 +161,35 @@ impl MatchedIndicesSlab {
}

/// TODO: docs
#[derive(Debug)]
pub(super) struct MatchedIndices<'a> {
indices: &'a mut [CandidateCharIdx],
len: usize,
query_char_len: usize,
}

impl<'a> MatchedIndices<'a> {
/// TODO: docs
#[inline]
pub fn into_iter(self) -> impl Iterator<Item = CandidateCharIdx> + 'a {
self.indices[..self.len].iter().copied()
self.indices[..self.query_char_len].iter().copied()
}

/// TODO: docs
#[inline]
pub fn last(&self) -> CandidateCharIdx {
self.indices[self.len - 1]
}

/// TODO: docs
#[inline]
pub fn len(&self) -> usize {
self.len
pub fn is_full(&self) -> bool {
self.indices.len() == self.query_char_len
}

#[inline]
pub fn new(indices: &'a mut [CandidateCharIdx]) -> Self {
Self { indices, len: 0 }
Self { indices, query_char_len: 0 }
}

/// TODO: docs
#[inline]
pub fn push(&mut self, idx: CandidateCharIdx) {
self.indices[self.len] = idx;
self.len += 1;
}
}

impl core::fmt::Debug for MatchedIndices<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.indices[..self.len].fmt(f)
self.indices[self.query_char_len] = idx;
self.query_char_len += 1;
}
}

Expand Down
41 changes: 23 additions & 18 deletions src/algos/fzf/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,23 @@ impl Metric for FzfV2 {

let case_matcher = self.case_sensitivity.matcher(query);

let (matched_indices, bonus_vector) = matched_indices(
&mut self.slab.matched_indices,
&mut self.slab.bonus_vector,
query,
candidate,
&case_matcher,
&self.scheme,
)?;
let (matched_indices, last_matched_idx, bonus_vector) =
matched_indices(
&mut self.slab.matched_indices,
&mut self.slab.bonus_vector,
query,
candidate,
&case_matcher,
&self.scheme,
)?;

let (scores, consecutive, score, score_cell) = score(
&mut self.slab.scoring_matrix,
&mut self.slab.consecutive_matrix,
query,
candidate,
matched_indices,
last_matched_idx,
bonus_vector,
case_matcher,
);
Expand All @@ -117,17 +119,19 @@ fn matched_indices<'idx, 'bonus>(
candidate: Candidate,
case_matcher: &CaseMatcher,
scheme: &Scheme,
) -> Option<(MatchedIndices<'idx>, BonusVector<'bonus>)> {
) -> Option<(MatchedIndices<'idx>, CandidateCharIdx, BonusVector<'bonus>)> {
let mut query_chars = query.chars();

let mut query_char = query_chars.next().expect("query is not empty");

let mut prev_class = scheme.initial_char_class;

let mut bonuses = bonuses_slab.alloc(candidate);

let mut matched_idxs = indices_slab.alloc(query);

let mut last_matched_idx = CandidateCharIdx(0);

let mut bonuses = bonuses_slab.alloc(candidate);

for (char_idx, candidate_char) in candidate.char_idxs() {
let char_class = char_class(candidate_char, scheme);
let bonus = bonus(prev_class, char_class, scheme);
Expand All @@ -136,16 +140,20 @@ fn matched_indices<'idx, 'bonus>(
bonuses[char_idx] = bonus;

if case_matcher.eq(query_char, candidate_char) {
matched_idxs.push(char_idx);
if !matched_idxs.is_full() {
matched_idxs.push(char_idx);
}

last_matched_idx = char_idx;

if let Some(next_char) = query_chars.next() {
query_char = next_char;
}
}
}

if matched_idxs.len() == query.char_len() {
Some((matched_idxs, bonuses))
if matched_idxs.is_full() {
Some((matched_idxs, last_matched_idx, bonuses))
} else {
None
}
Expand All @@ -159,6 +167,7 @@ fn score<'scoring, 'consecutive>(
query: FzfQuery,
candidate: Candidate,
matched_indices: MatchedIndices,
last_matched_idx: CandidateCharIdx,
bonus_vector: BonusVector,
case_matcher: CaseMatcher,
) -> (Matrix<'scoring, Score>, Matrix<'consecutive, usize>, Score, MatrixCell)
Expand All @@ -167,10 +176,6 @@ fn score<'scoring, 'consecutive>(

let mut consecutive_matrix = consecutive_slab.alloc(query, candidate);

// The char index in the candidate string of the character that matched the
// last character in the query string.
let last_matched_idx = matched_indices.last();

let mut chars_idxs_rows = query
.chars()
.zip(matched_indices.into_iter())
Expand Down

0 comments on commit fc8926d

Please sign in to comment.