From: munnari.oz.au!goanna!minyos.xx.rmit.oz.au!stan!saturn!rob@uunet.uu.net ( Rob Allen)
Subject: Re: Generics?
Date: 9 Sep 92 01:34:35 GMT [thread overview]
Message-ID: <6978@stan.xx.swin.oz.au> (raw)
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 |
next reply other threads:[~1992-09-09 1:34 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
1992-09-09 1:34 Rob Allen [this message]
-- strict thread matches above, loose matches on Subject: below --
1992-09-09 5:26 Generics? Andrew Dunstan
1992-09-14 15:00 Generics? cis.ohio-state.edu!zaphod.mps.ohio-state.edu!darwin.sura.net!jvnc.net!net
2001-07-02 18:42 Generics??? Beard, Frank
2001-07-03 10:32 ` Generics??? Anders Wirzenius
2001-07-03 17:31 ` Generics??? Stephen Leake
2001-07-03 19:09 ` Generics??? tmoran
2001-07-04 5:30 ` Generics??? Anders Wirzenius
2001-07-02 22:33 Generics??? Michael Andersson
2001-07-02 17:48 ` Generics??? James Rogers
2001-07-02 18:54 ` Generics??? Ehud Lamm
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox