improve posmap

This commit is contained in:
Simon Gardling 2025-03-24 15:44:06 -04:00
parent 11571603db
commit 886eddf484
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
3 changed files with 73 additions and 33 deletions

View File

@ -1,38 +1,7 @@
use super::{bitboard::BitBoard, piece::Piece, CoordAxis, CoordPair};
use allocative::Allocative;
use const_for::const_for;
use rand::seq::IteratorRandom;
use std::{cmp::Ordering, fmt, mem::MaybeUninit};
/// Map of all points on the board against some type T
/// Used to index like so: example[i][j]
/// with each coordinate
pub struct PosMap<T>([T; Board::AREA.0 as usize]);
impl<T: Copy> PosMap<T> {
pub const unsafe fn uninit() -> Self {
Self([MaybeUninit::zeroed().assume_init(); Board::AREA.0 as usize])
}
pub const fn from(v: [[T; Board::SIZE as usize]; Board::SIZE as usize]) -> Self {
let mut n = unsafe { Self::uninit() };
const_for!(i in 0..Board::SIZE => {
const_for!(j in 0..Board::SIZE => {
n.set(CoordPair::from_axes(i, j), v[i as usize][j as usize]);
});
});
n
}
pub const fn get(&self, coords: CoordPair) -> &T {
&self.0[coords.0 as usize]
}
pub const fn set(&mut self, coords: CoordPair, value: T) {
self.0[coords.0 as usize] = value;
}
}
use std::{cmp::Ordering, fmt};
#[derive(PartialEq, Eq, Copy, Clone, Debug, Allocative)]
pub enum Winner {

View File

@ -2,7 +2,9 @@ mod bitboard;
mod board;
mod coords;
mod piece;
mod pos_map;
pub use board::{Board, PosMap, Winner};
pub use board::{Board, Winner};
pub use coords::{CoordAxis, CoordPair};
pub use piece::Piece;
pub use pos_map::PosMap;

69
src/repr/pos_map.rs Normal file
View File

@ -0,0 +1,69 @@
use const_for::const_for;
use std::mem::MaybeUninit;
use super::{Board, CoordPair};
/// Map of all points on the board against some type T
/// Used to index like so: example[i][j]
/// with each coordinate
pub struct PosMap<T>([T; Board::AREA.0 as usize]);
impl<T: Copy> PosMap<T> {
pub const unsafe fn uninit() -> Self {
Self([MaybeUninit::zeroed().assume_init(); Board::AREA.0 as usize])
}
pub const fn from(v: [[T; Board::SIZE as usize]; Board::SIZE as usize]) -> Self {
let mut n = unsafe { Self::uninit() };
const_for!(i in 0..Board::SIZE => {
const_for!(j in 0..Board::SIZE => {
n.set(CoordPair::from_axes(i, j), v[i as usize][j as usize]);
});
});
n
}
pub const fn into_2d(&self) -> [[T; Board::SIZE as usize]; Board::SIZE as usize] {
let mut n: [[T; Board::SIZE as usize]; Board::SIZE as usize] =
unsafe { MaybeUninit::zeroed().assume_init() };
const_for!(i in 0..Board::SIZE => {
const_for!(j in 0..Board::SIZE => {
n[i as usize][j as usize] = *self.get(CoordPair::from_axes(i, j));
});
});
n
}
pub const fn get(&self, coords: CoordPair) -> &T {
&self.0[coords.0 as usize]
}
pub const fn set(&mut self, coords: CoordPair, value: T) {
self.0[coords.0 as usize] = value;
}
}
#[cfg(test)]
mod test {
use crate::repr::coords::CoordPairInner;
use super::*;
#[test]
fn from_into_test() {
let data: [[CoordPairInner; Board::SIZE as usize]; Board::SIZE as usize] = (0..Board::AREA
.0)
.collect::<Vec<_>>()
.chunks(Board::SIZE as usize)
.map(|x| -> [CoordPairInner; Board::SIZE as usize] { x.to_vec().try_into().unwrap() })
.collect::<Vec<_>>()
.try_into()
.unwrap();
let posmap = PosMap::from(data.clone());
assert_eq!(data, posmap.into_2d());
}
}