Add FromGraph trait, and add implementation for FrozenGraph

This commit is contained in:
2026-09-19 17:53:44 +02:00
parent e593298eba
commit 038e984c97
4 changed files with 99 additions and 70 deletions
+33
View File
@@ -237,6 +237,39 @@ pub trait GraphTopologyDeletion: GraphTopology {
fn delete_edge(&mut self, e: Self::Edge);
}
/// A trait for constructing an undirected graph topology by converting another graph
/// implementation to this one.
pub trait FromGraph: GraphTopology {
/// Creates a new graph instance that is isomorphic to `source`.
///
/// Returns the copied graph instance and two mappings from the vertices and edges of `source`
/// to the corresponding vertices and edges of the returned graph.
///
/// # Examples
///
/// ```
/// use grapherity::prelude::*;
/// use grapherity::models::{AppendGraph, FrozenGraph};
///
/// let mut source = AppendGraph::new();
/// let u = source.add_vertex();
/// let v = source.add_vertex();
/// let e = source.add_edge(u, v);
/// let (graph, vertex_map, edge_map) = FrozenGraph::from_graph(&source);
///
/// assert!(graph.are_adjacent(vertex_map[u], vertex_map[v]));
/// ```
fn from_graph<G: GraphTopology>(
source: &G,
) -> (
Self,
ElementMap<G::Vertex, Self::Vertex>,
ElementMap<G::Edge, Self::Edge>,
)
where
Self: Sized;
}
/// A cursor for traversing the incidences of a vertex one step at a time.
///
/// Because the cursor is [`Copy`], its state can be saved and restored to replay or branch a