Class LongHeapSemiIndirectPriorityQueue
- All Implemented Interfaces:
IndirectPriorityQueue<Long>
,LongIndirectPriorityQueue
- Direct Known Subclasses:
LongHeapIndirectPriorityQueue
public class LongHeapSemiIndirectPriorityQueue extends Object implements LongIndirectPriorityQueue
Instances of this class use as reference list a reference array,
which must be provided to each constructor. The priority queue is represented
using a heap. The heap is enlarged as needed, but it is never shrunk. Use the
trim()
method to reduce its size, if necessary.
This implementation allows one to enqueue several time the same index, but
you must be careful when calling changed()
.
-
Constructor Summary
Constructors Constructor Description LongHeapSemiIndirectPriorityQueue(long[] refArray)
Creates a new empty queue with capacity equal to the length of the reference array and using the natural order.LongHeapSemiIndirectPriorityQueue(long[] refArray, int capacity)
Creates a new empty queue with given capacity and using the natural order.LongHeapSemiIndirectPriorityQueue(long[] refArray, int[] a)
Wraps a given array in a queue using the natural order.LongHeapSemiIndirectPriorityQueue(long[] refArray, int[] a, int size)
Wraps a given array in a queue using the natural order.LongHeapSemiIndirectPriorityQueue(long[] refArray, int[] a, int size, LongComparator c)
Wraps a given array in a queue using a given comparator.LongHeapSemiIndirectPriorityQueue(long[] refArray, int[] a, LongComparator c)
Wraps a given array in a queue using a given comparator.LongHeapSemiIndirectPriorityQueue(long[] refArray, int capacity, LongComparator c)
Creates a new empty queue without elements with a given capacity and comparator.LongHeapSemiIndirectPriorityQueue(long[] refArray, LongComparator c)
Creates a new empty queue with capacity equal to the length of the reference array and a given comparator. -
Method Summary
Modifier and Type Method Description void
allChanged()
Rebuilds this heap in a bottom-up fashion (in linear time).void
changed()
Notifies this queue that the first element has changed (optional operation).void
clear()
Removes all elements from this queue.LongComparator
comparator()
Returns the type-specific comparator associated with this queue.int
dequeue()
Dequeues the first element from this queue.void
enqueue(int x)
Enqueues a new element.int
first()
Returns the first element of this queue.int
front(int[] a)
Writes in the provided array the front of the queue, that is, the set of indices whose elements have the same priority as the top.int
size()
Returns the number of elements in this queue.String
toString()
void
trim()
Trims the backing array so that it has exactlysize()
elements.
-
Constructor Details
-
LongHeapSemiIndirectPriorityQueue
Creates a new empty queue without elements with a given capacity and comparator.- Parameters:
refArray
- the reference array.capacity
- the initial capacity of this queue.c
- the comparator used in this queue, ornull
for the natural order.
-
LongHeapSemiIndirectPriorityQueue
public LongHeapSemiIndirectPriorityQueue(long[] refArray, int capacity)Creates a new empty queue with given capacity and using the natural order.- Parameters:
refArray
- the reference array.capacity
- the initial capacity of this queue.
-
LongHeapSemiIndirectPriorityQueue
Creates a new empty queue with capacity equal to the length of the reference array and a given comparator.- Parameters:
refArray
- the reference array.c
- the comparator used in this queue, ornull
for the natural order.
-
LongHeapSemiIndirectPriorityQueue
public LongHeapSemiIndirectPriorityQueue(long[] refArray)Creates a new empty queue with capacity equal to the length of the reference array and using the natural order.- Parameters:
refArray
- the reference array.
-
LongHeapSemiIndirectPriorityQueue
Wraps a given array in a queue using a given comparator.The queue returned by this method will be backed by the given array. The first
size
element of the array will be rearranged so to form a heap (this is more efficient than enqueing the elements ofa
one by one).- Parameters:
refArray
- the reference array.a
- an array of indices intorefArray
.size
- the number of elements to be included in the queue.c
- the comparator used in this queue, ornull
for the natural order.
-
LongHeapSemiIndirectPriorityQueue
Wraps a given array in a queue using a given comparator.The queue returned by this method will be backed by the given array. The elements of the array will be rearranged so to form a heap (this is more efficient than enqueing the elements of
a
one by one).- Parameters:
refArray
- the reference array.a
- an array of indices intorefArray
.c
- the comparator used in this queue, ornull
for the natural order.
-
LongHeapSemiIndirectPriorityQueue
public LongHeapSemiIndirectPriorityQueue(long[] refArray, int[] a, int size)Wraps a given array in a queue using the natural order.The queue returned by this method will be backed by the given array. The first
size
element of the array will be rearranged so to form a heap (this is more efficient than enqueing the elements ofa
one by one).- Parameters:
refArray
- the reference array.a
- an array of indices intorefArray
.size
- the number of elements to be included in the queue.
-
LongHeapSemiIndirectPriorityQueue
public LongHeapSemiIndirectPriorityQueue(long[] refArray, int[] a)Wraps a given array in a queue using the natural order.The queue returned by this method will be backed by the given array. The elements of the array will be rearranged so to form a heap (this is more efficient than enqueing the elements of
a
one by one).- Parameters:
refArray
- the reference array.a
- an array of indices intorefArray
.
-
-
Method Details
-
enqueue
public void enqueue(int x)Description copied from interface:IndirectPriorityQueue
Enqueues a new element.- Specified by:
enqueue
in interfaceIndirectPriorityQueue<Long>
- Parameters:
x
- the element to enqueue.
-
dequeue
public int dequeue()Description copied from interface:IndirectPriorityQueue
Dequeues the first element from this queue.- Specified by:
dequeue
in interfaceIndirectPriorityQueue<Long>
- Returns:
- the dequeued element.
-
first
public int first()Description copied from interface:IndirectPriorityQueue
Returns the first element of this queue.- Specified by:
first
in interfaceIndirectPriorityQueue<Long>
- Returns:
- the first element.
-
changed
public void changed()Notifies this queue that the first element has changed (optional operation).This default implementation just calls
IndirectPriorityQueue.changed(int)
with argumentIndirectPriorityQueue.first()
.The caller must guarantee that when this method is called the index of the first element appears just once in the queue. Failure to do so will bring the queue in an inconsistent state, and will cause unpredictable behaviour.
- Specified by:
changed
in interfaceIndirectPriorityQueue<Long>
-
allChanged
public void allChanged()Rebuilds this heap in a bottom-up fashion (in linear time).- Specified by:
allChanged
in interfaceIndirectPriorityQueue<Long>
-
size
public int size()Description copied from interface:IndirectPriorityQueue
Returns the number of elements in this queue.- Specified by:
size
in interfaceIndirectPriorityQueue<Long>
- Returns:
- the number of elements in this queue.
-
clear
public void clear()Description copied from interface:IndirectPriorityQueue
Removes all elements from this queue.- Specified by:
clear
in interfaceIndirectPriorityQueue<Long>
-
trim
public void trim()Trims the backing array so that it has exactlysize()
elements. -
comparator
Description copied from interface:LongIndirectPriorityQueue
Returns the type-specific comparator associated with this queue.Note that this specification strengthens the one given in
IndirectPriorityQueue
.- Specified by:
comparator
in interfaceIndirectPriorityQueue<Long>
- Specified by:
comparator
in interfaceLongIndirectPriorityQueue
- Returns:
- the comparator associated with this queue.
- See Also:
IndirectPriorityQueue.comparator()
-
front
public int front(int[] a)Writes in the provided array the front of the queue, that is, the set of indices whose elements have the same priority as the top.- Specified by:
front
in interfaceIndirectPriorityQueue<Long>
- Parameters:
a
- an array whose initial part will be filled with the frnot (must be sized as least as the heap size).- Returns:
- the number of elements of the front.
-
toString
-