PriorityQueues no longer extends Object Variant.

This commit is contained in:
Speiger 2021-06-23 19:22:19 +02:00
parent a669f69d99
commit 357b40e670
8 changed files with 33 additions and 121 deletions

View File

@ -1,8 +1,9 @@
# Changelog of versions # Changelog of versions
## Version 0.3.0 ## Version 0.3.0 (Breaking 0.2.0)
- Added: Stack.isEmpty was missing - Added: Stack.isEmpty was missing
- Changed: remove/removeLast/enqueue/enqueueFirst no longer use Type Suffixes - Changed: remove/removeLast/enqueue/enqueueFirst no longer use Type Suffixes
- Removed: Suffixes for unmodifiable & synchronize functions. - Removed: Suffixes for unmodifiable & synchronize functions.
- Changed: Primitive Stacks no longer depend on the base Stack class. Because seriously not needed. - Changed: Primitive Stacks no longer depend on the base Stack class. Because seriously not needed.
- Changed: PriorityQueues no longer extends Object Variant.

View File

@ -13,6 +13,11 @@ But its focus is a different one.
- SplitIterators - SplitIterators
- Iterators - Iterators
# Notes about Versions
Any 0.x.0 version (Minor) can be reason for massive changes including API.
To ensure that problems can be dealt with even if it is breaking the current API.
Any breaking changes will be Documented (once 1.0 is released)
# How to install # How to install
Using Gradle: Using Gradle:
```gradle ```gradle

View File

@ -115,7 +115,7 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_G
} }
@Override @Override
public KEY_TYPE DEQUEUE() { public KEY_TYPE dequeue() {
if(first == last) throw new NoSuchElementException(); if(first == last) throw new NoSuchElementException();
KEY_TYPE data = array[first]; KEY_TYPE data = array[first];
#if TYPE_OBJECT #if TYPE_OBJECT
@ -127,7 +127,7 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_G
} }
@Override @Override
public KEY_TYPE DEQUEUE_LAST() { public KEY_TYPE dequeueLast() {
if(first == last) throw new NoSuchElementException(); if(first == last) throw new NoSuchElementException();
if(last == 0) last = array.length; if(last == 0) last = array.length;
KEY_TYPE data = array[--last]; KEY_TYPE data = array[--last];
@ -139,7 +139,7 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_G
} }
@Override @Override
public KEY_TYPE PEEK(int index) { public KEY_TYPE peek(int index) {
if(first == last || index < 0 || index > size()) throw new NoSuchElementException(); if(first == last || index < 0 || index > size()) throw new NoSuchElementException();
return array[(first + index) % array.length]; 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; 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() { protected void reduce() {
final int size = size(); final int size = size();
if (array.length > MIN_CAPACITY && size <= array.length / 4) resize(size, array.length / 2); if (array.length > MIN_CAPACITY && size <= array.length / 4) resize(size, array.length / 2);

View File

@ -191,7 +191,7 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY
} }
@Override @Override
public KEY_TYPE DEQUEUE() { public KEY_TYPE dequeue() {
if(size <= 0) throw new NoSuchElementException(); if(size <= 0) throw new NoSuchElementException();
int index = findFirstIndex(); int index = findFirstIndex();
KEY_TYPE value = array[index]; KEY_TYPE value = array[index];
@ -204,7 +204,7 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY
} }
@Override @Override
public KEY_TYPE PEEK(int index) { public KEY_TYPE peek(int index) {
if(index < 0 || index >= size) throw new NoSuchElementException(); if(index < 0 || index >= size) throw new NoSuchElementException();
return array[index]; return array[index];
} }
@ -268,15 +268,6 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY
return input; 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() { protected int findFirstIndex() {
if(firstIndex == -1) { if(firstIndex == -1) {
int index = size-1; int index = size-1;
@ -306,7 +297,7 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY
@Override @Override
public KEY_TYPE NEXT() { public KEY_TYPE NEXT() {
return DEQUEUE(); return dequeue();
} }
} }
} }

View File

@ -207,7 +207,7 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY_
} }
@Override @Override
public KEY_TYPE DEQUEUE() { public KEY_TYPE dequeue() {
if(size <= 0) throw new NoSuchElementException(); if(size <= 0) throw new NoSuchElementException();
KEY_TYPE value = array[0]; KEY_TYPE value = array[0];
array[0] = array[--size]; array[0] = array[--size];
@ -219,7 +219,7 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY_
} }
@Override @Override
public KEY_TYPE PEEK(int index) { public KEY_TYPE peek(int index) {
if(index < 0 || index >= size) throw new NoSuchElementException(); if(index < 0 || index >= size) throw new NoSuchElementException();
return array[index]; return array[index];
} }
@ -265,15 +265,6 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY_
return input; 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 { private class Iter implements ITERATOR KEY_GENERIC_TYPE {
@Override @Override
public boolean hasNext() { public boolean hasNext() {
@ -282,7 +273,7 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY_
@Override @Override
public KEY_TYPE NEXT() { public KEY_TYPE NEXT() {
return DEQUEUE(); return dequeue();
} }
} }
} }

