priorityqueue

package
v0.0.0-...-a479826 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package priorityqueue implements a priority queue (binary heap).

In computer science, a priority queue is an abstract data type similar to a regular queue or stack abstract data type. In a priority queue, each element has an associated priority, which determines its order of service. Priority queue serves highest priority items first. Priority values have to be instances of an ordered data type, and higher priority can be given either to the lesser or to the greater values with respect to the given order relation. For example, in Java standard library, PriorityQueue's the least elements with respect to the order have the highest priority. This implementation detail is without much practical significance, since passing to the opposite order relation turns the least values into the greatest, and vice versa.

Reference: https://en.wikipedia.org/wiki/Priority_queue

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Element

type Element[T any] struct {
	Value T // the value stored with this element
	// contains filtered or unexported fields
}

Element is an element of a priority queue.

func (*Element[T]) Index

func (e *Element[T]) Index() int

Index returns the index of this element in queue.

type Queue

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

Queue represents an priority queue which holds the elements in a slice.

func New

func New[T cmp.Ordered](v ...T) *Queue[T]

New returns an initialized priority queue with cmp.Less as the less function and the given values v added.

func NewFunc

func NewFunc[T any](less container.Less[T], v ...T) *Queue[T]

NewFunc returns an initialized priority queue with the given function less as the less function and the given values v added.

func (*Queue[T]) Clear

func (q *Queue[T]) Clear()

Clear removes all elements in queue.

func (*Queue[T]) Dequeue

func (q *Queue[T]) Dequeue() (value T, ok bool)

Dequeue removes the first element if exists in queue and returns it. The ok result indicates whether such element was removed from queue.

func (*Queue[T]) Elements

func (q *Queue[T]) Elements() []*Element[T]

Elements returns the underlying elements slice of queue.

func (*Queue[T]) Enqueue

func (q *Queue[T]) Enqueue(v T) *Element[T]

Enqueue adds the value v to the end of queue.

Example
package main

import (
	"fmt"

	"github.com/docodex/gopkg/container/queue/priorityqueue"
)

func main() {
	h := priorityqueue.NewFunc(func(a, b int) bool {
		return a > b
	})
	h.Enqueue(3)
	h.Enqueue(5)
	h.Enqueue(1)
	h.Enqueue(2)
	v, _ := h.Peek()
	fmt.Printf("maximum: %d\n", v)
	for h.Len() > 0 {
		v, _ := h.Dequeue()
		fmt.Printf("%d ", v)
	}

}
Output:

maximum: 5
5 3 2 1

func (*Queue[T]) Fix

func (q *Queue[T]) Fix(i int)

Fix re-establishes queue ordering after the element at index i has changed its value. Changing the value of the element at index i and then calling Fix is equivalent to, but less expensive than, calling Queue.Remove followed by a Queue.Enqueue of the new value. The complexity is O(log n) where n = h.Len().

func (*Queue[T]) Len

func (q *Queue[T]) Len() int

Len returns the number of elements of queue q. The complexity is O(1).

func (*Queue[T]) MarshalJSON

func (q *Queue[T]) MarshalJSON() ([]byte, error)

MarshalJSON marshals queue into valid JSON. Ref: std json.Marshaler.

func (*Queue[T]) Peek

func (q *Queue[T]) Peek() (value T, ok bool)

Peek returns the first element if exists in queue without removing it. The ok result indicates whether such element was found in queue.

func (*Queue[T]) Remove

func (q *Queue[T]) Remove(i int) (value T, ok bool)

Remove removes and returns the element at index i from queue. The complexity is O(log n) where n = h.Len().

func (*Queue[T]) String

func (q *Queue[T]) String() string

String returns the string representation of queue. Ref: std fmt.Stringer.

func (*Queue[T]) UnmarshalJSON

func (q *Queue[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals a JSON description of queue. The input can be assumed to be a valid encoding of a JSON value. UnmarshalJSON must copy the JSON data if it wishes to retain the data after returning. Ref: std json.Unmarshaler.

func (*Queue[T]) Update

func (q *Queue[T]) Update(i int, v T)

Update updates the element value to v at index i, and re-establishes heap ordering. Queue.Update is equivalent to, but less expensive than, calling Queue.Remove followed by a Queue.Enqueue of the new value. The complexity is O(log n) where n = h.Len().

Example
package main

import (
	"fmt"

	"github.com/docodex/gopkg/container/queue/priorityqueue"
)

func main() {
	// Some items and their priorities.
	items := map[string]int{
		"apple": 2, "pear": 4,
	}

	// An Item is something we manage in a priority queue.
	type Item struct {
		value    string // The value of the item; arbitrary.
		priority int    // The priority of the item in queue.
	}

	// Create a priority queue, put the items in it, and
	// establish the priority queue (heap) invariants.
	q := priorityqueue.NewFunc(func(a, b *Item) bool {
		return a.priority > b.priority
	})
	for value, priority := range items {
		q.Enqueue(&Item{
			value:    value,
			priority: priority,
		})
	}

	// Insert a new item and then modify its priority.
	orange := q.Enqueue(&Item{
		value:    "orange",
		priority: 1,
	})
	orange.Value.priority = 5
	q.Fix(orange.Index())

	// Insert a new item and then update its priority.
	banana := q.Enqueue(&Item{
		value:    "banana",
		priority: 3,
	})
	q.Update(banana.Index(), &Item{
		value:    "banana",
		priority: 1,
	})

	// Take the items out; they arrive in decreasing priority order.
	for q.Len() > 0 {
		item, _ := q.Dequeue()
		fmt.Printf("%.2d:%s ", item.priority, item.value)
	}
}
Output:

05:orange 04:pear 02:apple 01:banana

func (*Queue[T]) Values

func (q *Queue[T]) Values() []T

Values returns all values in queue (in Queue.Dequeue order).

Jump to

Keyboard shortcuts

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