Skip to content

kyroy/priority-queue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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
}

About

A priority queue implementation in Golang.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages