home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.ada
- Path: sparky!uunet!noc.near.net!inmet!spock!stt
- From: stt@spock.camb.inmet.com (Tucker Taft)
- Subject: Re: [null arrays]
- Message-ID: <1992Nov20.204935.27016@inmet.camb.inmet.com>
- Sender: news@inmet.camb.inmet.com
- Nntp-Posting-Host: spock
- Organization: Intermetrics Inc, Cambridge MA
- References: <Bxz5M0.AxC@dale.cts.com>
- Date: Fri, 20 Nov 1992 20:49:35 GMT
- Lines: 65
-
- 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
-