сериализация полного чанка
This commit is contained in:
@@ -10,4 +10,5 @@ public interface BitArray {
|
||||
int get(int index);
|
||||
|
||||
ByteBuffer byteBuffer();
|
||||
int size();
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package mc.utils.array;
|
||||
|
||||
public class BitByteArray extends AbstractBitBufferArray {
|
||||
|
||||
private int countElements;
|
||||
|
||||
public BitByteArray(int bitPerEntity, int arraySize, boolean direct) {
|
||||
super(bitPerEntity, arraySize, direct);
|
||||
}
|
||||
@@ -55,8 +57,16 @@ public class BitByteArray extends AbstractBitBufferArray {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return countElements;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int calculateCapacity() {
|
||||
return (arraySize * bitPerEntity / Byte.SIZE + 1) * Byte.BYTES;
|
||||
int bits = arraySize * bitPerEntity;
|
||||
int var1 = bits % Byte.SIZE;
|
||||
this.countElements = bits / Byte.SIZE + (var1 > 0 ? 1 : 0);
|
||||
return countElements;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.nio.LongBuffer;
|
||||
public class BitLongArray extends AbstractBitBufferArray {
|
||||
|
||||
private final LongBuffer longBuffer;
|
||||
private int countElements;
|
||||
|
||||
public BitLongArray(int bitPerEntity, int arraySize, boolean direct) {
|
||||
super(bitPerEntity, arraySize, direct);
|
||||
@@ -60,8 +61,17 @@ public class BitLongArray extends AbstractBitBufferArray {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return countElements;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int calculateCapacity() {
|
||||
return (arraySize * bitPerEntity / Long.SIZE + 1) * Long.BYTES;
|
||||
int bits = arraySize * bitPerEntity;
|
||||
int var1 = bits % Long.SIZE;
|
||||
int var2 = (bits + (var1 > 0 ? Long.SIZE - var1 : 0));
|
||||
this.countElements = var2 / Long.SIZE;
|
||||
return var2 / Byte.SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,12 +17,21 @@ class BitByteArrayTest {
|
||||
assertEquals(1, byteBuffer.limit());
|
||||
assertEquals(0, byteBuffer.position());
|
||||
assertEquals(1, byteBuffer.array().length);
|
||||
assertEquals(1, bitArray.size());
|
||||
|
||||
bitArray = new BitByteArray(4, 2);
|
||||
byteBuffer = bitArray.byteBuffer();
|
||||
|
||||
assertEquals(1, byteBuffer.capacity());
|
||||
assertEquals(1, byteBuffer.limit());
|
||||
assertEquals(1, bitArray.size());
|
||||
|
||||
bitArray = new BitByteArray(4, 3);
|
||||
byteBuffer = bitArray.byteBuffer();
|
||||
|
||||
assertEquals(2, byteBuffer.capacity());
|
||||
assertEquals(2, byteBuffer.limit());
|
||||
assertEquals(2, bitArray.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -18,12 +18,14 @@ class BitLongArrayTest {
|
||||
assertEquals(8, byteBuffer.limit());
|
||||
assertEquals(0, byteBuffer.position());
|
||||
assertEquals(8, byteBuffer.array().length);
|
||||
assertEquals(1, bitArray.size());
|
||||
|
||||
bitArray = new BitLongArray(13, 5);
|
||||
byteBuffer = bitArray.byteBuffer();
|
||||
|
||||
assertEquals(16, byteBuffer.capacity());
|
||||
assertEquals(16, byteBuffer.limit());
|
||||
assertEquals(2, bitArray.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user