use crate::{NodeId, Pattern};
#[derive(Clone, Debug)]
pub struct NodeMap<T> {
data: Vec<Option<T>>,
}
impl<T> NodeMap<T> {
#[must_use]
pub fn new(pattern: &Pattern) -> Self {
Self {
data: std::iter::repeat_with(|| None)
.take(pattern.len())
.collect(),
}
}
pub fn insert(&mut self, id: NodeId, value: T) -> Option<T> {
self.data[id.0 as usize].replace(value)
}
#[must_use]
pub fn get(&self, id: NodeId) -> Option<&T> {
self.data.get(id.0 as usize)?.as_ref()
}
#[must_use]
pub fn get_mut(&mut self, id: NodeId) -> Option<&mut T> {
self.data.get_mut(id.0 as usize)?.as_mut()
}
}