Debian Codegraph @

heap.go

raw symbols SCIP
1 package graph
2
3 type prioQueue struct {
4 heap []int // vertices in heap order
5 index []int // index of each vertex in the heap
6 cost []int64
7 }
8
9 func emptyPrioQueue(cost []int64) *prioQueue {
10 return &prioQueue{
11 index: make([]int, len(cost)),
12 cost: cost,
13 }
14 }
15
16 func newPrioQueue(cost []int64) *prioQueue {
17 n := len(cost)
18 q := &prioQueue{
19 heap: make([]int, n),
20 index: make([]int, n),
21 cost: cost,
22 }
23 for i := range q.heap {
24 q.heap[i] = i
25 q.index[i] = i
26 }
27 return q
28 }
29
30 // Len returns the number of elements in the queue.
31 func (q *prioQueue) Len() int {
32 return len(q.heap)
33 }
34
35 // Push pushes v onto the queue.
36 // The time complexity is O(log n) where n = q.Len().
37 func (q *prioQueue) Push(v int) {
38 n := q.Len()
39 q.heap = append(q.heap, v)
40 q.index[v] = n
41 q.up(n)
42 }
43
44 // Pop removes the minimum element from the queue and returns it.
45 // The time complexity is O(log n) where n = q.Len().
46 func (q *prioQueue) Pop() int {
47 n := q.Len() - 1
48 q.swap(0, n)
49 q.down(0, n)
50
51 v := q.heap[n]
52 q.index[v] = -1
53 q.heap = q.heap[:n]
54 return v
55 }
56
57 // Contains tells whether v is in the queue.
58 func (q *prioQueue) Contains(v int) bool {
59 return q.index[v] >= 0
60 }
61
62 // Fix re-establishes the ordering after the cost for v has changed.
63 // The time complexity is O(log n) where n = q.Len().
64 func (q *prioQueue) Fix(v int) {
65 if i := q.index[v]; !q.down(i, q.Len()) {
66 q.up(i)
67 }
68 }
69
70 func (q *prioQueue) less(i, j int) bool {
71 return q.cost[q.heap[i]] < q.cost[q.heap[j]]
72 }
73
74 func (q *prioQueue) swap(i, j int) {
75 q.heap[i], q.heap[j] = q.heap[j], q.heap[i]
76 q.index[q.heap[i]] = i
77 q.index[q.heap[j]] = j
78 }
79
80 func (q *prioQueue) up(j int) {
81 for {
82 i := (j - 1) / 2 // parent
83 if i == j || !q.less(j, i) {
84 break
85 }
86 q.swap(i, j)
87 j = i
88 }
89 }
90
91 func (q *prioQueue) down(i0, n int) bool {
92 i := i0
93 for {
94 j1 := 2*i + 1
95 if j1 >= n || j1 < 0 { // j1 < 0 after int overflow
96 break
97 }
98 j := j1 // left child
99 if j2 := j1 + 1; j2 < n && q.less(j2, j1) {
100 j = j2 // = 2*i + 2 // right child
101 }
102 if !q.less(j, i) {
103 break
104 }
105 q.swap(i, j)
106 i = j
107 }
108 return i > i0
109 }