Finished ArrayList

-Finished: Missing Methods to ArrayList
-Added: Stack.class
-Added: Trimmable interface
This commit is contained in:
Speiger 2020-11-28 15:05:28 +01:00
parent 737c87daca
commit 0af3266f02
11 changed files with 231 additions and 18 deletions

View File

@ -2,29 +2,31 @@ package speiger.src.builder.example;
public enum ClassType public enum ClassType
{ {
BOOLEAN("boolean", "Boolean", "Boolean", "booleans", "BOOLEAN"), BOOLEAN("boolean", "Boolean", "Boolean", "booleans", "BOOLEAN", "false"),
BYTE("byte", "Byte", "Byte", "bytes", "BYTES"), BYTE("byte", "Byte", "Byte", "bytes", "BYTES", "(byte)0"),
SHORT("short", "Short", "Short", "shorts", "SHORT"), SHORT("short", "Short", "Short", "shorts", "SHORT", "(short)0"),
CHAR("char", "Character", "Char", "chars", "CHAR"), CHAR("char", "Character", "Char", "chars", "CHAR", "(char)0"),
INT("int", "Integer", "Int", "ints", "INT"), INT("int", "Integer", "Int", "ints", "INT", "0"),
LONG("long", "Long", "Long", "longs", "LONG"), LONG("long", "Long", "Long", "longs", "LONG", "0L"),
FLOAT("float", "Float", "Float", "floats", "FLOAT"), FLOAT("float", "Float", "Float", "floats", "FLOAT", "0F"),
DOUBLE("double", "Double", "Double", "doubles", "DOUBLE"), DOUBLE("double", "Double", "Double", "doubles", "DOUBLE", "0D"),
OBJECT("T", "T", "Object", "objects", "OBJECT"); OBJECT("T", "T", "Object", "objects", "OBJECT", "null");
String keyType; String keyType;
String classType; String classType;
String fileType; String fileType;
String pathType; String pathType;
String capType; String capType;
String emptyValue;
private ClassType(String keyType, String classType, String fileType, String pathType, String capType) private ClassType(String keyType, String classType, String fileType, String pathType, String capType, String emptyValue)
{ {
this.keyType = keyType; this.keyType = keyType;
this.classType = classType; this.classType = classType;
this.fileType = fileType; this.fileType = fileType;
this.pathType = pathType; this.pathType = pathType;
this.capType = capType; this.capType = capType;
this.emptyValue = emptyValue;
} }
public String getKeyType() public String getKeyType()
@ -57,6 +59,11 @@ public enum ClassType
return capType; return capType;
} }
public String getEmptyValue()
{
return emptyValue;
}
public boolean isPrimitiveBlocking() public boolean isPrimitiveBlocking()
{ {
return this == BOOLEAN || this == OBJECT; return this == BOOLEAN || this == OBJECT;

View File

@ -28,6 +28,7 @@ public class GlobalVariables
addSimpleMapper("PACKAGE", type.getPathType()); addSimpleMapper("PACKAGE", type.getPathType());
addSimpleMapper("CLASS_TYPE", type.getClassType()); addSimpleMapper("CLASS_TYPE", type.getClassType());
addSimpleMapper("KEY_TYPE", type.getKeyType()); addSimpleMapper("KEY_TYPE", type.getKeyType());
addSimpleMapper("EMPTY_VALUE", type.getEmptyValue());
addSimpleMapper(" KEY_GENERIC_TYPE", type == ClassType.OBJECT ? "<"+type.getKeyType()+">" : ""); addSimpleMapper(" KEY_GENERIC_TYPE", type == ClassType.OBJECT ? "<"+type.getKeyType()+">" : "");
addSimpleMapper(" GENERIC_BRACES", type == ClassType.OBJECT ? " <"+type.getKeyType()+">" : ""); addSimpleMapper(" GENERIC_BRACES", type == ClassType.OBJECT ? " <"+type.getKeyType()+">" : "");
addSimpleMapper("BRACES", type == ClassType.OBJECT ? "<>" : ""); addSimpleMapper("BRACES", type == ClassType.OBJECT ? "<>" : "");
@ -67,6 +68,7 @@ public class GlobalVariables
addClassMapper("SUB_LIST", "SubList"); addClassMapper("SUB_LIST", "SubList");
addClassMapper("ARRAY_LIST", "ArrayList"); addClassMapper("ARRAY_LIST", "ArrayList");
addClassMapper("LIST", "List"); addClassMapper("LIST", "List");
addClassMapper("STACK", "Stack");
return this; return this;
} }
@ -78,6 +80,10 @@ public class GlobalVariables
addFunctionMapper("REMOVE_KEY", "rem"); addFunctionMapper("REMOVE_KEY", "rem");
addFunctionMapper("REMOVE", "remove"); addFunctionMapper("REMOVE", "remove");
addFunctionMapper("PREVIOUS", "previous"); addFunctionMapper("PREVIOUS", "previous");
addFunctionMapper("PEEK", "peek");
addFunctionMapper("POP", "pop");
addFunctionMapper("PUSH", "push");
addFunctionMapper("TOP", "top");
return this; return this;
} }

