type TAssocArray = class FElements: array of string; {You can change this to whatever type you want} FIndexList: TStrings; public constructor Create; destructor Destroy; function GetValue(index: string): string; procedure SetValue(index,value: string); property Items[index: string] read GetValue write SetValue; default; // must be public, it can't be published (it's an array property) end;implementationconstructor TAssocArray.Create;begin FIndexList := TStringList.Create;end;destructor TAssocArray.Destroy;begin FIndexList.Free;end;function TAssocArray.GetValue(index: string): string;var i: integer;begin i := FIndexList.IndexOf(index); if i >= 0 then GetValue := FElements[i] else GetValue := '';end;procedure TAssocArray.SetValue(index,value: string);var i,len: integer;begin i := FIndexList.IndexOf(index); if i = -1 then begin len := length(FElements); SetLength(FElements,len+1); FElements[len] := value; FIndexList.Add(index); end else begin FElements[i] := value; end;end;
type TStringHashList = class(TObject)protected function HashOf(); procedure Insert();public constructor Create(); destructor Destroy; override; function Add(); procedure Clear; function Find(); function Remove(); property CaseSensitive: Boolean; [rw] property Count: Integer; [r] property Data: Pointer; default; [rw] property List: PStringHashItemList; [r]end;
unit Unit1; {$mode objfpc}{$H+}interfaceuses Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls, StringHashList;type { TForm1 } TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; procedure Button1Click(Sender: TObject); private { private declarations } public { public declarations } SH: TStringHashList; end;var Form1: TForm1; i: Integer;implementation{ TForm1 }procedure TForm1.Button1Click(Sender: TObject);var aPoint: ^TPoint;begin // initialize hash SH := TStringHashList.Create(False); // insert a few TPoint elements into hash for i := 0 to 3 do begin New(aPoint); aPoint^.X := i; aPoint^.Y := i; SH.Add(IntToStr(i), aPoint); end; // copy hash key-value pair info to memo text for i := 0 to (SH.Count - 1) do begin Memo1.Lines.Add(SH.List[i]^.Key + ' => (' + IntToStr(TPoint(SH.List[i]^.Data^).X) + ', ' + IntToStr(TPoint(SH.List[i]^.Data^).Y) + ')' ); end; // delete hash SH.Destroy; // also disposes of ^TPoint dataend;initialization {$I unit1.lrs}end.
0 => (0, 0)1 => (1, 1)2 => (2, 2)3 => (3, 3)
type PStringHashItem = ^TStringHashItem; TStringHashItem = record HashValue: Cardinal; Key: String; Data: Pointer; end; PStringHashItemList = ^PStringHashItem; TStringHashList = class(TObject) private FList: PStringHashItemList; .... end;
Case CompareValue(Val, FList[I]^.HashValue)<=0 of
FList[Index] := Item;
Free Pascal treats pointers much the same way as C does. This means that a pointer to some type can be treated as being an array of this type.
procedure TForm1.Button1Click(Sender: TObject);var TA: TableauAssociatif;begin TA := TableauAssociatif.Create; TA['prénom'] := 'Jacques'; TA['nom'] := 'Dupont'; TA['age'] := '53'; ShowMessage(TA['prénom'] + ' ' + TA['nom'] + ' a ' + TA['age'] + ' ans.'); TA.Destroy;end;
uses fgl;...type TValueArray = array of Integer; TNameValueMap = specialize TFPGMap<String,TValueArray>;...var NameValueMap: TNameValueMap;begin NameValueMap := TNameValueMap.Create; SetLength(NameValueMap['peter'],10); NameValueMap['peter',1] := 10; NameValueMap['peter',2] := 20; SetLength(NameValueMap['joe'],10); NameValueMap['joe',1] := 4;end;
Don't make it complicated, it's easy:Code: [Select] SetLength(NameValueMap['peter'],10);
SetLength(NameValueMap['peter'],10);
This doesn't seem to work (at least not with my config).
{$mode objfpc}{$H+}uses fgl;type TValueArray = array of Integer; TNameValueMap = specialize TFPGMap<String,TValueArray>;var NameValueMap: TNameValueMap; TempValueArray: TValueArray;begin NameValueMap := TNameValueMap.Create; TempValueArray := NameValueMap['peter']; SetLength(TempValueArray,10); TempValueArray[1] := 10; TempValueArray[2] := 20; NameValueMap['peter'] := TempValueArray; TempValueArray := NameValueMap['joe']; SetLength(TempValueArray,10); TempValueArray[1] := 4; NameValueMap['joe'] := TempValueArray;end.
My problem is:Can I use associative and! multidimensional arrays in lazarus? Is there any component of it?