- All Implemented Interfaces:
Serializable,Cloneable,Iterable<Long>,Collection<Long>,Queue<Long>,LongQueue
This is a highly optimized implementation which uses
- the Wegener bottom-up heuristic and
- sentinel values
addAsLong(long) and removeAsLong() operations.
Operation findMin,
is a worst-case O(1) operation. All bounds are worst-case if the user
initializes the heap with a capacity larger or equal to the total number of
elements that are going to be inserted into the heap.
See the following papers for details about the optimizations:
- Ingo Wegener. BOTTOM-UP-HEAPSORT, a new variant of HEAPSORT beating, on an average, QUICKSORT (if n is not very small). Theoretical Computer Science, 118(1), 81--98, 1993.
- Peter Sanders. Fast Priority Queues for Cached Memory. Algorithms Engineering and Experiments (ALENEX), 312--327, 1999.
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
- JHeaps Library
Copyright (c) 2014-2022 Dimitrios Michail. Apache License 2.0. - github.com
- JHeaps Library
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new, empty heap, using the natural ordering of its keys.LongArrayHeap(int capacity) Constructs a new, empty heap, with a provided initial capacity using the natural ordering of its keys. -
Method Summary
Modifier and TypeMethodDescriptionbooleanaddAsLong(long key) voidclear()clone()booleancontainsAsLong(long e) longbooleanisEmpty()iterator()booleanofferAsLong(long key) longbooleanremoveAsLong(long e) intsize()Methods inherited from class java.util.AbstractCollection
add, addAll, contains, containsAll, remove, removeAll, retainAll, toArray, toArray, toStringMethods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray, toArray, toArray
-
Constructor Details
-
LongArrayHeap
public LongArrayHeap()Constructs a new, empty heap, using the natural ordering of its keys.The initial capacity of the heap is
16and adjusts automatically based on the sequence of insertions and deletions. -
LongArrayHeap
public LongArrayHeap(int capacity) Constructs a new, empty heap, with a provided initial capacity using the natural ordering of its keys.The initial capacity of the heap is provided by the user and is adjusted automatically based on the sequence of insertions and deletions. The capacity will never become smaller than the initial requested capacity.
- Parameters:
capacity- the initial heap capacity
-
-
Method Details
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection<Long>- Specified by:
isEmptyin interfaceLongQueue- Overrides:
isEmptyin classAbstractCollection<Long>
-
iterator
- Specified by:
iteratorin interfaceCollection<Long>- Specified by:
iteratorin interfaceIterable<Long>- Specified by:
iteratorin classAbstractCollection<Long>
-
containsAsLong
public boolean containsAsLong(long e) - Specified by:
containsAsLongin interfaceLongQueue
-
size
public int size()- Specified by:
sizein interfaceCollection<Long>- Specified by:
sizein classAbstractCollection<Long>
-
clear
public void clear()- Specified by:
clearin interfaceCollection<Long>- Overrides:
clearin classAbstractCollection<Long>
-
elementAsLong
public long elementAsLong()- Specified by:
elementAsLongin interfaceLongQueue- See Also:
-
offerAsLong
public boolean offerAsLong(long key) - Specified by:
offerAsLongin interfaceLongQueue- See Also:
-
addAsLong
public boolean addAsLong(long key) -
removeAsLong
public long removeAsLong()- Specified by:
removeAsLongin interfaceLongQueue- See Also:
-
removeAsLong
public boolean removeAsLong(long e) - Specified by:
removeAsLongin interfaceLongQueue- See Also:
-
clone
-