From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Date: 20 Nov 92 20:49:35 GMT From: noc.near.net!inmet!spock!stt@uunet.uu.net (Tucker Taft) Subject: Re: [null arrays] Message-ID: <1992Nov20.204935.27016@inmet.camb.inmet.com> List-Id: In article 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