Skip to content

Commit

Permalink
- Make more functions work with one-based indices:
Browse files Browse the repository at this point in the history
  - List.replaceAt
  - List.positionOnTrue
  - List.positionList
- Use listGet instead of listNth in some places
- Add tests for List.List.replaceAt and List.replaceAtIndexFirst


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@22973 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
lochel committed Oct 27, 2014
1 parent de29fb1 commit 4037b0a
Show file tree
Hide file tree
Showing 18 changed files with 98 additions and 121 deletions.
28 changes: 14 additions & 14 deletions Compiler/BackEnd/BackendQSS.mo
Expand Up @@ -314,35 +314,35 @@ algorithm
case (DAE.CREF(componentRef = cr as DAE.CREF_IDENT(_,t1,subs),ty=t),(states,disc,algs))
equation
p = List.positionOnTrue(cr,states,ComponentReference.crefEqual);
ident = stringAppend(stringAppend("x[",intString(p+1)),"]");
ident = stringAppend(stringAppend("x[",intString(p)),"]");
then (DAE.CREF(DAE.CREF_IDENT(ident,t1,subs),t),(states,disc,algs));
case (DAE.CREF(componentRef = cr as DAE.CREF_QUAL(_,t1,subs,_),ty=t),(states,disc,algs))
equation
p = List.positionOnTrue(cr,states,ComponentReference.crefEqual);
ident = stringAppend(stringAppend("x[",intString(p+1)),"]");
ident = stringAppend(stringAppend("x[",intString(p)),"]");
then (DAE.CREF(DAE.CREF_IDENT(ident,t1,subs),t),(states,disc,algs));
case (DAE.CREF(componentRef = cr as DAE.CREF_IDENT(_,t1,subs),ty=t),(states,disc,algs))
equation
//p = List.position(cr,disc);
p = List.positionOnTrue(cr,disc,ComponentReference.crefEqual);
ident = stringAppend(stringAppend("d[",intString(p+1)),"]");
ident = stringAppend(stringAppend("d[",intString(p)),"]");
then (DAE.CREF(DAE.CREF_IDENT(ident,t1,subs),t),(states,disc,algs));
case (DAE.CREF(componentRef = cr as DAE.CREF_QUAL(_,t1,subs,_),ty=t),(states,disc,algs))
equation
p = List.positionOnTrue(cr,disc,ComponentReference.crefEqual);
ident = stringAppend(stringAppend("d[",intString(p+1)),"]");
ident = stringAppend(stringAppend("d[",intString(p)),"]");
then (DAE.CREF(DAE.CREF_IDENT(ident,t1,subs),t),(states,disc,algs));
case (DAE.CREF(componentRef = cr as DAE.CREF_IDENT(_,t1,subs),ty=t),(states,disc,algs))
equation
//p = List.position(cr,algs);
p = List.positionOnTrue(cr,algs,ComponentReference.crefEqual);
ident = stringAppend(stringAppend("a[",intString(p+1)),"]");
ident = stringAppend(stringAppend("a[",intString(p)),"]");
then (DAE.CREF(DAE.CREF_IDENT(ident,t1,subs),t),(states,disc,algs));
case (DAE.CREF(componentRef = cr as DAE.CREF_QUAL(_,t1,subs,_),ty=t),(states,disc,algs))
equation
//p = List.position(cr,algs);
p = List.positionOnTrue(cr,algs,ComponentReference.crefEqual);
ident = stringAppend(stringAppend("a[",intString(p+1)),"]");
ident = stringAppend(stringAppend("a[",intString(p)),"]");
then (DAE.CREF(DAE.CREF_IDENT(ident,t1,subs),t),(states,disc,algs));
case (DAE.CREF(cr as DAE.CREF_IDENT(_,t1,subs),t),(states,disc,algs))
equation
Expand Down Expand Up @@ -461,19 +461,19 @@ algorithm
case (_,_,_,_)
equation
p = List.positionOnTrue(cr,states,ComponentReference.crefEqual);
then stringAppend(stringAppend("x[",intString(p+1)),"]");
then stringAppend(stringAppend("x[",intString(p)),"]");
case (DAE.CREF_QUAL(_,_,_,r),_,_,_)
equation
p = List.positionOnTrue(r,states,ComponentReference.crefEqual);
then stringAppend(stringAppend("der(x[",intString(p+1)),"])");
then stringAppend(stringAppend("der(x[",intString(p)),"])");
case (_,_,_,_)
equation
p = List.positionOnTrue(cr,disc,ComponentReference.crefEqual);
then stringAppend(stringAppend("d[",intString(p+1)),"]");
then stringAppend(stringAppend("d[",intString(p)),"]");
case (_,_,_,_)
equation
p = List.positionOnTrue(cr,algs,ComponentReference.crefEqual);
then stringAppend(stringAppend("a[",intString(p+1)),"]");
then stringAppend(stringAppend("a[",intString(p)),"]");
case (_,_,_,_)
then ComponentReference.printComponentRefStr(cr);
end matchcontinue;
Expand Down Expand Up @@ -545,7 +545,7 @@ algorithm
s = stringAppend(s,"*/\n");
p = List.positionOnTrue(condition,zc_exps,Expression.expEqual);
s = stringAppend(s,"d[");
s = stringAppend(s,intString(p+1+offset));
s = stringAppend(s,intString(p+offset));
s = stringAppend(s,"] := 1.0;\n");
then s;

