partial test complete

This commit is contained in:
Henry Schimke
2023-04-24 17:42:40 -05:00
parent a811b56fbb
commit d08f67feed
8 changed files with 115 additions and 47 deletions

View File

@@ -5,12 +5,12 @@ use crate::{Exceptions, Point};
pub struct Matrix<T: Default + Clone + Copy> {
width: usize,
height: usize,
data: Vec<T>,
data: Vec<Option<T>>,
}
impl<T: Default + Clone + Copy> Matrix<T> {
pub fn with_data(width: usize, height: usize, data: Vec<T>) -> Result<Matrix<T>> {
if (width != 0 && data.len() / width as usize != height as usize) {
pub fn with_data(width: usize, height: usize, data: Vec<Option<T>>) -> Result<Matrix<T>> {
if width != 0 && data.len() / width as usize != height as usize {
return Err(Exceptions::illegal_argument_with(
"invalid size: width * height is too big",
));
@@ -31,7 +31,7 @@ impl<T: Default + Clone + Copy> Matrix<T> {
Ok(Self {
width,
height,
data: vec![T::default(); width * height],
data: vec![None; width * height],
})
}
@@ -66,13 +66,15 @@ impl<T: Default + Clone + Copy> Matrix<T> {
pub fn get(&self, x: usize, y: usize) -> Option<T> {
if x >= self.width || y >= self.height {
None
} else {
Some(self.data[Self::get_offset(x, y, self.width)])
} else if let Some(Some(d)) = self.data.get(Self::get_offset(x, y, self.width)){
Some(*d)
}else {
None
}
}
pub fn set(&mut self, x: usize, y: usize, value: T) -> T {
self.data[Self::get_offset(x, y, self.width)] = value;
self.data[Self::get_offset(x, y, self.width)] = Some(value);
self.get(x, y).unwrap()
}
@@ -84,7 +86,7 @@ impl<T: Default + Clone + Copy> Matrix<T> {
self.set(p.x as usize, p.y as usize, value)
}
pub fn data(&self) -> &[T] {
pub fn data(&self) -> &[Option<T>] {
&self.data
}
@@ -97,10 +99,10 @@ impl<T: Default + Clone + Copy> Matrix<T> {
// }
pub fn clear_with(&mut self, value: T) {
self.data.fill(value)
self.data.fill(Some(value))
}
pub fn clear(&mut self) {
self.data.fill(T::default())
self.data.fill(None)
}
}