Add basic parts of the Transposition Table
This commit is contained in:
@@ -6,4 +6,5 @@ pub mod history;
|
||||
pub mod mailbox;
|
||||
pub mod square;
|
||||
pub mod state;
|
||||
pub mod transposition_table;
|
||||
pub mod zobrist;
|
||||
|
||||
81
src/board/transposition_table.rs
Normal file
81
src/board/transposition_table.rs
Normal 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(())
|
||||
}
|
||||
}
|
||||
@@ -106,7 +106,7 @@ impl Default for ZobristKeys {
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
|
||||
pub struct ZobristHash {
|
||||
hash: u64,
|
||||
pub hash: u64,
|
||||
}
|
||||
|
||||
impl ZobristHash {
|
||||
|
||||
Reference in New Issue
Block a user