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"]
#![feature(test)]
#![cfg_attr(feature = "webassembly", no_std)]
#![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}};
#[cfg(not(feature = "webassembly"))]
use std::{cmp, iter, str, collections::BinaryHeap, f32};
#[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
}
}
#[derive(Clone, Debug, PartialEq)]
struct Candidate<'a> {
value: &'a str,
mask: CandidateBitmask,
}
impl<'a> Candidate<'a> {
fn from(value: &'a str) -> Candidate<'a> {
let mask = CandidateBitmask::from(&mut value.to_ascii_lowercase().chars());
Candidate { value, mask }
}
}
#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub struct SearchResult {
index: usize,
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 {
pub fn index(&self) -> usize {
self.index
}
pub fn score(&self) -> f32 {
self.score
}
}
pub struct MatcherParameters {
pub slash_bonus: f32,
pub separator_bonus: f32,
pub camelcase_bonus: f32,
pub period_bonus: f32,
pub max_gap: usize,
pub cache_size: usize,
pub distance_penalty: f32,
pub min_distance_penalty: f32,
pub cumulative_distance_penalty: f32,
}
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,
}
}
}
pub struct Matcher<'a> {
candidates: Vec<Candidate<'a>>,
parameters: MatcherParameters,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum MatcherError {
TextEncoding(str::Utf8Error),
}
impl<'a> Matcher<'a> {
pub fn new(dictionary: &str, parameters: MatcherParameters) -> Result<Matcher, MatcherError> {
let candidates = dictionary
.lines()
.map(|line| Candidate::from(line))
.collect();
Ok(Matcher {
candidates,
parameters,
})
}
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()
}
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;
let (query_char_index, query_char) = match query_chars.next() {
Some((idx, ch)) => (idx, ch),
None => return 1.0f32,
};
let candidate_start_index = match candidate_chars.peek() {
Some(&(idx, _)) => idx,
None => return 1.0f32,
};
let cache_offset = query_char_index * candidate_len + candidate_start_index;
if let Some(cached_score) = match_score_cache[cache_offset] {
return cached_score;
}
let mut best_match_index: Option<usize> = None;
let mut remaining_candidate_chars = self.parameters.max_gap;
let mut last_candidate_char: Option<char> = None;
let mut last_slash: Option<usize> = None;
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;
}
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};
#[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());
}
#[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)))
}
}