Add GraphTopology::incident_vertices and tests
This commit is contained in:
@@ -120,6 +120,12 @@ impl GraphTopology for AppendGraph {
|
||||
.map(|(v, _)| v)
|
||||
}
|
||||
|
||||
fn incident_vertices(&self, e: Self::Edge) -> (Self::Vertex, Self::Vertex) {
|
||||
let v2 = self.incidences[e.0].adjacent;
|
||||
let v1 = self.incidences[e.0 ^ 1].adjacent;
|
||||
(v1, v2)
|
||||
}
|
||||
|
||||
fn edges(&self) -> impl Iterator<Item = Self::Edge> {
|
||||
(0..self.incidences.len()).step_by(2).map(Incidence)
|
||||
}
|
||||
@@ -153,4 +159,18 @@ mod tests {
|
||||
|
||||
crate::graph_topology_test_fixtures!(AppendGraph);
|
||||
crate::graph_topology_tests!(AppendGraph);
|
||||
|
||||
#[test]
|
||||
fn incident_vertices_paired_index() {
|
||||
let mut graph = AppendGraph::new();
|
||||
let v1 = graph.add_vertex();
|
||||
let v2 = graph.add_vertex();
|
||||
let e = graph.add_edge(v1, v2);
|
||||
let f = Incidence(e.0 + 1);
|
||||
let (u1, u2) = graph.incident_vertices(f);
|
||||
assert!(
|
||||
(u1 == v1 && u2 == v2) || (u1 == v2 && u2 == v1),
|
||||
"unexpected incident vertices {u1:?} and {u2:?} for edge {f:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,6 +176,19 @@ impl GraphTopology for Graph {
|
||||
.map(|(vs, _)| self.vertices.get_idx(vs.0).unwrap())
|
||||
}
|
||||
|
||||
fn incident_vertices(&self, e: Self::Edge) -> (Self::Vertex, Self::Vertex) {
|
||||
let v2 = self
|
||||
.vertices
|
||||
.get_idx(self.incidences[e].adjacent.0)
|
||||
.unwrap();
|
||||
let f = self.incidences.get_idx(e.arr_idx() ^ 1).unwrap();
|
||||
let v1 = self
|
||||
.vertices
|
||||
.get_idx(self.incidences[f].adjacent.0)
|
||||
.unwrap();
|
||||
(v1, v2)
|
||||
}
|
||||
|
||||
fn edges(&self) -> impl Iterator<Item = Self::Edge> {
|
||||
self.incidences
|
||||
.iter()
|
||||
@@ -249,6 +262,23 @@ mod tests {
|
||||
crate::graph_topology_tests!(Graph);
|
||||
crate::graph_topology_deletion_tests!(Graph);
|
||||
|
||||
#[test]
|
||||
fn incident_vertices_paired_index() {
|
||||
let mut graph = Graph::new();
|
||||
let v1 = graph.add_vertex();
|
||||
let v2 = graph.add_vertex();
|
||||
let e = graph.add_edge(v1, v2);
|
||||
let f = graph
|
||||
.incidences
|
||||
.get_idx(e.arr_idx() + 1)
|
||||
.expect("paired index should be valid");
|
||||
let (u1, u2) = graph.incident_vertices(f);
|
||||
assert!(
|
||||
(u1 == v1 && u2 == v2) || (u1 == v2 && u2 == v1),
|
||||
"unexpected incident vertices {u1:?} and {u2:?} for edge {f:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delete_edge_paired_index() {
|
||||
let mut graph = Graph::new();
|
||||
|
||||
Reference in New Issue
Block a user