Add Graph::new()

This commit is contained in:
2025-10-08 17:04:25 +02:00
parent 679b597f92
commit 66e1498c76
2 changed files with 9 additions and 6 deletions
+1 -6
View File
@@ -6,12 +6,7 @@ use models::Vertex;
fn main() { 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::new();
// TODO: The initializations should happen in an initializer or constructor
incidence_headers: vec![],
next_incidences: vec![],
incidence_vertices: vec![],
};
let vertices: [Vertex; 10] = core::array::from_fn(|_| graph.add_vertex()); let vertices: [Vertex; 10] = core::array::from_fn(|_| graph.add_vertex());
graph.add_edge(vertices[0], vertices[1]); graph.add_edge(vertices[0], vertices[1]);
graph.add_edge(vertices[1], vertices[2]); graph.add_edge(vertices[1], vertices[2]);
+8
View File
@@ -26,6 +26,14 @@ pub struct Graph {
// TODO: Add iterator of edges. // TODO: Add iterator of edges.
// TODO: Add iterator of vertex neighbors, see 'fn add_adjacent()'. // TODO: Add iterator of vertex neighbors, see 'fn add_adjacent()'.
impl Graph { impl Graph {
pub fn new() -> Graph {
Graph {
incidence_headers: vec![],
next_incidences: vec![],
incidence_vertices: vec![],
}
}
pub fn vertex_count(&self) -> usize { pub fn vertex_count(&self) -> usize {
self.incidence_headers.len() self.incidence_headers.len()
} }