DeferredQueue
inserts the least SimEvent
to its end instead of its start
#474
-
Relevant CodeIn /*
* Adds an event in some position from the tail of the list.
* If the event time is smaller than the maxTime, traverses the list
* to find the place to insert the event.
* It uses a reverse iterator because usually in such cases,
* the time of the new event is close to the last events.
* Starting from the tail of the list will ensure the lowest number
* of iterations on the best cases.
* */
final var reverseEvtIterator = eventList.listIterator(eventList.size() - 1);
while (reverseEvtIterator.hasPrevious()) {
if (reverseEvtIterator.previous().getTime() <= eventTime) {
reverseEvtIterator.next();
reverseEvtIterator.add(newEvent);
addedToMiddle++;
return;
}
}
eventList.add(newEvent); // should be reverseEvtIterator.add(newEvent) Above code exists in this repo since 2019 (#206). If I'm not getting this wrong, then it should be obvious. Inserting |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hello @VVsxmja, sorry for the delay. Anyway, since it uses a List to store the events and almost all the time each new event will have a time higher than the previous event, those events are added to the end of the list with If a Stack was used, then each new event (usually with a time higher than the previous one) would be added to the top of the stack, so that we get a reverse order. If you have any further comments, please let me know. |
Beta Was this translation helpful? Give feedback.
Hello @VVsxmja, sorry for the delay.
This class DeferredQueue was inherited from CloudSim. I think that in issue #206 I renamed it to SortedQueue but probably decided to keep the old name in following commits.
Anyway, since it uses a List to store the events and almost all the time each new event will have a time higher than the previous event, those events are added to the end of the list with
eventList.add(newEvent)
. This way the order is expected to be[1.0, 2.0, 3.0, 4.0]
.If a Stack was used, then each new event (usually with a time higher than the previous one) would be added to the top of the stack, so that we get a reverse order.
If you have any further comments, please let me know.