Skip to content

Commit

Permalink
avoid failure(_=BaseHashTable.get()), use hasKey() instead
Browse files Browse the repository at this point in the history
Belonging to [master]:
  - OpenModelica/OMCompiler#2306
  • Loading branch information
hkiel authored and OpenModelica-Hudson committed Mar 23, 2018
1 parent 43f36d5 commit 13b595d
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 105 deletions.
78 changes: 33 additions & 45 deletions Compiler/BackEnd/BackendVarTransform.mo
Expand Up @@ -279,8 +279,8 @@ algorithm
list<DAE.Ident> iv;
Option<HashTable2.HashTable> derConst;
case ((REPLACEMENTS(hashTable=ht)),src,_) /* source dest */
equation
_ = BaseHashTable.get(src,ht) "if rule a->b exists, fail";
guard
BaseHashTable.hasKey(src,ht) "if rule a->b exists, fail"
then
fail();
case ((REPLACEMENTS(ht,invHt,eht,iv,derConst)),src,dst)
Expand Down Expand Up @@ -371,26 +371,18 @@ protected function addReplacementInv2 "
input DAE.ComponentRef dst;
input DAE.ComponentRef src;
output HashTable3.HashTable outInvHt;
protected
list<DAE.ComponentRef> srcs;
algorithm
outInvHt:=
matchcontinue (invHt,dst,src)
local
HashTable3.HashTable invHt_1;
list<DAE.ComponentRef> srcs;
case (_,_,_)
equation
failure(_ = BaseHashTable.get(dst,invHt)) "No previous elt for dst -> src";
invHt_1 = BaseHashTable.add((dst, {src}),invHt);
then
invHt_1;
case (_,_,_)
equation
srcs = BaseHashTable.get(dst,invHt) "previous elt for dst -> src, append..";
srcs = src::srcs;
invHt_1 = BaseHashTable.add((dst, srcs),invHt);
then
invHt_1;
end matchcontinue;
if BaseHashTable.hasKey(dst,invHt) then
// previous elt for dst -> src, append..
srcs := BaseHashTable.get(dst,invHt);
srcs := src::srcs;
outInvHt := BaseHashTable.add((dst, srcs),invHt);
else
// No previous elt for dst -> src
outInvHt:= BaseHashTable.add((dst, {src}),invHt);
end if;
end addReplacementInv2;

