49 lines
1.4 KiB
Rust
49 lines
1.4 KiB
Rust
use nd_array::Array;
|
|
use crate::system::Position;
|
|
|
|
pub trait Storage {
|
|
fn at(&self, position: &Position) -> bool;
|
|
fn deposit(&mut self, position: &Position);
|
|
}
|
|
|
|
pub struct VectorStorage {
|
|
backing: Vec<bool>,
|
|
grid_size: u32,
|
|
dim: u32,
|
|
}
|
|
|
|
impl VectorStorage {
|
|
pub fn new(grid_size: u32, dim: u32) -> VectorStorage {
|
|
VectorStorage { grid_size, dim, backing: vec![false; grid_size.pow(dim) as usize] }
|
|
}
|
|
|
|
pub fn linear_index(&self, position: &Position) -> usize {
|
|
assert!(position.0 <= self.grid_size as i32 && -(self.grid_size as i32) <= position.0);
|
|
assert!(position.1 <= self.grid_size as i32 && -(self.grid_size as i32) <= position.1);
|
|
|
|
let x = (position.0 + (self.grid_size as i32) / 2) as usize;
|
|
let y = (position.1 + (self.grid_size as i32) / 2) as usize;
|
|
|
|
return self.grid_size as usize * y + x
|
|
}
|
|
|
|
/*
|
|
* Convenience function for c-binding
|
|
* */
|
|
pub fn write(&mut self, position: &Position, val: bool) {
|
|
let index = self.linear_index(position);
|
|
self.backing[index] = val;
|
|
}
|
|
}
|
|
|
|
impl Storage for VectorStorage {
|
|
fn at(&self, position: &Position) -> bool {
|
|
return self.backing[self.linear_index(position)]
|
|
}
|
|
|
|
fn deposit(&mut self, position: &Position) {
|
|
let index = self.linear_index(position);
|
|
self.backing[index] = true;
|
|
}
|
|
}
|