Skip to content

Commit

Permalink
optimize some list operations
Browse files Browse the repository at this point in the history
matchcontinue -> match
  • Loading branch information
hkiel authored and OpenModelica-Hudson committed Mar 3, 2016
1 parent 3a40ea3 commit f0f455b
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 219 deletions.
25 changes: 11 additions & 14 deletions Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -57,6 +57,7 @@ protected import ExpressionSimplify;
protected import Flags;
protected import HashSet;
protected import List;
protected import MetaModelica.Dangerous;
protected import Util;
protected import Types;

Expand Down Expand Up @@ -2043,21 +2044,17 @@ end listVar1;

public function equationSystemsVarsLst
input BackendDAE.EqSystems systs;
input list<BackendDAE.Var> inVars;
output list<BackendDAE.Var> outVars;
output list<BackendDAE.Var> outVars = {};
protected
list<BackendDAE.Var> vars;
BackendDAE.Variables v;
algorithm
outVars := match (systs)
local
BackendDAE.EqSystems rest;
list<BackendDAE.Var> vars, vars1;
BackendDAE.Variables v;

case {} then inVars;
case BackendDAE.EQSYSTEM(orderedVars=v)::rest equation
vars = varList(v);
vars1 = listAppend(inVars,vars);
then equationSystemsVarsLst(rest,vars1);
end match;
for es in systs loop
BackendDAE.EQSYSTEM(orderedVars=v) := es;
vars := varList(v);
outVars := List.append_reverse(vars, outVars);
end for;
outVars := MetaModelica.Dangerous.listReverseInPlace(outVars);
end equationSystemsVarsLst;

public function daeVars "returns orderedVars"
Expand Down
28 changes: 14 additions & 14 deletions Compiler/BackEnd/Differentiate.mo
Expand Up @@ -2655,15 +2655,15 @@ algorithm
String matrixName;

case ({}, _, _, _, _, _, _, _)
then (inElementsDer, inFunctionTree, inElementsNoDer, inBooleanLst);
then (MetaModelica.Dangerous.listReverseInPlace(inElementsDer), inFunctionTree, MetaModelica.Dangerous.listReverseInPlace(inElementsNoDer), MetaModelica.Dangerous.listReverseInPlace(inBooleanLst));

case ((var1 as DAE.VAR(componentRef = cref, ty= (DAE.T_COMPLEX(varLst=varLst,complexClassType=ClassInf.RECORD())), binding=SOME(binding)))::rest, _, BackendDAE.DIFFINPUTDATA(matrixName=SOME(matrixName)), _, _, _, _, _) equation
dcref = createDiffedCrefName(cref, matrixName);
var = DAEUtil.replaceCrefInVar(dcref, var1);
(dbinding, functions) = differentiateExp(binding, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter, expStack);
var = DAEUtil.replaceBindungInVar(dbinding, var);
vars = listAppend(inElementsDer, {var});
blst = listAppend(inBooleanLst, {true});
vars = var::inElementsDer;
blst = true::inBooleanLst;
(vars, functions, elementsNoDer, blst) = differentiateElementVars(rest, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, vars, inElementsNoDer, blst, maxIter, expStack);
then (vars, functions, elementsNoDer, blst);

Expand All @@ -2674,10 +2674,10 @@ algorithm
//crefLst = List.map1(varLst,Expression.generateCrefsFromExpVar,dcref);
//tpLst = List.map(varLst,Types.getVarType);
//newVars = List.threadMap1(crefLst, tpLst, DAEUtil.replaceCrefandTypeInVar, var);
//elementsNoDer = listAppend(inElementsNoDer, newVars);
//elementsNoDer = List.append_reverse(newVars,inElementsNoDer);

vars = listAppend(inElementsDer, {var});
blst = listAppend(inBooleanLst, {true});
vars = var::inElementsDer;
blst = true::inBooleanLst;
(vars, functions, elementsNoDer, blst) = differentiateElementVars(rest, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, vars, inElementsNoDer, blst, maxIter, expStack);
then (vars, functions, elementsNoDer, blst);

Expand All @@ -2686,8 +2686,8 @@ algorithm
crefLst = Expression.extractCrefsFromExp(binding);
({},{}) = BackendVariable.getVarLst(crefLst, timevars, {}, {});

