coords: use num::Integer::div_mod_floor for from_index
This commit is contained in:
parent
cbf47482ad
commit
00238fc1f1
@ -1,6 +1,5 @@
|
|||||||
use std::fmt::Display;
|
|
||||||
|
|
||||||
use super::Board;
|
use super::Board;
|
||||||
|
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
|
||||||
@ -15,7 +14,7 @@ const_assert!(CoordPairInner::MAX as usize >= Board::BOARD_AREA as usize);
|
|||||||
#[derive(PartialEq, Eq, Copy, Clone, Hash)]
|
#[derive(PartialEq, Eq, Copy, Clone, Hash)]
|
||||||
pub struct CoordPair(pub CoordPairInner);
|
pub struct CoordPair(pub CoordPairInner);
|
||||||
|
|
||||||
impl Display for CoordPair {
|
impl std::fmt::Display for CoordPair {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
let (i, j) = (*self).into();
|
let (i, j) = (*self).into();
|
||||||
write!(f, "({}, {})", i, j)
|
write!(f, "({}, {})", i, j)
|
||||||
@ -29,14 +28,12 @@ impl std::fmt::Debug for CoordPair {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Convert a row and column to an index in the board
|
/// Convert a row and column to an index in the board
|
||||||
pub const fn get_index(row: CoordAxis, col: CoordAxis) -> CoordPair {
|
const fn get_index(row: CoordAxis, col: CoordAxis) -> CoordPair {
|
||||||
CoordPair(row * Board::BOARD_SIZE + col)
|
CoordPair(row * Board::BOARD_SIZE + col)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn from_index(index: CoordPair) -> (CoordAxis, CoordAxis) {
|
fn from_index(index: CoordPair) -> (CoordAxis, CoordAxis) {
|
||||||
let row = index.0 % Board::BOARD_SIZE;
|
index.0.div_mod_floor(&Board::BOARD_SIZE)
|
||||||
let col = (index.0 - row) / Board::BOARD_SIZE;
|
|
||||||
(col, row)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<(CoordAxis, CoordAxis)> for CoordPair {
|
impl From<(CoordAxis, CoordAxis)> for CoordPair {
|
||||||
@ -56,7 +53,7 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn into_outof() {
|
fn into_out_of() {
|
||||||
let a: CoordPair = (1, 3).into();
|
let a: CoordPair = (1, 3).into();
|
||||||
let back: (CoordAxis, CoordAxis) = a.into();
|
let back: (CoordAxis, CoordAxis) = a.into();
|
||||||
assert_eq!(back, (1, 3));
|
assert_eq!(back, (1, 3));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user