home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / ada / 2563 < prev    next >
Encoding:
Internet Message Format  |  1992-09-08  |  2.5 KB

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