From 1713618d4cc0194674f91fd2d24ef2de88f21784 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Thu, 18 Jan 2024 18:29:10 +0100 Subject: create small iced demo --- src/math/context.rs | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/math/context.rs (limited to 'src/math/context.rs') diff --git a/src/math/context.rs b/src/math/context.rs new file mode 100644 index 0000000..cb0bca0 --- /dev/null +++ b/src/math/context.rs @@ -0,0 +1,74 @@ +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) + } +} + +#[macro_export] +macro_rules! variables { + {$($x:expr => $y:expr), *} => { + { + let mut h : HashMap = HashMap::new(); + $( + h.insert($x.to_string(), $y); + )* + h + } + }; +} -- cgit v1.2.3-70-g09d2