Project is now buildable.
-Moved: Code generation is in its own sourceset. -Fixed: Bugs that caused that the project isnt buildable. -Changed: Made build.gradle to a standard.
This commit is contained in:
+275
@@ -0,0 +1,275 @@
|
||||
package speiger.src.collections.PACKAGE.queues;
|
||||
|
||||
#if TYPE_OBJECT
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
#endif
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import speiger.src.collections.PACKAGE.collections.ITERATOR;
|
||||
#if !TYPE_OBJECT
|
||||
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
|
||||
#endif
|
||||
import speiger.src.collections.utils.ITrimmable;
|
||||
|
||||
public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_GENERIC_TYPE, ITrimmable
|
||||
{
|
||||
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
|
||||
public static final int MIN_CAPACITY = 4;
|
||||
protected transient KEY_TYPE[] array;
|
||||
protected int first;
|
||||
protected int last;
|
||||
|
||||
public ARRAY_FIFO_QUEUE(KEY_TYPE[] values) {
|
||||
this(values, 0, values.length);
|
||||
}
|
||||
|
||||
public ARRAY_FIFO_QUEUE(KEY_TYPE[] values, int size) {
|
||||
this(values, 0, size);
|
||||
}
|
||||
|
||||
public ARRAY_FIFO_QUEUE(KEY_TYPE[] values, int offset, int size) {
|
||||
if (values.length < size) throw new IllegalArgumentException("Initial array (" + values.length + ") is smaller then the expected size (" + size + ")");
|
||||
if(values.length <= 0) values = NEW_KEY_ARRAY(1);
|
||||
array = values;
|
||||
first = offset;
|
||||
last = (offset + size) % array.length;
|
||||
if(array.length == size) expand();
|
||||
}
|
||||
|
||||
public ARRAY_FIFO_QUEUE(int capacity) {
|
||||
if (capacity < 0) throw new IllegalArgumentException("Initial capacity (" + capacity + ") is negative");
|
||||
array = NEW_KEY_ARRAY(Math.max(1, capacity));
|
||||
}
|
||||
|
||||
public ARRAY_FIFO_QUEUE() {
|
||||
this(MIN_CAPACITY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITERATOR KEY_GENERIC_TYPE iterator() {
|
||||
return new Iter();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
final int apparentLength = last - first;
|
||||
return apparentLength >= 0 ? apparentLength : array.length + apparentLength;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
#if TYPE_OBJECT
|
||||
Arrays.fill(array, null);
|
||||
#endif
|
||||
first = last = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ENQUEUE(KEY_TYPE e) {
|
||||
array[last] = e;
|
||||
last = ++last % array.length;
|
||||
if(last == first) expand();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ENQUEUE_FIRST(KEY_TYPE e) {
|
||||
if(first == 0) first = array.length;
|
||||
array[--first] = e;
|
||||
if(first == last) expand();
|
||||
}
|
||||
|
||||
@Override
|
||||
public KEY_TYPE DEQUEUE() {
|
||||
if(first == last) throw new NoSuchElementException();
|
||||
KEY_TYPE data = array[first];
|
||||
#if TYPE_OBJECT
|
||||
array[first] = null;
|
||||
#endif
|
||||
first = ++first % array.length;
|
||||
reduce();
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KEY_TYPE DEQUEUE_LAST() {
|
||||
if(first == last) throw new NoSuchElementException();
|
||||
if(last == 0) last = array.length;
|
||||
KEY_TYPE data = array[--last];
|
||||
#if TYPE_OBJECT
|
||||
array[last] = null;
|
||||
#endif
|
||||
reduce();
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KEY_TYPE PEEK(int index) {
|
||||
if(first == last || index < 0 || index > size()) throw new NoSuchElementException();
|
||||
return array[(first + index) % array.length];
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean REMOVE(KEY_TYPE e) {
|
||||
if(first == last) return false;
|
||||
for(int i = 0,m=size();i<m;i++) {
|
||||
int index = (first + i) % array.length;
|
||||
if(e == array[index])
|
||||
return removeIndex(index);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean REMOVE_LAST(KEY_TYPE e) {
|
||||
if(first == last) return false;
|
||||
if(first == last) return false;
|
||||
for(int i = size()-1;i>=0;i--) {
|
||||
int index = (first + i) % array.length;
|
||||
if(e == array[index])
|
||||
return removeIndex(index);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected boolean removeIndex(int index) {
|
||||
if(first >= last ? index < first && index > last : index < first || index > last) return false;
|
||||
if(index == first) {
|
||||
#if TYPE_OBJECT
|
||||
array[first] = null;
|
||||
#endif
|
||||
first++;
|
||||
}
|
||||
else if(index == last) {
|
||||
last--;
|
||||
#if TYPE_OBJECT
|
||||
array[last] = null;
|
||||
#endif
|
||||
}
|
||||
else if(index > last) {
|
||||
System.arraycopy(array, first, array, first+1, (index - first));
|
||||
#if TYPE_OBJECT
|
||||
array[first] = null;
|
||||
#endif
|
||||
first = ++first % array.length;
|
||||
}
|
||||
else if(index < first) {
|
||||
System.arraycopy(array, index+1, array, index, (last - index) - 1);
|
||||
#if TYPE_OBJECT
|
||||
array[last] = null;
|
||||
#endif
|
||||
if(--last < 0) last += array.length;
|
||||
}
|
||||
else {
|
||||
if(index - first < last - index) {
|
||||
System.arraycopy(array, first, array, first+1, (index - first));
|
||||
#if TYPE_OBJECT
|
||||
array[first] = null;
|
||||
#endif
|
||||
first = ++first % array.length;
|
||||
}
|
||||
else {
|
||||
System.arraycopy(array, index+1, array, index, (last - index) - 1);
|
||||
#if TYPE_OBJECT
|
||||
array[last] = null;
|
||||
#endif
|
||||
if(--last < 0) last += array.length;
|
||||
}
|
||||
}
|
||||
reduce();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChanged() {}
|
||||
|
||||
@Override
|
||||
public COMPARATOR KEY_SUPER_GENERIC_TYPE comparator() { return null; }
|
||||
|
||||
@Override
|
||||
public boolean trim(int size) {
|
||||
int newSize = Math.max(size, size());
|
||||
if(newSize >= array.length) return false;
|
||||
KEY_TYPE[] newArray = NEW_KEY_ARRAY(newSize);
|
||||
if(first <= last) System.arraycopy(array, first, newArray, 0, last - first);
|
||||
else {
|
||||
System.arraycopy(array, first, newArray, 0, array.length - first);
|
||||
System.arraycopy(array, 0, newArray, array.length - first, last);
|
||||
}
|
||||
first = 0;
|
||||
last = size();
|
||||
array = newArray;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] input) {
|
||||
if(input == null || input.length < size()) input = NEW_KEY_ARRAY(size());
|
||||
if (first <= last) System.arraycopy(array, first, input, 0, last - first);
|
||||
else {
|
||||
System.arraycopy(array, first, input, 0, array.length - first);
|
||||
System.arraycopy(array, 0, input, array.length - first, last);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
protected void expand() {
|
||||
resize(array.length, (int)Math.min(MAX_ARRAY_SIZE, 2L * array.length));
|
||||
}
|
||||
|
||||
protected final void resize(int oldSize, int newSize) {
|
||||
KEY_TYPE[] newArray = NEW_KEY_ARRAY(newSize);
|
||||
if(first >= last) {
|
||||
if(oldSize != 0)
|
||||
{
|
||||
System.arraycopy(array, first, newArray, 0, array.length - first);
|
||||
System.arraycopy(array, 0, newArray, array.length - first, last);
|
||||
}
|
||||
}
|
||||
else System.arraycopy(array, first, newArray, 0, last-first);
|
||||
first = 0;
|
||||
last = oldSize;
|
||||
array = newArray;
|
||||
}
|
||||
|
||||
private class Iter implements ITERATOR KEY_GENERIC_TYPE
|
||||
{
|
||||
int index = first;
|
||||
@Override
|
||||
public boolean hasNext()
|
||||
{
|
||||
return index != last;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KEY_TYPE NEXT() {
|
||||
KEY_TYPE value = array[index];
|
||||
removeIndex(index++);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
+220
@@ -0,0 +1,220 @@
|
||||
package speiger.src.collections.PACKAGE.queues;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.NoSuchElementException;
|
||||
#if TYPE_OBJECT
|
||||
import java.util.Comparator;
|
||||
import java.util.Objects;
|
||||
#endif
|
||||
|
||||
import speiger.src.collections.PACKAGE.collections.COLLECTION;
|
||||
import speiger.src.collections.PACKAGE.collections.ITERATOR;
|
||||
#if !TYPE_OBJECT
|
||||
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
|
||||
#endif
|
||||
import speiger.src.collections.PACKAGE.utils.ARRAYS;
|
||||
|
||||
public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY_GENERIC_TYPE
|
||||
{
|
||||
protected transient KEY_TYPE[] array = EMPTY_KEY_ARRAY;
|
||||
protected int size;
|
||||
protected int firstIndex = -1;
|
||||
protected COMPARATOR KEY_SUPER_GENERIC_TYPE comparator;
|
||||
|
||||
public ARRAY_PRIORITY_QUEUE() {
|
||||
this(0, null);
|
||||
}
|
||||
|
||||
public ARRAY_PRIORITY_QUEUE(COMPARATOR KEY_SUPER_GENERIC_TYPE comp) {
|
||||
this(0, comp);
|
||||
}
|
||||
|
||||
public ARRAY_PRIORITY_QUEUE(int size, COMPARATOR KEY_SUPER_GENERIC_TYPE comp) {
|
||||
if(size > 0) array = NEW_KEY_ARRAY(size);
|
||||
this.size = size;
|
||||
comparator = comp;
|
||||
}
|
||||
|
||||
public ARRAY_PRIORITY_QUEUE(KEY_TYPE[] array) {
|
||||
this(array, array.length);
|
||||
}
|
||||
|
||||
public ARRAY_PRIORITY_QUEUE(KEY_TYPE[] array, int size) {
|
||||
this.array = Arrays.copyOf(array, size);
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public ARRAY_PRIORITY_QUEUE(KEY_TYPE[] array, COMPARATOR KEY_SUPER_GENERIC_TYPE comp) {
|
||||
this(array, array.length, comp);
|
||||
}
|
||||
|
||||
public ARRAY_PRIORITY_QUEUE(KEY_TYPE[] array, int size, COMPARATOR KEY_SUPER_GENERIC_TYPE comp) {
|
||||
this.array = Arrays.copyOf(array, size);
|
||||
this.size = size;
|
||||
this.comparator = comp;
|
||||
}
|
||||
|
||||
public ARRAY_PRIORITY_QUEUE(COLLECTION KEY_GENERIC_TYPE c) {
|
||||
array = CAST_KEY_ARRAY c.TO_ARRAY();
|
||||
size = c.size();
|
||||
}
|
||||
|
||||
public ARRAY_PRIORITY_QUEUE(COLLECTION KEY_GENERIC_TYPE c, COMPARATOR KEY_SUPER_GENERIC_TYPE comp) {
|
||||
array = CAST_KEY_ARRAY c.TO_ARRAY();
|
||||
size = c.size();
|
||||
comparator = comp;
|
||||
}
|
||||
|
||||
public static GENERIC_KEY_BRACES ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE wrap(KEY_TYPE[] array) {
|
||||
return wrap(array, array.length);
|
||||
}
|
||||
|
||||
public static GENERIC_KEY_BRACES ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE wrap(KEY_TYPE[] array, int size) {
|
||||
ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE queue = new ARRAY_PRIORITY_QUEUEBRACES();
|
||||
queue.array = array;
|
||||
queue.size = size;
|
||||
return queue;
|
||||
}
|
||||
|
||||
public static GENERIC_KEY_BRACES ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE wrap(KEY_TYPE[] array, COMPARATOR KEY_SUPER_GENERIC_TYPE comp) {
|
||||
return wrap(array, array.length, comp);
|
||||
}
|
||||
|
||||
public static GENERIC_KEY_BRACES ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE wrap(KEY_TYPE[] array, int size, COMPARATOR KEY_SUPER_GENERIC_TYPE comp) {
|
||||
ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE queue = new ARRAY_PRIORITY_QUEUEBRACES(comp);
|
||||
queue.array = array;
|
||||
queue.size = size;
|
||||
return queue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ENQUEUE(KEY_TYPE e) {
|
||||
if(size == array.length) array = Arrays.copyOf(array, size+1);
|
||||
if(firstIndex != -1){
|
||||
int compare = comparator == null ? COMPAREABLE_TO_KEY(e, array[firstIndex]) : comparator.compare(e, array[firstIndex]);
|
||||
if(compare < 0) firstIndex = size;
|
||||
else if(compare > 0) firstIndex = -1;
|
||||
}
|
||||
array[size++] = e;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KEY_TYPE DEQUEUE() {
|
||||
if(size <= 0) throw new NoSuchElementException();
|
||||
int index = findFirstIndex();
|
||||
KEY_TYPE value = array[index];
|
||||
if(index != --size) System.arraycopy(array, index+1, array, index, size - index);
|
||||
#if TYPE_OBJECT
|
||||
array[size] = null;
|
||||
#endif
|
||||
firstIndex = -1;
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KEY_TYPE PEEK(int index) {
|
||||
if(index < 0 || index >= size) throw new NoSuchElementException();
|
||||
return array[index];
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean REMOVE(KEY_TYPE e) {
|
||||
for(int i = 0;i<size;i++)
|
||||
if(KEY_EQUALS(e, array[i])) return removeIndex(i);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean REMOVE_LAST(KEY_TYPE e) {
|
||||
for(int i = size-1;i>=0;i--)
|
||||
if(KEY_EQUALS(e, array[i])) return removeIndex(i);
|
||||
return false;
|
||||
}
|
||||
|
||||
protected boolean removeIndex(int index) {
|
||||
if(index != --size) System.arraycopy(array, index+1, array, index, size - index);
|
||||
#if TYPE_OBJECT
|
||||
array[size] = null;
|
||||
#endif
|
||||
if(index == firstIndex) firstIndex = -1;
|
||||
else if(firstIndex != -1 && index >= firstIndex) firstIndex--;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChanged() {
|
||||
firstIndex = -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
#if TYPE_OBJECT
|
||||
Arrays.fill(array, null);
|
||||
#endif
|
||||
size = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITERATOR KEY_GENERIC_TYPE iterator() {
|
||||
return new Iter();
|
||||
}
|
||||
|
||||
@Override
|
||||
public COMPARATOR KEY_SUPER_GENERIC_TYPE comparator() {
|
||||
return comparator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] input) {
|
||||
if(input == null || input.length < size()) input = NEW_KEY_ARRAY(size());
|
||||
System.arraycopy(array, 0, input, 0, size());
|
||||
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;
|
||||
KEY_TYPE value = array[index];
|
||||
if(comparator == null) {
|
||||
for(int i = index;i>=0;i--) {
|
||||
if(COMPAREABLE_TO_KEY(array[i], value) < 0)
|
||||
value = array[index = i];
|
||||
}
|
||||
}
|
||||
else {
|
||||
for(int i = index;i>=0;i--) {
|
||||
if(comparator.compare(array[i], value) < 0)
|
||||
value = array[index = i];
|
||||
}
|
||||
}
|
||||
firstIndex = index;
|
||||
}
|
||||
return firstIndex;
|
||||
}
|
||||
|
||||
public class Iter implements ITERATOR KEY_GENERIC_TYPE {
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return !isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public KEY_TYPE NEXT() {
|
||||
return DEQUEUE();
|
||||
}
|
||||
}
|
||||
}
|
||||
+199
@@ -0,0 +1,199 @@
|
||||
package speiger.src.collections.PACKAGE.queues;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.NoSuchElementException;
|
||||
#if TYPE_OBJECT
|
||||
import java.util.Comparator;
|
||||
import java.util.Objects;
|
||||
#endif
|
||||
|
||||
import speiger.src.collections.PACKAGE.collections.COLLECTION;
|
||||
import speiger.src.collections.PACKAGE.collections.ITERATOR;
|
||||
#if !TYPE_OBJECT
|
||||
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
|
||||
#endif
|
||||
import speiger.src.collections.PACKAGE.utils.ARRAYS;
|
||||
|
||||
public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY_GENERIC_TYPE
|
||||
{
|
||||
protected transient KEY_TYPE[] array = EMPTY_KEY_ARRAY;
|
||||
protected int size;
|
||||
protected COMPARATOR KEY_SUPER_GENERIC_TYPE comparator;
|
||||
|
||||
public HEAP_PRIORITY_QUEUE() {
|
||||
this(0, null);
|
||||
}
|
||||
|
||||
public HEAP_PRIORITY_QUEUE(COMPARATOR KEY_SUPER_GENERIC_TYPE comp) {
|
||||
this(0, comp);
|
||||
}
|
||||
|
||||
public HEAP_PRIORITY_QUEUE(int size, COMPARATOR KEY_SUPER_GENERIC_TYPE comp) {
|
||||
if(size > 0) array = NEW_KEY_ARRAY(size);
|
||||
this.size = size;
|
||||
comparator = comp;
|
||||
}
|
||||
|
||||
public HEAP_PRIORITY_QUEUE(KEY_TYPE[] array) {
|
||||
this(array, array.length);
|
||||
}
|
||||
|
||||
public HEAP_PRIORITY_QUEUE(KEY_TYPE[] array, int size) {
|
||||
this.array = Arrays.copyOf(array, size);
|
||||
this.size = size;
|
||||
ARRAYS.heapify(array, size, null);
|
||||
}
|
||||
|
||||
public HEAP_PRIORITY_QUEUE(KEY_TYPE[] array, COMPARATOR KEY_SUPER_GENERIC_TYPE comp) {
|
||||
this(array, array.length, comp);
|
||||
}
|
||||
|
||||
public HEAP_PRIORITY_QUEUE(KEY_TYPE[] array, int size, COMPARATOR KEY_SUPER_GENERIC_TYPE comp) {
|
||||
this.array = Arrays.copyOf(array, size);
|
||||
this.size = size;
|
||||
comparator = comp;
|
||||
ARRAYS.heapify(array, size, comp);
|
||||
}
|
||||
|
||||
public HEAP_PRIORITY_QUEUE(COLLECTION KEY_GENERIC_TYPE c) {
|
||||
array = CAST_KEY_ARRAY c.TO_ARRAY();
|
||||
size = c.size();
|
||||
ARRAYS.heapify(array, size, null);
|
||||
}
|
||||
|
||||
public HEAP_PRIORITY_QUEUE(COLLECTION KEY_GENERIC_TYPE c, COMPARATOR KEY_SUPER_GENERIC_TYPE comp) {
|
||||
array = CAST_KEY_ARRAY c.TO_ARRAY();
|
||||
size = c.size();
|
||||
comparator = comp;
|
||||
ARRAYS.heapify(array, size, comp);
|
||||
}
|
||||
|
||||
public static GENERIC_KEY_BRACES HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE wrap(KEY_TYPE[] array) {
|
||||
return wrap(array, array.length);
|
||||
}
|
||||
|
||||
public static GENERIC_KEY_BRACES HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE wrap(KEY_TYPE[] array, int size) {
|
||||
HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE queue = new HEAP_PRIORITY_QUEUEBRACES();
|
||||
queue.array = array;
|
||||
queue.size = size;
|
||||
ARRAYS.heapify(array, size, null);
|
||||
return queue;
|
||||
}
|
||||
|
||||
public static GENERIC_KEY_BRACES HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE wrap(KEY_TYPE[] array, COMPARATOR KEY_SUPER_GENERIC_TYPE comp) {
|
||||
return wrap(array, array.length, comp);
|
||||
}
|
||||
|
||||
public static GENERIC_KEY_BRACES HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE wrap(KEY_TYPE[] array, int size, COMPARATOR KEY_SUPER_GENERIC_TYPE comp) {
|
||||
HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE queue = new HEAP_PRIORITY_QUEUEBRACES(comp);
|
||||
queue.array = array;
|
||||
queue.size = size;
|
||||
ARRAYS.heapify(array, size, comp);
|
||||
return queue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
#if TYPE_OBJECT
|
||||
Arrays.fill(array, null);
|
||||
#endif
|
||||
size = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITERATOR KEY_GENERIC_TYPE iterator() {
|
||||
return new Iter();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ENQUEUE(KEY_TYPE e) {
|
||||
if(size == array.length) array = Arrays.copyOf(array, size + 1);
|
||||
array[size++] = e;
|
||||
ARRAYS.shiftUp(array, size-1, comparator);
|
||||
}
|
||||
|
||||
@Override
|
||||
public KEY_TYPE DEQUEUE() {
|
||||
if(size <= 0) throw new NoSuchElementException();
|
||||
KEY_TYPE value = array[0];
|
||||
array[0] = array[--size];
|
||||
#if TYPE_OBJECT
|
||||
array[size] = null;
|
||||
#endif
|
||||
if(size != 0) ARRAYS.shiftDown(array, size, 0, comparator);
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KEY_TYPE PEEK(int index) {
|
||||
if(index < 0 || index >= size) throw new NoSuchElementException();
|
||||
return array[index];
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean REMOVE(KEY_TYPE e) {
|
||||
for(int i = 0;i<size;i++)
|
||||
if(KEY_EQUALS(e, array[i])) return removeIndex(i);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean REMOVE_LAST(KEY_TYPE e) {
|
||||
for(int i = size-1;i>=0;i--)
|
||||
if(KEY_EQUALS(e, array[i])) return removeIndex(i);
|
||||
return false;
|
||||
}
|
||||
|
||||
protected boolean removeIndex(int index) {
|
||||
array[index] = array[--size];
|
||||
#if TYPE_OBJECT
|
||||
array[size] = null;
|
||||
#endif
|
||||
if(size != index) ARRAYS.shiftDown(array, size, index, comparator);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChanged() {
|
||||
if(size <= 0) return;
|
||||
ARRAYS.shiftDown(array, size, 0, comparator);
|
||||
}
|
||||
|
||||
@Override
|
||||
public COMPARATOR KEY_SUPER_GENERIC_TYPE comparator() {
|
||||
return comparator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] input) {
|
||||
if(input == null || input.length < size()) input = NEW_KEY_ARRAY(size());
|
||||
System.arraycopy(array, 0, input, 0, size());
|
||||
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
|
||||
public class Iter implements ITERATOR KEY_GENERIC_TYPE {
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return !isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public KEY_TYPE NEXT() {
|
||||
return DEQUEUE();
|
||||
}
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package speiger.src.collections.PACKAGE.queues;
|
||||
|
||||
public interface PRIORITY_DEQUEUE KEY_GENERIC_TYPE extends PRIORITY_QUEUE KEY_GENERIC_TYPE
|
||||
{
|
||||
public void ENQUEUE_FIRST(KEY_TYPE e);
|
||||
public KEY_TYPE DEQUEUE_LAST();
|
||||
public default KEY_TYPE LAST_KEY() { return PEEK(size()-1); }
|
||||
|
||||
#if !TYPE_OBJECT
|
||||
public default void enqueueFirst(CLASS_TYPE e) { ENQUEUE_FIRST(OBJ_TO_KEY(e)); }
|
||||
public default CLASS_TYPE dequeueLast() { return KEY_TO_OBJ(DEQUEUE_LAST()); }
|
||||
public default CLASS_TYPE last() { return peek(size()-1); }
|
||||
#endif
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
package speiger.src.collections.PACKAGE.queues;
|
||||
|
||||
#if TYPE_OBJECT
|
||||
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
|
||||
|
||||
#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
|
||||
#endif
|
||||
{
|
||||
#if TYPE_OBJECT
|
||||
public default boolean isEmpty() { return size() <= 0; }
|
||||
public int size();
|
||||
public void clear();
|
||||
|
||||
#endif
|
||||
public void ENQUEUE(KEY_TYPE e);
|
||||
public KEY_TYPE DEQUEUE();
|
||||
|
||||
public KEY_TYPE PEEK(int index);
|
||||
public default KEY_TYPE FIRST_KEY() { return PEEK(0); }
|
||||
|
||||
public boolean REMOVE(KEY_TYPE e);
|
||||
public boolean REMOVE_LAST(KEY_TYPE e);
|
||||
|
||||
public void onChanged();
|
||||
|
||||
@PrimitiveOverride
|
||||
public COMPARATOR KEY_SUPER_GENERIC_TYPE comparator();
|
||||
|
||||
public default KEY_TYPE[] TO_ARRAY() { return TO_ARRAY(NEW_KEY_ARRAY(size())); }
|
||||
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] input);
|
||||
|
||||
#if !TYPE_OBJECT
|
||||
public default void enqueue(CLASS_TYPE e) { ENQUEUE(OBJ_TO_KEY(e)); }
|
||||
public default CLASS_TYPE dequeue() { return KEY_TO_OBJ(DEQUEUE()); }
|
||||
|
||||
public default CLASS_TYPE peek(int index) { return KEY_TO_OBJ(PEEK(index)); }
|
||||
public default CLASS_TYPE first() { return peek(0); }
|
||||
|
||||
public default boolean remove(CLASS_TYPE e) { return REMOVE(OBJ_TO_KEY(e)); }
|
||||
public default boolean removeLast(CLASS_TYPE e) { return REMOVE_LAST(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
|
||||
}
|
||||
Reference in New Issue
Block a user