mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-04-06 07:46:08 +00:00
Clean up
This commit is contained in:
@@ -18,8 +18,6 @@
|
||||
|
||||
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;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
@@ -65,8 +63,7 @@ public class DataBits {
|
||||
this(bits, length, (AtomicLongArray) null);
|
||||
}
|
||||
|
||||
public DataBits(int bits, int length, DataInputStream din) throws IOException
|
||||
{
|
||||
public DataBits(int bits, int length, DataInputStream din) throws IOException {
|
||||
this(bits, length, longs(din));
|
||||
}
|
||||
|
||||
@@ -83,8 +80,7 @@ public class DataBits {
|
||||
int var4 = (length + valuesPerLong - 1) / valuesPerLong;
|
||||
|
||||
if (data != null) {
|
||||
if (data.length() != var4)
|
||||
{
|
||||
if (data.length() != var4) {
|
||||
throw new RuntimeException("NO! Trying to load " + data.length() + " into actual size of " + var4 + " because length: " + length + " (bits: " + bits + ")");
|
||||
}
|
||||
this.data = data;
|
||||
@@ -93,36 +89,30 @@ public class DataBits {
|
||||
}
|
||||
}
|
||||
|
||||
public String toString()
|
||||
{
|
||||
public String toString() {
|
||||
return "DBits: " + size + "/" + bits + "[" + data.length() + "]";
|
||||
}
|
||||
|
||||
private static int dataLength(int bits, int length)
|
||||
{
|
||||
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{
|
||||
private static AtomicLongArray longs(DataInputStream din) throws IOException {
|
||||
AtomicLongArray a = new AtomicLongArray(din.readInt());
|
||||
|
||||
for(int i = 0; i < a.length(); i++)
|
||||
{
|
||||
for (int i = 0; i < a.length(); i++) {
|
||||
a.set(i, din.readLong());
|
||||
}
|
||||
|
||||
return a;
|
||||
}
|
||||
|
||||
public DataBits setBits(int newBits)
|
||||
{
|
||||
if(bits != newBits)
|
||||
{
|
||||
public DataBits setBits(int newBits) {
|
||||
if (bits != newBits) {
|
||||
DataBits newData = new DataBits(newBits, size);
|
||||
AtomicInteger c = new AtomicInteger(0);
|
||||
|
||||
for(int i = 0; i < size; i++)
|
||||
{
|
||||
for (int i = 0; i < size; i++) {
|
||||
newData.set(i, get(i));
|
||||
}
|
||||
|
||||
@@ -183,14 +173,12 @@ public class DataBits {
|
||||
|
||||
public void getAll(IntConsumer var0) {
|
||||
int var1 = 0;
|
||||
for(int i = 0; i < data.length(); i++)
|
||||
{
|
||||
for (int i = 0; i < data.length(); i++) {
|
||||
long var5 = data.get(i);
|
||||
for (int var7 = 0; var7 < valuesPerLong; var7++) {
|
||||
var0.accept((int) (var5 & mask));
|
||||
var5 >>= bits;
|
||||
if (++var1 >= size)
|
||||
{
|
||||
if (++var1 >= size) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -200,8 +188,7 @@ public class DataBits {
|
||||
public void write(DataOutputStream dos) throws IOException {
|
||||
dos.writeInt(data.length());
|
||||
|
||||
for(int i = 0; i < data.length(); i++)
|
||||
{
|
||||
for (int i = 0; i < data.length(); i++) {
|
||||
dos.writeLong(data.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,9 +18,6 @@
|
||||
|
||||
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;
|
||||
@@ -40,8 +37,7 @@ public class DataContainer<T> {
|
||||
private final int length;
|
||||
private final Writable<T> writer;
|
||||
|
||||
public DataContainer(Writable<T> writer, int length, T empty)
|
||||
{
|
||||
public DataContainer(Writable<T> writer, int length, T empty) {
|
||||
this.writer = writer;
|
||||
this.length = length;
|
||||
this.bits = new AtomicInteger(INITIAL_BITS);
|
||||
@@ -58,25 +54,22 @@ public class DataContainer<T> {
|
||||
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() +
|
||||
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
|
||||
{
|
||||
public byte[] write() throws IOException {
|
||||
ByteArrayOutputStream boas = new ByteArrayOutputStream();
|
||||
write(boas);
|
||||
return boas.toByteArray();
|
||||
}
|
||||
|
||||
public void write(OutputStream out) throws IOException
|
||||
{
|
||||
public void write(OutputStream out) throws IOException {
|
||||
writeDos(new DataOutputStream(out));
|
||||
}
|
||||
|
||||
public void writeDos(DataOutputStream dos) throws IOException
|
||||
{
|
||||
public void writeDos(DataOutputStream dos) throws IOException {
|
||||
dos.writeInt(length);
|
||||
dos.writeInt(palette.get().size());
|
||||
palette.get().iterateIO((data, __) -> writer.writeNodeData(dos, data));
|
||||
@@ -91,38 +84,30 @@ public class DataContainer<T> {
|
||||
return d;
|
||||
}
|
||||
|
||||
private Palette<T> newPalette(int bits)
|
||||
{
|
||||
if(bits <= LINEAR_BITS_LIMIT)
|
||||
{
|
||||
private Palette<T> newPalette(int bits) {
|
||||
if (bits <= LINEAR_BITS_LIMIT) {
|
||||
return new LinearPalette<>(LINEAR_INITIAL_LENGTH);
|
||||
}
|
||||
|
||||
return new HashPalette<>();
|
||||
}
|
||||
|
||||
private void checkBits()
|
||||
{
|
||||
if(palette.get().size() >= BIT[bits.get()])
|
||||
{
|
||||
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)
|
||||
{
|
||||
public void ensurePaletted(T t) {
|
||||
if (palette.get().id(t) == -1) {
|
||||
checkBits();
|
||||
}
|
||||
}
|
||||
|
||||
public void set(int position, T t)
|
||||
{
|
||||
public void set(int position, T t) {
|
||||
int id = palette.get().id(t);
|
||||
|
||||
if(id == -1)
|
||||
{
|
||||
if (id == -1) {
|
||||
checkBits();
|
||||
id = palette.get().add(t);
|
||||
}
|
||||
@@ -130,24 +115,19 @@ public class DataContainer<T> {
|
||||
data.get().set(position, id);
|
||||
}
|
||||
|
||||
public T get(int position)
|
||||
{
|
||||
int id = data.get().get(position)+1;
|
||||
public T get(int position) {
|
||||
int id = data.get().get(position) + 1;
|
||||
|
||||
if(id <= 0)
|
||||
{
|
||||
if (id <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return palette.get().get(id-1);
|
||||
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)
|
||||
{
|
||||
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()));
|
||||
}
|
||||
|
||||
@@ -159,25 +139,20 @@ public class DataContainer<T> {
|
||||
private static int[] computeBitLimits() {
|
||||
int[] m = new int[16];
|
||||
|
||||
for(int i = 0; i < m.length; i++)
|
||||
{
|
||||
for (int i = 0; i < m.length; i++) {
|
||||
m[i] = (int) Math.pow(2, i);
|
||||
}
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
protected static int bits(int size)
|
||||
{
|
||||
if(DataContainer.BIT[INITIAL_BITS] >= size)
|
||||
{
|
||||
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)
|
||||
{
|
||||
for (int i = 0; i < DataContainer.BIT.length; i++) {
|
||||
if (DataContainer.BIT[i] >= size) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
|
||||
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;
|
||||
|
||||
@@ -30,8 +29,7 @@ public class HashPalette<T> implements Palette<T> {
|
||||
private final KMap<Integer, T> lookup;
|
||||
private final AtomicInteger size;
|
||||
|
||||
public HashPalette()
|
||||
{
|
||||
public HashPalette() {
|
||||
this.size = new AtomicInteger(0);
|
||||
this.palette = new LinkedHashMap<>();
|
||||
this.lookup = new KMap<>();
|
||||
@@ -39,8 +37,7 @@ public class HashPalette<T> implements Palette<T> {
|
||||
|
||||
@Override
|
||||
public T get(int id) {
|
||||
if(id < 0 || id >= size.get())
|
||||
{
|
||||
if (id < 0 || id >= size.get()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -52,8 +49,7 @@ public class HashPalette<T> implements Palette<T> {
|
||||
int index = size.getAndIncrement();
|
||||
palette.put(t, index);
|
||||
|
||||
if(t != null)
|
||||
{
|
||||
if (t != null) {
|
||||
lookup.put(index, t);
|
||||
}
|
||||
|
||||
@@ -73,8 +69,7 @@ public class HashPalette<T> implements Palette<T> {
|
||||
|
||||
@Override
|
||||
public void iterate(Consumer2<T, Integer> c) {
|
||||
for(T i : palette.keySet())
|
||||
{
|
||||
for (T i : palette.keySet()) {
|
||||
c.accept(i, id(i));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,16 +28,14 @@ public class LinearPalette<T> implements Palette<T> {
|
||||
private final AtomicReference<AtomicReferenceArray<T>> palette;
|
||||
private final AtomicInteger size;
|
||||
|
||||
public LinearPalette(int initialSize)
|
||||
{
|
||||
public LinearPalette(int initialSize) {
|
||||
this.size = new AtomicInteger(0);
|
||||
this.palette = new AtomicReference<>(new AtomicReferenceArray<>(initialSize));
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get(int id) {
|
||||
if(id < 0 || id >= size.get())
|
||||
{
|
||||
if (id < 0 || id >= size.get()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -53,12 +51,10 @@ public class LinearPalette<T> implements Palette<T> {
|
||||
}
|
||||
|
||||
private void grow(int newLength) {
|
||||
if(newLength > palette.get().length())
|
||||
{
|
||||
if (newLength > palette.get().length()) {
|
||||
AtomicReferenceArray<T> a = new AtomicReferenceArray<>(newLength + size.get());
|
||||
|
||||
for(int i = 0; i < palette.get().length(); i++)
|
||||
{
|
||||
for (int i = 0; i < palette.get().length(); i++) {
|
||||
a.set(i, palette.get().get(i));
|
||||
}
|
||||
|
||||
@@ -68,15 +64,12 @@ public class LinearPalette<T> implements Palette<T> {
|
||||
|
||||
@Override
|
||||
public int id(T t) {
|
||||
for(int i = 0; i < size(); i++)
|
||||
{
|
||||
if(t == null && palette.get().get(i) == null)
|
||||
{
|
||||
for (int i = 0; i < size(); i++) {
|
||||
if (t == null && palette.get().get(i) == null) {
|
||||
return i;
|
||||
}
|
||||
|
||||
if(t != null && t.equals(palette.get().get(i)))
|
||||
{
|
||||
if (t != null && t.equals(palette.get().get(i))) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
@@ -91,8 +84,7 @@ public class LinearPalette<T> implements Palette<T> {
|
||||
|
||||
@Override
|
||||
public void iterate(Consumer2<T, Integer> c) {
|
||||
for(int i = 0; i < size(); i++)
|
||||
{
|
||||
for (int i = 0; i < size(); i++) {
|
||||
c.accept(palette.get().get(i), i);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,18 +34,16 @@ public interface Palette<T> {
|
||||
|
||||
int size();
|
||||
|
||||
default int bits()
|
||||
{
|
||||
default int bits() {
|
||||
return DataContainer.bits(size());
|
||||
}
|
||||
|
||||
void iterate(Consumer2<T, Integer> c);
|
||||
|
||||
default void iterateIO(Consumer2IO<T, Integer> c)
|
||||
{
|
||||
iterate((a,b)-> {
|
||||
default void iterateIO(Consumer2IO<T, Integer> c) {
|
||||
iterate((a, b) -> {
|
||||
try {
|
||||
c.accept(a,b);
|
||||
c.accept(a, b);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -53,22 +51,19 @@ public interface Palette<T> {
|
||||
}
|
||||
|
||||
default Palette<T> from(int size, Writable<T> writable, DataInputStream in) throws IOException {
|
||||
for(int i = 0; i < size; i++)
|
||||
{
|
||||
for (int i = 0; i < size; i++) {
|
||||
add(writable.readNodeData(in));
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
default Palette<T> from(Palette<T> oldPalette)
|
||||
{
|
||||
oldPalette.iterate((k,v) -> add(k));
|
||||
default Palette<T> from(Palette<T> oldPalette) {
|
||||
oldPalette.iterate((k, v) -> add(k));
|
||||
return this;
|
||||
}
|
||||
|
||||
default KList<T> list()
|
||||
{
|
||||
default KList<T> list() {
|
||||
KList<T> t = new KList<>();
|
||||
iterate((tx, __) -> t.add(tx));
|
||||
return t;
|
||||
|
||||
@@ -27,7 +27,6 @@ import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
@SuppressWarnings({"DefaultAnnotationParam", "Lombok"})
|
||||
@Data
|
||||
|
||||
@@ -18,8 +18,6 @@
|
||||
|
||||
package com.volmit.iris.util.hunk.storage;
|
||||
|
||||
import com.volmit.iris.util.collection.KMap;
|
||||
import com.volmit.iris.util.data.palette.PalettedContainer;
|
||||
import com.volmit.iris.util.function.Consumer4;
|
||||
import com.volmit.iris.util.function.Consumer4IO;
|
||||
import com.volmit.iris.util.hunk.Hunk;
|
||||
@@ -29,7 +27,6 @@ import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
@SuppressWarnings({"DefaultAnnotationParam", "Lombok"})
|
||||
@Data
|
||||
@@ -38,7 +35,7 @@ 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, T e) {
|
||||
super(w,h,d);
|
||||
super(w, h, d);
|
||||
data = new DataContainer<>(writer, w * h * d, e);
|
||||
}
|
||||
|
||||
@@ -57,16 +54,12 @@ public class PaletteHunk<T> extends StorageHunk<T> implements Hunk<T> {
|
||||
|
||||
@Override
|
||||
public synchronized Hunk<T> iterateSync(Consumer4<Integer, Integer, Integer, T> c) {
|
||||
for(int i = 0; i < getWidth(); i++)
|
||||
{
|
||||
for(int j = 0; j < getHeight(); j++)
|
||||
{
|
||||
for(int k = 0; k < getDepth(); k++)
|
||||
{
|
||||
T t = getRaw(i,j,k);
|
||||
if(t != null)
|
||||
{
|
||||
c.accept(i,j,k,t);
|
||||
for (int i = 0; i < getWidth(); i++) {
|
||||
for (int j = 0; j < getHeight(); j++) {
|
||||
for (int k = 0; k < getDepth(); k++) {
|
||||
T t = getRaw(i, j, k);
|
||||
if (t != null) {
|
||||
c.accept(i, j, k, t);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -76,16 +69,12 @@ public class PaletteHunk<T> extends StorageHunk<T> implements Hunk<T> {
|
||||
|
||||
@Override
|
||||
public synchronized Hunk<T> iterateSyncIO(Consumer4IO<Integer, Integer, Integer, T> c) throws IOException {
|
||||
for(int i = 0; i < getWidth(); i++)
|
||||
{
|
||||
for(int j = 0; j < getHeight(); j++)
|
||||
{
|
||||
for(int k = 0; k < getDepth(); k++)
|
||||
{
|
||||
T t = getRaw(i,j,k);
|
||||
if(t != null)
|
||||
{
|
||||
c.accept(i,j,k,t);
|
||||
for (int i = 0; i < getWidth(); i++) {
|
||||
for (int j = 0; j < getHeight(); j++) {
|
||||
for (int k = 0; k < getDepth(); k++) {
|
||||
T t = getRaw(i, j, k);
|
||||
if (t != null) {
|
||||
c.accept(i, j, k, t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
|
||||
package com.volmit.iris.util.hunk.storage;
|
||||
|
||||
import com.volmit.iris.util.data.palette.PalettedContainer;
|
||||
import com.volmit.iris.util.function.Consumer4;
|
||||
import com.volmit.iris.util.function.Consumer4IO;
|
||||
import com.volmit.iris.util.hunk.Hunk;
|
||||
@@ -26,41 +25,38 @@ import com.volmit.iris.util.hunk.bits.DataContainer;
|
||||
import com.volmit.iris.util.hunk.bits.Writable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
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, T e) {
|
||||
super(width, height, depth);
|
||||
hunk = (allow) ? new PaletteHunk<>(width, height, depth, this, e) : factory.get();
|
||||
}
|
||||
|
||||
public DataContainer<T> palette()
|
||||
{
|
||||
return isPalette() ? ((PaletteHunk<T>)hunk).getData() : null;
|
||||
public DataContainer<T> palette() {
|
||||
return isPalette() ? ((PaletteHunk<T>) hunk).getData() : null;
|
||||
}
|
||||
|
||||
public void setPalette(DataContainer<T> c) {
|
||||
if(isPalette())
|
||||
{
|
||||
((PaletteHunk<T>)hunk).setPalette(c);
|
||||
if (isPalette()) {
|
||||
((PaletteHunk<T>) hunk).setPalette(c);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isPalette()
|
||||
{
|
||||
public boolean isPalette() {
|
||||
return hunk instanceof PaletteHunk;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRaw(int x, int y, int z, T t) {
|
||||
hunk.setRaw(x,y,z,t);
|
||||
hunk.setRaw(x, y, z, t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getRaw(int x, int y, int z) {
|
||||
return hunk.getRaw(x,y,z);
|
||||
return hunk.getRaw(x, y, z);
|
||||
}
|
||||
|
||||
public int getEntryCount() {
|
||||
|
||||
Reference in New Issue
Block a user