-
Notifications
You must be signed in to change notification settings - Fork 2
/
Dictionary.mod
580 lines (443 loc) · 16.7 KB
/
Dictionary.mod
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
(*!m2pim*) (* Copyright (c) 2017 Modula-2 Software Foundation. *)
IMPLEMENTATION MODULE Dictionary;
(* Key/Value Dictionary *)
IMPORT String;
FROM SYSTEM IMPORT TSIZE;
FROM Storage IMPORT ALLOCATE, DEALLOCATE;
FROM String IMPORT StringT; (* alias for String.String *)
TYPE Node = POINTER TO NodeDescriptor;
TYPE NodeDescriptor = RECORD
level : CARDINAL;
key : Key;
value : Value;
left,
right : Node
END; (* NodeDescriptor *)
TYPE Cache = RECORD
key : Key;
value : Value
END; (* Cache *)
TYPE Tree = RECORD;
entries : CARDINAL;
root : Node;
lastSearch : Cache;
lastStatus : Status
END; (* Tree *)
VAR
dictionary : Tree;
prevNode,
candidate,
bottom : Node;
(* Introspection *)
(* ---------------------------------------------------------------------------
* function Dictionary.count()
* ---------------------------------------------------------------------------
* Returns the number of key/value pairs in the global dictionary.
* Does not set dictionary status.
* ------------------------------------------------------------------------ *)
PROCEDURE count ( ) : CARDINAL;
BEGIN
RETURN dictionary.entries
END count;
(* ---------------------------------------------------------------------------
* function Dictionary.status()
* ---------------------------------------------------------------------------
* Returns the status of the last operation on the global dictionary.
* Does not set dictionary status.
* ------------------------------------------------------------------------ *)
PROCEDURE status ( ) : Status;
BEGIN
RETURN dictionary.lastStatus
END status;
(* Lookup Operations *)
(* ---------------------------------------------------------------------------
* function Dictionary.isPresent(key)
* ---------------------------------------------------------------------------
* Returns TRUE if key is present in the global dictionary, else FALSE.
* Fails and returns NIL if key is NIL. Sets dictionary status.
* ------------------------------------------------------------------------ *)
PROCEDURE isPresent ( key : Key ) : BOOLEAN;
VAR
value : Value;
BEGIN
(* bail out if key is NIL *)
IF key = NilKey THEN
dictionary.lastStatus := NilNotPermitted;
RETURN FALSE
END; (* IF *)
(* return TRUE if key matches last searched key *)
IF (dictionary.lastSearch.key # NilKey) AND
(key = dictionary.lastSearch.key) THEN
dictionary.lastStatus := Success;
RETURN TRUE
END; (* IF *)
(* search key *)
value := lookup(dictionary.root, key, dictionary.lastStatus);
(* update cache if entry found *)
IF value # NilValue THEN
dictionary.lastSearch.key := key;
dictionary.lastSearch.value := value;
END; (* IF *)
RETURN (dictionary.lastStatus = Success)
END isPresent;
(* ---------------------------------------------------------------------------
* function Dictionary.valueForKey(key)
* ---------------------------------------------------------------------------
* Returns the value stored for key in the global dictionary, or NIL if no key
* is present in the dictionary. Fails if key is NIL. Sets dictionary status.
* ------------------------------------------------------------------------ *)
PROCEDURE valueForKey ( key : Key ) : Value;
VAR
value : Value;
BEGIN
(* bail out if key is NIL *)
IF key = NilKey THEN
dictionary.lastStatus := NilNotPermitted;
RETURN NilKey
END; (* IF *)
(* return cached value if key matches last searched key *)
IF (dictionary.lastSearch.key # NilKey) AND
(key = dictionary.lastSearch.key) THEN
dictionary.lastStatus := Success;
RETURN dictionary.lastSearch.value
END; (* IF *)
(* search key *)
value := lookup(dictionary.root, key, dictionary.lastStatus);
(* update cache if entry found *)
IF value # NilKey THEN
dictionary.lastSearch.key := key;
dictionary.lastSearch.value := value
END; (* IF *)
RETURN value
END valueForKey;
(* Insert Operations *)
(* ---------------------------------------------------------------------------
* procedure Dictionary.StoreValueForKey(key, value)
* ---------------------------------------------------------------------------
* Stores value for key in the global dictionary. Fails if key or value or
* both are NIL. Sets dictionary status.
* ------------------------------------------------------------------------ *)
PROCEDURE StoreValueForKey ( key : Key; value : Value );
VAR
newRoot : Node;
BEGIN
(* bail out if key or value or both are NIL *)
IF (key = NilKey) OR (value = NilValue) THEN
dictionary.lastStatus := NilNotPermitted;
RETURN
END; (* IF *)
(* insert new entry *)
newRoot := insert(dictionary.root, key, value, dictionary.lastStatus);
(* replace dictionary root, update counter *)
IF dictionary.lastStatus = Success THEN
dictionary.root := newRoot;
dictionary.entries := dictionary.entries + 1
END (* IF *)
END StoreValueForKey;
(* ---------------------------------------------------------------------------
* procedure Dictionary.StoreArrayForKey(key, array)
* ---------------------------------------------------------------------------
* Obtains an interned string for array, then stores the string as value for
* key in the global dictionary. Fails if key is NIL or if array produces a
* NIL string. Sets dictionary status.
* ------------------------------------------------------------------------ *)
PROCEDURE StoreArrayForKey
( key : Key; VAR (* CONST *) array : ARRAY OF CHAR );
VAR
value : Value;
BEGIN
(* bail out if key is NIL *)
IF key = NilKey THEN
dictionary.lastStatus := NilNotPermitted;
RETURN
END; (* IF *)
(* check key before getting interned string for value *)
IF lookup(dictionary.root, key, dictionary.lastStatus) = NilValue THEN
value := String.forArray(array);
IF value = NilValue THEN
dictionary.lastStatus := NilNotPermitted;
RETURN
ELSE (* all clear *)
StoreValueForKey(value, key)
END (* IF *)
END (* IF *)
END StoreArrayForKey;
(* Removal Operations *)
(* ---------------------------------------------------------------------------
* procedure Dictionary.RemoveKey(key)
* ---------------------------------------------------------------------------
* Removes key and its value from the global dictionary. Fails if key is NIL
* or if key is not present in the dictionary. Sets dictionary status.
* ------------------------------------------------------------------------ *)
PROCEDURE RemoveKey ( key : Key );
VAR
newRoot : Node;
BEGIN
(* bail out if key is NIL *)
IF key = NilKey THEN
dictionary.lastStatus := NilNotPermitted;
RETURN
END; (* IF *)
(* remove *)
newRoot := remove(dictionary.root, key, dictionary.lastStatus);
IF dictionary.lastStatus = Success THEN
dictionary.root := newRoot;
dictionary.entries := dictionary.entries - 1;
(* clear cache if removed key is in cache *)
IF key = dictionary.lastSearch.key THEN
dictionary.lastSearch.key := NilKey;
dictionary.lastSearch.value := NilValue
END (* IF *)
END (* IF *)
END RemoveKey;
(* Iteration *)
(* ---------------------------------------------------------------------------
* procedure Dictionary.WithKeyValuePairsDo(p)
* ---------------------------------------------------------------------------
* Iterates over all key/value pairs in the global dictionary in key order
* and calls calls visitor procedure p for each pair, passing key and value.
* Keys are ordered in ASCII collation order. Sets dictionary status.
* ------------------------------------------------------------------------ *)
PROCEDURE WithKeyValuePairsDo ( p : VisitorProc );
BEGIN
IF p = NILPROC THEN
dictionary.lastStatus := NilNotPermitted;
RETURN
END; (* IF *)
Traverse(dictionary.root, p);
dictionary.lastStatus := Success
END WithKeyValuePairsDo;
(* ************************************************************************ *
* Private Operations *
* ************************************************************************ *)
(* ---------------------------------------------------------------------------
* AA Tree Implementation
* ---------------------------------------------------------------------------
* Reference documents:
* (1) AA Trees -- http://user.it.uu.se/~arnea/ps/simp.pdf
* (2) Sentinel Search -- http://user.it.uu.se/~arnea/ps/searchproc.pdf
* ------------------------------------------------------------------------ *)
(* ---------------------------------------------------------------------------
* private function lookup(node, key, status)
* ---------------------------------------------------------------------------
* Looks up key in the subtree whose root is thisNode. Returns its value if
* key is found, otherwise returns NIL. Passes status back in status.
* ------------------------------------------------------------------------ *)
PROCEDURE lookup ( thisNode : Node; key : Key; VAR status : Status ) : Value;
VAR
searchKey : String.Comparison;
BEGIN
(* set sentinel's key to search key *)
bottom^.key := key;
(* compare search key and key of current node *)
searchKey := String.comparison(key, thisNode^.key);
(* search until key is found or bottom of tree is reached *)
WHILE searchKey # String.Equal DO
(* move down left if key is less than key of current node *)
IF searchKey = String.Less THEN (* key < thisNode^.key *)
thisNode := thisNode^.left
(* move down right if key is greater than key of current node *)
ELSE (* key > thisNode^.key *)
thisNode := thisNode^.right
END; (* IF *)
(* compare search key and key of current node *)
searchKey := String.comparison(key, thisNode^.key)
END; (* WHILE *)
(* restore sentinel's key *)
bottom^.key := NilKey;
(* check whether or not bottom has been reached *)
IF thisNode # bottom THEN
status := Success;
RETURN thisNode^.value
ELSE (* bottom reached -- key not found *)
status := EntryNotFound;
RETURN NilKey
END (* IF *)
END lookup;
(* ---------------------------------------------------------------------------
* private function skew(node)
* ---------------------------------------------------------------------------
* Rotates node to the right if its left child has the same level as node.
* Returns the new root node. Node must not be NIL.
* ------------------------------------------------------------------------ *)
PROCEDURE skew ( node : Node ) : Node;
VAR
tempNode : Node;
BEGIN
(* rotate right if left child has same level *)
IF node^.level = node^.left^.level THEN
tempNode := node;
node := node^.left;
tempNode^.left := node^.right;
node^.right := tempNode
END; (* IF *)
RETURN node
END skew;
(* ---------------------------------------------------------------------------
* private function split(node)
* ---------------------------------------------------------------------------
* Rotates node to the left and promotes the level of its right child to
* become its new parent if node has two consecutive right children with the
* same level as node. Returns the new root node. Node must not be NIL.
* ------------------------------------------------------------------------ *)
PROCEDURE split ( node : Node ) : Node;
VAR
tempNode : Node;
BEGIN
(* rotate left if there are two right children on same level *)
IF node^.level = node^.right^.right^.level THEN
tempNode := node;
node := node^.right;
tempNode^.right := node^.left;
node^.right := tempNode;
node^.level := node^.level + 1
END; (* IF *)
RETURN node
END split;
(* ---------------------------------------------------------------------------
* private function insert(node, key, value, status)
* ---------------------------------------------------------------------------
* Recursively inserts a new entry for <key> with <value> into the tree whose
* root node is <node>. Returns the new root node of the resulting tree. If
* allocation fails or if a node with the same key already exists, then NO
* entry will be inserted and NIL is returned.
* ------------------------------------------------------------------------ *)
PROCEDURE insert
( node : Node;
key,
value : StringT;
VAR status : Status ) : Node;
VAR
newNode : Node;
BEGIN
IF node = bottom THEN
(* allocate new node *)
ALLOCATE(newNode, TSIZE(NodeDescriptor));
(* bail out if allocation failed *)
IF newNode = NIL THEN
status := AllocationFailed;
RETURN NIL
END; (* IF *)
(* init new node *)
newNode^.level := 1;
newNode^.key := key;
newNode^.value := value;
newNode^.left := bottom;
newNode^.right := bottom;
(* link it to the tree *)
node := newNode
ELSE
CASE String.comparison(key, node^.key) OF
(* key already exists *)
String.Equal :
status := KeyAlreadyPresent;
RETURN NIL
(* key < node^.key *)
| String.Less :
(* recursive insert left *)
node := insert(node^.left, key, value, status)
(* key > node^.key *)
| String.Greater :
(* recursive insert right *)
node := insert(node^.right, key, value, status)
END (* CASE *)
END; (* IF *)
(* bail out if allocation failed *)
IF status = AllocationFailed THEN
RETURN NIL
END; (* IF *)
(* rebalance the tree *)
node := skew(node);
node := split(node);
status := Success;
RETURN node
END insert;
(* ---------------------------------------------------------------------------
* private function remove(node, key, status)
* ---------------------------------------------------------------------------
* Recursively searches the tree whose root node is <node> for a node whose
* key is <key> and if found, removes that node and rebalances the resulting
* tree, then returns the new root of the resulting tree. If no node with
* <key> exists, then NIL is returned.
* ------------------------------------------------------------------------ *)
PROCEDURE remove ( node : Node; key : Key; VAR status : Status ) : Node;
BEGIN
(* exit when bottom reached *)
IF node = bottom THEN
status := EntryNotFound;
RETURN NIL;
END; (* IF *)
(* move down recursively until key is found or bottom is reached *)
prevNode := node;
(* move down left if search key is less than that of current node *)
IF String.comparison(key, node^.key) = String.Less THEN
node := remove(node^.left, key, status)
(* move down right if search key is not less than that of current node *)
ELSE
candidate := node;
node := remove(node^.right, key, status)
END; (* IF *)
(* remove entry *)
IF (node = prevNode) AND
(candidate # bottom) AND
(candidate^.key = key) THEN
candidate^.key := node^.key;
candidate := bottom;
node := node^.right;
DEALLOCATE(prevNode, TSIZE(NodeDescriptor));
status := Success
(* rebalance on the way back up *)
ELSIF
(node^.level - 1 > node^.left^.level) OR
(node^.level -1 < node^.right^.level) THEN
node^.level := node^.level - 1;
IF node^.level < node^.right^.level THEN
node^.right^.level := node^.level
END; (* IF *)
node := skew(node);
node := skew(node^.right);
node := skew(node^.right^.right);
node := split(node);
node := split(node^.right)
END; (* IF *)
RETURN node
END remove;
(* ---------------------------------------------------------------------------
* private procedure Traverse(node, visit)
* ---------------------------------------------------------------------------
* Recursively traverses the tree whose root node is node, in-order and calls
* the passed in visitor procedure for each node, passing its key and value.
* ------------------------------------------------------------------------ *)
PROCEDURE Traverse ( node : Node; visit : VisitorProc );
BEGIN
(* exit when bottom reached *)
IF node = bottom THEN
RETURN
END; (* IF *)
(* traverse left subtree *)
Traverse(node^.left, visit);
(* call visitor proc passing key and value *)
visit(node^.key, node^.value);
(* traverse right subtree *)
Traverse(node^.right, visit)
END Traverse;
BEGIN
(* init helper nodes *)
prevNode := NIL;
candidate := NIL;
(* init sentinel node *)
ALLOCATE(bottom, TSIZE(NodeDescriptor));
(* bottom^ := { 0, NIL, NIL, bottom, bottom } *)
bottom^.level := 0;
bottom^.key := NilKey;
bottom^.value := NilValue;
bottom^.left := bottom;
bottom^.right := bottom;
(* init dictionary *)
(* dictionary := { 0, bottom, NIL, NIL, Success } *)
dictionary.entries := 0;
dictionary.root := bottom;
dictionary.lastSearch.key := NilKey;
dictionary.lastSearch.value := NilValue;
dictionary.lastStatus := Success;
END Dictionary.