2019-05-08 02:42:51 +00:00
|
|
|
package heap
|
|
|
|
|
|
|
|
import (
|
2019-05-08 03:04:03 +00:00
|
|
|
"github.com/474420502/focus/compare"
|
2019-05-08 02:42:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Heap struct {
|
|
|
|
size int
|
|
|
|
elements []interface{}
|
|
|
|
Compare compare.Compare
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(Compare compare.Compare) *Heap {
|
|
|
|
h := &Heap{Compare: Compare}
|
|
|
|
h.elements = make([]interface{}, 16, 16)
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heap) Size() int {
|
|
|
|
return h.size
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heap) Values() []interface{} {
|
|
|
|
return h.elements[0:h.size]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heap) grow() {
|
|
|
|
ecap := len(h.elements)
|
|
|
|
if h.size >= ecap {
|
|
|
|
ecap = ecap << 1
|
|
|
|
grow := make([]interface{}, ecap, ecap)
|
|
|
|
copy(grow, h.elements)
|
|
|
|
h.elements = grow
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heap) Empty() bool {
|
|
|
|
return h.size < 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heap) Clear() {
|
|
|
|
h.size = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heap) Reborn() {
|
|
|
|
h.size = 0
|
|
|
|
h.elements = make([]interface{}, 16, 16)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heap) Top() (interface{}, bool) {
|
|
|
|
if h.size != 0 {
|
|
|
|
return h.elements[0], true
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2019-07-28 18:18:22 +00:00
|
|
|
func (h *Heap) Put(v interface{}) {
|
2019-05-08 02:42:51 +00:00
|
|
|
if v == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
h.grow()
|
|
|
|
|
|
|
|
curidx := h.size
|
|
|
|
h.size++
|
|
|
|
// up
|
|
|
|
for curidx != 0 {
|
|
|
|
pidx := (curidx - 1) >> 1
|
|
|
|
pvalue := h.elements[pidx]
|
|
|
|
if h.Compare(v, pvalue) > 0 {
|
|
|
|
h.elements[curidx] = pvalue
|
|
|
|
curidx = pidx
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
h.elements[curidx] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heap) slimming() {
|
|
|
|
|
|
|
|
elen := len(h.elements)
|
|
|
|
if elen >= 32 {
|
|
|
|
ecap := elen >> 1
|
|
|
|
if h.size <= ecap {
|
|
|
|
ecap = elen - (ecap >> 1)
|
|
|
|
slimming := make([]interface{}, ecap, ecap)
|
|
|
|
copy(slimming, h.elements)
|
|
|
|
h.elements = slimming
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heap) Pop() (interface{}, bool) {
|
|
|
|
|
|
|
|
if h.size == 0 {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
curidx := 0
|
|
|
|
top := h.elements[curidx]
|
|
|
|
h.size--
|
|
|
|
|
|
|
|
h.slimming()
|
|
|
|
|
|
|
|
if h.size == 0 {
|
|
|
|
return top, true
|
|
|
|
}
|
|
|
|
|
|
|
|
downvalue := h.elements[h.size]
|
|
|
|
var cidx, c1, c2 int
|
2019-08-01 02:07:26 +00:00
|
|
|
var cvalue1, cvalue2 interface{}
|
2019-05-08 02:42:51 +00:00
|
|
|
// down
|
|
|
|
for {
|
|
|
|
cidx = curidx << 1
|
|
|
|
|
2019-08-01 02:07:26 +00:00
|
|
|
c1 = cidx + 1
|
2019-05-08 02:42:51 +00:00
|
|
|
c2 = cidx + 2
|
2019-08-01 02:07:26 +00:00
|
|
|
|
2019-05-08 02:42:51 +00:00
|
|
|
if c2 < h.size {
|
|
|
|
|
2019-08-01 02:07:26 +00:00
|
|
|
cvalue2 = h.elements[c2]
|
2019-05-08 02:42:51 +00:00
|
|
|
cvalue1 = h.elements[c1]
|
|
|
|
|
|
|
|
if h.Compare(cvalue1, cvalue2) >= 0 {
|
|
|
|
cidx = c1
|
|
|
|
} else {
|
|
|
|
cidx = c2
|
|
|
|
}
|
|
|
|
} else {
|
2019-08-01 02:36:26 +00:00
|
|
|
cidx = c1
|
2019-05-08 02:42:51 +00:00
|
|
|
if c1 < h.size {
|
|
|
|
cvalue1 = h.elements[c1]
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-08-01 02:07:26 +00:00
|
|
|
if h.Compare(h.elements[cidx], downvalue) > 0 {
|
|
|
|
h.elements[curidx] = h.elements[cidx]
|
2019-05-08 02:42:51 +00:00
|
|
|
curidx = cidx
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
h.elements[curidx] = downvalue
|
|
|
|
return top, true
|
|
|
|
}
|