-
Notifications
You must be signed in to change notification settings - Fork 132
/
events.go
53 lines (44 loc) · 1.15 KB
/
events.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package simulator
import "time"
// Event is a simulator-internal event.
type Event struct {
// Time at which the event was submitted.
time time.Time
// Each event is assigned a sequence number.
// Events with equal time are ordered by their sequence number.
sequenceNumber int
// Either armadaevents.EventSequence or scheduleEvent.
eventSequenceOrScheduleEvent any
// Maintained by the heap.Interface methods.
index int
}
// scheduleEvent is an event indicating the scheduler should be run.
type scheduleEvent struct{}
type EventLog []Event
func (el EventLog) Len() int { return len(el) }
func (el EventLog) Less(i, j int) bool {
if el[i].time == el[j].time {
return el[i].sequenceNumber < el[j].sequenceNumber
}
return el[j].time.After(el[i].time)
}
func (el EventLog) Swap(i, j int) {
el[i], el[j] = el[j], el[i]
el[i].index = i
el[j].index = j
}
func (el *EventLog) Push(x any) {
n := len(*el)
item := x.(Event)
item.index = n
*el = append(*el, item)
}
func (el *EventLog) Pop() any {
old := *el
n := len(old)
item := old[n-1]
old[n-1] = Event{} // avoid memory leak
item.index = -1 // for safety
*el = old[0 : n-1]
return item
}