再度优化heap
This commit is contained in:
parent
f63759645e
commit
b6fe42de8f
|
@ -110,7 +110,6 @@ func (h *Heap) Pop() (interface{}, bool) {
|
||||||
|
|
||||||
downvalue := h.elements[h.size]
|
downvalue := h.elements[h.size]
|
||||||
var cidx, c1, c2 int
|
var cidx, c1, c2 int
|
||||||
var cvalue1, cvalue2 interface{}
|
|
||||||
// down
|
// down
|
||||||
for {
|
for {
|
||||||
cidx = curidx << 1
|
cidx = curidx << 1
|
||||||
|
@ -119,11 +118,7 @@ func (h *Heap) Pop() (interface{}, bool) {
|
||||||
c2 = cidx + 2
|
c2 = cidx + 2
|
||||||
|
|
||||||
if c2 < h.size {
|
if c2 < h.size {
|
||||||
|
if h.Compare(h.elements[c1], h.elements[c2]) >= 0 {
|
||||||
cvalue2 = h.elements[c2]
|
|
||||||
cvalue1 = h.elements[c1]
|
|
||||||
|
|
||||||
if h.Compare(cvalue1, cvalue2) >= 0 {
|
|
||||||
cidx = c1
|
cidx = c1
|
||||||
} else {
|
} else {
|
||||||
cidx = c2
|
cidx = c2
|
||||||
|
|
|
@ -9,6 +9,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHeapGrowSlimming(t *testing.T) {
|
func TestHeapGrowSlimming(t *testing.T) {
|
||||||
|
|
||||||
|
for ii := 0; ii < 1000; ii++ {
|
||||||
|
|
||||||
h := New(compare.Int)
|
h := New(compare.Int)
|
||||||
var results []int
|
var results []int
|
||||||
for i := 0; i < 100; i++ {
|
for i := 0; i < 100; i++ {
|
||||||
|
@ -52,6 +55,7 @@ func TestHeapGrowSlimming(t *testing.T) {
|
||||||
if !h.Empty() {
|
if !h.Empty() {
|
||||||
t.Error("clear reborn is error")
|
t.Error("clear reborn is error")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,6 +108,20 @@ func TestHeapPushTopPop(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// func BenchmarkPush(b *testing.B) {
|
||||||
|
// h := New(compare.Int)
|
||||||
|
// b.N = 40000000
|
||||||
|
// var results []int
|
||||||
|
// for i := 0; i < b.N; i++ {
|
||||||
|
// results = append(results, randomdata.Number(0, 1000000000))
|
||||||
|
// }
|
||||||
|
|
||||||
|
// b.ResetTimer()
|
||||||
|
// for _, v := range results {
|
||||||
|
// h.Put(v)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
// func Int(k1, k2 interface{}) int {
|
// func Int(k1, k2 interface{}) int {
|
||||||
// c1 := k1.(int)
|
// c1 := k1.(int)
|
||||||
// c2 := k2.(int)
|
// c2 := k2.(int)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user