Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 3b5135a

Browse files
lochelOpenModelica-Hudson
authored andcommitted
Simplify match/matchcontinue pattern
1 parent 1754fe3 commit 3b5135a

File tree

1 file changed

+63
-90
lines changed

1 file changed

+63
-90
lines changed

Compiler/BackEnd/IndexReduction.mo

Lines changed: 63 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -162,20 +162,13 @@ protected function getChangedEqnsAndLowest
162162
input array<Integer> ass2;
163163
input list<Integer> iAcc;
164164
input Integer iLowest;
165-
output list<Integer> oAcc;
166-
output Integer oLowest;
165+
output list<Integer> oAcc = iAcc;
166+
output Integer oLowest = iLowest;
167167
algorithm
168-
(oAcc,oLowest) := match (index,ass2,iAcc,iLowest)
169-
local
170-
list<Integer> acc;
171-
Integer l;
172-
case (0,_,_,_) then (iAcc,iLowest);
173-
case (_,_,_,_)
174-
equation
175-
true = intGt(index,0);
176-
(acc,l) = getChangedEqnsAndLowest(index-1,ass2,List.consOnTrue(intLt(ass2[index],1),index,iAcc),index);
177-
then (acc,l);
178-
end match;
168+
for i in index:-1:1 loop
169+
oAcc := List.consOnTrue(intLt(ass2[i], 1), i, oAcc);
170+
oLowest := i;
171+
end for;
179172
end getChangedEqnsAndLowest;
180173

181174
protected function pantelidesIndexReduction1
@@ -201,8 +194,8 @@ protected function pantelidesIndexReduction1
201194
output BackendDAE.StructurallySingularSystemHandlerArg outArg;
202195
output list<tuple<list<Integer>,list<Integer>,list<Integer>>> oNotDiffableMSS;
203196
algorithm
204-
(osyst,oshared,outAssignments1,outAssignments2,outArg,oNotDiffableMSS):=
205-
matchcontinue (unassignedStates,unassignedEqns,alleqns,iEqns,actualEqn,inSystem,inShared,inAssignments1,inAssignments2,mark,markarr,inArg,iNotDiffableMSS)
197+
(osyst,oshared,outAssignments1,outAssignments2,outArg,oNotDiffableMSS) :=
198+
matchcontinue (unassignedStates, unassignedEqns, alleqns, iEqns)
206199
local
207200
list<Integer> states,eqns,eqns_1,ueqns;
208201
list<list<Integer>> statelst,ueqnsrest,eqnsrest,eqnsrest_1;
@@ -211,12 +204,12 @@ algorithm
211204
BackendDAE.Shared shared;
212205
BackendDAE.StructurallySingularSystemHandlerArg arg;
213206
list<tuple<list<Integer>,list<Integer>,list<Integer>>> notDiffableMSS;
214-
case (_,_,_,{},_,_,_,_,_,_,_,_,_)
207+
case (_, _, _, {})
215208
equation
216209
(syst,shared,ass1,ass2,arg) = handleundifferntiableMSSLst(iNotDiffableMSS,inSystem,inShared,inAssignments1,inAssignments2,inArg);
217210
then
218211
(syst,shared,ass1,ass2,arg,{});
219-
case (states::statelst,ueqns::ueqnsrest,eqns::eqnsrest,eqns_1::eqnsrest_1,_,_,_,_,_,_,_,_,_)
212+
case (states::statelst, ueqns::ueqnsrest, eqns::eqnsrest, eqns_1::eqnsrest_1)
220213
equation
221214
(syst,shared,ass1,ass2,arg,notDiffableMSS) =
222215
pantelidesIndexReductionMSS(states,ueqns,eqns,eqns_1,actualEqn,inSystem,inShared,inAssignments1,inAssignments2,mark,markarr,inArg,iNotDiffableMSS);
@@ -474,14 +467,14 @@ protected function unassignedContinuesEqns
474467
input tuple<list<Integer>,list<Integer>,list<Integer>> inFold;
475468
output tuple<list<Integer>,list<Integer>,list<Integer>> outFold;
476469
algorithm
477-
outFold := matchcontinue(eindx,vars,inFold)
470+
outFold := matchcontinue(inFold)
478471
local
479472
Integer vindx;
480473
list<Integer> unassignedEqns,eqnsLst,varlst,discEqns;
481474
list<BackendDAE.Var> vlst;
482475
Boolean b,ba;
483476
list<Boolean> blst;
484-
/* case(_,_,(unassignedEqns,eqnsLst))
477+
/* case((unassignedEqns,eqnsLst))
485478
equation
486479
vindx = ass2[eindx];
487480
true = intGt(vindx,0);
@@ -490,7 +483,7 @@ algorithm
490483
eqnsLst = List.consOnTrue(not b, eindx, eqnsLst);
491484
then
492485
((unassignedEqns,eqnsLst));
493-
*/ case(_,_,(unassignedEqns,eqnsLst,discEqns))
486+
*/ case((unassignedEqns,eqnsLst,discEqns))
494487
equation
495488
vindx = ass2[eindx];
496489
ba = intLt(vindx,1);
@@ -505,7 +498,7 @@ algorithm
505498
discEqns = List.consOnTrue(b, eindx, discEqns);
506499
then
507500
((unassignedEqns,eqnsLst,discEqns));
508-
case(_,_,(unassignedEqns,eqnsLst,discEqns))
501+
case((unassignedEqns,eqnsLst,discEqns))
509502
equation
510503
vindx = ass2[eindx];
511504
false = intGt(vindx,0);
@@ -625,10 +618,10 @@ algorithm
625618
eqnslst1 := List.map1r(eqnslst1,arrayGet,imapIncRowEqn);
626619
eqnslst1 := List.uniqueIntN(listAppend(MSSSeqs,eqnslst1),numEqs1);
627620
eqnslst1 := listAppend(eqnslst1,eqnslst);
628-
if Flags.isSet(Flags.BLT_DUMP) then
629-
print("Update Incidence Matrix: ");
630-
BackendDump.debuglst(eqnslst1,intString," ","\n");
631-
end if;
621+
if Flags.isSet(Flags.BLT_DUMP) then
622+
print("Update Incidence Matrix: ");
623+
BackendDump.debuglst(eqnslst1,intString," ","\n");
624+
end if;
632625
funcs := BackendDAEUtil.getFunctions(inShared);
633626
(syst,omapEqnIncRow,omapIncRowEqn) :=
634627
BackendDAEUtil.updateIncidenceMatrixScalar(syst, BackendDAE.SOLVABLE(), SOME(funcs), eqnslst1, imapEqnIncRow, imapIncRowEqn);
@@ -846,15 +839,13 @@ protected function statesWithUnusedDerivative
846839
input list<Integer> iAcc;
847840
output list<Integer> oAcc;
848841
algorithm
849-
oAcc := matchcontinue(state,mt,iAcc)
850-
case(_,_,_)
842+
oAcc := matchcontinue(state)
843+
case(_)
851844
equation
852-
List.map1AllValue(mt[state],intLt,true,0);
845+
List.map1AllValue(mt[state], intLt, true, 0);
853846
then
854847
state::iAcc;
855-
else
856-
then
857-
iAcc;
848+
else iAcc;
858849
end matchcontinue;
859850
end statesWithUnusedDerivative;
860851

