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

View File

@@ -2,11 +2,11 @@ use super::Board;
use num::Integer;
use static_assertions::const_assert;
// PERF! having `Coord` be of type `u8` (instead of usize) increases
// performance by about 1-2% overall
/// PERF! having `Coord` be of type `u8` (instead of usize) increases
/// performance by about 1-2% overall
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;
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 {
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 {
@@ -34,8 +38,10 @@ impl std::fmt::Debug for CoordPair {
}
}
fn from_index(index: CoordPair) -> (CoordAxis, CoordAxis) {
index.0.div_mod_floor(&Board::BOARD_SIZE)
impl From<CoordPair> for (CoordAxis, CoordAxis) {
fn from(val: CoordPair) -> Self {
val.from_index()
}
}
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)]
mod tests {
use super::*;