Mejoras de rendimiento

This commit is contained in:
=
2024-01-27 22:44:39 +01:00
parent cf7dae67ba
commit 742f941751
3 changed files with 83 additions and 31 deletions

View File

@@ -1,6 +1,7 @@
#[inline] #[inline]
pub fn fzs(pattern: &str, list: &mut [String]) { pub fn fzs(pattern: &str, list: &mut Vec<String>, n: usize) {
list.sort_by_key(|x| distance(pattern, x)); // list.sort_by_key(|x| distance(pattern, x));
*list = sort_n_first(list, n, |x| distance(pattern, x.as_str()));
} }
fn distance(pattern: &str, text: &str) -> usize { fn distance(pattern: &str, text: &str) -> usize {
@@ -30,3 +31,32 @@ fn distance(pattern: &str, text: &str) -> usize {
matrix[0][0] matrix[0][0]
} }
fn sort_n_first<F>(list: &mut [String], n: usize, mut key: F) -> Vec<String>
where
F: FnMut(String) -> usize,
{
let mut result = vec![String::from(""); n];
let mut values = vec![0; n];
for e in list {
let e_val = key(e.clone());
for (i, other) in result.clone().iter().enumerate() {
if other.is_empty() {
result[i] = e.to_string();
values[i] = e_val;
break;
}
if e_val < values[i] {
result.insert(i, e.to_string());
result.truncate(n);
values.insert(i, e_val);
values.truncate(n);
break;
}
}
}
// result.reverse();
result
}

View File

@@ -12,6 +12,7 @@ pub fn test(
list: Vec<String>, list: Vec<String>,
input: Arc<RwLock<(Vec<char>, String)>>, input: Arc<RwLock<(Vec<char>, String)>>,
result: Arc<RwLock<Vec<String>>>, result: Arc<RwLock<Vec<String>>>,
n: usize,
) -> JoinHandle<()> { ) -> JoinHandle<()> {
// let list = [ // let list = [
// "pear", // "pear",
@@ -33,7 +34,7 @@ pub fn test(
thread::spawn(move || { thread::spawn(move || {
let pattern = String::from_iter(input.read().unwrap().0.clone()); let pattern = String::from_iter(input.read().unwrap().0.clone());
fzs(pattern.as_str(), &mut list2); fzs(pattern.as_str(), &mut list2, n);
*result.write().unwrap() = list2; *result.write().unwrap() = list2;
}) })
} }

View File

@@ -48,27 +48,42 @@ pub fn run(path: &str, recursive: usize) {
let cursor: Arc<RwLock<usize>> = Arc::new(RwLock::new(0)); let cursor: Arc<RwLock<usize>> = Arc::new(RwLock::new(0));
let results: Arc<RwLock<Vec<String>>> = Arc::new(RwLock::new(Vec::new())); let results: Arc<RwLock<Vec<String>>> = Arc::new(RwLock::new(Vec::new()));
start_get_input(size.0 as usize, input.clone(), cont.clone(), cursor.clone()); start_get_input(size.0 as usize, input.clone(), cont.clone(), cursor.clone());
// let list = read_dir(path)
// .unwrap()
// .map(|x| x.unwrap().file_name().to_str().unwrap().to_string())
// .collect::<Vec<String>>();
let list = get_files(Vec::new(), path, recursive); execute!(stdout(), MoveTo(2, 3), Print("Reading files...")).unwrap();
let mut list = Vec::new();
get_files(&mut list, path, recursive);
execute!(stdout(), MoveTo(2, 3), Print(" ")).unwrap();
let mut search_thread = test(list.clone(), input.clone(), results.clone()); let mut search_thread = test(
list.clone(),
input.clone(),
results.clone(),
size.1 as usize - 4,
);
let mut processing = false; let mut processing = false;
let mut last_input = String::new(); let mut last_input = String::new();
let mut loading_message = String::from("Loading...");
loading_message.push_str(String::from_iter(vec![' '; size.0 as usize - (4 + 10)]).as_str());
let clear_loading = String::from_iter(vec![' '; size.0 as usize - 4]);
while cont.read().unwrap().to_owned() { while cont.read().unwrap().to_owned() {
let input_buff = input.read().unwrap().0.clone(); let input_buff = input.read().unwrap().0.clone();
if String::from_iter(input.read().unwrap().clone().0).as_str() != last_input.as_str() { if String::from_iter(input.read().unwrap().clone().0).as_str() != last_input.as_str() {
search_thread = test(list.clone(), input.clone(), results.clone()); search_thread = test(
list.clone(),
input.clone(),
results.clone(),
size.1 as usize - 4,
);
processing = true; processing = true;
execute!(stdout(), MoveTo(2, 3), Print(loading_message.clone())).unwrap();
} }
if processing && search_thread.is_finished() { if processing && search_thread.is_finished() {
let res_list = results.read().unwrap(); let res_list = results.read().unwrap();
queue!(stdout(), Hide).unwrap(); execute!(stdout(), Hide, MoveTo(2, 3), Print(clear_loading.clone())).unwrap();
for (i, res) in res_list[0..(size.1 as usize - 4).min(res_list.len())] for (i, res) in res_list[0..(size.1 as usize - 4).min(res_list.len())]
.iter() .iter()
.enumerate() .enumerate()
@@ -78,18 +93,21 @@ pub fn run(path: &str, recursive: usize) {
queue!( queue!(
stdout(), stdout(),
MoveTo(2, 3 + i as u16), MoveTo(2, 3 + i as u16),
Print(format!("{}{}", res, spaces)) Print(format!("{}{}", res, spaces)),
// MoveTo(2, 3),
// Print(format!("{} ", res_list.len())),
) )
.unwrap(); .unwrap();
} }
queue!(stdout(), Show).unwrap(); queue!(stdout(), Show).unwrap();
processing = false;
} }
queue!( queue!(
stdout(), stdout(),
MoveTo(2, 1), MoveTo(2, 1),
Print(input.read().unwrap().clone().1), Print(input.read().unwrap().clone().1),
MoveTo(cursor.read().unwrap().to_owned() as u16 + 2, 1) MoveTo(cursor.read().unwrap().to_owned() as u16 + 2, 1),
) )
.unwrap(); .unwrap();
@@ -142,8 +160,8 @@ fn print_frame(width: usize, height: usize) {
execute!(stdout()).unwrap(); execute!(stdout()).unwrap();
} }
fn get_files(list: Vec<String>, path: &str, recursive: usize) -> Vec<String> { fn get_files(list: &mut Vec<String>, path: &str, recursive: usize) {
let mut list2 = list.clone(); // let mut list2 = list.clone();
let path_format = if path == "." { let path_format = if path == "." {
"".to_string() "".to_string()
@@ -152,18 +170,19 @@ fn get_files(list: Vec<String>, path: &str, recursive: usize) -> Vec<String> {
}; };
if recursive < 1 { if recursive < 1 {
list2.append( if let Ok(file_list) = read_dir(path) {
&mut read_dir(path) list.append(
.unwrap() &mut file_list
.map(|x| { .map(|x| {
format!( format!(
"{}{}", "{}{}",
path_format, path_format,
x.unwrap().file_name().to_str().unwrap() x.unwrap().file_name().to_str().unwrap()
) )
}) })
.collect::<Vec<String>>(), .collect(),
) );
}
} else { } else {
for e in read_dir(path) for e in read_dir(path)
.unwrap() .unwrap()
@@ -171,11 +190,13 @@ fn get_files(list: Vec<String>, path: &str, recursive: usize) -> Vec<String> {
.collect::<Vec<String>>() .collect::<Vec<String>>()
{ {
let e_path = format!("{}{}", path_format, e); let e_path = format!("{}{}", path_format, e);
list2.push(e_path.clone()); list.push(e_path.clone());
if metadata(e_path.clone()).unwrap().is_dir() {
list2 = get_files(list2, e_path.as_str().clone(), recursive - 1); if let Ok(met) = metadata(e_path.clone()) {
if met.is_dir() {
get_files(list, e_path.as_str().clone(), recursive - 1);
}
} }
} }
} }
list2
} }