-
Notifications
You must be signed in to change notification settings - Fork 188
/
Quick.Arrays.pas
428 lines (361 loc) · 9.98 KB
/
Quick.Arrays.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
{ ***************************************************************************
Copyright (c) 2016-2019 Kike Pérez
Unit : Quick.Arrays
Description : Multifuntional Arrays
Author : Kike Pérez
Version : 1.2
Created : 24/03/2019
Modified : 16/10/2019
This file is part of QuickLib: https://github.com/exilon/QuickLib
***************************************************************************
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 Quick.Arrays;
{$i QuickLib.inc}
interface
uses
SysUtils,
Generics.Defaults,
Generics.Collections,
Quick.Value;
type
TXArray<T> = record
type
TEnumerator = class(Generics.Collections.TEnumerator<T>)
private
fArray : ^TArray<T>;
fIndex : Integer;
protected
function DoGetCurrent: T; override;
function DoMoveNext: Boolean; override;
public
constructor Create(var aArray: TArray<T>);
end;
private type
arrayofT = array of T;
ParrayofT = ^arrayofT;
private
fArray : TArray<T>;
function GetItem(Index : Integer) : T;
procedure SetItem(Index : Integer; const aValue : T);
function GetCapacity: Integer;
function GetCount: Integer;
procedure SetCapacity(const Value: Integer);
function GetPArray: ParrayofT;
public
function GetEnumerator: TEnumerator<T>;
property AsArray : TArray<T> read fArray;
property Items[Index : Integer] : T read GetItem write SetItem; default;
property Count : Integer read GetCount;
property Capacity : Integer read GetCapacity write SetCapacity;
function Add(aValue : T) : Integer;
procedure Insert(aItem : T; aIndex : Integer);
procedure Delete(aIndex : Integer);
procedure Remove(aItem : T);
function Contains(aItem : T) : Boolean;
function IndexOf(aItem : T) : Integer;
property PArray : ParrayofT read GetPArray;
class operator Implicit(const Value : TxArray<T>) : TArray<T>;
class operator Implicit(const Value : TArray<T>) : TxArray<T>;
end;
TPair = record
Name : string;
Value : string;
constructor Create(const aName, aValue : string);
end;
TPairArray = TArray<TPair>;
PPairArray = ^TPairArray;
TPairXArray = TXArray<TPair>;
TFlexArray = TXArray<TFlexValue>;
TFlexPairArray = TArray<TFlexPair>;
TPairArrayHelper = record helper for TPairArray
public
function GetValue(const aName : string) : string;
function GetPair(const aName : string) : TPair;
function Add(aPair : TPair) : Integer; overload;
function Add(const aName, aValue : string) : Integer; overload;
procedure AddOrUpdate(const aName, aValue : string);
function Exists(const aName : string) : Boolean;
function Remove(const aName : string) : Boolean;
function Count : Integer;
property Items[const aName : string] : string read GetValue write AddOrUpdate;
end;
TFlexPairArrayHelper = record helper for TFlexPairArray
public
function GetValue(const aName : string) : TFlexValue;
function GetPair(const aName : string) : TFlexPair;
function Add(aFlexPair : TFlexPair) : Integer; overload;
function Add(const aName: string; aValue : TFlexValue): Integer; overload;
procedure AddOrUpdate(const aName : string; aValue : TFlexValue);
function Exists(const aName : string) : Boolean;
function Remove(const aName : string) : Boolean;
function Count : Integer;
property Items[const aName : string] : TFlexValue read GetValue write AddOrUpdate;
end;
implementation
{ TxArray }
function TxArray<T>.GetItem(Index : Integer) : T;
begin
Result := fArray[Index];
end;
function TXArray<T>.GetPArray: ParrayofT;
begin
Pointer(Result) := fArray;
end;
procedure TXArray<T>.SetCapacity(const Value: Integer);
begin
if Value = High(fArray) then Exit;
if Value < 0 then SetLength(fArray,1)
else SetLength(fArray, Value);
end;
procedure TxArray<T>.SetItem(Index : Integer; const aValue : T);
begin
fArray[Index] := aValue;
end;
function TXArray<T>.GetCapacity: Integer;
begin
Result := High(fArray) + 1;
end;
function TXArray<T>.GetCount: Integer;
begin
Result := High(fArray) + 1;
end;
function TxArray<T>.GetEnumerator: TEnumerator<T>;
begin
Result := TEnumerator.Create(fArray);
end;
function TxArray<T>.Add(aValue : T) : Integer;
begin
SetLength(fArray, Length(fArray) + 1);
fArray[High(fArray)] := aValue;
Result := High(fArray);
end;
procedure TxArray<T>.Delete(aIndex : Integer);
begin
System.Delete(fArray,aIndex,1);
end;
procedure TxArray<T>.Remove(aItem : T);
var
nindex : Integer;
begin
nindex := IndexOf(aItem);
if nindex > -1 then System.Delete(fArray,nindex,1);
end;
procedure TxArray<T>.Insert(aItem : T; aIndex : Integer);
begin
System.Insert(aItem,fArray,aIndex);
end;
function TxArray<T>.Contains(aItem : T) : Boolean;
var
icomparer : IEqualityComparer<T>;
i : Integer;
begin
Result := False;
icomparer := TEqualityComparer<T>.Default;
for i := Low(fArray) to High(fArray) do
begin
if icomparer.Equals(fArray[i],aItem) then Exit(True);
end;
end;
function TxArray<T>.IndexOf(aItem : T) : Integer;
var
icomparer : IEqualityComparer<T>;
i : Integer;
begin
icomparer := TEqualityComparer<T>.Default;
for i := Low(fArray) to High(fArray) do
begin
if icomparer.Equals(fArray[i],aItem) then Exit(i);
end;
Result := -1;
end;
class operator TxArray<T>.Implicit(const Value : TxArray<T>) : TArray<T>;
begin
Result := Value.fArray;
end;
class operator TXArray<T>.Implicit(const Value: TArray<T>): TxArray<T>;
begin
Result.fArray := Value;
end;
{ TXArray<T>.TEnumerator }
constructor TXArray<T>.TEnumerator.Create(var aArray: TArray<T>);
begin
fIndex := -1;
fArray := @aArray;
end;
function TXArray<T>.TEnumerator.DoGetCurrent: T;
begin
Result := TArray<T>(fArray^)[fIndex];
end;
function TXArray<T>.TEnumerator.DoMoveNext: Boolean;
begin
Inc(fIndex);
Result := fIndex < High(TArray<T>(fArray^))+1;
end;
{ TFlexPairArrayHelper }
function TFlexPairArrayHelper.Add(const aName: string; aValue : TFlexValue): Integer;
begin
SetLength(Self,Length(Self)+1);
Self[High(Self)].Name := aName;
Self[High(Self)].Value := aValue;
Result := High(Self);
end;
function TFlexPairArrayHelper.Count: Integer;
begin
Result := High(Self) + 1;
end;
function TFlexPairArrayHelper.Add(aFlexPair: TFlexPair): Integer;
begin
SetLength(Self,Length(Self)+1);
Self[High(Self)] := aFlexPair;
Result := High(Self);
end;
function TFlexPairArrayHelper.Exists(const aName: string): Boolean;
var
i : Integer;
begin
Result := False;
for i := Low(Self) to High(Self) do
begin
if CompareText(Self[i].Name,aName) = 0 then Exit(True)
end;
end;
function TFlexPairArrayHelper.GetPair(const aName: string): TFlexPair;
var
i : Integer;
begin
for i := Low(Self) to High(Self) do
begin
if CompareText(Self[i].Name,aName) = 0 then Exit(Self[i]);
end;
end;
function TFlexPairArrayHelper.GetValue(const aName: string): TFlexValue;
var
i : Integer;
begin
Result.Clear;
for i := Low(Self) to High(Self) do
begin
if CompareText(Self[i].Name,aName) = 0 then Exit(Self[i].Value);
end;
end;
function TFlexPairArrayHelper.Remove(const aName: string): Boolean;
var
i : Integer;
begin
for i := Low(Self) to High(Self) do
begin
if CompareText(Self[i].Name,aName) = 0 then
begin
System.Delete(Self,i,1);
Exit(True);
end;
end;
Result := False;
end;
procedure TFlexPairArrayHelper.AddOrUpdate(const aName : string; aValue : TFlexValue);
var
i : Integer;
begin
for i := Low(Self) to High(Self) do
begin
if CompareText(Self[i].Name,aName) = 0 then
begin
Self[i].Value := aValue;
Exit;
end;
end;
//if not exists add it
Self.Add(aName,aValue);
end;
{ TPair }
constructor TPair.Create(const aName, aValue: string);
begin
Name := aName;
Value := aValue;
end;
{ TPairArrayHelper }
function TPairArrayHelper.Add(aPair: TPair): Integer;
begin
SetLength(Self,Length(Self)+1);
Self[High(Self)] := aPair;
Result := High(Self);
end;
function TPairArrayHelper.Add(const aName, aValue: string): Integer;
begin
SetLength(Self,Length(Self)+1);
Self[High(Self)].Name := aName;
Self[High(Self)].Value := aValue;
Result := High(Self);
end;
procedure TPairArrayHelper.AddOrUpdate(const aName, aValue: string);
var
i : Integer;
begin
for i := Low(Self) to High(Self) do
begin
if CompareText(Self[i].Name,aName) = 0 then
begin
Self[i].Value := aValue;
Exit;
end;
end;
//if not exists add it
Self.Add(aName,aValue);
end;
function TPairArrayHelper.Count: Integer;
begin
Result := High(Self) + 1;
end;
function TPairArrayHelper.Exists(const aName: string): Boolean;
var
i : Integer;
begin
Result := False;
for i := Low(Self) to High(Self) do
begin
if CompareText(Self[i].Name,aName) = 0 then Exit(True)
end;
end;
function TPairArrayHelper.GetPair(const aName: string): TPair;
var
i : Integer;
begin
for i := Low(Self) to High(Self) do
begin
if CompareText(Self[i].Name,aName) = 0 then Exit(Self[i]);
end;
end;
function TPairArrayHelper.GetValue(const aName: string): string;
var
i : Integer;
begin
Result := '';
for i := Low(Self) to High(Self) do
begin
if CompareText(Self[i].Name,aName) = 0 then Exit(Self[i].Value);
end;
end;
function TPairArrayHelper.Remove(const aName: string): Boolean;
var
i : Integer;
begin
for i := Low(Self) to High(Self) do
begin
if CompareText(Self[i].Name,aName) = 0 then
begin
System.Delete(Self,i,1);
Exit(True);
end;
end;
Result := False;
end;
end.