From: noc.near.net!inmet!spock!stt@uunet.uu.net (Tucker Taft)
Subject: Re: [null arrays]
Date: 20 Nov 92 20:49:35 GMT [thread overview]
Message-ID: <1992Nov20.204935.27016@inmet.camb.inmet.com> (raw)
In article <Bxz5M0.AxC@dale.cts.com>
jhb@dale.cts.com (John Bollenbacher) writes:
>Thanks to all that responded to my initial mail on this thread. I have a
>followup question. Is there a more straightforward way to code the function
>(REMOVE) below, given the restriction on sliding in aggregate creation?
>
>package TEST is
> subtype T is NATURAL range 0 .. 10;
>
> type ARR is array (T range <>) of BOOLEAN;
>
> type A(N : T := 0) is record
> DATA : ARR(1..N);
> end record;
>
> function REMOVE(ELEMENT : BOOLEAN;
> FROM : A) return A;
>
>end TEST;
>package body TEST is
> function REMOVE(ELEMENT : BOOLEAN;
> FROM : A) return A is
> RESULT : ARR(1..FROM.N-1);
> begin
> for I in FROM.DATA'RANGE loop
> if FROM.DATA(I) = ELEMENT then
> RESULT := FROM.DATA(1..I-1) & FROM.DATA(I+1..FROM.N);
> return (FROM.N - 1, RESULT);
> end if;
> end loop;
> return FROM;
> end REMOVE;
>
>end TEST;
Yes, this is a little more efficient as it avoids the extra assignment,
by using explicit array subtype conversion to achieve the "sliding.":
function Remove(Element : Boolean;
From : A) return A is
subtype Result_Subtype is Arr(1..From.N-1);
begin
for I in From.Data'Range loop
if From.Data(I) = Element then
return(From.N-1, Result_Subtype(
From.Data(1..I-1) & From.Data(I+1..From.N));
end if;
end loop;
return From;
end Remove;
If you expect it is common for Remove to have no effect, then you
could save a few more cycles by moving the declaration of the
Result_Subtype into a nested block statement surrounding the
statement returning the aggregate.
>- John Bollenbacher jhb@dale.cts.com -
>- Titan Linkabit Corp. (619) 552-9963 -
>- 3033 Sience Park Rd. -
>- San Diego, Ca. 92121 -
S. Tucker Taft stt@inmet.com
Intermetrics, Inc.
Cambridge, MA 02138
next reply other threads:[~1992-11-20 20:49 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
1992-11-20 20:49 Tucker Taft [this message]
-- strict thread matches above, loose matches on Subject: below --
1992-11-19 17:51 [null arrays] John Bollenbacher
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox