Skip to content

Commit

Permalink
fzf-v2: remove old code
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Oct 27, 2023
1 parent 5de6080 commit f5f52d5
Showing 1 changed file with 6 additions and 294 deletions.
300 changes: 6 additions & 294 deletions src/algos/fzf_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,133 +33,6 @@ pub struct FzfV2 {
}

impl FzfV2 {
/// TODO: docs
#[inline]
fn fuzzy_match(&self, query: &str, candidate: &str) -> Option<Score> {
// Phase 1.

let m = query.len();

let n = candidate.len();

let mut idx = 0;

let mut h0 = vec![0; n];

let mut c0 = vec![0; n];

let mut b = vec![0; n];

let mut f = vec![0; m];

let t = candidate.chars().collect::<Vec<_>>();

// Phase 2.

let mut max_score = 0;

let mut max_score_pos = 0;

let mut query_chars = query.chars();

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

let mut last_idx = 0;

let first_query_char = query_char;

let mut prev_h0: Score = 0;

let mut prev_class = self.scheme.initial_char_class;

let mut is_in_gap = false;

let h0_sub = &mut h0[..];

let c0_sub = &mut c0[..];

let b_sub = &mut b[..];

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

let mut pidx = 0;

for (offset, candidate_char) in candidate.char_indices() {
let char_class = char_class(candidate_char, &self.scheme);

let bonus = bonus(prev_class, char_class, &self.scheme);

b_sub[offset] = bonus;

prev_class = char_class;

if case_matcher.eq(query_char, candidate_char) {
last_idx = offset;
f[pidx] = offset;
pidx += 1;

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

if case_matcher.eq(first_query_char, candidate_char) {
let score =
bonus::MATCH + bonus * bonus::FIRST_QUERY_CHAR_MULTIPLIER;

h0_sub[offset] = score;
c0_sub[offset] = 1;

if m == 1 && score > max_score {
max_score = score;
max_score_pos = offset;
if bonus >= bonus::BOUNDARY {
break;
}
}

is_in_gap = false;
} else {
let penalty = if is_in_gap {
penalty::GAP_EXTENSION
} else {
penalty::GAP_START
};
h0_sub[offset] = prev_h0.saturating_sub(penalty);
c0_sub[offset] = 0;
is_in_gap = true;
}

prev_h0 = h0_sub[offset];
}

if pidx != m {
return None;
}

if m == 1 {
// TODO: return score.
return Some(max_score);
}

// Phase 3.
//
// Fill in score matrix H.

let score = phase_3(
&f,
&t,
&b,
&h0,
&c0,
query,
last_idx,
self.case_sensitivity,
);

Some(score)
}

/// TODO: docs
#[inline]
pub fn new() -> Self {
Expand Down Expand Up @@ -212,8 +85,6 @@ impl Metric for FzfV2 {

let query = query.raw();

// let _ = self.fuzzy_match(query, candidate);

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

let candidate = self.candidate_slab.alloc(candidate);
Expand All @@ -235,7 +106,6 @@ impl Metric for FzfV2 {
matched_indices,
bonus_vector,
case_matcher,
&self.scheme,
);

println!("\n{score:?}");
Expand Down Expand Up @@ -302,7 +172,6 @@ fn score<'scoring>(
matched_indices: MatchedIndices,
bonus_vector: BonusVector,
case_matcher: CaseMatcher,
scheme: &scheme::Scheme,
) -> (Matrix<'scoring, Score>, Score, MatrixCell) {
let mut scoring_matrix = scoring_slab.alloc(query, candidate);

Expand Down Expand Up @@ -466,12 +335,12 @@ where
for (char_idx, candidate_char) in candidate.char_idxs() {
let ((cell, left_cell), up_left_cell) = cols.next().unwrap();

let score_left = scoring_matrix[left_cell] as i64
- if is_in_gap {
let score_left =
scoring_matrix[left_cell].saturating_sub(if is_in_gap {
penalty::GAP_EXTENSION
} else {
penalty::GAP_START
} as i64;
});

let mut consecutive = 0;

Expand All @@ -494,7 +363,7 @@ where
}
}

if ((score + bonus) as i64) < score_left {
if score + bonus < score_left {
consecutive = 0;
score + bonus_vector[char_idx]
} else {
Expand All @@ -504,9 +373,9 @@ where
0
};

is_in_gap = (score_up_left as i64) < score_left;
is_in_gap = score_up_left < score_left;

let score = (score_up_left as i64).max(score_left).max(0) as Score;
let score = score_up_left.max(score_left).max(0);

if score > max_score {
max_score = score;
Expand All @@ -522,163 +391,6 @@ where
(max_score, max_score_cell)
}

fn phase_3(
f: &[usize],
t: &[char],
b: &[Score],
h0: &[Score],
c0: &[usize],
query: &str,
last_idx: usize,
case_sensitivity: CaseSensitivity,
) -> Score {
let m = query.len();

let mut max_score = 0;

let mut max_score_pos = 0;

let f0 = f[0];

let width = last_idx - f0 + 1;

let mut h = vec![0; m * width];

h[..last_idx + 1 - f0].copy_from_slice(&h0[f0..last_idx + 1]);

// Possible length of consecutive chunk at each position.
let mut c = vec![0; m * width];

c[..last_idx + 1 - f0].copy_from_slice(&c0[f0..last_idx + 1]);

let f_sub = &f[1..];

let p_sub = &query[1..][..f_sub.len()];

let case_matcher = case_sensitivity.matcher(query);

for (offset, f) in f_sub.iter().enumerate() {
let c_len = c.len();
let c_ptr = c.as_mut_ptr();

let h_len = h.len();
let h_ptr = h.as_mut_ptr();

let pchar = p_sub.chars().nth(offset).unwrap();
let pidx = offset + 1;
let row = pidx * width;
let mut is_in_gap = false;
let t_sub = &t[*f..last_idx + 1];
let b_sub = &b[*f..][..t_sub.len()];
let c_sub = &mut c[row + f - f0..][..t_sub.len()];
let h_sub = &mut h[row + f - f0..][..t_sub.len()];

let c_diag = {
let c = unsafe { core::slice::from_raw_parts_mut(c_ptr, c_len) };
&mut c[row + f - f0 - 1 - width..][..t_sub.len()]
};

let h_diag = {
let h = unsafe { core::slice::from_raw_parts_mut(h_ptr, h_len) };
&mut h[row + f - f0 - 1 - width..][..t_sub.len()]
};

let h_left = {
let h = unsafe { core::slice::from_raw_parts_mut(h_ptr, h_len) };
&mut h[row + f - f0 - 1..][..t_sub.len()]
};

// println!("-------------");
// println!("pchar: {:?}", pchar);
// println!("pidx: {:?}", pidx);
// println!("row: {:?}", row);
// println!("inGap: {:?}", is_in_gap);
// println!("Tsub: {:?}", t_sub);
// println!("Bsub: {:?}", b_sub);
// println!("Csub: {:?}", c_sub);
// println!("Cdiag: {:?}", c_diag);
// println!("Hsub: {:?}", h_sub);
// println!("Hdiag: {:?}", h_diag);
// println!("Hleft: {:?}", h_left);
// println!("-------------");

h_left[0] = 0;

for (offset, &char) in t_sub.iter().enumerate() {
let col = offset + f;

let mut s1 = 0;

let mut consecutive = 0;

let s2 = if is_in_gap {
h_left[offset] as i32 - penalty::GAP_EXTENSION as i32
} else {
h_left[offset] as i32 - penalty::GAP_START as i32
};

if case_matcher.eq(pchar, char) {
s1 = h_diag[offset] + bonus::MATCH;
let mut small_b = b_sub[offset];
consecutive = c_diag[offset] + 1;

if consecutive > 1 {
let fb = b[col - consecutive + 1];
if small_b >= bonus::BOUNDARY && small_b > fb {
consecutive = 1;
} else {
small_b = bonus::CONSECUTIVE.max(fb).max(small_b);
}
}

if (s1 as i32) + (small_b as i32) < s2 {
s1 += b_sub[offset];
consecutive = 0;
} else {
s1 += small_b;
}
}

// println!("-------------");
// println!("s1: {:?}", s1);
// println!("s2: {:?}", s2);
// println!("offset: {:?}", offset);

c_sub[offset] = consecutive;

is_in_gap = (s1 as i32) < s2;

let score = (s1 as i32).max(s2).max(0) as Score;

if pidx == m - 1 && score > max_score {
max_score = score;
max_score_pos = col;
}

// println!("-------------");
// println!("score: {:?}", score);
// println!("offset: {:?}", offset);

h_sub[offset] = score;
}
}

println!("{c:?}");
println!("\n{max_score:?}");
println!("\n{h:?}");

// println!("f0: {:?}", f0);
// println!("width: {:?}", width);
// println!("h: {:?}", h);
// println!("c: {:?}", c);
// println!("f_sub: {:?}", f_sub);
// println!("p_bub: {:?}", p_sub);

let j = f0;

max_score
}

/// TODO: docs
#[derive(Clone)]
struct CandidateSlab {
Expand Down

0 comments on commit f5f52d5

Please sign in to comment.