// src/tt.rs use crate::types::Move; #[derive(Clone, Copy)] pub struct TTEntry { pub hash: u64, pub depth: u8, pub flag: u8, pub score: i32, pub best_move: Move, } pub const TT_EXACT: u8 = 0; pub const TT_ALPHA: u8 = 1; pub const TT_BETA: u8 = 2; pub struct TranspositionTable { entries: Vec, size_mask: usize, } impl TranspositionTable { pub fn new(mb: usize) -> Self { let num_entries = (mb * 1024 * 1024) / std::mem::size_of::(); let mut capacity = 1; while capacity <= num_entries { capacity *= 2; } capacity /= 2; Self { entries: vec![ TTEntry { hash: 0, depth: 0, flag: 0, score: 0, best_move: Move::NONE, }; capacity ], size_mask: capacity - 1, } } pub fn store(&mut self, hash: u64, depth: u8, flag: u8, score: i32, best_move: Move) { let index = (hash as usize) & self.size_mask; self.entries[index] = TTEntry { hash, depth, flag, score, best_move, }; } pub fn probe(&self, hash: u64) -> Option { let index = (hash as usize) & self.size_mask; let entry = self.entries[index]; if entry.hash == hash { Some(entry) } else { None } } pub fn clear(&mut self) { for entry in &mut self.entries { entry.hash = 0; entry.depth = 0; entry.flag = 0; entry.score = 0; entry.best_move = Move::NONE; } } }