Salvador's suggestion is the most interesting, but I'm not sure it will
work... Recursive calls require you to pass a variable number of parameters
which can be a bit inefficient.
On the other hand, you can create a special, multi-index iterator object.
The object would be a chain-of-responsibility pattern, where only one member
of the chain operates, but with the twist that the chain will "double-dip".
At runtime, you create the structure based on the number of "loops" you want
to iterate over.
The challenge is going to be how to get the actual values indexed back up
the chain.
So, I can see three "chain methods" where a chain method is a method that
operates on the object or another object below it in the chain.
..Increment
..Reset
..Collect
where collect returns an array containing the values you will ultimately
pass to your function.
(I guess the Collect method is technically a decorator method, in that it
operates on each object in the chain, not just one object in the chain.)
public interface IRespIterator
{
public bool Increment();
public void Reset();
public void Collect(int arg, int[] list);
}
public class ResponsibleTerminator : IRespIterator
{
public bool Increment()
{ return false; }
public void Reset() { }
public void Collect(int[] myarray, int arg, int[] list)
{
f(list); // here is where you call your function with the variable
list of parameters
}
}
public class ResponsibleIterator : IRespIterator
{
private ResponsibleIterator childIterator ;
private int myLimit = 0;
private int myCurrent = 0;
public ResponsibleIterator(int limit, IRespIterator mychild)
{
childIterator = mychild;
myLimit = limit;
}
public bool Increment()
{
if (childIterator.Increment() == True)
return True;
if (myCurrent < myLimit)
{
myCurrent++;
childIterator.Reset();
return True;
}
else
return False;
}
public void Reset()
{
myCurrent = 0;
childIterator.Reset();
}
void Collect(int[] myarray, int arg, int[] list)
{
list[arg] = myarray[myCurrent];
arg++;
childIterator.Collect(myarray, arg, list);
}
}
Now, when you want to iterate over three loops, creating a list of three
elements,
you could do this:
//---------- in the calling class
IRespIterator newlist = new ResponsibleTerminator();
For (int i = 0; i < loopcount; i++)
{
newlist = new ResponsibleIterator(a.Length - i, newlist);
}
int[] paramlist = new int[loopcount];
Do while (newlist.Increment())
{
newlist.Collect(a, 0, paramlist);
}
All done.
Warning... air code. I did not compile this or debug it. I am known for
making goofy syntax errors due to the fact that I program in many languages,
so make sure you understand what I am trying to do.
Note: the code above is pretty much all of the code you'd need to do this.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik
Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
<xx****@abv.bg> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi,
I have an array and I want to make many "for" cycles as follows:
int[] a=new int[n];
...//init a
for (i1=0;i1<a.Length;i1++)
for (i2=0;i2<a.Length-1;i2++)
for (i3=0;i3<a.Length-2;i3++)
...
for (ik=0;ik<a.Length-k-1;ik++)
{
f(a[i1],a[i2]....) //some operation over
//selected elements
}
I want the count of "for" cycles to be set at run time as parameter.
How can I do this? I want this to be very fast.
Thanks.