mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-26 11:07:53 +01:00
Remove some AdaptivePalette fields
This commit is contained in:
parent
58b103afd6
commit
8839503767
@ -22,7 +22,7 @@ public class PaletteGetBenchmark {
|
|||||||
|
|
||||||
@Setup
|
@Setup
|
||||||
public void setup() {
|
public void setup() {
|
||||||
palette = Palette.newPalette(dimension, 15, 4, 1);
|
palette = Palette.newPalette(dimension, 15, 4);
|
||||||
AtomicInteger value = new AtomicInteger();
|
AtomicInteger value = new AtomicInteger();
|
||||||
palette.setAll((x, y, z) -> value.getAndIncrement());
|
palette.setAll((x, y, z) -> value.getAndIncrement());
|
||||||
}
|
}
|
||||||
|
@ -13,20 +13,16 @@ import java.util.function.IntUnaryOperator;
|
|||||||
*/
|
*/
|
||||||
final class AdaptivePalette implements Palette {
|
final class AdaptivePalette implements Palette {
|
||||||
final int dimension;
|
final int dimension;
|
||||||
final int dimensionBitCount;
|
|
||||||
final int maxBitsPerEntry;
|
final int maxBitsPerEntry;
|
||||||
final int defaultBitsPerEntry;
|
final int defaultBitsPerEntry;
|
||||||
final int bitsIncrement;
|
|
||||||
|
|
||||||
SpecializedPalette palette;
|
SpecializedPalette palette;
|
||||||
|
|
||||||
AdaptivePalette(int dimension, int maxBitsPerEntry, int bitsPerEntry, int bitsIncrement) {
|
AdaptivePalette(int dimension, int maxBitsPerEntry, int bitsPerEntry) {
|
||||||
this.dimensionBitCount = validateDimension(dimension);
|
validateDimension(dimension);
|
||||||
|
|
||||||
this.dimension = dimension;
|
this.dimension = dimension;
|
||||||
this.maxBitsPerEntry = maxBitsPerEntry;
|
this.maxBitsPerEntry = maxBitsPerEntry;
|
||||||
this.defaultBitsPerEntry = bitsPerEntry;
|
this.defaultBitsPerEntry = bitsPerEntry;
|
||||||
this.bitsIncrement = bitsIncrement;
|
|
||||||
|
|
||||||
this.palette = new FilledPalette(dimension, 0);
|
this.palette = new FilledPalette(dimension, 0);
|
||||||
}
|
}
|
||||||
@ -132,8 +128,9 @@ final class AdaptivePalette implements Palette {
|
|||||||
flexiblePalette.getAll((x, y, z, value) -> entries.add(value));
|
flexiblePalette.getAll((x, y, z, value) -> entries.add(value));
|
||||||
if (entries.size() == 1) {
|
if (entries.size() == 1) {
|
||||||
return new FilledPalette(dimension, entries.iterator().nextInt());
|
return new FilledPalette(dimension, entries.iterator().nextInt());
|
||||||
} else {
|
} else if (flexiblePalette.bitsPerEntry() > defaultBitsPerEntry) {
|
||||||
final int bitsPerEntry = Math.max(4, MathUtils.bitsToRepresent(entries.size() - 1));
|
final int bitsPerEntry = MathUtils.bitsToRepresent(entries.size() - 1);
|
||||||
|
assert bitsPerEntry < flexiblePalette.bitsPerEntry();
|
||||||
flexiblePalette.resize(bitsPerEntry);
|
flexiblePalette.resize(bitsPerEntry);
|
||||||
return flexiblePalette;
|
return flexiblePalette;
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,7 @@ final class FlexiblePalette implements SpecializedPalette, Cloneable {
|
|||||||
|
|
||||||
// Specific to this palette type
|
// Specific to this palette type
|
||||||
private final AdaptivePalette adaptivePalette;
|
private final AdaptivePalette adaptivePalette;
|
||||||
|
private final int dimensionBitCount;
|
||||||
private int bitsPerEntry;
|
private int bitsPerEntry;
|
||||||
|
|
||||||
private boolean hasPalette;
|
private boolean hasPalette;
|
||||||
@ -44,6 +45,7 @@ final class FlexiblePalette implements SpecializedPalette, Cloneable {
|
|||||||
|
|
||||||
FlexiblePalette(AdaptivePalette adaptivePalette, int bitsPerEntry) {
|
FlexiblePalette(AdaptivePalette adaptivePalette, int bitsPerEntry) {
|
||||||
this.adaptivePalette = adaptivePalette;
|
this.adaptivePalette = adaptivePalette;
|
||||||
|
this.dimensionBitCount = (int) (Math.log(adaptivePalette.dimension()) / Math.log(2));
|
||||||
|
|
||||||
this.bitsPerEntry = bitsPerEntry;
|
this.bitsPerEntry = bitsPerEntry;
|
||||||
this.hasPalette = bitsPerEntry <= maxBitsPerEntry();
|
this.hasPalette = bitsPerEntry <= maxBitsPerEntry();
|
||||||
@ -257,7 +259,7 @@ final class FlexiblePalette implements SpecializedPalette, Cloneable {
|
|||||||
final int size = maxSize();
|
final int size = maxSize();
|
||||||
final int dimensionMinus = dimension - 1;
|
final int dimensionMinus = dimension - 1;
|
||||||
final int[] ids = hasPalette ? paletteToValueList.elements() : null;
|
final int[] ids = hasPalette ? paletteToValueList.elements() : null;
|
||||||
final int dimensionBitCount = adaptivePalette.dimensionBitCount;
|
final int dimensionBitCount = this.dimensionBitCount;
|
||||||
final int shiftedDimensionBitCount = dimensionBitCount << 1;
|
final int shiftedDimensionBitCount = dimensionBitCount << 1;
|
||||||
for (int i = 0; i < values.length; i++) {
|
for (int i = 0; i < values.length; i++) {
|
||||||
final long value = values[i];
|
final long value = values[i];
|
||||||
@ -329,7 +331,7 @@ final class FlexiblePalette implements SpecializedPalette, Cloneable {
|
|||||||
final int lastPaletteIndex = this.lastPaletteIndex;
|
final int lastPaletteIndex = this.lastPaletteIndex;
|
||||||
if (lastPaletteIndex >= maxPaletteSize(bitsPerEntry)) {
|
if (lastPaletteIndex >= maxPaletteSize(bitsPerEntry)) {
|
||||||
// Palette is full, must resize
|
// Palette is full, must resize
|
||||||
resize(bitsPerEntry + adaptivePalette.bitsIncrement);
|
resize(bitsPerEntry + 1);
|
||||||
return getPaletteIndex(value);
|
return getPaletteIndex(value);
|
||||||
}
|
}
|
||||||
final int lookup = valueToPaletteMap.putIfAbsent(value, lastPaletteIndex);
|
final int lookup = valueToPaletteMap.putIfAbsent(value, lastPaletteIndex);
|
||||||
@ -340,7 +342,7 @@ final class FlexiblePalette implements SpecializedPalette, Cloneable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int getSectionIndex(int x, int y, int z) {
|
int getSectionIndex(int x, int y, int z) {
|
||||||
final int dimensionBitCount = adaptivePalette.dimensionBitCount;
|
final int dimensionBitCount = this.dimensionBitCount;
|
||||||
return y << (dimensionBitCount << 1) | z << dimensionBitCount | x;
|
return y << (dimensionBitCount << 1) | z << dimensionBitCount | x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,15 +12,15 @@ import java.util.function.IntUnaryOperator;
|
|||||||
*/
|
*/
|
||||||
public interface Palette extends Writeable {
|
public interface Palette extends Writeable {
|
||||||
static Palette blocks() {
|
static Palette blocks() {
|
||||||
return newPalette(16, 8, 6, 1);
|
return newPalette(16, 8, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Palette biomes() {
|
static Palette biomes() {
|
||||||
return newPalette(4, 2, 1, 1);
|
return newPalette(4, 3, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Palette newPalette(int dimension, int maxBitsPerEntry, int bitsPerEntry, int bitIncrement) {
|
static Palette newPalette(int dimension, int maxBitsPerEntry, int bitsPerEntry) {
|
||||||
return new AdaptivePalette(dimension, maxBitsPerEntry, bitsPerEntry, bitIncrement);
|
return new AdaptivePalette(dimension, maxBitsPerEntry, bitsPerEntry);
|
||||||
}
|
}
|
||||||
|
|
||||||
int get(int x, int y, int z);
|
int get(int x, int y, int z);
|
||||||
|
@ -71,7 +71,7 @@ public class PaletteTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void resize() {
|
public void resize() {
|
||||||
Palette palette = Palette.newPalette(16, 5, 2, 1);
|
Palette palette = Palette.newPalette(16, 5, 2);
|
||||||
palette.set(0, 0, 0, 1);
|
palette.set(0, 0, 0, 1);
|
||||||
assertEquals(2, palette.bitsPerEntry());
|
assertEquals(2, palette.bitsPerEntry());
|
||||||
palette.set(0, 0, 1, 2);
|
palette.set(0, 0, 1, 2);
|
||||||
@ -252,7 +252,7 @@ public class PaletteTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void replaceLoop() {
|
public void replaceLoop() {
|
||||||
var palette = Palette.newPalette(2, 15, 4, 1);
|
var palette = Palette.newPalette(2, 15, 4);
|
||||||
palette.setAll((x, y, z) -> x + y + z);
|
palette.setAll((x, y, z) -> x + y + z);
|
||||||
final int dimension = palette.dimension();
|
final int dimension = palette.dimension();
|
||||||
for (int x = 0; x < dimension; x++) {
|
for (int x = 0; x < dimension; x++) {
|
||||||
@ -266,21 +266,21 @@ public class PaletteTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void dimension() {
|
public void dimension() {
|
||||||
assertThrows(Exception.class, () -> Palette.newPalette(-4, 5, 3, 1));
|
assertThrows(Exception.class, () -> Palette.newPalette(-4, 5, 3));
|
||||||
assertThrows(Exception.class, () -> Palette.newPalette(0, 5, 3, 1));
|
assertThrows(Exception.class, () -> Palette.newPalette(0, 5, 3));
|
||||||
assertThrows(Exception.class, () -> Palette.newPalette(1, 5, 3, 1));
|
assertThrows(Exception.class, () -> Palette.newPalette(1, 5, 3));
|
||||||
assertDoesNotThrow(() -> Palette.newPalette(2, 5, 3, 1));
|
assertDoesNotThrow(() -> Palette.newPalette(2, 5, 3));
|
||||||
assertThrows(Exception.class, () -> Palette.newPalette(3, 5, 3, 1));
|
assertThrows(Exception.class, () -> Palette.newPalette(3, 5, 3));
|
||||||
assertDoesNotThrow(() -> Palette.newPalette(4, 5, 3, 1));
|
assertDoesNotThrow(() -> Palette.newPalette(4, 5, 3));
|
||||||
assertThrows(Exception.class, () -> Palette.newPalette(6, 5, 3, 1));
|
assertThrows(Exception.class, () -> Palette.newPalette(6, 5, 3));
|
||||||
assertDoesNotThrow(() -> Palette.newPalette(16, 5, 3, 1));
|
assertDoesNotThrow(() -> Palette.newPalette(16, 5, 3));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<Palette> testPalettes() {
|
private static List<Palette> testPalettes() {
|
||||||
return List.of(
|
return List.of(
|
||||||
Palette.newPalette(2, 5, 3, 1),
|
Palette.newPalette(2, 5, 3),
|
||||||
Palette.newPalette(4, 5, 3, 1),
|
Palette.newPalette(4, 5, 3),
|
||||||
Palette.newPalette(8, 5, 3, 1),
|
Palette.newPalette(8, 5, 3),
|
||||||
Palette.newPalette(16, 5, 3, 1));
|
Palette.newPalette(16, 5, 3));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user