Rename Graph.vertex_incidence_headers to "incidence_headers"

This commit is contained in:
2025-10-06 10:41:04 +02:00
parent 5a1080783c
commit e518d72a9e
+11 -11
View File
@@ -11,8 +11,8 @@ struct VertexIncidenceHeader {
// TODO: Visibility of Graph members. // TODO: Visibility of Graph members.
struct Graph { struct Graph {
// TODO: Index 'vertex_incidence_headers' by a 'Vertex' instead of 'Vertex.0'? // TODO: Index 'incidence_headers' by a 'Vertex' instead of 'Vertex.0'?
vertex_incidence_headers: Vec<VertexIncidenceHeader>, incidence_headers: Vec<VertexIncidenceHeader>,
next_incidences: Vec<Option<Incidence>>, next_incidences: Vec<Option<Incidence>>,
incidence_vertices: Vec<Vertex>, incidence_vertices: Vec<Vertex>,
} }
@@ -20,7 +20,7 @@ struct Graph {
// TODO: Add iterator of vertex neighbors, see 'fn add_adjacent()'. // TODO: Add iterator of vertex neighbors, see 'fn add_adjacent()'.
impl Graph { impl Graph {
fn vertex_count(&self) -> usize { fn vertex_count(&self) -> usize {
self.vertex_incidence_headers.len() self.incidence_headers.len()
} }
fn edge_count(&self) -> usize { fn edge_count(&self) -> usize {
@@ -28,12 +28,12 @@ impl Graph {
} }
fn degree(&self, v: Vertex) -> usize { fn degree(&self, v: Vertex) -> usize {
self.vertex_incidence_headers[v.0].incidence_count self.incidence_headers[v.0].incidence_count
} }
// TODO: 'fn are_adjacent()' could probably be done with a neighbor vertex iterator. // TODO: 'fn are_adjacent()' could probably be done with a neighbor vertex iterator.
fn are_adjacent(&self, v1: Vertex, v2: Vertex) -> bool { fn are_adjacent(&self, v1: Vertex, v2: Vertex) -> bool {
let Some(mut incidence) = self.vertex_incidence_headers[v1.0].first_incidence else { let Some(mut incidence) = self.incidence_headers[v1.0].first_incidence else {
return false; return false;
}; };
loop { loop {
@@ -50,11 +50,11 @@ impl Graph {
// TODO: 'fn add_vertex()' should not be publicly accessible for all graph implementations. // TODO: 'fn add_vertex()' should not be publicly accessible for all graph implementations.
// TODO: 'fn add_vertex()' needs variants for custom vertex data. // TODO: 'fn add_vertex()' needs variants for custom vertex data.
fn add_vertex(&mut self) -> Vertex { fn add_vertex(&mut self) -> Vertex {
self.vertex_incidence_headers.push(VertexIncidenceHeader { self.incidence_headers.push(VertexIncidenceHeader {
incidence_count: 0, incidence_count: 0,
first_incidence: None, first_incidence: None,
}); });
Vertex(self.vertex_incidence_headers.len() - 1) Vertex(self.incidence_headers.len() - 1)
} }
// TODO: 'fn add_edge()' should not be publicly accessible for all graph implementations. // TODO: 'fn add_edge()' should not be publicly accessible for all graph implementations.
@@ -66,9 +66,9 @@ impl Graph {
fn add_incidence(&mut self, v1: Vertex, v2: Vertex) { fn add_incidence(&mut self, v1: Vertex, v2: Vertex) {
self.incidence_vertices.push(v2); self.incidence_vertices.push(v2);
self.next_incidences.push(self.vertex_incidence_headers[v1.0].first_incidence.take()); self.next_incidences.push(self.incidence_headers[v1.0].first_incidence.take());
self.vertex_incidence_headers[v1.0].incidence_count += 1; self.incidence_headers[v1.0].incidence_count += 1;
self.vertex_incidence_headers[v1.0].first_incidence = Some(Incidence(self.incidence_vertices.len() - 1)); self.incidence_headers[v1.0].first_incidence = Some(Incidence(self.incidence_vertices.len() - 1));
} }
} }
@@ -76,7 +76,7 @@ fn main() {
// TODO: Move this graph example into one or more tests. // TODO: Move this graph example into one or more tests.
let mut graph = Graph{ let mut graph = Graph{
// TODO: The initializations should happen in an initializer or constructor // TODO: The initializations should happen in an initializer or constructor
vertex_incidence_headers: vec![], incidence_headers: vec![],
next_incidences: vec![], next_incidences: vec![],
incidence_vertices: vec![], incidence_vertices: vec![],
}; };