@@ -1161,8 +1152,7 @@ protected function replaceFinalVarsGetExp
11611152
input output BackendVarTransform.VariableReplacements repl;
11621153
input output Boolean b;
11631154
algorithm
1164-
(repl, b) :=
1165-
matchcontinue (inVar)
1155+
(repl, b) := matchcontinue (inVar)
11661156
local
11671157
BackendVarTransform.VariableReplacements repl1;
11681158
DAE.ComponentRef cr;
@@ -1525,16 +1515,12 @@ protected
15251515
Integer rang, nStateCandidates, nUnassignedEquations;
15261516
list<BackendDAE.Var> stateCandidates;
15271517
algorithm
1528-
15291518
for tpl in iTplLst loop
1530-
15311519
(_,_,nStateCandidates,nUnassignedEquations,stateCandidates,_,_,_) := tpl;
15321520
rang := nStateCandidates - nUnassignedEquations;
15331521
(_,stateCandidates) := List.split(stateCandidates, rang);
15341522
dummyStates := listAppend(stateCandidates, dummyStates);
1535-
15361523
end for;
1537-
15381524
end reduceStateSets2;
15391525

15401526

@@ -1691,8 +1677,7 @@ protected function selectStates
16911677
output HashTableCrIntToExp.HashTable oHt;
16921678
output Integer oSetIndex;
16931679
algorithm
1694-
(osyst,oshared,oHt,oSetIndex) :=
1695-
matchcontinue inSystem
1680+
(osyst,oshared,oHt,oSetIndex) := matchcontinue inSystem
16961681
local
16971682
list<BackendDAE.Equation> eqnslst;
16981683
HashTableCrIntToExp.HashTable ht;
@@ -2257,29 +2242,22 @@ protected function getIncidenceMatrixSelectStates
22572242
input BackendDAE.IncidenceMatrixT mT "input/output";
22582243
input BackendDAE.IncidenceMatrix mo;
22592244
input array<Integer> stateindexs;
2245+
protected
2246+
list<Integer> row, negrow;
22602247
algorithm
2261-
_ := match(nEqns,m,mT,mo,stateindexs)
2262-
local
2263-
list<Integer> row,negrow;
2264-
case (0,_,_,_,_) then ();
2265-
case (_,_,_,_,_)
2266-
equation
2267-
// get row
2268-
row = mo[nEqns];
2269-
// replace negative index with index from stateindexs
2270-
row = List.map1(row,replaceStateIndex,stateindexs);
2271-
// update m
2272-
arrayUpdate(m,nEqns,row);
2273-
// update mT
2274-
(row,negrow) = List.split1OnTrue(row, intGt, 0);
2275-
_ = List.fold1(row,Array.consToElement,nEqns,mT);
2276-
row = List.map(negrow,intAbs);
2277-
_ = List.fold1(row,Array.consToElement,-nEqns,mT);
2278-
// next
2279-
getIncidenceMatrixSelectStates(nEqns-1,m,mT,mo,stateindexs);
2280-
then
2281-
();
2282-
end match;
2248+
for i in nEqns:-1:1 loop
2249+
// get row
2250+
row := mo[i];
2251+
// replace negative index with index from stateindexs
2252+
row := List.map1(row,replaceStateIndex,stateindexs);
2253+
// update m
2254+
arrayUpdate(m,i,row);
2255+
// update mT
2256+
(row, negrow) := List.split1OnTrue(row, intGt, 0);
2257+
_ := List.fold1(row,Array.consToElement,i,mT);
2258+
row := List.map(negrow,intAbs);
2259+
_ := List.fold1(row,Array.consToElement,-i,mT);
2260+
end for;
22832261
end getIncidenceMatrixSelectStates;
22842262

