Skip to content

Commit

Permalink
Some List changes:
Browse files Browse the repository at this point in the history
- List.find was replaced with the more efficient List.mapFirst.
- List.selectFirst* was replaced by List.find*, to make List
  a bit less surprising.
  • Loading branch information
perost committed Aug 25, 2015
1 parent 540a464 commit 1981ce9
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 66 deletions.
4 changes: 2 additions & 2 deletions Compiler/BackEnd/StateMachineFeatures.mo
Expand Up @@ -352,7 +352,7 @@ algorithm
bindingKind := BackendDAE.EQUATION_ATTRIBUTES(false, BackendDAE.BINDING_EQUATION(), BackendDAE.NO_LOOP());

R(initialState, refining) := comp;
flatA := List.selectFirst1(flatAs, findInitialState, initialState);
flatA := List.find1(flatAs, findInitialState, initialState);
FLAT_AUTOMATON(initialState, states, sms) := flatA;
preRef := ComponentReference.crefPrefixString(SMS_PRE, initialState);
nStates := arrayLength(states);
Expand Down Expand Up @@ -522,7 +522,7 @@ algorithm
outLocalEqns := BackendDAE.EQUATION(exp, rhs, source, attr) :: outLocalEqns;

// Find variable corresponding to componentRef
var := List.selectFirst1(outLocal,cmpVarCref,componentRef);
var := List.find1(outLocal,cmpVarCref,componentRef);

// If lhs "x" is a state variable, i.e., "x" appears somewhere in the global equation system as "previous(x)",
// than substitute all "previous(x)" by a fresh variable "x_previous" which is defined by an equation "x_previous = if resetOfState than initialValueOfX else previous(x)"
Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/Absyn.mo
Expand Up @@ -5401,7 +5401,7 @@ protected
list<ClassPart> class_parts;
algorithm
CLASS(body = PARTS(classParts = class_parts)) := inCls;
outExternal := List.selectFirst(class_parts, isExternalPart);
outExternal := List.find(class_parts, isExternalPart);
end getExternalDecl;

protected function isExternalPart
Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/ExpressionSimplify.mo
Expand Up @@ -4408,7 +4408,7 @@ algorithm
* Exponentation is very expensive compared to the inner expressions.
*/
((exp_lst as (_ :: _ :: _ :: _))) = Expression.factors(e1);
_ = List.selectFirst(exp_lst,Expression.isConstValue);
_ = List.find(exp_lst,Expression.isConstValue);
exp_lst_1 = simplifyBinaryDistributePow(exp_lst, e2);
then Expression.makeProductLst(exp_lst_1);
// (e1^e2)^e3 => e1^(e2*e3)
Expand Down
6 changes: 3 additions & 3 deletions Compiler/FrontEnd/InstUtil.mo
Expand Up @@ -214,7 +214,7 @@ algorithm
//print(" Ceval res: ("+stringDelimitList(List.map(valList,ValuesUtil.printValStr),",")+")\n");

blist = List.map(valList,ValuesUtil.valueBool);
selectedBranch = List.selectFirstBoolList(blist, tbs, fb);
selectedBranch = List.findBoolList(blist, tbs, fb);
selectedBranch = makeDAEElementInitial(selectedBranch);
then listAppend(selectedBranch,acc);
else elem::acc;
Expand Down Expand Up @@ -2014,14 +2014,14 @@ algorithm
case (exp as Absyn.CREF(componentRef = cref), (all_el, stack, accum_el, b))
equation
id = Absyn.crefFirstIdent(cref);
e = List.selectFirst1(all_el, isElementNamed, id);
e = List.find1(all_el, isElementNamed, id);
then
(exp, (all_el, stack, e :: accum_el, b));

case (exp as Absyn.CALL(function_ = cref), (all_el, stack, accum_el, b))
equation
id = Absyn.crefFirstIdent(cref);
e = List.selectFirst1(all_el, isElementNamed, id);
e = List.find1(all_el, isElementNamed, id);
then
(exp, (all_el, stack, e :: accum_el, b));

Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/Mod.mo
Expand Up @@ -2915,7 +2915,7 @@ algorithm

