56 lines
1.0 KiB
Go
56 lines
1.0 KiB
Go
package main
|
|
|
|
import (
|
|
"sync"
|
|
|
|
pqueuekey "github.com/474420502/focus/priority_queuekey"
|
|
)
|
|
|
|
// Queue 存储队列
|
|
type Queue struct {
|
|
queue *pqueuekey.PriorityQueue
|
|
lock sync.Mutex
|
|
}
|
|
|
|
// NewQueue 创建队列
|
|
func NewQueue() *Queue {
|
|
q := &Queue{}
|
|
q.queue = pqueuekey.New(CompareTaskID)
|
|
return q
|
|
}
|
|
|
|
// Get 获取该值是否存在
|
|
func (q *Queue) Get(key interface{}) (result interface{}, ok bool) {
|
|
q.lock.Lock()
|
|
defer q.lock.Unlock()
|
|
return q.queue.Get(key)
|
|
}
|
|
|
|
// Push 入队
|
|
func (q *Queue) Push(key interface{}, value interface{}) {
|
|
q.lock.Lock()
|
|
defer q.lock.Unlock()
|
|
q.queue.Push(key, value)
|
|
}
|
|
|
|
// Pop 出队
|
|
func (q *Queue) Pop() (result interface{}, ok bool) {
|
|
q.lock.Lock()
|
|
defer q.lock.Unlock()
|
|
return q.queue.Pop()
|
|
}
|
|
|
|
// Remove 删除指定key
|
|
func (q *Queue) Remove(key interface{}) (result interface{}, ok bool) {
|
|
q.lock.Lock()
|
|
defer q.lock.Unlock()
|
|
return q.queue.Remove(key)
|
|
}
|
|
|
|
// Top 队头
|
|
func (q *Queue) Top() (result interface{}, ok bool) {
|
|
q.lock.Lock()
|
|
defer q.lock.Unlock()
|
|
return q.queue.Top()
|
|
}
|