Compare commits
11 Commits
v1.0.0
...
array_with
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b71ba9ebdd | ||
|
|
05bcac44ef | ||
|
|
0403bebea4 | ||
| 841c58b4e8 | |||
| 3ff7e7e855 | |||
|
|
36d7f0208b | ||
| 6e253bd2d3 | |||
|
|
b2d62bb55a | ||
|
|
055113962d | ||
|
|
ad18004bc2 | ||
|
|
741a685e16 |
@@ -1,6 +1,8 @@
|
||||
package plist
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPriority(t *testing.T) {
|
||||
pl := New()
|
||||
|
||||
114
priority_queue/priority_queue.go
Normal file
114
priority_queue/priority_queue.go
Normal file
@@ -0,0 +1,114 @@
|
||||
package plist
|
||||
|
||||
import (
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/emirpasic/gods/utils"
|
||||
)
|
||||
|
||||
// PriorityQueue 优先队列 适合数据量不大, 加索引
|
||||
type PriorityQueue struct {
|
||||
data []interface{}
|
||||
left, right, mid int
|
||||
cap int
|
||||
size int
|
||||
isSorted bool
|
||||
comparator utils.Comparator
|
||||
}
|
||||
|
||||
// NewWithInt compare use int
|
||||
func NewWithInt() *PriorityQueue {
|
||||
p := new(PriorityQueue)
|
||||
p.cap = 11
|
||||
p.mid = 5
|
||||
p.data = make([]interface{}, p.cap, p.cap)
|
||||
|
||||
p.comparator = func(a, b interface{}) int {
|
||||
if a.(int) > b.(int) {
|
||||
return 1
|
||||
}
|
||||
return -1
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func (pq *PriorityQueue) String() string {
|
||||
|
||||
return "(" + spew.Sprint(pq.data[pq.mid]) + "," + spew.Sprint(pq.size) + ")" + spew.Sprint(pq.Values())
|
||||
}
|
||||
|
||||
func (pq *PriorityQueue) reMakeData() {
|
||||
var temp []interface{}
|
||||
|
||||
if pq.cap <= 1024 {
|
||||
pq.cap = pq.cap*2 + 1
|
||||
temp = make([]interface{}, pq.cap, pq.cap)
|
||||
} else {
|
||||
pq.cap = pq.cap*5/8*2 + 1
|
||||
temp = make([]interface{}, pq.cap, pq.cap)
|
||||
}
|
||||
|
||||
pq.mid = (pq.cap - 1) / 2
|
||||
left := pq.mid - pq.size/2
|
||||
|
||||
copy(temp[left:], pq.data[pq.left:pq.right])
|
||||
|
||||
pq.left = left
|
||||
pq.right = pq.left + pq.size
|
||||
|
||||
pq.data = temp
|
||||
}
|
||||
func (pq *PriorityQueue) Push(v interface{}) {
|
||||
|
||||
if pq.size == 0 {
|
||||
pq.data[pq.mid] = v
|
||||
pq.left = pq.mid
|
||||
pq.right = pq.mid + 1
|
||||
pq.size = 1
|
||||
return
|
||||
}
|
||||
|
||||
midvalue := pq.data[pq.mid]
|
||||
if pq.comparator(v, midvalue) > 0 {
|
||||
if pq.left == 0 {
|
||||
pq.reMakeData()
|
||||
}
|
||||
|
||||
pq.left--
|
||||
pq.data[pq.left] = v
|
||||
} else {
|
||||
|
||||
if pq.right == pq.cap {
|
||||
pq.reMakeData()
|
||||
}
|
||||
|
||||
pq.data[pq.right] = v
|
||||
pq.right++
|
||||
}
|
||||
|
||||
pq.isSorted = false
|
||||
pq.size++
|
||||
}
|
||||
|
||||
// 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.data[pq.left:pq.right]
|
||||
if !pq.isSorted {
|
||||
utils.Sort(values, pq.comparator)
|
||||
pq.isSorted = true
|
||||
}
|
||||
return values
|
||||
}
|
||||
86
priority_queue/priority_queue_test.go
Normal file
86
priority_queue/priority_queue_test.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package plist
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/emirpasic/gods/utils"
|
||||
|
||||
"github.com/Pallinder/go-randomdata"
|
||||
|
||||
"github.com/emirpasic/gods/trees/binaryheap"
|
||||
)
|
||||
|
||||
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))
|
||||
if i%100 == 0 {
|
||||
p.Values()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
33
structure.go
33
structure.go
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user