View File

@ -16,33 +16,10 @@ public interface PRIORITY_DEQUEUE KEY_GENERIC_TYPE extends PRIORITY_QUEUE KEY_GE
* @return the last element inserted * @return the last element inserted
* @throws java.util.NoSuchElementException if no element is in the deque * @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 * Peeking function for the last element
* @return the Last Element within the dequeue without deleting it * @return the Last Element within the dequeue without deleting it
*/ */
public default KEY_TYPE LAST_KEY() { return PEEK(size()-1); } public default KEY_TYPE last() { 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
} }

View File

@ -5,7 +5,6 @@ import java.util.Comparator;
#else #else
import speiger.src.collections.PACKAGE.collections.ITERABLE; import speiger.src.collections.PACKAGE.collections.ITERABLE;
import speiger.src.collections.PACKAGE.functions.COMPARATOR; import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.objects.queues.ObjectPriorityQueue;
#endif #endif
/** /**
@ -15,10 +14,9 @@ import speiger.src.collections.objects.queues.ObjectPriorityQueue;
#if TYPE_OBJECT #if TYPE_OBJECT
public interface PRIORITY_QUEUE KEY_GENERIC_TYPE extends Iterable<KEY_TYPE> public interface PRIORITY_QUEUE KEY_GENERIC_TYPE extends Iterable<KEY_TYPE>
#else #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 #endif
{ {
#if TYPE_OBJECT
/** /**
* @return true if the PriorityQueue is empty * @return true if the PriorityQueue is empty
*/ */
@ -33,7 +31,6 @@ public interface PRIORITY_QUEUE KEY_GENERIC_TYPE extends ObjectPriorityQueue<CLA
*/ */
public void clear(); public void clear();
#endif
/** /**
* Method to insert a element into the PriorityQueue * Method to insert a element into the PriorityQueue
* @param e the element that should be inserted * @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 * @return a element from the Queue
* @throws java.util.NoSuchElementException if no element is present * @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. * Peeking function to see whats inside the queue.
* @param index of the element that is requested to be viewed. * @param index of the element that is requested to be viewed.
* @return the element that is requested * @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 * Shows the element that is to be returned next
* @return the first element in the Queue * @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 * 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 * @return the sorter of the Queue, can be null
*/ */
@PrimitiveOverride
public COMPARATOR KEY_SUPER_GENERIC_TYPE comparator(); 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 * @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); 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
}

View File

