PriorityQueues
A collection of generic and modular priority queue implementations in Go.
This repository provides four distinct priority queue strategies, each targeting a specific use case or trade-off between simplicity, flexibility, and key-based access.
π¦ Packages Overview
| Package |
Key Support |
Priority Injection |
Stability |
Typical Use Case |
pqs |
β |
β (item is prio) |
β |
Simple int/float/string queue |
mpqs |
β |
β
|
β
|
Manual prio for structs |
kpqs |
β
|
β (prio from item) |
β
|
Tasks with embedded priority |
kmpqs |
β
|
β
|
β
|
Schedulers, process queues |
Each package is self-contained and independently tested.
β¨ Why multiple queues?
Different use cases require different queue behavior:
- Minimal overhead? Use
pqs.
- Custom priority from a field? Use
kpqs.
- Keyed access with externally determined priority? Use
kmpqs.
- Just need control over the comparator? Use
mpqs.
π Structure
priorityqueues/
βββ kmpqs/ // keyed + manual prio
βββ kpqs/ // keyed + prio from item
βββ mpqs/ // manual prio only
βββ pqs/ // basic queue
Each directory contains:
*.go β package implementation
*_test.go β unit tests and examples
README.md β (optional) package-specific usage and design notes
β
Features
- Go 1.18+ generic support
container/heap under the hood
- Stable priority resolution with tie-breaking by insertion order
- Optional key-based lookup (
kmpqs, kpqs)
- Custom comparator functions
π Getting Started
Install with:
go get github.com/byExist/priorityqueues
Import the package you need:
import "github.com/byExist/priorityqueues/kmpqs"
Then initialize and use:
q := kmpqs.New(
kmpqs.StableMinFirst[*Process, int],
func(p *Process) string { return p.PID },
)
kmpqs.Enqueue(q, &Process{PID: "123", Name: "nginx"}, 5)
π§ͺ Testing
Each package includes full test coverage and practical Example functions.
Run all tests with:
go test ./...
π See Also
π License
MIT License. See LICENSE for details.