use crate::types::NodeId;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum State {
Unvisited,
OnStack,
Finished,
}
pub(crate) fn find(edges: &[Vec<NodeId>]) -> Option<Vec<NodeId>> {
let mut states = vec![State::Unvisited; edges.len()];
let mut stack: Vec<(NodeId, usize)> = Vec::new();
for root in (0_u32..).take(edges.len()).map(NodeId::new) {
if states.get(root.index()) != Some(&State::Unvisited) {
continue;
}
set(&mut states, root, State::OnStack);
stack.push((root, 0));
while let Some((node, next)) = stack.last_mut() {
let node = *node;
let dependency = edges.get(node.index()).and_then(|deps| deps.get(*next));
*next += 1;
let Some(dependency) = dependency.copied() else {
set(&mut states, node, State::Finished);
stack.pop();
continue;
};
match states.get(dependency.index()) {
Some(State::OnStack) => return Some(cycle_from(&stack, dependency)),
Some(State::Unvisited) => {
set(&mut states, dependency, State::OnStack);
stack.push((dependency, 0));
}
Some(State::Finished) | None => {}
}
}
}
None
}
fn set(states: &mut [State], node: NodeId, state: State) {
if let Some(slot) = states.get_mut(node.index()) {
*slot = state;
}
}
fn cycle_from(stack: &[(NodeId, usize)], start: NodeId) -> Vec<NodeId> {
let mut cycle: Vec<NodeId> = stack
.iter()
.map(|(node, _)| *node)
.skip_while(|node| *node != start)
.collect();
let lowest = cycle
.iter()
.enumerate()
.min_by_key(|(_, node)| **node)
.map_or(0, |(position, _)| position);
cycle.rotate_left(lowest);
cycle
}
#[cfg(test)]
mod tests {
use super::find;
use crate::types::NodeId;
fn edges(lists: &[&[u32]]) -> Vec<Vec<NodeId>> {
lists
.iter()
.map(|list| list.iter().copied().map(NodeId::new).collect())
.collect()
}
fn ids(raw: &[u32]) -> Vec<NodeId> {
raw.iter().copied().map(NodeId::new).collect()
}
#[test]
fn a_graph_with_no_edges_has_no_cycle() {
assert_eq!(find(&[]), None);
assert_eq!(find(&edges(&[&[], &[], &[]])), None);
}
#[test]
fn a_diamond_is_not_a_cycle() {
assert_eq!(find(&edges(&[&[1, 2], &[3], &[3], &[]])), None);
}
#[test]
fn a_node_that_depends_on_itself_is_a_cycle_of_one() {
assert_eq!(find(&edges(&[&[], &[1]])), Some(ids(&[1])));
}
#[test]
fn a_cycle_of_two_is_reported_from_its_lowest_id() {
assert_eq!(find(&edges(&[&[1], &[0]])), Some(ids(&[0, 1])));
}
#[test]
fn a_cycle_reached_through_a_tail_leaves_the_tail_out() {
let graph = edges(&[&[3], &[], &[4], &[2], &[3]]);
assert_eq!(find(&graph), Some(ids(&[2, 4, 3])));
}
#[test]
fn the_first_cycle_in_root_then_edge_order_is_the_one_reported() {
let graph = edges(&[&[2], &[3], &[0], &[1]]);
assert_eq!(find(&graph), Some(ids(&[0, 2])));
let graph = edges(&[&[2, 1], &[0], &[0]]);
assert_eq!(find(&graph), Some(ids(&[0, 2])));
}
#[test]
fn a_long_chain_is_searched_without_recursion() {
let length = 100_000_u32;
let mut chain: Vec<Vec<NodeId>> = (1..length).map(|next| vec![NodeId::new(next)]).collect();
chain.push(Vec::new());
assert_eq!(find(&chain), None);
if let Some(last) = chain.last_mut() {
last.push(NodeId::new(0));
}
let cycle = find(&chain);
assert_eq!(cycle.as_ref().map(Vec::len), Some(100_000));
assert_eq!(
cycle.as_ref().and_then(|c| c.first()),
Some(&NodeId::new(0))
);
}
#[test]
fn an_edge_to_an_id_outside_the_graph_is_ignored() {
assert_eq!(find(&edges(&[&[7]])), None);
}
}