Expand Down Expand Up @@ -577,7 +577,7 @@ algorithm
s = stringAppend(s,"*/\n");
p = List.positionOnTrue(condition,zc_exps,Expression.expEqual);
s = stringAppend(s,"d[");
s = stringAppend(s,intString(p+1+offset));
s = stringAppend(s,intString(p+offset));
s = stringAppend(s,"] := 0.0;\n");
then s;
end matchcontinue;
Expand Down Expand Up @@ -847,12 +847,12 @@ algorithm
DAE.Exp e;
case (DAE.CREF(componentRef = cr as DAE.CREF_IDENT(_,t1,subs),ty=t),inputs)
equation
p = List.positionOnTrue(cr,inputs,ComponentReference.crefEqual);
p = List.positionOnTrue(cr,inputs,ComponentReference.crefEqual)-1 "shift to zero-based index";
ident = stringAppend("i",intString(p));
then (DAE.CREF(DAE.CREF_IDENT(ident,t1,subs),t),inputs);
case (DAE.CREF(componentRef = cr as DAE.CREF_QUAL(_,t1,subs,_),ty=t),inputs)
equation
p = List.positionOnTrue(cr,inputs,ComponentReference.crefEqual);
p = List.positionOnTrue(cr,inputs,ComponentReference.crefEqual)-1 "shift to zero-based index";
ident = stringAppend("i",intString(p));
then (DAE.CREF(DAE.CREF_IDENT(ident,t1,subs),t),inputs);
case (e,inputs) then (e,inputs);
Expand Down
10 changes: 5 additions & 5 deletions Compiler/BackEnd/HpcOmEqSystems.mo
Expand Up @@ -642,7 +642,7 @@ protected
algorithm
idx := listGet(positionLst,n);
entry := listGet(replacingLst,n);
outLst := List.replaceAt(entry,idx-1,inLst);
outLst := List.replaceAt(entry,idx,inLst);
end replaceAtPositionFromList;


Expand Down Expand Up @@ -1152,7 +1152,7 @@ algorithm
equation
varLst = List.first(lstLstIn);
varLst = varIn::varLst;
lstLstOut = List.replaceAt(varLst,0,lstLstIn);
lstLstOut = List.replaceAt(varLst, 1, lstLstIn);
then
lstLstOut;
end matchcontinue;
Expand All @@ -1175,7 +1175,7 @@ algorithm
equation
eqLst = List.first(lstLstIn);
eqLst = eqIn::eqLst;
lstLstOut = List.replaceAt(eqLst,0,lstLstIn);
lstLstOut = List.replaceAt(eqLst, 1, lstLstIn);
then
lstLstOut;
end matchcontinue;
Expand Down Expand Up @@ -1460,8 +1460,8 @@ algorithm
varLst := listGet(varLstLst,idx);
// remove all vars that are assigned to a constant or alias, remove the equations, update the replacement rule
((eqLst,varLst,repl)) := simplifyEquations2((eqLst,varLst,repl));
eqLstLst := List.replaceAt(eqLst,idx-1,eqLstLst);
varLstLst := List.replaceAt(varLst,idx-1,varLstLst);
eqLstLst := List.replaceAt(eqLst, idx, eqLstLst);
varLstLst := List.replaceAt(varLst, idx, varLstLst);
tplOut := ((eqLstLst,varLstLst,repl));
end simplifyEquations1;

