@Generated(date="2017-07-11T19:16:23+0200",
value="KTypeIndexedHeapPriorityQueue.java")
public class DoubleIndexedHeapPriorityQueue
extends java.lang.Object
implements IntDoubleMap, java.lang.Cloneable
doubles,
i.e. top() is the smallest element of the queue.
as defined by Sedgewick: Algorithms 4th Edition (2011).
This class is also a IntDoubleMap, and acts like a (K,V) = (int, double) map with >= 0 keys.
It assures O(log(N)) complexity for insertion, deletion, updating priorities,
and constant time to examine the min element by top() and for containsKey(int).
Important:
Ordering of elements must be defined either
* by natural ordering
* or by a custom comparator provided in constructors,
see comparator() .
Warning : This implementation uses direct indexing, meaning that a map
at any given time is only able to have int keys in
the [0 ; capacity()[ range. So when a put(int, double) occurs, the map may be resized to be able hold a key exceeding the current capacity.
| Modifier and Type | Class and Description |
|---|---|
class |
DoubleIndexedHeapPriorityQueue.EntryIterator
An iterator implementation for
iterator() entries. |
class |
DoubleIndexedHeapPriorityQueue.KeysCollection
A view of the keys inside this Indexed heap.
|
class |
DoubleIndexedHeapPriorityQueue.KeysIterator
An iterator over the set of assigned keys.
|
class |
DoubleIndexedHeapPriorityQueue.ValuesCollection
A view over the set of values of this map.
|
class |
DoubleIndexedHeapPriorityQueue.ValuesIterator
An iterator over the set of assigned values.
|
| Modifier and Type | Field and Description |
|---|---|
double[] |
buffer
Internal array for storing the priority queue.
|
protected DoubleComparator |
comparator
Defines the Comparator ordering of the queue,
If null, natural ordering is used.
|
protected double |
defaultValue |
protected int |
elementsCount
Number of elements in the queue.
|
protected IteratorPool<IntDoubleCursor,DoubleIndexedHeapPriorityQueue.EntryIterator> |
entryIteratorPool
internal pool of EntryIterator (must be created in constructor)
|
int[] |
pq
Internal array for storing index to buffer position matching
i.e for an index i, pq[i] is the position of element in priority queue buffer.
|
protected int[] |
qp
Internal array pq inversing :
i.e for a priority buffer position pos, qp[pos] is the index of the value.,
ie qp[pq|i]] = i
|
| Constructor and Description |
|---|
DoubleIndexedHeapPriorityQueue()
Default constructor: create with a default
numbers of elements (
Containers.DEFAULT_EXPECTED_ELEMENTS),
using the natural ordering of doubles. |
DoubleIndexedHeapPriorityQueue(DoubleComparator comp)
Create with default sizing strategy and initial capacity for storing
Containers.DEFAULT_EXPECTED_ELEMENTS elements. |
DoubleIndexedHeapPriorityQueue(DoubleComparator comp,
int initialCapacity)
Create with a given initial capacity, using a
Comparator for ordering.
|
DoubleIndexedHeapPriorityQueue(int initialCapacity)
Create with an initial capacity,
using the natural ordering of
doubles |
DoubleIndexedHeapPriorityQueue(IntDoubleAssociativeContainer container)
Create a indexed heap from all key-value pairs of another container.
|
| Modifier and Type | Method and Description |
|---|---|
double |
addTo(int key,
double incrementValue)
Adds
incrementValue to any existing value for the given key
or inserts incrementValue if key did not previously exist. |
int |
capacity()
Return the maximum number of keys this container is guaranteed to hold without reallocating.
|
void |
clear()
Clear all keys and values in the container.
|
DoubleIndexedHeapPriorityQueue |
clone()
Clone this object.
|
DoubleComparator |
comparator()
Get the custom comparator used for comparing elements
|
boolean |
containsKey(int key)
Returns
true if this container has an association to a value for
the given key. |
protected void |
ensureBufferSpace(int index)
Ensures the internal buffer has enough free slots to accommodate the index
index. |
boolean |
equals(java.lang.Object obj)
this instance and obj can only be equal if either:
(both don't have set comparators)
or
(both have equal comparators defined by
comparator().equals(obj.comparator))
then the indexed heaps are considered equal as IntDoubleMap |
<T extends IntDoublePredicate> |
forEach(T predicate)
Applies a
predicate to container elements, as long as the predicate
returns true. |
<T extends IntDoubleProcedure> |
forEach(T procedure)
Applies a given procedure to all keys-value pairs in this container.
|
static DoubleIndexedHeapPriorityQueue |
from(int[] keys,
double[] values)
Creates a Indexed heap from two index-aligned arrays of key-value pairs.
|
static DoubleIndexedHeapPriorityQueue |
from(IntDoubleAssociativeContainer container)
Create a indexed heap from all key-value pairs of another container..
|
double |
get(int key)
cost: O(1)
|
double |
getDefaultValue()
Returns the "default value" value used
in methods returning "default value"
|
int |
hashCode() |
boolean |
isEmpty() |
DoubleIndexedHeapPriorityQueue.EntryIterator |
iterator()
Returns a cursor over the entries (key-value pairs) in this map.
|
DoubleIndexedHeapPriorityQueue.KeysCollection |
keys()
Returns a collection view of keys of this container.
|
double |
popTop()
Retrieve, and remove the top element of the queue,
i.e. the min/max element with respect to the comparison criteria
(implementation defined) Returns the default value if empty.
|
double |
put(int key,
double element)
Place a given key and value in the container.
|
int |
putAll(IntDoubleAssociativeContainer container)
Puts all keys from another container to this map, replacing the values
of existing keys, if such keys are present.
|
int |
putAll(java.lang.Iterable<? extends IntDoubleCursor> iterable)
Puts all keys from an iterable cursor to this map, replacing the values
of existing keys, if such keys are present.
|
boolean |
putIfAbsent(int key,
double value)
Trove-inspired API method.
|
double |
putOrAdd(int key,
double putValue,
double incrementValue)
If
key exists, putValue is inserted into the map,
otherwise any existing value is incremented by additionValue. |
double |
remove(int key)
Remove the (key, value) pair associated with the given key, and returns the assocaited value.
|
int |
removeAll(IntContainer container)
Removes all keys (and associated values) present in a given container.
|
int |
removeAll(IntDoublePredicate predicate)
Removes all keys (and associated values) for which the predicate on (key, value) pairs returns
true. |
int |
removeAll(IntPredicate predicate)
Removes all keys (and associated values) for which the predicate on keys returns
true. |
void |
setDefaultValue(double defaultValue)
Set the "default value" value to be used
in methods returning the "default value"
|
int |
size() |
double |
top()
Retrieve, but not remove, the top element of the queue,
i.e. the min. element with respect to the comparison criteria
of the queue.
|
int |
topKey()
Retrieve the key corresponding to the top element of the queue,
i.e. the min element with respect to the comparison criteria
of the queue.
|
java.lang.String |
toString() |
void |
updatePriorities()
Update priorities of all the elements of the queue, to re-establish the
correct priorities towards the comparison criteria. cost: O(n*log(N))
|
void |
updatePriority(int key)
Update the priority of the value associated with key, to re-establish the value correct priority
towards the comparison criteria.
|
void |
updateTopPriority()
Update the priority of the
top() element, to re-establish its actual priority
towards the comparison criteria when it may have changed such that it is no longer the
min element with respect to the comparison criteria. |
DoubleIndexedHeapPriorityQueue.ValuesCollection |
values()
Returns a collection view of all values present in this container.
|
public double[] buffer
Direct indexed priority queue iteration: iterate pq[i] for i in [0; pq.length[ and buffer[pq[i]] to get value where pq[i] > 0
public int[] pq
Direct iteration: iterate pq[i] for indices i in [0; pq.length[ where pq[i] > 0, then buffer[pq[i]] is the value associated with index i.
protected int[] qp
protected int elementsCount
protected DoubleComparator comparator
protected double defaultValue
protected final IteratorPool<IntDoubleCursor,DoubleIndexedHeapPriorityQueue.EntryIterator> entryIteratorPool
public DoubleIndexedHeapPriorityQueue(DoubleComparator comp, int initialCapacity)
public DoubleIndexedHeapPriorityQueue(DoubleComparator comp)
Containers.DEFAULT_EXPECTED_ELEMENTS elements.BoundedProportionalArraySizingStrategypublic DoubleIndexedHeapPriorityQueue()
Containers.DEFAULT_EXPECTED_ELEMENTS),
using the natural ordering of doubles.public DoubleIndexedHeapPriorityQueue(int initialCapacity)
doublespublic DoubleIndexedHeapPriorityQueue(IntDoubleAssociativeContainer container)
public static DoubleIndexedHeapPriorityQueue from(IntDoubleAssociativeContainer container)
public static DoubleIndexedHeapPriorityQueue from(int[] keys, double[] values)
public void clear()
clear in interface IntDoubleMappublic DoubleIndexedHeapPriorityQueue.EntryIterator iterator()
Iterator.next(). To read the current key and value use the cursor's
public fields. An example is shown below.
for (IntShortCursor c : intShortMap)
{
System.out.println("index=" + c.index
+ " key=" + c.key
+ " value=" + c.value);
}
The index field inside the cursor gives the internal index inside
the container's implementation. The interpretation of this index depends on
to the container.
iterator in interface IntDoubleAssociativeContaineriterator in interface java.lang.Iterable<IntDoubleCursor>public int size()
size in interface IntDoubleAssociativeContainerpublic int capacity()
O(n) time.capacity in interface IntDoubleAssociativeContainerpublic <T extends IntDoubleProcedure> T forEach(T procedure)
IntDoubleProcedure. This lets the caller to call methods of the argument
by chaining the call (even if the argument is an anonymous type) to retrieve computed values,
for example.forEach in interface IntDoubleAssociativeContainerpublic <T extends IntDoublePredicate> T forEach(T predicate)
predicate to container elements, as long as the predicate
returns true. The iteration is interrupted otherwise.
Returns the argument (any
subclass of IntDoublePredicate. This lets the caller to call methods of the argument
by chaining the call (even if the argument is an anonymous type) to retrieve computed values,
for example.
The iteration is continued as long as the predicate returns true.forEach in interface IntDoubleAssociativeContainerpublic boolean isEmpty()
isEmpty in interface IntDoubleAssociativeContainertrue if this hash map contains no assigned keys.public int removeAll(IntContainer container)
removeAll in interface IntDoubleAssociativeContainerpublic int removeAll(IntPredicate predicate)
true.removeAll in interface IntDoubleAssociativeContainerpublic int removeAll(IntDoublePredicate predicate)
true.removeAll in interface IntDoubleAssociativeContainerpublic boolean putIfAbsent(int key,
double value)
if (!map.containsKey(key))
map.put(key, value);
putIfAbsent in interface IntDoubleMapkey - The key of the value to check.value - The value to put if key does not exist.true if key did not exist and value
was placed in the map.public int putAll(IntDoubleAssociativeContainer container)
putAll in interface IntDoubleMappublic int putAll(java.lang.Iterable<? extends IntDoubleCursor> iterable)
putAll in interface IntDoubleMappublic double put(int key,
double element)
Important:
Whenever a new (key, value) pair is inserted, or
a value is updated with an already present key as specified by the IntDoubleMap.put(int, double)
contract, the inserted value priority is always consistent towards the comparison criteria.
In other words, there is no need to call updatePriority(int) after a put(int, double).
put in interface IntDoubleMapkey - the integer key, must be >= 0element - the associated valuepublic double putOrAdd(int key,
double putValue,
double incrementValue)
key exists, putValue is inserted into the map,
otherwise any existing value is incremented by additionValue.putOrAdd in interface IntDoubleMapkey - The key of the value to adjust.putValue - The value to put if key does not exist.incrementValue - The value to add to the existing value if key exists.key (after
changes).public double addTo(int key,
double incrementValue)
incrementValue to any existing value for the given key
or inserts incrementValue if key did not previously exist.addTo in interface IntDoubleMapkey - The key of the value to adjust.incrementValue - The value to put or add to the existing value if key exists.key (after changes).public double top()
public int topKey()
public double popTop()
public double get(int key)
get in interface IntDoubleMappublic double remove(int key)
remove in interface IntDoubleMappublic void updatePriority(int key)
public void updateTopPriority()
top() element, to re-establish its actual priority
towards the comparison criteria when it may have changed such that it is no longer the
min element with respect to the comparison criteria.
cost: O(log(N))public boolean containsKey(int key)
true if this container has an association to a value for
the given key.
cost: O(1)containsKey in interface IntDoubleAssociativeContainerpublic int hashCode()
hashCode in class java.lang.Objectpublic boolean equals(java.lang.Object obj)
(both don't have set comparators)
or
(both have equal comparators defined by comparator().equals(obj.comparator))
then the indexed heaps are considered equal as IntDoubleMapequals in class java.lang.Objectpublic DoubleIndexedHeapPriorityQueue clone()
clone in class java.lang.Objectpublic void updatePriorities()
public DoubleIndexedHeapPriorityQueue.KeysCollection keys()
IntDoubleAssociativeContainerkeys in interface IntDoubleAssociativeContainerpublic DoubleIndexedHeapPriorityQueue.ValuesCollection values()
IntDoubleAssociativeContainervalues in interface IntDoubleAssociativeContainerpublic java.lang.String toString()
toString in class java.lang.Objectpublic double getDefaultValue()
getDefaultValue in interface IntDoubleMappublic void setDefaultValue(double defaultValue)
setDefaultValue in interface IntDoubleMappublic DoubleComparator comparator()
doubles is used insteadprotected void ensureBufferSpace(int index)
index. Increases internal buffer size if needed.Copyright © 2017. All rights reserved.