structure/priority_queue/priority_queue.go

81 lines
1.7 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 NewIterator(pq.queue.top)
}
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)
}
func (pq *PriorityQueue) IndexNode(idx int) (*Node, bool) {
n := pq.queue.indexNode(idx)
return n, n != nil
}
2019-04-07 18:11:37 +00:00
func (pq *PriorityQueue) Get(key interface{}) (interface{}, bool) {
return pq.queue.Get(key)
}
func (pq *PriorityQueue) GetNode(key interface{}) (*Node, bool) {
return pq.queue.GetNode(key)
}
func (pq *PriorityQueue) GetAround(key interface{}) [3]interface{} {
return pq.queue.GetAround(key)
}
func (pq *PriorityQueue) GetAroundNode(key interface{}) [3]*Node {
return pq.queue.getArounNode(key)
}
func (pq *PriorityQueue) GetRange(k1, k2 interface{}) []interface{} {
return pq.queue.GetRange(k1, k2)
}
func (pq *PriorityQueue) RemoveIndex(idx int) (*Node, bool) {
2019-04-07 18:11:37 +00:00
return pq.queue.RemoveIndex(idx)
}
func (pq *PriorityQueue) Remove(key interface{}) (*Node, bool) {
2019-04-07 18:11:37 +00:00
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
}