comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Copying rows in a two dimensional array.
Date: Mon, 01 Feb 2010 08:55:01 +0200
Date: 2010-02-01T08:55:01+02:00	[thread overview]
Message-ID: <4b667ac4$0$6289$4f793bc4@news.tdc.fi> (raw)
In-Reply-To: <4b6637a1$0$4586$4d3efbfe@news.sover.net>

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
       .      @       .



  parent reply	other threads:[~2010-02-01  6:55 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-02-01  2:11 Copying rows in a two dimensional array Peter C. Chapin
2010-02-01  4:42 ` Jeffrey R. Carter
2010-02-01  6:55 ` Niklas Holsti [this message]
2010-02-01 23:36   ` Peter C. Chapin
2010-02-04  4:27   ` Hibou57 (Yannick Duchêne)
2010-02-01  8:37 ` Dmitry A. Kazakov
2010-02-02  0:11   ` Randy Brukardt
2010-02-07 16:13     ` Robert A Duff
2010-02-08  6:30       ` tmoran
2010-02-08 13:15         ` Robert A Duff
2010-02-08 13:45           ` Dmitry A. Kazakov
2010-02-08 21:20             ` Robert A Duff
2010-02-08 23:26               ` (see below)
2010-02-09  0:36                 ` Randy Brukardt
2010-02-09  1:03                   ` (see below)
2010-02-09  7:11                   ` Pascal Obry
2010-02-09  8:14                     ` AdaMagica
2010-02-09 14:33                 ` Robert A Duff
2010-02-09  1:05               ` Adam Beneschan
2010-02-09 14:45                 ` Robert A Duff
2010-02-09 18:50                   ` tmoran
2010-02-09 19:51                   ` Pascal Obry
2010-02-09 23:03                     ` Robert A Duff
2010-02-08 18:53           ` tmoran
2010-02-08 21:14             ` Robert A Duff
2010-02-08 21:29               ` Pascal Obry
2010-02-09  8:56                 ` Jean-Pierre Rosen
2010-02-09  9:14                   ` AdaMagica
2010-02-09 11:19                     ` Jean-Pierre Rosen
2010-02-09 14:26                 ` Robert A Duff
2010-02-09  6:34               ` tmoran
2010-02-09 14:29                 ` Robert A Duff
2010-02-09 18:49                   ` tmoran
2010-02-09 22:58                     ` Robert A Duff
2010-02-01 22:10 ` Jerry
2010-02-02  0:07   ` Randy Brukardt
2010-02-02  8:52   ` Jean-Pierre Rosen
2010-02-02 22:23     ` Jerry
2010-02-03  1:24       ` Adam Beneschan
2010-02-04  4:42     ` Hibou57 (Yannick Duchêne)
2010-02-14  0:42     ` jonathan
2010-02-14  1:54       ` Hibou57 (Yannick Duchêne)
2010-02-14 16:16         ` jonathan
2010-03-22  8:56           ` Ole-Hjalmar Kristensen
2010-02-16  6:51     ` David Thompson
2010-02-04  4:13 ` Hibou57 (Yannick Duchêne)
2010-02-04  9:10   ` Dmitry A. Kazakov
2010-02-04  9:23     ` Hibou57 (Yannick Duchêne)
replies disabled

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