Algorithm / pattern to process variable embedded loops in java without recursion -


i looking algorithm/pattern process undefined quantity of inner loops without recursion, in java.

i suppose inner loops same (or similar) process.

each loop produces/processes/stores datas, depending on outer loops. complexity exponential.

usage: combinatorics example. looking generic solution.

some examples (which dont comply):

embedded not variable: if number of inner loops known:

for (int i=0;...) { // process 1      (int j=0;...)         {     // process 2          (int k=0;...)             {             // process 3              }         } } 

multiple loops, not embedded

for (int which_loop = 1; ...) { // loop which_loop  // problem: loops not embedded, cant reuse datas ... } 

one approach make iterators array of iterators, , use count.

for simplicity, let assume variables go same number (n), , number of such variables num_dimensions. assumption can relaxed checking against array instead of value n.

pseudo code:

int[] arr = new int[num_dimensions]; int k = 0; while (true) {    if (arr[k] == n) {  // go next iterator     k++;     if (k > num_dimensions - 1) { // done!       break;     }   }      if (k == 0) {       process(arr); // process here. arr state of iterators.       arr[k]++;     } else {        // increase iterator , go first iterator.       (int = 0; < k; i++) arr[i] = 0;        arr[k]++;       k = 0;     }   } 

the idea similar counting binary represented number, count can go number n - not 1:

0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -