From 97c5cc33dc5f1aa14cf5101854e278e3ddb53481 Mon Sep 17 00:00:00 2001 From: Wilfred Hughes Date: Sun, 27 Jun 2021 22:19:57 -0700 Subject: [PATCH] Return a vec of graphnodes from find_route --- src/ucs.rs | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/ucs.rs b/src/ucs.rs index e92572988..e0bb1d751 100644 --- a/src/ucs.rs +++ b/src/ucs.rs @@ -1,14 +1,14 @@ #![allow(dead_code)] use std::cmp::Ordering; -use std::collections::BinaryHeap; use std::collections::HashSet; +use std::collections::{BinaryHeap, HashMap}; use std::hash::{Hash, Hasher}; use crate::tree_diff::Node; use Action::*; -#[derive(Debug, Eq)] +#[derive(Debug, Eq, Clone)] struct GraphNode<'a> { distance: u64, action: Action, @@ -106,13 +106,14 @@ impl Action { } } -fn find_route<'a>(start: GraphNode<'a>) { +fn find_route<'a>(start: GraphNode<'a>) -> Vec> { let mut heap = BinaryHeap::new(); - heap.push(OrderedGraphNode { gn: start }); + heap.push(OrderedGraphNode { gn: start.clone() }); let mut visited: HashSet = HashSet::new(); + let mut predecessors: HashMap = HashMap::new(); - loop { + 'outer: loop { match heap.pop() { Some(ogn) => { let egn = EqualityGraphNode { gn: ogn.gn }; @@ -122,9 +123,10 @@ fn find_route<'a>(start: GraphNode<'a>) { let gn = egn.gn; for new_gn in next_graph_nodes(&gn) { + predecessors.insert(EqualityGraphNode { gn: new_gn.clone() }, gn.clone()); + if new_gn.is_end() { - println!("Success!"); - break; + break 'outer; } heap.push(OrderedGraphNode { gn: new_gn }); @@ -132,9 +134,24 @@ fn find_route<'a>(start: GraphNode<'a>) { visited.insert(EqualityGraphNode { gn }); } - None => break, + None => panic!("Ran out of graph nodes before reaching end"), } } + + let mut current = start; + let mut res = vec![]; + loop { + res.push(current.clone()); + if current.is_end() { + break; + } + + current = predecessors + .remove(&EqualityGraphNode { gn: current }) + .unwrap(); + } + + res } fn next_graph_nodes<'a>(gn: &GraphNode<'a>) -> Vec> {