Skip to content

Commit fbc20a7

Browse files
authored
Fix clippy warnings (TheAlgorithms#396)
1 parent bf8b17a commit fbc20a7

File tree

6 files changed

+15
-19
lines changed

6 files changed

+15
-19
lines changed

src/data_structures/b_tree.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,18 @@ impl BTreeProps {
100100
self.insert_non_full(&mut node.children[u_index], key);
101101
}
102102
}
103-
104-
fn traverse_node<T: Ord + Debug>(&self, node: &Node<T>, depth: usize) {
103+
fn traverse_node<T: Ord + Debug>(node: &Node<T>, depth: usize) {
105104
if node.is_leaf() {
106105
print!(" {0:{<1$}{2:?}{0:}<1$} ", "", depth, node.keys);
107106
} else {
108107
let _depth = depth + 1;
109108
for (index, key) in node.keys.iter().enumerate() {
110-
self.traverse_node(&node.children[index], _depth);
109+
Self::traverse_node(&node.children[index], _depth);
111110
// Check https://doc.rust-lang.org/std/fmt/index.html
112111
// And https://stackoverflow.com/a/35280799/2849127
113112
print!("{0:{<1$}{2:?}{0:}<1$}", "", depth, key);
114113
}
115-
self.traverse_node(node.children.last().unwrap(), _depth);
114+
Self::traverse_node(node.children.last().unwrap(), _depth);
116115
}
117116
}
118117
}
@@ -141,7 +140,7 @@ where
141140
}
142141

143142
pub fn traverse(&self) {
144-
self.props.traverse_node(&self.root, 0);
143+
BTreeProps::traverse_node(&self.root, 0);
145144
println!();
146145
}
147146

src/data_structures/linked_list.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,16 @@ impl<T> LinkedList<T> {
178178
}
179179
}
180180

181-
pub fn get(&mut self, index: i32) -> Option<&T> {
182-
self.get_ith_node(self.head, index)
181+
pub fn get(&mut self, index: i32) -> Option<&'static T> {
182+
Self::get_ith_node(self.head, index)
183183
}
184184

185-
fn get_ith_node(&mut self, node: Option<NonNull<Node<T>>>, index: i32) -> Option<&T> {
185+
fn get_ith_node(node: Option<NonNull<Node<T>>>, index: i32) -> Option<&'static T> {
186186
match node {
187187
None => None,
188188
Some(next_ptr) => match index {
189189
0 => Some(unsafe { &(*next_ptr.as_ptr()).val }),
190-
_ => self.get_ith_node(unsafe { (*next_ptr.as_ptr()).next }, index - 1),
190+
_ => Self::get_ith_node(unsafe { (*next_ptr.as_ptr()).next }, index - 1),
191191
},
192192
}
193193
}

src/dynamic_programming/edit_distance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub fn edit_distance_se(str_a: &str, str_b: &str) -> u32 {
7272
// c is distances[i][j-1] and s is distances[i-1][j-1] at the beginning of each round of iteration
7373
char_b = str_b[j - 1];
7474
c = min(
75-
s + if char_a == char_b { 0 } else { 1 },
75+
s + u32::from(char_a != char_b),
7676
min(c + 1, distances[j] + 1),
7777
);
7878
// c is updated to distances[i][j], and will thus become distances[i][j-1] for the next cell

src/general/huffman_encoding.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ impl<T> Ord for HuffmanNode<T> {
4343
impl<T: Clone + Copy + Ord> HuffmanNode<T> {
4444
/// Turn the tree into the map that can be used in encoding
4545
pub fn get_alphabet(
46-
&self,
4746
height: u32,
4847
path: u64,
4948
node: &HuffmanNode<T>,
@@ -60,8 +59,8 @@ impl<T: Clone + Copy + Ord> HuffmanNode<T> {
6059
);
6160
}
6261
None => {
63-
self.get_alphabet(height + 1, path, node.left.as_ref().unwrap(), map);
64-
self.get_alphabet(
62+
Self::get_alphabet(height + 1, path, node.left.as_ref().unwrap(), map);
63+
Self::get_alphabet(
6564
height + 1,
6665
path | (1 << height),
6766
node.right.as_ref().unwrap(),
@@ -103,7 +102,7 @@ impl<T: Clone + Copy + Ord> HuffmanDictionary<T> {
103102
});
104103
}
105104
let root = queue.pop().unwrap();
106-
root.get_alphabet(0, 0, &root, &mut alph);
105+
HuffmanNode::get_alphabet(0, 0, &root, &mut alph);
107106
HuffmanDictionary {
108107
alphabet: alph,
109108
root,

src/graph/floyd_warshall.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn floyd_warshall<V: Ord + Copy, E: Ord + Copy + Add<Output = E>>(
3030
});
3131
}
3232
}
33-
let keys = map.iter().map(|(k, _)| *k).collect::<Vec<_>>();
33+
let keys = map.keys().copied().collect::<Vec<_>>();
3434
for &k in &keys {
3535
for &i in &keys {
3636
if map[&i].get(&k).is_none() {
@@ -49,9 +49,7 @@ pub fn floyd_warshall<V: Ord + Copy, E: Ord + Copy + Add<Output = E>>(
4949
match entry_i_j {
5050
Some(&e) => {
5151
if e > entry_i_k + entry_k_j {
52-
map.entry(i)
53-
.or_insert(BTreeMap::new())
54-
.insert(j, entry_i_k + entry_k_j);
52+
map.entry(i).or_default().insert(j, entry_i_k + entry_k_j);
5553
}
5654
}
5755
None => {

src/graph/prufer_code.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn prufer_encode<V: Ord + Copy>(tree: &Graph<V>) -> Vec<V> {
3333

3434
#[inline]
3535
fn add_directed_edge<V: Ord + Copy>(tree: &mut Graph<V>, a: V, b: V) {
36-
tree.entry(a).or_insert(vec![]).push(b);
36+
tree.entry(a).or_default().push(b);
3737
}
3838

3939
#[inline]

0 commit comments

Comments
 (0)