优化heap
This commit is contained in:
parent
5ef163bca7
commit
411281d865
|
@ -110,40 +110,37 @@ func (h *Heap) Pop() (interface{}, bool) {
|
|||
|
||||
downvalue := h.elements[h.size]
|
||||
var cidx, c1, c2 int
|
||||
var cvalue1, cvalue2, cvalue interface{}
|
||||
var cvalue1, cvalue2 interface{}
|
||||
// down
|
||||
for {
|
||||
cidx = curidx << 1
|
||||
|
||||
c2 = cidx + 2
|
||||
if c2 < h.size {
|
||||
cvalue2 = h.elements[c2]
|
||||
|
||||
c1 = cidx + 1
|
||||
c2 = cidx + 2
|
||||
|
||||
if c2 < h.size {
|
||||
|
||||
cvalue2 = h.elements[c2]
|
||||
cvalue1 = h.elements[c1]
|
||||
|
||||
if h.Compare(cvalue1, cvalue2) >= 0 {
|
||||
cidx = c1
|
||||
cvalue = cvalue1
|
||||
} else {
|
||||
cidx = c2
|
||||
cvalue = cvalue2
|
||||
}
|
||||
} else {
|
||||
|
||||
c1 = cidx + 1
|
||||
if c1 < h.size {
|
||||
cvalue1 = h.elements[c1]
|
||||
cidx = c1
|
||||
cvalue = cvalue1
|
||||
} else {
|
||||
break
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if h.Compare(cvalue, downvalue) > 0 {
|
||||
h.elements[curidx] = cvalue
|
||||
if h.Compare(h.elements[cidx], downvalue) > 0 {
|
||||
h.elements[curidx] = h.elements[cidx]
|
||||
curidx = cidx
|
||||
} else {
|
||||
break
|
||||
|
|
|
@ -1,11 +1,60 @@
|
|||
package heap
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/474420502/focus/compare"
|
||||
"github.com/Pallinder/go-randomdata"
|
||||
)
|
||||
|
||||
func TestHeapGrowSlimming(t *testing.T) {
|
||||
h := New(compare.Int)
|
||||
var results []int
|
||||
for i := 0; i < 100; i++ {
|
||||
v := randomdata.Number(0, 100)
|
||||
results = append(results, v)
|
||||
h.Put(v)
|
||||
}
|
||||
sort.Slice(results, func(i, j int) bool {
|
||||
if results[i] > results[j] {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
if h.Size() != 100 || h.Empty() {
|
||||
t.Error("size != 100")
|
||||
}
|
||||
|
||||
for i := 0; !h.Empty(); i++ {
|
||||
v, _ := h.Pop()
|
||||
if results[i] != v {
|
||||
t.Error("heap is error")
|
||||
}
|
||||
}
|
||||
|
||||
if h.Size() != 0 {
|
||||
t.Error("size != 0")
|
||||
}
|
||||
|
||||
h.Put(1)
|
||||
h.Put(5)
|
||||
h.Put(2)
|
||||
|
||||
if h.Values()[0] != 5 {
|
||||
t.Error("top is not equal to 5")
|
||||
}
|
||||
|
||||
h.Clear()
|
||||
h.Reborn()
|
||||
|
||||
if !h.Empty() {
|
||||
t.Error("clear reborn is error")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestHeapPushTopPop(t *testing.T) {
|
||||
h := New(compare.Int)
|
||||
l := []int{9, 5, 15, 2, 3}
|
||||
|
|
Loading…
Reference in New Issue
Block a user