structure/priority_queue/priority_queue.go

60 lines
1.2 KiB
Go
Raw Normal View History

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 {
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)
}
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
}
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
}
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-04-07 18:11:37 +00:00
func (pq *PriorityQueue) Get(key interface{}) (interface{}, bool) {
return pq.queue.Get(key)
}
2019-04-07 18:11:37 +00:00
func (pq *PriorityQueue) RemoveWithIndex(idx int) bool {
return pq.queue.RemoveIndex(idx)
}
2019-04-07 18:11:37 +00:00
func (pq *PriorityQueue) Remove(key interface{}) bool {
return pq.queue.Remove(key)
}
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
}