-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftreapapp.lpr
169 lines (142 loc) · 4.36 KB
/
ftreapapp.lpr
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
{$mode objfpc}{$H+}{$J-}
{$ASSERTIONS ON}
{$warnings on}
{$hints on}
{$R+}
program ftreapapp;
uses
treap, itreap, rheap;
type
TIntTreapNode = specialize TTreapNode<LongInt>;
TImpIntTreapNode = specialize TImplicitTreapNode<LongInt>;
const
NODES_NUM = 100;
MAX_KEY = 1000;
var
ra, ln, rn: TIntTreapNode;
ria: TImpIntTreapNode = nil;
ta: array of longint;
i, j: longint;
begin
//TIntTreapNode.ClassInfo;
WriteLn('TIntTreapNode instanceSize - ', TIntTreapNode.InstanceSize);
WriteLn('TRandomHeapNode instanceSize - ', TRandomHeapNode.InstanceSize);
SetLength(ta, NODES_NUM);
for i := 0 to NODES_NUM - 1 do
begin
ta[i] := 2*i; //Random(MAX_KEY);
end;
for i := 0 to NODES_NUM - 1 do
begin
TIntTreapNode.Insert(ra, ta[i]);
end;
WriteLn('Check treap structure - ', TIntTreapNode.CheckStucture(ra));
WriteLn('Size - ', TIntTreapNode.GetSize(ra));
if ra <> nil then
WriteLn(ra.FKey);
WriteLn('PASSED...');
//ReadLn();
WriteLn('Check treap Contains method.');
for i := 0 to NODES_NUM - 1 do
begin
Assert(TIntTreapNode.Contains(ra, ta[i]));
Assert(not TIntTreapNode.Contains(ra, 1));
end;
WriteLn('PASSED...');
WriteLn('Check treap GetPosition method.');
for i := 0 to NODES_NUM - 1 do
begin
writeLn('Key - ', ta[i], ' Pos - ', TIntTreapNode.GetPosition(ra, ta[i]));
Assert(i = TIntTreapNode.GetPosition(ra, ta[i]));
end;
WriteLn('PASSED...');
WriteLn('Check treap GetAt method.');
for i := 0 to NODES_NUM - 1 do
begin
Assert(2*i = TIntTreapNode.GetAt(ra, i));
end;
WriteLn('PASSED...');
WriteLn('Check treap BisectLeft method.');
for i := 0 to NODES_NUM - 1 do
begin
writeLn('Key - ', ta[i], ' BL - ', TIntTreapNode.BisectLeft(ra, ta[i]));
Assert(i = TIntTreapNode.BisectLeft(ra, ta[i]));
end;
WriteLn('PASSED...');
WriteLn('Check treap BisectRight method.');
for i := 0 to NODES_NUM - 1 do
begin
writeLn('Key - ', ta[i], ' BR - ', TIntTreapNode.BisectRight(ra, ta[i]));
Assert(i+1 = TIntTreapNode.BisectRight(ra, ta[i]));
end;
WriteLn('PASSED...');
WriteLn('Check treap DeleteAt method.');
for i := 0 to NODES_NUM - 1 do
begin
j := TIntTreapNode.RemoveAt(ra, 0);
WriteLn(j);
end;
WriteLn('Check treap structure - ', TIntTreapNode.CheckStucture(ra));
WriteLn('Size - ', TIntTreapNode.GetSize(ra));
WriteLn('PASSED...');
TIntTreapNode.DestroyTreap(ra);
for i := 0 to NODES_NUM - 1 do
begin
TIntTreapNode.Insert(ra, ta[i]);
end;
WriteLn('Check treap structure - ', TIntTreapNode.CheckStucture(ra));
WriteLn('Size - ', TIntTreapNode.GetSize(ra));
WriteLn('DivideRight test ...');
for i := 0 to NODES_NUM - 1 do
begin
TIntTreapNode.DivideRight(ra, ta[i], ln, rn);
Assert(TIntTreapNode.Contains(ln, ta[i]));
Assert(not TIntTreapNode.Contains(rn, ta[i]));
ra := TIntTreapNode.Meld(ln, rn) as TIntTreapNode;
end;
WriteLn('PASSED...');
WriteLn('Check treap structure - ', TIntTreapNode.CheckStucture(ra));
WriteLn('Size - ', TIntTreapNode.GetSize(ra));
WriteLn('DivideLeft test ...');
for i := 0 to NODES_NUM - 1 do
begin
TIntTreapNode.DivideLeft(ra, ta[i], ln, rn);
Assert(TIntTreapNode.Contains(rn, ta[i]));
Assert(not TIntTreapNode.Contains(ln, ta[i]));
ra := TIntTreapNode.Meld(ln, rn) as TIntTreapNode;
end;
WriteLn('Check treap structure - ', TIntTreapNode.CheckStucture(ra));
WriteLn('Size - ', TIntTreapNode.GetSize(ra));
WriteLn('PASSED...');
TIntTreapNode.DestroyTreap(ra);
Assert(ra = nil);
Assert(TIntTreapNode.GetSize(ra) = 0);
WriteLn('-----------------------');
WriteLn('Implicit Treap test ...');
WriteLn('-----------------------');
WriteLn('TImpIntTreapNode instanceSize - ', TImpIntTreapNode.InstanceSize);
for i := 0 to NODES_NUM - 1 do
begin
TImpIntTreapNode.InsertAt(ria, 0, ta[i]);
end;
Assert(True = TImpIntTreapNode.CheckStucture(ria));
WriteLn('Check implicit treap structure PASSED...');
for i := 0 to NODES_NUM - 1 do
begin
j := TImpIntTreapNode.GetAt(ria, i);
WriteLn(j);
Assert(ta[NODES_NUM-1-i] = j);
end;
WriteLn('InsertAt PASSED...');
for i := 0 to NODES_NUM - 1 do
begin
j := TImpIntTreapNode.RemoveAt(ria, 0);
WriteLn(j);
Assert(ta[NODES_NUM-1-i] = j);
end;
WriteLn('RemoveAt PASSED...');
Assert(ria = nil);
Assert(TImpIntTreapNode.GetSize(ria) = 0);
TImpIntTreapNode.DestroyTreap(ria);
//ReadLn();
end.