use crate::Entity;
pub enum StorageError {
InvalidEntity,
}
pub struct DenseVec<T> {
data: Vec<T>,
}
impl<T> DenseVec<T>
where T: Default + Clone
{
pub fn new() -> Self {
Self {
data: Vec::new(),
}
}
pub fn insert(&mut self, entity: &Entity, value: T) -> Result<(), StorageError> {
if let Some(index) = entity.index() {
if index >= self.data.len() {
self.data.resize(index + 1, T::default());
}
self.data[index] = value;
Ok(())
} else {
Err(StorageError::InvalidEntity)
}
}
pub fn get(&self, entity: &Entity) -> Option<&T> {
if let Some(index) = entity.index() {
self.data.get(index)
} else {
None
}
}
pub fn get_mut(&mut self, entity: &Entity) -> Option<&mut T> {
if let Some(index) = entity.index() {
self.data.get_mut(index)
} else {
None
}
}
}