Initial release #1
+13
-15
@@ -21,6 +21,7 @@ struct IncidenceEntry {
|
||||
next: Option<Incidence>,
|
||||
neighbor: Vertex,
|
||||
}
|
||||
|
||||
struct VertexNeighborIterator<'a> {
|
||||
graph: &'a AppendGraph,
|
||||
incidence: Option<Incidence>,
|
||||
@@ -85,7 +86,7 @@ impl GraphTopology for AppendGraph {
|
||||
}
|
||||
|
||||
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> {
|
||||
@@ -183,9 +184,8 @@ mod tests {
|
||||
fn are_adjacent_vertex_self() {
|
||||
let mut graph = AppendGraph::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"
|
||||
);
|
||||
}
|
||||
@@ -290,11 +290,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(),
|
||||
@@ -313,9 +313,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);
|
||||
@@ -334,10 +333,10 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn multiple_edges() {
|
||||
let mult = 3;
|
||||
let k = 3;
|
||||
let mut graph = AppendGraph::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!(
|
||||
@@ -345,18 +344,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]),
|
||||
|
||||
Reference in New Issue
Block a user