fix lintting

This commit is contained in:
Jean-Marie Mineau 2023-02-26 12:43:14 +01:00 committed by Jean-Marie Mineau
parent 323e77b4e4
commit c4442e2741
2 changed files with 36 additions and 30 deletions

View file

@ -20,7 +20,7 @@ impl Dict {
let words: std::io::Result<Vec<_>> = file let words: std::io::Result<Vec<_>> = file
.lines() .lines()
.enumerate() .enumerate()
.filter_map(|(i, e)| (i != 0).then(|| e)) // Skip the first line ( = to size of the dict) .filter_map(|(i, e)| (i != 0).then_some(e)) // Skip the first line ( = to size of the dict)
.into_iter() .into_iter()
.collect(); .collect();
let words = words?; let words = words?;
@ -76,8 +76,8 @@ impl From<&Dict> for AnagramDict {
fn from(dict: &Dict) -> Self { fn from(dict: &Dict) -> Self {
let mut map = Self { let mut map = Self {
map: HashMap::<FrequencyHash, Vec<String>>::new(), map: HashMap::<FrequencyHash, Vec<String>>::new(),
nb_wild_cards: dict.nb_wild_cards.clone(), nb_wild_cards: dict.nb_wild_cards,
wild_card_char: dict.wild_card_char.clone(), wild_card_char: dict.wild_card_char,
letters: HashSet::<char>::new(), letters: HashSet::<char>::new(),
}; };
@ -87,15 +87,16 @@ impl From<&Dict> for AnagramDict {
let len = dict.words.len(); let len = dict.words.len();
for word in dict.words.iter() { for word in dict.words.iter() {
let freq = FrequencyHash::compute(word); let freq = FrequencyHash::compute(word);
for char_ in word.chars() { word.chars().for_each(|char_| {
if char_ != map.wild_card_char { if char_ != map.wild_card_char {
map.letters.insert(char_); map.letters.insert(char_);
} }
} });
map.add_word_with_wild_card(freq, word, dict.nb_wild_cards, dict.wild_card_char); map.add_word_with_wild_card(freq, word, dict.nb_wild_cards, dict.wild_card_char);
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
{ {
// Show avancement when running with a stding available
i += 1; i += 1;
print!("{}%\r", (100 * i) / len); print!("{}%\r", (100 * i) / len);
io::stdout().flush().unwrap(); io::stdout().flush().unwrap();
@ -116,15 +117,15 @@ impl AnagramDict {
if freq.get_freq(self.wild_card_char) > self.nb_wild_cards { if freq.get_freq(self.wild_card_char) > self.nb_wild_cards {
let mut result = vec![]; let mut result = vec![];
freq.remove_one_char(self.wild_card_char); freq.remove_one_char(self.wild_card_char);
for char_ in self.letters.iter() { self.letters.iter().for_each(|char_| {
let mut freq = freq.clone(); let mut freq = freq.clone();
freq.add_one_char(char_.clone()); freq.add_one_char(*char_);
if let Some(anagrams) = self.find_freq(freq) { if let Some(anagrams) = self.find_freq(freq) {
for anagram in anagrams { anagrams
result.push(anagram); .iter()
} .for_each(|anagram| result.push(anagram.clone()));
}
} }
});
if result.is_empty() { if result.is_empty() {
None None
} else { } else {
@ -145,11 +146,11 @@ impl AnagramDict {
self.map self.map
.entry(freq.clone()) .entry(freq.clone())
.and_modify(|anagrams| anagrams.push(word.to_string())) .and_modify(|anagrams| anagrams.push(word.to_string()))
.or_insert(vec![word.to_string()]); .or_insert_with(|| vec![word.to_string()]);
if nb_wild_card != 0 { if nb_wild_card != 0 {
for char_ in freq.0.clone().keys() { for char_ in freq.0.keys() {
let mut freq = freq.clone(); let mut freq = freq.clone();
freq.remove_one_char(char_.clone()); freq.remove_one_char(*char_);
freq.add_one_char(wild_card_symbole); freq.add_one_char(wild_card_symbole);
self.add_word_with_wild_card(freq, word, nb_wild_card - 1, wild_card_symbole); self.add_word_with_wild_card(freq, word, nb_wild_card - 1, wild_card_symbole);
} }
@ -164,18 +165,18 @@ impl AnagramDict {
/// We use the [`char`] type for simplicity. The initial problem was in ascii /// We use the [`char`] type for simplicity. The initial problem was in ascii
/// anyway, but keep in mind that [`char`]s are utf-8 scalar values, and in /// anyway, but keep in mind that [`char`]s are utf-8 scalar values, and in
/// some alphabet those do not match characters. /// some alphabet those do not match characters.
#[derive(Debug, PartialEq, Eq, Clone)] #[derive(Debug, Eq, Clone)]
struct FrequencyHash(HashMap<char, u8>); struct FrequencyHash(HashMap<char, u8>);
impl FrequencyHash { impl FrequencyHash {
/// Compute the [`FrequencyHash`] of a slice. /// Compute the [`FrequencyHash`] of a slice.
fn compute(string: &str) -> Self { fn compute(string: &str) -> Self {
let mut map = HashMap::<char, u8>::new(); let mut map = HashMap::<char, u8>::new();
for char_ in string.chars() { string.chars().for_each(|char_| {
map.entry(char_) map.entry(char_)
.and_modify(|counter| *counter += 1) .and_modify(|counter| *counter += 1)
.or_insert(1); .or_insert(1);
} });
Self(map) Self(map)
} }
@ -209,7 +210,7 @@ impl FrequencyHash {
/// Return the number of occurence of a char in the word /// Return the number of occurence of a char in the word
fn get_freq(&self, char_: char) -> u8 { fn get_freq(&self, char_: char) -> u8 {
if let Some(freq) = self.0.get(&char_) { if let Some(freq) = self.0.get(&char_) {
freq.clone() *freq
} else { } else {
0 0
} }
@ -221,3 +222,9 @@ impl Hash for FrequencyHash {
self.get_unique_vec().hash(state); self.get_unique_vec().hash(state);
} }
} }
impl PartialEq for FrequencyHash {
fn eq(&self, other: &Self) -> bool {
self.get_unique_vec() == other.get_unique_vec()
}
}

View file

@ -71,19 +71,18 @@ fn update_letters() {
while let Some(child) = result.first_element_child() { while let Some(child) = result.first_element_child() {
child.remove(); child.remove();
} }
DICTIONNARY if let Some(anagrms) = DICTIONNARY
.lock() .lock()
.expect("Failed to access DICTIONNARY") .expect("Failed to access DICTIONNARY")
.as_ref() .as_ref()
.map(|dict| dict.find(&val)) .and_then(|dict| dict.find(&val))
.flatten() {
.map(|anagrms| {
for anagram in anagrms { for anagram in anagrms {
let val = document().create_element("li").unwrap(); let val = document().create_element("li").unwrap();
val.set_text_content(Some(&anagram)); val.set_text_content(Some(&anagram));
result.append_child(&val).unwrap(); result.append_child(&val).unwrap();
} }
}); };
} }
fn load_dict(text: JsValue) { fn load_dict(text: JsValue) {
@ -113,8 +112,8 @@ fn load_dict(text: JsValue) {
// Called when the wasm module is instantiated // Called when the wasm module is instantiated
#[wasm_bindgen(start)] #[wasm_bindgen(start)]
fn main() -> Result<(), JsValue> { fn main() -> Result<(), JsValue> {
let load_dict_c1 = Closure::<dyn FnMut(_)>::new(move |text: JsValue| load_dict(text)); let load_dict_c1 = Closure::<dyn FnMut(_)>::new(load_dict);
let load_dict_c2 = Closure::<dyn FnMut(_)>::new(move |text: JsValue| load_dict(text)); let load_dict_c2 = Closure::<dyn FnMut(_)>::new(load_dict);
let update_dict = Closure::<dyn FnMut(_)>::new(move |event: Event| { let update_dict = Closure::<dyn FnMut(_)>::new(move |event: Event| {
display_spinner(); display_spinner();