Skip to main content

sde_sim_rs/proc/
mod.rs

1pub mod increment;
2pub mod util;
3
4use crate::func::Function;
5use std::collections::HashMap;
6
7#[derive(Clone)]
8pub struct AlgebraicProcess {
9    pub name: String,
10    pub coefficients: Vec<Box<Function>>,
11}
12
13pub struct LevyProcess {
14    pub name: String,
15    pub coefficients: Vec<Box<Function>>,
16    pub incrementors: Vec<Box<dyn increment::Incrementor>>,
17}
18
19impl Clone for LevyProcess {
20    fn clone(&self) -> Self {
21        Self {
22            name: self.name.clone(),
23            coefficients: self.coefficients.clone(),
24            incrementors: self.incrementors.iter().map(|i| i.clone_box()).collect(),
25        }
26    }
27}
28
29impl LevyProcess {
30    pub fn new(
31        name: String,
32        coefficients: Vec<Box<Function>>,
33        incrementors: Vec<Box<dyn increment::Incrementor>>,
34    ) -> Result<Self, String> {
35        if coefficients.len() != incrementors.len() {
36            return Err("Number of coefficients must match incrementors".into());
37        }
38        Ok(Self {
39            name,
40            coefficients,
41            incrementors,
42        })
43    }
44}
45
46#[derive(Clone)]
47pub enum Process {
48    Algebraic(Box<AlgebraicProcess>),
49    Levy(Box<LevyProcess>),
50}
51
52impl Process {
53    pub fn name(&self) -> &str {
54        match self {
55            Process::Levy(p) => &p.name,
56            Process::Algebraic(p) => &p.name,
57        }
58    }
59}
60
61#[derive(Clone)]
62pub struct ProcessUniverse {
63    pub processes: Vec<Process>,
64    pub process_registry: HashMap<String, usize>,
65    pub stochastic_registry: HashMap<String, usize>,
66    pub levy_process_indices: Vec<usize>,
67    pub algebraic_process_indices: Vec<usize>,
68}
69
70impl ProcessUniverse {
71    pub fn new(processes: Vec<Process>, stochastic_registry: HashMap<String, usize>) -> Self {
72        let mut levy_process_indices = Vec::new();
73        let mut algebraic_process_indices = Vec::new();
74        let mut process_registry = HashMap::with_capacity(processes.len());
75        for (idx, proc) in processes.iter().enumerate() {
76            process_registry.insert(proc.name().to_string(), idx);
77            match proc {
78                Process::Levy(_) => levy_process_indices.push(idx),
79                Process::Algebraic(_) => algebraic_process_indices.push(idx),
80            }
81        }
82        Self {
83            processes,
84            process_registry,
85            stochastic_registry,
86            levy_process_indices,
87            algebraic_process_indices,
88        }
89    }
90}