Class LongRadixAddressableHeap<V>
- java.lang.Object
-
- org.jheaps.monotone.LongRadixAddressableHeap<V>
-
- Type Parameters:
V
- the type of values maintained by this heap
- All Implemented Interfaces:
Serializable
,AddressableHeap<Long,V>
public class LongRadixAddressableHeap<V> extends Object
An addressable radix heap for (signed) long keys. The heap stores long keys sorted according to the natural ordering of its keys. A radix heap is a monotone heap, especially designed for algorithms (such as Dijkstra) which scan elements in order of nondecreasing keys.The implementation uses arrays in order to store the elements. Operations
insert
andfindMin
are worst-case constant time. The cost of operationdeleteMin
is amortized O(logC) assuming the radix-heap contains keys in the range [0, C] or equivalently [a,a+C]. This implementation views long values as signed numbers.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
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.jheaps.AddressableHeap
AddressableHeap.Handle<K,V>
-
-
Constructor Summary
Constructors Constructor Description LongRadixAddressableHeap(long minKey, long maxKey)
Constructs a new heap which can store values between a minimum and a maximum key value (inclusive).
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Clear all the elements of the heap.Comparator<? super K>
comparator()
Always returnsnull
since this heap uses the natural ordering of its keys.AddressableHeap.Handle<K,V>
deleteMin()
Delete and return an element with the minimum key.AddressableHeap.Handle<K,V>
findMin()
Find an element with the minimum key.AddressableHeap.Handle<K,V>
insert(K key)
Insert a new element into the heap with a null value.AddressableHeap.Handle<K,V>
insert(K key, V value)
Insert a new element into the heap.boolean
isEmpty()
Returnstrue
if this heap is empty.long
size()
Returns the number of elements in the heap.
-
-
-
Constructor Detail
-
LongRadixAddressableHeap
public LongRadixAddressableHeap(long minKey, long maxKey)
Constructs a new heap which can store values between a minimum and a maximum key value (inclusive). It is important to use the smallest key range as the heap uses O(logC) where C=maxKey-minKey+1 buckets to store elements. Moreover, the operationdeleteMin
requires amortized O(logC) time.- Parameters:
minKey
- the non-negative minimum key that this heap supports (inclusive)maxKey
- the maximum key that this heap supports (inclusive)- Throws:
IllegalArgumentException
- if the minimum key is negativeIllegalArgumentException
- if the maximum key is less than the minimum key
-
-
Method Detail
-
findMin
public AddressableHeap.Handle<K,V> findMin()
Find an element with the minimum key.- Specified by:
findMin
in interfaceAddressableHeap<K,V>
- Returns:
- a handle to an element with minimum key
-
insert
public AddressableHeap.Handle<K,V> insert(K key)
Insert a new element into the heap with a null value.- Specified by:
insert
in interfaceAddressableHeap<K,V>
- Parameters:
key
- the element's key- Returns:
- a handle for the newly added element
- Throws:
IllegalArgumentException
- if the key is nullIllegalArgumentException
- if the key is less than the minimum allowed keyIllegalArgumentException
- if the key is more than the maximum allowed keyIllegalArgumentException
- if the key is less than the last deleted key (or the minimum key allowed if no key has been deleted)
-
insert
public AddressableHeap.Handle<K,V> insert(K key, V value)
Insert a new element into the heap.- Specified by:
insert
in interfaceAddressableHeap<K,V>
- Parameters:
key
- the element's keyvalue
- the element's value- Returns:
- a handle for the newly added element
- Throws:
IllegalArgumentException
- if the key is nullIllegalArgumentException
- if the key is less than the minimum allowed keyIllegalArgumentException
- if the key is more than the maximum allowed keyIllegalArgumentException
- if the key is less than the last deleted key (or the minimum key allowed if no key has been deleted)
-
deleteMin
public AddressableHeap.Handle<K,V> deleteMin()
Delete and return an element with the minimum key. If multiple such elements exists, only one of them will be deleted. After the element is deleted the handle is invalidated and only methodAddressableHeap.Handle.getKey()
andAddressableHeap.Handle.getValue()
can be used. The cost of this operation is amortized O(logC) assuming the heap contains keys in the range [0, C] or equivalently [a, a+C].- Specified by:
deleteMin
in interfaceAddressableHeap<K,V>
- Returns:
- a handle to the deleted element with minimum key
-
isEmpty
public boolean isEmpty()
Returnstrue
if this heap is empty.- Specified by:
isEmpty
in interfaceAddressableHeap<K,V>
- Returns:
true
if this heap is empty,false
otherwise
-
size
public long size()
Returns the number of elements in the heap.- Specified by:
size
in interfaceAddressableHeap<K,V>
- Returns:
- the number of elements in the heap
-
clear
public void clear()
Clear all the elements of the heap. After calling this method all handles should be considered invalidated and the behavior of methodsAddressableHeap.Handle.decreaseKey(Object)
andAddressableHeap.Handle.delete()
is undefined.- Specified by:
clear
in interfaceAddressableHeap<K,V>
-
comparator
public Comparator<? super K> comparator()
Always returnsnull
since this heap uses the natural ordering of its keys.- Specified by:
comparator
in interfaceAddressableHeap<K,V>
- Returns:
null
since this heap uses the natural ordering of its keys
-
-