pq

package module
v0.0.0-...-2ad50c3 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2015 License: MIT Imports: 2 Imported by: 0

README

pq

Heap based Priority Queue. Based on the containers/heap standard library.

Usage

package main

import (
    "fmt"
    "github.com/libds/pq"
)

func main() {
    p := pq.NewMin()

    p.Push(&Item{
        Value: "build:src",
        Priority: 5,
    })

    p.Push(&Item{
        Value: "test:run",
        Priority: 15,
    })

    job := p.Pop()
    fmt.Println(job) // {Value: test:run, Priority: 15, Index: 0}

    item := p.Peek()
    fmt.Println(item.Value) // build:src

    fmt.Println(p.Size()) // 1

}

API Docs

https://godoc.org/github.com/libds/pq

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item struct {
	Index    int
	Priority int
	Value    interface{}
}

All nodes in the priority queue are added as an Item instance

func (*Item) String

func (item *Item) String() string

type MaxPQ

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

func NewMax

func NewMax() *MaxPQ

Initialize a Heap Maximum Priority Queue

func (*MaxPQ) Peek

func (p *MaxPQ) Peek() *Item

Returns the maximum item in the queue without removing it

The complexity is O(1)

func (*MaxPQ) Pop

func (p *MaxPQ) Pop() *Item

Pop removes the maximum element from the heap and returns it.

The complexity is O(log(n)) where n = p.Size().

func (*MaxPQ) Push

func (p *MaxPQ) Push(value interface{}, priority int)

Push pushes the element x onto the heap.

The complexity is O(log(n)) where n = p.Size().

func (*MaxPQ) Size

func (p *MaxPQ) Size() int

Returns the amount of items in the queue.

Complexity = O(1)

func (*MaxPQ) Update

func (pq *MaxPQ) Update(item *Item, value interface{}, priority int)

Update modifies the priority and value of an Item in the queue.

The Complexity is O(1) if you only update the value. And O(log(n)) if you change the priority, where n = p.Size()

type MinPQ

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

func NewMin

func NewMin() *MinPQ

Initialize a Heap Minimum Priority Queue

func (*MinPQ) Peek

func (p *MinPQ) Peek() *Item

Returns the minimum item in the queue without removing it

The complexity is O(1)

func (*MinPQ) Pop

func (p *MinPQ) Pop() *Item

Pop removes the minimum element from the heap and returns it.

The complexity is O(log(n)) where n = p.Size().

func (*MinPQ) Push

func (p *MinPQ) Push(value interface{}, priority int)

Push pushes the element x onto the heap.

The complexity is O(log(n)) where n = p.Size().

func (*MinPQ) Size

func (p *MinPQ) Size() int

Returns the amount of items in the queue.

Complexity = O(1)

func (*MinPQ) Update

func (p *MinPQ) Update(item *Item, value interface{}, priority int)

update modifies the priority and value of an Item in the queue.

The Complexity is O(1) if you only update the value. And O(log(n)) if you change the priority, where n = p.Size()

Jump to

Keyboard shortcuts

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