screeps_pathfinding/utils/cache/
lcm_cache_struct.rs1use std::collections::HashMap;
2
3use screeps::{LocalCostMatrix, RoomName};
4
5#[derive(Debug, Clone)]
9pub struct LCMCache {
10 cache: HashMap<RoomName, LocalCostMatrix>,
11}
12
13impl Default for LCMCache {
14 fn default() -> Self {
15 Self::new()
16 }
17}
18
19impl LCMCache {
20 pub fn new() -> Self {
22 Self {
23 cache: HashMap::new(),
24 }
25 }
26
27 pub fn get_cached_lcm(&self, room_name: &RoomName) -> Option<&LocalCostMatrix> {
31 self.cache.get(room_name)
32 }
33
34 pub fn is_lcm_cached(&self, room_name: &RoomName) -> bool {
36 self.cache.contains_key(room_name)
37 }
38
39 pub fn get_lcm(
41 &mut self,
42 room_name: &RoomName,
43 generator_fn: impl FnOnce(&RoomName) -> LocalCostMatrix,
44 ) -> &LocalCostMatrix {
45 if !self.is_lcm_cached(room_name) {
46 let lcm = generator_fn(room_name);
48 let _ = self.cache.insert(*room_name, lcm);
49 }
50
51 self.get_cached_lcm(room_name).unwrap() }
53
54 pub fn update_cached_lcm(&mut self, room_name: RoomName, lcm: LocalCostMatrix) {
59 let _ = self.cache.insert(room_name, lcm);
60 }
61
62 pub fn remove_cached_lcm(&mut self, room_name: &RoomName) {
64 self.cache.remove(room_name);
65 }
66}