comp.lang.ada
 help / color / mirror / Atom feed
From: Jim Rogers <jimmaureenrogers@att.net>
Subject: Re: Discriminant computation problem
Date: Mon, 15 Nov 2004 03:24:14 GMT
Date: 2004-11-15T03:24:14+00:00	[thread overview]
Message-ID: <yjVld.906538$Gx4.504752@bgtnsc04-news.ops.worldnet.att.net> (raw)
In-Reply-To: NmTld.122$rc.97840@news20.bellglobal.com

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




  reply	other threads:[~2004-11-15  3:24 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-11-15  1:11 Discriminant computation problem Sandro Magi
2004-11-15  3:24 ` Jim Rogers [this message]
2004-11-15 14:02   ` Sandro Magi
2004-11-15 15:16     ` Martin Krischik
2004-11-15 16:02     ` Dmitry A. Kazakov
2004-11-15 22:11 ` Nick Roberts
2004-11-15 23:25   ` tmoran
2004-11-16 20:00     ` Nick Roberts
2004-11-16 20:14       ` tmoran
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox