2019-01-26 10:45:30 +00:00
|
|
|
package plist
|
|
|
|
|
2019-01-30 10:37:12 +00:00
|
|
|
import (
|
2019-02-08 22:32:58 +00:00
|
|
|
"log"
|
2019-01-30 10:37:12 +00:00
|
|
|
"testing"
|
2019-01-26 10:45:30 +00:00
|
|
|
|
2019-01-31 10:39:17 +00:00
|
|
|
"github.com/emirpasic/gods/utils"
|
|
|
|
|
2019-01-30 10:37:12 +00:00
|
|
|
"github.com/Pallinder/go-randomdata"
|
|
|
|
|
|
|
|
"github.com/emirpasic/gods/trees/binaryheap"
|
|
|
|
)
|
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
type PriorityQ struct {
|
|
|
|
heap *binaryheap.Heap
|
|
|
|
comparator utils.Comparator
|
|
|
|
topk int
|
|
|
|
next *PriorityQ
|
|
|
|
}
|
2019-01-30 10:37:12 +00:00
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
func (pq *PriorityQ) Push(v interface{}) {
|
|
|
|
}
|
2019-01-30 10:37:12 +00:00
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
func TestNPQ(t *testing.T) {
|
|
|
|
h1 := binaryheap.NewWithIntComparator()
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
h1.Push(i)
|
2019-02-01 11:18:48 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
h1.Values()[0] = 3
|
2019-02-01 11:18:48 +00:00
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
log.Println(h1)
|
|
|
|
|
|
|
|
}
|
2019-02-01 11:18:48 +00:00
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
func TestPriorityQueue(t *testing.T) {
|
|
|
|
p := NewWithInt()
|
2019-01-26 10:45:30 +00:00
|
|
|
|
2019-02-10 17:27:47 +00:00
|
|
|
for i := 0; i < 20; i++ {
|
2019-02-08 22:32:58 +00:00
|
|
|
p.Push(randomdata.Number(0, 10000))
|
2019-02-10 17:27:47 +00:00
|
|
|
t.Log(p.String())
|
2019-01-31 10:39:17 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 22:32:58 +00:00
|
|
|
t.Error(p.String())
|
2019-01-31 10:39:17 +00:00
|
|
|
}
|
2019-01-30 10:37:12 +00:00
|
|
|
|
2019-02-01 11:18:48 +00:00
|
|
|
func BenchmarkPriorityQueue(b *testing.B) {
|
2019-01-30 10:37:12 +00:00
|
|
|
p := NewWithInt()
|
|
|
|
|
2019-02-01 11:18:48 +00:00
|
|
|
// for i := 0; i < 100000; i++ {
|
|
|
|
// p.Push(randomdata.Number(0, 100000))
|
|
|
|
// // p.Values()
|
|
|
|
// }
|
|
|
|
|
2019-01-30 10:37:12 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2019-01-31 10:39:17 +00:00
|
|
|
p.Push(randomdata.Number(0, 100000))
|
2019-01-30 10:37:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-01-26 10:45:30 +00:00
|
|
|
|
2019-01-31 10:39:17 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-01-30 10:37:12 +00:00
|
|
|
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()
|
2019-01-26 10:45:30 +00:00
|
|
|
}
|