priority

package
v0.0.0-...-a268b5b Latest Latest
Warning

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

Go to latest
Published: May 2, 2022 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package priority implements a PriorityQueue

From: https://pkg.go.dev/container/heap@go1.17.6#example-package-PriorityQueue

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item struct {
	Value    string // The Value of the item; arbitrary.
	Priority int    // The Priority of the item in the queue.
	// The Index is needed by Update and is maintained by the Priority.Interface methods.
	Index int // The Index of the item in the Priority.
}

An Item is something we manage in a Priority queue.

type Queue

type Queue []*Item

A Queue implements Priority.Interface and holds Items.

A Queue with some items, adds and manipulates an item, and then removes the items in Priority order.

Example:

// Some items and their priorities.
items := map[string]int{
    "banana": 3, "apple": 2, "pear": 4,
}

// Create a Priority queue, put the items in it, and
// establish the Priority queue (Priority) invariants.
pq := make(Queue, len(items))
i := 0
for Value, Priority := range items {
    pq[i] = &Item{
        Value:    Value,
        Priority: Priority,
        Index:    i,
    }
    i++
}
Priority.Init(&pq)

// Insert a new item and then modify its Priority.
item := &Item{
    Value:    "orange",
    Priority: 1,
}
Priority.Push(&pq, item)
pq.Update(item, item.Value, 5)

// Take the items out; they arrive in decreasing Priority order.
for pq.Len() > 0 {
    item := Priority.Pop(&pq).(*Item)
    fmt.Printf("%.2d:%s ", item.Priority, item.Value)
}

From: https://pkg.go.dev/container/heap@go1.17.6#example-package-PriorityQueue

func (Queue) Len

func (pq Queue) Len() int

func (Queue) Less

func (pq Queue) Less(i, j int) bool

func (*Queue) Pop

func (pq *Queue) Pop() interface{}

func (*Queue) PopValue

func (pq *Queue) PopValue() string

func (*Queue) Push

func (pq *Queue) Push(x interface{})

func (*Queue) PushValue

func (pq *Queue) PushValue(value string) *Item

func (Queue) Swap

func (pq Queue) Swap(i, j int)

func (*Queue) Update

func (pq *Queue) Update(item *Item, value string, priority int)

Update modifies the Priority and Value of an Item in the queue.

func (*Queue) UpdatePriority

func (pq *Queue) UpdatePriority(item *Item, priority int)

Jump to

Keyboard shortcuts

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