Initial release #1

Merged
warrence merged 102 commits from dev into main 2026-06-30 10:26:52 +02:00
Showing only changes of commit 02cd1a0dea - Show all commits
+13 -15
View File
@@ -21,6 +21,7 @@ struct IncidenceEntry {
next: Option<Incidence>, next: Option<Incidence>,
neighbor: Vertex, neighbor: Vertex,
} }
struct VertexNeighborIterator<'a> { struct VertexNeighborIterator<'a> {
graph: &'a AppendGraph, graph: &'a AppendGraph,
incidence: Option<Incidence>, incidence: Option<Incidence>,
@@ -85,7 +86,7 @@ impl GraphTopology for AppendGraph {
} }
fn are_adjacent(&self, v1: Self::Vertex, v2: Self::Vertex) -> bool { 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> { fn vertices(&self) -> impl Iterator<Item = Self::Vertex> {
@@ -183,9 +184,8 @@ mod tests {
fn are_adjacent_vertex_self() { fn are_adjacent_vertex_self() {
let mut graph = AppendGraph::new(); let mut graph = AppendGraph::new();
let v = graph.add_vertex(); let v = graph.add_vertex();
assert_eq!( assert!(
graph.are_adjacent(v, v), !graph.are_adjacent(v, v),
false,
"should not be adjacent to itself" "should not be adjacent to itself"
); );
} }
@@ -290,11 +290,11 @@ mod tests {
assert_eq!( assert_eq!(
graph.neighbors(vertices[4]).count(), graph.neighbors(vertices[4]).count(),
4, 4,
"unexpected vertex count" "unexpected neighbor count"
); );
// Expects each neighbor to appear exactly once. This will not work if there are multiple edges. // 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]) { for v in graph.neighbors(vertices[4]) {
assert_eq!( assert_eq!(
neighbors.iter().filter(|&x| *x == v).count(), neighbors.iter().filter(|&x| *x == v).count(),
@@ -313,9 +313,8 @@ mod tests {
assert_eq!(graph.vertex_count(), 1, "unexpected vertex count"); assert_eq!(graph.vertex_count(), 1, "unexpected vertex count");
assert_eq!(graph.edge_count(), 1, "unexpected edge count"); assert_eq!(graph.edge_count(), 1, "unexpected edge count");
assert_eq!(graph.degree(v), 2, "unexpected degree"); assert_eq!(graph.degree(v), 2, "unexpected degree");
assert_eq!( assert!(
graph.are_adjacent(v, v), graph.are_adjacent(v, v),
true,
"vertex with loop edge should be adjacent to itself" "vertex with loop edge should be adjacent to itself"
); );
let mut neighbors = graph.neighbors(v); let mut neighbors = graph.neighbors(v);
@@ -334,10 +333,10 @@ mod tests {
#[test] #[test]
fn multiple_edges() { fn multiple_edges() {
let mult = 3; let k = 3;
let mut graph = AppendGraph::new(); let mut graph = AppendGraph::new();
let vertices = [graph.add_vertex(), graph.add_vertex()]; let vertices = [graph.add_vertex(), graph.add_vertex()];
for _ in 0..mult { for _ in 0..k {
graph.add_edge(vertices[0], vertices[1]); graph.add_edge(vertices[0], vertices[1]);
} }
assert_eq!( assert_eq!(
@@ -345,18 +344,17 @@ mod tests {
vertices.len(), vertices.len(),
"unexpected vertex count" "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 { 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]), graph.are_adjacent(vertices[0], vertices[1]),
true,
"should be adjacent" "should be adjacent"
); );
for i in 0..2 { for i in 0..2 {
let mut neighbors = graph.neighbors(vertices[i]); let mut neighbors = graph.neighbors(vertices[i]);
for j in 0..mult { for j in 0..k {
assert_eq!( assert_eq!(
neighbors.next(), neighbors.next(),
Some(vertices[1 - i]), Some(vertices[1 - i]),