use std::collections::HashMap;
use std::sync::Arc;
use std::sync::Mutex;
use crate::compute::computation::Value;
use crate::symbolic::core::Expr;
pub struct ParsingCache {
cache: Mutex<HashMap<String, Arc<Expr>>>,
}
impl ParsingCache {
#[must_use]
pub fn new() -> Self {
Self {
cache: Mutex::new(HashMap::new()),
}
}
pub fn get(
&self,
input: &str,
) -> Option<Arc<Expr>> {
let cache = self.cache.lock().expect(
"ParsingCache lock \
poisoned",
);
cache.get(input).cloned()
}
pub fn set(
&self,
input: String,
expr: Arc<Expr>,
) {
let mut cache = self.cache.lock().expect(
"ParsingCache lock \
poisoned",
);
cache.insert(input, expr);
}
pub fn clear(&self) {
let mut cache = self.cache.lock().expect(
"ParsingCache lock \
poisoned",
);
cache.clear();
}
}
impl Default for ParsingCache {
fn default() -> Self {
Self::new()
}
}
pub struct ComputationResultCache {
cache: Mutex<HashMap<Arc<Expr>, Value>>,
}
impl ComputationResultCache {
#[must_use]
pub fn new() -> Self {
Self {
cache: Mutex::new(HashMap::new()),
}
}
pub fn get(
&self,
expr: &Arc<Expr>,
) -> Option<Value> {
let cache = self.cache.lock().expect(
"ComputationResultCache \
lock poisoned",
);
cache.get(expr).cloned()
}
pub fn set(
&self,
expr: Arc<Expr>,
value: Value,
) {
let mut cache = self
.cache
.lock()
.expect("ComputationResultCache lock poisoned");
cache.insert(expr, value);
}
pub fn clear(&self) {
let mut cache = self
.cache
.lock()
.expect("ComputationResultCache lock poisoned");
cache.clear();
}
}
impl Default for ComputationResultCache {
fn default() -> Self {
Self::new()
}
}