Package io.github.jbellis.jvector.util
Class AbstractLongHeap
java.lang.Object
io.github.jbellis.jvector.util.AbstractLongHeap
- Direct Known Subclasses:
BoundedLongHeap,GrowableLongHeap
A min heap that stores longs; a primitive priority queue that like all priority queues maintains
a partial ordering of its elements such that the least element can always be found in constant
time. Push()'s and pop()'s require log(size).
push(long) may either grow the heap or
replace the worst element, depending on the subclass implementation.
The heap is a min heap, meaning that the top element is the lowest value.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionAbstractLongHeap(int initialSize) Create an empty heap with the configured initial size. -
Method Summary
Modifier and TypeMethodDescriptionprotected longadd(long element) final voidclear()Removes all entries from the PriorityQueue.protected voiddownHeap(int i) longget(int i) Return the element at the ith location in the heap array.final longpop()Removes and returns the least element of the PriorityQueue in log(size) time.abstract booleanpush(long element) Adds a value to an LongHeap in log(size) time.final intsize()Returns the number of elements currently stored in the PriorityQueue.final longtop()Returns the least element of the LongHeap in constant time.protected voidupHeap(int origPos)
-
Field Details
-
heap
protected long[] heap -
size
protected int size
-
-
Constructor Details
-
AbstractLongHeap
public AbstractLongHeap(int initialSize) Create an empty heap with the configured initial size.- Parameters:
initialSize- the initial size of the heap
-
-
Method Details
-
push
public abstract boolean push(long element) Adds a value to an LongHeap in log(size) time.- Returns:
- true if the new value was added. (A fixed-size heap will not add the new value if it is full, and the new value is worse than the existing ones.)
-
add
protected long add(long element) -
top
public final long top()Returns the least element of the LongHeap in constant time. It is up to the caller to verify that the heap is not empty; no checking is done, and if no elements have been added, 0 is returned. -
pop
public final long pop()Removes and returns the least element of the PriorityQueue in log(size) time.- Throws:
IllegalStateException- if the LongHeap is empty.
-
size
public final int size()Returns the number of elements currently stored in the PriorityQueue. -
clear
public final void clear()Removes all entries from the PriorityQueue. -
upHeap
protected void upHeap(int origPos) -
downHeap
protected void downHeap(int i) -
get
public long get(int i) Return the element at the ith location in the heap array. Use for iterating over elements when the order doesn't matter. Note that the valid arguments range from [1, size].
-