Upgraded engine and moved PrettyJsonWriter into the engine

This commit is contained in:
Speiger 2021-09-29 14:12:14 +02:00
parent 4763e62b53
commit bb5ddf3142
5 changed files with 305 additions and 4 deletions

View File

@ -64,5 +64,5 @@ dependencies {
compile 'com.google.code.gson:gson:2.8.6'
//Primitive Collections
compile 'de.speiger:Primitive-Collections:0.4.0'
compile 'de.speiger:Primitive-Collections:0.4.1'
}

View File

@ -82,4 +82,14 @@ public class GLDataType
{
return ignoreAttributeSize ? dataType.getByteSize() : attributeSize * dataType.getByteSize();
}
public static GLDataType byName(String name)
{
return NAME_TO_TYPE.get(name);
}
public static GLDataType byID(int id)
{
return ID_TO_TYPE.get(id);
}
}

View File

@ -8,6 +8,7 @@ import speiger.src.collections.ints.maps.impl.hash.Int2ObjectLinkedOpenHashMap;
import speiger.src.collections.ints.maps.impl.hash.Int2ObjectOpenHashMap;
import speiger.src.collections.ints.maps.interfaces.Int2ObjectMap;
import speiger.src.collections.objects.lists.ObjectArrayList;
import speiger.src.collections.objects.lists.ObjectList;
import speiger.src.collections.objects.sets.ObjectLinkedOpenHashSet;
import speiger.src.collections.objects.sets.ObjectOpenHashSet;
@ -32,9 +33,9 @@ public class CollectionUtils
return sets;
}
public static <T> List<T>[] createList(int size)
public static <T> ObjectList<T>[] createList(int size)
{
List<T>[] list = new List[size];
ObjectList<T>[] list = new ObjectList[size];
for(int i = 0;i<size;i++)
{
list[i] = new ObjectArrayList<>();

View File

@ -8,6 +8,7 @@ import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
@ -15,6 +16,7 @@ import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
public class JsonUtil
{
@ -83,6 +85,19 @@ public class JsonUtil
}
}
public static void iterateValues(JsonElement element, Consumer<JsonPrimitive> result)
{
if(element.isJsonPrimitive()) result.accept(element.getAsJsonPrimitive());
else if(element.isJsonArray())
{
JsonArray array = element.getAsJsonArray();
for(int i = 0,m=array.size();i<m;i++)
{
iterateValues(array.get(i), result);
}
}
}
public static boolean getOrDefault(JsonObject obj, String name, boolean defaultValue)
{
JsonElement el = obj.get(name);
@ -142,6 +157,36 @@ public class JsonUtil
return array;
}
public static JsonArray toArray(short[] values)
{
JsonArray array = new JsonArray();
for(int i = 0,m=values.length;i<m;i++)
{
array.add(values[i]);
}
return array;
}
public static JsonArray toArray(int[] values)
{
JsonArray array = new JsonArray();
for(int i = 0,m=values.length;i<m;i++)
{
array.add(values[i]);
}
return array;
}
public static JsonArray toArray(long[] values)
{
JsonArray array = new JsonArray();
for(int i = 0,m=values.length;i<m;i++)
{
array.add(values[i]);
}
return array;
}
public static JsonArray toArray(float[] values)
{
JsonArray array = new JsonArray();
@ -152,6 +197,16 @@ public class JsonUtil
return array;
}
public static JsonArray toArray(double[] values)
{
JsonArray array = new JsonArray();
for(int i = 0,m=values.length;i<m;i++)
{
array.add(values[i]);
}
return array;
}
public static JsonArray toArray(Number[] values)
{
JsonArray array = new JsonArray();
@ -161,7 +216,26 @@ public class JsonUtil
}
return array;
}
public static JsonArray toArray(List<Number> values)
{
JsonArray array = new JsonArray();
for(int i = 0,m=values.size();i<m;i++)
{
array.add(values.get(i));
}
return array;
}
public static Number[] parseArray(JsonArray array)
{
Number[] data = new Number[array.size()];
for(int i = 0,m=data.length;i<m;i++)
{
data[i] = array.get(i).getAsNumber();
}
return data;
}
public static byte[] parseByteArray(JsonArray array)
{
@ -173,6 +247,36 @@ public class JsonUtil
return data;
}
public static short[] parseShortArray(JsonArray array)
{
short[] data = new short[array.size()];
for(int i = 0,m=data.length;i<m;i++)
{
data[i] = array.get(i).getAsByte();
}
return data;
}
public static int[] parseIntArray(JsonArray array)
{
int[] data = new int[array.size()];
for(int i = 0,m=data.length;i<m;i++)
{
data[i] = array.get(i).getAsByte();
}
return data;
}
public static long[] parseLongArray(JsonArray array)
{
long[] data = new long[array.size()];
for(int i = 0,m=data.length;i<m;i++)
{
data[i] = array.get(i).getAsByte();
}
return data;
}
public static float[] parseFloatArray(JsonArray array)
{
float[] data = new float[array.size()];
@ -182,4 +286,14 @@ public class JsonUtil
}
return data;
}
public static double[] parseDoubleArray(JsonArray array)
{
double[] data = new double[array.size()];
for(int i = 0,m=data.length;i<m;i++)
{
data[i] = array.get(i).getAsFloat();
}
return data;
}
}

