Skip to content

Commit

Permalink
Copy default return value for map in LithiumHashPalette#copy
Browse files Browse the repository at this point in the history
  • Loading branch information
2No2Name committed Feb 3, 2025
1 parent 6d97cda commit b3e0c91
Showing 1 changed file with 12 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.caffeinemc.mods.lithium.common.world.chunk;

import com.google.common.collect.ImmutableList;
import it.unimi.dsi.fastutil.HashCommon;
import it.unimi.dsi.fastutil.objects.Reference2IntMap;
import it.unimi.dsi.fastutil.objects.Reference2IntOpenHashMap;
Expand All @@ -13,6 +12,7 @@
import net.minecraft.world.level.chunk.MissingPaletteEntryException;
import net.minecraft.world.level.chunk.Palette;
import net.minecraft.world.level.chunk.PaletteResize;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.List;
Expand All @@ -35,7 +35,7 @@ public class LithiumHashPalette<T> implements Palette<T> {
private T[] entries;
private int size = 0;

public LithiumHashPalette(IdMap<T> idList, PaletteResize<T> resizeHandler, int indexBits, T[] entries, Reference2IntMap<T> table, int size) {
private LithiumHashPalette(IdMap<T> idList, PaletteResize<T> resizeHandler, int indexBits, T[] entries, Reference2IntMap<T> table, int size) {
this.idList = idList;
this.resizeHandler = resizeHandler;
this.indexBits = indexBits;
Expand Down Expand Up @@ -66,7 +66,7 @@ public LithiumHashPalette(IdMap<T> idList, int bits, PaletteResize<T> resizeHand
}

@Override
public int idFor(T obj) {
public int idFor(@NotNull T obj) {
int id = this.table.getInt(obj);

if (id == ABSENT_VALUE) {
Expand All @@ -77,7 +77,7 @@ public int idFor(T obj) {
}

@Override
public boolean maybeHas(Predicate<T> predicate) {
public boolean maybeHas(@NotNull Predicate<T> predicate) {
for (int i = 0; i < this.size; ++i) {
if (predicate.test(this.entries[i])) {
return true;
Expand Down Expand Up @@ -121,7 +121,7 @@ private void resize(int neededCapacity) {
}

@Override
public T valueFor(int id) {
public @NotNull T valueFor(int id) {
T[] entries = this.entries;

T entry = null;
Expand Down Expand Up @@ -156,7 +156,7 @@ public void read(FriendlyByteBuf buf) {
int entryCount = buf.readVarInt();

for (int i = 0; i < entryCount; ++i) {
this.addEntry(this.idList.byId(buf.readVarInt()));
this.addEntry(this.idList.byIdOrThrow(buf.readVarInt()));
}
}

Expand Down Expand Up @@ -187,8 +187,10 @@ public int getSize() {
}

@Override
public Palette<T> copy(PaletteResize<T> resizeHandler) {
return new LithiumHashPalette<>(this.idList, resizeHandler, this.indexBits, this.entries.clone(), new Reference2IntOpenHashMap<>(this.table), this.size);
public @NotNull Palette<T> copy(@NotNull PaletteResize<T> resizeHandler) {
Reference2IntOpenHashMap<T> newTable = new Reference2IntOpenHashMap<>(this.table);
newTable.defaultReturnValue(this.table.defaultReturnValue());
return new LithiumHashPalette<>(this.idList, resizeHandler, this.indexBits, this.entries.clone(), newTable, this.size);
}

private void clear() {
Expand All @@ -198,13 +200,8 @@ private void clear() {
}

public List<T> getElements() {
ImmutableList.Builder<T> builder = new ImmutableList.Builder<>();
for (T entry : this.entries) {
if (entry != null) {
builder.add(entry);
}
}
return builder.build();
T[] copy = Arrays.copyOf(this.entries, this.size);
return Arrays.asList(copy);
}

public static <A> Palette<A> create(int bits, IdMap<A> idList, PaletteResize<A> listener, List<A> list) {
Expand Down

0 comments on commit b3e0c91

Please # to comment.