Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit ff5f8bf

Browse files
hkielOpenModelica-Hudson
authored andcommitted
implement non-failing versions of get and has in BaseHashSet
Belonging to [master]: - #2306
1 parent 63888ab commit ff5f8bf

File tree

3 files changed

+69
-55
lines changed

3 files changed

+69
-55
lines changed

Compiler/FrontEnd/CheckModel.mo

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -336,21 +336,19 @@ public function isCrefListAlgorithmOutput
336336
input DAE.Algorithm inAlgorithm;
337337
input DAE.ElementSource inSource;
338338
input DAE.Expand inCrefExpansionRule;
339-
output Boolean outResult;
339+
output Boolean outResult = false;
340340
protected
341341
HashSet.HashSet ht = HashSet.emptyHashSet();
342342
list<DAE.ComponentRef> algOutCrefs;
343343
algorithm
344344
algOutCrefs := CheckModel.checkAndGetAlgorithmOutputs(inAlgorithm, inSource, inCrefExpansionRule);
345345
ht := List.fold(algOutCrefs, BaseHashSet.add, ht);
346-
try
347-
for cr in crefList loop
348-
BaseHashSet.get(cr, ht);
349-
end for;
350-
outResult := true;
351-
else
352-
outResult := false;
353-
end try;
346+
for cr in crefList loop
347+
if not BaseHashSet.has(cr, ht) then
348+
return;
349+
end if;
350+
end for;
351+
outResult := true;
354352
end isCrefListAlgorithmOutput;
355353

356354
protected function algorithmOutputs "This function finds the the outputs of an algorithm.
@@ -919,9 +917,8 @@ algorithm
919917
DAE.ComponentRef cr;
920918
list<DAE.ComponentRef> rest, crlst;
921919
case ({}, _, _) then iAcc;
922-
case(cr::rest, _, _)
920+
case(cr::rest, _, _) guard BaseHashSet.has(cr, hs)
923921
equation
924-
_ = BaseHashSet.get(cr, hs);
925922
crlst = List.unionEltOnTrue(cr, iAcc, ComponentReference.crefEqual);
926923
then
927924
getcr(rest, hs, crlst);

Compiler/SimCode/SimCodeUtil.mo

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5346,16 +5346,15 @@ protected function createSingleComplexEqnCode3
53465346
output Boolean outB;
53475347
output HashSet.HashSet oht;
53485348
algorithm
5349-
(outB, oht) := matchcontinue(inExp, iht)
5349+
(outB, oht) := match(inExp, iht)
53505350
local
53515351
DAE.ComponentRef cr;
53525352
HashSet.HashSet ht;
53535353
list<DAE.ComponentRef> crefs;
53545354
list<DAE.Exp> expLst;
53555355

5356-
case (DAE.CREF(componentRef=cr), _)
5356+
case (DAE.CREF(componentRef=cr), _) guard BaseHashSet.has(cr, iht)
53575357
equation
5358-
_ = BaseHashSet.get(cr, iht);
53595358
ht = BaseHashSet.delete(cr, iht);
53605359
then
53615360
(true, ht);
@@ -5381,7 +5380,7 @@ algorithm
53815380
then (true, iht);
53825381
else
53835382
(false, iht);
5384-
end matchcontinue;
5383+
end match;
53855384
end createSingleComplexEqnCode3;
53865385

53875386
protected function createSingleArrayEqnCode

Compiler/Util/BaseHashSet.mo

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -122,37 +122,35 @@ public function add
122122
input HashSet hashSet;
123123
output HashSet outHashSet;
124124
algorithm
125-
outHashSet := matchcontinue (entry,hashSet)
125+
outHashSet := match (entry,hashSet)
126126
local
127-
Integer hval,indx,newpos,n,n_1,bsize,indx_1;
128-
tuple<Integer,Integer,array<Option<Key>>> varr_1,varr;
127+
Integer hval,indx,newpos,n,bsize;
128+
tuple<Integer,Integer,array<Option<Key>>> varr;
129129
list<tuple<Key,Integer>> indexes;
130-
array<list<tuple<Key,Integer>>> hashvec_1,hashvec;
130+
array<list<tuple<Key,Integer>>> hashvec;
131131
Key key;
132+
Option<Key> fkey;
132133
FuncsTuple fntpl;
133134
FuncHash hashFunc;
134135
FuncKeyString keystrFunc;
135136
String s;
136137

137138
// Adding when not existing previously
138-
case (key,((hashvec,varr,bsize,_,fntpl as (hashFunc,_,_))))
139+
case (key,((hashvec,varr,bsize,n,fntpl as (hashFunc,_,_))))
139140
equation
140-
failure((_) = get(key, hashSet));
141-
indx = hashFunc(key, bsize);
142-
newpos = valueArrayLength(varr);
143-
varr_1 = valueArrayAdd(varr, key);
144-
indexes = hashvec[indx + 1];
145-
hashvec_1 = arrayUpdate(hashvec, indx + 1, ((key,newpos) :: indexes));
146-
n_1 = valueArrayLength(varr_1);
147-
then ((hashvec_1,varr_1,bsize,n_1,fntpl));
148-
149-
// adding when already present => Updating value
150-
case (key,((hashvec,varr,bsize,n,fntpl)))
151-
equation
152-
(_,indx) = get1(key, hashSet);
153-
//print("adding when present, indx =" );print(intString(indx));print("\n");
154-
varr_1 = valueArraySetnth(varr, indx, key);
155-
then ((hashvec,varr_1,bsize,n,fntpl));
141+
(fkey,indx) = get1(key, hashSet);
142+
if isSome(fkey) then
143+
//print("adding when present, indx =" );print(intString(indx));print("\n");
144+
varr = valueArraySetnth(varr, indx, key);
145+
else
146+
indx = hashFunc(key, bsize);
147+
newpos = valueArrayLength(varr);
148+
varr = valueArrayAdd(varr, key);
149+
indexes = hashvec[indx + 1];
150+
hashvec = arrayUpdate(hashvec, indx + 1, ((key,newpos) :: indexes));
151+
n = valueArrayLength(varr);
152+
end if;
153+
then ((hashvec,varr,bsize,n,fntpl));
156154

