Primitive-Collections/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template

920 lines
27 KiB
Plaintext

package speiger.src.collections.PACKAGE.lists;
import java.util.Arrays;
#if TYPE_OBJECT
import java.util.Comparator;
#endif
import java.util.Collection;
import java.util.Iterator;
import java.util.Objects;
#if TYPE_OBJECT
import java.util.function.Consumer;
#endif
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
#if PRIMITIVES
import java.util.function.JAVA_PREDICATE;
import java.util.function.JAVA_UNARY_OPERATOR;
#endif
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;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif
import speiger.src.collections.PACKAGE.utils.ARRAYS;
import speiger.src.collections.PACKAGE.utils.ITERATORS;
#if TYPE_OBJECT
import speiger.src.collections.utils.Stack;
#else
import speiger.src.collections.objects.utils.ObjectArrays;
#endif
import speiger.src.collections.PACKAGE.utils.IARRAY;
import speiger.src.collections.utils.SanityChecks;
#if TYPE_OBJECT
/**
* A Type-Specific Array-based implementation of list that is written to reduce (un)boxing
*
* <p>This implementation is optimized to improve how data is processed with interfaces like {@link IARRAY}, {@link Stack}
* and with optimized functions that use type-specific implementations for primitives and optimized logic for bulkactions.
*/
public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE implements IARRAY<KEY_TYPE>, Stack<KEY_TYPE>
#else
/**
* A Type-Specific Array-based implementation of list that is written to reduce (un)boxing
*
* <p>This implementation is optimized to improve how data is processed with interfaces like {@link IARRAY}, {@link STACK}
* and with optimized functions that use type-specific implementations for primitives and optimized logic for bulkactions.
*/
public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE implements IARRAY, STACK
#endif
{
static final int DEFAULT_ARRAY_SIZE = 10;
/** The backing array */
protected transient KEY_TYPE[] data;
/** The current size of the elements stored in the backing array */
protected int size = 0;
/**
* Creates a new ArrayList with a Empty array.
*/
public ARRAY_LIST() {
data = EMPTY_KEY_ARRAY;
}
/**
* Creates a new ArrayList with the specific requested size
*/
public ARRAY_LIST(int size) {
data = NEW_KEY_ARRAY(size);
}
/**
* Creates a new ArrayList a copy with the contents of the Collection.
*/
public ARRAY_LIST(Collection<? extends CLASS_TYPE> c) {
this(c.size());
size = ITERATORS.unwrap(data, c.iterator());
}
/**
* Creates a new ArrayList a copy with the contents of the Collection.
*/
public ARRAY_LIST(COLLECTION KEY_GENERIC_TYPE c) {
this(c.size());
size = ITERATORS.unwrap(data, c.iterator());
}
/**
* Creates a new ArrayList a copy with the contents of the List.
*/
public ARRAY_LIST(LIST KEY_GENERIC_TYPE l) {
this(l.size());
size = l.size();
l.getElements(0, data, 0, size);
}
/**
* Creates a new ArrayList with a Copy of the array
*/
public ARRAY_LIST(KEY_TYPE[] a) {
this(a, 0, a.length);
}
/**
* Creates a new ArrayList with a Copy of the array with a custom length
*/
public ARRAY_LIST(KEY_TYPE[] a, int length) {
this(a, 0, length);
}
/**
* Creates a new ArrayList with a Copy of the array with in the custom range.
*/
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;
}
/**
* Creates a wrapped arraylist that uses the array as backing array
* @param a elements that should be wrapped
* @return a Wrapped list using the input array
*/
public static GENERIC_KEY_BRACES ARRAY_LIST KEY_GENERIC_TYPE wrap(KEY_TYPE... a) {
return wrap(a, a.length);
}
/**
* Creates a wrapped arraylist that uses the array as backing array and a custom fillsize
* @param a elements that should be wrapped
* @param length the size of the elements within the array
* @return a Wrapped list using the input array
*/
public static GENERIC_KEY_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;
}
#if TYPE_OBJECT
/**
* Creates a new ArrayList with a EmptyObject array of the Type requested
* @param c the type of the array
* @return a typed List
*/
public static GENERIC_KEY_BRACES ARRAY_LIST KEY_GENERIC_TYPE of(Class<KEY_TYPE> c) {
ARRAY_LIST KEY_GENERIC_TYPE list = new ARRAY_LISTBRACES();
list.data = (KEY_TYPE[])ObjectArrays.newArray(c.getClass().getComponentType(), 0);
return list;
}
#endif
/**
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return <tt>true</tt> (as specified by {@link Collection#add})
*/
@Override
public boolean add(KEY_TYPE e) {
grow(size + 1);
data[size++] = e;
return true;
}
/**
* Appends the specified element to the end of this Stack.
* @param e element to be appended to this Stack
*/
@Override
public void PUSH(KEY_TYPE e) {
add(e);
}
/**
* Appends the specified element to the index of the list
* @param index the index where to append the element to
* @param e the element to append to the list
* @throws IndexOutOfBoundsException if index is outside of the lists range
*/
@Override
public void add(int index, KEY_TYPE e) {
checkAddRange(index);
grow(size + 1);
if(index != size) System.arraycopy(data, index, data, index+1, size - index);
data[index] = e;
size++;
}
/**
* Appends the specified elements to the index of the list.
* This function may delegate to more appropiate function if nessesary
* @param index the index where to append the elements to
* @param c the elements to append to the list
* @throws IndexOutOfBoundsException if index is outside of the lists range
* @deprecated if type is primitive
*/
@Override
@Primitive
public boolean addAll(int index, Collection<? extends CLASS_TYPE> c) {
if(c instanceof COLLECTION) return addAll(index, (COLLECTION KEY_GENERIC_TYPE)c);
int add = c.size();
if(add <= 0) return false;
grow(size + add);
if(index != size) System.arraycopy(data, index, data, index+add, size - index);
size+=add;
Iterator<? extends CLASS_TYPE> iter = c.iterator();
while(add != 0) data[index++] = OBJ_TO_KEY(iter.next());
return true;
}
/**
* Appends the specified elements to the index of the list.
* This function may delegate to more appropiate function if nessesary
* @param index the index where to append the elements to
* @param c the elements to append to the list
* @throws IndexOutOfBoundsException if index is outside of the lists range
* @deprecated if type is primitive
*/
@Override
public boolean addAll(int index, COLLECTION KEY_GENERIC_TYPE c) {
if(c instanceof LIST) return addAll(index, (LIST KEY_GENERIC_TYPE)c);
int add = c.size();
if(add <= 0) return false;
grow(size + add);
if(index != size) System.arraycopy(data, index, data, index+add, size - index);
size+=add;
ITERATOR KEY_GENERIC_TYPE iter = c.iterator();
while(add-- != 0) data[index++] = iter.NEXT();
return true;
}
/**
* Appends the specified elements to the index of the list.
* @param index the index where to append the elements to
* @param c the elements to append to the list
* @throws IndexOutOfBoundsException if index is outside of the lists range
* @deprecated if type is primitive
*/
@Override
public boolean addAll(int index, LIST KEY_GENERIC_TYPE c) {
int add = c.size();
if(add <= 0) return false;
checkAddRange(index);
grow(size + add);
if(index != size) System.arraycopy(data, index, data, index+add, size - index);
size+=add;
c.getElements(0, data, index, c.size());
return true;
}
/**
* Appends the specified array elements to the index of the list.
* @param from the index where to append the elements to
* @param a the elements to append to the list
* @param offset where to start ino the array
* @param length the amount of elements to insert
* @throws IndexOutOfBoundsException if index is outside of the lists range
* @deprecated if type is primitive
*/
@Override
public void addElements(int from, KEY_TYPE[] a, int offset, int length) {
if(length <= 0) return;
checkAddRange(from);
grow(size + length);
if(from != size) System.arraycopy(data, from, data, from+length, size - length);
size+=length;
System.arraycopy(a, offset, data, from, length);
}
/**
* A function to fast fetch elements from the list
* @param from index where the list should be fetching elements from
* @param a the array where the values should be inserted to
* @param offset the startIndex of where the array should be written to
* @param length the number of elements the values should be fetched from
* @return the inputArray
* @throws NullPointerException if the array is null
* @throws IndexOutOfBoundsException if from is outside of the lists range
* @throws IllegalStateException if offset or length are smaller then 0 or exceed the array length
*/
@Override
public KEY_TYPE[] getElements(int from, KEY_TYPE[] a, int offset, int length) {
SanityChecks.checkArrayCapacity(size, offset, length);
System.arraycopy(data, from, a, offset, length);
return a;
}
/**
* a function to fast remove elements from the list.
* @param from the start index of where the elements should be removed from (inclusive)
* @param to the end index of where the elements should be removed to (exclusive)
*/
@Override
public void removeElements(int from, int to) {
checkRange(from);
checkAddRange(to);
int length = to - from;
if(length <= 0) return;
if(to != size) System.arraycopy(data, to, data, from, size - to);
#if TYPE_OBJECT
for(int i = 0;i<length;i++)
data[i+to] = null;
#endif
size -= length;
}
#if TYPE_OBJECT
/**
* A function to fast extract elements out of the list, this removes the elements that were fetched.
* @param from the start index of where the elements should be fetched from (inclusive)
* @param to the end index of where the elements should be fetched to (exclusive)
* @param type the type of the OutputArray
* @return a array of the elements that were fetched
*/
@Override
public <K> K[] extractElements(int from, int to, Class<K> type) {
checkRange(from);
checkAddRange(to);
int length = to - from;
K[] a = ARRAYS.newArray(type, length);
if(length <= 0) return a;
System.arraycopy(data, from, a, 0, length);
if(to != size) System.arraycopy(data, to, data, from, size - to);
for(int i = 0;i<length;i++)
data[i+to] = null;
size -= length;
return a;
}
#else
/**
* A function to fast extract elements out of the list, this removes the elements that were fetched.
* @param from the start index of where the elements should be fetched from (inclusive)
* @param to the end index of where the elements should be fetched to (exclusive)
* @return a array of the elements that were fetched
*/
@Override
public KEY_TYPE[] extractElements(int from, int to) {
int length = to - from;
if(length <= 0) return ARRAYS.EMPTY_ARRAY;
KEY_TYPE[] a = new KEY_TYPE[length];
System.arraycopy(data, from, a, 0, length);
if(to != size) System.arraycopy(data, to, data, from, size - to);
size -= length;
return a;
}
#endif
/**
* A function to find if the Element is present in this list.
* @param o the element that is searched for
* @return if the element was found.
* @deprecated if type-specific but still supported because of special edgecase Object-Comparason features
*/
@Override
@Primitive
public boolean contains(Object o) {
return indexOf(o) != -1;
}
/**
* A function to find the index of a given element
* @param o the element that is searched for
* @return the index of the element if found. (if not found then -1)
* @deprecated if type-specific but still supported because of special edgecase Object-Comparason features
*/
@Override
@Primitive
public int indexOf(Object o) {
#if TYPE_OBJECT
if(o == null) {
for(int i = 0;i<size;i++)
if(data[i] == null) return i;
return -1;
}
#else
if(o == null) return -1;
#endif
for(int i = 0;i<size;i++) {
if(EQUALS_KEY_TYPE(data[i], o)) return i;
}
return -1;
}
/**
* A function to find the last index of a given element
* @param o the element that is searched for
* @return the last index of the element if found. (if not found then -1)
* @deprecated if type-specific but still supported because of special edgecase Object-Comparason features
*/
@Override
@Primitive
public int lastIndexOf(Object o) {
#if TYPE_OBJECT
if(o == null) {
for(int i = size - 1;i>=0;i--)
if(data[i] == null) return i;
return -1;
}
#else
if(o == null) return -1;
#endif
for(int i = size - 1;i>=0;i--) {
if(EQUALS_KEY_TYPE(data[i], o)) return i;
}
return -1;
}
#if TYPE_OBJECT
/**
* Sorts the elements specified by the Natural order either by using the Comparator or the elements
* @param c the sorter of the elements, can be null
* @see List#sort(Comparator)
* @see ARRAYS#stableSort(KEY_TYPE[], Comparator)
*/
@Override
public void sort(Comparator<? super CLASS_TYPE> c) {
if(c != null) ARRAYS.stableSort(data, size, c);
else ARRAYS.stableSort((Comparable[])data, size);
}
/**
* Sorts the elements specified by the Natural order either by using the Comparator or the elements using a unstable sort
* @param c the sorter of the elements, can be null
* @see List#sort(Comparator)
* @see ARRAYS#unstableSort(KEY_TYPE[], Comparator)
*/
@Override
public void unstableSort(Comparator<? super CLASS_TYPE> c) {
if(c != null) ARRAYS.unstableSort(data, size, c);
else ARRAYS.unstableSort((Comparable[])data, size);
}
#else
/**
* A Type Specific implementation of the Collection#contains function.
* @param e the element that is searched for.
* @return if the element was found
*/
@Override
public boolean contains(KEY_TYPE e) {
return indexOf(e) != -1;
}
/**
* A Type-Specific function to find the index of a given element
* @param e the element that is searched for
* @return the index of the element if found. (if not found then -1)
*/
@Override
public int indexOf(KEY_TYPE e) {
for(int i = 0;i<size;i++) {
if(KEY_EQUALS(data[i], e)) return i;
}
return -1;
}
/**
* A Type-Specific function to find the last index of a given element
* @param e the element that is searched for
* @return the last index of the element if found. (if not found then -1)
*/
@Override
public int lastIndexOf(KEY_TYPE e) {
for(int i = size - 1;i>=0;i--) {
if(KEY_EQUALS(data[i], e)) return i;
}
return -1;
}
/**
* Sorts the elements specified by the Natural order either by using the Comparator or the elements
* @param c the sorter of the elements, can be null
* @see List#sort(Comparator)
* @see ARRAYS#stableSort(KEY_TYPE[], COMPARATOR)
*/
@Override
public void sort(COMPARATOR c) {
if(c != null) ARRAYS.stableSort(data, size, c);
else ARRAYS.stableSort(data, size);
}
/**
* Sorts the elements specified by the Natural order either by using the Comparator or the elements using a unstable sort
* @param c the sorter of the elements, can be null
* @see List#sort(Comparator)
* @see ARRAYS#unstableSort(KEY_TYPE[], COMPARATOR)
*/
@Override
public void unstableSort(COMPARATOR c) {
if(c != null) ARRAYS.unstableSort(data, size, c);
else ARRAYS.unstableSort(data, size);
}
#endif
/**
* A Type-Specific get function to reduce (un)boxing
* @param index the index of the element to fetch
* @return the value of the requested index
* @throws IndexOutOfBoundsException if the index is out of range
*/
@Override
public KEY_TYPE GET_KEY(int index) {
checkRange(index);
return data[index];
}
/**
* Provides the Selected Object from the stack.
* Top to bottom
* @param index of the element that should be provided
* @return the element that was requested
* @throws ArrayIndexOutOfBoundsException if the index is out of bounds
* @see Stack#peek(int)
*/
@Override
public KEY_TYPE PEEK(int index) {
checkRange((size() - 1) - index);
return data[(size() - 1) - index];
}
/**
* Provides the Underlying Array in the Implementation
* @return underlying Array
* @throws ClassCastException if the return type does not match the underlying array. (Only for Object Implementations)
*/
@Override
public KEY_TYPE[] elements() {
return data;
}
/**
* A Type Specific foreach function that reduces (un)boxing
*
* @implSpec
* <p>The default implementation behaves as if:
* <pre>{@code
* for(int i = 0;i<size;i++)
* action.accept(data[i]);
* }</pre>
*
* @param action The action to be performed for each element
* @throws java.util.NullPointerException if the specified action is null
* @see Iterable#forEach(Consumer)
*/
@Override
public void forEach(CONSUMER KEY_SUPER_GENERIC_TYPE action) {
Objects.requireNonNull(action);
for(int i = 0;i<size;i++)
action.accept(data[i]);
}
/**
* A Type-Specific set function to reduce (un)boxing
* @param index the index of the element to set
* @param e the value that should be set
* @return the previous element
* @throws IndexOutOfBoundsException if the index is out of range
*/
@Override
public KEY_TYPE set(int index, KEY_TYPE e) {
checkRange(index);
KEY_TYPE old = data[index];
data[index] = e;
return old;
}
/**
* A function to replace all values in the list
* @param o the action to replace the values
* @throws NullPointerException if o is null
*/
@Override
@Primitive
public void replaceAll(UnaryOperator<CLASS_TYPE> o) {
#if PRIMITIVES
Objects.requireNonNull(o);
#if TYPE_BYTE || TYPE_SHORT || TYPE_CHAR || TYPE_FLOAT
REPLACE(T -> OBJ_TO_KEY(o.apply(KEY_TO_OBJ(SanityChecks.SANITY_CAST(T)))));
#else
REPLACE(T -> OBJ_TO_KEY(o.apply(KEY_TO_OBJ(T))));
#endif
#else
Objects.requireNonNull(o);
for(int i = 0;i<size;i++)
data[i] = OBJ_TO_KEY(o.apply(KEY_TO_OBJ(data[i])));
#endif
}
#if PRIMITIVES
/**
* A Type-Specific replace function to reduce (un)boxing
* @param o the action to replace the values
* @throws NullPointerException if o is null
*/
@Override
public void REPLACE(JAVA_UNARY_OPERATOR o) {
for(int i = 0;i<size;i++)
#if TYPE_BYTE || TYPE_SHORT || TYPE_CHAR || TYPE_FLOAT
data[i] = SanityChecks.SANITY_CAST(o.APPLY_CAST(data[i]));
#else
data[i] = o.APPLY(data[i]);
#endif
}
#endif
/**
* A Type-Specific remove function to reduce (un)boxing
* @param index the index of the element to fetch
* @return the value of the requested index
* @throws IndexOutOfBoundsException if the index is out of range
*/
@Override
public KEY_TYPE REMOVE(int index) {
checkRange(index);
KEY_TYPE old = data[index];
size--;
if(index != size) System.arraycopy(data, index+1, data, index, size - index);
#if TYPE_OBJECT
data[size] = null;
#endif
return old;
}
#if !TYPE_OBJECT
/**
* A Type-Specific implementation of remove. This implementation iterates over the elements until it finds the element that is searched for or it runs out of elements.
* It stops after finding the first element
* @param e the element that is searched for
* @return true if the element was found and removed.
*/
@Override
public boolean REMOVE_KEY(KEY_TYPE type) {
int index = indexOf(type);
if(index == -1) return false;
REMOVE(index);
return true;
}
#endif
/**
* A Type-Specific pop function to reduce (un)boxing
* @param index the index of the element to fetch
* @return the value of the requested index
* @throws IndexOutOfBoundsException if the index is out of range
*/
@Override
public KEY_TYPE POP() {
return REMOVE(size() - 1);
}
/**
* A function to remove all elements that were provided in the other collection
* This function might delegate to a more appropiate function if nessesary
* @param c the elements that should be removed
* @return true if the collection was modified
* @throws NullPointerException if the collection is null
* @deprecated if the collection is type-specific
*/
@Override
@Primitive
public boolean removeAll(Collection<?> c) {
if(c.isEmpty()) return false;
boolean modified = false;
int j = 0;
for(int i = 0;i<size;i++) {
if(!c.contains(KEY_TO_OBJ(data[i]))) data[j++] = data[i];
else modified = true;
}
#if TYPE_OBJECT
Arrays.fill(data, j, size, null);
#endif
size = j;
return modified;
}
/**
* A function to retain all elements that were provided in the other collection
* This function might delegate to a more appropiate function if nessesary
* @param c the elements that should be kept. If empty, ARRAY_LIST#clear is called.
* @return true if the collection was modified
* @throws NullPointerException if the collection is null
* @deprecated if the collection is type-specific
*/
@Override
@Primitive
public boolean retainAll(Collection<?> c) {
if(c.isEmpty()) {
boolean modifed = size > 0;
clear();
return modifed;
}
boolean modified = false;
int j = 0;
for(int i = 0;i<size;i++) {
if(c.contains(KEY_TO_OBJ(data[i]))) data[j++] = data[i];
else modified = true;
}
#if TYPE_OBJECT
Arrays.fill(data, j, size, null);
#endif
size = j;
return modified;
}
/**
* A optimized List#removeIf(Predicate) that more quickly removes elements from the list then the ArrayList implementation
* @param filter the filter to remove elements
* @return true if the list was modified
* @deprecated if Type-Specific, use #remIf instead
*/
@Override
@Primitive
public boolean removeIf(Predicate<? super CLASS_TYPE> filter) {
Objects.requireNonNull(filter);
boolean modified = false;
int j = 0;
for(int i = 0;i<size;i++) {
if(!filter.test(KEY_TO_OBJ(data[i]))) data[j++] = data[i];
else modified = true;
}
#if TYPE_OBJECT
Arrays.fill(data, j, size, null);
#endif
size = j;
return modified;
}
/**
* A function to remove all elements that were provided in the other collection
* @param c the elements that should be removed
* @return true if the collection was modified
* @throws NullPointerException if the collection is null
*/
@Override
public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c) {
if(c.isEmpty()) return false;
boolean modified = false;
int j = 0;
for(int i = 0;i<size;i++) {
if(!c.contains(data[i])) data[j++] = data[i];
else modified = true;
}
#if TYPE_OBJECT
Arrays.fill(data, j, size, null);
#endif
size = j;
return modified;
}
/**
* A function to retain all elements that were provided in the other collection
* This function might delegate to a more appropiate function if nessesary
* @param c the elements that should be kept. If empty, ARRAY_LIST#clear is called.
* @return true if the collection was modified
* @throws NullPointerException if the collection is null
*/
@Override
public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c) {
if(c.isEmpty()) {
boolean modifed = size > 0;
clear();
return modifed;
}
boolean modified = false;
int j = 0;
for(int i = 0;i<size;i++) {
if(c.contains(data[i])) data[j++] = data[i];
else modified = true;
}
#if TYPE_OBJECT
Arrays.fill(data, j, size, null);
#endif
size = j;
return modified;
}
#if PRIMITIVES
/**
* A optimized List#removeIf(Predicate) that more quickly removes elements from the list then the ArrayList implementation
* @param filter the filter to remove elements
* @return true if the list was modified
*/
@Override
public boolean remIf(JAVA_PREDICATE filter) {
Objects.requireNonNull(filter);
boolean modified = false;
int j = 0;
for(int i = 0;i<size;i++) {
if(!filter.test(data[i])) data[j++] = data[i];
else modified = true;
}
size = j;
return modified;
}
#endif
/**
* A toArray implementation that ensures the Array itself is a Object.
* @return a Array of the elements in the list
*/
@Override
@Primitive
public Object[] toArray() {
Object[] obj = new Object[size];
for(int i = 0;i<size;i++)
obj[i] = KEY_TO_OBJ(data[i]);
return obj;
}
/**
* A toArray implementation that ensures the Array itself is a Object.
* @param a original array. If null a Object array with the right size is created. If to small the Array of the same type is created with the right size
* @return a Array of the elements in the list
*/
@Override
@Primitive
public <E> E[] toArray(E[] a) {
if(a == null) a = (E[])new Object[size];
else if(a.length < size) a = (E[])ObjectArrays.newArray(a.getClass().getComponentType(), size);
for(int i = 0;i<size;i++)
a[i] = (E)KEY_TO_OBJ(data[i]);
return a;
}
#if !TYPE_OBJECT
@Override
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) {
if(a.length < size) a = new KEY_TYPE[size];
System.arraycopy(data, 0, a, 0, size);
return a;
}
#endif
/**
* A function to return the size of the list
* @return the size of elements in the list
*/
@Override
public int size() {
return size;
}
/**
* A function to ensure the elements are within the requested size.
* If smaller then the stored elements they get removed as needed.
* If bigger it is ensured that enough room is provided depending on the implementation
* @param size the requested amount of elements/room for elements
*/
@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_KEY_VALUE);
this.size = size;
}
/**
* A function to clear all elements in the list.
*/
@Override
public void clear() {
#if TYPE_OBJECT
for(int i = 0;i<size;data[i] = null,i++);
#endif
size = 0;
}
/**
* Trims the original collection down to the size of the current elements or the requested size depending which is bigger
* @param size the requested trim size.
*/
@Override
public boolean trim(int size) {
if(size > size() || size() == data.length) return false;
int value = Math.max(size, size());
data = value == 0 ? EMPTY_KEY_ARRAY : Arrays.copyOf(data, value);
return true;
}
/**
* Increases the capacity of this implementation instance, if necessary,
* to ensure that it can hold at least the number of elements specified by
* the minimum capacity argument.
*
* @param size the desired minimum capacity
*/
@Override
public void ensureCapacity(int size) {
grow(size);
}
protected void grow(int capacity) {
if(capacity < data.length) return;
data = Arrays.copyOf(data, data == ARRAYS.EMPTY_ARRAY ? DEFAULT_ARRAY_SIZE : (int)Math.max(Math.min((long)data.length + (data.length >> 1), SanityChecks.MAX_ARRAY_SIZE), capacity));
}
protected void checkRange(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
protected void checkAddRange(int index) {
if (index < 0 || index > size)
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
}