-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMapClass.pas
More file actions
346 lines (277 loc) · 7.23 KB
/
MapClass.pas
File metadata and controls
346 lines (277 loc) · 7.23 KB
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
(*
Greenfish Icon Editor Pro
Copyright (c) 2012-13 B. Szalkai
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*)
unit MapClass;
{$mode delphi}
interface
uses
SysUtils, Contnrs;
type
TMap<TKey, TValue> = class
public type
TNode = class
private type
TColor = (clRed, clBlack);
private
Color: TColor;
Parent, Left, Right: TNode;
FKey: TKey;
public
Value: TValue;
property Key: TKey read FKey;
constructor Create(const AKey: TKey);
destructor Destroy; override;
end;
TEnumerator = class
private
Root: TNode;
Stack: TStack;
function GetCurrent: TNode;
public
constructor Create(const ARoot: TNode); virtual;
destructor Destroy; override;
function MoveNext: Boolean;
property Current: TNode read GetCurrent;
end;
protected
Root: TNode;
FCount: integer;
function Find(const Key: TKey; out Parent: TNode): TNode;
function AddNode(const Key: TKey): TNode;
procedure RotateLeft(const Node: TNode);
procedure RotateRight(const Node: TNode);
function GetValue(const Key: TKey): TValue;
procedure SetValue(const Key: TKey; const Value: TValue);
public
constructor Create; virtual;
destructor Destroy; override;
procedure Clear;
function ContainsKey(const Key: TKey): boolean;
function TryGet(const Key: TKey): TValue;
function GetEnumerator: TEnumerator;
property Count: integer read FCount;
// Causes access violation on read if key not found
property Map[Key: TKey]: TValue read GetValue write SetValue; default;
end;
implementation
// TMap.TNode
constructor TMap<TKey, TValue>.TNode.Create;
begin
Color := clBlack;
Parent := nil;
Left := nil;
Right := nil;
FKey := AKey;
Value := Default(TValue);
end;
destructor TMap<TKey, TValue>.TNode.Destroy;
begin
if Left <> nil then Left.Free;
if Right <> nil then Right.Free;
inherited;
end;
// TMap
function TMap<TKey, TValue>.Find;
begin
Result := Root;
Parent := nil;
while Result <> nil do
begin
if Key = Result.Key then Break;
Parent := Result;
if Key < Result.Key then Result := Result.Left else Result := Result.Right;
end;
end;
function TMap<TKey, TValue>.AddNode;
var
Node, Parent, Node2: TNode;
begin
Node := Find(Key, Parent);
if Node <> nil then Exit(Node);
Node := TNode.Create(Key);
Result := Node;
inc(FCount);
if Parent <> nil then
begin
if Key < Parent.Key then Parent.Left := Node else Parent.Right := Node;
Node.Parent := Parent;
end else
Root := Node;
while (Node.Parent <> nil) and (Node.Parent.Color = clRed) do
if Node.Parent = Node.Parent.Parent.Left then
begin
Node2 := Node.Parent.Parent.Right;
if (Node2 <> nil) and (Node2.Color = clRed) then
begin
Node.Parent.Color := clBlack;
Node := Node.Parent.Parent;
Node.Color := clRed;
Node2.Color := clBlack;
end else
begin
if Node = Node.Parent.Right then
begin
Node := Node.Parent;
RotateLeft(Node);
end;
Node.Parent.Color := clBlack;
Node.Parent.Parent.Color := clRed;
RotateRight(Node.Parent.Parent);
end;
end else
begin
Node2 := Node.Parent.Parent.Left;
if (Node2 <> nil) and (Node2.Color = clRed) then
begin
Node.Parent.Color := clBlack;
Node := Node.Parent.Parent;
Node.Color := clRed;
Node2.Color := clBlack;
end else
begin
if Node = Node.Parent.Left then
begin
Node := Node.Parent;
RotateRight(Node);
end;
Node.Parent.Color := clBlack;
Node.Parent.Parent.Color := clRed;
RotateLeft(Node.Parent.Parent);
end;
end;
Root.Color := clBlack;
end;
procedure TMap<TKey, TValue>.RotateLeft;
var
Node2: TNode;
begin
Node2 := Node.Right;
Node.Right := Node2.Left;
if Node2.Left <> nil then Node2.Left.Parent := Node;
Node2.Parent := Node.Parent;
if Node.Parent = nil then Root := Node2 else
if Node = Node.Parent.Left then
Node.Parent.Left := Node2 else
Node.Parent.Right := Node2;
Node2.Left := Node;
Node.Parent := Node2;
end;
procedure TMap<TKey, TValue>.RotateRight;
var
Node2: TNode;
begin
Node2 := Node.Left;
Node.Left := Node2.Right;
if Node2.Right <> nil then Node2.Right.Parent := Node;
Node2.Parent := Node.Parent;
if Node.Parent = nil then Root := Node2 else
if Node = Node.Parent.Left then
Node.Parent.Left := Node2 else
Node.Parent.Right := Node2;
Node2.Right := Node;
Node.Parent := Node2;
end;
function TMap<TKey, TValue>.GetValue;
var
Dummy: TNode;
begin
Result := Find(Key, Dummy).Value;
end;
procedure TMap<TKey, TValue>.SetValue;
begin
AddNode(Key).Value := Value;
end;
constructor TMap<TKey, TValue>.Create;
begin
Root := nil;
FCount := 0;
end;
destructor TMap<TKey, TValue>.Destroy;
begin
Clear;
inherited;
end;
procedure TMap<TKey, TValue>.Clear;
begin
FreeAndNil(Root);
FCount := 0;
end;
function TMap<TKey, TValue>.ContainsKey;
var
Dummy: TNode;
begin
Result := (Find(Key, Dummy) <> nil);
end;
function TMap<TKey, TValue>.TryGet;
var
Node, Parent: TNode;
begin
Node := Find(Key, Parent);
if Node = nil then
Result := Default(TValue)
else Result := Node.Value;
end;
function TMap<TKey, TValue>.GetEnumerator;
begin
Result := TEnumerator.Create(Root);
end;
// TMap.TEnumerator
constructor TMap<TKey, TValue>.TEnumerator.Create;
begin
Root := ARoot;
Stack := TStack.Create;
end;
destructor TMap<TKey, TValue>.TEnumerator.Destroy;
begin
Stack.Free;
end;
function TMap<TKey, TValue>.TEnumerator.GetCurrent;
begin
if Stack.AtLeast(1) then Exit(Stack.Peek);
Exit(nil); // should never happen
end;
function TMap<TKey, TValue>.TEnumerator.MoveNext;
var
Node: TNode;
begin
if not Stack.AtLeast(1) then
begin
if Root = nil then Exit(False);
Stack.Push(Root);
Exit(True);
end;
Node := Stack.Peek;
if Node.Left <> nil then
begin
Stack.Push(Node.Left);
Exit(True);
end;
if Node.Right <> nil then
begin
Stack.Push(Node.Right);
Exit(True);
end;
while Stack.AtLeast(1) do
begin
Node := Stack.Pop;
if (Node.Parent <> nil) and
(Node = Node.Parent.Left) and (Node.Parent.Right <> nil) then
begin
Stack.Push(Node.Parent.Right);
Exit(True);
end;
end;
Exit(False);
end;
end.