From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on ip-172-31-65-14.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-0.9 required=3.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 Path: eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Marek Newsgroups: comp.lang.ada Subject: 2-dimensional view on 1 dimensional array Date: Sun, 23 Oct 2022 14:31:22 +0200 Organization: A noiseless patient Spider Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 23 Oct 2022 12:31:22 -0000 (UTC) Injection-Info: reader01.eternal-september.org; posting-host="bf5d702cf9c441d4f50ef8eb9cd2c095"; logging-data="1286601"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Usf13KML1ukaJv+0I9mCC" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.2 Cancel-Lock: sha1:RFR9JPy2cAa9AhHXiH5EgxGdkFQ= Content-Language: en-US Xref: reader01.eternal-september.org comp.lang.ada:64545 List-Id: Hi. Assume we have: generic type T is private; type T_Array is array (Natural range <>) of aliased T; type T_Array_Access is access all T_Array; package Buffers is type Row_Array is array (Natural range <>) of aliased T_Array_Access; type Row_Array_Access is access all Row_Array; type Buffer is tagged record Raw_Buffer : T_Array_Access := null; Rows_Table : Row_Array_Access := null; Rows : Natural := 0; Columns : Natural := 0; Step : Integer := 0; Max_Rows : Natural := 0; end record; procedure Init (This : in out Buffer; Buffer : T_Array_Access; Rows : Natural; Columns : Natural; Step : Integer); procedure Set_Value (This : in out Buffer; Value : T); end Buffers; ..and: with Ada.Unchecked_Deallocation; package body Buffers is type T_Access is access all T; ------------ -- Init -- ------------ procedure Init (This : in out Buffer; Buffer : T_Array_Access; Rows : Natural; Columns : Natural; Step : Integer) is procedure Free is new Ada.Unchecked_Deallocation (Row_Array, Row_Array_Access); Row_Index : Integer := 0; begin This.Raw_Buffer := Buffer; This.Rows := Rows; This.Columns := Columns; This.Step := Step; if Rows > This.Max_Rows then if This.Rows_Table /= null then Free (This.Rows_Table); end if; declare New_Rows : constant Row_Array_Access := new Row_Array (0 .. Rows - 1); begin This.Rows_Table := New_Rows; This.Max_Rows := Rows; end; end if; for H in 0 .. Rows - 1 loop declare Row_Start : constant T_Access := This.Raw_Buffer (Row_Index * Step)'Access; begin This.Rows_Table (H) := ... -- What code here? Row_Index := Row_Index + 1; end; end loop; end Init; ----------------- -- Set_Value -- ----------------- procedure Set_Value (This : in out Buffer; Value : T) is begin if This.Rows > 0 then for Y in 0 .. This.Rows - 1 loop declare Row : constant T_Array_Access := This.Rows_Table (Y); begin if This.Step > 0 then for X in 0 .. This.Step - 1 loop Row (X) := Value; end loop; end if; end; end loop; end if; end Set_Value; end Buffers; and finally: with Buffers; procedure Test is type Float_Array is array (Natural range <>) of aliased Float; type Float_Array_Access is access all Float_Array; package Buffer_Package is new Buffers (Float, Float_Array, Float_Array_Access); use Buffer_Package; A : aliased Float_Array := (0 .. 99 => 0.0); B : Buffer_Package.Buffer; begin B.Init (A'Access, 10, 10, 10); B.Set_Value (10.0); end Test; Is there any possibilities to get this working? Idea is to have another (2 dimensional) view on 1 dimensional array. Unchecked_Conversion is not working (different sizes of objects). Address_To_Access conversion also (unconstrained array); Marek