Class SortedEventQueue<E>
- Type Parameters:
E
- event type
- All Implemented Interfaces:
EventQueue<E>
EventQueue
interface.
A descending sorted list is used to hold the data, internally stored
in an ArrayList
. Look-up operations are done by binary search,
leading to a complexity of O(log n). enqueue()
and dequeue(E)
operations also need to move all elements with a lesser time stamp (max.
O(n)). dequeue()
, getMin()
and dequeueAll()
are done
in O(1), since the elements with the least time stamp are always placed at
the end of the list (descending sorting order).
Note: The performance of this queue type declines with the number of entries. Thus, this implementation seems better suited for local event queues, which often are memory-critical and generally hold fewer events.
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptiondequeue()
Dequeues the event with the smallest time stamp.Removes the entry of the given event.Dequeues all elements with the smallest time stamp.dequeueAll
(Time time) Dequeues all elements with the given time stamp.void
Enqueues an event at the given time.getMin()
Gets the minimal time stamp.Gets the time of the given event but does not dequeue it.boolean
isEmpty()
Checks if the queue is empty.int
size()
Returns the number of elements in the queue.toString()
-
Constructor Details
-
SortedEventQueue
public SortedEventQueue()
-
-
Method Details
-
getMin
Description copied from interface:EventQueue
Gets the minimal time stamp.An empty event queue may result in non-deterministic behavior and is not checked!
- Returns:
- current minimal time stamp (does not test if queue is empty!)
-
dequeue
Description copied from interface:EventQueue
Dequeues the event with the smallest time stamp.If there are several events with the same time stamp, the result can be any of these events
- Returns:
- event with the smallest time stamp or null if the queue is empty
- See Also:
-
enqueue
Description copied from interface:EventQueue
Enqueues an event at the given time.Note that only distinct events may be added to the queue. If the very same event already exists, it may be overwritten.
- Specified by:
enqueue
in interfaceEventQueue<E>
- Parameters:
event
- the event to be added to the queuetime
- the time stamp of the event, must be a future time
-
dequeueAll
Description copied from interface:EventQueue
Dequeues all elements with the given time stamp.An empty event queue may result in non-deterministic behavior and is not checked!
- Specified by:
dequeueAll
in interfaceEventQueue<E>
- Parameters:
time
- the time stamp of the events to dequeue- Returns:
- a list containing all events with this time stamp or an empty list if there are not events at the given time
-
dequeueAll
Description copied from interface:EventQueue
Dequeues all elements with the smallest time stamp. A call to this method is equivalent todequeueAll(getMin())
.An empty event queue may result in non-deterministic behavior and is not checked!
- Specified by:
dequeueAll
in interfaceEventQueue<E>
- Returns:
- a list containing all events with the minimum time stamp or an empty list if the event queue is empty.
- See Also:
-
getTime
Description copied from interface:EventQueue
Gets the time of the given event but does not dequeue it.- Specified by:
getTime
in interfaceEventQueue<E>
- Parameters:
event
- the event to retrieve the time for- Returns:
- time stamp of the event or null if the event does not exist
-
isEmpty
public boolean isEmpty()Description copied from interface:EventQueue
Checks if the queue is empty.- Specified by:
isEmpty
in interfaceEventQueue<E>
- Returns:
- true if the queue is empty, false otherwise
-
size
public int size()Description copied from interface:EventQueue
Returns the number of elements in the queue.- Specified by:
size
in interfaceEventQueue<E>
- Returns:
- number of queue entries
-
dequeue
Description copied from interface:EventQueue
Removes the entry of the given event.- Specified by:
dequeue
in interfaceEventQueue<E>
- Parameters:
event
- the event to be removed from the queue- Returns:
- time stamp of the dequeued event or null if the event was not part of the queue
-
toString
-