Primitive-Collections/src/builder/resources/speiger/assets/collections/templates/queues/PriorityQueue.template

203 lines
6.7 KiB
Plaintext

package speiger.src.collections.PACKAGE.queues;
#if TYPE_OBJECT
import java.util.Comparator;
import java.util.Collection;
import java.util.Iterator;
import java.util.function.IntFunction;
#else
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
#endif
import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERABLE;
import speiger.src.collections.PACKAGE.collections.ITERATOR;
#if QUEUES_FEATURE
import speiger.src.collections.PACKAGE.utils.PRIORITY_QUEUES;
#endif
/**
* A Simple PriorityQueue (or Queue) interface that provides with the nessesary functions to interact with it, without cluttering with the Collection interface.
* @Type(T)
*/
public interface PRIORITY_QUEUE KEY_GENERIC_TYPE extends ITERABLE KEY_GENERIC_TYPE
{
/**
* @return true if the PriorityQueue is empty
*/
public default boolean isEmpty() { return size() <= 0; }
/**
* @return the amount of elements that are stored in the PriorityQueue
*/
public int size();
/**
* clears all elements within the PriorityQueue,
* this does not resize the backing arrays
*/
public void clear();
/**
* Method to insert a element into the PriorityQueue
* @param e the element that should be inserted
*/
public void enqueue(KEY_TYPE e);
/**
* Method to mass insert elements into the PriorityQueue
* @param e the elements that should be inserted
*/
public default void enqueueAll(KEY_TYPE... e) {
enqueueAll(e, 0, e.length);
}
/**
* Method to mass insert elements into the PriorityQueue
* @param e the elements that should be inserted
* @param length the amount of elements that should be inserted
*/
public default void enqueueAll(KEY_TYPE[] e, int length) {
enqueueAll(e, 0, length);
}
/**
* Method to mass insert elements into the PriorityQueue
* @param e the elements that should be inserted
* @param offset the offset where in the array should be started
* @param length the amount of elements that should be inserted
*/
public default void enqueueAll(KEY_TYPE[] e, int offset, int length) {
for(int i = 0;i<length;i++)
enqueue(e[i+offset]);
}
/**
* Method to mass insert elements into the PriorityQueue
* @param c the elements that should be inserted from the Collection
*/
public default void enqueueAll(COLLECTION KEY_GENERIC_TYPE c) {
for(ITERATOR KEY_GENERIC_TYPE iter = c.iterator();iter.hasNext();)
enqueue(iter.NEXT());
}
#if TYPE_OBJECT
/**
* Method to mass insert elements into the PriorityQueue
* This method exists to add support for Java Collections to make it more useable
* @param c the elements that should be inserted from the Collection
*/
public default void enqueueAll(Collection<? extends CLASS_TYPE> c) {
for(Iterator<? extends CLASS_TYPE> iter = c.iterator();iter.hasNext();)
enqueue(iter.next());
}
#endif
/**
* Method to extract a element from the PriorityQueue
* @return a element from the Queue
* @throws java.util.NoSuchElementException if no element is present
*/
public KEY_TYPE dequeue();
/**
* Peeking function to see whats inside the queue.
* @param index of the element that is requested to be viewed.
* @return the element that is requested
*/
public KEY_TYPE peek(int index);
/**
* Shows the element that is to be returned next
* @return the first element in the Queue
*/
public default KEY_TYPE first() { return peek(0); }
/**
* Method to find out if a element is part of the queue
* @param e the element that is searched for
* @return true if the element is in the queue
*/
public boolean contains(KEY_TYPE e);
/**
* Removes the first found element in the queue
* @param e the element that should be removed
* @return if a searched element was removed
*/
public boolean removeFirst(KEY_TYPE e);
/**
* Removes the last found element in the queue
* @param e the element that should be removed
* @return if a searched element was removed
*/
public boolean removeLast(KEY_TYPE e);
/**
* Allows to notify the Queue to be revalidate its data
*/
public void onChanged();
/**
* A Function that does a shallow clone of the PriorityQueue itself.
* This function is more optimized then a copy constructor since the PriorityQueue does not have to be unsorted/resorted.
* It can be compared to Cloneable but with less exception risk
* @return a Shallow Copy of the PriorityQueue
* @note Wrappers and view PriorityQueues will not support this feature
*/
public PRIORITY_QUEUE KEY_GENERIC_TYPE copy();
/**
* @return the sorter of the Queue, can be null
*/
public COMPARATOR KEY_SUPER_GENERIC_TYPE comparator();
#if TYPE_OBJECT
/**
* @return draining iterator of the PriorityQueue
*/
public ITERATOR KEY_GENERIC_TYPE iterator();
#endif
#if QUEUES_FEATURE
/**
* Creates a Wrapped PriorityQueue that is Synchronized
* @return a new PriorityQueue that is synchronized
* @see PRIORITY_QUEUES#synchronize
*/
public default PRIORITY_QUEUE KEY_GENERIC_TYPE synchronizeQueue() { return PRIORITY_QUEUES.synchronize(this); }
/**
* Creates a Wrapped PriorityQueue that is Synchronized
* @param mutex is the controller of the synchronization block
* @return a new PriorityQueue Wrapper that is synchronized
* @see PRIORITY_QUEUES#synchronize
*/
public default PRIORITY_QUEUE KEY_GENERIC_TYPE synchronizeQueue(Object mutex) { return PRIORITY_QUEUES.synchronize(this, mutex); }
#endif
/**
* A method to drop the contents of the Queue without clearing the queue
* @Type(E)
* @return the contents of the queue into a seperate array.
*/
public default GENERIC_SPECIAL_KEY_BRACES<E> KEY_SPECIAL_TYPE[] TO_ARRAY() { return TO_ARRAY(NEW_SPECIAL_KEY_ARRAY(size())); }
/**
* A method to drop the contents of the Queue without clearing the queue
* @param input where the elements should be inserted to. If it does not fit then it creates a new appropiatly created array
* @Type(E)
* @return the contents of the queue into a seperate array.
* @note if the Type is generic then a Object Array is created instead of a Type Array
*/
public GENERIC_SPECIAL_KEY_BRACES<E> KEY_SPECIAL_TYPE[] TO_ARRAY(KEY_SPECIAL_TYPE[] input);
#if TYPE_OBJECT
/**
* A Helper function that simplifies the process of creating a new Array.
* @param action the array creation function
* @param <E> the returning arrayType
* @return an array containing all of the elements in this collection
* @see Collection#toArray(Object[])
*/
default <E> E[] TO_ARRAY(IntFunction<E[]> action) {
return TO_ARRAY(action.apply(size()));
}
#endif
}