https://github.com/dnaeon/go-priorityqueue
Simple and generic implementation of priority queues in Go
https://github.com/dnaeon/go-priorityqueue
Last synced: 9 months ago
JSON representation
Simple and generic implementation of priority queues in Go
- Host: GitHub
- URL: https://github.com/dnaeon/go-priorityqueue
- Owner: dnaeon
- License: bsd-2-clause
- Created: 2023-10-16T14:34:07.000Z (about 2 years ago)
- Default Branch: v1
- Last Pushed: 2023-11-12T08:44:25.000Z (about 2 years ago)
- Last Synced: 2025-03-27T05:41:37.220Z (10 months ago)
- Language: Go
- Size: 9.77 KB
- Stars: 30
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-priorityqueue
[](https://github.com/dnaeon/go-priorityqueue/actions/workflows/test.yaml/badge.svg)
[](https://pkg.go.dev/gopkg.in/dnaeon/go-priorityqueue.v1)
[](https://goreportcard.com/report/gopkg.in/dnaeon/go-priorityqueue.v1)
[](https://codecov.io/gh/dnaeon/go-priorityqueue)
A simple, generic implementation of [Priority
Queue](https://en.wikipedia.org/wiki/Priority_queue), based on
[container/heap](https://pkg.go.dev/container/heap).
This package is built on top of the functionality already provided by
`container/heap`, and also adds various convenience methods for
creating new priority queues, predicates for testing whether the queue
is empty, synchronization so it can be safely used by multiple
goroutines.
## Installation
Execute the following command.
``` shell
go get -v gopkg.in/dnaeon/go-priorityqueue.v1
```
## Usage
``` go
package main
import (
"fmt"
pq "gopkg.in/dnaeon/go-priorityqueue.v1"
)
func main() {
// Create a new priority queue
queue := pq.New[string, int64](pq.MinHeap)
// Insert items in the queue
queue.Put("apple", 10)
queue.Put("banana", 3)
queue.Put("pear", 20)
queue.Put("orange", 15)
// Update priority of an item
queue.Update("banana", 42)
for !queue.IsEmpty() {
item := queue.Get()
fmt.Printf("%s: %d\n", item.Value, item.Priority)
}
// Output:
// apple: 10
// orange: 15
// pear: 20
// banana: 42
}
```
Make sure to check the included [test cases](./priority_queue_test.go) for
additional examples.
## Tests
Run the tests.
``` shell
make test
```
## License
`go-priorityqueue` is Open Source and licensed under the [BSD
License](http://opensource.org/licenses/BSD-2-Clause).
## Credits
Some parts of `go-priorityqueue` re-use code from the examples in
[container/heap](https://pkg.go.dev/container/heap#example-package-PriorityQueue).