1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#![crate_name = "fiveo"]
#![crate_type = "lib"]
// Include the unstable testing library.
#![feature(test)]
// Don't build with the standard library when targeting WebAssembly.
#![cfg_attr(feature = "webassembly", no_std)]
// We want to disable the default allocator and rely on `wee_alloc` for allocations in WebAssembly builds.  We include the
// `alloc` crate for some extra core functionality.
#![cfg_attr(feature = "webassembly", feature(alloc, core_intrinsics, core_float))]
#[cfg(feature = "webassembly")]
extern crate alloc;
#[cfg(feature = "webassembly")]
use {alloc::{BinaryHeap, Vec}, core::{cmp, iter, str, f32, num::Float}};

// When we're not targetting webassembly, import the same types from libstd.
#[cfg(not(feature = "webassembly"))]
use std::{cmp, iter, str, collections::BinaryHeap, f32};

/// A 32-bit bitmask that maps the alphabet to the first 25 bits.
#[derive(Clone, Debug, PartialEq, Eq)]
struct CandidateBitmask(u32);

impl CandidateBitmask {
    pub fn from(values: &mut Iterator<Item = char>) -> Self {
        let mut bitmask = 0 as u32;

        loop {
            match values.next() {
                Some(val) if val.is_ascii() && val.is_alphabetic() => {
                    bitmask |= 1 << (val as u8 - 'a' as u8);
                }
                Some(_) => continue,
                None => break,
            }
        }

        CandidateBitmask(bitmask)
    }

    fn matches(&self, other: &Self) -> bool {
        (self.0 & other.0) == self.0
    }
}

/// A match candidate in a `Matcher`s dictionary.
#[derive(Clone, Debug, PartialEq)]
struct Candidate<'a> {
    /// The original value of this entry in the dictionary.
    value: &'a str,

    /// A bitmask used to perform quick `String::contains` operations for single characters.
    mask: CandidateBitmask,
}

impl<'a> Candidate<'a> {
    /// Create a new `Candidate` from the given string, creating a copy of it, normalizing to lowercase
    /// and generating a `CandidateBitmask`.
    fn from(value: &'a str) -> Candidate<'a> {
        let mask = CandidateBitmask::from(&mut value.to_ascii_lowercase().chars());

        Candidate { value, mask }
    }
}

/// An approximate search result from a `Search` created by a `Matcher`.
#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub struct SearchResult {
    /// A reference to the `Candidate` value this result came from.
    index: usize,
    /// A score of this result ranked against the query string.
    score: f32,
}

impl Eq for SearchResult {}

impl Ord for SearchResult {
    fn cmp(&self, other: &Self) -> cmp::Ordering {
        self.score.partial_cmp(&other.score).unwrap()
    }
}

impl SearchResult {
    /// Get a reference to the string value of this result.
    pub fn index(&self) -> usize {
        self.index
    }

    /// Get the score of this result.
    pub fn score(&self) -> f32 {
        self.score
    }
}

/// A set of parameters that can be tuned to change the scores assigned to certain matches.
pub struct MatcherParameters {
    /// The bonus for a matching character found after a slash.
    pub slash_bonus: f32,

    /// The bonus for a matching character found after a separator.
    pub separator_bonus: f32,

    /// The bonus for a matching character found as a separator of a CamelCase string.
    pub camelcase_bonus: f32,

    /// The bonus for a matching character found after a period.
    pub period_bonus: f32,

    /// The maximum gap between a search character and a match to be considered before moving to the next character.
    pub max_gap: usize,

    /// The size of the cache that will be allocated each search for memoizing score function calls.  Query/candidate pairs exceeding
    /// this size will only be matched using the simple matcher.
    pub cache_size: usize,

    /// The base distance penalty for matches occurring after proceeding without a successful match.
    pub distance_penalty: f32,

    /// The lowest value the distance penalty can decrease to.
    pub min_distance_penalty: f32,

    /// The increment that the distance penalty decreases in.
    pub cumulative_distance_penalty: f32,
}

