191 lines
3.2 KiB
Go
191 lines
3.2 KiB
Go
package plist
|
|
|
|
import (
|
|
"github.com/davecgh/go-spew/spew"
|
|
"github.com/emirpasic/gods/utils"
|
|
)
|
|
|
|
// PriorityQueue 优先队列 适合数据量不大, 加索引
|
|
type PriorityQueue struct {
|
|
index *Index
|
|
indexlimit int
|
|
node *Node
|
|
size int
|
|
comparator utils.Comparator
|
|
}
|
|
|
|
type Index struct {
|
|
node *Node
|
|
next *Index
|
|
nlen int
|
|
}
|
|
|
|
type Node struct {
|
|
value interface{}
|
|
|
|
// prev *Node
|
|
next *Node
|
|
}
|
|
|
|
// NewWithInt compare use int
|
|
func NewWithInt() *PriorityQueue {
|
|
p := new(PriorityQueue)
|
|
p.indexlimit = 10
|
|
p.comparator = func(a, b interface{}) int {
|
|
if a.(int) > b.(int) {
|
|
return 1
|
|
}
|
|
return -1
|
|
}
|
|
return p
|
|
}
|
|
|
|
func (pq *PriorityQueue) String() string {
|
|
content := ""
|
|
for cur := pq.node; cur != nil; cur = cur.next {
|
|
// var prevcontent string
|
|
// if cur.prev != nil {
|
|
// prevcontent = "(" + spew.Sprint(cur.prev.value) + "<-)"
|
|
// } else {
|
|
// prevcontent = "(nil)"
|
|
// }
|
|
|
|
// content += spew.Sprint(cur.value) + prevcontent + "-"
|
|
content += spew.Sprint(cur.value) + "-"
|
|
}
|
|
|
|
if content != "" {
|
|
if content[len(content)-1] == '-' {
|
|
content = content[:len(content)-1]
|
|
}
|
|
}
|
|
|
|
idxContent := ""
|
|
for idx := pq.index; idx != nil; idx = idx.next {
|
|
idxContent += spew.Sprint(idx.node.value) + "(" + spew.Sprint(idx.nlen) + ")-"
|
|
}
|
|
|
|
return content + "\n" + idxContent
|
|
}
|
|
|
|
func (pq *PriorityQueue) Push(v interface{}) {
|
|
|
|
node := new(Node)
|
|
node.value = v
|
|
|
|
if pq.node == nil {
|
|
//创建索引
|
|
index := new(Index)
|
|
index.nlen = 1
|
|
index.node = node
|
|
|
|
pq.index = index
|
|
pq.node = node
|
|
|
|
return
|
|
}
|
|
// find the node of index to start
|
|
idx := pq.index
|
|
|
|
for {
|
|
|
|
if idx.next == nil {
|
|
break
|
|
}
|
|
|
|
if pq.comparator(v, idx.next.node.value) > 0 {
|
|
break
|
|
}
|
|
|
|
idx = idx.next
|
|
}
|
|
|
|
cur := idx.node
|
|
|
|
//cur := pq.node
|
|
if pq.comparator(v, pq.node.value) > 0 {
|
|
pq.node = node
|
|
node.next = cur
|
|
|
|
pq.index.node = pq.node
|
|
pq.index.nlen++
|
|
|
|
// cur.prev = node
|
|
return
|
|
}
|
|
|
|
for i := 0; cur.next != nil; i++ {
|
|
|
|
if i >= pq.indexlimit {
|
|
|
|
if idx.next != nil && idx.next.nlen < pq.indexlimit {
|
|
idx.next.nlen += idx.nlen - pq.indexlimit
|
|
idx.nlen = pq.indexlimit
|
|
idx.next.node = cur
|
|
} else {
|
|
index := new(Index)
|
|
index.node = cur
|
|
index.nlen = idx.nlen - pq.indexlimit
|
|
index.next = idx.next
|
|
|
|
idx.next = index
|
|
idx.nlen = pq.indexlimit
|
|
idx = index
|
|
i = 0
|
|
}
|
|
|
|
}
|
|
|
|
if pq.comparator(v, cur.next.value) > 0 {
|
|
temp := cur.next
|
|
cur.next = node
|
|
node.next = temp
|
|
// node.prev = cur
|
|
// temp.prev = node
|
|
|
|
idx.nlen++
|
|
|
|
// if pq.index.nlen >= pq.indexlimit {
|
|
// // 分裂
|
|
|
|
// }
|
|
|
|
return
|
|
}
|
|
|
|
cur = cur.next
|
|
|
|
}
|
|
|
|
cur.next = node
|
|
|
|
// node.prev = cur
|
|
pq.size++
|
|
idx.nlen++
|
|
|
|
}
|
|
|
|
// func (pq *PriorityQueue) Top() (interface{}, bool) {
|
|
// return pq.Get(0)
|
|
// }
|
|
|
|
// func (pq *PriorityQueue) Bottom() (interface{}, bool) {
|
|
// return pq.Get(pq.right - 1)
|
|
// }
|
|
|
|
// func (pq *PriorityQueue) Get(index int) (interface{}, bool) {
|
|
// if index < pq.size {
|
|
// return pq.Values()[index], true
|
|
// }
|
|
// return nil, false
|
|
// }
|
|
|
|
// func (pq *PriorityQueue) Values() []interface{} {
|
|
// // values := pq.datas[pq.left:pq.right]
|
|
// // if !pq.isSorted {
|
|
// // utils.Sort(values, pq.comparator)
|
|
// // pq.isSorted = true
|
|
// // }
|
|
// return pq.datas[pq.left:pq.right]
|
|
// }
|