Class UpdatablePriorityQueue<T>

java.lang.Object
java.util.AbstractCollection<T>
com.complexible.common.collect.UpdatablePriorityQueue<T>
All Implemented Interfaces:
Iterable<T>, Collection<T>, Queue<T>

public class UpdatablePriorityQueue<T> extends AbstractCollection<T> implements Queue<T>
PriorityQueue class implemented as a binary heap with the additional function that updates the head element if its priority changes.
  • Constructor Details

    • UpdatablePriorityQueue

      public UpdatablePriorityQueue(Comparator<? super T> c)
      Construct an empty PriorityQueue with a specified comparator.
    • UpdatablePriorityQueue

      public UpdatablePriorityQueue(Collection<? extends T> coll, Comparator<? super T> c)
      Construct a PriorityQueue from another Collection.
    • UpdatablePriorityQueue

      public UpdatablePriorityQueue(int initialSize, Comparator<? super T> c)
      Construct a PriorityQueue with an initial size and a specified comparator that can be null if natural .
  • Method Details

    • offer

      public boolean offer(T x)
      Specified by:
      offer in interface Queue<T>
    • add

      public boolean add(T x)
      Specified by:
      add in interface Collection<T>
      Specified by:
      add in interface Queue<T>
      Overrides:
      add in class AbstractCollection<T>
    • size

      public int size()
      Specified by:
      size in interface Collection<T>
      Specified by:
      size in class AbstractCollection<T>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<T>
      Overrides:
      clear in class AbstractCollection<T>
    • iterator

      public Iterator<T> iterator()
      Returns an iterator over the elements in this PriorityQueue. The iterator does not view the elements in any particular order and its does no support remove operation.
      Specified by:
      iterator in interface Collection<T>
      Specified by:
      iterator in interface Iterable<T>
      Specified by:
      iterator in class AbstractCollection<T>
    • element

      public T element()
      Specified by:
      element in interface Queue<T>
    • peek

      public T peek()
      Specified by:
      peek in interface Queue<T>
    • poll

      public T poll()
      Specified by:
      poll in interface Queue<T>
    • remove

      public T remove()
      Specified by:
      remove in interface Queue<T>
    • update

      public void update()
      Updates the queue when the priority of head element and only the the priority of the head element changes.
    • replace

      public T replace(T newElement)
      Replaces the current head element with the given element. The new element is inserted to the queue but might not be the head if it is not the minimum element according to the comparator. This function is equivalent to the following function but more efficient.
       public T replace(T newElement) {
          T result = queue.remove();
          queue.add(newElement);   
          return result;
       }