View File

@ -0,0 +1,176 @@
package speiger.src.coreengine.utils.io;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import com.google.gson.stream.JsonWriter;
import speiger.src.collections.bytes.collections.ByteStack;
import speiger.src.collections.bytes.lists.ByteArrayList;
public class PrettyGsonWriter extends JsonWriter
{
String indent;
ByteStack blocked = ByteArrayList.wrap((byte)0);
int[] usedArrays = new int[32];
int arrayPointer = 0;
String[] singleNames = new String[32];
int namePointer = 0;
String nextArray = null;
Set<String> singleLineObjects = new HashSet<>();
public PrettyGsonWriter(Writer out)
{
this(new Overrider(out));
}
public PrettyGsonWriter(Writer out, String indent)
{
this(new Overrider(out));
setTabs(indent);
}
private PrettyGsonWriter(Overrider rider)
{
super(rider);
rider.owner = this;
}
public PrettyGsonWriter addSinlgeLines(String... names)
{
singleLineObjects.addAll(Arrays.asList(names));
return this;
}
public void setTabs(String key)
{
setIndent(key);
if(key.length() == 0) indent = null;
else indent = key;
}
private void push(boolean value, boolean object)
{
if(indent == null) return;
if(value)
{
if(++arrayPointer >= usedArrays.length) usedArrays = Arrays.copyOf(usedArrays, usedArrays.length * 2);
if(!object && ++namePointer >= singleNames.length) singleNames = Arrays.copyOf(singleNames, singleNames.length * 2);
}
else if((blocked.top() & 1) != 0) usedArrays[arrayPointer]++;
blocked.push((byte)((value ? 1 : 0) | (value && !object ? 2 : 0)));
}
private void pop()
{
if(indent == null) return;
byte value = blocked.pop();
if((value & 1) != 0) usedArrays[arrayPointer--] = 0;
if((value & 2) != 0) singleNames[namePointer--] = null;
}
@Override
public JsonWriter name(String name) throws IOException
{
if(singleLineObjects.contains(name)) nextArray = name;
return super.name(name);
}
@Override
public JsonWriter beginArray() throws IOException
{
super.beginArray();
push(true, false);
singleNames[namePointer] = nextArray;
nextArray = null;
return this;
}
@Override
public JsonWriter beginObject() throws IOException
{
boolean shouldRework = shouldBlockObject();
push(false, true);
super.beginObject();
if(shouldRework) push(shouldRework, true);
return this;
}
@Override
public JsonWriter endArray() throws IOException
{
boolean ignore = usedArrays[arrayPointer] > 0;
if(ignore) pop();
super.endArray();
if(!ignore) pop();
return this;
}
@Override
public JsonWriter endObject() throws IOException
{
super.endObject();
if(shouldBlockObject()) pop();
pop();
return this;
}
public boolean shouldBlockObject()
{
return singleNames[namePointer] != null;
}
public boolean isBlocked()
{
return (blocked.top() & 1) != 0;
}
public static class Overrider extends BufferedWriter
{
PrettyGsonWriter owner;
public Overrider(Writer out)
{
super(out);
}
@Override
public void write(int c) throws IOException
{
if(owner.indent != null && owner.isBlocked())
{
if(c == '\n') return;
else if(c == ',' && owner.shouldBlockObject())
{
super.write(", ");
return;
}
}
super.write(c);
}
@Override
public Writer append(char c) throws IOException
{
if(owner.indent != null && c == ',' && owner.isBlocked() && (owner.usedArrays[owner.arrayPointer] <= 0))
{
super.write(", ");
return this;
}
return super.append(c);
}
@Override
public void write(String str) throws IOException
{
if(owner.indent != null && owner.isBlocked() && str.equals(owner.indent)) return;
super.write(str);
}
}
}