Skip to content

Commit

Permalink
Removed cyclic dependency between Error and Util
Browse files Browse the repository at this point in the history
added missing file
  • Loading branch information
JKRT authored and sjoelund committed Aug 16, 2019
1 parent c59e7a8 commit d226fe5
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 114 deletions.
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/FrontEnd/Ceval.mo
Expand Up @@ -5599,7 +5599,7 @@ protected function makeReductionAllCombinations
output list<list<Values.Value>> valMatrix;
algorithm
valMatrix := match (inValMatrix,rtype)
case (_,Absyn.COMBINE()) then listReverse(Util.allCombinations(inValMatrix,SOME(100000),AbsynUtil.dummyInfo));
case (_,Absyn.COMBINE()) then listReverse(List.allCombinations(inValMatrix,SOME(100000),AbsynUtil.dummyInfo));
case (_,Absyn.THREAD()) then listReverse(List.transposeList(inValMatrix));
end match;
end makeReductionAllCombinations;
Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/FrontEnd/Expression.mo
Expand Up @@ -12299,7 +12299,7 @@ public function rangesToSubscripts
input list<list<DAE.Subscript>> inRangelist;
output list<list<DAE.Subscript>> outSubslst;
algorithm
outSubslst := Util.allCombinations(inRangelist, NONE(), AbsynUtil.dummyInfo);
outSubslst := List.allCombinations(inRangelist, NONE(), AbsynUtil.dummyInfo);
end rangesToSubscripts;

public function expandSubscript
Expand Down
92 changes: 91 additions & 1 deletion OMCompiler/Compiler/Util/List.mo
@@ -1,7 +1,7 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2014, Open Source Modelica Consortium (OSMC),
* Copyright (c) 1998-2019, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
Expand Down Expand Up @@ -101,6 +101,7 @@ import MetaModelica.Dangerous.{listReverseInPlace, arrayGetNoBoundsChecking, arr
import MetaModelica.Dangerous;
import DoubleEndedList;
import GC;
import Error;

public function create<T>
"Creates a list from an element."
Expand Down Expand Up @@ -7416,5 +7417,94 @@ algorithm
outList := listReverseInPlace(outList);
end mapIndices;

public function allCombinations<T>
"{{1,2,3},{4,5},{6}} => {{1,4,6},{1,5,6},{2,4,6},...}.
The output is a 2-dim list with lengths (len1*len2*...*lenN)) and N.

This function screams WARNING I USE COMBINATORIAL EXPLOSION.
So there are flags that limit the size of the set it works on."
input list<list<T>> lst;
input Option<Integer> maxTotalSize;
input SourceInfo info;
output list<list<T>> out;
algorithm
out := matchcontinue (lst,maxTotalSize,info)
local
Integer sz,maxSz;
case (_,SOME(maxSz),_)
algorithm
sz := intMul(listLength(lst), applyAndFold(lst,intMul,listLength,1));
true := (sz <= maxSz);
then allCombinations2(lst);
case (_,NONE(),_) then allCombinations2(lst);
case (_,SOME(_),_)
algorithm
Error.addSourceMessage(Error.COMPILER_NOTIFICATION, {"List.allCombinations failed because the input was too large"}, info);
then fail();
end matchcontinue;
end allCombinations;

protected function allCombinations2<T>
"{{1,2,3},{4,5},{6}} => {{1,4,6},{1,5,6},{2,4,6},...}.
The output is a 2-dim list with lengths (len1*len2*...*lenN)) and N.

This function screams WARNING I USE COMBINATORIAL EXPLOSION."
input list<list<T>> ilst;
output list<list<T>> out;
algorithm
out := match (ilst)
local
list<T> x;
list<list<T>> lst;
case {} then {};
case (x::lst)
algorithm
lst := allCombinations2(lst);
lst := allCombinations3(x, lst, {});
then lst;
end match;
end allCombinations2;

protected function allCombinations3<T>
input list<T> ilst1;
input list<list<T>> ilst2;
input list<list<T>> iacc;
output list<list<T>> out;
algorithm
out := match (ilst1,ilst2,iacc)
local
T x;
list<T> lst1;
list<list<T>> lst2;
list<list<T>> acc;
case ({},_,acc) then listReverse(acc);
case (x::lst1,lst2,acc)
algorithm
acc := allCombinations4(x, lst2, acc);
acc := allCombinations3(lst1, lst2, acc);
then acc;
end match;
end allCombinations3;

protected function allCombinations4<T>
input T x;
input list<list<T>> ilst;
input list<list<T>> iacc;
output list<list<T>> out;
algorithm
out := match (x,ilst,iacc)
local
list<T> l;
list<list<T>> lst;
list<list<T>> acc;
case (_,{},acc) then {x}::acc;
case (_,{l},acc) then (x::l)::acc;
case (_,l::lst,acc)
algorithm
acc := allCombinations4(x, lst, (x::l)::acc);
then acc;
end match;
end allCombinations4;

annotation(__OpenModelica_Interface="util");
end List;
113 changes: 2 additions & 111 deletions OMCompiler/Compiler/Util/Util.mo
@@ -1,7 +1,7 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2014, Open Source Modelica Consortium (OSMC),
* Copyright (c) 1998-2019, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
Expand Down Expand Up @@ -46,20 +46,7 @@ encapsulated package Util
A type variable is exactly what it sounds like, a type bound to a variable.
It is used for higher order functions, i.e. in MetaModelica Compiler (MMC) the possibility to pass a
\"pointer\" to a function into another function. But it can also be used for
generic data types, like in C++ templates.

A type variable in MetaModelica Compiler (MMC) is written as:
replaceable type TyVar subtypeof Any;
For instance,
function listFill
replaceable type TyVar subtypeof Any;
input TyVar in;
input Integer i;
output list<TyVar>
...
end listFill;
the type variable TyVar is here used as a generic type for the function listFill,
which returns a list of n elements of a certain type."
generic data types, like in C++ templates."

public uniontype ReplacePattern
record REPLACEPATTERN
Expand Down Expand Up @@ -88,7 +75,6 @@ protected
import Autoconf;
import ClockIndexes;
import Config;
import Error;
import Flags;
import Global;
import List;
Expand Down Expand Up @@ -1274,101 +1260,6 @@ algorithm
outValue := if valueEq(inKey, k) then v else assoc(inKey, listRest(inList));
end assoc;

public function allCombinations<T>
"{{1,2,3},{4,5},{6}} => {{1,4,6},{1,5,6},{2,4,6},...}.
The output is a 2-dim list with lengths (len1*len2*...*lenN)) and N.

This function screams WARNING I USE COMBINATORIAL EXPLOSION.
So there are flags that limit the size of the set it works on."
input list<list<T>> lst;
input Option<Integer> maxTotalSize;
input SourceInfo info;
output list<list<T>> out;
algorithm
out := matchcontinue (lst,maxTotalSize,info)
local
Integer sz,maxSz;
case (_,SOME(maxSz),_)
equation
sz = intMul(listLength(lst),List.applyAndFold(lst,intMul,listLength,1));
true = (sz <= maxSz);
then allCombinations2(lst);

case (_,NONE(),_) then allCombinations2(lst);

case (_,SOME(_),_)
equation
Error.addSourceMessage(Error.COMPILER_NOTIFICATION, {"Util.allCombinations failed because the input was too large"}, info);
then fail();
end matchcontinue;
end allCombinations;

protected function allCombinations2<T>
"{{1,2,3},{4,5},{6}} => {{1,4,6},{1,5,6},{2,4,6},...}.
The output is a 2-dim list with lengths (len1*len2*...*lenN)) and N.

This function screams WARNING I USE COMBINATORIAL EXPLOSION."
input list<list<T>> ilst;
output list<list<T>> out;
algorithm
out := match (ilst)
local
list<T> x;
list<list<T>> lst;

case {} then {};
case (x::lst)
equation
lst = allCombinations2(lst);
lst = allCombinations3(x, lst, {});
then lst;
end match;
end allCombinations2;

protected function allCombinations3<T>
input list<T> ilst1;
input list<list<T>> ilst2;
input list<list<T>> iacc;
output list<list<T>> out;
algorithm
out := match (ilst1,ilst2,iacc)
local
T x;
list<T> lst1;
list<list<T>> lst2;
list<list<T>> acc;


case ({},_,acc) then listReverse(acc);
case (x::lst1,lst2,acc)
equation
acc = allCombinations4(x, lst2, acc);
acc = allCombinations3(lst1, lst2, acc);
then acc;
end match;
end allCombinations3;

protected function allCombinations4<T>
input T x;
input list<list<T>> ilst;
input list<list<T>> iacc;
output list<list<T>> out;
algorithm
out := match (x,ilst,iacc)
local
list<T> l;
list<list<T>> lst;
list<list<T>> acc;

case (_,{},acc) then {x}::acc;
case (_,{l},acc) then (x::l)::acc;
case (_,l::lst,acc)
equation
acc = allCombinations4(x, lst, (x::l)::acc);
then acc;
end match;
end allCombinations4;

public function boolInt
"Returns 1 if the given boolean is true, otherwise 0."
input Boolean inBoolean;
Expand Down

0 comments on commit d226fe5

Please sign in to comment.