Skip to content

Commit

Permalink
recursive -> loop list optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
hkiel committed Feb 17, 2016
1 parent dd6bae1 commit 3cd16cc
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 78 deletions.
82 changes: 37 additions & 45 deletions Compiler/FrontEnd/DAEUtil.mo
Expand Up @@ -5418,20 +5418,16 @@ end avlTreeToList2;
public function avlTreeAddLst "Adds a list of (key,value) pairs"
input list<tuple<DAE.AvlKey,DAE.AvlValue>> inValues;
input DAE.AvlTree inTree;
output DAE.AvlTree outTree;
algorithm
outTree := match(inValues,inTree)
local
DAE.AvlKey key;
list<tuple<DAE.AvlKey,DAE.AvlValue>> values;
DAE.AvlValue val;
DAE.AvlTree tree;
case({},tree) then tree;
case((key,val)::values,tree) equation
tree = avlTreeAdd(tree,key,val);
tree = avlTreeAddLst(values,tree);
then tree;
end match;
output DAE.AvlTree outTree = inTree;
protected
DAE.AvlKey key;
list<tuple<DAE.AvlKey,DAE.AvlValue>> values = inValues;
DAE.AvlValue val;
algorithm
while not listEmpty(values) loop
(key,val)::values := values;
outTree := avlTreeAdd(outTree,key,val);
end while;
end avlTreeAddLst;

public function avlTreeAdd "
Expand Down Expand Up @@ -5526,54 +5522,50 @@ end balance;
protected function doBalance "perform balance if difference is > 1 or < -1"
input Integer difference;
input DAE.AvlTree inBt;
output DAE.AvlTree outBt;
output DAE.AvlTree outBt = inBt;
algorithm
outBt := matchcontinue(difference,inBt)
local DAE.AvlTree bt;
case(-1,bt) then computeHeight(bt);
case(0,bt) then computeHeight(bt);
case(1,bt) then computeHeight(bt);
/* d < -1 or d > 1 */
case(_,bt) equation
bt = doBalance2(difference,bt);
then bt;
else inBt;
end matchcontinue;
if difference < -1 or difference > 1 then
try
outBt := doBalance2(difference, inBt);
else
outBt := inBt;
end try;
else
outBt := computeHeight(inBt);
end if;
end doBalance;

protected function doBalance2 "help function to doBalance"
input Integer difference;
input DAE.AvlTree inBt;
output DAE.AvlTree outBt;
protected
DAE.AvlTree bt = inBt;
algorithm
outBt := matchcontinue(difference,inBt)
local DAE.AvlTree bt;
case(_,bt) equation
true = difference < 0;
bt = doBalance3(bt);
bt = rotateLeft(bt);
then bt;
case(_,bt) equation
true = difference > 0;
bt = doBalance4(bt);
bt = rotateRight(bt);
then bt;
end matchcontinue;
if difference < 0 then
bt := doBalance3(bt);
bt := rotateLeft(bt);
end if;
if difference > 0 then
bt := doBalance4(bt);
bt := rotateRight(bt);
end if;
outBt := bt;
end doBalance2;

protected function doBalance3 "help function to doBalance2"
input DAE.AvlTree inBt;
output DAE.AvlTree outBt;
algorithm
outBt := matchcontinue(inBt)
outBt := match(inBt)
local DAE.AvlTree rr,bt;
case(bt) equation
true = differenceInHeight(getOption(rightNode(bt))) > 0;
case(bt) guard differenceInHeight(getOption(rightNode(bt))) > 0
equation
rr = rotateRight(getOption(rightNode(bt)));
bt = setRight(bt,SOME(rr));
then bt;
else inBt;
end matchcontinue;
end match;
end doBalance3;

protected function doBalance4 "help function to doBalance2"
Expand All @@ -5582,8 +5574,8 @@ protected function doBalance4 "help function to doBalance2"
algorithm
outBt := match(inBt)
local DAE.AvlTree rl,bt;
case(bt) equation
true = differenceInHeight(getOption(leftNode(bt))) < 0;
case(bt) guard differenceInHeight(getOption(leftNode(bt))) < 0
equation
rl = rotateLeft(getOption(leftNode(bt)));
bt = setLeft(bt,SOME(rl));
then bt;
Expand Down
13 changes: 5 additions & 8 deletions Compiler/FrontEnd/NFSCodeEnv.mo
Expand Up @@ -2162,16 +2162,13 @@ end balance;
protected function doBalance
"Performs balance if difference is > 1 or < -1"
input Integer difference;
input AvlTree bt;
input AvlTree inBt;
output AvlTree outBt;
algorithm
outBt := match(difference, bt)
case(-1, _) then computeHeight(bt);
case( 0, _) then computeHeight(bt);
case( 1, _) then computeHeight(bt);
// d < -1 or d > 1
else doBalance2(difference < 0, bt);
end match;
outBt := if difference < -1 or difference > 1 then
doBalance2(difference < 0, inBt)
else
computeHeight(inBt);
end doBalance;

protected function doBalance2
Expand Down
47 changes: 22 additions & 25 deletions Compiler/FrontEnd/SCodeUtil.mo
Expand Up @@ -986,31 +986,28 @@ public function translateEitemlist
input list<Absyn.ElementItem> inAbsynElementItemLst;
input SCode.Visibility inVisibility;
output list<SCode.Element> outElementLst;
algorithm
outElementLst := match (inAbsynElementItemLst,inVisibility)
local
list<SCode.Element> l,e_1,es_1;
list<Absyn.ElementItem> es;
SCode.Visibility vis;
Absyn.Element e;

case ({},_) then {};
case ((Absyn.ELEMENTITEM(element = e) :: es),vis)
equation
// fprintln(Flags.TRANSLATE, "translating element: " + Dump.unparseElementStr(1, e));
e_1 = translateElement(e, vis);
es_1 = translateEitemlist(es, vis);
l = listAppend(e_1, es_1);
then l;

case ((Absyn.LEXER_COMMENT() :: es),vis)
then translateEitemlist(es, vis);

case ((_ :: es),vis)
equation
Error.addMessage(Error.INTERNAL_ERROR,{"SCodeUtil.translateEitemlist failed"});
then translateEitemlist(es, vis);
end match;
protected
list<SCode.Element> l = {};
list<Absyn.ElementItem> es = inAbsynElementItemLst;
Absyn.ElementItem ei;
SCode.Visibility vis;
Absyn.Element e;
algorithm
while not listEmpty(es) loop
ei::es := es;
_ := match (ei)
local
list<SCode.Element> e_1;
case (Absyn.ELEMENTITEM(element = e))
equation
// fprintln(Flags.TRANSLATE, "translating element: " + Dump.unparseElementStr(1, e));
e_1 = translateElement(e, inVisibility);
l = listAppend(l, e_1);
then ();
else ();
end match;
end while;
outElementLst := l;
end translateEitemlist;

// stefan
Expand Down

0 comments on commit 3cd16cc

Please sign in to comment.