mirror of
https://github.com/cccb/servicepoint.git
synced 2025-01-18 10:00:14 +01:00
remove duplicate code into trait impl
This commit is contained in:
parent
c017b85962
commit
b8baf8127d
|
@ -43,10 +43,6 @@ impl ByteGrid {
|
|||
}
|
||||
}
|
||||
|
||||
fn is_position_valid(&self, x: isize, y: isize) -> bool {
|
||||
x > 0 && x < self.width as isize && y > 0 && y < self.height as isize
|
||||
}
|
||||
|
||||
fn check_indexes(&self, x: usize, y: usize) {
|
||||
assert!(
|
||||
x < self.width,
|
||||
|
@ -96,22 +92,6 @@ impl Grid<u8> for ByteGrid {
|
|||
self.data[x + y * self.width]
|
||||
}
|
||||
|
||||
fn get_optional(&self, x: isize, y: isize) -> Option<u8> {
|
||||
if self.is_position_valid(x, y) {
|
||||
Some(self.get(x as usize, y as usize))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn set_optional(&mut self, x: isize, y: isize, value: u8) -> Option<u8> {
|
||||
if self.is_position_valid(x, y) {
|
||||
Some(self.set(x as usize, y as usize, value))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn fill(&mut self, value: u8) {
|
||||
self.data.fill(value);
|
||||
}
|
||||
|
@ -151,7 +131,7 @@ impl RefGrid<u8> for ByteGrid {
|
|||
}
|
||||
|
||||
fn get_ref_optional(&self, x: isize, y: isize) -> Option<&u8> {
|
||||
if self.is_position_valid(x, y) {
|
||||
if self.is_in_bounds(x, y) {
|
||||
Some(&self.data[x as usize + y as usize * self.width])
|
||||
} else {
|
||||
None
|
||||
|
@ -164,7 +144,7 @@ impl RefGrid<u8> for ByteGrid {
|
|||
}
|
||||
|
||||
fn get_ref_mut_optional(&mut self, x: isize, y: isize) -> Option<&mut u8> {
|
||||
if self.is_position_valid(x, y) {
|
||||
if self.is_in_bounds(x, y) {
|
||||
Some(&mut self.data[x as usize + y as usize * self.width])
|
||||
} else {
|
||||
None
|
||||
|
|
|
@ -31,7 +31,13 @@ pub trait Grid<T> {
|
|||
/// * `x` and `y`: position of the cell to read
|
||||
///
|
||||
/// returns: Value at position or None
|
||||
fn get_optional(&self, x: isize, y: isize) -> Option<T>;
|
||||
fn get_optional(&self, x: isize, y: isize) -> Option<T> {
|
||||
if self.is_in_bounds(x, y) {
|
||||
Some(self.get(x as usize, y as usize))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the value at the specified position if the position is inside of bounds
|
||||
///
|
||||
|
@ -40,7 +46,13 @@ pub trait Grid<T> {
|
|||
/// * `x` and `y`: position of the cell to read
|
||||
///
|
||||
/// returns: the old value or None
|
||||
fn set_optional(&mut self, x: isize, y: isize, value: T) -> Option<T>;
|
||||
fn set_optional(&mut self, x: isize, y: isize, value: T) -> Option<T> {
|
||||
if self.is_in_bounds(x, y) {
|
||||
Some(self.set(x as usize, y as usize, value))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets all cells in the grid to the specified value
|
||||
fn fill(&mut self, value: T);
|
||||
|
@ -50,6 +62,14 @@ pub trait Grid<T> {
|
|||
|
||||
/// the height in y-direction
|
||||
fn height(&self) -> usize;
|
||||
|
||||
/// Checks whether the specified position is
|
||||
fn is_in_bounds(&self, x: isize, y: isize) -> bool {
|
||||
x > 0
|
||||
&& x < self.width() as isize
|
||||
&& y > 0
|
||||
&& y < self.height() as isize
|
||||
}
|
||||
}
|
||||
|
||||
/// A grid that can return cells as references.
|
||||
|
|
|
@ -60,10 +60,6 @@ impl PixelGrid {
|
|||
}
|
||||
}
|
||||
|
||||
fn is_position_valid(&self, x: isize, y: isize) -> bool {
|
||||
x > 0 && x < self.width as isize && y > 0 && y < self.height as isize
|
||||
}
|
||||
|
||||
fn check_indexes(&self, x: usize, y: usize) {
|
||||
assert!(
|
||||
x < self.width,
|
||||
|
@ -100,27 +96,6 @@ impl Grid<bool> for PixelGrid {
|
|||
self.bit_vec.get(x + y * self.width)
|
||||
}
|
||||
|
||||
fn get_optional(&self, x: isize, y: isize) -> Option<bool> {
|
||||
if self.is_position_valid(x, y) {
|
||||
Some(self.get(x as usize, y as usize))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn set_optional(
|
||||
&mut self,
|
||||
x: isize,
|
||||
y: isize,
|
||||
value: bool,
|
||||
) -> Option<bool> {
|
||||
if self.is_position_valid(x, y) {
|
||||
Some(self.set(x as usize, y as usize, value))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the state of all pixels in the `PixelGrid`.
|
||||
///
|
||||
/// # Arguments
|
||||
|
|
Loading…
Reference in a new issue