Remove valuesPerLong

This commit is contained in:
themode 2022-01-03 21:47:16 +01:00 committed by TheMode
parent 7af813ea72
commit b4b6c333bf

View File

@ -8,12 +8,18 @@ import net.minestom.server.utils.binary.BinaryWriter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
final class PaletteImpl implements Palette, Cloneable { final class PaletteImpl implements Palette, Cloneable {
// Magic values generated with "Integer.MAX_VALUE >> (31 - bitsPerIndex)" for bitsPerIndex between 1 and 16 private static final int[] MAGIC_MASKS;
private static final int[] MAGIC_MASKS = private static final int[] VALUES_PER_LONG;
{0, 1, 3, 7,
15, 31, 63, 127, 255, static {
511, 1023, 2047, 4095, final int entries = 16;
8191, 16383, 32767}; MAGIC_MASKS = new int[entries];
VALUES_PER_LONG = new int[entries];
for (int i = 1; i < entries; i++) {
MAGIC_MASKS[i] = Integer.MAX_VALUE >> (31 - i);
VALUES_PER_LONG[i] = Long.SIZE / i;
}
}
// Specific to this palette type // Specific to this palette type
private final int dimension; private final int dimension;
@ -23,7 +29,6 @@ final class PaletteImpl implements Palette, Cloneable {
private int bitsPerEntry; private int bitsPerEntry;
private int valuesPerLong;
private boolean hasPalette; private boolean hasPalette;
private int lastPaletteIndex = 1; // First index is air private int lastPaletteIndex = 1; // First index is air
@ -46,7 +51,6 @@ final class PaletteImpl implements Palette, Cloneable {
this.bitsPerEntry = bitsPerEntry; this.bitsPerEntry = bitsPerEntry;
this.valuesPerLong = Long.SIZE / bitsPerEntry;
this.hasPalette = bitsPerEntry <= maxBitsPerEntry; this.hasPalette = bitsPerEntry <= maxBitsPerEntry;
this.paletteToValueList = new IntArrayList(1); this.paletteToValueList = new IntArrayList(1);
@ -67,6 +71,8 @@ final class PaletteImpl implements Palette, Cloneable {
x %= dimension; x %= dimension;
y %= dimension; y %= dimension;
z %= dimension; z %= dimension;
final int valuesPerLong = VALUES_PER_LONG[bitsPerEntry];
final int sectionIdentifier = getSectionIndex(x, y, z); final int sectionIdentifier = getSectionIndex(x, y, z);
final int index = sectionIdentifier / valuesPerLong; final int index = sectionIdentifier / valuesPerLong;
final int bitIndex = sectionIdentifier % valuesPerLong * bitsPerEntry; final int bitIndex = sectionIdentifier % valuesPerLong * bitsPerEntry;
@ -87,6 +93,7 @@ final class PaletteImpl implements Palette, Cloneable {
return; return;
} }
// Initialize the section // Initialize the section
final int valuesPerLong = VALUES_PER_LONG[bitsPerEntry];
this.values = new long[(size + valuesPerLong - 1) / valuesPerLong]; this.values = new long[(size + valuesPerLong - 1) / valuesPerLong];
} }
x %= dimension; x %= dimension;
@ -94,6 +101,7 @@ final class PaletteImpl implements Palette, Cloneable {
z %= dimension; z %= dimension;
// Change to palette value // Change to palette value
value = getPaletteIndex(value); value = getPaletteIndex(value);
final int valuesPerLong = VALUES_PER_LONG[bitsPerEntry];
final int sectionIndex = getSectionIndex(x, y, z); final int sectionIndex = getSectionIndex(x, y, z);
final int index = sectionIndex / valuesPerLong; final int index = sectionIndex / valuesPerLong;
final int bitIndex = (sectionIndex % valuesPerLong) * bitsPerEntry; final int bitIndex = (sectionIndex % valuesPerLong) * bitsPerEntry;
@ -194,7 +202,6 @@ final class PaletteImpl implements Palette, Cloneable {
this.bitsPerEntry = palette.bitsPerEntry; this.bitsPerEntry = palette.bitsPerEntry;
this.valuesPerLong = palette.valuesPerLong;
this.hasPalette = palette.hasPalette; this.hasPalette = palette.hasPalette;
this.lastPaletteIndex = palette.lastPaletteIndex; this.lastPaletteIndex = palette.lastPaletteIndex;
this.count = palette.count; this.count = palette.count;