curl2info/structure.go

255 lines
4.8 KiB
Go
Raw Normal View History

package curl2info
2018-11-29 08:22:27 +00:00
import "container/heap"
// TrieWord Trie 需要的Word接口
type TrieWord interface {
GetWord() string
}
// TrieStrWord 最简单的TrieWord 结构
type TrieStrWord string
// GetWord 获取单词
func (tsw *TrieStrWord) GetWord() string {
return (string)(*tsw)
}
// Trie 前缀树
type Trie struct {
isWord bool
value interface{}
char byte
prev *Trie
next map[byte]*Trie
}
// NewTrie Initialize your data structure here.
func NewTrie() *Trie {
return &Trie{next: make(map[byte]*Trie)}
}
// Insert a word into the trie.
func (trie *Trie) Insert(iword TrieWord) {
cur := trie
word := iword.GetWord()
l := len(word)
for i := 0; i < l; i++ {
c := word[i]
if next, ok := cur.next[c]; ok {
cur = next
} else {
create := NewTrie()
cur.next[c] = create
create.char = c
create.prev = cur
cur = create
}
}
cur.isWord = true
cur.value = iword
}
// AllWords 所有单词
func (trie *Trie) AllWords() []string {
var result []string
for _, v := range trie.next {
look(v, "", &result)
}
return result
}
func look(cur *Trie, content string, result *[]string) {
content += string(cur.char)
if cur.isWord {
*result = append(*result, content)
}
for _, v := range cur.next {
look(v, content, result)
}
}
// Remove 移除单词
func (trie *Trie) Remove(word string) {
cur := trie
l := len(word)
for i := 0; i < l; i++ {
c := word[i]
if next, ok := cur.next[c]; ok {
cur = next
} else {
return
}
}
if cur != nil {
cur.isWord = false
cur.value = nil
lastchar := cur.char
if len(cur.next) == 0 {
for cur.isWord != true && cur.prev != nil {
lastchar = cur.char
cur = cur.prev
if len(cur.next) > 1 {
return
}
}
delete(cur.next, lastchar)
}
}
}
// SearchMostPrefix Returns if the word is in the trie.
func (trie *Trie) SearchMostPrefix(iword TrieWord) interface{} {
cur := trie
word := iword.GetWord()
l := len(word)
var result interface{}
for i := 0; i < l; i++ {
c := word[i]
if next, ok := cur.next[c]; ok {
cur = next
if cur.isWord {
result = cur.value
}
} else {
return result
}
}
return result
}
// Match Returns if the word is in the trie.
func (trie *Trie) Match(iword TrieWord) interface{} {
cur := trie
word := iword.GetWord()
l := len(word)
for i := 0; i < l; i++ {
c := word[i]
if next, ok := cur.next[c]; ok {
cur = next
} else {
return nil
}
}
return cur.value
}
// StartsWith Returns if there is any word in the trie that starts with the given prefix. */
func (trie *Trie) StartsWith(prefix string) bool {
cur := trie
l := len(prefix)
for i := 0; i < l; i++ {
c := prefix[i]
if next, ok := cur.next[c]; ok {
cur = next
} else {
return false
}
}
return true
}
2018-11-29 08:22:27 +00:00
// 优先队列 所在的域
// 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
// }