comp.lang.ada
 help / color / mirror / Atom feed
From: "Nick Roberts" <nickroberts@adaos.worldonline.co.uk>
Subject: Re: Multidimensional array vs. array of array
Date: Thu, 13 Dec 2001 21:49:35 -0000
Date: 2001-12-13T21:49:35+00:00	[thread overview]
Message-ID: <9vb864$e8rbj$3@ID-25716.news.dfncis.de> (raw)
In-Reply-To: slrna1grlf.k1.lutz@taranis.iks-jena.de

"Lutz Donnerhacke" <lutz@iks-jena.de> wrote in message
news:slrna1grlf.k1.lutz@taranis.iks-jena.de...

> >You can do it two different ways:
> >
> >1) Put the array of arrays in a record and return the record
>
> I'm unable to do this. Example?

I don't think this can be done.

> >2) Create an access type for the array of arrays. Dynamically
> >   allocate the array of arrays, and return its access value.
>
> *gna* No way. *drawing anti-satan symbols*

Anyway that solves nothing, in itself.

I suspect the only neat solution is for the next revision to permit
multi-dimensional slices, and a new kind of 'dimension reducing' array
conversion.

In the meantime, I can only suggest that you fudge by:

(1) using an appropriate multi-dimensional array; AND

(1a) redefine all your row/column functions so that they accept this
multidimensional array (and simply check that they have been given just one
row or column); OR

(1b) define a set of 'wrapper' functions that do this check, extract the row
or column into an appropriate array type, and then call the proper function,
perhaps doing the reverse conversion on the way back; OR

(1c) redefine all your row/column functions so that they accept this
multidimensional array plus an identification of the row/column to be
operated upon.

Horrid, yes, but I can't think of anything better. E.g.:


   type Matrix is array (Positive range <>,
                         Positive range <>) of Float;

   ...

   function Sum_Row (M: in Matrix) return Float is
      R: Float := 0.0;
   begin
      if M'Length(1) /= 1 then
         raise Constraint_Error; -- or another exception
      end if;
      for i in M'Range(2) loop
         R := R + M(M'First(1),i); -- M'First(1)=M'Last(1)=row
      end loop;
      return R;
   end;


An alternative with a wrapper function would be:


   type Vector is array (Positive range <>) of Float;

   ...

   function Sum (V: in Vector) return Float is
      R: Float := 0.0;
   begin
      for i in V'Range loop
         R := R + V(i);
      end loop;
      return R;
   end;

   ...

   function Sum_Row (M: in Matrix) return Float is
      V: Vector(M'Range(2));
   begin
      if M'Length(1) /= 1 then
         raise Constraint_Error; -- or another exception
      end if;
      for i in M'Range(2) loop
         V(i) := M'(M'First(1),i);
      end loop;
      return Sum(V);
   end;


Finally, you could pass the whole matrix (presumably this would actually be
done by reference), and simply identify the row/column to be acted upon:


   function Sum_Row (M:   in Matrix;
                     Row: in Positive) return Float is
      R: Float := 0.0;
   begin
      for i in M'Range(2) loop
         R := R + M(Row,i);
      end loop;
      return R;
   end;


It may be that the last solution, while ugly, is actually the fastest.

Does this help at all?

--
Best wishes,
Nick Roberts






  reply	other threads:[~2001-12-13 21:49 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-12-11 22:22 Multidimensional array vs. array of array Lutz Donnerhacke
2001-12-11 22:45 ` Mark Lundquist
2001-12-17 15:56   ` Lutz Donnerhacke
2001-12-11 23:03 ` Stephen Leake
2001-12-12  8:39   ` Lutz Donnerhacke
2001-12-13  0:26     ` James Rogers
2001-12-13  9:02       ` Lutz Donnerhacke
2001-12-13 21:49         ` Nick Roberts [this message]
2001-12-14  9:45           ` Lutz Donnerhacke
replies disabled

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