Class AbstractLongHeap

java.lang.Object
io.github.jbellis.jvector.util.AbstractLongHeap
Direct Known Subclasses:
BoundedLongHeap, GrowableLongHeap

public abstract class AbstractLongHeap extends Object
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
    Modifier and Type
    Field
    Description
    protected long[]
     
    protected int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    AbstractLongHeap(int initialSize)
    Create an empty heap with the configured initial size.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected long
    add(long element)
     
    final void
    Removes all entries from the PriorityQueue.
    protected void
    downHeap(int i)
     
    long
    get(int i)
    Return the element at the ith location in the heap array.
    final long
    pop()
    Removes and returns the least element of the PriorityQueue in log(size) time.
    abstract boolean
    push(long element)
    Adds a value to an LongHeap in log(size) time.
    final int
    Returns the number of elements currently stored in the PriorityQueue.
    final long
    top()
    Returns the least element of the LongHeap in constant time.
    protected void
    upHeap(int origPos)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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].