Skip to content

Commit

Permalink
Clean up some code
Browse files Browse the repository at this point in the history
avoid warning about incorrect use of listAppend
recursion -> loop
listReverseInPlace where possible
  • Loading branch information
hkiel committed Mar 7, 2016
1 parent 0cdcd6a commit 80b17a1
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 182 deletions.
6 changes: 4 additions & 2 deletions Compiler/BackEnd/BackendDAETransform.mo
Expand Up @@ -62,6 +62,7 @@ protected import SymbolicJacobian;
protected import System;
protected import Util;
protected import Values;
protected import MetaModelica.Dangerous;

// =============================================================================
// strongComponents and stuff
Expand Down Expand Up @@ -137,7 +138,8 @@ algorithm
acc := e :: acc;
end for;

outAcc := listArray(listReverse(acc));
acc := Dangerous.listReverseInPlace(acc);
outAcc := listArray(acc);
end varAssignmentNonScalar;

protected function analyseStrongComponentsScalar "author: Frenkel TUD 2011-05
Expand All @@ -161,7 +163,7 @@ algorithm
outComps := acomp :: outComps;
end for;

outComps := MetaModelica.Dangerous.listReverseInPlace(outComps);
outComps := Dangerous.listReverseInPlace(outComps);
end analyseStrongComponentsScalar;

protected function analyseStrongComponentScalar "author: Frenkel TUD 2011-05"
Expand Down
74 changes: 32 additions & 42 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -94,7 +94,7 @@ protected import Inline;
protected import InlineArrayEquations;
protected import List;
protected import Matching;
protected import MetaModelica.Dangerous.listReverseInPlace;
protected import MetaModelica.Dangerous;
protected import OnRelaxation;
protected import RemoveSimpleEquations;
protected import ResolveLoops;
Expand Down Expand Up @@ -2264,8 +2264,10 @@ algorithm
outIncidenceArrayT := fillincidenceMatrixT(row, row_indices, outIncidenceArrayT);
end for;

outIncidenceArray := listArray(listReverse(iarr));
omapIncRowEqn := listArray(listReverse(imap));
iarr := Dangerous.listReverseInPlace(iarr);
imap := Dangerous.listReverseInPlace(imap);
outIncidenceArray := listArray(iarr);
omapIncRowEqn := listArray(imap);
end incidenceMatrixDispatchScalar;

protected function fillincidenceMatrixT
Expand Down Expand Up @@ -4126,7 +4128,7 @@ algorithm
// special case for it initial() then ... else ... end if; only else branch needs to be checked
case(_,BackendDAE.IF_EQUATION(conditions={DAE.CALL(path=Absyn.IDENT("initial"))},eqnstrue={_},eqnsfalse=eqnselse),_,_,_)
equation
(row,size) = adjacencyRowEnhancedEqnLst(eqnselse,inVariables,mark,rowmark,kvars,{},0,trytosolve);
(row,size) = adjacencyRowEnhancedEqnLst(eqnselse,inVariables,mark,rowmark,kvars,trytosolve);
then
(row,size);

Expand All @@ -4144,7 +4146,7 @@ algorithm
_ = List.fold1(lst,markNegativ,rowmark,mark);
row1 = adjacencyRowEnhanced1(lst,DAE.RCONST(0.0),DAE.RCONST(0.0),vars,kvars,mark,rowmark,{},trytosolve);

(row, size) = adjacencyRowEnhancedEqnLst(eqnselse, vars, mark, rowmark, kvars, {}, 0, trytosolve);
(row, size) = adjacencyRowEnhancedEqnLst(eqnselse, vars, mark, rowmark, kvars, trytosolve);
lst = List.map(row,Util.tuple21);
(lst, row, size) = List.fold5(eqnslst, adjacencyRowEnhancedEqnLstIfBranches, vars, mark, rowmark, kvars, trytosolve, (lst, row, size));

