Re: Discriminant computation problem

From: Jim Rogers (jimmaureenrogers_at_att.net)
Date: 11/15/04

  • Next message: Preben Randhol: "Re: Wiki on Ada"
    Date: Mon, 15 Nov 2004 03:24:14 GMT
    
    

    Sandro Magi <smagi@naasking.homeip.net> wrote in
    news:NmTld.122$rc.97840@news20.bellglobal.com:

    > Just learning Ada and I came across this little issue. I can't perform
    > a computation on a discriminant. Suppose I want to create a bit vector
    > type that uses an array of Integers as its underlying storage type
    > (just an example). I want the user to provide the length of the bit
    > vector in bits, but I need to define the upper bound on the Integer
    > array in Integer'Size units.
    >
    > type Int_Array is array (Positive range <>) of Integer;
    >
    > type Bit_Vector (Max_Bit_Size : Positive) is
    > record
    > Series : Int_Array (Positive'First .. Max_Bit_Size/Integer'Size);
    > end record;

    You might want to reconsider your design.
    First of all, your chosen example is a very poor way to represent
    a bit vector in Ada. This appears to be an Ada translation of a C
    or C++ programming approach.

    A more Ada-like way to accomplish your ends is:

    type Bit_Vector is array(Positive range <>) of Boolean;
    pragma Pack(Bit_Array);

    If you want to hide this implementation you can provide
    a package such as:

    package Bit_Vectors is
       type Bit_Vector(Max_Size : Positive) is private;
       ....
    private
       type Internal_Vector is array(Positive range <>) of Boolean;
       pragma Pack(Internal_Vector);
       type Bit_Vector(Max_Size : Positive) is record
          Buffer : Internal_Vector(Max_Size);
       end record;
    end Bit_Vectors;

    Each bit is individually addressable through array indexing.
    A packed array of boolean results in each boolean element
    occupying a single bit.

    There is no advantage in using an Integer type as an underlying
    element size. Integer is always a signed type. You will always
    error by not accounting for the sign bit.

    My second point deals with your desire to perform a calculation
    on a parameterized value. The more acceptable way to accomplish
    such an end is the use of a generic package rather than a
    record discriminant. The use of a generic package provides
    encapsulation and information hiding for your operations that
    prevent you from unnecessarily exposing internal data structures.

    Jim Rogers


  • Next message: Preben Randhol: "Re: Wiki on Ada"

    Relevant Pages

    • Re: array of generic packages?
      ... > package shapePointeris new shape; ... you cannot have an array of generic packages. ... type Triangle is ... ... the generic package is not really all that useful if all you ...
      (comp.lang.ada)
    • Re: Variable length arrays proposal
      ... specifying the discriminant which is fixed for the object' lifetime; ... >> nor could you make an array of these with varying lengths. ... do either in Ada; you can have an array of a single constrained ... String for this, since Standard.String is type array of Character ...
      (comp.std.c)
    • Re: Non repeating list of names
      ... I probably do not need a 4 dimension array as my discriminant is the part of line from the left to the first comma. ... So I guess the program you suggested could do that if I use some function that reads the line, returns the Name portion of the line as the discriminant for repeated value and then writes the whole line to the correct sublist? ...
      (microsoft.public.scripting.vbscript)
    • Re: Task discriminants
      ... "are tasks on an array elaborated in order from ... task's discriminant the same as its index on the array? ... (Obviously the start value of Id and the index range of the array have to be ... But if the order of elaboration isn't guaranteed, ...
      (comp.lang.ada)
    • Re: Task discriminants
      ... > This prompts the question, "are tasks on an array elaborated in order from ... > task's discriminant the same as its index on the array? ... > couldn't locate order of elaboration of array elements. ...
      (comp.lang.ada)