protected function makeTransitive "
Expand Down Expand Up @@ -559,22 +551,22 @@ algorithm
case (_,DAE.CREF_IDENT(ident=ident,identType=ty as DAE.T_ARRAY()),NONE())
equation
precr = ComponentReference.makeCrefIdent(ident,ty,{});
failure(_ = BaseHashTable.get(precr,extendrepl));
false = BaseHashTable.hasKey(precr,extendrepl);
// update Replacements
erepl = BaseHashTable.add((precr, DAE.RCONST(0.0)),extendrepl);
then erepl;
case (_,DAE.CREF_IDENT(ident=ident,identType=ty as DAE.T_ARRAY()),SOME(pcr))
equation
precr = ComponentReference.makeCrefIdent(ident,ty,{});
precr1 = ComponentReference.joinCrefs(pcr,precr);
failure(_ = BaseHashTable.get(precr1,extendrepl));
false = BaseHashTable.hasKey(precr1,extendrepl);
// update Replacements
erepl = BaseHashTable.add((precr1, DAE.RCONST(0.0)),extendrepl);
then erepl;
case (_,DAE.CREF_IDENT(ident=ident,identType=ty as DAE.T_COMPLEX(complexClassType=ClassInf.RECORD(_),varLst=varLst)),NONE())
equation
precr = ComponentReference.makeCrefIdent(ident,ty,{});
failure(_ = BaseHashTable.get(precr,extendrepl));
false = BaseHashTable.hasKey(precr,extendrepl);
// update Replacements
erepl = BaseHashTable.add((precr, DAE.RCONST(0.0)),extendrepl);
// Create a list of crefs from names
Expand All @@ -585,7 +577,7 @@ algorithm
equation
_ = ComponentReference.makeCrefIdent(ident,ty,{});
precr1 = ComponentReference.joinCrefs(pcr,cr);
failure(_ = BaseHashTable.get(precr1,extendrepl));
false = BaseHashTable.hasKey(precr1,extendrepl);
// update Replacements
erepl = BaseHashTable.add((precr1, DAE.RCONST(0.0)),extendrepl);
// Create a list of crefs from names
Expand All @@ -595,15 +587,15 @@ algorithm
case (_,DAE.CREF_IDENT(ident=ident,identType=ty,subscriptLst=_::_),NONE())
equation
precr = ComponentReference.makeCrefIdent(ident,ty,{});
failure(_ = BaseHashTable.get(precr,extendrepl));
false = BaseHashTable.hasKey(precr,extendrepl);
// update Replacements
erepl = BaseHashTable.add((precr, DAE.RCONST(0.0)),extendrepl);
then erepl;
case (_,DAE.CREF_IDENT(ident=ident,identType=ty,subscriptLst=_::_),SOME(pcr))
equation
precr = ComponentReference.makeCrefIdent(ident,ty,{});
precr1 = ComponentReference.joinCrefs(pcr,precr);
failure(_ = BaseHashTable.get(precr1,extendrepl));
false = BaseHashTable.hasKey(precr1,extendrepl);
// update Replacements
erepl = BaseHashTable.add((precr1, DAE.RCONST(0.0)),extendrepl);
then erepl;
Expand All @@ -613,7 +605,7 @@ algorithm
case (_,DAE.CREF_QUAL(ident=ident,identType=ty,subscriptLst=subscriptLst,componentRef=subcr),NONE())
equation
precr = ComponentReference.makeCrefIdent(ident,ty,{});
failure(_ = BaseHashTable.get(precr,extendrepl));
false = BaseHashTable.hasKey(precr,extendrepl);
// update Replacements
erepl = BaseHashTable.add((precr, DAE.RCONST(0.0)),extendrepl);
precrn = ComponentReference.makeCrefIdent(ident,ty,subscriptLst);
Expand All @@ -623,7 +615,7 @@ algorithm
equation
precr = ComponentReference.makeCrefIdent(ident,ty,{});
precr1 = ComponentReference.joinCrefs(pcr,precr);
failure(_ = BaseHashTable.get(precr1,extendrepl));
false = BaseHashTable.hasKey(precr1,extendrepl);
// update Replacements
erepl = BaseHashTable.add((precr1, DAE.RCONST(0.0)),extendrepl);
precrn = ComponentReference.makeCrefIdent(ident,ty,subscriptLst);
Expand Down Expand Up @@ -793,18 +785,17 @@ public function hasReplacement "
output Boolean bOut;
algorithm
bOut:=
matchcontinue (inVariableReplacements,inComponentRef)
match (inVariableReplacements,inComponentRef)
local
DAE.ComponentRef src;
DAE.Exp dst;
HashTable2.HashTable ht;
case (REPLACEMENTS(hashTable=ht),src)
equation
_ = BaseHashTable.get(src,ht);
guard BaseHashTable.hasKey(src,ht)
then
true;
else false;
end matchcontinue;
end match;
end hasReplacement;

public function hasReplacementCrefFirst "
Expand All @@ -815,18 +806,17 @@ public function hasReplacementCrefFirst "
output Boolean bOut;
algorithm
bOut:=
matchcontinue (inComponentRef,inVariableReplacements)
match (inComponentRef,inVariableReplacements)
local
DAE.ComponentRef src;
DAE.Exp dst;
HashTable2.HashTable ht;
case (src,REPLACEMENTS(hashTable=ht))
equation
_ = BaseHashTable.get(src,ht);
guard BaseHashTable.hasKey(src,ht)
then
true;
else false;
end matchcontinue;
end match;
end hasReplacementCrefFirst;

public function hasNoReplacementCrefFirst "
Expand All @@ -837,18 +827,17 @@ public function hasNoReplacementCrefFirst "
output Boolean bOut;
algorithm
bOut:=
matchcontinue (inComponentRef,inVariableReplacements)
match (inComponentRef,inVariableReplacements)
local
DAE.ComponentRef src;
DAE.Exp dst;
HashTable2.HashTable ht;
case (src,REPLACEMENTS(hashTable=ht))
equation
_ = BaseHashTable.get(src,ht);
guard BaseHashTable.hasKey(src,ht)
then
false;
else true;
end matchcontinue;
end match;
end hasNoReplacementCrefFirst;

public function varHasNoReplacement "
Expand All @@ -859,18 +848,17 @@ public function varHasNoReplacement "
output Boolean bOut;
algorithm
bOut:=
matchcontinue (var,inVariableReplacements)
match (var,inVariableReplacements)
local
DAE.ComponentRef src;
DAE.Exp dst;
HashTable2.HashTable ht;
case (BackendDAE.VAR(varName=src),REPLACEMENTS(hashTable=ht))
equation
_ = BaseHashTable.get(src,ht);
guard BaseHashTable.hasKey(src,ht)
then
false;
else true;
end matchcontinue;
end match;
end varHasNoReplacement;