Expand Down
22 changes: 11 additions & 11 deletions Compiler/BackEnd/HpcOmScheduler.mo
Expand Up @@ -2795,7 +2795,7 @@ algorithm
tasks = listAppend(tasks,assLocks);
thread = Debug.bcallret1(List.isNotEmpty(threads),List.first,threads,{});
thread = listAppend(tasks,thread);
threads = Debug.bcallret3(List.isNotEmpty(threads),List.replaceAt,thread,0,threads,{thread});
threads = Debug.bcallret3(List.isNotEmpty(threads),List.replaceAt,thread,1,threads,{thread});
//_ = printThreadSchedule(thread,thr);
outgoingDepTasks = listAppend(relLocks,outgoingDepTasks);
outgoingDepTasks = listAppend(assLocks,outgoingDepTasks);
Expand Down Expand Up @@ -4080,7 +4080,7 @@ algorithm
//assign rootNode to current thread and start a new thread(cluster)
thread = listGet(clustersIn,currThread);
thread = front::thread;
clusters = List.replaceAt(thread,currThread-1,clustersIn);
clusters = List.replaceAt(thread,currThread,clustersIn);
//print("cluster: "+&intListString(thread)+&"\n");
clusters = listAppend(clusters,{{}});
clusters = TDS_InitialCluster1(iTaskGraph,iTaskGraphT,iTaskGraphMeta,lastArrayIn,lactArrayIn,fpredArrayIn,rootNodes,taskAssIn,currThread+1,rest,clusters);
Expand All @@ -4095,7 +4095,7 @@ algorithm
//assign node from queue to a thread (in reversed order)
thread = listGet(clustersIn,currThread);
thread = front::thread;
clusters = List.replaceAt(thread,currThread-1,clustersIn);
clusters = List.replaceAt(thread,currThread,clustersIn);
//print("cluster: "+&intListString(thread)+&"\n");
_ = arrayUpdate(taskAssIn,front,currThread);
// go to predecessor
Expand All @@ -4112,7 +4112,7 @@ algorithm
//assign node from queue to a thread (in reversed order)
thread = listGet(clustersIn,currThread);
thread = front::thread;
clusters = List.replaceAt(thread,currThread-1,clustersIn);
clusters = List.replaceAt(thread,currThread,clustersIn);
//print("cluster: "+&intListString(thread)+&"\n");
_ = arrayUpdate(taskAssIn,front,currThread);
// check for other parents to get the next fpred
Expand Down Expand Up @@ -4315,7 +4315,7 @@ algorithm
// update the processorTimes
((_,exeCost)) = HpcOmTaskGraph.getExeCost(node,taskGraphMetaIn);
newTime = eft +. exeCost;
processorTime = List.replaceAt(newTime,processor-1,processorTimeIn);
processorTime = List.replaceAt(newTime,processor,processorTimeIn);
// next node
(taskAss,procAss) = MCP_getTaskAssignment1(rest,taskAss,procAss,processorTime,taskGraphIn,taskGraphMetaIn);
then
Expand Down Expand Up @@ -4803,10 +4803,10 @@ protected
ElementType r1,r2;
list<ElementType> lstTmp;
algorithm
r1 := listGet(lstIn,idx1);
r2 := listGet(lstIn,idx2);
lstTmp := List.replaceAt(r1,idx2-1,lstIn);
lstOut := List.replaceAt(r2,idx1-1,lstTmp);
r1 := listGet(lstIn, idx1);
r2 := listGet(lstIn, idx2);
lstTmp := List.replaceAt(r1, idx2, lstIn);
lstOut := List.replaceAt(r2, idx1, lstTmp);
end swapEntriesInList;

