From fbc20a77e9f86313679e2c81f4f3bdaf4b0d63aa Mon Sep 17 00:00:00 2001 From: Arjun31415 Date: Sun, 2 Oct 2022 14:42:31 +0530 Subject: [PATCH] Fix clippy warnings (#396) --- src/data_structures/b_tree.rs | 9 ++++----- src/data_structures/linked_list.rs | 8 ++++---- src/dynamic_programming/edit_distance.rs | 2 +- src/general/huffman_encoding.rs | 7 +++---- src/graph/floyd_warshall.rs | 6 ++---- src/graph/prufer_code.rs | 2 +- 6 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/data_structures/b_tree.rs b/src/data_structures/b_tree.rs index a17a85215d3..22c8c28f0aa 100644 --- a/src/data_structures/b_tree.rs +++ b/src/data_structures/b_tree.rs @@ -100,19 +100,18 @@ impl BTreeProps { self.insert_non_full(&mut node.children[u_index], key); } } - - fn traverse_node(&self, node: &Node, depth: usize) { + fn traverse_node(node: &Node, depth: usize) { if node.is_leaf() { print!(" {0:{<1$}{2:?}{0:}<1$} ", "", depth, node.keys); } else { let _depth = depth + 1; for (index, key) in node.keys.iter().enumerate() { - self.traverse_node(&node.children[index], _depth); + Self::traverse_node(&node.children[index], _depth); // Check https://doc.rust-lang.org/std/fmt/index.html // And https://stackoverflow.com/a/35280799/2849127 print!("{0:{<1$}{2:?}{0:}<1$}", "", depth, key); } - self.traverse_node(node.children.last().unwrap(), _depth); + Self::traverse_node(node.children.last().unwrap(), _depth); } } } @@ -141,7 +140,7 @@ where } pub fn traverse(&self) { - self.props.traverse_node(&self.root, 0); + BTreeProps::traverse_node(&self.root, 0); println!(); } diff --git a/src/data_structures/linked_list.rs b/src/data_structures/linked_list.rs index c87d2953592..1c6b84a7a6c 100644 --- a/src/data_structures/linked_list.rs +++ b/src/data_structures/linked_list.rs @@ -178,16 +178,16 @@ impl LinkedList { } } - pub fn get(&mut self, index: i32) -> Option<&T> { - self.get_ith_node(self.head, index) + pub fn get(&mut self, index: i32) -> Option<&'static T> { + Self::get_ith_node(self.head, index) } - fn get_ith_node(&mut self, node: Option>>, index: i32) -> Option<&T> { + fn get_ith_node(node: Option>>, index: i32) -> Option<&'static T> { match node { None => None, Some(next_ptr) => match index { 0 => Some(unsafe { &(*next_ptr.as_ptr()).val }), - _ => self.get_ith_node(unsafe { (*next_ptr.as_ptr()).next }, index - 1), + _ => Self::get_ith_node(unsafe { (*next_ptr.as_ptr()).next }, index - 1), }, } } diff --git a/src/dynamic_programming/edit_distance.rs b/src/dynamic_programming/edit_distance.rs index 913d58c87ed..1bce3045618 100644 --- a/src/dynamic_programming/edit_distance.rs +++ b/src/dynamic_programming/edit_distance.rs @@ -72,7 +72,7 @@ pub fn edit_distance_se(str_a: &str, str_b: &str) -> u32 { // c is distances[i][j-1] and s is distances[i-1][j-1] at the beginning of each round of iteration char_b = str_b[j - 1]; c = min( - s + if char_a == char_b { 0 } else { 1 }, + s + u32::from(char_a != char_b), min(c + 1, distances[j] + 1), ); // c is updated to distances[i][j], and will thus become distances[i][j-1] for the next cell diff --git a/src/general/huffman_encoding.rs b/src/general/huffman_encoding.rs index 5332592446a..8212ddc8240 100644 --- a/src/general/huffman_encoding.rs +++ b/src/general/huffman_encoding.rs @@ -43,7 +43,6 @@ impl Ord for HuffmanNode { impl HuffmanNode { /// Turn the tree into the map that can be used in encoding pub fn get_alphabet( - &self, height: u32, path: u64, node: &HuffmanNode, @@ -60,8 +59,8 @@ impl HuffmanNode { ); } None => { - self.get_alphabet(height + 1, path, node.left.as_ref().unwrap(), map); - self.get_alphabet( + Self::get_alphabet(height + 1, path, node.left.as_ref().unwrap(), map); + Self::get_alphabet( height + 1, path | (1 << height), node.right.as_ref().unwrap(), @@ -103,7 +102,7 @@ impl HuffmanDictionary { }); } let root = queue.pop().unwrap(); - root.get_alphabet(0, 0, &root, &mut alph); + HuffmanNode::get_alphabet(0, 0, &root, &mut alph); HuffmanDictionary { alphabet: alph, root, diff --git a/src/graph/floyd_warshall.rs b/src/graph/floyd_warshall.rs index 7f43fbb4f09..36d1a7a41cc 100644 --- a/src/graph/floyd_warshall.rs +++ b/src/graph/floyd_warshall.rs @@ -30,7 +30,7 @@ pub fn floyd_warshall>( }); } } - let keys = map.iter().map(|(k, _)| *k).collect::>(); + let keys = map.keys().copied().collect::>(); for &k in &keys { for &i in &keys { if map[&i].get(&k).is_none() { @@ -49,9 +49,7 @@ pub fn floyd_warshall>( match entry_i_j { Some(&e) => { if e > entry_i_k + entry_k_j { - map.entry(i) - .or_insert(BTreeMap::new()) - .insert(j, entry_i_k + entry_k_j); + map.entry(i).or_default().insert(j, entry_i_k + entry_k_j); } } None => { diff --git a/src/graph/prufer_code.rs b/src/graph/prufer_code.rs index 5478fbaf441..bfa264e72e6 100644 --- a/src/graph/prufer_code.rs +++ b/src/graph/prufer_code.rs @@ -33,7 +33,7 @@ pub fn prufer_encode(tree: &Graph) -> Vec { #[inline] fn add_directed_edge(tree: &mut Graph, a: V, b: V) { - tree.entry(a).or_insert(vec![]).push(b); + tree.entry(a).or_default().push(b); } #[inline]