Skip to content

Commit

Permalink
Fix purpleprotocol#56 - Don't consume self in Dijkstra::get_path_to
Browse files Browse the repository at this point in the history
We only consume `self` for `iterator: VecDequeue`, which is unused
outside of this routine and might as well be a local variable.
  • Loading branch information
cemeyer committed Dec 9, 2021
1 parent 3b4a033 commit 727f7bd
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/iterators/dijkstra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ impl Ord for VertexMeta {
pub struct Dijkstra<'a, T> {
source: &'a VertexId,
iterable: &'a Graph<T>,
iterator: VecDeque<VertexId>,
distances: HashMap<VertexId, f32>,
previous: HashMap<VertexId, Option<VertexId>>,
}
Expand All @@ -74,7 +73,6 @@ impl<'a, T> Dijkstra<'a, T> {
let mut instance = Dijkstra {
source: src,
iterable: graph,
iterator: VecDeque::with_capacity(graph.vertex_count()),
distances: HashMap::with_capacity(graph.vertex_count()),
previous: HashMap::with_capacity(graph.vertex_count()),
};
Expand All @@ -97,25 +95,25 @@ impl<'a, T> Dijkstra<'a, T> {
Ok(())
}

pub fn get_path_to(mut self, vert: &'a VertexId) -> Result<VertexIter, GraphErr> {
pub fn get_path_to(&self, vert: &'a VertexId) -> Result<VertexIter<'a>, GraphErr> {
if self.iterable.fetch(vert).is_none() {
return Err(GraphErr::NoSuchVertex);
}

if self.previous.contains_key(vert) {
let mut cur_vert = Some(vert);
self.iterator.clear();
let mut iterator = VecDeque::new();

while cur_vert.is_some() {
self.iterator.push_front(*cur_vert.unwrap());
while let Some(cur_vert_inner) = cur_vert {
iterator.push_front(*cur_vert_inner);

match self.previous.get(cur_vert.unwrap()) {
match self.previous.get(cur_vert_inner) {
Some(v) => cur_vert = v.as_ref(),
None => cur_vert = None,
}
}

return Ok(VertexIter(Box::new(OwningIterator::new(self.iterator))));
return Ok(VertexIter(Box::new(OwningIterator::new(iterator))));
}

Ok(VertexIter(Box::new(iter::empty())))
Expand Down

0 comments on commit 727f7bd

Please sign in to comment.