Browse Prior Art Database

# Nested for Loop Simulation

IP.com Disclosure Number: IPCOM000111982D
Original Publication Date: 1994-Apr-01
Included in the Prior Art Database: 2005-Mar-26
Document File: 4 page(s) / 103K

IBM

## Related People

Dischner, B: AUTHOR

## Abstract

Disclosed is an algorithm for simulating n nested for loops in which n, the level of nesting, is defined at run time. The algorithm takes three parameters:

This text was extracted from an ASCII text file.
This is the abbreviated version, containing approximately 50% of the total text.

Nested for Loop Simulation

Disclosed is an algorithm for simulating n nested for loops in
which n, the level of nesting, is defined at run time.  The algorithm
takes three parameters:

o   the number of for loops to be simulated,

o   a pointer to an array of limits corresponding to the individual
for loops, and

o   a pointer to a array of functions that implement the bodies of
the respective for loops.

The algorithm allocates memory for n counters, one for each of the
loops.  A while loop is then used in conjunction with a couple of if
and a single for statement to effect the simulation.

Nested for loops provide an idiom that proves useful in
applications that process data over multiple dimensions.
Multidimensional matrix operations are an obvious example in which
the use of nested for loops is ubiquitous.  Typically, the number of
dimensions is fixed prior to compile time and the for loops are then
coded to the required nesting level.

Problems may be encountered in which the dimensionality is
variable, as occurs in the conversion of Octcodes in an Octree
between differing resolution levels.  (Octrees use Octcodes to
represent points in space by dividing and subsequently subdividing
the space into octants, with the resolution defined as the number of
subdivisions.)  In order for a conversion function to be versatile,
it must be able to convert from any resolution r to any other
resolution s, where s may be of higher or lower resolution.  By
allowing the nesting level to be specified as a parameter, the
disclosed for loop simulation algorithm achieves the required
flexibility.

The algorithm has been implemented in C++ and is presented
below for descriptive purposes.  Function do_for_loops is the
algorithm implementation.  The remaining code has been included to
show a complete environment that utilizes the for loop simulation
function.

Further explanations follow the code.

#include <iostream.h>

#include <math.h>

#define LOOPS         4

#define LOOP_COUNT_1  3

#define LOOP_COUNT_2  5

#define LOOP_COUNT_3  4

#define LOOP_COUNT_4  6
int  loop_one();
int  loop_two();
int  loop_three();
int  loop_four();
void  do_for_loops(int, int*, int(*[])());

// *****************************************************************
***

// Purpose: Main driver to test nested FOR loop simulation.
main()
{ int  loop_limits[LOOPS];
int  (*bodies[LOOPS])();

loop_limits[0] = LOOP_COUNT_1;
loop_limits[1] = LOOP_COUNT_2;
loop_limits[2] = LOOP_COUNT_3;
loop_limits[3] = LOOP_COUNT_4;

bodies[0] = loop_one;
bodies[1] = loop_two;
bodies[2] = loop_three;
bodies[3] = loop_four;

do_for_loops(LOOPS, loop_limits, bodies);
}

// **************************************************************
***

// Purpose: Execute the supplied functions as the bodies of the
specified

//          number of...