Skip to content

Commit

Permalink
Optimized EditSession.countBlocks.
Browse files Browse the repository at this point in the history
Also fixed the questionable semantics for that anonymous class' "contains" method.
  • Loading branch information
TomyLobo committed Jun 23, 2013
1 parent 7cdc0f5 commit a732aaa
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions src/main/java/com/sk89q/worldedit/EditSession.java
Expand Up @@ -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;
Expand Down Expand Up @@ -533,6 +534,18 @@ public int countBlock(Region region, Set<Integer> searchIDs) {
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.
*
Expand All @@ -543,22 +556,6 @@ public int countBlock(Region region, Set<Integer> searchIDs) {
public int countBlocks(Region region, Set<BaseBlock> searchBlocks) {
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) {
// Doing this for speed
Vector min = region.getMinimumPoint();
Expand All @@ -577,7 +574,7 @@ public boolean contains(Object o) {
Vector pt = new Vector(x, y, z);

BaseBlock compare = new BaseBlock(getBlockType(pt), getBlockData(pt));
if (newSet.contains(compare)) {
if (containsFuzzy(searchBlocks, compare)) {
++count;
}
}
Expand All @@ -586,7 +583,7 @@ public boolean contains(Object o) {
} else {
for (Vector pt : region) {
BaseBlock compare = new BaseBlock(getBlockType(pt), getBlockData(pt));
if (newSet.contains(compare)) {
if (containsFuzzy(searchBlocks, compare)) {
++count;
}
}
Expand Down

0 comments on commit a732aaa

Please sign in to comment.