Skip to content

Commit 312f900

Browse files
committed
Add demo project with examples from online docs
1 parent 0e4c957 commit 312f900

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+5211
-0
lines changed

Demos/ArrayUtilsDemo.dpr

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
{
2+
* Demo project for the DelphiDabbler.Lib.ArrayUtils unit.
3+
*
4+
* Contains copies the example code of all TArrayUtils methods included in the
5+
* documentation at https://delphidabbler.com/url/arrayutils-docs.
6+
*
7+
* This is a console application.
8+
*
9+
* Any copyright in this file is dedicated to the Public Domain.
10+
* http://creativecommons.org/publicdomain/zero/1.0/
11+
}
12+
13+
program ArrayUtilsDemo;
14+
15+
{$APPTYPE CONSOLE}
16+
17+
uses
18+
SysUtils,
19+
DelphiDabbler.Lib.ArrayUtils in '..\DelphiDabbler.Lib.ArrayUtils.pas',
20+
Registrar in 'Registrar.pas',
21+
Chop in 'Examples\Chop.pas',
22+
Compare in 'Examples\Compare.pas',
23+
Concat in 'Examples\Concat.pas',
24+
Contains in 'Examples\Contains.pas',
25+
Copy in 'Examples\Copy.pas',
26+
CopyReversed in 'Examples\CopyReversed.pas',
27+
CopySorted in 'Examples\CopySorted.pas',
28+
DeDup in 'Examples\DeDup.pas',
29+
Delete in 'Examples\Delete.pas',
30+
DeleteAndFree in 'Examples\DeleteAndFree.pas',
31+
DeleteAndFreeRange in 'Examples\DeleteAndFreeRange.pas',
32+
DeleteAndRelease in 'Examples\DeleteAndRelease.pas',
33+
DeleteAndReleaseRange in 'Examples\DeleteAndReleaseRange.pas',
34+
DeleteRange in 'Examples\DeleteRange.pas',
35+
Equal in 'Examples\Equal.pas',
36+
EqualStart in 'Examples\EqualStart.pas',
37+
Every in 'Examples\Every.pas',
38+
FindAll in 'Examples\FindAll.pas',
39+
FindAllIndices in 'Examples\FindAllIndices.pas',
40+
FindDef in 'Examples\FindDef.pas',
41+
FindIndex in 'Examples\FindIndex.pas',
42+
FindLastDef in 'Examples\FindLastDef.pas',
43+
FindLastIndex in 'Examples\FindLastIndex.pas',
44+
First in 'Examples\First.pas',
45+
ForEach in 'Examples\ForEach.pas',
46+
IndexOf in 'Examples\IndexOf.pas',
47+
IndicesOf in 'Examples\IndicesOf.pas',
48+
Insert in 'Examples\Insert.pas',
49+
Last in 'Examples\Last.pas',
50+
LastIndexOf in 'Examples\LastIndexOf.pas',
51+
Max in 'Examples\Max.pas',
52+
Min in 'Examples\Min.pas',
53+
MinMax in 'Examples\MinMax.pas',
54+
OccurrencesOf in 'Examples\OccurrencesOf.pas',
55+
Pick in 'Examples\Pick.pas',
56+
Pop in 'Examples\Pop.pas',
57+
PopAndFree in 'Examples\PopAndFree.pas',
58+
PopAndRelease in 'Examples\PopAndRelease.pas',
59+
Push in 'Examples\Push.pas',
60+
Reduce in 'Examples\Reduce.pas',
61+
Reverse in 'Examples\Reverse.pas',
62+
Shift in 'Examples\Shift.pas',
63+
ShiftAndFree in 'Examples\ShiftAndFree.pas',
64+
ShiftAndRelease in 'Examples\ShiftAndRelease.pas',
65+
Slice in 'Examples\Slice.pas',
66+
Some in 'Examples\Some.pas',
67+
Sort in 'Examples\Sort.pas',
68+
Transform in 'Examples\Transform.pas',
69+
TryFind in 'Examples\TryFind.pas',
70+
TryFindLast in 'Examples\TryFindLast.pas',
71+
UnShift in 'Examples\UnShift.pas';
72+
73+
var
74+
MethodName: string;
75+
MethodTitle: string;
76+
ProcInfo: TProcedureInfo;
77+
ProcTitle: string;
78+
79+
begin
80+
ReportMemoryLeaksOnShutdown := True;
81+
try
82+
for MethodName in TRegistrar.MethodNames do
83+
begin
84+
MethodTitle := 'TArrayUtils.' + MethodName;
85+
Writeln(MethodTitle);
86+
Writeln(StringOfChar('=', Length(MethodTitle)));
87+
Writeln;
88+
for ProcInfo in TRegistrar.GetProcs(MethodName) do
89+
begin
90+
ProcTitle := 'Running ' + ProcInfo.Name;
91+
Writeln(ProcTitle);
92+
Writeln(StringOfChar('-', Length(ProcTitle)));
93+
Writeln;
94+
// Call procedure
95+
ProcInfo.Proc;
96+
Writeln('DONE');
97+
Writeln;
98+
end;
99+
end;
100+
except
101+
on E: Exception do
102+
Writeln(E.ClassName, ': ', E.Message);
103+
end;
104+
Writeln;
105+
Writeln('END OF DEMO: press enter to exit');
106+
Readln;
107+
end.

