1 Star 0 Fork 0

lin2631/sc-go

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
timeline.go 1.76 KB
Copy Edit Raw Blame History
lin2631 authored 2022-10-29 20:33 . start write the main logic
package core
import (
"container/heap"
"fmt"
)
type Timeline interface {
IsEmpty() bool
Insert(e IEvent) error
OutFirst() (IEvent, error)
}
//Timeline是目前集群当前反映未来的事件时间线
//该时间线为因为新任务的提交,和机器更新,以及调度算法的改变而变化
type timeLine struct {
eventQueue eventHeap
}
//初始化一个Timeline,根据三种类型的事件
func InitTimeline(i []IEvent) Timeline {
var t timeLine
t.eventQueue = make(eventHeap, 0)
t.eventQueue = append(t.eventQueue, i...)
heap.Init(&t.eventQueue)
return &t
}
//从几条sortedList当中有着最先节点的list,然后将最先节点出列
//该最先节点就是下一个该执行的事件
func (t *timeLine) OutFirst() (IEvent, error) {
if t.IsEmpty() {
return nil, fmt.Errorf("there is no event in the queue")
}
return heap.Pop(&(t.eventQueue)).(IEvent), nil
}
func (t *timeLine) IsEmpty() bool {
return len(t.eventQueue) == 0
}
// 向时间线加入新的事件
func (t *timeLine) Insert(e IEvent) error {
heap.Push(&(t.eventQueue), e)
return nil
}
//----------------private----------------------------------------------
// use the container/heap to realize the event_queue,the top of the heap is the first event will happen next
type eventHeap []IEvent
func (h eventHeap) Len() int { return len(h) }
func (h eventHeap) Less(i, j int) bool { return h[i].HappenTime() < h[j].HappenTime() }
func (h eventHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *eventHeap) Push(x interface{}) {
// Push and Pop use pointer receivers because they modify the slice's length,
// not just its contents.
*h = append(*h, x.(IEvent))
}
func (h *eventHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lin2631/sc-go.git
git@gitee.com:lin2631/sc-go.git
lin2631
sc-go
sc-go
467849e1f467

Search