public class MysteryUpdatablePriorityQueueImplementation extends java.lang.Object implements UpdatablePriorityQueue<java.lang.Integer,java.lang.Double>
This implementation is optimized for one particular, specialized use: when the items are densely-packed unique integers in some fixed range [0, N] (for example, node IDs in a graph) and the priorities are Doubles.
Any method call that attempts to refer to an item less than zero or greater than maxValue (which is set at construction time) will throw an IndexOutOfBoundsException.
The "non-member priority" for this queue is Double.NEGATIVE_INFINITY. This will be the reported priority of any item not currently stored in the queue. In addition, setting the priority of any item to this special value is equivalent to removing it from the queue.
Note that, as an implementer of the PriorityQueue interface (by way of UpdatablePriorityQueue), this class is a *max* priority queue --- remove() and peek() give the PriorityPair with the highest priority. This means that users who want a *min* priority queue should negate all their priorities.
Constructor and Description |
---|
MysteryUpdatablePriorityQueueImplementation(int maxValue)
Constructs a queue, given the maximum item value that the queue shall be
able to store.
|
Modifier and Type | Method and Description |
---|---|
void |
add(java.lang.Integer item,
java.lang.Double priority)
Adds an item, with the given priority, to the queue.
|
void |
add(PriorityPair<java.lang.Integer,java.lang.Double> itemPair)
Adds an item, with the priority given in the pair, to the queue.
|
void |
clear()
Removes all items from the queue.
|
java.lang.Double |
getPriority(java.lang.Integer item)
Returns the priority of the given item, or, if that item is not in the
queue, an implementation-specific sentinel value.
|
boolean |
isEmpty()
Returns true if the queue is empty.
|
java.lang.Double |
nonMemberPriority()
Returns the sentinel value used by getPriority() to indicate items not
currently stored in the queue.
|
PriorityPair<java.lang.Integer,java.lang.Double> |
peek()
Returns the maximum item in the queue, without removing it.
|
PriorityPair<java.lang.Integer,java.lang.Double> |
remove()
Removes the maximum item from the queue, and returns it.
|
boolean |
setPriority(java.lang.Integer item,
java.lang.Double newPriority)
Sets the priority of a given item.
|
public MysteryUpdatablePriorityQueueImplementation(int maxValue)
public void add(PriorityPair<java.lang.Integer,java.lang.Double> itemPair)
UpdatablePriorityQueue
add
in interface PriorityQueue<PriorityPair<java.lang.Integer,java.lang.Double>>
add
in interface UpdatablePriorityQueue<java.lang.Integer,java.lang.Double>
public void add(java.lang.Integer item, java.lang.Double priority)
UpdatablePriorityQueue
add
in interface UpdatablePriorityQueue<java.lang.Integer,java.lang.Double>
public PriorityPair<java.lang.Integer,java.lang.Double> remove()
PriorityQueue
remove
in interface PriorityQueue<PriorityPair<java.lang.Integer,java.lang.Double>>
public boolean setPriority(java.lang.Integer item, java.lang.Double newPriority)
UpdatablePriorityQueue
The consequences of this operation vary depending on two conditions:
The four cases covered by combinations of these conditions are:
The method returns true if the pair (item, newPriority) is in the queue after the call. (This is !DEL: true in cases 1 and 2; false in 3 and 4.)
setPriority
in interface UpdatablePriorityQueue<java.lang.Integer,java.lang.Double>
public PriorityPair<java.lang.Integer,java.lang.Double> peek()
PriorityQueue
peek
in interface PriorityQueue<PriorityPair<java.lang.Integer,java.lang.Double>>
public boolean isEmpty()
PriorityQueue
isEmpty
in interface PriorityQueue<PriorityPair<java.lang.Integer,java.lang.Double>>
public void clear()
PriorityQueue
clear
in interface PriorityQueue<PriorityPair<java.lang.Integer,java.lang.Double>>
public java.lang.Double nonMemberPriority()
UpdatablePriorityQueue
nonMemberPriority
in interface UpdatablePriorityQueue<java.lang.Integer,java.lang.Double>
public java.lang.Double getPriority(java.lang.Integer item)
UpdatablePriorityQueue
getPriority
in interface UpdatablePriorityQueue<java.lang.Integer,java.lang.Double>