Delphi: Declaring constant record type containing constant arrays

 ̄綄美尐妖づ 提交于 2019-12-10 20:47:35

问题


I have many constant arrays that do not all have the same number of elements.

To store these arrays, I have declared an array type large enough to store (or reference?) every element of the largest of these arrays:

type
  TElements = array [1 .. 1024] of Single;

Each of these TElements arrays are logically associated with one other TElements array that does have the same number of elements.

So to pair up these equally-sized arrays, I have declared a record type as:

type
  TPair = record
    n : Integer; // number of elements in both TElements arrays
    x : ^TElements;
    y : ^TElements;
  end;

I am then defining constant TPair records containing the constant TElements array pairs:

const

  ElementsA1 : array [1 .. 3] of Single = (0.0, 1.0,  2.0);
  ElementsA2 : array [1 .. 3] of Single = (0.0, 10.0, 100.0);
  ElementsA  : TPair =
  (
    n : 3;
    x : @ElementsA1;
    y : @ElementsA2;
  );

  ElementsB1 : array [1 .. 4] of Single = (0.0, 1.0,  2.0,   3.0);
  ElementsB2 : array [1 .. 4] of Single = (0.0, 10.0, 100.0, 1000.0);
  ElementsB  : TPair =
  (
    n : 4;
    x : @ElementsB1;
    y : @ElementsB2;
  );  

This seems like an inefficient way to reference the array data (maybe not, I dunno).

I would like to maintain a single constant data type (a "pair" data type) that contains two constant arrays.

Within each "pair", both arrays are guaranteed to have the same number of elements.

However, it can not be guaranteed that the number of array elements in one "pair" will equal the number of array elements in any other "pair".

Is there a way to declare a constant "pair" data type so that the contained array sizes are determined by the constant array definition?

Ideally, I would like to get rid of the TElements type and the awkward pointers. Something like this would be cool if it would compile:

type
  TPair = record
    x : array of Single; 
    y : array of Single; 
  end;

const

  ElementsA : TPair =
  (
    x : (0.0, 1.0,  2.0);
    y : (0.0, 10.0, 100.0);
  );

  ElementsB : TPair =
  (
    x : (0.0, 1.0,  2.0,   3.0);
    y : (0.0, 10.0, 100.0, 1000.0);
  );

But I guess since the arrays are declared as dynamic arrays, it doesn't want to allocate memory for them before runtime?


回答1:


Is there a way to declare a constant "pair" data type so that the contained array sizes are determined by the constant array definition?

No, sadly this is not possible. You have to declare the size of your array inside the square brackets.



来源:https://stackoverflow.com/questions/8187952/delphi-declaring-constant-record-type-containing-constant-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!