home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!munnari.oz.au!goanna!minyos.xx.rmit.oz.au!stan!saturn!rob
- From: rob@saturn.cs.swin.OZ.AU (Rob Allen)
- Newsgroups: comp.lang.ada
- Subject: Re: Generics?
- Message-ID: <6978@stan.xx.swin.oz.au>
- Date: 9 Sep 92 01:34:35 GMT
- References: <tp923021.715953043@jarrah>
- Sender: news@stan.xx.swin.oz.au
- Lines: 45
-
- tp923021@jarrah.canberra.edu.au (Ben Elliston) writes:
- >I've since moved onto making an array a generic type (such as this):
- >GENERIC
- >Type Index IS (<>);
- >[..]
- >Type Stack_Array IS Array(Index) of Character;
-
- >Type Stack_Type IS RECORD
- > Data_Store: Stack_Array;
- > Pointer: Natural;
- >END RECORD;
-
- >As it stands now, it seems that since the "pointer" variable (which
- >indicates the current "highwater" mark of the array) is a natural, that
- >Ada won't allow a natural to access an array indexed by some unknown
- >(generic) type.
- >For example, if the array was instantiated using a subtype of naturals
- >(0 .. 100), allowing a natural to access elements of the array could
- >lead to errors (of course, stack.data_store(103) would cause problems).
- >Basically, I need to be able to "look at" any element in the array
- >regardless of how large an array is defined in the given procedure using
- >this package.
- >I'm not too familiar with generics, and if someone could shed some light
- >on this, I would really appreciate it.
- >------------------------------------------------------------------------------
- Why not declare Pointer to be of type Index ? You can use it directly as
- a subscript, initiallize it to Index'first and check for full against
- Index'last. However if the store is allowed to be logically empty then
- there is a classic problem: it is illegal to initiallise Pointer to the
- item before the first [Index'pred(Index'first)]. The easiest solution is
- to waste the first array element.
- If however you decide to keep Pointer as a Natural where 1 corresponds to
- the first element then always use it as
- X.Data_Store(Index'val(X.Pointer - 1 + Pos_1st))
- where
- Pos_1st : constant := Index'pos(Index'first);
- is to account for the likelihood that the actual Index is an arbitrary
- subtype of a discrete type. Using this approach the 'full' test is
- X.Pointer = Index'length
- Though none of the above code has been checked, I hope that helps. rob
- --
- +---------------------------------------------------------------------+
- | Robert K Allen email: rob@saturn.cs.swin.oz.au |
- | Computer Science, snail: PO Box 218, Hawthorn, |
- | Swinburne University of Technology, VIC 3122 Australia |
-