/// Define a sane set of default `MatcherParameters` that adhere to the same parameters followed by Cmd-T.
///
/// The rules in these default parameters prefers slashes over separators, and camelcase / separators over periods, with a max gap of 10.
impl Default for MatcherParameters {
    fn default() -> Self {
        MatcherParameters {
            camelcase_bonus: 0.8f32,
            separator_bonus: 0.8f32,
            slash_bonus: 0.9f32,
            period_bonus: 0.7f32,
            max_gap: 10,
            cache_size: 2000,
            distance_penalty: 0.6f32,
            min_distance_penalty: 0.2f32 + f32::EPSILON,
            cumulative_distance_penalty: 0.05f32,
        }
    }
}

/// A fuzzy-search algorithm that uses bitmasks to perform fast string comparisons.
///
/// # Example:
///
/// ```rust
/// let searcher = fiveo::Matcher::new("my_word1\nmy_word2\n", fiveo::MatcherParameters::default()).unwrap();
/// // Search for "my_word1" and return a maximum of 10 results.
/// let matches = searcher.search("my_word1", 10);
/// ```
pub struct Matcher<'a> {
    /// A list of entries in this `Matcher`s dictionary.
    candidates: Vec<Candidate<'a>>,

    /// The parameters used to tune the scoring function.
    parameters: MatcherParameters,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum MatcherError {
    TextEncoding(str::Utf8Error),
}

impl<'a> Matcher<'a> {
    /// Create a new `Matcher` from the given input data.  The input data should contain
    /// a list of input entries delimited by newlines that a matching dictionary can be built from.
    pub fn new(dictionary: &str, parameters: MatcherParameters) -> Result<Matcher, MatcherError> {
        let candidates = dictionary
            .lines()
            .map(|line| Candidate::from(line))
            .collect();

        Ok(Matcher {
            candidates,
            parameters,
        })
    }

    /// Search for `Candidate`s that approximately match the given `query` string and return a
    /// list of `SearchResult`s.
    pub fn search(&self, query: &str, max_results: usize) -> Vec<SearchResult> {
        let query_lowercase = query.to_lowercase();
        let query_mask = CandidateBitmask::from(&mut query_lowercase.chars());
        let mut result_heap: BinaryHeap<SearchResult> = BinaryHeap::with_capacity(max_results);
        for (candidate_index, candidate) in self.candidates.iter().enumerate() {
            if !query_mask.matches(&candidate.mask) {
                continue;
            }

            let cache_len = query.len() * candidate.value.len();
            let mut match_idx_cache = Vec::with_capacity(cache_len);
            let mut match_score_cache = Vec::with_capacity(cache_len);
            match_idx_cache.resize(cache_len, None);
            match_score_cache.resize(cache_len, None);

            let score = self.score_candidate(
                query,
                candidate,
                &mut match_idx_cache,
                &mut match_score_cache,
            );

            if score > 0.0f32 {
                let is_higher_scoring = result_heap
                    .peek()
                    .map(|rs| score > rs.score())
                    .unwrap_or(false);

                if is_higher_scoring || result_heap.len() < max_results {
                    result_heap.push(SearchResult {
                        index: candidate_index,
                        score,
                    })
                }

                if result_heap.capacity() > max_results {
                    result_heap.pop();
                }
            }
        }

        result_heap.into_sorted_vec()
    }

    /// Score the given `candidate` against the search query`.  Use the `match_idx_cache_` and `match_score_cache` to memoize calls
    /// to the `score_candidate_recursive` fn.
    fn score_candidate(
        &self,
        query: &str,
        candidate: &Candidate,
        match_idx_cache: &mut Vec<Option<usize>>,
        match_score_cache: &mut Vec<Option<f32>>,
    ) -> f32 {
        let query_len = query.len();
        let candidate_len = candidate.value.len();
        let score = query_len as f32
            * self.score_candidate_recursive(
                &mut query.char_indices().peekable(),
                query_len,
                &mut candidate.value.char_indices().peekable(),
                candidate_len,
                match_idx_cache,
                match_score_cache,
            );

        score.max(0.)
    }

