Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kseo/prioqueue
Binary-heap based priority queue for Go
https://github.com/kseo/prioqueue
Last synced: 26 days ago
JSON representation
Binary-heap based priority queue for Go
- Host: GitHub
- URL: https://github.com/kseo/prioqueue
- Owner: kseo
- License: bsd-3-clause
- Created: 2016-10-13T14:47:35.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2016-10-14T16:22:35.000Z (about 8 years ago)
- Last Synced: 2024-06-20T14:25:54.887Z (5 months ago)
- Language: Go
- Homepage: https://godoc.org/github.com/kseo/prioqueue
- Size: 13.7 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
priority-queue
==============
[![Build Status](https://travis-ci.org/kseo/prioqueue.svg?branch=master)](https://travis-ci.org/kseo/prioqueue)
[![GoDoc](https://godoc.org/github.com/kseo/prioqueue?status.svg)](https://godoc.org/github.com/kseo/prioqueue)Binary-heap based priority queue for Go. Elements are insrted using `Add`, and the
maximum element is removed using `RemoveMax`, observed with `Peek`.## Example
```go
package mainimport (
"fmt""github.com/kseo/prioqueue"
)func intCmpFunc(a, b interface{}) int {
if a.(int) < b.(int) {
return -1
} else if a.(int) > b.(int) {
return 1
} // else
return 0
}func sort(xs []int) []int {
var result []int
elements := make([]interface{}, len(xs))
for i, d := range xs {
elements[i] = d
}
pq := prioqueue.NewPriorityQueue(elements, intCmpFunc)
for !pq.IsEmpty() {
max, _ := pq.RemoveMax()
result = append(result, max.(int))
}
return result
}func main() {
xs := []int{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9}
fmt.Printf("%v\n", sort(xs))
}
```