Mejoras de rendimiento
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#[inline]
|
||||
pub fn fzs(pattern: &str, list: &mut [String]) {
|
||||
list.sort_by_key(|x| distance(pattern, x));
|
||||
pub fn fzs(pattern: &str, list: &mut Vec<String>, n: usize) {
|
||||
// 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 {
|
||||
@@ -30,3 +31,32 @@ fn distance(pattern: &str, text: &str) -> usize {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ pub fn test(
|
||||
list: Vec<String>,
|
||||
input: Arc<RwLock<(Vec<char>, String)>>,
|
||||
result: Arc<RwLock<Vec<String>>>,
|
||||
n: usize,
|
||||
) -> JoinHandle<()> {
|
||||
// let list = [
|
||||
// "pear",
|
||||
@@ -33,7 +34,7 @@ pub fn test(
|
||||
thread::spawn(move || {
|
||||
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;
|
||||
})
|
||||
}
|
||||
|
||||
@@ -48,27 +48,42 @@ pub fn run(path: &str, recursive: usize) {
|
||||
let cursor: Arc<RwLock<usize>> = Arc::new(RwLock::new(0));
|
||||
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());
|
||||
// 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 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() {
|
||||
let input_buff = input.read().unwrap().0.clone();
|
||||
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;
|
||||
execute!(stdout(), MoveTo(2, 3), Print(loading_message.clone())).unwrap();
|
||||
}
|
||||
|
||||
if processing && search_thread.is_finished() {
|
||||
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())]
|
||||
.iter()
|
||||
.enumerate()
|
||||
@@ -78,18 +93,21 @@ pub fn run(path: &str, recursive: usize) {
|
||||
queue!(
|
||||
stdout(),
|
||||
MoveTo(2, 3 + i as u16),
|
||||
Print(format!("{}{}", res, spaces))
|
||||
Print(format!("{}{}", res, spaces)),
|
||||
// MoveTo(2, 3),
|
||||
// Print(format!("{} ", res_list.len())),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
queue!(stdout(), Show).unwrap();
|
||||
processing = false;
|
||||
}
|
||||
|
||||
queue!(
|
||||
stdout(),
|
||||
MoveTo(2, 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();
|
||||
|
||||
@@ -142,8 +160,8 @@ fn print_frame(width: usize, height: usize) {
|
||||
execute!(stdout()).unwrap();
|
||||
}
|
||||
|
||||
fn get_files(list: Vec<String>, path: &str, recursive: usize) -> Vec<String> {
|
||||
let mut list2 = list.clone();
|
||||
fn get_files(list: &mut Vec<String>, path: &str, recursive: usize) {
|
||||
// let mut list2 = list.clone();
|
||||
|
||||
let path_format = if path == "." {
|
||||
"".to_string()
|
||||
@@ -152,9 +170,9 @@ fn get_files(list: Vec<String>, path: &str, recursive: usize) -> Vec<String> {
|
||||
};
|
||||
|
||||
if recursive < 1 {
|
||||
list2.append(
|
||||
&mut read_dir(path)
|
||||
.unwrap()
|
||||
if let Ok(file_list) = read_dir(path) {
|
||||
list.append(
|
||||
&mut file_list
|
||||
.map(|x| {
|
||||
format!(
|
||||
"{}{}",
|
||||
@@ -162,8 +180,9 @@ fn get_files(list: Vec<String>, path: &str, recursive: usize) -> Vec<String> {
|
||||
x.unwrap().file_name().to_str().unwrap()
|
||||
)
|
||||
})
|
||||
.collect::<Vec<String>>(),
|
||||
)
|
||||
.collect(),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
for e in read_dir(path)
|
||||
.unwrap()
|
||||
@@ -171,11 +190,13 @@ fn get_files(list: Vec<String>, path: &str, recursive: usize) -> Vec<String> {
|
||||
.collect::<Vec<String>>()
|
||||
{
|
||||
let e_path = format!("{}{}", path_format, e);
|
||||
list2.push(e_path.clone());
|
||||
if metadata(e_path.clone()).unwrap().is_dir() {
|
||||
list2 = get_files(list2, e_path.as_str().clone(), recursive - 1);
|
||||
list.push(e_path.clone());
|
||||
|
||||
if let Ok(met) = metadata(e_path.clone()) {
|
||||
if met.is_dir() {
|
||||
get_files(list, e_path.as_str().clone(), recursive - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
list2
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user