-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap.go
More file actions
32 lines (26 loc) · 695 Bytes
/
heap.go
File metadata and controls
32 lines (26 loc) · 695 Bytes
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
package priorityqueue
// implements heap.Interface
type heap[T any] struct {
elements []T
less func(T, T) bool
}
func (h *heap[T]) Len() int {
return len(h.elements)
}
func (h *heap[T]) Push(x any) {
h.elements = append(h.elements, x.(T))
}
func (h *heap[T]) Less(i, j int) bool {
return h.less(h.elements[i], h.elements[j])
}
// Pop is called after the first element is swapped with the last
// so return the last element and resize the slice
func (h *heap[T]) Pop() any {
last := len(h.elements) - 1
element := h.elements[last]
h.elements = h.elements[:last]
return element
}
func (h *heap[T]) Swap(i, j int) {
h.elements[i], h.elements[j] = h.elements[j], h.elements[i]
}