type Interface interface { sort.Interface Push(x any) // add x as element Len(),注意这里不是heap的Push Pop() any // remove and return element Len() - 1.,注意这里不是heap的Pop }
其中sort.Interface为排序接口,具体如下
1 2 3 4 5 6 7 8 9 10 11
type Interface interface { // Len is the number of elements in the collection. Len() int
// Less reports whether the element with index i // must sort before the element with index j. Less(i, j int) bool
// Swap swaps the elements with indexes i and j. Swap(i, j int) }
func(h *IntHeap) Push(x any) { // Push and Pop use pointer receivers because they modify the slice's length, // not just its contents. *h = append(*h, x.(int)) }
func(h *IntHeap) Pop() any { old := *h n := len(old) x := old[n-1] *h = old[0 : n-1] return x }