-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.go
65 lines (63 loc) · 1.5 KB
/
container.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package golangUtil
type heap struct {
heapArr []int
value []uintptr
length int
}
func (h *heap) down(index int) {
var result = index
if index*2 < h.length && h.heapArr[result] < h.heapArr[index*2] {
result = index * 2
}
if index*2+1 < h.length && h.heapArr[result] < h.heapArr[index*2+1] {
result = index*2 + 1
}
if result != index {
h.swap(index, result)
h.down(result)
}
}
func (h *heap) up(index int) {
for index/2 > 0 {
if h.heapArr[index] > h.heapArr[index/2] {
h.swap(index, index/2)
index = index / 2
} else {
break
}
}
}
func (h *heap) peekMin() uintptr {
return h.value[h.length-1]
}
func (h *heap) insert(key int, value uintptr) {
h.length++
if h.length >= len(h.heapArr) {
h.heapArr = append(h.heapArr, make([]int, 100)...)
h.value = append(h.value, make([]uintptr, 100)...)
}
h.heapArr[h.length] = key
h.value[h.length] = value
h.down(h.length)
h.up(h.length)
}
func (h *heap) delete(index int) {
h.swap(index, h.length)
h.length--
h.down(index)
h.up(index)
}
func (h *heap) peekMax() uintptr {
if h.length <= 0 {
return uintptr(0)
}
return h.value[1]
}
func (h *heap) swap(heapPre int, heapLast int) {
h.heapArr[heapPre] = h.heapArr[heapPre] ^ h.heapArr[heapLast]
h.heapArr[heapLast] = h.heapArr[heapPre] ^ h.heapArr[heapLast]
h.heapArr[heapPre] = h.heapArr[heapPre] ^ h.heapArr[heapLast]
h.value[heapPre] = h.value[heapPre] ^ h.value[heapLast]
h.value[heapLast] = h.value[heapPre] ^ h.value[heapLast]
h.value[heapPre] = h.value[heapPre] ^ h.value[heapLast]
}