https://github.com/kyroy/priority-queue
A priority queue implementation in Golang.
https://github.com/kyroy/priority-queue
go golang library priority priority-queue queue
Last synced: 3 months ago
JSON representation
A priority queue implementation in Golang.
- Host: GitHub
- URL: https://github.com/kyroy/priority-queue
- Owner: kyroy
- License: apache-2.0
- Created: 2018-03-24T22:21:32.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-31T03:24:55.000Z (about 8 years ago)
- Last Synced: 2024-06-20T12:14:58.011Z (almost 2 years ago)
- Topics: go, golang, library, priority, priority-queue, queue
- Language: Go
- Homepage:
- Size: 8.79 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# priority-queue
[](http://godoc.org/github.com/Kyroy/priority-queue)
[](https://jenkins.kyroy.com/job/github.com-kyroy/job/priority-queue/job/master/)
[](https://jenkins.kyroy.com/job/github.com-kyroy/job/priority-queue/job/master/)
[](https://jenkins.kyroy.com/job/github.com-kyroy/job/priority-queue/job/master/)
[](https://goreportcard.com/report/github.com/kyroy/priority-queue)
[](https://github.com/Kyroy/priority-queue/blob/master/LICENSE)
A [priority queue](https://en.wikipedia.org/wiki/Priority_queue) implementation in golang where every element in the queue can be accessed by its index in constant time.
This is achieved by using Golang's [sort](https://golang.org/pkg/sort/) package.
## Usage
```bash
go get github.com/kyroy/priority-queue
```
```go
import "github.com/kyroy/priority-queue"
````
```go
type Data struct {
value int
}
func main() {
queue := pq.NewPriorityQueue()
// Insert
queue.Insert(Data{value: 7}, 3.5)
queue.Insert(Data{value: 7}, 5.)
queue.Insert(Data{value: 123}, 2.5)
fmt.Println("len", queue.Len()) // len 3
// Get
for i := 0; i < queue.Len(); i++ {
d, prio := queue.Get(i)
fmt.Println("get", i, d, prio)
}
// get 0 {123} 2.5
// get 1 {7} 3.5
// get 2 {7} 5
// Pop...
d := queue.PopLowest().(Data)
fmt.Println("d", d) // d {123}
d = queue.PopHighest().(Data)
fmt.Println("d", d) // d {7}
// Len
fmt.Println("len", queue.Len()) // len 1
}
```
### WithMinPrioSize
*Note: Equivalend for `WithMaxPrioSize`*
```go
type Data struct {
value int
}
func main() {
queue := pq.NewPriorityQueue(pq.WithMinPrioSize(2))
// Insert
queue.Insert(Data{value: 7}, 3.5)
queue.Insert(Data{value: 7}, 5.)
queue.Insert(Data{value: 123}, 2.5)
fmt.Println("len", queue.Len()) // len 2
// Get
for i := 0; i < queue.Len(); i++ {
d, prio := queue.Get(i)
fmt.Println("get", i, d, prio)
}
// get 0 {123} 2.5
// get 1 {7} 3.5
// Pop...
d := queue.PopLowest().(Data)
fmt.Println("d", d) // d {123}
d = queue.PopHighest().(Data)
fmt.Println("d", d) // d {7}
// Len
fmt.Println("len", queue.Len()) // len 0
}
```