focus/heap/heap.go

155 lines
2.3 KiB
Go
Raw Normal View History

new file: .gitignore new file: LICENSE new file: README.md new file: compare/compare.go new file: for_test.go new file: heap/heap.go new file: heap/heap_test.go new file: interface.go new file: lastack/lastack.go new file: lastack/lastack_test.go new file: list/list.go new file: list/list_test.go new file: map/hashmap/hashmap.go new file: map/hashmap/hashmap_test.go new file: priority_list/iterator.go new file: priority_list/priority_list.go new file: priority_list/priority_list_test.go new file: priority_queue/iterator.go new file: priority_queue/priority_queue.go new file: priority_queue/priority_queue_test.go new file: priority_queue/vbt.go new file: priority_queue/vbt_test.go new file: priority_queuekey/iterator.go new file: priority_queuekey/priority_queuekey.go new file: priority_queuekey/priority_queuekey_test.go new file: priority_queuekey/vbt.go new file: priority_queuekey/vbt_test.go new file: set/hashset/hashset.go new file: set/hashset/hashset_test.go new file: set/treeset/treeset.go new file: set/treeset/treeset_test.go new file: sparse_array/array2/array2.go new file: sparse_array/array2/array2_test.go new file: sparse_array/array3/array3.go new file: sparse_array/array3/array3_test.go new file: sparse_array/arrayn/arrayn.go new file: sparse_array/arrayn/arrayn_test.go new file: stack/stack.go new file: stack/stack_test.go new file: tree/avl/avl.go new file: tree/avl/avl_test.go new file: tree/avl/iterator.go new file: tree/avldup/avldup.go new file: tree/avldup/avldup_test.go new file: tree/avldup/iterator.go new file: tree/avlkey/avlkey.go new file: tree/avlkey/avlkey_test.go new file: tree/avlkey/iterator.go new file: tree/avlkeydup/avlkeydup.go new file: tree/avlkeydup/avlkeydup_test.go new file: tree/avlkeydup/iterator.go new file: tree/vbt/iterator.go new file: tree/vbt/vbt.go new file: tree/vbt/vbt_test.go new file: tree/vbtkey/iterator.go new file: tree/vbtkey/vbtkey.go new file: tree/vbtkey/vbtkey_test.go
2019-05-08 02:42:51 +00:00
package heap
import (
2019-05-08 03:04:03 +00:00
"github.com/474420502/focus/compare"
new file: .gitignore new file: LICENSE new file: README.md new file: compare/compare.go new file: for_test.go new file: heap/heap.go new file: heap/heap_test.go new file: interface.go new file: lastack/lastack.go new file: lastack/lastack_test.go new file: list/list.go new file: list/list_test.go new file: map/hashmap/hashmap.go new file: map/hashmap/hashmap_test.go new file: priority_list/iterator.go new file: priority_list/priority_list.go new file: priority_list/priority_list_test.go new file: priority_queue/iterator.go new file: priority_queue/priority_queue.go new file: priority_queue/priority_queue_test.go new file: priority_queue/vbt.go new file: priority_queue/vbt_test.go new file: priority_queuekey/iterator.go new file: priority_queuekey/priority_queuekey.go new file: priority_queuekey/priority_queuekey_test.go new file: priority_queuekey/vbt.go new file: priority_queuekey/vbt_test.go new file: set/hashset/hashset.go new file: set/hashset/hashset_test.go new file: set/treeset/treeset.go new file: set/treeset/treeset_test.go new file: sparse_array/array2/array2.go new file: sparse_array/array2/array2_test.go new file: sparse_array/array3/array3.go new file: sparse_array/array3/array3_test.go new file: sparse_array/arrayn/arrayn.go new file: sparse_array/arrayn/arrayn_test.go new file: stack/stack.go new file: stack/stack_test.go new file: tree/avl/avl.go new file: tree/avl/avl_test.go new file: tree/avl/iterator.go new file: tree/avldup/avldup.go new file: tree/avldup/avldup_test.go new file: tree/avldup/iterator.go new file: tree/avlkey/avlkey.go new file: tree/avlkey/avlkey_test.go new file: tree/avlkey/iterator.go new file: tree/avlkeydup/avlkeydup.go new file: tree/avlkeydup/avlkeydup_test.go new file: tree/avlkeydup/iterator.go new file: tree/vbt/iterator.go new file: tree/vbt/vbt.go new file: tree/vbt/vbt_test.go new file: tree/vbtkey/iterator.go new file: tree/vbtkey/vbtkey.go new file: tree/vbtkey/vbtkey_test.go
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
}
func (h *Heap) Push(v interface{}) {
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
var cvalue1, cvalue2, cvalue interface{}
// down
for {
cidx = curidx << 1
c2 = cidx + 2
if c2 < h.size {
cvalue2 = h.elements[c2]
c1 = cidx + 1
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
curidx = cidx
} else {
break
}
}
h.elements[curidx] = downvalue
return top, true
}