An implementation of priority queue,ported from the beanstalkd
- Push/Pop item
- Remove item on anywhere
To start using pq
, install Go and run go get
:
$ go get -u https://github.com/x-debug/pq
This will retrieve the library.
You have to define the item structure first
type MinIntItem struct {
value int
}
func (item MinIntItem) Less(other Item) bool {
return item.value > other.(MinIntItem).value
}
Push item to PQ
pq := NewPriorityQueue()
pq.Push(MinIntItem{value: 3})
pq.Push(MinIntItem{value: 9})
pq.Push(MinIntItem{value: 1})
Pop item from PQ
item := pq.Pop().(MinIntItem)
Or remove item from PQ
item := pq.Remove(2).(MinIntItem)
more examples see pq_test.go
CMUQ 15-121 Priority Queues and Heaps
pq
source code is available under the MIT License.