Source file
src/container/heap/example_pq_test.go
1
2
3
4
5
6 package heap_test
7
8 import (
9 "container/heap"
10 "fmt"
11 )
12
13
14 type Item struct {
15 value string
16 priority int
17
18 index int
19 }
20
21
22 type PriorityQueue []*Item
23
24 func (pq PriorityQueue) Len() int { return len(pq) }
25
26 func (pq PriorityQueue) Less(i, j int) bool {
27
28 return pq[i].priority > pq[j].priority
29 }
30
31 func (pq PriorityQueue) Swap(i, j int) {
32 pq[i], pq[j] = pq[j], pq[i]
33 pq[i].index = i
34 pq[j].index = j
35 }
36
37 func (pq *PriorityQueue) Push(x any) {
38 n := len(*pq)
39 item := x.(*Item)
40 item.index = n
41 *pq = append(*pq, item)
42 }
43
44 func (pq *PriorityQueue) Pop() any {
45 old := *pq
46 n := len(old)
47 item := old[n-1]
48 old[n-1] = nil
49 item.index = -1
50 *pq = old[0 : n-1]
51 return item
52 }
53
54
55 func (pq *PriorityQueue) update(item *Item, value string, priority int) {
56 item.value = value
57 item.priority = priority
58 heap.Fix(pq, item.index)
59 }
60
61
62
63 func Example_priorityQueue() {
64
65 items := map[string]int{
66 "banana": 3, "apple": 2, "pear": 4,
67 }
68
69
70
71 pq := make(PriorityQueue, len(items))
72 i := 0
73 for value, priority := range items {
74 pq[i] = &Item{
75 value: value,
76 priority: priority,
77 index: i,
78 }
79 i++
80 }
81 heap.Init(&pq)
82
83
84 item := &Item{
85 value: "orange",
86 priority: 1,
87 }
88 heap.Push(&pq, item)
89 pq.update(item, item.value, 5)
90
91
92 for pq.Len() > 0 {
93 item := heap.Pop(&pq).(*Item)
94 fmt.Printf("%.2d:%s ", item.priority, item.value)
95 }
96
97
98 }
99
View as plain text