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

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

public class BinaryHeapPriorityQueue<E>
extends java.util.AbstractSet<E>
implements PriorityQueue<E>, java.util.Iterator<E>

PriorityQueue with explicit double priority values. Larger doubles are higher priorities. BinaryHeap-backed.

Author:
Dan Klein, Christopher Manning For each entry, uses ~ 24 (entry) + 16? (Map.Entry) + 4 (List entry) = 44 bytes?

Constructor Summary
BinaryHeapPriorityQueue()
           
BinaryHeapPriorityQueue(int initCapacity)
           
BinaryHeapPriorityQueue(MapFactory<java.lang.Object,edu.stanford.nlp.util.BinaryHeapPriorityQueue.Entry<E>> mapFactory)
           
BinaryHeapPriorityQueue(MapFactory<java.lang.Object,edu.stanford.nlp.util.BinaryHeapPriorityQueue.Entry<E>> mapFactory, int initCapacity)
           
 
Method Summary
 boolean add(E key)
          Adds an object to the queue with the minimum priority (Double.NEGATIVE_INFINITY).
 boolean add(E key, double priority)
          Convenience method for if you want to pretend relaxPriority doesn't exist, or if you really want add's return conditions.
 boolean changePriority(E key, double priority)
          Changes a priority, either up or down, adding the key it if it wasn't there already.
 void clear()
          Clears the queue.
 boolean contains(java.lang.Object key)
          Returns whether the queue contains the given key.
 boolean decreasePriority(E key, double priority)
          Demotes a key in the queue, adding it if it wasn't there already.
 BinaryHeapPriorityQueue<E> deepCopy()
           
 BinaryHeapPriorityQueue<E> deepCopy(MapFactory<java.lang.Object,edu.stanford.nlp.util.BinaryHeapPriorityQueue.Entry<E>> mapFactory)
           
 E getFirst()
          Finds the object with the highest priority and returns it, without modifying the queue.
 E getObject(E key)
          Searches for the object in the queue and returns it.
 double getPriority()
          Gets the priority of the highest-priority element of the queue.
 double getPriority(E key)
          Get the priority of a key -- if the key is not in the queue, Double.NEGATIVE_INFINITY is returned.
 boolean hasNext()
           
 boolean isEmpty()
          Checks if the queue is empty.
 java.util.Iterator<E> iterator()
           
static void main(java.lang.String[] args)
           
 E next()
           
 boolean relaxPriority(E key, double priority)
          Promotes a key in the queue, adding it if it wasn't there already.
 void remove()
           
 boolean remove(java.lang.Object key)
           
 E removeFirst()
          Finds the object with the highest priority, removes it, and returns it.
 int size()
          Get the number of elements in the queue.
 java.util.List<E> toSortedList()
           
 java.lang.String toString()
           
 java.lang.String toString(int maxKeysToPrint)
          Returns a representation of the queue in decreasing priority order, displaying at most maxKeysToPring elements.
 java.lang.String toVerticalString()
           
 
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
 
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, retainAll, toArray, toArray
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
addAll, containsAll, equals, hashCode, removeAll, retainAll, toArray, toArray
 

Constructor Detail

BinaryHeapPriorityQueue

public BinaryHeapPriorityQueue()

BinaryHeapPriorityQueue

public BinaryHeapPriorityQueue(int initCapacity)

BinaryHeapPriorityQueue

public BinaryHeapPriorityQueue(MapFactory<java.lang.Object,edu.stanford.nlp.util.BinaryHeapPriorityQueue.Entry<E>> mapFactory)

BinaryHeapPriorityQueue

public BinaryHeapPriorityQueue(MapFactory<java.lang.Object,edu.stanford.nlp.util.BinaryHeapPriorityQueue.Entry<E>> mapFactory,
                               int initCapacity)
Method Detail

hasNext

public boolean hasNext()
Specified by:
hasNext in interface java.util.Iterator<E>

next

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

remove

public void remove()
Specified by:
remove in interface java.util.Iterator<E>

removeFirst

public E removeFirst()
Finds the object with the highest priority, removes it, and returns it.

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

getFirst

public E getFirst()
Finds the object with the highest priority and returns it, without modifying the queue.

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

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.

getObject

public E getObject(E key)
Searches for the object in the queue and returns it. May be useful if you can create a new object that is .equals() to an object in the queue but is not actually identical, or if you want to modify an object that is in the queue.

Returns:
null if the object is not in the queue, otherwise returns the object.

getPriority

public double getPriority(E key)
Get the priority of a key -- if the key is not in the queue, Double.NEGATIVE_INFINITY is returned.

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.

add

public boolean add(E key)
Adds an object to the queue with the minimum priority (Double.NEGATIVE_INFINITY). If the object is already in the queue with worse priority, this does nothing. If the object is already present, with better priority, it will NOT cause an a decreasePriority.

Specified by:
add in interface java.util.Collection<E>
Specified by:
add in interface java.util.Set<E>
Overrides:
add in class java.util.AbstractCollection<E>
Parameters:
key - an Object value
Returns:
whether the key was present before

add

public boolean add(E key,
                   double priority)
Convenience method for if you want to pretend relaxPriority doesn't exist, or if you really want add's return conditions.

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

remove

public boolean remove(java.lang.Object key)
Specified by:
remove in interface java.util.Collection<E>
Specified by:
remove in interface java.util.Set<E>
Overrides:
remove in class java.util.AbstractCollection<E>

relaxPriority

public boolean relaxPriority(E key,
                             double priority)
Promotes a key in the queue, adding it if it wasn't there already. If the specified priority is worse than the current priority, nothing happens. Faster than add if you don't care about whether the key is new.

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

decreasePriority

public boolean decreasePriority(E key,
                                double priority)
Demotes a key in the queue, adding it if it wasn't there already. If the specified priority is better than the current priority, nothing happens. If you decrease the priority on a non-present key, it will get added, but at it's old implicit priority of Double.NEGATIVE_INFINITY.

Parameters:
key - an Object value
Returns:
whether the priority actually improved.

changePriority

public boolean changePriority(E key,
                              double priority)
Changes a priority, either up or down, adding the key it if it wasn't there already.

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

isEmpty

public boolean isEmpty()
Checks if the queue is empty.

Specified by:
isEmpty in interface java.util.Collection<E>
Specified by:
isEmpty in interface java.util.Set<E>
Overrides:
isEmpty in class java.util.AbstractCollection<E>
Returns:
a boolean value

size

public int size()
Get the 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>
Returns:
queue size

contains

public boolean contains(java.lang.Object key)
Returns whether the queue contains the given key.

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

toSortedList

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

deepCopy

public BinaryHeapPriorityQueue<E> deepCopy(MapFactory<java.lang.Object,edu.stanford.nlp.util.BinaryHeapPriorityQueue.Entry<E>> mapFactory)

deepCopy

public BinaryHeapPriorityQueue<E> deepCopy()

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>

clear

public void clear()
Clears the queue.

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>

toString

public java.lang.String toString()
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.

toVerticalString

public java.lang.String toVerticalString()

main

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


Stanford NLP Group