Class DoubleRadixHeap

java.lang.Object
org.jheaps.monotone.DoubleRadixHeap
All Implemented Interfaces:
Serializable, Heap<Double>

public class DoubleRadixHeap extends Object
A radix heap for double keys. The heap stores double keys sorted according to the natural ordering of its keys. A radix heap is a monotone heap, especially designed for algorithms (such as Dijkstra) which scan elements in order of nondecreasing keys.

Note that this implementation uses the fact that the IEEE floating-point standard has the property that for any valid floating-point numbers a and b, a<=b if and only if bits(a)<= bits(b), where bits(x) denotes the re-interpretation of x as an unsigned integer (long in our case).

This implementation uses arrays in order to store the elements. Operations insert and findMin are worst-case constant time. The cost of operation deleteMin is amortized O(logC) assuming the radix-heap contains keys in the range [0, C] or equivalently [a,a+C]. Note, however, that C here depends on the distance of the minimum and maximum value when they are translated into unsigned longs.

Note that this implementation is not synchronized. If multiple threads access a heap concurrently, and at least one of the threads modifies the heap structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements or changing the key of some element.) This is typically accomplished by synchronizing on some object that naturally encapsulates the heap.

Author:
Dimitrios Michail
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected List<Double>[]
    The buckets as lists.
    protected Double
    The current minimum value (cached)
    protected int
    The current minimum value bucket (cached)
    protected int
    The current minimum value position in bucket (cached)
    protected static final int
    Denotes that a key does not belong to a bucket
    protected Double
    Last deleted key.
    protected Double
    Maximum key allowed
    protected Double
    Minimum key allowed
    protected long
    Number of elements
  • Constructor Summary

    Constructors
    Constructor
    Description
    DoubleRadixHeap(double minKey, double maxKey)
    Constructs a new heap which can store values between a minimum and a maximum key value (inclusive).
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clear all the elements of this heap.
    Always returns null since this heap uses the natural ordering of its keys.
    protected int
    Compares its two arguments for order.
    protected int
    computeBucket(Double key, Double minKey)
    Compute the bucket of a key based on a minimum key.
    Delete and return an element with the minimum key.
    Find an element with the minimum key.
    void
    Insert a key into the heap.
    boolean
    Returns true if this heap is empty.
    protected int
    msd(Double a, Double b)
    Compute the most significant digit which is different in the binary representation of two values, or -1 if numbers are equal.
    long
    Returns the number of elements in this heap.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • EMPTY

      protected static final int EMPTY
      Denotes that a key does not belong to a bucket
      See Also:
    • buckets

      protected List<Double>[] buckets
      The buckets as lists. We use array-lists instead of linked-lists, to be cache friendly.
    • size

      protected long size
      Number of elements
    • lastDeletedKey

      protected Double lastDeletedKey
      Last deleted key. This value is used to distribute elements in the buckets. Should be initialized with the minKey value.
    • currentMin

      protected Double currentMin
      The current minimum value (cached)
    • currentMinBucket

      protected int currentMinBucket
      The current minimum value bucket (cached)
    • currentMinPos

      protected int currentMinPos
      The current minimum value position in bucket (cached)
    • minKey

      protected Double minKey
      Minimum key allowed
    • maxKey

      protected Double maxKey
      Maximum key allowed
  • Constructor Details

    • DoubleRadixHeap

      public DoubleRadixHeap(double minKey, double maxKey)
      Constructs a new heap which can store values between a minimum and a maximum key value (inclusive). It is important to use the smallest key range as the heap uses O(logC) where C=maxKey-minKey+1 buckets to store elements. Moreover, the operation deleteMin requires amortized O(logC) time.
      Parameters:
      minKey - the non-negative minimum key that this heap supports (inclusive)
      maxKey - the maximum key that this heap supports (inclusive)
      Throws:
      IllegalArgumentException - if the minimum key is negative
      IllegalArgumentException - if the maximum key is less than the minimum key
  • Method Details

    • compare

      protected int compare(Double o1, Double o2)
      Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
      Parameters:
      o1 - the first object to be compared.
      o2 - the second object to be compared.
      Returns:
      a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
    • msd

      protected int msd(Double a, Double b)
      Compute the most significant digit which is different in the binary representation of two values, or -1 if numbers are equal.
      Parameters:
      a - the first value
      b - the second value
      Returns:
      the most significant digit which is different or -1 if numbers are equal
    • findMin

      public Double findMin()
      Find an element with the minimum key.
      Specified by:
      findMin in interface Heap<K>
      Returns:
      an element with the minimum key
    • insert

      public void insert(Double key)
      Insert a key into the heap.
      Specified by:
      insert in interface Heap<K>
      Parameters:
      key - the key to insert
      Throws:
      IllegalArgumentException - if the key is null
      IllegalArgumentException - if the key is less than the minimum allowed key
      IllegalArgumentException - if the key is more than the maximum allowed key
      IllegalArgumentException - if the key is less than the last deleted key (or the minimum key allowed if no key has been deleted)
    • deleteMin

      public Double deleteMin()
      Delete and return an element with the minimum key. If multiple such elements exists, only one of them will be deleted. The cost of this operation is amortized O(logC) assuming the heap contains keys in the range [0, C] or equivalently [a, a+C].
      Specified by:
      deleteMin in interface Heap<K>
      Returns:
      the deleted element with the minimum key
    • isEmpty

      public boolean isEmpty()
      Returns true if this heap is empty.
      Specified by:
      isEmpty in interface Heap<K>
      Returns:
      true if this heap is empty, false otherwise
    • size

      public long size()
      Returns the number of elements in this heap.
      Specified by:
      size in interface Heap<K>
      Returns:
      the number of elements in this heap
    • clear

      public void clear()
      Clear all the elements of this heap.
      Specified by:
      clear in interface Heap<K>
    • comparator

      public Comparator<? super Double> comparator()
      Always returns null since this heap uses the natural ordering of its keys.
      Specified by:
      comparator in interface Heap<K>
      Returns:
      null since this heap uses the natural ordering of its keys
    • computeBucket

      protected int computeBucket(Double key, Double minKey)
      Compute the bucket of a key based on a minimum key.
      Parameters:
      key - the key
      minKey - the minimum key
      Returns:
      the bucket where the key should go