开发者

How to use a TFileStream to read 2D matrices into dynamic array?

I need to read a large (2000x2000) matrix of binary data from a file into a dynamic array with Delphi 2010. I don't know the dimensions until run-time.

I've never read raw data like this, and don't know IEEE so I'm posting this to see if I'm on track.

I plan to use a TFileStream to read one row at a time.

I need to be able to read as many of these formats as possible:

 16-bit two's complement binary integer
 32-bit two's complement binary integer
 64-bit two's complement binary integer
 IEEE single precision floating-point

Fo开发者_如何学Gor 32-bit two's complement, I'm thinking something like the code below. Changing to Int64 and Int16 should be straight forward. How can I read the IEEE?

Am I on the right track? Any suggestions on this code, or how to elegantly extend it for all 4 data types above? Since my post-processing will be the same after reading this data, I guess I'll have to copy the matrix into a common format when done.

I have no problem just having four procedures (one for each data type) like the one below, but perhaps there's an elegant way to use RTTI or buffers and then move()'s so that the same code works for all 4 datatypes?

Thanks!

  type
    TRowData = array of Int32;

   procedure ReadMatrix;
   var
     Matrix: array of TRowData;
     NumberOfRows: Cardinal;
     NumberOfCols: Cardinal;
     CurRow: Integer;
   begin
     NumberOfRows := 20; // not known until run time
     NumberOfCols := 100; // not known until run time
     SetLength(Matrix, NumberOfRows);
     for CurRow := 0 to NumberOfRows do
       begin
         SetLength(Matrix[CurRow], NumberOfCols);
         FileStream.ReadBuffer(Matrix[CurRow], NumberOfCols * SizeOf(Int32)) );
       end;
   end;


No, AFAIK there's no way to use RTTI to set up multidimensional arrays. But if you're using Delphi 2010, you should be able to use generics, like so:

type
  TRowData<T> = array of T;

 procedure ReadMatrix<T>;
 var
   Matrix: array of TRowData<T>;
   NumberOfRows: Cardinal;
   NumberOfCols: Cardinal;
   CurRow: Integer;
 begin
   NumberOfRows := 20; // not known until run time
   NumberOfCols := 100; // not known until run time
   SetLength(Matrix, NumberOfRows, NumberOfCols);
   for CurRow := 0 to NumberOfRows do
     FileStream.ReadBuffer(Matrix[CurRow][0], NumberOfCols * SizeOf(T)) );
 end;

This will have to be in a class, though, as Delphi 2010 doesn't support standalone procedures with generic types. Once you've got this set up, you can call TWhateverClass.ReadMatrix<integer>, TWhateverClass.ReadMatrix<int64>, TWhateverClass.ReadMatrix<single>, and so on.

Also, if you have a multidimensional array with X dimensions, you can pass X length parameters to SetLength, not just one. So use one call to SetLength(Matrix, NumberOfRows, NumberOfCols) outside the loop, instead of initializing each row separately to the same width.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