Expand Down Expand Up @@ -4174,23 +4176,20 @@ protected function adjacencyRowEnhancedEqnLstIfBranches
input Boolean trytosolve;
input tuple<list<Integer>,BackendDAE.AdjacencyMatrixElementEnhanced,Integer> intpl;
output tuple<list<Integer>,BackendDAE.AdjacencyMatrixElementEnhanced,Integer> outtpl;
algorithm
outtpl := match(iEqns,inVariables,mark,rowmark,kvars,intpl)
local
BackendDAE.AdjacencyMatrixElementEnhanced row,iRow;
Integer size,iSize;
BackendDAE.Equation eqn;
list<BackendDAE.Equation> rest;
list<Integer> lst,inLstAllBranch;
case ({},_,_,_,_,_) then intpl;
case (eqn::rest,_,_,_,_,_)
equation
(inLstAllBranch,iRow,iSize) = intpl;
(row,size) = adjacencyRowEnhanced(inVariables, eqn, mark, rowmark, kvars, trytosolve);
lst = List.map(row,Util.tuple21);
lst = List.intersectionOnTrue(lst, inLstAllBranch,intEq);
then adjacencyRowEnhancedEqnLstIfBranches(rest, inVariables, mark, rowmark, kvars, trytosolve, (lst, listAppend(row,iRow), size + iSize));
end match;
protected
BackendDAE.AdjacencyMatrixElementEnhanced row, iRow;
list<Integer> lst, inLstAllBranch;
Integer size, iSize;
algorithm
(inLstAllBranch,iRow,iSize) := intpl;
for eqn in iEqns loop
(row,size) := adjacencyRowEnhanced(inVariables, eqn, mark, rowmark, kvars, trytosolve);
lst := List.map(row,Util.tuple21);
inLstAllBranch := List.intersectionOnTrue(lst, inLstAllBranch,intEq);
iSize := iSize + size;
iRow := listAppend(row,iRow);
end for;
outtpl := (inLstAllBranch,iRow,iSize);
end adjacencyRowEnhancedEqnLstIfBranches;

protected function adjacencyRowEnhancedEqnLst
Expand All @@ -4199,27 +4198,18 @@ protected function adjacencyRowEnhancedEqnLst
input Integer mark;
input array<Integer> rowmark;
input BackendDAE.Variables kvars;
input BackendDAE.AdjacencyMatrixElementEnhanced iRow;
input Integer iSize;
input Boolean trytosolve;
output BackendDAE.AdjacencyMatrixElementEnhanced outRow;
output Integer oSize;
output BackendDAE.AdjacencyMatrixElementEnhanced outRow = {};
output Integer oSize = 0;
protected
BackendDAE.AdjacencyMatrixElementEnhanced row;
Integer size;
algorithm
(outRow,oSize) := match(iEqns,inVariables,mark,rowmark,kvars,iRow,iSize)
local
BackendDAE.AdjacencyMatrixElementEnhanced row;
Integer size;
BackendDAE.Equation eqn;
list<BackendDAE.Equation> rest;
case ({},_,_,_,_,_,_) then (iRow,iSize);
case (eqn::rest,_,_,_,_,_,_)
equation
(row,size) = adjacencyRowEnhanced(inVariables,eqn,mark,rowmark,kvars,trytosolve);
row = listAppend(row,iRow);
(row,size) = adjacencyRowEnhancedEqnLst(rest,inVariables,mark,rowmark,kvars,row,size + iSize, trytosolve);
then
(row,size);
end match;
for eqn in iEqns loop
(row,size) := adjacencyRowEnhanced(inVariables,eqn,mark,rowmark,kvars,trytosolve);
outRow := listAppend(row,outRow);
oSize := oSize + size;
end for;
end adjacencyRowEnhancedEqnLst;

protected function adjacencyRowAlgorithmOutputs
Expand Down Expand Up @@ -7690,7 +7680,7 @@ algorithm
if listEmpty(outSysts) then
outSysts := {BackendDAEUtil.createEqSystem(BackendVariable.emptyVars(), BackendEquation.emptyEqns())};
else
outSysts := listReverseInPlace(outSysts);
outSysts := Dangerous.listReverseInPlace(outSysts);
end if;

outShared.removedEqs := BackendEquation.addEquations(reqns, outShared.removedEqs);
Expand Down
44 changes: 20 additions & 24 deletions Compiler/BackEnd/OnRelaxation.mo
Expand Up @@ -60,6 +60,7 @@ protected import Matching;
protected import Sorting;
protected import SymbolicJacobian;
protected import Util;
protected import MetaModelica.Dangerous;


