Skip to content

Commit c6d4335

Browse files
committed
Satisfied clippy
1 parent ce932f4 commit c6d4335

File tree

3 files changed

+30
-30
lines changed

3 files changed

+30
-30
lines changed

src/algorithms.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub fn dijkstra<T: Display + Clone + Eq + Hash>(graph: &mut Graph<T>, source_nod
9898
closed_node_ids.insert(node.borrow().clone().id);
9999
}
100100

101-
Ok(ShortestPathTree::from_graph(&graph, &source_node_id))
101+
Ok(ShortestPathTree::from_graph(graph, source_node_id))
102102
}
103103

104104
fn calc_min_distance<T: Display + Eq + Clone>(node: &Rc<RefCell<Node<T>>>, weight: i32, source: &Rc<RefCell<Node<T>>>) {
@@ -197,7 +197,7 @@ pub fn bellman_ford<T: Display + Eq + Clone + Hash>(graph: &mut Graph<T>, source
197197
let nodes_count = graph.size();
198198

199199
for _ in 0..nodes_count - 1 {
200-
for (_, node) in &graph.nodes {
200+
for node in graph.nodes.values() {
201201
let node_ref = node.borrow();
202202

203203
if node_ref.distance == std::i32::MAX {
@@ -212,15 +212,15 @@ pub fn bellman_ford<T: Display + Eq + Clone + Hash>(graph: &mut Graph<T>, source
212212
if new_distance < target_node_ref.distance {
213213
target_node_ref.distance = new_distance;
214214
let mut shortest_path = node_ref.shortest_path.clone();
215-
shortest_path.push(Rc::clone(&node));
215+
shortest_path.push(Rc::clone(node));
216216
target_node_ref.shortest_path = shortest_path;
217217
}
218218
}
219219
}
220220
}
221221

222222
// Check for negative cycles
223-
for (_, node) in &graph.nodes {
223+
for node in graph.nodes.values() {
224224
let node_ref = node.borrow();
225225

226226
for edge in &node_ref.edges {
@@ -234,7 +234,7 @@ pub fn bellman_ford<T: Display + Eq + Clone + Hash>(graph: &mut Graph<T>, source
234234
}
235235
}
236236

237-
Ok(ShortestPathTree::from_graph(&graph, &source_node_id))
237+
Ok(ShortestPathTree::from_graph(graph, source_node_id))
238238
}
239239

240240
/// Errors that can occur when algorithms are run.

src/graph.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::{fmt::{Display, Debug}, rc::Rc, cell::RefCell, hash::Hash, collections::HashMap};
1+
use std::{fmt::Display, rc::Rc, cell::RefCell, hash::Hash, collections::HashMap};
22

3-
use crate::{Node, Edge, Graph, AddEdgeError, ShortestPath};
3+
use crate::{Node, Edge, Graph, AddEdgeError};
44

55
// Node implementations
66

@@ -71,7 +71,7 @@ impl<T: Display + Eq> PartialEq for Edge<T> {
7171

7272
// Graph implementations
7373

74-
impl<'a, T: Display + Clone + Eq + Hash> Graph<T> {
74+
impl<T: Display + Clone + Eq + Hash> Graph<T> {
7575

7676
/// Creates a new and empty graph.
7777
pub fn new() -> Self {
@@ -184,10 +184,10 @@ impl<'a, T: Display + Clone + Eq + Hash> Graph<T> {
184184
if !self.nodes.contains_key(source_id) && !self.nodes.contains_key(target_id) {
185185
return false;
186186
}
187-
let parent = Rc::clone(&self.nodes.get(source_id).unwrap());
188-
let target = Rc::clone(&self.nodes.get(target_id).unwrap());
187+
let parent = Rc::clone(self.nodes.get(source_id).unwrap());
188+
let target = Rc::clone(self.nodes.get(target_id).unwrap());
189189
self.nodes.get(source_id).unwrap().borrow_mut().edges.push(Edge::new(weight, parent, target));
190-
return true;
190+
true
191191
}
192192

193193
/// Tries to add a new edge to the graph that connects two nodes in a single direction from source to target.
@@ -226,8 +226,8 @@ impl<'a, T: Display + Clone + Eq + Hash> Graph<T> {
226226
} else if !self.nodes.contains_key(target_id) {
227227
return Err(AddEdgeError::TargetMissing);
228228
}
229-
let parent = Rc::clone(&self.nodes.get(source_id).unwrap());
230-
let target = Rc::clone(&self.nodes.get(target_id).unwrap());
229+
let parent = Rc::clone(self.nodes.get(source_id).unwrap());
230+
let target = Rc::clone(self.nodes.get(target_id).unwrap());
231231
self.nodes.get(source_id).unwrap().borrow_mut().edges.push(Edge::new(weight, parent, target));
232232
Ok(())
233233
}
@@ -257,8 +257,8 @@ impl<'a, T: Display + Clone + Eq + Hash> Graph<T> {
257257
if !self.nodes.contains_key(source_id) && !self.nodes.contains_key(target) {
258258
return false;
259259
}
260-
let parent = Rc::clone(&self.nodes.get(source_id).unwrap());
261-
let destination = Rc::clone(&self.nodes.get(target).unwrap());
260+
let parent = Rc::clone(self.nodes.get(source_id).unwrap());
261+
let destination = Rc::clone(self.nodes.get(target).unwrap());
262262
self.nodes.get(source_id).unwrap().borrow_mut().edges.push(Edge::new(weight, parent.clone(), destination.clone()));
263263
self.nodes.get(target).unwrap().borrow_mut().edges.push(Edge::new(weight, destination, parent));
264264
true
@@ -300,8 +300,8 @@ impl<'a, T: Display + Clone + Eq + Hash> Graph<T> {
300300
} else if !self.nodes.contains_key(target_id) {
301301
return Err(AddEdgeError::TargetMissing);
302302
}
303-
let parent = Rc::clone(&self.nodes.get(source_id).unwrap());
304-
let destination = Rc::clone(&self.nodes.get(target_id).unwrap());
303+
let parent = Rc::clone(self.nodes.get(source_id).unwrap());
304+
let destination = Rc::clone(self.nodes.get(target_id).unwrap());
305305
self.nodes.get(source_id).unwrap().borrow_mut().edges.push(Edge::new(weight, parent.clone(), destination.clone()));
306306
self.nodes.get(target_id).unwrap().borrow_mut().edges.push(Edge::new(weight, destination, parent));
307307
Ok(())
@@ -374,13 +374,19 @@ impl<'a, T: Display + Clone + Eq + Hash> Graph<T> {
374374

375375
}
376376

377+
impl<T: Display + Clone + Eq + Hash> Default for Graph<T> {
378+
fn default() -> Self {
379+
Self::new()
380+
}
381+
}
382+
377383
impl<T: Display> Display for Graph<T> {
378384
/// Formats the graph to show all edges between nodes
379385
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
380386
let mut graph = String::new();
381387
graph.push_str(&format!("{:13} | {:08} | edges\n", "id", "distance"));
382388
graph.push_str("--------------------------------------------------------------------\n");
383-
for (_, node) in &self.nodes {
389+
for node in self.nodes.values() {
384390
let id = &node.borrow().id;
385391
let distance = node.borrow().distance;
386392
if distance != i32::MAX {
@@ -389,11 +395,7 @@ impl<T: Display> Display for Graph<T> {
389395
graph.push_str(&format!("{:13} | {:8} | ", id, ""));
390396
}
391397
for edge in &node.borrow().edges {
392-
if edge.weight < 0 {
393-
graph.push_str(&format!("(--({})-> {})", edge.weight, edge.target.borrow().id));
394-
} else {
395-
graph.push_str(&format!("(--({})-> {})", edge.weight, edge.target.borrow().id));
396-
}
398+
graph.push_str(&format!("(--({})-> {})", edge.weight, edge.target.borrow().id));
397399
}
398400
graph.push('\n');
399401
}
@@ -429,7 +431,7 @@ impl<T: Display + Clone + Eq + Hash + From<String>> From<&Vec<Vec<i32>>> for Gra
429431
let mut graph: Graph<T> = Graph::new();
430432
for (i_y, y) in value.iter().enumerate() {
431433
for (i_x, _x) in y.iter().enumerate() {
432-
graph.add_node(String::from(format!("[{}|{}]", i_x, i_y)).into());
434+
graph.add_node(format!("[{}|{}]", i_x, i_y).into());
433435
}
434436
}
435437
for (i_y, y) in value.iter().enumerate() {

src/lib.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ impl<T: Display + Clone + Eq + Hash> ShortestPathTree<T> {
127127
/// The `distance` is the minimal distance to this node and `shortest_path` is the path taken to get to this node.
128128
///
129129
/// If either `distance` or `shortest_path` is None, no path will be added.
130+
#[allow(clippy::unnecessary_unwrap)]
130131
fn add_result(&mut self, node_id: T, distance: Option<i32>, shortest_path: Option<ShortestPath<T>>) {
131132
if shortest_path.is_none() | distance.is_none() {
132133
self.results.insert(node_id, None);
@@ -176,10 +177,7 @@ impl<T: Display + Clone + Eq + Hash> ShortestPathTree<T> {
176177
/// # }
177178
/// ```
178179
pub fn shortest_distance(&self, target_id: &T) -> Option<i32> {
179-
match self.results.get(&target_id)? {
180-
Some(res) => Some(res.0),
181-
None => None,
182-
}
180+
self.results.get(target_id)?.as_ref().map(|res| res.0)
183181
}
184182

185183
/// Creates a shortest path tree from a graph and the id of a source node. A pathfinding algorithm
@@ -237,7 +235,7 @@ impl<T: Display + Clone> ShortestPath<T> {
237235
/// # Ok(())}
238236
/// ```
239237
pub fn source(&self) -> Option<&T> {
240-
self.path.first().clone()
238+
self.path.first()
241239
}
242240

243241
/// Id of target node where this shortest path ends.
@@ -262,7 +260,7 @@ impl<T: Display + Clone> ShortestPath<T> {
262260
/// # Ok(())}
263261
/// ```
264262
pub fn target(&self) -> Option<&T> {
265-
self.path.last().clone()
263+
self.path.last()
266264
}
267265

268266
/// Id's of nodes that form the shortest path.

0 commit comments

Comments
 (0)