aboutsummaryrefslogtreecommitdiff
path: root/src/index.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/index.rs')
-rw-r--r--src/index.rs21
1 files changed, 16 insertions, 5 deletions
diff --git a/src/index.rs b/src/index.rs
index cc86f0c..be23e0e 100644
--- a/src/index.rs
+++ b/src/index.rs
@@ -16,7 +16,7 @@ use crate::vector;
/// or read from a file.
#[derive(Clone, Debug)]
pub struct Index {
- filecache : Vec<FileCache>,
+ pub filecache : Vec<FileCache>,
}
impl Default for Index {
@@ -69,7 +69,7 @@ impl Index {
if content.is_empty() {
result_tx.send(FileCache {
- path: "".to_string(),
+ path: "[is_empty]".to_string(),
vector : FileVector::default()
}).ok();
continue;
@@ -78,7 +78,7 @@ impl Index {
let words : Vec<String> = splitter::split_to_words(content);
let fv = FileVector::from_words(words);
result_tx.send(FileCache {
- path: "".to_string(),
+ path: path.to_string(),
vector : fv
}).ok();
}
@@ -128,7 +128,7 @@ impl Index {
Self { filecache }
}
- pub fn search(&self, search_args : Vec<String>) -> Vec<SearchResult> {
+ pub fn search(&self, search_args : Vec<String>, callback : impl Fn(u8)) -> Vec<SearchResult> {
let mut v : HashMap<Indexer, Count> = HashMap::new();
let mut opt : HashMap<Indexer, Count> = HashMap::new();
@@ -149,13 +149,20 @@ impl Index {
let mut results : Vec<SearchResult> = Vec::new();
- for filecache in self.filecache.iter() {
+ let mut last_p = 0;
+ for (i, filecache) in self.filecache.iter().enumerate() {
let mut r = SearchResult { priority : 0, path : filecache.path.clone() };
r.priority = vector::match_vector(&v, &filecache.vector);
if r.priority > 0 {
r.priority += vector::scalar_product(&opt, &filecache.vector);
results.push(r);
}
+
+ let p = i * 100 / self.filecache.len();
+ if last_p < p {
+ callback(p as u8);
+ last_p = p;
+ }
}
results.sort_by(|a, b| b.priority.cmp(&a.priority));
results
@@ -170,4 +177,8 @@ impl Index {
pub fn num_files(&self) -> usize {
self.filecache.len()
}
+
+ pub fn import(&mut self, index : Index) {
+ self.filecache = index.filecache;
+ }
}