use std::{sync::Arc, hash::Hash};
use dashmap::DashMap;
use crate::{Cache, Threshold};
#[derive(Debug)]
pub struct SimpleCache<T>
where T: Hash + Eq {
thresholds_by_layer: Vec<DashMap<Arc<T>, Threshold, fxhash::FxBuildHasher>>,
}
impl <T> Default for SimpleCache<T>
where T: Hash + Eq {
fn default() -> Self {
Self { thresholds_by_layer: vec![] }
}
}
impl<T> Cache for SimpleCache<T>
where T: Hash + Eq {
type State = T;
fn initialize(&mut self, problem: &dyn crate::Problem<State = Self::State>) {
let nb_variables = problem.nb_variables();
for _ in 0..=nb_variables {
self.thresholds_by_layer.push(Default::default());
}
}
fn get_threshold(&self, state: &T, depth: usize) -> Option<Threshold> {
self.thresholds_by_layer[depth].get(state).as_deref().copied()
}
fn update_threshold(&self, state: Arc<T>, depth: usize, value: isize, explored: bool) {
self.thresholds_by_layer[depth].entry(state)
.and_modify(|e| *e = Threshold { value, explored }.max(*e))
.or_insert(Threshold { value, explored });
}
fn clear_layer(&self, depth: usize) {
self.thresholds_by_layer[depth].clear();
}
fn clear(&self) {
self.thresholds_by_layer.iter().for_each(|l| l.clear());
}
}