protected function getMedian3 "gets the median of the 3 reals and the info which of the inputs is the median"
Expand Down Expand Up @@ -5592,7 +5592,7 @@ algorithm
((_,exeCost)) = HpcOmTaskGraph.getExeCost(taskIdx,taskGraphMeta);
finishingTime = finishingTime +. exeCost;
task = updateTimeFinished(task, finishingTime);
thread = List.replaceAt(task,taskNum-1,thread);
thread = List.replaceAt(task, taskNum, thread);
threadTasks = arrayUpdate(threadTasksIn,threadIdx,thread);
checkedTasks = arrayUpdate(checkedTasksIn,taskIdx,task);
then
Expand Down Expand Up @@ -5620,7 +5620,7 @@ algorithm
finishingTime = realMax(finishingTime,finishingTime1);
// update
task = updateTimeFinished(task, finishingTime);
thread = List.replaceAt(task,taskNum-1,thread);
thread = List.replaceAt(task, taskNum, thread);
threadTasks = arrayUpdate(threadTasksIn,threadIdx,thread);
checkedTasks = arrayUpdate(checkedTasksIn,taskIdx,task);
then
Expand Down
12 changes: 6 additions & 6 deletions Compiler/BackEnd/HpcOmTaskGraph.mo
Expand Up @@ -3798,7 +3798,7 @@ algorithm
mergedSet = List.flatten(List.map1(mergedNodes,Array.getIndexFirst,primInComps));
//print("updateInComps1 mergedSet:" +& stringDelimitList(List.map(mergedSet,intString),",") +& "\n");
inCompLstTmp = List.fold(mergedNodes, updateInComps2, inCompLstIn);
inCompLstTmp = List.replaceAt(mergedSet,nodeIdx-1,inCompLstTmp);
inCompLstTmp = List.replaceAt(mergedSet, nodeIdx, inCompLstTmp);
then
inCompLstTmp;
else
Expand All @@ -3812,7 +3812,7 @@ protected function updateInComps2 "Replaces the entry <%iNodeIdx - 1%> in inComp
input list<list<Integer>> inCompLstIn;
output list<list<Integer>> inCompLstOut;
algorithm
inCompLstOut := List.replaceAt({},iNodeIdx-1,inCompLstIn);
inCompLstOut := List.replaceAt({}, iNodeIdx, inCompLstIn);
end updateInComps2;

protected function equalLists " compares two lists and sets true if they are equal.
Expand Down Expand Up @@ -3919,7 +3919,7 @@ algorithm
child = listGet(nodeChildren,1);
pathLst = List.first(lstIn);
pathLst = inPath::pathLst;
lstTmp = List.replaceAt(pathLst,0,lstIn);
lstTmp = List.replaceAt(pathLst, 1, lstIn);
rest = List.deleteMember(allNodes,inPath);
lstTmp = findOneChildParents(rest,graphIn,doNotMerge,lstTmp,child);
then
Expand All @@ -3933,7 +3933,7 @@ algorithm
true = List.isEmpty(nodeChildren) and listLength(parents) == 1;
pathLst = List.first(lstIn);
pathLst = inPath::pathLst;
lstTmp = List.replaceAt(pathLst,0,lstIn);
lstTmp = List.replaceAt(pathLst, 1, lstIn);
rest = List.deleteMember(allNodes,inPath);
lstTmp = findOneChildParents(rest,graphIn,doNotMerge,lstTmp,0);
then
Expand Down Expand Up @@ -3991,7 +3991,7 @@ algorithm
childLst = listReverse(childLst);
childLst = parent :: childLst;
childLst = listReverse(childLst);
lstTmp = List.replaceAt(childLst, lstIdx-1, lstIn);
lstTmp = List.replaceAt(childLst, lstIdx, lstIn);
then
lstTmp;
case(_,_,_)
Expand Down Expand Up @@ -4910,7 +4910,7 @@ algorithm
((level,_,_)) := arrayGet(nodeInfo,idx);
pSet := listGet(parallelSetIn,level);
pSet := idx :: pSet;
parallelSetOut := List.replaceAt(pSet,level-1,parallelSetIn);
parallelSetOut := List.replaceAt(pSet, level, parallelSetIn);
end gatherParallelSets1;

