File: PasDoc_HierarchyTree.pas

package info (click to toggle)
pasdoc 0.16.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,572 kB
  • sloc: pascal: 28,894; javascript: 7,665; xml: 2,597; makefile: 523; sh: 417
file content (353 lines) | stat: -rw-r--r-- 8,331 bytes parent folder | download | duplicates (4)
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
{
  Copyright 1998-2018 PasDoc developers.

  This file is part of "PasDoc".

  "PasDoc" 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 2 of the License, or
  (at your option) any later version.

  "PasDoc" 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 "PasDoc"; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA

  ----------------------------------------------------------------------------
}

{
  @author(Johannes Berg <johannes@sipsolutions.de>)
  a n-ary tree for PasItems --- for use in Class Hierarchy
}
unit PasDoc_HierarchyTree;

{$I pasdoc_defines.inc}

interface

uses
  Classes,
  PasDoc_Items;

type
  TPasItemNode = class(TObject)
  protected
    FChildren: TList;
    FParent: TPasItemNode;
    FItem: TPasItem;
    FName: string;
    function GetName: string;
  protected
    procedure AddChild(const Child: TPasItemNode); overload;
    function AddChild(const AName: string): TPasItemNode; overload;
    function AddChild(const AItem: TPasItem): TPasItemNode; overload;
    function FindItem(const AName: string): TPasItemNode;
    procedure Adopt(const AChild: TPasItemNode);
    function Orphan(const AChild: TPasItemNode): boolean;
    procedure Sort;
  public
    constructor Create;
    destructor Destroy; override;
    function Level: Integer;
    property Name: string read GetName;
    property Item: TPasItem read FItem;
    property Parent: TPasItemNode read FParent;
  end;

  TStringCardinalTree = class
  protected
    FRoot: TPasItemNode;
    function GetIsEmpty: boolean;
    function GetFirstItem: TPasItemNode;
    procedure NeedRoot;
  public
    function ItemOfName(const AName: string): TPasItemNode;
    function InsertName(const AName: string): TPasItemNode; overload;
    function InsertItem(const AItem: TPasItem): TPasItemNode; overload;
    function InsertParented(const AParent: TPasItemNode;
      const AItem: TPasItem): TPasItemNode; overload;
    function InsertParented(const AParent: TPasItemNode;
      const AName: string): TPasItemNode; overload;
    procedure MoveChildLast(const Child, Parent: TPasItemNode);

    property IsEmpty: boolean read GetIsEmpty;
    property FirstItem: TPasItemNode read GetFirstItem;

    function Level(const ANode: TPasItemNode): Integer;
    function NextItem(const ANode: TPasItemNode): TPasItemNode;

    procedure Sort;

    constructor Create;
    destructor Destroy; override;
  end;

function NewStringCardinalTree: TStringCardinalTree;

implementation
uses
  SysUtils;

function SortProc(A, B: Pointer): Integer;
begin
  Result := CompareText(TPasItemNode(A).Name, TPasItemNode(B).Name)
end;

function NewStringCardinalTree: TStringCardinalTree;
begin
  Result := TStringCardinalTree.Create;
end;

{ TStringCardinalTree }

constructor TStringCardinalTree.Create;
begin
  FRoot := nil;
end;

destructor TStringCardinalTree.Destroy;
begin
  FRoot.Free;
  inherited;
end;

function TStringCardinalTree.GetFirstItem: TPasItemNode;
begin
  Result := nil;
  if Assigned(FRoot) then begin
    if FRoot.FChildren.Count > 0 then begin
      Result := TPasItemNode(FRoot.FChildren[0]);
    end;
  end;
end;

function TStringCardinalTree.GetIsEmpty: boolean;
begin
  Result := not Assigned(FRoot);
end;

function TStringCardinalTree.InsertName(
  const AName: string): TPasItemNode;
begin
  NeedRoot;
  Result := FRoot.AddChild(AName);
end;

function TStringCardinalTree.InsertParented(const AParent: TPasItemNode;
  const AItem: TPasItem): TPasItemNode;
begin
  if AParent = nil then begin
    NeedRoot;
    Result := FRoot.AddChild(AItem);
  end else begin
    Result := AParent.AddChild(AItem);
  end
end;

function TStringCardinalTree.InsertParented(const AParent: TPasItemNode;
  const AName: string): TPasItemNode;
begin
  if AParent = nil then begin
    NeedRoot;
    Result := FRoot.AddChild(AName);
  end else begin
    Result := AParent.AddChild(AName);
  end
end;

function TStringCardinalTree.Level(
  const ANode: TPasItemNode): Integer;
begin
  Result := ANode.Level;
end;

procedure TStringCardinalTree.MoveChildLast(const Child,
  Parent: TPasItemNode);
