From 4488bb80913ec0fa886aafbb07e65d218690ab21 Mon Sep 17 00:00:00 2001 From: sk89q Date: Sat, 26 Feb 2011 20:30:53 -0800 Subject: [PATCH] Added iterator implementation to CuboidRegion, courtesy of hash. --- .../sk89q/worldedit/regions/CuboidRegion.java | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/com/sk89q/worldedit/regions/CuboidRegion.java b/src/com/sk89q/worldedit/regions/CuboidRegion.java index 6445dfbaa..2823ed7df 100644 --- a/src/com/sk89q/worldedit/regions/CuboidRegion.java +++ b/src/com/sk89q/worldedit/regions/CuboidRegion.java @@ -313,6 +313,38 @@ public class CuboidRegion implements Region { * @return iterator of points inside the region */ public Iterator iterator() { - throw new UnsupportedOperationException("Not implemented"); + return new Iterator() { + private Vector min = getMinimumPoint(); + private Vector max = getMaximumPoint(); + private int nextX = min.getBlockX(); + private int nextY = min.getBlockY(); + private int nextZ = min.getBlockZ(); + + public boolean hasNext() { + return (nextX != Integer.MIN_VALUE); + } + + public BlockVector next() { + if (!hasNext()) throw new java.util.NoSuchElementException(); + BlockVector answer = new BlockVector(nextX, nextY, nextZ); + nextX++; + if (nextX > max.getBlockX()) { + nextX = min.getBlockX(); + nextY++; + if (nextY > max.getBlockY()) { + nextY = min.getBlockY(); + nextZ++; + if (nextZ > max.getBlockZ()) { + nextX = Integer.MIN_VALUE; + } + } + } + return answer; + } + + public void remove() { + throw new UnsupportedOperationException(); + } + }; } }