protected function getCostsForNode " function to compute the costs for the next node (including the execution costs and the communication costs).
Expand Down
8 changes: 4 additions & 4 deletions Compiler/BackEnd/ResolveLoops.mo
Expand Up @@ -587,7 +587,7 @@ algorithm
//print("replace equation "+&intString(pos)+&"\n");
replEqs = pos::replEqsIn;
pos = listGet(eqMapping,pos);
eqLst = List.replaceAt(resolvedEq,pos-1,eqLstIn);
eqLst = List.replaceAt(resolvedEq,pos,eqLstIn);

(eqLst,replEqs) = resolveLoops_resolveAndReplace(rest,eqCrossLstIn,varCrossLstIn,mIn,mTIn,eqMapping,varMapping,eqLst,varLstIn,replEqs);
then
Expand Down Expand Up @@ -640,7 +640,7 @@ algorithm
//print("replace equation "+&intString(pos)+&"\n");
replEqs = pos::replEqsIn;
pos = listGet(eqMapping,pos);
eqLst = List.replaceAt(resolvedEq,pos-1,eqLstIn);
eqLst = List.replaceAt(resolvedEq,pos,eqLstIn);

(eqLst,replEqs) = resolveLoops_resolveAndReplace(rest,eqCrossLstIn,varCrossLstIn,mIn,mTIn,eqMapping,varMapping,eqLst,varLstIn,replEqs);
then
Expand All @@ -665,7 +665,7 @@ algorithm
//print("replace equation "+&intString(pos)+&"\n");
pos = listGet(eqMapping,pos);
replEqs = pos::replEqsIn;
eqLst = List.replaceAt(resolvedEq,pos-1,eqLstIn);
eqLst = List.replaceAt(resolvedEq,pos,eqLstIn);
(eqLst,replEqs) = resolveLoops_resolveAndReplace(rest,eqCrossLstIn,varCrossLstIn,mIn,mTIn,eqMapping,varMapping,eqLst,varLstIn,replEqs);
then
(eqLst,replEqs);
Expand Down Expand Up @@ -1923,7 +1923,7 @@ algorithm
maxNum = List.fold(numOfAdjVars,intMax,List.first(numOfAdjVars));
replEqIdx = listGet(resolveEqs,List.position(maxNum,numOfAdjVars));
//BackendDump.dumpEquationList(unassEqsIn," not updated unassEqs");
unassEqs = List.replaceAt(resolvedEq,replEqIdx-1,unassEqsIn);
unassEqs = List.replaceAt(resolvedEq,replEqIdx,unassEqsIn);
//print("replace equation "+&intString(replEqIdx)+&"\n");
//BackendDump.dumpEquationList(unassEqs,"updated unassEqs");
then reshuffling_post4_resolveAndReplace(rest,unassEqs,unassVarsIn,me,meT);
Expand Down
4 changes: 2 additions & 2 deletions Compiler/BackEnd/Tearing.mo
Expand Up @@ -2843,9 +2843,9 @@ algorithm
case(_,_,_,_,_,_,_,_,_,_)
equation
((eqQueue,eq_coll,eqns,vars)) = getpossibleEqn((eqQueueIn,m,me,listArray(ass1),listArray(ass2),mapEqnIncRow));
order = listGet(orderIn,1);
order = listGet(orderIn, 1);
order = eq_coll::order;
orderOut = List.replaceAt(order,0,orderIn);
orderOut = List.replaceAt(order, 1, orderIn);
then (eqQueue,eqns,vars,orderOut,true);
else
then ({},{},{},orderIn,false);
Expand Down
20 changes: 7 additions & 13 deletions Compiler/FrontEnd/Ceval.mo
Expand Up @@ -797,7 +797,7 @@ algorithm
case (cache,env,DAE.CAST(ty = DAE.T_ENUMERATION(path = path, names = n), exp = e), impl, stOpt, msg,_)
equation
(cache, Values.INTEGER(i), stOpt) = ceval(cache, env, e, impl, stOpt,msg,numIter+1);
str = listNth(n, i - 1);
str = listGet(n, i);
path = Absyn.joinPaths(path, Absyn.IDENT(str));
then
(cache, Values.ENUM_LITERAL(path, i), stOpt);
Expand All @@ -824,8 +824,7 @@ algorithm
case (cache,env,DAE.ASUB(exp = e,sub = ((DAE.ICONST(indx))::{})),impl,stOpt,msg,_)
equation
(cache,Values.ARRAY(vals,_),stOpt) = ceval(cache,env, e, impl, stOpt,msg,numIter+1) "asub" ;
indx_1 = indx - 1;
v = listNth(vals, indx_1);
v = listGet(vals, indx);
then
(cache,v,stOpt);

