Common neighbors #9

Merged
warrence merged 6 commits from common-neighbors into main 2026-10-16 20:30:32 +02:00
Showing only changes of commit 5b6ad13f0e - Show all commits
+43 -2
View File
@@ -1,6 +1,48 @@
//! Generators for common graphs. //! Generators for common graphs.
use crate::traits::{GraphTopology, GraphTopologyAddition}; use crate::models::AppendGraph;
use crate::traits::{GraphTopology, GraphTopologyAddition, GraphTopologyConversion};
/// Builds a graph of type `G` by running a generator on a temporary [`AppendGraph`].
///
/// `generate_as` fills a temporary graph and converts it to `G`. It returns the handles of the
/// elements it created as handles of the resulting graph. This makes it possible to obtain graph
/// types directly that do not implement [`GraphTopologyAddition`], such as [`FrozenGraph`].
///
/// # Examples
///
/// ```
/// use grapherity::generators;
/// use grapherity::models::FrozenGraph;
/// use grapherity::traits::GraphTopology;
///
/// let (graph, vertices, edges) = generators::generate_as::<FrozenGraph, _, _>(generators::petersen);
/// assert_eq!(graph.vertex_count(), 10);
/// assert_eq!(graph.edge_count(), 15);
/// assert!(graph.vertices().all(|v| graph.degree(v) == 3));
/// ```
///
/// [`FrozenGraph`]: crate::models::FrozenGraph
pub fn generate_as<G, const V: usize, const E: usize>(
generator: impl FnOnce(
&mut AppendGraph,
) -> (
[<AppendGraph as GraphTopology>::Vertex; V],
[<AppendGraph as GraphTopology>::Edge; E],
),
) -> (G, [G::Vertex; V], [G::Edge; E])
where
G: GraphTopologyConversion,
{
let mut builder = AppendGraph::new();
let (vertices, edges) = generator(&mut builder);
let (graph, iso) = G::convert(&builder);
(
graph,
vertices.map(|v| iso.vertex(v)),
edges.map(|e| iso.edge(e)),
)
}
/// Generates a Petersen graph. /// Generates a Petersen graph.
/// ///
@@ -43,7 +85,6 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::models::AppendGraph;
#[test] #[test]
fn petersen() { fn petersen() {