Add VertexMap to attach data to vertices
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
pub mod algorithms;
|
||||
pub mod maps;
|
||||
pub mod models;
|
||||
pub mod traits;
|
||||
|
||||
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
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,
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
if i < self.data.len() {
|
||||
&self.data[i]
|
||||
} else {
|
||||
&self.default
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
if i >= self.data.len() {
|
||||
self.data.resize(i + 1, self.default.clone());
|
||||
}
|
||||
&mut self.data[i]
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::models::append_graph::AppendGraph;
|
||||
use crate::traits::GraphTopology;
|
||||
|
||||
#[test]
|
||||
fn initial_values_are_default() {
|
||||
let mut graph = AppendGraph::new();
|
||||
let v1 = graph.add_vertex();
|
||||
let v2 = graph.add_vertex();
|
||||
let map = graph.vertex_map(42);
|
||||
assert_eq!(map[v1], 42);
|
||||
assert_eq!(map[v2], 42);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn write_and_read() {
|
||||
let mut graph = AppendGraph::new();
|
||||
let v1 = graph.add_vertex();
|
||||
let v2 = graph.add_vertex();
|
||||
let mut map = graph.vertex_map(0);
|
||||
map[v1] = 7;
|
||||
assert_eq!(map[v1], 7);
|
||||
assert_eq!(map[v2], 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lazy_growth_on_read() {
|
||||
let mut graph = AppendGraph::new();
|
||||
graph.add_vertex();
|
||||
let map = graph.vertex_map(99);
|
||||
let v = graph.add_vertex();
|
||||
assert_eq!(map[v], 99);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lazy_growth_on_write() {
|
||||
let mut graph = AppendGraph::new();
|
||||
let v1 = graph.add_vertex();
|
||||
let mut map = graph.vertex_map(0);
|
||||
let v2 = graph.add_vertex();
|
||||
map[v2] = 7;
|
||||
assert_eq!(map[v1], 0);
|
||||
assert_eq!(map[v2], 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sync_expands_to_new_vertices() {
|
||||
let mut graph = AppendGraph::new();
|
||||
graph.add_vertex();
|
||||
let mut map = graph.vertex_map(42);
|
||||
graph.add_vertex();
|
||||
graph.add_vertex();
|
||||
assert!(
|
||||
map.data.len() < graph.vertex_capacity(),
|
||||
"precondition: map is stale before sync"
|
||||
);
|
||||
map.sync(&graph);
|
||||
assert_eq!(map.data.len(), graph.vertex_capacity());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sync_does_not_overwrite_existing_values() {
|
||||
let mut graph = AppendGraph::new();
|
||||
let v = graph.add_vertex();
|
||||
let mut map = graph.vertex_map(0);
|
||||
map[v] = 5;
|
||||
graph.add_vertex();
|
||||
map.sync(&graph);
|
||||
assert_eq!(map[v], 5);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::maps::VertexMap;
|
||||
use crate::traits::GraphTopology;
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||
@@ -83,6 +84,14 @@ impl GraphTopology for AppendGraph {
|
||||
self.incidences.len() / 2
|
||||
}
|
||||
|
||||
fn vertex_capacity(&self) -> usize {
|
||||
self.vertices.len()
|
||||
}
|
||||
|
||||
fn vertex_map<T: Clone>(&self, default: T) -> VertexMap<Self::Vertex, T> {
|
||||
VertexMap::new(default, |v| v.0, self.vertex_capacity())
|
||||
}
|
||||
|
||||
fn degree(&self, v: Self::Vertex) -> usize {
|
||||
self.vertices[v.0].incidence_count
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use typed_generational_arena::{Arena, Index};
|
||||
|
||||
use crate::maps::VertexMap;
|
||||
use crate::traits::{GraphTopology, GraphTopologyDeletion};
|
||||
|
||||
type Vertex = Index<VertexIncidenceHeader, usize, usize>;
|
||||
@@ -162,6 +163,14 @@ impl GraphTopology for Graph {
|
||||
self.incidences.len() / 2
|
||||
}
|
||||
|
||||
fn vertex_capacity(&self) -> usize {
|
||||
self.vertices.capacity()
|
||||
}
|
||||
|
||||
fn vertex_map<T: Clone>(&self, default: T) -> VertexMap<Self::Vertex, T> {
|
||||
VertexMap::new(default, |v| v.arr_idx(), self.vertex_capacity())
|
||||
}
|
||||
|
||||
fn degree(&self, v: Self::Vertex) -> usize {
|
||||
self.vertices[v].incidence_count
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use crate::maps::VertexMap;
|
||||
|
||||
// TODO: Add functions to reserve memory for vertices and edges.
|
||||
// TODO: Add iterator of incident edges for a vertex.
|
||||
// TODO: Add finding incident vertices for an edge.
|
||||
@@ -8,6 +10,8 @@ pub trait GraphTopology {
|
||||
|
||||
fn vertex_count(&self) -> usize;
|
||||
fn edge_count(&self) -> usize;
|
||||
fn vertex_capacity(&self) -> usize;
|
||||
fn vertex_map<T: Clone>(&self, default: T) -> VertexMap<Self::Vertex, T>;
|
||||
fn degree(&self, v: Self::Vertex) -> usize;
|
||||
fn are_adjacent(&self, v1: Self::Vertex, v2: Self::Vertex) -> bool;
|
||||
fn vertices(&self) -> impl Iterator<Item = Self::Vertex>;
|
||||
|
||||
Reference in New Issue
Block a user