Added Priority Queue Module

This commit is contained in:
2022-12-07 05:30:23 +01:00
parent ce9343348e
commit 3ce52668df
9 changed files with 323 additions and 210 deletions
@@ -26,8 +26,8 @@ import speiger.src.collections.PACKAGE.lists.LINKED_LIST;
#endif
#endif
#if SET_MODULE && !TYPE_BOOLEAN
import speiger.src.collections.PACKAGE.sets.SET;
#if LINKED_SET_FEATURE || LINKED_CUSTOM_SET_FEATURE || SET_FEATURE || CUSTOM_SET_FEATURE || RB_TREE_SET_FEATURE || AVL_TREE_SET_FEATURE || ARRAY_SET_FEATURE
import speiger.src.collections.PACKAGE.sets.SET;
#if LINKED_SET_FEATURE
import speiger.src.collections.PACKAGE.sets.LINKED_HASH_SET;
#else if LINKED_CUSTOM_SET_FEATURE
@@ -1,7 +1,9 @@
package speiger.src.collections.PACKAGE.lists;
#if TYPE_OBJECT
#if DEQUEUE_FEATURE
import java.util.Comparator;
#endif
import java.util.function.BiFunction;
#else if PRIMITIVES
import java.nio.JAVA_BUFFER;
@@ -31,9 +33,13 @@ import speiger.src.collections.PACKAGE.collections.STACK;
import speiger.src.collections.ints.functions.function.Int2ObjectFunction;
#endif
import speiger.src.collections.PACKAGE.collections.ITERATOR;
#if DEQUEUE_FEATURE
import speiger.src.collections.PACKAGE.queues.PRIORITY_DEQUEUE;
#endif
#if !TYPE_OBJECT
#if DEQUEUE_FEATURE
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
#endif
import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif
import speiger.src.collections.PACKAGE.utils.ARRAYS;
@@ -62,10 +68,10 @@ import speiger.src.collections.utils.SanityChecks;
*
* @Type(T)
*/
#if TYPE_OBJECT
public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_GENERIC_TYPE, Stack KEY_GENERIC_TYPE
#else
#if DEQUEUE_FEATURE
public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_GENERIC_TYPE, STACK KEY_GENERIC_TYPE
#else
public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE implements STACK KEY_GENERIC_TYPE
#endif
{
Entry KEY_GENERIC_TYPE first;
@@ -222,6 +228,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
return true;
}
#if DEQUEUE_FEATURE
@Override
public void enqueue(KEY_TYPE e) {
add(e);
@@ -232,6 +239,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
add(0, e);
}
#endif
@Override
public void push(KEY_TYPE e) {
add(e);
@@ -291,6 +299,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
return a;
}
#if DEQUEUE_FEATURE
@Override
public KEY_TYPE first() {
if(first == null) throw new NoSuchElementException();
@@ -303,11 +312,11 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
return last.value;
}
#endif
@Override
public KEY_TYPE peek(int index) {
return GET_KEY((size() - 1) - index);
}
@Override
public KEY_TYPE GET_KEY(int index) {
checkRange(index);
@@ -525,6 +534,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
}
#endif
#if DEQUEUE_FEATURE
@Override
public void onChanged() {}
@Override
@@ -538,25 +548,24 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
@Override
public KEY_TYPE dequeueLast() {
return pop();
}
#endif
@Override
public KEY_TYPE pop() {
if(last == null) throw new NoSuchElementException();
return unlinkLast(last);
}
@Override
public KEY_TYPE pop() {
return dequeueLast();
}
#if DEQUEUE_FEATURE
@Override
public boolean removeFirst(KEY_TYPE e) {
if(size == 0) return false;
for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next) {
if(KEY_EQUALS(entry.value, e)) {
unlink(entry);
return true;
}
}
return false;
#if TYPE_OBJECT
return remove(e);
#else
return REMOVE_KEY(e);
#endif
}
@Override
@@ -571,6 +580,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
return false;
}
#endif
@Override
public KEY_TYPE swapRemove(int index) {
checkRange(index);
@@ -653,7 +663,14 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
#else
@Override
public boolean REMOVE_KEY(KEY_TYPE e) {
return removeFirst(e);
if(size == 0) return false;
for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next) {
if(KEY_EQUALS(entry.value, e)) {
unlink(entry);
return true;
}
}
return false;
}
#endif
@@ -6,7 +6,9 @@ import java.util.Iterator;
#endif
import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERATOR;
#if QUEUES_FEATURE
import speiger.src.collections.PACKAGE.utils.PRIORITY_QUEUES;
#endif
/**
* A Type Speciifc PriorityDeque or Dequeue interface to allow implementations like FIFO queues.
@@ -81,6 +83,7 @@ public interface PRIORITY_DEQUEUE KEY_GENERIC_TYPE extends PRIORITY_QUEUE KEY_GE
*/
public default KEY_TYPE last() { return peek(size()-1); }
#if QUEUES_FEATURE
/**
* Creates a Wrapped PriorityDequeue that is Synchronized
* @return a new PriorityDequeue that is synchronized
@@ -96,6 +99,7 @@ public interface PRIORITY_DEQUEUE KEY_GENERIC_TYPE extends PRIORITY_QUEUE KEY_GE
*/
public default PRIORITY_DEQUEUE KEY_GENERIC_TYPE synchronizeQueue(Object mutex) { return PRIORITY_QUEUES.synchronize(this, mutex); }
#endif
@Override
public PRIORITY_DEQUEUE KEY_GENERIC_TYPE copy();
}
@@ -12,7 +12,9 @@ import speiger.src.collections.PACKAGE.functions.COMPARATOR;
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
/**
@@ -147,8 +149,9 @@ public interface PRIORITY_QUEUE KEY_GENERIC_TYPE extends ITERABLE KEY_GENERIC_TY
* @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
@@ -163,7 +166,8 @@ public interface PRIORITY_QUEUE KEY_GENERIC_TYPE extends ITERABLE KEY_GENERIC_TY
* @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)
@@ -37,8 +37,8 @@ import speiger.src.collections.PACKAGE.lists.LINKED_LIST;
#endif
#if !TYPE_BOOLEAN && OBJECT_ASYNC_MODULE
#if SET_MODULE
import speiger.src.collections.PACKAGE.sets.SET;
#if LINKED_SET_FEATURE || LINKED_CUSTOM_SET_FEATURE || SET_FEATURE || CUSTOM_SET_FEATURE || RB_TREE_SET_FEATURE || AVL_TREE_SET_FEATURE || ARRAY_SET_FEATURE
import speiger.src.collections.PACKAGE.sets.SET;
#if LINKED_SET_FEATURE
import speiger.src.collections.PACKAGE.sets.LINKED_HASH_SET;
#else if LINKED_CUSTOM_SET_FEATURE
@@ -11,7 +11,9 @@ import speiger.src.collections.PACKAGE.collections.COLLECTION;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
#endif
#if DEQUEUE_FEATURE
import speiger.src.collections.PACKAGE.queues.PRIORITY_DEQUEUE;
#endif
import speiger.src.collections.PACKAGE.queues.PRIORITY_QUEUE;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.CONSUMER;
@@ -46,6 +48,7 @@ public class PRIORITY_QUEUES
return queue instanceof SynchronizedPriorityQueue ? (SynchronizedPriorityQueue KEY_GENERIC_TYPE)queue : new SynchronizedPriorityQueueBRACES(queue, mutex);
}
#if DEQUEUE_FEATURE
/**
* Returns a synchronized PriorityDequeue instance based on the instance given.
* @param dequeue that should be synchronized
@@ -67,6 +70,7 @@ public class PRIORITY_QUEUES
return dequeue instanceof SynchronizedPriorityDequeue ? (SynchronizedPriorityDequeue KEY_GENERIC_TYPE)dequeue : new SynchronizedPriorityDequeueBRACES(dequeue, mutex);
}
#endif
/**
* Wrapper class for synchronization
* @Type(T)
@@ -134,6 +138,7 @@ public class PRIORITY_QUEUES
public int count(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return queue.count(filter); } }
}
#if DEQUEUE_FEATURE
/**
* Wrapper class for synchronization
* @Type(T)
@@ -166,4 +171,5 @@ public class PRIORITY_QUEUES
@Override
public PRIORITY_DEQUEUE KEY_GENERIC_TYPE copy() { synchronized(mutex) { return dequeue.copy(); } }
}
#endif
}