Change generate_as and petersen to return Vecs instead of arrays
This commit is contained in:
+34
-27
@@ -3,11 +3,11 @@
|
|||||||
use crate::models::AppendGraph;
|
use crate::models::AppendGraph;
|
||||||
use crate::traits::{GraphTopology, GraphTopologyAddition, GraphTopologyConversion};
|
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
|
/// `generate_as` fills a temporary [`AppendGraph`] and converts it to `G`. It returns the handles
|
||||||
/// elements it created as handles of the resulting graph. This makes it possible to obtain graph
|
/// of the elements it created as handles of the resulting graph. This makes it possible to obtain
|
||||||
/// types directly that do not implement [`GraphTopologyAddition`], such as [`FrozenGraph`].
|
/// graph types directly that do not implement [`GraphTopologyAddition`], such as [`FrozenGraph`].
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
@@ -16,21 +16,26 @@ use crate::traits::{GraphTopology, GraphTopologyAddition, GraphTopologyConversio
|
|||||||
/// use grapherity::generators;
|
/// use grapherity::generators;
|
||||||
/// use grapherity::models::FrozenGraph;
|
/// 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.vertex_count(), 10);
|
||||||
/// assert_eq!(graph.edge_count(), 15);
|
|
||||||
/// assert!(graph.vertices().all(|v| graph.degree(v) == 3));
|
/// 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
|
/// [`FrozenGraph`]: crate::models::FrozenGraph
|
||||||
pub fn generate_as<G, const V: usize, const E: usize>(
|
pub fn generate_as<G>(
|
||||||
generator: impl FnOnce(
|
generator: impl FnOnce(
|
||||||
&mut AppendGraph,
|
&mut AppendGraph,
|
||||||
) -> (
|
) -> (
|
||||||
[<AppendGraph as GraphTopology>::Vertex; V],
|
Vec<<AppendGraph as GraphTopology>::Vertex>,
|
||||||
[<AppendGraph as GraphTopology>::Edge; E],
|
Vec<<AppendGraph as GraphTopology>::Edge>,
|
||||||
),
|
),
|
||||||
) -> (G, [G::Vertex; V], [G::Edge; E])
|
) -> (G, Vec<G::Vertex>, Vec<G::Edge>)
|
||||||
where
|
where
|
||||||
G: GraphTopologyConversion,
|
G: GraphTopologyConversion,
|
||||||
{
|
{
|
||||||
@@ -39,8 +44,8 @@ where
|
|||||||
let (graph, iso) = G::convert(&builder);
|
let (graph, iso) = G::convert(&builder);
|
||||||
(
|
(
|
||||||
graph,
|
graph,
|
||||||
vertices.map(|v| iso.vertex(v)),
|
vertices.into_iter().map(|v| iso.vertex(v)).collect(),
|
||||||
edges.map(|e| iso.edge(e)),
|
edges.into_iter().map(|e| iso.edge(e)).collect(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,27 +117,29 @@ 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)*.
|
/// vertices and edges. This graph is also the Kneser graph *KG(5, 2)*.
|
||||||
///
|
///
|
||||||
/// [Petersen graph]: https://en.wikipedia.org/wiki/Petersen_graph
|
/// [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 N: usize = 5;
|
||||||
const K: usize = 2;
|
const K: usize = 2;
|
||||||
|
|
||||||
graph.reserve_vertices(10);
|
graph.reserve_vertices(10);
|
||||||
graph.reserve_edges(15);
|
graph.reserve_edges(15);
|
||||||
let vertices = core::array::from_fn(|_| graph.add_vertex());
|
let vertices: Vec<_> = (0..10).map(|_| graph.add_vertex()).collect();
|
||||||
let edges = core::array::from_fn(|j| {
|
let edges = (0..15)
|
||||||
let i = j / 3;
|
.map(|j| {
|
||||||
match j % 3 {
|
let i = j / 3;
|
||||||
0 => {
|
match j % 3 {
|
||||||
let outer = if i < N - 1 { i + 1 } else { 0 };
|
0 => {
|
||||||
graph.add_edge(vertices[i], vertices[outer])
|
let outer = if i < N - 1 { i + 1 } else { 0 };
|
||||||
|
graph.add_edge(vertices[i], vertices[outer])
|
||||||
|
}
|
||||||
|
1 => {
|
||||||
|
let inner = if i < N - K { i + N + K } else { i + K };
|
||||||
|
graph.add_edge(vertices[i + N], vertices[inner])
|
||||||
|
}
|
||||||
|
_ => graph.add_edge(vertices[i], vertices[i + N]),
|
||||||
}
|
}
|
||||||
1 => {
|
})
|
||||||
let inner = if i < N - K { i + N + K } else { i + K };
|
.collect();
|
||||||
graph.add_edge(vertices[i + N], vertices[inner])
|
|
||||||
}
|
|
||||||
_ => graph.add_edge(vertices[i], vertices[i + N]),
|
|
||||||
}
|
|
||||||
});
|
|
||||||
(vertices, edges)
|
(vertices, edges)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user