vars = listAppend(inElementsNoDer, {var});
blst = listAppend(inBooleanLst, {false});
vars = var::inElementsNoDer;
blst = false::inBooleanLst;
(vars, functions, elementsNoDer, blst) = differentiateElementVars(rest, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, inElementsDer, vars, blst, maxIter, expStack);
then (vars, functions, elementsNoDer, blst);

Expand All @@ -2699,8 +2699,8 @@ algorithm
var = DAEUtil.replaceCrefInVar(dcref, var1);
(dbinding, functions) = differentiateExp(binding, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter, expStack);
var = DAEUtil.replaceBindungInVar(dbinding, var);
vars = listAppend(inElementsDer, {var});
blst = listAppend(inBooleanLst, {true});
vars = var::inElementsDer;
blst = true::inBooleanLst;
(vars, functions, elementsNoDer, blst) = differentiateElementVars(rest, inDiffwrtCref, inInputData, inDiffType, functions, vars, inElementsNoDer, blst, maxIter, expStack);
then (vars, functions, elementsNoDer, blst);

Expand All @@ -2710,14 +2710,14 @@ algorithm
(e, functions) = differentiateCrefs(e, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter, expStack);
dcref = Expression.expCref(e);
var = DAEUtil.replaceCrefInVar(dcref, var1);
vars = listAppend(inElementsDer, {var});
blst = listAppend(inBooleanLst, {true});
vars = var::inElementsDer;
blst = true::inBooleanLst;
(vars, functions, elementsNoDer, blst) = differentiateElementVars(rest, inDiffwrtCref, inInputData, inDiffType, functions, vars, inElementsNoDer, blst, maxIter, expStack);
then (vars, functions, elementsNoDer, blst);

case((var as DAE.VAR())::rest, _, _, _, _, _, _, _) equation
elementsNoDer = listAppend(inElementsNoDer, {var});
blst = listAppend(inBooleanLst, {false});
elementsNoDer = var::inElementsNoDer;
blst = false::inBooleanLst;
(vars, functions, elementsNoDer, blst) = differentiateElementVars(rest, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, inElementsDer, elementsNoDer, blst, maxIter, expStack);
then (vars, functions, elementsNoDer, blst);
end matchcontinue;
Expand Down
6 changes: 3 additions & 3 deletions Compiler/BackEnd/EvaluateFunctions.mo
Expand Up @@ -558,9 +558,9 @@ algorithm
end if;

// build the new partial function
elements = listAppend(allInputs,updatedVarOutputs);
elements = listAppend(elements,protectVars);
elements = listAppend(elements,algs);
elements = listAppend(protectVars,algs);
elements = listAppend(updatedVarOutputs,elements);
elements = listAppend(allInputs,elements);
elements = List.unique(elements);
(func,path) = updateFunctionBody(func,elements,idx, updatedVarOutputs, allOutputs);
funcs = if funcIsPartConst then DAEUtil.addDaeFunction({func},funcs) else funcs;
Expand Down
37 changes: 15 additions & 22 deletions Compiler/BackEnd/Tearing.mo
Expand Up @@ -887,7 +887,7 @@ algorithm
markTVars(tSel_always,ass1);
(_,unsolv,_) = List.intersection1OnTrue(unsolvables,tSel_always,intEq);
// equations not yet assigned containing the tvars
vareqns = findVareqns(ass2,isAssignedSaveEnhanced,mt,tSel_always,{});
vareqns = findVareqns(ass2,isAssignedSaveEnhanced,mt,tSel_always);
if Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
print("Assignable equations containing new tvars:\n");
BackendDump.dumpAdjacencyRowEnhanced(vareqns);
Expand Down Expand Up @@ -916,26 +916,17 @@ protected function findVareqns
input CompFunc inCompFunc;
input BackendDAE.AdjacencyMatrixTEnhanced mt;
input list<Integer> tSel_alwaysIn;
input list<tuple<Integer,BackendDAE.Solvability>> vareqnsIn;
output list<tuple<Integer,BackendDAE.Solvability>> vareqnsOut;
output list<tuple<Integer,BackendDAE.Solvability>> vareqnsOut = {};
partial function CompFunc
input array<Integer> inValue;
input tuple<Integer,BackendDAE.Solvability> inElement;
output Boolean outIsEqual;
end CompFunc;
algorithm
vareqnsOut := match(ass2In,inCompFunc,mt,tSel_alwaysIn,vareqnsIn)
local
Integer tvar;
list<Integer> rest;
list<tuple<Integer,BackendDAE.Solvability>> vareqns;
case(_,_,_,{},_)
then List.unique(vareqnsIn);
case(_,_,_,tvar::rest,_)
equation
vareqns = List.removeOnTrue(ass2In,inCompFunc,mt[tvar]);
then findVareqns(ass2In,inCompFunc,mt,rest,listAppend(vareqnsIn,vareqns));
end match;
for tvar in tSel_alwaysIn loop
vareqnsOut := List.append_reverse(List.removeOnTrue(ass2In,inCompFunc,mt[tvar]), vareqnsOut);
end for;
vareqnsOut := List.unique(vareqnsOut);
end findVareqns;


