comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <nholsti@icon.fi>
Subject: Re: Help: Dynamic-size arrays using record discriminant.
Date: 1998/10/11
Date: 1998-10-11T00:00:00+00:00	[thread overview]
Message-ID: <36208A62.80E2640B@icon.fi> (raw)
In-Reply-To: 361E3D0C.41AAC13B@classnet.co.il

Alon Matas wrote:
> 
> -- I have this problem:
> -- There is a record with a discriminant (and a default value to the
> -- discriminant). The record contains a constrained array, and the range
> -- of the array depends on the discriminant. For example:
> 
> procedure PROBLEM is
> 
>         type ARRAY_TYPE  is array (INTEGER range <>) of NATURAL;
>         type RECORD_TYPE (SIZE: INTEGER:= 5) is
>                 record
>                         VECTOR: ARRAY_TYPE (1..SIZE);
>                 end record;
> 
> -- Now, I have a variable with the record type:
>         REC: RECORD_TYPE;
>         TEMP: RECORD_TYPE;  -- This variable will be explained later.
> 
> -- The question is how do I change the array size (i.e, the discriminant
> 
> -- value) WITHOUT loosing its current content.

   [text snipped]

> Of course, I can do something like this:
> 
>         REC:= (SIZE => 6, VECTOR => (others => 0));

You can do

    REC := (SIZE => 6, VECTOR => REC.VECTOR & (6 => 0));

However, the compiler is likely to generate a temporary and
copy VECTOR contents back and forth. If this is performance-
critical, you should perhaps consider other data structures
where existing VECTOR data remain in place. For example, the
SIZE could be an ordinary component, instead of a discriminant.
You must then constrain the VECTOR component to some assumed
maximum size.

You should anyway constrain the SIZE discriminant, since some
compilers will allocate RECORD_TYPE memory for the maximum
possible SIZE value (integer'last in your code).

- Niklas




  reply	other threads:[~1998-10-11  0:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-10-09  0:00 Help: Dynamic-size arrays using record discriminant Alon Matas
1998-10-11  0:00 ` Niklas Holsti [this message]
1998-10-13  0:00 ` Robert I. Eachus
1998-10-16  0:00 ` Samuel Mize
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox