1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
use super::*; use std::collections::HashMap; use kg_utils::collections::LruCache; pub trait OpathCache { fn get(&mut self, n: &NodeRef) -> &Opath; fn contains(&mut self, n: &NodeRef) -> bool; fn len(&self) -> usize; } #[derive(Debug)] pub struct NodePathLruCache { cache: LruCache<*const Node, Opath>, } impl NodePathLruCache { pub fn with_size(size: usize) -> NodePathLruCache { NodePathLruCache { cache: LruCache::new(size), } } } impl OpathCache for NodePathLruCache { fn get(&mut self, n: &NodeRef) -> &Opath { let p = n.data_ptr(); if !self.cache.contains_key(&p) { self.cache.insert(p, Opath::from(n)); } self.cache.get_mut(&p).unwrap() } fn contains(&mut self, n: &NodeRef) -> bool { let p = n.data_ptr(); self.cache.contains_key(&p) } fn len(&self) -> usize { self.cache.len() } } #[derive(Debug)] pub struct NodePathCache { cache: HashMap<*const Node, Opath>, } impl NodePathCache { pub fn new() -> NodePathCache { NodePathCache { cache: HashMap::new(), } } pub fn with_capacity(capacity: usize) -> NodePathCache { NodePathCache { cache: HashMap::with_capacity(capacity), } } } impl OpathCache for NodePathCache { fn get(&mut self, n: &NodeRef) -> &Opath { let p = n.data_ptr(); if !self.cache.contains_key(&p) { self.cache.insert(p, Opath::from(n)); } self.cache.get(&p).unwrap() } fn contains(&mut self, n: &NodeRef) -> bool { let p = n.data_ptr(); self.cache.contains_key(&p) } fn len(&self) -> usize { self.cache.len() } }