rapidfuzz 0.1.0

rapid string matching library
Documentation
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
}