-
Notifications
You must be signed in to change notification settings - Fork 168
/
priorityqueue.go
89 lines (72 loc) · 2.16 KB
/
priorityqueue.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
package queue
import "container/heap"
// An Item is something we manage in a Priority queue.
type Item struct {
Value interface{} // 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 PriorityQueueSlice implements heap.Interface and holds Items.
type PriorityQueueSlice []*Item
func (pq PriorityQueueSlice) Len() int { return len(pq) }
func (pq PriorityQueueSlice) Less(i, j int) bool {
// We want Pop to give us the highest, not lowest, Priority so we use greater than here.
return pq[i].Priority > pq[j].Priority
}
func (pq PriorityQueueSlice) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].Index = i
pq[j].Index = j
}
func (pq *PriorityQueueSlice) Push(x interface{}) {
n := len(*pq)
item := x.(*Item)
item.Index = n
*pq = append(*pq, item)
}
func (pq *PriorityQueueSlice) Pop() interface{} {
old := *pq
n := len(old)
item := old[n-1]
item.Index = -1 // for safety
*pq = old[0 : n-1]
return item
}
// update modifies the Priority and Value of an Item in the queue.
func (pq *PriorityQueueSlice) Update(item *Item, value interface{}, priority int) {
item.Value = value
item.Priority = priority
heap.Fix(pq, item.Index)
}
type PriorityQueue struct {
priorityQueueSlice PriorityQueueSlice
}
//参数是数据大小
func (pq *PriorityQueue) Init(initItemSliceNum int){
pq.priorityQueueSlice = make(PriorityQueueSlice,0,initItemSliceNum)
}
func (pq *PriorityQueue) Push(item *Item){
heap.Push(&pq.priorityQueueSlice, item)
}
func (pq *PriorityQueue) Pop() *Item {
if pq.Len() ==0 {
return nil
}
return heap.Pop(&pq.priorityQueueSlice).(*Item)
}
func (pq *PriorityQueue) GetHighest() *Item{
if len(pq.priorityQueueSlice)>0 {
return pq.priorityQueueSlice[0]
}
return nil
}
func (pq *PriorityQueue) Len() int {
return len(pq.priorityQueueSlice)
}
func (pq *PriorityQueue) Update(item *Item, value interface{}, priority int){
pq.priorityQueueSlice.Update(item, value, priority)
}
func (pq *PriorityQueue) Remove(item *Item){
heap.Remove(&pq.priorityQueueSlice,item.Index)
}