case DAE.MOD(subModLst = submods)
equation
_ = List.selectFirst(submods, isUntypedSubMod);
_ = List.find(submods, isUntypedSubMod);
then
true;

Expand Down
4 changes: 2 additions & 2 deletions Compiler/FrontEnd/NFInstUtil.mo
Expand Up @@ -1653,7 +1653,7 @@ public import SCode;
// case NFInstTypes.SINGLE_CONDITION(condition = cond) then cond;
// case NFInstTypes.ARRAY_CONDITION(conditions = condl)
// equation
// _ = List.selectFirst(condl, conditionFalse);
// _ = List.find(condl, conditionFalse);
// then
// false;
//
Expand All @@ -1673,7 +1673,7 @@ public import SCode;
// case NFInstTypes.SINGLE_CONDITION(condition = cond) then not cond;
// case NFInstTypes.ARRAY_CONDITION(conditions = condl)
// equation
// _ = List.selectFirst(condl, conditionTrue);
// _ = List.find(condl, conditionTrue);
// then
// false;
//
Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/NFSCodeFlatten.mo
Expand Up @@ -75,7 +75,7 @@ protected
String name;
algorithm
prog := listReverse(inProgram);
SCode.CLASS(name = name) := List.selectFirst(prog, isClass);
SCode.CLASS(name = name) := List.find(prog, isClass);
outClassName := Absyn.IDENT(name);
end getLastClassNameInProgram;

Expand Down
4 changes: 2 additions & 2 deletions Compiler/FrontEnd/SCode.mo
Expand Up @@ -3798,7 +3798,7 @@ protected
list<SubMod> submods;
algorithm
ANNOTATION(modification = MOD(subModLst = submods)) := inAnnotation;
NAMEMOD(mod = MOD(info = info, binding = SOME(exp))) := List.selectFirst1(submods, hasNamedAnnotation, inName);
NAMEMOD(mod = MOD(info = info, binding = SOME(exp))) := List.find1(submods, hasNamedAnnotation, inName);
end getNamedAnnotation;

protected function hasNamedAnnotation
Expand Down Expand Up @@ -3937,7 +3937,7 @@ algorithm

case ANNOTATION(MOD(fp, ep, submods, _, info))
equation
inline_mod = List.selectFirst(submods, isInlineTypeSubMod);
inline_mod = List.find(submods, isInlineTypeSubMod);
then
SOME(ANNOTATION(MOD(fp, ep, {inline_mod}, NONE(), info)));

Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/Static.mo
Expand Up @@ -7925,7 +7925,7 @@ algorithm

case DAE.T_METARECORD(source = {fq_path})
algorithm
DAE.TYPES_VAR(name = str) := List.selectFirst(inType.fields, Types.varHasMetaRecordType);
DAE.TYPES_VAR(name = str) := List.find(inType.fields, Types.varHasMetaRecordType);
fn_str := Absyn.pathString(fq_path);
Error.addSourceMessage(Error.METARECORD_CONTAINS_METARECORD_MEMBER,
{fn_str, str}, inInfo);
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Script/Interactive.mo
Expand Up @@ -15278,7 +15278,7 @@ protected
list<Absyn.Class> classes;
algorithm
Absyn.PROGRAM(classes=classes) := inProgram;
cl := List.selectFirst1(classes,getClassInProgramWork,inString);
cl := List.find1(classes,getClassInProgramWork,inString);
end getClassInProgram;

protected function getClassInProgramWork
Expand Down
6 changes: 3 additions & 3 deletions Compiler/SimCode/SimCodeFunctionUtil.mo
Expand Up @@ -535,7 +535,7 @@ protected
algorithm
DAE.T_COMPLEX(complexClassType = ClassInf.RECORD(path)) := ty;
name := Absyn.pathStringUnquoteReplaceDot(path, "_");
decl := List.selectFirst1(allDecls, isRecordDecl, name);
decl := List.find1(allDecls, isRecordDecl, name);
end getRecordDependenciesFromType;

