Class TrackedHeap<E extends TrackedHeap.Element>
java.lang.Object
de.tomatengames.util.data.TrackedHeap<E>
- Type Parameters:
E- The type of the elements in the heap. Must extendTrackedHeap.Element.
A heap that allows to efficiently update and remove arbitrary elements.
The first element is the smallest element according to the Comparator specified to the constructor.
This implementation is not thread-safe.
- Since:
- 1.5
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionTrackedHeap(@NotNull Comparator<E> comparator) Creates a new and emptyTrackedHeap. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all elements from this heap.booleanReturns if this heap contains the specified element.voidMoves the specified element to its position in the heap if it decreased.getFirst()Returns the first element of this heap.voidMoves the specified element to its position in the heap if it increased.booleanInserts the specified element into this heap.booleanisEmpty()Returns if this heap is empty.voidMoves the specified element to its position in the heap.booleanRemoves the specified element from this heap.Returns and removes the first element of this heap.intsize()Returns the number of elements in this heap.
-
Constructor Details
-
TrackedHeap
Creates a new and emptyTrackedHeap.- Parameters:
comparator- The comparator that will be used to compare elements. Notnull. The smallest element is the first element.
-
-
Method Details
-
insert
Inserts the specified element into this heap. If the element is already in this heap, its position in the heap is updated usingmove(Element).- Parameters:
element- The element that should be inserted. Ifnull, nothing happens.- Returns:
- If the element has been inserted. If
false, it was already in the heap and may have been moved. - Throws:
IllegalArgumentException- If the element is already in another heap.- Implementation Note:
- O(log n)
-
remove
Removes the specified element from this heap. If the element is not in this heap, nothing happens.- Parameters:
element- The element that should be removed. Ifnull, nothing happens.- Returns:
- If the element has been removed successfully. If
false, nothing happened. - Implementation Note:
- O(log n)
-
getFirst
Returns the first element of this heap. The first element is the smallest element according to theComparatorof the heap.- Returns:
- The first element of this heap. If this heap is empty,
nullis returned. - Implementation Note:
- O(1)
-
removeFirst
Returns and removes the first element of this heap. The first element is the smallest element according to theComparatorof the heap.- Returns:
- The first element of this heap. If this heap is empty,
nullis returned and nothing happens. - Implementation Note:
- O(log n)
-
move
Moves the specified element to its position in the heap. This method should be called after the element changed its value considered by theComparatorin order to repair the heap.If the element is not present in this heap, nothing happens.
This method should be preferred over the following semantically equivalent calls.
heap.increase(element); heap.decrease(element);
andif (heap.remove(element)) { heap.insert(element); }- Parameters:
element- The element that has been changed and should be moved. Ifnull, nothing happens.- Implementation Note:
- O(log n)
-
increase
Moves the specified element to its position in the heap if it increased. This method should be called after the element increased its value considered by theComparatorin order to repair the heap. Usemove(Element)instead if the value may have decreased.If the element is not present in this heap, nothing happens. If the element decreased instead of increased, nothing happens.
- Parameters:
element- The element that has been changed and should be moved. Ifnull, nothing happens.- See Also:
- Implementation Note:
- O(log n)
-
decrease
Moves the specified element to its position in the heap if it decreased. This method should be called after the element decreased its value considered by theComparatorin order to repair the heap. Usemove(Element)instead if the value may have increased.If the element is not present in this heap, nothing happens. If the element increased instead of decreased, nothing happens.
- Parameters:
element- The element that has been changed and should be moved. Ifnull, nothing happens.- See Also:
- Implementation Note:
- O(log n)
-
clear
public void clear()Removes all elements from this heap. The heap will be empty after this operation.- Implementation Note:
- O(n)
-
isEmpty
public boolean isEmpty()Returns if this heap is empty.- Returns:
- If this heap is empty.
-
size
public int size()Returns the number of elements in this heap.- Returns:
- The number of elements in this heap. Not negative.
-
contains
-