aboutsummaryrefslogtreecommitdiffstats
path: root/modelica-parser-lalrpop/src
diff options
context:
space:
mode:
authorbnewbold <bnewbold@robocracy.org>2016-12-26 00:43:58 -0800
committerbnewbold <bnewbold@robocracy.org>2016-12-26 00:43:58 -0800
commit851d72cdd58a51adcf93bcd9a5956d82d3b5417d (patch)
treeff37476a74d39926e8c52de470040bbd3b5c7bba /modelica-parser-lalrpop/src
parent9cf287fef8e614f57d0d52d579d6721ba54683f4 (diff)
downloadmodelthing-851d72cdd58a51adcf93bcd9a5956d82d3b5417d.tar.gz
modelthing-851d72cdd58a51adcf93bcd9a5956d82d3b5417d.zip
parser: refactor vec operations into hashset
Diffstat (limited to 'modelica-parser-lalrpop/src')
-rw-r--r--modelica-parser-lalrpop/src/ast.rs44
1 files changed, 21 insertions, 23 deletions
diff --git a/modelica-parser-lalrpop/src/ast.rs b/modelica-parser-lalrpop/src/ast.rs
index 7eb9dda..f31681e 100644
--- a/modelica-parser-lalrpop/src/ast.rs
+++ b/modelica-parser-lalrpop/src/ast.rs
@@ -1,7 +1,8 @@
use std::clone::Clone;
use std::fmt::{Debug, Formatter, Error};
-use std::collections::HashMap;
+use std::collections::{HashMap, HashSet};
+use std::iter::FromIterator;
#[derive(Clone, PartialEq)]
@@ -146,7 +147,7 @@ impl ModelicaModel {
// the sole element on the LHS of an equation.
// Bugs:
// if a var is on LHS and RHS of same equation
- pub fn get_free_vars(&self) -> Vec<String> {
+ pub fn get_free_vars(&self) -> HashSet<String> {
// Start with components, and remove constants and parameters
let vars = self.components.iter().filter(|v| match v.prefix {
Some(ComponentPrefix::Constant) | Some(ComponentPrefix::Parameter) => false,
@@ -164,39 +165,34 @@ impl ModelicaModel {
}
let vars = vars.filter(|v| !outputs.contains(&v.name));
- vars.map(|c| c.name.clone()).collect()
+ HashSet::from_iter(vars.map(|c| c.name.clone()))
}
}
-fn union_strings(a: &Vec<String>, b: &Vec<String>) -> Vec<String> {
- let mut u = a.clone();
- for e in b {
- if !(u.contains(&e)) {
- u.push(e.clone());
- }
- }
- u
-}
-
impl Expr {
// Order is undefined
- // TODO: should return a HashSet, not a Vec
- pub fn identifiers(&self) -> Vec<String> {
+ pub fn identifiers(&self) -> HashSet<String> {
use self::Expr::*;
match *self {
- Integer(_) | Float(_) | Boolean(_) | StringLiteral(_) => vec![],
- Ident(ref s) => vec![s.clone()],
+ Integer(_) | Float(_) | Boolean(_) | StringLiteral(_) => HashSet::new(),
+ Ident(ref s) => {
+ let mut hs = HashSet::new();
+ hs.insert(s.to_string());
+ hs
+ },
Der(ref e) | Sign(ref e) => e.identifiers(),
MathUnaryExpr(_, ref e) => e.identifiers(),
BinExpr(_, ref e1, ref e2) => {
- union_strings(&e1.identifiers(), &e2.identifiers())
+ let mut hs = e1.identifiers();
+ hs.extend(e2.identifiers());
+ hs
},
Array(ref el) => {
- let mut all: Vec<String> = vec![];
+ let mut all: HashSet<String> = HashSet::new();
for e in el {
- all.append(&mut e.identifiers());
+ all.extend(e.identifiers());
}
all
}
@@ -204,15 +200,17 @@ impl Expr {
}
pub fn contains(&self, ident: &str) -> bool{
- self.identifiers().contains(&ident.to_string())
+ self.identifiers().contains(ident)
}
}
impl SimpleEquation {
// Order is undefined
- pub fn identifiers(&self) -> Vec<String> {
- union_strings(&self.lhs.identifiers(), &self.rhs.identifiers())
+ pub fn identifiers(&self) -> HashSet<String> {
+ let mut hs = self.lhs.identifiers();
+ hs.extend(self.rhs.identifiers());
+ hs
}
pub fn contains(&self, ident: &str) -> bool{