From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,3abdaa39e1c27f49 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!fu-berlin.de!uni-berlin.de!not-for-mail From: "Nick Roberts" Newsgroups: comp.lang.ada Subject: Re: Discriminant computation problem Date: Tue, 16 Nov 2004 20:00:01 -0000 Message-ID: <2vv4icF2pe63tU1@uni-berlin.de> References: <2vsns5F2p6vbpU1@uni-berlin.de> X-Trace: news.uni-berlin.de 98NpkgXgI6I0RQU0l4J7SgBC62rkbEs/5sW85uah/1ELdAu5Q= X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-RFC2646: Format=Flowed; Original Xref: g2news1.google.com comp.lang.ada:6231 Date: 2004-11-16T20:00:01+00:00 List-Id: 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