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: a07f3367d7,bf03d731a6ef511f X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!feeder3.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.84.MISMATCH!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!syros.belnet.be!news.belnet.be!newsfeed00.sul.t-online.de!t-online.de!tiscali!newsfeed1.ip.tiscali.net!fi.sn.net!newsfeed2.fi.sn.net!news.song.fi!not-for-mail Date: Mon, 01 Feb 2010 08:55:01 +0200 From: Niklas Holsti Organization: Tidorum Ltd User-Agent: Mozilla-Thunderbird 2.0.0.22 (X11/20090706) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Copying rows in a two dimensional array. References: <4b6637a1$0$4586$4d3efbfe@news.sover.net> In-Reply-To: <4b6637a1$0$4586$4d3efbfe@news.sover.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <4b667ac4$0$6289$4f793bc4@news.tdc.fi> NNTP-Posting-Host: 81.17.205.61 X-Trace: 1265007300 news.tdc.fi 6289 81.17.205.61:44127 X-Complaints-To: abuse@tdcnet.fi Xref: g2news1.google.com comp.lang.ada:8844 Date: 2010-02-01T08:55:01+02:00 List-Id: Peter C. Chapin wrote: > This is something of a newbie question.. > > I'm working with a two dimensional array of floating point values. Lets call > it A. The array has type > > type Matrix is array(Positive range <>, Positive range <>) of Floating_Type; > > I need to exchange two rows in this array. What I'd like to do is something > along these lines: > > Temp_Array := A(I, 1 .. Size); > A(I, 1 .. Size) := A(K, 1 .. Size); > A(K, 1 .. Size) := Temp_Array; > > The compiler (GNAT GPL 2009) has a problem with this syntax and, after looking > into it some, I think that's because slicing only works for one dimensional > arrays. Fair enough. > > So I thought, "Perhaps A needs to be an array of arrays." > > type Matrix is array(Positive range <>) of WHAT_EXACTLY? > > Apparently the component type of an array needs to be fully constrained (which > again makes sense) yet I don't know the size I'll want to use at the point > where this type is declared. One solution -- which may not match your design in other respects -- is to make the Matrix an array of accesses to rows: type Row is array (Positive range <>) of Float; type Row_Ref is access Row; type Matrix is array (Positive range <>) of Row_Ref; Exchanging two rows is then very quick: Temp_Ref := A(I); A(I) := A(K); A(K) := Temp_Ref; However, you now have to allocate the rows using "new Row (1 .. Size)" and perhaps later deallocate them using Unchecked_Deallocation. Another solution -- which perhaps you have considered -- is to use a generic package to define row and matrix types: generic Size : Positive; package Matrices is type Row is array (1 .. Size) of Float; type Matrix is array (1 .. Size) of Row; end Matrices; However, this will probably force some other parts of your code to become generic, too, parametrized either by a Size, or by an instance of Matrices. -- Niklas Holsti Tidorum Ltd niklas holsti tidorum fi . @ .