1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
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)
}
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<String, Complex> = HashMap::new();
$(
h.insert($x.to_string(), $y);
)*
h
}
};
}
|