com.browseengine.bobo.util
Class IntBoundedPriorityQueue

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractQueue<E>
          extended by java.util.PriorityQueue<Integer>
              extended by com.browseengine.bobo.util.IntBoundedPriorityQueue
All Implemented Interfaces:
Serializable, Iterable<Integer>, Collection<Integer>, Queue<Integer>

public class IntBoundedPriorityQueue
extends PriorityQueue<Integer>

A priority queue for int that uses int[] heap for storage. Always have least element on top. When the queue is full, new elements are added if they are bigger than the least element to replace them.

Author:
"Xiaoyang Gu"
See Also:
Serialized Form

Nested Class Summary
static class IntBoundedPriorityQueue.IntComparator
           
static interface IntBoundedPriorityQueue.IntIterator
           
 
Constructor Summary
IntBoundedPriorityQueue(IntBoundedPriorityQueue.IntComparator comparator, int capacity, int forbiddenValue)
           
 
Method Summary
 Integer element()
           Retrieves, but does not remove, the head of this queue.
 int intElement()
           
 IntBoundedPriorityQueue.IntIterator iterator()
          Returns an iterator over the elements in this collection.
 boolean offer(int item)
          When the queue is full, the offered elements are added if they are bigger than the smallest one already in the queue.
 boolean offer(Integer item)
          When the queue is full, the offered elements are added if they are bigger than the smallest one already in the queue.
 Integer peek()
          Retrieves, but does not remove, the head of this queue, returning null if this queue is empty.
 int peekInt()
          Retrieves, but does not remove, the head of this queue, returning the forbidden value if the queue is empty.
 Integer poll()
          Retrieves and removes the head of this queue, or null if this queue is empty.
 int pollInt()
          Retrieves and removes the head of this queue, or the forbidden value if this queue is empty.
 int size()
          Returns the number of elements in this collection.
 
Methods inherited from class java.util.PriorityQueue
add, clear, comparator, contains, remove, toArray, toArray
 
Methods inherited from class java.util.AbstractQueue
addAll, remove
 
Methods inherited from class java.util.AbstractCollection
containsAll, isEmpty, removeAll, retainAll, 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
containsAll, equals, hashCode, isEmpty, removeAll, retainAll
 

Constructor Detail

IntBoundedPriorityQueue

public IntBoundedPriorityQueue(IntBoundedPriorityQueue.IntComparator comparator,
                               int capacity,
                               int forbiddenValue)
Parameters:
capacity - the maximum number of items the queue accepts
comparator - a comparator that is used to order the items.
Method Detail

element

public Integer element()
                throws NoSuchElementException
Retrieves, but does not remove, the head of this queue. This implementation returns the result of peek unless the queue is empty.

Specified by:
element in interface Queue<Integer>
Overrides:
element in class AbstractQueue<Integer>
Throws:
NoSuchElementException
See Also:
Queue.element()

intElement

public int intElement()
               throws NoSuchElementException
Throws:
NoSuchElementException

iterator

public IntBoundedPriorityQueue.IntIterator iterator()
Returns an iterator over the elements in this collection. There are no guarantees concerning the order in which the elements are returned (unless this collection is an instance of some class that provides a guarantee).

Specified by:
iterator in interface Iterable<Integer>
Specified by:
iterator in interface Collection<Integer>
Overrides:
iterator in class PriorityQueue<Integer>
See Also:
AbstractCollection.iterator()

offer

public boolean offer(Integer item)
When the queue is full, the offered elements are added if they are bigger than the smallest one already in the queue.
Inserts the specified element into this queue, if possible. When using queues that may impose insertion restrictions (for example capacity bounds), method offer is generally preferable to method Collection.add, which can fail to insert an element only by throwing an exception.

Specified by:
offer in interface Queue<Integer>
Overrides:
offer in class PriorityQueue<Integer>
See Also:
Queue.offer(java.lang.Object)

offer

public boolean offer(int item)
When the queue is full, the offered elements are added if they are bigger than the smallest one already in the queue.
Inserts the specified element into this queue, if possible. When using queues that may impose insertion restrictions (for example capacity bounds), method offer is generally preferable to method Collection.add, which can fail to insert an element only by throwing an exception.

See Also:
Queue.offer(java.lang.Object)

peek

public Integer peek()
Retrieves, but does not remove, the head of this queue, returning null if this queue is empty.

Specified by:
peek in interface Queue<Integer>
Overrides:
peek in class PriorityQueue<Integer>
See Also:
Queue.peek()

peekInt

public int peekInt()
Retrieves, but does not remove, the head of this queue, returning the forbidden value if the queue is empty.

Returns:

poll

public Integer poll()
Retrieves and removes the head of this queue, or null if this queue is empty.

Specified by:
poll in interface Queue<Integer>
Overrides:
poll in class PriorityQueue<Integer>
See Also:
Queue.poll()

pollInt

public int pollInt()
Retrieves and removes the head of this queue, or the forbidden value if this queue is empty.

Returns:

size

public int size()
Returns the number of elements in this collection.

Specified by:
size in interface Collection<Integer>
Overrides:
size in class PriorityQueue<Integer>
See Also:
AbstractCollection.size()


Copyright © 2011. All Rights Reserved.