德尔福:声明包含数组常量不变的记录类型 [英] Delphi: Declaring constant record type containing constant arrays

查看:142
本文介绍了德尔福:声明包含数组常量不变的记录类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多常量数组是的的都具有相同数量的元素。

要存储这些阵列,我宣布一个数组类型大到足以存储的最大的这些阵列的每个元素(或参考):

 键入
  单TElements =数组[1 .. 1024];

每个这些TElements阵列逻辑上与另一个TElements阵列相关联的确实的有相同数量的元素。

所以要配对这些同样大小的数组,我宣布一个记录类型为:

 键入
  TPair =记录
    n:整数; //元件在两个TElements阵列数
    X:^ TElements;
    Y:^ TElements;
  结束;

我然后定义包含常数TElements阵列偶不变TPair记录:

 常量  ElementsA1:数组[1 .. 3]单=(0.0,1.0,2.0);
  ElementsA2:数组[1 .. 3]单=(0.0,10.0,100.0);
  ElementsA:TPair =
  (
    N:3;
    X:@ ElementsA1;
    Y:@ ElementsA2;
  );  ElementsB1:数组[1 .. 4]单=(0.0,1.0,2.0,3.0);
  ElementsB2:数组[1 .. 4]单=(0.0,10.0,100.0,1000.0);
  ElementsB:TPair =
  (
    N:4;
    X:@ ElementsB1;
    Y:@ ElementsB2;
  );


这似乎是引用数组数据(也许不是,我不知道)的有效方式。

我想保持一个恒定的数据类型,它包含两个常量数组(一个双数据类型)。

在每个对子,两个阵列都保证有相同数量的元素。

然而,它不能保证该阵列元件的一个配对的数量将等于阵列元素的数量在任何其他的一对。

有这么所包含的数组的大小是由常量数组定义决心声明常数配对的数据类型的方法吗?

在理想情况下,我想摆脱TElements类型和尴尬的指针。这样的事情将是冷静,如果它会编译:

 键入
  TPair =记录
    X:单阵列;
    Y:单数组;
  结束;常量  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);
  );

不过,我猜想因为数组被声明为动态数组,它不希望在运行之前为他们分配内存?


修改

对不起,在编辑回答这个问题,但我可以接受的格式或无法评论回答我的问题了8小时。

不知道这个解决方案是可行的所有类似的情况,但我发现接近这个简单(回想起来还挺现在很明显)的方式。

我移除了TElements和TPair类型。然后我宣布每对一个2维数组:

 常量  ElementsA:数组[1..2]数组[1 .. 3]单=
  (
    (0.0,1.0,2.0),
    (0.0,10.0,100.0)
  );

希望这有助于!


解决方案

  

有没有声明常数配对数据类型的方式,使所包含的数组的大小是由常量数组定义确定的?


没有,可悲的是这是不可能的。你必须声明你的数组方括号中的大小。

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?


EDIT

Sorry for answering this in an edit, but I can't comment with formatting or answer my question for another 8 hours..

Not sure if this solution is viable for all similar situations, but I've found a simpler (and kinda obvious now in retrospect) way to approach this.

I removed both the TElements and the TPair types. I then declared each pair as a 2-dimensional array:

const

  ElementsA : array [1 .. 2] of array [1 .. 3] of Single =
  (
    (0.0, 1.0,  2.0),
    (0.0, 10.0, 100.0)
  );

Hope this helps!

解决方案

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.

这篇关于德尔福:声明包含数组常量不变的记录类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