Add "algorithms" and "models" modules for existing code and related todos
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
// TODO: Vertex value must not be public.
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||
pub struct Vertex(pub usize);
|
||||
|
||||
// TODO: Incidence value must not be public.
|
||||
// TODO: Incidence must not be public.
|
||||
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||
pub struct Incidence(pub usize);
|
||||
|
||||
// TODO: VertexIncidenceHeader and its fields must not be public, but without iterators there is currently no other way to access vertex incidences.
|
||||
pub struct VertexIncidenceHeader {
|
||||
incidence_count: usize,
|
||||
pub first_incidence: Option<Incidence>,
|
||||
}
|
||||
|
||||
// TODO: Graph fields must not be public.
|
||||
pub struct Graph {
|
||||
// TODO: Index 'incidence_headers' by a 'Vertex' instead of 'Vertex.0'?
|
||||
pub incidence_headers: Vec<VertexIncidenceHeader>,
|
||||
pub next_incidences: Vec<Option<Incidence>>,
|
||||
pub incidence_vertices: Vec<Vertex>,
|
||||
}
|
||||
|
||||
// TODO: Add iterator of vertices.
|
||||
// TODO: Add iterator of edges.
|
||||
// TODO: Add iterator of vertex neighbors, see 'fn add_adjacent()'.
|
||||
impl Graph {
|
||||
pub fn vertex_count(&self) -> usize {
|
||||
self.incidence_headers.len()
|
||||
}
|
||||
|
||||
pub fn edge_count(&self) -> usize {
|
||||
self.next_incidences.len() / 2
|
||||
}
|
||||
|
||||
pub fn degree(&self, v: Vertex) -> usize {
|
||||
self.incidence_headers[v.0].incidence_count
|
||||
}
|
||||
|
||||
// TODO: 'fn are_adjacent()' could probably be done with a neighbor vertex iterator.
|
||||
pub fn are_adjacent(&self, v1: Vertex, v2: Vertex) -> bool {
|
||||
let Some(mut incidence) = self.incidence_headers[v1.0].first_incidence else {
|
||||
return false;
|
||||
};
|
||||
loop {
|
||||
if self.incidence_vertices[incidence.0] == v2 {
|
||||
return true;
|
||||
}
|
||||
let Some(next) = self.next_incidences[incidence.0] else {
|
||||
return false;
|
||||
};
|
||||
incidence = next;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: 'fn add_vertex()' should not be publicly accessible for all graph implementations.
|
||||
// TODO: 'fn add_vertex()' needs variants for custom vertex data.
|
||||
pub fn add_vertex(&mut self) -> Vertex {
|
||||
self.incidence_headers.push(VertexIncidenceHeader {
|
||||
incidence_count: 0,
|
||||
first_incidence: None,
|
||||
});
|
||||
Vertex(self.incidence_headers.len() - 1)
|
||||
}
|
||||
|
||||
// TODO: 'fn add_edge()' should not be publicly accessible for all graph implementations.
|
||||
// TODO: 'fn add_edge()' needs variants for custom edge data.
|
||||
pub fn add_edge(&mut self, v1: Vertex, v2: Vertex) {
|
||||
self.add_incidence(v1, v2);
|
||||
self.add_incidence(v2, v1);
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user