aboutsummaryrefslogtreecommitdiff
path: root/src/math/context.rs
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2024-01-18 18:29:10 +0100
committerNathan Reiner <nathan@nathanreiner.xyz>2024-01-18 18:29:10 +0100
commit1713618d4cc0194674f91fd2d24ef2de88f21784 (patch)
tree1cb39a43019c071ca127cb9f609c045327798de3 /src/math/context.rs
parent670c1881af4680ce7c248498528d14b98210af3f (diff)
create small iced demo
Diffstat (limited to 'src/math/context.rs')
-rw-r--r--src/math/context.rs74
1 files changed, 74 insertions, 0 deletions
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<Operation>,
+ vars: HashMap<String, Complex>,
+ funcs: HashMap<String, Arc<dyn Function>>,
+}
+
+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<Operation>) -> Self {
+ self.ops = ops;
+ self
+ }
+
+ pub fn with_variables(mut self, vars: HashMap<String, Complex>) -> Self {
+ self.vars = vars;
+ self
+ }
+
+ pub fn with_functions(mut self, funcs: HashMap<String, Arc<dyn Function>>) -> Self {
+ self.funcs = funcs;
+ self
+ }
+
+ pub fn operations(&self) -> &Vec<Operation> {
+ &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<dyn Function>) {
+ 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<dyn Function>> {
+ self.funcs.get(name)
+ }
+}
+
+#[macro_export]
+macro_rules! variables {
+ {$($x:expr => $y:expr), *} => {
+ {
+ let mut h : HashMap<String, Complex> = HashMap::new();
+ $(
+ h.insert($x.to_string(), $y);
+ )*
+ h
+ }
+ };
+}