V
- Value typepublic class DoubleObjectMaxHeap<V> extends Object implements DoubleObjectHeap<V>
Modifier and Type | Class and Description |
---|---|
private class |
DoubleObjectMaxHeap.UnsortedIter
Unsorted iterator - in heap order.
|
Modifier and Type | Field and Description |
---|---|
protected int |
size
Current size of heap.
|
private static int |
TWO_HEAP_INITIAL_SIZE
Initial size of the 2-ary heap.
|
protected double[] |
twoheap
Base heap.
|
protected Object[] |
twovals
Base heap values.
|
Constructor and Description |
---|
DoubleObjectMaxHeap()
Constructor, with default size.
|
DoubleObjectMaxHeap(int minsize)
Constructor, with given minimum size.
|
Modifier and Type | Method and Description |
---|---|
void |
add(double o,
V v)
Add a key-value pair to the heap
|
void |
add(double key,
V val,
int max)
Add a key-value pair to the heap if it improves the top.
|
void |
clear()
Clear the heap contents.
|
private void |
heapifyDown(double cur,
Object val)
Invoke heapify-down for the root object.
|
private void |
heapifyUp(int twopos,
double cur,
Object val)
Heapify-Up method for 2-ary heap.
|
boolean |
isEmpty()
Is the heap empty?
|
double |
peekKey()
Get the current top key
|
V |
peekValue()
Get the current top value
|
void |
poll()
Remove the first element
|
void |
replaceTopElement(double reinsert,
V val)
Combined operation that removes the top element, and inserts a new element
instead.
|
int |
size()
Query the size
|
String |
toString() |
DoubleObjectMaxHeap.UnsortedIter |
unsortedIter()
Get an unsorted iterator to inspect the heap.
|
protected double[] twoheap
protected Object[] twovals
protected int size
private static final int TWO_HEAP_INITIAL_SIZE
public DoubleObjectMaxHeap()
public DoubleObjectMaxHeap(int minsize)
minsize
- Minimum sizepublic void clear()
DoubleObjectHeap
clear
in interface DoubleObjectHeap<V>
public int size()
DoubleObjectHeap
size
in interface DoubleObjectHeap<V>
public boolean isEmpty()
DoubleObjectHeap
isEmpty
in interface DoubleObjectHeap<V>
true
when the size is 0.public void add(double o, V v)
DoubleObjectHeap
add
in interface DoubleObjectHeap<V>
o
- Keyv
- Valuepublic void add(double key, V val, int max)
DoubleObjectHeap
add
in interface DoubleObjectHeap<V>
key
- Keyval
- Valuemax
- Desired maximum sizepublic void replaceTopElement(double reinsert, V val)
DoubleObjectHeap
replaceTopElement
in interface DoubleObjectHeap<V>
reinsert
- Key of new elementval
- Value of new elementprivate void heapifyUp(int twopos, double cur, Object val)
twopos
- Position in 2-ary heap.cur
- Current objectval
- Current valuepublic void poll()
DoubleObjectHeap
poll
in interface DoubleObjectHeap<V>
private void heapifyDown(double cur, Object val)
cur
- Object to insert.val
- Value to reinsert.public double peekKey()
DoubleObjectHeap
peekKey
in interface DoubleObjectHeap<V>
public V peekValue()
DoubleObjectHeap
peekValue
in interface DoubleObjectHeap<V>
public DoubleObjectMaxHeap.UnsortedIter unsortedIter()
DoubleObjectHeap
unsortedIter
in interface DoubleObjectHeap<V>