2020-01-09 03:05:42 +00:00
|
|
|
/*
|
2021-01-01 15:10:36 +00:00
|
|
|
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
|
2020-01-09 03:05:42 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2020-04-29 20:01:53 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2020-04-29 20:01:53 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
2020-01-09 03:05:42 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
2020-01-09 03:05:42 +00:00
|
|
|
*/
|
|
|
|
|
2020-04-29 20:01:53 +00:00
|
|
|
package org.geysermc.connector.network.translators.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);
|
|
|
|
|
2020-10-15 06:30:25 +00:00
|
|
|
private static final BitArrayVersion[] VALUES = values();
|
|
|
|
|
2019-08-30 21:47:33 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-10-15 06:30:25 +00:00
|
|
|
public static BitArrayVersion forBitsCeil(int bits) {
|
|
|
|
for (int i = VALUES.length - 1; i >= 0; i--) {
|
|
|
|
BitArrayVersion version = VALUES[i];
|
|
|
|
if (version.bits >= bits) {
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2019-08-30 21:47:33 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-15 06:30:25 +00:00
|
|
|
public BitArray createArray(int size) {
|
|
|
|
return this.createArray(size, new int[MathUtils.ceil((float) size / entriesPerWord)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public BitArray createArray(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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|