Debian Codegraph @

collection.go

raw symbols SCIP
1 package graph
2
3 import (
4 "container/heap"
5 "errors"
6 )
7
8 // priorityQueue implements a minimum priority queue using a minimum binary heap
9 // that prioritizes smaller values over larger values.
10 type priorityQueue[T comparable] struct {
11 items *minHeap[T]
12 cache map[T]*priorityItem[T]
13 }
14
15 // priorityItem is an item on the binary heap consisting of a priority value and
16 // an actual payload value.
17 type priorityItem[T comparable] struct {
18 value T
19 priority float64
20 index int
21 }
22
23 func newPriorityQueue[T comparable]() *priorityQueue[T] {
24 return &priorityQueue[T]{
25 items: &minHeap[T]{},
26 cache: map[T]*priorityItem[T]{},
27 }
28 }
29
30 // Len returns the total number of items in the priority queue.
31 func (p *priorityQueue[T]) Len() int {
32 return p.items.Len()
33 }
34
35 // Push pushes a new item with the given priority into the queue. This operation
36 // may cause a re-balance of the heap and thus scales with O(log n).
37 func (p *priorityQueue[T]) Push(item T, priority float64) {
38 if _, ok := p.cache[item]; ok {
39 return
40 }
41
42 newItem := &priorityItem[T]{
43 value: item,
44 priority: priority,
45 index: 0,
46 }
47
48 heap.Push(p.items, newItem)
49 p.cache[item] = newItem
50 }
51
52 // Pop returns and removes the item with the lowest priority. This operation may
53 // cause a re-balance of the heap and thus scales with O(log n).
54 func (p *priorityQueue[T]) Pop() (T, error) {
55 if len(*p.items) == 0 {
56 var empty T
57 return empty, errors.New("priority queue is empty")
58 }
59
60 item := heap.Pop(p.items).(*priorityItem[T])
61 delete(p.cache, item.value)
62
63 return item.value, nil
64 }
65
66 // UpdatePriority updates the priority of a given item and sets it to the given
67 // priority. If the item doesn't exist, nothing happens. This operation may
68 // cause a re-balance of the heap and this scales with O(log n).
69 func (p *priorityQueue[T]) UpdatePriority(item T, priority float64) {
70 targetItem, ok := p.cache[item]
71 if !ok {
72 return
73 }
74
75 targetItem.priority = priority
76 heap.Fix(p.items, targetItem.index)
77 }
78
79 // minHeap is a minimum binary heap that implements heap.Interface.
80 type minHeap[T comparable] []*priorityItem[T]
81
82 func (m *minHeap[T]) Len() int {
83 return len(*m)
84 }
85
86 func (m *minHeap[T]) Less(i, j int) bool {
87 return (*m)[i].priority < (*m)[j].priority
88 }
89
90 func (m *minHeap[T]) Swap(i, j int) {
91 (*m)[i], (*m)[j] = (*m)[j], (*m)[i]
92 (*m)[i].index = i
93 (*m)[j].index = j
94 }
95
96 func (m *minHeap[T]) Push(item interface{}) {
97 i := item.(*priorityItem[T])
98 i.index = len(*m)
99 *m = append(*m, i)
100 }
101
102 func (m *minHeap[T]) Pop() interface{} {
103 old := *m
104 item := old[len(old)-1]
105 *m = old[:len(old)-1]
106
107 return item
108 }
109
110 type stack[T any] interface {
111 push(T)
112 pop() (T, error)
113 top() (T, error)
114 isEmpty() bool
115 // forEach iterate the stack from bottom to top
116 forEach(func(T))
117 }
118
119 func newStack[T any]() stack[T] {
120 return &stackImpl[T]{
121 elements: make([]T, 0),
122 }
123 }
124
125 type stackImpl[T any] struct {
126 elements []T
127 }
128
129 func (s *stackImpl[T]) push(t T) {
130 s.elements = append(s.elements, t)
131 }
132
133 func (s *stackImpl[T]) pop() (T, error) {
134 e, err := s.top()
135 if err != nil {
136 var defaultValue T
137 return defaultValue, err
138 }
139
140 s.elements = s.elements[:len(s.elements)-1]
141 return e, nil
142 }
143
144 func (s *stackImpl[T]) top() (T, error) {
145 if s.isEmpty() {
146 var defaultValue T
147 return defaultValue, errors.New("no element in stack")
148 }
149
150 return s.elements[len(s.elements)-1], nil
151 }
152
153 func (s *stackImpl[T]) isEmpty() bool {
154 return len(s.elements) == 0
155 }
156
157 func (s *stackImpl[T]) forEach(f func(T)) {
158 for _, e := range s.elements {
159 f(e)
160 }
161 }