pq

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2023 License: MIT Imports: 1 Imported by: 0

README

Go Priority Queue

Copyright © 2023 kk-min

Generic priority queue implementation for Go.

Usage

type CustomItem struct{
	Key string
	Val int
}

// Create a PriorityQueue of type CustomItem by passing in a "less" comparator function to CreatePQ:
q := pq.CreatePQ[CustomItem](func(a, b CustomItem) bool { return a.Val < b.Val })

// Put items in the Priority Queue:
q.Put(CustomItem{"a", 5})
q.Put(CustomItem{"b", 1})

// Peek the head of the queue:
q.Peek() // CustomItem{"b", 1}

// Get (Dequeue) the head of the queue:
q.Get() // CustomItem{"b", 1}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LessFunction

type LessFunction[T comparable] func(T, T) bool

Generic comparator function that is passed as a parameter when creating a new PQ.

type PQ

type PQ[T comparable] struct {
	Items []*T
	// contains filtered or unexported fields
}

Internally maintains a slice of pointers to items of type T.

func CreatePQ

func CreatePQ[T comparable](less LessFunction[T]) *PQ[T]

Creates a new instance of a priority queue with the given comparator function.

func (*PQ[T]) Get

func (pq *PQ[T]) Get() T

Removes and returns the top item from the priority queue.

func (*PQ[T]) Len

func (pq *PQ[T]) Len() int

Returns the size of the priority queue.

func (*PQ[T]) Less

func (pq *PQ[T]) Less(i, j int) bool

For implementing the heap interface; **do not use**.

func (*PQ[T]) Peek

func (pq *PQ[T]) Peek() T

Returns the top item from the priority queue without removing it.

func (*PQ[T]) Pop

func (pq *PQ[T]) Pop() any

For implementing the heap interface; **do not use**.

func (*PQ[T]) Push

func (pq *PQ[T]) Push(item any)

For implementing the heap interface; **do not use**.

func (*PQ[T]) Put

func (pq *PQ[T]) Put(item T)

Adds an item to the priority queue.

func (*PQ[T]) Swap

func (pq *PQ[T]) Swap(i, j int)

For implementing the heap interface; **do not use**.

Jump to

Keyboard shortcuts

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