Added Priority Queue Module

This commit is contained in:
Speiger 2022-12-07 05:30:23 +01:00
parent ce9343348e
commit 3ce52668df
9 changed files with 323 additions and 210 deletions

File diff suppressed because it is too large Load Diff

View File

@ -75,7 +75,8 @@ public class CollectionModule extends BaseModule
addClassMapper("LIST_ITERATOR", "ListIterator"); addClassMapper("LIST_ITERATOR", "ListIterator");
addClassMapper("BI_ITERATOR", "BidirectionalIterator"); addClassMapper("BI_ITERATOR", "BidirectionalIterator");
addClassMapper("ITERATOR", "Iterator"); addClassMapper("ITERATOR", "Iterator");
addClassMapper("STACK", "Stack"); if(keyType.isObject()) addSimpleMapper("STACK", "Stack");
else addClassMapper("STACK", "Stack");
addClassMapper("STRATEGY", "Strategy"); addClassMapper("STRATEGY", "Strategy");
} }

View File

@ -1,5 +1,9 @@
package speiger.src.builder.modules; package speiger.src.builder.modules;
import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;
import speiger.src.builder.ClassType; import speiger.src.builder.ClassType;
@SuppressWarnings("javadoc") @SuppressWarnings("javadoc")
@ -12,22 +16,46 @@ public class PrioQueueModule extends BaseModule
@Override @Override
protected void loadVariables() {} protected void loadVariables() {}
@Override @Override
protected void loadFlags() {}
@Override
protected void loadFunctions() {} protected void loadFunctions() {}
@Override @Override
public boolean areDependenciesLoaded() { return isDependencyLoaded(CollectionModule.INSTANCE); } public boolean areDependenciesLoaded() { return isDependencyLoaded(CollectionModule.INSTANCE); }
@Override @Override
protected void loadBlockades() public Set<String> getModuleKeys(ClassType keyType, ClassType valueType) {
{ return new TreeSet<>(Arrays.asList("Wrappers", "Implementations", "Dequeue", "FiFoQueue", "HeapQueue", "ArrayPrioQueue"));
}
@Override
protected void loadFlags() {
if(isModuleEnabled()) addFlag("QUEUE_MODULE");
if(isModuleEnabled("Wrappers")) addKeyFlag("QUEUES_FEATURE");
boolean implementations = isModuleEnabled("Implementations");
if(isModuleEnabled("Dequeue")) {
addKeyFlag("DEQUEUE_FEATURE");
if(implementations && isModuleEnabled("FiFoQueue")) addKeyFlag("FIFO_QUEUE_FEATURE");
}
if(implementations && isModuleEnabled("HeapQueue")) addKeyFlag("HEAP_QUEUE_FEATURE");
if(implementations && isModuleEnabled("ArrayPrioQueue")) addKeyFlag("ARRAY_QUEUE_FEATURE");
}
@Override
protected void loadBlockades() {
if(!isModuleEnabled()) addBlockedFiles("PriorityQueue", "AbstractPriorityQueue");
if(!isModuleEnabled("Wrappers")) addBlockedFiles("PriorityQueues");
boolean implementations = !isModuleEnabled("Implementations");
boolean dequeue = !isModuleEnabled("Dequeue");
if(dequeue) addBlockedFiles("PriorityDequeue");
if(dequeue || implementations || !isModuleEnabled("FiFoQueue")) addBlockedFiles("ArrayFIFOQueue");
if(implementations || !isModuleEnabled("HeapQueue")) addBlockedFiles("HeapPriorityQueue");
if(implementations || !isModuleEnabled("ArrayPrioQueue")) addBlockedFiles("ArrayPriorityQueue");
if(keyType == ClassType.BOOLEAN) { if(keyType == ClassType.BOOLEAN) {
addBlockedFiles("QueueTests"); addBlockedFiles("QueueTests");
} }
} }
@Override @Override
protected void loadRemappers() protected void loadRemappers() {
{
//Main Classes //Main Classes
addRemapper("AbstractPriorityQueue", "Abstract%sPriorityQueue"); addRemapper("AbstractPriorityQueue", "Abstract%sPriorityQueue");
@ -38,8 +66,7 @@ public class PrioQueueModule extends BaseModule
} }
@Override @Override
protected void loadClasses() protected void loadClasses() {
{
//Implementation Classes //Implementation Classes
addClassMapper("ARRAY_FIFO_QUEUE", "ArrayFIFOQueue"); addClassMapper("ARRAY_FIFO_QUEUE", "ArrayFIFOQueue");
addClassMapper("ARRAY_PRIORITY_QUEUE", "ArrayPriorityQueue"); addClassMapper("ARRAY_PRIORITY_QUEUE", "ArrayPriorityQueue");

View File

@ -26,8 +26,8 @@ import speiger.src.collections.PACKAGE.lists.LINKED_LIST;
#endif #endif
#endif #endif
#if SET_MODULE && !TYPE_BOOLEAN #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 #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 #if LINKED_SET_FEATURE
import speiger.src.collections.PACKAGE.sets.LINKED_HASH_SET; import speiger.src.collections.PACKAGE.sets.LINKED_HASH_SET;
#else if LINKED_CUSTOM_SET_FEATURE #else if LINKED_CUSTOM_SET_FEATURE

View File

@ -1,7 +1,9 @@
package speiger.src.collections.PACKAGE.lists; package speiger.src.collections.PACKAGE.lists;
#if TYPE_OBJECT #if TYPE_OBJECT
#if DEQUEUE_FEATURE
import java.util.Comparator; import java.util.Comparator;
#endif
import java.util.function.BiFunction; import java.util.function.BiFunction;
#else if PRIMITIVES #else if PRIMITIVES
import java.nio.JAVA_BUFFER; import java.nio.JAVA_BUFFER;
@ -31,9 +33,13 @@ import speiger.src.collections.PACKAGE.collections.STACK;
import speiger.src.collections.ints.functions.function.Int2ObjectFunction; import speiger.src.collections.ints.functions.function.Int2ObjectFunction;
#endif #endif
import speiger.src.collections.PACKAGE.collections.ITERATOR; import speiger.src.collections.PACKAGE.collections.ITERATOR;
#if DEQUEUE_FEATURE
import speiger.src.collections.PACKAGE.queues.PRIORITY_DEQUEUE; import speiger.src.collections.PACKAGE.queues.PRIORITY_DEQUEUE;
#endif
#if !TYPE_OBJECT #if !TYPE_OBJECT
#if DEQUEUE_FEATURE
import speiger.src.collections.PACKAGE.functions.COMPARATOR; import speiger.src.collections.PACKAGE.functions.COMPARATOR;
#endif
import speiger.src.collections.PACKAGE.functions.CONSUMER; import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif #endif
import speiger.src.collections.PACKAGE.utils.ARRAYS; import speiger.src.collections.PACKAGE.utils.ARRAYS;
@ -62,10 +68,10 @@ import speiger.src.collections.utils.SanityChecks;
* *
* @Type(T) * @Type(T)
*/ */
#if TYPE_OBJECT #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 PRIORITY_DEQUEUE KEY_GENERIC_TYPE, STACK KEY_GENERIC_TYPE 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 #endif
{ {
Entry KEY_GENERIC_TYPE first; Entry KEY_GENERIC_TYPE first;
@ -222,6 +228,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
return true; return true;
} }
#if DEQUEUE_FEATURE
@Override @Override
public void enqueue(KEY_TYPE e) { public void enqueue(KEY_TYPE e) {
add(e); add(e);
@ -232,6 +239,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
add(0, e); add(0, e);
} }
#endif
@Override @Override
public void push(KEY_TYPE e) { public void push(KEY_TYPE e) {
add(e); add(e);
@ -291,6 +299,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
return a; return a;
} }
#if DEQUEUE_FEATURE
@Override @Override
public KEY_TYPE first() { public KEY_TYPE first() {
if(first == null) throw new NoSuchElementException(); 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; return last.value;
} }
#endif
@Override @Override
public KEY_TYPE peek(int index) { public KEY_TYPE peek(int index) {
return GET_KEY((size() - 1) - index); return GET_KEY((size() - 1) - index);
} }
@Override @Override
public KEY_TYPE GET_KEY(int index) { public KEY_TYPE GET_KEY(int index) {
checkRange(index); checkRange(index);
@ -525,6 +534,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
} }
#endif #endif
#if DEQUEUE_FEATURE
@Override @Override
public void onChanged() {} public void onChanged() {}
@Override @Override
@ -538,25 +548,24 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
@Override @Override
public KEY_TYPE dequeueLast() { public KEY_TYPE dequeueLast() {
return pop();
}
#endif
@Override
public KEY_TYPE pop() {
if(last == null) throw new NoSuchElementException(); if(last == null) throw new NoSuchElementException();
return unlinkLast(last); return unlinkLast(last);
} }
@Override #if DEQUEUE_FEATURE
public KEY_TYPE pop() {
return dequeueLast();
}
@Override @Override
public boolean removeFirst(KEY_TYPE e) { public boolean removeFirst(KEY_TYPE e) {
if(size == 0) return false; #if TYPE_OBJECT
for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next) { return remove(e);
if(KEY_EQUALS(entry.value, e)) { #else
unlink(entry); return REMOVE_KEY(e);
return true; #endif
}
}
return false;
} }
@Override @Override
@ -571,6 +580,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
return false; return false;
} }
#endif
@Override @Override
public KEY_TYPE swapRemove(int index) { public KEY_TYPE swapRemove(int index) {
checkRange(index); checkRange(index);
@ -653,7 +663,14 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
#else #else
@Override @Override
public boolean REMOVE_KEY(KEY_TYPE e) { 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 #endif

View File

@ -6,7 +6,9 @@ import java.util.Iterator;
#endif #endif
import speiger.src.collections.PACKAGE.collections.COLLECTION; import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERATOR; import speiger.src.collections.PACKAGE.collections.ITERATOR;
#if QUEUES_FEATURE
import speiger.src.collections.PACKAGE.utils.PRIORITY_QUEUES; import speiger.src.collections.PACKAGE.utils.PRIORITY_QUEUES;
#endif
/** /**
* A Type Speciifc PriorityDeque or Dequeue interface to allow implementations like FIFO queues. * 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); } public default KEY_TYPE last() { return peek(size()-1); }
#if QUEUES_FEATURE
/** /**
* Creates a Wrapped PriorityDequeue that is Synchronized * Creates a Wrapped PriorityDequeue that is Synchronized
* @return a new 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); } public default PRIORITY_DEQUEUE KEY_GENERIC_TYPE synchronizeQueue(Object mutex) { return PRIORITY_QUEUES.synchronize(this, mutex); }
#endif
@Override @Override
public PRIORITY_DEQUEUE KEY_GENERIC_TYPE copy(); public PRIORITY_DEQUEUE KEY_GENERIC_TYPE copy();
} }

View File

@ -12,7 +12,9 @@ import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.PACKAGE.collections.COLLECTION; import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERABLE; import speiger.src.collections.PACKAGE.collections.ITERABLE;
import speiger.src.collections.PACKAGE.collections.ITERATOR; import speiger.src.collections.PACKAGE.collections.ITERATOR;
#if QUEUES_FEATURE
import speiger.src.collections.PACKAGE.utils.PRIORITY_QUEUES; 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 * @return draining iterator of the PriorityQueue
*/ */
public ITERATOR KEY_GENERIC_TYPE iterator(); public ITERATOR KEY_GENERIC_TYPE iterator();
#endif #endif
#if QUEUES_FEATURE
/** /**
* Creates a Wrapped PriorityQueue that is Synchronized * Creates a Wrapped PriorityQueue that is Synchronized
* @return a new 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 * @see PRIORITY_QUEUES#synchronize
*/ */
public default PRIORITY_QUEUE KEY_GENERIC_TYPE synchronizeQueue(Object mutex) { return PRIORITY_QUEUES.synchronize(this, mutex); } 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 * A method to drop the contents of the Queue without clearing the queue
* @Type(E) * @Type(E)

View File

@ -37,8 +37,8 @@ import speiger.src.collections.PACKAGE.lists.LINKED_LIST;
#endif #endif
#if !TYPE_BOOLEAN && OBJECT_ASYNC_MODULE #if !TYPE_BOOLEAN && OBJECT_ASYNC_MODULE
#if SET_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 #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 #if LINKED_SET_FEATURE
import speiger.src.collections.PACKAGE.sets.LINKED_HASH_SET; import speiger.src.collections.PACKAGE.sets.LINKED_HASH_SET;
#else if LINKED_CUSTOM_SET_FEATURE #else if LINKED_CUSTOM_SET_FEATURE

View File

@ -11,7 +11,9 @@ import speiger.src.collections.PACKAGE.collections.COLLECTION;
#if !TYPE_OBJECT #if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.COMPARATOR; import speiger.src.collections.PACKAGE.functions.COMPARATOR;
#endif #endif
#if DEQUEUE_FEATURE
import speiger.src.collections.PACKAGE.queues.PRIORITY_DEQUEUE; import speiger.src.collections.PACKAGE.queues.PRIORITY_DEQUEUE;
#endif
import speiger.src.collections.PACKAGE.queues.PRIORITY_QUEUE; import speiger.src.collections.PACKAGE.queues.PRIORITY_QUEUE;
#if !TYPE_OBJECT #if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.CONSUMER; 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); 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. * Returns a synchronized PriorityDequeue instance based on the instance given.
* @param dequeue that should be synchronized * @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); return dequeue instanceof SynchronizedPriorityDequeue ? (SynchronizedPriorityDequeue KEY_GENERIC_TYPE)dequeue : new SynchronizedPriorityDequeueBRACES(dequeue, mutex);
} }
#endif
/** /**
* Wrapper class for synchronization * Wrapper class for synchronization
* @Type(T) * @Type(T)
@ -134,6 +138,7 @@ public class PRIORITY_QUEUES
public int count(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return queue.count(filter); } } public int count(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return queue.count(filter); } }
} }
#if DEQUEUE_FEATURE
/** /**
* Wrapper class for synchronization * Wrapper class for synchronization
* @Type(T) * @Type(T)
@ -166,4 +171,5 @@ public class PRIORITY_QUEUES
@Override @Override
public PRIORITY_DEQUEUE KEY_GENERIC_TYPE copy() { synchronized(mutex) { return dequeue.copy(); } } public PRIORITY_DEQUEUE KEY_GENERIC_TYPE copy() { synchronized(mutex) { return dequeue.copy(); } }
} }
#endif
} }