[Solved] Generic Table? lazarus / 21-General ---------------------------------------------------------------------- cpicanco · 2023-12-27 01:41 Hi everyone, Does FPC RTL have a generic table, for Row Col access? For example, to specialize with a TRecord in each cell: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TMyTable = specialize TFPCTable; Is there a recommended way to go here? Best, R ---------------------------------------------------------------------- kwyan · 2023-12-27 07:16 Can you explain more about your requirements as it may have different alternatives? One of the solution is in-memory table (TMemDataSet). Similar to TSQLQuery, you can use TDBEdit, TDBGrid .... for this Memory Table. ---------------------------------------------------------------------- Чебурашка · 2023-12-27 08:53 Quote from: cpicanco on December 27, 2023, 02:41:26 amHi everyone, Does FPC RTL have a generic table, for Row Col access? For example, to specialize with a TRecord in each cell: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TMyTable = specialize TFPCTable; Is there a recommended way to go here? Best, R Seems what you ask does not exist at the moment, but composing fpglist of fpgmap can easily solve the need (fpglist does the rows, while fpgmaps do the columns and allows you to access by column name also). Just make sure each row has same number of keys. Moreover be very careful with records, as you have to assign their address to the resulting table, therefor you must first allocate memory and eventually release it when you dispose your objects. Otherwise you will have memleaks. ---------------------------------------------------------------------- Wallaby · 2023-12-27 10:58 Quote from: cpicanco on December 27, 2023, 02:41:26 amIs there a recommended way to go here? You could easily make one yourself, e.g. (in Delphi syntax): TMyList = TObjectList> The first list will have rows, the second one will have columns. Or for speed, use TDictionary where the 64-bit QWord key is made of 32-bit row and 32-bit column. ---------------------------------------------------------------------- cpicanco · 2023-12-27 11:17 Thank you for your suggestions. To give you some more details, I am trying to create a navigation system for on screen controls. The idea is to create a map table using the origin (x, y) of controls so that I will be able to go Up, Down, Left, Right in non-empty cells of the table. I said records earlier, but actually I am using interfaces to access my controls: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ISelectable = interface[some-guid]  function Origin : TPoint;  procedure Select;  procedure ConfirmSelection;end; I was taking a look at the TCSVDocument class and FPC implements row/col access using nested TObjectList, so I was considering doing something like: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i generic TTable = classprivate  type    TCell = class       Value : ;    end;    TRow = class       FCells : TObjectList;    end;private    FRows : TObjectList;end; ---------------------------------------------------------------------- cpicanco · 2023-12-27 11:28 Quote from: Wallaby on December 27, 2023, 11:58:29 amOr for speed, use TDictionary where the 64-bit QWord key is made of 32-bit row and 32-bit column. Thank you Wallaby, much simpler! I will give it a try. ---------------------------------------------------------------------- cpicanco · 2023-12-27 11:31 Quote from: Чебурашка on December 27, 2023, 09:53:11 amSeems what you ask does not exist at the moment, but composing fpglist of fpgmap can easily solve the need (fpglist does the rows, while fpgmaps do the columns and allows you to access by column name also). Just make sure each row has same number of keys. Moreover be very careful with records, as you have to assign their address to the resulting table, therefor you must first allocate memory and eventually release it when you dispose your objects. Otherwise you will have memleaks. Thank you Чебурашка, I will give it a try too. ---------------------------------------------------------------------- Чебурашка · 2023-12-27 11:34 Quote from: cpicanco on December 27, 2023, 12:17:35 pmThank you for your suggestions. To give you some more details, I am trying to create a navigation system for on screen controls. The idea is to create a map table using the origin (x, y) of controls so that I will be able to go Up, Down, Left, Right in non-empty cells of the table. I said records earlier, but actually I am using interfaces to access my controls: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ISelectable = interface[some-guid]  function Origin : TPoint;  procedure Select;  procedure ConfirmSelection;end; I was taking a look at the TCSVDocument class and FPC implements row/col access using nested TObjectList, so I was considering doing something like: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i generic TTable = classprivate  type    TCell = class       Value : ;    end;    TRow = class       FCells : TObjectList;    end;private    FRows : TObjectList;end; I suggest you to use a single fpglist, wrapped by a component that does the access by row,col indexes with just a simple listIndex := COLS*rowIndex + colIndex, is much easier. ---------------------------------------------------------------------- jcmontherock · 2023-12-27 16:32 The more simple is using dynamic array of records or array of classes. For row/cols access for results from database I'm using an array of array of string. ---------------------------------------------------------------------- cpicanco · 2023-12-27 16:38 Hi everyone, Here is my first step towards it, many thanks to Wallaby and Чебурашка! Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Generics.Table; {$mode objfpc} interface uses  Classes, SysUtils, Generics.Collections; type   { TTable }   generic TTable = class  private    type      TTableSpec = specialize TDictionary;     var      FTableData: TTableSpec;      FRowCount: UInt32;      FColCount: UInt32;       function KeyOf(ACol, ARow: UInt32): UInt64;    function GetCell(ACol, ARow: UInt32): T;      procedure SetCell(ACol, ARow: UInt32; const AValue: T);      function GetRowCount: UInt32;      function GetColCount: UInt32;      function IsCellEmpty(const AValue: T): Boolean; virtual;      procedure SetColCount(AValue: UInt32);      procedure SetRowCount(AValue: UInt32);    public      constructor Create;      constructor Create(ARowCount, AColCount: UInt32);      destructor Destroy; override;      function IsEmpty(ACol, ARow: UInt32) : Boolean;      function AsString : string;      procedure AppendRow;      procedure AppendCol;      procedure InsertRow(ARow: UInt32; const AValues: array of T);      procedure InsertCol(ACol: UInt32; const AValues: array of T);      procedure RemoveRow(ARow: UInt32);      procedure RemoveCol(ACol: UInt32);       procedure Clear;      procedure Empty;      procedure Populate;      property Cells[ACol, ARow: UInt32]: T read GetCell write SetCell; default;      property RowCount: UInt32 read GetRowCount write SetRowCount;      property ColCount: UInt32 read GetColCount write SetColCount;  end; implementation { TTable } constructor TTable.Create(ARowCount, AColCount: UInt32);begin  FRowCount := ARowCount;  FColCount := AColCount;  FTableData := TTableSpec.Create;  Populate;end; destructor TTable.Destroy;begin  FTableData.Free;  inherited Destroy;end; function TTable.IsEmpty(ACol, ARow: UInt32): Boolean;begin  Result := IsCellEmpty(FTableData[KeyOf(ACol, ARow)]);end; function TTable.AsString: string;var  LRow, LCol : UInt32;begin  Result := '';  if FColCount = 0 then Exit;  if FRowCount = 0 then Exit;  for LRow := 0 to FRowCount-1 do begin    for LCol := 0 to FColCount-1 do begin    if IsCellEmpty(GetCell(LCol, LRow)) then begin        Result := Result + '(x)';      end else begin        Result := Result + '(o)';    end;    end;    Result := Result + LineEnding;  end;end; procedure TTable.AppendRow;var  i: UInt32;begin  for i := 0 to FColCount - 1 do begin    FTableData.Add(KeyOf(FColCount-1, i), Default(T));  end;  Inc(FRowCount);end; procedure TTable.AppendCol;var  i: UInt32;begin  for i := 0 to FRowCount - 1 do begin    FTableData.Add(KeyOf(i, FRowCount-1), Default(T));  end;  Inc(FColCount);end; function TTable.KeyOf(ACol, ARow: UInt32): UInt64;begin  Result := (UInt64(ARow) shl 32) or UInt64(ACol);end; function TTable.GetCell(ACol, ARow: UInt32): T;begin  Result := FTableData[KeyOf(ACol, ARow)];end; procedure TTable.SetCell(ACol, ARow: UInt32; const AValue: T);begin  FTableData[KeyOf(ACol, ARow)] := AValue;end; function TTable.GetRowCount: UInt32;begin  Result := FRowCount;end; function TTable.GetColCount: UInt32;begin  Result := FColCount;end; function TTable.IsCellEmpty(const AValue: T): Boolean;begin  Result := AValue = Default(T);end; procedure TTable.SetColCount(AValue: UInt32);var  i: UInt32;begin  if AValue = FColCount then Exit;   if AValue > FColCount then begin    for i := FColCount to AValue-1 do begin      AppendCol;    end;  end else begin    for i := AValue to FColCount-1 do begin      RemoveCol(i);  end;  end;end; procedure TTable.SetRowCount(AValue: UInt32);var  i: UInt32;begin  if AValue = FRowCount then Exit;   if AValue > FRowCount then begin    for i := FRowCount to AValue-1 do begin      AppendRow;    end;  end else begin    for i := AValue to FRowCount-1 do begin    RemoveRow(i);    end;  end;end; constructor TTable.Create;begin  FRowCount := 0;  FColCount := 0;  FTableData := TTableSpec.Create;end; procedure TTable.InsertRow(ARow: UInt32; const AValues: array of T);var  i : UInt32;begin  if ARow > FRowCount then  raise Exception.Create('Invalid row index for insertion.');   if Length(AValues) <> FColCount then    raise Exception.Create('Number of values must match the number of columns.');   for i := 0 to FColCount-1 do begin    FTableData.Add(KeyOf(i, ARow), AValues[i]);  end;   Inc(FRowCount);end; procedure TTable.InsertCol(ACol: UInt32; const AValues: array of T);var  i : UInt32;begin  if ACol > FRowCount then    raise Exception.Create('Invalid col index for insertion.');   if Length(AValues) <> FRowCount then    raise Exception.Create('Number of values must match the number of rows.');   for i := 0 to FRowCount-1 do begin    FTableData.Add(KeyOf(ACol, i), AValues[i]);  end;   Inc(FColCount);end; procedure TTable.RemoveRow(ARow: UInt32);var  i: UInt32;begin  if ARow >= FRowCount then    raise Exception.Create('Invalid row index for removal.');   for i := 0 to FColCount-1 do begin    FTableData.Remove(KeyOf(i, ARow));  end;   Dec(FRowCount);end; procedure TTable.RemoveCol(ACol: UInt32);var  i: UInt32;begin  if ACol >= FColCount then    raise Exception.Create('Invalid col index for removal.');   for i := 0 to FRowCount-1 do begin    FTableData.Remove(KeyOf(ACol, i));  end;   Dec(FColCount);end; procedure TTable.Clear;begin  FTableData.Clear;end; procedure TTable.Empty;var  LCol : UInt32;  LRow : UInt32;begin  for LCol := 0 to FColCount-1 do begin    for LRow := 0 to FRowCount-1 do begin      FTableData[KeyOf(LCol, LRow)] := Default(T);    end;  end;end; procedure TTable.Populate;var  LCol, LRow: UInt64;begin  for LCol := 0 to FColCount-1 do begin    for LRow := 0 to FRowCount-1 do begin      FTableData.Add(KeyOf(LCol, LRow), Default(T));    end;  end;end; end. I will write a test unit soon, but here is a really simple "test" program: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program generictable; uses Generics.Table; type  TTable = specialize TTable; var  Table : TTable; begin  Table := TTable.Create(3, 3);  Table.Cells[0, 0] := 5;  Table.Cells[1, 1] := 5;  Table.Cells[2, 2] := 5;  WriteLn(Table.AsString);   ReadLn;end. ---------------------------------------------------------------------- cpicanco · 2023-12-27 16:48 I am using TDictionary from Generics.Collections because it is more stable than TFPGMap (which may fail in some circunstances, although I can't explain why). I found a benchmark (https://www.benibela.de/fpc-map- benchmark_en.html) and I think that TDictionary will do a pretty good job when speed is an issue too. ---------------------------------------------------------------------- paweld · 2023-12-27 18:06 Quote from: cpicancoI am using TDictionary from Generics.Collections because it is more stable than TFPGMap (which may fail in some circunstances, although I can't explain why). Very interesting statement. Can you provide more information on this topic? Example code, fpc and system version ---------------------------------------------------------------------- cpicanco · 2023-12-27 18:12 Quote from: paweld on December 27, 2023, 07:06:01 pmQuote from: cpicancoI am using TDictionary from Generics.Collections because it is more stable than TFPGMap (which may fail in some circunstances, although I can't explain why). Very interesting statement. Can you provide more information on this topic? Example code, fpc and system version Sure, there is a discussion here https://forum.lazarus.freepascal.org/index.php/topic,65269.msg497252.h tml#msg497252