2019-03-12 02:54:52 +00:00
|
|
|
package pqueue
|
|
|
|
|
2019-04-08 10:47:12 +00:00
|
|
|
import "474420502.top/eson/structure/compare"
|
2019-03-16 17:41:07 +00:00
|
|
|
|
2019-03-12 02:54:52 +00:00
|
|
|
type PriorityQueue struct {
|
2019-03-24 17:40:12 +00:00
|
|
|
queue *vbTree
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pq *PriorityQueue) Iterator() *Iterator {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(Compare compare.Compare) *PriorityQueue {
|
|
|
|
return &PriorityQueue{queue: newVBT(Compare)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pq *PriorityQueue) Size() int {
|
|
|
|
return pq.queue.Size()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pq *PriorityQueue) Push(value interface{}) {
|
2019-04-07 18:11:37 +00:00
|
|
|
pq.queue.Put(value)
|
2019-03-24 17:40:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pq *PriorityQueue) Top() (result interface{}, ok bool) {
|
2019-04-07 18:11:37 +00:00
|
|
|
if pq.queue.top != nil {
|
|
|
|
return pq.queue.top.value, true
|
2019-03-24 17:40:12 +00:00
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pq *PriorityQueue) Pop() (result interface{}, ok bool) {
|
2019-04-07 18:11:37 +00:00
|
|
|
if pq.queue.top != nil {
|
|
|
|
result = pq.queue.top.value
|
|
|
|
pq.queue.removeNode(pq.queue.top)
|
|
|
|
return result, true
|
|
|
|
}
|
2019-03-24 17:40:12 +00:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2019-04-07 18:11:37 +00:00
|
|
|
func (pq *PriorityQueue) Index(idx int) (interface{}, bool) {
|
|
|
|
return pq.queue.Index(idx)
|
2019-03-24 17:40:12 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 18:11:37 +00:00
|
|
|
func (pq *PriorityQueue) Get(key interface{}) (interface{}, bool) {
|
|
|
|
return pq.queue.Get(key)
|
2019-03-24 17:40:12 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 18:11:37 +00:00
|
|
|
func (pq *PriorityQueue) RemoveWithIndex(idx int) bool {
|
|
|
|
return pq.queue.RemoveIndex(idx)
|
|
|
|
}
|
2019-03-24 17:40:12 +00:00
|
|
|
|
2019-04-07 18:11:37 +00:00
|
|
|
func (pq *PriorityQueue) Remove(key interface{}) bool {
|
|
|
|
return pq.queue.Remove(key)
|
2019-03-24 17:40:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pq *PriorityQueue) Values() []interface{} {
|
2019-04-07 18:11:37 +00:00
|
|
|
return pq.queue.Values()
|
2019-03-12 02:54:52 +00:00
|
|
|
}
|