22852263
protected function replaceStateIndex
@@ -2314,20 +2292,18 @@ protected function getIncidenceMatrixLevelEquations
23142292
input array<Integer> stateindexs;
23152293
input DAE.FunctionTree functionTree;
23162294
algorithm
2317-
_ :=
2318-
match (iEqns, vars, index, sindex, m, mT, om, mapEqnIncRow, mapIncRowEqn, stateindexs, functionTree)
2295+
_ := match (iEqns)
23192296
local
23202297
list<BackendDAE.Equation> rest;
23212298
AvlSetInt.Tree rowTree;
23222299
list<Integer> row,rowindxs,negrow;
23232300
BackendDAE.Equation e;
23242301
Integer i1,rowSize,size;
23252302

2326-
case ({}, _, _, _, _, _, _, _, _, _, _) then ();
2303+
case {} then ();
23272304

23282305
// i < n
2329-
case (e::rest, _, _, _, _, _, _, _, _, _, _)
2330-
equation
2306+
case e::rest equation
23312307
// compute the row
23322308
(rowTree,size) = BackendDAEUtil.incidenceRow(e, vars, BackendDAE.SOLVABLE(), SOME(functionTree), AvlSetInt.EMPTY());
23332309
row = AvlSetInt.listKeys(rowTree);
@@ -2348,8 +2324,7 @@ algorithm
23482324
_ = List.fold1(row,Array.appendToElement,rowindxs,mT);
23492325
// next equation
23502326
getIncidenceMatrixLevelEquations(rest, vars, i1, rowSize, m, mT, om, mapEqnIncRow, mapIncRowEqn, stateindexs, functionTree);
2351-
then
2352-
();
2327+
then ();
23532328
end match;
23542329
end getIncidenceMatrixLevelEquations;
23552330

