package it.univr.di.cstnu.util;
import it.univr.di.cstnu.graph.LabeledNode;
import it.univr.di.labeledvalue.Constants;
import javax.annotation.Nonnull;
/**
* Active wait for a timepoint.
*
* The scope of this class is to maintain the list of active waits for a node, allowing a fast determination of the greatest wait and fast removal of useless waits.
*
* For these reasons, a max priority queue with the key the wait value and value of the associated contingent node is used.
*
* Since the library has a minimum priority queue implementation, I reuse it, making it private and providing all the proper methods for manipulating it as a maximum priority queue.
*
* Assumption
* The wait values are positive integers because it is supposed to manage waits at runtime.
*/
public class ActiveWaits {
/**
* (delay-->contingent node)
*/
private final ExtendedPriorityQueue wait = new ExtendedPriorityQueue<>(true);
/**
* Add an active wait represented as (Contingent, positive value) to the queue.
*
* @param contingent the contingent node.
* @param value the positive value representing the wait.
*/
public void addWait(LabeledNode contingent, int value) {
if (value < 0) {
throw new IllegalArgumentException("Only positive values are admitted.");
}
wait.insertOrUpdate(contingent, value);
}
/**
* @return the maximum wait value presented in the queue if the queue is not empty, {@link Constants#INT_NULL}
* otherwise.
*/
public int getMaximum() {
if (wait.isEmpty()) {
return Constants.INT_NULL;
}
assert wait.getFirstEntry() != null;
return wait.getFirstEntry().getKey();
}
/**
* Remove the wait associated with the contingent from the queue.
*
* @param contingent node to remove
*
* @return true if the wait associated with the contingent node was removed.
*/
public boolean remove(@Nonnull LabeledNode contingent) {
if (wait.getStatus(contingent) != ExtendedPriorityQueue.Status.isPresent) {
return false;
}
wait.delete(contingent);
return true;
}
/**
* @return the number of active waits
*/
public int size() {
return (int) this.wait.size();
}
/**
* @return the string representation. Beware that values are negated in this representation.
*/
public String toString() {
return wait.toString();
}
}