Package com.complexible.common.collect
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>
PriorityQueue class implemented as a binary heap with the additional function that updates the head element if its
priority changes.
-
Constructor Summary
ConstructorsConstructorDescriptionUpdatablePriorityQueue(int initialSize, Comparator<? super T> c) Construct a PriorityQueue with an initial size and a specified comparator that can be null if natural .UpdatablePriorityQueue(Collection<? extends T> coll, Comparator<? super T> c) Construct a PriorityQueue from another Collection.UpdatablePriorityQueue(Comparator<? super T> c) Construct an empty PriorityQueue with a specified comparator. -
Method Summary
Modifier and TypeMethodDescriptionbooleanvoidclear()element()iterator()Returns an iterator over the elements in this PriorityQueue.booleanpeek()poll()remove()Replaces the current head element with the given element.intsize()voidupdate()Updates the queue when the priority of head element and only the the priority of the head element changes.Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
addAll, contains, containsAll, equals, hashCode, isEmpty, parallelStream, remove, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray, toArray
-
Constructor Details
-
UpdatablePriorityQueue
Construct an empty PriorityQueue with a specified comparator. -
UpdatablePriorityQueue
Construct a PriorityQueue from another Collection. -
UpdatablePriorityQueue
Construct a PriorityQueue with an initial size and a specified comparator that can be null if natural .
-
-
Method Details
-
offer
-
add
- Specified by:
addin interfaceCollection<T>- Specified by:
addin interfaceQueue<T>- Overrides:
addin classAbstractCollection<T>
-
size
public int size()- Specified by:
sizein interfaceCollection<T>- Specified by:
sizein classAbstractCollection<T>
-
clear
public void clear()- Specified by:
clearin interfaceCollection<T>- Overrides:
clearin classAbstractCollection<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:
iteratorin interfaceCollection<T>- Specified by:
iteratorin interfaceIterable<T>- Specified by:
iteratorin classAbstractCollection<T>
-
element
-
peek
-
poll
-
remove
-
update
public void update()Updates the queue when the priority of head element and only the the priority of the head element changes. -
replace
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; }
-