comp.lang.ada
 help / color / mirror / Atom feed
* Help: Dynamic-size arrays using record discriminant.
@ 1998-10-09  0:00 Alon Matas
  1998-10-11  0:00 ` Niklas Holsti
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Alon Matas @ 1998-10-09  0:00 UTC (permalink / raw)


-- 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.

-- For example, lets say it was filled with values:

begin
    for I in REC.VECTOR'RANGE loop
                REC.VECTOR(I):= I;
    end loop;

-- Now the array contains the values 1, 2, 3, 4 and 5. Lets say I want
to
-- enlarge the array to 6 cells. Theoretically, I can do this:

    REC:= (SIZE => 6, VECTOR => (1, 2, 3, 4, 5, 0));

-- But lets say I don't know the content of the first five cells (for
example,
-- they came from input). I must give SOME value to "VECTOR" (otherwise
it's
-- not a complete aggregate). Of course, I can do something like this:

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

-- ...but then I loose the original content of the array!

-- The only solution I could come up with is quite a shabby one. It goes

-- like this:

        TEMP:= (SIZE => 6, VECTOR => (others => 0));          -- Copying
the original

-- array into a

-- temporary array.
        TEMP.VECTOR(REC.VECTOR'RANGE):= REC.VECTOR;  -- Enlarging the

-- original

-- array.
        REC:= (SIZE => 6, VECTOR => TEMP.VECTOR);    -- Copying the
temporary

-- array into the

-- original array.

-- It's working but it's ugly. Is there a better way?

-- If you have an idea, please help!
-- Thanks!

end PROBLEM;




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Help: Dynamic-size arrays using record discriminant.
  1998-10-09  0:00 Help: Dynamic-size arrays using record discriminant Alon Matas
@ 1998-10-11  0:00 ` Niklas Holsti
  1998-10-13  0:00 ` Robert I. Eachus
  1998-10-16  0:00 ` Samuel Mize
  2 siblings, 0 replies; 4+ messages in thread
From: Niklas Holsti @ 1998-10-11  0:00 UTC (permalink / raw)


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




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Help: Dynamic-size arrays using record discriminant.
  1998-10-09  0:00 Help: Dynamic-size arrays using record discriminant Alon Matas
  1998-10-11  0:00 ` Niklas Holsti
@ 1998-10-13  0:00 ` Robert I. Eachus
  1998-10-16  0:00 ` Samuel Mize
  2 siblings, 0 replies; 4+ messages in thread
From: Robert I. Eachus @ 1998-10-13  0:00 UTC (permalink / raw)


In article <361E3D0C.41AAC13B@classnet.co.il> Alon Matas <amatas@classnet.co.il> writes:

   -- 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:

   -- The question is how do I change the array size (i.e, the discriminant
   -- value) WITHOUT loosing its current content...

   Easy, make the discriminant an element of the record.

   If you insist on being clever, do the following.  First insure that
your compiler actually allocates the maximum size necessary for the
largest possible value of the object.  Next, declare a type which
overlays exactly the discriminated record type, but where the
discriminant is replaced by a array component.  Now use address
clauses to do the aliasing and do what you want...

   Or you can just realize that some compilers will create the objects
in a way that requires hidden heap allocations and pointers, and from
the fact that your declarations:

        type ARRAY_TYPE  is array (INTEGER range <>) of NATURAL;
        type RECORD_TYPE (SIZE: INTEGER:= 5) is
                record
                        VECTOR: ARRAY_TYPE (1..SIZE);
                end record;

    don't lead to a compiler warning, that you are using such an
implementation.  So not only is it necessary to copy the old value to
the new object, but a clever compiler will (depending on how you write
the code) optimize away the "extra" copy.
--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Help: Dynamic-size arrays using record discriminant.
  1998-10-09  0:00 Help: Dynamic-size arrays using record discriminant Alon Matas
  1998-10-11  0:00 ` Niklas Holsti
  1998-10-13  0:00 ` Robert I. Eachus
@ 1998-10-16  0:00 ` Samuel Mize
  2 siblings, 0 replies; 4+ messages in thread
From: Samuel Mize @ 1998-10-16  0:00 UTC (permalink / raw)


In article <361E3D0C.41AAC13B@classnet.co.il>,
Alon Matas  <amatas@classnet.co.il> wrote:
>-- I have this problem:

I believe this will work:

    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;
        REC: RECORD_TYPE;
             -- SIZE will be 5 by default
             -- enough space will be allocated by many compilers
             --  to hold largest possible value, i.e. with
             --  SIZE=integer'last
    begin
        -- * * *
         -- REC gets initial values somehow
        -- * * *


        -- add New_Value to the end of Rec
        Rec :=
            (SIZE => SIZE + 1,
             VECTOR => (VECTOR'RANGE
                         => VECTOR (VECTOR'FIRST..VECTOR'LAST),
                        VECTOR'LAST + 1 => New_Value));

Best,
Sam Mize

-- 
Samuel Mize -- smize@imagin.net (home email) -- Team Ada
Fight Spam: see http://www.cauce.org/ \\\ Smert Spamonam




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1998-10-16  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-10-09  0:00 Help: Dynamic-size arrays using record discriminant Alon Matas
1998-10-11  0:00 ` Niklas Holsti
1998-10-13  0:00 ` Robert I. Eachus
1998-10-16  0:00 ` Samuel Mize

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