Skip to content

Commit a7f9c57

Browse files
committed
refactor(05/2015): simplify
1 parent eaa80da commit a7f9c57

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/utils/graphs/travelling_salesman.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,21 @@ impl<T: PartialEq + Eq + Hash> TravellingSalesman<T> {
2020

2121
pub fn find_shortest_path_cost(&self) -> Option<usize>
2222
where
23-
T: PartialEq + Clone,
23+
T: PartialEq + Clone + Copy,
2424
{
2525
self.all_paths().iter().map(|path| path.cost).min()
2626
}
2727

2828
pub fn find_longest_path_cost(&self) -> Option<usize>
2929
where
30-
T: PartialEq + Clone,
30+
T: PartialEq + Clone + Copy,
3131
{
3232
self.all_paths().iter().map(|path| path.cost).max()
3333
}
3434

3535
fn all_paths(&self) -> Vec<Path<T>>
3636
where
37-
T: PartialEq + Clone,
37+
T: PartialEq + Clone + Copy,
3838
{
3939
self.graph
4040
.nodes()
@@ -46,7 +46,7 @@ impl<T: PartialEq + Eq + Hash> TravellingSalesman<T> {
4646

4747
fn all_paths_between(&self, from: &T, to: &T) -> Vec<Path<T>>
4848
where
49-
T: PartialEq + Clone,
49+
T: PartialEq + Clone + Copy,
5050
{
5151
let path_length = self.graph.nodes().len();
5252

@@ -61,18 +61,16 @@ impl<T: PartialEq + Eq + Hash> TravellingSalesman<T> {
6161
continue;
6262
}
6363

64-
for node in self.graph.nodes() {
65-
let mut new_path = path.clone();
66-
let last = new_path.last().clone();
67-
let next = node.clone();
68-
64+
for next in self.graph.nodes() {
65+
let last = path.last();
6966
if last == next {
7067
continue;
7168
}
7269

73-
let cost = self.weights[&(last, next)];
70+
let cost = self.weights[&(*last, *next)];
71+
let mut new_path = path.clone();
7472

75-
if new_path.add(node, cost).is_ok() {
73+
if new_path.add(next, cost).is_ok() {
7674
queue.push_back(new_path);
7775
}
7876
}

0 commit comments

Comments
 (0)