funcheap_siftup(h []int, i int) { v := h[i] for i > 0 { p := (i - 1) / d_ary if h[p] <= v { break } h[i] = h[p] i = p } h[i] = v }
funcheap_siftdown(h []int, i int) { v := h[i] n := (len(h) + d_ary - 2) / d_ary for i < n { b := d_ary*i + 1 min := b for j := b + 1; j < b+d_ary && j < len(h); j++ { if h[j] < h[min] { min = j } } if h[min] >= v { break } h[i] = h[min] i = min } h[i] = v }
funcheap_push(h []int, x int) []int { h = append(h, x) heap_siftup(h, len(h)-1) return h }
funcheap_pop(h []int) ([]int, int) { x := h[0] h[0] = h[len(h)-1] h = h[:len(h)-1] iflen(h) > 1 { heap_siftdown(h, 0) } return h, x }