How to realize dynamic for-loops in source code
Visual Sudio solution you can dowload here
like this
for(int i = 0 ; i < 10 ; i++)
for(int j = 0 ; j < 4 ; j++)
for(int k = 0 ; k< 9 ; k++)
...........................
........................
Console.Write( i , j , k ....)
* This source code was highlighted with Source Code Highlighter.
for this task i create class ManyFor
it simple to use (see ManyForTest)
ManyFor class generate all combination of i , j , k , ....
Example:
int[] mm = new int[] { 3, 2, 4 };
ManyFor mf = new ManyFor(mm);
for (int s = 0; s < mf.Count(); s++)
{
ReadOnlyCollection<int> r = mf.Get(s);
int i = r[0], j = r[1], k = r[2];
Console.WriteLine(i + " " + j + " " + k);
}
* This source code was highlighted with Source Code Highlighter.
OUTPUT :
0 0 0
0 0 1
0 0 2
0 0 3
0 1 0
0 1 1
0 1 2
0 1 3
1 0 0
1 0 1
1 0 2
1 0 3
1 1 0
1 1 1
1 1 2
1 1 3
2 0 0
2 0 1
2 0 2
2 0 3
2 1 0
2 1 1
2 1 2
2 1 3
// CLASS
class ManyFor
{
readonly int[] id; // all zero : 0 0 0 0 0 0
readonly int[] max; // : 5 2 1 6 9 1
readonly int[] div;
int numberIteration = 1;
int N;
public ManyFor(int[] max)
{
N = max.Length;
this.max = new int[N];
id = new int[N];
div = new int[N];
for (int i = N - 1; i >= 0; i--)
{
if (max[i] < 0)
throw new ArgumentOutOfRangeException("max", " must be >= 0 ");
div[i] = numberIteration;
this.max[i] = max[i];
checked
{
numberIteration *= max[i];
}
}
if (N == 0) numberIteration = 0;
}
public int Count()
{
return numberIteration;
}
public ReadOnlyCollection<int> Get(int n)
{
if (n < 0 || n >= numberIteration)
throw new ArgumentOutOfRangeException("n", " n < 0 || n >= numberIteration n = " + n);
for (int i = 0; i < N; i++)
{
id[i] = n / div[i];
n -= id[i] * div[i];
}
return Array.AsReadOnly(id);
}
}
* This source code was highlighted with Source Code Highlighter.
//=========Class For Big Iterations=================================================
private string GetString(int[] max )
{
m = new ManyForBig(max);
StringBuilder s0 = new StringBuilder("");
for (ReadOnlyCollection<int> r = m.Next(); r != null; r = m.Next())
for (int j = 0; j < r.Count; j++)
s0.Append(r[j]);
return s0.ToString();
}
* This source code was highlighted with Source Code Highlighter.
//=======================================================================
class ManyForBig
{
readonly int[] id; // all zero : 0 0 0 0 0 0
readonly int[] max; // : 5 2 1 6 9 1
private bool getFirst;
private int N;
private int index; // 0 <= index < N
public ManyForBig(int[] max)
{
N = max.Length;
this.max = (new List<int>(max)).ToArray();
id = new int[N];
index = N - 1;
getFirst = true;
if (N == 0) index = -1;
foreach (int i in max)
{
if (i < 0) throw new ArgumentOutOfRangeException("Error ManyFor i< 0");
if (i == 0)
{
index = -1;
break;
}
}
}
public ReadOnlyCollection<int> Next()
{
if (index == -1) return null;
if (getFirst)
{
getFirst = false;
return Array.AsReadOnly(id);
}
else if (id[index] + 1 == max[index])
{
for (; index >= 0 && id[index] + 1 == max[index]; index--)
{ }
if (index == -1) return null; //end
id[index]++;
for (int i = index + 1; i < N; i++)
id[i] = 0;
index = N - 1;
}
else
{
id[index]++;
}
return Array.AsReadOnly(id);
}
}
* This source code was highlighted with Source Code Highlighter.
//=========================================================
tags:
Many number of for-loops , Quesiton about nesting for loops, A generator for any number of for loop ,