pq

package module
v0.0.0-...-6e21825 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2018 License: Apache-2.0 Imports: 1 Imported by: 10 modules, 10 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

priority-queue

Documentation Build Status Jenkins tests Jenkins coverage Go Report Card License

A priority queue implementation in golang where every element in the queue can be accessed by its index in constant time. This is achieved by using Golang's sort package.

Usage

go get github.com/kyroy/priority-queue
import "github.com/kyroy/priority-queue"
type Data struct {
	value int
}

func main() {
    queue := pq.NewPriorityQueue()
    
    // Insert
    queue.Insert(Data{value: 7}, 3.5)
    queue.Insert(Data{value: 7}, 5.)
    queue.Insert(Data{value: 123}, 2.5)
    
    fmt.Println("len", queue.Len()) // len 3
    
    // Get
    for i := 0; i < queue.Len(); i++ {
    	d, prio := queue.Get(i)
    	fmt.Println("get", i, d, prio)
    }
    // get 0 {123} 2.5
    // get 1 {7} 3.5
    // get 2 {7} 5
    
    // Pop...
    d := queue.PopLowest().(Data)
    fmt.Println("d", d) // d {123}
    d = queue.PopHighest().(Data)
    fmt.Println("d", d) // d {7}
    
    // Len
    fmt.Println("len", queue.Len()) // len 1
}
WithMinPrioSize

Note: Equivalend for WithMaxPrioSize

type Data struct {
	value int
}

func main() {
    queue := pq.NewPriorityQueue(pq.WithMinPrioSize(2))
    
    // Insert
    queue.Insert(Data{value: 7}, 3.5)
    queue.Insert(Data{value: 7}, 5.)
    queue.Insert(Data{value: 123}, 2.5)
    
    fmt.Println("len", queue.Len()) // len 2
    
    // Get
    for i := 0; i < queue.Len(); i++ {
    	d, prio := queue.Get(i)
    	fmt.Println("get", i, d, prio)
    }
    // get 0 {123} 2.5
    // get 1 {7} 3.5
    
    // Pop...
    d := queue.PopLowest().(Data)
    fmt.Println("d", d) // d {123}
    d = queue.PopHighest().(Data)
    fmt.Println("d", d) // d {7}
    
    // Len
    fmt.Println("len", queue.Len()) // len 0
}

Documentation

Overview

Package pq implements a priority queue data structure on top of golang's sort.Interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option struct {
	// contains filtered or unexported fields
}

Option represents the option

func WithMaxPrioSize

func WithMaxPrioSize(size int) Option

WithMaxPrioSize limits the size of the priority queue to 'size' by keeping the elements with the highest priorities.

func WithMinPrioSize

func WithMinPrioSize(size int) Option

WithMinPrioSize limits the size of the priority queue to 'size' by keeping the elements with the lowest priorities.

type PriorityQueue

type PriorityQueue struct {
	// contains filtered or unexported fields
}

PriorityQueue represents the queue

func NewPriorityQueue

func NewPriorityQueue(options ...Option) *PriorityQueue

NewPriorityQueue returns an initialized PriorityQueue with the given options

func (*PriorityQueue) Get

func (p *PriorityQueue) Get(i int) (interface{}, float64)

Get returns the element with the i-th priority (from low to high). So the element with the lowest priority has the index 0. And the element with the highest priority has the index queue.Len()-1.

func (*PriorityQueue) Insert

func (p *PriorityQueue) Insert(v interface{}, priority float64)

Insert inserts a new element into the queue.

func (*PriorityQueue) Len

func (p *PriorityQueue) Len() int

Len returns the number of elements in the queue.

func (*PriorityQueue) PopHighest

func (p *PriorityQueue) PopHighest() interface{}

PopHighest removes the element with the highest priority from the queue and returns it. If the queue is empty, nil is returned.

func (*PriorityQueue) PopLowest

func (p *PriorityQueue) PopLowest() interface{}

PopLowest removes the element with the lowest priority from the queue and returns it. If the queue is empty, nil is returned.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址