View File

@ -17,7 +17,6 @@ public class TestBuilder extends TemplateProcessor
{ {
Map<String, EnumSet<ClassType>> blocked = new HashMap<String, EnumSet<ClassType>>(); Map<String, EnumSet<ClassType>> blocked = new HashMap<String, EnumSet<ClassType>>();
public static final ClassType[] TYPE = ClassType.values(); public static final ClassType[] TYPE = ClassType.values();
List<GlobalVariables> varibles = new ArrayList<GlobalVariables>(); List<GlobalVariables> varibles = new ArrayList<GlobalVariables>();
public TestBuilder() public TestBuilder()
@ -52,6 +51,7 @@ public class TestBuilder extends TemplateProcessor
varibles.add(type); varibles.add(type);
} }
blocked.put("Consumer", EnumSet.of(ClassType.OBJECT)); blocked.put("Consumer", EnumSet.of(ClassType.OBJECT));
blocked.put("Stack", EnumSet.of(ClassType.OBJECT));
} }
@Override @Override

View File

@ -0,0 +1,8 @@
package speiger.src.collections.utils;
import java.util.RandomAccess;
public interface IArray extends RandomAccess, ITrimmable
{
public void ensureCapacity(int size);
}

View File

@ -0,0 +1,10 @@
package speiger.src.collections.utils;
public interface ITrimmable
{
public default void trim() {
trim(0);
}
public void trim(int size);
}

View File

@ -0,0 +1,18 @@
package speiger.src.collections.utils;
public interface Stack<T>
{
public void push(T e);
public T pop();
public int size();
public void clear();
public default T top() {
return peek(0);
}
public T peek(int index);
}

View File

@ -0,0 +1,37 @@
package speiger.src.collections.PACKAGE.collections;
import speiger.src.collections.utils.Stack;
public interface STACK extends Stack<CLASS_TYPE>
{
public void PUSH(KEY_TYPE e);
public KEY_TYPE POP();
public default KEY_TYPE TOP() {
return PEEK(0);
}
public KEY_TYPE PEEK(int index);
#if !OBJECT_TYPE
@Deprecated
public default void push(CLASS_TYPE e) { PUSH(OBJ_TO_KEY(e)); }
@Deprecated
public default CLASS_TYPE pop() { return KEY_TO_OBJ(POP()); }
@Deprecated
public default CLASS_TYPE top() {
return peek(size() - 1);
}
@Deprecated
public default CLASS_TYPE bottom() {
return peek(0);
}
@Deprecated
public default CLASS_TYPE peek(int index) { return KEY_TO_OBJ(PEEK(index)); }
#endif
}

View File

