Rather than using global x
and y
, try something like so:
bool is_there_zero(const m256x256& m, int x = 0, int y = 0)
{
if (m[x][y]->getID() == 0)
return true;
if (++y == 256) {
y = 0;
if (++x == 256) {
return false;
}
}
return is_there_zero(m, x, y);
}
So we bump the y
value, and if that reaches the end of the row we reset it to zero and bump the x
coordinate, and finally terminate the recursion when both are equal to 256. The original matrix is untouched.
In other words, we've turned it into a poor man's double for loop, only with added function call overhead and the risk of running out of stack...
As such, I'm not at all sure why you'd want to do it this way rather than just iterating. The only advantage I can see is that, with some "creative" use of the ternary operator you could turn this into a one-line constexpr
function, which could potentially be evaluated at compile-time for a fixed m
, but that hardly seems worth the bother.
EDIT: Re-reading the question, I see you only wanted to test the leading diagonal, so it's a bit simpler:
bool is_there_zero(const m256x256& m, int x = 0)
{
if (x == 256)
return false;
if (m[x][x]->getID() == 0)
return true;
return is_there_zero(m, ++x);
}