Documentation
¶
Index ¶
- type Item
- type PriorityQueue
- func (pq *PriorityQueue[T]) Dequeue() (T, int, bool)
- func (pq *PriorityQueue[T]) Enqueue(value T, priority int) *Item[T]
- func (pq PriorityQueue[T]) Len() int
- func (pq PriorityQueue[T]) Less(i, j int) bool
- func (pq *PriorityQueue[T]) Peek() (*Item[T], bool)
- func (pq *PriorityQueue[T]) Pop() interface{}
- func (pq *PriorityQueue[T]) Push(x interface{})
- func (pq PriorityQueue[T]) Swap(i, j int)
- func (pq *PriorityQueue[T]) Update(item *Item[T], value T, priority int) error
- type ThreadSafePriorityQueue
- func (tspq *ThreadSafePriorityQueue[T]) Dequeue() (T, int, bool)
- func (tspq *ThreadSafePriorityQueue[T]) Enqueue(value T, priority int) *Item[T]
- func (tspq *ThreadSafePriorityQueue[T]) Items() []*Item[T]
- func (tspq *ThreadSafePriorityQueue[T]) Len() int
- func (tspq *ThreadSafePriorityQueue[T]) Peek() (*Item[T], bool)
- func (tspq *ThreadSafePriorityQueue[T]) Update(item *Item[T], value T, priority int) error
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 ¶
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)
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