public function getReplacementVarArraySize
Expand Down
62 changes: 27 additions & 35 deletions Compiler/BackEnd/Uncertainties.mo
Expand Up @@ -1758,7 +1758,7 @@ protected function findArraysPartiallyIndexed2 "
output HashTable.HashTable outHt;

algorithm
outHt := matchcontinue(inRef,indubRef,inht)
outHt := match(inRef,indubRef,inht)
local
DAE.ComponentRef c1,c2;
DAE.Exp e1;
Expand All @@ -1768,35 +1768,26 @@ algorithm
case({}, _, ht) then ht;

case(((DAE.CREF(c1,_))::expl1),dubRef,ht)
equation
c2 = ComponentReference.crefStripLastSubs(c1);
failure(_ = BaseHashTable.get(c2,dubRef));
dubRef = BaseHashTable.add((c2,1),dubRef);
ht = findArraysPartiallyIndexed2(expl1,dubRef,ht);
then ht;

case(((DAE.CREF(c1,_))::expl1),dubRef,ht)
equation
c2 = ComponentReference.crefStripLastSubs(c1);
_ = BaseHashTable.get(c2,dubRef);
_ = BaseHashTable.get(c2,ht);// if we have one occurrence, most likely it will be more.
ht = findArraysPartiallyIndexed2(expl1,dubRef,ht);
algorithm
c2 := ComponentReference.crefStripLastSubs(c1);
if BaseHashTable.hasKey(c2,dubRef) then
if BaseHashTable.hasKey(c2,ht) then
// if we have one occurrence, most likely it will be more.
else
ht := BaseHashTable.add((c2,1),ht);
end if;
else
dubRef := BaseHashTable.add((c2,1),dubRef);
end if;
ht := findArraysPartiallyIndexed2(expl1,dubRef,ht);
then ht;

case(((DAE.CREF(c1,_))::expl1),dubRef,ht)
equation
c2 = ComponentReference.crefStripLastSubs(c1);
_ = BaseHashTable.get(c2,dubRef);
failure(_ = BaseHashTable.get(c2,ht));
ht = BaseHashTable.add((c2,1),ht);
ht = findArraysPartiallyIndexed2(expl1,dubRef,ht);
then ht;
case(_::expl1,dubRef,ht)
equation
ht = findArraysPartiallyIndexed2(expl1,dubRef,ht);
then
ht;
end matchcontinue;
end match;
end findArraysPartiallyIndexed2;


Expand Down Expand Up @@ -2228,26 +2219,27 @@ protected function moveVariables2 "
output list<BackendDAE.Var> outVarLst2;
algorithm
(outVarLst1,outVarLst2):=
matchcontinue (inVarLst1,inVarLst2,hashTable)
match (inVarLst1,inVarLst2,hashTable)
local
list<BackendDAE.Var> globalKnownVars,vs_1,knvars_1,vs;
BackendDAE.Var v;
DAE.ComponentRef cr;
HashTable.HashTable mvars;
case ({},globalKnownVars,_) then ({},globalKnownVars);
case (((v as BackendDAE.VAR(varName = cr)) :: vs),globalKnownVars,mvars)
equation
_ = BaseHashTable.get(cr,mvars) "alg var moved to known vars";
(vs_1,knvars_1) = moveVariables2(vs, globalKnownVars, mvars);
then
(vs_1,(v :: knvars_1));
case (((v as BackendDAE.VAR(varName = cr)) :: vs),globalKnownVars,mvars)
equation
failure(_ = BaseHashTable.get(cr,mvars)) "alg var not moved to known vars";
(vs_1,knvars_1) = moveVariables2(vs, globalKnownVars, mvars);
algorithm
if BaseHashTable.hasKey(cr,mvars) then
// alg var moved to known vars
(vs_1,knvars_1) := moveVariables2(vs, globalKnownVars, mvars);
knvars_1 := (v :: knvars_1);
else
// alg var not moved to known vars
(vs_1,knvars_1) := moveVariables2(vs, globalKnownVars, mvars);
vs_1 := (v :: vs_1);
end if;
then
((v :: vs_1),knvars_1);
end matchcontinue;
(vs_1,knvars_1);
end match;
end moveVariables2;

