|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.util.AbstractCollection
org.drools.util.PriorityQueue
public class PriorityQueue
Binary heap implementation of Buffer
that provides for removal
based on Comparator
ordering.
remove()
method always returns the
first element as determined by the sort order. (The
ascendingOrder
flag in the constructors can be used to reverse
the sort order, in which case will always remove the last
element.) The removal order is not the same as the order of
iteration; elements are returned by the iterator in no particular order.
The add(Object)
and remove()
operations perform in
logarithmic time. The get()
operation performs in constant time. All
other operations perform in linear time or worse. Note that this
implementation is not synchronized.
Field Summary | |
---|---|
protected boolean |
ascendingOrder
If true, the first element as determined by the sort order will be returned. |
protected java.util.Comparator |
comparator
The comparator used to order the elements |
protected java.lang.Object[] |
elements
The elements in this buffer. |
protected int |
size
The number of elements currently in this buffer. |
Constructor Summary | |
---|---|
PriorityQueue()
Constructs a new empty buffer that sorts in ascending order by the natural order of the objects added. |
|
PriorityQueue(boolean ascendingOrder)
Constructs a new empty buffer specifying the sort order and using the natural order of the objects added. |
|
PriorityQueue(boolean ascendingOrder,
java.util.Comparator comparator)
Constructs a new empty buffer specifying the sort order and comparator. |
|
PriorityQueue(java.util.Comparator comparator)
Constructs a new empty buffer that sorts in ascending order using the specified comparator. |
|
PriorityQueue(int capacity)
Constructs a new empty buffer that sorts in ascending order by the natural order of the objects added, specifying an initial capacity. |
|
PriorityQueue(int capacity,
boolean ascendingOrder)
Constructs a new empty buffer that specifying initial capacity and sort order, using the natural order of the objects added. |
|
PriorityQueue(int capacity,
boolean ascendingOrder,
java.util.Comparator comparator)
Constructs a new empty buffer that specifying initial capacity, sort order and comparator. |
|
PriorityQueue(int capacity,
java.util.Comparator comparator)
Constructs a new empty buffer that sorts in ascending order using the specified comparator and initial capacity. |
Method Summary | |
---|---|
boolean |
add(java.lang.Object element)
Adds an element to the buffer. |
void |
clear()
Clears all elements from the buffer. |
java.util.Comparator |
comparator()
Gets the comparator being used for this buffer, null is natural order. |
protected int |
compare(java.lang.Object a,
java.lang.Object b)
Compares two objects using the comparator if specified, or the natural order otherwise. |
java.lang.Object |
get()
Gets the next element to be removed without actually removing it (peek). |
protected void |
grow()
Increases the size of the heap to support additional elements |
boolean |
isAscendingOrder()
Checks whether the heap is ascending or descending order. |
protected boolean |
isAtCapacity()
Tests if the buffer is at capacity. |
java.util.Iterator |
iterator()
Returns an iterator over this heap's elements. |
protected void |
percolateDownMaxHeap(int index)
Percolates element down heap from the position given by the index. |
protected void |
percolateDownMinHeap(int index)
Percolates element down heap from the position given by the index. |
protected void |
percolateUpMaxHeap(int index)
Percolates element up heap from from the position given by the index. |
protected void |
percolateUpMaxHeap(java.lang.Object element)
Percolates a new element up heap from the bottom. |
protected void |
percolateUpMinHeap(int index)
Percolates element up heap from the position given by the index. |
protected void |
percolateUpMinHeap(java.lang.Object element)
Percolates a new element up heap from the bottom. |
java.lang.Object |
remove()
Gets and removes the next element (pop). |
int |
size()
Returns the number of elements in this buffer. |
java.lang.String |
toString()
Returns a string representation of this heap. |
Methods inherited from class java.util.AbstractCollection |
---|
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface java.util.Collection |
---|
equals, hashCode |
Field Detail |
---|
protected java.lang.Object[] elements
protected int size
protected boolean ascendingOrder
protected java.util.Comparator comparator
Constructor Detail |
---|
public PriorityQueue()
public PriorityQueue(java.util.Comparator comparator)
comparator
- the comparator used to order the elements, null means use
natural orderpublic PriorityQueue(boolean ascendingOrder)
ascendingOrder
- if true
the heap is created as a minimum heap;
otherwise, the heap is created as a maximum heappublic PriorityQueue(boolean ascendingOrder, java.util.Comparator comparator)
ascendingOrder
- true to use the order imposed by the given comparator; false
to reverse that ordercomparator
- the comparator used to order the elements, null means use
natural orderpublic PriorityQueue(int capacity)
capacity
- the initial capacity for the buffer, greater than zero
java.lang.IllegalArgumentException
- if capacity
is <= 0
public PriorityQueue(int capacity, java.util.Comparator comparator)
capacity
- the initial capacity for the buffer, greater than zerocomparator
- the comparator used to order the elements, null means use
natural order
java.lang.IllegalArgumentException
- if capacity
is <= 0
public PriorityQueue(int capacity, boolean ascendingOrder)
capacity
- the initial capacity for the buffer, greater than zeroascendingOrder
- if true
the heap is created as a minimum heap;
otherwise, the heap is created as a maximum heap.
java.lang.IllegalArgumentException
- if capacity
is <= 0
public PriorityQueue(int capacity, boolean ascendingOrder, java.util.Comparator comparator)
capacity
- the initial capacity for the buffer, greater than zeroascendingOrder
- true to use the order imposed by the given comparator; false
to reverse that ordercomparator
- the comparator used to order the elements, null means use
natural order
java.lang.IllegalArgumentException
- if capacity
is <= 0
Method Detail |
---|
public boolean isAscendingOrder()
public java.util.Comparator comparator()
public int size()
size
in interface java.util.Collection
size
in class java.util.AbstractCollection
public void clear()
clear
in interface java.util.Collection
clear
in class java.util.AbstractCollection
public boolean add(java.lang.Object element)
add
in interface java.util.Collection
add
in class java.util.AbstractCollection
element
- the element to be added
public java.lang.Object get()
java.util.NoSuchElementException
- if the buffer is emptypublic java.lang.Object remove()
java.util.NoSuchElementException
- if the buffer is emptyprotected boolean isAtCapacity()
true
if buffer is full; false
otherwise.protected void percolateDownMinHeap(int index)
index
- the index for the elementprotected void percolateDownMaxHeap(int index)
index
- the index of the elementprotected void percolateUpMinHeap(int index)
index
- the index of the element to be percolated upprotected void percolateUpMinHeap(java.lang.Object element)
element
- the elementprotected void percolateUpMaxHeap(int index)
index
- the index of the element to be percolated upprotected void percolateUpMaxHeap(java.lang.Object element)
element
- the elementprotected int compare(java.lang.Object a, java.lang.Object b)
a
- the first objectb
- the second object
protected void grow()
public java.util.Iterator iterator()
iterator
in interface java.lang.Iterable
iterator
in interface java.util.Collection
iterator
in class java.util.AbstractCollection
public java.lang.String toString()
toString
in class java.util.AbstractCollection
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |