Skip to content

Commit

Permalink
avoid intermediate lists
Browse files Browse the repository at this point in the history
instead use:
List.fold(List.map()) -> List.applyAndFold()
List.map(List.map()) -> List.mapMap()
listReverse(List.mapFlat()) -> List.mapFlatReverse()
  • Loading branch information
hkiel authored and OpenModelica-Hudson committed Apr 5, 2016
1 parent 6a1e39b commit 9027e88
Show file tree
Hide file tree
Showing 25 changed files with 80 additions and 76 deletions.
6 changes: 3 additions & 3 deletions Compiler/BackEnd/BackendDAETransform.mo
Expand Up @@ -253,7 +253,7 @@ algorithm
var_lst = List.map(var_varindx_lst, Util.tuple21);
crlst = List.map(var_lst,BackendVariable.varCref);
// its only an array equation if all the solved variables belong to an array. Otherwise we have to handle it as a non-linear system
b1 = List.fold(List.map(crlst,ComponentReference.isArrayElement),boolAnd,true);
b1 = List.applyAndFold(crlst,boolAnd,ComponentReference.isArrayElement,true);
if not b1 then
expLst = List.map(crlst, Expression.crefExp);
true = List.exist1(inEqnLst,crefsAreArray,expLst);
Expand Down Expand Up @@ -380,7 +380,7 @@ algorithm
if info < 0 then
// info < 0: if INFO = -i, the i-th argument had an illegal value
// this case should never happen
varnames := stringDelimitList(List.map(List.map(inVars, BackendVariable.varCref), ComponentReference.printComponentRefStr), " ;\n ");
varnames := stringDelimitList(List.mapMap(inVars, BackendVariable.varCref, ComponentReference.printComponentRefStr), " ;\n ");
eqns := BackendEquation.listEquation(inEqns);
vars := BackendVariable.listVar1(inVars);
funcs := BackendDAEUtil.getFunctions(inShared);
Expand All @@ -398,7 +398,7 @@ algorithm
// singular, so the solution could not be computed.
varname := ComponentReference.printComponentRefStr(BackendVariable.varCref(listGet(inVars, info)));
infoStr := intString(info);
varnames := stringDelimitList(List.map(List.map(inVars, BackendVariable.varCref), ComponentReference.printComponentRefStr), " ;\n ");
varnames := stringDelimitList(List.mapMap(inVars, BackendVariable.varCref, ComponentReference.printComponentRefStr), " ;\n ");
eqns := BackendEquation.listEquation(inEqns);
vars := BackendVariable.listVar1(inVars);
funcs := BackendDAEUtil.getFunctions(inShared);
Expand Down
4 changes: 2 additions & 2 deletions Compiler/BackEnd/BackendDump.mo
Expand Up @@ -3712,8 +3712,8 @@ protected
list<tuple<Boolean,String>> varAtts,eqAtts;
algorithm
BackendDAE.DAE(eqs=eqSysts, shared=shared) := dae;
eqLst := List.flatten(List.map(List.map(eqSysts,BackendEquation.getEqnsFromEqSystem),BackendEquation.equationList));
varLst := List.flatten(List.map(List.map(eqSysts,BackendVariable.daeVars),BackendVariable.varList));
eqLst := List.flatten(List.mapMap(eqSysts,BackendEquation.getEqnsFromEqSystem,BackendEquation.equationList));
varLst := List.flatten(List.mapMap(eqSysts,BackendVariable.daeVars,BackendVariable.varList));
vars := BackendVariable.listVar1(varLst);
eqs := BackendEquation.listEquation(eqLst);
// build the incidence matrix for the whole System
Expand Down
2 changes: 1 addition & 1 deletion Compiler/BackEnd/Differentiate.mo
Expand Up @@ -2305,7 +2305,7 @@ algorithm
print("### Detailed arguments list: \n");
print(stringDelimitList(List.map(expl, ExpressionDump.printExpStr), ", ") + "\n");
print("### and argument types: \n");
print(stringDelimitList(List.map(List.map(expl, Expression.typeof), Types.printTypeStr), " | ") + "\n");
print(stringDelimitList(List.mapMap(expl, Expression.typeof, Types.printTypeStr), " | ") + "\n");
print("### and output type: \n" + Types.printTypeStr(dtp) + "\n");
end if;

Expand Down
2 changes: 1 addition & 1 deletion Compiler/BackEnd/DumpGraphML.mo
Expand Up @@ -527,7 +527,7 @@ algorithm
(_,vlst) = BackendDAETransform.getEquationAndSolvedVarIndxes(comp);
varcomp1 = List.fold1r(vlst,arrayUpdate,iN,varcomp);
varlst = List.map1r(vlst,BackendVariable.getVarAt,vars);
text = intString(iN) + ":" + stringDelimitList(List.map(List.map(varlst,BackendVariable.varCref),ComponentReference.printComponentRefStr),"\n");
text = intString(iN) + ":" + stringDelimitList(List.mapMap(varlst,BackendVariable.varCref,ComponentReference.printComponentRefStr),"\n");
label = GraphML.NODELABEL_INTERNAL(text,NONE(),GraphML.FONTPLAIN());
(graphInfo,_) = GraphML.addNode("n" + intString(iN),GraphML.COLOR_GREEN,{label},GraphML.RECTANGLE(),NONE(),{},graph,graphInfo);
then
Expand Down
7 changes: 3 additions & 4 deletions Compiler/BackEnd/EvaluateFunctions.mo
Expand Up @@ -1298,12 +1298,11 @@ algorithm
eqsOut := generateConstEqs(lhsExps2,constComplExps,eqsOut);

// build the partial function algorithm, replace the qualified crefs
stmts1 := List.mapFlat(funcAlgs, DAEUtil.getStatement);
//stmts1 := listReverse(stmts1);
stmts1 := List.mapFlatReverse(funcAlgs, DAEUtil.getStatement);
//stmts1 := List.filterOnTrue(stmts1,statementRHSIsNotConst);
// remove the constant values
//stmts1 := traverseStmtsAndUpdate(stmts1,stmtCanBeRemoved,replIn,{});
stmts1 := listReverse(stmts1);
// stmts1 := listReverse(stmts1);

// build new crefs for the scalars
(stmts1,_) := DAEUtil.traverseDAEEquationsStmts(stmts1,Expression.traverseSubexpressionsHelper,(makeIdentCref,varScalarCrefs));
Expand Down Expand Up @@ -2046,7 +2045,7 @@ algorithm
lhsExps := List.fold1(stmts,getStatementLHSScalar,funcTreeIn,{});
lhsExps := List.unique(lhsExps);
outputs := List.map(lhsExps,Expression.expCref);
hasNoRepl := List.fold(List.map1(outputs,BackendVarTransform.hasNoReplacementCrefFirst,repl),boolAnd,true);
hasNoRepl := List.applyAndFold1(outputs,boolAnd,BackendVarTransform.hasNoReplacementCrefFirst,repl,true);
if hasNoRepl then
if Flags.isSet(Flags.EVAL_FUNC_DUMP) then print("For-loop evaluation is skipped, since the first loop evaluated nothing.\n"); end if;
fail();
Expand Down
2 changes: 1 addition & 1 deletion Compiler/BackEnd/RemoveSimpleEquations.mo
Expand Up @@ -3711,7 +3711,7 @@ algorithm
repl = getAliasReplacements(iStatesetrepl, aliasVars);
// do not replace the set variables
hs = HashSet.emptyHashSet();
hs = List.fold(List.map(statescandidates, BackendVariable.varCref), BaseHashSet.add, hs);
hs = List.applyAndFold(statescandidates, BaseHashSet.add, BackendVariable.varCref, hs);
ovars = replaceOtherStateSetVars(ovars, vars, aliasVars, hs, {});
(eqns, b) = BackendVarTransform.replaceEquations(eqns, repl, SOME(BackendVarTransform.skipPreChangeEdgeOperator));
(oeqns, b1) = BackendVarTransform.replaceEquations(oeqns, repl, SOME(BackendVarTransform.skipPreChangeEdgeOperator));
Expand Down
4 changes: 2 additions & 2 deletions Compiler/BackEnd/ResolveLoops.mo
Expand Up @@ -1913,11 +1913,11 @@ algorithm
b1 := intEq(listLength(row),2); // only two variables
eqLst := List.mapList((List.map1(vars,Array.getIndexFirst,meT)),Util.tuple31);
numEqs := List.map(eqLst,listLength);
b3 := List.fold(List.map1(numEqs,intEq,2),boolOr,false); // at least one adjacent variable hast only 2 adj equations
b3 := List.applyAndFold1(numEqs,boolOr,intEq,2,false); // at least one adjacent variable hast only 2 adj equations
eqs := List.flatten(eqLst);
b2 := intEq(listLength(eqs),listLength(List.unique(eqs))+2);
b1 := b1 and b2 and b3;
chooseThis := b1 and List.fold(List.map(row,isSolvable),boolAnd,true);
chooseThis := b1 and List.applyAndFold(row,boolAnd,isSolvable,true);
end chooseEquation;