    fn score_candidate_recursive(
        &self,
        query_chars: &mut iter::Peekable<str::CharIndices>,
        query_len: usize,
        candidate_chars: &mut iter::Peekable<str::CharIndices>,
        candidate_len: usize,
        match_idx_cache: &mut Vec<Option<usize>>,
        match_score_cache: &mut Vec<Option<f32>>,
    ) -> f32 {
        let mut score = 0.0f32;

        // Return a score of 0 if there are no characters remaining to compare.
        let (query_char_index, query_char) = match query_chars.next() {
            Some((idx, ch)) => (idx, ch),
            None => return 1.0f32,
        };

        // Peek the next character so we can calculate the distance bonus and store an entry in the matches/memo caches
        // for this call.
        let candidate_start_index = match candidate_chars.peek() {
            Some(&(idx, _)) => idx,
            None => return 1.0f32,
        };

        // Calculate the position in the memo/best match caches.
        let cache_offset = query_char_index * candidate_len + candidate_start_index;

        if let Some(cached_score) = match_score_cache[cache_offset] {
            return cached_score;
        }

        // The position of the best match.
        let mut best_match_index: Option<usize> = None;

        // The remaining number of characters to process before the gap is considered too large.
        let mut remaining_candidate_chars = self.parameters.max_gap;

        // The last character that was checked in the candidate search string.
        let mut last_candidate_char: Option<char> = None;

        // Position of the last back/forwards slash that was found.
        let mut last_slash: Option<usize> = None;

        // The growing distance penalty.
        let mut distance_penalty = self.parameters.distance_penalty;

        while let Some((candidate_char_index, candidate_char)) = candidate_chars.next() {
            if remaining_candidate_chars == 0 {
                break;
            }

            if query_char_index == 0 && (query_char == '\\' || query_char == '/') {
                last_slash = Some(candidate_char_index);
            }

            if query_char == candidate_char
                || query_char.to_lowercase().eq(candidate_char.to_lowercase())
            {
                let query_char_score = if candidate_char_index == candidate_start_index {
                    1.0f32
                } else {
                    match last_candidate_char {
                        Some(val) => match val {
                            '/' => self.parameters.slash_bonus,
                            '-' | '_' | ' ' | '0'...'9' => self.parameters.separator_bonus,
                            'a'...'z' if candidate_char.is_uppercase() => {
                                self.parameters.camelcase_bonus
                            }
                            '.' => self.parameters.period_bonus,
                            _ => distance_penalty,
                        },
                        _ => distance_penalty,
                    }
                };

                if query_char_index > 0 && distance_penalty > self.parameters.min_distance_penalty {
                    distance_penalty -= self.parameters.cumulative_distance_penalty;
                }

                let mut new_score = query_char_score
                    * self.score_candidate_recursive(
                        query_chars,
                        query_len,
                        candidate_chars,
                        candidate_len,
                        match_idx_cache,
                        match_score_cache,
                    );

                if query_char_index == 0 {
                    new_score /= (candidate_len - last_slash.unwrap_or(0)) as f32;
                }

                if new_score > score {
                    score = new_score;
                    best_match_index = Some(candidate_char_index);

                    if f32::abs(score - 1.0f32) < f32::EPSILON {
                        break;
                    }
                }
            }

            last_candidate_char = Some(candidate_char);
            remaining_candidate_chars -= 1;
        }

        // Store the results in the cache so we don't have to run this computation again during this search.
        match_score_cache[cache_offset] = Some(score);
        match_idx_cache[cache_offset] = best_match_index;

        score
    }
}

#[cfg(test)]
mod tests {
    extern crate test;

    use super::*;
    use self::test::{black_box, Bencher};

    /// Exact matches should always return a perfect score of 1.0f32 and take precedence over any other matches.
    #[test]
    pub fn exact_match_has_perfect_score() {
        let searcher = Matcher::new("my_word1", MatcherParameters::default()).unwrap();
        let results = searcher.search("my_word1", 1);

        assert_eq!(1.0f32, results[0].score());
    }

    /// A benchmark that searches through a list of 13k files from the Unreal Engine 4 source code.
    #[bench]
    fn unreal_engine_search(b: &mut Bencher) {
        let searcher = Matcher::new(
            include_str!("../benchmark_data/ue4_file_list.txt"),
            MatcherParameters::default(),
        ).unwrap();

        b.iter(|| black_box(searcher.search("file.cpp", 100)))
    }
}