7 type Comparable interface {
8 Less(o interface{}) bool
11 //-----------------------------------------------------------------------------
17 h.Push(String("msg1"), 1)
18 h.Push(String("msg3"), 3)
19 h.Push(String("msg2"), 2)
30 func NewHeap() *Heap {
31 return &Heap{pq: make([]*pqItem, 0)}
34 func (h *Heap) Len() int64 {
35 return int64(len(h.pq))
38 func (h *Heap) Push(value interface{}, priority Comparable) {
39 heap.Push(&h.pq, &pqItem{value: value, priority: priority})
42 func (h *Heap) Peek() interface{} {
49 func (h *Heap) Update(value interface{}, priority Comparable) {
50 h.pq.Update(h.pq[0], value, priority)
53 func (h *Heap) Pop() interface{} {
54 item := heap.Pop(&h.pq).(*pqItem)
58 //-----------------------------------------------------------------------------
60 ///////////////////////
61 // From: http://golang.org/pkg/container/heap/#example__priorityQueue
69 type priorityQueue []*pqItem
71 func (pq priorityQueue) Len() int { return len(pq) }
73 func (pq priorityQueue) Less(i, j int) bool {
74 return pq[i].priority.Less(pq[j].priority)
77 func (pq priorityQueue) Swap(i, j int) {
78 pq[i], pq[j] = pq[j], pq[i]
83 func (pq *priorityQueue) Push(x interface{}) {
87 *pq = append(*pq, item)
90 func (pq *priorityQueue) Pop() interface{} {
94 item.index = -1 // for safety
99 func (pq *priorityQueue) Update(item *pqItem, value interface{}, priority Comparable) {
101 item.priority = priority
102 heap.Fix(pq, item.index)