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
use crate::fuzzy; use rayon::prelude::*; use std::sync::atomic::{AtomicU8, Ordering}; pub fn extract_unsorted(query: &str, choices: &Vec<&str>) -> Vec<(u8, String)> { let result = choices.iter() .map(|choice| (fuzzy::ratio(&query, &choice, 0), choice.to_string())) .collect(); result } pub fn extract_unsorted_par(query: &str, choices: &Vec<&str>) -> Vec<(u8, String)> { let result = choices.par_iter() .map(|choice| (fuzzy::ratio(&query, &choice, 0), choice.to_string())) .collect(); result } pub fn extract_sorted(query: &str, choices: &Vec<&str>) -> Vec<(u8, String)> { let mut result: Vec<(u8, String)> = choices.iter() .map(|choice| (fuzzy::ratio(&query, &choice, 0), choice.to_string())) .collect(); result.sort_by(|(val_a, _), (val_b, _)| val_a.cmp(val_b)); result } pub fn extract_sorted_par(query: &str, choices: &Vec<&str>) -> Vec<(u8, String)> { let mut result: Vec<(u8, String)> = choices.par_iter() .map(|choice| (fuzzy::ratio(&query, &choice, 0), choice.to_string())) .collect(); result.sort_by(|(val_a, _), (val_b, _)| val_a.cmp(val_b)); result } pub fn extract_max_par(query: &str, choices: &Vec<&str>) -> Option<(u8, usize)> { let max_ratio = AtomicU8::new(0); let result = choices.par_iter() .enumerate() .map(|(pos, choice)| { let res = fuzzy::ratio(&query, choice, max_ratio.load(Ordering::Relaxed)); max_ratio.fetch_max(res, Ordering::SeqCst); (res, pos) }) .max_by(|(val_a, _), (val_b, _)| val_a.cmp(val_b)); result } pub fn extract_max(query: &str, choices: &Vec<&str>) -> Option<(u8, usize)> { let max_ratio = AtomicU8::new(0); let result = choices.iter() .enumerate() .map(|(pos, choice)| { let res = fuzzy::ratio(&query, choice, max_ratio.load(Ordering::Relaxed)); max_ratio.fetch_max(res, Ordering::SeqCst); (res, pos) }) .max_by(|(val_a, _), (val_b, _)| val_a.cmp(val_b)); result }