diff options
Diffstat (limited to 'src/function_cache.rs')
| -rw-r--r-- | src/function_cache.rs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/function_cache.rs b/src/function_cache.rs new file mode 100644 index 0000000..d4beb16 --- /dev/null +++ b/src/function_cache.rs @@ -0,0 +1,32 @@ +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<Complex, Complex>, +} + +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 + } + } +} |