From d43e233a426e4c15b2c1a9d3196029983b80c894 Mon Sep 17 00:00:00 2001 From: aumgn Date: Mon, 16 Apr 2012 08:47:43 +0200 Subject: [PATCH] Slightly improve FlatRegion iteration --- .../worldedit/regions/CylinderRegion.java | 5 ++ .../regions/FlatRegion3DIterator.java | 63 +++++++++++++++++++ .../worldedit/regions/Polygonal2DRegion.java | 2 +- 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/sk89q/worldedit/regions/FlatRegion3DIterator.java diff --git a/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java b/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java index f6da7d825..171873a8a 100644 --- a/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java @@ -368,6 +368,11 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { return false; } + @Override + public Iterator iterator() { + return new FlatRegion3DIterator(this); + } + @Override public Iterable asFlatRegion() { return new Iterable() { diff --git a/src/main/java/com/sk89q/worldedit/regions/FlatRegion3DIterator.java b/src/main/java/com/sk89q/worldedit/regions/FlatRegion3DIterator.java new file mode 100644 index 000000000..d365e248b --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/regions/FlatRegion3DIterator.java @@ -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 { + + private Iterator flatIterator; + private int minY; + private int maxY; + + private Vector2D next2D; + private int nextY; + + public FlatRegion3DIterator(FlatRegion region, Iterator 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(); + } +} diff --git a/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java b/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java index 4bec4cd97..ebe32b34d 100644 --- a/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java @@ -492,7 +492,7 @@ public class Polygonal2DRegion extends AbstractRegion implements FlatRegion { */ @Override public Iterator iterator() { - return new RegionIterator(this); + return new FlatRegion3DIterator(this); } @Override