begin
  NeedRoot;
  if FRoot.Orphan(Child) then begin
    Parent.Adopt(Child);
  end;
end;

procedure TStringCardinalTree.NeedRoot;
begin
  if not Assigned(FRoot) then begin
    FRoot := TPasItemNode.Create;
  end;
end;

function TStringCardinalTree.ItemOfName(
  const AName: string): TPasItemNode;
begin
  NeedRoot;
  Result := FRoot.FindItem(AName);
end;

function TStringCardinalTree.NextItem(
  const ANode: TPasItemNode): TPasItemNode;
var
  idx: Integer;
  LNode: TPasItemNode;
begin
  Result := nil;
  if ANode.FChildren.Count > 0 then begin
    Result := TPasItemNode(ANode.FChildren[0]);
  end;
  if Result = nil then begin
    if Assigned(ANode.FParent) then begin
      idx := ANode.FParent.FChildren.IndexOf(ANode);
      if idx + 1 < ANode.FParent.FChildren.Count then begin
        Result := TPasItemNode(ANode.FParent.FChildren[idx + 1]);
      end;
    end;
  end;
  if Result = nil then begin
    LNode := ANode.FParent;
    while Assigned(LNode) do begin
      if Assigned(LNode.FParent) then begin
        idx := LNode.FParent.FChildren.IndexOf(LNode);
        if LNode.FParent.FChildren.Count > idx + 1 then begin
          Result := TPasItemNode(LNode.FParent.FChildren[idx +
            1]);
          break;
        end;
      end;
      LNode := LNode.FParent;
    end;
  end;
end;

procedure TStringCardinalTree.Sort;
begin
  if Assigned(FRoot) then begin
    FRoot.Sort;
  end;
end;

function TStringCardinalTree.InsertItem(
  const AItem: TPasItem): TPasItemNode;
begin
  Result := InsertParented(nil, AItem);
end;

{ TPasItemNode }

procedure TPasItemNode.AddChild(const Child: TPasItemNode);
begin
  FChildren.Add(Child);
end;

function TPasItemNode.AddChild(const AName: string): TPasItemNode;
begin
  Result := TPasItemNode.Create;
  Result.FItem := nil;
  Result.FName := AName;
  Result.FParent := Self;
  AddChild(Result);
end;

function TPasItemNode.AddChild(const AItem: TPasItem): TPasItemNode;
begin
  Result := TPasItemNode.Create;
  Result.FItem := AItem;
  Result.FParent := Self;
  AddChild(Result);
end;

procedure TPasItemNode.Adopt(const AChild:
  TPasItemNode);
begin
  FChildren.Add(AChild);
  AChild.FParent := Self;
end;

constructor TPasItemNode.Create;
begin
  FParent := nil;
  FChildren := TList.Create;
  FItem := nil;
end;

destructor TPasItemNode.Destroy;
var
  i: Integer;
begin
  for i := 0 to FChildren.Count-1 do begin
    TObject(FChildren.Items[i]).Free;
  end;
  FChildren.Free;
  inherited;
end;

function TPasItemNode.FindItem(
  const AName: string): TPasItemNode;
var
  i: Integer;
  LName: string;
begin
  Result := nil;
  LName := LowerCase(AName);
  for i := 0 to FChildren.Count - 1 do begin
    if LowerCase(TPasItemNode(FChildren[i]).Name) = LName then
      begin
      Result := TPasItemNode(FChildren[i]);
      break;
    end;
    Result := TPasItemNode(FChildren[i]).FindItem(AName);
    if Assigned(Result) then break;
  end;
end;

function TPasItemNode.GetName: string;
begin
  if Assigned(FItem) then begin
    Result := FItem.Name;
  end else begin
    Result := FName;
  end;
end;

function TPasItemNode.Level: Integer;
begin
  if Assigned(FParent) then begin
    Result := FParent.Level + 1;
  end else begin
    Result := 0;
  end;
end;

function TPasItemNode.Orphan(
  const AChild: TPasItemNode): boolean;
var
  i: Integer;
begin
  i := FChildren.IndexOf(AChild);
  Result := false;
  if i >= 0 then begin
    FChildren.Delete(i);
    Result := true;
  end else begin
    for i := FChildren.Count - 1 downto 0 do begin
      Result := TPasItemNode(FChildren[i]).Orphan(AChild);
      if Result then break;
    end;
  end;
end;

procedure TPasItemNode.Sort;
var
  i: Integer;
begin
  FChildren.Sort( {$IFDEF FPC}@{$ENDIF} SortProc);
  for i := FChildren.Count-1 downto 0 do begin
    TPasItemNode(FChildren[i]).Sort;
  end;
end;

end.