use std::collections::HashMap;
use std::hash::Hash;
pub(crate) struct ResultCache<V, K: Eq + Hash + Clone> {
cache: HashMap<K, HashMap<K, V>>,
}
impl<V: Clone, K: Eq + Hash + Clone> Default for ResultCache<V, K> {
fn default() -> Self {
ResultCache {
cache: HashMap::new(),
}
}
}
impl<V: Clone, K: Eq + Hash + Clone> ResultCache<V, K> {
pub(crate) fn new() -> Self {
Self::default()
}
pub(crate) fn add_result(&mut self, a: K, b: K, result: V) {
self.cache
.entry(a.clone())
.or_default()
.insert(b.clone(), result.clone());
self.cache.entry(b).or_default().insert(a, result);
}
pub(crate) fn get_result(&self, a: &K, b: &K) -> Option<V> {
self.cache.get(a).and_then(|m| m.get(b)).cloned()
}
}