edu.stanford.nlp.util
Class BinaryHeapPriorityQueue

java.lang.Object
  extended byjava.util.AbstractCollection
      extended byjava.util.AbstractSet
          extended byedu.stanford.nlp.util.BinaryHeapPriorityQueue
All Implemented Interfaces:
Collection, Iterator, PriorityQueue, Set

public class BinaryHeapPriorityQueue
extends AbstractSet
implements PriorityQueue, Iterator

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(MapFactory mapFactory)
           
 
Method Summary
 boolean add(Object key)
          Adds an object to the queue with the minimum priority (Double.NEGATIVE_INFINITY).
 boolean add(Object key, double priority)
          Convenience method for if you want to pretend increaseKey doesn't exist, or if you really want add's return conditions.
 boolean changePriority(Object 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(Object key)
          Returns whether the queue contains the given key.
 boolean decreasePriority(Object key, double priority)
          Demotes a key in the queue, adding it if it wasn't there already.
 BinaryHeapPriorityQueue deepCopy()
           
 BinaryHeapPriorityQueue deepCopy(MapFactory mapFactory)
           
 Object getFirst()
          Finds the object with the highest priority and returns it, without modifying the queue.
 double getPriority(Object 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.
 Iterator iterator()
           
static void main(String[] args)
           
 Object next()
           
 boolean relaxPriority(Object key, double priority)
          Promotes a key in the queue, adding it if it wasn't there already.
 void remove()
           
 boolean remove(Object key)
           
 Object removeFirst()
          Finds the object with the highest priority, removes it, and returns it.
 int size()
          Get the number of elements in the queue.
 List toSortedList()
           
 String toString()
           
 
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(MapFactory mapFactory)
Method Detail

hasNext

public boolean hasNext()
Specified by:
hasNext in interface Iterator

next

public Object next()
Specified by:
next in interface Iterator

remove

public void remove()
Specified by:
remove in interface Iterator

removeFirst

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

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

getFirst

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

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

getPriority

public double getPriority(Object 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
Parameters:
key -
Returns:

add

public boolean add(Object 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 Set
Parameters:
key - an Object value
Returns:
whether the key was present before

add

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

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

remove

public boolean remove(Object key)
Specified by:
remove in interface Set

relaxPriority

public boolean relaxPriority(Object 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
Parameters:
key - an Object value
priority -
Returns:
whether the priority actually improved.

decreasePriority

public boolean decreasePriority(Object 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(Object 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
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 Set
Returns:
a boolean value

size

public int size()
Get the number of elements in the queue.

Specified by:
size in interface Set
Returns:
queue size

contains

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

Specified by:
contains in interface Set

toSortedList

public List toSortedList()
Specified by:
toSortedList in interface PriorityQueue

deepCopy

public BinaryHeapPriorityQueue deepCopy(MapFactory mapFactory)

deepCopy

public BinaryHeapPriorityQueue deepCopy()

iterator

public Iterator iterator()
Specified by:
iterator in interface Set

clear

public void clear()
Clears the queue.

Specified by:
clear in interface Set

toString

public String toString()

main

public static void main(String[] args)


Stanford NLP Group