use std::collections::HashMap; use crate::math::{ complex::Complex, context::Context, expression_function::ExpressionFunction, function::{FunctionArgument, Function}, }; #[derive(Default)] pub struct FunctionCache { func: ExpressionFunction, cache: HashMap, } impl FunctionCache { pub fn new(func: ExpressionFunction) -> Self { Self { func, cache: HashMap::new(), } } pub fn eval(&mut self, x: Complex, ctx: &Context) -> Complex { if self.cache.contains_key(&x) { *self.cache.get(&x).unwrap() } else { let fargs = FunctionArgument::from_values(vec![x]); let v = self.func.eval(&fargs, ctx).unwrap(); self.cache.insert(x, v); v } } pub fn name(&self) -> &str { self.func.name() } }