Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Nov 20, 2023
1 parent b35d7c1 commit 96ab6bf
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 111 deletions.
181 changes: 70 additions & 111 deletions src/algos/fzf/v2.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::ops::Range;

use super::{query::*, scoring::*, slab::*, *};
use crate::utils::Opts;
use crate::*;

/// TODO: docs
Expand Down Expand Up @@ -107,18 +108,20 @@ impl Metric for FzfV2 {
CaseSensitivity::Smart => pattern.has_uppercase,
};

let char_eq =
utils::char_eq(is_case_sensitive, self.normalization);
let opts = Opts::new(
true,
true,
is_candidate_ascii,
is_case_sensitive,
false,
);

let score = fzf_v2(
&mut self.slab,
pattern,
candidate,
&self.scheme,
char_eq,
is_case_sensitive,
self.with_matched_ranges,
(&mut self.slab, is_candidate_ascii),
&mut matched_ranges,
&opts,
)?;

let distance = FzfDistance::from_score(score);
Expand All @@ -142,19 +145,29 @@ impl Metric for FzfV2 {
let char_eq =
utils::char_eq(is_case_sensitive, self.normalization);

pattern.score(
candidate,
&self.scheme,
char_eq,
let find_first = utils::find_first_fun(
true,
true,
is_candidate_ascii,
is_case_sensitive,
self.with_matched_ranges,
(&mut self.slab, is_candidate_ascii),
&mut matched_ranges,
fzf_v2,
)
false,
);

todo!();

// pattern.score(
// candidate,
// &self.scheme,
// char_eq,
// is_case_sensitive,
// self.with_matched_ranges,
// (&mut self.slab, is_candidate_ascii, find_first),
// &mut matched_ranges,
// fzf_v2,
// )
})?;

total_score += score;
// total_score += score;
}

