SimpleBitStorage .java

net.minecraft.util.SimpleBitStorage (class): members: InitializationException, valuesPerLong, divideShift, divideAdd, divideMul, MAGIC, getAndSet, getBits.

net.minecraft.utilclass386 linescommon7.2 KB

Implements

API Surface (10)

int getAndSet(final int index, final int value)
void set(final int index, final int value)
void fill(final int value)
int get(final int index)
long[] getRaw()
int getSize()
int getBits()
void getAll(final IntConsumer output)
void unpack(final int[] output)
BitStorage copy()

Fields & Constants (9)

MAGIC
data
bits
mask
size
valuesPerLong
divideMul
divideAdd
divideShift

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

SimpleBitStorage.java386 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util;
2
3
import java.util.Arrays;
4
import java.util.function.IntConsumer;
5
import org.apache.commons.lang3.Validate;
6
import org.jspecify.annotations.Nullable;
7
8
public class SimpleBitStorage implements BitStorage {
9
	private static final int[] MAGIC = new int[]{
10
		-1,
11
		-1,
12
		0,
13
		Integer.MIN_VALUE,
14
		0,
15
		0,
16
		1431655765,
17
		1431655765,
18
		0,
19
		Integer.MIN_VALUE,
20
		0,
21
		1,
22
		858993459,
23
		858993459,
24
		0,
25
		715827882,
26
		715827882,
27
		0,
28
		613566756,
29
		613566756,
30
		0,
31
		Integer.MIN_VALUE,
32
		0,
33
		2,
34
		477218588,
35
		477218588,
36
		0,
37
		429496729,
38
		429496729,
39
		0,
40
		390451572,
41
		390451572,
42
		0,
43
		357913941,
44
		357913941,
45
		0,
46
		330382099,
47
		330382099,
48
		0,
49
		306783378,
50
		306783378,
51
		0,
52
		286331153,
53
		286331153,
54
		0,
55
		Integer.MIN_VALUE,
56
		0,
57
		3,
58
		252645135,
59
		252645135,
60
		0,
61
		238609294,
62
		238609294,
63
		0,
64
		226050910,
65
		226050910,
66
		0,
67
		214748364,
68
		214748364,
69
		0,
70
		204522252,
71
		204522252,
72
		0,
73
		195225786,
74
		195225786,
75
		0,
76
		186737708,
77
		186737708,
78
		0,
79
		178956970,
80
		178956970,
81
		0,
82
		171798691,
83
		171798691,
84
		0,
85
		165191049,
86
		165191049,
87
		0,
88
		159072862,
89
		159072862,
90
		0,
91
		153391689,
92
		153391689,
93
		0,
94
		148102320,
95
		148102320,
96
		0,
97
		143165576,
98
		143165576,
99
		0,
100
		138547332,
101
		138547332,
102
		0,
103
		Integer.MIN_VALUE,
104
		0,
105
		4,
106
		130150524,
107
		130150524,
108
		0,
109
		126322567,
110
		126322567,
111
		0,
112
		122713351,
113
		122713351,
114
		0,
115
		119304647,
116
		119304647,
117
		0,
118
		116080197,
119
		116080197,
120
		0,
121
		113025455,
122
		113025455,
123
		0,
124
		110127366,
125
		110127366,
126
		0,
127
		107374182,
128
		107374182,
129
		0,
130
		104755299,
131
		104755299,
132
		0,
133
		102261126,
134
		102261126,
135
		0,
136
		99882960,
137
		99882960,
138
		0,
139
		97612893,
140
		97612893,
141
		0,
142
		95443717,
143
		95443717,
144
		0,
145
		93368854,
146
		93368854,
147
		0,
148
		91382282,
149
		91382282,
150
		0,
151
		89478485,
152
		89478485,
153
		0,
154
		87652393,
155
		87652393,
156
		0,
157
		85899345,
158
		85899345,
159
		0,
160
		84215045,
161
		84215045,
162
		0,
163
		82595524,
164
		82595524,
165
		0,
166
		81037118,
167
		81037118,
168
		0,
169
		79536431,
170
		79536431,
171
		0,
172
		78090314,
173
		78090314,
174
		0,
175
		76695844,
176
		76695844,
177
		0,
178
		75350303,
179
		75350303,
180
		0,
181
		74051160,
182
		74051160,
183
		0,
184
		72796055,
185
		72796055,
186
		0,
187
		71582788,
188
		71582788,
189
		0,
190
		70409299,
191
		70409299,
192
		0,
193
		69273666,
194
		69273666,
195
		0,
196
		68174084,
197
		68174084,
198
		0,
199
		Integer.MIN_VALUE,
200
		0,
201
		5
202
	};
203
	private final long[] data;
204
	private final int bits;
205
	private final long mask;
206
	private final int size;
207
	private final int valuesPerLong;
208
	private final int divideMul;
209
	private final int divideAdd;
210
	private final int divideShift;
211
212
	public SimpleBitStorage(final int bits, final int size, final int[] values) {
213
		this(bits, size);
214
		int outputIndex = 0;
215
216
		int inputOffset;
217
		for (inputOffset = 0; inputOffset <= size - this.valuesPerLong; inputOffset += this.valuesPerLong) {
218
			long packedValue = 0L;
219
220
			for (int indexInLong = this.valuesPerLong - 1; indexInLong >= 0; indexInLong--) {
221
				packedValue <<= bits;
222
				packedValue |= values[inputOffset + indexInLong] & this.mask;
223
			}
224
225
			this.data[outputIndex++] = packedValue;
226
		}
227
228
		int remainderCount = size - inputOffset;
229
		if (remainderCount > 0) {
230
			long lastPackedValue = 0L;
231
232
			for (int indexInLong = remainderCount - 1; indexInLong >= 0; indexInLong--) {
233
				lastPackedValue <<= bits;
234
				lastPackedValue |= values[inputOffset + indexInLong] & this.mask;
235
			}
236
237
			this.data[outputIndex] = lastPackedValue;
238
		}
239
	}
240
241
	public SimpleBitStorage(final int bits, final int size) {
242
		this(bits, size, (long[])null);
243
	}
244
245
	public SimpleBitStorage(final int bits, final int size, final long @Nullable [] data) {
246
		Validate.inclusiveBetween(1L, 32L, bits);
247
		this.size = size;
248
		this.bits = bits;
249
		this.mask = (1L << bits) - 1L;
250
		this.valuesPerLong = (char)(64 / bits);
251
		int row = 3 * (this.valuesPerLong - 1);
252
		this.divideMul = MAGIC[row + 0];
253
		this.divideAdd = MAGIC[row + 1];
254
		this.divideShift = MAGIC[row + 2];
255
		int requiredLength = (size + this.valuesPerLong - 1) / this.valuesPerLong;
256
		if (data != null) {
257
			if (data.length != requiredLength) {
258
				throw new SimpleBitStorage.InitializationException("Invalid length given for storage, got: " + data.length + " but expected: " + requiredLength);
259
			}
260
261
			this.data = data;
262
		} else {
263
			this.data = new long[requiredLength];
264
		}
265
	}
266
267
	private int cellIndex(final int bitIndex) {
268
		long mul = Integer.toUnsignedLong(this.divideMul);
269
		long add = Integer.toUnsignedLong(this.divideAdd);
270
		return (int)(bitIndex * mul + add >> 32 >> this.divideShift);
271
	}
272
273
	@Override
274
	public int getAndSet(final int index, final int value) {
275
		Validate.inclusiveBetween(0L, this.size - 1, index);
276
		Validate.inclusiveBetween(0L, this.mask, value);
277
		int cellIndex = this.cellIndex(index);
278
		long cellValue = this.data[cellIndex];
279
		int bitIndex = (index - cellIndex * this.valuesPerLong) * this.bits;
280
		int oldValue = (int)(cellValue >> bitIndex & this.mask);
281
		this.data[cellIndex] = cellValue & ~(this.mask << bitIndex) | (value & this.mask) << bitIndex;
282
		return oldValue;
283
	}
284
285
	@Override
286
	public void set(final int index, final int value) {
287
		Validate.inclusiveBetween(0L, this.size - 1, index);
288
		Validate.inclusiveBetween(0L, this.mask, value);
289
		int cellIndex = this.cellIndex(index);
290
		long cellValue = this.data[cellIndex];
291
		int bitIndex = (index - cellIndex * this.valuesPerLong) * this.bits;
292
		this.data[cellIndex] = cellValue & ~(this.mask << bitIndex) | (value & this.mask) << bitIndex;
293
	}
294
295
	@Override
296
	public void fill(final int value) {
297
		Validate.inclusiveBetween(0L, this.mask, value);
298
		long cellValue = value & this.mask;
299
		int offset = this.bits;
300
301
		do {
302
			cellValue |= cellValue << offset;
303
			offset <<= 1;
304
		} while (offset < 64);
305
306
		Arrays.fill(this.data, cellValue);
307
	}
308
309
	@Override
310
	public int get(final int index) {
311
		Validate.inclusiveBetween(0L, this.size - 1, index);
312
		int cellIndex = this.cellIndex(index);
313
		long cellValue = this.data[cellIndex];
314
		int bitIndex = (index - cellIndex * this.valuesPerLong) * this.bits;
315
		return (int)(cellValue >> bitIndex & this.mask);
316
	}
317
318
	@Override
319
	public long[] getRaw() {
320
		return this.data;
321
	}
322
323
	@Override
324
	public int getSize() {
325
		return this.size;
326
	}
327
328
	@Override
329
	public int getBits() {
330
		return this.bits;
331
	}
332
333
	@Override
334
	public void getAll(final IntConsumer output) {
335
		int count = 0;
336
337
		for (long cellValue : this.data) {
338
			for (int value = 0; value < this.valuesPerLong; value++) {
339
				output.accept((int)(cellValue & this.mask));
340
				cellValue >>= this.bits;
341
				if (++count >= this.size) {
342
					return;
343
				}
344
			}
345
		}
346
	}
347
348
	@Override
349
	public void unpack(final int[] output) {
350
		int dataLength = this.data.length;
351
		int outputOffset = 0;
352
353
		for (int i = 0; i < dataLength - 1; i++) {
354
			long cellValue = this.data[i];
355
356
			for (int indexInLong = 0; indexInLong < this.valuesPerLong; indexInLong++) {
357
				output[outputOffset + indexInLong] = (int)(cellValue & this.mask);
358
				cellValue >>= this.bits;
359
			}
360
361
			outputOffset += this.valuesPerLong;
362
		}
363
364
		int remainder = this.size - outputOffset;
365
		if (remainder > 0) {
366
			long cellValue = this.data[dataLength - 1];
367
368
			for (int indexInLong = 0; indexInLong < remainder; indexInLong++) {
369
				output[outputOffset + indexInLong] = (int)(cellValue & this.mask);
370
				cellValue >>= this.bits;
371
			}
372
		}
373
	}
374
375
	@Override
376
	public BitStorage copy() {
377
		return new SimpleBitStorage(this.bits, this.size, (long[])this.data.clone());
378
	}
379
380
	public static class InitializationException extends RuntimeException {
381
		private InitializationException(final String message) {
382
			super(message);
383
		}
384
	}
385
}
386

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