use std::collections::HashMap;
use screeps::{LocalCostMatrix, RoomName};
#[derive(Debug, Clone)]
pub struct LCMCache {
cache: HashMap<RoomName, LocalCostMatrix>,
}
impl Default for LCMCache {
fn default() -> Self {
Self::new()
}
}
impl LCMCache {
pub fn new() -> Self {
Self {
cache: HashMap::new(),
}
}
pub fn get_cached_lcm(&self, room_name: &RoomName) -> Option<&LocalCostMatrix> {
self.cache.get(room_name)
}
pub fn is_lcm_cached(&self, room_name: &RoomName) -> bool {
self.cache.contains_key(room_name)
}
pub fn get_lcm(
&mut self,
room_name: &RoomName,
generator_fn: impl FnOnce(&RoomName) -> LocalCostMatrix,
) -> &LocalCostMatrix {
if !self.is_lcm_cached(room_name) {
let lcm = generator_fn(room_name);
let _ = self.cache.insert(*room_name, lcm);
}
self.get_cached_lcm(room_name).unwrap() }
pub fn update_cached_lcm(&mut self, room_name: RoomName, lcm: LocalCostMatrix) {
let _ = self.cache.insert(room_name, lcm);
}
pub fn remove_cached_lcm(&mut self, room_name: &RoomName) {
self.cache.remove(room_name);
}
}