I have a certain nesting of loops, something similar to this:
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
for (int k = i + j; ;k += i + j)
{
doing stuff here...
}
}
}
Returning all over my project. the only thing changing is what I do inside the loops.
Is there a way to put the whole loop thing somewhere else and just tell what needs to be done in the loop? I was thinking of using something like this:
public void Main()
{
CostumLoop(i =>
{
doing stuff here...
});
}
public void CostumLoop(Action<int> doStuff)
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
for (int k = i + j; ;k += i + j)
{
doStuff(k);
}
}
}
}
But then I can't use any breaks or continue etc (which I actually need)... Is there maybe a way of telling that the break in de lambda is going to executed inside a loop and that it shouldn't give an error?
Any ideas?
Loading
Sam HobbsPosted Mar 7, 2011, 9:36 PM
Yeah this value type and reference type stuff is confusing. You are probably correct that the boolean was not changed because of the way it was passed. You solve that by putting "ref" with the parameter.
I assume that you need break and continue only for the innermost loop. I seldom use either one so I might get confused about which is which. For me, I would usually make a function that I call to do the processing for each iteration and then the return is the equivalent of .... let's see; the equivalent of continue, correct? So if you use a delegate to specify a function to execute for each iteration, then the return is the equivalent of the continue.
So you need a boolean to indicate the equivalent of a break; that the current innermost loop is to stop. A for loop is like a while except the for loop has the intializer and the iteration as well as a condition. So you could add a boolean to the condition in the innermost for loop and that can be used to stop the current loop. I hope you understand.
Does that satisfy your requirements?
One suggestion. Instead of passing many parameters, you could define a class or structure that you use to pass some of the parameters. It might make things easier to work with.
VulpesPosted Mar 8, 2011, 12:47 PM
Sam HobbsPosted Mar 8, 2011, 12:24 PM
VulpesPosted Mar 8, 2011, 5:06 AM
Roy SPosted Mar 7, 2011, 7:27 PM
This is an actual example of one of those loops:
for (int i = -8; i < 9; i += 8)
{
for (int j = -1; j < 2; j++)
{
if (i != 0 || j != 0)
{
for (int k = position + i + j; k >= 0 && k < 64 && 2 * (k & 7) != 7 - 7 * j && !board[k].CanBeMovedBy(owner); k += i + j)
{
if (!(board[k] is Empty))
{
if (board[k] is Queen || board[k] is Rook && i * j == 0 || board[k] is Bishop && i * j != 0)
{
return true;
}
else
{
break;
}
}
}
}
}
}
theLizardPosted Mar 7, 2011, 7:18 PM
{
for (int j = 0; j < 10; j++)
{
for (int k = i + j; ;k += i + j)
{
doStuff(k);
}
}
}
What are you trying to do here?
for (int k = i + j; ;k += i + j)
{
doStuff(k);
}
K is always 0, what does doStuf(k) do to break out of the loop at this point
Roy SPosted Mar 7, 2011, 6:59 PM
Making my own delegate with boolean as return value worked but after testing, it seemed about 7 times slower than the original code...
so yes you're right it's too much of a mess and also too slow. I'll have to do it with the old way and maybe make a snippet to make it easier.
VulpesPosted Mar 7, 2011, 6:49 PM