Multidimensional array vs. array of array

Quote:

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

Quote:

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