Rename Graph to AppendGraph for future other graph models
This commit is contained in:
+18
-18
@@ -22,7 +22,7 @@ struct IncidenceEntry {
|
|||||||
neighbor: Vertex,
|
neighbor: Vertex,
|
||||||
}
|
}
|
||||||
struct VertexNeighborIterator<'a> {
|
struct VertexNeighborIterator<'a> {
|
||||||
graph: &'a Graph,
|
graph: &'a AppendGraph,
|
||||||
incidence: Option<Incidence>,
|
incidence: Option<Incidence>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,15 +42,15 @@ impl<'a> Iterator for VertexNeighborIterator<'a> {
|
|||||||
// TODO: Add function to delete vertices.
|
// TODO: Add function to delete vertices.
|
||||||
// TODO: Add function to delete edges.
|
// TODO: Add function to delete edges.
|
||||||
// TODO: Add iterator of edges.
|
// TODO: Add iterator of edges.
|
||||||
pub struct Graph {
|
pub struct AppendGraph {
|
||||||
// TODO: Index 'incidence_headers' by a 'Vertex' instead of 'Vertex.0'?
|
// TODO: Index 'incidence_headers' by a 'Vertex' instead of 'Vertex.0'?
|
||||||
vertices: Vec<VertexIncidenceHeader>,
|
vertices: Vec<VertexIncidenceHeader>,
|
||||||
incidences: Vec<IncidenceEntry>,
|
incidences: Vec<IncidenceEntry>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Graph {
|
impl AppendGraph {
|
||||||
pub fn new() -> Graph {
|
pub fn new() -> Self {
|
||||||
Graph {
|
Self {
|
||||||
vertices: vec![],
|
vertices: vec![],
|
||||||
incidences: vec![],
|
incidences: vec![],
|
||||||
}
|
}
|
||||||
@@ -66,7 +66,7 @@ impl Graph {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GraphTopology for Graph {
|
impl GraphTopology for AppendGraph {
|
||||||
type Vertex = Vertex;
|
type Vertex = Vertex;
|
||||||
|
|
||||||
type Edge = Incidence;
|
type Edge = Incidence;
|
||||||
@@ -119,14 +119,14 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn add_vertex() {
|
fn add_vertex() {
|
||||||
let mut graph = Graph::new();
|
let mut graph = AppendGraph::new();
|
||||||
let v = graph.add_vertex();
|
let v = graph.add_vertex();
|
||||||
assert_ne!(graph.add_vertex(), v, "unexpected duplicate vertex");
|
assert_ne!(graph.add_vertex(), v, "unexpected duplicate vertex");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn vertex_count_empty() {
|
fn vertex_count_empty() {
|
||||||
let graph = Graph::new();
|
let graph = AppendGraph::new();
|
||||||
assert_eq!(graph.vertex_count(), 0, "unexpected vertex count");
|
assert_eq!(graph.vertex_count(), 0, "unexpected vertex count");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,7 +138,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn edge_count_empty() {
|
fn edge_count_empty() {
|
||||||
let graph = Graph::new();
|
let graph = AppendGraph::new();
|
||||||
assert_eq!(graph.edge_count(), 0, "unexpected edge count");
|
assert_eq!(graph.edge_count(), 0, "unexpected edge count");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,7 +150,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn degree_zero() {
|
fn degree_zero() {
|
||||||
let mut graph = Graph::new();
|
let mut graph = AppendGraph::new();
|
||||||
let v = graph.add_vertex();
|
let v = graph.add_vertex();
|
||||||
assert_eq!(graph.degree(v), 0, "unexpected non-zero degree");
|
assert_eq!(graph.degree(v), 0, "unexpected non-zero degree");
|
||||||
}
|
}
|
||||||
@@ -171,7 +171,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn are_adjacent_vertex_self() {
|
fn are_adjacent_vertex_self() {
|
||||||
let mut graph = Graph::new();
|
let mut graph = AppendGraph::new();
|
||||||
let v = graph.add_vertex();
|
let v = graph.add_vertex();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
graph.are_adjacent(v, v),
|
graph.are_adjacent(v, v),
|
||||||
@@ -182,7 +182,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn are_adjacent_single_edge() {
|
fn are_adjacent_single_edge() {
|
||||||
let mut graph = Graph::new();
|
let mut graph = AppendGraph::new();
|
||||||
let v1 = graph.add_vertex();
|
let v1 = graph.add_vertex();
|
||||||
let v2 = graph.add_vertex();
|
let v2 = graph.add_vertex();
|
||||||
assert!(!graph.are_adjacent(v1, v2), "should not be adjacent");
|
assert!(!graph.are_adjacent(v1, v2), "should not be adjacent");
|
||||||
@@ -239,7 +239,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn vertices_empty() {
|
fn vertices_empty() {
|
||||||
let graph = Graph::new();
|
let graph = AppendGraph::new();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
graph.vertices().count(),
|
graph.vertices().count(),
|
||||||
0,
|
0,
|
||||||
@@ -264,7 +264,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn neighbors_empty() {
|
fn neighbors_empty() {
|
||||||
let mut graph = Graph::new();
|
let mut graph = AppendGraph::new();
|
||||||
let vertex = graph.add_vertex();
|
let vertex = graph.add_vertex();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
graph.neighbors(vertex).count(),
|
graph.neighbors(vertex).count(),
|
||||||
@@ -297,7 +297,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn loop_edge() {
|
fn loop_edge() {
|
||||||
let mut graph = Graph::new();
|
let mut graph = AppendGraph::new();
|
||||||
let v = graph.add_vertex();
|
let v = graph.add_vertex();
|
||||||
graph.add_edge(v, v);
|
graph.add_edge(v, v);
|
||||||
assert_eq!(graph.vertex_count(), 1, "unexpected vertex count");
|
assert_eq!(graph.vertex_count(), 1, "unexpected vertex count");
|
||||||
@@ -325,7 +325,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn multiple_edges() {
|
fn multiple_edges() {
|
||||||
let mult = 3;
|
let mult = 3;
|
||||||
let mut graph = Graph::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..mult {
|
||||||
graph.add_edge(vertices[0], vertices[1]);
|
graph.add_edge(vertices[0], vertices[1]);
|
||||||
@@ -364,8 +364,8 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_test_graph() -> (Graph, [Vertex; 10]) {
|
fn make_test_graph() -> (AppendGraph, [Vertex; 10]) {
|
||||||
let mut graph = Graph::new();
|
let mut graph = AppendGraph::new();
|
||||||
let vertices: [Vertex; 10] = core::array::from_fn(|_| graph.add_vertex());
|
let vertices: [Vertex; 10] = core::array::from_fn(|_| graph.add_vertex());
|
||||||
graph.add_edge(vertices[0], vertices[1]);
|
graph.add_edge(vertices[0], vertices[1]);
|
||||||
graph.add_edge(vertices[1], vertices[2]);
|
graph.add_edge(vertices[1], vertices[2]);
|
||||||
|
|||||||
+5
-5
@@ -1,10 +1,10 @@
|
|||||||
use grapherity::algorithms;
|
use grapherity::algorithms;
|
||||||
use grapherity::models::Graph;
|
use grapherity::models::AppendGraph;
|
||||||
use grapherity::traits::GraphTopology;
|
use grapherity::traits::GraphTopology;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn dijkstra_single_vertex() {
|
fn dijkstra_single_vertex() {
|
||||||
let mut graph = Graph::new();
|
let mut graph = AppendGraph::new();
|
||||||
let v1 = graph.add_vertex();
|
let v1 = graph.add_vertex();
|
||||||
let (distances, predecessors) = algorithms::dijkstra(&graph, v1);
|
let (distances, predecessors) = algorithms::dijkstra(&graph, v1);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@@ -30,7 +30,7 @@ fn dijkstra_single_vertex() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn dijkstra_disconnected() {
|
fn dijkstra_disconnected() {
|
||||||
let mut graph = Graph::new();
|
let mut graph = AppendGraph::new();
|
||||||
let v1 = graph.add_vertex();
|
let v1 = graph.add_vertex();
|
||||||
graph.add_vertex();
|
graph.add_vertex();
|
||||||
let (distances, predecessors) = algorithms::dijkstra(&graph, v1);
|
let (distances, predecessors) = algorithms::dijkstra(&graph, v1);
|
||||||
@@ -107,8 +107,8 @@ fn dijkstra() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_test_graph() -> (Graph, [<Graph as GraphTopology>::Vertex; 10]) {
|
fn make_test_graph() -> (AppendGraph, [<AppendGraph as GraphTopology>::Vertex; 10]) {
|
||||||
let mut graph = Graph::new();
|
let mut graph = AppendGraph::new();
|
||||||
let vertices = core::array::from_fn::<_, 10, _>(|_| graph.add_vertex());
|
let vertices = core::array::from_fn::<_, 10, _>(|_| graph.add_vertex());
|
||||||
graph.add_edge(vertices[0], vertices[1]);
|
graph.add_edge(vertices[0], vertices[1]);
|
||||||
graph.add_edge(vertices[1], vertices[2]);
|
graph.add_edge(vertices[1], vertices[2]);
|
||||||
|
|||||||
Reference in New Issue
Block a user