@ -117,6 +117,12 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION
return new LIST_ITER(index); return new LIST_ITER(index);
} }
@Override
public void size(int size) {
while(size > size()) add(EMPTY_VALUE);
while(size < size()) REMOVE(size() - 1);
}
private class SUB_LIST extends ABSTRACT_LIST KEY_GENERIC_TYPE { private class SUB_LIST extends ABSTRACT_LIST KEY_GENERIC_TYPE {
ABSTRACT_LIST KEY_GENERIC_TYPE l; ABSTRACT_LIST KEY_GENERIC_TYPE l;
int offset; int offset;

View File

@ -5,19 +5,38 @@ import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import speiger.src.collections.PACKAGE.collections.COLLECTION; import speiger.src.collections.PACKAGE.collections.COLLECTION;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.collections.STACK;
#endif
import speiger.src.collections.PACKAGE.collections.ITERATOR; import speiger.src.collections.PACKAGE.collections.ITERATOR;
import speiger.src.collections.PACKAGE.utils.ARRAYS; import speiger.src.collections.PACKAGE.utils.ARRAYS;
import speiger.src.collections.PACKAGE.utils.ITERATORS;
#if TYPE_OBJECT
import speiger.src.collections.utils.Stack;
#endif
import speiger.src.collections.utils.IArray;
import speiger.src.collections.utils.SanityChecks; import speiger.src.collections.utils.SanityChecks;
public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE #if TYPE_OBJECT
public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE implements IArray, Stack<KEY_TYPE>
#else
public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE implements IArray, STACK
#endif
{ {
static final int DEFAULT_ARRAY_SIZE = 10; static final int DEFAULT_ARRAY_SIZE = 10;
protected transient KEY_TYPE[] data; protected transient KEY_TYPE[] data;
protected int size = 0; protected int size = 0;
public ARRAY_LIST(int size) public ARRAY_LIST() {
{ #if TYPE_OBJECT
data = (KEY_TYPE[])ARRAYS.EMPTY_ARRAY;
#else
data = ARRAYS.EMPTY_ARRAY;
#endif
}
public ARRAY_LIST(int size) {
#if TYPE_OBJECT #if TYPE_OBJECT
data = (KEY_TYPE[])new Object[size]; data = (KEY_TYPE[])new Object[size];
#else #else
@ -25,6 +44,49 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
#endif #endif
} }
public ARRAY_LIST(Collection<? extends CLASS_TYPE> c) {
this(c.size());
size = ITERATORS.unwrap(data, c.iterator());
}
public ARRAY_LIST(COLLECTION KEY_GENERIC_TYPE c) {
this(c.size());
size = ITERATORS.unwrap(data, c.iterator());
}
public ARRAY_LIST(LIST KEY_GENERIC_TYPE l) {
this(l.size());
size = l.size();
l.getElements(0, data, 0, size);
}
public ARRAY_LIST(KEY_TYPE[] a) {
this(a, 0, a.length);
}
public ARRAY_LIST(KEY_TYPE[] a, int length) {
this(a, 0, length);
}
public ARRAY_LIST(KEY_TYPE[] a, int offset, int length) {
this(length);
SanityChecks.checkArrayCapacity(a.length, offset, length);
System.arraycopy(a, offset, data, 0, length);
size = length;
}
public static GENERIC_BRACES ARRAY_LIST KEY_GENERIC_TYPE wrap(KEY_TYPE[] a) {
return wrap(a, a.length);
}
public static GENERIC_BRACES ARRAY_LIST KEY_GENERIC_TYPE wrap(KEY_TYPE[] a, int length) {
SanityChecks.checkArrayCapacity(a.length, 0, length);
ARRAY_LIST KEY_GENERIC_TYPE list = new ARRAY_LISTBRACES();
list.data = a;
list.size = length;
return list;
}
@Override @Override
public void add(int index, CLASS_TYPE element) { public void add(int index, CLASS_TYPE element) {
checkAddRange(index); checkAddRange(index);
@ -181,6 +243,14 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
return size; return size;
} }
@Override
public void size(int size) {
if(size > data.length)
data = Arrays.copyOf(data, size);
else if(size < size() && size >= 0)
Arrays.fill(data, size, size(), EMPTY_VALUE);
}
@Override @Override
public void clear() { public void clear() {
#if TYPE_OBJECT #if TYPE_OBJECT
@ -189,6 +259,38 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
size = 0; size = 0;
} }
@Override
public void trim(int size) {
if(size > size() || size() == data.length) return;
int value = Math.min(size, size());
#if TYPE_OBJECT
data = value == 0 ? (KEY_TYPE[])ARRAYS.EMPTY_ARRAY : Arrays.copyOf(data, value);
#else
data = value == 0 ? ARRAYS.EMPTY_ARRAY : Arrays.copyOf(data, value);
#endif
}
@Override
public void ensureCapacity(int size) {
grow(size);
}
@Override
public void PUSH(KEY_TYPE e) {
add(e);
}
@Override
public KEY_TYPE POP() {
return REMOVE(size() - 1);
}
@Override
public KEY_TYPE PEEK(int index) {
checkRange((size() - 1) - index);
return data[(size() - 1) - index];
}
protected void grow(int capacity) protected void grow(int capacity)
{ {
if(capacity < data.length) return; if(capacity < data.length) return;

View File

@ -56,6 +56,8 @@ public interface LIST KEY_GENERIC_TYPE extends COLLECTION KEY_GENERIC_TYPE, List
@Override @Override
public LIST KEY_GENERIC_TYPE subList(int from, int to); public LIST KEY_GENERIC_TYPE subList(int from, int to);
public void size(int size);
#if !TYPE_OBJECT #if !TYPE_OBJECT
@Override @Override
@Deprecated @Deprecated

View File

@ -1,5 +1,6 @@
package speiger.src.collections.PACKAGE.utils; package speiger.src.collections.PACKAGE.utils;
import java.util.Iterator;
import speiger.src.collections.PACKAGE.collections.ITERATOR; import speiger.src.collections.PACKAGE.collections.ITERATOR;
public class ITERATORS public class ITERATORS
@ -12,6 +13,22 @@ public class ITERATORS
return new ArrayIteratorBRACES(a, start, end); return new ArrayIteratorBRACES(a, start, end);
} }
public static GENERIC_BRACES int unwrap(KEY_TYPE[] a, Iterator<? extends CLASS_TYPE> i) {
return unwrap(a, i, 0, a.length);
}
public static GENERIC_BRACES int unwrap(KEY_TYPE[] a, Iterator<? extends CLASS_TYPE> i, int offset) {
return unwrap(a, i, offset, a.length);
}
public static GENERIC_BRACES int unwrap(KEY_TYPE[] a, Iterator<? extends CLASS_TYPE> i, int offset, int max) {
if(max < 0) throw new IllegalStateException("The max size is smaller then 0");
if(offset + max >= a.length) throw new IllegalStateException("largest array index exceeds array size");
int index = 0;
for(;index<max && i.hasNext();index++) a[index+offset] = OBJ_TO_KEY(i.next());
return index;
}
public static GENERIC_BRACES int unwrap(KEY_TYPE[] a, ITERATOR KEY_GENERIC_TYPE i) { public static GENERIC_BRACES int unwrap(KEY_TYPE[] a, ITERATOR KEY_GENERIC_TYPE i) {
return unwrap(a, i, 0, a.length); return unwrap(a, i, 0, a.length);
} }