[comp.lang.ada] Abstraction of variable size vectors

binette@asterix.drev.dnd.ca (Louis-Simon Binette) (08/02/90)

                                                 Valcartier, August 1st

      I post this message again since I just realized that the original
one (dated July 23 or so..) contained an error in its IP address (I did
not receive any answer to it !).   I apologize for those who spent time
trying to send me an answer.  Anyway here is the message with the right
IP address:

Dear Folks,

      I am currently in the process of designing a generic package that
encapsulates variable size vectors whose components are referenced by a
discrete type. The basic declarations of the package specification are:

generic
  type GT_ELM is private;   -- Element type
  type GT_NDX is (<>);      -- Index type of vector
package VECTOR_MNGR_TEMPLATE is
  type T_VECTOR is limited private;
  -- Other declarations come here (types and subprograms)

       with T_VECTOR being the abstract data type for the variable size
vector.  In order to extend the functionality of the language with res-
pect to the bounds of arrays [LRM 3.6.1] I have encapsulated the actual
values of the lower and upper bounds as part of the vector structure:

private
  ...;
  type T_VECTOR is record
                     LOWER_BOUND,
                     UPPER_BOUND : ...;  -- Base type of GT_NDX !!!
                     ...;
                   end record;
  ...;
end VECTOR_MNGR_TEMPLATE;

      As you might know, the range covered by the bounds of the array is
defined by the _base type_ of GT_NDX [LRM 3.5 (4)] in order to cope with
_null arrays_.  Unfortunately, a type declaration like:

  type T_BASE_NDX is range GT_NDX'BASE'FIRST..GT_NDX'BASE'LAST;

      in the package is illegal since the bounds are not static (as spe-
cified in LRM 4.9 (11): "...other than a generic formal type;...").

     Any opinion about what looks to me as a lack of extensibility of the
language?  I would greatly appreciate suggestions (if any !) to this pro-
blem while keeping the genericity ("type GT_NDX is (<>);") of the package


                                      Thanks in advance

                                      Louis-Simon Binette
                                      Internet : simon@quebec.drev.dnd.ca
                                      IP...... : simon@131.132.32.17

NCOHEN@IBM.COM ("Norman H. Cohen") (08/02/90)

Ref: INFO-ADA Digest Volume 90 Issue 148 (Wed, Aug 1, 1990) Item #1

It took me a while to figure out what Louis-Simon Binette was trying to
do.  As I understand it, he is trying to imitate Ada arrays with his
own private type, specifying the index and components as generic
parameters.  He needs to store descriptor information--upper and lower
bounds of his imitation arrays--but these values may come from outside
the index subtype.  For example (by analogy to a null string), the
index subtype may be Positive, the lower bound may be one, AND THE UPPER
BOUND MAY BE ZERO.

The solution is simply to pass the index BASE type rather than the index
SUBtype as the generic actual parameter:

   generic
      type Element_Type is private;
      type Index_Base_Type is (<>);
      Index_Subtype_Lower_Bound, Index_Subtype_Upper_Bound:
         in Index_Base_Type;
   package Vector_Manager_Template is
      type Vector is limited private;
      ...
   private
      type Vector is
         record
            Vector_Lower_Bound, Vector_Upper_Bound: Index_Base_Type;
            ...
         end record;
   end Vector_Manager_Template;

The index subtype bounds are passed as generic parameters so that
when a Vector value is created with specified bounds, the package can
check whether the bounds are compatible with the index subtype.

Norman H. Cohen