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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use super::VariableRef;

use linked_hash_map::{Entry, LinkedHashMap};
use std::fmt;

#[derive(Default)]
pub struct Model {
    variables: Vec<VariableRef>,
    variable_indexes: LinkedHashMap<String, usize>,
    most_recent: usize,
}

impl Model {
    pub fn new() -> Model {
        Default::default()
    }

    /// Adds a variable to the model
    ///
    /// If the variable `name` already exists in the model, the pre-existing variable is replaced
    /// by the `variable`.
    ///
    /// Returns the index of the variable which saves a call to `get_variable_index`.
    pub fn add_variable(&mut self, name: &str, variable: VariableRef) -> usize {
        let variables = &mut self.variables;
        let most_recent = &mut self.most_recent;

        match self.variable_indexes.entry(name.into()) {
            Entry::Occupied(entry) => {
                *most_recent = *entry.get();
                variables[*most_recent] = variable;

                *most_recent
            }
            Entry::Vacant(entry) => {
                variables.push(variable);
                *most_recent = variables.len() - 1;
                entry.insert(*most_recent);

                *most_recent
            }
        }
    }

    pub fn get_variable_index(&self, name: &str) -> Option<usize> {
        let index = self.variable_indexes.get(name)?;
        Some(*index)
    }

    pub fn get_variable_by_index(&self, index: usize) -> Option<&VariableRef> {
        let variable = self.variables.get(index)?;
        Some(variable)
    }

    pub fn get_variable_by_name(&self, name: &str) -> Option<&VariableRef> {
        let index = self.variable_indexes.get(name)?;
        let variable = self.variables.get(*index)?;
        Some(variable)
    }

    pub fn get_most_recently_added(&self) -> Option<&VariableRef> {
        self.variables.get(self.most_recent)
    }

    pub fn variables_iter(&self) -> VariablesIter {
        VariablesIter {
            iter: self.variable_indexes.iter(),
            variables: &self.variables,
        }
    }
}

impl fmt::Display for Model {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        for (name, variable) in self.variables_iter() {
            write!(f, "{} = ", name)?;
            variable.borrow().fmt(f)?;
            writeln!(f, ";")?;
        }

        Ok(())
    }
}

pub struct VariablesIter<'a> {
    iter: ::linked_hash_map::Iter<'a, String, usize>,
    variables: &'a Vec<VariableRef>,
}

impl<'a> Iterator for VariablesIter<'a> {
    type Item = (&'a str, &'a VariableRef);

    fn next(&mut self) -> Option<(&'a str, &'a VariableRef)> {
        let next = self.iter.next()?;

        Some((next.0, &self.variables[*next.1]))
    }
}