x-debug / pq

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PriorityQueue

An implementation of priority queue,ported from the beanstalkd

Features

  • Push/Pop item
  • Remove item on anywhere

Installing

To start using pq, install Go and run go get:

$ go get -u https://github.com/x-debug/pq

This will retrieve the library.

Example

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

References

CMUQ 15-121 Priority Queues and Heaps

License

pq source code is available under the MIT License.

About

License:MIT License


Languages

Language:Go 100.0%