/*
Expand Down Expand Up @@ -1385,31 +1386,26 @@ protected function getOrphansIncidenceMatrix
input list<Integer> orphans;
input array<Integer> invmap;
input array<list<Integer>> vorphansarray;
input list<list<Integer>> m;
input array<list<Integer>> mT;
input Boolean addself;
output array<list<Integer>> outM;
output array<list<Integer>> outMT;
algorithm
(outM, outMT) := match(orphans, invmap, vorphansarray, m, mT, addself)
local
Integer o, i;
list<Integer> rest, lst;
array<list<Integer>> am, amT;
case ({}, _, _, _, _, _)
then
(listArray(listReverse(m)), mT);
case (o::rest, _, _, _, _, _)
equation
// print("getOrphansIncidenceMatrix for " + intString(o) + "\n");
lst = List.map1r(vorphansarray[o], arrayGet, invmap);
i = invmap[o];
lst = List.consOnTrue(addself, i, lst);
amT = List.fold1(lst, Array.consToElement, i, mT);
(am, amT) = getOrphansIncidenceMatrix(rest, invmap, vorphansarray, lst::m, amT, addself);
then
(am, amT);
end match;
output array<list<Integer>> outMT = mT;
protected
list<list<Integer>> m = {};
list<Integer> lst;
Integer i;
array<list<Integer>> am, amT;
algorithm
for o in orphans loop
lst := List.map1r(vorphansarray[o], arrayGet, invmap);
i := invmap[o];
lst := List.consOnTrue(addself, i, lst);
outMT := List.fold1(lst, Array.consToElement, i, outMT);
m := lst::m;
end for;
m := Dangerous.listReverseInPlace(m);
outM := listArray(m);
outMT := mT;
end getOrphansIncidenceMatrix;

protected function getOrder
Expand Down Expand Up @@ -1456,7 +1452,7 @@ algorithm
// print("invmap\n");
// BackendDump.dumpMatching(invmap);
range := List.intRange(size);
(m, mt) := getOrphansIncidenceMatrix(vorphans, invmap, vorphansarray, {}, arrayCreate(size, {}), true);
(m, mt) := getOrphansIncidenceMatrix(vorphans, invmap, vorphansarray, arrayCreate(size, {}), true);
// BackendDump.dumpIncidenceMatrix(m);
// BackendDump.dumpIncidenceMatrixT(mt);
ass := listArray(range);
Expand All @@ -1466,7 +1462,7 @@ algorithm
// print("order: " + stringDelimitList(List.map(order, intString), ", ") + "\n");
// print("Links\n");
// BackendDump.dumpComponentsOLD(linkslst);
(m, mt) := getOrphansIncidenceMatrix(vorphans, invmap, vorphansarray, {}, arrayCreate(size, {}), false);
(m, mt) := getOrphansIncidenceMatrix(vorphans, invmap, vorphansarray, arrayCreate(size, {}), false);
// BackendDump.dumpIncidenceMatrix(m);
// BackendDump.dumpIncidenceMatrixT(mt);
reduceOrphancMatrix(listReverse(comps), m);
Expand Down
17 changes: 7 additions & 10 deletions Compiler/FrontEnd/InstUtil.mo
Expand Up @@ -1678,9 +1678,9 @@ algorithm
(_, se) = Absyn.getExpsFromArrayDimOpt(ado);
(l3, l4) = getExpsFromMod(m);
l1 = listAppend(se, listAppend(l1, l3));
l2 = listAppend(l2, l4);
l4 = listAppend(l2, l4);
then
(l1, l2);
(l1, l4);

// redeclare long class extends class, investigate cc and mods
case (SCode.REDECL(element = SCode.CLASS(prefixes = SCode.PREFIXES(replaceablePrefix = rp),
Expand All @@ -1689,9 +1689,9 @@ algorithm
(l1, l2) = getExpsFromConstrainClass(rp);
(l3, l4) = getExpsFromMod(m);
l1 = listAppend(l1, l3);
l2 = listAppend(l2, l4);
l4 = listAppend(l2, l4);
then
(l1, l2);
(l1, l4);

// redeclare long class, investigate cc
case SCode.REDECL(element = SCode.CLASS(prefixes = SCode.PREFIXES(replaceablePrefix = rp)))
Expand All @@ -1708,9 +1708,9 @@ algorithm
(_, se) = Absyn.getExpsFromArrayDim(ad);
(l3, l4) = getExpsFromMod(m);
l1 = listAppend(se, listAppend(l1, l3));
l2 = listAppend(l2, l4);
l4 = listAppend(l2, l4);
then
(l1, l2);
(l1, l4);

end match;
end getExpsFromMod;
Expand Down Expand Up @@ -6330,12 +6330,9 @@ algorithm
// sort the inners to put Modelica types first!
(innerModelicaServices, innerModelica, innerOthers) = splitInners(innerElts, {}, {}, {});

sorted = listAppend(innerModelicaServices, innerModelica);
sorted = listAppend(sorted, innerOthers);
// put the inner elements first
sorted = listAppend(sorted, innerouterElts);
// put the innerouter elements second
sorted = listAppend(sorted, otherElts);
sorted = listAppend(innerModelicaServices, listAppend(innerModelica, listAppend(innerOthers, listAppend(innerouterElts, otherElts))));
then
sorted;
end matchcontinue;
Expand Down

0 comments on commit 80b17a1

Please sign in to comment.