home *** CD-ROM | disk | FTP | other *** search
- 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
- From: andrewd@cs.adelaide.edu.au (Andrew Dunstan)
- Newsgroups: comp.lang.ada
- Subject: Re: Instanciation
- Message-ID: <8278@sirius.ucs.adelaide.edu.au>
- Date: 22 Aug 92 01:52:25 GMT
- References: <tp923021.714292909@kari>
- Sender: news@ucs.adelaide.edu.au
- Lines: 67
- Nntp-Posting-Host: achilles.cs.adelaide.edu.au
-
- In article <tp923021.714292909@kari>, tp923021@kari.canberra.edu.au (Ben Elliston) writes:
- |> Hi.
- |>
- |> I've got a package which I'm currently coding which uses a static data
- |> structure (an array of records to be precise). Now, this package is an
- |> ADT with a private data type.
- |>
- |> I want to be able to modify the size of this array from within a client
- |> program at runtime.
- |>
- |> Can anyone suggest how I might be able to go about this?
- |>
- |> Thanks.
- |>
-
- Depends if you want to be able to modify it arbitrarily, or just at
- instantiation time. If the latter, make the package generic and pass in the
- size you want as a generic parameter. For more flexibility, you can't do
- it using a purely static data structure, since the size of such objects is
- irrevocably fixed by their declaration. You could try something like this:
-
-
- package mine is
-
- type my_type is private;
- procedure expand(m : in out my_type; by : positive);
- ...
- private
- type my_array is array(positive range <>) of my_record;
- type my_access is access my_array;
- type my_type is record
- ma : my_access := new my_array(1..initial_size);
- end record;
-
- end mine;
-
- package body mine is
-
- procedure expand(m : in out my_type; by : positive) is
- new_ma : my_access := new my_array(1..m.ma'length+by);
- begin
- new_ma(1..m.ma'length) := m.ma(1..m.ma'length);
- m.ma := new_ma;
- end expand;
-
- ...
- end mine;
-
- Note:
- (1) Self-organising structures, which expand automatically as needed
- are better (and easier to use) than those which you have to
- manipulate explicitly.
- (2) Expansion in a scheme like this is better and more efficiently done
- by expanding by a constant FACTOR, say 1.5 or 2. It turns out that
- the ammortised cost of expanding is kept lower that way.
- (3) I have not included any garbage collection in my skeleton code.
- When I use this scheme, I generally include it, by using
- unchecked_deallocation.
- --
- --
- #######################################################################
- # Andrew Dunstan # There's nothing good or bad #
- # Department of Computer Science # but thinking makes it so. #
- # University of Adelaide # #
- # South Australia # - Shakespeare #
- # net: andrewd@cs.adelaide.edu.au # #
- #######################################################################
-