protected function getDoublicates
Expand Down
4 changes: 2 additions & 2 deletions Compiler/BackEnd/StateMachineFeatures.mo
Expand Up @@ -501,8 +501,8 @@ algorithm
for i in 1:n loop
cref := arrayGet(states, i);
MODE(name, isInitial, edges, eqs, outgoing, outShared, outLocal, crefPrevious) := BaseHashTable.get(cref, modes);
crefLocalsSet := List.fold(List.map(outLocal, BackendVariable.varCref), BaseHashSet.add, HashSet.emptyHashSet());
crefSharedSet := List.fold(List.map(outShared, BackendVariable.varCref), BaseHashSet.add, HashSet.emptyHashSet());
crefLocalsSet := List.applyAndFold(outLocal, BaseHashSet.add, BackendVariable.varCref, HashSet.emptyHashSet());
crefSharedSet := List.applyAndFold(outShared, BaseHashSet.add, BackendVariable.varCref, HashSet.emptyHashSet());
// collect all inner outer outputs together with the index of the state they occur in
outStateInnerOuters := listAppend(outStateInnerOuters, List.map(List.filterOnTrue(outLocal,filterInnerOuters), function Util.makeTuple(inValue1=i)));

Expand Down
3 changes: 1 addition & 2 deletions Compiler/BackEnd/Tearing.mo
Expand Up @@ -1249,7 +1249,7 @@ algorithm
eqnsize = listLength(mapEqnIncRow[cnonscalar]);
if Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
print("Eqn Size: " + intString(eqnsize) + "\n");
// fcall(Flags.TEARING_DUMPVERBOSE, print,"Rows(not assigned variables in eqn " + intString(c) + ":\n" + stringDelimitList(List.map(List.map(rows,Util.tuple21),intString),", ") + "\n");
// fcall(Flags.TEARING_DUMPVERBOSE, print,"Rows(not assigned variables in eqn " + intString(c) + ":\n" + stringDelimitList(List.mapMap(rows,Util.tuple21,intString),", ") + "\n");
print("Rows (not assigned variables in eqn " + intString(c) + "):\n");
BackendDump.dumpAdjacencyRowEnhanced(rows);
print("\n");
Expand Down Expand Up @@ -4421,4 +4421,3 @@ end dumpMatchingList;

annotation(__OpenModelica_Interface="backend");
end Tearing;

2 changes: 1 addition & 1 deletion Compiler/BackEnd/VisualXML.mo
Expand Up @@ -103,7 +103,7 @@ algorithm
//get all variables that contain visualization vars
knownVarLst := BackendVariable.varList(knownVars);
aliasVarLst := BackendVariable.varList(aliasVars);
allVarLst := List.flatten(List.map(List.map(eqs, BackendVariable.daeVars),BackendVariable.varList));
allVarLst := List.flatten(List.mapMap(eqs, BackendVariable.daeVars,BackendVariable.varList));

