Add GraphTopology::incidences(), update and add related tests

This commit is contained in:
2026-05-08 13:12:23 +02:00
parent d3fcd690a5
commit 0bcd270d12
4 changed files with 378 additions and 63 deletions
+29
View File
@@ -70,6 +70,24 @@ impl IncidentEdgeCursor {
}
}
struct IncidenceIterator<'a> {
graph: &'a Graph,
incidence: Option<IncidenceSlot>,
}
impl<'a> Iterator for IncidenceIterator<'a> {
type Item = (Vertex, Edge);
fn next(&mut self) -> Option<Self::Item> {
let current = self.incidence?;
let e = self.graph.incidences.get_idx(current.0).unwrap();
let entry = &self.graph.incidences[e];
self.incidence = entry.next;
let v = self.graph.vertices.get_idx(entry.adjacent.0).unwrap();
Some((v, self.graph.normalize_edge(e)))
}
}
pub struct Graph {
// TODO: Arena index and generation types could be externalized to Graph.
vertices: Arena<VertexIncidenceHeader, usize, usize>,
@@ -142,6 +160,10 @@ impl Graph {
self.incidences[previous].next = next;
}
}
fn normalize_edge(&self, e: Edge) -> Edge {
self.incidences.get_idx(e.arr_idx() & !1).unwrap()
}
}
impl Default for Graph {
@@ -205,6 +227,13 @@ impl GraphTopology for Graph {
.map(|(i, _)| i)
}
fn incidences(&self, v: Self::Vertex) -> impl Iterator<Item = (Self::Vertex, Self::Edge)> {
IncidenceIterator {
graph: self,
incidence: self.vertices[v].first_incidence,
}
}
fn add_vertex(&mut self) -> Self::Vertex {
self.vertices.insert(VertexIncidenceHeader {
incidence_count: 0,