Package org.jheaps.monotone
Class BigIntegerRadixHeap
java.lang.Object
org.jheaps.monotone.BigIntegerRadixHeap
- All Implemented Interfaces:
Serializable,Heap<BigInteger>
A radix heap for
BigInteger keys. The heap stores BigInteger
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 and findMin are worst-case constant time. The cost of
operation deleteMin is amortized O(logC) assuming the radix-heap
contains keys in the range [0, C] or equivalently
[a,a+C].
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:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected List<BigInteger>[]The buckets as lists.protected BigIntegerThe current minimum value (cached)protected intThe current minimum value bucket (cached)protected intThe current minimum value position in bucket (cached)protected static final intDenotes that a key does not belong to a bucketprotected BigIntegerLast deleted key.protected BigIntegerMaximum key allowedprotected BigIntegerMinimum key allowedprotected longNumber of elements -
Constructor Summary
ConstructorsConstructorDescriptionBigIntegerRadixHeap(BigInteger minKey, BigInteger maxKey) Constructs a new heap which can store values between a minimum and a maximum key value (inclusive). -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clear all the elements of this heap.Comparator<? super BigInteger> Always returnsnullsince this heap uses the natural ordering of its keys.protected intcompare(BigInteger o1, BigInteger o2) Compares its two arguments for order.protected intcomputeBucket(BigInteger key, BigInteger minKey) Compute the bucket of a key based on a minimum key.Delete and return an element with the minimum key.findMin()Find an element with the minimum key.voidinsert(BigInteger key) Insert a key into the heap.booleanisEmpty()Returnstrueif this heap is empty.protected intmsd(BigInteger a, BigInteger b) Compute the most significant digit which is different in the binary representation of two values, or -1 if numbers are equal.longsize()Returns the number of elements in this heap.
-
Field Details
-
EMPTY
protected static final int EMPTYDenotes that a key does not belong to a bucket- See Also:
-
buckets
The buckets as lists. We use array-lists instead of linked-lists, to be cache friendly. -
size
protected long sizeNumber of elements -
lastDeletedKey
Last deleted key. This value is used to distribute elements in the buckets. Should be initialized with theminKeyvalue. -
currentMin
The current minimum value (cached) -
currentMinBucket
protected int currentMinBucketThe current minimum value bucket (cached) -
currentMinPos
protected int currentMinPosThe current minimum value position in bucket (cached) -
minKey
Minimum key allowed -
maxKey
Maximum key allowed
-
-
Constructor Details
-
BigIntegerRadixHeap
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 operationdeleteMinrequires 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 Details
-
compare
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.- Parameters:
o1- the first object to be compared.o2- the second object to be compared.- Returns:
- a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
-
msd
Compute the most significant digit which is different in the binary representation of two values, or -1 if numbers are equal.- Parameters:
a- the first valueb- the second value- Returns:
- the most significant digit which is different or -1 if numbers are equal
-
findMin
Find an element with the minimum key. -
insert
Insert a key into the heap.- Specified by:
insertin interfaceHeap<K>- Parameters:
key- the key to insert- 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
Delete and return an element with the minimum key. If multiple such elements exists, only one of them will be deleted. The cost of this operation is amortized O(logC) assuming the heap contains keys in the range [0, C] or equivalently [a, a+C]. -
isEmpty
public boolean isEmpty()Returnstrueif this heap is empty. -
size
public long size()Returns the number of elements in this heap. -
clear
public void clear()Clear all the elements of this heap. -
comparator
Always returnsnullsince this heap uses the natural ordering of its keys.- Specified by:
comparatorin interfaceHeap<K>- Returns:
nullsince this heap uses the natural ordering of its keys
-
computeBucket
Compute the bucket of a key based on a minimum key.- Parameters:
key- the keyminKey- the minimum key- Returns:
- the bucket where the key should go
-