Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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 main

import (
"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))
}
```