move pQueueExecute to structure

This commit is contained in:
huangsimin 2018-11-29 16:22:27 +08:00
parent 420f307ad2
commit 10f43ba51a
2 changed files with 97 additions and 87 deletions

87
base.go
View File

@ -1,88 +1 @@
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
Priority 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].Priority < ns[j].Priority
}
// 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
// }

View File

@ -1,5 +1,7 @@
package curl2info
import "container/heap"
// TrieWord Trie 需要的Word接口
type TrieWord interface {
GetWord() string
@ -155,3 +157,98 @@ func (trie *Trie) StartsWith(prefix string) bool {
}
return true
}
// 优先队列 所在的域
// parseQueue for Heap, Container List
type parseQueue []*parseFunction
// parseFunction 优先执行参数
type parseFunction struct {
ExecuteFunction func(u *CURL, soption string)
ParamCURL *CURL
ParamData string
Priority 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].Priority < ns[j].Priority
}
// Push 实现heap.Interface接口定义的额外方法
func (nodes *parseQueue) Push(exec// func (pqe *pQueueExecute) String() string {
// content := ""
// for _, node := range pqe.nodes {
// content += strconv.Itoa(node.Prioty)
// content += " "
// }
// return content
// }
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
// }