PackedBitStorage .java

net.minecraft.util.datafix.PackedBitStorage (class): members: BIT_TO_LONG_SHIFT, getBits, getRaw, bits, mask, data, set, size, get. imports: Validate, Mth.

net.minecraft.util.datafixclass66 linescommon2.1 KB

API Surface (4)

void set(final int index, final int value)
int get(final int index)
long[] getRaw()
int getBits()

Fields & Constants (5)

BIT_TO_LONG_SHIFT
data
bits
mask
size

String literals (2)

"Invalid length given for storage, got: "
" but expected: "

Code Graph: Dependencies (1)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

PackedBitStorage.java66 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util.datafix;
2
3
import net.minecraft.util.Mth;
4
import org.apache.commons.lang3.Validate;
5
6
public class PackedBitStorage {
7
	private static final int BIT_TO_LONG_SHIFT = 6;
8
	private final long[] data;
9
	private final int bits;
10
	private final long mask;
11
	private final int size;
12
13
	public PackedBitStorage(final int bits, final int size) {
14
		this(bits, size, new long[Mth.roundToward(size * bits, 64) / 64]);
15
	}
16
17
	public PackedBitStorage(final int bits, final int size, final long[] data) {
18
		Validate.inclusiveBetween(1L, 32L, bits);
19
		this.size = size;
20
		this.bits = bits;
21
		this.data = data;
22
		this.mask = (1L << bits) - 1L;
23
		int requiredLength = Mth.roundToward(size * bits, 64) / 64;
24
		if (data.length != requiredLength) {
25
			throw new IllegalArgumentException("Invalid length given for storage, got: " + data.length + " but expected: " + requiredLength);
26
		}
27
	}
28
29
	public void set(final int index, final int value) {
30
		Validate.inclusiveBetween(0L, this.size - 1, index);
31
		Validate.inclusiveBetween(0L, this.mask, value);
32
		int position = index * this.bits;
33
		int startData = position >> 6;
34
		int endData = (index + 1) * this.bits - 1 >> 6;
35
		int startBit = position ^ startData << 6;
36
		this.data[startData] = this.data[startData] & ~(this.mask << startBit) | (value & this.mask) << startBit;
37
		if (startData != endData) {
38
			int shiftBits = 64 - startBit;
39
			int wantedBits = this.bits - shiftBits;
40
			this.data[endData] = this.data[endData] >>> wantedBits << wantedBits | (value & this.mask) >> shiftBits;
41
		}
42
	}
43
44
	public int get(final int index) {
45
		Validate.inclusiveBetween(0L, this.size - 1, index);
46
		int position = index * this.bits;
47
		int startData = position >> 6;
48
		int endData = (index + 1) * this.bits - 1 >> 6;
49
		int startBit = position ^ startData << 6;
50
		if (startData == endData) {
51
			return (int)(this.data[startData] >>> startBit & this.mask);
52
		}
53
54
		int shiftBits = 64 - startBit;
55
		return (int)((this.data[startData] >>> startBit | this.data[endData] << shiftBits) & this.mask);
56
	}
57
58
	public long[] getRaw() {
59
		return this.data;
60
	}
61
62
	public int getBits() {
63
		return this.bits;
64
	}
65
}
66

Decompiled Minecraft 26.3 source of PackedBitStorage (net/minecraft/util/datafix/PackedBitStorage.java) with Mojang official mappings, stored verbatim on the site.