@@ -122,37 +122,35 @@ public function add
122122 input HashSet hashSet;
123123 output HashSet outHashSet;
124124algorithm
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 ;
171169end add;
172170
173171public 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;
280277algorithm
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;
294291end has;
295292
296293public function hasAll "Returns true if all keys are in the HashSet."
@@ -308,18 +305,18 @@ algorithm
308305end hasAll;
309306
310307public 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;
315312algorithm
316313 (okey,_):= get1(key,hashSet);
317314end get;
318315
319316protected 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;
324321algorithm
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 ;
353352protected
354353 Key key2;
355354algorithm
@@ -359,7 +358,7 @@ algorithm
359358 return ;
360359 end if ;
361360 end for ;
362- fail() ;
361+ found : = false ;
363362end get2;
364363
365364public 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;
545544end 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+
547565annotation(__OpenModelica_Interface= "util" );
548566end BaseHashSet ;
0 commit comments