Demos/ArrayUtilsDemo.dproj

Lines changed: 1151 additions & 0 deletions
Large diffs are not rendered by default.

Demos/Examples/Chop.pas

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
* Example code for TArrayUtils.Chop<T>.
3+
*
4+
* Contains copies the example code included in the Array Utilities Unit
5+
* documentation at https://delphidabbler.com/url/arrayutils-docs.
6+
*
7+
* Any copyright in this file is dedicated to the Public Domain.
8+
* http://creativecommons.org/publicdomain/zero/1.0/
9+
}
10+
11+
unit Chop;
12+
13+
interface
14+
15+
implementation
16+
17+
uses
18+
SysUtils,
19+
Generics.Defaults,
20+
DelphiDabbler.Lib.ArrayUtils,
21+
Registrar;
22+
23+
procedure Chop_Eg1;
24+
var
25+
A, ATest, ASlice, AExpectedSlice, AExpectedRemainder: TArray<string>;
26+
begin
27+
A := TArray<string>.Create('a', 'stitch', 'in', 'time', 'saves', 'nine');
28+
// slice from the start of A
29+
ATest := Copy(A);
30+
ASlice := TArrayUtils.Chop<string>(ATest, 0, 2);
31+
AExpectedRemainder := TArray<string>.Create('time', 'saves', 'nine');
32+
AExpectedSlice := TArray<string>.Create('a', 'stitch', 'in');
33+
Assert(TArrayUtils.Equal<string>(AExpectedRemainder, ATest, SameStr));
34+
Assert(TArrayUtils.Equal<string>(AExpectedSlice, ASlice, SameStr));
35+
// Chop in the middle of ATest
36+
ATest := Copy(A);
37+
ASlice := TArrayUtils.Chop<string>(ATest, 2, 4);
38+
AExpectedRemainder := TArray<string>.Create('a', 'stitch', 'nine');
39+
AExpectedSlice := TArray<string>.Create('in', 'time', 'saves');
40+
Assert(TArrayUtils.Equal<string>(AExpectedRemainder, ATest, SameStr));
41+
Assert(TArrayUtils.Equal<string>(AExpectedSlice, ASlice, SameStr));
42+
// Chop at the end of ATest
43+
ATest := Copy(A);
44+
ASlice := TArrayUtils.Chop<string>(ATest, 4, 5);
45+
AExpectedRemainder := TArray<string>.Create('a', 'stitch', 'in', 'time');
46+
AExpectedSlice := TArray<string>.Create('saves', 'nine');
47+
Assert(TArrayUtils.Equal<string>(AExpectedRemainder, ATest, SameStr));
48+
Assert(TArrayUtils.Equal<string>(AExpectedSlice, ASlice, SameStr));
49+
end;
50+
51+
procedure Chop_Eg2;
52+
var
53+
A, ATest, ASlice, AExpectedSlice, AExpectedRemainder: TArray<string>;
54+
begin
55+
A := TArray<string>.Create('a', 'stitch', 'in', 'time', 'saves', 'nine');
56+
// slice from mid to end of A
57+
ATest := Copy(A);
58+
ASlice := TArrayUtils.Chop<string>(ATest, 3);
59+
AExpectedRemainder := TArray<string>.Create('a', 'stitch', 'in');
60+
AExpectedSlice := TArray<string>.Create('time', 'saves', 'nine');
61+
Assert(TArrayUtils.Equal<string>(AExpectedRemainder, ATest, SameStr));
62+
Assert(TArrayUtils.Equal<string>(AExpectedSlice, ASlice, SameStr));
63+
// Chop of all of ATest
64+
ATest := Copy(A);
65+
ASlice := TArrayUtils.Chop<string>(ATest, 0);
66+
AExpectedRemainder := TArray<string>.Create();
67+
AExpectedSlice := Copy(A);
68+
Assert(TArrayUtils.Equal<string>(AExpectedRemainder, ATest, SameStr));
69+
Assert(TArrayUtils.Equal<string>(AExpectedSlice, ASlice, SameStr));
70+
end;
71+
72+
initialization
73+
74+
TRegistrar.Add(
75+
'Chop<T>',
76+
TArray<TProcedureInfo>.Create(
77+
TProcedureInfo.Create('Chop_Eg1', @Chop_Eg1),
78+
TProcedureInfo.Create('Chop_Eg2', @Chop_Eg2)
79+
)
80+
);
81+
82+
end.

