Skip to content

Commit

Permalink
refactor FzfV2 to use a Candidate enum
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Nov 22, 2023
1 parent a73092a commit 54353a4
Showing 12 changed files with 700 additions and 604 deletions.
111 changes: 111 additions & 0 deletions src/algos/fzf/candidate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use super::*;
use crate::utils::*;
use crate::{Candidate, CandidateMatches};

/// TODO: docs
pub(super) struct CandidateV2<'a> {
/// TODO: docs
bonuses: &'a mut [Bonus],

/// TODO: docs
base: Candidate<'a>,

/// TODO: docs
opts: CandidateOpts,
}

/// TODO: docs
#[derive(Clone, Copy)]
pub(super) struct CandidateOpts {
/// TODO: docs
pub char_eq: CharEq,

/// TODO: docs
pub is_case_sensitive: bool,
}

impl Default for CandidateOpts {
#[inline(always)]
fn default() -> Self {
Self { char_eq: char_eq(false, false), is_case_sensitive: false }
}
}

impl CandidateOpts {
#[inline(always)]
pub fn new(is_case_sensitive: bool, is_normalized: bool) -> Self {
Self {
char_eq: char_eq(is_case_sensitive, is_normalized),
is_case_sensitive,
}
}
}

impl<'a> CandidateV2<'a> {
#[inline(always)]
pub fn bonus_at(&mut self, char_idx: usize, scheme: &Scheme) -> Score {
let bonus = &mut self.bonuses[char_idx];

if bonus.is_set() {
return bonus.value();
}

let prev_class = if char_idx == 0 {
scheme.initial_char_class
} else {
char_class(self.char(char_idx - 1), scheme)
};

let this_class = char_class(self.char(char_idx), scheme);

let bonus = &mut self.bonuses[char_idx];

bonus.set(compute_bonus(prev_class, this_class, scheme));

bonus.value()
}

#[inline(always)]
pub fn char(&self, char_idx: usize) -> char {
self.base.char(char_idx)
}

#[inline(always)]
pub fn char_len(&self) -> usize {
self.base.char_len()
}

#[inline(always)]
pub fn into_base(self) -> Candidate<'a> {
self.base
}

#[inline(always)]
pub fn matches(&self, ch: char) -> CandidateMatches<'a> {
self.base.matches(ch, self.opts.is_case_sensitive, self.opts.char_eq)
}

#[inline(always)]
pub fn matches_from(
&self,
char_offset: usize,
ch: char,
) -> CandidateMatches<'a> {
self.base.matches_from(
char_offset,
ch,
self.opts.is_case_sensitive,
self.opts.char_eq,
)
}

#[inline(always)]
pub fn new(
base: Candidate<'a>,
bonus_slab: &'a mut BonusSlab,
opts: CandidateOpts,
) -> Self {
let bonuses = bonus_slab.alloc(base.char_len());
Self { base, bonuses, opts }
}
}
5 changes: 3 additions & 2 deletions src/algos/fzf/common.rs
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ pub(super) fn calculate_score(
if opts.char_eq(pattern_char, candidate_ch) {
score += bonus::MATCH;

let mut bonus = bonus(prev_class, ch_class, scheme);
let mut bonus = compute_bonus(prev_class, ch_class, scheme);

if consecutive == 0 {
first_bonus = bonus;
@@ -146,7 +146,8 @@ pub(super) fn exact_match(
if pattern_char_idx == 0 {
bonus_start = current_start_offset + byte_offset;
start_offset += byte_offset + candidate_ch.len_utf8();
current_bonus = bonus(prev_char_class, char_class, scheme);
current_bonus =
compute_bonus(prev_char_class, char_class, scheme);
}

pattern_char_idx += 1;
3 changes: 3 additions & 0 deletions src/algos/fzf/mod.rs
Original file line number Diff line number Diff line change
@@ -43,6 +43,7 @@
//! [fzf]: https://github.com/junegunn/fzf
//! [extended-search]: https://github.com/junegunn/fzf#search-syntax
mod candidate;
mod common;
mod distance;
mod parser;
@@ -55,6 +56,7 @@ mod v1;
#[cfg(feature = "fzf-v1")]
mod v2;

use candidate::*;
use common::*;
pub use distance::FzfDistance;
use distance::*;
@@ -64,6 +66,7 @@ pub use scheme::FzfScheme;
#[doc(hidden)]
pub use scheme::Scheme;
use scoring::*;
use slab::*;
#[cfg(feature = "fzf-v1")]
pub use v1::FzfV1;
#[cfg(feature = "fzf-v1")]
26 changes: 15 additions & 11 deletions src/algos/fzf/query.rs
Original file line number Diff line number Diff line change
@@ -4,10 +4,10 @@ use super::*;
use crate::*;

/// TODO: docs
type FuzzyAlgo<O, T> = fn(
type FuzzyAlgo<T> = fn(
Pattern,
&str,
O,
Candidate,
CandidateOpts,
&Scheme,
Option<&mut MatchedRanges>,
T,
@@ -300,14 +300,14 @@ impl<'a> Pattern<'a> {

/// TODO: docs
#[inline]
pub(super) fn score<O: Opts, E>(
pub(super) fn score<E>(
self,
candidate: &str,
opts: O,
candidate: Candidate,
opts: CandidateOpts,
scheme: &Scheme,
mut ranges_buf: Option<&mut MatchedRanges>,
extra: E,
fuzzy_algo: FuzzyAlgo<O, E>,
fuzzy_algo: FuzzyAlgo<E>,
) -> Option<Score> {
if self.is_inverse {
ranges_buf = None;
@@ -319,19 +319,23 @@ impl<'a> Pattern<'a> {
},

MatchType::Exact => {
exact_match(self, candidate, opts, scheme, ranges_buf)
todo!()
// exact_match(self, candidate, opts, scheme, ranges_buf)
},

MatchType::PrefixExact => {
prefix_match(self, candidate, opts, scheme, ranges_buf)
todo!()
// prefix_match(self, candidate, opts, scheme, ranges_buf)
},

MatchType::SuffixExact => {
suffix_match(self, candidate, opts, scheme, ranges_buf)
todo!()
// suffix_match(self, candidate, opts, scheme, ranges_buf)
},

MatchType::EqualExact => {
equal_match(self, candidate, opts, scheme, ranges_buf)
todo!()
// equal_match(self, candidate, opts, scheme, ranges_buf)
},
};

2 changes: 1 addition & 1 deletion src/algos/fzf/scoring.rs
Original file line number Diff line number Diff line change
@@ -75,7 +75,7 @@ fn non_ascii_char_class(ch: char, scheme: &Scheme) -> CharClass {

/// TODO: docs
#[inline]
pub(super) fn bonus(
pub(super) fn compute_bonus(
prev_class: CharClass,
next_class: CharClass,
scheme: &Scheme,
Loading

0 comments on commit 54353a4

Please sign in to comment.