|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
public interface FindableQueue<S,T extends Comparable<T> & Findable<S>>
A FindableQueue is a priority queue that additionally supports
fast lookup/removal of elements by element key (via the
Findable interface). Separating the key value from the
object identity allows us to look up copies of an object of
unknown priority, to re-insert an object under a new priority,
or, in general, to use only partial information about the object
(e.g., an order ID code) to look it up. Note that a standard
queue-like behavior, in which the entire object ID is used as
a lookup value, can be obtained by defining Findable.getKey()
simply to return this.
A FindableQueue is a generic type, parameterized by
two type parameters, T and S. T represents
the type of the objects being stored in the queue, while
S represents the type of object used as the key used for
looking up arbitrary entries in the queue.
FindableQueue objects sort their contents into increasing
priority order. That is, the first element of the queue is the
one with the smallest priority:
the first element of the queue is the element
x such that for every y in the queue,
x.compareTo(y)<=0. (That is,x is less than
or equal to every y.)
Objects of this type MUST NOT consume more than amortized O(n) space for a queue containing n objects.
Comparable,
Findable| Method Summary | |
|---|---|
boolean |
contains(T item)
true iff this queue contains the object item,
as located via its key (Findable.getKey()). |
T |
first()
Return the first item, by priority, from the queue. |
T |
get(T element)
This looks up an arbitrary object in the queue, according to its Findable.getKey() method, and returns a reference to the
object stored in the queue. |
void |
insert(T element)
Insert an object into the queue, ordered according to its priority via the natural order. |
boolean |
isEmpty()
Test whether the queue contains any elements. |
int |
length()
Get the number of elements currently in this queue. |
T |
pop()
Remove and return the first item, by priority, from the queue. |
T |
remove(T element)
Remove an element by reference. |
| Method Detail |
|---|
boolean contains(T item)
true iff this queue contains the object item,
as located via its key (Findable.getKey()).
The semantics of this method are equivalent to the following.
Assume that the queue is implemented in terms of a List
named _heap, then a naive implementation of
contains(Comparable) would be:
private ListNote that this is only semantically equivalent to the required_heap; public boolean contains(T item) { for (T qElem : _heap) { if (qElem.getKey().equals(item.getKey())) { return true; } } return false; }
contains method -- it produces the same output,
but does not run in the required asymptotic time bound (see
below). In practice, objects of type FindableQueue
will probably not use a simple list for their heap representation
and definitely will not use a linear scan to locate
an item by key.
This method MUST run in amortized O(1) time.
item - Object to look up in the queue.
true iff this queue contains item
IllegalArgumentException - iff item==null.int length()
This method MUST run in O(1) time.
T get(T element)
Findable.getKey() method, and returns a reference to the
object stored in the queue.
Semantically, this method is similar to contains(Comparable),
in that it is equivalent to scanning through the priority queue,
looking for any queue entry that is equivalent to element,
according to the Findable.getKey() method. As with
contains(Comparable), however, a linear scan will not
meet the asymptotic time requirements of this object.
This method MUST run in amortized O(1) time.
element - Item to search for in the queue.
null if no such object is present in the queue.
IllegalArgumentException - iff element==null.boolean isEmpty()
This method MUST run in O(1) time.
true iff the queue contains no elements.void insert(T element)
natural order.
If the object is already present in the queue, according to the
Findable.getKey() method, then the previous copy is
removed from the queue before this copy is inserted.
null objects are not allowed in these queues, so
this prevents their insertion by generating an
IllegalArgumentException.
This method MUST run in O(log(n)) time for a queue containing n elements.
element - Item to insert into the queue in priority order
IllegalArgumentException - iff element==nullT first()
null if the queue is currently empty.
This method MUST run in O(1) time.
T pop()
first(), this does change the state
of the queue, by removing the first element from the queue
and decreasing the queue length by one.
Returns null if the queue is currently empty.
This method MUST run in O(log(n)) time for a queue containing n elements.
null if
the queue is currently empty.T remove(T element)
Findable.getKey() method to locate the element in the
queue and then removes it from the queue, maintaining priority
order among all other elements of the queue. It returns a
reference to the removed item (i.e., the item that was actually
found in the queue, not the argument to this method). If the requested
element is not currently in the queue, this returns
null without
modifying the queue.
This method MUST run in O(log(n)) time for a queue containing n elements.
element - Element to remove from the queue.
IllegalArgumentException - iff element==null.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||