hash.rs 917 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. use game::Game;
  2. use evaluate::PosValue;
  3. use std::collections::{HashMap};
  4. use std::hash::{BuildHasher, Hasher, Hash};
  5. use log::info;
  6. #[derive(Clone)]
  7. pub enum EntryType {
  8. Value,
  9. LowerBound,
  10. UpperBound,
  11. }
  12. #[derive(Clone)]
  13. pub struct CacheEntry {
  14. entry_type: EntryType,
  15. depth: i32,
  16. value: PosValue
  17. }
  18. pub struct Cache {
  19. hashmap: HashMap<Game, CacheEntry>,
  20. }
  21. impl Cache {
  22. pub fn new() -> Self {
  23. Cache {
  24. hashmap: HashMap::new()
  25. }
  26. }
  27. pub fn lookup(&self, game_pos: &Game) -> Option<CacheEntry> {
  28. self.hashmap.get(game_pos).map(|x| x.clone())
  29. }
  30. pub fn cache(&mut self, game_pos: &Game, ce: CacheEntry) {
  31. if self.hashmap.len() > 1000000 {
  32. let first_key = self.hashmap.keys().next().unwrap().clone();
  33. self.hashmap.remove(&first_key);
  34. }
  35. self.hashmap.insert(game_pos.clone(), ce);
  36. }
  37. }