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
|
HI09: This example demonstrates how FunnelWeb can be used to create generics
in languages that do not already provide them.
@! We have to set the output line length limit up to cater for Barry Dwyer's
@! rather horizontal coding style.
@p maximum_output_line_length = 100
@O@<hi09.out@>==@{
@<Generic Set Module@>@(NaryTree@,NaryTreeSet@)
@}
@$@<Generic Set Module@>@(@2@)==@{@-
@! @1 is the base type, @2 is the set type.
[inherit ('@1'), environment ('@2')]
module @2;
type @2 = ^@2Record;
@2Record = record
Member: @1;
Next: @2;
end;
procedure Null@2 (var Result: @2);
begin new (Result);
Result^.Member := (- MaxInt)::@1;
Result^.Next := nil end;
function IsNull@2 (S: @2): boolean;
begin IsNull@2 := S^.Member::integer = - MaxInt end;
procedure ForEach@1 (S: @2; procedure DoIt (i: @1));
var ThisS, NextS: @2;
begin ThisS := S;
while ThisS^.Member::integer <> - MaxInt do
begin NextS := ThisS^.Next;
DoIt (ThisS^.Member);
ThisS := NextS end;
end;
function First@1 (S: @2): @1;
begin First@1 := S^.Member end;
function Is@1InSet (i: @1; S: @2): boolean;
procedure TestEquals (j: @1);
begin if Equal@1 (i, j) then Is@1InSet := true; end;
begin Is@1InSet := false; ForEach@1 (S, TestEquals); end;
function Includes@2 (S1, S2: @2): boolean;
var Result: boolean;
procedure TestIfInS1 (i: @1);
begin if Result then if not Is@1InSet (i, S1) then Result := false; end;
begin Result := true;
ForEach@1 (S2, TestIfInS1);
Includes@2 := Result end;
function Disjoint@2s (S1, S2: @2): boolean;
var Result: boolean;
procedure TestIfInS1 (i: @1);
begin if Result then if Is@1InSet (i, S1) then Result := false; end;
begin Result := true;
ForEach@1 (S2, TestIfInS1);
Disjoint@2s := Result end;
function Equal@2 (S1, S2: @2): boolean;
begin
Equal@2 := Includes@2 (S1, S2) and Includes@2 (S2, S1);
end;
procedure Insert@1 (i: @1; var S: @2);
var This, Pred, Succ: @2;
begin
if not Is@1InSet (i, S) then
begin
Pred := nil; Succ := S;
while Succ^.Member::integer > i::integer do begin
Pred := Succ; Succ := Succ^.Next end;
if Succ^.Member::integer < i::integer then begin
new (This); This^.Next := Succ; This^.Member := i;
if Pred <> nil then Pred^.Next := This else S := This;
end;
end;
end;
procedure Insert@1s (S1: @2; var S2: @2);
var This, Pred, Succ: @2;
procedure Add@1 (i: @1);
begin Insert@1 (i, S2) end;
begin
ForEach@1 (S1, Add@1);
end;
procedure Remove@1 (i: @1; var S: @2);
var Pred, This: @2;
begin
Pred := nil; This := S;
while not Equal@1 (This^.Member, i) do begin
Pred := This; This := This^.Next end;
if Pred <> nil then Pred^.Next := This^.Next else S := This^.Next;
Dispose (This);
end;
procedure Dispose@2 (var S: @2);
var Old: @2;
begin
while S <> nil do begin Old := S; S := S^.Next; Dispose (Old) end;
end;
end.
@}
|