-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathcontainer.go
103 lines (85 loc) · 2.13 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package antnet
import (
"container/heap"
)
type item struct {
value int // The value of the item; arbitrary.
priority int // The priority of the item in the queue.
index int // The index of the item in the heap.
}
type priorityQueue struct {
im map[int]int
m map[int]*item
cf func(l, r *item) bool
}
func minHeapComp(l, r *item) bool { return l.priority < r.priority }
func maxHeapComp(l, r *item) bool { return l.priority > r.priority }
func (r *priorityQueue) Len() int { return len(r.im) }
func (r *priorityQueue) Less(i, j int) bool { return r.cf(r.m[r.im[i]], r.m[r.im[j]]) }
func (r *priorityQueue) Swap(i, j int) {
r.im[i], r.im[j] = r.im[j], r.im[i]
r.m[r.im[i]].index = i
r.m[r.im[j]].index = j
}
func (r *priorityQueue) Push(x interface{}) {
it := x.(*item)
if _, ok := r.m[it.value]; !ok {
n := len(r.im)
it.index = n
r.im[n] = it.value
r.m[it.value] = it
} else {
LogWarn("heap can't insert repeated value:%v", it.value)
}
}
func (r *priorityQueue) Pop() interface{} {
n := len(r.im)
it := r.m[r.im[n-1]]
delete(r.im, n-1)
delete(r.m, it.value)
return it
}
type Heap struct {
p *priorityQueue
}
func (r *Heap) Push(priority, value int) {
heap.Push(r.p, &item{priority: priority, value: value})
}
func (r *Heap) Pop() int {
return heap.Pop(r.p).(*item).value
}
func (r *Heap) Update(value, priority int) {
if it, ok := r.p.m[value]; ok {
it.priority = priority
heap.Fix(r.p, it.index)
}
}
func (r *Heap) Top() int {
_, v := r.GetMin()
return v
}
func (r *Heap) GetMin() (priority int, value int) {
it := heap.Pop(r.p).(*item)
heap.Push(r.p, it)
return it.priority, it.value
}
func (r *Heap) GetMax() (priority int, value int) {
return r.GetMin()
}
func (r *Heap) GetPriority(value int) (priority int, find bool) {
it, ok := r.p.m[value]
return it.priority, ok
}
func (r *Heap) Len() int {
return len(r.p.m)
}
func NewMinHeap() *Heap {
h := &Heap{p: &priorityQueue{m: map[int]*item{}, im: map[int]int{}, cf: minHeapComp}}
heap.Init(h.p)
return h
}
func NewMaxHeap() *Heap {
h := &Heap{p: &priorityQueue{m: map[int]*item{}, im: map[int]int{}, cf: maxHeapComp}}
heap.Init(h.p)
return h
}