github.com/LdDl/ch@v1.7.8/heap_min.go (about) 1 package ch 2 3 import "container/heap" 4 5 type minHeapVertex struct { 6 id int64 7 distance float64 8 } 9 10 type minHeap []*minHeapVertex 11 12 func (h minHeap) Len() int { return len(h) } 13 func (h minHeap) Less(i, j int) bool { return h[i].distance < h[j].distance } // Min-Heap 14 func (h minHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } 15 16 func (h *minHeap) Push(x interface{}) { 17 *h = append(*h, x.(*minHeapVertex)) 18 } 19 20 func (h *minHeap) Pop() interface{} { 21 heapSize := len(*h) 22 lastNode := (*h)[heapSize-1] 23 *h = (*h)[0 : heapSize-1] 24 return lastNode 25 } 26 27 func (h *minHeap) add_with_priority(id int64, val float64) { 28 nds := &minHeapVertex{ 29 id: id, 30 distance: val, 31 } 32 heap.Push(h, nds) 33 }