public abstract class ObjectHeap<K> extends AbstractHeap
Modifier and Type | Field and Description |
---|---|
protected Object[] |
queue
Heap storage: queue
|
DEFAULT_INITIAL_CAPACITY, modCount, size, validSize
Constructor and Description |
---|
ObjectHeap(int size)
Constructor with initial capacity.
|
Modifier and Type | Method and Description |
---|---|
void |
add(Object key)
Add a key-value pair to the heap
|
void |
add(Object key,
int max)
Add a key-value pair to the heap, except if the new element is larger than
the top, and we are at design size (overflow)
|
void |
clear()
Delete all elements from the heap.
|
protected abstract boolean |
comp(Object o1,
Object o2)
Compare two objects
|
protected void |
ensureValid()
Repair the heap
|
protected boolean |
heapifyDown(int ipos,
Object curkey)
Execute a "Heapify Downwards" aka "SiftDown".
|
protected void |
heapifyUp(int pos,
Object curkey)
Execute a "Heapify Upwards" aka "SiftUp".
|
Object |
peek()
Get the current top key
|
Object |
poll()
Remove the first element
|
protected Object |
removeAt(int pos)
Remove the element at the given position.
|
Object |
replaceTopElement(Object e)
Combined operation that removes the top element, and inserts a new element
instead.
|
protected void |
resize(int requiredSize)
Test whether we need to resize to have the requested capacity.
|
desiredSize, heapModified, size
protected transient Object[] queue
public ObjectHeap(int size)
size
- initial capacitypublic void add(Object key)
key
- Keypublic void add(Object key, int max)
key
- Keymax
- Maximum size of heappublic Object replaceTopElement(Object e)
e
- New element to insertpublic Object peek()
public Object poll()
protected void ensureValid()
protected Object removeAt(int pos)
pos
- Element position.protected void heapifyUp(int pos, Object curkey)
pos
- insertion positioncurkey
- Current keyprotected boolean heapifyDown(int ipos, Object curkey)
ipos
- re-insertion positioncurkey
- Current keyprotected final void resize(int requiredSize)
requiredSize
- required capacitypublic void clear()
clear
in class AbstractHeap