//collect all visualization objects
(knownVarLst,allVisuals) := List.fold(knownVarLst,isVisualizationVar,({},{}));
Expand Down
4 changes: 2 additions & 2 deletions Compiler/FFrontEnd/FLookup.mo
Expand Up @@ -349,8 +349,8 @@ algorithm
equation
refs = FNode.extendsRefs(inRef);
false = listEmpty(refs);
refs = List.map(List.map(refs, FNode.fromRef), FNode.target);
// print("Searching for: " + inName + " in extends targets:\n\t" + stringDelimitList(List.map(List.map(refs, FNode.fromRef), FNode.toPathStr), "\n\t") + "\n");
refs = List.mapMap(refs, FNode.fromRef, FNode.target);
// print("Searching for: " + inName + " in extends targets:\n\t" + stringDelimitList(List.mapMap(refs, FNode.fromRef, FNode.toPathStr), "\n\t") + "\n");
(g, r) = search(g, refs, inName, ignoreParentsAndImports, inMsg);
then
(g, r);
Expand Down
4 changes: 2 additions & 2 deletions Compiler/FFrontEnd/FNode.mo
Expand Up @@ -1025,9 +1025,9 @@ algorithm
case (_, _)
equation
s = originalScope(toRef(inNode));
b1 = List.fold(List.map(s, inFunctionRefIs), boolOr, false);
b1 = List.applyAndFold(s, boolOr, inFunctionRefIs, false);
s = contextualScope(toRef(inNode));
b2 = List.fold(List.map(s, inFunctionRefIs), boolOr, false);
b2 = List.applyAndFold(s, boolOr, inFunctionRefIs, false);
b = boolOr(b1, b2);
then
b;
Expand Down
4 changes: 2 additions & 2 deletions Compiler/FrontEnd/Algorithm.mo
Expand Up @@ -136,8 +136,8 @@ public function makeTupleAssignmentNoTypeCheck
protected
Boolean b1,b2;
algorithm
b1 := List.fold(List.map(lhs, Expression.isWild), boolAnd, true);
b2 := List.fold(List.map(List.restOrEmpty(lhs), Expression.isWild), boolAnd, true);
b1 := List.applyAndFold(lhs, boolAnd, Expression.isWild, true);
b2 := List.applyAndFold(List.restOrEmpty(lhs), boolAnd, Expression.isWild, true);
outStatement := makeTupleAssignmentNoTypeCheck2(b1,b2,ty,lhs,rhs,source);
end makeTupleAssignmentNoTypeCheck;

Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/CheckModel.mo
Expand Up @@ -567,7 +567,7 @@ protected function countSimpleEqnSize
input HashSet.HashSet ihs;
output Integer osimpleEqnSize;
algorithm
osimpleEqnSize := List.fold(List.map1(inEqns, countSimpleEqnSizeWork, ihs), intAdd, 0);
osimpleEqnSize := List.applyAndFold1(inEqns, intAdd, countSimpleEqnSizeWork, ihs, 0);
end countSimpleEqnSize;

protected function countSimpleEqnSizeWork
Expand Down
5 changes: 2 additions & 3 deletions Compiler/FrontEnd/ComponentReference.mo
Expand Up @@ -3576,10 +3576,9 @@ protected
String s1,s2;
algorithm
s1 := stringDelimitList(toStringList(cref), "_P");
s2 := stringDelimitList(List.map(List.map(crefSubs(cref),Expression.getSubscriptExp),ExpressionDump.printExpStr),",");
s2 := stringDelimitList(List.mapMap(crefSubs(cref),Expression.getSubscriptExp,ExpressionDump.printExpStr),",");
s := s1+"["+s2+"]";
end crefAppendedSubs;
end crefAppendedSubs;

