Optimized EditSession.countBlocks.

Also fixed the questionable semantics for that anonymous class' "contains" method.
This commit is contained in:
TomyLobo 2013-06-23 22:09:50 +02:00
parent 28047894c4
commit eacd147694

View File

@ -19,6 +19,7 @@
package com.sk89q.worldedit; package com.sk89q.worldedit;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Deque; import java.util.Deque;
import java.util.HashMap; import java.util.HashMap;
@ -534,6 +535,18 @@ public class EditSession {
return countBlocks(region, passOn); return countBlocks(region, passOn);
} }
private static boolean containsFuzzy(Collection<BaseBlock> 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. * 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<BaseBlock> searchBlocks) { public int countBlocks(Region region, Set<BaseBlock> searchBlocks) {
int count = 0; int count = 0;
// allow -1 data in the searchBlocks to match any type
Set<BaseBlock> newSet = new HashSet<BaseBlock>() {
@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) { if (region instanceof CuboidRegion) {
// Doing this for speed // Doing this for speed
Vector min = region.getMinimumPoint(); Vector min = region.getMinimumPoint();
@ -578,7 +575,7 @@ public class EditSession {
Vector pt = new Vector(x, y, z); Vector pt = new Vector(x, y, z);
BaseBlock compare = new BaseBlock(getBlockType(pt), getBlockData(pt)); BaseBlock compare = new BaseBlock(getBlockType(pt), getBlockData(pt));
if (newSet.contains(compare)) { if (containsFuzzy(searchBlocks, compare)) {
++count; ++count;
} }
} }
@ -587,7 +584,7 @@ public class EditSession {
} else { } else {
for (Vector pt : region) { for (Vector pt : region) {
BaseBlock compare = new BaseBlock(getBlockType(pt), getBlockData(pt)); BaseBlock compare = new BaseBlock(getBlockType(pt), getBlockData(pt));
if (newSet.contains(compare)) { if (containsFuzzy(searchBlocks, compare)) {
++count; ++count;
} }
} }