protected function isRecordDecl
Expand Down Expand Up @@ -1042,11 +1042,11 @@ algorithm
list<SimCode.Variable> inVars;
case (_, SimCode.FUNCTION(functionArguments = inVars))
equation
failure(_ = List.selectFirst(inVars, isFunctionPtr));
failure(_ = List.find(inVars, isFunctionPtr));
then ();
case (_, SimCode.EXTERNAL_FUNCTION(inVars = inVars))
equation
failure(_ = List.selectFirst(inVars, isFunctionPtr));
failure(_ = List.find(inVars, isFunctionPtr));
then ();
else
equation
Expand Down
98 changes: 50 additions & 48 deletions Compiler/Util/List.mo
Expand Up @@ -5265,7 +5265,7 @@ public function select1 = filter1OnTrue;
public function select1r = filter1rOnTrue;
public function select2 = filter2OnTrue;

public function selectFirst<T>
public function find<T>
"This function retrieves the first element of a list for which the passed
function evaluates to true."
input list<T> inList;
Expand All @@ -5284,31 +5284,9 @@ algorithm
end if;
end for;
fail();
end selectFirst;

public function selectFirstBoolList<T>
"This function returns the first value in the given list for which the
corresponding element in the boolean list is true."
input list<Boolean> inBooleans;
input list<T> inList;
input T inFalseValue;
output T outElement;
protected
T e;
list<T> rest = inList;
algorithm
for b in inBooleans loop
e :: rest := rest;

if b then
outElement := e;
return;
end if;
end for;
outElement := inFalseValue;
end selectFirstBoolList;
end find;

public function selectFirst1<T, ArgT1>
public function find1<T, ArgT1>
"This function retrieves the first element of a list for which the passed
function evaluates to true."
input list<T> inList;
Expand All @@ -5329,7 +5307,29 @@ algorithm
end if;
end for;
fail();
end selectFirst1;
end find1;

public function findBoolList<T>
"This function returns the first value in the given list for which the
corresponding element in the boolean list is true."
input list<Boolean> inBooleans;
input list<T> inList;
input T inFalseValue;
output T outElement;
protected
T e;
list<T> rest = inList;
algorithm
for b in inBooleans loop
e :: rest := rest;

if b then
outElement := e;
return;
end if;
end for;
outElement := inFalseValue;
end findBoolList;

public function deleteMember<T>
"Takes a list and a value, and deletes the first occurence of the value in the
Expand Down Expand Up @@ -5872,28 +5872,6 @@ algorithm
outList := {a, b};
end first2FromTuple3;

public function find<TI, TO>
"Takes a list of elements and a map function, and returns the first element
that the map function succeeds for."
input list<TI> inList;
input FindFunc inFindFunc;
output TO outElement;

partial function FindFunc
input TI inElement;
output TO outElement;
end FindFunc;
algorithm
for e in inList loop
try
outElement := inFindFunc(e);
return;
else
end try;
end for;
fail();
end find;

public function findMap<T>
"Same as map, but stops when it find a certain element as indicated by the
mapping function. Returns the new list, and whether the element was found or
Expand Down Expand Up @@ -6398,5 +6376,29 @@ algorithm
end for;
end separate1OnTrue;

public function mapFirst<TI, TO>
input list<TI> inList;
input FindMapFunc inFunc;
output TO outElement;

partial function FindMapFunc
input TI inElement;
output TO outElement;
output Boolean outFound;
end FindMapFunc;
protected
Boolean found;
algorithm
for e in inList loop
(outElement, found) := inFunc(e);

if found then
return;
end if;
end for;
fail();
end mapFirst;


annotation(__OpenModelica_Interface="util");
end List;

0 comments on commit 1981ce9

Please sign in to comment.