mirror of
https://github.com/drewcassidy/vector-victor.git
synced 2024-09-01 14:58:35 +00:00
27 lines
735 B
Rust
27 lines
735 B
Rust
|
pub trait Index2D: Copy {
|
||
|
fn to_1d(self, height: usize, width: usize) -> Option<usize> {
|
||
|
let (r, c) = self.to_2d(height, width)?;
|
||
|
Some(r * width + c)
|
||
|
}
|
||
|
|
||
|
fn to_2d(self, height: usize, width: usize) -> Option<(usize, usize)>;
|
||
|
}
|
||
|
|
||
|
impl Index2D for usize {
|
||
|
fn to_2d(self, height: usize, width: usize) -> Option<(usize, usize)> {
|
||
|
match self < (height * width) {
|
||
|
true => Some((self / width, self % width)),
|
||
|
false => None,
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Index2D for (usize, usize) {
|
||
|
fn to_2d(self, height: usize, width: usize) -> Option<(usize, usize)> {
|
||
|
match self.0 < height && self.1 < width {
|
||
|
true => Some(self),
|
||
|
false => None,
|
||
|
}
|
||
|
}
|
||
|
}
|