cf31ee2f01
Rename EdgeMap.len to capacity. Add VertexMap.capacity and EdgeMap.capacity. Deprecate VertexMap.len and EdgeMap.len.
134 lines
3.1 KiB
Rust
134 lines
3.1 KiB
Rust
use std::ops::{Index, IndexMut};
|
|
|
|
use crate::traits::GraphTopology;
|
|
|
|
pub struct VertexMap<V: Copy, T: Clone> {
|
|
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 {
|
|
inner: EntityMap::new(default, to_index, capacity),
|
|
}
|
|
}
|
|
|
|
// Reads beyond 'capacity' are valid and return the default value.
|
|
pub fn capacity(&self) -> usize {
|
|
self.inner.capacity()
|
|
}
|
|
|
|
#[deprecated(since = "0.2.2", note = "use 'capacity' instead")]
|
|
pub fn len(&self) -> usize {
|
|
self.capacity()
|
|
}
|
|
|
|
|
|
pub fn sync<G: GraphTopology<Vertex = V>>(&mut self, graph: &G) {
|
|
self.inner.resize(graph.vertex_capacity());
|
|
}
|
|
}
|
|
|
|
impl<V: Copy, T: Clone> Index<V> for VertexMap<V, T> {
|
|
type Output = T;
|
|
|
|
fn index(&self, v: V) -> &T {
|
|
&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),
|
|
}
|
|
}
|
|
|
|
// Reads beyond 'capacity' are valid and return the default value.
|
|
pub fn capacity(&self) -> usize {
|
|
self.inner.capacity()
|
|
}
|
|
|
|
#[deprecated(since = "0.2.2", note = "use 'capacity' instead")]
|
|
pub fn len(&self) -> usize {
|
|
self.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 capacity(&self) -> usize {
|
|
self.data.len()
|
|
}
|
|
|
|
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 {
|
|
&self.default
|
|
}
|
|
}
|
|
}
|
|
|
|
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());
|
|
}
|
|
&mut self.data[i]
|
|
}
|
|
}
|