Difference between revisions of "TFPGMap"

From Lazarus wiki
Jump to navigationJump to search
Line 1: Line 1:
 
TFPGMap is part of the <code>FGL</code> (Free Generics Library).
 
TFPGMap is part of the <code>FGL</code> (Free Generics Library).
  
 +
==Example==
 
Here is an example provided by forum user '''Remy Lebeau''':
 
Here is an example provided by forum user '''Remy Lebeau''':
  
Line 23: Line 24:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
==IndexOfData==
 +
By default <code>TFPGMap<></code> does not provide a comparison for the Data type. So method IndexOfData will not find anything. You need to provide it by assigning the OnDataCompare event:
 +
 +
<syntaxhighlight lang="pascal">
 +
uses
 +
  StrUtils, fgl;
 +
 +
{$R *.lfm}
 +
 +
type
 +
  TMyTest = specialize TFPGMap<PtrInt, String>;
 +
...
 +
  public
 +
    property MyTest: TMyTest read FMyTest write SetMyTest;
 +
  end;
 +
...
 +
 +
function CompareStringFunc(const aData1, aData2: String): Integer;
 +
begin
 +
  Result := AnsiCompareStr(aData1, aData2);
 +
end;
 +
 +
procedure TForm1.FormCreate(Sender: TObject);
 +
var
 +
  i: PtrInt = 0;
 +
begin
 +
  FMyTest:= TMyTest.Create;
 +
  FMyTest.OnDataCompare := @CompareStringFunc;
 +
</syntaxhighlight>
 
==See also==
 
==See also==
  
 
* https://www.freepascal.org/docs-html/current/rtl/fgl/tfpgmap.html
 
* https://www.freepascal.org/docs-html/current/rtl/fgl/tfpgmap.html

Revision as of 18:38, 13 November 2022

TFPGMap is part of the FGL (Free Generics Library).

Example

Here is an example provided by forum user Remy Lebeau:

uses
  ..., fgl;
 
var
  Dict: TFPGMap<string, integer>;
  Value: Integer;
begin
  Dict := TFPGMap<string, integer>.Create;
  try
    Dict.Add('VarName', 99);
    ...
    Value := Dict['VarName'];
    ...
  finally
    Dict.Free;
  end;
end;

IndexOfData

By default TFPGMap<> does not provide a comparison for the Data type. So method IndexOfData will not find anything. You need to provide it by assigning the OnDataCompare event:

uses
  StrUtils, fgl;

{$R *.lfm}

type
  TMyTest = specialize TFPGMap<PtrInt, String>;
...
  public
    property MyTest: TMyTest read FMyTest write SetMyTest;
  end;
...
 
function CompareStringFunc(const aData1, aData2: String): Integer;
begin
  Result := AnsiCompareStr(aData1, aData2);
end;
 
procedure TForm1.FormCreate(Sender: TObject);
var
  i: PtrInt = 0;
begin
  FMyTest:= TMyTest.Create;
  FMyTest.OnDataCompare := @CompareStringFunc;

See also