priorityqueue

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2025 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item[T any] struct {
	Value    T   // The value of the item
	Priority int // The priority of the item
	Index    int // The index of the item in the heap
}

Item represents an item in the priority queue

type PriorityQueue

type PriorityQueue[T any] []*Item[T]

PriorityQueue implements heap.Interface and holds Items

Example
// Create a priority queue
pq := NewPriorityQueue[string]()

// Add some items
pq.Enqueue("Low priority", 1)
pq.Enqueue("High priority", 10)
pq.Enqueue("Medium priority", 5)

// Process the queue
for pq.Len() > 0 {
	value, priority, _ := pq.Dequeue()
	fmt.Printf("%s (priority: %d)\n", value, priority)
}
Output:

High priority (priority: 10)
Medium priority (priority: 5)
Low priority (priority: 1)

func NewPriorityQueue

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

NewPriorityQueue creates a new priority queue

func (*PriorityQueue[T]) Dequeue

func (pq *PriorityQueue[T]) Dequeue() (T, int, bool)

Dequeue removes and returns the highest priority item

func (*PriorityQueue[T]) Enqueue

func (pq *PriorityQueue[T]) Enqueue(value T, priority int) *Item[T]

Enqueue adds an item to the priority queue

func (PriorityQueue[T]) Len

func (pq PriorityQueue[T]) Len() int

func (PriorityQueue[T]) Less

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

func (*PriorityQueue[T]) Peek

func (pq *PriorityQueue[T]) Peek() (*Item[T], bool)

Peek returns the highest priority item without removing it

func (*PriorityQueue[T]) Pop

func (pq *PriorityQueue[T]) Pop() interface{}

func (*PriorityQueue[T]) Push

func (pq *PriorityQueue[T]) Push(x interface{})

func (PriorityQueue[T]) Swap

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

func (*PriorityQueue[T]) Update

func (pq *PriorityQueue[T]) Update(item *Item[T], value T, priority int) error

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

type ThreadSafePriorityQueue

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

ThreadSafePriorityQueue wraps a PriorityQueue with a mutex for thread safety

func NewThreadSafePriorityQueue

func NewThreadSafePriorityQueue[T any]() *ThreadSafePriorityQueue[T]

NewThreadSafePriorityQueue creates a new thread-safe priority queue

func (*ThreadSafePriorityQueue[T]) Dequeue

func (tspq *ThreadSafePriorityQueue[T]) Dequeue() (T, int, bool)

Dequeue removes and returns the highest priority item

func (*ThreadSafePriorityQueue[T]) Enqueue

func (tspq *ThreadSafePriorityQueue[T]) Enqueue(value T, priority int) *Item[T]

Enqueue adds an item to the priority queue

func (*ThreadSafePriorityQueue[T]) Items

func (tspq *ThreadSafePriorityQueue[T]) Items() []*Item[T]

returns all items in the queue

func (*ThreadSafePriorityQueue[T]) Len

func (tspq *ThreadSafePriorityQueue[T]) Len() int

Read-only operations use RLock/RUnlock

func (*ThreadSafePriorityQueue[T]) Peek

func (tspq *ThreadSafePriorityQueue[T]) Peek() (*Item[T], bool)

Peek returns the highest priority item without removing it

func (*ThreadSafePriorityQueue[T]) Update

func (tspq *ThreadSafePriorityQueue[T]) Update(item *Item[T], value T, priority int) error

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