157155
case (key,((_,_,bsize,_,(hashFunc,_,keystrFunc))))
158156
equation
@@ -167,7 +165,7 @@ algorithm
167165
print("\n");
168166
then
169167
fail();
170-
end matchcontinue;
168+
end match;
171169
end add;
172170

173171
public function addNoUpdCheck
@@ -225,9 +223,8 @@ algorithm
225223

226224
// Adding when not existing previously
227225
case (_,
228-
((hashvec, varr, bsize, _, fntpl as (hashFunc, _, _))))
226+
((hashvec, varr, bsize, _, fntpl as (hashFunc, _, _)))) guard not has(key, hashSet)
229227
equation
230-
failure(_ = get(key, hashSet));
231228
indx = hashFunc(key, bsize);
232229
newpos = valueArrayLength(varr);
233230
varr_1 = valueArrayAdd(varr, key);
@@ -261,7 +258,7 @@ algorithm
261258
/* adding when already present => Updating value */
262259
case (_,(hashvec,varr,bsize,n,fntpl))
263260
equation
264-
(_,indx) = get1(key, hashSet);
261+
(SOME(_),indx) = get1(key, hashSet);
265262
varr_1 = valueArrayClearnth(varr, indx);
266263
then ((hashvec,varr_1,bsize,n,fntpl));
267264
else
@@ -278,19 +275,19 @@ public function has
278275
input HashSet hashSet;
279276
output Boolean b;
280277
algorithm
281-
b:= matchcontinue(key,hashSet)
278+
b:= match(key,hashSet)
279+
local
280+
Option<Key> oKey;
282281
// empty set containg nothing
283282
case (_,(_,(0,_,_),_,_,_))
284283
then
285284
false;
286-
case(_,_)
285+
else
287286
equation
288-
get(key,hashSet);
287+
(oKey,_) = get1(key,hashSet);
289288
then
290-
true;
291-
else
292-
false;
293-
end matchcontinue;
289+
isSome(oKey);
290+
end match;
294291
end has;
295292

296293
public function hasAll "Returns true if all keys are in the HashSet."
@@ -308,18 +305,18 @@ algorithm
308305
end hasAll;
309306

310307
public function get
311-
"Returns Key from the HashSet. Fails if not present"
308+
"Returns Key from the HashSet. Returns NONE() if not present"
312309
input Key key;
313310
input HashSet hashSet;
314-
output Key okey;
311+
output Option<Key> okey;
315312
algorithm
316313
(okey,_):= get1(key,hashSet);
317314
end get;
318315

319316
protected function get1 "help function to get"
320317
input Key key;
321318
input HashSet hashSet;
322-
output Key okey;
319+
output Option<Key> okey;
323320
output Integer indx;
324321
algorithm
325322
(okey,indx) := match (key,hashSet)
@@ -328,16 +325,17 @@ algorithm
328325
list<tuple<Key,Integer>> indexes;
329326
array<list<tuple<Key,Integer>>> hashvec;
330327
ValueArray varr;
331-
Key k;
328+
Option<Key> k;
332329
FuncEq keyEqual;
333330
FuncHash hashFunc;
331+
Boolean b;
334332

335333
case (_,(hashvec,varr,bsize,_,(hashFunc,keyEqual,_)))
336334
equation
337335
hashindx = hashFunc(key, bsize);
338336
indexes = hashvec[hashindx + 1];
339-
indx = get2(key, indexes, keyEqual);
340-
k = valueArrayNth(varr, indx);
337+
(indx,b) = get2(key, indexes, keyEqual);
338+
k = if b then valueArrayNthT(varr, indx) else NONE();
341339
then
342340
(k,indx);
343341

@@ -350,6 +348,7 @@ protected function get2
350348
input list<tuple<Key,Integer>> keyIndices;
351349
input FuncEq keyEqual;
352350
output Integer index;
351+
output Boolean found = true;
353352
protected
354353
Key key2;
355354
algorithm
@@ -359,7 +358,7 @@ algorithm
359358
return;
360359
end if;
361360
end for;
362-
fail();
361+
found := false;
363362
end get2;
364363

365364
public function printHashSet ""
@@ -537,12 +536,31 @@ algorithm
537536
array<Option<Key>> arr;
538537
case ((n,_,arr),_)
539538
equation
540-
(pos <= n) = true;
539+
(pos <= n) = true; // should be pos<n
541540
SOME(k) = arr[pos + 1];
542541
then
543542
k;
544543
end match;
545544
end valueArrayNth;
546545

546+
protected function valueArrayNthT
547+
"Retrieve the n:th Value from ValueArray, index from 0..n-1."
548+
input ValueArray valueArray;
549+
input Integer pos;
550+
output Option<Key> key;
551+
algorithm
552+
key := match (valueArray,pos)
553+
local
554+
Key k;
555+
Integer n;
556+
array<Option<Key>> arr;
557+
case ((n,_,arr),_)
558+
equation
559+
(pos <= n) = true; // should be pos<n
560+
then
561+
arr[pos + 1];
562+
end match;
563+
end valueArrayNthT;
564+
547565
annotation(__OpenModelica_Interface="util");
548566
end BaseHashSet;

0 commit comments

Comments
 (0)