prioritize

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2023 License: MIT Imports: 2 Imported by: 0

README

go-prioritize

Priority Queue for Go with Generic support

Installation

go get github.com/jinwoo1225/go-prioritize

Usage

package main

import (
    "fmt"
    "github.com/jinwoo1225/go-prioritize"
)

func main() {
    // Create a new priority queue
    pq := prioritize.NewPriorityQueue[string](nil)

    // Add some items
    pq.Push(prioritize.NewItem[string]("foo", 1))
    pq.Push(prioritize.NewItem[string]("bar", 2))
    pq.Push(prioritize.NewItem[string]("baz", 3))

    // Pop the highest priority item
    item := pq.Pop()
    fmt.Println(item) // "baz"

    // Pop the next highest priority item
    item = pq.Pop()
    fmt.Println(item) // "bar"

    // Pop the next highest priority item
    item = pq.Pop()
    fmt.Println(item) // "foo"

    // Pop the next highest priority item
    item = pq.Pop()
    fmt.Println(item) // nil (queue is empty)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item[T any] struct {
	// The Value of the item; arbitrary.
	Value T
	// The Priority of the item in the queue.
	Priority int64
	// contains filtered or unexported fields
}

Item for generic support

func NewItem

func NewItem[T any](value T, priority int64) Item[T]

type PriorityQueue

type PriorityQueue[T any] struct {
	// contains filtered or unexported fields
}

func NewPriorityQueue

func NewPriorityQueue[T any](items []Item[T]) *PriorityQueue[T]

func (*PriorityQueue[T]) Peek

func (h *PriorityQueue[T]) Peek() Item[T]

func (*PriorityQueue[T]) Pop

func (h *PriorityQueue[T]) Pop() Item[T]

func (*PriorityQueue[T]) Push

func (h *PriorityQueue[T]) Push(x Item[T])

func (*PriorityQueue[T]) Range

func (h *PriorityQueue[T]) Range() []Item[T]

func (*PriorityQueue[T]) Update

func (h *PriorityQueue[T]) Update(item Item[T], value T, priority int64)

type PriorityQueuer

type PriorityQueuer[T any] interface {
	Pop() Item[T]
	Push(x Item[T])
	Update(item Item[T], value T, priority int64)
	Peek() Item[T]
	Range() []Item[T]
}

Jump to

Keyboard shortcuts

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