Expand Down Expand Up @@ -1414,29 +1405,31 @@ protected function tearingBFS1 " function checks for possible assignments and ca
input BackendDAE.AdjacencyMatrixElementEnhanced inNextQueue;
output BackendDAE.AdjacencyMatrixElementEnhanced outNextQueue;
algorithm
outNextQueue := matchcontinue(rows,size,c,mt,ass1,ass2,inNextQueue)
outNextQueue := match(rows,size,c,mt,ass1,ass2,inNextQueue)
local
// there is only one variable assignable from this equation and the equation is solvable for this variable
case (_,_,_,_,_,_,_)
guard
intEq(listLength(rows),size) and
solvableLst(rows)
equation
true = intEq(listLength(rows),size);
true = solvableLst(rows);
if Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
print("Assign Eqns: " + stringDelimitList(List.map(c,intString),", ") + "\n");
end if;
then
// make assignment and get next equations
tearingBFS2(rows,c,mt,ass1,ass2,inNextQueue);
/* case (_,_,_,_,_,_,_)
guard
intEq(listLength(rows),size) and
not solvableLst(rows);
equation
true = intEq(listLength(rows),size);
false = solvableLst(rows);
//fcall(Flags.TEARING_DUMPVERBOSE, print,"cannot Assign Var" + intString(r) + " with Eqn " + intString(c) + "\n");
then
inNextQueue;
*/
else inNextQueue;
end matchcontinue;
end match;
end tearingBFS1;


Expand Down Expand Up @@ -2990,7 +2983,7 @@ algorithm
end if;

// 2. Collect all variables from different potential-sets in one list
selectedvars := listAppend(listAppend(listAppend(listAppend(listAppend(listAppend(listAppend(listAppend(listAppend(potentials1,potentials2),potentials3),potentials4),potentials5),potentials6),potentials7),potentials8),potentials9),potentials10);
selectedvars := listAppend(potentials1,listAppend(potentials2,listAppend(potentials3,listAppend(potentials4,listAppend(potentials5,listAppend(potentials6,listAppend(potentials7,listAppend(potentials8,listAppend(potentials9,potentials10)))))))));
if Flags.isSet(Flags.TEARING_DUMP) or Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
print("1st: "+ stringDelimitList(List.map(selectedvars,intString),",")+"\n(All potentials)\n\n");
end if;
Expand Down
70 changes: 32 additions & 38 deletions Compiler/FrontEnd/DAEUtil.mo
Expand Up @@ -6143,19 +6143,20 @@ end getUniontypePathsElements;

protected function getDAEDeclsFromValueblocks
input list<DAE.Exp> exps;
output list<DAE.Element> outEls;
output list<DAE.Element> outEls = {};
algorithm
outEls := matchcontinue (exps)
local
list<DAE.Exp> rest;
list<DAE.Element> els1,els2;
case {} then {};
case DAE.MATCHEXPRESSION(localDecls = els1)::rest
equation
els2 = getDAEDeclsFromValueblocks(rest);
then listAppend(els1,els2);
case _::rest then getDAEDeclsFromValueblocks(rest);
end matchcontinue;
for ex in exps loop
_ := match ex
local
list<DAE.Element> els1;
case DAE.MATCHEXPRESSION(localDecls = els1)
algorithm
outEls := List.append_reverse(els1, outEls);
then ();
else ();
end match;
end for;
outEls := MetaModelica.Dangerous.listReverseInPlace(outEls);
end getDAEDeclsFromValueblocks;

// protected function transformDerInline "This is not used.
Expand Down Expand Up @@ -7123,7 +7124,7 @@ algorithm

