Refactoring a while loop without if .. else

?

-

Is it possible to refactor the while loop into one method? The only way
I can think of is to use if ... else when deciding the setSize but
that would be inefficient.


if (....) {
while (thread == Thread.currentThread()) {
setSize(x, getHeight());

x--;

if (x == SOME_VALUE) {
thread = null;
}
}
} else {
while (thread == Thread.currentThread()) {
setSize(getWidth(), y);

y--;

if (y == SOME_VALUE) {
thread = null;
}
}
}
 
W

Wibble

I'll assume setSize has no side affects other than changing size...

if (thread==Thread.currentThread() {
if (...) setSize((x=SOME_VALUE)+1, getHeight());
else setSize(getWidth(), (y=SOME_VALUE)+1);
thread = null;
}
 
W

Wibble

Or maybe setSize sets y & x equal to SOME_VALUE. Then the following
should do:

while(true);
 
M

Michael Rauscher

Hi,
Is it possible to refactor the while loop into one method? The only way
I can think of is to use if ... else when deciding the setSize but
that would be inefficient.

I'll give an untested example (should work for non-negative x and y
values) but have to mention, that I'm in doubt that this will be more
efficient. It sets the size and decrements the corresponding x/y-values
with respect to a given direction.

So, let's assume the follow constants and instance variables;

public static final int HORIZONTAL = 0xffffffff;
public static final int VERTICAL = 0;
private int direction = HORIZONTAL; // or VERTICAL

Then one can rewrite the loops as follows:

int xMask = direction;
int yMask = ~xMask;

int newX = x & xMask + getWidth() & yMask;
int newY = y & yMask + getHeight() & xMask;

int stopX = SOME_VALUE & xMask - 1 & yMask;
int stopY = SOME_VALUE & yMask - 1 & xMask;

while ( thread == Thread.currentThread() ) {
setSize( newX, newY );

newX -= 1 & xMask;
newY -= 1 & yMask;

if ( newX == stopX || newY == stopY )
thread = null;
}

Bye
Michael
 
B

Boudewijn Dijkstra

- said:
Is it possible to refactor the while loop into one method? The only way I
can think of is to use if ... else when deciding the setSize but that would
be inefficient.

Unroll it:

switch (x) {
case 20: setSize(20, getHeight());
case 19: setSize(19, getHeight());
case 18: setSize(18, getHeight());
case 17: setSize(17, getHeight());
case 16: setSize(16, getHeight());
case 15: setSize(15, getHeight());
case 14: setSize(14, getHeight());
case 13: setSize(13, getHeight());
case 12: setSize(12, getHeight());
case 11: setSize(11, getHeight());
case 10: setSize(10, getHeight());
case 9: setSize( 9, getHeight());
case 8: setSize( 8, getHeight());
case 7: setSize( 7, getHeight());
case 6: setSize( 6, getHeight());
case 5: setSize( 5, getHeight());
case 4: setSize( 4, getHeight());
case 3: setSize( 3, getHeight());
case 2: setSize( 2, getHeight());
case 1: setSize( 1, getHeight());
}

etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top