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

603 lines
21 KiB
Plaintext

package speiger.src.collections.PACKAGE.lists;
#if PRIMITIVES
import java.nio.JAVA_BUFFER;
#endif
import java.util.List;
#if !TYPE_OBJECT && !TYPE_BOOLEAN
import java.util.function.JAVA_UNARY_OPERATOR;
#endif
import java.util.Objects;
import java.util.Comparator;
#if !TYPE_BOOLEAN
import java.util.function.UnaryOperator;
#endif
import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.SPLIT_ITERATOR;
import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
#endif
import speiger.src.collections.PACKAGE.utils.ARRAYS;
#if LISTS_FEATURE
import speiger.src.collections.PACKAGE.utils.LISTS;
#endif
#if INT_LIST_MODULE && !TYPE_INT
import speiger.src.collections.ints.lists.IntList;
#endif
import speiger.src.collections.PACKAGE.utils.SPLIT_ITERATORS;
#if TYPE_BYTE || TYPE_SHORT || TYPE_CHAR || TYPE_FLOAT
import speiger.src.collections.utils.SanityChecks;
#endif
/**
* A Type Specific List interface that reduces boxing/unboxing and adds a couple extra quality of life features
* @Type(T)
*/
public interface LIST KEY_GENERIC_TYPE extends COLLECTION KEY_GENERIC_TYPE, List<CLASS_TYPE>
{
#if !TYPE_OBJECT
/**
* A Type-Specific add Function to reduce (un)boxing
* @param e the element to add
* @return true if the list was modified
* @see List#add(Object)
*/
@Override
public boolean add(KEY_TYPE e);
/**
* A Type-Specific add Function to reduce (un)boxing
* @param e the element to add
* @param index index at which the specified element is to be inserted
* @see List#add(int, Object)
*/
public void add(int index, KEY_TYPE e);
#endif
/**
* A Helper function that will only add elements if it is not present.
* @param e the element to add
* @return true if the list was modified
*/
public default boolean addIfAbsent(KEY_TYPE e) {
if(indexOf(e) == -1) return add(e);
return false;
}
/**
* A Helper function that will only add elements if it is present.
* @param e the element to add
* @return true if the list was modified
*/
public default boolean addIfPresent(KEY_TYPE e) {
if(indexOf(e) != -1) return add(e);
return false;
}
/**
* A Type-Specific addAll Function to reduce (un)boxing
* @param c the elements that need to be added
* @param index index at which the specified elements is to be inserted
* @return true if the list was modified
* @see java.util.List#addAll(int, java.util.Collection)
*/
public boolean addAll(int index, COLLECTION KEY_GENERIC_TYPE c);
/**
* A Type-Specific and optimized addAll function that allows a faster transfer of elements
* @param c the elements that need to be added
* @return true if the list was modified
*/
public boolean addAll(LIST KEY_GENERIC_TYPE c);
/**
* A Type-Specific and optimized addAll function that allows a faster transfer of elements
* @param c the elements that need to be added
* @param index index at which the specified elements is to be inserted
* @return true if the list was modified
*/
public boolean addAll(int index, LIST KEY_GENERIC_TYPE c);
/**
* Helper method that returns the first element of a List.
* This function was introduced due to how annoying it is to get/remove the last element of a list.
* This simplifies this process a bit.
* @return first element of the list
*/
public default KEY_TYPE GET_FIRST_KEY() {
return GET_KEY(0);
}
/**
* Helper method that returns the last element of a List.
* This function was introduced due to how annoying it is to get/remove the last element of a list.
* This simplifies this process a bit.
* @return last element of the list
*/
public default KEY_TYPE GET_LAST_KEY() {
return GET_KEY(size() - 1);
}
/**
* Helper method that removes and returns the first element of a List.
* This function was introduced due to how annoying it is to get/remove the last element of a list.
* This simplifies this process a bit.
* @return first element of the list and removes it
*/
public default KEY_TYPE REMOVE_FIRST_KEY() {
return REMOVE(0);
}
/**
* Helper method that removes and returns the last element of a List.
* This function was introduced due to how annoying it is to get/remove the last element of a list.
* This simplifies this process a bit.
* @return last element of the list and removes it
*/
public default KEY_TYPE REMOVE_LAST_KEY() {
return REMOVE(size() - 1);
}
#if !TYPE_OBJECT
/**
* A Type-Specific get function to reduce (un)boxing
* @param index the index of the value that is requested
* @return the value at the given index
* @throws IndexOutOfBoundsException if the index is not within the list range
* @see List#get(int)
*/
public KEY_TYPE GET_KEY(int index);
/**
* A Type-Specific set function to reduce (un)boxing
* @param index index of the element to replace
* @param e element to be stored at the specified position
* @return the element previously at the specified position
* @throws IndexOutOfBoundsException if the index is not within the list range
* @see List#set(int, Object)
*/
public KEY_TYPE set(int index, KEY_TYPE e);
/**
* A Type-Specific remove function to reduce (un)boxing
* @param index the index of the element to be removed
* @return the element previously at the specified position
* @see List#remove(int)
*/
public KEY_TYPE REMOVE(int index);
/**
* A Type-Specific indexOf function to reduce (un)boxing
* @param e the element that is searched for
* @return the index of the element if found. (if not found then -1)
* @note does not support null values
*/
public int indexOf(KEY_TYPE e);
/**
* A Type-Specific lastIndexOf function to reduce (un)boxing
* @param e the element that is searched for
* @return the lastIndex of the element if found. (if not found then -1)
* @note does not support null values
*/
public int lastIndexOf(KEY_TYPE e);
#if !TYPE_BOOLEAN
/**
* A Type-Specific replace function to reduce (un)boxing
* @param o the action to replace the values
* @throws NullPointerException if o is null
*/
public default void REPLACE(JAVA_UNARY_OPERATOR o) {
Objects.requireNonNull(o);
LIST_ITERATOR iter = listIterator();
while (iter.hasNext())
#if TYPE_BYTE || TYPE_SHORT || TYPE_CHAR || TYPE_FLOAT
iter.set(SanityChecks.SANITY_CAST(o.APPLY_CAST(iter.NEXT())));
#else
iter.set(o.APPLY(iter.NEXT()));
#endif
}
#endif
#else
/**
* A function to replace all values in the list
* @param o the action to replace the values
* @throws NullPointerException if o is null
*/
@Override
public default void replaceAll(UnaryOperator<CLASS_TYPE> o) {
Objects.requireNonNull(o);
LIST_ITERATOR KEY_GENERIC_TYPE iter = listIterator();
while (iter.hasNext()) iter.set(o.apply(iter.NEXT()));
}
#endif
/**
* A function to fast add elements to the list
* @param a the elements that should be added
* @throws IndexOutOfBoundsException if from is outside of the lists range
*/
public default void addElements(KEY_TYPE... a) { addElements(size(), a, 0, a.length); }
/**
* A function to fast add elements to the list
* @param from the index where the elements should be added into the list
* @param a the elements that should be added
* @throws IndexOutOfBoundsException if from is outside of the lists range
*/
public default void addElements(int from, KEY_TYPE... a) { addElements(from, a, 0, a.length); }
/**
* A function to fast add elements to the list
* @param from the index where the elements should be added into the list
* @param a the elements that should be added
* @param offset the start index of the array should be read from
* @param length how many elements should be read from
* @throws IndexOutOfBoundsException if from is outside of the lists range
*/
public void addElements(int from, KEY_TYPE[] a, int offset, int 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
* @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
*/
public default KEY_TYPE[] getElements(int from, KEY_TYPE[] a) { return getElements(from, a, 0, a.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
*/
public KEY_TYPE[] getElements(int from, KEY_TYPE[] a, int offset, int length);
/**
* 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)
*/
public void removeElements(int from, int to);
/**
* A Highly Optimized remove function that removes the desired element.
* But instead of shifting the elements to the left it moves the last element to the removed space.
* @param index the index of the element to be removed
* @return the element previously at the specified position
*/
public KEY_TYPE swapRemove(int index);
/**
* A Highly Optimized remove function that removes the desired element.
* But instead of shifting the elements to the left it moves the last element to the removed space.
* @param e the element that should be removed
* @return true if the element was removed
*/
public boolean REMOVE_SWAP(KEY_TYPE e);
#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
* @Type(K)
*/
public <K> K[] extractElements(int from, int to, Class<K> type);
/**
* Sorts the elements specified by the Natural order either by using the Comparator or the elements
* @see java.util.List#sort(Comparator)
*/
@Override
public default void sort(Comparator<? super CLASS_TYPE> c) {
KEY_TYPE[] array = (KEY_TYPE[])TO_ARRAY();
if(c != null) ARRAYS.stableSort(array, c);
else ARRAYS.stableSort(array);
LIST_ITERATOR KEY_GENERIC_TYPE iter = listIterator();
for (int i = 0,m=size();i<m && iter.hasNext();i++) {
iter.NEXT();
iter.set(array[i]);
}
}
/**
* 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 java.util.List#sort(Comparator)
*/
public default void unstableSort(Comparator<? super CLASS_TYPE> c) {
KEY_TYPE[] array = (KEY_TYPE[])TO_ARRAY();
if(c != null) ARRAYS.unstableSort(array, c);
else ARRAYS.unstableSort(array);
LIST_ITERATOR KEY_GENERIC_TYPE iter = listIterator();
for (int i = 0,m=size();i<m && iter.hasNext();i++) {
iter.NEXT();
iter.set(array[i]);
}
}
#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
*/
public KEY_TYPE[] extractElements(int from, int to);
#if PRIMITIVES
/**
* Helper function that allows to fastFill a buffer reducing the duplication requirement
* @param buffer where the data should be stored in.
*/
public default void fillBuffer(JAVA_BUFFER buffer) { buffer.put(TO_ARRAY()); }
#endif
/** {@inheritDoc}
* <p>This default implementation delegates to the corresponding type-specific function.
* @deprecated Please use the corresponding type-specific function instead.
*/
@Override
@Deprecated
default void sort(Comparator<? super CLASS_TYPE> c) {
sort((K, V) -> c.compare(KEY_TO_OBJ(K), KEY_TO_OBJ(V)));
}
/**
* 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 java.util.List#sort(Comparator)
* @see ARRAYS#stableSort(KEY_TYPE[], COMPARATOR)
*/
public default void sort(COMPARATOR c) {
KEY_TYPE[] array = TO_ARRAY();
if(c != null) ARRAYS.stableSort(array, c);
else ARRAYS.stableSort(array);
LIST_ITERATOR KEY_GENERIC_TYPE iter = listIterator();
for (int i = 0,m=size();i<m && iter.hasNext();i++) {
iter.NEXT();
iter.set(array[i]);
}
}
/**
* <p>This default implementation delegates to the corresponding type-specific function.
* @param c the sorter of the elements, can be null
* @deprecated Please use the corresponding type-specific function instead.
*/
@Deprecated
public default void unstableSort(Comparator<? super CLASS_TYPE> c) {
unstableSort((K, V) -> c.compare(KEY_TO_OBJ(K), KEY_TO_OBJ(V)));
}
/**
* 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 java.util.List#sort(Comparator)
* @see ARRAYS#unstableSort(KEY_TYPE[], COMPARATOR)
*/
public default void unstableSort(COMPARATOR c) {
KEY_TYPE[] array = TO_ARRAY();
if(c != null) ARRAYS.unstableSort(array, c);
else ARRAYS.unstableSort(array);
LIST_ITERATOR KEY_GENERIC_TYPE iter = listIterator();
for (int i = 0,m=size();i<m && iter.hasNext();i++) {
iter.NEXT();
iter.set(array[i]);
}
}
#endif
/**
* A Indexed forEach implementation that allows you to keep track of how many elements were already iterated over.
* @param action The action to be performed for each element
* @throws java.lang.NullPointerException if the specified action is null
*/
@Override
public default void forEachIndexed(BI_FROM_INT_CONSUMER KEY_GENERIC_TYPE action) {
Objects.requireNonNull(action);
for(int i = 0,m=size();i<m;action.accept(i, GET_KEY(i++)));
}
/**
* A Type-Specific Iterator of listIterator
* @see java.util.List#listIterator
*/
@Override
public LIST_ITERATOR KEY_GENERIC_TYPE listIterator();
/**
* A Type-Specific Iterator of listIterator
* @see java.util.List#listIterator(int)
*/
@Override
public LIST_ITERATOR KEY_GENERIC_TYPE listIterator(int index);
/**
* Creates a Iterator that follows the indecies provided.<br>
* For example if the Lists Contents is:<br> -1, 0 1 <br>and the indecies are: <br>0, 1, 2, 2, 1, 0<br>
* then the iterator will return the following values: <br>-1, 0, 1, 1, 0, -1
* @param indecies that should be used for the iteration.
* @return a custom indexed iterator
*/
public LIST_ITERATOR KEY_GENERIC_TYPE indexedIterator(int...indecies);
#if INT_LIST_MODULE
/**
* Creates a Iterator that follows the indecies provided.<br>
* For example if the Lists Contents is:<br> -1, 0 1 <br>and the indecies are: <br>0, 1, 2, 2, 1, 0<br>
* then the iterator will return the following values: <br>-1, 0, 1, 1, 0, -1
* @param indecies that should be used for the iteration.
* @return a custom indexed iterator
*/
public LIST_ITERATOR KEY_GENERIC_TYPE indexedIterator(IntList indecies);
#endif
/**
* A Type-Specific List of subList
* @see java.util.List#subList(int, int)
*/
@Override
public LIST KEY_GENERIC_TYPE subList(int from, int to);
/**
* A Type-Specific List Helper that shows all elements in reverse.
* @return a list wrapper that has all elements reversed!
*/
public LIST KEY_GENERIC_TYPE reversed();
#if LISTS_FEATURE
/**
* Creates a Wrapped List that is Synchronized
* @return a new List that is synchronized
* @see LISTS#synchronize
*/
public default LIST KEY_GENERIC_TYPE synchronize() { return LISTS.synchronize(this); }
/**
* Creates a Wrapped List that is Synchronized
* @param mutex is the controller of the synchronization block
* @return a new List Wrapper that is synchronized
* @see LISTS#synchronize
*/
public default LIST KEY_GENERIC_TYPE synchronize(Object mutex) { return LISTS.synchronize(this, mutex); }
/**
* Creates a Wrapped List that is unmodifiable
* @return a new List Wrapper that is unmodifiable
* @see LISTS#unmodifiable
*/
public default LIST KEY_GENERIC_TYPE unmodifiable() { return LISTS.unmodifiable(this); }
#endif
/**
* 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
*/
public void size(int size);
@Override
public LIST KEY_GENERIC_TYPE copy();
#if !TYPE_OBJECT
/** {@inheritDoc}
* <p>This default implementation delegates to the corresponding type-specific function.
* @deprecated Please use the corresponding type-specific function instead.
*/
@Override
@Deprecated
public default boolean add(CLASS_TYPE e) {
return COLLECTION.super.add(e);
}
/** {@inheritDoc}
* <p>This default implementation delegates to the corresponding type-specific function.
* @deprecated Please use the corresponding type-specific function instead.
*/
@Override
@Deprecated
public default CLASS_TYPE get(int index) {
return KEY_TO_OBJ(GET_KEY(index));
}
/** {@inheritDoc}
* <p>This default implementation delegates to the corresponding type-specific function.
* @deprecated Please use the corresponding type-specific function instead.
*/
@Override
@Deprecated
public default CLASS_TYPE set(int index, CLASS_TYPE e) {
return KEY_TO_OBJ(set(index, OBJ_TO_KEY(e)));
}
/** {@inheritDoc}
* <p>This default implementation delegates to the corresponding type-specific function.
* @deprecated Please use the corresponding type-specific function instead.
*/
@Override
@Deprecated
public default int indexOf(Object o) {
return indexOf(CLASS_TO_KEY(o));
}
/** {@inheritDoc}
* <p>This default implementation delegates to the corresponding type-specific function.
* @deprecated Please use the corresponding type-specific function instead.
*/
@Override
@Deprecated
public default int lastIndexOf(Object o) {
return lastIndexOf(CLASS_TO_KEY(o));
}
/** {@inheritDoc}
* <p>This default implementation delegates to the corresponding type-specific function.
* @deprecated Please use the corresponding type-specific function instead.
*/
@Override
@Deprecated
public default boolean contains(Object o) {
return COLLECTION.super.contains(o);
}
/** {@inheritDoc}
* <p>This default implementation delegates to the corresponding type-specific function.
* @deprecated Please use the corresponding type-specific function instead.
*/
@Override
@Deprecated
public default boolean remove(Object o) {
return COLLECTION.super.remove(o);
}
/** {@inheritDoc}
* <p>This default implementation delegates to the corresponding type-specific function.
* @deprecated Please use the corresponding type-specific function instead.
*/
@Override
@Deprecated
public default CLASS_TYPE remove(int index) {
return KEY_TO_OBJ(REMOVE(index));
}
#if !TYPE_BOOLEAN
/** {@inheritDoc}
* <p>This default implementation delegates to the corresponding type-specific function.
* @deprecated Please use the corresponding type-specific function instead.
*/
@Override
@Deprecated
public default void replaceAll(UnaryOperator<CLASS_TYPE> o) {
Objects.requireNonNull(o);
#if TYPE_BYTE || TYPE_SHORT || TYPE_CHAR || TYPE_FLOAT
REPLACE(T -> OBJ_TO_KEY(o.apply(KEY_TO_OBJ((KEY_TYPE)T))));
#else
REPLACE(T -> OBJ_TO_KEY(o.apply(KEY_TO_OBJ(T))));
#endif
}
#endif
#endif
/**
* A Type Specific Type Splititerator to reduce boxing/unboxing
* @return type specific splititerator
*/
@Override
default SPLIT_ITERATOR KEY_GENERIC_TYPE spliterator() { return SPLIT_ITERATORS.createSplititerator(this, 0); }
}