git @ Cat's Eye Technologies Dipple / a002161
Add rudimentary implementation of association lists in Oberon. Chris Pressey 10 months ago
1 changed file(s) with 64 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 MODULE AssocList;
1
2 IMPORT Out;
3
4 TYPE
5 Value* = ARRAY 256 OF CHAR;
6 ListPtr* = POINTER TO List;
7 List = RECORD
8 key: INTEGER;
9 val: Value;
10 next: ListPtr
11 END;
12
13 PROCEDURE Make*(key: INTEGER; val: ARRAY OF CHAR; next: ListPtr): ListPtr;
14 VAR e: ListPtr;
15 BEGIN
16 NEW(e);
17 e^.key := key;
18 e^.val := val;
19 e^.next := next;
20 RETURN e
21 END Make;
22
23 PROCEDURE Find*(e: ListPtr; key: INTEGER): ListPtr;
24 VAR
25 f: ListPtr;
26 result: ListPtr;
27 BEGIN
28 f := e;
29 result := NIL;
30 WHILE (f # NIL) & (result = NIL) DO
31 IF f^.key = key THEN
32 result := f;
33 END;
34 f := f^.next;
35 END;
36 RETURN result
37 END Find;
38
39 PROCEDURE DisplayValue*(e: ListPtr);
40 BEGIN
41 IF e # NIL THEN
42 Out.String(e^.val);
43 ELSE
44 Out.String("(NIL!)");
45 END;
46 Out.Ln;
47 END DisplayValue;
48
49 PROCEDURE Demo*;
50 VAR
51 e: ListPtr;
52 v: ListPtr;
53 k: INTEGER;
54 BEGIN
55 e := Make(40, "Hello", Make(80, "World!", NIL));
56 FOR k := 1 TO 10 DO
57 v := Find(e, k * 10);
58 DisplayValue(v);
59 END
60 END Demo;
61
62 BEGIN
63 END AssocList.