let distance = FzfDistance::from_score(total_score);
Expand All @@ -176,27 +189,18 @@ impl Metric for FzfV2 {
/// TODO: docs
#[inline]
pub(super) fn fzf_v2(
slab: &mut V2Slab,
pattern: Pattern,
candidate: &str,
scheme: &Scheme,
char_eq: CharEq,
is_case_sensitive: bool,
with_matched_ranges: bool,
(slab, is_candidate_ascii): (&mut V2Slab, bool),
ranges: &mut MatchedRanges,
opts: &Opts,
) -> Option<Score> {
if pattern.is_empty() {
return Some(0);
}

let (matches, last_match_offset) = matches(
&mut slab.matched_indices,
pattern,
candidate,
is_case_sensitive,
is_candidate_ascii,
char_eq,
)?;
let (matches, last_match_offset) =
matches(&mut slab.matched_indices, pattern, candidate, opts)?;

let first_match = matches[0];

Expand Down Expand Up @@ -225,23 +229,21 @@ pub(super) fn fzf_v2(
&mut slab.consecutive_matrix,
pattern,
candidate,
is_case_sensitive,
is_candidate_ascii,
char_eq,
matches,
bonus_vector,
opts,
);

if with_matched_ranges {
matched_ranges(
scores,
consecutive,
score_cell,
candidate,
first_match.byte_offset,
ranges,
);
};
// if with_matched_ranges {
// matched_ranges(
// scores,
// consecutive,
// score_cell,
// candidate,
// first_match.byte_offset,
// ranges,
// );
// };

Some(score)
}
Expand All @@ -252,9 +254,7 @@ fn matches<'idx>(
indices_slab: &'idx mut MatchedIndicesSlab,
pattern: Pattern,
mut candidate: &str,
is_case_sensitive: bool,
is_candidate_ascii: bool,
char_eq: CharEq,
opts: &Opts,
) -> Option<(&'idx mut [MatchedIdx], usize)> {
let matched_idxs = indices_slab.alloc(pattern.char_len());

Expand All @@ -265,26 +265,15 @@ fn matches<'idx>(
loop {
let query_char = pattern.char(query_char_idx);

let (byte_offset, matched_char) = utils::find_first(
query_char,
candidate,
is_candidate_ascii,
is_case_sensitive,
char_eq,
)?;
let (byte_offset, matched_char_byte_len) =
opts.find_first(query_char, candidate)?;

let char_offset = if is_candidate_ascii {
byte_offset
} else {
utils::char_len(&candidate[..byte_offset])
};
let char_offset = opts.byte_to_char_offset(candidate, byte_offset);

last_matched_idx += MatchedIdx { byte_offset, char_offset };

matched_idxs[query_char_idx] = last_matched_idx;

let matched_char_byte_len = matched_char.len_utf8();

// SAFETY: the start of the range is within the byte length of the
// candidate and it's a valid char boundary.
candidate = unsafe {
Expand All @@ -302,14 +291,10 @@ fn matches<'idx>(
}

let last_char_offset_inclusive = last_matched_idx.byte_offset
+ if let Some((byte_offset, matched_char)) = utils::find_last(
pattern.char(query_char_idx),
candidate,
is_candidate_ascii,
is_case_sensitive,
char_eq,
) {
byte_offset + matched_char.len_utf8()
+ if let Some((byte_offset, matched_char_byte_len)) =
opts.find_last(pattern.char(query_char_idx), candidate)
{
byte_offset + matched_char_byte_len
} else {
0
};
Expand Down Expand Up @@ -345,11 +330,9 @@ fn score<'scoring, 'consecutive>(
consecutive_slab: &'consecutive mut MatrixSlab<usize>,
pattern: Pattern,
candidate: &str,
is_case_sensitive: bool,
is_candidate_ascii: bool,
char_eq: CharEq,
matches: &[MatchedIdx],
bonus_vector: &[Score],
opts: &Opts,
) -> (Matrix<'scoring, Score>, Matrix<'consecutive, usize>, Score, MatrixCell)
{
// The length of the bonus slice is the same as the character length of the
Expand All @@ -369,9 +352,7 @@ fn score<'scoring, 'consecutive>(
bonus_vector,
pattern.char(0),
candidate,
is_case_sensitive,
is_candidate_ascii,
char_eq,
opts,
);

let (max_score, max_score_cell) = score_remaining_rows(
Expand All @@ -381,9 +362,7 @@ fn score<'scoring, 'consecutive>(
matches,
candidate,
bonus_vector,
is_case_sensitive,
is_candidate_ascii,
char_eq,
opts,
max_score,
max_score_cell,
);
Expand All @@ -399,9 +378,7 @@ fn score_first_row(
bonus_vector: &[Score],
query_first_char: char,
mut candidate: &str,
is_case_sensitive: bool,
is_candidate_ascii: bool,
char_eq: CharEq,
opts: &Opts,
) -> (Score, MatrixCell) {
let mut max_score: Score = 0;

Expand All @@ -416,13 +393,9 @@ fn score_first_row(
let mut penalty = penalty::GAP_START;

while !candidate.is_empty() {
let Some((byte_idx, matched_char)) = utils::find_first(
query_first_char,
candidate,
is_candidate_ascii,
is_case_sensitive,
char_eq,
) else {
let Some((byte_offset, matched_char_byte_len)) =
opts.find_first(query_first_char, candidate)
else {
for col in col..scores_first_row.len() {
let score = prev_score.saturating_sub(penalty);
penalty = penalty::GAP_EXTENSION;
Expand All @@ -433,23 +406,19 @@ fn score_first_row(
break;
};

let char_idx = if is_candidate_ascii {
byte_idx
} else {
utils::char_len(&candidate[..byte_idx])
};
let char_offset = opts.byte_to_char_offset(candidate, byte_offset);

// TODO: explain what this does.
{
for col in col..col + char_idx {
for col in col..col + char_offset {
let score = prev_score.saturating_sub(penalty);
penalty = penalty::GAP_EXTENSION;
scores_first_row[col] = score;
prev_score = score;
}
}

col += char_idx;
col += char_offset;

consecutives_first_row[col] = 1;

Expand All @@ -467,7 +436,7 @@ fn score_first_row(

col += 1;

candidate = &candidate[byte_idx + matched_char.len_utf8()..];
candidate = &candidate[byte_offset + matched_char_byte_len..];
}

(max_score, MatrixCell(max_score_col))
Expand All @@ -482,9 +451,7 @@ fn score_remaining_rows(
matches: &[MatchedIdx],
candidate: &str,
bonus_vector: &[Score],
is_case_sensitive: bool,
is_candidate_ascii: bool,
char_eq: CharEq,
opts: &Opts,
mut max_score: Score,
mut max_score_cell: MatrixCell,
) -> (Score, MatrixCell) {
Expand All @@ -509,13 +476,9 @@ fn score_remaining_rows(
let mut penalty = penalty::GAP_START;

while !candidate.is_empty() {
let Some((byte_offset, matched_char)) = utils::find_first(
query_char,
candidate,
is_candidate_ascii,
is_case_sensitive,
char_eq,
) else {
let Some((byte_offset, matched_char_byte_len)) =
opts.find_first(query_char, candidate)
else {
for col in column..matrix_width {
let score_left = scores_row[col - 1];
let score = score_left.saturating_sub(penalty);
Expand All @@ -526,11 +489,7 @@ fn score_remaining_rows(
break;
};

let char_offset = if is_candidate_ascii {
byte_offset
} else {
utils::char_len(&candidate[..byte_offset])
};
let char_offset = opts.byte_to_char_offset(candidate, byte_offset);

// TODO: explain what this does.
penalty = penalty::GAP_START;
Expand Down Expand Up @@ -590,7 +549,7 @@ fn score_remaining_rows(

column += 1;

candidate = &candidate[byte_offset + matched_char.len_utf8()..];
candidate = &candidate[byte_offset + matched_char_byte_len..];
}
}

Expand Down
Loading

0 comments on commit 96ab6bf

Please sign in to comment.