curl2info/base.go
2018-11-23 15:30:52 +08:00

89 lines
1.9 KiB
Go

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