aboutsummaryrefslogtreecommitdiff
path: root/src/context.rs
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2024-01-17 22:30:02 +0100
committerNathan Reiner <nathan@nathanreiner.xyz>2024-01-17 22:30:02 +0100
commit77cf9aa7535a1d9481f0bd3caeea26e2b85c5019 (patch)
tree03b794cc47e14da1d3896c4251c89fa6c40a7132 /src/context.rs
parent5ccc6e5ec8a433f68bfeb17e8dcedec5b62a36a9 (diff)
migrate from f64 to own complex
Diffstat (limited to 'src/context.rs')
-rw-r--r--src/context.rs24
1 files changed, 20 insertions, 4 deletions
diff --git a/src/context.rs b/src/context.rs
index 12bea6b..47be92b 100644
--- a/src/context.rs
+++ b/src/context.rs
@@ -1,11 +1,12 @@
use crate::function::Function;
use crate::operation::Operation;
+use crate::complex::Complex;
use std::collections::HashMap;
#[derive(Default)]
pub struct Context {
ops: Vec<Operation>,
- vars: HashMap<String, f64>,
+ vars: HashMap<String, Complex>,
funcs: HashMap<String, Box<dyn Function>>,
}
@@ -19,7 +20,7 @@ impl Context {
self
}
- pub fn with_variables(mut self, vars: HashMap<String, f64>) -> Self {
+ pub fn with_variables(mut self, vars: HashMap<String, Complex>) -> Self {
self.vars = vars;
self
}
@@ -33,7 +34,7 @@ impl Context {
&self.ops
}
- pub fn variable_mut(&mut self, name: &str) -> Option<&mut f64> {
+ pub fn variable_mut(&mut self, name: &str) -> Option<&mut Complex> {
self.vars.get_mut(name)
}
@@ -41,11 +42,26 @@ impl Context {
self.funcs.get_mut(name)
}
- pub fn variable(&self, name: &str) -> Option<&f64> {
+ pub fn variable(&self, name: &str) -> Option<&Complex> {
self.vars.get(name)
}
+ #[allow(clippy::borrowed_box)]
pub fn function(&self, name: &str) -> Option<&Box<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
+ }
+ };
+}
+