Topic: Dynamic Nested Loops Algorithm
Author: yigal@tovna.co.il (Yigal Dayan)
Date: Wed, 30 Nov 1994 16:22:34 GMT Raw View
> Does anybody have a routine (or aware of an algorithm, described somewhere)
> for simulating nested loops when you don't know the number of levels in
> advance, but know it only at the run-time (and want to have it as a
> parameter)?
Here's one way of doing it:
get_next_combination - given a set of vectors, find all combinations containing
one element from each vector. normally this type of ennumeration is done with
nested loops, but since the number of vectors is variable, the loops can't be
hard-coded. instead, we create a running 'number' representing one combination,
and let the function increment this number until it overflows. use as follows:
bzero(combination, max_cols * sizeof(int));
do
{
(use current combination)
} while (get_next_combination(combination, col_size, max_cols));
get_next_combination(combination, col_size, max_cols)
register int *combination, *col_size, max_cols;
{
register int posit;
posit = 0; /* least significant position */
while (++combination posit == col_size posit ) /* exhausted column possibilities */
{
combination posit = 0; /* reset column to its first possibility */
if (++posit == max_cols) /* move to next column */
return(FALSE); /* end of list */
}
return(TRUE);
}
--
Yigal
Author: berger@sbcm10.sbcm.com (Mitchel Berger)
Date: Fri, 25 Nov 1994 19:05:44 GMT Raw View
> Does anybody have a routine (or aware of an algorithm, described somewhere)
> for simulating nested loops when you don't know the number of levels in
> advance, but know it only at the run-time (and want to have it as a
> parameter)?
I wondered about this back in '78, when I was first told that in
BASIC, loops must be nested and not crossed.
(I wanted to do something like:
10 FOR i = 1 to n
20 FOR a[i] = 1 to m
30 NEXT i
:
:
10 FOR i = 1 to n
20 NEXT a[i]
30 NEXT i
I figured this would get me n nested loops -- the equivalent for n
FORs followed by n NEXTs, no?)
The truth is I never needed it. I'm pretty sure the body of the loop
can always be run for the same values in the same order using a
constant flow description.
--
Micha Berger red---6-murder---kindness-Abraham-body---nefesh
berger@sbcm.com 212 224-4937 green-7-incest---Torah----Jacob---mind----ruach
aishdas@iia.org 201 916-0287 blue--8-idolatry-worship--Isaac---soul-neshamah
<a href=http://www.iia.org/~aishdas>AishDas Society's Home Page</a>
Author: zaykin@stat.ncsu.edu (Dmitri Zaykin)
Date: 18 Nov 1994 23:24:58 GMT Raw View
Does anybody have a routine (or aware of an algorithm, described somewhere)
for simulating nested loops when you don't know the number of levels in
advance, but know it only at the run-time (and want to have it as a parameter)?
To be more clear, what I need is a function which does
following:
void NestedLoops(int Depth, int *a, int *b, int *i
/* ...perhaps some other stuff...*/)
{
for(i[0] = a[0]; i[0] < b[0]; i[0]++) {
for(i[1] = a[1]; i[1] < b[1]; i[1]++) {
for(i[2] = a[2]; i[2] < b[2]; i[2]++) {
for(i[3] = a[3]; i[3] < b[3]; i[3]++) {
/* ... etc ... */
for(i[Depth] = a[Depth]; i[Depth] < b[Depth]; i[Depth]++) {
/**** innermost loop: do whatever is the purpose here ****/
}
/* ... close all brackets ... */
}
}
}
}
where Depth is a "nestness" variable, ( which should be read from the
file or determined
at the run time otherwise )
i[] - array of indices,
a[] - array of lower bounds
b[] - array of upper bounds
The idea of hard-wiring (say) 100 loops and then breaking
off from somewhere does not work and I think the reasonable
way might resemble something (recursive) like:
void NestedLoops(int CurrentLevel, int Depth, int *a, int *b, int *i
/* ...perhaps some other stuff...*/)
{
if(CurrentLevel == Depth) return;
/** some very clever code here ***/
NestedLoops(CurrentLevel++, Depth, a, b, i /* ......*/ );
}
If anybody could help, please send me a message to zaykin@stat.ncsu.edu
Dmitri Zaykin