Skip to content

Commit 731a6ce

Browse files
authored
Use difference instead of intersection (#13720)
1 parent 71c22b6 commit 731a6ce

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

OMCompiler/Compiler/BackEnd/EvaluateFunctions.mo

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3165,7 +3165,6 @@ algorithm
31653165
if Flags.isSet(Flags.EVAL_FUNC_DUMP) then
31663166
print("--> the predicted const outputs:\n"+stringDelimitList(List.map(outExps,ExpressionDump.printExpStr),"\n"));
31673167
end if;
3168-
(_,_,_) = List.intersection1OnTrue(outExps,allLHS,Expression.expEqual);
31693168

31703169
//_ = (not listEmpty(constOutExps)) and listEmpty(varOutExps);
31713170
//repl = bcallret3(not predicted, BackendVarTransform.removeReplacements,replIn,varCrefs,NONE(),replIn);

OMCompiler/Compiler/BackEnd/Matching.mo

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import IndexReduction;
6161
import Inline;
6262
import List;
6363
import MetaModelica.Dangerous;
64+
import UnorderedSet;
6465
import Util;
6566
import Sorting;
6667
import System;
@@ -5892,14 +5893,14 @@ algorithm
58925893
//update m
58935894
for e in eqIdxs loop
58945895
row := m[e];
5895-
(_,row,_) := List.intersection1OnTrue(row,varIdxs,intEq);
5896+
row := UnorderedSet.difference_list(row, varIdxs, Util.id, intEq);
58965897
arrayUpdate(m,e,row);
5897-
//update mt
5898-
for varIdx in varIdxs loop
5899-
row := arrayGet(mt,varIdx);
5900-
row := List.deleteMemberOnTrue(e, row, intEq);
5901-
arrayUpdate(mt,varIdx,row);
5902-
end for;
5898+
end for;
5899+
//update mt
5900+
for varIdx in varIdxs loop
5901+
row := mt[varIdx];
5902+
row := UnorderedSet.difference_list(row, eqIdxs, Util.id, intEq);
5903+
arrayUpdate(mt,varIdx,row);
59035904
end for;
59045905
end if;
59055906
idx := idx+1;
@@ -5937,7 +5938,7 @@ algorithm
59375938
//print("remove edges between eq: "+intString(idx)+" and vars "+stringDelimitList(List.map(varIdxs,intString),", ")+"\n");
59385939
//update m
59395940
row := m[idx];
5940-
(_,row,_) := List.intersection1OnTrue(row,varIdxs,intEq);
5941+
row := UnorderedSet.difference_list(row,varIdxs,Util.id,intEq);
59415942
arrayUpdate(m,idx,row);
59425943
//update mt
59435944
for varIdx in varIdxs loop

OMCompiler/Compiler/Util/UnorderedSet.mo

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,38 @@ public
703703
set := fromList(acc, hashFunc, keyEqFunc);
704704
end intersection_list;
705705

706+
function difference_list
707+
"lst1 / lst2, assuming unique lists"
708+
input list<T> inList1;
709+
input list<T> inList2;
710+
input Hash hashFunc;
711+
input KeyEq keyEqFunc;
712+
output list<T> acc = {};
713+
protected
714+
UnorderedSet<T> set2;
715+
list<T> lst1 = inList1, lst2 = inList2;
716+
algorithm
717+
// remove common start, since this seems to be very common
718+
while not (listEmpty(lst1) or listEmpty(lst2)) and keyEqFunc(listHead(lst1), listHead(lst2)) loop
719+
lst1 := listRest(lst1);
720+
lst2 := listRest(lst2);
721+
end while;
722+
723+
// {} - B = {}
724+
// A - {} = A
725+
if listEmpty(lst1) or listEmpty(lst2) then
726+
acc := lst1;
727+
return;
728+
end if;
729+
730+
set2 := fromList(lst2, hashFunc, keyEqFunc);
731+
for k in lst1 loop
732+
if not contains(k, set2) then
733+
acc := k :: acc;
734+
end if;
735+
end for;
736+
end difference_list;
737+
706738
function difference
707739
"set1 / set2"
708740
input UnorderedSet<T> set1;

0 commit comments

Comments
 (0)