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

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