-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathheap.go
55 lines (46 loc) · 987 Bytes
/
heap.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
package gohalt
import "sync"
type blatheap struct {
buffer []uint64
lock sync.Mutex
}
func (lh *blatheap) Len() int {
lh.lock.Lock()
defer lh.lock.Unlock()
return len(lh.buffer)
}
func (lh *blatheap) Less(i int, j int) bool {
lh.lock.Lock()
defer lh.lock.Unlock()
return lh.buffer[i] < lh.buffer[j]
}
func (lh *blatheap) Swap(i int, j int) {
lh.lock.Lock()
defer lh.lock.Unlock()
lh.buffer[i], lh.buffer[j] = lh.buffer[j], lh.buffer[i]
}
func (lh *blatheap) Push(x interface{}) {
lh.lock.Lock()
defer lh.lock.Unlock()
if lat, ok := x.(uint64); ok {
lh.buffer = append(lh.buffer, lat)
}
}
func (lh *blatheap) Pop() interface{} {
lh.lock.Lock()
defer lh.lock.Unlock()
blen := len(lh.buffer)
val := lh.buffer[blen-1]
lh.buffer = lh.buffer[:blen-1]
return val
}
func (lh *blatheap) At(pos int) uint64 {
lh.lock.Lock()
defer lh.lock.Unlock()
return lh.buffer[pos]
}
func (lh *blatheap) Prune() {
lh.lock.Lock()
defer lh.lock.Unlock()
lh.buffer = nil
}