summaryrefslogtreecommitdiff
path: root/graph/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'graph/src/lib.rs')
-rw-r--r--graph/src/lib.rs31
1 files changed, 28 insertions, 3 deletions
diff --git a/graph/src/lib.rs b/graph/src/lib.rs
index 26159c6..6813df3 100644
--- a/graph/src/lib.rs
+++ b/graph/src/lib.rs
@@ -214,12 +214,37 @@ impl<T: Hash + Eq + PartialEq + Clone + Copy + Ord + PartialOrd + Display + Debu
{
}
+/// We need both the node index of a parent and the edge index of the
+/// child that points to this parent.
+#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
+pub struct Parent(usize, usize);
+
+impl Parent {
+ /// Return the index of the parent node.
+ #[inline]
+ pub fn node(&self) -> usize {
+ self.0
+ }
+
+ /// Return the index of the edge that points to the child.
+ #[inline]
+ pub fn edge(&self) -> usize {
+ self.1
+ }
+
+ /// Construct a parent strucure.
+ #[inline]
+ pub fn new(node: usize, edge: usize) -> Self {
+ Self(node, edge)
+ }
+}
+
/// A parents-knowing graph can return an iterator of parents for any
/// node.
pub trait ParentsGraph: Graph {
/// The type of an iterator that goes through the parents of a
/// node.
- type Iter<'a>: Iterator<Item = usize> + 'a
+ type Iter<'a>: Iterator<Item = Parent> + 'a
where
Self: 'a;
@@ -340,14 +365,14 @@ mod test_cycle_detection {
let graph = builder.build_ref();
- assert_eq!(graph.contains_cycles()?, true);
+ assert!(!graph.contains_cycles()?);
// Remove the link from the last node to the first node.
builder.remove_edge(4, 0, |_| true)?;
let graph = builder.build();
- assert_eq!(graph.contains_cycles()?, false);
+ assert!(!graph.contains_cycles()?);
Ok(())
}