annotation(__OpenModelica_Interface="frontend");
end ComponentReference;

2 changes: 1 addition & 1 deletion Compiler/FrontEnd/DAEUtil.mo
Expand Up @@ -6628,7 +6628,7 @@ algorithm
list<String> names;
case(DAE.FUNCARG(ty = DAE.T_ARRAY(dims=arrayDims)))
equation
then List.fold(List.map(arrayDims, Expression.dimensionSize),intAdd,0);
then List.applyAndFold(arrayDims, intAdd, Expression.dimensionSize,0);
case(DAE.FUNCARG(ty = DAE.T_ENUMERATION(names=names)))
equation
then listLength(names);
Expand Down
26 changes: 13 additions & 13 deletions Compiler/FrontEnd/Expression.mo
Expand Up @@ -2141,7 +2141,7 @@ algorithm
// count the variables in record
case DAE.T_COMPLEX(varLst = varLst)
equation
lstInt = List.map(List.map(varLst, varType), sizeOf);
lstInt = List.mapMap(varLst, varType, sizeOf);
nr = List.reduce(lstInt, intAdd);
then
nr;
Expand Down Expand Up @@ -6362,7 +6362,7 @@ public function anyExpHasCrefName "Returns a true if any exp contains a cref tha
input String name;
output Boolean hasCref;
algorithm
hasCref := List.fold(List.map1(inExps, expHasCrefName, name), boolOr, false);
hasCref := List.applyAndFold1(inExps, boolOr, expHasCrefName, name, false);
end anyExpHasCrefName;

