forked from GeyserMC/Geyser
100 lines
3.4 KiB
Java
100 lines
3.4 KiB
Java
/*
|
|
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
|
*
|
|
* 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:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* 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.
|
|
*
|
|
* @author GeyserMC
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
*/
|
|
|
|
package org.geysermc.connector.network.translators.world.chunk.bitarray;
|
|
|
|
import com.nukkitx.network.util.Preconditions;
|
|
import org.geysermc.connector.utils.MathUtils;
|
|
|
|
import java.util.Arrays;
|
|
|
|
public class PaddedBitArray implements BitArray {
|
|
|
|
/**
|
|
* Array used to store data
|
|
*/
|
|
private final int[] words;
|
|
|
|
/**
|
|
* Palette version information
|
|
*/
|
|
private final BitArrayVersion version;
|
|
|
|
/**
|
|
* Number of entries in this palette (<b>not</b> the length of the words array that internally backs this palette)
|
|
*/
|
|
private final int size;
|
|
|
|
PaddedBitArray(BitArrayVersion version, int size, int[] words) {
|
|
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
|
|
public BitArrayVersion getVersion() {
|
|
return this.version;
|
|
}
|
|
|
|
@Override
|
|
public BitArray copy() {
|
|
return new PaddedBitArray(this.version, this.size, Arrays.copyOf(this.words, this.words.length));
|
|
}
|
|
}
|