PriorityQueues no longer extends Object Variant.

This commit is contained in:
2021-06-23 19:22:19 +02:00
parent a669f69d99
commit 357b40e670
8 changed files with 33 additions and 121 deletions
@@ -115,7 +115,7 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_G
}
@Override
public KEY_TYPE DEQUEUE() {
public KEY_TYPE dequeue() {
if(first == last) throw new NoSuchElementException();
KEY_TYPE data = array[first];
#if TYPE_OBJECT
@@ -127,7 +127,7 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_G
}
@Override
public KEY_TYPE DEQUEUE_LAST() {
public KEY_TYPE dequeueLast() {
if(first == last) throw new NoSuchElementException();
if(last == 0) last = array.length;
KEY_TYPE data = array[--last];
@@ -139,7 +139,7 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_G
}
@Override
public KEY_TYPE PEEK(int index) {
public KEY_TYPE peek(int index) {
if(first == last || index < 0 || index > size()) throw new NoSuchElementException();
return array[(first + index) % array.length];
}
@@ -264,25 +264,6 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_G
return input;
}
#if !TYPE_OBJECT
@Override
public CLASS_TYPE[] toArray(CLASS_TYPE[] input) {
if(input == null || input.length < size()) input = NEW_CLASS_ARRAY(size());
if (first <= last) {
for(int i = 0,m=last-first;i<m;i++)
input[i] = KEY_TO_OBJ(array[first + i]);
}
else {
int offset = 0;
for(int i = 0,m=array.length-first;i<m;i++,offset++)
input[i] = KEY_TO_OBJ(array[first + i]);
for(int i = 0;i<last;i++)
input[offset+i] = KEY_TO_OBJ(array[i]);
}
return input;
}
#endif
protected void reduce() {
final int size = size();
if (array.length > MIN_CAPACITY && size <= array.length / 4) resize(size, array.length / 2);
@@ -191,7 +191,7 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY
}
@Override
public KEY_TYPE DEQUEUE() {
public KEY_TYPE dequeue() {
if(size <= 0) throw new NoSuchElementException();
int index = findFirstIndex();
KEY_TYPE value = array[index];
@@ -204,7 +204,7 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY
}
@Override
public KEY_TYPE PEEK(int index) {
public KEY_TYPE peek(int index) {
if(index < 0 || index >= size) throw new NoSuchElementException();
return array[index];
}
@@ -268,15 +268,6 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY
return input;
}
#if !TYPE_OBJECT
@Override
public CLASS_TYPE[] toArray(CLASS_TYPE[] input) {
if(input == null || input.length < size()) input = new CLASS_TYPE[size()];
for(int i = 0;i<size;i++) input[i] = KEY_TO_OBJ(array[i]);
return input;
}
#endif
protected int findFirstIndex() {
if(firstIndex == -1) {
int index = size-1;
@@ -306,7 +297,7 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY
@Override
public KEY_TYPE NEXT() {
return DEQUEUE();
return dequeue();
}
}
}
@@ -207,7 +207,7 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY_
}
@Override
public KEY_TYPE DEQUEUE() {
public KEY_TYPE dequeue() {
if(size <= 0) throw new NoSuchElementException();
KEY_TYPE value = array[0];
array[0] = array[--size];
@@ -219,7 +219,7 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY_
}
@Override
public KEY_TYPE PEEK(int index) {
public KEY_TYPE peek(int index) {
if(index < 0 || index >= size) throw new NoSuchElementException();
return array[index];
}
@@ -265,15 +265,6 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY_
return input;
}
#if !TYPE_OBJECT
@Override
public CLASS_TYPE[] toArray(CLASS_TYPE[] input) {
if(input == null || input.length < size()) input = new CLASS_TYPE[size()];
for(int i = 0;i<size;i++) input[i] = KEY_TO_OBJ(array[i]);
return input;
}
#endif
private class Iter implements ITERATOR KEY_GENERIC_TYPE {
@Override
public boolean hasNext() {
@@ -282,7 +273,7 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY_
@Override
public KEY_TYPE NEXT() {
return DEQUEUE();
return dequeue();
}
}
}
@@ -16,33 +16,10 @@ public interface PRIORITY_DEQUEUE KEY_GENERIC_TYPE extends PRIORITY_QUEUE KEY_GE
* @return the last element inserted
* @throws java.util.NoSuchElementException if no element is in the deque
*/
public KEY_TYPE DEQUEUE_LAST();
public KEY_TYPE dequeueLast();
/**
* Peeking function for the last element
* @return the Last Element within the dequeue without deleting it
*/
public default KEY_TYPE LAST_KEY() { return PEEK(size()-1); }
#if !TYPE_OBJECT
/**
* Boxed Method for the enqueue first method
* @param e the boxed element that should be inserted
*/
@Deprecated
public default void enqueueFirst(CLASS_TYPE e) { enqueueFirst(OBJ_TO_KEY(e)); }
/**
* Boxed Method for the dequeue last method
* @return the last element of the Dequeue
* @throws java.util.NoSuchElementException if no element is in the dequeue
*
*/
@Deprecated
public default CLASS_TYPE dequeueLast() { return KEY_TO_OBJ(DEQUEUE_LAST()); }
/**
* Peeking function for the last element
* @return the Last Element within the dequeue without deleting it as boxed element
*/
@Deprecated
public default CLASS_TYPE last() { return peek(size()-1); }
#endif
public default KEY_TYPE last() { return peek(size()-1); }
}
@@ -5,7 +5,6 @@ import java.util.Comparator;
#else
import speiger.src.collections.PACKAGE.collections.ITERABLE;
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.objects.queues.ObjectPriorityQueue;
#endif
/**
@@ -15,10 +14,9 @@ import speiger.src.collections.objects.queues.ObjectPriorityQueue;
#if TYPE_OBJECT
public interface PRIORITY_QUEUE KEY_GENERIC_TYPE extends Iterable<KEY_TYPE>
#else
public interface PRIORITY_QUEUE KEY_GENERIC_TYPE extends ObjectPriorityQueue<CLASS_TYPE>, ITERABLE KEY_GENERIC_TYPE
public interface PRIORITY_QUEUE KEY_GENERIC_TYPE extends ITERABLE KEY_GENERIC_TYPE
#endif
{
#if TYPE_OBJECT
/**
* @return true if the PriorityQueue is empty
*/
@@ -33,7 +31,6 @@ public interface PRIORITY_QUEUE KEY_GENERIC_TYPE extends ObjectPriorityQueue<CLA
*/
public void clear();
#endif
/**
* Method to insert a element into the PriorityQueue
* @param e the element that should be inserted
@@ -44,19 +41,19 @@ public interface PRIORITY_QUEUE KEY_GENERIC_TYPE extends ObjectPriorityQueue<CLA
* @return a element from the Queue
* @throws java.util.NoSuchElementException if no element is present
*/
public KEY_TYPE DEQUEUE();
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);
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_KEY() { return PEEK(0); }
public default KEY_TYPE first() { return peek(0); }
/**
* Removes the first found element in the queue
@@ -79,7 +76,6 @@ public interface PRIORITY_QUEUE KEY_GENERIC_TYPE extends ObjectPriorityQueue<CLA
/**
* @return the sorter of the Queue, can be null
*/
@PrimitiveOverride
public COMPARATOR KEY_SUPER_GENERIC_TYPE comparator();
/**
@@ -94,27 +90,4 @@ public interface PRIORITY_QUEUE KEY_GENERIC_TYPE extends ObjectPriorityQueue<CLA
* @note if the Type is generic then a Object Array is created instead of a Type Array
*/
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] input);
#if !TYPE_OBJECT
@Deprecated
public default void enqueue(CLASS_TYPE e) { enqueue(OBJ_TO_KEY(e)); }
@Deprecated
public default CLASS_TYPE dequeue() { return KEY_TO_OBJ(DEQUEUE()); }
@Deprecated
public default CLASS_TYPE peek(int index) { return KEY_TO_OBJ(PEEK(index)); }
@Deprecated
public default CLASS_TYPE first() { return peek(0); }
@Deprecated
public default boolean remove(CLASS_TYPE e) { return remove(OBJ_TO_KEY(e)); }
@Deprecated
public default boolean removeLast(CLASS_TYPE e) { return removeLast(OBJ_TO_KEY(e)); }
@Deprecated
public default CLASS_TYPE[] toArray() { return toArray(new CLASS_TYPE[size()]); }
@Deprecated
public CLASS_TYPE[] toArray(CLASS_TYPE[] input);
#endif
}
}