-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtopk.go
137 lines (118 loc) · 2.86 KB
/
topk.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package topk
import (
"container/heap"
"fmt"
"strings"
)
// An Item is something we manage in a priority queue.
type Item struct {
value int // The value of the item; arbitrary.
priority int // The priority of the item in the queue.
// The index is needed by update and is maintained by the heap.Interface methods.
index int // The index of the item in the heap.
}
// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue struct {
item_array []*Item
n int
capacity int
}
func NewPriorityQueueForTopK(k int) *PriorityQueue {
return &PriorityQueue{
item_array: make([]*Item, k),
n: 0,
capacity: k,
}
}
func (pq PriorityQueue) Len() int { return pq.n }
func (pq PriorityQueue) Less(i, j int) bool {
// fmt.Printf("Less, i:%d, j:%d, item_array.len:%d, pi:%p, pj:%p", i, j, pq.n, pq.item_array[i], pq.item_array[j])
// We want Pop to give us the highest, not lowest, priority so we use greater than here.
return pq.item_array[i].priority < pq.item_array[j].priority
}
func (pq PriorityQueue) Swap(i, j int) {
pq.item_array[i], pq.item_array[j] = pq.item_array[j], pq.item_array[i]
pq.item_array[i].index = i
pq.item_array[j].index = j
}
func (pq *PriorityQueue) Push(x any) {
if pq.n == pq.capacity {
return
}
item := x.(*Item)
item.index = pq.n
pq.item_array[pq.n] = item
pq.n += 1
}
func (pq *PriorityQueue) Pop() any {
n := pq.n
if n == 0 {
return nil
}
item := pq.item_array[n-1]
pq.item_array[n-1] = nil // avoid memory leak
item.index = -1 // for safety
pq.n -= 1
return item
}
func (pq *PriorityQueue) Top() *Item {
//n := pq.n
item := pq.item_array[0]
return item
}
func (pq *PriorityQueue) TryPush(x any) {
item := x.(*Item)
if pq.n == pq.capacity {
top := pq.Top()
if item.priority > top.priority {
heap.Pop(pq)
heap.Push(pq, item)
}
} else {
pq.Push(item)
if pq.n > 1 {
heap.Fix(pq, pq.n-1)
}
}
}
// update modifies the priority and value of an Item in the queue.
func (pq *PriorityQueue) update(item *Item, value int, priority int) {
item.value = value
item.priority = priority
heap.Fix(pq, item.index)
}
type TOPK struct {
pq *PriorityQueue
}
func NewTOPK(k int) *TOPK {
ret := &TOPK{
pq: NewPriorityQueueForTopK(k),
}
ret.Init()
return ret
}
func (topk *TOPK) Init() {
heap.Init(topk.pq)
}
func (topk *TOPK) Add(item *Item) {
topk.pq.TryPush(item)
}
func (topk *TOPK) Add2(value int, priority int) {
topk.pq.TryPush(&Item{value: value, priority: priority})
}
func (topk *TOPK) Dump() {
for i := 0; i < topk.pq.n; i++ {
item := topk.pq.item_array[i]
fmt.Printf("%d: %d\n", item.value, item.priority)
}
}
func (topk *TOPK) Dumps() string {
var sb strings.Builder
for i := 0; i < topk.pq.n; i++ {
item := topk.pq.item_array[i]
s := fmt.Sprintf("%d:%d", item.value, item.priority)
sb.WriteString(s)
sb.WriteString(",")
}
return sb.String()
}