-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathTeeGenericTree.pas
401 lines (308 loc) · 8.21 KB
/
TeeGenericTree.pas
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
// Latest source:
// https://github.com/davidberneda/GenericTree
unit TeeGenericTree;
{
Generic Tree structure
======================
Basic usage:
var Root : TNode<String>;
Root := TNode<String>.Create;
try
Root.Add('Hello').Add('World !');
finally
Root.Free;
end;
The generic type can be pre-declared for easier typing:
type
TFloatTree = TNode<Single>;
var Tree1 : TFloatTree;
Features:
Adding nodes using the Add method, returns the new created node:
var Node : TNode<String>;
Node := Root.Add('abc');
"Count" returns the number of child nodes for a given node:
var t : Integer,
t:=Node.Count;
"Empty" returns True when the "Count" of children nodes is zero:
var b : Boolean,
b:=Node.Empty;
Destroying a node removes it from its parent:
Node.Free;
Nodes can be accessed using the default array property:
Node := Root[3];
"Clear" removes and destroys all children nodes of a given node (recursively):
Node.Clear;
"Index" returns the position of a node in its parent children array, or -1
if the node is a "root" node.
var t : Integer;
t:=Node.Index;
"Parent" property returns the node that is the parent of a given node, or
nil if the node is a "root" node.
var tmp : TNode<String>;
tmp:=Node.Parent;
A node can be "detached" from its parent (without destroying it), setting the
Parent property to nil:
Node.Parent:=nil;
A node can also be removed and destroyed using its Parent Delete method:
Root.Delete(3); // removes and destroys the 4th child of Root
Traversing nodes (recursively or not) using the ForEach method:
var Total:Integer;
Total:=0;
Root.ForEach(procedure(const Item:TNode<String>) begin Inc(Total); end);
The "Level" property returns the depth of a node, (the number of parent->parent->...),
being zero for root nodes that have no parent.
var t : Integer;
t:=Node.Level;
Exchanging nodes (swap positions):
Root.Exchange( 5, 9 ); <-- swap positions
Sorting nodes:
Root.Sort(function(const A,B:TNode<String>):Integer
begin
if CaseSensitive then
result:=CompareStr(A.Data,B.Data)
else
result:=CompareText(A.Data,B.Data);
end);
}
interface
type
TInteger=NativeInt;
TNode<T>=class
public
type
// <0 : A<B
// 0 : A=B
// >0 : A>B
TCompareProc=reference to function(const A,B:TNode<T>):Integer;
private
type
TTypeItem=TNode<T>;
var
FItems : TArray<TNode<T>>;
{$IFDEF AUTOREFCOUNT}[Weak]{$ENDIF}
FParent : TNode<T>;
procedure Adopt(const Item:TNode<T>);
procedure Extract(const Index: TInteger; const ACount: TInteger=1);
function Get(const Index:TInteger):TNode<T>; inline;
function GetIndex:TInteger;
function GetLevel:TInteger;
function ItemsCopy:TArray<TNode<T>>;
procedure Orphan;
procedure PrivateSort(const ACompare: TCompareProc; const l,r:TInteger);
procedure SetParent(const Value:TNode<T>);
public
type
TNodeProc=reference to procedure(const Item:TNode<T>);
var
Data : T;
Constructor Create(const AData:T); overload;
Destructor Destroy; override;
function Add(const AData:T):TNode<T>;
procedure Clear; inline;
function Count:TInteger; inline;
function Empty:Boolean; inline;
procedure Exchange(const Index1,Index2:TInteger);
procedure Delete(const Index:TInteger; const ACount:TInteger=1);
procedure ForEach(const AProc:TNodeProc; const Recursive:Boolean=True);
procedure Sort(const ACompare:TCompareProc; const Recursive:Boolean=True);
property Index:TInteger read GetIndex;
property Item[const Index:TInteger]:TNode<T> read Get; default;
property Items:TArray<TNode<T>> read FItems;
property Level:TInteger read GetLevel;
property Parent:TNode<T> read FParent write SetParent;
end;
implementation
{ TNode<T> }
// Creates a new Node
Constructor TNode<T>.Create(const AData: T);
begin
inherited Create;
Data:=AData;
end;
// Remove and destroy all children nodes, then remove Self from Parent
Destructor TNode<T>.Destroy;
begin
Clear;
Orphan;
inherited;
end;
// Returns children node at Index position
function TNode<T>.Get(const Index: TInteger): TNode<T>;
begin
result:=FItems[Index];
end;
// Returns the number of children nodes
function TNode<T>.Count: TInteger;
begin
result:=Length(FItems);
end;
// Adds a new node and sets its AData
function TNode<T>.Add(const AData: T): TNode<T>;
begin
result:=TNode<T>.Create(AData);
Adopt(result);
end;
// Remove and destroy all children nodes
procedure TNode<T>.Clear;
begin
Delete(0,Count);
FItems:=nil;
end;
// Removes ACount items from the array, starting at Index position (without destroying them)
procedure TNode<T>.Extract(const Index, ACount: TInteger);
{$IF CompilerVersion<=28}
var t : TInteger;
{$ENDIF}
begin
{$IF CompilerVersion>28}
System.Delete(FItems,Index,ACount);
{$ELSE}
t:=Count-ACount;
if t-Index>0 then
System.Move(FItems[Index+ACount],FItems[Index],SizeOf(TObject)*(t-Index));
SetLength(FItems,t);
{$IFEND}
end;
// Removes and destroys children nodes from Index position (ACount default = 1)
procedure TNode<T>.Delete(const Index, ACount: TInteger);
var t : TInteger;
begin
// Destroy nodes
for t:=Index to Index+ACount-1 do
begin
FItems[t].FParent:=nil;
FItems[t].Free;
end;
Extract(Index,ACount);
end;
// Returns True when this node has no children nodes
function TNode<T>.Empty:Boolean;
begin
result:=Count=0;
end;
// Swap children nodes at positions: Index1 <---> Index2
procedure TNode<T>.Exchange(const Index1, Index2: TInteger);
var tmp : TNode<T>;
begin
tmp:=FItems[Index1];
FItems[Index1]:=FItems[Index2];
FItems[Index2]:=tmp;
end;
function TNode<T>.ItemsCopy:TArray<TNode<T>>;
var t : TInteger;
begin
SetLength(result,Count);
for t:=0 to Count-1 do
result[t]:=FItems[t];
end;
// Calls AProc for each children node (optionally recursive)
procedure TNode<T>.ForEach(const AProc: TNodeProc; const Recursive: Boolean);
var t : TInteger;
N : TTypeItem;
tmp : TArray<TTypeItem>;
begin
tmp:=ItemsCopy;
t:=0;
while t<Length(tmp) do
begin
N:=tmp[t];
if N<>nil then
begin
AProc(N);
if Recursive then
N.ForEach(AProc);
end;
Inc(t);
end;
end;
// Returns the Index position of Self in Parent children list
function TNode<T>.GetIndex: TInteger;
var t : TInteger;
begin
if FParent<>nil then
for t:=0 to FParent.Count-1 do
if FParent[t]=Self then
Exit(t);
result:=-1;
end;
// Returns the number of parents in the hierarchy up to top of tree
function TNode<T>.GetLevel: TInteger;
begin
if FParent=nil then
result:=0
else
result:=FParent.Level+1;
end;
// Adds Item to children list, sets Item Parent = Self
procedure TNode<T>.Adopt(const Item:TNode<T>);
var L: TInteger;
begin
Item.FParent:=Self;
// Pending: Capacity
L:=Count;
SetLength(FItems,L+1);
FItems[L]:=Item;
end;
// Removes itself from Parent children list
procedure TNode<T>.Orphan;
begin
if FParent<>nil then
FParent.Extract(Index);
end;
// Sets or changes the Parent node of Self
procedure TNode<T>.SetParent(const Value: TNode<T>);
begin
if FParent<>Value then
begin
Orphan;
FParent:=Value;
if FParent<>nil then
FParent.Adopt(Self);
end;
end;
// Internal. Re-order nodes using QuickSort algorithm
procedure TNode<T>.PrivateSort(const ACompare: TCompareProc; const l,r:TInteger);
var i : TInteger;
j : TInteger;
x : TInteger;
begin
i:=l;
j:=r;
x:=(i+j) shr 1;
while i<j do
begin
while ACompare(Self[x],Self[i])>0 do inc(i);
while ACompare(Self[x],Self[j])<0 do dec(j);
if i<j then
begin
Exchange(i,j);
if i=x then
x:=j
else
if j=x then
x:=i;
end;
if i<=j then
begin
inc(i);
dec(j)
end;
end;
if l<j then
PrivateSort(ACompare,l,j);
if i<r then
PrivateSort(ACompare,i,r);
end;
// Re-order children items according to a custom ACompare function
procedure TNode<T>.Sort(const ACompare: TCompareProc; const Recursive: Boolean);
var t : TInteger;
begin
if Count>1 then
PrivateSort(ACompare,0,Count-1);
//Optionally, re-order all children-children... nodes
if Recursive then
for t:=0 to Count-1 do
Items[t].Sort(ACompare,Recursive);
end;
end.