Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can Ada iterate over Nd array?

633 views
Skip to first unread message

Okasu

unread,
Apr 26, 2012, 12:42:29 AM4/26/12
to
I can't iterate over Nd array in Ada, becuse of
>expression for dimension must be static
So following code won't work:

for X in A'Range loop
for Y in A'Range (X) loop
null;
end loop;
end loop;

Geez, guys, what if i have to work with 1000d array?
How can i iterate over it?

Jeffrey Carter

unread,
Apr 26, 2012, 1:36:24 AM4/26/12
to
I have no idea what you're trying to do.

Obviously you're expecting A to be square (or cubic, or hyper-cubic, or ...), to
have indices of an integer type, and to have lower bounds of 1. None of these
are necessarily true.

Multi-dimensional arrays need not be square, so A'range (X) might reference
dimensions of A that don't exist (if it were allowed).

Array indices can be of any discreet type, including enumeration types. I don't
know what A'range (Green) or A'range ('E') would mean.

Even if an array has integral indices, the lower bound need not be 1. Again,
A'range (X) might reference dimensions of A that don't exist.

I can't see what you'd have in place of "null;" for a 1000-dimension array. You
can reference the whole array (A) or index a specific component of A, which
requires as many indices as A has dimensions. You have 2 indices, X and Y; where
are you going to get the other 998 indices?

In general, you iterate over an N-dimensional by having N nested "for" loops, 1
for each dimension of the array:

type Three_D is array (1 .. 3, 7 .. 300, 'A' .. 'Z') of Integer;

A : Three_D;

for I in A'range (1) loop
for J in A'range (2) loop
for K in A'range (3) loop
-- Do something with A (I, J, K)
end loop;
end loop;
end loop;

--
Jeff Carter
"Ada has made you lazy and careless. You can write programs in C that
are just as safe by the simple application of super-human diligence."
E. Robert Tisdale
72

Okasu

unread,
Apr 26, 2012, 2:02:13 AM4/26/12
to
On 2012-04-26, Jeffrey Carter <spam.jrc...@spam.not.acm.org> wrote:
>
> type Three_D is array (1 .. 3, 7 .. 300, 'A' .. 'Z') of Integer;
>
> A : Three_D;
>
> for I in A'range (1) loop
> for J in A'range (2) loop
> for K in A'range (3) loop
> -- Do something with A (I, J, K)
> end loop;
> end loop;
> end loop;
>

It's a brain dead code.
So you trying to say that i have to write loops for 10/100/1000d arrays
by hand?

Okasu

unread,
Apr 26, 2012, 2:05:41 AM4/26/12
to
On 2012-04-26, Okasu <oka...@gmail.com> wrote:
> for X in A'Range loop
> for Y in A'Range (X) loop
> null;
> end loop;
> end loop;

This is just an abstraction to show how should work iteration over
multidimensional array, nothing else.

Dmitry A. Kazakov

unread,
Apr 26, 2012, 4:01:39 AM4/26/12
to
On Thu, 26 Apr 2012 06:02:13 +0000 (UTC), Okasu wrote:

> So you trying to say that i have to write loops for 10/100/1000d arrays
> by hand?

You should introduce an index type of your own (a Nth tuple) and use a flat
container, e.g. a map over that index.

Unfortunately Ada does not have 1st class indices, which was #10 in my wish
list for Ada 202X.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

Georg Bauhaus

unread,
Apr 26, 2012, 5:00:12 AM4/26/12
to
This will depend on your arrays.

With typical languages, typically, yes. Like in C, C++, Fortran, Pascal,
Lisp, ML, OCaml, Java, etc etc, arrays in Ada are basic. Array indexing
uses the same scheme as everyone else, except that Ada lets you
specify 'Range, 'First, and so on, no mishaps there. Even for an
array of dimension 1000, the scheme does not change in any of
these languages.