case (true, _, DAE.DAE(els))
equation
els = sortDAEElementsInModelicaCodeOrder(inElements, els, {});
els = sortDAEElementsInModelicaCodeOrder(inElements, els);
then DAE.DAE(els);

end match;
Expand All @@ -7134,32 +7135,25 @@ protected function sortDAEElementsInModelicaCodeOrder
sort the DAE elements back in the order they are in the file"
input list<tuple<SCode.Element, DAE.Mod>> inElements;
input list<DAE.Element> inDaeEls;
input list<DAE.Element> inAcc;
output list<DAE.Element> outDaeEls;
output list<DAE.Element> outDaeEls = {};
protected
list<DAE.Element> rest = inDaeEls;
algorithm
outDaeEls := match(inElements, inDaeEls, inAcc)
local
list<DAE.Element> dae, named, rest, els, acc;
Absyn.Ident name;
list<tuple<SCode.Element, DAE.Mod>> restEl;

case ({}, _, _) then listAppend(inAcc, inDaeEls);

case (((SCode.COMPONENT(name = name),_))::restEl, dae, acc)
equation
(named, rest) = splitVariableNamed(dae, name, {}, {});
acc = listAppend(acc, named);
els = sortDAEElementsInModelicaCodeOrder(restEl, rest, acc);
then
els;

case (((_,_))::restEl, dae, acc)
equation
els = sortDAEElementsInModelicaCodeOrder(restEl, dae, acc);
then
els;

end match;
for e in inElements loop
_ := match e
local
list<DAE.Element> named;
Absyn.Ident name;
case (SCode.COMPONENT(name = name),_)
algorithm
(named, rest) := splitVariableNamed(rest, name, {}, {});
outDaeEls := List.append_reverse(named, outDaeEls);
then ();
else ();
end match;
end for;
outDaeEls := List.append_reverse(inDaeEls, outDaeEls);
outDaeEls := MetaModelica.Dangerous.listReverseInPlace(outDaeEls);
end sortDAEElementsInModelicaCodeOrder;

protected function splitVariableNamed
Expand Down
20 changes: 7 additions & 13 deletions Compiler/FrontEnd/InstUtil.mo
Expand Up @@ -1677,7 +1677,7 @@ algorithm
(l1, l2) = getExpsFromConstrainClass(rp);
(_, se) = Absyn.getExpsFromArrayDimOpt(ado);
(l3, l4) = getExpsFromMod(m);
l1 = listAppend(listAppend(se, l1), l3);
l1 = listAppend(se, listAppend(l1, l3));
l2 = listAppend(l2, l4);
then
(l1, l2);
Expand Down Expand Up @@ -1707,7 +1707,7 @@ algorithm
(l1, l2) = getExpsFromConstrainClass(rp);
(_, se) = Absyn.getExpsFromArrayDim(ad);
(l3, l4) = getExpsFromMod(m);
l1 = listAppend(listAppend(se, l1), l3);
l1 = listAppend(se, listAppend(l1, l3));
l2 = listAppend(l2, l4);
then
(l1, l2);
Expand Down Expand Up @@ -6769,7 +6769,7 @@ protected function extractCorrectClassMod2
input list<DAE.SubMod> premod;
output DAE.Mod omod;
output list<DAE.SubMod> restmods;
algorithm (omod,restmods) := matchcontinue( smod , name , premod)
algorithm (omod,restmods) := match( smod , name , premod)
local
DAE.Mod mod;
DAE.SubMod sub;
Expand All @@ -6779,9 +6779,10 @@ algorithm (omod,restmods) := matchcontinue( smod , name , premod)
case({},_,_) then (DAE.NOMOD(),premod);

case(DAE.NAMEMOD(id, mod) :: rest, _, _)
guard
stringEq(id, name)
equation
true = stringEq(id, name);
rest2 = listAppend(premod,rest);
rest2 = listAppend(premod,rest);
then
(mod, rest2);

Expand All @@ -6790,14 +6791,7 @@ algorithm (omod,restmods) := matchcontinue( smod , name , premod)
(mod,rest2) = extractCorrectClassMod2(rest,name,premod);
then
(mod, sub::rest2);

else
equation
true = Flags.isSet(Flags.FAILTRACE);
Debug.trace("- extract_Correct_Class_Mod_2 failed\n");
then
fail();
end matchcontinue;
end match;
end extractCorrectClassMod2;

public function traverseModAddFinal
Expand Down

0 comments on commit f0f455b

Please sign in to comment.