2022-08-13 14:09:48 +00:00
|
|
|
use super::matcher;
|
|
|
|
|
use super::thread_pool;
|
|
|
|
|
|
|
|
|
|
use std::sync::Arc;
|
2022-08-26 09:25:05 +00:00
|
|
|
use std::sync::Mutex;
|
2022-08-13 14:09:48 +00:00
|
|
|
|
|
|
|
|
pub struct Match {
|
|
|
|
|
pub score: i64,
|
|
|
|
|
pub content: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct Options {
|
|
|
|
|
pub pattern: String,
|
|
|
|
|
pub minimun_score: i64,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Options {
|
|
|
|
|
pub fn new(pattern: String) -> Self {
|
2022-08-26 09:25:05 +00:00
|
|
|
Self {
|
|
|
|
|
pattern,
|
|
|
|
|
minimun_score: 20,
|
|
|
|
|
}
|
2022-08-13 14:09:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn sort_strings(options: Options, strings: Vec<String>) -> Arc<Mutex<Vec<Match>>> {
|
|
|
|
|
let matches: Arc<Mutex<Vec<Match>>> = Arc::new(Mutex::new(Vec::new()));
|
2022-08-25 18:42:22 +00:00
|
|
|
let matcher = Arc::new(Mutex::new(matcher::Matcher::new(options.pattern)));
|
2022-08-13 14:09:48 +00:00
|
|
|
|
|
|
|
|
let pool = thread_pool::ThreadPool::new(std::thread::available_parallelism().unwrap().get());
|
|
|
|
|
|
|
|
|
|
for string in strings {
|
|
|
|
|
let thread_matcher = Arc::clone(&matcher);
|
|
|
|
|
let thread_matches = Arc::clone(&matches);
|
|
|
|
|
pool.execute(move || {
|
|
|
|
|
let score = thread_matcher.lock().unwrap().score(string.to_string());
|
|
|
|
|
if score > 25 {
|
|
|
|
|
let mut tmp = thread_matches.lock().unwrap();
|
|
|
|
|
let content = string.clone();
|
2022-08-26 09:25:05 +00:00
|
|
|
tmp.push(Match { score, content });
|
2022-08-13 14:09:48 +00:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
drop(pool);
|
|
|
|
|
|
2022-08-26 09:25:05 +00:00
|
|
|
matches
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.sort_by(|a, b| a.score.cmp(&b.score));
|
|
|
|
|
matches
|
2022-08-13 14:09:48 +00:00
|
|
|
}
|