/
HashTableStringToPath.mo
626 lines (573 loc) · 17.1 KB
/
HashTableStringToPath.mo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
package HashTableStringToPath "
This file is an extension to OpenModelica.
Copyright (c) 2007 MathCore Engineering AB
All rights reserved.
This package contains implementation of hashtables, instead of the binary trees used in OpenModelica.
It is a generic implementation that can be used for any Key, Value datatypes. But the code must be copied
since MetaModelica is not powerful enough to handle general generics.
"
/* Below is the instance specific code. For each hashtable the user must define:
Key - The key used to uniquely define elements in a hashtable
Value - The data to associate with each key
hashFunc - A function that maps a key to a positive integer.
keyEqual - A comparison function between two keys, returns true if equal.
*/
/* HashTable instance specific code */
public import Absyn;
protected import System;
protected import Util;
public
type Key = String;
type Value = Absyn.Path;
protected function hashFunc
input Key str;
output Integer res;
algorithm
res := System.hash(str);
end hashFunc;
protected function keyEqual
input Key key1;
input Key key2;
output Boolean res;
algorithm
res := key1 ==& key2;
end keyEqual;
public function dumpHashTable ""
input HashTable t;
algorithm
print("HashTable:\n");
print(Util.stringDelimitList(Util.listMap(hashTableList(t),dumpTuple),"\n"));
print("\n");
end dumpHashTable;
protected function dumpTuple
input tuple<Key,Value> tpl;
output String str;
algorithm
str := matchcontinue(tpl)
local
String id;
Absyn.Path p;
case((id,p)) equation
str = "{" +& id +& "," +& Absyn.pathString(p) +& "}";
then str;
end matchcontinue;
end dumpTuple;
/* end of HashTable instance specific code */
/* Generic hashtable code below!! */
public
uniontype HashTable
record HASHTABLE
list<tuple<Key,Integer>>[:] hashTable " hashtable to translate Key to array indx" ;
ValueArray valueArr "Array of values" ;
Integer bucketSize "bucket size" ;
Integer numberOfEntries "number of entries in hashtable" ;
end HASHTABLE;
end HashTable;
uniontype ValueArray "array of values are expandable, to amortize the cost of adding elements in a more
efficient manner"
record VALUE_ARRAY
Integer numberOfElements "number of elements in hashtable" ;
Integer arrSize "size of crefArray" ;
Option<tuple<Key,Value>>[:] valueArray "array of values";
end VALUE_ARRAY;
end ValueArray;
public function cloneHashTable "
Author BZ 2008-06
Make a stand-alone-copy of hashtable.
"
input HashTable inHash;
output HashTable outHash;
algorithm outHash := matchcontinue(inHash)
local
list<tuple<Key,Integer>>[:] arg1,arg1_2;
Integer arg3,arg4,arg3_2,arg4_2,arg21,arg21_2,arg22,arg22_2;
Option<tuple<Key,Value>>[:] arg23,arg23_2;
case(HASHTABLE(arg1,VALUE_ARRAY(arg21,arg22,arg23),arg3,arg4))
equation
arg1_2 = arrayCopy(arg1);
arg21_2 = arg21;
arg22_2 = arg22;
arg23_2 = arrayCopy(arg23);
arg3_2 = arg3;
arg4_2 = arg4;
then
HASHTABLE(arg1_2,VALUE_ARRAY(arg21_2,arg22_2,arg23_2),arg3_2,arg4_2);
end matchcontinue;
end cloneHashTable;
public function nullHashTable "
author: PA
Returns an empty HashTable.
Using the bucketsize 100 and array size 10.
"
output HashTable hashTable;
list<tuple<Key,Integer>>[:] arr;
list<Option<tuple<Key,Value>>> lst;
Option<tuple<Key,Value>>[:] emptyarr;
algorithm
arr := fill({}, 0);
emptyarr := listArray({});
hashTable := HASHTABLE(arr,VALUE_ARRAY(0,0,emptyarr),0,0);
end nullHashTable;
public function emptyHashTable "
author: PA
Returns an empty HashTable.
Using the bucketsize 100 and array size 10.
"
output HashTable hashTable;
list<tuple<Key,Integer>>[:] arr;
list<Option<tuple<Key,Value>>> lst;
Option<tuple<Key,Value>>[:] emptyarr;
algorithm
arr := fill({}, 1000);
// lst := Util.listFill(NONE, 100);
// emptyarr := listArray(lst);
emptyarr := fill(NONE(), 100);
hashTable := HASHTABLE(arr,VALUE_ARRAY(0,100,emptyarr),1000,0);
end emptyHashTable;
public function isEmpty "Returns true if hashtable is empty"
input HashTable hashTable;
output Boolean res;
algorithm
res := matchcontinue(hashTable)
case(HASHTABLE(_,_,_,0)) then true;
case(_) then false;
end matchcontinue;
end isEmpty;
public function add "
author: PA
Add a Key-Value tuple to hashtable.
If the Key-Value tuple already exists, the function updates the Value.
"
input tuple<Key,Value> entry;
input HashTable hashTable;
output HashTable outHahsTable;
algorithm
outVariables:=
matchcontinue (entry,hashTable)
local
Integer hval,indx,newpos,n,n_1,bsize,indx_1;
ValueArray varr_1,varr;
list<tuple<Key,Integer>> indexes;
list<tuple<Key,Integer>>[:] hashvec_1,hashvec;
String name_str;
tuple<Key,Value> v,newv;
Key key;
Value value;
/* Adding when not existing previously */
case ((v as (key,value)),(hashTable as HASHTABLE(hashvec,varr,bsize,n)))
equation
failure((_) = get(key, hashTable));
hval = hashFunc(key);
indx = intMod(hval, bsize);
newpos = valueArrayLength(varr);
varr_1 = valueArrayAdd(varr, v);
indexes = hashvec[indx + 1];
hashvec_1 = arrayUpdate(hashvec, indx + 1, ((key,newpos) :: indexes));
n_1 = valueArrayLength(varr_1);
then HASHTABLE(hashvec_1,varr_1,bsize,n_1);
/* adding when already present => Updating value */
case ((newv as (key,value)),(hashTable as HASHTABLE(hashvec,varr,bsize,n)))
equation
(_,indx) = get1(key, hashTable);
//print("adding when present, indx =" );print(intString(indx));print("\n");
indx_1 = indx - 1;
varr_1 = valueArraySetnth(varr, indx, newv);
then HASHTABLE(hashvec,varr_1,bsize,n);
case (_,_)
equation
print("-HashTable.add failed\n");
then
fail();
end matchcontinue;
end add;
public function anyKeyInHashTable "Returns true if any of the keys are present in the hashtable. Stops and returns true upon first occurence"
input list<Key> keys;
input HashTable ht;
output Boolean res;
algorithm
res := matchcontinue(keys,ht)
local Key key;
case({},ht) then false;
case(key::keys,ht) equation
_ = get(key,ht);
then true;
case(_::keys,ht) then anyKeyInHashTable(keys,ht);
end matchcontinue;
end anyKeyInHashTable;
public function addListNoUpd "adds several keys with the same value, using addNuUpdCheck. Can be used to use HashTable as a Set"
input list<Key> keys;
input Value v;
input HashTable ht;
output HashTable outHt;
algorithm
ht := matchcontinue(keys,v,ht)
local Key key;
case ({},v,ht) then ht;
case(key::keys,v,ht) equation
ht = addNoUpdCheck((key,v),ht);
ht = addListNoUpd(keys,v,ht);
then ht;
end matchcontinue;
end addListNoUpd;
public function addNoUpdCheck "
author: PA
Add a Key-Value tuple to hashtable.
If the Key-Value tuple already exists, the function updates the Value.
"
input tuple<Key,Value> entry;
input HashTable hashTable;
output HashTable outHahsTable;
algorithm
outVariables:=
matchcontinue (entry,hashTable)
local
Integer hval,indx,newpos,n,n_1,bsize,indx_1;
ValueArray varr_1,varr;
list<tuple<Key,Integer>> indexes;
list<tuple<Key,Integer>>[:] hashvec_1,hashvec;
String name_str;
tuple<Key,Value> v,newv;
Key key;
Value value;
/* Adding when not existing previously */
case ((v as (key,value)),(hashTable as HASHTABLE(hashvec,varr,bsize,n)))
equation
hval = hashFunc(key);
indx = intMod(hval, bsize);
newpos = valueArrayLength(varr);
varr_1 = valueArrayAdd(varr, v);
indexes = hashvec[indx + 1];
hashvec_1 = arrayUpdate(hashvec, indx + 1, ((key,newpos) :: indexes));
n_1 = valueArrayLength(varr_1);
then HASHTABLE(hashvec_1,varr_1,bsize,n_1);
case (_,_)
equation
print("-HashTable.addNoUpdCheck failed\n");
then
fail();
end matchcontinue;
end addNoUpdCheck;
public function delete "
author: PA
delete the Value associatied with Key from the HashTable.
Note: This function does not delete from the index table, only from the ValueArray.
This means that a lot of deletions will not make the HashTable more compact, it will still contain
a lot of incices information.
"
input Key key;
input HashTable hashTable;
output HashTable outHahsTable;
algorithm
outVariables:=
matchcontinue (key,hashTable)
local
Integer hval,indx,newpos,n,n_1,bsize,indx_1;
ValueArray varr_1,varr;
list<tuple<Key,Integer>> indexes;
list<tuple<Key,Integer>>[:] hashvec_1,hashvec;
String name_str;
tuple<Key,Value> v,newv;
Key key;
Value value;
/* adding when already present => Updating value */
case (key,(hashTable as HASHTABLE(hashvec,varr,bsize,n)))
equation
(_,indx) = get1(key, hashTable);
indx_1 = indx - 1;
varr_1 = valueArrayClearnth(varr, indx);
then HASHTABLE(hashvec,varr_1,bsize,n);
case (_,hashTable)
equation
print("-HashTable.delete failed\n");
print("content:"); dumpHashTable(hashTable);
then
fail();
end matchcontinue;
end delete;
public function get "
author: PA
Returns a Value given a Key and a HashTable.
"
input Key key;
input HashTable hashTable;
output Value value;
algorithm
(value,_):= get1(key,hashTable);
end get;
protected function get1 "help function to get"
input Key key;
input HashTable hashTable;
output Value value;
output Integer indx;
algorithm
(value,indx):=
matchcontinue (key,hashTable)
local
Integer hval,hashindx,indx,indx_1,bsize,n;
list<tuple<Key,Integer>> indexes;
Value v;
list<tuple<Key,Integer>>[:] hashvec;
ValueArray varr;
Key k;
case (key,(hashTable as HASHTABLE(hashvec,varr,bsize,n)))
equation
hval = hashFunc(key);
hashindx = intMod(hval, bsize);
indexes = hashvec[hashindx + 1];
indx = get2(key, indexes);
(k, v) = valueArrayNth(varr, indx);
true = keyEqual(k, key);
then
(v,indx);
end matchcontinue;
end get1;
protected function get2 "
author: PA
Helper function to get
"
input Key key;
input list<tuple<Key,Integer>> keyIndices;
output Integer index;
algorithm
index :=
matchcontinue (key,keyIndices)
local
Key key2;
Value res;
list<tuple<Key,Integer>> xs;
case (key,((key2,index) :: _))
equation
true = keyEqual(key, key2);
then
index;
case (key,(_ :: xs))
equation
index = get2(key, xs);
then
index;
end matchcontinue;
end get2;
public function hashTableValueList "return the Value entries as a list of Values"
input HashTable hashTable;
output list<Value> valLst;
algorithm
valLst := Util.listMap(hashTableList(hashTable),Util.tuple22);
end hashTableValueList;
public function hashTableKeyList "return the Key entries as a list of Keys"
input HashTable hashTable;
output list<Key> valLst;
algorithm
valLst := Util.listMap(hashTableList(hashTable),Util.tuple21);
end hashTableKeyList;
public function hashTableList "returns the entries in the hashTable as a list of tuple<Key,Value>"
input HashTable hashTable;
output list<tuple<Key,Value>> tplLst;
algorithm
tplLst := matchcontinue(hashTable)
local ValueArray varr;
case(HASHTABLE(valueArr = varr)) equation
tplLst = valueArrayList(varr);
then tplLst;
end matchcontinue;
end hashTableList;
public function valueArrayList "
author: PA
Transforms a ValueArray to a tuple<Key,Value> list
"
input ValueArray valueArray;
output list<tuple<Key,Value>> tplLst;
algorithm
tplLst :=
matchcontinue (valueArray)
local
Option<tuple<Key,Value>>[:] arr;
tuple<Key,Value> elt;
Integer lastpos,n,size;
list<tuple<Key,Value>> lst;
case (VALUE_ARRAY(numberOfElements = 0,valueArray = arr)) then {};
case (VALUE_ARRAY(numberOfElements = 1,valueArray = arr))
equation
SOME(elt) = arr[0 + 1];
then
{elt};
case (VALUE_ARRAY(numberOfElements = n,arrSize = size,valueArray = arr))
equation
lastpos = n - 1;
lst = valueArrayList2(arr, 0, lastpos);
then
lst;
end matchcontinue;
end valueArrayList;
protected function valueArrayList2 "Helper function to valueArrayList"
input Option<tuple<Key,Value>>[:] inVarOptionArray1;
input Integer inInteger2;
input Integer inInteger3;
output list<tuple<Key,Value>> outVarLst;
algorithm
outVarLst:=
matchcontinue (inVarOptionArray1,inInteger2,inInteger3)
local
tuple<Key,Value> v;
Option<tuple<Key,Value>>[:] arr;
Integer pos,lastpos,pos_1;
list<tuple<Key,Value>> res;
case (arr,pos,lastpos)
equation
(pos == lastpos) = true;
SOME(v) = arr[pos + 1];
then
{v};
case (arr,pos,lastpos)
equation
pos_1 = pos + 1;
SOME(v) = arr[pos + 1];
res = valueArrayList2(arr, pos_1, lastpos);
then
(v :: res);
case (arr,pos,lastpos)
equation
pos_1 = pos + 1;
NONE = arr[pos + 1];
res = valueArrayList2(arr, pos_1, lastpos);
then
(res);
end matchcontinue;
end valueArrayList2;
public function valueArrayLength "
author: PA
Returns the number of elements in the ValueArray
"
input ValueArray valueArray;
output Integer size;
algorithm
size := matchcontinue (valueArray)
case (VALUE_ARRAY(numberOfElements = size)) then size;
end matchcontinue;
end valueArrayLength;
public function valueArrayAdd "function: valueArrayAdd
author: PA
Adds an entry last to the ValueArray, increasing array size
if no space left by factor 1.4
"
input ValueArray valueArray;
input tuple<Key,Value> entry;
output ValueArray outValueArray;
algorithm
outValueArray:=
matchcontinue (valueArray,entry)
local
Integer n_1,n,size,expandsize,expandsize_1,newsize;
Option<tuple<Key,Value>>[:] arr_1,arr,arr_2;
Real rsize,rexpandsize;
case (VALUE_ARRAY(numberOfElements = n,arrSize = size,valueArray = arr),entry)
equation
(n < size) = true "Have space to add array elt." ;
n_1 = n + 1;
arr_1 = arrayUpdate(arr, n + 1, SOME(entry));
then
VALUE_ARRAY(n_1,size,arr_1);
case (VALUE_ARRAY(numberOfElements = n,arrSize = size,valueArray = arr),entry)
equation
(n < size) = false "Do NOT have splace to add array elt. Expand with factor 1.4" ;
rsize = intReal(size);
rexpandsize = rsize*.0.4;
expandsize = realInt(rexpandsize);
expandsize_1 = intMax(expandsize, 1);
newsize = expandsize_1 + size;
arr_1 = Util.arrayExpand(expandsize_1, arr, NONE);
n_1 = n + 1;
arr_2 = arrayUpdate(arr_1, n + 1, SOME(entry));
then
VALUE_ARRAY(n_1,newsize,arr_2);
case (_,_)
equation
print("-HashTable.valueArrayAdd failed\n");
then
fail();
end matchcontinue;
end valueArrayAdd;
public function valueArraySetnth "function: valueArraySetnth
author: PA
Set the n:th variable in the ValueArray to value.
"
input ValueArray valueArray;
input Integer pos;
input tuple<Key,Value> entry;
output ValueArray outValueArray;
algorithm
outValueArray:=
matchcontinue (valueArray,pos,entry)
local
Option<tuple<Key,Value>>[:] arr_1,arr;
Integer n,size,pos;
case (VALUE_ARRAY(n,size,arr),pos,entry)
equation
(pos < size) = true;
arr_1 = arrayUpdate(arr, pos + 1, SOME(entry));
then
VALUE_ARRAY(n,size,arr_1);
case (_,_,_)
equation
print("-HashTable.valueArraySetnth failed\n");
then
fail();
end matchcontinue;
end valueArraySetnth;
public function valueArrayClearnth "
author: PA
Clears the n:th variable in the ValueArray (set to NONE).
"
input ValueArray valueArray;
input Integer pos;
output ValueArray outValueArray;
algorithm
outValueArray:=
matchcontinue (valueArray,pos)
local
Option<tuple<Key,Value>>[:] arr_1,arr;
Integer n,size,pos;
case (VALUE_ARRAY(n,size,arr),pos)
equation
(pos < size) = true;
arr_1 = arrayUpdate(arr, pos + 1, NONE);
then
VALUE_ARRAY(n,size,arr_1);
case (_,_)
equation
print("-HashTable.valueArrayClearnth failed\n");
then
fail();
end matchcontinue;
end valueArrayClearnth;
public function valueArrayNth "function: valueArrayNth
author: PA
Retrieve the n:th Vale from ValueArray, index from 0..n-1.
"
input ValueArray valueArray;
input Integer pos;
output Key key;
output Value value;
algorithm
(key, value) :=
matchcontinue (valueArray,pos)
local
Key k;
Value v;
Integer n,pos,len;
Option<tuple<Key,Value>>[:] arr;
String ps,lens,ns;
case (VALUE_ARRAY(numberOfElements = n,valueArray = arr),pos)
equation
(pos < n) = true;
SOME((k,v)) = arr[pos + 1];
then
(k, v);
case (VALUE_ARRAY(numberOfElements = n,valueArray = arr),pos)
equation
(pos < n) = true;
NONE = arr[pos + 1];
then
fail();
end matchcontinue;
end valueArrayNth;
end HashTableStringToPath;