use game::Game; use evaluate::PosValue; use std::collections::{HashMap}; use std::hash::{BuildHasher, Hasher, Hash}; use log::info; #[derive(Clone)] pub enum EntryType { Value, LowerBound, UpperBound, } #[derive(Clone)] pub struct CacheEntry { entry_type: EntryType, depth: i32, value: PosValue } impl CacheEntry { pub fn new_value(depth: i32, value: PosValue) -> Self { CacheEntry { entry_type: EntryType::Value, depth, value } } } pub struct Cache { hashmap: HashMap, } impl Cache { pub fn new() -> Self { Cache { hashmap: HashMap::new() } } pub fn lookup(&self, game_pos: &Game) -> Option { self.hashmap.get(game_pos).map(|x| x.clone()) } pub fn cache(&mut self, game_pos: &Game, ce: CacheEntry) { if self.hashmap.len() > 1000000 { let first_key = self.hashmap.keys().next().unwrap().clone(); self.hashmap.remove(&first_key); } self.hashmap.insert(game_pos.clone(), ce); if self.hashmap.len() % 1024 == 0 { info!("hash contains {} items", self.hashmap.len()); } } }