mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-04-08 16:56:25 +00:00
Fixes
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
|
||||
package com.volmit.iris.util.hunk.bits;
|
||||
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.util.data.Varint;
|
||||
import org.apache.commons.lang3.Validate;
|
||||
|
||||
@@ -66,7 +67,7 @@ public class DataBits {
|
||||
|
||||
public DataBits(int bits, int length, DataInputStream din) throws IOException
|
||||
{
|
||||
this(bits, length, longs(din, (length + ((char) (64 / bits)) - 1) / ((char) (64 / bits))));
|
||||
this(bits, length, longs(din));
|
||||
}
|
||||
|
||||
public DataBits(int bits, int length, AtomicLongArray data) {
|
||||
@@ -80,10 +81,11 @@ public class DataBits {
|
||||
this.divideAdd = MAGIC[var3 + 1];
|
||||
this.divideShift = MAGIC[var3 + 2];
|
||||
int var4 = (length + valuesPerLong - 1) / valuesPerLong;
|
||||
|
||||
if (data != null) {
|
||||
if (data.length() != var4)
|
||||
{
|
||||
throw new RuntimeException("NO!");
|
||||
throw new RuntimeException("NO! Trying to load " + data.length() + " into actual size of " + var4 + " because length: " + length + " (bits: " + bits + ")");
|
||||
}
|
||||
this.data = data;
|
||||
} else {
|
||||
@@ -91,10 +93,20 @@ public class DataBits {
|
||||
}
|
||||
}
|
||||
|
||||
private static AtomicLongArray longs(DataInputStream din, int len) throws IOException{
|
||||
AtomicLongArray a = new AtomicLongArray(len);
|
||||
public String toString()
|
||||
{
|
||||
return "DBits: " + size + "/" + bits + "[" + data.length() + "]";
|
||||
}
|
||||
|
||||
for(int i = 0; i < len; i++)
|
||||
private static int dataLength(int bits, int length)
|
||||
{
|
||||
return (length + ((char) (64 / bits)) - 1) / ((char) (64 / bits));
|
||||
}
|
||||
|
||||
private static AtomicLongArray longs(DataInputStream din) throws IOException{
|
||||
AtomicLongArray a = new AtomicLongArray(din.readInt());
|
||||
|
||||
for(int i = 0; i < a.length(); i++)
|
||||
{
|
||||
a.set(i, din.readLong());
|
||||
}
|
||||
@@ -108,7 +120,12 @@ public class DataBits {
|
||||
{
|
||||
DataBits newData = new DataBits(newBits, size);
|
||||
AtomicInteger c = new AtomicInteger(0);
|
||||
getAll((i) -> newData.set(c.incrementAndGet(), i));
|
||||
|
||||
for(int i = 0; i < size; i++)
|
||||
{
|
||||
newData.set(i, get(i));
|
||||
}
|
||||
|
||||
return newData;
|
||||
}
|
||||
|
||||
@@ -181,7 +198,7 @@ public class DataBits {
|
||||
}
|
||||
|
||||
public void write(DataOutputStream dos) throws IOException {
|
||||
dos.writeByte(bits);
|
||||
dos.writeInt(data.length());
|
||||
|
||||
for(int i = 0; i < data.length(); i++)
|
||||
{
|
||||
|
||||
@@ -18,13 +18,13 @@
|
||||
|
||||
package com.volmit.iris.util.hunk.bits;
|
||||
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.util.data.Varint;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
@@ -40,13 +40,27 @@ public class DataContainer<T> {
|
||||
private final int length;
|
||||
private final Writable<T> writer;
|
||||
|
||||
public DataContainer(Writable<T> writer, int length)
|
||||
public DataContainer(Writable<T> writer, int length, T empty)
|
||||
{
|
||||
this.writer = writer;
|
||||
this.length = length;
|
||||
this.palette = new AtomicReference<>(newPalette(INITIAL_BITS));
|
||||
this.data = new AtomicReference<>(new DataBits(INITIAL_BITS, length));
|
||||
this.bits = new AtomicInteger(INITIAL_BITS);
|
||||
this.data = new AtomicReference<>(new DataBits(INITIAL_BITS, length));
|
||||
this.palette = new AtomicReference<>(newPalette(INITIAL_BITS));
|
||||
this.ensurePaletted(empty);
|
||||
}
|
||||
|
||||
public DataContainer(DataInputStream din, Writable<T> writer) throws IOException {
|
||||
this.writer = writer;
|
||||
this.length = din.readInt();
|
||||
this.palette = new AtomicReference<>(newPalette(din));
|
||||
this.data = new AtomicReference<>(new DataBits(palette.get().bits(), length, din));
|
||||
this.bits = new AtomicInteger(palette.get().bits());
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
return "DataContainer <" + length + " x " + bits + " bits> -> Palette<"+palette.get().getClass().getSimpleName().replaceAll("\\QPalette\\E", "")+">: " + palette.get().size() +
|
||||
" " + data.get().toString() + " PalBit: " + palette.get().bits();
|
||||
}
|
||||
|
||||
public byte[] write() throws IOException
|
||||
@@ -56,33 +70,25 @@ public class DataContainer<T> {
|
||||
return boas.toByteArray();
|
||||
}
|
||||
|
||||
public static <T> DataContainer<T> read(InputStream in, Writable<T> writer) throws IOException {
|
||||
DataInputStream din = new DataInputStream(in);
|
||||
return readDin(din, writer);
|
||||
}
|
||||
|
||||
public static <T> DataContainer<T> readDin(DataInputStream in, Writable<T> writer) throws IOException {
|
||||
DataInputStream din = new DataInputStream(in);
|
||||
DataContainer<T> container = new DataContainer<>(writer, Varint.readUnsignedVarInt(din));
|
||||
int paletteSize = Varint.readUnsignedVarInt(din);
|
||||
container.palette.set(container.newPalette(BIT[paletteSize]).from(paletteSize, writer, din));
|
||||
container.data.set(new DataBits(container.palette.get().bits(), container.length, din));
|
||||
return container;
|
||||
}
|
||||
|
||||
public void write(OutputStream out) throws IOException
|
||||
{
|
||||
DataOutputStream dos = new DataOutputStream(out);
|
||||
writeDos(dos);
|
||||
writeDos(new DataOutputStream(out));
|
||||
}
|
||||
|
||||
public void writeDos(DataOutputStream out) throws IOException
|
||||
public void writeDos(DataOutputStream dos) throws IOException
|
||||
{
|
||||
DataOutputStream dos = new DataOutputStream(out);
|
||||
Varint.writeUnsignedVarInt(length);
|
||||
Varint.writeUnsignedVarInt(palette.get().size());
|
||||
dos.writeInt(length);
|
||||
dos.writeInt(palette.get().size());
|
||||
palette.get().iterateIO((data, __) -> writer.writeNodeData(dos, data));
|
||||
data.get().write(dos);
|
||||
dos.flush();
|
||||
}
|
||||
|
||||
private Palette<T> newPalette(DataInputStream din) throws IOException {
|
||||
int paletteSize = din.readInt();
|
||||
Palette<T> d = newPalette(bits(paletteSize));
|
||||
d.from(paletteSize, writer, din);
|
||||
return d;
|
||||
}
|
||||
|
||||
private Palette<T> newPalette(int bits)
|
||||
@@ -95,12 +101,29 @@ public class DataContainer<T> {
|
||||
return new HashPalette<>();
|
||||
}
|
||||
|
||||
private void checkBits()
|
||||
{
|
||||
if(palette.get().size() >= BIT[bits.get()])
|
||||
{
|
||||
setBits(bits.get() + 1);
|
||||
}
|
||||
}
|
||||
|
||||
public void ensurePaletted(T t)
|
||||
{
|
||||
if(palette.get().id(t) == -1)
|
||||
{
|
||||
checkBits();
|
||||
}
|
||||
}
|
||||
|
||||
public void set(int position, T t)
|
||||
{
|
||||
int id = palette.get().id(t);
|
||||
|
||||
if(id == -1)
|
||||
{
|
||||
checkBits();
|
||||
id = palette.get().add(t);
|
||||
}
|
||||
|
||||
@@ -109,14 +132,14 @@ public class DataContainer<T> {
|
||||
|
||||
public T get(int position)
|
||||
{
|
||||
int id = data.get().get(position);
|
||||
int id = data.get().get(position)+1;
|
||||
|
||||
if(id <= 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return palette.get().get(id - 1);
|
||||
return palette.get().get(id-1);
|
||||
}
|
||||
|
||||
public void setBits(int bits)
|
||||
@@ -143,4 +166,22 @@ public class DataContainer<T> {
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
protected static int bits(int size)
|
||||
{
|
||||
if(DataContainer.BIT[INITIAL_BITS] >= size)
|
||||
{
|
||||
return INITIAL_BITS;
|
||||
}
|
||||
|
||||
for(int i = 0; i < DataContainer.BIT.length; i++)
|
||||
{
|
||||
if(DataContainer.BIT[i] >= size)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return DataContainer.BIT.length - 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,20 +18,22 @@
|
||||
|
||||
package com.volmit.iris.util.hunk.bits;
|
||||
|
||||
import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;
|
||||
import com.volmit.iris.util.collection.KMap;
|
||||
import com.volmit.iris.util.function.Consumer2;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class HashPalette<T> implements Palette<T> {
|
||||
private final KMap<T, Integer> palette;
|
||||
private final LinkedHashMap<T, Integer> palette;
|
||||
private final KMap<Integer, T> lookup;
|
||||
private final AtomicInteger size;
|
||||
|
||||
public HashPalette()
|
||||
{
|
||||
this.size = new AtomicInteger(0);
|
||||
this.palette = new KMap<>();
|
||||
this.palette = new LinkedHashMap<>();
|
||||
this.lookup = new KMap<>();
|
||||
}
|
||||
|
||||
@@ -49,7 +51,12 @@ public class HashPalette<T> implements Palette<T> {
|
||||
public int add(T t) {
|
||||
int index = size.getAndIncrement();
|
||||
palette.put(t, index);
|
||||
lookup.put(index, t);
|
||||
|
||||
if(t != null)
|
||||
{
|
||||
lookup.put(index, t);
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
|
||||
@@ -70,7 +70,12 @@ public class LinearPalette<T> implements Palette<T> {
|
||||
public int id(T t) {
|
||||
for(int i = 0; i < size(); i++)
|
||||
{
|
||||
if(t.equals(palette.get().get(i)))
|
||||
if(t == null && palette.get().get(i) == null)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
|
||||
if(t != null && t.equals(palette.get().get(i)))
|
||||
{
|
||||
return i;
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
|
||||
package com.volmit.iris.util.hunk.bits;
|
||||
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
import com.volmit.iris.util.function.Consumer2;
|
||||
import com.volmit.iris.util.function.Consumer2IO;
|
||||
|
||||
@@ -35,25 +36,7 @@ public interface Palette<T> {
|
||||
|
||||
default int bits()
|
||||
{
|
||||
return bits(DataContainer.INITIAL_BITS);
|
||||
}
|
||||
|
||||
default int bits(int minBits)
|
||||
{
|
||||
if(size() <= DataContainer.BIT[minBits])
|
||||
{
|
||||
return minBits;
|
||||
}
|
||||
|
||||
for(int i = 0; i < DataContainer.BIT.length; i++)
|
||||
{
|
||||
if(DataContainer.BIT[i] >= size())
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return DataContainer.BIT.length - 1;
|
||||
return DataContainer.bits(size());
|
||||
}
|
||||
|
||||
void iterate(Consumer2<T, Integer> c);
|
||||
@@ -83,4 +66,11 @@ public interface Palette<T> {
|
||||
oldPalette.iterate((k,v) -> add(k));
|
||||
return this;
|
||||
}
|
||||
|
||||
default KList<T> list()
|
||||
{
|
||||
KList<T> t = new KList<>();
|
||||
iterate((tx, __) -> t.add(tx));
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,12 +37,11 @@ import java.util.Map;
|
||||
public class PaletteHunk<T> extends StorageHunk<T> implements Hunk<T> {
|
||||
private DataContainer<T> data;
|
||||
|
||||
public PaletteHunk(int w, int h, int d, Writable<T> writer) {
|
||||
public PaletteHunk(int w, int h, int d, Writable<T> writer, T e) {
|
||||
super(w,h,d);
|
||||
data = new DataContainer<>(writer, w * h * d);
|
||||
data = new DataContainer<>(writer, w * h * d, e);
|
||||
}
|
||||
|
||||
|
||||
public void setPalette(DataContainer<T> c) {
|
||||
data = c;
|
||||
}
|
||||
|
||||
@@ -31,9 +31,9 @@ import java.util.function.Supplier;
|
||||
|
||||
public abstract class PaletteOrHunk<T> extends StorageHunk<T> implements Hunk<T>, Writable<T> {
|
||||
private final Hunk<T> hunk;
|
||||
public PaletteOrHunk(int width, int height, int depth, boolean allow, Supplier<Hunk<T>> factory) {
|
||||
public PaletteOrHunk(int width, int height, int depth, boolean allow, Supplier<Hunk<T>> factory, T e) {
|
||||
super(width, height, depth);
|
||||
hunk = (allow) ? new PaletteHunk<>(width, height, depth, this) : factory.get();
|
||||
hunk = (allow) ? new PaletteHunk<>(width, height, depth, this, e) : factory.get();
|
||||
}
|
||||
|
||||
public DataContainer<T> palette()
|
||||
|
||||
Reference in New Issue
Block a user