Replace VertexMap and EdgeMap with now public EntityMap
This commit is contained in:
@@ -194,7 +194,7 @@ macro_rules! bfs_tests {
|
||||
}
|
||||
|
||||
fn assert_bfs_distances(
|
||||
distances: &$crate::maps::VertexMap<
|
||||
distances: &$crate::maps::EntityMap<
|
||||
<$T as $crate::traits::GraphTopology>::Vertex,
|
||||
Option<u32>,
|
||||
>,
|
||||
@@ -222,7 +222,7 @@ macro_rules! bfs_tests {
|
||||
}
|
||||
|
||||
fn assert_bfs_predecessors(
|
||||
predecessors: &$crate::maps::VertexMap<
|
||||
predecessors: &$crate::maps::EntityMap<
|
||||
<$T as $crate::traits::GraphTopology>::Vertex,
|
||||
Option<<$T as $crate::traits::GraphTopology>::Vertex>,
|
||||
>,
|
||||
|
||||
@@ -163,7 +163,7 @@ macro_rules! dfs_tests {
|
||||
}
|
||||
|
||||
fn assert_dfs_visited(
|
||||
visited: &$crate::maps::VertexMap<<$T as $crate::traits::GraphTopology>::Vertex, bool>,
|
||||
visited: &$crate::maps::EntityMap<<$T as $crate::traits::GraphTopology>::Vertex, bool>,
|
||||
vertices: &[<$T as $crate::traits::GraphTopology>::Vertex],
|
||||
) {
|
||||
for i in 0..10 {
|
||||
@@ -177,8 +177,8 @@ macro_rules! dfs_tests {
|
||||
|
||||
fn assert_dfs_predecessors(
|
||||
graph: &$T,
|
||||
visited: &$crate::maps::VertexMap<<$T as $crate::traits::GraphTopology>::Vertex, bool>,
|
||||
predecessors: &$crate::maps::VertexMap<
|
||||
visited: &$crate::maps::EntityMap<<$T as $crate::traits::GraphTopology>::Vertex, bool>,
|
||||
predecessors: &$crate::maps::EntityMap<
|
||||
<$T as $crate::traits::GraphTopology>::Vertex,
|
||||
Option<<$T as $crate::traits::GraphTopology>::Vertex>,
|
||||
>,
|
||||
|
||||
@@ -131,7 +131,7 @@ macro_rules! dijkstra_tests {
|
||||
}
|
||||
|
||||
fn assert_distances_single_vertex(
|
||||
distances: &$crate::maps::VertexMap<
|
||||
distances: &$crate::maps::EntityMap<
|
||||
<$T as $crate::traits::GraphTopology>::Vertex,
|
||||
Option<u32>,
|
||||
>,
|
||||
@@ -160,7 +160,7 @@ macro_rules! dijkstra_tests {
|
||||
}
|
||||
|
||||
fn assert_distances_disconnected(
|
||||
distances: &$crate::maps::VertexMap<
|
||||
distances: &$crate::maps::EntityMap<
|
||||
<$T as $crate::traits::GraphTopology>::Vertex,
|
||||
Option<u32>,
|
||||
>,
|
||||
@@ -204,7 +204,7 @@ macro_rules! dijkstra_tests {
|
||||
}
|
||||
|
||||
fn assert_distances_test_graph(
|
||||
distances: &$crate::maps::VertexMap<
|
||||
distances: &$crate::maps::EntityMap<
|
||||
<$T as $crate::traits::GraphTopology>::Vertex,
|
||||
Option<u32>,
|
||||
>,
|
||||
@@ -261,7 +261,7 @@ macro_rules! dijkstra_tests {
|
||||
}
|
||||
|
||||
fn assert_distances_unweighted_test_graph(
|
||||
distances: &$crate::maps::VertexMap<
|
||||
distances: &$crate::maps::EntityMap<
|
||||
<$T as $crate::traits::GraphTopology>::Vertex,
|
||||
Option<u32>,
|
||||
>,
|
||||
@@ -300,7 +300,7 @@ macro_rules! dijkstra_tests {
|
||||
<$T as $crate::traits::GraphTopology>::Vertex,
|
||||
<$T as $crate::traits::GraphTopology>::Edge,
|
||||
)>; 10],
|
||||
$crate::maps::EdgeMap<<$T as $crate::traits::GraphTopology>::Edge, u32>,
|
||||
$crate::maps::EntityMap<<$T as $crate::traits::GraphTopology>::Edge, u32>,
|
||||
) {
|
||||
use $crate::traits::GraphTopology;
|
||||
let (graph, vertices, edges, incidences) = make_test_graph();
|
||||
|
||||
+9
-161
@@ -1,6 +1,6 @@
|
||||
#[doc(hidden)]
|
||||
#[macro_export]
|
||||
macro_rules! vertex_map_tests {
|
||||
macro_rules! entity_map_tests {
|
||||
($T:ty) => {
|
||||
#[test]
|
||||
fn initial_values_are_default() {
|
||||
@@ -48,7 +48,7 @@ macro_rules! vertex_map_tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sync_expands_to_new_vertices() {
|
||||
fn extend_to_new_vertices() {
|
||||
use $crate::traits::GraphTopology;
|
||||
let mut graph = <$T>::new();
|
||||
graph.add_vertex();
|
||||
@@ -59,21 +59,21 @@ macro_rules! vertex_map_tests {
|
||||
}
|
||||
assert!(
|
||||
map.capacity() < graph.vertex_capacity(),
|
||||
"precondition: map is stale before sync"
|
||||
"precondition: map is stale before extend"
|
||||
);
|
||||
map.sync(&graph);
|
||||
map.extend(graph.vertex_capacity());
|
||||
assert_eq!(map.capacity(), graph.vertex_capacity());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sync_does_not_overwrite_existing_values() {
|
||||
fn extend_does_not_overwrite_existing_values() {
|
||||
use $crate::traits::GraphTopology;
|
||||
let mut graph = <$T>::new();
|
||||
let v = graph.add_vertex();
|
||||
let mut map = graph.vertex_map(0);
|
||||
map[v] = 5;
|
||||
graph.add_vertex();
|
||||
map.sync(&graph);
|
||||
map.extend(graph.vertex_capacity());
|
||||
assert_eq!(map[v], 5);
|
||||
}
|
||||
};
|
||||
@@ -81,7 +81,7 @@ macro_rules! vertex_map_tests {
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export]
|
||||
macro_rules! vertex_map_deletion_tests {
|
||||
macro_rules! entity_map_deletion_tests {
|
||||
($T:ty) => {
|
||||
#[test]
|
||||
fn surviving_vertex_readable_after_delete() {
|
||||
@@ -108,7 +108,7 @@ macro_rules! vertex_map_deletion_tests {
|
||||
map[v1] = 5;
|
||||
let capacity_before = graph.vertex_capacity();
|
||||
graph.delete_vertex(v2);
|
||||
map.sync(&graph);
|
||||
map.extend(graph.vertex_capacity());
|
||||
assert_eq!(map.capacity(), capacity_before);
|
||||
assert_eq!(map[v1], 5);
|
||||
}
|
||||
@@ -124,162 +124,10 @@ macro_rules! vertex_map_deletion_tests {
|
||||
map[v1] = 99;
|
||||
graph.delete_vertex(v1);
|
||||
let v2 = graph.add_vertex();
|
||||
// VertexMap uses raw indices, not vertex identity. A new vertex v2 reusing the slot
|
||||
// EntityMap uses raw indices, not vertex identity. A new vertex v2 reusing the slot
|
||||
// of previously deleted v1 sees the old value. Callers must reinitialize stale slots
|
||||
// after deletion.
|
||||
assert_eq!(map[v2], 99);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export]
|
||||
macro_rules! edge_map_tests {
|
||||
($T:ty) => {
|
||||
#[test]
|
||||
fn initial_values_are_default() {
|
||||
use $crate::traits::GraphTopology;
|
||||
let mut graph = <$T>::new();
|
||||
let v1 = graph.add_vertex();
|
||||
let v2 = graph.add_vertex();
|
||||
let e1 = graph.add_edge(v1, v2);
|
||||
let e2 = graph.add_edge(v1, v2);
|
||||
let map = graph.edge_map(42);
|
||||
assert_eq!(map[e1], 42);
|
||||
assert_eq!(map[e2], 42);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn write_and_read() {
|
||||
use $crate::traits::GraphTopology;
|
||||
let mut graph = <$T>::new();
|
||||
let v1 = graph.add_vertex();
|
||||
let v2 = graph.add_vertex();
|
||||
let e1 = graph.add_edge(v1, v2);
|
||||
let e2 = graph.add_edge(v1, v2);
|
||||
let mut map = graph.edge_map(0);
|
||||
map[e1] = 7;
|
||||
assert_eq!(map[e1], 7);
|
||||
assert_eq!(map[e2], 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lazy_growth_on_read() {
|
||||
use $crate::traits::GraphTopology;
|
||||
let mut graph = <$T>::new();
|
||||
let v1 = graph.add_vertex();
|
||||
let v2 = graph.add_vertex();
|
||||
graph.add_edge(v1, v2);
|
||||
let map = graph.edge_map(99);
|
||||
let e = graph.add_edge(v1, v2);
|
||||
assert_eq!(map[e], 99);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lazy_growth_on_write() {
|
||||
use $crate::traits::GraphTopology;
|
||||
let mut graph = <$T>::new();
|
||||
let v1 = graph.add_vertex();
|
||||
let v2 = graph.add_vertex();
|
||||
let e1 = graph.add_edge(v1, v2);
|
||||
let mut map = graph.edge_map(0);
|
||||
let e2 = graph.add_edge(v1, v2);
|
||||
map[e2] = 7;
|
||||
assert_eq!(map[e1], 0);
|
||||
assert_eq!(map[e2], 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sync_expands_to_new_edges() {
|
||||
use $crate::traits::GraphTopology;
|
||||
let mut graph = <$T>::new();
|
||||
let v1 = graph.add_vertex();
|
||||
let v2 = graph.add_vertex();
|
||||
graph.add_edge(v1, v2);
|
||||
let mut map = graph.edge_map(42);
|
||||
let capacity_before = map.capacity();
|
||||
while graph.edge_capacity() <= capacity_before {
|
||||
graph.add_edge(v1, v2);
|
||||
}
|
||||
assert!(
|
||||
map.capacity() < graph.edge_capacity(),
|
||||
"precondition: map is stale before sync"
|
||||
);
|
||||
map.sync(&graph);
|
||||
assert_eq!(map.capacity(), graph.edge_capacity());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sync_does_not_overwrite_existing_values() {
|
||||
use $crate::traits::GraphTopology;
|
||||
let mut graph = <$T>::new();
|
||||
let v1 = graph.add_vertex();
|
||||
let v2 = graph.add_vertex();
|
||||
let e = graph.add_edge(v1, v2);
|
||||
let mut map = graph.edge_map(0);
|
||||
map[e] = 5;
|
||||
graph.add_edge(v1, v2);
|
||||
map.sync(&graph);
|
||||
assert_eq!(map[e], 5);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export]
|
||||
macro_rules! edge_map_deletion_tests {
|
||||
($T:ty) => {
|
||||
#[test]
|
||||
fn surviving_edge_readable_after_delete() {
|
||||
use $crate::traits::GraphTopology;
|
||||
use $crate::traits::GraphTopologyDeletion;
|
||||
let mut graph = <$T>::new();
|
||||
let v1 = graph.add_vertex();
|
||||
let v2 = graph.add_vertex();
|
||||
let e1 = graph.add_edge(v1, v2);
|
||||
let e2 = graph.add_edge(v1, v2);
|
||||
let mut map = graph.edge_map(0);
|
||||
map[e1] = 1;
|
||||
map[e2] = 2;
|
||||
graph.delete_edge(e2);
|
||||
assert_eq!(map[e1], 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn capacity_does_not_shrink_after_delete() {
|
||||
use $crate::traits::GraphTopology;
|
||||
use $crate::traits::GraphTopologyDeletion;
|
||||
let mut graph = <$T>::new();
|
||||
let v1 = graph.add_vertex();
|
||||
let v2 = graph.add_vertex();
|
||||
let e1 = graph.add_edge(v1, v2);
|
||||
let e2 = graph.add_edge(v1, v2);
|
||||
let mut map = graph.edge_map(0);
|
||||
map[e1] = 5;
|
||||
let capacity_before = graph.edge_capacity();
|
||||
graph.delete_edge(e2);
|
||||
map.sync(&graph);
|
||||
assert_eq!(map.capacity(), capacity_before);
|
||||
assert_eq!(map[e1], 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reused_slot_returns_old_value() {
|
||||
use $crate::traits::GraphTopology;
|
||||
use $crate::traits::GraphTopologyDeletion;
|
||||
let mut graph = <$T>::new();
|
||||
let v1 = graph.add_vertex();
|
||||
let v2 = graph.add_vertex();
|
||||
graph.add_edge(v1, v2);
|
||||
let e1 = graph.add_edge(v1, v2);
|
||||
let mut map = graph.edge_map(0);
|
||||
map[e1] = 99;
|
||||
graph.delete_edge(e1);
|
||||
let e2 = graph.add_edge(v1, v2);
|
||||
// EdgeMap uses raw indices, not edge identity. Because to_index uses arr_idx/2,
|
||||
// both halves of a deleted edge pair map to the same index, so a new edge reusing
|
||||
// either slot sees the old value. Callers must reinitialize stale slots after deletion.
|
||||
assert_eq!(map[e2], 99);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user