|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectjava.util.AbstractCollection<E>
com.aliasi.util.BoundedPriorityQueue<E>
public class BoundedPriorityQueue<E>
A BoundedPriorityQueue implements a priority queue
with an upper bound on the number of elements. If the queue is not
full, added elements are always added. If the queue is full and
the added element is greater than the smallest element in the
queue, the smallest element is removed and the new element is
added. If the queue is full and the added element is not greater
than the smallest element in the queue, the new element is not
added.
Bounded priority queues are the ideal data structure with which
to implement n-best accumulators. A priority queue of bound
n can find the n-best elements of a
collection of m elements using O(n) space
and O(m n log n) time.
Bounded priority queues may also be used as the basis of a search implementation with the bound implementing heuristic n-best pruning.
Because bounded priority queues require a comparator and a
maximum size constraint, they do not comply with the recommendation
in the Collection interface in that they neither
implement a nullary constructor nor a constructor taking a single
collection. Instead, they are constructed with a comparator and
a maximum size bound.
Implementation Note: Priority queues are implemented on
top of TreeSet with element wrappers to adapt object
equality and the priority comparator. Because tree sets implement
balanced trees, the priority queue operations,
add(Object), pop() and
peek(), all require O(log n) time where
n is the size of the queue. A standard heap-based
implementation of a queue implements peeks in constant time and
adds and pops in O(log n) time. For our intended
applications, pops are more likely than peeks and we need access to
the worst element in the queue. An upside-down ordered heap
implementation of priority queues implements bounded adds most
efficiently, but requires up to O(n) for a pop or peek
and an O(n log n) sort before iteration.
| Constructor Summary | |
|---|---|
BoundedPriorityQueue(Comparator<? super E> comparator,
int maxSize)
Construct a bounded priority queue which uses the specified comparator to order elements and allows up to the specified maximum number of elements. |
|
| Method Summary | |
|---|---|
boolean |
add(E o)
Conditionally add the specified element to this queue. |
void |
clear()
Removes all elements from this queue. |
boolean |
isEmpty()
|
Iterator<E> |
iterator()
Returns an iterator over the elements in this bounded priority queue. |
int |
maxSize()
Returns the maximum size allowed for this queue. |
E |
peek()
Returns the largest element in the queue or null
if the queue is empty. |
E |
pop()
Remove and return the largest element in the queue or return null if the queue is empty. |
boolean |
remove(Object obj)
Removes the specified object from the priority queue. |
void |
setMaxSize(int maxSize)
Sets the maximum size of this bounded priority queue to the specified maximum size. |
int |
size()
Returns the current number of elements in this priority queue. |
| Methods inherited from class java.util.AbstractCollection |
|---|
addAll, contains, containsAll, removeAll, retainAll, toArray, toArray, toString |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Methods inherited from interface java.util.Collection |
|---|
addAll, contains, containsAll, equals, hashCode, removeAll, retainAll, toArray, toArray |
| Constructor Detail |
|---|
public BoundedPriorityQueue(Comparator<? super E> comparator,
int maxSize)
comparator - Comparator to order elements.maxSize - Maximum number of elements in the queue.
IllegalArgumentException - If the maximum size is less than 1.| Method Detail |
|---|
public boolean isEmpty()
isEmpty in interface Collection<E>isEmpty in class AbstractCollection<E>public E peek()
PriorityQueuenull
if the queue is empty.
peek in interface PriorityQueue<E>null
if the queue is empty.public E pop()
PriorityQueuenull if the queue is empty.
pop in interface PriorityQueue<E>public boolean remove(Object obj)
remove in interface Collection<E>remove in class AbstractCollection<E>obj - Object to remove from priority queue.
true if the object was removed.
ClassCastException - If the specified object is not
compatible with this collection.public void setMaxSize(int maxSize)
Note that this operation is not thread safe and should not be called concurrently with any other operations on this queue.
maxSize - New maximum size for this queue.public boolean add(E o)
add in interface Collection<E>add in class AbstractCollection<E>o - Object to add to queue.
true if the object was added.public void clear()
clear in interface Collection<E>clear in class AbstractCollection<E>public int size()
size in interface Collection<E>size in class AbstractCollection<E>public int maxSize()
public Iterator<E> iterator()
iterator in interface Iterable<E>iterator in interface Collection<E>iterator in class AbstractCollection<E>
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||