-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUArraysCatSnippets.pas
550 lines (494 loc) · 15.1 KB
/
UArraysCatSnippets.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
{
* This unit was generated automatically. It incorporates a selection of source
* code taken from the Code Snippets Database at
* https://github.com/delphidabbler/code-snippets.
*
* The unit is copyright © 2005-2025 by Peter Johnson & Contributors and is
* licensed under the MIT License (https://opensource.org/licenses/MIT).
*
* Generated on : Fri, 17 Jan 2025 09:42:43 GMT.
* Generated by : DelphiDabbler CodeSnip Release 4.24.0.
*
* The latest version of CodeSnip is available from the CodeSnip GitHub project
* at https://github.com/delphidabbler/codesnip.
}
unit UArraysCatSnippets;
interface
uses
SysUtils, Classes, Types, Generics.Defaults;
{
Dynamic array of bytes.
Ensures TBytes is available on non-Unicode compilers that don't define it in
SysUtils.
}
type
{$IFDEF UNICODE}
TBytes = SysUtils.TBytes;
{$ELSE}
TBytes = array of Byte;
{$ENDIF}
{
A method-only advanced record that provides utility methods for working with
generic dynamic arrays.
}
type
TArrayUtils = record
public
// Returns first element of given array, which must not be empty.
class function First<T>(const A: array of T): T; static;
// Returns last element of given array, which must not be empty.
class function Last<T>(const A: array of T): T; static;
// Returns index of given item in given array or -1 if element no in array.
// Given equality comparer is used to compare array elements with Elem.
class function IndexOf<T>(const Item: T; const A: array of T;
const EqualityComparer: Generics.Defaults.TEqualityComparison<T>):
Integer; static;
// Checks if two given arrays have the same contents, in same order. Given
// equality comparer is used to compare array elements.
class function Equal<T>(const Left, Right: array of T;
const EqualityComparer: Generics.Defaults.TEqualityComparison<T>):
Boolean; static;
// Checks if the first Count elements of the given arrays are the same.
// Given equality comparer is used to compare array elements.
class function SameStart<T>(const Left, Right: array of T;
const Count: Integer;
const EqualityComparer: Generics.Defaults.TEqualityComparison<T>):
Boolean; static;
// Creates and returns a new array that is the reverse of the given array.
class function Reverse<T>(const A: array of T): TArray<T>; static;
// Returns the maximum value of array A, which must not be be empty. The
// given comparer must return -ve if its 1st argument is less than the 2nd
// argument, +ve if the reverse holds and zero if both arguments are equal.
class function Max<T>(const A: array of T; const Comparer: TComparison<T>):
T; static;
// Returns the minimum value of array A, which must not be be empty. The
// given comparer must return -ve if its 1st argument is less than the 2nd
// argument, +ve if the reverse holds and zero if both arguments are equal.
class function Min<T>(const A: array of T; const Comparer: TComparison<T>):
T; static;
// Finds the minimum and maximum value of array A, which must not be empty.
// The minimum and maximum are returned via the MinValue and MaxValue
// parameters respectively. The given comparer must return -ve if its 1st
// argument is less than the 2nd argument, +ve if the reverse holds and zero
// if both arguments are equal.
class procedure MinMax<T>(const A: array of T;
const Comparer: TComparison<T>; out MinValue, MaxValue: T); static;
end;
{
Appends array of bytes B2 to the end of byte array B1.
}
procedure AppendByteArray(var B1: TBytes; const B2: array of Byte);
{
Copies the elements of string array Strings to string list SL, replacing any
existing contents of SL.
}
procedure ArrayToStringList(const Strings: array of string;
const SL: Classes.TStrings);
{
Checks if two byte arrays are equal.
The arrays are equal if they have the same number of elements and elements at
the same position in the array are equal.
}
function ByteArraysEqual(const B1, B2: array of Byte): Boolean;
{
Checks if two byte arrays B1 and B2 are equal for the first Count elements.
False is returned if any array has less than Count elements.
Count must be >= 1.
}
function ByteArraysSameStart(const B1, B2: array of Byte; const Count: Integer):
Boolean;
{
Deletes a sequence of bytes from byte array B starting at index Start with
length Len.
If either Start or Len are less than 0 they are taken as zero. If Start is
beyond the end of the array or if Len is 0 then the whole array is returned
unchanged. If the sequence of bytes to be chopped extends beyond the end of
the array it is truncated from Start.
}
function ChopByteArray(const B: array of Byte; Start, Len: Integer):
TBytes;
{
Makes a copy of an array of bytes.
}
function CloneByteArray(const B: array of Byte): TBytes;
{
Concatenates two byte arrays B1 and B2 and returns the resulting array.
The result is the contents of B1 followed by the contents of B2.
}
function ConcatByteArrays(const B1, B2: array of Byte): TBytes;
{
Returns the index of the first occurrence of byte B in byte array A, or -1 if
B is not in A.
}
function IndexOfByte(const B: Byte; const A: array of Byte): Integer;
{
Returns the index of the last occurrence of byte B in byte array A, or -1 if B
is not in A.
}
function LastIndexOfByte(const B: Byte; const A: array of Byte): Integer;
{
Removes the last element of byte array A and returns the element. The length
of A shrinks by one.
A must not be empty.
}
function PopByteArray(var A: TBytes): Byte;
{
Pushes byte B onto the end of byte array A. The length of A grows by one.
}
procedure PushByteArray(const B: Byte; var A: TBytes);
{
Returns a copy of a given byte array with the order of the bytes reversed.
}
function ReverseByteArray(const A: array of Byte): TBytes;
{
Removes the first element of byte array A and returns the element. The length
of A shrinks by one.
A must not be empty.
}
function ShiftByteArray(var A: TBytes): Byte;
{
Slices a range of bytes from byte array B, starting at index Start with length
Len, and returns the result.
If either Start or Len are less than 0, they are taken as 0. If Start is
beyond the end of the array or if Len is 0 then an empty array is returned. If
the sequence of bytes to be sliced extends beyond the end of the array it is
truncated from Start.
}
function SliceByteArray(const B: array of Byte; Start, Len: Integer):
TBytes;
{
Creates and returns a dynamic string array containing all the strings from the
given string list.
}
function StringListToArray(const SL: Classes.TStrings): Types.TStringDynArray;
{
Inserts byte B at the beginning of byte array A. The length of A grows by one.
}
procedure UnShiftByteArray(const B: Byte; var A: TBytes);
implementation
{
Appends array of bytes B2 to the end of byte array B1.
}
procedure AppendByteArray(var B1: TBytes; const B2: array of Byte);
var
OldB1Len: Integer;
begin
if Length(B2) = 0 then
Exit;
OldB1Len := Length(B1);
SetLength(B1, OldB1Len + Length(B2));
Move(B2[0], B1[OldB1Len], Length(B2));
end;
{
Copies the elements of string array Strings to string list SL, replacing any
existing contents of SL.
}
procedure ArrayToStringList(const Strings: array of string;
const SL: Classes.TStrings);
var
Idx: Integer; // loops thru each string in array
begin
SL.Clear;
for Idx := 0 to Pred(Length(Strings)) do
SL.Add(Strings[Idx]);
end;
{
Checks if two byte arrays are equal.
The arrays are equal if they have the same number of elements and elements at
the same position in the array are equal.
}
function ByteArraysEqual(const B1, B2: array of Byte): Boolean;
var
I: Integer;
begin
Result := Length(B1) = Length(B2);
if Result then
begin
for I := 0 to High(B1) do
begin
if B1[I] <> B2[I] then
begin
Result := False;
Exit;
end;
end;
end;
end;
{
Checks if two byte arrays B1 and B2 are equal for the first Count elements.
False is returned if any array has less than Count elements.
Count must be >= 1.
}
function ByteArraysSameStart(const B1, B2: array of Byte; const Count: Integer):
Boolean;
var
I: Integer;
begin
Assert(Count > 0, 'Count must be >= 1');
Result := False;
if (Length(B1) < Count) or (Length(B2) < Count) then
Exit;
for I := 0 to Pred(Count) do
if B1[I] <> B2[I] then
Exit;
Result := True;
end;
{
Deletes a sequence of bytes from byte array B starting at index Start with
length Len.
If either Start or Len are less than 0 they are taken as zero. If Start is
beyond the end of the array or if Len is 0 then the whole array is returned
unchanged. If the sequence of bytes to be chopped extends beyond the end of
the array it is truncated from Start.
}
function ChopByteArray(const B: array of Byte; Start, Len: Integer):
TBytes;
var
First, Last: TBytes;
begin
if Start < 0 then
Start := 0;
if Len < 0 then
Len := 0
else if Start >= Length(B) then
Len := 0
else if Start + Len > Length(B) then
Len := Length(B) - Start;
First := SliceByteArray(B, 0, Start);
Last := SliceByteArray(B, Start + Len, Length(B));
Result := ConcatByteArrays(First, Last);
end;
{
Makes a copy of an array of bytes.
}
function CloneByteArray(const B: array of Byte): TBytes;
begin
SetLength(Result, Length(B));
if Length(B) > 0 then
Move(B[0], Result[0], Length(B));
end;
{
Concatenates two byte arrays B1 and B2 and returns the resulting array.
The result is the contents of B1 followed by the contents of B2.
}
function ConcatByteArrays(const B1, B2: array of Byte): TBytes;
begin
Result := CloneByteArray(B1);
AppendByteArray(Result, B2);
end;
{
Returns the index of the first occurrence of byte B in byte array A, or -1 if
B is not in A.
}
function IndexOfByte(const B: Byte; const A: array of Byte): Integer;
var
I: Integer;
begin
Result := -1;
for I := 0 to Pred(Length(A)) do
begin
if A[I] = B then
begin
Result := I;
Exit;
end;
end;
end;
{
Returns the index of the last occurrence of byte B in byte array A, or -1 if B
is not in A.
}
function LastIndexOfByte(const B: Byte; const A: array of Byte): Integer;
var
I: Integer;
begin
Result := -1;
for I := Pred(Length(A)) downto 0 do
begin
if A[I] = B then
begin
Result := I;
Exit;
end;
end;
end;
{
Removes the last element of byte array A and returns the element. The length
of A shrinks by one.
A must not be empty.
}
function PopByteArray(var A: TBytes): Byte;
begin
Assert(Length(A) > 0, 'A must be a non-empty array');
Result := A[Pred(Length(A))];
SetLength(A, Length(A) - 1);
end;
{
Pushes byte B onto the end of byte array A. The length of A grows by one.
}
procedure PushByteArray(const B: Byte; var A: TBytes);
begin
SetLength(A, Length(A) + 1);
A[Pred(Length(A))] := B;
end;
{
Returns a copy of a given byte array with the order of the bytes reversed.
}
function ReverseByteArray(const A: array of Byte): TBytes;
var
I: Integer;
begin
SetLength(Result, Length(A));
for I := 0 to High(A) do
Result[High(A)-I] := A[I];
end;
{
Removes the first element of byte array A and returns the element. The length
of A shrinks by one.
A must not be empty.
}
function ShiftByteArray(var A: TBytes): Byte;
begin
Assert(Length(A) > 0, 'A must be a non-empty array');
Result := A[0];
Move(A[1], A[0], Length(A) - 1);
SetLength(A, Length(A) - 1);
end;
{
Slices a range of bytes from byte array B, starting at index Start with length
Len, and returns the result.
If either Start or Len are less than 0, they are taken as 0. If Start is
beyond the end of the array or if Len is 0 then an empty array is returned. If
the sequence of bytes to be sliced extends beyond the end of the array it is
truncated from Start.
}
function SliceByteArray(const B: array of Byte; Start, Len: Integer):
TBytes;
begin
if Start < 0 then
Start := 0;
if Len < 0 then
Len := 0
else if Start >= Length(B) then
Len := 0
else if Start + Len > Length(B) then
Len := Length(B) - Start;
SetLength(Result, Len);
if Len > 0 then
Move(B[Start], Result[0], Len);
end;
{
Creates and returns a dynamic string array containing all the strings from the
given string list.
}
function StringListToArray(const SL: Classes.TStrings): Types.TStringDynArray;
var
Idx: Integer; // loops thru each string in SL
begin
SetLength(Result, SL.Count);
for Idx := 0 to Pred(SL.Count) do
Result[Idx] := SL[Idx];
end;
{
Inserts byte B at the beginning of byte array A. The length of A grows by one.
}
procedure UnShiftByteArray(const B: Byte; var A: TBytes);
begin
SetLength(A, Length(A) + 1);
Move(A[0], A[1], Length(A) - 1);
A[0] := B;
end;
class function TArrayUtils.Equal<T>(const Left, Right: array of T;
const EqualityComparer: Generics.Defaults.TEqualityComparison<T>): Boolean;
var
I: Integer;
begin
Result := Length(Left) = Length(Right);
if Result then
begin
for I := 0 to High(Left) do
if not EqualityComparer(Left[I], Right[I]) then
Exit(False);
end
end;
class function TArrayUtils.First<T>(const A: array of T): T;
begin
Assert(Length(A) > 0);
Result := A[0];
end;
class function TArrayUtils.IndexOf<T>(const Item: T; const A: array of T;
const EqualityComparer: Generics.Defaults.TEqualityComparison<T>): Integer;
var
Idx: Integer;
begin
for Idx := Low(A) to High(A) do
if EqualityComparer(A[Idx], Item) then
Exit(Idx);
Result := -1;
end;
class function TArrayUtils.Last<T>(const A: array of T): T;
begin
Assert(Length(A) > 0);
Result := A[Pred(Length(A))];
end;
class function TArrayUtils.Max<T>(const A: array of T;
const Comparer: TComparison<T>): T;
var
Idx: Integer;
begin
Assert(System.Length(A) > 0);
Result := A[0];
for Idx := 1 to Pred(System.Length(A)) do
if Comparer(A[Idx], Result) > 0 then
Result := A[Idx];
end;
class function TArrayUtils.Min<T>(const A: array of T;
const Comparer: TComparison<T>): T;
var
Idx: Integer;
begin
Assert(System.Length(A) > 0);
Result := A[0];
for Idx := 1 to Pred(System.Length(A)) do
if Comparer(A[Idx], Result) < 0 then
Result := A[Idx];
end;
class procedure TArrayUtils.MinMax<T>(const A: array of T;
const Comparer: TComparison<T>; out MinValue, MaxValue: T);
var
Idx: Integer;
begin
Assert(System.Length(A) > 0);
MinValue := A[0];
MaxValue := A[0];
for Idx := 1 to Pred(System.Length(A)) do
begin
if Comparer(A[Idx], MinValue) < 0 then
MinValue := A[Idx]
else if Comparer(A[Idx], MaxValue) > 0 then
MaxValue := A[Idx];
end;
end;
class function TArrayUtils.Reverse<T>(const A: array of T): TArray<T>;
var
I: Integer;
begin
SetLength(Result, Length(A));
for I := 0 to High(A) do
Result[High(A)-I] := A[I];
end;
class function TArrayUtils.SameStart<T>(const Left, Right: array of T;
const Count: Integer;
const EqualityComparer: Generics.Defaults.TEqualityComparison<T>): Boolean;
var
I: Integer;
begin
Assert(Count > 0, 'Count must be >= 1');
if (Length(Left) < Count) or (Length(Right) < Count) then
Exit(False);
for I := 0 to Pred(Count) do
if not EqualityComparer(Left[I], Right[I]) then
Exit(False);
Result := True;
end;
end.