Fix same issues in Graph draft as well

This commit is contained in:
2026-04-23 18:23:54 +02:00
parent 02cd1a0dea
commit 67e16f50cd
+22 -18
View File
@@ -21,6 +21,7 @@ struct IncidenceEntry {
next: Option<Incidence>,
neighbor: Vertex,
}
struct VertexNeighborIterator<'a> {
graph: &'a Graph,
incidence: Option<Incidence>,
@@ -64,6 +65,12 @@ impl Graph {
}
}
impl Default for Graph {
fn default() -> Self {
Self::new()
}
}
impl GraphTopology for Graph {
type Vertex = Vertex;
@@ -82,7 +89,7 @@ impl GraphTopology for Graph {
}
fn are_adjacent(&self, v1: Self::Vertex, v2: Self::Vertex) -> bool {
self.neighbors(v1).find(|&x| x == v2).is_some()
self.neighbors(v1).any(|x| x == v2)
}
fn vertices(&self) -> impl Iterator<Item = Self::Vertex> {
@@ -180,9 +187,8 @@ mod tests {
fn are_adjacent_vertex_self() {
let mut graph = Graph::new();
let v = graph.add_vertex();
assert_eq!(
graph.are_adjacent(v, v),
false,
assert!(
!graph.are_adjacent(v, v),
"should not be adjacent to itself"
);
}
@@ -211,7 +217,7 @@ mod tests {
assert!(
graph.are_adjacent(vertices[9], vertices[5]),
"expected {:?} and {:?} to be adjacent",
vertices[0],
vertices[9],
vertices[5]
);
assert!(
@@ -287,11 +293,11 @@ mod tests {
assert_eq!(
graph.neighbors(vertices[4]).count(),
4,
"unexpected vertex count"
"unexpected neighbor count"
);
// Expects each neighbor to appear exactly once. This will not work if there are multiple edges.
let neighbors: Vec<Vertex> = vec![vertices[1], vertices[2], vertices[7], vertices[8]];
let neighbors = vec![vertices[1], vertices[2], vertices[7], vertices[8]];
for v in graph.neighbors(vertices[4]) {
assert_eq!(
neighbors.iter().filter(|&x| *x == v).count(),
@@ -310,9 +316,8 @@ mod tests {
assert_eq!(graph.vertex_count(), 1, "unexpected vertex count");
assert_eq!(graph.edge_count(), 1, "unexpected edge count");
assert_eq!(graph.degree(v), 2, "unexpected degree");
assert_eq!(
assert!(
graph.are_adjacent(v, v),
true,
"vertex with loop edge should be adjacent to itself"
);
let mut neighbors = graph.neighbors(v);
@@ -326,15 +331,15 @@ mod tests {
Some(v),
"vertex should be neighbor of itself twice"
);
assert_eq!(neighbors.next(), None, "two many neighbors from iterator");
assert_eq!(neighbors.next(), None, "too many neighbors from iterator");
}
#[test]
fn multiple_edges() {
let mult = 3;
let k = 3;
let mut graph = Graph::new();
let vertices = [graph.add_vertex(), graph.add_vertex()];
for _ in 0..mult {
for _ in 0..k {
graph.add_edge(vertices[0], vertices[1]);
}
assert_eq!(
@@ -342,18 +347,17 @@ mod tests {
vertices.len(),
"unexpected vertex count"
);
assert_eq!(graph.edge_count(), mult, "unexpected edge count");
assert_eq!(graph.edge_count(), k, "unexpected edge count");
for v in vertices {
assert_eq!(graph.degree(v), mult, "unexpected degree of {v:?}");
assert_eq!(graph.degree(v), k, "unexpected degree of {v:?}");
}
assert_eq!(
assert!(
graph.are_adjacent(vertices[0], vertices[1]),
true,
"should be adjacent"
);
for i in 0..2 {
let mut neighbors = graph.neighbors(vertices[i]);
for j in 0..mult {
for j in 0..k {
assert_eq!(
neighbors.next(),
Some(vertices[1 - i]),
@@ -365,7 +369,7 @@ mod tests {
assert_eq!(
neighbors.next(),
None,
"two many neighbors of {:?} from iterator",
"too many neighbors of {:?} from iterator",
vertices[i]
);
}