Add Clone to graph models, and tests

This commit is contained in:
2026-09-19 17:19:47 +02:00
parent 4c6a8a43e4
commit e593298eba
6 changed files with 164 additions and 0 deletions
+10
View File
@@ -43,6 +43,7 @@ pub type AppendGraphEdgeMap<T> = ElementMap<Edge, T>;
/// An [`Incidence`] for [`AppendGraph`].
pub type AppendGraphIncidence = Incidence<Vertex, Edge>;
#[derive(Clone)]
struct VertexIncidenceHeader {
incidence_count: usize,
first_incidence: Option<Edge>,
@@ -101,11 +102,18 @@ impl IncidenceCursor<AppendGraph> for AppendGraphIncidenceCursor {
/// Both [`add_vertex`] and [`add_edge`] run in amortised *O(1)* time. [`degree`] runs in *O(1)*
/// time since vertex degrees are stored. Space complexity is *O(|V| + |E|)*.
///
/// # Cloning
///
/// [`Vertex`] and [`Edge`] handles obtained from a clone are valid, and refer to the same vertex or
/// edge in the original, and vice versa. However, once cloned, the two instances no longer affect
/// each other.
///
/// [`add_vertex`]: Self::add_vertex
/// [`add_edge`]: Self::add_edge
/// [`degree`]: Self::degree
/// [`FrozenGraph`]: crate::models::FrozenGraph
/// [`Graph`]: crate::models::Graph
#[derive(Clone)]
pub struct AppendGraph {
vertices: Vec<VertexIncidenceHeader>,
incidences: Vec<IncidenceEntry>,
@@ -257,6 +265,8 @@ mod trait_tests {
crate::graph_topology_tests!(AppendGraph);
crate::graph_topology_addition_tests!(AppendGraph);
crate::graph_topology_clone_tests!(AppendGraph);
crate::graph_topology_addition_clone_tests!(AppendGraph);
}
#[cfg(test)]
+8
View File
@@ -87,9 +87,16 @@ impl IncidenceCursor<FrozenGraph> for FrozenGraphIncidenceCursor {
///
/// [`degree`] runs in *O(1)* time. Space complexity is *O(|V| + |E|)*.
///
/// # Cloning
///
/// [`Vertex`] and [`Edge`] handles obtained from a clone are valid, and refer to the same vertex or
/// edge in the original, and vice versa. However, once cloned, the two instances no longer affect
/// each other.
///
/// [`degree`]: Self::degree
/// [`AppendGraph`]: crate::models::AppendGraph
/// [`Graph`]: crate::models::Graph
#[derive(Clone)]
pub struct FrozenGraph {
vertices: Vec<usize>,
incidences: Vec<IncidenceEntry>,
@@ -255,6 +262,7 @@ mod trait_tests {
use super::FrozenGraph;
crate::graph_topology_tests!(FrozenGraph);
crate::graph_topology_clone_tests!(FrozenGraph);
}
#[cfg(test)]
+10
View File
@@ -49,6 +49,7 @@ impl IncidenceSlot {
/// Referenced by [`Vertex`] as a type parameter of the underlying arena. Not intended for direct
/// external use.
#[doc(hidden)]
#[derive(Clone)]
pub struct VertexIncidenceHeader {
incidence_count: usize,
first_incidence: Option<IncidenceSlot>,
@@ -129,12 +130,19 @@ impl IncidenceCursor<Graph> for GraphIncidenceCursor {
/// planned, performing deletions first allows freed slots to be reused by subsequent additions,
/// potentially avoiding reallocation.
///
/// # Cloning
///
/// [`Vertex`] and [`Edge`] handles obtained from a clone are valid, and refer to the same vertex or
/// edge in the original, and vice versa. However, once cloned, the two instances no longer affect
/// each other.
///
/// [`add_vertex`]: Self::add_vertex
/// [`add_edge`]: Self::add_edge
/// [`degree`]: Self::degree
/// [`delete_vertex`]: Self::delete_vertex
/// [`delete_edge`]: Self::delete_edge
/// [`AppendGraph`]: crate::models::AppendGraph
#[derive(Clone)]
pub struct Graph {
// TODO: Arena index and generation types could be externalized to Graph.
vertices: Arena<VertexIncidenceHeader, usize, usize>,
@@ -398,6 +406,8 @@ mod trait_tests {
crate::graph_topology_addition_tests!(Graph);
crate::graph_topology_deletion_tests!(Graph);
crate::graph_topology_addition_deletion_tests!(Graph);
crate::graph_topology_clone_tests!(Graph);
crate::graph_topology_addition_clone_tests!(Graph);
}
#[cfg(test)]