package minheapimporttype entry[, any] struct { key value }// A Heap is a minimum heap ordered by key.typeHeap[ cmp.Ordered, any] []entry[, ]func ( Heap[, ]) () int { returnlen() }func ( Heap[, ]) () bool { returnlen() == 0 }// Peek returns the smallest element in the heap.// It must not be called when the heap is empty.func ( Heap[, ]) () (, ) { := [0]return .key, .value}// Push adds an element to the heap.func ( *Heap[, ]) ( , ) { * = append(*, entry[, ]{key: , value: }) (*).up(len(*) - 1)}// Pop removes and returns the smallest element in the heap.// It must not be called when the heap is empty.func ( *Heap[, ]) () (, ) { := * := len() - 1 := [0] [0] = []varentry[, ] [] = = [:]if > 0 { .down(0) } * = return .key, .value}// Clear removes all elements from the heap.func ( *Heap[, ]) () {clear(*) * = (*)[:0]}func ( Heap[, ]) ( int) {for > 0 { := ( - 1) / 2if [].key <= [].key {return } [], [] = [], [] = }}func ( Heap[, ]) ( int) {for { := 2* + 1if >= len() {break }if := + 1; < len() && [].key < [].key { = }if [].key <= [].key {break } [], [] = [], [] = }}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.