package dasherJava.core.collections;
import java.util.ArrayList;
import java.util.Collections;
public class DynamicPriorityQueue<E extends Comparable<E>> extends ArrayList<E> {
//A simple priority queue implementation that finds the minimum (or maximum if invert is true)
//by iterating over all elements. This is not very efficient, but is required to handle
//dynamically and frequently changing priorities.
//Implementation note: Is extending ArrayList the best option, or are there more efficient ones?
private final boolean invert;
public DynamicPriorityQueue(boolean invert) {
this.invert=invert;
}
public E peek() { //Returns the head of the queue without removing it
return invert ? Collections.max(this) : Collections.min(this);
}
public E poll() { //Removes and returns the head of the queue
E head = invert ? Collections.max(this) : Collections.min(this);
remove(head);
return head;
}
public void addAll(E[] elements) {
for (E e : elements) {
add(e);
}
}
public void removeAll(E[] elements) {
for (E e : elements) {
remove(e);
}
}
}