2022-08-19 04:07:05 +00:00
|
|
|
use std::fmt::Debug;
|
|
|
|
|
|
|
|
pub trait Index2D: Copy + Debug {
|
2022-12-04 00:56:39 +00:00
|
|
|
#[inline(always)]
|
2022-08-01 07:33:43 +00:00
|
|
|
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 {
|
2022-12-04 00:56:39 +00:00
|
|
|
#[inline(always)]
|
2022-08-01 07:33:43 +00:00
|
|
|
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) {
|
2022-12-04 00:56:39 +00:00
|
|
|
#[inline(always)]
|
2022-08-01 07:33:43 +00:00
|
|
|
fn to_2d(self, height: usize, width: usize) -> Option<(usize, usize)> {
|
|
|
|
match self.0 < height && self.1 < width {
|
|
|
|
true => Some(self),
|
|
|
|
false => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|