home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / ada / 3321 < prev    next >
Encoding:
Text File  |  1992-11-20  |  2.5 KB  |  78 lines

  1. Newsgroups: comp.lang.ada
  2. Path: sparky!uunet!noc.near.net!inmet!spock!stt
  3. From: stt@spock.camb.inmet.com (Tucker Taft)
  4. Subject: Re: [null arrays]
  5. Message-ID: <1992Nov20.204935.27016@inmet.camb.inmet.com>
  6. Sender: news@inmet.camb.inmet.com
  7. Nntp-Posting-Host: spock
  8. Organization: Intermetrics Inc, Cambridge MA
  9. References: <Bxz5M0.AxC@dale.cts.com>
  10. Date: Fri, 20 Nov 1992 20:49:35 GMT
  11. Lines: 65
  12.  
  13. In article <Bxz5M0.AxC@dale.cts.com> 
  14.   jhb@dale.cts.com (John Bollenbacher) writes:
  15.  
  16. >Thanks to all that responded to my initial mail on this thread.  I have a
  17. >followup question.  Is there a more straightforward way to code the function 
  18. >(REMOVE) below, given the restriction on sliding in aggregate creation?
  19. >
  20. >package TEST is
  21. >  subtype T is NATURAL range 0 .. 10;
  22. >
  23. >  type ARR is array (T range <>) of BOOLEAN;
  24. >  
  25. >  type A(N : T := 0) is record 
  26. >    DATA : ARR(1..N);
  27. >  end record;  
  28. >  function REMOVE(ELEMENT : BOOLEAN;
  29. >                  FROM    : A) return A;
  30. >  
  31. >end TEST; 
  32. >package body TEST is
  33. >  function REMOVE(ELEMENT : BOOLEAN;
  34. >                  FROM    : A) return A is
  35. >    RESULT : ARR(1..FROM.N-1);
  36. >  begin
  37. >    for I in FROM.DATA'RANGE loop
  38. >      if FROM.DATA(I) = ELEMENT then
  39. >        RESULT := FROM.DATA(1..I-1) & FROM.DATA(I+1..FROM.N);
  40. >        return (FROM.N - 1, RESULT);
  41. >      end if;
  42. >    end loop;    
  43. >    return FROM;
  44. >  end REMOVE; 
  45. >  
  46. >end TEST;
  47.  
  48. Yes, this is a little more efficient as it avoids the extra assignment,
  49. by using explicit array subtype conversion to achieve the "sliding.":
  50.  
  51.     function Remove(Element : Boolean;
  52.                     From : A) return A is
  53.       subtype Result_Subtype is Arr(1..From.N-1);
  54.     begin
  55.       for I in From.Data'Range loop
  56.         if From.Data(I) = Element then
  57.           return(From.N-1, Result_Subtype(
  58.             From.Data(1..I-1) & From.Data(I+1..From.N));
  59.         end if;
  60.       end loop;
  61.       return From;
  62.     end Remove;
  63.  
  64. If you expect it is common for Remove to have no effect, then you
  65. could save a few more cycles by moving the declaration of the
  66. Result_Subtype into a nested block statement surrounding the
  67. statement returning the aggregate.
  68.  
  69. >- John Bollenbacher                                        jhb@dale.cts.com -
  70. >- Titan Linkabit Corp.                                       (619) 552-9963 -
  71. >- 3033 Sience Park Rd.                                                      -
  72. >- San Diego, Ca. 92121                                                      -
  73.  
  74. S. Tucker Taft  stt@inmet.com
  75. Intermetrics, Inc.
  76. Cambridge, MA  02138
  77.