Compare commits

...

9 Commits

Author SHA1 Message Date
huangsimin
0403bebea4 TODO: Index 有错误 2019-02-13 10:46:30 +08:00
841c58b4e8 TODO: 修正 index 2019-02-11 01:27:47 +08:00
3ff7e7e855 还要完成index 2019-02-09 06:32:58 +08:00
huangsimin
36d7f0208b TODO: 2019-02-01 19:18:48 +08:00
6e253bd2d3 TODO: PQ finish task 2019-02-01 03:57:40 +08:00
huangsimin
b2d62bb55a 完善priority_queue中, TODO: 继续 2019-01-31 18:39:17 +08:00
huangsimin
055113962d TODO: 设计出几种不同优先队列的架构. 2019-01-30 18:37:12 +08:00
huangsimin
ad18004bc2 TODO: level find 2019-01-27 18:42:08 +08:00
huangsimin
741a685e16 TODO: Finish priority_list 2019-01-26 18:45:30 +08:00
4 changed files with 333 additions and 1 deletions

View File

@ -1,6 +1,8 @@
package plist
import "testing"
import (
"testing"
)
func TestPriority(t *testing.T) {
pl := New()

View File

@ -0,0 +1,190 @@
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]
// }

View File

@ -0,0 +1,107 @@
package plist
import (
"log"
"testing"
"github.com/emirpasic/gods/utils"
"github.com/Pallinder/go-randomdata"
"github.com/emirpasic/gods/trees/binaryheap"
)
type PriorityQ struct {
heap *binaryheap.Heap
comparator utils.Comparator
topk int
next *PriorityQ
}
func (pq *PriorityQ) Push(v interface{}) {
}
func TestNPQ(t *testing.T) {
h1 := binaryheap.NewWithIntComparator()
for i := 0; i < 10; i++ {
h1.Push(i)
}
h1.Values()[0] = 3
log.Println(h1)
}
func TestPriorityQueue(t *testing.T) {
p := NewWithInt()
for i := 0; i < 100; i++ {
p.Push(randomdata.Number(0, 10000))
t.Log(p.String())
}
t.Error(p.String())
}
func BenchmarkPriorityQueue(b *testing.B) {
p := NewWithInt()
// for i := 0; i < 10000; i++ {
// p.Push(randomdata.Number(0, 100000))
// // p.Values()
// }
b.N = 100000
for i := 0; i < b.N; i++ {
p.Push(randomdata.Number(0, 100000))
}
}
func TestHeap(t *testing.T) {
heap := binaryheap.NewWithIntComparator()
for i := 0; i < 10; i++ {
heap.Push(randomdata.Number(0, 1000))
}
t.Error(heap.Peek())
t.Error(heap.Values())
utils.Sort(heap.Values(), utils.IntComparator)
}
func BenchmarkList_InsertValues123(b *testing.B) {
a := func(v1, v2 interface{}) int {
if v1.(int) > v2.(int) {
return -1
}
return 1
}
h := binaryheap.NewWith(a)
TOPK := 50
for i := 0; i < TOPK*1000; i++ {
h.Push(i)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
h.Push(i)
l := []interface{}{}
for n := 0; n < TOPK; n++ {
v, _ := h.Pop()
l = append(l, v)
}
for _, v := range l {
h.Push(v)
}
}
b.StopTimer()
}

View File

@ -1,3 +1,36 @@
package structure
// NValue Node节点的必备的结构
type NValue struct {
IValue interface{}
}
// ValueInt 返回 Int
func (node *NValue) ValueInt() int {
return node.IValue.(int)
}
// Value 返回 Value interface
func (node *NValue) Value() interface{} {
return node.IValue
}
// ValueInt8 返回 Int
func (node *NValue) ValueInt8() int8 {
return node.IValue.(int8)
}
// ValueInt16 返回 Int
func (node *NValue) ValueInt16() int16 {
return node.IValue.(int16)
}
// ValueInt32 返回 Int32
func (node *NValue) ValueInt32() int32 {
return node.IValue.(int32)
}
// ValueInt64 返回 Int64
func (node *NValue) ValueInt64() int64 {
return node.IValue.(int64)
}