Skip to content

Commit

Permalink
feat: some improvements on palette & bitarray
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Dec 7, 2024
1 parent d843dbf commit e2edb84
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public enum BitArrayVersion {
}

public static BitArrayVersion get(int version, boolean read) {
for (BitArrayVersion ver : values()) {
for (BitArrayVersion ver : VALUES) {
if ((!read && ver.entriesPerWord <= version) || (read && ver.bits == version)) {
return ver;
}
Expand All @@ -52,24 +52,24 @@ public static BitArrayVersion forBitsCeil(int bits) {
return null;
}

public int getWordsForSize(int size) {
Preconditions.checkArgument(this != V0);
return GenericMath.ceil((float) size / this.entriesPerWord);
}

public BitArray createArray(int size) {
if (this == V0) {
return SingletonBitArray.INSTANCE;
}
return this.createArray(size, new int[getWordsForSize(size)]);
}

public int getWordsForSize(int size) {
Preconditions.checkArgument(this != V0);
return GenericMath.ceil((float) size / this.entriesPerWord);
}

public BitArray createArray(int size, int[] words) {
if (this == V3 || this == V5 || this == V6) {
if (this == V0) {
return SingletonBitArray.INSTANCE;
} else if (this == V3 || this == V5 || this == V6) {
// Padded palettes aren't able to use bitwise operations due to their padding.
return new PaddedBitArray(this, size, words);
} else if (this == V0) {
return SingletonBitArray.INSTANCE;
} else {
return new Pow2BitArray(this, size, words);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import java.util.List;

/**
* TODO: add tests
*
* @author JukeboxMC | daoge_cmd | CoolLoong
*/
@Slf4j
Expand Down Expand Up @@ -69,7 +67,7 @@ public void writeToNetwork(ByteBuf byteBuf, RuntimeDataSerializer<V> serializer)
}

public void writeToStoragePersistent(ByteBuf byteBuf, PersistentDataSerializer<V> serializer) {
var version = isEmpty() ? BitArrayVersion.V0 : this.bitArray.version();
var version = oneEntryOnly() ? BitArrayVersion.V0 : this.bitArray.version();
byteBuf.writeByte(Palette.getPaletteHeader(version, false));

if (version != BitArrayVersion.V0) {
Expand Down Expand Up @@ -118,12 +116,12 @@ public void readFromStoragePersistent(ByteBuf byteBuf, PersistentDataDeserialize

public void writeToStorageRuntime(ByteBuf byteBuf, RuntimeDataSerializer<V> serializer, Palette<V> last) {
// FIXME: copy last flag
// if (last != null && last.palette.equals(this.palette)) {
// if (last != null && last.equals(this)) {
// byteBuf.writeByte(COPY_LAST_FLAG_HEADER);
// return;
// }

var version = this.isEmpty() ? BitArrayVersion.V0 : this.bitArray.version();
var version = this.oneEntryOnly() ? BitArrayVersion.V0 : this.bitArray.version();
byteBuf.writeByte(Palette.getPaletteHeader(version, true));
if (version != BitArrayVersion.V0) {
for (int word : this.bitArray.words()) {
Expand Down Expand Up @@ -167,13 +165,18 @@ public void readFromStorageRuntime(ByteBuf byteBuf, RuntimeDataDeserializer<V> d
}
}

public boolean isEmpty() {
public boolean oneEntryOnly() {
if (this.palette.size() == 1) {
return true;
}
// Do not use stream, this will be quicker
// The palette list may contain more than one entry,
// but the words are all point to the first entry.
// In this case, the palette is still one entry only.
for (int word : this.bitArray.words()) {
// Do not use stream, this will be quicker
if (Integer.toUnsignedLong(word) != 0L) {
// The word is not point to the first entry,
// so this palette shouldn't be empty
return false;
}
}
Expand Down Expand Up @@ -217,6 +220,8 @@ private int paletteIndexFor(V value) {
var next = version.next;
if (next != null) {
this.onResize(next);
} else {
throw new PaletteException("Palette have reached the max bit array version");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public BiomeType getBiomeType(int x, int y, int z) {
}

public boolean isAirSection() {
return blockLayers[0].isEmpty() && blockLayers[0].get(0) == AIR.getDefaultState();
return blockLayers[0].oneEntryOnly() && blockLayers[0].get(0) == AIR.getDefaultState();
}

public void writeToNetwork(ByteBuf byteBuf) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.allaymc.server.datastruct.palette;

import org.allaymc.server.datastruct.bitarray.BitArrayVersion;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* @author daoge_cmd
*/
class PaletteTest {

@Test
void test() {
var obj0 = new Object();
var palette = new Palette<>(obj0, BitArrayVersion.V0);
assertEquals(obj0, palette.get(0));

var obj1 = new Object();
palette.set(1, obj1);
assertEquals(obj1, palette.get(1));

var obj2 = new Object();
palette.set(2, obj2);
assertEquals(obj2, palette.get(2));

// TODO: more tests
}
}

0 comments on commit e2edb84

Please sign in to comment.