从静态数组迁移到动态数组



以前我有矩阵数据集设计的静态数组

TMatrix = record
    row, column: word; {m columns ,n strings }
    Data: array[1..160, 1..160] of real
 var
 Mymatrix  : TMatrix;
 begin
 Mymatrix.row := 160; - maximum size for row us is 160 for 2 x 2 static design.
 Mymatrix.columns := 160; -  maximum size for column us is 160  for 2 x 2 static design.

使用当前的设计,我只能在2维矩阵设计中具有160 x 160。如果我输入更多数组大小[1..161,1..161],则编译器将警报E2100数据类型太大:超过2 GB错误。因此,如果我将代码转换为动态数组,我需要重新结构我当前的所有代码才能读取矩阵从0开始。previoulsy,对于静态数组,数组将从1开始。一些外部功能它开始从1。

开始读取矩阵。

所以,现在我遇到了当前的代码,我需要创建超过千的n x n矩阵大小。使用我当前的静态阵列设计,如果低于160 x 160,一切都很好。因此,我需要获得任何解决方案,而没有太多的方法来改变我当前的静态阵列设计。

谢谢。

继续使用1个基于1个索引。您可以做几种不同的方式。例如:

type
  TMatrix = record
  private
    Data: array of array of Real;
    function GetRowCount: Integer;
    function GetColCount: Integer;
    function GetItem(Row, Col: Integer): Real;
    procedure SetItem(Row, Col: Integer; Value: Real);
  public      
    procedure SetSize(RowCount, ColCount: Integer);
    property RowCount: Integer read GetRowCount;
    property ColCount: Integer read GetColCount;
    property Items[Row, Col: Integer]: Real read GetItem write SetItem; default;
  end;
function TMatrix.GetRowCount: Integer;
begin
  Result := Length(Data)-1;
end;
function TMatrix.GetColCount: Integer;
begin
  if Assigned(Data) then
    Result := Length(Data[0])-1
  else
    Result := 0;
end;
procedure TMatrix.SetSize(RowCount, ColCount: Integer);
begin
  SetLength(Data, RowCount+1, ColCount+1);
end;
function TMatrix.GetItem(Row, Col: Integer): Real;
begin
  Assert(InRange(Row, 1, RowCount));
  Assert(InRange(Col, 1, ColCount));
  Result := Data[Row, Col];
end;
procedure TMatrix.SetItem(Row, Col: Integer; Value: Real);
begin
  Assert(InRange(Row, 1, RowCount));
  Assert(InRange(Col, 1, ColCount));
  Data[Row, Col] := Value;
end;

这里的技巧是,即使动态数组使用基于0的索引,您只需忽略存储在0索引中的值即可。如果您是从使用1个基于1个索引的Fortran移植代码,则此方法通常是最有效的。

最新更新