Improve replaceAll

This commit is contained in:
themode 2022-01-08 06:28:48 +01:00 committed by TheMode
parent 592adccf81
commit 9c8fcc4286

View File

@ -269,21 +269,18 @@ final class PaletteImpl implements Palette, Cloneable {
@Override @Override
public void replaceAll(@NotNull EntryFunction function) { public void replaceAll(@NotNull EntryFunction function) {
int[] cache = sizeCache(maxSize()); int[] cache = sizeCache(maxSize());
AtomicInteger arrayIndex = new AtomicInteger();
AtomicInteger count = new AtomicInteger(); AtomicInteger count = new AtomicInteger();
// Fill cache with values
getAll((x, y, z, value) -> { getAll((x, y, z, value) -> {
final int newValue = function.apply(x, y, z, value); final int newValue = function.apply(x, y, z, value);
final int index = count.getPlain(); final int index = arrayIndex.getPlain();
count.setPlain(index + 1); arrayIndex.setPlain(index + 1);
cache[index] = newValue != value ? getPaletteIndex(newValue) : value; cache[index] = newValue != value ? getPaletteIndex(newValue) : value;
if (newValue != 0) count.setPlain(count.getPlain() + 1);
}); });
// Set values to final arrayUse setAll for generation // Update palette content
count.setPlain(0); updateAll(cache);
setAll((x, y, z) -> { this.count = count.getPlain();
final int index = count.getPlain();
count.setPlain(index + 1);
return cache[index];
});
} }
@Override @Override