Demos/Examples/Compare.pas

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
* Example code for TArrayUtils.Compare<T>.
3+
*
4+
* Contains copies the example code included in the Array Utilities Unit
5+
* documentation at https://delphidabbler.com/url/arrayutils-docs.
6+
*
7+
* Any copyright in this file is dedicated to the Public Domain.
8+
* http://creativecommons.org/publicdomain/zero/1.0/
9+
}
10+
11+
unit Compare;
12+
13+
interface
14+
15+
implementation
16+
17+
uses
18+
SysUtils,
19+
Generics.Defaults,
20+
DelphiDabbler.Lib.ArrayUtils,
21+
Registrar;
22+
23+
procedure Compare_Eg1;
24+
var
25+
A, B, C, D, E: TArray<Integer>;
26+
ComparerFn: TComparison<Integer>;
27+
begin
28+
A := TArray<Integer>.Create(1, 2, 3);
29+
B := TArray<Integer>.Create(1, 2, 3);
30+
C := TArray<Integer>.Create(1, 2);
31+
D := TArray<Integer>.Create(1, 5, 7);
32+
E := TArray<Integer>.Create(); // empty
33+
ComparerFn := function(const Left, Right: Integer): Integer
34+
begin
35+
Result := Left - Right;
36+
end;
37+
Assert(TArrayUtils.Compare<Integer>(A, B, ComparerFn) = 0);
38+
Assert(TArrayUtils.Compare<Integer>(A, C, ComparerFn) > 0);
39+
Assert(TArrayUtils.Compare<Integer>(A, D, ComparerFn) < 0);
40+
Assert(TArrayUtils.Compare<Integer>(A, E, ComparerFn) > 0);
41+
end;
42+
43+
procedure Compare_Eg2;
44+
var
45+
A, B, C, D, E: TArray<string>;
46+
ComparerObj: IComparer<string>;
47+
begin
48+
A := TArray<string>.Create('a', 'b', 'c');
49+
B := TArray<string>.Create('a', 'b', 'c');
50+
C := TArray<string>.Create('a', 'b');
51+
D := TArray<string>.Create('a', 'd', 'f');
52+
E := TArray<string>.Create(); // empty
53+
ComparerObj := TDelegatedComparer<string>.Create(CompareStr);
54+
Assert(TArrayUtils.Compare<string>(A, B, ComparerObj) = 0);
55+
Assert(TArrayUtils.Compare<string>(A, C, ComparerObj) > 0);
56+
Assert(TArrayUtils.Compare<string>(A, D, ComparerObj) < 0);
57+
Assert(TArrayUtils.Compare<string>(A, E, ComparerObj) > 0);
58+
end;
59+
60+
initialization
61+
62+
TRegistrar.Add(
63+
'Compare<T>',
64+
TArray<TProcedureInfo>.Create(
65+
TProcedureInfo.Create('Compare_Eg1', @Compare_Eg1),
66+
TProcedureInfo.Create('Compare_Eg2', @Compare_Eg2)
67+
)
68+
);
69+
70+
end.

