netkit.util
Class UpdatablePriorityQueue<E>

java.lang.Object
  extended by netkit.util.UpdatablePriorityQueue<E>
All Implemented Interfaces:
java.lang.Iterable<E>

public class UpdatablePriorityQueue<E>
extends java.lang.Object
implements java.lang.Iterable<E>


Constructor Summary
UpdatablePriorityQueue()
          Creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering.
UpdatablePriorityQueue(int initialCapacity)
          Creates a PriorityQueue with the specified initial capacity that orders its elements according to their natural ordering.
UpdatablePriorityQueue(int initialCapacity, java.util.Comparator<? super E> comparator)
          Creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.
 
Method Summary
 netkit.util.UpdatablePriorityQueue.QueueElement add(E e)
          Inserts the specified element into this priority queue.
 void clear()
          Removes all of the elements from this priority queue.
 java.util.Comparator<? super E> comparator()
          Returns the comparator used to order the elements in this queue, or null if this queue is sorted according to the natural ordering of its elements.
 boolean contains(E o)
          Returns true if this queue contains the specified element.
 boolean isEmpty()
           
 java.util.Iterator<E> iterator()
          Returns an iterator over the elements in this queue.
 E peek()
           
 E poll()
           
 boolean remove(E o)
          Removes a single instance of the specified element from this queue, if it is present.
 boolean remove(netkit.util.UpdatablePriorityQueue.QueueElement qe)
          Removes a single instance of the specified element from this queue, if it is present.
 int size()
           
 java.lang.Object[] toArray()
          Returns an array containing all of the elements in this queue.
<T> T[]
toArray(T[] a)
          Returns an array containing all of the elements in this queue; the runtime type of the returned array is that of the specified array.
 void update(netkit.util.UpdatablePriorityQueue.QueueElement qe)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UpdatablePriorityQueue

public UpdatablePriorityQueue()
Creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering.


UpdatablePriorityQueue

public UpdatablePriorityQueue(int initialCapacity)
Creates a PriorityQueue with the specified initial capacity that orders its elements according to their natural ordering.

Parameters:
initialCapacity - the initial capacity for this priority queue
Throws:
java.lang.IllegalArgumentException - if initialCapacity is less than 1

UpdatablePriorityQueue

public UpdatablePriorityQueue(int initialCapacity,
                              java.util.Comparator<? super E> comparator)
Creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.

Parameters:
initialCapacity - the initial capacity for this priority queue
comparator - the comparator that will be used to order this priority queue. If null, the natural ordering of the elements will be used.
Throws:
java.lang.IllegalArgumentException - if initialCapacity is less than 1
Method Detail

add

public netkit.util.UpdatablePriorityQueue.QueueElement add(E e)
Inserts the specified element into this priority queue.

Returns:
true (as specified by Collection.add(E))
Throws:
java.lang.ClassCastException - if the specified element cannot be compared with elements currently in this priority queue according to the priority queue's ordering
java.lang.NullPointerException - if the specified element is null

peek

public E peek()

update

public void update(netkit.util.UpdatablePriorityQueue.QueueElement qe)

remove

public boolean remove(netkit.util.UpdatablePriorityQueue.QueueElement qe)
Removes a single instance of the specified element from this queue, if it is present. More formally, removes an element e such that o.equals(e), if this queue contains one or more such elements. Returns true if and only if this queue contained the specified element (or equivalently, if this queue changed as a result of the call).

Parameters:
qe - element to be removed from this queue, if present
Returns:
true if this queue changed as a result of the call

remove

public boolean remove(E o)
Removes a single instance of the specified element from this queue, if it is present. More formally, removes an element e such that o.equals(e), if this queue contains one or more such elements. Returns true if and only if this queue contained the specified element (or equivalently, if this queue changed as a result of the call).

Parameters:
o - element to be removed from this queue, if present
Returns:
true if this queue changed as a result of the call

isEmpty

public boolean isEmpty()

contains

public boolean contains(E o)
Returns true if this queue contains the specified element. More formally, returns true if and only if this queue contains at least one element e such that o.equals(e).

Parameters:
o - object to be checked for containment in this queue
Returns:
true if this queue contains the specified element

toArray

public java.lang.Object[] toArray()
Returns an array containing all of the elements in this queue. The elements are in no particular order.

The returned array will be "safe" in that no references to it are maintained by this queue. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.

This method acts as bridge between array-based and collection-based APIs.

Returns:
an array containing all of the elements in this queue

toArray

public <T> T[] toArray(T[] a)
Returns an array containing all of the elements in this queue; the runtime type of the returned array is that of the specified array. The returned array elements are in no particular order. If the queue fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this queue.

If the queue fits in the specified array with room to spare (i.e., the array has more elements than the queue), the element in the array immediately following the end of the collection is set to null.

Like the toArray() method, this method acts as bridge between array-based and collection-based APIs. Further, this method allows precise control over the runtime type of the output array, and may, under certain circumstances, be used to save allocation costs.

Suppose x is a queue known to contain only strings. The following code can be used to dump the queue into a newly allocated array of String:

     String[] y = x.toArray(new String[0]);
Note that toArray(new Object[0]) is identical in function to toArray().

Parameters:
a - the array into which the elements of the queue are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Returns:
an array containing all of the elements in this queue
Throws:
java.lang.ArrayStoreException - if the runtime type of the specified array is not a supertype of the runtime type of every element in this queue
java.lang.NullPointerException - if the specified array is null

iterator

public java.util.Iterator<E> iterator()
Returns an iterator over the elements in this queue. The iterator does not return the elements in any particular order.

Specified by:
iterator in interface java.lang.Iterable<E>
Returns:
an iterator over the elements in this queue

size

public int size()

clear

public void clear()
Removes all of the elements from this priority queue. The queue will be empty after this call returns.


poll

public E poll()

comparator

public java.util.Comparator<? super E> comparator()
Returns the comparator used to order the elements in this queue, or null if this queue is sorted according to the natural ordering of its elements.

Returns:
the comparator used to order this queue, or null if this queue is sorted according to the natural ordering of its elements