hash.rs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. impl CacheEntry {
  19. pub fn new_value(depth: i32, value: PosValue) -> Self {
  20. CacheEntry {
  21. entry_type: EntryType::Value,
  22. depth,
  23. value
  24. }
  25. }
  26. }
  27. pub struct Cache {
  28. hashmap: HashMap<Game, CacheEntry>,
  29. }
  30. impl Cache {
  31. pub fn new() -> Self {
  32. Cache {
  33. hashmap: HashMap::new()
  34. }
  35. }
  36. pub fn lookup(&self, game_pos: &Game) -> Option<CacheEntry> {
  37. self.hashmap.get(game_pos).map(|x| x.clone())
  38. }
  39. pub fn cache(&mut self, game_pos: &Game, ce: CacheEntry) {
  40. if self.hashmap.len() > 1000000 {
  41. let first_key = self.hashmap.keys().next().unwrap().clone();
  42. self.hashmap.remove(&first_key);
  43. }
  44. self.hashmap.insert(game_pos.clone(), ce);
  45. if self.hashmap.len() % 1024 == 0 {
  46. info!("hash contains {} items", self.hashmap.len());
  47. }
  48. }
  49. }