forked from VSoftTechnologies/DUnitX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDUnitX.Generics.pas
443 lines (346 loc) · 12.1 KB
/
DUnitX.Generics.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
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
436
437
438
439
440
441
442
443
{***************************************************************************}
{ }
{ DUnitX }
{ }
{ Copyright (C) 2013 Vincent Parrett }
{ }
{ http://www.finalbuilder.com }
{ }
{ }
{***************************************************************************}
{ }
{ Licensed under the Apache License, Version 2.0 (the "License"); }
{ you may not use this file except in compliance with the License. }
{ You may obtain a copy of the License at }
{ }
{ http://www.apache.org/licenses/LICENSE-2.0 }
{ }
{ Unless required by applicable law or agreed to in writing, software }
{ distributed under the License is distributed on an "AS IS" BASIS, }
{ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
{ See the License for the specific language governing permissions and }
{ limitations under the License. }
{ }
{***************************************************************************}
unit DUnitX.Generics;
interface
uses
Generics.Defaults,
Generics.Collections;
{$I DUnitX.inc}
type
//Delphi does not have reference counted collection types.. so we created one here.
//This will typically be used where we return IEnumerbable<T> from a function
//TODO: need unit tests!!!
IReadOnlyList<T> = interface(IEnumerable<T>)
function GetCapacity : integer;
function GetCount : integer;
function GetItem(index : integer) : T;
function GetOnNotify : TCollectionNotifyEvent<T>;
procedure SetOnNotify(value : TCollectionNotifyEvent<T>);
function First: T;
function Last: T;
function Contains(const Value: T): Boolean;
function IndexOf(const Value: T): Integer;
function LastIndexOf(const Value: T): Integer;
function BinarySearch(const Item: T; out Index: Integer): Boolean; overload;
function BinarySearch(const Item: T; out Index: Integer; const AComparer: IComparer<T>): Boolean; overload;
property Capacity: Integer read GetCapacity;
property Count: Integer read GetCount;
property Items[Index: Integer]: T read GetItem; default;
property OnNotify: TCollectionNotifyEvent<T> read GetOnNotify write SetOnNotify;
end;
IList<T> = interface(IReadOnlyList<T>)
procedure SetCapacity(const value : integer);
procedure SetCount(const value : integer);
procedure SetItem(index : integer; value : T);
function Add(const Value: T): Integer;
procedure AddRange(const Values: array of T); overload;
procedure AddRange(const Collection: IEnumerable<T>); overload;
procedure AddRange(Collection: TEnumerable<T>); overload;
procedure Insert(Index: Integer; const Value: T);
procedure InsertRange(Index: Integer; const Values: array of T); overload;
procedure InsertRange(Index: Integer; const Collection: IEnumerable<T>); overload;
procedure InsertRange(Index: Integer; const Collection: TEnumerable<T>); overload;
function Remove(const Value: T): Integer;
procedure Delete(Index: Integer);
procedure DeleteRange(AIndex, ACount: Integer);
function Extract(const Value: T): T;
procedure Exchange(Index1, Index2: Integer);
procedure Move(CurIndex, NewIndex: Integer);
procedure Clear;
procedure Reverse;
procedure Sort; overload;
procedure Sort(const AComparer: IComparer<T>); overload;
procedure TrimExcess;
function ToArray: TArray<T>;
property Capacity: Integer read GetCapacity write SetCapacity;
property Count: Integer read GetCount write SetCount;
property Items[Index: Integer]: T read GetItem write SetItem; default;
property OnNotify: TCollectionNotifyEvent<T> read GetOnNotify write SetOnNotify;
end;
//Trying to implement IEnumerable<T> in the same class as IEnumerable does not
//seem to be possible in delphi.. it's and excercise in extreme frustration!
//Simple base IEnumerable Base implementation
TDUnitXEnumerable = class(TInterfacedObject, IEnumerable)
protected
function IEnumerable.GetEnumerator = GetNonGenEnumerator;
function GetNonGenEnumerator: IEnumerator; virtual; abstract;
end;
TDUnitXList<T> = class(TDUnitXEnumerable, IList<T>,IEnumerable<T>)
private
FList : TList<T>;
protected
//IEnumerable<T>
function GetEnumerator: IEnumerator<T>;overload;
function GetNonGenEnumerator: IEnumerator;override;
//IList<T>
function GetCapacity : integer;
procedure SetCapacity(const value : integer);
function GetCount : integer;
procedure SetCount(const value : integer);
function GetItem(index : integer) : T;
procedure SetItem(index : integer; value : T);
function GetOnNotify : TCollectionNotifyEvent<T>;
procedure SetOnNotify(value : TCollectionNotifyEvent<T>);
function Add(const Value: T): Integer;
procedure AddRange(const Values: array of T); overload;
procedure AddRange(const Collection: IEnumerable<T>); overload;
procedure AddRange(Collection: TEnumerable<T>); overload;
procedure Insert(Index: Integer; const Value: T);
procedure InsertRange(Index: Integer; const Values: array of T); overload;
procedure InsertRange(Index: Integer; const Collection: IEnumerable<T>); overload;
procedure InsertRange(Index: Integer; const Collection: TEnumerable<T>); overload;
function Remove(const Value: T): Integer;
procedure Delete(Index: Integer);
procedure DeleteRange(AIndex, ACount: Integer);
function Extract(const Value: T): T;
procedure Exchange(Index1, Index2: Integer);
procedure Move(CurIndex, NewIndex: Integer);
function First: T;
function Last: T;
procedure Clear;
function Contains(const Value: T): Boolean;
function IndexOf(const Value: T): Integer;
function LastIndexOf(const Value: T): Integer;
procedure Reverse;
procedure Sort; overload;
procedure Sort(const AComparer: IComparer<T>); overload;
function BinarySearch(const Item: T; out Index: Integer): Boolean; overload;
function BinarySearch(const Item: T; out Index: Integer; const AComparer: IComparer<T>): Boolean; overload;
procedure TrimExcess;
function ToArray: TArray<T>;
public
constructor Create; overload;
constructor Create(const AComparer: IComparer<T>); overload;
constructor Create(Collection: TEnumerable<T>); overload;
destructor Destroy; override;
end;
TDUnitXIEnumerator<T> = class(TInterfacedObject,IEnumerator<T>,IEnumerator)
private
FList : IList<T>;
FIndex : integer;
protected
function GetCurrentGen: T;
function IEnumerator<T>.GetCurrent = GetCurrentGen;
function GetCurrent: TObject;
function MoveNext: Boolean;
procedure Reset;
public
constructor Create(const AList : IList<T>);
end;
implementation
{ TListFactory }
{ TDUnitList<T> }
function TDUnitXList<T>.Add(const Value: T): Integer;
begin
result := FList.Add(Value);
end;
procedure TDUnitXList<T>.AddRange(const Collection: IEnumerable<T>);
begin
FList.AddRange(collection);
end;
procedure TDUnitXList<T>.AddRange(Collection: TEnumerable<T>);
begin
FList.AddRange(collection);
end;
procedure TDUnitXList<T>.AddRange(const Values: array of T);
begin
FList.AddRange(Values);
end;
function TDUnitXList<T>.BinarySearch(const Item: T; out Index: Integer; const AComparer: IComparer<T>): Boolean;
begin
result := FList.BinarySearch(Item,Index,AComparer);
end;
function TDUnitXList<T>.BinarySearch(const Item: T; out Index: Integer): Boolean;
begin
result := FList.BinarySearch(Item,Index);
end;
procedure TDUnitXList<T>.Clear;
begin
FList.Clear;
end;
function TDUnitXList<T>.Contains(const Value: T): Boolean;
begin
result := FList.Contains(Value);
end;
constructor TDUnitXList<T>.Create;
begin
FList := TList<T>.Create;
end;
constructor TDUnitXList<T>.Create(Collection: TEnumerable<T>);
begin
FList := TList<T>.Create(collection);
end;
constructor TDUnitXList<T>.Create(const AComparer: IComparer<T>);
begin
FList := TList<T>.Create(AComparer);
end;
procedure TDUnitXList<T>.Delete(Index: Integer);
begin
FList.Delete(index);
end;
procedure TDUnitXList<T>.DeleteRange(AIndex, ACount: Integer);
begin
FList.DeleteRange(AIndex,ACount);
end;
destructor TDUnitXList<T>.Destroy;
begin
FList.Destroy;
inherited;
end;
procedure TDUnitXList<T>.Exchange(Index1, Index2: Integer);
begin
FList.Exchange(Index1,Index2);
end;
function TDUnitXList<T>.Extract(const Value: T): T;
begin
result := FList.Extract(Value);
end;
function TDUnitXList<T>.First: T;
begin
result := FList.First;
end;
function TDUnitXList<T>.GetCapacity: integer;
begin
result := FList.Capacity;
end;
function TDUnitXList<T>.GetCount: integer;
begin
result := FList.Count;
end;
function TDUnitXList<T>.GetEnumerator: IEnumerator<T>;
begin
result := TDUnitXIEnumerator<T>.Create(Self);
end;
function TDUnitXList<T>.GetItem(index: integer): T;
begin
result := FList.Items[index];
end;
function TDUnitXList<T>.GetNonGenEnumerator: IEnumerator;
begin
result := nil;
end;
function TDUnitXList<T>.GetOnNotify: TCollectionNotifyEvent<T>;
begin
result := FList.OnNotify;
end;
function TDUnitXList<T>.IndexOf(const Value: T): Integer;
begin
result := FList.IndexOf(Value);
end;
procedure TDUnitXList<T>.Insert(Index: Integer; const Value: T);
begin
FList.Insert(Index,Value);
end;
procedure TDUnitXList<T>.InsertRange(Index: Integer; const Collection: TEnumerable<T>);
begin
FList.InsertRange(Index,Collection);
end;
procedure TDUnitXList<T>.InsertRange(Index: Integer; const Values: array of T);
begin
FList.InsertRange(Index,Values);
end;
procedure TDUnitXList<T>.InsertRange(Index: Integer; const Collection: IEnumerable<T>);
begin
FList.InsertRange(Index,Collection);
end;
function TDUnitXList<T>.Last: T;
begin
result := FList.Last;
end;
function TDUnitXList<T>.LastIndexOf(const Value: T): Integer;
begin
result := FList.LastIndexOf(Value);
end;
procedure TDUnitXList<T>.Move(CurIndex, NewIndex: Integer);
begin
FList.Move(CurIndex,NewIndex);
end;
function TDUnitXList<T>.Remove(const Value: T): Integer;
begin
result := FList.Remove(Value);
end;
procedure TDUnitXList<T>.Reverse;
begin
FList.Reverse;
end;
procedure TDUnitXList<T>.SetCapacity(const value: integer);
begin
FList.Capacity := value;
end;
procedure TDUnitXList<T>.SetCount(const value: integer);
begin
FList.Count := value;
end;
procedure TDUnitXList<T>.SetItem(index: integer; value: T);
begin
FList.Items[index] := value;
end;
procedure TDUnitXList<T>.SetOnNotify(value: TCollectionNotifyEvent<T>);
begin
FList.OnNotify := value;
end;
procedure TDUnitXList<T>.Sort(const AComparer: IComparer<T>);
begin
FList.Sort(AComparer);
end;
procedure TDUnitXList<T>.Sort;
begin
FList.Sort;
end;
function TDUnitXList<T>.ToArray: TArray<T>;
begin
result := nil; //FList.ToArray<T>;
end;
procedure TDUnitXList<T>.TrimExcess;
begin
FList.TrimExcess;
end;
{ TDUnitIEnumerator<T> }
constructor TDUnitXIEnumerator<T>.Create(const AList: IList<T>);
begin
FList := AList;
FIndex := -1;
end;
function TDUnitXIEnumerator<T>.GetCurrent: TObject;
begin
result := nil;
end;
function TDUnitXIEnumerator<T>.GetCurrentGen: T;
begin
Result := FList[FIndex];
end;
function TDUnitXIEnumerator<T>.MoveNext: Boolean;
begin
if FIndex >= FList.Count then
Exit(False);
Inc(FIndex);
Result := FIndex < FList.Count;
end;
procedure TDUnitXIEnumerator<T>.Reset;
begin
FIndex := -1;
end;
end.