Change generate_as and petersen to return Vecs instead of arrays

This commit is contained in:
2026-10-22 20:30:24 +02:00
parent baa6775156
commit f5bb3ad07a
+23 -16
View File
@@ -3,11 +3,11 @@
use crate::models::AppendGraph;
use crate::traits::{GraphTopology, GraphTopologyAddition, GraphTopologyConversion};
/// Builds a graph of type `G` by running a generator on a temporary [`AppendGraph`].
/// Builds a graph of type `G` by running a generator on a temporary graph.
///
/// `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`].
/// `generate_as` fills a temporary [`AppendGraph`] 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
///
@@ -16,21 +16,26 @@ use crate::traits::{GraphTopology, GraphTopologyAddition, GraphTopologyConversio
/// use grapherity::generators;
/// use grapherity::models::FrozenGraph;
///
/// let (graph, vertices, edges) = generators::generate_as::<FrozenGraph, _, _>(generators::petersen);
/// // Generates a parameterless Petersen graph as a FrozenGraph model.
/// 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));
///
/// // Generates a parameterized complete graph as a FrozenGraph model.
/// let (graph, vertices, edges) = generators::generate_as::<FrozenGraph>(|g| generators::complete(g, 5));
/// assert_eq!(graph.vertex_count(), 5);
/// assert!(graph.vertices().all(|v| graph.degree(v) == 4));
/// ```
///
/// [`FrozenGraph`]: crate::models::FrozenGraph
pub fn generate_as<G, const V: usize, const E: usize>(
pub fn generate_as<G>(
generator: impl FnOnce(
&mut AppendGraph,
) -> (
[<AppendGraph as GraphTopology>::Vertex; V],
[<AppendGraph as GraphTopology>::Edge; E],
Vec<<AppendGraph as GraphTopology>::Vertex>,
Vec<<AppendGraph as GraphTopology>::Edge>,
),
) -> (G, [G::Vertex; V], [G::Edge; E])
) -> (G, Vec<G::Vertex>, Vec<G::Edge>)
where
G: GraphTopologyConversion,
{
@@ -39,8 +44,8 @@ where
let (graph, iso) = G::convert(&builder);
(
graph,
vertices.map(|v| iso.vertex(v)),
edges.map(|e| iso.edge(e)),
vertices.into_iter().map(|v| iso.vertex(v)).collect(),
edges.into_iter().map(|e| iso.edge(e)).collect(),
)
}
@@ -112,14 +117,15 @@ pub fn complete<G: GraphTopologyAddition>(graph: &mut G, n: usize) -> (Vec<G::Ve
/// vertices and edges. This graph is also the Kneser graph *KG(5, 2)*.
///
/// [Petersen graph]: https://en.wikipedia.org/wiki/Petersen_graph
pub fn petersen<G: GraphTopologyAddition>(graph: &mut G) -> ([G::Vertex; 10], [G::Edge; 15]) {
pub fn petersen<G: GraphTopologyAddition>(graph: &mut G) -> (Vec<G::Vertex>, Vec<G::Edge>) {
const N: usize = 5;
const K: usize = 2;
graph.reserve_vertices(10);
graph.reserve_edges(15);
let vertices = core::array::from_fn(|_| graph.add_vertex());
let edges = core::array::from_fn(|j| {
let vertices: Vec<_> = (0..10).map(|_| graph.add_vertex()).collect();
let edges = (0..15)
.map(|j| {
let i = j / 3;
match j % 3 {
0 => {
@@ -132,7 +138,8 @@ pub fn petersen<G: GraphTopologyAddition>(graph: &mut G) -> ([G::Vertex; 10], [G
}
_ => graph.add_edge(vertices[i], vertices[i + N]),
}
});
})
.collect();
(vertices, edges)
}