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

View File

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

View File

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

@ -53,7 +53,7 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION
}
return -1;
}
@Override
public int lastIndexOf(Object o) {
LIST_ITERATOR KEY_GENERIC_TYPE iter = listIterator(size());
@ -106,7 +106,7 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION
public ITERATOR KEY_GENERIC_TYPE iterator() {
return listIterator(0);
}
@Override
public LIST_ITERATOR KEY_GENERIC_TYPE listIterator() {
return listIterator(0);
@ -117,6 +117,12 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION
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 {
ABSTRACT_LIST KEY_GENERIC_TYPE l;
int offset;

View File

@ -5,19 +5,38 @@ import java.util.Collection;
import java.util.Iterator;
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.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;
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;
protected transient KEY_TYPE[] data;
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
data = (KEY_TYPE[])new Object[size];
#else
@ -25,6 +44,49 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
#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
public void add(int index, CLASS_TYPE element) {
checkAddRange(index);
@ -155,7 +217,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
checkRange(index);
return data[index];
}
@Override
public KEY_TYPE set(int index, KEY_TYPE e) {
checkRange(index);
@ -163,7 +225,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
data[index] = e;
return old;
}
@Override
public KEY_TYPE REMOVE(int index) {
checkRange(index);
@ -181,6 +243,14 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
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
public void clear() {
#if TYPE_OBJECT
@ -189,6 +259,38 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
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)
{
if(capacity < data.length) return;

View File

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

View File

@ -1,5 +1,6 @@
package speiger.src.collections.PACKAGE.utils;
import java.util.Iterator;
import speiger.src.collections.PACKAGE.collections.ITERATOR;
public class ITERATORS
@ -12,6 +13,22 @@ public class ITERATORS
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) {
return unwrap(a, i, 0, a.length);
}