mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2025-12-19 15:09:18 +00:00
cleanup palette and fix incorrect bit resizing
This commit is contained in:
@@ -105,7 +105,7 @@ nmsBindings.forEach { key, value ->
|
||||
pluginJars(tasks.jar.flatMap { it.archiveFile })
|
||||
javaLauncher = javaToolchains.launcherFor { languageVersion = JavaLanguageVersion.of(jvmVersion.getOrDefault(key, 21))}
|
||||
runDirectory.convention(layout.buildDirectory.dir("run/$key"))
|
||||
systemProperty("disable.watchdog", "")
|
||||
systemProperty("disable.watchdog", "true")
|
||||
systemProperty("net.kyori.ansi.colorLevel", color)
|
||||
systemProperty("com.mojang.eula.agree", true)
|
||||
systemProperty("iris.suppressReporting", !errorReporting)
|
||||
|
||||
@@ -141,11 +141,13 @@ public class DataContainer<T> {
|
||||
}
|
||||
|
||||
public void writeDos(DataOutputStream dos) throws IOException {
|
||||
Varint.writeUnsignedVarInt(length, dos);
|
||||
Varint.writeUnsignedVarInt(palette.get().size(), dos);
|
||||
palette.get().iterateIO((data, __) -> writer.writeNodeData(dos, data));
|
||||
data.get().write(dos);
|
||||
dos.flush();
|
||||
synchronized (this) {
|
||||
Varint.writeUnsignedVarInt(length, dos);
|
||||
Varint.writeUnsignedVarInt(palette.get().size(), dos);
|
||||
palette.get().iterateIO((data, __) -> writer.writeNodeData(dos, data));
|
||||
data.get().write(dos);
|
||||
dos.flush();
|
||||
}
|
||||
}
|
||||
|
||||
private Palette<T> newPalette(DataInputStream din) throws IOException {
|
||||
@@ -163,51 +165,41 @@ public class DataContainer<T> {
|
||||
return new HashPalette<>();
|
||||
}
|
||||
|
||||
public void ensurePaletted(T t) {
|
||||
if (palette.get().id(t) == -1) {
|
||||
expandOne();
|
||||
}
|
||||
}
|
||||
|
||||
public void set(int position, T t) {
|
||||
synchronized (this) {
|
||||
int id = palette.get().id(t);
|
||||
|
||||
if (id == -1) {
|
||||
expandOne();
|
||||
id = palette.get().add(t);
|
||||
updateBits();
|
||||
}
|
||||
|
||||
data.get().set(position, id);
|
||||
}
|
||||
}
|
||||
|
||||
private void expandOne() {
|
||||
if (palette.get().size() + 1 >= BIT[bits.get()]) {
|
||||
setBits(bits.get() + 1);
|
||||
private void updateBits() {
|
||||
if (palette.get().bits() == bits.get())
|
||||
return;
|
||||
|
||||
int bits = palette.get().bits();
|
||||
if (this.bits.get() <= LINEAR_BITS_LIMIT != bits <= LINEAR_BITS_LIMIT) {
|
||||
palette.updateAndGet(p -> newPalette(bits).from(p));
|
||||
}
|
||||
|
||||
data.updateAndGet(d -> d.setBits(bits));
|
||||
this.bits.set(bits);
|
||||
}
|
||||
|
||||
public T get(int position) {
|
||||
synchronized (this) {
|
||||
int id = data.get().get(position) + 1;
|
||||
int id = data.get().get(position);
|
||||
|
||||
if (id <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return palette.get().get(id - 1);
|
||||
}
|
||||
}
|
||||
|
||||
public void setBits(int bits) {
|
||||
if (this.bits.get() != bits) {
|
||||
if (this.bits.get() <= LINEAR_BITS_LIMIT != bits <= LINEAR_BITS_LIMIT) {
|
||||
palette.set(newPalette(bits).from(palette.get()));
|
||||
}
|
||||
|
||||
this.bits.set(bits);
|
||||
data.set(data.get().setBits(bits));
|
||||
return palette.get().get(id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,24 +23,22 @@ import com.volmit.iris.util.function.Consumer2;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
public class HashPalette<T> implements Palette<T> {
|
||||
private final ReentrantLock lock = new ReentrantLock();
|
||||
private final LinkedHashMap<T, Integer> palette;
|
||||
private final KMap<Integer, T> lookup;
|
||||
private final AtomicInteger size;
|
||||
|
||||
public HashPalette() {
|
||||
this.size = new AtomicInteger(0);
|
||||
this.size = new AtomicInteger(1);
|
||||
this.palette = new LinkedHashMap<>();
|
||||
this.lookup = new KMap<>();
|
||||
add(null);
|
||||
palette.put(null, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get(int id) {
|
||||
if (id < 0 || id >= size.get()) {
|
||||
if (id <= 0 || id >= size.get()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -49,17 +47,16 @@ public class HashPalette<T> implements Palette<T> {
|
||||
|
||||
@Override
|
||||
public int add(T t) {
|
||||
lock.lock();
|
||||
try {
|
||||
int index = size.getAndIncrement();
|
||||
palette.put(t, index);
|
||||
if (t == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (t != null) {
|
||||
synchronized (palette) {
|
||||
return palette.computeIfAbsent(t, $ -> {
|
||||
int index = size.getAndIncrement();
|
||||
lookup.put(index, t);
|
||||
}
|
||||
return index;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
return index;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,8 +77,7 @@ public class HashPalette<T> implements Palette<T> {
|
||||
|
||||
@Override
|
||||
public void iterate(Consumer2<T, Integer> c) {
|
||||
lock.lock();
|
||||
try {
|
||||
synchronized (palette) {
|
||||
for (T i : palette.keySet()) {
|
||||
if (i == null) {
|
||||
continue;
|
||||
@@ -89,8 +85,6 @@ public class HashPalette<T> implements Palette<T> {
|
||||
|
||||
c.accept(i, id(i));
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,17 +21,16 @@ package com.volmit.iris.util.hunk.bits;
|
||||
import com.volmit.iris.util.function.Consumer2;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.concurrent.atomic.AtomicReferenceArray;
|
||||
|
||||
public class LinearPalette<T> implements Palette<T> {
|
||||
private final AtomicReference<AtomicReferenceArray<T>> palette;
|
||||
private volatile AtomicReferenceArray<T> palette;
|
||||
private final AtomicInteger size;
|
||||
|
||||
public LinearPalette(int initialSize) {
|
||||
this.size = new AtomicInteger(0);
|
||||
this.palette = new AtomicReference<>(new AtomicReferenceArray<>(initialSize));
|
||||
palette.get().set(size.getAndIncrement(), null);
|
||||
this.size = new AtomicInteger(1);
|
||||
this.palette = new AtomicReferenceArray<>(initialSize);
|
||||
palette.set(0, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -40,26 +39,29 @@ public class LinearPalette<T> implements Palette<T> {
|
||||
return null;
|
||||
}
|
||||
|
||||
return palette.get().get(id);
|
||||
return palette.get(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int add(T t) {
|
||||
if (t == null) {
|
||||
return 0;
|
||||
}
|
||||
int index = size.getAndIncrement();
|
||||
grow(index + 1);
|
||||
palette.get().set(index, t);
|
||||
palette.set(index, t);
|
||||
return index;
|
||||
}
|
||||
|
||||
private void grow(int newLength) {
|
||||
if (newLength > palette.get().length()) {
|
||||
AtomicReferenceArray<T> a = new AtomicReferenceArray<>(newLength + size.get());
|
||||
private synchronized void grow(int newLength) {
|
||||
if (newLength > palette.length()) {
|
||||
AtomicReferenceArray<T> a = new AtomicReferenceArray<>(newLength);
|
||||
|
||||
for (int i = 0; i < palette.get().length(); i++) {
|
||||
a.set(i, palette.get().get(i));
|
||||
for (int i = 0; i < palette.length(); i++) {
|
||||
a.set(i, palette.get(i));
|
||||
}
|
||||
|
||||
palette.set(a);
|
||||
palette = a;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,8 +71,8 @@ public class LinearPalette<T> implements Palette<T> {
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (int i = 1; i < size() + 1; i++) {
|
||||
if (t.equals(palette.get().get(i))) {
|
||||
for (int i = 1; i < size.get(); i++) {
|
||||
if (t.equals(palette.get(i))) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
@@ -86,7 +88,7 @@ public class LinearPalette<T> implements Palette<T> {
|
||||
@Override
|
||||
public void iterate(Consumer2<T, Integer> c) {
|
||||
for (int i = 1; i < size() + 1; i++) {
|
||||
c.accept(palette.get().get(i), i);
|
||||
c.accept(palette.get(i), i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user