(I will be very interested in seeing an array of dimension 1000
that isn't sparse.)

APL, SETL, R, and other languages are different.

When there is some library made for arrays, it can allow notions
such as "apply a subprogram Proc (First, Last: Vector) to the
block of dimension N between First and Last" where
N = Length(First) = Length(Last). There are some such libraries
for Ada, even in its standard library.

Message has been deleted

gautier...@hotmail.com

unread,
Apr 26, 2012, 5:40:16 AM4/26/12
to
Le jeudi 26 avril 2012 06:42:29 UTC+2, Okasu a écrit :

> Geez, guys, what if i have to work with 1000d array?

Even by programming on all sorts of projects, there are good chances are you'll never need more than a few array dimensions in your entire active life.

We could make a poll here: which is the maximum number of array dimensions you ever used ?
My answer: probably 5 or 6.
And with very specific animals as indices: some enumerated types including Boolean, plus a few ranges. No reason not to write explicitly the loops.
_________________________
Gautier's Ada programming
http://gautiersblog.blogspot.com/search/label/Ada
NB: follow the above link for a valid e-mail address

ytomino

unread,
Apr 26, 2012, 6:57:29 AM4/26/12
to
declare
Liner_A : array(1..A'Size / A'Component_Size) of the element type of A;
for Liner_A'Address use A'Address;
begin
for I in Liner_A'Range loop
null;
end loop;
end;

Adam Beneschan

unread,
Apr 26, 2012, 11:48:10 AM4/26/12
to
On Wednesday, April 25, 2012 11:02:13 PM UTC-7, Okasu wrote:
> On 2012-04-26, Jeffrey Carter wrote:
> >
> > type Three_D is array (1 .. 3, 7 .. 300, 'A' .. 'Z') of Integer;
> >
> > A : Three_D;
> >
> > for I in A'range (1) loop
> > for J in A'range (2) loop
> > for K in A'range (3) loop
> > -- Do something with A (I, J, K)
> > end loop;
> > end loop;
> > end loop;
> >
>
> It's a brain dead code.
> So you trying to say that i have to write loops for 10/100/1000d arrays
> by hand?

I don't think I've *ever* seen an array with more than three dimensions ever used, in 35 years of programming. (Exception: I think I once created a 4-dimensional array in APL in an attempt to write a one-line function to do something complicated. But that's more of a game, not really programming.) If you really need a way to associate arbitrarily long tuples with elements, you probably should be using some other kind of structure than an array.

But I think Ada 2012 does support what you're looking for; see the example at the bottom of 5.5.2 (http://www.ada-auth.org/standards/12rm/html/RM-5-5-2.html).

-- Adam
Message has been deleted

Jeffrey Carter

unread,
Apr 26, 2012, 2:09:00 PM4/26/12
to
On 04/26/2012 02:15 AM, gautier.de...@gmail.com wrote:
>
> We could make a poll here: which is the maximum number of array dimensions you ever used ?
> My answer: probably 5 or 6.

I don't recall ever using more than 3.

I don't think arrays with more than about 30 dimensions are going to be possible
with current technology: given only 2 index values/dimension, that's 2 ** 30
components, or 1 GB if each component is a byte. Much more than that and you
won't be able to allocate the array. Even in the foreseeable future it doesn't
look as if a 100-dimension array will be possible. 1000 dimensions is right out!

--
Jeff Carter
"I'm a kike, a yid, a heebie, a hook nose! I'm Kosher,
Mum! I'm a Red Sea pedestrian, and proud of it!"
Monty Python's Life of Brian
77

Okasu

unread,
Apr 26, 2012, 4:54:16 PM4/26/12
to
Thanks, it's exactly what i'm looking for, i'm happy to know that i can
easily iterate over any kind of array in Ada.

BrianG

unread,
Apr 26, 2012, 8:28:58 PM4/26/12
to
On 04/26/2012 02:09 PM, Jeffrey Carter wrote:
> On 04/26/2012 02:15 AM, gautier.de...@gmail.com wrote:
>>
>> We could make a poll here: which is the maximum number of array
>> dimensions you ever used ?
>> My answer: probably 5 or 6.
>
> I don't recall ever using more than 3.
>
> I don't think arrays with more than about 30 dimensions are going to be
> possible with current technology: given only 2 index values/dimension,
> that's 2 ** 30 components, or 1 GB if each component is a byte. Much
> more than that and you won't be able to allocate the array. Even in the
> foreseeable future it doesn't look as if a 100-dimension array will be
> possible. 1000 dimensions is right out!
>
If what is desired is an "n-dimensional array" (i.e. n is a parameter of
some sort - generic or procedure parameter), what are you going to do
with it, even if you can iterate? How do you declare such a type? If
it's to be used as a library to work on "n-dimensional arrays", how do
you specify subroutine parameters? (Except as a raw memory address or
equivalent.)

--
---
BrianG
000
@[Google's email domain]
.com

Randy Brukardt

unread,
Apr 26, 2012, 8:36:10 PM4/26/12
to
"Dmitry A. Kazakov" <mai...@dmitry-kazakov.de> wrote in message
news:18y0zty0yw1m3.1dkfnp5etqdk0$.dlg@40tude.net...
> On Thu, 26 Apr 2012 06:02:13 +0000 (UTC), Okasu wrote:
>
>> So you trying to say that i have to write loops for 10/100/1000d arrays
>> by hand?
>
> You should introduce an index type of your own (a Nth tuple) and use a
> flat
> container, e.g. a map over that index.
>
> Unfortunately Ada does not have 1st class indices, which was #10 in my
> wish
> list for Ada 202X.

It does, however, have 2nd class indices (see 4.1.6), which work better than
first class indicies most of the time.

Also, you can iterate over the entire array without using any indicies in
Ada 2012:

for E of A loop
null;
end loop;

This also works for containers.

But this doesn't work in earlier versions of Ada.

Randy.


Randy Brukardt

unread,
Apr 26, 2012, 8:39:32 PM4/26/12
to
"Adam Beneschan" <ad...@irvine.com> wrote in message
news:393172.2032.1335455290715.JavaMail.geo-discussion-forums@yntt13...
...
>I don't think I've *ever* seen an array with more than three dimensions
>ever used, in 35 years of programming. (

I have (or had) a five dimension array in one of my programs. It was quite a
problem to avoid running out of space (the size multiplies in a hurry!)

I think some of our internal test programs use 6 dimension arrays, under the
assumption that no one will ever try to use more! But that was more of a
white-box test than any real attempt to do anything.

Randy.


Jerrid Kimball

unread,
Apr 26, 2012, 9:58:49 PM4/26/12
to
And in a recent GNAT wavefront, it seems to only work for
one-dimensional arrays. Otherwise, you get an error "too few subscripts
in array reference" which doesn't make a lot of sense. This is 7.1w
from January, but supposedly 7.0 has full 2012 support. I've noticed a
lot of problems in this wavefront regarding 2012 support so hopefully
they're not issues in 7.0 stable.

Nasser M. Abbasi

unread,
Apr 27, 2012, 12:19:38 AM4/27/12
to
On 04/26/2012 10:48 AM, Adam Beneschan wrote:
> On Wednesday, April 25, 2012 11:02:13 PM UTC-7, Okasu wrote:
>> On 2012-04-26, Jeffrey Carter wrote:
>> >
>> > type Three_D is array (1 .. 3, 7 .. 300, 'A' .. 'Z') of Integer;
>> >
>> > A : Three_D;
>> >
>> > for I in A'range (1) loop
>> > for J in A'range (2) loop
>> > for K in A'range (3) loop
>> > -- Do something with A (I, J, K)
>> > end loop;
>> > end loop;
>> > end loop;
>> >
>>
>> It's a brain dead code.
>> So you trying to say that i have to write loops for 10/100/1000d arrays
>> by hand?
>


> I don't think I've *ever* seen an array with more than three dimensions ever used, in 35 years of programming.

me neither. I guess OP is working in the unified field theory area as
one of the the string theories (M-theory) contains 11 dimensional
space-time continuum for it to be correct. But 1000 dimensions is
something I can not understand. May be a new theory is in the works?

>
> But I think Ada 2012 does support what you're looking for; see the
>example at the bottom of 5.5.2
(http://www.ada-auth.org/standards/12rm/html/RM-5-5-2.html).
>
> -- Adam

That is very interesting. I will definitely need to convert some Ada
code I wrote that uses 2D grids for solving a PDE using this new
construct to see better how it works. I hope GNAT supports this new
feature, I will have to find out.

--Nasser

Martin

unread,
Apr 27, 2012, 3:39:35 AM4/27/12
to
This works for me with GNAT PRO v7.0.1:

with Ada.Text_IO; use Ada.Text_IO;
procedure Multi_D_Arrays is
type A is array (1 .. 3) of Integer;
type B is array (1 .. 4) of A;
type C is array (1 .. 2) of B;
procedure Set (A_C : in out C; Value : in Integer) is
V : Integer := Value;
begin
for I of A_C loop
for J of I loop
for K of J loop
K := V;
V := V + 1;
end loop;
end loop;
end loop;
end Set;
procedure Display (A_C : C) is
begin
for I of A_C loop
for J of I loop
for K of J loop
Put_Line (Integer'Image (K));
end loop;
end loop;
end loop;
end Display;
My_C : C;
begin
Set (My_C, 0);
Display (My_C);
Set (My_C, 10_000);
Display (My_C);
end Multi_D_Arrays;

Gives:
D:\Ada\multi_d_arrays\lib\multi_d_arrays
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
[2012-04-27 08:38:39] process terminated successfully (elapsed time: 00.11s)

-- Martin

Stephen Leake

unread,
Apr 27, 2012, 7:09:29 AM4/27/12
to
Can you show an example of what you want in another language?

I've never heard of such a thing being possible.

--
-- Stephe

Shark8

unread,
Apr 27, 2012, 12:06:30 PM4/27/12
to
On Thursday, April 26, 2012 7:36:10 PM UTC-5, Randy Brukardt wrote:
> Also, you can iterate over the entire array without using any indicies in
> Ada 2012:
>
> for E of A loop
> null;
> end loop;
>
> This also works for containers.


Ok, that is really nice.

Bill Findlay

unread,
Apr 27, 2012, 12:23:32 PM4/27/12
to
On 26/04/2012 16:48, in article
393172.2032.1335455290715.JavaMail.geo-discussion-forums@yntt13, "Adam
Beneschan" <ad...@irvine.com> wrote:

> I don't think I've *ever* seen an array with more than three dimensions ever
> used, in 35 years of programming.

From a Pascal compiler, written 36 years ago:

TYPE
CHECKRANGE = (POSITIVE,NEGATIVE,POSORNEG) ;
VAR
CHECKROUTINE : ARRAY [CHECKRANGE, (* +VE,-VE,OR EITHER *)
BOOLEAN, (* OVERFLOW TEST NECESSARY *)
BOOLEAN, (* MAX TEST NECESSARY *)
BOOLEAN, (* MIN TEST NECESSARY *)
BOOLEAN (* SIGN TEST NECESSARY *)
] OF MONITORROUTINES;

I admit that it was noteworthy even then.

--
Bill Findlay
with blueyonder.co.uk;
use surname & forename;


Jeffrey Carter

unread,
Apr 27, 2012, 2:08:15 PM4/27/12
to
On 04/27/2012 12:39 AM, Martin wrote:
> type A is array (1 .. 3) of Integer;
> type B is array (1 .. 4) of A;
> type C is array (1 .. 2) of B;

These are all 1-D arrays.

--
Jeff Carter
"We'll make Rock Ridge think it's a chicken
that got caught in a tractor's nuts!"
Blazing Saddles
87

Robert A Duff

unread,
Apr 27, 2012, 9:30:22 AM4/27/12
to
"Randy Brukardt" <ra...@rrsoftware.com> writes:

> I have (or had) a five dimension array in one of my programs.

I'm curious what the purpose was.

>...It was quite a
> problem to avoid running out of space (the size multiplies in a hurry!)

Yeah. Which is why 1000 dimensions is ridiculous -- if the length
in each dimension is just 2, you have 2**1000 components.
I suppose you could have a 1x1x...x1 array. Or if just one
'Length is 0, the whole thing is empty. But those don't seem
terribly useful.

- Bob

Robert A Duff

unread,
Apr 27, 2012, 9:46:46 AM4/27/12
to
BrianG <m...@null.email> writes:

> If what is desired is an "n-dimensional array" (i.e. n is a parameter of
> some sort - generic or procedure parameter), what are you going to do
> with it, even if you can iterate? How do you declare such a type? If
> it's to be used as a library to work on "n-dimensional arrays", how do
> you specify subroutine parameters? (Except as a raw memory address or
> equivalent.)

APL answers these questions (also J). It's a rather different
language than Ada. ;-)

- Bob

Georg Bauhaus

unread,
Apr 28, 2012, 4:24:36 AM4/28/12
to
On 27.04.12 15:30, Robert A Duff wrote:
> "Randy Brukardt"<ra...@rrsoftware.com> writes:
>
>> I have (or had) a five dimension array in one of my programs.
>
> I'm curious what the purpose was.

Just a guess: when the iteration does not compute
something from the index values themselves, then
it is brain dead having to put them there.

björn lundin

unread,
Apr 28, 2012, 5:35:49 AM4/28/12
to
We use a 5 dimensional array for selecting locations
In our wms/wcs system. Warehouse control system, that is.
A location is described by a 5- coordinat system
Store
Rack
Stack
Level
Depth

This was designed around 1990 so today we might have used other
Techniques. Dbacces was slow then,
--
Björn Lundin
Message has been deleted

björn lundin

unread,
Apr 28, 2012, 7:19:15 PM4/28/12
to
Usual values are
Stores: 2-5
Racks: 20
Stacks: 100
Level: 15
Depth: usually 1, sometimes 2, in rare cases up to 12
So, not that many, a large project may have up two 100_000 locations
Physical pallet locations that is.
The values of course varies with projects.

Yes, it could be mapped somehow, but this

Loc : location_type renames stores(2,4,7,4,1)

notation makes it readable.
And loops for finding free locations are fast. They had to be in ram, since the location should be chosen
At a selection point on the conveyor system, without the pallet stopping. That meant
* conveyor send 'pallet at selection location'
* location selection
* send new destination to conveyor

This had to be fast, serial lines was used, and databases were not that fast.
--
Björn lundin

Martin

unread,
Apr 30, 2012, 3:20:10 AM4/30/12
to
True but a 3-d array can always be re-arranged into 3 * 1-d arrays.

Is there an underlying difference between the 2 styles? Can a 'genuine' multi-dimensionsal array be implemented more efficiently?
-- Martin

Robert A Duff

unread,
Apr 30, 2012, 9:21:58 AM4/30/12
to
Martin <mar...@thedowies.com> writes:

> True but a 3-d array can always be re-arranged into 3 * 1-d arrays.

That's true if the bounds are fixed, or all but the outermost one are
fixed. But a two-dimensional array can have "range <>" in both
dimensions, which is impossible (in Ada) for an array of array.

- Bob

gautier...@hotmail.com

unread,
Apr 30, 2012, 10:48:21 AM4/30/12
to
Sounds familiar (mutatis mutandis)... Here is:

type Aggregator is record
loc : Locator_String;
[ some other data ]
end record;

type Aggregator_array is array(Integer range <>) of Aggregator;

type Aggregator_stack(max: Natural) is record
top : Natural:= 0;
item: Aggregator_array(1..max);
end record;

type Monster is
array(Coverage_type,
Risk_type,
Struct_mod_type)
of
Aggregator_stack(max);

from a tool which is used to compact data - without loss of important information, of course :-).
Each cell, agg(cov,ris,smo).item(idx) is of type Aggregator...
The first 3 index types are short enumerated types.

G.

Martin

unread,
Apr 30, 2012, 11:42:08 AM4/30/12
to
On Monday, April 30, 2012 2:21:58 PM UTC+1, Robert A Duff wrote:
Ah, ok. I don't think I've run into that - my world is usually very bounded ;-)
Cheers
-- Martin

Randy Brukardt

unread,
Apr 30, 2012, 10:02:26 PM4/30/12
to
"Robert A Duff" <bob...@shell01.TheWorld.com> wrote in message
news:wccsjfp...@shell01.TheWorld.com...
> "Randy Brukardt" <ra...@rrsoftware.com> writes:
>
>> I have (or had) a five dimension array in one of my programs.
>
> I'm curious what the purpose was.

I think it held the results of a simulation where 5 parameters were varied
small amounts from the starting point. But I admit I don't remember what I
did with the results afterwards.

Randy.


0 new messages