Add EdgeMap and private generic EntityMap, update tests
This commit is contained in:
+92
-16
@@ -3,25 +3,18 @@ use std::ops::{Index, IndexMut};
|
||||
use crate::traits::GraphTopology;
|
||||
|
||||
pub struct VertexMap<V: Copy, T: Clone> {
|
||||
data: Vec<T>,
|
||||
default: T,
|
||||
to_index: fn(V) -> usize,
|
||||
inner: EntityMap<V, T>,
|
||||
}
|
||||
|
||||
impl<V: Copy, T: Clone> VertexMap<V, T> {
|
||||
pub fn new(default: T, to_index: fn(V) -> usize, capacity: usize) -> Self {
|
||||
Self {
|
||||
data: vec![default.clone(); capacity],
|
||||
default,
|
||||
to_index,
|
||||
inner: EntityMap::new(default, to_index, capacity),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sync<G: GraphTopology<Vertex = V>>(&mut self, graph: &G) {
|
||||
let capacity = graph.vertex_capacity();
|
||||
if capacity > self.data.len() {
|
||||
self.data.resize(capacity, self.default.clone());
|
||||
}
|
||||
self.inner.resize(graph.vertex_capacity());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +22,73 @@ impl<V: Copy, T: Clone> Index<V> for VertexMap<V, T> {
|
||||
type Output = T;
|
||||
|
||||
fn index(&self, v: V) -> &T {
|
||||
let i = (self.to_index)(v);
|
||||
&self.inner[v]
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: Copy, T: Clone> IndexMut<V> for VertexMap<V, T> {
|
||||
fn index_mut(&mut self, v: V) -> &mut T {
|
||||
&mut self.inner[v]
|
||||
}
|
||||
}
|
||||
|
||||
pub struct EdgeMap<E: Copy, T: Clone> {
|
||||
inner: EntityMap<E, T>,
|
||||
}
|
||||
|
||||
impl<E: Copy, T: Clone> EdgeMap<E, T> {
|
||||
pub fn new(default: T, to_index: fn(E) -> usize, capacity: usize) -> Self {
|
||||
Self {
|
||||
inner: EntityMap::new(default, to_index, capacity),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sync<G: GraphTopology<Edge = E>>(&mut self, graph: &G) {
|
||||
self.inner.resize(graph.edge_capacity());
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Copy, T: Clone> Index<E> for EdgeMap<E, T> {
|
||||
type Output = T;
|
||||
|
||||
fn index(&self, e: E) -> &T {
|
||||
&self.inner[e]
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Copy, T: Clone> IndexMut<E> for EdgeMap<E, T> {
|
||||
fn index_mut(&mut self, e: E) -> &mut T {
|
||||
&mut self.inner[e]
|
||||
}
|
||||
}
|
||||
|
||||
struct EntityMap<E: Copy, T: Clone> {
|
||||
data: Vec<T>,
|
||||
default: T,
|
||||
to_index: fn(E) -> usize,
|
||||
}
|
||||
|
||||
impl<E: Copy, T: Clone> EntityMap<E, T> {
|
||||
pub fn new(default: T, to_index: fn(E) -> usize, capacity: usize) -> Self {
|
||||
Self {
|
||||
data: vec![default.clone(); capacity],
|
||||
default,
|
||||
to_index,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resize(&mut self, capacity: usize) {
|
||||
if capacity > self.data.len() {
|
||||
self.data.resize(capacity, self.default.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Copy, T: Clone> Index<E> for EntityMap<E, T> {
|
||||
type Output = T;
|
||||
|
||||
fn index(&self, e: E) -> &T {
|
||||
let i = (self.to_index)(e);
|
||||
if i < self.data.len() {
|
||||
&self.data[i]
|
||||
} else {
|
||||
@@ -38,9 +97,9 @@ impl<V: Copy, T: Clone> Index<V> for VertexMap<V, T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: Copy, T: Clone> IndexMut<V> for VertexMap<V, T> {
|
||||
fn index_mut(&mut self, v: V) -> &mut T {
|
||||
let i = (self.to_index)(v);
|
||||
impl<E: Copy, T: Clone> IndexMut<E> for EntityMap<E, T> {
|
||||
fn index_mut(&mut self, e: E) -> &mut T {
|
||||
let i = (self.to_index)(e);
|
||||
if i >= self.data.len() {
|
||||
self.data.resize(i + 1, self.default.clone());
|
||||
}
|
||||
@@ -49,7 +108,7 @@ impl<V: Copy, T: Clone> IndexMut<V> for VertexMap<V, T> {
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod append_graph_tests {
|
||||
mod append_graph_vertex_map_tests {
|
||||
use crate::models::append_graph::AppendGraph;
|
||||
use crate::traits::GraphTopology;
|
||||
|
||||
@@ -57,10 +116,27 @@ mod append_graph_tests {
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod graph_tests {
|
||||
mod append_graph_edge_map_tests {
|
||||
use crate::models::append_graph::AppendGraph;
|
||||
use crate::traits::GraphTopology;
|
||||
|
||||
crate::edge_map_tests!(AppendGraph);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod graph_vertex_map_tests {
|
||||
use crate::models::graph::Graph;
|
||||
use crate::traits::{GraphTopology, GraphTopologyDeletion};
|
||||
|
||||
crate::vertex_map_tests!(Graph);
|
||||
crate::vertex_map_deletion_tests!(Graph);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod graph_edge_map_tests {
|
||||
use crate::models::graph::Graph;
|
||||
use crate::traits::{GraphTopology, GraphTopologyDeletion};
|
||||
|
||||
crate::edge_map_tests!(Graph);
|
||||
crate::edge_map_deletion_tests!(Graph);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user