Difference between revisions of "TFPGMap"

From Free Pascal wiki
Jump to navigationJump to search
 
(One intermediate revision by the same user not shown)
Line 7: Line 7:
 
   
 
   
 
type
 
type
   TMyMap = specialize TFPGMap<string, integer>;
+
   TMyDict = specialize TFPGMap<string, integer>;
 
var
 
var
   Dict: TMyMap;
+
   Dict: TMyDict;
 
   Value: Integer;
 
   Value: Integer;
 
begin
 
begin
   Dict := TMyMap.Create;
+
   Dict := TMyDict.Create;
 
   try
 
   try
 
     Dict.Add('VarName', 99);
 
     Dict.Add('VarName', 99);
Line 36: Line 36:
 
   TMyTest = specialize TFPGMap<PtrInt, String>;
 
   TMyTest = specialize TFPGMap<PtrInt, String>;
 
...
 
...
 +
  private
 +
    FMyTest: TMyTest;
 +
  ...
 
   public
 
   public
 
     property MyTest: TMyTest read FMyTest write SetMyTest;
 
     property MyTest: TMyTest read FMyTest write SetMyTest;
Line 47: Line 50:
 
   
 
   
 
procedure TForm1.FormCreate(Sender: TObject);
 
procedure TForm1.FormCreate(Sender: TObject);
var
 
  i: PtrInt = 0;
 
 
begin
 
begin
 
   FMyTest:= TMyTest.Create;
 
   FMyTest:= TMyTest.Create;
 
   FMyTest.OnDataCompare := @CompareStringFunc;
 
   FMyTest.OnDataCompare := @CompareStringFunc;
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 
==See also==
 
==See also==
  
 
* [https://www.freepascal.org/docs-html/current/rtl/fgl/tfpgmap.html Free Pascal docs on TFPGMap]
 
* [https://www.freepascal.org/docs-html/current/rtl/fgl/tfpgmap.html Free Pascal docs on TFPGMap]

Latest revision as of 18:47, 13 November 2022

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

Example

uses
  fgl;
 
type
  TMyDict = specialize TFPGMap<string, integer>;
var
  Dict: TMyDict;
  Value: Integer;
begin
  Dict := TMyDict.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>;
...
  private
    FMyTest: TMyTest;
  ...
  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);
begin
  FMyTest:= TMyTest.Create;
  FMyTest.OnDataCompare := @CompareStringFunc;

See also