coord stuff

This commit is contained in:
2025-03-05 22:05:15 -05:00
parent 075364e337
commit 3608366482
2 changed files with 13 additions and 11 deletions

View File

@@ -89,6 +89,7 @@ impl fmt::Display for Board {
} }
writeln!(f)?; writeln!(f)?;
} }
// put a line at the bottom of the board too // put a line at the bottom of the board too
writeln!(f, " {}", horiz_sep_line)?; writeln!(f, " {}", horiz_sep_line)?;
@@ -112,6 +113,7 @@ impl fmt::Debug for Board {
} }
impl Board { impl Board {
/// Width of the board
pub const BOARD_SIZE: CoordAxis = 8; pub const BOARD_SIZE: CoordAxis = 8;
/// Area of the board /// Area of the board

View File

@@ -2,11 +2,11 @@ use super::Board;
use num::Integer; use num::Integer;
use static_assertions::const_assert; use static_assertions::const_assert;
// PERF! having `Coord` be of type `u8` (instead of usize) increases /// PERF! having `Coord` be of type `u8` (instead of usize) increases
// performance by about 1-2% overall /// performance by about 1-2% overall
pub type CoordAxis = u8; pub type CoordAxis = u8;
// using u8 for this results in a ~3-6% perf increase /// using u8 for this results in a ~3-6% perf increase
pub type CoordPairInner = u8; pub type CoordPairInner = u8;
const_assert!(CoordPairInner::MAX as usize >= Board::BOARD_AREA as usize); const_assert!(CoordPairInner::MAX as usize >= Board::BOARD_AREA as usize);
@@ -19,6 +19,10 @@ impl CoordPair {
pub const fn from_axes(row: CoordAxis, col: CoordAxis) -> Self { pub const fn from_axes(row: CoordAxis, col: CoordAxis) -> Self {
Self(row * Board::BOARD_SIZE + col) Self(row * Board::BOARD_SIZE + col)
} }
fn from_index(&self) -> (CoordAxis, CoordAxis) {
self.0.div_mod_floor(&Board::BOARD_SIZE)
}
} }
impl std::fmt::Display for CoordPair { impl std::fmt::Display for CoordPair {
@@ -34,8 +38,10 @@ impl std::fmt::Debug for CoordPair {
} }
} }
fn from_index(index: CoordPair) -> (CoordAxis, CoordAxis) { impl From<CoordPair> for (CoordAxis, CoordAxis) {
index.0.div_mod_floor(&Board::BOARD_SIZE) fn from(val: CoordPair) -> Self {
val.from_index()
}
} }
impl From<(CoordAxis, CoordAxis)> for CoordPair { impl From<(CoordAxis, CoordAxis)> for CoordPair {
@@ -44,12 +50,6 @@ impl From<(CoordAxis, CoordAxis)> for CoordPair {
} }
} }
impl From<CoordPair> for (CoordAxis, CoordAxis) {
fn from(val: CoordPair) -> Self {
from_index(val)
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;