kill the old funky can_reach
fn
This commit is contained in:
parent
4b1d3b7036
commit
dcf6f08138
@ -203,49 +203,6 @@ pub fn block_query<P>(b: &ast::Block, p: P) -> bool where P: FnMut(&ast::Expr) -
|
||||
return v.flag;
|
||||
}
|
||||
|
||||
/// K: Eq + Hash<S>, V, S, H: Hasher<S>
|
||||
///
|
||||
/// Determines whether there exists a path from `source` to `destination`. The
|
||||
/// graph is defined by the `edges_map`, which maps from a node `S` to a list of
|
||||
/// its adjacent nodes `T`.
|
||||
///
|
||||
/// Efficiency note: This is implemented in an inefficient way because it is
|
||||
/// typically invoked on very small graphs. If the graphs become larger, a more
|
||||
/// efficient graph representation and algorithm would probably be advised.
|
||||
pub fn can_reach<T, S>(edges_map: &HashMap<T, Vec<T>, S>, source: T,
|
||||
destination: T) -> bool
|
||||
where S: HashState, T: Hash + Eq + Clone,
|
||||
{
|
||||
if source == destination {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Do a little breadth-first-search here. The `queue` list
|
||||
// doubles as a way to detect if we've seen a particular FR
|
||||
// before. Note that we expect this graph to be an *extremely
|
||||
// shallow* tree.
|
||||
let mut queue = vec!(source);
|
||||
let mut i = 0;
|
||||
while i < queue.len() {
|
||||
match edges_map.get(&queue[i]) {
|
||||
Some(edges) => {
|
||||
for target in edges {
|
||||
if *target == destination {
|
||||
return true;
|
||||
}
|
||||
|
||||
if !queue.iter().any(|x| x == target) {
|
||||
queue.push((*target).clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Memoizes a one-argument closure using the given RefCell containing
|
||||
/// a type implementing MutableMap to serve as a cache.
|
||||
///
|
||||
|
Loading…
x
Reference in New Issue
Block a user