From 0a4755724c5e59fc9b497aeddaa54b03ff22c25b Mon Sep 17 00:00:00 2001 From: theteachr Date: Sun, 24 Dec 2023 02:17:58 +0530 Subject: [PATCH 1/3] IdioMaTIc RuST --- src/model.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/model.rs b/src/model.rs index 3a213dd..71a45c7 100644 --- a/src/model.rs +++ b/src/model.rs @@ -23,20 +23,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) { - *f -= 1; - } - } - } + self.docs.remove(file_path).into_iter().for_each(|mut doc| { + doc.tf + .keys() + .filter_map(|term| self.df.get_mut(term)) + .for_each(|f| *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)> { From d4269102000e68ec01388b19733ae6d976330a5c Mon Sep 17 00:00:00 2001 From: theteachr Date: Sun, 24 Dec 2023 03:48:57 +0530 Subject: [PATCH 2/3] Use the `Entry` API --- src/model.rs | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/src/model.rs b/src/model.rs index 71a45c7..e9ead10 100644 --- a/src/model.rs +++ b/src/model.rs @@ -61,23 +61,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(&t).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().for_each(|term| { + *self.df.entry(term).or_insert(0) += 1; + }); self.docs.insert(file_path, Doc {count, tf, last_modified}); } From 4c7daf03aa091b2f6eb441297c276ee3bf858e7d Mon Sep 17 00:00:00 2001 From: theteachr Date: Sun, 24 Dec 2023 04:39:09 +0530 Subject: [PATCH 3/3] Fix errors --- src/model.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/model.rs b/src/model.rs index e9ead10..f1f1b24 100644 --- a/src/model.rs +++ b/src/model.rs @@ -23,12 +23,13 @@ pub struct Model { impl Model { fn remove_document(&mut self, file_path: &Path) { - self.docs.remove(file_path).into_iter().for_each(|mut doc| { - doc.tf - .keys() - .filter_map(|term| self.df.get_mut(term)) - .for_each(|f| *f -= 1) - }) + if let Some(doc) = self.docs.remove(file_path) { + 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 { @@ -64,11 +65,11 @@ impl Model { let count = Lexer::new(content) .into_iter() .map(|term| { - *tf.entry(&t).or_insert(0) += 1; + *tf.entry(term).or_insert(0) += 1; }) .count(); - tf.keys().for_each(|term| { + tf.keys().cloned().for_each(|term| { *self.df.entry(term).or_insert(0) += 1; });