Use CoordStack for INST Flooding (#676)
This commit is contained in:
parent
c08b333909
commit
da5f8068c0
@ -42,11 +42,11 @@ public:
|
|||||||
stack(NULL),
|
stack(NULL),
|
||||||
stack_size(0)
|
stack_size(0)
|
||||||
{
|
{
|
||||||
stack = (unsigned short(*)[2])(malloc(sizeof(unsigned short)*2*stack_limit));
|
stack = new unsigned short[stack_limit][2];
|
||||||
}
|
}
|
||||||
~CoordStack()
|
~CoordStack()
|
||||||
{
|
{
|
||||||
free(stack);
|
delete stack;
|
||||||
}
|
}
|
||||||
void push(int x, int y)
|
void push(int x, int y)
|
||||||
{
|
{
|
||||||
|
@ -723,7 +723,6 @@ int Simulation::FloodINST(int x, int y, int fullc, int cm)
|
|||||||
int c = TYP(fullc);
|
int c = TYP(fullc);
|
||||||
int x1, x2;
|
int x1, x2;
|
||||||
int coord_stack_limit = XRES*YRES;
|
int coord_stack_limit = XRES*YRES;
|
||||||
unsigned short (*coord_stack)[2];
|
|
||||||
int coord_stack_size = 0;
|
int coord_stack_size = 0;
|
||||||
int created_something = 0;
|
int created_something = 0;
|
||||||
|
|
||||||
@ -745,16 +744,15 @@ int Simulation::FloodINST(int x, int y, int fullc, int cm)
|
|||||||
if (TYP(pmap[y][x])!=cm || parts[ID(pmap[y][x])].life!=0)
|
if (TYP(pmap[y][x])!=cm || parts[ID(pmap[y][x])].life!=0)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
coord_stack = (short unsigned int (*)[2])malloc(sizeof(unsigned short)*2*coord_stack_limit);
|
CoordStack cs;
|
||||||
coord_stack[coord_stack_size][0] = x;
|
|
||||||
coord_stack[coord_stack_size][1] = y;
|
|
||||||
coord_stack_size++;
|
|
||||||
|
|
||||||
|
cs.push(x, y);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
coord_stack_size--;
|
cs.pop(x, y);
|
||||||
x = coord_stack[coord_stack_size][0];
|
|
||||||
y = coord_stack[coord_stack_size][1];
|
|
||||||
x1 = x2 = x;
|
x1 = x2 = x;
|
||||||
// go left as far as possible
|
// go left as far as possible
|
||||||
while (x1>=CELL)
|
while (x1>=CELL)
|
||||||
@ -790,14 +788,7 @@ int Simulation::FloodINST(int x, int y, int fullc, int cm)
|
|||||||
// travelling vertically up, skipping a horizontal line
|
// travelling vertically up, skipping a horizontal line
|
||||||
if (TYP(pmap[y-2][x1])==cm && !parts[ID(pmap[y-2][x1])].life)
|
if (TYP(pmap[y-2][x1])==cm && !parts[ID(pmap[y-2][x1])].life)
|
||||||
{
|
{
|
||||||
coord_stack[coord_stack_size][0] = x1;
|
cs.push(x1, y-2);
|
||||||
coord_stack[coord_stack_size][1] = y-2;
|
|
||||||
coord_stack_size++;
|
|
||||||
if (coord_stack_size>=coord_stack_limit)
|
|
||||||
{
|
|
||||||
free(coord_stack);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (y>=CELL+1)
|
else if (y>=CELL+1)
|
||||||
@ -809,14 +800,7 @@ int Simulation::FloodINST(int x, int y, int fullc, int cm)
|
|||||||
if (x==x1 || x==x2 || y>=YRES-CELL-1 || !PMAP_CMP_CONDUCTIVE(pmap[y+1][x], cm) || PMAP_CMP_CONDUCTIVE(pmap[y+1][x+1], cm) || PMAP_CMP_CONDUCTIVE(pmap[y+1][x-1], cm))
|
if (x==x1 || x==x2 || y>=YRES-CELL-1 || !PMAP_CMP_CONDUCTIVE(pmap[y+1][x], cm) || PMAP_CMP_CONDUCTIVE(pmap[y+1][x+1], cm) || PMAP_CMP_CONDUCTIVE(pmap[y+1][x-1], cm))
|
||||||
{
|
{
|
||||||
// if at the end of a horizontal section, or if it's a T junction or not a 1px wire crossing
|
// if at the end of a horizontal section, or if it's a T junction or not a 1px wire crossing
|
||||||
coord_stack[coord_stack_size][0] = x;
|
cs.push(x, y-1);
|
||||||
coord_stack[coord_stack_size][1] = y-1;
|
|
||||||
coord_stack_size++;
|
|
||||||
if (coord_stack_size>=coord_stack_limit)
|
|
||||||
{
|
|
||||||
free(coord_stack);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -829,14 +813,7 @@ int Simulation::FloodINST(int x, int y, int fullc, int cm)
|
|||||||
// travelling vertically down, skipping a horizontal line
|
// travelling vertically down, skipping a horizontal line
|
||||||
if (TYP(pmap[y+2][x1])==cm && !parts[ID(pmap[y+2][x1])].life)
|
if (TYP(pmap[y+2][x1])==cm && !parts[ID(pmap[y+2][x1])].life)
|
||||||
{
|
{
|
||||||
coord_stack[coord_stack_size][0] = x1;
|
cs.push(x1, y+2);
|
||||||
coord_stack[coord_stack_size][1] = y+2;
|
|
||||||
coord_stack_size++;
|
|
||||||
if (coord_stack_size>=coord_stack_limit)
|
|
||||||
{
|
|
||||||
free(coord_stack);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (y<YRES-CELL-1)
|
else if (y<YRES-CELL-1)
|
||||||
@ -848,21 +825,20 @@ int Simulation::FloodINST(int x, int y, int fullc, int cm)
|
|||||||
if (x==x1 || x==x2 || y<0 || !PMAP_CMP_CONDUCTIVE(pmap[y-1][x], cm) || PMAP_CMP_CONDUCTIVE(pmap[y-1][x+1], cm) || PMAP_CMP_CONDUCTIVE(pmap[y-1][x-1], cm))
|
if (x==x1 || x==x2 || y<0 || !PMAP_CMP_CONDUCTIVE(pmap[y-1][x], cm) || PMAP_CMP_CONDUCTIVE(pmap[y-1][x+1], cm) || PMAP_CMP_CONDUCTIVE(pmap[y-1][x-1], cm))
|
||||||
{
|
{
|
||||||
// if at the end of a horizontal section, or if it's a T junction or not a 1px wire crossing
|
// if at the end of a horizontal section, or if it's a T junction or not a 1px wire crossing
|
||||||
coord_stack[coord_stack_size][0] = x;
|
cs.push(x, y+1);
|
||||||
coord_stack[coord_stack_size][1] = y+1;
|
|
||||||
coord_stack_size++;
|
|
||||||
if (coord_stack_size>=coord_stack_limit)
|
|
||||||
{
|
|
||||||
free(coord_stack);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (coord_stack_size>0);
|
} while (cs.getSize()>0);
|
||||||
free(coord_stack);
|
}
|
||||||
|
catch (std::exception& e)
|
||||||
|
{
|
||||||
|
std::cerr << e.what() << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
return created_something;
|
return created_something;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user