comp.lang.ada
 help / color / mirror / Atom feed
From: "Nick Roberts" <nick.roberts@acm.org>
Subject: Re: Discriminant computation problem
Date: Tue, 16 Nov 2004 20:00:01 -0000
Date: 2004-11-16T20:00:01+00:00	[thread overview]
Message-ID: <2vv4icF2pe63tU1@uni-berlin.de> (raw)
In-Reply-To: aVamd.100382$R05.30042@attbi_s53

<tmoran@acm.org> wrote in message news:aVamd.100382$R05.30042@attbi_s53...

> Another approach is to make the type controlled, then have the
> Initialize routine do the size calculations based on the discriminant
> and call an allocator for the actual data.  Finalize then deallocates
> the data, of course.

Illustration of this technique:

   with Ada.Finalization;
   package Bit_Vectors is
      ...
      type Bit_Vector (Bit_Max: Bit_Number) is private;
      ...
   private
      ... -- declare Element_Array etc. as before

      type Vector_Payload (Max: Element_Index) is
         record
            ... -- as for Bit_Vector before
         end record;

      type Payload_Access is access Vector_Payload;

      type Bit_Vector (Bit_Max: Bit_Number) is
         new Ada.Finalization.Controlled with
         record
            Payload: Vector_Access;
         end record;

      procedure Initialize (Vector: in out Bit_Vector);
      procedure Finalize   (Vector: in out Bit_Vector);
   end;

   package body Bit_Vectors is
      ...

      procedure Free is
         new Unchecked_Deallocation( Bit_Vector_Payload,
                                    Dynamic_Bit_Vector );

      procedure Initialize (Vector: in out Bit_Vector) is
      begin
         Vector.Payload := new Bit_Vector_Payload'(
            Max => Vector.Bit_Max/Bits_Per_Element,
            ... -- as before
            );
      end;

      procedure Finalize (Vector: in out Bit_Vector) is
      begin
         Free( Vector.Payload );
      end;

      ...
   end Bit_Vectors;

This technique has the disadvantage that the type Bit_Vector is tagged, and 
that controlled types use up some extra memory (to store the list of objects 
needing finalisation). But I would guess that this unlikely to be a problem 
in pratice. It has the advantage of being a little neater to use.

-- 
Nick Roberts





  reply	other threads:[~2004-11-16 20:00 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
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 [this message]
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