mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-19 05:39:29 +02:00
Sparta: Remove examples of generics collections from Lazarus sources.
git-svn-id: trunk@62041 -
This commit is contained in:
parent
5446230d42
commit
8074bbd776
18
.gitattributes
vendored
18
.gitattributes
vendored
@ -4457,24 +4457,6 @@ components/sparta/dockedformeditor/source/spartaapi.pas svneol=native#text/pasca
|
||||
components/sparta/dockedformeditor/sparta_dockedformeditor.lpk svneol=native#text/plain
|
||||
components/sparta/dockedformeditor/sparta_dockedformeditor.pas svneol=native#text/pascal
|
||||
components/sparta/dockedformeditor/sparta_strconsts.pas svneol=native#text/pascal
|
||||
components/sparta/generics/examples/TArrayDouble/TArrayProjectDouble.lpi svneol=native#text/plain
|
||||
components/sparta/generics/examples/TArrayDouble/TArrayProjectDouble.lpr svneol=native#text/plain
|
||||
components/sparta/generics/examples/TArraySingle/TArrayProjectSingle.lpi svneol=native#text/plain
|
||||
components/sparta/generics/examples/TArraySingle/TArrayProjectSingle.lpr svneol=native#text/plain
|
||||
components/sparta/generics/examples/TComparer/TComparerProject.lpi svneol=native#text/plain
|
||||
components/sparta/generics/examples/TComparer/TComparerProject.lpr svneol=native#text/plain
|
||||
components/sparta/generics/examples/THashMap/THashMapProject.lpi svneol=native#text/plain
|
||||
components/sparta/generics/examples/THashMap/THashMapProject.lpr svneol=native#text/plain
|
||||
components/sparta/generics/examples/THashMapCaseInsensitive/THashMapCaseInsensitive.lpi svneol=native#text/plain
|
||||
components/sparta/generics/examples/THashMapCaseInsensitive/THashMapCaseInsensitive.lpr svneol=native#text/plain
|
||||
components/sparta/generics/examples/THashMapExtendedEqualityComparer/THashMapExtendedEqualityComparer.lpi svneol=native#text/plain
|
||||
components/sparta/generics/examples/THashMapExtendedEqualityComparer/THashMapExtendedEqualityComparer.lpr svneol=native#text/plain
|
||||
components/sparta/generics/examples/TObjectList/TObjectListProject.lpi svneol=native#text/plain
|
||||
components/sparta/generics/examples/TObjectList/TObjectListProject.lpr svneol=native#text/plain
|
||||
components/sparta/generics/examples/TQueue/TQueueProject.lpi svneol=native#text/plain
|
||||
components/sparta/generics/examples/TQueue/TQueueProject.lpr svneol=native#text/plain
|
||||
components/sparta/generics/examples/TStack/TStackProject.lpi svneol=native#text/plain
|
||||
components/sparta/generics/examples/TStack/TStackProject.lpr svneol=native#text/plain
|
||||
components/sparta/generics/source/generics.collections.pas svneol=native#text/pascal
|
||||
components/sparta/generics/source/generics.defaults.pas svneol=native#text/pascal
|
||||
components/sparta/generics/source/generics.hashes.pas svneol=native#text/pascal
|
||||
|
@ -1,66 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<CONFIG>
|
||||
<ProjectOptions>
|
||||
<Version Value="9"/>
|
||||
<PathDelim Value="\"/>
|
||||
<General>
|
||||
<Flags>
|
||||
<MainUnitHasCreateFormStatements Value="False"/>
|
||||
<MainUnitHasTitleStatement Value="False"/>
|
||||
</Flags>
|
||||
<SessionStorage Value="InProjectDir"/>
|
||||
<MainUnit Value="0"/>
|
||||
<Title Value="TArrayProjectDouble"/>
|
||||
<UseAppBundle Value="False"/>
|
||||
<ResourceType Value="res"/>
|
||||
</General>
|
||||
<i18n>
|
||||
<EnableI18N LFM="False"/>
|
||||
</i18n>
|
||||
<VersionInfo>
|
||||
<StringTable ProductVersion=""/>
|
||||
</VersionInfo>
|
||||
<BuildModes Count="1">
|
||||
<Item1 Name="Default" Default="True"/>
|
||||
</BuildModes>
|
||||
<PublishOptions>
|
||||
<Version Value="2"/>
|
||||
</PublishOptions>
|
||||
<RunParams>
|
||||
<local>
|
||||
<FormatVersion Value="1"/>
|
||||
</local>
|
||||
</RunParams>
|
||||
<Units Count="1">
|
||||
<Unit0>
|
||||
<Filename Value="TArrayProjectDouble.lpr"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
</Unit0>
|
||||
</Units>
|
||||
</ProjectOptions>
|
||||
<CompilerOptions>
|
||||
<Version Value="11"/>
|
||||
<PathDelim Value="\"/>
|
||||
<Target>
|
||||
<Filename Value="TArrayProjectDouble"/>
|
||||
</Target>
|
||||
<SearchPaths>
|
||||
<IncludeFiles Value="$(ProjOutDir)"/>
|
||||
<OtherUnitFiles Value="..\..\src"/>
|
||||
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
|
||||
</SearchPaths>
|
||||
</CompilerOptions>
|
||||
<Debugging>
|
||||
<Exceptions Count="3">
|
||||
<Item1>
|
||||
<Name Value="EAbort"/>
|
||||
</Item1>
|
||||
<Item2>
|
||||
<Name Value="ECodetoolError"/>
|
||||
</Item2>
|
||||
<Item3>
|
||||
<Name Value="EFOpenError"/>
|
||||
</Item3>
|
||||
</Exceptions>
|
||||
</Debugging>
|
||||
</CONFIG>
|
@ -1,91 +0,0 @@
|
||||
// Generic types for FreeSparta.com and FreePascal!
|
||||
// Original version by keeper89.blogspot.com, 2011
|
||||
// FPC version by Maciej Izak (hnb), 2014
|
||||
|
||||
program TArrayProjectDouble;
|
||||
|
||||
{$MODE DELPHI}
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
SysUtils, Math, Types, Generics.Collections, Generics.Defaults;
|
||||
|
||||
type
|
||||
TDoubleIntegerArray = array of TIntegerDynArray;
|
||||
|
||||
procedure PrintMatrix(A: TDoubleIntegerArray);
|
||||
var
|
||||
i, j: Integer;
|
||||
begin
|
||||
for i := Low(A) to High(A) do
|
||||
begin
|
||||
for j := Low(A[0]) to High(A[0]) do
|
||||
Write(A[i, j]: 3, ' ');
|
||||
Writeln;
|
||||
end;
|
||||
Writeln; Writeln;
|
||||
end;
|
||||
|
||||
function CustomCompare_1(constref Left, Right: TIntegerDynArray): Integer;
|
||||
begin
|
||||
Result := TCompare.Integer(Right[0], Left[0]);
|
||||
end;
|
||||
|
||||
function CustomCompare_2(constref Left, Right: TIntegerDynArray): Integer;
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
i := 0;
|
||||
repeat
|
||||
Result := TCompare.Integer(Right[i], Left[i]);
|
||||
Inc(i);
|
||||
until ((Result <> 0) or (i = Length(Left)));
|
||||
end;
|
||||
|
||||
var
|
||||
A: TDoubleIntegerArray;
|
||||
FoundIndex: Integer;
|
||||
i, j: Integer;
|
||||
|
||||
begin
|
||||
WriteLn('Working with TArray - a two-dimensional integer array');
|
||||
WriteLn;
|
||||
|
||||
// Fill integer array with random numbers [1 .. 50]
|
||||
SetLength(A, 4, 7);
|
||||
Randomize;
|
||||
for i := Low(A) to High(A) do
|
||||
for j := Low(A[0]) to High(A[0]) do
|
||||
A[i, j] := Math.RandomRange(1, 50);
|
||||
|
||||
// Equate some of the elements for further "cascade" sorting
|
||||
A[1, 0] := A[0, 0];
|
||||
A[2, 0] := A[0, 0];
|
||||
A[1, 1] := A[0, 1];
|
||||
|
||||
// Print out what happened
|
||||
Writeln('The original array:');
|
||||
PrintMatrix(A);
|
||||
|
||||
// ! FPC don't support anonymous methods yet
|
||||
//TArray.Sort<TIntegerDynArray>(A, TComparer<TIntegerDynArray>.Construct(
|
||||
// function (const Left, Right: TIntegerDynArray): Integer
|
||||
// begin
|
||||
// Result := Right[0] - Left[0];
|
||||
// end));
|
||||
// Sort descending 1st column, with cutom comparer_1
|
||||
TArrayHelper<TIntegerDynArray>.Sort(A, TComparer<TIntegerDynArray>.Construct(
|
||||
CustomCompare_1));
|
||||
Writeln('Descending in column 1:');
|
||||
PrintMatrix(A);
|
||||
|
||||
// Sort descending 1st column "cascade" -
|
||||
// If the line items are equal, compare neighboring
|
||||
TArrayHelper<TIntegerDynArray>.Sort(A, TComparer<TIntegerDynArray>.Construct(
|
||||
CustomCompare_2));
|
||||
Writeln('Cascade sorting, starting from the 1st column:');
|
||||
PrintMatrix(A);
|
||||
|
||||
Readln;
|
||||
end.
|
||||
|
@ -1,71 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<CONFIG>
|
||||
<ProjectOptions>
|
||||
<Version Value="9"/>
|
||||
<PathDelim Value="\"/>
|
||||
<General>
|
||||
<Flags>
|
||||
<MainUnitHasCreateFormStatements Value="False"/>
|
||||
<MainUnitHasTitleStatement Value="False"/>
|
||||
</Flags>
|
||||
<SessionStorage Value="InProjectDir"/>
|
||||
<MainUnit Value="0"/>
|
||||
<Title Value="TArrayProjectSingle"/>
|
||||
<UseAppBundle Value="False"/>
|
||||
<ResourceType Value="res"/>
|
||||
</General>
|
||||
<i18n>
|
||||
<EnableI18N LFM="False"/>
|
||||
</i18n>
|
||||
<VersionInfo>
|
||||
<StringTable ProductVersion=""/>
|
||||
</VersionInfo>
|
||||
<BuildModes Count="1">
|
||||
<Item1 Name="Default" Default="True"/>
|
||||
</BuildModes>
|
||||
<PublishOptions>
|
||||
<Version Value="2"/>
|
||||
</PublishOptions>
|
||||
<RunParams>
|
||||
<local>
|
||||
<FormatVersion Value="1"/>
|
||||
</local>
|
||||
</RunParams>
|
||||
<Units Count="1">
|
||||
<Unit0>
|
||||
<Filename Value="TArrayProjectSingle.lpr"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
</Unit0>
|
||||
</Units>
|
||||
</ProjectOptions>
|
||||
<CompilerOptions>
|
||||
<Version Value="11"/>
|
||||
<PathDelim Value="\"/>
|
||||
<Target>
|
||||
<Filename Value="TArrayProjectSingle"/>
|
||||
</Target>
|
||||
<SearchPaths>
|
||||
<IncludeFiles Value="$(ProjOutDir)"/>
|
||||
<OtherUnitFiles Value="..\..\src"/>
|
||||
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
|
||||
</SearchPaths>
|
||||
<Parsing>
|
||||
<SyntaxOptions>
|
||||
<SyntaxMode Value="Delphi"/>
|
||||
</SyntaxOptions>
|
||||
</Parsing>
|
||||
</CompilerOptions>
|
||||
<Debugging>
|
||||
<Exceptions Count="3">
|
||||
<Item1>
|
||||
<Name Value="EAbort"/>
|
||||
</Item1>
|
||||
<Item2>
|
||||
<Name Value="ECodetoolError"/>
|
||||
</Item2>
|
||||
<Item3>
|
||||
<Name Value="EFOpenError"/>
|
||||
</Item3>
|
||||
</Exceptions>
|
||||
</Debugging>
|
||||
</CONFIG>
|
@ -1,111 +0,0 @@
|
||||
// Generic types for FreeSparta.com and FreePascal!
|
||||
// Original version by keeper89.blogspot.com, 2011
|
||||
// FPC version by Maciej Izak (hnb), 2014
|
||||
|
||||
program TArrayProjectSingle;
|
||||
|
||||
{$MODE DELPHI}
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
SysUtils, Math, Types, Generics.Collections, Generics.Defaults;
|
||||
|
||||
function CompareIntReverse(constref Left, Right: Integer): Integer;
|
||||
begin
|
||||
Result := TCompare.Integer(Right, Left);
|
||||
end;
|
||||
|
||||
type
|
||||
TForCompare = class
|
||||
public
|
||||
function CompareIntReverseMethod(constref Left, Right: Integer): Integer;
|
||||
end;
|
||||
|
||||
function TForCompare.CompareIntReverseMethod(constref Left, Right: Integer): Integer;
|
||||
begin
|
||||
Result := TCompare.Integer(Right, Left);
|
||||
end;
|
||||
|
||||
procedure PrintMatrix(A: TIntegerDynArray);
|
||||
var
|
||||
item: Integer;
|
||||
begin
|
||||
for item in A do
|
||||
Write(item, ' ');
|
||||
Writeln; Writeln;
|
||||
end;
|
||||
|
||||
var
|
||||
A: TIntegerDynArray;
|
||||
FoundIndex: PtrInt;
|
||||
ForCompareObj: TForCompare;
|
||||
begin
|
||||
WriteLn('Working with TArray - one-dimensional integer array');
|
||||
WriteLn;
|
||||
|
||||
// Fill a one-dimensional array of integers by random numbers [1 .. 10]
|
||||
A := TIntegerDynArray.Create(1, 6, 3, 2, 9);
|
||||
|
||||
// Print out what happened
|
||||
Writeln('The original array:');
|
||||
PrintMatrix(A);
|
||||
|
||||
// Sort ascending without comparator
|
||||
TArrayHelper<Integer>.Sort(A);
|
||||
Writeln('Ascending Sort without parameters:');
|
||||
PrintMatrix(A);
|
||||
|
||||
// ! FPC don't support anonymous methods yet
|
||||
// Sort descending, the comparator is constructed
|
||||
// using an anonymous method
|
||||
//TArray.Sort<Integer>(A, TComparer<Integer>.Construct(
|
||||
// function (const Left, Right: Integer): Integer
|
||||
// begin
|
||||
// Result := Math.CompareValue(Right, Left)
|
||||
// end));
|
||||
|
||||
// Sort descending, the comparator is constructed
|
||||
// using an method
|
||||
TArrayHelper<Integer>.Sort(A, TComparer<Integer>.Construct(
|
||||
ForCompareObj.CompareIntReverseMethod));
|
||||
Writeln('Descending by TComparer<Integer>.Construct(ForCompareObj.Method):');
|
||||
PrintMatrix(A);
|
||||
|
||||
// Again sort ascending by using defaul
|
||||
TArrayHelper<Integer>.Sort(A, TComparer<Integer>.Default);
|
||||
Writeln('Ascending by TComparer<Integer>.Default:');
|
||||
PrintMatrix(A);
|
||||
|
||||
// Again descending using own comparator function
|
||||
TArrayHelper<Integer>.Sort(A, TComparer<Integer>.Construct(CompareIntReverse));
|
||||
Writeln('Descending by TComparer<Integer>.Construct(CompareIntReverse):');
|
||||
PrintMatrix(A);
|
||||
|
||||
// Searches for a nonexistent element
|
||||
Writeln('BinarySearch nonexistent element');
|
||||
if TArrayHelper<Integer>.BinarySearch(A, 5, FoundIndex) then
|
||||
Writeln('5 is found, its index ', FoundIndex)
|
||||
else
|
||||
Writeln('5 not found!');
|
||||
Writeln;
|
||||
|
||||
// Search for an existing item with default comparer
|
||||
Writeln('BinarySearch for an existing item ');
|
||||
if TArrayHelper<Integer>.BinarySearch(A, 6, FoundIndex) then
|
||||
Writeln('6 is found, its index ', FoundIndex)
|
||||
else
|
||||
Writeln('6 not found!');
|
||||
Writeln;
|
||||
|
||||
// Search for an existing item with custom comparer
|
||||
Writeln('BinarySearch for an existing item with custom comparer');
|
||||
if TArrayHelper<Integer>.BinarySearch(A, 6, FoundIndex,
|
||||
TComparer<Integer>.Construct(CompareIntReverse)) then
|
||||
Writeln('6 is found, its index ', FoundIndex)
|
||||
else
|
||||
Writeln('6 not found!');
|
||||
Writeln;
|
||||
|
||||
Readln;
|
||||
end.
|
||||
|
@ -1,66 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<CONFIG>
|
||||
<ProjectOptions>
|
||||
<Version Value="9"/>
|
||||
<PathDelim Value="\"/>
|
||||
<General>
|
||||
<Flags>
|
||||
<MainUnitHasCreateFormStatements Value="False"/>
|
||||
<MainUnitHasTitleStatement Value="False"/>
|
||||
</Flags>
|
||||
<SessionStorage Value="InProjectDir"/>
|
||||
<MainUnit Value="0"/>
|
||||
<Title Value="TComparerProject"/>
|
||||
<UseAppBundle Value="False"/>
|
||||
<ResourceType Value="res"/>
|
||||
</General>
|
||||
<i18n>
|
||||
<EnableI18N LFM="False"/>
|
||||
</i18n>
|
||||
<VersionInfo>
|
||||
<StringTable ProductVersion=""/>
|
||||
</VersionInfo>
|
||||
<BuildModes Count="1">
|
||||
<Item1 Name="Default" Default="True"/>
|
||||
</BuildModes>
|
||||
<PublishOptions>
|
||||
<Version Value="2"/>
|
||||
</PublishOptions>
|
||||
<RunParams>
|
||||
<local>
|
||||
<FormatVersion Value="1"/>
|
||||
</local>
|
||||
</RunParams>
|
||||
<Units Count="1">
|
||||
<Unit0>
|
||||
<Filename Value="TComparerProject.lpr"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
</Unit0>
|
||||
</Units>
|
||||
</ProjectOptions>
|
||||
<CompilerOptions>
|
||||
<Version Value="11"/>
|
||||
<PathDelim Value="\"/>
|
||||
<Target>
|
||||
<Filename Value="TComparerProject"/>
|
||||
</Target>
|
||||
<SearchPaths>
|
||||
<IncludeFiles Value="$(ProjOutDir)"/>
|
||||
<OtherUnitFiles Value="..\..\src"/>
|
||||
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
|
||||
</SearchPaths>
|
||||
</CompilerOptions>
|
||||
<Debugging>
|
||||
<Exceptions Count="3">
|
||||
<Item1>
|
||||
<Name Value="EAbort"/>
|
||||
</Item1>
|
||||
<Item2>
|
||||
<Name Value="ECodetoolError"/>
|
||||
</Item2>
|
||||
<Item3>
|
||||
<Name Value="EFOpenError"/>
|
||||
</Item3>
|
||||
</Exceptions>
|
||||
</Debugging>
|
||||
</CONFIG>
|
@ -1,124 +0,0 @@
|
||||
// Generic types for FreeSparta.com and FreePascal!
|
||||
// by Maciej Izak (hnb), 2014
|
||||
|
||||
program TComparerProject;
|
||||
|
||||
{$MODE DELPHI}
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
SysUtils, Generics.Collections, Generics.Defaults;
|
||||
|
||||
type
|
||||
|
||||
{ TCustomer }
|
||||
|
||||
TCustomer = record
|
||||
private
|
||||
FName: string;
|
||||
FMoney: Currency;
|
||||
public
|
||||
constructor Create(const Name: string; Money: Currency);
|
||||
property Name: string read FName write FName;
|
||||
property Money: Currency read FMoney write FMoney;
|
||||
function ToString: string;
|
||||
end;
|
||||
|
||||
TCustomerComparer = class(TComparer<TCustomer>)
|
||||
function Compare(constref Left, Right: TCustomer): Integer; override;
|
||||
end;
|
||||
|
||||
{ TCustomer }
|
||||
|
||||
constructor TCustomer.Create(const Name: string; Money: Currency);
|
||||
begin
|
||||
FName := Name;
|
||||
FMoney := Money;
|
||||
end;
|
||||
|
||||
function TCustomer.ToString: string;
|
||||
begin
|
||||
Result := Format('Name: %s >>> Money: %m', [Name, Money]);
|
||||
end;
|
||||
|
||||
// Ascending
|
||||
function TCustomerComparer.Compare(constref Left, Right: TCustomer): Integer;
|
||||
begin
|
||||
Result := TCompare.&String(Left.Name, Right.Name);
|
||||
if Result = 0 then
|
||||
Result := TCompare.Currency(Left.Money, Right.Money);
|
||||
end;
|
||||
|
||||
// Descending
|
||||
function CustomerCompare(constref Left, Right: TCustomer): Integer;
|
||||
begin
|
||||
Result := TCompare.&String(Right.Name, Left.Name);
|
||||
if Result = 0 then
|
||||
Result := TCompare.Currency(Right.Money, Left.Money);
|
||||
end;
|
||||
|
||||
var
|
||||
CustomersArray: TArray<TCustomer>;
|
||||
CustomersList: TList<TCustomer>;
|
||||
Comparer: TCustomerComparer;
|
||||
Customer: TCustomer;
|
||||
begin
|
||||
CustomersArray := TArray<TCustomer>.Create(
|
||||
TCustomer.Create('Derp', 2000),
|
||||
TCustomer.Create('Sheikh', 2000000000),
|
||||
TCustomer.Create('Derp', 1000),
|
||||
TCustomer.Create('Bill Gates', 1000000000));
|
||||
|
||||
Comparer := TCustomerComparer.Create;
|
||||
Comparer._AddRef;
|
||||
|
||||
// create TList with custom comparer
|
||||
CustomersList := TList<TCustomer>.Create(Comparer);
|
||||
CustomersList.AddRange(CustomersArray);
|
||||
|
||||
WriteLn('CustomersList before sort:');
|
||||
for Customer in CustomersList do
|
||||
WriteLn(Customer.ToString);
|
||||
WriteLn;
|
||||
|
||||
// default sort
|
||||
CustomersList.Sort; // will use TCustomerComparer (passed in the constructor)
|
||||
WriteLn('CustomersList after ascending sort (default with interface from constructor):');
|
||||
for Customer in CustomersList do
|
||||
WriteLn(Customer.ToString);
|
||||
WriteLn;
|
||||
|
||||
// construct with simple function
|
||||
CustomersList.Sort(TComparer<TCustomer>.Construct(CustomerCompare));
|
||||
WriteLn('CustomersList after descending sort (by using construct with function)');
|
||||
WriteLn('CustomersList.Sort(TComparer<TCustomer>.Construct(CustomerCompare)):');
|
||||
for Customer in CustomersList do
|
||||
WriteLn(Customer.ToString);
|
||||
WriteLn;
|
||||
|
||||
// construct with method
|
||||
CustomersList.Sort(TComparer<TCustomer>.Construct(Comparer.Compare));
|
||||
WriteLn('CustomersList after ascending sort (by using construct with method)');
|
||||
WriteLn('CustomersList.Sort(TComparer<TCustomer>.Construct(Comparer.Compare)):');
|
||||
for Customer in CustomersList do
|
||||
WriteLn(Customer.ToString);
|
||||
WriteLn;
|
||||
|
||||
WriteLn('CustomersArray before sort:');
|
||||
for Customer in CustomersArray do
|
||||
WriteLn(Customer.ToString);
|
||||
WriteLn;
|
||||
|
||||
// sort with interface
|
||||
TArrayHelper<TCustomer>.Sort(CustomersArray, TCustomerComparer.Create);
|
||||
WriteLn('CustomersArray after ascending sort (by using interfese - no construct)');
|
||||
WriteLn('TArrayHelper<TCustomer>.Sort(CustomersArray, TCustomerComparer.Create):');
|
||||
for Customer in CustomersArray do
|
||||
WriteLn(Customer.ToString);
|
||||
WriteLn;
|
||||
|
||||
CustomersList.Free;
|
||||
Comparer._Release;
|
||||
ReadLn;
|
||||
end.
|
||||
|
@ -1,71 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<CONFIG>
|
||||
<ProjectOptions>
|
||||
<Version Value="9"/>
|
||||
<PathDelim Value="\"/>
|
||||
<General>
|
||||
<Flags>
|
||||
<MainUnitHasCreateFormStatements Value="False"/>
|
||||
<MainUnitHasTitleStatement Value="False"/>
|
||||
</Flags>
|
||||
<SessionStorage Value="InProjectDir"/>
|
||||
<MainUnit Value="0"/>
|
||||
<Title Value="THashMapProject"/>
|
||||
<UseAppBundle Value="False"/>
|
||||
<ResourceType Value="res"/>
|
||||
</General>
|
||||
<i18n>
|
||||
<EnableI18N LFM="False"/>
|
||||
</i18n>
|
||||
<VersionInfo>
|
||||
<StringTable ProductVersion=""/>
|
||||
</VersionInfo>
|
||||
<BuildModes Count="1">
|
||||
<Item1 Name="Default" Default="True"/>
|
||||
</BuildModes>
|
||||
<PublishOptions>
|
||||
<Version Value="2"/>
|
||||
</PublishOptions>
|
||||
<RunParams>
|
||||
<local>
|
||||
<FormatVersion Value="1"/>
|
||||
</local>
|
||||
</RunParams>
|
||||
<Units Count="1">
|
||||
<Unit0>
|
||||
<Filename Value="THashMapProject.lpr"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
</Unit0>
|
||||
</Units>
|
||||
</ProjectOptions>
|
||||
<CompilerOptions>
|
||||
<Version Value="11"/>
|
||||
<PathDelim Value="\"/>
|
||||
<Target>
|
||||
<Filename Value="THashMapProject"/>
|
||||
</Target>
|
||||
<SearchPaths>
|
||||
<IncludeFiles Value="$(ProjOutDir)"/>
|
||||
<OtherUnitFiles Value="..\..\src"/>
|
||||
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
|
||||
</SearchPaths>
|
||||
<Parsing>
|
||||
<SyntaxOptions>
|
||||
<SyntaxMode Value="Delphi"/>
|
||||
</SyntaxOptions>
|
||||
</Parsing>
|
||||
</CompilerOptions>
|
||||
<Debugging>
|
||||
<Exceptions Count="3">
|
||||
<Item1>
|
||||
<Name Value="EAbort"/>
|
||||
</Item1>
|
||||
<Item2>
|
||||
<Name Value="ECodetoolError"/>
|
||||
</Item2>
|
||||
<Item3>
|
||||
<Name Value="EFOpenError"/>
|
||||
</Item3>
|
||||
</Exceptions>
|
||||
</Debugging>
|
||||
</CONFIG>
|
@ -1,218 +0,0 @@
|
||||
// Generic types for FreeSparta.com and FreePascal!
|
||||
// Original version by keeper89.blogspot.com, 2011
|
||||
// FPC version by Maciej Izak (hnb), 2014
|
||||
|
||||
program THashMapProject;
|
||||
|
||||
{$MODE DELPHI}
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
SysUtils, Generics.Collections, Generics.Defaults;
|
||||
|
||||
type
|
||||
TSubscriberInfo = record
|
||||
Name, SName: string;
|
||||
class function Create(const Name, SName: string): TSubscriberInfo; static;
|
||||
function ToString: string;
|
||||
end;
|
||||
|
||||
// Class containing handlers add / remove items in the dictionary
|
||||
THashMapEventsHandler = class
|
||||
public
|
||||
class procedure OnKeyNotify(Sender: TObject; constref Item: string;
|
||||
Action: TCollectionNotification);
|
||||
class procedure OnValueNotify(Sender: TObject; constref Item: TSubscriberInfo;
|
||||
Action: TCollectionNotification);
|
||||
end;
|
||||
|
||||
class function TSubscriberInfo.Create(const Name,
|
||||
SName: string): TSubscriberInfo;
|
||||
begin
|
||||
Result.Name := Name;
|
||||
Result.SName := SName;
|
||||
end;
|
||||
|
||||
function TSubscriberInfo.ToString: string;
|
||||
begin
|
||||
Result := Format('%s %s', [Name, SName]);
|
||||
end;
|
||||
|
||||
// Function to generate the dictionary contents into a string
|
||||
function PrintTelephoneDirectory(
|
||||
TelephoneDirectory: THashMap<string, TSubscriberInfo>): string;
|
||||
var
|
||||
PhoneNumber: string;
|
||||
begin
|
||||
Result := Format('Content directory (%d):', [TelephoneDirectory.Count]);
|
||||
|
||||
for PhoneNumber in TelephoneDirectory.Keys do
|
||||
Result := Result + Format(LineEnding + '%s: %s',
|
||||
[PhoneNumber, TelephoneDirectory[PhoneNumber].ToString]);
|
||||
end;
|
||||
|
||||
// Handlers add / remove items dictionary
|
||||
class procedure THashMapEventsHandler.OnKeyNotify(Sender: TObject;
|
||||
constref Item: string; Action: TCollectionNotification);
|
||||
begin
|
||||
case Action of
|
||||
cnAdded:
|
||||
Writeln(Format('OnKeyNotify! Phone %s added!', [Item]));
|
||||
cnRemoved:
|
||||
Writeln(Format('OnKeyNotify! Number %s deleted!', [Item]));
|
||||
end;
|
||||
end;
|
||||
|
||||
class procedure THashMapEventsHandler.OnValueNotify(Sender: TObject;
|
||||
constref Item: TSubscriberInfo; Action: TCollectionNotification);
|
||||
begin
|
||||
case Action of
|
||||
cnAdded:
|
||||
Writeln(Format('OnValueNotify! Subscriber %s added!', [Item.ToString]));
|
||||
cnRemoved:
|
||||
Writeln(Format('OnValueNotify! Subscriber %s deleted!', [Item.ToString]));
|
||||
end;
|
||||
end;
|
||||
|
||||
function CustomCompare(constref Left, Right: TPair<string, TSubscriberInfo>): Integer;
|
||||
begin
|
||||
// Comparable full first names, and then phones if necessary
|
||||
Result := TCompare.&String(Left.Value.ToString, Right.Value.ToString);
|
||||
if Result = 0 then
|
||||
Result := TCompare.&String(Left.Key, Right.Key);
|
||||
end;
|
||||
|
||||
var
|
||||
// Declare the "dictionary"
|
||||
// key is the telephone number which will be possible
|
||||
// to determine information about the owner
|
||||
TelephoneDirectory: THashMap<string, TSubscriberInfo>;
|
||||
TTelephoneArray: array of TPair<string, TSubscriberInfo>;
|
||||
TTelephoneArrayItem: TPair<string, TSubscriberInfo>;
|
||||
PhoneNumber: string;
|
||||
Subscriber: TSubscriberInfo;
|
||||
begin
|
||||
WriteLn('Working with THashMap - phonebook');
|
||||
WriteLn;
|
||||
|
||||
// create a directory
|
||||
// Constructor has several overloaded options which will
|
||||
// enable the capacity of the container, a comparator for values
|
||||
// or the initial data - we use the easiest option
|
||||
TelephoneDirectory := THashMap<string, TSubscriberInfo>.Create;
|
||||
|
||||
// ---------------------------------------------------
|
||||
// 1) Adding items to dictionary
|
||||
|
||||
// Add new users to the phonebook
|
||||
TelephoneDirectory.Add('9201111111', TSubscriberInfo.Create('Arnold', 'Schwarzenegger'));
|
||||
TelephoneDirectory.Add('9202222222', TSubscriberInfo.Create('Jessica', 'Alba'));
|
||||
TelephoneDirectory.Add('9203333333', TSubscriberInfo.Create('Brad', 'Pitt'));
|
||||
TelephoneDirectory.Add('9204444444', TSubscriberInfo.Create('Brad', 'Pitt'));
|
||||
TelephoneDirectory.Add('9205555555', TSubscriberInfo.Create('Sandra', 'Bullock'));
|
||||
// Adding a new subscriber if number already exist
|
||||
TelephoneDirectory.AddOrSetValue('9204444444',
|
||||
TSubscriberInfo.Create('Angelina', 'Jolie'));
|
||||
// Print list
|
||||
Writeln(PrintTelephoneDirectory(TelephoneDirectory));
|
||||
|
||||
// ---------------------------------------------------
|
||||
// 2) Working with the elements
|
||||
|
||||
// Set the "capacity" of the dictionary according to the current number of elements
|
||||
TelephoneDirectory.TrimExcess;
|
||||
// Is there a key? - ContainsKey
|
||||
if TelephoneDirectory.ContainsKey('9205555555') then
|
||||
Writeln('Phone 9205555555 registered!');
|
||||
// Is there a subscriber? - ContainsValue
|
||||
Subscriber := TSubscriberInfo.Create('Sandra', 'Bullock');
|
||||
if TelephoneDirectory.ContainsValue(Subscriber) then
|
||||
Writeln(Format('%s is in the directory!', [Subscriber.ToString]));
|
||||
// Try to get information via telephone. TryGetValue
|
||||
if TelephoneDirectory.TryGetValue('9204444444', Subscriber) then
|
||||
Writeln(Format('Number 9204444444 belongs to %s', [Subscriber.ToString]));
|
||||
// Directly access by phone number
|
||||
Writeln(Format('Phone 9201111111 subscribers: %s', [TelephoneDirectory['9201111111'].ToString]));
|
||||
// Number of people in the directory
|
||||
Writeln(Format('Total subscribers in the directory: %d', [TelephoneDirectory.Count]));
|
||||
|
||||
// ---------------------------------------------------
|
||||
// 3) Delete items
|
||||
|
||||
// Schwarzenegger now will not be listed
|
||||
TelephoneDirectory.Remove('9201111111');
|
||||
// Completely clear the list
|
||||
TelephoneDirectory.Clear;
|
||||
|
||||
// ---------------------------------------------------
|
||||
// 4) Events add / remove values
|
||||
//
|
||||
// Events OnKeyNotify OnValueNotify are designed for "tracking"
|
||||
// for adding / removing keys and values respectively
|
||||
TelephoneDirectory.OnKeyNotify := THashMapEventsHandler.OnKeyNotify;
|
||||
TelephoneDirectory.OnValueNotify := THashMapEventsHandler.OnValueNotify;
|
||||
|
||||
Writeln;
|
||||
// Try events
|
||||
TelephoneDirectory.Add('9201111111', TSubscriberInfo.Create('Arnold', 'Schwarzenegger'));
|
||||
TelephoneDirectory.Add('9202222222', TSubscriberInfo.Create('Jessica', 'Alba'));
|
||||
TelephoneDirectory['9202222222'] := TSubscriberInfo.Create('Monica', 'Bellucci');
|
||||
TelephoneDirectory.Clear;
|
||||
WriteLn;
|
||||
|
||||
TelephoneDirectory.Add('9201111111', TSubscriberInfo.Create('Monica', 'Bellucci'));
|
||||
TelephoneDirectory.Add('9202222222', TSubscriberInfo.Create('Sylvester', 'Stallone'));
|
||||
TelephoneDirectory.Add('9203333333', TSubscriberInfo.Create('Bruce', 'Willis'));
|
||||
WriteLn;
|
||||
|
||||
// Show keys (phones)
|
||||
Writeln('Keys (phones):');
|
||||
for PhoneNumber in TelephoneDirectory.Keys do
|
||||
Writeln(PhoneNumber);
|
||||
Writeln;
|
||||
|
||||
// Show values (subscribers)
|
||||
Writeln('Values (subscribers):');
|
||||
for Subscriber in TelephoneDirectory.Values do
|
||||
Writeln(Subscriber.ToString);
|
||||
Writeln;
|
||||
|
||||
// All together now
|
||||
Writeln('Subscribers list with phones:');
|
||||
for PhoneNumber in TelephoneDirectory.Keys do
|
||||
Writeln(Format('%s: %s',
|
||||
[PhoneNumber, TelephoneDirectory[PhoneNumber].ToString]));
|
||||
Writeln;
|
||||
|
||||
// In addition, we can "export" from the dictionary
|
||||
// to TArray
|
||||
// Sort the resulting array and display
|
||||
TTelephoneArray := TelephoneDirectory.ToArray;
|
||||
|
||||
// partial specializations not allowed
|
||||
// same for anonymous methods
|
||||
//TArray.Sort<TPair<string, TSubscriberInfo>>(
|
||||
// TTelephoneArray, TComparer<TPair<string, TSubscriberInfo>>.Construct(
|
||||
// function (const Left, Right: TPair<string, TSubscriberInfo>): Integer
|
||||
// begin
|
||||
// // Comparable full first names, and then phones if necessary
|
||||
// Result := CompareStr(Left.Value.ToString, Right.Value.ToString);
|
||||
// if Result = 0 then
|
||||
// Result := CompareStr(Left.Key, Right.Key);
|
||||
// end));
|
||||
|
||||
TArrayHelper<TelephoneDirectory.TDictionaryPair>.Sort(
|
||||
TTelephoneArray, TComparer<TelephoneDirectory.TDictionaryPair>.Construct(
|
||||
CustomCompare));
|
||||
// Print
|
||||
Writeln('Sorted list of subscribers into TArray (by name, and eventually by phone):');
|
||||
for TTelephoneArrayItem in TTelephoneArray do
|
||||
Writeln(Format('%s: %s',
|
||||
[TTelephoneArrayItem.Value.ToString, TTelephoneArrayItem.Key]));
|
||||
|
||||
Writeln;
|
||||
FreeAndNil(TelephoneDirectory);
|
||||
|
||||
Readln;
|
||||
end.
|
||||
|
@ -1,66 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<CONFIG>
|
||||
<ProjectOptions>
|
||||
<Version Value="9"/>
|
||||
<PathDelim Value="\"/>
|
||||
<General>
|
||||
<Flags>
|
||||
<MainUnitHasCreateFormStatements Value="False"/>
|
||||
<MainUnitHasTitleStatement Value="False"/>
|
||||
</Flags>
|
||||
<SessionStorage Value="InProjectDir"/>
|
||||
<MainUnit Value="0"/>
|
||||
<Title Value="THashMapCaseInsensitive"/>
|
||||
<UseAppBundle Value="False"/>
|
||||
<ResourceType Value="res"/>
|
||||
</General>
|
||||
<i18n>
|
||||
<EnableI18N LFM="False"/>
|
||||
</i18n>
|
||||
<VersionInfo>
|
||||
<StringTable ProductVersion=""/>
|
||||
</VersionInfo>
|
||||
<BuildModes Count="1">
|
||||
<Item1 Name="Default" Default="True"/>
|
||||
</BuildModes>
|
||||
<PublishOptions>
|
||||
<Version Value="2"/>
|
||||
</PublishOptions>
|
||||
<RunParams>
|
||||
<local>
|
||||
<FormatVersion Value="1"/>
|
||||
</local>
|
||||
</RunParams>
|
||||
<Units Count="1">
|
||||
<Unit0>
|
||||
<Filename Value="THashMapCaseInsensitive.lpr"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
</Unit0>
|
||||
</Units>
|
||||
</ProjectOptions>
|
||||
<CompilerOptions>
|
||||
<Version Value="11"/>
|
||||
<PathDelim Value="\"/>
|
||||
<Target>
|
||||
<Filename Value="THashMapCaseInsensitive"/>
|
||||
</Target>
|
||||
<SearchPaths>
|
||||
<IncludeFiles Value="$(ProjOutDir)"/>
|
||||
<OtherUnitFiles Value="..\..\src"/>
|
||||
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
|
||||
</SearchPaths>
|
||||
</CompilerOptions>
|
||||
<Debugging>
|
||||
<Exceptions Count="3">
|
||||
<Item1>
|
||||
<Name Value="EAbort"/>
|
||||
</Item1>
|
||||
<Item2>
|
||||
<Name Value="ECodetoolError"/>
|
||||
</Item2>
|
||||
<Item3>
|
||||
<Name Value="EFOpenError"/>
|
||||
</Item3>
|
||||
</Exceptions>
|
||||
</Debugging>
|
||||
</CONFIG>
|
@ -1,55 +0,0 @@
|
||||
// Generic types for FreeSparta.com and FreePascal!
|
||||
// by Maciej Izak (hnb), 2014
|
||||
|
||||
program THashMapCaseInsensitive;
|
||||
|
||||
{$MODE DELPHI}
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
Generics.Collections, Generics.Defaults;
|
||||
|
||||
var
|
||||
StringMap: THashMap<String, TEmptyRecord>;
|
||||
AnsiStringMap: THashMap<AnsiString, TEmptyRecord>;
|
||||
UnicodeStringMap: THashMap<UnicodeString, TEmptyRecord>;
|
||||
AdvancedHashMapWithBigLoadFactor: TCuckooD6<RawByteString, TEmptyRecord>;
|
||||
k: String;
|
||||
begin
|
||||
WriteLn('Working with case insensitive THashMap');
|
||||
WriteLn;
|
||||
// example constructors for different string types
|
||||
StringMap := THashMap<String, TEmptyRecord>.Create(TIStringComparer.Ordinal);
|
||||
StringMap.Free;
|
||||
AnsiStringMap := THashMap<AnsiString, TEmptyRecord>.Create(TIAnsiStringComparer.Ordinal);
|
||||
AnsiStringMap.Free;
|
||||
UnicodeStringMap := THashMap<UnicodeString, TEmptyRecord>.Create(TIUnicodeStringComparer.Ordinal);
|
||||
UnicodeStringMap.Free;
|
||||
|
||||
// standard TI*Comparer is dedicated for MAX_HASHLIST_COUNT = 4 and lower. For example DArrayCuckoo where D = 6
|
||||
// we need to create extra specialized TGIStringComparer type
|
||||
AdvancedHashMapWithBigLoadFactor := TCuckooD6<RawByteString, TEmptyRecord>.Create(
|
||||
TGIStringComparer<RawByteString, TDelphiSixfoldHashFactory>.Ordinal);
|
||||
AdvancedHashMapWithBigLoadFactor.Free;
|
||||
|
||||
// ok lets start
|
||||
// another way to create case insensitive hash map
|
||||
StringMap := THashMap<String, TEmptyRecord>.Create(TGIStringComparer<String>.Ordinal);
|
||||
|
||||
WriteLn('Add Cat and Dog');
|
||||
StringMap.Add('Cat', EmptyRecord);
|
||||
StringMap.Add('Dog', EmptyRecord);
|
||||
|
||||
//
|
||||
WriteLn('Contains CAT = ', StringMap.ContainsKey('CAT'));
|
||||
WriteLn('Contains dOG = ', StringMap.ContainsKey('dOG'));
|
||||
WriteLn('Contains Fox = ', StringMap.ContainsKey('Fox'));
|
||||
|
||||
WriteLn('Enumerate all keys :');
|
||||
for k in StringMap.Keys do
|
||||
WriteLn(' > ', k);
|
||||
|
||||
ReadLn;
|
||||
StringMap.Free;
|
||||
end.
|
||||
|
@ -1,66 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<CONFIG>
|
||||
<ProjectOptions>
|
||||
<Version Value="9"/>
|
||||
<PathDelim Value="\"/>
|
||||
<General>
|
||||
<Flags>
|
||||
<MainUnitHasCreateFormStatements Value="False"/>
|
||||
<MainUnitHasTitleStatement Value="False"/>
|
||||
</Flags>
|
||||
<SessionStorage Value="InProjectDir"/>
|
||||
<MainUnit Value="0"/>
|
||||
<Title Value="THashMapExtendedEqualityComparer"/>
|
||||
<UseAppBundle Value="False"/>
|
||||
<ResourceType Value="res"/>
|
||||
</General>
|
||||
<i18n>
|
||||
<EnableI18N LFM="False"/>
|
||||
</i18n>
|
||||
<VersionInfo>
|
||||
<StringTable ProductVersion=""/>
|
||||
</VersionInfo>
|
||||
<BuildModes Count="1">
|
||||
<Item1 Name="Default" Default="True"/>
|
||||
</BuildModes>
|
||||
<PublishOptions>
|
||||
<Version Value="2"/>
|
||||
</PublishOptions>
|
||||
<RunParams>
|
||||
<local>
|
||||
<FormatVersion Value="1"/>
|
||||
</local>
|
||||
</RunParams>
|
||||
<Units Count="1">
|
||||
<Unit0>
|
||||
<Filename Value="THashMapExtendedEqualityComparer.lpr"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
</Unit0>
|
||||
</Units>
|
||||
</ProjectOptions>
|
||||
<CompilerOptions>
|
||||
<Version Value="11"/>
|
||||
<PathDelim Value="\"/>
|
||||
<Target>
|
||||
<Filename Value="THashMapExtendedEqualityComparer"/>
|
||||
</Target>
|
||||
<SearchPaths>
|
||||
<IncludeFiles Value="$(ProjOutDir)"/>
|
||||
<OtherUnitFiles Value="..\..\src"/>
|
||||
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
|
||||
</SearchPaths>
|
||||
</CompilerOptions>
|
||||
<Debugging>
|
||||
<Exceptions Count="3">
|
||||
<Item1>
|
||||
<Name Value="EAbort"/>
|
||||
</Item1>
|
||||
<Item2>
|
||||
<Name Value="ECodetoolError"/>
|
||||
</Item2>
|
||||
<Item3>
|
||||
<Name Value="EFOpenError"/>
|
||||
</Item3>
|
||||
</Exceptions>
|
||||
</Debugging>
|
||||
</CONFIG>
|
@ -1,108 +0,0 @@
|
||||
// Generic types for FreeSparta.com and FreePascal!
|
||||
// by Maciej Izak (hnb), 2014
|
||||
|
||||
program THashMapExtendedEqualityComparer;
|
||||
|
||||
{$MODE DELPHI}
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
SysUtils, Generics.Collections, Generics.Defaults;
|
||||
|
||||
type
|
||||
|
||||
{ TTaxPayer }
|
||||
|
||||
TTaxPayer = record
|
||||
TaxID: Integer;
|
||||
Name: string;
|
||||
|
||||
constructor Create(ATaxID: Integer; const AName: string);
|
||||
function ToString: string;
|
||||
end;
|
||||
|
||||
constructor TTaxPayer.Create(ATaxID: Integer; const AName: string);
|
||||
begin
|
||||
TaxID := ATaxID;
|
||||
Name := AName;
|
||||
end;
|
||||
|
||||
function TTaxPayer.ToString: string;
|
||||
begin
|
||||
Result := Format('TaxID = %-10d Name = %-17s', [TaxID, Name]);
|
||||
end;
|
||||
|
||||
function EqualityComparison(constref ALeft, ARight: TTaxPayer): Boolean;
|
||||
begin
|
||||
Result := ALeft.TaxID = ARight.TaxID;
|
||||
end;
|
||||
|
||||
procedure ExtendedHasher(constref AValue: TTaxPayer; AHashList: PUInt32);
|
||||
begin
|
||||
// don't work with TCuckooD6 map because default TCuckooD6 needs TDelphiSixfoldHashFactory
|
||||
// and TDefaultHashFactory = TDelphiQuadrupleHashFactory
|
||||
// (TDelphiQuadrupleHashFactory is compatible with TDelphiDoubleHashFactory and TDelphiHashFactory)
|
||||
TDefaultHashFactory.GetHashList(@AValue.TaxID, SizeOf(Integer), AHashList);
|
||||
end;
|
||||
|
||||
var
|
||||
map: THashMap<TTaxPayer, string>; // THashMap = TCuckooD4
|
||||
LTaxPayer: TTaxPayer;
|
||||
LSansa: TTaxPayer;
|
||||
LPair: TPair<TTaxPayer, string>;
|
||||
begin
|
||||
WriteLn('program of tax office - ExtendedEqualityComparer for THashMap');
|
||||
WriteLn;
|
||||
|
||||
// to identify the taxpayer need only nip
|
||||
map := THashMap<TTaxPayer, string>.Create(
|
||||
TExtendedEqualityComparer<TTaxPayer>.Construct(EqualityComparison, ExtendedHasher));
|
||||
|
||||
map.Add(TTaxPayer.Create(1234567890, 'Joffrey Baratheon'), 'guilty');
|
||||
map.Add(TTaxPayer.Create(90, 'Little Finger'), 'swindler');
|
||||
map.Add(TTaxPayer.Create(667, 'John Snow'), 'delinquent tax');
|
||||
|
||||
// useless in this place but we can convert Keys to TArray<TKey> :)
|
||||
WriteLn(Format('All taxpayers (count = %d)', [Length(map.Keys.ToArray)]));
|
||||
for LTaxPayer in map.Keys do
|
||||
WriteLn(' > ', LTaxPayer.ToString);
|
||||
|
||||
LSansa := TTaxPayer.Create(667, 'Sansa Stark');
|
||||
|
||||
// exist because custom EqualityComparison and ExtendedHasher
|
||||
WriteLn;
|
||||
WriteLn(LSansa.Name, ' exist in map = ', map.ContainsKey(LSansa));
|
||||
WriteLn;
|
||||
|
||||
//
|
||||
WriteLn('All taxpayers');
|
||||
for LPair in map do
|
||||
WriteLn(' > ', LPair.Key.ToString, ' is ', LPair.Value);
|
||||
|
||||
// Add or set sansa? :)
|
||||
WriteLn;
|
||||
WriteLn(Format('AddOrSet(%s, ''innocent'')', [LSansa.ToString]));
|
||||
map.AddOrSetValue(LSansa, 'innocent');
|
||||
WriteLn;
|
||||
|
||||
//
|
||||
WriteLn('All taxpayers');
|
||||
for LPair in map do
|
||||
WriteLn(' > ', LPair.Key.ToString, ' is ', LPair.Value);
|
||||
|
||||
// Add or set sansa? :)
|
||||
WriteLn;
|
||||
LSansa.TaxID := 668;
|
||||
WriteLn(Format('AddOrSet(%s, ''innocent'')', [LSansa.ToString]));
|
||||
map.AddOrSetValue(LSansa, 'innocent');
|
||||
WriteLn;
|
||||
|
||||
//
|
||||
WriteLn('All taxpayers');
|
||||
for LPair in map do
|
||||
WriteLn(' > ', LPair.Key.ToString, ' is ', LPair.Value);
|
||||
|
||||
ReadLn;
|
||||
map.Free;
|
||||
end.
|
||||
|
@ -1,66 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<CONFIG>
|
||||
<ProjectOptions>
|
||||
<Version Value="9"/>
|
||||
<PathDelim Value="\"/>
|
||||
<General>
|
||||
<Flags>
|
||||
<MainUnitHasCreateFormStatements Value="False"/>
|
||||
<MainUnitHasTitleStatement Value="False"/>
|
||||
</Flags>
|
||||
<SessionStorage Value="InProjectDir"/>
|
||||
<MainUnit Value="0"/>
|
||||
<Title Value="TObjectListProject"/>
|
||||
<UseAppBundle Value="False"/>
|
||||
<ResourceType Value="res"/>
|
||||
</General>
|
||||
<i18n>
|
||||
<EnableI18N LFM="False"/>
|
||||
</i18n>
|
||||
<VersionInfo>
|
||||
<StringTable ProductVersion=""/>
|
||||
</VersionInfo>
|
||||
<BuildModes Count="1">
|
||||
<Item1 Name="Default" Default="True"/>
|
||||
</BuildModes>
|
||||
<PublishOptions>
|
||||
<Version Value="2"/>
|
||||
</PublishOptions>
|
||||
<RunParams>
|
||||
<local>
|
||||
<FormatVersion Value="1"/>
|
||||
</local>
|
||||
</RunParams>
|
||||
<Units Count="1">
|
||||
<Unit0>
|
||||
<Filename Value="TObjectListProject.lpr"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
</Unit0>
|
||||
</Units>
|
||||
</ProjectOptions>
|
||||
<CompilerOptions>
|
||||
<Version Value="11"/>
|
||||
<PathDelim Value="\"/>
|
||||
<Target>
|
||||
<Filename Value="TObjectListProject"/>
|
||||
</Target>
|
||||
<SearchPaths>
|
||||
<IncludeFiles Value="$(ProjOutDir)"/>
|
||||
<OtherUnitFiles Value="..\..\src"/>
|
||||
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
|
||||
</SearchPaths>
|
||||
</CompilerOptions>
|
||||
<Debugging>
|
||||
<Exceptions Count="3">
|
||||
<Item1>
|
||||
<Name Value="EAbort"/>
|
||||
</Item1>
|
||||
<Item2>
|
||||
<Name Value="ECodetoolError"/>
|
||||
</Item2>
|
||||
<Item3>
|
||||
<Name Value="EFOpenError"/>
|
||||
</Item3>
|
||||
</Exceptions>
|
||||
</Debugging>
|
||||
</CONFIG>
|
@ -1,194 +0,0 @@
|
||||
// Generic types for FreeSparta.com and FreePascal!
|
||||
// Original version by keeper89.blogspot.com, 2011
|
||||
// FPC version by Maciej Izak (hnb), 2014
|
||||
|
||||
program TObjectListProject;
|
||||
|
||||
{$MODE DELPHI}
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
SysUtils, Generics.Collections, Generics.Defaults, DateUtils;
|
||||
|
||||
type
|
||||
TPlayer = class
|
||||
public
|
||||
Name, Team: string;
|
||||
BirthDay: TDateTime;
|
||||
NTeamGoals: Byte; // Number of goals for the national team
|
||||
constructor Create(const Name: string; BirthDay: TDateTime;
|
||||
const Team: string; NTeamGoals: Byte = 0);
|
||||
function ToString: string;
|
||||
end;
|
||||
|
||||
// Class containing handlers add / remove list items
|
||||
TListEventsHandler = class
|
||||
public
|
||||
class procedure OnListChanged(Sender: TObject; constref Item: TPlayer;
|
||||
Action: TCollectionNotification);
|
||||
end;
|
||||
|
||||
|
||||
constructor TPlayer.Create(const Name: string; BirthDay: TDateTime;
|
||||
const Team: string; NTeamGoals: Byte);
|
||||
begin
|
||||
Self.Name := Name;
|
||||
Self.BirthDay := BirthDay;
|
||||
Self.Team := Team;
|
||||
Self.NTeamGoals := NTeamGoals;
|
||||
end;
|
||||
|
||||
function TPlayer.ToString: string;
|
||||
begin
|
||||
Result := Format('%s - Age: %d Team: %s Goals: %d',
|
||||
[Name,
|
||||
DateUtils.YearsBetween(Date, BirthDay),
|
||||
Team, NTeamGoals])
|
||||
end;
|
||||
|
||||
// Function sort descending goals for the national team
|
||||
function ComparePlayersByGoalsDecs(constref Player1, Player2: TPlayer): Integer;
|
||||
begin
|
||||
Result := TCompare.UInt8(Player2.NTeamGoals, Player1.NTeamGoals);
|
||||
end;
|
||||
|
||||
class procedure TListEventsHandler.OnListChanged(Sender: TObject; constref Item: TPlayer;
|
||||
Action: TCollectionNotification);
|
||||
var
|
||||
Mes: string;
|
||||
begin
|
||||
// Unlike TDictionary we added Action = cnExtracted
|
||||
case Action of
|
||||
cnAdded:
|
||||
Mes := 'added to the list!';
|
||||
cnRemoved:
|
||||
Mes := 'removed from the list!';
|
||||
cnExtracted:
|
||||
Mes := 'extracted from the list!';
|
||||
end;
|
||||
Writeln(Format('Football player %s %s ', [Item.ToString, Mes]));
|
||||
end;
|
||||
|
||||
var
|
||||
// Declare TObjectList as storage for TPlayer
|
||||
PlayersList: TObjectList<TPlayer>;
|
||||
Player: TPlayer;
|
||||
FoundIndex: PtrInt;
|
||||
begin
|
||||
WriteLn('Working with TObjectList - football manager');
|
||||
WriteLn;
|
||||
|
||||
PlayersList := TObjectList<TPlayer>.Create;
|
||||
|
||||
// ---------------------------------------------------
|
||||
// 1) Adding items
|
||||
|
||||
PlayersList.Add(
|
||||
TPlayer.Create('Zinedine Zidane', EncodeDate(1972, 06, 23), 'France', 31));
|
||||
PlayersList.Add(
|
||||
TPlayer.Create('Raul', EncodeDate(1977, 06, 27), 'Spain', 44));
|
||||
PlayersList.Add(
|
||||
TPlayer.Create('Ronaldo', EncodeDate(1976, 09, 22), 'Brazil', 62));
|
||||
// Adding the specified position
|
||||
PlayersList.Insert(0,
|
||||
TPlayer.Create('Luis Figo', EncodeDate(1972, 11, 4), 'Portugal', 33));
|
||||
// Add a few players through InsertRange (AddRange works similarly)
|
||||
PlayersList.InsertRange(0,
|
||||
[TPlayer.Create('David Beckham', EncodeDate(1975, 05, 2), 'England', 17),
|
||||
TPlayer.Create('Alessandro Del Piero', EncodeDate(1974, 11, 9), 'Italy ', 27),
|
||||
TPlayer.Create('Raul', EncodeDate(1977, 06, 27), 'Spain', 44)]);
|
||||
Player := TPlayer.Create('Raul', EncodeDate(1977, 06, 27), 'Spain', 44);
|
||||
PlayersList.Add(Player);
|
||||
|
||||
|
||||
// ---------------------------------------------------
|
||||
// 2) Access and check the items
|
||||
|
||||
// Is there a player in the list - Contains
|
||||
if PlayersList.Contains(Player) then
|
||||
Writeln('Raul is in the list!');
|
||||
// Player index and count of items in the list
|
||||
Writeln(Format('Raul is %d-th on the list of %d players.',
|
||||
[PlayersList.IndexOf(Player) + 1, PlayersList.Count]));
|
||||
// Index access
|
||||
Writeln(Format('1st in the list: %s', [PlayersList[0].ToString]));
|
||||
// The first player
|
||||
Writeln(Format('1st in the list: %s', [PlayersList.First.ToString]));
|
||||
// The last player
|
||||
Writeln(Format('Last in the list: %s', [PlayersList.Last.ToString]));
|
||||
// "Reverse" elements
|
||||
PlayersList.Reverse;
|
||||
Writeln('List items have been "reversed"');
|
||||
Writeln;
|
||||
|
||||
|
||||
// ---------------------------------------------------
|
||||
// 3) Moving and removing items
|
||||
|
||||
// Changing places players in the list
|
||||
PlayersList.Exchange(0, 1);
|
||||
// Move back 1 player
|
||||
PlayersList.Move(1, 0);
|
||||
|
||||
// Removes the element at index
|
||||
PlayersList.Delete(5);
|
||||
// Or a number of elements starting at index
|
||||
PlayersList.DeleteRange(5, 2);
|
||||
// Remove the item from the list, if the item
|
||||
// exists returns its index in the list
|
||||
Writeln(Format('Removed %d-st player', [PlayersList.Remove(Player) + 1]));
|
||||
|
||||
// Extract and return the item, if there is no Player in the list then
|
||||
// Extract will return = nil, (anyway Raul is already removed via Remove)
|
||||
Player := PlayersList.Extract(Player);
|
||||
if Assigned(Player) then
|
||||
Writeln(Format('Extracted: %s', [Player.ToString]));
|
||||
|
||||
// Clear the list completely
|
||||
PlayersList.Clear;
|
||||
Writeln;
|
||||
|
||||
// ---------------------------------------------------
|
||||
// 4) Event OnNotify, sorting and searching
|
||||
|
||||
PlayersList.OnNotify := TListEventsHandler.OnListChanged;
|
||||
|
||||
PlayersList.Add(
|
||||
TPlayer.Create('Zinedine Zidane', EncodeDate(1972, 06, 23), 'France', 31));
|
||||
PlayersList.Add(
|
||||
TPlayer.Create('Raul', EncodeDate(1977, 06, 27), 'Spain', 44));
|
||||
PlayersList.Add(
|
||||
TPlayer.Create('Ronaldo', EncodeDate(1976, 09, 22), 'Brazil', 62));
|
||||
PlayersList.AddRange(
|
||||
[TPlayer.Create('David Beckham', EncodeDate(1975, 05, 2), 'England', 17),
|
||||
TPlayer.Create('Alessandro Del Piero', EncodeDate(1974, 11, 9), 'Italy ', 27),
|
||||
TPlayer.Create('Raul', EncodeDate(1977, 06, 27), 'Spain', 44)]);
|
||||
|
||||
PlayersList.Remove(PlayersList.Last);
|
||||
Player := PlayersList.Extract(PlayersList[0]);
|
||||
|
||||
PlayersList.Sort(TComparer<TPlayer>.Construct(ComparePlayersByGoalsDecs));
|
||||
Writeln;
|
||||
Writeln('Sorted list of players:');
|
||||
for Player in PlayersList do
|
||||
Writeln(Player.ToString);
|
||||
Writeln;
|
||||
|
||||
// Find Ronaldo!
|
||||
// TArray BinarySearch requires sorted list
|
||||
// IndexOf does not require sorted list
|
||||
// but BinarySearch is usually faster
|
||||
Player := PlayersList[0];
|
||||
if PlayersList.BinarySearch(Player, FoundIndex,
|
||||
TComparer<TPlayer>.Construct(ComparePlayersByGoalsDecs)) then
|
||||
Writeln(Format('Ronaldo is in the sorted list at position %d', [FoundIndex + 1]));
|
||||
|
||||
Writeln;
|
||||
|
||||
// With the destruction of the list remove all elements
|
||||
// OnNotify show it
|
||||
FreeAndNil(PlayersList);
|
||||
|
||||
Readln;
|
||||
end.
|
||||
|
@ -1,66 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<CONFIG>
|
||||
<ProjectOptions>
|
||||
<Version Value="9"/>
|
||||
<PathDelim Value="\"/>
|
||||
<General>
|
||||
<Flags>
|
||||
<MainUnitHasCreateFormStatements Value="False"/>
|
||||
<MainUnitHasTitleStatement Value="False"/>
|
||||
</Flags>
|
||||
<SessionStorage Value="InProjectDir"/>
|
||||
<MainUnit Value="0"/>
|
||||
<Title Value="TQueueProject"/>
|
||||
<UseAppBundle Value="False"/>
|
||||
<ResourceType Value="res"/>
|
||||
</General>
|
||||
<i18n>
|
||||
<EnableI18N LFM="False"/>
|
||||
</i18n>
|
||||
<VersionInfo>
|
||||
<StringTable ProductVersion=""/>
|
||||
</VersionInfo>
|
||||
<BuildModes Count="1">
|
||||
<Item1 Name="Default" Default="True"/>
|
||||
</BuildModes>
|
||||
<PublishOptions>
|
||||
<Version Value="2"/>
|
||||
</PublishOptions>
|
||||
<RunParams>
|
||||
<local>
|
||||
<FormatVersion Value="1"/>
|
||||
</local>
|
||||
</RunParams>
|
||||
<Units Count="1">
|
||||
<Unit0>
|
||||
<Filename Value="TQueueProject.lpr"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
</Unit0>
|
||||
</Units>
|
||||
</ProjectOptions>
|
||||
<CompilerOptions>
|
||||
<Version Value="11"/>
|
||||
<PathDelim Value="\"/>
|
||||
<Target>
|
||||
<Filename Value="TQueueProject"/>
|
||||
</Target>
|
||||
<SearchPaths>
|
||||
<IncludeFiles Value="$(ProjOutDir)"/>
|
||||
<OtherUnitFiles Value="..\..\src"/>
|
||||
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
|
||||
</SearchPaths>
|
||||
</CompilerOptions>
|
||||
<Debugging>
|
||||
<Exceptions Count="3">
|
||||
<Item1>
|
||||
<Name Value="EAbort"/>
|
||||
</Item1>
|
||||
<Item2>
|
||||
<Name Value="ECodetoolError"/>
|
||||
</Item2>
|
||||
<Item3>
|
||||
<Name Value="EFOpenError"/>
|
||||
</Item3>
|
||||
</Exceptions>
|
||||
</Debugging>
|
||||
</CONFIG>
|
@ -1,89 +0,0 @@
|
||||
// Generic types for FreeSparta.com and FreePascal!
|
||||
// Original version by keeper89.blogspot.com, 2011
|
||||
// FPC version by Maciej Izak (hnb), 2014
|
||||
|
||||
program TQueueProject;
|
||||
|
||||
{$MODE DELPHI}
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
SysUtils, Generics.Collections;
|
||||
|
||||
type
|
||||
// This is FreeSpaaarta! versions =)
|
||||
TSpartaVersion = (svFreeSparta, svBasic, svStarter, svProfessional);
|
||||
|
||||
TCustomer = record
|
||||
strict private
|
||||
const
|
||||
SV_NAMES: array [TSpartaVersion] of string =
|
||||
('FreeSparta', 'Basic', 'Starter', 'Professional');
|
||||
public
|
||||
var
|
||||
SpartaVersion: TSpartaVersion;
|
||||
class function Create(SpartaVersion: TSpartaVersion): TCustomer; static;
|
||||
function ToString: string;
|
||||
end;
|
||||
|
||||
class function TCustomer.Create(SpartaVersion: TSpartaVersion): TCustomer;
|
||||
begin
|
||||
Result.SpartaVersion := SpartaVersion;
|
||||
end;
|
||||
|
||||
function TCustomer.ToString: string;
|
||||
begin
|
||||
Result := Format('Sparta %s', [SV_NAMES[SpartaVersion]])
|
||||
end;
|
||||
|
||||
var
|
||||
CustomerQueue: TQueue<TCustomer>;
|
||||
Customer: TCustomer;
|
||||
begin
|
||||
WriteLn('Working with TQueue - buy FreeSparta.com');
|
||||
WriteLn;
|
||||
|
||||
// "Create" turn in sales
|
||||
CustomerQueue := TQueue<TCustomer>.Create;
|
||||
|
||||
// Add a few people in the queue
|
||||
// Enqueue - puts the item in the queue
|
||||
CustomerQueue.Enqueue(TCustomer.Create(svFreeSparta));
|
||||
CustomerQueue.Enqueue(TCustomer.Create(svBasic));
|
||||
CustomerQueue.Enqueue(TCustomer.Create(svBasic));
|
||||
CustomerQueue.Enqueue(TCustomer.Create(svBasic));
|
||||
CustomerQueue.Enqueue(TCustomer.Create(svStarter));
|
||||
CustomerQueue.Enqueue(TCustomer.Create(svStarter));
|
||||
CustomerQueue.Enqueue(TCustomer.Create(svProfessional));
|
||||
CustomerQueue.Enqueue(TCustomer.Create(svProfessional));
|
||||
|
||||
// Part of customers served
|
||||
// Dequeue - remove an element from the queue
|
||||
// btw if TQueue is TObjectQueue also call Free for object
|
||||
Customer := CustomerQueue.Dequeue;
|
||||
Writeln(Format('Sold (Dequeue): %s', [Customer.ToString]));
|
||||
// Extract - similar to Dequeue, but causes in OnNotify
|
||||
// Action = cnExtracted instead cnRemoved
|
||||
Customer := CustomerQueue.Extract;
|
||||
Writeln(Format('Sold (Extract): %s', [Customer.ToString]));
|
||||
|
||||
// For what came next buyer?
|
||||
// Peek - returns the first element, but does not remove it from the queue
|
||||
Writeln(Format('Serves customers come for %s',
|
||||
[CustomerQueue.Peek.ToString]));
|
||||
|
||||
// The remaining buyers
|
||||
Writeln;
|
||||
Writeln(Format('Buyers left: %d', [CustomerQueue.Count]));
|
||||
for Customer in CustomerQueue do
|
||||
Writeln(Customer.ToString);
|
||||
|
||||
// We serve all
|
||||
// Clear - clears the queue
|
||||
CustomerQueue.Clear;
|
||||
|
||||
FreeAndNil(CustomerQueue);
|
||||
|
||||
Readln;
|
||||
end.
|
||||
|
@ -1,66 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<CONFIG>
|
||||
<ProjectOptions>
|
||||
<Version Value="9"/>
|
||||
<PathDelim Value="\"/>
|
||||
<General>
|
||||
<Flags>
|
||||
<MainUnitHasCreateFormStatements Value="False"/>
|
||||
<MainUnitHasTitleStatement Value="False"/>
|
||||
</Flags>
|
||||
<SessionStorage Value="InProjectDir"/>
|
||||
<MainUnit Value="0"/>
|
||||
<Title Value="TStackProject"/>
|
||||
<UseAppBundle Value="False"/>
|
||||
<ResourceType Value="res"/>
|
||||
</General>
|
||||
<i18n>
|
||||
<EnableI18N LFM="False"/>
|
||||
</i18n>
|
||||
<VersionInfo>
|
||||
<StringTable ProductVersion=""/>
|
||||
</VersionInfo>
|
||||
<BuildModes Count="1">
|
||||
<Item1 Name="Default" Default="True"/>
|
||||
</BuildModes>
|
||||
<PublishOptions>
|
||||
<Version Value="2"/>
|
||||
</PublishOptions>
|
||||
<RunParams>
|
||||
<local>
|
||||
<FormatVersion Value="1"/>
|
||||
</local>
|
||||
</RunParams>
|
||||
<Units Count="1">
|
||||
<Unit0>
|
||||
<Filename Value="TStackProject.lpr"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
</Unit0>
|
||||
</Units>
|
||||
</ProjectOptions>
|
||||
<CompilerOptions>
|
||||
<Version Value="11"/>
|
||||
<PathDelim Value="\"/>
|
||||
<Target>
|
||||
<Filename Value="TStackProject"/>
|
||||
</Target>
|
||||
<SearchPaths>
|
||||
<IncludeFiles Value="$(ProjOutDir)"/>
|
||||
<OtherUnitFiles Value="..\..\src"/>
|
||||
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
|
||||
</SearchPaths>
|
||||
</CompilerOptions>
|
||||
<Debugging>
|
||||
<Exceptions Count="3">
|
||||
<Item1>
|
||||
<Name Value="EAbort"/>
|
||||
</Item1>
|
||||
<Item2>
|
||||
<Name Value="ECodetoolError"/>
|
||||
</Item2>
|
||||
<Item3>
|
||||
<Name Value="EFOpenError"/>
|
||||
</Item3>
|
||||
</Exceptions>
|
||||
</Debugging>
|
||||
</CONFIG>
|
@ -1,85 +0,0 @@
|
||||
// Generic types for FreeSparta.com and FreePascal!
|
||||
// Original version by keeper89.blogspot.com, 2011
|
||||
// FPC version by Maciej Izak (hnb), 2014
|
||||
program TStackProject;
|
||||
|
||||
{$MODE DELPHI}
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
SysUtils,
|
||||
Generics.Collections;
|
||||
|
||||
type
|
||||
// We will cook pancakes, put them on a plate and take the last
|
||||
TPancakeType = (ptMeat, ptCherry, ptCurds);
|
||||
|
||||
TPancake = record
|
||||
strict private
|
||||
const
|
||||
PANCAKE_TYPE_NAMES: array [TPancakeType] of string =
|
||||
('meat', 'cherry', 'curds');
|
||||
public
|
||||
var
|
||||
PancakeType: TPancakeType;
|
||||
class function Create(PancakeType: TPancakeType): TPancake; static;
|
||||
function ToString: string;
|
||||
end;
|
||||
|
||||
class function TPancake.Create(PancakeType: TPancakeType): TPancake;
|
||||
begin
|
||||
Result.PancakeType := PancakeType;
|
||||
end;
|
||||
|
||||
function TPancake.ToString: string;
|
||||
begin
|
||||
Result := Format('Pancake with %s', [PANCAKE_TYPE_NAMES[PancakeType]])
|
||||
end;
|
||||
|
||||
var
|
||||
PancakesPlate: TStack<TPancake>;
|
||||
Pancake: TPancake;
|
||||
|
||||
begin
|
||||
WriteLn('Working with TStack - pancakes');
|
||||
WriteLn;
|
||||
|
||||
// "Create" a plate of pancakes
|
||||
PancakesPlate := TStack<TPancake>.Create;
|
||||
|
||||
// Bake some pancakes
|
||||
// Push - puts items on the stack
|
||||
PancakesPlate.Push(TPancake.Create(ptMeat));
|
||||
PancakesPlate.Push(TPancake.Create(ptCherry));
|
||||
PancakesPlate.Push(TPancake.Create(ptCherry));
|
||||
PancakesPlate.Push(TPancake.Create(ptCurds));
|
||||
PancakesPlate.Push(TPancake.Create(ptMeat));
|
||||
|
||||
// Eating some pancakes
|
||||
// Pop - removes an item from the stack
|
||||
Pancake := PancakesPlate.Pop;
|
||||
Writeln(Format('Ate a pancake (Pop): %s', [Pancake.ToString]));
|
||||
// Extract - similar to Pop, but causes in OnNotify
|
||||
// Action = cnExtracted instead of cnRemoved
|
||||
Pancake := PancakesPlate.Extract;
|
||||
Writeln(Format('Ate a pancake (Extract): %s', [Pancake.ToString]));
|
||||
|
||||
// What is the last pancake?
|
||||
// Peek - returns the last item, but does not remove it from the stack
|
||||
Writeln(Format('Last pancake: %s', [PancakesPlate.Peek.ToString]));
|
||||
|
||||
// Show the remaining pancakes
|
||||
Writeln;
|
||||
Writeln(Format('Total pancakes: %d', [PancakesPlate.Count]));
|
||||
for Pancake in PancakesPlate do
|
||||
Writeln(Pancake.ToString);
|
||||
|
||||
// Eat up all
|
||||
// Clear - clears the stack
|
||||
PancakesPlate.Clear;
|
||||
|
||||
FreeAndNil(PancakesPlate);
|
||||
|
||||
Readln;
|
||||
end.
|
||||
|
Loading…
Reference in New Issue
Block a user