>>Take the following loop nest:
>>
>>DO i1=-1,1
>>DO i2=-1,1
>>...
>>DO iN=-1,1
>>...
>>END DO
>>...
>>END DO
>>
>>where N is a small number, say 2, 3, 4, 5 or so. I want to find a way to
>>write the above code *without* nested DO loops, for a generic N, so that
>>I do not need to write special cases for each N.
Here's a straightforward implementation of the concept.
I(N) is the set of do variables and II loops over I(N) for updating.
PROGRAM ARBITRARY_DIMENSIONS_PROG
INTEGER, PARAMETER :: N = 5
INTEGER, PARAMETER :: LB(N) = (/ -1, -1, -1, -1, -1 /)
INTEGER, PARAMETER :: UB(N) = (/ 1, 1, 1, 1, 1 /)
INTEGER :: II, I(N)
I = LB
DO
WRITE(*,*) I
DO II = 1, N
I(II) = I(II)+1
IF (I(II) <= UB(II)) EXIT
I(II) = LB(II)
END DO
IF (II > N) EXIT
END DO
END PROGRAM ARBITRARY_DIMENSIONS_PROG
--
Yasuki Arasaki
[log in to unmask]
|