Skip to content

Commit

Permalink
Merge pull request #2 from nomad/candidate_enum
Browse files Browse the repository at this point in the history
distinguish ascii and unicode candidates via `Candidate` enum
  • Loading branch information
noib3 authored Nov 24, 2023
2 parents a73092a + 92d6f2f commit d09beb2
Show file tree
Hide file tree
Showing 23 changed files with 2,271 additions and 1,951 deletions.
22 changes: 10 additions & 12 deletions fuzz/fuzz_targets/fzf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,27 @@ fuzz_target!(|data: (Query, Candidate)| {

let mut fzf_v2 = FzfV2::new();

let mut ranges = norm::MatchedRanges::default();

with_opts(|case_sensitivity, normalization, scheme| {
let mach = fzf_v1
let _ = fzf_v1
.with_case_sensitivity(case_sensitivity)
.with_normalization(normalization)
.with_scoring_scheme(scheme)
.distance(query, candidate);
.distance_and_ranges(query, candidate, &mut ranges);

if let Some(mach) = mach {
for range in mach.matched_ranges() {
let _s = &candidate[range.clone()];
}
for range in ranges.as_slice() {
let _ = &candidate[range.clone()];
}

let mach = fzf_v2
let _ = fzf_v2
.with_case_sensitivity(case_sensitivity)
.with_normalization(normalization)
.with_scoring_scheme(scheme)
.distance(query, candidate);
.distance_and_ranges(query, candidate, &mut ranges);

if let Some(mach) = mach {
for range in mach.matched_ranges() {
let _s = &candidate[range.clone()];
}
for range in ranges.as_slice() {
let _ = &candidate[range.clone()];
}
});
});
115 changes: 115 additions & 0 deletions src/algos/fzf/candidate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
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
initial_char_class: CharClass,

/// 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 {
self.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,
initial_char_class: CharClass,
opts: CandidateOpts,
) -> Self {
let bonuses = bonus_slab.alloc(base.char_len());
Self { base, bonuses, initial_char_class, opts }
}
}
Loading

0 comments on commit d09beb2

Please sign in to comment.