use crate::math::complex::Complex; use crate::math::function::Function; use crate::math::operation::Operation; use crate::{commonsense_functions, commonsense_operations, commonsense_variables, functions, variables}; use std::collections::HashMap; use std::sync::Arc; #[derive(Default, Clone)] pub struct Context { ops: Vec, vars: HashMap, funcs: HashMap>, } impl Context { pub fn new() -> Self { Self::default() } pub fn commonsense() -> Self { Self::default() .with_operations(commonsense_operations!{}) .with_functions(commonsense_functions!{}) .with_variables(commonsense_variables!{}) } pub fn with_operations(mut self, ops: Vec) -> Self { self.ops = ops; self } pub fn with_variables(mut self, vars: HashMap) -> Self { self.vars = vars; self } pub fn with_functions(mut self, funcs: HashMap>) -> Self { self.funcs = funcs; self } pub fn operations(&self) -> &Vec { &self.ops } pub fn set_variable(&mut self, name: &str, value: Complex) { self.vars.insert(name.to_string(), value); } pub fn set_function(&mut self, name: &str, func: Arc) { self.funcs.insert(name.to_string(), func); } pub fn variable(&self, name: &str) -> Option<&Complex> { self.vars.get(name) } pub fn function(&self, name: &str) -> Option<&Arc> { self.funcs.get(name) } pub fn remove_variable(&mut self, name: &str) { self.vars.remove(name); } pub fn remove_function(&mut self, name: &str) { self.funcs.remove(name); } } #[macro_export] macro_rules! variables { {$($x:expr => $y:expr), *} => { { let mut h : HashMap = HashMap::new(); $( h.insert($x.to_string(), $y); )* h } }; }