Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IdioMaTIc RuST #19

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 15 additions & 22 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ pub struct Model {
impl Model {
fn remove_document(&mut self, file_path: &Path) {
if let Some(doc) = self.docs.remove(file_path) {
for t in doc.tf.keys() {
if let Some(f) = self.df.get_mut(t) {
for term in doc.tf.keys() {
if let Some(f) = self.df.get_mut(term) {
*f -= 1;
}
}
}
}

pub fn requires_reindexing(&mut self, file_path: &Path, last_modified: SystemTime) -> bool {
if let Some(doc) = self.docs.get(file_path) {
return doc.last_modified < last_modified;
}
return true;
self.docs
.get(file_path)
.filter(|doc| doc.last_modified < last_modified)
.is_some()
}

pub fn search_query(&self, query: &[char]) -> Vec<(PathBuf, f32)> {
Expand All @@ -62,23 +62,16 @@ impl Model {

let mut tf = TermFreq::new();

let mut count = 0;
for t in Lexer::new(content) {
if let Some(f) = tf.get_mut(&t) {
*f += 1;
} else {
tf.insert(t, 1);
}
count += 1;
}
let count = Lexer::new(content)
.into_iter()
.map(|term| {
*tf.entry(term).or_insert(0) += 1;
})
.count();

for t in tf.keys() {
if let Some(f) = self.df.get_mut(t) {
*f += 1;
} else {
self.df.insert(t.to_string(), 1);
}
}
tf.keys().cloned().for_each(|term| {
*self.df.entry(term).or_insert(0) += 1;
});

self.docs.insert(file_path, Doc {count, tf, last_modified});
}
Expand Down