Skip to content

Commit

Permalink
lint: cleanup a few clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfertel committed Jan 20, 2024
1 parent f3bc453 commit 66e1ceb
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 40 deletions.
11 changes: 5 additions & 6 deletions src/ciphers/morse_code.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::HashMap;
use std::io;

const UNKNOWN_CHARACTER: &str = "........";
const _UNKNOWN_MORSE_CHARACTER: &str = "_";
Expand All @@ -19,7 +18,7 @@ pub fn encode(message: &str) -> String {
// Declarative macro for creating readable map declarations, for more info see https://doc.rust-lang.org/book/ch19-06-macros.html
macro_rules! map {
($($key:expr => $value:expr),* $(,)?) => {
std::iter::Iterator::collect(std::array::IntoIter::new([$(($key, $value),)*]))
std::iter::Iterator::collect(IntoIterator::into_iter([$(($key, $value),)*]))
};
}

Expand Down Expand Up @@ -109,10 +108,10 @@ fn _decode_part(string: &str) -> String {
/// Given a morse code, return the corresponding message.
/// If the code is invalid, the undecipherable part of the code is replaced by `_`.
#[cfg(test)]
pub fn decode(string: &str) -> Result<String, io::Error> {
pub fn decode(string: &str) -> Result<String, std::io::Error> {
if !_check_all_parts(string) {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"Invalid morse code",
));
}
Expand Down Expand Up @@ -192,7 +191,7 @@ mod tests {
fn decrypt_invalid_morsecode_with_spaces() {
let encypted = "1... . .-.. .-.. --- / -- --- .-. ... .";
let result = decode(encypted).map_err(|e| e.kind());
let expected = Err(io::ErrorKind::InvalidData);
let expected = Err(std::io::ErrorKind::InvalidData);

assert_eq!(expected, result);
}
Expand Down
2 changes: 1 addition & 1 deletion src/data_structures/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ where
{
let mut node = &mut self.root;
for c in key.into_iter() {
node = node.children.entry(c).or_insert_with(Node::default);
node = node.children.entry(c).or_default();
}
node.value = Some(value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/dynamic_programming/rod_cutting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn rod_cutting(price: &mut Vec<u32>) -> u32 {
return 0;
}

let mut val = vec![0; (length + 1) as usize];
let mut val = vec![0; length + 1];
val[0] = 0;

// build the table in bottom up manner and return the last entry from the table
Expand Down
4 changes: 2 additions & 2 deletions src/general/convex_hull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ fn sort_by_min_angle(pts: &[(f64, f64)], min: &(f64, f64)) -> Vec<(f64, f64)> {
.iter()
.map(|x| {
(
((x.1 - min.1) as f64).atan2((x.0 - min.0) as f64),
(x.1 - min.1).atan2(x.0 - min.0),
// angle
((x.1 - min.1) as f64).hypot((x.0 - min.0) as f64),
(x.1 - min.1).hypot(x.0 - min.0),
// distance (we want the closest to be first)
*x,
)
Expand Down
4 changes: 2 additions & 2 deletions src/general/huffman_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl<T> PartialEq for HuffmanNode<T> {

impl<T> PartialOrd for HuffmanNode<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.frequency.cmp(&other.frequency).reverse())
Some(self.cmp(&other))
}
}

Expand All @@ -41,7 +41,7 @@ impl<T> Ord for HuffmanNode<T> {
}

impl<T: Clone + Copy + Ord> HuffmanNode<T> {
/// Turn the tree into the map that can be used in encoding
/// Turn the tree into the map that can be used in encoding.
pub fn get_alphabet(
&self,
height: u32,
Expand Down
22 changes: 11 additions & 11 deletions src/graphs/prim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use std::ops::Add;
type Graph<V, E> = BTreeMap<V, BTreeMap<V, E>>;

fn add_edge<V: Ord + Copy, E: Ord + Add + Copy>(graph: &mut Graph<V, E>, v1: V, v2: V, c: E) {
graph.entry(v1).or_insert_with(BTreeMap::new).insert(v2, c);
graph.entry(v2).or_insert_with(BTreeMap::new).insert(v1, c);
graph.entry(v1).or_default().insert(v2, c);
graph.entry(v2).or_default().insert(v1, c);
}

// selects a start and run the algorithm from it
// Selects a start and run the algorithm from it.
pub fn prim<V: Ord + Copy + std::fmt::Debug, E: Ord + Add + Copy + std::fmt::Debug>(
graph: &Graph<V, E>,
) -> Graph<V, E> {
Expand All @@ -19,32 +19,32 @@ pub fn prim<V: Ord + Copy + std::fmt::Debug, E: Ord + Add + Copy + std::fmt::Deb
}
}

// only works for a connected graph
// if the given graph is not connected it will return the MST of the connected subgraph
// Only works for a connected graph.
// If the given graph is not connected it will return the MST of the connected subgraph.
pub fn prim_with_start<V: Ord + Copy, E: Ord + Add + Copy>(
graph: &Graph<V, E>,
start: V,
) -> Graph<V, E> {
// will contain the MST
// Will contain the MST.
let mut mst: Graph<V, E> = Graph::new();
// a priority queue based on a binary heap, used to get the cheapest edge
// the elements are an edge: the cost, destination and source
// A priority queue based on a binary heap, used to get the cheapest edge.
// The elements are an edge: the cost, destination and source.
let mut prio = BinaryHeap::new();

mst.insert(start, BTreeMap::new());

for (v, c) in &graph[&start] {
// the heap is a max heap, we have to use Reverse when adding to simulate a min heap
// The heap is a max heap, we have to use Reverse when adding to simulate a min heap.
prio.push(Reverse((*c, v, start)));
}

while let Some(Reverse((dist, t, prev))) = prio.pop() {
// the destination of the edge has already been seen
// The destination of the edge has already been seen.
if mst.contains_key(t) {
continue;
}

// the destination is a new vertex
// The destination is a new vertex.
add_edge(&mut mst, prev, *t, dist);

for (v, c) in &graph[t] {
Expand Down
5 changes: 2 additions & 3 deletions src/graphs/prufer_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ pub fn prufer_encode<V: Ord + Copy>(tree: &Graph<V>) -> Vec<V> {
if tree.len() <= 2 {
return vec![];
}
let mut result: Vec<V> = Vec::new();
result.reserve(tree.len() - 2);
let mut result: Vec<V> = Vec::with_capacity(tree.len() - 2);
let mut queue = BinaryHeap::new();
let mut in_tree = BTreeSet::new();
let mut degree = BTreeMap::new();
Expand All @@ -33,7 +32,7 @@ pub fn prufer_encode<V: Ord + Copy>(tree: &Graph<V>) -> Vec<V> {

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

#[inline]
Expand Down
4 changes: 2 additions & 2 deletions src/graphs/representation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ pub struct Edge(u32, u32);

#[derive(Debug, Clone)]
pub struct Graph {
vertices: Vec<Vertex>,
edges: Vec<Edge>,
pub vertices: Vec<Vertex>,
pub edges: Vec<Edge>,
}

impl Graph {
Expand Down
6 changes: 3 additions & 3 deletions src/math/gaussian_elimination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn echelon(matrix: &mut [Vec<f32>], i: usize, j: usize) {
let size = matrix.len();
if matrix[i][i] == 0f32 {
} else {
let factor = matrix[j + 1][i] as f32 / matrix[i][i] as f32;
let factor = matrix[j + 1][i] / matrix[i][i];
(i..size + 1).for_each(|k| {
matrix[j + 1][k] -= factor * matrix[i][k];
});
Expand All @@ -49,9 +49,9 @@ fn eliminate(matrix: &mut [Vec<f32>], i: usize) {
if matrix[i][i] == 0f32 {
} else {
for j in (1..i + 1).rev() {
let factor = matrix[j - 1][i] as f32 / matrix[i][i] as f32;
let factor = matrix[j - 1][i] / matrix[i][i];
for k in (0..size + 1).rev() {
matrix[j - 1][k] -= factor * matrix[i][k] as f32;
matrix[j - 1][k] -= factor * matrix[i][k];
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/math/pollard_rho.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn pollard_rho_customizable(
{
small_iteration += 1;
x = advance(x, c, number);
let diff = (x as i128 - y as i128).abs() as u128;
let diff = (x as i128 - y as i128).unsigned_abs();
remainder = (remainder * diff) % number as u128;
}
current_gcd = gcd(remainder as u64, number);
Expand All @@ -87,7 +87,7 @@ fn pollard_rho_customizable(
if current_gcd == number {
while current_gcd == 1 {
x_start = advance(x_start, c, number);
current_gcd = gcd((x_start as i128 - y as i128).abs() as u64, number);
current_gcd = gcd((x_start as i128 - y as i128).unsigned_abs() as u64, number);
}
}
current_gcd
Expand Down Expand Up @@ -178,8 +178,7 @@ pub fn pollard_rho_factorize(
return result;
}
let mut to_be_factored = vec![number];
while !to_be_factored.is_empty() {
let last = to_be_factored.pop().unwrap();
while let Some(last) = to_be_factored.pop() {
if last < minimum_prime_factors.len() as u64 {
result.append(&mut factor_using_mpf(last as usize, minimum_prime_factors));
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/searching/kth_smallest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn partition<T: PartialOrd>(arr: &mut [T], lo: isize, hi: isize) -> isize {
arr.swap(i as usize, j as usize);
}
}
arr.swap(i as usize, pivot as usize);
arr.swap(i as usize, pivot);
i
}

Expand Down
2 changes: 1 addition & 1 deletion src/sorting/shell_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn shell_sort<T: Ord + Copy>(values: &mut [T]) {
// make swaps
while pos >= gap && values[pos - gap] > val_current {
values[pos] = values[pos - gap];
pos = pos - gap;
pos -= gap;
}
values[pos] = val_current;
}
Expand Down
6 changes: 3 additions & 3 deletions src/sorting/tim_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ fn merge<T: Default + Clone + Eq + Ord + Copy>(
) -> &[T] {
let len1 = m - l + 1;
let len2 = r - m;
let mut left = vec![T::default(); len1 as usize];
let mut right = vec![T::default(); len2 as usize];
let mut left = vec![T::default(); len1];
let mut right = vec![T::default(); len2];

left[..len1].clone_from_slice(&arr[l..(len1 + l)]);

Expand Down Expand Up @@ -73,7 +73,7 @@ fn merge<T: Default + Clone + Eq + Ord + Copy>(
}

fn _tim_sort<T: Ord + Eq + Default + Clone + Copy>(arr: &mut [T], n: usize) {
let min_run = min_run_length(MIN_MERGE) as usize;
let min_run = min_run_length(MIN_MERGE);

let mut i = 0;
while i < n {
Expand Down

0 comments on commit 66e1ceb

Please sign in to comment.