Add basic parts of the Transposition Table

This commit is contained in:
stefiosif
2024-10-13 17:32:42 +03:00
parent c40e8478c8
commit 33617df497
3 changed files with 83 additions and 1 deletions

View File

@@ -6,4 +6,5 @@ pub mod history;
pub mod mailbox;
pub mod square;
pub mod state;
pub mod transposition_table;
pub mod zobrist;

View File

@@ -0,0 +1,81 @@
use crate::movegen::r#move::Move;
use super::zobrist::ZobristHash;
#[derive(Clone)]
pub struct TranspositionTable {
positions: Vec<Option<TranspositionTableEntry>>,
size: u64,
}
impl TranspositionTable {
pub fn new(size: u64) -> Self {
Self {
positions: (0..size).map(|_| None).collect(),
size,
}
}
pub fn lookup(&self, zobrist_hash: ZobristHash) -> &TranspositionTableEntry {
self.positions
.get((zobrist_hash.hash % self.size) as usize)
.unwrap()
.as_ref()
.unwrap()
}
pub fn insert(&mut self, tt_entry: TranspositionTableEntry, hash: ZobristHash) {
let index = hash.hash % self.size;
self.positions.insert(index as usize, Some(tt_entry));
}
}
#[derive(Clone)]
pub struct TranspositionTableEntry {
hash: ZobristHash,
depth: u8,
alpha: i32,
beta: i32,
mv: Move,
node_type: NodeType,
}
impl TranspositionTableEntry {
pub const fn new(
hash: ZobristHash,
depth: u8,
alpha: i32,
beta: i32,
mv: Move,
node_type: NodeType,
) -> Self {
Self {
hash,
depth,
alpha,
beta,
mv,
node_type,
}
}
}
#[derive(Clone)]
pub enum NodeType {
Exact,
LowerBound,
UpperBound,
}
#[cfg(test)]
mod tests {
#[test]
fn test_transpotisition() -> Result<(), String> {
// TODO:
// >have specific board state
// >run a deep search for this board state
// >test some expected outcomes that should have been stored in the TT
Ok(())
}
}

View File

@@ -106,7 +106,7 @@ impl Default for ZobristKeys {
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
pub struct ZobristHash {
hash: u64,
pub hash: u64,
}
impl ZobristHash {