@Generated(date="2017-07-11T19:16:22+0200",
value="KTypeHeapPriorityQueue.java")
public class ObjectHeapPriorityQueue<KType>
extends AbstractObjectCollection<KType>
implements ObjectPriorityQueue<KType>, java.lang.Cloneable
Objects.
i.e. top() is the smallest element,
as defined by Sedgewick: Algorithms 4th Edition (2011).
It assure O(log(N)) complexity for insertion, deletion and update priority of the min element,
and constant time to examine the min element by top().
Important:
Ordering of elements must be defined either
* by Comparable
* or by a custom comparator provided in constructors,
see comparator() .
| Modifier and Type | Class and Description |
|---|---|
class |
ObjectHeapPriorityQueue.ValueIterator
An iterator implementation for
iterator(). |
| Modifier and Type | Field and Description |
|---|---|
java.lang.Object[] |
buffer
Internal array for storing the priority queue.
|
protected java.util.Comparator<? super KType> |
comparator
Defines the Comparator ordering of the queue,
If null, natural ordering is used.
|
protected KType |
currentOccurenceToBeRemoved
The current value set for removeAll
|
protected KType |
defaultValue
Default value returned when specified
in methods.
|
protected int |
elementsCount
Number of elements in the queue.
|
protected ObjectPredicate<? super KType> |
removeAllOccurencesPredicate
Internal predicate for removeAll
|
protected ArraySizingStrategy |
resizer
Buffer resizing strategy.
|
protected IteratorPool<ObjectCursor<KType>,ObjectHeapPriorityQueue.ValueIterator> |
valueIteratorPool
internal pool of ValueIterator (must be created in constructor)
|
containsNegateTestPredicate, containsTestPredicate, negatePredicate, testContainer, testPredicate| Constructor and Description |
|---|
ObjectHeapPriorityQueue()
Default constructor: create with a default
numbers of elements (
Containers.DEFAULT_EXPECTED_ELEMENTS),
using the Comparable natural ordering. |
ObjectHeapPriorityQueue(java.util.Comparator<? super KType> comp)
Create with default sizing strategy and initial capacity
(
Containers.DEFAULT_EXPECTED_ELEMENTS)
using a specific Comparator. |
ObjectHeapPriorityQueue(java.util.Comparator<? super KType> comp,
int initialCapacity)
Create with a given initial capacity, using a
Comparator for ordering.
|
ObjectHeapPriorityQueue(java.util.Comparator<? super KType> comp,
int initialCapacity,
ArraySizingStrategy resizer)
Create with a Comparator, an initial capacity, and a custom buffer resizing strategy.
|
ObjectHeapPriorityQueue(int initialCapacity)
Create with an initial capacity,
using the Comparable natural ordering
|
ObjectHeapPriorityQueue(ObjectContainer<? extends KType> container)
Creates a new heap from elements of another container.
|
| Modifier and Type | Method and Description |
|---|---|
void |
add(KType element)
Insert a Object into the queue.
|
int |
addAll(java.lang.Iterable<? extends ObjectCursor<? extends KType>> iterable)
Adds all elements from another iterable.
|
int |
addAll(ObjectContainer<? extends KType> container)
Adds all elements from another container.
|
int |
capacity()
Return the maximum number of elements this container is guaranteed to hold without reallocating.
|
void |
clear()
Removes all elements from this collection.
|
ObjectHeapPriorityQueue<KType> |
clone()
Clone this object.
|
java.util.Comparator<? super KType> |
comparator()
Get the custom comparator used for comparing elements
|
boolean |
contains(KType element)
Lookup a given element in the container.
|
protected void |
ensureBufferSpace(int expectedAdditions)
Ensures the internal buffer has enough free slots to store
expectedAdditions. |
boolean |
equals(java.lang.Object obj)
this instance and obj can only be equal to this if either:
(both don't have set comparators)
or
(both have equal comparators defined by
comparator().equals(obj.comparator))
then, both heap elements are compared with equals(Object) iterating their buffer. |
<T extends ObjectPredicate<? super KType>> |
forEach(T predicate)
Applies a
predicate to container elements, as long as the predicate
returns true. |
<T extends ObjectProcedure<? super KType>> |
forEach(T procedure)
Applies a
procedure to all container elements. |
static <KType> ObjectHeapPriorityQueue<KType> |
from(KType... elements)
Create a heap from a variable number of arguments or an array of
Object. |
static <KType> ObjectHeapPriorityQueue<KType> |
from(ObjectContainer<KType> container)
Create a heap from elements of another container (constructor shortcut)
|
KType |
getDefaultValue()
Returns the "default value" value used
in methods returning "default value"
|
int |
hashCode() |
ObjectHeapPriorityQueue.ValueIterator |
iterator()
Returns an iterator to a cursor traversing the collection.
|
KType |
popTop()
Retrieve, and remove the top element of the queue,
i.e. the min element with respect to the comparison criteria
(implementation defined) Returns the default value if empty.
|
int |
removeAll(KType e1)
Removes all occurrences of
e from this collection. |
int |
removeAll(ObjectPredicate<? super KType> predicate)
Removes all elements in this collection for which the
given predicate returns
true. |
void |
setDefaultValue(KType defaultValue)
Set the "default value" value to be used
in methods returning "default value"
|
int |
size()
Return the current number of elements in this container.
|
KType[] |
toArray(KType[] target)
Default implementation for:
Copies all elements of this container to an existing array of the same type.
|
KType |
top()
Retrieve, but not remove, the top element of the queue,
i.e. the min element with respect to the comparison criteria
(implementation defined)
of the queue.
|
void |
updatePriorities()
Update priorities of all the elements of the queue, to re-establish the correct priorities
towards the comparison criteria.
|
void |
updateTopPriority()
Update the priority of the
ObjectPriorityQueue.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. |
isEmpty, removeAll, retainAll, retainAll, toArray, toArray, toStringfinalize, getClass, notify, notifyAll, wait, wait, waitremoveAll, retainAll, retainAllisEmpty, toArray, toArraypublic java.lang.Object[] buffer
Direct priority queue iteration: iterate buffer[i] for i in [1; size()] (included) but is out-of-order w.r.t popTop()
protected int elementsCount
protected java.util.Comparator<? super KType> comparator
protected final ArraySizingStrategy resizer
protected final IteratorPool<ObjectCursor<KType>,ObjectHeapPriorityQueue.ValueIterator> valueIteratorPool
protected KType currentOccurenceToBeRemoved
protected ObjectPredicate<? super KType> removeAllOccurencesPredicate
public ObjectHeapPriorityQueue(java.util.Comparator<? super KType> comp, int initialCapacity, ArraySizingStrategy resizer)
public ObjectHeapPriorityQueue(java.util.Comparator<? super KType> comp)
Containers.DEFAULT_EXPECTED_ELEMENTS)
using a specific Comparator.BoundedProportionalArraySizingStrategypublic ObjectHeapPriorityQueue()
Containers.DEFAULT_EXPECTED_ELEMENTS),
using the Comparable natural ordering.public ObjectHeapPriorityQueue(int initialCapacity)
public ObjectHeapPriorityQueue(java.util.Comparator<? super KType> comp, int initialCapacity)
BoundedProportionalArraySizingStrategypublic ObjectHeapPriorityQueue(ObjectContainer<? extends KType> container)
public static <KType> ObjectHeapPriorityQueue<KType> from(ObjectContainer<KType> container)
public static <KType> ObjectHeapPriorityQueue<KType> from(KType... elements)
Object.public int removeAll(KType e1)
e from this collection.removeAll in interface ObjectCollection<KType>e1 - Element to be removed from this collection, if present.public int removeAll(ObjectPredicate<? super KType> predicate)
true.removeAll in interface ObjectCollection<KType>public void clear()
clear in interface ObjectCollection<KType>public ObjectHeapPriorityQueue.ValueIterator iterator()
The iterator is implemented as a
cursor and it returns the same cursor instance on every call to
Iterator.next() (to avoid boxing of primitive types). To read the current
list's value (or index in the list) use the cursor's public fields. An example is
shown below.
for (ObjectCursor<Object> c : container) {
System.out.println("index=" + c.index + " value=" + c.value);
}
iterator in interface ObjectContainer<KType>iterator in interface java.lang.Iterable<ObjectCursor<KType>>public boolean contains(KType element)
contains in interface ObjectContainer<KType>true if this container has an element
equal to e.public int size()
O(n) time, although implementing classes
should try to maintain the current size and return in constant time.size in interface ObjectContainer<KType>public int capacity()
O(n) time.capacity in interface ObjectContainer<KType>public <T extends ObjectProcedure<? super KType>> T forEach(T procedure)
procedure to all container elements. Returns the argument (any
subclass of ObjectProcedure. 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 (IntContainer):
int count = container.forEach(new IntProcedure() {
int count; // this is a field declaration in an anonymous class.
public void apply(int value) { count++; }}).count;
forEach in interface ObjectContainer<KType>public <T extends ObjectPredicate<? super KType>> T forEach(T predicate)
predicate to container elements, as long as the predicate
returns true. The iteration is interrupted otherwise.forEach in interface ObjectContainer<KType>public void add(KType element)
add in interface ObjectPriorityQueue<KType>public KType top()
top in interface ObjectPriorityQueue<KType>public KType popTop()
popTop in interface ObjectPriorityQueue<KType>public int addAll(ObjectContainer<? extends KType> container)
public int addAll(java.lang.Iterable<? extends ObjectCursor<? extends KType>> iterable)
public int hashCode()
hashCode in class java.lang.Objectpublic void updatePriorities()
updatePriorities in interface ObjectPriorityQueue<KType>public void updateTopPriority()
ObjectPriorityQueue.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))updateTopPriority in interface ObjectPriorityQueue<KType>public ObjectHeapPriorityQueue<KType> clone()
clone 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, both heap elements are compared with equals(Object) iterating their buffer.equals in class java.lang.Objectprotected void ensureBufferSpace(int expectedAdditions)
expectedAdditions. Increases internal buffer size if needed.public KType[] toArray(KType[] target)
toArray in interface ObjectContainer<KType>toArray in class AbstractObjectCollection<KType>target - The target array must be large enough to hold all elements, i.e >= ObjectContainer.size().public java.util.Comparator<? super KType> comparator()
Objects is used instead
, which means objects in this case must be Comparable.public KType getDefaultValue()
getDefaultValue in interface ObjectPriorityQueue<KType>public void setDefaultValue(KType defaultValue)
setDefaultValue in interface ObjectPriorityQueue<KType>Copyright © 2017. All rights reserved.