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=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=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(); } } }