diff --git a/Cargo.toml b/Cargo.toml index a994a96..e4fc4a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "grapherity" -version = "0.1.0" +version = "0.2.0" authors = ["Stefan Müller"] edition = "2024" rust-version = "1.85.0" diff --git a/src/algorithms.rs b/src/algorithms.rs index a2620b8..730ed4c 100644 --- a/src/algorithms.rs +++ b/src/algorithms.rs @@ -261,14 +261,14 @@ where (visited, None) } -pub fn find_path(graph: &G, source: G::Vertex, target: G::Vertex) -> Option> +pub fn dfs_find_path(graph: &G, source: G::Vertex, target: G::Vertex) -> Option> where G: GraphTopology, { - find_path_where(graph, source, |v| v == target) + dfs_find_path_where(graph, source, |v| v == target) } -pub fn find_path_where(graph: &G, source: G::Vertex, predicate: P) -> Option> +pub fn dfs_find_path_where(graph: &G, source: G::Vertex, predicate: P) -> Option> where G: GraphTopology, P: Fn(G::Vertex) -> bool, diff --git a/src/models.rs b/src/models.rs index f1601bc..5f556aa 100644 --- a/src/models.rs +++ b/src/models.rs @@ -1,2 +1,5 @@ pub mod append_graph; pub mod graph; + +pub use append_graph::{AppendGraph, AppendGraphEdgeMap, AppendGraphVertexMap}; +pub use graph::{Graph, GraphEdgeMap, GraphVertexMap}; diff --git a/src/models/append_graph.rs b/src/models/append_graph.rs index 65657f8..9ccfef7 100644 --- a/src/models/append_graph.rs +++ b/src/models/append_graph.rs @@ -5,9 +5,12 @@ use crate::traits::{GraphTopology, IncidenceCursor}; pub struct Vertex(usize); #[derive(Copy, Clone, PartialEq, Eq, Debug)] -pub struct Incidence(usize); +pub struct Edge(usize); -impl Incidence { +pub type AppendGraphVertexMap = VertexMap; +pub type AppendGraphEdgeMap = EdgeMap; + +impl Edge { fn normalize(&self) -> Self { Self(self.0 & !1) } @@ -15,22 +18,22 @@ impl Incidence { struct VertexIncidenceHeader { incidence_count: usize, - first_incidence: Option, + first_incidence: Option, } #[derive(Copy, Clone)] struct IncidenceEntry { - next: Option, + next: Option, adjacent: Vertex, } #[derive(Copy, Clone)] pub struct AppendGraphIncidenceCursor { - incidence: Option, + incidence: Option, } impl IncidenceCursor for AppendGraphIncidenceCursor { - fn next(&mut self, graph: &AppendGraph) -> Option<(Vertex, Incidence)> { + fn next(&mut self, graph: &AppendGraph) -> Option<(Vertex, Edge)> { graph.step_incidence(&mut self.incidence) } } @@ -56,15 +59,15 @@ impl AppendGraph { adjacent: v2, }); self.vertices[v1.0].incidence_count += 1; - self.vertices[v1.0].first_incidence = Some(Incidence(self.incidences.len() - 1)); + self.vertices[v1.0].first_incidence = Some(Edge(self.incidences.len() - 1)); } - fn raw_incidences(&self, v: Vertex) -> impl Iterator { + fn raw_incidences(&self, v: Vertex) -> impl Iterator { let mut incidence = self.vertices[v.0].first_incidence; std::iter::from_fn(move || self.step_incidence(&mut incidence)) } - fn step_incidence(&self, incidence: &mut Option) -> Option<(Vertex, Incidence)> { + fn step_incidence(&self, incidence: &mut Option) -> Option<(Vertex, Edge)> { let current = (*incidence)?; let entry = self.incidences[current.0]; *incidence = entry.next; @@ -80,7 +83,7 @@ impl Default for AppendGraph { impl GraphTopology for AppendGraph { type Vertex = Vertex; - type Edge = Incidence; + type Edge = Edge; type IncidenceCursor = AppendGraphIncidenceCursor; fn vertex_count(&self) -> usize { @@ -130,7 +133,7 @@ impl GraphTopology for AppendGraph { } fn edges(&self) -> impl Iterator { - (0..self.incidences.len()).step_by(2).map(Incidence) + (0..self.incidences.len()).step_by(2).map(Edge) } fn incident_edges(&self, v: Self::Vertex) -> impl Iterator { @@ -158,7 +161,7 @@ impl GraphTopology for AppendGraph { fn add_edge(&mut self, v1: Self::Vertex, v2: Self::Vertex) -> Self::Edge { self.add_incidence(v1, v2); self.add_incidence(v2, v1); - Incidence(self.incidences.len() - 2) + Edge(self.incidences.len() - 2) } } @@ -175,7 +178,7 @@ mod tests { let v1 = graph.add_vertex(); let v2 = graph.add_vertex(); let e = graph.add_edge(v1, v2); - let f = Incidence(e.0 + 1); + let f = Edge(e.0 + 1); let (u1, u2) = graph.incident_vertices(f); assert!( (u1 == v1 && u2 == v2) || (u1 == v2 && u2 == v1), diff --git a/src/models/graph.rs b/src/models/graph.rs index 309d268..ed86205 100644 --- a/src/models/graph.rs +++ b/src/models/graph.rs @@ -3,9 +3,11 @@ use typed_generational_arena::{Arena, Index}; use crate::maps::{EdgeMap, VertexMap}; use crate::traits::{GraphTopology, GraphTopologyDeletion, IncidenceCursor}; -type Vertex = Index; +pub type Vertex = Index; +pub type Edge = Index; -type Edge = Index; +pub type GraphVertexMap = VertexMap; +pub type GraphEdgeMap = EdgeMap; #[derive(Copy, Clone, PartialEq, Eq, Debug)] struct VertexSlot(usize); diff --git a/src/testing/bfs_testing.rs b/src/testing/bfs_testing.rs index 532d289..709ac9f 100644 --- a/src/testing/bfs_testing.rs +++ b/src/testing/bfs_testing.rs @@ -1,3 +1,4 @@ +#[doc(hidden)] #[macro_export] macro_rules! bfs_tests { ($T:ty) => { diff --git a/src/testing/dfs_testing.rs b/src/testing/dfs_testing.rs index 205f020..78238c8 100644 --- a/src/testing/dfs_testing.rs +++ b/src/testing/dfs_testing.rs @@ -1,3 +1,4 @@ +#[doc(hidden)] #[macro_export] macro_rules! dfs_tests { ($T:ty) => { diff --git a/src/testing/dijkstra_testing.rs b/src/testing/dijkstra_testing.rs index bbb2579..3815c4e 100644 --- a/src/testing/dijkstra_testing.rs +++ b/src/testing/dijkstra_testing.rs @@ -1,3 +1,4 @@ +#[doc(hidden)] #[macro_export] macro_rules! dijkstra_tests { ($T:ty) => { diff --git a/src/testing/find_path_testing.rs b/src/testing/find_path_testing.rs index c7cfbdc..c9c4e42 100644 --- a/src/testing/find_path_testing.rs +++ b/src/testing/find_path_testing.rs @@ -1,3 +1,4 @@ +#[doc(hidden)] #[macro_export] macro_rules! find_path_tests { ($T:ty) => { @@ -7,7 +8,7 @@ macro_rules! find_path_tests { let mut graph = <$T>::new(); let v = graph.add_vertex(); assert_eq!( - $crate::algorithms::find_path(&graph, v, v), + $crate::algorithms::dfs_find_path(&graph, v, v), Some(vec![]), "path from source to itself should be empty" ); @@ -17,7 +18,7 @@ macro_rules! find_path_tests { fn find_path_disconnected() { let (graph, vertices) = make_test_graph_disconnected(); assert_eq!( - $crate::algorithms::find_path(&graph, vertices[0], vertices[1]), + $crate::algorithms::dfs_find_path(&graph, vertices[0], vertices[1]), None, "no path should exist to disconnected vertex" ); @@ -30,7 +31,7 @@ macro_rules! find_path_tests { let v1 = graph.add_vertex(); let v2 = graph.add_vertex(); let e = graph.add_edge(v1, v2); - let path = $crate::algorithms::find_path(&graph, v1, v2) + let path = $crate::algorithms::dfs_find_path(&graph, v1, v2) .expect("path should exist between adjacent vertices"); assert_eq!( path.len(), @@ -43,7 +44,7 @@ macro_rules! find_path_tests { #[test] fn find_path() { let (graph, vertices, _, _) = make_test_graph(); - let path = $crate::algorithms::find_path(&graph, vertices[0], vertices[9]) + let path = $crate::algorithms::dfs_find_path(&graph, vertices[0], vertices[9]) .expect(&format!( "path should exist between connected vertices {:?} and {:?}", vertices[0], vertices[9] @@ -57,7 +58,7 @@ macro_rules! find_path_tests { let mut graph = <$T>::new(); let v = graph.add_vertex(); assert_eq!( - $crate::algorithms::find_path_where(&graph, v, |u| u == v), + $crate::algorithms::dfs_find_path_where(&graph, v, |u| u == v), Some(vec![]), "path from source to itself should be empty" ); @@ -67,7 +68,7 @@ macro_rules! find_path_tests { fn find_path_where_disconnected() { let (graph, vertices) = make_test_graph_disconnected(); assert_eq!( - $crate::algorithms::find_path_where(&graph, vertices[0], |v| v == vertices[1]), + $crate::algorithms::dfs_find_path_where(&graph, vertices[0], |v| v == vertices[1]), None, "no path should exist to disconnected vertex" ); @@ -77,7 +78,7 @@ macro_rules! find_path_tests { fn find_path_where_no_match() { let (graph, vertices, _, _) = make_test_graph(); assert_eq!( - $crate::algorithms::find_path_where(&graph, vertices[0], |_| false), + $crate::algorithms::dfs_find_path_where(&graph, vertices[0], |_| false), None, "no path should exist when predicate never matches" ); @@ -87,7 +88,7 @@ macro_rules! find_path_tests { fn find_path_where() { let (graph, vertices, _, _) = make_test_graph(); let path = - $crate::algorithms::find_path_where(&graph, vertices[0], |v| v == vertices[9]) + $crate::algorithms::dfs_find_path_where(&graph, vertices[0], |v| v == vertices[9]) .expect(&format!( "path should exist between connected vertices {:?} and {:?}", vertices[0], vertices[9] diff --git a/src/testing/graph_topology_testing.rs b/src/testing/graph_topology_testing.rs index de09aaf..400290c 100644 --- a/src/testing/graph_topology_testing.rs +++ b/src/testing/graph_topology_testing.rs @@ -1,3 +1,4 @@ +#[doc(hidden)] #[macro_export] macro_rules! graph_topology_test_fixtures { ($T:ty) => { @@ -101,6 +102,7 @@ macro_rules! graph_topology_test_fixtures { }; } +#[doc(hidden)] #[macro_export] macro_rules! graph_topology_tests { ($T:ty) => { @@ -748,6 +750,7 @@ macro_rules! graph_topology_tests { }; } +#[doc(hidden)] #[macro_export] macro_rules! graph_topology_deletion_tests { ($T:ty) => { diff --git a/src/testing/maps_testing.rs b/src/testing/maps_testing.rs index fdac415..202b2db 100644 --- a/src/testing/maps_testing.rs +++ b/src/testing/maps_testing.rs @@ -1,3 +1,4 @@ +#[doc(hidden)] #[macro_export] macro_rules! vertex_map_tests { ($T:ty) => { @@ -78,6 +79,7 @@ macro_rules! vertex_map_tests { }; } +#[doc(hidden)] #[macro_export] macro_rules! vertex_map_deletion_tests { ($T:ty) => { @@ -130,6 +132,7 @@ macro_rules! vertex_map_deletion_tests { }; } +#[doc(hidden)] #[macro_export] macro_rules! edge_map_tests { ($T:ty) => { @@ -222,6 +225,7 @@ macro_rules! edge_map_tests { }; } +#[doc(hidden)] #[macro_export] macro_rules! edge_map_deletion_tests { ($T:ty) => { diff --git a/tests/bfs.rs b/tests/bfs.rs index 52996e5..293ea12 100644 --- a/tests/bfs.rs +++ b/tests/bfs.rs @@ -1,12 +1,12 @@ mod append_graph_tests { - use grapherity::models::append_graph::AppendGraph; + use grapherity::models::AppendGraph; grapherity::graph_topology_test_fixtures!(AppendGraph); grapherity::bfs_tests!(AppendGraph); } mod graph_tests { - use grapherity::models::graph::Graph; + use grapherity::models::Graph; grapherity::graph_topology_test_fixtures!(Graph); grapherity::bfs_tests!(Graph); diff --git a/tests/dfs.rs b/tests/dfs.rs index 4524c57..ec7e039 100644 --- a/tests/dfs.rs +++ b/tests/dfs.rs @@ -1,12 +1,12 @@ mod append_graph_tests { - use grapherity::models::append_graph::AppendGraph; + use grapherity::models::AppendGraph; grapherity::graph_topology_test_fixtures!(AppendGraph); grapherity::dfs_tests!(AppendGraph); } mod graph_tests { - use grapherity::models::graph::Graph; + use grapherity::models::Graph; grapherity::graph_topology_test_fixtures!(Graph); grapherity::dfs_tests!(Graph); diff --git a/tests/dijkstra.rs b/tests/dijkstra.rs index 5a54b49..b495546 100644 --- a/tests/dijkstra.rs +++ b/tests/dijkstra.rs @@ -1,12 +1,12 @@ mod append_graph_tests { - use grapherity::models::append_graph::AppendGraph; + use grapherity::models::AppendGraph; grapherity::graph_topology_test_fixtures!(AppendGraph); grapherity::dijkstra_tests!(AppendGraph); } mod graph_tests { - use grapherity::models::graph::Graph; + use grapherity::models::Graph; grapherity::graph_topology_test_fixtures!(Graph); grapherity::dijkstra_tests!(Graph); diff --git a/tests/find_path.rs b/tests/find_path.rs index c46085e..7f296c8 100644 --- a/tests/find_path.rs +++ b/tests/find_path.rs @@ -1,12 +1,12 @@ mod append_graph_tests { - use grapherity::models::append_graph::AppendGraph; + use grapherity::models::AppendGraph; grapherity::graph_topology_test_fixtures!(AppendGraph); grapherity::find_path_tests!(AppendGraph); } mod graph_tests { - use grapherity::models::graph::Graph; + use grapherity::models::Graph; grapherity::graph_topology_test_fixtures!(Graph); grapherity::find_path_tests!(Graph); diff --git a/tests/maps.rs b/tests/maps.rs index 0b4e2f7..b936db9 100644 --- a/tests/maps.rs +++ b/tests/maps.rs @@ -1,24 +1,24 @@ mod append_graph_vertex_map_tests { - use grapherity::models::append_graph::AppendGraph; + use grapherity::models::AppendGraph; grapherity::vertex_map_tests!(AppendGraph); } mod append_graph_edge_map_tests { - use grapherity::models::append_graph::AppendGraph; + use grapherity::models::AppendGraph; grapherity::edge_map_tests!(AppendGraph); } mod graph_vertex_map_tests { - use grapherity::models::graph::Graph; + use grapherity::models::Graph; grapherity::vertex_map_tests!(Graph); grapherity::vertex_map_deletion_tests!(Graph); } mod graph_edge_map_tests { - use grapherity::models::graph::Graph; + use grapherity::models::Graph; grapherity::edge_map_tests!(Graph); grapherity::edge_map_deletion_tests!(Graph);