#include "lu_internal.h"
static lu_int dfs_end(
lu_int i, const lu_int *begin, const lu_int *end, const lu_int *index,
lu_int top, lu_int *xi, lu_int *pstack, lu_int *marked, const lu_int M);
static lu_int dfs(
lu_int i, const lu_int *begin, const lu_int *index,
lu_int top, lu_int *xi, lu_int *pstack, lu_int *marked, const lu_int M);
lu_int lu_dfs(
lu_int i, const lu_int *begin, const lu_int *end, const lu_int *index,
lu_int top, lu_int *xi, lu_int *pstack, lu_int *marked, const lu_int M)
{
if (marked[i] == M)
return top;
return end ?
dfs_end(i, begin, end, index, top, xi, pstack, marked, M) :
dfs(i, begin, index, top, xi, pstack, marked, M);
}
static lu_int dfs_end(
lu_int i, const lu_int *begin, const lu_int *end, const lu_int *index,
lu_int top, lu_int *xi, lu_int *pstack, lu_int *marked, const lu_int M)
{
lu_int inext, done, p, head = 0;
assert(marked[i] != M);
xi[0] = i;
while (head >= 0)
{
i = xi[head];
if (marked[i] != M)
{
marked[i] = M;
pstack[head] = begin[i];
}
done = 1;
for (p = pstack[head]; p < end[i]; p++)
{
inext = index[p];
if (marked[inext] == M)
continue;
pstack[head] = p+1;
xi[++head] = inext;
done = 0;
break;
}
if (done)
{
head--;
xi[--top] = i;
}
}
return top;
}
static lu_int dfs(
lu_int i, const lu_int *begin, const lu_int *index,
lu_int top, lu_int *xi, lu_int *pstack, lu_int *marked, const lu_int M)
{
lu_int inext, done, p, head = 0;
assert(marked[i] != M);
xi[0] = i;
while (head >= 0)
{
i = xi[head];
if (marked[i] != M)
{
marked[i] = M;
pstack[head] = begin[i];
}
done = 1;
for (p = pstack[head]; (inext = index[p]) >= 0; p++)
{
if (marked[inext] == M)
continue;
pstack[head] = p+1;
xi[++head] = inext;
done = 0;
break;
}
if (done)
{
head--;
xi[--top] = i;
}
}
return top;
}