Add edge return type to GraphTopology::add_edge()

There is currently no use for the return type, but it will come.
This commit is contained in:
2026-04-22 14:28:21 +02:00
parent da9ba7b0ad
commit 9088dd4683
2 changed files with 7 additions and 3 deletions
+5 -2
View File
@@ -10,7 +10,7 @@ impl From<Vertex> for usize {
} }
#[derive(Copy, Clone, PartialEq, Eq)] #[derive(Copy, Clone, PartialEq, Eq)]
struct Incidence(usize); pub struct Incidence(usize);
struct VertexIncidenceHeader { struct VertexIncidenceHeader {
incidence_count: usize, incidence_count: usize,
@@ -69,6 +69,8 @@ impl Graph {
impl GraphTopology for Graph { impl GraphTopology for Graph {
type Vertex = Vertex; type Vertex = Vertex;
type Edge = Incidence;
fn vertex_count(&self) -> usize { fn vertex_count(&self) -> usize {
self.vertices.len() self.vertices.len()
} }
@@ -104,9 +106,10 @@ impl GraphTopology for Graph {
Vertex(self.vertices.len() - 1) Vertex(self.vertices.len() - 1)
} }
fn add_edge(&mut self, v1: Self::Vertex, v2: Self::Vertex) { fn add_edge(&mut self, v1: Self::Vertex, v2: Self::Vertex) -> Self::Edge {
self.add_incidence(v1, v2); self.add_incidence(v1, v2);
self.add_incidence(v2, v1); self.add_incidence(v2, v1);
Incidence(self.incidences.len() - 2)
} }
} }
+2 -1
View File
@@ -1,5 +1,6 @@
pub trait GraphTopology { pub trait GraphTopology {
type Vertex: Copy + Eq; type Vertex: Copy + Eq;
type Edge;
fn vertex_count(&self) -> usize; fn vertex_count(&self) -> usize;
fn edge_count(&self) -> usize; fn edge_count(&self) -> usize;
@@ -8,5 +9,5 @@ pub trait GraphTopology {
fn vertices(&self) -> impl Iterator<Item = Self::Vertex>; fn vertices(&self) -> impl Iterator<Item = Self::Vertex>;
fn neighbors(&self, v: Self::Vertex) -> impl Iterator<Item = Self::Vertex>; fn neighbors(&self, v: Self::Vertex) -> impl Iterator<Item = Self::Vertex>;
fn add_vertex(&mut self) -> Self::Vertex; fn add_vertex(&mut self) -> Self::Vertex;
fn add_edge(&mut self, v1: Self::Vertex, v2: Self::Vertex); fn add_edge(&mut self, v1: Self::Vertex, v2: Self::Vertex) -> Self::Edge;
} }