curl2info/base.go
2018-11-22 23:05:42 +08:00

85 lines
1.7 KiB
Go

package curl2info
import (
"container/heap"
)
// Nodes for Heap, Container List
type Nodes []*ParseFunction
// ParseFunction 优先执行参数
type ParseFunction struct {
ExecuteFunction func(u *CURL, soption string)
ParamCURL *CURL
ParamData string
Prioty int
}
// Swap 实现sort.Iterface
func (nodes *Nodes) Swap(i, j int) {
ns := *nodes
ns[i], ns[j] = ns[j], ns[i]
}
// Less Priority Want Less
func (nodes *Nodes) Less(i, j int) bool {
ns := *nodes
return ns[i].Prioty < ns[j].Prioty
}
// Push 实现heap.Interface接口定义的额外方法
func (nodes *Nodes) Push(exec interface{}) {
*nodes = append(*nodes, exec.(*ParseFunction))
}
// Pop 堆顶
func (nodes *Nodes) Pop() (exec interface{}) {
nlen := nodes.Len()
exec = (*nodes)[nlen-1] // 返回删除的元素
*nodes = (*nodes)[:nlen-1] // [n:m]不包括下标为m的元素
return exec
}
// Len len(nodes)
func (nodes *Nodes) Len() int {
return len(*nodes)
}
// PQueueExecute 优先函数队列
type PQueueExecute struct {
nodes Nodes
}
// NewPQueueExecute Create A PQueueExecute
func NewPQueueExecute() *PQueueExecute {
pe := &PQueueExecute{}
pe.nodes = make(Nodes, 0)
heap.Init(&pe.nodes)
return pe
}
// Push Create A PQueueExecute
func (pqe *PQueueExecute) Push(exec *ParseFunction) {
heap.Push(&pqe.nodes, exec)
heap.Fix(&pqe.nodes, pqe.Len()-1)
}
// Pop Create A PQueueExecute
func (pqe *PQueueExecute) Pop() *ParseFunction {
return heap.Pop(&pqe.nodes).(*ParseFunction)
}
// Len Create A PQueueExecute
func (pqe *PQueueExecute) Len() int {
return pqe.nodes.Len()
}
// func (pqe *PQueueExecute) String() string {
// content := ""
// for _, node := range pqe.nodes {
// content += strconv.Itoa(node.Prioty)
// content += " "
// }
// return content
// }