edu.stanford.nlp.util
Class FixedPrioritiesPriorityQueue<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractSet<E>
          extended by edu.stanford.nlp.util.FixedPrioritiesPriorityQueue<E>
All Implemented Interfaces:
PriorityQueue<E>, java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.Iterator<E>, java.util.Set<E>

public class FixedPrioritiesPriorityQueue<E>
extends java.util.AbstractSet<E>
implements PriorityQueue<E>, java.util.Iterator<E>, java.io.Serializable, java.lang.Cloneable

A priority queue based on a binary heap. This implementation trades flexibility for speed: while it is up to 2x faster than BinaryHeapPriorityQueue and nearly as fast as PriorityQueue, it does not support removing or changing the priority of an element. Also, while getPriority(Object key) is supported, performance will be linear, not constant.

Author:
Dan Klein, Bill MacCartney
See Also:
Serialized Form

Constructor Summary
FixedPrioritiesPriorityQueue()
           
FixedPrioritiesPriorityQueue(int capacity)
           
 
Method Summary
 boolean add(E key, double priority)
          Adds a key to the queue with the given priority.
 boolean changePriority(E key, double priority)
          Not supported in this implementation.
 void clear()
           
 FixedPrioritiesPriorityQueue<E> clone()
          Returns a clone of this priority queue.
 E getFirst()
          Returns the highest-priority element without removing it from the queue.
 double getPriority()
          Gets the priority of the highest-priority element of the queue.
 double getPriority(java.lang.Object key)
          Note that this method will be linear (not constant) time in this implementation! Better not to use it.
 boolean hasNext()
          Returns true if the priority queue is non-empty
 java.util.Iterator<E> iterator()
           
static void main(java.lang.String[] args)
           
 E next()
          Returns the element in the queue with highest priority, and pops it from the queue.
 boolean relaxPriority(E key, double priority)
          Not supported in this implementation.
 void remove()
          Not supported -- next() already removes the head of the queue.
 E removeFirst()
          Returns the highest-priority element and removes it from the queue.
 int size()
          Number of elements in the queue.
 java.util.List<E> toSortedList()
           
 java.lang.String toString()
          Returns a representation of the queue in decreasing priority order.
 java.lang.String toString(int maxKeysToPrint)
          Returns a representation of the queue in decreasing priority order, displaying at most maxKeysToPring elements.
 
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
 
Methods inherited from class java.util.AbstractCollection
add, addAll, contains, containsAll, isEmpty, remove, retainAll, toArray, toArray
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
add, addAll, contains, containsAll, equals, hashCode, isEmpty, remove, removeAll, retainAll, toArray, toArray
 

Constructor Detail

FixedPrioritiesPriorityQueue

public FixedPrioritiesPriorityQueue()

FixedPrioritiesPriorityQueue

public FixedPrioritiesPriorityQueue(int capacity)
Method Detail

hasNext

public boolean hasNext()
Returns true if the priority queue is non-empty

Specified by:
hasNext in interface java.util.Iterator<E>

next

public E next()
Returns the element in the queue with highest priority, and pops it from the queue.

Specified by:
next in interface java.util.Iterator<E>

remove

public void remove()
Not supported -- next() already removes the head of the queue.

Specified by:
remove in interface java.util.Iterator<E>

add

public boolean add(E key,
                   double priority)
Adds a key to the queue with the given priority. If the key is already in the queue, it will be added an additional time, NOT promoted/demoted.

Specified by:
add in interface PriorityQueue<E>
Returns:
true if this set did not already contain the specified element.

changePriority

public boolean changePriority(E key,
                              double priority)
Not supported in this implementation.

Specified by:
changePriority in interface PriorityQueue<E>
Parameters:
key - an E value
Returns:
whether the priority actually changed.

getFirst

public E getFirst()
Returns the highest-priority element without removing it from the queue.

Specified by:
getFirst in interface PriorityQueue<E>
Returns:
the object with minimum key

getPriority

public double getPriority(java.lang.Object key)
Note that this method will be linear (not constant) time in this implementation! Better not to use it.

Specified by:
getPriority in interface PriorityQueue<E>
Parameters:
key - The object to assess
Returns:
A key's priority. If the key is not in the queue, Double.NEGATIVE_INFINITY is returned.

getPriority

public double getPriority()
Gets the priority of the highest-priority element of the queue.

Specified by:
getPriority in interface PriorityQueue<E>
Returns:
The priority of the highest-priority element of the queue.

relaxPriority

public boolean relaxPriority(E key,
                             double priority)
Not supported in this implementation.

Specified by:
relaxPriority in interface PriorityQueue<E>

removeFirst

public E removeFirst()
Returns the highest-priority element and removes it from the queue.

Specified by:
removeFirst in interface PriorityQueue<E>
Returns:
the object with highest priority

toSortedList

public java.util.List<E> toSortedList()
Specified by:
toSortedList in interface PriorityQueue<E>

size

public int size()
Number of elements in the queue.

Specified by:
size in interface java.util.Collection<E>
Specified by:
size in interface java.util.Set<E>
Specified by:
size in class java.util.AbstractCollection<E>

clear

public void clear()
Specified by:
clear in interface java.util.Collection<E>
Specified by:
clear in interface java.util.Set<E>
Overrides:
clear in class java.util.AbstractCollection<E>

iterator

public java.util.Iterator<E> iterator()
Specified by:
iterator in interface java.lang.Iterable<E>
Specified by:
iterator in interface java.util.Collection<E>
Specified by:
iterator in interface java.util.Set<E>
Specified by:
iterator in class java.util.AbstractCollection<E>

toString

public java.lang.String toString()
Returns a representation of the queue in decreasing priority order.

Overrides:
toString in class java.util.AbstractCollection<E>

toString

public java.lang.String toString(int maxKeysToPrint)
Returns a representation of the queue in decreasing priority order, displaying at most maxKeysToPring elements.

Specified by:
toString in interface PriorityQueue<E>
Parameters:
maxKeysToPrint - The maximum number of keys to print. Less are printed if there are less than this number of items in the PriorityQueue. If this number is non-positive, then all elements in the PriorityQueue are printed.
Returns:
A String representation of the high priority items in the queue.

clone

public FixedPrioritiesPriorityQueue<E> clone()
Returns a clone of this priority queue. Modifications to one will not affect modifications to the other.

Overrides:
clone in class java.lang.Object

main

public static void main(java.lang.String[] args)


Stanford NLP Group