-
Notifications
You must be signed in to change notification settings - Fork 285
/
heap.go
113 lines (95 loc) · 1.75 KB
/
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
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
package shuttle
import (
"container/heap"
"sync"
"sync/atomic"
)
type Item struct {
Value interface{}
Ticket int64
}
type MinHeap []*Item
func (m *MinHeap) Len() int { return len(*m) }
func (m *MinHeap) Less(i, j int) bool {
return (*m)[i].Ticket < (*m)[j].Ticket
}
func (m *MinHeap) Swap(i, j int) {
(*m)[i], (*m)[j] = (*m)[j], (*m)[i]
}
func (m *MinHeap) Push(x interface{}) {
item := x.(*Item)
*m = append(*m, item)
}
func (m *MinHeap) Pop() interface{} {
old := *m
n := len(old)
item := old[n-1]
*m = old[:n-1]
return item
}
func NewMinArrange() *MinArrange {
h := make(MinHeap, 0, 8)
return &MinArrange{
oldTicket: 0,
minHeap: &h,
result: make(chan *Item, 32),
closed: false,
closeChan: make(chan bool, 1),
}
}
type MinArrange struct {
oldTicket int64
minHeap *MinHeap
result chan *Item
closeChan chan bool
closed bool
sync.Mutex
}
func (m *MinArrange) Pop() (item *Item) {
item = <-m.result
return
}
func (m *MinArrange) Push(v *Item) {
m.Lock()
heap.Push(m.minHeap, v)
Loop:
for len(*m.minHeap) > 0 && m.oldTicket+1 == (*m.minHeap)[0].Ticket {
select {
case m.result <- (*m.minHeap)[0]:
heap.Pop(m.minHeap)
m.oldTicket ++
default:
break Loop
}
}
m.Unlock()
}
func (m *MinArrange) Close() {
m.closed = true
m.closeChan <- true
}
func NewSequenceHeap() *SequenceHeap {
return &SequenceHeap{
NewMinArrange(), 0,
}
}
type SequenceHeap struct {
*MinArrange
ticket int64
}
func (s *SequenceHeap) Pop() interface{} {
v := s.MinArrange.Pop()
if v != nil {
return v.Value
}
return nil
}
func (s *SequenceHeap) Push(v interface{}) {
s.MinArrange.Push(&Item{
Value: v,
Ticket: s.getTicket(),
})
}
func (s *SequenceHeap) getTicket() int64 {
return atomic.AddInt64(&s.ticket, 1)
}