Data fixes for tec plates

This commit is contained in:
cyberpwn
2021-09-25 18:25:00 -04:00
parent b6d9eb3dcc
commit a80031c3c5
26 changed files with 184 additions and 71 deletions

View File

@@ -18,6 +18,7 @@
package com.volmit.iris.util.hunk.bits;
import com.volmit.iris.Iris;
import org.apache.commons.lang3.Validate;
import java.io.DataInputStream;

View File

@@ -18,6 +18,8 @@
package com.volmit.iris.util.hunk.bits;
import com.volmit.iris.Iris;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
@@ -25,9 +27,10 @@ import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;
public class DataContainer<T> {
protected static final int INITIAL_BITS = 3;
protected static final int INITIAL_BITS = 2;
protected static final int LINEAR_BITS_LIMIT = 5;
protected static final int LINEAR_INITIAL_LENGTH = (int) Math.pow(2, LINEAR_BITS_LIMIT) + 1;
protected static final int[] BIT = computeBitLimits();
@@ -37,13 +40,12 @@ 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) {
this.writer = writer;
this.length = 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 {
@@ -54,6 +56,16 @@ public class DataContainer<T> {
this.bits = new AtomicInteger(palette.get().bits());
}
public DataBits getData()
{
return data.get();
}
public Palette<T> getPalette()
{
return palette.get();
}
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();
@@ -79,7 +91,7 @@ public class DataContainer<T> {
private Palette<T> newPalette(DataInputStream din) throws IOException {
int paletteSize = din.readInt();
Palette<T> d = newPalette(bits(paletteSize));
Palette<T> d = newPalette(bits(paletteSize+1));
d.from(paletteSize, writer, din);
return d;
}
@@ -105,24 +117,36 @@ public class DataContainer<T> {
}
public void set(int position, T t) {
int id = palette.get().id(t);
synchronized (this)
{
int id = palette.get().id(t);
if (id == -1) {
checkBits();
id = palette.get().add(t);
if (id == -1) {
expandOne();
id = palette.get().add(t);
}
data.get().set(position, id);
}
}
data.get().set(position, id);
private void expandOne() {
if (palette.get().size()+1 >= BIT[bits.get()]) {
setBits(bits.get() + 1);
}
}
public T get(int position) {
int id = data.get().get(position) + 1;
synchronized (this)
{
int id = data.get().get(position) + 1;
if (id <= 0) {
return null;
if (id <= 0) {
return null;
}
return palette.get().get(id - 1);
}
return palette.get().get(id - 1);
}
public void setBits(int bits) {
@@ -159,4 +183,8 @@ public class DataContainer<T> {
return DataContainer.BIT.length - 1;
}
public int size() {
return getData().getSize();
}
}

View File

@@ -33,6 +33,7 @@ public class HashPalette<T> implements Palette<T> {
this.size = new AtomicInteger(0);
this.palette = new LinkedHashMap<>();
this.lookup = new KMap<>();
add(null);
}
@Override
@@ -58,18 +59,28 @@ public class HashPalette<T> implements Palette<T> {
@Override
public int id(T t) {
if(t == null)
{
return 0;
}
Integer v = palette.get(t);
return v != null ? v : -1;
}
@Override
public int size() {
return size.get();
return size.get() - 1;
}
@Override
public void iterate(Consumer2<T, Integer> c) {
for (T i : palette.keySet()) {
if(i == null)
{
continue;
}
c.accept(i, id(i));
}
}

View File

@@ -31,6 +31,7 @@ public class LinearPalette<T> implements Palette<T> {
public LinearPalette(int initialSize) {
this.size = new AtomicInteger(0);
this.palette = new AtomicReference<>(new AtomicReferenceArray<>(initialSize));
palette.get().set(size.getAndIncrement(), null);
}
@Override
@@ -64,12 +65,13 @@ 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) {
return i;
}
if(t == null)
{
return 0;
}
if (t != null && t.equals(palette.get().get(i))) {
for (int i = 0; i < size(); i++) {
if (t.equals(palette.get().get(i))) {
return i;
}
}
@@ -79,12 +81,12 @@ public class LinearPalette<T> implements Palette<T> {
@Override
public int size() {
return size.get();
return size.get()-1;
}
@Override
public void iterate(Consumer2<T, Integer> c) {
for (int i = 0; i < size(); i++) {
for (int i = 1; i < size()+1; i++) {
c.accept(palette.get().get(i), i);
}
}

View File

@@ -35,7 +35,7 @@ public interface Palette<T> {
int size();
default int bits() {
return DataContainer.bits(size());
return DataContainer.bits(size()+1);
}
void iterate(Consumer2<T, Integer> c);

View File

@@ -0,0 +1,53 @@
/*
* Iris is a World Generator for Minecraft Bukkit Servers
* Copyright (c) 2021 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.volmit.iris.util.hunk.bits;
import com.volmit.iris.Iris;
import com.volmit.iris.engine.data.cache.Cache;
import com.volmit.iris.util.io.IO;
import com.volmit.iris.util.mantle.Mantle;
import com.volmit.iris.util.math.RNG;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
public class TecTest {
public static void go()
{
Mantle m = new Mantle(new File("dummy"), 256);
int size = 255;
int mx = (int) Math.pow(size, 3);
for(int i = 0; i < mx; i++)
{
int[] p = Cache.to3D(i, size, size);
m.set(p[0], p[1], p[2], RNG.r.s(1));
}
m.close();
m = new Mantle(new File("dummy"), 256);
m.get(0,0,0, String.class);
}
}

View File

@@ -27,6 +27,7 @@ import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.IOException;
import java.util.concurrent.locks.ReentrantLock;
@SuppressWarnings({"DefaultAnnotationParam", "Lombok"})
@Data
@@ -34,16 +35,15 @@ import java.io.IOException;
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) {
public PaletteHunk(int w, int h, int d, Writable<T> writer) {
super(w, h, d);
data = new DataContainer<>(writer, w * h * d, e);
data = new DataContainer<>(writer, w * h * d);
}
public void setPalette(DataContainer<T> c) {
data = c;
}
public boolean isMapped() {
return false;
}

View File

@@ -30,9 +30,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, T e) {
public PaletteOrHunk(int width, int height, int depth, boolean allow, Supplier<Hunk<T>> factory) {
super(width, height, depth);
hunk = (allow) ? new PaletteHunk<>(width, height, depth, this, e) : factory.get();
hunk = (allow) ? new PaletteHunk<>(width, height, depth, this) : factory.get();
}
public DataContainer<T> palette() {