From eacd147694f9cc7371df662f94a5ff99e523a366 Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Sun, 23 Jun 2013 22:09:50 +0200 Subject: [PATCH] Optimized EditSession.countBlocks. Also fixed the questionable semantics for that anonymous class' "contains" method. --- .../java/com/sk89q/worldedit/EditSession.java | 33 +++++++++---------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/EditSession.java b/src/main/java/com/sk89q/worldedit/EditSession.java index a81818844..016aa9c1f 100644 --- a/src/main/java/com/sk89q/worldedit/EditSession.java +++ b/src/main/java/com/sk89q/worldedit/EditSession.java @@ -19,6 +19,7 @@ package com.sk89q.worldedit; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.Deque; import java.util.HashMap; @@ -534,6 +535,18 @@ public class EditSession { return countBlocks(region, passOn); } + private static boolean containsFuzzy(Collection collection, Object o) { + // allow -1 data in the searchBlocks to match any type + for (BaseBlock b : collection) { + if (o instanceof BaseBlock) { + if (b.equalsFuzzy((BaseBlock) o)) { + return true; + } + } + } + return false; + } + /** * Count the number of blocks of a list of types in a region. * @@ -544,22 +557,6 @@ public class EditSession { public int countBlocks(Region region, Set searchBlocks) { int count = 0; - // allow -1 data in the searchBlocks to match any type - Set newSet = new HashSet() { - @Override - public boolean contains(Object o) { - for (BaseBlock b : this.toArray(new BaseBlock[this.size()])) { - if (o instanceof BaseBlock) { - if (b.equalsFuzzy((BaseBlock) o)) { - return true; - } - } - } - return false; - } - }; - newSet.addAll(searchBlocks); - if (region instanceof CuboidRegion) { // Doing this for speed Vector min = region.getMinimumPoint(); @@ -578,7 +575,7 @@ public class EditSession { Vector pt = new Vector(x, y, z); BaseBlock compare = new BaseBlock(getBlockType(pt), getBlockData(pt)); - if (newSet.contains(compare)) { + if (containsFuzzy(searchBlocks, compare)) { ++count; } } @@ -587,7 +584,7 @@ public class EditSession { } else { for (Vector pt : region) { BaseBlock compare = new BaseBlock(getBlockType(pt), getBlockData(pt)); - if (newSet.contains(compare)) { + if (containsFuzzy(searchBlocks, compare)) { ++count; } }