-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNathan.TArrayHelper.pas
292 lines (236 loc) · 9.13 KB
/
Nathan.TArrayHelper.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
unit Nathan.TArrayHelper;
interface
uses
System.Classes,
System.SysUtils,
System.Generics.Collections,
System.Generics.Defaults;
{$M+}
type
// https://stackoverflow.com/questions/1600239/class-helper-for-generic-class
// https://stackoverflow.com/questions/22577119/arrays-in-tlist
// http://rvelthuis.de/articles/articles-openarr.html
// https://msdn.microsoft.com/de-de/library/system.array_methods(v=vs.110).aspx
// https://stackoverflow.com/questions/4922129/how-do-i-convert-an-array-to-a-listobject-in-c
// Helper looks like:
// https://msdn.microsoft.com/de-de/library/system.array_methods(v=vs.110).aspx
// https://msdn.microsoft.com/de-de/library/system.array(v=vs.110).aspx
TArrayHelperOption = (ahoNone, ahoIgnoreDuplicates);
TArrayHelperOptions = set of TArrayHelperOption;
TTArrayHelperNathan = class helper for TArray
public
class function ToString<T>(const Source: TArray<T>): string; static;
class function IndexOf<T>(const Source: array of T; const Value: T): Integer; overload;
class function IndexOf<T>(const Source: array of T; const Value: T; const Comparer: IComparer<T>): Integer; overload;
class function IndexOf<T>(const Source: array of T; const Value: T; const Comparer: IComparer<T>; StartIndex, Count: Integer): Integer; overload;
class function LastIndexOf<T>(const Source: array of T; const Value: T): Integer; overload;
class function LastIndexOf<T>(const Source: array of T; const Value: T; const Comparer: IComparer<T>): Integer; overload;
class function LastIndexOf<T>(const Source: array of T; const Value: T; const Comparer: IComparer<T>; Index, Count: Integer): Integer; overload;
class function ToList<T>(const Source: array of T): TList<T>;
class procedure Clear<T>(var Source: TArray<T>); overload;
class procedure Clear<T>(var Source: TArray<T>; Index, Count: Integer); overload;
class function Clone<T>(const Source: TArray<T>): TArray<T>;
class function Empty<T>(): TArray<T>;
class function Find<T>(const Source: TArray<T>; Predicate: TPredicate<T>): T;
class function FindAll<T>(const Source: TArray<T>; Predicate: TPredicate<T>): TArray<T>;
class function FindIndex<T>(const Source: TArray<T>; Predicate: TPredicate<T>): Integer; overload;
class function FindIndex<T>(const Source: TArray<T>; StartIndex: Integer; Predicate: TPredicate<T>): Integer; overload;
class function FindIndex<T>(const Source: TArray<T>; StartIndex, Count: Integer; Predicate: TPredicate<T>): Integer; overload;
class procedure ForEach<T>(Source: TArray<T>; Action: TProc<T>);
class function Add<T>(var Source: TArray<T>; const Value: T): Integer; overload;
class function Add<T>(var Source: TArray<T>; const Value: T; Options: TArrayHelperOptions): Integer; overload;
class procedure Remove<T>(var Source: TArray<T>; const Value: T);
class procedure RemoveAt<T>(var Source: TArray<T>; Index: Integer);
function Filter<T>(Predicate: TPredicate<T>): TArray<T>;
end;
{$M-}
implementation
uses
System.TypInfo,
System.Rtti,
System.RTLConsts;
{ TTArrayHelperNathan }
class function TTArrayHelperNathan.ToList<T>(const Source: array of T): TList<T>;
var
Idx: Integer;
begin
Result := TList<T>.Create();
for Idx := Low(Source) to High(Source) do
Result.Add(Source[Idx]);
end;
class function TTArrayHelperNathan.ToString<T>(const Source: TArray<T>): string;
var
Value: TValue;
Each: TValue;
Idx: Integer;
begin
{$REGION 'Infos'}
// if TypeInfo(T) = TypeInfo(string) then
// Writeln(PTypeInfo(TypeInfo(T))^.Name);
// GetTypeKind(T).
// Value := TValue.From<T>(Source);
// Result := Value.ToString;
// Value := TValue.From<T>(Source);
// if (Value.Kind = tkInteger) then
// Result := Value.AsString;
// Result := TValue.ToString(Value);
{$ENDREGION}
Result := '';
Value := TValue.From(Source);
for Idx := 0 to Value.GetArrayLength - 1 do
begin
if (Idx > 0) then
Result := Result + ', ';
Each := value.GetArrayElement(Idx);
Result := Result + Each.ToString;
end;
end;
class function TTArrayHelperNathan.IndexOf<T>(const Source: array of T; const Value: T): Integer;
begin
Result := IndexOf<T>(Source, Value, TComparer<T>.Default, 0, Length(Source))
end;
class function TTArrayHelperNathan.IndexOf<T>(const Source: array of T;
const Value: T; const Comparer: IComparer<T>): Integer;
begin
Result := IndexOf<T>(Source, Value, Comparer, 0, Length(Source))
end;
class function TTArrayHelperNathan.IndexOf<T>(const Source: array of T; const Value: T;
const Comparer: IComparer<T>; StartIndex, Count: Integer): Integer;
var
Idx: Integer;
begin
if (StartIndex < Low(Source)) or (StartIndex + Count > Length(Source)) then
raise EArgumentOutOfRangeException.CreateRes(@SArgumentOutOfRange);
Result := -1;
for Idx := StartIndex to Count - 1 do
if Comparer.Compare(Source[Idx], Value) = 0 then
Exit(Idx);
end;
class function TTArrayHelperNathan.LastIndexOf<T>(const Source: array of T; const Value: T): Integer;
begin
Result := LastIndexOf<T>(Source, Value, TComparer<T>.Default, 0, Length(Source))
end;
class function TTArrayHelperNathan.LastIndexOf<T>(const Source: array of T;
const Value: T; const Comparer: IComparer<T>): Integer;
begin
Result := LastIndexOf<T>(Source, Value, Comparer, 0, Length(Source))
end;
class function TTArrayHelperNathan.LastIndexOf<T>(const Source: array of T;
const Value: T; const Comparer: IComparer<T>; Index, Count: Integer): Integer;
var
Idx: Integer;
begin
if (Index < Low(Source)) or (Index + Count > Length(Source)) then
raise EArgumentOutOfRangeException.CreateRes(@SArgumentOutOfRange);
Result := -1;
for Idx := High(Source) downto Low(Source) do
if Comparer.Compare(Source[Idx], Value) = 0 then
Exit(Idx);
end;
class procedure TTArrayHelperNathan.Remove<T>(var Source: TArray<T>; const Value: T);
begin
RemoveAt<T>(Source, IndexOf(Source, Value));
end;
class procedure TTArrayHelperNathan.RemoveAt<T>(var Source: TArray<T>; Index: Integer);
begin
if (Index > Length(Source)) then
raise EArgumentOutOfRangeException.CreateRes(@SArgumentOutOfRange);
Delete(Source, Index, 1);
end;
class procedure TTArrayHelperNathan.Clear<T>(var Source: TArray<T>);
begin
Clear<T>(Source, Low(Source), Length(Source));
end;
class function TTArrayHelperNathan.Add<T>(var Source: TArray<T>; const Value: T): Integer;
begin
Result := Length(Source) + 1;
Insert(Value, Source, Result);
end;
class function TTArrayHelperNathan.Add<T>(var Source: TArray<T>; const Value: T; Options: TArrayHelperOptions): Integer;
begin
if (ahoIgnoreDuplicates in Options) then
begin
Result := IndexOf<T>(Source, Value);
if (Result > -1) then
Exit;
end;
Result := Length(Source) + 1;
Insert(Value, Source, Result);
end;
class procedure TTArrayHelperNathan.Clear<T>(var Source: TArray<T>; Index, Count: Integer);
begin
if (Count = 0) then
Exit;
Assert((Index >= Low(Source)) and (Index <= High(Source)));
Delete(Source, Index, Count);
end;
class function TTArrayHelperNathan.Clone<T>(const Source: TArray<T>): TArray<T>;
begin
SetLength(Result, Length(Source));
TArray.Copy<T>(Source, Result, Length(Source))
end;
class function TTArrayHelperNathan.Empty<T>: TArray<T>;
begin
Result := [];
end;
class function TTArrayHelperNathan.Find<T>(const Source: TArray<T>; Predicate: TPredicate<T>): T;
var
Idx: Integer;
begin
Result := Default(T);
for Idx := High(Source) downto Low(Source) do
begin
if Predicate(Source[Idx]) then
Exit(Source[Idx]);
end;
end;
class function TTArrayHelperNathan.FindAll<T>(const Source: TArray<T>; Predicate: TPredicate<T>): TArray<T>;
var
Idx: Integer;
begin
for Idx := Low(Source) to High(Source) do
begin
if Assigned(Predicate) and Predicate(Source[Idx]) then
Insert(Source[Idx], Result, Length(Result));
end;
end;
class function TTArrayHelperNathan.FindIndex<T>(const Source: TArray<T>; Predicate: TPredicate<T>): Integer;
begin
Result := FindIndex<T>(Source, 0, Predicate);
end;
class function TTArrayHelperNathan.FindIndex<T>(const Source: TArray<T>;
StartIndex: Integer; Predicate: TPredicate<T>): Integer;
begin
Result := FindIndex<T>(Source, StartIndex, Length(Source), Predicate);
end;
class function TTArrayHelperNathan.FindIndex<T>(const Source: TArray<T>;
StartIndex, Count: Integer; Predicate: TPredicate<T>): Integer;
var
Idx: Integer;
begin
if (StartIndex < Low(Source)) or (Count > Length(Source)) then
raise EArgumentOutOfRangeException.Create(SArgumentOutOfRange);
Result := -1;
for Idx := StartIndex to Count - 1 do
if Assigned(Predicate) and Predicate(Source[Idx]) then
Exit(Idx);
end;
class procedure TTArrayHelperNathan.ForEach<T>(Source: TArray<T>; Action: TProc<T>);
var
Idx: Integer;
begin
for Idx := High(Source) downto Low(Source) do
Action(Source[Idx]);
end;
function TTArrayHelperNathan.Filter<T>(Predicate: TPredicate<T>): TArray<T>;
var
Idx: Integer;
begin
Result := Empty<T>;
for Idx := High(TArray<T>(Self)) downto Low(TArray<T>(Self)) do
begin
if (Assigned(Predicate) and Predicate(TArray<T>(Self)[Idx])) then
Insert(TArray<T>(Self)[Idx], Result, Length(Result));
end;
end;
end.