2020-01-09 03:05:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
|
|
|
*
|
|
|
|
* This code in this file is derived from NukkitX and permission has
|
|
|
|
* been granted to us allowing the usage of it in Geyser.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2020 The NukkitX Project
|
|
|
|
* https://github.com/NukkitX/Nukkit
|
|
|
|
*/
|
|
|
|
|
2019-09-10 21:50:34 +00:00
|
|
|
package org.geysermc.connector.world.chunk.bitarray;
|
2019-08-30 21:47:33 +00:00
|
|
|
|
|
|
|
import org.geysermc.connector.utils.MathUtils;
|
|
|
|
|
2019-09-10 21:50:34 +00:00
|
|
|
public enum BitArrayVersion {
|
2019-08-30 21:47:33 +00:00
|
|
|
V16(16, 2, null),
|
|
|
|
V8(8, 4, V16),
|
|
|
|
V6(6, 5, V8), // 2 bit padding
|
|
|
|
V5(5, 6, V6), // 2 bit padding
|
|
|
|
V4(4, 8, V5),
|
|
|
|
V3(3, 10, V4), // 2 bit padding
|
|
|
|
V2(2, 16, V3),
|
|
|
|
V1(1, 32, V2);
|
|
|
|
|
|
|
|
final byte bits;
|
|
|
|
final byte entriesPerWord;
|
|
|
|
final int maxEntryValue;
|
2019-09-10 21:50:34 +00:00
|
|
|
final BitArrayVersion next;
|
2019-08-30 21:47:33 +00:00
|
|
|
|
2019-09-10 21:50:34 +00:00
|
|
|
BitArrayVersion(int bits, int entriesPerWord, BitArrayVersion next) {
|
2019-08-30 21:47:33 +00:00
|
|
|
this.bits = (byte) bits;
|
|
|
|
this.entriesPerWord = (byte) entriesPerWord;
|
|
|
|
this.maxEntryValue = (1 << this.bits) - 1;
|
|
|
|
this.next = next;
|
|
|
|
}
|
|
|
|
|
2019-09-10 21:50:34 +00:00
|
|
|
public static BitArrayVersion get(int version, boolean read) {
|
|
|
|
for (BitArrayVersion ver : values()) {
|
|
|
|
if ((!read && ver.entriesPerWord <= version) || (read && ver.bits == version)) {
|
|
|
|
return ver;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new IllegalArgumentException("Invalid palette version: " + version);
|
|
|
|
}
|
|
|
|
|
|
|
|
public BitArray createPalette(int size) {
|
2019-08-30 21:47:33 +00:00
|
|
|
return this.createPalette(size, new int[MathUtils.ceil((float) size / entriesPerWord)]);
|
|
|
|
}
|
|
|
|
|
2019-09-10 21:50:34 +00:00
|
|
|
public byte getId() {
|
2019-08-30 21:47:33 +00:00
|
|
|
return bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getMaxEntryValue() {
|
|
|
|
return maxEntryValue;
|
|
|
|
}
|
|
|
|
|
2019-09-10 21:50:34 +00:00
|
|
|
public int getWordsForSize(int size) {
|
|
|
|
return MathUtils.ceil((float) size / entriesPerWord);
|
|
|
|
}
|
|
|
|
|
|
|
|
public BitArrayVersion next() {
|
2019-08-30 21:47:33 +00:00
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
2019-09-10 21:50:34 +00:00
|
|
|
public BitArray createPalette(int size, int[] words) {
|
2019-08-30 21:47:33 +00:00
|
|
|
if (this == V3 || this == V5 || this == V6) {
|
|
|
|
// Padded palettes aren't able to use bitwise operations due to their padding.
|
2019-09-10 21:50:34 +00:00
|
|
|
return new PaddedBitArray(this, size, words);
|
2019-08-30 21:47:33 +00:00
|
|
|
} else {
|
2019-09-10 21:50:34 +00:00
|
|
|
return new Pow2BitArray(this, size, words);
|
2019-08-30 21:47:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|