-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathuheap.go
59 lines (48 loc) · 990 Bytes
/
uheap.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
package ron
import "container/heap"
type UUIDHeap struct {
uuids []UUID
desc bool
}
func MakeUHeap(desc bool, size int) (ret UUIDHeap) {
ret.uuids = make([]UUID, 0, size)
ret.desc = desc
return
}
func (h *UUIDHeap) Less(i, j int) bool {
c := Compare(h.uuids[i], h.uuids[j])
if h.desc {
c = -c
}
return c < 0
}
func (h UUIDHeap) Len() int { return len(h.uuids) }
func (h UUIDHeap) Swap(i, j int) {
h.uuids[i], h.uuids[j] = h.uuids[j], h.uuids[i]
}
func (h *UUIDHeap) Push(x interface{}) {
item := x.(UUID)
h.uuids = append(h.uuids, item)
}
func (h *UUIDHeap) Pop() interface{} {
n := len(h.uuids)
item := h.uuids[n-1]
h.uuids = h.uuids[0 : n-1]
return item
}
func (h *UUIDHeap) Put(u UUID) {
heap.Push(h, u)
}
func (h *UUIDHeap) Take() UUID {
return heap.Pop(h).(UUID)
}
func (h *UUIDHeap) PopUnique() (ret UUID) {
if len(h.uuids) == 0 {
return ZERO_UUID
}
ret = heap.Pop(h).(UUID)
for len(h.uuids) > 0 && h.uuids[0] == ret {
heap.Pop(h)
}
return
}