Slightly improve FlatRegion iteration

This commit is contained in:
aumgn 2012-04-16 08:47:43 +02:00
parent a7b49ff793
commit d43e233a42
3 changed files with 69 additions and 1 deletions

View File

@ -368,6 +368,11 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion {
return false; return false;
} }
@Override
public Iterator<BlockVector> iterator() {
return new FlatRegion3DIterator(this);
}
@Override @Override
public Iterable<Vector2D> asFlatRegion() { public Iterable<Vector2D> asFlatRegion() {
return new Iterable<Vector2D>() { return new Iterable<Vector2D>() {

View File

@ -0,0 +1,63 @@
package com.sk89q.worldedit.regions;
import java.util.Iterator;
import java.util.NoSuchElementException;
import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.Vector2D;
public class FlatRegion3DIterator implements Iterator<BlockVector> {
private Iterator<Vector2D> flatIterator;
private int minY;
private int maxY;
private Vector2D next2D;
private int nextY;
public FlatRegion3DIterator(FlatRegion region, Iterator<Vector2D> flatIterator) {
this.flatIterator = flatIterator;
this.minY = region.getMinimumY();
this.maxY = region.getMaximumY();
if (flatIterator.hasNext()) {
this.next2D = flatIterator.next();
} else {
this.next2D = null;
}
this.nextY = minY;
}
public FlatRegion3DIterator(FlatRegion region) {
this(region, region.asFlatRegion().iterator());
}
@Override
public boolean hasNext() {
return next2D != null;
}
@Override
public BlockVector next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
BlockVector current = new BlockVector(next2D.getBlockX(), nextY, next2D.getBlockZ());
if (nextY < maxY) {
nextY++;
} else if (flatIterator.hasNext()) {
next2D = flatIterator.next();
nextY = minY;
} else {
next2D = null;
}
return current;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}

View File

@ -492,7 +492,7 @@ public class Polygonal2DRegion extends AbstractRegion implements FlatRegion {
*/ */
@Override @Override
public Iterator<BlockVector> iterator() { public Iterator<BlockVector> iterator() {
return new RegionIterator(this); return new FlatRegion3DIterator(this);
} }
@Override @Override