Add len() for VertexMap and EdgeMap
This commit is contained in:
+14
@@ -13,6 +13,11 @@ impl<V: Copy, T: Clone> VertexMap<V, T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reads beyond len() are valid and return the default value.
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
self.inner.len()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn sync<G: GraphTopology<Vertex = V>>(&mut self, graph: &G) {
|
pub fn sync<G: GraphTopology<Vertex = V>>(&mut self, graph: &G) {
|
||||||
self.inner.resize(graph.vertex_capacity());
|
self.inner.resize(graph.vertex_capacity());
|
||||||
}
|
}
|
||||||
@@ -43,6 +48,11 @@ impl<E: Copy, T: Clone> EdgeMap<E, T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reads beyond len() are valid and return the default value.
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
self.inner.len()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn sync<G: GraphTopology<Edge = E>>(&mut self, graph: &G) {
|
pub fn sync<G: GraphTopology<Edge = E>>(&mut self, graph: &G) {
|
||||||
self.inner.resize(graph.edge_capacity());
|
self.inner.resize(graph.edge_capacity());
|
||||||
}
|
}
|
||||||
@@ -77,6 +87,10 @@ impl<E: Copy, T: Clone> EntityMap<E, T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
self.data.len()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn resize(&mut self, capacity: usize) {
|
pub fn resize(&mut self, capacity: usize) {
|
||||||
if capacity > self.data.len() {
|
if capacity > self.data.len() {
|
||||||
self.data.resize(capacity, self.default.clone());
|
self.data.resize(capacity, self.default.clone());
|
||||||
|
|||||||
@@ -48,16 +48,16 @@ macro_rules! vertex_map_tests {
|
|||||||
let mut graph = <$T>::new();
|
let mut graph = <$T>::new();
|
||||||
graph.add_vertex();
|
graph.add_vertex();
|
||||||
let mut map = graph.vertex_map(42);
|
let mut map = graph.vertex_map(42);
|
||||||
let initial_len = map.inner.data.len();
|
let initial_len = map.len();
|
||||||
while graph.vertex_capacity() <= initial_len {
|
while graph.vertex_capacity() <= initial_len {
|
||||||
graph.add_vertex();
|
graph.add_vertex();
|
||||||
}
|
}
|
||||||
assert!(
|
assert!(
|
||||||
map.inner.data.len() < graph.vertex_capacity(),
|
map.len() < graph.vertex_capacity(),
|
||||||
"precondition: map is stale before sync"
|
"precondition: map is stale before sync"
|
||||||
);
|
);
|
||||||
map.sync(&graph);
|
map.sync(&graph);
|
||||||
assert_eq!(map.inner.data.len(), graph.vertex_capacity());
|
assert_eq!(map.len(), graph.vertex_capacity());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -99,7 +99,7 @@ macro_rules! vertex_map_deletion_tests {
|
|||||||
let capacity_before = graph.vertex_capacity();
|
let capacity_before = graph.vertex_capacity();
|
||||||
graph.delete_vertex(v2);
|
graph.delete_vertex(v2);
|
||||||
map.sync(&graph);
|
map.sync(&graph);
|
||||||
assert_eq!(map.inner.data.len(), capacity_before);
|
assert_eq!(map.len(), capacity_before);
|
||||||
assert_eq!(map[v1], 5);
|
assert_eq!(map[v1], 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,16 +180,16 @@ macro_rules! edge_map_tests {
|
|||||||
let v2 = graph.add_vertex();
|
let v2 = graph.add_vertex();
|
||||||
graph.add_edge(v1, v2);
|
graph.add_edge(v1, v2);
|
||||||
let mut map = graph.edge_map(42);
|
let mut map = graph.edge_map(42);
|
||||||
let initial_len = map.inner.data.len();
|
let initial_len = map.len();
|
||||||
while graph.edge_capacity() <= initial_len {
|
while graph.edge_capacity() <= initial_len {
|
||||||
graph.add_edge(v1, v2);
|
graph.add_edge(v1, v2);
|
||||||
}
|
}
|
||||||
assert!(
|
assert!(
|
||||||
map.inner.data.len() < graph.edge_capacity(),
|
map.len() < graph.edge_capacity(),
|
||||||
"precondition: map is stale before sync"
|
"precondition: map is stale before sync"
|
||||||
);
|
);
|
||||||
map.sync(&graph);
|
map.sync(&graph);
|
||||||
assert_eq!(map.inner.data.len(), graph.edge_capacity());
|
assert_eq!(map.len(), graph.edge_capacity());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -237,7 +237,7 @@ macro_rules! edge_map_deletion_tests {
|
|||||||
let capacity_before = graph.edge_capacity();
|
let capacity_before = graph.edge_capacity();
|
||||||
graph.delete_edge(e2);
|
graph.delete_edge(e2);
|
||||||
map.sync(&graph);
|
map.sync(&graph);
|
||||||
assert_eq!(map.inner.data.len(), capacity_before);
|
assert_eq!(map.len(), capacity_before);
|
||||||
assert_eq!(map[e1], 5);
|
assert_eq!(map[e1], 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user