Combine next_incidences and incidence_vertices of Graph into one, and rename incidence_headers to vertices
This commit is contained in:
+25
-21
@@ -10,6 +10,11 @@ struct VertexIncidenceHeader {
|
||||
first_incidence: Option<Incidence>,
|
||||
}
|
||||
|
||||
struct IncidenceEntry {
|
||||
next: Option<Incidence>,
|
||||
neighbor: Vertex,
|
||||
}
|
||||
|
||||
pub struct VertexNeighborIterator<'a> {
|
||||
graph: &'a Graph,
|
||||
incidence: Option<Incidence>,
|
||||
@@ -20,8 +25,9 @@ impl<'a> Iterator for VertexNeighborIterator<'a> {
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let incidence = self.incidence?;
|
||||
self.incidence = self.graph.next_incidences[incidence.0];
|
||||
Some(self.graph.incidence_vertices[incidence.0])
|
||||
let entry = &self.graph.incidences[incidence.0];
|
||||
self.incidence = entry.next;
|
||||
Some(entry.neighbor)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,30 +38,28 @@ impl<'a> Iterator for VertexNeighborIterator<'a> {
|
||||
// TODO: Add iterator of edges.
|
||||
pub struct Graph {
|
||||
// TODO: Index 'incidence_headers' by a 'Vertex' instead of 'Vertex.0'?
|
||||
incidence_headers: Vec<VertexIncidenceHeader>,
|
||||
next_incidences: Vec<Option<Incidence>>,
|
||||
incidence_vertices: Vec<Vertex>,
|
||||
vertices: Vec<VertexIncidenceHeader>,
|
||||
incidences: Vec<IncidenceEntry>,
|
||||
}
|
||||
|
||||
impl Graph {
|
||||
pub fn new() -> Graph {
|
||||
Graph {
|
||||
incidence_headers: vec![],
|
||||
next_incidences: vec![],
|
||||
incidence_vertices: vec![],
|
||||
vertices: vec![],
|
||||
incidences: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn vertex_count(&self) -> usize {
|
||||
self.incidence_headers.len()
|
||||
self.vertices.len()
|
||||
}
|
||||
|
||||
pub fn edge_count(&self) -> usize {
|
||||
self.next_incidences.len() / 2
|
||||
self.incidences.len() / 2
|
||||
}
|
||||
|
||||
pub fn degree(&self, v: Vertex) -> usize {
|
||||
self.incidence_headers[v.0].incidence_count
|
||||
self.vertices[v.0].incidence_count
|
||||
}
|
||||
|
||||
pub fn are_adjacent(&self, v1: Vertex, v2: Vertex) -> bool {
|
||||
@@ -64,11 +68,11 @@ impl Graph {
|
||||
|
||||
// TODO: 'fn add_vertex()' needs variants for custom vertex data.
|
||||
pub fn add_vertex(&mut self) -> Vertex {
|
||||
self.incidence_headers.push(VertexIncidenceHeader {
|
||||
self.vertices.push(VertexIncidenceHeader {
|
||||
incidence_count: 0,
|
||||
first_incidence: None,
|
||||
});
|
||||
Vertex(self.incidence_headers.len() - 1)
|
||||
Vertex(self.vertices.len() - 1)
|
||||
}
|
||||
|
||||
// TODO: 'fn add_edge()' needs variants for custom edge data.
|
||||
@@ -78,22 +82,22 @@ impl Graph {
|
||||
}
|
||||
|
||||
fn add_incidence(&mut self, v1: Vertex, v2: Vertex) {
|
||||
self.incidence_vertices.push(v2);
|
||||
self.next_incidences
|
||||
.push(self.incidence_headers[v1.0].first_incidence.take());
|
||||
self.incidence_headers[v1.0].incidence_count += 1;
|
||||
self.incidence_headers[v1.0].first_incidence =
|
||||
Some(Incidence(self.incidence_vertices.len() - 1));
|
||||
self.incidences.push(IncidenceEntry {
|
||||
next: self.vertices[v1.0].first_incidence.take(),
|
||||
neighbor: v2,
|
||||
});
|
||||
self.vertices[v1.0].incidence_count += 1;
|
||||
self.vertices[v1.0].first_incidence = Some(Incidence(self.incidences.len() - 1));
|
||||
}
|
||||
|
||||
pub fn vertices(&self) -> impl Iterator<Item = Vertex> {
|
||||
(0..self.incidence_headers.len()).map(Vertex)
|
||||
(0..self.vertices.len()).map(Vertex)
|
||||
}
|
||||
|
||||
pub fn neighbors(&self, v: Vertex) -> impl Iterator<Item = Vertex> {
|
||||
VertexNeighborIterator {
|
||||
graph: self,
|
||||
incidence: self.incidence_headers[v.0].first_incidence,
|
||||
incidence: self.vertices[v.0].first_incidence,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user