Expand Down Expand Up @@ -1513,8 +1512,7 @@ algorithm
true = Types.dimensionsKnown(tp);
(sizelst as (_ :: _)) = Types.getDimensionSizes(tp);
(cache,Values.INTEGER(dim),st_1) = ceval(cache, env, dimExp, impl, st,msg,numIter+1);
dim_1 = dim - 1;
i = listNth(sizelst, dim_1);
i = listGet(sizelst, dim);
then
(cache,Values.INTEGER(i),st_1);

Expand Down Expand Up @@ -4134,8 +4132,7 @@ algorithm
(cache,Values.REAL(rv2),_) = ceval(cache,env, Expression.makeASUB(s1,{s2}), impl, st,msg,numIter+1);
correctDim = matrixDimension - 1;
zeroList = List.fill(Values.REAL(0.0), correctDim);
correctPlace = row - 1;
listWithElement = List.replaceAt(Values.REAL(rv2), correctPlace, zeroList);
listWithElement = List.replaceAt(Values.REAL(rv2), row, zeroList);
newRow = row + 1;
v = ValuesUtil.makeArray(listWithElement);
(cache,retExp) = cevalBuiltinDiagonal2(cache,env, s1, impl, st, matrixDimension, newRow, {v},msg,numIter);
Expand All @@ -4151,8 +4148,7 @@ algorithm

correctDim = matrixDimension - 1;
zeroList = List.fill(Values.REAL(0.0), correctDim);
correctPlace = row - 1;
listWithElement = List.replaceAt(Values.REAL(rv2), correctPlace, zeroList);
listWithElement = List.replaceAt(Values.REAL(rv2), row, zeroList);
newRow = row + 1;
v = ValuesUtil.makeArray(listWithElement);
appendedList = listAppend(listIN, {v});
Expand All @@ -4166,8 +4162,7 @@ algorithm
(cache,Values.INTEGER(iv2),_) = ceval(cache,env, Expression.makeASUB(s1,{s2}), impl, st,msg,numIter+1);
correctDim = matrixDimension - 1;
zeroList = List.fill(Values.INTEGER(0), correctDim);
correctPlace = row - 1;
listWithElement = List.replaceAt(Values.INTEGER(iv2), correctPlace, zeroList);
listWithElement = List.replaceAt(Values.INTEGER(iv2), row, zeroList);
newRow = row + 1;
v = ValuesUtil.makeArray(listWithElement);
(cache,retExp) = cevalBuiltinDiagonal2(cache,env, s1, impl, st, matrixDimension, newRow, {v},msg,numIter);
Expand All @@ -4183,8 +4178,7 @@ algorithm

correctDim = matrixDimension - 1;
zeroList = List.fill(Values.INTEGER(0), correctDim);
correctPlace = row - 1;
listWithElement = List.replaceAt(Values.INTEGER(iv2), correctPlace, zeroList);
listWithElement = List.replaceAt(Values.INTEGER(iv2), row, zeroList);
newRow = row + 1;
v = ValuesUtil.makeArray(listWithElement);
appendedList = listAppend(listIN, {v});
Expand Down

0 comments on commit 4037b0a

Please sign in to comment.