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 com.nukkitx.network.util.Preconditions;
|
|
|
|
import org.geysermc.connector.utils.MathUtils;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
2019-09-10 21:50:34 +00:00
|
|
|
public class PaddedBitArray implements BitArray {
|
2019-08-30 21:47:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Array used to store data
|
|
|
|
*/
|
|
|
|
private final int[] words;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Palette version information
|
|
|
|
*/
|
2019-09-10 21:50:34 +00:00
|
|
|
private final BitArrayVersion version;
|
2019-08-30 21:47:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of entries in this palette (<b>not</b> the length of the words array that internally backs this palette)
|
|
|
|
*/
|
|
|
|
private final int size;
|
|
|
|
|
2019-09-10 21:50:34 +00:00
|
|
|
PaddedBitArray(BitArrayVersion version, int size, int[] words) {
|
2019-08-30 21:47:33 +00:00
|
|
|
this.size = size;
|
|
|
|
this.version = version;
|
|
|
|
this.words = words;
|
|
|
|
int expectedWordsLength = MathUtils.ceil((float) size / version.entriesPerWord);
|
|
|
|
if (words.length != expectedWordsLength) {
|
|
|
|
throw new IllegalArgumentException("Invalid length given for storage, got: " + words.length +
|
|
|
|
" but expected: " + expectedWordsLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void set(int index, int value) {
|
|
|
|
Preconditions.checkElementIndex(index, this.size);
|
|
|
|
Preconditions.checkArgument(value >= 0 && value <= this.version.maxEntryValue, "Invalid value");
|
|
|
|
int arrayIndex = index / this.version.entriesPerWord;
|
|
|
|
int offset = (index % this.version.entriesPerWord) * this.version.bits;
|
|
|
|
|
|
|
|
this.words[arrayIndex] = this.words[arrayIndex] & ~(this.version.maxEntryValue << offset) | (value & this.version.maxEntryValue) << offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int get(int index) {
|
|
|
|
Preconditions.checkElementIndex(index, this.size);
|
|
|
|
int arrayIndex = index / this.version.entriesPerWord;
|
|
|
|
int offset = (index % this.version.entriesPerWord) * this.version.bits;
|
|
|
|
|
|
|
|
return (this.words[arrayIndex] >>> offset) & this.version.maxEntryValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int size() {
|
|
|
|
return this.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int[] getWords() {
|
|
|
|
return this.words;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-09-10 21:50:34 +00:00
|
|
|
public BitArrayVersion getVersion() {
|
2019-08-30 21:47:33 +00:00
|
|
|
return this.version;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-09-10 21:50:34 +00:00
|
|
|
public BitArray copy() {
|
|
|
|
return new PaddedBitArray(this.version, this.size, Arrays.copyOf(this.words, this.words.length));
|
2019-08-30 21:47:33 +00:00
|
|
|
}
|
|
|
|
}
|