Skip to content

Commit

Permalink
[NB] Use UnorderedSet to make list unique (#11344)
Browse files Browse the repository at this point in the history
* [NB] Use UnorderedSet to make list unique

`List.unique` is O(N^2) while `UnorderedSet.unique_list` is O(N).
  • Loading branch information
phannebohm committed Oct 6, 2023
1 parent bf2b968 commit 0c3d54d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public
extends Module.resolveSingularitiesInterface;
input list<list<Integer>> marked_eqns_lst;
protected
UnorderedSet<Integer> marked_eqns_set;
list<Integer> marked_eqns;
SlicingStatus status;
Pointer<Equation> constraint, sliced_eqn, diffed_eqn;
Expand All @@ -118,7 +119,14 @@ public
algorithm
if not listEmpty(marked_eqns_lst) then
changed := true;
marked_eqns := List.unique(List.flatten(marked_eqns_lst));
// marked_eqns_lst to flat uniqie list (via UnorderedSet)
marked_eqns_set := UnorderedSet.new(Util.id, intEq, Util.nextPrime(sum(listLength(l) for l in marked_eqns_lst)));
for lst in marked_eqns_lst loop
for e in lst loop
UnorderedSet.add(e, marked_eqns_set);
end for;
end for;
marked_eqns := UnorderedSet.toList(marked_eqns_set);
// --------------------------------------------------------
// 1. BASIC INDEX REDUCTION
// --------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions OMCompiler/Compiler/NBackEnd/Util/NBSlice.mo
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ public

// sort
for i in 1:arrayLength(indices) loop
indices[i] := List.sort(List.unique(indices[i]), intLt);
indices[i] := List.sort(UnorderedSet.unique_list(indices[i], Util.id, intEq), intLt);
end for;
end getDependentCrefIndicesPseudoArray;

Expand Down Expand Up @@ -390,7 +390,7 @@ public

// sort
for i in 1:arrayLength(indices) loop
indices[i] := List.sort(List.unique(indices[i]), intLt);
indices[i] := List.sort(UnorderedSet.unique_list(indices[i], Util.id, intEq), intLt);
end for;
end getDependentCrefIndicesPseudoFor;

Expand Down

0 comments on commit 0c3d54d

Please sign in to comment.