2019-01-31 10:39:17 +00:00
|
|
|
package plist
|
|
|
|
|
|
|
|
import (
|
2019-02-08 22:32:58 +00:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2019-01-31 10:39:17 +00:00
|
|
|
"github.com/emirpasic/gods/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PriorityQueue 优先队列 适合数据量不大, 加索引
|
|
|
|
type PriorityQueue struct {
|
2019-02-08 22:32:58 +00:00
|
|
|
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{}
|
|
|
|
|
2019-02-10 17:27:47 +00:00
|
|
|
// prev *Node
|
2019-02-08 22:32:58 +00:00
|
|
|
next *Node
|
2019-01-31 10:39:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewWithInt compare use int
|
|
|
|
func NewWithInt() *PriorityQueue {
|
|
|
|
p := new(PriorityQueue)
|
2019-02-10 17:27:47 +00:00
|
|
|
p.indexlimit = 20
|
2019-01-31 10:39:17 +00:00
|
|
|
p.comparator = func(a, b interface{}) int {
|
|
|
|
if a.(int) > b.(int) {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
func (pq *PriorityQueue) String() string {
|
|
|
|
content := ""
|
|
|
|
for cur := pq.node; cur != nil; cur = cur.next {
|
2019-02-10 17:27:47 +00:00
|
|
|
// 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) + "-"
|
|
|
|
}
|
2019-01-31 19:57:40 +00:00
|
|
|
|
2019-02-10 17:27:47 +00:00
|
|
|
if content != "" {
|
|
|
|
if content[len(content)-1] == '-' {
|
|
|
|
content = content[:len(content)-1]
|
|
|
|
}
|
2019-01-31 19:57:40 +00:00
|
|
|
}
|
2019-02-10 17:27:47 +00:00
|
|
|
|
|
|
|
idxContent := ""
|
|
|
|
for idx := pq.index; idx != nil; idx = idx.next {
|
|
|
|
idxContent += spew.Sprint(idx.node.value) + "(" + spew.Sprint(idx.nlen) + ")-"
|
2019-02-08 22:32:58 +00:00
|
|
|
}
|
2019-02-10 17:27:47 +00:00
|
|
|
|
|
|
|
return content + "\n" + idxContent
|
2019-01-31 19:57:40 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 10:39:17 +00:00
|
|
|
func (pq *PriorityQueue) Push(v interface{}) {
|
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
node := new(Node)
|
|
|
|
node.value = v
|
2019-02-01 11:18:48 +00:00
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
if pq.node == nil {
|
|
|
|
//创建索引
|
|
|
|
index := new(Index)
|
|
|
|
index.nlen = 1
|
|
|
|
index.node = node
|
2019-02-01 11:18:48 +00:00
|
|
|
|
2019-02-10 17:27:47 +00:00
|
|
|
pq.index = index
|
|
|
|
pq.node = node
|
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// find the node of index to start
|
|
|
|
idx := pq.index
|
2019-01-31 10:39:17 +00:00
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
for {
|
2019-01-31 10:39:17 +00:00
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
if idx.next == nil {
|
|
|
|
break
|
|
|
|
}
|
2019-01-31 10:39:17 +00:00
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
if pq.comparator(v, idx.next.node.value) > 0 {
|
|
|
|
break
|
|
|
|
}
|
2019-01-31 10:39:17 +00:00
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
idx = idx.next
|
|
|
|
}
|
2019-01-31 10:39:17 +00:00
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
cur := idx.node
|
2019-01-31 10:39:17 +00:00
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
//cur := pq.node
|
|
|
|
if pq.comparator(v, pq.node.value) > 0 {
|
|
|
|
pq.node = node
|
|
|
|
node.next = cur
|
2019-02-01 11:18:48 +00:00
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
pq.index.node = pq.node
|
2019-02-10 17:27:47 +00:00
|
|
|
pq.index.nlen++
|
2019-01-31 10:39:17 +00:00
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
// cur.prev = node
|
|
|
|
return
|
|
|
|
}
|
2019-02-01 11:18:48 +00:00
|
|
|
|
2019-02-10 17:27:47 +00:00
|
|
|
for i := 0; cur.next != nil; i++ {
|
|
|
|
|
|
|
|
if i >= pq.indexlimit-1 {
|
|
|
|
if idx.nlen >= pq.indexlimit*2 {
|
|
|
|
index := new(Index)
|
|
|
|
index.node = node
|
|
|
|
index.nlen = idx.nlen - pq.indexlimit
|
|
|
|
index.next = idx.next
|
|
|
|
|
|
|
|
idx.next = index
|
|
|
|
idx.nlen = pq.indexlimit
|
|
|
|
idx = index
|
|
|
|
i = 0
|
|
|
|
}
|
|
|
|
}
|
2019-02-01 11:18:48 +00:00
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
if pq.comparator(v, cur.next.value) > 0 {
|
|
|
|
temp := cur.next
|
|
|
|
cur.next = node
|
|
|
|
node.next = temp
|
|
|
|
// node.prev = cur
|
|
|
|
// temp.prev = node
|
2019-02-01 11:18:48 +00:00
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
idx.nlen++
|
2019-02-01 11:18:48 +00:00
|
|
|
|
2019-02-10 17:27:47 +00:00
|
|
|
// if pq.index.nlen >= pq.indexlimit {
|
|
|
|
// // 分裂
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
return
|
2019-01-31 10:39:17 +00:00
|
|
|
}
|
2019-02-01 11:18:48 +00:00
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
cur = cur.next
|
2019-02-10 17:27:47 +00:00
|
|
|
|
2019-01-31 10:39:17 +00:00
|
|
|
}
|
2019-01-31 19:57:40 +00:00
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
cur.next = node
|
|
|
|
|
|
|
|
// node.prev = cur
|
2019-01-31 10:39:17 +00:00
|
|
|
pq.size++
|
2019-02-10 17:27:47 +00:00
|
|
|
idx.nlen++
|
2019-01-31 10:39:17 +00:00
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
}
|
2019-01-31 10:39:17 +00:00
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
// func (pq *PriorityQueue) Top() (interface{}, bool) {
|
|
|
|
// return pq.Get(0)
|
2019-01-31 19:57:40 +00:00
|
|
|
// }
|
2019-01-31 10:39:17 +00:00
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
// func (pq *PriorityQueue) Bottom() (interface{}, bool) {
|
|
|
|
// return pq.Get(pq.right - 1)
|
2019-01-31 19:57:40 +00:00
|
|
|
// }
|
2019-01-31 10:39:17 +00:00
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
// func (pq *PriorityQueue) Get(index int) (interface{}, bool) {
|
|
|
|
// if index < pq.size {
|
|
|
|
// return pq.Values()[index], true
|
|
|
|
// }
|
|
|
|
// return nil, false
|
|
|
|
// }
|
2019-01-31 10:39:17 +00:00
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
// 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]
|
|
|
|
// }
|