Documentation
¶
Overview ¶
Package heap provides a generic, type-safe heap implementation.
Index ¶
- type Heap
- type HeapWrapper
- type ThreadSafeHeap
- func (tsh *ThreadSafeHeap[T]) IsEmpty() bool
- func (tsh *ThreadSafeHeap[T]) Len() int
- func (tsh *ThreadSafeHeap[T]) Peek() T
- func (tsh *ThreadSafeHeap[T]) Pop() T
- func (tsh *ThreadSafeHeap[T]) Push(item T)
- func (tsh *ThreadSafeHeap[T]) TryPeek() (T, bool)
- func (tsh *ThreadSafeHeap[T]) TryPop() (T, bool)
- func (tsh *ThreadSafeHeap[T]) WithLock(fn func(*Heap[T]))
- func (tsh *ThreadSafeHeap[T]) WithRLock(fn func(*Heap[T]))
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Heap ¶
type Heap[T any] struct { // contains filtered or unexported fields }
Heap is a generic min-heap data structure. The heap property is maintained using a user-provided comparison function. For a min-heap, the function should return true if the first argument is less than the second.
The zero value is not usable; use NewHeap to create a new instance.
This implementation is not thread-safe. For concurrent use, see ThreadSafeHeap.
func NewHeap ¶
NewHeap creates a new heap with the given comparison function. For a min-heap, lessFunc should return true if i < j. For a max-heap, lessFunc should return true if i > j.
Example:
minHeap := NewHeap(func(a, b int) bool { return a < b })
maxHeap := NewHeap(func(a, b int) bool { return a > b })
func (*Heap[T]) Peek ¶
func (h *Heap[T]) Peek() T
Peek returns the minimum element without removing it from the heap. Panics if the heap is empty. Time complexity: O(1)
type HeapWrapper ¶
HeapWrapper wraps a type-safe Heap[T] to implement heap.Interface. This allows using the standard library's heap functions while maintaining type safety at the wrapper level.
Note: The wrapper still requires type assertions when using heap.Push/heap.Pop from the standard library, but provides a bridge for interoperability.
func NewHeapWrapper ¶
func NewHeapWrapper[T any](lessFunc func(i, j T) bool) *HeapWrapper[T]
NewHeapWrapper creates a new wrapper around a type-safe heap that implements heap.Interface.
func WrapHeap ¶
func WrapHeap[T any](h *Heap[T]) *HeapWrapper[T]
WrapHeap wraps an existing type-safe heap to implement heap.Interface.
func (*HeapWrapper[T]) Fix ¶
func (hw *HeapWrapper[T]) Fix(i int)
Fix re-establishes the heap ordering after the element at index i has changed its value.
func (*HeapWrapper[T]) Init ¶
func (hw *HeapWrapper[T]) Init()
Init initializes the heap using the standard library's heap.Init function. This is useful if you've manipulated the underlying slice directly.
func (*HeapWrapper[T]) Less ¶
func (hw *HeapWrapper[T]) Less(i, j int) bool
Less reports whether the element with index i should sort before index j. This satisfies sort.Interface (embedded in heap.Interface).
func (*HeapWrapper[T]) Pop ¶
func (hw *HeapWrapper[T]) Pop() any
Pop removes and returns the last element from the slice. This is the low-level interface method - use heap.Pop() for proper heap semantics. This satisfies heap.Interface.
func (*HeapWrapper[T]) Push ¶
func (hw *HeapWrapper[T]) Push(x any)
Push adds x to the heap at the end of the slice. This is the low-level interface method - use heap.Push() for proper heap semantics. This satisfies heap.Interface.
func (*HeapWrapper[T]) Remove ¶
func (hw *HeapWrapper[T]) Remove(i int) T
Remove removes and returns the element at index i from the heap.
func (*HeapWrapper[T]) Swap ¶
func (hw *HeapWrapper[T]) Swap(i, j int)
Swap swaps the elements with indexes i and j. This satisfies sort.Interface (embedded in heap.Interface).
type ThreadSafeHeap ¶
type ThreadSafeHeap[T any] struct { // contains filtered or unexported fields }
ThreadSafeHeap wraps a Heap[T] with a RWMutex to provide thread-safe operations. It maintains the same API as Heap[T] but is safe for concurrent use by multiple goroutines. Uses optimizations for read-heavy workloads.
func NewThreadSafeHeap ¶
func NewThreadSafeHeap[T any](lessFunc func(i, j T) bool) *ThreadSafeHeap[T]
NewThreadSafeHeap creates a new thread-safe heap with the given comparison function.
func WrapHeapThreadSafe ¶
func WrapHeapThreadSafe[T any](h *Heap[T]) *ThreadSafeHeap[T]
WrapHeapThreadSafe wraps an existing heap to make it thread-safe. Note: After wrapping, the original heap should not be used directly to avoid race conditions.
func (*ThreadSafeHeap[T]) IsEmpty ¶
func (tsh *ThreadSafeHeap[T]) IsEmpty() bool
IsEmpty returns true if the heap is empty in a thread-safe manner. This uses the lock-free Len() method.
func (*ThreadSafeHeap[T]) Len ¶
func (tsh *ThreadSafeHeap[T]) Len() int
Len returns the number of elements in the heap in a thread-safe manner. This uses a cached value for lock-free reads.
func (*ThreadSafeHeap[T]) Peek ¶
func (tsh *ThreadSafeHeap[T]) Peek() T
Peek returns the minimum element without removing it in a thread-safe manner. Panics if the heap is empty.
func (*ThreadSafeHeap[T]) Pop ¶
func (tsh *ThreadSafeHeap[T]) Pop() T
Pop removes and returns the minimum element from the heap in a thread-safe manner. Panics if the heap is empty.
func (*ThreadSafeHeap[T]) Push ¶
func (tsh *ThreadSafeHeap[T]) Push(item T)
Push adds an item to the heap in a thread-safe manner.
func (*ThreadSafeHeap[T]) TryPeek ¶
func (tsh *ThreadSafeHeap[T]) TryPeek() (T, bool)
TryPeek attempts to peek at the minimum element. Returns the element and true if successful, zero value and false if empty. This is useful to avoid panics when the heap might be empty.
func (*ThreadSafeHeap[T]) TryPop ¶
func (tsh *ThreadSafeHeap[T]) TryPop() (T, bool)
TryPop attempts to pop an element from the heap. Returns the element and true if successful, zero value and false if empty. This is useful to avoid panics when the heap might be empty.
func (*ThreadSafeHeap[T]) WithLock ¶
func (tsh *ThreadSafeHeap[T]) WithLock(fn func(*Heap[T]))
WithLock provides exclusive access to the underlying heap for batch operations. The provided function is executed while holding the mutex. This is useful for performing multiple operations atomically.
Example:
tsh.WithLock(func(h *Heap[int]) {
h.Push(1)
h.Push(2)
// Both pushes happen atomically
})
func (*ThreadSafeHeap[T]) WithRLock ¶
func (tsh *ThreadSafeHeap[T]) WithRLock(fn func(*Heap[T]))
WithRLock provides read-only access to the underlying heap. The provided function is executed while holding the read mutex. This is useful for performing multiple read operations efficiently.
Example:
var min, len int
tsh.WithRLock(func(h *Heap[int]) {
min = h.Peek()
len = h.Len()
// Both reads happen under the same lock
})