ardnew
ardnew

Reputation: 2086

Delphi: Declaring constant record type containing constant arrays

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?

Upvotes: 3

Views: 7331

Answers (1)

David Heffernan
David Heffernan

Reputation: 613013

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.

Upvotes: 4

Related Questions