2023-03-04 05:18:31 +00:00
|
|
|
// Package binheap implements a non-concurrent binary max-heap.
|
2023-03-03 05:35:17 +00:00
|
|
|
//
|
|
|
|
// # Implementation
|
|
|
|
//
|
|
|
|
// [H] is parameterized over two types, one for the priority levels, one for
|
|
|
|
// the elements. Internally, there are two equally-sized buffers for these
|
|
|
|
// types. Re-heaping operations swap corresponding entries in these buffers
|
|
|
|
// in lock-step.
|
2023-03-02 03:22:37 +00:00
|
|
|
package binheap
|
|
|
|
|
|
|
|
import "golang.org/x/exp/constraints"
|
|
|
|
|
2023-03-02 09:53:12 +00:00
|
|
|
// H is a binary max-heap.
|
2023-03-02 03:22:37 +00:00
|
|
|
//
|
2023-03-03 05:35:17 +00:00
|
|
|
// `P` is the type of the priority levels, and `E` the type of the elements.
|
|
|
|
type H[P constraints.Ordered, E any] struct {
|
|
|
|
prs []P
|
|
|
|
els []E
|
|
|
|
len int
|
2023-03-02 03:22:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make creates a new heap.
|
2023-03-03 05:35:17 +00:00
|
|
|
func Make[P constraints.Ordered, E any](cap int) H[P, E] {
|
|
|
|
priorities := make([]P, cap)
|
|
|
|
elements := make([]E, cap)
|
|
|
|
h := H[P, E]{prs: priorities, els: elements}
|
2023-03-02 03:22:37 +00:00
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
// Capacity returns the total capacity of the heap.
|
2023-03-03 05:35:17 +00:00
|
|
|
func (h *H[P, E]) Capacity() int {
|
|
|
|
return cap(h.prs)
|
2023-03-02 03:22:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the number of items in the heap.
|
2023-03-03 05:35:17 +00:00
|
|
|
func (h *H[P, E]) Len() int {
|
2023-03-02 03:22:37 +00:00
|
|
|
return h.len
|
|
|
|
}
|
|
|
|
|
|
|
|
// CanExtract returns true if the heap has any item, otherwise false.
|
2023-03-03 05:35:17 +00:00
|
|
|
func (h *H[P, E]) CanExtract() bool {
|
2023-03-02 03:22:37 +00:00
|
|
|
return h.len != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// CanInsert returns true if the heap has unused capacity, otherwise false.
|
2023-03-03 05:35:17 +00:00
|
|
|
func (h *H[P, E]) CanInsert() bool {
|
|
|
|
return cap(h.prs)-h.len != 0
|
2023-03-02 03:22:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Extract returns the current heap root, then performs a heap-down pass.
|
|
|
|
//
|
|
|
|
// If the heap is empty, it panics.
|
2023-03-03 05:35:17 +00:00
|
|
|
func (h *H[P, E]) Extract() (P, E) {
|
2023-03-02 03:22:37 +00:00
|
|
|
if !h.CanExtract() {
|
|
|
|
panic("heap is empty")
|
|
|
|
}
|
2023-03-03 05:35:17 +00:00
|
|
|
// extract root
|
|
|
|
priority := h.prs[0]
|
|
|
|
element := h.els[0]
|
|
|
|
// move last entry to root position
|
|
|
|
h.prs[0] = h.prs[h.len-1]
|
|
|
|
h.els[0] = h.els[h.len-1]
|
|
|
|
// clear the former last entry position,
|
|
|
|
// so as not to hold onto garbage
|
|
|
|
var emptyPriority P
|
2023-03-02 03:22:37 +00:00
|
|
|
var emptyElem E
|
2023-03-03 05:35:17 +00:00
|
|
|
h.prs[h.len-1] = emptyPriority
|
|
|
|
h.els[h.len-1] = emptyElem
|
|
|
|
// heap-down
|
2023-03-02 03:22:37 +00:00
|
|
|
h.len--
|
|
|
|
idx := 0
|
|
|
|
for {
|
2023-03-03 05:35:17 +00:00
|
|
|
left := idx<<1 + 1
|
|
|
|
right := idx<<1 + 2
|
2023-03-02 03:22:37 +00:00
|
|
|
largest := idx
|
2023-03-03 05:35:17 +00:00
|
|
|
if left < h.len && h.prs[left] > h.prs[largest] {
|
2023-03-02 03:22:37 +00:00
|
|
|
largest = left
|
|
|
|
}
|
2023-03-03 05:35:17 +00:00
|
|
|
if right < h.len && h.prs[right] > h.prs[largest] {
|
2023-03-02 03:22:37 +00:00
|
|
|
largest = right
|
|
|
|
}
|
|
|
|
if largest == idx {
|
|
|
|
break
|
|
|
|
}
|
2023-03-03 05:35:17 +00:00
|
|
|
h.prs[idx], h.prs[largest] = h.prs[largest], h.prs[idx]
|
|
|
|
h.els[idx], h.els[largest] = h.els[largest], h.els[idx]
|
2023-03-02 03:22:37 +00:00
|
|
|
idx = largest
|
|
|
|
}
|
|
|
|
|
2023-03-03 05:35:17 +00:00
|
|
|
return priority, element
|
2023-03-02 03:22:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert adds an item to the heap, then performs a heap-up pass.
|
|
|
|
//
|
|
|
|
// If the heap is full, it panics.
|
2023-03-03 05:35:17 +00:00
|
|
|
func (h *H[P, E]) Insert(priority P, elem E) {
|
2023-03-02 03:22:37 +00:00
|
|
|
if !h.CanInsert() {
|
|
|
|
panic("heap is full")
|
|
|
|
}
|
2023-03-03 05:35:17 +00:00
|
|
|
// insert new item into last position
|
2023-03-02 03:22:37 +00:00
|
|
|
idx := h.len
|
2023-03-03 05:35:17 +00:00
|
|
|
h.prs[idx] = priority
|
|
|
|
h.els[idx] = elem
|
|
|
|
// heap-up
|
2023-03-02 03:22:37 +00:00
|
|
|
h.len++
|
|
|
|
for {
|
2023-03-03 05:35:17 +00:00
|
|
|
parent := (idx - 1) >> 1
|
|
|
|
if parent < 0 || h.prs[parent] >= h.prs[idx] {
|
2023-03-02 03:22:37 +00:00
|
|
|
break
|
|
|
|
}
|
2023-03-03 05:35:17 +00:00
|
|
|
h.prs[parent], h.prs[idx] = h.prs[idx], h.prs[parent]
|
|
|
|
h.els[parent], h.els[idx] = h.els[idx], h.els[parent]
|
2023-03-02 03:22:37 +00:00
|
|
|
idx = parent
|
|
|
|
}
|
|
|
|
}
|