Re: Discriminant computation problem
From: Jim Rogers (jimmaureenrogers_at_att.net)
Date: 11/15/04
- Previous message: Sandro Magi: "Re: Ada problem"
- In reply to: Sandro Magi: "Discriminant computation problem"
- Next in thread: Sandro Magi: "Re: Discriminant computation problem"
- Reply: Sandro Magi: "Re: Discriminant computation problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Previous message: Sandro Magi: "Re: Ada problem"
- In reply to: Sandro Magi: "Discriminant computation problem"
- Next in thread: Sandro Magi: "Re: Discriminant computation problem"
- Reply: Sandro Magi: "Re: Discriminant computation problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|