github.com/qiuhoude/go-web@v0.0.0-20220223060959-ab545e78f20d/algorithm/datastructures/queue/priority_queue.go (about)

     1  package queue
     2  
     3  import "github.com/qiuhoude/go-web/algorithm/datastructures/minheap"
     4  
     5  // 优先级队列
     6  type PriorityQueue struct {
     7  	heap *minheap.Heap
     8  }
     9  
    10  func NewPriorityQueue(f minheap.CompareFunc) *PriorityQueue {
    11  	return &PriorityQueue{heap: minheap.NewHeap(f)}
    12  }
    13  
    14  func (q *PriorityQueue) Enqueue(v interface{}) bool {
    15  	q.heap.Add(v)
    16  	return true
    17  }
    18  
    19  func (q *PriorityQueue) Dequeue() interface{} {
    20  	return q.heap.Poll()
    21  }
    22  
    23  func (q *PriorityQueue) IsEmpty() bool {
    24  	return q.heap.Len() == 0
    25  }
    26  
    27  func (q *PriorityQueue) Len() int {
    28  	return q.heap.Len()
    29  }
    30  
    31  // 移除对应的值
    32  func (q *PriorityQueue) Remove(e interface{}, eqFunc func(e, b interface{}) bool) interface{} {
    33  	return q.heap.Remove(e, eqFunc)
    34  }