algs

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2023 License: MIT Imports: 4 Imported by: 0

README

pkg说明

algs

主要提供优先级队列的功能。通过algs.GetPQ()创建队列。目前队列元素只支持string类型,使用方式如下所示,priority值越小,优先级越高:

pq := algs.GetPQ()
	item2 := algs.GetItem("test2", 2)
	item1 := algs.GetItem("test1", 1)
	item3 := algs.GetItem("test3", 3)
	item0 := algs.GetItem("test0", 0)
	pq.Push(item2)
	pq.Push(item1)
	pq.Push(item3)
	pq.Push(item0)
	for pq.Len() > 0 {
		item, ok := pq.PopItem()
		fmt.Printf("item: %#v,ok: %v\n", item, ok)
	}
item: &algs.Item{value:"test0", priority:0, index:-1},ok: true
item: &algs.Item{value:"test1", priority:1, index:-1},ok: true
item: &algs.Item{value:"test2", priority:2, index:-1},ok: true
item: &algs.Item{value:"test3", priority:3, index:-1},ok: true

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Comparable

func Comparable(i interface{}) bool

func FirstNotEmpty

func FirstNotEmpty(strs ...string) string

func RandStr

func RandStr(length int, withNum bool) string

Types

type Item

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

An Item is something we manage in a priority queue.

func NewItem

func NewItem(value string, priority int) *Item

func (Item) Value

func (i Item) Value() string

type PriorityQueue

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

PriorityQueue wrapper

func NewPQ

func NewPQ() *PriorityQueue

func (*PriorityQueue) Len

func (p *PriorityQueue) Len() int

func (*PriorityQueue) Pop

func (p *PriorityQueue) Pop() (*Item, bool)

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

func (*PriorityQueue) Push

func (p *PriorityQueue) Push(item *Item)

func (*PriorityQueue) Update

func (p *PriorityQueue) Update(item *Item, value string, priority int)

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

Jump to

Keyboard shortcuts

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