Demos/Examples/Concat.pas

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
* Example code for TArrayUtils.Concat<T>.
3+
*
4+
* Contains copies the example code included in the Array Utilities Unit
5+
* documentation at https://delphidabbler.com/url/arrayutils-docs.
6+
*
7+
* Any copyright in this file is dedicated to the Public Domain.
8+
* http://creativecommons.org/publicdomain/zero/1.0/
9+
}
10+
11+
unit Concat;
12+
13+
interface
14+
15+
implementation
16+
17+
uses
18+
DelphiDabbler.Lib.ArrayUtils,
19+
Registrar;
20+
21+
procedure Concat_Eg;
22+
var
23+
A, B, Got, Expected: TArray<Integer>;
24+
begin
25+
A := TArray<Integer>.Create(1, 2, 3, 4);
26+
B := TArray<Integer>.Create(42, 56);
27+
Got := TArrayUtils.Concat<Integer>(A, B);
28+
Expected := TArray<Integer>.Create(1, 2, 3, 4, 42, 56);
29+
Assert(TArrayUtils.Equal<Integer>(Expected, Got));
30+
end;
31+
32+
initialization
33+
34+
TRegistrar.Add(
35+
'Concat<T>',
36+
TArray<TProcedureInfo>.Create(
37+
TProcedureInfo.Create('Concat_Eg', @Concat_Eg)
38+
)
39+
);
40+
41+
end.

Demos/Examples/Contains.pas

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
* Example code for TArrayUtils.Contains<T>.
3+
*
4+
* Contains copies the example code included in the Array Utilities Unit
5+
* documentation at https://delphidabbler.com/url/arrayutils-docs.
6+
*
7+
* Any copyright in this file is dedicated to the Public Domain.
8+
* http://creativecommons.org/publicdomain/zero/1.0/
9+
}
10+
11+
unit Contains;
12+
13+
interface
14+
15+
implementation
16+
17+
uses
18+
SysUtils,
19+
Generics.Defaults,
20+
DelphiDabbler.Lib.ArrayUtils,
21+
Registrar;
22+
23+
procedure Contains_Eg1;
24+
var
25+
A: TArray<Integer>;
26+
EqComparerFn: TEqualityComparison<Integer>;
27+
begin
28+
A := TArray<Integer>.Create(1, 2, 3);
29+
EqComparerFn := function(const Left, Right: Integer): Boolean
30+
begin
31+
Result := Left = Right;
32+
end;
33+
Assert(TArrayUtils.Contains<Integer>(2, A, EqComparerFn) = True);
34+
Assert(TArrayUtils.Contains<Integer>(4, A, EqComparerFn) = False);
35+
end;
36+
37+
procedure Contains_Eg2;
38+
var
39+
A: TArray<string>;
40+
EqComparerObj: IEqualityComparer<string>;
41+
begin
42+
A := TArray<string>.Create('a', 'b', 'c');
43+
EqComparerObj := TDelegatedEqualityComparer<string>.Create(
44+
SameStr,
45+
function(const Value: string): Integer
46+
begin
47+
// only do this if you KNOW the hash function won't be called
48+
Result := 0;
49+
end
50+
);
51+
Assert(TArrayUtils.Contains<string>('b', A, EqComparerObj) = True);
52+
Assert(TArrayUtils.Contains<string>('d', A, EqComparerObj) = False);
53+
end;
54+
55+
initialization
56+
57+
TRegistrar.Add(
58+
'Contains<T>',
59+
TArray<TProcedureInfo>.Create(
60+
TProcedureInfo.Create('Contains_Eg1', @Contains_Eg1),
61+
TProcedureInfo.Create('Contains_Eg2', @Contains_Eg2)
62+
)
63+
);
64+
65+
end.

0 commit comments

Comments
 (0)