Finished ArrayList
-Finished: Missing Methods to ArrayList -Added: Stack.class -Added: Trimmable interface
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user