public function traversingexpHasName "Returns a true if the exp contains a cref that starts with the given name"
Expand Down Expand Up @@ -10496,30 +10496,30 @@ algorithm
then c1+intMax(c2,c3);
case DAE.CALL(path=Absyn.IDENT(name),expLst=exps,attr=DAE.CALL_ATTR(ty=tp,builtin=true))
equation
c1 = List.fold(List.map(exps,complexity),intAdd,0);
c1 = List.applyAndFold(exps,intAdd,complexity,0);
c2 = complexityBuiltin(name,tp);
/* TODO: Cost is based on type and size of inputs. Maybe even name for builtins :) */
then c1+c2;
case DAE.CALL(expLst=exps)
equation
c1 = List.fold(List.map(exps,complexity),intAdd,0);
c1 = List.applyAndFold(exps,intAdd,complexity,0);
c2 = listLength(exps);
/* TODO: Cost is based on type and size of inputs. Maybe even name for builtins :) */
then c1+c2+25;
case DAE.RECORD(exps=exps)
equation
c1 = List.fold(List.map(exps,complexity),intAdd,1);
c1 = List.applyAndFold(exps,intAdd,complexity,1);
then c1;
case DAE.PARTEVALFUNCTION()
then complexityVeryBig; /* This should not be here anyway :) */
case DAE.ARRAY(array=exps,ty=tp)
equation
c1 = List.fold(List.map(exps,complexity),intAdd,if isArrayType(tp) then 0 else complexityAlloc);
c1 = List.applyAndFold(exps,intAdd,complexity,if isArrayType(tp) then 0 else complexityAlloc);
c2 = listLength(exps);
then c1+c2;
case DAE.MATRIX(matrix=matrix as (exps::_))
equation
c1 = List.fold(List.map(List.flatten(matrix),complexity),intAdd,complexityAlloc);
c1 = List.applyAndFold(List.flatten(matrix),intAdd,complexity,complexityAlloc);
c2 = listLength(exps)*listLength(matrix);
then c1 + c2;
case DAE.RANGE(start=e1,stop=e2,step=NONE())
Expand All @@ -10528,13 +10528,13 @@ algorithm
then complexityDimLarge+complexity(e1)+complexity(e2)+complexity(e3); /* TODO: Check type maybe? */
case DAE.TUPLE(PR=exps)
equation
c1 = List.fold(List.map(exps,complexity),intAdd,complexityAlloc);
c1 = List.applyAndFold(exps,intAdd,complexity,complexityAlloc);
c2 = listLength(exps);
then c1+c2;
case DAE.CAST(exp=e,ty=tp) then tpComplexity(tp)+complexity(e);
case DAE.ASUB(exp=e,sub=exps)
equation
c1 = List.fold(List.map(exps,complexity),intAdd,complexityAlloc);
c1 = List.applyAndFold(exps,intAdd,complexity,complexityAlloc);
c2 = listLength(exps);
c3 = complexity(e);
then c1+c2+c3;
Expand All @@ -10546,21 +10546,21 @@ algorithm
case DAE.REDUCTION() then complexityVeryBig; /* TODO: We need a real traversal... */
case DAE.LIST(valList=exps)
equation
c1 = List.fold(List.map(exps,complexity),intAdd,complexityAlloc);
c1 = List.applyAndFold(exps,intAdd,complexity,complexityAlloc);
c2 = listLength(exps);
then c1+c2+complexityAlloc;
case DAE.CONS(car=e1,cdr=e2)
then complexityAlloc+complexity(e1)+complexity(e2);
case DAE.META_TUPLE(listExp=exps)
equation
c1 = List.fold(List.map(exps,complexity),intAdd,complexityAlloc);
c1 = List.applyAndFold(exps,intAdd,complexity,complexityAlloc);
c2 = listLength(exps);
then complexityAlloc+c1+c2;
case DAE.META_OPTION(exp=NONE()) then 0;
case DAE.META_OPTION(exp=SOME(e)) then complexity(e)+complexityAlloc;
case DAE.METARECORDCALL(args=exps)
equation
c1 = List.fold(List.map(exps,complexity),intAdd,complexityAlloc);
c1 = List.applyAndFold(exps,intAdd,complexity,complexityAlloc);
c2 = listLength(exps);
then c1+c2+complexityAlloc;
case DAE.MATCHEXPRESSION() then complexityVeryBig;
Expand Down Expand Up @@ -10596,7 +10596,7 @@ algorithm
list<DAE.Dimension> dims;
case DAE.T_ARRAY(dims=dims)
equation
i = List.fold(List.map(dims,dimComplexity),intMul,1);
i = List.applyAndFold(dims,intMul,dimComplexity,1);
then i;
else 0;
end match;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/NFMod.mo
Expand Up @@ -1075,7 +1075,7 @@ algorithm
case (SCode.NAMEMOD(mod = SCode.MOD(binding = SOME((e, _))))::rest, _)
equation
cl = Absyn.getCrefFromExp(e,true,true);
true = List.fold(List.map1(cl, Absyn.crefFirstEqual, id), boolOr, false);
true = List.applyAndFold1(cl, boolOr, Absyn.crefFirstEqual, id, false);
then
rest;

Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/NFSCodeMod.mo
Expand Up @@ -893,7 +893,7 @@ algorithm
case (SCode.NAMEMOD(mod = SCode.MOD(binding = SOME(e)))::rest, _)
equation
cl = Absyn.getCrefFromExp(e,true,true);
true = List.fold(List.map1(cl, Absyn.crefFirstEqual, id), boolOr, false);
true = List.applyAndFold1(cl, boolOr, Absyn.crefFirstEqual, id, false);
then
rest;

Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/Patternm.mo
Expand Up @@ -2521,7 +2521,7 @@ algorithm
ld2 = SCodeUtil.translateEitemlist(ld, SCode.PROTECTED());

// Filter out the components (just to be sure)
true = List.fold(List.map1(ld2, SCode.isComponentWithDirection, Absyn.BIDIR()), boolAnd, true);
true = List.applyAndFold1(ld2, boolAnd, SCode.isComponentWithDirection, Absyn.BIDIR(), true);
((cache,b)) = List.fold1(ld2, checkLocalShadowing, env, (cache,false));
ld2 = if b then {} else ld2;

Expand Down

0 comments on commit 9027e88

Please sign in to comment.