Add GraphTopology.incidence_cursor() for certain use-cases where the iterator is problematic

Also add raw_incidences() and step_incidence() to AppendGraph analogously to
Graph, which allows to remove RawIncidenceIterator and simplify its call sites.
This commit is contained in:
2026-06-26 23:36:41 +02:00
parent c197766229
commit f009a86f1e
3 changed files with 54 additions and 27 deletions
+21 -2
View File
@@ -1,7 +1,7 @@
use typed_generational_arena::{Arena, Index};
use crate::maps::{EdgeMap, VertexMap};
use crate::traits::{GraphTopology, GraphTopologyDeletion};
use crate::traits::{GraphTopology, GraphTopologyDeletion, IncidenceCursor};
type Vertex = Index<VertexIncidenceHeader, usize, usize>;
@@ -34,6 +34,19 @@ impl IncidentEdgeCursor {
}
}
#[derive(Copy, Clone)]
pub struct GraphIncidenceCursor {
incidence: Option<IncidenceSlot>,
}
impl IncidenceCursor<Graph> for GraphIncidenceCursor {
fn next(&mut self, graph: &Graph) -> Option<(Vertex, Edge)> {
graph
.step_incidence(&mut self.incidence)
.map(|(vs, e)| (graph.vertices.get_idx(vs.0).unwrap(), e))
}
}
pub struct Graph {
// TODO: Arena index and generation types could be externalized to Graph.
vertices: Arena<VertexIncidenceHeader, usize, usize>,
@@ -137,8 +150,8 @@ impl Default for Graph {
impl GraphTopology for Graph {
type Vertex = Vertex;
type Edge = Edge;
type IncidenceCursor = GraphIncidenceCursor;
fn vertex_count(&self) -> usize {
self.vertices.len()
@@ -210,6 +223,12 @@ impl GraphTopology for Graph {
.map(|(vs, e)| (self.vertices.get_idx(vs.0).unwrap(), self.normalize_edge(e)))
}
fn incidence_cursor(&self, v: Self::Vertex) -> Self::IncidenceCursor {
GraphIncidenceCursor {
incidence: self.vertices[v].first_incidence,
}
}
fn add_vertex(&mut self) -> Self::Vertex {
self.vertices.insert(VertexIncidenceHeader {
incidence_count: 0,