Skip to content

dnaeon/go-priorityqueue

Repository files navigation

go-priorityqueue

Build Status Go Reference Go Report Card codecov

A simple, generic implementation of Priority Queue, based on 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.

go get -v gopkg.in/dnaeon/go-priorityqueue.v1

Usage

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 for additional examples.

Tests

Run the tests.

make test

License

go-priorityqueue is Open Source and licensed under the BSD License.

Credits

Some parts of go-priorityqueue re-use code from the examples in container/heap.

About

Simple and generic implementation of priority queues in Go

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •