From ff5f8bf62d7a1580a6e929b235fc0a30733a8605 Mon Sep 17 00:00:00 2001 From: hkiel Date: Thu, 22 Mar 2018 15:22:15 +0100 Subject: [PATCH] implement non-failing versions of get and has in BaseHashSet Belonging to [master]: - OpenModelica/OMCompiler#2306 --- Compiler/FrontEnd/CheckModel.mo | 19 +++---- Compiler/SimCode/SimCodeUtil.mo | 7 +-- Compiler/Util/BaseHashSet.mo | 98 +++++++++++++++++++-------------- 3 files changed, 69 insertions(+), 55 deletions(-) diff --git a/Compiler/FrontEnd/CheckModel.mo b/Compiler/FrontEnd/CheckModel.mo index 70a253989dc..6cbe5230d3d 100644 --- a/Compiler/FrontEnd/CheckModel.mo +++ b/Compiler/FrontEnd/CheckModel.mo @@ -336,21 +336,19 @@ public function isCrefListAlgorithmOutput input DAE.Algorithm inAlgorithm; input DAE.ElementSource inSource; input DAE.Expand inCrefExpansionRule; - output Boolean outResult; + output Boolean outResult = false; protected HashSet.HashSet ht = HashSet.emptyHashSet(); list algOutCrefs; algorithm algOutCrefs := CheckModel.checkAndGetAlgorithmOutputs(inAlgorithm, inSource, inCrefExpansionRule); ht := List.fold(algOutCrefs, BaseHashSet.add, ht); - try - for cr in crefList loop - BaseHashSet.get(cr, ht); - end for; - outResult := true; - else - outResult := false; - end try; + for cr in crefList loop + if not BaseHashSet.has(cr, ht) then + return; + end if; + end for; + outResult := true; end isCrefListAlgorithmOutput; protected function algorithmOutputs "This function finds the the outputs of an algorithm. @@ -919,9 +917,8 @@ algorithm DAE.ComponentRef cr; list rest, crlst; case ({}, _, _) then iAcc; - case(cr::rest, _, _) + case(cr::rest, _, _) guard BaseHashSet.has(cr, hs) equation - _ = BaseHashSet.get(cr, hs); crlst = List.unionEltOnTrue(cr, iAcc, ComponentReference.crefEqual); then getcr(rest, hs, crlst); diff --git a/Compiler/SimCode/SimCodeUtil.mo b/Compiler/SimCode/SimCodeUtil.mo index 2b830eb11dd..5f08472d73c 100644 --- a/Compiler/SimCode/SimCodeUtil.mo +++ b/Compiler/SimCode/SimCodeUtil.mo @@ -5346,16 +5346,15 @@ protected function createSingleComplexEqnCode3 output Boolean outB; output HashSet.HashSet oht; algorithm - (outB, oht) := matchcontinue(inExp, iht) + (outB, oht) := match(inExp, iht) local DAE.ComponentRef cr; HashSet.HashSet ht; list crefs; list expLst; - case (DAE.CREF(componentRef=cr), _) + case (DAE.CREF(componentRef=cr), _) guard BaseHashSet.has(cr, iht) equation - _ = BaseHashSet.get(cr, iht); ht = BaseHashSet.delete(cr, iht); then (true, ht); @@ -5381,7 +5380,7 @@ algorithm then (true, iht); else (false, iht); - end matchcontinue; + end match; end createSingleComplexEqnCode3; protected function createSingleArrayEqnCode diff --git a/Compiler/Util/BaseHashSet.mo b/Compiler/Util/BaseHashSet.mo index 7b1355d0ca5..b3f43890cda 100644 --- a/Compiler/Util/BaseHashSet.mo +++ b/Compiler/Util/BaseHashSet.mo @@ -122,37 +122,35 @@ public function add input HashSet hashSet; output HashSet outHashSet; algorithm - outHashSet := matchcontinue (entry,hashSet) + outHashSet := match (entry,hashSet) local - Integer hval,indx,newpos,n,n_1,bsize,indx_1; - tuple>> varr_1,varr; + Integer hval,indx,newpos,n,bsize; + tuple>> varr; list> indexes; - array>> hashvec_1,hashvec; + array>> hashvec; Key key; + Option fkey; FuncsTuple fntpl; FuncHash hashFunc; FuncKeyString keystrFunc; String s; // Adding when not existing previously - case (key,((hashvec,varr,bsize,_,fntpl as (hashFunc,_,_)))) + case (key,((hashvec,varr,bsize,n,fntpl as (hashFunc,_,_)))) equation - failure((_) = get(key, hashSet)); - indx = hashFunc(key, bsize); - newpos = valueArrayLength(varr); - varr_1 = valueArrayAdd(varr, key); - indexes = hashvec[indx + 1]; - hashvec_1 = arrayUpdate(hashvec, indx + 1, ((key,newpos) :: indexes)); - n_1 = valueArrayLength(varr_1); - then ((hashvec_1,varr_1,bsize,n_1,fntpl)); - - // adding when already present => Updating value - case (key,((hashvec,varr,bsize,n,fntpl))) - equation - (_,indx) = get1(key, hashSet); - //print("adding when present, indx =" );print(intString(indx));print("\n"); - varr_1 = valueArraySetnth(varr, indx, key); - then ((hashvec,varr_1,bsize,n,fntpl)); + (fkey,indx) = get1(key, hashSet); + if isSome(fkey) then + //print("adding when present, indx =" );print(intString(indx));print("\n"); + varr = valueArraySetnth(varr, indx, key); + else + indx = hashFunc(key, bsize); + newpos = valueArrayLength(varr); + varr = valueArrayAdd(varr, key); + indexes = hashvec[indx + 1]; + hashvec = arrayUpdate(hashvec, indx + 1, ((key,newpos) :: indexes)); + n = valueArrayLength(varr); + end if; + then ((hashvec,varr,bsize,n,fntpl)); case (key,((_,_,bsize,_,(hashFunc,_,keystrFunc)))) equation @@ -167,7 +165,7 @@ algorithm print("\n"); then fail(); - end matchcontinue; + end match; end add; public function addNoUpdCheck @@ -225,9 +223,8 @@ algorithm // Adding when not existing previously case (_, - ((hashvec, varr, bsize, _, fntpl as (hashFunc, _, _)))) + ((hashvec, varr, bsize, _, fntpl as (hashFunc, _, _)))) guard not has(key, hashSet) equation - failure(_ = get(key, hashSet)); indx = hashFunc(key, bsize); newpos = valueArrayLength(varr); varr_1 = valueArrayAdd(varr, key); @@ -261,7 +258,7 @@ algorithm /* adding when already present => Updating value */ case (_,(hashvec,varr,bsize,n,fntpl)) equation - (_,indx) = get1(key, hashSet); + (SOME(_),indx) = get1(key, hashSet); varr_1 = valueArrayClearnth(varr, indx); then ((hashvec,varr_1,bsize,n,fntpl)); else @@ -278,19 +275,19 @@ public function has input HashSet hashSet; output Boolean b; algorithm - b:= matchcontinue(key,hashSet) + b:= match(key,hashSet) + local + Option oKey; // empty set containg nothing case (_,(_,(0,_,_),_,_,_)) then false; - case(_,_) + else equation - get(key,hashSet); + (oKey,_) = get1(key,hashSet); then - true; - else - false; - end matchcontinue; + isSome(oKey); + end match; end has; public function hasAll "Returns true if all keys are in the HashSet." @@ -308,10 +305,10 @@ algorithm end hasAll; public function get -"Returns Key from the HashSet. Fails if not present" +"Returns Key from the HashSet. Returns NONE() if not present" input Key key; input HashSet hashSet; - output Key okey; + output Option okey; algorithm (okey,_):= get1(key,hashSet); end get; @@ -319,7 +316,7 @@ end get; protected function get1 "help function to get" input Key key; input HashSet hashSet; - output Key okey; + output Option okey; output Integer indx; algorithm (okey,indx) := match (key,hashSet) @@ -328,16 +325,17 @@ algorithm list> indexes; array>> hashvec; ValueArray varr; - Key k; + Option k; FuncEq keyEqual; FuncHash hashFunc; + Boolean b; case (_,(hashvec,varr,bsize,_,(hashFunc,keyEqual,_))) equation hashindx = hashFunc(key, bsize); indexes = hashvec[hashindx + 1]; - indx = get2(key, indexes, keyEqual); - k = valueArrayNth(varr, indx); + (indx,b) = get2(key, indexes, keyEqual); + k = if b then valueArrayNthT(varr, indx) else NONE(); then (k,indx); @@ -350,6 +348,7 @@ protected function get2 input list> keyIndices; input FuncEq keyEqual; output Integer index; + output Boolean found = true; protected Key key2; algorithm @@ -359,7 +358,7 @@ algorithm return; end if; end for; - fail(); + found := false; end get2; public function printHashSet "" @@ -537,12 +536,31 @@ algorithm array> arr; case ((n,_,arr),_) equation - (pos <= n) = true; + (pos <= n) = true; // should be pos key; +algorithm + key := match (valueArray,pos) + local + Key k; + Integer n; + array> arr; + case ((n,_,arr),_) + equation + (pos <= n) = true; // should be pos