Implement getBlock for chunk batching extent

Also improve speed of comparators, by using ::comparingX and bitwise
ops.
This commit is contained in:
Kenzie Togami
2019-06-23 01:03:18 -07:00
parent 625cbe5e3d
commit d27daefd3e
3 changed files with 108 additions and 31 deletions

View File

@ -19,7 +19,6 @@
package com.sk89q.worldedit.math;
import com.google.common.collect.ComparisonChain;
import com.sk89q.worldedit.math.transform.AffineTransform;
import java.util.Comparator;
@ -28,7 +27,7 @@ import java.util.Comparator;
* An immutable 2-dimensional vector.
*/
public final class BlockVector2 {
public static final BlockVector2 ZERO = new BlockVector2(0, 0);
public static final BlockVector2 UNIT_X = new BlockVector2(1, 0);
public static final BlockVector2 UNIT_Z = new BlockVector2(0, 1);
@ -48,12 +47,8 @@ public final class BlockVector2 {
* cdef
* </pre>
*/
public static final Comparator<BlockVector2> COMPARING_GRID_ARRANGEMENT = (a, b) -> {
return ComparisonChain.start()
.compare(a.getBlockZ(), b.getBlockZ())
.compare(a.getBlockX(), b.getBlockX())
.result();
};
public static final Comparator<BlockVector2> COMPARING_GRID_ARRANGEMENT =
Comparator.comparingInt(BlockVector2::getZ).thenComparingInt(BlockVector2::getX);
public static BlockVector2 at(double x, double z) {
return at((int) Math.floor(x), (int) Math.floor(z));
@ -303,6 +298,27 @@ public final class BlockVector2 {
return divide(n, n);
}
/**
* Shift all components right.
*
* @param x the value to shift x by
* @param z the value to shift z by
* @return a new vector
*/
public BlockVector2 shr(int x, int z) {
return at(this.x >> x, this.z >> z);
}
/**
* Shift all components right by {@code n}.
*
* @param n the value to shift by
* @return a new vector
*/
public BlockVector2 shr(int n) {
return shr(n, n);
}
/**
* Get the length of the vector.
*
@ -532,5 +548,4 @@ public final class BlockVector2 {
public String toString() {
return "(" + x + ", " + z + ")";
}
}

View File

@ -19,13 +19,12 @@
package com.sk89q.worldedit.math;
import static com.google.common.base.Preconditions.checkArgument;
import com.google.common.collect.ComparisonChain;
import com.sk89q.worldedit.math.transform.AffineTransform;
import java.util.Comparator;
import static com.google.common.base.Preconditions.checkArgument;
/**
* An immutable 3-dimensional vector.
*/
@ -64,18 +63,15 @@ public final class BlockVector3 {
// thread-safe initialization idiom
private static final class YzxOrderComparator {
private static final Comparator<BlockVector3> YZX_ORDER = (a, b) -> {
return ComparisonChain.start()
.compare(a.y, b.y)
.compare(a.z, b.z)
.compare(a.x, b.x)
.result();
};
private static final Comparator<BlockVector3> YZX_ORDER =
Comparator.comparingInt(BlockVector3::getY)
.thenComparingInt(BlockVector3::getZ)
.thenComparingInt(BlockVector3::getX);
}
/**
* Returns a comparator that sorts vectors first by Y, then Z, then X.
*
*
* <p>
* Useful for sorting by chunk block storage order.
*/
@ -348,6 +344,28 @@ public final class BlockVector3 {
return divide(n, n, n);
}
/**
* Shift all components right.
*
* @param x the value to shift x by
* @param y the value to shift y by
* @param z the value to shift z by
* @return a new vector
*/
public BlockVector3 shr(int x, int y, int z) {
return at(this.x >> x, this.y >> y, this.z >> z);
}
/**
* Shift all components right by {@code n}.
*
* @param n the value to shift by
* @return a new vector
*/
public BlockVector3 shr(int n) {
return shr(n, n, n);
}
/**
* Get the length of the vector.
*