home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / ada / 2401 < prev    next >
Encoding:
Internet Message Format  |  1992-08-21  |  2.8 KB

  1. Path: sparky!uunet!usc!sdd.hp.com!mips!mips!munnari.oz.au!yoyo.aarnet.edu.au!sirius.ucs.adelaide.edu.au!cs.adelaide.edu.au!andrewd
  2. From: andrewd@cs.adelaide.edu.au (Andrew Dunstan)
  3. Newsgroups: comp.lang.ada
  4. Subject: Re: Instanciation
  5. Message-ID: <8278@sirius.ucs.adelaide.edu.au>
  6. Date: 22 Aug 92 01:52:25 GMT
  7. References: <tp923021.714292909@kari>
  8. Sender: news@ucs.adelaide.edu.au
  9. Lines: 67
  10. Nntp-Posting-Host: achilles.cs.adelaide.edu.au
  11.  
  12. In article <tp923021.714292909@kari>, tp923021@kari.canberra.edu.au (Ben Elliston) writes:
  13. |> Hi.
  14. |> 
  15. |> I've got a package which I'm currently coding which uses a static data
  16. |> structure (an array of records to be precise).  Now, this package is an
  17. |> ADT with a private data type.
  18. |> 
  19. |> I want to be able to modify the size of this array from within a client
  20. |> program at runtime.
  21. |> 
  22. |> Can anyone suggest how I might be able to go about this?
  23. |> 
  24. |> Thanks.
  25. |> 
  26.  
  27. Depends if you want to be able to modify it arbitrarily, or just at
  28. instantiation time. If the latter, make the package generic and pass in the
  29. size you want as a generic parameter. For more flexibility, you can't do
  30. it using a purely static data structure, since the size of such objects is
  31. irrevocably fixed by their declaration. You could try something like this:
  32.  
  33.  
  34. package mine is
  35.  
  36.    type my_type is private;
  37.    procedure expand(m : in out my_type; by : positive);
  38.    ...
  39. private
  40.    type my_array is array(positive range <>) of my_record;
  41.    type my_access is access my_array;
  42.    type my_type is record
  43.       ma : my_access := new my_array(1..initial_size);
  44.    end record;
  45.  
  46. end mine;
  47.  
  48. package body mine is
  49.  
  50.    procedure expand(m : in out my_type; by : positive) is
  51.       new_ma : my_access := new my_array(1..m.ma'length+by);
  52.    begin
  53.       new_ma(1..m.ma'length) := m.ma(1..m.ma'length);
  54.       m.ma := new_ma;
  55.    end expand;
  56.  
  57.    ...
  58. end mine;
  59.  
  60. Note:
  61.   (1) Self-organising structures, which expand automatically as needed
  62.       are better (and easier to use) than those which you have to
  63.       manipulate explicitly.
  64.   (2) Expansion in a scheme like this is better and more efficiently done
  65.       by expanding by a constant FACTOR, say 1.5 or 2. It turns out that
  66.       the ammortised cost of expanding is kept lower that way.
  67.   (3) I have not included any garbage collection in my skeleton code.
  68.       When I use this scheme, I generally include it, by using
  69.       unchecked_deallocation. 
  70. --  
  71. -- 
  72. #######################################################################
  73. #  Andrew Dunstan                   #   There's nothing good or bad   #
  74. #  Department of Computer Science   #   but thinking makes it so.     #
  75. #  University of Adelaide           #                                 #
  76. #  South Australia                  #          - Shakespeare          #
  77. #  net: andrewd@cs.adelaide.edu.au  #                                 #
  78. #######################################################################
  79.