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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit Collection;
interface
{$SCOPEDENUMS ON}
uses
Boolean,
Comparator,
Condition,
Enumerator,
Exception,
Integer._32,
Iterator,
Object_.Base,
Reference.Manager,
Text;
type
TDuplicateMode = (Allow, Filter, Error);
ECollection = class(EException);
EDuplicate = class(ECollection);
TCollection<TElement> = class(TBaseObject)
private
FElementManager: TReferenceManager<TElement>;
procedure SetElementManager(const Value: TReferenceManager<TElement>);
protected
function GetCount(): TInteger32; virtual; abstract;
procedure SetCount(const Value: TInteger32); virtual;
function GetCapacity(): TInteger32; virtual;
procedure SetCapacity(const Value: TInteger32); virtual;
procedure AcquireElement(Element: TElement); virtual;
procedure ReleaseElement(Element: TElement); virtual;
procedure ReleaseElements(); virtual;
function GetComparator(): TComparator<TElement>; virtual; abstract;
procedure SetComparator(const Value: TComparator<TElement>); virtual; abstract;
public
Unassigned: TElement;
RejectUnassigned: TBoolean;
constructor Create(); override;
destructor Destroy(); override;
procedure BeforeDestruction(); override;
function ToText(): TText; override;
function Add(Element: TElement): TBoolean; virtual; abstract;
function Contains(Element: TElement): TBoolean; overload; virtual; abstract;
procedure Clear(); virtual; abstract;
function CreateIterator(): TIterator<TElement>; virtual; abstract;
function Extract(Element: TElement): TElement; overload; virtual; abstract;
function Extract(Condition: TCondition<TElement>): TElement; overload; virtual; abstract;
function Dequeue(): TElement; virtual; abstract;
function GetEnumerator(): TEnumerator<TElement>; virtual;
procedure AddAll(Elements: TCollection<TElement>); overload; virtual;
procedure AddAll(const Elements: array of TElement); overload; virtual;
procedure AddAll(Iterator: TIterator<TElement>); overload; virtual;
function Remove(Element: TElement): TBoolean; overload; virtual;
function Remove(Condition: TCondition<TElement>): TBoolean; overload; virtual;
procedure RemoveAll(Elements: TCollection<TElement>); virtual;
function ToArray(): TArray<TElement>; virtual;
procedure FilterFrom(Source: TCollection<TElement>; Condition: TCondition<TElement>);
function Contains(Condition: TCondition<TElement>): TBoolean; overload; virtual;
function ContainsAll(Elements: TCollection<TElement>): TBoolean; virtual;
function Find(Condition: TCondition<TElement>): TElement;
function GetMinimum(Comparator: TComparator<TElement>): TElement;
function GetMaximum(Comparator: TComparator<TElement>): TElement;
function MatchesAll(Condition: TCondition<TElement>): TBoolean;
function CountMatches(Condition: TCondition<TElement>): TInteger32;
property Count: TInteger32 read GetCount write SetCount;
property Capacity: TInteger32 read GetCapacity write SetCapacity;
property ElementManager: TReferenceManager<TElement> read FElementManager write SetElementManager;
property Comparator: TComparator<TElement> read GetComparator write SetComparator;
end;
implementation
uses
Enumerator.Iterator,
Exception.NotImplemented,
Integer._32.Positive.TextFormat,
Mathematics.Sign,
Reference.User;
{ TCollection<TElement> }
procedure TCollection<TElement>.AcquireElement(Element: TElement);
begin
if not RejectUnassigned or (Comparator.Compare(Element, Unassigned) <> sZero) then
ElementManager.Acquire(Element)
else
raise ECollection.Create('Adding unassigned value');
end;
procedure TCollection<TElement>.AddAll(Elements: TCollection<TElement>);
var
Element: TElement;
begin
for Element in Elements do
Add(Element);
end;
procedure TCollection<TElement>.AddAll(Iterator: TIterator<TElement>);
begin
while Iterator.IsValid() do
begin
Add(Iterator.Current);
Iterator.GotoNext();
end;
end;
procedure TCollection<TElement>.AddAll(const Elements: array of TElement);
var
Element: TElement;
begin
for Element in Elements do
Add(Element);
end;
procedure TCollection<TElement>.BeforeDestruction();
begin
ReleaseElements();
inherited BeforeDestruction();
end;
function TCollection<TElement>.Contains(Condition: TCondition<TElement>): TBoolean;
var
Iterator: TIterator<TElement>;
begin
Condition.AddReference();
Result := False;
Iterator := CreateIterator();
while not Result and Iterator.IsValid() do
begin
Result := Condition.Matches(Iterator.Current);
Iterator.GotoNext();
end;
Iterator.Free();
Condition.ReleaseReference();
end;
function TCollection<TElement>.ContainsAll(Elements: TCollection<TElement>): TBoolean;
var
Iterator: TIterator<TElement>;
begin
Result := True;
Iterator := Elements.CreateIterator();
while Result and Iterator.IsValid() do
begin
Result := Contains(Iterator.Current);
Iterator.GotoNext();
end;
Iterator.Free();
end;
function TCollection<TElement>.CountMatches(Condition: TCondition<TElement>): TInteger32;
var
Element: TElement;
begin
Condition.AddReference();
Result := 0;
for Element in Self do
if Condition.Matches(Element) then
Inc(Result);
Condition.ReleaseReference();
end;
constructor TCollection<TElement>.Create();
begin
inherited Create();
ElementManager := TReferenceUser<TElement>.Create();
end;
destructor TCollection<TElement>.Destroy();
begin
ElementManager.Free();
inherited Destroy();
end;
procedure TCollection<TElement>.FilterFrom(Source: TCollection<TElement>; Condition: TCondition<TElement>);
var
Element: TElement;
begin
Condition.AddReference();
for Element in Source do
if Condition.Matches(Element) then
Add(Element);
Condition.ReleaseReference();
end;
function TCollection<TElement>.Find(Condition: TCondition<TElement>): TElement;
var
Iterator: TIterator<TElement>;
begin
Condition.AddReference();
Result := Unassigned;
Iterator := CreateIterator();
while (Comparator.Compare(Result, Unassigned) = sZero) and Iterator.IsValid() do
begin
if Condition.Matches(Iterator.Current) then
Result := Iterator.Current
else
Iterator.GotoNext();
end;
Iterator.Free();
Condition.ReleaseReference();
end;
function TCollection<TElement>.GetCapacity(): TInteger32;
begin
raise ENotSupported.Create(ClassType, 'GetCapacity');
end;
function TCollection<TElement>.GetEnumerator(): TEnumerator<TElement>;
begin
Result := TIteratorEnumerator<TElement>.Create(CreateIterator());
end;
function TCollection<TElement>.GetMaximum(Comparator: TComparator<TElement>): TElement;
var
Candidate: TElement;
Iterator: TIterator<TElement>;
begin
Comparator.AddReference();
Iterator := CreateIterator();
if Iterator.IsValid then
begin
Result := Iterator.Current;
Iterator.GotoNext();
while Iterator.IsValid() do
begin
Candidate := Iterator.Current;
if Comparator.Compare(Candidate, Result) > sZero then
Result := Candidate;
Iterator.GotoNext();
end;
end
else
Result := Unassigned;
Iterator.Free();
Comparator.ReleaseReference();
end;
function TCollection<TElement>.GetMinimum(Comparator: TComparator<TElement>): TElement;
var
Candidate: TElement;
Iterator: TIterator<TElement>;
begin
Comparator.AddReference();
Iterator := CreateIterator();
if Iterator.IsValid then
begin
Result := Iterator.Current;
Iterator.GotoNext();
while Iterator.IsValid() do
begin
Candidate := Iterator.Current;
if Comparator.Compare(Candidate, Result) < sZero then
Result := Candidate;
Iterator.GotoNext();
end;
end
else
Result := Unassigned;
Iterator.Free();
Comparator.ReleaseReference();
end;
function TCollection<TElement>.MatchesAll(Condition: TCondition<TElement>): TBoolean;
var
Iterator: TIterator<TElement>;
begin
Condition.AddReference();
Iterator := CreateIterator();
Result := True;
while Result and Iterator.IsValid do
begin
Result := Condition.Matches(Iterator.Current);
Iterator.GotoNext();
end;
Iterator.Free();
Condition.ReleaseReference();
end;
procedure TCollection<TElement>.ReleaseElement(Element: TElement);
begin
ElementManager.Release(Element);
end;
procedure TCollection<TElement>.ReleaseElements();
var
Element: TElement;
begin
for Element in Self do
ReleaseElement(Element);
end;
function TCollection<TElement>.Remove(Element: TElement): TBoolean;
var
ExtractedElement: TElement;
begin
ExtractedElement := Extract(Element);
Result := Comparator.Compare(ExtractedElement, Unassigned) <> sZero;
if Result then
ReleaseElement(ExtractedElement);
end;
function TCollection<TElement>.Remove(Condition: TCondition<TElement>): TBoolean;
var
ExtractedElement: TElement;
begin
ExtractedElement := Extract(Condition);
Result := Comparator.Compare(ExtractedElement, Unassigned) <> sZero;
if Result then
ReleaseElement(ExtractedElement);
end;
procedure TCollection<TElement>.RemoveAll(Elements: TCollection<TElement>);
var
Element: TElement;
begin
for Element in Elements do
Remove(Element);
end;
procedure TCollection<TElement>.SetCapacity(const Value: TInteger32);
begin
raise ENotSupported.Create(ClassType, 'SetCapacity');
end;
procedure TCollection<TElement>.SetCount(const Value: TInteger32);
begin
raise ENotSupported.Create(ClassType, 'SetCount');
end;
procedure TCollection<TElement>.SetElementManager(const Value: TReferenceManager<TElement>);
begin
if Value <> ElementManager then
begin
FElementManager.Free();
FElementManager := Value;
end;
end;
function TCollection<TElement>.ToArray(): TArray<TElement>;
var
Index: TInteger32;
Iterator: TIterator<TElement>;
begin
SetLength(Result, Count);
Iterator := CreateIterator();
Index := 0;
while Iterator.IsValid() do
begin
Result[Index] := Iterator.Current;
Inc(Index);
Iterator.GotoNext();
end;
Iterator.Free();
end;
function TCollection<TElement>.ToText(): TText;
begin
Result := '#Elements: ' + PositiveInteger32ToText(Count);
end;
end.
|