replaceable type ElementType subtypeof Any;
Expand Down
4 changes: 2 additions & 2 deletions Compiler/FrontEnd/ConnectionGraph.mo
Expand Up @@ -858,7 +858,7 @@ algorithm
setRootDistance(nextLevel,table,distance+1,{},irooted);
case(cr::rest,_,_,_,_)
equation
failure(_ = BaseHashTable.get(cr, irooted));
false = BaseHashTable.hasKey(cr, irooted);
rooted = BaseHashTable.add((cr,distance),irooted);
next = BaseHashTable.get(cr, table);
//print("- ConnectionGraph.setRootDistance: Set Distance " +
Expand All @@ -870,7 +870,7 @@ algorithm
setRootDistance(rest,table,distance,next,rooted);
case(cr::rest,_,_,_,_)
equation
failure(_ = BaseHashTable.get(cr, irooted));
false = BaseHashTable.hasKey(cr, irooted);
rooted = BaseHashTable.add((cr,distance),irooted);
//print("- ConnectionGraph.setRootDistance: Set Distance " +
// ComponentReference.printComponentRefStr(cr) + " , " + intString(distance) + "\n");
Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/InstUtil.mo
Expand Up @@ -8173,8 +8173,8 @@ modifiers to prevent infinite recursion"
algorithm
(outMods,outCmod,outM) := matchcontinue(variability,updatedComps,cref,mods,cmod,m)
case (_,_,_,_,_,_)
guard BaseHashTable.hasKey(cref,updatedComps)
equation
_ = BaseHashTable.get(cref,updatedComps);
checkVariabilityOfUpdatedComponent(variability,cref);
then (DAE.NOMOD(),DAE.NOMOD(),SCode.NOMOD());

Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/Patternm.mo
Expand Up @@ -1649,7 +1649,7 @@ algorithm
case ({},_,acc,unusedHt) then (listReverse(acc),unusedHt);
case (DAE.VAR(componentRef=DAE.CREF_IDENT(ident=name), source=DAE.SOURCE(info=info))::rest,_,acc,unusedHt)
equation
failure(_ = BaseHashTable.get(name, ht));
false = BaseHashTable.hasKey(name, ht);
unusedHt = BaseHashTable.add((name,Absyn.IDENT("")),unusedHt);
Error.assertionOrAddSourceMessage(not Flags.isSet(Flags.PATTERNM_ALL_INFO),Error.META_UNUSED_DECL, {name}, info);
(acc,unusedHt) = filterUnusedDecls(rest,ht,acc,unusedHt);
Expand Down
3 changes: 1 addition & 2 deletions Compiler/SimCode/SimCodeFunctionUtil.mo
Expand Up @@ -2349,8 +2349,7 @@ algorithm
HashTableStringToPath.HashTable ht;

case (_, _, ht, _)
equation
BaseHashTable.get(pathstr, ht);
guard BaseHashTable.hasKey(pathstr, ht)
then ht;

case (path, _, ht, _)
Expand Down
30 changes: 11 additions & 19 deletions Compiler/Util/VarTransform.mo
Expand Up @@ -945,26 +945,18 @@ protected function addReplacementInv2 "
input DAE.ComponentRef dst;
input DAE.ComponentRef src;
output HashTable3.HashTable outInvHt;
protected
list<DAE.ComponentRef> srcs;
algorithm
outInvHt:=
matchcontinue (invHt,dst,src)
local
HashTable3.HashTable invHt_1;
list<DAE.ComponentRef> srcs;
case (_,_,_)
equation
failure(_ = BaseHashTable.get(dst,invHt)) "No previous elt for dst -> src" ;
invHt_1 = BaseHashTable.add((dst, {src}),invHt);
then
invHt_1;
case (_,_,_)
equation
srcs = BaseHashTable.get(dst,invHt) "previous elt for dst -> src, append.." ;
srcs = amortizeUnion(src::srcs);//List.union({},src::srcs);
invHt_1 = BaseHashTable.add((dst, srcs),invHt);
then
invHt_1;
end matchcontinue;
if BaseHashTable.hasKey(dst,invHt) then
// previous elt for dst -> src, append.
srcs := BaseHashTable.get(dst,invHt);
srcs := amortizeUnion(src::srcs);//List.union({},src::srcs);
outInvHt := BaseHashTable.add((dst, srcs),invHt);
else
// No previous elt for dst -> src
outInvHt := BaseHashTable.add((dst, {src}),invHt);
end if;
end addReplacementInv2;

protected function amortizeUnion "performs listUnion but in an 'amortized' way, by only doing it occasionally"
Expand Down

0 comments on commit 13b595d

Please sign in to comment.