ai-0/lab1/src/algo/board.rs

47 lines
998 B
Rust

use std::ops::{Index, IndexMut};
pub(crate) struct Board {
data: Box<[usize]>,
}
impl Board {
pub fn alloc(size: usize) -> Self {
return Self {
data: vec![0usize; size].into_boxed_slice(),
};
}
pub fn len(&self) -> usize {
return self.data.len();
}
}
impl Index<usize> for Board {
type Output = usize;
fn index(&self, index: usize) -> &Self::Output {
return self
.data
.get(index)
.unwrap_or_else(|| panic!("Y out of bounds: {index} >= {}", self.data.len()));
}
}
impl IndexMut<usize> for Board {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
let len = self.data.len();
return self
.data
.get_mut(index)
.unwrap_or_else(|| panic!("Y out of bounds: {index} >= {len}"));
}
}
impl Clone for Board {
fn clone(&self) -> Self {
return Self {
data: self.data.clone(),
};
}
}