@@ -2383,12 +2358,12 @@ protected function partitionSystem1
23832358
input Integer iNSystems;
23842359
output Integer oNSystems;
23852360
algorithm
2386-
oNSystems := matchcontinue(index,m,mT,rowmarkarr,collmarkarr,iNSystems)
2361+
oNSystems := matchcontinue(index)
23872362
local
23882363
list<Integer> rows;
23892364
Integer nsystems;
2390-
case (0,_,_,_,_,_) then iNSystems-1;
2391-
case (_,_,_,_,_,_)
2365+
case 0 then iNSystems-1;
2366+
case _
23922367
equation
23932368
// if unmarked then increse nsystems
23942369
false = intGt(rowmarkarr[index],0);
@@ -2397,12 +2372,10 @@ algorithm
23972372
nsystems = partitionSystemstraverseRows(rows,{},m,mT,rowmarkarr,collmarkarr,iNSystems);
23982373
then
23992374
partitionSystem1(index-1,m,mT,rowmarkarr,collmarkarr,nsystems);
2400-
else
2401-
equation
2402-
// if marked skipp it
2403-
true = intGt(rowmarkarr[index],0);
2404-
then
2405-
partitionSystem1(index-1,m,mT,rowmarkarr,collmarkarr,iNSystems);
2375+
else equation
2376+
// if marked skip it
2377+
true = intGt(rowmarkarr[index],0);
2378+
then partitionSystem1(index-1,m,mT,rowmarkarr,collmarkarr,iNSystems);
24062379
end matchcontinue;
24072380
end partitionSystem1;
24082381

@@ -2416,15 +2389,15 @@ protected function partitionSystemstraverseRows
24162389
input Integer iNSystems;
24172390
output Integer oNSystems;
24182391
algorithm
2419-
oNSystems := matchcontinue(iRows,iQueue,m,mT,rowmarkarr,collmarkarr,iNSystems)
2392+
oNSystems := matchcontinue(iRows,iQueue)
24202393
local
24212394
list<Integer> rest,colls,rows;
24222395
Integer r;
2423-
case ({},{},_,_,_,_,_) then iNSystems+1;
2424-
case ({},_,_,_,_,_,_)
2396+
case ({},{}) then iNSystems+1;
2397+
case ({},_)
24252398
then
24262399
partitionSystemstraverseRows(iQueue,{},m,mT,rowmarkarr,collmarkarr,iNSystems);
2427-
case (r::rest,_,_,_,_,_,_)
2400+
case (r::rest,_)
24282401
equation
24292402
// if unmarked then add
24302403
false = intGt(collmarkarr[r],0);
@@ -2437,7 +2410,7 @@ algorithm
24372410
rows = listAppend(rows,iQueue);
24382411
then
24392412
partitionSystemstraverseRows(rest,rows,m,mT,rowmarkarr,collmarkarr,iNSystems);
2440-
case (r::rest,_,_,_,_,_,_)
2413+
case (r::rest,_)
24412414
equation
24422415
// if marked skipp it
24432416
true = intGt(collmarkarr[r],0);
@@ -2452,12 +2425,12 @@ protected function partitionSystemSplitt
24522425
input array<list<Integer>> systsarr;
24532426
output array<list<Integer>> osystsarr;
24542427
algorithm
2455-
osystsarr := match(index,rowmarkarr,systsarr)
2428+
osystsarr := match(index)
24562429
local
24572430
Integer i;
24582431
array<list<Integer>> arr;
2459-
case (0,_,_) then systsarr;
2460-
case (_,_,_)
2432+
case (0) then systsarr;
2433+
case (_)
24612434
equation
24622435
i = rowmarkarr[index];
24632436
arr = Array.consToElement(i, index, systsarr);
@@ -2507,8 +2480,8 @@ protected
25072480
StateSets stateSets;
25082481
algorithm
25092482
try
2510-
for seteqns in iSets loop
2511-
if not listEmpty(List.select1r(seteqns,Matching.isUnAssigned,vec1)) then // ignore sets without unassigned equations, because all assigned states already in dummy states
2483+
for seteqns in iSets loop
2484+
if not listEmpty(List.select1r(seteqns,Matching.isUnAssigned,vec1)) then // ignore sets without unassigned equations, because all assigned states already in dummy states
25122485
// print("seteqns: " + intString(listLength(seteqns)) + "\n");
25132486
// print(stringDelimitList(List.map(seteqns,intString),", ") + "\n");
25142487
unassigned := List.select1r(seteqns,Matching.isUnAssigned,vec1);
@@ -2559,10 +2532,10 @@ algorithm
25592532
dummyStates := List.map(varlst,BackendVariable.varCref);
25602533
outDummyStates := listAppend(outDummyStates,dummyStates);
25612534
outDummyVars := listAppend(varlst, outDummyVars);
2562-
end if;
2563-
end for;
2535+
end if;
2536+
end for;
25642537
else
2565-
Error.addMessage(Error.INTERNAL_ERROR, {"- IndexReduction.processComps4New failed!"});
2538+
Error.addMessage(Error.INTERNAL_ERROR, {"- IndexReduction.processComps4New failed!"});
25662539
fail();
25672540
end try;
25682541
end processComps4New;

0 commit comments

Comments
 (0)