@ -29,11 +29,11 @@ public abstract class BaseIntPriorityQueueTest extends BaseIntIterableTest
queue.enqueue(i); queue.enqueue(i);
} }
for(int i = 0;i<100;i++) { for(int i = 0;i<100;i++) {
Assert.assertEquals(i, queue.dequeueInt()); Assert.assertEquals(i, queue.dequeue());
} }
queue = create(TEST_ARRAY); queue = create(TEST_ARRAY);
for(int i = 0;i<100;i++) { for(int i = 0;i<100;i++) {
Assert.assertEquals(i, queue.dequeueInt()); Assert.assertEquals(i, queue.dequeue());
} }
} }
} }
@ -47,13 +47,13 @@ public abstract class BaseIntPriorityQueueTest extends BaseIntIterableTest
} }
if(isUnsortedRead()) { if(isUnsortedRead()) {
for(int i = 0;i<100;i++) { for(int i = 0;i<100;i++) {
int value = queue.peekInt(i); int value = queue.peek(i);
Assert.assertTrue(value >= 0 && value < 100); Assert.assertTrue(value >= 0 && value < 100);
} }
} }
else { else {
for(int i = 0;i<100;i++) { for(int i = 0;i<100;i++) {
Assert.assertEquals(i, queue.peekInt(i)); Assert.assertEquals(i, queue.peek(i));
} }
} }
} }
@ -68,16 +68,16 @@ public abstract class BaseIntPriorityQueueTest extends BaseIntIterableTest
} }
queue.remove(40); queue.remove(40);
for(int i = 0;i<99;i++) { for(int i = 0;i<99;i++) {
if(i >= 40) Assert.assertEquals(i + 1, queue.dequeueInt()); if(i >= 40) Assert.assertEquals(i + 1, queue.dequeue());
else Assert.assertEquals(i, queue.dequeueInt()); else Assert.assertEquals(i, queue.dequeue());
} }
for(int i = 0;i<100;i++) { for(int i = 0;i<100;i++) {
queue.enqueue(i); queue.enqueue(i);
} }
queue.removeLast(40); queue.removeLast(40);
for(int i = 0;i<99;i++) { for(int i = 0;i<99;i++) {
if(i >= 40) Assert.assertEquals(i + 1, queue.dequeueInt()); if(i >= 40) Assert.assertEquals(i + 1, queue.dequeue());
else Assert.assertEquals(i, queue.dequeueInt()); else Assert.assertEquals(i, queue.dequeue());
} }
} }
} }
@ -102,28 +102,21 @@ public abstract class BaseIntPriorityQueueTest extends BaseIntIterableTest
Assert.assertArrayEquals(array, nonData); Assert.assertArrayEquals(array, nonData);
} }
else { else {
Integer[] ref = IntArrays.wrap(TEST_ARRAY);
Integer[] shiftArray = new Integer[100];
int[] shiftPrimArray = new int[100]; int[] shiftPrimArray = new int[100];
for(int i = 0; i < 100; i++) { for(int i = 0; i < 100; i++) {
queue.enqueue(i); queue.enqueue(i);
shiftPrimArray[(i+80) % 100] = i; shiftPrimArray[(i+80) % 100] = i;
shiftArray[(i+80) % 100] = Integer.valueOf(i);
} }
Assert.assertArrayEquals(ref, queue.toArray());
Assert.assertArrayEquals(ref, queue.toArray(new Integer[100]));
Assert.assertArrayEquals(ref, queue.toArray(null));
Assert.assertArrayEquals(TEST_ARRAY, queue.toIntArray()); Assert.assertArrayEquals(TEST_ARRAY, queue.toIntArray());
Assert.assertArrayEquals(TEST_ARRAY, queue.toIntArray(new int[100])); Assert.assertArrayEquals(TEST_ARRAY, queue.toIntArray(new int[100]));
Assert.assertArrayEquals(TEST_ARRAY, queue.toIntArray(null)); Assert.assertArrayEquals(TEST_ARRAY, queue.toIntArray(null));
IntPriorityQueue other = create(queue.toIntArray()); IntPriorityQueue other = create(queue.toIntArray());
for(int i = 0;i<20;i++) { for(int i = 0;i<20;i++) {
other.dequeueInt(); other.dequeue();
other.enqueue(i); other.enqueue(i);
} }
Assert.assertArrayEquals(shiftPrimArray, other.toIntArray()); Assert.assertArrayEquals(shiftPrimArray, other.toIntArray());
Assert.assertArrayEquals(shiftPrimArray, other.toIntArray(new int[100])); Assert.assertArrayEquals(shiftPrimArray, other.toIntArray(new int[100]));
Assert.assertArrayEquals(shiftArray, other.toArray());
} }
} }
} }