Buffer that provides for
removal based on Comparator ordering.
The removal order of a binary heap is based on either the natural sort
order of its elements or a specified Comparator . The
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 remove()
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. Use
synchronizedBuffer(Buffer) or
decorate(Buffer)
to provide synchronized access to a PriorityBuffer:
Buffer heap = SynchronizedBuffer.decorate(new PriorityBuffer());
This class is Serializable from Commons Collections 3.2.
| Field Summary | |
|---|---|
| protected boolean |
If true, the first element as determined by the sort order will be returned. |
| protected Comparator |
The comparator used to order the elements |
| protected Object[] |
The elements in this buffer. |
| protected int |
The number of elements currently in this buffer. |
| Constructor Summary |
|---|
|
Constructs a new empty buffer that sorts in ascending order by the natural order of the objects added. |
|
Constructs a new empty buffer that sorts in ascending order using the specified comparator. |
|
Constructs a new empty buffer specifying the sort order and using the natural order of the objects added. |
|
Constructs a new empty buffer specifying the sort order and comparator. |
|
Constructs a new empty buffer that sorts in ascending order by the natural order of the objects added, specifying an initial capacity. |
|
Constructs a new empty buffer that sorts in ascending order using the specified comparator and initial capacity. |
|
Constructs a new empty buffer that specifying initial capacity and sort order, using the natural order of the objects added. |
|
Constructs a new empty buffer that specifying initial capacity, sort order and comparator. |
| Method Summary | |
|---|---|
| boolean |
Adds an element to the buffer. |
| void |
Clears all elements from the buffer. |
| Comparator |
Gets the comparator being used for this buffer, null is natural order. |
| protected int |
Compares two objects using the comparator if specified, or the natural order otherwise. |
| Object |
Gets the next element to be removed without actually removing it (peek). |
| protected void |
Increases the size of the heap to support additional elements |
| boolean |
Checks whether the heap is ascending or descending order. |
| protected boolean |
Tests if the buffer is at capacity. |
| Iterator |
Returns an iterator over this heap's elements. |
| protected void |
Percolates element down heap from the position given by the index. |
| protected void |
Percolates element down heap from the position given by the index. |
| protected void |
Percolates element up heap from from the position given by the index. |
| protected void |
Percolates a new element up heap from the bottom. |
| protected void |
Percolates element up heap from the position given by the index. |
| protected void |
Percolates a new element up heap from the bottom. |
| Object |
Gets and removes the next element (pop). |
| int |
Returns the number of elements in this buffer. |
| String |
Returns a string representation of this heap. |
| Methods inherited from java.utilAbstractCollection |
|---|
| Methods inherited from java.utilCollection |
|---|
| Methods inherited from java.langObject |
|---|
protected
boolean
ascendingOrder
protected
int
size
public PriorityBuffer
(
)
public PriorityBuffer
(
boolean
ascendingOrder
)
public PriorityBuffer
(
int
capacity
)
public PriorityBuffer
(
int
capacity,
boolean
ascendingOrder
)
The element added will be sorted according to the comparator in use.
public
void
clear
(
)
protected
void
grow
(
)
public
boolean
isAscendingOrder
(
)
protected
void
percolateDownMaxHeap
(
int
index
)
Assumes it is a maximum heap.
protected
void
percolateDownMinHeap
(
int
index
)
Assumes it is a minimum heap.
protected
void
percolateUpMaxHeap
(
int
index
)
Assume it is a maximum heap.
Assume it is a maximum heap.
protected
void
percolateUpMinHeap
(
int
index
)
Assumes it is a minimum heap.
Assumes it is a minimum heap.
public
int
size
(
)