* re: [null arrays]
@ 1992-11-19 17:51 John Bollenbacher
0 siblings, 0 replies; 2+ messages in thread
From: John Bollenbacher @ 1992-11-19 17:51 UTC (permalink / raw)
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;
--
-----------------------------------------------------------------------------
- John Bollenbacher jhb@dale.cts.com -
- Titan Linkabit Corp. (619) 552-9963 -
- 3033 Sience Park Rd. -
- San Diego, Ca. 92121 -
-----------------------------------------------------------------------------
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [null arrays]
@ 1992-11-20 20:49 Tucker Taft
0 siblings, 0 replies; 2+ messages in thread
From: Tucker Taft @ 1992-11-20 20:49 UTC (permalink / 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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~1992-11-20 20:49 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1992-11-20 20:49 [null arrays] Tucker Taft
-- strict thread matches above, loose matches on Subject: below --
1992-11-19 17:51 John Bollenbacher
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox