-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoobjects.pas
More file actions
207 lines (175 loc) · 4.2 KB
/
oobjects.pas
File metadata and controls
207 lines (175 loc) · 4.2 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
{|----------------------------------------------------------------------
| Unit: OObjects
|
| Author: Egbert van Nes
|
| Description: Some changed TLists (more compatible with Borland Pascal 7)
|
| Copyright (c) 2000 Egbert van Nes
| All rights reserved
| Disclaimer and licence notes: see license.txt
|
|----------------------------------------------------------------------
}
(*
Modified by Corpsman, to Support Lazarus Linux. 01.09.2009
*)
Unit oobjects;
{$IFDEF FPC}
{$MODE Delphi}
{$ENDIF}
Interface
Uses Classes;
Const
MaxCollectionSize = Maxint Div (SizeOf(Integer) * 2);
Type
TOCollection = Class(TList)
public
Constructor Create(ACapacity: Integer);
Procedure AtFree(Index: Integer);
Procedure FreeAll;
Procedure DoFree(Item: Pointer);
Procedure FreeItem(Item: Pointer); virtual;
Destructor Destroy; override;
End;
TNoOwnerCollection = Class(TOCollection)
public
Procedure FreeItem(Item: Pointer); override;
End;
{ TSortedCollection object }
TSortedCollection = Class(TOCollection)
public
Duplicates: Boolean;
Constructor Create(ACapacity: Integer);
Function Compare(Key1, Key2: Pointer): Integer; virtual; abstract;
Function IndexOf(Item: Pointer): Integer; virtual;
Procedure Add(Item: Pointer); virtual;
Procedure AddReplace(Item: Pointer); virtual;
{if duplicate then replace the duplicate else add}
Function KeyOf(Item: Pointer): Pointer; virtual;
Function Search(Key: Pointer; Var Index: Integer): Boolean; virtual;
End;
{ TStrCollection object }
TStrCollection = Class(TSortedCollection)
public
Function Compare(Key1, Key2: Pointer): Integer; override;
Procedure FreeItem(Item: Pointer); override;
End;
Implementation
Uses SysUtils;
Constructor TOCollection.Create(ACapacity: Integer);
Begin
Inherited Create;
SetCapacity(ACapacity);
{Delta is automatic in TList}
End;
Destructor TOCollection.Destroy;
Begin
FreeAll;
Inherited Destroy;
End;
Procedure TOCollection.AtFree(Index: Integer);
Var
Item: Pointer;
Begin
Item := Items[Index];
Delete(Index);
FreeItem(Item);
End;
Procedure TOCollection.FreeAll;
Var
I: Integer;
Begin
Try
For I := 0 To Count - 1 Do
FreeItem(Items[I]);
Finally
Count := 0;
End;
End;
Procedure TOCollection.DoFree(Item: Pointer);
Begin
AtFree(IndexOf(Item));
End;
Procedure TOCollection.FreeItem(Item: Pointer);
Begin
If (Item <> Nil) Then
With TObject(Item) As TObject Do
Free;
End;
{----------------------------------------------------------------virtual;
Implementing TNoOwnerCollection
-----------------------------------------------------------------}
Procedure TNoOwnerCollection.FreeItem(Item: Pointer);
Begin
End;
{ TSortedCollection }
Constructor TSortedCollection.Create(ACapacity: Integer);
Begin
Inherited Create(ACapacity);
Duplicates := False;
End;
Function TSortedCollection.IndexOf(Item: Pointer): Integer;
Var
I: Integer;
Begin
IndexOf := -1;
If Search(KeyOf(Item), I) Then Begin
If Duplicates Then
While (I < Count) And (Item <> Items[I]) Do
inc(I);
If I < Count Then IndexOf := I;
End;
End;
Procedure TSortedCollection.AddReplace(Item: Pointer);
Var
Index: Integer;
Begin
If Search(KeyOf(Item), Index) Then
Delete(Index);
Add(Item);
End;
Procedure TSortedCollection.Add(Item: Pointer);
Var
I: Integer;
Begin
i := 0; // Shutdown Compiler Warning.
If Not Search(KeyOf(Item), I) Or Duplicates Then
Insert(I, Item);
End;
Function TSortedCollection.KeyOf(Item: Pointer): Pointer;
Begin
KeyOf := Item;
End;
Function TSortedCollection.Search(Key: Pointer; Var Index: Integer): Boolean;
Var
L, H, I, C: Integer;
Begin
Search := False;
L := 0;
H := Count - 1;
While L <= H Do Begin
I := (L + H) Shr 1;
C := Compare(KeyOf(Items[I]), Key);
If C < 0 Then
L := I + 1
Else Begin
H := I - 1;
If C = 0 Then Begin
Search := True;
If Not Duplicates Then L := I;
End;
End;
End;
Index := L;
End;
{ TStrCollection }
Function TStrCollection.Compare(Key1, Key2: Pointer): Integer;
Begin
Compare := StrComp(Key1, Key2);
End;
Procedure TStrCollection.FreeItem(Item: Pointer);
Begin
StrDispose(Item);
End;
End.