Class ObjectHeapPriorityQueue<K>

  • All Implemented Interfaces:
    PriorityQueue<K>, java.io.Serializable

    public class ObjectHeapPriorityQueue<K>
    extends java.lang.Object
    implements PriorityQueue<K>, java.io.Serializable
    A type-specific heap-based priority queue.

    Instances of this class represent a priority queue using a heap. The heap is enlarged as needed, but it is never shrunk. Use the trim() method to reduce its size, if necessary.

    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      ObjectHeapPriorityQueue()
      Creates a new empty queue using the natural order.
      ObjectHeapPriorityQueue​(int capacity)
      Creates a new empty queue with a given capacity and using the natural order.
      ObjectHeapPriorityQueue​(int capacity, java.util.Comparator<? super K> c)
      Creates a new empty queue with a given capacity and comparator.
      ObjectHeapPriorityQueue​(java.util.Collection<? extends K> collection)
      Creates a queue using the elements in a collection using the natural order.
      ObjectHeapPriorityQueue​(java.util.Collection<? extends K> collection, java.util.Comparator<? super K> c)
      Creates a queue using the elements in a collection using a given comparator.
      ObjectHeapPriorityQueue​(java.util.Comparator<? super K> c)
      Creates a new empty queue with a given comparator.
      ObjectHeapPriorityQueue​(K[] a)
      Wraps a given array in a queue using the natural order.
      ObjectHeapPriorityQueue​(K[] a, int size)
      Wraps a given array in a queue using the natural order.
      ObjectHeapPriorityQueue​(K[] a, int size, java.util.Comparator<? super K> c)
      Wraps a given array in a queue using a given comparator.
      ObjectHeapPriorityQueue​(K[] a, java.util.Comparator<? super K> c)
      Wraps a given array in a queue using a given comparator.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void changed()
      Notifies the queue that the first element has changed (optional operation).
      void clear()
      Removes all elements from this queue.
      java.util.Comparator<? super K> comparator()
      Returns the comparator associated with this queue, or null if it uses its elements' natural ordering.
      K dequeue()
      Dequeues the first element from the queue.
      void enqueue​(K x)
      Enqueues a new element.
      K first()
      Returns the first element of the queue.
      int size()
      Returns the number of elements in this queue.
      void trim()
      Trims the underlying heap array so that it has exactly size() elements.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ObjectHeapPriorityQueue

        public ObjectHeapPriorityQueue​(int capacity,
                                       java.util.Comparator<? super K> c)
        Creates a new empty queue with a given capacity and comparator.
        Parameters:
        capacity - the initial capacity of this queue.
        c - the comparator used in this queue, or null for the natural order.
      • ObjectHeapPriorityQueue

        public ObjectHeapPriorityQueue​(int capacity)
        Creates a new empty queue with a given capacity and using the natural order.
        Parameters:
        capacity - the initial capacity of this queue.
      • ObjectHeapPriorityQueue

        public ObjectHeapPriorityQueue​(java.util.Comparator<? super K> c)
        Creates a new empty queue with a given comparator.
        Parameters:
        c - the comparator used in this queue, or null for the natural order.
      • ObjectHeapPriorityQueue

        public ObjectHeapPriorityQueue()
        Creates a new empty queue using the natural order.
      • ObjectHeapPriorityQueue

        public ObjectHeapPriorityQueue​(K[] a,
                                       int size,
                                       java.util.Comparator<? super K> c)
        Wraps a given array in a queue using a given comparator.

        The queue returned by this method will be backed by the given array. The first size element of the array will be rearranged so to form a heap (this is more efficient than enqueing the elements of a one by one).

        Parameters:
        a - an array.
        size - the number of elements to be included in the queue.
        c - the comparator used in this queue, or null for the natural order.
      • ObjectHeapPriorityQueue

        public ObjectHeapPriorityQueue​(K[] a,
                                       java.util.Comparator<? super K> c)
        Wraps a given array in a queue using a given comparator.

        The queue returned by this method will be backed by the given array. The elements of the array will be rearranged so to form a heap (this is more efficient than enqueing the elements of a one by one).

        Parameters:
        a - an array.
        c - the comparator used in this queue, or null for the natural order.
      • ObjectHeapPriorityQueue

        public ObjectHeapPriorityQueue​(K[] a,
                                       int size)
        Wraps a given array in a queue using the natural order.

        The queue returned by this method will be backed by the given array. The first size element of the array will be rearranged so to form a heap (this is more efficient than enqueing the elements of a one by one).

        Parameters:
        a - an array.
        size - the number of elements to be included in the queue.
      • ObjectHeapPriorityQueue

        public ObjectHeapPriorityQueue​(K[] a)
        Wraps a given array in a queue using the natural order.

        The queue returned by this method will be backed by the given array. The elements of the array will be rearranged so to form a heap (this is more efficient than enqueing the elements of a one by one).

        Parameters:
        a - an array.
      • ObjectHeapPriorityQueue

        public ObjectHeapPriorityQueue​(java.util.Collection<? extends K> collection,
                                       java.util.Comparator<? super K> c)
        Creates a queue using the elements in a collection using a given comparator.

        This constructor is more efficient than enqueing the elements of collection one by one.

        Parameters:
        collection - a collection; its elements will be used to initialize the queue.
        c - the comparator used in this queue, or null for the natural order.
      • ObjectHeapPriorityQueue

        public ObjectHeapPriorityQueue​(java.util.Collection<? extends K> collection)
        Creates a queue using the elements in a collection using the natural order.

        This constructor is more efficient than enqueing the elements of collection one by one.

        Parameters:
        collection - a collection; its elements will be used to initialize the queue.
    • Method Detail

      • enqueue

        public void enqueue​(K x)
        Description copied from interface: PriorityQueue
        Enqueues a new element.
        Specified by:
        enqueue in interface PriorityQueue<K>
        Parameters:
        x - the element to enqueue.
      • dequeue

        public K dequeue()
        Description copied from interface: PriorityQueue
        Dequeues the first element from the queue.
        Specified by:
        dequeue in interface PriorityQueue<K>
        Returns:
        the dequeued element.
      • first

        public K first()
        Description copied from interface: PriorityQueue
        Returns the first element of the queue.
        Specified by:
        first in interface PriorityQueue<K>
        Returns:
        the first element.
      • changed

        public void changed()
        Description copied from interface: PriorityQueue
        Notifies the queue that the first element has changed (optional operation).

        This default implementation just throws an UnsupportedOperationException.

        Specified by:
        changed in interface PriorityQueue<K>
      • size

        public int size()
        Description copied from interface: PriorityQueue
        Returns the number of elements in this queue.
        Specified by:
        size in interface PriorityQueue<K>
        Returns:
        the number of elements in this queue.
      • clear

        public void clear()
        Description copied from interface: PriorityQueue
        Removes all elements from this queue.
        Specified by:
        clear in interface PriorityQueue<K>
      • trim

        public void trim()
        Trims the underlying heap array so that it has exactly size() elements.
      • comparator

        public java.util.Comparator<? super K> comparator()
        Description copied from interface: PriorityQueue
        Returns the comparator associated with this queue, or null if it uses its elements' natural ordering.
        Specified by:
        comparator in interface PriorityQueue<K>
        Returns:
        the comparator associated with this sorted set, or null if it uses its elements' natural ordering.