Una forma de hacerlo con un minimo uso de memoria y rapido acceso a los datos.
Código Delphi
[-]
unit uDataMatrix;
interface
uses SysUtils;
type
TDataArray= array [0..MaxInt div 16-1] of double;
PDataArray= ^TDataArray;
TDataMatrix= class
private
FColCount,FRowCount:integer;
FData: PDataArray;
procedure SetData(AData:PDataArray; ARows,ACols:integer);
function GetCell(ARow,ACol:integer):double;
public
procedure Select(Model:string); overload;
procedure Select(Family:string; nS:integer; cC:string); overload;
property RowCount:integer read FRowCount;
property ColCount:integer read FColCount;
property Cells[ARow,ACol:integer]:double read GetCell; default;
end;
var
DataMatrix: TDataMatrix;
implementation
type
RModel= record
Name:string;
Rows:integer;
Cols:integer;
Data:Pointer;
end;
var
L_72_F_32_1_1V: array [0..19] of double = ( 0,2,3,4,1, 1,3,4,5,3, 2,3,3,4,8, 3,2,3,4,9 );
L_72_F_32_1_2V: array [0..14] of double = ( 1,2,3,4,1, 2,3,4,5,3, 2,3,3,4,8 );
L_72_F_32_1_3V: array [0..13] of double = ( 1,2,3,4,1,3,4, 2,3,4,5,3,2,3 );
OTRFAMILY_1_3V: array [0..13] of double = ( 1,2,3,4,1,3,4, 2,3,4,5,3,2,3 );
Models: array [0..3] of RModel= (
( Name:'L_72_F_32_1_1V'; Rows:4; Cols:5; Data:@L_72_F_32_1_1V ),
( Name:'L_72_F_32_1_2V'; Rows:3; Cols:5; Data:@L_72_F_32_1_2V ),
( Name:'L_72_F_32_1_3V'; Rows:2; Cols:7; Data:@L_72_F_32_1_3V ),
( Name:'OTRFAMILY_1_3V'; Rows:2; Cols:7; Data:@OTRFAMILY_1_3V )
);
function TDataMatrix.GetCell(ARow,ACol:integer):double;
begin
Result:= FData^[ARow*FColCount+ACol];
end;
procedure TDataMatrix.SetData(AData:PDataArray; ARows,ACols:integer);
begin
FData := AData;
FRowCount:= ARows;
FColCount:= ACols;
end;
procedure TDataMatrix.Select(Model:string);
var
i:integer;
begin
for i:=High(Models) downto 0 do
if Models[i].Name=Model then begin
SetData( Models[i].Data, Models[i].Rows, Models[i].Cols );
Exit;
end;
Raise Exception.Create('Unknow Model');
end;
procedure TDataMatrix.Select(Family:string; nS:integer; cC:string);
begin
Select( Format( '%s_%d_%s' ,[family,nS,cC]) );
end;
initialization
begin
DataMatrix:= TDataMatrix.Create;
end;
finalization
begin
DataMatrix.free;
end;
end.
Como usar la unit:
Código Delphi
[-]
uses uDataMatrix;
DataMatrix.Select('L_72_F_32', 1,'3V');
for Row:=0 to DataMatrix.RowCount-1 do
for Col:=0 to DataMatrix.ColCount-1 do
Sum:= sum+ DataMatrix[Row,Col];
DataMatrix.Select('L_72_F_32_1_3V');
var
Data:TDataMatrix;
begin
Data:= TDataMatrix.Create;
try
Data.Select('L_72_F_32',1,'3V');
Data.RowCount; Data.ColCount; Data[columna,fila]; Data.Select('OTROMODELO', 1,'1V');
..etc.
finally
Data.Free;
end;
end;
Saludos